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

From: martin rudalics <[email-masked]>
Date: Thu, 13 Feb 2025 10:33:58 +0100

>> Hello, i would like to know why there is not an user-option for the
 >> `quit-window' function to kill windows instead burying them.
                                   ^^^^^^^
				  buffers

 >> I find this useful in buffers like `help-mode' to avoid filling
 >> list-buffers with unnecessary buffers.
 >>
 >> I'm currently using an `advice' to achieve this:
 >>
 >>    (advice-add #'quit-window :around
 >>                (lambda (orig-fn _ &rest window)
 >>                  (funcall orig-fn 't window)))
 >>
 >> and honestly I would prefer an option like: "quit-window-kill-buffer"
 >> (set to `nil' by default).
 >
 > I completely agree.  The lack of this option causes so much trouble.
 > For example, I needed to define in the init file:
 >
 >    (defun quit-window-kill-buffer ()
 >      "Quit WINDOW and kill its buffer."
 >      (interactive)
 >      (quit-window 1))
 >
 > and then update each and every keymap to override 'quit-window'
 > with 'quit-window-kill-buffer':
 >
 >    (define-key archive-mode-map "q" 'quit-window-kill-buffer)
 >    (define-key tar-mode-map "q" 'quit-window-kill-buffer)
 >    (define-key dired-mode-map "q" 'quit-window-kill-buffer)
 >    ...

I attach a patch that could simplify that.

martin
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -5446,6 +5446,15 @@ quit-restore-window
      ((eq bury-or-kill 'bury)
       (bury-buffer-internal buffer)))))
 
+(defcustom quit-window-kill-buffer-modes nil
+  "List of major modes telling `quit-window' to kill WINDOW's buffer.
+If non-nil, 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
+a member of this list 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.
@@ -5461,9 +5470,14 @@ quit-window
   (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-hooks 'quit-window-hook)
+
+      (setq kill-from-mode (memq major-mode 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.