Fix recently-introduced cast typo
[emacs.git] / lisp / window.el
blobc4572106f4588f9244f578fad744b7c897564edb
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 <https://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 a live buffer, a string naming a live
324 buffer or nil which means to return the current buffer.
326 This function is commonly used to process the (usually optional)
327 \"BUFFER-OR-NAME\" argument of window related functions where nil
328 stands for the current buffer."
329 (let ((buffer
330 (cond
331 ((not buffer-or-name)
332 (current-buffer))
333 ((bufferp buffer-or-name)
334 buffer-or-name)
335 ((stringp buffer-or-name)
336 (get-buffer buffer-or-name))
338 (error "No such buffer %s" buffer-or-name)))))
339 (if (buffer-live-p buffer)
340 buffer
341 (error "No such live buffer %s" buffer-or-name))))
343 (defun window-normalize-frame (frame)
344 "Return frame specified by FRAME.
345 FRAME must be a live frame or nil which means to return the
346 selected frame.
348 This function is commonly used to process the (usually optional)
349 \"FRAME\" argument of window and frame related functions where
350 nil stands for the selected frame."
351 (if frame
352 (if (frame-live-p frame)
353 frame
354 (error "%s is not a live frame" frame))
355 (selected-frame)))
357 (defun window-normalize-window (window &optional live-only)
358 "Return window specified by WINDOW.
359 If WINDOW is nil, return the selected window. Otherwise, if
360 WINDOW is a live or an internal window, return WINDOW; if
361 LIVE-ONLY is non-nil, return WINDOW for a live window only.
362 Otherwise, signal an error.
364 This function is commonly used to process the (usually optional)
365 \"WINDOW\" argument of window related functions where nil stands
366 for the selected window."
367 (cond
368 ((null window)
369 (selected-window))
370 (live-only
371 (if (window-live-p window)
372 window
373 (error "%s is not a live window" window)))
374 ((window-valid-p window)
375 window)
377 (error "%s is not a valid window" window))))
379 ;; Maybe this should go to frame.el.
380 (defun frame-char-size (&optional window-or-frame horizontal)
381 "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
382 If WINDOW-OR-FRAME is a live frame, return the value of
383 `frame-char-height' for that frame. If WINDOW-OR-FRAME is a
384 valid window, return the value of `frame-char-height' for that
385 window's frame. In any other case, return the value of
386 `frame-char-height' for the selected frame.
388 Optional argument HORIZONTAL non-nil means to return the value of
389 `frame-char-width' for WINDOW-OR-FRAME."
390 (let ((frame
391 (cond
392 ((window-valid-p window-or-frame)
393 (window-frame window-or-frame))
394 ((frame-live-p window-or-frame)
395 window-or-frame)
396 (t (selected-frame)))))
397 (if horizontal
398 (frame-char-width frame)
399 (frame-char-height frame))))
401 (defvar ignore-window-parameters nil
402 "If non-nil, standard functions ignore window parameters.
403 The functions currently affected by this are `split-window',
404 `delete-window', `delete-other-windows' and `other-window'.
406 An application may bind this to a non-nil value around calls to
407 these functions to inhibit processing of window parameters.")
409 ;; This must go to C, finally (or get removed).
410 (defconst window-safe-min-height 1
411 "The absolute minimum number of lines of any window.
412 Anything less might crash Emacs.")
414 (defun window-safe-min-pixel-height (&optional window)
415 "Return the absolute minimum pixel height of WINDOW."
416 (* window-safe-min-height
417 (frame-char-size (window-normalize-window window))))
419 (defcustom window-min-height 4
420 "The minimum total height, in lines, of any window.
421 The value has to accommodate one text line, a mode and header
422 line, a horizontal scroll bar and a bottom divider, if present.
423 A value less than `window-safe-min-height' is ignored. The value
424 of this variable is honored when windows are resized or split.
426 Applications should never rebind this variable. To resize a
427 window to a height less than the one specified here, an
428 application should instead call `window-resize' with a non-nil
429 IGNORE argument. In order to have `split-window' make a window
430 shorter, explicitly specify the SIZE argument of that function."
431 :type 'integer
432 :version "24.1"
433 :group 'windows)
435 (defun window-min-pixel-height (&optional window)
436 "Return the minimum pixel height of window WINDOW."
437 (* (max window-min-height window-safe-min-height)
438 (frame-char-size window)))
440 ;; This must go to C, finally (or get removed).
441 (defconst window-safe-min-width 2
442 "The absolute minimum number of columns of a window.
443 Anything less might crash Emacs.")
445 (defun window-safe-min-pixel-width (&optional window)
446 "Return the absolute minimum pixel width of WINDOW."
447 (* window-safe-min-width
448 (frame-char-size (window-normalize-window window) t)))
450 (defcustom window-min-width 10
451 "The minimum total width, in columns, of any window.
452 The value has to accommodate two text columns as well as margins,
453 fringes, a scroll bar and a right divider, if present. A value
454 less than `window-safe-min-width' is ignored. The value of this
455 variable is honored when windows are resized or split.
457 Applications should never rebind this variable. To resize a
458 window to a width less than the one specified here, an
459 application should instead call `window-resize' with a non-nil
460 IGNORE argument. In order to have `split-window' make a window
461 narrower, explicitly specify the SIZE argument of that function."
462 :type 'integer
463 :version "24.1"
464 :group 'windows)
466 (defun window-min-pixel-width (&optional window)
467 "Return the minimum pixel width of window WINDOW."
468 (* (max window-min-width window-safe-min-width)
469 (frame-char-size window t)))
471 (defun window-safe-min-pixel-size (&optional window horizontal)
472 "Return the absolute minimum pixel height of WINDOW.
473 Optional argument HORIZONTAL non-nil means return the absolute
474 minimum pixel width of WINDOW."
475 (if horizontal
476 (window-safe-min-pixel-width window)
477 (window-safe-min-pixel-height window)))
479 (defun window-min-pixel-size (&optional window horizontal)
480 "Return the minimum pixel height of WINDOW.
481 Optional argument HORIZONTAL non-nil means return the minimum
482 pixel width of WINDOW."
483 (if horizontal
484 (window-min-pixel-width window)
485 (window-min-pixel-height window)))
487 (defun window-combined-p (&optional window horizontal)
488 "Return non-nil if WINDOW has siblings in a given direction.
489 WINDOW must be a valid window and defaults to the selected one.
491 HORIZONTAL determines a direction for the window combination. If
492 HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
493 a vertical window combination. If HORIZONTAL is non-nil, return
494 non-nil if WINDOW is part of a horizontal window combination."
495 (setq window (window-normalize-window window))
496 (let ((parent (window-parent window)))
497 (and parent
498 (if horizontal
499 (window-left-child parent)
500 (window-top-child parent)))))
502 (defun window-combination-p (&optional window horizontal)
503 "Return WINDOW's first child if WINDOW is a vertical combination.
504 WINDOW can be any window and defaults to the selected one.
505 Optional argument HORIZONTAL non-nil means return WINDOW's first
506 child if WINDOW is a horizontal combination."
507 (setq window (window-normalize-window window))
508 (if horizontal
509 (window-left-child window)
510 (window-top-child window)))
512 (defun window-combinations (window &optional horizontal)
513 "Return largest number of windows vertically arranged within WINDOW.
514 WINDOW must be a valid window and defaults to the selected one.
515 If HORIZONTAL is non-nil, return the largest number of
516 windows horizontally arranged within WINDOW."
517 (setq window (window-normalize-window window))
518 (cond
519 ((window-live-p window)
520 ;; If WINDOW is live, return 1.
522 ((if horizontal
523 (window-left-child window)
524 (window-top-child window))
525 ;; If WINDOW is iso-combined, return the sum of the values for all
526 ;; child windows of WINDOW.
527 (let ((child (window-child window))
528 (count 0))
529 (while child
530 (setq count
531 (+ (window-combinations child horizontal)
532 count))
533 (setq child (window-right child)))
534 count))
536 ;; If WINDOW is not iso-combined, return the maximum value of any
537 ;; child window of WINDOW.
538 (let ((child (window-child window))
539 (count 1))
540 (while child
541 (setq count
542 (max (window-combinations child horizontal)
543 count))
544 (setq child (window-right child)))
545 count))))
547 (defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only)
548 "Helper function for `walk-window-tree' and `walk-window-subtree'."
549 (let (walk-window-tree-buffer)
550 (while walk-window-tree-window
551 (setq walk-window-tree-buffer
552 (window-buffer walk-window-tree-window))
553 (when (or walk-window-tree-buffer any)
554 (funcall fun walk-window-tree-window))
555 (unless walk-window-tree-buffer
556 (walk-window-tree-1
557 fun (window-left-child walk-window-tree-window) any)
558 (walk-window-tree-1
559 fun (window-top-child walk-window-tree-window) any))
560 (if sub-only
561 (setq walk-window-tree-window nil)
562 (setq walk-window-tree-window
563 (window-right walk-window-tree-window))))))
565 (defun walk-window-tree (fun &optional frame any minibuf)
566 "Run function FUN on each live window of FRAME.
567 FUN must be a function with one argument - a window. FRAME must
568 be a live frame and defaults to the selected one. ANY, if
569 non-nil, means to run FUN on all live and internal windows of
570 FRAME.
572 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
573 window even if it isn't active. MINIBUF nil or omitted means run
574 FUN on FRAME's minibuffer window only if it's active. In both
575 cases the minibuffer window must be part of FRAME. MINIBUF
576 neither nil nor t means never run FUN on the minibuffer window.
578 This function performs a pre-order, depth-first traversal of the
579 window tree. If FUN changes the window tree, the result is
580 unpredictable."
581 (setq frame (window-normalize-frame frame))
582 (walk-window-tree-1 fun (frame-root-window frame) any)
583 (when (memq minibuf '(nil t))
584 ;; Run FUN on FRAME's minibuffer window if requested.
585 (let ((minibuffer-window (minibuffer-window frame)))
586 (when (and (window-live-p minibuffer-window)
587 (eq (window-frame minibuffer-window) frame)
588 (or (eq minibuf t)
589 (minibuffer-window-active-p minibuffer-window)))
590 (funcall fun minibuffer-window)))))
592 (defun walk-window-subtree (fun &optional window any)
593 "Run function FUN on the subtree of windows rooted at WINDOW.
594 WINDOW defaults to the selected window. FUN must be a function
595 with one argument - a window. By default, run FUN only on live
596 windows of the subtree. If the optional argument ANY is non-nil,
597 run FUN on all live and internal windows of the subtree. If
598 WINDOW is live, run FUN on WINDOW only.
600 This function performs a pre-order, depth-first traversal of the
601 subtree rooted at WINDOW. If FUN changes that tree, the result
602 is unpredictable."
603 (setq window (window-normalize-window window))
604 (walk-window-tree-1 fun window any t))
606 (defun window-with-parameter (parameter &optional value frame any minibuf)
607 "Return first window on FRAME with PARAMETER non-nil.
608 FRAME defaults to the selected frame. Optional argument VALUE
609 non-nil means only return a window whose window-parameter value
610 for PARAMETER equals VALUE (comparison is done with `equal').
611 Optional argument ANY non-nil means consider internal windows
612 too.
614 Optional argument MINIBUF t means consider FRAME's minibuffer
615 window even if it isn't active. MINIBUF nil or omitted means
616 consider FRAME's minibuffer window only if it's active. In both
617 cases the minibuffer window must be part of FRAME. MINIBUF
618 neither nil nor t means never consider the minibuffer window."
619 (let (this-value)
620 (catch 'found
621 (walk-window-tree
622 (lambda (window)
623 (when (and (setq this-value (window-parameter window parameter))
624 (or (not value) (equal value this-value)))
625 (throw 'found window)))
626 frame any minibuf))))
628 ;;; Atomic windows.
629 (defun window-atom-root (&optional window)
630 "Return root of atomic window WINDOW is a part of.
631 WINDOW must be a valid window and defaults to the selected one.
632 Return nil if WINDOW is not part of an atomic window."
633 (setq window (window-normalize-window window))
634 (let (root)
635 (while (and window (window-parameter window 'window-atom))
636 (setq root window)
637 (setq window (window-parent window)))
638 root))
640 (defun window-make-atom (window)
641 "Make WINDOW an atomic window.
642 WINDOW must be an internal window. Return WINDOW."
643 (if (not (window-child window))
644 (error "Window %s is not an internal window" window)
645 (walk-window-subtree
646 (lambda (window)
647 (unless (window-parameter window 'window-atom)
648 (set-window-parameter window 'window-atom t)))
649 window t)
650 window))
652 (defun display-buffer-in-atom-window (buffer alist)
653 "Display BUFFER in an atomic window.
654 This function displays BUFFER in a new window that will be
655 combined with an existing window to form an atomic window. If
656 the existing window is already part of an atomic window, add the
657 new window to that atomic window. Operations like `split-window'
658 or `delete-window', when applied to a constituent of an atomic
659 window, are applied atomically to the root of that atomic window.
661 ALIST is an association list of symbols and values. The
662 following symbols can be used.
664 `window' specifies the existing window the new window shall be
665 combined with. Use `window-atom-root' to make the new window a
666 sibling of an atomic window's root. If an internal window is
667 specified here, all children of that window become part of the
668 atomic window too. If no window is specified, the new window
669 becomes a sibling of the selected window. By default, the
670 `window-atom' parameter of the existing window is set to `main'
671 provided it is live and was not set before.
673 `side' denotes the side of the existing window where the new
674 window shall be located. Valid values are `below', `right',
675 `above' and `left'. The default is `below'. By default, the
676 `window-atom' parameter of the new window is set to this value.
678 The return value is the new window, nil when creating that window
679 failed."
680 (let* ((ignore-window-parameters t)
681 (window-combination-limit t)
682 (window-combination-resize 'atom)
683 (window (cdr (assq 'window alist)))
684 (side (or (cdr (assq 'side alist)) 'below))
685 (atom (when window (window-parameter window 'window-atom)))
686 root new)
687 (setq window (window-normalize-window window))
688 (setq root (window-atom-root window))
689 ;; Split off new window.
690 (when (setq new (split-window-no-error window nil side))
691 (window-make-atom
692 (if (and root (not (eq root window)))
693 ;; When WINDOW was part of an atomic window and we did not
694 ;; split its root, root atomic window at old root.
695 root
696 ;; Otherwise, root atomic window at WINDOW's new parent.
697 (window-parent window)))
698 ;; Assign `window-atom' parameters, if needed.
699 (when (and (not atom) (window-live-p window))
700 (set-window-parameter window 'window-atom 'main))
701 (set-window-parameter new 'window-atom side)
702 ;; Display BUFFER in NEW and return NEW.
703 (window--display-buffer
704 buffer new 'window alist display-buffer-mark-dedicated))))
706 (defun window--atom-check-1 (window)
707 "Subroutine of `window--atom-check'."
708 (when window
709 (if (window-parameter window 'window-atom)
710 (let ((count 0))
711 (when (or (catch 'reset
712 (walk-window-subtree
713 (lambda (window)
714 (if (window-parameter window 'window-atom)
715 (setq count (1+ count))
716 (throw 'reset t)))
717 window t))
718 ;; count >= 1 must hold here. If there's no other
719 ;; window around dissolve this atomic window.
720 (= count 1))
721 ;; Dissolve atomic window.
722 (walk-window-subtree
723 (lambda (window)
724 (set-window-parameter window 'window-atom nil))
725 window t)))
726 ;; Check children.
727 (unless (window-buffer window)
728 (window--atom-check-1 (window-left-child window))
729 (window--atom-check-1 (window-top-child window))))
730 ;; Check right sibling
731 (window--atom-check-1 (window-right window))))
733 (defun window--atom-check (&optional frame)
734 "Check atomicity of all windows on FRAME.
735 FRAME defaults to the selected frame. If an atomic window is
736 wrongly configured, reset the atomicity of all its windows on
737 FRAME to nil. An atomic window is wrongly configured if it has
738 no child windows or one of its child windows is not atomic."
739 (window--atom-check-1 (frame-root-window frame)))
741 ;; Side windows.
742 (defcustom window-sides-vertical nil
743 "If non-nil, left and right side windows occupy full frame height.
744 If nil, top and bottom side windows occupy full frame width."
745 :type 'boolean
746 :initialize 'custom-initialize-default
747 :set 'window--sides-verticalize
748 :group 'windows
749 :version "26.1")
751 (defcustom window-sides-reversed nil
752 "Whether top/bottom side windows appear in reverse order.
753 When this is nil, side windows on the top and bottom of a frame
754 are always drawn from left to right with increasing slot values.
755 When this is t, side windows on the top and bottom of a frame are
756 always drawn from right to left with increasing slot values.
758 When this is `bidi', the drawing order is like that for the value
759 t if the value of `bidi-paragraph-direction' is `right-to-left'
760 in the buffer most recently shown in the window selected within
761 the main window area of this frame.
763 The layout of side windows on the left or right of a frame is not
764 affected by the value of this variable."
765 :type
766 '(choice (const :tag "Never" nil)
767 (const :tag "Bidi" bidi)
768 (const :tag "Always" t))
769 :initialize 'custom-initialize-default
770 :set 'window--sides-reverse
771 :group 'windows
772 :version "26.1")
774 (defcustom window-sides-slots '(nil nil nil nil)
775 "Number of available side window slots on each side of a frame.
776 The value is a list of four elements specifying the maximum
777 number of side windows that may be created on the left, top,
778 right and bottom side of any frame.
780 If an element is a number, `display-buffer-in-side-window' will
781 refrain from making a new side window if the number of windows on
782 that side is equal to or exceeds that number. Rather, it will
783 reuse the window whose `window-slot' value is nearest to the slot
784 specified via its ALIST argument. If an element is nil, this
785 means there's no bound on the number of windows on that side."
786 :version "24.1"
787 :risky t
788 :type
789 '(list
790 :value (nil nil nil nil)
791 (choice
792 :tag "Left"
793 :help-echo "Maximum number of left side windows."
794 :value nil
795 :format "%[Left%] %v\n"
796 (const :tag "Unlimited" :format "%t" nil)
797 (integer :tag "Number" :value 2 :size 5))
798 (choice
799 :tag "Top"
800 :help-echo "Maximum number of top side windows."
801 :value nil
802 :format "%[Top%] %v\n"
803 (const :tag "Unlimited" :format "%t" nil)
804 (integer :tag "Number" :value 3 :size 5))
805 (choice
806 :tag "Right"
807 :help-echo "Maximum number of right side windows."
808 :value nil
809 :format "%[Right%] %v\n"
810 (const :tag "Unlimited" :format "%t" nil)
811 (integer :tag "Number" :value 2 :size 5))
812 (choice
813 :tag "Bottom"
814 :help-echo "Maximum number of bottom side windows."
815 :value nil
816 :format "%[Bottom%] %v\n"
817 (const :tag "Unlimited" :format "%t" nil)
818 (integer :tag "Number" :value 3 :size 5)))
819 :group 'windows)
821 (defvar-local window--sides-shown nil
822 "Non-nil if this buffer was shown in a side window once.
823 If this variable is non-nil in a buffer, `switch-to-prev-buffer'
824 and `switch-to-next-buffer' will refrain from showing this buffer
825 within the main window area. `display-buffer-in-side-window'
826 sets this variable automatically.
828 Killing buffer local variables after showing the buffer in a side
829 window annihilates any effect provided by this variable.")
831 (defvar window--sides-inhibit-check nil
832 "Non-nil means inhibit any checks on side windows.")
834 (defun window--sides-reverse-on-frame-p (frame)
835 "Return non-nil when side windows should appear reversed on FRAME.
836 This uses some heuristics to guess the user's intentions when the
837 selected window of FRAME is a side window ."
838 (cond
839 ;; Reverse when `window-sides-reversed' is t. Do not reverse when
840 ;; `window-sides-reversed' is nil.
841 ((memq window-sides-reversed '(nil t))
842 window-sides-reversed)
843 ;; Reverse when FRAME's selected window shows a right-to-left buffer.
844 ((let ((window (frame-selected-window frame)))
845 (when (and (not (window-parameter window 'window-side))
846 (or (not (window-minibuffer-p window))
847 (setq window (minibuffer-selected-window))))
848 (with-current-buffer (window-buffer window)
849 (eq bidi-paragraph-direction 'right-to-left)))))
850 ;; Reverse when FRAME's `window-sides-main-selected-window' parameter
851 ;; specifies a live window showing a right-to-left buffer.
852 ((let ((window (frame-parameter
853 frame 'window-sides-main-selected-window)))
854 (when (window-live-p window)
855 (with-current-buffer (window-buffer window)
856 (eq bidi-paragraph-direction 'right-to-left)))))
857 ;; Reverse when all windows in FRAME's main window show right-to-left
858 ;; buffers.
860 (catch 'found
861 (walk-window-subtree
862 (lambda (window)
863 (with-current-buffer (window-buffer window)
864 (when (eq bidi-paragraph-direction 'left-to-right)
865 (throw 'found nil))))
866 (window-main-window frame))
867 t))))
869 (defun window-main-window (&optional frame)
870 "Return the main window of specified FRAME.
871 The optional argument FRAME must be a live frame and defaults to
872 the selected one.
874 If FRAME has no side windows, return FRAME's root window.
875 Otherwise, return either an internal non-side window such that
876 all other non-side windows on FRAME descend from it, or the
877 single live non-side window of FRAME."
878 (let ((frame (window-normalize-frame frame))
879 main sibling)
880 ;; Set main to the _last_ window found by `walk-window-tree' that
881 ;; is not a side window but has a side window as its sibling.
882 (walk-window-tree
883 (lambda (window)
884 (and (not (window-parameter window 'window-side))
885 (or (and (setq sibling (window-prev-sibling window))
886 (window-parameter sibling 'window-side))
887 (and (setq sibling (window-next-sibling window))
888 (window-parameter sibling 'window-side)))
889 (setq main window)))
890 frame t 'nomini)
891 (or main (frame-root-window frame))))
893 (defun window--make-major-side-window-next-to (side)
894 "Return window to split for making a major side window.
895 SIDE must be one of the symbols `left', `top', `right' or
896 `bottom'.
898 This is an auxiliary function of `window--make-major-side-window'
899 and must not be called when a window on SIDE exists already."
900 (let ((root (frame-root-window))
901 (window--sides-inhibit-check t)
902 window)
903 ;; (1) If a window on the opposite side exists, return that window's
904 ;; sibling.
905 ;; (2) If the new window shall span the entire side, return the
906 ;; frame's root window.
907 ;; (3) If a window on an orthogonal side exists, return that
908 ;; window's sibling.
909 ;; (4) Otherwise return the frame's root window.
910 (cond
911 ((or (and (eq side 'left)
912 (setq window (window-with-parameter 'window-side 'right nil t)))
913 (and (eq side 'top)
914 (setq window (window-with-parameter 'window-side 'bottom nil t))))
915 (window-prev-sibling window))
916 ((or (and (eq side 'right)
917 (setq window (window-with-parameter 'window-side 'left nil t)))
918 (and (eq side 'bottom)
919 (setq window (window-with-parameter 'window-side 'top nil t))))
920 (window-next-sibling window))
921 ((memq side '(left right))
922 (cond
923 (window-sides-vertical
924 root)
925 ((setq window (window-with-parameter 'window-side 'top nil t))
926 (window-next-sibling window))
927 ((setq window (window-with-parameter 'window-side 'bottom nil t))
928 (window-prev-sibling window))
929 (t root)))
930 ((memq side '(top bottom))
931 (cond
932 ((not window-sides-vertical)
933 root)
934 ((setq window (window-with-parameter 'window-side 'left nil t))
935 (window-next-sibling window))
936 ((setq window (window-with-parameter 'window-side 'right nil t))
937 (window-prev-sibling window))
938 (t root))))))
940 (defun window--make-major-side-window (buffer side slot &optional alist)
941 "Display BUFFER in a new major side window on the selected frame.
942 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
943 specifies the slot to use. ALIST is an association list of
944 symbols and values as passed to `display-buffer-in-side-window'.
945 Return the new window, nil if its creation failed.
947 This is an auxiliary function of `display-buffer-in-side-window'
948 and may be called only if no window on SIDE exists yet."
949 (let* ((left-or-right (memq side '(left right)))
950 (next-to (window--make-major-side-window-next-to side))
951 (on-side (cond
952 ((eq side 'top) 'above)
953 ((eq side 'bottom) 'below)
954 (t side)))
955 (window--sides-inhibit-check t)
956 ;; The following two bindings will tell `split-window' to take
957 ;; the space for the new window from the selected frame's main
958 ;; window and not make a new parent window unless needed.
959 (window-combination-resize 'side)
960 (window-combination-limit nil)
961 (window (split-window-no-error next-to nil on-side)))
962 (when window
963 ;; Initialize `window-side' parameter of new window to SIDE and
964 ;; make that parameter persistent.
965 (set-window-parameter window 'window-side side)
966 (add-to-list 'window-persistent-parameters '(window-side . writable))
967 ;; Install `window-slot' parameter of new window and make that
968 ;; parameter persistent.
969 (set-window-parameter window 'window-slot slot)
970 (add-to-list 'window-persistent-parameters '(window-slot . writable))
971 ;; Auto-adjust height/width of new window unless a size has been
972 ;; explicitly requested.
973 (unless (if left-or-right
974 (cdr (assq 'window-width alist))
975 (cdr (assq 'window-height alist)))
976 (setq alist
977 (cons
978 (cons
979 (if left-or-right 'window-width 'window-height)
980 (/ (window-total-size (frame-root-window) left-or-right)
981 ;; By default use a fourth of the size of the frame's
982 ;; root window.
984 alist)))
985 (with-current-buffer buffer
986 (setq window--sides-shown t))
987 ;; Install BUFFER in new window and return WINDOW.
988 (window--display-buffer buffer window 'window alist 'side))))
990 (defun display-buffer-in-side-window (buffer alist)
991 "Display BUFFER in a side window of the selected frame.
992 ALIST is an association list of symbols and values. The
993 following special symbols can be used in ALIST.
995 `side' denotes the side of the frame where the new window shall
996 be located. Valid values are `bottom', `right', `top' and
997 `left'. The default is `bottom'.
999 `slot' if non-nil, specifies the window slot where to display
1000 BUFFER. A value of zero or nil means use the middle slot on
1001 the specified side. A negative value means use a slot
1002 preceding (that is, above or on the left of) the middle slot.
1003 A positive value means use a slot following (that is, below or
1004 on the right of) the middle slot. The default is zero.
1006 If the current frame size or the settings of `window-sides-slots'
1007 do not permit making a new window, a suitable existing window may
1008 be reused and have its `window-slot' parameter value accordingly
1009 modified.
1011 Unless `display-buffer-mark-dedicated' is non-nil, dedicate the
1012 side window used to BUFFER so that it does not get reused by
1013 other `display-buffer' action functions. Return the window used
1014 for displaying BUFFER, nil if no suitable window can be found.
1016 This function installs the `window-side' and `window-slot'
1017 parameters and makes them persistent. It neither modifies ALIST
1018 nor installs any other window parameters unless they have been
1019 explicitly provided via a `window-parameters' entry in ALIST."
1020 (let* ((side (or (cdr (assq 'side alist)) 'bottom))
1021 (slot (or (cdr (assq 'slot alist)) 0))
1022 (left-or-right (memq side '(left right)))
1023 ;; Softly dedicate window to BUFFER unless
1024 ;; `display-buffer-mark-dedicated' already asks for it.
1025 (dedicated (or display-buffer-mark-dedicated 'side)))
1026 (cond
1027 ((not (memq side '(top bottom left right)))
1028 (error "Invalid side %s specified" side))
1029 ((not (numberp slot))
1030 (error "Invalid slot %s specified" slot)))
1032 (let* ((major (window-with-parameter 'window-side side nil t))
1033 ;; `major' is the major window on SIDE, `windows' the list of
1034 ;; life windows on SIDE.
1035 (reversed (window--sides-reverse-on-frame-p (selected-frame)))
1036 (windows
1037 (cond
1038 ((window-live-p major)
1039 (list major))
1040 ((window-valid-p major)
1041 (let* ((first (window-child major))
1042 (next (window-next-sibling first))
1043 (windows (list next first)))
1044 (setq reversed (> (window-parameter first 'window-slot)
1045 (window-parameter next 'window-slot)))
1046 (while (setq next (window-next-sibling next))
1047 (setq windows (cons next windows)))
1048 (if reversed windows (nreverse windows))))))
1049 (slots (when major (max 1 (window-child-count major))))
1050 (max-slots
1051 (nth (cond
1052 ((eq side 'left) 0)
1053 ((eq side 'top) 1)
1054 ((eq side 'right) 2)
1055 ((eq side 'bottom) 3))
1056 window-sides-slots))
1057 (window--sides-inhibit-check t)
1058 window this-window this-slot prev-window next-window
1059 best-window best-slot abs-slot)
1061 (cond
1062 ((and (numberp max-slots) (<= max-slots 0))
1063 ;; No side-slots available on this side. Don't raise an error,
1064 ;; just return nil.
1065 nil)
1066 ((not windows)
1067 ;; No major side window exists on this side, make one.
1068 (window--make-major-side-window buffer side slot alist))
1070 ;; Scan windows on SIDE.
1071 (catch 'found
1072 (dolist (window windows)
1073 (setq this-slot (window-parameter window 'window-slot))
1074 (cond
1075 ;; The following should not happen and probably be checked
1076 ;; by window--sides-check.
1077 ((not (numberp this-slot)))
1078 ((= this-slot slot)
1079 ;; A window with a matching slot has been found.
1080 (setq this-window window)
1081 (throw 'found t))
1083 ;; Check if this window has a better slot value wrt the
1084 ;; slot of the window we want.
1085 (setq abs-slot
1086 (if (or (and (> this-slot 0) (> slot 0))
1087 (and (< this-slot 0) (< slot 0)))
1088 (abs (- slot this-slot))
1089 (+ (abs slot) (abs this-slot))))
1090 (unless (and best-slot (<= best-slot abs-slot))
1091 (setq best-window window)
1092 (setq best-slot abs-slot))
1093 (if reversed
1094 (cond
1095 ((<= this-slot slot)
1096 (setq next-window window))
1097 ((not prev-window)
1098 (setq prev-window window)))
1099 (cond
1100 ((<= this-slot slot)
1101 (setq prev-window window))
1102 ((not next-window)
1103 (setq next-window window))))))))
1105 ;; `this-window' is the first window with the same SLOT.
1106 ;; `prev-window' is the window with the largest slot < SLOT. A new
1107 ;; window will be created after it.
1108 ;; `next-window' is the window with the smallest slot > SLOT. A new
1109 ;; window will be created before it.
1110 ;; `best-window' is the window with the smallest absolute difference
1111 ;; of its slot and SLOT.
1112 (or (and this-window
1113 ;; Reuse `this-window'.
1114 (with-current-buffer buffer
1115 (setq window--sides-shown t))
1116 (window--display-buffer
1117 buffer this-window 'reuse alist dedicated))
1118 (and (or (not max-slots) (< slots max-slots))
1119 (or (and next-window
1120 ;; Make new window before `next-window'.
1121 (let ((next-side (if left-or-right 'above 'left))
1122 (window-combination-resize 'side))
1123 (setq window (split-window-no-error
1124 next-window nil next-side))))
1125 (and prev-window
1126 ;; Make new window after `prev-window'.
1127 (let ((prev-side (if left-or-right 'below 'right))
1128 (window-combination-resize 'side))
1129 (setq window (split-window-no-error
1130 prev-window nil prev-side)))))
1131 (set-window-parameter window 'window-slot slot)
1132 (with-current-buffer buffer
1133 (setq window--sides-shown t))
1134 (window--display-buffer
1135 buffer window 'window alist dedicated))
1136 (and best-window
1137 ;; Reuse `best-window'.
1138 (progn
1139 ;; Give best-window the new slot value.
1140 (set-window-parameter best-window 'window-slot slot)
1141 (with-current-buffer buffer
1142 (setq window--sides-shown t))
1143 (window--display-buffer
1144 buffer best-window 'reuse alist dedicated)))))))))
1146 (defun window-toggle-side-windows (&optional frame)
1147 "Toggle side windows on specified FRAME.
1148 FRAME must be a live frame and defaults to the selected one.
1150 If FRAME has at least one side window, save FRAME's state in the
1151 FRAME's `window-state' frame parameter and delete all side
1152 windows on FRAME afterwards. Otherwise, if FRAME has a
1153 `window-state' parameter, use that to restore any side windows on
1154 FRAME leaving FRAME's main window alone. Signal an error if
1155 FRAME has no side window and no saved state is found."
1156 (interactive)
1157 (let* ((frame (window-normalize-frame frame))
1158 (window--sides-inhibit-check t)
1159 state)
1160 (cond
1161 ((window-with-parameter 'window-side nil frame)
1162 ;; At least one side window exists. Remove all side windows after
1163 ;; saving FRAME's state in its `window-state' parameter.
1164 (set-frame-parameter
1165 frame 'window-state (window-state-get (frame-root-window frame)))
1166 (let ((ignore-window-parameters t))
1167 (delete-other-windows (window-main-window frame))))
1168 ((setq state (frame-parameter frame 'window-state))
1169 ;; A window state was saved for FRAME. Restore it and put the
1170 ;; current root window into its main window.
1171 (let ((main-state (window-state-get (frame-root-window frame))))
1172 (window-state-put state (frame-root-window frame) t)
1173 (window-state-put main-state (window-main-window frame)))
1174 (window--sides-reverse-frame frame))
1176 (error "No side windows state found")))))
1178 (defun window--sides-reverse-all ()
1179 "Maybe reverse side windows on all frames."
1180 (unless window--sides-inhibit-check
1181 (dolist (frame (frame-list))
1182 (window--sides-reverse-frame frame))))
1184 (defun window--sides-reverse-frame (frame)
1185 "Maybe reverse side windows on FRAME."
1186 (when (eq window-sides-reversed 'bidi)
1187 (let ((window (frame-selected-window frame)))
1188 (unless (or (window-parameter window 'window-side)
1189 (window-minibuffer-p window))
1190 (set-frame-parameter
1191 frame 'window-sides-main-selected-window window))))
1192 (window--sides-reverse-side frame 'top)
1193 (window--sides-reverse-side frame 'bottom))
1195 (defun window--sides-reverse-side (frame side)
1196 "Maybe reverse windows on SIDE of FRAME."
1197 (let ((major (window-with-parameter 'window-side side frame t))
1198 (window--sides-inhibit-check t))
1199 (when (and major (not (window-live-p major)))
1200 (let* ((first (window-child major))
1201 (reversed (> (window-parameter first 'window-slot)
1202 (window-parameter
1203 (window-next-sibling first) 'window-slot)))
1204 (reverse (window--sides-reverse-on-frame-p frame)))
1205 (unless (eq reversed reverse)
1206 ;; We have to reverse.
1207 (let ((last (window-last-child major)))
1208 (while (and (not (eq first last))
1209 (not (eq first (window-next-sibling last))))
1210 (window-swap-states first last t)
1211 (setq first (window-next-sibling first))
1212 (setq last (window-prev-sibling last)))))))))
1214 (defun window--sides-reverse (symbol value)
1215 "Helper function for customizing `window-sides-reversed'."
1216 (set-default symbol value)
1217 (remove-hook 'buffer-list-update-hook 'window--sides-reverse-all)
1218 (remove-hook 'window-configuration-change-hook 'window--sides-reverse-all)
1219 (dolist (frame (frame-list))
1220 (set-frame-parameter frame 'window-sides-main-selected-window nil))
1221 (when (eq value 'bidi)
1222 (add-hook 'buffer-list-update-hook 'window--sides-reverse-all)
1223 (add-hook 'window-configuration-change-hook 'window--sides-reverse-all))
1224 (window--sides-reverse-all))
1226 (defun window--sides-verticalize-frame (&optional frame)
1227 "Maybe change side windows layout on specified FRAME."
1228 (setq frame (window-normalize-frame frame))
1229 (let ((window--sides-inhibit-check t)
1230 (root (frame-root-window frame))
1231 (main (window-main-window frame)))
1232 (when (and (not (eq main root))
1233 (not (eq (window-parent main) root))
1234 (window-combined-p main window-sides-vertical))
1235 (let* ((window--sides-inhibit-check t)
1236 (ignore-window-parameters t)
1237 (first (window-child root))
1238 (first-state
1239 (and first (window-parameter first 'window-side)
1240 (window-state-get first)))
1241 (last (window-last-child root))
1242 (last-state
1243 (and last (window-parameter last 'window-side)
1244 (window-state-get last)))
1245 (dummy (get-buffer-create " *dummy*"))
1246 major)
1247 (unwind-protect
1248 (progn
1249 (when first-state (delete-window first))
1250 (when last-state (delete-window last))
1251 (when first-state
1252 (setq major (window--make-major-side-window
1253 dummy (if window-sides-vertical 'top 'left) 0))
1254 (window-state-put first-state major t))
1255 (when last-state
1256 (setq major (window--make-major-side-window
1257 dummy (if window-sides-vertical 'bottom 'right) 0))
1258 (window-state-put last-state major t)))
1259 (kill-buffer " *dummy*"))))))
1261 (defun window--sides-verticalize (symbol value)
1262 "Helper function for customizing `window-sides-vertical'."
1263 (set-default symbol value)
1264 (dolist (frame (frame-list))
1265 (window--sides-verticalize-frame frame)))
1267 (defun window--sides-check-failed (frame)
1268 "Helper function for `window--sides-check'."
1269 (catch 'failed
1270 ;; FRAME must have a main window.
1271 (unless (window-main-window frame)
1272 (error "Frame %s has no main window" frame)
1273 (throw 'failed t))
1274 ;; Now check the side windows.
1275 (dolist (side '(left top right bottom))
1276 (let ((window (window-with-parameter 'window-side side frame t)))
1277 (when window
1278 ;; If WINDOW is live there must be no other window on this frame
1279 ;; with the same `window-side' parameter.
1280 (if (window-live-p window)
1281 (walk-window-tree
1282 (lambda (this)
1283 (when (and (eq (window-parameter this 'window-side) side)
1284 (not (eq this window)))
1285 (error "Window %s has same side %s as window %s but no common parent"
1286 this side window)
1287 (throw 'failed t)))
1288 frame t 'nomini)
1289 (walk-window-tree
1290 (lambda (this)
1291 (if (eq (window-parent this) window)
1292 (unless (eq (window-parameter this 'window-side) side)
1293 (error "Window %s has not same side %s as its parent %s"
1294 this side window)
1295 (throw 'failed t))
1296 (when (and (eq (window-parameter this 'window-side) side)
1297 (not (eq this window)))
1298 (error "Window %s has same side %s as major side window %s but its parent is %s"
1299 this side window (window-parent this))
1300 (throw 'failed t))))
1301 frame t 'nomini)))))))
1303 (defun window--sides-check (frame)
1304 "Check side windows configuration of FRAME.
1305 In a valid side windows configuration there can be at most one
1306 internal side window on each side and all its children must be
1307 live and have the same `window-side' parameter and no other
1308 window with the same `window-side' parameter exists on FRAME. If
1309 there is no such internal window, there may be at most one window
1310 with this side's `window-side' parameter on FRAME.
1312 If the configuration is invalid, reset the `window-side'
1313 parameters of all windows on FRAME."
1314 (when (and (not window--sides-inhibit-check)
1315 (window-with-parameter 'window-side nil frame t)
1316 (window--sides-check-failed frame))
1317 ;; Reset all `window-side' parameters.
1318 (walk-window-tree
1319 (lambda (window)
1320 (set-window-parameter window 'window-side nil))
1321 frame t 'nomini)
1322 (message "Side windows configuration reset for frame %s" frame)))
1324 (defun window--check (&optional frame)
1325 "Check atomic and side windows on FRAME.
1326 FRAME defaults to the selected frame."
1327 (window--sides-check frame)
1328 (window--atom-check frame))
1330 ;; Dumping frame/window contents.
1331 (defun window--dump-window (&optional window erase)
1332 "Dump WINDOW to buffer *window-frame-dump*.
1333 WINDOW must be a valid window and defaults to the selected one.
1334 Optional argument ERASE non-nil means erase *window-frame-dump*
1335 before writing to it."
1336 (setq window (window-normalize-window window))
1337 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1338 (when erase (erase-buffer))
1339 (insert
1340 (format "%s parent: %s\n" window (window-parent window))
1341 (format "pixel left: %s top: %s size: %s x %s new: %s\n"
1342 (window-pixel-left window) (window-pixel-top window)
1343 (window-size window t t) (window-size window nil t)
1344 (window-new-pixel window))
1345 (format "char left: %s top: %s size: %s x %s new: %s\n"
1346 (window-left-column window) (window-top-line window)
1347 (window-total-size window t) (window-total-size window)
1348 (window-new-total window))
1349 (format "normal: %s x %s new: %s\n"
1350 (window-normal-size window t) (window-normal-size window)
1351 (window-new-normal window)))
1352 (when (window-live-p window)
1353 (let ((fringes (window-fringes window))
1354 (margins (window-margins window)))
1355 (insert
1356 (format "body pixel: %s x %s char: %s x %s\n"
1357 (window-body-width window t) (window-body-height window t)
1358 (window-body-width window) (window-body-height window))
1359 (format "width left fringe: %s left margin: %s right margin: %s\n"
1360 (car fringes) (or (car margins) 0) (or (cdr margins) 0))
1361 (format "width right fringe: %s scroll-bar: %s divider: %s\n"
1362 (cadr fringes)
1363 (window-scroll-bar-width window)
1364 (window-right-divider-width window))
1365 (format "height header-line: %s mode-line: %s divider: %s\n"
1366 (window-header-line-height window)
1367 (window-mode-line-height window)
1368 (window-bottom-divider-width window)))))
1369 (insert "\n")))
1371 (defun window--dump-frame (&optional window-or-frame)
1372 "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
1373 WINDOW-OR-FRAME can be a frame or a window and defaults to the
1374 selected frame. When WINDOW-OR-FRAME is a window, dump that
1375 window's frame. The buffer *window-frame-dump* is erased before
1376 dumping to it."
1377 (let* ((window
1378 (cond
1379 ((or (not window-or-frame)
1380 (frame-live-p window-or-frame))
1381 (frame-root-window window-or-frame))
1382 ((or (window-live-p window-or-frame)
1383 (window-child window-or-frame))
1384 window-or-frame)
1386 (frame-root-window))))
1387 (frame (window-frame window)))
1388 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1389 (erase-buffer)
1390 (insert
1391 (format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
1392 (frame-pixel-width frame) (frame-pixel-height frame)
1393 (frame-total-cols frame) (frame-total-lines frame)
1394 (frame-char-width frame) (frame-char-height frame))
1395 (format "frame text pixel: %s x %s cols/lines: %s x %s\n"
1396 (frame-text-width frame) (frame-text-height frame)
1397 (frame-text-cols frame) (frame-text-lines frame))
1398 (format "tool: %s scroll: %s/%s fringe: %s border: %s right: %s bottom: %s\n\n"
1399 (if (fboundp 'tool-bar-height)
1400 (tool-bar-height frame t)
1401 "0")
1402 (frame-scroll-bar-width frame)
1403 (frame-scroll-bar-height frame)
1404 (frame-fringe-width frame)
1405 (frame-border-width frame)
1406 (frame-right-divider-width frame)
1407 (frame-bottom-divider-width frame)))
1408 (walk-window-tree 'window--dump-window frame t t))))
1410 ;;; Window sizes.
1411 (defun window-total-size (&optional window horizontal round)
1412 "Return the total height or width of WINDOW.
1413 WINDOW must be a valid window and defaults to the selected one.
1415 If HORIZONTAL is omitted or nil, return the total height of
1416 WINDOW, in lines. If WINDOW is live, its total height includes,
1417 in addition to the height of WINDOW's text, the heights of
1418 WINDOW's mode and header line and a bottom divider, if any.
1420 If HORIZONTAL is non-nil, return the total width of WINDOW, in
1421 columns. If WINDOW is live, its total width includes, in
1422 addition to the width of WINDOW's text, the widths of WINDOW's
1423 fringes, margins, scroll bars and its right divider, if any.
1425 If WINDOW is internal, return the respective size of the screen
1426 areas spanned by its children.
1428 Optional argument ROUND is handled as for `window-total-height'
1429 and `window-total-width'."
1430 (if horizontal
1431 (window-total-width window round)
1432 (window-total-height window round)))
1434 (defun window-size (&optional window horizontal pixelwise round)
1435 "Return the height or width of WINDOW.
1436 WINDOW must be a valid window and defaults to the selected one.
1438 If HORIZONTAL is omitted or nil, return the total height of
1439 WINDOW, in lines, like `window-total-height'. Otherwise return
1440 the total width, in columns, like `window-total-width'.
1442 Optional argument PIXELWISE means return the pixel size of WINDOW
1443 like `window-pixel-height' and `window-pixel-width'.
1445 Optional argument ROUND is ignored if PIXELWISE is non-nil and
1446 handled as for `window-total-height' and `window-total-width'
1447 otherwise."
1448 (if horizontal
1449 (if pixelwise
1450 (window-pixel-width window)
1451 (window-total-width window round))
1452 (if pixelwise
1453 (window-pixel-height window)
1454 (window-total-height window round))))
1456 (defvar window-size-fixed nil
1457 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
1458 If the value is `height', then only the window's height is fixed.
1459 If the value is `width', then only the window's width is fixed.
1460 Any other non-nil value fixes both the width and the height.
1462 Emacs won't change the size of any window displaying that buffer,
1463 unless it has no other choice (like when deleting a neighboring
1464 window).")
1465 (make-variable-buffer-local 'window-size-fixed)
1467 (defun window--preservable-size (window &optional horizontal)
1468 "Return height of WINDOW as `window-preserve-size' would preserve it.
1469 Optional argument HORIZONTAL non-nil means to return the width of
1470 WINDOW as `window-preserve-size' would preserve it."
1471 (if horizontal
1472 (window-body-width window t)
1473 (+ (window-body-height window t)
1474 (window-header-line-height window)
1475 (window-mode-line-height window))))
1477 (defun window-preserve-size (&optional window horizontal preserve)
1478 "Preserve height of window WINDOW.
1479 WINDOW must be a live window and defaults to the selected one.
1480 Optional argument HORIZONTAL non-nil means preserve the width of
1481 WINDOW.
1483 PRESERVE t means to preserve the current height/width of WINDOW's
1484 body in frame and window resizing operations whenever possible.
1485 The height/width of WINDOW will change only if Emacs has no other
1486 choice. Resizing a window whose height/width is preserved never
1487 throws an error.
1489 PRESERVE nil means to stop preserving the height/width of WINDOW,
1490 lifting the respective restraint induced by a previous call of
1491 `window-preserve-size' for WINDOW. Calling `enlarge-window',
1492 `shrink-window', `split-window' or `fit-window-to-buffer' with
1493 WINDOW as argument also removes the respective restraint.
1495 Other values of PRESERVE are reserved for future use."
1496 (setq window (window-normalize-window window t))
1497 (let* ((parameter (window-parameter window 'window-preserved-size))
1498 (width (nth 1 parameter))
1499 (height (nth 2 parameter)))
1500 (if horizontal
1501 (set-window-parameter
1502 window 'window-preserved-size
1503 (list
1504 (window-buffer window)
1505 (and preserve (window--preservable-size window t))
1506 height))
1507 (set-window-parameter
1508 window 'window-preserved-size
1509 (list
1510 (window-buffer window)
1511 width
1512 (and preserve (window--preservable-size window)))))))
1514 (defun window-preserved-size (&optional window horizontal)
1515 "Return preserved height of window WINDOW.
1516 WINDOW must be a live window and defaults to the selected one.
1517 Optional argument HORIZONTAL non-nil means to return preserved
1518 width of WINDOW."
1519 (setq window (window-normalize-window window t))
1520 (let* ((parameter (window-parameter window 'window-preserved-size))
1521 (buffer (nth 0 parameter))
1522 (width (nth 1 parameter))
1523 (height (nth 2 parameter)))
1524 (when (eq buffer (window-buffer window))
1525 (if horizontal width height))))
1527 (defun window--preserve-size (window horizontal)
1528 "Return non-nil when the height of WINDOW shall be preserved.
1529 Optional argument HORIZONTAL non-nil means to return non-nil when
1530 the width of WINDOW shall be preserved."
1531 (let ((size (window-preserved-size window horizontal)))
1532 (and (numberp size)
1533 (= size (window--preservable-size window horizontal)))))
1535 (defun window-safe-min-size (&optional window horizontal pixelwise)
1536 "Return safe minimum size of WINDOW.
1537 WINDOW must be a valid window and defaults to the selected one.
1538 Optional argument HORIZONTAL non-nil means return the minimum
1539 number of columns of WINDOW; otherwise return the minimum number
1540 of WINDOW's lines.
1542 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1543 of WINDOW."
1544 (setq window (window-normalize-window window))
1545 (if pixelwise
1546 (if horizontal
1547 (* window-safe-min-width
1548 (frame-char-width (window-frame window)))
1549 (* window-safe-min-height
1550 (frame-char-height (window-frame window))))
1551 (if horizontal window-safe-min-width window-safe-min-height)))
1553 (defun window-min-size (&optional window horizontal ignore pixelwise)
1554 "Return the minimum size of WINDOW.
1555 WINDOW must be a valid window and defaults to the selected one.
1556 Optional argument HORIZONTAL non-nil means return the minimum
1557 number of columns of WINDOW; otherwise return the minimum number
1558 of WINDOW's lines.
1560 The optional argument IGNORE has the same meaning as for
1561 `window-resizable'. Optional argument PIXELWISE non-nil means
1562 return the minimum pixel-size of WINDOW."
1563 (window--min-size-1
1564 (window-normalize-window window) horizontal ignore pixelwise))
1566 (defun window--min-size-ignore-p (window ignore)
1567 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
1568 (if (window-valid-p ignore)
1569 (eq window ignore)
1570 (not (memq ignore '(nil preserved)))))
1572 (defun window--min-size-1 (window horizontal ignore pixelwise)
1573 "Internal function of `window-min-size'."
1574 (let ((sub (window-child window)))
1575 (if sub
1576 (let ((value 0))
1577 ;; WINDOW is an internal window.
1578 (if (window-combined-p sub horizontal)
1579 ;; The minimum size of an iso-combination is the sum of
1580 ;; the minimum sizes of its child windows.
1581 (while sub
1582 (setq value (+ value
1583 (window--min-size-1
1584 sub horizontal ignore pixelwise)))
1585 (setq sub (window-right sub)))
1586 ;; The minimum size of an ortho-combination is the maximum
1587 ;; of the minimum sizes of its child windows.
1588 (while sub
1589 (setq value (max value
1590 (window--min-size-1
1591 sub horizontal ignore pixelwise)))
1592 (setq sub (window-right sub))))
1593 value)
1594 (with-current-buffer (window-buffer window)
1595 (cond
1596 ((window-minibuffer-p window)
1597 (if pixelwise (frame-char-height (window-frame window)) 1))
1598 ((window-size-fixed-p window horizontal ignore)
1599 ;; The minimum size of a fixed size window is its size.
1600 (window-size window horizontal pixelwise))
1601 ((eq ignore 'safe)
1602 ;; If IGNORE equals `safe' return the safe value.
1603 (window-safe-min-size window horizontal pixelwise))
1604 (horizontal
1605 ;; For the minimum width of a window take fringes and
1606 ;; scroll-bars into account. This is questionable and should
1607 ;; be removed as soon as we are able to split (and resize)
1608 ;; windows such that the new (or resized) windows can get a
1609 ;; size less than the user-specified `window-min-height' and
1610 ;; `window-min-width'.
1611 (let* ((char-size (frame-char-size window t))
1612 (fringes (window-fringes window))
1613 (margins (window-margins window))
1614 ;; Let the 'min-margins' parameter override the actual
1615 ;; widths of the margins. We allow any number to
1616 ;; replace the values specified by `window-margins'.
1617 ;; See bug#24193 for the rationale of this parameter.
1618 (min-margins (window-parameter window 'min-margins))
1619 (left-min-margin (and min-margins
1620 (numberp (car min-margins))
1621 (car min-margins)))
1622 (right-min-margin (and min-margins
1623 (numberp (cdr min-margins))
1624 (cdr min-margins)))
1625 (pixel-width
1626 (+ (window-safe-min-size window t t)
1627 (* (or left-min-margin (car margins) 0) char-size)
1628 (* (or right-min-margin(cdr margins) 0) char-size)
1629 (car fringes) (cadr fringes)
1630 (window-scroll-bar-width window)
1631 (window-right-divider-width window))))
1632 (if pixelwise
1633 (max
1634 (if window-resize-pixelwise
1635 pixel-width
1636 ;; Round up to next integral of columns.
1637 (* (ceiling pixel-width char-size) char-size))
1638 (if (window--min-size-ignore-p window ignore)
1640 (window-min-pixel-width window)))
1641 (max
1642 (ceiling pixel-width char-size)
1643 (if (window--min-size-ignore-p window ignore)
1645 window-min-width)))))
1646 ((let ((char-size (frame-char-size window))
1647 (pixel-height
1648 (+ (window-safe-min-size window nil t)
1649 (window-header-line-height window)
1650 (window-scroll-bar-height window)
1651 (window-mode-line-height window)
1652 (window-bottom-divider-width window))))
1653 (if pixelwise
1654 (max
1655 (if window-resize-pixelwise
1656 pixel-height
1657 ;; Round up to next integral of lines.
1658 (* (ceiling pixel-height char-size) char-size))
1659 (if (window--min-size-ignore-p window ignore)
1661 (window-min-pixel-height window)))
1662 (max (ceiling pixel-height char-size)
1663 (if (window--min-size-ignore-p window ignore)
1665 window-min-height))))))))))
1667 (defun window-sizable (window delta &optional horizontal ignore pixelwise)
1668 "Return DELTA if DELTA lines can be added to WINDOW.
1669 WINDOW must be a valid window and defaults to the selected one.
1670 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1671 columns can be added to WINDOW. A return value of zero means
1672 that no lines (or columns) can be added to WINDOW.
1674 This function looks only at WINDOW and, recursively, its child
1675 windows. The function `window-resizable' looks at other windows
1676 as well.
1678 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1679 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1680 return the maximum value in the range 0..DELTA by which WINDOW
1681 can be enlarged.
1683 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1684 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1685 return the minimum value in the range DELTA..0 by which WINDOW
1686 can be shrunk.
1688 The optional argument IGNORE has the same meaning as for
1689 `window-resizable'. Optional argument PIXELWISE non-nil means
1690 interpret DELTA as pixels."
1691 (setq window (window-normalize-window window))
1692 (cond
1693 ((< delta 0)
1694 (max (- (window-min-size window horizontal ignore pixelwise)
1695 (window-size window horizontal pixelwise))
1696 delta))
1697 ((> delta 0)
1698 (if (window-size-fixed-p window horizontal ignore)
1700 delta))
1701 (t 0)))
1703 (defun window-sizable-p (window delta &optional horizontal ignore pixelwise)
1704 "Return t if WINDOW can be resized by DELTA lines.
1705 WINDOW must be a valid window and defaults to the selected one.
1706 For the meaning of the arguments of this function see the
1707 doc-string of `window-sizable'."
1708 (setq window (window-normalize-window window))
1709 (if (> delta 0)
1710 (>= (window-sizable window delta horizontal ignore pixelwise)
1711 delta)
1712 (<= (window-sizable window delta horizontal ignore pixelwise)
1713 delta)))
1715 (defun window--size-fixed-1 (window horizontal ignore)
1716 "Internal function for `window-size-fixed-p'."
1717 (let ((sub (window-child window)))
1718 (catch 'fixed
1719 (if sub
1720 ;; WINDOW is an internal window.
1721 (if (window-combined-p sub horizontal)
1722 ;; An iso-combination is fixed size if all its child
1723 ;; windows are fixed-size.
1724 (progn
1725 (while sub
1726 (unless (window--size-fixed-1 sub horizontal ignore)
1727 ;; We found a non-fixed-size child window, so
1728 ;; WINDOW's size is not fixed.
1729 (throw 'fixed nil))
1730 (setq sub (window-right sub)))
1731 ;; All child windows are fixed-size, so WINDOW's size is
1732 ;; fixed.
1733 (throw 'fixed t))
1734 ;; An ortho-combination is fixed-size if at least one of its
1735 ;; child windows is fixed-size.
1736 (while sub
1737 (when (window--size-fixed-1 sub horizontal ignore)
1738 ;; We found a fixed-size child window, so WINDOW's size
1739 ;; is fixed.
1740 (throw 'fixed t))
1741 (setq sub (window-right sub))))
1742 ;; WINDOW is a live window.
1743 (and (or (not (windowp ignore)) (not (eq window ignore)))
1744 (or (and (not (eq ignore 'preserved))
1745 (window--preserve-size window horizontal))
1746 (with-current-buffer (window-buffer window)
1747 (if horizontal
1748 (memq window-size-fixed '(width t))
1749 (memq window-size-fixed '(height t))))))))))
1751 (defun window-size-fixed-p (&optional window horizontal ignore)
1752 "Return non-nil if WINDOW's height is fixed.
1753 WINDOW must be a valid window and defaults to the selected one.
1754 Optional argument HORIZONTAL non-nil means return non-nil if
1755 WINDOW's width is fixed. The optional argument IGNORE has the
1756 same meaning as for `window-resizable'.
1758 If this function returns nil, this does not necessarily mean that
1759 WINDOW can be resized in the desired direction. The function
1760 `window-resizable' can tell that."
1761 (when (or (windowp ignore) (memq ignore '(nil preserved)))
1762 (window--size-fixed-1
1763 (window-normalize-window window) horizontal ignore)))
1765 (defun window--min-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
1766 "Internal function for `window-min-delta'."
1767 (if (not (window-parent window))
1768 ;; If we can't go up, return zero.
1770 ;; Else try to find a non-fixed-size sibling of WINDOW.
1771 (let* ((parent (window-parent window))
1772 (sub (window-child parent)))
1773 (catch 'done
1774 (if (window-combined-p sub horizontal)
1775 ;; In an iso-combination throw DELTA if we find at least one
1776 ;; child window and that window is either not fixed-size or
1777 ;; we can ignore fixed-sizeness.
1778 (let ((skip (eq trail 'after)))
1779 (while sub
1780 (cond
1781 ((eq sub window)
1782 (setq skip (eq trail 'before)))
1783 (skip)
1784 ((window-size-fixed-p sub horizontal ignore))
1786 ;; We found a non-fixed-size child window.
1787 (throw 'done delta)))
1788 (setq sub (window-right sub))))
1789 ;; In an ortho-combination set DELTA to the minimum value by
1790 ;; which other child windows can shrink.
1791 (while sub
1792 (unless (eq sub window)
1793 (setq delta
1794 (min delta
1795 (max (- (window-size sub horizontal pixelwise 'ceiling)
1796 (window-min-size
1797 sub horizontal ignore pixelwise))
1798 0))))
1799 (setq sub (window-right sub))))
1800 (if noup
1801 delta
1802 (window--min-delta-1
1803 parent delta horizontal ignore trail nil pixelwise))))))
1805 (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise)
1806 "Return number of lines by which WINDOW can be shrunk.
1807 WINDOW must be a valid window and defaults to the selected one.
1808 Return zero if WINDOW cannot be shrunk.
1810 Optional argument HORIZONTAL non-nil means return number of
1811 columns by which WINDOW can be shrunk.
1813 The optional argument IGNORE has the same meaning as for
1814 `window-resizable'. Optional argument TRAIL restricts the
1815 windows that can be enlarged. If its value is `before', only
1816 windows to the left of or above WINDOW can be enlarged. If it is
1817 `after', only windows to the right of or below WINDOW can be
1818 enlarged.
1820 Optional argument NOUP non-nil means don't go up in the window
1821 tree, but try to enlarge windows within WINDOW's combination
1822 only. Optional argument NODOWN non-nil means don't check whether
1823 WINDOW itself (and its child windows) can be shrunk; check only
1824 whether at least one other window can be enlarged appropriately.
1826 Optional argument PIXELWISE non-nil means return number of pixels
1827 by which WINDOW can be shrunk."
1828 (setq window (window-normalize-window window))
1829 (let ((size (window-size window horizontal pixelwise 'floor))
1830 (minimum (window-min-size window horizontal ignore pixelwise)))
1831 (cond
1832 (nodown
1833 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1834 (window--min-delta-1
1835 window size horizontal ignore trail noup pixelwise))
1836 ((<= size minimum)
1837 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1838 ;; there's nothing to recover.
1841 ;; Otherwise, try to recover whatever WINDOW is larger than its
1842 ;; minimum size.
1843 (window--min-delta-1
1844 window (- size minimum) horizontal ignore trail noup pixelwise)))))
1846 (defun frame-windows-min-size (&optional frame horizontal ignore pixelwise)
1847 "Return minimum number of lines of FRAME's windows.
1848 HORIZONTAL non-nil means return number of columns of FRAME's
1849 windows. The optional argument IGNORE has the same meaning as
1850 for `window-resizable'. PIXELWISE non-nil means return sizes in
1851 pixels."
1852 (setq frame (window-normalize-frame frame))
1853 (let* ((root (frame-root-window frame))
1854 (mini (window-next-sibling root)))
1855 (+ (window-min-size root horizontal ignore pixelwise)
1856 (if (and mini (not horizontal))
1857 (window-min-size mini horizontal nil pixelwise)
1858 0))))
1860 (defun window--max-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
1861 "Internal function of `window-max-delta'."
1862 (if (not (window-parent window))
1863 ;; Can't go up. Return DELTA.
1864 delta
1865 (let* ((parent (window-parent window))
1866 (sub (window-child parent)))
1867 (catch 'fixed
1868 (if (window-combined-p sub horizontal)
1869 ;; For an iso-combination calculate how much we can get from
1870 ;; other child windows.
1871 (let ((skip (eq trail 'after)))
1872 (while sub
1873 (cond
1874 ((eq sub window)
1875 (setq skip (eq trail 'before)))
1876 (skip)
1878 (setq delta
1879 (+ delta
1880 (max
1881 (- (window-size sub horizontal pixelwise 'floor)
1882 (window-min-size
1883 sub horizontal ignore pixelwise))
1884 0)))))
1885 (setq sub (window-right sub))))
1886 ;; For an ortho-combination throw DELTA when at least one
1887 ;; child window is fixed-size.
1888 (while sub
1889 (when (and (not (eq sub window))
1890 (window-size-fixed-p sub horizontal ignore))
1891 (throw 'fixed delta))
1892 (setq sub (window-right sub))))
1893 (if noup
1894 ;; When NOUP is nil, DELTA is all we can get.
1895 delta
1896 ;; Else try with parent of WINDOW, passing the DELTA we
1897 ;; recovered so far.
1898 (window--max-delta-1
1899 parent delta horizontal ignore trail nil pixelwise))))))
1901 (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise)
1902 "Return maximum number of lines by which WINDOW can be enlarged.
1903 WINDOW must be a valid window and defaults to the selected one.
1904 The return value is zero if WINDOW cannot be enlarged.
1906 Optional argument HORIZONTAL non-nil means return maximum number
1907 of columns by which WINDOW can be enlarged.
1909 The optional argument IGNORE has the same meaning as for
1910 `window-resizable'. Optional argument TRAIL restricts the
1911 windows that can be enlarged. If its value is `before', only
1912 windows to the left of or above WINDOW can be enlarged. If it is
1913 `after', only windows to the right of or below WINDOW can be
1914 enlarged.
1916 Optional argument NOUP non-nil means don't go up in the window
1917 tree but try to obtain the entire space from windows within
1918 WINDOW's combination. Optional argument NODOWN non-nil means do
1919 not check whether WINDOW itself (and its child windows) can be
1920 enlarged; check only whether other windows can be shrunk
1921 appropriately.
1923 Optional argument PIXELWISE non-nil means return number of
1924 pixels by which WINDOW can be enlarged."
1925 (setq window (window-normalize-window window))
1926 (if (and (not nodown) (window-size-fixed-p window horizontal ignore))
1927 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1928 ;; size.
1930 ;; WINDOW has no fixed size.
1931 (window--max-delta-1 window 0 horizontal ignore trail noup pixelwise)))
1933 ;; Make NOUP also inhibit the min-size check.
1934 (defun window--resizable (window delta &optional horizontal ignore trail noup nodown pixelwise)
1935 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1936 WINDOW must be a valid window and defaults to the selected one.
1937 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1938 can be resized horizontally by DELTA columns. A return value of
1939 zero means that WINDOW is not resizable.
1941 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1942 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1943 return the maximum value in the range 0..DELTA by which WINDOW
1944 can be enlarged.
1946 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1947 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1948 return the minimum value in the range DELTA..0 that can be used
1949 for shrinking WINDOW.
1951 The optional argument IGNORE has the same meaning as for
1952 `window-resizable'. Optional argument TRAIL `before' means only
1953 windows to the left of or below WINDOW can be shrunk. Optional
1954 argument TRAIL `after' means only windows to the right of or
1955 above WINDOW can be shrunk.
1957 Optional argument NOUP non-nil means don't go up in the window
1958 tree but check only whether space can be obtained from (or given
1959 to) WINDOW's siblings. Optional argument NODOWN non-nil means
1960 don't go down in the window tree. This means do not check
1961 whether resizing would violate size restrictions of WINDOW or its
1962 child windows.
1964 Optional argument PIXELWISE non-nil means interpret DELTA as
1965 number of pixels."
1966 (setq window (window-normalize-window window))
1967 (cond
1968 ((< delta 0)
1969 (max (- (window-min-delta
1970 window horizontal ignore trail noup nodown pixelwise))
1971 delta))
1972 ((> delta 0)
1973 (min (window-max-delta
1974 window horizontal ignore trail noup nodown pixelwise)
1975 delta))
1976 (t 0)))
1978 (defun window--resizable-p (window delta &optional horizontal ignore trail noup nodown pixelwise)
1979 "Return t if WINDOW can be resized vertically by DELTA lines.
1980 WINDOW must be a valid window and defaults to the selected one.
1981 For the meaning of the arguments of this function see the
1982 doc-string of `window--resizable'.
1984 Optional argument PIXELWISE non-nil means interpret DELTA as
1985 pixels."
1986 (setq window (window-normalize-window window))
1987 (if (> delta 0)
1988 (>= (window--resizable
1989 window delta horizontal ignore trail noup nodown pixelwise)
1990 delta)
1991 (<= (window--resizable
1992 window delta horizontal ignore trail noup nodown pixelwise)
1993 delta)))
1995 (defun window-resizable (window delta &optional horizontal ignore pixelwise)
1996 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1997 WINDOW must be a valid window and defaults to the selected one.
1998 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1999 can be resized horizontally by DELTA columns. A return value of
2000 zero means that WINDOW is not resizable.
2002 DELTA positive means WINDOW shall be enlarged by DELTA lines or
2003 columns. If WINDOW cannot be enlarged by DELTA lines or columns
2004 return the maximum value in the range 0..DELTA by which WINDOW
2005 can be enlarged.
2007 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
2008 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
2009 return the minimum value in the range DELTA..0 that can be used
2010 for shrinking WINDOW.
2012 Optional argument IGNORE, if non-nil, means to ignore restraints
2013 induced by fixed size windows or the values of the variables
2014 `window-min-height' and `window-min-width'. The following values
2015 have special meanings: `safe' means that in addition live windows
2016 are allowed to get as small as `window-safe-min-height' lines and
2017 `window-safe-min-width' columns. `preserved' means to ignore
2018 only restrictions induced by `window-preserve-size'. If IGNORE
2019 is a window, then ignore restrictions for that window only.
2021 Optional argument PIXELWISE non-nil means interpret DELTA as
2022 pixels."
2023 (setq window (window-normalize-window window))
2024 (window--resizable window delta horizontal ignore nil nil nil pixelwise))
2026 (defun window-resizable-p (window delta &optional horizontal ignore pixelwise)
2027 "Return t if WINDOW can be resized vertically by DELTA lines.
2028 WINDOW must be a valid window and defaults to the selected one.
2029 For the meaning of the arguments of this function see the
2030 doc-string of `window-resizable'."
2031 (setq window (window-normalize-window window))
2032 (if (> delta 0)
2033 (>= (window--resizable
2034 window delta horizontal ignore nil nil nil pixelwise)
2035 delta)
2036 (<= (window--resizable
2037 window delta horizontal ignore nil nil nil pixelwise)
2038 delta)))
2040 ;; Aliases of functions defined in window.c.
2041 (defalias 'window-height 'window-total-height)
2042 (defalias 'window-width 'window-body-width)
2044 (defun window-full-height-p (&optional window)
2045 "Return t if WINDOW is as high as its containing frame.
2046 More precisely, return t if and only if the total height of
2047 WINDOW equals the total height of the root window of WINDOW's
2048 frame. WINDOW must be a valid window and defaults to the
2049 selected one."
2050 (setq window (window-normalize-window window))
2051 (if (window-minibuffer-p window)
2052 (eq window (frame-root-window (window-frame window)))
2053 (= (window-pixel-height window)
2054 (window-pixel-height (frame-root-window window)))))
2056 (defun window-full-width-p (&optional window)
2057 "Return t if WINDOW is as wide as its containing frame.
2058 More precisely, return t if and only if the total width of WINDOW
2059 equals the total width of the root window of WINDOW's frame.
2060 WINDOW must be a valid window and defaults to the selected one."
2061 (setq window (window-normalize-window window))
2062 (= (window-pixel-width window)
2063 (window-pixel-width (frame-root-window window))))
2065 (defun window-body-size (&optional window horizontal pixelwise)
2066 "Return the height or width of WINDOW's text area.
2067 WINDOW must be a live window and defaults to the selected one.
2069 If HORIZONTAL is omitted or nil, return the height of the text
2070 area, like `window-body-height'. Otherwise, return the width of
2071 the text area, like `window-body-width'. In either case, the
2072 optional argument PIXELWISE is passed to the functions."
2073 (if horizontal
2074 (window-body-width window pixelwise)
2075 (window-body-height window pixelwise)))
2077 (declare-function font-info "font.c" (name &optional frame))
2079 (defun window-font-width (&optional window face)
2080 "Return average character width 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 (width (aref info 11)))
2091 (if (> width 0)
2092 width
2093 (aref info 10)))
2094 (frame-char-width))))
2096 (defun window-font-height (&optional window face)
2097 "Return character height for the font of FACE used in WINDOW.
2098 WINDOW must be a live window and defaults to the selected one.
2100 If FACE is nil or omitted, the default face is used. If FACE is
2101 remapped (see `face-remapping-alist'), the function returns the
2102 information for the remapped face."
2103 (with-selected-window (window-normalize-window window t)
2104 (if (display-multi-font-p)
2105 (let* ((face (if face face 'default))
2106 (info (font-info (face-font face))))
2107 (aref info 3))
2108 (frame-char-height))))
2110 (defvar overflow-newline-into-fringe)
2112 (defun window-max-chars-per-line (&optional window face)
2113 "Return the number of characters that can be displayed on one line in WINDOW.
2114 WINDOW must be a live window and defaults to the selected one.
2116 The character width of FACE is used for the calculation. If FACE
2117 is nil or omitted, the default face is used. If FACE is
2118 remapped (see `face-remapping-alist'), the function uses the
2119 remapped face.
2121 This function is different from `window-body-width' in two
2122 ways. First, it accounts for the portions of the line reserved
2123 for the continuation glyph. Second, it accounts for the size of
2124 the font."
2125 (with-selected-window (window-normalize-window window t)
2126 (let* ((window-width (window-body-width window t))
2127 (font-width (window-font-width window face))
2128 (ncols (/ window-width font-width)))
2129 (if (and (display-graphic-p)
2130 overflow-newline-into-fringe
2131 (not
2132 (or (eq left-fringe-width 0)
2133 (and (null left-fringe-width)
2134 (= (frame-parameter nil 'left-fringe) 0))))
2135 (not
2136 (or (eq right-fringe-width 0)
2137 (and (null right-fringe-width)
2138 (= (frame-parameter nil 'right-fringe) 0)))))
2139 ncols
2140 ;; FIXME: This should remove 1 more column when there are no
2141 ;; fringes, lines are truncated, and the window is hscrolled,
2142 ;; but EOL is not in the view, because then there are 2
2143 ;; truncation glyphs, not one.
2144 (1- ncols)))))
2146 (defun window-current-scroll-bars (&optional window)
2147 "Return the current scroll bar types for WINDOW.
2148 WINDOW must be a live window and defaults to the selected one.
2150 The return value is a cons cell (VERTICAL . HORIZONTAL) where
2151 VERTICAL specifies the current location of the vertical scroll
2152 bar (`left', `right' or nil), and HORIZONTAL specifies the
2153 current location of the horizontal scroll bar (`bottom' or nil).
2155 Unlike `window-scroll-bars', this function reports the scroll bar
2156 type actually used, once frame defaults and `scroll-bar-mode' are
2157 taken into account."
2158 (setq window (window-normalize-window window t))
2159 (let ((vertical (nth 2 (window-scroll-bars window)))
2160 (horizontal (nth 5 (window-scroll-bars window)))
2161 (inherited (frame-current-scroll-bars (window-frame window))))
2162 (when (eq vertical t)
2163 (setq vertical (car inherited)))
2164 (when (eq horizontal t)
2165 (setq horizontal (cdr inherited)))
2166 (cons vertical (and horizontal 'bottom))))
2168 (defun walk-windows (fun &optional minibuf all-frames)
2169 "Cycle through all live windows, calling FUN for each one.
2170 FUN must specify a function with a window as its sole argument.
2171 The optional arguments MINIBUF and ALL-FRAMES specify the set of
2172 windows to include in the walk.
2174 MINIBUF t means include the minibuffer window even if the
2175 minibuffer is not active. MINIBUF nil or omitted means include
2176 the minibuffer window only if the minibuffer is active. Any
2177 other value means do not include the minibuffer window even if
2178 the minibuffer is active.
2180 ALL-FRAMES nil or omitted means consider all windows on the
2181 selected frame, plus the minibuffer window if specified by the
2182 MINIBUF argument. If the minibuffer counts, consider all windows
2183 on all frames that share that minibuffer too. The following
2184 non-nil values of ALL-FRAMES have special meanings:
2186 - t means consider all windows on all existing frames.
2188 - `visible' means consider all windows on all visible frames on
2189 the current terminal.
2191 - 0 (the number zero) means consider all windows on all visible
2192 and iconified frames on the current terminal.
2194 - A frame means consider all windows on that frame only.
2196 Anything else means consider all windows on the selected frame
2197 and no others.
2199 This function changes neither the order of recently selected
2200 windows nor the buffer list."
2201 ;; If we start from the minibuffer window, don't fail to come
2202 ;; back to it.
2203 (when (window-minibuffer-p)
2204 (setq minibuf t))
2205 ;; Make sure to not mess up the order of recently selected
2206 ;; windows. Use `save-selected-window' and `select-window'
2207 ;; with second argument non-nil for this purpose.
2208 (save-selected-window
2209 (when (framep all-frames)
2210 (select-window (frame-first-window all-frames) 'norecord))
2211 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
2212 (funcall fun walk-windows-window))))
2214 (defun window-at-side-p (&optional window side)
2215 "Return t if WINDOW is at SIDE of its containing frame.
2216 WINDOW must be a valid window and defaults to the selected one.
2217 SIDE can be any of the symbols `left', `top', `right' or
2218 `bottom'. The default value nil is handled like `bottom'."
2219 (setq window (window-normalize-window window))
2220 (let ((edge
2221 (cond
2222 ((eq side 'left) 0)
2223 ((eq side 'top) 1)
2224 ((eq side 'right) 2)
2225 ((memq side '(bottom nil)) 3))))
2226 (= (nth edge (window-pixel-edges window))
2227 (nth edge (window-pixel-edges (frame-root-window window))))))
2229 (defun window-at-side-list (&optional frame side)
2230 "Return list of all windows on SIDE of FRAME.
2231 FRAME must be a live frame and defaults to the selected frame.
2232 SIDE can be any of the symbols `left', `top', `right' or
2233 `bottom'. The default value nil is handled like `bottom'."
2234 (setq frame (window-normalize-frame frame))
2235 (let (windows)
2236 (walk-window-tree
2237 (lambda (window)
2238 (when (window-at-side-p window side)
2239 (setq windows (cons window windows))))
2240 frame nil 'nomini)
2241 (nreverse windows)))
2243 (defun window--in-direction-2 (window posn &optional horizontal)
2244 "Support function for `window-in-direction'."
2245 (if horizontal
2246 (let ((top (window-pixel-top window)))
2247 (if (> top posn)
2248 (- top posn)
2249 (- posn top (window-pixel-height window))))
2250 (let ((left (window-pixel-left window)))
2251 (if (> left posn)
2252 (- left posn)
2253 (- posn left (window-pixel-width window))))))
2255 ;; Predecessors to the below have been devised by Julian Assange in
2256 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
2257 ;; Neither of these allow one to selectively ignore specific windows
2258 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
2259 ;; the movement.
2260 (defun window-in-direction (direction &optional window ignore sign wrap mini)
2261 "Return window in DIRECTION as seen from WINDOW.
2262 More precisely, return the nearest window in direction DIRECTION
2263 as seen from the position of `window-point' in window WINDOW.
2264 DIRECTION must be one of `above', `below', `left' or `right'.
2265 WINDOW must be a live window and defaults to the selected one.
2267 Do not return a window whose `no-other-window' parameter is
2268 non-nil. If the nearest window's `no-other-window' parameter is
2269 non-nil, try to find another window in the indicated direction.
2270 If, however, the optional argument IGNORE is non-nil, return that
2271 window even if its `no-other-window' parameter is non-nil.
2273 Optional argument SIGN a negative number means to use the right
2274 or bottom edge of WINDOW as reference position instead of
2275 `window-point'. SIGN a positive number means to use the left or
2276 top edge of WINDOW as reference position.
2278 Optional argument WRAP non-nil means to wrap DIRECTION around
2279 frame borders. This means to return for WINDOW at the top of the
2280 frame and DIRECTION `above' the minibuffer window if the frame
2281 has one, and a window at the bottom of the frame otherwise.
2283 Optional argument MINI nil means to return the minibuffer window
2284 if and only if it is currently active. MINI non-nil means to
2285 return the minibuffer window even when it's not active. However,
2286 if WRAP is non-nil, always act as if MINI were nil.
2288 Return nil if no suitable window can be found."
2289 (setq window (window-normalize-window window t))
2290 (unless (memq direction '(above below left right))
2291 (error "Wrong direction %s" direction))
2292 (let* ((frame (window-frame window))
2293 (hor (memq direction '(left right)))
2294 (first (if hor
2295 (window-pixel-left window)
2296 (window-pixel-top window)))
2297 (last (+ first (window-size window hor t)))
2298 ;; The column / row value of `posn-at-point' can be nil for the
2299 ;; mini-window, guard against that.
2300 (posn
2301 (cond
2302 ((and (numberp sign) (< sign 0))
2303 (if hor
2304 (1- (+ (window-pixel-top window) (window-pixel-height window)))
2305 (1- (+ (window-pixel-left window) (window-pixel-width window)))))
2306 ((and (numberp sign) (> sign 0))
2307 (if hor
2308 (window-pixel-top window)
2309 (window-pixel-left window)))
2310 ((let ((posn-cons (nth 2 (posn-at-point (window-point window) window))))
2311 (if hor
2312 (+ (or (cdr posn-cons) 1) (window-pixel-top window))
2313 (+ (or (car posn-cons) 1) (window-pixel-left window)))))))
2314 (best-edge
2315 (cond
2316 ((eq direction 'below) (frame-pixel-height frame))
2317 ((eq direction 'right) (frame-pixel-width frame))
2318 (t -1)))
2319 (best-edge-2 best-edge)
2320 (best-diff-2 (if hor (frame-pixel-height frame) (frame-pixel-width frame)))
2321 best best-2 best-diff-2-new)
2322 (walk-window-tree
2323 (lambda (w)
2324 (let* ((w-top (window-pixel-top w))
2325 (w-left (window-pixel-left w)))
2326 (cond
2327 ((or (eq window w)
2328 ;; Ignore ourselves.
2329 (and (window-parameter w 'no-other-window)
2330 ;; Ignore W unless IGNORE is non-nil.
2331 (not ignore))))
2332 (hor
2333 (cond
2334 ((and (<= w-top posn)
2335 (< posn (+ w-top (window-pixel-height w))))
2336 ;; W is to the left or right of WINDOW and covers POSN.
2337 (when (or (and (eq direction 'left)
2338 (or (and (<= w-left first) (> w-left best-edge))
2339 (and wrap
2340 (window-at-side-p window 'left)
2341 (window-at-side-p w 'right))))
2342 (and (eq direction 'right)
2343 (or (and (>= w-left last) (< w-left best-edge))
2344 (and wrap
2345 (window-at-side-p window 'right)
2346 (window-at-side-p w 'left)))))
2347 (setq best-edge w-left)
2348 (setq best w)))
2349 ((and (or (and (eq direction 'left)
2350 (<= (+ w-left (window-pixel-width w)) first))
2351 (and (eq direction 'right) (<= last w-left)))
2352 ;; W is to the left or right of WINDOW but does not
2353 ;; cover POSN.
2354 (setq best-diff-2-new
2355 (window--in-direction-2 w posn hor))
2356 (or (< best-diff-2-new best-diff-2)
2357 (and (= best-diff-2-new best-diff-2)
2358 (if (eq direction 'left)
2359 (> w-left best-edge-2)
2360 (< w-left best-edge-2)))))
2361 (setq best-edge-2 w-left)
2362 (setq best-diff-2 best-diff-2-new)
2363 (setq best-2 w))))
2364 ((and (<= w-left posn)
2365 (< posn (+ w-left (window-pixel-width w))))
2366 ;; W is above or below WINDOW and covers POSN.
2367 (when (or (and (eq direction 'above)
2368 (or (and (<= w-top first) (> w-top best-edge))
2369 (and wrap
2370 (window-at-side-p window 'top)
2371 (if (active-minibuffer-window)
2372 (minibuffer-window-active-p w)
2373 (window-at-side-p w 'bottom)))))
2374 (and (eq direction 'below)
2375 (or (and (>= w-top first) (< w-top best-edge))
2376 (and wrap
2377 (if (active-minibuffer-window)
2378 (minibuffer-window-active-p window)
2379 (window-at-side-p window 'bottom))
2380 (window-at-side-p w 'top)))))
2381 (setq best-edge w-top)
2382 (setq best w)))
2383 ((and (or (and (eq direction 'above)
2384 (<= (+ w-top (window-pixel-height w)) first))
2385 (and (eq direction 'below) (<= last w-top)))
2386 ;; W is above or below WINDOW but does not cover POSN.
2387 (setq best-diff-2-new
2388 (window--in-direction-2 w posn hor))
2389 (or (< best-diff-2-new best-diff-2)
2390 (and (= best-diff-2-new best-diff-2)
2391 (if (eq direction 'above)
2392 (> w-top best-edge-2)
2393 (< w-top best-edge-2)))))
2394 (setq best-edge-2 w-top)
2395 (setq best-diff-2 best-diff-2-new)
2396 (setq best-2 w)))))
2397 frame nil (and mini t))
2398 (or best best-2)))
2400 (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
2401 "Return a live window satisfying PREDICATE.
2402 More precisely, cycle through all windows calling the function
2403 PREDICATE on each one of them with the window as its sole
2404 argument. Return the first window for which PREDICATE returns
2405 non-nil. Windows are scanned starting with the window following
2406 the selected window. If no window satisfies PREDICATE, return
2407 DEFAULT.
2409 MINIBUF t means include the minibuffer window even if the
2410 minibuffer is not active. MINIBUF nil or omitted means include
2411 the minibuffer window only if the minibuffer is active. Any
2412 other value means do not include the minibuffer window even if
2413 the minibuffer is active.
2415 ALL-FRAMES nil or omitted means consider all windows on the selected
2416 frame, plus the minibuffer window if specified by the MINIBUF
2417 argument. If the minibuffer counts, consider all windows on all
2418 frames that share that minibuffer too. The following non-nil
2419 values of ALL-FRAMES have special meanings:
2421 - t means consider all windows on all existing frames.
2423 - `visible' means consider all windows on all visible frames on
2424 the current terminal.
2426 - 0 (the number zero) means consider all windows on all visible
2427 and iconified frames on the current terminal.
2429 - A frame means consider all windows on that frame only.
2431 Anything else means consider all windows on the selected frame
2432 and no others."
2433 (catch 'found
2434 (dolist (window (window-list-1
2435 (next-window nil minibuf all-frames)
2436 minibuf all-frames))
2437 (when (funcall predicate window)
2438 (throw 'found window)))
2439 default))
2441 (defalias 'some-window 'get-window-with-predicate)
2443 (defun get-lru-window (&optional all-frames dedicated not-selected)
2444 "Return the least recently used window on frames specified by ALL-FRAMES.
2445 Return a full-width window if possible. A minibuffer window is
2446 never a candidate. A dedicated window is never a candidate
2447 unless DEDICATED is non-nil, so if all windows are dedicated, the
2448 value is nil. Avoid returning the selected window if possible.
2449 Optional argument NOT-SELECTED non-nil means never return the
2450 selected window.
2452 The following non-nil values of the optional argument ALL-FRAMES
2453 have special meanings:
2455 - t means consider all windows on all existing frames.
2457 - `visible' means consider all windows on all visible frames on
2458 the current terminal.
2460 - 0 (the number zero) means consider all windows on all visible
2461 and iconified frames on the current terminal.
2463 - A frame means consider all windows on that frame only.
2465 Any other value of ALL-FRAMES means consider all windows on the
2466 selected frame and no others."
2467 (let (best-window best-time second-best-window second-best-time time)
2468 (dolist (window (window-list-1 nil 'nomini all-frames))
2469 (when (and (or dedicated (not (window-dedicated-p window)))
2470 (or (not not-selected) (not (eq window (selected-window)))))
2471 (setq time (window-use-time window))
2472 (if (or (eq window (selected-window))
2473 (not (window-full-width-p window)))
2474 (when (or (not second-best-time) (< time second-best-time))
2475 (setq second-best-time time)
2476 (setq second-best-window window))
2477 (when (or (not best-time) (< time best-time))
2478 (setq best-time time)
2479 (setq best-window window)))))
2480 (or best-window second-best-window)))
2482 (defun get-mru-window (&optional all-frames dedicated not-selected)
2483 "Return the most recently used window on frames specified by ALL-FRAMES.
2484 A minibuffer window is never a candidate. A dedicated window is
2485 never a candidate unless DEDICATED is non-nil, so if all windows
2486 are dedicated, the value is nil. Optional argument NOT-SELECTED
2487 non-nil means never return the selected window.
2489 The following non-nil values of the optional argument ALL-FRAMES
2490 have special meanings:
2492 - t means consider all windows on all existing frames.
2494 - `visible' means consider all windows on all visible frames on
2495 the current terminal.
2497 - 0 (the number zero) means consider all windows on all visible
2498 and iconified frames on the current terminal.
2500 - A frame means consider all windows on that frame only.
2502 Any other value of ALL-FRAMES means consider all windows on the
2503 selected frame and no others."
2504 (let (best-window best-time time)
2505 (dolist (window (window-list-1 nil 'nomini all-frames))
2506 (setq time (window-use-time window))
2507 (when (and (or dedicated (not (window-dedicated-p window)))
2508 (or (not not-selected) (not (eq window (selected-window))))
2509 (or (not best-time) (> time best-time)))
2510 (setq best-time time)
2511 (setq best-window window)))
2512 best-window))
2514 (defun get-largest-window (&optional all-frames dedicated not-selected)
2515 "Return the largest window on frames specified by ALL-FRAMES.
2516 A minibuffer window is never a candidate. A dedicated window is
2517 never a candidate unless DEDICATED is non-nil, so if all windows
2518 are dedicated, the value is nil. Optional argument NOT-SELECTED
2519 non-nil means never return the selected window.
2521 The following non-nil values of the optional argument ALL-FRAMES
2522 have special meanings:
2524 - t means consider all windows on all existing frames.
2526 - `visible' means consider all windows on all visible frames on
2527 the current terminal.
2529 - 0 (the number zero) means consider all windows on all visible
2530 and iconified frames on the current terminal.
2532 - A frame means consider all windows on that frame only.
2534 Any other value of ALL-FRAMES means consider all windows on the
2535 selected frame and no others."
2536 (let ((best-size 0)
2537 best-window size)
2538 (dolist (window (window-list-1 nil 'nomini all-frames))
2539 (when (and (or dedicated (not (window-dedicated-p window)))
2540 (or (not not-selected) (not (eq window (selected-window)))))
2541 (setq size (* (window-pixel-height window)
2542 (window-pixel-width window)))
2543 (when (> size best-size)
2544 (setq best-size size)
2545 (setq best-window window))))
2546 best-window))
2548 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
2549 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2550 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2551 and defaults to the current buffer. If the selected window displays
2552 BUFFER-OR-NAME, it will be the first in the resulting list.
2554 MINIBUF t means include the minibuffer window even if the
2555 minibuffer is not active. MINIBUF nil or omitted means include
2556 the minibuffer window only if the minibuffer is active. Any
2557 other value means do not include the minibuffer window even if
2558 the minibuffer is active.
2560 ALL-FRAMES nil or omitted means consider all windows on the
2561 selected frame, plus the minibuffer window if specified by the
2562 MINIBUF argument. If the minibuffer counts, consider all windows
2563 on all frames that share that minibuffer too. The following
2564 non-nil values of ALL-FRAMES have special meanings:
2566 - t means consider all windows on all existing frames.
2568 - `visible' means consider all windows on all visible frames on
2569 the current terminal.
2571 - 0 (the number zero) means consider all windows on all visible
2572 and iconified frames on the current terminal.
2574 - A frame means consider all windows on that frame only.
2576 Anything else means consider all windows on the selected frame
2577 and no others."
2578 (let ((buffer (window-normalize-buffer buffer-or-name))
2579 windows)
2580 (dolist (window (window-list-1 (selected-window) minibuf all-frames))
2581 (when (eq (window-buffer window) buffer)
2582 (setq windows (cons window windows))))
2583 (nreverse windows)))
2585 (defun minibuffer-window-active-p (window)
2586 "Return t if WINDOW is the currently active minibuffer window."
2587 (and (window-live-p window) (eq window (active-minibuffer-window))))
2589 (defun count-windows (&optional minibuf)
2590 "Return the number of live windows on the selected frame.
2591 The optional argument MINIBUF specifies whether the minibuffer
2592 window shall be counted. See `walk-windows' for the precise
2593 meaning of this argument."
2594 (length (window-list-1 nil minibuf)))
2596 ;;; Resizing windows.
2597 (defun window--size-to-pixel (window size &optional horizontal pixelwise round-maybe)
2598 "For WINDOW convert SIZE lines to pixels.
2599 SIZE is supposed to specify a height of WINDOW in terms of text
2600 lines. The return value is the number of pixels specifying that
2601 height.
2603 WINDOW must be a valid window. Optional argument HORIZONTAL
2604 non-nil means convert SIZE columns to pixels.
2606 Optional argument PIXELWISE non-nil means SIZE already specifies
2607 pixels but may have to be adjusted to a multiple of the character
2608 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2609 means round to the nearest multiple of the character size of
2610 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2611 (setq window (window-normalize-window window))
2612 (let ((char-size (frame-char-size window horizontal)))
2613 (if pixelwise
2614 (if (and round-maybe (not window-resize-pixelwise))
2615 (* (round size char-size) char-size)
2616 size)
2617 (* size char-size))))
2619 (defun window--pixel-to-total-1 (window horizontal char-size)
2620 "Subroutine of `window--pixel-to-total'."
2621 (let ((child (window-child window)))
2622 (if (window-combination-p window horizontal)
2623 ;; In an iso-combination distribute sizes proportionally.
2624 (let ((remainder (window-new-total window))
2625 size best-child rem best-rem)
2626 ;; Initialize total sizes to each child's floor.
2627 (while child
2628 (setq size (max (/ (window-size child horizontal t) char-size) 1))
2629 (set-window-new-total child size)
2630 (setq remainder (- remainder size))
2631 (setq child (window-next-sibling child)))
2632 ;; Distribute remainder.
2633 (while (> remainder 0)
2634 (setq child (window-last-child window))
2635 (setq best-child nil)
2636 (setq best-rem 0)
2637 (while child
2638 (when (and (<= (window-new-total child)
2639 (/ (window-size child horizontal t) char-size))
2640 (> (setq rem (% (window-size child horizontal t)
2641 char-size))
2642 best-rem))
2643 (setq best-child child)
2644 (setq best-rem rem))
2645 (setq child (window-prev-sibling child)))
2646 ;; We MUST have a best-child here.
2647 (set-window-new-total best-child 1 t)
2648 (setq remainder (1- remainder)))
2649 ;; Recurse.
2650 (setq child (window-child window))
2651 (while child
2652 (window--pixel-to-total-1 child horizontal char-size)
2653 (setq child (window-next-sibling child))))
2654 ;; In an ortho-combination assign new sizes directly.
2655 (let ((size (window-new-total window)))
2656 (while child
2657 (set-window-new-total child size)
2658 (window--pixel-to-total-1 child horizontal char-size)
2659 (setq child (window-next-sibling child)))))))
2661 (defun window--pixel-to-total (&optional frame horizontal)
2662 "On FRAME assign new total window heights from pixel heights.
2663 FRAME must be a live frame and defaults to the selected frame.
2665 Optional argument HORIZONTAL non-nil means assign new total
2666 window widths from pixel widths."
2667 (setq frame (window-normalize-frame frame))
2668 (let* ((char-size (frame-char-size frame horizontal))
2669 (root (frame-root-window frame))
2670 (root-size (window-size root horizontal t))
2671 ;; We have to care about the minibuffer window only if it
2672 ;; appears together with the root window on this frame.
2673 (mini (let ((mini (minibuffer-window frame)))
2674 (and (eq (window-frame mini) frame)
2675 (not (eq mini root)) mini)))
2676 (mini-size (and mini (window-size mini horizontal t))))
2677 ;; We round the line/column sizes of windows here to the nearest
2678 ;; integer. In some cases this can make windows appear _larger_
2679 ;; than the containing frame (line/column-wise) because the latter's
2680 ;; sizes are not (yet) rounded. We might eventually fix that.
2681 (if (and mini (not horizontal))
2682 (let (lines)
2683 (set-window-new-total root (max (/ root-size char-size) 1))
2684 (set-window-new-total mini (max (/ mini-size char-size) 1))
2685 (setq lines (- (round (+ root-size mini-size) char-size)
2686 (+ (window-new-total root) (window-new-total mini))))
2687 (while (> lines 0)
2688 (if (>= (% root-size (window-new-total root))
2689 (% mini-size (window-new-total mini)))
2690 (set-window-new-total root 1 t)
2691 (set-window-new-total mini 1 t))
2692 (setq lines (1- lines))))
2693 (set-window-new-total root (round root-size char-size))
2694 (when mini
2695 ;; This is taken in the horizontal case only.
2696 (set-window-new-total mini (round mini-size char-size))))
2697 (unless (window-buffer root)
2698 (window--pixel-to-total-1 root horizontal char-size))
2699 ;; Apply the new sizes.
2700 (window-resize-apply-total frame horizontal)))
2702 (defun window--resize-reset (&optional frame horizontal)
2703 "Reset resize values for all windows on FRAME.
2704 FRAME defaults to the selected frame.
2706 This function stores the current value of `window-size' applied
2707 with argument HORIZONTAL in the new total size of all windows on
2708 FRAME. It also resets the new normal size of each of these
2709 windows."
2710 (window--resize-reset-1
2711 (frame-root-window (window-normalize-frame frame)) horizontal))
2713 (defun window--resize-reset-1 (window horizontal)
2714 "Internal function of `window--resize-reset'."
2715 ;; Register old size in the new total size.
2716 (set-window-new-pixel window (window-size window horizontal t))
2717 (set-window-new-total window (window-size window horizontal))
2718 ;; Reset new normal size.
2719 (set-window-new-normal window)
2720 (when (window-child window)
2721 (window--resize-reset-1 (window-child window) horizontal))
2722 (when (window-right window)
2723 (window--resize-reset-1 (window-right window) horizontal)))
2725 (defun window--resize-mini-window (window delta)
2726 "Resize minibuffer window WINDOW by DELTA pixels.
2727 If WINDOW cannot be resized by DELTA pixels make it as large (or
2728 as small) as possible, but don't signal an error."
2729 (when (window-minibuffer-p window)
2730 (let* ((frame (window-frame window))
2731 (root (frame-root-window frame))
2732 (height (window-pixel-height window))
2733 (min-delta
2734 (- (window-pixel-height root)
2735 (window-min-size root nil nil t))))
2736 ;; Sanitize DELTA.
2737 (cond
2738 ((<= (+ height delta) 0)
2739 (setq delta (- (frame-char-height (window-frame window)) height)))
2740 ((> delta min-delta)
2741 (setq delta min-delta)))
2743 (unless (zerop delta)
2744 ;; Resize now.
2745 (window--resize-reset frame)
2746 ;; Ideally we should be able to resize just the last child of root
2747 ;; here. See the comment in `resize-root-window-vertically' for
2748 ;; why we do not do that.
2749 (window--resize-this-window root (- delta) nil nil t)
2750 (set-window-new-pixel window (+ height delta))
2751 ;; The following routine catches the case where we want to resize
2752 ;; a minibuffer-only frame.
2753 (when (resize-mini-window-internal window)
2754 (window--pixel-to-total frame)
2755 (run-window-configuration-change-hook frame))))))
2757 (defun window--resize-apply-p (frame &optional horizontal)
2758 "Return t when a window on FRAME shall be resized vertically.
2759 Optional argument HORIZONTAL non-nil means return t when a window
2760 shall be resized horizontally."
2761 (catch 'apply
2762 (walk-window-tree
2763 (lambda (window)
2764 (unless (= (window-new-pixel window)
2765 (window-size window horizontal t))
2766 (throw 'apply t)))
2767 frame t)
2768 nil))
2770 (defun window-resize (window delta &optional horizontal ignore pixelwise)
2771 "Resize WINDOW vertically by DELTA lines.
2772 WINDOW can be an arbitrary window and defaults to the selected
2773 one. An attempt to resize the root window of a frame will raise
2774 an error though.
2776 DELTA a positive number means WINDOW shall be enlarged by DELTA
2777 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2778 lines.
2780 Optional argument HORIZONTAL non-nil means resize WINDOW
2781 horizontally by DELTA columns. In this case a positive DELTA
2782 means enlarge WINDOW by DELTA columns. DELTA negative means
2783 WINDOW shall be shrunk by -DELTA columns.
2785 Optional argument IGNORE, if non-nil, means to ignore restraints
2786 induced by fixed size windows or the values of the variables
2787 `window-min-height' and `window-min-width'. The following values
2788 have special meanings: `safe' means that in addition live windows
2789 are allowed to get as small as `window-safe-min-height' lines and
2790 `window-safe-min-width' columns. `preserved' means to ignore
2791 only restrictions induced by `window-preserve-size'. If IGNORE
2792 is a window, then ignore restrictions for that window only.
2794 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2795 pixels.
2797 This function resizes other windows proportionally and never
2798 deletes any windows. If you want to move only the low (right)
2799 edge of WINDOW consider using `adjust-window-trailing-edge'
2800 instead."
2801 (setq window (window-normalize-window window))
2802 (let* ((frame (window-frame window))
2803 (minibuffer-window (minibuffer-window frame))
2804 sibling)
2805 (setq delta (window--size-to-pixel
2806 window delta horizontal pixelwise t))
2807 (cond
2808 ((eq window (frame-root-window frame))
2809 (error "Cannot resize the root window of a frame"))
2810 ((window-minibuffer-p window)
2811 (if horizontal
2812 (error "Cannot resize minibuffer window horizontally")
2813 (window--resize-mini-window window delta)))
2814 ((and (not horizontal)
2815 (window-full-height-p window)
2816 (eq (window-frame minibuffer-window) frame)
2817 (or (not resize-mini-windows)
2818 (eq minibuffer-window (active-minibuffer-window))))
2819 ;; If WINDOW is full height and either `resize-mini-windows' is
2820 ;; nil or the minibuffer window is active, resize the minibuffer
2821 ;; window.
2822 (window--resize-mini-window minibuffer-window (- delta)))
2823 ((or (window--resizable-p
2824 window delta horizontal ignore nil nil nil t)
2825 (and (not ignore)
2826 (setq ignore 'preserved)
2827 (window--resizable-p
2828 window delta horizontal ignore nil nil nil t)))
2829 (window--resize-reset frame horizontal)
2830 (window--resize-this-window window delta horizontal ignore t)
2831 (if (and (not (eq window-combination-resize t))
2832 (window-combined-p window horizontal)
2833 (setq sibling (or (window-right window) (window-left window)))
2834 (window-sizable-p
2835 sibling (- delta) horizontal ignore t))
2836 ;; If window-combination-resize is nil, WINDOW is part of an
2837 ;; iso-combination, and WINDOW's neighboring right or left
2838 ;; sibling can be resized as requested, resize that sibling.
2839 (let ((normal-delta
2840 (/ (float delta)
2841 (window-size (window-parent window) horizontal t))))
2842 (window--resize-this-window sibling (- delta) horizontal nil t)
2843 (set-window-new-normal
2844 window (+ (window-normal-size window horizontal)
2845 normal-delta))
2846 (set-window-new-normal
2847 sibling (- (window-normal-size sibling horizontal)
2848 normal-delta)))
2849 ;; Otherwise, resize all other windows in the same combination.
2850 (window--resize-siblings window delta horizontal ignore))
2851 (when (window--resize-apply-p frame horizontal)
2852 (if (window-resize-apply frame horizontal)
2853 (progn
2854 (window--pixel-to-total frame horizontal)
2855 (run-window-configuration-change-hook frame))
2856 (error "Failed to apply resizing %s" window))))
2858 (error "Cannot resize window %s" window)))))
2860 (defun window-resize-no-error (window delta &optional horizontal ignore pixelwise)
2861 "Resize WINDOW vertically if it is resizable by DELTA lines.
2862 This function is like `window-resize' but does not signal an
2863 error when WINDOW cannot be resized. For the meaning of the
2864 optional arguments see the documentation of `window-resize'."
2865 (when (window--resizable-p
2866 window delta horizontal ignore nil nil nil pixelwise)
2867 (window-resize window delta horizontal ignore pixelwise)))
2869 (defun window--resize-child-windows-skip-p (window)
2870 "Return non-nil if WINDOW shall be skipped by resizing routines."
2871 (memq (window-new-normal window) '(ignore stuck skip)))
2873 (defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
2874 "Recursively set new normal height of child windows of window PARENT.
2875 HORIZONTAL non-nil means set the new normal width of these
2876 windows. WINDOW specifies a child window of PARENT that has been
2877 resized by THIS-DELTA lines (columns).
2879 Optional argument TRAIL either `before' or `after' means set values
2880 only for windows before or after WINDOW. Optional argument
2881 OTHER-DELTA, a number, specifies that this many lines (columns)
2882 have been obtained from (or returned to) an ancestor window of
2883 PARENT in order to resize WINDOW."
2884 (let* ((delta-normal
2885 (if (and (= (- this-delta)
2886 (window-size window horizontal t))
2887 (zerop other-delta))
2888 ;; When WINDOW gets deleted and we can return its entire
2889 ;; space to its siblings, use WINDOW's normal size as the
2890 ;; normal delta.
2891 (- (window-normal-size window horizontal))
2892 ;; In any other case calculate the normal delta from the
2893 ;; relation of THIS-DELTA to the total size of PARENT.
2894 (/ (float this-delta)
2895 (window-size parent horizontal t))))
2896 (sub (window-child parent))
2897 (parent-normal 0.0)
2898 (skip (eq trail 'after)))
2900 ;; Set parent-normal to the sum of the normal sizes of all child
2901 ;; windows of PARENT that shall be resized, excluding only WINDOW
2902 ;; and any windows specified by the optional TRAIL argument.
2903 (while sub
2904 (cond
2905 ((eq sub window)
2906 (setq skip (eq trail 'before)))
2907 (skip)
2909 (setq parent-normal
2910 (+ parent-normal (window-normal-size sub horizontal)))))
2911 (setq sub (window-right sub)))
2913 ;; Set the new normal size of all child windows of PARENT from what
2914 ;; they should have contributed for recovering THIS-DELTA lines
2915 ;; (columns).
2916 (setq sub (window-child parent))
2917 (setq skip (eq trail 'after))
2918 (while sub
2919 (cond
2920 ((eq sub window)
2921 (setq skip (eq trail 'before)))
2922 (skip)
2924 (let ((old-normal (window-normal-size sub horizontal)))
2925 (set-window-new-normal
2926 sub (min 1.0 ; Don't get larger than 1.
2927 (max (- old-normal
2928 (* (/ old-normal parent-normal)
2929 delta-normal))
2930 ;; Don't drop below 0.
2931 0.0))))))
2932 (setq sub (window-right sub)))
2934 (when (numberp other-delta)
2935 ;; Set the new normal size of windows from what they should have
2936 ;; contributed for recovering OTHER-DELTA lines (columns).
2937 (setq delta-normal (/ (float (window-size parent horizontal t))
2938 (+ (window-size parent horizontal t)
2939 other-delta)))
2940 (setq sub (window-child parent))
2941 (setq skip (eq trail 'after))
2942 (while sub
2943 (cond
2944 ((eq sub window)
2945 (setq skip (eq trail 'before)))
2946 (skip)
2948 (set-window-new-normal
2949 sub (min 1.0 ; Don't get larger than 1.
2950 (max (* (window-new-normal sub) delta-normal)
2951 ;; Don't drop below 0.
2952 0.0)))))
2953 (setq sub (window-right sub))))
2955 ;; Set the new normal size of WINDOW to what is left by the sum of
2956 ;; the normal sizes of its siblings.
2957 (set-window-new-normal
2958 window
2959 (let ((sum 0))
2960 (setq sub (window-child parent))
2961 (while sub
2962 (cond
2963 ((eq sub window))
2964 ((not (numberp (window-new-normal sub)))
2965 (setq sum (+ sum (window-normal-size sub horizontal))))
2967 (setq sum (+ sum (window-new-normal sub)))))
2968 (setq sub (window-right sub)))
2969 ;; Don't get larger than 1 or smaller than 0.
2970 (min 1.0 (max (- 1.0 sum) 0.0))))))
2972 (defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge char-size)
2973 "Resize child windows of window PARENT vertically by DELTA pixels.
2974 PARENT must be a vertically combined internal window.
2976 Optional argument HORIZONTAL non-nil means resize child windows
2977 of PARENT horizontally by DELTA pixels. In this case PARENT must
2978 be a horizontally combined internal window.
2980 WINDOW, if specified, must denote a child window of PARENT that
2981 is resized by DELTA pixels.
2983 The optional argument IGNORE has the same meaning as for
2984 `window-resizable'.
2986 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2987 of windows that shall be resized. If TRAIL equals `before',
2988 resize only windows on the left or above EDGE. If TRAIL equals
2989 `after', resize only windows on the right or below EDGE. Also,
2990 preferably only resize windows adjacent to EDGE.
2992 If the optional argument CHAR-SIZE is a positive integer, it specifies
2993 the number of pixels by which windows are incrementally resized.
2994 If CHAR-SIZE is nil, this means to use the value of
2995 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2997 Return the symbol `normalized' if new normal sizes have been
2998 already set by this routine."
2999 (let* ((first (window-child parent))
3000 (last (window-last-child parent))
3001 (parent-total (+ (window-size parent horizontal t)
3002 delta))
3003 (char-size (or char-size
3004 (and window-resize-pixelwise 1)
3005 (frame-char-size window horizontal)))
3006 sub best-window best-value best-delta)
3008 (if (and edge (memq trail '(before after))
3009 (progn
3010 (setq sub first)
3011 (while (and (window-right sub)
3012 (or (and (eq trail 'before)
3013 (not (window--resize-child-windows-skip-p
3014 (window-right sub))))
3015 (and (eq trail 'after)
3016 (window--resize-child-windows-skip-p sub))))
3017 (setq sub (window-right sub)))
3018 sub)
3019 (if horizontal
3020 (if (eq trail 'before)
3021 (= (+ (window-pixel-left sub) (window-pixel-width sub))
3022 edge)
3023 (= (window-pixel-left sub) edge))
3024 (if (eq trail 'before)
3025 (= (+ (window-pixel-top sub) (window-pixel-height sub))
3026 edge)
3027 (= (window-pixel-top sub) edge)))
3028 (window-sizable-p sub delta horizontal ignore t))
3029 ;; Resize only windows adjacent to EDGE.
3030 (progn
3031 (window--resize-this-window
3032 sub delta horizontal ignore t trail edge)
3033 (if (and window (eq (window-parent sub) parent))
3034 (progn
3035 ;; Assign new normal sizes.
3036 (set-window-new-normal
3037 sub (/ (float (window-new-pixel sub)) parent-total))
3038 (set-window-new-normal
3039 window (- (window-normal-size window horizontal)
3040 (- (window-new-normal sub)
3041 (window-normal-size sub horizontal)))))
3042 (window--resize-child-windows-normal
3043 parent horizontal sub 0 trail delta))
3044 ;; Return 'normalized to notify `window--resize-siblings' that
3045 ;; normal sizes have been already set.
3046 'normalized)
3047 ;; Resize all windows proportionally.
3048 (setq sub last)
3049 (while sub
3050 (cond
3051 ((or (window--resize-child-windows-skip-p sub)
3052 ;; Ignore windows to skip and fixed-size child windows -
3053 ;; in the latter case make it a window to skip.
3054 (and (not ignore)
3055 (window-size-fixed-p sub horizontal ignore)
3056 (set-window-new-normal sub 'ignore))))
3057 ((< delta 0)
3058 ;; When shrinking store the number of lines/cols we can get
3059 ;; from this window here together with the total/normal size
3060 ;; factor.
3061 (set-window-new-normal
3063 (cons
3064 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
3065 (window-min-delta sub horizontal ignore trail t nil t)
3066 (- (/ (float (window-size sub horizontal t))
3067 parent-total)
3068 (window-normal-size sub horizontal)))))
3069 ((> delta 0)
3070 ;; When enlarging store the total/normal size factor only
3071 (set-window-new-normal
3073 (- (/ (float (window-size sub horizontal t))
3074 parent-total)
3075 (window-normal-size sub horizontal)))))
3077 (setq sub (window-left sub)))
3079 (cond
3080 ((< delta 0)
3081 ;; Shrink windows by delta.
3082 (setq best-window t)
3083 (while (and best-window (not (zerop delta)))
3084 (setq sub last)
3085 (setq best-window nil)
3086 (setq best-value most-negative-fixnum)
3087 (while sub
3088 (when (and (consp (window-new-normal sub))
3089 (not (<= (car (window-new-normal sub)) 0))
3090 (> (cdr (window-new-normal sub)) best-value))
3091 (setq best-window sub)
3092 (setq best-value (cdr (window-new-normal sub))))
3094 (setq sub (window-left sub)))
3096 (when best-window
3097 (setq best-delta (min (car (window-new-normal best-window))
3098 char-size (- delta)))
3099 (setq delta (+ delta best-delta))
3100 (set-window-new-pixel best-window (- best-delta) t)
3101 (set-window-new-normal
3102 best-window
3103 (if (= (car (window-new-normal best-window)) best-delta)
3104 'skip ; We can't shrink best-window any further.
3105 (cons (- (car (window-new-normal best-window)) best-delta)
3106 (- (/ (float (window-new-pixel best-window))
3107 parent-total)
3108 (window-normal-size best-window horizontal))))))))
3109 ((> delta 0)
3110 ;; Enlarge windows by delta.
3111 (setq best-window t)
3112 (while (and best-window (not (zerop delta)))
3113 (setq sub last)
3114 (setq best-window nil)
3115 (setq best-value most-positive-fixnum)
3116 (while sub
3117 (when (and (numberp (window-new-normal sub))
3118 (< (window-new-normal sub) best-value))
3119 (setq best-window sub)
3120 (setq best-value (window-new-normal sub)))
3122 (setq sub (window-left sub)))
3124 (when best-window
3125 (setq best-delta (min delta char-size))
3126 (setq delta (- delta best-delta))
3127 (set-window-new-pixel best-window best-delta t)
3128 (set-window-new-normal
3129 best-window
3130 (- (/ (float (window-new-pixel best-window))
3131 parent-total)
3132 (window-normal-size best-window horizontal)))))))
3134 (when best-window
3135 (setq sub last)
3136 (while sub
3137 (when (or (consp (window-new-normal sub))
3138 (numberp (window-new-normal sub)))
3139 ;; Reset new normal size fields so `window-resize-apply'
3140 ;; won't use them to apply new sizes.
3141 (set-window-new-normal sub))
3143 (unless (eq (window-new-normal sub) 'ignore)
3144 ;; Resize this window's child windows (back-engineering
3145 ;; delta from sub's old and new total sizes).
3146 (let ((delta (- (window-new-pixel sub)
3147 (window-size sub horizontal t))))
3148 (unless (and (zerop delta) (not trail))
3149 ;; For the TRAIL non-nil case we have to resize SUB
3150 ;; recursively even if it's size does not change.
3151 (window--resize-this-window
3152 sub delta horizontal ignore nil trail edge))))
3153 (setq sub (window-left sub)))))))
3155 (defun window--resize-siblings (window delta &optional horizontal ignore trail edge char-size)
3156 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
3157 Optional argument HORIZONTAL non-nil means resize other windows
3158 when WINDOW is resized horizontally by DELTA pixels. WINDOW
3159 itself is not resized by this function.
3161 The optional argument IGNORE has the same meaning as for
3162 `window-resizable'.
3164 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3165 of windows that shall be resized. If TRAIL equals `before',
3166 resize only windows on the left or above EDGE. If TRAIL equals
3167 `after', resize only windows on the right or below EDGE. Also,
3168 preferably only resize windows adjacent to EDGE."
3169 (when (window-parent window)
3170 (let* ((parent (window-parent window))
3171 (sub (window-child parent)))
3172 (if (window-combined-p sub horizontal)
3173 ;; In an iso-combination try to extract DELTA from WINDOW's
3174 ;; siblings.
3175 (let ((skip (eq trail 'after))
3176 this-delta other-delta)
3177 ;; Decide which windows shall be left alone.
3178 (while sub
3179 (cond
3180 ((eq sub window)
3181 ;; Make sure WINDOW is left alone when
3182 ;; resizing its siblings.
3183 (set-window-new-normal sub 'ignore)
3184 (setq skip (eq trail 'before)))
3185 (skip
3186 ;; Make sure this sibling is left alone when
3187 ;; resizing its siblings.
3188 (set-window-new-normal sub 'ignore))
3189 ((not (window-size-fixed-p sub horizontal ignore))
3190 ;; Set this-delta to t to signal that we found a sibling
3191 ;; of WINDOW whose size is not fixed.
3192 (setq this-delta t)))
3194 (setq sub (window-right sub)))
3196 ;; Set this-delta to what we can get from WINDOW's siblings.
3197 (if (= (- delta) (window-size window horizontal t))
3198 ;; A deletion, presumably. We must handle this case
3199 ;; specially since `window--resizable' can't be used.
3200 (if this-delta
3201 ;; There's at least one resizable sibling we can
3202 ;; give WINDOW's size to.
3203 (setq this-delta delta)
3204 ;; No resizable sibling exists.
3205 (setq this-delta 0))
3206 ;; Any other form of resizing.
3207 (setq this-delta
3208 (window--resizable
3209 window delta horizontal ignore trail t nil t)))
3211 ;; Set other-delta to what we still have to get from
3212 ;; ancestor windows of parent.
3213 (setq other-delta (- delta this-delta))
3214 (unless (zerop other-delta)
3215 ;; Unless we got everything from WINDOW's siblings, PARENT
3216 ;; must be resized by other-delta lines or columns.
3217 (set-window-new-pixel parent other-delta 'add))
3219 (if (zerop this-delta)
3220 ;; We haven't got anything from WINDOW's siblings but we
3221 ;; must update the normal sizes to respect other-delta.
3222 (window--resize-child-windows-normal
3223 parent horizontal window this-delta trail other-delta)
3224 ;; We did get something from WINDOW's siblings which means
3225 ;; we have to resize their child windows.
3226 (unless (eq (window--resize-child-windows
3227 parent (- this-delta) horizontal
3228 window ignore trail edge char-size)
3229 ;; If `window--resize-child-windows' returns
3230 ;; 'normalized, this means it has set the
3231 ;; normal sizes already.
3232 'normalized)
3233 ;; Set the normal sizes.
3234 (window--resize-child-windows-normal
3235 parent horizontal window this-delta trail other-delta))
3236 ;; Set DELTA to what we still have to get from ancestor
3237 ;; windows.
3238 (setq delta other-delta)))
3240 ;; In an ortho-combination all siblings of WINDOW must be
3241 ;; resized by DELTA.
3242 (set-window-new-pixel parent delta 'add)
3243 (while sub
3244 (unless (eq sub window)
3245 (window--resize-this-window
3246 sub delta horizontal ignore t))
3247 (setq sub (window-right sub))))
3249 (unless (zerop delta)
3250 ;; "Go up."
3251 (window--resize-siblings
3252 parent delta horizontal ignore trail edge char-size)))))
3254 (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge char-size)
3255 "Resize WINDOW vertically by DELTA pixels.
3256 Optional argument HORIZONTAL non-nil means resize WINDOW
3257 horizontally by DELTA pixels.
3259 The optional argument IGNORE has the same meaning as for
3260 `window-resizable'. Optional argument ADD non-nil means add
3261 DELTA to the new total size of WINDOW.
3263 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3264 of windows that shall be resized. If TRAIL equals `before',
3265 resize only windows on the left or above EDGE. If TRAIL equals
3266 `after', resize only windows on the right or below EDGE. Also,
3267 preferably only resize windows adjacent to EDGE.
3269 If the optional argument CHAR-SIZE is a positive integer, it specifies
3270 the number of pixels by which windows are incrementally resized.
3271 If CHAR-SIZE is nil, this means to use the value of
3272 `frame-char-height' or `frame-char-width' of WINDOW's frame.
3274 This function recursively resizes WINDOW's child windows to fit the
3275 new size. Make sure that WINDOW is `window--resizable' before
3276 calling this function. Note that this function does not resize
3277 siblings of WINDOW or WINDOW's parent window. You have to
3278 eventually call `window-resize-apply' in order to make resizing
3279 actually take effect."
3280 (when add
3281 ;; Add DELTA to the new total size of WINDOW.
3282 (set-window-new-pixel window delta t))
3284 (let ((sub (window-child window)))
3285 (cond
3286 ((not sub))
3287 ((window-combined-p sub horizontal)
3288 ;; In an iso-combination resize child windows according to their
3289 ;; normal sizes.
3290 (window--resize-child-windows
3291 window delta horizontal nil ignore trail edge char-size))
3292 ;; In an ortho-combination resize each child window by DELTA.
3294 (while sub
3295 (window--resize-this-window
3296 sub delta horizontal ignore t trail edge char-size)
3297 (setq sub (window-right sub)))))))
3299 (defun window--resize-root-window (window delta horizontal ignore pixelwise)
3300 "Resize root window WINDOW vertically by DELTA lines.
3301 HORIZONTAL non-nil means resize root window WINDOW horizontally
3302 by DELTA columns.
3304 IGNORE non-nil means ignore any restrictions imposed by fixed
3305 size windows, `window-min-height' or `window-min-width' settings.
3307 This function is only called by the frame resizing routines. It
3308 resizes windows proportionally and never deletes any windows."
3309 (when (and (windowp window) (numberp delta))
3310 (let ((pixel-delta
3311 (if pixelwise
3312 delta
3313 (window--size-to-pixel window delta horizontal))))
3314 (when (window-sizable-p window pixel-delta horizontal ignore t)
3315 (window--resize-reset (window-frame window) horizontal)
3316 (window--resize-this-window
3317 window pixel-delta horizontal ignore t)))))
3319 (defun window--resize-root-window-vertically (window delta pixelwise)
3320 "Resize root window WINDOW vertically by DELTA lines.
3321 If DELTA is less than zero and we can't shrink WINDOW by DELTA
3322 lines, shrink it as much as possible. If DELTA is greater than
3323 zero, this function can resize fixed-size windows in order to
3324 recover the necessary lines. Return the number of lines that
3325 were recovered.
3327 Third argument PIXELWISE non-nil means to interpret DELTA as
3328 pixels and return the number of pixels that were recovered.
3330 This function is called by the minibuffer window resizing
3331 routines."
3332 (let* ((frame (window-frame window))
3333 (pixel-delta
3334 (cond
3335 (pixelwise
3336 delta)
3337 ((numberp delta)
3338 (* (frame-char-height frame) delta))
3339 (t 0)))
3340 ignore)
3341 (cond
3342 ((zerop pixel-delta))
3343 ((< pixel-delta 0)
3344 (setq pixel-delta (window-sizable window pixel-delta nil nil pixelwise))
3345 (window--resize-reset frame)
3346 ;; When shrinking the root window, emulate an edge drag in order
3347 ;; to not resize other windows if we can avoid it (Bug#12419).
3348 (window--resize-this-window
3349 window pixel-delta nil ignore t 'before
3350 (+ (window-pixel-top window) (window-pixel-height window)))
3351 ;; Don't record new normal sizes to make sure that shrinking back
3352 ;; proportionally works as intended.
3353 (walk-window-tree
3354 (lambda (window) (set-window-new-normal window 'ignore)) frame t))
3355 ((> pixel-delta 0)
3356 (window--resize-reset frame)
3357 (unless (window-sizable window pixel-delta nil nil pixelwise)
3358 (setq ignore t))
3359 ;; When growing the root window, resize proportionally. This
3360 ;; should give windows back their original sizes (hopefully).
3361 (window--resize-this-window
3362 window pixel-delta nil ignore t)))
3363 ;; Return the possibly adjusted DELTA.
3364 (if pixelwise
3365 pixel-delta
3366 (/ pixel-delta (frame-char-height frame)))))
3368 (defun window--sanitize-window-sizes (horizontal)
3369 "Assert that all windows on selected frame are large enough.
3370 If necessary and possible, make sure that every window on frame
3371 FRAME has its minimum height. Optional argument HORIZONTAL
3372 non-nil means to make sure that every window on frame FRAME has
3373 its minimum width. The minimum height/width of a window is the
3374 respective value returned by `window-min-size' for that window.
3376 Return t if all windows were resized appropriately. Return nil
3377 if at least one window could not be resized as requested, which
3378 may happen when the FRAME is not large enough to accommodate it."
3379 (let ((value t))
3380 (walk-window-tree
3381 (lambda (window)
3382 (let ((delta (- (window-min-size window horizontal nil t)
3383 (window-size window horizontal t))))
3384 (when (> delta 0)
3385 (if (window-resizable-p window delta horizontal nil t)
3386 (window-resize window delta horizontal nil t)
3387 (setq value nil))))))
3388 value))
3390 (defun adjust-window-trailing-edge (window delta &optional horizontal pixelwise)
3391 "Move WINDOW's bottom edge by DELTA lines.
3392 Optional argument HORIZONTAL non-nil means move WINDOW's right
3393 edge by DELTA columns. WINDOW must be a valid window and
3394 defaults to the selected one.
3396 Optional argument PIXELWISE non-nil means interpret DELTA as
3397 number of pixels.
3399 If DELTA is greater than zero, move the edge downwards or to the
3400 right. If DELTA is less than zero, move the edge upwards or to
3401 the left. If the edge can't be moved by DELTA lines or columns,
3402 move it as far as possible in the desired direction."
3403 (setq window (window-normalize-window window))
3404 (let* ((frame (window-frame window))
3405 (minibuffer-window (minibuffer-window frame))
3406 (right window)
3407 left first-left first-right this-delta min-delta max-delta ignore)
3409 (unless pixelwise
3410 (setq pixelwise t)
3411 (setq delta (* delta (frame-char-size window horizontal))))
3413 ;; Find the edge we want to move.
3414 (while (and (or (not (window-combined-p right horizontal))
3415 (not (window-right right)))
3416 (setq right (window-parent right))))
3417 (cond
3418 ((and (not right) (not horizontal)
3419 ;; Resize the minibuffer window if it's on the same frame as
3420 ;; and immediately below WINDOW and it's either active or
3421 ;; `resize-mini-windows' is nil.
3422 (eq (window-frame minibuffer-window) frame)
3423 (= (nth 1 (window-pixel-edges minibuffer-window))
3424 (nth 3 (window-pixel-edges window)))
3425 (or (not resize-mini-windows)
3426 (eq minibuffer-window (active-minibuffer-window))))
3427 (window--resize-mini-window minibuffer-window (- delta)))
3428 ((or (not (setq left right)) (not (setq right (window-right right))))
3429 (if horizontal
3430 (user-error "No window on the right of this one")
3431 (user-error "No window below this one")))
3433 ;; Set LEFT to the first resizable window on the left. This step is
3434 ;; needed to handle fixed-size windows.
3435 (setq first-left left)
3436 (while (and left
3437 (or (window-size-fixed-p left horizontal)
3438 (and (< delta 0)
3439 (<= (window-size left horizontal t)
3440 (window-min-size left horizontal nil 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)))))
3447 (unless left
3448 ;; We have to resize a size-preserved window. Start again with
3449 ;; the window initially on the left.
3450 (setq ignore 'preserved)
3451 (setq left first-left)
3452 (while (and left
3453 (or (window-size-fixed-p left horizontal 'preserved)
3454 (and (< delta 0)
3455 (<= (window-size left horizontal t)
3456 (window-min-size
3457 left horizontal 'preserved t)))))
3458 (setq left
3459 (or (window-left left)
3460 (progn
3461 (while (and (setq left (window-parent left))
3462 (not (window-combined-p left horizontal))))
3463 (window-left left)))))
3465 (unless left
3466 (if horizontal
3467 (user-error "No resizable window on the left of this one")
3468 (user-error "No resizable window above this one"))))
3470 ;; Set RIGHT to the first resizable window on the right. This step
3471 ;; is needed to handle fixed-size windows.
3472 (setq first-right right)
3473 (while (and right
3474 (or (window-size-fixed-p right horizontal)
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 ;; We have to resize a size-preserved window. Start again with
3487 ;; the window initially on the right.
3488 (setq ignore 'preserved)
3489 (setq right first-right)
3490 (while (and right
3491 (or (window-size-fixed-p right horizontal 'preserved)
3492 (and (> delta 0)
3493 (<= (window-size right horizontal t)
3494 (window-min-size
3495 right horizontal 'preserved t)))))
3496 (setq right
3497 (or (window-right right)
3498 (progn
3499 (while (and (setq right (window-parent right))
3500 (not (window-combined-p right horizontal))))
3501 (window-right right)))))
3502 (unless right
3503 (if horizontal
3504 (user-error "No resizable window on the right of this one")
3505 (user-error "No resizable window below this one"))))
3507 ;; LEFT and RIGHT (which might be both internal windows) are now the
3508 ;; two windows we want to resize.
3509 (cond
3510 ((> delta 0)
3511 (setq max-delta
3512 (window--max-delta-1
3513 left 0 horizontal ignore 'after nil pixelwise))
3514 (setq min-delta
3515 (window--min-delta-1
3516 right (- delta) horizontal ignore 'before nil pixelwise))
3517 (when (or (< max-delta delta) (> min-delta (- delta)))
3518 ;; We can't get the whole DELTA - move as far as possible.
3519 (setq delta (min max-delta (- min-delta))))
3520 (unless (zerop delta)
3521 ;; Start resizing.
3522 (window--resize-reset frame horizontal)
3523 ;; Try to enlarge LEFT first.
3524 (setq this-delta
3525 (window--resizable
3526 left delta horizontal ignore 'after nil nil pixelwise))
3527 (unless (zerop this-delta)
3528 (window--resize-this-window
3529 left this-delta horizontal ignore t 'before
3530 (if horizontal
3531 (+ (window-pixel-left left) (window-pixel-width left))
3532 (+ (window-pixel-top left) (window-pixel-height left)))))
3533 ;; Shrink windows on right of LEFT.
3534 (window--resize-siblings
3535 left delta horizontal ignore 'after
3536 (if horizontal
3537 (window-pixel-left right)
3538 (window-pixel-top right)))))
3539 ((< delta 0)
3540 (setq max-delta
3541 (window--max-delta-1
3542 right 0 horizontal ignore 'before nil pixelwise))
3543 (setq min-delta
3544 (window--min-delta-1
3545 left delta horizontal ignore 'after nil pixelwise))
3546 (when (or (< max-delta (- delta)) (> min-delta delta))
3547 ;; We can't get the whole DELTA - move as far as possible.
3548 (setq delta (max (- max-delta) min-delta)))
3549 (unless (zerop delta)
3550 ;; Start resizing.
3551 (window--resize-reset frame horizontal)
3552 ;; Try to enlarge RIGHT.
3553 (setq this-delta
3554 (window--resizable
3555 right (- delta) horizontal ignore 'before nil nil pixelwise))
3556 (unless (zerop this-delta)
3557 (window--resize-this-window
3558 right this-delta horizontal ignore t 'after
3559 (if horizontal
3560 (window-pixel-left right)
3561 (window-pixel-top right))))
3562 ;; Shrink windows on left of RIGHT.
3563 (window--resize-siblings
3564 right (- delta) horizontal ignore 'before
3565 (if horizontal
3566 (+ (window-pixel-left left) (window-pixel-width left))
3567 (+ (window-pixel-top left) (window-pixel-height left)))))))
3568 (unless (zerop delta)
3569 ;; Don't report an error in the standard case.
3570 (when (window--resize-apply-p frame horizontal)
3571 (if (window-resize-apply frame horizontal)
3572 (progn
3573 (window--pixel-to-total frame horizontal)
3574 (run-window-configuration-change-hook frame))
3575 ;; But do report an error if applying the changes fails.
3576 (error "Failed adjusting window %s" window))))))))
3578 (defun enlarge-window (delta &optional horizontal)
3579 "Make the selected window DELTA lines taller.
3580 Interactively, if no argument is given, make the selected window
3581 one line taller. If optional argument HORIZONTAL is non-nil,
3582 make selected window wider by DELTA columns. If DELTA is
3583 negative, shrink selected window by -DELTA lines or columns."
3584 (interactive "p")
3585 (let ((minibuffer-window (minibuffer-window)))
3586 (when (window-preserved-size nil horizontal)
3587 (window-preserve-size nil horizontal))
3588 (cond
3589 ((zerop delta))
3590 ((window-size-fixed-p nil horizontal)
3591 (user-error "Selected window has fixed size"))
3592 ((window-minibuffer-p)
3593 (if horizontal
3594 (user-error "Cannot resize minibuffer window horizontally")
3595 (window--resize-mini-window
3596 (selected-window) (* delta (frame-char-height)))))
3597 ((and (not horizontal)
3598 (window-full-height-p)
3599 (eq (window-frame minibuffer-window) (selected-frame))
3600 (not resize-mini-windows))
3601 ;; If the selected window is full height and `resize-mini-windows'
3602 ;; is nil, resize the minibuffer window.
3603 (window--resize-mini-window
3604 minibuffer-window (* (- delta) (frame-char-height))))
3605 ((window--resizable-p nil delta horizontal)
3606 (window-resize nil delta horizontal))
3607 ((window--resizable-p nil delta horizontal 'preserved)
3608 (window-resize nil delta horizontal 'preserved))
3609 ((eq this-command
3610 (if horizontal 'enlarge-window-horizontally 'enlarge-window))
3611 ;; For backward compatibility don't signal an error unless this
3612 ;; command is `enlarge-window(-horizontally)'.
3613 (user-error "Cannot enlarge selected window"))
3615 (window-resize
3616 nil (if (> delta 0)
3617 (window-max-delta nil horizontal)
3618 (- (window-min-delta nil horizontal)))
3619 horizontal)))))
3621 (defun shrink-window (delta &optional horizontal)
3622 "Make the selected window DELTA lines smaller.
3623 Interactively, if no argument is given, make the selected window
3624 one line smaller. If optional argument HORIZONTAL is non-nil,
3625 make selected window narrower by DELTA columns. If DELTA is
3626 negative, enlarge selected window by -DELTA lines or columns."
3627 (interactive "p")
3628 (let ((minibuffer-window (minibuffer-window)))
3629 (when (window-preserved-size nil horizontal)
3630 (window-preserve-size nil horizontal))
3631 (cond
3632 ((zerop delta))
3633 ((window-size-fixed-p nil horizontal)
3634 (user-error "Selected window has fixed size"))
3635 ((window-minibuffer-p)
3636 (if horizontal
3637 (user-error "Cannot resize minibuffer window horizontally")
3638 (window--resize-mini-window
3639 (selected-window) (* (- delta) (frame-char-height)))))
3640 ((and (not horizontal)
3641 (window-full-height-p)
3642 (eq (window-frame minibuffer-window) (selected-frame))
3643 (not resize-mini-windows))
3644 ;; If the selected window is full height and `resize-mini-windows'
3645 ;; is nil, resize the minibuffer window.
3646 (window--resize-mini-window
3647 minibuffer-window (* delta (frame-char-height))))
3648 ((window--resizable-p nil (- delta) horizontal)
3649 (window-resize nil (- delta) horizontal))
3650 ((window--resizable-p nil (- delta) horizontal 'preserved)
3651 (window-resize nil (- delta) horizontal 'preserved))
3652 ((eq this-command
3653 (if horizontal 'shrink-window-horizontally 'shrink-window))
3654 ;; For backward compatibility don't signal an error unless this
3655 ;; command is `shrink-window(-horizontally)'.
3656 (user-error "Cannot shrink selected window"))
3658 (window-resize
3659 nil (if (> delta 0)
3660 (- (window-min-delta nil horizontal))
3661 (window-max-delta nil horizontal))
3662 horizontal)))))
3664 (defun maximize-window (&optional window)
3665 "Maximize WINDOW.
3666 Make WINDOW as large as possible without deleting any windows.
3667 WINDOW must be a valid window and defaults to the selected one.
3669 If the option `window-resize-pixelwise' is non-nil maximize
3670 WINDOW pixelwise."
3671 (interactive)
3672 (setq window (window-normalize-window window))
3673 (window-resize
3674 window (window-max-delta window nil nil nil nil nil window-resize-pixelwise)
3675 nil nil window-resize-pixelwise)
3676 (window-resize
3677 window (window-max-delta window t nil nil nil nil window-resize-pixelwise)
3678 t nil window-resize-pixelwise))
3680 (defun minimize-window (&optional window)
3681 "Minimize WINDOW.
3682 Make WINDOW as small as possible without deleting any windows.
3683 WINDOW must be a valid window and defaults to the selected one.
3685 If the option `window-resize-pixelwise' is non-nil minimize
3686 WINDOW pixelwise."
3687 (interactive)
3688 (setq window (window-normalize-window window))
3689 (window-resize
3690 window
3691 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise))
3692 nil nil window-resize-pixelwise)
3693 (window-resize
3694 window
3695 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise))
3696 t nil window-resize-pixelwise))
3698 ;;; Window edges
3699 (defun window-edges (&optional window body absolute pixelwise)
3700 "Return a list of the edge distances of WINDOW.
3701 WINDOW must be a valid window and defaults to the selected one.
3702 The list returned has the form (LEFT TOP RIGHT BOTTOM).
3704 If the optional argument BODY is nil, this means to return the
3705 edges corresponding to the total size of WINDOW. BODY non-nil
3706 means to return the edges of WINDOW's body (aka text area). If
3707 BODY is non-nil, WINDOW must specify a live window.
3709 Optional argument ABSOLUTE nil means to return edges relative to
3710 the position of WINDOW's native frame. ABSOLUTE non-nil means to
3711 return coordinates relative to the origin - the position (0, 0) -
3712 of FRAME's display. On non-graphical systems this argument has
3713 no effect.
3715 Optional argument PIXELWISE nil means to return the coordinates
3716 in terms of the canonical character width and height of WINDOW's
3717 frame, rounded if necessary. PIXELWISE non-nil means to return
3718 the coordinates in pixels where the values for RIGHT and BOTTOM
3719 are one more than the actual value of these edges. Note that if
3720 ABSOLUTE is non-nil, PIXELWISE is implicitly non-nil too."
3721 (let* ((window (window-normalize-window window body))
3722 (frame (window-frame window))
3723 (border-width (frame-internal-border-width frame))
3724 (char-width (frame-char-width frame))
3725 (char-height (frame-char-height frame))
3726 (left (if pixelwise
3727 (+ (window-pixel-left window) border-width)
3728 (+ (window-left-column window)
3729 (/ border-width char-width))))
3730 (left-body
3731 (when body
3732 (+ (window-pixel-left window) border-width
3733 (if (eq (car (window-current-scroll-bars window)) 'left)
3734 (window-scroll-bar-width window)
3736 (nth 0 (window-fringes window))
3737 (* (or (nth 0 (window-margins window)) 0) char-width))))
3738 (top (if pixelwise
3739 (+ (window-pixel-top window) border-width)
3740 (+ (window-top-line window)
3741 (/ border-width char-height))))
3742 (top-body
3743 (when body
3744 (+ (window-pixel-top window) border-width
3745 (window-header-line-height window))))
3746 (right (+ left (if pixelwise
3747 (window-pixel-width window)
3748 (window-total-width window))))
3749 (right-body (and body (+ left-body (window-body-width window t))))
3750 (bottom (+ top (if pixelwise
3751 (window-pixel-height window)
3752 (window-total-height window))))
3753 (bottom-body (and body (+ top-body (window-body-height window t)))))
3754 (if absolute
3755 (let* ((native-edges (frame-edges frame 'native-edges))
3756 (left-off (nth 0 native-edges))
3757 (top-off (nth 1 native-edges)))
3758 (if body
3759 (list (+ left-body left-off) (+ top-body top-off)
3760 (+ right-body left-off) (+ bottom-body top-off))
3761 (list (+ left left-off) (+ top top-off)
3762 (+ right left-off) (+ bottom top-off))))
3763 (if body
3764 (if pixelwise
3765 (list left-body top-body right-body bottom-body)
3766 (list (/ left-body char-width) (/ top-body char-height)
3767 ;; Round up.
3768 (/ (+ right-body char-width -1) char-width)
3769 (/ (+ bottom-body char-height -1) char-height)))
3770 (list left top right bottom)))))
3772 (defun window-body-edges (&optional window)
3773 "Return a list of the edge coordinates of WINDOW's body.
3774 The return value is that of `window-edges' called with argument
3775 BODY non-nil."
3776 (window-edges window t))
3777 (defalias 'window-inside-edges 'window-body-edges)
3779 (defun window-pixel-edges (&optional window)
3780 "Return a list of the edge pixel coordinates of WINDOW.
3781 The return value is that of `window-edges' called with argument
3782 PIXELWISE non-nil."
3783 (window-edges window nil nil t))
3785 (defun window-body-pixel-edges (&optional window)
3786 "Return a list of the edge pixel coordinates of WINDOW's body.
3787 The return value is that of `window-edges' called with arguments
3788 BODY and PIXELWISE non-nil."
3789 (window-edges window t nil t))
3790 (defalias 'window-inside-pixel-edges 'window-body-pixel-edges)
3792 (defun window-absolute-pixel-edges (&optional window)
3793 "Return a list of the edge pixel coordinates of WINDOW.
3794 The return value is that of `window-edges' called with argument
3795 ABSOLUTE non-nil."
3796 (window-edges window nil t t))
3798 (defun window-absolute-body-pixel-edges (&optional window)
3799 "Return a list of the edge pixel coordinates of WINDOW's text area.
3800 The return value is that of `window-edges' called with arguments
3801 BODY and ABSOLUTE non-nil."
3802 (window-edges window t t t))
3803 (defalias 'window-inside-absolute-pixel-edges 'window-absolute-body-pixel-edges)
3805 (defun window-absolute-pixel-position (&optional position window)
3806 "Return display coordinates of POSITION in WINDOW.
3807 If the buffer position POSITION is visible in window WINDOW,
3808 return the display coordinates of the upper/left corner of the
3809 glyph at POSITION. The return value is a cons of the X- and
3810 Y-coordinates of that corner, relative to an origin at (0, 0) of
3811 WINDOW's display. Return nil if POSITION is not visible in
3812 WINDOW.
3814 WINDOW must be a live window and defaults to the selected window.
3815 POSITION defaults to the value of `window-point' of WINDOW."
3816 (let* ((window (window-normalize-window window t))
3817 (pos-in-window
3818 (pos-visible-in-window-p
3819 (or position (window-point window)) window t)))
3820 (when pos-in-window
3821 (let ((edges (window-absolute-body-pixel-edges window)))
3822 (cons (+ (nth 0 edges) (nth 0 pos-in-window))
3823 (+ (nth 1 edges) (nth 1 pos-in-window)))))))
3825 (defun frame-root-window-p (window)
3826 "Return non-nil if WINDOW is the root window of its frame."
3827 (eq window (frame-root-window window)))
3829 (defun window--subtree (window &optional next)
3830 "Return window subtree rooted at WINDOW.
3831 Optional argument NEXT non-nil means include WINDOW's right
3832 siblings in the return value.
3834 See the documentation of `window-tree' for a description of the
3835 return value."
3836 (let (list)
3837 (while window
3838 (setq list
3839 (cons
3840 (cond
3841 ((window-top-child window)
3842 (cons t (cons (window-edges window)
3843 (window--subtree (window-top-child window) t))))
3844 ((window-left-child window)
3845 (cons nil (cons (window-edges window)
3846 (window--subtree (window-left-child window) t))))
3847 (t window))
3848 list))
3849 (setq window (when next (window-next-sibling window))))
3850 (nreverse list)))
3852 (defun window-tree (&optional frame)
3853 "Return the window tree of frame FRAME.
3854 FRAME must be a live frame and defaults to the selected frame.
3855 The return value is a list of the form (ROOT MINI), where ROOT
3856 represents the window tree of the frame's root window, and MINI
3857 is the frame's minibuffer window.
3859 If the root window is not split, ROOT is the root window itself.
3860 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3861 for a horizontal split, and t for a vertical split. EDGES gives
3862 the combined size and position of the child windows in the split,
3863 and the rest of the elements are the child windows in the split.
3864 Each of the child windows may again be a window or a list
3865 representing a window split, and so on. EDGES is a list (LEFT
3866 TOP RIGHT BOTTOM) as returned by `window-edges'."
3867 (setq frame (window-normalize-frame frame))
3868 (window--subtree (frame-root-window frame) t))
3870 (defun other-window (count &optional all-frames)
3871 "Select another window in cyclic ordering of windows.
3872 COUNT specifies the number of windows to skip, starting with the
3873 selected window, before making the selection. If COUNT is
3874 positive, skip COUNT windows forwards. If COUNT is negative,
3875 skip -COUNT windows backwards. COUNT zero means do not skip any
3876 window, so select the selected window. In an interactive call,
3877 COUNT is the numeric prefix argument. Return nil.
3879 If the `other-window' parameter of the selected window is a
3880 function and `ignore-window-parameters' is nil, call that
3881 function with the arguments COUNT and ALL-FRAMES.
3883 This function does not select a window whose `no-other-window'
3884 window parameter is non-nil.
3886 This function uses `next-window' for finding the window to
3887 select. The argument ALL-FRAMES has the same meaning as in
3888 `next-window', but the MINIBUF argument of `next-window' is
3889 always effectively nil."
3890 (interactive "p")
3891 (let* ((window (selected-window))
3892 (function (and (not ignore-window-parameters)
3893 (window-parameter window 'other-window)))
3894 old-window old-count)
3895 (if (functionp function)
3896 (funcall function count all-frames)
3897 ;; `next-window' and `previous-window' may return a window we are
3898 ;; not allowed to select. Hence we need an exit strategy in case
3899 ;; all windows are non-selectable.
3900 (catch 'exit
3901 (while (> count 0)
3902 (setq window (next-window window nil all-frames))
3903 (cond
3904 ((eq window old-window)
3905 (when (= count old-count)
3906 ;; Keep out of infinite loops. When COUNT has not changed
3907 ;; since we last looked at `window' we're probably in one.
3908 (throw 'exit nil)))
3909 ((window-parameter window 'no-other-window)
3910 (unless old-window
3911 ;; The first non-selectable window `next-window' got us:
3912 ;; Remember it and the current value of COUNT.
3913 (setq old-window window)
3914 (setq old-count count)))
3916 (setq count (1- count)))))
3917 (while (< count 0)
3918 (setq window (previous-window window nil all-frames))
3919 (cond
3920 ((eq window old-window)
3921 (when (= count old-count)
3922 ;; Keep out of infinite loops. When COUNT has not changed
3923 ;; since we last looked at `window' we're probably in one.
3924 (throw 'exit nil)))
3925 ((window-parameter window 'no-other-window)
3926 (unless old-window
3927 ;; The first non-selectable window `previous-window' got
3928 ;; us: Remember it and the current value of COUNT.
3929 (setq old-window window)
3930 (setq old-count count)))
3932 (setq count (1+ count)))))
3934 (select-window window)
3935 ;; Always return nil.
3936 nil))))
3938 ;; This should probably return non-nil when the selected window is part
3939 ;; of an atomic window whose root is the frame's root window.
3940 (defun one-window-p (&optional nomini all-frames)
3941 "Return non-nil if the selected window is the only window.
3942 Optional arg NOMINI non-nil means don't count the minibuffer
3943 even if it is active. Otherwise, the minibuffer is counted
3944 when it is active.
3946 Optional argument ALL-FRAMES specifies the set of frames to
3947 consider, see also `next-window'. ALL-FRAMES nil or omitted
3948 means consider windows on the selected frame only, plus the
3949 minibuffer window if specified by the NOMINI argument. If the
3950 minibuffer counts, consider all windows on all frames that share
3951 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3952 with a special meaning are:
3954 - t means consider all windows on all existing frames.
3956 - `visible' means consider all windows on all visible frames on
3957 the current terminal.
3959 - 0 (the number zero) means consider all windows on all visible
3960 and iconified frames on the current terminal.
3962 - A frame means consider all windows on that frame only.
3964 Anything else means consider all windows on the selected frame
3965 and no others."
3966 (let ((base-window (selected-window)))
3967 (if (and nomini (eq base-window (minibuffer-window)))
3968 (setq base-window (next-window base-window)))
3969 (eq base-window
3970 (next-window base-window (if nomini 'arg) all-frames))))
3972 ;;; Deleting windows.
3973 (defun window-deletable-p (&optional window)
3974 "Return t if WINDOW can be safely deleted from its frame.
3975 WINDOW must be a valid window and defaults to the selected one.
3977 Return `frame' if WINDOW is the root window of its frame and that
3978 frame can be safely deleted."
3979 (setq window (window-normalize-window window))
3981 (unless (or ignore-window-parameters
3982 (eq (window-parameter window 'delete-window) t))
3983 ;; Handle atomicity.
3984 (when (window-parameter window 'window-atom)
3985 (setq window (window-atom-root window))))
3987 (let ((frame (window-frame window)))
3988 (cond
3989 ((frame-root-window-p window)
3990 ;; WINDOW's frame can be deleted only if there are other frames
3991 ;; on the same terminal, and it does not contain the active
3992 ;; minibuffer.
3993 (unless (or (eq frame (next-frame frame 0))
3994 ;; We can delete our frame only if no other frame
3995 ;; currently uses our minibuffer window.
3996 (catch 'other
3997 (dolist (other (frame-list))
3998 (when (and (not (eq other frame))
3999 (eq (window-frame (minibuffer-window other))
4000 frame))
4001 (throw 'other t))))
4002 (let ((minibuf (active-minibuffer-window)))
4003 (and minibuf (eq frame (window-frame minibuf)))))
4004 'frame))
4005 ((window-minibuffer-p window)
4006 ;; If WINDOW is the minibuffer window of a non-minibuffer-only
4007 ;; frame, it cannot be deleted separately.
4008 nil)
4009 ((or ignore-window-parameters
4010 (not (eq window (window-main-window frame))))
4011 ;; Otherwise, WINDOW can be deleted unless it is the main window
4012 ;; of its frame.
4013 t))))
4015 (defun window--in-subtree-p (window root)
4016 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
4017 (or (eq window root)
4018 (let ((parent (window-parent window)))
4019 (catch 'done
4020 (while parent
4021 (if (eq parent root)
4022 (throw 'done t)
4023 (setq parent (window-parent parent))))))))
4025 (defun delete-window (&optional window)
4026 "Delete WINDOW.
4027 WINDOW must be a valid window and defaults to the selected one.
4028 Return nil.
4030 If the variable `ignore-window-parameters' is non-nil or the
4031 `delete-window' parameter of WINDOW equals t, do not process any
4032 parameters of WINDOW. Otherwise, if the `delete-window'
4033 parameter of WINDOW specifies a function, call that function with
4034 WINDOW as its sole argument and return the value returned by that
4035 function.
4037 Otherwise, if WINDOW is part of an atomic window, call
4038 `delete-window' with the root of the atomic window as its
4039 argument. Signal an error if WINDOW is either the only window on
4040 its frame, the last non-side window, or part of an atomic window
4041 that is its frame's root window."
4042 (interactive)
4043 (setq window (window-normalize-window window))
4044 (let* ((frame (window-frame window))
4045 (function (window-parameter window 'delete-window))
4046 (parent (window-parent window))
4047 atom-root)
4048 (window--check frame)
4049 (catch 'done
4050 ;; Handle window parameters.
4051 (cond
4052 ;; Ignore window parameters if `ignore-window-parameters' tells
4053 ;; us so or `delete-window' equals t.
4054 ((or ignore-window-parameters (eq function t)))
4055 ((functionp function)
4056 ;; The `delete-window' parameter specifies the function to call.
4057 ;; If that function is `ignore' nothing is done. It's up to the
4058 ;; function called here to avoid infinite recursion.
4059 (throw 'done (funcall function window)))
4060 ((and (window-parameter window 'window-atom)
4061 (setq atom-root (window-atom-root window))
4062 (not (eq atom-root window)))
4063 (if (eq atom-root (frame-root-window frame))
4064 (error "Root of atomic window is root window of its frame")
4065 (throw 'done (delete-window atom-root))))
4066 ((not parent)
4067 (error "Attempt to delete minibuffer or sole ordinary window"))
4068 ((eq window (window-main-window frame))
4069 (error "Attempt to delete main window of frame %s" frame)))
4071 (let* ((horizontal (window-left-child parent))
4072 (size (window-size window horizontal t))
4073 (window-combination-resize
4074 (or window-combination-resize
4075 (window-parameter parent 'window-side)))
4076 (frame-selected
4077 (window--in-subtree-p (frame-selected-window frame) window))
4078 ;; Emacs 23 preferably gives WINDOW's space to its left
4079 ;; sibling.
4080 (sibling (or (window-left window) (window-right window))))
4081 (window--resize-reset frame horizontal)
4082 (cond
4083 ((and (not (eq window-combination-resize t))
4084 sibling (window-sizable-p sibling size horizontal nil t))
4085 ;; Resize WINDOW's sibling.
4086 (window--resize-this-window sibling size horizontal nil t)
4087 (set-window-new-normal
4088 sibling (+ (window-normal-size sibling horizontal)
4089 (window-normal-size window horizontal))))
4090 ((window--resizable-p window (- size) horizontal nil nil nil t t)
4091 ;; Can do without resizing fixed-size windows.
4092 (window--resize-siblings window (- size) horizontal))
4094 ;; Can't do without resizing fixed-size windows.
4095 (window--resize-siblings window (- size) horizontal t)))
4096 ;; Actually delete WINDOW.
4097 (delete-window-internal window)
4098 (window--pixel-to-total frame horizontal)
4099 (when (and frame-selected
4100 (window-parameter
4101 (frame-selected-window frame) 'no-other-window))
4102 ;; `delete-window-internal' has selected a window that should
4103 ;; not be selected, fix this here.
4104 (other-window -1 frame))
4105 (run-window-configuration-change-hook frame)
4106 (window--check frame)
4107 ;; Always return nil.
4108 nil))))
4110 (defun delete-other-windows (&optional window)
4111 "Make WINDOW fill its frame.
4112 WINDOW must be a valid window and defaults to the selected one.
4113 Return nil.
4115 If the variable `ignore-window-parameters' is non-nil or the
4116 `delete-other-windows' parameter of WINDOW equals t, do not pay
4117 attention to any other parameters of WINDOW. Otherwise, if the
4118 `delete-other-windows' parameter of WINDOW specifies a function,
4119 call that function with WINDOW as its sole argument and return
4120 the value returned by that function.
4122 Else, if WINDOW is part of an atomic window, call this function
4123 with the root of the atomic window as its argument. Signal an
4124 error if that root window is the root window of WINDOW's frame.
4125 Also signal an error if WINDOW is a side window. Do not delete
4126 any window whose `no-delete-other-windows' parameter is non-nil."
4127 (interactive)
4128 (setq window (window-normalize-window window))
4129 (let* ((frame (window-frame window))
4130 (function (window-parameter window 'delete-other-windows))
4131 atom-root main)
4132 (window--check frame)
4133 (catch 'done
4134 (cond
4135 ;; Ignore window parameters if `ignore-window-parameters' is t or
4136 ;; `delete-other-windows' is t.
4137 ((or ignore-window-parameters (eq function t)))
4138 ((functionp function)
4139 ;; The `delete-other-windows' parameter specifies the function
4140 ;; to call. If the function is `ignore' no windows are deleted.
4141 ;; It's up to the function called to avoid infinite recursion.
4142 (throw 'done (funcall function window)))
4143 ((and (window-parameter window 'window-atom)
4144 (setq atom-root (window-atom-root window))
4145 (not (eq atom-root window)))
4146 (if (eq atom-root (frame-root-window frame))
4147 (error "Root of atomic window is root window of its frame")
4148 (throw 'done (delete-other-windows atom-root))))
4149 ((window-parameter window 'window-side)
4150 (error "Cannot make side window the only window"))
4151 ((and (window-minibuffer-p window)
4152 (not (eq window (frame-root-window window))))
4153 (error "Can't expand minibuffer to full frame")))
4155 (cond
4156 ((or ignore-window-parameters
4157 (not (window-with-parameter 'no-delete-other-windows nil frame)))
4158 (setq main (frame-root-window frame)))
4159 ((catch 'tag
4160 (walk-window-tree
4161 (lambda (other)
4162 (when (or (and (window-parameter other 'window-side)
4163 (not (window-parameter
4164 other 'no-delete-other-windows)))
4165 (and (not (window-parameter other 'window-side))
4166 (window-parameter
4167 other 'no-delete-other-windows)))
4168 (throw 'tag nil))))
4170 (setq main (window-main-window frame)))
4172 ;; Delete windows via `delete-window' because we found either a
4173 ;; deletable side window or a non-deletable non-side-window.
4174 (dolist (other (window-list frame))
4175 (when (and (window-live-p other)
4176 (not (eq other window))
4177 (not (window-parameter
4178 other 'no-delete-other-windows))
4179 ;; When WINDOW and the other window are part of the
4180 ;; same atomic window, don't delete the other.
4181 (or (not atom-root)
4182 (not (eq (window-atom-root other) atom-root))))
4183 (condition-case nil
4184 (delete-window other)
4185 (error nil))))
4186 (throw 'done nil)))
4188 ;; If WINDOW is the main window of its frame do nothing.
4189 (unless (eq window main)
4190 (delete-other-windows-internal window main)
4191 (run-window-configuration-change-hook frame)
4192 (window--check frame))
4193 ;; Always return nil.
4194 nil)))
4196 (defun delete-other-windows-vertically (&optional window)
4197 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
4198 This may be a useful alternative binding for \\[delete-other-windows]
4199 if you often split windows horizontally."
4200 (interactive)
4201 (let* ((window (or window (selected-window)))
4202 (edges (window-edges window))
4203 (w window) delenda)
4204 (while (not (eq (setq w (next-window w 1)) window))
4205 (let ((e (window-edges w)))
4206 (when (and (= (car e) (car edges))
4207 (= (nth 2 e) (nth 2 edges)))
4208 (push w delenda))))
4209 (mapc 'delete-window delenda)))
4211 ;;; Windows and buffers.
4213 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
4214 ;; for (1) determining which buffer to show in the window when its
4215 ;; buffer shall be buried or killed and (2) which buffer to show for
4216 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
4218 ;; `prev-buffers' consists of <buffer, window-start, window-point>
4219 ;; triples. The entries on this list are ordered by the time their
4220 ;; buffer has been removed from the window, the most recently removed
4221 ;; buffer's entry being first. The window-start and window-point
4222 ;; components are `window-start' and `window-point' at the time the
4223 ;; buffer was removed from the window which implies that the entry must
4224 ;; be added when `set-window-buffer' removes the buffer from the window.
4226 ;; `next-buffers' is the list of buffers that have been replaced
4227 ;; recently by `switch-to-prev-buffer'. These buffers are the least
4228 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
4229 ;; candidates of `switch-to-next-buffer' to switch to. This list is
4230 ;; reset to nil by any action changing the window's buffer with the
4231 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
4232 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
4233 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
4235 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
4236 ;; if such a buffer was killed while the window was hidden within a
4237 ;; window configuration. Such killed buffers get removed whenever
4238 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
4240 ;; The following function is called by `set-window-buffer' _before_ it
4241 ;; replaces the buffer of the argument window with the new buffer.
4242 (defun record-window-buffer (&optional window)
4243 "Record WINDOW's buffer.
4244 WINDOW must be a live window and defaults to the selected one."
4245 (let* ((window (window-normalize-window window t))
4246 (buffer (window-buffer window))
4247 (entry (assq buffer (window-prev-buffers window))))
4248 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
4249 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
4250 (set-window-next-buffers window nil)
4252 (when entry
4253 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
4254 (set-window-prev-buffers
4255 window (assq-delete-all buffer (window-prev-buffers window))))
4257 ;; Don't record insignificant buffers.
4258 (unless (eq (aref (buffer-name buffer) 0) ?\s)
4259 ;; Add an entry for buffer to WINDOW's previous buffers.
4260 (with-current-buffer buffer
4261 (let ((start (window-start window))
4262 (point (window-point window)))
4263 (setq entry
4264 (cons buffer
4265 (if entry
4266 ;; We have an entry, update marker positions.
4267 (list (set-marker (nth 1 entry) start)
4268 (set-marker (nth 2 entry) point))
4269 ;; Make new markers.
4270 (list (copy-marker start)
4271 (copy-marker
4272 ;; Preserve window-point-insertion-type
4273 ;; (Bug#12588).
4274 point window-point-insertion-type)))))
4275 (set-window-prev-buffers
4276 window (cons entry (window-prev-buffers window)))))
4278 (run-hooks 'buffer-list-update-hook))))
4280 (defun unrecord-window-buffer (&optional window buffer)
4281 "Unrecord BUFFER in WINDOW.
4282 WINDOW must be a live window and defaults to the selected one.
4283 BUFFER must be a live buffer and defaults to the buffer of
4284 WINDOW."
4285 (let* ((window (window-normalize-window window t))
4286 (buffer (or buffer (window-buffer window))))
4287 (set-window-prev-buffers
4288 window (assq-delete-all buffer (window-prev-buffers window)))
4289 (set-window-next-buffers
4290 window (delq buffer (window-next-buffers window)))))
4292 (defun set-window-buffer-start-and-point (window buffer &optional start point)
4293 "Set WINDOW's buffer to BUFFER.
4294 WINDOW must be a live window and defaults to the selected one.
4295 Optional argument START non-nil means set WINDOW's start position
4296 to START. Optional argument POINT non-nil means set WINDOW's
4297 point to POINT. If WINDOW is selected this also sets BUFFER's
4298 `point' to POINT. If WINDOW is selected and the buffer it showed
4299 before was current this also makes BUFFER the current buffer."
4300 (setq window (window-normalize-window window t))
4301 (let ((selected (eq window (selected-window)))
4302 (current (eq (window-buffer window) (current-buffer))))
4303 (set-window-buffer window buffer)
4304 (when (and selected current)
4305 (set-buffer buffer))
4306 (when start
4307 ;; Don't force window-start here (even if POINT is nil).
4308 (set-window-start window start t))
4309 (when point
4310 (set-window-point window point))))
4312 (defcustom switch-to-visible-buffer t
4313 "If non-nil, allow switching to an already visible buffer.
4314 If this variable is non-nil, `switch-to-prev-buffer' and
4315 `switch-to-next-buffer' may switch to an already visible buffer.
4316 If this variable is nil, `switch-to-prev-buffer' and
4317 `switch-to-next-buffer' always try to avoid switching to a buffer
4318 that is already visible in another window on the same frame."
4319 :type 'boolean
4320 :version "24.1"
4321 :group 'windows)
4323 (defun switch-to-prev-buffer (&optional window bury-or-kill)
4324 "In WINDOW switch to previous buffer.
4325 WINDOW must be a live window and defaults to the selected one.
4326 Return the buffer switched to, nil if no suitable buffer could be
4327 found.
4329 Optional argument BURY-OR-KILL non-nil means the buffer currently
4330 shown in WINDOW is about to be buried or killed and consequently
4331 shall not be switched to in future invocations of this command.
4333 As a special case, if BURY-OR-KILL equals `append', this means to
4334 move the buffer to the end of WINDOW's previous buffers list so a
4335 future invocation of `switch-to-prev-buffer' less likely switches
4336 to it."
4337 (interactive)
4338 (let* ((window (window-normalize-window window t))
4339 (frame (window-frame window))
4340 (window-side (window-parameter window 'window-side))
4341 (old-buffer (window-buffer window))
4342 ;; Save this since it's destroyed by `set-window-buffer'.
4343 (next-buffers (window-next-buffers window))
4344 (pred (frame-parameter frame 'buffer-predicate))
4345 entry new-buffer killed-buffers visible)
4346 (when (window-minibuffer-p window)
4347 ;; Don't switch in minibuffer window.
4348 (unless (setq window (minibuffer-selected-window))
4349 (error "Window %s is a minibuffer window" window)))
4351 (unless (memq (window-dedicated-p window) '(nil side))
4352 ;; Don't switch in dedicated window.
4353 (error "Window %s is dedicated to buffer %s" window old-buffer))
4355 (catch 'found
4356 ;; Scan WINDOW's previous buffers first, skipping entries of next
4357 ;; buffers.
4358 (dolist (entry (window-prev-buffers window))
4359 (when (and (setq new-buffer (car entry))
4360 (or (buffer-live-p new-buffer)
4361 (not (setq killed-buffers
4362 (cons new-buffer killed-buffers))))
4363 (not (eq new-buffer old-buffer))
4364 (or (null pred) (funcall pred new-buffer))
4365 ;; When BURY-OR-KILL is nil, avoid switching to a
4366 ;; buffer in WINDOW's next buffers list.
4367 (or bury-or-kill (not (memq new-buffer next-buffers))))
4368 (if (and (not switch-to-visible-buffer)
4369 (get-buffer-window new-buffer frame))
4370 ;; Try to avoid showing a buffer visible in some other
4371 ;; window.
4372 (setq visible new-buffer)
4373 (set-window-buffer-start-and-point
4374 window new-buffer (nth 1 entry) (nth 2 entry))
4375 (throw 'found t))))
4376 ;; Scan reverted buffer list of WINDOW's frame next, skipping
4377 ;; entries of next buffers. Note that when we bury or kill a
4378 ;; buffer we don't reverse the global buffer list to avoid showing
4379 ;; a buried buffer instead. Otherwise, we must reverse the global
4380 ;; buffer list in order to make sure that switching to the
4381 ;; previous/next buffer traverse it in opposite directions. Skip
4382 ;; this step for side windows.
4383 (unless window-side
4384 (dolist (buffer (if bury-or-kill
4385 (buffer-list frame)
4386 (nreverse (buffer-list frame))))
4387 (when (and (buffer-live-p buffer)
4388 (not (eq buffer old-buffer))
4389 (or (null pred) (funcall pred buffer))
4390 (not (eq (aref (buffer-name buffer) 0) ?\s))
4391 ;; Don't show a buffer shown in a side window before.
4392 (not (buffer-local-value 'window--sides-shown buffer))
4393 (or bury-or-kill (not (memq buffer next-buffers))))
4394 (if (and (not switch-to-visible-buffer)
4395 (get-buffer-window buffer frame))
4396 ;; Try to avoid showing a buffer visible in some other window.
4397 (unless visible
4398 (setq visible buffer))
4399 (setq new-buffer buffer)
4400 (set-window-buffer-start-and-point window new-buffer)
4401 (throw 'found t)))))
4402 (unless bury-or-kill
4403 ;; Scan reverted next buffers last (must not use nreverse
4404 ;; here!).
4405 (dolist (buffer (reverse next-buffers))
4406 ;; Actually, buffer _must_ be live here since otherwise it
4407 ;; would have been caught in the scan of previous buffers.
4408 (when (and (or (buffer-live-p buffer)
4409 (not (setq killed-buffers
4410 (cons buffer killed-buffers))))
4411 (not (eq buffer old-buffer))
4412 (or (null pred) (funcall pred buffer))
4413 (setq entry (assq buffer (window-prev-buffers window))))
4414 (setq new-buffer buffer)
4415 (set-window-buffer-start-and-point
4416 window new-buffer (nth 1 entry) (nth 2 entry))
4417 (throw 'found t))))
4419 ;; Show a buffer visible in another window.
4420 (when visible
4421 (setq new-buffer visible)
4422 (set-window-buffer-start-and-point window new-buffer)))
4424 (if bury-or-kill
4425 (let ((entry (and (eq bury-or-kill 'append)
4426 (assq old-buffer (window-prev-buffers window)))))
4427 ;; Remove `old-buffer' from WINDOW's previous and (restored list
4428 ;; of) next buffers.
4429 (set-window-prev-buffers
4430 window (assq-delete-all old-buffer (window-prev-buffers window)))
4431 (set-window-next-buffers window (delq old-buffer next-buffers))
4432 (when entry
4433 ;; Append old-buffer's entry to list of WINDOW's previous
4434 ;; buffers so it's less likely to get switched to soon but
4435 ;; `display-buffer-in-previous-window' can nevertheless find
4436 ;; it.
4437 (set-window-prev-buffers
4438 window (append (window-prev-buffers window) (list entry)))))
4439 ;; Move `old-buffer' to head of WINDOW's restored list of next
4440 ;; buffers.
4441 (set-window-next-buffers
4442 window (cons old-buffer (delq old-buffer next-buffers))))
4444 ;; Remove killed buffers from WINDOW's previous and next buffers.
4445 (when killed-buffers
4446 (dolist (buffer killed-buffers)
4447 (set-window-prev-buffers
4448 window (assq-delete-all buffer (window-prev-buffers window)))
4449 (set-window-next-buffers
4450 window (delq buffer (window-next-buffers window)))))
4452 ;; Return new-buffer.
4453 new-buffer))
4455 (defun switch-to-next-buffer (&optional window)
4456 "In WINDOW switch to next buffer.
4457 WINDOW must be a live window and defaults to the selected one.
4458 Return the buffer switched to, nil if no suitable buffer could be
4459 found."
4460 (interactive)
4461 (let* ((window (window-normalize-window window t))
4462 (frame (window-frame window))
4463 (window-side (window-parameter window 'window-side))
4464 (old-buffer (window-buffer window))
4465 (next-buffers (window-next-buffers window))
4466 (pred (frame-parameter frame 'buffer-predicate))
4467 new-buffer entry killed-buffers visible)
4468 (when (window-minibuffer-p window)
4469 ;; Don't switch in minibuffer window.
4470 (unless (setq window (minibuffer-selected-window))
4471 (error "Window %s is a minibuffer window" window)))
4473 (unless (memq (window-dedicated-p window) '(nil side))
4474 ;; Don't switch in dedicated window.
4475 (error "Window %s is dedicated to buffer %s" window old-buffer))
4477 (catch 'found
4478 ;; Scan WINDOW's next buffers first.
4479 (dolist (buffer next-buffers)
4480 (when (and (or (buffer-live-p buffer)
4481 (not (setq killed-buffers
4482 (cons buffer killed-buffers))))
4483 (not (eq buffer old-buffer))
4484 (or (null pred) (funcall pred buffer))
4485 (setq entry (assq buffer (window-prev-buffers window))))
4486 (setq new-buffer buffer)
4487 (set-window-buffer-start-and-point
4488 window new-buffer (nth 1 entry) (nth 2 entry))
4489 (throw 'found t)))
4490 ;; Scan the buffer list of WINDOW's frame next, skipping previous
4491 ;; buffers entries. Skip this step for side windows.
4492 (unless window-side
4493 (dolist (buffer (buffer-list frame))
4494 (when (and (buffer-live-p buffer)
4495 (not (eq buffer old-buffer))
4496 (or (null pred) (funcall pred buffer))
4497 (not (eq (aref (buffer-name buffer) 0) ?\s))
4498 ;; Don't show a buffer shown in a side window before.
4499 (not (buffer-local-value 'window--sides-shown buffer))
4500 (not (assq buffer (window-prev-buffers window))))
4501 (if (and (not switch-to-visible-buffer)
4502 (get-buffer-window buffer frame))
4503 ;; Try to avoid showing a buffer visible in some other window.
4504 (setq visible buffer)
4505 (setq new-buffer buffer)
4506 (set-window-buffer-start-and-point window new-buffer)
4507 (throw 'found t)))))
4508 ;; Scan WINDOW's reverted previous buffers last (must not use
4509 ;; nreverse here!)
4510 (dolist (entry (reverse (window-prev-buffers window)))
4511 (when (and (setq new-buffer (car entry))
4512 (or (buffer-live-p new-buffer)
4513 (not (setq killed-buffers
4514 (cons new-buffer killed-buffers))))