Subject: Re: bug#76248: [Feature Request] user-option for killing buffer in `quit-window'.

From: martin rudalics <[email-masked]>
Date: Mon, 17 Feb 2025 10:20:23 +0100

> I've tested the patch and the implementation works like a charm.
 >
 > However, I would prefer this line to be added.
 >
 >    :type '(choice (const :tag "All major modes" t)
 > +                (const :tag "Bury, don't kill" nil)
 > 		         (repeat (symbol :tag "Major mode")))
 >    :version "31.1"
 >    :group 'windows)

Then maybe the attached would fit you better.  The text "Bury, don't
kill" is subtly misleading here because 'quit-window' will also bury the
buffer when its major mode is not a member of a list of modes specified
by this option.

martin

diff --git a/lisp/window.el b/lisp/window.el
index 1204103e83c..8297129e461 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -5447,6 +5447,23 @@ quit-restore-window
      ((eq bury-or-kill 'bury)
       (bury-buffer-internal buffer)))))
 
+(defcustom quit-window-kill-buffer nil
+  "Non-nil means `quit-window' may try to kill 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 when that buffer's major mode is
+either a member of this list or is derived from a member of this list.
+In either case it will kill the buffer regardless of the value of the
+KILL argument.
+
+If this is nil or a list of major modes such that the mode of WINDOW's
+buffer is not derived from a member of that list and KILL is nil,
+`quit-window' will bury WINDOW's buffer."
+  :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.
@@ -5460,11 +5477,19 @@ quit-window
 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.