1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 06:20:55 -08:00

Autoinsert: Allow condition to be a function

Currently a condition can only be a regexp or a major-mode symbol but
there's no real reason to not allow any predicate as a condition.
* doc/misc/autotype.texi: Document the new condition type in
'auto-insert-alist'.
* etc/NEWS: Announce the new condition type in 'auto-insert-alist'.
* lisp/autoinsert.el (auto-insert-alist): Add the new condition type to
the variable documentation.
(auto-insert): Change the way the condition is matched to allow for
custom predicates.
* test/lisp/autoinsert-tests.el
(autoinsert-tests-auto-insert-lambda/-nil): Add two tests for
lambdas (nil and t cases)
(autoinsert-tests-auto-insert-predicate/-nil): Add two tests for named
predicates (nil and t cases) (Bug#79178)
This commit is contained in:
mattiasdrp 2025-08-08 15:16:24 +02:00 committed by Eli Zaretskii
parent 6ffa926f90
commit bd1362b686
4 changed files with 72 additions and 12 deletions

View file

@ -325,6 +325,9 @@ The document was typeset with
Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
CONDITION may be a regexp that must match the new file's name, or it may be
a symbol that must match the major mode for this element to apply.
CONDITION can also be a custom predicate of no arguments declared with
'(predicate FUNCTION)'. Emacs will insert the text if the predicate
function returns non-nil.
Only the first matching element is effective.
Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
@ -368,12 +371,24 @@ Matches the visited file name against the elements of `auto-insert-alist'."
(pcase-lambda (`(,cond . ,action))
(if (atom cond)
(setq desc cond)
(setq desc (cdr cond)
cond (car cond)))
(when (if (symbolp cond)
(derived-mode-p cond)
;; if `cond' is a predicate, don't split it but set `desc' to a custom string
(if (and (consp cond) (equal (car cond) 'predicate))
(setq desc "predicate")
(setq desc (cdr cond)
cond (car cond))))
(when (cond
;; `cond' should be a major-mode variable
((symbolp cond)
(derived-mode-p cond))
;; `cond' should be a predicate that takes no argument
((and (consp cond) (equal (car cond) 'predicate))
(funcall (cadr cond)))
;; cond should be a regexp
(t
(and buffer-file-name
(string-match cond buffer-file-name)))
(string-match cond buffer-file-name))))
action))
auto-insert-alist)))
(goto-char 1)