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

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.
parent 3e38509f
...@@ -367,14 +367,12 @@ Each rule has the form (MATCHER . WHERE) where MATCHER examines ...@@ -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 the state of the buffer after a certain character was inserted
and WHERE specifies where to insert newlines. and WHERE specifies where to insert newlines.
MATCHER can be a character CHAR or a boolean function of no MATCHER is a character CHAR. The rule matches if the character
arguments. The rule matches if the character just inserted was just inserted was CHAR.
CHAR or if the function return non-nil.
WHERE and can be: WHERE can be:
* one of the symbols `before', `after', `around', `after-stay' or * one of the symbols `before', `after', `around', `after-stay'.
nil;
* a list of the preceding symbols, processed in order of * a list of the preceding symbols, processed in order of
appearance to insert multiple newlines; appearance to insert multiple newlines;
...@@ -388,8 +386,9 @@ inserted. `after-stay' means insert a newline after POS but stay ...@@ -388,8 +386,9 @@ inserted. `after-stay' means insert a newline after POS but stay
in the same place. in the same place.
Instead of the (MATCHER . WHERE) form, a rule can also be just a Instead of the (MATCHER . WHERE) form, a rule can also be just a
function of no arguments. It should return a value compatible function of a single argument, the character just inserted. It
with WHERE if the rule matches, or nil if it doesn't match. 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.") If multiple rules match, only first one is executed.")
...@@ -406,12 +405,10 @@ If multiple rules match, only first one is executed.") ...@@ -406,12 +405,10 @@ If multiple rules match, only first one is executed.")
(catch 'done (catch 'done
(while (setq probe (pop rules)) (while (setq probe (pop rules))
(cond ((and (consp probe) (cond ((and (consp probe)
(or (eq (car probe) last-command-event) (eq (car probe) last-command-event))
(and (functionp (car probe))
(funcall (car probe)))))
(throw 'done (cdr probe))) (throw 'done (cdr probe)))
((functionp probe) ((functionp probe)
(let ((res (funcall probe))) (let ((res (funcall probe last-command-event)))
(when res (throw 'done res))))))))) (when res (throw 'done res)))))))))
(when (and rule (when (and rule
(setq pos (electric--after-char-pos)) (setq pos (electric--after-char-pos))
......
...@@ -864,7 +864,7 @@ baz\"\"" ...@@ -864,7 +864,7 @@ baz\"\""
(should (equal (buffer-string) "int main ()\n{\n \n}")))) (should (equal (buffer-string) "int main ()\n{\n \n}"))))
(ert-deftest electric-pair-mode-newline-between-parens () (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) (plainer-c-mode)
(electric-layout-local-mode -1) ;; ensure e-l-m mode is off (electric-layout-local-mode -1) ;; ensure e-l-m mode is off
(electric-pair-local-mode 1) (electric-pair-local-mode 1)
...@@ -875,7 +875,7 @@ baz\"\"" ...@@ -875,7 +875,7 @@ baz\"\""
(should (equal (buffer-string) "int main () {\n \n}")))) (should (equal (buffer-string) "int main () {\n \n}"))))
(ert-deftest electric-layout-mode-newline-between-parens-without-e-p-m () (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) (plainer-c-mode)
(electric-layout-local-mode 1) (electric-layout-local-mode 1)
(electric-pair-local-mode -1) ;; ensure e-p-m mode is off (electric-pair-local-mode -1) ;; ensure e-p-m mode is off
...@@ -895,5 +895,26 @@ baz\"\"" ...@@ -895,5 +895,26 @@ baz\"\""
(call-interactively (key-binding `[,last-command-event]))) (call-interactively (key-binding `[,last-command-event])))
(should (equal (buffer-string) "int main () {\n \n}")))) (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) (provide 'electric-tests)
;;; electric-tests.el ends here ;;; electric-tests.el ends here
......
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