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

From: martin rudalics <[email-masked]>
Date: Fri, 14 Feb 2025 11:24:49 +0100

> It would be nice to support the value t as a shortcut that means all modes.

OK.

 > Then the name could be just 'quit-window-kill-buffer' for symmetry with
 > 'kill-buffer-quit-windows'.

Hmm...  What about people who eventually expect buffer names or regexps
to match the name of the window's buffer?

 > Wouldn't it be better to check for derived modes with 'derived-mode-p'?
 > Then it's possible to detect all modes inheriting from special-mode,
 > e.g. (derived-mode-p '(special-mode)).

OK.

 >> Maybe its handling should go to 'quit-restore-window' itself.
 >> Or we should have 'quit-windows-on' handle it too.
 >> Then what about 'delete-windows-on'?
 >
 > Not sure about other functions.

Me neither.  Maybe your original statement that it really is the "q" we
want to handle could be done via an additional condition like

(eq (key-binding "q") 'quit-window)

New patch attached.

martin

diff --git a/lisp/window.el b/lisp/window.el
index 290b5cae64d..a37dee80f46 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -5446,6 +5446,18 @@ quit-restore-window
      ((eq bury-or-kill 'bury)
       (bury-buffer-internal buffer)))))
 
+(defcustom quit-window-kill-buffer-modes nil
+  "Non-nil means `quit-window' may try to kill WINDOW's buffer.
+If this is t, `quit-window' will also 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."
+  :type '(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.
@@ -5459,11 +5471,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-modes t)
+		(and (listp quit-window-kill-buffer-modes)
+		     (derived-mode-p quit-window-kill-buffer-modes)))))
+
+    (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.