Subject: bug#41868: [PATCH] Add project-clean-up command

From: "Philip K." <[email-masked]>
Date: Tue, 16 Jun 2020 19:12:36 +0200

Eli Zaretskii <[email-masked]> writes:

>> From: "Basil L. Contovounesios" <[email-masked]>
>> Date: Tue, 16 Jun 2020 11:52:26 +0100
>> Cc: [email-masked], Dmitry Gutov <[email-masked]>
>> 
>> Perhaps some of the common wording can be factored out as well
>> (feel free to adapt as you see fit):
>> 
>>     "List of conditions to be ignored by `project-kill-buffers'.
>
> The first line is too general, and could deceive.  How about
>
>   Conditions for buffers `project-kill-buffers' should not kill.

It sounds good, so I used it in the revised patch below, together with a
few other minor improvments which Basil mentioned.

-- 
	Philip K.

>From 2172f4d3d310d75dadf5ef0af297476e873349b8 Mon Sep 17 00:00:00 2001
From: Philip K <[email-masked]>
Date: Fri, 12 Jun 2020 23:37:51 +0200
Subject: [PATCH] Add project-kill-buffers command

---
 lisp/progmodes/project.el | 42 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index f3df44fa7b..04d3b324d6 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -744,6 +744,48 @@ project-compile
          (default-directory (project-root pr)))
     (compile command comint)))
 
+(defcustom project-spare-buffers-conditions
+  '("\\*Help\\*")
+  "Conditions for buffers `project-kill-buffers' should not kill.
+If a condition is a string, it will be interpreted as a regular
+expression, and matched against the buffer name.  If a condition
+is a function, it will be called with the buffer object, and
+returns non-nil if it matches.  Buffers that match any condition
+are \"spared\", and will hence not be killed by
+`project-kill-buffers'."
+  :type '(repeat (choice regexp function))
+  :version "28.1")
+
+(defun project--buffer-list (pr)
+  "Return a list of all buffers in project PR."
+  (let ((root (project-root pr)) bufs)
+    (dolist (buf (buffer-list))
+      (let ((filename (or (buffer-file-name buf)
+                          (buffer-local-value 'default-directory buf))))
+        (when (and filename (file-in-directory-p filename root))
+          (push buf bufs))))
+    (nreverse bufs)))
+
+;;;###autoload
+(defun project-kill-buffers ()
+  "Kill all live buffers of a project.
+Certain buffers may be ignored, depending on the value of
+`project-spare-buffers-conditions'."
+  (interactive)
+  (let ((pr (project-current t)) bufs)
+    (dolist (buf (project--buffer-list pr))
+      (unless (seq-some
+               (lambda (c)
+                 (cond ((stringp c)
+                        (string-match-p c (buffer-name buf)))
+                       ((functionp c)
+                        (funcall c buf))))
+               project-spare-buffers-conditions)
+        (push buf bufs)))
+    (when (yes-or-no-p (format "Kill %d buffers in %s? "
+                               (length bufs) (project-root pr)))
+      (mapc #'kill-buffer bufs))))
+
 
 ;;; Project list
 
-- 
2.20.1