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

From: martin rudalics <[email-masked]>
Date: Fri, 14 Nov 2008 09:06:36 +0100

> But yes, it breaks one of my favorite uses.
 >
 > The docstring previously said:
 >
 > "If WINDOW is non-nil, it specifies a window; we delete that window,
 > and the buffer that is killed or buried is the one in that window."
 >
 > Now, it says:
 >
 > "If WINDOW
 > is dedicated or a minibuffer window, delete it and, if it's the
 > only window on its frame, delete its frame as well provided there
 > are other frames left.  Otherwise, display some other buffer in
 > the window."
 >
 > I was using the previously documented behavior. I have a function
 >
 > (defun quit-buffer-and-window ()
 >    (interactive)
 >    (quit-window nil (selected-window)))
 >
 > (I assign it to "q" in many modes). Now,
 >
 >   C-x 2 C-x o M-x quit-buffer-and-window <ENTER>
 >
 > leaves me with two windows.

Sorry.  I apparently fail to understand what that function is supposed
to do.  Please try the version below and tell me whether it does what
you want.  If it still doesn't, then please try to write a doc-string so
I can implement what that doc-string says.

Thank you, martin.

(defun quit-window (&optional kill window)
   "Delete WINDOW and bury its buffer.
WINDOW defaults to the selected window.  Optional argument KILL
non-nil means kill WINDOW's buffer instead of burying it.

Always delete WINDOW if its frame contains at least one other
window.  If WINDOW is dedicated or a minibuffer window, and is
the only window on its frame, try to delete its frame too.  In
any other case, display another buffer in WINDOW."
   (interactive "P")
   (let* ((window (or window (selected-window)))
	 (buffer (window-buffer window)))
     (if (eq window (frame-root-window (window-frame window)))
	;; WINDOW is the only window on its frame.  `delete-windows-on'
	;; knows how to handle that particular case.
	(delete-windows-on buffer (selected-frame))
       ;; There are other windows left, delete just this window.  But
       ;; don't throw an error if that fails for some reason; rather
       ;; switch to another buffer instead.
       (condition-case nil
	  (delete-window window)
	(error (with-selected-window window
		 (switch-to-buffer nil)))))

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