diff options
author | João Távora <joaotavora@gmail.com> | 2019-01-22 15:46:56 +0000 |
---|---|---|
committer | João Távora <joaotavora@gmail.com> | 2019-01-22 16:42:43 +0000 |
commit | fd943124439b7644392919bca8bc2a77e6316d92 (patch) | |
tree | 22fa8a1103239088dbd7dae9cd0c5974e5f72ebc /lisp/elec-pair.el | |
parent | 6ca4626c9fe33f78df685b4df7aca88abb272118 (diff) | |
download | emacs-fd943124439b7644392919bca8bc2a77e6316d92.tar.gz |
electric-layout-mode kicks in before electric-pair-mode
This aims to solve problems with indentation. Previously in, say, a
js-mode buffer with electric-layout-rules set to
(?\{ before after)
(?\} before)
would produce an intended:
function ()
{
<indented point>
}
The initial state
function () {
Would go immediately to the following by e-p-m
function () {}
Only then would e-l-m be applied to } first, and then again to {.
This makes lines indent in the wrong order, which can be a problem in
some modes.
The way we fix this is by reversing the order of e-p-m and e-l-m in
the post-self-insert-hook (and also fixing a number of details that
this uncovered). In the end this changes the sequence from
function () {
By way of e-l-m becomes:
function () <newline>
{
<newline>
The e-p-m inserts the pair
function () <newline>
{
<newline>}
And then e-l-m kicks in for the pair again, yielding the desired result
function () <newline>
{
<indented point>
}
* lisp/elec-pair.el (electric-pair--insert): Bind
electric-layout-no-duplicate-newlines.
(electric-pair-inhibit-if-helps-balance)
(electric-pair-skip-if-helps-balance): Use insert-before-markers,
playing nice with save-excurion.
(electric-pair-post-self-insert-function): Go to correct position
before checking electric-pair-inhibit-predicate and
electric-pair-skip-self predicate.
(electric-pair-post-self-insert-function): Increase priority to
50.
* lisp/electric.el (electric-indent-post-self-insert-function):
Delete trailing space in reindented line only if line was
really reindented. Rewrite comment.
(electric-layout-allow-duplicate-newlines): New variable.
(electric-layout-post-self-insert-function-1): Rewrite comments.
Honours electric-layout-allow-duplicate-newlines. Don't reindent
previous line because racecar.
* test/lisp/electric-tests.el: New test.
(plainer-c-mode): Move up.
(electric-modes-int-main-allman-style)
(electric-layout-int-main-kernel-style): Simplify
electric-layout-rules.
(electric-layout-for-c-style-du-jour): New helper.
(electric-layout-plainer-c-mode-use-c-style): New test.
Diffstat (limited to 'lisp/elec-pair.el')
-rw-r--r-- | lisp/elec-pair.el | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/lisp/elec-pair.el b/lisp/elec-pair.el index 2c5553e8dab..20581ad6573 100644 --- a/lisp/elec-pair.el +++ b/lisp/elec-pair.el @@ -227,7 +227,8 @@ inside a comment or string." (defun electric-pair--insert (char) (let ((last-command-event char) (blink-matching-paren nil) - (electric-pair-mode nil)) + (electric-pair-mode nil) + (electric-layout-allow-duplicate-newlines t)) (self-insert-command 1))) (cl-defmacro electric-pair--with-uncached-syntax ((table &optional start) &rest body) @@ -426,11 +427,10 @@ happened." (eq (cdr outermost) pair))))) ((eq syntax ?\") (electric-pair--unbalanced-strings-p char)))) - (insert-char char))))) + (insert-before-markers char))))) (defun electric-pair-skip-if-helps-balance (char) "Return non-nil if skipping CHAR would benefit parentheses' balance. - Works by first removing the character from the buffer, then doing some list calculations, finally restoring the situation as if nothing happened." @@ -452,7 +452,7 @@ happened." (not (eq (cdr outermost) pair))))))) ((eq syntax ?\") (electric-pair--inside-string-p char)))) - (insert-char char))))) + (insert-before-markers char))))) (defun electric-pair-default-skip-self (char) (if electric-pair-preserve-balance @@ -498,7 +498,9 @@ happened." ((and (memq syntax '(?\) ?\" ?\$)) (and (or unconditional (if (functionp electric-pair-skip-self) - (funcall electric-pair-skip-self last-command-event) + (save-excursion + (goto-char pos) + (funcall electric-pair-skip-self last-command-event)) electric-pair-skip-self)) (save-excursion (when (and (not (and unconditional @@ -525,8 +527,10 @@ happened." ((and (memq syntax '(?\( ?\" ?\$)) (not overwrite-mode) (or unconditional - (not (funcall electric-pair-inhibit-predicate - last-command-event)))) + (not (save-excursion + (goto-char pos) + (funcall electric-pair-inhibit-predicate + last-command-event))))) (save-excursion (electric-pair--insert pair))))) (_ (when (and (if (functionp electric-pair-open-newline-between-pairs) @@ -540,7 +544,7 @@ happened." (matching-paren (char-after)))) (save-excursion (newline 1 t))))))) -(put 'electric-pair-post-self-insert-function 'priority 20) +(put 'electric-pair-post-self-insert-function 'priority 50) (defun electric-pair-will-use-region () (and (use-region-p) |