From: philipk@posteo.net (Philip K.)
To: Dmitry Gutov <dgutov@yandex.ru>
Cc: 42386@debbugs.gnu.org
Subject: bug#42386: Acknowledgement ([PATCH] Handle symbols in project-kill-buffers-ignores)
Date: Mon, 27 Jul 2020 20:33:18 +0200 [thread overview]
Message-ID: <87ime8zu35.fsf@posteo.net> (raw)
In-Reply-To: <e302d2ed-609b-b306-3db6-22d99bff8a18@yandex.ru> (message from Dmitry Gutov on Mon, 27 Jul 2020 19:26:21 +0300)
[-- Attachment #1: Type: text/plain, Size: 565 bytes --]
I actually sent a patch yesterday, but thanks to your message I realised
that my messages weren't being sent (new mail provider, didn't configure
it properly).
I attached the patch below, and hope everything works now.
Dmitry Gutov <dgutov@yandex.ru> writes:
> Hey Philip,
>
> How's it going?
> On 21.07.2020 21:51, Philip K. wrote:
>> You're right, I forgot to stage that.
>
> I can fix up the minor nits and push the patch now, if you like.
>
> Just wanted to make sure you didn't come upon any more significant
> problems in daily usage.
--
Philip K.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-project-kill-buffer-conditions.patch --]
[-- Type: text/x-diff, Size: 6961 bytes --]
From ae708413a583fa48ed175ac51a465958030d914a Mon Sep 17 00:00:00 2001
From: Philip K <philip@warpmail.net>
Date: Mon, 20 Jul 2020 21:20:34 +0200
Subject: [PATCH] Add project-kill-buffer-conditions
This replaces its negation, project-kill-buffers-ignores, from the
previous version.
---
lisp/progmodes/project.el | 134 ++++++++++++++++++++++++++++++--------
1 file changed, 107 insertions(+), 27 deletions(-)
diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 67ce3dc7d9..237bc1635e 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -843,16 +843,56 @@ project-switch-to-buffer
nil
predicate))))
-(defcustom project-kill-buffers-ignores
- '("\\*Help\\*")
- "Conditions for buffers `project-kill-buffers' should not kill.
-Each condition is either a regular expression matching a buffer
-name, or a predicate function that takes a buffer object as
-argument and returns non-nil if it matches. Buffers that match
-any of the conditions will not be killed."
- :type '(repeat (choice regexp function))
+(defcustom project-kill-buffer-conditions
+ '(buffer-file-name ; All file-visiting buffers are included.
+ ;; Most of the temp buffers in the background:
+ (major-mode . fundamental-mode)
+ ;; non-text buffer such as xref, occur, vc, log, ...
+ (and (derived-mode . special-mode)
+ (not (major-mode . help-mode)))
+ (derived-mode . compilation-mode)
+ (derived-mode . dired-mode)
+ (derived-mode . diff-mode))
+ "List of conditions for how to kill buffers related to a project.
+This list is used by `project-kill-buffers'.
+Each condition is either:
+- a regular expression, to match a buffer name,
+- a predicate function that takes a buffer object as argument
+ and returns non-nil if the buffer should be killed,
+- a symbol, denoting a buffer local variable, where the buffer
+ is killed if it's value is non-nil. If the symbol also has a
+ function slot, it will be interpreted as a function first.
+- a cons-cell, where the car describes how to interpret the cdr.
+ The car can be one of the following:
+ * `major-mode': the buffer is killed if the buffers major
+ mode is eq to the cons-cell's cdr
+ * `defived-mode': the buffer is killed if the buffers major
+ mode is derived from the major mode denoted by the cons-cell's
+ cdr
+ * `not': the cdr is interpreted as a negation of a condition.
+ * `and': the cdr is a list of recursive conditions, that all have
+ to be met.
+ * `or': the cdr is a list of recursive conditions, of which at
+ least one has to be met.
+
+If any of these conditions are satified, a buffer will be
+killed. By default buffers are left alone, so that
+`project-kill-buffers' doesn't accidentally delete more than it
+should."
+ :type '(repeat (choice regexp function symbol
+ (cons :tag "Major mode"
+ (const major-mode) symbol)
+ (cons :tag "Derived mode"
+ (const derived-mode) symbol)
+ (cons :tag "Negation"
+ (const not) sexp)
+ (cons :tag "Conjunction"
+ (const and) sexp)
+ (cons :tag "Disjunction"
+ (const or) sexp)))
:version "28.1"
- :package-version '(project . "0.5.0"))
+ :group 'project
+ :package-version '(project . "0.6.0"))
(defun project--buffer-list (pr)
"Return the list of all buffers in project PR."
@@ -864,26 +904,66 @@ project--buffer-list
(push buf bufs)))
(nreverse bufs)))
-;;;###autoload
-(defun project-kill-buffers ()
- "Kill all live buffers belonging to the current project.
-Two buffers belong to the same project if their project instances,
-as reported by `project-current' in each buffer, are identical.
-Certain buffers may be \"spared\", see `project-kill-buffers-ignores'."
- (interactive)
- (let ((pr (project-current t)) bufs)
+(defun project--kill-buffer-check (buf conditions)
+ "Check if buffer BUF matches any element of the list CONDITIONS.
+See `project-kill-buffer-conditions' for more details on the form
+of CONDITIONS."
+ (catch 'kill
+ (dolist (c conditions)
+ (when (cond
+ ((stringp c)
+ (string-match-p c (buffer-name buf)))
+ ((symbolp c)
+ (funcall c buf))
+ ((eq (car-safe c) 'major-mode)
+ (eq (buffer-local-value 'major-mode buf)
+ (cdr c)))
+ ((eq (car-safe c) 'derived-mode)
+ (provided-mode-derived-p
+ (buffer-local-value 'major-mode buf)
+ (cdr c)))
+ ((eq (car-safe c) 'not)
+ (not (project--kill-buffer-check buf (cdr c))))
+ ((eq (car-safe c) 'or)
+ (project--kill-buffer-check buf (cdr c)))
+ ((eq (car-safe c) 'and)
+ (seq-every-p
+ (apply-partially #'project--kill-buffer-check
+ buf)
+ (mapcar #'list (cdr c)))))
+ (throw 'kill t)))))
+
+(defun project-list-buffers-to-kill (pr)
+ "Return list of buffers in project PR to kill.
+What buffers should or should not be killed is described
+in `project-kill-buffer-conditions'."
+ (let (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-kill-buffers-ignores)
+ (when (project--kill-buffer-check buf project-kill-buffer-conditions)
(push buf bufs)))
- (when (yes-or-no-p (format "Kill %d buffers in %s? "
- (length bufs) (project-root pr)))
- (mapc #'kill-buffer bufs))))
+ bufs))
+
+;;;###autoload
+(defun project-kill-buffers (&optional no-confirm)
+ "Kill all live buffers belonging to the current project.
+Two buffers belong to the same project if their project
+instances, as reported by `project-current' in each buffer, are
+identical. Only the buffers that match a condition in
+`project-kill-buffer-conditions' will be killed. If NO-CONFIRM
+is non-nil, the command will not ask the user for confirmation.
+NO-CONFIRM is always nil when the command is invoked
+interactivly."
+ (interactive)
+ (let* ((pr (project-current t))
+ (bufs (project-list-buffers-to-kill pr)))
+ (cond (no-confirm
+ (mapc #'kill-buffer bufs))
+ ((null bufs)
+ (message "No buffers to kill"))
+ ((yes-or-no-p (format "Kill %d buffers in %s? "
+ (length bufs)
+ (project-root pr)))
+ (mapc #'kill-buffer bufs)))))
\f
;;; Project list
--
2.20.1
next prev parent reply other threads:[~2020-07-27 18:33 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-16 8:15 bug#42386: [PATCH] Handle symbols in project-kill-buffers-ignores Philip K.
[not found] ` <handler.42386.B.159488736413990.ack@debbugs.gnu.org>
2020-07-16 8:47 ` bug#42386: Acknowledgement ([PATCH] Handle symbols in project-kill-buffers-ignores) Philip K.
2020-07-16 15:14 ` Eli Zaretskii
2020-07-16 18:08 ` Philip K.
2020-07-16 18:16 ` Philip K.
2020-07-16 19:35 ` Eli Zaretskii
2020-07-16 22:22 ` Philip K.
2020-07-17 6:38 ` Eli Zaretskii
2020-07-17 8:16 ` Philip K.
2020-07-17 10:49 ` Eli Zaretskii
2020-07-17 11:17 ` Philip K.
2020-07-17 11:26 ` Eli Zaretskii
2020-07-17 15:30 ` Philip K.
2020-07-17 15:43 ` Dmitry Gutov
2020-07-17 17:16 ` Philip K.
2020-07-17 22:21 ` Dmitry Gutov
2020-07-18 12:48 ` Philip K.
2020-07-19 23:10 ` Dmitry Gutov
2020-07-20 12:07 ` Philip K.
2020-07-20 13:39 ` Dmitry Gutov
2020-07-21 9:11 ` Philip K.
2020-07-21 14:27 ` Eli Zaretskii
2020-07-21 18:35 ` Philip K.
2020-07-21 18:57 ` Eli Zaretskii
2020-07-21 20:47 ` Dmitry Gutov
2020-07-21 18:45 ` Dmitry Gutov
2020-07-21 18:51 ` Philip K.
2020-07-27 16:26 ` Dmitry Gutov
2020-07-27 18:33 ` Philip K. [this message]
2020-07-28 22:33 ` Dmitry Gutov
2020-07-16 18:41 ` Dmitry Gutov
2020-07-16 22:46 ` Juri Linkov
2020-07-17 0:23 ` Dmitry Gutov
2020-07-16 13:43 ` bug#42386: [PATCH] Handle symbols in project-kill-buffers-ignores Dmitry Gutov
2020-07-16 18:00 ` Philip K.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87ime8zu35.fsf@posteo.net \
--to=philipk@posteo.net \
--cc=42386@debbugs.gnu.org \
--cc=dgutov@yandex.ru \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.