; Merge from origin/emacs-25
[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))
4514 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
4515 "Search LIST for a valid buffer to display in FRAME.
4516 Return nil when all buffers in LIST are undesirable for display,
4517 otherwise return the first suitable buffer in LIST.
4519 Buffers not visible in windows are preferred to visible buffers,
4520 unless VISIBLE-OK is non-nil.
4521 If the optional argument FRAME is nil, it defaults to the selected frame.
4522 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
4523 ;; This logic is more or less copied from other-buffer.
4524 (setq frame (or frame (selected-frame)))
4525 (let ((pred (frame-parameter frame 'buffer-predicate))
4526 found buf)
4527 (while (and (not found) list)
4528 (setq buf (car list))
4529 (if (and (not (eq buffer buf))
4530 (buffer-live-p buf)
4531 (or (null pred) (funcall pred buf))
4532 (not (eq (aref (buffer-name buf) 0) ?\s))
4533 (or visible-ok (null (get-buffer-window buf 'visible))))
4534 (setq found buf)
4535 (setq list (cdr list))))
4536 (car list)))
4538 (defun last-buffer (&optional buffer visible-ok frame)
4539 "Return the last buffer in FRAME's buffer list.
4540 If BUFFER is the last buffer, return the preceding buffer
4541 instead. Buffers not visible in windows are preferred to visible
4542 buffers, unless optional argument VISIBLE-OK is non-nil.
4543 Optional third argument FRAME nil or omitted means use the
4544 selected frame's buffer list. If no such buffer exists, return
4545 the buffer `*scratch*', creating it if necessary."
4546 (setq frame (or frame (selected-frame)))
4547 (or (get-next-valid-buffer (nreverse (buffer-list frame))
4548 buffer visible-ok frame)
4549 (get-buffer "*scratch*")
4550 (let ((scratch (get-buffer-create "*scratch*")))
4551 (set-buffer-major-mode scratch)
4552 scratch)))
4554 (defcustom frame-auto-hide-function #'iconify-frame
4555 "Function called to automatically hide frames.
4556 The function is called with one argument - a frame.
4558 Functions affected by this option are those that bury a buffer
4559 shown in a separate frame like `quit-window' and `bury-buffer'."
4560 :type '(choice (const :tag "Iconify" iconify-frame)
4561 (const :tag "Delete" delete-frame)
4562 (const :tag "Do nothing" ignore)
4563 function)
4564 :group 'windows
4565 :group 'frames
4566 :version "24.1")
4568 (defun window--delete (&optional window dedicated-only kill)
4569 "Delete WINDOW if possible.
4570 WINDOW must be a live window and defaults to the selected one.
4571 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
4572 only if it's dedicated to its buffer. Optional argument KILL
4573 means the buffer shown in window will be killed. Return non-nil
4574 if WINDOW gets deleted or its frame is auto-hidden."
4575 (setq window (window-normalize-window window t))
4576 (unless (and dedicated-only (not (window-dedicated-p window)))
4577 (let ((deletable (window-deletable-p window)))
4578 (cond
4579 ((eq deletable 'frame)
4580 (let ((frame (window-frame window)))
4581 (cond
4582 (kill
4583 (delete-frame frame))
4584 ((functionp frame-auto-hide-function)
4585 (funcall frame-auto-hide-function frame))))
4586 'frame)
4587 (deletable
4588 (delete-window window)
4589 t)))))
4591 (defun bury-buffer (&optional buffer-or-name)
4592 "Put BUFFER-OR-NAME at the end of the list of all buffers.
4593 There it is the least likely candidate for `other-buffer' to
4594 return; thus, the least likely buffer for \\[switch-to-buffer] to
4595 select by default.
4597 You can specify a buffer name as BUFFER-OR-NAME, or an actual
4598 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
4599 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
4600 remove the current buffer from the selected window if it is
4601 displayed there."
4602 (interactive)
4603 (let* ((buffer (window-normalize-buffer buffer-or-name)))
4604 ;; If `buffer-or-name' is not on the selected frame we unrecord it
4605 ;; although it's not "here" (call it a feature).
4606 (bury-buffer-internal buffer)
4607 ;; Handle case where `buffer-or-name' is nil and the current buffer
4608 ;; is shown in the selected window.
4609 (cond
4610 ((or buffer-or-name (not (eq buffer (window-buffer)))))
4611 ((window--delete nil t))
4613 ;; Switch to another buffer in window.
4614 (set-window-dedicated-p nil nil)
4615 (switch-to-prev-buffer nil 'bury)))
4617 ;; Always return nil.
4618 nil))
4620 (defun unbury-buffer ()
4621 "Switch to the last buffer in the buffer list."
4622 (interactive)
4623 (switch-to-buffer (last-buffer)))
4625 (defun next-buffer ()
4626 "In selected window switch to next buffer."
4627 (interactive)
4628 (cond
4629 ((window-minibuffer-p)
4630 (error "Cannot switch buffers in minibuffer window"))
4631 ((eq (window-dedicated-p) t)
4632 (error "Window is strongly dedicated to its buffer"))
4634 (switch-to-next-buffer))))
4636 (defun previous-buffer ()
4637 "In selected window switch to previous buffer."
4638 (interactive)
4639 (cond
4640 ((window-minibuffer-p)
4641 (error "Cannot switch buffers in minibuffer window"))
4642 ((eq (window-dedicated-p) t)
4643 (error "Window is strongly dedicated to its buffer"))
4645 (switch-to-prev-buffer))))
4647 (defun delete-windows-on (&optional buffer-or-name frame)
4648 "Delete all windows showing BUFFER-OR-NAME.
4649 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4650 and defaults to the current buffer.
4652 The following non-nil values of the optional argument FRAME
4653 have special meanings:
4655 - t means consider all windows on the selected frame only.
4657 - `visible' means consider all windows on all visible frames on
4658 the current terminal.
4660 - 0 (the number zero) means consider all windows on all visible
4661 and iconified frames on the current terminal.
4663 - A frame means consider all windows on that frame only.
4665 Any other value of FRAME means consider all windows on all
4666 frames.
4668 When a window showing BUFFER-OR-NAME is dedicated and the only
4669 window of its frame, that frame is deleted when there are other
4670 frames left."
4671 (interactive "BDelete windows on (buffer):\nP")
4672 (let ((buffer (window-normalize-buffer buffer-or-name))
4673 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4674 ;; `window-list-1' based function.
4675 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4676 (dolist (window (window-list-1 nil nil all-frames))
4677 (if (eq (window-buffer window) buffer)
4678 (let ((deletable (window-deletable-p window)))
4679 (cond
4680 ((and (eq deletable 'frame) (window-dedicated-p window))
4681 ;; Delete frame if and only if window is dedicated.
4682 (delete-frame (window-frame window)))
4683 ((eq deletable t)
4684 ;; Delete window.
4685 (delete-window window))
4687 ;; In window switch to previous buffer.
4688 (set-window-dedicated-p window nil)
4689 (switch-to-prev-buffer window 'bury))))
4690 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4691 (unrecord-window-buffer window buffer)))))
4693 (defun replace-buffer-in-windows (&optional buffer-or-name)
4694 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
4695 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4696 and defaults to the current buffer.
4698 When a window showing BUFFER-OR-NAME is dedicated, that window is
4699 deleted. If that window is the only window on its frame, the
4700 frame is deleted too when there are other frames left. If there
4701 are no other frames left, some other buffer is displayed in that
4702 window.
4704 This function removes the buffer denoted by BUFFER-OR-NAME from
4705 all window-local buffer lists."
4706 (interactive "bBuffer to replace: ")
4707 (let ((buffer (window-normalize-buffer buffer-or-name)))
4708 (dolist (window (window-list-1 nil nil t))
4709 (if (eq (window-buffer window) buffer)
4710 (unless (window--delete window t t)
4711 ;; Switch to another buffer in window.
4712 (set-window-dedicated-p window nil)
4713 (switch-to-prev-buffer window 'kill))
4714 ;; Unrecord BUFFER in WINDOW.
4715 (unrecord-window-buffer window buffer)))))
4717 (defun quit-restore-window (&optional window bury-or-kill)
4718 "Quit WINDOW and deal with its buffer.
4719 WINDOW must be a live window and defaults to the selected one.
4721 According to information stored in WINDOW's `quit-restore' window
4722 parameter either (1) delete WINDOW and its frame, (2) delete
4723 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4724 or (4) make WINDOW display some other buffer than the present
4725 one. If non-nil, reset `quit-restore' parameter to nil.
4727 Optional second argument BURY-OR-KILL tells how to proceed with
4728 the buffer of WINDOW. The following values are handled:
4730 nil means to not handle the buffer in a particular way. This
4731 means that if WINDOW is not deleted by this function, invoking
4732 `switch-to-prev-buffer' will usually show the buffer again.
4734 `append' means that if WINDOW is not deleted, move its buffer to
4735 the end of WINDOW's previous buffers so it's less likely that a
4736 future invocation of `switch-to-prev-buffer' will switch to it.
4737 Also, move the buffer to the end of the frame's buffer list.
4739 `bury' means that if WINDOW is not deleted, remove its buffer
4740 from WINDOW'S list of previous buffers. Also, move the buffer
4741 to the end of the frame's buffer list. This value provides the
4742 most reliable remedy to not have `switch-to-prev-buffer' switch
4743 to this buffer again without killing the buffer.
4745 `kill' means to kill WINDOW's buffer."
4746 (setq window (window-normalize-window window t))
4747 (let* ((buffer (window-buffer window))
4748 (quit-restore (window-parameter window 'quit-restore))
4749 (prev-buffer
4750 (let* ((prev-buffers (window-prev-buffers window))
4751 (prev-buffer (caar prev-buffers)))
4752 (and (or (not (eq prev-buffer buffer))
4753 (and (cdr prev-buffers)
4754 (not (eq (setq prev-buffer (cadr prev-buffers))
4755 buffer))))
4756 prev-buffer)))
4757 quad entry)
4758 (cond
4759 ((and (not prev-buffer)
4760 (or (eq (nth 1 quit-restore) 'frame)
4761 (and (eq (nth 1 quit-restore) 'window)
4762 ;; If the window has been created on an existing
4763 ;; frame and ended up as the sole window on that
4764 ;; frame, do not delete it (Bug#12764).
4765 (not (eq window (frame-root-window window)))))
4766 (eq (nth 3 quit-restore) buffer)
4767 ;; Delete WINDOW if possible.
4768 (window--delete window nil (eq bury-or-kill 'kill)))
4769 ;; If the previously selected window is still alive, select it.
4770 (when (window-live-p (nth 2 quit-restore))
4771 (select-window (nth 2 quit-restore))))
4772 ((and (listp (setq quad (nth 1 quit-restore)))
4773 (buffer-live-p (car quad))
4774 (eq (nth 3 quit-restore) buffer))
4775 ;; Show another buffer stored in quit-restore parameter.
4776 (when (and (integerp (nth 3 quad))
4777 (if (window-combined-p window)
4778 (/= (nth 3 quad) (window-total-height window))
4779 (/= (nth 3 quad) (window-total-width window))))
4780 ;; Try to resize WINDOW to its old height but don't signal an
4781 ;; error.
4782 (condition-case nil
4783 (window-resize
4784 window
4785 (- (nth 3 quad) (if (window-combined-p window)
4786 (window-total-height window)
4787 (window-total-width window)))
4788 (window-combined-p window t))
4789 (error nil)))
4790 (set-window-dedicated-p window nil)
4791 ;; Restore WINDOW's previous buffer, start and point position.
4792 (set-window-buffer-start-and-point
4793 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
4794 ;; Deal with the buffer we just removed from WINDOW.
4795 (setq entry (and (eq bury-or-kill 'append)
4796 (assq buffer (window-prev-buffers window))))
4797 (when bury-or-kill
4798 ;; Remove buffer from WINDOW's previous and next buffers.
4799 (set-window-prev-buffers
4800 window (assq-delete-all buffer (window-prev-buffers window)))
4801 (set-window-next-buffers
4802 window (delq buffer (window-next-buffers window))))
4803 (when entry
4804 ;; Append old buffer's entry to list of WINDOW's previous
4805 ;; buffers so it's less likely to get switched to soon but
4806 ;; `display-buffer-in-previous-window' can nevertheless find it.
4807 (set-window-prev-buffers
4808 window (append (window-prev-buffers window) (list entry))))
4809 ;; Reset the quit-restore parameter.
4810 (set-window-parameter window 'quit-restore nil)
4811 ;; Select old window.
4812 (when (window-live-p (nth 2 quit-restore))
4813 (select-window (nth 2 quit-restore))))
4815 ;; Show some other buffer in WINDOW and reset the quit-restore
4816 ;; parameter.
4817 (set-window-parameter window 'quit-restore nil)
4818 ;; Make sure that WINDOW is no more dedicated.
4819 (set-window-dedicated-p window nil)
4820 (switch-to-prev-buffer window bury-or-kill)))
4822 ;; Deal with the buffer.
4823 (cond
4824 ((not (buffer-live-p buffer)))
4825 ((eq bury-or-kill 'kill)
4826 (kill-buffer buffer))
4827 (bury-or-kill
4828 (bury-buffer-internal buffer)))))
4830 (defun quit-window (&optional kill window)
4831 "Quit WINDOW and bury its buffer.
4832 WINDOW must be a live window and defaults to the selected one.
4833 With prefix argument KILL non-nil, kill the buffer instead of
4834 burying it.
4836 According to information stored in WINDOW's `quit-restore' window
4837 parameter either (1) delete WINDOW and its frame, (2) delete
4838 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4839 or (4) make WINDOW display some other buffer than the present
4840 one. If non-nil, reset `quit-restore' parameter to nil."
4841 (interactive "P")
4842 (quit-restore-window window (if kill 'kill 'bury)))
4844 (defun quit-windows-on (&optional buffer-or-name kill frame)
4845 "Quit all windows showing BUFFER-OR-NAME.
4846 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4847 and defaults to the current buffer. Optional argument KILL
4848 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4849 BUFFER-OR-NAME. Optional argument FRAME is handled as by
4850 `delete-windows-on'.
4852 This function calls `quit-window' on all candidate windows
4853 showing BUFFER-OR-NAME."
4854 (interactive "BQuit windows on (buffer):\nP")
4855 (let ((buffer (window-normalize-buffer buffer-or-name))
4856 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4857 ;; `window-list-1' based function.
4858 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4859 (dolist (window (window-list-1 nil nil all-frames))
4860 (if (eq (window-buffer window) buffer)
4861 (quit-window kill window)
4862 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4863 (unrecord-window-buffer window buffer)))))
4865 (defun split-window (&optional window size side pixelwise)
4866 "Make a new window adjacent to WINDOW.
4867 WINDOW must be a valid window and defaults to the selected one.
4868 Return the new window which is always a live window.
4870 Optional argument SIZE a positive number means make WINDOW SIZE
4871 lines or columns tall. If SIZE is negative, make the new window
4872 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
4873 absolute value can be less than `window-min-height' or
4874 `window-min-width'; so this command can make a new window as
4875 small as one line or two columns. SIZE defaults to half of
4876 WINDOW's size.
4878 Optional third argument SIDE nil (or `below') specifies that the
4879 new window shall be located below WINDOW. SIDE `above' means the
4880 new window shall be located above WINDOW. In both cases SIZE
4881 specifies the new number of lines for WINDOW (or the new window
4882 if SIZE is negative) including space reserved for the mode and/or
4883 header line.
4885 SIDE t (or `right') specifies that the new window shall be
4886 located on the right side of WINDOW. SIDE `left' means the new
4887 window shall be located on the left of WINDOW. In both cases
4888 SIZE specifies the new number of columns for WINDOW (or the new
4889 window provided SIZE is negative) including space reserved for
4890 fringes and the scrollbar or a divider column. Any other non-nil
4891 value for SIDE is currently handled like t (or `right').
4893 PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
4895 If the variable `ignore-window-parameters' is non-nil or the
4896 `split-window' parameter of WINDOW equals t, do not process any
4897 parameters of WINDOW. Otherwise, if the `split-window' parameter
4898 of WINDOW specifies a function, call that function with all three
4899 arguments and return the value returned by that function.
4901 Otherwise, if WINDOW is part of an atomic window, \"split\" the
4902 root of that atomic window. The new window does not become a
4903 member of that atomic window.
4905 If WINDOW is live, properties of the new window like margins and
4906 scrollbars are inherited from WINDOW. If WINDOW is an internal
4907 window, these properties as well as the buffer displayed in the
4908 new window are inherited from the window selected on WINDOW's
4909 frame. The selected window is not changed by this function."
4910 (setq window (window-normalize-window window))
4911 (let* ((side (cond
4912 ((not side) 'below)
4913 ((memq side '(below above right left)) side)
4914 (t 'right)))
4915 (horizontal (not (memq side '(below above))))
4916 (frame (window-frame window))
4917 (parent (window-parent window))
4918 (function (window-parameter window 'split-window))
4919 (window-side (window-parameter window 'window-side))
4920 ;; Rebind the following two variables since in some cases we
4921 ;; have to override their value.
4922 (window-combination-limit window-combination-limit)
4923 (window-combination-resize window-combination-resize)
4924 (char-size (frame-char-size window horizontal))
4925 (pixel-size
4926 (when (numberp size)
4927 (window--size-to-pixel window size horizontal pixelwise t)))
4928 (divider-width (if horizontal
4929 (frame-right-divider-width frame)
4930 (frame-bottom-divider-width frame)))
4931 atom-root ignore)
4932 (window--check frame)
4933 (catch 'done
4934 (cond
4935 ;; Ignore window parameters if either `ignore-window-parameters'
4936 ;; is t or the `split-window' parameter equals t.
4937 ((or ignore-window-parameters (eq function t)))
4938 ((functionp function)
4939 ;; The `split-window' parameter specifies the function to call.
4940 ;; If that function is `ignore', do nothing.
4941 (throw 'done (funcall function window size side)))
4942 ;; If WINDOW is part of an atomic window, split the root window
4943 ;; of that atomic window instead.
4944 ((and (window-parameter window 'window-atom)
4945 (setq atom-root (window-atom-root window))
4946 (not (eq atom-root window)))
4947 (throw 'done (split-window atom-root size side pixelwise)))
4948 ;; If WINDOW is a side window or its first or last child is a
4949 ;; side window, throw an error unless `window-combination-resize'
4950 ;; equals 'side.
4951 ((and (not (eq window-combination-resize 'side))
4952 (window-parameter window 'window-side))
4953 (error "Cannot split side window or parent of side window"))
4954 ;; If `window-combination-resize' is 'side and window has a side
4955 ;; window sibling, bind `window-combination-limit' to t.
4956 ((and (not (eq window-combination-resize 'side))
4957 (or (and (window-prev-sibling window)
4958 (window-parameter
4959 (window-prev-sibling window) 'window-side))
4960 (and (window-next-sibling window)
4961 (window-parameter
4962 (window-next-sibling window) 'window-side))))
4963 (setq window-combination-limit t)))
4965 ;; If `window-combination-resize' is t and SIZE is non-negative,
4966 ;; bind `window-combination-limit' to t.
4967 (when (and (eq window-combination-resize t)
4968 pixel-size (> pixel-size 0))
4969 (setq window-combination-limit t))
4971 (let* ((parent-pixel-size
4972 ;; `parent-pixel-size' is the pixel size of WINDOW's
4973 ;; parent, provided it has one.
4974 (when parent (window-size parent horizontal t)))
4975 ;; `resize' non-nil means we are supposed to resize other
4976 ;; windows in WINDOW's combination.
4977 (resize
4978 (and window-combination-resize
4979 (or (window-parameter window 'window-side)
4980 (not (eq window-combination-resize 'side)))
4981 (not (eq window-combination-limit t))
4982 ;; Resize makes sense in iso-combinations only.
4983 (window-combined-p window horizontal)))
4984 ;; `old-pixel-size' is the current pixel size of WINDOW.
4985 (old-pixel-size (window-size window horizontal t))
4986 ;; `new-size' is the specified or calculated size of the
4987 ;; new window.
4988 new-pixel-size new-parent new-normal)
4989 (cond
4990 ((not pixel-size)
4991 (setq new-pixel-size
4992 (if resize
4993 ;; When resizing try to give the new window the
4994 ;; average size of a window in its combination.
4995 (max (min (- parent-pixel-size
4996 (window-min-size parent horizontal nil t))
4997 (/ parent-pixel-size
4998 (1+ (window-combinations parent horizontal))))
4999 (window-min-pixel-size))
5000 ;; Else try to give the new window half the size
5001 ;; of WINDOW (plus an eventual odd pixel).
5002 (/ old-pixel-size 2)))
5003 (unless window-resize-pixelwise
5004 ;; Round to nearest char-size multiple.
5005 (setq new-pixel-size
5006 (* char-size (round new-pixel-size char-size)))))
5007 ((>= pixel-size 0)
5008 ;; SIZE non-negative specifies the new size of WINDOW.
5010 ;; Note: Specifying a non-negative SIZE is practically
5011 ;; always done as workaround for making the new window
5012 ;; appear above or on the left of the new window (the
5013 ;; ispell window is a typical example of that). In all
5014 ;; these cases the SIDE argument should be set to 'above
5015 ;; or 'left in order to support the 'resize option.
5016 ;; Here we have to nest the windows instead, see above.
5017 (setq new-pixel-size (- old-pixel-size pixel-size)))
5019 ;; SIZE negative specifies the size of the new window.
5020 (setq new-pixel-size (- pixel-size))))
5022 ;; Check SIZE.
5023 (cond
5024 ((not pixel-size)
5025 (cond
5026 (resize
5027 ;; SIZE unspecified, resizing.
5028 (unless (or (window-sizable-p
5029 parent (- (+ new-pixel-size divider-width)) horizontal
5030 nil t)
5031 (window-sizable-p
5032 parent (- (+ new-pixel-size divider-width)) horizontal
5033 (setq ignore 'preserved) t))
5034 (error "Window %s too small for splitting" parent)))
5035 ((and (> (+ new-pixel-size divider-width
5036 (window-min-size window horizontal nil t))
5037 old-pixel-size)
5038 (> (+ new-pixel-size divider-width
5039 (window-min-size
5040 window horizontal (setq ignore 'preserved) t))
5041 old-pixel-size))
5042 ;; SIZE unspecified, no resizing.
5043 (error "Window %s too small for splitting" window))))
5044 ((and (>= pixel-size 0)
5045 (or (>= pixel-size old-pixel-size)
5046 (< new-pixel-size
5047 (window-safe-min-pixel-size window horizontal))))
5048 ;; SIZE specified as new size of old window. If the new size
5049 ;; is larger than the old size or the size of the new window
5050 ;; would be less than the safe minimum, signal an error.
5051 (error "Window %s too small for splitting" window))
5052 (resize
5053 ;; SIZE specified, resizing.
5054 (unless (or (window-sizable-p
5055 parent (- (+ new-pixel-size divider-width)) horizontal
5056 nil t)
5057 (window-sizable-p
5058 parent (- (+ new-pixel-size divider-width)) horizontal
5059 (setq ignore 'preserved) t))
5060 ;; If we cannot resize the parent give up.
5061 (error "Window %s too small for splitting" parent)))
5062 ((or (< new-pixel-size
5063 (window-safe-min-pixel-size window horizontal))
5064 (< (- old-pixel-size new-pixel-size)
5065 (window-safe-min-pixel-size window horizontal)))
5066 ;; SIZE specification violates minimum size restrictions.
5067 (error "Window %s too small for splitting" window)))
5069 (window--resize-reset frame horizontal)
5071 (setq new-parent
5072 ;; Make new-parent non-nil if we need a new parent window;
5073 ;; either because we want to nest or because WINDOW is not
5074 ;; iso-combined.
5075 (or (eq window-combination-limit t)
5076 (not (window-combined-p window horizontal))))
5077 (setq new-normal
5078 ;; Make new-normal the normal size of the new window.
5079 (cond
5080 (pixel-size (/ (float new-pixel-size)
5081 (if new-parent old-pixel-size parent-pixel-size)))
5082 (new-parent 0.5)
5083 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
5084 (t (/ (window-normal-size window horizontal) 2.0))))
5086 (if resize
5087 ;; Try to get space from OLD's siblings. We could go "up" and
5088 ;; try getting additional space from surrounding windows but
5089 ;; we won't be able to return space to those windows when we
5090 ;; delete the one we create here. Hence we do not go up.
5091 (progn
5092 (window--resize-child-windows
5093 parent (- new-pixel-size) horizontal nil ignore)
5094 (let* ((normal (- 1.0 new-normal))
5095 (sub (window-child parent)))
5096 (while sub
5097 (set-window-new-normal
5098 sub (* (window-normal-size sub horizontal) normal))
5099 (setq sub (window-right sub)))))
5100 ;; Get entire space from WINDOW.
5101 (set-window-new-pixel
5102 window (- old-pixel-size new-pixel-size))
5103 (window--resize-this-window
5104 window (- new-pixel-size) horizontal ignore)
5105 (set-window-new-normal
5106 window (- (if new-parent 1.0 (window-normal-size window horizontal))
5107 new-normal)))
5109 (let* ((new (split-window-internal window new-pixel-size side new-normal)))
5110 (window--pixel-to-total frame horizontal)
5111 ;; Assign window-side parameters, if any.
5112 (cond
5113 ((eq window-combination-resize 'side)
5114 (let ((window-side
5115 (cond
5116 (window-side window-side)
5117 ((eq side 'above) 'top)
5118 ((eq side 'below) 'bottom)
5119 (t side))))
5120 ;; We made a new side window.
5121 (set-window-parameter new 'window-side window-side)
5122 (when (and new-parent (window-parameter window 'window-side))
5123 ;; We've been splitting a side root window. Give the
5124 ;; new parent the same window-side parameter.
5125 (set-window-parameter
5126 (window-parent new) 'window-side window-side))))
5127 ((eq window-combination-resize 'atom)
5128 ;; Make sure `window--check-frame' won't destroy an existing
5129 ;; atomic window in case the new window gets nested inside.
5130 (unless (window-parameter window 'window-atom)
5131 (set-window-parameter window 'window-atom t))
5132 (when new-parent
5133 (set-window-parameter (window-parent new) 'window-atom t))
5134 (set-window-parameter new 'window-atom t)))
5136 ;; Sanitize sizes unless SIZE was specified.
5137 (unless size
5138 (window--sanitize-window-sizes horizontal))
5140 (run-window-configuration-change-hook frame)
5141 (run-window-scroll-functions new)
5142 (window--check frame)
5143 ;; Always return the new window.
5144 new)))))
5146 (defun split-window-no-error (&optional window size side pixelwise)
5147 "Make a new window adjacent to WINDOW.
5148 This function is like `split-window' but does not signal an error
5149 when WINDOW cannot be split.
5151 For the meaning of all arguments see the documentation of
5152 `split-window'."
5153 (condition-case nil
5154 (split-window window size side pixelwise)
5155 (error nil)))
5157 ;; I think this should be the default; I think people will prefer it--rms.
5158 (defcustom split-window-keep-point t
5159 "If non-nil, \\[split-window-below] preserves point in the new window.
5160 If nil, adjust point in the two windows to minimize redisplay.
5161 This option applies only to `split-window-below' and functions
5162 that call it. The low-level `split-window' function always keeps
5163 the original point in both windows."
5164 :type 'boolean
5165 :group 'windows)
5167 (defun split-window-below (&optional size)
5168 "Split the selected window into two windows, one above the other.
5169 The selected window is above. The newly split-off window is
5170 below and displays the same buffer. Return the new window.
5172 If optional argument SIZE is omitted or nil, both windows get the
5173 same height, or close to it. If SIZE is positive, the upper
5174 \(selected) window gets SIZE lines. If SIZE is negative, the
5175 lower (new) window gets -SIZE lines.
5177 If the variable `split-window-keep-point' is non-nil, both
5178 windows get the same value of point as the selected window.
5179 Otherwise, the window starts are chosen so as to minimize the
5180 amount of redisplay; this is convenient on slow terminals."
5181 (interactive "P")
5182 (let ((old-window (selected-window))
5183 (old-point (window-point))
5184 (size (and size (prefix-numeric-value size)))
5185 moved-by-window-height moved new-window bottom)
5186 (when (and size (< size 0) (< (- size) window-min-height))
5187 ;; `split-window' would not signal an error here.
5188 (error "Size of new window too small"))
5189 (setq new-window (split-window nil size))
5190 (unless split-window-keep-point
5191 (with-current-buffer (window-buffer)
5192 ;; Use `save-excursion' around vertical movements below
5193 ;; (Bug#10971). Note: When the selected window's buffer has a
5194 ;; header line, up to two lines of the buffer may not show up
5195 ;; in the resulting configuration.
5196 (save-excursion
5197 (goto-char (window-start))
5198 (setq moved (vertical-motion (window-height)))
5199 (set-window-start new-window (point))
5200 (when (> (point) (window-point new-window))
5201 (set-window-point new-window (point)))
5202 (when (= moved (window-height))
5203 (setq moved-by-window-height t)
5204 (vertical-motion -1))
5205 (setq bottom (point)))
5206 (and moved-by-window-height
5207 (<= bottom (point))
5208 (set-window-point old-window (1- bottom)))
5209 (and moved-by-window-height
5210 (<= (window-start new-window) old-point)
5211 (set-window-point new-window old-point)
5212 (select-window new-window))))
5213 ;; Always copy quit-restore parameter in interactive use.
5214 (let ((quit-restore (window-parameter old-window 'quit-restore)))
5215 (when quit-restore
5216 (set-window-parameter new-window 'quit-restore quit-restore)))
5217 new-window))
5219 (defalias 'split-window-vertically 'split-window-below)
5221 (defun split-window-right (&optional size)
5222 "Split the selected window into two side-by-side windows.
5223 The selected window is on the left. The newly split-off window
5224 is on the right and displays the same buffer. Return the new
5225 window.
5227 If optional argument SIZE is omitted or nil, both windows get the
5228 same width, or close to it. If SIZE is positive, the left-hand
5229 \(selected) window gets SIZE columns. If SIZE is negative, the
5230 right-hand (new) window gets -SIZE columns. Here, SIZE includes
5231 the width of the window's scroll bar; if there are no scroll
5232 bars, it includes the width of the divider column to the window's
5233 right, if any."
5234 (interactive "P")
5235 (let ((old-window (selected-window))
5236 (size (and size (prefix-numeric-value size)))
5237 new-window)
5238 (when (and size (< size 0) (< (- size) window-min-width))
5239 ;; `split-window' would not signal an error here.
5240 (error "Size of new window too small"))
5241 (setq new-window (split-window nil size t))
5242 ;; Always copy quit-restore parameter in interactive use.
5243 (let ((quit-restore (window-parameter old-window 'quit-restore)))
5244 (when quit-restore
5245 (set-window-parameter new-window 'quit-restore quit-restore)))
5246 new-window))
5248 (defalias 'split-window-horizontally 'split-window-right)
5250 ;;; Balancing windows.
5252 ;; The following routine uses the recycled code from an old version of
5253 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
5254 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
5255 ;; more readable (FWIW we'd need three loops - one to calculate the
5256 ;; minimum sizes per window, one to enlarge or shrink windows until the
5257 ;; new parent-size matches, and one where we shrink the largest/enlarge
5258 ;; the smallest window).
5259 (defun balance-windows-2 (window horizontal)
5260 "Subroutine of `balance-windows-1'.
5261 WINDOW must be a vertical combination (horizontal if HORIZONTAL
5262 is non-nil)."
5263 (let* ((char-size (if window-resize-pixelwise
5265 (frame-char-size window horizontal)))
5266 (first (window-child window))
5267 (sub first)
5268 (number-of-children 0)
5269 (parent-size (window-new-pixel window))
5270 (total-sum parent-size)
5271 failed size sub-total sub-delta sub-amount rest)
5272 (while sub
5273 (setq number-of-children (1+ number-of-children))
5274 (when (window-size-fixed-p sub horizontal)
5275 (setq total-sum
5276 (- total-sum (window-size sub horizontal t)))
5277 (set-window-new-normal sub 'ignore))
5278 (setq sub (window-right sub)))
5280 (setq failed t)
5281 (while (and failed (> number-of-children 0))
5282 (setq size (/ total-sum number-of-children))
5283 (setq failed nil)
5284 (setq sub first)
5285 (while (and sub (not failed))
5286 ;; Ignore child windows that should be ignored or are stuck.
5287 (unless (window--resize-child-windows-skip-p sub)
5288 (setq sub-total (window-size sub horizontal t))
5289 (setq sub-delta (- size sub-total))
5290 (setq sub-amount
5291 (window-sizable sub sub-delta horizontal nil t))
5292 ;; Register the new total size for this child window.
5293 (set-window-new-pixel sub (+ sub-total sub-amount))
5294 (unless (= sub-amount sub-delta)
5295 (setq total-sum (- total-sum sub-total sub-amount))
5296 (setq number-of-children (1- number-of-children))
5297 ;; We failed and need a new round.
5298 (setq failed t)
5299 (set-window-new-normal sub 'skip)))
5300 (setq sub (window-right sub))))
5302 ;; How can we be sure that `number-of-children' is NOT zero here ?
5303 (setq rest (% total-sum number-of-children))
5304 ;; Fix rounding by trying to enlarge non-stuck windows by one line
5305 ;; (column) until `rest' is zero.
5306 (setq sub first)
5307 (while (and sub (> rest 0))
5308 (unless (window--resize-child-windows-skip-p window)
5309 (set-window-new-pixel sub (min rest char-size) t)
5310 (setq rest (- rest char-size)))
5311 (setq sub (window-right sub)))
5313 ;; Fix rounding by trying to enlarge stuck windows by one line
5314 ;; (column) until `rest' equals zero.
5315 (setq sub first)
5316 (while (and sub (> rest 0))
5317 (unless (eq (window-new-normal sub) 'ignore)
5318 (set-window-new-pixel sub (min rest char-size) t)
5319 (setq rest (- rest char-size)))
5320 (setq sub (window-right sub)))
5322 (setq sub first)
5323 (while sub
5324 ;; Record new normal sizes.
5325 (set-window-new-normal
5326 sub (/ (if (eq (window-new-normal sub) 'ignore)
5327 (window-size sub horizontal t)
5328 (window-new-pixel sub))
5329 (float parent-size)))
5330 ;; Recursively balance each window's child windows.
5331 (balance-windows-1 sub horizontal)
5332 (setq sub (window-right sub)))))
5334 (defun balance-windows-1 (window &optional horizontal)
5335 "Subroutine of `balance-windows'."
5336 (if (window-child window)
5337 (let ((sub (window-child window)))
5338 (if (window-combined-p sub horizontal)
5339 (balance-windows-2 window horizontal)
5340 (let ((size (window-new-pixel window)))
5341 (while sub
5342 (set-window-new-pixel sub size)
5343 (balance-windows-1 sub horizontal)
5344 (setq sub (window-right sub))))))))
5346 (defun balance-windows (&optional window-or-frame)
5347 "Balance the sizes of windows of WINDOW-OR-FRAME.
5348 WINDOW-OR-FRAME is optional and defaults to the selected frame.
5349 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
5350 windows of that frame. If WINDOW-OR-FRAME denotes a window,
5351 recursively balance the sizes of all child windows of that
5352 window."
5353 (interactive)
5354 (let* ((window
5355 (cond
5356 ((or (not window-or-frame)
5357 (frame-live-p window-or-frame))
5358 (frame-root-window window-or-frame))
5359 ((or (window-live-p window-or-frame)
5360 (window-child window-or-frame))
5361 window-or-frame)
5363 (error "Not a window or frame %s" window-or-frame))))
5364 (frame (window-frame window)))
5365 ;; Balance vertically.
5366 (window--resize-reset (window-frame window))
5367 (balance-windows-1 window)
5368 (when (window--resize-apply-p frame)
5369 (window-resize-apply frame)
5370 (window--pixel-to-total frame)
5371 (run-window-configuration-change-hook frame))
5372 ;; Balance horizontally.
5373 (window--resize-reset (window-frame window) t)
5374 (balance-windows-1 window t)
5375 (when (window--resize-apply-p frame t)
5376 (window-resize-apply frame t)
5377 (window--pixel-to-total frame t)
5378 (run-window-configuration-change-hook frame))))
5380 (defun window-fixed-size-p (&optional window direction)
5381 "Return t if WINDOW cannot be resized in DIRECTION.
5382 WINDOW defaults to the selected window. DIRECTION can be
5383 nil (i.e. any), `height' or `width'."
5384 (with-current-buffer (window-buffer window)
5385 (when (and (boundp 'window-size-fixed) window-size-fixed)
5386 (not (and direction
5387 (member (cons direction window-size-fixed)
5388 '((height . width) (width . height))))))))
5390 ;;; A different solution to balance-windows.
5391 (defvar window-area-factor 1
5392 "Factor by which the window area should be over-estimated.
5393 This is used by `balance-windows-area'.
5394 Changing this globally has no effect.")
5395 (make-variable-buffer-local 'window-area-factor)
5397 (defun balance-windows-area-adjust (window delta horizontal pixelwise)
5398 "Wrapper around `window-resize' with error checking.
5399 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
5400 ;; `window-resize' may fail if delta is too large.
5401 (while (>= (abs delta) 1)
5402 (condition-case nil
5403 (progn
5404 ;; It was wrong to use `window-resize' here. Somehow
5405 ;; `balance-windows-area' depends on resizing windows
5406 ;; asymmetrically.
5407 (adjust-window-trailing-edge window delta horizontal pixelwise)
5408 (setq delta 0))
5409 (error
5410 ;;(message "adjust: %s" (error-message-string err))
5411 (setq delta (/ delta 2))))))
5413 (defun balance-windows-area ()
5414 "Make all visible windows the same area (approximately).
5415 See also `window-area-factor' to change the relative size of
5416 specific buffers."
5417 (interactive)
5418 (let* ((unchanged 0) (carry 0) (round 0)
5419 ;; Remove fixed-size windows.
5420 (wins (delq nil (mapcar (lambda (win)
5421 (if (not (window-fixed-size-p win)) win))
5422 (window-list nil 'nomini))))
5423 (changelog nil)
5424 (pixelwise window-resize-pixelwise)
5425 next)
5426 ;; Resizing a window changes the size of surrounding windows in complex
5427 ;; ways, so it's difficult to balance them all. The introduction of
5428 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
5429 ;; very difficult to do. `balance-window' above takes an off-line
5430 ;; approach: get the whole window tree, then balance it, then try to
5431 ;; adjust the windows so they fit the result.
5432 ;; Here, instead, we take a "local optimization" approach, where we just
5433 ;; go through all the windows several times until nothing needs to be
5434 ;; changed. The main problem with this approach is that it's difficult
5435 ;; to make sure it terminates, so we use some heuristic to try and break
5436 ;; off infinite loops.
5437 ;; After a round without any change, we allow a second, to give a chance
5438 ;; to the carry to propagate a minor imbalance from the end back to
5439 ;; the beginning.
5440 (while (< unchanged 2)
5441 ;; (message "New round")
5442 (setq unchanged (1+ unchanged) round (1+ round))
5443 (dolist (win wins)
5444 (setq next win)
5445 (while (progn (setq next (next-window next))
5446 (window-fixed-size-p next)))
5447 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
5448 (let* ((horiz
5449 (< (car (window-pixel-edges win)) (car (window-pixel-edges next))))
5450 (areadiff (/ (- (* (window-size next nil pixelwise)
5451 (window-size next t pixelwise)
5452 (buffer-local-value 'window-area-factor
5453 (window-buffer next)))
5454 (* (window-size win nil pixelwise)
5455 (window-size win t pixelwise)
5456 (buffer-local-value 'window-area-factor
5457 (window-buffer win))))
5458 (max (buffer-local-value 'window-area-factor
5459 (window-buffer win))
5460 (buffer-local-value 'window-area-factor
5461 (window-buffer next)))))
5462 (edgesize (if horiz
5463 (+ (window-size win nil pixelwise)
5464 (window-size next nil pixelwise))
5465 (+ (window-size win t pixelwise)
5466 (window-size next t pixelwise))))
5467 (diff (/ areadiff edgesize)))
5468 (when (zerop diff)
5469 ;; Maybe diff is actually closer to 1 than to 0.
5470 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
5471 (when (and (zerop diff) (not (zerop areadiff)))
5472 (setq diff (/ (+ areadiff carry) edgesize))
5473 ;; Change things smoothly.
5474 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
5475 (if (zerop diff)
5476 ;; Make sure negligible differences don't accumulate to
5477 ;; become significant.
5478 (setq carry (+ carry areadiff))
5479 ;; This used `adjust-window-trailing-edge' before and uses
5480 ;; `window-resize' now. Error wrapping is still needed.
5481 (balance-windows-area-adjust win diff horiz pixelwise)
5482 ;; (sit-for 0.5)
5483 (let ((change (cons win (window-pixel-edges win))))
5484 ;; If the same change has been seen already for this window,
5485 ;; we're most likely in an endless loop, so don't count it as
5486 ;; a change.
5487 (unless (member change changelog)
5488 (push change changelog)
5489 (setq unchanged 0 carry 0)))))))
5490 ;; We've now basically balanced all the windows.
5491 ;; But there may be some minor off-by-one imbalance left over,
5492 ;; so let's do some fine tuning.
5493 ;; (bw-finetune wins)
5494 ;; (message "Done in %d rounds" round)
5497 ;;; Window states, how to get them and how to put them in a window.
5498 (defun window--state-get-1 (window &optional writable)
5499 "Helper function for `window-state-get'."
5500 (let* ((type
5501 (cond
5502 ((window-top-child window) 'vc)
5503 ((window-left-child window) 'hc)
5504 (t 'leaf)))
5505 (buffer (window-buffer window))
5506 (selected (eq window (selected-window)))
5507 (head
5508 `(,type
5509 ,@(unless (window-next-sibling window) `((last . t)))
5510 (pixel-width . ,(window-pixel-width window))
5511 (pixel-height . ,(window-pixel-height window))
5512 (total-width . ,(window-total-width window))
5513 (total-height . ,(window-total-height window))
5514 (normal-height . ,(window-normal-size window))
5515 (normal-width . ,(window-normal-size window t))
5516 ,@(unless (window-live-p window)
5517 `((combination-limit . ,(window-combination-limit window))))
5518 ,@(let ((parameters (window-parameters window))
5519 list)
5520 ;; Make copies of those window parameters whose
5521 ;; persistence property is `writable' if WRITABLE is
5522 ;; non-nil and non-nil if WRITABLE is nil.
5523 (dolist (par parameters)
5524 (let ((pers (cdr (assq (car par)
5525 window-persistent-parameters))))
5526 (when (and pers (or (not writable) (eq pers 'writable)))
5527 (setq list (cons (cons (car par) (cdr par)) list)))))
5528 ;; Add `clone-of' parameter if necessary.
5529 (let ((pers (cdr (assq 'clone-of
5530 window-persistent-parameters))))
5531 (when (and pers (or (not writable) (eq pers 'writable))
5532 (not (assq 'clone-of list)))
5533 (setq list (cons (cons 'clone-of window) list))))
5534 (when list
5535 `((parameters . ,list))))
5536 ,@(when buffer
5537 ;; All buffer related things go in here.
5538 (let ((point (window-point window))
5539 (start (window-start window)))
5540 `((buffer
5541 ,(buffer-name buffer)
5542 (selected . ,selected)
5543 (hscroll . ,(window-hscroll window))
5544 (fringes . ,(window-fringes window))
5545 (margins . ,(window-margins window))
5546 (scroll-bars . ,(window-scroll-bars window))
5547 (vscroll . ,(window-vscroll window))
5548 (dedicated . ,(window-dedicated-p window))
5549 (point . ,(if writable
5550 point
5551 (with-current-buffer buffer
5552 (copy-marker point
5553 (buffer-local-value
5554 'window-point-insertion-type
5555 buffer)))))
5556 (start . ,(if writable
5557 start
5558 (with-current-buffer buffer
5559 (copy-marker start))))))))))
5560 (tail
5561 (when (memq type '(vc hc))
5562 (let (list)
5563 (setq window (window-child window))
5564 (while window
5565 (setq list (cons (window--state-get-1 window writable) list))
5566 (setq window (window-right window)))
5567 (nreverse list)))))
5568 (append head tail)))
5570 (defun window-state-get (&optional window writable)
5571 "Return state of WINDOW as a Lisp object.
5572 WINDOW can be any window and defaults to the root window of the
5573 selected frame.
5575 Optional argument WRITABLE non-nil means do not use markers for
5576 sampling `window-point' and `window-start'. Together, WRITABLE
5577 and the variable `window-persistent-parameters' specify which
5578 window parameters are saved by this function. WRITABLE should be
5579 non-nil when the return value shall be written to a file and read
5580 back in another session. Otherwise, an application may run into
5581 an `invalid-read-syntax' error while attempting to read back the
5582 value from file.
5584 The return value can be used as argument for `window-state-put'
5585 to put the state recorded here into an arbitrary window. The
5586 value can be also stored on disk and read back in a new session."
5587 (setq window
5588 (if window
5589 (if (window-valid-p window)
5590 window
5591 (error "%s is not a live or internal window" window))
5592 (frame-root-window)))
5593 ;; The return value is a cons whose car specifies some constraints on
5594 ;; the size of WINDOW. The cdr lists the states of the child windows
5595 ;; of WINDOW.
5596 (cons
5597 ;; Frame related things would go into a function, say `frame-state',
5598 ;; calling `window-state-get' to insert the frame's root window.
5599 `((min-height . ,(window-min-size window))
5600 (min-width . ,(window-min-size window t))
5601 (min-height-ignore . ,(window-min-size window nil t))
5602 (min-width-ignore . ,(window-min-size window t t))
5603 (min-height-safe . ,(window-min-size window nil 'safe))
5604 (min-width-safe . ,(window-min-size window t 'safe))
5605 (min-pixel-height . ,(window-min-size window nil nil t))
5606 (min-pixel-width . ,(window-min-size window t nil t))
5607 (min-pixel-height-ignore . ,(window-min-size window nil t t))
5608 (min-pixel-width-ignore . ,(window-min-size window t t t))
5609 (min-pixel-height-safe . ,(window-min-size window nil 'safe t))
5610 (min-pixel-width-safe . ,(window-min-size window t 'safe t)))
5611 (window--state-get-1 window writable)))
5613 (defvar window-state-put-list nil
5614 "Helper variable for `window-state-put'.")
5616 (defvar window-state-put-stale-windows nil
5617 "Helper variable for `window-state-put'.")
5619 (defun window--state-put-1 (state &optional window ignore totals pixelwise)
5620 "Helper function for `window-state-put'."
5621 (let ((type (car state)))
5622 (setq state (cdr state))
5623 (cond
5624 ((eq type 'leaf)
5625 ;; For a leaf window just add unprocessed entries to
5626 ;; `window-state-put-list'.
5627 (push (cons window state) window-state-put-list))
5628 ((memq type '(vc hc))
5629 (let* ((horizontal (eq type 'hc))
5630 (total (window-size window horizontal pixelwise))
5631 (first t)
5632 (window-combination-limit (cdr (assq 'combination-limit state)))
5633 size new)
5634 (dolist (item state)
5635 ;; Find the next child window. WINDOW always points to the
5636 ;; real window that we want to fill with what we find here.
5637 (when (memq (car item) '(leaf vc hc))
5638 (if (assq 'last item)
5639 ;; The last child window. Below `window--state-put-1'
5640 ;; will put into it whatever ITEM has in store.
5641 (setq new nil)
5642 ;; Not the last child window, prepare for splitting
5643 ;; WINDOW. SIZE is the new (and final) size of the old
5644 ;; window.
5645 (setq size
5646 (if totals
5647 ;; Use total size.
5648 (if pixelwise
5649 (cdr (assq (if horizontal
5650 'pixel-width
5651 'pixel-height)
5652 item))
5653 (cdr (assq (if horizontal
5654 'total-width
5655 'total-height)
5656 item)))
5657 ;; Use normalized size and round.
5658 (round
5659 (* total
5660 (cdr (assq (if horizontal 'normal-width 'normal-height)
5661 item))))))
5663 ;; Use safe sizes, we try to resize later.
5664 (setq size (max size
5665 (if horizontal
5666 (* window-safe-min-width
5667 (if pixelwise
5668 (frame-char-width (window-frame window))
5670 (* window-safe-min-height
5671 (if pixelwise
5672 (frame-char-height (window-frame window))
5673 1)))))
5674 (if (window-sizable-p window (- size) horizontal 'safe pixelwise)
5675 (progn
5676 (setq new (split-window-no-error
5677 window size horizontal pixelwise))
5678 (setq window-combination-limit nil))
5679 ;; Give up if we can't resize window down to safe sizes.
5680 (error "Cannot resize window %s" window))
5682 (when first
5683 (setq first nil)
5684 ;; When creating the first child window add for parent
5685 ;; unprocessed entries to `window-state-put-list'.
5686 (setq window-state-put-list
5687 (cons (cons (window-parent window) state)
5688 window-state-put-list))))
5690 ;; Now process the current window (either the one we've just
5691 ;; split or the last child of its parent).
5692 (window--state-put-1 item window ignore totals)
5693 ;; Continue with the last window split off.
5694 (setq window new))))))))
5696 (defun window--state-put-2 (ignore pixelwise)
5697 "Helper function for `window-state-put'."
5698 (dolist (item window-state-put-list)
5699 (let ((window (car item))
5700 (combination-limit (cdr (assq 'combination-limit item)))
5701 (parameters (cdr (assq 'parameters item)))
5702 (state (cdr (assq 'buffer item))))
5703 (when combination-limit
5704 (set-window-combination-limit window combination-limit))
5705 ;; Reset window's parameters and assign saved ones (we might want
5706 ;; a `remove-window-parameters' function here).
5707 (dolist (parameter (window-parameters window))
5708 (set-window-parameter window (car parameter) nil))
5709 (when parameters
5710 (dolist (parameter parameters)
5711 (set-window-parameter window (car parameter) (cdr parameter))))
5712 ;; Process buffer related state.
5713 (when state
5714 (let ((buffer (get-buffer (car state))))
5715 (if buffer
5716 (with-current-buffer buffer
5717 (set-window-buffer window buffer)
5718 (set-window-hscroll window (cdr (assq 'hscroll state)))
5719 (apply 'set-window-fringes
5720 (cons window (cdr (assq 'fringes state))))
5721 (let ((margins (cdr (assq 'margins state))))
5722 (set-window-margins window (car margins) (cdr margins)))
5723 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
5724 (set-window-scroll-bars
5725 window (car scroll-bars) (nth 2 scroll-bars)
5726 (nth 3 scroll-bars) (nth 5 scroll-bars)))
5727 (set-window-vscroll window (cdr (assq 'vscroll state)))
5728 ;; Adjust vertically.
5729 (if (or (memq window-size-fixed '(t height))
5730 (window-preserved-size window))
5731 ;; A fixed height window, try to restore the
5732 ;; original size.
5733 (let ((delta
5734 (- (cdr (assq
5735 (if pixelwise 'pixel-height 'total-height)
5736 item))
5737 (window-size window nil pixelwise)))
5738 window-size-fixed)
5739 (when (window--resizable-p
5740 window delta nil nil nil nil nil pixelwise)
5741 (window-resize window delta nil nil pixelwise)))
5742 ;; Else check whether the window is not high enough.
5743 (let* ((min-size
5744 (window-min-size window nil ignore pixelwise))
5745 (delta
5746 (- min-size (window-size window nil pixelwise))))
5747 (when (and (> delta 0)
5748 (window--resizable-p
5749 window delta nil ignore nil nil nil pixelwise))
5750 (window-resize window delta nil ignore pixelwise))))
5751 ;; Adjust horizontally.
5752 (if (or (memq window-size-fixed '(t width))
5753 (window-preserved-size window t))
5754 ;; A fixed width window, try to restore the original
5755 ;; size.
5756 (let ((delta
5757 (- (cdr (assq
5758 (if pixelwise 'pixel-width 'total-width)
5759 item))
5760 (window-size window t pixelwise)))
5761 window-size-fixed)
5762 (when (window--resizable-p
5763 window delta t nil nil nil nil pixelwise)
5764 (window-resize window delta t nil pixelwise)))
5765 ;; Else check whether the window is not wide enough.
5766 (let* ((min-size (window-min-size window t ignore pixelwise))
5767 (delta (- min-size (window-size window t pixelwise))))
5768 (when (and (> delta 0)
5769 (window--resizable-p
5770 window delta t ignore nil nil nil pixelwise))
5771 (window-resize window delta t ignore pixelwise))))
5772 ;; Set dedicated status.
5773 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
5774 ;; Install positions (maybe we should do this after all
5775 ;; windows have been created and sized).
5776 (ignore-errors
5777 ;; Set 'noforce argument to avoid that window start
5778 ;; overrides window point set below (Bug#24240).
5779 (set-window-start window (cdr (assq 'start state)) 'noforce)
5780 (set-window-point window (cdr (assq 'point state))))
5781 ;; Select window if it's the selected one.
5782 (when (cdr (assq 'selected state))
5783 (select-window window)))
5784 ;; We don't want to raise an error in case the buffer does
5785 ;; not exist anymore, so we switch to a previous one and
5786 ;; save the window with the intention of deleting it later
5787 ;; if possible.
5788 (switch-to-prev-buffer window)
5789 (push window window-state-put-stale-windows)))))))
5791 (defun window-state-put (state &optional window ignore)
5792 "Put window state STATE into WINDOW.
5793 STATE should be the state of a window returned by an earlier
5794 invocation of `window-state-get'. Optional argument WINDOW must
5795 specify a valid window and defaults to the selected one. If
5796 WINDOW is not live, replace WINDOW by a live one before putting
5797 STATE into it.
5799 Optional argument IGNORE non-nil means ignore minimum window
5800 sizes and fixed size restrictions. IGNORE equal `safe' means
5801 windows can get as small as `window-safe-min-height' and
5802 `window-safe-min-width'."
5803 (setq window-state-put-stale-windows nil)
5804 (setq window (window-normalize-window window))
5806 ;; When WINDOW is internal, reduce it to a live one to put STATE into,
5807 ;; see Bug#16793.
5808 (unless (window-live-p window)
5809 (let ((root window))
5810 (setq window (catch 'live
5811 (walk-window-subtree
5812 (lambda (window)
5813 (when (and (window-live-p window)
5814 (not (window-parameter window 'window-side)))
5815 (throw 'live window)))
5816 root)))
5817 (delete-other-windows-internal window root)))
5819 (set-window-dedicated-p window nil)
5821 (let* ((frame (window-frame window))
5822 (head (car state))
5823 ;; We check here (1) whether the total sizes of root window of
5824 ;; STATE and that of WINDOW are equal so we can avoid
5825 ;; calculating new sizes, and (2) if we do have to resize
5826 ;; whether we can do so without violating size restrictions.
5827 (pixelwise (and (cdr (assq 'pixel-width state))
5828 (cdr (assq 'pixel-height state))))
5829 (totals (or (and pixelwise
5830 (= (window-pixel-width window)
5831 (cdr (assq 'pixel-width state)))
5832 (= (window-pixel-height window)
5833 (cdr (assq 'pixel-height state))))
5834 (and (= (window-total-width window)
5835 (cdr (assq 'total-width state)))
5836 (= (window-total-height window)
5837 (cdr (assq 'total-height state))))))
5838 (min-height (cdr (assq
5839 (if pixelwise 'min-pixel-height 'min-height)
5840 head)))
5841 (min-width (cdr (assq
5842 (if pixelwise 'min-pixel-width 'min-weight)
5843 head))))
5844 (if (and (not totals)
5845 (or (> min-height (window-size window nil pixelwise))
5846 (> min-width (window-size window t pixelwise)))
5847 (or (not ignore)
5848 (and (setq min-height
5849 (cdr (assq
5850 (if pixelwise
5851 'min-pixel-height-ignore
5852 'min-height-ignore)
5853 head)))
5854 (setq min-width
5855 (cdr (assq
5856 (if pixelwise
5857 'min-pixel-width-ignore
5858 'min-width-ignore)
5859 head)))
5860 (or (> min-height
5861 (window-size window nil pixelwise))
5862 (> min-width
5863 (window-size window t pixelwise)))
5864 (or (not (eq ignore 'safe))
5865 (and (setq min-height
5866 (cdr (assq
5867 (if pixelwise
5868 'min-pixel-height-safe
5869 'min-height-safe)
5870 head)))
5871 (setq min-width
5872 (cdr (assq
5873 (if pixelwise
5874 'min-pixel-width-safe
5875 'min-width-safe)
5876 head)))
5877 (or (> min-height
5878 (window-size window nil pixelwise))
5879 (> min-width
5880 (window-size window t pixelwise))))))))
5881 ;; The check above might not catch all errors due to rounding
5882 ;; issues - so IGNORE equal 'safe might not always produce the
5883 ;; minimum possible state. But such configurations hardly make
5884 ;; sense anyway.
5885 (error "Window %s too small to accommodate state" window)
5886 (setq state (cdr state))
5887 (setq window-state-put-list nil)
5888 ;; Work on the windows of a temporary buffer to make sure that
5889 ;; splitting proceeds regardless of any buffer local values of
5890 ;; `window-size-fixed'. Release that buffer after the buffers of
5891 ;; all live windows have been set by `window--state-put-2'.
5892 (with-temp-buffer
5893 (set-window-buffer window (current-buffer))
5894 (window--state-put-1 state window nil totals pixelwise)
5895 (window--state-put-2 ignore pixelwise))
5896 (while window-state-put-stale-windows
5897 (let ((window (pop window-state-put-stale-windows)))
5898 (when (eq (window-deletable-p window) t)
5899 (delete-window window))))
5900 (window--check frame))))
5902 (defun window-swap-states (&optional window-1 window-2 size)
5903 "Swap the states of live windows WINDOW-1 and WINDOW-2.
5904 WINDOW-1 must specify a live window and defaults to the selected
5905 one. WINDOW-2 must specify a live window and defaults to the
5906 window following WINDOW-1 in the cyclic ordering of windows,
5907 excluding minibuffer windows and including live windows on all
5908 visible frames.
5910 Optional argument SIZE non-nil means to try swapping the sizes of
5911 WINDOW-1 and WINDOW-2 as well. A value of `height' means to swap
5912 heights only, a value of `width' means to swap widths only, while
5913 t means to swap both widths and heights, if possible. Frames are
5914 not resized by this function."
5915 (interactive)
5916 (setq window-1 (window-normalize-window window-1 t))
5917 (if window-2
5918 (unless (window-live-p window-2)
5919 (error "%s is not a live window" window-2))
5920 (setq window-2 (next-window window-1 'nomini 'visible)))
5921 (unless (eq window-1 window-2)
5922 (let* ((height (memq size '(t height)))
5923 (width (memq size '(t width)))
5924 (state-1 (window-state-get window-1))
5925 (width-1 (and width (window-text-width window-1 t)))
5926 (height-1 (and height (window-text-height window-1 t)))
5927 (state-2 (window-state-get window-2))
5928 (width-2 (and width (window-text-width window-2 t)))
5929 (height-2 (and height (window-text-height window-2 t)))
5930 old preserved)
5931 ;; Swap basic states.
5932 (window-state-put state-1 window-2 t)
5933 (window-state-put state-2 window-1 t)
5934 ;; Swap overlays with `window' property.
5935 (with-current-buffer (window-buffer window-1)
5936 (dolist (overlay (overlays-in (point-min) (point-max)))
5937 (let ((window (overlay-get overlay 'window)))
5938 (cond
5939 ((not window))
5940 ((eq window window-1)
5941 (overlay-put overlay 'window window-2))
5942 ((eq window window-2)
5943 (overlay-put overlay 'window window-1))))))
5944 (unless (eq (window-buffer window-1) (window-buffer window-2))
5945 (with-current-buffer (window-buffer window-2)
5946 (dolist (overlay (overlays-in (point-min) (point-max)))
5947 (let ((window (overlay-get overlay 'window)))
5948 (cond
5949 ((not window))
5950 ((eq window window-1)
5951 (overlay-put overlay 'window window-2))
5952 ((eq window window-2)
5953 (overlay-put overlay 'window window-1)))))))
5954 ;; Try to swap window sizes.
5955 (when size
5956 (unless (= (setq old (window-text-width window-1 t)) width-2)
5957 (window-resize-no-error window-1 (- width-2 old) t t t))
5958 (unless (= (setq old (window-text-width window-2 t)) width-1)
5959 (setq preserved (window-preserved-size window-1 t))
5960 (window-preserve-size window-1 t t)
5961 (window-resize-no-error window-2 (- width-1 old) t t t)
5962 (window-preserve-size window-1 t preserved))
5963 (unless (= (setq old (window-text-height window-1 t)) height-2)
5964 (window-resize-no-error window-1 (- height-2 old) nil t t))
5965 (unless (= (setq old (window-text-height window-2 t)) height-1)
5966 (setq preserved (window-preserved-size window-1))
5967 (window-preserve-size window-1 nil t)
5968 (window-resize-no-error window-2 (- height-1 old) nil t t)
5969 (window-preserve-size window-1 nil preserved))))))
5971 (defun display-buffer-record-window (type window buffer)
5972 "Record information for window used by `display-buffer'.
5973 TYPE specifies the type of the calling operation and must be one
5974 of the symbols `reuse' (when WINDOW existed already and was
5975 reused for displaying BUFFER), `window' (when WINDOW was created
5976 on an already existing frame), or `frame' (when WINDOW was
5977 created on a new frame). WINDOW is the window used for or created
5978 by the `display-buffer' routines. BUFFER is the buffer that
5979 shall be displayed.
5981 This function installs or updates the quit-restore parameter of
5982 WINDOW. The quit-restore parameter is a list of four elements:
5983 The first element is one of the symbols `window', `frame', `same' or
5984 `other'. The second element is either one of the symbols `window'
5985 or `frame' or a list whose elements are the buffer previously
5986 shown in the window, that buffer's window start and window point,
5987 and the window's height. The third element is the window
5988 selected at the time the parameter was created. The fourth
5989 element is BUFFER."
5990 (cond
5991 ((eq type 'reuse)
5992 (if (eq (window-buffer window) buffer)
5993 ;; WINDOW shows BUFFER already. Update WINDOW's quit-restore
5994 ;; parameter, if any.
5995 (let ((quit-restore (window-parameter window 'quit-restore)))
5996 (when (consp quit-restore)
5997 (setcar quit-restore 'same)
5998 ;; The selected-window might have changed in
5999 ;; between (Bug#20353).
6000 (unless (or (eq window (selected-window))
6001 (eq window (nth 2 quit-restore)))
6002 (setcar (cddr quit-restore) (selected-window)))))
6003 ;; WINDOW shows another buffer.
6004 (with-current-buffer (window-buffer window)
6005 (set-window-parameter
6006 window 'quit-restore
6007 (list 'other
6008 ;; A quadruple of WINDOW's buffer, start, point and height.
6009 (list (current-buffer) (window-start window)
6010 ;; Preserve window-point-insertion-type (Bug#12588).
6011 (copy-marker
6012 (window-point window) window-point-insertion-type)
6013 (if (window-combined-p window)
6014 (window-total-height window)
6015 (window-total-width window)))
6016 (selected-window) buffer)))))
6017 ((eq type 'window)
6018 ;; WINDOW has been created on an existing frame.
6019 (set-window-parameter
6020 window 'quit-restore
6021 (list 'window 'window (selected-window) buffer)))
6022 ((eq type 'frame)
6023 ;; WINDOW has been created on a new frame.
6024 (set-window-parameter
6025 window 'quit-restore
6026 (list 'frame 'frame (selected-window) buffer)))))
6028 (defcustom display-buffer-function nil
6029 "If non-nil, function to call to handle `display-buffer'.
6030 It will receive two args, the buffer and a flag which if non-nil
6031 means that the currently selected window is not acceptable. It
6032 should choose or create a window, display the specified buffer in
6033 it, and return the window.
6035 The specified function should call `display-buffer-record-window'
6036 with corresponding arguments to set up the quit-restore parameter
6037 of the window used."
6038 :type '(choice
6039 (const nil)
6040 (function :tag "function"))
6041 :group 'windows)
6043 (make-obsolete-variable 'display-buffer-function
6044 'display-buffer-alist "24.3")
6046 ;; Eventually, we want to turn this into a defvar; instead of
6047 ;; customizing this, the user should use a `pop-up-frame-parameters'
6048 ;; alist entry in `display-buffer-base-action'.
6049 (defcustom pop-up-frame-alist nil
6050 "Alist of parameters for automatically generated new frames.
6051 If non-nil, the value you specify here is used by the default
6052 `pop-up-frame-function' for the creation of new frames.
6054 Since `pop-up-frame-function' is used by `display-buffer' for
6055 making new frames, any value specified here by default affects
6056 the automatic generation of new frames via `display-buffer' and
6057 all functions based on it. The behavior of `make-frame' is not
6058 affected by this variable."
6059 :type '(repeat (cons :format "%v"
6060 (symbol :tag "Parameter")
6061 (sexp :tag "Value")))
6062 :group 'frames)
6064 (defcustom pop-up-frame-function
6065 (lambda () (make-frame pop-up-frame-alist))
6066 "Function used by `display-buffer' for creating a new frame.
6067 This function is called with no arguments and should return a new
6068 frame. The default value calls `make-frame' with the argument
6069 `pop-up-frame-alist'."
6070 :type 'function
6071 :group 'frames)
6073 (defcustom special-display-buffer-names nil
6074 "List of names of buffers that should be displayed specially.
6075 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
6076 its name is in this list, displays the buffer in a way specified
6077 by `special-display-function'. `special-display-popup-frame'
6078 \(the default for `special-display-function') usually displays
6079 the buffer in a separate frame made with the parameters specified
6080 by `special-display-frame-alist'. If `special-display-function'
6081 has been set to some other function, that function is called with
6082 the buffer as first, and nil as second argument.
6084 Alternatively, an element of this list can be specified as
6085 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
6086 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
6087 `special-display-popup-frame' will interpret such pairs as frame
6088 parameters when it creates a special frame, overriding the
6089 corresponding values from `special-display-frame-alist'.
6091 As a special case, if FRAME-PARAMETERS contains (same-window . t)
6092 `special-display-popup-frame' displays that buffer in the
6093 selected window. If FRAME-PARAMETERS contains (same-frame . t),
6094 it displays that buffer in a window on the selected frame.
6096 If `special-display-function' specifies some other function than
6097 `special-display-popup-frame', that function is called with the
6098 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
6099 argument.
6101 Finally, an element of this list can be also specified as
6102 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
6103 `special-display-popup-frame' will call FUNCTION with the buffer
6104 named BUFFER-NAME as first argument, and OTHER-ARGS as the
6105 second.
6107 Any alternative function specified here is responsible for
6108 setting up the quit-restore parameter of the window used.
6110 If this variable appears \"not to work\", because you added a
6111 name to it but the corresponding buffer is displayed in the
6112 selected window, look at the values of `same-window-buffer-names'
6113 and `same-window-regexps'. Those variables take precedence over
6114 this one.
6116 See also `special-display-regexps'."
6117 :type '(repeat
6118 (choice :tag "Buffer"
6119 :value ""
6120 (string :format "%v")
6121 (cons :tag "With parameters"
6122 :format "%v"
6123 :value ("" . nil)
6124 (string :format "%v")
6125 (repeat :tag "Parameters"
6126 (cons :format "%v"
6127 (symbol :tag "Parameter")
6128 (sexp :tag "Value"))))
6129 (list :tag "With function"
6130 :format "%v"
6131 :value ("" . nil)
6132 (string :format "%v")
6133 (function :tag "Function")
6134 (repeat :tag "Arguments" (sexp)))))
6135 :group 'windows
6136 :group 'frames)
6137 (make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3")
6138 (put 'special-display-buffer-names 'risky-local-variable t)
6140 (defcustom special-display-regexps nil
6141 "List of regexps saying which buffers should be displayed specially.
6142 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
6143 any regexp in this list matches its name, displays it specially
6144 using `special-display-function'. `special-display-popup-frame'
6145 \(the default for `special-display-function') usually displays
6146 the buffer in a separate frame made with the parameters specified
6147 by `special-display-frame-alist'. If `special-display-function'
6148 has been set to some other function, that function is called with
6149 the buffer as first, and nil as second argument.
6151 Alternatively, an element of this list can be specified as
6152 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
6153 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
6154 `special-display-popup-frame' will then interpret these pairs as
6155 frame parameters when creating a special frame for a buffer whose
6156 name matches REGEXP, overriding the corresponding values from
6157 `special-display-frame-alist'.
6159 As a special case, if FRAME-PARAMETERS contains (same-window . t)
6160 `special-display-popup-frame' displays buffers matching REGEXP in
6161 the selected window. (same-frame . t) in FRAME-PARAMETERS means
6162 to display such buffers in a window on the selected frame.
6164 If `special-display-function' specifies some other function than
6165 `special-display-popup-frame', that function is called with the
6166 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
6167 as second argument.
6169 Finally, an element of this list can be also specified as
6170 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
6171 will then call FUNCTION with the buffer whose name matched
6172 REGEXP as first, and OTHER-ARGS as second argument.
6174 Any alternative function specified here is responsible for
6175 setting up the quit-restore parameter of the window used.
6177 If this variable appears \"not to work\", because you added a
6178 name to it but the corresponding buffer is displayed in the
6179 selected window, look at the values of `same-window-buffer-names'
6180 and `same-window-regexps'. Those variables take precedence over
6181 this one.
6183 See also `special-display-buffer-names'."
6184 :type '(repeat
6185 (choice :tag "Buffer"
6186 :value ""
6187 (regexp :format "%v")
6188 (cons :tag "With parameters"
6189 :format "%v"
6190 :value ("" . nil)
6191 (regexp :format "%v")
6192 (repeat :tag "Parameters"
6193 (cons :format "%v"
6194 (symbol :tag "Parameter")
6195 (sexp :tag "Value"))))
6196 (list :tag "With function"
6197 :format "%v"
6198 :value ("" . nil)
6199 (regexp :format "%v")
6200 (function :tag "Function")
6201 (repeat :tag "Arguments" (sexp)))))
6202 :group 'windows
6203 :group 'frames)
6204 (make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3")
6205 (put 'special-display-regexps 'risky-local-variable t)
6207 (defun special-display-p (buffer-name)
6208 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
6209 More precisely, return t if `special-display-buffer-names' or
6210 `special-display-regexps' contain a string entry equaling or
6211 matching BUFFER-NAME. If `special-display-buffer-names' or
6212 `special-display-regexps' contain a list entry whose car equals
6213 or matches BUFFER-NAME, the return value is the cdr of that
6214 entry."
6215 (let (tmp)
6216 (cond
6217 ((member buffer-name special-display-buffer-names)
6219 ((setq tmp (assoc buffer-name special-display-buffer-names))
6220 (cdr tmp))
6221 ((catch 'found
6222 (dolist (regexp special-display-regexps)
6223 (cond
6224 ((stringp regexp)
6225 (when (string-match-p regexp buffer-name)
6226 (throw 'found t)))
6227 ((and (consp regexp) (stringp (car regexp))
6228 (string-match-p (car regexp) buffer-name))
6229 (throw 'found (cdr regexp))))))))))
6231 (defcustom special-display-frame-alist
6232 '((height . 14) (width . 80) (unsplittable . t))
6233 "Alist of parameters for special frames.
6234 Special frames are used for buffers whose names are listed in
6235 `special-display-buffer-names' and for buffers whose names match
6236 one of the regular expressions in `special-display-regexps'.
6238 This variable can be set in your init file, like this:
6240 (setq special-display-frame-alist \\='((width . 80) (height . 20)))
6242 These supersede the values given in `default-frame-alist'."
6243 :type '(repeat (cons :format "%v"
6244 (symbol :tag "Parameter")
6245 (sexp :tag "Value")))
6246 :group 'frames)
6247 (make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3")
6249 (defun special-display-popup-frame (buffer &optional args)
6250 "Pop up a frame displaying BUFFER and return its window.
6251 If BUFFER is already displayed in a visible or iconified frame,
6252 raise that frame. Otherwise, display BUFFER in a new frame.
6254 Optional argument ARGS is a list specifying additional
6255 information.
6257 If ARGS is an alist, use it as a list of frame parameters. If
6258 these parameters contain (same-window . t), display BUFFER in
6259 the selected window. If they contain (same-frame . t), display
6260 BUFFER in a window of the selected frame.
6262 If ARGS is a list whose car is a symbol, use (car ARGS) as a
6263 function to do the work. Pass it BUFFER as first argument, and
6264 pass the elements of (cdr ARGS) as the remaining arguments."
6265 (if (and args (symbolp (car args)))
6266 (apply (car args) buffer (cdr args))
6267 (let ((window (get-buffer-window buffer 0)))
6269 ;; If we have a window already, make it visible.
6270 (when window
6271 (let ((frame (window-frame window)))
6272 (make-frame-visible frame)
6273 (raise-frame frame)
6274 (display-buffer-record-window 'reuse window buffer)
6275 window))
6276 ;; Reuse the current window if the user requested it.
6277 (when (cdr (assq 'same-window args))
6278 (condition-case nil
6279 (progn (switch-to-buffer buffer nil t) (selected-window))
6280 (error nil)))
6281 ;; Stay on the same frame if requested.
6282 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
6283 (let* ((pop-up-windows t)
6284 pop-up-frames
6285 special-display-buffer-names special-display-regexps)
6286 (display-buffer buffer)))
6287 ;; If no window yet, make one in a new frame.
6288 (let* ((frame
6289 (with-current-buffer buffer
6290 (make-frame (append args special-display-frame-alist))))
6291 (window (frame-selected-window frame)))
6292 (display-buffer-record-window 'frame window buffer)
6293 (unless (eq buffer (window-buffer window))
6294 (set-window-buffer window buffer)
6295 (set-window-prev-buffers window nil))
6296 (set-window-dedicated-p window t)
6297 window)))))
6299 (defcustom special-display-function 'special-display-popup-frame
6300 "Function to call for displaying special buffers.
6301 This function is called with two arguments - the buffer and,
6302 optionally, a list - and should return a window displaying that
6303 buffer. The default value usually makes a separate frame for the
6304 buffer using `special-display-frame-alist' to specify the frame
6305 parameters. See the definition of `special-display-popup-frame'
6306 for how to specify such a function.
6308 A buffer is special when its name is either listed in
6309 `special-display-buffer-names' or matches a regexp in
6310 `special-display-regexps'.
6312 The specified function should call `display-buffer-record-window'
6313 with corresponding arguments to set up the quit-restore parameter
6314 of the window used."
6315 :type 'function
6316 :group 'frames)
6317 (make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3")
6319 (defcustom same-window-buffer-names nil
6320 "List of names of buffers that should appear in the \"same\" window.
6321 `display-buffer' and `pop-to-buffer' show a buffer whose name is
6322 on this list in the selected rather than some other window.
6324 An element of this list can be a cons cell instead of just a
6325 string. In that case, the cell's car must be a string specifying
6326 the buffer name. This is for compatibility with
6327 `special-display-buffer-names'; the cdr of the cons cell is
6328 ignored.
6330 See also `same-window-regexps'."
6331 :type '(repeat (string :format "%v"))
6332 :group 'windows)
6334 (defcustom same-window-regexps nil
6335 "List of regexps saying which buffers should appear in the \"same\" window.
6336 `display-buffer' and `pop-to-buffer' show a buffer whose name
6337 matches a regexp on this list in the selected rather than some
6338 other window.
6340 An element of this list can be a cons cell instead of just a
6341 string. In that case, the cell's car must be a regexp matching
6342 the buffer name. This is for compatibility with
6343 `special-display-regexps'; the cdr of the cons cell is ignored.
6345 See also `same-window-buffer-names'."
6346 :type '(repeat (regexp :format "%v"))
6347 :group 'windows)
6349 (defun same-window-p (buffer-name)
6350 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
6351 This function returns non-nil if `display-buffer' or
6352 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
6353 selected rather than (as usual) some other window. See
6354 `same-window-buffer-names' and `same-window-regexps'."
6355 (cond
6356 ((not (stringp buffer-name)))
6357 ;; The elements of `same-window-buffer-names' can be buffer
6358 ;; names or cons cells whose cars are buffer names.
6359 ((member buffer-name same-window-buffer-names))
6360 ((assoc buffer-name same-window-buffer-names))
6361 ((catch 'found
6362 (dolist (regexp same-window-regexps)
6363 ;; The elements of `same-window-regexps' can be regexps
6364 ;; or cons cells whose cars are regexps.
6365 (when (or (and (stringp regexp)
6366 (string-match-p regexp buffer-name))
6367 (and (consp regexp) (stringp (car regexp))
6368 (string-match-p (car regexp) buffer-name)))
6369 (throw 'found t)))))))
6371 (defcustom pop-up-frames nil
6372 "Whether `display-buffer' should make a separate frame.
6373 If nil, never make a separate frame.
6374 If the value is `graphic-only', make a separate frame
6375 on graphic displays only.
6376 Any other non-nil value means always make a separate frame."
6377 :type '(choice
6378 (const :tag "Never" nil)
6379 (const :tag "On graphic displays only" graphic-only)
6380 (const :tag "Always" t))
6381 :group 'windows)
6383 (defcustom display-buffer-reuse-frames nil
6384 "Non-nil means `display-buffer' should reuse frames.
6385 If the buffer in question is already displayed in a frame, raise
6386 that frame."
6387 :type 'boolean
6388 :version "21.1"
6389 :group 'windows)
6391 (make-obsolete-variable
6392 'display-buffer-reuse-frames
6393 "use a `reusable-frames' alist entry in `display-buffer-alist'."
6394 "24.3")
6396 (defcustom pop-up-windows t
6397 "Non-nil means `display-buffer' should make a new window."
6398 :type 'boolean
6399 :group 'windows)
6401 (defcustom split-window-preferred-function 'split-window-sensibly
6402 "Function called by `display-buffer' routines to split a window.
6403 This function is called with a window as single argument and is
6404 supposed to split that window and return the new window. If the
6405 window can (or shall) not be split, it is supposed to return nil.
6406 The default is to call the function `split-window-sensibly' which
6407 tries to split the window in a way which seems most suitable.
6408 You can customize the options `split-height-threshold' and/or
6409 `split-width-threshold' in order to have `split-window-sensibly'
6410 prefer either vertical or horizontal splitting.
6412 If you set this to any other function, bear in mind that the
6413 `display-buffer' routines may call this function two times. The
6414 argument of the first call is the largest window on its frame.
6415 If that call fails to return a live window, the function is
6416 called again with the least recently used window as argument. If
6417 that call fails too, `display-buffer' will use an existing window
6418 to display its buffer.
6420 The window selected at the time `display-buffer' was invoked is
6421 still selected when this function is called. Hence you can
6422 compare the window argument with the value of `selected-window'
6423 if you intend to split the selected window instead or if you do
6424 not want to split the selected window."
6425 :type 'function
6426 :version "23.1"
6427 :group 'windows)
6429 (defcustom split-height-threshold 80
6430 "Minimum height for splitting windows sensibly.
6431 If this is an integer, `split-window-sensibly' may split a window
6432 vertically only if it has at least this many lines. If this is
6433 nil, `split-window-sensibly' is not allowed to split a window
6434 vertically. If, however, a window is the only window on its
6435 frame, `split-window-sensibly' may split it vertically
6436 disregarding the value of this variable."
6437 :type '(choice (const nil) (integer :tag "lines"))
6438 :version "23.1"
6439 :group 'windows)
6441 (defcustom split-width-threshold 160
6442 "Minimum width for splitting windows sensibly.
6443 If this is an integer, `split-window-sensibly' may split a window
6444 horizontally only if it has at least this many columns. If this
6445 is nil, `split-window-sensibly' is not allowed to split a window
6446 horizontally."
6447 :type '(choice (const nil) (integer :tag "columns"))
6448 :version "23.1"
6449 :group 'windows)
6451 (defun window-splittable-p (window &optional horizontal)
6452 "Return non-nil if `split-window-sensibly' may split WINDOW.
6453 Optional argument HORIZONTAL nil or omitted means check whether
6454 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
6455 non-nil means check whether WINDOW may be split horizontally.
6457 WINDOW may be split vertically when the following conditions
6458 hold:
6459 - `window-size-fixed' is either nil or equals `width' for the
6460 buffer of WINDOW.
6461 - `split-height-threshold' is an integer and WINDOW is at least as
6462 high as `split-height-threshold'.
6463 - When WINDOW is split evenly, the emanating windows are at least
6464 `window-min-height' lines tall and can accommodate at least one
6465 line plus - if WINDOW has one - a mode line.
6467 WINDOW may be split horizontally when the following conditions
6468 hold:
6469 - `window-size-fixed' is either nil or equals `height' for the
6470 buffer of WINDOW.
6471 - `split-width-threshold' is an integer and WINDOW is at least as
6472 wide as `split-width-threshold'.
6473 - When WINDOW is split evenly, the emanating windows are at least
6474 `window-min-width' or two (whichever is larger) columns wide."
6475 (when (and (window-live-p window)
6476 (not (window-parameter window 'window-side)))
6477 (with-current-buffer (window-buffer window)
6478 (if horizontal
6479 ;; A window can be split horizontally when its width is not
6480 ;; fixed, it is at least `split-width-threshold' columns wide
6481 ;; and at least twice as wide as `window-min-width' and 2 (the
6482 ;; latter value is hardcoded).
6483 (and (memq window-size-fixed '(nil height))
6484 ;; Testing `window-full-width-p' here hardly makes any
6485 ;; sense nowadays. This can be done more intuitively by
6486 ;; setting up `split-width-threshold' appropriately.
6487 (numberp split-width-threshold)
6488 (>= (window-width window)
6489 (max split-width-threshold
6490 (* 2 (max window-min-width 2)))))
6491 ;; A window can be split vertically when its height is not
6492 ;; fixed, it is at least `split-height-threshold' lines high,
6493 ;; and it is at least twice as high as `window-min-height' and 2
6494 ;; if it has a mode line or 1.
6495 (and (memq window-size-fixed '(nil width))
6496 (numberp split-height-threshold)
6497 (>= (window-height window)
6498 (max split-height-threshold
6499 (* 2 (max window-min-height
6500 (if mode-line-format 2 1))))))))))
6502 (defun split-window-sensibly (&optional window)
6503 "Split WINDOW in a way suitable for `display-buffer'.
6504 WINDOW defaults to the currently selected window.
6505 If `split-height-threshold' specifies an integer, WINDOW is at
6506 least `split-height-threshold' lines tall and can be split
6507 vertically, split WINDOW into two windows one above the other and
6508 return the lower window. Otherwise, if `split-width-threshold'
6509 specifies an integer, WINDOW is at least `split-width-threshold'
6510 columns wide and can be split horizontally, split WINDOW into two
6511 windows side by side and return the window on the right. If this
6512 can't be done either and WINDOW is the only window on its frame,
6513 try to split WINDOW vertically disregarding any value specified
6514 by `split-height-threshold'. If that succeeds, return the lower
6515 window. Return nil otherwise.
6517 By default `display-buffer' routines call this function to split
6518 the largest or least recently used window. To change the default
6519 customize the option `split-window-preferred-function'.
6521 You can enforce this function to not split WINDOW horizontally,
6522 by setting (or binding) the variable `split-width-threshold' to
6523 nil. If, in addition, you set `split-height-threshold' to zero,
6524 chances increase that this function does split WINDOW vertically.
6526 In order to not split WINDOW vertically, set (or bind) the
6527 variable `split-height-threshold' to nil. Additionally, you can
6528 set `split-width-threshold' to zero to make a horizontal split
6529 more likely to occur.
6531 Have a look at the function `window-splittable-p' if you want to
6532 know how `split-window-sensibly' determines whether WINDOW can be
6533 split."
6534 (let ((window (or window (selected-window))))
6535 (or (and (window-splittable-p window)
6536 ;; Split window vertically.
6537 (with-selected-window window
6538 (split-window-below)))
6539 (and (window-splittable-p window t)
6540 ;; Split window horizontally.
6541 (with-selected-window window
6542 (split-window-right)))
6543 (and (eq window (frame-root-window (window-frame window)))
6544 (not (window-minibuffer-p window))
6545 ;; If WINDOW is the only window on its frame and is not the
6546 ;; minibuffer window, try to split it vertically disregarding
6547 ;; the value of `split-height-threshold'.
6548 (let ((split-height-threshold 0))
6549 (when (window-splittable-p window)
6550 (with-selected-window window
6551 (split-window-below))))))))
6553 (defun window--try-to-split-window (window &optional alist)
6554 "Try to split WINDOW.
6555 Return value returned by `split-window-preferred-function' if it
6556 represents a live window, nil otherwise."
6557 (and (window-live-p window)
6558 (not (frame-parameter (window-frame window) 'unsplittable))
6559 (let* ((window-combination-limit
6560 ;; When `window-combination-limit' equals
6561 ;; `display-buffer' or equals `resize-window' and a
6562 ;; `window-height' or `window-width' alist entry are
6563 ;; present, bind it to t so resizing steals space
6564 ;; preferably from the window that was split.
6565 (if (or (eq window-combination-limit 'display-buffer)
6566 (and (eq window-combination-limit 'window-size)
6567 (or (cdr (assq 'window-height alist))
6568 (cdr (assq 'window-width alist)))))
6570 window-combination-limit))
6571 (new-window
6572 ;; Since `split-window-preferred-function' might
6573 ;; throw an error use `condition-case'.
6574 (condition-case nil
6575 (funcall split-window-preferred-function window)
6576 (error nil))))
6577 (and (window-live-p new-window) new-window))))
6579 (defun window--frame-usable-p (frame)
6580 "Return FRAME if it can be used to display a buffer."
6581 (when (frame-live-p frame)
6582 (let ((window (frame-root-window frame)))
6583 ;; `frame-root-window' may be an internal window which is considered
6584 ;; "dead" by `window-live-p'. Hence if `window' is not live we
6585 ;; implicitly know that `frame' has a visible window we can use.
6586 (unless (and (window-live-p window)
6587 (or (window-minibuffer-p window)
6588 ;; If the window is soft-dedicated, the frame is usable.
6589 ;; Actually, even if the window is really dedicated,
6590 ;; the frame is still usable by splitting it.
6591 ;; At least Emacs-22 allowed it, and it is desirable
6592 ;; when displaying same-frame windows.
6593 nil ; (eq t (window-dedicated-p window))
6595 frame))))
6597 (defcustom even-window-sizes t
6598 "If non-nil `display-buffer' will try to even window sizes.
6599 Otherwise `display-buffer' will leave the window configuration
6600 alone. Special values are `height-only' to even heights only and
6601 `width-only' to even widths only. Any other value means to even
6602 any of them."
6603 :type '(choice
6604 (const :tag "Never" nil)
6605 (const :tag "Side-by-side windows only" width-only)
6606 (const :tag "Windows above or below only" height-only)
6607 (const :tag "Always" t))
6608 :version "25.1"
6609 :group 'windows)
6610 (defvaralias 'even-window-heights 'even-window-sizes)
6612 (defun window--even-window-sizes (window)
6613 "Even sizes of WINDOW and selected window.
6614 Even only if these windows are the only children of their parent,
6615 `even-window-sizes' has the appropriate value and the selected
6616 window is larger than WINDOW."
6617 (when (and (= (window-child-count (window-parent window)) 2)
6618 (eq (window-parent) (window-parent window)))
6619 (cond
6620 ((and (not (memq even-window-sizes '(nil height-only)))
6621 (window-combined-p window t)
6622 (> (window-total-width) (window-total-width window)))
6623 (condition-case nil
6624 (enlarge-window
6625 (/ (- (window-total-width window) (window-total-width)) 2) t)
6626 (error nil)))
6627 ((and (not (memq even-window-sizes '(nil width-only)))
6628 (window-combined-p window)
6629 (> (window-total-height) (window-total-height window)))
6630 (condition-case nil
6631 (enlarge-window
6632 (/ (- (window-total-height window) (window-total-height)) 2))
6633 (error nil))))))
6635 (defun window--display-buffer (buffer window type &optional alist dedicated)
6636 "Display BUFFER in WINDOW.
6637 TYPE must be one of the symbols `reuse', `window' or `frame' and
6638 is passed unaltered to `display-buffer-record-window'. ALIST is
6639 the alist argument of `display-buffer'. Set `window-dedicated-p'
6640 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
6641 live."
6642 (when (and (buffer-live-p buffer) (window-live-p window))
6643 (display-buffer-record-window type window buffer)
6644 (unless (eq buffer (window-buffer window))
6645 (set-window-dedicated-p window nil)
6646 (set-window-buffer window buffer)
6647 (when dedicated
6648 (set-window-dedicated-p window dedicated))
6649 (when (memq type '(window frame))
6650 (set-window-prev-buffers window nil)))
6651 (let ((quit-restore (window-parameter window 'quit-restore))
6652 (height (cdr (assq 'window-height alist)))
6653 (width (cdr (assq 'window-width alist)))
6654 (size (cdr (assq 'window-size alist)))
6655 (preserve-size (cdr (assq 'preserve-size alist))))
6656 (cond
6657 ((or (eq type 'frame)
6658 (and (eq (car quit-restore) 'same)
6659 (eq (nth 1 quit-restore) 'frame)))
6660 ;; Adjust size of frame if asked for.
6661 (cond
6662 ((not size))
6663 ((consp size)
6664 (let ((width (car size))
6665 (height (cdr size))
6666 (frame (window-frame window)))
6667 (when (and (numberp width) (numberp height))
6668 (set-frame-height
6669 frame (+ (frame-height frame)
6670 (- height (window-total-height window))))
6671 (set-frame-width
6672 frame (+ (frame-width frame)
6673 (- width (window-total-width window)))))))
6674 ((functionp size)
6675 (ignore-errors (funcall size window)))))
6676 ((or (eq type 'window)
6677 (and (eq (car quit-restore) 'same)
6678 (eq (nth 1 quit-restore) 'window)))
6679 ;; Adjust height of window if asked for.
6680 (cond
6681 ((not height))
6682 ((numberp height)
6683 (let* ((new-height
6684 (if (integerp height)
6685 height
6686 (round
6687 (* (window-total-height (frame-root-window window))
6688 height))))
6689 (delta (- new-height (window-total-height window))))
6690 (when (and (window--resizable-p window delta nil 'safe)
6691 (window-combined-p window))
6692 (window-resize window delta nil 'safe))))
6693 ((functionp height)
6694 (ignore-errors (funcall height window))))
6695 ;; Adjust width of window if asked for.
6696 (cond
6697 ((not width))
6698 ((numberp width)
6699 (let* ((new-width
6700 (if (integerp width)
6701 width
6702 (round
6703 (* (window-total-width (frame-root-window window))
6704 width))))
6705 (delta (- new-width (window-total-width window))))
6706 (when (and (window--resizable-p window delta t 'safe)
6707 (window-combined-p window t))
6708 (window-resize window delta t 'safe))))
6709 ((functionp width)
6710 (ignore-errors (funcall width window))))
6711 ;; Preserve window size if asked for.
6712 (when (consp preserve-size)
6713 (window-preserve-size window t (car preserve-size))
6714 (window-preserve-size window nil (cdr preserve-size)))))
6715 ;; Assign any window parameters specified.
6716 (let ((parameters (cdr (assq 'window-parameters alist))))
6717 (dolist (parameter parameters)
6718 (set-window-parameter
6719 window (car parameter) (cdr parameter)))))
6720 window))
6722 (defun window--maybe-raise-frame (frame)
6723 (let ((visible (frame-visible-p frame)))
6724 (unless (or (not visible)
6725 ;; Assume the selected frame is already visible enough.
6726 (eq frame (selected-frame))
6727 ;; Assume the frame from which we invoked the
6728 ;; minibuffer is visible.
6729 (and (minibuffer-window-active-p (selected-window))
6730 (eq frame (window-frame (minibuffer-selected-window)))))
6731 (raise-frame frame))))
6733 ;; FIXME: Not implemented.
6734 ;; FIXME: By the way, there could be more levels of dedication:
6735 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
6736 ;; the window hasn't been used for something else yet.
6737 ;; - `soft' (`softly') dedicated only allows reuse when asked explicitly.
6738 ;; - `strongly' never allows reuse.
6739 (defvar display-buffer-mark-dedicated nil
6740 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
6741 The actual non-nil value of this variable will be copied to the
6742 `window-dedicated-p' flag.")
6744 (defconst display-buffer--action-function-custom-type
6745 '(choice :tag "Function"
6746 (const :tag "--" ignore) ; default for insertion
6747 (const display-buffer-reuse-window)
6748 (const display-buffer-pop-up-window)
6749 (const display-buffer-same-window)
6750 (const display-buffer-pop-up-frame)
6751 (const display-buffer-below-selected)
6752 (const display-buffer-at-bottom)
6753 (const display-buffer-in-previous-window)
6754 (const display-buffer-use-some-window)
6755 (const display-buffer-use-some-frame)
6756 (function :tag "Other function"))
6757 "Custom type for `display-buffer' action functions.")
6759 (defconst display-buffer--action-custom-type
6760 `(cons :tag "Action"
6761 (choice :tag "Action functions"
6762 ,display-buffer--action-function-custom-type
6763 (repeat
6764 :tag "List of functions"
6765 ,display-buffer--action-function-custom-type))
6766 (alist :tag "Action arguments"
6767 :key-type symbol
6768 :value-type (sexp :tag "Value")))
6769 "Custom type for `display-buffer' actions.")
6771 (defvar display-buffer-overriding-action '(nil . nil)
6772 "Overriding action to perform to display a buffer.
6773 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6774 function or a list of functions. Each function should accept two
6775 arguments: a buffer to display and an alist similar to ALIST.
6776 See `display-buffer' for details.")
6777 (put 'display-buffer-overriding-action 'risky-local-variable t)
6779 (defcustom display-buffer-alist nil
6780 "Alist of conditional actions for `display-buffer'.
6781 This is a list of elements (CONDITION . ACTION), where:
6783 CONDITION is either a regexp matching buffer names, or a
6784 function that takes two arguments - a buffer name and the
6785 ACTION argument of `display-buffer' - and returns a boolean.
6787 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
6788 function or a list of functions. Each such function should
6789 accept two arguments: a buffer to display and an alist of the
6790 same form as ALIST. See `display-buffer' for details.
6792 `display-buffer' scans this alist until it either finds a
6793 matching regular expression or the function specified by a
6794 condition returns non-nil. In any of these cases, it adds the
6795 associated action to the list of actions it will try."
6796 :type `(alist :key-type
6797 (choice :tag "Condition"
6798 regexp
6799 (function :tag "Matcher function"))
6800 :value-type ,display-buffer--action-custom-type)
6801 :risky t
6802 :version "24.1"
6803 :group 'windows)
6805 (defcustom display-buffer-base-action '(nil . nil)
6806 "User-specified default action for `display-buffer'.
6807 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6808 function or a list of functions. Each function should accept two
6809 arguments: a buffer to display and an alist similar to ALIST.
6810 See `display-buffer' for details."
6811 :type display-buffer--action-custom-type
6812 :risky t
6813 :version "24.1"
6814 :group 'windows)
6816 (defconst display-buffer-fallback-action
6817 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
6818 display-buffer-reuse-window
6819 display-buffer--maybe-pop-up-frame-or-window
6820 display-buffer-in-previous-window
6821 display-buffer-use-some-window
6822 ;; If all else fails, pop up a new frame.
6823 display-buffer-pop-up-frame))
6824 "Default fallback action for `display-buffer'.
6825 This is the action used by `display-buffer' if no other actions
6826 specified, e.g. by the user options `display-buffer-alist' or
6827 `display-buffer-base-action'. See `display-buffer'.")
6828 (put 'display-buffer-fallback-action 'risky-local-variable t)
6830 (defun display-buffer-assq-regexp (buffer-name alist action)
6831 "Retrieve ALIST entry corresponding to BUFFER-NAME.
6832 ACTION is the action argument passed to `display-buffer'."
6833 (catch 'match
6834 (dolist (entry alist)
6835 (let ((key (car entry)))
6836 (when (or (and (stringp key)
6837 (string-match-p key buffer-name))
6838 (and (functionp key)
6839 (funcall key buffer-name action)))
6840 (throw 'match (cdr entry)))))))
6842 (defvar display-buffer--same-window-action
6843 '(display-buffer-same-window
6844 (inhibit-same-window . nil))
6845 "A `display-buffer' action for displaying in the same window.")
6846 (put 'display-buffer--same-window-action 'risky-local-variable t)
6848 (defvar display-buffer--other-frame-action
6849 '((display-buffer-reuse-window
6850 display-buffer-pop-up-frame)
6851 (reusable-frames . 0)
6852 (inhibit-same-window . t))
6853 "A `display-buffer' action for displaying in another frame.")
6854 (put 'display-buffer--other-frame-action 'risky-local-variable t)
6856 (defun display-buffer (buffer-or-name &optional action frame)
6857 "Display BUFFER-OR-NAME in some window, without selecting it.
6858 BUFFER-OR-NAME must be a buffer or the name of an existing
6859 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
6860 or nil if no such window is found.
6862 Optional argument ACTION, if non-nil, should specify a display
6863 action. Its form is described below.
6865 Optional argument FRAME, if non-nil, acts like an additional
6866 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
6867 specifying the frame(s) to search for a window that is already
6868 displaying the buffer. See `display-buffer-reuse-window'.
6870 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
6871 where FUNCTION is either a function or a list of functions, and
6872 ALIST is an arbitrary association list (alist).
6874 Each such FUNCTION should accept two arguments: the buffer to
6875 display and an alist. Based on those arguments, it should
6876 display the buffer and return the window. If the caller is
6877 prepared to handle the case of not displaying the buffer
6878 and returning nil from `display-buffer' it should pass
6879 \(allow-no-window . t) as an element of the ALIST.
6881 The `display-buffer' function builds a function list and an alist
6882 by combining the functions and alists specified in
6883 `display-buffer-overriding-action', `display-buffer-alist', the
6884 ACTION argument, `display-buffer-base-action', and
6885 `display-buffer-fallback-action' (in order). Then it calls each
6886 function in the combined function list in turn, passing the
6887 buffer as the first argument and the combined alist as the second
6888 argument, until one of the functions returns non-nil.
6890 If ACTION is nil, the function list and the alist are built using
6891 only the other variables mentioned above.
6893 Available action functions include:
6894 `display-buffer-same-window'
6895 `display-buffer-reuse-window'
6896 `display-buffer-pop-up-frame'
6897 `display-buffer-pop-up-window'
6898 `display-buffer-in-previous-window'
6899 `display-buffer-use-some-window'
6900 `display-buffer-use-some-frame'
6902 Recognized alist entries include:
6904 `inhibit-same-window' -- A non-nil value prevents the same
6905 window from being used for display.
6907 `inhibit-switch-frame' -- A non-nil value prevents any other
6908 frame from being raised or selected,
6909 even if the window is displayed there.
6911 `reusable-frames' -- Value specifies frame(s) to search for a
6912 window that already displays the buffer.
6913 See `display-buffer-reuse-window'.
6915 `pop-up-frame-parameters' -- Value specifies an alist of frame
6916 parameters to give a new frame, if
6917 one is created.
6919 `window-height' -- Value specifies either an integer (the number
6920 of lines of a new window), a floating point number (the
6921 fraction of a new window with respect to the height of the
6922 frame's root window) or a function to be called with one
6923 argument - a new window. The function is supposed to adjust
6924 the height of the window; its return value is ignored.
6925 Suitable functions are `shrink-window-if-larger-than-buffer'
6926 and `fit-window-to-buffer'.
6928 `window-width' -- Value specifies either an integer (the number
6929 of columns of a new window), a floating point number (the
6930 fraction of a new window with respect to the width of the
6931 frame's root window) or a function to be called with one
6932 argument - a new window. The function is supposed to adjust
6933 the width of the window; its return value is ignored.
6935 `allow-no-window' -- A non-nil value indicates readiness for the case
6936 of not displaying the buffer and FUNCTION can safely return
6937 a non-window value to suppress displaying.
6939 `preserve-size' -- Value should be either (t . nil) to
6940 preserve the width of the window, (nil . t) to preserve its
6941 height or (t . t) to preserve both.
6943 `window-parameters' -- Value specifies an alist of window
6944 parameters to give the chosen window.
6946 The ACTION argument to `display-buffer' can also have a non-nil
6947 and non-list value. This means to display the buffer in a window
6948 other than the selected one, even if it is already displayed in
6949 the selected window. If called interactively with a prefix
6950 argument, ACTION is t."
6951 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6952 (if current-prefix-arg t)))
6953 (let ((buffer (if (bufferp buffer-or-name)
6954 buffer-or-name
6955 (get-buffer buffer-or-name)))
6956 ;; Make sure that when we split windows the old window keeps
6957 ;; point, bug#14829.
6958 (split-window-keep-point t)
6959 ;; Handle the old form of the first argument.
6960 (inhibit-same-window (and action (not (listp action)))))
6961 (unless (listp action) (setq action nil))
6962 (if display-buffer-function
6963 ;; If `display-buffer-function' is defined, let it do the job.
6964 (funcall display-buffer-function buffer inhibit-same-window)
6965 ;; Otherwise, use the defined actions.
6966 (let* ((user-action
6967 (display-buffer-assq-regexp
6968 (buffer-name buffer) display-buffer-alist action))
6969 (special-action (display-buffer--special-action buffer))
6970 ;; Extra actions from the arguments to this function:
6971 (extra-action
6972 (cons nil (append (if inhibit-same-window
6973 '((inhibit-same-window . t)))
6974 (if frame
6975 `((reusable-frames . ,frame))))))
6976 ;; Construct action function list and action alist.
6977 (actions (list display-buffer-overriding-action
6978 user-action special-action action extra-action
6979 display-buffer-base-action
6980 display-buffer-fallback-action))
6981 (functions (apply 'append
6982 (mapcar (lambda (x)
6983 (setq x (car x))
6984 (if (functionp x) (list x) x))
6985 actions)))
6986 (alist (apply 'append (mapcar 'cdr actions)))
6987 window)
6988 (unless (buffer-live-p buffer)
6989 (error "Invalid buffer"))
6990 (while (and functions (not window))
6991 (setq window (funcall (car functions) buffer alist)
6992 functions (cdr functions)))
6993 (and (windowp window) window)))))
6995 (defun display-buffer-other-frame (buffer)
6996 "Display buffer BUFFER preferably in another frame.
6997 This uses the function `display-buffer' as a subroutine; see
6998 its documentation for additional customization information."
6999 (interactive "BDisplay buffer in other frame: ")
7000 (display-buffer buffer display-buffer--other-frame-action t))
7002 ;;; `display-buffer' action functions:
7004 (defun display-buffer-use-some-frame (buffer alist)
7005 "Display BUFFER in an existing frame that meets a predicate
7006 \(by default any frame other than the current frame). If
7007 successful, return the window used; otherwise return nil.
7009 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
7010 raising the frame.
7012 If ALIST has a non-nil `frame-predicate' entry, its value is a
7013 function taking one argument (a frame), returning non-nil if the
7014 frame is a candidate; this function replaces the default
7015 predicate.
7017 If ALIST has a non-nil `inhibit-same-window' entry, avoid using
7018 the currently selected window (only useful with a frame-predicate
7019 that allows the selected frame)."
7020 (let* ((predicate (or (cdr (assq 'frame-predicate alist))
7021 (lambda (frame)
7022 (and
7023 (not (eq frame (selected-frame)))
7024 (not (window-dedicated-p
7026 (get-lru-window frame)
7027 (frame-first-window frame)))))
7029 (frame (car (filtered-frame-list predicate)))
7030 (window (and frame (get-lru-window frame nil (cdr (assq 'inhibit-same-window alist))))))
7031 (when window
7032 (prog1
7033 (window--display-buffer
7034 buffer window 'frame alist display-buffer-mark-dedicated)
7035 (unless (cdr (assq 'inhibit-switch-frame alist))
7036 (window--maybe-raise-frame frame))))))
7038 (defun display-buffer-same-window (buffer alist)
7039 "Display BUFFER in the selected window.
7040 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
7041 if the selected window is a minibuffer window or is dedicated to
7042 another buffer; in that case, return nil. Otherwise, return the
7043 selected window."
7044 (unless (or (cdr (assq 'inhibit-same-window alist))
7045 (window-minibuffer-p)
7046 (window-dedicated-p))
7047 (window--display-buffer buffer (selected-window) 'reuse alist)))
7049 (defun display-buffer--maybe-same-window (buffer alist)
7050 "Conditionally display BUFFER in the selected window.
7051 If `same-window-p' returns non-nil for BUFFER's name, call
7052 `display-buffer-same-window' and return its value. Otherwise,
7053 return nil."
7054 (and (same-window-p (buffer-name buffer))
7055 (display-buffer-same-window buffer alist)))
7057 (defun display-buffer-reuse-window (buffer alist)
7058 "Return a window that is already displaying BUFFER.
7059 Return nil if no usable window is found.
7061 If ALIST has a non-nil `inhibit-same-window' entry, the selected
7062 window is not eligible for reuse.
7064 If ALIST contains a `reusable-frames' entry, its value determines
7065 which frames to search for a reusable window:
7066 nil -- the selected frame (actually the last non-minibuffer frame)
7067 A frame -- just that frame
7068 `visible' -- all visible frames
7069 0 -- all frames on the current terminal
7070 t -- all frames.
7072 If ALIST contains no `reusable-frames' entry, search just the
7073 selected frame if `display-buffer-reuse-frames' and
7074 `pop-up-frames' are both nil; search all frames on the current
7075 terminal if either of those variables is non-nil.
7077 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7078 event that a window on another frame is chosen, avoid raising
7079 that frame."
7080 (let* ((alist-entry (assq 'reusable-frames alist))
7081 (frames (cond (alist-entry (cdr alist-entry))
7082 ((if (eq pop-up-frames 'graphic-only)
7083 (display-graphic-p)
7084 pop-up-frames)
7086 (display-buffer-reuse-frames 0)
7087 (t (last-nonminibuffer-frame))))
7088 (window (if (and (eq buffer (window-buffer))
7089 (not (cdr (assq 'inhibit-same-window alist))))
7090 (selected-window)
7091 (car (delq (selected-window)
7092 (get-buffer-window-list buffer 'nomini
7093 frames))))))
7094 (when (window-live-p window)
7095 (prog1 (window--display-buffer buffer window 'reuse alist)
7096 (unless (cdr (assq 'inhibit-switch-frame alist))
7097 (window--maybe-raise-frame (window-frame window)))))))
7099 (defun display-buffer-reuse-mode-window (buffer alist)
7100 "Return a window based on the mode of the buffer it displays.
7101 Display BUFFER in the returned window. Return nil if no usable
7102 window is found.
7104 If ALIST contains a `mode' entry, its value is a major mode (a
7105 symbol) or a list of modes. A window is a candidate if it
7106 displays a buffer that derives from one of the given modes. When
7107 ALIST contains no `mode' entry, the current major mode of BUFFER
7108 is used.
7110 The behavior is also controlled by entries for
7111 `inhibit-same-window', `reusable-frames' and
7112 `inhibit-switch-frame' as is done in the function
7113 `display-buffer-reuse-window'."
7114 (let* ((alist-entry (assq 'reusable-frames alist))
7115 (alist-mode-entry (assq 'mode alist))
7116 (frames (cond (alist-entry (cdr alist-entry))
7117 ((if (eq pop-up-frames 'graphic-only)
7118 (display-graphic-p)
7119 pop-up-frames)
7121 (display-buffer-reuse-frames 0)
7122 (t (last-nonminibuffer-frame))))
7123 (inhibit-same-window-p (cdr (assq 'inhibit-same-window alist)))
7124 (windows (window-list-1 nil 'nomini frames))
7125 (buffer-mode (with-current-buffer buffer major-mode))
7126 (allowed-modes (if alist-mode-entry
7127 (cdr alist-mode-entry)
7128 buffer-mode))
7129 (curwin (selected-window))
7130 (curframe (selected-frame)))
7131 (unless (listp allowed-modes)
7132 (setq allowed-modes (list allowed-modes)))
7133 (let (same-mode-same-frame
7134 same-mode-other-frame
7135 derived-mode-same-frame
7136 derived-mode-other-frame)
7137 (dolist (window windows)
7138 (let ((mode?
7139 (with-current-buffer (window-buffer window)
7140 (cond ((memq major-mode allowed-modes)
7141 'same)
7142 ((derived-mode-p allowed-modes)
7143 'derived)))))
7144 (when (and mode?
7145 (not (and inhibit-same-window-p
7146 (eq window curwin))))
7147 (push window (if (eq curframe (window-frame window))
7148 (if (eq mode? 'same)
7149 same-mode-same-frame
7150 derived-mode-same-frame)
7151 (if (eq mode? 'same)
7152 same-mode-other-frame
7153 derived-mode-other-frame))))))
7154 (let ((window (car (nconc same-mode-same-frame
7155 same-mode-other-frame
7156 derived-mode-same-frame
7157 derived-mode-other-frame))))
7158 (when (window-live-p window)
7159 (prog1 (window--display-buffer buffer window 'reuse alist)
7160 (unless (cdr (assq 'inhibit-switch-frame alist))
7161 (window--maybe-raise-frame (window-frame window)))))))))
7163 (defun display-buffer--special-action (buffer)
7164 "Return special display action for BUFFER, if any.
7165 If `special-display-p' returns non-nil for BUFFER, return an
7166 appropriate display action involving `special-display-function'.
7167 See `display-buffer' for the format of display actions."
7168 (and special-display-function
7169 ;; `special-display-p' returns either t or a list of frame
7170 ;; parameters to pass to `special-display-function'.
7171 (let ((pars (special-display-p (buffer-name buffer))))
7172 (when pars
7173 (list (list #'display-buffer-reuse-window
7174 `(lambda (buffer _alist)
7175 (funcall special-display-function
7176 buffer ',(if (listp pars) pars)))))))))
7178 (defun display-buffer-pop-up-frame (buffer alist)
7179 "Display BUFFER in a new frame.
7180 This works by calling `pop-up-frame-function'. If successful,
7181 return the window used; otherwise return nil.
7183 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
7184 raising the new frame.
7186 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
7187 corresponding value is an alist of frame parameters to give the
7188 new frame."
7189 (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
7190 (pop-up-frame-alist (append params pop-up-frame-alist))
7191 (fun pop-up-frame-function)
7192 frame window)
7193 (when (and fun
7194 ;; Make BUFFER current so `make-frame' will use it as the
7195 ;; new frame's buffer (Bug#15133).
7196 (with-current-buffer buffer
7197 (setq frame (funcall fun)))
7198 (setq window (frame-selected-window frame)))
7199 (prog1 (window--display-buffer
7200 buffer window 'frame alist display-buffer-mark-dedicated)
7201 (unless (cdr (assq 'inhibit-switch-frame alist))
7202 (window--maybe-raise-frame frame))))))
7204 (defun display-buffer-pop-up-window (buffer alist)
7205 "Display BUFFER by popping up a new window.
7206 The new window is created on the selected frame, or in
7207 `last-nonminibuffer-frame' if no windows can be created there.
7208 If successful, return the new window; otherwise return nil.
7210 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7211 event that the new window is created on another frame, avoid
7212 raising the frame."
7213 (let ((frame (or (window--frame-usable-p (selected-frame))
7214 (window--frame-usable-p (last-nonminibuffer-frame))))
7215 window)
7216 (when (and (or (not (frame-parameter frame 'unsplittable))
7217 ;; If the selected frame cannot be split, look at
7218 ;; `last-nonminibuffer-frame'.
7219 (and (eq frame (selected-frame))
7220 (setq frame (last-nonminibuffer-frame))
7221 (window--frame-usable-p frame)
7222 (not (frame-parameter frame 'unsplittable))))
7223 ;; Attempt to split largest or least recently used window.
7224 (setq window (or (window--try-to-split-window
7225 (get-largest-window frame t) alist)
7226 (window--try-to-split-window
7227 (get-lru-window frame t) alist))))
7228 (prog1 (window--display-buffer
7229 buffer window 'window alist display-buffer-mark-dedicated)
7230 (unless (cdr (assq 'inhibit-switch-frame alist))
7231 (window--maybe-raise-frame (window-frame window)))))))
7233 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
7234 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
7235 If `pop-up-frames' is non-nil (and not `graphic-only' on a
7236 text-only terminal), try with `display-buffer-pop-up-frame'.
7238 If that cannot be done, and `pop-up-windows' is non-nil, try
7239 again with `display-buffer-pop-up-window'."
7240 (or (and (if (eq pop-up-frames 'graphic-only)
7241 (display-graphic-p)
7242 pop-up-frames)
7243 (display-buffer-pop-up-frame buffer alist))
7244 (and pop-up-windows
7245 (display-buffer-pop-up-window buffer alist))))
7247 (defun display-buffer-below-selected (buffer alist)
7248 "Try displaying BUFFER in a window below the selected window.
7249 If there is a window below the selected one and that window
7250 already displays BUFFER, use that window. Otherwise, try to
7251 create a new window below the selected one and show BUFFER there.
7252 If that attempt fails as well and there is a non-dedicated window
7253 below the selected one, use that window."
7254 (let (window)
7255 (or (and (setq window (window-in-direction 'below))
7256 (eq buffer (window-buffer window))
7257 (window--display-buffer buffer window 'reuse alist))
7258 (and (not (frame-parameter nil 'unsplittable))
7259 (let ((split-height-threshold 0)
7260 split-width-threshold)
7261 (setq window (window--try-to-split-window (selected-window) alist)))
7262 (window--display-buffer
7263 buffer window 'window alist display-buffer-mark-dedicated))
7264 (and (setq window (window-in-direction 'below))
7265 (not (window-dedicated-p window))
7266 (window--display-buffer
7267 buffer window 'reuse alist display-buffer-mark-dedicated)))))
7269 (defun display-buffer-at-bottom (buffer alist)
7270 "Try displaying BUFFER in a window at the bottom of the selected frame.
7271 This either reuses such a window provided it shows BUFFER
7272 already, splits a window at the bottom of the frame or the
7273 frame's root window, or reuses some window at the bottom of the
7274 selected frame."
7275 (let (bottom-window bottom-window-shows-buffer window)
7276 (walk-window-tree
7277 (lambda (window)
7278 (cond
7279 ((window-in-direction 'below window))
7280 ((and (not bottom-window-shows-buffer)
7281 (eq buffer (window-buffer window)))
7282 (setq bottom-window-shows-buffer t)
7283 (setq bottom-window window))
7284 ((not bottom-window)
7285 (setq bottom-window window)))
7286 nil nil 'nomini))
7287 (or (and bottom-window-shows-buffer
7288 (window--display-buffer
7289 buffer bottom-window 'reuse alist display-buffer-mark-dedicated))
7290 (and (not (frame-parameter nil 'unsplittable))
7291 (let (split-width-threshold)
7292 (setq window (window--try-to-split-window bottom-window alist)))
7293 (window--display-buffer
7294 buffer window 'window alist display-buffer-mark-dedicated))
7295 (and (not (frame-parameter nil 'unsplittable))
7296 (setq window (split-window-no-error (window-main-window)))
7297 (window--display-buffer
7298 buffer window 'window alist display-buffer-mark-dedicated))
7299 (and (setq window bottom-window)
7300 (not (window-dedicated-p window))
7301 (window--display-buffer
7302 buffer window 'reuse alist display-buffer-mark-dedicated)))))
7304 (defun display-buffer-in-previous-window (buffer alist)
7305 "Display BUFFER in a window previously showing it.
7306 If ALIST has a non-nil `inhibit-same-window' entry, the selected
7307 window is not eligible for reuse.
7309 If ALIST contains a `reusable-frames' entry, its value determines
7310 which frames to search for a reusable window:
7311 nil -- the selected frame (actually the last non-minibuffer frame)
7312 A frame -- just that frame
7313 `visible' -- all visible frames
7314 0 -- all frames on the current terminal
7315 t -- all frames.
7317 If ALIST contains no `reusable-frames' entry, search just the
7318 selected frame if `display-buffer-reuse-frames' and
7319 `pop-up-frames' are both nil; search all frames on the current
7320 terminal if either of those variables is non-nil.
7322 If ALIST has a `previous-window' entry, the window specified by
7323 that entry will override any other window found by the methods
7324 above, even if that window never showed BUFFER before."
7325 (let* ((alist-entry (assq 'reusable-frames alist))
7326 (inhibit-same-window
7327 (cdr (assq 'inhibit-same-window alist)))
7328 (frames (cond
7329 (alist-entry (cdr alist-entry))
7330 ((if (eq pop-up-frames 'graphic-only)
7331 (display-graphic-p)
7332 pop-up-frames)
7334 (display-buffer-reuse-frames 0)
7335 (t (last-nonminibuffer-frame))))
7336 best-window second-best-window window)
7337 ;; Scan windows whether they have shown the buffer recently.
7338 (catch 'best
7339 (dolist (window (window-list-1 (frame-first-window) 'nomini frames))
7340 (when (and (assq buffer (window-prev-buffers window))
7341 (not (window-dedicated-p window)))
7342 (if (eq window (selected-window))
7343 (unless inhibit-same-window
7344 (setq second-best-window window))
7345 (setq best-window window)
7346 (throw 'best t)))))
7347 ;; When ALIST has a `previous-window' entry, that entry may override
7348 ;; anything we found so far.
7349 (when (and (setq window (cdr (assq 'previous-window alist)))
7350 (window-live-p window)
7351 (not (window-dedicated-p window)))
7352 (if (eq window (selected-window))
7353 (unless inhibit-same-window
7354 (setq second-best-window window))
7355 (setq best-window window)))
7356 ;; Return best or second best window found.
7357 (when (setq window (or best-window second-best-window))
7358 (window--display-buffer buffer window 'reuse alist))))
7360 (defun display-buffer-use-some-window (buffer alist)
7361 "Display BUFFER in an existing window.
7362 Search for a usable window, set that window to the buffer, and
7363 return the window. If no suitable window is found, return nil.
7365 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7366 event that a window in another frame is chosen, avoid raising
7367 that frame."
7368 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
7369 (frame (or (window--frame-usable-p (selected-frame))
7370 (window--frame-usable-p (last-nonminibuffer-frame))))
7371 (window
7372 ;; Reuse an existing window.
7373 (or (get-lru-window frame nil not-this-window)
7374 (let ((window (get-buffer-window buffer 'visible)))
7375 (unless (and not-this-window
7376 (eq window (selected-window)))
7377 window))
7378 (get-largest-window 'visible nil not-this-window)
7379 (let ((window (get-buffer-window buffer 0)))
7380 (unless (and not-this-window
7381 (eq window (selected-window)))
7382 window))
7383 (get-largest-window 0 nil not-this-window)))
7384 (quit-restore (and (window-live-p window)
7385 (window-parameter window 'quit-restore)))
7386 (quad (nth 1 quit-restore)))
7387 (when (window-live-p window)
7388 ;; If the window was used by `display-buffer' before, try to
7389 ;; resize it to its old height but don't signal an error.
7390 (when (and (listp quad)
7391 (integerp (nth 3 quad))
7392 (> (nth 3 quad) (window-total-height window)))
7393 (condition-case nil
7394 (window-resize window (- (nth 3 quad) (window-total-height window)))
7395 (error nil)))
7397 (prog1
7398 (window--display-buffer buffer window 'reuse alist)
7399 (window--even-window-sizes window)
7400 (unless (cdr (assq 'inhibit-switch-frame alist))
7401 (window--maybe-raise-frame (window-frame window)))))))
7403 (defun display-buffer-no-window (_buffer alist)
7404 "Display BUFFER in no window.
7405 If ALIST has a non-nil `allow-no-window' entry, then don't display
7406 a window at all. This makes possible to override the default action
7407 and avoid displaying the buffer. It is assumed that when the caller
7408 specifies a non-nil `allow-no-window' then it can handle a nil value
7409 returned from `display-buffer' in this case."
7410 (when (cdr (assq 'allow-no-window alist))
7411 'fail))
7413 ;;; Display + selection commands:
7414 (defun pop-to-buffer (buffer-or-name &optional action norecord)
7415 "Display buffer specified by BUFFER-OR-NAME and select its window.
7416 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
7417 If it is a string not naming an existent buffer, create a buffer
7418 with that name. If BUFFER-OR-NAME is nil, choose some other
7419 buffer. In either case, make that buffer current and return it.
7421 This uses `display-buffer' as a subroutine. The optional ACTION
7422 argument is passed to `display-buffer' as its ACTION argument.
7423 See `display-buffer' for more information. ACTION is t if called
7424 interactively with a prefix argument, which means to pop to a
7425 window other than the selected one even if the buffer is already
7426 displayed in the selected window.
7428 If a suitable window is found, select that window. If it is not
7429 on the selected frame, raise that window's frame and give it
7430 input focus.
7432 Optional third arg NORECORD non-nil means do not put this buffer
7433 at the front of the list of recently selected ones."
7434 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
7435 (if current-prefix-arg t)))
7436 (let* ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))
7437 (old-frame (selected-frame))
7438 (window (display-buffer buffer action)))
7439 ;; Don't assume that `display-buffer' has supplied us with a window
7440 ;; (Bug#24332).
7441 (if window
7442 (let ((frame (window-frame window)))
7443 ;; If we chose another frame, make sure it gets input focus.
7444 (unless (eq frame old-frame)
7445 (select-frame-set-input-focus frame norecord))
7446 ;; Make sure the window is selected (Bug#8615), (Bug#6954)
7447 (select-window window norecord))
7448 ;; If `display-buffer' failed to supply a window, just make the
7449 ;; buffer current.
7450 (set-buffer buffer))
7451 ;; Return BUFFER even when we got no window.
7452 buffer))
7454 (defun pop-to-buffer-same-window (buffer &optional norecord)
7455 "Select buffer BUFFER in some window, preferably the same one.
7456 BUFFER may be a buffer, a string (a buffer name), or nil. If it
7457 is a string not naming an existent buffer, create a buffer with
7458 that name. If BUFFER is nil, choose some other buffer. Return
7459 the buffer.
7461 Optional argument NORECORD, if non-nil means do not put this
7462 buffer at the front of the list of recently selected ones.
7464 Unlike `pop-to-buffer', this function prefers using the selected
7465 window over popping up a new window or frame."
7466 (pop-to-buffer buffer display-buffer--same-window-action norecord))
7468 (defun read-buffer-to-switch (prompt)
7469 "Read the name of a buffer to switch to, prompting with PROMPT.
7470 Return the name of the buffer as a string.
7472 This function is intended for the `switch-to-buffer' family of
7473 commands since these need to omit the name of the current buffer
7474 from the list of completions and default values."
7475 (let ((rbts-completion-table (internal-complete-buffer-except)))
7476 (minibuffer-with-setup-hook
7477 (lambda ()
7478 (setq minibuffer-completion-table rbts-completion-table)
7479 ;; Since rbts-completion-table is built dynamically, we
7480 ;; can't just add it to the default value of
7481 ;; icomplete-with-completion-tables, so we add it
7482 ;; here manually.
7483 (if (and (boundp 'icomplete-with-completion-tables)
7484 (listp icomplete-with-completion-tables))
7485 (set (make-local-variable 'icomplete-with-completion-tables)
7486 (cons rbts-completion-table
7487 icomplete-with-completion-tables))))
7488 (read-buffer prompt (other-buffer (current-buffer))
7489 (confirm-nonexistent-file-or-buffer)))))
7491 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
7492 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
7493 If BUFFER-OR-NAME is nil, return the buffer returned by
7494 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
7495 exists, return that buffer. If no such buffer exists, create a
7496 buffer with the name BUFFER-OR-NAME and return that buffer."
7497 (if buffer-or-name
7498 (or (get-buffer buffer-or-name)
7499 (let ((buffer (get-buffer-create buffer-or-name)))
7500 (set-buffer-major-mode buffer)
7501 buffer))
7502 (other-buffer)))
7504 (defcustom switch-to-buffer-preserve-window-point t
7505 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
7506 If this is nil, `switch-to-buffer' displays the buffer at that
7507 buffer's `point'. If this is `already-displayed', it tries to
7508 display the buffer at its previous position in the selected
7509 window, provided the buffer is currently displayed in some other
7510 window on any visible or iconified frame. If this is t, it
7511 unconditionally tries to display the buffer at its previous
7512 position in the selected window.
7514 This variable is ignored if the buffer is already displayed in
7515 the selected window or never appeared in it before, or if
7516 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
7517 :type '(choice
7518 (const :tag "Never" nil)
7519 (const :tag "If already displayed elsewhere" already-displayed)
7520 (const :tag "Always" t))
7521 :group 'windows
7522 :version "26.1")
7524 (defcustom switch-to-buffer-in-dedicated-window nil
7525 "Allow switching to buffer in strongly dedicated windows.
7526 If non-nil, allow `switch-to-buffer' to proceed when called
7527 interactively and the selected window is strongly dedicated to
7528 its buffer.
7530 The following values are recognized:
7532 nil - disallow switching; signal an error
7534 prompt - prompt user whether to allow switching
7536 pop - perform `pop-to-buffer' instead
7538 t - undedicate selected window and switch
7540 When called non-interactively, `switch-to-buffer' always signals
7541 an error when the selected window is dedicated to its buffer and
7542 FORCE-SAME-WINDOW is non-nil."
7543 :type '(choice
7544 (const :tag "Disallow" nil)
7545 (const :tag "Prompt" prompt)
7546 (const :tag "Pop" pop)
7547 (const :tag "Allow" t))
7548 :group 'windows
7549 :version "25.1")
7551 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
7552 "Display buffer BUFFER-OR-NAME in the selected window.
7554 WARNING: This is NOT the way to work on another buffer temporarily
7555 within a Lisp program! Use `set-buffer' instead. That avoids
7556 messing with the window-buffer correspondences.
7558 If the selected window cannot display the specified buffer
7559 because it is a minibuffer window or strongly dedicated to
7560 another buffer, call `pop-to-buffer' to select the buffer in
7561 another window. In interactive use, if the selected window is
7562 strongly dedicated to its buffer, the value of the option
7563 `switch-to-buffer-in-dedicated-window' specifies how to proceed.
7565 If called interactively, read the buffer name using the
7566 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7567 determines whether to request confirmation before creating a new
7568 buffer.
7570 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
7571 If BUFFER-OR-NAME is a string that does not identify an existing
7572 buffer, create a buffer with that name. If BUFFER-OR-NAME is
7573 nil, switch to the buffer returned by `other-buffer'.
7575 If optional argument NORECORD is non-nil, do not put the buffer
7576 at the front of the buffer list, and do not make the window
7577 displaying it the most recently selected one.
7579 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
7580 must be displayed in the selected window when called
7581 non-interactively; if that is impossible, signal an error rather
7582 than calling `pop-to-buffer'.
7584 The option `switch-to-buffer-preserve-window-point' can be used
7585 to make the buffer appear at its last position in the selected
7586 window.
7588 Return the buffer switched to."
7589 (interactive
7590 (let ((force-same-window
7591 (cond
7592 ((window-minibuffer-p) nil)
7593 ((not (eq (window-dedicated-p) t)) 'force-same-window)
7594 ((pcase switch-to-buffer-in-dedicated-window
7595 (`nil (user-error
7596 "Cannot switch buffers in a dedicated window"))
7597 (`prompt
7598 (if (y-or-n-p
7599 (format "Window is dedicated to %s; undedicate it"
7600 (window-buffer)))
7601 (progn
7602 (set-window-dedicated-p nil nil)
7603 'force-same-window)
7604 (user-error
7605 "Cannot switch buffers in a dedicated window")))
7606 (`pop nil)
7607 (_ (set-window-dedicated-p nil nil) 'force-same-window))))))
7608 (list (read-buffer-to-switch "Switch to buffer: ") nil force-same-window)))
7609 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
7610 (cond
7611 ;; Don't call set-window-buffer if it's not needed since it
7612 ;; might signal an error (e.g. if the window is dedicated).
7613 ((eq buffer (window-buffer)))
7614 ((window-minibuffer-p)
7615 (if force-same-window
7616 (user-error "Cannot switch buffers in minibuffer window")
7617 (pop-to-buffer buffer norecord)))
7618 ((eq (window-dedicated-p) t)
7619 (if force-same-window
7620 (user-error "Cannot switch buffers in a dedicated window")
7621 (pop-to-buffer buffer norecord)))
7623 (let* ((entry (assq buffer (window-prev-buffers)))
7624 (displayed (and (eq switch-to-buffer-preserve-window-point
7625 'already-displayed)
7626 (get-buffer-window buffer 0))))
7627 (set-window-buffer nil buffer)
7628 (when (and entry
7629 (or (eq switch-to-buffer-preserve-window-point t)
7630 displayed))
7631 ;; Try to restore start and point of buffer in the selected
7632 ;; window (Bug#4041).
7633 (set-window-start (selected-window) (nth 1 entry) t)
7634 (set-window-point nil (nth 2 entry))))))
7636 (unless norecord
7637 (select-window (selected-window)))
7638 (set-buffer buffer)))
7640 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
7641 "Select the buffer specified by BUFFER-OR-NAME in another window.
7642 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
7643 nil. Return the buffer switched to.
7645 If called interactively, prompt for the buffer name using the
7646 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7647 determines whether to request confirmation before creating a new
7648 buffer.
7650 If BUFFER-OR-NAME is a string and does not identify an existing
7651 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
7652 nil, switch to the buffer returned by `other-buffer'.
7654 Optional second argument NORECORD non-nil means do not put this
7655 buffer at the front of the list of recently selected ones.
7657 This uses the function `display-buffer' as a subroutine; see its
7658 documentation for additional customization information."
7659 (interactive
7660 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
7661 (let ((pop-up-windows t))
7662 (pop-to-buffer buffer-or-name t norecord)))
7664 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
7665 "Switch to buffer BUFFER-OR-NAME in another frame.
7666 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
7667 nil. Return the buffer switched to.
7669 If called interactively, prompt for the buffer name using the
7670 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7671 determines whether to request confirmation before creating a new
7672 buffer.
7674 If BUFFER-OR-NAME is a string and does not identify an existing
7675 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
7676 nil, switch to the buffer returned by `other-buffer'.
7678 Optional second arg NORECORD non-nil means do not put this
7679 buffer at the front of the list of recently selected ones.
7681 This uses the function `display-buffer' as a subroutine; see its
7682 documentation for additional customization information."
7683 (interactive
7684 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
7685 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
7687 (defun set-window-text-height (window height)
7688 "Set the height in lines of the text display area of WINDOW to HEIGHT.
7689 WINDOW must be a live window and defaults to the selected one.
7690 HEIGHT doesn't include the mode line or header line, if any, or
7691 any partial-height lines in the text display area.
7693 Note that the current implementation of this function cannot
7694 always set the height exactly, but attempts to be conservative,
7695 by allocating more lines than are actually needed in the case
7696 where some error may be present."
7697 (setq window (window-normalize-window window t))
7698 (let ((delta (- height (window-text-height window))))
7699 (unless (zerop delta)
7700 ;; Setting window-min-height to a value like 1 can lead to very
7701 ;; bizarre displays because it also allows Emacs to make *other*
7702 ;; windows one line tall, which means that there's no more space
7703 ;; for the mode line.
7704 (let ((window-min-height (min 2 height)))
7705 (window-resize window delta)))))
7707 (defun enlarge-window-horizontally (delta)
7708 "Make selected window DELTA columns wider.
7709 Interactively, if no argument is given, make selected window one
7710 column wider."
7711 (interactive "p")
7712 (enlarge-window delta t))
7714 (defun shrink-window-horizontally (delta)
7715 "Make selected window DELTA columns narrower.
7716 Interactively, if no argument is given, make selected window one
7717 column narrower."
7718 (interactive "p")
7719 (shrink-window delta t))
7721 (defun count-screen-lines (&optional beg end count-final-newline window)
7722 "Return the number of screen lines in the region.
7723 The number of screen lines may be different from the number of actual lines,
7724 due to line breaking, display table, etc.
7726 Optional arguments BEG and END default to `point-min' and `point-max'
7727 respectively.
7729 If region ends with a newline, ignore it unless optional third argument
7730 COUNT-FINAL-NEWLINE is non-nil.
7732 The optional fourth argument WINDOW specifies the window used for obtaining
7733 parameters such as width, horizontal scrolling, and so on. The default is
7734 to use the selected window's parameters.
7736 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
7737 regardless of which buffer is displayed in WINDOW. This makes possible to use
7738 `count-screen-lines' in any buffer, whether or not it is currently displayed
7739 in some window."
7740 (unless beg
7741 (setq beg (point-min)))
7742 (unless end
7743 (setq end (point-max)))
7744 (if (= beg end)
7746 (save-excursion
7747 (save-restriction
7748 (widen)
7749 (narrow-to-region (min beg end)
7750 (if (and (not count-final-newline)
7751 (= ?\n (char-before (max beg end))))
7752 (1- (max beg end))
7753 (max beg end)))
7754 (goto-char (point-min))
7755 (1+ (vertical-motion (buffer-size) window))))))
7757 (defun window-buffer-height (window)
7758 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
7759 WINDOW must be a live window and defaults to the selected one."
7760 (setq window (window-normalize-window window t))
7761 (with-current-buffer (window-buffer window)
7762 (max 1
7763 (count-screen-lines (point-min) (point-max)
7764 ;; If buffer ends with a newline, ignore it when
7765 ;; counting height unless point is after it.
7766 (eobp)
7767 window))))
7769 ;;; Resizing windows and frames to fit their contents exactly.
7770 (defcustom fit-window-to-buffer-horizontally nil
7771 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
7772 If this is nil, `fit-window-to-buffer' never resizes windows
7773 horizontally. If this is `only', it can resize windows
7774 horizontally only. Any other value means `fit-window-to-buffer'
7775 can resize windows in both dimensions."
7776 :type 'boolean
7777 :version "24.4"
7778 :group 'help)
7780 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
7781 ;; counting title bar and outer borders.
7782 (defcustom fit-frame-to-buffer nil
7783 "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
7784 A frame is fit if and only if its root window is a live window
7785 and this option is non-nil. If this is `horizontally', frames
7786 are resized horizontally only. If this is `vertically', frames
7787 are resized vertically only. Any other non-nil value means
7788 frames can be resized in both dimensions."
7789 :type 'boolean
7790 :version "24.4"
7791 :group 'help)
7793 (defcustom fit-frame-to-buffer-margins '(nil nil nil nil)
7794 "Margins around frame for `fit-frame-to-buffer'.
7795 This specifies the numbers of pixels to be left free on the left,
7796 above, on the right, and below a frame fitted to its buffer. Set
7797 this to avoid obscuring other desktop objects like the taskbar.
7798 The default is nil for each side, which means to not add margins.
7800 The value specified here can be overridden for a specific frame
7801 by that frame's `fit-frame-to-buffer-margins' parameter, if
7802 present. See also `fit-frame-to-buffer-sizes'."
7803 :version "24.4"
7804 :type '(list
7805 (choice
7806 :tag "Left"
7807 :value nil
7808 :format "%[LeftMargin%] %v "
7809 (const :tag "None" :format "%t" nil)
7810 (integer :tag "Pixels" :size 5))
7811 (choice
7812 :tag "Top"
7813 :value nil
7814 :format "%[TopMargin%] %v "
7815 (const :tag "None" :format "%t" nil)
7816 (integer :tag "Pixels" :size 5))
7817 (choice
7818 :tag "Right"
7819 :value nil
7820 :format "%[RightMargin%] %v "
7821 (const :tag "None" :format "%t" nil)
7822 (integer :tag "Pixels" :size 5))
7823 (choice
7824 :tag "Bottom"
7825 :value nil
7826 :format "%[BottomMargin%] %v "
7827 (const :tag "None" :format "%t" nil)
7828 (integer :tag "Pixels" :size 5)))
7829 :group 'help)
7831 (defcustom fit-frame-to-buffer-sizes '(nil nil nil nil)
7832 "Size boundaries of frame for `fit-frame-to-buffer'.
7833 This list specifies the total maximum and minimum lines and
7834 maximum and minimum columns of the root window of any frame that
7835 shall be fit to its buffer. If any of these values is non-nil,
7836 it overrides the corresponding argument of `fit-frame-to-buffer'.
7838 On window systems where the menubar can wrap, fitting a frame to
7839 its buffer may swallow the last line(s). Specifying an
7840 appropriate minimum width value here can avoid such wrapping.
7842 See also `fit-frame-to-buffer-margins'."
7843 :version "24.4"
7844 :type '(list
7845 (choice
7846 :tag "Maximum Height"
7847 :value nil
7848 :format "%[MaxHeight%] %v "
7849 (const :tag "None" :format "%t" nil)
7850 (integer :tag "Lines" :size 5))
7851 (choice
7852 :tag "Minimum Height"
7853 :value nil
7854 :format "%[MinHeight%] %v "
7855 (const :tag "None" :format "%t" nil)
7856 (integer :tag "Lines" :size 5))
7857 (choice
7858 :tag "Maximum Width"
7859 :value nil
7860 :format "%[MaxWidth%] %v "
7861 (const :tag "None" :format "%t" nil)
7862 (integer :tag "Columns" :size 5))
7863 (choice
7864 :tag "Minimum Width"
7865 :value nil
7866 :format "%[MinWidth%] %v\n"
7867 (const :tag "None" :format "%t" nil)
7868 (integer :tag "Columns" :size 5)))
7869 :group 'help)
7871 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
7873 (defun window--sanitize-margin (margin left right)
7874 "Return MARGIN if it's a number between LEFT and RIGHT."
7875 (when (and (numberp margin)
7876 (<= left (- right margin)) (<= margin right))
7877 margin))
7879 (declare-function tool-bar-height "xdisp.c" (&optional frame pixelwise))
7881 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only)
7882 "Adjust size of FRAME to display the contents of its buffer exactly.
7883 FRAME can be any live frame and defaults to the selected one.
7884 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
7885 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
7886 FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
7887 `window-min-height' and `window-min-width' respectively.
7889 If the optional argument ONLY is `vertically', resize the frame
7890 vertically only. If ONLY is `horizontally', resize the frame
7891 horizontally only.
7893 The new position and size of FRAME can be additionally determined
7894 by customizing the options `fit-frame-to-buffer-sizes' and
7895 `fit-frame-to-buffer-margins' or the corresponding parameters of
7896 FRAME."
7897 (interactive)
7898 (unless (and (fboundp 'x-display-pixel-height)
7899 ;; We need the respective sizes now.
7900 (fboundp 'display-monitor-attributes-list))
7901 (user-error "Cannot resize frame in non-graphic Emacs"))
7902 (setq frame (window-normalize-frame frame))
7903 (when (window-live-p (frame-root-window frame))
7904 (with-selected-window (frame-root-window frame)
7905 (let* ((char-width (frame-char-width))
7906 (char-height (frame-char-height))
7907 (monitor-attributes (car (display-monitor-attributes-list
7908 (frame-parameter frame 'display))))
7909 (geometry (cdr (assq 'geometry monitor-attributes)))
7910 (display-width (- (nth 2 geometry) (nth 0 geometry)))
7911 (display-height (- (nth 3 geometry) (nth 1 geometry)))
7912 (workarea (cdr (assq 'workarea monitor-attributes)))
7913 ;; Handle margins.
7914 (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins)
7915 fit-frame-to-buffer-margins))
7916 (left-margin (if (nth 0 margins)
7917 (or (window--sanitize-margin
7918 (nth 0 margins) 0 display-width)
7920 (nth 0 workarea)))
7921 (top-margin (if (nth 1 margins)
7922 (or (window--sanitize-margin
7923 (nth 1 margins) 0 display-height)
7925 (nth 1 workarea)))
7926 (workarea-width (nth 2 workarea))
7927 (right-margin (if (nth 2 margins)
7928 (- display-width
7929 (or (window--sanitize-margin
7930 (nth 2 margins) left-margin display-width)
7932 (nth 2 workarea)))
7933 (workarea-height (nth 3 workarea))
7934 (bottom-margin (if (nth 3 margins)
7935 (- display-height
7936 (or (window--sanitize-margin
7937 (nth 3 margins) top-margin display-height)
7939 (nth 3 workarea)))
7940 ;; The pixel width of FRAME (which does not include the
7941 ;; window manager's decorations).
7942 (frame-width (frame-pixel-width))
7943 ;; The pixel width of the body of FRAME's root window.
7944 (window-body-width (window-body-width nil t))
7945 ;; The difference in pixels between total and body width of
7946 ;; FRAME's window.
7947 (window-extra-width (- (window-pixel-width) window-body-width))
7948 ;; The difference in pixels between the frame's pixel width
7949 ;; and the window's body width. This is the space we can't
7950 ;; use for fitting.
7951 (extra-width (- frame-width window-body-width))
7952 ;; The pixel position of FRAME's left border. We usually
7953 ;; try to leave this alone.
7954 (left
7955 (let ((left (frame-parameter nil 'left)))
7956 (if (consp left)
7957 (funcall (car left) (cadr left))
7958 left)))
7959 ;; The pixel height of FRAME (which does not include title
7960 ;; line, decorations, and sometimes neither the menu nor
7961 ;; the toolbar).
7962 (frame-height (frame-pixel-height))
7963 ;; The pixel height of FRAME's root window (we don't care
7964 ;; about the window's body height since the return value of
7965 ;; `window-text-pixel-size' includes header and mode line).
7966 (window-height (window-pixel-height))
7967 ;; The difference in pixels between the frame's pixel
7968 ;; height and the window's height.
7969 (extra-height (- frame-height window-height))
7970 ;; The pixel position of FRAME's top border.
7971 (top
7972 (let ((top (frame-parameter nil 'top)))
7973 (if (consp top)
7974 (funcall (car top) (cadr top))
7975 top)))
7976 ;; Sanitize minimum and maximum sizes.
7977 (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes)
7978 fit-frame-to-buffer-sizes))
7979 (max-height
7980 (cond
7981 ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height))
7982 ((numberp max-height) (* max-height char-height))
7983 (t display-height)))
7984 (min-height
7985 (cond
7986 ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height))
7987 ((numberp min-height) (* min-height char-height))
7988 (t (* window-min-height char-height))))
7989 (max-width
7990 (cond
7991 ((numberp (nth 2 sizes))
7992 (- (* (nth 2 sizes) char-width) window-extra-width))
7993 ((numberp max-width)
7994 (- (* max-width char-width) window-extra-width))
7995 (t display-width)))
7996 (min-width
7997 (cond
7998 ((numberp (nth 3 sizes))
7999 (- (* (nth 3 sizes) char-width) window-extra-width))
8000 ((numberp min-width)
8001 (- (* min-width char-width) window-extra-width))
8002 (t (* window-min-width char-width))))
8003 ;; Note: Currently, for a new frame the sizes of the header
8004 ;; and mode line may be estimated incorrectly
8005 (value (window-text-pixel-size
8006 nil t t workarea-width workarea-height t))
8007 (width (+ (car value) (window-right-divider-width)))
8008 (height
8009 (+ (cdr value)
8010 (window-bottom-divider-width)
8011 (window-scroll-bar-height))))
8012 ;; Don't change height or width when the window's size is fixed
8013 ;; in either direction or ONLY forbids it.
8014 (cond
8015 ((or (eq window-size-fixed 'width) (eq only 'vertically))
8016 (setq width nil))
8017 ((or (eq window-size-fixed 'height) (eq only 'horizontally))
8018 (setq height nil)))
8019 ;; Fit width to constraints.
8020 (when width
8021 (unless frame-resize-pixelwise
8022 ;; Round to character sizes.
8023 (setq width (* (/ (+ width char-width -1) char-width)
8024 char-width)))
8025 ;; Fit to maximum and minimum widths.
8026 (setq width (max (min width max-width) min-width))
8027 ;; Add extra width.
8028 (setq width (+ width extra-width))
8029 ;; Preserve margins.
8030 (let ((right (+ left width)))
8031 (cond
8032 ((> right right-margin)
8033 ;; Move frame to left (we don't know its real width).
8034 (setq left (max left-margin (- left (- right right-margin)))))
8035 ((< left left-margin)
8036 ;; Move frame to right.
8037 (setq left left-margin)))))
8038 ;; Fit height to constraints.
8039 (when height
8040 (unless frame-resize-pixelwise
8041 (setq height (* (/ (+ height char-height -1) char-height)
8042 char-height)))
8043 ;; Fit to maximum and minimum heights.
8044 (setq height (max (min height max-height) min-height))
8045 ;; Add extra height.
8046 (setq height (+ height extra-height))
8047 ;; Preserve margins.
8048 (let ((bottom (+ top height)))
8049 (cond
8050 ((> bottom bottom-margin)
8051 ;; Move frame up (we don't know its real height).
8052 (setq top (max top-margin (- top (- bottom bottom-margin)))))
8053 ((< top top-margin)
8054 ;; Move frame down.
8055 (setq top top-margin)))))
8056 ;; Apply changes.
8057 (set-frame-position frame left top)
8058 ;; Clumsily try to translate our calculations to what
8059 ;; `set-frame-size' wants.
8060 (when width
8061 (setq width (- (+ (frame-text-width) width)
8062 extra-width window-body-width)))
8063 (when height
8064 (setq height (- (+ (frame-text-height) height)
8065 extra-height window-height)))
8066 (set-frame-size
8067 frame
8068 (if width
8069 (if frame-resize-pixelwise
8070 width
8071 (/ width char-width))
8072 (frame-text-width))
8073 (if height
8074 (if frame-resize-pixelwise
8075 height
8076 (/ height char-height))
8077 (frame-text-height))
8078 frame-resize-pixelwise)))))
8080 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width preserve-size)
8081 "Adjust size of WINDOW to display its buffer's contents exactly.
8082 WINDOW must be a live window and defaults to the selected one.
8084 If WINDOW is part of a vertical combination, adjust WINDOW's
8085 height. The new height is calculated from the actual height of
8086 the accessible portion of its buffer. The optional argument
8087 MAX-HEIGHT specifies a maximum height and defaults to the height
8088 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
8089 minimum height and defaults to `window-min-height'. Both
8090 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
8091 and header line and a bottom divider, if any.
8093 If WINDOW is part of a horizontal combination and the value of
8094 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
8095 WINDOW's width. The new width of WINDOW is calculated from the
8096 maximum length of its buffer's lines that follow the current
8097 start position of WINDOW. The optional argument MAX-WIDTH
8098 specifies a maximum width and defaults to the width of WINDOW's
8099 frame. The optional argument MIN-WIDTH specifies a minimum width
8100 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
8101 are specified in columns and include fringes, margins, a
8102 scrollbar and a vertical divider, if any.
8104 If the optional argument `preserve-size' is non-nil, preserve the
8105 size of WINDOW (see `window-preserve-size').
8107 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
8108 If WINDOW is its frame's root window and the option
8109 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
8110 adjust the frame's size.
8112 Note that even if this function makes WINDOW large enough to show
8113 _all_ parts of its buffer you might not see the first part when
8114 WINDOW was scrolled. If WINDOW is resized horizontally, you will
8115 not see the top of its buffer unless WINDOW starts at its minimum
8116 accessible position."
8117 (interactive)
8118 (setq window (window-normalize-window window t))
8119 (if (eq window (frame-root-window window))
8120 (when fit-frame-to-buffer
8121 ;; Fit WINDOW's frame to buffer.
8122 (fit-frame-to-buffer
8123 (window-frame window)
8124 max-height min-height max-width min-width
8125 (and (memq fit-frame-to-buffer '(vertically horizontally))
8126 fit-frame-to-buffer)))
8127 (with-selected-window window
8128 (let* ((pixelwise window-resize-pixelwise)
8129 (char-height (frame-char-height))
8130 (char-width (frame-char-width))
8131 (total-height (window-size window nil pixelwise))
8132 (body-height (window-body-height window pixelwise))
8133 (body-width (window-body-width window pixelwise))
8134 (min-height
8135 ;; Sanitize MIN-HEIGHT.
8136 (if (numberp min-height)
8137 ;; Can't get smaller than `window-safe-min-height'.
8138 (max (if pixelwise
8139 (* char-height min-height)
8140 min-height)
8141 (if pixelwise
8142 (window-safe-min-pixel-height window)
8143 window-safe-min-height))
8144 ;; Preserve header and mode line if present.
8145 (max (if pixelwise
8146 (* char-height window-min-height)
8147 window-min-height)
8148 (window-min-size window nil window pixelwise))))
8149 (max-height
8150 ;; Sanitize MAX-HEIGHT.
8151 (if (numberp max-height)
8152 (min
8153 (+ total-height
8154 (window-max-delta
8155 window nil window nil nil nil pixelwise))
8156 (if pixelwise
8157 (* char-height max-height)
8158 max-height))
8159 (+ total-height (window-max-delta
8160 window nil window nil nil nil pixelwise))))
8161 height)
8162 (cond
8163 ;; If WINDOW is vertically combined, try to resize it
8164 ;; vertically.
8165 ((and (not (eq fit-window-to-buffer-horizontally 'only))
8166 (not (window-size-fixed-p window 'preserved))
8167 (window-combined-p))
8168 ;; Vertically we always want to fit the entire buffer.
8169 ;; WINDOW'S height can't get larger than its frame's pixel
8170 ;; height. Its width remains fixed.
8171 (setq height (+ (cdr (window-text-pixel-size
8172 nil nil t nil (frame-pixel-height) t))
8173 (window-scroll-bar-height window)
8174 (window-bottom-divider-width)))
8175 ;; Round height.
8176 (unless pixelwise
8177 (setq height (/ (+ height char-height -1) char-height)))
8178 (unless (= height total-height)
8179 (window-preserve-size window)
8180 (window-resize-no-error
8181 window
8182 (- (max min-height (min max-height height)) total-height)
8183 nil window pixelwise)
8184 (when preserve-size
8185 (window-preserve-size window nil t))))
8186 ;; If WINDOW is horizontally combined, try to resize it
8187 ;; horizontally.
8188 ((and fit-window-to-buffer-horizontally
8189 (not (window-size-fixed-p window t 'preserved))
8190 (window-combined-p nil t))
8191 (let* ((total-width (window-size window t pixelwise))
8192 (min-width
8193 ;; Sanitize MIN-WIDTH.
8194 (if (numberp min-width)
8195 ;; Can't get smaller than `window-safe-min-width'.
8196 (max (if pixelwise
8197 (* char-width min-width)
8198 min-width)
8199 (if pixelwise
8200 (window-safe-min-pixel-width)
8201 window-safe-min-width))
8202 ;; Preserve fringes, margins, scrollbars if present.
8203 (max (if pixelwise
8204 (* char-width window-min-width)
8205 window-min-width)
8206 (window-min-size nil nil window pixelwise))))
8207 (max-width
8208 ;; Sanitize MAX-WIDTH.
8209 (if (numberp max-width)
8210 (min (+ total-width
8211 (window-max-delta
8212 window t window nil nil nil pixelwise))
8213 (if pixelwise
8214 (* char-width max-width)
8215 max-width))
8216 (+ total-width (window-max-delta
8217 window t window nil nil nil pixelwise))))
8218 ;; When fitting horizontally, assume that WINDOW's
8219 ;; start position remains unaltered. WINDOW can't get
8220 ;; wider than its frame's pixel width, its height
8221 ;; remains unaltered.
8222 (width (+ (car (window-text-pixel-size
8223 nil (window-start) (point-max)
8224 (frame-pixel-width)
8225 ;; Add one char-height to assure that
8226 ;; we're on the safe side. This
8227 ;; overshoots when the first line below
8228 ;; the bottom is wider than the window.
8229 (* body-height
8230 (if pixelwise 1 char-height))))
8231 (window-right-divider-width))))
8232 (unless pixelwise
8233 (setq width (/ (+ width char-width -1) char-width)))
8234 (unless (= width body-width)
8235 (window-preserve-size window t)
8236 (window-resize-no-error
8237 window
8238 (- (max min-width
8239 (min max-width
8240 (+ total-width (- width body-width))))
8241 total-width)
8242 t window pixelwise)
8243 (when preserve-size
8244 (window-preserve-size window t t))))))))))
8246 (defun window-safely-shrinkable-p (&optional window)
8247 "Return t if WINDOW can be shrunk without shrinking other windows.
8248 WINDOW defaults to the selected window."
8249 (with-selected-window (or window (selected-window))
8250 (let ((edges (window-edges)))
8251 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
8252 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
8254 (defun shrink-window-if-larger-than-buffer (&optional window)
8255 "Shrink height of WINDOW if its buffer doesn't need so many lines.
8256 More precisely, shrink WINDOW vertically to be as small as
8257 possible, while still showing the full contents of its buffer.
8258 WINDOW must be a live window and defaults to the selected one.
8260 Do not shrink WINDOW to less than `window-min-height' lines. Do
8261 nothing if the buffer contains more lines than the present window
8262 height, or if some of the window's contents are scrolled out of
8263 view, or if shrinking this window would also shrink another
8264 window, or if the window is the only window of its frame.
8266 Return non-nil if the window was shrunk, nil otherwise."
8267 (interactive)
8268 (setq window (window-normalize-window window t))
8269 ;; Make sure that WINDOW is vertically combined and `point-min' is
8270 ;; visible (for whatever reason that's needed). The remaining issues
8271 ;; should be taken care of by `fit-window-to-buffer'.
8272 (when (and (window-combined-p window)
8273 (pos-visible-in-window-p (point-min) window))
8274 (fit-window-to-buffer window (window-total-height window))))
8276 (defun kill-buffer-and-window ()
8277 "Kill the current buffer and delete the selected window."
8278 (interactive)
8279 (let ((window-to-delete (selected-window))
8280 (buffer-to-kill (current-buffer))
8281 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
8282 (unwind-protect
8283 (progn
8284 (add-hook 'kill-buffer-hook delete-window-hook t t)
8285 (if (kill-buffer (current-buffer))
8286 ;; If `delete-window' failed before, we rerun it to regenerate
8287 ;; the error so it can be seen in the echo area.
8288 (when (eq (selected-window) window-to-delete)
8289 (delete-window))))
8290 ;; If the buffer is not dead for some reason (probably because
8291 ;; of a `quit' signal), remove the hook again.
8292 (ignore-errors
8293 (with-current-buffer buffer-to-kill
8294 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
8298 ;; Groups of windows (Follow Mode).
8300 ;; This section of functions extends the functionality of some window
8301 ;; manipulating commands to groups of windows cooperatively
8302 ;; displaying a buffer, typically with Follow Mode.
8304 ;; The xxx-function variables are permanent locals so that their local
8305 ;; status is undone only when explicitly programmed, not when a buffer
8306 ;; is reverted or a mode function is called.
8308 (defvar window-group-start-function nil)
8309 (make-variable-buffer-local 'window-group-start-function)
8310 (put 'window-group-start-function 'permanent-local t)
8311 (defun window-group-start (&optional window)
8312 "Return position at which display currently starts in the group of
8313 windows containing WINDOW. When a grouping mode (such as Follow Mode)
8314 is not active, this function is identical to `window-start'.
8316 WINDOW must be a live window and defaults to the selected one.
8317 This is updated by redisplay or by calling `set-window*-start'."
8318 (if (functionp window-group-start-function)
8319 (funcall window-group-start-function window)
8320 (window-start window)))
8322 (defvar window-group-end-function nil)
8323 (make-variable-buffer-local 'window-group-end-function)
8324 (put 'window-group-end-function 'permanent-local t)
8325 (defun window-group-end (&optional window update)
8326 "Return position at which display currently ends in the group of
8327 windows containing WINDOW. When a grouping mode (such as Follow Mode)
8328 is not active, this function is identical to `window-end'.
8330 WINDOW must be a live window and defaults to the selected one.
8331 This is updated by redisplay, when it runs to completion.
8332 Simply changing the buffer text or setting `window-group-start'
8333 does not update this value.
8334 Return nil if there is no recorded value. (This can happen if the
8335 last redisplay of WINDOW was preempted, and did not finish.)
8336 If UPDATE is non-nil, compute the up-to-date position
8337 if it isn't already recorded."
8338 (if (functionp window-group-end-function)
8339 (funcall window-group-end-function window update)
8340 (window-end window update)))
8342 (defvar set-window-group-start-function nil)
8343 (make-variable-buffer-local 'set-window-group-start-function)
8344 (put 'set-window-group-start-function 'permanent-local t)
8345 (defun set-window-group-start (window pos &optional noforce)
8346 "Make display in the group of windows containing WINDOW start at
8347 position POS in WINDOW's buffer. When a grouping mode (such as Follow
8348 Mode) is not active, this function is identical to `set-window-start'.
8350 WINDOW must be a live window and defaults to the selected one. Return
8351 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
8352 overriding motion of point in order to display at this exact start."
8353 (if (functionp set-window-group-start-function)
8354 (funcall set-window-group-start-function window pos noforce)
8355 (set-window-start window pos noforce)))
8357 (defvar recenter-window-group-function nil)
8358 (make-variable-buffer-local 'recenter-window-group-function)
8359 (put 'recenter-window-group-function 'permanent-local t)
8360 (defun recenter-window-group (&optional arg)
8361 "Center point in the group of windows containing the selected window
8362 and maybe redisplay frame. When a grouping mode (such as Follow Mode)
8363 is not active, this function is identical to `recenter'.
8365 With a numeric prefix argument ARG, recenter putting point on screen line ARG
8366 relative to the first window in the selected window group. If ARG is
8367 negative, it counts up from the bottom of the last window in the
8368 group. (ARG should be less than the total height of the window group.)
8370 If ARG is omitted or nil, then recenter with point on the middle line of
8371 the selected window group; if the variable `recenter-redisplay' is
8372 non-nil, also erase the entire frame and redraw it (when
8373 `auto-resize-tool-bars' is set to `grow-only', this resets the
8374 tool-bar's height to the minimum height needed); if
8375 `recenter-redisplay' has the special value `tty', then only tty frames
8376 are redrawn.
8378 Just C-u as prefix means put point in the center of the window
8379 and redisplay normally--don't erase and redraw the frame."
8380 (if (functionp recenter-window-group-function)
8381 (funcall recenter-window-group-function arg)
8382 (recenter arg)))
8384 (defvar pos-visible-in-window-group-p-function nil)
8385 (make-variable-buffer-local 'pos-visible-in-window-group-p-function)
8386 (put 'pos-visible-in-window-group-p-function 'permanent-local t)
8387 (defun pos-visible-in-window-group-p (&optional pos window partially)
8388 "Return non-nil if position POS is currently on the frame in the
8389 window group containing WINDOW. When a grouping mode (such as Follow
8390 Mode) is not active, this function is identical to
8391 `pos-visible-in-window-p'.
8393 WINDOW must be a live window and defaults to the selected one.
8395 Return nil if that position is scrolled vertically out of view. If a
8396 character is only partially visible, nil is returned, unless the
8397 optional argument PARTIALLY is non-nil. If POS is only out of view
8398 because of horizontal scrolling, return non-nil. If POS is t, it
8399 specifies the position of the last visible glyph in the window group.
8400 POS defaults to point in WINDOW; WINDOW defaults to the selected
8401 window.
8403 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
8404 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
8405 where X and Y are the pixel coordinates relative to the top left corner
8406 of the window. The remaining elements are omitted if the character after
8407 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
8408 off-window at the top and bottom of the screen line (\"row\") containing
8409 POS, ROWH is the visible height of that row, and VPOS is the row number
8410 \(zero-based)."
8411 (if (functionp pos-visible-in-window-group-p-function)
8412 (funcall pos-visible-in-window-group-p-function pos window partially)
8413 (pos-visible-in-window-p pos window partially)))
8415 (defvar selected-window-group-function nil)
8416 (make-variable-buffer-local 'selected-window-group-function)
8417 (put 'selected-window-group-function 'permanent-local t)
8418 (defun selected-window-group ()
8419 "Return the list of windows in the group containing the selected window.
8420 When a grouping mode (such as Follow Mode) is not active, the
8421 result is a list containing only the selected window."
8422 (if (functionp selected-window-group-function)
8423 (funcall selected-window-group-function)
8424 (list (selected-window))))
8426 (defvar move-to-window-group-line-function nil)
8427 (make-variable-buffer-local 'move-to-window-group-line-function)
8428 (put 'move-to-window-group-line-function 'permanent-local t)
8429 (defun move-to-window-group-line (arg)
8430 "Position point relative to the the current group of windows.
8431 When a grouping mode (such as Follow Mode) is not active, this
8432 function is identical to `move-to-window-line'.
8434 ARG nil means position point at center of the window group.
8435 Else, ARG specifies the vertical position within the window
8436 group; zero means top of first window in the group, negative
8437 means relative to the bottom of the last window in the group."
8438 (if (functionp move-to-window-group-line-function)
8439 (funcall move-to-window-group-line-function arg)
8440 (move-to-window-line arg)))
8443 (defvar recenter-last-op nil
8444 "Indicates the last recenter operation performed.
8445 Possible values: `top', `middle', `bottom', integer or float numbers.
8446 It can also be nil, which means the first value in `recenter-positions'.")
8448 (defcustom recenter-positions '(middle top bottom)
8449 "Cycling order for `recenter-top-bottom'.
8450 A list of elements with possible values `top', `middle', `bottom',
8451 integer or float numbers that define the cycling order for
8452 the command `recenter-top-bottom'.
8454 Top and bottom destinations are `scroll-margin' lines from the true
8455 window top and bottom. Middle redraws the frame and centers point
8456 vertically within the window. Integer number moves current line to
8457 the specified absolute window-line. Float number between 0.0 and 1.0
8458 means the percentage of the screen space from the top. The default
8459 cycling order is middle -> top -> bottom."
8460 :type '(repeat (choice
8461 (const :tag "Top" top)
8462 (const :tag "Middle" middle)
8463 (const :tag "Bottom" bottom)
8464 (integer :tag "Line number")
8465 (float :tag "Percentage")))
8466 :version "23.2"
8467 :group 'windows)
8469 (defun recenter-top-bottom (&optional arg)
8470 "Move current buffer line to the specified window line.
8471 With no prefix argument, successive calls place point according
8472 to the cycling order defined by `recenter-positions'.
8474 A prefix argument is handled like `recenter':
8475 With numeric prefix ARG, move current line to window-line ARG.
8476 With plain `C-u', move current line to window center."
8477 (interactive "P")
8478 (cond
8479 (arg (recenter arg)) ; Always respect ARG.
8481 (setq recenter-last-op
8482 (if (eq this-command last-command)
8483 (car (or (cdr (member recenter-last-op recenter-positions))
8484 recenter-positions))
8485 (car recenter-positions)))
8486 (let ((this-scroll-margin
8487 (min (max 0 scroll-margin)
8488 (truncate (/ (window-body-height) 4.0)))))
8489 (cond ((eq recenter-last-op 'middle)
8490 (recenter))
8491 ((eq recenter-last-op 'top)
8492 (recenter this-scroll-margin))
8493 ((eq recenter-last-op 'bottom)
8494 (recenter (- -1 this-scroll-margin)))
8495 ((integerp recenter-last-op)
8496 (recenter recenter-last-op))
8497 ((floatp recenter-last-op)
8498 (recenter (round (* recenter-last-op (window-height))))))))))
8500 (define-key global-map [?\C-l] 'recenter-top-bottom)
8502 (defun move-to-window-line-top-bottom (&optional arg)
8503 "Position point relative to window.
8505 With a prefix argument ARG, acts like `move-to-window-line'.
8507 With no argument, positions point at center of window.
8508 Successive calls position point at positions defined
8509 by `recenter-positions'."
8510 (interactive "P")
8511 (cond
8512 (arg (move-to-window-line arg)) ; Always respect ARG.
8514 (setq recenter-last-op
8515 (if (eq this-command last-command)
8516 (car (or (cdr (member recenter-last-op recenter-positions))
8517 recenter-positions))
8518 (car recenter-positions)))
8519 (let ((this-scroll-margin
8520 (min (max 0 scroll-margin)
8521 (truncate (/ (window-body-height) 4.0)))))
8522 (cond ((eq recenter-last-op 'middle)
8523 (call-interactively 'move-to-window-line))
8524 ((eq recenter-last-op 'top)
8525 (move-to-window-line this-scroll-margin))
8526 ((eq recenter-last-op 'bottom)
8527 (move-to-window-line (- -1 this-scroll-margin)))
8528 ((integerp recenter-last-op)
8529 (move-to-window-line recenter-last-op))
8530 ((floatp recenter-last-op)
8531 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
8533 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
8535 ;;; Scrolling commands.
8537 ;;; Scrolling commands which do not signal errors at top/bottom
8538 ;;; of buffer at first key-press (instead move to top/bottom
8539 ;;; of buffer).
8541 (defcustom scroll-error-top-bottom nil
8542 "Move point to top/bottom of buffer before signaling a scrolling error.
8543 A value of nil means just signal an error if no more scrolling possible.
8544 A value of t means point moves to the beginning or the end of the buffer
8545 \(depending on scrolling direction) when no more scrolling possible.
8546 When point is already on that position, then signal an error."
8547 :type 'boolean
8548 :group 'windows
8549 :version "24.1")
8551 (defun scroll-up-command (&optional arg)
8552 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
8553 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
8554 scroll window further, move cursor to the bottom line.
8555 When point is already on that position, then signal an error.
8556 A near full screen is `next-screen-context-lines' less than a full screen.
8557 Negative ARG means scroll downward.
8558 If ARG is the atom `-', scroll downward by nearly full screen."
8559 (interactive "^P")
8560 (cond
8561 ((null scroll-error-top-bottom)
8562 (scroll-up arg))
8563 ((eq arg '-)
8564 (scroll-down-command nil))
8565 ((< (prefix-numeric-value arg) 0)
8566 (scroll-down-command (- (prefix-numeric-value arg))))
8567 ((eobp)
8568 (scroll-up arg)) ; signal error
8570 (condition-case nil
8571 (scroll-up arg)
8572 (end-of-buffer
8573 (if arg
8574 ;; When scrolling by ARG lines can't be done,
8575 ;; move by ARG lines instead.
8576 (forward-line arg)
8577 ;; When ARG is nil for full-screen scrolling,
8578 ;; move to the bottom of the buffer.
8579 (goto-char (point-max))))))))
8581 (put 'scroll-up-command 'scroll-command t)
8583 (defun scroll-down-command (&optional arg)
8584 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
8585 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
8586 scroll window further, move cursor to the top line.
8587 When point is already on that position, then signal an error.
8588 A near full screen is `next-screen-context-lines' less than a full screen.
8589 Negative ARG means scroll upward.
8590 If ARG is the atom `-', scroll upward by nearly full screen."
8591 (interactive "^P")
8592 (cond
8593 ((null scroll-error-top-bottom)
8594 (scroll-down arg))
8595 ((eq arg '-)
8596 (scroll-up-command nil))
8597 ((< (prefix-numeric-value arg) 0)
8598 (scroll-up-command (- (prefix-numeric-value arg))))
8599 ((bobp)
8600 (scroll-down arg)) ; signal error
8602 (condition-case nil
8603 (scroll-down arg)
8604 (beginning-of-buffer
8605 (if arg
8606 ;; When scrolling by ARG lines can't be done,
8607 ;; move by ARG lines instead.
8608 (forward-line (- arg))
8609 ;; When ARG is nil for full-screen scrolling,
8610 ;; move to the top of the buffer.
8611 (goto-char (point-min))))))))
8613 (put 'scroll-down-command 'scroll-command t)
8615 ;;; Scrolling commands which scroll a line instead of full screen.
8617 (defun scroll-up-line (&optional arg)
8618 "Scroll text of selected window upward ARG lines; or one line if no ARG.
8619 If ARG is omitted or nil, scroll upward by one line.
8620 This is different from `scroll-up-command' that scrolls a full screen."
8621 (interactive "p")
8622 (scroll-up (or arg 1)))
8624 (put 'scroll-up-line 'scroll-command t)
8626 (defun scroll-down-line (&optional arg)
8627 "Scroll text of selected window down ARG lines; or one line if no ARG.
8628 If ARG is omitted or nil, scroll down by one line.
8629 This is different from `scroll-down-command' that scrolls a full screen."
8630 (interactive "p")
8631 (scroll-down (or arg 1)))
8633 (put 'scroll-down-line 'scroll-command t)
8636 (defun scroll-other-window-down (&optional lines)
8637 "Scroll the \"other window\" down.
8638 For more details, see the documentation for `scroll-other-window'."
8639 (interactive "P")
8640 (scroll-other-window
8641 ;; Just invert the argument's meaning.
8642 ;; We can do that without knowing which window it will be.
8643 (if (eq lines '-) nil
8644 (if (null lines) '-
8645 (- (prefix-numeric-value lines))))))
8647 (defun beginning-of-buffer-other-window (arg)
8648 "Move point to the beginning of the buffer in the other window.
8649 Leave mark at previous position.
8650 With arg N, put point N/10 of the way from the true beginning."
8651 (interactive "P")
8652 (let ((orig-window (selected-window))
8653 (window (other-window-for-scrolling)))
8654 ;; We use unwind-protect rather than save-window-excursion
8655 ;; because the latter would preserve the things we want to change.
8656 (unwind-protect
8657 (progn
8658 (select-window window)
8659 ;; Set point and mark in that window's buffer.
8660 (with-no-warnings
8661 (beginning-of-buffer arg))
8662 ;; Set point accordingly.
8663 (recenter '(t)))
8664 (select-window orig-window))))
8666 (defun end-of-buffer-other-window (arg)
8667 "Move point to the end of the buffer in the other window.
8668 Leave mark at previous position.
8669 With arg N, put point N/10 of the way from the true end."
8670 (interactive "P")
8671 ;; See beginning-of-buffer-other-window for comments.
8672 (let ((orig-window (selected-window))
8673 (window (other-window-for-scrolling)))
8674 (unwind-protect
8675 (progn
8676 (select-window window)
8677 (with-no-warnings
8678 (end-of-buffer arg))
8679 (recenter '(t)))
8680 (select-window orig-window))))
8682 (defvar mouse-autoselect-window-timer nil
8683 "Timer used by delayed window autoselection.")
8685 (defvar mouse-autoselect-window-position-1 nil
8686 "First mouse position recorded by delayed window autoselection.")
8688 (defvar mouse-autoselect-window-position nil
8689 "Last mouse position recorded by delayed window autoselection.")
8691 (defvar mouse-autoselect-window-window nil
8692 "Last window recorded by delayed window autoselection.")
8694 (defvar mouse-autoselect-window-state nil
8695 "When non-nil, special state of delayed window autoselection.
8696 Possible values are `suspend' (suspend autoselection after a menu or
8697 scrollbar interaction) and `select' (the next invocation of
8698 `handle-select-window' shall select the window immediately).")
8700 (defun mouse-autoselect-window-cancel (&optional force)
8701 "Cancel delayed window autoselection.
8702 Optional argument FORCE means cancel unconditionally."
8703 (unless (and (not force)
8704 ;; Don't cancel for select-window or select-frame events
8705 ;; or when the user drags a scroll bar.
8706 (or (memq this-command
8707 '(handle-select-window handle-switch-frame))
8708 (and (eq this-command 'scroll-bar-toolkit-scroll)
8709 (memq (nth 4 (event-end last-input-event))
8710 '(handle end-scroll)))))
8711 (setq mouse-autoselect-window-state nil)
8712 (setq mouse-autoselect-window-position-1 nil)
8713 (when (timerp mouse-autoselect-window-timer)
8714 (cancel-timer mouse-autoselect-window-timer))
8715 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
8717 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
8718 "Start delayed window autoselection.
8719 MOUSE-POSITION is the last position where the mouse was seen as returned
8720 by `mouse-position'. Optional argument WINDOW non-nil denotes the
8721 window where the mouse was seen. Optional argument SUSPEND non-nil
8722 means suspend autoselection."
8723 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
8724 (setq mouse-autoselect-window-position mouse-position)
8725 (when window (setq mouse-autoselect-window-window window))
8726 (setq mouse-autoselect-window-state (when suspend 'suspend))
8727 ;; Install timer which runs `mouse-autoselect-window-select' after
8728 ;; `mouse-autoselect-window' seconds.
8729 (setq mouse-autoselect-window-timer
8730 (run-at-time
8731 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
8733 (defun mouse-autoselect-window-select ()
8734 "Select window with delayed window autoselection.
8735 If the mouse position has stabilized in a non-selected window, select
8736 that window. The minibuffer window is selected only if the minibuffer
8737 is active. This function is run by `mouse-autoselect-window-timer'."
8738 (ignore-errors
8739 (let* ((mouse-position (mouse-position))
8740 (window
8741 (ignore-errors
8742 (window-at (cadr mouse-position) (cddr mouse-position)
8743 (car mouse-position)))))
8744 (cond
8745 ((or (and (fboundp 'menu-or-popup-active-p) (menu-or-popup-active-p))
8746 (and window
8747 (let ((coords (coordinates-in-window-p
8748 (cdr mouse-position) window)))
8749 (and (not (consp coords))
8750 (not (memq coords '(left-margin right-margin)))))))
8751 ;; A menu / popup dialog is active or the mouse is not on the
8752 ;; text region of WINDOW: Suspend autoselection temporarily.
8753 (mouse-autoselect-window-start mouse-position nil t))
8754 ((or (eq mouse-autoselect-window-state 'suspend)
8755 ;; When the mouse is at its first recorded position, restart
8756 ;; delayed autoselection. This works around a scenario with
8757 ;; two two-window frames with identical dimensions: select the
8758 ;; first window of the first frame, switch to the second
8759 ;; frame, move the mouse to its second window, minimize the
8760 ;; second frame. Now the second window of the first frame
8761 ;; gets selected although the mouse never really "moved" into
8762 ;; that window.
8763 (and (numberp mouse-autoselect-window)
8764 (equal (mouse-position) mouse-autoselect-window-position-1)))
8765 ;; Delayed autoselection was temporarily suspended, reenable it.
8766 (mouse-autoselect-window-start mouse-position))
8767 ((and window (not (eq window (selected-window)))
8768 (or (not (numberp mouse-autoselect-window))
8769 (and (>= mouse-autoselect-window 0)
8770 ;; If `mouse-autoselect-window' is non-negative,
8771 ;; select window if it's the same as before.
8772 (eq window mouse-autoselect-window-window))
8773 ;; Otherwise select window iff the mouse is at the same
8774 ;; position as before. Observe that the first test
8775 ;; after starting autoselection usually fails since the
8776 ;; value of `mouse-autoselect-window-position' recorded
8777 ;; there is the position where the mouse has entered the
8778 ;; new window and not necessarily where the mouse has
8779 ;; stopped moving.
8780 (equal mouse-position mouse-autoselect-window-position))
8781 ;; The minibuffer is a candidate window if it's active.
8782 (or (not (window-minibuffer-p window))
8783 (eq window (active-minibuffer-window))))
8784 ;; Mouse position has stabilized in non-selected window: Cancel
8785 ;; delayed autoselection and try to select that window.
8786 (mouse-autoselect-window-cancel t)
8787 ;; Select window where mouse appears unless the selected window is the
8788 ;; minibuffer. Use `unread-command-events' in order to execute pre-
8789 ;; and post-command hooks and trigger idle timers. To avoid delaying
8790 ;; autoselection again, set `mouse-autoselect-window-state'."
8791 (unless (window-minibuffer-p)
8792 (setq mouse-autoselect-window-state 'select)
8793 (setq unread-command-events
8794 (cons (list 'select-window (list window))
8795 unread-command-events))))
8796 ((or (and window (eq window (selected-window)))
8797 (not (numberp mouse-autoselect-window))
8798 (equal mouse-position mouse-autoselect-window-position))
8799 ;; Mouse position has either stabilized in the selected window or at
8800 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
8801 (mouse-autoselect-window-cancel t))
8803 ;; Mouse position has not stabilized yet, resume delayed
8804 ;; autoselection.
8805 (mouse-autoselect-window-start mouse-position window))))))
8807 (defun handle-select-window (event)
8808 "Handle select-window events."
8809 (interactive "^e")
8810 (let ((window (posn-window (event-start event))))
8811 (unless (or (not (window-live-p window))
8812 ;; Don't switch if we're currently in the minibuffer.
8813 ;; This tries to work around problems where the
8814 ;; minibuffer gets unselected unexpectedly, and where
8815 ;; you then have to move your mouse all the way down to
8816 ;; the minibuffer to select it.
8817 (window-minibuffer-p)
8818 ;; Don't switch to minibuffer window unless it's active.
8819 (and (window-minibuffer-p window)
8820 (not (minibuffer-window-active-p window)))
8821 ;; Don't switch when autoselection shall be delayed.
8822 (and (numberp mouse-autoselect-window)
8823 (not (eq mouse-autoselect-window-state 'select))
8824 (let ((position (mouse-position)))
8825 ;; Cancel any delayed autoselection.
8826 (mouse-autoselect-window-cancel t)
8827 ;; Start delayed autoselection from current mouse
8828 ;; position and window.
8829 (setq mouse-autoselect-window-position-1 position)
8830 (mouse-autoselect-window-start position window)
8831 ;; Executing a command cancels delayed autoselection.
8832 (add-hook
8833 'pre-command-hook 'mouse-autoselect-window-cancel))))
8834 (when mouse-autoselect-window
8835 ;; Reset state of delayed autoselection.
8836 (setq mouse-autoselect-window-state nil)
8837 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
8838 (run-hooks 'mouse-leave-buffer-hook))
8839 ;; Clear echo area.
8840 (message nil)
8841 (select-window window))))
8843 (defun truncated-partial-width-window-p (&optional window)
8844 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
8845 WINDOW must be a live window and defaults to the selected one.
8846 Return nil if WINDOW is not a partial-width window
8847 (regardless of the value of `truncate-lines').
8848 Otherwise, consult the value of `truncate-partial-width-windows'
8849 for the buffer shown in WINDOW."
8850 (setq window (window-normalize-window window t))
8851 (unless (window-full-width-p window)
8852 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
8853 (window-buffer window))))
8854 (if (integerp t-p-w-w)
8855 (< (window-width window) t-p-w-w)
8856 t-p-w-w))))
8859 ;; Automatically inform subprocesses of changes to window size.
8861 (defcustom window-adjust-process-window-size-function
8862 'window-adjust-process-window-size-smallest
8863 "Control how Emacs chooses inferior process window sizes.
8864 Emacs uses this function to tell processes the space they have
8865 available for displaying their output. After each window
8866 configuration change, Emacs calls the value of
8867 `window-adjust-process-window-size-function' for each process
8868 with a buffer being displayed in at least one window.
8869 This function is responsible for combining the sizes of the
8870 displayed windows and returning a cons (WIDTH . HEIGHT)
8871 describing the width and height with which Emacs will call
8872 `set-process-window-size' for that process. If the function
8873 returns nil, Emacs does not call `set-process-window-size'.
8875 This function is called with the process buffer as the current
8876 buffer and with two arguments: the process and a list of windows
8877 displaying process. Modes can make this variable buffer-local;
8878 additionally, the `adjust-window-size-function' process property
8879 overrides the global or buffer-local value of
8880 `window-adjust-process-window-size-function'."
8881 :type '(choice
8882 (const :tag "Minimum area of any window"
8883 window-adjust-process-window-size-smallest)
8884 (const :tag "Maximum area of any window"
8885 window-adjust-process-window-size-largest)
8886 (const :tag "Do not adjust process window sizes" ignore)
8887 function)
8888 :group 'windows
8889 :version "25.1")
8891 (defun window-adjust-process-window-size (reducer windows)
8892 "Adjust the window sizes of a process.
8893 WINDOWS is a list of windows associated with that process. REDUCER is
8894 a two-argument function used to combine the widths and heights of
8895 the given windows."
8896 (when windows
8897 (let ((width (window-max-chars-per-line (car windows)))
8898 (height (window-body-height (car windows))))
8899 (dolist (window (cdr windows))
8900 (setf width (funcall reducer width (window-max-chars-per-line window)))
8901 (setf height (funcall reducer height (window-body-height window))))
8902 (cons width height))))
8904 (defun window-adjust-process-window-size-smallest (_process windows)
8905 "Adjust the process window size of PROCESS.
8906 WINDOWS is a list of windows associated with PROCESS. Choose the
8907 smallest area available for displaying PROCESS's output."
8908 (window-adjust-process-window-size #'min windows))
8910 (defun window-adjust-process-window-size-largest (_process windows)
8911 "Adjust the process window size of PROCESS.
8912 WINDOWS is a list of windows associated with PROCESS. Choose the
8913 largest area available for displaying PROCESS's output."
8914 (window-adjust-process-window-size #'max windows))
8916 (defun window--process-window-list ()
8917 "Return an alist mapping processes to associated windows.
8918 A window is associated with a process if that window is
8919 displaying that processes's buffer."
8920 (let ((processes (process-list))
8921 (process-windows nil))
8922 (if processes
8923 (walk-windows
8924 (lambda (window)
8925 (let ((buffer (window-buffer window))
8926 (iter processes))
8927 (while (let ((process (car iter)))
8928 (if (and (process-live-p process)
8929 (eq buffer (process-buffer process)))
8930 (let ((procwin (assq process process-windows)))
8931 ;; Add this window to the list of windows
8932 ;; displaying process.
8933 (if procwin
8934 (push window (cdr procwin))
8935 (push (list process window) process-windows))
8936 ;; We found our process for this window, so
8937 ;; stop iterating over the process list.
8938 nil)
8939 (setf iter (cdr iter)))))))
8940 1 t))
8941 process-windows))
8943 (defun window--adjust-process-windows ()
8944 "Update process window sizes to match the current window configuration."
8945 (when (fboundp 'process-list)
8946 (dolist (procwin (window--process-window-list))
8947 (let ((process (car procwin)))
8948 (with-demoted-errors "Error adjusting window size: %S"
8949 (with-current-buffer (process-buffer process)
8950 (let ((size (funcall
8951 (or (process-get process 'adjust-window-size-function)
8952 window-adjust-process-window-size-function)
8953 process (cdr procwin))))
8954 (when size
8955 (set-process-window-size process (cdr size) (car size))))))))))
8957 (add-hook 'window-configuration-change-hook 'window--adjust-process-windows)
8960 ;; Some of these are in tutorial--default-keys, so update that if you
8961 ;; change these.
8962 (define-key ctl-x-map "0" 'delete-window)
8963 (define-key ctl-x-map "1" 'delete-other-windows)
8964 (define-key ctl-x-map "2" 'split-window-below)
8965 (define-key ctl-x-map "3" 'split-window-right)
8966 (define-key ctl-x-map "o" 'other-window)
8967 (define-key ctl-x-map "^" 'enlarge-window)
8968 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
8969 (define-key ctl-x-map "{" 'shrink-window-horizontally)
8970 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
8971 (define-key ctl-x-map "+" 'balance-windows)
8972 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
8974 ;;; window.el ends here