- #'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."
- :version "24.4"
- :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))
- ;; Don't pair up when we insert the second of "" or of ((.
- (and (eq char (char-before))
- (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)
- (let ((x (assq command-event electric-pair-pairs)))
- (cond
- (x (if (eq (car x) (cdr x)) ?\" ?\())
- ((rassq command-event electric-pair-pairs) ?\))
- ((nth 8 (syntax-ppss))
- (with-syntax-table text-mode-syntax-table (char-syntax command-event)))
- (t (char-syntax command-event)))))
-
-(defun electric-pair--insert (char)
- (let ((last-command-event char)
- (blink-matching-paren nil)
- (electric-pair-mode nil))
- (self-insert-command 1)))
-
-(defun electric-pair-post-self-insert-function ()
- (let* ((pos (and electric-pair-mode (electric--after-char-pos)))
- (syntax (and pos (electric-pair-syntax last-command-event)))
- (closer (if (eq syntax ?\()
- (cdr (or (assq last-command-event electric-pair-pairs)
- (aref (syntax-table) last-command-event)))
- last-command-event)))
- (cond
- ((null pos) nil)
- ;; Wrap a pair around the active region.
- ((and (memq syntax '(?\( ?\" ?\$)) (use-region-p))
- ;; FIXME: To do this right, we'd need a post-self-insert-function
- ;; so we could add-function around it and insert the closer after
- ;; all the rest of the hook has run.
- (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-region (1- pos) (point))
- (save-excursion
- (goto-char (mark))
- (electric-pair--insert last-command-event)))
- ;; Since we're right after the closer now, we could tell the rest of
- ;; post-self-insert-hook that we inserted `closer', but then we'd get
- ;; blink-paren to kick in, which is annoying.
- ;;(setq last-command-event closer)
- (insert closer))
- ;; Backslash-escaped: no pairing, no skipping.
- ((save-excursion
- (goto-char (1- pos))
- (not (zerop (% (skip-syntax-backward "\\") 2))))
- nil)
- ;; Skip self.
- ((and (memq syntax '(?\) ?\" ?\$))
- electric-pair-skip-self
- (eq (char-after pos) 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 (electric-pair--insert closer))))))
-
-(defun electric-pair-will-use-region ()
- (and (use-region-p)
- (memq (electric-pair-syntax last-command-event) '(?\( ?\" ?\$))))