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;
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.")
(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))
(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)
(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
(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 ?\r))
+ (call-interactively (key-binding `[,last-command-event])))
+ (should (equal (buffer-string) "int main () {\n \n}"))))
+
(provide 'electric-tests)
;;; electric-tests.el ends here