- #'electric-indent-post-self-insert-function
- 'append)
- ;; FIXME: Ugly!
- (let ((bp (memq #'blink-paren-post-self-insert-function
- (default-value 'post-self-insert-hook))))
- (when (memq #'electric-indent-post-self-insert-function bp)
- (setcar bp #'electric-indent-post-self-insert-function)
- (setcdr bp (cons #'blink-paren-post-self-insert-function
- (delq #'electric-indent-post-self-insert-function
- (cdr bp))))))))
-
-;; Electric pairing.
-
-(defcustom electric-pair-pairs
- '((?\" . ?\"))
- "Alist of pairs that should be used regardless of major mode."
- :group 'electricity
- :version "24.1"
- :type '(repeat (cons character character)))
-
-(defcustom electric-pair-skip-self t
- "If non-nil, skip char instead of inserting a second closing paren.
-When inserting a closing paren character right before the same character,
-just skip that character instead, so that hitting ( followed by ) results
-in \"()\" rather than \"())\".
-This can be convenient for people who find it easier to hit ) than C-f."
- :group 'electricity
- :version "24.1"
- :type 'boolean)
-
-(defcustom electric-pair-inhibit-predicate
- #'electric-pair-default-inhibit
- "Predicate to prevent insertion of a matching pair.
-The function is called with a single char (the opening char just inserted).
-If it returns non-nil, then `electric-pair-mode' will not insert a matching
-closer."
- :type '(choice
- (const :tag "Default" electric-pair-default-inhibit)
- (const :tag "Always pair" ignore)
- function))
-
-(defun electric-pair-default-inhibit (char)
- (or
- ;; I find it more often preferable not to pair when the
- ;; same char is next.
- (eq char (char-after))
- (eq char (char-before (1- (point))))
- ;; I also find it often preferable not to pair next to a word.
- (eq (char-syntax (following-char)) ?w)))
-
-(defun electric-pair-syntax (command-event)
- (and electric-pair-mode
- (let ((x (assq command-event electric-pair-pairs)))
- (cond
- (x (if (eq (car x) (cdr x)) ?\" ?\())
- ((rassq command-event electric-pair-pairs) ?\))
- (t (char-syntax command-event))))))
-
-(defun electric-pair-post-self-insert-function ()
- (let* ((syntax (and (eq (char-before) last-command-event) ; Sanity check.
- (electric-pair-syntax last-command-event)))
- ;; FIXME: when inserting the closer, we should maybe use
- ;; self-insert-command, although it may prove tricky running
- ;; post-self-insert-hook recursively, and we wouldn't want to trigger
- ;; blink-matching-open.
- (closer (if (eq syntax ?\()
- (cdr (or (assq last-command-event electric-pair-pairs)
- (aref (syntax-table) last-command-event)))
- last-command-event)))
- (cond
- ;; Wrap a pair around the active region.
- ((and (memq syntax '(?\( ?\" ?\$)) (use-region-p))
- (if (> (mark) (point))
- (goto-char (mark))
- ;; We already inserted the open-paren but at the end of the
- ;; region, so we have to remove it and start over.
- (delete-char -1)
- (save-excursion
- (goto-char (mark))
- ;; Do not insert after `save-excursion' marker (Bug#11520).
- (insert-before-markers last-command-event)))
- (insert closer))
- ;; Backslash-escaped: no pairing, no skipping.
- ((save-excursion
- (goto-char (1- (point)))
- (not (zerop (% (skip-syntax-backward "\\") 2))))
- nil)
- ;; Skip self.
- ((and (memq syntax '(?\) ?\" ?\$))
- electric-pair-skip-self
- (eq (char-after) last-command-event))
- ;; This is too late: rather than insert&delete we'd want to only skip (or
- ;; insert in overwrite mode). The difference is in what goes in the
- ;; undo-log and in the intermediate state which might be visible to other
- ;; post-self-insert-hook. We'll just have to live with it for now.
- (delete-char 1))
- ;; Insert matching pair.
- ((not (or (not (memq syntax `(?\( ?\" ?\$)))
- overwrite-mode
- (funcall electric-pair-inhibit-predicate last-command-event)))
- (save-excursion (insert closer))))))
-
-(defun electric-pair-will-use-region ()
- (and (use-region-p)
- (memq (electric-pair-syntax last-command-event) '(?\( ?\" ?\$))))