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: Wed, 28 Apr 2021 21:31:25 +0200

Juri Linkov <[email-masked]> writes:

> Your first patch was better in terms of customizability.
> but its disadvantage was that it introduced a new defcustom.
>
> Would it be possible to merge the advantage of your first patch
> with the cleaner solution of your latest patch?
Good idea.

> Is it possible to add the default value as a function that returns nil?
> This will implement FIXME, and will allow adding more options
> with more predicates:

I have found a way to merge the two approaches.  Not sure if
idiomatic, but it works.

[The patch omits NEWS entry and documentation; I will add those once we
found the implementation]

--8<-----------------------------cut here---------------start------------->8---
commit 3abcf022a1fb375f15df8438ebf0cf5b67082bbc
Author: Tino Calancha <[email-masked]>
Date:   Wed Apr 28 21:18:28 2021 +0200

    Merge the two approaches
    
    * lisp/files (some-buffers-default-predicate):
    Redefined; store a function that builds the actual predicate
    used in save-some-buffers.
    
    On top of commit 0c7f1e2e42d6bf9f95e88c02d4e1ed9cb40693d8

diff --git a/lisp/files.el b/lisp/files.el
index 8e8fbac8dc..707c6a77bc 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -5512,19 +5512,37 @@ save-some-buffers-action-alist
 (defvar-local buffer-save-without-query nil
   "Non-nil means `save-some-buffers' should save this buffer without asking.")
 
-(defcustom save-some-buffers-default-predicate nil
-  "Default predicate for `save-some-buffers'.
+(defvar save-some-buffers-default-fun (lambda () nil))
 
-This allows you to stop `save-some-buffers' from asking
-about certain files that you'd usually rather not save.
+(defvar save-some-buffers-in-current-project-fun
+  (lambda ()
+    (let ((project-dir (or (and (project-current) (project-root (project-current)))
+                           default-directory)))
+      (lambda () (file-in-directory-p default-directory project-dir)))))
 
-This function is called (with no parameters) from the buffer to
-be saved."
+(defcustom save-some-buffers-default-predicate save-some-buffers-default-fun
+  "Generator function of the default predicate for `save-some-buffers'.
+
+It must be a function with no arguments that returns a predicate.
+This predicate is called (with no parameters) from the buffer to be
+saved.
+
+This allows you to stop `save-some-buffers' from asking about certain
+files that you'd usually rather not save.
+
+The default value builds a predicate that prompts to save any modified
+file-visiting buffer.  The second value restricts to buffers inside
+the project from where `save-some-buffers' is invoked, or under the
+caller's `default-directory' if no project is found."
   :group 'auto-save
-  ;; FIXME nil should not be a valid option, let alone the default,
-  ;; eg so that add-function can be used.
-  :type '(choice (const :tag "Default" nil) function)
-  :version "26.1")
+  :type `(choice
+          (function :tag "All buffers" :value ,save-some-buffers-default-fun)
+          (function :tag "Buffers inside current project"
+                    :value ,save-some-buffers-in-current-project-fun))
+  :version "28.1")
+
+(defun save-some-buffers-fun ()
+  (funcall save-some-buffers-default-predicate))
 
 (defun save-some-buffers (&optional arg pred)
   "Save some modified file-visiting buffers.  Asks user about each one.
@@ -5553,7 +5571,8 @@ save-some-buffers
 change the additional actions you can take on files."
   (interactive "P")
   (unless pred
-    (setq pred save-some-buffers-default-predicate))
+    (setq pred (save-some-buffers-fun)))
+
   (let* ((switched-buffer nil)
          (save-some-buffers--switch-window-callback
           (lambda (buffer)

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