]> git.eshelyaron.com Git - emacs.git/commitdiff
Implement new user option 'quit-window-kill-buffer' (Bug#76248)
authorMartin Rudalics <rudalics@gmx.at>
Sun, 23 Feb 2025 09:19:49 +0000 (10:19 +0100)
committerEshel Yaron <me@eshelyaron.com>
Wed, 26 Feb 2025 09:31:33 +0000 (10:31 +0100)
* 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)

doc/lispref/windows.texi
lisp/window.el

index 7a98cf4ff1308962d4dfd27ffa94a4344d1c1132..6c4e59d448f97adb35a5d01274ea0c23c3e264b7 100644 (file)
@@ -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
index dac3a35f328b7bfea30bfb22ef1c38f1316b36af..363cb38ea0e5788d6f5099e08aaf5c58069f680a 100644 (file)
@@ -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.