Subject: [Improvement] Allow auto-insert to have functions as conditions
From: Mattias <[email-masked]>
To: [email-masked]
Date: Tue, 5 Aug 2025 18:16:48 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

--text follows this line--

Hi everyone,

First contribution to emacs. This comes from the fact that I have a 
custom function that overrides auto-insert to fit my needs and I 
realised it was not a big change and could be merged into emacs directly.

This small patch aims to allow any function acting as a predicate with 
no arguments to be used as a condition in auto-insert-alist

From 37d648185f4628c01d4e93c924de51f38ea47126 Mon Sep 17 00:00:00 2001
From: mattiasdrp <[email-masked]>
Date: Tue, 5 Aug 2025 18:11:58 +0200
Subject: [PATCH] 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
---
 lisp/autoinsert.el | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/lisp/autoinsert.el b/lisp/autoinsert.el
index e7492d1b9ed..f97bef7c575 100644
--- a/lisp/autoinsert.el
+++ b/lisp/autoinsert.el
@@ -323,8 +323,9 @@ The document was typeset with
 "))
   "A list specifying text to insert by default into a new file.
 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 may be a regexp that must match the new file's name, a symbol that
+must match the major mode for this element to apply or a custom predicate that
+takes no argument.
 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 +369,25 @@ 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 lambda, don't split it but set `desc' to a custom string
+                    (if (and (not (symbolp cond)) (functionp cond))
+                        (setq desc "`lambda condition'")
+                      (setq desc (cdr cond)
+                            cond (car cond))))
+                  (when (cond
+                         ;; cond should be a major-mode variable
+                         ((and (symbolp cond) (not (functionp cond)))
+                          (derived-mode-p cond))
+
+                         ;; cond should be a predicate that takes no argument
+                         ;; It can either be a named function or a lambda
+                         ((functionp cond)
+                          (funcall 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)
-- 
2.50.1