From: Martin Rudalics Date: Sun, 23 Feb 2025 09:19:49 +0000 (+0100) Subject: Implement new user option 'quit-window-kill-buffer' (Bug#76248) X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=b2e23d48d55e4877505d69e25dbd73cbcd9d5eaa;p=emacs.git Implement new user option 'quit-window-kill-buffer' (Bug#76248) * lisp/window.el (quit-window-kill-buffer): New option. (quit-window): Handle it. * doc/lispref/windows.texi (Quitting Windows): Describe new option 'quit-window-kill-buffer'. * etc/NEWS: Announce new option 'quit-window-kill-buffer'. (cherry picked from commit 99410ba902a01d56b49001b4c18a5390b3c5463b) --- diff --git a/doc/lispref/windows.texi b/doc/lispref/windows.texi index 7a98cf4ff13..6c4e59d448f 100644 --- a/doc/lispref/windows.texi +++ b/doc/lispref/windows.texi @@ -4953,11 +4953,26 @@ instead of burying it. @vindex quit-window-hook The function @code{quit-window} first runs @code{quit-window-hook}. -Then it calls the function @code{quit-restore-window}, described next, +Then it calls the function @code{quit-restore-window}, described below, which does the hard work. @end deffn -You can get more control by calling @code{quit-restore-window} instead. +The following option tells @code{quit-window} whether it should +preferably kill or bury @var{window}'s buffer. + +@defopt quit-window-kill-buffer +If this is @code{nil} and @var{kill} is @code{nil}, @code{quit-window} +will bury @var{window}'s buffer. If this is @code{t}, +@code{quit-window} will try to kill @var{window}'s buffer. Otherwise, +this should be a list of major modes. @code{quit-window} will kill the +buffer of @var{window} regardless of the value of @var{kill} if that +buffer's major mode is either a member of this list or is derived from a +member of this list. In any other case, @code{quit-window} will kill +the buffer only if @var{kill} is non-@code{nil} and bury it otherwise. +@end defopt + +You can get more control by calling @code{quit-restore-window} instead +of @code{quit-window}. @defun quit-restore-window &optional window bury-or-kill This function handles @var{window} and its buffer after quitting. The diff --git a/lisp/window.el b/lisp/window.el index dac3a35f328..363cb38ea0e 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -5390,6 +5390,21 @@ elsewhere. This value is used by `quit-windows-on'." ((eq bury-or-kill 'bury) (bury-buffer-internal buffer))))) +(defcustom quit-window-kill-buffer nil + "Non-nil means `quit-window' will try to kill WINDOW's buffer. +If this is nil and the KILL argument is nil, `quit-window' will bury +WINDOW's buffer. If this is t, `quit-window' will always try to kill +WINDOW's buffer. Otherwise, this should be a list of major modes. +`quit-window' will kill the buffer of its WINDOW argument regardless of +the value of KILL if that buffer's major mode is either a member of this +list or is derived from a member of this list. In any other case, +`quit-window' will kill the buffer only if KILL is non-nil and bury it +otherwise." + :type '(choice (boolean :tag "All major modes") + (repeat (symbol :tag "Major mode"))) + :version "31.1" + :group 'windows) + (defun quit-window (&optional kill window) "Quit WINDOW and bury its buffer. WINDOW must be a live window and defaults to the selected one. @@ -5403,11 +5418,19 @@ Windows' for more details. The functions in `quit-window-hook' will be run before doing anything else." (interactive "P") - ;; Run the hook from the buffer implied to get any buffer-local - ;; values. - (with-current-buffer (window-buffer (window-normalize-window window)) - (run-hooks 'quit-window-hook)) - (quit-restore-window window (if kill 'kill 'bury))) + (let (kill-from-mode) + (with-current-buffer (window-buffer (window-normalize-window window)) + ;; Run the hook from the buffer implied to get any buffer-local + ;; values. + (run-hooks 'quit-window-hook) + + (setq kill-from-mode + (or (eq quit-window-kill-buffer t) + (and (listp quit-window-kill-buffer) + (derived-mode-p quit-window-kill-buffer))))) + + (quit-restore-window + window (if (or kill kill-from-mode) 'kill 'bury)))) (defun quit-windows-on (&optional buffer-or-name kill frame) "Quit all windows showing BUFFER-OR-NAME.