From 6a9c268a340025bca428b5ec7c35229a29b4a95f Mon Sep 17 00:00:00 2001 From: Philip K Date: Thu, 16 Jul 2020 10:03:35 +0200 Subject: [PATCH] Replace project-kill-buffers-ignores with ...-kill-buffer-conditions --- lisp/progmodes/project.el | 119 ++++++++++++++++++++++++++++++++------ 1 file changed, 100 insertions(+), 19 deletions(-) diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el index 67ce3dc7d9..869401606a 100644 --- a/lisp/progmodes/project.el +++ b/lisp/progmodes/project.el @@ -843,16 +843,66 @@ 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, ... + (derived-mode . special-mode) + (derived-mode . compilation-mode) + (derived-mode . dired-mode) + (derived-mode . diff-mode)) + "Conditions for buffers `project-kill-buffers' should kill. +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. + +Buffers that match any of the conditions will not be killed." + :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")) + +(defcustom project-kill-buffers-ignores nil + "Conditions for buffers `project-kill-buffers' should not kill." + :type '(repeat choice regexp function) + :set (lambda (var val) + (add-to-list 'project-kill-buffer-conditions + (cons 'not val)) + (custom-set-default var val)) + :version "28.1" + :group 'project + :package-version '(project . "0.6.0"))) + +(make-obsolete-variable 'project-kill-buffers-ignores + 'project-kill-buffer-conditions + "0.6.0") (defun project--buffer-list (pr) "Return the list of all buffers in project PR." @@ -864,6 +914,41 @@ project--buffer-list (push buf bufs))) (nreverse bufs))) +(defun project--kill-buffer-check (buf &optional conds) + "Throw" + (unless conds + (setq conds project-kill-buffer-conditions)) + (catch (if (eq project-kill-buffer-conditions conds) + 'kill 'other) + (dolist (c conds) + (when (cond + ((stringp c) + (string-match-p c (buffer-name buf))) + ((and (functionp c) + (ignore-errors (funcall c buf)))) + ((and (symbolp c) (boundp c)) + (buffer-local-value 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) 'and) + (seq-every-p + (apply-partially #'project--kill-buffer-check + buf) + (cdr c))) + ((eq (car-safe c) 'or) + (seq-some + (apply-partially #'project--kill-buffer-check + buf) + (cdr c)))) + (throw 'kill t))))) + ;;;###autoload (defun project-kill-buffers () "Kill all live buffers belonging to the current project. @@ -873,17 +958,13 @@ project-kill-buffers (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-kill-buffers-ignores) + (when (project--kill-buffer-check buf) (push buf bufs))) - (when (yes-or-no-p (format "Kill %d buffers in %s? " - (length bufs) (project-root pr))) - (mapc #'kill-buffer bufs)))) + (if (null bufs) + (message "No buffers to kill") + (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