Port thread.c to OpenBSD ARM
[emacs.git] / lisp / window.el
blobc0a9ecd093ca56fbe896e6c6b346a18a6e916d47
1 ;;; window.el --- GNU Emacs window commands aside from those written in C -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2017 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: internal
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Window tree functions.
29 ;;; Code:
31 (defun internal--before-save-selected-window ()
32 (cons (selected-window)
33 ;; We save and restore all frames' selected windows, because
34 ;; `select-window' can change the frame-selected-window of
35 ;; whatever frame that window is in. Each text terminal's
36 ;; top-frame is preserved by putting it last in the list.
37 (apply #'append
38 (mapcar (lambda (terminal)
39 (let ((frames (frames-on-display-list terminal))
40 (top-frame (tty-top-frame terminal))
41 alist)
42 (if top-frame
43 (setq frames
44 (cons top-frame
45 (delq top-frame frames))))
46 (dolist (f frames)
47 (push (cons f (frame-selected-window f))
48 alist))
49 alist))
50 (terminal-list)))))
52 (defun internal--after-save-selected-window (state)
53 (dolist (elt (cdr state))
54 (and (frame-live-p (car elt))
55 (window-live-p (cdr elt))
56 (set-frame-selected-window (car elt) (cdr elt) 'norecord)))
57 (when (window-live-p (car state))
58 (select-window (car state) 'norecord)))
60 (defmacro save-selected-window (&rest body)
61 "Execute BODY, then select the previously selected window.
62 The value returned is the value of the last form in BODY.
64 This macro saves and restores the selected window, as well as the
65 selected window in each frame. If the previously selected window
66 is no longer live, then whatever window is selected at the end of
67 BODY remains selected. If the previously selected window of some
68 frame is no longer live at the end of BODY, that frame's selected
69 window is left alone.
71 This macro saves and restores the current buffer, since otherwise
72 its normal operation could make a different buffer current. The
73 order of recently selected windows and the buffer list ordering
74 are not altered by this macro (unless they are altered in BODY)."
75 (declare (indent 0) (debug t))
76 `(let ((save-selected-window--state (internal--before-save-selected-window)))
77 (save-current-buffer
78 (unwind-protect
79 (progn ,@body)
80 (internal--after-save-selected-window save-selected-window--state)))))
82 (defvar temp-buffer-window-setup-hook nil
83 "Normal hook run by `with-temp-buffer-window' before buffer display.
84 This hook is run by `with-temp-buffer-window' with the buffer to be
85 displayed current.")
87 (defvar temp-buffer-window-show-hook nil
88 "Normal hook run by `with-temp-buffer-window' after buffer display.
89 This hook is run by `with-temp-buffer-window' with the buffer
90 displayed and current and its window selected.")
92 (defun temp-buffer-window-setup (buffer-or-name)
93 "Set up temporary buffer specified by BUFFER-OR-NAME.
94 Return the buffer."
95 (let ((old-dir default-directory)
96 (buffer (get-buffer-create buffer-or-name)))
97 (with-current-buffer buffer
98 (kill-all-local-variables)
99 (setq default-directory old-dir)
100 (delete-all-overlays)
101 (setq buffer-read-only nil)
102 (setq buffer-file-name nil)
103 (setq buffer-undo-list t)
104 (let ((inhibit-read-only t)
105 (inhibit-modification-hooks t))
106 (erase-buffer)
107 (run-hooks 'temp-buffer-window-setup-hook))
108 ;; Return the buffer.
109 buffer)))
111 (defun temp-buffer-window-show (buffer &optional action)
112 "Show temporary buffer BUFFER in a window.
113 Return the window showing BUFFER. Pass ACTION as action argument
114 to `display-buffer'."
115 (let (window frame)
116 (with-current-buffer buffer
117 (set-buffer-modified-p nil)
118 (setq buffer-read-only t)
119 (goto-char (point-min))
120 (when (let ((window-combination-limit
121 ;; When `window-combination-limit' equals
122 ;; `temp-buffer' or `temp-buffer-resize' and
123 ;; `temp-buffer-resize-mode' is enabled in this
124 ;; buffer bind it to t so resizing steals space
125 ;; preferably from the window that was split.
126 (if (or (eq window-combination-limit 'temp-buffer)
127 (and (eq window-combination-limit
128 'temp-buffer-resize)
129 temp-buffer-resize-mode))
131 window-combination-limit)))
132 (setq window (display-buffer buffer action)))
133 (setq frame (window-frame window))
134 (unless (eq frame (selected-frame))
135 (raise-frame frame))
136 (setq minibuffer-scroll-window window)
137 (set-window-hscroll window 0)
138 (with-selected-window window
139 (run-hooks 'temp-buffer-window-show-hook)
140 (when temp-buffer-resize-mode
141 (resize-temp-buffer-window window)))
142 ;; Return the window.
143 window))))
145 (defmacro with-temp-buffer-window (buffer-or-name action quit-function &rest body)
146 "Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
147 BUFFER-OR-NAME must specify either a live buffer, or the name of
148 a buffer (if it does not exist, this macro creates it).
150 Make the buffer specified by BUFFER-OR-NAME empty before running
151 BODY and bind `standard-output' to that buffer, so that output
152 generated with `prin1' and similar functions in BODY goes into
153 that buffer. Do not make that buffer current for running the
154 forms in BODY. Use `with-current-buffer-window' instead if you
155 need to run BODY with that buffer current.
157 At the end of BODY, mark the specified buffer unmodified and
158 read-only, and display it in a window (but do not select it).
159 The display happens by calling `display-buffer' passing it the
160 ACTION argument. If `temp-buffer-resize-mode' is enabled, the
161 corresponding window may be resized automatically.
163 Return the value returned by BODY, unless QUIT-FUNCTION specifies
164 a function. In that case, run that function with two arguments -
165 the window showing the specified buffer and the value returned by
166 BODY - and return the value returned by that function.
168 If the buffer is displayed on a new frame, the window manager may
169 decide to select that frame. In that case, it's usually a good
170 strategy if QUIT-FUNCTION selects the window showing the buffer
171 before reading any value from the minibuffer; for example, when
172 asking a `yes-or-no-p' question.
174 This runs the hook `temp-buffer-window-setup-hook' before BODY,
175 with the specified buffer temporarily current. It runs the hook
176 `temp-buffer-window-show-hook' after displaying the buffer, with
177 that buffer temporarily current, and the window that was used to
178 display it temporarily selected.
180 This construct is similar to `with-output-to-temp-buffer' but,
181 neither runs `temp-buffer-setup-hook' which usually puts the
182 buffer in Help mode, nor `temp-buffer-show-function' (the ACTION
183 argument replaces this)."
184 (declare (debug t))
185 (let ((buffer (make-symbol "buffer"))
186 (window (make-symbol "window"))
187 (value (make-symbol "value")))
188 (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
189 (vaction action)
190 (vquit-function quit-function))
191 `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
192 (standard-output ,buffer)
193 ,window ,value)
194 (setq ,value (progn ,@body))
195 (with-current-buffer ,buffer
196 (setq ,window (temp-buffer-window-show ,buffer ,vaction)))
198 (if (functionp ,vquit-function)
199 (funcall ,vquit-function ,window ,value)
200 ,value)))))
202 (defmacro with-current-buffer-window (buffer-or-name action quit-function &rest body)
203 "Evaluate BODY with a buffer BUFFER-OR-NAME current and show that buffer.
204 This construct is like `with-temp-buffer-window' but unlike that
205 makes the buffer specified by BUFFER-OR-NAME current for running
206 BODY."
207 (declare (debug t))
208 (let ((buffer (make-symbol "buffer"))
209 (window (make-symbol "window"))
210 (value (make-symbol "value")))
211 (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
212 (vaction action)
213 (vquit-function quit-function))
214 `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
215 (standard-output ,buffer)
216 ,window ,value)
217 (with-current-buffer ,buffer
218 (setq ,value (progn ,@body))
219 (setq ,window (temp-buffer-window-show ,buffer ,vaction)))
221 (if (functionp ,vquit-function)
222 (funcall ,vquit-function ,window ,value)
223 ,value)))))
225 (defmacro with-displayed-buffer-window (buffer-or-name action quit-function &rest body)
226 "Show a buffer BUFFER-OR-NAME and evaluate BODY in that buffer.
227 This construct is like `with-current-buffer-window' but unlike that
228 displays the buffer specified by BUFFER-OR-NAME before running BODY."
229 (declare (debug t))
230 (let ((buffer (make-symbol "buffer"))
231 (window (make-symbol "window"))
232 (value (make-symbol "value")))
233 (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
234 (vaction action)
235 (vquit-function quit-function))
236 `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
237 (standard-output ,buffer)
238 ;; If a 'window-height' entry specifies a function,
239 ;; remember it here in order to call it below but replace
240 ;; the entry so `window--try-to-split-window' will bind
241 ;; `window-combination-limit' to t and the function does
242 ;; not resize any other window but the one we split this
243 ;; one off (Bug#25055, Bug#25179).
244 (vheight-function
245 (let ((window-height (assq 'window-height (cdr ,vaction))))
246 (when (functionp (cdr window-height))
247 (cdr window-height))))
248 (vaction-copied
249 (when vheight-function
250 (cons (car , vaction)
251 (cons
252 '(window-height . t)
253 (assq-delete-all
254 'window-height (cdr (copy-sequence ,vaction)))))))
255 ,window ,value)
256 (with-current-buffer ,buffer
257 (setq ,window (temp-buffer-window-show
258 ,buffer (or vaction-copied ,vaction))))
260 (let ((inhibit-read-only t)
261 (inhibit-modification-hooks t))
262 (setq ,value (progn ,@body)))
264 (set-window-point ,window (point-min))
266 (when vheight-function
267 (ignore-errors
268 (set-window-parameter ,window 'preserve-size nil)
269 (funcall vheight-function ,window)))
271 (when (consp (cdr (assq 'preserve-size (cdr ,vaction))))
272 (window-preserve-size
273 ,window t (cadr (assq 'preserve-size (cdr ,vaction))))
274 (window-preserve-size
275 ,window nil (cddr (assq 'preserve-size (cdr ,vaction)))))
277 (if (functionp ,vquit-function)
278 (funcall ,vquit-function ,window ,value)
279 ,value)))))
281 ;; The following two functions are like `window-next-sibling' and
282 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
283 ;; they don't substitute the selected window for nil), and they return
284 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
285 ;; a minibuffer window).
286 (defun window-right (window)
287 "Return WINDOW's right sibling.
288 Return nil if WINDOW is the root window of its frame. WINDOW can
289 be any window."
290 (and window (window-parent window) (window-next-sibling window)))
292 (defun window-left (window)
293 "Return WINDOW's left sibling.
294 Return nil if WINDOW is the root window of its frame. WINDOW can
295 be any window."
296 (and window (window-parent window) (window-prev-sibling window)))
298 (defun window-child (window)
299 "Return WINDOW's first child window.
300 WINDOW can be any window."
301 (or (window-top-child window) (window-left-child window)))
303 (defun window-child-count (window)
304 "Return number of WINDOW's child windows.
305 WINDOW can be any window."
306 (let ((count 0))
307 (when (and (windowp window) (setq window (window-child window)))
308 (while window
309 (setq count (1+ count))
310 (setq window (window-next-sibling window))))
311 count))
313 (defun window-last-child (window)
314 "Return last child window of WINDOW.
315 WINDOW can be any window."
316 (when (and (windowp window) (setq window (window-child window)))
317 (while (window-next-sibling window)
318 (setq window (window-next-sibling window))))
319 window)
321 (defun window-normalize-buffer (buffer-or-name)
322 "Return buffer specified by BUFFER-OR-NAME.
323 BUFFER-OR-NAME must be a live buffer, a string naming a live
324 buffer or nil which means to return the current buffer.
326 This function is commonly used to process the (usually optional)
327 \"BUFFER-OR-NAME\" argument of window related functions where nil
328 stands for the current buffer."
329 (let ((buffer
330 (cond
331 ((not buffer-or-name)
332 (current-buffer))
333 ((bufferp buffer-or-name)
334 buffer-or-name)
335 ((stringp buffer-or-name)
336 (get-buffer buffer-or-name))
338 (error "No such buffer %s" buffer-or-name)))))
339 (if (buffer-live-p buffer)
340 buffer
341 (error "No such live buffer %s" buffer-or-name))))
343 (defun window-normalize-frame (frame)
344 "Return frame specified by FRAME.
345 FRAME must be a live frame or nil which means to return the
346 selected frame.
348 This function is commonly used to process the (usually optional)
349 \"FRAME\" argument of window and frame related functions where
350 nil stands for the selected frame."
351 (if frame
352 (if (frame-live-p frame)
353 frame
354 (error "%s is not a live frame" frame))
355 (selected-frame)))
357 (defun window-normalize-window (window &optional live-only)
358 "Return window specified by WINDOW.
359 If WINDOW is nil, return the selected window. Otherwise, if
360 WINDOW is a live or an internal window, return WINDOW; if
361 LIVE-ONLY is non-nil, return WINDOW for a live window only.
362 Otherwise, signal an error.
364 This function is commonly used to process the (usually optional)
365 \"WINDOW\" argument of window related functions where nil stands
366 for the selected window."
367 (cond
368 ((null window)
369 (selected-window))
370 (live-only
371 (if (window-live-p window)
372 window
373 (error "%s is not a live window" window)))
374 ((window-valid-p window)
375 window)
377 (error "%s is not a valid window" window))))
379 ;; Maybe this should go to frame.el.
380 (defun frame-char-size (&optional window-or-frame horizontal)
381 "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
382 If WINDOW-OR-FRAME is a live frame, return the value of
383 `frame-char-height' for that frame. If WINDOW-OR-FRAME is a
384 valid window, return the value of `frame-char-height' for that
385 window's frame. In any other case, return the value of
386 `frame-char-height' for the selected frame.
388 Optional argument HORIZONTAL non-nil means to return the value of
389 `frame-char-width' for WINDOW-OR-FRAME."
390 (let ((frame
391 (cond
392 ((window-valid-p window-or-frame)
393 (window-frame window-or-frame))
394 ((frame-live-p window-or-frame)
395 window-or-frame)
396 (t (selected-frame)))))
397 (if horizontal
398 (frame-char-width frame)
399 (frame-char-height frame))))
401 (defvar ignore-window-parameters nil
402 "If non-nil, standard functions ignore window parameters.
403 The functions currently affected by this are `split-window',
404 `delete-window', `delete-other-windows' and `other-window'.
406 An application may bind this to a non-nil value around calls to
407 these functions to inhibit processing of window parameters.")
409 ;; This must go to C, finally (or get removed).
410 (defconst window-safe-min-height 1
411 "The absolute minimum number of lines of any window.
412 Anything less might crash Emacs.")
414 (defun window-safe-min-pixel-height (&optional window)
415 "Return the absolute minimum pixel height of WINDOW."
416 (* window-safe-min-height
417 (frame-char-size (window-normalize-window window))))
419 (defcustom window-min-height 4
420 "The minimum total height, in lines, of any window.
421 The value has to accommodate one text line, a mode and header
422 line, a horizontal scroll bar and a bottom divider, if present.
423 A value less than `window-safe-min-height' is ignored. The value
424 of this variable is honored when windows are resized or split.
426 Applications should never rebind this variable. To resize a
427 window to a height less than the one specified here, an
428 application should instead call `window-resize' with a non-nil
429 IGNORE argument. In order to have `split-window' make a window
430 shorter, explicitly specify the SIZE argument of that function."
431 :type 'integer
432 :version "24.1"
433 :group 'windows)
435 (defun window-min-pixel-height (&optional window)
436 "Return the minimum pixel height of window WINDOW."
437 (* (max window-min-height window-safe-min-height)
438 (frame-char-size window)))
440 ;; This must go to C, finally (or get removed).
441 (defconst window-safe-min-width 2
442 "The absolute minimum number of columns of a window.
443 Anything less might crash Emacs.")
445 (defun window-safe-min-pixel-width (&optional window)
446 "Return the absolute minimum pixel width of WINDOW."
447 (* window-safe-min-width
448 (frame-char-size (window-normalize-window window) t)))
450 (defcustom window-min-width 10
451 "The minimum total width, in columns, of any window.
452 The value has to accommodate two text columns as well as margins,
453 fringes, a scroll bar and a right divider, if present. A value
454 less than `window-safe-min-width' is ignored. The value of this
455 variable is honored when windows are resized or split.
457 Applications should never rebind this variable. To resize a
458 window to a width less than the one specified here, an
459 application should instead call `window-resize' with a non-nil
460 IGNORE argument. In order to have `split-window' make a window
461 narrower, explicitly specify the SIZE argument of that function."
462 :type 'integer
463 :version "24.1"
464 :group 'windows)
466 (defun window-min-pixel-width (&optional window)
467 "Return the minimum pixel width of window WINDOW."
468 (* (max window-min-width window-safe-min-width)
469 (frame-char-size window t)))
471 (defun window-safe-min-pixel-size (&optional window horizontal)
472 "Return the absolute minimum pixel height of WINDOW.
473 Optional argument HORIZONTAL non-nil means return the absolute
474 minimum pixel width of WINDOW."
475 (if horizontal
476 (window-safe-min-pixel-width window)
477 (window-safe-min-pixel-height window)))
479 (defun window-min-pixel-size (&optional window horizontal)
480 "Return the minimum pixel height of WINDOW.
481 Optional argument HORIZONTAL non-nil means return the minimum
482 pixel width of WINDOW."
483 (if horizontal
484 (window-min-pixel-width window)
485 (window-min-pixel-height window)))
487 (defun window-combined-p (&optional window horizontal)
488 "Return non-nil if WINDOW has siblings in a given direction.
489 WINDOW must be a valid window and defaults to the selected one.
491 HORIZONTAL determines a direction for the window combination. If
492 HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
493 a vertical window combination. If HORIZONTAL is non-nil, return
494 non-nil if WINDOW is part of a horizontal window combination."
495 (setq window (window-normalize-window window))
496 (let ((parent (window-parent window)))
497 (and parent
498 (if horizontal
499 (window-left-child parent)
500 (window-top-child parent)))))
502 (defun window-combination-p (&optional window horizontal)
503 "Return WINDOW's first child if WINDOW is a vertical combination.
504 WINDOW can be any window and defaults to the selected one.
505 Optional argument HORIZONTAL non-nil means return WINDOW's first
506 child if WINDOW is a horizontal combination."
507 (setq window (window-normalize-window window))
508 (if horizontal
509 (window-left-child window)
510 (window-top-child window)))
512 (defun window-combinations (window &optional horizontal)
513 "Return largest number of windows vertically arranged within WINDOW.
514 WINDOW must be a valid window and defaults to the selected one.
515 If HORIZONTAL is non-nil, return the largest number of
516 windows horizontally arranged within WINDOW."
517 (setq window (window-normalize-window window))
518 (cond
519 ((window-live-p window)
520 ;; If WINDOW is live, return 1.
522 ((if horizontal
523 (window-left-child window)
524 (window-top-child window))
525 ;; If WINDOW is iso-combined, return the sum of the values for all
526 ;; child windows of WINDOW.
527 (let ((child (window-child window))
528 (count 0))
529 (while child
530 (setq count
531 (+ (window-combinations child horizontal)
532 count))
533 (setq child (window-right child)))
534 count))
536 ;; If WINDOW is not iso-combined, return the maximum value of any
537 ;; child window of WINDOW.
538 (let ((child (window-child window))
539 (count 1))
540 (while child
541 (setq count
542 (max (window-combinations child horizontal)
543 count))
544 (setq child (window-right child)))
545 count))))
547 (defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only)
548 "Helper function for `walk-window-tree' and `walk-window-subtree'."
549 (let (walk-window-tree-buffer)
550 (while walk-window-tree-window
551 (setq walk-window-tree-buffer
552 (window-buffer walk-window-tree-window))
553 (when (or walk-window-tree-buffer any)
554 (funcall fun walk-window-tree-window))
555 (unless walk-window-tree-buffer
556 (walk-window-tree-1
557 fun (window-left-child walk-window-tree-window) any)
558 (walk-window-tree-1
559 fun (window-top-child walk-window-tree-window) any))
560 (if sub-only
561 (setq walk-window-tree-window nil)
562 (setq walk-window-tree-window
563 (window-right walk-window-tree-window))))))
565 (defun walk-window-tree (fun &optional frame any minibuf)
566 "Run function FUN on each live window of FRAME.
567 FUN must be a function with one argument - a window. FRAME must
568 be a live frame and defaults to the selected one. ANY, if
569 non-nil, means to run FUN on all live and internal windows of
570 FRAME.
572 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
573 window even if it isn't active. MINIBUF nil or omitted means run
574 FUN on FRAME's minibuffer window only if it's active. In both
575 cases the minibuffer window must be part of FRAME. MINIBUF
576 neither nil nor t means never run FUN on the minibuffer window.
578 This function performs a pre-order, depth-first traversal of the
579 window tree. If FUN changes the window tree, the result is
580 unpredictable."
581 (setq frame (window-normalize-frame frame))
582 (walk-window-tree-1 fun (frame-root-window frame) any)
583 (when (memq minibuf '(nil t))
584 ;; Run FUN on FRAME's minibuffer window if requested.
585 (let ((minibuffer-window (minibuffer-window frame)))
586 (when (and (window-live-p minibuffer-window)
587 (eq (window-frame minibuffer-window) frame)
588 (or (eq minibuf t)
589 (minibuffer-window-active-p minibuffer-window)))
590 (funcall fun minibuffer-window)))))
592 (defun walk-window-subtree (fun &optional window any)
593 "Run function FUN on the subtree of windows rooted at WINDOW.
594 WINDOW defaults to the selected window. FUN must be a function
595 with one argument - a window. By default, run FUN only on live
596 windows of the subtree. If the optional argument ANY is non-nil,
597 run FUN on all live and internal windows of the subtree. If
598 WINDOW is live, run FUN on WINDOW only.
600 This function performs a pre-order, depth-first traversal of the
601 subtree rooted at WINDOW. If FUN changes that tree, the result
602 is unpredictable."
603 (setq window (window-normalize-window window))
604 (walk-window-tree-1 fun window any t))
606 (defun window-with-parameter (parameter &optional value frame any minibuf)
607 "Return first window on FRAME with PARAMETER non-nil.
608 FRAME defaults to the selected frame. Optional argument VALUE
609 non-nil means only return a window whose window-parameter value
610 for PARAMETER equals VALUE (comparison is done with `equal').
611 Optional argument ANY non-nil means consider internal windows
612 too.
614 Optional argument MINIBUF t means consider FRAME's minibuffer
615 window even if it isn't active. MINIBUF nil or omitted means
616 consider FRAME's minibuffer window only if it's active. In both
617 cases the minibuffer window must be part of FRAME. MINIBUF
618 neither nil nor t means never consider the minibuffer window."
619 (let (this-value)
620 (catch 'found
621 (walk-window-tree
622 (lambda (window)
623 (when (and (setq this-value (window-parameter window parameter))
624 (or (not value) (equal value this-value)))
625 (throw 'found window)))
626 frame any minibuf))))
628 ;;; Atomic windows.
629 (defun window-atom-root (&optional window)
630 "Return root of atomic window WINDOW is a part of.
631 WINDOW must be a valid window and defaults to the selected one.
632 Return nil if WINDOW is not part of an atomic window."
633 (setq window (window-normalize-window window))
634 (let (root)
635 (while (and window (window-parameter window 'window-atom))
636 (setq root window)
637 (setq window (window-parent window)))
638 root))
640 (defun window-make-atom (window)
641 "Make WINDOW an atomic window.
642 WINDOW must be an internal window. Return WINDOW."
643 (if (not (window-child window))
644 (error "Window %s is not an internal window" window)
645 (walk-window-subtree
646 (lambda (window)
647 (unless (window-parameter window 'window-atom)
648 (set-window-parameter window 'window-atom t)))
649 window t)
650 window))
652 (defun display-buffer-in-atom-window (buffer alist)
653 "Display BUFFER in an atomic window.
654 This function displays BUFFER in a new window that will be
655 combined with an existing window to form an atomic window. If
656 the existing window is already part of an atomic window, add the
657 new window to that atomic window. Operations like `split-window'
658 or `delete-window', when applied to a constituent of an atomic
659 window, are applied atomically to the root of that atomic window.
661 ALIST is an association list of symbols and values. The
662 following symbols can be used.
664 `window' specifies the existing window the new window shall be
665 combined with. Use `window-atom-root' to make the new window a
666 sibling of an atomic window's root. If an internal window is
667 specified here, all children of that window become part of the
668 atomic window too. If no window is specified, the new window
669 becomes a sibling of the selected window. By default, the
670 `window-atom' parameter of the existing window is set to `main'
671 provided it is live and was not set before.
673 `side' denotes the side of the existing window where the new
674 window shall be located. Valid values are `below', `right',
675 `above' and `left'. The default is `below'. By default, the
676 `window-atom' parameter of the new window is set to this value.
678 The return value is the new window, nil when creating that window
679 failed."
680 (let* ((ignore-window-parameters t)
681 (window-combination-limit t)
682 (window-combination-resize 'atom)
683 (window (cdr (assq 'window alist)))
684 (side (or (cdr (assq 'side alist)) 'below))
685 (atom (when window (window-parameter window 'window-atom)))
686 root new)
687 (setq window (window-normalize-window window))
688 (setq root (window-atom-root window))
689 ;; Split off new window.
690 (when (setq new (split-window-no-error window nil side))
691 (window-make-atom
692 (if (and root (not (eq root window)))
693 ;; When WINDOW was part of an atomic window and we did not
694 ;; split its root, root atomic window at old root.
695 root
696 ;; Otherwise, root atomic window at WINDOW's new parent.
697 (window-parent window)))
698 ;; Assign `window-atom' parameters, if needed.
699 (when (and (not atom) (window-live-p window))
700 (set-window-parameter window 'window-atom 'main))
701 (set-window-parameter new 'window-atom side)
702 ;; Display BUFFER in NEW and return NEW.
703 (window--display-buffer
704 buffer new 'window alist display-buffer-mark-dedicated))))
706 (defun window--atom-check-1 (window)
707 "Subroutine of `window--atom-check'."
708 (when window
709 (if (window-parameter window 'window-atom)
710 (let ((count 0))
711 (when (or (catch 'reset
712 (walk-window-subtree
713 (lambda (window)
714 (if (window-parameter window 'window-atom)
715 (setq count (1+ count))
716 (throw 'reset t)))
717 window t))
718 ;; count >= 1 must hold here. If there's no other
719 ;; window around dissolve this atomic window.
720 (= count 1))
721 ;; Dissolve atomic window.
722 (walk-window-subtree
723 (lambda (window)
724 (set-window-parameter window 'window-atom nil))
725 window t)))
726 ;; Check children.
727 (unless (window-buffer window)
728 (window--atom-check-1 (window-left-child window))
729 (window--atom-check-1 (window-top-child window))))
730 ;; Check right sibling
731 (window--atom-check-1 (window-right window))))
733 (defun window--atom-check (&optional frame)
734 "Check atomicity of all windows on FRAME.
735 FRAME defaults to the selected frame. If an atomic window is
736 wrongly configured, reset the atomicity of all its windows on
737 FRAME to nil. An atomic window is wrongly configured if it has
738 no child windows or one of its child windows is not atomic."
739 (window--atom-check-1 (frame-root-window frame)))
741 ;; Side windows.
742 (defcustom window-sides-vertical nil
743 "If non-nil, left and right side windows occupy full frame height.
744 If nil, top and bottom side windows occupy full frame width."
745 :type 'boolean
746 :initialize 'custom-initialize-default
747 :set 'window--sides-verticalize
748 :group 'windows
749 :version "26.1")
751 (defcustom window-sides-reversed nil
752 "Whether top/bottom side windows appear in reverse order.
753 When this is nil, side windows on the top and bottom of a frame
754 are always drawn from left to right with increasing slot values.
755 When this is t, side windows on the top and bottom of a frame are
756 always drawn from right to left with increasing slot values.
758 When this is `bidi', the drawing order is like that for the value
759 t if the value of `bidi-paragraph-direction' is `right-to-left'
760 in the buffer most recently shown in the window selected within
761 the main window area of this frame.
763 The layout of side windows on the left or right of a frame is not
764 affected by the value of this variable."
765 :type
766 '(choice (const :tag "Never" nil)
767 (const :tag "Bidi" bidi)
768 (const :tag "Always" t))
769 :initialize 'custom-initialize-default
770 :set 'window--sides-reverse
771 :group 'windows
772 :version "26.1")
774 (defcustom window-sides-slots '(nil nil nil nil)
775 "Number of available side window slots on each side of a frame.
776 The value is a list of four elements specifying the maximum
777 number of side windows that may be created on the left, top,
778 right and bottom side of any frame.
780 If an element is a number, `display-buffer-in-side-window' will
781 refrain from making a new side window if the number of windows on
782 that side is equal to or exceeds that number. Rather, it will
783 reuse the window whose `window-slot' value is nearest to the slot
784 specified via its ALIST argument. If an element is nil, this
785 means there's no bound on the number of windows on that side."
786 :version "24.1"
787 :risky t
788 :type
789 '(list
790 :value (nil nil nil nil)
791 (choice
792 :tag "Left"
793 :help-echo "Maximum number of left side windows."
794 :value nil
795 :format "%[Left%] %v\n"
796 (const :tag "Unlimited" :format "%t" nil)
797 (integer :tag "Number" :value 2 :size 5))
798 (choice
799 :tag "Top"
800 :help-echo "Maximum number of top side windows."
801 :value nil
802 :format "%[Top%] %v\n"
803 (const :tag "Unlimited" :format "%t" nil)
804 (integer :tag "Number" :value 3 :size 5))
805 (choice
806 :tag "Right"
807 :help-echo "Maximum number of right side windows."
808 :value nil
809 :format "%[Right%] %v\n"
810 (const :tag "Unlimited" :format "%t" nil)
811 (integer :tag "Number" :value 2 :size 5))
812 (choice
813 :tag "Bottom"
814 :help-echo "Maximum number of bottom side windows."
815 :value nil
816 :format "%[Bottom%] %v\n"
817 (const :tag "Unlimited" :format "%t" nil)
818 (integer :tag "Number" :value 3 :size 5)))
819 :group 'windows)
821 (defvar-local window--sides-shown nil
822 "Non-nil if this buffer was shown in a side window once.
823 If this variable is non-nil in a buffer, `switch-to-prev-buffer'
824 and `switch-to-next-buffer' will refrain from showing this buffer
825 within the main window area. `display-buffer-in-side-window'
826 sets this variable automatically.
828 Killing buffer local variables after showing the buffer in a side
829 window annihilates any effect provided by this variable.")
831 (defvar window--sides-inhibit-check nil
832 "Non-nil means inhibit any checks on side windows.")
834 (defun window--sides-reverse-on-frame-p (frame)
835 "Return non-nil when side windows should appear reversed on FRAME.
836 This uses some heuristics to guess the user's intentions when the
837 selected window of FRAME is a side window ."
838 (cond
839 ;; Reverse when `window-sides-reversed' is t. Do not reverse when
840 ;; `window-sides-reversed' is nil.
841 ((memq window-sides-reversed '(nil t))
842 window-sides-reversed)
843 ;; Reverse when FRAME's selected window shows a right-to-left buffer.
844 ((let ((window (frame-selected-window frame)))
845 (when (and (not (window-parameter window 'window-side))
846 (or (not (window-minibuffer-p window))
847 (setq window (minibuffer-selected-window))))
848 (with-current-buffer (window-buffer window)
849 (eq bidi-paragraph-direction 'right-to-left)))))
850 ;; Reverse when FRAME's `window-sides-main-selected-window' parameter
851 ;; specifies a live window showing a right-to-left buffer.
852 ((let ((window (frame-parameter
853 frame 'window-sides-main-selected-window)))
854 (when (window-live-p window)
855 (with-current-buffer (window-buffer window)
856 (eq bidi-paragraph-direction 'right-to-left)))))
857 ;; Reverse when all windows in FRAME's main window show right-to-left
858 ;; buffers.
860 (catch 'found
861 (walk-window-subtree
862 (lambda (window)
863 (with-current-buffer (window-buffer window)
864 (when (eq bidi-paragraph-direction 'left-to-right)
865 (throw 'found nil))))
866 (window-main-window frame))
867 t))))
869 (defun window-main-window (&optional frame)
870 "Return the main window of specified FRAME.
871 The optional argument FRAME must be a live frame and defaults to
872 the selected one.
874 If FRAME has no side windows, return FRAME's root window.
875 Otherwise, return either an internal non-side window such that
876 all other non-side windows on FRAME descend from it, or the
877 single live non-side window of FRAME."
878 (let ((frame (window-normalize-frame frame))
879 main sibling)
880 ;; Set main to the _last_ window found by `walk-window-tree' that
881 ;; is not a side window but has a side window as its sibling.
882 (walk-window-tree
883 (lambda (window)
884 (and (not (window-parameter window 'window-side))
885 (or (and (setq sibling (window-prev-sibling window))
886 (window-parameter sibling 'window-side))
887 (and (setq sibling (window-next-sibling window))
888 (window-parameter sibling 'window-side)))
889 (setq main window)))
890 frame t 'nomini)
891 (or main (frame-root-window frame))))
893 (defun window--make-major-side-window-next-to (side)
894 "Return window to split for making a major side window.
895 SIDE must be one of the symbols `left', `top', `right' or
896 `bottom'.
898 This is an auxiliary function of `window--make-major-side-window'
899 and must not be called when a window on SIDE exists already."
900 (let ((root (frame-root-window))
901 (window--sides-inhibit-check t)
902 window)
903 ;; (1) If a window on the opposite side exists, return that window's
904 ;; sibling.
905 ;; (2) If the new window shall span the entire side, return the
906 ;; frame's root window.
907 ;; (3) If a window on an orthogonal side exists, return that
908 ;; window's sibling.
909 ;; (4) Otherwise return the frame's root window.
910 (cond
911 ((or (and (eq side 'left)
912 (setq window (window-with-parameter 'window-side 'right nil t)))
913 (and (eq side 'top)
914 (setq window (window-with-parameter 'window-side 'bottom nil t))))
915 (window-prev-sibling window))
916 ((or (and (eq side 'right)
917 (setq window (window-with-parameter 'window-side 'left nil t)))
918 (and (eq side 'bottom)
919 (setq window (window-with-parameter 'window-side 'top nil t))))
920 (window-next-sibling window))
921 ((memq side '(left right))
922 (cond
923 (window-sides-vertical
924 root)
925 ((setq window (window-with-parameter 'window-side 'top nil t))
926 (window-next-sibling window))
927 ((setq window (window-with-parameter 'window-side 'bottom nil t))
928 (window-prev-sibling window))
929 (t root)))
930 ((memq side '(top bottom))
931 (cond
932 ((not window-sides-vertical)
933 root)
934 ((setq window (window-with-parameter 'window-side 'left nil t))
935 (window-next-sibling window))
936 ((setq window (window-with-parameter 'window-side 'right nil t))
937 (window-prev-sibling window))
938 (t root))))))
940 (defun window--make-major-side-window (buffer side slot &optional alist)
941 "Display BUFFER in a new major side window on the selected frame.
942 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
943 specifies the slot to use. ALIST is an association list of
944 symbols and values as passed to `display-buffer-in-side-window'.
945 Return the new window, nil if its creation failed.
947 This is an auxiliary function of `display-buffer-in-side-window'
948 and may be called only if no window on SIDE exists yet."
949 (let* ((left-or-right (memq side '(left right)))
950 (next-to (window--make-major-side-window-next-to side))
951 (on-side (cond
952 ((eq side 'top) 'above)
953 ((eq side 'bottom) 'below)
954 (t side)))
955 (window--sides-inhibit-check t)
956 ;; The following two bindings will tell `split-window' to take
957 ;; the space for the new window from the selected frame's main
958 ;; window and not make a new parent window unless needed.
959 (window-combination-resize 'side)
960 (window-combination-limit nil)
961 (window (split-window-no-error next-to nil on-side)))
962 (when window
963 ;; Initialize `window-side' parameter of new window to SIDE and
964 ;; make that parameter persistent.
965 (set-window-parameter window 'window-side side)
966 (add-to-list 'window-persistent-parameters '(window-side . writable))
967 ;; Install `window-slot' parameter of new window and make that
968 ;; parameter persistent.
969 (set-window-parameter window 'window-slot slot)
970 (add-to-list 'window-persistent-parameters '(window-slot . writable))
971 ;; Auto-adjust height/width of new window unless a size has been
972 ;; explicitly requested.
973 (unless (if left-or-right
974 (cdr (assq 'window-width alist))
975 (cdr (assq 'window-height alist)))
976 (setq alist
977 (cons
978 (cons
979 (if left-or-right 'window-width 'window-height)
980 (/ (window-total-size (frame-root-window) left-or-right)
981 ;; By default use a fourth of the size of the frame's
982 ;; root window.
984 alist)))
985 (with-current-buffer buffer
986 (setq window--sides-shown t))
987 ;; Install BUFFER in new window and return WINDOW.
988 (window--display-buffer buffer window 'window alist 'side))))
990 (defun display-buffer-in-side-window (buffer alist)
991 "Display BUFFER in a side window of the selected frame.
992 ALIST is an association list of symbols and values. The
993 following special symbols can be used in ALIST.
995 `side' denotes the side of the frame where the new window shall
996 be located. Valid values are `bottom', `right', `top' and
997 `left'. The default is `bottom'.
999 `slot' if non-nil, specifies the window slot where to display
1000 BUFFER. A value of zero or nil means use the middle slot on
1001 the specified side. A negative value means use a slot
1002 preceding (that is, above or on the left of) the middle slot.
1003 A positive value means use a slot following (that is, below or
1004 on the right of) the middle slot. The default is zero.
1006 If the current frame size or the settings of `window-sides-slots'
1007 do not permit making a new window, a suitable existing window may
1008 be reused and have its `window-slot' parameter value accordingly
1009 modified.
1011 Unless `display-buffer-mark-dedicated' is non-nil, softly
1012 dedicate the side window used to BUFFER. Return the window used
1013 for displaying BUFFER, nil if no suitable window can be found.
1015 This function installs the `window-side' and `window-slot'
1016 parameters and makes them persistent. It neither modifies ALIST
1017 nor installs any other window parameters unless they have been
1018 explicitly provided via a `window-parameters' entry in ALIST."
1019 (let* ((side (or (cdr (assq 'side alist)) 'bottom))
1020 (slot (or (cdr (assq 'slot alist)) 0))
1021 (left-or-right (memq side '(left right)))
1022 ;; Softly dedicate window to BUFFER unless
1023 ;; `display-buffer-mark-dedicated' already asks for it.
1024 (dedicated (or display-buffer-mark-dedicated 'side)))
1025 (cond
1026 ((not (memq side '(top bottom left right)))
1027 (error "Invalid side %s specified" side))
1028 ((not (numberp slot))
1029 (error "Invalid slot %s specified" slot)))
1031 (let* ((major (window-with-parameter 'window-side side nil t))
1032 ;; `major' is the major window on SIDE, `windows' the list of
1033 ;; life windows on SIDE.
1034 (reversed (window--sides-reverse-on-frame-p (selected-frame)))
1035 (windows
1036 (cond
1037 ((window-live-p major)
1038 (list major))
1039 ((window-valid-p major)
1040 (let* ((first (window-child major))
1041 (next (window-next-sibling first))
1042 (windows (list next first)))
1043 (setq reversed (> (window-parameter first 'window-slot)
1044 (window-parameter next 'window-slot)))
1045 (while (setq next (window-next-sibling next))
1046 (setq windows (cons next windows)))
1047 (if reversed windows (nreverse windows))))))
1048 (slots (when major (max 1 (window-child-count major))))
1049 (max-slots
1050 (nth (cond
1051 ((eq side 'left) 0)
1052 ((eq side 'top) 1)
1053 ((eq side 'right) 2)
1054 ((eq side 'bottom) 3))
1055 window-sides-slots))
1056 (window--sides-inhibit-check t)
1057 window this-window this-slot prev-window next-window
1058 best-window best-slot abs-slot)
1060 (cond
1061 ((and (numberp max-slots) (<= max-slots 0))
1062 ;; No side-slots available on this side. Don't raise an error,
1063 ;; just return nil.
1064 nil)
1065 ((not windows)
1066 ;; No major side window exists on this side, make one.
1067 (window--make-major-side-window buffer side slot alist))
1069 ;; Scan windows on SIDE.
1070 (catch 'found
1071 (dolist (window windows)
1072 (setq this-slot (window-parameter window 'window-slot))
1073 (cond
1074 ;; The following should not happen and probably be checked
1075 ;; by window--sides-check.
1076 ((not (numberp this-slot)))
1077 ((= this-slot slot)
1078 ;; A window with a matching slot has been found.
1079 (setq this-window window)
1080 (throw 'found t))
1082 ;; Check if this window has a better slot value wrt the
1083 ;; slot of the window we want.
1084 (setq abs-slot
1085 (if (or (and (> this-slot 0) (> slot 0))
1086 (and (< this-slot 0) (< slot 0)))
1087 (abs (- slot this-slot))
1088 (+ (abs slot) (abs this-slot))))
1089 (unless (and best-slot (<= best-slot abs-slot))
1090 (setq best-window window)
1091 (setq best-slot abs-slot))
1092 (if reversed
1093 (cond
1094 ((<= this-slot slot)
1095 (setq next-window window))
1096 ((not prev-window)
1097 (setq prev-window window)))
1098 (cond
1099 ((<= this-slot slot)
1100 (setq prev-window window))
1101 ((not next-window)
1102 (setq next-window window))))))))
1104 ;; `this-window' is the first window with the same SLOT.
1105 ;; `prev-window' is the window with the largest slot < SLOT. A new
1106 ;; window will be created after it.
1107 ;; `next-window' is the window with the smallest slot > SLOT. A new
1108 ;; window will be created before it.
1109 ;; `best-window' is the window with the smallest absolute difference
1110 ;; of its slot and SLOT.
1111 (or (and this-window
1112 ;; Reuse `this-window'.
1113 (with-current-buffer buffer
1114 (setq window--sides-shown t))
1115 (window--display-buffer
1116 buffer this-window 'reuse alist dedicated))
1117 (and (or (not max-slots) (< slots max-slots))
1118 (or (and next-window
1119 ;; Make new window before `next-window'.
1120 (let ((next-side (if left-or-right 'above 'left))
1121 (window-combination-resize 'side))
1122 (setq window (split-window-no-error
1123 next-window nil next-side))))
1124 (and prev-window
1125 ;; Make new window after `prev-window'.
1126 (let ((prev-side (if left-or-right 'below 'right))
1127 (window-combination-resize 'side))
1128 (setq window (split-window-no-error
1129 prev-window nil prev-side)))))
1130 (set-window-parameter window 'window-slot slot)
1131 (with-current-buffer buffer
1132 (setq window--sides-shown t))
1133 (window--display-buffer
1134 buffer window 'window alist dedicated))
1135 (and best-window
1136 ;; Reuse `best-window'.
1137 (progn
1138 ;; Give best-window the new slot value.
1139 (set-window-parameter best-window 'window-slot slot)
1140 (with-current-buffer buffer
1141 (setq window--sides-shown t))
1142 (window--display-buffer
1143 buffer best-window 'reuse alist dedicated)))))))))
1145 (defun window-toggle-side-windows (&optional frame)
1146 "Toggle side windows on specified FRAME.
1147 FRAME must be a live frame and defaults to the selected one.
1149 If FRAME has at least one side window, save FRAME's state in the
1150 FRAME's `window-state' frame parameter and delete all side
1151 windows on FRAME afterwards. Otherwise, if FRAME has a
1152 `window-state' parameter, use that to restore any side windows on
1153 FRAME leaving FRAME's main window alone. Signal an error if
1154 FRAME has no side window and no saved state is found."
1155 (interactive)
1156 (let* ((frame (window-normalize-frame frame))
1157 (window--sides-inhibit-check t)
1158 state)
1159 (cond
1160 ((window-with-parameter 'window-side nil frame)
1161 ;; At least one side window exists. Remove all side windows after
1162 ;; saving FRAME's state in its `window-state' parameter.
1163 (set-frame-parameter
1164 frame 'window-state (window-state-get (frame-root-window frame)))
1165 (let ((ignore-window-parameters t))
1166 (delete-other-windows (window-main-window frame))))
1167 ((setq state (frame-parameter frame 'window-state))
1168 ;; A window state was saved for FRAME. Restore it and put the
1169 ;; current root window into its main window.
1170 (let ((main-state (window-state-get (frame-root-window frame))))
1171 (window-state-put state (frame-root-window frame) t)
1172 (window-state-put main-state (window-main-window frame)))
1173 (window--sides-reverse-frame frame))
1175 (error "No side windows state found")))))
1177 (defun window--sides-reverse-all ()
1178 "Maybe reverse side windows on all frames."
1179 (unless window--sides-inhibit-check
1180 (dolist (frame (frame-list))
1181 (window--sides-reverse-frame frame))))
1183 (defun window--sides-reverse-frame (frame)
1184 "Maybe reverse side windows on FRAME."
1185 (when (eq window-sides-reversed 'bidi)
1186 (let ((window (frame-selected-window frame)))
1187 (unless (or (window-parameter window 'window-side)
1188 (window-minibuffer-p window))
1189 (set-frame-parameter
1190 frame 'window-sides-main-selected-window window))))
1191 (window--sides-reverse-side frame 'top)
1192 (window--sides-reverse-side frame 'bottom))
1194 (defun window--sides-reverse-side (frame side)
1195 "Maybe reverse windows on SIDE of FRAME."
1196 (let ((major (window-with-parameter 'window-side side frame t))
1197 (window--sides-inhibit-check t))
1198 (when (and major (not (window-live-p major)))
1199 (let* ((first (window-child major))
1200 (reversed (> (window-parameter first 'window-slot)
1201 (window-parameter
1202 (window-next-sibling first) 'window-slot)))
1203 (reverse (window--sides-reverse-on-frame-p frame)))
1204 (unless (eq reversed reverse)
1205 ;; We have to reverse.
1206 (let ((last (window-last-child major)))
1207 (while (and (not (eq first last))
1208 (not (eq first (window-next-sibling last))))
1209 (window-swap-states first last t)
1210 (setq first (window-next-sibling first))
1211 (setq last (window-prev-sibling last)))))))))
1213 (defun window--sides-reverse (symbol value)
1214 "Helper function for customizing `window-sides-reversed'."
1215 (set-default symbol value)
1216 (remove-hook 'buffer-list-update-hook 'window--sides-reverse-all)
1217 (remove-hook 'window-configuration-change-hook 'window--sides-reverse-all)
1218 (dolist (frame (frame-list))
1219 (set-frame-parameter frame 'window-sides-main-selected-window nil))
1220 (when (eq value 'bidi)
1221 (add-hook 'buffer-list-update-hook 'window--sides-reverse-all)
1222 (add-hook 'window-configuration-change-hook 'window--sides-reverse-all))
1223 (window--sides-reverse-all))
1225 (defun window--sides-verticalize-frame (&optional frame)
1226 "Maybe change side windows layout on specified FRAME."
1227 (setq frame (window-normalize-frame frame))
1228 (let ((window--sides-inhibit-check t)
1229 (root (frame-root-window frame))
1230 (main (window-main-window frame)))
1231 (when (and (not (eq main root))
1232 (not (eq (window-parent main) root))
1233 (window-combined-p main window-sides-vertical))
1234 (let* ((window--sides-inhibit-check t)
1235 (ignore-window-parameters t)
1236 (first (window-child root))
1237 (first-state
1238 (and first (window-parameter first 'window-side)
1239 (window-state-get first)))
1240 (last (window-last-child root))
1241 (last-state
1242 (and last (window-parameter last 'window-side)
1243 (window-state-get last)))
1244 (dummy (get-buffer-create " *dummy*"))
1245 major)
1246 (unwind-protect
1247 (progn
1248 (when first-state (delete-window first))
1249 (when last-state (delete-window last))
1250 (when first-state
1251 (setq major (window--make-major-side-window
1252 dummy (if window-sides-vertical 'top 'left) 0))
1253 (window-state-put first-state major t))
1254 (when last-state
1255 (setq major (window--make-major-side-window
1256 dummy (if window-sides-vertical 'bottom 'right) 0))
1257 (window-state-put last-state major t)))
1258 (kill-buffer " *dummy*"))))))
1260 (defun window--sides-verticalize (symbol value)
1261 "Helper function for customizing `window-sides-vertical'."
1262 (set-default symbol value)
1263 (dolist (frame (frame-list))
1264 (window--sides-verticalize-frame frame)))
1266 (defun window--sides-check-failed (frame)
1267 "Helper function for `window--sides-check'."
1268 (catch 'failed
1269 ;; FRAME must have a main window.
1270 (unless (window-main-window frame)
1271 (error "Frame %s has no main window" frame)
1272 (throw 'failed t))
1273 ;; Now check the side windows.
1274 (dolist (side '(left top right bottom))
1275 (let ((window (window-with-parameter 'window-side side frame t)))
1276 (when window
1277 ;; If WINDOW is live there must be no other window on this frame
1278 ;; with the same `window-side' parameter.
1279 (if (window-live-p window)
1280 (walk-window-tree
1281 (lambda (this)
1282 (when (and (eq (window-parameter this 'window-side) side)
1283 (not (eq this window)))
1284 (error "Window %s has same side %s as window %s but no common parent"
1285 this side window)
1286 (throw 'failed t)))
1287 frame t 'nomini)
1288 (walk-window-tree
1289 (lambda (this)
1290 (if (eq (window-parent this) window)
1291 (unless (eq (window-parameter this 'window-side) side)
1292 (error "Window %s has not same side %s as its parent %s"
1293 this side window)
1294 (throw 'failed t))
1295 (when (and (eq (window-parameter this 'window-side) side)
1296 (not (eq this window)))
1297 (error "Window %s has same side %s as major side window %s but its parent is %s"
1298 this side window (window-parent this))
1299 (throw 'failed t))))
1300 frame t 'nomini)))))))
1302 (defun window--sides-check (frame)
1303 "Check side windows configuration of FRAME.
1304 In a valid side windows configuration there can be at most one
1305 internal side window on each side and all its children must be
1306 live and have the same `window-side' parameter and no other
1307 window with the same `window-side' parameter exists on FRAME. If
1308 there is no such internal window, there may be at most one window
1309 with this side's `window-side' parameter on FRAME.
1311 If the configuration is invalid, reset the `window-side'
1312 parameters of all windows on FRAME."
1313 (when (and (not window--sides-inhibit-check)
1314 (window-with-parameter 'window-side nil frame t)
1315 (window--sides-check-failed frame))
1316 ;; Reset all `window-side' parameters.
1317 (walk-window-tree
1318 (lambda (window)
1319 (set-window-parameter window 'window-side nil))
1320 frame t 'nomini)
1321 (message "Side windows configuration reset for frame %s" frame)))
1323 (defun window--check (&optional frame)
1324 "Check atomic and side windows on FRAME.
1325 FRAME defaults to the selected frame."
1326 (window--sides-check frame)
1327 (window--atom-check frame))
1329 ;; Dumping frame/window contents.
1330 (defun window--dump-window (&optional window erase)
1331 "Dump WINDOW to buffer *window-frame-dump*.
1332 WINDOW must be a valid window and defaults to the selected one.
1333 Optional argument ERASE non-nil means erase *window-frame-dump*
1334 before writing to it."
1335 (setq window (window-normalize-window window))
1336 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1337 (when erase (erase-buffer))
1338 (insert
1339 (format "%s parent: %s\n" window (window-parent window))
1340 (format "pixel left: %s top: %s size: %s x %s new: %s\n"
1341 (window-pixel-left window) (window-pixel-top window)
1342 (window-size window t t) (window-size window nil t)
1343 (window-new-pixel window))
1344 (format "char left: %s top: %s size: %s x %s new: %s\n"
1345 (window-left-column window) (window-top-line window)
1346 (window-total-size window t) (window-total-size window)
1347 (window-new-total window))
1348 (format "normal: %s x %s new: %s\n"
1349 (window-normal-size window t) (window-normal-size window)
1350 (window-new-normal window)))
1351 (when (window-live-p window)
1352 (let ((fringes (window-fringes window))
1353 (margins (window-margins window)))
1354 (insert
1355 (format "body pixel: %s x %s char: %s x %s\n"
1356 (window-body-width window t) (window-body-height window t)
1357 (window-body-width window) (window-body-height window))
1358 (format "width left fringe: %s left margin: %s right margin: %s\n"
1359 (car fringes) (or (car margins) 0) (or (cdr margins) 0))
1360 (format "width right fringe: %s scroll-bar: %s divider: %s\n"
1361 (cadr fringes)
1362 (window-scroll-bar-width window)
1363 (window-right-divider-width window))
1364 (format "height header-line: %s mode-line: %s divider: %s\n"
1365 (window-header-line-height window)
1366 (window-mode-line-height window)
1367 (window-bottom-divider-width window)))))
1368 (insert "\n")))
1370 (defun window--dump-frame (&optional window-or-frame)
1371 "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
1372 WINDOW-OR-FRAME can be a frame or a window and defaults to the
1373 selected frame. When WINDOW-OR-FRAME is a window, dump that
1374 window's frame. The buffer *window-frame-dump* is erased before
1375 dumping to it."
1376 (let* ((window
1377 (cond
1378 ((or (not window-or-frame)
1379 (frame-live-p window-or-frame))
1380 (frame-root-window window-or-frame))
1381 ((or (window-live-p window-or-frame)
1382 (window-child window-or-frame))
1383 window-or-frame)
1385 (frame-root-window))))
1386 (frame (window-frame window)))
1387 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1388 (erase-buffer)
1389 (insert
1390 (format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
1391 (frame-pixel-width frame) (frame-pixel-height frame)
1392 (frame-total-cols frame) (frame-total-lines frame)
1393 (frame-char-width frame) (frame-char-height frame))
1394 (format "frame text pixel: %s x %s cols/lines: %s x %s\n"
1395 (frame-text-width frame) (frame-text-height frame)
1396 (frame-text-cols frame) (frame-text-lines frame))
1397 (format "tool: %s scroll: %s/%s fringe: %s border: %s right: %s bottom: %s\n\n"
1398 (if (fboundp 'tool-bar-height)
1399 (tool-bar-height frame t)
1400 "0")
1401 (frame-scroll-bar-width frame)
1402 (frame-scroll-bar-height frame)
1403 (frame-fringe-width frame)
1404 (frame-border-width frame)
1405 (frame-right-divider-width frame)
1406 (frame-bottom-divider-width frame)))
1407 (walk-window-tree 'window--dump-window frame t t))))
1409 ;;; Window sizes.
1410 (defun window-total-size (&optional window horizontal round)
1411 "Return the total height or width of WINDOW.
1412 WINDOW must be a valid window and defaults to the selected one.
1414 If HORIZONTAL is omitted or nil, return the total height of
1415 WINDOW, in lines. If WINDOW is live, its total height includes,
1416 in addition to the height of WINDOW's text, the heights of
1417 WINDOW's mode and header line and a bottom divider, if any.
1419 If HORIZONTAL is non-nil, return the total width of WINDOW, in
1420 columns. If WINDOW is live, its total width includes, in
1421 addition to the width of WINDOW's text, the widths of WINDOW's
1422 fringes, margins, scroll bars and its right divider, if any.
1424 If WINDOW is internal, return the respective size of the screen
1425 areas spanned by its children.
1427 Optional argument ROUND is handled as for `window-total-height'
1428 and `window-total-width'."
1429 (if horizontal
1430 (window-total-width window round)
1431 (window-total-height window round)))
1433 (defun window-size (&optional window horizontal pixelwise round)
1434 "Return the height or width of WINDOW.
1435 WINDOW must be a valid window and defaults to the selected one.
1437 If HORIZONTAL is omitted or nil, return the total height of
1438 WINDOW, in lines, like `window-total-height'. Otherwise return
1439 the total width, in columns, like `window-total-width'.
1441 Optional argument PIXELWISE means return the pixel size of WINDOW
1442 like `window-pixel-height' and `window-pixel-width'.
1444 Optional argument ROUND is ignored if PIXELWISE is non-nil and
1445 handled as for `window-total-height' and `window-total-width'
1446 otherwise."
1447 (if horizontal
1448 (if pixelwise
1449 (window-pixel-width window)
1450 (window-total-width window round))
1451 (if pixelwise
1452 (window-pixel-height window)
1453 (window-total-height window round))))
1455 (defvar window-size-fixed nil
1456 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
1457 If the value is `height', then only the window's height is fixed.
1458 If the value is `width', then only the window's width is fixed.
1459 Any other non-nil value fixes both the width and the height.
1461 Emacs won't change the size of any window displaying that buffer,
1462 unless it has no other choice (like when deleting a neighboring
1463 window).")
1464 (make-variable-buffer-local 'window-size-fixed)
1466 (defun window--preservable-size (window &optional horizontal)
1467 "Return height of WINDOW as `window-preserve-size' would preserve it.
1468 Optional argument HORIZONTAL non-nil means to return the width of
1469 WINDOW as `window-preserve-size' would preserve it."
1470 (if horizontal
1471 (window-body-width window t)
1472 (+ (window-body-height window t)
1473 (window-header-line-height window)
1474 (window-mode-line-height window))))
1476 (defun window-preserve-size (&optional window horizontal preserve)
1477 "Preserve height of window WINDOW.
1478 WINDOW must be a live window and defaults to the selected one.
1479 Optional argument HORIZONTAL non-nil means preserve the width of
1480 WINDOW.
1482 PRESERVE t means to preserve the current height/width of WINDOW's
1483 body in frame and window resizing operations whenever possible.
1484 The height/width of WINDOW will change only if Emacs has no other
1485 choice. Resizing a window whose height/width is preserved never
1486 throws an error.
1488 PRESERVE nil means to stop preserving the height/width of WINDOW,
1489 lifting the respective restraint induced by a previous call of
1490 `window-preserve-size' for WINDOW. Calling `enlarge-window',
1491 `shrink-window', `split-window' or `fit-window-to-buffer' with
1492 WINDOW as argument also removes the respective restraint.
1494 Other values of PRESERVE are reserved for future use."
1495 (setq window (window-normalize-window window t))
1496 (let* ((parameter (window-parameter window 'window-preserved-size))
1497 (width (nth 1 parameter))
1498 (height (nth 2 parameter)))
1499 (if horizontal
1500 (set-window-parameter
1501 window 'window-preserved-size
1502 (list
1503 (window-buffer window)
1504 (and preserve (window--preservable-size window t))
1505 height))
1506 (set-window-parameter
1507 window 'window-preserved-size
1508 (list
1509 (window-buffer window)
1510 width
1511 (and preserve (window--preservable-size window)))))))
1513 (defun window-preserved-size (&optional window horizontal)
1514 "Return preserved height of window WINDOW.
1515 WINDOW must be a live window and defaults to the selected one.
1516 Optional argument HORIZONTAL non-nil means to return preserved
1517 width of WINDOW."
1518 (setq window (window-normalize-window window t))
1519 (let* ((parameter (window-parameter window 'window-preserved-size))
1520 (buffer (nth 0 parameter))
1521 (width (nth 1 parameter))
1522 (height (nth 2 parameter)))
1523 (when (eq buffer (window-buffer window))
1524 (if horizontal width height))))
1526 (defun window--preserve-size (window horizontal)
1527 "Return non-nil when the height of WINDOW shall be preserved.
1528 Optional argument HORIZONTAL non-nil means to return non-nil when
1529 the width of WINDOW shall be preserved."
1530 (let ((size (window-preserved-size window horizontal)))
1531 (and (numberp size)
1532 (= size (window--preservable-size window horizontal)))))
1534 (defun window-safe-min-size (&optional window horizontal pixelwise)
1535 "Return safe minimum size of WINDOW.
1536 WINDOW must be a valid window and defaults to the selected one.
1537 Optional argument HORIZONTAL non-nil means return the minimum
1538 number of columns of WINDOW; otherwise return the minimum number
1539 of WINDOW's lines.
1541 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1542 of WINDOW."
1543 (setq window (window-normalize-window window))
1544 (if pixelwise
1545 (if horizontal
1546 (* window-safe-min-width
1547 (frame-char-width (window-frame window)))
1548 (* window-safe-min-height
1549 (frame-char-height (window-frame window))))
1550 (if horizontal window-safe-min-width window-safe-min-height)))
1552 (defun window-min-size (&optional window horizontal ignore pixelwise)
1553 "Return the minimum size of WINDOW.
1554 WINDOW must be a valid window and defaults to the selected one.
1555 Optional argument HORIZONTAL non-nil means return the minimum
1556 number of columns of WINDOW; otherwise return the minimum number
1557 of WINDOW's lines.
1559 The optional argument IGNORE has the same meaning as for
1560 `window-resizable'. Optional argument PIXELWISE non-nil means
1561 return the minimum pixel-size of WINDOW."
1562 (window--min-size-1
1563 (window-normalize-window window) horizontal ignore pixelwise))
1565 (defun window--min-size-ignore-p (window ignore)
1566 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
1567 (if (window-valid-p ignore)
1568 (eq window ignore)
1569 (not (memq ignore '(nil preserved)))))
1571 (defun window--min-size-1 (window horizontal ignore pixelwise)
1572 "Internal function of `window-min-size'."
1573 (let ((sub (window-child window)))
1574 (if sub
1575 (let ((value 0))
1576 ;; WINDOW is an internal window.
1577 (if (window-combined-p sub horizontal)
1578 ;; The minimum size of an iso-combination is the sum of
1579 ;; the minimum sizes of its child windows.
1580 (while sub
1581 (setq value (+ value
1582 (window--min-size-1
1583 sub horizontal ignore pixelwise)))
1584 (setq sub (window-right sub)))
1585 ;; The minimum size of an ortho-combination is the maximum
1586 ;; of the minimum sizes of its child windows.
1587 (while sub
1588 (setq value (max value
1589 (window--min-size-1
1590 sub horizontal ignore pixelwise)))
1591 (setq sub (window-right sub))))
1592 value)
1593 (with-current-buffer (window-buffer window)
1594 (cond
1595 ((window-minibuffer-p window)
1596 (if pixelwise (frame-char-height (window-frame window)) 1))
1597 ((window-size-fixed-p window horizontal ignore)
1598 ;; The minimum size of a fixed size window is its size.
1599 (window-size window horizontal pixelwise))
1600 ((eq ignore 'safe)
1601 ;; If IGNORE equals `safe' return the safe value.
1602 (window-safe-min-size window horizontal pixelwise))
1603 (horizontal
1604 ;; For the minimum width of a window take fringes and
1605 ;; scroll-bars into account. This is questionable and should
1606 ;; be removed as soon as we are able to split (and resize)
1607 ;; windows such that the new (or resized) windows can get a
1608 ;; size less than the user-specified `window-min-height' and
1609 ;; `window-min-width'.
1610 (let* ((char-size (frame-char-size window t))
1611 (fringes (window-fringes window))
1612 (margins (window-margins window))
1613 ;; Let the 'min-margins' parameter override the actual
1614 ;; widths of the margins. We allow any number to
1615 ;; replace the values specified by `window-margins'.
1616 ;; See bug#24193 for the rationale of this parameter.
1617 (min-margins (window-parameter window 'min-margins))
1618 (left-min-margin (and min-margins
1619 (numberp (car min-margins))
1620 (car min-margins)))
1621 (right-min-margin (and min-margins
1622 (numberp (cdr min-margins))
1623 (cdr min-margins)))
1624 (pixel-width
1625 (+ (window-safe-min-size window t t)
1626 (* (or left-min-margin (car margins) 0) char-size)
1627 (* (or right-min-margin(cdr margins) 0) char-size)
1628 (car fringes) (cadr fringes)
1629 (window-scroll-bar-width window)
1630 (window-right-divider-width window))))
1631 (if pixelwise
1632 (max
1633 (if window-resize-pixelwise
1634 pixel-width
1635 ;; Round up to next integral of columns.
1636 (* (ceiling pixel-width char-size) char-size))
1637 (if (window--min-size-ignore-p window ignore)
1639 (window-min-pixel-width window)))
1640 (max
1641 (ceiling pixel-width char-size)
1642 (if (window--min-size-ignore-p window ignore)
1644 window-min-width)))))
1645 ((let ((char-size (frame-char-size window))
1646 (pixel-height
1647 (+ (window-safe-min-size window nil t)
1648 (window-header-line-height window)
1649 (window-scroll-bar-height window)
1650 (window-mode-line-height window)
1651 (window-bottom-divider-width window))))
1652 (if pixelwise
1653 (max
1654 (if window-resize-pixelwise
1655 pixel-height
1656 ;; Round up to next integral of lines.
1657 (* (ceiling pixel-height char-size) char-size))
1658 (if (window--min-size-ignore-p window ignore)
1660 (window-min-pixel-height window)))
1661 (max (ceiling pixel-height char-size)
1662 (if (window--min-size-ignore-p window ignore)
1664 window-min-height))))))))))
1666 (defun window-sizable (window delta &optional horizontal ignore pixelwise)
1667 "Return DELTA if DELTA lines can be added to WINDOW.
1668 WINDOW must be a valid window and defaults to the selected one.
1669 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1670 columns can be added to WINDOW. A return value of zero means
1671 that no lines (or columns) can be added to WINDOW.
1673 This function looks only at WINDOW and, recursively, its child
1674 windows. The function `window-resizable' looks at other windows
1675 as well.
1677 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1678 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1679 return the maximum value in the range 0..DELTA by which WINDOW
1680 can be enlarged.
1682 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1683 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1684 return the minimum value in the range DELTA..0 by which WINDOW
1685 can be shrunk.
1687 The optional argument IGNORE has the same meaning as for
1688 `window-resizable'. Optional argument PIXELWISE non-nil means
1689 interpret DELTA as pixels."
1690 (setq window (window-normalize-window window))
1691 (cond
1692 ((< delta 0)
1693 (max (- (window-min-size window horizontal ignore pixelwise)
1694 (window-size window horizontal pixelwise))
1695 delta))
1696 ((> delta 0)
1697 (if (window-size-fixed-p window horizontal ignore)
1699 delta))
1700 (t 0)))
1702 (defun window-sizable-p (window delta &optional horizontal ignore pixelwise)
1703 "Return t if WINDOW can be resized by DELTA lines.
1704 WINDOW must be a valid window and defaults to the selected one.
1705 For the meaning of the arguments of this function see the
1706 doc-string of `window-sizable'."
1707 (setq window (window-normalize-window window))
1708 (if (> delta 0)
1709 (>= (window-sizable window delta horizontal ignore pixelwise)
1710 delta)
1711 (<= (window-sizable window delta horizontal ignore pixelwise)
1712 delta)))
1714 (defun window--size-fixed-1 (window horizontal ignore)
1715 "Internal function for `window-size-fixed-p'."
1716 (let ((sub (window-child window)))
1717 (catch 'fixed
1718 (if sub
1719 ;; WINDOW is an internal window.
1720 (if (window-combined-p sub horizontal)
1721 ;; An iso-combination is fixed size if all its child
1722 ;; windows are fixed-size.
1723 (progn
1724 (while sub
1725 (unless (window--size-fixed-1 sub horizontal ignore)
1726 ;; We found a non-fixed-size child window, so
1727 ;; WINDOW's size is not fixed.
1728 (throw 'fixed nil))
1729 (setq sub (window-right sub)))
1730 ;; All child windows are fixed-size, so WINDOW's size is
1731 ;; fixed.
1732 (throw 'fixed t))
1733 ;; An ortho-combination is fixed-size if at least one of its
1734 ;; child windows is fixed-size.
1735 (while sub
1736 (when (window--size-fixed-1 sub horizontal ignore)
1737 ;; We found a fixed-size child window, so WINDOW's size
1738 ;; is fixed.
1739 (throw 'fixed t))
1740 (setq sub (window-right sub))))
1741 ;; WINDOW is a live window.
1742 (and (or (not (windowp ignore)) (not (eq window ignore)))
1743 (or (and (not (eq ignore 'preserved))
1744 (window--preserve-size window horizontal))
1745 (with-current-buffer (window-buffer window)
1746 (if horizontal
1747 (memq window-size-fixed '(width t))
1748 (memq window-size-fixed '(height t))))))))))
1750 (defun window-size-fixed-p (&optional window horizontal ignore)
1751 "Return non-nil if WINDOW's height is fixed.
1752 WINDOW must be a valid window and defaults to the selected one.
1753 Optional argument HORIZONTAL non-nil means return non-nil if
1754 WINDOW's width is fixed. The optional argument IGNORE has the
1755 same meaning as for `window-resizable'.
1757 If this function returns nil, this does not necessarily mean that
1758 WINDOW can be resized in the desired direction. The function
1759 `window-resizable' can tell that."
1760 (when (or (windowp ignore) (memq ignore '(nil preserved)))
1761 (window--size-fixed-1
1762 (window-normalize-window window) horizontal ignore)))
1764 (defun window--min-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
1765 "Internal function for `window-min-delta'."
1766 (if (not (window-parent window))
1767 ;; If we can't go up, return zero.
1769 ;; Else try to find a non-fixed-size sibling of WINDOW.
1770 (let* ((parent (window-parent window))
1771 (sub (window-child parent)))
1772 (catch 'done
1773 (if (window-combined-p sub horizontal)
1774 ;; In an iso-combination throw DELTA if we find at least one
1775 ;; child window and that window is either not fixed-size or
1776 ;; we can ignore fixed-sizeness.
1777 (let ((skip (eq trail 'after)))
1778 (while sub
1779 (cond
1780 ((eq sub window)
1781 (setq skip (eq trail 'before)))
1782 (skip)
1783 ((window-size-fixed-p sub horizontal ignore))
1785 ;; We found a non-fixed-size child window.
1786 (throw 'done delta)))
1787 (setq sub (window-right sub))))
1788 ;; In an ortho-combination set DELTA to the minimum value by
1789 ;; which other child windows can shrink.
1790 (while sub
1791 (unless (eq sub window)
1792 (setq delta
1793 (min delta
1794 (max (- (window-size sub horizontal pixelwise 'ceiling)
1795 (window-min-size
1796 sub horizontal ignore pixelwise))
1797 0))))
1798 (setq sub (window-right sub))))
1799 (if noup
1800 delta
1801 (window--min-delta-1
1802 parent delta horizontal ignore trail nil pixelwise))))))
1804 (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise)
1805 "Return number of lines by which WINDOW can be shrunk.
1806 WINDOW must be a valid window and defaults to the selected one.
1807 Return zero if WINDOW cannot be shrunk.
1809 Optional argument HORIZONTAL non-nil means return number of
1810 columns by which WINDOW can be shrunk.
1812 The optional argument IGNORE has the same meaning as for
1813 `window-resizable'. Optional argument TRAIL restricts the
1814 windows that can be enlarged. If its value is `before', only
1815 windows to the left of or above WINDOW can be enlarged. If it is
1816 `after', only windows to the right of or below WINDOW can be
1817 enlarged.
1819 Optional argument NOUP non-nil means don't go up in the window
1820 tree, but try to enlarge windows within WINDOW's combination
1821 only. Optional argument NODOWN non-nil means don't check whether
1822 WINDOW itself (and its child windows) can be shrunk; check only
1823 whether at least one other window can be enlarged appropriately.
1825 Optional argument PIXELWISE non-nil means return number of pixels
1826 by which WINDOW can be shrunk."
1827 (setq window (window-normalize-window window))
1828 (let ((size (window-size window horizontal pixelwise 'floor))
1829 (minimum (window-min-size window horizontal ignore pixelwise)))
1830 (cond
1831 (nodown
1832 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1833 (window--min-delta-1
1834 window size horizontal ignore trail noup pixelwise))
1835 ((<= size minimum)
1836 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1837 ;; there's nothing to recover.
1840 ;; Otherwise, try to recover whatever WINDOW is larger than its
1841 ;; minimum size.
1842 (window--min-delta-1
1843 window (- size minimum) horizontal ignore trail noup pixelwise)))))
1845 (defun frame-windows-min-size (&optional frame horizontal ignore pixelwise)
1846 "Return minimum number of lines of FRAME's windows.
1847 HORIZONTAL non-nil means return number of columns of FRAME's
1848 windows. The optional argument IGNORE has the same meaning as
1849 for `window-resizable'. PIXELWISE non-nil means return sizes in
1850 pixels."
1851 (setq frame (window-normalize-frame frame))
1852 (let* ((root (frame-root-window frame))
1853 (mini (window-next-sibling root)))
1854 (+ (window-min-size root horizontal ignore pixelwise)
1855 (if (and mini (not horizontal))
1856 (window-min-size mini horizontal nil pixelwise)
1857 0))))
1859 (defun window--max-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
1860 "Internal function of `window-max-delta'."
1861 (if (not (window-parent window))
1862 ;; Can't go up. Return DELTA.
1863 delta
1864 (let* ((parent (window-parent window))
1865 (sub (window-child parent)))
1866 (catch 'fixed
1867 (if (window-combined-p sub horizontal)
1868 ;; For an iso-combination calculate how much we can get from
1869 ;; other child windows.
1870 (let ((skip (eq trail 'after)))
1871 (while sub
1872 (cond
1873 ((eq sub window)
1874 (setq skip (eq trail 'before)))
1875 (skip)
1877 (setq delta
1878 (+ delta
1879 (max
1880 (- (window-size sub horizontal pixelwise 'floor)
1881 (window-min-size
1882 sub horizontal ignore pixelwise))
1883 0)))))
1884 (setq sub (window-right sub))))
1885 ;; For an ortho-combination throw DELTA when at least one
1886 ;; child window is fixed-size.
1887 (while sub
1888 (when (and (not (eq sub window))
1889 (window-size-fixed-p sub horizontal ignore))
1890 (throw 'fixed delta))
1891 (setq sub (window-right sub))))
1892 (if noup
1893 ;; When NOUP is nil, DELTA is all we can get.
1894 delta
1895 ;; Else try with parent of WINDOW, passing the DELTA we
1896 ;; recovered so far.
1897 (window--max-delta-1
1898 parent delta horizontal ignore trail nil pixelwise))))))
1900 (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise)
1901 "Return maximum number of lines by which WINDOW can be enlarged.
1902 WINDOW must be a valid window and defaults to the selected one.
1903 The return value is zero if WINDOW cannot be enlarged.
1905 Optional argument HORIZONTAL non-nil means return maximum number
1906 of columns by which WINDOW can be enlarged.
1908 The optional argument IGNORE has the same meaning as for
1909 `window-resizable'. Optional argument TRAIL restricts the
1910 windows that can be enlarged. If its value is `before', only
1911 windows to the left of or above WINDOW can be enlarged. If it is
1912 `after', only windows to the right of or below WINDOW can be
1913 enlarged.
1915 Optional argument NOUP non-nil means don't go up in the window
1916 tree but try to obtain the entire space from windows within
1917 WINDOW's combination. Optional argument NODOWN non-nil means do
1918 not check whether WINDOW itself (and its child windows) can be
1919 enlarged; check only whether other windows can be shrunk
1920 appropriately.
1922 Optional argument PIXELWISE non-nil means return number of
1923 pixels by which WINDOW can be enlarged."
1924 (setq window (window-normalize-window window))
1925 (if (and (not nodown) (window-size-fixed-p window horizontal ignore))
1926 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1927 ;; size.
1929 ;; WINDOW has no fixed size.
1930 (window--max-delta-1 window 0 horizontal ignore trail noup pixelwise)))
1932 ;; Make NOUP also inhibit the min-size check.
1933 (defun window--resizable (window delta &optional horizontal ignore trail noup nodown pixelwise)
1934 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1935 WINDOW must be a valid window and defaults to the selected one.
1936 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1937 can be resized horizontally by DELTA columns. A return value of
1938 zero means that WINDOW is not resizable.
1940 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1941 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1942 return the maximum value in the range 0..DELTA by which WINDOW
1943 can be enlarged.
1945 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1946 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1947 return the minimum value in the range DELTA..0 that can be used
1948 for shrinking WINDOW.
1950 The optional argument IGNORE has the same meaning as for
1951 `window-resizable'. Optional argument TRAIL `before' means only
1952 windows to the left of or below WINDOW can be shrunk. Optional
1953 argument TRAIL `after' means only windows to the right of or
1954 above WINDOW can be shrunk.
1956 Optional argument NOUP non-nil means don't go up in the window
1957 tree but check only whether space can be obtained from (or given
1958 to) WINDOW's siblings. Optional argument NODOWN non-nil means
1959 don't go down in the window tree. This means do not check
1960 whether resizing would violate size restrictions of WINDOW or its
1961 child windows.
1963 Optional argument PIXELWISE non-nil means interpret DELTA as
1964 number of pixels."
1965 (setq window (window-normalize-window window))
1966 (cond
1967 ((< delta 0)
1968 (max (- (window-min-delta
1969 window horizontal ignore trail noup nodown pixelwise))
1970 delta))
1971 ((> delta 0)
1972 (min (window-max-delta
1973 window horizontal ignore trail noup nodown pixelwise)
1974 delta))
1975 (t 0)))
1977 (defun window--resizable-p (window delta &optional horizontal ignore trail noup nodown pixelwise)
1978 "Return t if WINDOW can be resized vertically by DELTA lines.
1979 WINDOW must be a valid window and defaults to the selected one.
1980 For the meaning of the arguments of this function see the
1981 doc-string of `window--resizable'.
1983 Optional argument PIXELWISE non-nil means interpret DELTA as
1984 pixels."
1985 (setq window (window-normalize-window window))
1986 (if (> delta 0)
1987 (>= (window--resizable
1988 window delta horizontal ignore trail noup nodown pixelwise)
1989 delta)
1990 (<= (window--resizable
1991 window delta horizontal ignore trail noup nodown pixelwise)
1992 delta)))
1994 (defun window-resizable (window delta &optional horizontal ignore pixelwise)
1995 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1996 WINDOW must be a valid window and defaults to the selected one.
1997 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1998 can be resized horizontally by DELTA columns. A return value of
1999 zero means that WINDOW is not resizable.
2001 DELTA positive means WINDOW shall be enlarged by DELTA lines or
2002 columns. If WINDOW cannot be enlarged by DELTA lines or columns
2003 return the maximum value in the range 0..DELTA by which WINDOW
2004 can be enlarged.
2006 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
2007 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
2008 return the minimum value in the range DELTA..0 that can be used
2009 for shrinking WINDOW.
2011 Optional argument IGNORE, if non-nil, means to ignore restraints
2012 induced by fixed size windows or the values of the variables
2013 `window-min-height' and `window-min-width'. The following values
2014 have special meanings: `safe' means that in addition live windows
2015 are allowed to get as small as `window-safe-min-height' lines and
2016 `window-safe-min-width' columns. `preserved' means to ignore
2017 only restrictions induced by `window-preserve-size'. If IGNORE
2018 is a window, then ignore restrictions for that window only.
2020 Optional argument PIXELWISE non-nil means interpret DELTA as
2021 pixels."
2022 (setq window (window-normalize-window window))
2023 (window--resizable window delta horizontal ignore nil nil nil pixelwise))
2025 (defun window-resizable-p (window delta &optional horizontal ignore pixelwise)
2026 "Return t if WINDOW can be resized vertically by DELTA lines.
2027 WINDOW must be a valid window and defaults to the selected one.
2028 For the meaning of the arguments of this function see the
2029 doc-string of `window-resizable'."
2030 (setq window (window-normalize-window window))
2031 (if (> delta 0)
2032 (>= (window--resizable
2033 window delta horizontal ignore nil nil nil pixelwise)
2034 delta)
2035 (<= (window--resizable
2036 window delta horizontal ignore nil nil nil pixelwise)
2037 delta)))
2039 ;; Aliases of functions defined in window.c.
2040 (defalias 'window-height 'window-total-height)
2041 (defalias 'window-width 'window-body-width)
2043 (defun window-full-height-p (&optional window)
2044 "Return t if WINDOW is as high as its containing frame.
2045 More precisely, return t if and only if the total height of
2046 WINDOW equals the total height of the root window of WINDOW's
2047 frame. WINDOW must be a valid window and defaults to the
2048 selected one."
2049 (setq window (window-normalize-window window))
2050 (if (window-minibuffer-p window)
2051 (eq window (frame-root-window (window-frame window)))
2052 (= (window-pixel-height window)
2053 (window-pixel-height (frame-root-window window)))))
2055 (defun window-full-width-p (&optional window)
2056 "Return t if WINDOW is as wide as its containing frame.
2057 More precisely, return t if and only if the total width of WINDOW
2058 equals the total width of the root window of WINDOW's frame.
2059 WINDOW must be a valid window and defaults to the selected one."
2060 (setq window (window-normalize-window window))
2061 (= (window-pixel-width window)
2062 (window-pixel-width (frame-root-window window))))
2064 (defun window-body-size (&optional window horizontal pixelwise)
2065 "Return the height or width of WINDOW's text area.
2066 WINDOW must be a live window and defaults to the selected one.
2068 If HORIZONTAL is omitted or nil, return the height of the text
2069 area, like `window-body-height'. Otherwise, return the width of
2070 the text area, like `window-body-width'. In either case, the
2071 optional argument PIXELWISE is passed to the functions."
2072 (if horizontal
2073 (window-body-width window pixelwise)
2074 (window-body-height window pixelwise)))
2076 (declare-function font-info "font.c" (name &optional frame))
2078 (defun window-font-width (&optional window face)
2079 "Return average character width for the font of FACE used in WINDOW.
2080 WINDOW must be a live window and defaults to the selected one.
2082 If FACE is nil or omitted, the default face is used. If FACE is
2083 remapped (see `face-remapping-alist'), the function returns the
2084 information for the remapped face."
2085 (with-selected-window (window-normalize-window window t)
2086 (if (display-multi-font-p)
2087 (let* ((face (if face face 'default))
2088 (info (font-info (face-font face)))
2089 (width (aref info 11)))
2090 (if (> width 0)
2091 width
2092 (aref info 10)))
2093 (frame-char-width))))
2095 (defun window-font-height (&optional window face)
2096 "Return character height for the font of FACE used in WINDOW.
2097 WINDOW must be a live window and defaults to the selected one.
2099 If FACE is nil or omitted, the default face is used. If FACE is
2100 remapped (see `face-remapping-alist'), the function returns the
2101 information for the remapped face."
2102 (with-selected-window (window-normalize-window window t)
2103 (if (display-multi-font-p)
2104 (let* ((face (if face face 'default))
2105 (info (font-info (face-font face))))
2106 (aref info 3))
2107 (frame-char-height))))
2109 (defvar overflow-newline-into-fringe)
2111 (defun window-max-chars-per-line (&optional window face)
2112 "Return the number of characters that can be displayed on one line in WINDOW.
2113 WINDOW must be a live window and defaults to the selected one.
2115 The character width of FACE is used for the calculation. If FACE
2116 is nil or omitted, the default face is used. If FACE is
2117 remapped (see `face-remapping-alist'), the function uses the
2118 remapped face.
2120 This function is different from `window-body-width' in two
2121 ways. First, it accounts for the portions of the line reserved
2122 for the continuation glyph. Second, it accounts for the size of
2123 the font."
2124 (with-selected-window (window-normalize-window window t)
2125 (let* ((window-width (window-body-width window t))
2126 (font-width (window-font-width window face))
2127 (ncols (/ window-width font-width)))
2128 (if (and (display-graphic-p)
2129 overflow-newline-into-fringe
2130 (not
2131 (or (eq left-fringe-width 0)
2132 (and (null left-fringe-width)
2133 (= (frame-parameter nil 'left-fringe) 0))))
2134 (not
2135 (or (eq right-fringe-width 0)
2136 (and (null right-fringe-width)
2137 (= (frame-parameter nil 'right-fringe) 0)))))
2138 ncols
2139 ;; FIXME: This should remove 1 more column when there are no
2140 ;; fringes, lines are truncated, and the window is hscrolled,
2141 ;; but EOL is not in the view, because then there are 2
2142 ;; truncation glyphs, not one.
2143 (1- ncols)))))
2145 (defun window-current-scroll-bars (&optional window)
2146 "Return the current scroll bar types for WINDOW.
2147 WINDOW must be a live window and defaults to the selected one.
2149 The return value is a cons cell (VERTICAL . HORIZONTAL) where
2150 VERTICAL specifies the current location of the vertical scroll
2151 bar (`left', `right' or nil), and HORIZONTAL specifies the
2152 current location of the horizontal scroll bar (`bottom' or nil).
2154 Unlike `window-scroll-bars', this function reports the scroll bar
2155 type actually used, once frame defaults and `scroll-bar-mode' are
2156 taken into account."
2157 (setq window (window-normalize-window window t))
2158 (let ((vertical (nth 2 (window-scroll-bars window)))
2159 (horizontal (nth 5 (window-scroll-bars window)))
2160 (inherited (frame-current-scroll-bars (window-frame window))))
2161 (when (eq vertical t)
2162 (setq vertical (car inherited)))
2163 (when (eq horizontal t)
2164 (setq horizontal (cdr inherited)))
2165 (cons vertical (and horizontal 'bottom))))
2167 (defun walk-windows (fun &optional minibuf all-frames)
2168 "Cycle through all live windows, calling FUN for each one.
2169 FUN must specify a function with a window as its sole argument.
2170 The optional arguments MINIBUF and ALL-FRAMES specify the set of
2171 windows to include in the walk.
2173 MINIBUF t means include the minibuffer window even if the
2174 minibuffer is not active. MINIBUF nil or omitted means include
2175 the minibuffer window only if the minibuffer is active. Any
2176 other value means do not include the minibuffer window even if
2177 the minibuffer is active.
2179 ALL-FRAMES nil or omitted means consider all windows on the
2180 selected frame, plus the minibuffer window if specified by the
2181 MINIBUF argument. If the minibuffer counts, consider all windows
2182 on all frames that share that minibuffer too. The following
2183 non-nil values of ALL-FRAMES have special meanings:
2185 - t means consider all windows on all existing frames.
2187 - `visible' means consider all windows on all visible frames on
2188 the current terminal.
2190 - 0 (the number zero) means consider all windows on all visible
2191 and iconified frames on the current terminal.
2193 - A frame means consider all windows on that frame only.
2195 Anything else means consider all windows on the selected frame
2196 and no others.
2198 This function changes neither the order of recently selected
2199 windows nor the buffer list."
2200 ;; If we start from the minibuffer window, don't fail to come
2201 ;; back to it.
2202 (when (window-minibuffer-p)
2203 (setq minibuf t))
2204 ;; Make sure to not mess up the order of recently selected
2205 ;; windows. Use `save-selected-window' and `select-window'
2206 ;; with second argument non-nil for this purpose.
2207 (save-selected-window
2208 (when (framep all-frames)
2209 (select-window (frame-first-window all-frames) 'norecord))
2210 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
2211 (funcall fun walk-windows-window))))
2213 (defun window-at-side-p (&optional window side)
2214 "Return t if WINDOW is at SIDE of its containing frame.
2215 WINDOW must be a valid window and defaults to the selected one.
2216 SIDE can be any of the symbols `left', `top', `right' or
2217 `bottom'. The default value nil is handled like `bottom'."
2218 (setq window (window-normalize-window window))
2219 (let ((edge
2220 (cond
2221 ((eq side 'left) 0)
2222 ((eq side 'top) 1)
2223 ((eq side 'right) 2)
2224 ((memq side '(bottom nil)) 3))))
2225 (= (nth edge (window-pixel-edges window))
2226 (nth edge (window-pixel-edges (frame-root-window window))))))
2228 (defun window-at-side-list (&optional frame side)
2229 "Return list of all windows on SIDE of FRAME.
2230 FRAME must be a live frame and defaults to the selected frame.
2231 SIDE can be any of the symbols `left', `top', `right' or
2232 `bottom'. The default value nil is handled like `bottom'."
2233 (setq frame (window-normalize-frame frame))
2234 (let (windows)
2235 (walk-window-tree
2236 (lambda (window)
2237 (when (window-at-side-p window side)
2238 (setq windows (cons window windows))))
2239 frame nil 'nomini)
2240 (nreverse windows)))
2242 (defun window--in-direction-2 (window posn &optional horizontal)
2243 "Support function for `window-in-direction'."
2244 (if horizontal
2245 (let ((top (window-pixel-top window)))
2246 (if (> top posn)
2247 (- top posn)
2248 (- posn top (window-pixel-height window))))
2249 (let ((left (window-pixel-left window)))
2250 (if (> left posn)
2251 (- left posn)
2252 (- posn left (window-pixel-width window))))))
2254 ;; Predecessors to the below have been devised by Julian Assange in
2255 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
2256 ;; Neither of these allow one to selectively ignore specific windows
2257 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
2258 ;; the movement.
2259 (defun window-in-direction (direction &optional window ignore sign wrap mini)
2260 "Return window in DIRECTION as seen from WINDOW.
2261 More precisely, return the nearest window in direction DIRECTION
2262 as seen from the position of `window-point' in window WINDOW.
2263 DIRECTION must be one of `above', `below', `left' or `right'.
2264 WINDOW must be a live window and defaults to the selected one.
2266 Do not return a window whose `no-other-window' parameter is
2267 non-nil. If the nearest window's `no-other-window' parameter is
2268 non-nil, try to find another window in the indicated direction.
2269 If, however, the optional argument IGNORE is non-nil, return that
2270 window even if its `no-other-window' parameter is non-nil.
2272 Optional argument SIGN a negative number means to use the right
2273 or bottom edge of WINDOW as reference position instead of
2274 `window-point'. SIGN a positive number means to use the left or
2275 top edge of WINDOW as reference position.
2277 Optional argument WRAP non-nil means to wrap DIRECTION around
2278 frame borders. This means to return for WINDOW at the top of the
2279 frame and DIRECTION `above' the minibuffer window if the frame
2280 has one, and a window at the bottom of the frame otherwise.
2282 Optional argument MINI nil means to return the minibuffer window
2283 if and only if it is currently active. MINI non-nil means to
2284 return the minibuffer window even when it's not active. However,
2285 if WRAP is non-nil, always act as if MINI were nil.
2287 Return nil if no suitable window can be found."
2288 (setq window (window-normalize-window window t))
2289 (unless (memq direction '(above below left right))
2290 (error "Wrong direction %s" direction))
2291 (let* ((frame (window-frame window))
2292 (hor (memq direction '(left right)))
2293 (first (if hor
2294 (window-pixel-left window)
2295 (window-pixel-top window)))
2296 (last (+ first (window-size window hor t)))
2297 ;; The column / row value of `posn-at-point' can be nil for the
2298 ;; mini-window, guard against that.
2299 (posn
2300 (cond
2301 ((and (numberp sign) (< sign 0))
2302 (if hor
2303 (1- (+ (window-pixel-top window) (window-pixel-height window)))
2304 (1- (+ (window-pixel-left window) (window-pixel-width window)))))
2305 ((and (numberp sign) (> sign 0))
2306 (if hor
2307 (window-pixel-top window)
2308 (window-pixel-left window)))
2309 ((let ((posn-cons (nth 2 (posn-at-point (window-point window) window))))
2310 (if hor
2311 (+ (or (cdr posn-cons) 1) (window-pixel-top window))
2312 (+ (or (car posn-cons) 1) (window-pixel-left window)))))))
2313 (best-edge
2314 (cond
2315 ((eq direction 'below) (frame-pixel-height frame))
2316 ((eq direction 'right) (frame-pixel-width frame))
2317 (t -1)))
2318 (best-edge-2 best-edge)
2319 (best-diff-2 (if hor (frame-pixel-height frame) (frame-pixel-width frame)))
2320 best best-2 best-diff-2-new)
2321 (walk-window-tree
2322 (lambda (w)
2323 (let* ((w-top (window-pixel-top w))
2324 (w-left (window-pixel-left w)))
2325 (cond
2326 ((or (eq window w)
2327 ;; Ignore ourselves.
2328 (and (window-parameter w 'no-other-window)
2329 ;; Ignore W unless IGNORE is non-nil.
2330 (not ignore))))
2331 (hor
2332 (cond
2333 ((and (<= w-top posn)
2334 (< posn (+ w-top (window-pixel-height w))))
2335 ;; W is to the left or right of WINDOW and covers POSN.
2336 (when (or (and (eq direction 'left)
2337 (or (and (<= w-left first) (> w-left best-edge))
2338 (and wrap
2339 (window-at-side-p window 'left)
2340 (window-at-side-p w 'right))))
2341 (and (eq direction 'right)
2342 (or (and (>= w-left last) (< w-left best-edge))
2343 (and wrap
2344 (window-at-side-p window 'right)
2345 (window-at-side-p w 'left)))))
2346 (setq best-edge w-left)
2347 (setq best w)))
2348 ((and (or (and (eq direction 'left)
2349 (<= (+ w-left (window-pixel-width w)) first))
2350 (and (eq direction 'right) (<= last w-left)))
2351 ;; W is to the left or right of WINDOW but does not
2352 ;; cover POSN.
2353 (setq best-diff-2-new
2354 (window--in-direction-2 w posn hor))
2355 (or (< best-diff-2-new best-diff-2)
2356 (and (= best-diff-2-new best-diff-2)
2357 (if (eq direction 'left)
2358 (> w-left best-edge-2)
2359 (< w-left best-edge-2)))))
2360 (setq best-edge-2 w-left)
2361 (setq best-diff-2 best-diff-2-new)
2362 (setq best-2 w))))
2363 ((and (<= w-left posn)
2364 (< posn (+ w-left (window-pixel-width w))))
2365 ;; W is above or below WINDOW and covers POSN.
2366 (when (or (and (eq direction 'above)
2367 (or (and (<= w-top first) (> w-top best-edge))
2368 (and wrap
2369 (window-at-side-p window 'top)
2370 (if (active-minibuffer-window)
2371 (minibuffer-window-active-p w)
2372 (window-at-side-p w 'bottom)))))
2373 (and (eq direction 'below)
2374 (or (and (>= w-top first) (< w-top best-edge))
2375 (and wrap
2376 (if (active-minibuffer-window)
2377 (minibuffer-window-active-p window)
2378 (window-at-side-p window 'bottom))
2379 (window-at-side-p w 'top)))))
2380 (setq best-edge w-top)
2381 (setq best w)))
2382 ((and (or (and (eq direction 'above)
2383 (<= (+ w-top (window-pixel-height w)) first))
2384 (and (eq direction 'below) (<= last w-top)))
2385 ;; W is above or below WINDOW but does not cover POSN.
2386 (setq best-diff-2-new
2387 (window--in-direction-2 w posn hor))
2388 (or (< best-diff-2-new best-diff-2)
2389 (and (= best-diff-2-new best-diff-2)
2390 (if (eq direction 'above)
2391 (> w-top best-edge-2)
2392 (< w-top best-edge-2)))))
2393 (setq best-edge-2 w-top)
2394 (setq best-diff-2 best-diff-2-new)
2395 (setq best-2 w)))))
2396 frame nil (and mini t))
2397 (or best best-2)))
2399 (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
2400 "Return a live window satisfying PREDICATE.
2401 More precisely, cycle through all windows calling the function
2402 PREDICATE on each one of them with the window as its sole
2403 argument. Return the first window for which PREDICATE returns
2404 non-nil. Windows are scanned starting with the window following
2405 the selected window. If no window satisfies PREDICATE, return
2406 DEFAULT.
2408 MINIBUF t means include the minibuffer window even if the
2409 minibuffer is not active. MINIBUF nil or omitted means include
2410 the minibuffer window only if the minibuffer is active. Any
2411 other value means do not include the minibuffer window even if
2412 the minibuffer is active.
2414 ALL-FRAMES nil or omitted means consider all windows on the selected
2415 frame, plus the minibuffer window if specified by the MINIBUF
2416 argument. If the minibuffer counts, consider all windows on all
2417 frames that share that minibuffer too. The following non-nil
2418 values of ALL-FRAMES have special meanings:
2420 - t means consider all windows on all existing frames.
2422 - `visible' means consider all windows on all visible frames on
2423 the current terminal.
2425 - 0 (the number zero) means consider all windows on all visible
2426 and iconified frames on the current terminal.
2428 - A frame means consider all windows on that frame only.
2430 Anything else means consider all windows on the selected frame
2431 and no others."
2432 (catch 'found
2433 (dolist (window (window-list-1
2434 (next-window nil minibuf all-frames)
2435 minibuf all-frames))
2436 (when (funcall predicate window)
2437 (throw 'found window)))
2438 default))
2440 (defalias 'some-window 'get-window-with-predicate)
2442 (defun get-lru-window (&optional all-frames dedicated not-selected)
2443 "Return the least recently used window on frames specified by ALL-FRAMES.
2444 Return a full-width window if possible. A minibuffer window is
2445 never a candidate. A dedicated window is never a candidate
2446 unless DEDICATED is non-nil, so if all windows are dedicated, the
2447 value is nil. Avoid returning the selected window if possible.
2448 Optional argument NOT-SELECTED non-nil means never return the
2449 selected window.
2451 The following non-nil values of the optional argument ALL-FRAMES
2452 have special meanings:
2454 - t means consider all windows on all existing frames.
2456 - `visible' means consider all windows on all visible frames on
2457 the current terminal.
2459 - 0 (the number zero) means consider all windows on all visible
2460 and iconified frames on the current terminal.
2462 - A frame means consider all windows on that frame only.
2464 Any other value of ALL-FRAMES means consider all windows on the
2465 selected frame and no others."
2466 (let (best-window best-time second-best-window second-best-time time)
2467 (dolist (window (window-list-1 nil 'nomini all-frames))
2468 (when (and (or dedicated (not (window-dedicated-p window)))
2469 (or (not not-selected) (not (eq window (selected-window)))))
2470 (setq time (window-use-time window))
2471 (if (or (eq window (selected-window))
2472 (not (window-full-width-p window)))
2473 (when (or (not second-best-time) (< time second-best-time))
2474 (setq second-best-time time)
2475 (setq second-best-window window))
2476 (when (or (not best-time) (< time best-time))
2477 (setq best-time time)
2478 (setq best-window window)))))
2479 (or best-window second-best-window)))
2481 (defun get-mru-window (&optional all-frames dedicated not-selected)
2482 "Return the most recently used window on frames specified by ALL-FRAMES.
2483 A minibuffer window is never a candidate. A dedicated window is
2484 never a candidate unless DEDICATED is non-nil, so if all windows
2485 are dedicated, the value is nil. Optional argument NOT-SELECTED
2486 non-nil means never return the selected window.
2488 The following non-nil values of the optional argument ALL-FRAMES
2489 have special meanings:
2491 - t means consider all windows on all existing frames.
2493 - `visible' means consider all windows on all visible frames on
2494 the current terminal.
2496 - 0 (the number zero) means consider all windows on all visible
2497 and iconified frames on the current terminal.
2499 - A frame means consider all windows on that frame only.
2501 Any other value of ALL-FRAMES means consider all windows on the
2502 selected frame and no others."
2503 (let (best-window best-time time)
2504 (dolist (window (window-list-1 nil 'nomini all-frames))
2505 (setq time (window-use-time window))
2506 (when (and (or dedicated (not (window-dedicated-p window)))
2507 (or (not not-selected) (not (eq window (selected-window))))
2508 (or (not best-time) (> time best-time)))
2509 (setq best-time time)
2510 (setq best-window window)))
2511 best-window))
2513 (defun get-largest-window (&optional all-frames dedicated not-selected)
2514 "Return the largest window on frames specified by ALL-FRAMES.
2515 A minibuffer window is never a candidate. A dedicated window is
2516 never a candidate unless DEDICATED is non-nil, so if all windows
2517 are dedicated, the value is nil. Optional argument NOT-SELECTED
2518 non-nil means never return the selected window.
2520 The following non-nil values of the optional argument ALL-FRAMES
2521 have special meanings:
2523 - t means consider all windows on all existing frames.
2525 - `visible' means consider all windows on all visible frames on
2526 the current terminal.
2528 - 0 (the number zero) means consider all windows on all visible
2529 and iconified frames on the current terminal.
2531 - A frame means consider all windows on that frame only.
2533 Any other value of ALL-FRAMES means consider all windows on the
2534 selected frame and no others."
2535 (let ((best-size 0)
2536 best-window size)
2537 (dolist (window (window-list-1 nil 'nomini all-frames))
2538 (when (and (or dedicated (not (window-dedicated-p window)))
2539 (or (not not-selected) (not (eq window (selected-window)))))
2540 (setq size (* (window-pixel-height window)
2541 (window-pixel-width window)))
2542 (when (> size best-size)
2543 (setq best-size size)
2544 (setq best-window window))))
2545 best-window))
2547 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
2548 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2549 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2550 and defaults to the current buffer. If the selected window displays
2551 BUFFER-OR-NAME, it will be the first in the resulting list.
2553 MINIBUF t means include the minibuffer window even if the
2554 minibuffer is not active. MINIBUF nil or omitted means include
2555 the minibuffer window only if the minibuffer is active. Any
2556 other value means do not include the minibuffer window even if
2557 the minibuffer is active.
2559 ALL-FRAMES nil or omitted means consider all windows on the
2560 selected frame, plus the minibuffer window if specified by the
2561 MINIBUF argument. If the minibuffer counts, consider all windows
2562 on all frames that share that minibuffer too. The following
2563 non-nil values of ALL-FRAMES have special meanings:
2565 - t means consider all windows on all existing frames.
2567 - `visible' means consider all windows on all visible frames on
2568 the current terminal.
2570 - 0 (the number zero) means consider all windows on all visible
2571 and iconified frames on the current terminal.
2573 - A frame means consider all windows on that frame only.
2575 Anything else means consider all windows on the selected frame
2576 and no others."
2577 (let ((buffer (window-normalize-buffer buffer-or-name))
2578 windows)
2579 (dolist (window (window-list-1 (selected-window) minibuf all-frames))
2580 (when (eq (window-buffer window) buffer)
2581 (setq windows (cons window windows))))
2582 (nreverse windows)))
2584 (defun minibuffer-window-active-p (window)
2585 "Return t if WINDOW is the currently active minibuffer window."
2586 (eq window (active-minibuffer-window)))
2588 (defun count-windows (&optional minibuf)
2589 "Return the number of live windows on the selected frame.
2590 The optional argument MINIBUF specifies whether the minibuffer
2591 window shall be counted. See `walk-windows' for the precise
2592 meaning of this argument."
2593 (length (window-list-1 nil minibuf)))
2595 ;;; Resizing windows.
2596 (defun window--size-to-pixel (window size &optional horizontal pixelwise round-maybe)
2597 "For WINDOW convert SIZE lines to pixels.
2598 SIZE is supposed to specify a height of WINDOW in terms of text
2599 lines. The return value is the number of pixels specifying that
2600 height.
2602 WINDOW must be a valid window. Optional argument HORIZONTAL
2603 non-nil means convert SIZE columns to pixels.
2605 Optional argument PIXELWISE non-nil means SIZE already specifies
2606 pixels but may have to be adjusted to a multiple of the character
2607 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2608 means round to the nearest multiple of the character size of
2609 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2610 (setq window (window-normalize-window window))
2611 (let ((char-size (frame-char-size window horizontal)))
2612 (if pixelwise
2613 (if (and round-maybe (not window-resize-pixelwise))
2614 (* (round size char-size) char-size)
2615 size)
2616 (* size char-size))))
2618 (defun window--pixel-to-total-1 (window horizontal char-size)
2619 "Subroutine of `window--pixel-to-total'."
2620 (let ((child (window-child window)))
2621 (if (window-combination-p window horizontal)
2622 ;; In an iso-combination distribute sizes proportionally.
2623 (let ((remainder (window-new-total window))
2624 size best-child rem best-rem)
2625 ;; Initialize total sizes to each child's floor.
2626 (while child
2627 (setq size (max (/ (window-size child horizontal t) char-size) 1))
2628 (set-window-new-total child size)
2629 (setq remainder (- remainder size))
2630 (setq child (window-next-sibling child)))
2631 ;; Distribute remainder.
2632 (while (> remainder 0)
2633 (setq child (window-last-child window))
2634 (setq best-child nil)
2635 (setq best-rem 0)
2636 (while child
2637 (when (and (<= (window-new-total child)
2638 (/ (window-size child horizontal t) char-size))
2639 (> (setq rem (% (window-size child horizontal t)
2640 char-size))
2641 best-rem))
2642 (setq best-child child)
2643 (setq best-rem rem))
2644 (setq child (window-prev-sibling child)))
2645 ;; We MUST have a best-child here.
2646 (set-window-new-total best-child 1 t)
2647 (setq remainder (1- remainder)))
2648 ;; Recurse.
2649 (setq child (window-child window))
2650 (while child
2651 (window--pixel-to-total-1 child horizontal char-size)
2652 (setq child (window-next-sibling child))))
2653 ;; In an ortho-combination assign new sizes directly.
2654 (let ((size (window-new-total window)))
2655 (while child
2656 (set-window-new-total child size)
2657 (window--pixel-to-total-1 child horizontal char-size)
2658 (setq child (window-next-sibling child)))))))
2660 (defun window--pixel-to-total (&optional frame horizontal)
2661 "On FRAME assign new total window heights from pixel heights.
2662 FRAME must be a live frame and defaults to the selected frame.
2664 Optional argument HORIZONTAL non-nil means assign new total
2665 window widths from pixel widths."
2666 (setq frame (window-normalize-frame frame))
2667 (let* ((char-size (frame-char-size frame horizontal))
2668 (root (frame-root-window frame))
2669 (root-size (window-size root horizontal t))
2670 ;; We have to care about the minibuffer window only if it
2671 ;; appears together with the root window on this frame.
2672 (mini (let ((mini (minibuffer-window frame)))
2673 (and (eq (window-frame mini) frame)
2674 (not (eq mini root)) mini)))
2675 (mini-size (and mini (window-size mini horizontal t))))
2676 ;; We round the line/column sizes of windows here to the nearest
2677 ;; integer. In some cases this can make windows appear _larger_
2678 ;; than the containing frame (line/column-wise) because the latter's
2679 ;; sizes are not (yet) rounded. We might eventually fix that.
2680 (if (and mini (not horizontal))
2681 (let (lines)
2682 (set-window-new-total root (max (/ root-size char-size) 1))
2683 (set-window-new-total mini (max (/ mini-size char-size) 1))
2684 (setq lines (- (round (+ root-size mini-size) char-size)
2685 (+ (window-new-total root) (window-new-total mini))))
2686 (while (> lines 0)
2687 (if (>= (% root-size (window-new-total root))
2688 (% mini-size (window-new-total mini)))
2689 (set-window-new-total root 1 t)
2690 (set-window-new-total mini 1 t))
2691 (setq lines (1- lines))))
2692 (set-window-new-total root (round root-size char-size))
2693 (when mini
2694 ;; This is taken in the horizontal case only.
2695 (set-window-new-total mini (round mini-size char-size))))
2696 (unless (window-buffer root)
2697 (window--pixel-to-total-1 root horizontal char-size))
2698 ;; Apply the new sizes.
2699 (window-resize-apply-total frame horizontal)))
2701 (defun window--resize-reset (&optional frame horizontal)
2702 "Reset resize values for all windows on FRAME.
2703 FRAME defaults to the selected frame.
2705 This function stores the current value of `window-size' applied
2706 with argument HORIZONTAL in the new total size of all windows on
2707 FRAME. It also resets the new normal size of each of these
2708 windows."
2709 (window--resize-reset-1
2710 (frame-root-window (window-normalize-frame frame)) horizontal))
2712 (defun window--resize-reset-1 (window horizontal)
2713 "Internal function of `window--resize-reset'."
2714 ;; Register old size in the new total size.
2715 (set-window-new-pixel window (window-size window horizontal t))
2716 (set-window-new-total window (window-size window horizontal))
2717 ;; Reset new normal size.
2718 (set-window-new-normal window)
2719 (when (window-child window)
2720 (window--resize-reset-1 (window-child window) horizontal))
2721 (when (window-right window)
2722 (window--resize-reset-1 (window-right window) horizontal)))
2724 (defun window--resize-mini-window (window delta)
2725 "Resize minibuffer window WINDOW by DELTA pixels.
2726 If WINDOW cannot be resized by DELTA pixels make it as large (or
2727 as small) as possible, but don't signal an error."
2728 (when (window-minibuffer-p window)
2729 (let* ((frame (window-frame window))
2730 (root (frame-root-window frame))
2731 (height (window-pixel-height window))
2732 (min-delta
2733 (- (window-pixel-height root)
2734 (window-min-size root nil nil t))))
2735 ;; Sanitize DELTA.
2736 (cond
2737 ((<= (+ height delta) 0)
2738 (setq delta (- (frame-char-height (window-frame window)) height)))
2739 ((> delta min-delta)
2740 (setq delta min-delta)))
2742 (unless (zerop delta)
2743 ;; Resize now.
2744 (window--resize-reset frame)
2745 ;; Ideally we should be able to resize just the last child of root
2746 ;; here. See the comment in `resize-root-window-vertically' for
2747 ;; why we do not do that.
2748 (window--resize-this-window root (- delta) nil nil t)
2749 (set-window-new-pixel window (+ height delta))
2750 ;; The following routine catches the case where we want to resize
2751 ;; a minibuffer-only frame.
2752 (when (resize-mini-window-internal window)
2753 (window--pixel-to-total frame)
2754 (run-window-configuration-change-hook frame))))))
2756 (defun window--resize-apply-p (frame &optional horizontal)
2757 "Return t when a window on FRAME shall be resized vertically.
2758 Optional argument HORIZONTAL non-nil means return t when a window
2759 shall be resized horizontally."
2760 (catch 'apply
2761 (walk-window-tree
2762 (lambda (window)
2763 (unless (= (window-new-pixel window)
2764 (window-size window horizontal t))
2765 (throw 'apply t)))
2766 frame t)
2767 nil))
2769 (defun window-resize (window delta &optional horizontal ignore pixelwise)
2770 "Resize WINDOW vertically by DELTA lines.
2771 WINDOW can be an arbitrary window and defaults to the selected
2772 one. An attempt to resize the root window of a frame will raise
2773 an error though.
2775 DELTA a positive number means WINDOW shall be enlarged by DELTA
2776 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2777 lines.
2779 Optional argument HORIZONTAL non-nil means resize WINDOW
2780 horizontally by DELTA columns. In this case a positive DELTA
2781 means enlarge WINDOW by DELTA columns. DELTA negative means
2782 WINDOW shall be shrunk by -DELTA columns.
2784 Optional argument IGNORE, if non-nil, means to ignore restraints
2785 induced by fixed size windows or the values of the variables
2786 `window-min-height' and `window-min-width'. The following values
2787 have special meanings: `safe' means that in addition live windows
2788 are allowed to get as small as `window-safe-min-height' lines and
2789 `window-safe-min-width' columns. `preserved' means to ignore
2790 only restrictions induced by `window-preserve-size'. If IGNORE
2791 is a window, then ignore restrictions for that window only.
2793 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2794 pixels.
2796 This function resizes other windows proportionally and never
2797 deletes any windows. If you want to move only the low (right)
2798 edge of WINDOW consider using `adjust-window-trailing-edge'
2799 instead."
2800 (setq window (window-normalize-window window))
2801 (let* ((frame (window-frame window))
2802 (minibuffer-window (minibuffer-window frame))
2803 sibling)
2804 (setq delta (window--size-to-pixel
2805 window delta horizontal pixelwise t))
2806 (cond
2807 ((eq window (frame-root-window frame))
2808 (error "Cannot resize the root window of a frame"))
2809 ((window-minibuffer-p window)
2810 (if horizontal
2811 (error "Cannot resize minibuffer window horizontally")
2812 (window--resize-mini-window window delta)))
2813 ((and (not horizontal)
2814 (window-full-height-p window)
2815 (eq (window-frame minibuffer-window) frame)
2816 (or (not resize-mini-windows)
2817 (eq minibuffer-window (active-minibuffer-window))))
2818 ;; If WINDOW is full height and either `resize-mini-windows' is
2819 ;; nil or the minibuffer window is active, resize the minibuffer
2820 ;; window.
2821 (window--resize-mini-window minibuffer-window (- delta)))
2822 ((or (window--resizable-p
2823 window delta horizontal ignore nil nil nil t)
2824 (and (not ignore)
2825 (setq ignore 'preserved)
2826 (window--resizable-p
2827 window delta horizontal ignore nil nil nil t)))
2828 (window--resize-reset frame horizontal)
2829 (window--resize-this-window window delta horizontal ignore t)
2830 (if (and (not (eq window-combination-resize t))
2831 (window-combined-p window horizontal)
2832 (setq sibling (or (window-right window) (window-left window)))
2833 (window-sizable-p
2834 sibling (- delta) horizontal ignore t))
2835 ;; If window-combination-resize is nil, WINDOW is part of an
2836 ;; iso-combination, and WINDOW's neighboring right or left
2837 ;; sibling can be resized as requested, resize that sibling.
2838 (let ((normal-delta
2839 (/ (float delta)
2840 (window-size (window-parent window) horizontal t))))
2841 (window--resize-this-window sibling (- delta) horizontal nil t)
2842 (set-window-new-normal
2843 window (+ (window-normal-size window horizontal)
2844 normal-delta))
2845 (set-window-new-normal
2846 sibling (- (window-normal-size sibling horizontal)
2847 normal-delta)))
2848 ;; Otherwise, resize all other windows in the same combination.
2849 (window--resize-siblings window delta horizontal ignore))
2850 (when (window--resize-apply-p frame horizontal)
2851 (if (window-resize-apply frame horizontal)
2852 (progn
2853 (window--pixel-to-total frame horizontal)
2854 (run-window-configuration-change-hook frame))
2855 (error "Failed to apply resizing %s" window))))
2857 (error "Cannot resize window %s" window)))))
2859 (defun window-resize-no-error (window delta &optional horizontal ignore pixelwise)
2860 "Resize WINDOW vertically if it is resizable by DELTA lines.
2861 This function is like `window-resize' but does not signal an
2862 error when WINDOW cannot be resized. For the meaning of the
2863 optional arguments see the documentation of `window-resize'."
2864 (when (window--resizable-p
2865 window delta horizontal ignore nil nil nil pixelwise)
2866 (window-resize window delta horizontal ignore pixelwise)))
2868 (defun window--resize-child-windows-skip-p (window)
2869 "Return non-nil if WINDOW shall be skipped by resizing routines."
2870 (memq (window-new-normal window) '(ignore stuck skip)))
2872 (defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
2873 "Recursively set new normal height of child windows of window PARENT.
2874 HORIZONTAL non-nil means set the new normal width of these
2875 windows. WINDOW specifies a child window of PARENT that has been
2876 resized by THIS-DELTA lines (columns).
2878 Optional argument TRAIL either `before' or `after' means set values
2879 only for windows before or after WINDOW. Optional argument
2880 OTHER-DELTA, a number, specifies that this many lines (columns)
2881 have been obtained from (or returned to) an ancestor window of
2882 PARENT in order to resize WINDOW."
2883 (let* ((delta-normal
2884 (if (and (= (- this-delta)
2885 (window-size window horizontal t))
2886 (zerop other-delta))
2887 ;; When WINDOW gets deleted and we can return its entire
2888 ;; space to its siblings, use WINDOW's normal size as the
2889 ;; normal delta.
2890 (- (window-normal-size window horizontal))
2891 ;; In any other case calculate the normal delta from the
2892 ;; relation of THIS-DELTA to the total size of PARENT.
2893 (/ (float this-delta)
2894 (window-size parent horizontal t))))
2895 (sub (window-child parent))
2896 (parent-normal 0.0)
2897 (skip (eq trail 'after)))
2899 ;; Set parent-normal to the sum of the normal sizes of all child
2900 ;; windows of PARENT that shall be resized, excluding only WINDOW
2901 ;; and any windows specified by the optional TRAIL argument.
2902 (while sub
2903 (cond
2904 ((eq sub window)
2905 (setq skip (eq trail 'before)))
2906 (skip)
2908 (setq parent-normal
2909 (+ parent-normal (window-normal-size sub horizontal)))))
2910 (setq sub (window-right sub)))
2912 ;; Set the new normal size of all child windows of PARENT from what
2913 ;; they should have contributed for recovering THIS-DELTA lines
2914 ;; (columns).
2915 (setq sub (window-child parent))
2916 (setq skip (eq trail 'after))
2917 (while sub
2918 (cond
2919 ((eq sub window)
2920 (setq skip (eq trail 'before)))
2921 (skip)
2923 (let ((old-normal (window-normal-size sub horizontal)))
2924 (set-window-new-normal
2925 sub (min 1.0 ; Don't get larger than 1.
2926 (max (- old-normal
2927 (* (/ old-normal parent-normal)
2928 delta-normal))
2929 ;; Don't drop below 0.
2930 0.0))))))
2931 (setq sub (window-right sub)))
2933 (when (numberp other-delta)
2934 ;; Set the new normal size of windows from what they should have
2935 ;; contributed for recovering OTHER-DELTA lines (columns).
2936 (setq delta-normal (/ (float (window-size parent horizontal t))
2937 (+ (window-size parent horizontal t)
2938 other-delta)))
2939 (setq sub (window-child parent))
2940 (setq skip (eq trail 'after))
2941 (while sub
2942 (cond
2943 ((eq sub window)
2944 (setq skip (eq trail 'before)))
2945 (skip)
2947 (set-window-new-normal
2948 sub (min 1.0 ; Don't get larger than 1.
2949 (max (* (window-new-normal sub) delta-normal)
2950 ;; Don't drop below 0.
2951 0.0)))))
2952 (setq sub (window-right sub))))
2954 ;; Set the new normal size of WINDOW to what is left by the sum of
2955 ;; the normal sizes of its siblings.
2956 (set-window-new-normal
2957 window
2958 (let ((sum 0))
2959 (setq sub (window-child parent))
2960 (while sub
2961 (cond
2962 ((eq sub window))
2963 ((not (numberp (window-new-normal sub)))
2964 (setq sum (+ sum (window-normal-size sub horizontal))))
2966 (setq sum (+ sum (window-new-normal sub)))))
2967 (setq sub (window-right sub)))
2968 ;; Don't get larger than 1 or smaller than 0.
2969 (min 1.0 (max (- 1.0 sum) 0.0))))))
2971 (defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge char-size)
2972 "Resize child windows of window PARENT vertically by DELTA pixels.
2973 PARENT must be a vertically combined internal window.
2975 Optional argument HORIZONTAL non-nil means resize child windows
2976 of PARENT horizontally by DELTA pixels. In this case PARENT must
2977 be a horizontally combined internal window.
2979 WINDOW, if specified, must denote a child window of PARENT that
2980 is resized by DELTA pixels.
2982 The optional argument IGNORE has the same meaning as for
2983 `window-resizable'.
2985 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2986 of windows that shall be resized. If TRAIL equals `before',
2987 resize only windows on the left or above EDGE. If TRAIL equals
2988 `after', resize only windows on the right or below EDGE. Also,
2989 preferably only resize windows adjacent to EDGE.
2991 If the optional argument CHAR-SIZE is a positive integer, it specifies
2992 the number of pixels by which windows are incrementally resized.
2993 If CHAR-SIZE is nil, this means to use the value of
2994 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2996 Return the symbol `normalized' if new normal sizes have been
2997 already set by this routine."
2998 (let* ((first (window-child parent))
2999 (last (window-last-child parent))
3000 (parent-total (+ (window-size parent horizontal t)
3001 delta))
3002 (char-size (or char-size
3003 (and window-resize-pixelwise 1)
3004 (frame-char-size window horizontal)))
3005 sub best-window best-value best-delta)
3007 (if (and edge (memq trail '(before after))
3008 (progn
3009 (setq sub first)
3010 (while (and (window-right sub)
3011 (or (and (eq trail 'before)
3012 (not (window--resize-child-windows-skip-p
3013 (window-right sub))))
3014 (and (eq trail 'after)
3015 (window--resize-child-windows-skip-p sub))))
3016 (setq sub (window-right sub)))
3017 sub)
3018 (if horizontal
3019 (if (eq trail 'before)
3020 (= (+ (window-pixel-left sub) (window-pixel-width sub))
3021 edge)
3022 (= (window-pixel-left sub) edge))
3023 (if (eq trail 'before)
3024 (= (+ (window-pixel-top sub) (window-pixel-height sub))
3025 edge)
3026 (= (window-pixel-top sub) edge)))
3027 (window-sizable-p sub delta horizontal ignore t))
3028 ;; Resize only windows adjacent to EDGE.
3029 (progn
3030 (window--resize-this-window
3031 sub delta horizontal ignore t trail edge)
3032 (if (and window (eq (window-parent sub) parent))
3033 (progn
3034 ;; Assign new normal sizes.
3035 (set-window-new-normal
3036 sub (/ (float (window-new-pixel sub)) parent-total))
3037 (set-window-new-normal
3038 window (- (window-normal-size window horizontal)
3039 (- (window-new-normal sub)
3040 (window-normal-size sub horizontal)))))
3041 (window--resize-child-windows-normal
3042 parent horizontal sub 0 trail delta))
3043 ;; Return 'normalized to notify `window--resize-siblings' that
3044 ;; normal sizes have been already set.
3045 'normalized)
3046 ;; Resize all windows proportionally.
3047 (setq sub last)
3048 (while sub
3049 (cond
3050 ((or (window--resize-child-windows-skip-p sub)
3051 ;; Ignore windows to skip and fixed-size child windows -
3052 ;; in the latter case make it a window to skip.
3053 (and (not ignore)
3054 (window-size-fixed-p sub horizontal ignore)
3055 (set-window-new-normal sub 'ignore))))
3056 ((< delta 0)
3057 ;; When shrinking store the number of lines/cols we can get
3058 ;; from this window here together with the total/normal size
3059 ;; factor.
3060 (set-window-new-normal
3062 (cons
3063 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
3064 (window-min-delta sub horizontal ignore trail t nil t)
3065 (- (/ (float (window-size sub horizontal t))
3066 parent-total)
3067 (window-normal-size sub horizontal)))))
3068 ((> delta 0)
3069 ;; When enlarging store the total/normal size factor only
3070 (set-window-new-normal
3072 (- (/ (float (window-size sub horizontal t))
3073 parent-total)
3074 (window-normal-size sub horizontal)))))
3076 (setq sub (window-left sub)))
3078 (cond
3079 ((< delta 0)
3080 ;; Shrink windows by delta.
3081 (setq best-window t)
3082 (while (and best-window (not (zerop delta)))
3083 (setq sub last)
3084 (setq best-window nil)
3085 (setq best-value most-negative-fixnum)
3086 (while sub
3087 (when (and (consp (window-new-normal sub))
3088 (not (<= (car (window-new-normal sub)) 0))
3089 (> (cdr (window-new-normal sub)) best-value))
3090 (setq best-window sub)
3091 (setq best-value (cdr (window-new-normal sub))))
3093 (setq sub (window-left sub)))
3095 (when best-window
3096 (setq best-delta (min (car (window-new-normal best-window))
3097 char-size (- delta)))
3098 (setq delta (+ delta best-delta))
3099 (set-window-new-pixel best-window (- best-delta) t)
3100 (set-window-new-normal
3101 best-window
3102 (if (= (car (window-new-normal best-window)) best-delta)
3103 'skip ; We can't shrink best-window any further.
3104 (cons (- (car (window-new-normal best-window)) best-delta)
3105 (- (/ (float (window-new-pixel best-window))
3106 parent-total)
3107 (window-normal-size best-window horizontal))))))))
3108 ((> delta 0)
3109 ;; Enlarge windows by delta.
3110 (setq best-window t)
3111 (while (and best-window (not (zerop delta)))
3112 (setq sub last)
3113 (setq best-window nil)
3114 (setq best-value most-positive-fixnum)
3115 (while sub
3116 (when (and (numberp (window-new-normal sub))
3117 (< (window-new-normal sub) best-value))
3118 (setq best-window sub)
3119 (setq best-value (window-new-normal sub)))
3121 (setq sub (window-left sub)))
3123 (when best-window
3124 (setq best-delta (min delta char-size))
3125 (setq delta (- delta best-delta))
3126 (set-window-new-pixel best-window best-delta t)
3127 (set-window-new-normal
3128 best-window
3129 (- (/ (float (window-new-pixel best-window))
3130 parent-total)
3131 (window-normal-size best-window horizontal)))))))
3133 (when best-window
3134 (setq sub last)
3135 (while sub
3136 (when (or (consp (window-new-normal sub))
3137 (numberp (window-new-normal sub)))
3138 ;; Reset new normal size fields so `window-resize-apply'
3139 ;; won't use them to apply new sizes.
3140 (set-window-new-normal sub))
3142 (unless (eq (window-new-normal sub) 'ignore)
3143 ;; Resize this window's child windows (back-engineering
3144 ;; delta from sub's old and new total sizes).
3145 (let ((delta (- (window-new-pixel sub)
3146 (window-size sub horizontal t))))
3147 (unless (and (zerop delta) (not trail))
3148 ;; For the TRAIL non-nil case we have to resize SUB
3149 ;; recursively even if it's size does not change.
3150 (window--resize-this-window
3151 sub delta horizontal ignore nil trail edge))))
3152 (setq sub (window-left sub)))))))
3154 (defun window--resize-siblings (window delta &optional horizontal ignore trail edge char-size)
3155 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
3156 Optional argument HORIZONTAL non-nil means resize other windows
3157 when WINDOW is resized horizontally by DELTA pixels. WINDOW
3158 itself is not resized by this function.
3160 The optional argument IGNORE has the same meaning as for
3161 `window-resizable'.
3163 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3164 of windows that shall be resized. If TRAIL equals `before',
3165 resize only windows on the left or above EDGE. If TRAIL equals
3166 `after', resize only windows on the right or below EDGE. Also,
3167 preferably only resize windows adjacent to EDGE."
3168 (when (window-parent window)
3169 (let* ((parent (window-parent window))
3170 (sub (window-child parent)))
3171 (if (window-combined-p sub horizontal)
3172 ;; In an iso-combination try to extract DELTA from WINDOW's
3173 ;; siblings.
3174 (let ((skip (eq trail 'after))
3175 this-delta other-delta)
3176 ;; Decide which windows shall be left alone.
3177 (while sub
3178 (cond
3179 ((eq sub window)
3180 ;; Make sure WINDOW is left alone when
3181 ;; resizing its siblings.
3182 (set-window-new-normal sub 'ignore)
3183 (setq skip (eq trail 'before)))
3184 (skip
3185 ;; Make sure this sibling is left alone when
3186 ;; resizing its siblings.
3187 (set-window-new-normal sub 'ignore))
3188 ((not (window-size-fixed-p sub horizontal ignore))
3189 ;; Set this-delta to t to signal that we found a sibling
3190 ;; of WINDOW whose size is not fixed.
3191 (setq this-delta t)))
3193 (setq sub (window-right sub)))
3195 ;; Set this-delta to what we can get from WINDOW's siblings.
3196 (if (= (- delta) (window-size window horizontal t))
3197 ;; A deletion, presumably. We must handle this case
3198 ;; specially since `window--resizable' can't be used.
3199 (if this-delta
3200 ;; There's at least one resizable sibling we can
3201 ;; give WINDOW's size to.
3202 (setq this-delta delta)
3203 ;; No resizable sibling exists.
3204 (setq this-delta 0))
3205 ;; Any other form of resizing.
3206 (setq this-delta
3207 (window--resizable
3208 window delta horizontal ignore trail t nil t)))
3210 ;; Set other-delta to what we still have to get from
3211 ;; ancestor windows of parent.
3212 (setq other-delta (- delta this-delta))
3213 (unless (zerop other-delta)
3214 ;; Unless we got everything from WINDOW's siblings, PARENT
3215 ;; must be resized by other-delta lines or columns.
3216 (set-window-new-pixel parent other-delta 'add))
3218 (if (zerop this-delta)
3219 ;; We haven't got anything from WINDOW's siblings but we
3220 ;; must update the normal sizes to respect other-delta.
3221 (window--resize-child-windows-normal
3222 parent horizontal window this-delta trail other-delta)
3223 ;; We did get something from WINDOW's siblings which means
3224 ;; we have to resize their child windows.
3225 (unless (eq (window--resize-child-windows
3226 parent (- this-delta) horizontal
3227 window ignore trail edge char-size)
3228 ;; If `window--resize-child-windows' returns
3229 ;; 'normalized, this means it has set the
3230 ;; normal sizes already.
3231 'normalized)
3232 ;; Set the normal sizes.
3233 (window--resize-child-windows-normal
3234 parent horizontal window this-delta trail other-delta))
3235 ;; Set DELTA to what we still have to get from ancestor
3236 ;; windows.
3237 (setq delta other-delta)))
3239 ;; In an ortho-combination all siblings of WINDOW must be
3240 ;; resized by DELTA.
3241 (set-window-new-pixel parent delta 'add)
3242 (while sub
3243 (unless (eq sub window)
3244 (window--resize-this-window
3245 sub delta horizontal ignore t))
3246 (setq sub (window-right sub))))
3248 (unless (zerop delta)
3249 ;; "Go up."
3250 (window--resize-siblings
3251 parent delta horizontal ignore trail edge char-size)))))
3253 (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge char-size)
3254 "Resize WINDOW vertically by DELTA pixels.
3255 Optional argument HORIZONTAL non-nil means resize WINDOW
3256 horizontally by DELTA pixels.
3258 The optional argument IGNORE has the same meaning as for
3259 `window-resizable'. Optional argument ADD non-nil means add
3260 DELTA to the new total size of WINDOW.
3262 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3263 of windows that shall be resized. If TRAIL equals `before',
3264 resize only windows on the left or above EDGE. If TRAIL equals
3265 `after', resize only windows on the right or below EDGE. Also,
3266 preferably only resize windows adjacent to EDGE.
3268 If the optional argument CHAR-SIZE is a positive integer, it specifies
3269 the number of pixels by which windows are incrementally resized.
3270 If CHAR-SIZE is nil, this means to use the value of
3271 `frame-char-height' or `frame-char-width' of WINDOW's frame.
3273 This function recursively resizes WINDOW's child windows to fit the
3274 new size. Make sure that WINDOW is `window--resizable' before
3275 calling this function. Note that this function does not resize
3276 siblings of WINDOW or WINDOW's parent window. You have to
3277 eventually call `window-resize-apply' in order to make resizing
3278 actually take effect."
3279 (when add
3280 ;; Add DELTA to the new total size of WINDOW.
3281 (set-window-new-pixel window delta t))
3283 (let ((sub (window-child window)))
3284 (cond
3285 ((not sub))
3286 ((window-combined-p sub horizontal)
3287 ;; In an iso-combination resize child windows according to their
3288 ;; normal sizes.
3289 (window--resize-child-windows
3290 window delta horizontal nil ignore trail edge char-size))
3291 ;; In an ortho-combination resize each child window by DELTA.
3293 (while sub
3294 (window--resize-this-window
3295 sub delta horizontal ignore t trail edge char-size)
3296 (setq sub (window-right sub)))))))
3298 (defun window--resize-root-window (window delta horizontal ignore pixelwise)
3299 "Resize root window WINDOW vertically by DELTA lines.
3300 HORIZONTAL non-nil means resize root window WINDOW horizontally
3301 by DELTA columns.
3303 IGNORE non-nil means ignore any restrictions imposed by fixed
3304 size windows, `window-min-height' or `window-min-width' settings.
3306 This function is only called by the frame resizing routines. It
3307 resizes windows proportionally and never deletes any windows."
3308 (when (and (windowp window) (numberp delta))
3309 (let ((pixel-delta
3310 (if pixelwise
3311 delta
3312 (window--size-to-pixel window delta horizontal))))
3313 (when (window-sizable-p window pixel-delta horizontal ignore t)
3314 (window--resize-reset (window-frame window) horizontal)
3315 (window--resize-this-window
3316 window pixel-delta horizontal ignore t)))))
3318 (defun window--resize-root-window-vertically (window delta pixelwise)
3319 "Resize root window WINDOW vertically by DELTA lines.
3320 If DELTA is less than zero and we can't shrink WINDOW by DELTA
3321 lines, shrink it as much as possible. If DELTA is greater than
3322 zero, this function can resize fixed-size windows in order to
3323 recover the necessary lines. Return the number of lines that
3324 were recovered.
3326 Third argument PIXELWISE non-nil means to interpret DELTA as
3327 pixels and return the number of pixels that were recovered.
3329 This function is called by the minibuffer window resizing
3330 routines."
3331 (let* ((frame (window-frame window))
3332 (pixel-delta
3333 (cond
3334 (pixelwise
3335 delta)
3336 ((numberp delta)
3337 (* (frame-char-height frame) delta))
3338 (t 0)))
3339 ignore)
3340 (cond
3341 ((zerop pixel-delta))
3342 ((< pixel-delta 0)
3343 (setq pixel-delta (window-sizable window pixel-delta nil nil pixelwise))
3344 (window--resize-reset frame)
3345 ;; When shrinking the root window, emulate an edge drag in order
3346 ;; to not resize other windows if we can avoid it (Bug#12419).
3347 (window--resize-this-window
3348 window pixel-delta nil ignore t 'before
3349 (+ (window-pixel-top window) (window-pixel-height window)))
3350 ;; Don't record new normal sizes to make sure that shrinking back
3351 ;; proportionally works as intended.
3352 (walk-window-tree
3353 (lambda (window) (set-window-new-normal window 'ignore)) frame t))
3354 ((> pixel-delta 0)
3355 (window--resize-reset frame)
3356 (unless (window-sizable window pixel-delta nil nil pixelwise)
3357 (setq ignore t))
3358 ;; When growing the root window, resize proportionally. This
3359 ;; should give windows back their original sizes (hopefully).
3360 (window--resize-this-window
3361 window pixel-delta nil ignore t)))
3362 ;; Return the possibly adjusted DELTA.
3363 (if pixelwise
3364 pixel-delta
3365 (/ pixel-delta (frame-char-height frame)))))
3367 (defun window--sanitize-window-sizes (horizontal)
3368 "Assert that all windows on selected frame are large enough.
3369 If necessary and possible, make sure that every window on frame
3370 FRAME has its minimum height. Optional argument HORIZONTAL
3371 non-nil means to make sure that every window on frame FRAME has
3372 its minimum width. The minimum height/width of a window is the
3373 respective value returned by `window-min-size' for that window.
3375 Return t if all windows were resized appropriately. Return nil
3376 if at least one window could not be resized as requested, which
3377 may happen when the FRAME is not large enough to accommodate it."
3378 (let ((value t))
3379 (walk-window-tree
3380 (lambda (window)
3381 (let ((delta (- (window-min-size window horizontal nil t)
3382 (window-size window horizontal t))))
3383 (when (> delta 0)
3384 (if (window-resizable-p window delta horizontal nil t)
3385 (window-resize window delta horizontal nil t)
3386 (setq value nil))))))
3387 value))
3389 (defun adjust-window-trailing-edge (window delta &optional horizontal pixelwise)
3390 "Move WINDOW's bottom edge by DELTA lines.
3391 Optional argument HORIZONTAL non-nil means move WINDOW's right
3392 edge by DELTA columns. WINDOW must be a valid window and
3393 defaults to the selected one.
3395 Optional argument PIXELWISE non-nil means interpret DELTA as
3396 number of pixels.
3398 If DELTA is greater than zero, move the edge downwards or to the
3399 right. If DELTA is less than zero, move the edge upwards or to
3400 the left. If the edge can't be moved by DELTA lines or columns,
3401 move it as far as possible in the desired direction."
3402 (setq window (window-normalize-window window))
3403 (let* ((frame (window-frame window))
3404 (minibuffer-window (minibuffer-window frame))
3405 (right window)
3406 left first-left first-right this-delta min-delta max-delta ignore)
3408 (unless pixelwise
3409 (setq pixelwise t)
3410 (setq delta (* delta (frame-char-size window horizontal))))
3412 ;; Find the edge we want to move.
3413 (while (and (or (not (window-combined-p right horizontal))
3414 (not (window-right right)))
3415 (setq right (window-parent right))))
3416 (cond
3417 ((and (not right) (not horizontal)
3418 ;; Resize the minibuffer window if it's on the same frame as
3419 ;; and immediately below WINDOW and it's either active or
3420 ;; `resize-mini-windows' is nil.
3421 (eq (window-frame minibuffer-window) frame)
3422 (= (nth 1 (window-pixel-edges minibuffer-window))
3423 (nth 3 (window-pixel-edges window)))
3424 (or (not resize-mini-windows)
3425 (eq minibuffer-window (active-minibuffer-window))))
3426 (window--resize-mini-window minibuffer-window (- delta)))
3427 ((or (not (setq left right)) (not (setq right (window-right right))))
3428 (if horizontal
3429 (user-error "No window on the right of this one")
3430 (user-error "No window below this one")))
3432 ;; Set LEFT to the first resizable window on the left. This step is
3433 ;; needed to handle fixed-size windows.
3434 (setq first-left left)
3435 (while (and left
3436 (or (window-size-fixed-p left horizontal)
3437 (and (< delta 0)
3438 (<= (window-size left horizontal t)
3439 (window-min-size left horizontal nil t)))))
3440 (setq left
3441 (or (window-left left)
3442 (progn
3443 (while (and (setq left (window-parent left))
3444 (not (window-combined-p left horizontal))))
3445 (window-left left)))))
3446 (unless left
3447 ;; We have to resize a size-preserved window. Start again with
3448 ;; the window initially on the left.
3449 (setq ignore 'preserved)
3450 (setq left first-left)
3451 (while (and left
3452 (or (window-size-fixed-p left horizontal 'preserved)
3453 (and (< delta 0)
3454 (<= (window-size left horizontal t)
3455 (window-min-size
3456 left horizontal 'preserved t)))))
3457 (setq left
3458 (or (window-left left)
3459 (progn
3460 (while (and (setq left (window-parent left))
3461 (not (window-combined-p left horizontal))))
3462 (window-left left)))))
3464 (unless left
3465 (if horizontal
3466 (user-error "No resizable window on the left of this one")
3467 (user-error "No resizable window above this one"))))
3469 ;; Set RIGHT to the first resizable window on the right. This step
3470 ;; is needed to handle fixed-size windows.
3471 (setq first-right right)
3472 (while (and right
3473 (or (window-size-fixed-p right horizontal)
3474 (and (> delta 0)
3475 (<= (window-size right horizontal t)
3476 (window-min-size
3477 right horizontal 'preserved t)))))
3478 (setq right
3479 (or (window-right right)
3480 (progn
3481 (while (and (setq right (window-parent right))
3482 (not (window-combined-p right horizontal))))
3483 (window-right right)))))
3484 (unless right
3485 ;; We have to resize a size-preserved window. Start again with
3486 ;; the window initially on the right.
3487 (setq ignore 'preserved)
3488 (setq right first-right)
3489 (while (and right
3490 (or (window-size-fixed-p right horizontal 'preserved)
3491 (and (> delta 0)
3492 (<= (window-size right horizontal t)
3493 (window-min-size
3494 right horizontal 'preserved t)))))
3495 (setq right
3496 (or (window-right right)
3497 (progn
3498 (while (and (setq right (window-parent right))
3499 (not (window-combined-p right horizontal))))
3500 (window-right right)))))
3501 (unless right
3502 (if horizontal
3503 (user-error "No resizable window on the right of this one")
3504 (user-error "No resizable window below this one"))))
3506 ;; LEFT and RIGHT (which might be both internal windows) are now the
3507 ;; two windows we want to resize.
3508 (cond
3509 ((> delta 0)
3510 (setq max-delta
3511 (window--max-delta-1
3512 left 0 horizontal ignore 'after nil pixelwise))
3513 (setq min-delta
3514 (window--min-delta-1
3515 right (- delta) horizontal ignore 'before nil pixelwise))
3516 (when (or (< max-delta delta) (> min-delta (- delta)))
3517 ;; We can't get the whole DELTA - move as far as possible.
3518 (setq delta (min max-delta (- min-delta))))
3519 (unless (zerop delta)
3520 ;; Start resizing.
3521 (window--resize-reset frame horizontal)
3522 ;; Try to enlarge LEFT first.
3523 (setq this-delta
3524 (window--resizable
3525 left delta horizontal ignore 'after nil nil pixelwise))
3526 (unless (zerop this-delta)
3527 (window--resize-this-window
3528 left this-delta horizontal ignore t 'before
3529 (if horizontal
3530 (+ (window-pixel-left left) (window-pixel-width left))
3531 (+ (window-pixel-top left) (window-pixel-height left)))))
3532 ;; Shrink windows on right of LEFT.
3533 (window--resize-siblings
3534 left delta horizontal ignore 'after
3535 (if horizontal
3536 (window-pixel-left right)
3537 (window-pixel-top right)))))
3538 ((< delta 0)
3539 (setq max-delta
3540 (window--max-delta-1
3541 right 0 horizontal ignore 'before nil pixelwise))
3542 (setq min-delta
3543 (window--min-delta-1
3544 left delta horizontal ignore 'after nil pixelwise))
3545 (when (or (< max-delta (- delta)) (> min-delta delta))
3546 ;; We can't get the whole DELTA - move as far as possible.
3547 (setq delta (max (- max-delta) min-delta)))
3548 (unless (zerop delta)
3549 ;; Start resizing.
3550 (window--resize-reset frame horizontal)
3551 ;; Try to enlarge RIGHT.
3552 (setq this-delta
3553 (window--resizable
3554 right (- delta) horizontal ignore 'before nil nil pixelwise))
3555 (unless (zerop this-delta)
3556 (window--resize-this-window
3557 right this-delta horizontal ignore t 'after
3558 (if horizontal
3559 (window-pixel-left right)
3560 (window-pixel-top right))))
3561 ;; Shrink windows on left of RIGHT.
3562 (window--resize-siblings
3563 right (- delta) horizontal ignore 'before
3564 (if horizontal
3565 (+ (window-pixel-left left) (window-pixel-width left))
3566 (+ (window-pixel-top left) (window-pixel-height left)))))))
3567 (unless (zerop delta)
3568 ;; Don't report an error in the standard case.
3569 (when (window--resize-apply-p frame horizontal)
3570 (if (window-resize-apply frame horizontal)
3571 (progn
3572 (window--pixel-to-total frame horizontal)
3573 (run-window-configuration-change-hook frame))
3574 ;; But do report an error if applying the changes fails.
3575 (error "Failed adjusting window %s" window))))))))
3577 (defun enlarge-window (delta &optional horizontal)
3578 "Make the selected window DELTA lines taller.
3579 Interactively, if no argument is given, make the selected window
3580 one line taller. If optional argument HORIZONTAL is non-nil,
3581 make selected window wider by DELTA columns. If DELTA is
3582 negative, shrink selected window by -DELTA lines or columns."
3583 (interactive "p")
3584 (let ((minibuffer-window (minibuffer-window)))
3585 (when (window-preserved-size nil horizontal)
3586 (window-preserve-size nil horizontal))
3587 (cond
3588 ((zerop delta))
3589 ((window-size-fixed-p nil horizontal)
3590 (user-error "Selected window has fixed size"))
3591 ((window-minibuffer-p)
3592 (if horizontal
3593 (user-error "Cannot resize minibuffer window horizontally")
3594 (window--resize-mini-window
3595 (selected-window) (* delta (frame-char-height)))))
3596 ((and (not horizontal)
3597 (window-full-height-p)
3598 (eq (window-frame minibuffer-window) (selected-frame))
3599 (not resize-mini-windows))
3600 ;; If the selected window is full height and `resize-mini-windows'
3601 ;; is nil, resize the minibuffer window.
3602 (window--resize-mini-window
3603 minibuffer-window (* (- delta) (frame-char-height))))
3604 ((window--resizable-p nil delta horizontal)
3605 (window-resize nil delta horizontal))
3606 ((window--resizable-p nil delta horizontal 'preserved)
3607 (window-resize nil delta horizontal 'preserved))
3608 ((eq this-command
3609 (if horizontal 'enlarge-window-horizontally 'enlarge-window))
3610 ;; For backward compatibility don't signal an error unless this
3611 ;; command is `enlarge-window(-horizontally)'.
3612 (user-error "Cannot enlarge selected window"))
3614 (window-resize
3615 nil (if (> delta 0)
3616 (window-max-delta nil horizontal)
3617 (- (window-min-delta nil horizontal)))
3618 horizontal)))))
3620 (defun shrink-window (delta &optional horizontal)
3621 "Make the selected window DELTA lines smaller.
3622 Interactively, if no argument is given, make the selected window
3623 one line smaller. If optional argument HORIZONTAL is non-nil,
3624 make selected window narrower by DELTA columns. If DELTA is
3625 negative, enlarge selected window by -DELTA lines or columns."
3626 (interactive "p")
3627 (let ((minibuffer-window (minibuffer-window)))
3628 (when (window-preserved-size nil horizontal)
3629 (window-preserve-size nil horizontal))
3630 (cond
3631 ((zerop delta))
3632 ((window-size-fixed-p nil horizontal)
3633 (user-error "Selected window has fixed size"))
3634 ((window-minibuffer-p)
3635 (if horizontal
3636 (user-error "Cannot resize minibuffer window horizontally")
3637 (window--resize-mini-window
3638 (selected-window) (* (- delta) (frame-char-height)))))
3639 ((and (not horizontal)
3640 (window-full-height-p)
3641 (eq (window-frame minibuffer-window) (selected-frame))
3642 (not resize-mini-windows))
3643 ;; If the selected window is full height and `resize-mini-windows'
3644 ;; is nil, resize the minibuffer window.
3645 (window--resize-mini-window
3646 minibuffer-window (* delta (frame-char-height))))
3647 ((window--resizable-p nil (- delta) horizontal)
3648 (window-resize nil (- delta) horizontal))
3649 ((window--resizable-p nil (- delta) horizontal 'preserved)
3650 (window-resize nil (- delta) horizontal 'preserved))
3651 ((eq this-command
3652 (if horizontal 'shrink-window-horizontally 'shrink-window))
3653 ;; For backward compatibility don't signal an error unless this
3654 ;; command is `shrink-window(-horizontally)'.
3655 (user-error "Cannot shrink selected window"))
3657 (window-resize
3658 nil (if (> delta 0)
3659 (- (window-min-delta nil horizontal))
3660 (window-max-delta nil horizontal))
3661 horizontal)))))
3663 (defun maximize-window (&optional window)
3664 "Maximize WINDOW.
3665 Make WINDOW as large as possible without deleting any windows.
3666 WINDOW must be a valid window and defaults to the selected one.
3668 If the option `window-resize-pixelwise' is non-nil maximize
3669 WINDOW pixelwise."
3670 (interactive)
3671 (setq window (window-normalize-window window))
3672 (window-resize
3673 window (window-max-delta window nil nil nil nil nil window-resize-pixelwise)
3674 nil nil window-resize-pixelwise)
3675 (window-resize
3676 window (window-max-delta window t nil nil nil nil window-resize-pixelwise)
3677 t nil window-resize-pixelwise))
3679 (defun minimize-window (&optional window)
3680 "Minimize WINDOW.
3681 Make WINDOW as small as possible without deleting any windows.
3682 WINDOW must be a valid window and defaults to the selected one.
3684 If the option `window-resize-pixelwise' is non-nil minimize
3685 WINDOW pixelwise."
3686 (interactive)
3687 (setq window (window-normalize-window window))
3688 (window-resize
3689 window
3690 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise))
3691 nil nil window-resize-pixelwise)
3692 (window-resize
3693 window
3694 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise))
3695 t nil window-resize-pixelwise))
3697 ;;; Window edges
3698 (defun window-edges (&optional window body absolute pixelwise)
3699 "Return a list of the edge distances of WINDOW.
3700 WINDOW must be a valid window and defaults to the selected one.
3701 The list returned has the form (LEFT TOP RIGHT BOTTOM).
3703 If the optional argument BODY is nil, this means to return the
3704 edges corresponding to the total size of WINDOW. BODY non-nil
3705 means to return the edges of WINDOW's body (aka text area). If
3706 BODY is non-nil, WINDOW must specify a live window.
3708 Optional argument ABSOLUTE nil means to return edges relative to
3709 the position of WINDOW's native frame. ABSOLUTE non-nil means to
3710 return coordinates relative to the origin - the position (0, 0) -
3711 of FRAME's display. On non-graphical systems this argument has
3712 no effect.
3714 Optional argument PIXELWISE nil means to return the coordinates
3715 in terms of the canonical character width and height of WINDOW's
3716 frame, rounded if necessary. PIXELWISE non-nil means to return
3717 the coordinates in pixels where the values for RIGHT and BOTTOM
3718 are one more than the actual value of these edges. Note that if
3719 ABSOLUTE is non-nil, PIXELWISE is implicitly non-nil too."
3720 (let* ((window (window-normalize-window window body))
3721 (frame (window-frame window))
3722 (border-width (frame-internal-border-width frame))
3723 (char-width (frame-char-width frame))
3724 (char-height (frame-char-height frame))
3725 (left (if pixelwise
3726 (+ (window-pixel-left window) border-width)
3727 (+ (window-left-column window)
3728 (/ border-width char-width))))
3729 (left-body
3730 (when body
3731 (+ (window-pixel-left window) border-width
3732 (if (eq (car (window-current-scroll-bars window)) 'left)
3733 (window-scroll-bar-width window)
3735 (nth 0 (window-fringes window))
3736 (* (or (nth 0 (window-margins window)) 0) char-width))))
3737 (top (if pixelwise
3738 (+ (window-pixel-top window) border-width)
3739 (+ (window-top-line window)
3740 (/ border-width char-height))))
3741 (top-body
3742 (when body
3743 (+ (window-pixel-top window) border-width
3744 (window-header-line-height window))))
3745 (right (+ left (if pixelwise
3746 (window-pixel-width window)
3747 (window-total-width window))))
3748 (right-body (and body (+ left-body (window-body-width window t))))
3749 (bottom (+ top (if pixelwise
3750 (window-pixel-height window)
3751 (window-total-height window))))
3752 (bottom-body (and body (+ top-body (window-body-height window t)))))
3753 (if absolute
3754 (let* ((native-edges (frame-edges frame 'native-edges))
3755 (left-off (nth 0 native-edges))
3756 (top-off (nth 1 native-edges)))
3757 (if body
3758 (list (+ left-body left-off) (+ top-body top-off)
3759 (+ right-body left-off) (+ bottom-body top-off))
3760 (list (+ left left-off) (+ top top-off)
3761 (+ right left-off) (+ bottom top-off))))
3762 (if body
3763 (if pixelwise
3764 (list left-body top-body right-body bottom-body)
3765 (list (/ left-body char-width) (/ top-body char-height)
3766 ;; Round up.
3767 (/ (+ right-body char-width -1) char-width)
3768 (/ (+ bottom-body char-height -1) char-height)))
3769 (list left top right bottom)))))
3771 (defun window-body-edges (&optional window)
3772 "Return a list of the edge coordinates of WINDOW's body.
3773 The return value is that of `window-edges' called with argument
3774 BODY non-nil."
3775 (window-edges window t))
3776 (defalias 'window-inside-edges 'window-body-edges)
3778 (defun window-pixel-edges (&optional window)
3779 "Return a list of the edge pixel coordinates of WINDOW.
3780 The return value is that of `window-edges' called with argument
3781 PIXELWISE non-nil."
3782 (window-edges window nil nil t))
3784 (defun window-body-pixel-edges (&optional window)
3785 "Return a list of the edge pixel coordinates of WINDOW's body.
3786 The return value is that of `window-edges' called with arguments
3787 BODY and PIXELWISE non-nil."
3788 (window-edges window t nil t))
3789 (defalias 'window-inside-pixel-edges 'window-body-pixel-edges)
3791 (defun window-absolute-pixel-edges (&optional window)
3792 "Return a list of the edge pixel coordinates of WINDOW.
3793 The return value is that of `window-edges' called with argument
3794 ABSOLUTE non-nil."
3795 (window-edges window nil t t))
3797 (defun window-absolute-body-pixel-edges (&optional window)
3798 "Return a list of the edge pixel coordinates of WINDOW's text area.
3799 The return value is that of `window-edges' called with arguments
3800 BODY and ABSOLUTE non-nil."
3801 (window-edges window t t t))
3802 (defalias 'window-inside-absolute-pixel-edges 'window-absolute-body-pixel-edges)
3804 (defun window-absolute-pixel-position (&optional position window)
3805 "Return display coordinates of POSITION in WINDOW.
3806 If the buffer position POSITION is visible in window WINDOW,
3807 return the display coordinates of the upper/left corner of the
3808 glyph at POSITION. The return value is a cons of the X- and
3809 Y-coordinates of that corner, relative to an origin at (0, 0) of
3810 WINDOW's display. Return nil if POSITION is not visible in
3811 WINDOW.
3813 WINDOW must be a live window and defaults to the selected window.
3814 POSITION defaults to the value of `window-point' of WINDOW."
3815 (let* ((window (window-normalize-window window t))
3816 (pos-in-window
3817 (pos-visible-in-window-p
3818 (or position (window-point window)) window t)))
3819 (when pos-in-window
3820 (let ((edges (window-absolute-body-pixel-edges window)))
3821 (cons (+ (nth 0 edges) (nth 0 pos-in-window))
3822 (+ (nth 1 edges) (nth 1 pos-in-window)))))))
3824 (defun frame-root-window-p (window)
3825 "Return non-nil if WINDOW is the root window of its frame."
3826 (eq window (frame-root-window window)))
3828 (defun window--subtree (window &optional next)
3829 "Return window subtree rooted at WINDOW.
3830 Optional argument NEXT non-nil means include WINDOW's right
3831 siblings in the return value.
3833 See the documentation of `window-tree' for a description of the
3834 return value."
3835 (let (list)
3836 (while window
3837 (setq list
3838 (cons
3839 (cond
3840 ((window-top-child window)
3841 (cons t (cons (window-edges window)
3842 (window--subtree (window-top-child window) t))))
3843 ((window-left-child window)
3844 (cons nil (cons (window-edges window)
3845 (window--subtree (window-left-child window) t))))
3846 (t window))
3847 list))
3848 (setq window (when next (window-next-sibling window))))
3849 (nreverse list)))
3851 (defun window-tree (&optional frame)
3852 "Return the window tree of frame FRAME.
3853 FRAME must be a live frame and defaults to the selected frame.
3854 The return value is a list of the form (ROOT MINI), where ROOT
3855 represents the window tree of the frame's root window, and MINI
3856 is the frame's minibuffer window.
3858 If the root window is not split, ROOT is the root window itself.
3859 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3860 for a horizontal split, and t for a vertical split. EDGES gives
3861 the combined size and position of the child windows in the split,
3862 and the rest of the elements are the child windows in the split.
3863 Each of the child windows may again be a window or a list
3864 representing a window split, and so on. EDGES is a list (LEFT
3865 TOP RIGHT BOTTOM) as returned by `window-edges'."
3866 (setq frame (window-normalize-frame frame))
3867 (window--subtree (frame-root-window frame) t))
3869 (defun other-window (count &optional all-frames)
3870 "Select another window in cyclic ordering of windows.
3871 COUNT specifies the number of windows to skip, starting with the
3872 selected window, before making the selection. If COUNT is
3873 positive, skip COUNT windows forwards. If COUNT is negative,
3874 skip -COUNT windows backwards. COUNT zero means do not skip any
3875 window, so select the selected window. In an interactive call,
3876 COUNT is the numeric prefix argument. Return nil.
3878 If the `other-window' parameter of the selected window is a
3879 function and `ignore-window-parameters' is nil, call that
3880 function with the arguments COUNT and ALL-FRAMES.
3882 This function does not select a window whose `no-other-window'
3883 window parameter is non-nil.
3885 This function uses `next-window' for finding the window to
3886 select. The argument ALL-FRAMES has the same meaning as in
3887 `next-window', but the MINIBUF argument of `next-window' is
3888 always effectively nil."
3889 (interactive "p")
3890 (let* ((window (selected-window))
3891 (function (and (not ignore-window-parameters)
3892 (window-parameter window 'other-window)))
3893 old-window old-count)
3894 (if (functionp function)
3895 (funcall function count all-frames)
3896 ;; `next-window' and `previous-window' may return a window we are
3897 ;; not allowed to select. Hence we need an exit strategy in case
3898 ;; all windows are non-selectable.
3899 (catch 'exit
3900 (while (> count 0)
3901 (setq window (next-window window nil all-frames))
3902 (cond
3903 ((eq window old-window)
3904 (when (= count old-count)
3905 ;; Keep out of infinite loops. When COUNT has not changed
3906 ;; since we last looked at `window' we're probably in one.
3907 (throw 'exit nil)))
3908 ((window-parameter window 'no-other-window)
3909 (unless old-window
3910 ;; The first non-selectable window `next-window' got us:
3911 ;; Remember it and the current value of COUNT.
3912 (setq old-window window)
3913 (setq old-count count)))
3915 (setq count (1- count)))))
3916 (while (< count 0)
3917 (setq window (previous-window window nil all-frames))
3918 (cond
3919 ((eq window old-window)
3920 (when (= count old-count)
3921 ;; Keep out of infinite loops. When COUNT has not changed
3922 ;; since we last looked at `window' we're probably in one.
3923 (throw 'exit nil)))
3924 ((window-parameter window 'no-other-window)
3925 (unless old-window
3926 ;; The first non-selectable window `previous-window' got
3927 ;; us: Remember it and the current value of COUNT.
3928 (setq old-window window)
3929 (setq old-count count)))
3931 (setq count (1+ count)))))
3933 (select-window window)
3934 ;; Always return nil.
3935 nil))))
3937 ;; This should probably return non-nil when the selected window is part
3938 ;; of an atomic window whose root is the frame's root window.
3939 (defun one-window-p (&optional nomini all-frames)
3940 "Return non-nil if the selected window is the only window.
3941 Optional arg NOMINI non-nil means don't count the minibuffer
3942 even if it is active. Otherwise, the minibuffer is counted
3943 when it is active.
3945 Optional argument ALL-FRAMES specifies the set of frames to
3946 consider, see also `next-window'. ALL-FRAMES nil or omitted
3947 means consider windows on the selected frame only, plus the
3948 minibuffer window if specified by the NOMINI argument. If the
3949 minibuffer counts, consider all windows on all frames that share
3950 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3951 with a special meaning are:
3953 - t means consider all windows on all existing frames.
3955 - `visible' means consider all windows on all visible frames on
3956 the current terminal.
3958 - 0 (the number zero) means consider all windows on all visible
3959 and iconified frames on the current terminal.
3961 - A frame means consider all windows on that frame only.
3963 Anything else means consider all windows on the selected frame
3964 and no others."
3965 (let ((base-window (selected-window)))
3966 (if (and nomini (eq base-window (minibuffer-window)))
3967 (setq base-window (next-window base-window)))
3968 (eq base-window
3969 (next-window base-window (if nomini 'arg) all-frames))))
3971 ;;; Deleting windows.
3972 (defun window-deletable-p (&optional window)
3973 "Return t if WINDOW can be safely deleted from its frame.
3974 WINDOW must be a valid window and defaults to the selected one.
3976 Return `frame' if WINDOW is the root window of its frame and that
3977 frame can be safely deleted."
3978 (setq window (window-normalize-window window))
3980 (unless (or ignore-window-parameters
3981 (eq (window-parameter window 'delete-window) t))
3982 ;; Handle atomicity.
3983 (when (window-parameter window 'window-atom)
3984 (setq window (window-atom-root window))))
3986 (let ((frame (window-frame window)))
3987 (cond
3988 ((frame-root-window-p window)
3989 ;; WINDOW's frame can be deleted only if there are other frames
3990 ;; on the same terminal, and it does not contain the active
3991 ;; minibuffer.
3992 (unless (or (eq frame (next-frame frame 0))
3993 ;; We can delete our frame only if no other frame
3994 ;; currently uses our minibuffer window.
3995 (catch 'other
3996 (dolist (other (frame-list))
3997 (when (and (not (eq other frame))
3998 (eq (window-frame (minibuffer-window other))
3999 frame))
4000 (throw 'other t))))
4001 (let ((minibuf (active-minibuffer-window)))
4002 (and minibuf (eq frame (window-frame minibuf)))))
4003 'frame))
4004 ((window-minibuffer-p window)
4005 ;; If WINDOW is the minibuffer window of a non-minibuffer-only
4006 ;; frame, it cannot be deleted separately.
4007 nil)
4008 ((or ignore-window-parameters
4009 (not (eq window (window-main-window frame))))
4010 ;; Otherwise, WINDOW can be deleted unless it is the main window
4011 ;; of its frame.
4012 t))))
4014 (defun window--in-subtree-p (window root)
4015 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
4016 (or (eq window root)
4017 (let ((parent (window-parent window)))
4018 (catch 'done
4019 (while parent
4020 (if (eq parent root)
4021 (throw 'done t)
4022 (setq parent (window-parent parent))))))))
4024 (defun delete-window (&optional window)
4025 "Delete WINDOW.
4026 WINDOW must be a valid window and defaults to the selected one.
4027 Return nil.
4029 If the variable `ignore-window-parameters' is non-nil or the
4030 `delete-window' parameter of WINDOW equals t, do not process any
4031 parameters of WINDOW. Otherwise, if the `delete-window'
4032 parameter of WINDOW specifies a function, call that function with
4033 WINDOW as its sole argument and return the value returned by that
4034 function.
4036 Otherwise, if WINDOW is part of an atomic window, call
4037 `delete-window' with the root of the atomic window as its
4038 argument. Signal an error if WINDOW is either the only window on
4039 its frame, the last non-side window, or part of an atomic window
4040 that is its frame's root window."
4041 (interactive)
4042 (setq window (window-normalize-window window))
4043 (let* ((frame (window-frame window))
4044 (function (window-parameter window 'delete-window))
4045 (parent (window-parent window))
4046 atom-root)
4047 (window--check frame)
4048 (catch 'done
4049 ;; Handle window parameters.
4050 (cond
4051 ;; Ignore window parameters if `ignore-window-parameters' tells
4052 ;; us so or `delete-window' equals t.
4053 ((or ignore-window-parameters (eq function t)))
4054 ((functionp function)
4055 ;; The `delete-window' parameter specifies the function to call.
4056 ;; If that function is `ignore' nothing is done. It's up to the
4057 ;; function called here to avoid infinite recursion.
4058 (throw 'done (funcall function window)))
4059 ((and (window-parameter window 'window-atom)
4060 (setq atom-root (window-atom-root window))
4061 (not (eq atom-root window)))
4062 (if (eq atom-root (frame-root-window frame))
4063 (error "Root of atomic window is root window of its frame")
4064 (throw 'done (delete-window atom-root))))
4065 ((not parent)
4066 (error "Attempt to delete minibuffer or sole ordinary window"))
4067 ((eq window (window-main-window frame))
4068 (error "Attempt to delete main window of frame %s" frame)))
4070 (let* ((horizontal (window-left-child parent))
4071 (size (window-size window horizontal t))
4072 (window-combination-resize
4073 (or window-combination-resize
4074 (window-parameter parent 'window-side)))
4075 (frame-selected
4076 (window--in-subtree-p (frame-selected-window frame) window))
4077 ;; Emacs 23 preferably gives WINDOW's space to its left
4078 ;; sibling.
4079 (sibling (or (window-left window) (window-right window))))
4080 (window--resize-reset frame horizontal)
4081 (cond
4082 ((and (not (eq window-combination-resize t))
4083 sibling (window-sizable-p sibling size horizontal nil t))
4084 ;; Resize WINDOW's sibling.
4085 (window--resize-this-window sibling size horizontal nil t)
4086 (set-window-new-normal
4087 sibling (+ (window-normal-size sibling horizontal)
4088 (window-normal-size window horizontal))))
4089 ((window--resizable-p window (- size) horizontal nil nil nil t t)
4090 ;; Can do without resizing fixed-size windows.
4091 (window--resize-siblings window (- size) horizontal))
4093 ;; Can't do without resizing fixed-size windows.
4094 (window--resize-siblings window (- size) horizontal t)))
4095 ;; Actually delete WINDOW.
4096 (delete-window-internal window)
4097 (window--pixel-to-total frame horizontal)
4098 (when (and frame-selected
4099 (window-parameter
4100 (frame-selected-window frame) 'no-other-window))
4101 ;; `delete-window-internal' has selected a window that should
4102 ;; not be selected, fix this here.
4103 (other-window -1 frame))
4104 (run-window-configuration-change-hook frame)
4105 (window--check frame)
4106 ;; Always return nil.
4107 nil))))
4109 (defun delete-other-windows (&optional window)
4110 "Make WINDOW fill its frame.
4111 WINDOW must be a valid window and defaults to the selected one.
4112 Return nil.
4114 If the variable `ignore-window-parameters' is non-nil or the
4115 `delete-other-windows' parameter of WINDOW equals t, do not pay
4116 attention to any other parameters of WINDOW. Otherwise, if the
4117 `delete-other-windows' parameter of WINDOW specifies a function,
4118 call that function with WINDOW as its sole argument and return
4119 the value returned by that function.
4121 Else, if WINDOW is part of an atomic window, call this function
4122 with the root of the atomic window as its argument. Signal an
4123 error if that root window is the root window of WINDOW's frame.
4124 Also signal an error if WINDOW is a side window. Do not delete
4125 any window whose `no-delete-other-windows' parameter is non-nil."
4126 (interactive)
4127 (setq window (window-normalize-window window))
4128 (let* ((frame (window-frame window))
4129 (function (window-parameter window 'delete-other-windows))
4130 atom-root main)
4131 (window--check frame)
4132 (catch 'done
4133 (cond
4134 ;; Ignore window parameters if `ignore-window-parameters' is t or
4135 ;; `delete-other-windows' is t.
4136 ((or ignore-window-parameters (eq function t)))
4137 ((functionp function)
4138 ;; The `delete-other-windows' parameter specifies the function
4139 ;; to call. If the function is `ignore' no windows are deleted.
4140 ;; It's up to the function called to avoid infinite recursion.
4141 (throw 'done (funcall function window)))
4142 ((and (window-parameter window 'window-atom)
4143 (setq atom-root (window-atom-root window))
4144 (not (eq atom-root window)))
4145 (if (eq atom-root (frame-root-window frame))
4146 (error "Root of atomic window is root window of its frame")
4147 (throw 'done (delete-other-windows atom-root))))
4148 ((window-parameter window 'window-side)
4149 (error "Cannot make side window the only window"))
4150 ((and (window-minibuffer-p window)
4151 (not (eq window (frame-root-window window))))
4152 (error "Can't expand minibuffer to full frame")))
4154 (cond
4155 ((or ignore-window-parameters
4156 (not (window-with-parameter 'no-delete-other-windows nil frame)))
4157 (setq main (frame-root-window frame)))
4158 ((catch 'tag
4159 (walk-window-tree
4160 (lambda (other)
4161 (when (or (and (window-parameter other 'window-side)
4162 (not (window-parameter
4163 other 'no-delete-other-windows)))
4164 (and (not (window-parameter other 'window-side))
4165 (window-parameter
4166 other 'no-delete-other-windows)))
4167 (throw 'tag nil))))
4169 (setq main (window-main-window frame)))
4171 ;; Delete windows via `delete-window' because we found either a
4172 ;; deletable side window or a non-deletable non-side-window.
4173 (dolist (other (window-list frame))
4174 (when (and (window-live-p other)
4175 (not (eq other window))
4176 (not (window-parameter
4177 other 'no-delete-other-windows))
4178 ;; When WINDOW and the other window are part of the
4179 ;; same atomic window, don't delete the other.
4180 (or (not atom-root)
4181 (not (eq (window-atom-root other) atom-root))))
4182 (condition-case nil
4183 (delete-window other)
4184 (error nil))))
4185 (throw 'done nil)))
4187 ;; If WINDOW is the main window of its frame do nothing.
4188 (unless (eq window main)
4189 (delete-other-windows-internal window main)
4190 (run-window-configuration-change-hook frame)
4191 (window--check frame))
4192 ;; Always return nil.
4193 nil)))
4195 (defun delete-other-windows-vertically (&optional window)
4196 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
4197 This may be a useful alternative binding for \\[delete-other-windows]
4198 if you often split windows horizontally."
4199 (interactive)
4200 (let* ((window (or window (selected-window)))
4201 (edges (window-edges window))
4202 (w window) delenda)
4203 (while (not (eq (setq w (next-window w 1)) window))
4204 (let ((e (window-edges w)))
4205 (when (and (= (car e) (car edges))
4206 (= (nth 2 e) (nth 2 edges)))
4207 (push w delenda))))
4208 (mapc 'delete-window delenda)))
4210 ;;; Windows and buffers.
4212 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
4213 ;; for (1) determining which buffer to show in the window when its
4214 ;; buffer shall be buried or killed and (2) which buffer to show for
4215 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
4217 ;; `prev-buffers' consists of <buffer, window-start, window-point>
4218 ;; triples. The entries on this list are ordered by the time their
4219 ;; buffer has been removed from the window, the most recently removed
4220 ;; buffer's entry being first. The window-start and window-point
4221 ;; components are `window-start' and `window-point' at the time the
4222 ;; buffer was removed from the window which implies that the entry must
4223 ;; be added when `set-window-buffer' removes the buffer from the window.
4225 ;; `next-buffers' is the list of buffers that have been replaced
4226 ;; recently by `switch-to-prev-buffer'. These buffers are the least
4227 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
4228 ;; candidates of `switch-to-next-buffer' to switch to. This list is
4229 ;; reset to nil by any action changing the window's buffer with the
4230 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
4231 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
4232 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
4234 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
4235 ;; if such a buffer was killed while the window was hidden within a
4236 ;; window configuration. Such killed buffers get removed whenever
4237 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
4239 ;; The following function is called by `set-window-buffer' _before_ it
4240 ;; replaces the buffer of the argument window with the new buffer.
4241 (defun record-window-buffer (&optional window)
4242 "Record WINDOW's buffer.
4243 WINDOW must be a live window and defaults to the selected one."
4244 (let* ((window (window-normalize-window window t))
4245 (buffer (window-buffer window))
4246 (entry (assq buffer (window-prev-buffers window))))
4247 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
4248 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
4249 (set-window-next-buffers window nil)
4251 (when entry
4252 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
4253 (set-window-prev-buffers
4254 window (assq-delete-all buffer (window-prev-buffers window))))
4256 ;; Don't record insignificant buffers.
4257 (unless (eq (aref (buffer-name buffer) 0) ?\s)
4258 ;; Add an entry for buffer to WINDOW's previous buffers.
4259 (with-current-buffer buffer
4260 (let ((start (window-start window))
4261 (point (window-point window)))
4262 (setq entry
4263 (cons buffer
4264 (if entry
4265 ;; We have an entry, update marker positions.
4266 (list (set-marker (nth 1 entry) start)
4267 (set-marker (nth 2 entry) point))
4268 ;; Make new markers.
4269 (list (copy-marker start)
4270 (copy-marker
4271 ;; Preserve window-point-insertion-type
4272 ;; (Bug#12588).
4273 point window-point-insertion-type)))))
4274 (set-window-prev-buffers
4275 window (cons entry (window-prev-buffers window)))))
4277 (run-hooks 'buffer-list-update-hook))))
4279 (defun unrecord-window-buffer (&optional window buffer)
4280 "Unrecord BUFFER in WINDOW.
4281 WINDOW must be a live window and defaults to the selected one.
4282 BUFFER must be a live buffer and defaults to the buffer of
4283 WINDOW."
4284 (let* ((window (window-normalize-window window t))
4285 (buffer (or buffer (window-buffer window))))
4286 (set-window-prev-buffers
4287 window (assq-delete-all buffer (window-prev-buffers window)))
4288 (set-window-next-buffers
4289 window (delq buffer (window-next-buffers window)))))
4291 (defun set-window-buffer-start-and-point (window buffer &optional start point)
4292 "Set WINDOW's buffer to BUFFER.
4293 WINDOW must be a live window and defaults to the selected one.
4294 Optional argument START non-nil means set WINDOW's start position
4295 to START. Optional argument POINT non-nil means set WINDOW's
4296 point to POINT. If WINDOW is selected this also sets BUFFER's
4297 `point' to POINT. If WINDOW is selected and the buffer it showed
4298 before was current this also makes BUFFER the current buffer."
4299 (setq window (window-normalize-window window t))
4300 (let ((selected (eq window (selected-window)))
4301 (current (eq (window-buffer window) (current-buffer))))
4302 (set-window-buffer window buffer)
4303 (when (and selected current)
4304 (set-buffer buffer))
4305 (when start
4306 ;; Don't force window-start here (even if POINT is nil).
4307 (set-window-start window start t))
4308 (when point
4309 (set-window-point window point))))
4311 (defcustom switch-to-visible-buffer t
4312 "If non-nil, allow switching to an already visible buffer.
4313 If this variable is non-nil, `switch-to-prev-buffer' and
4314 `switch-to-next-buffer' may switch to an already visible buffer.
4315 If this variable is nil, `switch-to-prev-buffer' and
4316 `switch-to-next-buffer' always try to avoid switching to a buffer
4317 that is already visible in another window on the same frame."
4318 :type 'boolean
4319 :version "24.1"
4320 :group 'windows)
4322 (defun switch-to-prev-buffer (&optional window bury-or-kill)
4323 "In WINDOW switch to previous buffer.
4324 WINDOW must be a live window and defaults to the selected one.
4325 Return the buffer switched to, nil if no suitable buffer could be
4326 found.
4328 Optional argument BURY-OR-KILL non-nil means the buffer currently
4329 shown in WINDOW is about to be buried or killed and consequently
4330 shall not be switched to in future invocations of this command.
4332 As a special case, if BURY-OR-KILL equals `append', this means to
4333 move the buffer to the end of WINDOW's previous buffers list so a
4334 future invocation of `switch-to-prev-buffer' less likely switches
4335 to it."
4336 (interactive)
4337 (let* ((window (window-normalize-window window t))
4338 (frame (window-frame window))
4339 (window-side (window-parameter window 'window-side))
4340 (old-buffer (window-buffer window))
4341 ;; Save this since it's destroyed by `set-window-buffer'.
4342 (next-buffers (window-next-buffers window))
4343 (pred (frame-parameter frame 'buffer-predicate))
4344 entry new-buffer killed-buffers visible)
4345 (when (window-minibuffer-p window)
4346 ;; Don't switch in minibuffer window.
4347 (unless (setq window (minibuffer-selected-window))
4348 (error "Window %s is a minibuffer window" window)))
4350 (unless (memq (window-dedicated-p window) '(nil side))
4351 ;; Don't switch in dedicated window.
4352 (error "Window %s is dedicated to buffer %s" window old-buffer))
4354 (catch 'found
4355 ;; Scan WINDOW's previous buffers first, skipping entries of next
4356 ;; buffers.
4357 (dolist (entry (window-prev-buffers window))
4358 (when (and (setq new-buffer (car entry))
4359 (or (buffer-live-p new-buffer)
4360 (not (setq killed-buffers
4361 (cons new-buffer killed-buffers))))
4362 (not (eq new-buffer old-buffer))
4363 (or (null pred) (funcall pred new-buffer))
4364 ;; When BURY-OR-KILL is nil, avoid switching to a
4365 ;; buffer in WINDOW's next buffers list.
4366 (or bury-or-kill (not (memq new-buffer next-buffers))))
4367 (if (and (not switch-to-visible-buffer)
4368 (get-buffer-window new-buffer frame))
4369 ;; Try to avoid showing a buffer visible in some other
4370 ;; window.
4371 (setq visible new-buffer)
4372 (set-window-buffer-start-and-point
4373 window new-buffer (nth 1 entry) (nth 2 entry))
4374 (throw 'found t))))
4375 ;; Scan reverted buffer list of WINDOW's frame next, skipping
4376 ;; entries of next buffers. Note that when we bury or kill a
4377 ;; buffer we don't reverse the global buffer list to avoid showing
4378 ;; a buried buffer instead. Otherwise, we must reverse the global
4379 ;; buffer list in order to make sure that switching to the
4380 ;; previous/next buffer traverse it in opposite directions. Skip
4381 ;; this step for side windows.
4382 (unless window-side
4383 (dolist (buffer (if bury-or-kill
4384 (buffer-list frame)
4385 (nreverse (buffer-list frame))))
4386 (when (and (buffer-live-p buffer)
4387 (not (eq buffer old-buffer))
4388 (or (null pred) (funcall pred buffer))
4389 (not (eq (aref (buffer-name buffer) 0) ?\s))
4390 ;; Don't show a buffer shown in a side window before.
4391 (not (buffer-local-value 'window--sides-shown buffer))
4392 (or bury-or-kill (not (memq buffer next-buffers))))
4393 (if (and (not switch-to-visible-buffer)
4394 (get-buffer-window buffer frame))
4395 ;; Try to avoid showing a buffer visible in some other window.
4396 (unless visible
4397 (setq visible buffer))
4398 (setq new-buffer buffer)
4399 (set-window-buffer-start-and-point window new-buffer)
4400 (throw 'found t)))))
4401 (unless bury-or-kill
4402 ;; Scan reverted next buffers last (must not use nreverse
4403 ;; here!).
4404 (dolist (buffer (reverse next-buffers))
4405 ;; Actually, buffer _must_ be live here since otherwise it
4406 ;; would have been caught in the scan of previous buffers.
4407 (when (and (or (buffer-live-p buffer)
4408 (not (setq killed-buffers
4409 (cons buffer killed-buffers))))
4410 (not (eq buffer old-buffer))
4411 (or (null pred) (funcall pred buffer))
4412 (setq entry (assq buffer (window-prev-buffers window))))
4413 (setq new-buffer buffer)
4414 (set-window-buffer-start-and-point
4415 window new-buffer (nth 1 entry) (nth 2 entry))
4416 (throw 'found t))))
4418 ;; Show a buffer visible in another window.
4419 (when visible
4420 (setq new-buffer visible)
4421 (set-window-buffer-start-and-point window new-buffer)))
4423 (if bury-or-kill
4424 (let ((entry (and (eq bury-or-kill 'append)
4425 (assq old-buffer (window-prev-buffers window)))))
4426 ;; Remove `old-buffer' from WINDOW's previous and (restored list
4427 ;; of) next buffers.
4428 (set-window-prev-buffers
4429 window (assq-delete-all old-buffer (window-prev-buffers window)))
4430 (set-window-next-buffers window (delq old-buffer next-buffers))
4431 (when entry
4432 ;; Append old-buffer's entry to list of WINDOW's previous
4433 ;; buffers so it's less likely to get switched to soon but
4434 ;; `display-buffer-in-previous-window' can nevertheless find
4435 ;; it.
4436 (set-window-prev-buffers
4437 window (append (window-prev-buffers window) (list entry)))))
4438 ;; Move `old-buffer' to head of WINDOW's restored list of next
4439 ;; buffers.
4440 (set-window-next-buffers
4441 window (cons old-buffer (delq old-buffer next-buffers))))
4443 ;; Remove killed buffers from WINDOW's previous and next buffers.
4444 (when killed-buffers
4445 (dolist (buffer killed-buffers)
4446 (set-window-prev-buffers
4447 window (assq-delete-all buffer (window-prev-buffers window)))
4448 (set-window-next-buffers
4449 window (delq buffer (window-next-buffers window)))))
4451 ;; Return new-buffer.
4452 new-buffer))
4454 (defun switch-to-next-buffer (&optional window)
4455 "In WINDOW switch to next buffer.
4456 WINDOW must be a live window and defaults to the selected one.
4457 Return the buffer switched to, nil if no suitable buffer could be
4458 found."
4459 (interactive)
4460 (let* ((window (window-normalize-window window t))
4461 (frame (window-frame window))
4462 (window-side (window-parameter window 'window-side))
4463 (old-buffer (window-buffer window))
4464 (next-buffers (window-next-buffers window))
4465 (pred (frame-parameter frame 'buffer-predicate))
4466 new-buffer entry killed-buffers visible)
4467 (when (window-minibuffer-p window)
4468 ;; Don't switch in minibuffer window.
4469 (unless (setq window (minibuffer-selected-window))
4470 (error "Window %s is a minibuffer window" window)))
4472 (unless (memq (window-dedicated-p window) '(nil side))
4473 ;; Don't switch in dedicated window.
4474 (error "Window %s is dedicated to buffer %s" window old-buffer))
4476 (catch 'found
4477 ;; Scan WINDOW's next buffers first.
4478 (dolist (buffer next-buffers)
4479 (when (and (or (buffer-live-p buffer)
4480 (not (setq killed-buffers
4481 (cons buffer killed-buffers))))
4482 (not (eq buffer old-buffer))
4483 (or (null pred) (funcall pred buffer))
4484 (setq entry (assq buffer (window-prev-buffers window))))
4485 (setq new-buffer buffer)
4486 (set-window-buffer-start-and-point
4487 window new-buffer (nth 1 entry) (nth 2 entry))
4488 (throw 'found t)))
4489 ;; Scan the buffer list of WINDOW's frame next, skipping previous
4490 ;; buffers entries. Skip this step for side windows.
4491 (unless window-side
4492 (dolist (buffer (buffer-list frame))
4493 (when (and (buffer-live-p buffer)
4494 (not (eq buffer old-buffer))
4495 (or (null pred) (funcall pred buffer))
4496 (not (eq (aref (buffer-name buffer) 0) ?\s))
4497 ;; Don't show a buffer shown in a side window before.
4498 (not (buffer-local-value 'window--sides-shown buffer))
4499 (not (assq buffer (window-prev-buffers window))))
4500 (if (and (not switch-to-visible-buffer)
4501 (get-buffer-window buffer frame))
4502 ;; Try to avoid showing a buffer visible in some other window.
4503 (setq visible buffer)
4504 (setq new-buffer buffer)
4505 (set-window-buffer-start-and-point window new-buffer)
4506 (throw 'found t)))))
4507 ;; Scan WINDOW's reverted previous buffers last (must not use
4508 ;; nreverse here!)
4509 (dolist (entry (reverse (window-prev-buffers window)))
4510 (when (and (setq new-buffer (car entry))
4511 (or (buffer-live-p new-buffer)
4512 (not (setq killed-buffers
4513 (cons new-buffer killed-buffers))))
4514 (not (eq new-buffer old-buffer))
4515 (or (null pred) (funcall pred new-buffer)))
4516 (if (and (not switch-to-visible-buffer)
4517 (get-buffer-window new-buffer frame))
4518 ;; Try to avoid showing a buffer visible in some other window.
4519 (unless visible
4520 (setq visible new-buffer))
4521 (set-window-buffer-start-and-point
4522 window new-buffer (nth 1 entry) (nth 2 entry))
4523 (throw 'found t))))
4525 ;; Show a buffer visible in another window.
4526 (when visible
4527 (setq new-buffer visible)
4528 (set-window-buffer-start-and-point window new-buffer)))
4530 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
4531 (set-window-next-buffers window (delq new-buffer next-buffers))
4533 ;; Remove killed buffers from WINDOW's previous and next buffers.
4534 (when killed-buffers
4535 (dolist (buffer killed-buffers)
4536 (set-window-prev-buffers
4537 window (assq-delete-all buffer (window-prev-buffers window)))
4538 (set-window-next-buffers
4539 window (delq buffer (window-next-buffers window)))))
4541 ;; Return new-buffer.
4542 new-buffer))
4544 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
4545 "Search LIST for a valid buffer to display in FRAME.
4546 Return nil when all buffers in LIST are undesirable for display,
4547 otherwise return the first suitable buffer in LIST.
4549 Buffers not visible in windows are preferred to visible buffers,
4550 unless VISIBLE-OK is non-nil.
4551 If the optional argument FRAME is nil, it defaults to the selected frame.
4552 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
4553 ;; This logic is more or less copied from other-buffer.
4554 (setq frame (or frame (selected-frame)))
4555 (let ((pred (frame-parameter frame 'buffer-predicate))
4556 found buf)
4557 (while (and (not found) list)
4558 (setq buf (car list))
4559 (if (and (not (eq buffer buf))
4560 (buffer-live-p buf)
4561 (or (null pred) (funcall pred buf))
4562 (not (eq (aref (buffer-name buf) 0) ?\s))
4563 (or visible-ok (null (get-buffer-window buf 'visible))))
4564 (setq found buf)
4565 (setq list (cdr list))))
4566 (car list)))
4568 (defun last-buffer (&optional buffer visible-ok frame)
4569 "Return the last buffer in FRAME's buffer list.
4570 If BUFFER is the last buffer, return the preceding buffer
4571 instead. Buffers not visible in windows are preferred to visible
4572 buffers, unless optional argument VISIBLE-OK is non-nil.
4573 Optional third argument FRAME nil or omitted means use the
4574 selected frame's buffer list. If no such buffer exists, return
4575 the buffer `*scratch*', creating it if necessary."
4576 (setq frame (or frame (selected-frame)))
4577 (or (get-next-valid-buffer (nreverse (buffer-list frame))
4578 buffer visible-ok frame)
4579 (get-buffer "*scratch*")
4580 (let ((scratch (get-buffer-create "*scratch*")))
4581 (set-buffer-major-mode scratch)
4582 scratch)))
4584 (defcustom frame-auto-hide-function #'iconify-frame
4585 "Function called to automatically hide frames.
4586 The function is called with one argument - a frame.
4588 Functions affected by this option are those that bury a buffer
4589 shown in a separate frame like `quit-window' and `bury-buffer'."
4590 :type '(choice (const :tag "Iconify" iconify-frame)
4591 (const :tag "Make invisible" make-frame-invisible)
4592 (const :tag "Delete" delete-frame)
4593 (const :tag "Do nothing" ignore)
4594 function)
4595 :group 'windows
4596 :group 'frames
4597 :version "26.1")
4599 (defun window--delete (&optional window dedicated-only kill)
4600 "Delete WINDOW if possible.
4601 WINDOW must be a live window and defaults to the selected one.
4602 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
4603 only if it's dedicated to its buffer. Optional argument KILL
4604 means the buffer shown in window will be killed. Return non-nil
4605 if WINDOW gets deleted or its frame is auto-hidden."
4606 (setq window (window-normalize-window window t))
4607 (unless (and dedicated-only (not (window-dedicated-p window)))
4608 (let ((deletable (window-deletable-p window)))
4609 (cond
4610 ((eq deletable 'frame)
4611 (let ((frame (window-frame window)))
4612 (cond
4613 (kill
4614 (delete-frame frame))
4615 ((functionp (frame-parameter frame 'auto-hide-function))
4616 (funcall (frame-parameter frame 'auto-hide-function)))
4617 ((functionp frame-auto-hide-function)
4618 (funcall frame-auto-hide-function frame))))
4619 'frame)
4620 (deletable
4621 (delete-window window)
4622 t)))))
4624 (defun bury-buffer (&optional buffer-or-name)
4625 "Put BUFFER-OR-NAME at the end of the list of all buffers.
4626 There it is the least likely candidate for `other-buffer' to
4627 return; thus, the least likely buffer for \\[switch-to-buffer] to
4628 select by default.
4630 You can specify a buffer name as BUFFER-OR-NAME, or an actual
4631 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
4632 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
4633 remove the current buffer from the selected window if it is
4634 displayed there."
4635 (interactive)
4636 (let* ((buffer (window-normalize-buffer buffer-or-name)))
4637 ;; If `buffer-or-name' is not on the selected frame we unrecord it
4638 ;; although it's not "here" (call it a feature).
4639 (bury-buffer-internal buffer)
4640 ;; Handle case where `buffer-or-name' is nil and the current buffer
4641 ;; is shown in the selected window.
4642 (cond
4643 ((or buffer-or-name (not (eq buffer (window-buffer)))))
4644 ((window--delete nil t))
4646 ;; Switch to another buffer in window.
4647 (set-window-dedicated-p nil nil)
4648 (switch-to-prev-buffer nil 'bury)))
4650 ;; Always return nil.
4651 nil))
4653 (defun unbury-buffer ()
4654 "Switch to the last buffer in the buffer list."
4655 (interactive)
4656 (switch-to-buffer (last-buffer)))
4658 (defun next-buffer ()
4659 "In selected window switch to next buffer."
4660 (interactive)
4661 (cond
4662 ((window-minibuffer-p)
4663 (error "Cannot switch buffers in minibuffer window"))
4664 ((eq (window-dedicated-p) t)
4665 (error "Window is strongly dedicated to its buffer"))
4667 (switch-to-next-buffer))))
4669 (defun previous-buffer ()
4670 "In selected window switch to previous buffer."
4671 (interactive)
4672 (cond
4673 ((window-minibuffer-p)
4674 (error "Cannot switch buffers in minibuffer window"))
4675 ((eq (window-dedicated-p) t)
4676 (error "Window is strongly dedicated to its buffer"))
4678 (switch-to-prev-buffer))))
4680 (defun delete-windows-on (&optional buffer-or-name frame)
4681 "Delete all windows showing BUFFER-OR-NAME.
4682 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4683 and defaults to the current buffer.
4685 The following non-nil values of the optional argument FRAME
4686 have special meanings:
4688 - t means consider all windows on the selected frame only.
4690 - `visible' means consider all windows on all visible frames on
4691 the current terminal.
4693 - 0 (the number zero) means consider all windows on all visible
4694 and iconified frames on the current terminal.
4696 - A frame means consider all windows on that frame only.
4698 Any other value of FRAME means consider all windows on all
4699 frames.
4701 When a window showing BUFFER-OR-NAME is dedicated and the only
4702 window of its frame, that frame is deleted when there are other
4703 frames left."
4704 (interactive "BDelete windows on (buffer):\nP")
4705 (let ((buffer (window-normalize-buffer buffer-or-name))
4706 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4707 ;; `window-list-1' based function.
4708 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4709 (dolist (window (window-list-1 nil nil all-frames))
4710 (if (eq (window-buffer window) buffer)
4711 (let ((deletable (window-deletable-p window)))
4712 (cond
4713 ((and (eq deletable 'frame) (window-dedicated-p window))
4714 ;; Delete frame if and only if window is dedicated.
4715 (delete-frame (window-frame window)))
4716 ((eq deletable t)
4717 ;; Delete window.
4718 (delete-window window))
4720 ;; In window switch to previous buffer.
4721 (set-window-dedicated-p window nil)
4722 (switch-to-prev-buffer window 'bury))))
4723 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4724 (unrecord-window-buffer window buffer)))))
4726 (defun replace-buffer-in-windows (&optional buffer-or-name)
4727 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
4728 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4729 and defaults to the current buffer.
4731 When a window showing BUFFER-OR-NAME is dedicated, that window is
4732 deleted. If that window is the only window on its frame, the
4733 frame is deleted too when there are other frames left. If there
4734 are no other frames left, some other buffer is displayed in that
4735 window.
4737 This function removes the buffer denoted by BUFFER-OR-NAME from
4738 all window-local buffer lists."
4739 (interactive "bBuffer to replace: ")
4740 (let ((buffer (window-normalize-buffer buffer-or-name)))
4741 (dolist (window (window-list-1 nil nil t))
4742 (if (eq (window-buffer window) buffer)
4743 (unless (window--delete window t t)
4744 ;; Switch to another buffer in window.
4745 (set-window-dedicated-p window nil)
4746 (switch-to-prev-buffer window 'kill))
4747 ;; Unrecord BUFFER in WINDOW.
4748 (unrecord-window-buffer window buffer)))))
4750 (defun quit-restore-window (&optional window bury-or-kill)
4751 "Quit WINDOW and deal with its buffer.
4752 WINDOW must be a live window and defaults to the selected one.
4754 According to information stored in WINDOW's `quit-restore' window
4755 parameter either (1) delete WINDOW and its frame, (2) delete
4756 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4757 or (4) make WINDOW display some other buffer than the present
4758 one. If non-nil, reset `quit-restore' parameter to nil.
4760 Optional second argument BURY-OR-KILL tells how to proceed with
4761 the buffer of WINDOW. The following values are handled:
4763 nil means to not handle the buffer in a particular way. This
4764 means that if WINDOW is not deleted by this function, invoking
4765 `switch-to-prev-buffer' will usually show the buffer again.
4767 `append' means that if WINDOW is not deleted, move its buffer to
4768 the end of WINDOW's previous buffers so it's less likely that a
4769 future invocation of `switch-to-prev-buffer' will switch to it.
4770 Also, move the buffer to the end of the frame's buffer list.
4772 `bury' means that if WINDOW is not deleted, remove its buffer
4773 from WINDOW'S list of previous buffers. Also, move the buffer
4774 to the end of the frame's buffer list. This value provides the
4775 most reliable remedy to not have `switch-to-prev-buffer' switch
4776 to this buffer again without killing the buffer.
4778 `kill' means to kill WINDOW's buffer."
4779 (setq window (window-normalize-window window t))
4780 (let* ((buffer (window-buffer window))
4781 (quit-restore (window-parameter window 'quit-restore))
4782 (prev-buffer
4783 (let* ((prev-buffers (window-prev-buffers window))
4784 (prev-buffer (caar prev-buffers)))
4785 (and (or (not (eq prev-buffer buffer))
4786 (and (cdr prev-buffers)
4787 (not (eq (setq prev-buffer (cadr prev-buffers))
4788 buffer))))
4789 prev-buffer)))
4790 quad entry)
4791 (cond
4792 ((and (not prev-buffer)
4793 (or (eq (nth 1 quit-restore) 'frame)
4794 (and (eq (nth 1 quit-restore) 'window)
4795 ;; If the window has been created on an existing
4796 ;; frame and ended up as the sole window on that
4797 ;; frame, do not delete it (Bug#12764).
4798 (not (eq window (frame-root-window window)))))
4799 (eq (nth 3 quit-restore) buffer)
4800 ;; Delete WINDOW if possible.
4801 (window--delete window nil (eq bury-or-kill 'kill)))
4802 ;; If the previously selected window is still alive, select it.
4803 (when (window-live-p (nth 2 quit-restore))
4804 (select-window (nth 2 quit-restore))))
4805 ((and (listp (setq quad (nth 1 quit-restore)))
4806 (buffer-live-p (car quad))
4807 (eq (nth 3 quit-restore) buffer))
4808 ;; Show another buffer stored in quit-restore parameter.
4809 (when (and (integerp (nth 3 quad))
4810 (if (window-combined-p window)
4811 (/= (nth 3 quad) (window-total-height window))
4812 (/= (nth 3 quad) (window-total-width window))))
4813 ;; Try to resize WINDOW to its old height but don't signal an
4814 ;; error.
4815 (condition-case nil
4816 (window-resize
4817 window
4818 (- (nth 3 quad) (if (window-combined-p window)
4819 (window-total-height window)
4820 (window-total-width window)))
4821 (window-combined-p window t))
4822 (error nil)))
4823 (set-window-dedicated-p window nil)
4824 ;; Restore WINDOW's previous buffer, start and point position.
4825 (set-window-buffer-start-and-point
4826 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
4827 ;; Deal with the buffer we just removed from WINDOW.
4828 (setq entry (and (eq bury-or-kill 'append)
4829 (assq buffer (window-prev-buffers window))))
4830 (when bury-or-kill
4831 ;; Remove buffer from WINDOW's previous and next buffers.
4832 (set-window-prev-buffers
4833 window (assq-delete-all buffer (window-prev-buffers window)))
4834 (set-window-next-buffers
4835 window (delq buffer (window-next-buffers window))))
4836 (when entry
4837 ;; Append old buffer's entry to list of WINDOW's previous
4838 ;; buffers so it's less likely to get switched to soon but
4839 ;; `display-buffer-in-previous-window' can nevertheless find it.
4840 (set-window-prev-buffers
4841 window (append (window-prev-buffers window) (list entry))))
4842 ;; Reset the quit-restore parameter.
4843 (set-window-parameter window 'quit-restore nil)
4844 ;; Select old window.
4845 (when (window-live-p (nth 2 quit-restore))
4846 (select-window (nth 2 quit-restore))))
4848 ;; Show some other buffer in WINDOW and reset the quit-restore
4849 ;; parameter.
4850 (set-window-parameter window 'quit-restore nil)
4851 ;; Make sure that WINDOW is no more dedicated.
4852 (set-window-dedicated-p window nil)
4853 (switch-to-prev-buffer window bury-or-kill)))
4855 ;; Deal with the buffer.
4856 (cond
4857 ((not (buffer-live-p buffer)))
4858 ((eq bury-or-kill 'kill)
4859 (kill-buffer buffer))
4860 (bury-or-kill
4861 (bury-buffer-internal buffer)))))
4863 (defun quit-window (&optional kill window)
4864 "Quit WINDOW and bury its buffer.
4865 WINDOW must be a live window and defaults to the selected one.
4866 With prefix argument KILL non-nil, kill the buffer instead of
4867 burying it.
4869 According to information stored in WINDOW's `quit-restore' window
4870 parameter either (1) delete WINDOW and its frame, (2) delete
4871 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4872 or (4) make WINDOW display some other buffer than the present
4873 one. If non-nil, reset `quit-restore' parameter to nil."
4874 (interactive "P")
4875 (quit-restore-window window (if kill 'kill 'bury)))
4877 (defun quit-windows-on (&optional buffer-or-name kill frame)
4878 "Quit all windows showing BUFFER-OR-NAME.
4879 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4880 and defaults to the current buffer. Optional argument KILL
4881 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4882 BUFFER-OR-NAME. Optional argument FRAME is handled as by
4883 `delete-windows-on'.
4885 This function calls `quit-window' on all candidate windows
4886 showing BUFFER-OR-NAME."
4887 (interactive "BQuit windows on (buffer):\nP")
4888 (let ((buffer (window-normalize-buffer buffer-or-name))
4889 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4890 ;; `window-list-1' based function.
4891 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4892 (dolist (window (window-list-1 nil nil all-frames))
4893 (if (eq (window-buffer window) buffer)
4894 (quit-window kill window)
4895 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4896 (unrecord-window-buffer window buffer)))))
4898 (defun split-window (&optional window size side pixelwise)
4899 "Make a new window adjacent to WINDOW.
4900 WINDOW must be a valid window and defaults to the selected one.
4901 Return the new window which is always a live window.
4903 Optional argument SIZE a positive number means make WINDOW SIZE
4904 lines or columns tall. If SIZE is negative, make the new window
4905 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
4906 absolute value can be less than `window-min-height' or
4907 `window-min-width'; so this command can make a new window as
4908 small as one line or two columns. SIZE defaults to half of
4909 WINDOW's size.
4911 Optional third argument SIDE nil (or `below') specifies that the
4912 new window shall be located below WINDOW. SIDE `above' means the
4913 new window shall be located above WINDOW. In both cases SIZE
4914 specifies the new number of lines for WINDOW (or the new window
4915 if SIZE is negative) including space reserved for the mode and/or
4916 header line.
4918 SIDE t (or `right') specifies that the new window shall be
4919 located on the right side of WINDOW. SIDE `left' means the new
4920 window shall be located on the left of WINDOW. In both cases
4921 SIZE specifies the new number of columns for WINDOW (or the new
4922 window provided SIZE is negative) including space reserved for
4923 fringes and the scrollbar or a divider column. Any other non-nil
4924 value for SIDE is currently handled like t (or `right').
4926 PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
4928 If the variable `ignore-window-parameters' is non-nil or the
4929 `split-window' parameter of WINDOW equals t, do not process any
4930 parameters of WINDOW. Otherwise, if the `split-window' parameter
4931 of WINDOW specifies a function, call that function with all three
4932 arguments and return the value returned by that function.
4934 Otherwise, if WINDOW is part of an atomic window, \"split\" the
4935 root of that atomic window. The new window does not become a
4936 member of that atomic window.
4938 If WINDOW is live, properties of the new window like margins and
4939 scrollbars are inherited from WINDOW. If WINDOW is an internal
4940 window, these properties as well as the buffer displayed in the
4941 new window are inherited from the window selected on WINDOW's
4942 frame. The selected window is not changed by this function."
4943 (setq window (window-normalize-window window))
4944 (let* ((side (cond
4945 ((not side) 'below)
4946 ((memq side '(below above right left)) side)
4947 (t 'right)))
4948 (horizontal (not (memq side '(below above))))
4949 (frame (window-frame window))
4950 (parent (window-parent window))
4951 (function (window-parameter window 'split-window))
4952 (window-side (window-parameter window 'window-side))
4953 ;; Rebind the following two variables since in some cases we
4954 ;; have to override their value.
4955 (window-combination-limit window-combination-limit)
4956 (window-combination-resize window-combination-resize)
4957 (char-size (frame-char-size window horizontal))
4958 (pixel-size
4959 (when (numberp size)
4960 (window--size-to-pixel window size horizontal pixelwise t)))
4961 (divider-width (if horizontal
4962 (frame-right-divider-width frame)
4963 (frame-bottom-divider-width frame)))
4964 atom-root ignore)
4965 (window--check frame)
4966 (catch 'done
4967 (cond
4968 ;; Ignore window parameters if either `ignore-window-parameters'
4969 ;; is t or the `split-window' parameter equals t.
4970 ((or ignore-window-parameters (eq function t)))
4971 ((functionp function)
4972 ;; The `split-window' parameter specifies the function to call.
4973 ;; If that function is `ignore', do nothing.
4974 (throw 'done (funcall function window size side)))
4975 ;; If WINDOW is part of an atomic window, split the root window
4976 ;; of that atomic window instead.
4977 ((and (window-parameter window 'window-atom)
4978 (setq atom-root (window-atom-root window))
4979 (not (eq atom-root window)))
4980 (throw 'done (split-window atom-root size side pixelwise)))
4981 ;; If WINDOW is a side window or its first or last child is a
4982 ;; side window, throw an error unless `window-combination-resize'
4983 ;; equals 'side.
4984 ((and (not (eq window-combination-resize 'side))
4985 (window-parameter window 'window-side))
4986 (error "Cannot split side window or parent of side window"))
4987 ;; If `window-combination-resize' is 'side and window has a side
4988 ;; window sibling, bind `window-combination-limit' to t.
4989 ((and (not (eq window-combination-resize 'side))
4990 (or (and (window-prev-sibling window)
4991 (window-parameter
4992 (window-prev-sibling window) 'window-side))
4993 (and (window-next-sibling window)
4994 (window-parameter
4995 (window-next-sibling window) 'window-side))))
4996 (setq window-combination-limit t)))
4998 ;; If `window-combination-resize' is t and SIZE is non-negative,
4999 ;; bind `window-combination-limit' to t.
5000 (when (and (eq window-combination-resize t)
5001 pixel-size (> pixel-size 0))
5002 (setq window-combination-limit t))
5004 (let* ((parent-pixel-size
5005 ;; `parent-pixel-size' is the pixel size of WINDOW's
5006 ;; parent, provided it has one.
5007 (when parent (window-size parent horizontal t)))
5008 ;; `resize' non-nil means we are supposed to resize other
5009 ;; windows in WINDOW's combination.
5010 (resize
5011 (and window-combination-resize
5012 (or (window-parameter window 'window-side)
5013 (not (eq window-combination-resize 'side)))
5014 (not (eq window-combination-limit t))
5015 ;; Resize makes sense in iso-combinations only.
5016 (window-combined-p window horizontal)))
5017 ;; `old-pixel-size' is the current pixel size of WINDOW.
5018 (old-pixel-size (window-size window horizontal t))
5019 ;; `new-size' is the specified or calculated size of the
5020 ;; new window.
5021 new-pixel-size new-parent new-normal)
5022 (cond
5023 ((not pixel-size)
5024 (setq new-pixel-size
5025 (if resize
5026 ;; When resizing try to give the new window the
5027 ;; average size of a window in its combination.
5028 (max (min (- parent-pixel-size
5029 (window-min-size parent horizontal nil t))
5030 (/ parent-pixel-size
5031 (1+ (window-combinations parent horizontal))))
5032 (window-min-pixel-size))
5033 ;; Else try to give the new window half the size
5034 ;; of WINDOW (plus an eventual odd pixel).
5035 (/ old-pixel-size 2)))
5036 (unless window-resize-pixelwise
5037 ;; Round to nearest char-size multiple.
5038 (setq new-pixel-size
5039 (* char-size (round new-pixel-size char-size)))))
5040 ((>= pixel-size 0)
5041 ;; SIZE non-negative specifies the new size of WINDOW.
5043 ;; Note: Specifying a non-negative SIZE is practically
5044 ;; always done as workaround for making the new window
5045 ;; appear above or on the left of the new window (the
5046 ;; ispell window is a typical example of that). In all
5047 ;; these cases the SIDE argument should be set to 'above
5048 ;; or 'left in order to support the 'resize option.
5049 ;; Here we have to nest the windows instead, see above.
5050 (setq new-pixel-size (- old-pixel-size pixel-size)))
5052 ;; SIZE negative specifies the size of the new window.
5053 (setq new-pixel-size (- pixel-size))))
5055 ;; Check SIZE.
5056 (cond
5057 ((not pixel-size)
5058 (cond
5059 (resize
5060 ;; SIZE unspecified, resizing.
5061 (unless (or (window-sizable-p
5062 parent (- (+ new-pixel-size divider-width)) horizontal
5063 nil t)
5064 (window-sizable-p
5065 parent (- (+ new-pixel-size divider-width)) horizontal
5066 (setq ignore 'preserved) t))
5067 (error "Window %s too small for splitting" parent)))
5068 ((and (> (+ new-pixel-size divider-width
5069 (window-min-size window horizontal nil t))
5070 old-pixel-size)
5071 (> (+ new-pixel-size divider-width
5072 (window-min-size
5073 window horizontal (setq ignore 'preserved) t))
5074 old-pixel-size))
5075 ;; SIZE unspecified, no resizing.
5076 (error "Window %s too small for splitting" window))))
5077 ((and (>= pixel-size 0)
5078 (or (>= pixel-size old-pixel-size)
5079 (< new-pixel-size
5080 (window-safe-min-pixel-size window horizontal))))
5081 ;; SIZE specified as new size of old window. If the new size
5082 ;; is larger than the old size or the size of the new window
5083 ;; would be less than the safe minimum, signal an error.
5084 (error "Window %s too small for splitting" window))
5085 (resize
5086 ;; SIZE specified, resizing.
5087 (unless (or (window-sizable-p
5088 parent (- (+ new-pixel-size divider-width)) horizontal
5089 nil t)
5090 (window-sizable-p
5091 parent (- (+ new-pixel-size divider-width)) horizontal
5092 (setq ignore 'preserved) t))
5093 ;; If we cannot resize the parent give up.
5094 (error "Window %s too small for splitting" parent)))
5095 ((or (< new-pixel-size
5096 (window-safe-min-pixel-size window horizontal))
5097 (< (- old-pixel-size new-pixel-size)
5098 (window-safe-min-pixel-size window horizontal)))
5099 ;; SIZE specification violates minimum size restrictions.
5100 (error "Window %s too small for splitting" window)))
5102 (window--resize-reset frame horizontal)
5104 (setq new-parent
5105 ;; Make new-parent non-nil if we need a new parent window;
5106 ;; either because we want to nest or because WINDOW is not
5107 ;; iso-combined.
5108 (or (eq window-combination-limit t)
5109 (not (window-combined-p window horizontal))))
5110 (setq new-normal
5111 ;; Make new-normal the normal size of the new window.
5112 (cond
5113 (pixel-size (/ (float new-pixel-size)
5114 (if new-parent old-pixel-size parent-pixel-size)))
5115 (new-parent 0.5)
5116 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
5117 (t (/ (window-normal-size window horizontal) 2.0))))
5119 (if resize
5120 ;; Try to get space from OLD's siblings. We could go "up" and
5121 ;; try getting additional space from surrounding windows but
5122 ;; we won't be able to return space to those windows when we
5123 ;; delete the one we create here. Hence we do not go up.
5124 (progn
5125 (window--resize-child-windows
5126 parent (- new-pixel-size) horizontal nil ignore)
5127 (let* ((normal (- 1.0 new-normal))
5128 (sub (window-child parent)))
5129 (while sub
5130 (set-window-new-normal
5131 sub (* (window-normal-size sub horizontal) normal))
5132 (setq sub (window-right sub)))))
5133 ;; Get entire space from WINDOW.
5134 (set-window-new-pixel
5135 window (- old-pixel-size new-pixel-size))
5136 (window--resize-this-window
5137 window (- new-pixel-size) horizontal ignore)
5138 (set-window-new-normal
5139 window (- (if new-parent 1.0 (window-normal-size window horizontal))
5140 new-normal)))
5142 (let* ((new (split-window-internal window new-pixel-size side new-normal)))
5143 (window--pixel-to-total frame horizontal)
5144 ;; Assign window-side parameters, if any.
5145 (cond
5146 ((eq window-combination-resize 'side)
5147 (let ((window-side
5148 (cond
5149 (window-side window-side)
5150 ((eq side 'above) 'top)
5151 ((eq side 'below) 'bottom)
5152 (t side))))
5153 ;; We made a new side window.
5154 (set-window-parameter new 'window-side window-side)
5155 (when (and new-parent (window-parameter window 'window-side))
5156 ;; We've been splitting a side root window. Give the
5157 ;; new parent the same window-side parameter.
5158 (set-window-parameter
5159 (window-parent new) 'window-side window-side))))
5160 ((eq window-combination-resize 'atom)
5161 ;; Make sure `window--check-frame' won't destroy an existing
5162 ;; atomic window in case the new window gets nested inside.
5163 (unless (window-parameter window 'window-atom)
5164 (set-window-parameter window 'window-atom t))
5165 (when new-parent
5166 (set-window-parameter (window-parent new) 'window-atom t))
5167 (set-window-parameter new 'window-atom t)))
5169 ;; Sanitize sizes unless SIZE was specified.
5170 (unless size
5171 (window--sanitize-window-sizes horizontal))
5173 (run-window-configuration-change-hook frame)
5174 (run-window-scroll-functions new)
5175 (window--check frame)
5176 ;; Always return the new window.
5177 new)))))
5179 (defun split-window-no-error (&optional window size side pixelwise)
5180 "Make a new window adjacent to WINDOW.
5181 This function is like `split-window' but does not signal an error
5182 when WINDOW cannot be split.
5184 For the meaning of all arguments see the documentation of
5185 `split-window'."
5186 (condition-case nil
5187 (split-window window size side pixelwise)
5188 (error nil)))
5190 ;; I think this should be the default; I think people will prefer it--rms.
5191 (defcustom split-window-keep-point t
5192 "If non-nil, \\[split-window-below] preserves point in the new window.
5193 If nil, adjust point in the two windows to minimize redisplay.
5194 This option applies only to `split-window-below' and functions
5195 that call it. The low-level `split-window' function always keeps
5196 the original point in both windows."
5197 :type 'boolean
5198 :group 'windows)
5200 (defun split-window-below (&optional size)
5201 "Split the selected window into two windows, one above the other.
5202 The selected window is above. The newly split-off window is
5203 below and displays the same buffer. Return the new window.
5205 If optional argument SIZE is omitted or nil, both windows get the
5206 same height, or close to it. If SIZE is positive, the upper
5207 \(selected) window gets SIZE lines. If SIZE is negative, the
5208 lower (new) window gets -SIZE lines.
5210 If the variable `split-window-keep-point' is non-nil, both
5211 windows get the same value of point as the selected window.
5212 Otherwise, the window starts are chosen so as to minimize the
5213 amount of redisplay; this is convenient on slow terminals."
5214 (interactive "P")
5215 (let ((old-window (selected-window))
5216 (old-point (window-point))
5217 (size (and size (prefix-numeric-value size)))
5218 moved-by-window-height moved new-window bottom)
5219 (when (and size (< size 0) (< (- size) window-min-height))
5220 ;; `split-window' would not signal an error here.
5221 (error "Size of new window too small"))
5222 (setq new-window (split-window nil size))
5223 (unless split-window-keep-point
5224 (with-current-buffer (window-buffer)
5225 ;; Use `save-excursion' around vertical movements below
5226 ;; (Bug#10971). Note: When the selected window's buffer has a
5227 ;; header line, up to two lines of the buffer may not show up
5228 ;; in the resulting configuration.
5229 (save-excursion
5230 (goto-char (window-start))
5231 (setq moved (vertical-motion (window-height)))
5232 (set-window-start new-window (point))
5233 (when (> (point) (window-point new-window))
5234 (set-window-point new-window (point)))
5235 (when (= moved (window-height))
5236 (setq moved-by-window-height t)
5237 (vertical-motion -1))
5238 (setq bottom (point)))
5239 (and moved-by-window-height
5240 (<= bottom (point))
5241 (set-window-point old-window (1- bottom)))
5242 (and moved-by-window-height
5243 (<= (window-start new-window) old-point)
5244 (set-window-point new-window old-point)
5245 (select-window new-window))))
5246 ;; Always copy quit-restore parameter in interactive use.
5247 (let ((quit-restore (window-parameter old-window 'quit-restore)))
5248 (when quit-restore
5249 (set-window-parameter new-window 'quit-restore quit-restore)))
5250 new-window))
5252 (defalias 'split-window-vertically 'split-window-below)
5254 (defun split-window-right (&optional size)
5255 "Split the selected window into two side-by-side windows.
5256 The selected window is on the left. The newly split-off window
5257 is on the right and displays the same buffer. Return the new
5258 window.
5260 If optional argument SIZE is omitted or nil, both windows get the
5261 same width, or close to it. If SIZE is positive, the left-hand
5262 \(selected) window gets SIZE columns. If SIZE is negative, the
5263 right-hand (new) window gets -SIZE columns. Here, SIZE includes
5264 the width of the window's scroll bar; if there are no scroll
5265 bars, it includes the width of the divider column to the window's
5266 right, if any."
5267 (interactive "P")
5268 (let ((old-window (selected-window))
5269 (size (and size (prefix-numeric-value size)))
5270 new-window)
5271 (when (and size (< size 0) (< (- size) window-min-width))
5272 ;; `split-window' would not signal an error here.
5273 (error "Size of new window too small"))
5274 (setq new-window (split-window nil size t))
5275 ;; Always copy quit-restore parameter in interactive use.
5276 (let ((quit-restore (window-parameter old-window 'quit-restore)))
5277 (when quit-restore
5278 (set-window-parameter new-window 'quit-restore quit-restore)))
5279 new-window))
5281 (defalias 'split-window-horizontally 'split-window-right)
5283 ;;; Balancing windows.
5285 ;; The following routine uses the recycled code from an old version of
5286 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
5287 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
5288 ;; more readable (FWIW we'd need three loops - one to calculate the
5289 ;; minimum sizes per window, one to enlarge or shrink windows until the
5290 ;; new parent-size matches, and one where we shrink the largest/enlarge
5291 ;; the smallest window).
5292 (defun balance-windows-2 (window horizontal)
5293 "Subroutine of `balance-windows-1'.
5294 WINDOW must be a vertical combination (horizontal if HORIZONTAL
5295 is non-nil)."
5296 (let* ((char-size (if window-resize-pixelwise
5298 (frame-char-size window horizontal)))
5299 (first (window-child window))
5300 (sub first)
5301 (number-of-children 0)
5302 (parent-size (window-new-pixel window))
5303 (total-sum parent-size)
5304 failed size sub-total sub-delta sub-amount rest)
5305 (while sub
5306 (setq number-of-children (1+ number-of-children))
5307 (when (window-size-fixed-p sub horizontal)
5308 (setq total-sum
5309 (- total-sum (window-size sub horizontal t)))
5310 (set-window-new-normal sub 'ignore))
5311 (setq sub (window-right sub)))
5313 (setq failed t)
5314 (while (and failed (> number-of-children 0))
5315 (setq size (/ total-sum number-of-children))
5316 (setq failed nil)
5317 (setq sub first)
5318 (while (and sub (not failed))
5319 ;; Ignore child windows that should be ignored or are stuck.
5320 (unless (window--resize-child-windows-skip-p sub)
5321 (setq sub-total (window-size sub horizontal t))
5322 (setq sub-delta (- size sub-total))
5323 (setq sub-amount
5324 (window-sizable sub sub-delta horizontal nil t))
5325 ;; Register the new total size for this child window.
5326 (set-window-new-pixel sub (+ sub-total sub-amount))
5327 (unless (= sub-amount sub-delta)
5328 (setq total-sum (- total-sum sub-total sub-amount))
5329 (setq number-of-children (1- number-of-children))
5330 ;; We failed and need a new round.
5331 (setq failed t)
5332 (set-window-new-normal sub 'skip)))
5333 (setq sub (window-right sub))))
5335 ;; How can we be sure that `number-of-children' is NOT zero here ?
5336 (setq rest (% total-sum number-of-children))
5337 ;; Fix rounding by trying to enlarge non-stuck windows by one line
5338 ;; (column) until `rest' is zero.
5339 (setq sub first)
5340 (while (and sub (> rest 0))
5341 (unless (window--resize-child-windows-skip-p window)
5342 (set-window-new-pixel sub (min rest char-size) t)
5343 (setq rest (- rest char-size)))
5344 (setq sub (window-right sub)))
5346 ;; Fix rounding by trying to enlarge stuck windows by one line
5347 ;; (column) until `rest' equals zero.
5348 (setq sub first)
5349 (while (and sub (> rest 0))
5350 (unless (eq (window-new-normal sub) 'ignore)
5351 (set-window-new-pixel sub (min rest char-size) t)
5352 (setq rest (- rest char-size)))
5353 (setq sub (window-right sub)))
5355 (setq sub first)
5356 (while sub
5357 ;; Record new normal sizes.
5358 (set-window-new-normal
5359 sub (/ (if (eq (window-new-normal sub) 'ignore)
5360 (window-size sub horizontal t)
5361 (window-new-pixel sub))
5362 (float parent-size)))
5363 ;; Recursively balance each window's child windows.
5364 (balance-windows-1 sub horizontal)
5365 (setq sub (window-right sub)))))
5367 (defun balance-windows-1 (window &optional horizontal)
5368 "Subroutine of `balance-windows'."
5369 (if (window-child window)
5370 (let ((sub (window-child window)))
5371 (if (window-combined-p sub horizontal)
5372 (balance-windows-2 window horizontal)
5373 (let ((size (window-new-pixel window)))
5374 (while sub
5375 (set-window-new-pixel sub size)
5376 (balance-windows-1 sub horizontal)
5377 (setq sub (window-right sub))))))))
5379 (defun balance-windows (&optional window-or-frame)
5380 "Balance the sizes of windows of WINDOW-OR-FRAME.
5381 WINDOW-OR-FRAME is optional and defaults to the selected frame.
5382 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
5383 windows of that frame. If WINDOW-OR-FRAME denotes a window,
5384 recursively balance the sizes of all child windows of that
5385 window."
5386 (interactive)
5387 (let* ((window
5388 (cond
5389 ((or (not window-or-frame)
5390 (frame-live-p window-or-frame))
5391 (frame-root-window window-or-frame))
5392 ((or (window-live-p window-or-frame)
5393 (window-child window-or-frame))
5394 window-or-frame)
5396 (error "Not a window or frame %s" window-or-frame))))
5397 (frame (window-frame window)))
5398 ;; Balance vertically.
5399 (window--resize-reset (window-frame window))
5400 (balance-windows-1 window)
5401 (when (window--resize-apply-p frame)
5402 (window-resize-apply frame)
5403 (window--pixel-to-total frame)
5404 (run-window-configuration-change-hook frame))
5405 ;; Balance horizontally.
5406 (window--resize-reset (window-frame window) t)
5407 (balance-windows-1 window t)
5408 (when (window--resize-apply-p frame t)
5409 (window-resize-apply frame t)
5410 (window--pixel-to-total frame t)
5411 (run-window-configuration-change-hook frame))))
5413 (defun window-fixed-size-p (&optional window direction)
5414 "Return t if WINDOW cannot be resized in DIRECTION.
5415 WINDOW defaults to the selected window. DIRECTION can be
5416 nil (i.e. any), `height' or `width'."
5417 (with-current-buffer (window-buffer window)
5418 (when (and (boundp 'window-size-fixed) window-size-fixed)
5419 (not (and direction
5420 (member (cons direction window-size-fixed)
5421 '((height . width) (width . height))))))))
5423 ;;; A different solution to balance-windows.
5424 (defvar window-area-factor 1
5425 "Factor by which the window area should be over-estimated.
5426 This is used by `balance-windows-area'.
5427 Changing this globally has no effect.")
5428 (make-variable-buffer-local 'window-area-factor)
5430 (defun balance-windows-area-adjust (window delta horizontal pixelwise)
5431 "Wrapper around `window-resize' with error checking.
5432 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
5433 ;; `window-resize' may fail if delta is too large.
5434 (while (>= (abs delta) 1)
5435 (condition-case nil
5436 (progn
5437 ;; It was wrong to use `window-resize' here. Somehow
5438 ;; `balance-windows-area' depends on resizing windows
5439 ;; asymmetrically.
5440 (adjust-window-trailing-edge window delta horizontal pixelwise)
5441 (setq delta 0))
5442 (error
5443 ;;(message "adjust: %s" (error-message-string err))
5444 (setq delta (/ delta 2))))))
5446 (defun balance-windows-area ()
5447 "Make all visible windows the same area (approximately).
5448 See also `window-area-factor' to change the relative size of
5449 specific buffers."
5450 (interactive)
5451 (let* ((unchanged 0) (carry 0) (round 0)
5452 ;; Remove fixed-size windows.
5453 (wins (delq nil (mapcar (lambda (win)
5454 (if (not (window-fixed-size-p win)) win))
5455 (window-list nil 'nomini))))
5456 (changelog nil)
5457 (pixelwise window-resize-pixelwise)
5458 next)
5459 ;; Resizing a window changes the size of surrounding windows in complex
5460 ;; ways, so it's difficult to balance them all. The introduction of
5461 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
5462 ;; very difficult to do. `balance-window' above takes an off-line
5463 ;; approach: get the whole window tree, then balance it, then try to
5464 ;; adjust the windows so they fit the result.
5465 ;; Here, instead, we take a "local optimization" approach, where we just
5466 ;; go through all the windows several times until nothing needs to be
5467 ;; changed. The main problem with this approach is that it's difficult
5468 ;; to make sure it terminates, so we use some heuristic to try and break
5469 ;; off infinite loops.
5470 ;; After a round without any change, we allow a second, to give a chance
5471 ;; to the carry to propagate a minor imbalance from the end back to
5472 ;; the beginning.
5473 (while (< unchanged 2)
5474 ;; (message "New round")
5475 (setq unchanged (1+ unchanged) round (1+ round))
5476 (dolist (win wins)
5477 (setq next win)
5478 (while (progn (setq next (next-window next))
5479 (window-fixed-size-p next)))
5480 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
5481 (let* ((horiz
5482 (< (car (window-pixel-edges win)) (car (window-pixel-edges next))))
5483 (areadiff (/ (- (* (window-size next nil pixelwise)
5484 (window-size next t pixelwise)
5485 (buffer-local-value 'window-area-factor
5486 (window-buffer next)))
5487 (* (window-size win nil pixelwise)
5488 (window-size win t pixelwise)
5489 (buffer-local-value 'window-area-factor
5490 (window-buffer win))))
5491 (max (buffer-local-value 'window-area-factor
5492 (window-buffer win))
5493 (buffer-local-value 'window-area-factor
5494 (window-buffer next)))))
5495 (edgesize (if horiz
5496 (+ (window-size win nil pixelwise)
5497 (window-size next nil pixelwise))
5498 (+ (window-size win t pixelwise)
5499 (window-size next t pixelwise))))
5500 (diff (/ areadiff edgesize)))
5501 (when (zerop diff)
5502 ;; Maybe diff is actually closer to 1 than to 0.
5503 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
5504 (when (and (zerop diff) (not (zerop areadiff)))
5505 (setq diff (/ (+ areadiff carry) edgesize))
5506 ;; Change things smoothly.
5507 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
5508 (if (zerop diff)
5509 ;; Make sure negligible differences don't accumulate to
5510 ;; become significant.
5511 (setq carry (+ carry areadiff))
5512 ;; This used `adjust-window-trailing-edge' before and uses
5513 ;; `window-resize' now. Error wrapping is still needed.
5514 (balance-windows-area-adjust win diff horiz pixelwise)
5515 ;; (sit-for 0.5)
5516 (let ((change (cons win (window-pixel-edges win))))
5517 ;; If the same change has been seen already for this window,
5518 ;; we're most likely in an endless loop, so don't count it as
5519 ;; a change.
5520 (unless (member change changelog)
5521 (push change changelog)
5522 (setq unchanged 0 carry 0)))))))
5523 ;; We've now basically balanced all the windows.
5524 ;; But there may be some minor off-by-one imbalance left over,
5525 ;; so let's do some fine tuning.
5526 ;; (bw-finetune wins)
5527 ;; (message "Done in %d rounds" round)
5530 ;;; Window states, how to get them and how to put them in a window.
5531 (defun window--state-get-1 (window &optional writable)
5532 "Helper function for `window-state-get'."
5533 (let* ((type
5534 (cond
5535 ((window-top-child window) 'vc)
5536 ((window-left-child window) 'hc)
5537 (t 'leaf)))
5538 (buffer (window-buffer window))
5539 (selected (eq window (selected-window)))
5540 (head
5541 `(,type
5542 ,@(unless (window-next-sibling window) `((last . t)))
5543 (pixel-width . ,(window-pixel-width window))
5544 (pixel-height . ,(window-pixel-height window))
5545 (total-width . ,(window-total-width window))
5546 (total-height . ,(window-total-height window))
5547 (normal-height . ,(window-normal-size window))
5548 (normal-width . ,(window-normal-size window t))
5549 ,@(unless (window-live-p window)
5550 `((combination-limit . ,(window-combination-limit window))))
5551 ,@(let ((parameters (window-parameters window))
5552 list)
5553 ;; Make copies of those window parameters whose
5554 ;; persistence property is `writable' if WRITABLE is
5555 ;; non-nil and non-nil if WRITABLE is nil.
5556 (dolist (par parameters)
5557 (let ((pers (cdr (assq (car par)
5558 window-persistent-parameters))))
5559 (when (and pers (or (not writable) (eq pers 'writable)))
5560 (setq list (cons (cons (car par) (cdr par)) list)))))
5561 ;; Add `clone-of' parameter if necessary.
5562 (let ((pers (cdr (assq 'clone-of
5563 window-persistent-parameters))))
5564 (when (and pers (or (not writable) (eq pers 'writable))
5565 (not (assq 'clone-of list)))
5566 (setq list (cons (cons 'clone-of window) list))))
5567 (when list
5568 `((parameters . ,list))))
5569 ,@(when buffer
5570 ;; All buffer related things go in here.
5571 (let ((point (window-point window))
5572 (start (window-start window)))
5573 `((buffer
5574 ,(buffer-name buffer)
5575 (selected . ,selected)
5576 (hscroll . ,(window-hscroll window))
5577 (fringes . ,(window-fringes window))
5578 (margins . ,(window-margins window))
5579 (scroll-bars . ,(window-scroll-bars window))
5580 (vscroll . ,(window-vscroll window))
5581 (dedicated . ,(window-dedicated-p window))
5582 (point . ,(if writable
5583 point
5584 (with-current-buffer buffer
5585 (copy-marker point
5586 (buffer-local-value
5587 'window-point-insertion-type
5588 buffer)))))
5589 (start . ,(if writable
5590 start
5591 (with-current-buffer buffer
5592 (copy-marker start))))))))))
5593 (tail
5594 (when (memq type '(vc hc))
5595 (let (list)
5596 (setq window (window-child window))
5597 (while window
5598 (setq list (cons (window--state-get-1 window writable) list))
5599 (setq window (window-right window)))
5600 (nreverse list)))))
5601 (append head tail)))
5603 (defun window-state-get (&optional window writable)
5604 "Return state of WINDOW as a Lisp object.
5605 WINDOW can be any window and defaults to the root window of the
5606 selected frame.
5608 Optional argument WRITABLE non-nil means do not use markers for
5609 sampling `window-point' and `window-start'. Together, WRITABLE
5610 and the variable `window-persistent-parameters' specify which
5611 window parameters are saved by this function. WRITABLE should be
5612 non-nil when the return value shall be written to a file and read
5613 back in another session. Otherwise, an application may run into
5614 an `invalid-read-syntax' error while attempting to read back the
5615 value from file.
5617 The return value can be used as argument for `window-state-put'
5618 to put the state recorded here into an arbitrary window. The
5619 value can be also stored on disk and read back in a new session."
5620 (setq window
5621 (if window
5622 (if (window-valid-p window)
5623 window
5624 (error "%s is not a live or internal window" window))
5625 (frame-root-window)))
5626 ;; The return value is a cons whose car specifies some constraints on
5627 ;; the size of WINDOW. The cdr lists the states of the child windows
5628 ;; of WINDOW.
5629 (cons
5630 ;; Frame related things would go into a function, say `frame-state',
5631 ;; calling `window-state-get' to insert the frame's root window.
5632 `((min-height . ,(window-min-size window))
5633 (min-width . ,(window-min-size window t))
5634 (min-height-ignore . ,(window-min-size window nil t))
5635 (min-width-ignore . ,(window-min-size window t t))
5636 (min-height-safe . ,(window-min-size window nil 'safe))
5637 (min-width-safe . ,(window-min-size window t 'safe))
5638 (min-pixel-height . ,(window-min-size window nil nil t))
5639 (min-pixel-width . ,(window-min-size window t nil t))
5640 (min-pixel-height-ignore . ,(window-min-size window nil t t))
5641 (min-pixel-width-ignore . ,(window-min-size window t t t))
5642 (min-pixel-height-safe . ,(window-min-size window nil 'safe t))
5643 (min-pixel-width-safe . ,(window-min-size window t 'safe t)))
5644 (window--state-get-1 window writable)))
5646 (defvar window-state-put-list nil
5647 "Helper variable for `window-state-put'.")
5649 (defvar window-state-put-stale-windows nil
5650 "Helper variable for `window-state-put'.")
5652 (defun window--state-put-1 (state &optional window ignore totals pixelwise)
5653 "Helper function for `window-state-put'."
5654 (let ((type (car state)))
5655 (setq state (cdr state))
5656 (cond
5657 ((eq type 'leaf)
5658 ;; For a leaf window just add unprocessed entries to
5659 ;; `window-state-put-list'.
5660 (push (cons window state) window-state-put-list))
5661 ((memq type '(vc hc))
5662 (let* ((horizontal (eq type 'hc))
5663 (total (window-size window horizontal pixelwise))
5664 (first t)
5665 (window-combination-limit (cdr (assq 'combination-limit state)))
5666 size new)
5667 (dolist (item state)
5668 ;; Find the next child window. WINDOW always points to the
5669 ;; real window that we want to fill with what we find here.
5670 (when (memq (car item) '(leaf vc hc))
5671 (if (assq 'last item)
5672 ;; The last child window. Below `window--state-put-1'
5673 ;; will put into it whatever ITEM has in store.
5674 (setq new nil)
5675 ;; Not the last child window, prepare for splitting
5676 ;; WINDOW. SIZE is the new (and final) size of the old
5677 ;; window.
5678 (setq size
5679 (if totals
5680 ;; Use total size.
5681 (if pixelwise
5682 (cdr (assq (if horizontal
5683 'pixel-width
5684 'pixel-height)
5685 item))
5686 (cdr (assq (if horizontal
5687 'total-width
5688 'total-height)
5689 item)))
5690 ;; Use normalized size and round.
5691 (round
5692 (* total
5693 (cdr (assq (if horizontal 'normal-width 'normal-height)
5694 item))))))
5696 ;; Use safe sizes, we try to resize later.
5697 (setq size (max size
5698 (if horizontal
5699 (* window-safe-min-width
5700 (if pixelwise
5701 (frame-char-width (window-frame window))
5703 (* window-safe-min-height
5704 (if pixelwise
5705 (frame-char-height (window-frame window))
5706 1)))))
5707 (if (window-sizable-p window (- size) horizontal 'safe pixelwise)
5708 (progn
5709 (setq new (split-window-no-error
5710 window size horizontal pixelwise))
5711 (setq window-combination-limit nil))
5712 ;; Give up if we can't resize window down to safe sizes.
5713 (error "Cannot resize window %s" window))
5715 (when first
5716 (setq first nil)
5717 ;; When creating the first child window add for parent
5718 ;; unprocessed entries to `window-state-put-list'.
5719 (setq window-state-put-list
5720 (cons (cons (window-parent window) state)
5721 window-state-put-list))))
5723 ;; Now process the current window (either the one we've just
5724 ;; split or the last child of its parent).
5725 (window--state-put-1 item window ignore totals)
5726 ;; Continue with the last window split off.
5727 (setq window new))))))))
5729 (defun window--state-put-2 (ignore pixelwise)
5730 "Helper function for `window-state-put'."
5731 (dolist (item window-state-put-list)
5732 (let ((window (car item))
5733 (combination-limit (cdr (assq 'combination-limit item)))
5734 (parameters (cdr (assq 'parameters item)))
5735 (state (cdr (assq 'buffer item))))
5736 (when combination-limit
5737 (set-window-combination-limit window combination-limit))
5738 ;; Reset window's parameters and assign saved ones (we might want
5739 ;; a `remove-window-parameters' function here).
5740 (dolist (parameter (window-parameters window))
5741 (set-window-parameter window (car parameter) nil))
5742 (when parameters
5743 (dolist (parameter parameters)
5744 (set-window-parameter window (car parameter) (cdr parameter))))
5745 ;; Process buffer related state.
5746 (when state
5747 (let ((buffer (get-buffer (car state))))
5748 (if buffer
5749 (with-current-buffer buffer
5750 (set-window-buffer window buffer)
5751 (set-window-hscroll window (cdr (assq 'hscroll state)))
5752 (apply 'set-window-fringes
5753 (cons window (cdr (assq 'fringes state))))
5754 (let ((margins (cdr (assq 'margins state))))
5755 (set-window-margins window (car margins) (cdr margins)))
5756 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
5757 (set-window-scroll-bars
5758 window (car scroll-bars) (nth 2 scroll-bars)
5759 (nth 3 scroll-bars) (nth 5 scroll-bars)))
5760 (set-window-vscroll window (cdr (assq 'vscroll state)))
5761 ;; Adjust vertically.
5762 (if (or (memq window-size-fixed '(t height))
5763 (window-preserved-size window))
5764 ;; A fixed height window, try to restore the
5765 ;; original size.
5766 (let ((delta
5767 (- (cdr (assq
5768 (if pixelwise 'pixel-height 'total-height)
5769 item))
5770 (window-size window nil pixelwise)))
5771 window-size-fixed)
5772 (when (window--resizable-p
5773 window delta nil nil nil nil nil pixelwise)
5774 (window-resize window delta nil nil pixelwise)))
5775 ;; Else check whether the window is not high enough.
5776 (let* ((min-size
5777 (window-min-size window nil ignore pixelwise))
5778 (delta
5779 (- min-size (window-size window nil pixelwise))))
5780 (when (and (> delta 0)
5781 (window--resizable-p
5782 window delta nil ignore nil nil nil pixelwise))
5783 (window-resize window delta nil ignore pixelwise))))
5784 ;; Adjust horizontally.
5785 (if (or (memq window-size-fixed '(t width))
5786 (window-preserved-size window t))
5787 ;; A fixed width window, try to restore the original
5788 ;; size.
5789 (let ((delta
5790 (- (cdr (assq
5791 (if pixelwise 'pixel-width 'total-width)
5792 item))
5793 (window-size window t pixelwise)))
5794 window-size-fixed)
5795 (when (window--resizable-p
5796 window delta t nil nil nil nil pixelwise)
5797 (window-resize window delta t nil pixelwise)))
5798 ;; Else check whether the window is not wide enough.
5799 (let* ((min-size (window-min-size window t ignore pixelwise))
5800 (delta (- min-size (window-size window t pixelwise))))
5801 (when (and (> delta 0)
5802 (window--resizable-p
5803 window delta t ignore nil nil nil pixelwise))
5804 (window-resize window delta t ignore pixelwise))))
5805 ;; Set dedicated status.
5806 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
5807 ;; Install positions (maybe we should do this after all
5808 ;; windows have been created and sized).
5809 (ignore-errors
5810 ;; Set 'noforce argument to avoid that window start
5811 ;; overrides window point set below (Bug#24240).
5812 (set-window-start window (cdr (assq 'start state)) 'noforce)
5813 (set-window-point window (cdr (assq 'point state))))
5814 ;; Select window if it's the selected one.
5815 (when (cdr (assq 'selected state))
5816 (select-window window)))
5817 ;; We don't want to raise an error in case the buffer does
5818 ;; not exist anymore, so we switch to a previous one and
5819 ;; save the window with the intention of deleting it later
5820 ;; if possible.
5821 (switch-to-prev-buffer window)
5822 (push window window-state-put-stale-windows)))))))
5824 (defun window-state-put (state &optional window ignore)
5825 "Put window state STATE into WINDOW.
5826 STATE should be the state of a window returned by an earlier
5827 invocation of `window-state-get'. Optional argument WINDOW must
5828 specify a valid window and defaults to the selected one. If
5829 WINDOW is not live, replace WINDOW by a live one before putting
5830 STATE into it.
5832 Optional argument IGNORE non-nil means ignore minimum window
5833 sizes and fixed size restrictions. IGNORE equal `safe' means
5834 windows can get as small as `window-safe-min-height' and
5835 `window-safe-min-width'."
5836 (setq window-state-put-stale-windows nil)
5837 (setq window (window-normalize-window window))
5839 ;; When WINDOW is internal, reduce it to a live one to put STATE into,
5840 ;; see Bug#16793.
5841 (unless (window-live-p window)
5842 (let ((root window))
5843 (setq window (catch 'live
5844 (walk-window-subtree
5845 (lambda (window)
5846 (when (and (window-live-p window)
5847 (not (window-parameter window 'window-side)))
5848 (throw 'live window)))
5849 root)))
5850 (delete-other-windows-internal window root)))
5852 (set-window-dedicated-p window nil)
5854 (let* ((frame (window-frame window))
5855 (head (car state))
5856 ;; We check here (1) whether the total sizes of root window of
5857 ;; STATE and that of WINDOW are equal so we can avoid
5858 ;; calculating new sizes, and (2) if we do have to resize
5859 ;; whether we can do so without violating size restrictions.
5860 (pixelwise (and (cdr (assq 'pixel-width state))
5861 (cdr (assq 'pixel-height state))))
5862 (totals (or (and pixelwise
5863 (= (window-pixel-width window)
5864 (cdr (assq 'pixel-width state)))
5865 (= (window-pixel-height window)
5866 (cdr (assq 'pixel-height state))))
5867 (and (= (window-total-width window)
5868 (cdr (assq 'total-width state)))
5869 (= (window-total-height window)
5870 (cdr (assq 'total-height state))))))
5871 (min-height (cdr (assq
5872 (if pixelwise 'min-pixel-height 'min-height)
5873 head)))
5874 (min-width (cdr (assq
5875 (if pixelwise 'min-pixel-width 'min-weight)
5876 head))))
5877 (if (and (not totals)
5878 (or (> min-height (window-size window nil pixelwise))
5879 (> min-width (window-size window t pixelwise)))
5880 (or (not ignore)
5881 (and (setq min-height
5882 (cdr (assq
5883 (if pixelwise
5884 'min-pixel-height-ignore
5885 'min-height-ignore)
5886 head)))
5887 (setq min-width
5888 (cdr (assq
5889 (if pixelwise
5890 'min-pixel-width-ignore
5891 'min-width-ignore)
5892 head)))
5893 (or (> min-height
5894 (window-size window nil pixelwise))
5895 (> min-width
5896 (window-size window t pixelwise)))
5897 (or (not (eq ignore 'safe))
5898 (and (setq min-height
5899 (cdr (assq
5900 (if pixelwise
5901 'min-pixel-height-safe
5902 'min-height-safe)
5903 head)))
5904 (setq min-width
5905 (cdr (assq
5906 (if pixelwise
5907 'min-pixel-width-safe
5908 'min-width-safe)
5909 head)))
5910 (or (> min-height
5911 (window-size window nil pixelwise))
5912 (> min-width
5913 (window-size window t pixelwise))))))))
5914 ;; The check above might not catch all errors due to rounding
5915 ;; issues - so IGNORE equal 'safe might not always produce the
5916 ;; minimum possible state. But such configurations hardly make
5917 ;; sense anyway.
5918 (error "Window %s too small to accommodate state" window)
5919 (setq state (cdr state))
5920 (setq window-state-put-list nil)
5921 ;; Work on the windows of a temporary buffer to make sure that
5922 ;; splitting proceeds regardless of any buffer local values of
5923 ;; `window-size-fixed'. Release that buffer after the buffers of
5924 ;; all live windows have been set by `window--state-put-2'.
5925 (with-temp-buffer
5926 (set-window-buffer window (current-buffer))
5927 (window--state-put-1 state window nil totals pixelwise)
5928 (window--state-put-2 ignore pixelwise))
5929 (while window-state-put-stale-windows
5930 (let ((window (pop window-state-put-stale-windows)))
5931 (when (eq (window-deletable-p window) t)
5932 (delete-window window))))
5933 (window--check frame))))
5935 (defun window-swap-states (&optional window-1 window-2 size)
5936 "Swap the states of live windows WINDOW-1 and WINDOW-2.
5937 WINDOW-1 must specify a live window and defaults to the selected
5938 one. WINDOW-2 must specify a live window and defaults to the
5939 window following WINDOW-1 in the cyclic ordering of windows,
5940 excluding minibuffer windows and including live windows on all
5941 visible frames.
5943 Optional argument SIZE non-nil means to try swapping the sizes of
5944 WINDOW-1 and WINDOW-2 as well. A value of `height' means to swap
5945 heights only, a value of `width' means to swap widths only, while
5946 t means to swap both widths and heights, if possible. Frames are
5947 not resized by this function."
5948 (interactive)
5949 (setq window-1 (window-normalize-window window-1 t))
5950 (if window-2
5951 (unless (window-live-p window-2)
5952 (error "%s is not a live window" window-2))
5953 (setq window-2 (next-window window-1 'nomini 'visible)))
5954 (unless (eq window-1 window-2)
5955 (let* ((height (memq size '(t height)))
5956 (width (memq size '(t width)))
5957 (state-1 (window-state-get window-1))
5958 (width-1 (and width (window-text-width window-1 t)))
5959 (height-1 (and height (window-text-height window-1 t)))
5960 (state-2 (window-state-get window-2))
5961 (width-2 (and width (window-text-width window-2 t)))
5962 (height-2 (and height (window-text-height window-2 t)))
5963 old preserved)
5964 ;; Swap basic states.
5965 (window-state-put state-1 window-2 t)
5966 (window-state-put state-2 window-1 t)
5967 ;; Swap overlays with `window' property.
5968 (with-current-buffer (window-buffer window-1)
5969 (dolist (overlay (overlays-in (point-min) (point-max)))
5970 (let ((window (overlay-get overlay 'window)))
5971 (cond
5972 ((not window))
5973 ((eq window window-1)
5974 (overlay-put overlay 'window window-2))
5975 ((eq window window-2)
5976 (overlay-put overlay 'window window-1))))))
5977 (unless (eq (window-buffer window-1) (window-buffer window-2))
5978 (with-current-buffer (window-buffer window-2)
5979 (dolist (overlay (overlays-in (point-min) (point-max)))
5980 (let ((window (overlay-get overlay 'window)))
5981 (cond
5982 ((not window))
5983 ((eq window window-1)
5984 (overlay-put overlay 'window window-2))
5985 ((eq window window-2)
5986 (overlay-put overlay 'window window-1)))))))
5987 ;; Try to swap window sizes.
5988 (when size
5989 (unless (= (setq old (window-text-width window-1 t)) width-2)
5990 (window-resize-no-error window-1 (- width-2 old) t t t))
5991 (unless (= (setq old (window-text-width window-2 t)) width-1)
5992 (setq preserved (window-preserved-size window-1 t))
5993 (window-preserve-size window-1 t t)
5994 (window-resize-no-error window-2 (- width-1 old) t t t)
5995 (window-preserve-size window-1 t preserved))
5996 (unless (= (setq old (window-text-height window-1 t)) height-2)
5997 (window-resize-no-error window-1 (- height-2 old) nil t t))
5998 (unless (= (setq old (window-text-height window-2 t)) height-1)
5999 (setq preserved (window-preserved-size window-1))
6000 (window-preserve-size window-1 nil t)
6001 (window-resize-no-error window-2 (- height-1 old) nil t t)
6002 (window-preserve-size window-1 nil preserved))))))
6004 (defun display-buffer-record-window (type window buffer)
6005 "Record information for window used by `display-buffer'.
6006 TYPE specifies the type of the calling operation and must be one
6007 of the symbols `reuse' (when WINDOW existed already and was
6008 reused for displaying BUFFER), `window' (when WINDOW was created
6009 on an already existing frame), or `frame' (when WINDOW was
6010 created on a new frame). WINDOW is the window used for or created
6011 by the `display-buffer' routines. BUFFER is the buffer that
6012 shall be displayed.
6014 This function installs or updates the quit-restore parameter of
6015 WINDOW. The quit-restore parameter is a list of four elements:
6016 The first element is one of the symbols `window', `frame', `same' or
6017 `other'. The second element is either one of the symbols `window'
6018 or `frame' or a list whose elements are the buffer previously
6019 shown in the window, that buffer's window start and window point,
6020 and the window's height. The third element is the window
6021 selected at the time the parameter was created. The fourth
6022 element is BUFFER."
6023 (cond
6024 ((eq type 'reuse)
6025 (if (eq (window-buffer window) buffer)
6026 ;; WINDOW shows BUFFER already. Update WINDOW's quit-restore
6027 ;; parameter, if any.
6028 (let ((quit-restore (window-parameter window 'quit-restore)))
6029 (when (consp quit-restore)
6030 (setcar quit-restore 'same)
6031 ;; The selected-window might have changed in
6032 ;; between (Bug#20353).
6033 (unless (or (eq window (selected-window))
6034 (eq window (nth 2 quit-restore)))
6035 (setcar (cddr quit-restore) (selected-window)))))
6036 ;; WINDOW shows another buffer.
6037 (with-current-buffer (window-buffer window)
6038 (set-window-parameter
6039 window 'quit-restore
6040 (list 'other
6041 ;; A quadruple of WINDOW's buffer, start, point and height.
6042 (list (current-buffer) (window-start window)
6043 ;; Preserve window-point-insertion-type (Bug#12588).
6044 (copy-marker
6045 (window-point window) window-point-insertion-type)
6046 (if (window-combined-p window)
6047 (window-total-height window)
6048 (window-total-width window)))
6049 (selected-window) buffer)))))
6050 ((eq type 'window)
6051 ;; WINDOW has been created on an existing frame.
6052 (set-window-parameter
6053 window 'quit-restore
6054 (list 'window 'window (selected-window) buffer)))
6055 ((eq type 'frame)
6056 ;; WINDOW has been created on a new frame.
6057 (set-window-parameter
6058 window 'quit-restore
6059 (list 'frame 'frame (selected-window) buffer)))))
6061 (defcustom display-buffer-function nil
6062 "If non-nil, function to call to handle `display-buffer'.
6063 It will receive two args, the buffer and a flag which if non-nil
6064 means that the currently selected window is not acceptable. It
6065 should choose or create a window, display the specified buffer in
6066 it, and return the window.
6068 The specified function should call `display-buffer-record-window'
6069 with corresponding arguments to set up the quit-restore parameter
6070 of the window used."
6071 :type '(choice
6072 (const nil)
6073 (function :tag "function"))
6074 :group 'windows)
6076 (make-obsolete-variable 'display-buffer-function
6077 'display-buffer-alist "24.3")
6079 ;; Eventually, we want to turn this into a defvar; instead of
6080 ;; customizing this, the user should use a `pop-up-frame-parameters'
6081 ;; alist entry in `display-buffer-base-action'.
6082 (defcustom pop-up-frame-alist nil
6083 "Alist of parameters for automatically generated new frames.
6084 If non-nil, the value you specify here is used by the default
6085 `pop-up-frame-function' for the creation of new frames.
6087 Since `pop-up-frame-function' is used by `display-buffer' for
6088 making new frames, any value specified here by default affects
6089 the automatic generation of new frames via `display-buffer' and
6090 all functions based on it. The behavior of `make-frame' is not
6091 affected by this variable."
6092 :type '(repeat (cons :format "%v"
6093 (symbol :tag "Parameter")
6094 (sexp :tag "Value")))
6095 :group 'frames)
6097 (defcustom pop-up-frame-function
6098 (lambda () (make-frame pop-up-frame-alist))
6099 "Function used by `display-buffer' for creating a new frame.
6100 This function is called with no arguments and should return a new
6101 frame. The default value calls `make-frame' with the argument
6102 `pop-up-frame-alist'."
6103 :type 'function
6104 :group 'frames)
6106 (defcustom special-display-buffer-names nil
6107 "List of names of buffers that should be displayed specially.
6108 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
6109 its name is in this list, displays the buffer in a way specified
6110 by `special-display-function'. `special-display-popup-frame'
6111 \(the default for `special-display-function') usually displays
6112 the buffer in a separate frame made with the parameters specified
6113 by `special-display-frame-alist'. If `special-display-function'
6114 has been set to some other function, that function is called with
6115 the buffer as first, and nil as second argument.
6117 Alternatively, an element of this list can be specified as
6118 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
6119 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
6120 `special-display-popup-frame' will interpret such pairs as frame
6121 parameters when it creates a special frame, overriding the
6122 corresponding values from `special-display-frame-alist'.
6124 As a special case, if FRAME-PARAMETERS contains (same-window . t)
6125 `special-display-popup-frame' displays that buffer in the
6126 selected window. If FRAME-PARAMETERS contains (same-frame . t),
6127 it displays that buffer in a window on the selected frame.
6129 If `special-display-function' specifies some other function than
6130 `special-display-popup-frame', that function is called with the
6131 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
6132 argument.
6134 Finally, an element of this list can be also specified as
6135 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
6136 `special-display-popup-frame' will call FUNCTION with the buffer
6137 named BUFFER-NAME as first argument, and OTHER-ARGS as the
6138 second.
6140 Any alternative function specified here is responsible for
6141 setting up the quit-restore parameter of the window used.
6143 If this variable appears \"not to work\", because you added a
6144 name to it but the corresponding buffer is displayed in the
6145 selected window, look at the values of `same-window-buffer-names'
6146 and `same-window-regexps'. Those variables take precedence over
6147 this one.
6149 See also `special-display-regexps'."
6150 :type '(repeat
6151 (choice :tag "Buffer"
6152 :value ""
6153 (string :format "%v")
6154 (cons :tag "With parameters"
6155 :format "%v"
6156 :value ("" . nil)
6157 (string :format "%v")
6158 (repeat :tag "Parameters"
6159 (cons :format "%v"
6160 (symbol :tag "Parameter")
6161 (sexp :tag "Value"))))
6162 (list :tag "With function"
6163 :format "%v"
6164 :value ("" . nil)
6165 (string :format "%v")
6166 (function :tag "Function")
6167 (repeat :tag "Arguments" (sexp)))))
6168 :group 'windows
6169 :group 'frames)
6170 (make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3")
6171 (put 'special-display-buffer-names 'risky-local-variable t)
6173 (defcustom special-display-regexps nil
6174 "List of regexps saying which buffers should be displayed specially.
6175 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
6176 any regexp in this list matches its name, displays it specially
6177 using `special-display-function'. `special-display-popup-frame'
6178 \(the default for `special-display-function') usually displays
6179 the buffer in a separate frame made with the parameters specified
6180 by `special-display-frame-alist'. If `special-display-function'
6181 has been set to some other function, that function is called with
6182 the buffer as first, and nil as second argument.
6184 Alternatively, an element of this list can be specified as
6185 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
6186 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
6187 `special-display-popup-frame' will then interpret these pairs as
6188 frame parameters when creating a special frame for a buffer whose
6189 name matches REGEXP, overriding the corresponding values from
6190 `special-display-frame-alist'.
6192 As a special case, if FRAME-PARAMETERS contains (same-window . t)
6193 `special-display-popup-frame' displays buffers matching REGEXP in
6194 the selected window. (same-frame . t) in FRAME-PARAMETERS means
6195 to display such buffers in a window on the selected frame.
6197 If `special-display-function' specifies some other function than
6198 `special-display-popup-frame', that function is called with the
6199 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
6200 as second argument.
6202 Finally, an element of this list can be also specified as
6203 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
6204 will then call FUNCTION with the buffer whose name matched
6205 REGEXP as first, and OTHER-ARGS as second argument.
6207 Any alternative function specified here is responsible for
6208 setting up the quit-restore parameter of the window used.
6210 If this variable appears \"not to work\", because you added a
6211 name to it but the corresponding buffer is displayed in the
6212 selected window, look at the values of `same-window-buffer-names'
6213 and `same-window-regexps'. Those variables take precedence over
6214 this one.
6216 See also `special-display-buffer-names'."
6217 :type '(repeat
6218 (choice :tag "Buffer"
6219 :value ""
6220 (regexp :format "%v")
6221 (cons :tag "With parameters"
6222 :format "%v"
6223 :value ("" . nil)
6224 (regexp :format "%v")
6225 (repeat :tag "Parameters"
6226 (cons :format "%v"
6227 (symbol :tag "Parameter")
6228 (sexp :tag "Value"))))
6229 (list :tag "With function"
6230 :format "%v"
6231 :value ("" . nil)
6232 (regexp :format "%v")
6233 (function :tag "Function")
6234 (repeat :tag "Arguments" (sexp)))))
6235 :group 'windows
6236 :group 'frames)
6237 (make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3")
6238 (put 'special-display-regexps 'risky-local-variable t)
6240 (defun special-display-p (buffer-name)
6241 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
6242 More precisely, return t if `special-display-buffer-names' or
6243 `special-display-regexps' contain a string entry equaling or
6244 matching BUFFER-NAME. If `special-display-buffer-names' or
6245 `special-display-regexps' contain a list entry whose car equals
6246 or matches BUFFER-NAME, the return value is the cdr of that
6247 entry."
6248 (let (tmp)
6249 (cond
6250 ((member buffer-name special-display-buffer-names)
6252 ((setq tmp (assoc buffer-name special-display-buffer-names))
6253 (cdr tmp))
6254 ((catch 'found
6255 (dolist (regexp special-display-regexps)
6256 (cond
6257 ((stringp regexp)
6258 (when (string-match-p regexp buffer-name)
6259 (throw 'found t)))
6260 ((and (consp regexp) (stringp (car regexp))
6261 (string-match-p (car regexp) buffer-name))
6262 (throw 'found (cdr regexp))))))))))
6264 (defcustom special-display-frame-alist
6265 '((height . 14) (width . 80) (unsplittable . t))
6266 "Alist of parameters for special frames.
6267 Special frames are used for buffers whose names are listed in
6268 `special-display-buffer-names' and for buffers whose names match
6269 one of the regular expressions in `special-display-regexps'.
6271 This variable can be set in your init file, like this:
6273 (setq special-display-frame-alist \\='((width . 80) (height . 20)))
6275 These supersede the values given in `default-frame-alist'."
6276 :type '(repeat (cons :format "%v"
6277 (symbol :tag "Parameter")
6278 (sexp :tag "Value")))
6279 :group 'frames)
6280 (make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3")
6282 (defun special-display-popup-frame (buffer &optional args)
6283 "Pop up a frame displaying BUFFER and return its window.
6284 If BUFFER is already displayed in a visible or iconified frame,
6285 raise that frame. Otherwise, display BUFFER in a new frame.
6287 Optional argument ARGS is a list specifying additional
6288 information.
6290 If ARGS is an alist, use it as a list of frame parameters. If
6291 these parameters contain (same-window . t), display BUFFER in
6292 the selected window. If they contain (same-frame . t), display
6293 BUFFER in a window of the selected frame.
6295 If ARGS is a list whose car is a symbol, use (car ARGS) as a
6296 function to do the work. Pass it BUFFER as first argument, and
6297 pass the elements of (cdr ARGS) as the remaining arguments."
6298 (if (and args (symbolp (car args)))
6299 (apply (car args) buffer (cdr args))
6300 (let ((window (get-buffer-window buffer 0)))
6302 ;; If we have a window already, make it visible.
6303 (when window
6304 (let ((frame (window-frame window)))
6305 (make-frame-visible frame)
6306 (raise-frame frame)
6307 (display-buffer-record-window 'reuse window buffer)
6308 window))
6309 ;; Reuse the current window if the user requested it.
6310 (when (cdr (assq 'same-window args))
6311 (condition-case nil
6312 (progn (switch-to-buffer buffer nil t) (selected-window))
6313 (error nil)))
6314 ;; Stay on the same frame if requested.
6315 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
6316 (let* ((pop-up-windows t)
6317 pop-up-frames
6318 special-display-buffer-names special-display-regexps)
6319 (display-buffer buffer)))
6320 ;; If no window yet, make one in a new frame.
6321 (let* ((frame
6322 (with-current-buffer buffer
6323 (make-frame (append args special-display-frame-alist))))
6324 (window (frame-selected-window frame)))
6325 (display-buffer-record-window 'frame window buffer)
6326 (unless (eq buffer (window-buffer window))
6327 (set-window-buffer window buffer)
6328 (set-window-prev-buffers window nil))
6329 (set-window-dedicated-p window t)
6330 window)))))
6332 (defcustom special-display-function 'special-display-popup-frame
6333 "Function to call for displaying special buffers.
6334 This function is called with two arguments - the buffer and,
6335 optionally, a list - and should return a window displaying that
6336 buffer. The default value usually makes a separate frame for the
6337 buffer using `special-display-frame-alist' to specify the frame
6338 parameters. See the definition of `special-display-popup-frame'
6339 for how to specify such a function.
6341 A buffer is special when its name is either listed in
6342 `special-display-buffer-names' or matches a regexp in
6343 `special-display-regexps'.
6345 The specified function should call `display-buffer-record-window'
6346 with corresponding arguments to set up the quit-restore parameter
6347 of the window used."
6348 :type 'function
6349 :group 'frames)
6350 (make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3")
6352 (defcustom same-window-buffer-names nil
6353 "List of names of buffers that should appear in the \"same\" window.
6354 `display-buffer' and `pop-to-buffer' show a buffer whose name is
6355 on this list in the selected rather than some other window.
6357 An element of this list can be a cons cell instead of just a
6358 string. In that case, the cell's car must be a string specifying
6359 the buffer name. This is for compatibility with
6360 `special-display-buffer-names'; the cdr of the cons cell is
6361 ignored.
6363 See also `same-window-regexps'."
6364 :type '(repeat (string :format "%v"))
6365 :group 'windows)
6367 (defcustom same-window-regexps nil
6368 "List of regexps saying which buffers should appear in the \"same\" window.
6369 `display-buffer' and `pop-to-buffer' show a buffer whose name
6370 matches a regexp on this list in the selected rather than some
6371 other window.
6373 An element of this list can be a cons cell instead of just a
6374 string. In that case, the cell's car must be a regexp matching
6375 the buffer name. This is for compatibility with
6376 `special-display-regexps'; the cdr of the cons cell is ignored.
6378 See also `same-window-buffer-names'."
6379 :type '(repeat (regexp :format "%v"))
6380 :group 'windows)
6382 (defun same-window-p (buffer-name)
6383 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
6384 This function returns non-nil if `display-buffer' or
6385 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
6386 selected rather than (as usual) some other window. See
6387 `same-window-buffer-names' and `same-window-regexps'."
6388 (cond
6389 ((not (stringp buffer-name)))
6390 ;; The elements of `same-window-buffer-names' can be buffer
6391 ;; names or cons cells whose cars are buffer names.
6392 ((member buffer-name same-window-buffer-names))
6393 ((assoc buffer-name same-window-buffer-names))
6394 ((catch 'found
6395 (dolist (regexp same-window-regexps)
6396 ;; The elements of `same-window-regexps' can be regexps
6397 ;; or cons cells whose cars are regexps.
6398 (when (or (and (stringp regexp)
6399 (string-match-p regexp buffer-name))
6400 (and (consp regexp) (stringp (car regexp))
6401 (string-match-p (car regexp) buffer-name)))
6402 (throw 'found t)))))))
6404 (defcustom pop-up-frames nil
6405 "Whether `display-buffer' should make a separate frame.
6406 If nil, never make a separate frame.
6407 If the value is `graphic-only', make a separate frame
6408 on graphic displays only.
6409 Any other non-nil value means always make a separate frame."
6410 :type '(choice
6411 (const :tag "Never" nil)
6412 (const :tag "On graphic displays only" graphic-only)
6413 (const :tag "Always" t))
6414 :group 'windows)
6416 (defcustom display-buffer-reuse-frames nil
6417 "Non-nil means `display-buffer' should reuse frames.
6418 If the buffer in question is already displayed in a frame, raise
6419 that frame."
6420 :type 'boolean
6421 :version "21.1"
6422 :group 'windows)
6424 (make-obsolete-variable
6425 'display-buffer-reuse-frames
6426 "use a `reusable-frames' alist entry in `display-buffer-alist'."
6427 "24.3")
6429 (defcustom pop-up-windows t
6430 "Non-nil means `display-buffer' should make a new window."
6431 :type 'boolean
6432 :group 'windows)
6434 (defcustom split-window-preferred-function 'split-window-sensibly
6435 "Function called by `display-buffer' routines to split a window.
6436 This function is called with a window as single argument and is
6437 supposed to split that window and return the new window. If the
6438 window can (or shall) not be split, it is supposed to return nil.
6439 The default is to call the function `split-window-sensibly' which
6440 tries to split the window in a way which seems most suitable.
6441 You can customize the options `split-height-threshold' and/or
6442 `split-width-threshold' in order to have `split-window-sensibly'
6443 prefer either vertical or horizontal splitting.
6445 If you set this to any other function, bear in mind that the
6446 `display-buffer' routines may call this function two times. The
6447 argument of the first call is the largest window on its frame.
6448 If that call fails to return a live window, the function is
6449 called again with the least recently used window as argument. If
6450 that call fails too, `display-buffer' will use an existing window
6451 to display its buffer.
6453 The window selected at the time `display-buffer' was invoked is
6454 still selected when this function is called. Hence you can
6455 compare the window argument with the value of `selected-window'
6456 if you intend to split the selected window instead or if you do
6457 not want to split the selected window."
6458 :type 'function
6459 :version "23.1"
6460 :group 'windows)
6462 (defcustom split-height-threshold 80
6463 "Minimum height for splitting windows sensibly.
6464 If this is an integer, `split-window-sensibly' may split a window
6465 vertically only if it has at least this many lines. If this is
6466 nil, `split-window-sensibly' is not allowed to split a window
6467 vertically. If, however, a window is the only window on its
6468 frame, `split-window-sensibly' may split it vertically
6469 disregarding the value of this variable."
6470 :type '(choice (const nil) (integer :tag "lines"))
6471 :version "23.1"
6472 :group 'windows)
6474 (defcustom split-width-threshold 160
6475 "Minimum width for splitting windows sensibly.
6476 If this is an integer, `split-window-sensibly' may split a window
6477 horizontally only if it has at least this many columns. If this
6478 is nil, `split-window-sensibly' is not allowed to split a window
6479 horizontally."
6480 :type '(choice (const nil) (integer :tag "columns"))
6481 :version "23.1"
6482 :group 'windows)
6484 (defun window-splittable-p (window &optional horizontal)
6485 "Return non-nil if `split-window-sensibly' may split WINDOW.
6486 Optional argument HORIZONTAL nil or omitted means check whether
6487 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
6488 non-nil means check whether WINDOW may be split horizontally.
6490 WINDOW may be split vertically when the following conditions
6491 hold:
6492 - `window-size-fixed' is either nil or equals `width' for the
6493 buffer of WINDOW.
6494 - `split-height-threshold' is an integer and WINDOW is at least as
6495 high as `split-height-threshold'.
6496 - When WINDOW is split evenly, the emanating windows are at least
6497 `window-min-height' lines tall and can accommodate at least one
6498 line plus - if WINDOW has one - a mode line.
6500 WINDOW may be split horizontally when the following conditions
6501 hold:
6502 - `window-size-fixed' is either nil or equals `height' for the
6503 buffer of WINDOW.
6504 - `split-width-threshold' is an integer and WINDOW is at least as
6505 wide as `split-width-threshold'.
6506 - When WINDOW is split evenly, the emanating windows are at least
6507 `window-min-width' or two (whichever is larger) columns wide."
6508 (when (and (window-live-p window)
6509 (not (window-parameter window 'window-side)))
6510 (with-current-buffer (window-buffer window)
6511 (if horizontal
6512 ;; A window can be split horizontally when its width is not
6513 ;; fixed, it is at least `split-width-threshold' columns wide
6514 ;; and at least twice as wide as `window-min-width' and 2 (the
6515 ;; latter value is hardcoded).
6516 (and (memq window-size-fixed '(nil height))
6517 ;; Testing `window-full-width-p' here hardly makes any
6518 ;; sense nowadays. This can be done more intuitively by
6519 ;; setting up `split-width-threshold' appropriately.
6520 (numberp split-width-threshold)
6521 (>= (window-width window)
6522 (max split-width-threshold
6523 (* 2 (max window-min-width 2)))))
6524 ;; A window can be split vertically when its height is not
6525 ;; fixed, it is at least `split-height-threshold' lines high,
6526 ;; and it is at least twice as high as `window-min-height' and 2
6527 ;; if it has a mode line or 1.
6528 (and (memq window-size-fixed '(nil width))
6529 (numberp split-height-threshold)
6530 (>= (window-height window)
6531 (max split-height-threshold
6532 (* 2 (max window-min-height
6533 (if mode-line-format 2 1))))))))))
6535 (defun split-window-sensibly (&optional window)
6536 "Split WINDOW in a way suitable for `display-buffer'.
6537 WINDOW defaults to the currently selected window.
6538 If `split-height-threshold' specifies an integer, WINDOW is at
6539 least `split-height-threshold' lines tall and can be split
6540 vertically, split WINDOW into two windows one above the other and
6541 return the lower window. Otherwise, if `split-width-threshold'
6542 specifies an integer, WINDOW is at least `split-width-threshold'
6543 columns wide and can be split horizontally, split WINDOW into two
6544 windows side by side and return the window on the right. If this
6545 can't be done either and WINDOW is the only window on its frame,
6546 try to split WINDOW vertically disregarding any value specified
6547 by `split-height-threshold'. If that succeeds, return the lower
6548 window. Return nil otherwise.
6550 By default `display-buffer' routines call this function to split
6551 the largest or least recently used window. To change the default
6552 customize the option `split-window-preferred-function'.
6554 You can enforce this function to not split WINDOW horizontally,
6555 by setting (or binding) the variable `split-width-threshold' to
6556 nil. If, in addition, you set `split-height-threshold' to zero,
6557 chances increase that this function does split WINDOW vertically.
6559 In order to not split WINDOW vertically, set (or bind) the
6560 variable `split-height-threshold' to nil. Additionally, you can
6561 set `split-width-threshold' to zero to make a horizontal split
6562 more likely to occur.
6564 Have a look at the function `window-splittable-p' if you want to
6565 know how `split-window-sensibly' determines whether WINDOW can be
6566 split."
6567 (let ((window (or window (selected-window))))
6568 (or (and (window-splittable-p window)
6569 ;; Split window vertically.
6570 (with-selected-window window
6571 (split-window-below)))
6572 (and (window-splittable-p window t)
6573 ;; Split window horizontally.
6574 (with-selected-window window
6575 (split-window-right)))
6576 (and (eq window (frame-root-window (window-frame window)))
6577 (not (window-minibuffer-p window))
6578 ;; If WINDOW is the only window on its frame and is not the
6579 ;; minibuffer window, try to split it vertically disregarding
6580 ;; the value of `split-height-threshold'.
6581 (let ((split-height-threshold 0))
6582 (when (window-splittable-p window)
6583 (with-selected-window window
6584 (split-window-below))))))))
6586 (defun window--try-to-split-window (window &optional alist)
6587 "Try to split WINDOW.
6588 Return value returned by `split-window-preferred-function' if it
6589 represents a live window, nil otherwise."
6590 (and (window-live-p window)
6591 (not (frame-parameter (window-frame window) 'unsplittable))
6592 (let* ((window-combination-limit
6593 ;; When `window-combination-limit' equals
6594 ;; `display-buffer' or equals `resize-window' and a
6595 ;; `window-height' or `window-width' alist entry are
6596 ;; present, bind it to t so resizing steals space
6597 ;; preferably from the window that was split.
6598 (if (or (eq window-combination-limit 'display-buffer)
6599 (and (eq window-combination-limit 'window-size)
6600 (or (cdr (assq 'window-height alist))
6601 (cdr (assq 'window-width alist)))))
6603 window-combination-limit))
6604 (new-window
6605 ;; Since `split-window-preferred-function' might
6606 ;; throw an error use `condition-case'.
6607 (condition-case nil
6608 (funcall split-window-preferred-function window)
6609 (error nil))))
6610 (and (window-live-p new-window) new-window))))
6612 (defun window--frame-usable-p (frame)
6613 "Return FRAME if it can be used to display a buffer."
6614 (when (frame-live-p frame)
6615 (let ((window (frame-root-window frame)))
6616 ;; `frame-root-window' may be an internal window which is considered
6617 ;; "dead" by `window-live-p'. Hence if `window' is not live we
6618 ;; implicitly know that `frame' has a visible window we can use.
6619 (unless (and (window-live-p window)
6620 (or (window-minibuffer-p window)
6621 ;; If the window is soft-dedicated, the frame is usable.
6622 ;; Actually, even if the window is really dedicated,
6623 ;; the frame is still usable by splitting it.
6624 ;; At least Emacs-22 allowed it, and it is desirable
6625 ;; when displaying same-frame windows.
6626 nil ; (eq t (window-dedicated-p window))
6628 frame))))
6630 (defcustom even-window-sizes t
6631 "If non-nil `display-buffer' will try to even window sizes.
6632 Otherwise `display-buffer' will leave the window configuration
6633 alone. Special values are `height-only' to even heights only and
6634 `width-only' to even widths only. Any other value means to even
6635 any of them."
6636 :type '(choice
6637 (const :tag "Never" nil)
6638 (const :tag "Side-by-side windows only" width-only)
6639 (const :tag "Windows above or below only" height-only)
6640 (const :tag "Always" t))
6641 :version "25.1"
6642 :group 'windows)
6643 (defvaralias 'even-window-heights 'even-window-sizes)
6645 (defun window--even-window-sizes (window)
6646 "Even sizes of WINDOW and selected window.
6647 Even only if these windows are the only children of their parent,
6648 `even-window-sizes' has the appropriate value and the selected
6649 window is larger than WINDOW."
6650 (when (and (= (window-child-count (window-parent window)) 2)
6651 (eq (window-parent) (window-parent window)))
6652 (cond
6653 ((and (not (memq even-window-sizes '(nil height-only)))
6654 (window-combined-p window t)
6655 (> (window-total-width) (window-total-width window)))
6656 (condition-case nil
6657 (enlarge-window
6658 (/ (- (window-total-width window) (window-total-width)) 2) t)
6659 (error nil)))
6660 ((and (not (memq even-window-sizes '(nil width-only)))
6661 (window-combined-p window)
6662 (> (window-total-height) (window-total-height window)))
6663 (condition-case nil
6664 (enlarge-window
6665 (/ (- (window-total-height window) (window-total-height)) 2))
6666 (error nil))))))
6668 (defun window--display-buffer (buffer window type &optional alist dedicated)
6669 "Display BUFFER in WINDOW.
6670 TYPE must be one of the symbols `reuse', `window' or `frame' and
6671 is passed unaltered to `display-buffer-record-window'. ALIST is
6672 the alist argument of `display-buffer'. Set `window-dedicated-p'
6673 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
6674 live."
6675 (when (and (buffer-live-p buffer) (window-live-p window))
6676 (display-buffer-record-window type window buffer)
6677 (unless (eq buffer (window-buffer window))
6678 (set-window-dedicated-p window nil)
6679 (set-window-buffer window buffer))
6680 (when dedicated
6681 (set-window-dedicated-p window dedicated))
6682 (when (memq type '(window frame))
6683 (set-window-prev-buffers window nil))
6684 (let ((quit-restore (window-parameter window 'quit-restore))
6685 (height (cdr (assq 'window-height alist)))
6686 (width (cdr (assq 'window-width alist)))
6687 (size (cdr (assq 'window-size alist)))
6688 (preserve-size (cdr (assq 'preserve-size alist))))
6689 (cond
6690 ((or (eq type 'frame)
6691 (and (eq (car quit-restore) 'same)
6692 (eq (nth 1 quit-restore) 'frame)))
6693 ;; Adjust size of frame if asked for.
6694 (cond
6695 ((not size))
6696 ((consp size)
6697 (let ((width (car size))
6698 (height (cdr size))
6699 (frame (window-frame window)))
6700 (when (and (numberp width) (numberp height))
6701 (set-frame-height
6702 frame (+ (frame-height frame)
6703 (- height (window-total-height window))))
6704 (set-frame-width
6705 frame (+ (frame-width frame)
6706 (- width (window-total-width window)))))))
6707 ((functionp size)
6708 (ignore-errors (funcall size window)))))
6709 ((or (eq type 'window)
6710 (and (eq (car quit-restore) 'same)
6711 (eq (nth 1 quit-restore) 'window)))
6712 ;; Adjust height of window if asked for.
6713 (cond
6714 ((not height))
6715 ((numberp height)
6716 (let* ((new-height
6717 (if (integerp height)
6718 height
6719 (round
6720 (* (window-total-height (frame-root-window window))
6721 height))))
6722 (delta (- new-height (window-total-height window))))
6723 (when (and (window--resizable-p window delta nil 'safe)
6724 (window-combined-p window))
6725 (window-resize window delta nil 'safe))))
6726 ((functionp height)
6727 (ignore-errors (funcall height window))))
6728 ;; Adjust width of window if asked for.
6729 (cond
6730 ((not width))
6731 ((numberp width)
6732 (let* ((new-width
6733 (if (integerp width)
6734 width
6735 (round
6736 (* (window-total-width (frame-root-window window))
6737 width))))
6738 (delta (- new-width (window-total-width window))))
6739 (when (and (window--resizable-p window delta t 'safe)
6740 (window-combined-p window t))
6741 (window-resize window delta t 'safe))))
6742 ((functionp width)
6743 (ignore-errors (funcall width window))))
6744 ;; Preserve window size if asked for.
6745 (when (consp preserve-size)
6746 (window-preserve-size window t (car preserve-size))
6747 (window-preserve-size window nil (cdr preserve-size)))))
6748 ;; Assign any window parameters specified.
6749 (let ((parameters (cdr (assq 'window-parameters alist))))
6750 (dolist (parameter parameters)
6751 (set-window-parameter
6752 window (car parameter) (cdr parameter)))))
6753 window))
6755 (defun window--maybe-raise-frame (frame)
6756 (make-frame-visible frame)
6757 (unless (or (frame-parameter frame 'no-focus-on-map)
6758 ;; Don't raise frames that should not get focus.
6759 (frame-parameter frame 'no-accept-focus)
6760 ;; Assume the selected frame is already visible enough.
6761 (eq frame (selected-frame))
6762 ;; Assume the frame from which we invoked the
6763 ;; minibuffer is visible.
6764 (and (minibuffer-window-active-p (selected-window))
6765 (eq frame (window-frame (minibuffer-selected-window)))))
6766 (raise-frame frame)))
6768 ;; FIXME: Not implemented.
6769 ;; FIXME: By the way, there could be more levels of dedication:
6770 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
6771 ;; the window hasn't been used for something else yet.
6772 ;; - `soft' (`softly') dedicated only allows reuse when asked explicitly.
6773 ;; - `strongly' never allows reuse.
6774 (defvar display-buffer-mark-dedicated nil
6775 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
6776 The actual non-nil value of this variable will be copied to the
6777 `window-dedicated-p' flag.")
6779 (defconst display-buffer--action-function-custom-type
6780 '(choice :tag "Function"
6781 (const :tag "--" ignore) ; default for insertion
6782 (const display-buffer-reuse-window)
6783 (const display-buffer-pop-up-window)
6784 (const display-buffer-same-window)
6785 (const display-buffer-pop-up-frame)
6786 (const display-buffer-in-child-frame)
6787 (const display-buffer-below-selected)
6788 (const display-buffer-at-bottom)
6789 (const display-buffer-in-previous-window)
6790 (const display-buffer-use-some-window)
6791 (const display-buffer-use-some-frame)
6792 (function :tag "Other function"))
6793 "Custom type for `display-buffer' action functions.")
6795 (defconst display-buffer--action-custom-type
6796 `(cons :tag "Action"
6797 (choice :tag "Action functions"
6798 ,display-buffer--action-function-custom-type
6799 (repeat
6800 :tag "List of functions"
6801 ,display-buffer--action-function-custom-type))
6802 (alist :tag "Action arguments"
6803 :key-type symbol
6804 :value-type (sexp :tag "Value")))
6805 "Custom type for `display-buffer' actions.")
6807 (defvar display-buffer-overriding-action '(nil . nil)
6808 "Overriding action to perform to display a buffer.
6809 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6810 function or a list of functions. Each function should accept two
6811 arguments: a buffer to display and an alist similar to ALIST.
6812 See `display-buffer' for details.")
6813 (put 'display-buffer-overriding-action 'risky-local-variable t)
6815 (defcustom display-buffer-alist nil
6816 "Alist of conditional actions for `display-buffer'.
6817 This is a list of elements (CONDITION . ACTION), where:
6819 CONDITION is either a regexp matching buffer names, or a
6820 function that takes two arguments - a buffer name and the
6821 ACTION argument of `display-buffer' - and returns a boolean.
6823 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
6824 function or a list of functions. Each such function should
6825 accept two arguments: a buffer to display and an alist of the
6826 same form as ALIST. See `display-buffer' for details.
6828 `display-buffer' scans this alist until it either finds a
6829 matching regular expression or the function specified by a
6830 condition returns non-nil. In any of these cases, it adds the
6831 associated action to the list of actions it will try."
6832 :type `(alist :key-type
6833 (choice :tag "Condition"
6834 regexp
6835 (function :tag "Matcher function"))
6836 :value-type ,display-buffer--action-custom-type)
6837 :risky t
6838 :version "24.1"
6839 :group 'windows)
6841 (defcustom display-buffer-base-action '(nil . nil)
6842 "User-specified default action for `display-buffer'.
6843 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6844 function or a list of functions. Each function should accept two
6845 arguments: a buffer to display and an alist similar to ALIST.
6846 See `display-buffer' for details."
6847 :type display-buffer--action-custom-type
6848 :risky t
6849 :version "24.1"
6850 :group 'windows)
6852 (defconst display-buffer-fallback-action
6853 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
6854 display-buffer-reuse-window
6855 display-buffer--maybe-pop-up-frame-or-window
6856 display-buffer-in-previous-window
6857 display-buffer-use-some-window
6858 ;; If all else fails, pop up a new frame.
6859 display-buffer-pop-up-frame))
6860 "Default fallback action for `display-buffer'.
6861 This is the action used by `display-buffer' if no other actions
6862 specified, e.g. by the user options `display-buffer-alist' or
6863 `display-buffer-base-action'. See `display-buffer'.")
6864 (put 'display-buffer-fallback-action 'risky-local-variable t)
6866 (defun display-buffer-assq-regexp (buffer-name alist action)
6867 "Retrieve ALIST entry corresponding to BUFFER-NAME.
6868 ACTION is the action argument passed to `display-buffer'."
6869 (catch 'match
6870 (dolist (entry alist)
6871 (let ((key (car entry)))
6872 (when (or (and (stringp key)
6873 (string-match-p key buffer-name))
6874 (and (functionp key)
6875 (funcall key buffer-name action)))
6876 (throw 'match (cdr entry)))))))
6878 (defvar display-buffer--same-window-action
6879 '(display-buffer-same-window
6880 (inhibit-same-window . nil))
6881 "A `display-buffer' action for displaying in the same window.")
6882 (put 'display-buffer--same-window-action 'risky-local-variable t)
6884 (defvar display-buffer--other-frame-action
6885 '((display-buffer-reuse-window
6886 display-buffer-pop-up-frame)
6887 (reusable-frames . 0)
6888 (inhibit-same-window . t))
6889 "A `display-buffer' action for displaying in another frame.")
6890 (put 'display-buffer--other-frame-action 'risky-local-variable t)
6892 (defun display-buffer (buffer-or-name &optional action frame)
6893 "Display BUFFER-OR-NAME in some window, without selecting it.
6894 BUFFER-OR-NAME must be a buffer or the name of an existing
6895 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
6896 or nil if no such window is found.
6898 Optional argument ACTION, if non-nil, should specify a display
6899 action. Its form is described below.
6901 Optional argument FRAME, if non-nil, acts like an additional
6902 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
6903 specifying the frame(s) to search for a window that is already
6904 displaying the buffer. See `display-buffer-reuse-window'.
6906 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
6907 where FUNCTION is either a function or a list of functions, and
6908 ALIST is an arbitrary association list (alist).
6910 Each such FUNCTION should accept two arguments: the buffer to
6911 display and an alist. Based on those arguments, it should
6912 display the buffer and return the window. If the caller is
6913 prepared to handle the case of not displaying the buffer
6914 and returning nil from `display-buffer' it should pass
6915 \(allow-no-window . t) as an element of the ALIST.
6917 The `display-buffer' function builds a function list and an alist
6918 by combining the functions and alists specified in
6919 `display-buffer-overriding-action', `display-buffer-alist', the
6920 ACTION argument, `display-buffer-base-action', and
6921 `display-buffer-fallback-action' (in order). Then it calls each
6922 function in the combined function list in turn, passing the
6923 buffer as the first argument and the combined alist as the second
6924 argument, until one of the functions returns non-nil.
6926 If ACTION is nil, the function list and the alist are built using
6927 only the other variables mentioned above.
6929 Available action functions include:
6930 `display-buffer-same-window'
6931 `display-buffer-reuse-window'
6932 `display-buffer-pop-up-frame'
6933 `display-buffer-in-child-frame'
6934 `display-buffer-pop-up-window'
6935 `display-buffer-in-previous-window'
6936 `display-buffer-use-some-window'
6937 `display-buffer-use-some-frame'
6939 Recognized alist entries include:
6941 `inhibit-same-window' -- A non-nil value prevents the same
6942 window from being used for display.
6944 `inhibit-switch-frame' -- A non-nil value prevents any other
6945 frame from being raised or selected,
6946 even if the window is displayed there.
6948 `reusable-frames' -- Value specifies frame(s) to search for a
6949 window that already displays the buffer.
6950 See `display-buffer-reuse-window'.
6952 `pop-up-frame-parameters' -- Value specifies an alist of frame
6953 parameters to give a new frame, if
6954 one is created.
6956 `window-height' -- Value specifies either an integer (the number
6957 of lines of a new window), a floating point number (the
6958 fraction of a new window with respect to the height of the
6959 frame's root window) or a function to be called with one
6960 argument - a new window. The function is supposed to adjust
6961 the height of the window; its return value is ignored.
6962 Suitable functions are `shrink-window-if-larger-than-buffer'
6963 and `fit-window-to-buffer'.
6965 `window-width' -- Value specifies either an integer (the number
6966 of columns of a new window), a floating point number (the
6967 fraction of a new window with respect to the width of the
6968 frame's root window) or a function to be called with one
6969 argument - a new window. The function is supposed to adjust
6970 the width of the window; its return value is ignored.
6972 `allow-no-window' -- A non-nil value indicates readiness for the case
6973 of not displaying the buffer and FUNCTION can safely return
6974 a non-window value to suppress displaying.
6976 `preserve-size' -- Value should be either (t . nil) to
6977 preserve the width of the window, (nil . t) to preserve its
6978 height or (t . t) to preserve both.
6980 `window-parameters' -- Value specifies an alist of window
6981 parameters to give the chosen window.
6983 The ACTION argument to `display-buffer' can also have a non-nil
6984 and non-list value. This means to display the buffer in a window
6985 other than the selected one, even if it is already displayed in
6986 the selected window. If called interactively with a prefix
6987 argument, ACTION is t."
6988 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6989 (if current-prefix-arg t)))
6990 (let ((buffer (if (bufferp buffer-or-name)
6991 buffer-or-name
6992 (get-buffer buffer-or-name)))
6993 ;; Make sure that when we split windows the old window keeps
6994 ;; point, bug#14829.
6995 (split-window-keep-point t)
6996 ;; Handle the old form of the first argument.
6997 (inhibit-same-window (and action (not (listp action)))))
6998 (unless (listp action) (setq action nil))
6999 (if display-buffer-function
7000 ;; If `display-buffer-function' is defined, let it do the job.
7001 (funcall display-buffer-function buffer inhibit-same-window)
7002 ;; Otherwise, use the defined actions.
7003 (let* ((user-action
7004 (display-buffer-assq-regexp
7005 (buffer-name buffer) display-buffer-alist action))
7006 (special-action (display-buffer--special-action buffer))
7007 ;; Extra actions from the arguments to this function:
7008 (extra-action
7009 (cons nil (append (if inhibit-same-window
7010 '((inhibit-same-window . t)))
7011 (if frame
7012 `((reusable-frames . ,frame))))))
7013 ;; Construct action function list and action alist.
7014 (actions (list display-buffer-overriding-action
7015 user-action special-action action extra-action
7016 display-buffer-base-action
7017 display-buffer-fallback-action))
7018 (functions (apply 'append
7019 (mapcar (lambda (x)
7020 (setq x (car x))
7021 (if (functionp x) (list x) x))
7022 actions)))
7023 (alist (apply 'append (mapcar 'cdr actions)))
7024 window)
7025 (unless (buffer-live-p buffer)
7026 (error "Invalid buffer"))
7027 (while (and functions (not window))
7028 (setq window (funcall (car functions) buffer alist)
7029 functions (cdr functions)))
7030 (and (windowp window) window)))))
7032 (defun display-buffer-other-frame (buffer)
7033 "Display buffer BUFFER preferably in another frame.
7034 This uses the function `display-buffer' as a subroutine; see
7035 its documentation for additional customization information."
7036 (interactive "BDisplay buffer in other frame: ")
7037 (display-buffer buffer display-buffer--other-frame-action t))
7039 ;;; `display-buffer' action functions:
7041 (defun display-buffer-use-some-frame (buffer alist)
7042 "Display BUFFER in an existing frame that meets a predicate
7043 \(by default any frame other than the current frame). If
7044 successful, return the window used; otherwise return nil.
7046 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
7047 raising the frame.
7049 If ALIST has a non-nil `frame-predicate' entry, its value is a
7050 function taking one argument (a frame), returning non-nil if the
7051 frame is a candidate; this function replaces the default
7052 predicate.
7054 If ALIST has a non-nil `inhibit-same-window' entry, avoid using
7055 the currently selected window (only useful with a frame-predicate
7056 that allows the selected frame)."
7057 (let* ((predicate (or (cdr (assq 'frame-predicate alist))
7058 (lambda (frame)
7059 (and
7060 (not (eq frame (selected-frame)))
7061 (not (window-dedicated-p
7063 (get-lru-window frame)
7064 (frame-first-window frame)))))
7066 (frame (car (filtered-frame-list predicate)))
7067 (window (and frame (get-lru-window frame nil (cdr (assq 'inhibit-same-window alist))))))
7068 (when window
7069 (prog1
7070 (window--display-buffer
7071 buffer window 'frame alist display-buffer-mark-dedicated)
7072 (unless (cdr (assq 'inhibit-switch-frame alist))
7073 (window--maybe-raise-frame frame))))))
7075 (defun display-buffer-same-window (buffer alist)
7076 "Display BUFFER in the selected window.
7077 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
7078 if the selected window is a minibuffer window or is dedicated to
7079 another buffer; in that case, return nil. Otherwise, return the
7080 selected window."
7081 (unless (or (cdr (assq 'inhibit-same-window alist))
7082 (window-minibuffer-p)
7083 (window-dedicated-p))
7084 (window--display-buffer buffer (selected-window) 'reuse alist)))
7086 (defun display-buffer--maybe-same-window (buffer alist)
7087 "Conditionally display BUFFER in the selected window.
7088 If `same-window-p' returns non-nil for BUFFER's name, call
7089 `display-buffer-same-window' and return its value. Otherwise,
7090 return nil."
7091 (and (same-window-p (buffer-name buffer))
7092 (display-buffer-same-window buffer alist)))
7094 (defun display-buffer-reuse-window (buffer alist)
7095 "Return a window that is already displaying BUFFER.
7096 Return nil if no usable window is found.
7098 If ALIST has a non-nil `inhibit-same-window' entry, the selected
7099 window is not eligible for reuse.
7101 If ALIST contains a `reusable-frames' entry, its value determines
7102 which frames to search for a reusable window:
7103 nil -- the selected frame (actually the last non-minibuffer frame)
7104 A frame -- just that frame
7105 `visible' -- all visible frames
7106 0 -- all frames on the current terminal
7107 t -- all frames.
7109 If ALIST contains no `reusable-frames' entry, search just the
7110 selected frame if `display-buffer-reuse-frames' and
7111 `pop-up-frames' are both nil; search all frames on the current
7112 terminal if either of those variables is non-nil.
7114 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7115 event that a window on another frame is chosen, avoid raising
7116 that frame."
7117 (let* ((alist-entry (assq 'reusable-frames alist))
7118 (frames (cond (alist-entry (cdr alist-entry))
7119 ((if (eq pop-up-frames 'graphic-only)
7120 (display-graphic-p)
7121 pop-up-frames)
7123 (display-buffer-reuse-frames 0)
7124 (t (last-nonminibuffer-frame))))
7125 (window (if (and (eq buffer (window-buffer))
7126 (not (cdr (assq 'inhibit-same-window alist))))
7127 (selected-window)
7128 (car (delq (selected-window)
7129 (get-buffer-window-list buffer 'nomini
7130 frames))))))
7131 (when (window-live-p window)
7132 (prog1 (window--display-buffer buffer window 'reuse alist)
7133 (unless (cdr (assq 'inhibit-switch-frame alist))
7134 (window--maybe-raise-frame (window-frame window)))))))
7136 (defun display-buffer-reuse-mode-window (buffer alist)
7137 "Return a window based on the mode of the buffer it displays.
7138 Display BUFFER in the returned window. Return nil if no usable
7139 window is found.
7141 If ALIST contains a `mode' entry, its value is a major mode (a
7142 symbol) or a list of modes. A window is a candidate if it
7143 displays a buffer that derives from one of the given modes. When
7144 ALIST contains no `mode' entry, the current major mode of BUFFER
7145 is used.
7147 The behavior is also controlled by entries for
7148 `inhibit-same-window', `reusable-frames' and
7149 `inhibit-switch-frame' as is done in the function
7150 `display-buffer-reuse-window'."
7151 (let* ((alist-entry (assq 'reusable-frames alist))
7152 (alist-mode-entry (assq 'mode alist))
7153 (frames (cond (alist-entry (cdr alist-entry))
7154 ((if (eq pop-up-frames 'graphic-only)
7155 (display-graphic-p)
7156 pop-up-frames)
7158 (display-buffer-reuse-frames 0)
7159 (t (last-nonminibuffer-frame))))
7160 (inhibit-same-window-p (cdr (assq 'inhibit-same-window alist)))
7161 (windows (window-list-1 nil 'nomini frames))
7162 (buffer-mode (with-current-buffer buffer major-mode))
7163 (allowed-modes (if alist-mode-entry
7164 (cdr alist-mode-entry)
7165 buffer-mode))
7166 (curwin (selected-window))
7167 (curframe (selected-frame)))
7168 (unless (listp allowed-modes)
7169 (setq allowed-modes (list allowed-modes)))
7170 (let (same-mode-same-frame
7171 same-mode-other-frame
7172 derived-mode-same-frame
7173 derived-mode-other-frame)
7174 (dolist (window windows)
7175 (let ((mode?
7176 (with-current-buffer (window-buffer window)
7177 (cond ((memq major-mode allowed-modes)
7178 'same)
7179 ((derived-mode-p allowed-modes)
7180 'derived)))))
7181 (when (and mode?
7182 (not (and inhibit-same-window-p
7183 (eq window curwin))))
7184 (push window (if (eq curframe (window-frame window))
7185 (if (eq mode? 'same)
7186 same-mode-same-frame
7187 derived-mode-same-frame)
7188 (if (eq mode? 'same)
7189 same-mode-other-frame
7190 derived-mode-other-frame))))))
7191 (let ((window (car (nconc same-mode-same-frame
7192 same-mode-other-frame
7193 derived-mode-same-frame
7194 derived-mode-other-frame))))
7195 (when (window-live-p window)
7196 (prog1 (window--display-buffer buffer window 'reuse alist)
7197 (unless (cdr (assq 'inhibit-switch-frame alist))
7198 (window--maybe-raise-frame (window-frame window)))))))))
7200 (defun display-buffer--special-action (buffer)
7201 "Return special display action for BUFFER, if any.
7202 If `special-display-p' returns non-nil for BUFFER, return an
7203 appropriate display action involving `special-display-function'.
7204 See `display-buffer' for the format of display actions."
7205 (and special-display-function
7206 ;; `special-display-p' returns either t or a list of frame
7207 ;; parameters to pass to `special-display-function'.
7208 (let ((pars (special-display-p (buffer-name buffer))))
7209 (when pars
7210 (list (list #'display-buffer-reuse-window
7211 (lambda (buffer _alist)
7212 (funcall special-display-function
7213 buffer (if (listp pars) pars)))))))))
7215 (defun display-buffer-pop-up-frame (buffer alist)
7216 "Display BUFFER in a new frame.
7217 This works by calling `pop-up-frame-function'. If successful,
7218 return the window used; otherwise return nil.
7220 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
7221 raising the new frame.
7223 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
7224 corresponding value is an alist of frame parameters to give the
7225 new frame."
7226 (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
7227 (pop-up-frame-alist (append params pop-up-frame-alist))
7228 (fun pop-up-frame-function)
7229 frame window)
7230 (when (and fun
7231 ;; Make BUFFER current so `make-frame' will use it as the
7232 ;; new frame's buffer (Bug#15133).
7233 (with-current-buffer buffer
7234 (setq frame (funcall fun)))
7235 (setq window (frame-selected-window frame)))
7236 (prog1 (window--display-buffer
7237 buffer window 'frame alist display-buffer-mark-dedicated)
7238 (unless (cdr (assq 'inhibit-switch-frame alist))
7239 (window--maybe-raise-frame frame))))))
7241 (defun display-buffer-pop-up-window (buffer alist)
7242 "Display BUFFER by popping up a new window.
7243 The new window is created on the selected frame, or in
7244 `last-nonminibuffer-frame' if no windows can be created there.
7245 If successful, return the new window; otherwise return nil.
7247 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7248 event that the new window is created on another frame, avoid
7249 raising the frame."
7250 (let ((frame (or (window--frame-usable-p (selected-frame))
7251 (window--frame-usable-p (last-nonminibuffer-frame))))
7252 window)
7253 (when (and (or (not (frame-parameter frame 'unsplittable))
7254 ;; If the selected frame cannot be split, look at
7255 ;; `last-nonminibuffer-frame'.
7256 (and (eq frame (selected-frame))
7257 (setq frame (last-nonminibuffer-frame))
7258 (window--frame-usable-p frame)
7259 (not (frame-parameter frame 'unsplittable))))
7260 ;; Attempt to split largest or least recently used window.
7261 (setq window (or (window--try-to-split-window
7262 (get-largest-window frame t) alist)
7263 (window--try-to-split-window
7264 (get-lru-window frame t) alist))))
7266 (prog1 (window--display-buffer
7267 buffer window 'window alist display-buffer-mark-dedicated)
7268 (unless (cdr (assq 'inhibit-switch-frame alist))
7269 (window--maybe-raise-frame (window-frame window)))))))
7271 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
7272 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
7273 If `pop-up-frames' is non-nil (and not `graphic-only' on a
7274 text-only terminal), try with `display-buffer-pop-up-frame'.
7276 If that cannot be done, and `pop-up-windows' is non-nil, try
7277 again with `display-buffer-pop-up-window'."
7278 (or (and (if (eq pop-up-frames 'graphic-only)
7279 (display-graphic-p)
7280 pop-up-frames)
7281 (display-buffer-pop-up-frame buffer alist))
7282 (and pop-up-windows
7283 (display-buffer-pop-up-window buffer alist))))
7285 (defun display-buffer-in-child-frame (buffer alist)
7286 "Display BUFFER in a child frame.
7287 By default, this either reuses a child frame of the selected
7288 frame or makes a new child frame of the selected frame. If
7289 successful, return the window used; otherwise return nil.
7291 If ALIST has a non-nil 'child-frame-parameters' entry, the
7292 corresponding value is an alist of frame parameters to give the
7293 new frame. A 'parent-frame' parameter specifying the selected
7294 frame is provided by default. If the child frame should be or
7295 become the child of any other frame, a corresponding entry must
7296 be added to ALIST."
7297 (let* ((parameters
7298 (append
7299 (cdr (assq 'child-frame-parameters alist))
7300 `((parent-frame . ,(selected-frame)))))
7301 (parent (or (assq 'parent-frame parameters)
7302 (selected-frame)))
7303 (share (assq 'share-child-frame parameters))
7304 share1 frame window)
7305 (with-current-buffer buffer
7306 (when (frame-live-p parent)
7307 (catch 'frame
7308 (dolist (frame1 (frame-list))
7309 (when (eq (frame-parent frame1) parent)
7310 (setq share1 (assq 'share-child-frame
7311 (frame-parameters frame1)))
7312 (when (eq share share1)
7313 (setq frame frame1)
7314 (throw 'frame t))))))
7316 (if frame
7317 (setq window (frame-selected-window frame))
7318 (setq frame (make-frame parameters))
7319 (setq window (frame-selected-window frame))))
7321 (prog1 (window--display-buffer
7322 buffer window 'frame alist display-buffer-mark-dedicated)
7323 (unless (cdr (assq 'inhibit-switch-frame alist))
7324 (window--maybe-raise-frame frame)))))
7326 (defun display-buffer-below-selected (buffer alist)
7327 "Try displaying BUFFER in a window below the selected window.
7328 If there is a window below the selected one and that window
7329 already displays BUFFER, use that window. Otherwise, try to
7330 create a new window below the selected one and show BUFFER there.
7331 If that attempt fails as well and there is a non-dedicated window
7332 below the selected one, use that window."
7333 (let (window)
7334 (or (and (setq window (window-in-direction 'below))
7335 (eq buffer (window-buffer window))
7336 (window--display-buffer buffer window 'reuse alist))
7337 (and (not (frame-parameter nil 'unsplittable))
7338 (let ((split-height-threshold 0)
7339 split-width-threshold)
7340 (setq window (window--try-to-split-window
7341 (selected-window) alist)))
7342 (window--display-buffer
7343 buffer window 'window alist display-buffer-mark-dedicated))
7344 (and (setq window (window-in-direction 'below))
7345 (not (window-dedicated-p window))
7346 (window--display-buffer
7347 buffer window 'reuse alist display-buffer-mark-dedicated)))))
7349 (defun display-buffer-at-bottom (buffer alist)
7350 "Try displaying BUFFER in a window at the bottom of the selected frame.
7351 This either reuses such a window provided it shows BUFFER
7352 already, splits a window at the bottom of the frame or the
7353 frame's root window, or reuses some window at the bottom of the
7354 selected frame."
7355 (let (bottom-window bottom-window-shows-buffer window)
7356 (walk-window-tree
7357 (lambda (window)
7358 (cond
7359 ((window-in-direction 'below window))
7360 ((and (not bottom-window-shows-buffer)
7361 (eq buffer (window-buffer window)))
7362 (setq bottom-window-shows-buffer t)
7363 (setq bottom-window window))
7364 ((not bottom-window)
7365 (setq bottom-window window)))
7366 nil nil 'nomini))
7367 (or (and bottom-window-shows-buffer
7368 (window--display-buffer
7369 buffer bottom-window 'reuse alist display-buffer-mark-dedicated))
7370 (and (not (frame-parameter nil 'unsplittable))
7371 (let (split-width-threshold)
7372 (setq window (window--try-to-split-window bottom-window alist)))
7373 (window--display-buffer
7374 buffer window 'window alist display-buffer-mark-dedicated))
7375 (and (not (frame-parameter nil 'unsplittable))
7376 (setq window (split-window-no-error (window-main-window)))
7377 (window--display-buffer
7378 buffer window 'window alist display-buffer-mark-dedicated))
7379 (and (setq window bottom-window)
7380 (not (window-dedicated-p window))
7381 (window--display-buffer
7382 buffer window 'reuse alist display-buffer-mark-dedicated)))))
7384 (defun display-buffer-in-previous-window (buffer alist)
7385 "Display BUFFER in a window previously showing it.
7386 If ALIST has a non-nil `inhibit-same-window' entry, the selected
7387 window is not eligible for reuse.
7389 If ALIST contains a `reusable-frames' entry, its value determines
7390 which frames to search for a reusable window:
7391 nil -- the selected frame (actually the last non-minibuffer frame)
7392 A frame -- just that frame
7393 `visible' -- all visible frames
7394 0 -- all frames on the current terminal
7395 t -- all frames.
7397 If ALIST contains no `reusable-frames' entry, search just the
7398 selected frame if `display-buffer-reuse-frames' and
7399 `pop-up-frames' are both nil; search all frames on the current
7400 terminal if either of those variables is non-nil.
7402 If ALIST has a `previous-window' entry, the window specified by
7403 that entry will override any other window found by the methods
7404 above, even if that window never showed BUFFER before."
7405 (let* ((alist-entry (assq 'reusable-frames alist))
7406 (inhibit-same-window
7407 (cdr (assq 'inhibit-same-window alist)))
7408 (frames (cond
7409 (alist-entry (cdr alist-entry))
7410 ((if (eq pop-up-frames 'graphic-only)
7411 (display-graphic-p)
7412 pop-up-frames)
7414 (display-buffer-reuse-frames 0)
7415 (t (last-nonminibuffer-frame))))
7416 best-window second-best-window window)
7417 ;; Scan windows whether they have shown the buffer recently.
7418 (catch 'best
7419 (dolist (window (window-list-1 (frame-first-window) 'nomini frames))
7420 (when (and (assq buffer (window-prev-buffers window))
7421 (not (window-dedicated-p window)))
7422 (if (eq window (selected-window))
7423 (unless inhibit-same-window
7424 (setq second-best-window window))
7425 (setq best-window window)
7426 (throw 'best t)))))
7427 ;; When ALIST has a `previous-window' entry, that entry may override
7428 ;; anything we found so far.
7429 (when (and (setq window (cdr (assq 'previous-window alist)))
7430 (window-live-p window)
7431 (not (window-dedicated-p window)))
7432 (if (eq window (selected-window))
7433 (unless inhibit-same-window
7434 (setq second-best-window window))
7435 (setq best-window window)))
7436 ;; Return best or second best window found.
7437 (when (setq window (or best-window second-best-window))
7438 (window--display-buffer buffer window 'reuse alist))))
7440 (defun display-buffer-use-some-window (buffer alist)
7441 "Display BUFFER in an existing window.
7442 Search for a usable window, set that window to the buffer, and
7443 return the window. If no suitable window is found, return nil.
7445 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7446 event that a window in another frame is chosen, avoid raising
7447 that frame."
7448 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
7449 (frame (or (window--frame-usable-p (selected-frame))
7450 (window--frame-usable-p (last-nonminibuffer-frame))))
7451 (window
7452 ;; Reuse an existing window.
7453 (or (get-lru-window frame nil not-this-window)
7454 (let ((window (get-buffer-window buffer 'visible)))
7455 (unless (and not-this-window
7456 (eq window (selected-window)))
7457 window))
7458 (get-largest-window 'visible nil not-this-window)
7459 (let ((window (get-buffer-window buffer 0)))
7460 (unless (and not-this-window
7461 (eq window (selected-window)))
7462 window))
7463 (get-largest-window 0 nil not-this-window)))
7464 (quit-restore (and (window-live-p window)
7465 (window-parameter window 'quit-restore)))
7466 (quad (nth 1 quit-restore)))
7467 (when (window-live-p window)
7468 ;; If the window was used by `display-buffer' before, try to
7469 ;; resize it to its old height but don't signal an error.
7470 (when (and (listp quad)
7471 (integerp (nth 3 quad))
7472 (> (nth 3 quad) (window-total-height window)))
7473 (condition-case nil
7474 (window-resize window (- (nth 3 quad) (window-total-height window)))
7475 (error nil)))
7477 (prog1
7478 (window--display-buffer buffer window 'reuse alist)
7479 (window--even-window-sizes window)
7480 (unless (cdr (assq 'inhibit-switch-frame alist))
7481 (window--maybe-raise-frame (window-frame window)))))))
7483 (defun display-buffer-no-window (_buffer alist)
7484 "Display BUFFER in no window.
7485 If ALIST has a non-nil `allow-no-window' entry, then don't display
7486 a window at all. This makes possible to override the default action
7487 and avoid displaying the buffer. It is assumed that when the caller
7488 specifies a non-nil `allow-no-window' then it can handle a nil value
7489 returned from `display-buffer' in this case."
7490 (when (cdr (assq 'allow-no-window alist))
7491 'fail))
7493 ;;; Display + selection commands:
7494 (defun pop-to-buffer (buffer-or-name &optional action norecord)
7495 "Display buffer specified by BUFFER-OR-NAME and select its window.
7496 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
7497 If it is a string not naming an existent buffer, create a buffer
7498 with that name. If BUFFER-OR-NAME is nil, choose some other
7499 buffer. In either case, make that buffer current and return it.
7501 This uses `display-buffer' as a subroutine. The optional ACTION
7502 argument is passed to `display-buffer' as its ACTION argument.
7503 See `display-buffer' for more information. ACTION is t if called
7504 interactively with a prefix argument, which means to pop to a
7505 window other than the selected one even if the buffer is already
7506 displayed in the selected window.
7508 If a suitable window is found, select that window. If it is not
7509 on the selected frame, raise that window's frame and give it
7510 input focus.
7512 Optional third arg NORECORD non-nil means do not put this buffer
7513 at the front of the list of recently selected ones."
7514 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
7515 (if current-prefix-arg t)))
7516 (let* ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))
7517 (old-frame (selected-frame))
7518 (window (display-buffer buffer action)))
7519 ;; Don't assume that `display-buffer' has supplied us with a window
7520 ;; (Bug#24332).
7521 (if window
7522 (let ((frame (window-frame window)))
7523 ;; If we chose another frame, make sure it gets input focus.
7524 (unless (eq frame old-frame)
7525 (select-frame-set-input-focus frame norecord))
7526 ;; Make sure the window is selected (Bug#8615), (Bug#6954)
7527 (select-window window norecord))
7528 ;; If `display-buffer' failed to supply a window, just make the
7529 ;; buffer current.
7530 (set-buffer buffer))
7531 ;; Return BUFFER even when we got no window.
7532 buffer))
7534 (defun pop-to-buffer-same-window (buffer &optional norecord)
7535 "Select buffer BUFFER in some window, preferably the same one.
7536 BUFFER may be a buffer, a string (a buffer name), or nil. If it
7537 is a string not naming an existent buffer, create a buffer with
7538 that name. If BUFFER is nil, choose some other buffer. Return
7539 the buffer.
7541 Optional argument NORECORD, if non-nil means do not put this
7542 buffer at the front of the list of recently selected ones.
7544 Unlike `pop-to-buffer', this function prefers using the selected
7545 window over popping up a new window or frame."
7546 (pop-to-buffer buffer display-buffer--same-window-action norecord))
7548 (defun read-buffer-to-switch (prompt)
7549 "Read the name of a buffer to switch to, prompting with PROMPT.
7550 Return the name of the buffer as a string.
7552 This function is intended for the `switch-to-buffer' family of
7553 commands since these need to omit the name of the current buffer
7554 from the list of completions and default values."
7555 (let ((rbts-completion-table (internal-complete-buffer-except)))
7556 (minibuffer-with-setup-hook
7557 (lambda ()
7558 (setq minibuffer-completion-table rbts-completion-table)
7559 ;; Since rbts-completion-table is built dynamically, we
7560 ;; can't just add it to the default value of
7561 ;; icomplete-with-completion-tables, so we add it
7562 ;; here manually.
7563 (if (and (boundp 'icomplete-with-completion-tables)
7564 (listp icomplete-with-completion-tables))
7565 (set (make-local-variable 'icomplete-with-completion-tables)
7566 (cons rbts-completion-table
7567 icomplete-with-completion-tables))))
7568 (read-buffer prompt (other-buffer (current-buffer))
7569 (confirm-nonexistent-file-or-buffer)))))
7571 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
7572 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
7573 If BUFFER-OR-NAME is nil, return the buffer returned by
7574 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
7575 exists, return that buffer. If no such buffer exists, create a
7576 buffer with the name BUFFER-OR-NAME and return that buffer."
7577 (if buffer-or-name
7578 (or (get-buffer buffer-or-name)
7579 (let ((buffer (get-buffer-create buffer-or-name)))
7580 (set-buffer-major-mode buffer)
7581 buffer))
7582 (other-buffer)))
7584 (defcustom switch-to-buffer-preserve-window-point t
7585 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
7586 If this is nil, `switch-to-buffer' displays the buffer at that
7587 buffer's `point'. If this is `already-displayed', it tries to
7588 display the buffer at its previous position in the selected
7589 window, provided the buffer is currently displayed in some other
7590 window on any visible or iconified frame. If this is t, it
7591 unconditionally tries to display the buffer at its previous
7592 position in the selected window.
7594 This variable is ignored if the buffer is already displayed in
7595 the selected window or never appeared in it before, or if
7596 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
7597 :type '(choice
7598 (const :tag "Never" nil)
7599 (const :tag "If already displayed elsewhere" already-displayed)
7600 (const :tag "Always" t))
7601 :group 'windows
7602 :version "26.1")
7604 (defcustom switch-to-buffer-in-dedicated-window nil
7605 "Allow switching to buffer in strongly dedicated windows.
7606 If non-nil, allow `switch-to-buffer' to proceed when called
7607 interactively and the selected window is strongly dedicated to
7608 its buffer.
7610 The following values are recognized:
7612 nil - disallow switching; signal an error
7614 prompt - prompt user whether to allow switching
7616 pop - perform `pop-to-buffer' instead
7618 t - undedicate selected window and switch
7620 When called non-interactively, `switch-to-buffer' always signals
7621 an error when the selected window is dedicated to its buffer and
7622 FORCE-SAME-WINDOW is non-nil."
7623 :type '(choice
7624 (const :tag "Disallow" nil)
7625 (const :tag "Prompt" prompt)
7626 (const :tag "Pop" pop)
7627 (const :tag "Allow" t))
7628 :group 'windows
7629 :version "25.1")
7631 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
7632 "Display buffer BUFFER-OR-NAME in the selected window.
7634 WARNING: This is NOT the way to work on another buffer temporarily
7635 within a Lisp program! Use `set-buffer' instead. That avoids
7636 messing with the window-buffer correspondences.
7638 If the selected window cannot display the specified buffer
7639 because it is a minibuffer window or strongly dedicated to
7640 another buffer, call `pop-to-buffer' to select the buffer in
7641 another window. In interactive use, if the selected window is
7642 strongly dedicated to its buffer, the value of the option
7643 `switch-to-buffer-in-dedicated-window' specifies how to proceed.
7645 If called interactively, read the buffer name using the
7646 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7647 determines whether to request confirmation before creating a new
7648 buffer.
7650 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
7651 If BUFFER-OR-NAME is a string that does not identify an existing
7652 buffer, create a buffer with that name. If BUFFER-OR-NAME is
7653 nil, switch to the buffer returned by `other-buffer'.
7655 If optional argument NORECORD is non-nil, do not put the buffer
7656 at the front of the buffer list, and do not make the window
7657 displaying it the most recently selected one.
7659 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
7660 must be displayed in the selected window when called
7661 non-interactively; if that is impossible, signal an error rather
7662 than calling `pop-to-buffer'.
7664 The option `switch-to-buffer-preserve-window-point' can be used
7665 to make the buffer appear at its last position in the selected
7666 window.
7668 Return the buffer switched to."
7669 (interactive
7670 (let ((force-same-window
7671 (cond
7672 ((window-minibuffer-p) nil)
7673 ((not (eq (window-dedicated-p) t)) 'force-same-window)
7674 ((pcase switch-to-buffer-in-dedicated-window
7675 (`nil (user-error
7676 "Cannot switch buffers in a dedicated window"))
7677 (`prompt
7678 (if (y-or-n-p
7679 (format "Window is dedicated to %s; undedicate it"
7680 (window-buffer)))
7681 (progn
7682 (set-window-dedicated-p nil nil)
7683 'force-same-window)
7684 (user-error
7685 "Cannot switch buffers in a dedicated window")))
7686 (`pop nil)
7687 (_ (set-window-dedicated-p nil nil) 'force-same-window))))))
7688 (list (read-buffer-to-switch "Switch to buffer: ") nil force-same-window)))
7689 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
7690 (cond
7691 ;; Don't call set-window-buffer if it's not needed since it
7692 ;; might signal an error (e.g. if the window is dedicated).
7693 ((eq buffer (window-buffer)))
7694 ((window-minibuffer-p)
7695 (if force-same-window
7696 (user-error "Cannot switch buffers in minibuffer window")
7697 (pop-to-buffer buffer norecord)))
7698 ((eq (window-dedicated-p) t)
7699 (if force-same-window
7700 (user-error "Cannot switch buffers in a dedicated window")
7701 (pop-to-buffer buffer norecord)))
7703 (let* ((entry (assq buffer (window-prev-buffers)))
7704 (displayed (and (eq switch-to-buffer-preserve-window-point
7705 'already-displayed)
7706 (get-buffer-window buffer 0))))
7707 (set-window-buffer nil buffer)
7708 (when (and entry
7709 (or (eq switch-to-buffer-preserve-window-point t)
7710 displayed))
7711 ;; Try to restore start and point of buffer in the selected
7712 ;; window (Bug#4041).
7713 (set-window-start (selected-window) (nth 1 entry) t)
7714 (set-window-point nil (nth 2 entry))))))
7716 (unless norecord
7717 (select-window (selected-window)))
7718 (set-buffer buffer)))
7720 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
7721 "Select the buffer specified by BUFFER-OR-NAME in another window.
7722 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
7723 nil. Return the buffer switched to.
7725 If called interactively, prompt for the buffer name using the
7726 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7727 determines whether to request confirmation before creating a new
7728 buffer.
7730 If BUFFER-OR-NAME is a string and does not identify an existing
7731 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
7732 nil, switch to the buffer returned by `other-buffer'.
7734 Optional second argument NORECORD non-nil means do not put this
7735 buffer at the front of the list of recently selected ones.
7737 This uses the function `display-buffer' as a subroutine; see its
7738 documentation for additional customization information."
7739 (interactive
7740 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
7741 (let ((pop-up-windows t))
7742 (pop-to-buffer buffer-or-name t norecord)))
7744 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
7745 "Switch to buffer BUFFER-OR-NAME in another frame.
7746 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
7747 nil. Return the buffer switched to.
7749 If called interactively, prompt for the buffer name using the
7750 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7751 determines whether to request confirmation before creating a new
7752 buffer.
7754 If BUFFER-OR-NAME is a string and does not identify an existing
7755 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
7756 nil, switch to the buffer returned by `other-buffer'.
7758 Optional second arg NORECORD non-nil means do not put this
7759 buffer at the front of the list of recently selected ones.
7761 This uses the function `display-buffer' as a subroutine; see its
7762 documentation for additional customization information."
7763 (interactive
7764 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
7765 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
7767 (defun set-window-text-height (window height)
7768 "Set the height in lines of the text display area of WINDOW to HEIGHT.
7769 WINDOW must be a live window and defaults to the selected one.
7770 HEIGHT doesn't include the mode line or header line, if any, or
7771 any partial-height lines in the text display area.
7773 Note that the current implementation of this function cannot
7774 always set the height exactly, but attempts to be conservative,
7775 by allocating more lines than are actually needed in the case
7776 where some error may be present."
7777 (setq window (window-normalize-window window t))
7778 (let ((delta (- height (window-text-height window))))
7779 (unless (zerop delta)
7780 ;; Setting window-min-height to a value like 1 can lead to very
7781 ;; bizarre displays because it also allows Emacs to make *other*
7782 ;; windows one line tall, which means that there's no more space
7783 ;; for the mode line.
7784 (let ((window-min-height (min 2 height)))
7785 (window-resize window delta)))))
7787 (defun enlarge-window-horizontally (delta)
7788 "Make selected window DELTA columns wider.
7789 Interactively, if no argument is given, make selected window one
7790 column wider."
7791 (interactive "p")
7792 (enlarge-window delta t))
7794 (defun shrink-window-horizontally (delta)
7795 "Make selected window DELTA columns narrower.
7796 Interactively, if no argument is given, make selected window one
7797 column narrower."
7798 (interactive "p")
7799 (shrink-window delta t))
7801 (defun count-screen-lines (&optional beg end count-final-newline window)
7802 "Return the number of screen lines in the region.
7803 The number of screen lines may be different from the number of actual lines,
7804 due to line breaking, display table, etc.
7806 Optional arguments BEG and END default to `point-min' and `point-max'
7807 respectively.
7809 If region ends with a newline, ignore it unless optional third argument
7810 COUNT-FINAL-NEWLINE is non-nil.
7812 The optional fourth argument WINDOW specifies the window used for obtaining
7813 parameters such as width, horizontal scrolling, and so on. The default is
7814 to use the selected window's parameters.
7816 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
7817 regardless of which buffer is displayed in WINDOW. This makes possible to use
7818 `count-screen-lines' in any buffer, whether or not it is currently displayed
7819 in some window."
7820 (unless beg
7821 (setq beg (point-min)))
7822 (unless end
7823 (setq end (point-max)))
7824 (if (= beg end)
7826 (save-excursion
7827 (save-restriction
7828 (widen)
7829 (narrow-to-region (min beg end)
7830 (if (and (not count-final-newline)
7831 (= ?\n (char-before (max beg end))))
7832 (1- (max beg end))
7833 (max beg end)))
7834 (goto-char (point-min))
7835 (1+ (vertical-motion (buffer-size) window))))))
7837 (defun window-buffer-height (window)
7838 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
7839 WINDOW must be a live window and defaults to the selected one."
7840 (setq window (window-normalize-window window t))
7841 (with-current-buffer (window-buffer window)
7842 (max 1
7843 (count-screen-lines (point-min) (point-max)
7844 ;; If buffer ends with a newline, ignore it when
7845 ;; counting height unless point is after it.
7846 (eobp)
7847 window))))
7849 ;;; Resizing windows and frames to fit their contents exactly.
7850 (defcustom fit-window-to-buffer-horizontally nil
7851 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
7852 If this is nil, `fit-window-to-buffer' never resizes windows
7853 horizontally. If this is `only', it can resize windows
7854 horizontally only. Any other value means `fit-window-to-buffer'
7855 can resize windows in both dimensions."
7856 :type 'boolean
7857 :version "24.4"
7858 :group 'help)
7860 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
7861 ;; counting title bar and outer borders.
7862 (defcustom fit-frame-to-buffer nil
7863 "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
7864 A frame is fit if and only if its root window is a live window
7865 and this option is non-nil. If this is `horizontally', frames
7866 are resized horizontally only. If this is `vertically', frames
7867 are resized vertically only. Any other non-nil value means
7868 frames can be resized in both dimensions."
7869 :type 'boolean
7870 :version "24.4"
7871 :group 'help)
7873 (defcustom fit-frame-to-buffer-margins '(nil nil nil nil)
7874 "Margins around frame for `fit-frame-to-buffer'.
7875 This specifies the numbers of pixels to be left free on the left,
7876 above, on the right, and below a frame fitted to its buffer. Set
7877 this to avoid obscuring other desktop objects like the taskbar.
7878 The default is nil for each side, which means to not add margins.
7880 The value specified here can be overridden for a specific frame
7881 by that frame's `fit-frame-to-buffer-margins' parameter, if
7882 present. See also `fit-frame-to-buffer-sizes'."
7883 :version "24.4"
7884 :type '(list
7885 (choice
7886 :tag "Left"
7887 :value nil
7888 :format "%[LeftMargin%] %v "
7889 (const :tag "None" :format "%t" nil)
7890 (integer :tag "Pixels" :size 5))
7891 (choice
7892 :tag "Top"
7893 :value nil
7894 :format "%[TopMargin%] %v "
7895 (const :tag "None" :format "%t" nil)
7896 (integer :tag "Pixels" :size 5))
7897 (choice
7898 :tag "Right"
7899 :value nil
7900 :format "%[RightMargin%] %v "
7901 (const :tag "None" :format "%t" nil)
7902 (integer :tag "Pixels" :size 5))
7903 (choice
7904 :tag "Bottom"
7905 :value nil
7906 :format "%[BottomMargin%] %v "
7907 (const :tag "None" :format "%t" nil)
7908 (integer :tag "Pixels" :size 5)))
7909 :group 'help)
7911 (defcustom fit-frame-to-buffer-sizes '(nil nil nil nil)
7912 "Size boundaries of frame for `fit-frame-to-buffer'.
7913 This list specifies the total maximum and minimum lines and
7914 maximum and minimum columns of the root window of any frame that
7915 shall be fit to its buffer. If any of these values is non-nil,
7916 it overrides the corresponding argument of `fit-frame-to-buffer'.
7918 On window systems where the menubar can wrap, fitting a frame to
7919 its buffer may swallow the last line(s). Specifying an
7920 appropriate minimum width value here can avoid such wrapping.
7922 See also `fit-frame-to-buffer-margins'."
7923 :version "24.4"
7924 :type '(list
7925 (choice
7926 :tag "Maximum Height"
7927 :value nil
7928 :format "%[MaxHeight%] %v "
7929 (const :tag "None" :format "%t" nil)
7930 (integer :tag "Lines" :size 5))
7931 (choice
7932 :tag "Minimum Height"
7933 :value nil
7934 :format "%[MinHeight%] %v "
7935 (const :tag "None" :format "%t" nil)
7936 (integer :tag "Lines" :size 5))
7937 (choice
7938 :tag "Maximum Width"
7939 :value nil
7940 :format "%[MaxWidth%] %v "
7941 (const :tag "None" :format "%t" nil)
7942 (integer :tag "Columns" :size 5))
7943 (choice
7944 :tag "Minimum Width"
7945 :value nil
7946 :format "%[MinWidth%] %v\n"
7947 (const :tag "None" :format "%t" nil)
7948 (integer :tag "Columns" :size 5)))
7949 :group 'help)
7951 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
7953 (defun window--sanitize-margin (margin left right)
7954 "Return MARGIN if it's a number between LEFT and RIGHT.
7955 Return 0 otherwise."
7956 (if (and (numberp margin)
7957 (<= left (- right margin)) (<= margin right))
7958 margin
7961 (declare-function tool-bar-height "xdisp.c" (&optional frame pixelwise))
7963 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only)
7964 "Adjust size of FRAME to display the contents of its buffer exactly.
7965 FRAME can be any live frame and defaults to the selected one.
7966 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
7967 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
7968 FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
7969 `window-min-height' and `window-min-width' respectively.
7971 If the optional argument ONLY is `vertically', resize the frame
7972 vertically only. If ONLY is `horizontally', resize the frame
7973 horizontally only.
7975 The new position and size of FRAME can be additionally determined
7976 by customizing the options `fit-frame-to-buffer-sizes' and
7977 `fit-frame-to-buffer-margins' or setting the corresponding
7978 parameters of FRAME."
7979 (interactive)
7980 (unless (fboundp 'display-monitor-attributes-list)
7981 (user-error "Cannot resize frame in non-graphic Emacs"))
7982 (setq frame (window-normalize-frame frame))
7983 (when (window-live-p (frame-root-window frame))
7984 (let* ((char-width (frame-char-width frame))
7985 (char-height (frame-char-height frame))
7986 ;; WINDOW is FRAME's root window.
7987 (window (frame-root-window frame))
7988 (parent (frame-parent frame))
7989 (monitor-attributes
7990 (unless parent
7991 (car (display-monitor-attributes-list
7992 (frame-parameter frame 'display)))))
7993 ;; FRAME'S parent or display sizes. Used in connection
7994 ;; with margins.
7995 (geometry
7996 (unless parent
7997 (cdr (assq 'geometry monitor-attributes))))
7998 (parent-or-display-width
7999 (if parent
8000 (frame-native-width parent)
8001 (- (nth 2 geometry) (nth 0 geometry))))
8002 (parent-or-display-height
8003 (if parent
8004 (frame-native-height parent)
8005 (- (nth 3 geometry) (nth 1 geometry))))
8006 ;; FRAME'S parent or workarea sizes. Used when no margins
8007 ;; are specified.
8008 (parent-or-workarea
8009 (if parent
8010 `(0 0 ,parent-or-display-width ,parent-or-display-height)
8011 (cdr (assq 'workarea monitor-attributes))))
8012 ;; The outer size of FRAME. Needed to calculate the
8013 ;; margins around the root window's body that have to
8014 ;; remain untouched by fitting.
8015 (outer-edges (frame-edges frame 'outer-edges))
8016 (outer-width (if outer-edges
8017 (- (nth 2 outer-edges) (nth 0 outer-edges))
8018 ;; A poor guess.
8019 (frame-pixel-width frame)))
8020 (outer-height (if outer-edges
8021 (- (nth 3 outer-edges) (nth 1 outer-edges))
8022 ;; Another poor guess.
8023 (frame-pixel-height frame)))
8024 ;; The text size of of FRAME. Needed to specify FRAME's
8025 ;; text size after the root window's body's new sizes have
8026 ;; been calculated.
8027 (text-width (frame-text-width frame))
8028 (text-height (frame-text-height frame))
8029 ;; WINDOW's body size.
8030 (body-width (window-body-width window t))
8031 (body-height (window-body-height window t))
8032 ;; The difference between FRAME's outer size and WINDOW's
8033 ;; body size.
8034 (outer-minus-body-width (- outer-width body-width))
8035 (outer-minus-body-height (- outer-height body-height))
8036 ;; The difference between FRAME's text size and WINDOW's
8037 ;; body size (these values "should" be positive).
8038 (text-minus-body-width (- text-width body-width))
8039 (text-minus-body-height (- text-height body-height))
8040 ;; The current position of FRAME.
8041 (position (frame-position frame))
8042 (left (car position))
8043 (top (cdr position))
8044 ;; The margins specified for FRAME. These represent pixel
8045 ;; offsets from the left, top, right and bottom edge of the
8046 ;; display or FRAME's parent's native rectangle and have to
8047 ;; take care of the display's taskbar and other obstacles.
8048 ;; If they are unspecified, constrain the resulting frame
8049 ;; to its workarea or the parent frame's native rectangle.
8050 (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins)
8051 fit-frame-to-buffer-margins))
8052 ;; Convert margins into pixel offsets from the left-top
8053 ;; corner of FRAME's display or parent.
8054 (left-margin (if (nth 0 margins)
8055 (window--sanitize-margin
8056 (nth 0 margins) 0 parent-or-display-width)
8057 (nth 0 parent-or-workarea)))
8058 (top-margin (if (nth 1 margins)
8059 (window--sanitize-margin
8060 (nth 1 margins) 0 parent-or-display-height)
8061 (nth 1 parent-or-workarea)))
8062 (right-margin (if (nth 2 margins)
8063 (- parent-or-display-width
8064 (window--sanitize-margin
8065 (nth 2 margins) left-margin
8066 parent-or-display-width))
8067 (nth 2 parent-or-workarea)))
8068 (bottom-margin (if (nth 3 margins)
8069 (- parent-or-display-height
8070 (window--sanitize-margin
8071 (nth 3 margins) top-margin
8072 parent-or-display-height))
8073 (nth 3 parent-or-workarea)))
8074 ;; Minimum and maximum sizes specified for FRAME.
8075 (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes)
8076 fit-frame-to-buffer-sizes))
8077 ;; Calculate the minimum and maximum pixel sizes of FRAME
8078 ;; from the values provided by the MAX-HEIGHT, MIN-HEIGHT,
8079 ;; MAX-WIDTH and MIN-WIDTH arguments or, if these are nil,
8080 ;; from those provided by `fit-frame-to-buffer-sizes'.
8081 (max-height
8082 (min
8083 (cond
8084 ((numberp max-height) (* max-height char-height))
8085 ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height))
8086 (t parent-or-display-height))
8087 ;; The following is the maximum height that fits into the
8088 ;; top and bottom margins.
8089 (max (- bottom-margin top-margin outer-minus-body-height))))
8090 (min-height
8091 (cond
8092 ((numberp min-height) (* min-height char-height))
8093 ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height))
8094 (t (window-min-size window nil nil t))))
8095 (max-width
8096 (min
8097 (cond
8098 ((numberp max-width) (* max-width char-width))
8099 ((numberp (nth 2 sizes)) (* (nth 2 sizes) char-width))
8100 (t parent-or-display-width))
8101 ;; The following is the maximum width that fits into the
8102 ;; left and right margins.
8103 (max (- right-margin left-margin outer-minus-body-width))))
8104 (min-width
8105 (cond
8106 ((numberp min-width) (* min-width char-width))
8107 ((numberp (nth 3 sizes)) (nth 3 sizes))
8108 (t (window-min-size window t nil t))))
8109 ;; Note: Currently, for a new frame the sizes of the header
8110 ;; and mode line may be estimated incorrectly
8111 (size
8112 (window-text-pixel-size window t t max-width max-height))
8113 (width (max (car size) min-width))
8114 (height (max (cdr size) min-height)))
8115 ;; Don't change height or width when the window's size is fixed
8116 ;; in either direction or ONLY forbids it.
8117 (cond
8118 ((or (eq window-size-fixed 'width) (eq only 'vertically))
8119 (setq width nil))
8120 ((or (eq window-size-fixed 'height) (eq only 'horizontally))
8121 (setq height nil)))
8122 ;; Fit width to constraints.
8123 (when width
8124 (unless frame-resize-pixelwise
8125 ;; Round to character sizes.
8126 (setq width (* (/ (+ width char-width -1) char-width)
8127 char-width)))
8128 ;; The new outer width (in pixels).
8129 (setq outer-width (+ width outer-minus-body-width))
8130 ;; Maybe move FRAME to preserve margins.
8131 (let ((right (+ left outer-width)))
8132 (cond
8133 ((> right right-margin)
8134 ;; Move frame to left.
8135 (setq left (max left-margin (- left (- right right-margin)))))
8136 ((< left left-margin)
8137 ;; Move frame to right.
8138 (setq left left-margin)))))
8139 ;; Fit height to constraints.
8140 (when height
8141 (unless frame-resize-pixelwise
8142 (setq height (* (/ (+ height char-height -1) char-height)
8143 char-height)))
8144 ;; The new outer height.
8145 (setq outer-height (+ height outer-minus-body-height))
8146 ;; Preserve margins.
8147 (let ((bottom (+ top outer-height)))
8148 (cond
8149 ((> bottom bottom-margin)
8150 ;; Move frame up.
8151 (setq top (max top-margin (- top (- bottom bottom-margin)))))
8152 ((< top top-margin)
8153 ;; Move frame down.
8154 (setq top top-margin)))))
8155 ;; Apply our changes.
8156 (setq text-width
8157 (if width
8158 (+ width text-minus-body-width)
8159 (frame-text-width frame)))
8160 (setq text-height
8161 (if height
8162 (+ height text-minus-body-height)
8163 (frame-text-height frame)))
8164 (modify-frame-parameters
8165 frame `((left . ,left) (top . ,top)
8166 (width . (text-pixels . ,text-width))
8167 (height . (text-pixels . ,text-height)))))))
8169 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width preserve-size)
8170 "Adjust size of WINDOW to display its buffer's contents exactly.
8171 WINDOW must be a live window and defaults to the selected one.
8173 If WINDOW is part of a vertical combination, adjust WINDOW's
8174 height. The new height is calculated from the actual height of
8175 the accessible portion of its buffer. The optional argument
8176 MAX-HEIGHT specifies a maximum height and defaults to the height
8177 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
8178 minimum height and defaults to `window-min-height'. Both
8179 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
8180 and header line and a bottom divider, if any.
8182 If WINDOW is part of a horizontal combination and the value of
8183 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
8184 WINDOW's width. The new width of WINDOW is calculated from the
8185 maximum length of its buffer's lines that follow the current
8186 start position of WINDOW. The optional argument MAX-WIDTH
8187 specifies a maximum width and defaults to the width of WINDOW's
8188 frame. The optional argument MIN-WIDTH specifies a minimum width
8189 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
8190 are specified in columns and include fringes, margins, a
8191 scrollbar and a vertical divider, if any.
8193 If the optional argument `preserve-size' is non-nil, preserve the
8194 size of WINDOW (see `window-preserve-size').
8196 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
8197 If WINDOW is its frame's root window and the option
8198 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
8199 adjust the frame's size.
8201 Note that even if this function makes WINDOW large enough to show
8202 _all_ parts of its buffer you might not see the first part when
8203 WINDOW was scrolled. If WINDOW is resized horizontally, you will
8204 not see the top of its buffer unless WINDOW starts at its minimum
8205 accessible position."
8206 (interactive)
8207 (setq window (window-normalize-window window t))
8208 (if (eq window (frame-root-window window))
8209 (when fit-frame-to-buffer
8210 ;; Fit WINDOW's frame to buffer.
8211 (fit-frame-to-buffer
8212 (window-frame window)
8213 max-height min-height max-width min-width
8214 (and (memq fit-frame-to-buffer '(vertically horizontally))
8215 fit-frame-to-buffer)))
8216 (with-selected-window window
8217 (let* ((pixelwise window-resize-pixelwise)
8218 (char-height (frame-char-height))
8219 (char-width (frame-char-width))
8220 (total-height (window-size window nil pixelwise))
8221 (body-height (window-body-height window pixelwise))
8222 (body-width (window-body-width window pixelwise))
8223 (min-height
8224 ;; Sanitize MIN-HEIGHT.
8225 (if (numberp min-height)
8226 ;; Can't get smaller than `window-safe-min-height'.
8227 (max (if pixelwise
8228 (* char-height min-height)
8229 min-height)
8230 (if pixelwise
8231 (window-safe-min-pixel-height window)
8232 window-safe-min-height))
8233 ;; Preserve header and mode line if present.
8234 (max (if pixelwise
8235 (* char-height window-min-height)
8236 window-min-height)
8237 (window-min-size window nil window pixelwise))))
8238 (max-height
8239 ;; Sanitize MAX-HEIGHT.
8240 (if (numberp max-height)
8241 (min
8242 (+ total-height
8243 (window-max-delta
8244 window nil window nil t nil pixelwise))
8245 (if pixelwise
8246 (* char-height max-height)
8247 max-height))
8248 (+ total-height (window-max-delta
8249 window nil window nil t nil pixelwise))))
8250 height)
8251 (cond
8252 ;; If WINDOW is vertically combined, try to resize it
8253 ;; vertically.
8254 ((and (not (eq fit-window-to-buffer-horizontally 'only))
8255 (not (window-size-fixed-p window 'preserved))
8256 (window-combined-p))
8257 ;; Vertically we always want to fit the entire buffer.
8258 ;; WINDOW'S height can't get larger than its frame's pixel
8259 ;; height. Its width remains fixed.
8260 (setq height (+ (cdr (window-text-pixel-size
8261 nil nil t nil (frame-pixel-height) t))
8262 (window-scroll-bar-height window)
8263 (window-bottom-divider-width)))
8264 ;; Round height.
8265 (unless pixelwise
8266 (setq height (/ (+ height char-height -1) char-height)))
8267 (unless (= height total-height)
8268 (window-preserve-size window)
8269 (window-resize-no-error
8270 window
8271 (- (max min-height (min max-height height)) total-height)
8272 nil window pixelwise)
8273 (when preserve-size
8274 (window-preserve-size window nil t))))
8275 ;; If WINDOW is horizontally combined, try to resize it
8276 ;; horizontally.
8277 ((and fit-window-to-buffer-horizontally
8278 (not (window-size-fixed-p window t 'preserved))
8279 (window-combined-p nil t))
8280 (let* ((total-width (window-size window t pixelwise))
8281 (min-width
8282 ;; Sanitize MIN-WIDTH.
8283 (if (numberp min-width)
8284 ;; Can't get smaller than `window-safe-min-width'.
8285 (max (if pixelwise
8286 (* char-width min-width)
8287 min-width)
8288 (if pixelwise
8289 (window-safe-min-pixel-width)
8290 window-safe-min-width))
8291 ;; Preserve fringes, margins, scrollbars if present.
8292 (max (if pixelwise
8293 (* char-width window-min-width)
8294 window-min-width)
8295 (window-min-size nil nil window pixelwise))))
8296 (max-width
8297 ;; Sanitize MAX-WIDTH.
8298 (if (numberp max-width)
8299 (min (+ total-width
8300 (window-max-delta
8301 window t window nil t nil pixelwise))
8302 (if pixelwise
8303 (* char-width max-width)
8304 max-width))
8305 (+ total-width (window-max-delta
8306 window t window nil t nil pixelwise))))
8307 ;; When fitting horizontally, assume that WINDOW's
8308 ;; start position remains unaltered. WINDOW can't get
8309 ;; wider than its frame's pixel width, its height
8310 ;; remains unaltered.
8311 (width (+ (car (window-text-pixel-size
8312 nil (window-start) (point-max)
8313 (frame-pixel-width)
8314 ;; Add one char-height to assure that
8315 ;; we're on the safe side. This
8316 ;; overshoots when the first line below
8317 ;; the bottom is wider than the window.
8318 (* body-height
8319 (if pixelwise 1 char-height))))
8320 (window-right-divider-width))))
8321 (unless pixelwise
8322 (setq width (/ (+ width char-width -1) char-width)))
8323 (unless (= width body-width)
8324 (window-preserve-size window t)
8325 (window-resize-no-error
8326 window
8327 (- (max min-width
8328 (min max-width
8329 (+ total-width (- width body-width))))
8330 total-width)
8331 t window pixelwise)
8332 (when preserve-size
8333 (window-preserve-size window t t))))))))))
8335 (defun window-safely-shrinkable-p (&optional window)
8336 "Return t if WINDOW can be shrunk without shrinking other windows.
8337 WINDOW defaults to the selected window."
8338 (with-selected-window (or window (selected-window))
8339 (let ((edges (window-edges)))
8340 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
8341 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
8343 (defun shrink-window-if-larger-than-buffer (&optional window)
8344 "Shrink height of WINDOW if its buffer doesn't need so many lines.
8345 More precisely, shrink WINDOW vertically to be as small as
8346 possible, while still showing the full contents of its buffer.
8347 WINDOW must be a live window and defaults to the selected one.
8349 Do not shrink WINDOW to less than `window-min-height' lines. Do
8350 nothing if the buffer contains more lines than the present window
8351 height, or if some of the window's contents are scrolled out of
8352 view, or if shrinking this window would also shrink another
8353 window, or if the window is the only window of its frame.
8355 Return non-nil if the window was shrunk, nil otherwise."
8356 (interactive)
8357 (setq window (window-normalize-window window t))
8358 ;; Make sure that WINDOW is vertically combined and `point-min' is
8359 ;; visible (for whatever reason that's needed). The remaining issues
8360 ;; should be taken care of by `fit-window-to-buffer'.
8361 (when (and (window-combined-p window)
8362 (pos-visible-in-window-p (point-min) window))
8363 (fit-window-to-buffer window (window-total-height window))))
8365 (defun window-largest-empty-rectangle--maximums-1 (quad maximums)
8366 "Support function for `window-largest-empty-rectangle'."
8367 (cond
8368 ((null maximums)
8369 (list quad))
8370 ((> (car quad) (caar maximums))
8371 (cons quad maximums))
8373 (cons (car maximums)
8374 (window-largest-empty-rectangle--maximums-1 quad (cdr maximums))))))
8376 (defun window-largest-empty-rectangle--maximums (quad maximums count)
8377 "Support function for `window-largest-empty-rectangle'."
8378 (setq maximums (window-largest-empty-rectangle--maximums-1 quad maximums))
8379 (if (> (length maximums) count)
8380 (nbutlast maximums)
8381 maximums))
8383 (defun window-largest-empty-rectangle--disjoint-maximums (maximums count)
8384 "Support function for `window-largest-empty-rectangle'."
8385 (setq maximums (sort maximums (lambda (x y) (> (car x) (car y)))))
8386 (let ((new-length 0)
8387 new-maximums)
8388 (while (and maximums (< new-length count))
8389 (let* ((maximum (car maximums))
8390 (at (nth 2 maximum))
8391 (to (nth 3 maximum)))
8392 (catch 'drop
8393 (dolist (new-maximum new-maximums)
8394 (let ((new-at (nth 2 new-maximum))
8395 (new-to (nth 3 new-maximum)))
8396 (when (if (< at new-at) (> to new-at) (< at new-to))
8397 ;; Intersection -> drop.
8398 (throw 'drop nil))))
8399 (setq new-maximums (cons maximum new-maximums))
8400 (setq new-length (1+ new-length)))
8401 (setq maximums (cdr maximums))))
8403 (nreverse new-maximums)))
8405 (defun window-largest-empty-rectangle (&optional window count min-width min-height positions left)
8406 "Return dimensions of largest empty rectangle in WINDOW.
8407 WINDOW must be a live window and defaults to the selected one.
8409 The return value is a triple of the width and the start and end
8410 Y-coordinates of the largest rectangle that can be inscribed into
8411 the empty space (the space not displaying any text) of WINDOW's
8412 text area. The return value is nil if the current glyph matrix
8413 of WINDOW is not up-to-date.
8415 Optional argument COUNT, if non-nil, specifies the maximum number
8416 of rectangles to return. This means that the return value is a
8417 list of triples specifying rectangles with the largest rectangle
8418 first. COUNT can be also a cons cell whose car specifies the
8419 number of rectangles to return and whose cdr, if non-nil, states
8420 that all rectangles returned must be disjoint.
8422 Note that the right edge of any rectangle returned by this
8423 function is the right edge of WINDOW (the left edge if its buffer
8424 displays RTL text).
8426 Optional arguments MIN-WIDTH and MIN-HEIGHT, if non-nil, specify
8427 the minimum width and height of any rectangle returned.
8429 Optional argument POSITIONS, if non-nil, is a cons cell whose car
8430 specifies the uppermost and whose cdr specifies the lowermost
8431 pixel position that must be covered by any rectangle returned.
8432 Note that positions are counted from the start of the text area
8433 of WINDOW.
8435 Optional argument LEFT, if non-nil, means to return values suitable for
8436 buffers displaying right to left text."
8437 ;; Process lines as returned by ‘window-lines-pixel-dimensions’.
8438 ;; STACK is a stack that contains rows that have to be processed yet.
8439 (let* ((window (window-normalize-window window t))
8440 (disjoint (and (consp count) (cdr count)))
8441 (count (or (and (numberp count) count)
8442 (and (consp count) (numberp (car count)) (car count))))
8443 (rows (window-lines-pixel-dimensions window nil nil t t left))
8444 (rows-at 0)
8445 (max-size 0)
8446 row stack stack-at stack-to
8447 top top-width top-at top-to top-size
8448 max-width max-at max-to maximums)
8449 ;; ROWS-AT is the position where the first element of ROWS starts.
8450 ;; STACK-AT is the position where the first element of STACK starts.
8451 (while rows
8452 (setq row (car rows))
8453 (if (or (not stack) (>= (car row) (caar stack)))
8454 (progn
8455 (unless stack
8456 (setq stack-at rows-at))
8457 (setq stack (cons row stack))
8458 ;; Set ROWS-AT to where the first element of ROWS ends
8459 ;; which, after popping ROW, makes it the start position of
8460 ;; the next ROW.
8461 (setq rows-at (cdr row))
8462 (setq rows (cdr rows)))
8463 (setq top (car stack))
8464 (setq stack (cdr stack))
8465 (setq top-width (car top))
8466 (setq top-at (if stack (cdar stack) stack-at))
8467 (setq top-to (cdr top))
8468 (setq top-size (* top-width (- top-to top-at)))
8469 (unless (or (and min-width (< top-width min-width))
8470 (and min-height (< (- top-to top-at) min-height))
8471 (and positions
8472 (or (> top-at (car positions))
8473 (< top-to (cdr positions)))))
8474 (if count
8475 (if disjoint
8476 (setq maximums (cons (list top-size top-width top-at top-to)
8477 maximums))
8478 (setq maximums (window-largest-empty-rectangle--maximums
8479 (list top-size top-width top-at top-to)
8480 maximums count)))
8481 (when (> top-size max-size)
8482 (setq max-size top-size)
8483 (setq max-width top-width)
8484 (setq max-at top-at)
8485 (setq max-to top-to))))
8486 (if (and stack (> (caar stack) (car row)))
8487 ;; Have new top element of stack include old top.
8488 (setq stack (cons (cons (caar stack) (cdr top)) (cdr stack)))
8489 ;; Move rows-at backwards to top-at.
8490 (setq rows-at top-at))))
8492 (when stack
8493 ;; STACK-TO is the position where the stack ends.
8494 (setq stack-to (cdar stack))
8495 (while stack
8496 (setq top (car stack))
8497 (setq stack (cdr stack))
8498 (setq top-width (car top))
8499 (setq top-at (if stack (cdar stack) stack-at))
8500 (setq top-size (* top-width (- stack-to top-at)))
8501 (unless (or (and min-width (< top-width min-width))
8502 (and min-height (< (- stack-to top-at) min-height))
8503 (and positions
8504 (or (> top-at (car positions))
8505 (< stack-to (cdr positions)))))
8506 (if count
8507 (if disjoint
8508 (setq maximums (cons (list top-size top-width top-at stack-to)
8509 maximums))
8510 (setq maximums (window-largest-empty-rectangle--maximums
8511 (list top-size top-width top-at stack-to)
8512 maximums count)))
8513 (when (> top-size max-size)
8514 (setq max-size top-size)
8515 (setq max-width top-width)
8516 (setq max-at top-at)
8517 (setq max-to stack-to))))))
8519 (cond
8520 (maximums
8521 (if disjoint
8522 (window-largest-empty-rectangle--disjoint-maximums maximums count)
8523 maximums))
8524 ((> max-size 0)
8525 (list max-width max-at max-to)))))
8527 (defun kill-buffer-and-window ()
8528 "Kill the current buffer and delete the selected window."
8529 (interactive)
8530 (let ((window-to-delete (selected-window))
8531 (buffer-to-kill (current-buffer))
8532 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
8533 (unwind-protect
8534 (progn
8535 (add-hook 'kill-buffer-hook delete-window-hook t t)
8536 (if (kill-buffer (current-buffer))
8537 ;; If `delete-window' failed before, we rerun it to regenerate
8538 ;; the error so it can be seen in the echo area.
8539 (when (eq (selected-window) window-to-delete)
8540 (delete-window))))
8541 ;; If the buffer is not dead for some reason (probably because
8542 ;; of a `quit' signal), remove the hook again.
8543 (ignore-errors
8544 (with-current-buffer buffer-to-kill
8545 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
8549 ;; Groups of windows (Follow Mode).
8551 ;; This section of functions extends the functionality of some window
8552 ;; manipulating commands to groups of windows cooperatively
8553 ;; displaying a buffer, typically with Follow Mode.
8555 ;; The xxx-function variables are permanent locals so that their local
8556 ;; status is undone only when explicitly programmed, not when a buffer
8557 ;; is reverted or a mode function is called.
8559 (defvar window-group-start-function nil)
8560 (make-variable-buffer-local 'window-group-start-function)
8561 (put 'window-group-start-function 'permanent-local t)
8562 (defun window-group-start (&optional window)
8563 "Return position at which display currently starts in the group of
8564 windows containing WINDOW. When a grouping mode (such as Follow Mode)
8565 is not active, this function is identical to `window-start'.
8567 WINDOW must be a live window and defaults to the selected one.
8568 This is updated by redisplay or by calling `set-window*-start'."
8569 (if (functionp window-group-start-function)
8570 (funcall window-group-start-function window)
8571 (window-start window)))
8573 (defvar window-group-end-function nil)
8574 (make-variable-buffer-local 'window-group-end-function)
8575 (put 'window-group-end-function 'permanent-local t)
8576 (defun window-group-end (&optional window update)
8577 "Return position at which display currently ends in the group of
8578 windows containing WINDOW. When a grouping mode (such as Follow Mode)
8579 is not active, this function is identical to `window-end'.
8581 WINDOW must be a live window and defaults to the selected one.
8582 This is updated by redisplay, when it runs to completion.
8583 Simply changing the buffer text or setting `window-group-start'
8584 does not update this value.
8585 Return nil if there is no recorded value. (This can happen if the
8586 last redisplay of WINDOW was preempted, and did not finish.)
8587 If UPDATE is non-nil, compute the up-to-date position
8588 if it isn't already recorded."
8589 (if (functionp window-group-end-function)
8590 (funcall window-group-end-function window update)
8591 (window-end window update)))
8593 (defvar set-window-group-start-function nil)
8594 (make-variable-buffer-local 'set-window-group-start-function)
8595 (put 'set-window-group-start-function 'permanent-local t)
8596 (defun set-window-group-start (window pos &optional noforce)
8597 "Make display in the group of windows containing WINDOW start at
8598 position POS in WINDOW's buffer. When a grouping mode (such as Follow
8599 Mode) is not active, this function is identical to `set-window-start'.
8601 WINDOW must be a live window and defaults to the selected one. Return
8602 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
8603 overriding motion of point in order to display at this exact start."
8604 (if (functionp set-window-group-start-function)
8605 (funcall set-window-group-start-function window pos noforce)
8606 (set-window-start window pos noforce)))
8608 (defvar recenter-window-group-function nil)
8609 (make-variable-buffer-local 'recenter-window-group-function)
8610 (put 'recenter-window-group-function 'permanent-local t)
8611 (defun recenter-window-group (&optional arg)
8612 "Center point in the group of windows containing the selected window
8613 and maybe redisplay frame. When a grouping mode (such as Follow Mode)
8614 is not active, this function is identical to `recenter'.
8616 With a numeric prefix argument ARG, recenter putting point on screen line ARG
8617 relative to the first window in the selected window group. If ARG is
8618 negative, it counts up from the bottom of the last window in the
8619 group. (ARG should be less than the total height of the window group.)
8621 If ARG is omitted or nil, then recenter with point on the middle line of
8622 the selected window group; if the variable `recenter-redisplay' is
8623 non-nil, also erase the entire frame and redraw it (when
8624 `auto-resize-tool-bars' is set to `grow-only', this resets the
8625 tool-bar's height to the minimum height needed); if
8626 `recenter-redisplay' has the special value `tty', then only tty frames
8627 are redrawn.
8629 Just C-u as prefix means put point in the center of the window
8630 and redisplay normally--don't erase and redraw the frame."
8631 (if (functionp recenter-window-group-function)
8632 (funcall recenter-window-group-function arg)
8633 (recenter arg)))
8635 (defvar pos-visible-in-window-group-p-function nil)
8636 (make-variable-buffer-local 'pos-visible-in-window-group-p-function)
8637 (put 'pos-visible-in-window-group-p-function 'permanent-local t)
8638 (defun pos-visible-in-window-group-p (&optional pos window partially)
8639 "Return non-nil if position POS is currently on the frame in the
8640 window group containing WINDOW. When a grouping mode (such as Follow
8641 Mode) is not active, this function is identical to
8642 `pos-visible-in-window-p'.
8644 WINDOW must be a live window and defaults to the selected one.
8646 Return nil if that position is scrolled vertically out of view. If a
8647 character is only partially visible, nil is returned, unless the
8648 optional argument PARTIALLY is non-nil. If POS is only out of view
8649 because of horizontal scrolling, return non-nil. If POS is t, it
8650 specifies the position of the last visible glyph in the window group.
8651 POS defaults to point in WINDOW; WINDOW defaults to the selected
8652 window.
8654 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
8655 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
8656 where X and Y are the pixel coordinates relative to the top left corner
8657 of the window. The remaining elements are omitted if the character after
8658 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
8659 off-window at the top and bottom of the screen line (\"row\") containing
8660 POS, ROWH is the visible height of that row, and VPOS is the row number
8661 \(zero-based)."
8662 (if (functionp pos-visible-in-window-group-p-function)
8663 (funcall pos-visible-in-window-group-p-function pos window partially)
8664 (pos-visible-in-window-p pos window partially)))
8666 (defvar selected-window-group-function nil)
8667 (make-variable-buffer-local 'selected-window-group-function)
8668 (put 'selected-window-group-function 'permanent-local t)
8669 (defun selected-window-group ()
8670 "Return the list of windows in the group containing the selected window.
8671 When a grouping mode (such as Follow Mode) is not active, the
8672 result is a list containing only the selected window."
8673 (if (functionp selected-window-group-function)
8674 (funcall selected-window-group-function)
8675 (list (selected-window))))
8677 (defvar move-to-window-group-line-function nil)
8678 (make-variable-buffer-local 'move-to-window-group-line-function)
8679 (put 'move-to-window-group-line-function 'permanent-local t)
8680 (defun move-to-window-group-line (arg)
8681 "Position point relative to the the current group of windows.
8682 When a grouping mode (such as Follow Mode) is not active, this
8683 function is identical to `move-to-window-line'.
8685 ARG nil means position point at center of the window group.
8686 Else, ARG specifies the vertical position within the window
8687 group; zero means top of first window in the group, negative
8688 means relative to the bottom of the last window in the group."
8689 (if (functionp move-to-window-group-line-function)
8690 (funcall move-to-window-group-line-function arg)
8691 (move-to-window-line arg)))
8694 (defvar recenter-last-op nil
8695 "Indicates the last recenter operation performed.
8696 Possible values: `top', `middle', `bottom', integer or float numbers.
8697 It can also be nil, which means the first value in `recenter-positions'.")
8699 (defcustom recenter-positions '(middle top bottom)
8700 "Cycling order for `recenter-top-bottom'.
8701 A list of elements with possible values `top', `middle', `bottom',
8702 integer or float numbers that define the cycling order for
8703 the command `recenter-top-bottom'.
8705 Top and bottom destinations are `scroll-margin' lines from the true
8706 window top and bottom. Middle redraws the frame and centers point
8707 vertically within the window. Integer number moves current line to
8708 the specified absolute window-line. Float number between 0.0 and 1.0
8709 means the percentage of the screen space from the top. The default
8710 cycling order is middle -> top -> bottom."
8711 :type '(repeat (choice
8712 (const :tag "Top" top)
8713 (const :tag "Middle" middle)
8714 (const :tag "Bottom" bottom)
8715 (integer :tag "Line number")
8716 (float :tag "Percentage")))
8717 :version "23.2"
8718 :group 'windows)
8720 (defun recenter-top-bottom (&optional arg)
8721 "Move current buffer line to the specified window line.
8722 With no prefix argument, successive calls place point according
8723 to the cycling order defined by `recenter-positions'.
8725 A prefix argument is handled like `recenter':
8726 With numeric prefix ARG, move current line to window-line ARG.
8727 With plain `C-u', move current line to window center."
8728 (interactive "P")
8729 (cond
8730 (arg (recenter arg)) ; Always respect ARG.
8732 (setq recenter-last-op
8733 (if (eq this-command last-command)
8734 (car (or (cdr (member recenter-last-op recenter-positions))
8735 recenter-positions))
8736 (car recenter-positions)))
8737 (let ((this-scroll-margin
8738 (min (max 0 scroll-margin)
8739 (truncate (/ (window-body-height) 4.0)))))
8740 (cond ((eq recenter-last-op 'middle)
8741 (recenter))
8742 ((eq recenter-last-op 'top)
8743 (recenter this-scroll-margin))
8744 ((eq recenter-last-op 'bottom)
8745 (recenter (- -1 this-scroll-margin)))
8746 ((integerp recenter-last-op)
8747 (recenter recenter-last-op))
8748 ((floatp recenter-last-op)
8749 (recenter (round (* recenter-last-op (window-height))))))))))
8751 (define-key global-map [?\C-l] 'recenter-top-bottom)
8753 (defun move-to-window-line-top-bottom (&optional arg)
8754 "Position point relative to window.
8756 With a prefix argument ARG, acts like `move-to-window-line'.
8758 With no argument, positions point at center of window.
8759 Successive calls position point at positions defined
8760 by `recenter-positions'."
8761 (interactive "P")
8762 (cond
8763 (arg (move-to-window-line arg)) ; Always respect ARG.
8765 (setq recenter-last-op
8766 (if (eq this-command last-command)
8767 (car (or (cdr (member recenter-last-op recenter-positions))
8768 recenter-positions))
8769 (car recenter-positions)))
8770 (let ((this-scroll-margin
8771 (min (max 0 scroll-margin)
8772 (truncate (/ (window-body-height) 4.0)))))
8773 (cond ((eq recenter-last-op 'middle)
8774 (call-interactively 'move-to-window-line))
8775 ((eq recenter-last-op 'top)
8776 (move-to-window-line this-scroll-margin))
8777 ((eq recenter-last-op 'bottom)
8778 (move-to-window-line (- -1 this-scroll-margin)))
8779 ((integerp recenter-last-op)
8780 (move-to-window-line recenter-last-op))
8781 ((floatp recenter-last-op)
8782 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
8784 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
8786 ;;; Scrolling commands.
8788 ;;; Scrolling commands which do not signal errors at top/bottom
8789 ;;; of buffer at first key-press (instead move to top/bottom
8790 ;;; of buffer).
8792 (defcustom scroll-error-top-bottom nil
8793 "Move point to top/bottom of buffer before signaling a scrolling error.
8794 A value of nil means just signal an error if no more scrolling possible.
8795 A value of t means point moves to the beginning or the end of the buffer
8796 \(depending on scrolling direction) when no more scrolling possible.
8797 When point is already on that position, then signal an error."
8798 :type 'boolean
8799 :group 'windows
8800 :version "24.1")
8802 (defun scroll-up-command (&optional arg)
8803 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
8804 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
8805 scroll window further, move cursor to the bottom line.
8806 When point is already on that position, then signal an error.
8807 A near full screen is `next-screen-context-lines' less than a full screen.
8808 Negative ARG means scroll downward.
8809 If ARG is the atom `-', scroll downward by nearly full screen."
8810 (interactive "^P")
8811 (cond
8812 ((null scroll-error-top-bottom)
8813 (scroll-up arg))
8814 ((eq arg '-)
8815 (scroll-down-command nil))
8816 ((< (prefix-numeric-value arg) 0)
8817 (scroll-down-command (- (prefix-numeric-value arg))))
8818 ((eobp)
8819 (scroll-up arg)) ; signal error
8821 (condition-case nil
8822 (scroll-up arg)
8823 (end-of-buffer
8824 (if arg
8825 ;; When scrolling by ARG lines can't be done,
8826 ;; move by ARG lines instead.
8827 (forward-line arg)
8828 ;; When ARG is nil for full-screen scrolling,
8829 ;; move to the bottom of the buffer.
8830 (goto-char (point-max))))))))
8832 (put 'scroll-up-command 'scroll-command t)
8834 (defun scroll-down-command (&optional arg)
8835 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
8836 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
8837 scroll window further, move cursor to the top line.
8838 When point is already on that position, then signal an error.
8839 A near full screen is `next-screen-context-lines' less than a full screen.
8840 Negative ARG means scroll upward.
8841 If ARG is the atom `-', scroll upward by nearly full screen."
8842 (interactive "^P")
8843 (cond
8844 ((null scroll-error-top-bottom)
8845 (scroll-down arg))
8846 ((eq arg '-)
8847 (scroll-up-command nil))
8848 ((< (prefix-numeric-value arg) 0)
8849 (scroll-up-command (- (prefix-numeric-value arg))))
8850 ((bobp)
8851 (scroll-down arg)) ; signal error
8853 (condition-case nil
8854 (scroll-down arg)
8855 (beginning-of-buffer
8856 (if arg
8857 ;; When scrolling by ARG lines can't be done,
8858 ;; move by ARG lines instead.
8859 (forward-line (- arg))
8860 ;; When ARG is nil for full-screen scrolling,
8861 ;; move to the top of the buffer.
8862 (goto-char (point-min))))))))
8864 (put 'scroll-down-command 'scroll-command t)
8866 ;;; Scrolling commands which scroll a line instead of full screen.
8868 (defun scroll-up-line (&optional arg)
8869 "Scroll text of selected window upward ARG lines; or one line if no ARG.
8870 If ARG is omitted or nil, scroll upward by one line.
8871 This is different from `scroll-up-command' that scrolls a full screen."
8872 (interactive "p")
8873 (scroll-up (or arg 1)))
8875 (put 'scroll-up-line 'scroll-command t)
8877 (defun scroll-down-line (&optional arg)
8878 "Scroll text of selected window down ARG lines; or one line if no ARG.
8879 If ARG is omitted or nil, scroll down by one line.
8880 This is different from `scroll-down-command' that scrolls a full screen."
8881 (interactive "p")
8882 (scroll-down (or arg 1)))
8884 (put 'scroll-down-line 'scroll-command t)
8887 (defun scroll-other-window-down (&optional lines)
8888 "Scroll the \"other window\" down.
8889 For more details, see the documentation for `scroll-other-window'."
8890 (interactive "P")
8891 (scroll-other-window
8892 ;; Just invert the argument's meaning.
8893 ;; We can do that without knowing which window it will be.
8894 (if (eq lines '-) nil
8895 (if (null lines) '-
8896 (- (prefix-numeric-value lines))))))
8898 (defun beginning-of-buffer-other-window (arg)
8899 "Move point to the beginning of the buffer in the other window.
8900 Leave mark at previous position.
8901 With arg N, put point N/10 of the way from the true beginning."
8902 (interactive "P")
8903 (let ((orig-window (selected-window))
8904 (window (other-window-for-scrolling)))
8905 ;; We use unwind-protect rather than save-window-excursion
8906 ;; because the latter would preserve the things we want to change.
8907 (unwind-protect
8908 (progn
8909 (select-window window)
8910 ;; Set point and mark in that window's buffer.
8911 (with-no-warnings
8912 (beginning-of-buffer arg))
8913 ;; Set point accordingly.
8914 (recenter '(t)))
8915 (select-window orig-window))))
8917 (defun end-of-buffer-other-window (arg)
8918 "Move point to the end of the buffer in the other window.
8919 Leave mark at previous position.
8920 With arg N, put point N/10 of the way from the true end."
8921 (interactive "P")
8922 ;; See beginning-of-buffer-other-window for comments.
8923 (let ((orig-window (selected-window))
8924 (window (other-window-for-scrolling)))
8925 (unwind-protect
8926 (progn
8927 (select-window window)
8928 (with-no-warnings
8929 (end-of-buffer arg))
8930 (recenter '(t)))
8931 (select-window orig-window))))
8933 (defvar mouse-autoselect-window-timer nil
8934 "Timer used by delayed window autoselection.")
8936 (defvar mouse-autoselect-window-position-1 nil
8937 "First mouse position recorded by delayed window autoselection.")
8939 (defvar mouse-autoselect-window-position nil
8940 "Last mouse position recorded by delayed window autoselection.")
8942 (defvar mouse-autoselect-window-window nil
8943 "Last window recorded by delayed window autoselection.")
8945 (defvar mouse-autoselect-window-state nil
8946 "When non-nil, special state of delayed window autoselection.
8947 Possible values are `suspend' (suspend autoselection after a menu or
8948 scrollbar interaction) and `select' (the next invocation of
8949 `handle-select-window' shall select the window immediately).")
8951 (defun mouse-autoselect-window-cancel (&optional force)
8952 "Cancel delayed window autoselection.
8953 Optional argument FORCE means cancel unconditionally."
8954 (unless (and (not force)
8955 ;; Don't cancel for select-window or select-frame events
8956 ;; or when the user drags a scroll bar.
8957 (or (memq this-command
8958 '(handle-select-window handle-switch-frame))
8959 (and (eq this-command 'scroll-bar-toolkit-scroll)
8960 (memq (nth 4 (event-end last-input-event))
8961 '(handle end-scroll)))))
8962 (setq mouse-autoselect-window-state nil)
8963 (setq mouse-autoselect-window-position-1 nil)
8964 (when (timerp mouse-autoselect-window-timer)
8965 (cancel-timer mouse-autoselect-window-timer))
8966 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
8968 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
8969 "Start delayed window autoselection.
8970 MOUSE-POSITION is the last position where the mouse was seen as returned
8971 by `mouse-position'. Optional argument WINDOW non-nil denotes the
8972 window where the mouse was seen. Optional argument SUSPEND non-nil
8973 means suspend autoselection."
8974 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
8975 (setq mouse-autoselect-window-position mouse-position)
8976 (when window (setq mouse-autoselect-window-window window))
8977 (setq mouse-autoselect-window-state (when suspend 'suspend))
8978 ;; Install timer which runs `mouse-autoselect-window-select' after
8979 ;; `mouse-autoselect-window' seconds.
8980 (setq mouse-autoselect-window-timer
8981 (run-at-time
8982 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
8984 (defun mouse-autoselect-window-select ()
8985 "Select window with delayed window autoselection.
8986 If the mouse position has stabilized in a non-selected window, select
8987 that window. The minibuffer window is selected only if the minibuffer
8988 is active. This function is run by `mouse-autoselect-window-timer'."
8989 (let* ((mouse-position (mouse-position))
8990 (mouse-x (and (numberp (cadr mouse-position))
8991 (cadr mouse-position)))
8992 (mouse-y (and (numberp (cddr mouse-position))
8993 (cddr mouse-position)))
8994 (frame (and mouse-x mouse-y (car mouse-position)))
8995 (window (and frame (window-at mouse-x mouse-y frame))))
8996 (cond
8997 ((or (and (fboundp 'menu-or-popup-active-p) (menu-or-popup-active-p))
8998 (and window
8999 (let ((coords (coordinates-in-window-p
9000 (cdr mouse-position) window)))
9001 (and (not (consp coords))
9002 (not (memq coords '(left-margin right-margin)))))))
9003 ;; A menu / popup dialog is active or the mouse is not on the
9004 ;; text region of WINDOW: Suspend autoselection temporarily.
9005 (mouse-autoselect-window-start mouse-position nil t))
9006 ((or (eq mouse-autoselect-window-state 'suspend)
9007 ;; When the mouse is at its first recorded position, restart
9008 ;; delayed autoselection. This works around a scenario with
9009 ;; two two-window frames with identical dimensions: select the
9010 ;; first window of the first frame, switch to the second
9011 ;; frame, move the mouse to its second window, minimize the
9012 ;; second frame. Now the second window of the first frame
9013 ;; gets selected although the mouse never really "moved" into
9014 ;; that window.
9015 (and (numberp mouse-autoselect-window)
9016 (equal (mouse-position) mouse-autoselect-window-position-1)))
9017 ;; Delayed autoselection was temporarily suspended, reenable it.
9018 (mouse-autoselect-window-start mouse-position))
9019 ((and window
9020 (or (not (numberp mouse-autoselect-window))
9021 (and (>= mouse-autoselect-window 0)
9022 ;; If `mouse-autoselect-window' is non-negative,
9023 ;; select window if it's the same as before.
9024 (eq window mouse-autoselect-window-window))
9025 ;; Otherwise select window iff the mouse is at the same
9026 ;; position as before. Observe that the first test
9027 ;; after starting autoselection usually fails since the
9028 ;; value of `mouse-autoselect-window-position' recorded
9029 ;; there is the position where the mouse has entered the
9030 ;; new window and not necessarily where the mouse has
9031 ;; stopped moving.
9032 (equal mouse-position mouse-autoselect-window-position))
9033 ;; The minibuffer is a candidate window if it's active.
9034 (or (not (window-minibuffer-p window))
9035 (eq window (active-minibuffer-window))))
9036 ;; Mouse position has stabilized in non-selected window: Cancel
9037 ;; delayed autoselection and try to select that window.
9038 (mouse-autoselect-window-cancel t)
9039 ;; Use `unread-command-events' in order to execute pre- and
9040 ;; post-command hooks and trigger idle timers. To avoid delaying
9041 ;; autoselection again, set `mouse-autoselect-window-state'."
9042 (setq mouse-autoselect-window-state 'select)
9043 (setq unread-command-events
9044 (cons (list 'select-window (list window))
9045 unread-command-events)))
9046 ((or (not (numberp mouse-autoselect-window))
9047 (equal mouse-position mouse-autoselect-window-position))
9048 ;; Mouse position has stabilized at
9049 ;; `mouse-autoselect-window-position': Cancel delayed
9050 ;; autoselection.
9051 (mouse-autoselect-window-cancel t))
9052 (window
9053 ;; Mouse position has not stabilized yet, resume delayed
9054 ;; autoselection.
9055 (mouse-autoselect-window-start mouse-position window)))))
9057 (defun handle-select-window (event)
9058 "Handle select-window events."
9059 (interactive "^e")
9060 (let* ((window (posn-window (event-start event)))
9061 (frame (and (window-live-p window) (window-frame window)))
9062 (old-frame (selected-frame)))
9063 (unless (or (not (window-live-p window))
9064 ;; Don't switch when autoselection shall be delayed.
9065 (and (numberp mouse-autoselect-window)
9066 (not (eq mouse-autoselect-window-state 'select))
9067 (let ((position (mouse-position)))
9068 ;; Cancel any delayed autoselection.
9069 (mouse-autoselect-window-cancel t)
9070 ;; Start delayed autoselection from current mouse
9071 ;; position and window.
9072 (setq mouse-autoselect-window-position-1 position)
9073 (mouse-autoselect-window-start position window)
9074 ;; Executing a command cancels delayed autoselection.
9075 (add-hook
9076 'pre-command-hook 'mouse-autoselect-window-cancel)))
9077 ;; Don't switch to a `no-accept-focus' frame unless it's
9078 ;; already selected.
9079 (and (not (eq frame (selected-frame)))
9080 (frame-parameter frame 'no-accept-focus))
9081 ;; Don't switch to minibuffer window unless it's active.
9082 (and (window-minibuffer-p window)
9083 (not (minibuffer-window-active-p window))))
9084 ;; Reset state of delayed autoselection.
9085 (setq mouse-autoselect-window-state nil)
9086 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
9087 (run-hooks 'mouse-leave-buffer-hook)
9088 ;; Clear echo area.
9089 (message nil)
9090 ;; Select the window before giving the frame focus since otherwise
9091 ;; we might get two windows with an active cursor.
9092 (select-window window)
9093 (cond
9094 ((or (not (memq (window-system frame) '(x w32 ns)))
9095 (not focus-follows-mouse)
9096 ;; Focus FRAME if it's either a child frame or an ancestor
9097 ;; of the frame switched from.
9098 (and (not (frame-parameter frame 'parent-frame))
9099 (not (frame-ancestor-p frame old-frame)))))
9100 ((eq focus-follows-mouse 'auto-raise)
9101 ;; Focus and auto-raise frame.
9102 (x-focus-frame frame)
9103 ;; This doesn't seem to work when we move from a normal frame
9104 ;; right into the child frame of another frame - we should raise
9105 ;; that child frame's ancestor frame first ...
9106 (raise-frame frame))
9108 ;; Just focus frame.
9109 (x-focus-frame frame t))))))
9111 (defun truncated-partial-width-window-p (&optional window)
9112 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
9113 WINDOW must be a live window and defaults to the selected one.
9114 Return nil if WINDOW is not a partial-width window
9115 (regardless of the value of `truncate-lines').
9116 Otherwise, consult the value of `truncate-partial-width-windows'
9117 for the buffer shown in WINDOW."
9118 (setq window (window-normalize-window window t))
9119 (unless (window-full-width-p window)
9120 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
9121 (window-buffer window))))
9122 (if (integerp t-p-w-w)
9123 (< (window-width window) t-p-w-w)
9124 t-p-w-w))))
9127 ;; Automatically inform subprocesses of changes to window size.
9129 (defcustom window-adjust-process-window-size-function
9130 'window-adjust-process-window-size-smallest
9131 "Control how Emacs chooses inferior process window sizes.
9132 Emacs uses this function to tell processes the space they have
9133 available for displaying their output. After each window
9134 configuration change, Emacs calls the value of
9135 `window-adjust-process-window-size-function' for each process
9136 with a buffer being displayed in at least one window.
9137 This function is responsible for combining the sizes of the
9138 displayed windows and returning a cons (WIDTH . HEIGHT)
9139 describing the width and height with which Emacs will call
9140 `set-process-window-size' for that process. If the function
9141 returns nil, Emacs does not call `set-process-window-size'.
9143 This function is called with the process buffer as the current
9144 buffer and with two arguments: the process and a list of windows
9145 displaying process. Modes can make this variable buffer-local;
9146 additionally, the `adjust-window-size-function' process property
9147 overrides the global or buffer-local value of
9148 `window-adjust-process-window-size-function'."
9149 :type '(choice
9150 (const :tag "Minimum area of any window"
9151 window-adjust-process-window-size-smallest)
9152 (const :tag "Maximum area of any window"
9153 window-adjust-process-window-size-largest)
9154 (const :tag "Do not adjust process window sizes" ignore)
9155 function)
9156 :group 'windows
9157 :version "25.1")
9159 (defun window-adjust-process-window-size (reducer windows)
9160 "Adjust the window sizes of a process.
9161 WINDOWS is a list of windows associated with that process. REDUCER is
9162 a two-argument function used to combine the widths and heights of
9163 the given windows."
9164 (when windows
9165 (let ((width (window-max-chars-per-line (car windows)))
9166 (height (window-body-height (car windows))))
9167 (dolist (window (cdr windows))
9168 (setf width (funcall reducer width (window-max-chars-per-line window)))
9169 (setf height (funcall reducer height (window-body-height window))))
9170 (cons width height))))
9172 (defun window-adjust-process-window-size-smallest (_process windows)
9173 "Adjust the process window size of PROCESS.
9174 WINDOWS is a list of windows associated with PROCESS. Choose the
9175 smallest area available for displaying PROCESS's output."
9176 (window-adjust-process-window-size #'min windows))
9178 (defun window-adjust-process-window-size-largest (_process windows)
9179 "Adjust the process window size of PROCESS.
9180 WINDOWS is a list of windows associated with PROCESS. Choose the
9181 largest area available for displaying PROCESS's output."
9182 (window-adjust-process-window-size #'max windows))
9184 (defun window--process-window-list ()
9185 "Return an alist mapping processes to associated windows.
9186 A window is associated with a process if that window is
9187 displaying that processes's buffer."
9188 (let ((processes (process-list))
9189 (process-windows nil))
9190 (if processes
9191 (walk-windows
9192 (lambda (window)
9193 (let ((buffer (window-buffer window))
9194 (iter processes))
9195 (while (let ((process (car iter)))
9196 (if (and (process-live-p process)
9197 (eq buffer (process-buffer process)))
9198 (let ((procwin (assq process process-windows)))
9199 ;; Add this window to the list of windows
9200 ;; displaying process.
9201 (if procwin
9202 (push window (cdr procwin))
9203 (push (list process window) process-windows))
9204 ;; We found our process for this window, so
9205 ;; stop iterating over the process list.
9206 nil)
9207 (setf iter (cdr iter)))))))
9208 1 t))
9209 process-windows))
9211 (defun window--adjust-process-windows ()
9212 "Update process window sizes to match the current window configuration."
9213 (when (fboundp 'process-list)
9214 (dolist (procwin (window--process-window-list))
9215 (let ((process (car procwin)))
9216 (with-demoted-errors "Error adjusting window size: %S"
9217 (with-current-buffer (process-buffer process)
9218 (let ((size (funcall
9219 (or (process-get process 'adjust-window-size-function)
9220 window-adjust-process-window-size-function)
9221 process (cdr procwin))))
9222 (when size
9223 (set-process-window-size process (cdr size) (car size))))))))))
9225 (add-hook 'window-configuration-change-hook 'window--adjust-process-windows)
9228 ;; Some of these are in tutorial--default-keys, so update that if you
9229 ;; change these.
9230 (define-key ctl-x-map "0" 'delete-window)
9231 (define-key ctl-x-map "1" 'delete-other-windows)
9232 (define-key ctl-x-map "2" 'split-window-below)
9233 (define-key ctl-x-map "3" 'split-window-right)
9234 (define-key ctl-x-map "o" 'other-window)
9235 (define-key ctl-x-map "^" 'enlarge-window)
9236 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
9237 (define-key ctl-x-map "{" 'shrink-window-horizontally)
9238 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
9239 (define-key ctl-x-map "+" 'balance-windows)
9240 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
9242 ;;; window.el ends here