From 102626e2259a927c563187d8ce3393d54a1a6b35 Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Wed, 19 Jun 2013 00:42:32 -0400 Subject: [PATCH] * lisp/electric.el (electric-pair-mode): Improve interaction with electric-layout-mode. (electric-pair-default-inhibit): Don't assume (eq char (char-before)). (electric-pair-syntax): Use text-mode-syntax-table in comments and strings. (electric-pair--insert): New function. (electric-pair-post-self-insert-function): Use it and electric--after-char-pos. --- lisp/ChangeLog | 15 ++++++++++++-- lisp/electric.el | 54 +++++++++++++++++++++++++++++------------------- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 4add465d0c..8508ef44b3 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,14 @@ +2013-06-19 Stefan Monnier + + * electric.el (electric-pair-mode): Improve interaction with + electric-layout-mode. + (electric-pair-default-inhibit): Don't assume (eq char (char-before)). + (electric-pair-syntax): Use text-mode-syntax-table in comments + and strings. + (electric-pair--insert): New function. + (electric-pair-post-self-insert-function): Use it and + electric--after-char-pos. + 2013-06-19 Leo Liu * progmodes/octave.el (octave-help): Fix regexp. @@ -19,8 +30,8 @@ (eww-handle-link, eww-tag-link, eww-tag-a): New defuns. (eww-mode-map): Move "p" to "l". Bind "p", "n", "t", and "u". (eww-back-url): Rename from eww-previous-url. - (eww-next-url, eww-previous-url, eww-up-url, eww-top-url): New - defuns. + (eww-next-url, eww-previous-url, eww-up-url, eww-top-url): + New defuns. 2013-06-18 Dmitry Gutov diff --git a/lisp/electric.el b/lisp/electric.el index 86997d4aac..351468fd75 100644 --- a/lisp/electric.el +++ b/lisp/electric.el @@ -319,51 +319,63 @@ closer." ;; 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)))) + ;; 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) - (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)))))) + (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* ((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. + (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)) - (if (> (mark) (point)) - (goto-char (mark)) + ;; 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-char -1) + (delete-region (1- pos) (point)) (save-excursion (goto-char (mark)) - ;; Do not insert after `save-excursion' marker (Bug#11520). - (insert-before-markers last-command-event))) + (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- (point))) + (goto-char (1- pos)) (not (zerop (% (skip-syntax-backward "\\") 2)))) nil) ;; Skip self. ((and (memq syntax '(?\) ?\" ?\$)) electric-pair-skip-self - (eq (char-after) last-command-event)) + (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 @@ -373,7 +385,7 @@ closer." ((not (or (not (memq syntax `(?\( ?\" ?\$))) overwrite-mode (funcall electric-pair-inhibit-predicate last-command-event))) - (save-excursion (insert closer)))))) + (save-excursion (electric-pair--insert closer)))))) (defun electric-pair-will-use-region () (and (use-region-p) -- 2.20.1