Commit 5e88b50d authored by João Távora's avatar João Távora

Correctly reindent previous line in electric-indent-mode

Fixes: bug#35254

Do this even when electric-indent-inhibit is t, except when the
newline insertion is being performed by electric-layout-mode.

* lisp/electric.el (electric-indent-post-self-insert-function):
Reindent previous line unless operating under
electric-layout-mode.
(electric-layout-post-self-insert-function-1): Bind
electric-indent-inhibit to 'electric-layout-mode.

* test/lisp/electric-tests.el
(electric-layout-control-reindentation): New test.
parent 2a2a1bdb
Pipeline #2225 failed with stage
in 63 minutes and 5 seconds
......@@ -270,10 +270,13 @@ or comment."
(goto-char before)
(condition-case-unless-debug ()
(indent-according-to-mode)
(error (throw 'indent-error nil)))
;; The goal here will be to remove the trailing
;; whitespace after reindentation of the previous line
;; because that may have (re)introduced it.
(error (throw 'indent-error nil))))
(unless (eq electric-indent-inhibit 'electric-layout-mode)
;; Unless we're operating under
;; `electric-layout-mode' (Bug#35254), the goal here
;; will be to remove the trailing whitespace after
;; reindentation of the previous line because that
;; may have (re)introduced it.
(goto-char before)
;; We were at EOL in marker `before' before the call
;; to `indent-according-to-mode' but after we may
......@@ -451,7 +454,7 @@ If multiple rules match, only first one is executed.")
;; really wants to reindent, then
;; `last-command-event' should be in
;; `electric-indent-chars'.
(let ((electric-indent-inhibit t))
(let ((electric-indent-inhibit 'electric-layout-mode))
(funcall nl-after)))))))
(pcase sym
('before (funcall nl-before))
......
......@@ -876,6 +876,25 @@ baz\"\""
(call-interactively (key-binding `[,last-command-event])))
(should (equal (buffer-string) "int main () {\n \n}"))))
(ert-deftest electric-layout-control-reindentation ()
"Same as `e-l-int-main-kernel-style', but checking Bug#35254."
(ert-with-test-buffer ()
(plainer-c-mode)
(electric-layout-local-mode 1)
(electric-pair-local-mode 1)
(electric-indent-local-mode 1)
(setq-local electric-layout-rules
'((?\{ . (after))
(?\} . (before))))
(insert "int main () ")
(let ((last-command-event ?\{))
(call-interactively (key-binding `[,last-command-event])))
(should (equal (buffer-string) "int main () {\n \n}"))
;; insert an additional newline and check indentation and
;; reindentation
(call-interactively 'newline)
(should (equal (buffer-string) "int main () {\n\n \n}"))))
(define-derived-mode plainer-c-mode c-mode "pC"
"A plainer/saner C-mode with no internal electric machinery."
(c-toggle-electric-state -1)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment