Revert previous patch; comment was OK after all.
[emacs.git] / lisp / window.el
blob2c0ea8e4d56a1dbf88e7a17fc7177fe3175bddc2
1 ;;; window.el --- GNU Emacs window commands aside from those written in C -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2018 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: internal
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Window tree functions.
29 ;;; Code:
31 (defun internal--before-save-selected-window ()
32 (cons (selected-window)
33 ;; We save and restore all frames' selected windows, because
34 ;; `select-window' can change the frame-selected-window of
35 ;; whatever frame that window is in. Each text terminal's
36 ;; top-frame is preserved by putting it last in the list.
37 (apply #'append
38 (mapcar (lambda (terminal)
39 (let ((frames (frames-on-display-list terminal))
40 (top-frame (tty-top-frame terminal))
41 alist)
42 (if top-frame
43 (setq frames
44 (cons top-frame
45 (delq top-frame frames))))
46 (dolist (f frames)
47 (push (cons f (frame-selected-window f))
48 alist))
49 alist))
50 (terminal-list)))))
52 (defun internal--after-save-selected-window (state)
53 (dolist (elt (cdr state))
54 (and (frame-live-p (car elt))
55 (window-live-p (cdr elt))
56 (set-frame-selected-window (car elt) (cdr elt) 'norecord)))
57 (when (window-live-p (car state))
58 (select-window (car state) 'norecord)))
60 (defmacro save-selected-window (&rest body)
61 "Execute BODY, then select the previously selected window.
62 The value returned is the value of the last form in BODY.
64 This macro saves and restores the selected window, as well as the
65 selected window in each frame. If the previously selected window
66 is no longer live, then whatever window is selected at the end of
67 BODY remains selected. If the previously selected window of some
68 frame is no longer live at the end of BODY, that frame's selected
69 window is left alone.
71 This macro saves and restores the current buffer, since otherwise
72 its normal operation could make a different buffer current. The
73 order of recently selected windows and the buffer list ordering
74 are not altered by this macro (unless they are altered in BODY)."
75 (declare (indent 0) (debug t))
76 `(let ((save-selected-window--state (internal--before-save-selected-window)))
77 (save-current-buffer
78 (unwind-protect
79 (progn ,@body)
80 (internal--after-save-selected-window save-selected-window--state)))))
82 (defvar temp-buffer-window-setup-hook nil
83 "Normal hook run by `with-temp-buffer-window' before buffer display.
84 This hook is run by `with-temp-buffer-window' with the buffer to be
85 displayed current.")
87 (defvar temp-buffer-window-show-hook nil
88 "Normal hook run by `with-temp-buffer-window' after buffer display.
89 This hook is run by `with-temp-buffer-window' with the buffer
90 displayed and current and its window selected.")
92 (defun temp-buffer-window-setup (buffer-or-name)
93 "Set up temporary buffer specified by BUFFER-OR-NAME.
94 Return the buffer."
95 (let ((old-dir default-directory)
96 (buffer (get-buffer-create buffer-or-name)))
97 (with-current-buffer buffer
98 (kill-all-local-variables)
99 (setq default-directory old-dir)
100 (delete-all-overlays)
101 (setq buffer-read-only nil)
102 (setq buffer-file-name nil)
103 (setq buffer-undo-list t)
104 (let ((inhibit-read-only t)
105 (inhibit-modification-hooks t))
106 (erase-buffer)
107 (run-hooks 'temp-buffer-window-setup-hook))
108 ;; Return the buffer.
109 buffer)))
111 (defun temp-buffer-window-show (buffer &optional action)
112 "Show temporary buffer BUFFER in a window.
113 Return the window showing BUFFER. Pass ACTION as action argument
114 to `display-buffer'."
115 (let (window frame)
116 (with-current-buffer buffer
117 (set-buffer-modified-p nil)
118 (setq buffer-read-only t)
119 (goto-char (point-min))
120 (when (let ((window-combination-limit
121 ;; When `window-combination-limit' equals
122 ;; `temp-buffer' or `temp-buffer-resize' and
123 ;; `temp-buffer-resize-mode' is enabled in this
124 ;; buffer bind it to t so resizing steals space
125 ;; preferably from the window that was split.
126 (if (or (eq window-combination-limit 'temp-buffer)
127 (and (eq window-combination-limit
128 'temp-buffer-resize)
129 temp-buffer-resize-mode))
131 window-combination-limit)))
132 (setq window (display-buffer buffer action)))
133 (setq frame (window-frame window))
134 (unless (eq frame (selected-frame))
135 (raise-frame frame))
136 (setq minibuffer-scroll-window window)
137 (set-window-hscroll window 0)
138 (with-selected-window window
139 (run-hooks 'temp-buffer-window-show-hook)
140 (when temp-buffer-resize-mode
141 (resize-temp-buffer-window window)))
142 ;; Return the window.
143 window))))
145 (defmacro with-temp-buffer-window (buffer-or-name action quit-function &rest body)
146 "Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
147 BUFFER-OR-NAME must specify either a live buffer, or the name of
148 a buffer (if it does not exist, this macro creates it).
150 Make the buffer specified by BUFFER-OR-NAME empty before running
151 BODY and bind `standard-output' to that buffer, so that output
152 generated with `prin1' and similar functions in BODY goes into
153 that buffer. Do not make that buffer current for running the
154 forms in BODY. Use `with-current-buffer-window' instead if you
155 need to run BODY with that buffer current.
157 At the end of BODY, mark the specified buffer unmodified and
158 read-only, and display it in a window (but do not select it).
159 The display happens by calling `display-buffer' passing it the
160 ACTION argument. If `temp-buffer-resize-mode' is enabled, the
161 corresponding window may be resized automatically.
163 Return the value returned by BODY, unless QUIT-FUNCTION specifies
164 a function. In that case, run that function with two arguments -
165 the window showing the specified buffer and the value returned by
166 BODY - and return the value returned by that function.
168 If the buffer is displayed on a new frame, the window manager may
169 decide to select that frame. In that case, it's usually a good
170 strategy if QUIT-FUNCTION selects the window showing the buffer
171 before reading any value from the minibuffer; for example, when
172 asking a `yes-or-no-p' question.
174 This runs the hook `temp-buffer-window-setup-hook' before BODY,
175 with the specified buffer temporarily current. It runs the hook
176 `temp-buffer-window-show-hook' after displaying the buffer, with
177 that buffer temporarily current, and the window that was used to
178 display it temporarily selected.
180 This construct is similar to `with-output-to-temp-buffer' but,
181 neither runs `temp-buffer-setup-hook' which usually puts the
182 buffer in Help mode, nor `temp-buffer-show-function' (the ACTION
183 argument replaces this)."
184 (declare (debug t))
185 (let ((buffer (make-symbol "buffer"))
186 (window (make-symbol "window"))
187 (value (make-symbol "value")))
188 (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
189 (vaction action)
190 (vquit-function quit-function))
191 `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
192 (standard-output ,buffer)
193 ,window ,value)
194 (setq ,value (progn ,@body))
195 (with-current-buffer ,buffer
196 (setq ,window (temp-buffer-window-show ,buffer ,vaction)))
198 (if (functionp ,vquit-function)
199 (funcall ,vquit-function ,window ,value)
200 ,value)))))
202 (defmacro with-current-buffer-window (buffer-or-name action quit-function &rest body)
203 "Evaluate BODY with a buffer BUFFER-OR-NAME current and show that buffer.
204 This construct is like `with-temp-buffer-window' but unlike that
205 makes the buffer specified by BUFFER-OR-NAME current for running
206 BODY."
207 (declare (debug t))
208 (let ((buffer (make-symbol "buffer"))
209 (window (make-symbol "window"))
210 (value (make-symbol "value")))
211 (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
212 (vaction action)
213 (vquit-function quit-function))
214 `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
215 (standard-output ,buffer)
216 ,window ,value)
217 (with-current-buffer ,buffer
218 (setq ,value (progn ,@body))
219 (setq ,window (temp-buffer-window-show ,buffer ,vaction)))
221 (if (functionp ,vquit-function)
222 (funcall ,vquit-function ,window ,value)
223 ,value)))))
225 (defmacro with-displayed-buffer-window (buffer-or-name action quit-function &rest body)
226 "Show a buffer BUFFER-OR-NAME and evaluate BODY in that buffer.
227 This construct is like `with-current-buffer-window' but unlike that
228 displays the buffer specified by BUFFER-OR-NAME before running BODY."
229 (declare (debug t))
230 (let ((buffer (make-symbol "buffer"))
231 (window (make-symbol "window"))
232 (value (make-symbol "value")))
233 (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
234 (vaction action)
235 (vquit-function quit-function))
236 `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
237 (standard-output ,buffer)
238 ;; If a 'window-height' entry specifies a function,
239 ;; remember it here in order to call it below but replace
240 ;; the entry so `window--try-to-split-window' will bind
241 ;; `window-combination-limit' to t and the function does
242 ;; not resize any other window but the one we split this
243 ;; one off (Bug#25055, Bug#25179).
244 (vheight-function
245 (let ((window-height (assq 'window-height (cdr ,vaction))))
246 (when (functionp (cdr window-height))
247 (cdr window-height))))
248 (vaction-copied
249 (when vheight-function
250 (cons (car , vaction)
251 (cons
252 '(window-height . t)
253 (assq-delete-all
254 'window-height (cdr (copy-sequence ,vaction)))))))
255 ,window ,value)
256 (with-current-buffer ,buffer
257 (setq ,window (temp-buffer-window-show
258 ,buffer (or vaction-copied ,vaction))))
260 (let ((inhibit-read-only t)
261 (inhibit-modification-hooks t))
262 (setq ,value (progn ,@body)))
264 (set-window-point ,window (point-min))
266 (when vheight-function
267 (ignore-errors
268 (set-window-parameter ,window 'preserve-size nil)
269 (funcall vheight-function ,window)))
271 (when (consp (cdr (assq 'preserve-size (cdr ,vaction))))
272 (window-preserve-size
273 ,window t (cadr (assq 'preserve-size (cdr ,vaction))))
274 (window-preserve-size
275 ,window nil (cddr (assq 'preserve-size (cdr ,vaction)))))
277 (if (functionp ,vquit-function)
278 (funcall ,vquit-function ,window ,value)
279 ,value)))))
281 ;; The following two functions are like `window-next-sibling' and
282 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
283 ;; they don't substitute the selected window for nil), and they return
284 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
285 ;; a minibuffer window).
286 (defun window-right (window)
287 "Return WINDOW's right sibling.
288 Return nil if WINDOW is the root window of its frame. WINDOW can
289 be any window."
290 (and window (window-parent window) (window-next-sibling window)))
292 (defun window-left (window)
293 "Return WINDOW's left sibling.
294 Return nil if WINDOW is the root window of its frame. WINDOW can
295 be any window."
296 (and window (window-parent window) (window-prev-sibling window)))
298 (defun window-child (window)
299 "Return WINDOW's first child window.
300 WINDOW can be any window."
301 (or (window-top-child window) (window-left-child window)))
303 (defun window-child-count (window)
304 "Return number of WINDOW's child windows.
305 WINDOW can be any window."
306 (let ((count 0))
307 (when (and (windowp window) (setq window (window-child window)))
308 (while window
309 (setq count (1+ count))
310 (setq window (window-next-sibling window))))
311 count))
313 (defun window-last-child (window)
314 "Return last child window of WINDOW.
315 WINDOW can be any window."
316 (when (and (windowp window) (setq window (window-child window)))
317 (while (window-next-sibling window)
318 (setq window (window-next-sibling window))))
319 window)
321 (defun window-normalize-buffer (buffer-or-name)
322 "Return buffer specified by BUFFER-OR-NAME.
323 BUFFER-OR-NAME must be a live buffer, a string naming a live
324 buffer or nil which means to return the current buffer.
326 This function is commonly used to process the (usually optional)
327 \"BUFFER-OR-NAME\" argument of window related functions where nil
328 stands for the current buffer."
329 (let ((buffer
330 (cond
331 ((not buffer-or-name)
332 (current-buffer))
333 ((bufferp buffer-or-name)
334 buffer-or-name)
335 ((stringp buffer-or-name)
336 (get-buffer buffer-or-name))
338 (error "No such buffer %s" buffer-or-name)))))
339 (if (buffer-live-p buffer)
340 buffer
341 (error "No such live buffer %s" buffer-or-name))))
343 (defun window-normalize-frame (frame)
344 "Return frame specified by FRAME.
345 FRAME must be a live frame or nil which means to return the
346 selected frame.
348 This function is commonly used to process the (usually optional)
349 \"FRAME\" argument of window and frame related functions where
350 nil stands for the selected frame."
351 (if frame
352 (if (frame-live-p frame)
353 frame
354 (error "%s is not a live frame" frame))
355 (selected-frame)))
357 (defun window-normalize-window (window &optional live-only)
358 "Return window specified by WINDOW.
359 If WINDOW is nil, return the selected window. Otherwise, if
360 WINDOW is a live or an internal window, return WINDOW; if
361 LIVE-ONLY is non-nil, return WINDOW for a live window only.
362 Otherwise, signal an error.
364 This function is commonly used to process the (usually optional)
365 \"WINDOW\" argument of window related functions where nil stands
366 for the selected window."
367 (cond
368 ((null window)
369 (selected-window))
370 (live-only
371 (if (window-live-p window)
372 window
373 (error "%s is not a live window" window)))
374 ((window-valid-p window)
375 window)
377 (error "%s is not a valid window" window))))
379 ;; Maybe this should go to frame.el.
380 (defun frame-char-size (&optional window-or-frame horizontal)
381 "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
382 If WINDOW-OR-FRAME is a live frame, return the value of
383 `frame-char-height' for that frame. If WINDOW-OR-FRAME is a
384 valid window, return the value of `frame-char-height' for that
385 window's frame. In any other case, return the value of
386 `frame-char-height' for the selected frame.
388 Optional argument HORIZONTAL non-nil means to return the value of
389 `frame-char-width' for WINDOW-OR-FRAME."
390 (let ((frame
391 (cond
392 ((window-valid-p window-or-frame)
393 (window-frame window-or-frame))
394 ((frame-live-p window-or-frame)
395 window-or-frame)
396 (t (selected-frame)))))
397 (if horizontal
398 (frame-char-width frame)
399 (frame-char-height frame))))
401 (defvar ignore-window-parameters nil
402 "If non-nil, standard functions ignore window parameters.
403 The functions currently affected by this are `split-window',
404 `delete-window', `delete-other-windows' and `other-window'.
406 An application may bind this to a non-nil value around calls to
407 these functions to inhibit processing of window parameters.")
409 ;; This must go to C, finally (or get removed).
410 (defconst window-safe-min-height 1
411 "The absolute minimum number of lines of any window.
412 Anything less might crash Emacs.")
414 (defun window-safe-min-pixel-height (&optional window)
415 "Return the absolute minimum pixel height of WINDOW."
416 (* window-safe-min-height
417 (frame-char-size (window-normalize-window window))))
419 (defcustom window-min-height 4
420 "The minimum total height, in lines, of any window.
421 The value has to accommodate one text line, a mode and header
422 line, a horizontal scroll bar and a bottom divider, if present.
423 A value less than `window-safe-min-height' is ignored. The value
424 of this variable is honored when windows are resized or split.
426 Applications should never rebind this variable. To resize a
427 window to a height less than the one specified here, an
428 application should instead call `window-resize' with a non-nil
429 IGNORE argument. In order to have `split-window' make a window
430 shorter, explicitly specify the SIZE argument of that function."
431 :type 'integer
432 :version "24.1"
433 :group 'windows)
435 (defun window-min-pixel-height (&optional window)
436 "Return the minimum pixel height of window WINDOW."
437 (* (max window-min-height window-safe-min-height)
438 (frame-char-size window)))
440 ;; This must go to C, finally (or get removed).
441 (defconst window-safe-min-width 2
442 "The absolute minimum number of columns of a window.
443 Anything less might crash Emacs.")
445 (defun window-safe-min-pixel-width (&optional window)
446 "Return the absolute minimum pixel width of WINDOW."
447 (* window-safe-min-width
448 (frame-char-size (window-normalize-window window) t)))
450 (defcustom window-min-width 10
451 "The minimum total width, in columns, of any window.
452 The value has to accommodate two text columns as well as margins,
453 fringes, a scroll bar and a right divider, if present. A value
454 less than `window-safe-min-width' is ignored. The value of this
455 variable is honored when windows are resized or split.
457 Applications should never rebind this variable. To resize a
458 window to a width less than the one specified here, an
459 application should instead call `window-resize' with a non-nil
460 IGNORE argument. In order to have `split-window' make a window
461 narrower, explicitly specify the SIZE argument of that function."
462 :type 'integer
463 :version "24.1"
464 :group 'windows)
466 (defun window-min-pixel-width (&optional window)
467 "Return the minimum pixel width of window WINDOW."
468 (* (max window-min-width window-safe-min-width)
469 (frame-char-size window t)))
471 (defun window-safe-min-pixel-size (&optional window horizontal)
472 "Return the absolute minimum pixel height of WINDOW.
473 Optional argument HORIZONTAL non-nil means return the absolute
474 minimum pixel width of WINDOW."
475 (if horizontal
476 (window-safe-min-pixel-width window)
477 (window-safe-min-pixel-height window)))
479 (defun window-min-pixel-size (&optional window horizontal)
480 "Return the minimum pixel height of WINDOW.
481 Optional argument HORIZONTAL non-nil means return the minimum
482 pixel width of WINDOW."
483 (if horizontal
484 (window-min-pixel-width window)
485 (window-min-pixel-height window)))
487 (defun window-combined-p (&optional window horizontal)
488 "Return non-nil if WINDOW has siblings in a given direction.
489 WINDOW must be a valid window and defaults to the selected one.
491 HORIZONTAL determines a direction for the window combination. If
492 HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
493 a vertical window combination. If HORIZONTAL is non-nil, return
494 non-nil if WINDOW is part of a horizontal window combination."
495 (setq window (window-normalize-window window))
496 (let ((parent (window-parent window)))
497 (and parent
498 (if horizontal
499 (window-left-child parent)
500 (window-top-child parent)))))
502 (defun window-combination-p (&optional window horizontal)
503 "Return WINDOW's first child if WINDOW is a vertical combination.
504 WINDOW can be any window and defaults to the selected one.
505 Optional argument HORIZONTAL non-nil means return WINDOW's first
506 child if WINDOW is a horizontal combination."
507 (setq window (window-normalize-window window))
508 (if horizontal
509 (window-left-child window)
510 (window-top-child window)))
512 (defun window-combinations (window &optional horizontal)
513 "Return largest number of windows vertically arranged within WINDOW.
514 WINDOW must be a valid window and defaults to the selected one.
515 If HORIZONTAL is non-nil, return the largest number of
516 windows horizontally arranged within WINDOW."
517 (setq window (window-normalize-window window))
518 (cond
519 ((window-live-p window)
520 ;; If WINDOW is live, return 1.
522 ((if horizontal
523 (window-left-child window)
524 (window-top-child window))
525 ;; If WINDOW is iso-combined, return the sum of the values for all
526 ;; child windows of WINDOW.
527 (let ((child (window-child window))
528 (count 0))
529 (while child
530 (setq count
531 (+ (window-combinations child horizontal)
532 count))
533 (setq child (window-right child)))
534 count))
536 ;; If WINDOW is not iso-combined, return the maximum value of any
537 ;; child window of WINDOW.
538 (let ((child (window-child window))
539 (count 1))
540 (while child
541 (setq count
542 (max (window-combinations child horizontal)
543 count))
544 (setq child (window-right child)))
545 count))))
547 (defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only)
548 "Helper function for `walk-window-tree' and `walk-window-subtree'."
549 (let (walk-window-tree-buffer)
550 (while walk-window-tree-window
551 (setq walk-window-tree-buffer
552 (window-buffer walk-window-tree-window))
553 (when (or walk-window-tree-buffer any)
554 (funcall fun walk-window-tree-window))
555 (unless walk-window-tree-buffer
556 (walk-window-tree-1
557 fun (window-left-child walk-window-tree-window) any)
558 (walk-window-tree-1
559 fun (window-top-child walk-window-tree-window) any))
560 (if sub-only
561 (setq walk-window-tree-window nil)
562 (setq walk-window-tree-window
563 (window-right walk-window-tree-window))))))
565 (defun walk-window-tree (fun &optional frame any minibuf)
566 "Run function FUN on each live window of FRAME.
567 FUN must be a function with one argument - a window. FRAME must
568 be a live frame and defaults to the selected one. ANY, if
569 non-nil, means to run FUN on all live and internal windows of
570 FRAME.
572 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
573 window even if it isn't active. MINIBUF nil or omitted means run
574 FUN on FRAME's minibuffer window only if it's active. In both
575 cases the minibuffer window must be part of FRAME. MINIBUF
576 neither nil nor t means never run FUN on the minibuffer window.
578 This function performs a pre-order, depth-first traversal of the
579 window tree. If FUN changes the window tree, the result is
580 unpredictable."
581 (setq frame (window-normalize-frame frame))
582 (walk-window-tree-1 fun (frame-root-window frame) any)
583 (when (memq minibuf '(nil t))
584 ;; Run FUN on FRAME's minibuffer window if requested.
585 (let ((minibuffer-window (minibuffer-window frame)))
586 (when (and (window-live-p minibuffer-window)
587 (eq (window-frame minibuffer-window) frame)
588 (or (eq minibuf t)
589 (minibuffer-window-active-p minibuffer-window)))
590 (funcall fun minibuffer-window)))))
592 (defun walk-window-subtree (fun &optional window any)
593 "Run function FUN on the subtree of windows rooted at WINDOW.
594 WINDOW defaults to the selected window. FUN must be a function
595 with one argument - a window. By default, run FUN only on live
596 windows of the subtree. If the optional argument ANY is non-nil,
597 run FUN on all live and internal windows of the subtree. If
598 WINDOW is live, run FUN on WINDOW only.
600 This function performs a pre-order, depth-first traversal of the
601 subtree rooted at WINDOW. If FUN changes that tree, the result
602 is unpredictable."
603 (setq window (window-normalize-window window))
604 (walk-window-tree-1 fun window any t))
606 (defun window-with-parameter (parameter &optional value frame any minibuf)
607 "Return first window on FRAME with PARAMETER non-nil.
608 FRAME defaults to the selected frame. Optional argument VALUE
609 non-nil means only return a window whose window-parameter value
610 for PARAMETER equals VALUE (comparison is done with `equal').
611 Optional argument ANY non-nil means consider internal windows
612 too.
614 Optional argument MINIBUF t means consider FRAME's minibuffer
615 window even if it isn't active. MINIBUF nil or omitted means
616 consider FRAME's minibuffer window only if it's active. In both
617 cases the minibuffer window must be part of FRAME. MINIBUF
618 neither nil nor t means never consider the minibuffer window."
619 (let (this-value)
620 (catch 'found
621 (walk-window-tree
622 (lambda (window)
623 (when (and (setq this-value (window-parameter window parameter))
624 (or (not value) (equal value this-value)))
625 (throw 'found window)))
626 frame any minibuf))))
628 ;;; Atomic windows.
629 (defun window-atom-root (&optional window)
630 "Return root of atomic window WINDOW is a part of.
631 WINDOW must be a valid window and defaults to the selected one.
632 Return nil if WINDOW is not part of an atomic window."
633 (setq window (window-normalize-window window))
634 (let (root)
635 (while (and window (window-parameter window 'window-atom))
636 (setq root window)
637 (setq window (window-parent window)))
638 root))
640 (defun window-make-atom (window)
641 "Make WINDOW an atomic window.
642 WINDOW must be an internal window. Return WINDOW."
643 (if (not (window-child window))
644 (error "Window %s is not an internal window" window)
645 (walk-window-subtree
646 (lambda (window)
647 (unless (window-parameter window 'window-atom)
648 (set-window-parameter window 'window-atom t)))
649 window t)
650 window))
652 (defun display-buffer-in-atom-window (buffer alist)
653 "Display BUFFER in an atomic window.
654 This function displays BUFFER in a new window that will be
655 combined with an existing window to form an atomic window. If
656 the existing window is already part of an atomic window, add the
657 new window to that atomic window. Operations like `split-window'
658 or `delete-window', when applied to a constituent of an atomic
659 window, are applied atomically to the root of that atomic window.
661 ALIST is an association list of symbols and values. The
662 following symbols can be used.
664 `window' specifies the existing window the new window shall be
665 combined with. Use `window-atom-root' to make the new window a
666 sibling of an atomic window's root. If an internal window is
667 specified here, all children of that window become part of the
668 atomic window too. If no window is specified, the new window
669 becomes a sibling of the selected window. By default, the
670 `window-atom' parameter of the existing window is set to `main'
671 provided it is live and was not set before.
673 `side' denotes the side of the existing window where the new
674 window shall be located. Valid values are `below', `right',
675 `above' and `left'. The default is `below'. By default, the
676 `window-atom' parameter of the new window is set to this value.
678 The return value is the new window, nil when creating that window
679 failed."
680 (let* ((ignore-window-parameters t)
681 (window-combination-limit t)
682 (window-combination-resize 'atom)
683 (window (cdr (assq 'window alist)))
684 (side (or (cdr (assq 'side alist)) 'below))
685 (atom (when window (window-parameter window 'window-atom)))
686 root new)
687 (setq window (window-normalize-window window))
688 (setq root (window-atom-root window))
689 ;; Split off new window.
690 (when (setq new (split-window-no-error window nil side))
691 (window-make-atom
692 (if (and root (not (eq root window)))
693 ;; When WINDOW was part of an atomic window and we did not
694 ;; split its root, root atomic window at old root.
695 root
696 ;; Otherwise, root atomic window at WINDOW's new parent.
697 (window-parent window)))
698 ;; Assign `window-atom' parameters, if needed.
699 (when (and (not atom) (window-live-p window))
700 (set-window-parameter window 'window-atom 'main))
701 (set-window-parameter new 'window-atom side)
702 ;; Display BUFFER in NEW and return NEW.
703 (window--display-buffer
704 buffer new 'window alist display-buffer-mark-dedicated))))
706 (defun window--atom-check-1 (window)
707 "Subroutine of `window--atom-check'."
708 (when window
709 (if (window-parameter window 'window-atom)
710 (let ((count 0))
711 (when (or (catch 'reset
712 (walk-window-subtree
713 (lambda (window)
714 (if (window-parameter window 'window-atom)
715 (setq count (1+ count))
716 (throw 'reset t)))
717 window t))
718 ;; count >= 1 must hold here. If there's no other
719 ;; window around dissolve this atomic window.
720 (= count 1))
721 ;; Dissolve atomic window.
722 (walk-window-subtree
723 (lambda (window)
724 (set-window-parameter window 'window-atom nil))
725 window t)))
726 ;; Check children.
727 (unless (window-buffer window)
728 (window--atom-check-1 (window-left-child window))
729 (window--atom-check-1 (window-top-child window))))
730 ;; Check right sibling
731 (window--atom-check-1 (window-right window))))
733 (defun window--atom-check (&optional frame)
734 "Check atomicity of all windows on FRAME.
735 FRAME defaults to the selected frame. If an atomic window is
736 wrongly configured, reset the atomicity of all its windows on
737 FRAME to nil. An atomic window is wrongly configured if it has
738 no child windows or one of its child windows is not atomic."
739 (window--atom-check-1 (frame-root-window frame)))
741 ;; Side windows.
742 (defcustom window-sides-vertical nil
743 "If non-nil, left and right side windows occupy full frame height.
744 If nil, top and bottom side windows occupy full frame width."
745 :type 'boolean
746 :initialize 'custom-initialize-default
747 :set 'window--sides-verticalize
748 :group 'windows
749 :version "26.1")
751 (defcustom window-sides-reversed nil
752 "Whether top/bottom side windows appear in reverse order.
753 When this is nil, side windows on the top and bottom of a frame
754 are always drawn from left to right with increasing slot values.
755 When this is t, side windows on the top and bottom of a frame are
756 always drawn from right to left with increasing slot values.
758 When this is `bidi', the drawing order is like that for the value
759 t if the value of `bidi-paragraph-direction' is `right-to-left'
760 in the buffer most recently shown in the window selected within
761 the main window area of this frame.
763 The layout of side windows on the left or right of a frame is not
764 affected by the value of this variable."
765 :type
766 '(choice (const :tag "Never" nil)
767 (const :tag "Bidi" bidi)
768 (const :tag "Always" t))
769 :initialize 'custom-initialize-default
770 :set 'window--sides-reverse
771 :group 'windows
772 :version "26.1")
774 (defcustom window-sides-slots '(nil nil nil nil)
775 "Number of available side window slots on each side of a frame.
776 The value is a list of four elements specifying the maximum
777 number of side windows that may be created on the left, top,
778 right and bottom side of any frame.
780 If an element is a number, `display-buffer-in-side-window' will
781 refrain from making a new side window if the number of windows on
782 that side is equal to or exceeds that number. Rather, it will
783 reuse the window whose `window-slot' value is nearest to the slot
784 specified via its ALIST argument. If an element is nil, this
785 means there's no bound on the number of windows on that side."
786 :version "24.1"
787 :risky t
788 :type
789 '(list
790 :value (nil nil nil nil)
791 (choice
792 :tag "Left"
793 :help-echo "Maximum number of left side windows."
794 :value nil
795 :format "%[Left%] %v\n"
796 (const :tag "Unlimited" :format "%t" nil)
797 (integer :tag "Number" :value 2 :size 5))
798 (choice
799 :tag "Top"
800 :help-echo "Maximum number of top side windows."
801 :value nil
802 :format "%[Top%] %v\n"
803 (const :tag "Unlimited" :format "%t" nil)
804 (integer :tag "Number" :value 3 :size 5))
805 (choice
806 :tag "Right"
807 :help-echo "Maximum number of right side windows."
808 :value nil
809 :format "%[Right%] %v\n"
810 (const :tag "Unlimited" :format "%t" nil)
811 (integer :tag "Number" :value 2 :size 5))
812 (choice
813 :tag "Bottom"
814 :help-echo "Maximum number of bottom side windows."
815 :value nil
816 :format "%[Bottom%] %v\n"
817 (const :tag "Unlimited" :format "%t" nil)
818 (integer :tag "Number" :value 3 :size 5)))
819 :group 'windows)
821 (defvar-local window--sides-shown nil
822 "Non-nil if this buffer was shown in a side window once.
823 If this variable is non-nil in a buffer, `switch-to-prev-buffer'
824 and `switch-to-next-buffer' will refrain from showing this buffer
825 within the main window area. `display-buffer-in-side-window'
826 sets this variable automatically.
828 Killing buffer local variables after showing the buffer in a side
829 window annihilates any effect provided by this variable.")
831 (defvar window--sides-inhibit-check nil
832 "Non-nil means inhibit any checks on side windows.")
834 (defun window--sides-reverse-on-frame-p (frame)
835 "Return non-nil when side windows should appear reversed on FRAME.
836 This uses some heuristics to guess the user's intentions when the
837 selected window of FRAME is a side window ."
838 (cond
839 ;; Reverse when `window-sides-reversed' is t. Do not reverse when
840 ;; `window-sides-reversed' is nil.
841 ((memq window-sides-reversed '(nil t))
842 window-sides-reversed)
843 ;; Reverse when FRAME's selected window shows a right-to-left buffer.
844 ((let ((window (frame-selected-window frame)))
845 (when (and (not (window-parameter window 'window-side))
846 (or (not (window-minibuffer-p window))
847 (setq window (minibuffer-selected-window))))
848 (with-current-buffer (window-buffer window)
849 (eq bidi-paragraph-direction 'right-to-left)))))
850 ;; Reverse when FRAME's `window-sides-main-selected-window' parameter
851 ;; specifies a live window showing a right-to-left buffer.
852 ((let ((window (frame-parameter
853 frame 'window-sides-main-selected-window)))
854 (when (window-live-p window)
855 (with-current-buffer (window-buffer window)
856 (eq bidi-paragraph-direction 'right-to-left)))))
857 ;; Reverse when all windows in FRAME's main window show right-to-left
858 ;; buffers.
860 (catch 'found
861 (walk-window-subtree
862 (lambda (window)
863 (with-current-buffer (window-buffer window)
864 (when (eq bidi-paragraph-direction 'left-to-right)
865 (throw 'found nil))))
866 (window-main-window frame))
867 t))))
869 (defun window-main-window (&optional frame)
870 "Return the main window of specified FRAME.
871 The optional argument FRAME must be a live frame and defaults to
872 the selected one.
874 If FRAME has no side windows, return FRAME's root window.
875 Otherwise, return either an internal non-side window such that
876 all other non-side windows on FRAME descend from it, or the
877 single live non-side window of FRAME."
878 (let ((frame (window-normalize-frame frame))
879 main sibling)
880 ;; Set main to the _last_ window found by `walk-window-tree' that
881 ;; is not a side window but has a side window as its sibling.
882 (walk-window-tree
883 (lambda (window)
884 (and (not (window-parameter window 'window-side))
885 (or (and (setq sibling (window-prev-sibling window))
886 (window-parameter sibling 'window-side))
887 (and (setq sibling (window-next-sibling window))
888 (window-parameter sibling 'window-side)))
889 (setq main window)))
890 frame t 'nomini)
891 (or main (frame-root-window frame))))
893 (defun window--make-major-side-window-next-to (side)
894 "Return window to split for making a major side window.
895 SIDE must be one of the symbols `left', `top', `right' or
896 `bottom'.
898 This is an auxiliary function of `window--make-major-side-window'
899 and must not be called when a window on SIDE exists already."
900 (let ((root (frame-root-window))
901 (window--sides-inhibit-check t)
902 window)
903 ;; (1) If a window on the opposite side exists, return that window's
904 ;; sibling.
905 ;; (2) If the new window shall span the entire side, return the
906 ;; frame's root window.
907 ;; (3) If a window on an orthogonal side exists, return that
908 ;; window's sibling.
909 ;; (4) Otherwise return the frame's root window.
910 (cond
911 ((or (and (eq side 'left)
912 (setq window (window-with-parameter 'window-side 'right nil t)))
913 (and (eq side 'top)
914 (setq window (window-with-parameter 'window-side 'bottom nil t))))
915 (window-prev-sibling window))
916 ((or (and (eq side 'right)
917 (setq window (window-with-parameter 'window-side 'left nil t)))
918 (and (eq side 'bottom)
919 (setq window (window-with-parameter 'window-side 'top nil t))))
920 (window-next-sibling window))
921 ((memq side '(left right))
922 (cond
923 (window-sides-vertical
924 root)
925 ((setq window (window-with-parameter 'window-side 'top nil t))
926 (window-next-sibling window))
927 ((setq window (window-with-parameter 'window-side 'bottom nil t))
928 (window-prev-sibling window))
929 (t root)))
930 ((memq side '(top bottom))
931 (cond
932 ((not window-sides-vertical)
933 root)
934 ((setq window (window-with-parameter 'window-side 'left nil t))
935 (window-next-sibling window))
936 ((setq window (window-with-parameter 'window-side 'right nil t))
937 (window-prev-sibling window))
938 (t root))))))
940 (defun window--make-major-side-window (buffer side slot &optional alist)
941 "Display BUFFER in a new major side window on the selected frame.
942 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
943 specifies the slot to use. ALIST is an association list of
944 symbols and values as passed to `display-buffer-in-side-window'.
945 Return the new window, nil if its creation failed.
947 This is an auxiliary function of `display-buffer-in-side-window'
948 and may be called only if no window on SIDE exists yet."
949 (let* ((left-or-right (memq side '(left right)))
950 (next-to (window--make-major-side-window-next-to side))
951 (on-side (cond
952 ((eq side 'top) 'above)
953 ((eq side 'bottom) 'below)
954 (t side)))
955 (window--sides-inhibit-check t)
956 ;; The following two bindings will tell `split-window' to take
957 ;; the space for the new window from the selected frame's main
958 ;; window and not make a new parent window unless needed.
959 (window-combination-resize 'side)
960 (window-combination-limit nil)
961 (window (split-window-no-error next-to nil on-side)))
962 (when window
963 ;; Initialize `window-side' parameter of new window to SIDE and
964 ;; make that parameter persistent.
965 (set-window-parameter window 'window-side side)
966 (add-to-list 'window-persistent-parameters '(window-side . writable))
967 ;; Install `window-slot' parameter of new window and make that
968 ;; parameter persistent.
969 (set-window-parameter window 'window-slot slot)
970 (add-to-list 'window-persistent-parameters '(window-slot . writable))
971 ;; Auto-adjust height/width of new window unless a size has been
972 ;; explicitly requested.
973 (unless (if left-or-right
974 (cdr (assq 'window-width alist))
975 (cdr (assq 'window-height alist)))
976 (setq alist
977 (cons
978 (cons
979 (if left-or-right 'window-width 'window-height)
980 (/ (window-total-size (frame-root-window) left-or-right)
981 ;; By default use a fourth of the size of the frame's
982 ;; root window.
984 alist)))
985 (with-current-buffer buffer
986 (setq window--sides-shown t))
987 ;; Install BUFFER in new window and return WINDOW.
988 (window--display-buffer buffer window 'window alist 'side))))
990 (defun display-buffer-in-side-window (buffer alist)
991 "Display BUFFER in a side window of the selected frame.
992 ALIST is an association list of symbols and values. The
993 following special symbols can be used in ALIST.
995 `side' denotes the side of the frame where the new window shall
996 be located. Valid values are `bottom', `right', `top' and
997 `left'. The default is `bottom'.
999 `slot' if non-nil, specifies the window slot where to display
1000 BUFFER. A value of zero or nil means use the middle slot on
1001 the specified side. A negative value means use a slot
1002 preceding (that is, above or on the left of) the middle slot.
1003 A positive value means use a slot following (that is, below or
1004 on the right of) the middle slot. The default is zero.
1006 If the current frame size or the settings of `window-sides-slots'
1007 do not permit making a new window, a suitable existing window may
1008 be reused and have its `window-slot' parameter value accordingly
1009 modified.
1011 Unless `display-buffer-mark-dedicated' is non-nil, dedicate the
1012 side window used to BUFFER so that it does not get reused by
1013 other `display-buffer' action functions. Return the window used
1014 for displaying BUFFER, nil if no suitable window can be found.
1016 This function installs the `window-side' and `window-slot'
1017 parameters and makes them persistent. It neither modifies ALIST
1018 nor installs any other window parameters unless they have been
1019 explicitly provided via a `window-parameters' entry in ALIST."
1020 (let* ((side (or (cdr (assq 'side alist)) 'bottom))
1021 (slot (or (cdr (assq 'slot alist)) 0))
1022 (left-or-right (memq side '(left right)))
1023 ;; Softly dedicate window to BUFFER unless
1024 ;; `display-buffer-mark-dedicated' already asks for it.
1025 (dedicated (or display-buffer-mark-dedicated 'side)))
1026 (cond
1027 ((not (memq side '(top bottom left right)))
1028 (error "Invalid side %s specified" side))
1029 ((not (numberp slot))
1030 (error "Invalid slot %s specified" slot)))
1032 (let* ((major (window-with-parameter 'window-side side nil t))
1033 ;; `major' is the major window on SIDE, `windows' the list of
1034 ;; life windows on SIDE.
1035 (reversed (window--sides-reverse-on-frame-p (selected-frame)))
1036 (windows
1037 (cond
1038 ((window-live-p major)
1039 (list major))
1040 ((window-valid-p major)
1041 (let* ((first (window-child major))
1042 (next (window-next-sibling first))
1043 (windows (list next first)))
1044 (setq reversed (> (window-parameter first 'window-slot)
1045 (window-parameter next 'window-slot)))
1046 (while (setq next (window-next-sibling next))
1047 (setq windows (cons next windows)))
1048 (if reversed windows (nreverse windows))))))
1049 (slots (when major (max 1 (window-child-count major))))
1050 (max-slots
1051 (nth (cond
1052 ((eq side 'left) 0)
1053 ((eq side 'top) 1)
1054 ((eq side 'right) 2)
1055 ((eq side 'bottom) 3))
1056 window-sides-slots))
1057 (window--sides-inhibit-check t)
1058 window this-window this-slot prev-window next-window
1059 best-window best-slot abs-slot)
1061 (cond
1062 ((and (numberp max-slots) (<= max-slots 0))
1063 ;; No side-slots available on this side. Don't raise an error,
1064 ;; just return nil.
1065 nil)
1066 ((not windows)
1067 ;; No major side window exists on this side, make one.
1068 (window--make-major-side-window buffer side slot alist))
1070 ;; Scan windows on SIDE.
1071 (catch 'found
1072 (dolist (window windows)
1073 (setq this-slot (window-parameter window 'window-slot))
1074 (cond
1075 ;; The following should not happen and probably be checked
1076 ;; by window--sides-check.
1077 ((not (numberp this-slot)))
1078 ((= this-slot slot)
1079 ;; A window with a matching slot has been found.
1080 (setq this-window window)
1081 (throw 'found t))
1083 ;; Check if this window has a better slot value wrt the
1084 ;; slot of the window we want.
1085 (setq abs-slot
1086 (if (or (and (> this-slot 0) (> slot 0))
1087 (and (< this-slot 0) (< slot 0)))
1088 (abs (- slot this-slot))
1089 (+ (abs slot) (abs this-slot))))
1090 (unless (and best-slot (<= best-slot abs-slot))
1091 (setq best-window window)
1092 (setq best-slot abs-slot))
1093 (if reversed
1094 (cond
1095 ((<= this-slot slot)
1096 (setq next-window window))
1097 ((not prev-window)
1098 (setq prev-window window)))
1099 (cond
1100 ((<= this-slot slot)
1101 (setq prev-window window))
1102 ((not next-window)
1103 (setq next-window window))))))))
1105 ;; `this-window' is the first window with the same SLOT.
1106 ;; `prev-window' is the window with the largest slot < SLOT. A new
1107 ;; window will be created after it.
1108 ;; `next-window' is the window with the smallest slot > SLOT. A new
1109 ;; window will be created before it.
1110 ;; `best-window' is the window with the smallest absolute difference
1111 ;; of its slot and SLOT.
1112 (or (and this-window
1113 ;; Reuse `this-window'.
1114 (with-current-buffer buffer
1115 (setq window--sides-shown t))
1116 (window--display-buffer
1117 buffer this-window 'reuse alist dedicated))
1118 (and (or (not max-slots) (< slots max-slots))
1119 (or (and next-window
1120 ;; Make new window before `next-window'.
1121 (let ((next-side (if left-or-right 'above 'left))
1122 (window-combination-resize 'side))
1123 (setq window (split-window-no-error
1124 next-window nil next-side))))
1125 (and prev-window
1126 ;; Make new window after `prev-window'.
1127 (let ((prev-side (if left-or-right 'below 'right))
1128 (window-combination-resize 'side))
1129 (setq window (split-window-no-error
1130 prev-window nil prev-side)))))
1131 (set-window-parameter window 'window-slot slot)
1132 (with-current-buffer buffer
1133 (setq window--sides-shown t))
1134 (window--display-buffer
1135 buffer window 'window alist dedicated))
1136 (and best-window
1137 ;; Reuse `best-window'.
1138 (progn
1139 ;; Give best-window the new slot value.
1140 (set-window-parameter best-window 'window-slot slot)
1141 (with-current-buffer buffer
1142 (setq window--sides-shown t))
1143 (window--display-buffer
1144 buffer best-window 'reuse alist dedicated)))))))))
1146 (defun window-toggle-side-windows (&optional frame)
1147 "Toggle display of side windows on specified FRAME.
1148 FRAME must be a live frame and defaults to the selected one.
1150 If FRAME has at least one side window, delete all side
1151 windows on FRAME after saving FRAME's state in the
1152 FRAME's `window-state' frame parameter. Otherwise,
1153 restore any side windows recorded in FRAME's `window-state'
1154 parameter, leaving FRAME's main window alone. Signal an
1155 error if FRAME has no side windows and no saved state for
1156 it is found."
1157 (interactive)
1158 (let* ((frame (window-normalize-frame frame))
1159 (window--sides-inhibit-check t)
1160 state)
1161 (cond
1162 ((window-with-parameter 'window-side nil frame)
1163 ;; At least one side window exists. Remove all side windows after
1164 ;; saving FRAME's state in its `window-state' parameter.
1165 (set-frame-parameter
1166 frame 'window-state (window-state-get (frame-root-window frame)))
1167 (let ((ignore-window-parameters t))
1168 (delete-other-windows (window-main-window frame))))
1169 ((setq state (frame-parameter frame 'window-state))
1170 ;; A window state was saved for FRAME. Restore it and put the
1171 ;; current root window into its main window.
1172 (let ((main-state (window-state-get (frame-root-window frame))))
1173 (window-state-put state (frame-root-window frame) t)
1174 (window-state-put main-state (window-main-window frame)))
1175 (window--sides-reverse-frame frame))
1177 (error "No side windows state found")))))
1179 (defun window--sides-reverse-all ()
1180 "Maybe reverse side windows on all frames."
1181 (unless window--sides-inhibit-check
1182 (dolist (frame (frame-list))
1183 (window--sides-reverse-frame frame))))
1185 (defun window--sides-reverse-frame (frame)
1186 "Maybe reverse side windows on FRAME."
1187 (when (eq window-sides-reversed 'bidi)
1188 (let ((window (frame-selected-window frame)))
1189 (unless (or (window-parameter window 'window-side)
1190 (window-minibuffer-p window))
1191 (set-frame-parameter
1192 frame 'window-sides-main-selected-window window))))
1193 (window--sides-reverse-side frame 'top)
1194 (window--sides-reverse-side frame 'bottom))
1196 (defun window--sides-reverse-side (frame side)
1197 "Maybe reverse windows on SIDE of FRAME."
1198 (let ((major (window-with-parameter 'window-side side frame t))
1199 (window--sides-inhibit-check t))
1200 (when (and major (not (window-live-p major)))
1201 (let* ((first (window-child major))
1202 (reversed (> (window-parameter first 'window-slot)
1203 (window-parameter
1204 (window-next-sibling first) 'window-slot)))
1205 (reverse (window--sides-reverse-on-frame-p frame)))
1206 (unless (eq reversed reverse)
1207 ;; We have to reverse.
1208 (let ((last (window-last-child major)))
1209 (while (and (not (eq first last))
1210 (not (eq first (window-next-sibling last))))
1211 (window-swap-states first last t)
1212 (setq first (window-next-sibling first))
1213 (setq last (window-prev-sibling last)))))))))
1215 (defun window--sides-reverse (symbol value)
1216 "Helper function for customizing `window-sides-reversed'."
1217 (set-default symbol value)
1218 (remove-hook 'buffer-list-update-hook 'window--sides-reverse-all)
1219 (remove-hook 'window-configuration-change-hook 'window--sides-reverse-all)
1220 (dolist (frame (frame-list))
1221 (set-frame-parameter frame 'window-sides-main-selected-window nil))
1222 (when (eq value 'bidi)
1223 (add-hook 'buffer-list-update-hook 'window--sides-reverse-all)
1224 (add-hook 'window-configuration-change-hook 'window--sides-reverse-all))
1225 (window--sides-reverse-all))
1227 (defun window--sides-verticalize-frame (&optional frame)
1228 "Maybe change side windows layout on specified FRAME."
1229 (setq frame (window-normalize-frame frame))
1230 (let ((window--sides-inhibit-check t)
1231 (root (frame-root-window frame))
1232 (main (window-main-window frame)))
1233 (when (and (not (eq main root))
1234 (not (eq (window-parent main) root))
1235 (window-combined-p main window-sides-vertical))
1236 (let* ((window--sides-inhibit-check t)
1237 (ignore-window-parameters t)
1238 (first (window-child root))
1239 (first-state
1240 (and first (window-parameter first 'window-side)
1241 (window-state-get first)))
1242 (last (window-last-child root))
1243 (last-state
1244 (and last (window-parameter last 'window-side)
1245 (window-state-get last)))
1246 (dummy (get-buffer-create " *dummy*"))
1247 major)
1248 (unwind-protect
1249 (progn
1250 (when first-state (delete-window first))
1251 (when last-state (delete-window last))
1252 (when first-state
1253 (setq major (window--make-major-side-window
1254 dummy (if window-sides-vertical 'top 'left) 0))
1255 (window-state-put first-state major t))
1256 (when last-state
1257 (setq major (window--make-major-side-window
1258 dummy (if window-sides-vertical 'bottom 'right) 0))
1259 (window-state-put last-state major t)))
1260 (kill-buffer " *dummy*"))))))
1262 (defun window--sides-verticalize (symbol value)
1263 "Helper function for customizing `window-sides-vertical'."
1264 (set-default symbol value)
1265 (dolist (frame (frame-list))
1266 (window--sides-verticalize-frame frame)))
1268 (defun window--sides-check-failed (frame)
1269 "Helper function for `window--sides-check'."
1270 (catch 'failed
1271 ;; FRAME must have a main window.
1272 (unless (window-main-window frame)
1273 (error "Frame %s has no main window" frame)
1274 (throw 'failed t))
1275 ;; Now check the side windows.
1276 (dolist (side '(left top right bottom))
1277 (let ((window (window-with-parameter 'window-side side frame t)))
1278 (when window
1279 ;; If WINDOW is live there must be no other window on this frame
1280 ;; with the same `window-side' parameter.
1281 (if (window-live-p window)
1282 (walk-window-tree
1283 (lambda (this)
1284 (when (and (eq (window-parameter this 'window-side) side)
1285 (not (eq this window)))
1286 (error "Window %s has same side %s as window %s but no common parent"
1287 this side window)
1288 (throw 'failed t)))
1289 frame t 'nomini)
1290 (walk-window-tree
1291 (lambda (this)
1292 (if (eq (window-parent this) window)
1293 (unless (eq (window-parameter this 'window-side) side)
1294 (error "Window %s has not same side %s as its parent %s"
1295 this side window)
1296 (throw 'failed t))
1297 (when (and (eq (window-parameter this 'window-side) side)
1298 (not (eq this window)))
1299 (error "Window %s has same side %s as major side window %s but its parent is %s"
1300 this side window (window-parent this))
1301 (throw 'failed t))))
1302 frame t 'nomini)))))))
1304 (defun window--sides-check (frame)
1305 "Check side windows configuration of FRAME.
1306 In a valid side windows configuration there can be at most one
1307 internal side window on each side and all its children must be
1308 live and have the same `window-side' parameter and no other
1309 window with the same `window-side' parameter exists on FRAME. If
1310 there is no such internal window, there may be at most one window
1311 with this side's `window-side' parameter on FRAME.
1313 If the configuration is invalid, reset the `window-side'
1314 parameters of all windows on FRAME."
1315 (when (and (not window--sides-inhibit-check)
1316 (window-with-parameter 'window-side nil frame t)
1317 (window--sides-check-failed frame))
1318 ;; Reset all `window-side' parameters.
1319 (walk-window-tree
1320 (lambda (window)
1321 (set-window-parameter window 'window-side nil))
1322 frame t 'nomini)
1323 (message "Side windows configuration reset for frame %s" frame)))
1325 (defun window--check (&optional frame)
1326 "Check atomic and side windows on FRAME.
1327 FRAME defaults to the selected frame."
1328 (window--sides-check frame)
1329 (window--atom-check frame))
1331 ;; Dumping frame/window contents.
1332 (defun window--dump-window (&optional window erase)
1333 "Dump WINDOW to buffer *window-frame-dump*.
1334 WINDOW must be a valid window and defaults to the selected one.
1335 Optional argument ERASE non-nil means erase *window-frame-dump*
1336 before writing to it."
1337 (setq window (window-normalize-window window))
1338 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1339 (when erase (erase-buffer))
1340 (insert
1341 (format "%s parent: %s\n" window (window-parent window))
1342 (format "pixel left: %s top: %s size: %s x %s new: %s\n"
1343 (window-pixel-left window) (window-pixel-top window)
1344 (window-size window t t) (window-size window nil t)
1345 (window-new-pixel window))
1346 (format "char left: %s top: %s size: %s x %s new: %s\n"
1347 (window-left-column window) (window-top-line window)
1348 (window-total-size window t) (window-total-size window)
1349 (window-new-total window))
1350 (format "normal: %s x %s new: %s\n"
1351 (window-normal-size window t) (window-normal-size window)
1352 (window-new-normal window)))
1353 (when (window-live-p window)
1354 (let ((fringes (window-fringes window))
1355 (margins (window-margins window)))
1356 (insert
1357 (format "body pixel: %s x %s char: %s x %s\n"
1358 (window-body-width window t) (window-body-height window t)
1359 (window-body-width window) (window-body-height window))
1360 (format "width left fringe: %s left margin: %s right margin: %s\n"
1361 (car fringes) (or (car margins) 0) (or (cdr margins) 0))
1362 (format "width right fringe: %s scroll-bar: %s divider: %s\n"
1363 (cadr fringes)
1364 (window-scroll-bar-width window)
1365 (window-right-divider-width window))
1366 (format "height header-line: %s mode-line: %s divider: %s\n"
1367 (window-header-line-height window)
1368 (window-mode-line-height window)
1369 (window-bottom-divider-width window)))))
1370 (insert "\n")))
1372 (defun window--dump-frame (&optional window-or-frame)
1373 "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
1374 WINDOW-OR-FRAME can be a frame or a window and defaults to the
1375 selected frame. When WINDOW-OR-FRAME is a window, dump that
1376 window's frame. The buffer *window-frame-dump* is erased before
1377 dumping to it."
1378 (let* ((window
1379 (cond
1380 ((or (not window-or-frame)
1381 (frame-live-p window-or-frame))
1382 (frame-root-window window-or-frame))
1383 ((or (window-live-p window-or-frame)
1384 (window-child window-or-frame))
1385 window-or-frame)
1387 (frame-root-window))))
1388 (frame (window-frame window)))
1389 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1390 (erase-buffer)
1391 (insert
1392 (format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
1393 (frame-pixel-width frame) (frame-pixel-height frame)
1394 (frame-total-cols frame) (frame-total-lines frame)
1395 (frame-char-width frame) (frame-char-height frame))
1396 (format "frame text pixel: %s x %s cols/lines: %s x %s\n"
1397 (frame-text-width frame) (frame-text-height frame)
1398 (frame-text-cols frame) (frame-text-lines frame))
1399 (format "tool: %s scroll: %s/%s fringe: %s border: %s right: %s bottom: %s\n\n"
1400 (if (fboundp 'tool-bar-height)
1401 (tool-bar-height frame t)
1402 "0")
1403 (frame-scroll-bar-width frame)
1404 (frame-scroll-bar-height frame)
1405 (frame-fringe-width frame)
1406 (frame-border-width frame)
1407 (frame-right-divider-width frame)
1408 (frame-bottom-divider-width frame)))
1409 (walk-window-tree 'window--dump-window frame t t))))
1411 ;;; Window sizes.
1412 (defun window-total-size (&optional window horizontal round)
1413 "Return the total height or width of WINDOW.
1414 WINDOW must be a valid window and defaults to the selected one.
1416 If HORIZONTAL is omitted or nil, return the total height of
1417 WINDOW, in lines. If WINDOW is live, its total height includes,
1418 in addition to the height of WINDOW's text, the heights of
1419 WINDOW's mode and header line and a bottom divider, if any.
1421 If HORIZONTAL is non-nil, return the total width of WINDOW, in
1422 columns. If WINDOW is live, its total width includes, in
1423 addition to the width of WINDOW's text, the widths of WINDOW's
1424 fringes, margins, scroll bars and its right divider, if any.
1426 If WINDOW is internal, return the respective size of the screen
1427 areas spanned by its children.
1429 Optional argument ROUND is handled as for `window-total-height'
1430 and `window-total-width'."
1431 (if horizontal
1432 (window-total-width window round)
1433 (window-total-height window round)))
1435 (defun window-size (&optional window horizontal pixelwise round)
1436 "Return the height or width of WINDOW.
1437 WINDOW must be a valid window and defaults to the selected one.
1439 If HORIZONTAL is omitted or nil, return the total height of
1440 WINDOW, in lines, like `window-total-height'. Otherwise return
1441 the total width, in columns, like `window-total-width'.
1443 Optional argument PIXELWISE means return the pixel size of WINDOW
1444 like `window-pixel-height' and `window-pixel-width'.
1446 Optional argument ROUND is ignored if PIXELWISE is non-nil and
1447 handled as for `window-total-height' and `window-total-width'
1448 otherwise."
1449 (if horizontal
1450 (if pixelwise
1451 (window-pixel-width window)
1452 (window-total-width window round))
1453 (if pixelwise
1454 (window-pixel-height window)
1455 (window-total-height window round))))
1457 (defvar window-size-fixed nil
1458 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
1459 If the value is `height', then only the window's height is fixed.
1460 If the value is `width', then only the window's width is fixed.
1461 Any other non-nil value fixes both the width and the height.
1463 Emacs won't change the size of any window displaying that buffer,
1464 unless it has no other choice (like when deleting a neighboring
1465 window).")
1466 (make-variable-buffer-local 'window-size-fixed)
1468 (defun window--preservable-size (window &optional horizontal)
1469 "Return height of WINDOW as `window-preserve-size' would preserve it.
1470 Optional argument HORIZONTAL non-nil means to return the width of
1471 WINDOW as `window-preserve-size' would preserve it."
1472 (if horizontal
1473 (window-body-width window t)
1474 (+ (window-body-height window t)
1475 (window-header-line-height window)
1476 (window-mode-line-height window))))
1478 (defun window-preserve-size (&optional window horizontal preserve)
1479 "Preserve height of window WINDOW.
1480 WINDOW must be a live window and defaults to the selected one.
1481 Optional argument HORIZONTAL non-nil means preserve the width of
1482 WINDOW.
1484 PRESERVE t means to preserve the current height/width of WINDOW's
1485 body in frame and window resizing operations whenever possible.
1486 The height/width of WINDOW will change only if Emacs has no other
1487 choice. Resizing a window whose height/width is preserved never
1488 throws an error.
1490 PRESERVE nil means to stop preserving the height/width of WINDOW,
1491 lifting the respective restraint induced by a previous call of
1492 `window-preserve-size' for WINDOW. Calling `enlarge-window',
1493 `shrink-window', `split-window' or `fit-window-to-buffer' with
1494 WINDOW as argument also removes the respective restraint.
1496 Other values of PRESERVE are reserved for future use."
1497 (setq window (window-normalize-window window t))
1498 (let* ((parameter (window-parameter window 'window-preserved-size))
1499 (width (nth 1 parameter))
1500 (height (nth 2 parameter)))
1501 (if horizontal
1502 (set-window-parameter
1503 window 'window-preserved-size
1504 (list
1505 (window-buffer window)
1506 (and preserve (window--preservable-size window t))
1507 height))
1508 (set-window-parameter
1509 window 'window-preserved-size
1510 (list
1511 (window-buffer window)
1512 width
1513 (and preserve (window--preservable-size window)))))))
1515 (defun window-preserved-size (&optional window horizontal)
1516 "Return preserved height of window WINDOW.
1517 WINDOW must be a live window and defaults to the selected one.
1518 Optional argument HORIZONTAL non-nil means to return preserved
1519 width of WINDOW."
1520 (setq window (window-normalize-window window t))
1521 (let* ((parameter (window-parameter window 'window-preserved-size))
1522 (buffer (nth 0 parameter))
1523 (width (nth 1 parameter))
1524 (height (nth 2 parameter)))
1525 (when (eq buffer (window-buffer window))
1526 (if horizontal width height))))
1528 (defun window--preserve-size (window horizontal)
1529 "Return non-nil when the height of WINDOW shall be preserved.
1530 Optional argument HORIZONTAL non-nil means to return non-nil when
1531 the width of WINDOW shall be preserved."
1532 (let ((size (window-preserved-size window horizontal)))
1533 (and (numberp size)
1534 (= size (window--preservable-size window horizontal)))))
1536 (defun window-safe-min-size (&optional window horizontal pixelwise)
1537 "Return safe minimum size of WINDOW.
1538 WINDOW must be a valid window and defaults to the selected one.
1539 Optional argument HORIZONTAL non-nil means return the minimum
1540 number of columns of WINDOW; otherwise return the minimum number
1541 of WINDOW's lines.
1543 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1544 of WINDOW."
1545 (setq window (window-normalize-window window))
1546 (if pixelwise
1547 (if horizontal
1548 (* window-safe-min-width
1549 (frame-char-width (window-frame window)))
1550 (* window-safe-min-height
1551 (frame-char-height (window-frame window))))
1552 (if horizontal window-safe-min-width window-safe-min-height)))
1554 (defun window-min-size (&optional window horizontal ignore pixelwise)
1555 "Return the minimum size of WINDOW.
1556 WINDOW must be a valid window and defaults to the selected one.
1557 Optional argument HORIZONTAL non-nil means return the minimum
1558 number of columns of WINDOW; otherwise return the minimum number
1559 of WINDOW's lines.
1561 The optional argument IGNORE has the same meaning as for
1562 `window-resizable'. Optional argument PIXELWISE non-nil means
1563 return the minimum pixel-size of WINDOW."
1564 (window--min-size-1
1565 (window-normalize-window window) horizontal ignore pixelwise))
1567 (defun window--min-size-ignore-p (window ignore)
1568 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
1569 (if (window-valid-p ignore)
1570 (eq window ignore)
1571 (not (memq ignore '(nil preserved)))))
1573 (defun window--min-size-1 (window horizontal ignore pixelwise)
1574 "Internal function of `window-min-size'."
1575 (let ((sub (window-child window)))
1576 (if sub
1577 (let ((value 0))
1578 ;; WINDOW is an internal window.
1579 (if (window-combined-p sub horizontal)
1580 ;; The minimum size of an iso-combination is the sum of
1581 ;; the minimum sizes of its child windows.
1582 (while sub
1583 (setq value (+ value
1584 (window--min-size-1
1585 sub horizontal ignore pixelwise)))
1586 (setq sub (window-right sub)))
1587 ;; The minimum size of an ortho-combination is the maximum
1588 ;; of the minimum sizes of its child windows.
1589 (while sub
1590 (setq value (max value
1591 (window--min-size-1
1592 sub horizontal ignore pixelwise)))
1593 (setq sub (window-right sub))))
1594 value)
1595 (with-current-buffer (window-buffer window)
1596 (cond
1597 ((window-minibuffer-p window)
1598 (if pixelwise (frame-char-height (window-frame window)) 1))
1599 ((window-size-fixed-p window horizontal ignore)
1600 ;; The minimum size of a fixed size window is its size.
1601 (window-size window horizontal pixelwise))
1602 ((eq ignore 'safe)
1603 ;; If IGNORE equals `safe' return the safe value.
1604 (window-safe-min-size window horizontal pixelwise))
1605 (horizontal
1606 ;; For the minimum width of a window take fringes and
1607 ;; scroll-bars into account. This is questionable and should
1608 ;; be removed as soon as we are able to split (and resize)
1609 ;; windows such that the new (or resized) windows can get a
1610 ;; size less than the user-specified `window-min-height' and
1611 ;; `window-min-width'.
1612 (let* ((char-size (frame-char-size window t))
1613 (fringes (window-fringes window))
1614 (margins (window-margins window))
1615 ;; Let the 'min-margins' parameter override the actual
1616 ;; widths of the margins. We allow any number to
1617 ;; replace the values specified by `window-margins'.
1618 ;; See bug#24193 for the rationale of this parameter.
1619 (min-margins (window-parameter window 'min-margins))
1620 (left-min-margin (and min-margins
1621 (numberp (car min-margins))
1622 (car min-margins)))
1623 (right-min-margin (and min-margins
1624 (numberp (cdr min-margins))
1625 (cdr min-margins)))
1626 (pixel-width
1627 (+ (window-safe-min-size window t t)
1628 (* (or left-min-margin (car margins) 0) char-size)
1629 (* (or right-min-margin(cdr margins) 0) char-size)
1630 (car fringes) (cadr fringes)
1631 (window-scroll-bar-width window)
1632 (window-right-divider-width window))))
1633 (if pixelwise
1634 (max
1635 (if window-resize-pixelwise
1636 pixel-width
1637 ;; Round up to next integral of columns.
1638 (* (ceiling pixel-width char-size) char-size))
1639 (if (window--min-size-ignore-p window ignore)
1641 (window-min-pixel-width window)))
1642 (max
1643 (ceiling pixel-width char-size)
1644 (if (window--min-size-ignore-p window ignore)
1646 window-min-width)))))
1647 ((let ((char-size (frame-char-size window))
1648 (pixel-height
1649 (+ (window-safe-min-size window nil t)
1650 (window-header-line-height window)
1651 (window-scroll-bar-height window)
1652 (window-mode-line-height window)
1653 (window-bottom-divider-width window))))
1654 (if pixelwise
1655 (max
1656 (if window-resize-pixelwise
1657 pixel-height
1658 ;; Round up to next integral of lines.
1659 (* (ceiling pixel-height char-size) char-size))
1660 (if (window--min-size-ignore-p window ignore)
1662 (window-min-pixel-height window)))
1663 (max (ceiling pixel-height char-size)
1664 (if (window--min-size-ignore-p window ignore)
1666 window-min-height))))))))))
1668 (defun window-sizable (window delta &optional horizontal ignore pixelwise)
1669 "Return DELTA if DELTA lines can be added to WINDOW.
1670 WINDOW must be a valid window and defaults to the selected one.
1671 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1672 columns can be added to WINDOW. A return value of zero means
1673 that no lines (or columns) can be added to WINDOW.
1675 This function looks only at WINDOW and, recursively, its child
1676 windows. The function `window-resizable' looks at other windows
1677 as well.
1679 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1680 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1681 return the maximum value in the range 0..DELTA by which WINDOW
1682 can be enlarged.
1684 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1685 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1686 return the minimum value in the range DELTA..0 by which WINDOW
1687 can be shrunk.
1689 The optional argument IGNORE has the same meaning as for
1690 `window-resizable'. Optional argument PIXELWISE non-nil means
1691 interpret DELTA as pixels."
1692 (setq window (window-normalize-window window))
1693 (cond
1694 ((< delta 0)
1695 (max (- (window-min-size window horizontal ignore pixelwise)
1696 (window-size window horizontal pixelwise))
1697 delta))
1698 ((> delta 0)
1699 (if (window-size-fixed-p window horizontal ignore)
1701 delta))
1702 (t 0)))
1704 (defun window-sizable-p (window delta &optional horizontal ignore pixelwise)
1705 "Return t if WINDOW can be resized by DELTA lines.
1706 WINDOW must be a valid window and defaults to the selected one.
1707 For the meaning of the arguments of this function see the
1708 doc-string of `window-sizable'."
1709 (setq window (window-normalize-window window))
1710 (if (> delta 0)
1711 (>= (window-sizable window delta horizontal ignore pixelwise)
1712 delta)
1713 (<= (window-sizable window delta horizontal ignore pixelwise)
1714 delta)))
1716 (defun window--size-fixed-1 (window horizontal ignore)
1717 "Internal function for `window-size-fixed-p'."
1718 (let ((sub (window-child window)))
1719 (catch 'fixed
1720 (if sub
1721 ;; WINDOW is an internal window.
1722 (if (window-combined-p sub horizontal)
1723 ;; An iso-combination is fixed size if all its child
1724 ;; windows are fixed-size.
1725 (progn
1726 (while sub
1727 (unless (window--size-fixed-1 sub horizontal ignore)
1728 ;; We found a non-fixed-size child window, so
1729 ;; WINDOW's size is not fixed.
1730 (throw 'fixed nil))
1731 (setq sub (window-right sub)))
1732 ;; All child windows are fixed-size, so WINDOW's size is
1733 ;; fixed.
1734 (throw 'fixed t))
1735 ;; An ortho-combination is fixed-size if at least one of its
1736 ;; child windows is fixed-size.
1737 (while sub
1738 (when (window--size-fixed-1 sub horizontal ignore)
1739 ;; We found a fixed-size child window, so WINDOW's size
1740 ;; is fixed.
1741 (throw 'fixed t))
1742 (setq sub (window-right sub))))
1743 ;; WINDOW is a live window.
1744 (and (or (not (windowp ignore)) (not (eq window ignore)))
1745 (or (and (not (eq ignore 'preserved))
1746 (window--preserve-size window horizontal))
1747 (with-current-buffer (window-buffer window)
1748 (if horizontal
1749 (memq window-size-fixed '(width t))
1750 (memq window-size-fixed '(height t))))))))))
1752 (defun window-size-fixed-p (&optional window horizontal ignore)
1753 "Return non-nil if WINDOW's height is fixed.
1754 WINDOW must be a valid window and defaults to the selected one.
1755 Optional argument HORIZONTAL non-nil means return non-nil if
1756 WINDOW's width is fixed. The optional argument IGNORE has the
1757 same meaning as for `window-resizable'.
1759 If this function returns nil, this does not necessarily mean that
1760 WINDOW can be resized in the desired direction. The function
1761 `window-resizable' can tell that."
1762 (when (or (windowp ignore) (memq ignore '(nil preserved)))
1763 (window--size-fixed-1
1764 (window-normalize-window window) horizontal ignore)))
1766 (defun window--min-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
1767 "Internal function for `window-min-delta'."
1768 (if (not (window-parent window))
1769 ;; If we can't go up, return zero.
1771 ;; Else try to find a non-fixed-size sibling of WINDOW.
1772 (let* ((parent (window-parent window))
1773 (sub (window-child parent)))
1774 (catch 'done
1775 (if (window-combined-p sub horizontal)
1776 ;; In an iso-combination throw DELTA if we find at least one
1777 ;; child window and that window is either not fixed-size or
1778 ;; we can ignore fixed-sizeness.
1779 (let ((skip (eq trail 'after)))
1780 (while sub
1781 (cond
1782 ((eq sub window)
1783 (setq skip (eq trail 'before)))
1784 (skip)
1785 ((window-size-fixed-p sub horizontal ignore))
1787 ;; We found a non-fixed-size child window.
1788 (throw 'done delta)))
1789 (setq sub (window-right sub))))
1790 ;; In an ortho-combination set DELTA to the minimum value by
1791 ;; which other child windows can shrink.
1792 (while sub
1793 (unless (eq sub window)
1794 (setq delta
1795 (min delta
1796 (max (- (window-size sub horizontal pixelwise 'ceiling)
1797 (window-min-size
1798 sub horizontal ignore pixelwise))
1799 0))))
1800 (setq sub (window-right sub))))
1801 (if noup
1802 delta
1803 (window--min-delta-1
1804 parent delta horizontal ignore trail nil pixelwise))))))
1806 (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise)
1807 "Return number of lines by which WINDOW can be shrunk.
1808 WINDOW must be a valid window and defaults to the selected one.
1809 Return zero if WINDOW cannot be shrunk.
1811 Optional argument HORIZONTAL non-nil means return number of
1812 columns by which WINDOW can be shrunk.
1814 The optional argument IGNORE has the same meaning as for
1815 `window-resizable'. Optional argument TRAIL restricts the
1816 windows that can be enlarged. If its value is `before', only
1817 windows to the left of or above WINDOW can be enlarged. If it is
1818 `after', only windows to the right of or below WINDOW can be
1819 enlarged.
1821 Optional argument NOUP non-nil means don't go up in the window
1822 tree, but try to enlarge windows within WINDOW's combination
1823 only. Optional argument NODOWN non-nil means don't check whether
1824 WINDOW itself (and its child windows) can be shrunk; check only
1825 whether at least one other window can be enlarged appropriately.
1827 Optional argument PIXELWISE non-nil means return number of pixels
1828 by which WINDOW can be shrunk."
1829 (setq window (window-normalize-window window))
1830 (let ((size (window-size window horizontal pixelwise 'floor))
1831 (minimum (window-min-size window horizontal ignore pixelwise)))
1832 (cond
1833 (nodown
1834 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1835 (window--min-delta-1
1836 window size horizontal ignore trail noup pixelwise))
1837 ((<= size minimum)
1838 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1839 ;; there's nothing to recover.
1842 ;; Otherwise, try to recover whatever WINDOW is larger than its
1843 ;; minimum size.
1844 (window--min-delta-1
1845 window (- size minimum) horizontal ignore trail noup pixelwise)))))
1847 (defun frame-windows-min-size (&optional frame horizontal ignore pixelwise)
1848 "Return minimum number of lines of FRAME's windows.
1849 HORIZONTAL non-nil means return number of columns of FRAME's
1850 windows. The optional argument IGNORE has the same meaning as
1851 for `window-resizable'. PIXELWISE non-nil means return sizes in
1852 pixels."
1853 (setq frame (window-normalize-frame frame))
1854 (let* ((root (frame-root-window frame))
1855 (mini (window-next-sibling root)))
1856 (+ (window-min-size root horizontal ignore pixelwise)
1857 (if (and mini (not horizontal))
1858 (window-min-size mini horizontal nil pixelwise)
1859 0))))
1861 (defun window--max-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
1862 "Internal function of `window-max-delta'."
1863 (if (not (window-parent window))
1864 ;; Can't go up. Return DELTA.
1865 delta
1866 (let* ((parent (window-parent window))
1867 (sub (window-child parent)))
1868 (catch 'fixed
1869 (if (window-combined-p sub horizontal)
1870 ;; For an iso-combination calculate how much we can get from
1871 ;; other child windows.
1872 (let ((skip (eq trail 'after)))
1873 (while sub
1874 (cond
1875 ((eq sub window)
1876 (setq skip (eq trail 'before)))
1877 (skip)
1879 (setq delta
1880 (+ delta
1881 (max
1882 (- (window-size sub horizontal pixelwise 'floor)
1883 (window-min-size
1884 sub horizontal ignore pixelwise))
1885 0)))))
1886 (setq sub (window-right sub))))
1887 ;; For an ortho-combination throw DELTA when at least one
1888 ;; child window is fixed-size.
1889 (while sub
1890 (when (and (not (eq sub window))
1891 (window-size-fixed-p sub horizontal ignore))
1892 (throw 'fixed delta))
1893 (setq sub (window-right sub))))
1894 (if noup
1895 ;; When NOUP is nil, DELTA is all we can get.
1896 delta
1897 ;; Else try with parent of WINDOW, passing the DELTA we
1898 ;; recovered so far.
1899 (window--max-delta-1
1900 parent delta horizontal ignore trail nil pixelwise))))))
1902 (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise)
1903 "Return maximum number of lines by which WINDOW can be enlarged.
1904 WINDOW must be a valid window and defaults to the selected one.
1905 The return value is zero if WINDOW cannot be enlarged.
1907 Optional argument HORIZONTAL non-nil means return maximum number
1908 of columns by which WINDOW can be enlarged.
1910 The optional argument IGNORE has the same meaning as for
1911 `window-resizable'. Optional argument TRAIL restricts the
1912 windows that can be enlarged. If its value is `before', only
1913 windows to the left of or above WINDOW can be enlarged. If it is
1914 `after', only windows to the right of or below WINDOW can be
1915 enlarged.
1917 Optional argument NOUP non-nil means don't go up in the window
1918 tree but try to obtain the entire space from windows within
1919 WINDOW's combination. Optional argument NODOWN non-nil means do
1920 not check whether WINDOW itself (and its child windows) can be
1921 enlarged; check only whether other windows can be shrunk
1922 appropriately.
1924 Optional argument PIXELWISE non-nil means return number of
1925 pixels by which WINDOW can be enlarged."
1926 (setq window (window-normalize-window window))
1927 (if (and (not nodown) (window-size-fixed-p window horizontal ignore))
1928 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1929 ;; size.
1931 ;; WINDOW has no fixed size.
1932 (window--max-delta-1 window 0 horizontal ignore trail noup pixelwise)))
1934 ;; Make NOUP also inhibit the min-size check.
1935 (defun window--resizable (window delta &optional horizontal ignore trail noup nodown pixelwise)
1936 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1937 WINDOW must be a valid window and defaults to the selected one.
1938 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1939 can be resized horizontally by DELTA columns. A return value of
1940 zero means that WINDOW is not resizable.
1942 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1943 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1944 return the maximum value in the range 0..DELTA by which WINDOW
1945 can be enlarged.
1947 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1948 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1949 return the minimum value in the range DELTA..0 that can be used
1950 for shrinking WINDOW.
1952 The optional argument IGNORE has the same meaning as for
1953 `window-resizable'. Optional argument TRAIL `before' means only
1954 windows to the left of or below WINDOW can be shrunk. Optional
1955 argument TRAIL `after' means only windows to the right of or
1956 above WINDOW can be shrunk.
1958 Optional argument NOUP non-nil means don't go up in the window
1959 tree but check only whether space can be obtained from (or given
1960 to) WINDOW's siblings. Optional argument NODOWN non-nil means
1961 don't go down in the window tree. This means do not check
1962 whether resizing would violate size restrictions of WINDOW or its
1963 child windows.
1965 Optional argument PIXELWISE non-nil means interpret DELTA as
1966 number of pixels."
1967 (setq window (window-normalize-window window))
1968 (cond
1969 ((< delta 0)
1970 (max (- (window-min-delta
1971 window horizontal ignore trail noup nodown pixelwise))
1972 delta))
1973 ((> delta 0)
1974 (min (window-max-delta
1975 window horizontal ignore trail noup nodown pixelwise)
1976 delta))
1977 (t 0)))
1979 (defun window--resizable-p (window delta &optional horizontal ignore trail noup nodown pixelwise)
1980 "Return t if WINDOW can be resized vertically by DELTA lines.
1981 WINDOW must be a valid window and defaults to the selected one.
1982 For the meaning of the arguments of this function see the
1983 doc-string of `window--resizable'.
1985 Optional argument PIXELWISE non-nil means interpret DELTA as
1986 pixels."
1987 (setq window (window-normalize-window window))
1988 (if (> delta 0)
1989 (>= (window--resizable
1990 window delta horizontal ignore trail noup nodown pixelwise)
1991 delta)
1992 (<= (window--resizable
1993 window delta horizontal ignore trail noup nodown pixelwise)
1994 delta)))
1996 (defun window-resizable (window delta &optional horizontal ignore pixelwise)
1997 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1998 WINDOW must be a valid window and defaults to the selected one.
1999 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
2000 can be resized horizontally by DELTA columns. A return value of
2001 zero means that WINDOW is not resizable.
2003 DELTA positive means WINDOW shall be enlarged by DELTA lines or
2004 columns. If WINDOW cannot be enlarged by DELTA lines or columns
2005 return the maximum value in the range 0..DELTA by which WINDOW
2006 can be enlarged.
2008 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
2009 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
2010 return the minimum value in the range DELTA..0 that can be used
2011 for shrinking WINDOW.
2013 Optional argument IGNORE, if non-nil, means to ignore restraints
2014 induced by fixed size windows or the values of the variables
2015 `window-min-height' and `window-min-width'. The following values
2016 have special meanings: `safe' means that in addition live windows
2017 are allowed to get as small as `window-safe-min-height' lines and
2018 `window-safe-min-width' columns. `preserved' means to ignore
2019 only restrictions induced by `window-preserve-size'. If IGNORE
2020 is a window, then ignore restrictions for that window only.
2022 Optional argument PIXELWISE non-nil means interpret DELTA as
2023 pixels."
2024 (setq window (window-normalize-window window))
2025 (window--resizable window delta horizontal ignore nil nil nil pixelwise))
2027 (defun window-resizable-p (window delta &optional horizontal ignore pixelwise)
2028 "Return t if WINDOW can be resized vertically by DELTA lines.
2029 WINDOW must be a valid window and defaults to the selected one.
2030 For the meaning of the arguments of this function see the
2031 doc-string of `window-resizable'."
2032 (setq window (window-normalize-window window))
2033 (if (> delta 0)
2034 (>= (window--resizable
2035 window delta horizontal ignore nil nil nil pixelwise)
2036 delta)
2037 (<= (window--resizable
2038 window delta horizontal ignore nil nil nil pixelwise)
2039 delta)))
2041 ;; Aliases of functions defined in window.c.
2042 (defalias 'window-height 'window-total-height)
2043 (defalias 'window-width 'window-body-width)
2045 (defun window-full-height-p (&optional window)
2046 "Return t if WINDOW is as high as its containing frame.
2047 More precisely, return t if and only if the total height of
2048 WINDOW equals the total height of the root window of WINDOW's
2049 frame. WINDOW must be a valid window and defaults to the
2050 selected one."
2051 (setq window (window-normalize-window window))
2052 (if (window-minibuffer-p window)
2053 (eq window (frame-root-window (window-frame window)))
2054 (= (window-pixel-height window)
2055 (window-pixel-height (frame-root-window window)))))
2057 (defun window-full-width-p (&optional window)
2058 "Return t if WINDOW is as wide as its containing frame.
2059 More precisely, return t if and only if the total width of WINDOW
2060 equals the total width of the root window of WINDOW's frame.
2061 WINDOW must be a valid window and defaults to the selected one."
2062 (setq window (window-normalize-window window))
2063 (= (window-pixel-width window)
2064 (window-pixel-width (frame-root-window window))))
2066 (defun window-body-size (&optional window horizontal pixelwise)
2067 "Return the height or width of WINDOW's text area.
2068 WINDOW must be a live window and defaults to the selected one.
2070 If HORIZONTAL is omitted or nil, return the height of the text
2071 area, like `window-body-height'. Otherwise, return the width of
2072 the text area, like `window-body-width'. In either case, the
2073 optional argument PIXELWISE is passed to the functions."
2074 (if horizontal
2075 (window-body-width window pixelwise)
2076 (window-body-height window pixelwise)))
2078 (declare-function font-info "font.c" (name &optional frame))
2080 (defun window-font-width (&optional window face)
2081 "Return average character width for the font of FACE used in WINDOW.
2082 WINDOW must be a live window and defaults to the selected one.
2084 If FACE is nil or omitted, the default face is used. If FACE is
2085 remapped (see `face-remapping-alist'), the function returns the
2086 information for the remapped face."
2087 (with-selected-window (window-normalize-window window t)
2088 (if (display-multi-font-p)
2089 (let* ((face (if face face 'default))
2090 (info (font-info (face-font face)))
2091 (width (aref info 11)))
2092 (if (> width 0)
2093 width
2094 (aref info 10)))
2095 (frame-char-width))))
2097 (defun window-font-height (&optional window face)
2098 "Return character height for the font of FACE used in WINDOW.
2099 WINDOW must be a live window and defaults to the selected one.
2101 If FACE is nil or omitted, the default face is used. If FACE is
2102 remapped (see `face-remapping-alist'), the function returns the
2103 information for the remapped face."
2104 (with-selected-window (window-normalize-window window t)
2105 (if (display-multi-font-p)
2106 (let* ((face (if face face 'default))
2107 (info (font-info (face-font face))))
2108 (aref info 3))
2109 (frame-char-height))))
2111 (defvar overflow-newline-into-fringe)
2113 (defun window-max-chars-per-line (&optional window face)
2114 "Return the number of characters that can be displayed on one line in WINDOW.
2115 WINDOW must be a live window and defaults to the selected one.
2117 The character width of FACE is used for the calculation. If FACE
2118 is nil or omitted, the default face is used. If FACE is
2119 remapped (see `face-remapping-alist'), the function uses the
2120 remapped face.
2122 This function is different from `window-body-width' in two
2123 ways. First, it accounts for the portions of the line reserved
2124 for the continuation glyph. Second, it accounts for the size of
2125 the font."
2126 (with-selected-window (window-normalize-window window t)
2127 (let* ((window-width (window-body-width window t))
2128 (font-width (window-font-width window face))
2129 (ncols (/ window-width font-width)))
2130 (if (and (display-graphic-p)
2131 overflow-newline-into-fringe
2132 (not
2133 (or (eq left-fringe-width 0)
2134 (and (null left-fringe-width)
2135 (= (frame-parameter nil 'left-fringe) 0))))
2136 (not
2137 (or (eq right-fringe-width 0)
2138 (and (null right-fringe-width)
2139 (= (frame-parameter nil 'right-fringe) 0)))))
2140 ncols
2141 ;; FIXME: This should remove 1 more column when there are no
2142 ;; fringes, lines are truncated, and the window is hscrolled,
2143 ;; but EOL is not in the view, because then there are 2
2144 ;; truncation glyphs, not one.
2145 (1- ncols)))))
2147 (defun window-current-scroll-bars (&optional window)
2148 "Return the current scroll bar types for WINDOW.
2149 WINDOW must be a live window and defaults to the selected one.
2151 The return value is a cons cell (VERTICAL . HORIZONTAL) where
2152 VERTICAL specifies the current location of the vertical scroll
2153 bar (`left', `right' or nil), and HORIZONTAL specifies the
2154 current location of the horizontal scroll bar (`bottom' or nil).
2156 Unlike `window-scroll-bars', this function reports the scroll bar
2157 type actually used, once frame defaults and `scroll-bar-mode' are
2158 taken into account."
2159 (setq window (window-normalize-window window t))
2160 (let ((vertical (nth 2 (window-scroll-bars window)))
2161 (horizontal (nth 5 (window-scroll-bars window)))
2162 (inherited (frame-current-scroll-bars (window-frame window))))
2163 (when (eq vertical t)
2164 (setq vertical (car inherited)))
2165 (when (eq horizontal t)
2166 (setq horizontal (cdr inherited)))
2167 (cons vertical (and horizontal 'bottom))))
2169 (defun walk-windows (fun &optional minibuf all-frames)
2170 "Cycle through all live windows, calling FUN for each one.
2171 FUN must specify a function with a window as its sole argument.
2172 The optional arguments MINIBUF and ALL-FRAMES specify the set of
2173 windows to include in the walk.
2175 MINIBUF t means include the minibuffer window even if the
2176 minibuffer is not active. MINIBUF nil or omitted means include
2177 the minibuffer window only if the minibuffer is active. Any
2178 other value means do not include the minibuffer window even if
2179 the minibuffer is active.
2181 ALL-FRAMES nil or omitted means consider all windows on the
2182 selected frame, plus the minibuffer window if specified by the
2183 MINIBUF argument. If the minibuffer counts, consider all windows
2184 on all frames that share that minibuffer too. The following
2185 non-nil values of ALL-FRAMES have special meanings:
2187 - t means consider all windows on all existing frames.
2189 - `visible' means consider all windows on all visible frames on
2190 the current terminal.
2192 - 0 (the number zero) means consider all windows on all visible
2193 and iconified frames on the current terminal.
2195 - A frame means consider all windows on that frame only.
2197 Anything else means consider all windows on the selected frame
2198 and no others.
2200 This function changes neither the order of recently selected
2201 windows nor the buffer list."
2202 ;; If we start from the minibuffer window, don't fail to come
2203 ;; back to it.
2204 (when (window-minibuffer-p)
2205 (setq minibuf t))
2206 ;; Make sure to not mess up the order of recently selected
2207 ;; windows. Use `save-selected-window' and `select-window'
2208 ;; with second argument non-nil for this purpose.
2209 (save-selected-window
2210 (when (framep all-frames)
2211 (select-window (frame-first-window all-frames) 'norecord))
2212 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
2213 (funcall fun walk-windows-window))))
2215 (defun window-at-side-p (&optional window side)
2216 "Return t if WINDOW is at SIDE of its containing frame.
2217 WINDOW must be a valid window and defaults to the selected one.
2218 SIDE can be any of the symbols `left', `top', `right' or
2219 `bottom'. The default value nil is handled like `bottom'."
2220 (setq window (window-normalize-window window))
2221 (let ((edge
2222 (cond
2223 ((eq side 'left) 0)
2224 ((eq side 'top) 1)
2225 ((eq side 'right) 2)
2226 ((memq side '(bottom nil)) 3))))
2227 (= (nth edge (window-pixel-edges window))
2228 (nth edge (window-pixel-edges (frame-root-window window))))))
2230 (defun window-at-side-list (&optional frame side)
2231 "Return list of all windows on SIDE of FRAME.
2232 FRAME must be a live frame and defaults to the selected frame.
2233 SIDE can be any of the symbols `left', `top', `right' or
2234 `bottom'. The default value nil is handled like `bottom'."
2235 (setq frame (window-normalize-frame frame))
2236 (let (windows)
2237 (walk-window-tree
2238 (lambda (window)
2239 (when (window-at-side-p window side)
2240 (setq windows (cons window windows))))
2241 frame nil 'nomini)
2242 (nreverse windows)))
2244 (defun window--in-direction-2 (window posn &optional horizontal)
2245 "Support function for `window-in-direction'."
2246 (if horizontal
2247 (let ((top (window-pixel-top window)))
2248 (if (> top posn)
2249 (- top posn)
2250 (- posn top (window-pixel-height window))))
2251 (let ((left (window-pixel-left window)))
2252 (if (> left posn)
2253 (- left posn)
2254 (- posn left (window-pixel-width window))))))
2256 ;; Predecessors to the below have been devised by Julian Assange in
2257 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
2258 ;; Neither of these allow one to selectively ignore specific windows
2259 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
2260 ;; the movement.
2261 (defun window-in-direction (direction &optional window ignore sign wrap mini)
2262 "Return window in DIRECTION as seen from WINDOW.
2263 More precisely, return the nearest window in direction DIRECTION
2264 as seen from the position of `window-point' in window WINDOW.
2265 DIRECTION must be one of `above', `below', `left' or `right'.
2266 WINDOW must be a live window and defaults to the selected one.
2268 Do not return a window whose `no-other-window' parameter is
2269 non-nil. If the nearest window's `no-other-window' parameter is
2270 non-nil, try to find another window in the indicated direction.
2271 If, however, the optional argument IGNORE is non-nil, return that
2272 window even if its `no-other-window' parameter is non-nil.
2274 Optional argument SIGN a negative number means to use the right
2275 or bottom edge of WINDOW as reference position instead of
2276 `window-point'. SIGN a positive number means to use the left or
2277 top edge of WINDOW as reference position.
2279 Optional argument WRAP non-nil means to wrap DIRECTION around
2280 frame borders. This means to return for WINDOW at the top of the
2281 frame and DIRECTION `above' the minibuffer window if the frame
2282 has one, and a window at the bottom of the frame otherwise.
2284 Optional argument MINI nil means to return the minibuffer window
2285 if and only if it is currently active. MINI non-nil means to
2286 return the minibuffer window even when it's not active. However,
2287 if WRAP is non-nil, always act as if MINI were nil.
2289 Return nil if no suitable window can be found."
2290 (setq window (window-normalize-window window t))
2291 (unless (memq direction '(above below left right))
2292 (error "Wrong direction %s" direction))
2293 (let* ((frame (window-frame window))
2294 (hor (memq direction '(left right)))
2295 (first (if hor
2296 (window-pixel-left window)
2297 (window-pixel-top window)))
2298 (last (+ first (window-size window hor t)))
2299 ;; The column / row value of `posn-at-point' can be nil for the
2300 ;; mini-window, guard against that.
2301 (posn
2302 (cond
2303 ((and (numberp sign) (< sign 0))
2304 (if hor
2305 (1- (+ (window-pixel-top window) (window-pixel-height window)))
2306 (1- (+ (window-pixel-left window) (window-pixel-width window)))))
2307 ((and (numberp sign) (> sign 0))
2308 (if hor
2309 (window-pixel-top window)
2310 (window-pixel-left window)))
2311 ((let ((posn-cons (nth 2 (posn-at-point (window-point window) window))))
2312 (if hor
2313 (+ (or (cdr posn-cons) 1) (window-pixel-top window))
2314 (+ (or (car posn-cons) 1) (window-pixel-left window)))))))
2315 (best-edge
2316 (cond
2317 ((eq direction 'below) (frame-pixel-height frame))
2318 ((eq direction 'right) (frame-pixel-width frame))
2319 (t -1)))
2320 (best-edge-2 best-edge)
2321 (best-diff-2 (if hor (frame-pixel-height frame) (frame-pixel-width frame)))
2322 best best-2 best-diff-2-new)
2323 (walk-window-tree
2324 (lambda (w)
2325 (let* ((w-top (window-pixel-top w))
2326 (w-left (window-pixel-left w)))
2327 (cond
2328 ((or (eq window w)
2329 ;; Ignore ourselves.
2330 (and (window-parameter w 'no-other-window)
2331 ;; Ignore W unless IGNORE is non-nil.
2332 (not ignore))))
2333 (hor
2334 (cond
2335 ((and (<= w-top posn)
2336 (< posn (+ w-top (window-pixel-height w))))
2337 ;; W is to the left or right of WINDOW and covers POSN.
2338 (when (or (and (eq direction 'left)
2339 (or (and (<= w-left first) (> w-left best-edge))
2340 (and wrap
2341 (window-at-side-p window 'left)
2342 (window-at-side-p w 'right))))
2343 (and (eq direction 'right)
2344 (or (and (>= w-left last) (< w-left best-edge))
2345 (and wrap
2346 (window-at-side-p window 'right)
2347 (window-at-side-p w 'left)))))
2348 (setq best-edge w-left)
2349 (setq best w)))
2350 ((and (or (and (eq direction 'left)
2351 (<= (+ w-left (window-pixel-width w)) first))
2352 (and (eq direction 'right) (<= last w-left)))
2353 ;; W is to the left or right of WINDOW but does not
2354 ;; cover POSN.
2355 (setq best-diff-2-new
2356 (window--in-direction-2 w posn hor))
2357 (or (< best-diff-2-new best-diff-2)
2358 (and (= best-diff-2-new best-diff-2)
2359 (if (eq direction 'left)
2360 (> w-left best-edge-2)
2361 (< w-left best-edge-2)))))
2362 (setq best-edge-2 w-left)
2363 (setq best-diff-2 best-diff-2-new)
2364 (setq best-2 w))))
2365 ((and (<= w-left posn)
2366 (< posn (+ w-left (window-pixel-width w))))
2367 ;; W is above or below WINDOW and covers POSN.
2368 (when (or (and (eq direction 'above)
2369 (or (and (<= w-top first) (> w-top best-edge))
2370 (and wrap
2371 (window-at-side-p window 'top)
2372 (if (active-minibuffer-window)
2373 (minibuffer-window-active-p w)
2374 (window-at-side-p w 'bottom)))))
2375 (and (eq direction 'below)
2376 (or (and (>= w-top first) (< w-top best-edge))
2377 (and wrap
2378 (if (active-minibuffer-window)
2379 (minibuffer-window-active-p window)
2380 (window-at-side-p window 'bottom))
2381 (window-at-side-p w 'top)))))
2382 (setq best-edge w-top)
2383 (setq best w)))
2384 ((and (or (and (eq direction 'above)
2385 (<= (+ w-top (window-pixel-height w)) first))
2386 (and (eq direction 'below) (<= last w-top)))
2387 ;; W is above or below WINDOW but does not cover POSN.
2388 (setq best-diff-2-new
2389 (window--in-direction-2 w posn hor))
2390 (or (< best-diff-2-new best-diff-2)
2391 (and (= best-diff-2-new best-diff-2)
2392 (if (eq direction 'above)
2393 (> w-top best-edge-2)
2394 (< w-top best-edge-2)))))
2395 (setq best-edge-2 w-top)
2396 (setq best-diff-2 best-diff-2-new)
2397 (setq best-2 w)))))
2398 frame nil (and mini t))
2399 (or best best-2)))
2401 (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
2402 "Return a live window satisfying PREDICATE.
2403 More precisely, cycle through all windows calling the function
2404 PREDICATE on each one of them with the window as its sole
2405 argument. Return the first window for which PREDICATE returns
2406 non-nil. Windows are scanned starting with the window following
2407 the selected window. If no window satisfies PREDICATE, return
2408 DEFAULT.
2410 MINIBUF t means include the minibuffer window even if the
2411 minibuffer is not active. MINIBUF nil or omitted means include
2412 the minibuffer window only if the minibuffer is active. Any
2413 other value means do not include the minibuffer window even if
2414 the minibuffer is active.
2416 ALL-FRAMES nil or omitted means consider all windows on the selected
2417 frame, plus the minibuffer window if specified by the MINIBUF
2418 argument. If the minibuffer counts, consider all windows on all
2419 frames that share that minibuffer too. The following non-nil
2420 values of ALL-FRAMES have special meanings:
2422 - t means consider all windows on all existing frames.
2424 - `visible' means consider all windows on all visible frames on
2425 the current terminal.
2427 - 0 (the number zero) means consider all windows on all visible
2428 and iconified frames on the current terminal.
2430 - A frame means consider all windows on that frame only.
2432 Anything else means consider all windows on the selected frame
2433 and no others."
2434 (catch 'found
2435 (dolist (window (window-list-1
2436 (next-window nil minibuf all-frames)
2437 minibuf all-frames))
2438 (when (funcall predicate window)
2439 (throw 'found window)))
2440 default))
2442 (defalias 'some-window 'get-window-with-predicate)
2444 (defun get-lru-window (&optional all-frames dedicated not-selected)
2445 "Return the least recently used window on frames specified by ALL-FRAMES.
2446 Return a full-width window if possible. A minibuffer window is
2447 never a candidate. A dedicated window is never a candidate
2448 unless DEDICATED is non-nil, so if all windows are dedicated, the
2449 value is nil. Avoid returning the selected window if possible.
2450 Optional argument NOT-SELECTED non-nil means never return the
2451 selected window.
2453 The following non-nil values of the optional argument ALL-FRAMES
2454 have special meanings:
2456 - t means consider all windows on all existing frames.
2458 - `visible' means consider all windows on all visible frames on
2459 the current terminal.
2461 - 0 (the number zero) means consider all windows on all visible
2462 and iconified frames on the current terminal.
2464 - A frame means consider all windows on that frame only.
2466 Any other value of ALL-FRAMES means consider all windows on the
2467 selected frame and no others."
2468 (let (best-window best-time second-best-window second-best-time time)
2469 (dolist (window (window-list-1 nil 'nomini all-frames))
2470 (when (and (or dedicated (not (window-dedicated-p window)))
2471 (or (not not-selected) (not (eq window (selected-window)))))
2472 (setq time (window-use-time window))
2473 (if (or (eq window (selected-window))
2474 (not (window-full-width-p window)))
2475 (when (or (not second-best-time) (< time second-best-time))
2476 (setq second-best-time time)
2477 (setq second-best-window window))
2478 (when (or (not best-time) (< time best-time))
2479 (setq best-time time)
2480 (setq best-window window)))))
2481 (or best-window second-best-window)))
2483 (defun get-mru-window (&optional all-frames dedicated not-selected)
2484 "Return the most recently used 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-window best-time time)
2506 (dolist (window (window-list-1 nil 'nomini all-frames))
2507 (setq time (window-use-time window))
2508 (when (and (or dedicated (not (window-dedicated-p window)))
2509 (or (not not-selected) (not (eq window (selected-window))))
2510 (or (not best-time) (> time best-time)))
2511 (setq best-time time)
2512 (setq best-window window)))
2513 best-window))
2515 (defun get-largest-window (&optional all-frames dedicated not-selected)
2516 "Return the largest window on frames specified by ALL-FRAMES.
2517 A minibuffer window is never a candidate. A dedicated window is
2518 never a candidate unless DEDICATED is non-nil, so if all windows
2519 are dedicated, the value is nil. Optional argument NOT-SELECTED
2520 non-nil means never return the selected window.
2522 The following non-nil values of the optional argument ALL-FRAMES
2523 have special meanings:
2525 - t means consider all windows on all existing frames.
2527 - `visible' means consider all windows on all visible frames on
2528 the current terminal.
2530 - 0 (the number zero) means consider all windows on all visible
2531 and iconified frames on the current terminal.
2533 - A frame means consider all windows on that frame only.
2535 Any other value of ALL-FRAMES means consider all windows on the
2536 selected frame and no others."
2537 (let ((best-size 0)
2538 best-window size)
2539 (dolist (window (window-list-1 nil 'nomini all-frames))
2540 (when (and (or dedicated (not (window-dedicated-p window)))
2541 (or (not not-selected) (not (eq window (selected-window)))))
2542 (setq size (* (window-pixel-height window)
2543 (window-pixel-width window)))
2544 (when (> size best-size)
2545 (setq best-size size)
2546 (setq best-window window))))
2547 best-window))
2549 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
2550 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2551 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2552 and defaults to the current buffer. If the selected window displays
2553 BUFFER-OR-NAME, it will be the first in the resulting list.
2555 MINIBUF t means include the minibuffer window even if the
2556 minibuffer is not active. MINIBUF nil or omitted means include
2557 the minibuffer window only if the minibuffer is active. Any
2558 other value means do not include the minibuffer window even if
2559 the minibuffer is active.
2561 ALL-FRAMES nil or omitted means consider all windows on the
2562 selected frame, plus the minibuffer window if specified by the
2563 MINIBUF argument. If the minibuffer counts, consider all windows
2564 on all frames that share that minibuffer too. The following
2565 non-nil values of ALL-FRAMES have special meanings:
2567 - t means consider all windows on all existing frames.
2569 - `visible' means consider all windows on all visible frames on
2570 the current terminal.
2572 - 0 (the number zero) means consider all windows on all visible
2573 and iconified frames on the current terminal.
2575 - A frame means consider all windows on that frame only.
2577 Anything else means consider all windows on the selected frame
2578 and no others."
2579 (let ((buffer (window-normalize-buffer buffer-or-name))
2580 windows)
2581 (dolist (window (window-list-1 (selected-window) minibuf all-frames))
2582 (when (eq (window-buffer window) buffer)
2583 (setq windows (cons window windows))))
2584 (nreverse windows)))
2586 (defun minibuffer-window-active-p (window)
2587 "Return t if WINDOW is the currently active minibuffer window."
2588 (and (window-live-p window) (eq window (active-minibuffer-window))))
2590 (defun count-windows (&optional minibuf)
2591 "Return the number of live windows on the selected frame.
2592 The optional argument MINIBUF specifies whether the minibuffer
2593 window shall be counted. See `walk-windows' for the precise
2594 meaning of this argument."
2595 (length (window-list-1 nil minibuf)))
2597 ;;; Resizing windows.
2598 (defun window--size-to-pixel (window size &optional horizontal pixelwise round-maybe)
2599 "For WINDOW convert SIZE lines to pixels.
2600 SIZE is supposed to specify a height of WINDOW in terms of text
2601 lines. The return value is the number of pixels specifying that
2602 height.
2604 WINDOW must be a valid window. Optional argument HORIZONTAL
2605 non-nil means convert SIZE columns to pixels.
2607 Optional argument PIXELWISE non-nil means SIZE already specifies
2608 pixels but may have to be adjusted to a multiple of the character
2609 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2610 means round to the nearest multiple of the character size of
2611 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2612 (setq window (window-normalize-window window))
2613 (let ((char-size (frame-char-size window horizontal)))
2614 (if pixelwise
2615 (if (and round-maybe (not window-resize-pixelwise))
2616 (* (round size char-size) char-size)
2617 size)
2618 (* size char-size))))
2620 (defun window--pixel-to-total-1 (window horizontal char-size)
2621 "Subroutine of `window--pixel-to-total'."
2622 (let ((child (window-child window)))
2623 (if (window-combination-p window horizontal)
2624 ;; In an iso-combination distribute sizes proportionally.
2625 (let ((remainder (window-new-total window))
2626 size best-child rem best-rem)
2627 ;; Initialize total sizes to each child's floor.
2628 (while child
2629 (setq size (max (/ (window-size child horizontal t) char-size) 1))
2630 (set-window-new-total child size)
2631 (setq remainder (- remainder size))
2632 (setq child (window-next-sibling child)))
2633 ;; Distribute remainder.
2634 (while (> remainder 0)
2635 (setq child (window-last-child window))
2636 (setq best-child nil)
2637 (setq best-rem 0)
2638 (while child
2639 (when (and (<= (window-new-total child)
2640 (/ (window-size child horizontal t) char-size))
2641 (> (setq rem (% (window-size child horizontal t)
2642 char-size))
2643 best-rem))
2644 (setq best-child child)
2645 (setq best-rem rem))
2646 (setq child (window-prev-sibling child)))
2647 ;; We MUST have a best-child here.
2648 (set-window-new-total best-child 1 t)
2649 (setq remainder (1- remainder)))
2650 ;; Recurse.
2651 (setq child (window-child window))
2652 (while child
2653 (window--pixel-to-total-1 child horizontal char-size)
2654 (setq child (window-next-sibling child))))
2655 ;; In an ortho-combination assign new sizes directly.
2656 (let ((size (window-new-total window)))
2657 (while child
2658 (set-window-new-total child size)
2659 (window--pixel-to-total-1 child horizontal char-size)
2660 (setq child (window-next-sibling child)))))))
2662 (defun window--pixel-to-total (&optional frame horizontal)
2663 "On FRAME assign new total window heights from pixel heights.
2664 FRAME must be a live frame and defaults to the selected frame.
2666 Optional argument HORIZONTAL non-nil means assign new total
2667 window widths from pixel widths."
2668 (setq frame (window-normalize-frame frame))
2669 (let* ((char-size (frame-char-size frame horizontal))
2670 (root (frame-root-window frame))
2671 (root-size (window-size root horizontal t))
2672 ;; We have to care about the minibuffer window only if it
2673 ;; appears together with the root window on this frame.
2674 (mini (let ((mini (minibuffer-window frame)))
2675 (and (eq (window-frame mini) frame)
2676 (not (eq mini root)) mini)))
2677 (mini-size (and mini (window-size mini horizontal t))))
2678 ;; We round the line/column sizes of windows here to the nearest
2679 ;; integer. In some cases this can make windows appear _larger_
2680 ;; than the containing frame (line/column-wise) because the latter's
2681 ;; sizes are not (yet) rounded. We might eventually fix that.
2682 (if (and mini (not horizontal))
2683 (let (lines)
2684 (set-window-new-total root (max (/ root-size char-size) 1))
2685 (set-window-new-total mini (max (/ mini-size char-size) 1))
2686 (setq lines (- (round (+ root-size mini-size) char-size)
2687 (+ (window-new-total root) (window-new-total mini))))
2688 (while (> lines 0)
2689 (if (>= (% root-size (window-new-total root))
2690 (% mini-size (window-new-total mini)))
2691 (set-window-new-total root 1 t)
2692 (set-window-new-total mini 1 t))
2693 (setq lines (1- lines))))
2694 (set-window-new-total root (round root-size char-size))
2695 (when mini
2696 ;; This is taken in the horizontal case only.
2697 (set-window-new-total mini (round mini-size char-size))))
2698 (unless (window-buffer root)
2699 (window--pixel-to-total-1 root horizontal char-size))
2700 ;; Apply the new sizes.
2701 (window-resize-apply-total frame horizontal)))
2703 (defun window--resize-reset (&optional frame horizontal)
2704 "Reset resize values for all windows on FRAME.
2705 FRAME defaults to the selected frame.
2707 This function stores the current value of `window-size' applied
2708 with argument HORIZONTAL in the new total size of all windows on
2709 FRAME. It also resets the new normal size of each of these
2710 windows."
2711 (window--resize-reset-1
2712 (frame-root-window (window-normalize-frame frame)) horizontal))
2714 (defun window--resize-reset-1 (window horizontal)
2715 "Internal function of `window--resize-reset'."
2716 ;; Register old size in the new total size.
2717 (set-window-new-pixel window (window-size window horizontal t))
2718 (set-window-new-total window (window-size window horizontal))
2719 ;; Reset new normal size.
2720 (set-window-new-normal window)
2721 (when (window-child window)
2722 (window--resize-reset-1 (window-child window) horizontal))
2723 (when (window-right window)
2724 (window--resize-reset-1 (window-right window) horizontal)))
2726 (defun window--resize-mini-window (window delta)
2727 "Resize minibuffer window WINDOW by DELTA pixels.
2728 If WINDOW cannot be resized by DELTA pixels make it as large (or
2729 as small) as possible, but don't signal an error."
2730 (when (window-minibuffer-p window)
2731 (let* ((frame (window-frame window))
2732 (root (frame-root-window frame))
2733 (height (window-pixel-height window))
2734 (min-delta
2735 (- (window-pixel-height root)
2736 (window-min-size root nil nil t))))
2737 ;; Sanitize DELTA.
2738 (cond
2739 ((<= (+ height delta) 0)
2740 (setq delta (- (frame-char-height (window-frame window)) height)))
2741 ((> delta min-delta)
2742 (setq delta min-delta)))
2744 (unless (zerop delta)
2745 ;; Resize now.
2746 (window--resize-reset frame)
2747 ;; Ideally we should be able to resize just the last child of root
2748 ;; here. See the comment in `resize-root-window-vertically' for
2749 ;; why we do not do that.
2750 (window--resize-this-window root (- delta) nil nil t)
2751 (set-window-new-pixel window (+ height delta))
2752 ;; The following routine catches the case where we want to resize
2753 ;; a minibuffer-only frame.
2754 (when (resize-mini-window-internal window)
2755 (window--pixel-to-total frame)
2756 (run-window-configuration-change-hook frame))))))
2758 (defun window--resize-apply-p (frame &optional horizontal)
2759 "Return t when a window on FRAME shall be resized vertically.
2760 Optional argument HORIZONTAL non-nil means return t when a window
2761 shall be resized horizontally."
2762 (catch 'apply
2763 (walk-window-tree
2764 (lambda (window)
2765 (unless (= (window-new-pixel window)
2766 (window-size window horizontal t))
2767 (throw 'apply t)))
2768 frame t)
2769 nil))
2771 (defun window-resize (window delta &optional horizontal ignore pixelwise)
2772 "Resize WINDOW vertically by DELTA lines.
2773 WINDOW can be an arbitrary window and defaults to the selected
2774 one. An attempt to resize the root window of a frame will raise
2775 an error though.
2777 DELTA a positive number means WINDOW shall be enlarged by DELTA
2778 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2779 lines.
2781 Optional argument HORIZONTAL non-nil means resize WINDOW
2782 horizontally by DELTA columns. In this case a positive DELTA
2783 means enlarge WINDOW by DELTA columns. DELTA negative means
2784 WINDOW shall be shrunk by -DELTA columns.
2786 Optional argument IGNORE, if non-nil, means to ignore restraints
2787 induced by fixed size windows or the values of the variables
2788 `window-min-height' and `window-min-width'. The following values
2789 have special meanings: `safe' means that in addition live windows
2790 are allowed to get as small as `window-safe-min-height' lines and
2791 `window-safe-min-width' columns. `preserved' means to ignore
2792 only restrictions induced by `window-preserve-size'. If IGNORE
2793 is a window, then ignore restrictions for that window only.
2795 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2796 pixels.
2798 This function resizes other windows proportionally and never
2799 deletes any windows. If you want to move only the low (right)
2800 edge of WINDOW consider using `adjust-window-trailing-edge'
2801 instead."
2802 (setq window (window-normalize-window window))
2803 (let* ((frame (window-frame window))
2804 (minibuffer-window (minibuffer-window frame))
2805 sibling)
2806 (setq delta (window--size-to-pixel
2807 window delta horizontal pixelwise t))
2808 (cond
2809 ((eq window (frame-root-window frame))
2810 (error "Cannot resize the root window of a frame"))
2811 ((window-minibuffer-p window)
2812 (if horizontal
2813 (error "Cannot resize minibuffer window horizontally")
2814 (window--resize-mini-window window delta)))
2815 ((and (not horizontal)
2816 (window-full-height-p window)
2817 (eq (window-frame minibuffer-window) frame)
2818 (or (not resize-mini-windows)
2819 (eq minibuffer-window (active-minibuffer-window))))
2820 ;; If WINDOW is full height and either `resize-mini-windows' is
2821 ;; nil or the minibuffer window is active, resize the minibuffer
2822 ;; window.
2823 (window--resize-mini-window minibuffer-window (- delta)))
2824 ((or (window--resizable-p
2825 window delta horizontal ignore nil nil nil t)
2826 (and (not ignore)
2827 (setq ignore 'preserved)
2828 (window--resizable-p
2829 window delta horizontal ignore nil nil nil t)))
2830 (window--resize-reset frame horizontal)
2831 (window--resize-this-window window delta horizontal ignore t)
2832 (if (and (not (eq window-combination-resize t))
2833 (window-combined-p window horizontal)
2834 (setq sibling (or (window-right window) (window-left window)))
2835 (window-sizable-p
2836 sibling (- delta) horizontal ignore t))
2837 ;; If window-combination-resize is nil, WINDOW is part of an
2838 ;; iso-combination, and WINDOW's neighboring right or left
2839 ;; sibling can be resized as requested, resize that sibling.
2840 (let ((normal-delta
2841 (/ (float delta)
2842 (window-size (window-parent window) horizontal t))))
2843 (window--resize-this-window sibling (- delta) horizontal nil t)
2844 (set-window-new-normal
2845 window (+ (window-normal-size window horizontal)
2846 normal-delta))
2847 (set-window-new-normal
2848 sibling (- (window-normal-size sibling horizontal)
2849 normal-delta)))
2850 ;; Otherwise, resize all other windows in the same combination.
2851 (window--resize-siblings window delta horizontal ignore))
2852 (when (window--resize-apply-p frame horizontal)
2853 (if (window-resize-apply frame horizontal)
2854 (progn
2855 (window--pixel-to-total frame horizontal)
2856 (run-window-configuration-change-hook frame))
2857 (error "Failed to apply resizing %s" window))))
2859 (error "Cannot resize window %s" window)))))
2861 (defun window-resize-no-error (window delta &optional horizontal ignore pixelwise)
2862 "Resize WINDOW vertically if it is resizable by DELTA lines.
2863 This function is like `window-resize' but does not signal an
2864 error when WINDOW cannot be resized. For the meaning of the
2865 optional arguments see the documentation of `window-resize'."
2866 (when (window--resizable-p
2867 window delta horizontal ignore nil nil nil pixelwise)
2868 (window-resize window delta horizontal ignore pixelwise)))
2870 (defun window--resize-child-windows-skip-p (window)
2871 "Return non-nil if WINDOW shall be skipped by resizing routines."
2872 (memq (window-new-normal window) '(ignore stuck skip)))
2874 (defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
2875 "Recursively set new normal height of child windows of window PARENT.
2876 HORIZONTAL non-nil means set the new normal width of these
2877 windows. WINDOW specifies a child window of PARENT that has been
2878 resized by THIS-DELTA lines (columns).
2880 Optional argument TRAIL either `before' or `after' means set values
2881 only for windows before or after WINDOW. Optional argument
2882 OTHER-DELTA, a number, specifies that this many lines (columns)
2883 have been obtained from (or returned to) an ancestor window of
2884 PARENT in order to resize WINDOW."
2885 (let* ((delta-normal
2886 (if (and (= (- this-delta)
2887 (window-size window horizontal t))
2888 (zerop other-delta))
2889 ;; When WINDOW gets deleted and we can return its entire
2890 ;; space to its siblings, use WINDOW's normal size as the
2891 ;; normal delta.
2892 (- (window-normal-size window horizontal))
2893 ;; In any other case calculate the normal delta from the
2894 ;; relation of THIS-DELTA to the total size of PARENT.
2895 (/ (float this-delta)
2896 (window-size parent horizontal t))))
2897 (sub (window-child parent))
2898 (parent-normal 0.0)
2899 (skip (eq trail 'after)))
2901 ;; Set parent-normal to the sum of the normal sizes of all child
2902 ;; windows of PARENT that shall be resized, excluding only WINDOW
2903 ;; and any windows specified by the optional TRAIL argument.
2904 (while sub
2905 (cond
2906 ((eq sub window)
2907 (setq skip (eq trail 'before)))
2908 (skip)
2910 (setq parent-normal
2911 (+ parent-normal (window-normal-size sub horizontal)))))
2912 (setq sub (window-right sub)))
2914 ;; Set the new normal size of all child windows of PARENT from what
2915 ;; they should have contributed for recovering THIS-DELTA lines
2916 ;; (columns).
2917 (setq sub (window-child parent))
2918 (setq skip (eq trail 'after))
2919 (while sub
2920 (cond
2921 ((eq sub window)
2922 (setq skip (eq trail 'before)))
2923 (skip)
2925 (let ((old-normal (window-normal-size sub horizontal)))
2926 (set-window-new-normal
2927 sub (min 1.0 ; Don't get larger than 1.
2928 (max (- old-normal
2929 (* (/ old-normal parent-normal)
2930 delta-normal))
2931 ;; Don't drop below 0.
2932 0.0))))))
2933 (setq sub (window-right sub)))
2935 (when (numberp other-delta)
2936 ;; Set the new normal size of windows from what they should have
2937 ;; contributed for recovering OTHER-DELTA lines (columns).
2938 (setq delta-normal (/ (float (window-size parent horizontal t))
2939 (+ (window-size parent horizontal t)
2940 other-delta)))
2941 (setq sub (window-child parent))
2942 (setq skip (eq trail 'after))
2943 (while sub
2944 (cond
2945 ((eq sub window)
2946 (setq skip (eq trail 'before)))
2947 (skip)
2949 (set-window-new-normal
2950 sub (min 1.0 ; Don't get larger than 1.
2951 (max (* (window-new-normal sub) delta-normal)
2952 ;; Don't drop below 0.
2953 0.0)))))
2954 (setq sub (window-right sub))))
2956 ;; Set the new normal size of WINDOW to what is left by the sum of
2957 ;; the normal sizes of its siblings.
2958 (set-window-new-normal
2959 window
2960 (let ((sum 0))
2961 (setq sub (window-child parent))
2962 (while sub
2963 (cond
2964 ((eq sub window))
2965 ((not (numberp (window-new-normal sub)))
2966 (setq sum (+ sum (window-normal-size sub horizontal))))
2968 (setq sum (+ sum (window-new-normal sub)))))
2969 (setq sub (window-right sub)))
2970 ;; Don't get larger than 1 or smaller than 0.
2971 (min 1.0 (max (- 1.0 sum) 0.0))))))
2973 (defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge char-size)
2974 "Resize child windows of window PARENT vertically by DELTA pixels.
2975 PARENT must be a vertically combined internal window.
2977 Optional argument HORIZONTAL non-nil means resize child windows
2978 of PARENT horizontally by DELTA pixels. In this case PARENT must
2979 be a horizontally combined internal window.
2981 WINDOW, if specified, must denote a child window of PARENT that
2982 is resized by DELTA pixels.
2984 The optional argument IGNORE has the same meaning as for
2985 `window-resizable'.
2987 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2988 of windows that shall be resized. If TRAIL equals `before',
2989 resize only windows on the left or above EDGE. If TRAIL equals
2990 `after', resize only windows on the right or below EDGE. Also,
2991 preferably only resize windows adjacent to EDGE.
2993 If the optional argument CHAR-SIZE is a positive integer, it specifies
2994 the number of pixels by which windows are incrementally resized.
2995 If CHAR-SIZE is nil, this means to use the value of
2996 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2998 Return the symbol `normalized' if new normal sizes have been
2999 already set by this routine."
3000 (let* ((first (window-child parent))
3001 (last (window-last-child parent))
3002 (parent-total (+ (window-size parent horizontal t)
3003 delta))
3004 (char-size (or char-size
3005 (and window-resize-pixelwise 1)
3006 (frame-char-size window horizontal)))
3007 sub best-window best-value best-delta)
3009 (if (and edge (memq trail '(before after))
3010 (progn
3011 (setq sub first)
3012 (while (and (window-right sub)
3013 (or (and (eq trail 'before)
3014 (not (window--resize-child-windows-skip-p
3015 (window-right sub))))
3016 (and (eq trail 'after)
3017 (window--resize-child-windows-skip-p sub))))
3018 (setq sub (window-right sub)))
3019 sub)
3020 (if horizontal
3021 (if (eq trail 'before)
3022 (= (+ (window-pixel-left sub) (window-pixel-width sub))
3023 edge)
3024 (= (window-pixel-left sub) edge))
3025 (if (eq trail 'before)
3026 (= (+ (window-pixel-top sub) (window-pixel-height sub))
3027 edge)
3028 (= (window-pixel-top sub) edge)))
3029 (window-sizable-p sub delta horizontal ignore t))
3030 ;; Resize only windows adjacent to EDGE.
3031 (progn
3032 (window--resize-this-window
3033 sub delta horizontal ignore t trail edge)
3034 (if (and window (eq (window-parent sub) parent))
3035 (progn
3036 ;; Assign new normal sizes.
3037 (set-window-new-normal
3038 sub (/ (float (window-new-pixel sub)) parent-total))
3039 (set-window-new-normal
3040 window (- (window-normal-size window horizontal)
3041 (- (window-new-normal sub)
3042 (window-normal-size sub horizontal)))))
3043 (window--resize-child-windows-normal
3044 parent horizontal sub 0 trail delta))
3045 ;; Return 'normalized to notify `window--resize-siblings' that
3046 ;; normal sizes have been already set.
3047 'normalized)
3048 ;; Resize all windows proportionally.
3049 (setq sub last)
3050 (while sub
3051 (cond
3052 ((or (window--resize-child-windows-skip-p sub)
3053 ;; Ignore windows to skip and fixed-size child windows -
3054 ;; in the latter case make it a window to skip.
3055 (and (not ignore)
3056 (window-size-fixed-p sub horizontal ignore)
3057 (set-window-new-normal sub 'ignore))))
3058 ((< delta 0)
3059 ;; When shrinking store the number of lines/cols we can get
3060 ;; from this window here together with the total/normal size
3061 ;; factor.
3062 (set-window-new-normal
3064 (cons
3065 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
3066 (window-min-delta sub horizontal ignore trail t nil t)
3067 (- (/ (float (window-size sub horizontal t))
3068 parent-total)
3069 (window-normal-size sub horizontal)))))
3070 ((> delta 0)
3071 ;; When enlarging store the total/normal size factor only
3072 (set-window-new-normal
3074 (- (/ (float (window-size sub horizontal t))
3075 parent-total)
3076 (window-normal-size sub horizontal)))))
3078 (setq sub (window-left sub)))
3080 (cond
3081 ((< delta 0)
3082 ;; Shrink windows by delta.
3083 (setq best-window t)
3084 (while (and best-window (not (zerop delta)))
3085 (setq sub last)
3086 (setq best-window nil)
3087 (setq best-value most-negative-fixnum)
3088 (while sub
3089 (when (and (consp (window-new-normal sub))
3090 (not (<= (car (window-new-normal sub)) 0))
3091 (> (cdr (window-new-normal sub)) best-value))
3092 (setq best-window sub)
3093 (setq best-value (cdr (window-new-normal sub))))
3095 (setq sub (window-left sub)))
3097 (when best-window
3098 (setq best-delta (min (car (window-new-normal best-window))
3099 char-size (- delta)))
3100 (setq delta (+ delta best-delta))
3101 (set-window-new-pixel best-window (- best-delta) t)
3102 (set-window-new-normal
3103 best-window
3104 (if (= (car (window-new-normal best-window)) best-delta)
3105 'skip ; We can't shrink best-window any further.
3106 (cons (- (car (window-new-normal best-window)) best-delta)
3107 (- (/ (float (window-new-pixel best-window))
3108 parent-total)
3109 (window-normal-size best-window horizontal))))))))
3110 ((> delta 0)
3111 ;; Enlarge windows by delta.
3112 (setq best-window t)
3113 (while (and best-window (not (zerop delta)))
3114 (setq sub last)
3115 (setq best-window nil)
3116 (setq best-value most-positive-fixnum)
3117 (while sub
3118 (when (and (numberp (window-new-normal sub))
3119 (< (window-new-normal sub) best-value))
3120 (setq best-window sub)
3121 (setq best-value (window-new-normal sub)))
3123 (setq sub (window-left sub)))
3125 (when best-window
3126 (setq best-delta (min delta char-size))
3127 (setq delta (- delta best-delta))
3128 (set-window-new-pixel best-window best-delta t)
3129 (set-window-new-normal
3130 best-window
3131 (- (/ (float (window-new-pixel best-window))
3132 parent-total)
3133 (window-normal-size best-window horizontal)))))))
3135 (when best-window
3136 (setq sub last)
3137 (while sub
3138 (when (or (consp (window-new-normal sub))
3139 (numberp (window-new-normal sub)))
3140 ;; Reset new normal size fields so `window-resize-apply'
3141 ;; won't use them to apply new sizes.
3142 (set-window-new-normal sub))
3144 (unless (eq (window-new-normal sub) 'ignore)
3145 ;; Resize this window's child windows (back-engineering
3146 ;; delta from sub's old and new total sizes).
3147 (let ((delta (- (window-new-pixel sub)
3148 (window-size sub horizontal t))))
3149 (unless (and (zerop delta) (not trail))
3150 ;; For the TRAIL non-nil case we have to resize SUB
3151 ;; recursively even if it's size does not change.
3152 (window--resize-this-window
3153 sub delta horizontal ignore nil trail edge))))
3154 (setq sub (window-left sub)))))))
3156 (defun window--resize-siblings (window delta &optional horizontal ignore trail edge char-size)
3157 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
3158 Optional argument HORIZONTAL non-nil means resize other windows
3159 when WINDOW is resized horizontally by DELTA pixels. WINDOW
3160 itself is not resized by this function.
3162 The optional argument IGNORE has the same meaning as for
3163 `window-resizable'.
3165 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3166 of windows that shall be resized. If TRAIL equals `before',
3167 resize only windows on the left or above EDGE. If TRAIL equals
3168 `after', resize only windows on the right or below EDGE. Also,
3169 preferably only resize windows adjacent to EDGE."
3170 (when (window-parent window)
3171 (let* ((parent (window-parent window))
3172 (sub (window-child parent)))
3173 (if (window-combined-p sub horizontal)
3174 ;; In an iso-combination try to extract DELTA from WINDOW's
3175 ;; siblings.
3176 (let ((skip (eq trail 'after))
3177 this-delta other-delta)
3178 ;; Decide which windows shall be left alone.
3179 (while sub
3180 (cond
3181 ((eq sub window)
3182 ;; Make sure WINDOW is left alone when
3183 ;; resizing its siblings.
3184 (set-window-new-normal sub 'ignore)
3185 (setq skip (eq trail 'before)))
3186 (skip
3187 ;; Make sure this sibling is left alone when
3188 ;; resizing its siblings.
3189 (set-window-new-normal sub 'ignore))
3190 ((not (window-size-fixed-p sub horizontal ignore))
3191 ;; Set this-delta to t to signal that we found a sibling
3192 ;; of WINDOW whose size is not fixed.
3193 (setq this-delta t)))
3195 (setq sub (window-right sub)))
3197 ;; Set this-delta to what we can get from WINDOW's siblings.
3198 (if (= (- delta) (window-size window horizontal t))
3199 ;; A deletion, presumably. We must handle this case
3200 ;; specially since `window--resizable' can't be used.
3201 (if this-delta
3202 ;; There's at least one resizable sibling we can
3203 ;; give WINDOW's size to.
3204 (setq this-delta delta)
3205 ;; No resizable sibling exists.
3206 (setq this-delta 0))
3207 ;; Any other form of resizing.
3208 (setq this-delta
3209 (window--resizable
3210 window delta horizontal ignore trail t nil t)))
3212 ;; Set other-delta to what we still have to get from
3213 ;; ancestor windows of parent.
3214 (setq other-delta (- delta this-delta))
3215 (unless (zerop other-delta)
3216 ;; Unless we got everything from WINDOW's siblings, PARENT
3217 ;; must be resized by other-delta lines or columns.
3218 (set-window-new-pixel parent other-delta 'add))
3220 (if (zerop this-delta)
3221 ;; We haven't got anything from WINDOW's siblings but we
3222 ;; must update the normal sizes to respect other-delta.
3223 (window--resize-child-windows-normal
3224 parent horizontal window this-delta trail other-delta)
3225 ;; We did get something from WINDOW's siblings which means
3226 ;; we have to resize their child windows.
3227 (unless (eq (window--resize-child-windows
3228 parent (- this-delta) horizontal
3229 window ignore trail edge char-size)
3230 ;; If `window--resize-child-windows' returns
3231 ;; 'normalized, this means it has set the
3232 ;; normal sizes already.
3233 'normalized)
3234 ;; Set the normal sizes.
3235 (window--resize-child-windows-normal
3236 parent horizontal window this-delta trail other-delta))
3237 ;; Set DELTA to what we still have to get from ancestor
3238 ;; windows.
3239 (setq delta other-delta)))
3241 ;; In an ortho-combination all siblings of WINDOW must be
3242 ;; resized by DELTA.
3243 (set-window-new-pixel parent delta 'add)
3244 (while sub
3245 (unless (eq sub window)
3246 (window--resize-this-window
3247 sub delta horizontal ignore t))
3248 (setq sub (window-right sub))))
3250 (unless (zerop delta)
3251 ;; "Go up."
3252 (window--resize-siblings
3253 parent delta horizontal ignore trail edge char-size)))))
3255 (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge char-size)
3256 "Resize WINDOW vertically by DELTA pixels.
3257 Optional argument HORIZONTAL non-nil means resize WINDOW
3258 horizontally by DELTA pixels.
3260 The optional argument IGNORE has the same meaning as for
3261 `window-resizable'. Optional argument ADD non-nil means add
3262 DELTA to the new total size of WINDOW.
3264 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3265 of windows that shall be resized. If TRAIL equals `before',
3266 resize only windows on the left or above EDGE. If TRAIL equals
3267 `after', resize only windows on the right or below EDGE. Also,
3268 preferably only resize windows adjacent to EDGE.
3270 If the optional argument CHAR-SIZE is a positive integer, it specifies
3271 the number of pixels by which windows are incrementally resized.
3272 If CHAR-SIZE is nil, this means to use the value of
3273 `frame-char-height' or `frame-char-width' of WINDOW's frame.
3275 This function recursively resizes WINDOW's child windows to fit the
3276 new size. Make sure that WINDOW is `window--resizable' before
3277 calling this function. Note that this function does not resize
3278 siblings of WINDOW or WINDOW's parent window. You have to
3279 eventually call `window-resize-apply' in order to make resizing
3280 actually take effect."
3281 (when add
3282 ;; Add DELTA to the new total size of WINDOW.
3283 (set-window-new-pixel window delta t))
3285 (let ((sub (window-child window)))
3286 (cond
3287 ((not sub))
3288 ((window-combined-p sub horizontal)
3289 ;; In an iso-combination resize child windows according to their
3290 ;; normal sizes.
3291 (window--resize-child-windows
3292 window delta horizontal nil ignore trail edge char-size))
3293 ;; In an ortho-combination resize each child window by DELTA.
3295 (while sub
3296 (window--resize-this-window
3297 sub delta horizontal ignore t trail edge char-size)
3298 (setq sub (window-right sub)))))))
3300 (defun window--resize-root-window (window delta horizontal ignore pixelwise)
3301 "Resize root window WINDOW vertically by DELTA lines.
3302 HORIZONTAL non-nil means resize root window WINDOW horizontally
3303 by DELTA columns.
3305 IGNORE non-nil means ignore any restrictions imposed by fixed
3306 size windows, `window-min-height' or `window-min-width' settings.
3308 This function is only called by the frame resizing routines. It
3309 resizes windows proportionally and never deletes any windows."
3310 (when (and (windowp window) (numberp delta))
3311 (let ((pixel-delta
3312 (if pixelwise
3313 delta
3314 (window--size-to-pixel window delta horizontal))))
3315 (when (window-sizable-p window pixel-delta horizontal ignore t)
3316 (window--resize-reset (window-frame window) horizontal)
3317 (window--resize-this-window
3318 window pixel-delta horizontal ignore t)))))
3320 (defun window--resize-root-window-vertically (window delta pixelwise)
3321 "Resize root window WINDOW vertically by DELTA lines.
3322 If DELTA is less than zero and we can't shrink WINDOW by DELTA
3323 lines, shrink it as much as possible. If DELTA is greater than
3324 zero, this function can resize fixed-size windows in order to
3325 recover the necessary lines. Return the number of lines that
3326 were recovered.
3328 Third argument PIXELWISE non-nil means to interpret DELTA as
3329 pixels and return the number of pixels that were recovered.
3331 This function is called by the minibuffer window resizing
3332 routines."
3333 (let* ((frame (window-frame window))
3334 (pixel-delta
3335 (cond
3336 (pixelwise
3337 delta)
3338 ((numberp delta)
3339 (* (frame-char-height frame) delta))
3340 (t 0)))
3341 ignore)
3342 (cond
3343 ((zerop pixel-delta))
3344 ((< pixel-delta 0)
3345 (setq pixel-delta (window-sizable window pixel-delta nil nil pixelwise))
3346 (window--resize-reset frame)
3347 ;; When shrinking the root window, emulate an edge drag in order
3348 ;; to not resize other windows if we can avoid it (Bug#12419).
3349 (window--resize-this-window
3350 window pixel-delta nil ignore t 'before
3351 (+ (window-pixel-top window) (window-pixel-height window)))
3352 ;; Don't record new normal sizes to make sure that shrinking back
3353 ;; proportionally works as intended.
3354 (walk-window-tree
3355 (lambda (window) (set-window-new-normal window 'ignore)) frame t))
3356 ((> pixel-delta 0)
3357 (window--resize-reset frame)
3358 (unless (window-sizable window pixel-delta nil nil pixelwise)
3359 (setq ignore t))
3360 ;; When growing the root window, resize proportionally. This
3361 ;; should give windows back their original sizes (hopefully).
3362 (window--resize-this-window
3363 window pixel-delta nil ignore t)))
3364 ;; Return the possibly adjusted DELTA.
3365 (if pixelwise
3366 pixel-delta
3367 (/ pixel-delta (frame-char-height frame)))))
3369 (defun window--sanitize-window-sizes (horizontal)
3370 "Assert that all windows on selected frame are large enough.
3371 If necessary and possible, make sure that every window on frame
3372 FRAME has its minimum height. Optional argument HORIZONTAL
3373 non-nil means to make sure that every window on frame FRAME has
3374 its minimum width. The minimum height/width of a window is the
3375 respective value returned by `window-min-size' for that window.
3377 Return t if all windows were resized appropriately. Return nil
3378 if at least one window could not be resized as requested, which
3379 may happen when the FRAME is not large enough to accommodate it."
3380 (let ((value t))
3381 (walk-window-tree
3382 (lambda (window)
3383 (let ((delta (- (window-min-size window horizontal nil t)
3384 (window-size window horizontal t))))
3385 (when (> delta 0)
3386 (if (window-resizable-p window delta horizontal nil t)
3387 (window-resize window delta horizontal nil t)
3388 (setq value nil))))))
3389 value))
3391 (defun adjust-window-trailing-edge (window delta &optional horizontal pixelwise)
3392 "Move WINDOW's bottom edge by DELTA lines.
3393 Optional argument HORIZONTAL non-nil means move WINDOW's right
3394 edge by DELTA columns. WINDOW must be a valid window and
3395 defaults to the selected one.
3397 Optional argument PIXELWISE non-nil means interpret DELTA as
3398 number of pixels.
3400 If DELTA is greater than zero, move the edge downwards or to the
3401 right. If DELTA is less than zero, move the edge upwards or to
3402 the left. If the edge can't be moved by DELTA lines or columns,
3403 move it as far as possible in the desired direction."
3404 (setq window (window-normalize-window window))
3405 (let* ((frame (window-frame window))
3406 (minibuffer-window (minibuffer-window frame))
3407 (right window)
3408 left first-left first-right this-delta min-delta max-delta ignore)
3410 (unless pixelwise
3411 (setq pixelwise t)
3412 (setq delta (* delta (frame-char-size window horizontal))))
3414 ;; Find the edge we want to move.
3415 (while (and (or (not (window-combined-p right horizontal))
3416 (not (window-right right)))
3417 (setq right (window-parent right))))
3418 (cond
3419 ((and (not right) (not horizontal)
3420 ;; Resize the minibuffer window if it's on the same frame as
3421 ;; and immediately below WINDOW and it's either active or
3422 ;; `resize-mini-windows' is nil.
3423 (eq (window-frame minibuffer-window) frame)
3424 (= (nth 1 (window-pixel-edges minibuffer-window))
3425 (nth 3 (window-pixel-edges window)))
3426 (or (not resize-mini-windows)
3427 (eq minibuffer-window (active-minibuffer-window))))
3428 (window--resize-mini-window minibuffer-window (- delta)))
3429 ((or (not (setq left right)) (not (setq right (window-right right))))
3430 (if horizontal
3431 (user-error "No window on the right of this one")
3432 (user-error "No window below this one")))
3434 ;; Set LEFT to the first resizable window on the left. This step is
3435 ;; needed to handle fixed-size windows.
3436 (setq first-left left)
3437 (while (and left
3438 (or (window-size-fixed-p left horizontal)
3439 (and (< delta 0)
3440 (<= (window-size left horizontal t)
3441 (window-min-size left horizontal nil t)))))
3442 (setq left
3443 (or (window-left left)
3444 (progn
3445 (while (and (setq left (window-parent left))
3446 (not (window-combined-p left horizontal))))
3447 (window-left left)))))
3448 (unless left
3449 ;; We have to resize a size-preserved window. Start again with
3450 ;; the window initially on the left.
3451 (setq ignore 'preserved)
3452 (setq left first-left)
3453 (while (and left
3454 (or (window-size-fixed-p left horizontal 'preserved)
3455 (and (< delta 0)
3456 (<= (window-size left horizontal t)
3457 (window-min-size
3458 left horizontal 'preserved t)))))
3459 (setq left
3460 (or (window-left left)
3461 (progn
3462 (while (and (setq left (window-parent left))
3463 (not (window-combined-p left horizontal))))
3464 (window-left left)))))
3466 (unless left
3467 (if horizontal
3468 (user-error "No resizable window on the left of this one")
3469 (user-error "No resizable window above this one"))))
3471 ;; Set RIGHT to the first resizable window on the right. This step
3472 ;; is needed to handle fixed-size windows.
3473 (setq first-right right)
3474 (while (and right
3475 (or (window-size-fixed-p right horizontal)
3476 (and (> delta 0)
3477 (<= (window-size right horizontal t)
3478 (window-min-size
3479 right horizontal 'preserved t)))))
3480 (setq right
3481 (or (window-right right)
3482 (progn
3483 (while (and (setq right (window-parent right))
3484 (not (window-combined-p right horizontal))))
3485 (window-right right)))))
3486 (unless right
3487 ;; We have to resize a size-preserved window. Start again with
3488 ;; the window initially on the right.
3489 (setq ignore 'preserved)
3490 (setq right first-right)
3491 (while (and right
3492 (or (window-size-fixed-p right horizontal 'preserved)
3493 (and (> delta 0)
3494 (<= (window-size right horizontal t)
3495 (window-min-size
3496 right horizontal 'preserved t)))))
3497 (setq right
3498 (or (window-right right)
3499 (progn
3500 (while (and (setq right (window-parent right))
3501 (not (window-combined-p right horizontal))))
3502 (window-right right)))))
3503 (unless right
3504 (if horizontal
3505 (user-error "No resizable window on the right of this one")
3506 (user-error "No resizable window below this one"))))
3508 ;; LEFT and RIGHT (which might be both internal windows) are now the
3509 ;; two windows we want to resize.
3510 (cond
3511 ((> delta 0)
3512 (setq max-delta
3513 (window--max-delta-1
3514 left 0 horizontal ignore 'after nil pixelwise))
3515 (setq min-delta
3516 (window--min-delta-1
3517 right (- delta) horizontal ignore 'before nil pixelwise))
3518 (when (or (< max-delta delta) (> min-delta (- delta)))
3519 ;; We can't get the whole DELTA - move as far as possible.
3520 (setq delta (min max-delta (- min-delta))))
3521 (unless (zerop delta)
3522 ;; Start resizing.
3523 (window--resize-reset frame horizontal)
3524 ;; Try to enlarge LEFT first.
3525 (setq this-delta
3526 (window--resizable
3527 left delta horizontal ignore 'after nil nil pixelwise))
3528 (unless (zerop this-delta)
3529 (window--resize-this-window
3530 left this-delta horizontal ignore t 'before
3531 (if horizontal
3532 (+ (window-pixel-left left) (window-pixel-width left))
3533 (+ (window-pixel-top left) (window-pixel-height left)))))
3534 ;; Shrink windows on right of LEFT.
3535 (window--resize-siblings
3536 left delta horizontal ignore 'after
3537 (if horizontal
3538 (window-pixel-left right)
3539 (window-pixel-top right)))))
3540 ((< delta 0)
3541 (setq max-delta
3542 (window--max-delta-1
3543 right 0 horizontal ignore 'before nil pixelwise))
3544 (setq min-delta
3545 (window--min-delta-1
3546 left delta horizontal ignore 'after nil pixelwise))
3547 (when (or (< max-delta (- delta)) (> min-delta delta))
3548 ;; We can't get the whole DELTA - move as far as possible.
3549 (setq delta (max (- max-delta) min-delta)))
3550 (unless (zerop delta)
3551 ;; Start resizing.
3552 (window--resize-reset frame horizontal)
3553 ;; Try to enlarge RIGHT.
3554 (setq this-delta
3555 (window--resizable
3556 right (- delta) horizontal ignore 'before nil nil pixelwise))
3557 (unless (zerop this-delta)
3558 (window--resize-this-window
3559 right this-delta horizontal ignore t 'after
3560 (if horizontal
3561 (window-pixel-left right)
3562 (window-pixel-top right))))
3563 ;; Shrink windows on left of RIGHT.
3564 (window--resize-siblings
3565 right (- delta) horizontal ignore 'before
3566 (if horizontal
3567 (+ (window-pixel-left left) (window-pixel-width left))
3568 (+ (window-pixel-top left) (window-pixel-height left)))))))
3569 (unless (zerop delta)
3570 ;; Don't report an error in the standard case.
3571 (when (window--resize-apply-p frame horizontal)
3572 (if (window-resize-apply frame horizontal)
3573 (progn
3574 (window--pixel-to-total frame horizontal)
3575 (run-window-configuration-change-hook frame))
3576 ;; But do report an error if applying the changes fails.
3577 (error "Failed adjusting window %s" window))))))))
3579 (defun enlarge-window (delta &optional horizontal)
3580 "Make the selected window DELTA lines taller.
3581 Interactively, if no argument is given, make the selected window
3582 one line taller. If optional argument HORIZONTAL is non-nil,
3583 make selected window wider by DELTA columns. If DELTA is
3584 negative, shrink selected window by -DELTA lines or columns."
3585 (interactive "p")
3586 (let ((minibuffer-window (minibuffer-window)))
3587 (when (window-preserved-size nil horizontal)
3588 (window-preserve-size nil horizontal))
3589 (cond
3590 ((zerop delta))
3591 ((window-size-fixed-p nil horizontal)
3592 (user-error "Selected window has fixed size"))
3593 ((window-minibuffer-p)
3594 (if horizontal
3595 (user-error "Cannot resize minibuffer window horizontally")
3596 (window--resize-mini-window
3597 (selected-window) (* delta (frame-char-height)))))
3598 ((and (not horizontal)
3599 (window-full-height-p)
3600 (eq (window-frame minibuffer-window) (selected-frame))
3601 (not resize-mini-windows))
3602 ;; If the selected window is full height and `resize-mini-windows'
3603 ;; is nil, resize the minibuffer window.
3604 (window--resize-mini-window
3605 minibuffer-window (* (- delta) (frame-char-height))))
3606 ((window--resizable-p nil delta horizontal)
3607 (window-resize nil delta horizontal))
3608 ((window--resizable-p nil delta horizontal 'preserved)
3609 (window-resize nil delta horizontal 'preserved))
3610 ((eq this-command
3611 (if horizontal 'enlarge-window-horizontally 'enlarge-window))
3612 ;; For backward compatibility don't signal an error unless this
3613 ;; command is `enlarge-window(-horizontally)'.
3614 (user-error "Cannot enlarge selected window"))
3616 (window-resize
3617 nil (if (> delta 0)
3618 (window-max-delta nil horizontal)
3619 (- (window-min-delta nil horizontal)))
3620 horizontal)))))
3622 (defun shrink-window (delta &optional horizontal)
3623 "Make the selected window DELTA lines smaller.
3624 Interactively, if no argument is given, make the selected window
3625 one line smaller. If optional argument HORIZONTAL is non-nil,
3626 make selected window narrower by DELTA columns. If DELTA is
3627 negative, enlarge selected window by -DELTA lines or columns."
3628 (interactive "p")
3629 (let ((minibuffer-window (minibuffer-window)))
3630 (when (window-preserved-size nil horizontal)
3631 (window-preserve-size nil horizontal))
3632 (cond
3633 ((zerop delta))
3634 ((window-size-fixed-p nil horizontal)
3635 (user-error "Selected window has fixed size"))
3636 ((window-minibuffer-p)
3637 (if horizontal
3638 (user-error "Cannot resize minibuffer window horizontally")
3639 (window--resize-mini-window
3640 (selected-window) (* (- delta) (frame-char-height)))))
3641 ((and (not horizontal)
3642 (window-full-height-p)
3643 (eq (window-frame minibuffer-window) (selected-frame))
3644 (not resize-mini-windows))
3645 ;; If the selected window is full height and `resize-mini-windows'
3646 ;; is nil, resize the minibuffer window.
3647 (window--resize-mini-window
3648 minibuffer-window (* delta (frame-char-height))))
3649 ((window--resizable-p nil (- delta) horizontal)
3650 (window-resize nil (- delta) horizontal))
3651 ((window--resizable-p nil (- delta) horizontal 'preserved)
3652 (window-resize nil (- delta) horizontal 'preserved))
3653 ((eq this-command
3654 (if horizontal 'shrink-window-horizontally 'shrink-window))
3655 ;; For backward compatibility don't signal an error unless this
3656 ;; command is `shrink-window(-horizontally)'.
3657 (user-error "Cannot shrink selected window"))
3659 (window-resize
3660 nil (if (> delta 0)
3661 (- (window-min-delta nil horizontal))
3662 (window-max-delta nil horizontal))
3663 horizontal)))))
3665 (defun maximize-window (&optional window)
3666 "Maximize WINDOW.
3667 Make WINDOW as large as possible without deleting any windows.
3668 WINDOW must be a valid window and defaults to the selected one.
3670 If the option `window-resize-pixelwise' is non-nil maximize
3671 WINDOW pixelwise."
3672 (interactive)
3673 (setq window (window-normalize-window window))
3674 (window-resize
3675 window (window-max-delta window nil nil nil nil nil window-resize-pixelwise)
3676 nil nil window-resize-pixelwise)
3677 (window-resize
3678 window (window-max-delta window t nil nil nil nil window-resize-pixelwise)
3679 t nil window-resize-pixelwise))
3681 (defun minimize-window (&optional window)
3682 "Minimize WINDOW.
3683 Make WINDOW as small as possible without deleting any windows.
3684 WINDOW must be a valid window and defaults to the selected one.
3686 If the option `window-resize-pixelwise' is non-nil minimize
3687 WINDOW pixelwise."
3688 (interactive)
3689 (setq window (window-normalize-window window))
3690 (window-resize
3691 window
3692 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise))
3693 nil nil window-resize-pixelwise)
3694 (window-resize
3695 window
3696 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise))
3697 t nil window-resize-pixelwise))
3699 ;;; Window edges
3700 (defun window-edges (&optional window body absolute pixelwise)
3701 "Return a list of the edge distances of WINDOW.
3702 WINDOW must be a valid window and defaults to the selected one.
3703 The list returned has the form (LEFT TOP RIGHT BOTTOM).
3705 If the optional argument BODY is nil, this means to return the
3706 edges corresponding to the total size of WINDOW. BODY non-nil
3707 means to return the edges of WINDOW's body (aka text area). If
3708 BODY is non-nil, WINDOW must specify a live window.
3710 Optional argument ABSOLUTE nil means to return edges relative to
3711 the position of WINDOW's native frame. ABSOLUTE non-nil means to
3712 return coordinates relative to the origin - the position (0, 0) -
3713 of FRAME's display. On non-graphical systems this argument has
3714 no effect.
3716 Optional argument PIXELWISE nil means to return the coordinates
3717 in terms of the canonical character width and height of WINDOW's
3718 frame, rounded if necessary. PIXELWISE non-nil means to return
3719 the coordinates in pixels where the values for RIGHT and BOTTOM
3720 are one more than the actual value of these edges. Note that if
3721 ABSOLUTE is non-nil, PIXELWISE is implicitly non-nil too."
3722 (let* ((window (window-normalize-window window body))
3723 (frame (window-frame window))
3724 (border-width (frame-internal-border-width frame))
3725 (char-width (frame-char-width frame))
3726 (char-height (frame-char-height frame))
3727 (left (if pixelwise
3728 (+ (window-pixel-left window) border-width)
3729 (+ (window-left-column window)
3730 (/ border-width char-width))))
3731 (left-body
3732 (when body
3733 (+ (window-pixel-left window) border-width
3734 (if (eq (car (window-current-scroll-bars window)) 'left)
3735 (window-scroll-bar-width window)
3737 (nth 0 (window-fringes window))
3738 (* (or (nth 0 (window-margins window)) 0) char-width))))
3739 (top (if pixelwise
3740 (+ (window-pixel-top window) border-width)
3741 (+ (window-top-line window)
3742 (/ border-width char-height))))
3743 (top-body
3744 (when body
3745 (+ (window-pixel-top window) border-width
3746 (window-header-line-height window))))
3747 (right (+ left (if pixelwise
3748 (window-pixel-width window)
3749 (window-total-width window))))
3750 (right-body (and body (+ left-body (window-body-width window t))))
3751 (bottom (+ top (if pixelwise
3752 (window-pixel-height window)
3753 (window-total-height window))))
3754 (bottom-body (and body (+ top-body (window-body-height window t)))))
3755 (if absolute
3756 (let* ((native-edges (frame-edges frame 'native-edges))
3757 (left-off (nth 0 native-edges))
3758 (top-off (nth 1 native-edges)))
3759 (if body
3760 (list (+ left-body left-off) (+ top-body top-off)
3761 (+ right-body left-off) (+ bottom-body top-off))
3762 (list (+ left left-off) (+ top top-off)
3763 (+ right left-off) (+ bottom top-off))))
3764 (if body
3765 (if pixelwise
3766 (list left-body top-body right-body bottom-body)
3767 (list (/ left-body char-width) (/ top-body char-height)
3768 ;; Round up.
3769 (/ (+ right-body char-width -1) char-width)
3770 (/ (+ bottom-body char-height -1) char-height)))
3771 (list left top right bottom)))))
3773 (defun window-body-edges (&optional window)
3774 "Return a list of the edge coordinates of WINDOW's body.
3775 The return value is that of `window-edges' called with argument
3776 BODY non-nil."
3777 (window-edges window t))
3778 (defalias 'window-inside-edges 'window-body-edges)
3780 (defun window-pixel-edges (&optional window)
3781 "Return a list of the edge pixel coordinates of WINDOW.
3782 The return value is that of `window-edges' called with argument
3783 PIXELWISE non-nil."
3784 (window-edges window nil nil t))
3786 (defun window-body-pixel-edges (&optional window)
3787 "Return a list of the edge pixel coordinates of WINDOW's body.
3788 The return value is that of `window-edges' called with arguments
3789 BODY and PIXELWISE non-nil."
3790 (window-edges window t nil t))
3791 (defalias 'window-inside-pixel-edges 'window-body-pixel-edges)
3793 (defun window-absolute-pixel-edges (&optional window)
3794 "Return a list of the edge pixel coordinates of WINDOW.
3795 The return value is that of `window-edges' called with argument
3796 ABSOLUTE non-nil."
3797 (window-edges window nil t t))
3799 (defun window-absolute-body-pixel-edges (&optional window)
3800 "Return a list of the edge pixel coordinates of WINDOW's text area.
3801 The return value is that of `window-edges' called with arguments
3802 BODY and ABSOLUTE non-nil."
3803 (window-edges window t t t))
3804 (defalias 'window-inside-absolute-pixel-edges 'window-absolute-body-pixel-edges)
3806 (defun window-absolute-pixel-position (&optional position window)
3807 "Return display coordinates of POSITION in WINDOW.
3808 If the buffer position POSITION is visible in window WINDOW,
3809 return the display coordinates of the upper/left corner of the
3810 glyph at POSITION. The return value is a cons of the X- and
3811 Y-coordinates of that corner, relative to an origin at (0, 0) of
3812 WINDOW's display. Return nil if POSITION is not visible in
3813 WINDOW.
3815 WINDOW must be a live window and defaults to the selected window.
3816 POSITION defaults to the value of `window-point' of WINDOW."
3817 (let* ((window (window-normalize-window window t))
3818 (pos-in-window
3819 (pos-visible-in-window-p
3820 (or position (window-point window)) window t)))
3821 (when pos-in-window
3822 (let ((edges (window-absolute-body-pixel-edges window)))
3823 (cons (+ (nth 0 edges) (nth 0 pos-in-window))
3824 (+ (nth 1 edges) (nth 1 pos-in-window)))))))
3826 (defun frame-root-window-p (window)
3827 "Return non-nil if WINDOW is the root window of its frame."
3828 (eq window (frame-root-window window)))
3830 (defun window--subtree (window &optional next)
3831 "Return window subtree rooted at WINDOW.
3832 Optional argument NEXT non-nil means include WINDOW's right
3833 siblings in the return value.
3835 See the documentation of `window-tree' for a description of the
3836 return value."
3837 (let (list)
3838 (while window
3839 (setq list
3840 (cons
3841 (cond
3842 ((window-top-child window)
3843 (cons t (cons (window-edges window)
3844 (window--subtree (window-top-child window) t))))
3845 ((window-left-child window)
3846 (cons nil (cons (window-edges window)
3847 (window--subtree (window-left-child window) t))))
3848 (t window))
3849 list))
3850 (setq window (when next (window-next-sibling window))))
3851 (nreverse list)))
3853 (defun window-tree (&optional frame)
3854 "Return the window tree of frame FRAME.
3855 FRAME must be a live frame and defaults to the selected frame.
3856 The return value is a list of the form (ROOT MINI), where ROOT
3857 represents the window tree of the frame's root window, and MINI
3858 is the frame's minibuffer window.
3860 If the root window is not split, ROOT is the root window itself.
3861 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3862 for a horizontal split, and t for a vertical split. EDGES gives
3863 the combined size and position of the child windows in the split,
3864 and the rest of the elements are the child windows in the split.
3865 Each of the child windows may again be a window or a list
3866 representing a window split, and so on. EDGES is a list (LEFT
3867 TOP RIGHT BOTTOM) as returned by `window-edges'."
3868 (setq frame (window-normalize-frame frame))
3869 (window--subtree (frame-root-window frame) t))
3871 (defun other-window (count &optional all-frames)
3872 "Select another window in cyclic ordering of windows.
3873 COUNT specifies the number of windows to skip, starting with the
3874 selected window, before making the selection. If COUNT is
3875 positive, skip COUNT windows forwards. If COUNT is negative,
3876 skip -COUNT windows backwards. COUNT zero means do not skip any
3877 window, so select the selected window. In an interactive call,
3878 COUNT is the numeric prefix argument. Return nil.
3880 If the `other-window' parameter of the selected window is a
3881 function and `ignore-window-parameters' is nil, call that
3882 function with the arguments COUNT and ALL-FRAMES.
3884 This function does not select a window whose `no-other-window'
3885 window parameter is non-nil.
3887 This function uses `next-window' for finding the window to
3888 select. The argument ALL-FRAMES has the same meaning as in
3889 `next-window', but the MINIBUF argument of `next-window' is
3890 always effectively nil."
3891 (interactive "p")
3892 (let* ((window (selected-window))
3893 (function (and (not ignore-window-parameters)
3894 (window-parameter window 'other-window)))
3895 old-window old-count)
3896 (if (functionp function)
3897 (funcall function count all-frames)
3898 ;; `next-window' and `previous-window' may return a window we are
3899 ;; not allowed to select. Hence we need an exit strategy in case
3900 ;; all windows are non-selectable.
3901 (catch 'exit
3902 (while (> count 0)
3903 (setq window (next-window window nil all-frames))
3904 (cond
3905 ((eq window old-window)
3906 (when (= count old-count)
3907 ;; Keep out of infinite loops. When COUNT has not changed
3908 ;; since we last looked at `window' we're probably in one.
3909 (throw 'exit nil)))
3910 ((window-parameter window 'no-other-window)
3911 (unless old-window
3912 ;; The first non-selectable window `next-window' got us:
3913 ;; Remember it and the current value of COUNT.
3914 (setq old-window window)
3915 (setq old-count count)))
3917 (setq count (1- count)))))
3918 (while (< count 0)
3919 (setq window (previous-window window nil all-frames))
3920 (cond
3921 ((eq window old-window)
3922 (when (= count old-count)
3923 ;; Keep out of infinite loops. When COUNT has not changed
3924 ;; since we last looked at `window' we're probably in one.
3925 (throw 'exit nil)))
3926 ((window-parameter window 'no-other-window)
3927 (unless old-window
3928 ;; The first non-selectable window `previous-window' got
3929 ;; us: Remember it and the current value of COUNT.
3930 (setq old-window window)
3931 (setq old-count count)))
3933 (setq count (1+ count)))))
3935 (select-window window)
3936 ;; Always return nil.
3937 nil))))
3939 ;; This should probably return non-nil when the selected window is part
3940 ;; of an atomic window whose root is the frame's root window.
3941 (defun one-window-p (&optional nomini all-frames)
3942 "Return non-nil if the selected window is the only window.
3943 Optional arg NOMINI non-nil means don't count the minibuffer
3944 even if it is active. Otherwise, the minibuffer is counted
3945 when it is active.
3947 Optional argument ALL-FRAMES specifies the set of frames to
3948 consider, see also `next-window'. ALL-FRAMES nil or omitted
3949 means consider windows on the selected frame only, plus the
3950 minibuffer window if specified by the NOMINI argument. If the
3951 minibuffer counts, consider all windows on all frames that share
3952 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3953 with a special meaning are:
3955 - t means consider all windows on all existing frames.
3957 - `visible' means consider all windows on all visible frames on
3958 the current terminal.
3960 - 0 (the number zero) means consider all windows on all visible
3961 and iconified frames on the current terminal.
3963 - A frame means consider all windows on that frame only.
3965 Anything else means consider all windows on the selected frame
3966 and no others."
3967 (let ((base-window (selected-window)))
3968 (if (and nomini (eq base-window (minibuffer-window)))
3969 (setq base-window (next-window base-window)))
3970 (eq base-window
3971 (next-window base-window (if nomini 'arg) all-frames))))
3973 ;;; Deleting windows.
3974 (defun window-deletable-p (&optional window)
3975 "Return t if WINDOW can be safely deleted from its frame.
3976 WINDOW must be a valid window and defaults to the selected one.
3978 Return `frame' if WINDOW is the root window of its frame and that
3979 frame can be safely deleted."
3980 (setq window (window-normalize-window window))
3982 (unless (or ignore-window-parameters
3983 (eq (window-parameter window 'delete-window) t))
3984 ;; Handle atomicity.
3985 (when (window-parameter window 'window-atom)
3986 (setq window (window-atom-root window))))
3988 (let ((frame (window-frame window)))
3989 (cond
3990 ((frame-root-window-p window)
3991 ;; WINDOW's frame can be deleted only if there are other frames
3992 ;; on the same terminal, and it does not contain the active
3993 ;; minibuffer.
3994 (unless (or (eq frame (next-frame frame 0))
3995 ;; We can delete our frame only if no other frame
3996 ;; currently uses our minibuffer window.
3997 (catch 'other
3998 (dolist (other (frame-list))
3999 (when (and (not (eq other frame))
4000 (eq (window-frame (minibuffer-window other))
4001 frame))
4002 (throw 'other t))))
4003 (let ((minibuf (active-minibuffer-window)))
4004 (and minibuf (eq frame (window-frame minibuf)))))
4005 'frame))
4006 ((window-minibuffer-p window)
4007 ;; If WINDOW is the minibuffer window of a non-minibuffer-only
4008 ;; frame, it cannot be deleted separately.
4009 nil)
4010 ((or ignore-window-parameters
4011 (not (eq window (window-main-window frame))))
4012 ;; Otherwise, WINDOW can be deleted unless it is the main window
4013 ;; of its frame.
4014 t))))
4016 (defun window--in-subtree-p (window root)
4017 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
4018 (or (eq window root)
4019 (let ((parent (window-parent window)))
4020 (catch 'done
4021 (while parent
4022 (if (eq parent root)
4023 (throw 'done t)
4024 (setq parent (window-parent parent))))))))
4026 (defun delete-window (&optional window)
4027 "Delete WINDOW.
4028 WINDOW must be a valid window and defaults to the selected one.
4029 Return nil.
4031 If the variable `ignore-window-parameters' is non-nil or the
4032 `delete-window' parameter of WINDOW equals t, do not process any
4033 parameters of WINDOW. Otherwise, if the `delete-window'
4034 parameter of WINDOW specifies a function, call that function with
4035 WINDOW as its sole argument and return the value returned by that
4036 function.
4038 Otherwise, if WINDOW is part of an atomic window, call
4039 `delete-window' with the root of the atomic window as its
4040 argument. Signal an error if WINDOW is either the only window on
4041 its frame, the last non-side window, or part of an atomic window
4042 that is its frame's root window."
4043 (interactive)
4044 (setq window (window-normalize-window window))
4045 (let* ((frame (window-frame window))
4046 (function (window-parameter window 'delete-window))
4047 (parent (window-parent window))
4048 atom-root)
4049 (window--check frame)
4050 (catch 'done
4051 ;; Handle window parameters.
4052 (cond
4053 ;; Ignore window parameters if `ignore-window-parameters' tells
4054 ;; us so or `delete-window' equals t.
4055 ((or ignore-window-parameters (eq function t)))
4056 ((functionp function)
4057 ;; The `delete-window' parameter specifies the function to call.
4058 ;; If that function is `ignore' nothing is done. It's up to the
4059 ;; function called here to avoid infinite recursion.
4060 (throw 'done (funcall function window)))
4061 ((and (window-parameter window 'window-atom)
4062 (setq atom-root (window-atom-root window))
4063 (not (eq atom-root window)))
4064 (if (eq atom-root (frame-root-window frame))
4065 (error "Root of atomic window is root window of its frame")
4066 (throw 'done (delete-window atom-root))))
4067 ((not parent)
4068 (error "Attempt to delete minibuffer or sole ordinary window"))
4069 ((eq window (window-main-window frame))
4070 (error "Attempt to delete main window of frame %s" frame)))
4072 (let* ((horizontal (window-left-child parent))
4073 (size (window-size window horizontal t))
4074 (window-combination-resize
4075 (or window-combination-resize
4076 (window-parameter parent 'window-side)))
4077 (frame-selected
4078 (window--in-subtree-p (frame-selected-window frame) window))
4079 ;; Emacs 23 preferably gives WINDOW's space to its left
4080 ;; sibling.
4081 (sibling (or (window-left window) (window-right window))))
4082 (window--resize-reset frame horizontal)
4083 (cond
4084 ((and (not (eq window-combination-resize t))
4085 sibling (window-sizable-p sibling size horizontal nil t))
4086 ;; Resize WINDOW's sibling.
4087 (window--resize-this-window sibling size horizontal nil t)
4088 (set-window-new-normal
4089 sibling (+ (window-normal-size sibling horizontal)
4090 (window-normal-size window horizontal))))
4091 ((window--resizable-p window (- size) horizontal nil nil nil t t)
4092 ;; Can do without resizing fixed-size windows.
4093 (window--resize-siblings window (- size) horizontal))
4095 ;; Can't do without resizing fixed-size windows.
4096 (window--resize-siblings window (- size) horizontal t)))
4097 ;; Actually delete WINDOW.
4098 (delete-window-internal window)
4099 (window--pixel-to-total frame horizontal)
4100 (when (and frame-selected
4101 (window-parameter
4102 (frame-selected-window frame) 'no-other-window))
4103 ;; `delete-window-internal' has selected a window that should
4104 ;; not be selected, fix this here.
4105 (other-window -1 frame))
4106 (run-window-configuration-change-hook frame)
4107 (window--check frame)
4108 ;; Always return nil.
4109 nil))))
4111 (defun delete-other-windows (&optional window)
4112 "Make WINDOW fill its frame.
4113 WINDOW must be a valid window and defaults to the selected one.
4114 Return nil.
4116 If the variable `ignore-window-parameters' is non-nil or the
4117 `delete-other-windows' parameter of WINDOW equals t, do not pay
4118 attention to any other parameters of WINDOW. Otherwise, if the
4119 `delete-other-windows' parameter of WINDOW specifies a function,
4120 call that function with WINDOW as its sole argument and return
4121 the value returned by that function.
4123 Else, if WINDOW is part of an atomic window, call this function
4124 with the root of the atomic window as its argument. Signal an
4125 error if that root window is the root window of WINDOW's frame.
4126 Also signal an error if WINDOW is a side window. Do not delete
4127 any window whose `no-delete-other-windows' parameter is non-nil."
4128 (interactive)
4129 (setq window (window-normalize-window window))
4130 (let* ((frame (window-frame window))
4131 (function (window-parameter window 'delete-other-windows))
4132 atom-root main)
4133 (window--check frame)
4134 (catch 'done
4135 (cond
4136 ;; Ignore window parameters if `ignore-window-parameters' is t or
4137 ;; `delete-other-windows' is t.
4138 ((or ignore-window-parameters (eq function t)))
4139 ((functionp function)
4140 ;; The `delete-other-windows' parameter specifies the function
4141 ;; to call. If the function is `ignore' no windows are deleted.
4142 ;; It's up to the function called to avoid infinite recursion.
4143 (throw 'done (funcall function window)))
4144 ((and (window-parameter window 'window-atom)
4145 (setq atom-root (window-atom-root window))
4146 (not (eq atom-root window)))
4147 (if (eq atom-root (frame-root-window frame))
4148 (error "Root of atomic window is root window of its frame")
4149 (throw 'done (delete-other-windows atom-root))))
4150 ((window-parameter window 'window-side)
4151 (error "Cannot make side window the only window"))
4152 ((and (window-minibuffer-p window)
4153 (not (eq window (frame-root-window window))))
4154 (error "Can't expand minibuffer to full frame")))
4156 (cond
4157 ((or ignore-window-parameters
4158 (not (window-with-parameter 'no-delete-other-windows nil frame)))
4159 (setq main (frame-root-window frame)))
4160 ((catch 'tag
4161 (walk-window-tree
4162 (lambda (other)
4163 (when (or (and (window-parameter other 'window-side)
4164 (not (window-parameter
4165 other 'no-delete-other-windows)))
4166 (and (not (window-parameter other 'window-side))
4167 (window-parameter
4168 other 'no-delete-other-windows)))
4169 (throw 'tag nil))))
4171 (setq main (window-main-window frame)))
4173 ;; Delete windows via `delete-window' because we found either a
4174 ;; deletable side window or a non-deletable non-side-window.
4175 (dolist (other (window-list frame))
4176 (when (and (window-live-p other)
4177 (not (eq other window))
4178 (not (window-parameter
4179 other 'no-delete-other-windows))
4180 ;; When WINDOW and the other window are part of the
4181 ;; same atomic window, don't delete the other.
4182 (or (not atom-root)
4183 (not (eq (window-atom-root other) atom-root))))
4184 (condition-case nil
4185 (delete-window other)
4186 (error nil))))
4187 (throw 'done nil)))
4189 ;; If WINDOW is the main window of its frame do nothing.
4190 (unless (eq window main)
4191 (delete-other-windows-internal window main)
4192 (run-window-configuration-change-hook frame)
4193 (window--check frame))
4194 ;; Always return nil.
4195 nil)))
4197 (defun delete-other-windows-vertically (&optional window)
4198 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
4199 This may be a useful alternative binding for \\[delete-other-windows]
4200 if you often split windows horizontally."
4201 (interactive)
4202 (let* ((window (or window (selected-window)))
4203 (edges (window-edges window))
4204 (w window) delenda)
4205 (while (not (eq (setq w (next-window w 1)) window))
4206 (let ((e (window-edges w)))
4207 (when (and (= (car e) (car edges))
4208 (= (nth 2 e) (nth 2 edges)))
4209 (push w delenda))))
4210 (mapc 'delete-window delenda)))
4212 ;;; Windows and buffers.
4214 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
4215 ;; for (1) determining which buffer to show in the window when its
4216 ;; buffer shall be buried or killed and (2) which buffer to show for
4217 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
4219 ;; `prev-buffers' consists of <buffer, window-start, window-point>
4220 ;; triples. The entries on this list are ordered by the time their
4221 ;; buffer has been removed from the window, the most recently removed
4222 ;; buffer's entry being first. The window-start and window-point
4223 ;; components are `window-start' and `window-point' at the time the
4224 ;; buffer was removed from the window which implies that the entry must
4225 ;; be added when `set-window-buffer' removes the buffer from the window.
4227 ;; `next-buffers' is the list of buffers that have been replaced
4228 ;; recently by `switch-to-prev-buffer'. These buffers are the least
4229 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
4230 ;; candidates of `switch-to-next-buffer' to switch to. This list is
4231 ;; reset to nil by any action changing the window's buffer with the
4232 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
4233 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
4234 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
4236 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
4237 ;; if such a buffer was killed while the window was hidden within a
4238 ;; window configuration. Such killed buffers get removed whenever
4239 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
4241 ;; The following function is called by `set-window-buffer' _before_ it
4242 ;; replaces the buffer of the argument window with the new buffer.
4243 (defun record-window-buffer (&optional window)
4244 "Record WINDOW's buffer.
4245 WINDOW must be a live window and defaults to the selected one."
4246 (let* ((window (window-normalize-window window t))
4247 (buffer (window-buffer window))
4248 (entry (assq buffer (window-prev-buffers window))))
4249 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
4250 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
4251 (set-window-next-buffers window nil)
4253 (when entry
4254 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
4255 (set-window-prev-buffers
4256 window (assq-delete-all buffer (window-prev-buffers window))))
4258 ;; Don't record insignificant buffers.
4259 (unless (eq (aref (buffer-name buffer) 0) ?\s)
4260 ;; Add an entry for buffer to WINDOW's previous buffers.
4261 (with-current-buffer buffer
4262 (let ((start (window-start window))
4263 (point (window-point window)))
4264 (setq entry
4265 (cons buffer
4266 (if entry
4267 ;; We have an entry, update marker positions.
4268 (list (set-marker (nth 1 entry) start)
4269 (set-marker (nth 2 entry) point))
4270 ;; Make new markers.
4271 (list (copy-marker start)
4272 (copy-marker
4273 ;; Preserve window-point-insertion-type
4274 ;; (Bug#12588).
4275 point window-point-insertion-type)))))
4276 (set-window-prev-buffers
4277 window (cons entry (window-prev-buffers window)))))
4279 (run-hooks 'buffer-list-update-hook))))
4281 (defun unrecord-window-buffer (&optional window buffer)
4282 "Unrecord BUFFER in WINDOW.
4283 WINDOW must be a live window and defaults to the selected one.
4284 BUFFER must be a live buffer and defaults to the buffer of
4285 WINDOW."
4286 (let* ((window (window-normalize-window window t))
4287 (buffer (or buffer (window-buffer window))))
4288 (set-window-prev-buffers
4289 window (assq-delete-all buffer (window-prev-buffers window)))
4290 (set-window-next-buffers
4291 window (delq buffer (window-next-buffers window)))))
4293 (defun set-window-buffer-start-and-point (window buffer &optional start point)
4294 "Set WINDOW's buffer to BUFFER.
4295 WINDOW must be a live window and defaults to the selected one.
4296 Optional argument START non-nil means set WINDOW's start position
4297 to START. Optional argument POINT non-nil means set WINDOW's
4298 point to POINT. If WINDOW is selected this also sets BUFFER's
4299 `point' to POINT. If WINDOW is selected and the buffer it showed
4300 before was current this also makes BUFFER the current buffer."
4301 (setq window (window-normalize-window window t))
4302 (let ((selected (eq window (selected-window)))
4303 (current (eq (window-buffer window) (current-buffer))))
4304 (set-window-buffer window buffer)
4305 (when (and selected current)
4306 (set-buffer buffer))
4307 (when start
4308 ;; Don't force window-start here (even if POINT is nil).
4309 (set-window-start window start t))
4310 (when point
4311 (set-window-point window point))))
4313 (defcustom switch-to-visible-buffer t
4314 "If non-nil, allow switching to an already visible buffer.
4315 If this variable is non-nil, `switch-to-prev-buffer' and
4316 `switch-to-next-buffer' may switch to an already visible buffer.
4317 If this variable is nil, `switch-to-prev-buffer' and
4318 `switch-to-next-buffer' always try to avoid switching to a buffer
4319 that is already visible in another window on the same frame."
4320 :type 'boolean
4321 :version "24.1"
4322 :group 'windows)
4324 (defun switch-to-prev-buffer (&optional window bury-or-kill)
4325 "In WINDOW switch to previous buffer.
4326 WINDOW must be a live window and defaults to the selected one.
4327 Return the buffer switched to, nil if no suitable buffer could be
4328 found.
4330 Optional argument BURY-OR-KILL non-nil means the buffer currently
4331 shown in WINDOW is about to be buried or killed and consequently
4332 shall not be switched to in future invocations of this command.
4334 As a special case, if BURY-OR-KILL equals `append', this means to
4335 move the buffer to the end of WINDOW's previous buffers list so a
4336 future invocation of `switch-to-prev-buffer' less likely switches
4337 to it."
4338 (interactive)
4339 (let* ((window (window-normalize-window window t))
4340 (frame (window-frame window))
4341 (window-side (window-parameter window 'window-side))
4342 (old-buffer (window-buffer window))
4343 ;; Save this since it's destroyed by `set-window-buffer'.
4344 (next-buffers (window-next-buffers window))
4345 (pred (frame-parameter frame 'buffer-predicate))
4346 entry new-buffer killed-buffers visible)
4347 (when (window-minibuffer-p window)
4348 ;; Don't switch in minibuffer window.
4349 (unless (setq window (minibuffer-selected-window))
4350 (error "Window %s is a minibuffer window" window)))
4352 (unless (memq (window-dedicated-p window) '(nil side))
4353 ;; Don't switch in dedicated window.
4354 (error "Window %s is dedicated to buffer %s" window old-buffer))
4356 (catch 'found
4357 ;; Scan WINDOW's previous buffers first, skipping entries of next
4358 ;; buffers.
4359 (dolist (entry (window-prev-buffers window))
4360 (when (and (setq new-buffer (car entry))
4361 (or (buffer-live-p new-buffer)
4362 (not (setq killed-buffers
4363 (cons new-buffer killed-buffers))))
4364 (not (eq new-buffer old-buffer))
4365 (or (null pred) (funcall pred new-buffer))
4366 ;; When BURY-OR-KILL is nil, avoid switching to a
4367 ;; buffer in WINDOW's next buffers list.
4368 (or bury-or-kill (not (memq new-buffer next-buffers))))
4369 (if (and (not switch-to-visible-buffer)
4370 (get-buffer-window new-buffer frame))
4371 ;; Try to avoid showing a buffer visible in some other
4372 ;; window.
4373 (setq visible new-buffer)
4374 (set-window-buffer-start-and-point
4375 window new-buffer (nth 1 entry) (nth 2 entry))
4376 (throw 'found t))))
4377 ;; Scan reverted buffer list of WINDOW's frame next, skipping
4378 ;; entries of next buffers. Note that when we bury or kill a
4379 ;; buffer we don't reverse the global buffer list to avoid showing
4380 ;; a buried buffer instead. Otherwise, we must reverse the global
4381 ;; buffer list in order to make sure that switching to the
4382 ;; previous/next buffer traverse it in opposite directions. Skip
4383 ;; this step for side windows.
4384 (unless window-side
4385 (dolist (buffer (if bury-or-kill
4386 (buffer-list frame)
4387 (nreverse (buffer-list frame))))
4388 (when (and (buffer-live-p buffer)
4389 (not (eq buffer old-buffer))
4390 (or (null pred) (funcall pred buffer))
4391 (not (eq (aref (buffer-name buffer) 0) ?\s))
4392 ;; Don't show a buffer shown in a side window before.
4393 (not (buffer-local-value 'window--sides-shown buffer))
4394 (or bury-or-kill (not (memq buffer next-buffers))))
4395 (if (and (not switch-to-visible-buffer)
4396 (get-buffer-window buffer frame))
4397 ;; Try to avoid showing a buffer visible in some other window.
4398 (unless visible
4399 (setq visible buffer))
4400 (setq new-buffer buffer)
4401 (set-window-buffer-start-and-point window new-buffer)
4402 (throw 'found t)))))
4403 (unless bury-or-kill
4404 ;; Scan reverted next buffers last (must not use nreverse
4405 ;; here!).
4406 (dolist (buffer (reverse next-buffers))
4407 ;; Actually, buffer _must_ be live here since otherwise it
4408 ;; would have been caught in the scan of previous buffers.
4409 (when (and (or (buffer-live-p buffer)
4410 (not (setq killed-buffers
4411 (cons buffer killed-buffers))))
4412 (not (eq buffer old-buffer))
4413 (or (null pred) (funcall pred buffer))
4414 (setq entry (assq buffer (window-prev-buffers window))))
4415 (setq new-buffer buffer)
4416 (set-window-buffer-start-and-point
4417 window new-buffer (nth 1 entry) (nth 2 entry))
4418 (throw 'found t))))
4420 ;; Show a buffer visible in another window.
4421 (when visible
4422 (setq new-buffer visible)
4423 (set-window-buffer-start-and-point window new-buffer)))
4425 (if bury-or-kill
4426 (let ((entry (and (eq bury-or-kill 'append)
4427 (assq old-buffer (window-prev-buffers window)))))
4428 ;; Remove `old-buffer' from WINDOW's previous and (restored list
4429 ;; of) next buffers.
4430 (set-window-prev-buffers
4431 window (assq-delete-all old-buffer (window-prev-buffers window)))
4432 (set-window-next-buffers window (delq old-buffer next-buffers))
4433 (when entry
4434 ;; Append old-buffer's entry to list of WINDOW's previous
4435 ;; buffers so it's less likely to get switched to soon but
4436 ;; `display-buffer-in-previous-window' can nevertheless find
4437 ;; it.
4438 (set-window-prev-buffers
4439 window (append (window-prev-buffers window) (list entry)))))
4440 ;; Move `old-buffer' to head of WINDOW's restored list of next
4441 ;; buffers.
4442 (set-window-next-buffers
4443 window (cons old-buffer (delq old-buffer next-buffers))))
4445 ;; Remove killed buffers from WINDOW's previous and next buffers.
4446 (when killed-buffers
4447 (dolist (buffer killed-buffers)
4448 (set-window-prev-buffers
4449 window (assq-delete-all buffer (window-prev-buffers window)))
4450 (set-window-next-buffers
4451 window (delq buffer (window-next-buffers window)))))
4453 ;; Return new-buffer.
4454 new-buffer))
4456 (defun switch-to-next-buffer (&optional window)
4457 "In WINDOW switch to next buffer.
4458 WINDOW must be a live window and defaults to the selected one.
4459 Return the buffer switched to, nil if no suitable buffer could be
4460 found."
4461 (interactive)
4462 (let* ((window (window-normalize-window window t))
4463 (frame (window-frame window))
4464 (window-side (window-parameter window 'window-side))
4465 (old-buffer (window-buffer window))
4466 (next-buffers (window-next-buffers window))
4467 (pred (frame-parameter frame 'buffer-predicate))
4468 new-buffer entry killed-buffers visible)
4469 (when (window-minibuffer-p window)
4470 ;; Don't switch in minibuffer window.
4471 (unless (setq window (minibuffer-selected-window))
4472 (error "Window %s is a minibuffer window" window)))
4474 (unless (memq (window-dedicated-p window) '(nil side))
4475 ;; Don't switch in dedicated window.
4476 (error "Window %s is dedicated to buffer %s" window old-buffer))
4478 (catch 'found
4479 ;; Scan WINDOW's next buffers first.
4480 (dolist (buffer next-buffers)
4481 (when (and (or (buffer-live-p buffer)
4482 (not (setq killed-buffers
4483 (cons buffer killed-buffers))))
4484 (not (eq buffer old-buffer))
4485 (or (null pred) (funcall pred buffer))
4486 (setq entry (assq buffer (window-prev-buffers window))))
4487 (setq new-buffer buffer)
4488 (set-window-buffer-start-and-point
4489 window new-buffer (nth 1 entry) (nth 2 entry))
4490 (throw 'found t)))
4491 ;; Scan the buffer list of WINDOW's frame next, skipping previous
4492 ;; buffers entries. Skip this step for side windows.
4493 (unless window-side
4494 (dolist (buffer (buffer-list frame))
4495 (when (and (buffer-live-p buffer)
4496 (not (eq buffer old-buffer))
4497 (or (null pred) (funcall pred buffer))
4498 (not (eq (aref (buffer-name buffer) 0) ?\s))
4499 ;; Don't show a buffer shown in a side window before.
4500 (not (buffer-local-value 'window--sides-shown buffer))
4501 (not (assq buffer (window-prev-buffers window))))
4502 (if (and (not switch-to-visible-buffer)
4503 (get-buffer-window buffer frame))
4504 ;; Try to avoid showing a buffer visible in some other window.
4505 (setq visible buffer)
4506 (setq new-buffer buffer)
4507 (set-window-buffer-start-and-point window new-buffer)
4508 (throw 'found t)))))
4509 ;; Scan WINDOW's reverted previous buffers last (must not use
4510 ;; nreverse here!)
4511 (dolist (entry (reverse (window-prev-buffers window)))
4512 (when (and (setq new-buffer (car entry))
4513 (or (buffer-live-p new-buffer)
4514 (not (setq killed-buffers
4515 (cons new-buffer killed-buffers))))
4516 (not (eq new-buffer old-buffer))
4517 (or (null pred) (funcall pred new-buffer)))
4518 (if (and (not switch-to-visible-buffer)
4519 (get-buffer-window new-buffer frame))
4520 ;; Try to avoid showing a buffer visible in some other window.
4521 (unless visible
4522 (setq visible new-buffer))
4523 (set-window-buffer-start-and-point
4524 window new-buffer (nth 1 entry) (nth 2 entry))
4525 (throw 'found t))))
4527 ;; Show a buffer visible in another window.
4528 (when visible
4529 (setq new-buffer visible)
4530 (set-window-buffer-start-and-point window new-buffer)))
4532 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
4533 (set-window-next-buffers window (delq new-buffer next-buffers))
4535 ;; Remove killed buffers from WINDOW's previous and next buffers.
4536 (when killed-buffers
4537 (dolist (buffer killed-buffers)
4538 (set-window-prev-buffers
4539 window (assq-delete-all buffer (window-prev-buffers window)))
4540 (set-window-next-buffers
4541 window (delq buffer (window-next-buffers window)))))
4543 ;; Return new-buffer.
4544 new-buffer))
4546 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
4547 "Search LIST for a valid buffer to display in FRAME.
4548 Return nil when all buffers in LIST are undesirable for display,
4549 otherwise return the first suitable buffer in LIST.
4551 Buffers not visible in windows are preferred to visible buffers,
4552 unless VISIBLE-OK is non-nil.
4553 If the optional argument FRAME is nil, it defaults to the selected frame.
4554 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
4555 ;; This logic is more or less copied from other-buffer.
4556 (setq frame (or frame (selected-frame)))
4557 (let ((pred (frame-parameter frame 'buffer-predicate))
4558 found buf)
4559 (while (and (not found) list)
4560 (setq buf (car list))
4561 (if (and (not (eq buffer buf))
4562 (buffer-live-p buf)
4563 (or (null pred) (funcall pred buf))
4564 (not (eq (aref (buffer-name buf) 0) ?\s))
4565 (or visible-ok (null (get-buffer-window buf 'visible))))
4566 (setq found buf)
4567 (setq list (cdr list))))
4568 (car list)))
4570 (defun last-buffer (&optional buffer visible-ok frame)
4571 "Return the last buffer in FRAME's buffer list.
4572 If BUFFER is the last buffer, return the preceding buffer
4573 instead. Buffers not visible in windows are preferred to visible
4574 buffers, unless optional argument VISIBLE-OK is non-nil.
4575 Optional third argument FRAME nil or omitted means use the
4576 selected frame's buffer list. If no such buffer exists, return
4577 the buffer `*scratch*', creating it if necessary."
4578 (setq frame (or frame (selected-frame)))
4579 (or (get-next-valid-buffer (nreverse (buffer-list frame))
4580 buffer visible-ok frame)
4581 (get-buffer "*scratch*")
4582 (let ((scratch (get-buffer-create "*scratch*")))
4583 (set-buffer-major-mode scratch)
4584 scratch)))
4586 (defcustom frame-auto-hide-function #'iconify-frame
4587 "Function called to automatically hide frames.
4588 The function is called with one argument - a frame.
4590 Functions affected by this option are those that bury a buffer
4591 shown in a separate frame like `quit-window' and `bury-buffer'."
4592 :type '(choice (const :tag "Iconify" iconify-frame)
4593 (const :tag "Make invisible" make-frame-invisible)
4594 (const :tag "Delete" delete-frame)
4595 (const :tag "Do nothing" ignore)
4596 function)
4597 :group 'windows
4598 :group 'frames
4599 :version "26.1")
4601 (defun window--delete (&optional window dedicated-only kill)
4602 "Delete WINDOW if possible.
4603 WINDOW must be a live window and defaults to the selected one.
4604 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
4605 only if it's dedicated to its buffer. Optional argument KILL
4606 means the buffer shown in window will be killed. Return non-nil
4607 if WINDOW gets deleted or its frame is auto-hidden."
4608 (setq window (window-normalize-window window t))
4609 (unless (and dedicated-only (not (window-dedicated-p window)))
4610 (let ((deletable (window-deletable-p window)))
4611 (cond
4612 ((eq deletable 'frame)
4613 (let ((frame (window-frame window)))
4614 (cond
4615 (kill
4616 (delete-frame frame))
4617 ((functionp (frame-parameter frame 'auto-hide-function))
4618 (funcall (frame-parameter frame 'auto-hide-function)))
4619 ((functionp frame-auto-hide-function)
4620 (funcall frame-auto-hide-function frame))))
4621 'frame)
4622 (deletable
4623 (delete-window window)
4624 t)))))
4626 (defun bury-buffer (&optional buffer-or-name)
4627 "Put BUFFER-OR-NAME at the end of the list of all buffers.
4628 There it is the least likely candidate for `other-buffer' to
4629 return; thus, the least likely buffer for \\[switch-to-buffer] to
4630 select by default.
4632 You can specify a buffer name as BUFFER-OR-NAME, or an actual
4633 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
4634 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
4635 remove the current buffer from the selected window if it is
4636 displayed there."
4637 (interactive)
4638 (let* ((buffer (window-normalize-buffer buffer-or-name)))
4639 ;; If `buffer-or-name' is not on the selected frame we unrecord it
4640 ;; although it's not "here" (call it a feature).
4641 (bury-buffer-internal buffer)
4642 ;; Handle case where `buffer-or-name' is nil and the current buffer
4643 ;; is shown in the selected window.
4644 (cond
4645 ((or buffer-or-name (not (eq buffer (window-buffer)))))
4646 ((window--delete nil t))
4648 ;; Switch to another buffer in window.
4649 (set-window-dedicated-p nil nil)
4650 (switch-to-prev-buffer nil 'bury)))
4652 ;; Always return nil.
4653 nil))
4655 (defun unbury-buffer ()
4656 "Switch to the last buffer in the buffer list."
4657 (interactive)
4658 (switch-to-buffer (last-buffer)))
4660 (defun next-buffer ()
4661 "In selected window switch to next buffer."
4662 (interactive)
4663 (cond
4664 ((window-minibuffer-p)
4665 (error "Cannot switch buffers in minibuffer window"))
4666 ((eq (window-dedicated-p) t)
4667 (error "Window is strongly dedicated to its buffer"))
4669 (switch-to-next-buffer))))
4671 (defun previous-buffer ()
4672 "In selected window switch to previous buffer."
4673 (interactive)
4674 (cond
4675 ((window-minibuffer-p)
4676 (error "Cannot switch buffers in minibuffer window"))
4677 ((eq (window-dedicated-p) t)
4678 (error "Window is strongly dedicated to its buffer"))
4680 (switch-to-prev-buffer))))
4682 (defun delete-windows-on (&optional buffer-or-name frame)
4683 "Delete all windows showing BUFFER-OR-NAME.
4684 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4685 and defaults to the current buffer.
4687 The following non-nil values of the optional argument FRAME
4688 have special meanings:
4690 - t means consider all windows on the selected frame only.
4692 - `visible' means consider all windows on all visible frames on
4693 the current terminal.
4695 - 0 (the number zero) means consider all windows on all visible
4696 and iconified frames on the current terminal.
4698 - A frame means consider all windows on that frame only.
4700 Any other value of FRAME means consider all windows on all
4701 frames.
4703 When a window showing BUFFER-OR-NAME is dedicated and the only
4704 window of its frame, that frame is deleted when there are other
4705 frames left."
4706 (interactive "BDelete windows on (buffer):\nP")
4707 (let ((buffer (window-normalize-buffer buffer-or-name))
4708 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4709 ;; `window-list-1' based function.
4710 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4711 (dolist (window (window-list-1 nil nil all-frames))
4712 (if (eq (window-buffer window) buffer)
4713 (let ((deletable (window-deletable-p window)))
4714 (cond
4715 ((and (eq deletable 'frame) (window-dedicated-p window))
4716 ;; Delete frame if and only if window is dedicated.
4717 (delete-frame (window-frame window)))
4718 ((eq deletable t)
4719 ;; Delete window.
4720 (delete-window window))
4722 ;; In window switch to previous buffer.
4723 (set-window-dedicated-p window nil)
4724 (switch-to-prev-buffer window 'bury))))
4725 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4726 (unrecord-window-buffer window buffer)))))
4728 (defun replace-buffer-in-windows (&optional buffer-or-name)
4729 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
4730 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4731 and defaults to the current buffer.
4733 When a window showing BUFFER-OR-NAME is dedicated, that window is
4734 deleted. If that window is the only window on its frame, the
4735 frame is deleted too when there are other frames left. If there
4736 are no other frames left, some other buffer is displayed in that
4737 window.
4739 This function removes the buffer denoted by BUFFER-OR-NAME from
4740 all window-local buffer lists."
4741 (interactive "bBuffer to replace: ")
4742 (let ((buffer (window-normalize-buffer buffer-or-name)))
4743 (dolist (window (window-list-1 nil nil t))
4744 (if (eq (window-buffer window) buffer)
4745 (unless (window--delete window t t)
4746 ;; Switch to another buffer in window.
4747 (set-window-dedicated-p window nil)
4748 (switch-to-prev-buffer window 'kill))
4749 ;; Unrecord BUFFER in WINDOW.
4750 (unrecord-window-buffer window buffer)))))
4752 (defun quit-restore-window (&optional window bury-or-kill)
4753 "Quit WINDOW and deal with its buffer.
4754 WINDOW must be a live window and defaults to the selected one.
4756 According to information stored in WINDOW's `quit-restore' window
4757 parameter either (1) delete WINDOW and its frame, (2) delete
4758 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4759 or (4) make WINDOW display some other buffer than the present
4760 one. If non-nil, reset `quit-restore' parameter to nil.
4762 Optional second argument BURY-OR-KILL tells how to proceed with
4763 the buffer of WINDOW. The following values are handled:
4765 nil means to not handle the buffer in a particular way. This
4766 means that if WINDOW is not deleted by this function, invoking
4767 `switch-to-prev-buffer' will usually show the buffer again.
4769 `append' means that if WINDOW is not deleted, move its buffer to
4770 the end of WINDOW's previous buffers so it's less likely that a
4771 future invocation of `switch-to-prev-buffer' will switch to it.
4772 Also, move the buffer to the end of the frame's buffer list.
4774 `bury' means that if WINDOW is not deleted, remove its buffer
4775 from WINDOW'S list of previous buffers. Also, move the buffer
4776 to the end of the frame's buffer list. This value provides the
4777 most reliable remedy to not have `switch-to-prev-buffer' switch
4778 to this buffer again without killing the buffer.
4780 `kill' means to kill WINDOW's buffer."
4781 (setq window (window-normalize-window window t))
4782 (let* ((buffer (window-buffer window))
4783 (quit-restore (window-parameter window 'quit-restore))
4784 (prev-buffer
4785 (let* ((prev-buffers (window-prev-buffers window))
4786 (prev-buffer (caar prev-buffers)))
4787 (and (or (not (eq prev-buffer buffer))
4788 (and (cdr prev-buffers)
4789 (not (eq (setq prev-buffer (cadr prev-buffers))
4790 buffer))))
4791 prev-buffer)))
4792 quad entry)
4793 (cond
4794 ((and (not prev-buffer)
4795 (or (eq (nth 1 quit-restore) 'frame)
4796 (and (eq (nth 1 quit-restore) 'window)
4797 ;; If the window has been created on an existing
4798 ;; frame and ended up as the sole window on that
4799 ;; frame, do not delete it (Bug#12764).
4800 (not (eq window (frame-root-window window)))))
4801 (eq (nth 3 quit-restore) buffer)
4802 ;; Delete WINDOW if possible.
4803 (window--delete window nil (eq bury-or-kill 'kill)))
4804 ;; If the previously selected window is still alive, select it.
4805 (when (window-live-p (nth 2 quit-restore))
4806 (select-window (nth 2 quit-restore))))
4807 ((and (listp (setq quad (nth 1 quit-restore)))
4808 (buffer-live-p (car quad))
4809 (eq (nth 3 quit-restore) buffer))
4810 ;; Show another buffer stored in quit-restore parameter.
4811 (when (and (integerp (nth 3 quad))
4812 (if (window-combined-p window)
4813 (/= (nth 3 quad) (window-total-height window))
4814 (/= (nth 3 quad) (window-total-width window))))
4815 ;; Try to resize WINDOW to its old height but don't signal an
4816 ;; error.
4817 (condition-case nil
4818 (window-resize
4819 window
4820 (- (nth 3 quad) (if (window-combined-p window)
4821 (window-total-height window)
4822 (window-total-width window)))
4823 (window-combined-p window t))
4824 (error nil)))
4825 (set-window-dedicated-p window nil)
4826 ;; Restore WINDOW's previous buffer, start and point position.
4827 (set-window-buffer-start-and-point
4828 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
4829 ;; Deal with the buffer we just removed from WINDOW.
4830 (setq entry (and (eq bury-or-kill 'append)
4831 (assq buffer (window-prev-buffers window))))
4832 (when bury-or-kill
4833 ;; Remove buffer from WINDOW's previous and next buffers.
4834 (set-window-prev-buffers
4835 window (assq-delete-all buffer (window-prev-buffers window)))
4836 (set-window-next-buffers
4837 window (delq buffer (window-next-buffers window))))
4838 (when entry
4839 ;; Append old buffer's entry to list of WINDOW's previous
4840 ;; buffers so it's less likely to get switched to soon but
4841 ;; `display-buffer-in-previous-window' can nevertheless find it.
4842 (set-window-prev-buffers
4843 window (append (window-prev-buffers window) (list entry))))
4844 ;; Reset the quit-restore parameter.
4845 (set-window-parameter window 'quit-restore nil)
4846 ;; Select old window.
4847 (when (window-live-p (nth 2 quit-restore))
4848 (select-window (nth 2 quit-restore))))
4850 ;; Show some other buffer in WINDOW and reset the quit-restore
4851 ;; parameter.
4852 (set-window-parameter window 'quit-restore nil)
4853 ;; Make sure that WINDOW is no more dedicated.
4854 (set-window-dedicated-p window nil)
4855 (switch-to-prev-buffer window bury-or-kill)))
4857 ;; Deal with the buffer.
4858 (cond
4859 ((not (buffer-live-p buffer)))
4860 ((eq bury-or-kill 'kill)
4861 (kill-buffer buffer))
4862 (bury-or-kill
4863 (bury-buffer-internal buffer)))))
4865 (defun quit-window (&optional kill window)
4866 "Quit WINDOW and bury its buffer.
4867 WINDOW must be a live window and defaults to the selected one.
4868 With prefix argument KILL non-nil, kill the buffer instead of
4869 burying it.
4871 According to information stored in WINDOW's `quit-restore' window
4872 parameter either (1) delete WINDOW and its frame, (2) delete
4873 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4874 or (4) make WINDOW display some other buffer than the present
4875 one. If non-nil, reset `quit-restore' parameter to nil."
4876 (interactive "P")
4877 (quit-restore-window window (if kill 'kill 'bury)))
4879 (defun quit-windows-on (&optional buffer-or-name kill frame)
4880 "Quit all windows showing BUFFER-OR-NAME.
4881 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4882 and defaults to the current buffer. Optional argument KILL
4883 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4884 BUFFER-OR-NAME. Optional argument FRAME is handled as by
4885 `delete-windows-on'.
4887 This function calls `quit-window' on all candidate windows
4888 showing BUFFER-OR-NAME."
4889 (interactive "BQuit windows on (buffer):\nP")
4890 (let ((buffer (window-normalize-buffer buffer-or-name))
4891 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4892 ;; `window-list-1' based function.
4893 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4894 (dolist (window (window-list-1 nil nil all-frames))
4895 (if (eq (window-buffer window) buffer)
4896 (quit-window kill window)
4897 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4898 (unrecord-window-buffer window buffer)))))
4900 (defun split-window (&optional window size side pixelwise)
4901 "Make a new window adjacent to WINDOW.
4902 WINDOW must be a valid window and defaults to the selected one.
4903 Return the new window which is always a live window.
4905 Optional argument SIZE a positive number means make WINDOW SIZE
4906 lines or columns tall. If SIZE is negative, make the new window
4907 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
4908 absolute value can be less than `window-min-height' or
4909 `window-min-width'; so this command can make a new window as
4910 small as one line or two columns. SIZE defaults to half of
4911 WINDOW's size.
4913 Optional third argument SIDE nil (or `below') specifies that the
4914 new window shall be located below WINDOW. SIDE `above' means the
4915 new window shall be located above WINDOW. In both cases SIZE
4916 specifies the new number of lines for WINDOW (or the new window
4917 if SIZE is negative) including space reserved for the mode and/or
4918 header line.
4920 SIDE t (or `right') specifies that the new window shall be
4921 located on the right side of WINDOW. SIDE `left' means the new
4922 window shall be located on the left of WINDOW. In both cases
4923 SIZE specifies the new number of columns for WINDOW (or the new
4924 window provided SIZE is negative) including space reserved for
4925 fringes and the scrollbar or a divider column. Any other non-nil
4926 value for SIDE is currently handled like t (or `right').
4928 PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
4930 If the variable `ignore-window-parameters' is non-nil or the
4931 `split-window' parameter of WINDOW equals t, do not process any
4932 parameters of WINDOW. Otherwise, if the `split-window' parameter
4933 of WINDOW specifies a function, call that function with all three
4934 arguments and return the value returned by that function.
4936 Otherwise, if WINDOW is part of an atomic window, \"split\" the
4937 root of that atomic window. The new window does not become a
4938 member of that atomic window.
4940 If WINDOW is live, properties of the new window like margins and
4941 scrollbars are inherited from WINDOW. If WINDOW is an internal
4942 window, these properties as well as the buffer displayed in the
4943 new window are inherited from the window selected on WINDOW's
4944 frame. The selected window is not changed by this function."
4945 (setq window (window-normalize-window window))
4946 (let* ((side (cond
4947 ((not side) 'below)
4948 ((memq side '(below above right left)) side)
4949 (t 'right)))
4950 (horizontal (not (memq side '(below above))))
4951 (frame (window-frame window))
4952 (parent (window-parent window))
4953 (function (window-parameter window 'split-window))
4954 (window-side (window-parameter window 'window-side))
4955 ;; Rebind the following two variables since in some cases we
4956 ;; have to override their value.
4957 (window-combination-limit window-combination-limit)
4958 (window-combination-resize window-combination-resize)
4959 (char-size (frame-char-size window horizontal))
4960 (pixel-size
4961 (when (numberp size)
4962 (window--size-to-pixel window size horizontal pixelwise t)))
4963 (divider-width (if horizontal
4964 (frame-right-divider-width frame)
4965 (frame-bottom-divider-width frame)))
4966 atom-root ignore)
4967 (window--check frame)
4968 (catch 'done
4969 (cond
4970 ;; Ignore window parameters if either `ignore-window-parameters'
4971 ;; is t or the `split-window' parameter equals t.
4972 ((or ignore-window-parameters (eq function t)))
4973 ((functionp function)
4974 ;; The `split-window' parameter specifies the function to call.
4975 ;; If that function is `ignore', do nothing.
4976 (throw 'done (funcall function window size side)))
4977 ;; If WINDOW is part of an atomic window, split the root window
4978 ;; of that atomic window instead.
4979 ((and (window-parameter window 'window-atom)
4980 (setq atom-root (window-atom-root window))
4981 (not (eq atom-root window)))
4982 (throw 'done (split-window atom-root size side pixelwise)))
4983 ;; If WINDOW is a side window or its first or last child is a
4984 ;; side window, throw an error unless `window-combination-resize'
4985 ;; equals 'side.
4986 ((and (not (eq window-combination-resize 'side))
4987 (window-parameter window 'window-side))
4988 (error "Cannot split side window or parent of side window"))
4989 ;; If `window-combination-resize' is 'side and window has a side
4990 ;; window sibling, bind `window-combination-limit' to t.
4991 ((and (not (eq window-combination-resize 'side))
4992 (or (and (window-prev-sibling window)
4993 (window-parameter
4994 (window-prev-sibling window) 'window-side))
4995 (and (window-next-sibling window)
4996 (window-parameter
4997 (window-next-sibling window) 'window-side))))
4998 (setq window-combination-limit t)))
5000 ;; If `window-combination-resize' is t and SIZE is non-negative,
5001 ;; bind `window-combination-limit' to t.
5002 (when (and (eq window-combination-resize t)
5003 pixel-size (> pixel-size 0))
5004 (setq window-combination-limit t))
5006 (let* ((parent-pixel-size
5007 ;; `parent-pixel-size' is the pixel size of WINDOW's
5008 ;; parent, provided it has one.
5009 (when parent (window-size parent horizontal t)))
5010 ;; `resize' non-nil means we are supposed to resize other
5011 ;; windows in WINDOW's combination.
5012 (resize
5013 (and window-combination-resize
5014 (or (window-parameter window 'window-side)
5015 (not (eq window-combination-resize 'side)))
5016 (not (eq window-combination-limit t))
5017 ;; Resize makes sense in iso-combinations only.
5018 (window-combined-p window horizontal)))
5019 ;; `old-pixel-size' is the current pixel size of WINDOW.
5020 (old-pixel-size (window-size window horizontal t))
5021 ;; `new-size' is the specified or calculated size of the
5022 ;; new window.
5023 new-pixel-size new-parent new-normal)
5024 (cond
5025 ((not pixel-size)
5026 (setq new-pixel-size
5027 (if resize
5028 ;; When resizing try to give the new window the
5029 ;; average size of a window in its combination.
5030 (max (min (- parent-pixel-size
5031 (window-min-size parent horizontal nil t))
5032 (/ parent-pixel-size
5033 (1+ (window-combinations parent horizontal))))
5034 (window-min-pixel-size))
5035 ;; Else try to give the new window half the size
5036 ;; of WINDOW (plus an eventual odd pixel).
5037 (/ old-pixel-size 2)))
5038 (unless window-resize-pixelwise
5039 ;; Round to nearest char-size multiple.
5040 (setq new-pixel-size
5041 (* char-size (round new-pixel-size char-size)))))
5042 ((>= pixel-size 0)
5043 ;; SIZE non-negative specifies the new size of WINDOW.
5045 ;; Note: Specifying a non-negative SIZE is practically
5046 ;; always done as workaround for making the new window
5047 ;; appear above or on the left of the new window (the
5048 ;; ispell window is a typical example of that). In all
5049 ;; these cases the SIDE argument should be set to 'above
5050 ;; or 'left in order to support the 'resize option.
5051 ;; Here we have to nest the windows instead, see above.
5052 (setq new-pixel-size (- old-pixel-size pixel-size)))
5054 ;; SIZE negative specifies the size of the new window.
5055 (setq new-pixel-size (- pixel-size))))
5057 ;; Check SIZE.
5058 (cond
5059 ((not pixel-size)
5060 (cond
5061 (resize
5062 ;; SIZE unspecified, resizing.
5063 (unless (or (window-sizable-p
5064 parent (- (+ new-pixel-size divider-width)) horizontal
5065 nil t)
5066 (window-sizable-p
5067 parent (- (+ new-pixel-size divider-width)) horizontal
5068 (setq ignore 'preserved) t))
5069 (error "Window %s too small for splitting" parent)))
5070 ((and (> (+ new-pixel-size divider-width
5071 (window-min-size window horizontal nil t))
5072 old-pixel-size)
5073 (> (+ new-pixel-size divider-width
5074 (window-min-size
5075 window horizontal (setq ignore 'preserved) t))
5076 old-pixel-size))
5077 ;; SIZE unspecified, no resizing.
5078 (error "Window %s too small for splitting" window))))
5079 ((and (>= pixel-size 0)
5080 (or (>= pixel-size old-pixel-size)
5081 (< new-pixel-size
5082 (window-safe-min-pixel-size window horizontal))))
5083 ;; SIZE specified as new size of old window. If the new size
5084 ;; is larger than the old size or the size of the new window
5085 ;; would be less than the safe minimum, signal an error.
5086 (error "Window %s too small for splitting" window))
5087 (resize
5088 ;; SIZE specified, resizing.
5089 (unless (or (window-sizable-p
5090 parent (- (+ new-pixel-size divider-width)) horizontal
5091 nil t)
5092 (window-sizable-p
5093 parent (- (+ new-pixel-size divider-width)) horizontal
5094 (setq ignore 'preserved) t))
5095 ;; If we cannot resize the parent give up.
5096 (error "Window %s too small for splitting" parent)))
5097 ((or (< new-pixel-size
5098 (window-safe-min-pixel-size window horizontal))
5099 (< (- old-pixel-size new-pixel-size)
5100 (window-safe-min-pixel-size window horizontal)))
5101 ;; SIZE specification violates minimum size restrictions.
5102 (error "Window %s too small for splitting" window)))
5104 (window--resize-reset frame horizontal)
5106 (setq new-parent
5107 ;; Make new-parent non-nil if we need a new parent window;
5108 ;; either because we want to nest or because WINDOW is not
5109 ;; iso-combined.
5110 (or (eq window-combination-limit t)
5111 (not (window-combined-p window horizontal))))
5112 (setq new-normal
5113 ;; Make new-normal the normal size of the new window.
5114 (cond
5115 (pixel-size (/ (float new-pixel-size)
5116 (if new-parent old-pixel-size parent-pixel-size)))
5117 (new-parent 0.5)
5118 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
5119 (t (/ (window-normal-size window horizontal) 2.0))))
5121 (if resize
5122 ;; Try to get space from OLD's siblings. We could go "up" and
5123 ;; try getting additional space from surrounding windows but
5124 ;; we won't be able to return space to those windows when we
5125 ;; delete the one we create here. Hence we do not go up.
5126 (progn
5127 (window--resize-child-windows
5128 parent (- new-pixel-size) horizontal nil ignore)
5129 (let* ((normal (- 1.0 new-normal))
5130 (sub (window-child parent)))
5131 (while sub
5132 (set-window-new-normal
5133 sub (* (window-normal-size sub horizontal) normal))
5134 (setq sub (window-right sub)))))
5135 ;; Get entire space from WINDOW.
5136 (set-window-new-pixel
5137 window (- old-pixel-size new-pixel-size))
5138 (window--resize-this-window
5139 window (- new-pixel-size) horizontal ignore)
5140 (set-window-new-normal
5141 window (- (if new-parent 1.0 (window-normal-size window horizontal))
5142 new-normal)))
5144 (let* ((new (split-window-internal window new-pixel-size side new-normal)))
5145 (window--pixel-to-total frame horizontal)
5146 ;; Assign window-side parameters, if any.
5147 (cond
5148 ((eq window-combination-resize 'side)
5149 (let ((window-side
5150 (cond
5151 (window-side window-side)
5152 ((eq side 'above) 'top)
5153 ((eq side 'below) 'bottom)
5154 (t side))))
5155 ;; We made a new side window.
5156 (set-window-parameter new 'window-side window-side)
5157 (when (and new-parent (window-parameter window 'window-side))
5158 ;; We've been splitting a side root window. Give the
5159 ;; new parent the same window-side parameter.
5160 (set-window-parameter
5161 (window-parent new) 'window-side window-side))))
5162 ((eq window-combination-resize 'atom)
5163 ;; Make sure `window--check-frame' won't destroy an existing
5164 ;; atomic window in case the new window gets nested inside.
5165 (unless (window-parameter window 'window-atom)
5166 (set-window-parameter window 'window-atom t))
5167 (when new-parent
5168 (set-window-parameter (window-parent new) 'window-atom t))
5169 (set-window-parameter new 'window-atom t)))
5171 ;; Sanitize sizes unless SIZE was specified.
5172 (unless size
5173 (window--sanitize-window-sizes horizontal))
5175 (run-window-configuration-change-hook frame)
5176 (run-window-scroll-functions new)
5177 (window--check frame)
5178 ;; Always return the new window.
5179 new)))))
5181 (defun split-window-no-error (&optional window size side pixelwise)
5182 "Make a new window adjacent to WINDOW.
5183 This function is like `split-window' but does not signal an error
5184 when WINDOW cannot be split.
5186 For the meaning of all arguments see the documentation of
5187 `split-window'."
5188 (condition-case nil
5189 (split-window window size side pixelwise)
5190 (error nil)))
5192 ;; I think this should be the default; I think people will prefer it--rms.
5193 (defcustom split-window-keep-point t
5194 "If non-nil, \\[split-window-below] preserves point in the new window.
5195 If nil, adjust point in the two windows to minimize redisplay.
5196 This option applies only to `split-window-below' and functions
5197 that call it. The low-level `split-window' function always keeps
5198 the original point in both windows."
5199 :type 'boolean
5200 :group 'windows)
5202 (defun split-window-below (&optional size)
5203 "Split the selected window into two windows, one above the other.
5204 The selected window is above. The newly split-off window is
5205 below and displays the same buffer. Return the new window.
5207 If optional argument SIZE is omitted or nil, both windows get the
5208 same height, or close to it. If SIZE is positive, the upper
5209 \(selected) window gets SIZE lines. If SIZE is negative, the
5210 lower (new) window gets -SIZE lines.
5212 If the variable `split-window-keep-point' is non-nil, both
5213 windows get the same value of point as the selected window.
5214 Otherwise, the window starts are chosen so as to minimize the
5215 amount of redisplay; this is convenient on slow terminals."
5216 (interactive "P")
5217 (let ((old-window (selected-window))
5218 (old-point (window-point))
5219 (size (and size (prefix-numeric-value size)))
5220 moved-by-window-height moved new-window bottom)
5221 (when (and size (< size 0) (< (- size) window-min-height))
5222 ;; `split-window' would not signal an error here.
5223 (error "Size of new window too small"))
5224 (setq new-window (split-window nil size))
5225 (unless split-window-keep-point
5226 (with-current-buffer (window-buffer)
5227 ;; Use `save-excursion' around vertical movements below
5228 ;; (Bug#10971). Note: When the selected window's buffer has a
5229 ;; header line, up to two lines of the buffer may not show up
5230 ;; in the resulting configuration.
5231 (save-excursion
5232 (goto-char (window-start))
5233 (setq moved (vertical-motion (window-height)))
5234 (set-window-start new-window (point))
5235 (when (> (point) (window-point new-window))
5236 (set-window-point new-window (point)))
5237 (when (= moved (window-height))
5238 (setq moved-by-window-height t)
5239 (vertical-motion -1))
5240 (setq bottom (point)))
5241 (and moved-by-window-height
5242 (<= bottom (point))
5243 (set-window-point old-window (1- bottom)))
5244 (and moved-by-window-height
5245 (<= (window-start new-window) old-point)
5246 (set-window-point new-window old-point)
5247 (select-window new-window))))
5248 ;; Always copy quit-restore parameter in interactive use.
5249 (let ((quit-restore (window-parameter old-window 'quit-restore)))
5250 (when quit-restore
5251 (set-window-parameter new-window 'quit-restore quit-restore)))
5252 new-window))
5254 (defalias 'split-window-vertically 'split-window-below)
5256 (defun split-window-right (&optional size)
5257 "Split the selected window into two side-by-side windows.
5258 The selected window is on the left. The newly split-off window
5259 is on the right and displays the same buffer. Return the new
5260 window.
5262 If optional argument SIZE is omitted or nil, both windows get the
5263 same width, or close to it. If SIZE is positive, the left-hand
5264 \(selected) window gets SIZE columns. If SIZE is negative, the
5265 right-hand (new) window gets -SIZE columns. Here, SIZE includes
5266 the width of the window's scroll bar; if there are no scroll
5267 bars, it includes the width of the divider column to the window's
5268 right, if any."
5269 (interactive "P")
5270 (let ((old-window (selected-window))
5271 (size (and size (prefix-numeric-value size)))
5272 new-window)
5273 (when (and size (< size 0) (< (- size) window-min-width))
5274 ;; `split-window' would not signal an error here.
5275 (error "Size of new window too small"))
5276 (setq new-window (split-window nil size t))
5277 ;; Always copy quit-restore parameter in interactive use.
5278 (let ((quit-restore (window-parameter old-window 'quit-restore)))
5279 (when quit-restore
5280 (set-window-parameter new-window 'quit-restore quit-restore)))
5281 new-window))
5283 (defalias 'split-window-horizontally 'split-window-right)
5285 ;;; Balancing windows.
5287 ;; The following routine uses the recycled code from an old version of
5288 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
5289 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
5290 ;; more readable (FWIW we'd need three loops - one to calculate the
5291 ;; minimum sizes per window, one to enlarge or shrink windows until the
5292 ;; new parent-size matches, and one where we shrink the largest/enlarge
5293 ;; the smallest window).
5294 (defun balance-windows-2 (window horizontal)
5295 "Subroutine of `balance-windows-1'.
5296 WINDOW must be a vertical combination (horizontal if HORIZONTAL
5297 is non-nil)."
5298 (let* ((char-size (if window-resize-pixelwise
5300 (frame-char-size window horizontal)))
5301 (first (window-child window))
5302 (sub first)
5303 (number-of-children 0)
5304 (parent-size (window-new-pixel window))
5305 (total-sum parent-size)
5306 failed size sub-total sub-delta sub-amount rest)
5307 (while sub
5308 (setq number-of-children (1+ number-of-children))
5309 (when (window-size-fixed-p sub horizontal)
5310 (setq total-sum
5311 (- total-sum (window-size sub horizontal t)))
5312 (set-window-new-normal sub 'ignore))
5313 (setq sub (window-right sub)))
5315 (setq failed t)
5316 (while (and failed (> number-of-children 0))
5317 (setq size (/ total-sum number-of-children))
5318 (setq failed nil)
5319 (setq sub first)
5320 (while (and sub (not failed))
5321 ;; Ignore child windows that should be ignored or are stuck.
5322 (unless (window--resize-child-windows-skip-p sub)
5323 (setq sub-total (window-size sub horizontal t))
5324 (setq sub-delta (- size sub-total))
5325 (setq sub-amount
5326 (window-sizable sub sub-delta horizontal nil t))
5327 ;; Register the new total size for this child window.
5328 (set-window-new-pixel sub (+ sub-total sub-amount))
5329 (unless (= sub-amount sub-delta)
5330 (setq total-sum (- total-sum sub-total sub-amount))
5331 (setq number-of-children (1- number-of-children))
5332 ;; We failed and need a new round.
5333 (setq failed t)
5334 (set-window-new-normal sub 'skip)))
5335 (setq sub (window-right sub))))
5337 ;; How can we be sure that `number-of-children' is NOT zero here ?
5338 (setq rest (% total-sum number-of-children))
5339 ;; Fix rounding by trying to enlarge non-stuck windows by one line
5340 ;; (column) until `rest' is zero.
5341 (setq sub first)
5342 (while (and sub (> rest 0))
5343 (unless (window--resize-child-windows-skip-p window)
5344 (set-window-new-pixel sub (min rest char-size) t)
5345 (setq rest (- rest char-size)))
5346 (setq sub (window-right sub)))
5348 ;; Fix rounding by trying to enlarge stuck windows by one line
5349 ;; (column) until `rest' equals zero.
5350 (setq sub first)
5351 (while (and sub (> rest 0))
5352 (unless (eq (window-new-normal sub) 'ignore)
5353 (set-window-new-pixel sub (min rest char-size) t)
5354 (setq rest (- rest char-size)))
5355 (setq sub (window-right sub)))
5357 (setq sub first)
5358 (while sub
5359 ;; Record new normal sizes.
5360 (set-window-new-normal
5361 sub (/ (if (eq (window-new-normal sub) 'ignore)
5362 (window-size sub horizontal t)
5363 (window-new-pixel sub))
5364 (float parent-size)))
5365 ;; Recursively balance each window's child windows.
5366 (balance-windows-1 sub horizontal)
5367 (setq sub (window-right sub)))))
5369 (defun balance-windows-1 (window &optional horizontal)
5370 "Subroutine of `balance-windows'."
5371 (if (window-child window)
5372 (let ((sub (window-child window)))
5373 (if (window-combined-p sub horizontal)
5374 (balance-windows-2 window horizontal)
5375 (let ((size (window-new-pixel window)))
5376 (while sub
5377 (set-window-new-pixel sub size)
5378 (balance-windows-1 sub horizontal)
5379 (setq sub (window-right sub))))))))
5381 (defun balance-windows (&optional window-or-frame)
5382 "Balance the sizes of windows of WINDOW-OR-FRAME.
5383 WINDOW-OR-FRAME is optional and defaults to the selected frame.
5384 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
5385 windows of that frame. If WINDOW-OR-FRAME denotes a window,
5386 recursively balance the sizes of all child windows of that
5387 window."
5388 (interactive)
5389 (let* ((window
5390 (cond
5391 ((or (not window-or-frame)
5392 (frame-live-p window-or-frame))
5393 (frame-root-window window-or-frame))
5394 ((or (window-live-p window-or-frame)
5395 (window-child window-or-frame))
5396 window-or-frame)
5398 (error "Not a window or frame %s" window-or-frame))))
5399 (frame (window-frame window)))
5400 ;; Balance vertically.
5401 (window--resize-reset (window-frame window))
5402 (balance-windows-1 window)
5403 (when (window--resize-apply-p frame)
5404 (window-resize-apply frame)
5405 (window--pixel-to-total frame)
5406 (run-window-configuration-change-hook frame))
5407 ;; Balance horizontally.
5408 (window--resize-reset (window-frame window) t)
5409 (balance-windows-1 window t)
5410 (when (window--resize-apply-p frame t)
5411 (window-resize-apply frame t)
5412 (window--pixel-to-total frame t)
5413 (run-window-configuration-change-hook frame))))
5415 (defun window-fixed-size-p (&optional window direction)
5416 "Return t if WINDOW cannot be resized in DIRECTION.
5417 WINDOW defaults to the selected window. DIRECTION can be
5418 nil (i.e. any), `height' or `width'."
5419 (with-current-buffer (window-buffer window)
5420 (when (and (boundp 'window-size-fixed) window-size-fixed)
5421 (not (and direction
5422 (member (cons direction window-size-fixed)
5423 '((height . width) (width . height))))))))
5425 ;;; A different solution to balance-windows.
5426 (defvar window-area-factor 1
5427 "Factor by which the window area should be over-estimated.
5428 This is used by `balance-windows-area'.
5429 Changing this globally has no effect.")
5430 (make-variable-buffer-local 'window-area-factor)
5432 (defun balance-windows-area-adjust (window delta horizontal pixelwise)
5433 "Wrapper around `window-resize' with error checking.
5434 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
5435 ;; `window-resize' may fail if delta is too large.
5436 (while (>= (abs delta) 1)
5437 (condition-case nil
5438 (progn
5439 ;; It was wrong to use `window-resize' here. Somehow
5440 ;; `balance-windows-area' depends on resizing windows
5441 ;; asymmetrically.
5442 (adjust-window-trailing-edge window delta horizontal pixelwise)
5443 (setq delta 0))
5444 (error
5445 ;;(message "adjust: %s" (error-message-string err))
5446 (setq delta (/ delta 2))))))
5448 (defun balance-windows-area ()
5449 "Make all visible windows the same area (approximately).
5450 See also `window-area-factor' to change the relative size of
5451 specific buffers."
5452 (interactive)
5453 (let* ((unchanged 0) (carry 0) (round 0)
5454 ;; Remove fixed-size windows.
5455 (wins (delq nil (mapcar (lambda (win)
5456 (if (not (window-fixed-size-p win)) win))
5457 (window-list nil 'nomini))))
5458 (changelog nil)
5459 (pixelwise window-resize-pixelwise)
5460 next)
5461 ;; Resizing a window changes the size of surrounding windows in complex
5462 ;; ways, so it's difficult to balance them all. The introduction of
5463 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
5464 ;; very difficult to do. `balance-window' above takes an off-line
5465 ;; approach: get the whole window tree, then balance it, then try to
5466 ;; adjust the windows so they fit the result.
5467 ;; Here, instead, we take a "local optimization" approach, where we just
5468 ;; go through all the windows several times until nothing needs to be
5469 ;; changed. The main problem with this approach is that it's difficult
5470 ;; to make sure it terminates, so we use some heuristic to try and break
5471 ;; off infinite loops.
5472 ;; After a round without any change, we allow a second, to give a chance
5473 ;; to the carry to propagate a minor imbalance from the end back to
5474 ;; the beginning.
5475 (while (< unchanged 2)
5476 ;; (message "New round")
5477 (setq unchanged (1+ unchanged) round (1+ round))
5478 (dolist (win wins)
5479 (setq next win)
5480 (while (progn (setq next (next-window next))
5481 (window-fixed-size-p next)))
5482 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
5483 (let* ((horiz
5484 (< (car (window-pixel-edges win)) (car (window-pixel-edges next))))
5485 (areadiff (/ (- (* (window-size next nil pixelwise)
5486 (window-size next t pixelwise)
5487 (buffer-local-value 'window-area-factor
5488 (window-buffer next)))
5489 (* (window-size win nil pixelwise)
5490 (window-size win t pixelwise)
5491 (buffer-local-value 'window-area-factor
5492 (window-buffer win))))
5493 (max (buffer-local-value 'window-area-factor
5494 (window-buffer win))
5495 (buffer-local-value 'window-area-factor
5496 (window-buffer next)))))
5497 (edgesize (if horiz
5498 (+ (window-size win nil pixelwise)
5499 (window-size next nil pixelwise))
5500 (+ (window-size win t pixelwise)
5501 (window-size next t pixelwise))))
5502 (diff (/ areadiff edgesize)))
5503 (when (zerop diff)
5504 ;; Maybe diff is actually closer to 1 than to 0.
5505 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
5506 (when (and (zerop diff) (not (zerop areadiff)))
5507 (setq diff (/ (+ areadiff carry) edgesize))
5508 ;; Change things smoothly.
5509 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
5510 (if (zerop diff)
5511 ;; Make sure negligible differences don't accumulate to
5512 ;; become significant.
5513 (setq carry (+ carry areadiff))
5514 ;; This used `adjust-window-trailing-edge' before and uses
5515 ;; `window-resize' now. Error wrapping is still needed.
5516 (balance-windows-area-adjust win diff horiz pixelwise)
5517 ;; (sit-for 0.5)
5518 (let ((change (cons win (window-pixel-edges win))))
5519 ;; If the same change has been seen already for this window,
5520 ;; we're most likely in an endless loop, so don't count it as
5521 ;; a change.
5522 (unless (member change changelog)
5523 (push change changelog)
5524 (setq unchanged 0 carry 0)))))))
5525 ;; We've now basically balanced all the windows.
5526 ;; But there may be some minor off-by-one imbalance left over,
5527 ;; so let's do some fine tuning.
5528 ;; (bw-finetune wins)
5529 ;; (message "Done in %d rounds" round)
5532 ;;; Window states, how to get them and how to put them in a window.
5533 (defun window--state-get-1 (window &optional writable)
5534 "Helper function for `window-state-get'."
5535 (let* ((type
5536 (cond
5537 ((window-top-child window) 'vc)
5538 ((window-left-child window) 'hc)
5539 (t 'leaf)))
5540 (buffer (window-buffer window))
5541 (selected (eq window (selected-window)))
5542 (head
5543 `(,type
5544 ,@(unless (window-next-sibling window) `((last . t)))
5545 (pixel-width . ,(window-pixel-width window))
5546 (pixel-height . ,(window-pixel-height window))
5547 (total-width . ,(window-total-width window))
5548 (total-height . ,(window-total-height window))
5549 (normal-height . ,(window-normal-size window))
5550 (normal-width . ,(window-normal-size window t))
5551 ,@(unless (window-live-p window)
5552 `((combination-limit . ,(window-combination-limit window))))
5553 ,@(let ((parameters (window-parameters window))
5554 list)
5555 ;; Make copies of those window parameters whose
5556 ;; persistence property is `writable' if WRITABLE is
5557 ;; non-nil and non-nil if WRITABLE is nil.
5558 (dolist (par parameters)
5559 (let ((pers (cdr (assq (car par)
5560 window-persistent-parameters))))
5561 (when (and pers (or (not writable) (eq pers 'writable)))
5562 (setq list (cons (cons (car par) (cdr par)) list)))))
5563 ;; Add `clone-of' parameter if necessary.
5564 (let ((pers (cdr (assq 'clone-of
5565 window-persistent-parameters))))
5566 (when (and pers (or (not writable) (eq pers 'writable))
5567 (not (assq 'clone-of list)))
5568 (setq list (cons (cons 'clone-of window) list))))
5569 (when list
5570 `((parameters . ,list))))
5571 ,@(when buffer
5572 ;; All buffer related things go in here.
5573 (let ((point (window-point window))
5574 (start (window-start window)))
5575 `((buffer
5576 ,(buffer-name buffer)
5577 (selected . ,selected)
5578 (hscroll . ,(window-hscroll window))
5579 (fringes . ,(window-fringes window))
5580 (margins . ,(window-margins window))
5581 (scroll-bars . ,(window-scroll-bars window))
5582 (vscroll . ,(window-vscroll window))
5583 (dedicated . ,(window-dedicated-p window))
5584 (point . ,(if writable
5585 point
5586 (with-current-buffer buffer
5587 (copy-marker point
5588 (buffer-local-value
5589 'window-point-insertion-type
5590 buffer)))))
5591 (start . ,(if writable
5592 start
5593 (with-current-buffer buffer
5594 (copy-marker start))))))))))
5595 (tail
5596 (when (memq type '(vc hc))
5597 (let (list)
5598 (setq window (window-child window))
5599 (while window
5600 (setq list (cons (window--state-get-1 window writable) list))
5601 (setq window (window-right window)))
5602 (nreverse list)))))
5603 (append head tail)))
5605 (defun window-state-get (&optional window writable)
5606 "Return state of WINDOW as a Lisp object.
5607 WINDOW can be any window and defaults to the root window of the
5608 selected frame.
5610 Optional argument WRITABLE non-nil means do not use markers for
5611 sampling `window-point' and `window-start'. Together, WRITABLE
5612 and the variable `window-persistent-parameters' specify which
5613 window parameters are saved by this function. WRITABLE should be
5614 non-nil when the return value shall be written to a file and read
5615 back in another session. Otherwise, an application may run into
5616 an `invalid-read-syntax' error while attempting to read back the
5617 value from file.
5619 The return value can be used as argument for `window-state-put'
5620 to put the state recorded here into an arbitrary window. The
5621 value can be also stored on disk and read back in a new session."
5622 (setq window
5623 (if window
5624 (if (window-valid-p window)
5625 window
5626 (error "%s is not a live or internal window" window))
5627 (frame-root-window)))
5628 ;; The return value is a cons whose car specifies some constraints on
5629 ;; the size of WINDOW. The cdr lists the states of the child windows
5630 ;; of WINDOW.
5631 (cons
5632 ;; Frame related things would go into a function, say `frame-state',
5633 ;; calling `window-state-get' to insert the frame's root window.
5634 `((min-height . ,(window-min-size window))
5635 (min-width . ,(window-min-size window t))
5636 (min-height-ignore . ,(window-min-size window nil t))
5637 (min-width-ignore . ,(window-min-size window t t))
5638 (min-height-safe . ,(window-min-size window nil 'safe))
5639 (min-width-safe . ,(window-min-size window t 'safe))
5640 (min-pixel-height . ,(window-min-size window nil nil t))
5641 (min-pixel-width . ,(window-min-size window t nil t))
5642 (min-pixel-height-ignore . ,(window-min-size window nil t t))
5643 (min-pixel-width-ignore . ,(window-min-size window t t t))
5644 (min-pixel-height-safe . ,(window-min-size window nil 'safe t))
5645 (min-pixel-width-safe . ,(window-min-size window t 'safe t)))
5646 (window--state-get-1 window writable)))
5648 (defvar window-state-put-list nil
5649 "Helper variable for `window-state-put'.")
5651 (defvar window-state-put-stale-windows nil
5652 "Helper variable for `window-state-put'.")
5654 (defun window--state-put-1 (state &optional window ignore totals pixelwise)
5655 "Helper function for `window-state-put'."
5656 (let ((type (car state)))
5657 (setq state (cdr state))
5658 (cond
5659 ((eq type 'leaf)
5660 ;; For a leaf window just add unprocessed entries to
5661 ;; `window-state-put-list'.
5662 (push (cons window state) window-state-put-list))
5663 ((memq type '(vc hc))
5664 (let* ((horizontal (eq type 'hc))
5665 (total (window-size window horizontal pixelwise))
5666 (first t)
5667 (window-combination-limit (cdr (assq 'combination-limit state)))
5668 size new)
5669 (dolist (item state)
5670 ;; Find the next child window. WINDOW always points to the
5671 ;; real window that we want to fill with what we find here.
5672 (when (memq (car item) '(leaf vc hc))
5673 (if (assq 'last item)
5674 ;; The last child window. Below `window--state-put-1'
5675 ;; will put into it whatever ITEM has in store.
5676 (setq new nil)
5677 ;; Not the last child window, prepare for splitting
5678 ;; WINDOW. SIZE is the new (and final) size of the old
5679 ;; window.
5680 (setq size
5681 (if totals
5682 ;; Use total size.
5683 (if pixelwise
5684 (cdr (assq (if horizontal
5685 'pixel-width
5686 'pixel-height)
5687 item))
5688 (cdr (assq (if horizontal
5689 'total-width
5690 'total-height)
5691 item)))
5692 ;; Use normalized size and round.
5693 (round
5694 (* total
5695 (cdr (assq (if horizontal 'normal-width 'normal-height)
5696 item))))))
5698 ;; Use safe sizes, we try to resize later.
5699 (setq size (max size
5700 (if horizontal
5701 (* window-safe-min-width
5702 (if pixelwise
5703 (frame-char-width (window-frame window))
5705 (* window-safe-min-height
5706 (if pixelwise
5707 (frame-char-height (window-frame window))
5708 1)))))
5709 (if (window-sizable-p window (- size) horizontal 'safe pixelwise)
5710 (progn
5711 (setq new (split-window-no-error
5712 window size horizontal pixelwise))
5713 (setq window-combination-limit nil))
5714 ;; Give up if we can't resize window down to safe sizes.
5715 (error "Cannot resize window %s" window))
5717 (when first
5718 (setq first nil)
5719 ;; When creating the first child window add for parent
5720 ;; unprocessed entries to `window-state-put-list'.
5721 (setq window-state-put-list
5722 (cons (cons (window-parent window) state)
5723 window-state-put-list))))
5725 ;; Now process the current window (either the one we've just
5726 ;; split or the last child of its parent).
5727 (window--state-put-1 item window ignore totals)
5728 ;; Continue with the last window split off.
5729 (setq window new))))))))
5731 (defun window--state-put-2 (ignore pixelwise)
5732 "Helper function for `window-state-put'."
5733 (dolist (item window-state-put-list)
5734 (let ((window (car item))
5735 (combination-limit (cdr (assq 'combination-limit item)))
5736 (parameters (cdr (assq 'parameters item)))
5737 (state (cdr (assq 'buffer item))))
5738 (when combination-limit
5739 (set-window-combination-limit window combination-limit))
5740 ;; Reset window's parameters and assign saved ones (we might want
5741 ;; a `remove-window-parameters' function here).
5742 (dolist (parameter (window-parameters window))
5743 (set-window-parameter window (car parameter) nil))
5744 (when parameters
5745 (dolist (parameter parameters)
5746 (set-window-parameter window (car parameter) (cdr parameter))))
5747 ;; Process buffer related state.
5748 (when state
5749 (let ((buffer (get-buffer (car state))))
5750 (if buffer
5751 (with-current-buffer buffer
5752 (set-window-buffer window buffer)
5753 (set-window-hscroll window (cdr (assq 'hscroll state)))
5754 (apply 'set-window-fringes
5755 (cons window (cdr (assq 'fringes state))))
5756 (let ((margins (cdr (assq 'margins state))))
5757 (set-window-margins window (car margins) (cdr margins)))
5758 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
5759 (set-window-scroll-bars
5760 window (car scroll-bars) (nth 2 scroll-bars)
5761 (nth 3 scroll-bars) (nth 5 scroll-bars)))
5762 (set-window-vscroll window (cdr (assq 'vscroll state)))
5763 ;; Adjust vertically.
5764 (if (or (memq window-size-fixed '(t height))
5765 (window-preserved-size window))
5766 ;; A fixed height window, try to restore the
5767 ;; original size.
5768 (let ((delta
5769 (- (cdr (assq
5770 (if pixelwise 'pixel-height 'total-height)
5771 item))
5772 (window-size window nil pixelwise)))
5773 window-size-fixed)
5774 (when (window--resizable-p
5775 window delta nil nil nil nil nil pixelwise)
5776 (window-resize window delta nil nil pixelwise)))
5777 ;; Else check whether the window is not high enough.
5778 (let* ((min-size
5779 (window-min-size window nil ignore pixelwise))
5780 (delta
5781 (- min-size (window-size window nil pixelwise))))
5782 (when (and (> delta 0)
5783 (window--resizable-p
5784 window delta nil ignore nil nil nil pixelwise))
5785 (window-resize window delta nil ignore pixelwise))))
5786 ;; Adjust horizontally.
5787 (if (or (memq window-size-fixed '(t width))
5788 (window-preserved-size window t))
5789 ;; A fixed width window, try to restore the original
5790 ;; size.
5791 (let ((delta
5792 (- (cdr (assq
5793 (if pixelwise 'pixel-width 'total-width)
5794 item))
5795 (window-size window t pixelwise)))
5796 window-size-fixed)
5797 (when (window--resizable-p
5798 window delta t nil nil nil nil pixelwise)
5799 (window-resize window delta t nil pixelwise)))
5800 ;; Else check whether the window is not wide enough.
5801 (let* ((min-size (window-min-size window t ignore pixelwise))
5802 (delta (- min-size (window-size window t pixelwise))))
5803 (when (and (> delta 0)
5804 (window--resizable-p
5805 window delta t ignore nil nil nil pixelwise))
5806 (window-resize window delta t ignore pixelwise))))
5807 ;; Set dedicated status.
5808 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
5809 ;; Install positions (maybe we should do this after all
5810 ;; windows have been created and sized).
5811 (ignore-errors
5812 ;; Set 'noforce argument to avoid that window start
5813 ;; overrides window point set below (Bug#24240).
5814 (set-window-start window (cdr (assq 'start state)) 'noforce)
5815 (set-window-point window (cdr (assq 'point state))))
5816 ;; Select window if it's the selected one.
5817 (when (cdr (assq 'selected state))
5818 (select-window window)))
5819 ;; We don't want to raise an error in case the buffer does
5820 ;; not exist anymore, so we switch to a previous one and
5821 ;; save the window with the intention of deleting it later
5822 ;; if possible.
5823 (switch-to-prev-buffer window)
5824 (push window window-state-put-stale-windows)))))))
5826 (defun window-state-put (state &optional window ignore)
5827 "Put window state STATE into WINDOW.
5828 STATE should be the state of a window returned by an earlier
5829 invocation of `window-state-get'. Optional argument WINDOW must
5830 specify a valid window and defaults to the selected one. If
5831 WINDOW is not live, replace WINDOW by a live one before putting
5832 STATE into it.
5834 Optional argument IGNORE non-nil means ignore minimum window
5835 sizes and fixed size restrictions. IGNORE equal `safe' means
5836 windows can get as small as `window-safe-min-height' and
5837 `window-safe-min-width'."
5838 (setq window-state-put-stale-windows nil)
5839 (setq window (window-normalize-window window))
5841 ;; When WINDOW is internal, reduce it to a live one to put STATE into,
5842 ;; see Bug#16793.
5843 (unless (window-live-p window)
5844 (let ((root window))
5845 (setq window (catch 'live
5846 (walk-window-subtree
5847 (lambda (window)
5848 (when (and (window-live-p window)
5849 (not (window-parameter window 'window-side)))
5850 (throw 'live window)))
5851 root)))
5852 (delete-other-windows-internal window root)))
5854 (set-window-dedicated-p window nil)
5856 (let* ((frame (window-frame window))
5857 (head (car state))
5858 ;; We check here (1) whether the total sizes of root window of
5859 ;; STATE and that of WINDOW are equal so we can avoid
5860 ;; calculating new sizes, and (2) if we do have to resize
5861 ;; whether we can do so without violating size restrictions.
5862 (pixelwise (and (cdr (assq 'pixel-width state))
5863 (cdr (assq 'pixel-height state))))
5864 (totals (or (and pixelwise
5865 (= (window-pixel-width window)
5866 (cdr (assq 'pixel-width state)))
5867 (= (window-pixel-height window)
5868 (cdr (assq 'pixel-height state))))
5869 (and (= (window-total-width window)
5870 (cdr (assq 'total-width state)))
5871 (= (window-total-height window)
5872 (cdr (assq 'total-height state))))))
5873 (min-height (cdr (assq
5874 (if pixelwise 'min-pixel-height 'min-height)
5875 head)))
5876 (min-width (cdr (assq
5877 (if pixelwise 'min-pixel-width 'min-weight)
5878 head))))
5879 (if (and (not totals)
5880 (or (> min-height (window-size window nil pixelwise))
5881 (> min-width (window-size window t pixelwise)))
5882 (or (not ignore)
5883 (and (setq min-height
5884 (cdr (assq
5885 (if pixelwise
5886 'min-pixel-height-ignore
5887 'min-height-ignore)
5888 head)))
5889 (setq min-width
5890 (cdr (assq
5891 (if pixelwise
5892 'min-pixel-width-ignore
5893 'min-width-ignore)
5894 head)))
5895 (or (> min-height
5896 (window-size window nil pixelwise))
5897 (> min-width
5898 (window-size window t pixelwise)))
5899 (or (not (eq ignore 'safe))
5900 (and (setq min-height
5901 (cdr (assq
5902 (if pixelwise
5903 'min-pixel-height-safe
5904 'min-height-safe)
5905 head)))
5906 (setq min-width
5907 (cdr (assq
5908 (if pixelwise
5909 'min-pixel-width-safe
5910 'min-width-safe)
5911 head)))
5912 (or (> min-height
5913 (window-size window nil pixelwise))
5914 (> min-width
5915 (window-size window t pixelwise))))))))
5916 ;; The check above might not catch all errors due to rounding
5917 ;; issues - so IGNORE equal 'safe might not always produce the
5918 ;; minimum possible state. But such configurations hardly make
5919 ;; sense anyway.
5920 (error "Window %s too small to accommodate state" window)
5921 (setq state (cdr state))
5922 (setq window-state-put-list nil)
5923 ;; Work on the windows of a temporary buffer to make sure that
5924 ;; splitting proceeds regardless of any buffer local values of
5925 ;; `window-size-fixed'. Release that buffer after the buffers of
5926 ;; all live windows have been set by `window--state-put-2'.
5927 (with-temp-buffer
5928 (set-window-buffer window (current-buffer))
5929 (window--state-put-1 state window nil totals pixelwise)
5930 (window--state-put-2 ignore pixelwise))
5931 (while window-state-put-stale-windows
5932 (let ((window (pop window-state-put-stale-windows)))
5933 (when (eq (window-deletable-p window) t)
5934 (delete-window window))))
5935 (window--check frame))))
5937 (defun window-swap-states (&optional window-1 window-2 size)
5938 "Swap the states of live windows WINDOW-1 and WINDOW-2.
5939 WINDOW-1 must specify a live window and defaults to the selected
5940 one. WINDOW-2 must specify a live window and defaults to the
5941 window following WINDOW-1 in the cyclic ordering of windows,
5942 excluding minibuffer windows and including live windows on all
5943 visible frames.
5945 Optional argument SIZE non-nil means to try swapping the sizes of
5946 WINDOW-1 and WINDOW-2 as well. A value of `height' means to swap
5947 heights only, a value of `width' means to swap widths only, while
5948 t means to swap both widths and heights, if possible. Frames are
5949 not resized by this function."
5950 (interactive)
5951 (setq window-1 (window-normalize-window window-1 t))
5952 (if window-2
5953 (unless (window-live-p window-2)
5954 (error "%s is not a live window" window-2))
5955 (setq window-2 (next-window window-1 'nomini 'visible)))
5956 (unless (eq window-1 window-2)
5957 (let* ((height (memq size '(t height)))
5958 (width (memq size '(t width)))
5959 (state-1 (window-state-get window-1))
5960 (width-1 (and width (window-text-width window-1 t)))
5961 (height-1 (and height (window-text-height window-1 t)))
5962 (state-2 (window-state-get window-2))
5963 (width-2 (and width (window-text-width window-2 t)))
5964 (height-2 (and height (window-text-height window-2 t)))
5965 old preserved)
5966 ;; Swap basic states.
5967 (window-state-put state-1 window-2 t)
5968 (window-state-put state-2 window-1 t)
5969 ;; Swap overlays with `window' property.
5970 (with-current-buffer (window-buffer window-1)
5971 (dolist (overlay (overlays-in (point-min) (point-max)))
5972 (let ((window (overlay-get overlay 'window)))
5973 (cond
5974 ((not window))
5975 ((eq window window-1)
5976 (overlay-put overlay 'window window-2))
5977 ((eq window window-2)
5978 (overlay-put overlay 'window window-1))))))
5979 (unless (eq (window-buffer window-1) (window-buffer window-2))
5980 (with-current-buffer (window-buffer window-2)
5981 (dolist (overlay (overlays-in (point-min) (point-max)))
5982 (let ((window (overlay-get overlay 'window)))
5983 (cond
5984 ((not window))
5985 ((eq window window-1)
5986 (overlay-put overlay 'window window-2))
5987 ((eq window window-2)
5988 (overlay-put overlay 'window window-1)))))))
5989 ;; Try to swap window sizes.
5990 (when size
5991 (unless (= (setq old (window-text-width window-1 t)) width-2)
5992 (window-resize-no-error window-1 (- width-2 old) t t t))
5993 (unless (= (setq old (window-text-width window-2 t)) width-1)
5994 (setq preserved (window-preserved-size window-1 t))
5995 (window-preserve-size window-1 t t)
5996 (window-resize-no-error window-2 (- width-1 old) t t t)
5997 (window-preserve-size window-1 t preserved))
5998 (unless (= (setq old (window-text-height window-1 t)) height-2)
5999 (window-resize-no-error window-1 (- height-2 old) nil t t))
6000 (unless (= (setq old (window-text-height window-2 t)) height-1)
6001 (setq preserved (window-preserved-size window-1))
6002 (window-preserve-size window-1 nil t)
6003 (window-resize-no-error window-2 (- height-1 old) nil t t)
6004 (window-preserve-size window-1 nil preserved))))))
6006 (defun display-buffer-record-window (type window buffer)
6007 "Record information for window used by `display-buffer'.
6008 TYPE specifies the type of the calling operation and must be one
6009 of the symbols `reuse' (when WINDOW existed already and was
6010 reused for displaying BUFFER), `window' (when WINDOW was created
6011 on an already existing frame), or `frame' (when WINDOW was
6012 created on a new frame). WINDOW is the window used for or created
6013 by the `display-buffer' routines. BUFFER is the buffer that
6014 shall be displayed.
6016 This function installs or updates the quit-restore parameter of
6017 WINDOW. The quit-restore parameter is a list of four elements:
6018 The first element is one of the symbols `window', `frame', `same' or
6019 `other'. The second element is either one of the symbols `window'
6020 or `frame' or a list whose elements are the buffer previously
6021 shown in the window, that buffer's window start and window point,
6022 and the window's height. The third element is the window
6023 selected at the time the parameter was created. The fourth
6024 element is BUFFER."
6025 (cond
6026 ((eq type 'reuse)
6027 (if (eq (window-buffer window) buffer)
6028 ;; WINDOW shows BUFFER already. Update WINDOW's quit-restore
6029 ;; parameter, if any.
6030 (let ((quit-restore (window-parameter window 'quit-restore)))
6031 (when (consp quit-restore)
6032 (setcar quit-restore 'same)
6033 ;; The selected-window might have changed in
6034 ;; between (Bug#20353).
6035 (unless (or (eq window (selected-window))
6036 (eq window (nth 2 quit-restore)))
6037 (setcar (cddr quit-restore) (selected-window)))))
6038 ;; WINDOW shows another buffer.
6039 (with-current-buffer (window-buffer window)
6040 (set-window-parameter
6041 window 'quit-restore
6042 (list 'other
6043 ;; A quadruple of WINDOW's buffer, start, point and height.
6044 (list (current-buffer) (window-start window)
6045 ;; Preserve window-point-insertion-type (Bug#12588).
6046 (copy-marker
6047 (window-point window) window-point-insertion-type)
6048 (if (window-combined-p window)
6049 (window-total-height window)
6050 (window-total-width window)))
6051 (selected-window) buffer)))))
6052 ((eq type 'window)
6053 ;; WINDOW has been created on an existing frame.
6054 (set-window-parameter
6055 window 'quit-restore
6056 (list 'window 'window (selected-window) buffer)))
6057 ((eq type 'frame)
6058 ;; WINDOW has been created on a new frame.
6059 (set-window-parameter
6060 window 'quit-restore
6061 (list 'frame 'frame (selected-window) buffer)))))
6063 (defcustom display-buffer-function nil
6064 "If non-nil, function to call to handle `display-buffer'.
6065 It will receive two args, the buffer and a flag which if non-nil
6066 means that the currently selected window is not acceptable. It
6067 should choose or create a window, display the specified buffer in
6068 it, and return the window.
6070 The specified function should call `display-buffer-record-window'
6071 with corresponding arguments to set up the quit-restore parameter
6072 of the window used."
6073 :type '(choice
6074 (const nil)
6075 (function :tag "function"))
6076 :group 'windows)
6078 (make-obsolete-variable 'display-buffer-function
6079 'display-buffer-alist "24.3")
6081 ;; Eventually, we want to turn this into a defvar; instead of
6082 ;; customizing this, the user should use a `pop-up-frame-parameters'
6083 ;; alist entry in `display-buffer-base-action'.
6084 (defcustom pop-up-frame-alist nil
6085 "Alist of parameters for automatically generated new frames.
6086 If non-nil, the value you specify here is used by the default
6087 `pop-up-frame-function' for the creation of new frames.
6089 Since `pop-up-frame-function' is used by `display-buffer' for
6090 making new frames, any value specified here by default affects
6091 the automatic generation of new frames via `display-buffer' and
6092 all functions based on it. The behavior of `make-frame' is not
6093 affected by this variable."
6094 :type '(repeat (cons :format "%v"
6095 (symbol :tag "Parameter")
6096 (sexp :tag "Value")))
6097 :group 'frames)
6099 (defcustom pop-up-frame-function
6100 (lambda () (make-frame pop-up-frame-alist))
6101 "Function used by `display-buffer' for creating a new frame.
6102 This function is called with no arguments and should return a new
6103 frame. The default value calls `make-frame' with the argument
6104 `pop-up-frame-alist'."
6105 :type 'function
6106 :group 'frames)
6108 (defcustom special-display-buffer-names nil
6109 "List of names of buffers that should be displayed specially.
6110 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
6111 its name is in this list, displays the buffer in a way specified
6112 by `special-display-function'. `special-display-popup-frame'
6113 \(the default for `special-display-function') usually displays
6114 the buffer in a separate frame made with the parameters specified
6115 by `special-display-frame-alist'. If `special-display-function'
6116 has been set to some other function, that function is called with
6117 the buffer as first, and nil as second argument.
6119 Alternatively, an element of this list can be specified as
6120 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
6121 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
6122 `special-display-popup-frame' will interpret such pairs as frame
6123 parameters when it creates a special frame, overriding the
6124 corresponding values from `special-display-frame-alist'.
6126 As a special case, if FRAME-PARAMETERS contains (same-window . t)
6127 `special-display-popup-frame' displays that buffer in the
6128 selected window. If FRAME-PARAMETERS contains (same-frame . t),
6129 it displays that buffer in a window on the selected frame.
6131 If `special-display-function' specifies some other function than
6132 `special-display-popup-frame', that function is called with the
6133 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
6134 argument.
6136 Finally, an element of this list can be also specified as
6137 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
6138 `special-display-popup-frame' will call FUNCTION with the buffer
6139 named BUFFER-NAME as first argument, and OTHER-ARGS as the
6140 second.
6142 Any alternative function specified here is responsible for
6143 setting up the quit-restore parameter of the window used.
6145 If this variable appears \"not to work\", because you added a
6146 name to it but the corresponding buffer is displayed in the
6147 selected window, look at the values of `same-window-buffer-names'
6148 and `same-window-regexps'. Those variables take precedence over
6149 this one.
6151 See also `special-display-regexps'."
6152 :type '(repeat
6153 (choice :tag "Buffer"
6154 :value ""
6155 (string :format "%v")
6156 (cons :tag "With parameters"
6157 :format "%v"
6158 :value ("" . nil)
6159 (string :format "%v")
6160 (repeat :tag "Parameters"
6161 (cons :format "%v"
6162 (symbol :tag "Parameter")
6163 (sexp :tag "Value"))))
6164 (list :tag "With function"
6165 :format "%v"
6166 :value ("" . nil)
6167 (string :format "%v")
6168 (function :tag "Function")
6169 (repeat :tag "Arguments" (sexp)))))
6170 :group 'windows
6171 :group 'frames)
6172 (make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3")
6173 (put 'special-display-buffer-names 'risky-local-variable t)
6175 (defcustom special-display-regexps nil
6176 "List of regexps saying which buffers should be displayed specially.
6177 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
6178 any regexp in this list matches its name, displays it specially
6179 using `special-display-function'. `special-display-popup-frame'
6180 \(the default for `special-display-function') usually displays
6181 the buffer in a separate frame made with the parameters specified
6182 by `special-display-frame-alist'. If `special-display-function'
6183 has been set to some other function, that function is called with
6184 the buffer as first, and nil as second argument.
6186 Alternatively, an element of this list can be specified as
6187 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
6188 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
6189 `special-display-popup-frame' will then interpret these pairs as
6190 frame parameters when creating a special frame for a buffer whose
6191 name matches REGEXP, overriding the corresponding values from
6192 `special-display-frame-alist'.
6194 As a special case, if FRAME-PARAMETERS contains (same-window . t)
6195 `special-display-popup-frame' displays buffers matching REGEXP in
6196 the selected window. (same-frame . t) in FRAME-PARAMETERS means
6197 to display such buffers in a window on the selected frame.
6199 If `special-display-function' specifies some other function than
6200 `special-display-popup-frame', that function is called with the
6201 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
6202 as second argument.
6204 Finally, an element of this list can be also specified as
6205 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
6206 will then call FUNCTION with the buffer whose name matched
6207 REGEXP as first, and OTHER-ARGS as second argument.
6209 Any alternative function specified here is responsible for
6210 setting up the quit-restore parameter of the window used.
6212 If this variable appears \"not to work\", because you added a
6213 name to it but the corresponding buffer is displayed in the
6214 selected window, look at the values of `same-window-buffer-names'
6215 and `same-window-regexps'. Those variables take precedence over
6216 this one.
6218 See also `special-display-buffer-names'."
6219 :type '(repeat
6220 (choice :tag "Buffer"
6221 :value ""
6222 (regexp :format "%v")
6223 (cons :tag "With parameters"
6224 :format "%v"
6225 :value ("" . nil)
6226 (regexp :format "%v")
6227 (repeat :tag "Parameters"
6228 (cons :format "%v"
6229 (symbol :tag "Parameter")
6230 (sexp :tag "Value"))))
6231 (list :tag "With function"
6232 :format "%v"
6233 :value ("" . nil)
6234 (regexp :format "%v")
6235 (function :tag "Function")
6236 (repeat :tag "Arguments" (sexp)))))
6237 :group 'windows
6238 :group 'frames)
6239 (make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3")
6240 (put 'special-display-regexps 'risky-local-variable t)
6242 (defun special-display-p (buffer-name)
6243 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
6244 More precisely, return t if `special-display-buffer-names' or
6245 `special-display-regexps' contain a string entry equaling or
6246 matching BUFFER-NAME. If `special-display-buffer-names' or
6247 `special-display-regexps' contain a list entry whose car equals
6248 or matches BUFFER-NAME, the return value is the cdr of that
6249 entry."
6250 (let (tmp)
6251 (cond
6252 ((member buffer-name special-display-buffer-names)
6254 ((setq tmp (assoc buffer-name special-display-buffer-names))
6255 (cdr tmp))
6256 ((catch 'found
6257 (dolist (regexp special-display-regexps)
6258 (cond
6259 ((stringp regexp)
6260 (when (string-match-p regexp buffer-name)
6261 (throw 'found t)))
6262 ((and (consp regexp) (stringp (car regexp))
6263 (string-match-p (car regexp) buffer-name))
6264 (throw 'found (cdr regexp))))))))))
6266 (defcustom special-display-frame-alist
6267 '((height . 14) (width . 80) (unsplittable . t))
6268 "Alist of parameters for special frames.
6269 Special frames are used for buffers whose names are listed in
6270 `special-display-buffer-names' and for buffers whose names match
6271 one of the regular expressions in `special-display-regexps'.
6273 This variable can be set in your init file, like this:
6275 (setq special-display-frame-alist \\='((width . 80) (height . 20)))
6277 These supersede the values given in `default-frame-alist'."
6278 :type '(repeat (cons :format "%v"
6279 (symbol :tag "Parameter")
6280 (sexp :tag "Value")))
6281 :group 'frames)
6282 (make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3")
6284 (defun special-display-popup-frame (buffer &optional args)
6285 "Pop up a frame displaying BUFFER and return its window.
6286 If BUFFER is already displayed in a visible or iconified frame,
6287 raise that frame. Otherwise, display BUFFER in a new frame.
6289 Optional argument ARGS is a list specifying additional
6290 information.
6292 If ARGS is an alist, use it as a list of frame parameters. If
6293 these parameters contain (same-window . t), display BUFFER in
6294 the selected window. If they contain (same-frame . t), display
6295 BUFFER in a window of the selected frame.
6297 If ARGS is a list whose car is a symbol, use (car ARGS) as a
6298 function to do the work. Pass it BUFFER as first argument, and
6299 pass the elements of (cdr ARGS) as the remaining arguments."
6300 (if (and args (symbolp (car args)))
6301 (apply (car args) buffer (cdr args))
6302 (let ((window (get-buffer-window buffer 0)))
6304 ;; If we have a window already, make it visible.
6305 (when window
6306 (let ((frame (window-frame window)))
6307 (make-frame-visible frame)
6308 (raise-frame frame)
6309 (display-buffer-record-window 'reuse window buffer)
6310 window))
6311 ;; Reuse the current window if the user requested it.
6312 (when (cdr (assq 'same-window args))
6313 (condition-case nil
6314 (progn (switch-to-buffer buffer nil t) (selected-window))
6315 (error nil)))
6316 ;; Stay on the same frame if requested.
6317 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
6318 (let* ((pop-up-windows t)
6319 pop-up-frames
6320 special-display-buffer-names special-display-regexps)
6321 (display-buffer buffer)))
6322 ;; If no window yet, make one in a new frame.
6323 (let* ((frame
6324 (with-current-buffer buffer
6325 (make-frame (append args special-display-frame-alist))))
6326 (window (frame-selected-window frame)))
6327 (display-buffer-record-window 'frame window buffer)
6328 (unless (eq buffer (window-buffer window))
6329 (set-window-buffer window buffer)
6330 (set-window-prev-buffers window nil))
6331 (set-window-dedicated-p window t)
6332 window)))))
6334 (defcustom special-display-function 'special-display-popup-frame
6335 "Function to call for displaying special buffers.
6336 This function is called with two arguments - the buffer and,
6337 optionally, a list - and should return a window displaying that
6338 buffer. The default value usually makes a separate frame for the
6339 buffer using `special-display-frame-alist' to specify the frame
6340 parameters. See the definition of `special-display-popup-frame'
6341 for how to specify such a function.
6343 A buffer is special when its name is either listed in
6344 `special-display-buffer-names' or matches a regexp in
6345 `special-display-regexps'.
6347 The specified function should call `display-buffer-record-window'
6348 with corresponding arguments to set up the quit-restore parameter
6349 of the window used."
6350 :type 'function
6351 :group 'frames)
6352 (make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3")
6354 (defcustom same-window-buffer-names nil
6355 "List of names of buffers that should appear in the \"same\" window.
6356 `display-buffer' and `pop-to-buffer' show a buffer whose name is
6357 on this list in the selected rather than some other window.
6359 An element of this list can be a cons cell instead of just a
6360 string. In that case, the cell's car must be a string specifying
6361 the buffer name. This is for compatibility with
6362 `special-display-buffer-names'; the cdr of the cons cell is
6363 ignored.
6365 See also `same-window-regexps'."
6366 :type '(repeat (string :format "%v"))
6367 :group 'windows)
6369 (defcustom same-window-regexps nil
6370 "List of regexps saying which buffers should appear in the \"same\" window.
6371 `display-buffer' and `pop-to-buffer' show a buffer whose name
6372 matches a regexp on this list in the selected rather than some
6373 other window.
6375 An element of this list can be a cons cell instead of just a
6376 string. In that case, the cell's car must be a regexp matching
6377 the buffer name. This is for compatibility with
6378 `special-display-regexps'; the cdr of the cons cell is ignored.
6380 See also `same-window-buffer-names'."
6381 :type '(repeat (regexp :format "%v"))
6382 :group 'windows)
6384 (defun same-window-p (buffer-name)
6385 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
6386 This function returns non-nil if `display-buffer' or
6387 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
6388 selected rather than (as usual) some other window. See
6389 `same-window-buffer-names' and `same-window-regexps'."
6390 (cond
6391 ((not (stringp buffer-name)))
6392 ;; The elements of `same-window-buffer-names' can be buffer
6393 ;; names or cons cells whose cars are buffer names.
6394 ((member buffer-name same-window-buffer-names))
6395 ((assoc buffer-name same-window-buffer-names))
6396 ((catch 'found
6397 (dolist (regexp same-window-regexps)
6398 ;; The elements of `same-window-regexps' can be regexps
6399 ;; or cons cells whose cars are regexps.
6400 (when (or (and (stringp regexp)
6401 (string-match-p regexp buffer-name))
6402 (and (consp regexp) (stringp (car regexp))
6403 (string-match-p (car regexp) buffer-name)))
6404 (throw 'found t)))))))
6406 (defcustom pop-up-frames nil
6407 "Whether `display-buffer' should make a separate frame.
6408 If nil, never make a separate frame.
6409 If the value is `graphic-only', make a separate frame
6410 on graphic displays only.
6411 Any other non-nil value means always make a separate frame."
6412 :type '(choice
6413 (const :tag "Never" nil)
6414 (const :tag "On graphic displays only" graphic-only)
6415 (const :tag "Always" t))
6416 :group 'windows)
6418 (defcustom display-buffer-reuse-frames nil
6419 "Non-nil means `display-buffer' should reuse frames.
6420 If the buffer in question is already displayed in a frame, raise
6421 that frame."
6422 :type 'boolean
6423 :version "21.1"
6424 :group 'windows)
6426 (make-obsolete-variable
6427 'display-buffer-reuse-frames
6428 "use a `reusable-frames' alist entry in `display-buffer-alist'."
6429 "24.3")
6431 (defcustom pop-up-windows t
6432 "Non-nil means `display-buffer' should make a new window."
6433 :type 'boolean
6434 :group 'windows)
6436 (defcustom split-window-preferred-function 'split-window-sensibly
6437 "Function called by `display-buffer' routines to split a window.
6438 This function is called with a window as single argument and is
6439 supposed to split that window and return the new window. If the
6440 window can (or shall) not be split, it is supposed to return nil.
6441 The default is to call the function `split-window-sensibly' which
6442 tries to split the window in a way which seems most suitable.
6443 You can customize the options `split-height-threshold' and/or
6444 `split-width-threshold' in order to have `split-window-sensibly'
6445 prefer either vertical or horizontal splitting.
6447 If you set this to any other function, bear in mind that the
6448 `display-buffer' routines may call this function two times. The
6449 argument of the first call is the largest window on its frame.
6450 If that call fails to return a live window, the function is
6451 called again with the least recently used window as argument. If
6452 that call fails too, `display-buffer' will use an existing window
6453 to display its buffer.
6455 The window selected at the time `display-buffer' was invoked is
6456 still selected when this function is called. Hence you can
6457 compare the window argument with the value of `selected-window'
6458 if you intend to split the selected window instead or if you do
6459 not want to split the selected window."
6460 :type 'function
6461 :version "23.1"
6462 :group 'windows)
6464 (defcustom split-height-threshold 80
6465 "Minimum height for splitting windows sensibly.
6466 If this is an integer, `split-window-sensibly' may split a window
6467 vertically only if it has at least this many lines. If this is
6468 nil, `split-window-sensibly' is not allowed to split a window
6469 vertically. If, however, a window is the only window on its
6470 frame, or all the other ones are dedicated,
6471 `split-window-sensibly' may split it vertically disregarding the
6472 value of this variable."
6473 :type '(choice (const nil) (integer :tag "lines"))
6474 :version "23.1"
6475 :group 'windows)
6477 (defcustom split-width-threshold 160
6478 "Minimum width for splitting windows sensibly.
6479 If this is an integer, `split-window-sensibly' may split a window
6480 horizontally only if it has at least this many columns. If this
6481 is nil, `split-window-sensibly' is not allowed to split a window
6482 horizontally."
6483 :type '(choice (const nil) (integer :tag "columns"))
6484 :version "23.1"
6485 :group 'windows)
6487 (defun window-splittable-p (window &optional horizontal)
6488 "Return non-nil if `split-window-sensibly' may split WINDOW.
6489 Optional argument HORIZONTAL nil or omitted means check whether
6490 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
6491 non-nil means check whether WINDOW may be split horizontally.
6493 WINDOW may be split vertically when the following conditions
6494 hold:
6495 - `window-size-fixed' is either nil or equals `width' for the
6496 buffer of WINDOW.
6497 - `split-height-threshold' is an integer and WINDOW is at least as
6498 high as `split-height-threshold'.
6499 - When WINDOW is split evenly, the emanating windows are at least
6500 `window-min-height' lines tall and can accommodate at least one
6501 line plus - if WINDOW has one - a mode line.
6503 WINDOW may be split horizontally when the following conditions
6504 hold:
6505 - `window-size-fixed' is either nil or equals `height' for the
6506 buffer of WINDOW.
6507 - `split-width-threshold' is an integer and WINDOW is at least as
6508 wide as `split-width-threshold'.
6509 - When WINDOW is split evenly, the emanating windows are at least
6510 `window-min-width' or two (whichever is larger) columns wide."
6511 (when (and (window-live-p window)
6512 (not (window-parameter window 'window-side)))
6513 (with-current-buffer (window-buffer window)
6514 (if horizontal
6515 ;; A window can be split horizontally when its width is not
6516 ;; fixed, it is at least `split-width-threshold' columns wide
6517 ;; and at least twice as wide as `window-min-width' and 2 (the
6518 ;; latter value is hardcoded).
6519 (and (memq window-size-fixed '(nil height))
6520 ;; Testing `window-full-width-p' here hardly makes any
6521 ;; sense nowadays. This can be done more intuitively by
6522 ;; setting up `split-width-threshold' appropriately.
6523 (numberp split-width-threshold)
6524 (>= (window-width window)
6525 (max split-width-threshold
6526 (* 2 (max window-min-width 2)))))
6527 ;; A window can be split vertically when its height is not
6528 ;; fixed, it is at least `split-height-threshold' lines high,
6529 ;; and it is at least twice as high as `window-min-height' and 2
6530 ;; if it has a mode line or 1.
6531 (and (memq window-size-fixed '(nil width))
6532 (numberp split-height-threshold)
6533 (>= (window-height window)
6534 (max split-height-threshold
6535 (* 2 (max window-min-height
6536 (if mode-line-format 2 1))))))))))
6538 (defun split-window-sensibly (&optional window)
6539 "Split WINDOW in a way suitable for `display-buffer'.
6540 WINDOW defaults to the currently selected window.
6541 If `split-height-threshold' specifies an integer, WINDOW is at
6542 least `split-height-threshold' lines tall and can be split
6543 vertically, split WINDOW into two windows one above the other and
6544 return the lower window. Otherwise, if `split-width-threshold'
6545 specifies an integer, WINDOW is at least `split-width-threshold'
6546 columns wide and can be split horizontally, split WINDOW into two
6547 windows side by side and return the window on the right. If this
6548 can't be done either and WINDOW is the only window on its frame,
6549 try to split WINDOW vertically disregarding any value specified
6550 by `split-height-threshold'. If that succeeds, return the lower
6551 window. Return nil otherwise.
6553 By default `display-buffer' routines call this function to split
6554 the largest or least recently used window. To change the default
6555 customize the option `split-window-preferred-function'.
6557 You can enforce this function to not split WINDOW horizontally,
6558 by setting (or binding) the variable `split-width-threshold' to
6559 nil. If, in addition, you set `split-height-threshold' to zero,
6560 chances increase that this function does split WINDOW vertically.
6562 In order to not split WINDOW vertically, set (or bind) the
6563 variable `split-height-threshold' to nil. Additionally, you can
6564 set `split-width-threshold' to zero to make a horizontal split
6565 more likely to occur.
6567 Have a look at the function `window-splittable-p' if you want to
6568 know how `split-window-sensibly' determines whether WINDOW can be
6569 split."
6570 (let ((window (or window (selected-window))))
6571 (or (and (window-splittable-p window)
6572 ;; Split window vertically.
6573 (with-selected-window window
6574 (split-window-below)))
6575 (and (window-splittable-p window t)
6576 ;; Split window horizontally.
6577 (with-selected-window window
6578 (split-window-right)))
6579 (and
6580 ;; If WINDOW is the only usable window on its frame (it is
6581 ;; the only one or, not being the only one, all the other
6582 ;; ones are dedicated) and is not the minibuffer window, try
6583 ;; to split it vertically disregarding the value of
6584 ;; `split-height-threshold'.
6585 (let ((frame (window-frame window)))
6587 (eq window (frame-root-window frame))
6588 (catch 'done
6589 (walk-window-tree (lambda (w)
6590 (unless (or (eq w window)
6591 (window-dedicated-p w))
6592 (throw 'done nil)))
6593 frame)
6594 t)))
6595 (not (window-minibuffer-p window))
6596 (let ((split-height-threshold 0))
6597 (when (window-splittable-p window)
6598 (with-selected-window window
6599 (split-window-below))))))))
6601 (defun window--try-to-split-window (window &optional alist)
6602 "Try to split WINDOW.
6603 Return value returned by `split-window-preferred-function' if it
6604 represents a live window, nil otherwise."
6605 (and (window-live-p window)
6606 (not (frame-parameter (window-frame window) 'unsplittable))
6607 (let* ((window-combination-limit
6608 ;; When `window-combination-limit' equals
6609 ;; `display-buffer' or equals `resize-window' and a
6610 ;; `window-height' or `window-width' alist entry are
6611 ;; present, bind it to t so resizing steals space
6612 ;; preferably from the window that was split.
6613 (if (or (eq window-combination-limit 'display-buffer)
6614 (and (eq window-combination-limit 'window-size)
6615 (or (cdr (assq 'window-height alist))
6616 (cdr (assq 'window-width alist)))))
6618 window-combination-limit))
6619 (new-window
6620 ;; Since `split-window-preferred-function' might
6621 ;; throw an error use `condition-case'.
6622 (condition-case nil
6623 (funcall split-window-preferred-function window)
6624 (error nil))))
6625 (and (window-live-p new-window) new-window))))
6627 (defun window--frame-usable-p (frame)
6628 "Return FRAME if it can be used to display a buffer."
6629 (when (frame-live-p frame)
6630 (let ((window (frame-root-window frame)))
6631 ;; `frame-root-window' may be an internal window which is considered
6632 ;; "dead" by `window-live-p'. Hence if `window' is not live we
6633 ;; implicitly know that `frame' has a visible window we can use.
6634 (unless (and (window-live-p window)
6635 (or (window-minibuffer-p window)
6636 ;; If the window is soft-dedicated, the frame is usable.
6637 ;; Actually, even if the window is really dedicated,
6638 ;; the frame is still usable by splitting it.
6639 ;; At least Emacs-22 allowed it, and it is desirable
6640 ;; when displaying same-frame windows.
6641 nil ; (eq t (window-dedicated-p window))
6643 frame))))
6645 (defcustom even-window-sizes t
6646 "If non-nil `display-buffer' will try to even window sizes.
6647 Otherwise `display-buffer' will leave the window configuration
6648 alone. Special values are `height-only' to even heights only and
6649 `width-only' to even widths only. Any other value means to even
6650 any of them."
6651 :type '(choice
6652 (const :tag "Never" nil)
6653 (const :tag "Side-by-side windows only" width-only)
6654 (const :tag "Windows above or below only" height-only)
6655 (const :tag "Always" t))
6656 :version "25.1"
6657 :group 'windows)
6658 (defvaralias 'even-window-heights 'even-window-sizes)
6660 (defun window--even-window-sizes (window)
6661 "Even sizes of WINDOW and selected window.
6662 Even only if these windows are the only children of their parent,
6663 `even-window-sizes' has the appropriate value and the selected
6664 window is larger than WINDOW."
6665 (when (and (= (window-child-count (window-parent window)) 2)
6666 (eq (window-parent) (window-parent window)))
6667 (cond
6668 ((and (not (memq even-window-sizes '(nil height-only)))
6669 (window-combined-p window t)
6670 (> (window-total-width) (window-total-width window)))
6671 (condition-case nil
6672 (enlarge-window
6673 (/ (- (window-total-width window) (window-total-width)) 2) t)
6674 (error nil)))
6675 ((and (not (memq even-window-sizes '(nil width-only)))
6676 (window-combined-p window)
6677 (> (window-total-height) (window-total-height window)))
6678 (condition-case nil
6679 (enlarge-window
6680 (/ (- (window-total-height window) (window-total-height)) 2))
6681 (error nil))))))
6683 (defun window--display-buffer (buffer window type &optional alist dedicated)
6684 "Display BUFFER in WINDOW.
6685 TYPE must be one of the symbols `reuse', `window' or `frame' and
6686 is passed unaltered to `display-buffer-record-window'. ALIST is
6687 the alist argument of `display-buffer'. Set `window-dedicated-p'
6688 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
6689 live."
6690 (when (and (buffer-live-p buffer) (window-live-p window))
6691 (display-buffer-record-window type window buffer)
6692 (unless (eq buffer (window-buffer window))
6693 (set-window-dedicated-p window nil)
6694 (set-window-buffer window buffer))
6695 (when dedicated
6696 (set-window-dedicated-p window dedicated))
6697 (when (memq type '(window frame))
6698 (set-window-prev-buffers window nil))
6699 (let ((quit-restore (window-parameter window 'quit-restore))
6700 (height (cdr (assq 'window-height alist)))
6701 (width (cdr (assq 'window-width alist)))
6702 (size (cdr (assq 'window-size alist)))
6703 (preserve-size (cdr (assq 'preserve-size alist))))
6704 (cond
6705 ((or (eq type 'frame)
6706 (and (eq (car quit-restore) 'same)
6707 (eq (nth 1 quit-restore) 'frame)))
6708 ;; Adjust size of frame if asked for.
6709 (cond
6710 ((not size))
6711 ((consp size)
6712 (let ((width (car size))
6713 (height (cdr size))
6714 (frame (window-frame window)))
6715 (when (and (numberp width) (numberp height))
6716 (set-frame-height
6717 frame (+ (frame-height frame)
6718 (- height (window-total-height window))))
6719 (set-frame-width
6720 frame (+ (frame-width frame)
6721 (- width (window-total-width window)))))))
6722 ((functionp size)
6723 (ignore-errors (funcall size window)))))
6724 ((or (eq type 'window)
6725 (and (eq (car quit-restore) 'same)
6726 (eq (nth 1 quit-restore) 'window)))
6727 ;; Adjust height of window if asked for.
6728 (cond
6729 ((not height))
6730 ((numberp height)
6731 (let* ((new-height
6732 (if (integerp height)
6733 height
6734 (round
6735 (* (window-total-height (frame-root-window window))
6736 height))))
6737 (delta (- new-height (window-total-height window))))
6738 (when (and (window--resizable-p window delta nil 'safe)
6739 (window-combined-p window))
6740 (window-resize window delta nil 'safe))))
6741 ((functionp height)
6742 (ignore-errors (funcall height window))))
6743 ;; Adjust width of window if asked for.
6744 (cond
6745 ((not width))
6746 ((numberp width)
6747 (let* ((new-width
6748 (if (integerp width)
6749 width
6750 (round
6751 (* (window-total-width (frame-root-window window))
6752 width))))
6753 (delta (- new-width (window-total-width window))))
6754 (when (and (window--resizable-p window delta t 'safe)
6755 (window-combined-p window t))
6756 (window-resize window delta t 'safe))))
6757 ((functionp width)
6758 (ignore-errors (funcall width window))))
6759 ;; Preserve window size if asked for.
6760 (when (consp preserve-size)
6761 (window-preserve-size window t (car preserve-size))
6762 (window-preserve-size window nil (cdr preserve-size)))))
6763 ;; Assign any window parameters specified.
6764 (let ((parameters (cdr (assq 'window-parameters alist))))
6765 (dolist (parameter parameters)
6766 (set-window-parameter
6767 window (car parameter) (cdr parameter)))))
6768 window))
6770 (defun window--maybe-raise-frame (frame)
6771 (make-frame-visible frame)
6772 (unless (or (frame-parameter frame 'no-focus-on-map)
6773 ;; Don't raise frames that should not get focus.
6774 (frame-parameter frame 'no-accept-focus)
6775 ;; Assume the selected frame is already visible enough.
6776 (eq frame (selected-frame))
6777 ;; Assume the frame from which we invoked the
6778 ;; minibuffer is visible.
6779 (and (minibuffer-window-active-p (selected-window))
6780 (eq frame (window-frame (minibuffer-selected-window)))))
6781 (raise-frame frame)))
6783 ;; FIXME: Not implemented.
6784 ;; FIXME: By the way, there could be more levels of dedication:
6785 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
6786 ;; the window hasn't been used for something else yet.
6787 ;; - `soft' (`softly') dedicated only allows reuse when asked explicitly.
6788 ;; - `strongly' never allows reuse.
6789 (defvar display-buffer-mark-dedicated nil
6790 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
6791 The actual non-nil value of this variable will be copied to the
6792 `window-dedicated-p' flag.")
6794 (defconst display-buffer--action-function-custom-type
6795 '(choice :tag "Function"
6796 (const :tag "--" ignore) ; default for insertion
6797 (const display-buffer-reuse-window)
6798 (const display-buffer-pop-up-window)
6799 (const display-buffer-same-window)
6800 (const display-buffer-pop-up-frame)
6801 (const display-buffer-in-child-frame)
6802 (const display-buffer-below-selected)
6803 (const display-buffer-at-bottom)
6804 (const display-buffer-in-previous-window)
6805 (const display-buffer-use-some-window)
6806 (const display-buffer-use-some-frame)
6807 (function :tag "Other function"))
6808 "Custom type for `display-buffer' action functions.")
6810 (defconst display-buffer--action-custom-type
6811 `(cons :tag "Action"
6812 (choice :tag "Action functions"
6813 ,display-buffer--action-function-custom-type
6814 (repeat
6815 :tag "List of functions"
6816 ,display-buffer--action-function-custom-type))
6817 (alist :tag "Action arguments"
6818 :key-type symbol
6819 :value-type (sexp :tag "Value")))
6820 "Custom type for `display-buffer' actions.")
6822 (defvar display-buffer-overriding-action '(nil . nil)
6823 "Overriding action to perform to display a buffer.
6824 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6825 function or a list of functions. Each function should accept two
6826 arguments: a buffer to display and an alist similar to ALIST.
6827 See `display-buffer' for details.")
6828 (put 'display-buffer-overriding-action 'risky-local-variable t)
6830 (defcustom display-buffer-alist nil
6831 "Alist of conditional actions for `display-buffer'.
6832 This is a list of elements (CONDITION . ACTION), where:
6834 CONDITION is either a regexp matching buffer names, or a
6835 function that takes two arguments - a buffer name and the
6836 ACTION argument of `display-buffer' - and returns a boolean.
6838 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
6839 function or a list of functions. Each such function should
6840 accept two arguments: a buffer to display and an alist of the
6841 same form as ALIST. See `display-buffer' for details.
6843 `display-buffer' scans this alist until it either finds a
6844 matching regular expression or the function specified by a
6845 condition returns non-nil. In any of these cases, it adds the
6846 associated action to the list of actions it will try."
6847 :type `(alist :key-type
6848 (choice :tag "Condition"
6849 regexp
6850 (function :tag "Matcher function"))
6851 :value-type ,display-buffer--action-custom-type)
6852 :risky t
6853 :version "24.1"
6854 :group 'windows)
6856 (defcustom display-buffer-base-action '(nil . nil)
6857 "User-specified default action for `display-buffer'.
6858 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6859 function or a list of functions. Each function should accept two
6860 arguments: a buffer to display and an alist similar to ALIST.
6861 See `display-buffer' for details."
6862 :type display-buffer--action-custom-type
6863 :risky t
6864 :version "24.1"
6865 :group 'windows)
6867 (defconst display-buffer-fallback-action
6868 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
6869 display-buffer-reuse-window
6870 display-buffer--maybe-pop-up-frame-or-window
6871 display-buffer-in-previous-window
6872 display-buffer-use-some-window
6873 ;; If all else fails, pop up a new frame.
6874 display-buffer-pop-up-frame))
6875 "Default fallback action for `display-buffer'.
6876 This is the action used by `display-buffer' if no other actions
6877 specified, e.g. by the user options `display-buffer-alist' or
6878 `display-buffer-base-action'. See `display-buffer'.")
6879 (put 'display-buffer-fallback-action 'risky-local-variable t)
6881 (defun display-buffer-assq-regexp (buffer-name alist action)
6882 "Retrieve ALIST entry corresponding to BUFFER-NAME.
6883 ACTION is the action argument passed to `display-buffer'."
6884 (catch 'match
6885 (dolist (entry alist)
6886 (let ((key (car entry)))
6887 (when (or (and (stringp key)
6888 (string-match-p key buffer-name))
6889 (and (functionp key)
6890 (funcall key buffer-name action)))
6891 (throw 'match (cdr entry)))))))
6893 (defvar display-buffer--same-window-action
6894 '(display-buffer-same-window
6895 (inhibit-same-window . nil))
6896 "A `display-buffer' action for displaying in the same window.")
6897 (put 'display-buffer--same-window-action 'risky-local-variable t)
6899 (defvar display-buffer--other-frame-action
6900 '((display-buffer-reuse-window
6901 display-buffer-pop-up-frame)
6902 (reusable-frames . 0)
6903 (inhibit-same-window . t))
6904 "A `display-buffer' action for displaying in another frame.")
6905 (put 'display-buffer--other-frame-action 'risky-local-variable t)
6907 (defun display-buffer (buffer-or-name &optional action frame)
6908 "Display BUFFER-OR-NAME in some window, without selecting it.
6909 BUFFER-OR-NAME must be a buffer or the name of an existing
6910 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
6911 or nil if no such window is found.
6913 Optional argument ACTION, if non-nil, should specify a display
6914 action. Its form is described below.
6916 Optional argument FRAME, if non-nil, acts like an additional
6917 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
6918 specifying the frame(s) to search for a window that is already
6919 displaying the buffer. See `display-buffer-reuse-window'.
6921 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
6922 where FUNCTION is either a function or a list of functions, and
6923 ALIST is an arbitrary association list (alist).
6925 Each such FUNCTION should accept two arguments: the buffer to
6926 display and an alist. Based on those arguments, it should
6927 display the buffer and return the window. If the caller is
6928 prepared to handle the case of not displaying the buffer
6929 and returning nil from `display-buffer' it should pass
6930 \(allow-no-window . t) as an element of the ALIST.
6932 The `display-buffer' function builds a function list and an alist
6933 by combining the functions and alists specified in
6934 `display-buffer-overriding-action', `display-buffer-alist', the
6935 ACTION argument, `display-buffer-base-action', and
6936 `display-buffer-fallback-action' (in order). Then it calls each
6937 function in the combined function list in turn, passing the
6938 buffer as the first argument and the combined alist as the second
6939 argument, until one of the functions returns non-nil.
6941 If ACTION is nil, the function list and the alist are built using
6942 only the other variables mentioned above.
6944 Available action functions include:
6945 `display-buffer-same-window'
6946 `display-buffer-reuse-window'
6947 `display-buffer-pop-up-frame'
6948 `display-buffer-in-child-frame'
6949 `display-buffer-pop-up-window'
6950 `display-buffer-in-previous-window'
6951 `display-buffer-use-some-window'
6952 `display-buffer-use-some-frame'
6954 Recognized alist entries include:
6956 `inhibit-same-window' -- A non-nil value prevents the same
6957 window from being used for display.
6959 `inhibit-switch-frame' -- A non-nil value prevents any other
6960 frame from being raised or selected,
6961 even if the window is displayed there.
6963 `reusable-frames' -- Value specifies frame(s) to search for a
6964 window that already displays the buffer.
6965 See `display-buffer-reuse-window'.
6967 `pop-up-frame-parameters' -- Value specifies an alist of frame
6968 parameters to give a new frame, if
6969 one is created.
6971 `window-height' -- Value specifies either an integer (the number
6972 of lines of a new window), a floating point number (the
6973 fraction of a new window with respect to the height of the
6974 frame's root window) or a function to be called with one
6975 argument - a new window. The function is supposed to adjust
6976 the height of the window; its return value is ignored.
6977 Suitable functions are `shrink-window-if-larger-than-buffer'
6978 and `fit-window-to-buffer'.
6980 `window-width' -- Value specifies either an integer (the number
6981 of columns of a new window), a floating point number (the
6982 fraction of a new window with respect to the width of the
6983 frame's root window) or a function to be called with one
6984 argument - a new window. The function is supposed to adjust
6985 the width of the window; its return value is ignored.
6987 `allow-no-window' -- A non-nil value indicates readiness for the case
6988 of not displaying the buffer and FUNCTION can safely return
6989 a non-window value to suppress displaying.
6991 `preserve-size' -- Value should be either (t . nil) to
6992 preserve the width of the window, (nil . t) to preserve its
6993 height or (t . t) to preserve both.
6995 `window-parameters' -- Value specifies an alist of window
6996 parameters to give the chosen window.
6998 The ACTION argument to `display-buffer' can also have a non-nil
6999 and non-list value. This means to display the buffer in a window
7000 other than the selected one, even if it is already displayed in
7001 the selected window. If called interactively with a prefix
7002 argument, ACTION is t."
7003 (interactive (list (read-buffer "Display buffer: " (other-buffer))
7004 (if current-prefix-arg t)))
7005 (let ((buffer (if (bufferp buffer-or-name)
7006 buffer-or-name
7007 (get-buffer buffer-or-name)))
7008 ;; Make sure that when we split windows the old window keeps
7009 ;; point, bug#14829.
7010 (split-window-keep-point t)
7011 ;; Handle the old form of the first argument.
7012 (inhibit-same-window (and action (not (listp action)))))
7013 (unless (listp action) (setq action nil))
7014 (if display-buffer-function
7015 ;; If `display-buffer-function' is defined, let it do the job.
7016 (funcall display-buffer-function buffer inhibit-same-window)
7017 ;; Otherwise, use the defined actions.
7018 (let* ((user-action
7019 (display-buffer-assq-regexp
7020 (buffer-name buffer) display-buffer-alist action))
7021 (special-action (display-buffer--special-action buffer))
7022 ;; Extra actions from the arguments to this function:
7023 (extra-action
7024 (cons nil (append (if inhibit-same-window
7025 '((inhibit-same-window . t)))
7026 (if frame
7027 `((reusable-frames . ,frame))))))
7028 ;; Construct action function list and action alist.
7029 (actions (list display-buffer-overriding-action
7030 user-action special-action action extra-action
7031 display-buffer-base-action
7032 display-buffer-fallback-action))
7033 (functions (apply 'append
7034 (mapcar (lambda (x)
7035 (setq x (car x))
7036 (if (functionp x) (list x) x))
7037 actions)))
7038 (alist (apply 'append (mapcar 'cdr actions)))
7039 window)
7040 (unless (buffer-live-p buffer)
7041 (error "Invalid buffer"))
7042 (while (and functions (not window))
7043 (setq window (funcall (car functions) buffer alist)
7044 functions (cdr functions)))
7045 (and (windowp window) window)))))
7047 (defun display-buffer-other-frame (buffer)
7048 "Display buffer BUFFER preferably in another frame.
7049 This uses the function `display-buffer' as a subroutine; see
7050 its documentation for additional customization information."
7051 (interactive "BDisplay buffer in other frame: ")
7052 (display-buffer buffer display-buffer--other-frame-action t))
7054 ;;; `display-buffer' action functions:
7056 (defun display-buffer-use-some-frame (buffer alist)
7057 "Display BUFFER in an existing frame that meets a predicate
7058 \(by default any frame other than the current frame). If
7059 successful, return the window used; otherwise return nil.
7061 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
7062 raising the frame.
7064 If ALIST has a non-nil `frame-predicate' entry, its value is a
7065 function taking one argument (a frame), returning non-nil if the
7066 frame is a candidate; this function replaces the default
7067 predicate.
7069 If ALIST has a non-nil `inhibit-same-window' entry, avoid using
7070 the currently selected window (only useful with a frame-predicate
7071 that allows the selected frame)."
7072 (let* ((predicate (or (cdr (assq 'frame-predicate alist))
7073 (lambda (frame)
7074 (and
7075 (not (eq frame (selected-frame)))
7076 (not (window-dedicated-p
7078 (get-lru-window frame)
7079 (frame-first-window frame)))))
7081 (frame (car (filtered-frame-list predicate)))
7082 (window (and frame (get-lru-window frame nil (cdr (assq 'inhibit-same-window alist))))))
7083 (when window
7084 (prog1
7085 (window--display-buffer
7086 buffer window 'frame alist display-buffer-mark-dedicated)
7087 (unless (cdr (assq 'inhibit-switch-frame alist))
7088 (window--maybe-raise-frame frame))))))
7090 (defun display-buffer-same-window (buffer alist)
7091 "Display BUFFER in the selected window.
7092 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
7093 if the selected window is a minibuffer window or is dedicated to
7094 another buffer; in that case, return nil. Otherwise, return the
7095 selected window."
7096 (unless (or (cdr (assq 'inhibit-same-window alist))
7097 (window-minibuffer-p)
7098 (window-dedicated-p))
7099 (window--display-buffer buffer (selected-window) 'reuse alist)))
7101 (defun display-buffer--maybe-same-window (buffer alist)
7102 "Conditionally display BUFFER in the selected window.
7103 If `same-window-p' returns non-nil for BUFFER's name, call
7104 `display-buffer-same-window' and return its value. Otherwise,
7105 return nil."
7106 (and (same-window-p (buffer-name buffer))
7107 (display-buffer-same-window buffer alist)))
7109 (defun display-buffer-reuse-window (buffer alist)
7110 "Return a window that is already displaying BUFFER.
7111 Return nil if no usable window is found.
7113 If ALIST has a non-nil `inhibit-same-window' entry, the selected
7114 window is not eligible for reuse.
7116 If ALIST contains a `reusable-frames' entry, its value determines
7117 which frames to search for a reusable window:
7118 nil -- the selected frame (actually the last non-minibuffer frame)
7119 A frame -- just that frame
7120 `visible' -- all visible frames
7121 0 -- all frames on the current terminal
7122 t -- all frames.
7124 If ALIST contains no `reusable-frames' entry, search just the
7125 selected frame if `display-buffer-reuse-frames' and
7126 `pop-up-frames' are both nil; search all frames on the current
7127 terminal if either of those variables is non-nil.
7129 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7130 event that a window on another frame is chosen, avoid raising
7131 that frame."
7132 (let* ((alist-entry (assq 'reusable-frames alist))
7133 (frames (cond (alist-entry (cdr alist-entry))
7134 ((if (eq pop-up-frames 'graphic-only)
7135 (display-graphic-p)
7136 pop-up-frames)
7138 (display-buffer-reuse-frames 0)
7139 (t (last-nonminibuffer-frame))))
7140 (window (if (and (eq buffer (window-buffer))
7141 (not (cdr (assq 'inhibit-same-window alist))))
7142 (selected-window)
7143 (car (delq (selected-window)
7144 (get-buffer-window-list buffer 'nomini
7145 frames))))))
7146 (when (window-live-p window)
7147 (prog1 (window--display-buffer buffer window 'reuse alist)
7148 (unless (cdr (assq 'inhibit-switch-frame alist))
7149 (window--maybe-raise-frame (window-frame window)))))))
7151 (defun display-buffer-reuse-mode-window (buffer alist)
7152 "Return a window based on the mode of the buffer it displays.
7153 Display BUFFER in the returned window. Return nil if no usable
7154 window is found.
7156 If ALIST contains a `mode' entry, its value is a major mode (a
7157 symbol) or a list of modes. A window is a candidate if it
7158 displays a buffer that derives from one of the given modes. When
7159 ALIST contains no `mode' entry, the current major mode of BUFFER
7160 is used.
7162 The behavior is also controlled by entries for
7163 `inhibit-same-window', `reusable-frames' and
7164 `inhibit-switch-frame' as is done in the function
7165 `display-buffer-reuse-window'."
7166 (let* ((alist-entry (assq 'reusable-frames alist))
7167 (alist-mode-entry (assq 'mode alist))
7168 (frames (cond (alist-entry (cdr alist-entry))
7169 ((if (eq pop-up-frames 'graphic-only)
7170 (display-graphic-p)
7171 pop-up-frames)
7173 (display-buffer-reuse-frames 0)
7174 (t (last-nonminibuffer-frame))))
7175 (inhibit-same-window-p (cdr (assq 'inhibit-same-window alist)))
7176 (windows (window-list-1 nil 'nomini frames))
7177 (buffer-mode (with-current-buffer buffer major-mode))
7178 (allowed-modes (if alist-mode-entry
7179 (cdr alist-mode-entry)
7180 buffer-mode))
7181 (curwin (selected-window))
7182 (curframe (selected-frame)))
7183 (unless (listp allowed-modes)
7184 (setq allowed-modes (list allowed-modes)))
7185 (let (same-mode-same-frame
7186 same-mode-other-frame
7187 derived-mode-same-frame
7188 derived-mode-other-frame)
7189 (dolist (window windows)
7190 (let ((mode?
7191 (with-current-buffer (window-buffer window)
7192 (cond ((memq major-mode allowed-modes)
7193 'same)
7194 ((derived-mode-p allowed-modes)
7195 'derived)))))
7196 (when (and mode?
7197 (not (and inhibit-same-window-p
7198 (eq window curwin))))
7199 (push window (if (eq curframe (window-frame window))
7200 (if (eq mode? 'same)
7201 same-mode-same-frame
7202 derived-mode-same-frame)
7203 (if (eq mode? 'same)
7204 same-mode-other-frame
7205 derived-mode-other-frame))))))
7206 (let ((window (car (nconc same-mode-same-frame
7207 same-mode-other-frame
7208 derived-mode-same-frame
7209 derived-mode-other-frame))))
7210 (when (window-live-p window)
7211 (prog1 (window--display-buffer buffer window 'reuse alist)
7212 (unless (cdr (assq 'inhibit-switch-frame alist))
7213 (window--maybe-raise-frame (window-frame window)))))))))
7215 (defun display-buffer--special-action (buffer)
7216 "Return special display action for BUFFER, if any.
7217 If `special-display-p' returns non-nil for BUFFER, return an
7218 appropriate display action involving `special-display-function'.
7219 See `display-buffer' for the format of display actions."
7220 (and special-display-function
7221 ;; `special-display-p' returns either t or a list of frame
7222 ;; parameters to pass to `special-display-function'.
7223 (let ((pars (special-display-p (buffer-name buffer))))
7224 (when pars
7225 (list (list #'display-buffer-reuse-window
7226 (lambda (buffer _alist)
7227 (funcall special-display-function
7228 buffer (if (listp pars) pars)))))))))
7230 (defun display-buffer-pop-up-frame (buffer alist)
7231 "Display BUFFER in a new frame.
7232 This works by calling `pop-up-frame-function'. If successful,
7233 return the window used; otherwise return nil.
7235 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
7236 raising the new frame.
7238 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
7239 corresponding value is an alist of frame parameters to give the
7240 new frame."
7241 (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
7242 (pop-up-frame-alist (append params pop-up-frame-alist))
7243 (fun pop-up-frame-function)
7244 frame window)
7245 (when (and fun
7246 ;; Make BUFFER current so `make-frame' will use it as the
7247 ;; new frame's buffer (Bug#15133).
7248 (with-current-buffer buffer
7249 (setq frame (funcall fun)))
7250 (setq window (frame-selected-window frame)))
7251 (prog1 (window--display-buffer
7252 buffer window 'frame alist display-buffer-mark-dedicated)
7253 (unless (cdr (assq 'inhibit-switch-frame alist))
7254 (window--maybe-raise-frame frame))))))
7256 (defun display-buffer-pop-up-window (buffer alist)
7257 "Display BUFFER by popping up a new window.
7258 The new window is created on the selected frame, or in
7259 `last-nonminibuffer-frame' if no windows can be created there.
7260 If successful, return the new window; otherwise return nil.
7262 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7263 event that the new window is created on another frame, avoid
7264 raising the frame."
7265 (let ((frame (or (window--frame-usable-p (selected-frame))
7266 (window--frame-usable-p (last-nonminibuffer-frame))))
7267 window)
7268 (when (and (or (not (frame-parameter frame 'unsplittable))
7269 ;; If the selected frame cannot be split, look at
7270 ;; `last-nonminibuffer-frame'.
7271 (and (eq frame (selected-frame))
7272 (setq frame (last-nonminibuffer-frame))
7273 (window--frame-usable-p frame)
7274 (not (frame-parameter frame 'unsplittable))))
7275 ;; Attempt to split largest or least recently used window.
7276 (setq window (or (window--try-to-split-window
7277 (get-largest-window frame t) alist)
7278 (window--try-to-split-window
7279 (get-lru-window frame t) alist))))
7281 (prog1 (window--display-buffer
7282 buffer window 'window alist display-buffer-mark-dedicated)
7283 (unless (cdr (assq 'inhibit-switch-frame alist))
7284 (window--maybe-raise-frame (window-frame window)))))))
7286 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
7287 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
7288 If `pop-up-frames' is non-nil (and not `graphic-only' on a
7289 text-only terminal), try with `display-buffer-pop-up-frame'.
7291 If that cannot be done, and `pop-up-windows' is non-nil, try
7292 again with `display-buffer-pop-up-window'."
7293 (or (and (if (eq pop-up-frames 'graphic-only)
7294 (display-graphic-p)
7295 pop-up-frames)
7296 (display-buffer-pop-up-frame buffer alist))
7297 (and pop-up-windows
7298 (display-buffer-pop-up-window buffer alist))))
7300 (defun display-buffer-in-child-frame (buffer alist)
7301 "Display BUFFER in a child frame.
7302 By default, this either reuses a child frame of the selected
7303 frame or makes a new child frame of the selected frame. If
7304 successful, return the window used; otherwise return nil.
7306 If ALIST has a non-nil 'child-frame-parameters' entry, the
7307 corresponding value is an alist of frame parameters to give the
7308 new frame. A 'parent-frame' parameter specifying the selected
7309 frame is provided by default. If the child frame should be or
7310 become the child of any other frame, a corresponding entry must
7311 be added to ALIST."
7312 (let* ((parameters
7313 (append
7314 (cdr (assq 'child-frame-parameters alist))
7315 `((parent-frame . ,(selected-frame)))))
7316 (parent (or (assq 'parent-frame parameters)
7317 (selected-frame)))
7318 (share (assq 'share-child-frame parameters))
7319 share1 frame window)
7320 (with-current-buffer buffer
7321 (when (frame-live-p parent)
7322 (catch 'frame
7323 (dolist (frame1 (frame-list))
7324 (when (eq (frame-parent frame1) parent)
7325 (setq share1 (assq 'share-child-frame
7326 (frame-parameters frame1)))
7327 (when (eq share share1)
7328 (setq frame frame1)
7329 (throw 'frame t))))))
7331 (if frame
7332 (setq window (frame-selected-window frame))
7333 (setq frame (make-frame parameters))
7334 (setq window (frame-selected-window frame))))
7336 (prog1 (window--display-buffer
7337 buffer window 'frame alist display-buffer-mark-dedicated)
7338 (unless (cdr (assq 'inhibit-switch-frame alist))
7339 (window--maybe-raise-frame frame)))))
7341 (defun display-buffer-below-selected (buffer alist)
7342 "Try displaying BUFFER in a window below the selected window.
7343 If there is a window below the selected one and that window
7344 already displays BUFFER, use that window. Otherwise, try to
7345 create a new window below the selected one and show BUFFER there.
7346 If that attempt fails as well and there is a non-dedicated window
7347 below the selected one, use that window."
7348 (let (window)
7349 (or (and (setq window (window-in-direction 'below))
7350 (eq buffer (window-buffer window))
7351 (window--display-buffer buffer window 'reuse alist))
7352 (and (not (frame-parameter nil 'unsplittable))
7353 (let ((split-height-threshold 0)
7354 split-width-threshold)
7355 (setq window (window--try-to-split-window
7356 (selected-window) alist)))
7357 (window--display-buffer
7358 buffer window 'window alist display-buffer-mark-dedicated))
7359 (and (setq window (window-in-direction 'below))
7360 (not (window-dedicated-p window))
7361 (window--display-buffer
7362 buffer window 'reuse alist display-buffer-mark-dedicated)))))
7364 (defun display-buffer-at-bottom (buffer alist)
7365 "Try displaying BUFFER in a window at the bottom of the selected frame.
7366 This either reuses such a window provided it shows BUFFER
7367 already, splits a window at the bottom of the frame or the
7368 frame's root window, or reuses some window at the bottom of the
7369 selected frame."
7370 (let (bottom-window bottom-window-shows-buffer window)
7371 (walk-window-tree
7372 (lambda (window)
7373 (cond
7374 ((window-in-direction 'below window))
7375 ((and (not bottom-window-shows-buffer)
7376 (eq buffer (window-buffer window)))
7377 (setq bottom-window-shows-buffer t)
7378 (setq bottom-window window))
7379 ((not bottom-window)
7380 (setq bottom-window window)))
7381 nil nil 'nomini))
7382 (or (and bottom-window-shows-buffer
7383 (window--display-buffer
7384 buffer bottom-window 'reuse alist display-buffer-mark-dedicated))
7385 (and (not (frame-parameter nil 'unsplittable))
7386 (let (split-width-threshold)
7387 (setq window (window--try-to-split-window bottom-window alist)))
7388 (window--display-buffer
7389 buffer window 'window alist display-buffer-mark-dedicated))
7390 (and (not (frame-parameter nil 'unsplittable))
7391 (setq window (split-window-no-error (window-main-window)))
7392 (window--display-buffer
7393 buffer window 'window alist display-buffer-mark-dedicated))
7394 (and (setq window bottom-window)
7395 (not (window-dedicated-p window))
7396 (window--display-buffer
7397 buffer window 'reuse alist display-buffer-mark-dedicated)))))
7399 (defun display-buffer-in-previous-window (buffer alist)
7400 "Display BUFFER in a window previously showing it.
7401 If ALIST has a non-nil `inhibit-same-window' entry, the selected
7402 window is not eligible for reuse.
7404 If ALIST contains a `reusable-frames' entry, its value determines
7405 which frames to search for a reusable window:
7406 nil -- the selected frame (actually the last non-minibuffer frame)
7407 A frame -- just that frame
7408 `visible' -- all visible frames
7409 0 -- all frames on the current terminal
7410 t -- all frames.
7412 If ALIST contains no `reusable-frames' entry, search just the
7413 selected frame if `display-buffer-reuse-frames' and
7414 `pop-up-frames' are both nil; search all frames on the current
7415 terminal if either of those variables is non-nil.
7417 If ALIST has a `previous-window' entry, the window specified by
7418 that entry will override any other window found by the methods
7419 above, even if that window never showed BUFFER before."
7420 (let* ((alist-entry (assq 'reusable-frames alist))
7421 (inhibit-same-window
7422 (cdr (assq 'inhibit-same-window alist)))
7423 (frames (cond
7424 (alist-entry (cdr alist-entry))
7425 ((if (eq pop-up-frames 'graphic-only)
7426 (display-graphic-p)
7427 pop-up-frames)
7429 (display-buffer-reuse-frames 0)
7430 (t (last-nonminibuffer-frame))))
7431 best-window second-best-window window)
7432 ;; Scan windows whether they have shown the buffer recently.
7433 (catch 'best
7434 (dolist (window (window-list-1 (frame-first-window) 'nomini frames))
7435 (when (and (assq buffer (window-prev-buffers window))
7436 (not (window-dedicated-p window)))
7437 (if (eq window (selected-window))
7438 (unless inhibit-same-window
7439 (setq second-best-window window))
7440 (setq best-window window)
7441 (throw 'best t)))))
7442 ;; When ALIST has a `previous-window' entry, that entry may override
7443 ;; anything we found so far.
7444 (when (and (setq window (cdr (assq 'previous-window alist)))
7445 (window-live-p window)
7446 (not (window-dedicated-p window)))
7447 (if (eq window (selected-window))
7448 (unless inhibit-same-window
7449 (setq second-best-window window))
7450 (setq best-window window)))
7451 ;; Return best or second best window found.
7452 (when (setq window (or best-window second-best-window))
7453 (window--display-buffer buffer window 'reuse alist))))
7455 (defun display-buffer-use-some-window (buffer alist)
7456 "Display BUFFER in an existing window.
7457 Search for a usable window, set that window to the buffer, and
7458 return the window. If no suitable window is found, return nil.
7460 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7461 event that a window in another frame is chosen, avoid raising
7462 that frame."
7463 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
7464 (frame (or (window--frame-usable-p (selected-frame))
7465 (window--frame-usable-p (last-nonminibuffer-frame))))
7466 (window
7467 ;; Reuse an existing window.
7468 (or (get-lru-window frame nil not-this-window)
7469 (let ((window (get-buffer-window buffer 'visible)))
7470 (unless (and not-this-window
7471 (eq window (selected-window)))
7472 window))
7473 (get-largest-window 'visible nil not-this-window)
7474 (let ((window (get-buffer-window buffer 0)))
7475 (unless (and not-this-window
7476 (eq window (selected-window)))
7477 window))
7478 (get-largest-window 0 nil not-this-window)))
7479 (quit-restore (and (window-live-p window)
7480 (window-parameter window 'quit-restore)))
7481 (quad (nth 1 quit-restore)))
7482 (when (window-live-p window)
7483 ;; If the window was used by `display-buffer' before, try to
7484 ;; resize it to its old height but don't signal an error.
7485 (when (and (listp quad)
7486 (integerp (nth 3 quad))
7487 (> (nth 3 quad) (window-total-height window)))
7488 (condition-case nil
7489 (window-resize window (- (nth 3 quad) (window-total-height window)))
7490 (error nil)))
7492 (prog1
7493 (window--display-buffer buffer window 'reuse alist)
7494 (window--even-window-sizes window)
7495 (unless (cdr (assq 'inhibit-switch-frame alist))
7496 (window--maybe-raise-frame (window-frame window)))))))
7498 (defun display-buffer-no-window (_buffer alist)
7499 "Display BUFFER in no window.
7500 If ALIST has a non-nil `allow-no-window' entry, then don't display
7501 a window at all. This makes possible to override the default action
7502 and avoid displaying the buffer. It is assumed that when the caller
7503 specifies a non-nil `allow-no-window' then it can handle a nil value
7504 returned from `display-buffer' in this case."
7505 (when (cdr (assq 'allow-no-window alist))
7506 'fail))
7508 ;;; Display + selection commands:
7509 (defun pop-to-buffer (buffer-or-name &optional action norecord)
7510 "Display buffer specified by BUFFER-OR-NAME and select its window.
7511 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
7512 If it is a string not naming an existent buffer, create a buffer
7513 with that name. If BUFFER-OR-NAME is nil, choose some other
7514 buffer. In either case, make that buffer current and return it.
7516 This uses `display-buffer' as a subroutine. The optional ACTION
7517 argument is passed to `display-buffer' as its ACTION argument.
7518 See `display-buffer' for more information. ACTION is t if called
7519 interactively with a prefix argument, which means to pop to a
7520 window other than the selected one even if the buffer is already
7521 displayed in the selected window.
7523 If a suitable window is found, select that window. If it is not
7524 on the selected frame, raise that window's frame and give it
7525 input focus.
7527 Optional third arg NORECORD non-nil means do not put this buffer
7528 at the front of the list of recently selected ones."
7529 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
7530 (if current-prefix-arg t)))
7531 (let* ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))
7532 (old-frame (selected-frame))
7533 (window (display-buffer buffer action)))
7534 ;; Don't assume that `display-buffer' has supplied us with a window
7535 ;; (Bug#24332).
7536 (if window
7537 (let ((frame (window-frame window)))
7538 ;; If we chose another frame, make sure it gets input focus.
7539 (unless (eq frame old-frame)
7540 (select-frame-set-input-focus frame norecord))
7541 ;; Make sure the window is selected (Bug#8615), (Bug#6954)
7542 (select-window window norecord))
7543 ;; If `display-buffer' failed to supply a window, just make the
7544 ;; buffer current.
7545 (set-buffer buffer))
7546 ;; Return BUFFER even when we got no window.
7547 buffer))
7549 (defun pop-to-buffer-same-window (buffer &optional norecord)
7550 "Select buffer BUFFER in some window, preferably the same one.
7551 BUFFER may be a buffer, a string (a buffer name), or nil. If it
7552 is a string not naming an existent buffer, create a buffer with
7553 that name. If BUFFER is nil, choose some other buffer. Return
7554 the buffer.
7556 Optional argument NORECORD, if non-nil means do not put this
7557 buffer at the front of the list of recently selected ones.
7559 Unlike `pop-to-buffer', this function prefers using the selected
7560 window over popping up a new window or frame."
7561 (pop-to-buffer buffer display-buffer--same-window-action norecord))
7563 (defun read-buffer-to-switch (prompt)
7564 "Read the name of a buffer to switch to, prompting with PROMPT.
7565 Return the name of the buffer as a string.
7567 This function is intended for the `switch-to-buffer' family of
7568 commands since these need to omit the name of the current buffer
7569 from the list of completions and default values."
7570 (let ((rbts-completion-table (internal-complete-buffer-except)))
7571 (minibuffer-with-setup-hook
7572 (lambda ()
7573 (setq minibuffer-completion-table rbts-completion-table)
7574 ;; Since rbts-completion-table is built dynamically, we
7575 ;; can't just add it to the default value of
7576 ;; icomplete-with-completion-tables, so we add it
7577 ;; here manually.
7578 (if (and (boundp 'icomplete-with-completion-tables)
7579 (listp icomplete-with-completion-tables))
7580 (set (make-local-variable 'icomplete-with-completion-tables)
7581 (cons rbts-completion-table
7582 icomplete-with-completion-tables))))
7583 (read-buffer prompt (other-buffer (current-buffer))
7584 (confirm-nonexistent-file-or-buffer)))))
7586 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
7587 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
7588 If BUFFER-OR-NAME is nil, return the buffer returned by
7589 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
7590 exists, return that buffer. If no such buffer exists, create a
7591 buffer with the name BUFFER-OR-NAME and return that buffer."
7592 (if buffer-or-name
7593 (or (get-buffer buffer-or-name)
7594 (let ((buffer (get-buffer-create buffer-or-name)))
7595 (set-buffer-major-mode buffer)
7596 buffer))
7597 (other-buffer)))
7599 (defcustom switch-to-buffer-preserve-window-point t
7600 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
7601 If this is nil, `switch-to-buffer' displays the buffer at that
7602 buffer's `point'. If this is `already-displayed', it tries to
7603 display the buffer at its previous position in the selected
7604 window, provided the buffer is currently displayed in some other
7605 window on any visible or iconified frame. If this is t, it
7606 unconditionally tries to display the buffer at its previous
7607 position in the selected window.
7609 This variable is ignored if the buffer is already displayed in
7610 the selected window or never appeared in it before, or if
7611 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
7612 :type '(choice
7613 (const :tag "Never" nil)
7614 (const :tag "If already displayed elsewhere" already-displayed)
7615 (const :tag "Always" t))
7616 :group 'windows
7617 :version "26.1")
7619 (defcustom switch-to-buffer-in-dedicated-window nil
7620 "Allow switching to buffer in strongly dedicated windows.
7621 If non-nil, allow `switch-to-buffer' to proceed when called
7622 interactively and the selected window is strongly dedicated to
7623 its buffer.
7625 The following values are recognized:
7627 nil - disallow switching; signal an error
7629 prompt - prompt user whether to allow switching
7631 pop - perform `pop-to-buffer' instead
7633 t - undedicate selected window and switch
7635 When called non-interactively, `switch-to-buffer' always signals
7636 an error when the selected window is dedicated to its buffer and
7637 FORCE-SAME-WINDOW is non-nil."
7638 :type '(choice
7639 (const :tag "Disallow" nil)
7640 (const :tag "Prompt" prompt)
7641 (const :tag "Pop" pop)
7642 (const :tag "Allow" t))
7643 :group 'windows
7644 :version "25.1")
7646 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
7647 "Display buffer BUFFER-OR-NAME in the selected window.
7649 WARNING: This is NOT the way to work on another buffer temporarily
7650 within a Lisp program! Use `set-buffer' instead. That avoids
7651 messing with the window-buffer correspondences.
7653 If the selected window cannot display the specified buffer
7654 because it is a minibuffer window or strongly dedicated to
7655 another buffer, call `pop-to-buffer' to select the buffer in
7656 another window. In interactive use, if the selected window is
7657 strongly dedicated to its buffer, the value of the option
7658 `switch-to-buffer-in-dedicated-window' specifies how to proceed.
7660 If called interactively, read the buffer name using `read-buffer'.
7661 The variable `confirm-nonexistent-file-or-buffer' determines
7662 whether to request confirmation before creating a new buffer.
7663 See `read-buffer' for features related to input and completion
7664 of buffer names.
7666 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
7667 If BUFFER-OR-NAME is a string that does not identify an existing
7668 buffer, create a buffer with that name. If BUFFER-OR-NAME is
7669 nil, switch to the buffer returned by `other-buffer'.
7671 If optional argument NORECORD is non-nil, do not put the buffer
7672 at the front of the buffer list, and do not make the window
7673 displaying it the most recently selected one.
7675 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
7676 must be displayed in the selected window when called
7677 non-interactively; if that is impossible, signal an error rather
7678 than calling `pop-to-buffer'.
7680 The option `switch-to-buffer-preserve-window-point' can be used
7681 to make the buffer appear at its last position in the selected
7682 window.
7684 Return the buffer switched to."
7685 (interactive
7686 (let ((force-same-window
7687 (cond
7688 ((window-minibuffer-p) nil)
7689 ((not (eq (window-dedicated-p) t)) 'force-same-window)
7690 ((pcase switch-to-buffer-in-dedicated-window
7691 (`nil (user-error
7692 "Cannot switch buffers in a dedicated window"))
7693 (`prompt
7694 (if (y-or-n-p
7695 (format "Window is dedicated to %s; undedicate it"
7696 (window-buffer)))
7697 (progn
7698 (set-window-dedicated-p nil nil)
7699 'force-same-window)
7700 (user-error
7701 "Cannot switch buffers in a dedicated window")))
7702 (`pop nil)
7703 (_ (set-window-dedicated-p nil nil) 'force-same-window))))))
7704 (list (read-buffer-to-switch "Switch to buffer: ") nil force-same-window)))
7705 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
7706 (cond
7707 ;; Don't call set-window-buffer if it's not needed since it
7708 ;; might signal an error (e.g. if the window is dedicated).
7709 ((eq buffer (window-buffer)))
7710 ((window-minibuffer-p)
7711 (if force-same-window
7712 (user-error "Cannot switch buffers in minibuffer window")
7713 (pop-to-buffer buffer norecord)))
7714 ((eq (window-dedicated-p) t)
7715 (if force-same-window
7716 (user-error "Cannot switch buffers in a dedicated window")
7717 (pop-to-buffer buffer norecord)))
7719 (let* ((entry (assq buffer (window-prev-buffers)))
7720 (displayed (and (eq switch-to-buffer-preserve-window-point
7721 'already-displayed)
7722 (get-buffer-window buffer 0))))
7723 (set-window-buffer nil buffer)
7724 (when (and entry
7725 (or (eq switch-to-buffer-preserve-window-point t)
7726 displayed))
7727 ;; Try to restore start and point of buffer in the selected
7728 ;; window (Bug#4041).
7729 (set-window-start (selected-window) (nth 1 entry) t)
7730 (set-window-point nil (nth 2 entry))))))
7732 (unless norecord
7733 (select-window (selected-window)))
7734 (set-buffer buffer)))
7736 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
7737 "Select the buffer specified by BUFFER-OR-NAME in another window.
7738 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
7739 nil. Return the buffer switched to.
7741 If called interactively, read the buffer name using `read-buffer'.
7742 The variable `confirm-nonexistent-file-or-buffer' determines
7743 whether to request confirmation before creating a new buffer.
7744 See `read-buffer' for features related to input and completion
7745 of buffer names.
7747 If BUFFER-OR-NAME is a string and does not identify an existing
7748 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
7749 nil, switch to the buffer returned by `other-buffer'.
7751 Optional second argument NORECORD non-nil means do not put this
7752 buffer at the front of the list of recently selected ones.
7754 This uses the function `display-buffer' as a subroutine; see its
7755 documentation for additional customization information."
7756 (interactive
7757 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
7758 (let ((pop-up-windows t))
7759 (pop-to-buffer buffer-or-name t norecord)))
7761 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
7762 "Switch to buffer BUFFER-OR-NAME in another frame.
7763 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
7764 nil. Return the buffer switched to.
7766 If called interactively, read the buffer name using `read-buffer'.
7767 The variable `confirm-nonexistent-file-or-buffer' determines
7768 whether to request confirmation before creating a new buffer.
7769 See `read-buffer' for features related to input and completion
7770 of buffer names.
7772 If BUFFER-OR-NAME is a string and does not identify an existing
7773 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
7774 nil, switch to the buffer returned by `other-buffer'.
7776 Optional second arg NORECORD non-nil means do not put this
7777 buffer at the front of the list of recently selected ones.
7779 This uses the function `display-buffer' as a subroutine; see its
7780 documentation for additional customization information."
7781 (interactive
7782 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
7783 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
7785 (defun set-window-text-height (window height)
7786 "Set the height in lines of the text display area of WINDOW to HEIGHT.
7787 WINDOW must be a live window and defaults to the selected one.
7788 HEIGHT doesn't include the mode line or header line, if any, or
7789 any partial-height lines in the text display area.
7791 Note that the current implementation of this function cannot
7792 always set the height exactly, but attempts to be conservative,
7793 by allocating more lines than are actually needed in the case
7794 where some error may be present."
7795 (setq window (window-normalize-window window t))
7796 (let ((delta (- height (window-text-height window))))
7797 (unless (zerop delta)
7798 ;; Setting window-min-height to a value like 1 can lead to very
7799 ;; bizarre displays because it also allows Emacs to make *other*
7800 ;; windows one line tall, which means that there's no more space
7801 ;; for the mode line.
7802 (let ((window-min-height (min 2 height)))
7803 (window-resize window delta)))))
7805 (defun enlarge-window-horizontally (delta)
7806 "Make selected window DELTA columns wider.
7807 Interactively, if no argument is given, make selected window one
7808 column wider."
7809 (interactive "p")
7810 (enlarge-window delta t))
7812 (defun shrink-window-horizontally (delta)
7813 "Make selected window DELTA columns narrower.
7814 Interactively, if no argument is given, make selected window one
7815 column narrower."
7816 (interactive "p")
7817 (shrink-window delta t))
7819 (defun count-screen-lines (&optional beg end count-final-newline window)
7820 "Return the number of screen lines in the region.
7821 The number of screen lines may be different from the number of actual lines,
7822 due to line breaking, display table, etc.
7824 Optional arguments BEG and END default to `point-min' and `point-max'
7825 respectively.
7827 If region ends with a newline, ignore it unless optional third argument
7828 COUNT-FINAL-NEWLINE is non-nil.
7830 The optional fourth argument WINDOW specifies the window used for obtaining
7831 parameters such as width, horizontal scrolling, and so on. The default is
7832 to use the selected window's parameters.
7834 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
7835 regardless of which buffer is displayed in WINDOW. This makes possible to use
7836 `count-screen-lines' in any buffer, whether or not it is currently displayed
7837 in some window."
7838 (unless beg
7839 (setq beg (point-min)))
7840 (unless end
7841 (setq end (point-max)))
7842 (if (= beg end)
7844 (save-excursion
7845 (save-restriction
7846 (widen)
7847 (narrow-to-region (min beg end)
7848 (if (and (not count-final-newline)
7849 (= ?\n (char-before (max beg end))))
7850 (1- (max beg end))
7851 (max beg end)))
7852 (goto-char (point-min))
7853 (1+ (vertical-motion (buffer-size) window))))))
7855 (defun window-buffer-height (window)
7856 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
7857 WINDOW must be a live window and defaults to the selected one."
7858 (setq window (window-normalize-window window t))
7859 (with-current-buffer (window-buffer window)
7860 (max 1
7861 (count-screen-lines (point-min) (point-max)
7862 ;; If buffer ends with a newline, ignore it when
7863 ;; counting height unless point is after it.
7864 (eobp)
7865 window))))
7867 ;;; Resizing windows and frames to fit their contents exactly.
7868 (defcustom fit-window-to-buffer-horizontally nil
7869 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
7870 If this is nil, `fit-window-to-buffer' never resizes windows
7871 horizontally. If this is `only', it can resize windows
7872 horizontally only. Any other value means `fit-window-to-buffer'
7873 can resize windows in both dimensions."
7874 :type 'boolean
7875 :version "24.4"
7876 :group 'help)
7878 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
7879 ;; counting title bar and outer borders.
7880 (defcustom fit-frame-to-buffer nil
7881 "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
7882 A frame is fit if and only if its root window is a live window
7883 and this option is non-nil. If this is `horizontally', frames
7884 are resized horizontally only. If this is `vertically', frames
7885 are resized vertically only. Any other non-nil value means
7886 frames can be resized in both dimensions."
7887 :type 'boolean
7888 :version "24.4"
7889 :group 'help)
7891 (defcustom fit-frame-to-buffer-margins '(nil nil nil nil)
7892 "Margins around frame for `fit-frame-to-buffer'.
7893 This specifies the numbers of pixels to be left free on the left,
7894 above, on the right, and below a frame fitted to its buffer. Set
7895 this to avoid obscuring other desktop objects like the taskbar.
7896 The default is nil for each side, which means to not add margins.
7898 The value specified here can be overridden for a specific frame
7899 by that frame's `fit-frame-to-buffer-margins' parameter, if
7900 present. See also `fit-frame-to-buffer-sizes'."
7901 :version "24.4"
7902 :type '(list
7903 (choice
7904 :tag "Left"
7905 :value nil
7906 :format "%[LeftMargin%] %v "
7907 (const :tag "None" :format "%t" nil)
7908 (integer :tag "Pixels" :size 5))
7909 (choice
7910 :tag "Top"
7911 :value nil
7912 :format "%[TopMargin%] %v "
7913 (const :tag "None" :format "%t" nil)
7914 (integer :tag "Pixels" :size 5))
7915 (choice
7916 :tag "Right"
7917 :value nil
7918 :format "%[RightMargin%] %v "
7919 (const :tag "None" :format "%t" nil)
7920 (integer :tag "Pixels" :size 5))
7921 (choice
7922 :tag "Bottom"
7923 :value nil
7924 :format "%[BottomMargin%] %v "
7925 (const :tag "None" :format "%t" nil)
7926 (integer :tag "Pixels" :size 5)))
7927 :group 'help)
7929 (defcustom fit-frame-to-buffer-sizes '(nil nil nil nil)
7930 "Size boundaries of frame for `fit-frame-to-buffer'.
7931 This list specifies the total maximum and minimum lines and
7932 maximum and minimum columns of the root window of any frame that
7933 shall be fit to its buffer. If any of these values is non-nil,
7934 it overrides the corresponding argument of `fit-frame-to-buffer'.
7936 On window systems where the menubar can wrap, fitting a frame to
7937 its buffer may swallow the last line(s). Specifying an
7938 appropriate minimum width value here can avoid such wrapping.
7940 See also `fit-frame-to-buffer-margins'."
7941 :version "24.4"
7942 :type '(list
7943 (choice
7944 :tag "Maximum Height"
7945 :value nil
7946 :format "%[MaxHeight%] %v "
7947 (const :tag "None" :format "%t" nil)
7948 (integer :tag "Lines" :size 5))
7949 (choice
7950 :tag "Minimum Height"
7951 :value nil
7952 :format "%[MinHeight%] %v "
7953 (const :tag "None" :format "%t" nil)
7954 (integer :tag "Lines" :size 5))
7955 (choice
7956 :tag "Maximum Width"
7957 :value nil
7958 :format "%[MaxWidth%] %v "
7959 (const :tag "None" :format "%t" nil)
7960 (integer :tag "Columns" :size 5))
7961 (choice
7962 :tag "Minimum Width"
7963 :value nil
7964 :format "%[MinWidth%] %v\n"
7965 (const :tag "None" :format "%t" nil)
7966 (integer :tag "Columns" :size 5)))
7967 :group 'help)
7969 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
7971 (defun window--sanitize-margin (margin left right)
7972 "Return MARGIN if it's a number between LEFT and RIGHT.
7973 Return 0 otherwise."
7974 (if (and (numberp margin)
7975 (<= left (- right margin)) (<= margin right))
7976 margin
7979 (declare-function tool-bar-height "xdisp.c" (&optional frame pixelwise))
7981 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only)
7982 "Adjust size of FRAME to display the contents of its buffer exactly.
7983 FRAME can be any live frame and defaults to the selected one.
7984 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
7985 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
7986 FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
7987 `window-min-height' and `window-min-width' respectively.
7989 If the optional argument ONLY is `vertically', resize the frame
7990 vertically only. If ONLY is `horizontally', resize the frame
7991 horizontally only.
7993 The new position and size of FRAME can be additionally determined
7994 by customizing the options `fit-frame-to-buffer-sizes' and
7995 `fit-frame-to-buffer-margins' or setting the corresponding
7996 parameters of FRAME."
7997 (interactive)
7998 (unless (fboundp 'display-monitor-attributes-list)
7999 (user-error "Cannot resize frame in non-graphic Emacs"))
8000 (setq frame (window-normalize-frame frame))
8001 (when (window-live-p (frame-root-window frame))
8002 (let* ((char-width (frame-char-width frame))
8003 (char-height (frame-char-height frame))
8004 ;; WINDOW is FRAME's root window.
8005 (window (frame-root-window frame))
8006 (parent (frame-parent frame))
8007 (monitor-attributes
8008 (unless parent
8009 (car (display-monitor-attributes-list
8010 (frame-parameter frame 'display)))))
8011 ;; FRAME'S parent or display sizes. Used in connection
8012 ;; with margins.
8013 (geometry
8014 (unless parent
8015 (cdr (assq 'geometry monitor-attributes))))
8016 (parent-or-display-width
8017 (if parent
8018 (frame-native-width parent)
8019 (- (nth 2 geometry) (nth 0 geometry))))
8020 (parent-or-display-height
8021 (if parent
8022 (frame-native-height parent)
8023 (- (nth 3 geometry) (nth 1 geometry))))
8024 ;; FRAME'S parent or workarea sizes. Used when no margins
8025 ;; are specified.
8026 (parent-or-workarea
8027 (if parent
8028 `(0 0 ,parent-or-display-width ,parent-or-display-height)
8029 (cdr (assq 'workarea monitor-attributes))))
8030 ;; The outer size of FRAME. Needed to calculate the
8031 ;; margins around the root window's body that have to
8032 ;; remain untouched by fitting.
8033 (outer-edges (frame-edges frame 'outer-edges))
8034 (outer-width (if outer-edges
8035 (- (nth 2 outer-edges) (nth 0 outer-edges))
8036 ;; A poor guess.
8037 (frame-pixel-width frame)))
8038 (outer-height (if outer-edges
8039 (- (nth 3 outer-edges) (nth 1 outer-edges))
8040 ;; Another poor guess.
8041 (frame-pixel-height frame)))
8042 ;; The text size of FRAME. Needed to specify FRAME's
8043 ;; text size after the root window's body's new sizes have
8044 ;; been calculated.
8045 (text-width (frame-text-width frame))
8046 (text-height (frame-text-height frame))
8047 ;; WINDOW's body size.
8048 (body-width (window-body-width window t))
8049 (body-height (window-body-height window t))
8050 ;; The difference between FRAME's outer size and WINDOW's
8051 ;; body size.
8052 (outer-minus-body-width (- outer-width body-width))
8053 (outer-minus-body-height (- outer-height body-height))
8054 ;; The difference between FRAME's text size and WINDOW's
8055 ;; body size (these values "should" be positive).
8056 (text-minus-body-width (- text-width body-width))
8057 (text-minus-body-height (- text-height body-height))
8058 ;; The current position of FRAME.
8059 (position (frame-position frame))
8060 (left (car position))
8061 (top (cdr position))
8062 ;; The margins specified for FRAME. These represent pixel
8063 ;; offsets from the left, top, right and bottom edge of the
8064 ;; display or FRAME's parent's native rectangle and have to
8065 ;; take care of the display's taskbar and other obstacles.
8066 ;; If they are unspecified, constrain the resulting frame
8067 ;; to its workarea or the parent frame's native rectangle.
8068 (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins)
8069 fit-frame-to-buffer-margins))
8070 ;; Convert margins into pixel offsets from the left-top
8071 ;; corner of FRAME's display or parent.
8072 (left-margin (if (nth 0 margins)
8073 (window--sanitize-margin
8074 (nth 0 margins) 0 parent-or-display-width)
8075 (nth 0 parent-or-workarea)))
8076 (top-margin (if (nth 1 margins)
8077 (window--sanitize-margin
8078 (nth 1 margins) 0 parent-or-display-height)
8079 (nth 1 parent-or-workarea)))
8080 (right-margin (if (nth 2 margins)
8081 (- parent-or-display-width
8082 (window--sanitize-margin
8083 (nth 2 margins) left-margin
8084 parent-or-display-width))
8085 (nth 2 parent-or-workarea)))
8086 (bottom-margin (if (nth 3 margins)
8087 (- parent-or-display-height
8088 (window--sanitize-margin
8089 (nth 3 margins) top-margin
8090 parent-or-display-height))
8091 (nth 3 parent-or-workarea)))
8092 ;; Minimum and maximum sizes specified for FRAME.
8093 (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes)
8094 fit-frame-to-buffer-sizes))
8095 ;; Calculate the minimum and maximum pixel sizes of FRAME
8096 ;; from the values provided by the MAX-HEIGHT, MIN-HEIGHT,
8097 ;; MAX-WIDTH and MIN-WIDTH arguments or, if these are nil,
8098 ;; from those provided by `fit-frame-to-buffer-sizes'.
8099 (max-height
8100 (min
8101 (cond
8102 ((numberp max-height) (* max-height char-height))
8103 ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height))
8104 (t parent-or-display-height))
8105 ;; The following is the maximum height that fits into the
8106 ;; top and bottom margins.
8107 (max (- bottom-margin top-margin outer-minus-body-height))))
8108 (min-height
8109 (cond
8110 ((numberp min-height) (* min-height char-height))
8111 ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height))
8112 (t (window-min-size window nil nil t))))
8113 (max-width
8114 (min
8115 (cond
8116 ((numberp max-width) (* max-width char-width))
8117 ((numberp (nth 2 sizes)) (* (nth 2 sizes) char-width))
8118 (t parent-or-display-width))
8119 ;; The following is the maximum width that fits into the
8120 ;; left and right margins.
8121 (max (- right-margin left-margin outer-minus-body-width))))
8122 (min-width
8123 (cond
8124 ((numberp min-width) (* min-width char-width))
8125 ((numberp (nth 3 sizes)) (nth 3 sizes))
8126 (t (window-min-size window t nil t))))
8127 ;; Note: Currently, for a new frame the sizes of the header
8128 ;; and mode line may be estimated incorrectly
8129 (size
8130 (window-text-pixel-size window t t max-width max-height))
8131 (width (max (car size) min-width))
8132 (height (max (cdr size) min-height)))
8133 ;; Don't change height or width when the window's size is fixed
8134 ;; in either direction or ONLY forbids it.
8135 (cond
8136 ((or (eq window-size-fixed 'width) (eq only 'vertically))
8137 (setq width nil))
8138 ((or (eq window-size-fixed 'height) (eq only 'horizontally))
8139 (setq height nil)))
8140 ;; Fit width to constraints.
8141 (when width
8142 (unless frame-resize-pixelwise
8143 ;; Round to character sizes.
8144 (setq width (* (/ (+ width char-width -1) char-width)
8145 char-width)))
8146 ;; The new outer width (in pixels).
8147 (setq outer-width (+ width outer-minus-body-width))
8148 ;; Maybe move FRAME to preserve margins.
8149 (let ((right (+ left outer-width)))
8150 (cond
8151 ((> right right-margin)
8152 ;; Move frame to left.
8153 (setq left (max left-margin (- left (- right right-margin)))))
8154 ((< left left-margin)
8155 ;; Move frame to right.
8156 (setq left left-margin)))))
8157 ;; Fit height to constraints.
8158 (when height
8159 (unless frame-resize-pixelwise
8160 (setq height (* (/ (+ height char-height -1) char-height)
8161 char-height)))
8162 ;; The new outer height.
8163 (setq outer-height (+ height outer-minus-body-height))
8164 ;; Preserve margins.
8165 (let ((bottom (+ top outer-height)))
8166 (cond
8167 ((> bottom bottom-margin)
8168 ;; Move frame up.
8169 (setq top (max top-margin (- top (- bottom bottom-margin)))))
8170 ((< top top-margin)
8171 ;; Move frame down.
8172 (setq top top-margin)))))
8173 ;; Apply our changes.
8174 (setq text-width
8175 (if width
8176 (+ width text-minus-body-width)
8177 (frame-text-width frame)))
8178 (setq text-height
8179 (if height
8180 (+ height text-minus-body-height)
8181 (frame-text-height frame)))
8182 (modify-frame-parameters
8183 frame `((left . ,left) (top . ,top)
8184 (width . (text-pixels . ,text-width))
8185 (height . (text-pixels . ,text-height)))))))
8187 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width preserve-size)
8188 "Adjust size of WINDOW to display its buffer's contents exactly.
8189 WINDOW must be a live window and defaults to the selected one.
8191 If WINDOW is part of a vertical combination, adjust WINDOW's
8192 height. The new height is calculated from the actual height of
8193 the accessible portion of its buffer. The optional argument
8194 MAX-HEIGHT specifies a maximum height and defaults to the height
8195 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
8196 minimum height and defaults to `window-min-height'. Both
8197 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
8198 and header line and a bottom divider, if any.
8200 If WINDOW is part of a horizontal combination and the value of
8201 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
8202 WINDOW's width. The new width of WINDOW is calculated from the
8203 maximum length of its buffer's lines that follow the current
8204 start position of WINDOW. The optional argument MAX-WIDTH
8205 specifies a maximum width and defaults to the width of WINDOW's
8206 frame. The optional argument MIN-WIDTH specifies a minimum width
8207 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
8208 are specified in columns and include fringes, margins, a
8209 scrollbar and a vertical divider, if any.
8211 If the optional argument `preserve-size' is non-nil, preserve the
8212 size of WINDOW (see `window-preserve-size').
8214 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
8215 If WINDOW is its frame's root window and the option
8216 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
8217 adjust the frame's size.
8219 Note that even if this function makes WINDOW large enough to show
8220 _all_ parts of its buffer you might not see the first part when
8221 WINDOW was scrolled. If WINDOW is resized horizontally, you will
8222 not see the top of its buffer unless WINDOW starts at its minimum
8223 accessible position."
8224 (interactive)
8225 (setq window (window-normalize-window window t))
8226 (if (eq window (frame-root-window window))
8227 (when fit-frame-to-buffer
8228 ;; Fit WINDOW's frame to buffer.
8229 (fit-frame-to-buffer
8230 (window-frame window)
8231 max-height min-height max-width min-width
8232 (and (memq fit-frame-to-buffer '(vertically horizontally))
8233 fit-frame-to-buffer)))
8234 (with-selected-window window
8235 (let* ((pixelwise window-resize-pixelwise)
8236 (char-height (frame-char-height))
8237 (char-width (frame-char-width))
8238 (total-height (window-size window nil pixelwise))
8239 (body-height (window-body-height window pixelwise))
8240 (body-width (window-body-width window pixelwise))
8241 (min-height
8242 ;; Sanitize MIN-HEIGHT.
8243 (if (numberp min-height)
8244 ;; Can't get smaller than `window-safe-min-height'.
8245 (max (if pixelwise
8246 (* char-height min-height)
8247 min-height)
8248 (if pixelwise
8249 (window-safe-min-pixel-height window)
8250 window-safe-min-height))
8251 ;; Preserve header and mode line if present.
8252 (max (if pixelwise
8253 (* char-height window-min-height)
8254 window-min-height)
8255 (window-min-size window nil window pixelwise))))
8256 (max-height
8257 ;; Sanitize MAX-HEIGHT.
8258 (if (numberp max-height)
8259 (min
8260 (+ total-height
8261 (window-max-delta
8262 window nil window nil t nil pixelwise))
8263 (if pixelwise
8264 (* char-height max-height)
8265 max-height))
8266 (+ total-height (window-max-delta
8267 window nil window nil t nil pixelwise))))
8268 height)
8269 (cond
8270 ;; If WINDOW is vertically combined, try to resize it
8271 ;; vertically.
8272 ((and (not (eq fit-window-to-buffer-horizontally 'only))
8273 (not (window-size-fixed-p window 'preserved))
8274 (window-combined-p))
8275 ;; Vertically we always want to fit the entire buffer.
8276 ;; WINDOW'S height can't get larger than its frame's pixel
8277 ;; height. Its width remains fixed.
8278 (setq height (+ (cdr (window-text-pixel-size
8279 nil nil t nil (frame-pixel-height) t))
8280 (window-scroll-bar-height window)
8281 (window-bottom-divider-width)))
8282 ;; Round height.
8283 (unless pixelwise
8284 (setq height (/ (+ height char-height -1) char-height)))
8285 (unless (= height total-height)
8286 (window-preserve-size window)
8287 (window-resize-no-error
8288 window
8289 (- (max min-height (min max-height height)) total-height)
8290 nil window pixelwise)
8291 (when preserve-size
8292 (window-preserve-size window nil t))))
8293 ;; If WINDOW is horizontally combined, try to resize it
8294 ;; horizontally.
8295 ((and fit-window-to-buffer-horizontally
8296 (not (window-size-fixed-p window t 'preserved))
8297 (window-combined-p nil t))
8298 (let* ((total-width (window-size window t pixelwise))
8299 (min-width
8300 ;; Sanitize MIN-WIDTH.
8301 (if (numberp min-width)
8302 ;; Can't get smaller than `window-safe-min-width'.
8303 (max (if pixelwise
8304 (* char-width min-width)
8305 min-width)
8306 (if pixelwise
8307 (window-safe-min-pixel-width)
8308 window-safe-min-width))
8309 ;; Preserve fringes, margins, scrollbars if present.
8310 (max (if pixelwise
8311 (* char-width window-min-width)
8312 window-min-width)
8313 (window-min-size nil nil window pixelwise))))
8314 (max-width
8315 ;; Sanitize MAX-WIDTH.
8316 (if (numberp max-width)
8317 (min (+ total-width
8318 (window-max-delta
8319 window t window nil t nil pixelwise))
8320 (if pixelwise
8321 (* char-width max-width)
8322 max-width))
8323 (+ total-width (window-max-delta
8324 window t window nil t nil pixelwise))))
8325 ;; When fitting horizontally, assume that WINDOW's
8326 ;; start position remains unaltered. WINDOW can't get
8327 ;; wider than its frame's pixel width, its height
8328 ;; remains unaltered.
8329 (width (+ (car (window-text-pixel-size
8330 nil (window-start) (point-max)
8331 (frame-pixel-width)
8332 ;; Add one char-height to assure that
8333 ;; we're on the safe side. This
8334 ;; overshoots when the first line below
8335 ;; the bottom is wider than the window.
8336 (* body-height
8337 (if pixelwise 1 char-height))))
8338 (window-right-divider-width))))
8339 (unless pixelwise
8340 (setq width (/ (+ width char-width -1) char-width)))
8341 (unless (= width body-width)
8342 (window-preserve-size window t)
8343 (window-resize-no-error
8344 window
8345 (- (max min-width
8346 (min max-width
8347 (+ total-width (- width body-width))))
8348 total-width)
8349 t window pixelwise)
8350 (when preserve-size
8351 (window-preserve-size window t t))))))))))
8353 (defun window-safely-shrinkable-p (&optional window)
8354 "Return t if WINDOW can be shrunk without shrinking other windows.
8355 WINDOW defaults to the selected window."
8356 (with-selected-window (or window (selected-window))
8357 (let ((edges (window-edges)))
8358 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
8359 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
8361 (defun shrink-window-if-larger-than-buffer (&optional window)
8362 "Shrink height of WINDOW if its buffer doesn't need so many lines.
8363 More precisely, shrink WINDOW vertically to be as small as
8364 possible, while still showing the full contents of its buffer.
8365 WINDOW must be a live window and defaults to the selected one.
8367 Do not shrink WINDOW to less than `window-min-height' lines. Do
8368 nothing if the buffer contains more lines than the present window
8369 height, or if some of the window's contents are scrolled out of
8370 view, or if shrinking this window would also shrink another
8371 window, or if the window is the only window of its frame.
8373 Return non-nil if the window was shrunk, nil otherwise."
8374 (interactive)
8375 (setq window (window-normalize-window window t))
8376 ;; Make sure that WINDOW is vertically combined and `point-min' is
8377 ;; visible (for whatever reason that's needed). The remaining issues
8378 ;; should be taken care of by `fit-window-to-buffer'.
8379 (when (and (window-combined-p window)
8380 (pos-visible-in-window-p (point-min) window))
8381 (fit-window-to-buffer window (window-total-height window))))
8383 (defun window-largest-empty-rectangle--maximums-1 (quad maximums)
8384 "Support function for `window-largest-empty-rectangle'."
8385 (cond
8386 ((null maximums)
8387 (list quad))
8388 ((> (car quad) (caar maximums))
8389 (cons quad maximums))
8391 (cons (car maximums)
8392 (window-largest-empty-rectangle--maximums-1 quad (cdr maximums))))))
8394 (defun window-largest-empty-rectangle--maximums (quad maximums count)
8395 "Support function for `window-largest-empty-rectangle'."
8396 (setq maximums (window-largest-empty-rectangle--maximums-1 quad maximums))
8397 (if (> (length maximums) count)
8398 (nbutlast maximums)
8399 maximums))
8401 (defun window-largest-empty-rectangle--disjoint-maximums (maximums count)
8402 "Support function for `window-largest-empty-rectangle'."
8403 (setq maximums (sort maximums (lambda (x y) (> (car x) (car y)))))
8404 (let ((new-length 0)
8405 new-maximums)
8406 (while (and maximums (< new-length count))
8407 (let* ((maximum (car maximums))
8408 (at (nth 2 maximum))
8409 (to (nth 3 maximum)))
8410 (catch 'drop
8411 (dolist (new-maximum new-maximums)
8412 (let ((new-at (nth 2 new-maximum))
8413 (new-to (nth 3 new-maximum)))
8414 (when (if (< at new-at) (> to new-at) (< at new-to))
8415 ;; Intersection -> drop.
8416 (throw 'drop nil))))
8417 (setq new-maximums (cons maximum new-maximums))
8418 (setq new-length (1+ new-length)))
8419 (setq maximums (cdr maximums))))
8421 (nreverse new-maximums)))
8423 (defun window-largest-empty-rectangle (&optional window count min-width min-height positions left)
8424 "Return dimensions of largest empty rectangle in WINDOW.
8425 WINDOW must be a live window and defaults to the selected one.
8427 The return value is a triple of the width and the start and end
8428 Y-coordinates of the largest rectangle that can be inscribed into
8429 the empty space (the space not displaying any text) of WINDOW's
8430 text area. The return value is nil if the current glyph matrix
8431 of WINDOW is not up-to-date.
8433 Optional argument COUNT, if non-nil, specifies the maximum number
8434 of rectangles to return. This means that the return value is a
8435 list of triples specifying rectangles with the largest rectangle
8436 first. COUNT can be also a cons cell whose car specifies the
8437 number of rectangles to return and whose cdr, if non-nil, states
8438 that all rectangles returned must be disjoint.
8440 Note that the right edge of any rectangle returned by this
8441 function is the right edge of WINDOW (the left edge if its buffer
8442 displays RTL text).
8444 Optional arguments MIN-WIDTH and MIN-HEIGHT, if non-nil, specify
8445 the minimum width and height of any rectangle returned.
8447 Optional argument POSITIONS, if non-nil, is a cons cell whose car
8448 specifies the uppermost and whose cdr specifies the lowermost
8449 pixel position that must be covered by any rectangle returned.
8450 Note that positions are counted from the start of the text area
8451 of WINDOW.
8453 Optional argument LEFT, if non-nil, means to return values suitable for
8454 buffers displaying right to left text."
8455 ;; Process lines as returned by ‘window-lines-pixel-dimensions’.
8456 ;; STACK is a stack that contains rows that have yet to be processed.
8457 (let* ((window (window-normalize-window window t))
8458 (disjoint (and (consp count) (cdr count)))
8459 (count (or (and (numberp count) count)
8460 (and (consp count) (numberp (car count)) (car count))))
8461 (rows (window-lines-pixel-dimensions window nil nil t t left))
8462 (rows-at 0)
8463 (max-size 0)
8464 row stack stack-at stack-to
8465 top top-width top-at top-to top-size
8466 max-width max-at max-to maximums)
8467 ;; ROWS-AT is the position where the first element of ROWS starts.
8468 ;; STACK-AT is the position where the first element of STACK starts.
8469 (while rows
8470 (setq row (car rows))
8471 (if (or (not stack) (>= (car row) (caar stack)))
8472 (progn
8473 (unless stack
8474 (setq stack-at rows-at))
8475 (setq stack (cons row stack))
8476 ;; Set ROWS-AT to where the first element of ROWS ends
8477 ;; which, after popping ROW, makes it the start position of
8478 ;; the next ROW.
8479 (setq rows-at (cdr row))
8480 (setq rows (cdr rows)))
8481 (setq top (car stack))
8482 (setq stack (cdr stack))
8483 (setq top-width (car top))
8484 (setq top-at (if stack (cdar stack) stack-at))
8485 (setq top-to (cdr top))
8486 (setq top-size (* top-width (- top-to top-at)))
8487 (unless (or (and min-width (< top-width min-width))
8488 (and min-height (< (- top-to top-at) min-height))
8489 (and positions
8490 (or (> top-at (car positions))
8491 (< top-to (cdr positions)))))
8492 (if count
8493 (if disjoint
8494 (setq maximums (cons (list top-size top-width top-at top-to)
8495 maximums))
8496 (setq maximums (window-largest-empty-rectangle--maximums
8497 (list top-size top-width top-at top-to)
8498 maximums count)))
8499 (when (> top-size max-size)
8500 (setq max-size top-size)
8501 (setq max-width top-width)
8502 (setq max-at top-at)
8503 (setq max-to top-to))))
8504 (if (and stack (> (caar stack) (car row)))
8505 ;; Have new top element of stack include old top.
8506 (setq stack (cons (cons (caar stack) (cdr top)) (cdr stack)))
8507 ;; Move rows-at backwards to top-at.
8508 (setq rows-at top-at))))
8510 (when stack
8511 ;; STACK-TO is the position where the stack ends.
8512 (setq stack-to (cdar stack))
8513 (while stack
8514 (setq top (car stack))
8515 (setq stack (cdr stack))
8516 (setq top-width (car top))
8517 (setq top-at (if stack (cdar stack) stack-at))
8518 (setq top-size (* top-width (- stack-to top-at)))
8519 (unless (or (and min-width (< top-width min-width))
8520 (and min-height (< (- stack-to top-at) min-height))
8521 (and positions
8522 (or (> top-at (car positions))
8523 (< stack-to (cdr positions)))))
8524 (if count
8525 (if disjoint
8526 (setq maximums (cons (list top-size top-width top-at stack-to)
8527 maximums))
8528 (setq maximums (window-largest-empty-rectangle--maximums
8529 (list top-size top-width top-at stack-to)
8530 maximums count)))
8531 (when (> top-size max-size)
8532 (setq max-size top-size)
8533 (setq max-width top-width)
8534 (setq max-at top-at)
8535 (setq max-to stack-to))))))
8537 (cond
8538 (maximums
8539 (if disjoint
8540 (window-largest-empty-rectangle--disjoint-maximums maximums count)
8541 maximums))
8542 ((> max-size 0)
8543 (list max-width max-at max-to)))))
8545 (defun kill-buffer-and-window ()
8546 "Kill the current buffer and delete the selected window."
8547 (interactive)
8548 (let ((window-to-delete (selected-window))
8549 (buffer-to-kill (current-buffer))
8550 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
8551 (unwind-protect
8552 (progn
8553 (add-hook 'kill-buffer-hook delete-window-hook t t)
8554 (if (kill-buffer (current-buffer))
8555 ;; If `delete-window' failed before, we rerun it to regenerate
8556 ;; the error so it can be seen in the echo area.
8557 (when (eq (selected-window) window-to-delete)
8558 (delete-window))))
8559 ;; If the buffer is not dead for some reason (probably because
8560 ;; of a `quit' signal), remove the hook again.
8561 (ignore-errors
8562 (with-current-buffer buffer-to-kill
8563 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
8567 ;; Groups of windows (Follow Mode).
8569 ;; This section of functions extends the functionality of some window
8570 ;; manipulating commands to groups of windows cooperatively
8571 ;; displaying a buffer, typically with Follow Mode.
8573 ;; The xxx-function variables are permanent locals so that their local
8574 ;; status is undone only when explicitly programmed, not when a buffer
8575 ;; is reverted or a mode function is called.
8577 (defvar window-group-start-function nil)
8578 (make-variable-buffer-local 'window-group-start-function)
8579 (put 'window-group-start-function 'permanent-local t)
8580 (defun window-group-start (&optional window)
8581 "Return position at which display currently starts in the group of
8582 windows containing WINDOW. When a grouping mode (such as Follow Mode)
8583 is not active, this function is identical to `window-start'.
8585 WINDOW must be a live window and defaults to the selected one.
8586 This is updated by redisplay or by calling `set-window*-start'."
8587 (if (functionp window-group-start-function)
8588 (funcall window-group-start-function window)
8589 (window-start window)))
8591 (defvar window-group-end-function nil)
8592 (make-variable-buffer-local 'window-group-end-function)
8593 (put 'window-group-end-function 'permanent-local t)
8594 (defun window-group-end (&optional window update)
8595 "Return position at which display currently ends in the group of
8596 windows containing WINDOW. When a grouping mode (such as Follow Mode)
8597 is not active, this function is identical to `window-end'.
8599 WINDOW must be a live window and defaults to the selected one.
8600 This is updated by redisplay, when it runs to completion.
8601 Simply changing the buffer text or setting `window-group-start'
8602 does not update this value.
8603 Return nil if there is no recorded value. (This can happen if the
8604 last redisplay of WINDOW was preempted, and did not finish.)
8605 If UPDATE is non-nil, compute the up-to-date position
8606 if it isn't already recorded."
8607 (if (functionp window-group-end-function)
8608 (funcall window-group-end-function window update)
8609 (window-end window update)))
8611 (defvar set-window-group-start-function nil)
8612 (make-variable-buffer-local 'set-window-group-start-function)
8613 (put 'set-window-group-start-function 'permanent-local t)
8614 (defun set-window-group-start (window pos &optional noforce)
8615 "Make display in the group of windows containing WINDOW start at
8616 position POS in WINDOW's buffer. When a grouping mode (such as Follow
8617 Mode) is not active, this function is identical to `set-window-start'.
8619 WINDOW must be a live window and defaults to the selected one. Return
8620 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
8621 overriding motion of point in order to display at this exact start."
8622 (if (functionp set-window-group-start-function)
8623 (funcall set-window-group-start-function window pos noforce)
8624 (set-window-start window pos noforce)))
8626 (defvar recenter-window-group-function nil)
8627 (make-variable-buffer-local 'recenter-window-group-function)
8628 (put 'recenter-window-group-function 'permanent-local t)
8629 (defun recenter-window-group (&optional arg)
8630 "Center point in the group of windows containing the selected window
8631 and maybe redisplay frame. When a grouping mode (such as Follow Mode)
8632 is not active, this function is identical to `recenter'.
8634 With a numeric prefix argument ARG, recenter putting point on screen line ARG
8635 relative to the first window in the selected window group. If ARG is
8636 negative, it counts up from the bottom of the last window in the
8637 group. (ARG should be less than the total height of the window group.)
8639 If ARG is omitted or nil, then recenter with point on the middle line of
8640 the selected window group; if the variable `recenter-redisplay' is
8641 non-nil, also erase the entire frame and redraw it (when
8642 `auto-resize-tool-bars' is set to `grow-only', this resets the
8643 tool-bar's height to the minimum height needed); if
8644 `recenter-redisplay' has the special value `tty', then only tty frames
8645 are redrawn.
8647 Just C-u as prefix means put point in the center of the window
8648 and redisplay normally--don't erase and redraw the frame."
8649 (if (functionp recenter-window-group-function)
8650 (funcall recenter-window-group-function arg)
8651 (recenter arg)))
8653 (defvar pos-visible-in-window-group-p-function nil)
8654 (make-variable-buffer-local 'pos-visible-in-window-group-p-function)
8655 (put 'pos-visible-in-window-group-p-function 'permanent-local t)
8656 (defun pos-visible-in-window-group-p (&optional pos window partially)
8657 "Return non-nil if position POS is currently on the frame in the
8658 window group containing WINDOW. When a grouping mode (such as Follow
8659 Mode) is not active, this function is identical to
8660 `pos-visible-in-window-p'.
8662 WINDOW must be a live window and defaults to the selected one.
8664 Return nil if that position is scrolled vertically out of view. If a
8665 character is only partially visible, nil is returned, unless the
8666 optional argument PARTIALLY is non-nil. If POS is only out of view
8667 because of horizontal scrolling, return non-nil. If POS is t, it
8668 specifies the position of the last visible glyph in the window group.
8669 POS defaults to point in WINDOW; WINDOW defaults to the selected
8670 window.
8672 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
8673 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
8674 where X and Y are the pixel coordinates relative to the top left corner
8675 of the window. The remaining elements are omitted if the character after
8676 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
8677 off-window at the top and bottom of the screen line (\"row\") containing
8678 POS, ROWH is the visible height of that row, and VPOS is the row number
8679 \(zero-based)."
8680 (if (functionp pos-visible-in-window-group-p-function)
8681 (funcall pos-visible-in-window-group-p-function pos window partially)
8682 (pos-visible-in-window-p pos window partially)))
8684 (defvar selected-window-group-function nil)
8685 (make-variable-buffer-local 'selected-window-group-function)
8686 (put 'selected-window-group-function 'permanent-local t)
8687 (defun selected-window-group ()
8688 "Return the list of windows in the group containing the selected window.
8689 When a grouping mode (such as Follow Mode) is not active, the
8690 result is a list containing only the selected window."
8691 (if (functionp selected-window-group-function)
8692 (funcall selected-window-group-function)
8693 (list (selected-window))))
8695 (defvar move-to-window-group-line-function nil)
8696 (make-variable-buffer-local 'move-to-window-group-line-function)
8697 (put 'move-to-window-group-line-function 'permanent-local t)
8698 (defun move-to-window-group-line (arg)
8699 "Position point relative to the current group of windows.
8700 When a grouping mode (such as Follow Mode) is not active, this
8701 function is identical to `move-to-window-line'.
8703 ARG nil means position point at center of the window group.
8704 Else, ARG specifies the vertical position within the window
8705 group; zero means top of first window in the group, negative
8706 means relative to the bottom of the last window in the group."
8707 (if (functionp move-to-window-group-line-function)
8708 (funcall move-to-window-group-line-function arg)
8709 (move-to-window-line arg)))
8712 (defvar recenter-last-op nil
8713 "Indicates the last recenter operation performed.
8714 Possible values: `top', `middle', `bottom', integer or float numbers.
8715 It can also be nil, which means the first value in `recenter-positions'.")
8717 (defcustom recenter-positions '(middle top bottom)
8718 "Cycling order for `recenter-top-bottom'.
8719 A list of elements with possible values `top', `middle', `bottom',
8720 integer or float numbers that define the cycling order for
8721 the command `recenter-top-bottom'.
8723 Top and bottom destinations are `scroll-margin' lines from the true
8724 window top and bottom. Middle redraws the frame and centers point
8725 vertically within the window. Integer number moves current line to
8726 the specified absolute window-line. Float number between 0.0 and 1.0
8727 means the percentage of the screen space from the top. The default
8728 cycling order is middle -> top -> bottom."
8729 :type '(repeat (choice
8730 (const :tag "Top" top)
8731 (const :tag "Middle" middle)
8732 (const :tag "Bottom" bottom)
8733 (integer :tag "Line number")
8734 (float :tag "Percentage")))
8735 :version "23.2"
8736 :group 'windows)
8738 (defun recenter-top-bottom (&optional arg)
8739 "Move current buffer line to the specified window line.
8740 With no prefix argument, successive calls place point according
8741 to the cycling order defined by `recenter-positions'.
8743 A prefix argument is handled like `recenter':
8744 With numeric prefix ARG, move current line to window-line ARG.
8745 With plain `C-u', move current line to window center."
8746 (interactive "P")
8747 (cond
8748 (arg (recenter arg)) ; Always respect ARG.
8750 (setq recenter-last-op
8751 (if (eq this-command last-command)
8752 (car (or (cdr (member recenter-last-op recenter-positions))
8753 recenter-positions))
8754 (car recenter-positions)))
8755 (let ((this-scroll-margin
8756 (min (max 0 scroll-margin)
8757 (truncate (/ (window-body-height) 4.0)))))
8758 (cond ((eq recenter-last-op 'middle)
8759 (recenter))
8760 ((eq recenter-last-op 'top)
8761 (recenter this-scroll-margin))
8762 ((eq recenter-last-op 'bottom)
8763 (recenter (- -1 this-scroll-margin)))
8764 ((integerp recenter-last-op)
8765 (recenter recenter-last-op))
8766 ((floatp recenter-last-op)
8767 (recenter (round (* recenter-last-op (window-height))))))))))
8769 (define-key global-map [?\C-l] 'recenter-top-bottom)
8771 (defun move-to-window-line-top-bottom (&optional arg)
8772 "Position point relative to window.
8774 With a prefix argument ARG, acts like `move-to-window-line'.
8776 With no argument, positions point at center of window.
8777 Successive calls position point at positions defined
8778 by `recenter-positions'."
8779 (interactive "P")
8780 (cond
8781 (arg (move-to-window-line arg)) ; Always respect ARG.
8783 (setq recenter-last-op
8784 (if (eq this-command last-command)
8785 (car (or (cdr (member recenter-last-op recenter-positions))
8786 recenter-positions))
8787 (car recenter-positions)))
8788 (let ((this-scroll-margin
8789 (min (max 0 scroll-margin)
8790 (truncate (/ (window-body-height) 4.0)))))
8791 (cond ((eq recenter-last-op 'middle)
8792 (call-interactively 'move-to-window-line))
8793 ((eq recenter-last-op 'top)
8794 (move-to-window-line this-scroll-margin))
8795 ((eq recenter-last-op 'bottom)
8796 (move-to-window-line (- -1 this-scroll-margin)))
8797 ((integerp recenter-last-op)
8798 (move-to-window-line recenter-last-op))
8799 ((floatp recenter-last-op)
8800 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
8802 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
8804 ;;; Scrolling commands.
8806 ;;; Scrolling commands which do not signal errors at top/bottom
8807 ;;; of buffer at first key-press (instead move to top/bottom
8808 ;;; of buffer).
8810 (defcustom scroll-error-top-bottom nil
8811 "Move point to top/bottom of buffer before signaling a scrolling error.
8812 A value of nil means just signal an error if no more scrolling possible.
8813 A value of t means point moves to the beginning or the end of the buffer
8814 \(depending on scrolling direction) when no more scrolling possible.
8815 When point is already on that position, then signal an error."
8816 :type 'boolean
8817 :group 'windows
8818 :version "24.1")
8820 (defun scroll-up-command (&optional arg)
8821 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
8822 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
8823 scroll window further, move cursor to the bottom line.
8824 When point is already on that position, then signal an error.
8825 A near full screen is `next-screen-context-lines' less than a full screen.
8826 Negative ARG means scroll downward.
8827 If ARG is the atom `-', scroll downward by nearly full screen."
8828 (interactive "^P")
8829 (cond
8830 ((null scroll-error-top-bottom)
8831 (scroll-up arg))
8832 ((eq arg '-)
8833 (scroll-down-command nil))
8834 ((< (prefix-numeric-value arg) 0)
8835 (scroll-down-command (- (prefix-numeric-value arg))))
8836 ((eobp)
8837 (scroll-up arg)) ; signal error
8839 (condition-case nil
8840 (scroll-up arg)
8841 (end-of-buffer
8842 (if arg
8843 ;; When scrolling by ARG lines can't be done,
8844 ;; move by ARG lines instead.
8845 (forward-line arg)
8846 ;; When ARG is nil for full-screen scrolling,
8847 ;; move to the bottom of the buffer.
8848 (goto-char (point-max))))))))
8850 (put 'scroll-up-command 'scroll-command t)
8852 (defun scroll-down-command (&optional arg)
8853 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
8854 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
8855 scroll window further, move cursor to the top line.
8856 When point is already on that position, then signal an error.
8857 A near full screen is `next-screen-context-lines' less than a full screen.
8858 Negative ARG means scroll upward.
8859 If ARG is the atom `-', scroll upward by nearly full screen."
8860 (interactive "^P")
8861 (cond
8862 ((null scroll-error-top-bottom)
8863 (scroll-down arg))
8864 ((eq arg '-)
8865 (scroll-up-command nil))
8866 ((< (prefix-numeric-value arg) 0)
8867 (scroll-up-command (- (prefix-numeric-value arg))))
8868 ((bobp)
8869 (scroll-down arg)) ; signal error
8871 (condition-case nil
8872 (scroll-down arg)
8873 (beginning-of-buffer
8874 (if arg
8875 ;; When scrolling by ARG lines can't be done,
8876 ;; move by ARG lines instead.
8877 (forward-line (- arg))
8878 ;; When ARG is nil for full-screen scrolling,
8879 ;; move to the top of the buffer.
8880 (goto-char (point-min))))))))
8882 (put 'scroll-down-command 'scroll-command t)
8884 ;;; Scrolling commands which scroll a line instead of full screen.
8886 (defun scroll-up-line (&optional arg)
8887 "Scroll text of selected window upward ARG lines; or one line if no ARG.
8888 If ARG is omitted or nil, scroll upward by one line.
8889 This is different from `scroll-up-command' that scrolls a full screen."
8890 (interactive "p")
8891 (scroll-up (or arg 1)))
8893 (put 'scroll-up-line 'scroll-command t)
8895 (defun scroll-down-line (&optional arg)
8896 "Scroll text of selected window down ARG lines; or one line if no ARG.
8897 If ARG is omitted or nil, scroll down by one line.
8898 This is different from `scroll-down-command' that scrolls a full screen."
8899 (interactive "p")
8900 (scroll-down (or arg 1)))
8902 (put 'scroll-down-line 'scroll-command t)
8905 (defun scroll-other-window-down (&optional lines)
8906 "Scroll the \"other window\" down.
8907 For more details, see the documentation for `scroll-other-window'."
8908 (interactive "P")
8909 (scroll-other-window
8910 ;; Just invert the argument's meaning.
8911 ;; We can do that without knowing which window it will be.
8912 (if (eq lines '-) nil
8913 (if (null lines) '-
8914 (- (prefix-numeric-value lines))))))
8916 (defun beginning-of-buffer-other-window (arg)
8917 "Move point to the beginning of the buffer in the other window.
8918 Leave mark at previous position.
8919 With arg N, put point N/10 of the way from the true beginning."
8920 (interactive "P")
8921 (let ((orig-window (selected-window))
8922 (window (other-window-for-scrolling)))
8923 ;; We use unwind-protect rather than save-window-excursion
8924 ;; because the latter would preserve the things we want to change.
8925 (unwind-protect
8926 (progn
8927 (select-window window)
8928 ;; Set point and mark in that window's buffer.
8929 (with-no-warnings
8930 (beginning-of-buffer arg))
8931 ;; Set point accordingly.
8932 (recenter '(t)))
8933 (select-window orig-window))))
8935 (defun end-of-buffer-other-window (arg)
8936 "Move point to the end of the buffer in the other window.
8937 Leave mark at previous position.
8938 With arg N, put point N/10 of the way from the true end."
8939 (interactive "P")
8940 ;; See beginning-of-buffer-other-window for comments.
8941 (let ((orig-window (selected-window))
8942 (window (other-window-for-scrolling)))
8943 (unwind-protect
8944 (progn
8945 (select-window window)
8946 (with-no-warnings
8947 (end-of-buffer arg))
8948 (recenter '(t)))
8949 (select-window orig-window))))
8951 (defvar mouse-autoselect-window-timer nil
8952 "Timer used by delayed window autoselection.")
8954 (defvar mouse-autoselect-window-position-1 nil
8955 "First mouse position recorded by delayed window autoselection.")
8957 (defvar mouse-autoselect-window-position nil
8958 "Last mouse position recorded by delayed window autoselection.")
8960 (defvar mouse-autoselect-window-window nil
8961 "Last window recorded by delayed window autoselection.")
8963 (defvar mouse-autoselect-window-state nil
8964 "When non-nil, special state of delayed window autoselection.
8965 Possible values are `suspend' (suspend autoselection after a menu or
8966 scrollbar interaction) and `select' (the next invocation of
8967 `handle-select-window' shall select the window immediately).")
8969 (defun mouse-autoselect-window-cancel (&optional force)
8970 "Cancel delayed window autoselection.
8971 Optional argument FORCE means cancel unconditionally."
8972 (unless (and (not force)
8973 ;; Don't cancel for select-window or select-frame events
8974 ;; or when the user drags a scroll bar.
8975 (or (memq this-command
8976 '(handle-select-window handle-switch-frame))
8977 (and (eq this-command 'scroll-bar-toolkit-scroll)
8978 (memq (nth 4 (event-end last-input-event))
8979 '(handle end-scroll)))))
8980 (setq mouse-autoselect-window-state nil)
8981 (setq mouse-autoselect-window-position-1 nil)
8982 (when (timerp mouse-autoselect-window-timer)
8983 (cancel-timer mouse-autoselect-window-timer))
8984 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
8986 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
8987 "Start delayed window autoselection.
8988 MOUSE-POSITION is the last position where the mouse was seen as returned
8989 by `mouse-position'. Optional argument WINDOW non-nil denotes the
8990 window where the mouse was seen. Optional argument SUSPEND non-nil
8991 means suspend autoselection."
8992 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
8993 (setq mouse-autoselect-window-position mouse-position)
8994 (when window (setq mouse-autoselect-window-window window))
8995 (setq mouse-autoselect-window-state (when suspend 'suspend))
8996 ;; Install timer which runs `mouse-autoselect-window-select' after
8997 ;; `mouse-autoselect-window' seconds.
8998 (setq mouse-autoselect-window-timer
8999 (run-at-time
9000 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
9002 (defun mouse-autoselect-window-select ()
9003 "Select window with delayed window autoselection.
9004 If the mouse position has stabilized in a non-selected window, select
9005 that window. The minibuffer window is selected only if the minibuffer
9006 is active. This function is run by `mouse-autoselect-window-timer'."
9007 (let* ((mouse-position (mouse-position))
9008 (mouse-x (and (numberp (cadr mouse-position))
9009 (cadr mouse-position)))
9010 (mouse-y (and (numberp (cddr mouse-position))
9011 (cddr mouse-position)))
9012 (frame (and mouse-x mouse-y (car mouse-position)))
9013 (window (and frame (window-at mouse-x mouse-y frame))))
9014 (cond
9015 ((or (and (fboundp 'menu-or-popup-active-p) (menu-or-popup-active-p))
9016 (and window
9017 (let ((coords (coordinates-in-window-p
9018 (cdr mouse-position) window)))
9019 (and (not (consp coords))
9020 (not (memq coords '(left-margin right-margin)))))))
9021 ;; A menu / popup dialog is active or the mouse is not on the
9022 ;; text region of WINDOW: Suspend autoselection temporarily.
9023 (mouse-autoselect-window-start mouse-position nil t))
9024 ((or (eq mouse-autoselect-window-state 'suspend)
9025 ;; When the mouse is at its first recorded position, restart
9026 ;; delayed autoselection. This works around a scenario with
9027 ;; two two-window frames with identical dimensions: select the
9028 ;; first window of the first frame, switch to the second
9029 ;; frame, move the mouse to its second window, minimize the
9030 ;; second frame. Now the second window of the first frame
9031 ;; gets selected although the mouse never really "moved" into
9032 ;; that window.
9033 (and (numberp mouse-autoselect-window)
9034 (equal (mouse-position) mouse-autoselect-window-position-1)))
9035 ;; Delayed autoselection was temporarily suspended, reenable it.
9036 (mouse-autoselect-window-start mouse-position))
9037 ((and window
9038 (or (not (numberp mouse-autoselect-window))
9039 (and (>= mouse-autoselect-window 0)
9040 ;; If `mouse-autoselect-window' is non-negative,
9041 ;; select window if it's the same as before.
9042 (eq window mouse-autoselect-window-window))
9043 ;; Otherwise select window iff the mouse is at the same
9044 ;; position as before. Observe that the first test
9045 ;; after starting autoselection usually fails since the
9046 ;; value of `mouse-autoselect-window-position' recorded
9047 ;; there is the position where the mouse has entered the
9048 ;; new window and not necessarily where the mouse has
9049 ;; stopped moving.
9050 (equal mouse-position mouse-autoselect-window-position))
9051 ;; The minibuffer is a candidate window if it's active.
9052 (or (not (window-minibuffer-p window))
9053 (eq window (active-minibuffer-window))))
9054 ;; Mouse position has stabilized in non-selected window: Cancel
9055 ;; delayed autoselection and try to select that window.
9056 (mouse-autoselect-window-cancel t)
9057 ;; Use `unread-command-events' in order to execute pre- and
9058 ;; post-command hooks and trigger idle timers. To avoid delaying
9059 ;; autoselection again, set `mouse-autoselect-window-state'."
9060 (setq mouse-autoselect-window-state 'select)
9061 (setq unread-command-events
9062 (cons (list 'select-window (list window))
9063 unread-command-events)))
9064 ((or (not (numberp mouse-autoselect-window))
9065 (equal mouse-position mouse-autoselect-window-position))
9066 ;; Mouse position has stabilized at
9067 ;; `mouse-autoselect-window-position': Cancel delayed
9068 ;; autoselection.
9069 (mouse-autoselect-window-cancel t))
9070 (window
9071 ;; Mouse position has not stabilized yet, resume delayed
9072 ;; autoselection.
9073 (mouse-autoselect-window-start mouse-position window)))))
9075 (defun handle-select-window (event)
9076 "Handle select-window events."
9077 (interactive "^e")
9078 (let* ((window (posn-window (event-start event)))
9079 (frame (and (window-live-p window) (window-frame window)))
9080 (old-frame (selected-frame)))
9081 (unless (or (not (window-live-p window))
9082 ;; Don't switch when autoselection shall be delayed.
9083 (and (numberp mouse-autoselect-window)
9084 (not (eq mouse-autoselect-window-state 'select))
9085 (let ((position (mouse-position)))
9086 ;; Cancel any delayed autoselection.
9087 (mouse-autoselect-window-cancel t)
9088 ;; Start delayed autoselection from current mouse
9089 ;; position and window.
9090 (setq mouse-autoselect-window-position-1 position)
9091 (mouse-autoselect-window-start position window)
9092 ;; Executing a command cancels delayed autoselection.
9093 (add-hook
9094 'pre-command-hook 'mouse-autoselect-window-cancel)))
9095 ;; Don't switch to a `no-accept-focus' frame unless it's
9096 ;; already selected.
9097 (and (not (eq frame (selected-frame)))
9098 (frame-parameter frame 'no-accept-focus))
9099 ;; Don't switch to minibuffer window unless it's active.
9100 (and (window-minibuffer-p window)
9101 (not (minibuffer-window-active-p window))))
9102 ;; Reset state of delayed autoselection.
9103 (setq mouse-autoselect-window-state nil)
9104 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
9105 (run-hooks 'mouse-leave-buffer-hook)
9106 ;; Clear echo area.
9107 (message nil)
9108 ;; Select the window before giving the frame focus since otherwise
9109 ;; we might get two windows with an active cursor.
9110 (select-window window)
9111 (cond
9112 ((or (not (memq (window-system frame) '(x w32 ns)))
9113 (not focus-follows-mouse)
9114 ;; Focus FRAME if it's either a child frame or an ancestor
9115 ;; of the frame switched from.
9116 (and (not (frame-parameter frame 'parent-frame))
9117 (not (frame-ancestor-p frame old-frame)))))
9118 ((eq focus-follows-mouse 'auto-raise)
9119 ;; Focus and auto-raise frame.
9120 (x-focus-frame frame)
9121 ;; This doesn't seem to work when we move from a normal frame
9122 ;; right into the child frame of another frame - we should raise
9123 ;; that child frame's ancestor frame first ...
9124 (raise-frame frame))
9126 ;; Just focus frame.
9127 (x-focus-frame frame t))))))
9129 (defun truncated-partial-width-window-p (&optional window)
9130 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
9131 WINDOW must be a live window and defaults to the selected one.
9132 Return nil if WINDOW is not a partial-width window
9133 (regardless of the value of `truncate-lines').
9134 Otherwise, consult the value of `truncate-partial-width-windows'
9135 for the buffer shown in WINDOW."
9136 (setq window (window-normalize-window window t))
9137 (unless (window-full-width-p window)
9138 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
9139 (window-buffer window))))
9140 (if (integerp t-p-w-w)
9141 (< (window-width window) t-p-w-w)
9142 t-p-w-w))))
9145 ;; Automatically inform subprocesses of changes to window size.
9147 (defcustom window-adjust-process-window-size-function
9148 'window-adjust-process-window-size-smallest
9149 "Control how Emacs chooses inferior process window sizes.
9150 Emacs uses this function to tell processes the space they have
9151 available for displaying their output. After each window
9152 configuration change, Emacs calls the value of
9153 `window-adjust-process-window-size-function' for each process
9154 with a buffer being displayed in at least one window.
9155 This function is responsible for combining the sizes of the
9156 displayed windows and returning a cons (WIDTH . HEIGHT)
9157 describing the width and height with which Emacs will call
9158 `set-process-window-size' for that process. If the function
9159 returns nil, Emacs does not call `set-process-window-size'.
9161 This function is called with the process buffer as the current
9162 buffer and with two arguments: the process and a list of windows
9163 displaying process. Modes can make this variable buffer-local;
9164 additionally, the `adjust-window-size-function' process property
9165 overrides the global or buffer-local value of
9166 `window-adjust-process-window-size-function'."
9167 :type '(choice
9168 (const :tag "Minimum area of any window"
9169 window-adjust-process-window-size-smallest)
9170 (const :tag "Maximum area of any window"
9171 window-adjust-process-window-size-largest)
9172 (const :tag "Do not adjust process window sizes" ignore)
9173 function)
9174 :group 'windows
9175 :version "25.1")
9177 (defun window-adjust-process-window-size (reducer windows)
9178 "Adjust the window sizes of a process.
9179 WINDOWS is a list of windows associated with that process. REDUCER is
9180 a two-argument function used to combine the widths and heights of
9181 the given windows."
9182 (when windows
9183 (let ((width (window-max-chars-per-line (car windows)))
9184 (height (window-body-height (car windows))))
9185 (dolist (window (cdr windows))
9186 (setf width (funcall reducer width (window-max-chars-per-line window)))
9187 (setf height (funcall reducer height (window-body-height window))))
9188 (cons width height))))
9190 (defun window-adjust-process-window-size-smallest (_process windows)
9191 "Adjust the process window size of PROCESS.
9192 WINDOWS is a list of windows associated with PROCESS. Choose the
9193 smallest area available for displaying PROCESS's output."
9194 (window-adjust-process-window-size #'min windows))
9196 (defun window-adjust-process-window-size-largest (_process windows)
9197 "Adjust the process window size of PROCESS.
9198 WINDOWS is a list of windows associated with PROCESS. Choose the
9199 largest area available for displaying PROCESS's output."
9200 (window-adjust-process-window-size #'max windows))
9202 (defun window--process-window-list ()
9203 "Return an alist mapping processes to associated windows.
9204 A window is associated with a process if that window is
9205 displaying that processes's buffer."
9206 (let ((processes (process-list))
9207 (process-windows nil))
9208 (if processes
9209 (walk-windows
9210 (lambda (window)
9211 (let ((buffer (window-buffer window))
9212 (iter processes))
9213 (while (let ((process (car iter)))
9214 (if (and (process-live-p process)
9215 (eq buffer (process-buffer process)))
9216 (let ((procwin (assq process process-windows)))
9217 ;; Add this window to the list of windows
9218 ;; displaying process.
9219 (if procwin
9220 (push window (cdr procwin))
9221 (push (list process window) process-windows))
9222 ;; We found our process for this window, so
9223 ;; stop iterating over the process list.
9224 nil)
9225 (setf iter (cdr iter)))))))
9226 1 t))
9227 process-windows))
9229 (defun window--adjust-process-windows ()
9230 "Update process window sizes to match the current window configuration."
9231 (when (fboundp 'process-list)
9232 (dolist (procwin (window--process-window-list))
9233 (let ((process (car procwin)))
9234 (with-demoted-errors "Error adjusting window size: %S"
9235 (with-current-buffer (process-buffer process)
9236 (let ((size (funcall
9237 (or (process-get process 'adjust-window-size-function)
9238 window-adjust-process-window-size-function)
9239 process (cdr procwin))))
9240 (when size
9241 (set-process-window-size process (cdr size) (car size))))))))))
9243 (add-hook 'window-configuration-change-hook 'window--adjust-process-windows)
9246 ;; Some of these are in tutorial--default-keys, so update that if you
9247 ;; change these.
9248 (define-key ctl-x-map "0" 'delete-window)
9249 (define-key ctl-x-map "1" 'delete-other-windows)
9250 (define-key ctl-x-map "2" 'split-window-below)
9251 (define-key ctl-x-map "3" 'split-window-right)
9252 (define-key ctl-x-map "o" 'other-window)
9253 (define-key ctl-x-map "^" 'enlarge-window)
9254 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
9255 (define-key ctl-x-map "{" 'shrink-window-horizontally)
9256 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
9257 (define-key ctl-x-map "+" 'balance-windows)
9258 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
9260 ;;; window.el ends here