emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master a9ad3d4: Add save-some-buffers-root to save-some-buffers-default-


From: Juri Linkov
Subject: master a9ad3d4: Add save-some-buffers-root to save-some-buffers-default-predicate (bug#46374)
Date: Fri, 13 Aug 2021 03:11:19 -0400 (EDT)

branch: master
commit a9ad3d477441feefa3bf6107d58281cb64e0e78a
Author: Juri Linkov <juri@linkov.net>
Commit: Juri Linkov <juri@linkov.net>

    Add save-some-buffers-root to save-some-buffers-default-predicate 
(bug#46374)
    
    * lisp/files.el (save-some-buffers-default-predicate): Add choice
    'save-some-buffers-root'.
    (save-some-buffers-root): New predicate function.
    (save-some-buffers): Check if 'pred' returns a lexically-bound lambda,
    then use it as 'pred'.
    
    Thanks to Tino Calancha <tino.calancha@gmail.com>
---
 etc/NEWS      |  5 +++++
 lisp/files.el | 21 ++++++++++++++++++++-
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index ffe8f5b..26ede71 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -354,6 +354,11 @@ by dragging the tab lines of their topmost windows with 
the mouse.
 
 * Editing Changes in Emacs 28.1
 
+** New value 'save-some-buffers-root' of 'save-some-buffers-default-predicate'.
+It allows to ask about saving only files under the project root
+or in subdirectories of the directory that was default during
+command invocation.
+
 ---
 ** Dragging a file to Emacs will now also push the name of the file
 onto 'file-name-history'.
diff --git a/lisp/files.el b/lisp/files.el
index 6a6d540..775d871 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -5727,9 +5727,23 @@ be saved."
   :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)
+  :type '(choice (const :tag "Default" nil)
+                 (function :tag "Only in subdirs of root"
+                           save-some-buffers-root)
+                 (function :tag "Custom function"))
   :version "26.1")
 
+(defun save-some-buffers-root ()
+  "A predicate to check whether the buffer is under the root directory.
+Can be used as a value of `save-some-buffers-default-predicate'
+to save buffers only under the project root or in subdirectories
+of the directory that was default during command invocation."
+  (let ((root (or (and (featurep 'project) (project-current)
+                       (fboundp 'project-root)
+                       (project-root (project-current)))
+                  default-directory)))
+    (lambda () (file-in-directory-p default-directory root))))
+
 (defun save-some-buffers (&optional arg pred)
   "Save some modified file-visiting buffers.  Asks user about each one.
 You can answer `y' or SPC to save, `n' or DEL not to save, `C-r'
@@ -5758,6 +5772,11 @@ change the additional actions you can take on files."
   (interactive "P")
   (unless pred
     (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))))
+    (when (functionp pred-fun)
+      (setq pred pred-fun)))
   (let* ((switched-buffer nil)
          (save-some-buffers--switch-window-callback
           (lambda (buffer)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]