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