aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoão Távora2019-01-25 13:04:13 +0000
committerJoão Távora2019-01-25 17:32:18 +0000
commit682ab5d92a6922694a92fcde029811bccf98d700 (patch)
tree8ae6bace81652a4feff7e5abdb14890e1665f028
parentf845f8a279cfc2acd1051b4cd4924e2aede54017 (diff)
downloademacs-682ab5d92a6922694a92fcde029811bccf98d700.tar.gz
emacs-682ab5d92a6922694a92fcde029811bccf98d700.zip
Adjust previous electric.el and elec-pair.el change
This fixes a serious bug introduced previously electric-pair-inhibit-if-helps-balance and electric-pair-skip-if-helps-balance, whereby "innocent" markers were being pushed by those function's new save-change-and-restore semantics. The fix can probably still be improved. It also adds comments to parts of the code, where deemed necessary. * lisp/elec-pair.el (electric-pair--insert): Add comment. (electric-pair--save-literal-point-excursion): New helper macro. (electric-pair-inhibit-if-helps-balance) (electric-pair-skip-if-helps-balance): Don't use insert-before-markers since it may hurt other markers that have nothing to do with the 'save-excursion'. (electric-pair-post-self-insert-function): Use electric-pair--save-literal-point-excursion. * lisp/electric.el (electric-indent-post-self-insert-function): Remove lexical variable.
-rw-r--r--lisp/elec-pair.el28
-rw-r--r--lisp/electric.el10
2 files changed, 28 insertions, 10 deletions
diff --git a/lisp/elec-pair.el b/lisp/elec-pair.el
index 20581ad6573..b5ec492930e 100644
--- a/lisp/elec-pair.el
+++ b/lisp/elec-pair.el
@@ -228,6 +228,12 @@ inside a comment or string."
228 (let ((last-command-event char) 228 (let ((last-command-event char)
229 (blink-matching-paren nil) 229 (blink-matching-paren nil)
230 (electric-pair-mode nil) 230 (electric-pair-mode nil)
231 ;; When adding the "closer" delimiter, a job his function is
232 ;; frequently used for, we don't want to munch any extra
233 ;; newlines above us. That would be the default behaviour of
234 ;; `electric-layout-mode', which potentially kicked in before
235 ;; us to add these newlines, and is probably about to kick in
236 ;; again after we add the closer.
231 (electric-layout-allow-duplicate-newlines t)) 237 (electric-layout-allow-duplicate-newlines t))
232 (self-insert-command 1))) 238 (self-insert-command 1)))
233 239
@@ -406,6 +412,15 @@ strings."
406 (let ((ppss (electric-pair--syntax-ppss (point) '(comment)))) 412 (let ((ppss (electric-pair--syntax-ppss (point) '(comment))))
407 (memq (nth 3 ppss) (list t char)))) 413 (memq (nth 3 ppss) (list t char))))
408 414
415(defmacro electric-pair--save-literal-point-excursion (&rest body)
416 ;; FIXME: need this instead of `save-excursion' when functions in
417 ;; BODY, such as `electric-pair-inhibit-if-helps-balance' and
418 ;; `electric-pair-skip-if-helps-balance' modify and restore the
419 ;; buffer in a way that modifies the marker used by save-excursion.
420 (let ((point (make-symbol "point")))
421 `(let ((,point (point)))
422 (unwind-protect (progn ,@body) (goto-char ,point)))))
423
409(defun electric-pair-inhibit-if-helps-balance (char) 424(defun electric-pair-inhibit-if-helps-balance (char)
410 "Return non-nil if auto-pairing of CHAR would hurt parentheses' balance. 425 "Return non-nil if auto-pairing of CHAR would hurt parentheses' balance.
411 426
@@ -427,7 +442,7 @@ happened."
427 (eq (cdr outermost) pair))))) 442 (eq (cdr outermost) pair)))))
428 ((eq syntax ?\") 443 ((eq syntax ?\")
429 (electric-pair--unbalanced-strings-p char)))) 444 (electric-pair--unbalanced-strings-p char))))
430 (insert-before-markers char))))) 445 (insert char)))))
431 446
432(defun electric-pair-skip-if-helps-balance (char) 447(defun electric-pair-skip-if-helps-balance (char)
433 "Return non-nil if skipping CHAR would benefit parentheses' balance. 448 "Return non-nil if skipping CHAR would benefit parentheses' balance.
@@ -452,7 +467,7 @@ happened."
452 (not (eq (cdr outermost) pair))))))) 467 (not (eq (cdr outermost) pair)))))))
453 ((eq syntax ?\") 468 ((eq syntax ?\")
454 (electric-pair--inside-string-p char)))) 469 (electric-pair--inside-string-p char))))
455 (insert-before-markers char))))) 470 (insert char)))))
456 471
457(defun electric-pair-default-skip-self (char) 472(defun electric-pair-default-skip-self (char)
458 (if electric-pair-preserve-balance 473 (if electric-pair-preserve-balance
@@ -498,7 +513,7 @@ happened."
498 ((and (memq syntax '(?\) ?\" ?\$)) 513 ((and (memq syntax '(?\) ?\" ?\$))
499 (and (or unconditional 514 (and (or unconditional
500 (if (functionp electric-pair-skip-self) 515 (if (functionp electric-pair-skip-self)
501 (save-excursion 516 (electric-pair--save-literal-point-excursion
502 (goto-char pos) 517 (goto-char pos)
503 (funcall electric-pair-skip-self last-command-event)) 518 (funcall electric-pair-skip-self last-command-event))
504 electric-pair-skip-self)) 519 electric-pair-skip-self))
@@ -527,7 +542,7 @@ happened."
527 ((and (memq syntax '(?\( ?\" ?\$)) 542 ((and (memq syntax '(?\( ?\" ?\$))
528 (not overwrite-mode) 543 (not overwrite-mode)
529 (or unconditional 544 (or unconditional
530 (not (save-excursion 545 (not (electric-pair--save-literal-point-excursion
531 (goto-char pos) 546 (goto-char pos)
532 (funcall electric-pair-inhibit-predicate 547 (funcall electric-pair-inhibit-predicate
533 last-command-event))))) 548 last-command-event)))))
@@ -544,6 +559,11 @@ happened."
544 (matching-paren (char-after)))) 559 (matching-paren (char-after))))
545 (save-excursion (newline 1 t))))))) 560 (save-excursion (newline 1 t)))))))
546 561
562;; Prioritize this to kick in after
563;; `electric-layout-post-self-insert-function': that considerably
564;; simplifies interoperation when `electric-pair-mode',
565;; `electric-layout-mode' and `electric-indent-mode' are used
566;; together. Use `vc-region-history' on these lines for more info.
547(put 'electric-pair-post-self-insert-function 'priority 50) 567(put 'electric-pair-post-self-insert-function 'priority 50)
548 568
549(defun electric-pair-will-use-region () 569(defun electric-pair-will-use-region ()
diff --git a/lisp/electric.el b/lisp/electric.el
index b9458776e3b..657913a3961 100644
--- a/lisp/electric.el
+++ b/lisp/electric.el
@@ -270,14 +270,12 @@ or comment."
270 ;; hence copied). 270 ;; hence copied).
271 (let ((at-newline (<= pos (line-beginning-position)))) 271 (let ((at-newline (<= pos (line-beginning-position))))
272 (when at-newline 272 (when at-newline
273 (let ((before (copy-marker (1- pos) t)) 273 (let ((before (copy-marker (1- pos) t)))
274 inhibit-reindentation)
275 (save-excursion 274 (save-excursion
276 (unless 275 (unless
277 (setq inhibit-reindentation 276 (or (memq indent-line-function
278 (or (memq indent-line-function 277 electric-indent-functions-without-reindent)
279 electric-indent-functions-without-reindent) 278 electric-indent-inhibit)
280 electric-indent-inhibit))
281 ;; Don't reindent the previous line if the 279 ;; Don't reindent the previous line if the
282 ;; indentation function is not a real one. 280 ;; indentation function is not a real one.
283 (goto-char before) 281 (goto-char before)