Subject: Re: bug#46374: 28.0.50; Ask me to save buffers only if they are under callers dir

From: Tino Calancha <[email-masked]>
Date: Sun, 05 Sep 2021 12:09:26 +0200

Juri Linkov <[email-masked]> writes:

>> This means we need to invent some ad-hoc format to distinguish between
>> these cases.  For example, to create a lexically-bound predicate
>> at the beginning, it could be called with e.g.
>>
>>   (save-some-buffers t '(eval . save-some-buffers-root))
>>
>> and defcustom will look like:
>>
>> (defcustom save-some-buffers-default-predicate nil
>>   :type '(choice (const :tag "Default" nil)
>>                  (function :tag "Only in subdirs of root"
>>                            (eval . save-some-buffers-root))
>>                  (function :tag "Custom function"))
>
> Or maybe simply '(save-some-buffers-root):

Hi Juri!

Being able to set `save-some-buffers-root' as the value of
`save-some-buffers-default-predicate' is easy for users.

The problem I see is that it hides the real nature of `save-some-buffers-root':
- it's not a predicate (as the docstring of `save-some-buffers-default-predicate' suggests).
- it's a function generating the default predicate.

We can make the distinction (default predicate <-> func generating a default predicate)
more clear if we put the generating functions in a list.

Then, we can restrict the allowed pred-fun to the elements inside such a list.

I have played today with this quick-and-dirty patch:
How do you think?

--8<-----------------------------cut here---------------start------------->8---
diff --git a/lisp/files.el b/lisp/files.el
index 7e4bdab507..91582ec9b0 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -5731,6 +5731,13 @@ save-some-buffers-action-alist
 (defvar-local buffer-save-without-query nil
   "Non-nil means `save-some-buffers' should save this buffer without asking.")
 
+(defvar save-some-buffers-fn-generating-pred '(save-some-buffers-root)
+  "List of supported functions to generate a default predicate for `save-some-buffers'.
+Each element is a function with no arguments that returns a predicate
+suitable for `save-some-buffers'.
+You can use any of these functions as the value of
+`save-some-buffers-default-predicate'.")
+
 (defcustom save-some-buffers-default-predicate nil
   "Default predicate for `save-some-buffers'.
 
@@ -5789,7 +5796,8 @@ save-some-buffers
     (setq pred save-some-buffers-default-predicate))
   ;; Allow `pred' to be a function that returns a predicate
   ;; with lexical bindings in its original environment (bug#46374).
-  (let ((pred-fun (and (functionp pred) (funcall pred))))
+  (let ((pred-fun (and (memq pred save-some-buffers-fn-generating-pred)
+                       (funcall pred))))
     (when (functionp pred-fun)
       (setq pred pred-fun)))
   (let* ((switched-buffer nil)

--8<-----------------------------cut here---------------end--------------->8---