Subject: Re: quit-window does not kill the window

From: martin rudalics <[email-masked]>
Date: Fri, 14 Nov 2008 14:41:07 +0100

> In 22.X:
 >
 >   M-x quit-window    => buries the buffer, keeps the window
[...]
 > In 23.X, v1
 >
 >   M-x quit-window    => buries the buffer, keeps the window
[...]

Thanks for investigating this.  I was to silly to figure it out.

Deleting the window iff it's explicitly specified strikes me as
non-intuitive.  Anyway, the version below should do that.

Please give it another try.

And thanks again for the explanations, martin.

(defun quit-window (&optional kill window)
   "Quit WINDOW and bury or kill its buffer.
WINDOW defaults to the selected window.

If WINDOW is dedicated or a minibuffer window, delete WINDOW and,
if it's alone on its frame, its frame too.  Else, if WINDOW was
explicitly specified and not nil, delete it.  Otherwise, or if
deleting WINDOW fails in any of the preceding cases, display
another buffer in WINDOW using `switch-to-buffer'.

Optional argument KILL non-nil means kill WINDOW's buffer.
Otherwise, bury WINDOW's buffer, see `bury-buffer'."
   (interactive "P")
   ;; Don't let-bind WINDOW here; below we want to know whether the
   ;; WINDOW argument was explictly specified.
   (let* ((window-to-handle (or window (selected-window)))
	 (buffer (window-buffer window-to-handle)))
     (cond
      ((or (window-minibuffer-p window-to-handle)
	  (window-dedicated-p window-to-handle))
       ;; Minibuffer windows and dedicated windows are treated specially.
       (let ((frame (window-frame window-to-handle)))
	(if (eq window-to-handle (frame-root-window frame))
	    ;; The window to handle is alone on its frame.
	    ;; `delete-windows-on' knows what to do.
	    (delete-windows-on buffer frame)
	  ;; There are other windows on the frame, delete this one.
	  (delete-window window-to-handle))))
      ((not window)
       ;; No WINDOW was specified.  Switch to another buffer in the
       ;; selected window.
       (switch-to-buffer nil))
      (t
       ;; WINDOW was specified, try to delete it.  But don't throw an
       ;; error if that fails; rather switch to another buffer instead.
       (condition-case nil
	  (delete-window window-to-handle)
	(error (with-selected-window window-to-handle
		 (switch-to-buffer nil))))))

     ;; Deal with the buffer.
     (if kill
	(kill-buffer buffer)
       (bury-buffer buffer))))