Fix lower case installer name
[emacs.git] / lisp / window.el
blob8939e7d589b0647cc7954299ce524ca2fe0a0a91
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, softly
1012 dedicate the side window used to BUFFER. Return the window used
1013 for displaying BUFFER, nil if no suitable window can be found.
1015 This function installs the `window-side' and `window-slot'
1016 parameters and makes them persistent. It neither modifies ALIST
1017 nor installs any other window parameters unless they have been
1018 explicitly provided via a `window-parameters' entry in ALIST."
1019 (let* ((side (or (cdr (assq 'side alist)) 'bottom))
1020 (slot (or (cdr (assq 'slot alist)) 0))
1021 (left-or-right (memq side '(left right)))
1022 ;; Softly dedicate window to BUFFER unless
1023 ;; `display-buffer-mark-dedicated' already asks for it.
1024 (dedicated (or display-buffer-mark-dedicated 'side)))
1025 (cond
1026 ((not (memq side '(top bottom left right)))
1027 (error "Invalid side %s specified" side))
1028 ((not (numberp slot))
1029 (error "Invalid slot %s specified" slot)))
1031 (let* ((major (window-with-parameter 'window-side side nil t))
1032 ;; `major' is the major window on SIDE, `windows' the list of
1033 ;; life windows on SIDE.
1034 (reversed (window--sides-reverse-on-frame-p (selected-frame)))
1035 (windows
1036 (cond
1037 ((window-live-p major)
1038 (list major))
1039 ((window-valid-p major)
1040 (let* ((first (window-child major))
1041 (next (window-next-sibling first))
1042 (windows (list next first)))
1043 (setq reversed (> (window-parameter first 'window-slot)
1044 (window-parameter next 'window-slot)))
1045 (while (setq next (window-next-sibling next))
1046 (setq windows (cons next windows)))
1047 (if reversed windows (nreverse windows))))))
1048 (slots (when major (max 1 (window-child-count major))))
1049 (max-slots
1050 (nth (cond
1051 ((eq side 'left) 0)
1052 ((eq side 'top) 1)
1053 ((eq side 'right) 2)
1054 ((eq side 'bottom) 3))
1055 window-sides-slots))
1056 (window--sides-inhibit-check t)
1057 window this-window this-slot prev-window next-window
1058 best-window best-slot abs-slot)
1060 (cond
1061 ((and (numberp max-slots) (<= max-slots 0))
1062 ;; No side-slots available on this side. Don't raise an error,
1063 ;; just return nil.
1064 nil)
1065 ((not windows)
1066 ;; No major side window exists on this side, make one.
1067 (window--make-major-side-window buffer side slot alist))
1069 ;; Scan windows on SIDE.
1070 (catch 'found
1071 (dolist (window windows)
1072 (setq this-slot (window-parameter window 'window-slot))
1073 (cond
1074 ;; The following should not happen and probably be checked
1075 ;; by window--sides-check.
1076 ((not (numberp this-slot)))
1077 ((= this-slot slot)
1078 ;; A window with a matching slot has been found.
1079 (setq this-window window)
1080 (throw 'found t))
1082 ;; Check if this window has a better slot value wrt the
1083 ;; slot of the window we want.
1084 (setq abs-slot
1085 (if (or (and (> this-slot 0) (> slot 0))
1086 (and (< this-slot 0) (< slot 0)))
1087 (abs (- slot this-slot))
1088 (+ (abs slot) (abs this-slot))))
1089 (unless (and best-slot (<= best-slot abs-slot))
1090 (setq best-window window)
1091 (setq best-slot abs-slot))
1092 (if reversed
1093 (cond
1094 ((<= this-slot slot)
1095 (setq next-window window))
1096 ((not prev-window)
1097 (setq prev-window window)))
1098 (cond
1099 ((<= this-slot slot)
1100 (setq prev-window window))
1101 ((not next-window)
1102 (setq next-window window))))))))
1104 ;; `this-window' is the first window with the same SLOT.
1105 ;; `prev-window' is the window with the largest slot < SLOT. A new
1106 ;; window will be created after it.
1107 ;; `next-window' is the window with the smallest slot > SLOT. A new
1108 ;; window will be created before it.
1109 ;; `best-window' is the window with the smallest absolute difference
1110 ;; of its slot and SLOT.
1111 (or (and this-window
1112 ;; Reuse `this-window'.
1113 (with-current-buffer buffer
1114 (setq window--sides-shown t))
1115 (window--display-buffer
1116 buffer this-window 'reuse alist dedicated))
1117 (and (or (not max-slots) (< slots max-slots))
1118 (or (and next-window
1119 ;; Make new window before `next-window'.
1120 (let ((next-side (if left-or-right 'above 'left))
1121 (window-combination-resize 'side))
1122 (setq window (split-window-no-error
1123 next-window nil next-side))))
1124 (and prev-window
1125 ;; Make new window after `prev-window'.
1126 (let ((prev-side (if left-or-right 'below 'right))
1127 (window-combination-resize 'side))
1128 (setq window (split-window-no-error
1129 prev-window nil prev-side)))))
1130 (set-window-parameter window 'window-slot slot)
1131 (with-current-buffer buffer
1132 (setq window--sides-shown t))
1133 (window--display-buffer
1134 buffer window 'window alist dedicated))
1135 (and best-window
1136 ;; Reuse `best-window'.
1137 (progn
1138 ;; Give best-window the new slot value.
1139 (set-window-parameter best-window 'window-slot slot)
1140 (with-current-buffer buffer
1141 (setq window--sides-shown t))
1142 (window--display-buffer
1143 buffer best-window 'reuse alist dedicated)))))))))
1145 (defun window-toggle-side-windows (&optional frame)
1146 "Toggle side windows on specified FRAME.
1147 FRAME must be a live frame and defaults to the selected one.
1149 If FRAME has at least one side window, save FRAME's state in the
1150 FRAME's `window-state' frame parameter and delete all side
1151 windows on FRAME afterwards. Otherwise, if FRAME has a
1152 `window-state' parameter, use that to restore any side windows on
1153 FRAME leaving FRAME's main window alone. Signal an error if
1154 FRAME has no side window and no saved state is found."
1155 (interactive)
1156 (let* ((frame (window-normalize-frame frame))
1157 (window--sides-inhibit-check t)
1158 state)
1159 (cond
1160 ((window-with-parameter 'window-side nil frame)
1161 ;; At least one side window exists. Remove all side windows after
1162 ;; saving FRAME's state in its `window-state' parameter.
1163 (set-frame-parameter
1164 frame 'window-state (window-state-get (frame-root-window frame)))
1165 (let ((ignore-window-parameters t))
1166 (delete-other-windows (window-main-window frame))))
1167 ((setq state (frame-parameter frame 'window-state))
1168 ;; A window state was saved for FRAME. Restore it and put the
1169 ;; current root window into its main window.
1170 (let ((main-state (window-state-get (frame-root-window frame))))
1171 (window-state-put state (frame-root-window frame) t)
1172 (window-state-put main-state (window-main-window frame)))
1173 (window--sides-reverse-frame frame))
1175 (error "No side windows state found")))))
1177 (defun window--sides-reverse-all ()
1178 "Maybe reverse side windows on all frames."
1179 (unless window--sides-inhibit-check
1180 (dolist (frame (frame-list))
1181 (window--sides-reverse-frame frame))))
1183 (defun window--sides-reverse-frame (frame)
1184 "Maybe reverse side windows on FRAME."
1185 (when (eq window-sides-reversed 'bidi)
1186 (let ((window (frame-selected-window frame)))
1187 (unless (or (window-parameter window 'window-side)
1188 (window-minibuffer-p window))
1189 (set-frame-parameter
1190 frame 'window-sides-main-selected-window window))))
1191 (window--sides-reverse-side frame 'top)
1192 (window--sides-reverse-side frame 'bottom))
1194 (defun window--sides-reverse-side (frame side)
1195 "Maybe reverse windows on SIDE of FRAME."
1196 (let ((major (window-with-parameter 'window-side side frame t))
1197 (window--sides-inhibit-check t))
1198 (when (and major (not (window-live-p major)))
1199 (let* ((first (window-child major))
1200 (reversed (> (window-parameter first 'window-slot)
1201 (window-parameter
1202 (window-next-sibling first) 'window-slot)))
1203 (reverse (window--sides-reverse-on-frame-p frame)))
1204 (unless (eq reversed reverse)
1205 ;; We have to reverse.
1206 (let ((last (window-last-child major)))
1207 (while (and (not (eq first last))
1208 (not (eq first (window-next-sibling last))))
1209 (window-swap-states first last t)
1210 (setq first (window-next-sibling first))
1211 (setq last (window-prev-sibling last)))))))))
1213 (defun window--sides-reverse (symbol value)
1214 "Helper function for customizing `window-sides-reversed'."
1215 (set-default symbol value)
1216 (remove-hook 'buffer-list-update-hook 'window--sides-reverse-all)
1217 (remove-hook 'window-configuration-change-hook 'window--sides-reverse-all)
1218 (dolist (frame (frame-list))
1219 (set-frame-parameter frame 'window-sides-main-selected-window nil))
1220 (when (eq value 'bidi)
1221 (add-hook 'buffer-list-update-hook 'window--sides-reverse-all)
1222 (add-hook 'window-configuration-change-hook 'window--sides-reverse-all))
1223 (window--sides-reverse-all))
1225 (defun window--sides-verticalize-frame (&optional frame)
1226 "Maybe change side windows layout on specified FRAME."
1227 (setq frame (window-normalize-frame frame))
1228 (let ((window--sides-inhibit-check t)
1229 (root (frame-root-window frame))
1230 (main (window-main-window frame)))
1231 (when (and (not (eq main root))
1232 (not (eq (window-parent main) root))
1233 (window-combined-p main window-sides-vertical))
1234 (let* ((window--sides-inhibit-check t)
1235 (ignore-window-parameters t)
1236 (first (window-child root))
1237 (first-state
1238 (and first (window-parameter first 'window-side)
1239 (window-state-get first)))
1240 (last (window-last-child root))
1241 (last-state
1242 (and last (window-parameter last 'window-side)
1243 (window-state-get last)))
1244 (dummy (get-buffer-create " *dummy*"))
1245 major)
1246 (unwind-protect
1247 (progn
1248 (when first-state (delete-window first))
1249 (when last-state (delete-window last))
1250 (when first-state
1251 (setq major (window--make-major-side-window
1252 dummy (if window-sides-vertical 'top 'left) 0))
1253 (window-state-put first-state major t))
1254 (when last-state
1255 (setq major (window--make-major-side-window
1256 dummy (if window-sides-vertical 'bottom 'right) 0))
1257 (window-state-put last-state major t)))
1258 (kill-buffer " *dummy*"))))))
1260 (defun window--sides-verticalize (symbol value)
1261 "Helper function for customizing `window-sides-vertical'."
1262 (set-default symbol value)
1263 (dolist (frame (frame-list))
1264 (window--sides-verticalize-frame frame)))
1266 (defun window--sides-check-failed (frame)
1267 "Helper function for `window--sides-check'."
1268 (catch 'failed
1269 ;; FRAME must have a main window.
1270 (unless (window-main-window frame)
1271 (error "Frame %s has no main window" frame)
1272 (throw 'failed t))
1273 ;; Now check the side windows.
1274 (dolist (side '(left top right bottom))
1275 (let ((window (window-with-parameter 'window-side side frame t)))
1276 (when window
1277 ;; If WINDOW is live there must be no other window on this frame
1278 ;; with the same `window-side' parameter.
1279 (if (window-live-p window)
1280 (walk-window-tree
1281 (lambda (this)
1282 (when (and (eq (window-parameter this 'window-side) side)
1283 (not (eq this window)))
1284 (error "Window %s has same side %s as window %s but no common parent"
1285 this side window)
1286 (throw 'failed t)))
1287 frame t 'nomini)
1288 (walk-window-tree
1289 (lambda (this)
1290 (if (eq (window-parent this) window)
1291 (unless (eq (window-parameter this 'window-side) side)
1292 (error "Window %s has not same side %s as its parent %s"
1293 this side window)
1294 (throw 'failed t))
1295 (when (and (eq (window-parameter this 'window-side) side)
1296 (not (eq this window)))
1297 (error "Window %s has same side %s as major side window %s but its parent is %s"
1298 this side window (window-parent this))
1299 (throw 'failed t))))
1300 frame t 'nomini)))))))
1302 (defun window--sides-check (frame)
1303 "Check side windows configuration of FRAME.
1304 In a valid side windows configuration there can be at most one
1305 internal side window on each side and all its children must be
1306 live and have the same `window-side' parameter and no other
1307 window with the same `window-side' parameter exists on FRAME. If
1308 there is no such internal window, there may be at most one window
1309 with this side's `window-side' parameter on FRAME.
1311 If the configuration is invalid, reset the `window-side'
1312 parameters of all windows on FRAME."
1313 (when (and (not window--sides-inhibit-check)
1314 (window-with-parameter 'window-side nil frame t)
1315 (window--sides-check-failed frame))
1316 ;; Reset all `window-side' parameters.
1317 (walk-window-tree
1318 (lambda (window)
1319 (set-window-parameter window 'window-side nil))
1320 frame t 'nomini)
1321 (message "Side windows configuration reset for frame %s" frame)))
1323 (defun window--check (&optional frame)
1324 "Check atomic and side windows on FRAME.
1325 FRAME defaults to the selected frame."
1326 (window--sides-check frame)
1327 (window--atom-check frame))
1329 ;; Dumping frame/window contents.
1330 (defun window--dump-window (&optional window erase)
1331 "Dump WINDOW to buffer *window-frame-dump*.
1332 WINDOW must be a valid window and defaults to the selected one.
1333 Optional argument ERASE non-nil means erase *window-frame-dump*
1334 before writing to it."
1335 (setq window (window-normalize-window window))
1336 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1337 (when erase (erase-buffer))
1338 (insert
1339 (format "%s parent: %s\n" window (window-parent window))
1340 (format "pixel left: %s top: %s size: %s x %s new: %s\n"
1341 (window-pixel-left window) (window-pixel-top window)
1342 (window-size window t t) (window-size window nil t)
1343 (window-new-pixel window))
1344 (format "char left: %s top: %s size: %s x %s new: %s\n"
1345 (window-left-column window) (window-top-line window)
1346 (window-total-size window t) (window-total-size window)
1347 (window-new-total window))
1348 (format "normal: %s x %s new: %s\n"
1349 (window-normal-size window t) (window-normal-size window)
1350 (window-new-normal window)))
1351 (when (window-live-p window)
1352 (let ((fringes (window-fringes window))
1353 (margins (window-margins window)))
1354 (insert
1355 (format "body pixel: %s x %s char: %s x %s\n"
1356 (window-body-width window t) (window-body-height window t)
1357 (window-body-width window) (window-body-height window))
1358 (format "width left fringe: %s left margin: %s right margin: %s\n"
1359 (car fringes) (or (car margins) 0) (or (cdr margins) 0))
1360 (format "width right fringe: %s scroll-bar: %s divider: %s\n"
1361 (cadr fringes)
1362 (window-scroll-bar-width window)
1363 (window-right-divider-width window))
1364 (format "height header-line: %s mode-line: %s divider: %s\n"
1365 (window-header-line-height window)
1366 (window-mode-line-height window)
1367 (window-bottom-divider-width window)))))
1368 (insert "\n")))
1370 (defun window--dump-frame (&optional window-or-frame)
1371 "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
1372 WINDOW-OR-FRAME can be a frame or a window and defaults to the
1373 selected frame. When WINDOW-OR-FRAME is a window, dump that
1374 window's frame. The buffer *window-frame-dump* is erased before
1375 dumping to it."
1376 (let* ((window
1377 (cond
1378 ((or (not window-or-frame)
1379 (frame-live-p window-or-frame))
1380 (frame-root-window window-or-frame))
1381 ((or (window-live-p window-or-frame)
1382 (window-child window-or-frame))
1383 window-or-frame)
1385 (frame-root-window))))
1386 (frame (window-frame window)))
1387 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1388 (erase-buffer)
1389 (insert
1390 (format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
1391 (frame-pixel-width frame) (frame-pixel-height frame)
1392 (frame-total-cols frame) (frame-total-lines frame)
1393 (frame-char-width frame) (frame-char-height frame))
1394 (format "frame text pixel: %s x %s cols/lines: %s x %s\n"
1395 (frame-text-width frame) (frame-text-height frame)
1396 (frame-text-cols frame) (frame-text-lines frame))
1397 (format "tool: %s scroll: %s/%s fringe: %s border: %s right: %s bottom: %s\n\n"
1398 (if (fboundp 'tool-bar-height)
1399 (tool-bar-height frame t)
1400 "0")
1401 (frame-scroll-bar-width frame)
1402 (frame-scroll-bar-height frame)
1403 (frame-fringe-width frame)
1404 (frame-border-width frame)
1405 (frame-right-divider-width frame)
1406 (frame-bottom-divider-width frame)))
1407 (walk-window-tree 'window--dump-window frame t t))))
1409 ;;; Window sizes.
1410 (defun window-total-size (&optional window horizontal round)
1411 "Return the total height or width of WINDOW.
1412 WINDOW must be a valid window and defaults to the selected one.
1414 If HORIZONTAL is omitted or nil, return the total height of
1415 WINDOW, in lines. If WINDOW is live, its total height includes,
1416 in addition to the height of WINDOW's text, the heights of
1417 WINDOW's mode and header line and a bottom divider, if any.
1419 If HORIZONTAL is non-nil, return the total width of WINDOW, in
1420 columns. If WINDOW is live, its total width includes, in
1421 addition to the width of WINDOW's text, the widths of WINDOW's
1422 fringes, margins, scroll bars and its right divider, if any.
1424 If WINDOW is internal, return the respective size of the screen
1425 areas spanned by its children.
1427 Optional argument ROUND is handled as for `window-total-height'
1428 and `window-total-width'."
1429 (if horizontal
1430 (window-total-width window round)
1431 (window-total-height window round)))
1433 (defun window-size (&optional window horizontal pixelwise round)
1434 "Return the height or width of WINDOW.
1435 WINDOW must be a valid window and defaults to the selected one.
1437 If HORIZONTAL is omitted or nil, return the total height of
1438 WINDOW, in lines, like `window-total-height'. Otherwise return
1439 the total width, in columns, like `window-total-width'.
1441 Optional argument PIXELWISE means return the pixel size of WINDOW
1442 like `window-pixel-height' and `window-pixel-width'.
1444 Optional argument ROUND is ignored if PIXELWISE is non-nil and
1445 handled as for `window-total-height' and `window-total-width'
1446 otherwise."
1447 (if horizontal
1448 (if pixelwise
1449 (window-pixel-width window)
1450 (window-total-width window round))
1451 (if pixelwise
1452 (window-pixel-height window)
1453 (window-total-height window round))))
1455 (defvar window-size-fixed nil
1456 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
1457 If the value is `height', then only the window's height is fixed.
1458 If the value is `width', then only the window's width is fixed.
1459 Any other non-nil value fixes both the width and the height.
1461 Emacs won't change the size of any window displaying that buffer,
1462 unless it has no other choice (like when deleting a neighboring
1463 window).")
1464 (make-variable-buffer-local 'window-size-fixed)
1466 (defun window--preservable-size (window &optional horizontal)
1467 "Return height of WINDOW as `window-preserve-size' would preserve it.
1468 Optional argument HORIZONTAL non-nil means to return the width of
1469 WINDOW as `window-preserve-size' would preserve it."
1470 (if horizontal
1471 (window-body-width window t)
1472 (+ (window-body-height window t)
1473 (window-header-line-height window)
1474 (window-mode-line-height window))))
1476 (defun window-preserve-size (&optional window horizontal preserve)
1477 "Preserve height of window WINDOW.
1478 WINDOW must be a live window and defaults to the selected one.
1479 Optional argument HORIZONTAL non-nil means preserve the width of
1480 WINDOW.
1482 PRESERVE t means to preserve the current height/width of WINDOW's
1483 body in frame and window resizing operations whenever possible.
1484 The height/width of WINDOW will change only if Emacs has no other
1485 choice. Resizing a window whose height/width is preserved never
1486 throws an error.
1488 PRESERVE nil means to stop preserving the height/width of WINDOW,
1489 lifting the respective restraint induced by a previous call of
1490 `window-preserve-size' for WINDOW. Calling `enlarge-window',
1491 `shrink-window', `split-window' or `fit-window-to-buffer' with
1492 WINDOW as argument also removes the respective restraint.
1494 Other values of PRESERVE are reserved for future use."
1495 (setq window (window-normalize-window window t))
1496 (let* ((parameter (window-parameter window 'window-preserved-size))
1497 (width (nth 1 parameter))
1498 (height (nth 2 parameter)))
1499 (if horizontal
1500 (set-window-parameter
1501 window 'window-preserved-size
1502 (list
1503 (window-buffer window)
1504 (and preserve (window--preservable-size window t))
1505 height))
1506 (set-window-parameter
1507 window 'window-preserved-size
1508 (list
1509 (window-buffer window)
1510 width
1511 (and preserve (window--preservable-size window)))))))
1513 (defun window-preserved-size (&optional window horizontal)
1514 "Return preserved height of window WINDOW.
1515 WINDOW must be a live window and defaults to the selected one.
1516 Optional argument HORIZONTAL non-nil means to return preserved
1517 width of WINDOW."
1518 (setq window (window-normalize-window window t))
1519 (let* ((parameter (window-parameter window 'window-preserved-size))
1520 (buffer (nth 0 parameter))
1521 (width (nth 1 parameter))
1522 (height (nth 2 parameter)))
1523 (when (eq buffer (window-buffer window))
1524 (if horizontal width height))))
1526 (defun window--preserve-size (window horizontal)
1527 "Return non-nil when the height of WINDOW shall be preserved.
1528 Optional argument HORIZONTAL non-nil means to return non-nil when
1529 the width of WINDOW shall be preserved."
1530 (let ((size (window-preserved-size window horizontal)))
1531 (and (numberp size)
1532 (= size (window--preservable-size window horizontal)))))
1534 (defun window-safe-min-size (&optional window horizontal pixelwise)
1535 "Return safe minimum size of WINDOW.
1536 WINDOW must be a valid window and defaults to the selected one.
1537 Optional argument HORIZONTAL non-nil means return the minimum
1538 number of columns of WINDOW; otherwise return the minimum number
1539 of WINDOW's lines.
1541 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1542 of WINDOW."
1543 (setq window (window-normalize-window window))
1544 (if pixelwise
1545 (if horizontal
1546 (* window-safe-min-width
1547 (frame-char-width (window-frame window)))
1548 (* window-safe-min-height
1549 (frame-char-height (window-frame window))))
1550 (if horizontal window-safe-min-width window-safe-min-height)))
1552 (defun window-min-size (&optional window horizontal ignore pixelwise)
1553 "Return the minimum size of WINDOW.
1554 WINDOW must be a valid window and defaults to the selected one.
1555 Optional argument HORIZONTAL non-nil means return the minimum
1556 number of columns of WINDOW; otherwise return the minimum number
1557 of WINDOW's lines.
1559 The optional argument IGNORE has the same meaning as for
1560 `window-resizable'. Optional argument PIXELWISE non-nil means
1561 return the minimum pixel-size of WINDOW."
1562 (window--min-size-1
1563 (window-normalize-window window) horizontal ignore pixelwise))
1565 (defun window--min-size-ignore-p (window ignore)
1566 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
1567 (if (window-valid-p ignore)
1568 (eq window ignore)
1569 (not (memq ignore '(nil preserved)))))
1571 (defun window--min-size-1 (window horizontal ignore pixelwise)
1572 "Internal function of `window-min-size'."
1573 (let ((sub (window-child window)))
1574 (if sub
1575 (let ((value 0))
1576 ;; WINDOW is an internal window.
1577 (if (window-combined-p sub horizontal)
1578 ;; The minimum size of an iso-combination is the sum of
1579 ;; the minimum sizes of its child windows.
1580 (while sub
1581 (setq value (+ value
1582 (window--min-size-1
1583 sub horizontal ignore pixelwise)))
1584 (setq sub (window-right sub)))
1585 ;; The minimum size of an ortho-combination is the maximum
1586 ;; of the minimum sizes of its child windows.
1587 (while sub
1588 (setq value (max value
1589 (window--min-size-1
1590 sub horizontal ignore pixelwise)))
1591 (setq sub (window-right sub))))
1592 value)
1593 (with-current-buffer (window-buffer window)
1594 (cond
1595 ((window-minibuffer-p window)
1596 (if pixelwise (frame-char-height (window-frame window)) 1))
1597 ((window-size-fixed-p window horizontal ignore)
1598 ;; The minimum size of a fixed size window is its size.
1599 (window-size window horizontal pixelwise))
1600 ((eq ignore 'safe)
1601 ;; If IGNORE equals `safe' return the safe value.
1602 (window-safe-min-size window horizontal pixelwise))
1603 (horizontal
1604 ;; For the minimum width of a window take fringes and
1605 ;; scroll-bars into account. This is questionable and should
1606 ;; be removed as soon as we are able to split (and resize)
1607 ;; windows such that the new (or resized) windows can get a
1608 ;; size less than the user-specified `window-min-height' and
1609 ;; `window-min-width'.
1610 (let* ((char-size (frame-char-size window t))
1611 (fringes (window-fringes window))
1612 (margins (window-margins window))
1613 ;; Let the 'min-margins' parameter override the actual
1614 ;; widths of the margins. We allow any number to
1615 ;; replace the values specified by `window-margins'.
1616 ;; See bug#24193 for the rationale of this parameter.
1617 (min-margins (window-parameter window 'min-margins))
1618 (left-min-margin (and min-margins
1619 (numberp (car min-margins))
1620 (car min-margins)))
1621 (right-min-margin (and min-margins
1622 (numberp (cdr min-margins))
1623 (cdr min-margins)))
1624 (pixel-width
1625 (+ (window-safe-min-size window t t)
1626 (* (or left-min-margin (car margins) 0) char-size)
1627 (* (or right-min-margin(cdr margins) 0) char-size)
1628 (car fringes) (cadr fringes)
1629 (window-scroll-bar-width window)
1630 (window-right-divider-width window))))
1631 (if pixelwise
1632 (max
1633 (if window-resize-pixelwise
1634 pixel-width
1635 ;; Round up to next integral of columns.
1636 (* (ceiling pixel-width char-size) char-size))
1637 (if (window--min-size-ignore-p window ignore)
1639 (window-min-pixel-width window)))
1640 (max
1641 (ceiling pixel-width char-size)
1642 (if (window--min-size-ignore-p window ignore)
1644 window-min-width)))))
1645 ((let ((char-size (frame-char-size window))
1646 (pixel-height
1647 (+ (window-safe-min-size window nil t)
1648 (window-header-line-height window)
1649 (window-scroll-bar-height window)
1650 (window-mode-line-height window)
1651 (window-bottom-divider-width window))))
1652 (if pixelwise
1653 (max
1654 (if window-resize-pixelwise
1655 pixel-height
1656 ;; Round up to next integral of lines.
1657 (* (ceiling pixel-height char-size) char-size))
1658 (if (window--min-size-ignore-p window ignore)
1660 (window-min-pixel-height window)))
1661 (max (ceiling pixel-height char-size)
1662 (if (window--min-size-ignore-p window ignore)
1664 window-min-height))))))))))
1666 (defun window-sizable (window delta &optional horizontal ignore pixelwise)
1667 "Return DELTA if DELTA lines can be added to WINDOW.
1668 WINDOW must be a valid window and defaults to the selected one.
1669 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1670 columns can be added to WINDOW. A return value of zero means
1671 that no lines (or columns) can be added to WINDOW.
1673 This function looks only at WINDOW and, recursively, its child
1674 windows. The function `window-resizable' looks at other windows
1675 as well.
1677 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1678 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1679 return the maximum value in the range 0..DELTA by which WINDOW
1680 can be enlarged.
1682 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1683 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1684 return the minimum value in the range DELTA..0 by which WINDOW
1685 can be shrunk.
1687 The optional argument IGNORE has the same meaning as for
1688 `window-resizable'. Optional argument PIXELWISE non-nil means
1689 interpret DELTA as pixels."
1690 (setq window (window-normalize-window window))
1691 (cond
1692 ((< delta 0)
1693 (max (- (window-min-size window horizontal ignore pixelwise)
1694 (window-size window horizontal pixelwise))
1695 delta))
1696 ((> delta 0)
1697 (if (window-size-fixed-p window horizontal ignore)
1699 delta))
1700 (t 0)))
1702 (defun window-sizable-p (window delta &optional horizontal ignore pixelwise)
1703 "Return t if WINDOW can be resized by DELTA lines.
1704 WINDOW must be a valid window and defaults to the selected one.
1705 For the meaning of the arguments of this function see the
1706 doc-string of `window-sizable'."
1707 (setq window (window-normalize-window window))
1708 (if (> delta 0)
1709 (>= (window-sizable window delta horizontal ignore pixelwise)
1710 delta)
1711 (<= (window-sizable window delta horizontal ignore pixelwise)
1712 delta)))
1714 (defun window--size-fixed-1 (window horizontal ignore)
1715 "Internal function for `window-size-fixed-p'."
1716 (let ((sub (window-child window)))
1717 (catch 'fixed
1718 (if sub
1719 ;; WINDOW is an internal window.
1720 (if (window-combined-p sub horizontal)
1721 ;; An iso-combination is fixed size if all its child
1722 ;; windows are fixed-size.
1723 (progn
1724 (while sub
1725 (unless (window--size-fixed-1 sub horizontal ignore)
1726 ;; We found a non-fixed-size child window, so
1727 ;; WINDOW's size is not fixed.
1728 (throw 'fixed nil))
1729 (setq sub (window-right sub)))
1730 ;; All child windows are fixed-size, so WINDOW's size is
1731 ;; fixed.
1732 (throw 'fixed t))
1733 ;; An ortho-combination is fixed-size if at least one of its
1734 ;; child windows is fixed-size.
1735 (while sub
1736 (when (window--size-fixed-1 sub horizontal ignore)
1737 ;; We found a fixed-size child window, so WINDOW's size
1738 ;; is fixed.
1739 (throw 'fixed t))
1740 (setq sub (window-right sub))))
1741 ;; WINDOW is a live window.
1742 (and (or (not (windowp ignore)) (not (eq window ignore)))
1743 (or (and (not (eq ignore 'preserved))
1744 (window--preserve-size window horizontal))
1745 (with-current-buffer (window-buffer window)
1746 (if horizontal
1747 (memq window-size-fixed '(width t))
1748 (memq window-size-fixed '(height t))))))))))
1750 (defun window-size-fixed-p (&optional window horizontal ignore)
1751 "Return non-nil if WINDOW's height is fixed.
1752 WINDOW must be a valid window and defaults to the selected one.
1753 Optional argument HORIZONTAL non-nil means return non-nil if
1754 WINDOW's width is fixed. The optional argument IGNORE has the
1755 same meaning as for `window-resizable'.
1757 If this function returns nil, this does not necessarily mean that
1758 WINDOW can be resized in the desired direction. The function
1759 `window-resizable' can tell that."
1760 (when (or (windowp ignore) (memq ignore '(nil preserved)))
1761 (window--size-fixed-1
1762 (window-normalize-window window) horizontal ignore)))
1764 (defun window--min-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
1765 "Internal function for `window-min-delta'."
1766 (if (not (window-parent window))
1767 ;; If we can't go up, return zero.
1769 ;; Else try to find a non-fixed-size sibling of WINDOW.
1770 (let* ((parent (window-parent window))
1771 (sub (window-child parent)))
1772 (catch 'done
1773 (if (window-combined-p sub horizontal)
1774 ;; In an iso-combination throw DELTA if we find at least one
1775 ;; child window and that window is either not fixed-size or
1776 ;; we can ignore fixed-sizeness.
1777 (let ((skip (eq trail 'after)))
1778 (while sub
1779 (cond
1780 ((eq sub window)
1781 (setq skip (eq trail 'before)))
1782 (skip)
1783 ((window-size-fixed-p sub horizontal ignore))
1785 ;; We found a non-fixed-size child window.
1786 (throw 'done delta)))
1787 (setq sub (window-right sub))))
1788 ;; In an ortho-combination set DELTA to the minimum value by
1789 ;; which other child windows can shrink.
1790 (while sub
1791 (unless (eq sub window)
1792 (setq delta
1793 (min delta
1794 (max (- (window-size sub horizontal pixelwise 'ceiling)
1795 (window-min-size
1796 sub horizontal ignore pixelwise))
1797 0))))
1798 (setq sub (window-right sub))))
1799 (if noup
1800 delta
1801 (window--min-delta-1
1802 parent delta horizontal ignore trail nil pixelwise))))))
1804 (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise)
1805 "Return number of lines by which WINDOW can be shrunk.
1806 WINDOW must be a valid window and defaults to the selected one.
1807 Return zero if WINDOW cannot be shrunk.
1809 Optional argument HORIZONTAL non-nil means return number of
1810 columns by which WINDOW can be shrunk.
1812 The optional argument IGNORE has the same meaning as for
1813 `window-resizable'. Optional argument TRAIL restricts the
1814 windows that can be enlarged. If its value is `before', only
1815 windows to the left of or above WINDOW can be enlarged. If it is
1816 `after', only windows to the right of or below WINDOW can be
1817 enlarged.
1819 Optional argument NOUP non-nil means don't go up in the window
1820 tree, but try to enlarge windows within WINDOW's combination
1821 only. Optional argument NODOWN non-nil means don't check whether
1822 WINDOW itself (and its child windows) can be shrunk; check only
1823 whether at least one other window can be enlarged appropriately.
1825 Optional argument PIXELWISE non-nil means return number of pixels
1826 by which WINDOW can be shrunk."
1827 (setq window (window-normalize-window window))
1828 (let ((size (window-size window horizontal pixelwise 'floor))
1829 (minimum (window-min-size window horizontal ignore pixelwise)))
1830 (cond
1831 (nodown
1832 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1833 (window--min-delta-1
1834 window size horizontal ignore trail noup pixelwise))
1835 ((<= size minimum)
1836 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1837 ;; there's nothing to recover.
1840 ;; Otherwise, try to recover whatever WINDOW is larger than its
1841 ;; minimum size.
1842 (window--min-delta-1
1843 window (- size minimum) horizontal ignore trail noup pixelwise)))))
1845 (defun frame-windows-min-size (&optional frame horizontal ignore pixelwise)
1846 "Return minimum number of lines of FRAME's windows.
1847 HORIZONTAL non-nil means return number of columns of FRAME's
1848 windows. The optional argument IGNORE has the same meaning as
1849 for `window-resizable'. PIXELWISE non-nil means return sizes in
1850 pixels."
1851 (setq frame (window-normalize-frame frame))
1852 (let* ((root (frame-root-window frame))
1853 (mini (window-next-sibling root)))
1854 (+ (window-min-size root horizontal ignore pixelwise)
1855 (if (and mini (not horizontal))
1856 (window-min-size mini horizontal nil pixelwise)
1857 0))))
1859 (defun window--max-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
1860 "Internal function of `window-max-delta'."
1861 (if (not (window-parent window))
1862 ;; Can't go up. Return DELTA.
1863 delta
1864 (let* ((parent (window-parent window))
1865 (sub (window-child parent)))
1866 (catch 'fixed
1867 (if (window-combined-p sub horizontal)
1868 ;; For an iso-combination calculate how much we can get from
1869 ;; other child windows.
1870 (let ((skip (eq trail 'after)))
1871 (while sub
1872 (cond
1873 ((eq sub window)
1874 (setq skip (eq trail 'before)))
1875 (skip)
1877 (setq delta
1878 (+ delta
1879 (max
1880 (- (window-size sub horizontal pixelwise 'floor)
1881 (window-min-size
1882 sub horizontal ignore pixelwise))
1883 0)))))
1884 (setq sub (window-right sub))))
1885 ;; For an ortho-combination throw DELTA when at least one
1886 ;; child window is fixed-size.
1887 (while sub
1888 (when (and (not (eq sub window))
1889 (window-size-fixed-p sub horizontal ignore))
1890 (throw 'fixed delta))
1891 (setq sub (window-right sub))))
1892 (if noup
1893 ;; When NOUP is nil, DELTA is all we can get.
1894 delta
1895 ;; Else try with parent of WINDOW, passing the DELTA we
1896 ;; recovered so far.
1897 (window--max-delta-1
1898 parent delta horizontal ignore trail nil pixelwise))))))
1900 (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise)
1901 "Return maximum number of lines by which WINDOW can be enlarged.
1902 WINDOW must be a valid window and defaults to the selected one.
1903 The return value is zero if WINDOW cannot be enlarged.
1905 Optional argument HORIZONTAL non-nil means return maximum number
1906 of columns by which WINDOW can be enlarged.
1908 The optional argument IGNORE has the same meaning as for
1909 `window-resizable'. Optional argument TRAIL restricts the
1910 windows that can be enlarged. If its value is `before', only
1911 windows to the left of or above WINDOW can be enlarged. If it is
1912 `after', only windows to the right of or below WINDOW can be
1913 enlarged.
1915 Optional argument NOUP non-nil means don't go up in the window
1916 tree but try to obtain the entire space from windows within
1917 WINDOW's combination. Optional argument NODOWN non-nil means do
1918 not check whether WINDOW itself (and its child windows) can be
1919 enlarged; check only whether other windows can be shrunk
1920 appropriately.
1922 Optional argument PIXELWISE non-nil means return number of
1923 pixels by which WINDOW can be enlarged."
1924 (setq window (window-normalize-window window))
1925 (if (and (not nodown) (window-size-fixed-p window horizontal ignore))
1926 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1927 ;; size.
1929 ;; WINDOW has no fixed size.
1930 (window--max-delta-1 window 0 horizontal ignore trail noup pixelwise)))
1932 ;; Make NOUP also inhibit the min-size check.
1933 (defun window--resizable (window delta &optional horizontal ignore trail noup nodown pixelwise)
1934 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1935 WINDOW must be a valid window and defaults to the selected one.
1936 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1937 can be resized horizontally by DELTA columns. A return value of
1938 zero means that WINDOW is not resizable.
1940 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1941 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1942 return the maximum value in the range 0..DELTA by which WINDOW
1943 can be enlarged.
1945 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1946 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1947 return the minimum value in the range DELTA..0 that can be used
1948 for shrinking WINDOW.
1950 The optional argument IGNORE has the same meaning as for
1951 `window-resizable'. Optional argument TRAIL `before' means only
1952 windows to the left of or below WINDOW can be shrunk. Optional
1953 argument TRAIL `after' means only windows to the right of or
1954 above WINDOW can be shrunk.
1956 Optional argument NOUP non-nil means don't go up in the window
1957 tree but check only whether space can be obtained from (or given
1958 to) WINDOW's siblings. Optional argument NODOWN non-nil means
1959 don't go down in the window tree. This means do not check
1960 whether resizing would violate size restrictions of WINDOW or its
1961 child windows.
1963 Optional argument PIXELWISE non-nil means interpret DELTA as
1964 number of pixels."
1965 (setq window (window-normalize-window window))
1966 (cond
1967 ((< delta 0)
1968 (max (- (window-min-delta
1969 window horizontal ignore trail noup nodown pixelwise))
1970 delta))
1971 ((> delta 0)
1972 (min (window-max-delta
1973 window horizontal ignore trail noup nodown pixelwise)
1974 delta))
1975 (t 0)))
1977 (defun window--resizable-p (window delta &optional horizontal ignore trail noup nodown pixelwise)
1978 "Return t if WINDOW can be resized vertically by DELTA lines.
1979 WINDOW must be a valid window and defaults to the selected one.
1980 For the meaning of the arguments of this function see the
1981 doc-string of `window--resizable'.
1983 Optional argument PIXELWISE non-nil means interpret DELTA as
1984 pixels."
1985 (setq window (window-normalize-window window))
1986 (if (> delta 0)
1987 (>= (window--resizable
1988 window delta horizontal ignore trail noup nodown pixelwise)
1989 delta)
1990 (<= (window--resizable
1991 window delta horizontal ignore trail noup nodown pixelwise)
1992 delta)))
1994 (defun window-resizable (window delta &optional horizontal ignore pixelwise)
1995 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1996 WINDOW must be a valid window and defaults to the selected one.
1997 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1998 can be resized horizontally by DELTA columns. A return value of
1999 zero means that WINDOW is not resizable.
2001 DELTA positive means WINDOW shall be enlarged by DELTA lines or
2002 columns. If WINDOW cannot be enlarged by DELTA lines or columns
2003 return the maximum value in the range 0..DELTA by which WINDOW
2004 can be enlarged.
2006 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
2007 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
2008 return the minimum value in the range DELTA..0 that can be used
2009 for shrinking WINDOW.
2011 Optional argument IGNORE, if non-nil, means to ignore restraints
2012 induced by fixed size windows or the values of the variables
2013 `window-min-height' and `window-min-width'. The following values
2014 have special meanings: `safe' means that in addition live windows
2015 are allowed to get as small as `window-safe-min-height' lines and
2016 `window-safe-min-width' columns. `preserved' means to ignore
2017 only restrictions induced by `window-preserve-size'. If IGNORE
2018 is a window, then ignore restrictions for that window only.
2020 Optional argument PIXELWISE non-nil means interpret DELTA as
2021 pixels."
2022 (setq window (window-normalize-window window))
2023 (window--resizable window delta horizontal ignore nil nil nil pixelwise))
2025 (defun window-resizable-p (window delta &optional horizontal ignore pixelwise)
2026 "Return t if WINDOW can be resized vertically by DELTA lines.
2027 WINDOW must be a valid window and defaults to the selected one.
2028 For the meaning of the arguments of this function see the
2029 doc-string of `window-resizable'."
2030 (setq window (window-normalize-window window))
2031 (if (> delta 0)
2032 (>= (window--resizable
2033 window delta horizontal ignore nil nil nil pixelwise)
2034 delta)
2035 (<= (window--resizable
2036 window delta horizontal ignore nil nil nil pixelwise)
2037 delta)))
2039 ;; Aliases of functions defined in window.c.
2040 (defalias 'window-height 'window-total-height)
2041 (defalias 'window-width 'window-body-width)
2043 (defun window-full-height-p (&optional window)
2044 "Return t if WINDOW is as high as its containing frame.
2045 More precisely, return t if and only if the total height of
2046 WINDOW equals the total height of the root window of WINDOW's
2047 frame. WINDOW must be a valid window and defaults to the
2048 selected one."
2049 (setq window (window-normalize-window window))
2050 (if (window-minibuffer-p window)
2051 (eq window (frame-root-window (window-frame window)))
2052 (= (window-pixel-height window)
2053 (window-pixel-height (frame-root-window window)))))
2055 (defun window-full-width-p (&optional window)
2056 "Return t if WINDOW is as wide as its containing frame.
2057 More precisely, return t if and only if the total width of WINDOW
2058 equals the total width of the root window of WINDOW's frame.
2059 WINDOW must be a valid window and defaults to the selected one."
2060 (setq window (window-normalize-window window))
2061 (= (window-pixel-width window)
2062 (window-pixel-width (frame-root-window window))))
2064 (defun window-body-size (&optional window horizontal pixelwise)
2065 "Return the height or width of WINDOW's text area.
2066 WINDOW must be a live window and defaults to the selected one.
2068 If HORIZONTAL is omitted or nil, return the height of the text
2069 area, like `window-body-height'. Otherwise, return the width of
2070 the text area, like `window-body-width'. In either case, the
2071 optional argument PIXELWISE is passed to the functions."
2072 (if horizontal
2073 (window-body-width window pixelwise)
2074 (window-body-height window pixelwise)))
2076 (declare-function font-info "font.c" (name &optional frame))
2078 (defun window-font-width (&optional window face)
2079 "Return average character width for the font of FACE used in WINDOW.
2080 WINDOW must be a live window and defaults to the selected one.
2082 If FACE is nil or omitted, the default face is used. If FACE is
2083 remapped (see `face-remapping-alist'), the function returns the
2084 information for the remapped face."
2085 (with-selected-window (window-normalize-window window t)
2086 (if (display-multi-font-p)
2087 (let* ((face (if face face 'default))
2088 (info (font-info (face-font face)))
2089 (width (aref info 11)))
2090 (if (> width 0)
2091 width
2092 (aref info 10)))
2093 (frame-char-width))))
2095 (defun window-font-height (&optional window face)
2096 "Return character height for the font of FACE used in WINDOW.
2097 WINDOW must be a live window and defaults to the selected one.
2099 If FACE is nil or omitted, the default face is used. If FACE is
2100 remapped (see `face-remapping-alist'), the function returns the
2101 information for the remapped face."
2102 (with-selected-window (window-normalize-window window t)
2103 (if (display-multi-font-p)
2104 (let* ((face (if face face 'default))
2105 (info (font-info (face-font face))))
2106 (aref info 3))
2107 (frame-char-height))))
2109 (defvar overflow-newline-into-fringe)
2111 (defun window-max-chars-per-line (&optional window face)
2112 "Return the number of characters that can be displayed on one line in WINDOW.
2113 WINDOW must be a live window and defaults to the selected one.
2115 The character width of FACE is used for the calculation. If FACE
2116 is nil or omitted, the default face is used. If FACE is
2117 remapped (see `face-remapping-alist'), the function uses the
2118 remapped face.
2120 This function is different from `window-body-width' in two
2121 ways. First, it accounts for the portions of the line reserved
2122 for the continuation glyph. Second, it accounts for the size of
2123 the font."
2124 (with-selected-window (window-normalize-window window t)
2125 (let* ((window-width (window-body-width window t))
2126 (font-width (window-font-width window face))
2127 (ncols (/ window-width font-width)))
2128 (if (and (display-graphic-p)
2129 overflow-newline-into-fringe
2130 (not
2131 (or (eq left-fringe-width 0)
2132 (and (null left-fringe-width)
2133 (= (frame-parameter nil 'left-fringe) 0))))
2134 (not
2135 (or (eq right-fringe-width 0)
2136 (and (null right-fringe-width)
2137 (= (frame-parameter nil 'right-fringe) 0)))))
2138 ncols
2139 ;; FIXME: This should remove 1 more column when there are no
2140 ;; fringes, lines are truncated, and the window is hscrolled,
2141 ;; but EOL is not in the view, because then there are 2
2142 ;; truncation glyphs, not one.
2143 (1- ncols)))))
2145 (defun window-current-scroll-bars (&optional window)
2146 "Return the current scroll bar types for WINDOW.
2147 WINDOW must be a live window and defaults to the selected one.
2149 The return value is a cons cell (VERTICAL . HORIZONTAL) where
2150 VERTICAL specifies the current location of the vertical scroll
2151 bar (`left', `right' or nil), and HORIZONTAL specifies the
2152 current location of the horizontal scroll bar (`bottom' or nil).
2154 Unlike `window-scroll-bars', this function reports the scroll bar
2155 type actually used, once frame defaults and `scroll-bar-mode' are
2156 taken into account."
2157 (setq window (window-normalize-window window t))
2158 (let ((vertical (nth 2 (window-scroll-bars window)))
2159 (horizontal (nth 5 (window-scroll-bars window)))
2160 (inherited (frame-current-scroll-bars (window-frame window))))
2161 (when (eq vertical t)
2162 (setq vertical (car inherited)))
2163 (when (eq horizontal t)
2164 (setq horizontal (cdr inherited)))
2165 (cons vertical (and horizontal 'bottom))))
2167 (defun walk-windows (fun &optional minibuf all-frames)
2168 "Cycle through all live windows, calling FUN for each one.
2169 FUN must specify a function with a window as its sole argument.
2170 The optional arguments MINIBUF and ALL-FRAMES specify the set of
2171 windows to include in the walk.
2173 MINIBUF t means include the minibuffer window even if the
2174 minibuffer is not active. MINIBUF nil or omitted means include
2175 the minibuffer window only if the minibuffer is active. Any
2176 other value means do not include the minibuffer window even if
2177 the minibuffer is active.
2179 ALL-FRAMES nil or omitted means consider all windows on the
2180 selected frame, plus the minibuffer window if specified by the
2181 MINIBUF argument. If the minibuffer counts, consider all windows
2182 on all frames that share that minibuffer too. The following
2183 non-nil values of ALL-FRAMES have special meanings:
2185 - t means consider all windows on all existing frames.
2187 - `visible' means consider all windows on all visible frames on
2188 the current terminal.
2190 - 0 (the number zero) means consider all windows on all visible
2191 and iconified frames on the current terminal.
2193 - A frame means consider all windows on that frame only.
2195 Anything else means consider all windows on the selected frame
2196 and no others.
2198 This function changes neither the order of recently selected
2199 windows nor the buffer list."
2200 ;; If we start from the minibuffer window, don't fail to come
2201 ;; back to it.
2202 (when (window-minibuffer-p)
2203 (setq minibuf t))
2204 ;; Make sure to not mess up the order of recently selected
2205 ;; windows. Use `save-selected-window' and `select-window'
2206 ;; with second argument non-nil for this purpose.
2207 (save-selected-window
2208 (when (framep all-frames)
2209 (select-window (frame-first-window all-frames) 'norecord))
2210 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
2211 (funcall fun walk-windows-window))))
2213 (defun window-at-side-p (&optional window side)
2214 "Return t if WINDOW is at SIDE of its containing frame.
2215 WINDOW must be a valid window and defaults to the selected one.
2216 SIDE can be any of the symbols `left', `top', `right' or
2217 `bottom'. The default value nil is handled like `bottom'."
2218 (setq window (window-normalize-window window))
2219 (let ((edge
2220 (cond
2221 ((eq side 'left) 0)
2222 ((eq side 'top) 1)
2223 ((eq side 'right) 2)
2224 ((memq side '(bottom nil)) 3))))
2225 (= (nth edge (window-pixel-edges window))
2226 (nth edge (window-pixel-edges (frame-root-window window))))))
2228 (defun window-at-side-list (&optional frame side)
2229 "Return list of all windows on SIDE of FRAME.
2230 FRAME must be a live frame and defaults to the selected frame.
2231 SIDE can be any of the symbols `left', `top', `right' or
2232 `bottom'. The default value nil is handled like `bottom'."
2233 (setq frame (window-normalize-frame frame))
2234 (let (windows)
2235 (walk-window-tree
2236 (lambda (window)
2237 (when (window-at-side-p window side)
2238 (setq windows (cons window windows))))
2239 frame nil 'nomini)
2240 (nreverse windows)))
2242 (defun window--in-direction-2 (window posn &optional horizontal)
2243 "Support function for `window-in-direction'."
2244 (if horizontal
2245 (let ((top (window-pixel-top window)))
2246 (if (> top posn)
2247 (- top posn)
2248 (- posn top (window-pixel-height window))))
2249 (let ((left (window-pixel-left window)))
2250 (if (> left posn)
2251 (- left posn)
2252 (- posn left (window-pixel-width window))))))
2254 ;; Predecessors to the below have been devised by Julian Assange in
2255 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
2256 ;; Neither of these allow one to selectively ignore specific windows
2257 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
2258 ;; the movement.
2259 (defun window-in-direction (direction &optional window ignore sign wrap mini)
2260 "Return window in DIRECTION as seen from WINDOW.
2261 More precisely, return the nearest window in direction DIRECTION
2262 as seen from the position of `window-point' in window WINDOW.
2263 DIRECTION must be one of `above', `below', `left' or `right'.
2264 WINDOW must be a live window and defaults to the selected one.
2266 Do not return a window whose `no-other-window' parameter is
2267 non-nil. If the nearest window's `no-other-window' parameter is
2268 non-nil, try to find another window in the indicated direction.
2269 If, however, the optional argument IGNORE is non-nil, return that
2270 window even if its `no-other-window' parameter is non-nil.
2272 Optional argument SIGN a negative number means to use the right
2273 or bottom edge of WINDOW as reference position instead of
2274 `window-point'. SIGN a positive number means to use the left or
2275 top edge of WINDOW as reference position.
2277 Optional argument WRAP non-nil means to wrap DIRECTION around
2278 frame borders. This means to return for WINDOW at the top of the
2279 frame and DIRECTION `above' the minibuffer window if the frame
2280 has one, and a window at the bottom of the frame otherwise.
2282 Optional argument MINI nil means to return the minibuffer window
2283 if and only if it is currently active. MINI non-nil means to
2284 return the minibuffer window even when it's not active. However,
2285 if WRAP is non-nil, always act as if MINI were nil.
2287 Return nil if no suitable window can be found."
2288 (setq window (window-normalize-window window t))
2289 (unless (memq direction '(above below left right))
2290 (error "Wrong direction %s" direction))
2291 (let* ((frame (window-frame window))
2292 (hor (memq direction '(left right)))
2293 (first (if hor
2294 (window-pixel-left window)
2295 (window-pixel-top window)))
2296 (last (+ first (window-size window hor t)))
2297 ;; The column / row value of `posn-at-point' can be nil for the
2298 ;; mini-window, guard against that.
2299 (posn
2300 (cond
2301 ((and (numberp sign) (< sign 0))
2302 (if hor
2303 (1- (+ (window-pixel-top window) (window-pixel-height window)))
2304 (1- (+ (window-pixel-left window) (window-pixel-width window)))))
2305 ((and (numberp sign) (> sign 0))
2306 (if hor
2307 (window-pixel-top window)
2308 (window-pixel-left window)))
2309 ((let ((posn-cons (nth 2 (posn-at-point (window-point window) window))))
2310 (if hor
2311 (+ (or (cdr posn-cons) 1) (window-pixel-top window))
2312 (+ (or (car posn-cons) 1) (window-pixel-left window)))))))
2313 (best-edge
2314 (cond
2315 ((eq direction 'below) (frame-pixel-height frame))
2316 ((eq direction 'right) (frame-pixel-width frame))
2317 (t -1)))
2318 (best-edge-2 best-edge)
2319 (best-diff-2 (if hor (frame-pixel-height frame) (frame-pixel-width frame)))
2320 best best-2 best-diff-2-new)
2321 (walk-window-tree
2322 (lambda (w)
2323 (let* ((w-top (window-pixel-top w))
2324 (w-left (window-pixel-left w)))
2325 (cond
2326 ((or (eq window w)
2327 ;; Ignore ourselves.
2328 (and (window-parameter w 'no-other-window)
2329 ;; Ignore W unless IGNORE is non-nil.
2330 (not ignore))))
2331 (hor
2332 (cond
2333 ((and (<= w-top posn)
2334 (< posn (+ w-top (window-pixel-height w))))
2335 ;; W is to the left or right of WINDOW and covers POSN.
2336 (when (or (and (eq direction 'left)
2337 (or (and (<= w-left first) (> w-left best-edge))
2338 (and wrap
2339 (window-at-side-p window 'left)
2340 (window-at-side-p w 'right))))
2341 (and (eq direction 'right)
2342 (or (and (>= w-left last) (< w-left best-edge))
2343 (and wrap
2344 (window-at-side-p window 'right)
2345 (window-at-side-p w 'left)))))
2346 (setq best-edge w-left)
2347 (setq best w)))
2348 ((and (or (and (eq direction 'left)
2349 (<= (+ w-left (window-pixel-width w)) first))
2350 (and (eq direction 'right) (<= last w-left)))
2351 ;; W is to the left or right of WINDOW but does not
2352 ;; cover POSN.
2353 (setq best-diff-2-new
2354 (window--in-direction-2 w posn hor))
2355 (or (< best-diff-2-new best-diff-2)
2356 (and (= best-diff-2-new best-diff-2)
2357 (if (eq direction 'left)
2358 (> w-left best-edge-2)
2359 (< w-left best-edge-2)))))
2360 (setq best-edge-2 w-left)
2361 (setq best-diff-2 best-diff-2-new)
2362 (setq best-2 w))))
2363 ((and (<= w-left posn)
2364 (< posn (+ w-left (window-pixel-width w))))
2365 ;; W is above or below WINDOW and covers POSN.
2366 (when (or (and (eq direction 'above)
2367 (or (and (<= w-top first) (> w-top best-edge))
2368 (and wrap
2369 (window-at-side-p window 'top)
2370 (if (active-minibuffer-window)
2371 (minibuffer-window-active-p w)
2372 (window-at-side-p w 'bottom)))))
2373 (and (eq direction 'below)
2374 (or (and (>= w-top first) (< w-top best-edge))
2375 (and wrap
2376 (if (active-minibuffer-window)
2377 (minibuffer-window-active-p window)
2378 (window-at-side-p window 'bottom))
2379 (window-at-side-p w 'top)))))
2380 (setq best-edge w-top)
2381 (setq best w)))
2382 ((and (or (and (eq direction 'above)
2383 (<= (+ w-top (window-pixel-height w)) first))
2384 (and (eq direction 'below) (<= last w-top)))
2385 ;; W is above or below WINDOW but does not cover POSN.
2386 (setq best-diff-2-new
2387 (window--in-direction-2 w posn hor))
2388 (or (< best-diff-2-new best-diff-2)
2389 (and (= best-diff-2-new best-diff-2)
2390 (if (eq direction 'above)
2391 (> w-top best-edge-2)
2392 (< w-top best-edge-2)))))
2393 (setq best-edge-2 w-top)
2394 (setq best-diff-2 best-diff-2-new)
2395 (setq best-2 w)))))
2396 frame nil (and mini t))
2397 (or best best-2)))
2399 (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
2400 "Return a live window satisfying PREDICATE.
2401 More precisely, cycle through all windows calling the function
2402 PREDICATE on each one of them with the window as its sole
2403 argument. Return the first window for which PREDICATE returns
2404 non-nil. Windows are scanned starting with the window following
2405 the selected window. If no window satisfies PREDICATE, return
2406 DEFAULT.
2408 MINIBUF t means include the minibuffer window even if the
2409 minibuffer is not active. MINIBUF nil or omitted means include
2410 the minibuffer window only if the minibuffer is active. Any
2411 other value means do not include the minibuffer window even if
2412 the minibuffer is active.
2414 ALL-FRAMES nil or omitted means consider all windows on the selected
2415 frame, plus the minibuffer window if specified by the MINIBUF
2416 argument. If the minibuffer counts, consider all windows on all
2417 frames that share that minibuffer too. The following non-nil
2418 values of ALL-FRAMES have special meanings:
2420 - t means consider all windows on all existing frames.
2422 - `visible' means consider all windows on all visible frames on
2423 the current terminal.
2425 - 0 (the number zero) means consider all windows on all visible
2426 and iconified frames on the current terminal.
2428 - A frame means consider all windows on that frame only.
2430 Anything else means consider all windows on the selected frame
2431 and no others."
2432 (catch 'found
2433 (dolist (window (window-list-1
2434 (next-window nil minibuf all-frames)
2435 minibuf all-frames))
2436 (when (funcall predicate window)
2437 (throw 'found window)))
2438 default))
2440 (defalias 'some-window 'get-window-with-predicate)
2442 (defun get-lru-window (&optional all-frames dedicated not-selected)
2443 "Return the least recently used window on frames specified by ALL-FRAMES.
2444 Return a full-width window if possible. A minibuffer window is
2445 never a candidate. A dedicated window is never a candidate
2446 unless DEDICATED is non-nil, so if all windows are dedicated, the
2447 value is nil. Avoid returning the selected window if possible.
2448 Optional argument NOT-SELECTED non-nil means never return the
2449 selected window.
2451 The following non-nil values of the optional argument ALL-FRAMES
2452 have special meanings:
2454 - t means consider all windows on all existing frames.
2456 - `visible' means consider all windows on all visible frames on
2457 the current terminal.
2459 - 0 (the number zero) means consider all windows on all visible
2460 and iconified frames on the current terminal.
2462 - A frame means consider all windows on that frame only.
2464 Any other value of ALL-FRAMES means consider all windows on the
2465 selected frame and no others."
2466 (let (best-window best-time second-best-window second-best-time time)
2467 (dolist (window (window-list-1 nil 'nomini all-frames))
2468 (when (and (or dedicated (not (window-dedicated-p window)))
2469 (or (not not-selected) (not (eq window (selected-window)))))
2470 (setq time (window-use-time window))
2471 (if (or (eq window (selected-window))
2472 (not (window-full-width-p window)))
2473 (when (or (not second-best-time) (< time second-best-time))
2474 (setq second-best-time time)
2475 (setq second-best-window window))
2476 (when (or (not best-time) (< time best-time))
2477 (setq best-time time)
2478 (setq best-window window)))))
2479 (or best-window second-best-window)))
2481 (defun get-mru-window (&optional all-frames dedicated not-selected)
2482 "Return the most recently used window on frames specified by ALL-FRAMES.
2483 A minibuffer window is never a candidate. A dedicated window is
2484 never a candidate unless DEDICATED is non-nil, so if all windows
2485 are dedicated, the value is nil. Optional argument NOT-SELECTED
2486 non-nil means never return the selected window.
2488 The following non-nil values of the optional argument ALL-FRAMES
2489 have special meanings:
2491 - t means consider all windows on all existing frames.
2493 - `visible' means consider all windows on all visible frames on
2494 the current terminal.
2496 - 0 (the number zero) means consider all windows on all visible
2497 and iconified frames on the current terminal.
2499 - A frame means consider all windows on that frame only.
2501 Any other value of ALL-FRAMES means consider all windows on the
2502 selected frame and no others."
2503 (let (best-window best-time time)
2504 (dolist (window (window-list-1 nil 'nomini all-frames))
2505 (setq time (window-use-time window))
2506 (when (and (or dedicated (not (window-dedicated-p window)))
2507 (or (not not-selected) (not (eq window (selected-window))))
2508 (or (not best-time) (> time best-time)))
2509 (setq best-time time)
2510 (setq best-window window)))
2511 best-window))
2513 (defun get-largest-window (&optional all-frames dedicated not-selected)
2514 "Return the largest window on frames specified by ALL-FRAMES.
2515 A minibuffer window is never a candidate. A dedicated window is
2516 never a candidate unless DEDICATED is non-nil, so if all windows
2517 are dedicated, the value is nil. Optional argument NOT-SELECTED
2518 non-nil means never return the selected window.
2520 The following non-nil values of the optional argument ALL-FRAMES
2521 have special meanings:
2523 - t means consider all windows on all existing frames.
2525 - `visible' means consider all windows on all visible frames on
2526 the current terminal.
2528 - 0 (the number zero) means consider all windows on all visible
2529 and iconified frames on the current terminal.
2531 - A frame means consider all windows on that frame only.
2533 Any other value of ALL-FRAMES means consider all windows on the
2534 selected frame and no others."
2535 (let ((best-size 0)
2536 best-window size)
2537 (dolist (window (window-list-1 nil 'nomini all-frames))
2538 (when (and (or dedicated (not (window-dedicated-p window)))
2539 (or (not not-selected) (not (eq window (selected-window)))))
2540 (setq size (* (window-pixel-height window)
2541 (window-pixel-width window)))
2542 (when (> size best-size)
2543 (setq best-size size)
2544 (setq best-window window))))
2545 best-window))
2547 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
2548 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2549 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2550 and defaults to the current buffer. If the selected window displays
2551 BUFFER-OR-NAME, it will be the first in the resulting list.
2553 MINIBUF t means include the minibuffer window even if the
2554 minibuffer is not active. MINIBUF nil or omitted means include
2555 the minibuffer window only if the minibuffer is active. Any
2556 other value means do not include the minibuffer window even if
2557 the minibuffer is active.
2559 ALL-FRAMES nil or omitted means consider all windows on the
2560 selected frame, plus the minibuffer window if specified by the
2561 MINIBUF argument. If the minibuffer counts, consider all windows
2562 on all frames that share that minibuffer too. The following
2563 non-nil values of ALL-FRAMES have special meanings:
2565 - t means consider all windows on all existing frames.
2567 - `visible' means consider all windows on all visible frames on
2568 the current terminal.
2570 - 0 (the number zero) means consider all windows on all visible
2571 and iconified frames on the current terminal.
2573 - A frame means consider all windows on that frame only.
2575 Anything else means consider all windows on the selected frame
2576 and no others."
2577 (let ((buffer (window-normalize-buffer buffer-or-name))
2578 windows)
2579 (dolist (window (window-list-1 (selected-window) minibuf all-frames))
2580 (when (eq (window-buffer window) buffer)
2581 (setq windows (cons window windows))))
2582 (nreverse windows)))
2584 (defun minibuffer-window-active-p (window)
2585 "Return t if WINDOW is the currently active minibuffer window."
2586 (and (window-live-p window) (eq window (active-minibuffer-window))))
2588 (defun count-windows (&optional minibuf)
2589 "Return the number of live windows on the selected frame.
2590 The optional argument MINIBUF specifies whether the minibuffer
2591 window shall be counted. See `walk-windows' for the precise
2592 meaning of this argument."
2593 (length (window-list-1 nil minibuf)))
2595 ;;; Resizing windows.
2596 (defun window--size-to-pixel (window size &optional horizontal pixelwise round-maybe)
2597 "For WINDOW convert SIZE lines to pixels.
2598 SIZE is supposed to specify a height of WINDOW in terms of text
2599 lines. The return value is the number of pixels specifying that
2600 height.
2602 WINDOW must be a valid window. Optional argument HORIZONTAL
2603 non-nil means convert SIZE columns to pixels.
2605 Optional argument PIXELWISE non-nil means SIZE already specifies
2606 pixels but may have to be adjusted to a multiple of the character
2607 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2608 means round to the nearest multiple of the character size of
2609 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2610 (setq window (window-normalize-window window))
2611 (let ((char-size (frame-char-size window horizontal)))
2612 (if pixelwise
2613 (if (and round-maybe (not window-resize-pixelwise))
2614 (* (round size char-size) char-size)
2615 size)
2616 (* size char-size))))
2618 (defun window--pixel-to-total-1 (window horizontal char-size)
2619 "Subroutine of `window--pixel-to-total'."
2620 (let ((child (window-child window)))
2621 (if (window-combination-p window horizontal)
2622 ;; In an iso-combination distribute sizes proportionally.
2623 (let ((remainder (window-new-total window))
2624 size best-child rem best-rem)
2625 ;; Initialize total sizes to each child's floor.
2626 (while child
2627 (setq size (max (/ (window-size child horizontal t) char-size) 1))
2628 (set-window-new-total child size)
2629 (setq remainder (- remainder size))
2630 (setq child (window-next-sibling child)))
2631 ;; Distribute remainder.
2632 (while (> remainder 0)
2633 (setq child (window-last-child window))
2634 (setq best-child nil)
2635 (setq best-rem 0)
2636 (while child
2637 (when (and (<= (window-new-total child)
2638 (/ (window-size child horizontal t) char-size))
2639 (> (setq rem (% (window-size child horizontal t)
2640 char-size))
2641 best-rem))
2642 (setq best-child child)
2643 (setq best-rem rem))
2644 (setq child (window-prev-sibling child)))
2645 ;; We MUST have a best-child here.
2646 (set-window-new-total best-child 1 t)
2647 (setq remainder (1- remainder)))
2648 ;; Recurse.
2649 (setq child (window-child window))
2650 (while child
2651 (window--pixel-to-total-1 child horizontal char-size)
2652 (setq child (window-next-sibling child))))
2653 ;; In an ortho-combination assign new sizes directly.
2654 (let ((size (window-new-total window)))
2655 (while child
2656 (set-window-new-total child size)
2657 (window--pixel-to-total-1 child horizontal char-size)
2658 (setq child (window-next-sibling child)))))))
2660 (defun window--pixel-to-total (&optional frame horizontal)
2661 "On FRAME assign new total window heights from pixel heights.
2662 FRAME must be a live frame and defaults to the selected frame.
2664 Optional argument HORIZONTAL non-nil means assign new total
2665 window widths from pixel widths."
2666 (setq frame (window-normalize-frame frame))
2667 (let* ((char-size (frame-char-size frame horizontal))
2668 (root (frame-root-window frame))
2669 (root-size (window-size root horizontal t))
2670 ;; We have to care about the minibuffer window only if it
2671 ;; appears together with the root window on this frame.
2672 (mini (let ((mini (minibuffer-window frame)))
2673 (and (eq (window-frame mini) frame)
2674 (not (eq mini root)) mini)))
2675 (mini-size (and mini (window-size mini horizontal t))))
2676 ;; We round the line/column sizes of windows here to the nearest
2677 ;; integer. In some cases this can make windows appear _larger_
2678 ;; than the containing frame (line/column-wise) because the latter's
2679 ;; sizes are not (yet) rounded. We might eventually fix that.
2680 (if (and mini (not horizontal))
2681 (let (lines)
2682 (set-window-new-total root (max (/ root-size char-size) 1))
2683 (set-window-new-total mini (max (/ mini-size char-size) 1))
2684 (setq lines (- (round (+ root-size mini-size) char-size)
2685 (+ (window-new-total root) (window-new-total mini))))
2686 (while (> lines 0)
2687 (if (>= (% root-size (window-new-total root))
2688 (% mini-size (window-new-total mini)))
2689 (set-window-new-total root 1 t)
2690 (set-window-new-total mini 1 t))
2691 (setq lines (1- lines))))
2692 (set-window-new-total root (round root-size char-size))
2693 (when mini
2694 ;; This is taken in the horizontal case only.
2695 (set-window-new-total mini (round mini-size char-size))))
2696 (unless (window-buffer root)
2697 (window--pixel-to-total-1 root horizontal char-size))
2698 ;; Apply the new sizes.
2699 (window-resize-apply-total frame horizontal)))
2701 (defun window--resize-reset (&optional frame horizontal)
2702 "Reset resize values for all windows on FRAME.
2703 FRAME defaults to the selected frame.
2705 This function stores the current value of `window-size' applied
2706 with argument HORIZONTAL in the new total size of all windows on
2707 FRAME. It also resets the new normal size of each of these
2708 windows."
2709 (window--resize-reset-1
2710 (frame-root-window (window-normalize-frame frame)) horizontal))
2712 (defun window--resize-reset-1 (window horizontal)
2713 "Internal function of `window--resize-reset'."
2714 ;; Register old size in the new total size.
2715 (set-window-new-pixel window (window-size window horizontal t))
2716 (set-window-new-total window (window-size window horizontal))
2717 ;; Reset new normal size.
2718 (set-window-new-normal window)
2719 (when (window-child window)
2720 (window--resize-reset-1 (window-child window) horizontal))
2721 (when (window-right window)
2722 (window--resize-reset-1 (window-right window) horizontal)))
2724 (defun window--resize-mini-window (window delta)
2725 "Resize minibuffer window WINDOW by DELTA pixels.
2726 If WINDOW cannot be resized by DELTA pixels make it as large (or
2727 as small) as possible, but don't signal an error."
2728 (when (window-minibuffer-p window)
2729 (let* ((frame (window-frame window))
2730 (root (frame-root-window frame))
2731 (height (window-pixel-height window))
2732 (min-delta
2733 (- (window-pixel-height root)
2734 (window-min-size root nil nil t))))
2735 ;; Sanitize DELTA.
2736 (cond
2737 ((<= (+ height delta) 0)
2738 (setq delta (- (frame-char-height (window-frame window)) height)))
2739 ((> delta min-delta)
2740 (setq delta min-delta)))
2742 (unless (zerop delta)
2743 ;; Resize now.
2744 (window--resize-reset frame)
2745 ;; Ideally we should be able to resize just the last child of root
2746 ;; here. See the comment in `resize-root-window-vertically' for
2747 ;; why we do not do that.
2748 (window--resize-this-window root (- delta) nil nil t)
2749 (set-window-new-pixel window (+ height delta))
2750 ;; The following routine catches the case where we want to resize
2751 ;; a minibuffer-only frame.
2752 (when (resize-mini-window-internal window)
2753 (window--pixel-to-total frame)
2754 (run-window-configuration-change-hook frame))))))
2756 (defun window--resize-apply-p (frame &optional horizontal)
2757 "Return t when a window on FRAME shall be resized vertically.
2758 Optional argument HORIZONTAL non-nil means return t when a window
2759 shall be resized horizontally."
2760 (catch 'apply
2761 (walk-window-tree
2762 (lambda (window)
2763 (unless (= (window-new-pixel window)
2764 (window-size window horizontal t))
2765 (throw 'apply t)))
2766 frame t)
2767 nil))
2769 (defun window-resize (window delta &optional horizontal ignore pixelwise)
2770 "Resize WINDOW vertically by DELTA lines.
2771 WINDOW can be an arbitrary window and defaults to the selected
2772 one. An attempt to resize the root window of a frame will raise
2773 an error though.
2775 DELTA a positive number means WINDOW shall be enlarged by DELTA
2776 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2777 lines.
2779 Optional argument HORIZONTAL non-nil means resize WINDOW
2780 horizontally by DELTA columns. In this case a positive DELTA
2781 means enlarge WINDOW by DELTA columns. DELTA negative means
2782 WINDOW shall be shrunk by -DELTA columns.
2784 Optional argument IGNORE, if non-nil, means to ignore restraints
2785 induced by fixed size windows or the values of the variables
2786 `window-min-height' and `window-min-width'. The following values
2787 have special meanings: `safe' means that in addition live windows
2788 are allowed to get as small as `window-safe-min-height' lines and
2789 `window-safe-min-width' columns. `preserved' means to ignore
2790 only restrictions induced by `window-preserve-size'. If IGNORE
2791 is a window, then ignore restrictions for that window only.
2793 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2794 pixels.
2796 This function resizes other windows proportionally and never
2797 deletes any windows. If you want to move only the low (right)
2798 edge of WINDOW consider using `adjust-window-trailing-edge'
2799 instead."
2800 (setq window (window-normalize-window window))
2801 (let* ((frame (window-frame window))
2802 (minibuffer-window (minibuffer-window frame))
2803 sibling)
2804 (setq delta (window--size-to-pixel
2805 window delta horizontal pixelwise t))
2806 (cond
2807 ((eq window (frame-root-window frame))
2808 (error "Cannot resize the root window of a frame"))
2809 ((window-minibuffer-p window)
2810 (if horizontal
2811 (error "Cannot resize minibuffer window horizontally")
2812 (window--resize-mini-window window delta)))
2813 ((and (not horizontal)
2814 (window-full-height-p window)
2815 (eq (window-frame minibuffer-window) frame)
2816 (or (not resize-mini-windows)
2817 (eq minibuffer-window (active-minibuffer-window))))
2818 ;; If WINDOW is full height and either `resize-mini-windows' is
2819 ;; nil or the minibuffer window is active, resize the minibuffer
2820 ;; window.
2821 (window--resize-mini-window minibuffer-window (- delta)))
2822 ((or (window--resizable-p
2823 window delta horizontal ignore nil nil nil t)
2824 (and (not ignore)
2825 (setq ignore 'preserved)
2826 (window--resizable-p
2827 window delta horizontal ignore nil nil nil t)))
2828 (window--resize-reset frame horizontal)
2829 (window--resize-this-window window delta horizontal ignore t)
2830 (if (and (not (eq window-combination-resize t))
2831 (window-combined-p window horizontal)
2832 (setq sibling (or (window-right window) (window-left window)))
2833 (window-sizable-p
2834 sibling (- delta) horizontal ignore t))
2835 ;; If window-combination-resize is nil, WINDOW is part of an
2836 ;; iso-combination, and WINDOW's neighboring right or left
2837 ;; sibling can be resized as requested, resize that sibling.
2838 (let ((normal-delta
2839 (/ (float delta)
2840 (window-size (window-parent window) horizontal t))))
2841 (window--resize-this-window sibling (- delta) horizontal nil t)
2842 (set-window-new-normal
2843 window (+ (window-normal-size window horizontal)
2844 normal-delta))
2845 (set-window-new-normal
2846 sibling (- (window-normal-size sibling horizontal)
2847 normal-delta)))
2848 ;; Otherwise, resize all other windows in the same combination.
2849 (window--resize-siblings window delta horizontal ignore))
2850 (when (window--resize-apply-p frame horizontal)
2851 (if (window-resize-apply frame horizontal)
2852 (progn
2853 (window--pixel-to-total frame horizontal)
2854 (run-window-configuration-change-hook frame))
2855 (error "Failed to apply resizing %s" window))))
2857 (error "Cannot resize window %s" window)))))
2859 (defun window-resize-no-error (window delta &optional horizontal ignore pixelwise)
2860 "Resize WINDOW vertically if it is resizable by DELTA lines.
2861 This function is like `window-resize' but does not signal an
2862 error when WINDOW cannot be resized. For the meaning of the
2863 optional arguments see the documentation of `window-resize'."
2864 (when (window--resizable-p
2865 window delta horizontal ignore nil nil nil pixelwise)
2866 (window-resize window delta horizontal ignore pixelwise)))
2868 (defun window--resize-child-windows-skip-p (window)
2869 "Return non-nil if WINDOW shall be skipped by resizing routines."
2870 (memq (window-new-normal window) '(ignore stuck skip)))
2872 (defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
2873 "Recursively set new normal height of child windows of window PARENT.
2874 HORIZONTAL non-nil means set the new normal width of these
2875 windows. WINDOW specifies a child window of PARENT that has been
2876 resized by THIS-DELTA lines (columns).
2878 Optional argument TRAIL either `before' or `after' means set values
2879 only for windows before or after WINDOW. Optional argument
2880 OTHER-DELTA, a number, specifies that this many lines (columns)
2881 have been obtained from (or returned to) an ancestor window of
2882 PARENT in order to resize WINDOW."
2883 (let* ((delta-normal
2884 (if (and (= (- this-delta)
2885 (window-size window horizontal t))
2886 (zerop other-delta))
2887 ;; When WINDOW gets deleted and we can return its entire
2888 ;; space to its siblings, use WINDOW's normal size as the
2889 ;; normal delta.
2890 (- (window-normal-size window horizontal))
2891 ;; In any other case calculate the normal delta from the
2892 ;; relation of THIS-DELTA to the total size of PARENT.
2893 (/ (float this-delta)
2894 (window-size parent horizontal t))))
2895 (sub (window-child parent))
2896 (parent-normal 0.0)
2897 (skip (eq trail 'after)))
2899 ;; Set parent-normal to the sum of the normal sizes of all child
2900 ;; windows of PARENT that shall be resized, excluding only WINDOW
2901 ;; and any windows specified by the optional TRAIL argument.
2902 (while sub
2903 (cond
2904 ((eq sub window)
2905 (setq skip (eq trail 'before)))
2906 (skip)
2908 (setq parent-normal
2909 (+ parent-normal (window-normal-size sub horizontal)))))
2910 (setq sub (window-right sub)))
2912 ;; Set the new normal size of all child windows of PARENT from what
2913 ;; they should have contributed for recovering THIS-DELTA lines
2914 ;; (columns).
2915 (setq sub (window-child parent))
2916 (setq skip (eq trail 'after))
2917 (while sub
2918 (cond
2919 ((eq sub window)
2920 (setq skip (eq trail 'before)))
2921 (skip)
2923 (let ((old-normal (window-normal-size sub horizontal)))
2924 (set-window-new-normal
2925 sub (min 1.0 ; Don't get larger than 1.
2926 (max (- old-normal
2927 (* (/ old-normal parent-normal)
2928 delta-normal))
2929 ;; Don't drop below 0.
2930 0.0))))))
2931 (setq sub (window-right sub)))
2933 (when (numberp other-delta)
2934 ;; Set the new normal size of windows from what they should have
2935 ;; contributed for recovering OTHER-DELTA lines (columns).
2936 (setq delta-normal (/ (float (window-size parent horizontal t))
2937 (+ (window-size parent horizontal t)
2938 other-delta)))
2939 (setq sub (window-child parent))
2940 (setq skip (eq trail 'after))
2941 (while sub
2942 (cond
2943 ((eq sub window)
2944 (setq skip (eq trail 'before)))
2945 (skip)
2947 (set-window-new-normal
2948 sub (min 1.0 ; Don't get larger than 1.
2949 (max (* (window-new-normal sub) delta-normal)
2950 ;; Don't drop below 0.
2951 0.0)))))
2952 (setq sub (window-right sub))))
2954 ;; Set the new normal size of WINDOW to what is left by the sum of
2955 ;; the normal sizes of its siblings.
2956 (set-window-new-normal
2957 window
2958 (let ((sum 0))
2959 (setq sub (window-child parent))
2960 (while sub
2961 (cond
2962 ((eq sub window))
2963 ((not (numberp (window-new-normal sub)))
2964 (setq sum (+ sum (window-normal-size sub horizontal))))
2966 (setq sum (+ sum (window-new-normal sub)))))
2967 (setq sub (window-right sub)))
2968 ;; Don't get larger than 1 or smaller than 0.
2969 (min 1.0 (max (- 1.0 sum) 0.0))))))
2971 (defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge char-size)
2972 "Resize child windows of window PARENT vertically by DELTA pixels.
2973 PARENT must be a vertically combined internal window.
2975 Optional argument HORIZONTAL non-nil means resize child windows
2976 of PARENT horizontally by DELTA pixels. In this case PARENT must
2977 be a horizontally combined internal window.
2979 WINDOW, if specified, must denote a child window of PARENT that
2980 is resized by DELTA pixels.
2982 The optional argument IGNORE has the same meaning as for
2983 `window-resizable'.
2985 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2986 of windows that shall be resized. If TRAIL equals `before',
2987 resize only windows on the left or above EDGE. If TRAIL equals
2988 `after', resize only windows on the right or below EDGE. Also,
2989 preferably only resize windows adjacent to EDGE.
2991 If the optional argument CHAR-SIZE is a positive integer, it specifies
2992 the number of pixels by which windows are incrementally resized.
2993 If CHAR-SIZE is nil, this means to use the value of
2994 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2996 Return the symbol `normalized' if new normal sizes have been
2997 already set by this routine."
2998 (let* ((first (window-child parent))
2999 (last (window-last-child parent))
3000 (parent-total (+ (window-size parent horizontal t)
3001 delta))
3002 (char-size (or char-size
3003 (and window-resize-pixelwise 1)
3004 (frame-char-size window horizontal)))
3005 sub best-window best-value best-delta)
3007 (if (and edge (memq trail '(before after))
3008 (progn
3009 (setq sub first)
3010 (while (and (window-right sub)
3011 (or (and (eq trail 'before)
3012 (not (window--resize-child-windows-skip-p
3013 (window-right sub))))
3014 (and (eq trail 'after)
3015 (window--resize-child-windows-skip-p sub))))
3016 (setq sub (window-right sub)))
3017 sub)
3018 (if horizontal
3019 (if (eq trail 'before)
3020 (= (+ (window-pixel-left sub) (window-pixel-width sub))
3021 edge)
3022 (= (window-pixel-left sub) edge))
3023 (if (eq trail 'before)
3024 (= (+ (window-pixel-top sub) (window-pixel-height sub))
3025 edge)
3026 (= (window-pixel-top sub) edge)))
3027 (window-sizable-p sub delta horizontal ignore t))
3028 ;; Resize only windows adjacent to EDGE.
3029 (progn
3030 (window--resize-this-window
3031 sub delta horizontal ignore t trail edge)
3032 (if (and window (eq (window-parent sub) parent))
3033 (progn
3034 ;; Assign new normal sizes.
3035 (set-window-new-normal
3036 sub (/ (float (window-new-pixel sub)) parent-total))
3037 (set-window-new-normal
3038 window (- (window-normal-size window horizontal)
3039 (- (window-new-normal sub)
3040 (window-normal-size sub horizontal)))))
3041 (window--resize-child-windows-normal
3042 parent horizontal sub 0 trail delta))
3043 ;; Return 'normalized to notify `window--resize-siblings' that
3044 ;; normal sizes have been already set.
3045 'normalized)
3046 ;; Resize all windows proportionally.
3047 (setq sub last)
3048 (while sub
3049 (cond
3050 ((or (window--resize-child-windows-skip-p sub)
3051 ;; Ignore windows to skip and fixed-size child windows -
3052 ;; in the latter case make it a window to skip.
3053 (and (not ignore)
3054 (window-size-fixed-p sub horizontal ignore)
3055 (set-window-new-normal sub 'ignore))))
3056 ((< delta 0)
3057 ;; When shrinking store the number of lines/cols we can get
3058 ;; from this window here together with the total/normal size
3059 ;; factor.
3060 (set-window-new-normal
3062 (cons
3063 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
3064 (window-min-delta sub horizontal ignore trail t nil t)
3065 (- (/ (float (window-size sub horizontal t))
3066 parent-total)
3067 (window-normal-size sub horizontal)))))
3068 ((> delta 0)
3069 ;; When enlarging store the total/normal size factor only
3070 (set-window-new-normal
3072 (- (/ (float (window-size sub horizontal t))
3073 parent-total)
3074 (window-normal-size sub horizontal)))))
3076 (setq sub (window-left sub)))
3078 (cond
3079 ((< delta 0)
3080 ;; Shrink windows by delta.
3081 (setq best-window t)
3082 (while (and best-window (not (zerop delta)))
3083 (setq sub last)
3084 (setq best-window nil)
3085 (setq best-value most-negative-fixnum)
3086 (while sub
3087 (when (and (consp (window-new-normal sub))
3088 (not (<= (car (window-new-normal sub)) 0))
3089 (> (cdr (window-new-normal sub)) best-value))
3090 (setq best-window sub)
3091 (setq best-value (cdr (window-new-normal sub))))
3093 (setq sub (window-left sub)))
3095 (when best-window
3096 (setq best-delta (min (car (window-new-normal best-window))
3097 char-size (- delta)))
3098 (setq delta (+ delta best-delta))
3099 (set-window-new-pixel best-window (- best-delta) t)
3100 (set-window-new-normal
3101 best-window
3102 (if (= (car (window-new-normal best-window)) best-delta)
3103 'skip ; We can't shrink best-window any further.
3104 (cons (- (car (window-new-normal best-window)) best-delta)
3105 (- (/ (float (window-new-pixel best-window))
3106 parent-total)
3107 (window-normal-size best-window horizontal))))))))
3108 ((> delta 0)
3109 ;; Enlarge windows by delta.
3110 (setq best-window t)
3111 (while (and best-window (not (zerop delta)))
3112 (setq sub last)
3113 (setq best-window nil)
3114 (setq best-value most-positive-fixnum)
3115 (while sub
3116 (when (and (numberp (window-new-normal sub))
3117 (< (window-new-normal sub) best-value))
3118 (setq best-window sub)
3119 (setq best-value (window-new-normal sub)))
3121 (setq sub (window-left sub)))
3123 (when best-window
3124 (setq best-delta (min delta char-size))
3125 (setq delta (- delta best-delta))
3126 (set-window-new-pixel best-window best-delta t)
3127 (set-window-new-normal
3128 best-window
3129 (- (/ (float (window-new-pixel best-window))
3130 parent-total)
3131 (window-normal-size best-window horizontal)))))))
3133 (when best-window
3134 (setq sub last)
3135 (while sub
3136 (when (or (consp (window-new-normal sub))
3137 (numberp (window-new-normal sub)))
3138 ;; Reset new normal size fields so `window-resize-apply'
3139 ;; won't use them to apply new sizes.
3140 (set-window-new-normal sub))
3142 (unless (eq (window-new-normal sub) 'ignore)
3143 ;; Resize this window's child windows (back-engineering
3144 ;; delta from sub's old and new total sizes).
3145 (let ((delta (- (window-new-pixel sub)
3146 (window-size sub horizontal t))))
3147 (unless (and (zerop delta) (not trail))
3148 ;; For the TRAIL non-nil case we have to resize SUB
3149 ;; recursively even if it's size does not change.
3150 (window--resize-this-window
3151 sub delta horizontal ignore nil trail edge))))
3152 (setq sub (window-left sub)))))))
3154 (defun window--resize-siblings (window delta &optional horizontal ignore trail edge char-size)
3155 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
3156 Optional argument HORIZONTAL non-nil means resize other windows
3157 when WINDOW is resized horizontally by DELTA pixels. WINDOW
3158 itself is not resized by this function.
3160 The optional argument IGNORE has the same meaning as for
3161 `window-resizable'.
3163 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3164 of windows that shall be resized. If TRAIL equals `before',
3165 resize only windows on the left or above EDGE. If TRAIL equals
3166 `after', resize only windows on the right or below EDGE. Also,
3167 preferably only resize windows adjacent to EDGE."
3168 (when (window-parent window)
3169 (let* ((parent (window-parent window))
3170 (sub (window-child parent)))
3171 (if (window-combined-p sub horizontal)
3172 ;; In an iso-combination try to extract DELTA from WINDOW's
3173 ;; siblings.
3174 (let ((skip (eq trail 'after))
3175 this-delta other-delta)
3176 ;; Decide which windows shall be left alone.
3177 (while sub
3178 (cond
3179 ((eq sub window)
3180 ;; Make sure WINDOW is left alone when
3181 ;; resizing its siblings.
3182 (set-window-new-normal sub 'ignore)
3183 (setq skip (eq trail 'before)))
3184 (skip
3185 ;; Make sure this sibling is left alone when
3186 ;; resizing its siblings.
3187 (set-window-new-normal sub 'ignore))
3188 ((not (window-size-fixed-p sub horizontal ignore))
3189 ;; Set this-delta to t to signal that we found a sibling
3190 ;; of WINDOW whose size is not fixed.
3191 (setq this-delta t)))
3193 (setq sub (window-right sub)))
3195 ;; Set this-delta to what we can get from WINDOW's siblings.
3196 (if (= (- delta) (window-size window horizontal t))
3197 ;; A deletion, presumably. We must handle this case
3198 ;; specially since `window--resizable' can't be used.
3199 (if this-delta
3200 ;; There's at least one resizable sibling we can
3201 ;; give WINDOW's size to.
3202 (setq this-delta delta)
3203 ;; No resizable sibling exists.
3204 (setq this-delta 0))
3205 ;; Any other form of resizing.
3206 (setq this-delta
3207 (window--resizable
3208 window delta horizontal ignore trail t nil t)))
3210 ;; Set other-delta to what we still have to get from
3211 ;; ancestor windows of parent.
3212 (setq other-delta (- delta this-delta))
3213 (unless (zerop other-delta)
3214 ;; Unless we got everything from WINDOW's siblings, PARENT
3215 ;; must be resized by other-delta lines or columns.
3216 (set-window-new-pixel parent other-delta 'add))
3218 (if (zerop this-delta)
3219 ;; We haven't got anything from WINDOW's siblings but we
3220 ;; must update the normal sizes to respect other-delta.
3221 (window--resize-child-windows-normal
3222 parent horizontal window this-delta trail other-delta)
3223 ;; We did get something from WINDOW's siblings which means
3224 ;; we have to resize their child windows.
3225 (unless (eq (window--resize-child-windows
3226 parent (- this-delta) horizontal
3227 window ignore trail edge char-size)
3228 ;; If `window--resize-child-windows' returns
3229 ;; 'normalized, this means it has set the
3230 ;; normal sizes already.
3231 'normalized)
3232 ;; Set the normal sizes.
3233 (window--resize-child-windows-normal
3234 parent horizontal window this-delta trail other-delta))
3235 ;; Set DELTA to what we still have to get from ancestor
3236 ;; windows.
3237 (setq delta other-delta)))
3239 ;; In an ortho-combination all siblings of WINDOW must be
3240 ;; resized by DELTA.
3241 (set-window-new-pixel parent delta 'add)
3242 (while sub
3243 (unless (eq sub window)
3244 (window--resize-this-window
3245 sub delta horizontal ignore t))
3246 (setq sub (window-right sub))))
3248 (unless (zerop delta)
3249 ;; "Go up."
3250 (window--resize-siblings
3251 parent delta horizontal ignore trail edge char-size)))))
3253 (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge char-size)
3254 "Resize WINDOW vertically by DELTA pixels.
3255 Optional argument HORIZONTAL non-nil means resize WINDOW
3256 horizontally by DELTA pixels.
3258 The optional argument IGNORE has the same meaning as for
3259 `window-resizable'. Optional argument ADD non-nil means add
3260 DELTA to the new total size of WINDOW.
3262 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3263 of windows that shall be resized. If TRAIL equals `before',
3264 resize only windows on the left or above EDGE. If TRAIL equals
3265 `after', resize only windows on the right or below EDGE. Also,
3266 preferably only resize windows adjacent to EDGE.
3268 If the optional argument CHAR-SIZE is a positive integer, it specifies
3269 the number of pixels by which windows are incrementally resized.
3270 If CHAR-SIZE is nil, this means to use the value of
3271 `frame-char-height' or `frame-char-width' of WINDOW's frame.
3273 This function recursively resizes WINDOW's child windows to fit the
3274 new size. Make sure that WINDOW is `window--resizable' before
3275 calling this function. Note that this function does not resize
3276 siblings of WINDOW or WINDOW's parent window. You have to
3277 eventually call `window-resize-apply' in order to make resizing
3278 actually take effect."
3279 (when add
3280 ;; Add DELTA to the new total size of WINDOW.
3281 (set-window-new-pixel window delta t))
3283 (let ((sub (window-child window)))
3284 (cond
3285 ((not sub))
3286 ((window-combined-p sub horizontal)
3287 ;; In an iso-combination resize child windows according to their
3288 ;; normal sizes.
3289 (window--resize-child-windows
3290 window delta horizontal nil ignore trail edge char-size))
3291 ;; In an ortho-combination resize each child window by DELTA.
3293 (while sub
3294 (window--resize-this-window
3295 sub delta horizontal ignore t trail edge char-size)
3296 (setq sub (window-right sub)))))))
3298 (defun window--resize-root-window (window delta horizontal ignore pixelwise)
3299 "Resize root window WINDOW vertically by DELTA lines.
3300 HORIZONTAL non-nil means resize root window WINDOW horizontally
3301 by DELTA columns.
3303 IGNORE non-nil means ignore any restrictions imposed by fixed
3304 size windows, `window-min-height' or `window-min-width' settings.
3306 This function is only called by the frame resizing routines. It
3307 resizes windows proportionally and never deletes any windows."
3308 (when (and (windowp window) (numberp delta))
3309 (let ((pixel-delta
3310 (if pixelwise
3311 delta
3312 (window--size-to-pixel window delta horizontal))))
3313 (when (window-sizable-p window pixel-delta horizontal ignore t)
3314 (window--resize-reset (window-frame window) horizontal)
3315 (window--resize-this-window
3316 window pixel-delta horizontal ignore t)))))
3318 (defun window--resize-root-window-vertically (window delta pixelwise)
3319 "Resize root window WINDOW vertically by DELTA lines.
3320 If DELTA is less than zero and we can't shrink WINDOW by DELTA
3321 lines, shrink it as much as possible. If DELTA is greater than
3322 zero, this function can resize fixed-size windows in order to
3323 recover the necessary lines. Return the number of lines that
3324 were recovered.
3326 Third argument PIXELWISE non-nil means to interpret DELTA as
3327 pixels and return the number of pixels that were recovered.
3329 This function is called by the minibuffer window resizing
3330 routines."
3331 (let* ((frame (window-frame window))
3332 (pixel-delta
3333 (cond
3334 (pixelwise
3335 delta)
3336 ((numberp delta)
3337 (* (frame-char-height frame) delta))
3338 (t 0)))
3339 ignore)
3340 (cond
3341 ((zerop pixel-delta))
3342 ((< pixel-delta 0)
3343 (setq pixel-delta (window-sizable window pixel-delta nil nil pixelwise))
3344 (window--resize-reset frame)
3345 ;; When shrinking the root window, emulate an edge drag in order
3346 ;; to not resize other windows if we can avoid it (Bug#12419).
3347 (window--resize-this-window
3348 window pixel-delta nil ignore t 'before
3349 (+ (window-pixel-top window) (window-pixel-height window)))
3350 ;; Don't record new normal sizes to make sure that shrinking back
3351 ;; proportionally works as intended.
3352 (walk-window-tree
3353 (lambda (window) (set-window-new-normal window 'ignore)) frame t))
3354 ((> pixel-delta 0)
3355 (window--resize-reset frame)
3356 (unless (window-sizable window pixel-delta nil nil pixelwise)
3357 (setq ignore t))
3358 ;; When growing the root window, resize proportionally. This
3359 ;; should give windows back their original sizes (hopefully).
3360 (window--resize-this-window
3361 window pixel-delta nil ignore t)))
3362 ;; Return the possibly adjusted DELTA.
3363 (if pixelwise
3364 pixel-delta
3365 (/ pixel-delta (frame-char-height frame)))))
3367 (defun window--sanitize-window-sizes (horizontal)
3368 "Assert that all windows on selected frame are large enough.
3369 If necessary and possible, make sure that every window on frame
3370 FRAME has its minimum height. Optional argument HORIZONTAL
3371 non-nil means to make sure that every window on frame FRAME has
3372 its minimum width. The minimum height/width of a window is the
3373 respective value returned by `window-min-size' for that window.
3375 Return t if all windows were resized appropriately. Return nil
3376 if at least one window could not be resized as requested, which
3377 may happen when the FRAME is not large enough to accommodate it."
3378 (let ((value t))
3379 (walk-window-tree
3380 (lambda (window)
3381 (let ((delta (- (window-min-size window horizontal nil t)
3382 (window-size window horizontal t))))
3383 (when (> delta 0)
3384 (if (window-resizable-p window delta horizontal nil t)
3385 (window-resize window delta horizontal nil t)
3386 (setq value nil))))))
3387 value))
3389 (defun adjust-window-trailing-edge (window delta &optional horizontal pixelwise)
3390 "Move WINDOW's bottom edge by DELTA lines.
3391 Optional argument HORIZONTAL non-nil means move WINDOW's right
3392 edge by DELTA columns. WINDOW must be a valid window and
3393 defaults to the selected one.
3395 Optional argument PIXELWISE non-nil means interpret DELTA as
3396 number of pixels.
3398 If DELTA is greater than zero, move the edge downwards or to the
3399 right. If DELTA is less than zero, move the edge upwards or to
3400 the left. If the edge can't be moved by DELTA lines or columns,
3401 move it as far as possible in the desired direction."
3402 (setq window (window-normalize-window window))
3403 (let* ((frame (window-frame window))
3404 (minibuffer-window (minibuffer-window frame))
3405 (right window)
3406 left first-left first-right this-delta min-delta max-delta ignore)
3408 (unless pixelwise
3409 (setq pixelwise t)
3410 (setq delta (* delta (frame-char-size window horizontal))))
3412 ;; Find the edge we want to move.
3413 (while (and (or (not (window-combined-p right horizontal))
3414 (not (window-right right)))
3415 (setq right (window-parent right))))
3416 (cond
3417 ((and (not right) (not horizontal)
3418 ;; Resize the minibuffer window if it's on the same frame as
3419 ;; and immediately below WINDOW and it's either active or
3420 ;; `resize-mini-windows' is nil.
3421 (eq (window-frame minibuffer-window) frame)
3422 (= (nth 1 (window-pixel-edges minibuffer-window))
3423 (nth 3 (window-pixel-edges window)))
3424 (or (not resize-mini-windows)
3425 (eq minibuffer-window (active-minibuffer-window))))
3426 (window--resize-mini-window minibuffer-window (- delta)))
3427 ((or (not (setq left right)) (not (setq right (window-right right))))
3428 (if horizontal
3429 (user-error "No window on the right of this one")
3430 (user-error "No window below this one")))
3432 ;; Set LEFT to the first resizable window on the left. This step is
3433 ;; needed to handle fixed-size windows.
3434 (setq first-left left)
3435 (while (and left
3436 (or (window-size-fixed-p left horizontal)
3437 (and (< delta 0)
3438 (<= (window-size left horizontal t)
3439 (window-min-size left horizontal nil t)))))
3440 (setq left
3441 (or (window-left left)
3442 (progn
3443 (while (and (setq left (window-parent left))
3444 (not (window-combined-p left horizontal))))
3445 (window-left left)))))
3446 (unless left
3447 ;; We have to resize a size-preserved window. Start again with
3448 ;; the window initially on the left.
3449 (setq ignore 'preserved)
3450 (setq left first-left)
3451 (while (and left
3452 (or (window-size-fixed-p left horizontal 'preserved)
3453 (and (< delta 0)
3454 (<= (window-size left horizontal t)
3455 (window-min-size
3456 left horizontal 'preserved t)))))
3457 (setq left
3458 (or (window-left left)
3459 (progn
3460 (while (and (setq left (window-parent left))
3461 (not (window-combined-p left horizontal))))
3462 (window-left left)))))
3464 (unless left
3465 (if horizontal
3466 (user-error "No resizable window on the left of this one")
3467 (user-error "No resizable window above this one"))))
3469 ;; Set RIGHT to the first resizable window on the right. This step
3470 ;; is needed to handle fixed-size windows.
3471 (setq first-right right)
3472 (while (and right
3473 (or (window-size-fixed-p right horizontal)
3474 (and (> delta 0)
3475 (<= (window-size right horizontal t)
3476 (window-min-size
3477 right horizontal 'preserved t)))))
3478 (setq right
3479 (or (window-right right)
3480 (progn
3481 (while (and (setq right (window-parent right))
3482 (not (window-combined-p right horizontal))))
3483 (window-right right)))))
3484 (unless right
3485 ;; We have to resize a size-preserved window. Start again with
3486 ;; the window initially on the right.
3487 (setq ignore 'preserved)
3488 (setq right first-right)
3489 (while (and right
3490 (or (window-size-fixed-p right horizontal 'preserved)
3491 (and (> delta 0)
3492 (<= (window-size right horizontal t)
3493 (window-min-size
3494 right horizontal 'preserved t)))))
3495 (setq right
3496 (or (window-right right)
3497 (progn
3498 (while (and (setq right (window-parent right))
3499 (not (window-combined-p right horizontal))))
3500 (window-right right)))))
3501 (unless right
3502 (if horizontal
3503 (user-error "No resizable window on the right of this one")
3504 (user-error "No resizable window below this one"))))
3506 ;; LEFT and RIGHT (which might be both internal windows) are now the
3507 ;; two windows we want to resize.
3508 (cond
3509 ((> delta 0)
3510 (setq max-delta
3511 (window--max-delta-1
3512 left 0 horizontal ignore 'after nil pixelwise))
3513 (setq min-delta
3514 (window--min-delta-1
3515 right (- delta) horizontal ignore 'before nil pixelwise))
3516 (when (or (< max-delta delta) (> min-delta (- delta)))
3517 ;; We can't get the whole DELTA - move as far as possible.
3518 (setq delta (min max-delta (- min-delta))))
3519 (unless (zerop delta)
3520 ;; Start resizing.
3521 (window--resize-reset frame horizontal)
3522 ;; Try to enlarge LEFT first.
3523 (setq this-delta
3524 (window--resizable
3525 left delta horizontal ignore 'after nil nil pixelwise))
3526 (unless (zerop this-delta)
3527 (window--resize-this-window
3528 left this-delta horizontal ignore t 'before
3529 (if horizontal
3530 (+ (window-pixel-left left) (window-pixel-width left))
3531 (+ (window-pixel-top left) (window-pixel-height left)))))
3532 ;; Shrink windows on right of LEFT.
3533 (window--resize-siblings
3534 left delta horizontal ignore 'after
3535 (if horizontal
3536 (window-pixel-left right)
3537 (window-pixel-top right)))))
3538 ((< delta 0)
3539 (setq max-delta
3540 (window--max-delta-1
3541 right 0 horizontal ignore 'before nil pixelwise))
3542 (setq min-delta
3543 (window--min-delta-1
3544 left delta horizontal ignore 'after nil pixelwise))
3545 (when (or (< max-delta (- delta)) (> min-delta delta))
3546 ;; We can't get the whole DELTA - move as far as possible.
3547 (setq delta (max (- max-delta) min-delta)))
3548 (unless (zerop delta)
3549 ;; Start resizing.
3550 (window--resize-reset frame horizontal)
3551 ;; Try to enlarge RIGHT.
3552 (setq this-delta
3553 (window--resizable
3554 right (- delta) horizontal ignore 'before nil nil pixelwise))
3555 (unless (zerop this-delta)
3556 (window--resize-this-window
3557 right this-delta horizontal ignore t 'after
3558 (if horizontal
3559 (window-pixel-left right)
3560 (window-pixel-top right))))
3561 ;; Shrink windows on left of RIGHT.
3562 (window--resize-siblings
3563 right (- delta) horizontal ignore 'before
3564 (if horizontal
3565 (+ (window-pixel-left left) (window-pixel-width left))
3566 (+ (window-pixel-top left) (window-pixel-height left)))))))
3567 (unless (zerop delta)
3568 ;; Don't report an error in the standard case.
3569 (when (window--resize-apply-p frame horizontal)
3570 (if (window-resize-apply frame horizontal)
3571 (progn
3572 (window--pixel-to-total frame horizontal)
3573 (run-window-configuration-change-hook frame))
3574 ;; But do report an error if applying the changes fails.
3575 (error "Failed adjusting window %s" window))))))))
3577 (defun enlarge-window (delta &optional horizontal)
3578 "Make the selected window DELTA lines taller.
3579 Interactively, if no argument is given, make the selected window
3580 one line taller. If optional argument HORIZONTAL is non-nil,
3581 make selected window wider by DELTA columns. If DELTA is
3582 negative, shrink selected window by -DELTA lines or columns."
3583 (interactive "p")
3584 (let ((minibuffer-window (minibuffer-window)))
3585 (when (window-preserved-size nil horizontal)
3586 (window-preserve-size nil horizontal))
3587 (cond
3588 ((zerop delta))
3589 ((window-size-fixed-p nil horizontal)
3590 (user-error "Selected window has fixed size"))
3591 ((window-minibuffer-p)
3592 (if horizontal
3593 (user-error "Cannot resize minibuffer window horizontally")
3594 (window--resize-mini-window
3595 (selected-window) (* delta (frame-char-height)))))
3596 ((and (not horizontal)
3597 (window-full-height-p)
3598 (eq (window-frame minibuffer-window) (selected-frame))
3599 (not resize-mini-windows))
3600 ;; If the selected window is full height and `resize-mini-windows'
3601 ;; is nil, resize the minibuffer window.
3602 (window--resize-mini-window
3603 minibuffer-window (* (- delta) (frame-char-height))))
3604 ((window--resizable-p nil delta horizontal)
3605 (window-resize nil delta horizontal))
3606 ((window--resizable-p nil delta horizontal 'preserved)
3607 (window-resize nil delta horizontal 'preserved))
3608 ((eq this-command
3609 (if horizontal 'enlarge-window-horizontally 'enlarge-window))
3610 ;; For backward compatibility don't signal an error unless this
3611 ;; command is `enlarge-window(-horizontally)'.
3612 (user-error "Cannot enlarge selected window"))
3614 (window-resize
3615 nil (if (> delta 0)
3616 (window-max-delta nil horizontal)
3617 (- (window-min-delta nil horizontal)))
3618 horizontal)))))
3620 (defun shrink-window (delta &optional horizontal)
3621 "Make the selected window DELTA lines smaller.
3622 Interactively, if no argument is given, make the selected window
3623 one line smaller. If optional argument HORIZONTAL is non-nil,
3624 make selected window narrower by DELTA columns. If DELTA is
3625 negative, enlarge selected window by -DELTA lines or columns."
3626 (interactive "p")
3627 (let ((minibuffer-window (minibuffer-window)))
3628 (when (window-preserved-size nil horizontal)
3629 (window-preserve-size nil horizontal))
3630 (cond
3631 ((zerop delta))
3632 ((window-size-fixed-p nil horizontal)
3633 (user-error "Selected window has fixed size"))
3634 ((window-minibuffer-p)
3635 (if horizontal
3636 (user-error "Cannot resize minibuffer window horizontally")
3637 (window--resize-mini-window
3638 (selected-window) (* (- delta) (frame-char-height)))))
3639 ((and (not horizontal)
3640 (window-full-height-p)
3641 (eq (window-frame minibuffer-window) (selected-frame))
3642 (not resize-mini-windows))
3643 ;; If the selected window is full height and `resize-mini-windows'
3644 ;; is nil, resize the minibuffer window.
3645 (window--resize-mini-window
3646 minibuffer-window (* delta (frame-char-height))))
3647 ((window--resizable-p nil (- delta) horizontal)
3648 (window-resize nil (- delta) horizontal))
3649 ((window--resizable-p nil (- delta) horizontal 'preserved)
3650 (window-resize nil (- delta) horizontal 'preserved))
3651 ((eq this-command
3652 (if horizontal 'shrink-window-horizontally 'shrink-window))
3653 ;; For backward compatibility don't signal an error unless this
3654 ;; command is `shrink-window(-horizontally)'.
3655 (user-error "Cannot shrink selected window"))
3657 (window-resize
3658 nil (if (> delta 0)
3659 (- (window-min-delta nil horizontal))
3660 (window-max-delta nil horizontal))
3661 horizontal)))))
3663 (defun maximize-window (&optional window)
3664 "Maximize WINDOW.
3665 Make WINDOW as large as possible without deleting any windows.
3666 WINDOW must be a valid window and defaults to the selected one.
3668 If the option `window-resize-pixelwise' is non-nil maximize
3669 WINDOW pixelwise."
3670 (interactive)
3671 (setq window (window-normalize-window window))
3672 (window-resize
3673 window (window-max-delta window nil nil nil nil nil window-resize-pixelwise)
3674 nil nil window-resize-pixelwise)
3675 (window-resize
3676 window (window-max-delta window t nil nil nil nil window-resize-pixelwise)
3677 t nil window-resize-pixelwise))
3679 (defun minimize-window (&optional window)
3680 "Minimize WINDOW.
3681 Make WINDOW as small as possible without deleting any windows.
3682 WINDOW must be a valid window and defaults to the selected one.
3684 If the option `window-resize-pixelwise' is non-nil minimize
3685 WINDOW pixelwise."
3686 (interactive)
3687 (setq window (window-normalize-window window))
3688 (window-resize
3689 window
3690 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise))
3691 nil nil window-resize-pixelwise)
3692 (window-resize
3693 window
3694 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise))
3695 t nil window-resize-pixelwise))
3697 ;;; Window edges
3698 (defun window-edges (&optional window body absolute pixelwise)
3699 "Return a list of the edge distances of WINDOW.
3700 WINDOW must be a valid window and defaults to the selected one.
3701 The list returned has the form (LEFT TOP RIGHT BOTTOM).
3703 If the optional argument BODY is nil, this means to return the
3704 edges corresponding to the total size of WINDOW. BODY non-nil
3705 means to return the edges of WINDOW's body (aka text area). If
3706 BODY is non-nil, WINDOW must specify a live window.
3708 Optional argument ABSOLUTE nil means to return edges relative to
3709 the position of WINDOW's native frame. ABSOLUTE non-nil means to
3710 return coordinates relative to the origin - the position (0, 0) -
3711 of FRAME's display. On non-graphical systems this argument has
3712 no effect.
3714 Optional argument PIXELWISE nil means to return the coordinates
3715 in terms of the canonical character width and height of WINDOW's
3716 frame, rounded if necessary. PIXELWISE non-nil means to return
3717 the coordinates in pixels where the values for RIGHT and BOTTOM
3718 are one more than the actual value of these edges. Note that if
3719 ABSOLUTE is non-nil, PIXELWISE is implicitly non-nil too."
3720 (let* ((window (window-normalize-window window body))
3721 (frame (window-frame window))
3722 (border-width (frame-internal-border-width frame))
3723 (char-width (frame-char-width frame))
3724 (char-height (frame-char-height frame))
3725 (left (if pixelwise
3726 (+ (window-pixel-left window) border-width)
3727 (+ (window-left-column window)
3728 (/ border-width char-width))))
3729 (left-body
3730 (when body
3731 (+ (window-pixel-left window) border-width
3732 (if (eq (car (window-current-scroll-bars window)) 'left)
3733 (window-scroll-bar-width window)
3735 (nth 0 (window-fringes window))
3736 (* (or (nth 0 (window-margins window)) 0) char-width))))
3737 (top (if pixelwise
3738 (+ (window-pixel-top window) border-width)
3739 (+ (window-top-line window)
3740 (/ border-width char-height))))
3741 (top-body
3742 (when body
3743 (+ (window-pixel-top window) border-width
3744 (window-header-line-height window))))
3745 (right (+ left (if pixelwise
3746 (window-pixel-width window)
3747 (window-total-width window))))
3748 (right-body (and body (+ left-body (window-body-width window t))))
3749 (bottom (+ top (if pixelwise
3750 (window-pixel-height window)
3751 (window-total-height window))))
3752 (bottom-body (and body (+ top-body (window-body-height window t)))))
3753 (if absolute
3754 (let* ((native-edges (frame-edges frame 'native-edges))
3755 (left-off (nth 0 native-edges))
3756 (top-off (nth 1 native-edges)))
3757 (if body
3758 (list (+ left-body left-off) (+ top-body top-off)
3759 (+ right-body left-off) (+ bottom-body top-off))
3760 (list (+ left left-off) (+ top top-off)
3761 (+ right left-off) (+ bottom top-off))))
3762 (if body
3763 (if pixelwise
3764 (list left-body top-body right-body bottom-body)
3765 (list (/ left-body char-width) (/ top-body char-height)
3766 ;; Round up.
3767 (/ (+ right-body char-width -1) char-width)
3768 (/ (+ bottom-body char-height -1) char-height)))
3769 (list left top right bottom)))))
3771 (defun window-body-edges (&optional window)
3772 "Return a list of the edge coordinates of WINDOW's body.
3773 The return value is that of `window-edges' called with argument
3774 BODY non-nil."
3775 (window-edges window t))
3776 (defalias 'window-inside-edges 'window-body-edges)
3778 (defun window-pixel-edges (&optional window)
3779 "Return a list of the edge pixel coordinates of WINDOW.
3780 The return value is that of `window-edges' called with argument
3781 PIXELWISE non-nil."
3782 (window-edges window nil nil t))
3784 (defun window-body-pixel-edges (&optional window)
3785 "Return a list of the edge pixel coordinates of WINDOW's body.
3786 The return value is that of `window-edges' called with arguments
3787 BODY and PIXELWISE non-nil."
3788 (window-edges window t nil t))
3789 (defalias 'window-inside-pixel-edges 'window-body-pixel-edges)
3791 (defun window-absolute-pixel-edges (&optional window)
3792 "Return a list of the edge pixel coordinates of WINDOW.
3793 The return value is that of `window-edges' called with argument
3794 ABSOLUTE non-nil."
3795 (window-edges window nil t t))
3797 (defun window-absolute-body-pixel-edges (&optional window)
3798 "Return a list of the edge pixel coordinates of WINDOW's text area.
3799 The return value is that of `window-edges' called with arguments
3800 BODY and ABSOLUTE non-nil."
3801 (window-edges window t t t))
3802 (defalias 'window-inside-absolute-pixel-edges 'window-absolute-body-pixel-edges)
3804 (defun window-absolute-pixel-position (&optional position window)
3805 "Return display coordinates of POSITION in WINDOW.
3806 If the buffer position POSITION is visible in window WINDOW,
3807 return the display coordinates of the upper/left corner of the
3808 glyph at POSITION. The return value is a cons of the X- and
3809 Y-coordinates of that corner, relative to an origin at (0, 0) of
3810 WINDOW's display. Return nil if POSITION is not visible in
3811 WINDOW.
3813 WINDOW must be a live window and defaults to the selected window.
3814 POSITION defaults to the value of `window-point' of WINDOW."
3815 (let* ((window (window-normalize-window window t))
3816 (pos-in-window
3817 (pos-visible-in-window-p
3818 (or position (window-point window)) window t)))
3819 (when pos-in-window
3820 (let ((edges (window-absolute-body-pixel-edges window)))
3821 (cons (+ (nth 0 edges) (nth 0 pos-in-window))
3822 (+ (nth 1 edges) (nth 1 pos-in-window)))))))
3824 (defun frame-root-window-p (window)
3825 "Return non-nil if WINDOW is the root window of its frame."
3826 (eq window (frame-root-window window)))
3828 (defun window--subtree (window &optional next)
3829 "Return window subtree rooted at WINDOW.
3830 Optional argument NEXT non-nil means include WINDOW's right
3831 siblings in the return value.
3833 See the documentation of `window-tree' for a description of the
3834 return value."
3835 (let (list)
3836 (while window
3837 (setq list
3838 (cons
3839 (cond
3840 ((window-top-child window)
3841 (cons t (cons (window-edges window)
3842 (window--subtree (window-top-child window) t))))
3843 ((window-left-child window)
3844 (cons nil (cons (window-edges window)
3845 (window--subtree (window-left-child window) t))))
3846 (t window))
3847 list))
3848 (setq window (when next (window-next-sibling window))))
3849 (nreverse list)))
3851 (defun window-tree (&optional frame)
3852 "Return the window tree of frame FRAME.
3853 FRAME must be a live frame and defaults to the selected frame.
3854 The return value is a list of the form (ROOT MINI), where ROOT
3855 represents the window tree of the frame's root window, and MINI
3856 is the frame's minibuffer window.
3858 If the root window is not split, ROOT is the root window itself.
3859 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3860 for a horizontal split, and t for a vertical split. EDGES gives
3861 the combined size and position of the child windows in the split,
3862 and the rest of the elements are the child windows in the split.
3863 Each of the child windows may again be a window or a list
3864 representing a window split, and so on. EDGES is a list (LEFT
3865 TOP RIGHT BOTTOM) as returned by `window-edges'."
3866 (setq frame (window-normalize-frame frame))
3867 (window--subtree (frame-root-window frame) t))
3869 (defun other-window (count &optional all-frames)
3870 "Select another window in cyclic ordering of windows.
3871 COUNT specifies the number of windows to skip, starting with the
3872 selected window, before making the selection. If COUNT is
3873 positive, skip COUNT windows forwards. If COUNT is negative,
3874 skip -COUNT windows backwards. COUNT zero means do not skip any
3875 window, so select the selected window. In an interactive call,
3876 COUNT is the numeric prefix argument. Return nil.
3878 If the `other-window' parameter of the selected window is a
3879 function and `ignore-window-parameters' is nil, call that
3880 function with the arguments COUNT and ALL-FRAMES.
3882 This function does not select a window whose `no-other-window'
3883 window parameter is non-nil.
3885 This function uses `next-window' for finding the window to
3886 select. The argument ALL-FRAMES has the same meaning as in
3887 `next-window', but the MINIBUF argument of `next-window' is
3888 always effectively nil."
3889 (interactive "p")
3890 (let* ((window (selected-window))
3891 (function (and (not ignore-window-parameters)
3892 (window-parameter window 'other-window)))
3893 old-window old-count)
3894 (if (functionp function)
3895 (funcall function count all-frames)
3896 ;; `next-window' and `previous-window' may return a window we are
3897 ;; not allowed to select. Hence we need an exit strategy in case
3898 ;; all windows are non-selectable.
3899 (catch 'exit
3900 (while (> count 0)
3901 (setq window (next-window window nil all-frames))
3902 (cond
3903 ((eq window old-window)
3904 (when (= count old-count)
3905 ;; Keep out of infinite loops. When COUNT has not changed
3906 ;; since we last looked at `window' we're probably in one.
3907 (throw 'exit nil)))
3908 ((window-parameter window 'no-other-window)
3909 (unless old-window
3910 ;; The first non-selectable window `next-window' got us:
3911 ;; Remember it and the current value of COUNT.
3912 (setq old-window window)
3913 (setq old-count count)))
3915 (setq count (1- count)))))
3916 (while (< count 0)
3917 (setq window (previous-window window nil all-frames))
3918 (cond
3919 ((eq window old-window)
3920 (when (= count old-count)
3921 ;; Keep out of infinite loops. When COUNT has not changed
3922 ;; since we last looked at `window' we're probably in one.
3923 (throw 'exit nil)))
3924 ((window-parameter window 'no-other-window)
3925 (unless old-window
3926 ;; The first non-selectable window `previous-window' got
3927 ;; us: Remember it and the current value of COUNT.
3928 (setq old-window window)
3929 (setq old-count count)))
3931 (setq count (1+ count)))))
3933 (select-window window)
3934 ;; Always return nil.
3935 nil))))
3937 ;; This should probably return non-nil when the selected window is part
3938 ;; of an atomic window whose root is the frame's root window.
3939 (defun one-window-p (&optional nomini all-frames)
3940 "Return non-nil if the selected window is the only window.
3941 Optional arg NOMINI non-nil means don't count the minibuffer
3942 even if it is active. Otherwise, the minibuffer is counted
3943 when it is active.
3945 Optional argument ALL-FRAMES specifies the set of frames to
3946 consider, see also `next-window'. ALL-FRAMES nil or omitted
3947 means consider windows on the selected frame only, plus the
3948 minibuffer window if specified by the NOMINI argument. If the
3949 minibuffer counts, consider all windows on all frames that share
3950 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3951 with a special meaning are:
3953 - t means consider all windows on all existing frames.
3955 - `visible' means consider all windows on all visible frames on
3956 the current terminal.
3958 - 0 (the number zero) means consider all windows on all visible
3959 and iconified frames on the current terminal.
3961 - A frame means consider all windows on that frame only.
3963 Anything else means consider all windows on the selected frame
3964 and no others."
3965 (let ((base-window (selected-window)))
3966 (if (and nomini (eq base-window (minibuffer-window)))
3967 (setq base-window (next-window base-window)))
3968 (eq base-window
3969 (next-window base-window (if nomini 'arg) all-frames))))
3971 ;;; Deleting windows.
3972 (defun window-deletable-p (&optional window)
3973 "Return t if WINDOW can be safely deleted from its frame.
3974 WINDOW must be a valid window and defaults to the selected one.
3976 Return `frame' if WINDOW is the root window of its frame and that
3977 frame can be safely deleted."
3978 (setq window (window-normalize-window window))
3980 (unless (or ignore-window-parameters
3981 (eq (window-parameter window 'delete-window) t))
3982 ;; Handle atomicity.
3983 (when (window-parameter window 'window-atom)
3984 (setq window (window-atom-root window))))
3986 (let ((frame (window-frame window)))
3987 (cond
3988 ((frame-root-window-p window)
3989 ;; WINDOW's frame can be deleted only if there are other frames
3990 ;; on the same terminal, and it does not contain the active
3991 ;; minibuffer.
3992 (unless (or (eq frame (next-frame frame 0))
3993 ;; We can delete our frame only if no other frame
3994 ;; currently uses our minibuffer window.
3995 (catch 'other
3996 (dolist (other (frame-list))
3997 (when (and (not (eq other frame))
3998 (eq (window-frame (minibuffer-window other))
3999 frame))
4000 (throw 'other t))))
4001 (let ((minibuf (active-minibuffer-window)))
4002 (and minibuf (eq frame (window-frame minibuf)))))
4003 'frame))
4004 ((window-minibuffer-p window)
4005 ;; If WINDOW is the minibuffer window of a non-minibuffer-only
4006 ;; frame, it cannot be deleted separately.
4007 nil)
4008 ((or ignore-window-parameters
4009 (not (eq window (window-main-window frame))))
4010 ;; Otherwise, WINDOW can be deleted unless it is the main window
4011 ;; of its frame.
4012 t))))
4014 (defun window--in-subtree-p (window root)
4015 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
4016 (or (eq window root)
4017 (let ((parent (window-parent window)))
4018 (catch 'done
4019 (while parent
4020 (if (eq parent root)
4021 (throw 'done t)
4022 (setq parent (window-parent parent))))))))
4024 (defun delete-window (&optional window)
4025 "Delete WINDOW.
4026 WINDOW must be a valid window and defaults to the selected one.
4027 Return nil.
4029 If the variable `ignore-window-parameters' is non-nil or the
4030 `delete-window' parameter of WINDOW equals t, do not process any
4031 parameters of WINDOW. Otherwise, if the `delete-window'
4032 parameter of WINDOW specifies a function, call that function with
4033 WINDOW as its sole argument and return the value returned by that
4034 function.
4036 Otherwise, if WINDOW is part of an atomic window, call
4037 `delete-window' with the root of the atomic window as its
4038 argument. Signal an error if WINDOW is either the only window on
4039 its frame, the last non-side window, or part of an atomic window
4040 that is its frame's root window."
4041 (interactive)
4042 (setq window (window-normalize-window window))
4043 (let* ((frame (window-frame window))
4044 (function (window-parameter window 'delete-window))
4045 (parent (window-parent window))
4046 atom-root)
4047 (window--check frame)
4048 (catch 'done
4049 ;; Handle window parameters.
4050 (cond
4051 ;; Ignore window parameters if `ignore-window-parameters' tells
4052 ;; us so or `delete-window' equals t.
4053 ((or ignore-window-parameters (eq function t)))
4054 ((functionp function)
4055 ;; The `delete-window' parameter specifies the function to call.
4056 ;; If that function is `ignore' nothing is done. It's up to the
4057 ;; function called here to avoid infinite recursion.
4058 (throw 'done (funcall function window)))
4059 ((and (window-parameter window 'window-atom)
4060 (setq atom-root (window-atom-root window))
4061 (not (eq atom-root window)))
4062 (if (eq atom-root (frame-root-window frame))
4063 (error "Root of atomic window is root window of its frame")
4064 (throw 'done (delete-window atom-root))))
4065 ((not parent)
4066 (error "Attempt to delete minibuffer or sole ordinary window"))
4067 ((eq window (window-main-window frame))
4068 (error "Attempt to delete main window of frame %s" frame)))
4070 (let* ((horizontal (window-left-child parent))
4071 (size (window-size window horizontal t))
4072 (window-combination-resize
4073 (or window-combination-resize
4074 (window-parameter parent 'window-side)))
4075 (frame-selected
4076 (window--in-subtree-p (frame-selected-window frame) window))
4077 ;; Emacs 23 preferably gives WINDOW's space to its left
4078 ;; sibling.
4079 (sibling (or (window-left window) (window-right window))))
4080 (window--resize-reset frame horizontal)
4081 (cond
4082 ((and (not (eq window-combination-resize t))
4083 sibling (window-sizable-p sibling size horizontal nil t))
4084 ;; Resize WINDOW's sibling.
4085 (window--resize-this-window sibling size horizontal nil t)
4086 (set-window-new-normal
4087 sibling (+ (window-normal-size sibling horizontal)
4088 (window-normal-size window horizontal))))
4089 ((window--resizable-p window (- size) horizontal nil nil nil t t)
4090 ;; Can do without resizing fixed-size windows.
4091 (window--resize-siblings window (- size) horizontal))
4093 ;; Can't do without resizing fixed-size windows.
4094 (window--resize-siblings window (- size) horizontal t)))
4095 ;; Actually delete WINDOW.
4096 (delete-window-internal window)
4097 (window--pixel-to-total frame horizontal)
4098 (when (and frame-selected
4099 (window-parameter
4100 (frame-selected-window frame) 'no-other-window))
4101 ;; `delete-window-internal' has selected a window that should
4102 ;; not be selected, fix this here.
4103 (other-window -1 frame))
4104 (run-window-configuration-change-hook frame)
4105 (window--check frame)
4106 ;; Always return nil.
4107 nil))))
4109 (defun delete-other-windows (&optional window)
4110 "Make WINDOW fill its frame.
4111 WINDOW must be a valid window and defaults to the selected one.
4112 Return nil.
4114 If the variable `ignore-window-parameters' is non-nil or the
4115 `delete-other-windows' parameter of WINDOW equals t, do not pay
4116 attention to any other parameters of WINDOW. Otherwise, if the
4117 `delete-other-windows' parameter of WINDOW specifies a function,
4118 call that function with WINDOW as its sole argument and return
4119 the value returned by that function.
4121 Else, if WINDOW is part of an atomic window, call this function
4122 with the root of the atomic window as its argument. Signal an
4123 error if that root window is the root window of WINDOW's frame.
4124 Also signal an error if WINDOW is a side window. Do not delete
4125 any window whose `no-delete-other-windows' parameter is non-nil."
4126 (interactive)
4127 (setq window (window-normalize-window window))
4128 (let* ((frame (window-frame window))
4129 (function (window-parameter window 'delete-other-windows))
4130 atom-root main)
4131 (window--check frame)
4132 (catch 'done
4133 (cond
4134 ;; Ignore window parameters if `ignore-window-parameters' is t or
4135 ;; `delete-other-windows' is t.
4136 ((or ignore-window-parameters (eq function t)))
4137 ((functionp function)
4138 ;; The `delete-other-windows' parameter specifies the function
4139 ;; to call. If the function is `ignore' no windows are deleted.
4140 ;; It's up to the function called to avoid infinite recursion.
4141 (throw 'done (funcall function window)))
4142 ((and (window-parameter window 'window-atom)
4143 (setq atom-root (window-atom-root window))
4144 (not (eq atom-root window)))
4145 (if (eq atom-root (frame-root-window frame))
4146 (error "Root of atomic window is root window of its frame")
4147 (throw 'done (delete-other-windows atom-root))))
4148 ((window-parameter window 'window-side)
4149 (error "Cannot make side window the only window"))
4150 ((and (window-minibuffer-p window)
4151 (not (eq window (frame-root-window window))))
4152 (error "Can't expand minibuffer to full frame")))
4154 (cond
4155 ((or ignore-window-parameters
4156 (not (window-with-parameter 'no-delete-other-windows nil frame)))
4157 (setq main (frame-root-window frame)))
4158 ((catch 'tag
4159 (walk-window-tree
4160 (lambda (other)
4161 (when (or (and (window-parameter other 'window-side)
4162 (not (window-parameter
4163 other 'no-delete-other-windows)))
4164 (and (not (window-parameter other 'window-side))
4165 (window-parameter
4166 other 'no-delete-other-windows)))
4167 (throw 'tag nil))))
4169 (setq main (window-main-window frame)))
4171 ;; Delete windows via `delete-window' because we found either a
4172 ;; deletable side window or a non-deletable non-side-window.
4173 (dolist (other (window-list frame))
4174 (when (and (window-live-p other)
4175 (not (eq other window))
4176 (not (window-parameter
4177 other 'no-delete-other-windows))
4178 ;; When WINDOW and the other window are part of the
4179 ;; same atomic window, don't delete the other.
4180 (or (not atom-root)
4181 (not (eq (window-atom-root other) atom-root))))
4182 (condition-case nil
4183 (delete-window other)
4184 (error nil))))
4185 (throw 'done nil)))
4187 ;; If WINDOW is the main window of its frame do nothing.
4188 (unless (eq window main)
4189 (delete-other-windows-internal window main)
4190 (run-window-configuration-change-hook frame)
4191 (window--check frame))
4192 ;; Always return nil.
4193 nil)))
4195 (defun delete-other-windows-vertically (&optional window)
4196 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
4197 This may be a useful alternative binding for \\[delete-other-windows]
4198 if you often split windows horizontally."
4199 (interactive)
4200 (let* ((window (or window (selected-window)))
4201 (edges (window-edges window))
4202 (w window) delenda)
4203 (while (not (eq (setq w (next-window w 1)) window))
4204 (let ((e (window-edges w)))
4205 (when (and (= (car e) (car edges))
4206 (= (nth 2 e) (nth 2 edges)))
4207 (push w delenda))))
4208 (mapc 'delete-window delenda)))
4210 ;;; Windows and buffers.
4212 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
4213 ;; for (1) determining which buffer to show in the window when its
4214 ;; buffer shall be buried or killed and (2) which buffer to show for
4215 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
4217 ;; `prev-buffers' consists of <buffer, window-start, window-point>
4218 ;; triples. The entries on this list are ordered by the time their
4219 ;; buffer has been removed from the window, the most recently removed
4220 ;; buffer's entry being first. The window-start and window-point
4221 ;; components are `window-start' and `window-point' at the time the
4222 ;; buffer was removed from the window which implies that the entry must
4223 ;; be added when `set-window-buffer' removes the buffer from the window.
4225 ;; `next-buffers' is the list of buffers that have been replaced
4226 ;; recently by `switch-to-prev-buffer'. These buffers are the least
4227 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
4228 ;; candidates of `switch-to-next-buffer' to switch to. This list is
4229 ;; reset to nil by any action changing the window's buffer with the
4230 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
4231 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
4232 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
4234 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
4235 ;; if such a buffer was killed while the window was hidden within a
4236 ;; window configuration. Such killed buffers get removed whenever
4237 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
4239 ;; The following function is called by `set-window-buffer' _before_ it
4240 ;; replaces the buffer of the argument window with the new buffer.
4241 (defun record-window-buffer (&optional window)
4242 "Record WINDOW's buffer.
4243 WINDOW must be a live window and defaults to the selected one."
4244 (let* ((window (window-normalize-window window t))
4245 (buffer (window-buffer window))
4246 (entry (assq buffer (window-prev-buffers window))))
4247 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
4248 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
4249 (set-window-next-buffers window nil)
4251 (when entry
4252 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
4253 (set-window-prev-buffers
4254 window (assq-delete-all buffer (window-prev-buffers window))))
4256 ;; Don't record insignificant buffers.
4257 (unless (eq (aref (buffer-name buffer) 0) ?\s)
4258 ;; Add an entry for buffer to WINDOW's previous buffers.
4259 (with-current-buffer buffer
4260 (let ((start (window-start window))
4261 (point (window-point window)))
4262 (setq entry
4263 (cons buffer
4264 (if entry
4265 ;; We have an entry, update marker positions.
4266 (list (set-marker (nth 1 entry) start)
4267 (set-marker (nth 2 entry) point))
4268 ;; Make new markers.
4269 (list (copy-marker start)
4270 (copy-marker
4271 ;; Preserve window-point-insertion-type
4272 ;; (Bug#12588).
4273 point window-point-insertion-type)))))
4274 (set-window-prev-buffers
4275 window (cons entry (window-prev-buffers window)))))
4277 (run-hooks 'buffer-list-update-hook))))
4279 (defun unrecord-window-buffer (&optional window buffer)
4280 "Unrecord BUFFER in WINDOW.
4281 WINDOW must be a live window and defaults to the selected one.
4282 BUFFER must be a live buffer and defaults to the buffer of
4283 WINDOW."
4284 (let* ((window (window-normalize-window window t))
4285 (buffer (or buffer (window-buffer window))))
4286 (set-window-prev-buffers
4287 window (assq-delete-all buffer (window-prev-buffers window)))
4288 (set-window-next-buffers
4289 window (delq buffer (window-next-buffers window)))))
4291 (defun set-window-buffer-start-and-point (window buffer &optional start point)
4292 "Set WINDOW's buffer to BUFFER.
4293 WINDOW must be a live window and defaults to the selected one.
4294 Optional argument START non-nil means set WINDOW's start position
4295 to START. Optional argument POINT non-nil means set WINDOW's
4296 point to POINT. If WINDOW is selected this also sets BUFFER's
4297 `point' to POINT. If WINDOW is selected and the buffer it showed
4298 before was current this also makes BUFFER the current buffer."
4299 (setq window (window-normalize-window window t))
4300 (let ((selected (eq window (selected-window)))
4301 (current (eq (window-buffer window) (current-buffer))))
4302 (set-window-buffer window buffer)
4303 (when (and selected current)
4304 (set-buffer buffer))
4305 (when start
4306 ;; Don't force window-start here (even if POINT is nil).
4307 (set-window-start window start t))
4308 (when point
4309 (set-window-point window point))))
4311 (defcustom switch-to-visible-buffer t
4312 "If non-nil, allow switching to an already visible buffer.
4313 If this variable is non-nil, `switch-to-prev-buffer' and
4314 `switch-to-next-buffer' may switch to an already visible buffer.
4315 If this variable is nil, `switch-to-prev-buffer' and
4316 `switch-to-next-buffer' always try to avoid switching to a buffer
4317 that is already visible in another window on the same frame."
4318 :type 'boolean
4319 :version "24.1"
4320 :group 'windows)
4322 (defun switch-to-prev-buffer (&optional window bury-or-kill)
4323 "In WINDOW switch to previous buffer.
4324 WINDOW must be a live window and defaults to the selected one.
4325 Return the buffer switched to, nil if no suitable buffer could be
4326 found.
4328 Optional argument BURY-OR-KILL non-nil means the buffer currently
4329 shown in WINDOW is about to be buried or killed and consequently
4330 shall not be switched to in future invocations of this command.
4332 As a special case, if BURY-OR-KILL equals `append', this means to
4333 move the buffer to the end of WINDOW's previous buffers list so a
4334 future invocation of `switch-to-prev-buffer' less likely switches
4335 to it."
4336 (interactive)
4337 (let* ((window (window-normalize-window window t))
4338 (frame (window-frame window))
4339 (window-side (window-parameter window 'window-side))
4340 (old-buffer (window-buffer window))
4341 ;; Save this since it's destroyed by `set-window-buffer'.
4342 (next-buffers (window-next-buffers window))
4343 (pred (frame-parameter frame 'buffer-predicate))
4344 entry new-buffer killed-buffers visible)
4345 (when (window-minibuffer-p window)
4346 ;; Don't switch in minibuffer window.
4347 (unless (setq window (minibuffer-selected-window))
4348 (error "Window %s is a minibuffer window" window)))
4350 (unless (memq (window-dedicated-p window) '(nil side))
4351 ;; Don't switch in dedicated window.
4352 (error "Window %s is dedicated to buffer %s" window old-buffer))
4354 (catch 'found
4355 ;; Scan WINDOW's previous buffers first, skipping entries of next
4356 ;; buffers.
4357 (dolist (entry (window-prev-buffers window))
4358 (when (and (setq new-buffer (car entry))
4359 (or (buffer-live-p new-buffer)
4360 (not (setq killed-buffers
4361 (cons new-buffer killed-buffers))))
4362 (not (eq new-buffer old-buffer))
4363 (or (null pred) (funcall pred new-buffer))
4364 ;; When BURY-OR-KILL is nil, avoid switching to a
4365 ;; buffer in WINDOW's next buffers list.
4366 (or bury-or-kill (not (memq new-buffer next-buffers))))
4367 (if (and (not switch-to-visible-buffer)
4368 (get-buffer-window new-buffer frame))
4369 ;; Try to avoid showing a buffer visible in some other
4370 ;; window.
4371 (setq visible new-buffer)
4372 (set-window-buffer-start-and-point
4373 window new-buffer (nth 1 entry) (nth 2 entry))
4374 (throw 'found t))))
4375 ;; Scan reverted buffer list of WINDOW's frame next, skipping
4376 ;; entries of next buffers. Note that when we bury or kill a
4377 ;; buffer we don't reverse the global buffer list to avoid showing
4378 ;; a buried buffer instead. Otherwise, we must reverse the global
4379 ;; buffer list in order to make sure that switching to the
4380 ;; previous/next buffer traverse it in opposite directions. Skip
4381 ;; this step for side windows.
4382 (unless window-side
4383 (dolist (buffer (if bury-or-kill
4384 (buffer-list frame)
4385 (nreverse (buffer-list frame))))
4386 (when (and (buffer-live-p buffer)
4387 (not (eq buffer old-buffer))
4388 (or (null pred) (funcall pred buffer))
4389 (not (eq (aref (buffer-name buffer) 0) ?\s))
4390 ;; Don't show a buffer shown in a side window before.
4391 (not (buffer-local-value 'window--sides-shown buffer))
4392 (or bury-or-kill (not (memq buffer next-buffers))))
4393 (if (and (not switch-to-visible-buffer)
4394 (get-buffer-window buffer frame))
4395 ;; Try to avoid showing a buffer visible in some other window.
4396 (unless visible
4397 (setq visible buffer))
4398 (setq new-buffer buffer)
4399 (set-window-buffer-start-and-point window new-buffer)
4400 (throw 'found t)))))
4401 (unless bury-or-kill
4402 ;; Scan reverted next buffers last (must not use nreverse
4403 ;; here!).
4404 (dolist (buffer (reverse next-buffers))
4405 ;; Actually, buffer _must_ be live here since otherwise it
4406 ;; would have been caught in the scan of previous buffers.
4407 (when (and (or (buffer-live-p buffer)
4408 (not (setq killed-buffers
4409 (cons buffer killed-buffers))))
4410 (not (eq buffer old-buffer))
4411 (or (null pred) (funcall pred buffer))
4412 (setq entry (assq buffer (window-prev-buffers window))))
4413 (setq new-buffer buffer)
4414 (set-window-buffer-start-and-point
4415 window new-buffer (nth 1 entry) (nth 2 entry))
4416 (throw 'found t))))
4418 ;; Show a buffer visible in another window.
4419 (when visible
4420 (setq new-buffer visible)
4421 (set-window-buffer-start-and-point window new-buffer)))
4423 (if bury-or-kill
4424 (let ((entry (and (eq bury-or-kill 'append)
4425 (assq old-buffer (window-prev-buffers window)))))
4426 ;; Remove `old-buffer' from WINDOW's previous and (restored list
4427 ;; of) next buffers.
4428 (set-window-prev-buffers
4429 window (assq-delete-all old-buffer (window-prev-buffers window)))
4430 (set-window-next-buffers window (delq old-buffer next-buffers))
4431 (when entry
4432 ;; Append old-buffer's entry to list of WINDOW's previous
4433 ;; buffers so it's less likely to get switched to soon but
4434 ;; `display-buffer-in-previous-window' can nevertheless find
4435 ;; it.
4436 (set-window-prev-buffers
4437 window (append (window-prev-buffers window) (list entry)))))
4438 ;; Move `old-buffer' to head of WINDOW's restored list of next
4439 ;; buffers.
4440 (set-window-next-buffers
4441 window (cons old-buffer (delq old-buffer next-buffers))))
4443 ;; Remove killed buffers from WINDOW's previous and next buffers.
4444 (when killed-buffers
4445 (dolist (buffer killed-buffers)
4446 (set-window-prev-buffers
4447 window (assq-delete-all buffer (window-prev-buffers window)))
4448 (set-window-next-buffers
4449 window (delq buffer (window-next-buffers window)))))
4451 ;; Return new-buffer.
4452 new-buffer))
4454 (defun switch-to-next-buffer (&optional window)
4455 "In WINDOW switch to next buffer.
4456 WINDOW must be a live window and defaults to the selected one.
4457 Return the buffer switched to, nil if no suitable buffer could be
4458 found."
4459 (interactive)
4460 (let* ((window (window-normalize-window window t))
4461 (frame (window-frame window))
4462 (window-side (window-parameter window 'window-side))
4463 (old-buffer (window-buffer window))
4464 (next-buffers (window-next-buffers window))
4465 (pred (frame-parameter frame 'buffer-predicate))
4466 new-buffer entry killed-buffers visible)
4467 (when (window-minibuffer-p window)
4468 ;; Don't switch in minibuffer window.
4469 (unless (setq window (minibuffer-selected-window))
4470 (error "Window %s is a minibuffer window" window)))
4472 (unless (memq (window-dedicated-p window) '(nil side))
4473 ;; Don't switch in dedicated window.
4474 (error "Window %s is dedicated to buffer %s" window old-buffer))
4476 (catch 'found
4477 ;; Scan WINDOW's next buffers first.
4478 (dolist (buffer next-buffers)
4479 (when (and (or (buffer-live-p buffer)
4480 (not (setq killed-buffers
4481 (cons buffer killed-buffers))))
4482 (not (eq buffer old-buffer))
4483 (or (null pred) (funcall pred buffer))
4484 (setq entry (assq buffer (window-prev-buffers window))))
4485 (setq new-buffer buffer)
4486 (set-window-buffer-start-and-point
4487 window new-buffer (nth 1 entry) (nth 2 entry))
4488 (throw 'found t)))
4489 ;; Scan the buffer list of WINDOW's frame next, skipping previous
4490 ;; buffers entries. Skip this step for side windows.
4491 (unless window-side
4492 (dolist (buffer (buffer-list frame))
4493 (when (and (buffer-live-p buffer)
4494 (not (eq buffer old-buffer))
4495 (or (null pred) (funcall pred buffer))
4496 (not (eq (aref (buffer-name buffer) 0) ?\s))
4497 ;; Don't show a buffer shown in a side window before.
4498 (not (buffer-local-value 'window--sides-shown buffer))
4499 (not (assq buffer (window-prev-buffers window))))
4500 (if (and (not switch-to-visible-buffer)
4501 (get-buffer-window buffer frame))
4502 ;; Try to avoid showing a buffer visible in some other window.
4503 (setq visible buffer)
4504 (setq new-buffer buffer)
4505 (set-window-buffer-start-and-point window new-buffer)
4506 (throw 'found t)))))
4507 ;; Scan WINDOW's reverted previous buffers last (must not use
4508 ;; nreverse here!)
4509 (dolist (entry (reverse (window-prev-buffers window)))
4510 (when (and (setq new-buffer (car entry))
4511 (or (buffer-live-p new-buffer)
4512 (not (setq killed-buffers
4513 (cons new-buffer killed-buffers))))
4514 (not (eq new-buffer old-buffe