+(defun quit-window (&optional kill window)
+ "Quit the current buffer. Bury it, and maybe delete the selected frame.
+\(The frame is deleted if it is contains a dedicated window for the buffer.)
+With a prefix argument, kill the buffer instead.
+
+Noninteractively, if KILL is non-nil, then kill the current buffer,
+otherwise bury it.
+
+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."
+ (interactive "P")
+ (let ((buffer (window-buffer window))
+ (frame (window-frame (or window (selected-window))))
+ (window-solitary
+ (save-selected-window
+ (if window
+ (select-window window))
+ (one-window-p t)))
+ window-handled)
+
+ (save-selected-window
+ (if window
+ (select-window window))
+ (or (window-minibuffer-p)
+ (window-dedicated-p (selected-window))
+ (switch-to-buffer (other-buffer))))
+
+ ;; Get rid of the frame, if it has just one dedicated window
+ ;; and other visible frames exist.
+ (and (or (window-minibuffer-p) (window-dedicated-p window))
+ (delq frame (visible-frame-list))
+ window-solitary
+ (if (and (eq default-minibuffer-frame frame)
+ (= 1 (length (minibuffer-frame-list))))
+ (setq window nil)
+ (delete-frame frame)
+ (setq window-handled t)))
+
+ ;; Deal with the buffer.
+ (if kill
+ (kill-buffer buffer)
+ (bury-buffer buffer))
+
+ ;; Maybe get rid of the window.
+ (and window (not window-handled) (not window-solitary)
+ (delete-window window))))
+