Function: delete-window
delete-window is an interactive and byte-compiled function defined in
window.el.gz.
Signature
(delete-window &optional WINDOW)
Documentation
Delete specified WINDOW.
WINDOW must be a valid window and defaults to the selected one. Return nil.
If the variable ignore-window-parameters is non-nil or the
delete-window parameter of WINDOW equals t, do not process any
parameters of WINDOW. Otherwise, if the delete-window
parameter of WINDOW specifies a function, call that function with
WINDOW as its sole argument and return the value returned by that
function.
Otherwise, if WINDOW is part of an atomic window, call
delete-window with the root of the atomic window as its
argument. Signal an error if WINDOW is either the only window on
its frame, the last non-side window, or part of an atomic window
that is its frame's root window.
If WINDOW is the selected window on its frame, choose some other
window as that frame's selected window according to the value of
the option delete-window-choose-selected.
Probably introduced at or before Emacs version 24.1.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/window.el.gz
(defun delete-window (&optional window)
"Delete specified WINDOW.
WINDOW must be a valid window and defaults to the selected one.
Return nil.
If the variable `ignore-window-parameters' is non-nil or the
`delete-window' parameter of WINDOW equals t, do not process any
parameters of WINDOW. Otherwise, if the `delete-window'
parameter of WINDOW specifies a function, call that function with
WINDOW as its sole argument and return the value returned by that
function.
Otherwise, if WINDOW is part of an atomic window, call
`delete-window' with the root of the atomic window as its
argument. Signal an error if WINDOW is either the only window on
its frame, the last non-side window, or part of an atomic window
that is its frame's root window.
If WINDOW is the selected window on its frame, choose some other
window as that frame's selected window according to the value of
the option `delete-window-choose-selected'."
(interactive)
(setq window (window-normalize-window window))
(let* ((frame (window-frame window))
(function (window-parameter window 'delete-window))
(parent (window-parent window))
atom-root)
(window--check frame)
(catch 'done
;; Handle window parameters.
(cond
;; Ignore window parameters if `ignore-window-parameters' tells
;; us so or `delete-window' equals t.
((or ignore-window-parameters (eq function t)))
((functionp function)
;; The `delete-window' parameter specifies the function to call.
;; If that function is `ignore' nothing is done. It's up to the
;; function called here to avoid infinite recursion.
(throw 'done (funcall function window)))
((and (window-parameter window 'window-atom)
(setq atom-root (window-atom-root window))
(not (eq atom-root window)))
(if (eq atom-root (frame-root-window frame))
(error "Root of atomic window is root window of its frame")
(throw 'done (delete-window atom-root))))
((not parent)
(error "Attempt to delete minibuffer or sole ordinary window"))
((eq window (window-main-window frame))
(error "Attempt to delete main window of frame %s" frame)))
(let* ((horizontal (window-left-child parent))
(size (window-size window horizontal t))
(window-combination-resize
(or window-combination-resize
(window-parameter parent 'window-side)))
(frame-selected-window (frame-selected-window frame))
;; Emacs 23 preferably gives WINDOW's space to its left
;; sibling.
(sibling (or (window-left window) (window-right window)))
frame-selected-window-edges frame-selected-window-pos)
(window--resize-reset frame horizontal)
(cond
((and (not (eq window-combination-resize t))
sibling (window-sizable-p sibling size horizontal nil t))
;; Resize WINDOW's sibling.
(window--resize-this-window sibling size horizontal nil t)
(set-window-new-normal
sibling (+ (window-normal-size sibling horizontal)
(window-normal-size window horizontal))))
((window--resizable-p window (- size) horizontal nil nil nil t t)
;; Can do without resizing fixed-size windows.
(window--resize-siblings window (- size) horizontal))
(t
;; Can't do without resizing fixed-size windows.
(window--resize-siblings window (- size) horizontal t)))
(when (eq delete-window-choose-selected 'pos)
;; Remember edges and position of point of the selected window
;; of WINDOW'S frame.
(setq frame-selected-window-edges
(window-edges frame-selected-window nil nil t))
(setq frame-selected-window-pos
(nth 2 (posn-at-point nil frame-selected-window))))
;; Actually delete WINDOW.
(delete-window-internal window)
(window--pixel-to-total frame horizontal)
;; If we deleted the selected window of WINDOW's frame, choose
;; another one based on `delete-window-choose-selected'. Note
;; that both `window-at-x-y' and `get-mru-window' may fail to
;; produce a suitable window in which case we will fall back on
;; its frame's first window, chosen by `delete-window-internal'.
(cond
((window-live-p frame-selected-window))
((and frame-selected-window-pos
;; We have a recorded position of point of the previously
;; selected window. Try to find the window that is now
;; at that position.
(let ((new-frame-selected-window
(window-at-x-y
(+ (nth 0 frame-selected-window-edges)
(car frame-selected-window-pos))
(+ (nth 1 frame-selected-window-edges)
(cdr frame-selected-window-pos))
frame t)))
(and new-frame-selected-window
;; Select window at WINDOW's position at point.
(set-frame-selected-window
frame new-frame-selected-window)))))
((and (eq delete-window-choose-selected 'mru)
;; Try to use the most recently used window.
(let ((mru-window (get-mru-window frame nil nil t)))
(and mru-window
(set-frame-selected-window frame mru-window)))))
((and (window-no-other-p (frame-selected-window frame))
;; If `delete-window-internal' selected a window with a
;; non-nil 'no-other-window' parameter as its frame's
;; selected window, try to choose another one.
(catch 'found
(walk-window-tree
(lambda (other)
(unless (window-no-other-p other)
(set-frame-selected-window frame other)
(throw 'found t)))
frame))))
(t
;; Record the window chosen by `delete-window-internal'.
(set-frame-selected-window
frame (frame-selected-window frame))))
(window--check frame)
;; Always return nil.
nil))))