Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
7782 lines (7108 sloc) 307 KB
;;; window.el --- GNU Emacs window commands aside from those written in C
;; Copyright (C) 1985, 1989, 1992-1994, 2000-2015 Free Software
;; Foundation, Inc.
;; Maintainer: emacs-devel@gnu.org
;; Keywords: internal
;; Package: emacs
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Window tree functions.
;;; Code:
(defun internal--before-save-selected-window ()
(cons (selected-window)
;; We save and restore all frames' selected windows, because
;; `select-window' can change the frame-selected-window of
;; whatever frame that window is in. Each text terminal's
;; top-frame is preserved by putting it last in the list.
(apply #'append
(mapcar (lambda (terminal)
(let ((frames (frames-on-display-list terminal))
(top-frame (tty-top-frame terminal))
alist)
(if top-frame
(setq frames
(cons top-frame
(delq top-frame frames))))
(dolist (f frames)
(push (cons f (frame-selected-window f))
alist))
alist))
(terminal-list)))))
(defun internal--after-save-selected-window (state)
(dolist (elt (cdr state))
(and (frame-live-p (car elt))
(window-live-p (cdr elt))
(set-frame-selected-window (car elt) (cdr elt) 'norecord)))
(when (window-live-p (car state))
(select-window (car state) 'norecord)))
(defmacro save-selected-window (&rest body)
"Execute BODY, then select the previously selected window.
The value returned is the value of the last form in BODY.
This macro saves and restores the selected window, as well as the
selected window in each frame. If the previously selected window
is no longer live, then whatever window is selected at the end of
BODY remains selected. If the previously selected window of some
frame is no longer live at the end of BODY, that frame's selected
window is left alone.
This macro saves and restores the current buffer, since otherwise
its normal operation could make a different buffer current. The
order of recently selected windows and the buffer list ordering
are not altered by this macro (unless they are altered in BODY)."
(declare (indent 0) (debug t))
`(let ((save-selected-window--state (internal--before-save-selected-window)))
(save-current-buffer
(unwind-protect
(progn ,@body)
(internal--after-save-selected-window save-selected-window--state)))))
(defvar temp-buffer-window-setup-hook nil
"Normal hook run by `with-temp-buffer-window' before buffer display.
This hook is run by `with-temp-buffer-window' with the buffer to be
displayed current.")
(defvar temp-buffer-window-show-hook nil
"Normal hook run by `with-temp-buffer-window' after buffer display.
This hook is run by `with-temp-buffer-window' with the buffer
displayed and current and its window selected.")
(defun temp-buffer-window-setup (buffer-or-name)
"Set up temporary buffer specified by BUFFER-OR-NAME.
Return the buffer."
(let ((old-dir default-directory)
(buffer (get-buffer-create buffer-or-name)))
(with-current-buffer buffer
(kill-all-local-variables)
(setq default-directory old-dir)
(delete-all-overlays)
(setq buffer-read-only nil)
(setq buffer-file-name nil)
(setq buffer-undo-list t)
(let ((inhibit-read-only t)
(inhibit-modification-hooks t))
(erase-buffer)
(run-hooks 'temp-buffer-window-setup-hook))
;; Return the buffer.
buffer)))
(defun temp-buffer-window-show (&optional buffer action)
"Show temporary buffer BUFFER in a window.
Return the window showing BUFFER. Pass ACTION as action argument
to `display-buffer'."
(let (window frame)
(with-current-buffer buffer
(set-buffer-modified-p nil)
(setq buffer-read-only t)
(goto-char (point-min))
(when (let ((window-combination-limit
;; When `window-combination-limit' equals
;; `temp-buffer' or `temp-buffer-resize' and
;; `temp-buffer-resize-mode' is enabled in this
;; buffer bind it to t so resizing steals space
;; preferably from the window that was split.
(if (or (eq window-combination-limit 'temp-buffer)
(and (eq window-combination-limit
'temp-buffer-resize)
temp-buffer-resize-mode))
t
window-combination-limit)))
(setq window (display-buffer buffer action)))
(setq frame (window-frame window))
(unless (eq frame (selected-frame))
(raise-frame frame))
(setq minibuffer-scroll-window window)
(set-window-hscroll window 0)
(with-selected-window window
(run-hooks 'temp-buffer-window-show-hook)
(when temp-buffer-resize-mode
(resize-temp-buffer-window window)))
;; Return the window.
window))))
(defmacro with-temp-buffer-window (buffer-or-name action quit-function &rest body)
"Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
BUFFER-OR-NAME must specify either a live buffer, or the name of
a buffer (if it does not exist, this macro creates it).
Make the buffer specified by BUFFER-OR-NAME empty before running
BODY and bind `standard-output' to that buffer, so that output
generated with `prin1' and similar functions in BODY goes into
that buffer. Do not make that buffer current for running the
forms in BODY. Use `with-current-buffer-window' instead if you
need to run BODY with that buffer current.
At the end of BODY, mark the specified buffer unmodified and
read-only, and display it in a window (but do not select it).
The display happens by calling `display-buffer' passing it the
ACTION argument. If `temp-buffer-resize-mode' is enabled, the
corresponding window may be resized automatically.
Return the value returned by BODY, unless QUIT-FUNCTION specifies
a function. In that case, run that function with two arguments -
the window showing the specified buffer and the value returned by
BODY - and return the value returned by that function.
If the buffer is displayed on a new frame, the window manager may
decide to select that frame. In that case, it's usually a good
strategy if QUIT-FUNCTION selects the window showing the buffer
before reading any value from the minibuffer; for example, when
asking a `yes-or-no-p' question.
This runs the hook `temp-buffer-window-setup-hook' before BODY,
with the specified buffer temporarily current. It runs the hook
`temp-buffer-window-show-hook' after displaying the buffer, with
that buffer temporarily current, and the window that was used to
display it temporarily selected.
This construct is similar to `with-output-to-temp-buffer' but,
neither runs `temp-buffer-setup-hook' which usually puts the
buffer in Help mode, nor `temp-buffer-show-function' (the ACTION
argument replaces this)."
(declare (debug t))
(let ((buffer (make-symbol "buffer"))
(window (make-symbol "window"))
(value (make-symbol "value")))
`(let* ((,buffer (temp-buffer-window-setup ,buffer-or-name))
(standard-output ,buffer)
,window ,value)
(setq ,value (progn ,@body))
(with-current-buffer ,buffer
(setq ,window (temp-buffer-window-show ,buffer ,action)))
(if (functionp ,quit-function)
(funcall ,quit-function ,window ,value)
,value))))
(defmacro with-current-buffer-window (buffer-or-name action quit-function &rest body)
"Evaluate BODY with a buffer BUFFER-OR-NAME current and show that buffer.
This construct is like `with-temp-buffer-window' but unlike that
makes the buffer specified by BUFFER-OR-NAME current for running
BODY."
(declare (debug t))
(let ((buffer (make-symbol "buffer"))
(window (make-symbol "window"))
(value (make-symbol "value")))
`(let* ((,buffer (temp-buffer-window-setup ,buffer-or-name))
(standard-output ,buffer)
,window ,value)
(with-current-buffer ,buffer
(setq ,value (progn ,@body))
(setq ,window (temp-buffer-window-show ,buffer ,action)))
(if (functionp ,quit-function)
(funcall ,quit-function ,window ,value)
,value))))
;; The following two functions are like `window-next-sibling' and
;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
;; they don't substitute the selected window for nil), and they return
;; nil when WINDOW doesn't have a parent (like a frame's root window or
;; a minibuffer window).
(defun window-right (window)
"Return WINDOW's right sibling.
Return nil if WINDOW is the root window of its frame. WINDOW can
be any window."
(and window (window-parent window) (window-next-sibling window)))
(defun window-left (window)
"Return WINDOW's left sibling.
Return nil if WINDOW is the root window of its frame. WINDOW can
be any window."
(and window (window-parent window) (window-prev-sibling window)))
(defun window-child (window)
"Return WINDOW's first child window.
WINDOW can be any window."
(or (window-top-child window) (window-left-child window)))
(defun window-child-count (window)
"Return number of WINDOW's child windows.
WINDOW can be any window."
(let ((count 0))
(when (and (windowp window) (setq window (window-child window)))
(while window
(setq count (1+ count))
(setq window (window-next-sibling window))))
count))
(defun window-last-child (window)
"Return last child window of WINDOW.
WINDOW can be any window."
(when (and (windowp window) (setq window (window-child window)))
(while (window-next-sibling window)
(setq window (window-next-sibling window))))
window)
(defun window-normalize-buffer (buffer-or-name)
"Return buffer specified by BUFFER-OR-NAME.
BUFFER-OR-NAME must be either a buffer or a string naming a live
buffer and defaults to the current buffer."
(cond
((not buffer-or-name)
(current-buffer))
((bufferp buffer-or-name)
(if (buffer-live-p buffer-or-name)
buffer-or-name
(error "Buffer %s is not a live buffer" buffer-or-name)))
((get-buffer buffer-or-name))
(t
(error "No such buffer %s" buffer-or-name))))
(defun window-normalize-frame (frame)
"Return frame specified by FRAME.
FRAME must be a live frame and defaults to the selected frame."
(if frame
(if (frame-live-p frame)
frame
(error "%s is not a live frame" frame))
(selected-frame)))
(defun window-normalize-window (window &optional live-only)
"Return the window specified by WINDOW.
If WINDOW is nil, return the selected window. Otherwise, if
WINDOW is a live or an internal window, return WINDOW; if
LIVE-ONLY is non-nil, return WINDOW for a live window only.
Otherwise, signal an error."
(cond
((null window)
(selected-window))
(live-only
(if (window-live-p window)
window
(error "%s is not a live window" window)))
((window-valid-p window)
window)
(t
(error "%s is not a valid window" window))))
;; Maybe this should go to frame.el.
(defun frame-char-size (&optional window-or-frame horizontal)
"Return the value of `frame-char-height' for WINDOW-OR-FRAME.
If WINDOW-OR-FRAME is a live frame, return the value of
`frame-char-height' for that frame. If WINDOW-OR-FRAME is a
valid window, return the value of `frame-char-height' for that
window's frame. In any other case, return the value of
`frame-char-height' for the selected frame.
Optional argument HORIZONTAL non-nil means to return the value of
`frame-char-width' for WINDOW-OR-FRAME."
(let ((frame
(cond
((window-valid-p window-or-frame)
(window-frame window-or-frame))
((frame-live-p window-or-frame)
window-or-frame)
(t (selected-frame)))))
(if horizontal
(frame-char-width frame)
(frame-char-height frame))))
(defvar ignore-window-parameters nil
"If non-nil, standard functions ignore window parameters.
The functions currently affected by this are `split-window',
`delete-window', `delete-other-windows' and `other-window'.
An application may bind this to a non-nil value around calls to
these functions to inhibit processing of window parameters.")
;; This must go to C, finally (or get removed).
(defconst window-safe-min-height 1
"The absolute minimum number of lines of any window.
Anything less might crash Emacs.")
(defun window-safe-min-pixel-height (&optional window)
"Return the absolute minimum pixel height of WINDOW."
(* window-safe-min-height
(frame-char-size (window-normalize-window window))))
(defcustom window-min-height 4
"The minimum total height, in lines, of any window.
The value has to accommodate one text line, a mode and header
line, and a bottom divider, if present. A value less than
`window-safe-min-height' is ignored. The value of this variable
is honored when windows are resized or split.
Applications should never rebind this variable. To resize a
window to a height less than the one specified here, an
application should instead call `window-resize' with a non-nil
IGNORE argument. In order to have `split-window' make a window
shorter, explicitly specify the SIZE argument of that function."
:type 'integer
:version "24.1"
:group 'windows)
(defun window-min-pixel-height (&optional window)
"Return the minimum pixel height of window WINDOW."
(* (max window-min-height window-safe-min-height)
(frame-char-size window)))
;; This must go to C, finally (or get removed).
(defconst window-safe-min-width 2
"The absolute minimum number of columns of a window.
Anything less might crash Emacs.")
(defun window-safe-min-pixel-width (&optional window)
"Return the absolute minimum pixel width of WINDOW."
(* window-safe-min-width
(frame-char-size (window-normalize-window window) t)))
(defcustom window-min-width 10
"The minimum total width, in columns, of any window.
The value has to accommodate two text columns as well as margins,
fringes, a scroll bar and a right divider, if present. A value
less than `window-safe-min-width' is ignored. The value of this
variable is honored when windows are resized or split.
Applications should never rebind this variable. To resize a
window to a width less than the one specified here, an
application should instead call `window-resize' with a non-nil
IGNORE argument. In order to have `split-window' make a window
narrower, explicitly specify the SIZE argument of that function."
:type 'integer
:version "24.1"
:group 'windows)
(defun window-min-pixel-width (&optional window)
"Return the minimum pixel width of window WINDOW."
(* (max window-min-width window-safe-min-width)
(frame-char-size window t)))
(defun window-safe-min-pixel-size (&optional window horizontal)
"Return the absolute minimum pixel height of WINDOW.
Optional argument HORIZONTAL non-nil means return the absolute
minimum pixel width of WINDOW."
(if horizontal
(window-safe-min-pixel-width window)
(window-safe-min-pixel-height window)))
(defun window-combined-p (&optional window horizontal)
"Return non-nil if WINDOW has siblings in a given direction.
WINDOW must be a valid window and defaults to the selected one.
HORIZONTAL determines a direction for the window combination. If
HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
a vertical window combination. If HORIZONTAL is non-nil, return
non-nil if WINDOW is part of a horizontal window combination."
(setq window (window-normalize-window window))
(let ((parent (window-parent window)))
(and parent
(if horizontal
(window-left-child parent)
(window-top-child parent)))))
(defun window-combination-p (&optional window horizontal)
"Return WINDOW's first child if WINDOW is a vertical combination.
WINDOW can be any window and defaults to the selected one.
Optional argument HORIZONTAL non-nil means return WINDOW's first
child if WINDOW is a horizontal combination."
(setq window (window-normalize-window window))
(if horizontal
(window-left-child window)
(window-top-child window)))
(defun window-combinations (window &optional horizontal)
"Return largest number of windows vertically arranged within WINDOW.
WINDOW must be a valid window and defaults to the selected one.
If HORIZONTAL is non-nil, return the largest number of
windows horizontally arranged within WINDOW."
(setq window (window-normalize-window window))
(cond
((window-live-p window)
;; If WINDOW is live, return 1.
1)
((if horizontal
(window-left-child window)
(window-top-child window))
;; If WINDOW is iso-combined, return the sum of the values for all
;; child windows of WINDOW.
(let ((child (window-child window))
(count 0))
(while child
(setq count
(+ (window-combinations child horizontal)
count))
(setq child (window-right child)))
count))
(t
;; If WINDOW is not iso-combined, return the maximum value of any
;; child window of WINDOW.
(let ((child (window-child window))
(count 1))
(while child
(setq count
(max (window-combinations child horizontal)
count))
(setq child (window-right child)))
count))))
(defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only)
"Helper function for `walk-window-tree' and `walk-window-subtree'."
(let (walk-window-tree-buffer)
(while walk-window-tree-window
(setq walk-window-tree-buffer
(window-buffer walk-window-tree-window))
(when (or walk-window-tree-buffer any)
(funcall fun walk-window-tree-window))
(unless walk-window-tree-buffer
(walk-window-tree-1
fun (window-left-child walk-window-tree-window) any)
(walk-window-tree-1
fun (window-top-child walk-window-tree-window) any))
(if sub-only
(setq walk-window-tree-window nil)
(setq walk-window-tree-window
(window-right walk-window-tree-window))))))
(defun walk-window-tree (fun &optional frame any minibuf)
"Run function FUN on each live window of FRAME.
FUN must be a function with one argument - a window. FRAME must
be a live frame and defaults to the selected one. ANY, if
non-nil, means to run FUN on all live and internal windows of
FRAME.
Optional argument MINIBUF t means run FUN on FRAME's minibuffer
window even if it isn't active. MINIBUF nil or omitted means run
FUN on FRAME's minibuffer window only if it's active. In both
cases the minibuffer window must be part of FRAME. MINIBUF
neither nil nor t means never run FUN on the minibuffer window.
This function performs a pre-order, depth-first traversal of the
window tree. If FUN changes the window tree, the result is
unpredictable."
(setq frame (window-normalize-frame frame))
(walk-window-tree-1 fun (frame-root-window frame) any)
(when (memq minibuf '(nil t))
;; Run FUN on FRAME's minibuffer window if requested.
(let ((minibuffer-window (minibuffer-window frame)))
(when (and (window-live-p minibuffer-window)
(eq (window-frame minibuffer-window) frame)
(or (eq minibuf t)
(minibuffer-window-active-p minibuffer-window)))
(funcall fun minibuffer-window)))))
(defun walk-window-subtree (fun &optional window any)
"Run function FUN on the subtree of windows rooted at WINDOW.
WINDOW defaults to the selected window. FUN must be a function
with one argument - a window. By default, run FUN only on live
windows of the subtree. If the optional argument ANY is non-nil,
run FUN on all live and internal windows of the subtree. If
WINDOW is live, run FUN on WINDOW only.
This function performs a pre-order, depth-first traversal of the
subtree rooted at WINDOW. If FUN changes that tree, the result
is unpredictable."
(setq window (window-normalize-window window))
(walk-window-tree-1 fun window any t))
(defun window-with-parameter (parameter &optional value frame any minibuf)
"Return first window on FRAME with PARAMETER non-nil.
FRAME defaults to the selected frame. Optional argument VALUE
non-nil means only return a window whose window-parameter value
for PARAMETER equals VALUE (comparison is done with `equal').
Optional argument ANY non-nil means consider internal windows
too.
Optional argument MINIBUF t means consider FRAME's minibuffer
window even if it isn't active. MINIBUF nil or omitted means
consider FRAME's minibuffer window only if it's active. In both
cases the minibuffer window must be part of FRAME. MINIBUF
neither nil nor t means never consider the minibuffer window."
(let (this-value)
(catch 'found
(walk-window-tree
(lambda (window)
(when (and (setq this-value (window-parameter window parameter))
(or (not value) (equal value this-value)))
(throw 'found window)))
frame any minibuf))))
;;; Atomic windows.
(defun window-atom-root (&optional window)
"Return root of atomic window WINDOW is a part of.
WINDOW must be a valid window and defaults to the selected one.
Return nil if WINDOW is not part of an atomic window."
(setq window (window-normalize-window window))
(let (root)
(while (and window (window-parameter window 'window-atom))
(setq root window)
(setq window (window-parent window)))
root))
(defun window-make-atom (window)
"Make WINDOW an atomic window.
WINDOW must be an internal window. Return WINDOW."
(if (not (window-child window))
(error "Window %s is not an internal window" window)
(walk-window-subtree
(lambda (window)
(unless (window-parameter window 'window-atom)
(set-window-parameter window 'window-atom t)))
window t)
window))
(defun display-buffer-in-atom-window (buffer alist)
"Display BUFFER in an atomic window.
This function displays BUFFER in a new window that will be
combined with an existing window to form an atomic window. If
the existing window is already part of an atomic window, add the
new window to that atomic window. Operations like `split-window'
or `delete-window', when applied to a constituent of an atomic
window, are applied atomically to the root of that atomic window.
ALIST is an association list of symbols and values. The
following symbols can be used.
`window' specifies the existing window the new window shall be
combined with. Use `window-atom-root' to make the new window a
sibling of an atomic window's root. If an internal window is
specified here, all children of that window become part of the
atomic window too. If no window is specified, the new window
becomes a sibling of the selected window. By default, the
`window-atom' parameter of the existing window is set to `main'
provided it is live and was not set before.
`side' denotes the side of the existing window where the new
window shall be located. Valid values are `below', `right',
`above' and `left'. The default is `below'. By default, the
`window-atom' parameter of the new window is set to this value.
The return value is the new window, nil when creating that window
failed."
(let* ((ignore-window-parameters t)
(window-combination-limit t)
(window-combination-resize 'atom)
(window (cdr (assq 'window alist)))
(side (cdr (assq 'side alist)))
(atom (when window (window-parameter window 'window-atom)))
root new)
(setq window (window-normalize-window window))
(setq root (window-atom-root window))
;; Split off new window.
(when (setq new (split-window window nil side))
(window-make-atom
(if (and root (not (eq root window)))
;; When WINDOW was part of an atomic window and we did not
;; split its root, root atomic window at old root.
root
;; Otherwise, root atomic window at WINDOW's new parent.
(window-parent window)))
;; Assign `window-atom' parameters, if needed.
(when (and (not atom) (window-live-p window))
(set-window-parameter window 'window-atom 'main))
(set-window-parameter new 'window-atom side)
;; Display BUFFER in NEW and return NEW.
(window--display-buffer
buffer new 'window alist display-buffer-mark-dedicated))))
(defun window--atom-check-1 (window)
"Subroutine of `window--atom-check'."
(when window
(if (window-parameter window 'window-atom)
(let ((count 0))
(when (or (catch 'reset
(walk-window-subtree
(lambda (window)
(if (window-parameter window 'window-atom)
(setq count (1+ count))
(throw 'reset t)))
window t))
;; count >= 1 must hold here. If there's no other
;; window around dissolve this atomic window.
(= count 1))
;; Dissolve atomic window.
(walk-window-subtree
(lambda (window)
(set-window-parameter window 'window-atom nil))
window t)))
;; Check children.
(unless (window-buffer window)
(window--atom-check-1 (window-left-child window))
(window--atom-check-1 (window-top-child window))))
;; Check right sibling
(window--atom-check-1 (window-right window))))
(defun window--atom-check (&optional frame)
"Check atomicity of all windows on FRAME.
FRAME defaults to the selected frame. If an atomic window is
wrongly configured, reset the atomicity of all its windows on
FRAME to nil. An atomic window is wrongly configured if it has
no child windows or one of its child windows is not atomic."
(window--atom-check-1 (frame-root-window frame)))
;; Side windows.
(defvar window-sides '(left top right bottom)
"Window sides.")
(defcustom window-sides-vertical nil
"If non-nil, left and right side windows are full height.
Otherwise, top and bottom side windows are full width."
:type 'boolean
:group 'windows
:version "24.1")
(defcustom window-sides-slots '(nil nil nil nil)
"Maximum number of side window slots.
The value is a list of four elements specifying the number of
side window slots on (in this order) the left, top, right and
bottom side of each frame. If an element is a number, this means
to display at most that many side windows on the corresponding
side. If an element is nil, this means there's no bound on the
number of slots on that side."
:version "24.1"
:risky t
:type
'(list
:value (nil nil nil nil)
(choice
:tag "Left"
:help-echo "Maximum slots of left side window."
:value nil
:format "%[Left%] %v\n"
(const :tag "Unlimited" :format "%t" nil)
(integer :tag "Number" :value 2 :size 5))
(choice
:tag "Top"
:help-echo "Maximum slots of top side window."
:value nil
:format "%[Top%] %v\n"
(const :tag "Unlimited" :format "%t" nil)
(integer :tag "Number" :value 3 :size 5))
(choice
:tag "Right"
:help-echo "Maximum slots of right side window."
:value nil
:format "%[Right%] %v\n"
(const :tag "Unlimited" :format "%t" nil)
(integer :tag "Number" :value 2 :size 5))
(choice
:tag "Bottom"
:help-echo "Maximum slots of bottom side window."
:value nil
:format "%[Bottom%] %v\n"
(const :tag "Unlimited" :format "%t" nil)
(integer :tag "Number" :value 3 :size 5)))
:group 'windows)
(defun window--major-non-side-window (&optional frame)
"Return the major non-side window of frame FRAME.
The optional argument FRAME must be a live frame and defaults to
the selected one.
If FRAME has at least one side window, the major non-side window
is either an internal non-side window such that all other
non-side windows on FRAME descend from it, or the single live
non-side window of FRAME. If FRAME has no side windows, return
its root window."
(let ((frame (window-normalize-frame frame))
major sibling)
;; Set major to the _last_ window found by `walk-window-tree' that
;; is not a side window but has a side window as its sibling.
(walk-window-tree
(lambda (window)
(and (not (window-parameter window 'window-side))
(or (and (setq sibling (window-prev-sibling window))
(window-parameter sibling 'window-side))
(and (setq sibling (window-next-sibling window))
(window-parameter sibling 'window-side)))
(setq major window)))
frame t 'nomini)
(or major (frame-root-window frame))))
(defun window--major-side-window (side)
"Return major side window on SIDE.
SIDE must be one of the symbols `left', `top', `right' or
`bottom'. Return nil if no such window exists."
(let ((root (frame-root-window))
window)
;; (1) If a window on the opposite side exists, return that window's
;; sibling.
;; (2) If the new window shall span the entire side, return the
;; frame's root window.
;; (3) If a window on an orthogonal side exists, return that
;; window's sibling.
;; (4) Otherwise return the frame's root window.
(cond
((or (and (eq side 'left)
(setq window (window-with-parameter 'window-side 'right nil t)))
(and (eq side 'top)
(setq window (window-with-parameter 'window-side 'bottom nil t))))
(window-prev-sibling window))
((or (and (eq side 'right)
(setq window (window-with-parameter 'window-side 'left nil t)))
(and (eq side 'bottom)
(setq window (window-with-parameter 'window-side 'top nil t))))
(window-next-sibling window))
((memq side '(left right))
(cond
(window-sides-vertical
root)
((setq window (window-with-parameter 'window-side 'top nil t))
(window-next-sibling window))
((setq window (window-with-parameter 'window-side 'bottom nil t))
(window-prev-sibling window))
(t root)))
((memq side '(top bottom))
(cond
((not window-sides-vertical)
root)
((setq window (window-with-parameter 'window-side 'left nil t))
(window-next-sibling window))
((setq window (window-with-parameter 'window-side 'right nil t))
(window-prev-sibling window))
(t root))))))
(defun display-buffer-in-major-side-window (buffer side slot &optional alist)
"Display BUFFER in a new window on SIDE of the selected frame.
SIDE must be one of `left', `top', `right' or `bottom'. SLOT
specifies the slot to use. ALIST is an association list of
symbols and values as passed to `display-buffer-in-side-window'.
This function may be called only if no window on SIDE exists yet.
The new window automatically becomes the \"major\" side window on
SIDE. Return the new window, nil if its creation window failed."
(let* ((left-or-right (memq side '(left right)))
(major (window--major-side-window side))
(on-side (cond
((eq side 'top) 'above)
((eq side 'bottom) 'below)
(t side)))
;; The following two bindings will tell `split-window' to take
;; the space for the new window from `major' and not make a new
;; parent window unless needed.
(window-combination-resize 'side)
(window-combination-limit nil)
(new (split-window major nil on-side)))
(when new
;; Initialize `window-side' parameter of new window to SIDE.
(set-window-parameter new 'window-side side)
;; Install `window-slot' parameter of new window.
(set-window-parameter new 'window-slot slot)
;; Install `delete-window' parameter thus making sure that when
;; the new window is deleted, a side window on the opposite side
;; does not get resized.
(set-window-parameter new 'delete-window 'delete-side-window)
;; Auto-adjust height/width of new window unless a size has been
;; explicitly requested.
(unless (if left-or-right
(cdr (assq 'window-width alist))
(cdr (assq 'window-height alist)))
(setq alist
(cons
(cons
(if left-or-right 'window-width 'window-height)
(/ (window-total-size (frame-root-window) left-or-right)
;; By default use a fourth of the size of the frame's
;; root window.
4))
alist)))
;; Install BUFFER in new window and return NEW.
(window--display-buffer buffer new 'window alist 'side))))
(defun delete-side-window (window)
"Delete side window WINDOW."
(let ((window-combination-resize
(window-parameter (window-parent window) 'window-side))
(ignore-window-parameters t))
(delete-window window)))
(defun display-buffer-in-side-window (buffer alist)
"Display BUFFER in a side window of the selected frame.
ALIST is an association list of symbols and values. The
following special symbols can be used in ALIST.
`side' denotes the side of the frame where the new window shall
be located. Valid values are `bottom', `right', `top' and
`left'. The default is `bottom'.
`slot' if non-nil, specifies the window slot where to display
BUFFER. A value of zero or nil means use the middle slot on
the specified side. A negative value means use a slot
preceding (that is, above or on the left of) the middle slot.
A positive value means use a slot following (that is, below or
on the right of) the middle slot. The default is zero."
(let ((side (or (cdr (assq 'side alist)) 'bottom))
(slot (or (cdr (assq 'slot alist)) 0)))
(cond
((not (memq side '(top bottom left right)))
(error "Invalid side %s specified" side))
((not (numberp slot))
(error "Invalid slot %s specified" slot)))
(let* ((major (window-with-parameter 'window-side side nil t))
;; `major' is the major window on SIDE, `windows' the list of
;; life windows on SIDE.
(windows
(when major
(let (windows)
(walk-window-tree
(lambda (window)
(when (eq (window-parameter window 'window-side) side)
(setq windows (cons window windows))))
nil nil 'nomini)
(nreverse windows))))
(slots (when major (max 1 (window-child-count major))))
(max-slots
(nth (cond
((eq side 'left) 0)
((eq side 'top) 1)
((eq side 'right) 2)
((eq side 'bottom) 3))
window-sides-slots))
window this-window this-slot prev-window next-window
best-window best-slot abs-slot)
(cond
((and (numberp max-slots) (<= max-slots 0))
;; No side-slots available on this side. Don't create an error,
;; just return nil.
nil)
((not windows)
;; No major window exists on this side, make one.
(display-buffer-in-major-side-window buffer side slot alist))
(t
;; Scan windows on SIDE.
(catch 'found
(dolist (window windows)
(setq this-slot (window-parameter window 'window-slot))
(cond
;; The following should not happen and probably be checked
;; by window--side-check.
((not (numberp this-slot)))
((= this-slot slot)
;; A window with a matching slot has been found.
(setq this-window window)
(throw 'found t))
(t
;; Check if this window has a better slot value wrt the
;; slot of the window we want.
(setq abs-slot
(if (or (and (> this-slot 0) (> slot 0))
(and (< this-slot 0) (< slot 0)))
(abs (- slot this-slot))
(+ (abs slot) (abs this-slot))))
(unless (and best-slot (<= best-slot abs-slot))
(setq best-window window)
(setq best-slot abs-slot))
(cond
((<= this-slot slot)
(setq prev-window window))
((not next-window)
(setq next-window window)))))))
;; `this-window' is the first window with the same SLOT.
;; `prev-window' is the window with the largest slot < SLOT. A new
;; window will be created after it.
;; `next-window' is the window with the smallest slot > SLOT. A new
;; window will be created before it.
;; `best-window' is the window with the smallest absolute difference
;; of its slot and SLOT.
;; Note: We dedicate the window used softly to its buffer to
;; avoid that "other" (non-side) buffer display functions steal
;; it from us. This must eventually become customizable via
;; ALIST (or, better, avoided in the "other" functions).
(or (and this-window
;; Reuse `this-window'.
(window--display-buffer buffer this-window 'reuse alist 'side))
(and (or (not max-slots) (< slots max-slots))
(or (and next-window
;; Make new window before `next-window'.
(let ((next-side
(if (memq side '(left right)) 'above 'left))
(window-combination-resize 'side))
(setq window (split-window next-window nil next-side))
;; When the new window is deleted, its space
;; is returned to other side windows.
(set-window-parameter
window 'delete-window 'delete-side-window)
window))
(and prev-window
;; Make new window after `prev-window'.
(let ((prev-side
(if (memq side '(left right)) 'below 'right))
(window-combination-resize 'side))
(setq window (split-window prev-window nil prev-side))
;; When the new window is deleted, its space
;; is returned to other side windows.
(set-window-parameter
window 'delete-window 'delete-side-window)
window)))
(set-window-parameter window 'window-slot slot)
(window--display-buffer buffer window 'window alist 'side))
(and best-window
;; Reuse `best-window'.
(progn
;; Give best-window the new slot value.
(set-window-parameter best-window 'window-slot slot)
(window--display-buffer
buffer best-window 'reuse alist 'side)))))))))
(defun window--side-check (&optional frame)
"Check the side window configuration of FRAME.
FRAME defaults to the selected frame.
A valid side window configuration preserves the following two
invariants:
- If there exists a window whose window-side parameter is
non-nil, there must exist at least one live window whose
window-side parameter is nil.
- If a window W has a non-nil window-side parameter (i) it must
have a parent window and that parent's window-side parameter
must be either nil or the same as for W, and (ii) any child
window of W must have the same window-side parameter as W.
If the configuration is invalid, reset the window-side parameters
of all windows on FRAME to nil."
(let (left top right bottom none side parent parent-side)
(when (or (catch 'reset
(walk-window-tree
(lambda (window)
(setq side (window-parameter window 'window-side))
(setq parent (window-parent window))
(setq parent-side
(and parent (window-parameter parent 'window-side)))
;; The following `cond' seems a bit tedious, but I'd
;; rather stick to using just the stack.
(cond
(parent-side
(when (not (eq parent-side side))
;; A parent whose window-side is non-nil must
;; have a child with the same window-side.
(throw 'reset t)))
((not side)
(when (window-buffer window)
;; Record that we have at least one non-side,
;; live window.
(setq none t)))
((if (memq side '(left top))
(window-prev-sibling window)
(window-next-sibling window))
;; Left and top major side windows must not have a
;; previous sibling, right and bottom major side
;; windows must not have a next sibling.
(throw 'reset t))
;; Now check that there's no more than one major
;; window for any of left, top, right and bottom.
((eq side 'left)
(if left (throw 'reset t) (setq left t)))
((eq side 'top)
(if top (throw 'reset t) (setq top t)))
((eq side 'right)
(if right (throw 'reset t) (setq right t)))
((eq side 'bottom)
(if bottom (throw 'reset t) (setq bottom t)))
(t
(throw 'reset t))))
frame t 'nomini))
;; If there's a side window, there must be at least one
;; non-side window.
(and (or left top right bottom) (not none)))
(walk-window-tree
(lambda (window)
(set-window-parameter window 'window-side nil))
frame t 'nomini))))
(defun window--check (&optional frame)
"Check atomic and side windows on FRAME.
FRAME defaults to the selected frame."
(window--side-check frame)
(window--atom-check frame))
;; Dumping frame/window contents.
(defun window--dump-window (&optional window erase)
"Dump WINDOW to buffer *window-frame-dump*.
WINDOW must be a valid window and defaults to the selected one.
Optional argument ERASE non-nil means erase *window-frame-dump*
before writing to it."
(setq window (window-normalize-window window))
(with-current-buffer (get-buffer-create "*window-frame-dump*")
(when erase (erase-buffer))
(insert
(format "%s parent: %s\n" window (window-parent window))
(format "pixel left: %s top: %s size: %s x %s new: %s\n"
(window-pixel-left window) (window-pixel-top window)
(window-size window t t) (window-size window nil t)
(window-new-pixel window))
(format "char left: %s top: %s size: %s x %s new: %s\n"
(window-left-column window) (window-top-line window)
(window-total-size window t) (window-total-size window)
(window-new-total window))
(format "normal: %s x %s new: %s\n"
(window-normal-size window t) (window-normal-size window)
(window-new-normal window)))
(when (window-live-p window)
(let ((fringes (window-fringes window))
(margins (window-margins window)))
(insert
(format "body pixel: %s x %s char: %s x %s\n"
(window-body-width window t) (window-body-height window t)
(window-body-width window) (window-body-height window))
(format "width left fringe: %s left margin: %s right margin: %s\n"
(car fringes) (or (car margins) 0) (or (cdr margins) 0))
(format "width right fringe: %s scroll-bar: %s divider: %s\n"
(cadr fringes)
(window-scroll-bar-width window)
(window-right-divider-width window))
(format "height header-line: %s mode-line: %s divider: %s\n"
(window-header-line-height window)
(window-mode-line-height window)
(window-bottom-divider-width window)))))
(insert "\n")))
(defun window--dump-frame (&optional window-or-frame)
"Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
WINDOW-OR-FRAME can be a frame or a window and defaults to the
selected frame. When WINDOW-OR-FRAME is a window, dump that
window's frame. The buffer *window-frame-dump* is erased before
dumping to it."
(let* ((window
(cond
((or (not window-or-frame)
(frame-live-p window-or-frame))
(frame-root-window window-or-frame))
((or (window-live-p window-or-frame)
(window-child window-or-frame))
window-or-frame)
(t
(frame-root-window))))
(frame (window-frame window)))
(with-current-buffer (get-buffer-create "*window-frame-dump*")
(erase-buffer)
(insert
(format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
(frame-pixel-width frame) (frame-pixel-height frame)
(frame-total-cols frame) (frame-text-lines frame) ; (frame-total-lines frame)
(frame-char-width frame) (frame-char-height frame))
(format "frame text pixel: %s x %s cols/lines: %s x %s\n"
(frame-text-width frame) (frame-text-height frame)
(frame-text-cols frame) (frame-text-lines frame))
(format "tool: %s scroll: %s fringe: %s border: %s right: %s bottom: %s\n\n"
(if (fboundp 'tool-bar-height)
(tool-bar-height frame t)
"0")
(frame-scroll-bar-width frame)
(frame-fringe-width frame)
(frame-border-width frame)
(frame-right-divider-width frame)
(frame-bottom-divider-width frame)))
(walk-window-tree 'window--dump-window frame t t))))
;;; Window sizes.
(defun window-total-size (&optional window horizontal round)
"Return the total height or width of WINDOW.
WINDOW must be a valid window and defaults to the selected one.
If HORIZONTAL is omitted or nil, return the total height of
WINDOW, in lines. If WINDOW is live, its total height includes,
in addition to the height of WINDOW's text, the heights of
WINDOW's mode and header line and a bottom divider, if any.
If HORIZONTAL is non-nil, return the total width of WINDOW, in
columns. If WINDOW is live, its total width includes, in
addition to the width of WINDOW's text, the widths of WINDOW's
fringes, margins, scroll bars and its right divider, if any.
If WINDOW is internal, return the respective size of the screen
areas spanned by its children.
Optional argument ROUND is handled as for `window-total-height'
and `window-total-width'."
(if horizontal
(window-total-width window round)
(window-total-height window round)))
(defun window-size (&optional window horizontal pixelwise round)
"Return the height or width of WINDOW.
WINDOW must be a valid window and defaults to the selected one.
If HORIZONTAL is omitted or nil, return the total height of
WINDOW, in lines, like `window-total-height'. Otherwise return
the total width, in columns, like `window-total-width'.
Optional argument PIXELWISE means return the pixel size of WINDOW
like `window-pixel-height' and `window-pixel-width'.
Optional argument ROUND is ignored if PIXELWISE is non-nil and
handled as for `window-total-height' and `window-total-width'
otherwise."
(if horizontal
(if pixelwise
(window-pixel-width window)
(window-total-width window round))
(if pixelwise
(window-pixel-height window)
(window-total-height window round))))
(defvar window-size-fixed nil
"Non-nil in a buffer means windows displaying the buffer are fixed-size.
If the value is `height', then only the window's height is fixed.
If the value is `width', then only the window's width is fixed.
Any other non-nil value fixes both the width and the height.
Emacs won't change the size of any window displaying that buffer,
unless it has no other choice (like when deleting a neighboring
window).")
(make-variable-buffer-local 'window-size-fixed)
(defun window--size-ignore-p (window ignore)
"Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
(if (window-valid-p ignore) (eq window ignore) ignore))
(defun window-safe-min-size (&optional window horizontal pixelwise)
"Return safe minimum size of WINDOW.
WINDOW must be a valid window and defaults to the selected one.
Optional argument HORIZONTAL non-nil means return the minimum
number of columns of WINDOW; otherwise return the minimum number
of WINDOW's lines.
Optional argument PIXELWISE non-nil means return the minimum pixel-size
of WINDOW."
(setq window (window-normalize-window window))
(if pixelwise
(if horizontal
(* window-safe-min-width
(frame-char-width (window-frame window)))
(* window-safe-min-height
(frame-char-height (window-frame window))))
(if horizontal window-safe-min-width window-safe-min-height)))
(defun window-min-size (&optional window horizontal ignore pixelwise)
"Return the minimum size of WINDOW.
WINDOW must be a valid window and defaults to the selected one.
Optional argument HORIZONTAL non-nil means return the minimum
number of columns of WINDOW; otherwise return the minimum number
of WINDOW's lines.
Optional argument IGNORE, if non-nil, means ignore restrictions
imposed by fixed size windows, `window-min-height' or
`window-min-width' settings. If IGNORE equals `safe', live
windows may get as small as `window-safe-min-height' lines and
`window-safe-min-width' columns. If IGNORE is a window, ignore
restrictions for that window only. Any other non-nil value
means ignore all of the above restrictions for all windows.
Optional argument PIXELWISE non-nil means return the minimum pixel-size
of WINDOW."
(window--min-size-1
(window-normalize-window window) horizontal ignore pixelwise))
(defun window--min-size-1 (window horizontal ignore pixelwise)
"Internal function of `window-min-size'."
(let ((sub (window-child window)))
(if sub
(let ((value 0))
;; WINDOW is an internal window.
(if (window-combined-p sub horizontal)
;; The minimum size of an iso-combination is the sum of
;; the minimum sizes of its child windows.
(while sub
(setq value (+ value
(window--min-size-1
sub horizontal ignore pixelwise)))
(setq sub (window-right sub)))
;; The minimum size of an ortho-combination is the maximum
;; of the minimum sizes of its child windows.
(while sub
(setq value (max value
(window--min-size-1
sub horizontal ignore pixelwise)))
(setq sub (window-right sub))))
value)
(with-current-buffer (window-buffer window)
(cond
((and (not (window--size-ignore-p window ignore))
(window-size-fixed-p window horizontal))
;; The minimum size of a fixed size window is its size.
(window-size window horizontal pixelwise))
((or (eq ignore 'safe) (eq ignore window))
;; If IGNORE equals `safe' or WINDOW return the safe values.
(window-safe-min-size window horizontal pixelwise))
(horizontal
;; For the minimum width of a window take fringes and
;; scroll-bars into account. This is questionable and should
;; be removed as soon as we are able to split (and resize)
;; windows such that the new (or resized) windows can get a
;; size less than the user-specified `window-min-height' and
;; `window-min-width'.
(let* ((char-size (frame-char-size window t))
(fringes (window-fringes window))
(pixel-width
(+ (window-safe-min-size window t t)
(car fringes) (cadr fringes)
(window-scroll-bar-width window)
(window-right-divider-width window))))
(if pixelwise
(max
(if window-resize-pixelwise
pixel-width
;; Round up to next integral of columns.
(* (ceiling pixel-width char-size) char-size))
(if (window--size-ignore-p window ignore)
0
(window-min-pixel-width)))
(max
(ceiling pixel-width char-size)
(if (window--size-ignore-p window ignore)
0
window-min-width)))))
((let ((char-size (frame-char-size window))
(pixel-height
(+ (window-safe-min-size window nil t)
(window-header-line-height window)
(window-mode-line-height window)
(window-bottom-divider-width window))))
(if pixelwise
(max
(if window-resize-pixelwise
pixel-height
;; Round up to next integral of lines.
(* (ceiling pixel-height char-size) char-size))
(if (window--size-ignore-p window ignore)
0
(window-min-pixel-height)))
(max (ceiling pixel-height char-size)
(if (window--size-ignore-p window ignore)
0
window-min-height))))))))))
(defun window-sizable (window delta &optional horizontal ignore pixelwise)
"Return DELTA if DELTA lines can be added to WINDOW.
WINDOW must be a valid window and defaults to the selected one.
Optional argument HORIZONTAL non-nil means return DELTA if DELTA
columns can be added to WINDOW. A return value of zero means
that no lines (or columns) can be added to WINDOW.
This function looks only at WINDOW and, recursively, its child
windows. The function `window-resizable' looks at other windows
as well.
DELTA positive means WINDOW shall be enlarged by DELTA lines or
columns. If WINDOW cannot be enlarged by DELTA lines or columns
return the maximum value in the range 0..DELTA by which WINDOW
can be enlarged.
DELTA negative means WINDOW shall be shrunk by -DELTA lines or
columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
return the minimum value in the range DELTA..0 by which WINDOW
can be shrunk.
Optional argument IGNORE non-nil means ignore restrictions
imposed by fixed size windows, `window-min-height' or
`window-min-width' settings. If IGNORE equals `safe', live
windows may get as small as `window-safe-min-height' lines and
`window-safe-min-width' columns. If IGNORE is a window, ignore
restrictions for that window only. Any other non-nil value means
ignore all of the above restrictions for all windows.
Optional argument PIXELWISE non-nil means interpret DELTA as
pixels."
(setq window (window-normalize-window window))
(cond
((< delta 0)
(max (- (window-min-size window horizontal ignore pixelwise)
(window-size window horizontal pixelwise))
delta))
((window--size-ignore-p window ignore)
delta)
((> delta 0)
(if (window-size-fixed-p window horizontal)
0
delta))
(t 0)))
(defun window-sizable-p (window delta &optional horizontal ignore pixelwise)
"Return t if WINDOW can be resized by DELTA lines.
WINDOW must be a valid window and defaults to the selected one.
For the meaning of the arguments of this function see the
doc-string of `window-sizable'."
(setq window (window-normalize-window window))
(if (> delta 0)
(>= (window-sizable window delta horizontal ignore pixelwise)
delta)
(<= (window-sizable window delta horizontal ignore pixelwise)
delta)))
(defun window--size-fixed-1 (window horizontal)
"Internal function for `window-size-fixed-p'."
(let ((sub (window-child window)))
(catch 'fixed
(if sub
;; WINDOW is an internal window.
(if (window-combined-p sub horizontal)
;; An iso-combination is fixed size if all its child
;; windows are fixed-size.
(progn
(while sub
(unless (window--size-fixed-1 sub horizontal)
;; We found a non-fixed-size child window, so
;; WINDOW's size is not fixed.
(throw 'fixed nil))
(setq sub (window-right sub)))
;; All child windows are fixed-size, so WINDOW's size is
;; fixed.
(throw 'fixed t))
;; An ortho-combination is fixed-size if at least one of its
;; child windows is fixed-size.
(while sub
(when (window--size-fixed-1 sub horizontal)
;; We found a fixed-size child window, so WINDOW's size
;; is fixed.
(throw 'fixed t))
(setq sub (window-right sub))))
;; WINDOW is a live window.
(with-current-buffer (window-buffer window)
(if horizontal
(memq window-size-fixed '(width t))
(memq window-size-fixed '(height t))))))))
(defun window-size-fixed-p (&optional window horizontal)
"Return non-nil if WINDOW's height is fixed.
WINDOW must be a valid window and defaults to the selected one.
Optional argument HORIZONTAL non-nil means return non-nil if
WINDOW's width is fixed.
If this function returns nil, this does not necessarily mean that
WINDOW can be resized in the desired direction. The function
`window-resizable' can tell that."
(window--size-fixed-1
(window-normalize-window window) horizontal))
(defun window--min-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
"Internal function for `window-min-delta'."
(if (not (window-parent window))
;; If we can't go up, return zero.
0
;; Else try to find a non-fixed-size sibling of WINDOW.
(let* ((parent (window-parent window))
(sub (window-child parent)))
(catch 'done
(if (window-combined-p sub horizontal)
;; In an iso-combination throw DELTA if we find at least one
;; child window and that window is either not fixed-size or
;; we can ignore fixed-sizeness.
(let ((skip (eq trail 'after)))
(while sub
(cond
((eq sub window)
(setq skip (eq trail 'before)))
(skip)
((and (not (window--size-ignore-p window ignore))
(window-size-fixed-p sub horizontal)))
(t
;; We found a non-fixed-size child window.
(throw 'done delta)))
(setq sub (window-right sub))))
;; In an ortho-combination set DELTA to the minimum value by
;; which other child windows can shrink.
(while sub
(unless (eq sub window)
(setq delta
(min delta
(max (- (window-size sub horizontal pixelwise 'ceiling)
(window-min-size
sub horizontal ignore pixelwise))
0))))
(setq sub (window-right sub))))
(if noup
delta
(window--min-delta-1
parent delta horizontal ignore trail nil pixelwise))))))
(defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise)
"Return number of lines by which WINDOW can be shrunk.
WINDOW must be a valid window and defaults to the selected one.
Return zero if WINDOW cannot be shrunk.
Optional argument HORIZONTAL non-nil means return number of
columns by which WINDOW can be shrunk.
Optional argument IGNORE non-nil means ignore restrictions
imposed by fixed size windows, `window-min-height' or
`window-min-width' settings. If IGNORE is a window, ignore
restrictions for that window only. If IGNORE equals `safe',
live windows may get as small as `window-safe-min-height' lines
and `window-safe-min-width' columns. Any other non-nil value
means ignore all of the above restrictions for all windows.
Optional argument TRAIL restricts the windows that can be enlarged.
If its value is `before', only windows to the left of or above WINDOW
can be enlarged. If it is `after', only windows to the right of or
below WINDOW can be enlarged.
Optional argument NOUP non-nil means don't go up in the window
tree, but try to enlarge windows within WINDOW's combination only.
Optional argument NODOWN non-nil means don't check whether WINDOW
itself (and its child windows) can be shrunk; check only whether
at least one other window can be enlarged appropriately.
Optional argument PIXELWISE non-nil means return number of pixels
by which WINDOW can be shrunk."
(setq window (window-normalize-window window))
(let ((size (window-size window horizontal pixelwise 'floor))
(minimum (window-min-size window horizontal ignore pixelwise)))
(cond
(nodown
;; If NODOWN is t, try to recover the entire size of WINDOW.
(window--min-delta-1
window size horizontal ignore trail noup pixelwise))
((<= size minimum)
;; If NODOWN is nil and WINDOW's size is already at its minimum,
;; there's nothing to recover.
0)
(t
;; Otherwise, try to recover whatever WINDOW is larger than its
;; minimum size.
(window--min-delta-1
window (- size minimum) horizontal ignore trail noup pixelwise)))))
(defun window--max-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
"Internal function of `window-max-delta'."
(if (not (window-parent window))
;; Can't go up. Return DELTA.
delta
(let* ((parent (window-parent window))
(sub (window-child parent)))
(catch 'fixed
(if (window-combined-p sub horizontal)
;; For an iso-combination calculate how much we can get from
;; other child windows.
(let ((skip (eq trail 'after)))
(while sub
(cond
((eq sub window)
(setq skip (eq trail 'before)))
(skip)
(t
(setq delta
(+ delta
(max
(- (window-size sub horizontal pixelwise 'floor)
(window-min-size
sub horizontal ignore pixelwise))
0)))))
(setq sub (window-right sub))))
;; For an ortho-combination throw DELTA when at least one
;; child window is fixed-size.
(while sub
(when (and (not (eq sub window))
(not (window--size-ignore-p sub ignore))
(window-size-fixed-p sub horizontal))
(throw 'fixed delta))
(setq sub (window-right sub))))
(if noup
;; When NOUP is nil, DELTA is all we can get.
delta
;; Else try with parent of WINDOW, passing the DELTA we
;; recovered so far.
(window--max-delta-1
parent delta horizontal ignore trail nil pixelwise))))))
(defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise)
"Return maximum number of lines by which WINDOW can be enlarged.
WINDOW must be a valid window and defaults to the selected one.
The return value is zero if WINDOW cannot be enlarged.
Optional argument HORIZONTAL non-nil means return maximum number
of columns by which WINDOW can be enlarged.
Optional argument IGNORE non-nil means ignore restrictions
imposed by fixed size windows, `window-min-height' or
`window-min-width' settings. If IGNORE is a window, ignore
restrictions for that window only. If IGNORE equals `safe',
live windows may get as small as `window-safe-min-height' lines
and `window-safe-min-width' columns. Any other non-nil value means
ignore all of the above restrictions for all windows.
Optional argument TRAIL restricts the windows that can be enlarged.
If its value is `before', only windows to the left of or above WINDOW
can be enlarged. If it is `after', only windows to the right of or
below WINDOW can be enlarged.
Optional argument NOUP non-nil means don't go up in the window
tree but try to obtain the entire space from windows within
WINDOW's combination.
Optional argument NODOWN non-nil means do not check whether
WINDOW itself (and its child windows) can be enlarged; check
only whether other windows can be shrunk appropriately.
Optional argument PIXELWISE non-nil means return number of
pixels by which WINDOW can be enlarged."
(setq window (window-normalize-window window))
(if (and (not (window--size-ignore-p window ignore))
(not nodown) (window-size-fixed-p window horizontal))
;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
;; size.
0
;; WINDOW has no fixed size.
(window--max-delta-1 window 0 horizontal ignore trail noup pixelwise)))
;; Make NOUP also inhibit the min-size check.
(defun window--resizable (window delta &optional horizontal ignore trail noup nodown pixelwise)
"Return DELTA if WINDOW can be resized vertically by DELTA lines.
WINDOW must be a valid window and defaults to the selected one.
Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
can be resized horizontally by DELTA columns. A return value of
zero means that WINDOW is not resizable.
DELTA positive means WINDOW shall be enlarged by DELTA lines or
columns. If WINDOW cannot be enlarged by DELTA lines or columns,
return the maximum value in the range 0..DELTA by which WINDOW
can be enlarged.
DELTA negative means WINDOW shall be shrunk by -DELTA lines or
columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
return the minimum value in the range DELTA..0 that can be used
for shrinking WINDOW.
Optional argument IGNORE non-nil means ignore restrictions
imposed by fixed size windows, `window-min-height' or
`window-min-width' settings. If IGNORE is a window, ignore
restrictions for that window only. If IGNORE equals `safe',
live windows may get as small as `window-safe-min-height' lines
and `window-safe-min-width' columns. Any other non-nil value
means ignore all of the above restrictions for all windows.
Optional argument TRAIL `before' means only windows to the left
of or below WINDOW can be shrunk. Optional argument TRAIL
`after' means only windows to the right of or above WINDOW can be
shrunk.
Optional argument NOUP non-nil means don't go up in the window
tree but check only whether space can be obtained from (or given
to) WINDOW's siblings.
Optional argument NODOWN non-nil means don't go down in the
window tree. This means do not check whether resizing would
violate size restrictions of WINDOW or its child windows.
Optional argument PIXELWISE non-nil means interpret DELTA as
number of pixels."
(setq window (window-normalize-window window))
(cond
((< delta 0)
(max (- (window-min-delta
window horizontal ignore trail noup nodown pixelwise))
delta))
((> delta 0)
(min (window-max-delta
window horizontal ignore trail noup nodown pixelwise)
delta))
(t 0)))
(defun window--resizable-p (window delta &optional horizontal ignore trail noup nodown pixelwise)
"Return t if WINDOW can be resized vertically by DELTA lines.
WINDOW must be a valid window and defaults to the selected one.
For the meaning of the arguments of this function see the
doc-string of `window--resizable'.
Optional argument PIXELWISE non-nil means interpret DELTA as
pixels."
(setq window (window-normalize-window window))
(if (> delta 0)
(>= (window--resizable
window delta horizontal ignore trail noup nodown pixelwise)
delta)
(<= (window--resizable
window delta horizontal ignore trail noup nodown pixelwise)
delta)))
(defun window-resizable (window delta &optional horizontal ignore pixelwise)
"Return DELTA if WINDOW can be resized vertically by DELTA lines.
WINDOW must be a valid window and defaults to the selected one.
Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
can be resized horizontally by DELTA columns. A return value of
zero means that WINDOW is not resizable.
DELTA positive means WINDOW shall be enlarged by DELTA lines or
columns. If WINDOW cannot be enlarged by DELTA lines or columns
return the maximum value in the range 0..DELTA by which WINDOW
can be enlarged.
DELTA negative means WINDOW shall be shrunk by -DELTA lines or
columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
return the minimum value in the range DELTA..0 that can be used
for shrinking WINDOW.
Optional argument IGNORE non-nil means ignore restrictions
imposed by fixed size windows, `window-min-height' or
`window-min-width' settings. If IGNORE is a window, ignore
restrictions for that window only. If IGNORE equals `safe',
live windows may get as small as `window-safe-min-height' lines
and `window-safe-min-width' columns. Any other non-nil value
means ignore all of the above restrictions for all windows.
Optional argument PIXELWISE non-nil means interpret DELTA as
pixels."
(setq window (window-normalize-window window))
(window--resizable window delta horizontal ignore nil nil nil pixelwise))
(defun window-resizable-p (window delta &optional horizontal ignore pixelwise)
"Return t if WINDOW can be resized vertically by DELTA lines.
WINDOW must be a valid window and defaults to the selected one.
For the meaning of the arguments of this function see the
doc-string of `window-resizable'."
(setq window (window-normalize-window window))
(if (> delta 0)
(>= (window--resizable
window delta horizontal ignore nil nil nil pixelwise)
delta)
(<= (window--resizable
window delta horizontal ignore nil nil nil pixelwise)
delta)))
;; Aliases of functions defined in window.c.
(defalias 'window-height 'window-total-height)
(defalias 'window-width 'window-body-width)
;; Eventually the following two should work pixelwise.
;; See discussion in bug#4543.
(defun window-full-height-p (&optional window)
"Return t if WINDOW is as high as its containing frame.
More precisely, return t if and only if the total height of
WINDOW equals the total height of the root window of WINDOW's
frame. WINDOW must be a valid window and defaults to the
selected one."
(setq window (window-normalize-window window))
(= (window-pixel-height window)
(window-pixel-height (frame-root-window window))))
(defun window-full-width-p (&optional window)
"Return t if WINDOW is as wide as its containing frame.
More precisely, return t if and only if the total width of WINDOW
equals the total width of the root window of WINDOW's frame.
WINDOW must be a valid window and defaults to the selected one."
(setq window (window-normalize-window window))
(= (window-pixel-width window)
(window-pixel-width (frame-root-window window))))
(defun window-body-size (&optional window horizontal pixelwise)
"Return the height or width of WINDOW's text area.
WINDOW must be a live window and defaults to the selected one.
If HORIZONTAL is omitted or nil, return the height of the text
area, like `window-body-height'. Otherwise, return the width of
the text area, like `window-body-width'. In either case, the
optional argument PIXELWISE is passed to the functions."
(if horizontal
(window-body-width window pixelwise)
(window-body-height window pixelwise)))
(defun window-current-scroll-bars (&optional window)
"Return the current scroll bar settings for WINDOW.
WINDOW must be a live window and defaults to the selected one.
The return value is a cons cell (VERTICAL . HORIZONTAL) where
VERTICAL specifies the current location of the vertical scroll
bars (`left', `right', or nil), and HORIZONTAL specifies the
current location of the horizontal scroll bars (`top', `bottom',
or nil).
Unlike `window-scroll-bars', this function reports the scroll bar
type actually used, once frame defaults and `scroll-bar-mode' are
taken into account."
(setq window (window-normalize-window window t))
(let ((vert (nth 2 (window-scroll-bars window)))
(hor nil))
(when (or (eq vert t) (eq hor t))
(let ((fcsb (frame-current-scroll-bars (window-frame window))))
(if (eq vert t)
(setq vert (car fcsb)))
(if (eq hor t)
(setq hor (cdr fcsb)))))
(cons vert hor)))
(defun walk-windows (fun &optional minibuf all-frames)
"Cycle through all live windows, calling FUN for each one.
FUN must specify a function with a window as its sole argument.
The optional arguments MINIBUF and ALL-FRAMES specify the set of
windows to include in the walk.
MINIBUF t means include the minibuffer window even if the
minibuffer is not active. MINIBUF nil or omitted means include
the minibuffer window only if the minibuffer is active. Any
other value means do not include the minibuffer window even if
the minibuffer is active.
ALL-FRAMES nil or omitted means consider all windows on the
selected frame, plus the minibuffer window if specified by the
MINIBUF argument. If the minibuffer counts, consider all windows
on all frames that share that minibuffer too. The following
non-nil values of ALL-FRAMES have special meanings:
- t means consider all windows on all existing frames.
- `visible' means consider all windows on all visible frames on
the current terminal.
- 0 (the number zero) means consider all windows on all visible
and iconified frames on the current terminal.
- A frame means consider all windows on that frame only.
Anything else means consider all windows on the selected frame
and no others.
This function changes neither the order of recently selected
windows nor the buffer list."
;; If we start from the minibuffer window, don't fail to come
;; back to it.
(when (window-minibuffer-p)
(setq minibuf t))
;; Make sure to not mess up the order of recently selected
;; windows. Use `save-selected-window' and `select-window'
;; with second argument non-nil for this purpose.
(save-selected-window
(when (framep all-frames)
(select-window (frame-first-window all-frames) 'norecord))
(dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
(funcall fun walk-windows-window))))
(defun window-at-side-p (&optional window side)
"Return t if WINDOW is at SIDE of its containing frame.
WINDOW must be a valid window and defaults to the selected one.
SIDE can be any of the symbols `left', `top', `right' or
`bottom'. The default value nil is handled like `bottom'."
(setq window (window-normalize-window window))
(let ((edge
(cond
((eq side 'left) 0)
((eq side 'top) 1)
((eq side 'right) 2)
((memq side '(bottom nil)) 3))))
(= (nth edge (window-pixel-edges window))
(nth edge (window-pixel-edges (frame-root-window window))))))
(defun window-at-side-list (&optional frame side)
"Return list of all windows on SIDE of FRAME.
FRAME must be a live frame and defaults to the selected frame.
SIDE can be any of the symbols `left', `top', `right' or
`bottom'. The default value nil is handled like `bottom'."
(setq frame (window-normalize-frame frame))
(let (windows)
(walk-window-tree
(lambda (window)
(when (window-at-side-p window side)
(setq windows (cons window windows))))
frame nil 'nomini)
(nreverse windows)))
(defun window--in-direction-2 (window posn &optional horizontal)
"Support function for `window-in-direction'."
(if horizontal
(let ((top (window-pixel-top window)))
(if (> top posn)
(- top posn)
(- posn top (window-pixel-height window))))
(let ((left (window-pixel-left window)))
(if (> left posn)
(- left posn)
(- posn left (window-pixel-width window))))))
;; Predecessors to the below have been devised by Julian Assange in
;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
;; Neither of these allow to selectively ignore specific windows
;; (windows whose `no-other-window' parameter is non-nil) as targets of
;; the movement.
(defun window-in-direction (direction &optional window ignore sign wrap mini)
"Return window in DIRECTION as seen from WINDOW.
More precisely, return the nearest window in direction DIRECTION
as seen from the position of `window-point' in window WINDOW.
DIRECTION must be one of `above', `below', `left' or `right'.
WINDOW must be a live window and defaults to the selected one.
Do not return a window whose `no-other-window' parameter is
non-nil. If the nearest window's `no-other-window' parameter is
non-nil, try to find another window in the indicated direction.
If, however, the optional argument IGNORE is non-nil, return that
window even if its `no-other-window' parameter is non-nil.
Optional argument SIGN a negative number means to use the right
or bottom edge of WINDOW as reference position instead of
`window-point'. SIGN a positive number means to use the left or
top edge of WINDOW as reference position.
Optional argument WRAP non-nil means to wrap DIRECTION around
frame borders. This means to return for WINDOW at the top of the
frame and DIRECTION `above' the minibuffer window if the frame
has one, and a window at the bottom of the frame otherwise.
Optional argument MINI nil means to return the minibuffer window
if and only if it is currently active. MINI non-nil means to
return the minibuffer window even when it's not active. However,
if WRAP non-nil, always act as if MINI were nil.
Return nil if no suitable window can be found."
(setq window (window-normalize-window window t))
(unless (memq direction '(above below left right))
(error "Wrong direction %s" direction))
(let* ((frame (window-frame window))
(hor (memq direction '(left right)))
(first (if hor
(window-pixel-left window)
(window-pixel-top window)))
(last (+ first (window-size window hor t)))
;; The column / row value of `posn-at-point' can be nil for the
;; mini-window, guard against that.
(posn
(cond
((and (numberp sign) (< sign 0))
(if hor
(1- (+ (window-pixel-top window) (window-pixel-height window)))
(1- (+ (window-pixel-left window) (window-pixel-width window)))))
((and (numberp sign) (> sign 0))
(if hor
(window-pixel-top window)
(window-pixel-left window)))
((let ((posn-cons (nth 2 (posn-at-point (window-point window) window))))
(if hor
(+ (or (cdr posn-cons) 1) (window-pixel-top window))
(+ (or (car posn-cons) 1) (window-pixel-left window)))))))
(best-edge
(cond
((eq direction 'below) (frame-pixel-height frame))
((eq direction 'right) (frame-pixel-width frame))
(t -1)))
(best-edge-2 best-edge)
(best-diff-2 (if hor (frame-pixel-height frame) (frame-pixel-width frame)))
best best-2 best-diff-2-new)
(walk-window-tree
(lambda (w)
(let* ((w-top (window-pixel-top w))
(w-left (window-pixel-left w)))
(cond
((or (eq window w)
;; Ignore ourselves.
(and (window-parameter w 'no-other-window)
;; Ignore W unless IGNORE is non-nil.
(not ignore))))
(hor
(cond
((and (<= w-top posn)
(< posn (+ w-top (window-pixel-height w))))
;; W is to the left or right of WINDOW and covers POSN.
(when (or (and (eq direction 'left)
(or (and (<= w-left first) (> w-left best-edge))
(and wrap
(window-at-side-p window 'left)
(window-at-side-p w 'right))))
(and (eq direction 'right)
(or (and (>= w-left last) (< w-left best-edge))
(and wrap
(window-at-side-p window 'right)
(window-at-side-p w 'left)))))
(setq best-edge w-left)
(setq best w)))
((and (or (and (eq direction 'left)
(<= (+ w-left (window-pixel-width w)) first))
(and (eq direction 'right) (<= last w-left)))
;; W is to the left or right of WINDOW but does not
;; cover POSN.
(setq best-diff-2-new
(window--in-direction-2 w posn hor))
(or (< best-diff-2-new best-diff-2)
(and (= best-diff-2-new best-diff-2)
(if (eq direction 'left)
(> w-left best-edge-2)
(< w-left best-edge-2)))))
(setq best-edge-2 w-left)
(setq best-diff-2 best-diff-2-new)
(setq best-2 w))))
((and (<= w-left posn)
(< posn (+ w-left (window-pixel-width w))))
;; W is above or below WINDOW and covers POSN.
(when (or (and (eq direction 'above)
(or (and (<= w-top first) (> w-top best-edge))
(and wrap
(window-at-side-p window 'top)
(if (active-minibuffer-window)
(minibuffer-window-active-p w)
(window-at-side-p w 'bottom)))))
(and (eq direction 'below)
(or (and (>= w-top first) (< w-top best-edge))
(and wrap
(if (active-minibuffer-window)
(minibuffer-window-active-p window)
(window-at-side-p window 'bottom))
(window-at-side-p w 'top)))))
(setq best-edge w-top)
(setq best w)))
((and (or (and (eq direction 'above)
(<= (+ w-top (window-pixel-height w)) first))
(and (eq direction 'below) (<= last w-top)))
;; W is above or below WINDOW but does not cover POSN.
(setq best-diff-2-new
(window--in-direction-2 w posn hor))
(or (< best-diff-2-new best-diff-2)
(and (= best-diff-2-new best-diff-2)
(if (eq direction 'above)
(> w-top best-edge-2)
(< w-top best-edge-2)))))
(setq best-edge-2 w-top)
(setq best-diff-2 best-diff-2-new)
(setq best-2 w)))))
frame nil (and mini t))
(or best best-2)))
(defun get-window-with-predicate (predicate &optional minibuf all-frames default)
"Return a live window satisfying PREDICATE.
More precisely, cycle through all windows calling the function
PREDICATE on each one of them with the window as its sole
argument. Return the first window for which PREDICATE returns
non-nil. Windows are scanned starting with the window following
the selected window. If no window satisfies PREDICATE, return
DEFAULT.
MINIBUF t means include the minibuffer window even if the
minibuffer is not active. MINIBUF nil or omitted means include
the minibuffer window only if the minibuffer is active. Any
other value means do not include the minibuffer window even if
the minibuffer is active.
ALL-FRAMES nil or omitted means consider all windows on the selected
frame, plus the minibuffer window if specified by the MINIBUF
argument. If the minibuffer counts, consider all windows on all
frames that share that minibuffer too. The following non-nil
values of ALL-FRAMES have special meanings:
- t means consider all windows on all existing frames.
- `visible' means consider all windows on all visible frames on
the current terminal.
- 0 (the number zero) means consider all windows on all visible
and iconified frames on the current terminal.
- A frame means consider all windows on that frame only.
Anything else means consider all windows on the selected frame
and no others."
(catch 'found
(dolist (window (window-list-1
(next-window nil minibuf all-frames)
minibuf all-frames))
(when (funcall predicate window)
(throw 'found window)))
default))
(defalias 'some-window 'get-window-with-predicate)
(defun get-lru-window (&optional all-frames dedicated not-selected)
"Return the least recently used window on frames specified by ALL-FRAMES.
Return a full-width window if possible. A minibuffer window is
never a candidate. A dedicated window is never a candidate
unless DEDICATED is non-nil, so if all windows are dedicated, the
value is nil. Avoid returning the selected window if possible.
Optional argument NOT-SELECTED non-nil means never return the
selected window.
The following non-nil values of the optional argument ALL-FRAMES
have special meanings:
- t means consider all windows on all existing frames.
- `visible' means consider all windows on all visible frames on
the current terminal.
- 0 (the number zero) means consider all windows on all visible
and iconified frames on the current terminal.
- A frame means consider all windows on that frame only.
Any other value of ALL-FRAMES means consider all windows on the
selected frame and no others."
(let (best-window best-time second-best-window second-best-time time)
(dolist (window (window-list-1 nil 'nomini all-frames))
(when (and (or dedicated (not (window-dedicated-p window)))
(or (not not-selected) (not (eq window (selected-window)))))
(setq time (window-use-time window))
(if (or (eq window (selected-window))
(not (window-full-width-p window)))
(when (or (not second-best-time) (< time second-best-time))
(setq second-best-time time)
(setq second-best-window window))
(when (or (not best-time) (< time best-time))
(setq best-time time)
(setq best-window window)))))
(or best-window second-best-window)))
(defun get-mru-window (&optional all-frames dedicated not-selected)
"Return the most recently used window on frames specified by ALL-FRAMES.
A minibuffer window is never a candidate. A dedicated window is
never a candidate unless DEDICATED is non-nil, so if all windows
are dedicated, the value is nil. Optional argument NOT-SELECTED
non-nil means never return the selected window.
The following non-nil values of the optional argument ALL-FRAMES
have special meanings:
- t means consider all windows on all existing frames.
- `visible' means consider all windows on all visible frames on
the current terminal.
- 0 (the number zero) means consider all windows on all visible
and iconified frames on the current terminal.
- A frame means consider all windows on that frame only.
Any other value of ALL-FRAMES means consider all windows on the
selected frame and no others."
(let (best-window best-time time)
(dolist (window (window-list-1 nil 'nomini all-frames))
(setq time (window-use-time window))
(when (and (or dedicated (not (window-dedicated-p window)))
(or (not not-selected) (not (eq window (selected-window))))
(or (not best-time) (> time best-time)))
(setq best-time time)
(setq best-window window)))
best-window))
(defun get-largest-window (&optional all-frames dedicated not-selected)
"Return the largest window on frames specified by ALL-FRAMES.
A minibuffer window is never a candidate. A dedicated window is
never a candidate unless DEDICATED is non-nil, so if all windows
are dedicated, the value is nil. Optional argument NOT-SELECTED
non-nil means never return the selected window.
The following non-nil values of the optional argument ALL-FRAMES
have special meanings:
- t means consider all windows on all existing frames.
- `visible' means consider all windows on all visible frames on
the current terminal.
- 0 (the number zero) means consider all windows on all visible
and iconified frames on the current terminal.
- A frame means consider all windows on that frame only.
Any other value of ALL-FRAMES means consider all windows on the
selected frame and no others."
(let ((best-size 0)
best-window size)
(dolist (window (window-list-1 nil 'nomini all-frames))
(when (and (or dedicated (not (window-dedicated-p window)))
(or (not not-selected) (not (eq window (selected-window)))))
(setq size (* (window-pixel-height window)
(window-pixel-width window)))
(when (> size best-size)
(setq best-size size)
(setq best-window window))))
best-window))
(defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
"Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
BUFFER-OR-NAME may be a buffer or the name of an existing buffer
and defaults to the current buffer. Windows are scanned starting
with the selected window.
MINIBUF t means include the minibuffer window even if the
minibuffer is not active. MINIBUF nil or omitted means include
the minibuffer window only if the minibuffer is active. Any
other value means do not include the minibuffer window even if
the minibuffer is active.
ALL-FRAMES nil or omitted means consider all windows on the
selected frame, plus the minibuffer window if specified by the
MINIBUF argument. If the minibuffer counts, consider all windows
on all frames that share that minibuffer too. The following
non-nil values of ALL-FRAMES have special meanings:
- t means consider all windows on all existing frames.
- `visible' means consider all windows on all visible frames on
the current terminal.
- 0 (the number zero) means consider all windows on all visible
and iconified frames on the current terminal.
- A frame means consider all windows on that frame only.
Anything else means consider all windows on the selected frame
and no others."
(let ((buffer (window-normalize-buffer buffer-or-name))
windows)
(dolist (window (window-list-1 (selected-window) minibuf all-frames))
(when (eq (window-buffer window) buffer)
(setq windows (cons window windows))))
(nreverse windows)))
(defun minibuffer-window-active-p (window)
"Return t if WINDOW is the currently active minibuffer window."
(eq window (active-minibuffer-window)))
(defun count-windows (&optional minibuf)
"Return the number of live windows on the selected frame.
The optional argument MINIBUF specifies whether the minibuffer
window shall be counted. See `walk-windows' for the precise
meaning of this argument."
(length (window-list-1 nil minibuf)))
;;; Resizing windows.
(defun window--size-to-pixel (window size &optional horizontal pixelwise round-maybe)
"For WINDOW convert SIZE lines to pixels.
SIZE is supposed to specify a height of WINDOW in terms of text
lines. The return value is the number of pixels specifying that
height.
WINDOW must be a valid window. Optional argument HORIZONTAL
non-nil means convert SIZE columns to pixels.
Optional argument PIXELWISE non-nil means SIZE already specifies
pixels but may have to be adjusted to a multiple of the character
size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
means round to the nearest multiple of the character size of
WINDOW's frame if the option `window-resize-pixelwise' is nil."
(setq window (window-normalize-window window))
(let ((char-size (frame-char-size window horizontal)))
(if pixelwise
(if (and round-maybe (not window-resize-pixelwise))
(* (round size char-size) char-size)
size)
(* size char-size))))
(defun window--pixel-to-total-1 (window horizontal char-size)
"Subroutine of `window--pixel-to-total'."
(let ((child (window-child window)))
(if (window-combination-p window horizontal)
;; In an iso-combination distribute sizes proportionally.
(let ((remainder (window-new-total window))
size best-child rem best-rem)
;; Initialize total sizes to each child's floor.
(while child
(setq size (max (/ (window-size child horizontal t) char-size) 1))
(set-window-new-total child size)
(setq remainder (- remainder size))
(setq child (window-next-sibling child)))
;; Distribute remainder.
(while (> remainder 0)
(setq child (window-last-child window))
(setq best-child nil)
(setq best-rem 0)
(while child
(when (and (<= (window-new-total child)
(/ (window-size child horizontal t) char-size))
(> (setq rem (% (window-size child horizontal t)
char-size))
best-rem))
(setq best-child child)
(setq best-rem rem))
(setq child (window-prev-sibling child)))
;; We MUST have a best-child here.
(set-window-new-total best-child 1 t)
(setq remainder (1- remainder)))
;; Recurse.
(setq child (window-child window))
(while child
(window--pixel-to-total-1 child horizontal char-size)
(setq child (window-next-sibling child))))
;; In an ortho-combination assign new sizes directly.
(let ((size (window-new-total window)))
(while child
(set-window-new-total child size)
(window--pixel-to-total-1 child horizontal char-size)
(setq child (window-next-sibling child)))))))
(defun window--pixel-to-total (&optional frame horizontal)
"On FRAME assign new total window heights from pixel heights.
FRAME must be a live frame and defaults to the selected frame.
Optional argument HORIZONTAL non-nil means assign new total
window widths from pixel widths."
(setq frame (window-normalize-frame frame))
(let* ((char-size (frame-char-size frame horizontal))
(root (frame-root-window frame))
(root-size (window-size root horizontal t))
;; We have to care about the minibuffer window only if it
;; appears together with the root window on this frame.
(mini (let ((mini (minibuffer-window frame)))
(and (eq (window-frame mini) frame)
(not (eq mini root)) mini)))
(mini-size (and mini (window-size mini horizontal t))))
;; We round the line/column sizes of windows here to the nearest
;; integer. In some cases this can make windows appear _larger_
;; than the containing frame (line/column-wise) because the latter's
;; sizes are not (yet) rounded. We might eventually fix that.
(if (and mini (not horizontal))
(let (lines)
(set-window-new-total root (max (/ root-size char-size) 1))
(set-window-new-total mini (max (/ mini-size char-size) 1))
(setq lines (- (round (+ root-size mini-size) char-size)
(+ (window-new-total root) (window-new-total mini))))
(while (> lines 0)
(if (>= (% root-size (window-new-total root))
(% mini-size (window-new-total mini)))
(set-window-new-total root 1 t)
(set-window-new-total mini 1 t))
(setq lines (1- lines))))
(set-window-new-total root (round root-size char-size))
(when mini
;; This is taken in the horizontal case only.
(set-window-new-total mini (round mini-size char-size))))
(unless (window-buffer root)
(window--pixel-to-total-1 root horizontal char-size))
;; Apply the new sizes.
(window-resize-apply-total frame horizontal)))
(defun window--resize-reset (&optional frame horizontal)
"Reset resize values for all windows on FRAME.
FRAME defaults to the selected frame.
This function stores the current value of `window-size' applied
with argument HORIZONTAL in the new total size of all windows on
FRAME. It also resets the new normal size of each of these
windows."
(window--resize-reset-1
(frame-root-window (window-normalize-frame frame)) horizontal))
(defun window--resize-reset-1 (window horizontal)
"Internal function of `window--resize-reset'."
;; Register old size in the new total size.
(set-window-new-pixel window (window-size window horizontal t))
(set-window-new-total window (window-size window horizontal))
;; Reset new normal size.
(set-window-new-normal window)
(when (window-child window)
(window--resize-reset-1 (window-child window) horizontal))
(when (window-right window)
(window--resize-reset-1 (window-right window) horizontal)))
;; The following routine is used to manually resize the minibuffer
;; window and is currently used, for example, by ispell.el.
(defun window--resize-mini-window (window delta)
"Resize minibuffer window WINDOW by DELTA pixels.
If WINDOW cannot be resized by DELTA pixels make it as large (or
as small) as possible, but don't signal an error."
(when (window-minibuffer-p window)
(let* ((frame (window-frame window))
(root (frame-root-window frame))
(height (window-pixel-height window))
(min-delta
(- (window-pixel-height root)
(window-min-size root nil nil t))))
;; Sanitize DELTA.
(cond
((<= (+ height delta) 0)
(setq delta (- (frame-char-height (window-frame window)) height)))
((> delta min-delta)
(setq delta min-delta)))
(unless (zerop delta)
;; Resize now.
(window--resize-reset frame)
;; Ideally we should be able to resize just the last child of root
;; here. See the comment in `resize-root-window-vertically' for
;; why we do not do that.
(window--resize-this-window root (- delta) nil nil t)
(set-window-new-pixel window (+ height delta))
;; The following routine catches the case where we want to resize
;; a minibuffer-only frame.
(when (resize-mini-window-internal window)
(window--pixel-to-total frame)
(run-window-configuration-change-hook frame))))))
(defun window--resize-apply-p (frame &optional horizontal)
"Return t when a window on FRAME shall be resized vertically.
Optional argument HORIZONTAL non-nil means return t when a window
shall be resized horizontally."
(catch 'apply
(walk-window-tree
(lambda (window)
(unless (= (window-new-pixel window)
(window-size window horizontal t))
(throw 'apply t)))
frame t)
nil))
(defun window-resize (window delta &optional horizontal ignore pixelwise)
"Resize WINDOW vertically by DELTA lines.
WINDOW can be an arbitrary window and defaults to the selected
one. An attempt to resize the root window of a frame will raise
an error though.
DELTA a positive number means WINDOW shall be enlarged by DELTA
lines. DELTA negative means WINDOW shall be shrunk by -DELTA
lines.
Optional argument HORIZONTAL non-nil means resize WINDOW
horizontally by DELTA columns. In this case a positive DELTA
means enlarge WINDOW by DELTA columns. DELTA negative means
WINDOW shall be shrunk by -DELTA columns.
Optional argument IGNORE non-nil means ignore restrictions
imposed by fixed size windows, `window-min-height' or
`window-min-width' settings. If IGNORE is a window, ignore
restrictions for that window only. If IGNORE equals `safe',
live windows may get as small as `window-safe-min-height' lines
and `window-safe-min-width' columns. Any other non-nil value
means ignore all of the above restrictions for all windows.
Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
pixels.
This function resizes other windows proportionally and never
deletes any windows. If you want to move only the low (right)
edge of WINDOW consider using `adjust-window-trailing-edge'
instead."
(setq window (window-normalize-window window))
(let* ((frame (window-frame window))
(minibuffer-window (minibuffer-window frame))
sibling)
(setq delta (window--size-to-pixel
window delta horizontal pixelwise t))
(cond
((eq window (frame-root-window frame))
(error "Cannot resize the root window of a frame"))
((window-minibuffer-p window)
(if horizontal
(error "Cannot resize minibuffer window horizontally")
(window--resize-mini-window window delta)))
((and (not horizontal)
(window-full-height-p window)
(eq (window-frame minibuffer-window) frame)
(or (not resize-mini-windows)
(eq minibuffer-window (active-minibuffer-window))))
;; If WINDOW is full height and either `resize-mini-windows' is
;; nil or the minibuffer window is active, resize the minibuffer
;; window.
(window--resize-mini-window minibuffer-window (- delta)))
((window--resizable-p
window delta horizontal ignore nil nil nil t)
(window--resize-reset frame horizontal)
(window--resize-this-window window delta horizontal ignore t)
(if (and (not window-combination-resize)
(window-combined-p window horizontal)
(setq sibling (or (window-right window) (window-left window)))
(window-sizable-p
sibling (- delta) horizontal ignore t))
;; If window-combination-resize is nil, WINDOW is part of an
;; iso-combination, and WINDOW's neighboring right or left
;; sibling can be resized as requested, resize that sibling.
(let ((normal-delta
(/ (float delta)
(window-size (window-parent window) horizontal t))))
(window--resize-this-window sibling (- delta) horizontal nil t)
(set-window-new-normal
window (+ (window-normal-size window horizontal)
normal-delta))
(set-window-new-normal
sibling (- (window-normal-size sibling horizontal)
normal-delta)))
;; Otherwise, resize all other windows in the same combination.
(window--resize-siblings window delta horizontal ignore))
(when (window--resize-apply-p frame horizontal)
(if (window-resize-apply frame horizontal)
(progn
(window--pixel-to-total frame horizontal)
(run-window-configuration-change-hook frame))
(error "Failed to apply resizing %s" window))))
(t
(error "Cannot resize window %s" window)))))
(defun window-resize-no-error (window delta &optional horizontal ignore pixelwise)
"Resize WINDOW vertically if it is resizable by DELTA lines.
This function is like `window-resize' but does not signal an
error when WINDOW cannot be resized. For the meaning of the
optional arguments see the documentation of `window-resize'.
Optional argument PIXELWISE non-nil means interpret DELTA as
pixels."
(when (window--resizable-p
window delta horizontal ignore nil nil nil pixelwise)
(window-resize window delta horizontal ignore pixelwise)))
(defun window--resize-child-windows-skip-p (window)
"Return non-nil if WINDOW shall be skipped by resizing routines."
(memq (window-new-normal window) '(ignore stuck skip)))
(defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
"Recursively set new normal height of child windows of window PARENT.
HORIZONTAL non-nil means set the new normal width of these
windows. WINDOW specifies a child window of PARENT that has been
resized by THIS-DELTA lines (columns).
Optional argument TRAIL either `before' or `after' means set values
only for windows before or after WINDOW. Optional argument
OTHER-DELTA, a number, specifies that this many lines (columns)
have been obtained from (or returned to) an ancestor window of
PARENT in order to resize WINDOW."
(let* ((delta-normal
(if (and (= (- this-delta)
(window-size window horizontal t))
(zerop other-delta))
;; When WINDOW gets deleted and we can return its entire
;; space to its siblings, use WINDOW's normal size as the
;; normal delta.
(- (window-normal-size window horizontal))
;; In any other case calculate the normal delta from the
;; relation of THIS-DELTA to the total size of PARENT.
(/ (float this-delta)
(window-size parent horizontal t))))
(sub (window-child parent))
(parent-normal 0.0)
(skip (eq trail 'after)))
;; Set parent-normal to the sum of the normal sizes of all child
;; windows of PARENT that shall be resized, excluding only WINDOW
;; and any windows specified by the optional TRAIL argument.
(while sub
(cond
((eq sub window)
(setq skip (eq trail 'before)))
(skip)
(t
(setq parent-normal
(+ parent-normal (window-normal-size sub horizontal)))))
(setq sub (window-right sub)))
;; Set the new normal size of all child windows of PARENT from what
;; they should have contributed for recovering THIS-DELTA lines
;; (columns).
(setq sub (window-child parent))
(setq skip (eq trail 'after))
(while sub
(cond
((eq sub window)
(setq skip (eq trail 'before)))
(skip)
(t
(let ((old-normal (window-normal-size sub horizontal)))
(set-window-new-normal
sub (min 1.0 ; Don't get larger than 1.
(max (- old-normal
(* (/ old-normal parent-normal)
delta-normal))
;; Don't drop below 0.
0.0))))))
(setq sub (window-right sub)))
(when (numberp other-delta)
;; Set the new normal size of windows from what they should have
;; contributed for recovering OTHER-DELTA lines (columns).
(setq delta-normal (/ (float (window-size parent horizontal t))
(+ (window-size parent horizontal t)
other-delta)))
(setq sub (window-child parent))
(setq skip (eq trail 'after))
(while sub
(cond
((eq sub window)
(setq skip (eq trail 'before)))
(skip)
(t
(set-window-new-normal
sub (min 1.0 ; Don't get larger than 1.
(max (* (window-new-normal sub) delta-normal)
;; Don't drop below 0.
0.0)))))
(setq sub (window-right sub))))
;; Set the new normal size of WINDOW to what is left by the sum of
;; the normal sizes of its siblings.
(set-window-new-normal
window
(let ((sum 0))
(setq sub (window-child parent))
(while sub
(cond
((eq sub window))
((not (numberp (window-new-normal sub)))
(setq sum (+ sum (window-normal-size sub horizontal))))
(t
(setq sum (+ sum (window-new-normal sub)))))
(setq sub (window-right sub)))
;; Don't get larger than 1 or smaller than 0.
(min 1.0 (max (- 1.0 sum) 0.0))))))
(defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge char-size)
"Resize child windows of window PARENT vertically by DELTA pixels.
PARENT must be a vertically combined internal window.
Optional argument HORIZONTAL non-nil means resize child windows
of PARENT horizontally by DELTA pixels. In this case PARENT must
be a horizontally combined internal window.
WINDOW, if specified, must denote a child window of PARENT that
is resized by DELTA pixels.
Optional argument IGNORE non-nil means ignore restrictions
imposed by fixed size windows, `window-min-height' or
`window-min-width' settings. If IGNORE equals `safe', live
windows may get as small as `window-safe-min-height' lines and
`window-safe-min-width' columns. If IGNORE is a window, ignore
restrictions for that window only. Any other non-nil value means
ignore all of the above restrictions for all windows.
Optional arguments TRAIL and EDGE, when non-nil, restrict the set
of windows that shall be resized. If TRAIL equals `before',
resize only windows on the left or above EDGE. If TRAIL equals
`after', resize only windows on the right or below EDGE. Also,
preferably only resize windows adjacent to EDGE.
If the optional argument CHAR-SIZE is a positive integer, it specifies
the number of pixels by which windows are incrementally resized.
If CHAR-SIZE is nil, this means to use the value of
`frame-char-height' or `frame-char-width' of WINDOW's frame.
Return the symbol `normalized' if new normal sizes have been
already set by this routine."
(let* ((first (window-child parent))
(last (window-last-child parent))
(parent-total (+ (window-size parent horizontal t)
delta))
(char-size (or char-size
(and window-resize-pixelwise 1)
(frame-char-size window horizontal)))
sub best-window best-value best-delta)
(if (and edge (memq trail '(before after))
(progn
(setq sub first)
(while (and (window-right sub)
(or (and (eq trail 'before)
(not (window--resize-child-windows-skip-p
(window-right sub))))
(and (eq trail 'after)
(window--resize-child-windows-skip-p sub))))
(setq sub (window-right sub)))
sub)
(if horizontal
(if (eq trail 'before)
(= (+ (window-pixel-left sub) (window-pixel-width sub))
edge)
(= (window-pixel-left sub) edge))
(if (eq trail 'before)
(= (+ (window-pixel-top sub) (window-pixel-height sub))
edge)
(= (window-pixel-top sub) edge)))
(window-sizable-p sub delta horizontal ignore t))
;; Resize only windows adjacent to EDGE.
(progn
(window--resize-this-window
sub delta horizontal ignore t trail edge)
(if (and window (eq (window-parent sub) parent))
(progn
;; Assign new normal sizes.
(set-window-new-normal
sub (/ (float (window-new-pixel sub)) parent-total))
(set-window-new-normal
window (- (window-normal-size window horizontal)
(- (window-new-normal sub)
(window-normal-size sub horizontal)))))
(window--resize-child-windows-normal
parent horizontal sub 0 trail delta))
;; Return 'normalized to notify `window--resize-siblings' that
;; normal sizes have been already set.
'normalized)
;; Resize all windows proportionally.
(setq sub last)
(while sub
(cond
((or (window--resize-child-windows-skip-p sub)
;; Ignore windows to skip and fixed-size child windows -
;; in the latter case make it a window to skip.
(and (not ignore)
(window-size-fixed-p sub horizontal)
(set-window-new-normal sub 'ignore))))
((< delta 0)
;; When shrinking store the number of lines/cols we can get
;; from this window here together with the total/normal size
;; factor.
(set-window-new-normal
sub
(cons
;; We used to call this with NODOWN t, "fixed" 2011-05-11.
(window-min-delta sub horizontal ignore trail t nil t)
(- (/ (float (window-size sub horizontal t))
parent-total)
(window-normal-size sub horizontal)))))
((> delta 0)
;; When enlarging store the total/normal size factor only
(set-window-new-normal
sub
(- (/ (float (window-size sub horizontal t))
parent-total)
(window-normal-size sub horizontal)))))
(setq sub (window-left sub)))
(cond
((< delta 0)
;; Shrink windows by delta.
(setq best-window t)
(while (and best-window (not (zerop delta)))
(setq sub last)
(setq best-window nil)
(setq best-value most-negative-fixnum)
(while sub
(when (and (consp (window-new-normal sub))
(not (<= (car (window-new-normal sub)) 0))
(> (cdr (window-new-normal sub)) best-value))
(setq best-window sub)
(setq best-value (cdr (window-new-normal sub))))
(setq sub (window-left sub)))
(when best-window
(setq best-delta (min (car (window-new-normal best-window))
char-size (- delta)))
(setq delta (+ delta best-delta))
(set-window-new-pixel best-window (- best-delta) t)
(set-window-new-normal
best-window
(if (= (car (window-new-normal best-window)) best-delta)
'skip ; We can't shrink best-window any further.
(cons (- (car (window-new-normal best-window)) best-delta)
(- (/ (float (window-new-pixel best-window))
parent-total)
(window-normal-size best-window horizontal))))))))
((> delta 0)
;; Enlarge windows by delta.
(setq best-window t)
(while (and best-window (not (zerop delta)))
(setq sub last)
(setq best-window nil)
(setq best-value most-positive-fixnum)
(while sub
(when (and (numberp (window-new-normal sub))
(< (window-new-normal sub) best-value))
(setq best-window sub)
(setq best-value (window-new-normal sub)))
(setq sub (window-left sub)))
(when best-window
(setq best-delta (min delta char-size))
(setq delta (- delta best-delta))
(set-window-new-pixel best-window best-delta t)
(set-window-new-normal
best-window
(- (/ (float (window-new-pixel best-window))
parent-total)
(window-normal-size best-window horizontal)))))))
(when best-window
(setq sub last)
(while sub
(when (or (consp (window-new-normal sub))
(numberp (window-new-normal sub)))
;; Reset new normal size fields so `window-resize-apply'
;; won't use them to apply new sizes.
(set-window-new-normal sub))
(unless (eq (window-new-normal sub) 'ignore)
;; Resize this window's child windows (back-engineering
;; delta from sub's old and new total sizes).
(let ((delta (- (window-new-pixel sub)
(window-size sub horizontal t))))
(unless (and (zerop delta) (not trail))
;; For the TRAIL non-nil case we have to resize SUB
;; recursively even if it's size does not change.
(window--resize-this-window
sub delta horizontal ignore nil trail edge))))
(setq sub (window-left sub)))))))
(defun window--resize-siblings (window delta &optional horizontal ignore trail edge char-size)
"Resize other windows when WINDOW is resized vertically by DELTA pixels.
Optional argument HORIZONTAL non-nil means resize other windows
when WINDOW is resized horizontally by DELTA pixels. WINDOW
itself is not resized by this function.
Optional argument IGNORE non-nil means ignore restrictions
imposed by fixed size windows, `window-min-height' or
`window-min-width' settings. If IGNORE equals `safe', live
windows may get as small as `window-safe-min-height' lines and
`window-safe-min-width' columns. If IGNORE is a window, ignore
restrictions for that window only. Any other non-nil value means
ignore all of the above restrictions for all windows.
Optional arguments TRAIL and EDGE, when non-nil, refine the set
of windows that shall be resized. If TRAIL equals `before',
resize only windows on the left or above EDGE. If TRAIL equals
`after', resize only windows on the right or below EDGE. Also,
preferably only resize windows adjacent to EDGE."
(when (window-parent window)
(let* ((parent (window-parent window))
(sub (window-child parent)))
(if (window-combined-p sub horizontal)
;; In an iso-combination try to extract DELTA from WINDOW's
;; siblings.
(let ((skip (eq trail 'after))
this-delta other-delta)
;; Decide which windows shall be left alone.
(while sub
(cond
((eq sub window)
;; Make sure WINDOW is left alone when
;; resizing its siblings.
(set-window-new-normal sub 'ignore)
(setq skip (eq trail 'before)))
(skip
;; Make sure this sibling is left alone when
;; resizing its siblings.
(set-window-new-normal sub 'ignore))
((or (window--size-ignore-p sub ignore)
(not (window-size-fixed-p sub horizontal)))
;; Set this-delta to t to signal that we found a sibling
;; of WINDOW whose size is not fixed.
(setq this-delta t)))
(setq sub (window-right sub)))
;; Set this-delta to what we can get from WINDOW's siblings.
(if (= (- delta) (window-size window horizontal t))
;; A deletion, presumably. We must handle this case
;; specially since `window--resizable' can't be used.
(if this-delta
;; There's at least one resizable sibling we can
;; give WINDOW's size to.
(setq this-delta delta)
;; No resizable sibling exists.
(setq this-delta 0))
;; Any other form of resizing.
(setq this-delta
(window--resizable
window delta horizontal ignore trail t nil t)))
;; Set other-delta to what we still have to get from
;; ancestor windows of parent.
(setq other-delta (- delta this-delta))
(unless (zerop other-delta)
;; Unless we got everything from WINDOW's siblings, PARENT
;; must be resized by other-delta lines or columns.
(set-window-new-pixel parent other-delta 'add))
(if (zerop this-delta)
;; We haven't got anything from WINDOW's siblings but we
;; must update the normal sizes to respect other-delta.
(window--resize-child-windows-normal
parent horizontal window this-delta trail other-delta)
;; We did get something from WINDOW's siblings which means
;; we have to resize their child windows.
(unless (eq (window--resize-child-windows
parent (- this-delta) horizontal
window ignore trail edge char-size)
;; If `window--resize-child-windows' returns
;; 'normalized, this means it has set the
;; normal sizes already.
'normalized)
;; Set the normal sizes.
(window--resize-child-windows-normal
parent horizontal window this-delta trail other-delta))
;; Set DELTA to what we still have to get from ancestor
;; windows.
(setq delta other-delta)))
;; In an ortho-combination all siblings of WINDOW must be
;; resized by DELTA.
(set-window-new-pixel parent delta 'add)
(while sub
(unless (eq sub window)
(window--resize-this-window
sub delta horizontal ignore t))
(setq sub (window-right sub))))
(unless (zerop delta)
;; "Go up."
(window--resize-siblings
parent delta horizontal ignore trail edge char-size)))))
(defun window--resize-this-window (window delta &optional horizontal ignore add trail edge char-size)
"Resize WINDOW vertically by DELTA pixels.
Optional argument HORIZONTAL non-nil means resize WINDOW
horizontally by DELTA pixels.
Optional argument IGNORE non-nil means ignore restrictions
imposed by fixed size windows, `window-min-height' or
`window-min-width' settings. If IGNORE equals `safe', live
windows may get as small as `window-safe-min-height' lines and
`window-safe-min-width' columns. If IGNORE is a window, ignore
restrictions for that window only. Any other non-nil value
means ignore all of the above restrictions for all windows.
Optional argument ADD non-nil means add DELTA to the new total
size of WINDOW.
Optional arguments TRAIL and EDGE, when non-nil, refine the set
of windows that shall be resized. If TRAIL equals `before',
resize only windows on the left or above EDGE. If TRAIL equals
`after', resize only windows on the right or below EDGE. Also,
preferably only resize windows adjacent to EDGE.
If the optional argument CHAR-SIZE is a positive integer, it specifies
the number of pixels by which windows are incrementally resized.
If CHAR-SIZE is nil, this means to use the value of
`frame-char-height' or `frame-char-width' of WINDOW's frame.
This function recursively resizes WINDOW's child windows to fit the
new size. Make sure that WINDOW is `window--resizable' before
calling this function. Note that this function does not resize
siblings of WINDOW or WINDOW's parent window. You have to
eventually call `window-resize-apply' in order to make resizing
actually take effect."
(when add
;; Add DELTA to the new total size of WINDOW.
(set-window-new-pixel window delta t))
(let ((sub (window-child window)))
(cond
((not sub))
((window-combined-p sub horizontal)
;; In an iso-combination resize child windows according to their
;; normal sizes.
(window--resize-child-windows
window delta horizontal nil ignore trail edge char-size))
;; In an ortho-combination resize each child window by DELTA.
(t
(while sub
(window--resize-this-window
sub delta horizontal ignore t trail edge char-size)
(setq sub (window-right sub)))))))
(defun window--resize-root-window (window delta horizontal ignore pixelwise)
"Resize root window WINDOW vertically by DELTA lines.
HORIZONTAL non-nil means resize root window WINDOW horizontally
by DELTA columns.
IGNORE non-nil means ignore any restrictions imposed by fixed
size windows, `window-min-height' or `window-min-width' settings.
This function is only called by the frame resizing routines. It
resizes windows proportionally and never deletes any windows."
(when (and (windowp window) (numberp delta))
(let ((pixel-delta
(if pixelwise
delta
(window--size-to-pixel window delta horizontal))))
(when (window-sizable-p window pixel-delta horizontal ignore t)
(window--resize-reset (window-frame window) horizontal)
(window--resize-this-window
window pixel-delta horizontal ignore t)))))
(defun window--resize-root-window-vertically (window delta pixelwise)
"Resize root window WINDOW vertically by DELTA lines.
If DELTA is less than zero and we can't shrink WINDOW by DELTA
lines, shrink it as much as possible. If DELTA is greater than
zero, this function can resize fixed-size windows in order to
recover the necessary lines. Return the number of lines that
were recovered.
Third argument PIXELWISE non-nil means to interpret DELTA as
pixels and return the number of pixels that were recovered.
This function is called by the minibuffer window resizing
routines."
(let* ((frame (window-frame window))
(pixel-delta
(cond
(pixelwise
delta)
((numberp delta)
(* (frame-char-height frame) delta))
(t 0)))
ignore)
(cond
((zerop pixel-delta))
((< pixel-delta 0)
(setq pixel-delta (window-sizable window pixel-delta nil nil pixelwise))
(window--resize-reset frame)
;; When shrinking the root window, emulate an edge drag in order
;; to not resize other windows if we can avoid it (Bug#12419).
(window--resize-this-window
window pixel-delta nil ignore t 'before
(+ (window-pixel-top window) (window-pixel-height window)))
;; Don't record new normal sizes to make sure that shrinking back
;; proportionally works as intended.
(walk-window-tree
(lambda (window) (set-window-new-normal window 'ignore)) frame t))
((> pixel-delta 0)
(window--resize-reset frame)
(unless (window-sizable window pixel-delta nil nil pixelwise)
(setq ignore t))
;; When growing the root window, resize proportionally. This
;; should give windows back their original sizes (hopefully).
(window--resize-this-window
window pixel-delta nil ignore t)))
;; Return the possibly adjusted DELTA.
(if pixelwise
pixel-delta
(/ pixel-delta (frame-char-height frame)))))
(defun adjust-window-trailing-edge (window delta &optional horizontal pixelwise)
"Move WINDOW's bottom edge by DELTA lines.
Optional argument HORIZONTAL non-nil means move WINDOW's right
edge by DELTA columns. WINDOW must be a valid window and
defaults to the selected one.
Optional argument PIXELWISE non-nil means interpret DELTA as
number of pixels.
If DELTA is greater than zero, move the edge downwards or to the
right. If DELTA is less than zero, move the edge upwards or to
the left. If the edge can't be moved by DELTA lines or columns,
move it as far as possible in the desired direction."
(setq window (window-normalize-window window))
(let* ((frame (window-frame window))
(minibuffer-window (minibuffer-window frame))
(right window)
left this-delta min-delta max-delta)
(unless pixelwise
(setq pixelwise t)
(setq delta (* delta (frame-char-size window horizontal))))
;; Find the edge we want to move.
(while (and (or (not (window-combined-p right horizontal))
(not (window-right right)))
(setq right (window-parent right))))
(cond
((and (not right) (not horizontal)
;; Resize the minibuffer window if it's on the same frame as
;; and immediately below WINDOW and it's either active or
;; `resize-mini-windows' is nil.
(eq (window-frame minibuffer-window) frame)
(= (nth 1 (window-pixel-edges minibuffer-window))
(nth 3 (window-pixel-edges window)))
(or (not resize-mini-windows)
(eq minibuffer-window (active-minibuffer-window))))
(window--resize-mini-window minibuffer-window (- delta)))
((or (not (setq left right)) (not (setq right (window-right right))))
(if horizontal
(error "No window on the right of this one")
(error "No window below this one")))
(t
;; Set LEFT to the first resizable window on the left. This step is
;; needed to handle fixed-size windows.
(while (and left (window-size-fixed-p left horizontal))
(setq left
(or (window-left left)
(progn
(while (and (setq left (window-parent left))
(not (window-combined-p left horizontal))))
(window-left left)))))
(unless left
(if horizontal
(error "No resizable window on the left of this one")
(error "No resizable window above this one")))
;; Set RIGHT to the first resizable window on the right. This step
;; is needed to handle fixed-size windows.
(while (and right (window-size-fixed-p right horizontal))
(setq right
(or (window-right right)
(progn
(while (and (setq right (window-parent right))
(not (window-combined-p right horizontal))))
(window-right right)))))
(unless right
(if horizontal
(error "No resizable window on the right of this one")
(error "No resizable window below this one")))
;; LEFT and RIGHT (which might be both internal windows) are now the
;; two windows we want to resize.
(cond
((> delta 0)
(setq max-delta
(window--max-delta-1
left 0 horizontal nil 'after nil pixelwise))
(setq min-delta
(window--min-delta-1
right (- delta) horizontal nil 'before nil pixelwise))
(when (or (< max-delta delta) (> min-delta (- delta)))
;; We can't get the whole DELTA - move as far as possible.
(setq delta (min max-delta (- min-delta))))
(unless (zerop delta)
;; Start resizing.
(window--resize-reset frame horizontal)
;; Try to enlarge LEFT first.
(setq this-delta (window--resizable
left delta horizontal nil 'after nil nil pixelwise))
(unless (zerop this-delta)
(window--resize-this-window
left this-delta horizontal nil t 'before
(if horizontal
(+ (window-pixel-left left) (window-pixel-width left))
(+ (window-pixel-top left) (window-pixel-height left)))))
;; Shrink windows on right of LEFT.
(window--resize-siblings
left delta horizontal nil 'after
(if horizontal
(window-pixel-left right)
(window-pixel-top right)))))
((< delta 0)
(setq max-delta
(window--max-delta-1
right 0 horizontal nil 'before nil pixelwise))
(setq min-delta
(window--min-delta-1
left delta horizontal nil 'after nil pixelwise))
(when (or (< max-delta (- delta)) (> min-delta delta))
;; We can't get the whole DELTA - move as far as possible.
(setq delta (max (- max-delta) min-delta)))
(unless (zerop delta)
;; Start resizing.
(window--resize-reset frame horizontal)
;; Try to enlarge RIGHT.
(setq this-delta
(window--resizable
right (- delta) horizontal nil 'before nil nil pixelwise))
(unless (zerop this-delta)
(window--resize-this-window
right this-delta horizontal nil t 'after
(if horizontal
(window-pixel-left right)
(window-pixel-top right))))
;; Shrink windows on left of RIGHT.
(window--resize-siblings
right (- delta) horizontal nil 'before
(if horizontal
(+ (window-pixel-left left) (window-pixel-width left))
(+ (window-pixel-top left) (window-pixel-height left)))))))
(unless (zerop delta)
;; Don't report an error in the standard case.
(when (window--resize-apply-p frame horizontal)
(if (window-resize-apply frame horizontal)
(progn
(window--pixel-to-total frame horizontal)
(run-window-configuration-change-hook frame))
;; But do report an error if applying the changes fails.
(error "Failed adjusting window %s" window))))))))
(defun enlarge-window (delta &optional horizontal)
"Make the selected window DELTA lines taller.
Interactively, if no argument is given, make the selected window
one line taller. If optional argument HORIZONTAL is non-nil,
make selected window wider by DELTA columns. If DELTA is
negative, shrink selected window by -DELTA lines or columns."
(interactive "p")
(let ((minibuffer-window (minibuffer-window)))
(cond
((zerop delta))
((window-size-fixed-p nil horizontal)
(error "Selected window has fixed size"))
((window-minibuffer-p)
(if horizontal
(error "Cannot resize minibuffer window horizontally")
(window--resize-mini-window (selected-window) delta)))
((and (not horizontal)
(window-full-height-p)
(eq (window-frame minibuffer-window) (selected-frame))
(not resize-mini-windows))
;; If the selected window is full height and `resize-mini-windows'
;; is nil, resize the minibuffer window.
(window--resize-mini-window minibuffer-window (- delta)))
((window--resizable-p nil delta horizontal)
(window-resize nil delta horizontal))
(t
(window-resize
nil (if (> delta 0)
(window-max-delta nil horizontal)
(- (window-min-delta nil horizontal)))
horizontal)))))
(defun shrink-window (delta &optional horizontal)
"Make the selected window DELTA lines smaller.
Interactively, if no argument is given, make the selected window
one line smaller. If optional argument HORIZONTAL is non-nil,
make selected window narrower by DELTA columns. If DELTA is
negative, enlarge selected window by -DELTA lines or columns.
Also see the `window-min-height' variable."
(interactive "p")
(let ((minibuffer-window (minibuffer-window)))
(cond
((zerop delta))
((window-size-fixed-p nil horizontal)
(error "Selected window has fixed size"))
((window-minibuffer-p)
(if horizontal
(error "Cannot resize minibuffer window horizontally")
(window--resize-mini-window (selected-window) (- delta))))
((and (not horizontal)
(window-full-height-p)
(eq (window-frame minibuffer-window) (selected-frame))
(not resize-mini-windows))
;; If the selected window is full height and `resize-mini-windows'
;; is nil, resize the minibuffer window.
(window--resize-mini-window minibuffer-window delta))
((window--resizable-p nil (- delta) horizontal)
(window-resize nil (- delta) horizontal))
(t
(window-resize
nil (if (> delta 0)
(- (window-min-delta nil horizontal))
(window-max-delta nil horizontal))
horizontal)))))
(defun maximize-window (&optional window)
"Maximize WINDOW.
Make WINDOW as large as possible without deleting any windows.
WINDOW must be a valid window and defaults to the selected one.
If the option `window-resize-pixelwise' is non-nil maximize
WINDOW pixelwise."
(interactive)
(setq window (window-normalize-window window))
(window-resize
window (window-max-delta window nil nil nil nil nil window-resize-pixelwise)
nil nil window-resize-pixelwise)
(window-resize
window (window-max-delta window t nil nil nil nil window-resize-pixelwise)
t nil window-resize-pixelwise))
(defun minimize-window (&optional window)
"Minimize WINDOW.
Make WINDOW as small as possible without deleting any windows.
WINDOW must be a valid window and defaults to the selected one.
If the option `window-resize-pixelwise' is non-nil minimize
WINDOW pixelwise."
(interactive)
(setq window (window-normalize-window window))
(window-resize
window
(- (window-min-delta window nil nil nil nil nil window-resize-pixelwise))
nil nil window-resize-pixelwise)
(window-resize
window
(- (window-min-delta window t nil nil nil nil window-resize-pixelwise))
t nil window-resize-pixelwise))
(defun frame-root-window-p (window)
"Return non-nil if WINDOW is the root window of its frame."
(eq window (frame-root-window window)))
(defun window--subtree (window &optional next)
"Return window subtree rooted at WINDOW.
Optional argument NEXT non-nil means include WINDOW's right
siblings in the return value.
See the documentation of `window-tree' for a description of the
return value."
(let (list)
(while window
(setq list
(cons
(cond
((window-top-child window)
(cons t (cons (window-edges window)
(window--subtree (window-top-child window) t))))
((window-left-child window)
(cons nil (cons (window-edges window)
(window--subtree (window-left-child window) t))))
(t window))
list))
(setq window (when next (window-next-sibling window))))
(nreverse list)))
(defun window-tree (&optional frame)
"Return the window tree of frame FRAME.
FRAME must be a live frame and defaults to the selected frame.
The return value is a list of the form (ROOT MINI), where ROOT
represents the window tree of the frame's root window, and MINI
is the frame's minibuffer window.
If the root window is not split, ROOT is the root window itself.
Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
for a horizontal split, and t for a vertical split. EDGES gives
the combined size and position of the child windows in the split,
and the rest of the elements are the child windows in the split.
Each of the child windows may again be a window or a list
representing a window split, and so on. EDGES is a list (LEFT
TOP RIGHT BOTTOM) as returned by `window-edges'."
(setq frame (window-normalize-frame frame))
(window--subtree (frame-root-window frame) t))
(defun other-window (count &optional all-frames)
"Select another window in cyclic ordering of windows.
COUNT specifies the number of windows to skip, starting with the
selected window, before making the selection. If COUNT is
positive, skip COUNT windows forwards. If COUNT is negative,
skip -COUNT windows backwards. COUNT zero means do not skip any
window, so select the selected window. In an interactive call,
COUNT is the numeric prefix argument. Return nil.
If the `other-window' parameter of the selected window is a
function and `ignore-window-parameters' is nil, call that
function with the arguments COUNT and ALL-FRAMES.
This function does not select a window whose `no-other-window'
window parameter is non-nil.
This function uses `next-window' for finding the window to
select. The argument ALL-FRAMES has the same meaning as in
`next-window', but the MINIBUF argument of `next-window' is
always effectively nil."
(interactive "p")
(let* ((window (selected-window))
(function (and (not ignore-window-parameters)
(window-parameter window 'other-window)))
old-window old-count)
(if (functionp function)
(funcall function count all-frames)
;; `next-window' and `previous-window' may return a window we are
;; not allowed to select. Hence we need an exit strategy in case
;; all windows are non-selectable.
(catch 'exit
(while (> count 0)
(setq window (next-window window nil all-frames))
(cond
((eq window old-window)
(when (= count old-count)
;; Keep out of infinite loops. When COUNT has not changed
;; since we last looked at `window' we're probably in one.
(throw 'exit nil)))
((window-parameter window 'no-other-window)
(unless old-window
;; The first non-selectable window `next-window' got us:
;; Remember it and the current value of COUNT.
(setq old-window window)
(setq old-count count)))
(t
(setq count (1- count)))))
(while (< count 0)
(setq window (previous-window window nil all-frames))
(cond
((eq window old-window)
(when (= count old-count)
;; Keep out of infinite loops. When COUNT has not changed
;; since we last looked at `window' we're probably in one.
(throw 'exit nil)))
((window-parameter window 'no-other-window)
(unless old-window
;; The first non-selectable window `previous-window' got
;; us: Remember it and the current value of COUNT.
(setq old-window window)
(setq old-count count)))
(t
(setq count (1+ count)))))
(select-window window)
;; Always return nil.
nil))))
;; This should probably return non-nil when the selected window is part
;; of an atomic window whose root is the frame's root window.
(defun one-window-p (&optional nomini all-frames)
"Return non-nil if the selected window is the only window.
Optional arg NOMINI non-nil means don't count the minibuffer
even if it is active. Otherwise, the minibuffer is counted
when it is active.
Optional argument ALL-FRAMES specifies the set of frames to
consider, see also `next-window'. ALL-FRAMES nil or omitted
means consider windows on the selected frame only, plus the
minibuffer window if specified by the NOMINI argument. If the
minibuffer counts, consider all windows on all frames that share
that minibuffer too. The remaining non-nil values of ALL-FRAMES
with a special meaning are:
- t means consider all windows on all existing frames.
- `visible' means consider all windows on all visible frames on
the current terminal.
- 0 (the number zero) means consider all windows on all visible
and iconified frames on the current terminal.
- A frame means consider all windows on that frame only.
Anything else means consider all windows on the selected frame
and no others."
(let ((base-window (selected-window)))
(if (and nomini (eq base-window (minibuffer-window)))
(setq base-window (next-window base-window)))
(eq base-window
(next-window base-window (if nomini 'arg) all-frames))))
;;; Deleting windows.
(defun window-deletable-p (&optional window)
"Return t if WINDOW can be safely deleted from its frame.
WINDOW must be a valid window and defaults to the selected one.
Return 'frame if deleting WINDOW should also delete its frame."
(setq window (window-normalize-window window))
(unless (or ignore-window-parameters
(eq (window-parameter window 'delete-window) t))
;; Handle atomicity.
(when (window-parameter window 'window-atom)
(setq window (window-atom-root window))))
(let ((frame (window-frame window)))
(cond
((frame-root-window-p window)
;; WINDOW's frame can be deleted only if there are other frames
;; on the same terminal, and it does not contain the active
;; minibuffer.
(unless (or (eq frame (next-frame frame 0))
;; We can delete our frame only if no other frame
;; currently uses our minibuffer window.
(catch 'other
(dolist (other (frame-list))
(when (and (not (eq other frame))
(eq (window-frame (minibuffer-window other))
frame))
(throw 'other t))))
(let ((minibuf (active-minibuffer-window)))
(and minibuf (eq frame (window-frame minibuf)))))
'frame))
((or ignore-window-parameters
(not (eq window (window--major-non-side-window frame))))
;; WINDOW can be deleted unless it is the major non-side window of
;; its frame.
t))))
(defun window--in-subtree-p (window root)
"Return t if WINDOW is either ROOT or a member of ROOT's subtree."
(or (eq window root)
(let ((parent (window-parent window)))
(catch 'done
(while parent
(if (eq parent root)
(throw 'done t)
(setq parent (window-parent parent))))))))
(defun delete-window (&optional window)
"Delete 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."
(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--major-non-side-window frame))
(error "Attempt to delete last non-side window")))
(let* ((horizontal (window-left-child parent))
(size (window-size window horizontal t))
(frame-selected
(window--in-subtree-p (frame-selected-window frame) window))
;; Emacs 23 preferably gives WINDOW's space to its left
;; sibling.
(sibling (or (window-left window) (window-right window))))
(window--resize-reset frame horizontal)
(cond
((and (not window-combination-resize)
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)))
;; Actually delete WINDOW.
(delete-window-internal window)
(window--pixel-to-total frame horizontal)
(when (and frame-selected
(window-parameter
(frame-selected-window frame) 'no-other-window))
;; `delete-window-internal' has selected a window that should
;; not be selected, fix this here.
(other-window -1 frame))
(run-window-configuration-change-hook frame)
(window--check frame)
;; Always return nil.
nil))))
(defun delete-other-windows (&optional window)
"Make WINDOW fill its frame.
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-other-windows' parameter of WINDOW equals t, do not
process any parameters of WINDOW. Otherwise, if the
`delete-other-windows' 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 this
function with the root of the atomic window as its argument. If
WINDOW is a non-side window, make WINDOW the only non-side window
on the frame. Side windows are not deleted. If WINDOW is a side
window signal an error."
(interactive)
(setq window (window-normalize-window window))
(let* ((frame (window-frame window))
(function (window-parameter window 'delete-other-windows))
(window-side (window-parameter window 'window-side))
atom-root side-main)
(window--check frame)
(catch 'done
(cond
;; Ignore window parameters if `ignore-window-parameters' is t or
;; `delete-other-windows' is t.
((or ignore-window-parameters (eq function t)))
((functionp function)
;; The `delete-other-windows' parameter specifies the function
;; to call. If the function is `ignore' no windows are deleted.
;; It's up to the function called 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-other-windows atom-root))))
((memq window-side window-sides)
(error "Cannot make side window the only window"))
((and (window-minibuffer-p window)
(not (eq window (frame-root-window window))))
(error "Can't expand minibuffer to full frame")))
;; If WINDOW is the major non-side window, do nothing.
(if (window-with-parameter 'window-side)
(setq side-main (window--major-non-side-window frame))
(setq side-main (frame-root-window frame)))
(unless (eq window side-main)
(delete-other-windows-internal window side-main)
(run-window-configuration-change-hook frame)
(window--check frame))
;; Always return nil.
nil)))
(defun delete-other-windows-vertically (&optional window)
"Delete the windows in the same column with WINDOW, but not WINDOW itself.
This may be a useful alternative binding for \\[delete-other-windows]
if you often split windows horizontally."
(interactive)
(let* ((window (or window (selected-window)))
(edges (window-edges window))
(w window) delenda)
(while (not (eq (setq w (next-window w 1)) window))
(let ((e (window-edges w)))
(when (and (= (car e) (car edges))
(= (nth 2 e) (nth 2 edges)))
(push w delenda))))
(mapc 'delete-window delenda)))
;;; Windows and buffers.
;; `prev-buffers' and `next-buffers' are two reserved window slots used
;; for (1) determining which buffer to show in the window when its
;; buffer shall be buried or killed and (2) which buffer to show for
;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
;; `prev-buffers' consists of <buffer, window-start, window-point>
;; triples. The entries on this list are ordered by the time their
;; buffer has been removed from the window, the most recently removed
;; buffer's entry being first. The window-start and window-point
;; components are `window-start' and `window-point' at the time the
;; buffer was removed from the window which implies that the entry must
;; be added when `set-window-buffer' removes the buffer from the window.
;; `next-buffers' is the list of buffers that have been replaced
;; recently by `switch-to-prev-buffer'. These buffers are the least
;; preferred candidates of `switch-to-prev-buffer' and the preferred
;; candidates of `switch-to-next-buffer' to switch to. This list is
;; reset to nil by any action changing the window's buffer with the
;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
;; `switch-to-next-buffer' pops the last pushed buffer from it.
;; Both `prev-buffers' and `next-buffers' may reference killed buffers
;; if such a buffer was killed while the window was hidden within a
;; window configuration. Such killed buffers get removed whenever
;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
;; The following function is called by `set-window-buffer' _before_ it
;; replaces the buffer of the argument window with the new buffer.
(defun record-window-buffer (&optional window)
"Record WINDOW's buffer.
WINDOW must be a live window and defaults to the selected one."
(let* ((window (window-normalize-window window t))
(buffer (window-buffer window))
(entry (assq buffer (window-prev-buffers window))))
;; Reset WINDOW's next buffers. If needed, they are resurrected by
;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
(set-window-next-buffers window nil)
(when entry
;; Remove all entries for BUFFER from WINDOW's previous buffers.
(set-window-prev-buffers
window (assq-delete-all buffer (window-prev-buffers window))))
;; Don't record insignificant buffers.
(unless (eq (aref (buffer-name buffer) 0) ?\s)
;; Add an entry for buffer to WINDOW's previous buffers.
(with-current-buffer buffer
(let ((start (window-start window))
(point (window-point window)))
(setq entry
(cons buffer
(if entry
;; We have an entry, update marker positions.
(list (set-marker (nth 1 entry) start)
(set-marker (nth 2 entry) point))
;; Make new markers.
(list (copy-marker start)
(copy-marker
;; Preserve window-point-insertion-type
;; (Bug#12588).
point window-point-insertion-type)))))
(set-window-prev-buffers
window (cons entry (window-prev-buffers window)))))
(run-hooks 'buffer-list-update-hook))))
(defun unrecord-window-buffer (&optional window buffer)
"Unrecord BUFFER in WINDOW.
WINDOW must be a live window and defaults to the selected one.
BUFFER must be a live buffer and defaults to the buffer of
WINDOW."
(let* ((window (window-normalize-window window t))
(buffer (or buffer (window-buffer window))))
(set-window-prev-buffers
window (assq-delete-all buffer (window-prev-buffers window)))
(set-window-next-buffers
window (delq buffer (window-next-buffers window)))))
(defun set-window-buffer-start-and-point (window buffer &optional start point)
"Set WINDOW's buffer to BUFFER.
WINDOW must be a live window and defaults to the selected one.
Optional argument START non-nil means set WINDOW's start position
to START. Optional argument POINT non-nil means set WINDOW's
point to POINT. If WINDOW is selected this also sets BUFFER's
`point' to POINT. If WINDOW is selected and the buffer it showed
before was current this also makes BUFFER the current buffer."
(setq window (window-normalize-window window t))
(let ((selected (eq window (selected-window)))
(current (eq (window-buffer window) (current-buffer))))
(set-window-buffer window buffer)
(when (and selected current)
(set-buffer buffer))
(when start
;; Don't force window-start here (even if POINT is nil).
(set-window-start window start t))
(when point
(set-window-point window point))))
(defcustom switch-to-visible-buffer t
"If non-nil, allow switching to an already visible buffer.
If this variable is non-nil, `switch-to-prev-buffer' and
`switch-to-next-buffer' may switch to an already visible buffer
provided the buffer was shown before in the window specified as
argument to those functions. If this variable is nil,
`switch-to-prev-buffer' and `switch-to-next-buffer' always try to
avoid switching to a buffer that is already visible in another
window on the same frame."
:type 'boolean
:version "24.1"
:group 'windows)
(defun switch-to-prev-buffer (&optional window bury-or-kill)
"In WINDOW switch to previous buffer.
WINDOW must be a live window and defaults to the selected one.
Return the buffer switched to, nil if no suitable buffer could be
found.
Optional argument BURY-OR-KILL non-nil means the buffer currently
shown in WINDOW is about to be buried or killed and consequently
shall not be switched to in future invocations of this command.
As a special case, if BURY-OR-KILL equals `append', this means to
move the buffer to the end of WINDOW's previous buffers list so a
future invocation of `switch-to-prev-buffer' less likely switches
to it."
(interactive)
(let* ((window (window-normalize-window window t))
(frame (window-frame window))
(old-buffer (window-buffer window))
;; Save this since it's destroyed by `set-window-buffer'.
(next-buffers (window-next-buffers window))
(pred (frame-parameter frame 'buffer-predicate))
entry new-buffer killed-buffers visible)
(when (window-minibuffer-p window)
;; Don't switch in minibuffer window.
(unless (setq window (minibuffer-selected-window))
(error "Window %s is a minibuffer window" window)))
(when (window-dedicated-p window)
;; Don't switch in dedicated window.
(error "Window %s is dedicated to buffer %s" window old-buffer))
(catch 'found
;; Scan WINDOW's previous buffers first, skipping entries of next
;; buffers.
(dolist (entry (window-prev-buffers window))
(when (and (setq new-buffer (car entry))
(or (buffer-live-p new-buffer)
(not (setq killed-buffers
(cons new-buffer killed-buffers))))
(not (eq new-buffer old-buffer))
(or (null pred) (funcall pred new-buffer))
;; When BURY-OR-KILL is nil, avoid switching to a
;; buffer in WINDOW's next buffers list.
(or bury-or-kill (not (memq new-buffer next-buffers))))
(if (and (not switch-to-visible-buffer)
(get-buffer-window new-buffer frame))
;; Try to avoid showing a buffer visible in some other
;; window.
(setq visible new-buffer)
(set-window-buffer-start-and-point
window new-buffer (nth 1 entry) (nth 2 entry))
(throw 'found t))))
;; Scan reverted buffer list of WINDOW's frame next, skipping
;; entries of next buffers. Note that when we bury or kill a
;; buffer we don't reverse the global buffer list to avoid showing
;; a buried buffer instead. Otherwise, we must reverse the global
;; buffer list in order to make sure that switching to the
;; previous/next buffer traverse it in opposite directions.
(dolist (buffer (if bury-or-kill
(buffer-list frame)
(nreverse (buffer-list frame))))
(when (and (buffer-live-p buffer)
(not (eq buffer old-buffer))
(or (null pred) (funcall pred buffer))
(not (eq (aref (buffer-name buffer) 0) ?\s))
(or bury-or-kill (not (memq buffer next-buffers))))
(if (get-buffer-window buffer frame)
;; Try to avoid showing a buffer visible in some other window.
(unless visible
(setq visible buffer))
(setq new-buffer buffer)
(set-window-buffer-start-and-point window new-buffer)
(throw 'found t))))
(unless bury-or-kill
;; Scan reverted next buffers last (must not use nreverse
;; here!).
(dolist (buffer (reverse next-buffers))
;; Actually, buffer _must_ be live here since otherwise it
;; would have been caught in the scan of previous buffers.
(when (and (or (buffer-live-p buffer)
(not (setq killed-buffers
(cons buffer killed-buffers))))
(not (eq buffer old-buffer))
(or (null pred) (funcall pred buffer))
(setq entry (assq buffer (window-prev-buffers window))))
(setq new-buffer buffer)
(set-window-buffer-start-and-point
window new-buffer (nth 1 entry) (nth 2 entry))
(throw 'found t))))
;; Show a buffer visible in another window.
(when visible
(setq new-buffer visible)
(set-window-buffer-start-and-point window new-buffer)))
(if bury-or-kill
(let ((entry (and (eq bury-or-kill 'append)
(assq old-buffer (window-prev-buffers window)))))
;; Remove `old-buffer' from WINDOW's previous and (restored list
;; of) next buffers.
(set-window-prev-buffers
window (assq-delete-all old-buffer (window-prev-buffers window)))
(set-window-next-buffers window (delq old-buffer next-buffers))
(when entry
;; Append old-buffer's entry to list of WINDOW's previous
;; buffers so it's less likely to get switched to soon but
;; `display-buffer-in-previous-window' can nevertheless find
;; it.
(set-window-prev-buffers
window (append (window-prev-buffers window) (list entry)))))
;; Move `old-buffer' to head of WINDOW's restored list of next
;; buffers.
(set-window-next-buffers
window (cons old-buffer (delq old-buffer next-buffers))))
;; Remove killed buffers from WINDOW's previous and next buffers.
(when killed-buffers
(dolist (buffer killed-buffers)
(set-window-prev-buffers
window (assq-delete-all buffer (window-prev-buffers window)))
(set-window-next-buffers
window (delq buffer (window-next-buffers window)))))
;; Return new-buffer.
new-buffer))
(defun switch-to-next-buffer (&optional window)
"In WINDOW switch to next buffer.
WINDOW must be a live window and defaults to the selected one.
Return the buffer switched to, nil if no suitable buffer could be
found."
(interactive)
(let* ((window (window-normalize-window window t))
(frame (window-frame window))
(old-buffer (window-buffer window))
(next-buffers (window-next-buffers window))
(pred (frame-parameter frame 'buffer-predicate))
new-buffer entry killed-buffers visible)
(when (window-minibuffer-p window)
;; Don't switch in minibuffer window.
(unless (setq window (minibuffer-selected-window))
(error "Window %s is a minibuffer window" window)))
(when (window-dedicated-p window)
;; Don't switch in dedicated window.
(error "Window %s is dedicated to buffer %s" window old-buffer))
(catch 'found
;; Scan WINDOW's next buffers first.
(dolist (buffer next-buffers)
(when (and (or (buffer-live-p buffer)
(not (setq killed-buffers
(cons buffer killed-buffers))))
(not (eq buffer old-buffer))
(or (null pred) (funcall pred buffer))
(setq entry (assq buffer (window-prev-buffers window))))
(setq new-buffer buffer)
(set-window-buffer-start-and-point
window new-buffer (nth 1 entry) (nth 2 entry))
(throw 'found t)))
;; Scan the buffer list of WINDOW's frame next, skipping previous
;; buffers entries.
(dolist (buffer (buffer-list frame))
(when (and (buffer-live-p buffer)
(not (eq buffer old-buffer))
(or (null pred) (funcall pred buffer))
(not (eq (aref (buffer-name buffer) 0) ?\s))
(not (assq buffer (window-prev-buffers window))))
(if (get-buffer-window buffer frame)
;; Try to avoid showing a buffer visible in some other window.
(setq visible buffer)
(setq new-buffer buffer)
(set-window-buffer-start-and-point window new-buffer)
(throw 'found t))))
;; Scan WINDOW's reverted previous buffers last (must not use
;; nreverse here!)
(dolist (entry (reverse (window-prev-buffers window)))
(when (and (setq new-buffer (car entry))
(or (buffer-live-p new-buffer)
(not (setq killed-buffers
(cons new-buffer killed-buffers))))
(not (eq new-buffer old-buffer))
(or (null pred) (funcall pred new-buffer)))
(if (and (not switch-to-visible-buffer)
(get-buffer-window new-buffer frame))
;; Try to avoid showing a buffer visible in some other window.
(unless visible
(setq visible new-buffer))
(set-window-buffer-start-and-point
window new-buffer (nth 1 entry) (nth 2 entry))
(throw 'found t))))
;; Show a buffer visible in another window.
(when visible
(setq new-buffer visible)
(set-window-buffer-start-and-point window new-buffer)))
;; Remove `new-buffer' from and restore WINDOW's next buffers.
(set-window-next-buffers window (delq new-buffer next-buffers))
;; Remove killed buffers from WINDOW's previous and next buffers.
(when killed-buffers
(dolist (buffer killed-buffers)
(set-window-prev-buffers
window (assq-delete-all buffer (window-prev-buffers window)))
(set-window-next-buffers
window (delq buffer (window-next-buffers window)))))
;; Return new-buffer.
new-buffer))
(defun get-next-valid-buffer (list &optional buffer visible-ok frame)
"Search LIST for a valid buffer to display in FRAME.
Return nil when all buffers in LIST are undesirable for display,
otherwise return the first suitable buffer in LIST.
Buffers not visible in windows are preferred to visible buffers,
unless VISIBLE-OK is non-nil.
If the optional argument FRAME is nil, it defaults to the selected frame.
If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
;; This logic is more or less copied from other-buffer.
(setq frame (or frame (selected-frame)))
(let ((pred (frame-parameter frame 'buffer-predicate))
found buf)
(while (and (not found) list)
(setq buf (car list))
(if (and (not (eq buffer buf))
(buffer-live-p buf)
(or (null pred) (funcall pred buf))
(not (eq (aref (buffer-name buf) 0) ?\s))
(or visible-ok (null (get-buffer-window buf 'visible))))
(setq found buf)
(setq list (cdr list))))
(car list)))
(defun last-buffer (&optional buffer visible-ok frame)
"Return the last buffer in FRAME's buffer list.
If BUFFER is the last buffer, return the preceding buffer
instead. Buffers not visible in windows are preferred to visible
buffers, unless optional argument VISIBLE-OK is non-nil.
Optional third argument FRAME nil or omitted means use the
selected frame's buffer list. If no such buffer exists, return
the buffer `*scratch*', creating it if necessary."
(setq frame (or frame (selected-frame)))
(or (get-next-valid-buffer (nreverse (buffer-list frame))
buffer visible-ok frame)
(get-buffer "*scratch*")
(let ((scratch (get-buffer-create "*scratch*")))
(set-buffer-major-mode scratch)
scratch)))
(defcustom frame-auto-hide-function #'iconify-frame
"Function called to automatically hide frames.
The function is called with one argument - a frame.
Functions affected by this option are those that bury a buffer
shown in a separate frame like `quit-window' and `bury-buffer'."
:type '(choice (const :tag "Iconify" iconify-frame)
(const :tag "Delete" delete-frame)
(const :tag "Do nothing" ignore)
function)
:group 'windows
:group 'frames
:version "24.1")
(defun window--delete (&optional window dedicated-only kill)
"Delete WINDOW if possible.
WINDOW must be a live window and defaults to the selected one.
Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
only if it's dedicated to its buffer. Optional argument KILL
means the buffer shown in window will be killed. Return non-nil
if WINDOW gets deleted or its frame is auto-hidden."
(setq window (window-normalize-window window t))
(unless (and dedicated-only (not (window-dedicated-p window)))
(let ((deletable (window-deletable-p window)))
(cond
((eq deletable 'frame)
(let ((frame (window-frame window)))
(cond
(kill
(delete-frame frame))
((functionp frame-auto-hide-function)
(funcall frame-auto-hide-function frame))))
'frame)
(deletable
(delete-window window)
t)))))
(defun bury-buffer (&optional buffer-or-name)
"Put BUFFER-OR-NAME at the end of the list of all buffers.
There it is the least likely candidate for `other-buffer' to
return; thus, the least likely buffer for \\[switch-to-buffer] to
select by default.
You can specify a buffer name as BUFFER-OR-NAME, or an actual
buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
remove the current buffer from the selected window if it is
displayed there."
(interactive)
(let* ((buffer (window-normalize-buffer buffer-or-name)))
;; If `buffer-or-name' is not on the selected frame we unrecord it
;; although it's not "here" (call it a feature).
(bury-buffer-internal buffer)
;; Handle case where `buffer-or-name' is nil and the current buffer
;; is shown in the selected window.
(cond
((or buffer-or-name (not (eq buffer (window-buffer)))))
((window--delete nil t))
(t
;; Switch to another buffer in window.
(set-window-dedicated-p nil nil)
(switch-to-prev-buffer nil 'bury)))
;; Always return nil.
nil))
(defun unbury-buffer ()
"Switch to the last buffer in the buffer list."
(interactive)
(switch-to-buffer (last-buffer)))
(defun next-buffer ()
"In selected window switch to next buffer."
(interactive)
(cond
((window-minibuffer-p)
(error "Cannot switch buffers in minibuffer window"))
((eq (window-dedicated-p) t)
(error "Window is strongly dedicated to its buffer"))
(t
(switch-to-next-buffer))))
(defun previous-buffer ()
"In selected window switch to previous buffer."
(interactive)
(cond
((window-minibuffer-p)
(error "Cannot switch buffers in minibuffer window"))
((eq (window-dedicated-p) t)
(error "Window is strongly dedicated to its buffer"))
(t
(switch-to-prev-buffer))))
(defun delete-windows-on (&optional buffer-or-name frame)
"Delete all windows showing BUFFER-OR-NAME.
BUFFER-OR-NAME may be a buffer or the name of an existing buffer
and defaults to the current buffer.
The following non-nil values of the optional argument FRAME
have special meanings:
- t means consider all windows on the selected frame only.
- `visible' means consider all windows on all visible frames on
the current terminal.
- 0 (the number zero) means consider all windows on all visible
and iconified frames on the current terminal.