]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/elec-pair.el: Do modify+undo more carefully
authorStefan Monnier <monnier@iro.umontreal.ca>
Tue, 19 Feb 2019 00:00:44 +0000 (19:00 -0500)
committerStefan Monnier <monnier@iro.umontreal.ca>
Tue, 19 Feb 2019 00:00:44 +0000 (19:00 -0500)
(electric-pair-inhibit-if-helps-balance): Use the undo system
instead of undoing by hand.

lisp/elec-pair.el

index b5ec492930e1a08e8375dcef53646994101c3dee..3be09d87b4f1a9b639edfc8a3a656b1bb65bb093 100644 (file)
@@ -429,20 +429,25 @@ some list calculations, finally restoring the situation as if nothing
 happened."
   (pcase (electric-pair-syntax-info char)
     (`(,syntax ,pair ,_ ,s-or-c)
-     (unwind-protect
-         (progn
-           (delete-char -1)
-           (cond ((eq ?\( syntax)
-                  (let* ((pair-data
-                          (electric-pair--balance-info 1 s-or-c))
-                         (outermost (cdr pair-data)))
-                    (cond ((car outermost)
-                           nil)
-                          (t
-                           (eq (cdr outermost) pair)))))
-                 ((eq syntax ?\")
-                  (electric-pair--unbalanced-strings-p char))))
-       (insert char)))))
+     (catch 'done
+       ;; FIXME: modify+undo is *very* tricky business.  We used to
+       ;; use `delete-char' followed by `insert', but this changed the
+       ;; position some markers.  The real fix would be to compute the
+       ;; result without having to modify the buffer at all.
+       (atomic-change-group
+         (delete-char -1)
+         (throw
+          'done
+          (cond ((eq ?\( syntax)
+                 (let* ((pair-data
+                         (electric-pair--balance-info 1 s-or-c))
+                        (outermost (cdr pair-data)))
+                   (cond ((car outermost)
+                          nil)
+                         (t
+                          (eq (cdr outermost) pair)))))
+                ((eq syntax ?\")
+                 (electric-pair--unbalanced-strings-p char)))))))))
 
 (defun electric-pair-skip-if-helps-balance (char)
   "Return non-nil if skipping CHAR would benefit parentheses' balance.