From ba97dad7c427f3d6c1861b0ff1e139e7a3226bc0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jo=C3=A3o=20T=C3=A1vora?= Date: Fri, 28 Dec 2018 21:53:12 +0000 Subject: [PATCH] Tweak electric-layout-mode's API again after Stefan's comments * lisp/electric.el (electric-layout-rules): Tweak docstring. (electric-layout-post-self-insert-function-1): MATCHER is always a char. Call function with last-command-event. * test/lisp/electric-tests.el (electric-pair-mode-newline-between-parens) (electric-layout-mode-newline-between-parens-without-e-p-m): Don't pass a name to the ERT test buffer. (electric-layout-mode-newline-between-parens-without-e-p-m-2): New test. --- lisp/electric.el | 21 +++++++++------------ test/lisp/electric-tests.el | 25 +++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lisp/electric.el b/lisp/electric.el index 27d2bd83d49..b52efb07c7b 100644 --- a/lisp/electric.el +++ b/lisp/electric.el @@ -367,14 +367,12 @@ Each rule has the form (MATCHER . WHERE) where MATCHER examines the state of the buffer after a certain character was inserted and WHERE specifies where to insert newlines. -MATCHER can be a character CHAR or a boolean function of no -arguments. The rule matches if the character just inserted was -CHAR or if the function return non-nil. +MATCHER is a character CHAR. The rule matches if the character +just inserted was CHAR. -WHERE and can be: +WHERE can be: -* one of the symbols `before', `after', `around', `after-stay' or - nil; +* one of the symbols `before', `after', `around', `after-stay'. * a list of the preceding symbols, processed in order of appearance to insert multiple newlines; @@ -388,8 +386,9 @@ inserted. `after-stay' means insert a newline after POS but stay in the same place. Instead of the (MATCHER . WHERE) form, a rule can also be just a -function of no arguments. It should return a value compatible -with WHERE if the rule matches, or nil if it doesn't match. +function of a single argument, the character just inserted. It +should return a value compatible with WHERE if the rule matches, +or nil if it doesn't match. If multiple rules match, only first one is executed.") @@ -406,12 +405,10 @@ If multiple rules match, only first one is executed.") (catch 'done (while (setq probe (pop rules)) (cond ((and (consp probe) - (or (eq (car probe) last-command-event) - (and (functionp (car probe)) - (funcall (car probe))))) + (eq (car probe) last-command-event)) (throw 'done (cdr probe))) ((functionp probe) - (let ((res (funcall probe))) + (let ((res (funcall probe last-command-event))) (when res (throw 'done res))))))))) (when (and rule (setq pos (electric--after-char-pos)) diff --git a/test/lisp/electric-tests.el b/test/lisp/electric-tests.el index d40d942dab8..d3c2473730d 100644 --- a/test/lisp/electric-tests.el +++ b/test/lisp/electric-tests.el @@ -864,7 +864,7 @@ baz\"\"" (should (equal (buffer-string) "int main ()\n{\n \n}")))) (ert-deftest electric-pair-mode-newline-between-parens () - (ert-with-test-buffer (:name "electric-pair-mode-newline-between-parens") + (ert-with-test-buffer () (plainer-c-mode) (electric-layout-local-mode -1) ;; ensure e-l-m mode is off (electric-pair-local-mode 1) @@ -875,7 +875,7 @@ baz\"\"" (should (equal (buffer-string) "int main () {\n \n}")))) (ert-deftest electric-layout-mode-newline-between-parens-without-e-p-m () - (ert-with-test-buffer (:name "electric-pair-mode-newline-between-parens") + (ert-with-test-buffer () (plainer-c-mode) (electric-layout-local-mode 1) (electric-pair-local-mode -1) ;; ensure e-p-m mode is off @@ -895,5 +895,26 @@ baz\"\"" (call-interactively (key-binding `[,last-command-event]))) (should (equal (buffer-string) "int main () {\n \n}")))) +(ert-deftest electric-layout-mode-newline-between-parens-without-e-p-m-2 () + (ert-with-test-buffer () + (plainer-c-mode) + (electric-layout-local-mode 1) + (electric-pair-local-mode -1) ;; ensure e-p-m mode is off + (electric-indent-local-mode 1) + (setq-local electric-layout-rules + '((lambda (char) + (when (and + (eq char ?\n) + (eq (save-excursion + (skip-chars-backward "\t\s") + (char-before (1- (point)))) + (matching-paren (char-after)))) + '(after-stay))))) + (insert "int main () {}") + (backward-char 1) + (let ((last-command-event ? )) + (call-interactively (key-binding `[,last-command-event]))) + (should (equal (buffer-string) "int main () {\n \n}")))) + (provide 'electric-tests) ;;; electric-tests.el ends here -- 2.39.5