* lisp/auth-source.el (auth-source-backend-parse): `return' -> cl-return.
[emacs.git] / lisp / window.el
blob8b07ed462c970d31185ea345b8af88190510abe0
1 ;;; window.el --- GNU Emacs window commands aside from those written in C -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2017 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: internal
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Window tree functions.
29 ;;; Code:
31 (defun internal--before-save-selected-window ()
32 (cons (selected-window)
33 ;; We save and restore all frames' selected windows, because
34 ;; `select-window' can change the frame-selected-window of
35 ;; whatever frame that window is in. Each text terminal's
36 ;; top-frame is preserved by putting it last in the list.
37 (apply #'append
38 (mapcar (lambda (terminal)
39 (let ((frames (frames-on-display-list terminal))
40 (top-frame (tty-top-frame terminal))
41 alist)
42 (if top-frame
43 (setq frames
44 (cons top-frame
45 (delq top-frame frames))))
46 (dolist (f frames)
47 (push (cons f (frame-selected-window f))
48 alist))
49 alist))
50 (terminal-list)))))
52 (defun internal--after-save-selected-window (state)
53 (dolist (elt (cdr state))
54 (and (frame-live-p (car elt))
55 (window-live-p (cdr elt))
56 (set-frame-selected-window (car elt) (cdr elt) 'norecord)))
57 (when (window-live-p (car state))
58 (select-window (car state) 'norecord)))
60 (defmacro save-selected-window (&rest body)
61 "Execute BODY, then select the previously selected window.
62 The value returned is the value of the last form in BODY.
64 This macro saves and restores the selected window, as well as the
65 selected window in each frame. If the previously selected window
66 is no longer live, then whatever window is selected at the end of
67 BODY remains selected. If the previously selected window of some
68 frame is no longer live at the end of BODY, that frame's selected
69 window is left alone.
71 This macro saves and restores the current buffer, since otherwise
72 its normal operation could make a different buffer current. The
73 order of recently selected windows and the buffer list ordering
74 are not altered by this macro (unless they are altered in BODY)."
75 (declare (indent 0) (debug t))
76 `(let ((save-selected-window--state (internal--before-save-selected-window)))
77 (save-current-buffer
78 (unwind-protect
79 (progn ,@body)
80 (internal--after-save-selected-window save-selected-window--state)))))
82 (defvar temp-buffer-window-setup-hook nil
83 "Normal hook run by `with-temp-buffer-window' before buffer display.
84 This hook is run by `with-temp-buffer-window' with the buffer to be
85 displayed current.")
87 (defvar temp-buffer-window-show-hook nil
88 "Normal hook run by `with-temp-buffer-window' after buffer display.
89 This hook is run by `with-temp-buffer-window' with the buffer
90 displayed and current and its window selected.")
92 (defun temp-buffer-window-setup (buffer-or-name)
93 "Set up temporary buffer specified by BUFFER-OR-NAME.
94 Return the buffer."
95 (let ((old-dir default-directory)
96 (buffer (get-buffer-create buffer-or-name)))
97 (with-current-buffer buffer
98 (kill-all-local-variables)
99 (setq default-directory old-dir)
100 (delete-all-overlays)
101 (setq buffer-read-only nil)
102 (setq buffer-file-name nil)
103 (setq buffer-undo-list t)
104 (let ((inhibit-read-only t)
105 (inhibit-modification-hooks t))
106 (erase-buffer)
107 (run-hooks 'temp-buffer-window-setup-hook))
108 ;; Return the buffer.
109 buffer)))
111 (defun temp-buffer-window-show (buffer &optional action)
112 "Show temporary buffer BUFFER in a window.
113 Return the window showing BUFFER. Pass ACTION as action argument
114 to `display-buffer'."
115 (let (window frame)
116 (with-current-buffer buffer
117 (set-buffer-modified-p nil)
118 (setq buffer-read-only t)
119 (goto-char (point-min))
120 (when (let ((window-combination-limit
121 ;; When `window-combination-limit' equals
122 ;; `temp-buffer' or `temp-buffer-resize' and
123 ;; `temp-buffer-resize-mode' is enabled in this
124 ;; buffer bind it to t so resizing steals space
125 ;; preferably from the window that was split.
126 (if (or (eq window-combination-limit 'temp-buffer)
127 (and (eq window-combination-limit
128 'temp-buffer-resize)
129 temp-buffer-resize-mode))
131 window-combination-limit)))
132 (setq window (display-buffer buffer action)))
133 (setq frame (window-frame window))
134 (unless (eq frame (selected-frame))
135 (raise-frame frame))
136 (setq minibuffer-scroll-window window)
137 (set-window-hscroll window 0)
138 (with-selected-window window
139 (run-hooks 'temp-buffer-window-show-hook)
140 (when temp-buffer-resize-mode
141 (resize-temp-buffer-window window)))
142 ;; Return the window.
143 window))))
145 (defmacro with-temp-buffer-window (buffer-or-name action quit-function &rest body)
146 "Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
147 BUFFER-OR-NAME must specify either a live buffer, or the name of
148 a buffer (if it does not exist, this macro creates it).
150 Make the buffer specified by BUFFER-OR-NAME empty before running
151 BODY and bind `standard-output' to that buffer, so that output
152 generated with `prin1' and similar functions in BODY goes into
153 that buffer. Do not make that buffer current for running the
154 forms in BODY. Use `with-current-buffer-window' instead if you
155 need to run BODY with that buffer current.
157 At the end of BODY, mark the specified buffer unmodified and
158 read-only, and display it in a window (but do not select it).
159 The display happens by calling `display-buffer' passing it the
160 ACTION argument. If `temp-buffer-resize-mode' is enabled, the
161 corresponding window may be resized automatically.
163 Return the value returned by BODY, unless QUIT-FUNCTION specifies
164 a function. In that case, run that function with two arguments -
165 the window showing the specified buffer and the value returned by
166 BODY - and return the value returned by that function.
168 If the buffer is displayed on a new frame, the window manager may
169 decide to select that frame. In that case, it's usually a good
170 strategy if QUIT-FUNCTION selects the window showing the buffer
171 before reading any value from the minibuffer; for example, when
172 asking a `yes-or-no-p' question.
174 This runs the hook `temp-buffer-window-setup-hook' before BODY,
175 with the specified buffer temporarily current. It runs the hook
176 `temp-buffer-window-show-hook' after displaying the buffer, with
177 that buffer temporarily current, and the window that was used to
178 display it temporarily selected.
180 This construct is similar to `with-output-to-temp-buffer' but,
181 neither runs `temp-buffer-setup-hook' which usually puts the
182 buffer in Help mode, nor `temp-buffer-show-function' (the ACTION
183 argument replaces this)."
184 (declare (debug t))
185 (let ((buffer (make-symbol "buffer"))
186 (window (make-symbol "window"))
187 (value (make-symbol "value")))
188 (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
189 (vaction action)
190 (vquit-function quit-function))
191 `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
192 (standard-output ,buffer)
193 ,window ,value)
194 (setq ,value (progn ,@body))
195 (with-current-buffer ,buffer
196 (setq ,window (temp-buffer-window-show ,buffer ,vaction)))
198 (if (functionp ,vquit-function)
199 (funcall ,vquit-function ,window ,value)
200 ,value)))))
202 (defmacro with-current-buffer-window (buffer-or-name action quit-function &rest body)
203 "Evaluate BODY with a buffer BUFFER-OR-NAME current and show that buffer.
204 This construct is like `with-temp-buffer-window' but unlike that
205 makes the buffer specified by BUFFER-OR-NAME current for running
206 BODY."
207 (declare (debug t))
208 (let ((buffer (make-symbol "buffer"))
209 (window (make-symbol "window"))
210 (value (make-symbol "value")))
211 (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
212 (vaction action)
213 (vquit-function quit-function))
214 `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
215 (standard-output ,buffer)
216 ,window ,value)
217 (with-current-buffer ,buffer
218 (setq ,value (progn ,@body))
219 (setq ,window (temp-buffer-window-show ,buffer ,vaction)))
221 (if (functionp ,vquit-function)
222 (funcall ,vquit-function ,window ,value)
223 ,value)))))
225 (defmacro with-displayed-buffer-window (buffer-or-name action quit-function &rest body)
226 "Show a buffer BUFFER-OR-NAME and evaluate BODY in that buffer.
227 This construct is like `with-current-buffer-window' but unlike that
228 displays the buffer specified by BUFFER-OR-NAME before running BODY."
229 (declare (debug t))
230 (let ((buffer (make-symbol "buffer"))
231 (window (make-symbol "window"))
232 (value (make-symbol "value")))
233 (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
234 (vaction action)
235 (vquit-function quit-function))
236 `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
237 (standard-output ,buffer)
238 ;; If a 'window-height' entry specifies a function,
239 ;; remember it here in order to call it below but replace
240 ;; the entry so `window--try-to-split-window' will bind
241 ;; `window-combination-limit' to t and the function does
242 ;; not resize any other window but the one we split this
243 ;; one off (Bug#25055, Bug#25179).
244 (vheight-function
245 (let ((window-height (assq 'window-height (cdr ,vaction))))
246 (when (functionp (cdr window-height))
247 (cdr window-height))))
248 (vaction-copied
249 (when vheight-function
250 (cons (car , vaction)
251 (cons
252 '(window-height . t)
253 (assq-delete-all
254 'window-height (cdr (copy-sequence ,vaction)))))))
255 ,window ,value)
256 (with-current-buffer ,buffer
257 (setq ,window (temp-buffer-window-show
258 ,buffer (or vaction-copied ,vaction))))
260 (let ((inhibit-read-only t)
261 (inhibit-modification-hooks t))
262 (setq ,value (progn ,@body)))
264 (set-window-point ,window (point-min))
266 (when vheight-function
267 (ignore-errors
268 (set-window-parameter ,window 'preserve-size nil)
269 (funcall vheight-function ,window)))
271 (when (consp (cdr (assq 'preserve-size (cdr ,vaction))))
272 (window-preserve-size
273 ,window t (cadr (assq 'preserve-size (cdr ,vaction))))
274 (window-preserve-size
275 ,window nil (cddr (assq 'preserve-size (cdr ,vaction)))))
277 (if (functionp ,vquit-function)
278 (funcall ,vquit-function ,window ,value)
279 ,value)))))
281 ;; The following two functions are like `window-next-sibling' and
282 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
283 ;; they don't substitute the selected window for nil), and they return
284 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
285 ;; a minibuffer window).
286 (defun window-right (window)
287 "Return WINDOW's right sibling.
288 Return nil if WINDOW is the root window of its frame. WINDOW can
289 be any window."
290 (and window (window-parent window) (window-next-sibling window)))
292 (defun window-left (window)
293 "Return WINDOW's left sibling.
294 Return nil if WINDOW is the root window of its frame. WINDOW can
295 be any window."
296 (and window (window-parent window) (window-prev-sibling window)))
298 (defun window-child (window)
299 "Return WINDOW's first child window.
300 WINDOW can be any window."
301 (or (window-top-child window) (window-left-child window)))
303 (defun window-child-count (window)
304 "Return number of WINDOW's child windows.
305 WINDOW can be any window."
306 (let ((count 0))
307 (when (and (windowp window) (setq window (window-child window)))
308 (while window
309 (setq count (1+ count))
310 (setq window (window-next-sibling window))))
311 count))
313 (defun window-last-child (window)
314 "Return last child window of WINDOW.
315 WINDOW can be any window."
316 (when (and (windowp window) (setq window (window-child window)))
317 (while (window-next-sibling window)
318 (setq window (window-next-sibling window))))
319 window)
321 (defun window-normalize-buffer (buffer-or-name)
322 "Return buffer specified by BUFFER-OR-NAME.
323 BUFFER-OR-NAME must be either a buffer or a string naming a live
324 buffer and defaults to the current buffer."
325 (cond
326 ((not buffer-or-name)
327 (current-buffer))
328 ((bufferp buffer-or-name)
329 (if (buffer-live-p buffer-or-name)
330 buffer-or-name
331 (error "Buffer %s is not a live buffer" buffer-or-name)))
332 ((get-buffer buffer-or-name))
334 (error "No such buffer %s" buffer-or-name))))
336 (defun window-normalize-frame (frame)
337 "Return frame specified by FRAME.
338 FRAME must be a live frame and defaults to the selected frame."
339 (if frame
340 (if (frame-live-p frame)
341 frame
342 (error "%s is not a live frame" frame))
343 (selected-frame)))
345 (defun window-normalize-window (window &optional live-only)
346 "Return the window specified by WINDOW.
347 If WINDOW is nil, return the selected window. Otherwise, if
348 WINDOW is a live or an internal window, return WINDOW; if
349 LIVE-ONLY is non-nil, return WINDOW for a live window only.
350 Otherwise, signal an error."
351 (cond
352 ((null window)
353 (selected-window))
354 (live-only
355 (if (window-live-p window)
356 window
357 (error "%s is not a live window" window)))
358 ((window-valid-p window)
359 window)
361 (error "%s is not a valid window" window))))
363 ;; Maybe this should go to frame.el.
364 (defun frame-char-size (&optional window-or-frame horizontal)
365 "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
366 If WINDOW-OR-FRAME is a live frame, return the value of
367 `frame-char-height' for that frame. If WINDOW-OR-FRAME is a
368 valid window, return the value of `frame-char-height' for that
369 window's frame. In any other case, return the value of
370 `frame-char-height' for the selected frame.
372 Optional argument HORIZONTAL non-nil means to return the value of
373 `frame-char-width' for WINDOW-OR-FRAME."
374 (let ((frame
375 (cond
376 ((window-valid-p window-or-frame)
377 (window-frame window-or-frame))
378 ((frame-live-p window-or-frame)
379 window-or-frame)
380 (t (selected-frame)))))
381 (if horizontal
382 (frame-char-width frame)
383 (frame-char-height frame))))
385 (defvar ignore-window-parameters nil
386 "If non-nil, standard functions ignore window parameters.
387 The functions currently affected by this are `split-window',
388 `delete-window', `delete-other-windows' and `other-window'.
390 An application may bind this to a non-nil value around calls to
391 these functions to inhibit processing of window parameters.")
393 ;; This must go to C, finally (or get removed).
394 (defconst window-safe-min-height 1
395 "The absolute minimum number of lines of any window.
396 Anything less might crash Emacs.")
398 (defun window-safe-min-pixel-height (&optional window)
399 "Return the absolute minimum pixel height of WINDOW."
400 (* window-safe-min-height
401 (frame-char-size (window-normalize-window window))))
403 (defcustom window-min-height 4
404 "The minimum total height, in lines, of any window.
405 The value has to accommodate one text line, a mode and header
406 line, a horizontal scroll bar and a bottom divider, if present.
407 A value less than `window-safe-min-height' is ignored. The value
408 of this variable is honored when windows are resized or split.
410 Applications should never rebind this variable. To resize a
411 window to a height less than the one specified here, an
412 application should instead call `window-resize' with a non-nil
413 IGNORE argument. In order to have `split-window' make a window
414 shorter, explicitly specify the SIZE argument of that function."
415 :type 'integer
416 :version "24.1"
417 :group 'windows)
419 (defun window-min-pixel-height (&optional window)
420 "Return the minimum pixel height of window WINDOW."
421 (* (max window-min-height window-safe-min-height)
422 (frame-char-size window)))
424 ;; This must go to C, finally (or get removed).
425 (defconst window-safe-min-width 2
426 "The absolute minimum number of columns of a window.
427 Anything less might crash Emacs.")
429 (defun window-safe-min-pixel-width (&optional window)
430 "Return the absolute minimum pixel width of WINDOW."
431 (* window-safe-min-width
432 (frame-char-size (window-normalize-window window) t)))
434 (defcustom window-min-width 10
435 "The minimum total width, in columns, of any window.
436 The value has to accommodate two text columns as well as margins,
437 fringes, a scroll bar and a right divider, if present. A value
438 less than `window-safe-min-width' is ignored. The value of this
439 variable is honored when windows are resized or split.
441 Applications should never rebind this variable. To resize a
442 window to a width less than the one specified here, an
443 application should instead call `window-resize' with a non-nil
444 IGNORE argument. In order to have `split-window' make a window
445 narrower, explicitly specify the SIZE argument of that function."
446 :type 'integer
447 :version "24.1"
448 :group 'windows)
450 (defun window-min-pixel-width (&optional window)
451 "Return the minimum pixel width of window WINDOW."
452 (* (max window-min-width window-safe-min-width)
453 (frame-char-size window t)))
455 (defun window-safe-min-pixel-size (&optional window horizontal)
456 "Return the absolute minimum pixel height of WINDOW.
457 Optional argument HORIZONTAL non-nil means return the absolute
458 minimum pixel width of WINDOW."
459 (if horizontal
460 (window-safe-min-pixel-width window)
461 (window-safe-min-pixel-height window)))
463 (defun window-min-pixel-size (&optional window horizontal)
464 "Return the minimum pixel height of WINDOW.
465 Optional argument HORIZONTAL non-nil means return the minimum
466 pixel width of WINDOW."
467 (if horizontal
468 (window-min-pixel-width window)
469 (window-min-pixel-height window)))
471 (defun window-combined-p (&optional window horizontal)
472 "Return non-nil if WINDOW has siblings in a given direction.
473 WINDOW must be a valid window and defaults to the selected one.
475 HORIZONTAL determines a direction for the window combination. If
476 HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
477 a vertical window combination. If HORIZONTAL is non-nil, return
478 non-nil if WINDOW is part of a horizontal window combination."
479 (setq window (window-normalize-window window))
480 (let ((parent (window-parent window)))
481 (and parent
482 (if horizontal
483 (window-left-child parent)
484 (window-top-child parent)))))
486 (defun window-combination-p (&optional window horizontal)
487 "Return WINDOW's first child if WINDOW is a vertical combination.
488 WINDOW can be any window and defaults to the selected one.
489 Optional argument HORIZONTAL non-nil means return WINDOW's first
490 child if WINDOW is a horizontal combination."
491 (setq window (window-normalize-window window))
492 (if horizontal
493 (window-left-child window)
494 (window-top-child window)))
496 (defun window-combinations (window &optional horizontal)
497 "Return largest number of windows vertically arranged within WINDOW.
498 WINDOW must be a valid window and defaults to the selected one.
499 If HORIZONTAL is non-nil, return the largest number of
500 windows horizontally arranged within WINDOW."
501 (setq window (window-normalize-window window))
502 (cond
503 ((window-live-p window)
504 ;; If WINDOW is live, return 1.
506 ((if horizontal
507 (window-left-child window)
508 (window-top-child window))
509 ;; If WINDOW is iso-combined, return the sum of the values for all
510 ;; child windows of WINDOW.
511 (let ((child (window-child window))
512 (count 0))
513 (while child
514 (setq count
515 (+ (window-combinations child horizontal)
516 count))
517 (setq child (window-right child)))
518 count))
520 ;; If WINDOW is not iso-combined, return the maximum value of any
521 ;; child window of WINDOW.
522 (let ((child (window-child window))
523 (count 1))
524 (while child
525 (setq count
526 (max (window-combinations child horizontal)
527 count))
528 (setq child (window-right child)))
529 count))))
531 (defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only)
532 "Helper function for `walk-window-tree' and `walk-window-subtree'."
533 (let (walk-window-tree-buffer)
534 (while walk-window-tree-window
535 (setq walk-window-tree-buffer
536 (window-buffer walk-window-tree-window))
537 (when (or walk-window-tree-buffer any)
538 (funcall fun walk-window-tree-window))
539 (unless walk-window-tree-buffer
540 (walk-window-tree-1
541 fun (window-left-child walk-window-tree-window) any)
542 (walk-window-tree-1
543 fun (window-top-child walk-window-tree-window) any))
544 (if sub-only
545 (setq walk-window-tree-window nil)
546 (setq walk-window-tree-window
547 (window-right walk-window-tree-window))))))
549 (defun walk-window-tree (fun &optional frame any minibuf)
550 "Run function FUN on each live window of FRAME.
551 FUN must be a function with one argument - a window. FRAME must
552 be a live frame and defaults to the selected one. ANY, if
553 non-nil, means to run FUN on all live and internal windows of
554 FRAME.
556 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
557 window even if it isn't active. MINIBUF nil or omitted means run
558 FUN on FRAME's minibuffer window only if it's active. In both
559 cases the minibuffer window must be part of FRAME. MINIBUF
560 neither nil nor t means never run FUN on the minibuffer window.
562 This function performs a pre-order, depth-first traversal of the
563 window tree. If FUN changes the window tree, the result is
564 unpredictable."
565 (setq frame (window-normalize-frame frame))
566 (walk-window-tree-1 fun (frame-root-window frame) any)
567 (when (memq minibuf '(nil t))
568 ;; Run FUN on FRAME's minibuffer window if requested.
569 (let ((minibuffer-window (minibuffer-window frame)))
570 (when (and (window-live-p minibuffer-window)
571 (eq (window-frame minibuffer-window) frame)
572 (or (eq minibuf t)
573 (minibuffer-window-active-p minibuffer-window)))
574 (funcall fun minibuffer-window)))))
576 (defun walk-window-subtree (fun &optional window any)
577 "Run function FUN on the subtree of windows rooted at WINDOW.
578 WINDOW defaults to the selected window. FUN must be a function
579 with one argument - a window. By default, run FUN only on live
580 windows of the subtree. If the optional argument ANY is non-nil,
581 run FUN on all live and internal windows of the subtree. If
582 WINDOW is live, run FUN on WINDOW only.
584 This function performs a pre-order, depth-first traversal of the
585 subtree rooted at WINDOW. If FUN changes that tree, the result
586 is unpredictable."
587 (setq window (window-normalize-window window))
588 (walk-window-tree-1 fun window any t))
590 (defun window-with-parameter (parameter &optional value frame any minibuf)
591 "Return first window on FRAME with PARAMETER non-nil.
592 FRAME defaults to the selected frame. Optional argument VALUE
593 non-nil means only return a window whose window-parameter value
594 for PARAMETER equals VALUE (comparison is done with `equal').
595 Optional argument ANY non-nil means consider internal windows
596 too.
598 Optional argument MINIBUF t means consider FRAME's minibuffer
599 window even if it isn't active. MINIBUF nil or omitted means
600 consider FRAME's minibuffer window only if it's active. In both
601 cases the minibuffer window must be part of FRAME. MINIBUF
602 neither nil nor t means never consider the minibuffer window."
603 (let (this-value)
604 (catch 'found
605 (walk-window-tree
606 (lambda (window)
607 (when (and (setq this-value (window-parameter window parameter))
608 (or (not value) (equal value this-value)))
609 (throw 'found window)))
610 frame any minibuf))))
612 ;;; Atomic windows.
613 (defun window-atom-root (&optional window)
614 "Return root of atomic window WINDOW is a part of.
615 WINDOW must be a valid window and defaults to the selected one.
616 Return nil if WINDOW is not part of an atomic window."
617 (setq window (window-normalize-window window))
618 (let (root)
619 (while (and window (window-parameter window 'window-atom))
620 (setq root window)
621 (setq window (window-parent window)))
622 root))
624 (defun window-make-atom (window)
625 "Make WINDOW an atomic window.
626 WINDOW must be an internal window. Return WINDOW."
627 (if (not (window-child window))
628 (error "Window %s is not an internal window" window)
629 (walk-window-subtree
630 (lambda (window)
631 (unless (window-parameter window 'window-atom)
632 (set-window-parameter window 'window-atom t)))
633 window t)
634 window))
636 (defun display-buffer-in-atom-window (buffer alist)
637 "Display BUFFER in an atomic window.
638 This function displays BUFFER in a new window that will be
639 combined with an existing window to form an atomic window. If
640 the existing window is already part of an atomic window, add the
641 new window to that atomic window. Operations like `split-window'
642 or `delete-window', when applied to a constituent of an atomic
643 window, are applied atomically to the root of that atomic window.
645 ALIST is an association list of symbols and values. The
646 following symbols can be used.
648 `window' specifies the existing window the new window shall be
649 combined with. Use `window-atom-root' to make the new window a
650 sibling of an atomic window's root. If an internal window is
651 specified here, all children of that window become part of the
652 atomic window too. If no window is specified, the new window
653 becomes a sibling of the selected window. By default, the
654 `window-atom' parameter of the existing window is set to `main'
655 provided it is live and was not set before.
657 `side' denotes the side of the existing window where the new
658 window shall be located. Valid values are `below', `right',
659 `above' and `left'. The default is `below'. By default, the
660 `window-atom' parameter of the new window is set to this value.
662 The return value is the new window, nil when creating that window
663 failed."
664 (let* ((ignore-window-parameters t)
665 (window-combination-limit t)
666 (window-combination-resize 'atom)
667 (window (cdr (assq 'window alist)))
668 (side (or (cdr (assq 'side alist)) 'below))
669 (atom (when window (window-parameter window 'window-atom)))
670 root new)
671 (setq window (window-normalize-window window))
672 (setq root (window-atom-root window))
673 ;; Split off new window.
674 (when (setq new (split-window-no-error window nil side))
675 (window-make-atom
676 (if (and root (not (eq root window)))
677 ;; When WINDOW was part of an atomic window and we did not
678 ;; split its root, root atomic window at old root.
679 root
680 ;; Otherwise, root atomic window at WINDOW's new parent.
681 (window-parent window)))
682 ;; Assign `window-atom' parameters, if needed.
683 (when (and (not atom) (window-live-p window))
684 (set-window-parameter window 'window-atom 'main))
685 (set-window-parameter new 'window-atom side)
686 ;; Display BUFFER in NEW and return NEW.
687 (window--display-buffer
688 buffer new 'window alist display-buffer-mark-dedicated))))
690 (defun window--atom-check-1 (window)
691 "Subroutine of `window--atom-check'."
692 (when window
693 (if (window-parameter window 'window-atom)
694 (let ((count 0))
695 (when (or (catch 'reset
696 (walk-window-subtree
697 (lambda (window)
698 (if (window-parameter window 'window-atom)
699 (setq count (1+ count))
700 (throw 'reset t)))
701 window t))
702 ;; count >= 1 must hold here. If there's no other
703 ;; window around dissolve this atomic window.
704 (= count 1))
705 ;; Dissolve atomic window.
706 (walk-window-subtree
707 (lambda (window)
708 (set-window-parameter window 'window-atom nil))
709 window t)))
710 ;; Check children.
711 (unless (window-buffer window)
712 (window--atom-check-1 (window-left-child window))
713 (window--atom-check-1 (window-top-child window))))
714 ;; Check right sibling
715 (window--atom-check-1 (window-right window))))
717 (defun window--atom-check (&optional frame)
718 "Check atomicity of all windows on FRAME.
719 FRAME defaults to the selected frame. If an atomic window is
720 wrongly configured, reset the atomicity of all its windows on
721 FRAME to nil. An atomic window is wrongly configured if it has
722 no child windows or one of its child windows is not atomic."
723 (window--atom-check-1 (frame-root-window frame)))
725 ;; Side windows.
726 (defcustom window-sides-vertical nil
727 "If non-nil, left and right side windows occupy full frame height.
728 If nil, top and bottom side windows occupy full frame width."
729 :type 'boolean
730 :initialize 'custom-initialize-default
731 :set 'window--sides-verticalize
732 :group 'windows
733 :version "26.1")
735 (defcustom window-sides-reversed nil
736 "Whether top/bottom side windows appear in reverse order.
737 When this is nil, side windows on the top and bottom of a frame
738 are always drawn from left to right with increasing slot values.
739 When this is t, side windows on the top and bottom of a frame are
740 always drawn from right to left with increasing slot values.
742 When this is `bidi', the drawing order is like that for the value
743 t if the value of `bidi-paragraph-direction' is `right-to-left'
744 in the buffer most recently shown in the window selected within
745 the main window area of this frame.
747 The layout of side windows on the left or right of a frame is not
748 affected by the value of this variable."
749 :type
750 '(choice (const :tag "Never" nil)
751 (const :tag "Bidi" bidi)
752 (const :tag "Always" t))
753 :initialize 'custom-initialize-default
754 :set 'window--sides-reverse
755 :group 'windows
756 :version "26.1")
758 (defcustom window-sides-slots '(nil nil nil nil)
759 "Number of available side window slots on each side of a frame.
760 The value is a list of four elements specifying the maximum
761 number of side windows that may be created on the left, top,
762 right and bottom side of any frame.
764 If an element is a number, `display-buffer-in-side-window' will
765 refrain from making a new side window if the number of windows on
766 that side is equal to or exceeds that number. Rather, it will
767 reuse the window whose `window-slot' value is nearest to the slot
768 specified via its ALIST argument. If an element is nil, this
769 means there's no bound on the number of windows on that side."
770 :version "24.1"
771 :risky t
772 :type
773 '(list
774 :value (nil nil nil nil)
775 (choice
776 :tag "Left"
777 :help-echo "Maximum number of left side windows."
778 :value nil
779 :format "%[Left%] %v\n"
780 (const :tag "Unlimited" :format "%t" nil)
781 (integer :tag "Number" :value 2 :size 5))
782 (choice
783 :tag "Top"
784 :help-echo "Maximum number of top side windows."
785 :value nil
786 :format "%[Top%] %v\n"
787 (const :tag "Unlimited" :format "%t" nil)
788 (integer :tag "Number" :value 3 :size 5))
789 (choice
790 :tag "Right"
791 :help-echo "Maximum number of right side windows."
792 :value nil
793 :format "%[Right%] %v\n"
794 (const :tag "Unlimited" :format "%t" nil)
795 (integer :tag "Number" :value 2 :size 5))
796 (choice
797 :tag "Bottom"
798 :help-echo "Maximum number of bottom side windows."
799 :value nil
800 :format "%[Bottom%] %v\n"
801 (const :tag "Unlimited" :format "%t" nil)
802 (integer :tag "Number" :value 3 :size 5)))
803 :group 'windows)
805 (defvar-local window--sides-shown nil
806 "Non-nil if this buffer was shown in a side window once.
807 If this variable is non-nil in a buffer, `switch-to-prev-buffer'
808 and `switch-to-next-buffer' will refrain from showing this buffer
809 within the main window area. `display-buffer-in-side-window'
810 sets this variable automatically.
812 Killing buffer local variables after showing the buffer in a side
813 window annihilates any effect provided by this variable.")
815 (defvar window--sides-inhibit-check nil
816 "Non-nil means inhibit any checks on side windows.")
818 (defun window--sides-reverse-on-frame-p (frame)
819 "Return non-nil when side windows should appear reversed on FRAME.
820 This uses some heuristics to guess the user's intentions when the
821 selected window of FRAME is a side window ."
822 (cond
823 ;; Reverse when `window-sides-reversed' is t. Do not reverse when
824 ;; `window-sides-reversed' is nil.
825 ((memq window-sides-reversed '(nil t))
826 window-sides-reversed)
827 ;; Reverse when FRAME's selected window shows a right-to-left buffer.
828 ((let ((window (frame-selected-window frame)))
829 (when (and (not (window-parameter window 'window-side))
830 (or (not (window-minibuffer-p window))
831 (setq window (minibuffer-selected-window))))
832 (with-current-buffer (window-buffer window)
833 (eq bidi-paragraph-direction 'right-to-left)))))
834 ;; Reverse when FRAME's `window-sides-main-selected-window' parameter
835 ;; specifies a live window showing a right-to-left buffer.
836 ((let ((window (frame-parameter
837 frame 'window-sides-main-selected-window)))
838 (when (window-live-p window)
839 (with-current-buffer (window-buffer window)
840 (eq bidi-paragraph-direction 'right-to-left)))))
841 ;; Reverse when all windows in FRAME's main window show right-to-left
842 ;; buffers.
844 (catch 'found
845 (walk-window-subtree
846 (lambda (window)
847 (with-current-buffer (window-buffer window)
848 (when (eq bidi-paragraph-direction 'left-to-right)
849 (throw 'found nil))))
850 (window-main-window frame))
851 t))))
853 (defun window-main-window (&optional frame)
854 "Return the main window of specified FRAME.
855 The optional argument FRAME must be a live frame and defaults to
856 the selected one.
858 If FRAME has no side windows, return FRAME's root window.
859 Otherwise, return either an internal non-side window such that
860 all other non-side windows on FRAME descend from it, or the
861 single live non-side window of FRAME."
862 (let ((frame (window-normalize-frame frame))
863 main sibling)
864 ;; Set main to the _last_ window found by `walk-window-tree' that
865 ;; is not a side window but has a side window as its sibling.
866 (walk-window-tree
867 (lambda (window)
868 (and (not (window-parameter window 'window-side))
869 (or (and (setq sibling (window-prev-sibling window))
870 (window-parameter sibling 'window-side))
871 (and (setq sibling (window-next-sibling window))
872 (window-parameter sibling 'window-side)))
873 (setq main window)))
874 frame t 'nomini)
875 (or main (frame-root-window frame))))
877 (defun window--make-major-side-window-next-to (side)
878 "Return window to split for making a major side window.
879 SIDE must be one of the symbols `left', `top', `right' or
880 `bottom'.
882 This is an auxiliary function of `window--make-major-side-window'
883 and must not be called when a window on SIDE exists already."
884 (let ((root (frame-root-window))
885 (window--sides-inhibit-check t)
886 window)
887 ;; (1) If a window on the opposite side exists, return that window's
888 ;; sibling.
889 ;; (2) If the new window shall span the entire side, return the
890 ;; frame's root window.
891 ;; (3) If a window on an orthogonal side exists, return that
892 ;; window's sibling.
893 ;; (4) Otherwise return the frame's root window.
894 (cond
895 ((or (and (eq side 'left)
896 (setq window (window-with-parameter 'window-side 'right nil t)))
897 (and (eq side 'top)
898 (setq window (window-with-parameter 'window-side 'bottom nil t))))
899 (window-prev-sibling window))
900 ((or (and (eq side 'right)
901 (setq window (window-with-parameter 'window-side 'left nil t)))
902 (and (eq side 'bottom)
903 (setq window (window-with-parameter 'window-side 'top nil t))))
904 (window-next-sibling window))
905 ((memq side '(left right))
906 (cond
907 (window-sides-vertical
908 root)
909 ((setq window (window-with-parameter 'window-side 'top nil t))
910 (window-next-sibling window))
911 ((setq window (window-with-parameter 'window-side 'bottom nil t))
912 (window-prev-sibling window))
913 (t root)))
914 ((memq side '(top bottom))
915 (cond
916 ((not window-sides-vertical)
917 root)
918 ((setq window (window-with-parameter 'window-side 'left nil t))
919 (window-next-sibling window))
920 ((setq window (window-with-parameter 'window-side 'right nil t))
921 (window-prev-sibling window))
922 (t root))))))
924 (defun window--make-major-side-window (buffer side slot &optional alist)
925 "Display BUFFER in a new major side window on the selected frame.
926 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
927 specifies the slot to use. ALIST is an association list of
928 symbols and values as passed to `display-buffer-in-side-window'.
929 Return the new window, nil if its creation failed.
931 This is an auxiliary function of `display-buffer-in-side-window'
932 and may be called only if no window on SIDE exists yet."
933 (let* ((left-or-right (memq side '(left right)))
934 (next-to (window--make-major-side-window-next-to side))
935 (on-side (cond
936 ((eq side 'top) 'above)
937 ((eq side 'bottom) 'below)
938 (t side)))
939 (window--sides-inhibit-check t)
940 ;; The following two bindings will tell `split-window' to take
941 ;; the space for the new window from the selected frame's main
942 ;; window and not make a new parent window unless needed.
943 (window-combination-resize 'side)
944 (window-combination-limit nil)
945 (window (split-window-no-error next-to nil on-side)))
946 (when window
947 ;; Initialize `window-side' parameter of new window to SIDE and
948 ;; make that parameter persistent.
949 (set-window-parameter window 'window-side side)
950 (add-to-list 'window-persistent-parameters '(window-side . writable))
951 ;; Install `window-slot' parameter of new window and make that
952 ;; parameter persistent.
953 (set-window-parameter window 'window-slot slot)
954 (add-to-list 'window-persistent-parameters '(window-slot . writable))
955 ;; Auto-adjust height/width of new window unless a size has been
956 ;; explicitly requested.
957 (unless (if left-or-right
958 (cdr (assq 'window-width alist))
959 (cdr (assq 'window-height alist)))
960 (setq alist
961 (cons
962 (cons
963 (if left-or-right 'window-width 'window-height)
964 (/ (window-total-size (frame-root-window) left-or-right)
965 ;; By default use a fourth of the size of the frame's
966 ;; root window.
968 alist)))
969 (with-current-buffer buffer
970 (setq window--sides-shown t))
971 ;; Install BUFFER in new window and return WINDOW.
972 (window--display-buffer buffer window 'window alist 'side))))
974 (defun display-buffer-in-side-window (buffer alist)
975 "Display BUFFER in a side window of the selected frame.
976 ALIST is an association list of symbols and values. The
977 following special symbols can be used in ALIST.
979 `side' denotes the side of the frame where the new window shall
980 be located. Valid values are `bottom', `right', `top' and
981 `left'. The default is `bottom'.
983 `slot' if non-nil, specifies the window slot where to display
984 BUFFER. A value of zero or nil means use the middle slot on
985 the specified side. A negative value means use a slot
986 preceding (that is, above or on the left of) the middle slot.
987 A positive value means use a slot following (that is, below or
988 on the right of) the middle slot. The default is zero.
990 If the current frame size or the settings of `window-sides-slots'
991 do not permit making a new window, a suitable existing window may
992 be reused and have its `window-slot' parameter value accordingly
993 modified.
995 Unless `display-buffer-mark-dedicated' is non-nil, softly
996 dedicate the side window used to BUFFER. Return the window used
997 for displaying BUFFER, nil if no suitable window can be found.
999 This function installs the `window-side' and `window-slot'
1000 parameters and makes them persistent. It neither modifies ALIST
1001 nor installs any other window parameters unless they have been
1002 explicitly provided via a `window-parameter' entry in ALIST."
1003 (let* ((side (or (cdr (assq 'side alist)) 'bottom))
1004 (slot (or (cdr (assq 'slot alist)) 0))
1005 (left-or-right (memq side '(left right)))
1006 ;; Softly dedicate window to BUFFER unless
1007 ;; `display-buffer-mark-dedicated' already asks for it.
1008 (dedicated (or display-buffer-mark-dedicated 'side)))
1009 (cond
1010 ((not (memq side '(top bottom left right)))
1011 (error "Invalid side %s specified" side))
1012 ((not (numberp slot))
1013 (error "Invalid slot %s specified" slot)))
1015 (let* ((major (window-with-parameter 'window-side side nil t))
1016 ;; `major' is the major window on SIDE, `windows' the list of
1017 ;; life windows on SIDE.
1018 (reversed (window--sides-reverse-on-frame-p (selected-frame)))
1019 (windows
1020 (cond
1021 ((window-live-p major)
1022 (list major))
1023 ((window-valid-p major)
1024 (let* ((first (window-child major))
1025 (next (window-next-sibling first))
1026 (windows (list next first)))
1027 (setq reversed (> (window-parameter first 'window-slot)
1028 (window-parameter next 'window-slot)))
1029 (while (setq next (window-next-sibling next))
1030 (setq windows (cons next windows)))
1031 (if reversed windows (nreverse windows))))))
1032 (slots (when major (max 1 (window-child-count major))))
1033 (max-slots
1034 (nth (cond
1035 ((eq side 'left) 0)
1036 ((eq side 'top) 1)
1037 ((eq side 'right) 2)
1038 ((eq side 'bottom) 3))
1039 window-sides-slots))
1040 (window--sides-inhibit-check t)
1041 window this-window this-slot prev-window next-window
1042 best-window best-slot abs-slot)
1044 (cond
1045 ((and (numberp max-slots) (<= max-slots 0))
1046 ;; No side-slots available on this side. Don't raise an error,
1047 ;; just return nil.
1048 nil)
1049 ((not windows)
1050 ;; No major side window exists on this side, make one.
1051 (window--make-major-side-window buffer side slot alist))
1053 ;; Scan windows on SIDE.
1054 (catch 'found
1055 (dolist (window windows)
1056 (setq this-slot (window-parameter window 'window-slot))
1057 (cond
1058 ;; The following should not happen and probably be checked
1059 ;; by window--sides-check.
1060 ((not (numberp this-slot)))
1061 ((= this-slot slot)
1062 ;; A window with a matching slot has been found.
1063 (setq this-window window)
1064 (throw 'found t))
1066 ;; Check if this window has a better slot value wrt the
1067 ;; slot of the window we want.
1068 (setq abs-slot
1069 (if (or (and (> this-slot 0) (> slot 0))
1070 (and (< this-slot 0) (< slot 0)))
1071 (abs (- slot this-slot))
1072 (+ (abs slot) (abs this-slot))))
1073 (unless (and best-slot (<= best-slot abs-slot))
1074 (setq best-window window)
1075 (setq best-slot abs-slot))
1076 (if reversed
1077 (cond
1078 ((<= this-slot slot)
1079 (setq next-window window))
1080 ((not prev-window)
1081 (setq prev-window window)))
1082 (cond
1083 ((<= this-slot slot)
1084 (setq prev-window window))
1085 ((not next-window)
1086 (setq next-window window))))))))
1088 ;; `this-window' is the first window with the same SLOT.
1089 ;; `prev-window' is the window with the largest slot < SLOT. A new
1090 ;; window will be created after it.
1091 ;; `next-window' is the window with the smallest slot > SLOT. A new
1092 ;; window will be created before it.
1093 ;; `best-window' is the window with the smallest absolute difference
1094 ;; of its slot and SLOT.
1095 (or (and this-window
1096 ;; Reuse `this-window'.
1097 (with-current-buffer buffer
1098 (setq window--sides-shown t))
1099 (window--display-buffer
1100 buffer this-window 'reuse alist dedicated))
1101 (and (or (not max-slots) (< slots max-slots))
1102 (or (and next-window
1103 ;; Make new window before `next-window'.
1104 (let ((next-side (if left-or-right 'above 'left))
1105 (window-combination-resize 'side))
1106 (setq window (split-window-no-error
1107 next-window nil next-side))))
1108 (and prev-window
1109 ;; Make new window after `prev-window'.
1110 (let ((prev-side (if left-or-right 'below 'right))
1111 (window-combination-resize 'side))
1112 (setq window (split-window-no-error
1113 prev-window nil prev-side)))))
1114 (set-window-parameter window 'window-slot slot)
1115 (with-current-buffer buffer
1116 (setq window--sides-shown t))
1117 (window--display-buffer
1118 buffer window 'window alist dedicated))
1119 (and best-window
1120 ;; Reuse `best-window'.
1121 (progn
1122 ;; Give best-window the new slot value.
1123 (set-window-parameter best-window 'window-slot slot)
1124 (with-current-buffer buffer
1125 (setq window--sides-shown t))
1126 (window--display-buffer
1127 buffer best-window 'reuse alist dedicated)))))))))
1129 (defun window-toggle-side-windows (&optional frame)
1130 "Toggle side windows on specified FRAME.
1131 FRAME must be a live frame and defaults to the selected one.
1133 If FRAME has at least one side window, save FRAME's state in the
1134 FRAME's `window-state' frame parameter and delete all side
1135 windows on FRAME afterwards. Otherwise, if FRAME has a
1136 `window-state' parameter, use that to restore any side windows on
1137 FRAME leaving FRAME's main window alone. Signal an error if
1138 FRAME has no side window and no saved state is found."
1139 (interactive)
1140 (let* ((frame (window-normalize-frame frame))
1141 (window--sides-inhibit-check t)
1142 state)
1143 (cond
1144 ((window-with-parameter 'window-side nil frame)
1145 ;; At least one side window exists. Remove all side windows after
1146 ;; saving FRAME's state in its `window-state' parameter.
1147 (set-frame-parameter
1148 frame 'window-state (window-state-get (frame-root-window frame)))
1149 (let ((ignore-window-parameters t))
1150 (delete-other-windows (window-main-window frame))))
1151 ((setq state (frame-parameter frame 'window-state))
1152 ;; A window state was saved for FRAME. Restore it and put the
1153 ;; current root window into its main window.
1154 (let ((main-state (window-state-get (frame-root-window frame))))
1155 (window-state-put state (frame-root-window frame) t)
1156 (window-state-put main-state (window-main-window frame)))
1157 (window--sides-reverse-frame frame))
1159 (error "No side windows state found")))))
1161 (defun window--sides-reverse-all ()
1162 "Maybe reverse side windows on all frames."
1163 (unless window--sides-inhibit-check
1164 (dolist (frame (frame-list))
1165 (window--sides-reverse-frame frame))))
1167 (defun window--sides-reverse-frame (frame)
1168 "Maybe reverse side windows on FRAME."
1169 (when (eq window-sides-reversed 'bidi)
1170 (let ((window (frame-selected-window frame)))
1171 (unless (or (window-parameter window 'window-side)
1172 (window-minibuffer-p window))
1173 (set-frame-parameter
1174 frame 'window-sides-main-selected-window window))))
1175 (window--sides-reverse-side frame 'top)
1176 (window--sides-reverse-side frame 'bottom))
1178 (defun window--sides-reverse-side (frame side)
1179 "Maybe reverse windows on SIDE of FRAME."
1180 (let ((major (window-with-parameter 'window-side side frame t))
1181 (window--sides-inhibit-check t))
1182 (when (and major (not (window-live-p major)))
1183 (let* ((first (window-child major))
1184 (reversed (> (window-parameter first 'window-slot)
1185 (window-parameter
1186 (window-next-sibling first) 'window-slot)))
1187 (reverse (window--sides-reverse-on-frame-p frame)))
1188 (unless (eq reversed reverse)
1189 ;; We have to reverse.
1190 (let ((last (window-last-child major)))
1191 (while (and (not (eq first last))
1192 (not (eq first (window-next-sibling last))))
1193 (window-swap-states first last t)
1194 (setq first (window-next-sibling first))
1195 (setq last (window-prev-sibling last)))))))))
1197 (defun window--sides-reverse (symbol value)
1198 "Helper function for customizing `window-sides-reversed'."
1199 (set-default symbol value)
1200 (remove-hook 'buffer-list-update-hook 'window--sides-reverse-all)
1201 (remove-hook 'window-configuration-change-hook 'window--sides-reverse-all)
1202 (dolist (frame (frame-list))
1203 (set-frame-parameter frame 'window-sides-main-selected-window nil))
1204 (when (eq value 'bidi)
1205 (add-hook 'buffer-list-update-hook 'window--sides-reverse-all)
1206 (add-hook 'window-configuration-change-hook 'window--sides-reverse-all))
1207 (window--sides-reverse-all))
1209 (defun window--sides-verticalize-frame (&optional frame)
1210 "Maybe change side windows layout on specified FRAME."
1211 (setq frame (window-normalize-frame frame))
1212 (let ((window--sides-inhibit-check t)
1213 (root (frame-root-window frame))
1214 (main (window-main-window frame)))
1215 (when (and (not (eq main root))
1216 (not (eq (window-parent main) root))
1217 (window-combined-p main window-sides-vertical))
1218 (let* ((window--sides-inhibit-check t)
1219 (ignore-window-parameters t)
1220 (first (window-child root))
1221 (first-state
1222 (and first (window-parameter first 'window-side)
1223 (window-state-get first)))
1224 (last (window-last-child root))
1225 (last-state
1226 (and last (window-parameter last 'window-side)
1227 (window-state-get last)))
1228 (dummy (get-buffer-create " *dummy*"))
1229 major)
1230 (unwind-protect
1231 (progn
1232 (when first-state (delete-window first))
1233 (when last-state (delete-window last))
1234 (when first-state
1235 (setq major (window--make-major-side-window
1236 dummy (if window-sides-vertical 'top 'left) 0))
1237 (window-state-put first-state major t))
1238 (when last-state
1239 (setq major (window--make-major-side-window
1240 dummy (if window-sides-vertical 'bottom 'right) 0))
1241 (window-state-put last-state major t)))
1242 (kill-buffer " *dummy*"))))))
1244 (defun window--sides-verticalize (symbol value)
1245 "Helper function for customizing `window-sides-vertical'."
1246 (set-default symbol value)
1247 (dolist (frame (frame-list))
1248 (window--sides-verticalize-frame frame)))
1250 (defun window--sides-check-failed (frame)
1251 "Helper function for `window--sides-check'."
1252 (catch 'failed
1253 ;; FRAME must have a main window.
1254 (unless (window-main-window frame)
1255 (error "Frame %s has no main window" frame)
1256 (throw 'failed t))
1257 ;; Now check the side windows.
1258 (dolist (side '(left top right bottom))
1259 (let ((window (window-with-parameter 'window-side side frame t)))
1260 (when window
1261 ;; If WINDOW is live there must be no other window on this frame
1262 ;; with the same `window-side' parameter.
1263 (if (window-live-p window)
1264 (walk-window-tree
1265 (lambda (this)
1266 (when (and (eq (window-parameter this 'window-side) side)
1267 (not (eq this window)))
1268 (error "Window %s has same side %s as window %s but no common parent"
1269 this side window)
1270 (throw 'failed t)))
1271 frame t 'nomini)
1272 (walk-window-tree
1273 (lambda (this)
1274 (if (eq (window-parent this) window)
1275 (unless (eq (window-parameter this 'window-side) side)
1276 (error "Window %s has not same side %s as its parent %s"
1277 this side window)
1278 (throw 'failed t))
1279 (when (and (eq (window-parameter this 'window-side) side)
1280 (not (eq this window)))
1281 (error "Window %s has same side %s as major side window %s but its parent is %s"
1282 this side window (window-parent this))
1283 (throw 'failed t))))
1284 frame t 'nomini)))))))
1286 (defun window--sides-check (frame)
1287 "Check side windows configuration of FRAME.
1288 In a valid side windows configuration there can be at most one
1289 internal side window on each side and all its children must be
1290 live and have the same `window-side' parameter and no other
1291 window with the same `window-side' parameter exists on FRAME. If
1292 there is no such internal window, there may be at most one window
1293 with this side's `window-side' parameter on FRAME.
1295 If the configuration is invalid, reset the `window-side'
1296 parameters of all windows on FRAME."
1297 (when (and (not window--sides-inhibit-check)
1298 (window-with-parameter 'window-side nil frame t)
1299 (window--sides-check-failed frame))
1300 ;; Reset all `window-side' parameters.
1301 (walk-window-tree
1302 (lambda (window)
1303 (set-window-parameter window 'window-side nil))
1304 frame t 'nomini)
1305 (message "Side windows configuration reset for frame %s" frame)))
1307 (defun window--check (&optional frame)
1308 "Check atomic and side windows on FRAME.
1309 FRAME defaults to the selected frame."
1310 (window--sides-check frame)
1311 (window--atom-check frame))
1313 ;; Dumping frame/window contents.
1314 (defun window--dump-window (&optional window erase)
1315 "Dump WINDOW to buffer *window-frame-dump*.
1316 WINDOW must be a valid window and defaults to the selected one.
1317 Optional argument ERASE non-nil means erase *window-frame-dump*
1318 before writing to it."
1319 (setq window (window-normalize-window window))
1320 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1321 (when erase (erase-buffer))
1322 (insert
1323 (format "%s parent: %s\n" window (window-parent window))
1324 (format "pixel left: %s top: %s size: %s x %s new: %s\n"
1325 (window-pixel-left window) (window-pixel-top window)
1326 (window-size window t t) (window-size window nil t)
1327 (window-new-pixel window))
1328 (format "char left: %s top: %s size: %s x %s new: %s\n"
1329 (window-left-column window) (window-top-line window)
1330 (window-total-size window t) (window-total-size window)
1331 (window-new-total window))
1332 (format "normal: %s x %s new: %s\n"
1333 (window-normal-size window t) (window-normal-size window)
1334 (window-new-normal window)))
1335 (when (window-live-p window)
1336 (let ((fringes (window-fringes window))
1337 (margins (window-margins window)))
1338 (insert
1339 (format "body pixel: %s x %s char: %s x %s\n"
1340 (window-body-width window t) (window-body-height window t)
1341 (window-body-width window) (window-body-height window))
1342 (format "width left fringe: %s left margin: %s right margin: %s\n"
1343 (car fringes) (or (car margins) 0) (or (cdr margins) 0))
1344 (format "width right fringe: %s scroll-bar: %s divider: %s\n"
1345 (cadr fringes)
1346 (window-scroll-bar-width window)
1347 (window-right-divider-width window))
1348 (format "height header-line: %s mode-line: %s divider: %s\n"
1349 (window-header-line-height window)
1350 (window-mode-line-height window)
1351 (window-bottom-divider-width window)))))
1352 (insert "\n")))
1354 (defun window--dump-frame (&optional window-or-frame)
1355 "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
1356 WINDOW-OR-FRAME can be a frame or a window and defaults to the
1357 selected frame. When WINDOW-OR-FRAME is a window, dump that
1358 window's frame. The buffer *window-frame-dump* is erased before
1359 dumping to it."
1360 (let* ((window
1361 (cond
1362 ((or (not window-or-frame)
1363 (frame-live-p window-or-frame))
1364 (frame-root-window window-or-frame))
1365 ((or (window-live-p window-or-frame)
1366 (window-child window-or-frame))
1367 window-or-frame)
1369 (frame-root-window))))
1370 (frame (window-frame window)))
1371 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1372 (erase-buffer)
1373 (insert
1374 (format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
1375 (frame-pixel-width frame) (frame-pixel-height frame)
1376 (frame-total-cols frame) (frame-total-lines frame)
1377 (frame-char-width frame) (frame-char-height frame))
1378 (format "frame text pixel: %s x %s cols/lines: %s x %s\n"
1379 (frame-text-width frame) (frame-text-height frame)
1380 (frame-text-cols frame) (frame-text-lines frame))
1381 (format "tool: %s scroll: %s/%s fringe: %s border: %s right: %s bottom: %s\n\n"
1382 (if (fboundp 'tool-bar-height)
1383 (tool-bar-height frame t)
1384 "0")
1385 (frame-scroll-bar-width frame)
1386 (frame-scroll-bar-height frame)
1387 (frame-fringe-width frame)
1388 (frame-border-width frame)
1389 (frame-right-divider-width frame)
1390 (frame-bottom-divider-width frame)))
1391 (walk-window-tree 'window--dump-window frame t t))))
1393 ;;; Window sizes.
1394 (defun window-total-size (&optional window horizontal round)
1395 "Return the total height or width of WINDOW.
1396 WINDOW must be a valid window and defaults to the selected one.
1398 If HORIZONTAL is omitted or nil, return the total height of
1399 WINDOW, in lines. If WINDOW is live, its total height includes,
1400 in addition to the height of WINDOW's text, the heights of
1401 WINDOW's mode and header line and a bottom divider, if any.
1403 If HORIZONTAL is non-nil, return the total width of WINDOW, in
1404 columns. If WINDOW is live, its total width includes, in
1405 addition to the width of WINDOW's text, the widths of WINDOW's
1406 fringes, margins, scroll bars and its right divider, if any.
1408 If WINDOW is internal, return the respective size of the screen
1409 areas spanned by its children.
1411 Optional argument ROUND is handled as for `window-total-height'
1412 and `window-total-width'."
1413 (if horizontal
1414 (window-total-width window round)
1415 (window-total-height window round)))
1417 (defun window-size (&optional window horizontal pixelwise round)
1418 "Return the height or width of WINDOW.
1419 WINDOW must be a valid window and defaults to the selected one.
1421 If HORIZONTAL is omitted or nil, return the total height of
1422 WINDOW, in lines, like `window-total-height'. Otherwise return
1423 the total width, in columns, like `window-total-width'.
1425 Optional argument PIXELWISE means return the pixel size of WINDOW
1426 like `window-pixel-height' and `window-pixel-width'.
1428 Optional argument ROUND is ignored if PIXELWISE is non-nil and
1429 handled as for `window-total-height' and `window-total-width'
1430 otherwise."
1431 (if horizontal
1432 (if pixelwise
1433 (window-pixel-width window)
1434 (window-total-width window round))
1435 (if pixelwise
1436 (window-pixel-height window)
1437 (window-total-height window round))))
1439 (defvar window-size-fixed nil
1440 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
1441 If the value is `height', then only the window's height is fixed.
1442 If the value is `width', then only the window's width is fixed.
1443 Any other non-nil value fixes both the width and the height.
1445 Emacs won't change the size of any window displaying that buffer,
1446 unless it has no other choice (like when deleting a neighboring
1447 window).")
1448 (make-variable-buffer-local 'window-size-fixed)
1450 (defun window--preservable-size (window &optional horizontal)
1451 "Return height of WINDOW as `window-preserve-size' would preserve it.
1452 Optional argument HORIZONTAL non-nil means to return the width of
1453 WINDOW as `window-preserve-size' would preserve it."
1454 (if horizontal
1455 (window-body-width window t)
1456 (+ (window-body-height window t)
1457 (window-header-line-height window)
1458 (window-mode-line-height window))))
1460 (defun window-preserve-size (&optional window horizontal preserve)
1461 "Preserve height of window WINDOW.
1462 WINDOW must be a live window and defaults to the selected one.
1463 Optional argument HORIZONTAL non-nil means preserve the width of
1464 WINDOW.
1466 PRESERVE t means to preserve the current height/width of WINDOW's
1467 body in frame and window resizing operations whenever possible.
1468 The height/width of WINDOW will change only if Emacs has no other
1469 choice. Resizing a window whose height/width is preserved never
1470 throws an error.
1472 PRESERVE nil means to stop preserving the height/width of WINDOW,
1473 lifting the respective restraint induced by a previous call of
1474 `window-preserve-size' for WINDOW. Calling `enlarge-window',
1475 `shrink-window', `split-window' or `fit-window-to-buffer' with
1476 WINDOW as argument also removes the respective restraint.
1478 Other values of PRESERVE are reserved for future use."
1479 (setq window (window-normalize-window window t))
1480 (let* ((parameter (window-parameter window 'window-preserved-size))
1481 (width (nth 1 parameter))
1482 (height (nth 2 parameter)))
1483 (if horizontal
1484 (set-window-parameter
1485 window 'window-preserved-size
1486 (list
1487 (window-buffer window)
1488 (and preserve (window--preservable-size window t))
1489 height))
1490 (set-window-parameter
1491 window 'window-preserved-size
1492 (list
1493 (window-buffer window)
1494 width
1495 (and preserve (window--preservable-size window)))))))
1497 (defun window-preserved-size (&optional window horizontal)
1498 "Return preserved height of window WINDOW.
1499 WINDOW must be a live window and defaults to the selected one.
1500 Optional argument HORIZONTAL non-nil means to return preserved
1501 width of WINDOW."
1502 (setq window (window-normalize-window window t))
1503 (let* ((parameter (window-parameter window 'window-preserved-size))
1504 (buffer (nth 0 parameter))
1505 (width (nth 1 parameter))
1506 (height (nth 2 parameter)))
1507 (when (eq buffer (window-buffer window))
1508 (if horizontal width height))))
1510 (defun window--preserve-size (window horizontal)
1511 "Return non-nil when the height of WINDOW shall be preserved.
1512 Optional argument HORIZONTAL non-nil means to return non-nil when
1513 the width of WINDOW shall be preserved."
1514 (let ((size (window-preserved-size window horizontal)))
1515 (and (numberp size)
1516 (= size (window--preservable-size window horizontal)))))
1518 (defun window-safe-min-size (&optional window horizontal pixelwise)
1519 "Return safe minimum size of WINDOW.
1520 WINDOW must be a valid window and defaults to the selected one.
1521 Optional argument HORIZONTAL non-nil means return the minimum
1522 number of columns of WINDOW; otherwise return the minimum number
1523 of WINDOW's lines.
1525 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1526 of WINDOW."
1527 (setq window (window-normalize-window window))
1528 (if pixelwise
1529 (if horizontal
1530 (* window-safe-min-width
1531 (frame-char-width (window-frame window)))
1532 (* window-safe-min-height
1533 (frame-char-height (window-frame window))))
1534 (if horizontal window-safe-min-width window-safe-min-height)))
1536 (defun window-min-size (&optional window horizontal ignore pixelwise)
1537 "Return the minimum size of WINDOW.
1538 WINDOW must be a valid window and defaults to the selected one.
1539 Optional argument HORIZONTAL non-nil means return the minimum
1540 number of columns of WINDOW; otherwise return the minimum number
1541 of WINDOW's lines.
1543 The optional argument IGNORE has the same meaning as for
1544 `window-resizable'. Optional argument PIXELWISE non-nil means
1545 return the minimum pixel-size of WINDOW."
1546 (window--min-size-1
1547 (window-normalize-window window) horizontal ignore pixelwise))
1549 (defun window--min-size-ignore-p (window ignore)
1550 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
1551 (if (window-valid-p ignore)
1552 (eq window ignore)
1553 (not (memq ignore '(nil preserved)))))
1555 (defun window--min-size-1 (window horizontal ignore pixelwise)
1556 "Internal function of `window-min-size'."
1557 (let ((sub (window-child window)))
1558 (if sub
1559 (let ((value 0))
1560 ;; WINDOW is an internal window.
1561 (if (window-combined-p sub horizontal)
1562 ;; The minimum size of an iso-combination is the sum of
1563 ;; the minimum sizes of its child windows.
1564 (while sub
1565 (setq value (+ value
1566 (window--min-size-1
1567 sub horizontal ignore pixelwise)))
1568 (setq sub (window-right sub)))
1569 ;; The minimum size of an ortho-combination is the maximum
1570 ;; of the minimum sizes of its child windows.
1571 (while sub
1572 (setq value (max value
1573 (window--min-size-1
1574 sub horizontal ignore pixelwise)))
1575 (setq sub (window-right sub))))
1576 value)
1577 (with-current-buffer (window-buffer window)
1578 (cond
1579 ((window-minibuffer-p window)
1580 (if pixelwise (frame-char-height (window-frame window)) 1))
1581 ((window-size-fixed-p window horizontal ignore)
1582 ;; The minimum size of a fixed size window is its size.
1583 (window-size window horizontal pixelwise))
1584 ((eq ignore 'safe)
1585 ;; If IGNORE equals `safe' return the safe value.
1586 (window-safe-min-size window horizontal pixelwise))
1587 (horizontal
1588 ;; For the minimum width of a window take fringes and
1589 ;; scroll-bars into account. This is questionable and should
1590 ;; be removed as soon as we are able to split (and resize)
1591 ;; windows such that the new (or resized) windows can get a
1592 ;; size less than the user-specified `window-min-height' and
1593 ;; `window-min-width'.
1594 (let* ((char-size (frame-char-size window t))
1595 (fringes (window-fringes window))
1596 (margins (window-margins window))
1597 ;; Let the 'min-margins' parameter override the actual
1598 ;; widths of the margins. We allow any number to
1599 ;; replace the values specified by `window-margins'.
1600 ;; See bug#24193 for the rationale of this parameter.
1601 (min-margins (window-parameter window 'min-margins))
1602 (left-min-margin (and min-margins
1603 (numberp (car min-margins))
1604 (car min-margins)))
1605 (right-min-margin (and min-margins
1606 (numberp (cdr min-margins))
1607 (cdr min-margins)))
1608 (pixel-width
1609 (+ (window-safe-min-size window t t)
1610 (* (or left-min-margin (car margins) 0) char-size)
1611 (* (or right-min-margin(cdr margins) 0) char-size)
1612 (car fringes) (cadr fringes)
1613 (window-scroll-bar-width window)
1614 (window-right-divider-width window))))
1615 (if pixelwise
1616 (max
1617 (if window-resize-pixelwise
1618 pixel-width
1619 ;; Round up to next integral of columns.
1620 (* (ceiling pixel-width char-size) char-size))
1621 (if (window--min-size-ignore-p window ignore)
1623 (window-min-pixel-width window)))
1624 (max
1625 (ceiling pixel-width char-size)
1626 (if (window--min-size-ignore-p window ignore)
1628 window-min-width)))))
1629 ((let ((char-size (frame-char-size window))
1630 (pixel-height
1631 (+ (window-safe-min-size window nil t)
1632 (window-header-line-height window)
1633 (window-scroll-bar-height window)
1634 (window-mode-line-height window)
1635 (window-bottom-divider-width window))))
1636 (if pixelwise
1637 (max
1638 (if window-resize-pixelwise
1639 pixel-height
1640 ;; Round up to next integral of lines.
1641 (* (ceiling pixel-height char-size) char-size))
1642 (if (window--min-size-ignore-p window ignore)
1644 (window-min-pixel-height window)))
1645 (max (ceiling pixel-height char-size)
1646 (if (window--min-size-ignore-p window ignore)
1648 window-min-height))))))))))
1650 (defun window-sizable (window delta &optional horizontal ignore pixelwise)
1651 "Return DELTA if DELTA lines can be added to WINDOW.
1652 WINDOW must be a valid window and defaults to the selected one.
1653 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1654 columns can be added to WINDOW. A return value of zero means
1655 that no lines (or columns) can be added to WINDOW.
1657 This function looks only at WINDOW and, recursively, its child
1658 windows. The function `window-resizable' looks at other windows
1659 as well.
1661 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1662 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1663 return the maximum value in the range 0..DELTA by which WINDOW
1664 can be enlarged.
1666 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1667 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1668 return the minimum value in the range DELTA..0 by which WINDOW
1669 can be shrunk.
1671 The optional argument IGNORE has the same meaning as for
1672 `window-resizable'. Optional argument PIXELWISE non-nil means
1673 interpret DELTA as pixels."
1674 (setq window (window-normalize-window window))
1675 (cond
1676 ((< delta 0)
1677 (max (- (window-min-size window horizontal ignore pixelwise)
1678 (window-size window horizontal pixelwise))
1679 delta))
1680 ((> delta 0)
1681 (if (window-size-fixed-p window horizontal ignore)
1683 delta))
1684 (t 0)))
1686 (defun window-sizable-p (window delta &optional horizontal ignore pixelwise)
1687 "Return t if WINDOW can be resized by DELTA lines.
1688 WINDOW must be a valid window and defaults to the selected one.
1689 For the meaning of the arguments of this function see the
1690 doc-string of `window-sizable'."
1691 (setq window (window-normalize-window window))
1692 (if (> delta 0)
1693 (>= (window-sizable window delta horizontal ignore pixelwise)
1694 delta)
1695 (<= (window-sizable window delta horizontal ignore pixelwise)
1696 delta)))
1698 (defun window--size-fixed-1 (window horizontal ignore)
1699 "Internal function for `window-size-fixed-p'."
1700 (let ((sub (window-child window)))
1701 (catch 'fixed
1702 (if sub
1703 ;; WINDOW is an internal window.
1704 (if (window-combined-p sub horizontal)
1705 ;; An iso-combination is fixed size if all its child
1706 ;; windows are fixed-size.
1707 (progn
1708 (while sub
1709 (unless (window--size-fixed-1 sub horizontal ignore)
1710 ;; We found a non-fixed-size child window, so
1711 ;; WINDOW's size is not fixed.
1712 (throw 'fixed nil))
1713 (setq sub (window-right sub)))
1714 ;; All child windows are fixed-size, so WINDOW's size is
1715 ;; fixed.
1716 (throw 'fixed t))
1717 ;; An ortho-combination is fixed-size if at least one of its
1718 ;; child windows is fixed-size.
1719 (while sub
1720 (when (window--size-fixed-1 sub horizontal ignore)
1721 ;; We found a fixed-size child window, so WINDOW's size
1722 ;; is fixed.
1723 (throw 'fixed t))
1724 (setq sub (window-right sub))))
1725 ;; WINDOW is a live window.
1726 (and (or (not (windowp ignore)) (not (eq window ignore)))
1727 (or (and (not (eq ignore 'preserved))
1728 (window--preserve-size window horizontal))
1729 (with-current-buffer (window-buffer window)
1730 (if horizontal
1731 (memq window-size-fixed '(width t))
1732 (memq window-size-fixed '(height t))))))))))
1734 (defun window-size-fixed-p (&optional window horizontal ignore)
1735 "Return non-nil if WINDOW's height is fixed.
1736 WINDOW must be a valid window and defaults to the selected one.
1737 Optional argument HORIZONTAL non-nil means return non-nil if
1738 WINDOW's width is fixed. The optional argument IGNORE has the
1739 same meaning as for `window-resizable'.
1741 If this function returns nil, this does not necessarily mean that
1742 WINDOW can be resized in the desired direction. The function
1743 `window-resizable' can tell that."
1744 (when (or (windowp ignore) (memq ignore '(nil preserved)))
1745 (window--size-fixed-1
1746 (window-normalize-window window) horizontal ignore)))
1748 (defun window--min-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
1749 "Internal function for `window-min-delta'."
1750 (if (not (window-parent window))
1751 ;; If we can't go up, return zero.
1753 ;; Else try to find a non-fixed-size sibling of WINDOW.
1754 (let* ((parent (window-parent window))
1755 (sub (window-child parent)))
1756 (catch 'done
1757 (if (window-combined-p sub horizontal)
1758 ;; In an iso-combination throw DELTA if we find at least one
1759 ;; child window and that window is either not fixed-size or
1760 ;; we can ignore fixed-sizeness.
1761 (let ((skip (eq trail 'after)))
1762 (while sub
1763 (cond
1764 ((eq sub window)
1765 (setq skip (eq trail 'before)))
1766 (skip)
1767 ((window-size-fixed-p sub horizontal ignore))
1769 ;; We found a non-fixed-size child window.
1770 (throw 'done delta)))
1771 (setq sub (window-right sub))))
1772 ;; In an ortho-combination set DELTA to the minimum value by
1773 ;; which other child windows can shrink.
1774 (while sub
1775 (unless (eq sub window)
1776 (setq delta
1777 (min delta
1778 (max (- (window-size sub horizontal pixelwise 'ceiling)
1779 (window-min-size
1780 sub horizontal ignore pixelwise))
1781 0))))
1782 (setq sub (window-right sub))))
1783 (if noup
1784 delta
1785 (window--min-delta-1
1786 parent delta horizontal ignore trail nil pixelwise))))))
1788 (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise)
1789 "Return number of lines by which WINDOW can be shrunk.
1790 WINDOW must be a valid window and defaults to the selected one.
1791 Return zero if WINDOW cannot be shrunk.
1793 Optional argument HORIZONTAL non-nil means return number of
1794 columns by which WINDOW can be shrunk.
1796 The optional argument IGNORE has the same meaning as for
1797 `window-resizable'. Optional argument TRAIL restricts the
1798 windows that can be enlarged. If its value is `before', only
1799 windows to the left of or above WINDOW can be enlarged. If it is
1800 `after', only windows to the right of or below WINDOW can be
1801 enlarged.
1803 Optional argument NOUP non-nil means don't go up in the window
1804 tree, but try to enlarge windows within WINDOW's combination
1805 only. Optional argument NODOWN non-nil means don't check whether
1806 WINDOW itself (and its child windows) can be shrunk; check only
1807 whether at least one other window can be enlarged appropriately.
1809 Optional argument PIXELWISE non-nil means return number of pixels
1810 by which WINDOW can be shrunk."
1811 (setq window (window-normalize-window window))
1812 (let ((size (window-size window horizontal pixelwise 'floor))
1813 (minimum (window-min-size window horizontal ignore pixelwise)))
1814 (cond
1815 (nodown
1816 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1817 (window--min-delta-1
1818 window size horizontal ignore trail noup pixelwise))
1819 ((<= size minimum)
1820 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1821 ;; there's nothing to recover.
1824 ;; Otherwise, try to recover whatever WINDOW is larger than its
1825 ;; minimum size.
1826 (window--min-delta-1
1827 window (- size minimum) horizontal ignore trail noup pixelwise)))))
1829 (defun frame-windows-min-size (&optional frame horizontal ignore pixelwise)
1830 "Return minimum number of lines of FRAME's windows.
1831 HORIZONTAL non-nil means return number of columns of FRAME's
1832 windows. The optional argument IGNORE has the same meaning as
1833 for `window-resizable'. PIXELWISE non-nil means return sizes in
1834 pixels."
1835 (setq frame (window-normalize-frame frame))
1836 (let* ((root (frame-root-window frame))
1837 (mini (window-next-sibling root)))
1838 (+ (window-min-size root horizontal ignore pixelwise)
1839 (if (and mini (not horizontal))
1840 (window-min-size mini horizontal nil pixelwise)
1841 0))))
1843 (defun window--max-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
1844 "Internal function of `window-max-delta'."
1845 (if (not (window-parent window))
1846 ;; Can't go up. Return DELTA.
1847 delta
1848 (let* ((parent (window-parent window))
1849 (sub (window-child parent)))
1850 (catch 'fixed
1851 (if (window-combined-p sub horizontal)
1852 ;; For an iso-combination calculate how much we can get from
1853 ;; other child windows.
1854 (let ((skip (eq trail 'after)))
1855 (while sub
1856 (cond
1857 ((eq sub window)
1858 (setq skip (eq trail 'before)))
1859 (skip)
1861 (setq delta
1862 (+ delta
1863 (max
1864 (- (window-size sub horizontal pixelwise 'floor)
1865 (window-min-size
1866 sub horizontal ignore pixelwise))
1867 0)))))
1868 (setq sub (window-right sub))))
1869 ;; For an ortho-combination throw DELTA when at least one
1870 ;; child window is fixed-size.
1871 (while sub
1872 (when (and (not (eq sub window))
1873 (window-size-fixed-p sub horizontal ignore))
1874 (throw 'fixed delta))
1875 (setq sub (window-right sub))))
1876 (if noup
1877 ;; When NOUP is nil, DELTA is all we can get.
1878 delta
1879 ;; Else try with parent of WINDOW, passing the DELTA we
1880 ;; recovered so far.
1881 (window--max-delta-1
1882 parent delta horizontal ignore trail nil pixelwise))))))
1884 (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise)
1885 "Return maximum number of lines by which WINDOW can be enlarged.
1886 WINDOW must be a valid window and defaults to the selected one.
1887 The return value is zero if WINDOW cannot be enlarged.
1889 Optional argument HORIZONTAL non-nil means return maximum number
1890 of columns by which WINDOW can be enlarged.
1892 The optional argument IGNORE has the same meaning as for
1893 `window-resizable'. Optional argument TRAIL restricts the
1894 windows that can be enlarged. If its value is `before', only
1895 windows to the left of or above WINDOW can be enlarged. If it is
1896 `after', only windows to the right of or below WINDOW can be
1897 enlarged.
1899 Optional argument NOUP non-nil means don't go up in the window
1900 tree but try to obtain the entire space from windows within
1901 WINDOW's combination. Optional argument NODOWN non-nil means do
1902 not check whether WINDOW itself (and its child windows) can be
1903 enlarged; check only whether other windows can be shrunk
1904 appropriately.
1906 Optional argument PIXELWISE non-nil means return number of
1907 pixels by which WINDOW can be enlarged."
1908 (setq window (window-normalize-window window))
1909 (if (and (not nodown) (window-size-fixed-p window horizontal ignore))
1910 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1911 ;; size.
1913 ;; WINDOW has no fixed size.
1914 (window--max-delta-1 window 0 horizontal ignore trail noup pixelwise)))
1916 ;; Make NOUP also inhibit the min-size check.
1917 (defun window--resizable (window delta &optional horizontal ignore trail noup nodown pixelwise)
1918 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1919 WINDOW must be a valid window and defaults to the selected one.
1920 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1921 can be resized horizontally by DELTA columns. A return value of
1922 zero means that WINDOW is not resizable.
1924 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1925 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1926 return the maximum value in the range 0..DELTA by which WINDOW
1927 can be enlarged.
1929 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1930 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1931 return the minimum value in the range DELTA..0 that can be used
1932 for shrinking WINDOW.
1934 The optional argument IGNORE has the same meaning as for
1935 `window-resizable'. Optional argument TRAIL `before' means only
1936 windows to the left of or below WINDOW can be shrunk. Optional
1937 argument TRAIL `after' means only windows to the right of or
1938 above WINDOW can be shrunk.
1940 Optional argument NOUP non-nil means don't go up in the window
1941 tree but check only whether space can be obtained from (or given
1942 to) WINDOW's siblings. Optional argument NODOWN non-nil means
1943 don't go down in the window tree. This means do not check
1944 whether resizing would violate size restrictions of WINDOW or its
1945 child windows.
1947 Optional argument PIXELWISE non-nil means interpret DELTA as
1948 number of pixels."
1949 (setq window (window-normalize-window window))
1950 (cond
1951 ((< delta 0)
1952 (max (- (window-min-delta
1953 window horizontal ignore trail noup nodown pixelwise))
1954 delta))
1955 ((> delta 0)
1956 (min (window-max-delta
1957 window horizontal ignore trail noup nodown pixelwise)
1958 delta))
1959 (t 0)))
1961 (defun window--resizable-p (window delta &optional horizontal ignore trail noup nodown pixelwise)
1962 "Return t if WINDOW can be resized vertically by DELTA lines.
1963 WINDOW must be a valid window and defaults to the selected one.
1964 For the meaning of the arguments of this function see the
1965 doc-string of `window--resizable'.
1967 Optional argument PIXELWISE non-nil means interpret DELTA as
1968 pixels."
1969 (setq window (window-normalize-window window))
1970 (if (> delta 0)
1971 (>= (window--resizable
1972 window delta horizontal ignore trail noup nodown pixelwise)
1973 delta)
1974 (<= (window--resizable
1975 window delta horizontal ignore trail noup nodown pixelwise)
1976 delta)))
1978 (defun window-resizable (window delta &optional horizontal ignore pixelwise)
1979 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1980 WINDOW must be a valid window and defaults to the selected one.
1981 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1982 can be resized horizontally by DELTA columns. A return value of
1983 zero means that WINDOW is not resizable.
1985 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1986 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1987 return the maximum value in the range 0..DELTA by which WINDOW
1988 can be enlarged.
1990 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1991 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1992 return the minimum value in the range DELTA..0 that can be used
1993 for shrinking WINDOW.
1995 Optional argument IGNORE, if non-nil, means to ignore restraints
1996 induced by fixed size windows or the values of the variables
1997 `window-min-height' and `window-min-width'. The following values
1998 have special meanings: `safe' means that in addition live windows
1999 are allowed to get as small as `window-safe-min-height' lines and
2000 `window-safe-min-width' columns. `preserved' means to ignore
2001 only restrictions induced by `window-preserve-size'. If IGNORE
2002 is a window, then ignore restrictions for that window only.
2004 Optional argument PIXELWISE non-nil means interpret DELTA as
2005 pixels."
2006 (setq window (window-normalize-window window))
2007 (window--resizable window delta horizontal ignore nil nil nil pixelwise))
2009 (defun window-resizable-p (window delta &optional horizontal ignore pixelwise)
2010 "Return t if WINDOW can be resized vertically by DELTA lines.
2011 WINDOW must be a valid window and defaults to the selected one.
2012 For the meaning of the arguments of this function see the
2013 doc-string of `window-resizable'."
2014 (setq window (window-normalize-window window))
2015 (if (> delta 0)
2016 (>= (window--resizable
2017 window delta horizontal ignore nil nil nil pixelwise)
2018 delta)
2019 (<= (window--resizable
2020 window delta horizontal ignore nil nil nil pixelwise)
2021 delta)))
2023 ;; Aliases of functions defined in window.c.
2024 (defalias 'window-height 'window-total-height)
2025 (defalias 'window-width 'window-body-width)
2027 (defun window-full-height-p (&optional window)
2028 "Return t if WINDOW is as high as its containing frame.
2029 More precisely, return t if and only if the total height of
2030 WINDOW equals the total height of the root window of WINDOW's
2031 frame. WINDOW must be a valid window and defaults to the
2032 selected one."
2033 (setq window (window-normalize-window window))
2034 (if (window-minibuffer-p window)
2035 (eq window (frame-root-window (window-frame window)))
2036 (= (window-pixel-height window)
2037 (window-pixel-height (frame-root-window window)))))
2039 (defun window-full-width-p (&optional window)
2040 "Return t if WINDOW is as wide as its containing frame.
2041 More precisely, return t if and only if the total width of WINDOW
2042 equals the total width of the root window of WINDOW's frame.
2043 WINDOW must be a valid window and defaults to the selected one."
2044 (setq window (window-normalize-window window))
2045 (= (window-pixel-width window)
2046 (window-pixel-width (frame-root-window window))))
2048 (defun window-body-size (&optional window horizontal pixelwise)
2049 "Return the height or width of WINDOW's text area.
2050 WINDOW must be a live window and defaults to the selected one.
2052 If HORIZONTAL is omitted or nil, return the height of the text
2053 area, like `window-body-height'. Otherwise, return the width of
2054 the text area, like `window-body-width'. In either case, the
2055 optional argument PIXELWISE is passed to the functions."
2056 (if horizontal
2057 (window-body-width window pixelwise)
2058 (window-body-height window pixelwise)))
2060 (declare-function font-info "font.c" (name &optional frame))
2062 (defun window-font-width (&optional window face)
2063 "Return average character width for the font of FACE used in WINDOW.
2064 WINDOW must be a live window and defaults to the selected one.
2066 If FACE is nil or omitted, the default face is used. If FACE is
2067 remapped (see `face-remapping-alist'), the function returns the
2068 information for the remapped face."
2069 (with-selected-window (window-normalize-window window t)
2070 (if (display-multi-font-p)
2071 (let* ((face (if face face 'default))
2072 (info (font-info (face-font face)))
2073 (width (aref info 11)))
2074 (if (> width 0)
2075 width
2076 (aref info 10)))
2077 (frame-char-width))))
2079 (defun window-font-height (&optional window face)
2080 "Return character height for the font of FACE used in WINDOW.
2081 WINDOW must be a live window and defaults to the selected one.
2083 If FACE is nil or omitted, the default face is used. If FACE is
2084 remapped (see `face-remapping-alist'), the function returns the
2085 information for the remapped face."
2086 (with-selected-window (window-normalize-window window t)
2087 (if (display-multi-font-p)
2088 (let* ((face (if face face 'default))
2089 (info (font-info (face-font face))))
2090 (aref info 3))
2091 (frame-char-height))))
2093 (defvar overflow-newline-into-fringe)
2095 (defun window-max-chars-per-line (&optional window face)
2096 "Return the number of characters that can be displayed on one line in WINDOW.
2097 WINDOW must be a live window and defaults to the selected one.
2099 The character width of FACE is used for the calculation. If FACE
2100 is nil or omitted, the default face is used. If FACE is
2101 remapped (see `face-remapping-alist'), the function uses the
2102 remapped face.
2104 This function is different from `window-body-width' in two
2105 ways. First, it accounts for the portions of the line reserved
2106 for the continuation glyph. Second, it accounts for the size of
2107 the font."
2108 (with-selected-window (window-normalize-window window t)
2109 (let* ((window-width (window-body-width window t))
2110 (font-width (window-font-width window face))
2111 (ncols (/ window-width font-width)))
2112 (if (and (display-graphic-p)
2113 overflow-newline-into-fringe
2114 (not
2115 (or (eq left-fringe-width 0)
2116 (and (null left-fringe-width)
2117 (= (frame-parameter nil 'left-fringe) 0))))
2118 (not
2119 (or (eq right-fringe-width 0)
2120 (and (null right-fringe-width)
2121 (= (frame-parameter nil 'right-fringe) 0)))))
2122 ncols
2123 ;; FIXME: This should remove 1 more column when there are no
2124 ;; fringes, lines are truncated, and the window is hscrolled,
2125 ;; but EOL is not in the view, because then there are 2
2126 ;; truncation glyphs, not one.
2127 (1- ncols)))))
2129 (defun window-current-scroll-bars (&optional window)
2130 "Return the current scroll bar types for WINDOW.
2131 WINDOW must be a live window and defaults to the selected one.
2133 The return value is a cons cell (VERTICAL . HORIZONTAL) where
2134 VERTICAL specifies the current location of the vertical scroll
2135 bar (`left', `right' or nil), and HORIZONTAL specifies the
2136 current location of the horizontal scroll bar (`bottom' or nil).
2138 Unlike `window-scroll-bars', this function reports the scroll bar
2139 type actually used, once frame defaults and `scroll-bar-mode' are
2140 taken into account."
2141 (setq window (window-normalize-window window t))
2142 (let ((vertical (nth 2 (window-scroll-bars window)))
2143 (horizontal (nth 5 (window-scroll-bars window)))
2144 (inherited (frame-current-scroll-bars (window-frame window))))
2145 (when (eq vertical t)
2146 (setq vertical (car inherited)))
2147 (when (eq horizontal t)
2148 (setq horizontal (cdr inherited)))
2149 (cons vertical (and horizontal 'bottom))))
2151 (defun walk-windows (fun &optional minibuf all-frames)
2152 "Cycle through all live windows, calling FUN for each one.
2153 FUN must specify a function with a window as its sole argument.
2154 The optional arguments MINIBUF and ALL-FRAMES specify the set of
2155 windows to include in the walk.
2157 MINIBUF t means include the minibuffer window even if the
2158 minibuffer is not active. MINIBUF nil or omitted means include
2159 the minibuffer window only if the minibuffer is active. Any
2160 other value means do not include the minibuffer window even if
2161 the minibuffer is active.
2163 ALL-FRAMES nil or omitted means consider all windows on the
2164 selected frame, plus the minibuffer window if specified by the
2165 MINIBUF argument. If the minibuffer counts, consider all windows
2166 on all frames that share that minibuffer too. The following
2167 non-nil values of ALL-FRAMES have special meanings:
2169 - t means consider all windows on all existing frames.
2171 - `visible' means consider all windows on all visible frames on
2172 the current terminal.
2174 - 0 (the number zero) means consider all windows on all visible
2175 and iconified frames on the current terminal.
2177 - A frame means consider all windows on that frame only.
2179 Anything else means consider all windows on the selected frame
2180 and no others.
2182 This function changes neither the order of recently selected
2183 windows nor the buffer list."
2184 ;; If we start from the minibuffer window, don't fail to come
2185 ;; back to it.
2186 (when (window-minibuffer-p)
2187 (setq minibuf t))
2188 ;; Make sure to not mess up the order of recently selected
2189 ;; windows. Use `save-selected-window' and `select-window'
2190 ;; with second argument non-nil for this purpose.
2191 (save-selected-window
2192 (when (framep all-frames)
2193 (select-window (frame-first-window all-frames) 'norecord))
2194 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
2195 (funcall fun walk-windows-window))))
2197 (defun window-at-side-p (&optional window side)
2198 "Return t if WINDOW is at SIDE of its containing frame.
2199 WINDOW must be a valid window and defaults to the selected one.
2200 SIDE can be any of the symbols `left', `top', `right' or
2201 `bottom'. The default value nil is handled like `bottom'."
2202 (setq window (window-normalize-window window))
2203 (let ((edge
2204 (cond
2205 ((eq side 'left) 0)
2206 ((eq side 'top) 1)
2207 ((eq side 'right) 2)
2208 ((memq side '(bottom nil)) 3))))
2209 (= (nth edge (window-pixel-edges window))
2210 (nth edge (window-pixel-edges (frame-root-window window))))))
2212 (defun window-at-side-list (&optional frame side)
2213 "Return list of all windows on SIDE of FRAME.
2214 FRAME must be a live frame and defaults to the selected frame.
2215 SIDE can be any of the symbols `left', `top', `right' or
2216 `bottom'. The default value nil is handled like `bottom'."
2217 (setq frame (window-normalize-frame frame))
2218 (let (windows)
2219 (walk-window-tree
2220 (lambda (window)
2221 (when (window-at-side-p window side)
2222 (setq windows (cons window windows))))
2223 frame nil 'nomini)
2224 (nreverse windows)))
2226 (defun window--in-direction-2 (window posn &optional horizontal)
2227 "Support function for `window-in-direction'."
2228 (if horizontal
2229 (let ((top (window-pixel-top window)))
2230 (if (> top posn)
2231 (- top posn)
2232 (- posn top (window-pixel-height window))))
2233 (let ((left (window-pixel-left window)))
2234 (if (> left posn)
2235 (- left posn)
2236 (- posn left (window-pixel-width window))))))
2238 ;; Predecessors to the below have been devised by Julian Assange in
2239 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
2240 ;; Neither of these allow one to selectively ignore specific windows
2241 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
2242 ;; the movement.
2243 (defun window-in-direction (direction &optional window ignore sign wrap mini)
2244 "Return window in DIRECTION as seen from WINDOW.
2245 More precisely, return the nearest window in direction DIRECTION
2246 as seen from the position of `window-point' in window WINDOW.
2247 DIRECTION must be one of `above', `below', `left' or `right'.
2248 WINDOW must be a live window and defaults to the selected one.
2250 Do not return a window whose `no-other-window' parameter is
2251 non-nil. If the nearest window's `no-other-window' parameter is
2252 non-nil, try to find another window in the indicated direction.
2253 If, however, the optional argument IGNORE is non-nil, return that
2254 window even if its `no-other-window' parameter is non-nil.
2256 Optional argument SIGN a negative number means to use the right
2257 or bottom edge of WINDOW as reference position instead of
2258 `window-point'. SIGN a positive number means to use the left or
2259 top edge of WINDOW as reference position.
2261 Optional argument WRAP non-nil means to wrap DIRECTION around
2262 frame borders. This means to return for WINDOW at the top of the
2263 frame and DIRECTION `above' the minibuffer window if the frame
2264 has one, and a window at the bottom of the frame otherwise.
2266 Optional argument MINI nil means to return the minibuffer window
2267 if and only if it is currently active. MINI non-nil means to
2268 return the minibuffer window even when it's not active. However,
2269 if WRAP is non-nil, always act as if MINI were nil.
2271 Return nil if no suitable window can be found."
2272 (setq window (window-normalize-window window t))
2273 (unless (memq direction '(above below left right))
2274 (error "Wrong direction %s" direction))
2275 (let* ((frame (window-frame window))
2276 (hor (memq direction '(left right)))
2277 (first (if hor
2278 (window-pixel-left window)
2279 (window-pixel-top window)))
2280 (last (+ first (window-size window hor t)))
2281 ;; The column / row value of `posn-at-point' can be nil for the
2282 ;; mini-window, guard against that.
2283 (posn
2284 (cond
2285 ((and (numberp sign) (< sign 0))
2286 (if hor
2287 (1- (+ (window-pixel-top window) (window-pixel-height window)))
2288 (1- (+ (window-pixel-left window) (window-pixel-width window)))))
2289 ((and (numberp sign) (> sign 0))
2290 (if hor
2291 (window-pixel-top window)
2292 (window-pixel-left window)))
2293 ((let ((posn-cons (nth 2 (posn-at-point (window-point window) window))))
2294 (if hor
2295 (+ (or (cdr posn-cons) 1) (window-pixel-top window))
2296 (+ (or (car posn-cons) 1) (window-pixel-left window)))))))
2297 (best-edge
2298 (cond
2299 ((eq direction 'below) (frame-pixel-height frame))
2300 ((eq direction 'right) (frame-pixel-width frame))
2301 (t -1)))
2302 (best-edge-2 best-edge)
2303 (best-diff-2 (if hor (frame-pixel-height frame) (frame-pixel-width frame)))
2304 best best-2 best-diff-2-new)
2305 (walk-window-tree
2306 (lambda (w)
2307 (let* ((w-top (window-pixel-top w))
2308 (w-left (window-pixel-left w)))
2309 (cond
2310 ((or (eq window w)
2311 ;; Ignore ourselves.
2312 (and (window-parameter w 'no-other-window)
2313 ;; Ignore W unless IGNORE is non-nil.
2314 (not ignore))))
2315 (hor
2316 (cond
2317 ((and (<= w-top posn)
2318 (< posn (+ w-top (window-pixel-height w))))
2319 ;; W is to the left or right of WINDOW and covers POSN.
2320 (when (or (and (eq direction 'left)
2321 (or (and (<= w-left first) (> w-left best-edge))
2322 (and wrap
2323 (window-at-side-p window 'left)
2324 (window-at-side-p w 'right))))
2325 (and (eq direction 'right)
2326 (or (and (>= w-left last) (< w-left best-edge))
2327 (and wrap
2328 (window-at-side-p window 'right)
2329 (window-at-side-p w 'left)))))
2330 (setq best-edge w-left)
2331 (setq best w)))
2332 ((and (or (and (eq direction 'left)
2333 (<= (+ w-left (window-pixel-width w)) first))
2334 (and (eq direction 'right) (<= last w-left)))
2335 ;; W is to the left or right of WINDOW but does not
2336 ;; cover POSN.
2337 (setq best-diff-2-new
2338 (window--in-direction-2 w posn hor))
2339 (or (< best-diff-2-new best-diff-2)
2340 (and (= best-diff-2-new best-diff-2)
2341 (if (eq direction 'left)
2342 (> w-left best-edge-2)
2343 (< w-left best-edge-2)))))
2344 (setq best-edge-2 w-left)
2345 (setq best-diff-2 best-diff-2-new)
2346 (setq best-2 w))))
2347 ((and (<= w-left posn)
2348 (< posn (+ w-left (window-pixel-width w))))
2349 ;; W is above or below WINDOW and covers POSN.
2350 (when (or (and (eq direction 'above)
2351 (or (and (<= w-top first) (> w-top best-edge))
2352 (and wrap
2353 (window-at-side-p window 'top)
2354 (if (active-minibuffer-window)
2355 (minibuffer-window-active-p w)
2356 (window-at-side-p w 'bottom)))))
2357 (and (eq direction 'below)
2358 (or (and (>= w-top first) (< w-top best-edge))
2359 (and wrap
2360 (if (active-minibuffer-window)
2361 (minibuffer-window-active-p window)
2362 (window-at-side-p window 'bottom))
2363 (window-at-side-p w 'top)))))
2364 (setq best-edge w-top)
2365 (setq best w)))
2366 ((and (or (and (eq direction 'above)
2367 (<= (+ w-top (window-pixel-height w)) first))
2368 (and (eq direction 'below) (<= last w-top)))
2369 ;; W is above or below WINDOW but does not cover POSN.
2370 (setq best-diff-2-new
2371 (window--in-direction-2 w posn hor))
2372 (or (< best-diff-2-new best-diff-2)
2373 (and (= best-diff-2-new best-diff-2)
2374 (if (eq direction 'above)
2375 (> w-top best-edge-2)
2376 (< w-top best-edge-2)))))
2377 (setq best-edge-2 w-top)
2378 (setq best-diff-2 best-diff-2-new)
2379 (setq best-2 w)))))
2380 frame nil (and mini t))
2381 (or best best-2)))
2383 (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
2384 "Return a live window satisfying PREDICATE.
2385 More precisely, cycle through all windows calling the function
2386 PREDICATE on each one of them with the window as its sole
2387 argument. Return the first window for which PREDICATE returns
2388 non-nil. Windows are scanned starting with the window following
2389 the selected window. If no window satisfies PREDICATE, return
2390 DEFAULT.
2392 MINIBUF t means include the minibuffer window even if the
2393 minibuffer is not active. MINIBUF nil or omitted means include
2394 the minibuffer window only if the minibuffer is active. Any
2395 other value means do not include the minibuffer window even if
2396 the minibuffer is active.
2398 ALL-FRAMES nil or omitted means consider all windows on the selected
2399 frame, plus the minibuffer window if specified by the MINIBUF
2400 argument. If the minibuffer counts, consider all windows on all
2401 frames that share that minibuffer too. The following non-nil
2402 values of ALL-FRAMES have special meanings:
2404 - t means consider all windows on all existing frames.
2406 - `visible' means consider all windows on all visible frames on
2407 the current terminal.
2409 - 0 (the number zero) means consider all windows on all visible
2410 and iconified frames on the current terminal.
2412 - A frame means consider all windows on that frame only.
2414 Anything else means consider all windows on the selected frame
2415 and no others."
2416 (catch 'found
2417 (dolist (window (window-list-1
2418 (next-window nil minibuf all-frames)
2419 minibuf all-frames))
2420 (when (funcall predicate window)
2421 (throw 'found window)))
2422 default))
2424 (defalias 'some-window 'get-window-with-predicate)
2426 (defun get-lru-window (&optional all-frames dedicated not-selected)
2427 "Return the least recently used window on frames specified by ALL-FRAMES.
2428 Return a full-width window if possible. A minibuffer window is
2429 never a candidate. A dedicated window is never a candidate
2430 unless DEDICATED is non-nil, so if all windows are dedicated, the
2431 value is nil. Avoid returning the selected window if possible.
2432 Optional argument NOT-SELECTED non-nil means never return the
2433 selected window.
2435 The following non-nil values of the optional argument ALL-FRAMES
2436 have special meanings:
2438 - t means consider all windows on all existing frames.
2440 - `visible' means consider all windows on all visible frames on
2441 the current terminal.
2443 - 0 (the number zero) means consider all windows on all visible
2444 and iconified frames on the current terminal.
2446 - A frame means consider all windows on that frame only.
2448 Any other value of ALL-FRAMES means consider all windows on the
2449 selected frame and no others."
2450 (let (best-window best-time second-best-window second-best-time time)
2451 (dolist (window (window-list-1 nil 'nomini all-frames))
2452 (when (and (or dedicated (not (window-dedicated-p window)))
2453 (or (not not-selected) (not (eq window (selected-window)))))
2454 (setq time (window-use-time window))
2455 (if (or (eq window (selected-window))
2456 (not (window-full-width-p window)))
2457 (when (or (not second-best-time) (< time second-best-time))
2458 (setq second-best-time time)
2459 (setq second-best-window window))
2460 (when (or (not best-time) (< time best-time))
2461 (setq best-time time)
2462 (setq best-window window)))))
2463 (or best-window second-best-window)))
2465 (defun get-mru-window (&optional all-frames dedicated not-selected)
2466 "Return the most recently used window on frames specified by ALL-FRAMES.
2467 A minibuffer window is never a candidate. A dedicated window is
2468 never a candidate unless DEDICATED is non-nil, so if all windows
2469 are dedicated, the value is nil. Optional argument NOT-SELECTED
2470 non-nil means never return the selected window.
2472 The following non-nil values of the optional argument ALL-FRAMES
2473 have special meanings:
2475 - t means consider all windows on all existing frames.
2477 - `visible' means consider all windows on all visible frames on
2478 the current terminal.
2480 - 0 (the number zero) means consider all windows on all visible
2481 and iconified frames on the current terminal.
2483 - A frame means consider all windows on that frame only.
2485 Any other value of ALL-FRAMES means consider all windows on the
2486 selected frame and no others."
2487 (let (best-window best-time time)
2488 (dolist (window (window-list-1 nil 'nomini all-frames))
2489 (setq time (window-use-time window))
2490 (when (and (or dedicated (not (window-dedicated-p window)))
2491 (or (not not-selected) (not (eq window (selected-window))))
2492 (or (not best-time) (> time best-time)))
2493 (setq best-time time)
2494 (setq best-window window)))
2495 best-window))
2497 (defun get-largest-window (&optional all-frames dedicated not-selected)
2498 "Return the largest window on frames specified by ALL-FRAMES.
2499 A minibuffer window is never a candidate. A dedicated window is
2500 never a candidate unless DEDICATED is non-nil, so if all windows
2501 are dedicated, the value is nil. Optional argument NOT-SELECTED
2502 non-nil means never return the selected window.
2504 The following non-nil values of the optional argument ALL-FRAMES
2505 have special meanings:
2507 - t means consider all windows on all existing frames.
2509 - `visible' means consider all windows on all visible frames on
2510 the current terminal.
2512 - 0 (the number zero) means consider all windows on all visible
2513 and iconified frames on the current terminal.
2515 - A frame means consider all windows on that frame only.
2517 Any other value of ALL-FRAMES means consider all windows on the
2518 selected frame and no others."
2519 (let ((best-size 0)
2520 best-window size)
2521 (dolist (window (window-list-1 nil 'nomini all-frames))
2522 (when (and (or dedicated (not (window-dedicated-p window)))
2523 (or (not not-selected) (not (eq window (selected-window)))))
2524 (setq size (* (window-pixel-height window)
2525 (window-pixel-width window)))
2526 (when (> size best-size)
2527 (setq best-size size)
2528 (setq best-window window))))
2529 best-window))
2531 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
2532 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2533 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2534 and defaults to the current buffer. If the selected window displays
2535 BUFFER-OR-NAME, it will be the first in the resulting list.
2537 MINIBUF t means include the minibuffer window even if the
2538 minibuffer is not active. MINIBUF nil or omitted means include
2539 the minibuffer window only if the minibuffer is active. Any
2540 other value means do not include the minibuffer window even if
2541 the minibuffer is active.
2543 ALL-FRAMES nil or omitted means consider all windows on the
2544 selected frame, plus the minibuffer window if specified by the
2545 MINIBUF argument. If the minibuffer counts, consider all windows
2546 on all frames that share that minibuffer too. The following
2547 non-nil values of ALL-FRAMES have special meanings:
2549 - t means consider all windows on all existing frames.
2551 - `visible' means consider all windows on all visible frames on
2552 the current terminal.
2554 - 0 (the number zero) means consider all windows on all visible
2555 and iconified frames on the current terminal.
2557 - A frame means consider all windows on that frame only.
2559 Anything else means consider all windows on the selected frame
2560 and no others."
2561 (let ((buffer (window-normalize-buffer buffer-or-name))
2562 windows)
2563 (dolist (window (window-list-1 (selected-window) minibuf all-frames))
2564 (when (eq (window-buffer window) buffer)
2565 (setq windows (cons window windows))))
2566 (nreverse windows)))
2568 (defun minibuffer-window-active-p (window)
2569 "Return t if WINDOW is the currently active minibuffer window."
2570 (eq window (active-minibuffer-window)))
2572 (defun count-windows (&optional minibuf)
2573 "Return the number of live windows on the selected frame.
2574 The optional argument MINIBUF specifies whether the minibuffer
2575 window shall be counted. See `walk-windows' for the precise
2576 meaning of this argument."
2577 (length (window-list-1 nil minibuf)))
2579 ;;; Resizing windows.
2580 (defun window--size-to-pixel (window size &optional horizontal pixelwise round-maybe)
2581 "For WINDOW convert SIZE lines to pixels.
2582 SIZE is supposed to specify a height of WINDOW in terms of text
2583 lines. The return value is the number of pixels specifying that
2584 height.
2586 WINDOW must be a valid window. Optional argument HORIZONTAL
2587 non-nil means convert SIZE columns to pixels.
2589 Optional argument PIXELWISE non-nil means SIZE already specifies
2590 pixels but may have to be adjusted to a multiple of the character
2591 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2592 means round to the nearest multiple of the character size of
2593 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2594 (setq window (window-normalize-window window))
2595 (let ((char-size (frame-char-size window horizontal)))
2596 (if pixelwise
2597 (if (and round-maybe (not window-resize-pixelwise))
2598 (* (round size char-size) char-size)
2599 size)
2600 (* size char-size))))
2602 (defun window--pixel-to-total-1 (window horizontal char-size)
2603 "Subroutine of `window--pixel-to-total'."
2604 (let ((child (window-child window)))
2605 (if (window-combination-p window horizontal)
2606 ;; In an iso-combination distribute sizes proportionally.
2607 (let ((remainder (window-new-total window))
2608 size best-child rem best-rem)
2609 ;; Initialize total sizes to each child's floor.
2610 (while child
2611 (setq size (max (/ (window-size child horizontal t) char-size) 1))
2612 (set-window-new-total child size)
2613 (setq remainder (- remainder size))
2614 (setq child (window-next-sibling child)))
2615 ;; Distribute remainder.
2616 (while (> remainder 0)
2617 (setq child (window-last-child window))
2618 (setq best-child nil)
2619 (setq best-rem 0)
2620 (while child
2621 (when (and (<= (window-new-total child)
2622 (/ (window-size child horizontal t) char-size))
2623 (> (setq rem (% (window-size child horizontal t)
2624 char-size))
2625 best-rem))
2626 (setq best-child child)
2627 (setq best-rem rem))
2628 (setq child (window-prev-sibling child)))
2629 ;; We MUST have a best-child here.
2630 (set-window-new-total best-child 1 t)
2631 (setq remainder (1- remainder)))
2632 ;; Recurse.
2633 (setq child (window-child window))
2634 (while child
2635 (window--pixel-to-total-1 child horizontal char-size)
2636 (setq child (window-next-sibling child))))
2637 ;; In an ortho-combination assign new sizes directly.
2638 (let ((size (window-new-total window)))
2639 (while child
2640 (set-window-new-total child size)
2641 (window--pixel-to-total-1 child horizontal char-size)
2642 (setq child (window-next-sibling child)))))))
2644 (defun window--pixel-to-total (&optional frame horizontal)
2645 "On FRAME assign new total window heights from pixel heights.
2646 FRAME must be a live frame and defaults to the selected frame.
2648 Optional argument HORIZONTAL non-nil means assign new total
2649 window widths from pixel widths."
2650 (setq frame (window-normalize-frame frame))
2651 (let* ((char-size (frame-char-size frame horizontal))
2652 (root (frame-root-window frame))
2653 (root-size (window-size root horizontal t))
2654 ;; We have to care about the minibuffer window only if it
2655 ;; appears together with the root window on this frame.
2656 (mini (let ((mini (minibuffer-window frame)))
2657 (and (eq (window-frame mini) frame)
2658 (not (eq mini root)) mini)))
2659 (mini-size (and mini (window-size mini horizontal t))))
2660 ;; We round the line/column sizes of windows here to the nearest
2661 ;; integer. In some cases this can make windows appear _larger_
2662 ;; than the containing frame (line/column-wise) because the latter's
2663 ;; sizes are not (yet) rounded. We might eventually fix that.
2664 (if (and mini (not horizontal))
2665 (let (lines)
2666 (set-window-new-total root (max (/ root-size char-size) 1))
2667 (set-window-new-total mini (max (/ mini-size char-size) 1))
2668 (setq lines (- (round (+ root-size mini-size) char-size)
2669 (+ (window-new-total root) (window-new-total mini))))
2670 (while (> lines 0)
2671 (if (>= (% root-size (window-new-total root))
2672 (% mini-size (window-new-total mini)))
2673 (set-window-new-total root 1 t)
2674 (set-window-new-total mini 1 t))
2675 (setq lines (1- lines))))
2676 (set-window-new-total root (round root-size char-size))
2677 (when mini
2678 ;; This is taken in the horizontal case only.
2679 (set-window-new-total mini (round mini-size char-size))))
2680 (unless (window-buffer root)
2681 (window--pixel-to-total-1 root horizontal char-size))
2682 ;; Apply the new sizes.
2683 (window-resize-apply-total frame horizontal)))
2685 (defun window--resize-reset (&optional frame horizontal)
2686 "Reset resize values for all windows on FRAME.
2687 FRAME defaults to the selected frame.
2689 This function stores the current value of `window-size' applied
2690 with argument HORIZONTAL in the new total size of all windows on
2691 FRAME. It also resets the new normal size of each of these
2692 windows."
2693 (window--resize-reset-1
2694 (frame-root-window (window-normalize-frame frame)) horizontal))
2696 (defun window--resize-reset-1 (window horizontal)
2697 "Internal function of `window--resize-reset'."
2698 ;; Register old size in the new total size.
2699 (set-window-new-pixel window (window-size window horizontal t))
2700 (set-window-new-total window (window-size window horizontal))
2701 ;; Reset new normal size.
2702 (set-window-new-normal window)
2703 (when (window-child window)
2704 (window--resize-reset-1 (window-child window) horizontal))
2705 (when (window-right window)
2706 (window--resize-reset-1 (window-right window) horizontal)))
2708 (defun window--resize-mini-window (window delta)
2709 "Resize minibuffer window WINDOW by DELTA pixels.
2710 If WINDOW cannot be resized by DELTA pixels make it as large (or
2711 as small) as possible, but don't signal an error."
2712 (when (window-minibuffer-p window)
2713 (let* ((frame (window-frame window))
2714 (root (frame-root-window frame))
2715 (height (window-pixel-height window))
2716 (min-delta
2717 (- (window-pixel-height root)
2718 (window-min-size root nil nil t))))
2719 ;; Sanitize DELTA.
2720 (cond
2721 ((<= (+ height delta) 0)
2722 (setq delta (- (frame-char-height (window-frame window)) height)))
2723 ((> delta min-delta)
2724 (setq delta min-delta)))
2726 (unless (zerop delta)
2727 ;; Resize now.
2728 (window--resize-reset frame)
2729 ;; Ideally we should be able to resize just the last child of root
2730 ;; here. See the comment in `resize-root-window-vertically' for
2731 ;; why we do not do that.
2732 (window--resize-this-window root (- delta) nil nil t)
2733 (set-window-new-pixel window (+ height delta))
2734 ;; The following routine catches the case where we want to resize
2735 ;; a minibuffer-only frame.
2736 (when (resize-mini-window-internal window)
2737 (window--pixel-to-total frame)
2738 (run-window-configuration-change-hook frame))))))
2740 (defun window--resize-apply-p (frame &optional horizontal)
2741 "Return t when a window on FRAME shall be resized vertically.
2742 Optional argument HORIZONTAL non-nil means return t when a window
2743 shall be resized horizontally."
2744 (catch 'apply
2745 (walk-window-tree
2746 (lambda (window)
2747 (unless (= (window-new-pixel window)
2748 (window-size window horizontal t))
2749 (throw 'apply t)))
2750 frame t)
2751 nil))
2753 (defun window-resize (window delta &optional horizontal ignore pixelwise)
2754 "Resize WINDOW vertically by DELTA lines.
2755 WINDOW can be an arbitrary window and defaults to the selected
2756 one. An attempt to resize the root window of a frame will raise
2757 an error though.
2759 DELTA a positive number means WINDOW shall be enlarged by DELTA
2760 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2761 lines.
2763 Optional argument HORIZONTAL non-nil means resize WINDOW
2764 horizontally by DELTA columns. In this case a positive DELTA
2765 means enlarge WINDOW by DELTA columns. DELTA negative means
2766 WINDOW shall be shrunk by -DELTA columns.
2768 Optional argument IGNORE, if non-nil, means to ignore restraints
2769 induced by fixed size windows or the values of the variables
2770 `window-min-height' and `window-min-width'. The following values
2771 have special meanings: `safe' means that in addition live windows
2772 are allowed to get as small as `window-safe-min-height' lines and
2773 `window-safe-min-width' columns. `preserved' means to ignore
2774 only restrictions induced by `window-preserve-size'. If IGNORE
2775 is a window, then ignore restrictions for that window only.
2777 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2778 pixels.
2780 This function resizes other windows proportionally and never
2781 deletes any windows. If you want to move only the low (right)
2782 edge of WINDOW consider using `adjust-window-trailing-edge'
2783 instead."
2784 (setq window (window-normalize-window window))
2785 (let* ((frame (window-frame window))
2786 (minibuffer-window (minibuffer-window frame))
2787 sibling)
2788 (setq delta (window--size-to-pixel
2789 window delta horizontal pixelwise t))
2790 (cond
2791 ((eq window (frame-root-window frame))
2792 (error "Cannot resize the root window of a frame"))
2793 ((window-minibuffer-p window)
2794 (if horizontal
2795 (error "Cannot resize minibuffer window horizontally")
2796 (window--resize-mini-window window delta)))
2797 ((and (not horizontal)
2798 (window-full-height-p window)
2799 (eq (window-frame minibuffer-window) frame)
2800 (or (not resize-mini-windows)
2801 (eq minibuffer-window (active-minibuffer-window))))
2802 ;; If WINDOW is full height and either `resize-mini-windows' is
2803 ;; nil or the minibuffer window is active, resize the minibuffer
2804 ;; window.
2805 (window--resize-mini-window minibuffer-window (- delta)))
2806 ((or (window--resizable-p
2807 window delta horizontal ignore nil nil nil t)
2808 (and (not ignore)
2809 (setq ignore 'preserved)
2810 (window--resizable-p
2811 window delta horizontal ignore nil nil nil t)))
2812 (window--resize-reset frame horizontal)
2813 (window--resize-this-window window delta horizontal ignore t)
2814 (if (and (not (eq window-combination-resize t))
2815 (window-combined-p window horizontal)
2816 (setq sibling (or (window-right window) (window-left window)))
2817 (window-sizable-p
2818 sibling (- delta) horizontal ignore t))
2819 ;; If window-combination-resize is nil, WINDOW is part of an
2820 ;; iso-combination, and WINDOW's neighboring right or left
2821 ;; sibling can be resized as requested, resize that sibling.
2822 (let ((normal-delta
2823 (/ (float delta)
2824 (window-size (window-parent window) horizontal t))))
2825 (window--resize-this-window sibling (- delta) horizontal nil t)
2826 (set-window-new-normal
2827 window (+ (window-normal-size window horizontal)
2828 normal-delta))
2829 (set-window-new-normal
2830 sibling (- (window-normal-size sibling horizontal)
2831 normal-delta)))
2832 ;; Otherwise, resize all other windows in the same combination.
2833 (window--resize-siblings window delta horizontal ignore))
2834 (when (window--resize-apply-p frame horizontal)
2835 (if (window-resize-apply frame horizontal)
2836 (progn
2837 (window--pixel-to-total frame horizontal)
2838 (run-window-configuration-change-hook frame))
2839 (error "Failed to apply resizing %s" window))))
2841 (error "Cannot resize window %s" window)))))
2843 (defun window-resize-no-error (window delta &optional horizontal ignore pixelwise)
2844 "Resize WINDOW vertically if it is resizable by DELTA lines.
2845 This function is like `window-resize' but does not signal an
2846 error when WINDOW cannot be resized. For the meaning of the
2847 optional arguments see the documentation of `window-resize'."
2848 (when (window--resizable-p
2849 window delta horizontal ignore nil nil nil pixelwise)
2850 (window-resize window delta horizontal ignore pixelwise)))
2852 (defun window--resize-child-windows-skip-p (window)
2853 "Return non-nil if WINDOW shall be skipped by resizing routines."
2854 (memq (window-new-normal window) '(ignore stuck skip)))
2856 (defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
2857 "Recursively set new normal height of child windows of window PARENT.
2858 HORIZONTAL non-nil means set the new normal width of these
2859 windows. WINDOW specifies a child window of PARENT that has been
2860 resized by THIS-DELTA lines (columns).
2862 Optional argument TRAIL either `before' or `after' means set values
2863 only for windows before or after WINDOW. Optional argument
2864 OTHER-DELTA, a number, specifies that this many lines (columns)
2865 have been obtained from (or returned to) an ancestor window of
2866 PARENT in order to resize WINDOW."
2867 (let* ((delta-normal
2868 (if (and (= (- this-delta)
2869 (window-size window horizontal t))
2870 (zerop other-delta))
2871 ;; When WINDOW gets deleted and we can return its entire
2872 ;; space to its siblings, use WINDOW's normal size as the
2873 ;; normal delta.
2874 (- (window-normal-size window horizontal))
2875 ;; In any other case calculate the normal delta from the
2876 ;; relation of THIS-DELTA to the total size of PARENT.
2877 (/ (float this-delta)
2878 (window-size parent horizontal t))))
2879 (sub (window-child parent))
2880 (parent-normal 0.0)
2881 (skip (eq trail 'after)))
2883 ;; Set parent-normal to the sum of the normal sizes of all child
2884 ;; windows of PARENT that shall be resized, excluding only WINDOW
2885 ;; and any windows specified by the optional TRAIL argument.
2886 (while sub
2887 (cond
2888 ((eq sub window)
2889 (setq skip (eq trail 'before)))
2890 (skip)
2892 (setq parent-normal
2893 (+ parent-normal (window-normal-size sub horizontal)))))
2894 (setq sub (window-right sub)))
2896 ;; Set the new normal size of all child windows of PARENT from what
2897 ;; they should have contributed for recovering THIS-DELTA lines
2898 ;; (columns).
2899 (setq sub (window-child parent))
2900 (setq skip (eq trail 'after))
2901 (while sub
2902 (cond
2903 ((eq sub window)
2904 (setq skip (eq trail 'before)))
2905 (skip)
2907 (let ((old-normal (window-normal-size sub horizontal)))
2908 (set-window-new-normal
2909 sub (min 1.0 ; Don't get larger than 1.
2910 (max (- old-normal
2911 (* (/ old-normal parent-normal)
2912 delta-normal))
2913 ;; Don't drop below 0.
2914 0.0))))))
2915 (setq sub (window-right sub)))
2917 (when (numberp other-delta)
2918 ;; Set the new normal size of windows from what they should have
2919 ;; contributed for recovering OTHER-DELTA lines (columns).
2920 (setq delta-normal (/ (float (window-size parent horizontal t))
2921 (+ (window-size parent horizontal t)
2922 other-delta)))
2923 (setq sub (window-child parent))
2924 (setq skip (eq trail 'after))
2925 (while sub
2926 (cond
2927 ((eq sub window)
2928 (setq skip (eq trail 'before)))
2929 (skip)
2931 (set-window-new-normal
2932 sub (min 1.0 ; Don't get larger than 1.
2933 (max (* (window-new-normal sub) delta-normal)
2934 ;; Don't drop below 0.
2935 0.0)))))
2936 (setq sub (window-right sub))))
2938 ;; Set the new normal size of WINDOW to what is left by the sum of
2939 ;; the normal sizes of its siblings.
2940 (set-window-new-normal
2941 window
2942 (let ((sum 0))
2943 (setq sub (window-child parent))
2944 (while sub
2945 (cond
2946 ((eq sub window))
2947 ((not (numberp (window-new-normal sub)))
2948 (setq sum (+ sum (window-normal-size sub horizontal))))
2950 (setq sum (+ sum (window-new-normal sub)))))
2951 (setq sub (window-right sub)))
2952 ;; Don't get larger than 1 or smaller than 0.
2953 (min 1.0 (max (- 1.0 sum) 0.0))))))
2955 (defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge char-size)
2956 "Resize child windows of window PARENT vertically by DELTA pixels.
2957 PARENT must be a vertically combined internal window.
2959 Optional argument HORIZONTAL non-nil means resize child windows
2960 of PARENT horizontally by DELTA pixels. In this case PARENT must
2961 be a horizontally combined internal window.
2963 WINDOW, if specified, must denote a child window of PARENT that
2964 is resized by DELTA pixels.
2966 The optional argument IGNORE has the same meaning as for
2967 `window-resizable'.
2969 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2970 of windows that shall be resized. If TRAIL equals `before',
2971 resize only windows on the left or above EDGE. If TRAIL equals
2972 `after', resize only windows on the right or below EDGE. Also,
2973 preferably only resize windows adjacent to EDGE.
2975 If the optional argument CHAR-SIZE is a positive integer, it specifies
2976 the number of pixels by which windows are incrementally resized.
2977 If CHAR-SIZE is nil, this means to use the value of
2978 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2980 Return the symbol `normalized' if new normal sizes have been
2981 already set by this routine."
2982 (let* ((first (window-child parent))
2983 (last (window-last-child parent))
2984 (parent-total (+ (window-size parent horizontal t)
2985 delta))
2986 (char-size (or char-size
2987 (and window-resize-pixelwise 1)
2988 (frame-char-size window horizontal)))
2989 sub best-window best-value best-delta)
2991 (if (and edge (memq trail '(before after))
2992 (progn
2993 (setq sub first)
2994 (while (and (window-right sub)
2995 (or (and (eq trail 'before)
2996 (not (window--resize-child-windows-skip-p
2997 (window-right sub))))
2998 (and (eq trail 'after)
2999 (window--resize-child-windows-skip-p sub))))
3000 (setq sub (window-right sub)))
3001 sub)
3002 (if horizontal
3003 (if (eq trail 'before)
3004 (= (+ (window-pixel-left sub) (window-pixel-width sub))
3005 edge)
3006 (= (window-pixel-left sub) edge))
3007 (if (eq trail 'before)
3008 (= (+ (window-pixel-top sub) (window-pixel-height sub))
3009 edge)
3010 (= (window-pixel-top sub) edge)))
3011 (window-sizable-p sub delta horizontal ignore t))
3012 ;; Resize only windows adjacent to EDGE.
3013 (progn
3014 (window--resize-this-window
3015 sub delta horizontal ignore t trail edge)
3016 (if (and window (eq (window-parent sub) parent))
3017 (progn
3018 ;; Assign new normal sizes.
3019 (set-window-new-normal
3020 sub (/ (float (window-new-pixel sub)) parent-total))
3021 (set-window-new-normal
3022 window (- (window-normal-size window horizontal)
3023 (- (window-new-normal sub)
3024 (window-normal-size sub horizontal)))))
3025 (window--resize-child-windows-normal
3026 parent horizontal sub 0 trail delta))
3027 ;; Return 'normalized to notify `window--resize-siblings' that
3028 ;; normal sizes have been already set.
3029 'normalized)
3030 ;; Resize all windows proportionally.
3031 (setq sub last)
3032 (while sub
3033 (cond
3034 ((or (window--resize-child-windows-skip-p sub)
3035 ;; Ignore windows to skip and fixed-size child windows -
3036 ;; in the latter case make it a window to skip.
3037 (and (not ignore)
3038 (window-size-fixed-p sub horizontal ignore)
3039 (set-window-new-normal sub 'ignore))))
3040 ((< delta 0)
3041 ;; When shrinking store the number of lines/cols we can get
3042 ;; from this window here together with the total/normal size
3043 ;; factor.
3044 (set-window-new-normal
3046 (cons
3047 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
3048 (window-min-delta sub horizontal ignore trail t nil t)
3049 (- (/ (float (window-size sub horizontal t))
3050 parent-total)
3051 (window-normal-size sub horizontal)))))
3052 ((> delta 0)
3053 ;; When enlarging store the total/normal size factor only
3054 (set-window-new-normal
3056 (- (/ (float (window-size sub horizontal t))
3057 parent-total)
3058 (window-normal-size sub horizontal)))))
3060 (setq sub (window-left sub)))
3062 (cond
3063 ((< delta 0)
3064 ;; Shrink windows by delta.
3065 (setq best-window t)
3066 (while (and best-window (not (zerop delta)))
3067 (setq sub last)
3068 (setq best-window nil)
3069 (setq best-value most-negative-fixnum)
3070 (while sub
3071 (when (and (consp (window-new-normal sub))
3072 (not (<= (car (window-new-normal sub)) 0))
3073 (> (cdr (window-new-normal sub)) best-value))
3074 (setq best-window sub)
3075 (setq best-value (cdr (window-new-normal sub))))
3077 (setq sub (window-left sub)))
3079 (when best-window
3080 (setq best-delta (min (car (window-new-normal best-window))
3081 char-size (- delta)))
3082 (setq delta (+ delta best-delta))
3083 (set-window-new-pixel best-window (- best-delta) t)
3084 (set-window-new-normal
3085 best-window
3086 (if (= (car (window-new-normal best-window)) best-delta)
3087 'skip ; We can't shrink best-window any further.
3088 (cons (- (car (window-new-normal best-window)) best-delta)
3089 (- (/ (float (window-new-pixel best-window))
3090 parent-total)
3091 (window-normal-size best-window horizontal))))))))
3092 ((> delta 0)
3093 ;; Enlarge windows by delta.
3094 (setq best-window t)
3095 (while (and best-window (not (zerop delta)))
3096 (setq sub last)
3097 (setq best-window nil)
3098 (setq best-value most-positive-fixnum)
3099 (while sub
3100 (when (and (numberp (window-new-normal sub))
3101 (< (window-new-normal sub) best-value))
3102 (setq best-window sub)
3103 (setq best-value (window-new-normal sub)))
3105 (setq sub (window-left sub)))
3107 (when best-window
3108 (setq best-delta (min delta char-size))
3109 (setq delta (- delta best-delta))
3110 (set-window-new-pixel best-window best-delta t)
3111 (set-window-new-normal
3112 best-window
3113 (- (/ (float (window-new-pixel best-window))
3114 parent-total)
3115 (window-normal-size best-window horizontal)))))))
3117 (when best-window
3118 (setq sub last)
3119 (while sub
3120 (when (or (consp (window-new-normal sub))
3121 (numberp (window-new-normal sub)))
3122 ;; Reset new normal size fields so `window-resize-apply'
3123 ;; won't use them to apply new sizes.
3124 (set-window-new-normal sub))
3126 (unless (eq (window-new-normal sub) 'ignore)
3127 ;; Resize this window's child windows (back-engineering
3128 ;; delta from sub's old and new total sizes).
3129 (let ((delta (- (window-new-pixel sub)
3130 (window-size sub horizontal t))))
3131 (unless (and (zerop delta) (not trail))
3132 ;; For the TRAIL non-nil case we have to resize SUB
3133 ;; recursively even if it's size does not change.
3134 (window--resize-this-window
3135 sub delta horizontal ignore nil trail edge))))
3136 (setq sub (window-left sub)))))))
3138 (defun window--resize-siblings (window delta &optional horizontal ignore trail edge char-size)
3139 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
3140 Optional argument HORIZONTAL non-nil means resize other windows
3141 when WINDOW is resized horizontally by DELTA pixels. WINDOW
3142 itself is not resized by this function.
3144 The optional argument IGNORE has the same meaning as for
3145 `window-resizable'.
3147 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3148 of windows that shall be resized. If TRAIL equals `before',
3149 resize only windows on the left or above EDGE. If TRAIL equals
3150 `after', resize only windows on the right or below EDGE. Also,
3151 preferably only resize windows adjacent to EDGE."
3152 (when (window-parent window)
3153 (let* ((parent (window-parent window))
3154 (sub (window-child parent)))
3155 (if (window-combined-p sub horizontal)
3156 ;; In an iso-combination try to extract DELTA from WINDOW's
3157 ;; siblings.
3158 (let ((skip (eq trail 'after))
3159 this-delta other-delta)
3160 ;; Decide which windows shall be left alone.
3161 (while sub
3162 (cond
3163 ((eq sub window)
3164 ;; Make sure WINDOW is left alone when
3165 ;; resizing its siblings.
3166 (set-window-new-normal sub 'ignore)
3167 (setq skip (eq trail 'before)))
3168 (skip
3169 ;; Make sure this sibling is left alone when
3170 ;; resizing its siblings.
3171 (set-window-new-normal sub 'ignore))
3172 ((not (window-size-fixed-p sub horizontal ignore))
3173 ;; Set this-delta to t to signal that we found a sibling
3174 ;; of WINDOW whose size is not fixed.
3175 (setq this-delta t)))
3177 (setq sub (window-right sub)))
3179 ;; Set this-delta to what we can get from WINDOW's siblings.
3180 (if (= (- delta) (window-size window horizontal t))
3181 ;; A deletion, presumably. We must handle this case
3182 ;; specially since `window--resizable' can't be used.
3183 (if this-delta
3184 ;; There's at least one resizable sibling we can
3185 ;; give WINDOW's size to.
3186 (setq this-delta delta)
3187 ;; No resizable sibling exists.
3188 (setq this-delta 0))
3189 ;; Any other form of resizing.
3190 (setq this-delta
3191 (window--resizable
3192 window delta horizontal ignore trail t nil t)))
3194 ;; Set other-delta to what we still have to get from
3195 ;; ancestor windows of parent.
3196 (setq other-delta (- delta this-delta))
3197 (unless (zerop other-delta)
3198 ;; Unless we got everything from WINDOW's siblings, PARENT
3199 ;; must be resized by other-delta lines or columns.
3200 (set-window-new-pixel parent other-delta 'add))
3202 (if (zerop this-delta)
3203 ;; We haven't got anything from WINDOW's siblings but we
3204 ;; must update the normal sizes to respect other-delta.
3205 (window--resize-child-windows-normal
3206 parent horizontal window this-delta trail other-delta)
3207 ;; We did get something from WINDOW's siblings which means
3208 ;; we have to resize their child windows.
3209 (unless (eq (window--resize-child-windows
3210 parent (- this-delta) horizontal
3211 window ignore trail edge char-size)
3212 ;; If `window--resize-child-windows' returns
3213 ;; 'normalized, this means it has set the
3214 ;; normal sizes already.
3215 'normalized)
3216 ;; Set the normal sizes.
3217 (window--resize-child-windows-normal
3218 parent horizontal window this-delta trail other-delta))
3219 ;; Set DELTA to what we still have to get from ancestor
3220 ;; windows.
3221 (setq delta other-delta)))
3223 ;; In an ortho-combination all siblings of WINDOW must be
3224 ;; resized by DELTA.
3225 (set-window-new-pixel parent delta 'add)
3226 (while sub
3227 (unless (eq sub window)
3228 (window--resize-this-window
3229 sub delta horizontal ignore t))
3230 (setq sub (window-right sub))))
3232 (unless (zerop delta)
3233 ;; "Go up."
3234 (window--resize-siblings
3235 parent delta horizontal ignore trail edge char-size)))))
3237 (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge char-size)
3238 "Resize WINDOW vertically by DELTA pixels.
3239 Optional argument HORIZONTAL non-nil means resize WINDOW
3240 horizontally by DELTA pixels.
3242 The optional argument IGNORE has the same meaning as for
3243 `window-resizable'. Optional argument ADD non-nil means add
3244 DELTA to the new total size of WINDOW.
3246 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3247 of windows that shall be resized. If TRAIL equals `before',
3248 resize only windows on the left or above EDGE. If TRAIL equals
3249 `after', resize only windows on the right or below EDGE. Also,
3250 preferably only resize windows adjacent to EDGE.
3252 If the optional argument CHAR-SIZE is a positive integer, it specifies
3253 the number of pixels by which windows are incrementally resized.
3254 If CHAR-SIZE is nil, this means to use the value of
3255 `frame-char-height' or `frame-char-width' of WINDOW's frame.
3257 This function recursively resizes WINDOW's child windows to fit the
3258 new size. Make sure that WINDOW is `window--resizable' before
3259 calling this function. Note that this function does not resize
3260 siblings of WINDOW or WINDOW's parent window. You have to
3261 eventually call `window-resize-apply' in order to make resizing
3262 actually take effect."
3263 (when add
3264 ;; Add DELTA to the new total size of WINDOW.
3265 (set-window-new-pixel window delta t))
3267 (let ((sub (window-child window)))
3268 (cond
3269 ((not sub))
3270 ((window-combined-p sub horizontal)
3271 ;; In an iso-combination resize child windows according to their
3272 ;; normal sizes.
3273 (window--resize-child-windows
3274 window delta horizontal nil ignore trail edge char-size))
3275 ;; In an ortho-combination resize each child window by DELTA.
3277 (while sub
3278 (window--resize-this-window
3279 sub delta horizontal ignore t trail edge char-size)
3280 (setq sub (window-right sub)))))))
3282 (defun window--resize-root-window (window delta horizontal ignore pixelwise)
3283 "Resize root window WINDOW vertically by DELTA lines.
3284 HORIZONTAL non-nil means resize root window WINDOW horizontally
3285 by DELTA columns.
3287 IGNORE non-nil means ignore any restrictions imposed by fixed
3288 size windows, `window-min-height' or `window-min-width' settings.
3290 This function is only called by the frame resizing routines. It
3291 resizes windows proportionally and never deletes any windows."
3292 (when (and (windowp window) (numberp delta))
3293 (let ((pixel-delta
3294 (if pixelwise
3295 delta
3296 (window--size-to-pixel window delta horizontal))))
3297 (when (window-sizable-p window pixel-delta horizontal ignore t)
3298 (window--resize-reset (window-frame window) horizontal)
3299 (window--resize-this-window
3300 window pixel-delta horizontal ignore t)))))
3302 (defun window--resize-root-window-vertically (window delta pixelwise)
3303 "Resize root window WINDOW vertically by DELTA lines.
3304 If DELTA is less than zero and we can't shrink WINDOW by DELTA
3305 lines, shrink it as much as possible. If DELTA is greater than
3306 zero, this function can resize fixed-size windows in order to
3307 recover the necessary lines. Return the number of lines that
3308 were recovered.
3310 Third argument PIXELWISE non-nil means to interpret DELTA as
3311 pixels and return the number of pixels that were recovered.
3313 This function is called by the minibuffer window resizing
3314 routines."
3315 (let* ((frame (window-frame window))
3316 (pixel-delta
3317 (cond
3318 (pixelwise
3319 delta)
3320 ((numberp delta)
3321 (* (frame-char-height frame) delta))
3322 (t 0)))
3323 ignore)
3324 (cond
3325 ((zerop pixel-delta))
3326 ((< pixel-delta 0)
3327 (setq pixel-delta (window-sizable window pixel-delta nil nil pixelwise))
3328 (window--resize-reset frame)
3329 ;; When shrinking the root window, emulate an edge drag in order
3330 ;; to not resize other windows if we can avoid it (Bug#12419).
3331 (window--resize-this-window
3332 window pixel-delta nil ignore t 'before
3333 (+ (window-pixel-top window) (window-pixel-height window)))
3334 ;; Don't record new normal sizes to make sure that shrinking back
3335 ;; proportionally works as intended.
3336 (walk-window-tree
3337 (lambda (window) (set-window-new-normal window 'ignore)) frame t))
3338 ((> pixel-delta 0)
3339 (window--resize-reset frame)
3340 (unless (window-sizable window pixel-delta nil nil pixelwise)
3341 (setq ignore t))
3342 ;; When growing the root window, resize proportionally. This
3343 ;; should give windows back their original sizes (hopefully).
3344 (window--resize-this-window
3345 window pixel-delta nil ignore t)))
3346 ;; Return the possibly adjusted DELTA.
3347 (if pixelwise
3348 pixel-delta
3349 (/ pixel-delta (frame-char-height frame)))))
3351 (defun window--sanitize-window-sizes (horizontal)
3352 "Assert that all windows on selected frame are large enough.
3353 If necessary and possible, make sure that every window on frame
3354 FRAME has its minimum height. Optional argument HORIZONTAL
3355 non-nil means to make sure that every window on frame FRAME has
3356 its minimum width. The minimum height/width of a window is the
3357 respective value returned by `window-min-size' for that window.
3359 Return t if all windows were resized appropriately. Return nil
3360 if at least one window could not be resized as requested, which
3361 may happen when the FRAME is not large enough to accommodate it."
3362 (let ((value t))
3363 (walk-window-tree
3364 (lambda (window)
3365 (let ((delta (- (window-min-size window horizontal nil t)
3366 (window-size window horizontal t))))
3367 (when (> delta 0)
3368 (if (window-resizable-p window delta horizontal nil t)
3369 (window-resize window delta horizontal nil t)
3370 (setq value nil))))))
3371 value))
3373 (defun adjust-window-trailing-edge (window delta &optional horizontal pixelwise)
3374 "Move WINDOW's bottom edge by DELTA lines.
3375 Optional argument HORIZONTAL non-nil means move WINDOW's right
3376 edge by DELTA columns. WINDOW must be a valid window and
3377 defaults to the selected one.
3379 Optional argument PIXELWISE non-nil means interpret DELTA as
3380 number of pixels.
3382 If DELTA is greater than zero, move the edge downwards or to the
3383 right. If DELTA is less than zero, move the edge upwards or to
3384 the left. If the edge can't be moved by DELTA lines or columns,
3385 move it as far as possible in the desired direction."
3386 (setq window (window-normalize-window window))
3387 (let* ((frame (window-frame window))
3388 (minibuffer-window (minibuffer-window frame))
3389 (right window)
3390 left first-left first-right this-delta min-delta max-delta ignore)
3392 (unless pixelwise
3393 (setq pixelwise t)
3394 (setq delta (* delta (frame-char-size window horizontal))))
3396 ;; Find the edge we want to move.
3397 (while (and (or (not (window-combined-p right horizontal))
3398 (not (window-right right)))
3399 (setq right (window-parent right))))
3400 (cond
3401 ((and (not right) (not horizontal)
3402 ;; Resize the minibuffer window if it's on the same frame as
3403 ;; and immediately below WINDOW and it's either active or
3404 ;; `resize-mini-windows' is nil.
3405 (eq (window-frame minibuffer-window) frame)
3406 (= (nth 1 (window-pixel-edges minibuffer-window))
3407 (nth 3 (window-pixel-edges window)))
3408 (or (not resize-mini-windows)
3409 (eq minibuffer-window (active-minibuffer-window))))
3410 (window--resize-mini-window minibuffer-window (- delta)))
3411 ((or (not (setq left right)) (not (setq right (window-right right))))
3412 (if horizontal
3413 (user-error "No window on the right of this one")
3414 (user-error "No window below this one")))
3416 ;; Set LEFT to the first resizable window on the left. This step is
3417 ;; needed to handle fixed-size windows.
3418 (setq first-left left)
3419 (while (and left
3420 (or (window-size-fixed-p left horizontal)
3421 (and (< delta 0)
3422 (<= (window-size left horizontal t)
3423 (window-min-size left horizontal nil t)))))
3424 (setq left
3425 (or (window-left left)
3426 (progn
3427 (while (and (setq left (window-parent left))
3428 (not (window-combined-p left horizontal))))
3429 (window-left left)))))
3430 (unless left
3431 ;; We have to resize a size-preserved window. Start again with
3432 ;; the window initially on the left.
3433 (setq ignore 'preserved)
3434 (setq left first-left)
3435 (while (and left
3436 (or (window-size-fixed-p left horizontal 'preserved)
3437 (and (< delta 0)
3438 (<= (window-size left horizontal t)
3439 (window-min-size
3440 left horizontal 'preserved t)))))
3441 (setq left
3442 (or (window-left left)
3443 (progn
3444 (while (and (setq left (window-parent left))
3445 (not (window-combined-p left horizontal))))
3446 (window-left left)))))
3448 (unless left
3449 (if horizontal
3450 (user-error "No resizable window on the left of this one")
3451 (user-error "No resizable window above this one"))))
3453 ;; Set RIGHT to the first resizable window on the right. This step
3454 ;; is needed to handle fixed-size windows.
3455 (setq first-right right)
3456 (while (and right
3457 (or (window-size-fixed-p right horizontal)
3458 (and (> delta 0)
3459 (<= (window-size right horizontal t)
3460 (window-min-size
3461 right horizontal 'preserved t)))))
3462 (setq right
3463 (or (window-right right)
3464 (progn
3465 (while (and (setq right (window-parent right))
3466 (not (window-combined-p right horizontal))))
3467 (window-right right)))))
3468 (unless right
3469 ;; We have to resize a size-preserved window. Start again with
3470 ;; the window initially on the right.
3471 (setq ignore 'preserved)
3472 (setq right first-right)
3473 (while (and right
3474 (or (window-size-fixed-p right horizontal 'preserved)
3475 (and (> delta 0)
3476 (<= (window-size right horizontal t)
3477 (window-min-size
3478 right horizontal 'preserved t)))))
3479 (setq right
3480 (or (window-right right)
3481 (progn
3482 (while (and (setq right (window-parent right))
3483 (not (window-combined-p right horizontal))))
3484 (window-right right)))))
3485 (unless right
3486 (if horizontal
3487 (user-error "No resizable window on the right of this one")
3488 (user-error "No resizable window below this one"))))
3490 ;; LEFT and RIGHT (which might be both internal windows) are now the
3491 ;; two windows we want to resize.
3492 (cond
3493 ((> delta 0)
3494 (setq max-delta
3495 (window--max-delta-1
3496 left 0 horizontal ignore 'after nil pixelwise))
3497 (setq min-delta
3498 (window--min-delta-1
3499 right (- delta) horizontal ignore 'before nil pixelwise))
3500 (when (or (< max-delta delta) (> min-delta (- delta)))
3501 ;; We can't get the whole DELTA - move as far as possible.
3502 (setq delta (min max-delta (- min-delta))))
3503 (unless (zerop delta)
3504 ;; Start resizing.
3505 (window--resize-reset frame horizontal)
3506 ;; Try to enlarge LEFT first.
3507 (setq this-delta
3508 (window--resizable
3509 left delta horizontal ignore 'after nil nil pixelwise))
3510 (unless (zerop this-delta)
3511 (window--resize-this-window
3512 left this-delta horizontal ignore t 'before
3513 (if horizontal
3514 (+ (window-pixel-left left) (window-pixel-width left))
3515 (+ (window-pixel-top left) (window-pixel-height left)))))
3516 ;; Shrink windows on right of LEFT.
3517 (window--resize-siblings
3518 left delta horizontal ignore 'after
3519 (if horizontal
3520 (window-pixel-left right)
3521 (window-pixel-top right)))))
3522 ((< delta 0)
3523 (setq max-delta
3524 (window--max-delta-1
3525 right 0 horizontal ignore 'before nil pixelwise))
3526 (setq min-delta
3527 (window--min-delta-1
3528 left delta horizontal ignore 'after nil pixelwise))
3529 (when (or (< max-delta (- delta)) (> min-delta delta))
3530 ;; We can't get the whole DELTA - move as far as possible.
3531 (setq delta (max (- max-delta) min-delta)))
3532 (unless (zerop delta)
3533 ;; Start resizing.
3534 (window--resize-reset frame horizontal)
3535 ;; Try to enlarge RIGHT.
3536 (setq this-delta
3537 (window--resizable
3538 right (- delta) horizontal ignore 'before nil nil pixelwise))
3539 (unless (zerop this-delta)
3540 (window--resize-this-window
3541 right this-delta horizontal ignore t 'after
3542 (if horizontal
3543 (window-pixel-left right)
3544 (window-pixel-top right))))
3545 ;; Shrink windows on left of RIGHT.
3546 (window--resize-siblings
3547 right (- delta) horizontal ignore 'before
3548 (if horizontal
3549 (+ (window-pixel-left left) (window-pixel-width left))
3550 (+ (window-pixel-top left) (window-pixel-height left)))))))
3551 (unless (zerop delta)
3552 ;; Don't report an error in the standard case.
3553 (when (window--resize-apply-p frame horizontal)
3554 (if (window-resize-apply frame horizontal)
3555 (progn
3556 (window--pixel-to-total frame horizontal)
3557 (run-window-configuration-change-hook frame))
3558 ;; But do report an error if applying the changes fails.
3559 (error "Failed adjusting window %s" window))))))))
3561 (defun enlarge-window (delta &optional horizontal)
3562 "Make the selected window DELTA lines taller.
3563 Interactively, if no argument is given, make the selected window
3564 one line taller. If optional argument HORIZONTAL is non-nil,
3565 make selected window wider by DELTA columns. If DELTA is
3566 negative, shrink selected window by -DELTA lines or columns."
3567 (interactive "p")
3568 (let ((minibuffer-window (minibuffer-window)))
3569 (when (window-preserved-size nil horizontal)
3570 (window-preserve-size nil horizontal))
3571 (cond
3572 ((zerop delta))
3573 ((window-size-fixed-p nil horizontal)
3574 (user-error "Selected window has fixed size"))
3575 ((window-minibuffer-p)
3576 (if horizontal
3577 (user-error "Cannot resize minibuffer window horizontally")
3578 (window--resize-mini-window
3579 (selected-window) (* delta (frame-char-height)))))
3580 ((and (not horizontal)
3581 (window-full-height-p)
3582 (eq (window-frame minibuffer-window) (selected-frame))
3583 (not resize-mini-windows))
3584 ;; If the selected window is full height and `resize-mini-windows'
3585 ;; is nil, resize the minibuffer window.
3586 (window--resize-mini-window
3587 minibuffer-window (* (- delta) (frame-char-height))))
3588 ((window--resizable-p nil delta horizontal)
3589 (window-resize nil delta horizontal))
3590 ((window--resizable-p nil delta horizontal 'preserved)
3591 (window-resize nil delta horizontal 'preserved))
3592 ((eq this-command
3593 (if horizontal 'enlarge-window-horizontally 'enlarge-window))
3594 ;; For backward compatibility don't signal an error unless this
3595 ;; command is `enlarge-window(-horizontally)'.
3596 (user-error "Cannot enlarge selected window"))
3598 (window-resize
3599 nil (if (> delta 0)
3600 (window-max-delta nil horizontal)
3601 (- (window-min-delta nil horizontal)))
3602 horizontal)))))
3604 (defun shrink-window (delta &optional horizontal)
3605 "Make the selected window DELTA lines smaller.
3606 Interactively, if no argument is given, make the selected window
3607 one line smaller. If optional argument HORIZONTAL is non-nil,
3608 make selected window narrower by DELTA columns. If DELTA is
3609 negative, enlarge selected window by -DELTA lines or columns."
3610 (interactive "p")
3611 (let ((minibuffer-window (minibuffer-window)))
3612 (when (window-preserved-size nil horizontal)
3613 (window-preserve-size nil horizontal))
3614 (cond
3615 ((zerop delta))
3616 ((window-size-fixed-p nil horizontal)
3617 (user-error "Selected window has fixed size"))
3618 ((window-minibuffer-p)
3619 (if horizontal
3620 (user-error "Cannot resize minibuffer window horizontally")
3621 (window--resize-mini-window
3622 (selected-window) (* (- delta) (frame-char-height)))))
3623 ((and (not horizontal)
3624 (window-full-height-p)
3625 (eq (window-frame minibuffer-window) (selected-frame))
3626 (not resize-mini-windows))
3627 ;; If the selected window is full height and `resize-mini-windows'
3628 ;; is nil, resize the minibuffer window.
3629 (window--resize-mini-window
3630 minibuffer-window (* delta (frame-char-height))))
3631 ((window--resizable-p nil (- delta) horizontal)
3632 (window-resize nil (- delta) horizontal))
3633 ((window--resizable-p nil (- delta) horizontal 'preserved)
3634 (window-resize nil (- delta) horizontal 'preserved))
3635 ((eq this-command
3636 (if horizontal 'shrink-window-horizontally 'shrink-window))
3637 ;; For backward compatibility don't signal an error unless this
3638 ;; command is `shrink-window(-horizontally)'.
3639 (user-error "Cannot shrink selected window"))
3641 (window-resize
3642 nil (if (> delta 0)
3643 (- (window-min-delta nil horizontal))
3644 (window-max-delta nil horizontal))
3645 horizontal)))))
3647 (defun maximize-window (&optional window)
3648 "Maximize WINDOW.
3649 Make WINDOW as large as possible without deleting any windows.
3650 WINDOW must be a valid window and defaults to the selected one.
3652 If the option `window-resize-pixelwise' is non-nil maximize
3653 WINDOW pixelwise."
3654 (interactive)
3655 (setq window (window-normalize-window window))
3656 (window-resize
3657 window (window-max-delta window nil nil nil nil nil window-resize-pixelwise)
3658 nil nil window-resize-pixelwise)
3659 (window-resize
3660 window (window-max-delta window t nil nil nil nil window-resize-pixelwise)
3661 t nil window-resize-pixelwise))
3663 (defun minimize-window (&optional window)
3664 "Minimize WINDOW.
3665 Make WINDOW as small as possible without deleting any windows.
3666 WINDOW must be a valid window and defaults to the selected one.
3668 If the option `window-resize-pixelwise' is non-nil minimize
3669 WINDOW pixelwise."
3670 (interactive)
3671 (setq window (window-normalize-window window))
3672 (window-resize
3673 window
3674 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise))
3675 nil nil window-resize-pixelwise)
3676 (window-resize
3677 window
3678 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise))
3679 t nil window-resize-pixelwise))
3681 ;;; Window edges
3682 (defun window-edges (&optional window body absolute pixelwise)
3683 "Return a list of the edge distances of WINDOW.
3684 WINDOW must be a valid window and defaults to the selected one.
3685 The list returned has the form (LEFT TOP RIGHT BOTTOM).
3687 If the optional argument BODY is nil, this means to return the
3688 edges corresponding to the total size of WINDOW. BODY non-nil
3689 means to return the edges of WINDOW's body (aka text area). If
3690 BODY is non-nil, WINDOW must specify a live window.
3692 Optional argument ABSOLUTE nil means to return edges relative to
3693 the position of WINDOW's native frame. ABSOLUTE non-nil means to
3694 return coordinates relative to the origin - the position (0, 0) -
3695 of FRAME's display. On non-graphical systems this argument has
3696 no effect.
3698 Optional argument PIXELWISE nil means to return the coordinates
3699 in terms of the canonical character width and height of WINDOW's
3700 frame, rounded if necessary. PIXELWISE non-nil means to return
3701 the coordinates in pixels where the values for RIGHT and BOTTOM
3702 are one more than the actual value of these edges. Note that if
3703 ABSOLUTE is non-nil, PIXELWISE is implicitly non-nil too."
3704 (let* ((window (window-normalize-window window body))
3705 (frame (window-frame window))
3706 (border-width (frame-border-width frame))
3707 (char-width (frame-char-width frame))
3708 (char-height (frame-char-height frame))
3709 (left (if pixelwise
3710 (+ (window-pixel-left window) border-width)
3711 (+ (window-left-column window)
3712 (/ border-width char-width))))
3713 (left-body
3714 (when body
3715 (+ (window-pixel-left window) border-width
3716 (if (eq (car (window-current-scroll-bars window)) 'left)
3717 (window-scroll-bar-width window)
3719 (nth 0 (window-fringes window))
3720 (* (or (nth 0 (window-margins window)) 0) char-width))))
3721 (top (if pixelwise
3722 (+ (window-pixel-top window) border-width)
3723 (+ (window-top-line window)
3724 (/ border-width char-height))))
3725 (top-body
3726 (when body
3727 (+ (window-pixel-top window) border-width
3728 (window-header-line-height window))))
3729 (right (+ left (if pixelwise
3730 (window-pixel-width window)
3731 (window-total-width window))))
3732 (right-body (and body (+ left-body (window-body-width window t))))
3733 (bottom (+ top (if pixelwise
3734 (window-pixel-height window)
3735 (window-total-height window))))
3736 (bottom-body (and body (+ top-body (window-body-height window t)))))
3737 (if absolute
3738 (let* ((native-edges (frame-edges frame 'native-edges))
3739 (left-off (nth 0 native-edges))
3740 (top-off (nth 1 native-edges)))
3741 (if body
3742 (list (+ left-body left-off) (+ top-body top-off)
3743 (+ right-body left-off) (+ bottom-body top-off))
3744 (list (+ left left-off) (+ top top-off)
3745 (+ right left-off) (+ bottom top-off))))
3746 (if body
3747 (if pixelwise
3748 (list left-body top-body right-body bottom-body)
3749 (list (/ left-body char-width) (/ top-body char-height)
3750 ;; Round up.
3751 (/ (+ right-body char-width -1) char-width)
3752 (/ (+ bottom-body char-height -1) char-height)))
3753 (list left top right bottom)))))
3755 (defun window-body-edges (&optional window)
3756 "Return a list of the edge coordinates of WINDOW's body.
3757 The return value is that of `window-edges' called with argument
3758 BODY non-nil."
3759 (window-edges window t))
3760 (defalias 'window-inside-edges 'window-body-edges)
3762 (defun window-pixel-edges (&optional window)
3763 "Return a list of the edge pixel coordinates of WINDOW.
3764 The return value is that of `window-edges' called with argument
3765 PIXELWISE non-nil."
3766 (window-edges window nil nil t))
3768 (defun window-body-pixel-edges (&optional window)
3769 "Return a list of the edge pixel coordinates of WINDOW's body.
3770 The return value is that of `window-edges' called with arguments
3771 BODY and PIXELWISE non-nil."
3772 (window-edges window t nil t))
3773 (defalias 'window-inside-pixel-edges 'window-body-pixel-edges)
3775 (defun window-absolute-pixel-edges (&optional window)
3776 "Return a list of the edge pixel coordinates of WINDOW.
3777 The return value is that of `window-edges' called with argument
3778 ABSOLUTE non-nil."
3779 (window-edges window nil t t))
3781 (defun window-absolute-body-pixel-edges (&optional window)
3782 "Return a list of the edge pixel coordinates of WINDOW's text area.
3783 The return value is that of `window-edges' called with arguments
3784 BODY and ABSOLUTE non-nil."
3785 (window-edges window t t t))
3786 (defalias 'window-inside-absolute-pixel-edges 'window-absolute-body-pixel-edges)
3788 (defun window-absolute-pixel-position (&optional position window)
3789 "Return display coordinates of POSITION in WINDOW.
3790 If the buffer position POSITION is visible in window WINDOW,
3791 return the display coordinates of the upper/left corner of the
3792 glyph at POSITION. The return value is a cons of the X- and
3793 Y-coordinates of that corner, relative to an origin at (0, 0) of
3794 WINDOW's display. Return nil if POSITION is not visible in
3795 WINDOW.
3797 WINDOW must be a live window and defaults to the selected window.
3798 POSITION defaults to the value of `window-point' of WINDOW."
3799 (let* ((window (window-normalize-window window t))
3800 (pos-in-window
3801 (pos-visible-in-window-p
3802 (or position (window-point window)) window t)))
3803 (when pos-in-window
3804 (let ((edges (window-absolute-body-pixel-edges window)))
3805 (cons (+ (nth 0 edges) (nth 0 pos-in-window))
3806 (+ (nth 1 edges) (nth 1 pos-in-window)))))))
3808 (defun frame-root-window-p (window)
3809 "Return non-nil if WINDOW is the root window of its frame."
3810 (eq window (frame-root-window window)))
3812 (defun window--subtree (window &optional next)
3813 "Return window subtree rooted at WINDOW.
3814 Optional argument NEXT non-nil means include WINDOW's right
3815 siblings in the return value.
3817 See the documentation of `window-tree' for a description of the
3818 return value."
3819 (let (list)
3820 (while window
3821 (setq list
3822 (cons
3823 (cond
3824 ((window-top-child window)
3825 (cons t (cons (window-edges window)
3826 (window--subtree (window-top-child window) t))))
3827 ((window-left-child window)
3828 (cons nil (cons (window-edges window)
3829 (window--subtree (window-left-child window) t))))
3830 (t window))
3831 list))
3832 (setq window (when next (window-next-sibling window))))
3833 (nreverse list)))
3835 (defun window-tree (&optional frame)
3836 "Return the window tree of frame FRAME.
3837 FRAME must be a live frame and defaults to the selected frame.
3838 The return value is a list of the form (ROOT MINI), where ROOT
3839 represents the window tree of the frame's root window, and MINI
3840 is the frame's minibuffer window.
3842 If the root window is not split, ROOT is the root window itself.
3843 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3844 for a horizontal split, and t for a vertical split. EDGES gives
3845 the combined size and position of the child windows in the split,
3846 and the rest of the elements are the child windows in the split.
3847 Each of the child windows may again be a window or a list
3848 representing a window split, and so on. EDGES is a list (LEFT
3849 TOP RIGHT BOTTOM) as returned by `window-edges'."
3850 (setq frame (window-normalize-frame frame))
3851 (window--subtree (frame-root-window frame) t))
3853 (defun other-window (count &optional all-frames)
3854 "Select another window in cyclic ordering of windows.
3855 COUNT specifies the number of windows to skip, starting with the
3856 selected window, before making the selection. If COUNT is
3857 positive, skip COUNT windows forwards. If COUNT is negative,
3858 skip -COUNT windows backwards. COUNT zero means do not skip any
3859 window, so select the selected window. In an interactive call,
3860 COUNT is the numeric prefix argument. Return nil.
3862 If the `other-window' parameter of the selected window is a
3863 function and `ignore-window-parameters' is nil, call that
3864 function with the arguments COUNT and ALL-FRAMES.
3866 This function does not select a window whose `no-other-window'
3867 window parameter is non-nil.
3869 This function uses `next-window' for finding the window to
3870 select. The argument ALL-FRAMES has the same meaning as in
3871 `next-window', but the MINIBUF argument of `next-window' is
3872 always effectively nil."
3873 (interactive "p")
3874 (let* ((window (selected-window))
3875 (function (and (not ignore-window-parameters)
3876 (window-parameter window 'other-window)))
3877 old-window old-count)
3878 (if (functionp function)
3879 (funcall function count all-frames)
3880 ;; `next-window' and `previous-window' may return a window we are
3881 ;; not allowed to select. Hence we need an exit strategy in case
3882 ;; all windows are non-selectable.
3883 (catch 'exit
3884 (while (> count 0)
3885 (setq window (next-window window nil all-frames))
3886 (cond
3887 ((eq window old-window)
3888 (when (= count old-count)
3889 ;; Keep out of infinite loops. When COUNT has not changed
3890 ;; since we last looked at `window' we're probably in one.
3891 (throw 'exit nil)))
3892 ((window-parameter window 'no-other-window)
3893 (unless old-window
3894 ;; The first non-selectable window `next-window' got us:
3895 ;; Remember it and the current value of COUNT.
3896 (setq old-window window)
3897 (setq old-count count)))
3899 (setq count (1- count)))))
3900 (while (< count 0)
3901 (setq window (previous-window window nil all-frames))
3902 (cond
3903 ((eq window old-window)
3904 (when (= count old-count)
3905 ;; Keep out of infinite loops. When COUNT has not changed
3906 ;; since we last looked at `window' we're probably in one.
3907 (throw 'exit nil)))
3908 ((window-parameter window 'no-other-window)
3909 (unless old-window
3910 ;; The first non-selectable window `previous-window' got
3911 ;; us: Remember it and the current value of COUNT.
3912 (setq old-window window)
3913 (setq old-count count)))
3915 (setq count (1+ count)))))
3917 (select-window window)
3918 ;; Always return nil.
3919 nil))))
3921 ;; This should probably return non-nil when the selected window is part
3922 ;; of an atomic window whose root is the frame's root window.
3923 (defun one-window-p (&optional nomini all-frames)
3924 "Return non-nil if the selected window is the only window.
3925 Optional arg NOMINI non-nil means don't count the minibuffer
3926 even if it is active. Otherwise, the minibuffer is counted
3927 when it is active.
3929 Optional argument ALL-FRAMES specifies the set of frames to
3930 consider, see also `next-window'. ALL-FRAMES nil or omitted
3931 means consider windows on the selected frame only, plus the
3932 minibuffer window if specified by the NOMINI argument. If the
3933 minibuffer counts, consider all windows on all frames that share
3934 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3935 with a special meaning are:
3937 - t means consider all windows on all existing frames.
3939 - `visible' means consider all windows on all visible frames on
3940 the current terminal.
3942 - 0 (the number zero) means consider all windows on all visible
3943 and iconified frames on the current terminal.
3945 - A frame means consider all windows on that frame only.
3947 Anything else means consider all windows on the selected frame
3948 and no others."
3949 (let ((base-window (selected-window)))
3950 (if (and nomini (eq base-window (minibuffer-window)))
3951 (setq base-window (next-window base-window)))
3952 (eq base-window
3953 (next-window base-window (if nomini 'arg) all-frames))))
3955 ;;; Deleting windows.
3956 (defun window-deletable-p (&optional window)
3957 "Return t if WINDOW can be safely deleted from its frame.
3958 WINDOW must be a valid window and defaults to the selected one.
3960 Return `frame' if WINDOW is the root window of its frame and that
3961 frame can be safely deleted."
3962 (setq window (window-normalize-window window))
3964 (unless (or ignore-window-parameters
3965 (eq (window-parameter window 'delete-window) t))
3966 ;; Handle atomicity.
3967 (when (window-parameter window 'window-atom)
3968 (setq window (window-atom-root window))))
3970 (let ((frame (window-frame window)))
3971 (cond
3972 ((frame-root-window-p window)
3973 ;; WINDOW's frame can be deleted only if there are other frames
3974 ;; on the same terminal, and it does not contain the active
3975 ;; minibuffer.
3976 (unless (or (eq frame (next-frame frame 0))
3977 ;; We can delete our frame only if no other frame
3978 ;; currently uses our minibuffer window.
3979 (catch 'other
3980 (dolist (other (frame-list))
3981 (when (and (not (eq other frame))
3982 (eq (window-frame (minibuffer-window other))
3983 frame))
3984 (throw 'other t))))
3985 (let ((minibuf (active-minibuffer-window)))
3986 (and minibuf (eq frame (window-frame minibuf)))))
3987 'frame))
3988 ((window-minibuffer-p window)
3989 ;; If WINDOW is the minibuffer window of a non-minibuffer-only
3990 ;; frame, it cannot be deleted separately.
3991 nil)
3992 ((or ignore-window-parameters
3993 (not (eq window (window-main-window frame))))
3994 ;; Otherwise, WINDOW can be deleted unless it is the main window
3995 ;; of its frame.
3996 t))))
3998 (defun window--in-subtree-p (window root)
3999 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
4000 (or (eq window root)
4001 (let ((parent (window-parent window)))
4002 (catch 'done
4003 (while parent
4004 (if (eq parent root)
4005 (throw 'done t)
4006 (setq parent (window-parent parent))))))))
4008 (defun delete-window (&optional window)
4009 "Delete WINDOW.
4010 WINDOW must be a valid window and defaults to the selected one.
4011 Return nil.
4013 If the variable `ignore-window-parameters' is non-nil or the
4014 `delete-window' parameter of WINDOW equals t, do not process any
4015 parameters of WINDOW. Otherwise, if the `delete-window'
4016 parameter of WINDOW specifies a function, call that function with
4017 WINDOW as its sole argument and return the value returned by that
4018 function.
4020 Otherwise, if WINDOW is part of an atomic window, call
4021 `delete-window' with the root of the atomic window as its
4022 argument. Signal an error if WINDOW is either the only window on
4023 its frame, the last non-side window, or part of an atomic window
4024 that is its frame's root window."
4025 (interactive)
4026 (setq window (window-normalize-window window))
4027 (let* ((frame (window-frame window))
4028 (function (window-parameter window 'delete-window))
4029 (parent (window-parent window))
4030 atom-root)
4031 (window--check frame)
4032 (catch 'done
4033 ;; Handle window parameters.
4034 (cond
4035 ;; Ignore window parameters if `ignore-window-parameters' tells
4036 ;; us so or `delete-window' equals t.
4037 ((or ignore-window-parameters (eq function t)))
4038 ((functionp function)
4039 ;; The `delete-window' parameter specifies the function to call.
4040 ;; If that function is `ignore' nothing is done. It's up to the
4041 ;; function called here to avoid infinite recursion.
4042 (throw 'done (funcall function window)))
4043 ((and (window-parameter window 'window-atom)
4044 (setq atom-root (window-atom-root window))
4045 (not (eq atom-root window)))
4046 (if (eq atom-root (frame-root-window frame))
4047 (error "Root of atomic window is root window of its frame")
4048 (throw 'done (delete-window atom-root))))
4049 ((not parent)
4050 (error "Attempt to delete minibuffer or sole ordinary window"))
4051 ((eq window (window-main-window frame))
4052 (error "Attempt to delete main window of frame %s" frame)))
4054 (let* ((horizontal (window-left-child parent))
4055 (size (window-size window horizontal t))
4056 (window-combination-resize
4057 (or window-combination-resize
4058 (window-parameter parent 'window-side)))
4059 (frame-selected
4060 (window--in-subtree-p (frame-selected-window frame) window))
4061 ;; Emacs 23 preferably gives WINDOW's space to its left
4062 ;; sibling.
4063 (sibling (or (window-left window) (window-right window))))
4064 (window--resize-reset frame horizontal)
4065 (cond
4066 ((and (not (eq window-combination-resize t))
4067 sibling (window-sizable-p sibling size horizontal nil t))
4068 ;; Resize WINDOW's sibling.
4069 (window--resize-this-window sibling size horizontal nil t)
4070 (set-window-new-normal
4071 sibling (+ (window-normal-size sibling horizontal)
4072 (window-normal-size window horizontal))))
4073 ((window--resizable-p window (- size) horizontal nil nil nil t t)
4074 ;; Can do without resizing fixed-size windows.
4075 (window--resize-siblings window (- size) horizontal))
4077 ;; Can't do without resizing fixed-size windows.
4078 (window--resize-siblings window (- size) horizontal t)))
4079 ;; Actually delete WINDOW.
4080 (delete-window-internal window)
4081 (window--pixel-to-total frame horizontal)
4082 (when (and frame-selected
4083 (window-parameter
4084 (frame-selected-window frame) 'no-other-window))
4085 ;; `delete-window-internal' has selected a window that should
4086 ;; not be selected, fix this here.
4087 (other-window -1 frame))
4088 (run-window-configuration-change-hook frame)
4089 (window--check frame)
4090 ;; Always return nil.
4091 nil))))
4093 (defun delete-other-windows (&optional window)
4094 "Make WINDOW fill its frame.
4095 WINDOW must be a valid window and defaults to the selected one.
4096 Return nil.
4098 If the variable `ignore-window-parameters' is non-nil or the
4099 `delete-other-windows' parameter of WINDOW equals t, do not
4100 process any parameters of WINDOW. Otherwise, if the
4101 `delete-other-windows' parameter of WINDOW specifies a function,
4102 call that function with WINDOW as its sole argument and return
4103 the value returned by that function.
4105 Otherwise, if WINDOW is part of an atomic window, call this
4106 function with the root of the atomic window as its argument. If
4107 WINDOW is a non-side window, make WINDOW the only non-side window
4108 on the frame. Side windows are not deleted. If WINDOW is a side
4109 window signal an error."
4110 (interactive)
4111 (setq window (window-normalize-window window))
4112 (let* ((frame (window-frame window))
4113 (function (window-parameter window 'delete-other-windows))
4114 atom-root main)
4115 (window--check frame)
4116 (catch 'done
4117 (cond
4118 ;; Ignore window parameters if `ignore-window-parameters' is t or
4119 ;; `delete-other-windows' is t.
4120 ((or ignore-window-parameters (eq function t)))
4121 ((functionp function)
4122 ;; The `delete-other-windows' parameter specifies the function
4123 ;; to call. If the function is `ignore' no windows are deleted.
4124 ;; It's up to the function called to avoid infinite recursion.
4125 (throw 'done (funcall function window)))
4126 ((and (window-parameter window 'window-atom)
4127 (setq atom-root (window-atom-root window))
4128 (not (eq atom-root window)))
4129 (if (eq atom-root (frame-root-window frame))
4130 (error "Root of atomic window is root window of its frame")
4131 (throw 'done (delete-other-windows atom-root))))
4132 ((window-parameter window 'window-side)
4133 (error "Cannot make side window the only window"))
4134 ((and (window-minibuffer-p window)
4135 (not (eq window (frame-root-window window))))
4136 (error "Can't expand minibuffer to full frame")))
4138 (cond
4139 ((or ignore-window-parameters
4140 (not (window-with-parameter 'no-delete-other-window nil frame)))
4141 (setq main (frame-root-window frame)))
4142 ((catch 'tag
4143 (walk-window-tree
4144 (lambda (other)
4145 (when (or (and (window-parameter other 'window-side)
4146 (not (window-parameter
4147 other 'no-delete-other-window)))
4148 (and (not (window-parameter other 'window-side))
4149 (window-parameter
4150 other 'no-delete-other-window)))
4151 (throw 'tag nil))))
4153 (setq main (window-main-window frame)))
4155 ;; Delete other windows via `delete-window' because either a
4156 ;; side window is or a non-side-window is not deletable.
4157 (dolist (other (window-list frame))
4158 (when (and (window-live-p other)
4159 (not (eq other window))
4160 (not (window-parameter
4161 other 'no-delete-other-window))
4162 ;; When WINDOW and the other window are part of the
4163 ;; same atomic window, don't delete the other.
4164 (or (not atom-root)
4165 (not (eq (window-atom-root other) atom-root))))
4166 (condition-case nil
4167 (delete-window other)
4168 (error nil))))
4169 (throw 'done nil)))
4171 ;; If WINDOW is the main window of its frame do nothing.
4172 (unless (eq window main)
4173 (delete-other-windows-internal window main)
4174 (run-window-configuration-change-hook frame)
4175 (window--check frame))
4176 ;; Always return nil.
4177 nil)))
4179 (defun delete-other-windows-vertically (&optional window)
4180 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
4181 This may be a useful alternative binding for \\[delete-other-windows]
4182 if you often split windows horizontally."
4183 (interactive)
4184 (let* ((window (or window (selected-window)))
4185 (edges (window-edges window))
4186 (w window) delenda)
4187 (while (not (eq (setq w (next-window w 1)) window))
4188 (let ((e (window-edges w)))
4189 (when (and (= (car e) (car edges))
4190 (= (nth 2 e) (nth 2 edges)))
4191 (push w delenda))))
4192 (mapc 'delete-window delenda)))
4194 ;;; Windows and buffers.
4196 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
4197 ;; for (1) determining which buffer to show in the window when its
4198 ;; buffer shall be buried or killed and (2) which buffer to show for
4199 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
4201 ;; `prev-buffers' consists of <buffer, window-start, window-point>
4202 ;; triples. The entries on this list are ordered by the time their
4203 ;; buffer has been removed from the window, the most recently removed
4204 ;; buffer's entry being first. The window-start and window-point
4205 ;; components are `window-start' and `window-point' at the time the
4206 ;; buffer was removed from the window which implies that the entry must
4207 ;; be added when `set-window-buffer' removes the buffer from the window.
4209 ;; `next-buffers' is the list of buffers that have been replaced
4210 ;; recently by `switch-to-prev-buffer'. These buffers are the least
4211 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
4212 ;; candidates of `switch-to-next-buffer' to switch to. This list is
4213 ;; reset to nil by any action changing the window's buffer with the
4214 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
4215 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
4216 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
4218 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
4219 ;; if such a buffer was killed while the window was hidden within a
4220 ;; window configuration. Such killed buffers get removed whenever
4221 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
4223 ;; The following function is called by `set-window-buffer' _before_ it
4224 ;; replaces the buffer of the argument window with the new buffer.
4225 (defun record-window-buffer (&optional window)
4226 "Record WINDOW's buffer.
4227 WINDOW must be a live window and defaults to the selected one."
4228 (let* ((window (window-normalize-window window t))
4229 (buffer (window-buffer window))
4230 (entry (assq buffer (window-prev-buffers window))))
4231 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
4232 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
4233 (set-window-next-buffers window nil)
4235 (when entry
4236 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
4237 (set-window-prev-buffers
4238 window (assq-delete-all buffer (window-prev-buffers window))))
4240 ;; Don't record insignificant buffers.
4241 (unless (eq (aref (buffer-name buffer) 0) ?\s)
4242 ;; Add an entry for buffer to WINDOW's previous buffers.
4243 (with-current-buffer buffer
4244 (let ((start (window-start window))
4245 (point (window-point window)))
4246 (setq entry
4247 (cons buffer
4248 (if entry
4249 ;; We have an entry, update marker positions.
4250 (list (set-marker (nth 1 entry) start)
4251 (set-marker (nth 2 entry) point))
4252 ;; Make new markers.
4253 (list (copy-marker start)
4254 (copy-marker
4255 ;; Preserve window-point-insertion-type
4256 ;; (Bug#12588).
4257 point window-point-insertion-type)))))
4258 (set-window-prev-buffers
4259 window (cons entry (window-prev-buffers window)))))
4261 (run-hooks 'buffer-list-update-hook))))
4263 (defun unrecord-window-buffer (&optional window buffer)
4264 "Unrecord BUFFER in WINDOW.
4265 WINDOW must be a live window and defaults to the selected one.
4266 BUFFER must be a live buffer and defaults to the buffer of
4267 WINDOW."
4268 (let* ((window (window-normalize-window window t))
4269 (buffer (or buffer (window-buffer window))))
4270 (set-window-prev-buffers
4271 window (assq-delete-all buffer (window-prev-buffers window)))
4272 (set-window-next-buffers
4273 window (delq buffer (window-next-buffers window)))))
4275 (defun set-window-buffer-start-and-point (window buffer &optional start point)
4276 "Set WINDOW's buffer to BUFFER.
4277 WINDOW must be a live window and defaults to the selected one.
4278 Optional argument START non-nil means set WINDOW's start position
4279 to START. Optional argument POINT non-nil means set WINDOW's
4280 point to POINT. If WINDOW is selected this also sets BUFFER's
4281 `point' to POINT. If WINDOW is selected and the buffer it showed
4282 before was current this also makes BUFFER the current buffer."
4283 (setq window (window-normalize-window window t))
4284 (let ((selected (eq window (selected-window)))
4285 (current (eq (window-buffer window) (current-buffer))))
4286 (set-window-buffer window buffer)
4287 (when (and selected current)
4288 (set-buffer buffer))
4289 (when start
4290 ;; Don't force window-start here (even if POINT is nil).
4291 (set-window-start window start t))
4292 (when point
4293 (set-window-point window point))))
4295 (defcustom switch-to-visible-buffer t
4296 "If non-nil, allow switching to an already visible buffer.
4297 If this variable is non-nil, `switch-to-prev-buffer' and
4298 `switch-to-next-buffer' may switch to an already visible buffer.
4299 If this variable is nil, `switch-to-prev-buffer' and
4300 `switch-to-next-buffer' always try to avoid switching to a buffer
4301 that is already visible in another window on the same frame."
4302 :type 'boolean
4303 :version "24.1"
4304 :group 'windows)
4306 (defun switch-to-prev-buffer (&optional window bury-or-kill)
4307 "In WINDOW switch to previous buffer.
4308 WINDOW must be a live window and defaults to the selected one.
4309 Return the buffer switched to, nil if no suitable buffer could be
4310 found.
4312 Optional argument BURY-OR-KILL non-nil means the buffer currently
4313 shown in WINDOW is about to be buried or killed and consequently
4314 shall not be switched to in future invocations of this command.
4316 As a special case, if BURY-OR-KILL equals `append', this means to
4317 move the buffer to the end of WINDOW's previous buffers list so a
4318 future invocation of `switch-to-prev-buffer' less likely switches
4319 to it."
4320 (interactive)
4321 (let* ((window (window-normalize-window window t))
4322 (frame (window-frame window))
4323 (window-side (window-parameter window 'window-side))
4324 (old-buffer (window-buffer window))
4325 ;; Save this since it's destroyed by `set-window-buffer'.
4326 (next-buffers (window-next-buffers window))
4327 (pred (frame-parameter frame 'buffer-predicate))
4328 entry new-buffer killed-buffers visible)
4329 (when (window-minibuffer-p window)
4330 ;; Don't switch in minibuffer window.
4331 (unless (setq window (minibuffer-selected-window))
4332 (error "Window %s is a minibuffer window" window)))
4334 (unless (memq (window-dedicated-p window) '(nil side))
4335 ;; Don't switch in dedicated window.
4336 (error "Window %s is dedicated to buffer %s" window old-buffer))
4338 (catch 'found
4339 ;; Scan WINDOW's previous buffers first, skipping entries of next
4340 ;; buffers.
4341 (dolist (entry (window-prev-buffers window))
4342 (when (and (setq new-buffer (car entry))
4343 (or (buffer-live-p new-buffer)
4344 (not (setq killed-buffers
4345 (cons new-buffer killed-buffers))))
4346 (not (eq new-buffer old-buffer))
4347 (or (null pred) (funcall pred new-buffer))
4348 ;; When BURY-OR-KILL is nil, avoid switching to a
4349 ;; buffer in WINDOW's next buffers list.
4350 (or bury-or-kill (not (memq new-buffer next-buffers))))
4351 (if (and (not switch-to-visible-buffer)
4352 (get-buffer-window new-buffer frame))
4353 ;; Try to avoid showing a buffer visible in some other
4354 ;; window.
4355 (setq visible new-buffer)
4356 (set-window-buffer-start-and-point
4357 window new-buffer (nth 1 entry) (nth 2 entry))
4358 (throw 'found t))))
4359 ;; Scan reverted buffer list of WINDOW's frame next, skipping
4360 ;; entries of next buffers. Note that when we bury or kill a
4361 ;; buffer we don't reverse the global buffer list to avoid showing
4362 ;; a buried buffer instead. Otherwise, we must reverse the global
4363 ;; buffer list in order to make sure that switching to the
4364 ;; previous/next buffer traverse it in opposite directions. Skip
4365 ;; this step for side windows.
4366 (unless window-side
4367 (dolist (buffer (if bury-or-kill
4368 (buffer-list frame)
4369 (nreverse (buffer-list frame))))
4370 (when (and (buffer-live-p buffer)
4371 (not (eq buffer old-buffer))
4372 (or (null pred) (funcall pred buffer))
4373 (not (eq (aref (buffer-name buffer) 0) ?\s))
4374 ;; Don't show a buffer shown in a side window before.
4375 (not (buffer-local-value 'window--sides-shown buffer))
4376 (or bury-or-kill (not (memq buffer next-buffers))))
4377 (if (and (not switch-to-visible-buffer)
4378 (get-buffer-window buffer frame))
4379 ;; Try to avoid showing a buffer visible in some other window.
4380 (unless visible
4381 (setq visible buffer))
4382 (setq new-buffer buffer)
4383 (set-window-buffer-start-and-point window new-buffer)
4384 (throw 'found t)))))
4385 (unless bury-or-kill
4386 ;; Scan reverted next buffers last (must not use nreverse
4387 ;; here!).
4388 (dolist (buffer (reverse next-buffers))
4389 ;; Actually, buffer _must_ be live here since otherwise it
4390 ;; would have been caught in the scan of previous buffers.
4391 (when (and (or (buffer-live-p buffer)
4392 (not (setq killed-buffers
4393 (cons buffer killed-buffers))))
4394 (not (eq buffer old-buffer))
4395 (or (null pred) (funcall pred buffer))
4396 (setq entry (assq buffer (window-prev-buffers window))))
4397 (setq new-buffer buffer)
4398 (set-window-buffer-start-and-point
4399 window new-buffer (nth 1 entry) (nth 2 entry))
4400 (throw 'found t))))
4402 ;; Show a buffer visible in another window.
4403 (when visible
4404 (setq new-buffer visible)
4405 (set-window-buffer-start-and-point window new-buffer)))
4407 (if bury-or-kill
4408 (let ((entry (and (eq bury-or-kill 'append)
4409 (assq old-buffer (window-prev-buffers window)))))
4410 ;; Remove `old-buffer' from WINDOW's previous and (restored list
4411 ;; of) next buffers.
4412 (set-window-prev-buffers
4413 window (assq-delete-all old-buffer (window-prev-buffers window)))
4414 (set-window-next-buffers window (delq old-buffer next-buffers))
4415 (when entry
4416 ;; Append old-buffer's entry to list of WINDOW's previous
4417 ;; buffers so it's less likely to get switched to soon but
4418 ;; `display-buffer-in-previous-window' can nevertheless find
4419 ;; it.
4420 (set-window-prev-buffers
4421 window (append (window-prev-buffers window) (list entry)))))
4422 ;; Move `old-buffer' to head of WINDOW's restored list of next
4423 ;; buffers.
4424 (set-window-next-buffers
4425 window (cons old-buffer (delq old-buffer next-buffers))))
4427 ;; Remove killed buffers from WINDOW's previous and next buffers.
4428 (when killed-buffers
4429 (dolist (buffer killed-buffers)
4430 (set-window-prev-buffers
4431 window (assq-delete-all buffer (window-prev-buffers window)))
4432 (set-window-next-buffers
4433 window (delq buffer (window-next-buffers window)))))
4435 ;; Return new-buffer.
4436 new-buffer))
4438 (defun switch-to-next-buffer (&optional window)
4439 "In WINDOW switch to next buffer.
4440 WINDOW must be a live window and defaults to the selected one.
4441 Return the buffer switched to, nil if no suitable buffer could be
4442 found."
4443 (interactive)
4444 (let* ((window (window-normalize-window window t))
4445 (frame (window-frame window))
4446 (window-side (window-parameter window 'window-side))
4447 (old-buffer (window-buffer window))
4448 (next-buffers (window-next-buffers window))
4449 (pred (frame-parameter frame 'buffer-predicate))
4450 new-buffer entry killed-buffers visible)
4451 (when (window-minibuffer-p window)
4452 ;; Don't switch in minibuffer window.
4453 (unless (setq window (minibuffer-selected-window))
4454 (error "Window %s is a minibuffer window" window)))
4456 (unless (memq (window-dedicated-p window) '(nil side))
4457 ;; Don't switch in dedicated window.
4458 (error "Window %s is dedicated to buffer %s" window old-buffer))
4460 (catch 'found
4461 ;; Scan WINDOW's next buffers first.
4462 (dolist (buffer next-buffers)
4463 (when (and (or (buffer-live-p buffer)
4464 (not (setq killed-buffers
4465 (cons buffer killed-buffers))))
4466 (not (eq buffer old-buffer))
4467 (or (null pred) (funcall pred buffer))
4468 (setq entry (assq buffer (window-prev-buffers window))))
4469 (setq new-buffer buffer)
4470 (set-window-buffer-start-and-point
4471 window new-buffer (nth 1 entry) (nth 2 entry))
4472 (throw 'found t)))
4473 ;; Scan the buffer list of WINDOW's frame next, skipping previous
4474 ;; buffers entries. Skip this step for side windows.
4475 (unless window-side
4476 (dolist (buffer (buffer-list frame))
4477 (when (and (buffer-live-p buffer)
4478 (not (eq buffer old-buffer))
4479 (or (null pred) (funcall pred buffer))
4480 (not (eq (aref (buffer-name buffer) 0) ?\s))
4481 ;; Don't show a buffer shown in a side window before.
4482 (not (buffer-local-value 'window--sides-shown buffer))
4483 (not (assq buffer (window-prev-buffers window))))
4484 (if (and (not switch-to-visible-buffer)
4485 (get-buffer-window buffer frame))
4486 ;; Try to avoid showing a buffer visible in some other window.
4487 (setq visible buffer)
4488 (setq new-buffer buffer)
4489 (set-window-buffer-start-and-point window new-buffer)
4490 (throw 'found t)))))
4491 ;; Scan WINDOW's reverted previous buffers last (must not use
4492 ;; nreverse here!)
4493 (dolist (entry (reverse (window-prev-buffers window)))
4494 (when (and (setq new-buffer (car entry))
4495 (or (buffer-live-p new-buffer)
4496 (not (setq killed-buffers
4497 (cons new-buffer killed-buffers))))
4498 (not (eq new-buffer old-buffer))
4499 (or (null pred) (funcall pred new-buffer)))
4500 (if (and (not switch-to-visible-buffer)
4501 (get-buffer-window new-buffer frame))
4502 ;; Try to avoid showing a buffer visible in some other window.
4503 (unless visible
4504 (setq visible new-buffer))
4505 (set-window-buffer-start-and-point
4506 window new-buffer (nth 1 entry) (nth 2 entry))
4507 (throw 'found t))))
4509 ;; Show a buffer visible in another window.
4510 (when visible
4511 (setq new-buffer visible)
4512 (set-window-buffer-start-and-point window new-buffer)))