Subject: bug#79178: closed (Re: bug#79178: [Improvement] Allow auto-insert to have functions as conditions)

From: [email-masked] (GNU bug Tracking System)
Date: Thu, 07 Aug 2025 14:42:02 +0000

Your bug report

#79178: [Improvement] Allow auto-insert to have functions as conditions

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
79178: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=79178
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#79178: [Improvement] Allow auto-insert to have functions as conditions
From: Eli Zaretskii <[email-masked]>
To: Mattias <[email-masked]>
Cc: [email-masked]
Date: Thu, 07 Aug 2025 17:40:47 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

> Date: Thu, 7 Aug 2025 15:18:43 +0200
> Cc: [email-masked]
> From: Mattias <[email-masked]>
> 
> Hi and thanks again,
> 
> > It is best to use change-log-mode for formatting commit log messages,
> > as it will allow you to use M-q and other conveniences, and will obey
> > the formatting according to our conventions.  Also, don't forget the
> > periods at the end of each sentence (I added them above).
> 
> I tried using change-log-mode but it indented the whole commit 
> message with 4 spaces when I used M-q on it. My current mode seems to do 
> the job with M-q, I just didn't use it before (and it removes the : 
> between lisp/autoinsert and (auto-insert-alist) which looks like the 
> intended formatting since other commit logs are formatted this way).

Thanks, installed on the master branch (with a few tiny fixes), and
closing the bug.

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