From 5e88b50d542b6d1c4ff43f8ae0fabe8a647d842e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jo=C3=A3o=20T=C3=A1vora?= Date: Wed, 15 May 2019 13:10:22 +0100 Subject: [PATCH] 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. --- lisp/electric.el | 13 ++++++++----- test/lisp/electric-tests.el | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lisp/electric.el b/lisp/electric.el index 53e53bd975c..a14deb71afb 100644 --- a/lisp/electric.el +++ b/lisp/electric.el @@ -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)) diff --git a/test/lisp/electric-tests.el b/test/lisp/electric-tests.el index 4f1e5729be1..86c9eff9cda 100644 --- a/test/lisp/electric-tests.el +++ b/test/lisp/electric-tests.el @@ -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) -- 2.39.2