Avoid a custom-variable-type error (bug#27363)
[emacs.git] / lisp / window.el
blob8b07ed462c970d31185ea345b8af88190510abe0
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 <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Window tree functions.
29 ;;; Code:
31 (defun internal--before-save-selected-window ()
32 (cons (selected-window)
33 ;; We save and restore all frames' selected windows, because
34 ;; `select-window' can change the frame-selected-window of
35 ;; whatever frame that window is in. Each text terminal's
36 ;; top-frame is preserved by putting it last in the list.
37 (apply #'append
38 (mapcar (lambda (terminal)
39 (let ((frames (frames-on-display-list terminal))
40 (top-frame (tty-top-frame terminal))
41 alist)
42 (if top-frame
43 (setq frames
44 (cons top-frame
45 (delq top-frame frames))))
46 (dolist (f frames)
47 (push (cons f (frame-selected-window f))
48 alist))
49 alist))
50 (terminal-list)))))
52 (defun internal--after-save-selected-window (state)
53 (dolist (elt (cdr state))
54 (and (frame-live-p (car elt))
55 (window-live-p (cdr elt))
56 (set-frame-selected-window (car elt) (cdr elt) 'norecord)))
57 (when (window-live-p (car state))
58 (select-window (car state) 'norecord)))
60 (defmacro save-selected-window (&rest body)
61 "Execute BODY, then select the previously selected window.
62 The value returned is the value of the last form in BODY.
64 This macro saves and restores the selected window, as well as the
65 selected window in each frame. If the previously selected window
66 is no longer live, then whatever window is selected at the end of
67 BODY remains selected. If the previously selected window of some
68 frame is no longer live at the end of BODY, that frame's selected
69 window is left alone.
71 This macro saves and restores the current buffer, since otherwise
72 its normal operation could make a different buffer current. The
73 order of recently selected windows and the buffer list ordering
74 are not altered by this macro (unless they are altered in BODY)."
75 (declare (indent 0) (debug t))
76 `(let ((save-selected-window--state (internal--before-save-selected-window)))
77 (save-current-buffer
78 (unwind-protect
79 (progn ,@body)
80 (internal--after-save-selected-window save-selected-window--state)))))
82 (defvar temp-buffer-window-setup-hook nil
83 "Normal hook run by `with-temp-buffer-window' before buffer display.
84 This hook is run by `with-temp-buffer-window' with the buffer to be
85 displayed current.")
87 (defvar temp-buffer-window-show-hook nil
88 "Normal hook run by `with-temp-buffer-window' after buffer display.
89 This hook is run by `with-temp-buffer-window' with the buffer
90 displayed and current and its window selected.")
92 (defun temp-buffer-window-setup (buffer-or-name)
93 "Set up temporary buffer specified by BUFFER-OR-NAME.
94 Return the buffer."
95 (let ((old-dir default-directory)
96 (buffer (get-buffer-create buffer-or-name)))
97 (with-current-buffer buffer
98 (kill-all-local-variables)
99 (setq default-directory old-dir)
100 (delete-all-overlays)
101 (setq buffer-read-only nil)
102 (setq buffer-file-name nil)
103 (setq buffer-undo-list t)
104 (let ((inhibit-read-only t)
105 (inhibit-modification-hooks t))
106 (erase-buffer)
107 (run-hooks 'temp-buffer-window-setup-hook))
108 ;; Return the buffer.
109 buffer)))
111 (defun temp-buffer-window-show (buffer &optional action)
112 "Show temporary buffer BUFFER in a window.
113 Return the window showing BUFFER. Pass ACTION as action argument
114 to `display-buffer'."
115 (let (window frame)
116 (with-current-buffer buffer
117 (set-buffer-modified-p nil)
118 (setq buffer-read-only t)
119 (goto-char (point-min))
120 (when (let ((window-combination-limit
121 ;; When `window-combination-limit' equals
122 ;; `temp-buffer' or `temp-buffer-resize' and
123 ;; `temp-buffer-resize-mode' is enabled in this
124 ;; buffer bind it to t so resizing steals space
125 ;; preferably from the window that was split.
126 (if (or (eq window-combination-limit 'temp-buffer)
127 (and (eq window-combination-limit
128 'temp-buffer-resize)
129 temp-buffer-resize-mode))
131 window-combination-limit)))
132 (setq window (display-buffer buffer action)))
133 (setq frame (window-frame window))
134 (unless (eq frame (selected-frame))
135 (raise-frame frame))
136 (setq minibuffer-scroll-window window)
137 (set-window-hscroll window 0)
138 (with-selected-window window
139 (run-hooks 'temp-buffer-window-show-hook)
140 (when temp-buffer-resize-mode
141 (resize-temp-buffer-window window)))
142 ;; Return the window.
143 window))))
145 (defmacro with-temp-buffer-window (buffer-or-name action quit-function &rest body)
146 "Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
147 BUFFER-OR-NAME must specify either a live buffer, or the name of
148 a buffer (if it does not exist, this macro creates it).
150 Make the buffer specified by BUFFER-OR-NAME empty before running
151 BODY and bind `standard-output' to that buffer, so that output
152 generated with `prin1' and similar functions in BODY goes into
153 that buffer. Do not make that buffer current for running the
154 forms in BODY. Use `with-current-buffer-window' instead if you
155 need to run BODY with that buffer current.
157 At the end of BODY, mark the specified buffer unmodified and
158 read-only, and display it in a window (but do not select it).
159 The display happens by calling `display-buffer' passing it the
160 ACTION argument. If `temp-buffer-resize-mode' is enabled, the
161 corresponding window may be resized automatically.
163 Return the value returned by BODY, unless QUIT-FUNCTION specifies
164 a function. In that case, run that function with two arguments -
165 the window showing the specified buffer and the value returned by
166 BODY - and return the value returned by that function.
168 If the buffer is displayed on a new frame, the window manager may
169 decide to select that frame. In that case, it's usually a good
170 strategy if QUIT-FUNCTION selects the window showing the buffer
171 before reading any value from the minibuffer; for example, when
172 asking a `yes-or-no-p' question.
174 This runs the hook `temp-buffer-window-setup-hook' before BODY,
175 with the specified buffer temporarily current. It runs the hook
176 `temp-buffer-window-show-hook' after displaying the buffer, with
177 that buffer temporarily current, and the window that was used to
178 display it temporarily selected.
180 This construct is similar to `with-output-to-temp-buffer' but,
181 neither runs `temp-buffer-setup-hook' which usually puts the
182 buffer in Help mode, nor `temp-buffer-show-function' (the ACTION
183 argument replaces this)."
184 (declare (debug t))
185 (let ((buffer (make-symbol "buffer"))
186 (window (make-symbol "window"))
187 (value (make-symbol "value")))
188 (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
189 (vaction action)
190 (vquit-function quit-function))
191 `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
192 (standard-output ,buffer)
193 ,window ,value)
194 (setq ,value (progn ,@body))
195 (with-current-buffer ,buffer
196 (setq ,window (temp-buffer-window-show ,buffer ,vaction)))
198 (if (functionp ,vquit-function)
199 (funcall ,vquit-function ,window ,value)
200 ,value)))))
202 (defmacro with-current-buffer-window (buffer-or-name action quit-function &rest body)
203 "Evaluate BODY with a buffer BUFFER-OR-NAME current and show that buffer.
204 This construct is like `with-temp-buffer-window' but unlike that
205 makes the buffer specified by BUFFER-OR-NAME current for running
206 BODY."
207 (declare (debug t))
208 (let ((buffer (make-symbol "buffer"))
209 (window (make-symbol "window"))
210 (value (make-symbol "value")))
211 (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
212 (vaction action)
213 (vquit-function quit-function))
214 `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
215 (standard-output ,buffer)
216 ,window ,value)
217 (with-current-buffer ,buffer
218 (setq ,value (progn ,@body))
219 (setq ,window (temp-buffer-window-show ,buffer ,vaction)))
221 (if (functionp ,vquit-function)
222 (funcall ,vquit-function ,window ,value)
223 ,value)))))
225 (defmacro with-displayed-buffer-window (buffer-or-name action quit-function &rest body)
226 "Show a buffer BUFFER-OR-NAME and evaluate BODY in that buffer.
227 This construct is like `with-current-buffer-window' but unlike that
228 displays the buffer specified by BUFFER-OR-NAME before running BODY."
229 (declare (debug t))
230 (let ((buffer (make-symbol "buffer"))
231 (window (make-symbol "window"))
232 (value (make-symbol "value")))
233 (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
234 (vaction action)
235 (vquit-function quit-function))
236 `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
237 (standard-output ,buffer)
238 ;; 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 either a buffer or a string naming a live
324 buffer and defaults to the current buffer."
325 (cond
326 ((not buffer-or-name)
327 (current-buffer))
328 ((bufferp buffer-or-name)
329 (if (buffer-live-p buffer-or-name)
330 buffer-or-name
331 (error "Buffer %s is not a live buffer" buffer-or-name)))
332 ((get-buffer buffer-or-name))
334 (error "No such buffer %s" buffer-or-name))))
336 (defun window-normalize-frame (frame)
337 "Return frame specified by FRAME.
338 FRAME must be a live frame and defaults to the selected frame."
339 (if frame
340 (if (frame-live-p frame)
341 frame
342 (error "%s is not a live frame" frame))
343 (selected-frame)))
345 (defun window-normalize-window (window &optional live-only)
346 "Return the window specified by WINDOW.
347 If WINDOW is nil, return the selected window. Otherwise, if
348 WINDOW is a live or an internal window, return WINDOW; if
349 LIVE-ONLY is non-nil, return WINDOW for a live window only.
350 Otherwise, signal an error."
351 (cond
352 ((null window)
353 (selected-window))
354 (live-only
355 (if (window-live-p window)
356 window
357 (error "%s is not a live window" window)))
358 ((window-valid-p window)
359 window)
361 (error "%s is not a valid window" window))))
363 ;; Maybe this should go to frame.el.
364 (defun frame-char-size (&optional window-or-frame horizontal)
365 "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
366 If WINDOW-OR-FRAME is a live frame, return the value of
367 `frame-char-height' for that frame. If WINDOW-OR-FRAME is a
368 valid window, return the value of `frame-char-height' for that
369 window's frame. In any other case, return the value of
370 `frame-char-height' for the selected frame.
372 Optional argument HORIZONTAL non-nil means to return the value of
373 `frame-char-width' for WINDOW-OR-FRAME."
374 (let ((frame
375 (cond
376 ((window-valid-p window-or-frame)
377 (window-frame window-or-frame))
378 ((frame-live-p window-or-frame)
379 window-or-frame)
380 (t (selected-frame)))))
381 (if horizontal
382 (frame-char-width frame)
383 (frame-char-height frame))))
385 (defvar ignore-window-parameters nil
386 "If non-nil, standard functions ignore window parameters.
387 The functions currently affected by this are `split-window',
388 `delete-window', `delete-other-windows' and `other-window'.
390 An application may bind this to a non-nil value around calls to
391 these functions to inhibit processing of window parameters.")
393 ;; This must go to C, finally (or get removed).
394 (defconst window-safe-min-height 1
395 "The absolute minimum number of lines of any window.
396 Anything less might crash Emacs.")
398 (defun window-safe-min-pixel-height (&optional window)
399 "Return the absolute minimum pixel height of WINDOW."
400 (* window-safe-min-height
401 (frame-char-size (window-normalize-window window))))
403 (defcustom window-min-height 4
404 "The minimum total height, in lines, of any window.
405 The value has to accommodate one text line, a mode and header
406 line, a horizontal scroll bar and a bottom divider, if present.
407 A value less than `window-safe-min-height' is ignored. The value
408 of this variable is honored when windows are resized or split.
410 Applications should never rebind this variable. To resize a
411 window to a height less than the one specified here, an
412 application should instead call `window-resize' with a non-nil
413 IGNORE argument. In order to have `split-window' make a window
414 shorter, explicitly specify the SIZE argument of that function."
415 :type 'integer
416 :version "24.1"
417 :group 'windows)
419 (defun window-min-pixel-height (&optional window)
420 "Return the minimum pixel height of window WINDOW."
421 (* (max window-min-height window-safe-min-height)
422 (frame-char-size window)))
424 ;; This must go to C, finally (or get removed).
425 (defconst window-safe-min-width 2
426 "The absolute minimum number of columns of a window.
427 Anything less might crash Emacs.")
429 (defun window-safe-min-pixel-width (&optional window)
430 "Return the absolute minimum pixel width of WINDOW."
431 (* window-safe-min-width
432 (frame-char-size (window-normalize-window window) t)))
434 (defcustom window-min-width 10
435 "The minimum total width, in columns, of any window.
436 The value has to accommodate two text columns as well as margins,
437 fringes, a scroll bar and a right divider, if present. A value
438 less than `window-safe-min-width' is ignored. The value of this
439 variable is honored when windows are resized or split.
441 Applications should never rebind this variable. To resize a
442 window to a width less than the one specified here, an
443 application should instead call `window-resize' with a non-nil
444 IGNORE argument. In order to have `split-window' make a window
445 narrower, explicitly specify the SIZE argument of that function."
446 :type 'integer
447 :version "24.1"
448 :group 'windows)
450 (defun window-min-pixel-width (&optional window)
451 "Return the minimum pixel width of window WINDOW."
452 (* (max window-min-width window-safe-min-width)
453 (frame-char-size window t)))
455 (defun window-safe-min-pixel-size (&optional window horizontal)
456 "Return the absolute minimum pixel height of WINDOW.
457 Optional argument HORIZONTAL non-nil means return the absolute
458 minimum pixel width of WINDOW."
459 (if horizontal
460 (window-safe-min-pixel-width window)
461 (window-safe-min-pixel-height window)))
463 (defun window-min-pixel-size (&optional window horizontal)
464 "Return the minimum pixel height of WINDOW.
465 Optional argument HORIZONTAL non-nil means return the minimum
466 pixel width of WINDOW."
467 (if horizontal
468 (window-min-pixel-width window)
469 (window-min-pixel-height window)))
471 (defun window-combined-p (&optional window horizontal)
472 "Return non-nil if WINDOW has siblings in a given direction.
473 WINDOW must be a valid window and defaults to the selected one.
475 HORIZONTAL determines a direction for the window combination. If
476 HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
477 a vertical window combination. If HORIZONTAL is non-nil, return
478 non-nil if WINDOW is part of a horizontal window combination."
479 (setq window (window-normalize-window window))
480 (let ((parent (window-parent window)))
481 (and parent
482 (if horizontal
483 (window-left-child parent)
484 (window-top-child parent)))))
486 (defun window-combination-p (&optional window horizontal)
487 "Return WINDOW's first child if WINDOW is a vertical combination.
488 WINDOW can be any window and defaults to the selected one.
489 Optional argument HORIZONTAL non-nil means return WINDOW's first
490 child if WINDOW is a horizontal combination."
491 (setq window (window-normalize-window window))
492 (if horizontal
493 (window-left-child window)
494 (window-top-child window)))
496 (defun window-combinations (window &optional horizontal)
497 "Return largest number of windows vertically arranged within WINDOW.
498 WINDOW must be a valid window and defaults to the selected one.
499 If HORIZONTAL is non-nil, return the largest number of
500 windows horizontally arranged within WINDOW."
501 (setq window (window-normalize-window window))
502 (cond
503 ((window-live-p window)
504 ;; If WINDOW is live, return 1.
506 ((if horizontal
507 (window-left-child window)
508 (window-top-child window))
509 ;; If WINDOW is iso-combined, return the sum of the values for all
510 ;; child windows of WINDOW.
511 (let ((child (window-child window))
512 (count 0))
513 (while child
514 (setq count
515 (+ (window-combinations child horizontal)
516 count))
517 (setq child (window-right child)))
518 count))
520 ;; If WINDOW is not iso-combined, return the maximum value of any
521 ;; child window of WINDOW.
522 (let ((child (window-child window))
523 (count 1))
524 (while child
525 (setq count
526 (max (window-combinations child horizontal)
527 count))
528 (setq child (window-right child)))
529 count))))
531 (defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only)
532 "Helper function for `walk-window-tree' and `walk-window-subtree'."
533 (let (walk-window-tree-buffer)
534 (while walk-window-tree-window
535 (setq walk-window-tree-buffer
536 (window-buffer walk-window-tree-window))
537 (when (or walk-window-tree-buffer any)
538 (funcall fun walk-window-tree-window))
539 (unless walk-window-tree-buffer
540 (walk-window-tree-1
541 fun (window-left-child walk-window-tree-window) any)
542 (walk-window-tree-1
543 fun (window-top-child walk-window-tree-window) any))
544 (if sub-only
545 (setq walk-window-tree-window nil)
546 (setq walk-window-tree-window
547 (window-right walk-window-tree-window))))))
549 (defun walk-window-tree (fun &optional frame any minibuf)
550 "Run function FUN on each live window of FRAME.
551 FUN must be a function with one argument - a window. FRAME must
552 be a live frame and defaults to the selected one. ANY, if
553 non-nil, means to run FUN on all live and internal windows of
554 FRAME.
556 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
557 window even if it isn't active. MINIBUF nil or omitted means run
558 FUN on FRAME's minibuffer window only if it's active. In both
559 cases the minibuffer window must be part of FRAME. MINIBUF
560 neither nil nor t means never run FUN on the minibuffer window.
562 This function performs a pre-order, depth-first traversal of the
563 window tree. If FUN changes the window tree, the result is
564 unpredictable."
565 (setq frame (window-normalize-frame frame))
566 (walk-window-tree-1 fun (frame-root-window frame) any)
567 (when (memq minibuf '(nil t))
568 ;; Run FUN on FRAME's minibuffer window if requested.
569 (let ((minibuffer-window (minibuffer-window frame)))
570 (when (and (window-live-p minibuffer-window)
571 (eq (window-frame minibuffer-window) frame)
572 (or (eq minibuf t)
573 (minibuffer-window-active-p minibuffer-window)))
574 (funcall fun minibuffer-window)))))
576 (defun walk-window-subtree (fun &optional window any)
577 "Run function FUN on the subtree of windows rooted at WINDOW.
578 WINDOW defaults to the selected window. FUN must be a function
579 with one argument - a window. By default, run FUN only on live
580 windows of the subtree. If the optional argument ANY is non-nil,
581 run FUN on all live and internal windows of the subtree. If
582 WINDOW is live, run FUN on WINDOW only.
584 This function performs a pre-order, depth-first traversal of the
585 subtree rooted at WINDOW. If FUN changes that tree, the result
586 is unpredictable."
587 (setq window (window-normalize-window window))
588 (walk-window-tree-1 fun window any t))
590 (defun window-with-parameter (parameter &optional value frame any minibuf)
591 "Return first window on FRAME with PARAMETER non-nil.
592 FRAME defaults to the selected frame. Optional argument VALUE
593 non-nil means only return a window whose window-parameter value
594 for PARAMETER equals VALUE (comparison is done with `equal').
595 Optional argument ANY non-nil means consider internal windows
596 too.
598 Optional argument MINIBUF t means consider FRAME's minibuffer
599 window even if it isn't active. MINIBUF nil or omitted means
600 consider FRAME's minibuffer window only if it's active. In both
601 cases the minibuffer window must be part of FRAME. MINIBUF
602 neither nil nor t means never consider the minibuffer window."
603 (let (this-value)
604 (catch 'found
605 (walk-window-tree
606 (lambda (window)
607 (when (and (setq this-value (window-parameter window parameter))
608 (or (not value) (equal value this-value)))
609 (throw 'found window)))
610 frame any minibuf))))
612 ;;; Atomic windows.
613 (defun window-atom-root (&optional window)
614 "Return root of atomic window WINDOW is a part of.
615 WINDOW must be a valid window and defaults to the selected one.
616 Return nil if WINDOW is not part of an atomic window."
617 (setq window (window-normalize-window window))
618 (let (root)
619 (while (and window (window-parameter window 'window-atom))
620 (setq root window)
621 (setq window (window-parent window)))
622 root))
624 (defun window-make-atom (window)
625 "Make WINDOW an atomic window.
626 WINDOW must be an internal window. Return WINDOW."
627 (if (not (window-child window))
628 (error "Window %s is not an internal window" window)
629 (walk-window-subtree
630 (lambda (window)
631 (unless (window-parameter window 'window-atom)
632 (set-window-parameter window 'window-atom t)))
633 window t)
634 window))
636 (defun display-buffer-in-atom-window (buffer alist)
637 "Display BUFFER in an atomic window.
638 This function displays BUFFER in a new window that will be
639 combined with an existing window to form an atomic window. If
640 the existing window is already part of an atomic window, add the
641 new window to that atomic window. Operations like `split-window'
642 or `delete-window', when applied to a constituent of an atomic
643 window, are applied atomically to the root of that atomic window.
645 ALIST is an association list of symbols and values. The
646 following symbols can be used.
648 `window' specifies the existing window the new window shall be
649 combined with. Use `window-atom-root' to make the new window a
650 sibling of an atomic window's root. If an internal window is
651 specified here, all children of that window become part of the
652 atomic window too. If no window is specified, the new window
653 becomes a sibling of the selected window. By default, the
654 `window-atom' parameter of the existing window is set to `main'
655 provided it is live and was not set before.
657 `side' denotes the side of the existing window where the new
658 window shall be located. Valid values are `below', `right',
659 `above' and `left'. The default is `below'. By default, the
660 `window-atom' parameter of the new window is set to this value.
662 The return value is the new window, nil when creating that window
663 failed."
664 (let* ((ignore-window-parameters t)
665 (window-combination-limit t)
666 (window-combination-resize 'atom)
667 (window (cdr (assq 'window alist)))
668 (side (or (cdr (assq 'side alist)) 'below))
669 (atom (when window (window-parameter window 'window-atom)))
670 root new)
671 (setq window (window-normalize-window window))
672 (setq root (window-atom-root window))
673 ;; Split off new window.
674 (when (setq new (split-window-no-error window nil side))
675 (window-make-atom
676 (if (and root (not (eq root window)))
677 ;; When WINDOW was part of an atomic window and we did not
678 ;; split its root, root atomic window at old root.
679 root
680 ;; Otherwise, root atomic window at WINDOW's new parent.
681 (window-parent window)))
682 ;; Assign `window-atom' parameters, if needed.
683 (when (and (not atom) (window-live-p window))
684 (set-window-parameter window 'window-atom 'main))
685 (set-window-parameter new 'window-atom side)
686 ;; Display BUFFER in NEW and return NEW.
687 (window--display-buffer
688 buffer new 'window alist display-buffer-mark-dedicated))))
690 (defun window--atom-check-1 (window)
691 "Subroutine of `window--atom-check'."
692 (when window
693 (if (window-parameter window 'window-atom)
694 (let ((count 0))
695 (when (or (catch 'reset
696 (walk-window-subtree
697 (lambda (window)
698 (if (window-parameter window 'window-atom)
699 (setq count (1+ count))
700 (throw 'reset t)))
701 window t))
702 ;; count >= 1 must hold here. If there's no other
703 ;; window around dissolve this atomic window.
704 (= count 1))
705 ;; Dissolve atomic window.
706 (walk-window-subtree
707 (lambda (window)
708 (set-window-parameter window 'window-atom nil))
709 window t)))
710 ;; Check children.
711 (unless (window-buffer window)
712 (window--atom-check-1 (window-left-child window))
713 (window--atom-check-1 (window-top-child window))))
714 ;; Check right sibling
715 (window--atom-check-1 (window-right window))))
717 (defun window--atom-check (&optional frame)
718 "Check atomicity of all windows on FRAME.
719 FRAME defaults to the selected frame. If an atomic window is
720 wrongly configured, reset the atomicity of all its windows on
721 FRAME to nil. An atomic window is wrongly configured if it has
722 no child windows or one of its child windows is not atomic."
723 (window--atom-check-1 (frame-root-window frame)))
725 ;; Side windows.
726 (defcustom window-sides-vertical nil
727 "If non-nil, left and right side windows occupy full frame height.
728 If nil, top and bottom side windows occupy full frame width."
729 :type 'boolean
730 :initialize 'custom-initialize-default
731 :set 'window--sides-verticalize
732 :group 'windows
733 :version "26.1")
735 (defcustom window-sides-reversed nil
736 "Whether top/bottom side windows appear in reverse order.
737 When this is nil, side windows on the top and bottom of a frame
738 are always drawn from left to right with increasing slot values.
739 When this is t, side windows on the top and bottom of a frame are
740 always drawn from right to left with increasing slot values.
742 When this is `bidi', the drawing order is like that for the value
743 t if the value of `bidi-paragraph-direction' is `right-to-left'
744 in the buffer most recently shown in the window selected within
745 the main window area of this frame.
747 The layout of side windows on the left or right of a frame is not
748 affected by the value of this variable."
749 :type
750 '(choice (const :tag "Never" nil)
751 (const :tag "Bidi" bidi)
752 (const :tag "Always" t))
753 :initialize 'custom-initialize-default
754 :set 'window--sides-reverse
755 :group 'windows
756 :version "26.1")
758 (defcustom window-sides-slots '(nil nil nil nil)
759 "Number of available side window slots on each side of a frame.
760 The value is a list of four elements specifying the maximum
761 number of side windows that may be created on the left, top,
762 right and bottom side of any frame.
764 If an element is a number, `display-buffer-in-side-window' will
765 refrain from making a new side window if the number of windows on
766 that side is equal to or exceeds that number. Rather, it will
767 reuse the window whose `window-slot' value is nearest to the slot
768 specified via its ALIST argument. If an element is nil, this
769 means there's no bound on the number of windows on that side."
770 :version "24.1"
771 :risky t
772 :type
773 '(list
774 :value (nil nil nil nil)
775 (choice
776 :tag "Left"
777 :help-echo "Maximum number of left side windows."
778 :value nil
779 :format "%[Left%] %v\n"
780 (const :tag "Unlimited" :format "%t" nil)
781 (integer :tag "Number" :value 2 :size 5))
782 (choice
783 :tag "Top"
784 :help-echo "Maximum number of top side windows."
785 :value nil
786 :format "%[Top%] %v\n"
787 (const :tag "Unlimited" :format "%t" nil)
788 (integer :tag "Number" :value 3 :size 5))
789 (choice
790 :tag "Right"
791 :help-echo "Maximum number of right side windows."
792 :value nil
793 :format "%[Right%] %v\n"
794 (const :tag "Unlimited" :format "%t" nil)
795 (integer :tag "Number" :value 2 :size 5))
796 (choice
797 :tag "Bottom"
798 :help-echo "Maximum number of bottom side windows."
799 :value nil
800 :format "%[Bottom%] %v\n"
801 (const :tag "Unlimited" :format "%t" nil)
802 (integer :tag "Number" :value 3 :size 5)))
803 :group 'windows)
805 (defvar-local window--sides-shown nil
806 "Non-nil if this buffer was shown in a side window once.
807 If this variable is non-nil in a buffer, `switch-to-prev-buffer'
808 and `switch-to-next-buffer' will refrain from showing this buffer
809 within the main window area. `display-buffer-in-side-window'
810 sets this variable automatically.
812 Killing buffer local variables after showing the buffer in a side
813 window annihilates any effect provided by this variable.")
815 (defvar window--sides-inhibit-check nil
816 "Non-nil means inhibit any checks on side windows.")
818 (defun window--sides-reverse-on-frame-p (frame)
819 "Return non-nil when side windows should appear reversed on FRAME.
820 This uses some heuristics to guess the user's intentions when the
821 selected window of FRAME is a side window ."
822 (cond
823 ;; Reverse when `window-sides-reversed' is t. Do not reverse when
824 ;; `window-sides-reversed' is nil.
825 ((memq window-sides-reversed '(nil t))
826 window-sides-reversed)
827 ;; Reverse when FRAME's selected window shows a right-to-left buffer.
828 ((let ((window (frame-selected-window frame)))
829 (when (and (not (window-parameter window 'window-side))
830 (or (not (window-minibuffer-p window))
831 (setq window (minibuffer-selected-window))))
832 (with-current-buffer (window-buffer window)
833 (eq bidi-paragraph-direction 'right-to-left)))))
834 ;; Reverse when FRAME's `window-sides-main-selected-window' parameter
835 ;; specifies a live window showing a right-to-left buffer.
836 ((let ((window (frame-parameter
837 frame 'window-sides-main-selected-window)))
838 (when (window-live-p window)
839 (with-current-buffer (window-buffer window)
840 (eq bidi-paragraph-direction 'right-to-left)))))
841 ;; Reverse when all windows in FRAME's main window show right-to-left
842 ;; buffers.
844 (catch 'found
845 (walk-window-subtree
846 (lambda (window)
847 (with-current-buffer (window-buffer window)
848 (when (eq bidi-paragraph-direction 'left-to-right)
849 (throw 'found nil))))
850 (window-main-window frame))
851 t))))
853 (defun window-main-window (&optional frame)
854 "Return the main window of specified FRAME.
855 The optional argument FRAME must be a live frame and defaults to
856 the selected one.
858 If FRAME has no side windows, return FRAME's root window.
859 Otherwise, return either an internal non-side window such that
860 all other non-side windows on FRAME descend from it, or the
861 single live non-side window of FRAME."
862 (let ((frame (window-normalize-frame frame))
863 main sibling)
864 ;; Set main to the _last_ window found by `walk-window-tree' that
865 ;; is not a side window but has a side window as its sibling.
866 (walk-window-tree
867 (lambda (window)
868 (and (not (window-parameter window 'window-side))
869 (or (and (setq sibling (window-prev-sibling window))
870 (window-parameter sibling 'window-side))
871 (and (setq sibling (window-next-sibling window))
872 (window-parameter sibling 'window-side)))
873 (setq main window)))
874 frame t 'nomini)
875 (or main (frame-root-window frame))))
877 (defun window--make-major-side-window-next-to (side)
878 "Return window to split for making a major side window.
879 SIDE must be one of the symbols `left', `top', `right' or
880 `bottom'.
882 This is an auxiliary function of `window--make-major-side-window'
883 and must not be called when a window on SIDE exists already."
884 (let ((root (frame-root-window))
885 (window--sides-inhibit-check t)
886 window)
887 ;; (1) If a window on the opposite side exists, return that window's
888 ;; sibling.
889 ;; (2) If the new window shall span the entire side, return the
890 ;; frame's root window.
891 ;; (3) If a window on an orthogonal side exists, return that
892 ;; window's sibling.
893 ;; (4) Otherwise return the frame's root window.
894 (cond
895 ((or (and (eq side 'left)
896 (setq window (window-with-parameter 'window-side 'right nil t)))
897 (and (eq side 'top)
898 (setq window (window-with-parameter 'window-side 'bottom nil t))))
899 (window-prev-sibling window))
900 ((or (and (eq side 'right)
901 (setq window (window-with-parameter 'window-side 'left nil t)))
902 (and (eq side 'bottom)
903 (setq window (window-with-parameter 'window-side 'top nil t))))
904 (window-next-sibling window))
905 ((memq side '(left right))
906 (cond
907 (window-sides-vertical
908 root)
909 ((setq window (window-with-parameter 'window-side 'top nil t))
910 (window-next-sibling window))
911 ((setq window (window-with-parameter 'window-side 'bottom nil t))
912 (window-prev-sibling window))
913 (t root)))
914 ((memq side '(top bottom))
915 (cond
916 ((not window-sides-vertical)
917 root)
918 ((setq window (window-with-parameter 'window-side 'left nil t))
919 (window-next-sibling window))
920 ((setq window (window-with-parameter 'window-side 'right nil t))
921 (window-prev-sibling window))
922 (t root))))))
924 (defun window--make-major-side-window (buffer side slot &optional alist)
925 "Display BUFFER in a new major side window on the selected frame.
926 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
927 specifies the slot to use. ALIST is an association list of
928 symbols and values as passed to `display-buffer-in-side-window'.
929 Return the new window, nil if its creation failed.
931 This is an auxiliary function of `display-buffer-in-side-window'
932 and may be called only if no window on SIDE exists yet."
933 (let* ((left-or-right (memq side '(left right)))
934 (next-to (window--make-major-side-window-next-to side))
935 (on-side (cond
936 ((eq side 'top) 'above)
937 ((eq side 'bottom) 'below)
938 (t side)))
939 (window--sides-inhibit-check t)
940 ;; The following two bindings will tell `split-window' to take
941 ;; the space for the new window from the selected frame's main
942 ;; window and not make a new parent window unless needed.
943 (window-combination-resize 'side)
944 (window-combination-limit nil)
945 (window (split-window-no-error next-to nil on-side)))
946 (when window
947 ;; Initialize `window-side' parameter of new window to SIDE and
948 ;; make that parameter persistent.
949 (set-window-parameter window 'window-side side)
950 (add-to-list 'window-persistent-parameters '(window-side . writable))
951 ;; Install `window-slot' parameter of new window and make that
952 ;; parameter persistent.
953 (set-window-parameter window 'window-slot slot)
954 (add-to-list 'window-persistent-parameters '(window-slot . writable))
955 ;; Auto-adjust height/width of new window unless a size has been
956 ;; explicitly requested.
957 (unless (if left-or-right
958 (cdr (assq 'window-width alist))
959 (cdr (assq 'window-height alist)))
960 (setq alist
961 (cons
962 (cons
963 (if left-or-right 'window-width 'window-height)
964 (/ (window-total-size (frame-root-window) left-or-right)
965 ;; By default use a fourth of the size of the frame's
966 ;; root window.
968 alist)))
969 (with-current-buffer buffer
970 (setq window--sides-shown t))
971 ;; Install BUFFER in new window and return WINDOW.
972 (window--display-buffer buffer window 'window alist 'side))))
974 (defun display-buffer-in-side-window (buffer alist)
975 "Display BUFFER in a side window of the selected frame.
976 ALIST is an association list of symbols and values. The
977 following special symbols can be used in ALIST.
979 `side' denotes the side of the frame where the new window shall
980 be located. Valid values are `bottom', `right', `top' and
981 `left'. The default is `bottom'.
983 `slot' if non-nil, specifies the window slot where to display
984 BUFFER. A value of zero or nil means use the middle slot on
985 the specified side. A negative value means use a slot
986 preceding (that is, above or on the left of) the middle slot.
987 A positive value means use a slot following (that is, below or
988 on the right of) the middle slot. The default is zero.
990 If the current frame size or the settings of `window-sides-slots'
991 do not permit making a new window, a suitable existing window may
992 be reused and have its `window-slot' parameter value accordingly
993 modified.
995 Unless `display-buffer-mark-dedicated' is non-nil, softly
996 dedicate the side window used to BUFFER. Return the window used
997 for displaying BUFFER, nil if no suitable window can be found.
999 This function installs the `window-side' and `window-slot'
1000 parameters and makes them persistent. It neither modifies ALIST
1001 nor installs any other window parameters unless they have been
1002 explicitly provided via a `window-parameter' entry in ALIST."
1003 (let* ((side (or (cdr (assq 'side alist)) 'bottom))
1004 (slot (or (cdr (assq 'slot alist)) 0))
1005 (left-or-right (memq side '(left right)))
1006 ;; Softly dedicate window to BUFFER unless
1007 ;; `display-buffer-mark-dedicated' already asks for it.
1008 (dedicated (or display-buffer-mark-dedicated 'side)))
1009 (cond
1010 ((not (memq side '(top bottom left right)))
1011 (error "Invalid side %s specified" side))
1012 ((not (numberp slot))
1013 (error "Invalid slot %s specified" slot)))
1015 (let* ((major (window-with-parameter 'window-side side nil t))
1016 ;; `major' is the major window on SIDE, `windows' the list of
1017 ;; life windows on SIDE.
1018 (reversed (window--sides-reverse-on-frame-p (selected-frame)))
1019 (windows
1020 (cond
1021 ((window-live-p major)
1022 (list major))
1023 ((window-valid-p major)
1024 (let* ((first (window-child major))
1025 (next (window-next-sibling first))
1026 (windows (list next first)))
1027 (setq reversed (> (window-parameter first 'window-slot)
1028 (window-parameter next 'window-slot)))
1029 (while (setq next (window-next-sibling next))
1030 (setq windows (cons next windows)))
1031 (if reversed windows (nreverse windows))))))
1032 (slots (when major (max 1 (window-child-count major))))
1033 (max-slots
1034 (nth (cond
1035 ((eq side 'left) 0)
1036 ((eq side 'top) 1)
1037 ((eq side 'right) 2)
1038 ((eq side 'bottom) 3))
1039 window-sides-slots))
1040 (window--sides-inhibit-check t)
1041 window this-window this-slot prev-window next-window
1042 best-window best-slot abs-slot)
1044 (cond
1045 ((and (numberp max-slots) (<= max-slots 0))
1046 ;; No side-slots available on this side. Don't raise an error,
1047 ;; just return nil.
1048 nil)
1049 ((not windows)
1050 ;; No major side window exists on this side, make one.
1051 (window--make-major-side-window buffer side slot alist))
1053 ;; Scan windows on SIDE.
1054 (catch 'found
1055 (dolist (window windows)
1056 (setq this-slot (window-parameter window 'window-slot))
1057 (cond
1058 ;; The following should not happen and probably be checked
1059 ;; by window--sides-check.
1060 ((not (numberp this-slot)))
1061 ((= this-slot slot)
1062 ;; A window with a matching slot has been found.
1063 (setq this-window window)
1064 (throw 'found t))
1066 ;; Check if this window has a better slot value wrt the
1067 ;; slot of the window we want.
1068 (setq abs-slot
1069 (if (or (and (> this-slot 0) (> slot 0))
1070 (and (< this-slot 0) (< slot 0)))
1071 (abs (- slot this-slot))
1072 (+ (abs slot) (abs this-slot))))
1073 (unless (and best-slot (<= best-slot abs-slot))
1074 (setq best-window window)
1075 (setq best-slot abs-slot))
1076 (if reversed
1077 (cond
1078 ((<= this-slot slot)
1079 (setq next-window window))
1080 ((not prev-window)
1081 (setq prev-window window)))
1082 (cond
1083 ((<= this-slot slot)
1084 (setq prev-window window))
1085 ((not next-window)
1086 (setq next-window window))))))))
1088 ;; `this-window' is the first window with the same SLOT.
1089 ;; `prev-window' is the window with the largest slot < SLOT. A new
1090 ;; window will be created after it.
1091 ;; `next-window' is the window with the smallest slot > SLOT. A new
1092 ;; window will be created before it.
1093 ;; `best-window' is the window with the smallest absolute difference
1094 ;; of its slot and SLOT.
1095 (or (and this-window
1096 ;; Reuse `this-window'.
1097 (with-current-buffer buffer
1098 (setq window--sides-shown t))
1099 (window--display-buffer
1100 buffer this-window 'reuse alist dedicated))
1101 (and (or (not max-slots) (< slots max-slots))
1102 (or (and next-window
1103 ;; Make new window before `next-window'.
1104 (let ((next-side (if left-or-right 'above 'left))
1105 (window-combination-resize 'side))
1106 (setq window (split-window-no-error
1107 next-window nil next-side))))
1108 (and prev-window
1109 ;; Make new window after `prev-window'.
1110 (let ((prev-side (if left-or-right 'below 'right))
1111 (window-combination-resize 'side))
1112 (setq window (split-window-no-error
1113 prev-window nil prev-side)))))
1114 (set-window-parameter window 'window-slot slot)
1115 (with-current-buffer buffer
1116 (setq window--sides-shown t))
1117 (window--display-buffer
1118 buffer window 'window alist dedicated))
1119 (and best-window
1120 ;; Reuse `best-window'.
1121 (progn
1122 ;; Give best-window the new slot value.
1123 (set-window-parameter best-window 'window-slot slot)
1124 (with-current-buffer buffer
1125 (setq window--sides-shown t))
1126 (window--display-buffer
1127 buffer best-window 'reuse alist dedicated)))))))))
1129 (defun window-toggle-side-windows (&optional frame)
1130 "Toggle side windows on specified FRAME.
1131 FRAME must be a live frame and defaults to the selected one.
1133 If FRAME has at least one side window, save FRAME's state in the
1134 FRAME's `window-state' frame parameter and delete all side
1135 windows on FRAME afterwards. Otherwise, if FRAME has a
1136 `window-state' parameter, use that to restore any side windows on
1137 FRAME leaving FRAME's main window alone. Signal an error if
1138 FRAME has no side window and no saved state is found."
1139 (interactive)
1140 (let* ((frame (window-normalize-frame frame))
1141 (window--sides-inhibit-check t)
1142 state)
1143 (cond
1144 ((window-with-parameter 'window-side nil frame)
1145 ;; At least one side window exists. Remove all side windows after
1146 ;; saving FRAME's state in its `window-state' parameter.
1147 (set-frame-parameter
1148 frame 'window-state (window-state-get (frame-root-window frame)))
1149 (let ((ignore-window-parameters t))
1150 (delete-other-windows (window-main-window frame))))
1151 ((setq state (frame-parameter frame 'window-state))
1152 ;; A window state was saved for FRAME. Restore it and put the
1153 ;; current root window into its main window.
1154 (let ((main-state (window-state-get (frame-root-window frame))))
1155 (window-state-put state (frame-root-window frame) t)
1156 (window-state-put main-state (window-main-window frame)))
1157 (window--sides-reverse-frame frame))
1159 (error "No side windows state found")))))
1161 (defun window--sides-reverse-all ()
1162 "Maybe reverse side windows on all frames."
1163 (unless window--sides-inhibit-check
1164 (dolist (frame (frame-list))
1165 (window--sides-reverse-frame frame))))
1167 (defun window--sides-reverse-frame (frame)
1168 "Maybe reverse side windows on FRAME."
1169 (when (eq window-sides-reversed 'bidi)
1170 (let ((window (frame-selected-window frame)))
1171 (unless (or (window-parameter window 'window-side)
1172 (window-minibuffer-p window))
1173 (set-frame-parameter
1174 frame 'window-sides-main-selected-window window))))
1175 (window--sides-reverse-side frame 'top)
1176 (window--sides-reverse-side frame 'bottom))
1178 (defun window--sides-reverse-side (frame side)
1179 "Maybe reverse windows on SIDE of FRAME."
1180 (let ((major (window-with-parameter 'window-side side frame t))
1181 (window--sides-inhibit-check t))
1182 (when (and major (not (window-live-p major)))
1183 (let* ((first (window-child major))
1184 (reversed (> (window-parameter first 'window-slot)
1185 (window-parameter
1186 (window-next-sibling first) 'window-slot)))
1187 (reverse (window--sides-reverse-on-frame-p frame)))
1188 (unless (eq reversed reverse)
1189 ;; We have to reverse.
1190 (let ((last (window-last-child major)))
1191 (while (and (not (eq first last))
1192 (not (eq first (window-next-sibling last))))
1193 (window-swap-states first last t)
1194 (setq first (window-next-sibling first))
1195 (setq last (window-prev-sibling last)))))))))
1197 (defun window--sides-reverse (symbol value)
1198 "Helper function for customizing `window-sides-reversed'."
1199 (set-default symbol value)
1200 (remove-hook 'buffer-list-update-hook 'window--sides-reverse-all)
1201 (remove-hook 'window-configuration-change-hook 'window--sides-reverse-all)
1202 (dolist (frame (frame-list))
1203 (set-frame-parameter frame 'window-sides-main-selected-window nil))
1204 (when (eq value 'bidi)
1205 (add-hook 'buffer-list-update-hook 'window--sides-reverse-all)
1206 (add-hook 'window-configuration-change-hook 'window--sides-reverse-all))
1207 (window--sides-reverse-all))
1209 (defun window--sides-verticalize-frame (&optional frame)
1210 "Maybe change side windows layout on specified FRAME."
1211 (setq frame (window-normalize-frame frame))
1212 (let ((window--sides-inhibit-check t)
1213 (root (frame-root-window frame))
1214 (main (window-main-window frame)))
1215 (when (and (not (eq main root))
1216 (not (eq (window-parent main) root))
1217 (window-combined-p main window-sides-vertical))
1218 (let* ((window--sides-inhibit-check t)
1219 (ignore-window-parameters t)
1220 (first (window-child root))
1221 (first-state
1222 (and first (window-parameter first 'window-side)
1223 (window-state-get first)))
1224 (last (window-last-child root))
1225 (last-state
1226 (and last (window-parameter last 'window-side)
1227 (window-state-get last)))
1228 (dummy (get-buffer-create " *dummy*"))
1229 major)
1230 (unwind-protect
1231 (progn
1232 (when first-state (delete-window first))
1233 (when last-state (delete-window last))
1234 (when first-state
1235 (setq major (window--make-major-side-window
1236 dummy (if window-sides-vertical 'top 'left) 0))
1237 (window-state-put first-state major t))
1238 (when last-state
1239 (setq major (window--make-major-side-window
1240 dummy (if window-sides-vertical 'bottom 'right) 0))
1241 (window-state-put last-state major t)))
1242 (kill-buffer " *dummy*"))))))
1244 (defun window--sides-verticalize (symbol value)
1245 "Helper function for customizing `window-sides-vertical'."
1246 (set-default symbol value)
1247 (dolist (frame (frame-list))
1248 (window--sides-verticalize-frame frame)))
1250 (defun window--sides-check-failed (frame)
1251 "Helper function for `window--sides-check'."
1252 (catch 'failed
1253 ;; FRAME must have a main window.
1254 (unless (window-main-window frame)
1255 (error "Frame %s has no main window" frame)
1256 (throw 'failed t))
1257 ;; Now check the side windows.
1258 (dolist (side '(left top right bottom))
1259 (let ((window (window-with-parameter 'window-side side frame t)))
1260 (when window
1261 ;; If WINDOW is live there must be no other window on this frame
1262 ;; with the same `window-side' parameter.
1263 (if (window-live-p window)
1264 (walk-window-tree
1265 (lambda (this)
1266 (when (and (eq (window-parameter this 'window-side) side)
1267 (not (eq this window)))
1268 (error "Window %s has same side %s as window %s but no common parent"
1269 this side window)
1270 (throw 'failed t)))
1271 frame t 'nomini)
1272 (walk-window-tree
1273 (lambda (this)
1274 (if (eq (window-parent this) window)
1275 (unless (eq (window-parameter this 'window-side) side)
1276 (error "Window %s has not same side %s as its parent %s"
1277 this side window)
1278 (throw 'failed t))
1279 (when (and (eq (window-parameter this 'window-side) side)
1280 (not (eq this window)))
1281 (error "Window %s has same side %s as major side window %s but its parent is %s"
1282 this side window (window-parent this))
1283 (throw 'failed t))))
1284 frame t 'nomini)))))))
1286 (defun window--sides-check (frame)
1287 "Check side windows configuration of FRAME.
1288 In a valid side windows configuration there can be at most one
1289 internal side window on each side and all its children must be
1290 live and have the same `window-side' parameter and no other
1291 window with the same `window-side' parameter exists on FRAME. If
1292 there is no such internal window, there may be at most one window
1293 with this side's `window-side' parameter on FRAME.
1295 If the configuration is invalid, reset the `window-side'
1296 parameters of all windows on FRAME."
1297 (when (and (not window--sides-inhibit-check)
1298 (window-with-parameter 'window-side nil frame t)
1299 (window--sides-check-failed frame))
1300 ;; Reset all `window-side' parameters.
1301 (walk-window-tree
1302 (lambda (window)
1303 (set-window-parameter window 'window-side nil))
1304 frame t 'nomini)
1305 (message "Side windows configuration reset for frame %s" frame)))
1307 (defun window--check (&optional frame)
1308 "Check atomic and side windows on FRAME.
1309 FRAME defaults to the selected frame."
1310 (window--sides-check frame)
1311 (window--atom-check frame))
1313 ;; Dumping frame/window contents.
1314 (defun window--dump-window (&optional window erase)
1315 "Dump WINDOW to buffer *window-frame-dump*.
1316 WINDOW must be a valid window and defaults to the selected one.
1317 Optional argument ERASE non-nil means erase *window-frame-dump*
1318 before writing to it."
1319 (setq window (window-normalize-window window))
1320 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1321 (when erase (erase-buffer))
1322 (insert
1323 (format "%s parent: %s\n" window (window-parent window))
1324 (format "pixel left: %s top: %s size: %s x %s new: %s\n"
1325 (window-pixel-left window) (window-pixel-top window)
1326 (window-size window t t) (window-size window nil t)
1327 (window-new-pixel window))
1328 (format "char left: %s top: %s size: %s x %s new: %s\n"
1329 (window-left-column window) (window-top-line window)
1330 (window-total-size window t) (window-total-size window)
1331 (window-new-total window))
1332 (format "normal: %s x %s new: %s\n"
1333 (window-normal-size window t) (window-normal-size window)
1334 (window-new-normal window)))
1335 (when (window-live-p window)
1336 (let ((fringes (window-fringes window))
1337 (margins (window-margins window)))
1338 (insert
1339 (format "body pixel: %s x %s char: %s x %s\n"
1340 (window-body-width window t) (window-body-height window t)
1341 (window-body-width window) (window-body-height window))
1342 (format "width left fringe: %s left margin: %s right margin: %s\n"
1343 (car fringes) (or (car margins) 0) (or (cdr margins) 0))
1344 (format "width right fringe: %s scroll-bar: %s divider: %s\n"
1345 (cadr fringes)
1346 (window-scroll-bar-width window)
1347 (window-right-divider-width window))
1348 (format "height header-line: %s mode-line: %s divider: %s\n"
1349 (window-header-line-height window)
1350 (window-mode-line-height window)
1351 (window-bottom-divider-width window)))))
1352 (insert "\n")))
1354 (defun window--dump-frame (&optional window-or-frame)
1355 "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
1356 WINDOW-OR-FRAME can be a frame or a window and defaults to the
1357 selected frame. When WINDOW-OR-FRAME is a window, dump that
1358 window's frame. The buffer *window-frame-dump* is erased before
1359 dumping to it."
1360 (let* ((window
1361 (cond
1362 ((or (not window-or-frame)
1363 (frame-live-p window-or-frame))
1364 (frame-root-window window-or-frame))
1365 ((or (window-live-p window-or-frame)
1366 (window-child window-or-frame))
1367 window-or-frame)
1369 (frame-root-window))))
1370 (frame (window-frame window)))
1371 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1372 (erase-buffer)
1373 (insert
1374 (format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
1375 (frame-pixel-width frame) (frame-pixel-height frame)
1376 (frame-total-cols frame) (frame-total-lines frame)
1377 (frame-char-width frame) (frame-char-height frame))
1378 (format "frame text pixel: %s x %s cols/lines: %s x %s\n"
1379 (frame-text-width frame) (frame-text-height frame)
1380 (frame-text-cols frame) (frame-text-lines frame))
1381 (format "tool: %s scroll: %s/%s fringe: %s border: %s right: %s bottom: %s\n\n"
1382 (if (fboundp 'tool-bar-height)
1383 (tool-bar-height frame t)
1384 "0")
1385 (frame-scroll-bar-width frame)
1386 (frame-scroll-bar-height frame)
1387 (frame-fringe-width frame)
1388 (frame-border-width frame)
1389 (frame-right-divider-width frame)
1390 (frame-bottom-divider-width frame)))
1391 (walk-window-tree 'window--dump-window frame t t))))
1393 ;;; Window sizes.
1394 (defun window-total-size (&optional window horizontal round)
1395 "Return the total height or width of WINDOW.
1396 WINDOW must be a valid window and defaults to the selected one.
1398 If HORIZONTAL is omitted or nil, return the total height of
1399 WINDOW, in lines. If WINDOW is live, its total height includes,
1400 in addition to the height of WINDOW's text, the heights of
1401 WINDOW's mode and header line and a bottom divider, if any.
1403 If HORIZONTAL is non-nil, return the total width of WINDOW, in
1404 columns. If WINDOW is live, its total width includes, in
1405 addition to the width of WINDOW's text, the widths of WINDOW's
1406 fringes, margins, scroll bars and its right divider, if any.
1408 If WINDOW is internal, return the respective size of the screen
1409 areas spanned by its children.
1411 Optional argument ROUND is handled as for `window-total-height'
1412 and `window-total-width'."
1413 (if horizontal
1414 (window-total-width window round)
1415 (window-total-height window round)))
1417 (defun window-size (&optional window horizontal pixelwise round)
1418 "Return the height or width of WINDOW.
1419 WINDOW must be a valid window and defaults to the selected one.
1421 If HORIZONTAL is omitted or nil, return the total height of
1422 WINDOW, in lines, like `window-total-height'. Otherwise return
1423 the total width, in columns, like `window-total-width'.
1425 Optional argument PIXELWISE means return the pixel size of WINDOW
1426 like `window-pixel-height' and `window-pixel-width'.
1428 Optional argument ROUND is ignored if PIXELWISE is non-nil and
1429 handled as for `window-total-height' and `window-total-width'
1430 otherwise."
1431 (if horizontal
1432 (if pixelwise
1433 (window-pixel-width window)
1434 (window-total-width window round))
1435 (if pixelwise
1436 (window-pixel-height window)
1437 (window-total-height window round))))
1439 (defvar window-size-fixed nil
1440 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
1441 If the value is `height', then only the window's height is fixed.
1442 If the value is `width', then only the window's width is fixed.
1443 Any other non-nil value fixes both the width and the height.
1445 Emacs won't change the size of any window displaying that buffer,
1446 unless it has no other choice (like when deleting a neighboring
1447 window).")
1448 (make-variable-buffer-local 'window-size-fixed)
1450 (defun window--preservable-size (window &optional horizontal)
1451 "Return height of WINDOW as `window-preserve-size' would preserve it.
1452 Optional argument HORIZONTAL non-nil means to return the width of
1453 WINDOW as `window-preserve-size' would preserve it."
1454 (if horizontal
1455 (window-body-width window t)
1456 (+ (window-body-height window t)
1457 (window-header-line-height window)
1458 (window-mode-line-height window))))
1460 (defun window-preserve-size (&optional window horizontal preserve)
1461 "Preserve height of window WINDOW.
1462 WINDOW must be a live window and defaults to the selected one.
1463 Optional argument HORIZONTAL non-nil means preserve the width of
1464 WINDOW.
1466 PRESERVE t means to preserve the current height/width of WINDOW's
1467 body in frame and window resizing operations whenever possible.
1468 The height/width of WINDOW will change only if Emacs has no other
1469 choice. Resizing a window whose height/width is preserved never
1470 throws an error.
1472 PRESERVE nil means to stop preserving the height/width of WINDOW,
1473 lifting the respective restraint induced by a previous call of
1474 `window-preserve-size' for WINDOW. Calling `enlarge-window',
1475 `shrink-window', `split-window' or `fit-window-to-buffer' with
1476 WINDOW as argument also removes the respective restraint.
1478 Other values of PRESERVE are reserved for future use."
1479 (setq window (window-normalize-window window t))
1480 (let* ((parameter (window-parameter window 'window-preserved-size))
1481 (width (nth 1 parameter))
1482 (height (nth 2 parameter)))
1483 (if horizontal
1484 (set-window-parameter
1485 window 'window-preserved-size
1486 (list
1487 (window-buffer window)
1488 (and preserve (window--preservable-size window t))
1489 height))
1490 (set-window-parameter
1491 window 'window-preserved-size
1492 (list
1493 (window-buffer window)
1494 width
1495 (and preserve (window--preservable-size window)))))))
1497 (defun window-preserved-size (&optional window horizontal)
1498 "Return preserved height of window WINDOW.
1499 WINDOW must be a live window and defaults to the selected one.
1500 Optional argument HORIZONTAL non-nil means to return preserved
1501 width of WINDOW."
1502 (setq window (window-normalize-window window t))
1503 (let* ((parameter (window-parameter window 'window-preserved-size))
1504 (buffer (nth 0 parameter))
1505 (width (nth 1 parameter))
1506 (height (nth 2 parameter)))
1507 (when (eq buffer (window-buffer window))
1508 (if horizontal width height))))
1510 (defun window--preserve-size (window horizontal)
1511 "Return non-nil when the height of WINDOW shall be preserved.
1512 Optional argument HORIZONTAL non-nil means to return non-nil when
1513 the width of WINDOW shall be preserved."
1514 (let ((size (window-preserved-size window horizontal)))
1515 (and (numberp size)
1516 (= size (window--preservable-size window horizontal)))))
1518 (defun window-safe-min-size (&optional window horizontal pixelwise)
1519 "Return safe minimum size of WINDOW.
1520 WINDOW must be a valid window and defaults to the selected one.
1521 Optional argument HORIZONTAL non-nil means return the minimum
1522 number of columns of WINDOW; otherwise return the minimum number
1523 of WINDOW's lines.
1525 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1526 of WINDOW."
1527 (setq window (window-normalize-window window))
1528 (if pixelwise
1529 (if horizontal
1530 (* window-safe-min-width
1531 (frame-char-width (window-frame window)))
1532 (* window-safe-min-height
1533 (frame-char-height (window-frame window))))
1534 (if horizontal window-safe-min-width window-safe-min-height)))
1536 (defun window-min-size (&optional window horizontal ignore pixelwise)
1537 "Return the minimum size of WINDOW.
1538 WINDOW must be a valid window and defaults to the selected one.
1539 Optional argument HORIZONTAL non-nil means return the minimum
1540 number of columns of WINDOW; otherwise return the minimum number
1541 of WINDOW's lines.
1543 The optional argument IGNORE has the same meaning as for
1544 `window-resizable'. Optional argument PIXELWISE non-nil means
1545 return the minimum pixel-size of WINDOW."
1546 (window--min-size-1
1547 (window-normalize-window window) horizontal ignore pixelwise))
1549 (defun window--min-size-ignore-p (window ignore)
1550 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
1551 (if (window-valid-p ignore)
1552 (eq window ignore)
1553 (not (memq ignore '(nil preserved)))))
1555 (defun window--min-size-1 (window horizontal ignore pixelwise)
1556 "Internal function of `window-min-size'."
1557 (let ((sub (window-child window)))
1558 (if sub
1559 (let ((value 0))
1560 ;; WINDOW is an internal window.
1561 (if (window-combined-p sub horizontal)
1562 ;; The minimum size of an iso-combination is the sum of
1563 ;; the minimum sizes of its child windows.
1564 (while sub
1565 (setq value (+ value
1566 (window--min-size-1
1567 sub horizontal ignore pixelwise)))
1568 (setq sub (window-right sub)))
1569 ;; The minimum size of an ortho-combination is the maximum
1570 ;; of the minimum sizes of its child windows.
1571 (while sub
1572 (setq value (max value
1573 (window--min-size-1
1574 sub horizontal ignore pixelwise)))
1575 (setq sub (window-right sub))))
1576 value)
1577 (with-current-buffer (window-buffer window)
1578 (cond
1579 ((window-minibuffer-p window)
1580 (if pixelwise (frame-char-height (window-frame window)) 1))
1581 ((window-size-fixed-p window horizontal ignore)
1582 ;; The minimum size of a fixed size window is its size.
1583 (window-size window horizontal pixelwise))
1584 ((eq ignore 'safe)
1585 ;; If IGNORE equals `safe' return the safe value.
1586 (window-safe-min-size window horizontal pixelwise))
1587 (horizontal
1588 ;; For the minimum width of a window take fringes and
1589 ;; scroll-bars into account. This is questionable and should
1590 ;; be removed as soon as we are able to split (and resize)
1591 ;; windows such that the new (or resized) windows can get a
1592 ;; size less than the user-specified `window-min-height' and
1593 ;; `window-min-width'.
1594 (let* ((char-size (frame-char-size window t))
1595 (fringes (window-fringes window))
1596 (margins (window-margins window))
1597 ;; Let the 'min-margins' parameter override the actual
1598 ;; widths of the margins. We allow any number to
1599 ;; replace the values specified by `window-margins'.
1600 ;; See bug#24193 for the rationale of this parameter.
1601 (min-margins (window-parameter window 'min-margins))
1602 (left-min-margin (and min-margins
1603 (numberp (car min-margins))
1604 (car min-margins)))
1605 (right-min-margin (and min-margins
1606 (numberp (cdr min-margins))
1607 (cdr min-margins)))
1608 (pixel-width
1609 (+ (window-safe-min-size window t t)
1610 (* (or left-min-margin (car margins) 0) char-size)
1611 (* (or right-min-margin(cdr margins) 0) char-size)
1612 (car fringes) (cadr fringes)
1613 (window-scroll-bar-width window)
1614 (window-right-divider-width window))))
1615 (if pixelwise
1616 (max
1617 (if window-resize-pixelwise
1618 pixel-width
1619 ;; Round up to next integral of columns.
1620 (* (ceiling pixel-width char-size) char-size))
1621 (if (window--min-size-ignore-p window ignore)
1623 (window-min-pixel-width window)))
1624 (max
1625 (ceiling pixel-width char-size)
1626 (if (window--min-size-ignore-p window ignore)
1628 window-min-width)))))
1629 ((let ((char-size (frame-char-size window))
1630 (pixel-height
1631 (+ (window-safe-min-size window nil t)
1632 (window-header-line-height window)
1633 (window-scroll-bar-height window)
1634 (window-mode-line-height window)
1635 (window-bottom-divider-width window))))
1636 (if pixelwise
1637 (max
1638 (if window-resize-pixelwise
1639 pixel-height
1640 ;; Round up to next integral of lines.
1641 (* (ceiling pixel-height char-size) char-size))
1642 (if (window--min-size-ignore-p window ignore)
1644 (window-min-pixel-height window)))
1645 (max (ceiling pixel-height char-size)
1646 (if (window--min-size-ignore-p window ignore)
1648 window-min-height))))))))))
1650 (defun window-sizable (window delta &optional horizontal ignore pixelwise)
1651 "Return DELTA if DELTA lines can be added to WINDOW.
1652 WINDOW must be a valid window and defaults to the selected one.
1653 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1654 columns can be added to WINDOW. A return value of zero means
1655 that no lines (or columns) can be added to WINDOW.
1657 This function looks only at WINDOW and, recursively, its child
1658 windows. The function `window-resizable' looks at other windows
1659 as well.
1661 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1662 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1663 return the maximum value in the range 0..DELTA by which WINDOW
1664 can be enlarged.
1666 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1667 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1668 return the minimum value in the range DELTA..0 by which WINDOW
1669 can be shrunk.
1671 The optional argument IGNORE has the same meaning as for
1672 `window-resizable'. Optional argument PIXELWISE non-nil means
1673 interpret DELTA as pixels."
1674 (setq window (window-normalize-window window))
1675 (cond
1676 ((< delta 0)
1677 (max (- (window-min-size window horizontal ignore pixelwise)
1678 (window-size window horizontal pixelwise))
1679 delta))
1680 ((> delta 0)
1681 (if (window-size-fixed-p window horizontal ignore)
1683 delta))
1684 (t 0)))
1686 (defun window-sizable-p (window delta &optional horizontal ignore pixelwise)
1687 "Return t if WINDOW can be resized by DELTA lines.
1688 WINDOW must be a valid window and defaults to the selected one.
1689 For the meaning of the arguments of this function see the
1690 doc-string of `window-sizable'."
1691 (setq window (window-normalize-window window))
1692 (if (> delta 0)
1693 (>= (window-sizable window delta horizontal ignore pixelwise)
1694 delta)
1695 (<= (window-sizable window delta horizontal ignore pixelwise)
1696 delta)))
1698 (defun window--size-fixed-1 (window horizontal ignore)
1699 "Internal function for `window-size-fixed-p'."
1700 (let ((sub (window-child window)))
1701 (catch 'fixed
1702 (if sub
1703 ;; WINDOW is an internal window.
1704 (if (window-combined-p sub horizontal)
1705 ;; An iso-combination is fixed size if all its child
1706 ;; windows are fixed-size.
1707 (progn
1708 (while sub
1709 (unless (window--size-fixed-1 sub horizontal ignore)
1710 ;; We found a non-fixed-size child window, so
1711 ;; WINDOW's size is not fixed.
1712 (throw 'fixed nil))
1713 (setq sub (window-right sub)))
1714 ;; All child windows are fixed-size, so WINDOW's size is
1715 ;; fixed.
1716 (throw 'fixed t))
1717 ;; An ortho-combination is fixed-size if at least one of its
1718 ;; child windows is fixed-size.
1719 (while sub
1720 (when (window--size-fixed-1 sub horizontal ignore)
1721 ;; We found a fixed-size child window, so WINDOW's size
1722 ;; is fixed.
1723 (throw 'fixed t))
1724 (setq sub (window-right sub))))
1725 ;; WINDOW is a live window.
1726 (and (or (not (windowp ignore)) (not (eq window ignore)))
1727 (or (and (not (eq ignore 'preserved))
1728 (window--preserve-size window horizontal))
1729 (with-current-buffer (window-buffer window)
1730 (if horizontal
1731 (memq window-size-fixed '(width t))
1732 (memq window-size-fixed '(height t))))))))))
1734 (defun window-size-fixed-p (&optional window horizontal ignore)
1735 "Return non-nil if WINDOW's height is fixed.
1736 WINDOW must be a valid window and defaults to the selected one.
1737 Optional argument HORIZONTAL non-nil means return non-nil if
1738 WINDOW's width is fixed. The optional argument IGNORE has the
1739 same meaning as for `window-resizable'.
1741 If this function returns nil, this does not necessarily mean that
1742 WINDOW can be resized in the desired direction. The function
1743 `window-resizable' can tell that."
1744 (when (or (windowp ignore) (memq ignore '(nil preserved)))
1745 (window--size-fixed-1
1746 (window-normalize-window window) horizontal ignore)))
1748 (defun window--min-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
1749 "Internal function for `window-min-delta'."
1750 (if (not (window-parent window))
1751 ;; If we can't go up, return zero.
1753 ;; Else try to find a non-fixed-size sibling of WINDOW.
1754 (let* ((parent (window-parent window))
1755 (sub (window-child parent)))
1756 (catch 'done
1757 (if (window-combined-p sub horizontal)
1758 ;; In an iso-combination throw DELTA if we find at least one
1759 ;; child window and that window is either not fixed-size or
1760 ;; we can ignore fixed-sizeness.
1761 (let ((skip (eq trail 'after)))
1762 (while sub
1763 (cond
1764 ((eq sub window)
1765 (setq skip (eq trail 'before)))
1766 (skip)
1767 ((window-size-fixed-p sub horizontal ignore))
1769 ;; We found a non-fixed-size child window.
1770 (throw 'done delta)))
1771 (setq sub (window-right sub))))
1772 ;; In an ortho-combination set DELTA to the minimum value by
1773 ;; which other child windows can shrink.
1774 (while sub
1775 (unless (eq sub window)
1776 (setq delta
1777 (min delta
1778 (max (- (window-size sub horizontal pixelwise 'ceiling)
1779 (window-min-size
1780 sub horizontal ignore pixelwise))
1781 0))))
1782 (setq sub (window-right sub))))
1783 (if noup
1784 delta
1785 (window--min-delta-1
1786 parent delta horizontal ignore trail nil pixelwise))))))
1788 (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise)
1789 "Return number of lines by which WINDOW can be shrunk.
1790 WINDOW must be a valid window and defaults to the selected one.
1791 Return zero if WINDOW cannot be shrunk.
1793 Optional argument HORIZONTAL non-nil means return number of
1794 columns by which WINDOW can be shrunk.
1796 The optional argument IGNORE has the same meaning as for
1797 `window-resizable'. Optional argument TRAIL restricts the
1798 windows that can be enlarged. If its value is `before', only
1799 windows to the left of or above WINDOW can be enlarged. If it is
1800 `after', only windows to the right of or below WINDOW can be
1801 enlarged.
1803 Optional argument NOUP non-nil means don't go up in the window
1804 tree, but try to enlarge windows within WINDOW's combination
1805 only. Optional argument NODOWN non-nil means don't check whether
1806 WINDOW itself (and its child windows) can be shrunk; check only
1807 whether at least one other window can be enlarged appropriately.
1809 Optional argument PIXELWISE non-nil means return number of pixels
1810 by which WINDOW can be shrunk."
1811 (setq window (window-normalize-window window))
1812 (let ((size (window-size window horizontal pixelwise 'floor))
1813 (minimum (window-min-size window horizontal ignore pixelwise)))
1814 (cond
1815 (nodown
1816 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1817 (window--min-delta-1
1818 window size horizontal ignore trail noup pixelwise))
1819 ((<= size minimum)
1820 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1821 ;; there's nothing to recover.
1824 ;; Otherwise, try to recover whatever WINDOW is larger than its
1825 ;; minimum size.
1826 (window--min-delta-1
1827 window (- size minimum) horizontal ignore trail noup pixelwise)))))
1829 (defun frame-windows-min-size (&optional frame horizontal ignore pixelwise)
1830 "Return minimum number of lines of FRAME's windows.
1831 HORIZONTAL non-nil means return number of columns of FRAME's
1832 windows. The optional argument IGNORE has the same meaning as
1833 for `window-resizable'. PIXELWISE non-nil means return sizes in
1834 pixels."
1835 (setq frame (window-normalize-frame frame))
1836 (let* ((root (frame-root-window frame))
1837 (mini (window-next-sibling root)))
1838 (+ (window-min-size root horizontal ignore pixelwise)
1839 (if (and mini (not horizontal))
1840 (window-min-size mini horizontal nil pixelwise)
1841 0))))
1843 (defun window--max-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
1844 "Internal function of `window-max-delta'."
1845 (if (not (window-parent window))
1846 ;; Can't go up. Return DELTA.
1847 delta
1848 (let* ((parent (window-parent window))
1849 (sub (window-child parent)))
1850 (catch 'fixed
1851 (if (window-combined-p sub horizontal)
1852 ;; For an iso-combination calculate how much we can get from
1853 ;; other child windows.
1854 (let ((skip (eq trail 'after)))
1855 (while sub
1856 (cond
1857 ((eq sub window)
1858 (setq skip (eq trail 'before)))
1859 (skip)
1861 (setq delta
1862 (+ delta
1863 (max
1864 (- (window-size sub horizontal pixelwise 'floor)
1865 (window-min-size
1866 sub horizontal ignore pixelwise))
1867 0)))))
1868 (setq sub (window-right sub))))
1869 ;; For an ortho-combination throw DELTA when at least one
1870 ;; child window is fixed-size.
1871 (while sub
1872 (when (and (not (eq sub window))
1873 (window-size-fixed-p sub horizontal ignore))
1874 (throw 'fixed delta))
1875 (setq sub (window-right sub))))
1876 (if noup
1877 ;; When NOUP is nil, DELTA is all we can get.
1878 delta
1879 ;; Else try with parent of WINDOW, passing the DELTA we
1880 ;; recovered so far.
1881 (window--max-delta-1
1882 parent delta horizontal ignore trail nil pixelwise))))))
1884 (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise)
1885 "Return maximum number of lines by which WINDOW can be enlarged.
1886 WINDOW must be a valid window and defaults to the selected one.
1887 The return value is zero if WINDOW cannot be enlarged.
1889 Optional argument HORIZONTAL non-nil means return maximum number
1890 of columns by which WINDOW can be enlarged.
1892 The optional argument IGNORE has the same meaning as for
1893 `window-resizable'. Optional argument TRAIL restricts the
1894 windows that can be enlarged. If its value is `before', only
1895 windows to the left of or above WINDOW can be enlarged. If it is
1896 `after', only windows to the right of or below WINDOW can be
1897 enlarged.
1899 Optional argument NOUP non-nil means don't go up in the window
1900 tree but try to obtain the entire space from windows within
1901 WINDOW's combination. Optional argument NODOWN non-nil means do
1902 not check whether WINDOW itself (and its child windows) can be
1903 enlarged; check only whether other windows can be shrunk
1904 appropriately.
1906 Optional argument PIXELWISE non-nil means return number of
1907 pixels by which WINDOW can be enlarged."
1908 (setq window (window-normalize-window window))
1909 (if (and (not nodown) (window-size-fixed-p window horizontal ignore))
1910 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1911 ;; size.
1913 ;; WINDOW has no fixed size.
1914 (window--max-delta-1 window 0 horizontal ignore trail noup pixelwise)))
1916 ;; Make NOUP also inhibit the min-size check.
1917 (defun window--resizable (window delta &optional horizontal ignore trail noup nodown pixelwise)
1918 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1919 WINDOW must be a valid window and defaults to the selected one.
1920 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1921 can be resized horizontally by DELTA columns. A return value of
1922 zero means that WINDOW is not resizable.
1924 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1925 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1926 return the maximum value in the range 0..DELTA by which WINDOW
1927 can be enlarged.
1929 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1930 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1931 return the minimum value in the range DELTA..0 that can be used
1932 for shrinking WINDOW.
1934 The optional argument IGNORE has the same meaning as for
1935 `window-resizable'. Optional argument TRAIL `before' means only
1936 windows to the left of or below WINDOW can be shrunk. Optional
1937 argument TRAIL `after' means only windows to the right of or
1938 above WINDOW can be shrunk.
1940 Optional argument NOUP non-nil means don't go up in the window
1941 tree but check only whether space can be obtained from (or given
1942 to) WINDOW's siblings. Optional argument NODOWN non-nil means
1943 don't go down in the window tree. This means do not check
1944 whether resizing would violate size restrictions of WINDOW or its
1945 child windows.
1947 Optional argument PIXELWISE non-nil means interpret DELTA as
1948 number of pixels."
1949 (setq window (window-normalize-window window))
1950 (cond
1951 ((< delta 0)
1952 (max (- (window-min-delta
1953 window horizontal ignore trail noup nodown pixelwise))
1954 delta))
1955 ((> delta 0)
1956 (min (window-max-delta
1957 window horizontal ignore trail noup nodown pixelwise)
1958 delta))
1959 (t 0)))
1961 (defun window--resizable-p (window delta &optional horizontal ignore trail noup nodown pixelwise)
1962 "Return t if WINDOW can be resized vertically by DELTA lines.
1963 WINDOW must be a valid window and defaults to the selected one.
1964 For the meaning of the arguments of this function see the
1965 doc-string of `window--resizable'.
1967 Optional argument PIXELWISE non-nil means interpret DELTA as
1968 pixels."
1969 (setq window (window-normalize-window window))
1970 (if (> delta 0)
1971 (>= (window--resizable
1972 window delta horizontal ignore trail noup nodown pixelwise)
1973 delta)
1974 (<= (window--resizable
1975 window delta horizontal ignore trail noup nodown pixelwise)
1976 delta)))
1978 (defun window-resizable (window delta &optional horizontal ignore pixelwise)
1979 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1980 WINDOW must be a valid window and defaults to the selected one.
1981 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1982 can be resized horizontally by DELTA columns. A return value of
1983 zero means that WINDOW is not resizable.
1985 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1986 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1987 return the maximum value in the range 0..DELTA by which WINDOW
1988 can be enlarged.
1990 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1991 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1992 return the minimum value in the range DELTA..0 that can be used
1993 for shrinking WINDOW.
1995 Optional argument IGNORE, if non-nil, means to ignore restraints
1996 induced by fixed size windows or the values of the variables
1997 `window-min-height' and `window-min-width'. The following values
1998 have special meanings: `safe' means that in addition live windows
1999 are allowed to get as small as `window-safe-min-height' lines and
2000 `window-safe-min-width' columns. `preserved' means to ignore
2001 only restrictions induced by `window-preserve-size'. If IGNORE
2002 is a window, then ignore restrictions for that window only.
2004 Optional argument PIXELWISE non-nil means interpret DELTA as
2005 pixels."
2006 (setq window (window-normalize-window window))
2007 (window--resizable window delta horizontal ignore nil nil nil pixelwise))
2009 (defun window-resizable-p (window delta &optional horizontal ignore pixelwise)
2010 "Return t if WINDOW can be resized vertically by DELTA lines.
2011 WINDOW must be a valid window and defaults to the selected one.
2012 For the meaning of the arguments of this function see the
2013 doc-string of `window-resizable'."
2014 (setq window (window-normalize-window window))
2015 (if (> delta 0)
2016 (>= (window--resizable
2017 window delta horizontal ignore nil nil nil pixelwise)
2018 delta)
2019 (<= (window--resizable
2020 window delta horizontal ignore nil nil nil pixelwise)
2021 delta)))
2023 ;; Aliases of functions defined in window.c.
2024 (defalias 'window-height 'window-total-height)
2025 (defalias 'window-width 'window-body-width)
2027 (defun window-full-height-p (&optional window)
2028 "Return t if WINDOW is as high as its containing frame.
2029 More precisely, return t if and only if the total height of
2030 WINDOW equals the total height of the root window of WINDOW's
2031 frame. WINDOW must be a valid window and defaults to the
2032 selected one."
2033 (setq window (window-normalize-window window))
2034 (if (window-minibuffer-p window)
2035 (eq window (frame-root-window (window-frame window)))
2036 (= (window-pixel-height window)
2037 (window-pixel-height (frame-root-window window)))))
2039 (defun window-full-width-p (&optional window)
2040 "Return t if WINDOW is as wide as its containing frame.
2041 More precisely, return t if and only if the total width of WINDOW
2042 equals the total width of the root window of WINDOW's frame.
2043 WINDOW must be a valid window and defaults to the selected one."
2044 (setq window (window-normalize-window window))
2045 (= (window-pixel-width window)
2046 (window-pixel-width (frame-root-window window))))
2048 (defun window-body-size (&optional window horizontal pixelwise)
2049 "Return the height or width of WINDOW's text area.
2050 WINDOW must be a live window and defaults to the selected one.
2052 If HORIZONTAL is omitted or nil, return the height of the text
2053 area, like `window-body-height'. Otherwise, return the width of
2054 the text area, like `window-body-width'. In either case, the
2055 optional argument PIXELWISE is passed to the functions."
2056 (if horizontal
2057 (window-body-width window pixelwise)
2058 (window-body-height window pixelwise)))
2060 (declare-function font-info "font.c" (name &optional frame))
2062 (defun window-font-width (&optional window face)
2063 "Return average character width for the font of FACE used in WINDOW.
2064 WINDOW must be a live window and defaults to the selected one.
2066 If FACE is nil or omitted, the default face is used. If FACE is
2067 remapped (see `face-remapping-alist'), the function returns the
2068 information for the remapped face."
2069 (with-selected-window (window-normalize-window window t)
2070 (if (display-multi-font-p)
2071 (let* ((face (if face face 'default))
2072 (info (font-info (face-font face)))
2073 (width (aref info 11)))
2074 (if (> width 0)
2075 width
2076 (aref info 10)))
2077 (frame-char-width))))
2079 (defun window-font-height (&optional window face)
2080 "Return character height for the font of FACE used in WINDOW.
2081 WINDOW must be a live window and defaults to the selected one.
2083 If FACE is nil or omitted, the default face is used. If FACE is
2084 remapped (see `face-remapping-alist'), the function returns the
2085 information for the remapped face."
2086 (with-selected-window (window-normalize-window window t)
2087 (if (display-multi-font-p)
2088 (let* ((face (if face face 'default))
2089 (info (font-info (face-font face))))
2090 (aref info 3))
2091 (frame-char-height))))
2093 (defvar overflow-newline-into-fringe)
2095 (defun window-max-chars-per-line (&optional window face)
2096 "Return the number of characters that can be displayed on one line in WINDOW.
2097 WINDOW must be a live window and defaults to the selected one.
2099 The character width of FACE is used for the calculation. If FACE
2100 is nil or omitted, the default face is used. If FACE is
2101 remapped (see `face-remapping-alist'), the function uses the
2102 remapped face.
2104 This function is different from `window-body-width' in two
2105 ways. First, it accounts for the portions of the line reserved
2106 for the continuation glyph. Second, it accounts for the size of
2107 the font."
2108 (with-selected-window (window-normalize-window window t)
2109 (let* ((window-width (window-body-width window t))
2110 (font-width (window-font-width window face))
2111 (ncols (/ window-width font-width)))
2112 (if (and (display-graphic-p)
2113 overflow-newline-into-fringe
2114 (not
2115 (or (eq left-fringe-width 0)
2116 (and (null left-fringe-width)
2117 (= (frame-parameter nil 'left-fringe) 0))))
2118 (not
2119 (or (eq right-fringe-width 0)
2120 (and (null right-fringe-width)
2121 (= (frame-parameter nil 'right-fringe) 0)))))
2122 ncols
2123 ;; FIXME: This should remove 1 more column when there are no
2124 ;; fringes, lines are truncated, and the window is hscrolled,
2125 ;; but EOL is not in the view, because then there are 2
2126 ;; truncation glyphs, not one.
2127 (1- ncols)))))
2129 (defun window-current-scroll-bars (&optional window)
2130 "Return the current scroll bar types for WINDOW.
2131 WINDOW must be a live window and defaults to the selected one.
2133 The return value is a cons cell (VERTICAL . HORIZONTAL) where
2134 VERTICAL specifies the current location of the vertical scroll
2135 bar (`left', `right' or nil), and HORIZONTAL specifies the
2136 current location of the horizontal scroll bar (`bottom' or nil).
2138 Unlike `window-scroll-bars', this function reports the scroll bar
2139 type actually used, once frame defaults and `scroll-bar-mode' are
2140 taken into account."
2141 (setq window (window-normalize-window window t))
2142 (let ((vertical (nth 2 (window-scroll-bars window)))
2143 (horizontal (nth 5 (window-scroll-bars window)))
2144 (inherited (frame-current-scroll-bars (window-frame window))))
2145 (when (eq vertical t)
2146 (setq vertical (car inherited)))
2147 (when (eq horizontal t)
2148 (setq horizontal (cdr inherited)))
2149 (cons vertical (and horizontal 'bottom))))
2151 (defun walk-windows (fun &optional minibuf all-frames)
2152 "Cycle through all live windows, calling FUN for each one.
2153 FUN must specify a function with a window as its sole argument.
2154 The optional arguments MINIBUF and ALL-FRAMES specify the set of
2155 windows to include in the walk.
2157 MINIBUF t means include the minibuffer window even if the
2158 minibuffer is not active. MINIBUF nil or omitted means include
2159 the minibuffer window only if the minibuffer is active. Any
2160 other value means do not include the minibuffer window even if
2161 the minibuffer is active.
2163 ALL-FRAMES nil or omitted means consider all windows on the
2164 selected frame, plus the minibuffer window if specified by the
2165 MINIBUF argument. If the minibuffer counts, consider all windows
2166 on all frames that share that minibuffer too. The following
2167 non-nil values of ALL-FRAMES have special meanings:
2169 - t means consider all windows on all existing frames.
2171 - `visible' means consider all windows on all visible frames on
2172 the current terminal.
2174 - 0 (the number zero) means consider all windows on all visible
2175 and iconified frames on the current terminal.
2177 - A frame means consider all windows on that frame only.
2179 Anything else means consider all windows on the selected frame
2180 and no others.
2182 This function changes neither the order of recently selected
2183 windows nor the buffer list."
2184 ;; If we start from the minibuffer window, don't fail to come
2185 ;; back to it.
2186 (when (window-minibuffer-p)
2187 (setq minibuf t))
2188 ;; Make sure to not mess up the order of recently selected
2189 ;; windows. Use `save-selected-window' and `select-window'
2190 ;; with second argument non-nil for this purpose.
2191 (save-selected-window
2192 (when (framep all-frames)
2193 (select-window (frame-first-window all-frames) 'norecord))
2194 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
2195 (funcall fun walk-windows-window))))
2197 (defun window-at-side-p (&optional window side)
2198 "Return t if WINDOW is at SIDE of its containing frame.
2199 WINDOW must be a valid window and defaults to the selected one.
2200 SIDE can be any of the symbols `left', `top', `right' or
2201 `bottom'. The default value nil is handled like `bottom'."
2202 (setq window (window-normalize-window window))
2203 (let ((edge
2204 (cond
2205 ((eq side 'left) 0)
2206 ((eq side 'top) 1)
2207 ((eq side 'right) 2)
2208 ((memq side '(bottom nil)) 3))))
2209 (= (nth edge (window-pixel-edges window))
2210 (nth edge (window-pixel-edges (frame-root-window window))))))
2212 (defun window-at-side-list (&optional frame side)
2213 "Return list of all windows on SIDE of FRAME.
2214 FRAME must be a live frame and defaults to the selected frame.
2215 SIDE can be any of the symbols `left', `top', `right' or
2216 `bottom'. The default value nil is handled like `bottom'."
2217 (setq frame (window-normalize-frame frame))
2218 (let (windows)
2219 (walk-window-tree
2220 (lambda (window)
2221 (when (window-at-side-p window side)
2222 (setq windows (cons window windows))))
2223 frame nil 'nomini)
2224 (nreverse windows)))
2226 (defun window--in-direction-2 (window posn &optional horizontal)
2227 "Support function for `window-in-direction'."
2228 (if horizontal
2229 (let ((top (window-pixel-top window)))
2230 (if (> top posn)
2231 (- top posn)
2232 (- posn top (window-pixel-height window))))
2233 (let ((left (window-pixel-left window)))
2234 (if (> left posn)
2235 (- left posn)
2236 (- posn left (window-pixel-width window))))))
2238 ;; Predecessors to the below have been devised by Julian Assange in
2239 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
2240 ;; Neither of these allow one to selectively ignore specific windows
2241 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
2242 ;; the movement.
2243 (defun window-in-direction (direction &optional window ignore sign wrap mini)
2244 "Return window in DIRECTION as seen from WINDOW.
2245 More precisely, return the nearest window in direction DIRECTION
2246 as seen from the position of `window-point' in window WINDOW.
2247 DIRECTION must be one of `above', `below', `left' or `right'.
2248 WINDOW must be a live window and defaults to the selected one.
2250 Do not return a window whose `no-other-window' parameter is
2251 non-nil. If the nearest window's `no-other-window' parameter is
2252 non-nil, try to find another window in the indicated direction.
2253 If, however, the optional argument IGNORE is non-nil, return that
2254 window even if its `no-other-window' parameter is non-nil.
2256 Optional argument SIGN a negative number means to use the right
2257 or bottom edge of WINDOW as reference position instead of
2258 `window-point'. SIGN a positive number means to use the left or
2259 top edge of WINDOW as reference position.
2261 Optional argument WRAP non-nil means to wrap DIRECTION around
2262 frame borders. This means to return for WINDOW at the top of the
2263 frame and DIRECTION `above' the minibuffer window if the frame
2264 has one, and a window at the bottom of the frame otherwise.
2266 Optional argument MINI nil means to return the minibuffer window
2267 if and only if it is currently active. MINI non-nil means to
2268 return the minibuffer window even when it's not active. However,
2269 if WRAP is non-nil, always act as if MINI were nil.
2271 Return nil if no suitable window can be found."
2272 (setq window (window-normalize-window window t))
2273 (unless (memq direction '(above below left right))
2274 (error "Wrong direction %s" direction))
2275 (let* ((frame (window-frame window))
2276 (hor (memq direction '(left right)))
2277 (first (if hor
2278 (window-pixel-left window)
2279 (window-pixel-top window)))
2280 (last (+ first (window-size window hor t)))
2281 ;; The column / row value of `posn-at-point' can be nil for the
2282 ;; mini-window, guard against that.
2283 (posn
2284 (cond
2285 ((and (numberp sign) (< sign 0))
2286 (if hor
2287 (1- (+ (window-pixel-top window) (window-pixel-height window)))
2288 (1- (+ (window-pixel-left window) (window-pixel-width window)))))
2289 ((and (numberp sign) (> sign 0))
2290 (if hor
2291 (window-pixel-top window)
2292 (window-pixel-left window)))
2293 ((let ((posn-cons (nth 2 (posn-at-point (window-point window) window))))
2294 (if hor
2295 (+ (or (cdr posn-cons) 1) (window-pixel-top window))
2296 (+ (or (car posn-cons) 1) (window-pixel-left window)))))))
2297 (best-edge
2298 (cond
2299 ((eq direction 'below) (frame-pixel-height frame))
2300 ((eq direction 'right) (frame-pixel-width frame))
2301 (t -1)))
2302 (best-edge-2 best-edge)
2303 (best-diff-2 (if hor (frame-pixel-height frame) (frame-pixel-width frame)))
2304 best best-2 best-diff-2-new)
2305 (walk-window-tree
2306 (lambda (w)
2307 (let* ((w-top (window-pixel-top w))
2308 (w-left (window-pixel-left w)))
2309 (cond
2310 ((or (eq window w)
2311 ;; Ignore ourselves.
2312 (and (window-parameter w 'no-other-window)
2313 ;; Ignore W unless IGNORE is non-nil.
2314 (not ignore))))
2315 (hor
2316 (cond
2317 ((and (<= w-top posn)
2318 (< posn (+ w-top (window-pixel-height w))))
2319 ;; W is to the left or right of WINDOW and covers POSN.
2320 (when (or (and (eq direction 'left)
2321 (or (and (<= w-left first) (> w-left best-edge))
2322 (and wrap
2323 (window-at-side-p window 'left)
2324 (window-at-side-p w 'right))))
2325 (and (eq direction 'right)
2326 (or (and (>= w-left last) (< w-left best-edge))
2327 (and wrap
2328 (window-at-side-p window 'right)
2329 (window-at-side-p w 'left)))))
2330 (setq best-edge w-left)
2331 (setq best w)))
2332 ((and (or (and (eq direction 'left)
2333 (<= (+ w-left (window-pixel-width w)) first))
2334 (and (eq direction 'right) (<= last w-left)))
2335 ;; W is to the left or right of WINDOW but does not
2336 ;; cover POSN.
2337 (setq best-diff-2-new
2338 (window--in-direction-2 w posn hor))
2339 (or (< best-diff-2-new best-diff-2)
2340 (and (= best-diff-2-new best-diff-2)
2341 (if (eq direction 'left)
2342 (> w-left best-edge-2)
2343 (< w-left best-edge-2)))))
2344 (setq best-edge-2 w-left)
2345 (setq best-diff-2 best-diff-2-new)
2346 (setq best-2 w))))
2347 ((and (<= w-left posn)
2348 (< posn (+ w-left (window-pixel-width w))))
2349 ;; W is above or below WINDOW and covers POSN.
2350 (when (or (and (eq direction 'above)
2351 (or (and (<= w-top first) (> w-top best-edge))
2352 (and wrap
2353 (window-at-side-p window 'top)
2354 (if (active-minibuffer-window)
2355 (minibuffer-window-active-p w)
2356 (window-at-side-p w 'bottom)))))
2357 (and (eq direction 'below)
2358 (or (and (>= w-top first) (< w-top best-edge))
2359 (and wrap
2360 (if (active-minibuffer-window)
2361 (minibuffer-window-active-p window)
2362 (window-at-side-p window 'bottom))
2363 (window-at-side-p w 'top)))))
2364 (setq best-edge w-top)
2365 (setq best w)))
2366 ((and (or (and (eq direction 'above)
2367 (<= (+ w-top (window-pixel-height w)) first))
2368 (and (eq direction 'below) (<= last w-top)))
2369 ;; W is above or below WINDOW but does not cover POSN.
2370 (setq best-diff-2-new
2371 (window--in-direction-2 w posn hor))
2372 (or (< best-diff-2-new best-diff-2)
2373 (and (= best-diff-2-new best-diff-2)
2374 (if (eq direction 'above)
2375 (> w-top best-edge-2)
2376 (< w-top best-edge-2)))))
2377 (setq best-edge-2 w-top)
2378 (setq best-diff-2 best-diff-2-new)
2379 (setq best-2 w)))))
2380 frame nil (and mini t))
2381 (or best best-2)))
2383 (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
2384 "Return a live window satisfying PREDICATE.
2385 More precisely, cycle through all windows calling the function
2386 PREDICATE on each one of them with the window as its sole
2387 argument. Return the first window for which PREDICATE returns
2388 non-nil. Windows are scanned starting with the window following
2389 the selected window. If no window satisfies PREDICATE, return
2390 DEFAULT.
2392 MINIBUF t means include the minibuffer window even if the
2393 minibuffer is not active. MINIBUF nil or omitted means include
2394 the minibuffer window only if the minibuffer is active. Any
2395 other value means do not include the minibuffer window even if
2396 the minibuffer is active.
2398 ALL-FRAMES nil or omitted means consider all windows on the selected
2399 frame, plus the minibuffer window if specified by the MINIBUF
2400 argument. If the minibuffer counts, consider all windows on all
2401 frames that share that minibuffer too. The following non-nil
2402 values of ALL-FRAMES have special meanings:
2404 - t means consider all windows on all existing frames.
2406 - `visible' means consider all windows on all visible frames on
2407 the current terminal.
2409 - 0 (the number zero) means consider all windows on all visible
2410 and iconified frames on the current terminal.
2412 - A frame means consider all windows on that frame only.
2414 Anything else means consider all windows on the selected frame
2415 and no others."
2416 (catch 'found
2417 (dolist (window (window-list-1
2418 (next-window nil minibuf all-frames)
2419 minibuf all-frames))
2420 (when (funcall predicate window)
2421 (throw 'found window)))
2422 default))
2424 (defalias 'some-window 'get-window-with-predicate)
2426 (defun get-lru-window (&optional all-frames dedicated not-selected)
2427 "Return the least recently used window on frames specified by ALL-FRAMES.
2428 Return a full-width window if possible. A minibuffer window is
2429 never a candidate. A dedicated window is never a candidate
2430 unless DEDICATED is non-nil, so if all windows are dedicated, the
2431 value is nil. Avoid returning the selected window if possible.
2432 Optional argument NOT-SELECTED non-nil means never return the
2433 selected window.
2435 The following non-nil values of the optional argument ALL-FRAMES
2436 have special meanings:
2438 - t means consider all windows on all existing frames.
2440 - `visible' means consider all windows on all visible frames on
2441 the current terminal.
2443 - 0 (the number zero) means consider all windows on all visible
2444 and iconified frames on the current terminal.
2446 - A frame means consider all windows on that frame only.
2448 Any other value of ALL-FRAMES means consider all windows on the
2449 selected frame and no others."
2450 (let (best-window best-time second-best-window second-best-time time)
2451 (dolist (window (window-list-1 nil 'nomini all-frames))
2452 (when (and (or dedicated (not (window-dedicated-p window)))
2453 (or (not not-selected) (not (eq window (selected-window)))))
2454 (setq time (window-use-time window))
2455 (if (or (eq window (selected-window))
2456 (not (window-full-width-p window)))
2457 (when (or (not second-best-time) (< time second-best-time))
2458 (setq second-best-time time)
2459 (setq second-best-window window))
2460 (when (or (not best-time) (< time best-time))
2461 (setq best-time time)
2462 (setq best-window window)))))
2463 (or best-window second-best-window)))
2465 (defun get-mru-window (&optional all-frames dedicated not-selected)
2466 "Return the most recently used window on frames specified by ALL-FRAMES.
2467 A minibuffer window is never a candidate. A dedicated window is
2468 never a candidate unless DEDICATED is non-nil, so if all windows
2469 are dedicated, the value is nil. Optional argument NOT-SELECTED
2470 non-nil means never return the selected window.
2472 The following non-nil values of the optional argument ALL-FRAMES
2473 have special meanings:
2475 - t means consider all windows on all existing frames.
2477 - `visible' means consider all windows on all visible frames on
2478 the current terminal.
2480 - 0 (the number zero) means consider all windows on all visible
2481 and iconified frames on the current terminal.
2483 - A frame means consider all windows on that frame only.
2485 Any other value of ALL-FRAMES means consider all windows on the
2486 selected frame and no others."
2487 (let (best-window best-time time)
2488 (dolist (window (window-list-1 nil 'nomini all-frames))
2489 (setq time (window-use-time window))
2490 (when (and (or dedicated (not (window-dedicated-p window)))
2491 (or (not not-selected) (not (eq window (selected-window))))
2492 (or (not best-time) (> time best-time)))
2493 (setq best-time time)
2494 (setq best-window window)))
2495 best-window))
2497 (defun get-largest-window (&optional all-frames dedicated not-selected)
2498 "Return the largest window on frames specified by ALL-FRAMES.
2499 A minibuffer window is never a candidate. A dedicated window is
2500 never a candidate unless DEDICATED is non-nil, so if all windows
2501 are dedicated, the value is nil. Optional argument NOT-SELECTED
2502 non-nil means never return the selected window.
2504 The following non-nil values of the optional argument ALL-FRAMES
2505 have special meanings:
2507 - t means consider all windows on all existing frames.
2509 - `visible' means consider all windows on all visible frames on
2510 the current terminal.
2512 - 0 (the number zero) means consider all windows on all visible
2513 and iconified frames on the current terminal.
2515 - A frame means consider all windows on that frame only.
2517 Any other value of ALL-FRAMES means consider all windows on the
2518 selected frame and no others."
2519 (let ((best-size 0)
2520 best-window size)
2521 (dolist (window (window-list-1 nil 'nomini all-frames))
2522 (when (and (or dedicated (not (window-dedicated-p window)))
2523 (or (not not-selected) (not (eq window (selected-window)))))
2524 (setq size (* (window-pixel-height window)
2525 (window-pixel-width window)))
2526 (when (> size best-size)
2527 (setq best-size size)
2528 (setq best-window window))))
2529 best-window))
2531 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
2532 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2533 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2534 and defaults to the current buffer. If the selected window displays
2535 BUFFER-OR-NAME, it will be the first in the resulting list.
2537 MINIBUF t means include the minibuffer window even if the
2538 minibuffer is not active. MINIBUF nil or omitted means include
2539 the minibuffer window only if the minibuffer is active. Any
2540 other value means do not include the minibuffer window even if
2541 the minibuffer is active.
2543 ALL-FRAMES nil or omitted means consider all windows on the
2544 selected frame, plus the minibuffer window if specified by the
2545 MINIBUF argument. If the minibuffer counts, consider all windows
2546 on all frames that share that minibuffer too. The following
2547 non-nil values of ALL-FRAMES have special meanings:
2549 - t means consider all windows on all existing frames.
2551 - `visible' means consider all windows on all visible frames on
2552 the current terminal.
2554 - 0 (the number zero) means consider all windows on all visible
2555 and iconified frames on the current terminal.
2557 - A frame means consider all windows on that frame only.
2559 Anything else means consider all windows on the selected frame
2560 and no others."
2561 (let ((buffer (window-normalize-buffer buffer-or-name))
2562 windows)
2563 (dolist (window (window-list-1 (selected-window) minibuf all-frames))
2564 (when (eq (window-buffer window) buffer)
2565 (setq windows (cons window windows))))
2566 (nreverse windows)))
2568 (defun minibuffer-window-active-p (window)
2569 "Return t if WINDOW is the currently active minibuffer window."
2570 (eq window (active-minibuffer-window)))
2572 (defun count-windows (&optional minibuf)
2573 "Return the number of live windows on the selected frame.
2574 The optional argument MINIBUF specifies whether the minibuffer
2575 window shall be counted. See `walk-windows' for the precise
2576 meaning of this argument."
2577 (length (window-list-1 nil minibuf)))
2579 ;;; Resizing windows.
2580 (defun window--size-to-pixel (window size &optional horizontal pixelwise round-maybe)
2581 "For WINDOW convert SIZE lines to pixels.
2582 SIZE is supposed to specify a height of WINDOW in terms of text
2583 lines. The return value is the number of pixels specifying that
2584 height.
2586 WINDOW must be a valid window. Optional argument HORIZONTAL
2587 non-nil means convert SIZE columns to pixels.
2589 Optional argument PIXELWISE non-nil means SIZE already specifies
2590 pixels but may have to be adjusted to a multiple of the character
2591 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2592 means round to the nearest multiple of the character size of
2593 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2594 (setq window (window-normalize-window window))
2595 (let ((char-size (frame-char-size window horizontal)))
2596 (if pixelwise
2597 (if (and round-maybe (not window-resize-pixelwise))
2598 (* (round size char-size) char-size)
2599 size)
2600 (* size char-size))))
2602 (defun window--pixel-to-total-1 (window horizontal char-size)
2603 "Subroutine of `window--pixel-to-total'."
2604 (let ((child (window-child window)))
2605 (if (window-combination-p window horizontal)
2606 ;; In an iso-combination distribute sizes proportionally.
2607 (let ((remainder (window-new-total window))
2608 size best-child rem best-rem)
2609 ;; Initialize total sizes to each child's floor.
2610 (while child
2611 (setq size (max (/ (window-size child horizontal t) char-size) 1))
2612 (set-window-new-total child size)
2613 (setq remainder (- remainder size))
2614 (setq child (window-next-sibling child)))
2615 ;; Distribute remainder.
2616 (while (> remainder 0)
2617 (setq child (window-last-child window))
2618 (setq best-child nil)
2619 (setq best-rem 0)
2620 (while child
2621 (when (and (<= (window-new-total child)
2622 (/ (window-size child horizontal t) char-size))
2623 (> (setq rem (% (window-size child horizontal t)
2624 char-size))
2625 best-rem))
2626 (setq best-child child)
2627 (setq best-rem rem))
2628 (setq child (window-prev-sibling child)))
2629 ;; We MUST have a best-child here.
2630 (set-window-new-total best-child 1 t)
2631 (setq remainder (1- remainder)))
2632 ;; Recurse.
2633 (setq child (window-child window))
2634 (while child
2635 (window--pixel-to-total-1 child horizontal char-size)
2636 (setq child (window-next-sibling child))))
2637 ;; In an ortho-combination assign new sizes directly.
2638 (let ((size (window-new-total window)))
2639 (while child
2640 (set-window-new-total child size)
2641 (window--pixel-to-total-1 child horizontal char-size)
2642 (setq child (window-next-sibling child)))))))
2644 (defun window--pixel-to-total (&optional frame horizontal)
2645 "On FRAME assign new total window heights from pixel heights.
2646 FRAME must be a live frame and defaults to the selected frame.
2648 Optional argument HORIZONTAL non-nil means assign new total
2649 window widths from pixel widths."
2650 (setq frame (window-normalize-frame frame))
2651 (let* ((char-size (frame-char-size frame horizontal))
2652 (root (frame-root-window frame))
2653 (root-size (window-size root horizontal t))
2654 ;; We have to care about the minibuffer window only if it
2655 ;; appears together with the root window on this frame.
2656 (mini (let ((mini (minibuffer-window frame)))
2657 (and (eq (window-frame mini) frame)
2658 (not (eq mini root)) mini)))
2659 (mini-size (and mini (window-size mini horizontal t))))
2660 ;; We round the line/column sizes of windows here to the nearest
2661 ;; integer. In some cases this can make windows appear _larger_
2662 ;; than the containing frame (line/column-wise) because the latter's
2663 ;; sizes are not (yet) rounded. We might eventually fix that.
2664 (if (and mini (not horizontal))
2665 (let (lines)
2666 (set-window-new-total root (max (/ root-size char-size) 1))
2667 (set-window-new-total mini (max (/ mini-size char-size) 1))
2668 (setq lines (- (round (+ root-size mini-size) char-size)
2669 (+ (window-new-total root) (window-new-total mini))))
2670 (while (> lines 0)
2671 (if (>= (% root-size (window-new-total root))
2672 (% mini-size (window-new-total mini)))
2673 (set-window-new-total root 1 t)
2674 (set-window-new-total mini 1 t))
2675 (setq lines (1- lines))))
2676 (set-window-new-total root (round root-size char-size))
2677 (when mini
2678 ;; This is taken in the horizontal case only.
2679 (set-window-new-total mini (round mini-size char-size))))
2680 (unless (window-buffer root)
2681 (window--pixel-to-total-1 root horizontal char-size))
2682 ;; Apply the new sizes.
2683 (window-resize-apply-total frame horizontal)))
2685 (defun window--resize-reset (&optional frame horizontal)
2686 "Reset resize values for all windows on FRAME.
2687 FRAME defaults to the selected frame.
2689 This function stores the current value of `window-size' applied
2690 with argument HORIZONTAL in the new total size of all windows on
2691 FRAME. It also resets the new normal size of each of these
2692 windows."
2693 (window--resize-reset-1
2694 (frame-root-window (window-normalize-frame frame)) horizontal))
2696 (defun window--resize-reset-1 (window horizontal)
2697 "Internal function of `window--resize-reset'."
2698 ;; Register old size in the new total size.
2699 (set-window-new-pixel window (window-size window horizontal t))
2700 (set-window-new-total window (window-size window horizontal))
2701 ;; Reset new normal size.
2702 (set-window-new-normal window)
2703 (when (window-child window)
2704 (window--resize-reset-1 (window-child window) horizontal))
2705 (when (window-right window)
2706 (window--resize-reset-1 (window-right window) horizontal)))
2708 (defun window--resize-mini-window (window delta)
2709 "Resize minibuffer window WINDOW by DELTA pixels.
2710 If WINDOW cannot be resized by DELTA pixels make it as large (or
2711 as small) as possible, but don't signal an error."
2712 (when (window-minibuffer-p window)
2713 (let* ((frame (window-frame window))
2714 (root (frame-root-window frame))
2715 (height (window-pixel-height window))
2716 (min-delta
2717 (- (window-pixel-height root)
2718 (window-min-size root nil nil t))))
2719 ;; Sanitize DELTA.
2720 (cond
2721 ((<= (+ height delta) 0)
2722 (setq delta (- (frame-char-height (window-frame window)) height)))
2723 ((> delta min-delta)
2724 (setq delta min-delta)))
2726 (unless (zerop delta)
2727 ;; Resize now.
2728 (window--resize-reset frame)
2729 ;; Ideally we should be able to resize just the last child of root
2730 ;; here. See the comment in `resize-root-window-vertically' for
2731 ;; why we do not do that.
2732 (window--resize-this-window root (- delta) nil nil t)
2733 (set-window-new-pixel window (+ height delta))
2734 ;; The following routine catches the case where we want to resize
2735 ;; a minibuffer-only frame.
2736 (when (resize-mini-window-internal window)
2737 (window--pixel-to-total frame)
2738 (run-window-configuration-change-hook frame))))))
2740 (defun window--resize-apply-p (frame &optional horizontal)
2741 "Return t when a window on FRAME shall be resized vertically.
2742 Optional argument HORIZONTAL non-nil means return t when a window
2743 shall be resized horizontally."
2744 (catch 'apply
2745 (walk-window-tree
2746 (lambda (window)
2747 (unless (= (window-new-pixel window)
2748 (window-size window horizontal t))
2749 (throw 'apply t)))
2750 frame t)
2751 nil))
2753 (defun window-resize (window delta &optional horizontal ignore pixelwise)
2754 "Resize WINDOW vertically by DELTA lines.
2755 WINDOW can be an arbitrary window and defaults to the selected
2756 one. An attempt to resize the root window of a frame will raise
2757 an error though.
2759 DELTA a positive number means WINDOW shall be enlarged by DELTA
2760 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2761 lines.
2763 Optional argument HORIZONTAL non-nil means resize WINDOW
2764 horizontally by DELTA columns. In this case a positive DELTA
2765 means enlarge WINDOW by DELTA columns. DELTA negative means
2766 WINDOW shall be shrunk by -DELTA columns.
2768 Optional argument IGNORE, if non-nil, means to ignore restraints
2769 induced by fixed size windows or the values of the variables
2770 `window-min-height' and `window-min-width'. The following values
2771 have special meanings: `safe' means that in addition live windows
2772 are allowed to get as small as `window-safe-min-height' lines and
2773 `window-safe-min-width' columns. `preserved' means to ignore
2774 only restrictions induced by `window-preserve-size'. If IGNORE
2775 is a window, then ignore restrictions for that window only.
2777 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2778 pixels.
2780 This function resizes other windows proportionally and never
2781 deletes any windows. If you want to move only the low (right)
2782 edge of WINDOW consider using `adjust-window-trailing-edge'
2783 instead."
2784 (setq window (window-normalize-window window))
2785 (let* ((frame (window-frame window))
2786 (minibuffer-window (minibuffer-window frame))
2787 sibling)
2788 (setq delta (window--size-to-pixel
2789 window delta horizontal pixelwise t))
2790 (cond
2791 ((eq window (frame-root-window frame))
2792 (error "Cannot resize the root window of a frame"))
2793 ((window-minibuffer-p window)
2794 (if horizontal
2795 (error "Cannot resize minibuffer window horizontally")
2796 (window--resize-mini-window window delta)))
2797 ((and (not horizontal)
2798 (window-full-height-p window)
2799 (eq (window-frame minibuffer-window) frame)
2800 (or (not resize-mini-windows)
2801 (eq minibuffer-window (active-minibuffer-window))))
2802 ;; If WINDOW is full height and either `resize-mini-windows' is
2803 ;; nil or the minibuffer window is active, resize the minibuffer
2804 ;; window.
2805 (window--resize-mini-window minibuffer-window (- delta)))
2806 ((or (window--resizable-p
2807 window delta horizontal ignore nil nil nil t)
2808 (and (not ignore)
2809 (setq ignore 'preserved)
2810 (window--resizable-p
2811 window delta horizontal ignore nil nil nil t)))
2812 (window--resize-reset frame horizontal)
2813 (window--resize-this-window window delta horizontal ignore t)
2814 (if (and (not (eq window-combination-resize t))
2815 (window-combined-p window horizontal)
2816 (setq sibling (or (window-right window) (window-left window)))
2817 (window-sizable-p
2818 sibling (- delta) horizontal ignore t))
2819 ;; If window-combination-resize is nil, WINDOW is part of an
2820 ;; iso-combination, and WINDOW's neighboring right or left
2821 ;; sibling can be resized as requested, resize that sibling.
2822 (let ((normal-delta
2823 (/ (float delta)
2824 (window-size (window-parent window) horizontal t))))
2825 (window--resize-this-window sibling (- delta) horizontal nil t)
2826 (set-window-new-normal
2827 window (+ (window-normal-size window horizontal)
2828 normal-delta))
2829 (set-window-new-normal
2830 sibling (- (window-normal-size sibling horizontal)
2831 normal-delta)))
2832 ;; Otherwise, resize all other windows in the same combination.
2833 (window--resize-siblings window delta horizontal ignore))
2834 (when (window--resize-apply-p frame horizontal)
2835 (if (window-resize-apply frame horizontal)
2836 (progn
2837 (window--pixel-to-total frame horizontal)
2838 (run-window-configuration-change-hook frame))
2839 (error "Failed to apply resizing %s" window))))
2841 (error "Cannot resize window %s" window)))))
2843 (defun window-resize-no-error (window delta &optional horizontal ignore pixelwise)
2844 "Resize WINDOW vertically if it is resizable by DELTA lines.
2845 This function is like `window-resize' but does not signal an
2846 error when WINDOW cannot be resized. For the meaning of the
2847 optional arguments see the documentation of `window-resize'."
2848 (when (window--resizable-p
2849 window delta horizontal ignore nil nil nil pixelwise)
2850 (window-resize window delta horizontal ignore pixelwise)))
2852 (defun window--resize-child-windows-skip-p (window)
2853 "Return non-nil if WINDOW shall be skipped by resizing routines."
2854 (memq (window-new-normal window) '(ignore stuck skip)))
2856 (defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
2857 "Recursively set new normal height of child windows of window PARENT.
2858 HORIZONTAL non-nil means set the new normal width of these
2859 windows. WINDOW specifies a child window of PARENT that has been
2860 resized by THIS-DELTA lines (columns).
2862 Optional argument TRAIL either `before' or `after' means set values
2863 only for windows before or after WINDOW. Optional argument
2864 OTHER-DELTA, a number, specifies that this many lines (columns)
2865 have been obtained from (or returned to) an ancestor window of
2866 PARENT in order to resize WINDOW."
2867 (let* ((delta-normal
2868 (if (and (= (- this-delta)
2869 (window-size window horizontal t))
2870 (zerop other-delta))
2871 ;; When WINDOW gets deleted and we can return its entire
2872 ;; space to its siblings, use WINDOW's normal size as the
2873 ;; normal delta.
2874 (- (window-normal-size window horizontal))
2875 ;; In any other case calculate the normal delta from the
2876 ;; relation of THIS-DELTA to the total size of PARENT.
2877 (/ (float this-delta)
2878 (window-size parent horizontal t))))
2879 (sub (window-child parent))
2880 (parent-normal 0.0)
2881 (skip (eq trail 'after)))
2883 ;; Set parent-normal to the sum of the normal sizes of all child
2884 ;; windows of PARENT that shall be resized, excluding only WINDOW
2885 ;; and any windows specified by the optional TRAIL argument.
2886 (while sub
2887 (cond
2888 ((eq sub window)
2889 (setq skip (eq trail 'before)))
2890 (skip)
2892 (setq parent-normal
2893 (+ parent-normal (window-normal-size sub horizontal)))))
2894 (setq sub (window-right sub)))
2896 ;; Set the new normal size of all child windows of PARENT from what
2897 ;; they should have contributed for recovering THIS-DELTA lines
2898 ;; (columns).
2899 (setq sub (window-child parent))
2900 (setq skip (eq trail 'after))
2901 (while sub
2902 (cond
2903 ((eq sub window)
2904 (setq skip (eq trail 'before)))
2905 (skip)
2907 (let ((old-normal (window-normal-size sub horizontal)))
2908 (set-window-new-normal
2909 sub (min 1.0 ; Don't get larger than 1.
2910 (max (- old-normal
2911 (* (/ old-normal parent-normal)
2912 delta-normal))
2913 ;; Don't drop below 0.
2914 0.0))))))
2915 (setq sub (window-right sub)))
2917 (when (numberp other-delta)
2918 ;; Set the new normal size of windows from what they should have
2919 ;; contributed for recovering OTHER-DELTA lines (columns).
2920 (setq delta-normal (/ (float (window-size parent horizontal t))
2921 (+ (window-size parent horizontal t)
2922 other-delta)))
2923 (setq sub (window-child parent))
2924 (setq skip (eq trail 'after))
2925 (while sub
2926 (cond
2927 ((eq sub window)
2928 (setq skip (eq trail 'before)))
2929 (skip)
2931 (set-window-new-normal
2932 sub (min 1.0 ; Don't get larger than 1.
2933 (max (* (window-new-normal sub) delta-normal)
2934 ;; Don't drop below 0.
2935 0.0)))))
2936 (setq sub (window-right sub))))
2938 ;; Set the new normal size of WINDOW to what is left by the sum of
2939 ;; the normal sizes of its siblings.
2940 (set-window-new-normal
2941 window
2942 (let ((sum 0))
2943 (setq sub (window-child parent))
2944 (while sub
2945 (cond
2946 ((eq sub window))
2947 ((not (numberp (window-new-normal sub)))
2948 (setq sum (+ sum (window-normal-size sub horizontal))))
2950 (setq sum (+ sum (window-new-normal sub)))))
2951 (setq sub (window-right sub)))
2952 ;; Don't get larger than 1 or smaller than 0.
2953 (min 1.0 (max (- 1.0 sum) 0.0))))))
2955 (defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge char-size)
2956 "Resize child windows of window PARENT vertically by DELTA pixels.
2957 PARENT must be a vertically combined internal window.
2959 Optional argument HORIZONTAL non-nil means resize child windows
2960 of PARENT horizontally by DELTA pixels. In this case PARENT must
2961 be a horizontally combined internal window.
2963 WINDOW, if specified, must denote a child window of PARENT that
2964 is resized by DELTA pixels.
2966 The optional argument IGNORE has the same meaning as for
2967 `window-resizable'.
2969 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2970 of windows that shall be resized. If TRAIL equals `before',
2971 resize only windows on the left or above EDGE. If TRAIL equals
2972 `after', resize only windows on the right or below EDGE. Also,
2973 preferably only resize windows adjacent to EDGE.
2975 If the optional argument CHAR-SIZE is a positive integer, it specifies
2976 the number of pixels by which windows are incrementally resized.
2977 If CHAR-SIZE is nil, this means to use the value of
2978 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2980 Return the symbol `normalized' if new normal sizes have been
2981 already set by this routine."
2982 (let* ((first (window-child parent))
2983 (last (window-last-child parent))
2984 (parent-total (+ (window-size parent horizontal t)
2985 delta))
2986 (char-size (or char-size
2987 (and window-resize-pixelwise 1)
2988 (frame-char-size window horizontal)))
2989 sub best-window best-value best-delta)
2991 (if (and edge (memq trail '(before after))
2992 (progn
2993 (setq sub first)
2994 (while (and (window-right sub)
2995 (or (and (eq trail 'before)
2996 (not (window--resize-child-windows-skip-p
2997 (window-right sub))))
2998 (and (eq trail 'after)
2999 (window--resize-child-windows-skip-p sub))))
3000 (setq sub (window-right sub)))
3001 sub)
3002 (if horizontal
3003 (if (eq trail 'before)
3004 (= (+ (window-pixel-left sub) (window-pixel-width sub))
3005 edge)
3006 (= (window-pixel-left sub) edge))
3007 (if (eq trail 'before)
3008 (= (+ (window-pixel-top sub) (window-pixel-height sub))
3009 edge)
3010 (= (window-pixel-top sub) edge)))
3011 (window-sizable-p sub delta horizontal ignore t))
3012 ;; Resize only windows adjacent to EDGE.
3013 (progn
3014 (window--resize-this-window
3015 sub delta horizontal ignore t trail edge)
3016 (if (and window (eq (window-parent sub) parent))
3017 (progn
3018 ;; Assign new normal sizes.
3019 (set-window-new-normal
3020 sub (/ (float (window-new-pixel sub)) parent-total))
3021 (set-window-new-normal
3022 window (- (window-normal-size window horizontal)
3023 (- (window-new-normal sub)
3024 (window-normal-size sub horizontal)))))
3025 (window--resize-child-windows-normal
3026 parent horizontal sub 0 trail delta))
3027 ;; Return 'normalized to notify `window--resize-siblings' that
3028 ;; normal sizes have been already set.
3029 'normalized)
3030 ;; Resize all windows proportionally.
3031 (setq sub last)
3032 (while sub
3033 (cond
3034 ((or (window--resize-child-windows-skip-p sub)
3035 ;; Ignore windows to skip and fixed-size child windows -
3036 ;; in the latter case make it a window to skip.
3037 (and (not ignore)
3038 (window-size-fixed-p sub horizontal ignore)
3039 (set-window-new-normal sub 'ignore))))
3040 ((< delta 0)
3041 ;; When shrinking store the number of lines/cols we can get
3042 ;; from this window here together with the total/normal size
3043 ;; factor.
3044 (set-window-new-normal
3046 (cons
3047 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
3048 (window-min-delta sub horizontal ignore trail t nil t)
3049 (- (/ (float (window-size sub horizontal t))
3050 parent-total)
3051 (window-normal-size sub horizontal)))))
3052 ((> delta 0)
3053 ;; When enlarging store the total/normal size factor only
3054 (set-window-new-normal
3056 (- (/ (float (window-size sub horizontal t))
3057 parent-total)
3058 (window-normal-size sub horizontal)))))
3060 (setq sub (window-left sub)))
3062 (cond
3063 ((< delta 0)
3064 ;; Shrink windows by delta.
3065 (setq best-window t)
3066 (while (and best-window (not (zerop delta)))
3067 (setq sub last)
3068 (setq best-window nil)
3069 (setq best-value most-negative-fixnum)
3070 (while sub
3071 (when (and (consp (window-new-normal sub))
3072 (not (<= (car (window-new-normal sub)) 0))
3073 (> (cdr (window-new-normal sub)) best-value))
3074 (setq best-window sub)
3075 (setq best-value (cdr (window-new-normal sub))))
3077 (setq sub (window-left sub)))
3079 (when best-window
3080 (setq best-delta (min (car (window-new-normal best-window))
3081 char-size (- delta)))
3082 (setq delta (+ delta best-delta))
3083 (set-window-new-pixel best-window (- best-delta) t)
3084 (set-window-new-normal
3085 best-window
3086 (if (= (car (window-new-normal best-window)) best-delta)
3087 'skip ; We can't shrink best-window any further.
3088 (cons (- (car (window-new-normal best-window)) best-delta)
3089 (- (/ (float (window-new-pixel best-window))
3090 parent-total)
3091 (window-normal-size best-window horizontal))))))))
3092 ((> delta 0)
3093 ;; Enlarge windows by delta.
3094 (setq best-window t)
3095 (while (and best-window (not (zerop delta)))
3096 (setq sub last)
3097 (setq best-window nil)
3098 (setq best-value most-positive-fixnum)
3099 (while sub
3100 (when (and (numberp (window-new-normal sub))
3101 (< (window-new-normal sub) best-value))
3102 (setq best-window sub)
3103 (setq best-value (window-new-normal sub)))
3105 (setq sub (window-left sub)))
3107 (when best-window
3108 (setq best-delta (min delta char-size))
3109 (setq delta (- delta best-delta))
3110 (set-window-new-pixel best-window best-delta t)
3111 (set-window-new-normal
3112 best-window
3113 (- (/ (float (window-new-pixel best-window))
3114 parent-total)
3115 (window-normal-size best-window horizontal)))))))
3117 (when best-window
3118 (setq sub last)
3119 (while sub
3120 (when (or (consp (window-new-normal sub))
3121 (numberp (window-new-normal sub)))
3122 ;; Reset new normal size fields so `window-resize-apply'
3123 ;; won't use them to apply new sizes.
3124 (set-window-new-normal sub))
3126 (unless (eq (window-new-normal sub) 'ignore)
3127 ;; Resize this window's child windows (back-engineering
3128 ;; delta from sub's old and new total sizes).
3129 (let ((delta (- (window-new-pixel sub)
3130 (window-size sub horizontal t))))
3131 (unless (and (zerop delta) (not trail))
3132 ;; For the TRAIL non-nil case we have to resize SUB
3133 ;; recursively even if it's size does not change.
3134 (window--resize-this-window
3135 sub delta horizontal ignore nil trail edge))))
3136 (setq sub (window-left sub)))))))
3138 (defun window--resize-siblings (window delta &optional horizontal ignore trail edge char-size)
3139 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
3140 Optional argument HORIZONTAL non-nil means resize other windows
3141 when WINDOW is resized horizontally by DELTA pixels. WINDOW
3142 itself is not resized by this function.
3144 The optional argument IGNORE has the same meaning as for
3145 `window-resizable'.
3147 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3148 of windows that shall be resized. If TRAIL equals `before',
3149 resize only windows on the left or above EDGE. If TRAIL equals
3150 `after', resize only windows on the right or below EDGE. Also,
3151 preferably only resize windows adjacent to EDGE."
3152 (when (window-parent window)
3153 (let* ((parent (window-parent window))
3154 (sub (window-child parent)))
3155 (if (window-combined-p sub horizontal)
3156 ;; In an iso-combination try to extract DELTA from WINDOW's
3157 ;; siblings.
3158 (let ((skip (eq trail 'after))
3159 this-delta other-delta)
3160 ;; Decide which windows shall be left alone.
3161 (while sub
3162 (cond
3163 ((eq sub window)
3164 ;; Make sure WINDOW is left alone when
3165 ;; resizing its siblings.
3166 (set-window-new-normal sub 'ignore)
3167 (setq skip (eq trail 'before)))
3168 (skip
3169 ;; Make sure this sibling is left alone when
3170 ;; resizing its siblings.
3171 (set-window-new-normal sub 'ignore))
3172 ((not (window-size-fixed-p sub horizontal ignore))
3173 ;; Set this-delta to t to signal that we found a sibling
3174 ;; of WINDOW whose size is not fixed.
3175 (setq this-delta t)))
3177 (setq sub (window-right sub)))
3179 ;; Set this-delta to what we can get from WINDOW's siblings.
3180 (if (= (- delta) (window-size window horizontal t))
3181 ;; A deletion, presumably. We must handle this case
3182 ;; specially since `window--resizable' can't be used.
3183 (if this-delta
3184 ;; There's at least one resizable sibling we can
3185 ;; give WINDOW's size to.
3186 (setq this-delta delta)
3187 ;; No resizable sibling exists.
3188 (setq this-delta 0))
3189 ;; Any other form of resizing.
3190 (setq this-delta
3191 (window--resizable
3192 window delta horizontal ignore trail t nil t)))
3194 ;; Set other-delta to what we still have to get from
3195 ;; ancestor windows of parent.
3196 (setq other-delta (- delta this-delta))
3197 (unless (zerop other-delta)
3198 ;; Unless we got everything from WINDOW's siblings, PARENT
3199 ;; must be resized by other-delta lines or columns.
3200 (set-window-new-pixel parent other-delta 'add))
3202 (if (zerop this-delta)
3203 ;; We haven't got anything from WINDOW's siblings but we
3204 ;; must update the normal sizes to respect other-delta.
3205 (window--resize-child-windows-normal
3206 parent horizontal window this-delta trail other-delta)
3207 ;; We did get something from WINDOW's siblings which means
3208 ;; we have to resize their child windows.
3209 (unless (eq (window--resize-child-windows
3210 parent (- this-delta) horizontal
3211 window ignore trail edge char-size)
3212 ;; If `window--resize-child-windows' returns
3213 ;; 'normalized, this means it has set the
3214 ;; normal sizes already.
3215 'normalized)
3216 ;; Set the normal sizes.
3217 (window--resize-child-windows-normal
3218 parent horizontal window this-delta trail other-delta))
3219 ;; Set DELTA to what we still have to get from ancestor
3220 ;; windows.
3221 (setq delta other-delta)))
3223 ;; In an ortho-combination all siblings of WINDOW must be
3224 ;; resized by DELTA.
3225 (set-window-new-pixel parent delta 'add)
3226 (while sub
3227 (unless (eq sub window)
3228 (window--resize-this-window
3229 sub delta horizontal ignore t))
3230 (setq sub (window-right sub))))
3232 (unless (zerop delta)
3233 ;; "Go up."
3234 (window--resize-siblings
3235 parent delta horizontal ignore trail edge char-size)))))
3237 (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge char-size)
3238 "Resize WINDOW vertically by DELTA pixels.
3239 Optional argument HORIZONTAL non-nil means resize WINDOW
3240 horizontally by DELTA pixels.
3242 The optional argument IGNORE has the same meaning as for
3243 `window-resizable'. Optional argument ADD non-nil means add
3244 DELTA to the new total size of WINDOW.
3246 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3247 of windows that shall be resized. If TRAIL equals `before',
3248 resize only windows on the left or above EDGE. If TRAIL equals
3249 `after', resize only windows on the right or below EDGE. Also,
3250 preferably only resize windows adjacent to EDGE.
3252 If the optional argument CHAR-SIZE is a positive integer, it specifies
3253 the number of pixels by which windows are incrementally resized.
3254 If CHAR-SIZE is nil, this means to use the value of
3255 `frame-char-height' or `frame-char-width' of WINDOW's frame.
3257 This function recursively resizes WINDOW's child windows to fit the
3258 new size. Make sure that WINDOW is `window--resizable' before
3259 calling this function. Note that this function does not resize
3260 siblings of WINDOW or WINDOW's parent window. You have to
3261 eventually call `window-resize-apply' in order to make resizing
3262 actually take effect."
3263 (when add
3264 ;; Add DELTA to the new total size of WINDOW.
3265 (set-window-new-pixel window delta t))
3267 (let ((sub (window-child window)))
3268 (cond
3269 ((not sub))
3270 ((window-combined-p sub horizontal)
3271 ;; In an iso-combination resize child windows according to their
3272 ;; normal sizes.
3273 (window--resize-child-windows
3274 window delta horizontal nil ignore trail edge char-size))
3275 ;; In an ortho-combination resize each child window by DELTA.
3277 (while sub
3278 (window--resize-this-window
3279 sub delta horizontal ignore t trail edge char-size)
3280 (setq sub (window-right sub)))))))
3282 (defun window--resize-root-window (window delta horizontal ignore pixelwise)
3283 "Resize root window WINDOW vertically by DELTA lines.
3284 HORIZONTAL non-nil means resize root window WINDOW horizontally
3285 by DELTA columns.
3287 IGNORE non-nil means ignore any restrictions imposed by fixed
3288 size windows, `window-min-height' or `window-min-width' settings.
3290 This function is only called by the frame resizing routines. It
3291 resizes windows proportionally and never deletes any windows."
3292 (when (and (windowp window) (numberp delta))
3293 (let ((pixel-delta
3294 (if pixelwise
3295 delta
3296 (window--size-to-pixel window delta horizontal))))
3297 (when (window-sizable-p window pixel-delta horizontal ignore t)
3298 (window--resize-reset (window-frame window) horizontal)
3299 (window--resize-this-window
3300 window pixel-delta horizontal ignore t)))))
3302 (defun window--resize-root-window-vertically (window delta pixelwise)
3303 "Resize root window WINDOW vertically by DELTA lines.
3304 If DELTA is less than zero and we can't shrink WINDOW by DELTA
3305 lines, shrink it as much as possible. If DELTA is greater than
3306 zero, this function can resize fixed-size windows in order to
3307 recover the necessary lines. Return the number of lines that
3308 were recovered.
3310 Third argument PIXELWISE non-nil means to interpret DELTA as
3311 pixels and return the number of pixels that were recovered.
3313 This function is called by the minibuffer window resizing
3314 routines."
3315 (let* ((frame (window-frame window))
3316 (pixel-delta
3317 (cond
3318 (pixelwise
3319 delta)
3320 ((numberp delta)
3321 (* (frame-char-height frame) delta))
3322 (t 0)))
3323 ignore)
3324 (cond
3325 ((zerop pixel-delta))
3326 ((< pixel-delta 0)
3327 (setq pixel-delta (window-sizable window pixel-delta nil nil pixelwise))
3328 (window--resize-reset frame)
3329 ;; When shrinking the root window, emulate an edge drag in order
3330 ;; to not resize other windows if we can avoid it (Bug#12419).
3331 (window--resize-this-window
3332 window pixel-delta nil ignore t 'before
3333 (+ (window-pixel-top window) (window-pixel-height window)))
3334 ;; Don't record new normal sizes to make sure that shrinking back
3335 ;; proportionally works as intended.
3336 (walk-window-tree
3337 (lambda (window) (set-window-new-normal window 'ignore)) frame t))
3338 ((> pixel-delta 0)
3339 (window--resize-reset frame)
3340 (unless (window-sizable window pixel-delta nil nil pixelwise)
3341 (setq ignore t))
3342 ;; When growing the root window, resize proportionally. This
3343 ;; should give windows back their original sizes (hopefully).
3344 (window--resize-this-window
3345 window pixel-delta nil ignore t)))
3346 ;; Return the possibly adjusted DELTA.
3347 (if pixelwise
3348 pixel-delta
3349 (/ pixel-delta (frame-char-height frame)))))
3351 (defun window--sanitize-window-sizes (horizontal)
3352 "Assert that all windows on selected frame are large enough.
3353 If necessary and possible, make sure that every window on frame
3354 FRAME has its minimum height. Optional argument HORIZONTAL
3355 non-nil means to make sure that every window on frame FRAME has
3356 its minimum width. The minimum height/width of a window is the
3357 respective value returned by `window-min-size' for that window.
3359 Return t if all windows were resized appropriately. Return nil
3360 if at least one window could not be resized as requested, which
3361 may happen when the FRAME is not large enough to accommodate it."
3362 (let ((value t))
3363 (walk-window-tree
3364 (lambda (window)
3365 (let ((delta (- (window-min-size window horizontal nil t)
3366 (window-size window horizontal t))))
3367 (when (> delta 0)
3368 (if (window-resizable-p window delta horizontal nil t)
3369 (window-resize window delta horizontal nil t)
3370 (setq value nil))))))
3371 value))
3373 (defun adjust-window-trailing-edge (window delta &optional horizontal pixelwise)
3374 "Move WINDOW's bottom edge by DELTA lines.
3375 Optional argument HORIZONTAL non-nil means move WINDOW's right
3376 edge by DELTA columns. WINDOW must be a valid window and
3377 defaults to the selected one.
3379 Optional argument PIXELWISE non-nil means interpret DELTA as
3380 number of pixels.
3382 If DELTA is greater than zero, move the edge downwards or to the
3383 right. If DELTA is less than zero, move the edge upwards or to
3384 the left. If the edge can't be moved by DELTA lines or columns,
3385 move it as far as possible in the desired direction."
3386 (setq window (window-normalize-window window))
3387 (let* ((frame (window-frame window))
3388 (minibuffer-window (minibuffer-window frame))
3389 (right window)
3390 left first-left first-right this-delta min-delta max-delta ignore)
3392 (unless pixelwise
3393 (setq pixelwise t)
3394 (setq delta (* delta (frame-char-size window horizontal))))
3396 ;; Find the edge we want to move.
3397 (while (and (or (not (window-combined-p right horizontal))
3398 (not (window-right right)))
3399 (setq right (window-parent right))))
3400 (cond
3401 ((and (not right) (not horizontal)
3402 ;; Resize the minibuffer window if it's on the same frame as
3403 ;; and immediately below WINDOW and it's either active or
3404 ;; `resize-mini-windows' is nil.
3405 (eq (window-frame minibuffer-window) frame)
3406 (= (nth 1 (window-pixel-edges minibuffer-window))
3407 (nth 3 (window-pixel-edges window)))
3408 (or (not resize-mini-windows)
3409 (eq minibuffer-window (active-minibuffer-window))))
3410 (window--resize-mini-window minibuffer-window (- delta)))
3411 ((or (not (setq left right)) (not (setq right (window-right right))))
3412 (if horizontal
3413 (user-error "No window on the right of this one")
3414 (user-error "No window below this one")))
3416 ;; Set LEFT to the first resizable window on the left. This step is
3417 ;; needed to handle fixed-size windows.
3418 (setq first-left left)
3419 (while (and left
3420 (or (window-size-fixed-p left horizontal)
3421 (and (< delta 0)
3422 (<= (window-size left horizontal t)
3423 (window-min-size left horizontal nil t)))))
3424 (setq left
3425 (or (window-left left)
3426 (progn
3427 (while (and (setq left (window-parent left))
3428 (not (window-combined-p left horizontal))))
3429 (window-left left)))))
3430 (unless left
3431 ;; We have to resize a size-preserved window. Start again with
3432 ;; the window initially on the left.
3433 (setq ignore 'preserved)
3434 (setq left first-left)
3435 (while (and left
3436 (or (window-size-fixed-p left horizontal 'preserved)
3437 (and (< delta 0)
3438 (<= (window-size left horizontal t)
3439 (window-min-size
3440 left horizontal 'preserved t)))))
3441 (setq left
3442 (or (window-left left)
3443 (progn
3444 (while (and (setq left (window-parent left))
3445 (not (window-combined-p left horizontal))))
3446 (window-left left)))))
3448 (unless left
3449 (if horizontal
3450 (user-error "No resizable window on the left of this one")
3451 (user-error "No resizable window above this one"))))
3453 ;; Set RIGHT to the first resizable window on the right. This step
3454 ;; is needed to handle fixed-size windows.
3455 (setq first-right right)
3456 (while (and right
3457 (or (window-size-fixed-p right horizontal)
3458 (and (> delta 0)
3459 (<= (window-size right horizontal t)
3460 (window-min-size
3461 right horizontal 'preserved t)))))
3462 (setq right
3463 (or (window-right right)
3464 (progn
3465 (while (and (setq right (window-parent right))
3466 (not (window-combined-p right horizontal))))
3467 (window-right right)))))
3468 (unless right
3469 ;; We have to resize a size-preserved window. Start again with
3470 ;; the window initially on the right.
3471 (setq ignore 'preserved)
3472 (setq right first-right)
3473 (while (and right
3474 (or (window-size-fixed-p right horizontal 'preserved)
3475 (and (> delta 0)
3476 (<= (window-size right horizontal t)
3477 (window-min-size
3478 right horizontal 'preserved t)))))
3479 (setq right
3480 (or (window-right right)
3481 (progn
3482 (while (and (setq right (window-parent right))
3483 (not (window-combined-p right horizontal))))
3484 (window-right right)))))
3485 (unless right
3486 (if horizontal
3487 (user-error "No resizable window on the right of this one")
3488 (user-error "No resizable window below this one"))))
3490 ;; LEFT and RIGHT (which might be both internal windows) are now the
3491 ;; two windows we want to resize.
3492 (cond
3493 ((> delta 0)
3494 (setq max-delta
3495 (window--max-delta-1
3496 left 0 horizontal ignore 'after nil pixelwise))
3497 (setq min-delta
3498 (window--min-delta-1
3499 right (- delta) horizontal ignore 'before nil pixelwise))
3500 (when (or (< max-delta delta) (> min-delta (- delta)))
3501 ;; We can't get the whole DELTA - move as far as possible.
3502 (setq delta (min max-delta (- min-delta))))
3503 (unless (zerop delta)
3504 ;; Start resizing.
3505 (window--resize-reset frame horizontal)
3506 ;; Try to enlarge LEFT first.
3507 (setq this-delta
3508 (window--resizable
3509 left delta horizontal ignore 'after nil nil pixelwise))
3510 (unless (zerop this-delta)
3511 (window--resize-this-window
3512 left this-delta horizontal ignore t 'before
3513 (if horizontal
3514 (+ (window-pixel-left left) (window-pixel-width left))
3515 (+ (window-pixel-top left) (window-pixel-height left)))))
3516 ;; Shrink windows on right of LEFT.
3517 (window--resize-siblings
3518 left delta horizontal ignore 'after
3519 (if horizontal
3520 (window-pixel-left right)
3521 (window-pixel-top right)))))
3522 ((< delta 0)
3523 (setq max-delta
3524 (window--max-delta-1
3525 right 0 horizontal ignore 'before nil pixelwise))
3526 (setq min-delta
3527 (window--min-delta-1
3528 left delta horizontal ignore 'after nil pixelwise))
3529 (when (or (< max-delta (- delta)) (> min-delta delta))
3530 ;; We can't get the whole DELTA - move as far as possible.
3531 (setq delta (max (- max-delta) min-delta)))
3532 (unless (zerop delta)
3533 ;; Start resizing.
3534 (window--resize-reset frame horizontal)
3535 ;; Try to enlarge RIGHT.
3536 (setq this-delta
3537 (window--resizable
3538 right (- delta) horizontal ignore 'before nil nil pixelwise))
3539 (unless (zerop this-delta)
3540 (window--resize-this-window
3541 right this-delta horizontal ignore t 'after
3542 (if horizontal
3543 (window-pixel-left right)
3544 (window-pixel-top right))))
3545 ;; Shrink windows on left of RIGHT.
3546 (window--resize-siblings
3547 right (- delta) horizontal ignore 'before
3548 (if horizontal
3549 (+ (window-pixel-left left) (window-pixel-width left))
3550 (+ (window-pixel-top left) (window-pixel-height left)))))))
3551 (unless (zerop delta)
3552 ;; Don't report an error in the standard case.
3553 (when (window--resize-apply-p frame horizontal)
3554 (if (window-resize-apply frame horizontal)
3555 (progn
3556 (window--pixel-to-total frame horizontal)
3557 (run-window-configuration-change-hook frame))
3558 ;; But do report an error if applying the changes fails.
3559 (error "Failed adjusting window %s" window))))))))
3561 (defun enlarge-window (delta &optional horizontal)
3562 "Make the selected window DELTA lines taller.
3563 Interactively, if no argument is given, make the selected window
3564 one line taller. If optional argument HORIZONTAL is non-nil,
3565 make selected window wider by DELTA columns. If DELTA is
3566 negative, shrink selected window by -DELTA lines or columns."
3567 (interactive "p")
3568 (let ((minibuffer-window (minibuffer-window)))
3569 (when (window-preserved-size nil horizontal)
3570 (window-preserve-size nil horizontal))
3571 (cond
3572 ((zerop delta))
3573 ((window-size-fixed-p nil horizontal)
3574 (user-error "Selected window has fixed size"))
3575 ((window-minibuffer-p)
3576 (if horizontal
3577 (user-error "Cannot resize minibuffer window horizontally")
3578 (window--resize-mini-window
3579 (selected-window) (* delta (frame-char-height)))))
3580 ((and (not horizontal)
3581 (window-full-height-p)
3582 (eq (window-frame minibuffer-window) (selected-frame))
3583 (not resize-mini-windows))
3584 ;; If the selected window is full height and `resize-mini-windows'
3585 ;; is nil, resize the minibuffer window.
3586 (window--resize-mini-window
3587 minibuffer-window (* (- delta) (frame-char-height))))
3588 ((window--resizable-p nil delta horizontal)
3589 (window-resize nil delta horizontal))
3590 ((window--resizable-p nil delta horizontal 'preserved)
3591 (window-resize nil delta horizontal 'preserved))
3592 ((eq this-command
3593 (if horizontal 'enlarge-window-horizontally 'enlarge-window))
3594 ;; For backward compatibility don't signal an error unless this
3595 ;; command is `enlarge-window(-horizontally)'.
3596 (user-error "Cannot enlarge selected window"))
3598 (window-resize
3599 nil (if (> delta 0)
3600 (window-max-delta nil horizontal)
3601 (- (window-min-delta nil horizontal)))
3602 horizontal)))))
3604 (defun shrink-window (delta &optional horizontal)
3605 "Make the selected window DELTA lines smaller.
3606 Interactively, if no argument is given, make the selected window
3607 one line smaller. If optional argument HORIZONTAL is non-nil,
3608 make selected window narrower by DELTA columns. If DELTA is
3609 negative, enlarge selected window by -DELTA lines or columns."
3610 (interactive "p")
3611 (let ((minibuffer-window (minibuffer-window)))
3612 (when (window-preserved-size nil horizontal)
3613 (window-preserve-size nil horizontal))
3614 (cond
3615 ((zerop delta))
3616 ((window-size-fixed-p nil horizontal)
3617 (user-error "Selected window has fixed size"))
3618 ((window-minibuffer-p)
3619 (if horizontal
3620 (user-error "Cannot resize minibuffer window horizontally")
3621 (window--resize-mini-window
3622 (selected-window) (* (- delta) (frame-char-height)))))
3623 ((and (not horizontal)
3624 (window-full-height-p)
3625 (eq (window-frame minibuffer-window) (selected-frame))
3626 (not resize-mini-windows))
3627 ;; If the selected window is full height and `resize-mini-windows'
3628 ;; is nil, resize the minibuffer window.
3629 (window--resize-mini-window
3630 minibuffer-window (* delta (frame-char-height))))
3631 ((window--resizable-p nil (- delta) horizontal)
3632 (window-resize nil (- delta) horizontal))
3633 ((window--resizable-p nil (- delta) horizontal 'preserved)
3634 (window-resize nil (- delta) horizontal 'preserved))
3635 ((eq this-command
3636 (if horizontal 'shrink-window-horizontally 'shrink-window))
3637 ;; For backward compatibility don't signal an error unless this
3638 ;; command is `shrink-window(-horizontally)'.
3639 (user-error "Cannot shrink selected window"))
3641 (window-resize
3642 nil (if (> delta 0)
3643 (- (window-min-delta nil horizontal))
3644 (window-max-delta nil horizontal))
3645 horizontal)))))
3647 (defun maximize-window (&optional window)
3648 "Maximize WINDOW.
3649 Make WINDOW as large as possible without deleting any windows.
3650 WINDOW must be a valid window and defaults to the selected one.
3652 If the option `window-resize-pixelwise' is non-nil maximize
3653 WINDOW pixelwise."
3654 (interactive)
3655 (setq window (window-normalize-window window))
3656 (window-resize
3657 window (window-max-delta window nil nil nil nil nil window-resize-pixelwise)
3658 nil nil window-resize-pixelwise)
3659 (window-resize
3660 window (window-max-delta window t nil nil nil nil window-resize-pixelwise)
3661 t nil window-resize-pixelwise))
3663 (defun minimize-window (&optional window)
3664 "Minimize WINDOW.
3665 Make WINDOW as small 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 minimize
3669 WINDOW pixelwise."
3670 (interactive)
3671 (setq window (window-normalize-window window))
3672 (window-resize
3673 window
3674 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise))
3675 nil nil window-resize-pixelwise)
3676 (window-resize
3677 window
3678 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise))
3679 t nil window-resize-pixelwise))
3681 ;;; Window edges
3682 (defun window-edges (&optional window body absolute pixelwise)
3683 "Return a list of the edge distances of WINDOW.
3684 WINDOW must be a valid window and defaults to the selected one.
3685 The list returned has the form (LEFT TOP RIGHT BOTTOM).
3687 If the optional argument BODY is nil, this means to return the
3688 edges corresponding to the total size of WINDOW. BODY non-nil
3689 means to return the edges of WINDOW's body (aka text area). If
3690 BODY is non-nil, WINDOW must specify a live window.
3692 Optional argument ABSOLUTE nil means to return edges relative to
3693 the position of WINDOW's native frame. ABSOLUTE non-nil means to
3694 return coordinates relative to the origin - the position (0, 0) -
3695 of FRAME's display. On non-graphical systems this argument has
3696 no effect.
3698 Optional argument PIXELWISE nil means to return the coordinates
3699 in terms of the canonical character width and height of WINDOW's
3700 frame, rounded if necessary. PIXELWISE non-nil means to return
3701 the coordinates in pixels where the values for RIGHT and BOTTOM
3702 are one more than the actual value of these edges. Note that if
3703 ABSOLUTE is non-nil, PIXELWISE is implicitly non-nil too."
3704 (let* ((window (window-normalize-window window body))
3705 (frame (window-frame window))
3706 (border-width (frame-border-width frame))
3707 (char-width (frame-char-width frame))
3708 (char-height (frame-char-height frame))
3709 (left (if pixelwise
3710 (+ (window-pixel-left window) border-width)
3711 (+ (window-left-column window)
3712 (/ border-width char-width))))
3713 (left-body
3714 (when body
3715 (+ (window-pixel-left window) border-width
3716 (if (eq (car (window-current-scroll-bars window)) 'left)
3717 (window-scroll-bar-width window)
3719 (nth 0 (window-fringes window))
3720 (* (or (nth 0 (window-margins window)) 0) char-width))))
3721 (top (if pixelwise
3722 (+ (window-pixel-top window) border-width)
3723 (+ (window-top-line window)
3724 (/ border-width char-height))))
3725 (top-body
3726 (when body
3727 (+ (window-pixel-top window) border-width
3728 (window-header-line-height window))))
3729 (right (+ left (if pixelwise
3730 (window-pixel-width window)
3731 (window-total-width window))))
3732 (right-body (and body (+ left-body (window-body-width window t))))
3733 (bottom (+ top (if pixelwise
3734 (window-pixel-height window)
3735 (window-total-height window))))
3736 (bottom-body (and body (+ top-body (window-body-height window t)))))
3737 (if absolute
3738 (let* ((native-edges (frame-edges frame 'native-edges))
3739 (left-off (nth 0 native-edges))
3740 (top-off (nth 1 native-edges)))
3741 (if body
3742 (list (+ left-body left-off) (+ top-body top-off)
3743 (+ right-body left-off) (+ bottom-body top-off))
3744 (list (+ left left-off) (+ top top-off)
3745 (+ right left-off) (+ bottom top-off))))
3746 (if body
3747 (if pixelwise
3748 (list left-body top-body right-body bottom-body)
3749 (list (/ left-body char-width) (/ top-body char-height)
3750 ;; Round up.
3751 (/ (+ right-body char-width -1) char-width)
3752 (/ (+ bottom-body char-height -1) char-height)))
3753 (list left top right bottom)))))
3755 (defun window-body-edges (&optional window)
3756 "Return a list of the edge coordinates of WINDOW's body.
3757 The return value is that of `window-edges' called with argument
3758 BODY non-nil."
3759 (window-edges window t))
3760 (defalias 'window-inside-edges 'window-body-edges)
3762 (defun window-pixel-edges (&optional window)
3763 "Return a list of the edge pixel coordinates of WINDOW.
3764 The return value is that of `window-edges' called with argument
3765 PIXELWISE non-nil."
3766 (window-edges window nil nil t))
3768 (defun window-body-pixel-edges (&optional window)
3769 "Return a list of the edge pixel coordinates of WINDOW's body.
3770 The return value is that of `window-edges' called with arguments
3771 BODY and PIXELWISE non-nil."
3772 (window-edges window t nil t))
3773 (defalias 'window-inside-pixel-edges 'window-body-pixel-edges)
3775 (defun window-absolute-pixel-edges (&optional window)
3776 "Return a list of the edge pixel coordinates of WINDOW.
3777 The return value is that of `window-edges' called with argument
3778 ABSOLUTE non-nil."
3779 (window-edges window nil t t))
3781 (defun window-absolute-body-pixel-edges (&optional window)
3782 "Return a list of the edge pixel coordinates of WINDOW's text area.
3783 The return value is that of `window-edges' called with arguments
3784 BODY and ABSOLUTE non-nil."
3785 (window-edges window t t t))
3786 (defalias 'window-inside-absolute-pixel-edges 'window-absolute-body-pixel-edges)
3788 (defun window-absolute-pixel-position (&optional position window)
3789 "Return display coordinates of POSITION in WINDOW.
3790 If the buffer position POSITION is visible in window WINDOW,
3791 return the display coordinates of the upper/left corner of the
3792 glyph at POSITION. The return value is a cons of the X- and
3793 Y-coordinates of that corner, relative to an origin at (0, 0) of
3794 WINDOW's display. Return nil if POSITION is not visible in
3795 WINDOW.
3797 WINDOW must be a live window and defaults to the selected window.
3798 POSITION defaults to the value of `window-point' of WINDOW."
3799 (let* ((window (window-normalize-window window t))
3800 (pos-in-window
3801 (pos-visible-in-window-p
3802 (or position (window-point window)) window t)))
3803 (when pos-in-window
3804 (let ((edges (window-absolute-body-pixel-edges window)))
3805 (cons (+ (nth 0 edges) (nth 0 pos-in-window))
3806 (+ (nth 1 edges) (nth 1 pos-in-window)))))))
3808 (defun frame-root-window-p (window)
3809 "Return non-nil if WINDOW is the root window of its frame."
3810 (eq window (frame-root-window window)))
3812 (defun window--subtree (window &optional next)
3813 "Return window subtree rooted at WINDOW.
3814 Optional argument NEXT non-nil means include WINDOW's right
3815 siblings in the return value.
3817 See the documentation of `window-tree' for a description of the
3818 return value."
3819 (let (list)
3820 (while window
3821 (setq list
3822 (cons
3823 (cond
3824 ((window-top-child window)
3825 (cons t (cons (window-edges window)
3826 (window--subtree (window-top-child window) t))))
3827 ((window-left-child window)
3828 (cons nil (cons (window-edges window)
3829 (window--subtree (window-left-child window) t))))
3830 (t window))
3831 list))
3832 (setq window (when next (window-next-sibling window))))
3833 (nreverse list)))
3835 (defun window-tree (&optional frame)
3836 "Return the window tree of frame FRAME.
3837 FRAME must be a live frame and defaults to the selected frame.
3838 The return value is a list of the form (ROOT MINI), where ROOT
3839 represents the window tree of the frame's root window, and MINI
3840 is the frame's minibuffer window.
3842 If the root window is not split, ROOT is the root window itself.
3843 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3844 for a horizontal split, and t for a vertical split. EDGES gives
3845 the combined size and position of the child windows in the split,
3846 and the rest of the elements are the child windows in the split.
3847 Each of the child windows may again be a window or a list
3848 representing a window split, and so on. EDGES is a list (LEFT
3849 TOP RIGHT BOTTOM) as returned by `window-edges'."
3850 (setq frame (window-normalize-frame frame))
3851 (window--subtree (frame-root-window frame) t))
3853 (defun other-window (count &optional all-frames)
3854 "Select another window in cyclic ordering of windows.
3855 COUNT specifies the number of windows to skip, starting with the
3856 selected window, before making the selection. If COUNT is
3857 positive, skip COUNT windows forwards. If COUNT is negative,
3858 skip -COUNT windows backwards. COUNT zero means do not skip any
3859 window, so select the selected window. In an interactive call,
3860 COUNT is the numeric prefix argument. Return nil.
3862 If the `other-window' parameter of the selected window is a
3863 function and `ignore-window-parameters' is nil, call that
3864 function with the arguments COUNT and ALL-FRAMES.
3866 This function does not select a window whose `no-other-window'
3867 window parameter is non-nil.
3869 This function uses `next-window' for finding the window to
3870 select. The argument ALL-FRAMES has the same meaning as in
3871 `next-window', but the MINIBUF argument of `next-window' is
3872 always effectively nil."
3873 (interactive "p")
3874 (let* ((window (selected-window))
3875 (function (and (not ignore-window-parameters)
3876 (window-parameter window 'other-window)))
3877 old-window old-count)
3878 (if (functionp function)
3879 (funcall function count all-frames)
3880 ;; `next-window' and `previous-window' may return a window we are
3881 ;; not allowed to select. Hence we need an exit strategy in case
3882 ;; all windows are non-selectable.
3883 (catch 'exit
3884 (while (> count 0)
3885 (setq window (next-window window nil all-frames))
3886 (cond
3887 ((eq window old-window)
3888 (when (= count old-count)
3889 ;; Keep out of infinite loops. When COUNT has not changed
3890 ;; since we last looked at `window' we're probably in one.
3891 (throw 'exit nil)))
3892 ((window-parameter window 'no-other-window)
3893 (unless old-window
3894 ;; The first non-selectable window `next-window' got us:
3895 ;; Remember it and the current value of COUNT.
3896 (setq old-window window)
3897 (setq old-count count)))
3899 (setq count (1- count)))))
3900 (while (< count 0)
3901 (setq window (previous-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 `previous-window' got
3911 ;; us: Remember it and the current value of COUNT.
3912 (setq old-window window)
3913 (setq old-count count)))
3915 (setq count (1+ count)))))
3917 (select-window window)
3918 ;; Always return nil.
3919 nil))))
3921 ;; This should probably return non-nil when the selected window is part
3922 ;; of an atomic window whose root is the frame's root window.
3923 (defun one-window-p (&optional nomini all-frames)
3924 "Return non-nil if the selected window is the only window.
3925 Optional arg NOMINI non-nil means don't count the minibuffer
3926 even if it is active. Otherwise, the minibuffer is counted
3927 when it is active.
3929 Optional argument ALL-FRAMES specifies the set of frames to
3930 consider, see also `next-window'. ALL-FRAMES nil or omitted
3931 means consider windows on the selected frame only, plus the
3932 minibuffer window if specified by the NOMINI argument. If the
3933 minibuffer counts, consider all windows on all frames that share
3934 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3935 with a special meaning are:
3937 - t means consider all windows on all existing frames.
3939 - `visible' means consider all windows on all visible frames on
3940 the current terminal.
3942 - 0 (the number zero) means consider all windows on all visible
3943 and iconified frames on the current terminal.
3945 - A frame means consider all windows on that frame only.
3947 Anything else means consider all windows on the selected frame
3948 and no others."
3949 (let ((base-window (selected-window)))
3950 (if (and nomini (eq base-window (minibuffer-window)))
3951 (setq base-window (next-window base-window)))
3952 (eq base-window
3953 (next-window base-window (if nomini 'arg) all-frames))))
3955 ;;; Deleting windows.
3956 (defun window-deletable-p (&optional window)
3957 "Return t if WINDOW can be safely deleted from its frame.
3958 WINDOW must be a valid window and defaults to the selected one.
3960 Return `frame' if WINDOW is the root window of its frame and that
3961 frame can be safely deleted."
3962 (setq window (window-normalize-window window))
3964 (unless (or ignore-window-parameters
3965 (eq (window-parameter window 'delete-window) t))
3966 ;; Handle atomicity.
3967 (when (window-parameter window 'window-atom)
3968 (setq window (window-atom-root window))))
3970 (let ((frame (window-frame window)))
3971 (cond
3972 ((frame-root-window-p window)
3973 ;; WINDOW's frame can be deleted only if there are other frames
3974 ;; on the same terminal, and it does not contain the active
3975 ;; minibuffer.
3976 (unless (or (eq frame (next-frame frame 0))
3977 ;; We can delete our frame only if no other frame
3978 ;; currently uses our minibuffer window.
3979 (catch 'other
3980 (dolist (other (frame-list))
3981 (when (and (not (eq other frame))
3982 (eq (window-frame (minibuffer-window other))
3983 frame))
3984 (throw 'other t))))
3985 (let ((minibuf (active-minibuffer-window)))
3986 (and minibuf (eq frame (window-frame minibuf)))))
3987 'frame))
3988 ((window-minibuffer-p window)
3989 ;; If WINDOW is the minibuffer window of a non-minibuffer-only
3990 ;; frame, it cannot be deleted separately.
3991 nil)
3992 ((or ignore-window-parameters
3993 (not (eq window (window-main-window frame))))
3994 ;; Otherwise, WINDOW can be deleted unless it is the main window
3995 ;; of its frame.
3996 t))))
3998 (defun window--in-subtree-p (window root)
3999 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
4000 (or (eq window root)
4001 (let ((parent (window-parent window)))
4002 (catch 'done
4003 (while parent
4004 (if (eq parent root)
4005 (throw 'done t)
4006 (setq parent (window-parent parent))))))))
4008 (defun delete-window (&optional window)
4009 "Delete WINDOW.
4010 WINDOW must be a valid window and defaults to the selected one.
4011 Return nil.
4013 If the variable `ignore-window-parameters' is non-nil or the
4014 `delete-window' parameter of WINDOW equals t, do not process any
4015 parameters of WINDOW. Otherwise, if the `delete-window'
4016 parameter of WINDOW specifies a function, call that function with
4017 WINDOW as its sole argument and return the value returned by that
4018 function.
4020 Otherwise, if WINDOW is part of an atomic window, call
4021 `delete-window' with the root of the atomic window as its
4022 argument. Signal an error if WINDOW is either the only window on
4023 its frame, the last non-side window, or part of an atomic window
4024 that is its frame's root window."
4025 (interactive)
4026 (setq window (window-normalize-window window))
4027 (let* ((frame (window-frame window))
4028 (function (window-parameter window 'delete-window))
4029 (parent (window-parent window))
4030 atom-root)
4031 (window--check frame)
4032 (catch 'done
4033 ;; Handle window parameters.
4034 (cond
4035 ;; Ignore window parameters if `ignore-window-parameters' tells
4036 ;; us so or `delete-window' equals t.
4037 ((or ignore-window-parameters (eq function t)))
4038 ((functionp function)
4039 ;; The `delete-window' parameter specifies the function to call.
4040 ;; If that function is `ignore' nothing is done. It's up to the
4041 ;; function called here to avoid infinite recursion.
4042 (throw 'done (funcall function window)))
4043 ((and (window-parameter window 'window-atom)
4044 (setq atom-root (window-atom-root window))
4045 (not (eq atom-root window)))
4046 (if (eq atom-root (frame-root-window frame))
4047 (error "Root of atomic window is root window of its frame")
4048 (throw 'done (delete-window atom-root))))
4049 ((not parent)
4050 (error "Attempt to delete minibuffer or sole ordinary window"))
4051 ((eq window (window-main-window frame))
4052 (error "Attempt to delete main window of frame %s" frame)))
4054 (let* ((horizontal (window-left-child parent))
4055 (size (window-size window horizontal t))
4056 (window-combination-resize
4057 (or window-combination-resize
4058 (window-parameter parent 'window-side)))
4059 (frame-selected
4060 (window--in-subtree-p (frame-selected-window frame) window))
4061 ;; Emacs 23 preferably gives WINDOW's space to its left
4062 ;; sibling.
4063 (sibling (or (window-left window) (window-right window))))
4064 (window--resize-reset frame horizontal)
4065 (cond
4066 ((and (not (eq window-combination-resize t))
4067 sibling (window-sizable-p sibling size horizontal nil t))
4068 ;; Resize WINDOW's sibling.
4069 (window--resize-this-window sibling size horizontal nil t)
4070 (set-window-new-normal
4071 sibling (+ (window-normal-size sibling horizontal)
4072 (window-normal-size window horizontal))))
4073 ((window--resizable-p window (- size) horizontal nil nil nil t t)
4074 ;; Can do without resizing fixed-size windows.
4075 (window--resize-siblings window (- size) horizontal))
4077 ;; Can't do without resizing fixed-size windows.
4078 (window--resize-siblings window (- size) horizontal t)))
4079 ;; Actually delete WINDOW.
4080 (delete-window-internal window)
4081 (window--pixel-to-total frame horizontal)
4082 (when (and frame-selected
4083 (window-parameter
4084 (frame-selected-window frame) 'no-other-window))
4085 ;; `delete-window-internal' has selected a window that should
4086 ;; not be selected, fix this here.
4087 (other-window -1 frame))
4088 (run-window-configuration-change-hook frame)
4089 (window--check frame)
4090 ;; Always return nil.
4091 nil))))
4093 (defun delete-other-windows (&optional window)
4094 "Make WINDOW fill its frame.
4095 WINDOW must be a valid window and defaults to the selected one.
4096 Return nil.
4098 If the variable `ignore-window-parameters' is non-nil or the
4099 `delete-other-windows' parameter of WINDOW equals t, do not
4100 process any parameters of WINDOW. Otherwise, if the
4101 `delete-other-windows' parameter of WINDOW specifies a function,
4102 call that function with WINDOW as its sole argument and return
4103 the value returned by that function.
4105 Otherwise, if WINDOW is part of an atomic window, call this
4106 function with the root of the atomic window as its argument. If
4107 WINDOW is a non-side window, make WINDOW the only non-side window
4108 on the frame. Side windows are not deleted. If WINDOW is a side
4109 window signal an error."
4110 (interactive)
4111 (setq window (window-normalize-window window))
4112 (let* ((frame (window-frame window))
4113 (function (window-parameter window 'delete-other-windows))
4114 atom-root main)
4115 (window--check frame)
4116 (catch 'done
4117 (cond
4118 ;; Ignore window parameters if `ignore-window-parameters' is t or
4119 ;; `delete-other-windows' is t.
4120 ((or ignore-window-parameters (eq function t)))
4121 ((functionp function)
4122 ;; The `delete-other-windows' parameter specifies the function
4123 ;; to call. If the function is `ignore' no windows are deleted.
4124 ;; It's up to the function called to avoid infinite recursion.
4125 (throw 'done (funcall function window)))
4126 ((and (window-parameter window 'window-atom)
4127 (setq atom-root (window-atom-root window))
4128 (not (eq atom-root window)))
4129 (if (eq atom-root (frame-root-window frame))
4130 (error "Root of atomic window is root window of its frame")
4131 (throw 'done (delete-other-windows atom-root))))
4132 ((window-parameter window 'window-side)
4133 (error "Cannot make side window the only window"))
4134 ((and (window-minibuffer-p window)
4135 (not (eq window (frame-root-window window))))
4136 (error "Can't expand minibuffer to full frame")))
4138 (cond
4139 ((or ignore-window-parameters
4140 (not (window-with-parameter 'no-delete-other-window nil frame)))
4141 (setq main (frame-root-window frame)))
4142 ((catch 'tag
4143 (walk-window-tree
4144 (lambda (other)
4145 (when (or (and (window-parameter other 'window-side)
4146 (not (window-parameter
4147 other 'no-delete-other-window)))
4148 (and (not (window-parameter other 'window-side))
4149 (window-parameter
4150 other 'no-delete-other-window)))
4151 (throw 'tag nil))))
4153 (setq main (window-main-window frame)))
4155 ;; Delete other windows via `delete-window' because either a
4156 ;; side window is or a non-side-window is not deletable.
4157 (dolist (other (window-list frame))
4158 (when (and (window-live-p other)
4159 (not (eq other window))
4160 (not (window-parameter
4161 other 'no-delete-other-window))
4162 ;; When WINDOW and the other window are part of the
4163 ;; same atomic window, don't delete the other.
4164 (or (not atom-root)
4165 (not (eq (window-atom-root other) atom-root))))
4166 (condition-case nil
4167 (delete-window other)
4168 (error nil))))
4169 (throw 'done nil)))
4171 ;; If WINDOW is the main window of its frame do nothing.
4172 (unless (eq window main)
4173 (delete-other-windows-internal window main)
4174 (run-window-configuration-change-hook frame)
4175 (window--check frame))
4176 ;; Always return nil.
4177 nil)))
4179 (defun delete-other-windows-vertically (&optional window)
4180 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
4181 This may be a useful alternative binding for \\[delete-other-windows]
4182 if you often split windows horizontally."
4183 (interactive)
4184 (let* ((window (or window (selected-window)))
4185 (edges (window-edges window))
4186 (w window) delenda)
4187 (while (not (eq (setq w (next-window w 1)) window))
4188 (let ((e (window-edges w)))
4189 (when (and (= (car e) (car edges))
4190 (= (nth 2 e) (nth 2 edges)))
4191 (push w delenda))))
4192 (mapc 'delete-window delenda)))
4194 ;;; Windows and buffers.
4196 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
4197 ;; for (1) determining which buffer to show in the window when its
4198 ;; buffer shall be buried or killed and (2) which buffer to show for
4199 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
4201 ;; `prev-buffers' consists of <buffer, window-start, window-point>
4202 ;; triples. The entries on this list are ordered by the time their
4203 ;; buffer has been removed from the window, the most recently removed
4204 ;; buffer's entry being first. The window-start and window-point
4205 ;; components are `window-start' and `window-point' at the time the
4206 ;; buffer was removed from the window which implies that the entry must
4207 ;; be added when `set-window-buffer' removes the buffer from the window.
4209 ;; `next-buffers' is the list of buffers that have been replaced
4210 ;; recently by `switch-to-prev-buffer'. These buffers are the least
4211 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
4212 ;; candidates of `switch-to-next-buffer' to switch to. This list is
4213 ;; reset to nil by any action changing the window's buffer with the
4214 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
4215 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
4216 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
4218 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
4219 ;; if such a buffer was killed while the window was hidden within a
4220 ;; window configuration. Such killed buffers get removed whenever
4221 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
4223 ;; The following function is called by `set-window-buffer' _before_ it
4224 ;; replaces the buffer of the argument window with the new buffer.
4225 (defun record-window-buffer (&optional window)
4226 "Record WINDOW's buffer.
4227 WINDOW must be a live window and defaults to the selected one."
4228 (let* ((window (window-normalize-window window t))
4229 (buffer (window-buffer window))
4230 (entry (assq buffer (window-prev-buffers window))))
4231 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
4232 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
4233 (set-window-next-buffers window nil)
4235 (when entry
4236 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
4237 (set-window-prev-buffers
4238 window (assq-delete-all buffer (window-prev-buffers window))))
4240 ;; Don't record insignificant buffers.
4241 (unless (eq (aref (buffer-name buffer) 0) ?\s)
4242 ;; Add an entry for buffer to WINDOW's previous buffers.
4243 (with-current-buffer buffer
4244 (let ((start (window-start window))
4245 (point (window-point window)))
4246 (setq entry
4247 (cons buffer
4248 (if entry
4249 ;; We have an entry, update marker positions.
4250 (list (set-marker (nth 1 entry) start)
4251 (set-marker (nth 2 entry) point))
4252 ;; Make new markers.
4253 (list (copy-marker start)
4254 (copy-marker
4255 ;; Preserve window-point-insertion-type
4256 ;; (Bug#12588).
4257 point window-point-insertion-type)))))
4258 (set-window-prev-buffers
4259 window (cons entry (window-prev-buffers window)))))
4261 (run-hooks 'buffer-list-update-hook))))
4263 (defun unrecord-window-buffer (&optional window buffer)
4264 "Unrecord BUFFER in WINDOW.
4265 WINDOW must be a live window and defaults to the selected one.
4266 BUFFER must be a live buffer and defaults to the buffer of
4267 WINDOW."
4268 (let* ((window (window-normalize-window window t))
4269 (buffer (or buffer (window-buffer window))))
4270 (set-window-prev-buffers
4271 window (assq-delete-all buffer (window-prev-buffers window)))
4272 (set-window-next-buffers
4273 window (delq buffer (window-next-buffers window)))))
4275 (defun set-window-buffer-start-and-point (window buffer &optional start point)
4276 "Set WINDOW's buffer to BUFFER.
4277 WINDOW must be a live window and defaults to the selected one.
4278 Optional argument START non-nil means set WINDOW's start position
4279 to START. Optional argument POINT non-nil means set WINDOW's
4280 point to POINT. If WINDOW is selected this also sets BUFFER's
4281 `point' to POINT. If WINDOW is selected and the buffer it showed
4282 before was current this also makes BUFFER the current buffer."
4283 (setq window (window-normalize-window window t))
4284 (let ((selected (eq window (selected-window)))
4285 (current (eq (window-buffer window) (current-buffer))))
4286 (set-window-buffer window buffer)
4287 (when (and selected current)
4288 (set-buffer buffer))
4289 (when start
4290 ;; Don't force window-start here (even if POINT is nil).
4291 (set-window-start window start t))
4292 (when point
4293 (set-window-point window point))))
4295 (defcustom switch-to-visible-buffer t
4296 "If non-nil, allow switching to an already visible buffer.
4297 If this variable is non-nil, `switch-to-prev-buffer' and
4298 `switch-to-next-buffer' may switch to an already visible buffer.
4299 If this variable is nil, `switch-to-prev-buffer' and
4300 `switch-to-next-buffer' always try to avoid switching to a buffer
4301 that is already visible in another window on the same frame."
4302 :type 'boolean
4303 :version "24.1"
4304 :group 'windows)
4306 (defun switch-to-prev-buffer (&optional window bury-or-kill)
4307 "In WINDOW switch to previous buffer.
4308 WINDOW must be a live window and defaults to the selected one.
4309 Return the buffer switched to, nil if no suitable buffer could be
4310 found.
4312 Optional argument BURY-OR-KILL non-nil means the buffer currently
4313 shown in WINDOW is about to be buried or killed and consequently
4314 shall not be switched to in future invocations of this command.
4316 As a special case, if BURY-OR-KILL equals `append', this means to
4317 move the buffer to the end of WINDOW's previous buffers list so a
4318 future invocation of `switch-to-prev-buffer' less likely switches
4319 to it."
4320 (interactive)
4321 (let* ((window (window-normalize-window window t))
4322 (frame (window-frame window))
4323 (window-side (window-parameter window 'window-side))
4324 (old-buffer (window-buffer window))
4325 ;; Save this since it's destroyed by `set-window-buffer'.
4326 (next-buffers (window-next-buffers window))
4327 (pred (frame-parameter frame 'buffer-predicate))
4328 entry new-buffer killed-buffers visible)
4329 (when (window-minibuffer-p window)
4330 ;; Don't switch in minibuffer window.
4331 (unless (setq window (minibuffer-selected-window))
4332 (error "Window %s is a minibuffer window" window)))
4334 (unless (memq (window-dedicated-p window) '(nil side))
4335 ;; Don't switch in dedicated window.
4336 (error "Window %s is dedicated to buffer %s" window old-buffer))
4338 (catch 'found
4339 ;; Scan WINDOW's previous buffers first, skipping entries of next
4340 ;; buffers.
4341 (dolist (entry (window-prev-buffers window))
4342 (when (and (setq new-buffer (car entry))
4343 (or (buffer-live-p new-buffer)
4344 (not (setq killed-buffers
4345 (cons new-buffer killed-buffers))))
4346 (not (eq new-buffer old-buffer))
4347 (or (null pred) (funcall pred new-buffer))
4348 ;; When BURY-OR-KILL is nil, avoid switching to a
4349 ;; buffer in WINDOW's next buffers list.
4350 (or bury-or-kill (not (memq new-buffer next-buffers))))
4351 (if (and (not switch-to-visible-buffer)
4352 (get-buffer-window new-buffer frame))
4353 ;; Try to avoid showing a buffer visible in some other
4354 ;; window.
4355 (setq visible new-buffer)
4356 (set-window-buffer-start-and-point
4357 window new-buffer (nth 1 entry) (nth 2 entry))
4358 (throw 'found t))))
4359 ;; Scan reverted buffer list of WINDOW's frame next, skipping
4360 ;; entries of next buffers. Note that when we bury or kill a
4361 ;; buffer we don't reverse the global buffer list to avoid showing
4362 ;; a buried buffer instead. Otherwise, we must reverse the global
4363 ;; buffer list in order to make sure that switching to the
4364 ;; previous/next buffer traverse it in opposite directions. Skip
4365 ;; this step for side windows.
4366 (unless window-side
4367 (dolist (buffer (if bury-or-kill
4368 (buffer-list frame)
4369 (nreverse (buffer-list frame))))
4370 (when (and (buffer-live-p buffer)
4371 (not (eq buffer old-buffer))
4372 (or (null pred) (funcall pred buffer))
4373 (not (eq (aref (buffer-name buffer) 0) ?\s))
4374 ;; Don't show a buffer shown in a side window before.
4375 (not (buffer-local-value 'window--sides-shown buffer))
4376 (or bury-or-kill (not (memq buffer next-buffers))))
4377 (if (and (not switch-to-visible-buffer)
4378 (get-buffer-window buffer frame))
4379 ;; Try to avoid showing a buffer visible in some other window.
4380 (unless visible
4381 (setq visible buffer))
4382 (setq new-buffer buffer)
4383 (set-window-buffer-start-and-point window new-buffer)
4384 (throw 'found t)))))
4385 (unless bury-or-kill
4386 ;; Scan reverted next buffers last (must not use nreverse
4387 ;; here!).
4388 (dolist (buffer (reverse next-buffers))
4389 ;; Actually, buffer _must_ be live here since otherwise it
4390 ;; would have been caught in the scan of previous buffers.
4391 (when (and (or (buffer-live-p buffer)
4392 (not (setq killed-buffers
4393 (cons buffer killed-buffers))))
4394 (not (eq buffer old-buffer))
4395 (or (null pred) (funcall pred buffer))
4396 (setq entry (assq buffer (window-prev-buffers window))))
4397 (setq new-buffer buffer)
4398 (set-window-buffer-start-and-point
4399 window new-buffer (nth 1 entry) (nth 2 entry))
4400 (throw 'found t))))
4402 ;; Show a buffer visible in another window.
4403 (when visible
4404 (setq new-buffer visible)
4405 (set-window-buffer-start-and-point window new-buffer)))
4407 (if bury-or-kill
4408 (let ((entry (and (eq bury-or-kill 'append)
4409 (assq old-buffer (window-prev-buffers window)))))
4410 ;; Remove `old-buffer' from WINDOW's previous and (restored list
4411 ;; of) next buffers.
4412 (set-window-prev-buffers
4413 window (assq-delete-all old-buffer (window-prev-buffers window)))
4414 (set-window-next-buffers window (delq old-buffer next-buffers))
4415 (when entry
4416 ;; Append old-buffer's entry to list of WINDOW's previous
4417 ;; buffers so it's less likely to get switched to soon but
4418 ;; `display-buffer-in-previous-window' can nevertheless find
4419 ;; it.
4420 (set-window-prev-buffers
4421 window (append (window-prev-buffers window) (list entry)))))
4422 ;; Move `old-buffer' to head of WINDOW's restored list of next
4423 ;; buffers.
4424 (set-window-next-buffers
4425 window (cons old-buffer (delq old-buffer next-buffers))))
4427 ;; Remove killed buffers from WINDOW's previous and next buffers.
4428 (when killed-buffers
4429 (dolist (buffer killed-buffers)
4430 (set-window-prev-buffers
4431 window (assq-delete-all buffer (window-prev-buffers window)))
4432 (set-window-next-buffers
4433 window (delq buffer (window-next-buffers window)))))
4435 ;; Return new-buffer.
4436 new-buffer))
4438 (defun switch-to-next-buffer (&optional window)
4439 "In WINDOW switch to next buffer.
4440 WINDOW must be a live window and defaults to the selected one.
4441 Return the buffer switched to, nil if no suitable buffer could be
4442 found."
4443 (interactive)
4444 (let* ((window (window-normalize-window window t))
4445 (frame (window-frame window))
4446 (window-side (window-parameter window 'window-side))
4447 (old-buffer (window-buffer window))
4448 (next-buffers (window-next-buffers window))
4449 (pred (frame-parameter frame 'buffer-predicate))
4450 new-buffer entry killed-buffers visible)
4451 (when (window-minibuffer-p window)
4452 ;; Don't switch in minibuffer window.
4453 (unless (setq window (minibuffer-selected-window))
4454 (error "Window %s is a minibuffer window" window)))
4456 (unless (memq (window-dedicated-p window) '(nil side))
4457 ;; Don't switch in dedicated window.
4458 (error "Window %s is dedicated to buffer %s" window old-buffer))
4460 (catch 'found
4461 ;; Scan WINDOW's next buffers first.
4462 (dolist (buffer next-buffers)
4463 (when (and (or (buffer-live-p buffer)
4464 (not (setq killed-buffers
4465 (cons buffer killed-buffers))))
4466 (not (eq buffer old-buffer))
4467 (or (null pred) (funcall pred buffer))
4468 (setq entry (assq buffer (window-prev-buffers window))))
4469 (setq new-buffer buffer)
4470 (set-window-buffer-start-and-point
4471 window new-buffer (nth 1 entry) (nth 2 entry))
4472 (throw 'found t)))
4473 ;; Scan the buffer list of WINDOW's frame next, skipping previous
4474 ;; buffers entries. Skip this step for side windows.
4475 (unless window-side
4476 (dolist (buffer (buffer-list frame))
4477 (when (and (buffer-live-p buffer)
4478 (not (eq buffer old-buffer))
4479 (or (null pred) (funcall pred buffer))
4480 (not (eq (aref (buffer-name buffer) 0) ?\s))
4481 ;; Don't show a buffer shown in a side window before.
4482 (not (buffer-local-value 'window--sides-shown buffer))
4483 (not (assq buffer (window-prev-buffers window))))
4484 (if (and (not switch-to-visible-buffer)
4485 (get-buffer-window buffer frame))
4486 ;; Try to avoid showing a buffer visible in some other window.
4487 (setq visible buffer)
4488 (setq new-buffer buffer)
4489 (set-window-buffer-start-and-point window new-buffer)
4490 (throw 'found t)))))
4491 ;; Scan WINDOW's reverted previous buffers last (must not use
4492 ;; nreverse here!)
4493 (dolist (entry (reverse (window-prev-buffers window)))
4494 (when (and (setq new-buffer (car entry))
4495 (or (buffer-live-p new-buffer)
4496 (not (setq killed-buffers
4497 (cons new-buffer killed-buffers))))
4498 (not (eq new-buffer old-buffer))
4499 (or (null pred) (funcall pred new-buffer)))
4500 (if (and (not switch-to-visible-buffer)
4501 (get-buffer-window new-buffer frame))
4502 ;; Try to avoid showing a buffer visible in some other window.
4503 (unless visible
4504 (setq visible new-buffer))
4505 (set-window-buffer-start-and-point
4506 window new-buffer (nth 1 entry) (nth 2 entry))
4507 (throw 'found t))))
4509 ;; Show a buffer visible in another window.
4510 (when visible
4511 (setq new-buffer visible)
4512 (set-window-buffer-start-and-point window new-buffer)))
4514 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
4515 (set-window-next-buffers window (delq new-buffer next-buffers))
4517 ;; Remove killed buffers from WINDOW's previous and next buffers.
4518 (when killed-buffers
4519 (dolist (buffer killed-buffers)
4520 (set-window-prev-buffers
4521 window (assq-delete-all buffer (window-prev-buffers window)))
4522 (set-window-next-buffers
4523 window (delq buffer (window-next-buffers window)))))
4525 ;; Return new-buffer.
4526 new-buffer))
4528 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
4529 "Search LIST for a valid buffer to display in FRAME.
4530 Return nil when all buffers in LIST are undesirable for display,
4531 otherwise return the first suitable buffer in LIST.
4533 Buffers not visible in windows are preferred to visible buffers,
4534 unless VISIBLE-OK is non-nil.
4535 If the optional argument FRAME is nil, it defaults to the selected frame.
4536 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
4537 ;; This logic is more or less copied from other-buffer.
4538 (setq frame (or frame (selected-frame)))
4539 (let ((pred (frame-parameter frame 'buffer-predicate))
4540 found buf)
4541 (while (and (not found) list)
4542 (setq buf (car list))
4543 (if (and (not (eq buffer buf))
4544 (buffer-live-p buf)
4545 (or (null pred) (funcall pred buf))
4546 (not (eq (aref (buffer-name buf) 0) ?\s))
4547 (or visible-ok (null (get-buffer-window buf 'visible))))
4548 (setq found buf)
4549 (setq list (cdr list))))
4550 (car list)))
4552 (defun last-buffer (&optional buffer visible-ok frame)
4553 "Return the last buffer in FRAME's buffer list.
4554 If BUFFER is the last buffer, return the preceding buffer
4555 instead. Buffers not visible in windows are preferred to visible
4556 buffers, unless optional argument VISIBLE-OK is non-nil.
4557 Optional third argument FRAME nil or omitted means use the
4558 selected frame's buffer list. If no such buffer exists, return
4559 the buffer `*scratch*', creating it if necessary."
4560 (setq frame (or frame (selected-frame)))
4561 (or (get-next-valid-buffer (nreverse (buffer-list frame))
4562 buffer visible-ok frame)
4563 (get-buffer "*scratch*")
4564 (let ((scratch (get-buffer-create "*scratch*")))
4565 (set-buffer-major-mode scratch)
4566 scratch)))
4568 (defcustom frame-auto-hide-function #'iconify-frame
4569 "Function called to automatically hide frames.
4570 The function is called with one argument - a frame.
4572 Functions affected by this option are those that bury a buffer
4573 shown in a separate frame like `quit-window' and `bury-buffer'."
4574 :type '(choice (const :tag "Iconify" iconify-frame)
4575 (const :tag "Delete" delete-frame)
4576 (const :tag "Do nothing" ignore)
4577 function)
4578 :group 'windows
4579 :group 'frames
4580 :version "24.1")
4582 (defun window--delete (&optional window dedicated-only kill)
4583 "Delete WINDOW if possible.
4584 WINDOW must be a live window and defaults to the selected one.
4585 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
4586 only if it's dedicated to its buffer. Optional argument KILL
4587 means the buffer shown in window will be killed. Return non-nil
4588 if WINDOW gets deleted or its frame is auto-hidden."
4589 (setq window (window-normalize-window window t))
4590 (unless (and dedicated-only (not (window-dedicated-p window)))
4591 (let ((deletable (window-deletable-p window)))
4592 (cond
4593 ((eq deletable 'frame)
4594 (let ((frame (window-frame window)))
4595 (cond
4596 (kill
4597 (delete-frame frame))
4598 ((functionp frame-auto-hide-function)
4599 (funcall frame-auto-hide-function frame))))
4600 'frame)
4601 (deletable
4602 (delete-window window)
4603 t)))))
4605 (defun bury-buffer (&optional buffer-or-name)
4606 "Put BUFFER-OR-NAME at the end of the list of all buffers.
4607 There it is the least likely candidate for `other-buffer' to
4608 return; thus, the least likely buffer for \\[switch-to-buffer] to
4609 select by default.
4611 You can specify a buffer name as BUFFER-OR-NAME, or an actual
4612 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
4613 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
4614 remove the current buffer from the selected window if it is
4615 displayed there."
4616 (interactive)
4617 (let* ((buffer (window-normalize-buffer buffer-or-name)))
4618 ;; If `buffer-or-name' is not on the selected frame we unrecord it
4619 ;; although it's not "here" (call it a feature).
4620 (bury-buffer-internal buffer)
4621 ;; Handle case where `buffer-or-name' is nil and the current buffer
4622 ;; is shown in the selected window.
4623 (cond
4624 ((or buffer-or-name (not (eq buffer (window-buffer)))))
4625 ((window--delete nil t))
4627 ;; Switch to another buffer in window.
4628 (set-window-dedicated-p nil nil)
4629 (switch-to-prev-buffer nil 'bury)))
4631 ;; Always return nil.
4632 nil))
4634 (defun unbury-buffer ()
4635 "Switch to the last buffer in the buffer list."
4636 (interactive)
4637 (switch-to-buffer (last-buffer)))
4639 (defun next-buffer ()
4640 "In selected window switch to next buffer."
4641 (interactive)
4642 (cond
4643 ((window-minibuffer-p)
4644 (error "Cannot switch buffers in minibuffer window"))
4645 ((eq (window-dedicated-p) t)
4646 (error "Window is strongly dedicated to its buffer"))
4648 (switch-to-next-buffer))))
4650 (defun previous-buffer ()
4651 "In selected window switch to previous buffer."
4652 (interactive)
4653 (cond
4654 ((window-minibuffer-p)
4655 (error "Cannot switch buffers in minibuffer window"))
4656 ((eq (window-dedicated-p) t)
4657 (error "Window is strongly dedicated to its buffer"))
4659 (switch-to-prev-buffer))))
4661 (defun delete-windows-on (&optional buffer-or-name frame)
4662 "Delete all windows showing BUFFER-OR-NAME.
4663 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4664 and defaults to the current buffer.
4666 The following non-nil values of the optional argument FRAME
4667 have special meanings:
4669 - t means consider all windows on the selected frame only.
4671 - `visible' means consider all windows on all visible frames on
4672 the current terminal.
4674 - 0 (the number zero) means consider all windows on all visible
4675 and iconified frames on the current terminal.
4677 - A frame means consider all windows on that frame only.
4679 Any other value of FRAME means consider all windows on all
4680 frames.
4682 When a window showing BUFFER-OR-NAME is dedicated and the only
4683 window of its frame, that frame is deleted when there are other
4684 frames left."
4685 (interactive "BDelete windows on (buffer):\nP")
4686 (let ((buffer (window-normalize-buffer buffer-or-name))
4687 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4688 ;; `window-list-1' based function.
4689 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4690 (dolist (window (window-list-1 nil nil all-frames))
4691 (if (eq (window-buffer window) buffer)
4692 (let ((deletable (window-deletable-p window)))
4693 (cond
4694 ((and (eq deletable 'frame) (window-dedicated-p window))
4695 ;; Delete frame if and only if window is dedicated.
4696 (delete-frame (window-frame window)))
4697 ((eq deletable t)
4698 ;; Delete window.
4699 (delete-window window))
4701 ;; In window switch to previous buffer.
4702 (set-window-dedicated-p window nil)
4703 (switch-to-prev-buffer window 'bury))))
4704 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4705 (unrecord-window-buffer window buffer)))))
4707 (defun replace-buffer-in-windows (&optional buffer-or-name)
4708 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
4709 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4710 and defaults to the current buffer.
4712 When a window showing BUFFER-OR-NAME is dedicated, that window is
4713 deleted. If that window is the only window on its frame, the
4714 frame is deleted too when there are other frames left. If there
4715 are no other frames left, some other buffer is displayed in that
4716 window.
4718 This function removes the buffer denoted by BUFFER-OR-NAME from
4719 all window-local buffer lists."
4720 (interactive "bBuffer to replace: ")
4721 (let ((buffer (window-normalize-buffer buffer-or-name)))
4722 (dolist (window (window-list-1 nil nil t))
4723 (if (eq (window-buffer window) buffer)
4724 (unless (window--delete window t t)
4725 ;; Switch to another buffer in window.
4726 (set-window-dedicated-p window nil)
4727 (switch-to-prev-buffer window 'kill))
4728 ;; Unrecord BUFFER in WINDOW.
4729 (unrecord-window-buffer window buffer)))))
4731 (defun quit-restore-window (&optional window bury-or-kill)
4732 "Quit WINDOW and deal with its buffer.
4733 WINDOW must be a live window and defaults to the selected one.
4735 According to information stored in WINDOW's `quit-restore' window
4736 parameter either (1) delete WINDOW and its frame, (2) delete
4737 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4738 or (4) make WINDOW display some other buffer than the present
4739 one. If non-nil, reset `quit-restore' parameter to nil.
4741 Optional second argument BURY-OR-KILL tells how to proceed with
4742 the buffer of WINDOW. The following values are handled:
4744 nil means to not handle the buffer in a particular way. This
4745 means that if WINDOW is not deleted by this function, invoking
4746 `switch-to-prev-buffer' will usually show the buffer again.
4748 `append' means that if WINDOW is not deleted, move its buffer to
4749 the end of WINDOW's previous buffers so it's less likely that a
4750 future invocation of `switch-to-prev-buffer' will switch to it.
4751 Also, move the buffer to the end of the frame's buffer list.
4753 `bury' means that if WINDOW is not deleted, remove its buffer
4754 from WINDOW'S list of previous buffers. Also, move the buffer
4755 to the end of the frame's buffer list. This value provides the
4756 most reliable remedy to not have `switch-to-prev-buffer' switch
4757 to this buffer again without killing the buffer.
4759 `kill' means to kill WINDOW's buffer."
4760 (setq window (window-normalize-window window t))
4761 (let* ((buffer (window-buffer window))
4762 (quit-restore (window-parameter window 'quit-restore))
4763 (prev-buffer
4764 (let* ((prev-buffers (window-prev-buffers window))
4765 (prev-buffer (caar prev-buffers)))
4766 (and (or (not (eq prev-buffer buffer))
4767 (and (cdr prev-buffers)
4768 (not (eq (setq prev-buffer (cadr prev-buffers))
4769 buffer))))
4770 prev-buffer)))
4771 quad entry)
4772 (cond
4773 ((and (not prev-buffer)
4774 (or (eq (nth 1 quit-restore) 'frame)
4775 (and (eq (nth 1 quit-restore) 'window)
4776 ;; If the window has been created on an existing
4777 ;; frame and ended up as the sole window on that
4778 ;; frame, do not delete it (Bug#12764).
4779 (not (eq window (frame-root-window window)))))
4780 (eq (nth 3 quit-restore) buffer)
4781 ;; Delete WINDOW if possible.
4782 (window--delete window nil (eq bury-or-kill 'kill)))
4783 ;; If the previously selected window is still alive, select it.
4784 (when (window-live-p (nth 2 quit-restore))
4785 (select-window (nth 2 quit-restore))))
4786 ((and (listp (setq quad (nth 1 quit-restore)))
4787 (buffer-live-p (car quad))
4788 (eq (nth 3 quit-restore) buffer))
4789 ;; Show another buffer stored in quit-restore parameter.
4790 (when (and (integerp (nth 3 quad))
4791 (if (window-combined-p window)
4792 (/= (nth 3 quad) (window-total-height window))
4793 (/= (nth 3 quad) (window-total-width window))))
4794 ;; Try to resize WINDOW to its old height but don't signal an
4795 ;; error.
4796 (condition-case nil
4797 (window-resize
4798 window
4799 (- (nth 3 quad) (if (window-combined-p window)
4800 (window-total-height window)
4801 (window-total-width window)))
4802 (window-combined-p window t))
4803 (error nil)))
4804 (set-window-dedicated-p window nil)
4805 ;; Restore WINDOW's previous buffer, start and point position.
4806 (set-window-buffer-start-and-point
4807 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
4808 ;; Deal with the buffer we just removed from WINDOW.
4809 (setq entry (and (eq bury-or-kill 'append)
4810 (assq buffer (window-prev-buffers window))))
4811 (when bury-or-kill
4812 ;; Remove buffer from WINDOW's previous and next buffers.
4813 (set-window-prev-buffers
4814 window (assq-delete-all buffer (window-prev-buffers window)))
4815 (set-window-next-buffers
4816 window (delq buffer (window-next-buffers window))))
4817 (when entry
4818 ;; Append old buffer's entry to list of WINDOW's previous
4819 ;; buffers so it's less likely to get switched to soon but
4820 ;; `display-buffer-in-previous-window' can nevertheless find it.
4821 (set-window-prev-buffers
4822 window (append (window-prev-buffers window) (list entry))))
4823 ;; Reset the quit-restore parameter.
4824 (set-window-parameter window 'quit-restore nil)
4825 ;; Select old window.
4826 (when (window-live-p (nth 2 quit-restore))
4827 (select-window (nth 2 quit-restore))))
4829 ;; Show some other buffer in WINDOW and reset the quit-restore
4830 ;; parameter.
4831 (set-window-parameter window 'quit-restore nil)
4832 ;; Make sure that WINDOW is no more dedicated.
4833 (set-window-dedicated-p window nil)
4834 (switch-to-prev-buffer window bury-or-kill)))
4836 ;; Deal with the buffer.
4837 (cond
4838 ((not (buffer-live-p buffer)))
4839 ((eq bury-or-kill 'kill)
4840 (kill-buffer buffer))
4841 (bury-or-kill
4842 (bury-buffer-internal buffer)))))
4844 (defun quit-window (&optional kill window)
4845 "Quit WINDOW and bury its buffer.
4846 WINDOW must be a live window and defaults to the selected one.
4847 With prefix argument KILL non-nil, kill the buffer instead of
4848 burying it.
4850 According to information stored in WINDOW's `quit-restore' window
4851 parameter either (1) delete WINDOW and its frame, (2) delete
4852 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4853 or (4) make WINDOW display some other buffer than the present
4854 one. If non-nil, reset `quit-restore' parameter to nil."
4855 (interactive "P")
4856 (quit-restore-window window (if kill 'kill 'bury)))
4858 (defun quit-windows-on (&optional buffer-or-name kill frame)
4859 "Quit all windows showing BUFFER-OR-NAME.
4860 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4861 and defaults to the current buffer. Optional argument KILL
4862 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4863 BUFFER-OR-NAME. Optional argument FRAME is handled as by
4864 `delete-windows-on'.
4866 This function calls `quit-window' on all candidate windows
4867 showing BUFFER-OR-NAME."
4868 (interactive "BQuit windows on (buffer):\nP")
4869 (let ((buffer (window-normalize-buffer buffer-or-name))
4870 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4871 ;; `window-list-1' based function.
4872 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4873 (dolist (window (window-list-1 nil nil all-frames))
4874 (if (eq (window-buffer window) buffer)
4875 (quit-window kill window)
4876 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4877 (unrecord-window-buffer window buffer)))))
4879 (defun split-window (&optional window size side pixelwise)
4880 "Make a new window adjacent to WINDOW.
4881 WINDOW must be a valid window and defaults to the selected one.
4882 Return the new window which is always a live window.
4884 Optional argument SIZE a positive number means make WINDOW SIZE
4885 lines or columns tall. If SIZE is negative, make the new window
4886 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
4887 absolute value can be less than `window-min-height' or
4888 `window-min-width'; so this command can make a new window as
4889 small as one line or two columns. SIZE defaults to half of
4890 WINDOW's size.
4892 Optional third argument SIDE nil (or `below') specifies that the
4893 new window shall be located below WINDOW. SIDE `above' means the
4894 new window shall be located above WINDOW. In both cases SIZE
4895 specifies the new number of lines for WINDOW (or the new window
4896 if SIZE is negative) including space reserved for the mode and/or
4897 header line.
4899 SIDE t (or `right') specifies that the new window shall be
4900 located on the right side of WINDOW. SIDE `left' means the new
4901 window shall be located on the left of WINDOW. In both cases
4902 SIZE specifies the new number of columns for WINDOW (or the new
4903 window provided SIZE is negative) including space reserved for
4904 fringes and the scrollbar or a divider column. Any other non-nil
4905 value for SIDE is currently handled like t (or `right').
4907 PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
4909 If the variable `ignore-window-parameters' is non-nil or the
4910 `split-window' parameter of WINDOW equals t, do not process any
4911 parameters of WINDOW. Otherwise, if the `split-window' parameter
4912 of WINDOW specifies a function, call that function with all three
4913 arguments and return the value returned by that function.
4915 Otherwise, if WINDOW is part of an atomic window, \"split\" the
4916 root of that atomic window. The new window does not become a
4917 member of that atomic window.
4919 If WINDOW is live, properties of the new window like margins and
4920 scrollbars are inherited from WINDOW. If WINDOW is an internal
4921 window, these properties as well as the buffer displayed in the
4922 new window are inherited from the window selected on WINDOW's
4923 frame. The selected window is not changed by this function."
4924 (setq window (window-normalize-window window))
4925 (let* ((side (cond
4926 ((not side) 'below)
4927 ((memq side '(below above right left)) side)
4928 (t 'right)))
4929 (horizontal (not (memq side '(below above))))
4930 (frame (window-frame window))
4931 (parent (window-parent window))
4932 (function (window-parameter window 'split-window))
4933 (window-side (window-parameter window 'window-side))
4934 ;; Rebind the following two variables since in some cases we
4935 ;; have to override their value.
4936 (window-combination-limit window-combination-limit)
4937 (window-combination-resize window-combination-resize)
4938 (char-size (frame-char-size window horizontal))
4939 (pixel-size
4940 (when (numberp size)
4941 (window--size-to-pixel window size horizontal pixelwise t)))
4942 (divider-width (if horizontal
4943 (frame-right-divider-width frame)
4944 (frame-bottom-divider-width frame)))
4945 atom-root ignore)
4946 (window--check frame)
4947 (catch 'done
4948 (cond
4949 ;; Ignore window parameters if either `ignore-window-parameters'
4950 ;; is t or the `split-window' parameter equals t.
4951 ((or ignore-window-parameters (eq function t)))
4952 ((functionp function)
4953 ;; The `split-window' parameter specifies the function to call.
4954 ;; If that function is `ignore', do nothing.
4955 (throw 'done (funcall function window size side)))
4956 ;; If WINDOW is part of an atomic window, split the root window
4957 ;; of that atomic window instead.
4958 ((and (window-parameter window 'window-atom)
4959 (setq atom-root (window-atom-root window))
4960 (not (eq atom-root window)))
4961 (throw 'done (split-window atom-root size side pixelwise)))
4962 ;; If WINDOW is a side window or its first or last child is a
4963 ;; side window, throw an error unless `window-combination-resize'
4964 ;; equals 'side.
4965 ((and (not (eq window-combination-resize 'side))
4966 (window-parameter window 'window-side))
4967 (error "Cannot split side window or parent of side window"))
4968 ;; If `window-combination-resize' is 'side and window has a side
4969 ;; window sibling, bind `window-combination-limit' to t.
4970 ((and (not (eq window-combination-resize 'side))
4971 (or (and (window-prev-sibling window)
4972 (window-parameter
4973 (window-prev-sibling window) 'window-side))
4974 (and (window-next-sibling window)
4975 (window-parameter
4976 (window-next-sibling window) 'window-side))))
4977 (setq window-combination-limit t)))
4979 ;; If `window-combination-resize' is t and SIZE is non-negative,
4980 ;; bind `window-combination-limit' to t.
4981 (when (and (eq window-combination-resize t)
4982 pixel-size (> pixel-size 0))
4983 (setq window-combination-limit t))
4985 (let* ((parent-pixel-size
4986 ;; `parent-pixel-size' is the pixel size of WINDOW's
4987 ;; parent, provided it has one.
4988 (when parent (window-size parent horizontal t)))
4989 ;; `resize' non-nil means we are supposed to resize other
4990 ;; windows in WINDOW's combination.
4991 (resize
4992 (and window-combination-resize
4993 (or (window-parameter window 'window-side)
4994 (not (eq window-combination-resize 'side)))
4995 (not (eq window-combination-limit t))
4996 ;; Resize makes sense in iso-combinations only.
4997 (window-combined-p window horizontal)))
4998 ;; `old-pixel-size' is the current pixel size of WINDOW.
4999 (old-pixel-size (window-size window horizontal t))
5000 ;; `new-size' is the specified or calculated size of the
5001 ;; new window.
5002 new-pixel-size new-parent new-normal)
5003 (cond
5004 ((not pixel-size)
5005 (setq new-pixel-size
5006 (if resize
5007 ;; When resizing try to give the new window the
5008 ;; average size of a window in its combination.
5009 (max (min (- parent-pixel-size
5010 (window-min-size parent horizontal nil t))
5011 (/ parent-pixel-size
5012 (1+ (window-combinations parent horizontal))))
5013 (window-min-pixel-size))
5014 ;; Else try to give the new window half the size
5015 ;; of WINDOW (plus an eventual odd pixel).
5016 (/ old-pixel-size 2)))
5017 (unless window-resize-pixelwise
5018 ;; Round to nearest char-size multiple.
5019 (setq new-pixel-size
5020 (* char-size (round new-pixel-size char-size)))))
5021 ((>= pixel-size 0)
5022 ;; SIZE non-negative specifies the new size of WINDOW.
5024 ;; Note: Specifying a non-negative SIZE is practically
5025 ;; always done as workaround for making the new window
5026 ;; appear above or on the left of the new window (the
5027 ;; ispell window is a typical example of that). In all
5028 ;; these cases the SIDE argument should be set to 'above
5029 ;; or 'left in order to support the 'resize option.
5030 ;; Here we have to nest the windows instead, see above.
5031 (setq new-pixel-size (- old-pixel-size pixel-size)))
5033 ;; SIZE negative specifies the size of the new window.
5034 (setq new-pixel-size (- pixel-size))))
5036 ;; Check SIZE.
5037 (cond
5038 ((not pixel-size)
5039 (cond
5040 (resize
5041 ;; SIZE unspecified, resizing.
5042 (unless (or (window-sizable-p
5043 parent (- (+ new-pixel-size divider-width)) horizontal
5044 nil t)
5045 (window-sizable-p
5046 parent (- (+ new-pixel-size divider-width)) horizontal
5047 (setq ignore 'preserved) t))
5048 (error "Window %s too small for splitting" parent)))
5049 ((and (> (+ new-pixel-size divider-width
5050 (window-min-size window horizontal nil t))
5051 old-pixel-size)
5052 (> (+ new-pixel-size divider-width
5053 (window-min-size
5054 window horizontal (setq ignore 'preserved) t))
5055 old-pixel-size))
5056 ;; SIZE unspecified, no resizing.
5057 (error "Window %s too small for splitting" window))))
5058 ((and (>= pixel-size 0)
5059 (or (>= pixel-size old-pixel-size)
5060 (< new-pixel-size
5061 (window-safe-min-pixel-size window horizontal))))
5062 ;; SIZE specified as new size of old window. If the new size
5063 ;; is larger than the old size or the size of the new window
5064 ;; would be less than the safe minimum, signal an error.
5065 (error "Window %s too small for splitting" window))
5066 (resize
5067 ;; SIZE specified, resizing.
5068 (unless (or (window-sizable-p
5069 parent (- (+ new-pixel-size divider-width)) horizontal
5070 nil t)
5071 (window-sizable-p
5072 parent (- (+ new-pixel-size divider-width)) horizontal
5073 (setq ignore 'preserved) t))
5074 ;; If we cannot resize the parent give up.
5075 (error "Window %s too small for splitting" parent)))
5076 ((or (< new-pixel-size
5077 (window-safe-min-pixel-size window horizontal))
5078 (< (- old-pixel-size new-pixel-size)
5079 (window-safe-min-pixel-size window horizontal)))
5080 ;; SIZE specification violates minimum size restrictions.
5081 (error "Window %s too small for splitting" window)))
5083 (window--resize-reset frame horizontal)
5085 (setq new-parent
5086 ;; Make new-parent non-nil if we need a new parent window;
5087 ;; either because we want to nest or because WINDOW is not
5088 ;; iso-combined.
5089 (or (eq window-combination-limit t)
5090 (not (window-combined-p window horizontal))))
5091 (setq new-normal
5092 ;; Make new-normal the normal size of the new window.
5093 (cond
5094 (pixel-size (/ (float new-pixel-size)
5095 (if new-parent old-pixel-size parent-pixel-size)))
5096 (new-parent 0.5)
5097 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
5098 (t (/ (window-normal-size window horizontal) 2.0))))
5100 (if resize
5101 ;; Try to get space from OLD's siblings. We could go "up" and
5102 ;; try getting additional space from surrounding windows but
5103 ;; we won't be able to return space to those windows when we
5104 ;; delete the one we create here. Hence we do not go up.
5105 (progn
5106 (window--resize-child-windows
5107 parent (- new-pixel-size) horizontal nil ignore)
5108 (let* ((normal (- 1.0 new-normal))
5109 (sub (window-child parent)))
5110 (while sub
5111 (set-window-new-normal
5112 sub (* (window-normal-size sub horizontal) normal))
5113 (setq sub (window-right sub)))))
5114 ;; Get entire space from WINDOW.
5115 (set-window-new-pixel
5116 window (- old-pixel-size new-pixel-size))
5117 (window--resize-this-window
5118 window (- new-pixel-size) horizontal ignore)
5119 (set-window-new-normal
5120 window (- (if new-parent 1.0 (window-normal-size window horizontal))
5121 new-normal)))
5123 (let* ((new (split-window-internal window new-pixel-size side new-normal)))
5124 (window--pixel-to-total frame horizontal)
5125 ;; Assign window-side parameters, if any.
5126 (cond
5127 ((eq window-combination-resize 'side)
5128 (let ((window-side
5129 (cond
5130 (window-side window-side)
5131 ((eq side 'above) 'top)
5132 ((eq side 'below) 'bottom)
5133 (t side))))
5134 ;; We made a new side window.
5135 (set-window-parameter new 'window-side window-side)
5136 (when (and new-parent (window-parameter window 'window-side))
5137 ;; We've been splitting a side root window. Give the
5138 ;; new parent the same window-side parameter.
5139 (set-window-parameter
5140 (window-parent new) 'window-side window-side))))
5141 ((eq window-combination-resize 'atom)
5142 ;; Make sure `window--check-frame' won't destroy an existing
5143 ;; atomic window in case the new window gets nested inside.
5144 (unless (window-parameter window 'window-atom)
5145 (set-window-parameter window 'window-atom t))
5146 (when new-parent
5147 (set-window-parameter (window-parent new) 'window-atom t))
5148 (set-window-parameter new 'window-atom t)))
5150 ;; Sanitize sizes unless SIZE was specified.
5151 (unless size
5152 (window--sanitize-window-sizes horizontal))
5154 (run-window-configuration-change-hook frame)
5155 (run-window-scroll-functions new)
5156 (window--check frame)
5157 ;; Always return the new window.
5158 new)))))
5160 (defun split-window-no-error (&optional window size side pixelwise)
5161 "Make a new window adjacent to WINDOW.
5162 This function is like `split-window' but does not signal an error
5163 when WINDOW cannot be split.
5165 For the meaning of all arguments see the documentation of
5166 `split-window'."
5167 (condition-case nil
5168 (split-window window size side pixelwise)
5169 (error nil)))
5171 ;; I think this should be the default; I think people will prefer it--rms.
5172 (defcustom split-window-keep-point t
5173 "If non-nil, \\[split-window-below] preserves point in the new window.
5174 If nil, adjust point in the two windows to minimize redisplay.
5175 This option applies only to `split-window-below' and functions
5176 that call it. The low-level `split-window' function always keeps
5177 the original point in both windows."
5178 :type 'boolean
5179 :group 'windows)
5181 (defun split-window-below (&optional size)
5182 "Split the selected window into two windows, one above the other.
5183 The selected window is above. The newly split-off window is
5184 below and displays the same buffer. Return the new window.
5186 If optional argument SIZE is omitted or nil, both windows get the
5187 same height, or close to it. If SIZE is positive, the upper
5188 \(selected) window gets SIZE lines. If SIZE is negative, the
5189 lower (new) window gets -SIZE lines.
5191 If the variable `split-window-keep-point' is non-nil, both
5192 windows get the same value of point as the selected window.
5193 Otherwise, the window starts are chosen so as to minimize the
5194 amount of redisplay; this is convenient on slow terminals."
5195 (interactive "P")
5196 (let ((old-window (selected-window))
5197 (old-point (window-point))
5198 (size (and size (prefix-numeric-value size)))
5199 moved-by-window-height moved new-window bottom)
5200 (when (and size (< size 0) (< (- size) window-min-height))
5201 ;; `split-window' would not signal an error here.
5202 (error "Size of new window too small"))
5203 (setq new-window (split-window nil size))
5204 (unless split-window-keep-point
5205 (with-current-buffer (window-buffer)
5206 ;; Use `save-excursion' around vertical movements below
5207 ;; (Bug#10971). Note: When the selected window's buffer has a
5208 ;; header line, up to two lines of the buffer may not show up
5209 ;; in the resulting configuration.
5210 (save-excursion
5211 (goto-char (window-start))
5212 (setq moved (vertical-motion (window-height)))
5213 (set-window-start new-window (point))
5214 (when (> (point) (window-point new-window))
5215 (set-window-point new-window (point)))
5216 (when (= moved (window-height))
5217 (setq moved-by-window-height t)
5218 (vertical-motion -1))
5219 (setq bottom (point)))
5220 (and moved-by-window-height
5221 (<= bottom (point))
5222 (set-window-point old-window (1- bottom)))
5223 (and moved-by-window-height
5224 (<= (window-start new-window) old-point)
5225 (set-window-point new-window old-point)
5226 (select-window new-window))))
5227 ;; Always copy quit-restore parameter in interactive use.
5228 (let ((quit-restore (window-parameter old-window 'quit-restore)))
5229 (when quit-restore
5230 (set-window-parameter new-window 'quit-restore quit-restore)))
5231 new-window))
5233 (defalias 'split-window-vertically 'split-window-below)
5235 (defun split-window-right (&optional size)
5236 "Split the selected window into two side-by-side windows.
5237 The selected window is on the left. The newly split-off window
5238 is on the right and displays the same buffer. Return the new
5239 window.
5241 If optional argument SIZE is omitted or nil, both windows get the
5242 same width, or close to it. If SIZE is positive, the left-hand
5243 \(selected) window gets SIZE columns. If SIZE is negative, the
5244 right-hand (new) window gets -SIZE columns. Here, SIZE includes
5245 the width of the window's scroll bar; if there are no scroll
5246 bars, it includes the width of the divider column to the window's
5247 right, if any."
5248 (interactive "P")
5249 (let ((old-window (selected-window))
5250 (size (and size (prefix-numeric-value size)))
5251 new-window)
5252 (when (and size (< size 0) (< (- size) window-min-width))
5253 ;; `split-window' would not signal an error here.
5254 (error "Size of new window too small"))
5255 (setq new-window (split-window nil size t))
5256 ;; Always copy quit-restore parameter in interactive use.
5257 (let ((quit-restore (window-parameter old-window 'quit-restore)))
5258 (when quit-restore
5259 (set-window-parameter new-window 'quit-restore quit-restore)))
5260 new-window))
5262 (defalias 'split-window-horizontally 'split-window-right)
5264 ;;; Balancing windows.
5266 ;; The following routine uses the recycled code from an old version of
5267 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
5268 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
5269 ;; more readable (FWIW we'd need three loops - one to calculate the
5270 ;; minimum sizes per window, one to enlarge or shrink windows until the
5271 ;; new parent-size matches, and one where we shrink the largest/enlarge
5272 ;; the smallest window).
5273 (defun balance-windows-2 (window horizontal)
5274 "Subroutine of `balance-windows-1'.
5275 WINDOW must be a vertical combination (horizontal if HORIZONTAL
5276 is non-nil)."
5277 (let* ((char-size (if window-resize-pixelwise
5279 (frame-char-size window horizontal)))
5280 (first (window-child window))
5281 (sub first)
5282 (number-of-children 0)
5283 (parent-size (window-new-pixel window))
5284 (total-sum parent-size)
5285 failed size sub-total sub-delta sub-amount rest)
5286 (while sub
5287 (setq number-of-children (1+ number-of-children))
5288 (when (window-size-fixed-p sub horizontal)
5289 (setq total-sum
5290 (- total-sum (window-size sub horizontal t)))
5291 (set-window-new-normal sub 'ignore))
5292 (setq sub (window-right sub)))
5294 (setq failed t)
5295 (while (and failed (> number-of-children 0))
5296 (setq size (/ total-sum number-of-children))
5297 (setq failed nil)
5298 (setq sub first)
5299 (while (and sub (not failed))
5300 ;; Ignore child windows that should be ignored or are stuck.
5301 (unless (window--resize-child-windows-skip-p sub)
5302 (setq sub-total (window-size sub horizontal t))
5303 (setq sub-delta (- size sub-total))
5304 (setq sub-amount
5305 (window-sizable sub sub-delta horizontal nil t))
5306 ;; Register the new total size for this child window.
5307 (set-window-new-pixel sub (+ sub-total sub-amount))
5308 (unless (= sub-amount sub-delta)
5309 (setq total-sum (- total-sum sub-total sub-amount))
5310 (setq number-of-children (1- number-of-children))
5311 ;; We failed and need a new round.
5312 (setq failed t)
5313 (set-window-new-normal sub 'skip)))
5314 (setq sub (window-right sub))))
5316 ;; How can we be sure that `number-of-children' is NOT zero here ?
5317 (setq rest (% total-sum number-of-children))
5318 ;; Fix rounding by trying to enlarge non-stuck windows by one line
5319 ;; (column) until `rest' is zero.
5320 (setq sub first)
5321 (while (and sub (> rest 0))
5322 (unless (window--resize-child-windows-skip-p window)
5323 (set-window-new-pixel sub (min rest char-size) t)
5324 (setq rest (- rest char-size)))
5325 (setq sub (window-right sub)))
5327 ;; Fix rounding by trying to enlarge stuck windows by one line
5328 ;; (column) until `rest' equals zero.
5329 (setq sub first)
5330 (while (and sub (> rest 0))
5331 (unless (eq (window-new-normal sub) 'ignore)
5332 (set-window-new-pixel sub (min rest char-size) t)
5333 (setq rest (- rest char-size)))
5334 (setq sub (window-right sub)))
5336 (setq sub first)
5337 (while sub
5338 ;; Record new normal sizes.
5339 (set-window-new-normal
5340 sub (/ (if (eq (window-new-normal sub) 'ignore)
5341 (window-size sub horizontal t)
5342 (window-new-pixel sub))
5343 (float parent-size)))
5344 ;; Recursively balance each window's child windows.
5345 (balance-windows-1 sub horizontal)
5346 (setq sub (window-right sub)))))
5348 (defun balance-windows-1 (window &optional horizontal)
5349 "Subroutine of `balance-windows'."
5350 (if (window-child window)
5351 (let ((sub (window-child window)))
5352 (if (window-combined-p sub horizontal)
5353 (balance-windows-2 window horizontal)
5354 (let ((size (window-new-pixel window)))
5355 (while sub
5356 (set-window-new-pixel sub size)
5357 (balance-windows-1 sub horizontal)
5358 (setq sub (window-right sub))))))))
5360 (defun balance-windows (&optional window-or-frame)
5361 "Balance the sizes of windows of WINDOW-OR-FRAME.
5362 WINDOW-OR-FRAME is optional and defaults to the selected frame.
5363 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
5364 windows of that frame. If WINDOW-OR-FRAME denotes a window,
5365 recursively balance the sizes of all child windows of that
5366 window."
5367 (interactive)
5368 (let* ((window
5369 (cond
5370 ((or (not window-or-frame)
5371 (frame-live-p window-or-frame))
5372 (frame-root-window window-or-frame))
5373 ((or (window-live-p window-or-frame)
5374 (window-child window-or-frame))
5375 window-or-frame)
5377 (error "Not a window or frame %s" window-or-frame))))
5378 (frame (window-frame window)))
5379 ;; Balance vertically.
5380 (window--resize-reset (window-frame window))
5381 (balance-windows-1 window)
5382 (when (window--resize-apply-p frame)
5383 (window-resize-apply frame)
5384 (window--pixel-to-total frame)
5385 (run-window-configuration-change-hook frame))
5386 ;; Balance horizontally.
5387 (window--resize-reset (window-frame window) t)
5388 (balance-windows-1 window t)
5389 (when (window--resize-apply-p frame t)
5390 (window-resize-apply frame t)
5391 (window--pixel-to-total frame t)
5392 (run-window-configuration-change-hook frame))))
5394 (defun window-fixed-size-p (&optional window direction)
5395 "Return t if WINDOW cannot be resized in DIRECTION.
5396 WINDOW defaults to the selected window. DIRECTION can be
5397 nil (i.e. any), `height' or `width'."
5398 (with-current-buffer (window-buffer window)
5399 (when (and (boundp 'window-size-fixed) window-size-fixed)
5400 (not (and direction
5401 (member (cons direction window-size-fixed)
5402 '((height . width) (width . height))))))))
5404 ;;; A different solution to balance-windows.
5405 (defvar window-area-factor 1
5406 "Factor by which the window area should be over-estimated.
5407 This is used by `balance-windows-area'.
5408 Changing this globally has no effect.")
5409 (make-variable-buffer-local 'window-area-factor)
5411 (defun balance-windows-area-adjust (window delta horizontal pixelwise)
5412 "Wrapper around `window-resize' with error checking.
5413 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
5414 ;; `window-resize' may fail if delta is too large.
5415 (while (>= (abs delta) 1)
5416 (condition-case nil
5417 (progn
5418 ;; It was wrong to use `window-resize' here. Somehow
5419 ;; `balance-windows-area' depends on resizing windows
5420 ;; asymmetrically.
5421 (adjust-window-trailing-edge window delta horizontal pixelwise)
5422 (setq delta 0))
5423 (error
5424 ;;(message "adjust: %s" (error-message-string err))
5425 (setq delta (/ delta 2))))))
5427 (defun balance-windows-area ()
5428 "Make all visible windows the same area (approximately).
5429 See also `window-area-factor' to change the relative size of
5430 specific buffers."
5431 (interactive)
5432 (let* ((unchanged 0) (carry 0) (round 0)
5433 ;; Remove fixed-size windows.
5434 (wins (delq nil (mapcar (lambda (win)
5435 (if (not (window-fixed-size-p win)) win))
5436 (window-list nil 'nomini))))
5437 (changelog nil)
5438 (pixelwise window-resize-pixelwise)
5439 next)
5440 ;; Resizing a window changes the size of surrounding windows in complex
5441 ;; ways, so it's difficult to balance them all. The introduction of
5442 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
5443 ;; very difficult to do. `balance-window' above takes an off-line
5444 ;; approach: get the whole window tree, then balance it, then try to
5445 ;; adjust the windows so they fit the result.
5446 ;; Here, instead, we take a "local optimization" approach, where we just
5447 ;; go through all the windows several times until nothing needs to be
5448 ;; changed. The main problem with this approach is that it's difficult
5449 ;; to make sure it terminates, so we use some heuristic to try and break
5450 ;; off infinite loops.
5451 ;; After a round without any change, we allow a second, to give a chance
5452 ;; to the carry to propagate a minor imbalance from the end back to
5453 ;; the beginning.
5454 (while (< unchanged 2)
5455 ;; (message "New round")
5456 (setq unchanged (1+ unchanged) round (1+ round))
5457 (dolist (win wins)
5458 (setq next win)
5459 (while (progn (setq next (next-window next))
5460 (window-fixed-size-p next)))
5461 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
5462 (let* ((horiz
5463 (< (car (window-pixel-edges win)) (car (window-pixel-edges next))))
5464 (areadiff (/ (- (* (window-size next nil pixelwise)
5465 (window-size next t pixelwise)
5466 (buffer-local-value 'window-area-factor
5467 (window-buffer next)))
5468 (* (window-size win nil pixelwise)
5469 (window-size win t pixelwise)
5470 (buffer-local-value 'window-area-factor
5471 (window-buffer win))))
5472 (max (buffer-local-value 'window-area-factor
5473 (window-buffer win))
5474 (buffer-local-value 'window-area-factor
5475 (window-buffer next)))))
5476 (edgesize (if horiz
5477 (+ (window-size win nil pixelwise)
5478 (window-size next nil pixelwise))
5479 (+ (window-size win t pixelwise)
5480 (window-size next t pixelwise))))
5481 (diff (/ areadiff edgesize)))
5482 (when (zerop diff)
5483 ;; Maybe diff is actually closer to 1 than to 0.
5484 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
5485 (when (and (zerop diff) (not (zerop areadiff)))
5486 (setq diff (/ (+ areadiff carry) edgesize))
5487 ;; Change things smoothly.
5488 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
5489 (if (zerop diff)
5490 ;; Make sure negligible differences don't accumulate to
5491 ;; become significant.
5492 (setq carry (+ carry areadiff))
5493 ;; This used `adjust-window-trailing-edge' before and uses
5494 ;; `window-resize' now. Error wrapping is still needed.
5495 (balance-windows-area-adjust win diff horiz pixelwise)
5496 ;; (sit-for 0.5)
5497 (let ((change (cons win (window-pixel-edges win))))
5498 ;; If the same change has been seen already for this window,
5499 ;; we're most likely in an endless loop, so don't count it as
5500 ;; a change.
5501 (unless (member change changelog)
5502 (push change changelog)
5503 (setq unchanged 0 carry 0)))))))
5504 ;; We've now basically balanced all the windows.
5505 ;; But there may be some minor off-by-one imbalance left over,
5506 ;; so let's do some fine tuning.
5507 ;; (bw-finetune wins)
5508 ;; (message "Done in %d rounds" round)
5511 ;;; Window states, how to get them and how to put them in a window.
5512 (defun window--state-get-1 (window &optional writable)
5513 "Helper function for `window-state-get'."
5514 (let* ((type
5515 (cond
5516 ((window-top-child window) 'vc)
5517 ((window-left-child window) 'hc)
5518 (t 'leaf)))
5519 (buffer (window-buffer window))
5520 (selected (eq window (selected-window)))
5521 (head
5522 `(,type
5523 ,@(unless (window-next-sibling window) `((last . t)))
5524 (pixel-width . ,(window-pixel-width window))
5525 (pixel-height . ,(window-pixel-height window))
5526 (total-width . ,(window-total-width window))
5527 (total-height . ,(window-total-height window))
5528 (normal-height . ,(window-normal-size window))
5529 (normal-width . ,(window-normal-size window t))
5530 ,@(unless (window-live-p window)
5531 `((combination-limit . ,(window-combination-limit window))))
5532 ,@(let ((parameters (window-parameters window))
5533 list)
5534 ;; Make copies of those window parameters whose
5535 ;; persistence property is `writable' if WRITABLE is
5536 ;; non-nil and non-nil if WRITABLE is nil.
5537 (dolist (par parameters)
5538 (let ((pers (cdr (assq (car par)
5539 window-persistent-parameters))))
5540 (when (and pers (or (not writable) (eq pers 'writable)))
5541 (setq list (cons (cons (car par) (cdr par)) list)))))
5542 ;; Add `clone-of' parameter if necessary.
5543 (let ((pers (cdr (assq 'clone-of
5544 window-persistent-parameters))))
5545 (when (and pers (or (not writable) (eq pers 'writable))
5546 (not (assq 'clone-of list)))
5547 (setq list (cons (cons 'clone-of window) list))))
5548 (when list
5549 `((parameters . ,list))))
5550 ,@(when buffer
5551 ;; All buffer related things go in here.
5552 (let ((point (window-point window))
5553 (start (window-start window)))
5554 `((buffer
5555 ,(buffer-name buffer)
5556 (selected . ,selected)
5557 (hscroll . ,(window-hscroll window))
5558 (fringes . ,(window-fringes window))
5559 (margins . ,(window-margins window))
5560 (scroll-bars . ,(window-scroll-bars window))
5561 (vscroll . ,(window-vscroll window))
5562 (dedicated . ,(window-dedicated-p window))
5563 (point . ,(if writable
5564 point
5565 (with-current-buffer buffer
5566 (copy-marker point
5567 (buffer-local-value
5568 'window-point-insertion-type
5569 buffer)))))
5570 (start . ,(if writable
5571 start
5572 (with-current-buffer buffer
5573 (copy-marker start))))))))))
5574 (tail
5575 (when (memq type '(vc hc))
5576 (let (list)
5577 (setq window (window-child window))
5578 (while window
5579 (setq list (cons (window--state-get-1 window writable) list))
5580 (setq window (window-right window)))
5581 (nreverse list)))))
5582 (append head tail)))
5584 (defun window-state-get (&optional window writable)
5585 "Return state of WINDOW as a Lisp object.
5586 WINDOW can be any window and defaults to the root window of the
5587 selected frame.
5589 Optional argument WRITABLE non-nil means do not use markers for
5590 sampling `window-point' and `window-start'. Together, WRITABLE
5591 and the variable `window-persistent-parameters' specify which
5592 window parameters are saved by this function. WRITABLE should be
5593 non-nil when the return value shall be written to a file and read
5594 back in another session. Otherwise, an application may run into
5595 an `invalid-read-syntax' error while attempting to read back the
5596 value from file.
5598 The return value can be used as argument for `window-state-put'
5599 to put the state recorded here into an arbitrary window. The
5600 value can be also stored on disk and read back in a new session."
5601 (setq window
5602 (if window
5603 (if (window-valid-p window)
5604 window
5605 (error "%s is not a live or internal window" window))
5606 (frame-root-window)))
5607 ;; The return value is a cons whose car specifies some constraints on
5608 ;; the size of WINDOW. The cdr lists the states of the child windows
5609 ;; of WINDOW.
5610 (cons
5611 ;; Frame related things would go into a function, say `frame-state',
5612 ;; calling `window-state-get' to insert the frame's root window.
5613 `((min-height . ,(window-min-size window))
5614 (min-width . ,(window-min-size window t))
5615 (min-height-ignore . ,(window-min-size window nil t))
5616 (min-width-ignore . ,(window-min-size window t t))
5617 (min-height-safe . ,(window-min-size window nil 'safe))
5618 (min-width-safe . ,(window-min-size window t 'safe))
5619 (min-pixel-height . ,(window-min-size window nil nil t))
5620 (min-pixel-width . ,(window-min-size window t nil t))
5621 (min-pixel-height-ignore . ,(window-min-size window nil t t))
5622 (min-pixel-width-ignore . ,(window-min-size window t t t))
5623 (min-pixel-height-safe . ,(window-min-size window nil 'safe t))
5624 (min-pixel-width-safe . ,(window-min-size window t 'safe t)))
5625 (window--state-get-1 window writable)))
5627 (defvar window-state-put-list nil
5628 "Helper variable for `window-state-put'.")
5630 (defvar window-state-put-stale-windows nil
5631 "Helper variable for `window-state-put'.")
5633 (defun window--state-put-1 (state &optional window ignore totals pixelwise)
5634 "Helper function for `window-state-put'."
5635 (let ((type (car state)))
5636 (setq state (cdr state))
5637 (cond
5638 ((eq type 'leaf)
5639 ;; For a leaf window just add unprocessed entries to
5640 ;; `window-state-put-list'.
5641 (push (cons window state) window-state-put-list))
5642 ((memq type '(vc hc))
5643 (let* ((horizontal (eq type 'hc))
5644 (total (window-size window horizontal pixelwise))
5645 (first t)
5646 (window-combination-limit (cdr (assq 'combination-limit state)))
5647 size new)
5648 (dolist (item state)
5649 ;; Find the next child window. WINDOW always points to the
5650 ;; real window that we want to fill with what we find here.
5651 (when (memq (car item) '(leaf vc hc))
5652 (if (assq 'last item)
5653 ;; The last child window. Below `window--state-put-1'
5654 ;; will put into it whatever ITEM has in store.
5655 (setq new nil)
5656 ;; Not the last child window, prepare for splitting
5657 ;; WINDOW. SIZE is the new (and final) size of the old
5658 ;; window.
5659 (setq size
5660 (if totals
5661 ;; Use total size.
5662 (if pixelwise
5663 (cdr (assq (if horizontal
5664 'pixel-width
5665 'pixel-height)
5666 item))
5667 (cdr (assq (if horizontal
5668 'total-width
5669 'total-height)
5670 item)))
5671 ;; Use normalized size and round.
5672 (round
5673 (* total
5674 (cdr (assq (if horizontal 'normal-width 'normal-height)
5675 item))))))
5677 ;; Use safe sizes, we try to resize later.
5678 (setq size (max size
5679 (if horizontal
5680 (* window-safe-min-width
5681 (if pixelwise
5682 (frame-char-width (window-frame window))
5684 (* window-safe-min-height
5685 (if pixelwise
5686 (frame-char-height (window-frame window))
5687 1)))))
5688 (if (window-sizable-p window (- size) horizontal 'safe pixelwise)
5689 (progn
5690 (setq new (split-window-no-error
5691 window size horizontal pixelwise))
5692 (setq window-combination-limit nil))
5693 ;; Give up if we can't resize window down to safe sizes.
5694 (error "Cannot resize window %s" window))
5696 (when first
5697 (setq first nil)
5698 ;; When creating the first child window add for parent
5699 ;; unprocessed entries to `window-state-put-list'.
5700 (setq window-state-put-list
5701 (cons (cons (window-parent window) state)
5702 window-state-put-list))))
5704 ;; Now process the current window (either the one we've just
5705 ;; split or the last child of its parent).
5706 (window--state-put-1 item window ignore totals)
5707 ;; Continue with the last window split off.
5708 (setq window new))))))))
5710 (defun window--state-put-2 (ignore pixelwise)
5711 "Helper function for `window-state-put'."
5712 (dolist (item window-state-put-list)
5713 (let ((window (car item))
5714 (combination-limit (cdr (assq 'combination-limit item)))
5715 (parameters (cdr (assq 'parameters item)))
5716 (state (cdr (assq 'buffer item))))
5717 (when combination-limit
5718 (set-window-combination-limit window combination-limit))
5719 ;; Reset window's parameters and assign saved ones (we might want
5720 ;; a `remove-window-parameters' function here).
5721 (dolist (parameter (window-parameters window))
5722 (set-window-parameter window (car parameter) nil))
5723 (when parameters
5724 (dolist (parameter parameters)
5725 (set-window-parameter window (car parameter) (cdr parameter))))
5726 ;; Process buffer related state.
5727 (when state
5728 (let ((buffer (get-buffer (car state))))
5729 (if buffer
5730 (with-current-buffer buffer
5731 (set-window-buffer window buffer)
5732 (set-window-hscroll window (cdr (assq 'hscroll state)))
5733 (apply 'set-window-fringes
5734 (cons window (cdr (assq 'fringes state))))
5735 (let ((margins (cdr (assq 'margins state))))
5736 (set-window-margins window (car margins) (cdr margins)))
5737 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
5738 (set-window-scroll-bars
5739 window (car scroll-bars) (nth 2 scroll-bars)
5740 (nth 3 scroll-bars) (nth 5 scroll-bars)))
5741 (set-window-vscroll window (cdr (assq 'vscroll state)))
5742 ;; Adjust vertically.
5743 (if (or (memq window-size-fixed '(t height))
5744 (window-preserved-size window))
5745 ;; A fixed height window, try to restore the
5746 ;; original size.
5747 (let ((delta
5748 (- (cdr (assq
5749 (if pixelwise 'pixel-height 'total-height)
5750 item))
5751 (window-size window nil pixelwise)))
5752 window-size-fixed)
5753 (when (window--resizable-p
5754 window delta nil nil nil nil nil pixelwise)
5755 (window-resize window delta nil nil pixelwise)))
5756 ;; Else check whether the window is not high enough.
5757 (let* ((min-size
5758 (window-min-size window nil ignore pixelwise))
5759 (delta
5760 (- min-size (window-size window nil pixelwise))))
5761 (when (and (> delta 0)
5762 (window--resizable-p
5763 window delta nil ignore nil nil nil pixelwise))
5764 (window-resize window delta nil ignore pixelwise))))
5765 ;; Adjust horizontally.
5766 (if (or (memq window-size-fixed '(t width))
5767 (window-preserved-size window t))
5768 ;; A fixed width window, try to restore the original
5769 ;; size.
5770 (let ((delta
5771 (- (cdr (assq
5772 (if pixelwise 'pixel-width 'total-width)
5773 item))
5774 (window-size window t pixelwise)))
5775 window-size-fixed)
5776 (when (window--resizable-p
5777 window delta t nil nil nil nil pixelwise)
5778 (window-resize window delta t nil pixelwise)))
5779 ;; Else check whether the window is not wide enough.
5780 (let* ((min-size (window-min-size window t ignore pixelwise))
5781 (delta (- min-size (window-size window t pixelwise))))
5782 (when (and (> delta 0)
5783 (window--resizable-p
5784 window delta t ignore nil nil nil pixelwise))
5785 (window-resize window delta t ignore pixelwise))))
5786 ;; Set dedicated status.
5787 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
5788 ;; Install positions (maybe we should do this after all
5789 ;; windows have been created and sized).
5790 (ignore-errors
5791 ;; Set 'noforce argument to avoid that window start
5792 ;; overrides window point set below (Bug#24240).
5793 (set-window-start window (cdr (assq 'start state)) 'noforce)
5794 (set-window-point window (cdr (assq 'point state))))
5795 ;; Select window if it's the selected one.
5796 (when (cdr (assq 'selected state))
5797 (select-window window)))
5798 ;; We don't want to raise an error in case the buffer does
5799 ;; not exist anymore, so we switch to a previous one and
5800 ;; save the window with the intention of deleting it later
5801 ;; if possible.
5802 (switch-to-prev-buffer window)
5803 (push window window-state-put-stale-windows)))))))
5805 (defun window-state-put (state &optional window ignore)
5806 "Put window state STATE into WINDOW.
5807 STATE should be the state of a window returned by an earlier
5808 invocation of `window-state-get'. Optional argument WINDOW must
5809 specify a valid window and defaults to the selected one. If
5810 WINDOW is not live, replace WINDOW by a live one before putting
5811 STATE into it.
5813 Optional argument IGNORE non-nil means ignore minimum window
5814 sizes and fixed size restrictions. IGNORE equal `safe' means
5815 windows can get as small as `window-safe-min-height' and
5816 `window-safe-min-width'."
5817 (setq window-state-put-stale-windows nil)
5818 (setq window (window-normalize-window window))
5820 ;; When WINDOW is internal, reduce it to a live one to put STATE into,
5821 ;; see Bug#16793.
5822 (unless (window-live-p window)
5823 (let ((root window))
5824 (setq window (catch 'live
5825 (walk-window-subtree
5826 (lambda (window)
5827 (when (and (window-live-p window)
5828 (not (window-parameter window 'window-side)))
5829 (throw 'live window)))
5830 root)))
5831 (delete-other-windows-internal window root)))
5833 (set-window-dedicated-p window nil)
5835 (let* ((frame (window-frame window))
5836 (head (car state))
5837 ;; We check here (1) whether the total sizes of root window of
5838 ;; STATE and that of WINDOW are equal so we can avoid
5839 ;; calculating new sizes, and (2) if we do have to resize
5840 ;; whether we can do so without violating size restrictions.
5841 (pixelwise (and (cdr (assq 'pixel-width state))
5842 (cdr (assq 'pixel-height state))))
5843 (totals (or (and pixelwise
5844 (= (window-pixel-width window)
5845 (cdr (assq 'pixel-width state)))
5846 (= (window-pixel-height window)
5847 (cdr (assq 'pixel-height state))))
5848 (and (= (window-total-width window)
5849 (cdr (assq 'total-width state)))
5850 (= (window-total-height window)
5851 (cdr (assq 'total-height state))))))
5852 (min-height (cdr (assq
5853 (if pixelwise 'min-pixel-height 'min-height)
5854 head)))
5855 (min-width (cdr (assq
5856 (if pixelwise 'min-pixel-width 'min-weight)
5857 head))))
5858 (if (and (not totals)
5859 (or (> min-height (window-size window nil pixelwise))
5860 (> min-width (window-size window t pixelwise)))
5861 (or (not ignore)
5862 (and (setq min-height
5863 (cdr (assq
5864 (if pixelwise
5865 'min-pixel-height-ignore
5866 'min-height-ignore)
5867 head)))
5868 (setq min-width
5869 (cdr (assq
5870 (if pixelwise
5871 'min-pixel-width-ignore
5872 'min-width-ignore)
5873 head)))
5874 (or (> min-height
5875 (window-size window nil pixelwise))
5876 (> min-width
5877 (window-size window t pixelwise)))
5878 (or (not (eq ignore 'safe))
5879 (and (setq min-height
5880 (cdr (assq
5881 (if pixelwise
5882 'min-pixel-height-safe
5883 'min-height-safe)
5884 head)))
5885 (setq min-width
5886 (cdr (assq
5887 (if pixelwise
5888 'min-pixel-width-safe
5889 'min-width-safe)
5890 head)))
5891 (or (> min-height
5892 (window-size window nil pixelwise))
5893 (> min-width
5894 (window-size window t pixelwise))))))))
5895 ;; The check above might not catch all errors due to rounding
5896 ;; issues - so IGNORE equal 'safe might not always produce the
5897 ;; minimum possible state. But such configurations hardly make
5898 ;; sense anyway.
5899 (error "Window %s too small to accommodate state" window)
5900 (setq state (cdr state))
5901 (setq window-state-put-list nil)
5902 ;; Work on the windows of a temporary buffer to make sure that
5903 ;; splitting proceeds regardless of any buffer local values of
5904 ;; `window-size-fixed'. Release that buffer after the buffers of
5905 ;; all live windows have been set by `window--state-put-2'.
5906 (with-temp-buffer
5907 (set-window-buffer window (current-buffer))
5908 (window--state-put-1 state window nil totals pixelwise)
5909 (window--state-put-2 ignore pixelwise))
5910 (while window-state-put-stale-windows
5911 (let ((window (pop window-state-put-stale-windows)))
5912 (when (eq (window-deletable-p window) t)
5913 (delete-window window))))
5914 (window--check frame))))
5916 (defun window-swap-states (&optional window-1 window-2 size)
5917 "Swap the states of live windows WINDOW-1 and WINDOW-2.
5918 WINDOW-1 must specify a live window and defaults to the selected
5919 one. WINDOW-2 must specify a live window and defaults to the
5920 window following WINDOW-1 in the cyclic ordering of windows,
5921 excluding minibuffer windows and including live windows on all
5922 visible frames.
5924 Optional argument SIZE non-nil means to try swapping the sizes of
5925 WINDOW-1 and WINDOW-2 as well. A value of `height' means to swap
5926 heights only, a value of `width' means to swap widths only, while
5927 t means to swap both widths and heights, if possible. Frames are
5928 not resized by this function."
5929 (interactive)
5930 (setq window-1 (window-normalize-window window-1 t))
5931 (if window-2
5932 (unless (window-live-p window-2)
5933 (error "%s is not a live window" window-2))
5934 (setq window-2 (next-window window-1 'nomini 'visible)))
5935 (unless (eq window-1 window-2)
5936 (let* ((height (memq size '(t height)))
5937 (width (memq size '(t width)))
5938 (state-1 (window-state-get window-1))
5939 (width-1 (and width (window-text-width window-1 t)))
5940 (height-1 (and height (window-text-height window-1 t)))
5941 (state-2 (window-state-get window-2))
5942 (width-2 (and width (window-text-width window-2 t)))
5943 (height-2 (and height (window-text-height window-2 t)))
5944 old preserved)
5945 ;; Swap basic states.
5946 (window-state-put state-1 window-2 t)
5947 (window-state-put state-2 window-1 t)
5948 ;; Swap overlays with `window' property.
5949 (with-current-buffer (window-buffer window-1)
5950 (dolist (overlay (overlays-in (point-min) (point-max)))
5951 (let ((window (overlay-get overlay 'window)))
5952 (cond
5953 ((not window))
5954 ((eq window window-1)
5955 (overlay-put overlay 'window window-2))
5956 ((eq window window-2)
5957 (overlay-put overlay 'window window-1))))))
5958 (unless (eq (window-buffer window-1) (window-buffer window-2))
5959 (with-current-buffer (window-buffer window-2)
5960 (dolist (overlay (overlays-in (point-min) (point-max)))
5961 (let ((window (overlay-get overlay 'window)))
5962 (cond
5963 ((not window))
5964 ((eq window window-1)
5965 (overlay-put overlay 'window window-2))
5966 ((eq window window-2)
5967 (overlay-put overlay 'window window-1)))))))
5968 ;; Try to swap window sizes.
5969 (when size
5970 (unless (= (setq old (window-text-width window-1 t)) width-2)
5971 (window-resize-no-error window-1 (- width-2 old) t t t))
5972 (unless (= (setq old (window-text-width window-2 t)) width-1)
5973 (setq preserved (window-preserved-size window-1 t))
5974 (window-preserve-size window-1 t t)
5975 (window-resize-no-error window-2 (- width-1 old) t t t)
5976 (window-preserve-size window-1 t preserved))
5977 (unless (= (setq old (window-text-height window-1 t)) height-2)
5978 (window-resize-no-error window-1 (- height-2 old) nil t t))
5979 (unless (= (setq old (window-text-height window-2 t)) height-1)
5980 (setq preserved (window-preserved-size window-1))
5981 (window-preserve-size window-1 nil t)
5982 (window-resize-no-error window-2 (- height-1 old) nil t t)
5983 (window-preserve-size window-1 nil preserved))))))
5985 (defun display-buffer-record-window (type window buffer)
5986 "Record information for window used by `display-buffer'.
5987 TYPE specifies the type of the calling operation and must be one
5988 of the symbols `reuse' (when WINDOW existed already and was
5989 reused for displaying BUFFER), `window' (when WINDOW was created
5990 on an already existing frame), or `frame' (when WINDOW was
5991 created on a new frame). WINDOW is the window used for or created
5992 by the `display-buffer' routines. BUFFER is the buffer that
5993 shall be displayed.
5995 This function installs or updates the quit-restore parameter of
5996 WINDOW. The quit-restore parameter is a list of four elements:
5997 The first element is one of the symbols `window', `frame', `same' or
5998 `other'. The second element is either one of the symbols `window'
5999 or `frame' or a list whose elements are the buffer previously
6000 shown in the window, that buffer's window start and window point,
6001 and the window's height. The third element is the window
6002 selected at the time the parameter was created. The fourth
6003 element is BUFFER."
6004 (cond
6005 ((eq type 'reuse)
6006 (if (eq (window-buffer window) buffer)
6007 ;; WINDOW shows BUFFER already. Update WINDOW's quit-restore
6008 ;; parameter, if any.
6009 (let ((quit-restore (window-parameter window 'quit-restore)))
6010 (when (consp quit-restore)
6011 (setcar quit-restore 'same)
6012 ;; The selected-window might have changed in
6013 ;; between (Bug#20353).
6014 (unless (or (eq window (selected-window))
6015 (eq window (nth 2 quit-restore)))
6016 (setcar (cddr quit-restore) (selected-window)))))
6017 ;; WINDOW shows another buffer.
6018 (with-current-buffer (window-buffer window)
6019 (set-window-parameter
6020 window 'quit-restore
6021 (list 'other
6022 ;; A quadruple of WINDOW's buffer, start, point and height.
6023 (list (current-buffer) (window-start window)
6024 ;; Preserve window-point-insertion-type (Bug#12588).
6025 (copy-marker
6026 (window-point window) window-point-insertion-type)
6027 (if (window-combined-p window)
6028 (window-total-height window)
6029 (window-total-width window)))
6030 (selected-window) buffer)))))
6031 ((eq type 'window)
6032 ;; WINDOW has been created on an existing frame.
6033 (set-window-parameter
6034 window 'quit-restore
6035 (list 'window 'window (selected-window) buffer)))
6036 ((eq type 'frame)
6037 ;; WINDOW has been created on a new frame.
6038 (set-window-parameter
6039 window 'quit-restore
6040 (list 'frame 'frame (selected-window) buffer)))))
6042 (defcustom display-buffer-function nil
6043 "If non-nil, function to call to handle `display-buffer'.
6044 It will receive two args, the buffer and a flag which if non-nil
6045 means that the currently selected window is not acceptable. It
6046 should choose or create a window, display the specified buffer in
6047 it, and return the window.
6049 The specified function should call `display-buffer-record-window'
6050 with corresponding arguments to set up the quit-restore parameter
6051 of the window used."
6052 :type '(choice
6053 (const nil)
6054 (function :tag "function"))
6055 :group 'windows)
6057 (make-obsolete-variable 'display-buffer-function
6058 'display-buffer-alist "24.3")
6060 ;; Eventually, we want to turn this into a defvar; instead of
6061 ;; customizing this, the user should use a `pop-up-frame-parameters'
6062 ;; alist entry in `display-buffer-base-action'.
6063 (defcustom pop-up-frame-alist nil
6064 "Alist of parameters for automatically generated new frames.
6065 If non-nil, the value you specify here is used by the default
6066 `pop-up-frame-function' for the creation of new frames.
6068 Since `pop-up-frame-function' is used by `display-buffer' for
6069 making new frames, any value specified here by default affects
6070 the automatic generation of new frames via `display-buffer' and
6071 all functions based on it. The behavior of `make-frame' is not
6072 affected by this variable."
6073 :type '(repeat (cons :format "%v"
6074 (symbol :tag "Parameter")
6075 (sexp :tag "Value")))
6076 :group 'frames)
6078 (defcustom pop-up-frame-function
6079 (lambda () (make-frame pop-up-frame-alist))
6080 "Function used by `display-buffer' for creating a new frame.
6081 This function is called with no arguments and should return a new
6082 frame. The default value calls `make-frame' with the argument
6083 `pop-up-frame-alist'."
6084 :type 'function
6085 :group 'frames)
6087 (defcustom special-display-buffer-names nil
6088 "List of names of buffers that should be displayed specially.
6089 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
6090 its name is in this list, displays the buffer in a way specified
6091 by `special-display-function'. `special-display-popup-frame'
6092 \(the default for `special-display-function') usually displays
6093 the buffer in a separate frame made with the parameters specified
6094 by `special-display-frame-alist'. If `special-display-function'
6095 has been set to some other function, that function is called with
6096 the buffer as first, and nil as second argument.
6098 Alternatively, an element of this list can be specified as
6099 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
6100 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
6101 `special-display-popup-frame' will interpret such pairs as frame
6102 parameters when it creates a special frame, overriding the
6103 corresponding values from `special-display-frame-alist'.
6105 As a special case, if FRAME-PARAMETERS contains (same-window . t)
6106 `special-display-popup-frame' displays that buffer in the
6107 selected window. If FRAME-PARAMETERS contains (same-frame . t),
6108 it displays that buffer in a window on the selected frame.
6110 If `special-display-function' specifies some other function than
6111 `special-display-popup-frame', that function is called with the
6112 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
6113 argument.
6115 Finally, an element of this list can be also specified as
6116 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
6117 `special-display-popup-frame' will call FUNCTION with the buffer
6118 named BUFFER-NAME as first argument, and OTHER-ARGS as the
6119 second.
6121 Any alternative function specified here is responsible for
6122 setting up the quit-restore parameter of the window used.
6124 If this variable appears \"not to work\", because you added a
6125 name to it but the corresponding buffer is displayed in the
6126 selected window, look at the values of `same-window-buffer-names'
6127 and `same-window-regexps'. Those variables take precedence over
6128 this one.
6130 See also `special-display-regexps'."
6131 :type '(repeat
6132 (choice :tag "Buffer"
6133 :value ""
6134 (string :format "%v")
6135 (cons :tag "With parameters"
6136 :format "%v"
6137 :value ("" . nil)
6138 (string :format "%v")
6139 (repeat :tag "Parameters"
6140 (cons :format "%v"
6141 (symbol :tag "Parameter")
6142 (sexp :tag "Value"))))
6143 (list :tag "With function"
6144 :format "%v"
6145 :value ("" . nil)
6146 (string :format "%v")
6147 (function :tag "Function")
6148 (repeat :tag "Arguments" (sexp)))))
6149 :group 'windows
6150 :group 'frames)
6151 (make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3")
6152 (put 'special-display-buffer-names 'risky-local-variable t)
6154 (defcustom special-display-regexps nil
6155 "List of regexps saying which buffers should be displayed specially.
6156 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
6157 any regexp in this list matches its name, displays it specially
6158 using `special-display-function'. `special-display-popup-frame'
6159 \(the default for `special-display-function') usually displays
6160 the buffer in a separate frame made with the parameters specified
6161 by `special-display-frame-alist'. If `special-display-function'
6162 has been set to some other function, that function is called with
6163 the buffer as first, and nil as second argument.
6165 Alternatively, an element of this list can be specified as
6166 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
6167 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
6168 `special-display-popup-frame' will then interpret these pairs as
6169 frame parameters when creating a special frame for a buffer whose
6170 name matches REGEXP, overriding the corresponding values from
6171 `special-display-frame-alist'.
6173 As a special case, if FRAME-PARAMETERS contains (same-window . t)
6174 `special-display-popup-frame' displays buffers matching REGEXP in
6175 the selected window. (same-frame . t) in FRAME-PARAMETERS means
6176 to display such buffers in a window on the selected frame.
6178 If `special-display-function' specifies some other function than
6179 `special-display-popup-frame', that function is called with the
6180 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
6181 as second argument.
6183 Finally, an element of this list can be also specified as
6184 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
6185 will then call FUNCTION with the buffer whose name matched
6186 REGEXP as first, and OTHER-ARGS as second argument.
6188 Any alternative function specified here is responsible for
6189 setting up the quit-restore parameter of the window used.
6191 If this variable appears \"not to work\", because you added a
6192 name to it but the corresponding buffer is displayed in the
6193 selected window, look at the values of `same-window-buffer-names'
6194 and `same-window-regexps'. Those variables take precedence over
6195 this one.
6197 See also `special-display-buffer-names'."
6198 :type '(repeat
6199 (choice :tag "Buffer"
6200 :value ""
6201 (regexp :format "%v")
6202 (cons :tag "With parameters"
6203 :format "%v"
6204 :value ("" . nil)
6205 (regexp :format "%v")
6206 (repeat :tag "Parameters"
6207 (cons :format "%v"
6208 (symbol :tag "Parameter")
6209 (sexp :tag "Value"))))
6210 (list :tag "With function"
6211 :format "%v"
6212 :value ("" . nil)
6213 (regexp :format "%v")
6214 (function :tag "Function")
6215 (repeat :tag "Arguments" (sexp)))))
6216 :group 'windows
6217 :group 'frames)
6218 (make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3")
6219 (put 'special-display-regexps 'risky-local-variable t)
6221 (defun special-display-p (buffer-name)
6222 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
6223 More precisely, return t if `special-display-buffer-names' or
6224 `special-display-regexps' contain a string entry equaling or
6225 matching BUFFER-NAME. If `special-display-buffer-names' or
6226 `special-display-regexps' contain a list entry whose car equals
6227 or matches BUFFER-NAME, the return value is the cdr of that
6228 entry."
6229 (let (tmp)
6230 (cond
6231 ((member buffer-name special-display-buffer-names)
6233 ((setq tmp (assoc buffer-name special-display-buffer-names))
6234 (cdr tmp))
6235 ((catch 'found
6236 (dolist (regexp special-display-regexps)
6237 (cond
6238 ((stringp regexp)
6239 (when (string-match-p regexp buffer-name)
6240 (throw 'found t)))
6241 ((and (consp regexp) (stringp (car regexp))
6242 (string-match-p (car regexp) buffer-name))
6243 (throw 'found (cdr regexp))))))))))
6245 (defcustom special-display-frame-alist
6246 '((height . 14) (width . 80) (unsplittable . t))
6247 "Alist of parameters for special frames.
6248 Special frames are used for buffers whose names are listed in
6249 `special-display-buffer-names' and for buffers whose names match
6250 one of the regular expressions in `special-display-regexps'.
6252 This variable can be set in your init file, like this:
6254 (setq special-display-frame-alist \\='((width . 80) (height . 20)))
6256 These supersede the values given in `default-frame-alist'."
6257 :type '(repeat (cons :format "%v"
6258 (symbol :tag "Parameter")
6259 (sexp :tag "Value")))
6260 :group 'frames)
6261 (make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3")
6263 (defun special-display-popup-frame (buffer &optional args)
6264 "Pop up a frame displaying BUFFER and return its window.
6265 If BUFFER is already displayed in a visible or iconified frame,
6266 raise that frame. Otherwise, display BUFFER in a new frame.
6268 Optional argument ARGS is a list specifying additional
6269 information.
6271 If ARGS is an alist, use it as a list of frame parameters. If
6272 these parameters contain (same-window . t), display BUFFER in
6273 the selected window. If they contain (same-frame . t), display
6274 BUFFER in a window of the selected frame.
6276 If ARGS is a list whose car is a symbol, use (car ARGS) as a
6277 function to do the work. Pass it BUFFER as first argument, and
6278 pass the elements of (cdr ARGS) as the remaining arguments."
6279 (if (and args (symbolp (car args)))
6280 (apply (car args) buffer (cdr args))
6281 (let ((window (get-buffer-window buffer 0)))
6283 ;; If we have a window already, make it visible.
6284 (when window
6285 (let ((frame (window-frame window)))
6286 (make-frame-visible frame)
6287 (raise-frame frame)
6288 (display-buffer-record-window 'reuse window buffer)
6289 window))
6290 ;; Reuse the current window if the user requested it.
6291 (when (cdr (assq 'same-window args))
6292 (condition-case nil
6293 (progn (switch-to-buffer buffer nil t) (selected-window))
6294 (error nil)))
6295 ;; Stay on the same frame if requested.
6296 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
6297 (let* ((pop-up-windows t)
6298 pop-up-frames
6299 special-display-buffer-names special-display-regexps)
6300 (display-buffer buffer)))
6301 ;; If no window yet, make one in a new frame.
6302 (let* ((frame
6303 (with-current-buffer buffer
6304 (make-frame (append args special-display-frame-alist))))
6305 (window (frame-selected-window frame)))
6306 (display-buffer-record-window 'frame window buffer)
6307 (unless (eq buffer (window-buffer window))
6308 (set-window-buffer window buffer)
6309 (set-window-prev-buffers window nil))
6310 (set-window-dedicated-p window t)
6311 window)))))
6313 (defcustom special-display-function 'special-display-popup-frame
6314 "Function to call for displaying special buffers.
6315 This function is called with two arguments - the buffer and,
6316 optionally, a list - and should return a window displaying that
6317 buffer. The default value usually makes a separate frame for the
6318 buffer using `special-display-frame-alist' to specify the frame
6319 parameters. See the definition of `special-display-popup-frame'
6320 for how to specify such a function.
6322 A buffer is special when its name is either listed in
6323 `special-display-buffer-names' or matches a regexp in
6324 `special-display-regexps'.
6326 The specified function should call `display-buffer-record-window'
6327 with corresponding arguments to set up the quit-restore parameter
6328 of the window used."
6329 :type 'function
6330 :group 'frames)
6331 (make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3")
6333 (defcustom same-window-buffer-names nil
6334 "List of names of buffers that should appear in the \"same\" window.
6335 `display-buffer' and `pop-to-buffer' show a buffer whose name is
6336 on this list in the selected rather than some other window.
6338 An element of this list can be a cons cell instead of just a
6339 string. In that case, the cell's car must be a string specifying
6340 the buffer name. This is for compatibility with
6341 `special-display-buffer-names'; the cdr of the cons cell is
6342 ignored.
6344 See also `same-window-regexps'."
6345 :type '(repeat (string :format "%v"))
6346 :group 'windows)
6348 (defcustom same-window-regexps nil
6349 "List of regexps saying which buffers should appear in the \"same\" window.
6350 `display-buffer' and `pop-to-buffer' show a buffer whose name
6351 matches a regexp on this list in the selected rather than some
6352 other window.
6354 An element of this list can be a cons cell instead of just a
6355 string. In that case, the cell's car must be a regexp matching
6356 the buffer name. This is for compatibility with
6357 `special-display-regexps'; the cdr of the cons cell is ignored.
6359 See also `same-window-buffer-names'."
6360 :type '(repeat (regexp :format "%v"))
6361 :group 'windows)
6363 (defun same-window-p (buffer-name)
6364 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
6365 This function returns non-nil if `display-buffer' or
6366 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
6367 selected rather than (as usual) some other window. See
6368 `same-window-buffer-names' and `same-window-regexps'."
6369 (cond
6370 ((not (stringp buffer-name)))
6371 ;; The elements of `same-window-buffer-names' can be buffer
6372 ;; names or cons cells whose cars are buffer names.
6373 ((member buffer-name same-window-buffer-names))
6374 ((assoc buffer-name same-window-buffer-names))
6375 ((catch 'found
6376 (dolist (regexp same-window-regexps)
6377 ;; The elements of `same-window-regexps' can be regexps
6378 ;; or cons cells whose cars are regexps.
6379 (when (or (and (stringp regexp)
6380 (string-match-p regexp buffer-name))
6381 (and (consp regexp) (stringp (car regexp))
6382 (string-match-p (car regexp) buffer-name)))
6383 (throw 'found t)))))))
6385 (defcustom pop-up-frames nil
6386 "Whether `display-buffer' should make a separate frame.
6387 If nil, never make a separate frame.
6388 If the value is `graphic-only', make a separate frame
6389 on graphic displays only.
6390 Any other non-nil value means always make a separate frame."
6391 :type '(choice
6392 (const :tag "Never" nil)
6393 (const :tag "On graphic displays only" graphic-only)
6394 (const :tag "Always" t))
6395 :group 'windows)
6397 (defcustom display-buffer-reuse-frames nil
6398 "Non-nil means `display-buffer' should reuse frames.
6399 If the buffer in question is already displayed in a frame, raise
6400 that frame."
6401 :type 'boolean
6402 :version "21.1"
6403 :group 'windows)
6405 (make-obsolete-variable
6406 'display-buffer-reuse-frames
6407 "use a `reusable-frames' alist entry in `display-buffer-alist'."
6408 "24.3")
6410 (defcustom pop-up-windows t
6411 "Non-nil means `display-buffer' should make a new window."
6412 :type 'boolean
6413 :group 'windows)
6415 (defcustom split-window-preferred-function 'split-window-sensibly
6416 "Function called by `display-buffer' routines to split a window.
6417 This function is called with a window as single argument and is
6418 supposed to split that window and return the new window. If the
6419 window can (or shall) not be split, it is supposed to return nil.
6420 The default is to call the function `split-window-sensibly' which
6421 tries to split the window in a way which seems most suitable.
6422 You can customize the options `split-height-threshold' and/or
6423 `split-width-threshold' in order to have `split-window-sensibly'
6424 prefer either vertical or horizontal splitting.
6426 If you set this to any other function, bear in mind that the
6427 `display-buffer' routines may call this function two times. The
6428 argument of the first call is the largest window on its frame.
6429 If that call fails to return a live window, the function is
6430 called again with the least recently used window as argument. If
6431 that call fails too, `display-buffer' will use an existing window
6432 to display its buffer.
6434 The window selected at the time `display-buffer' was invoked is
6435 still selected when this function is called. Hence you can
6436 compare the window argument with the value of `selected-window'
6437 if you intend to split the selected window instead or if you do
6438 not want to split the selected window."
6439 :type 'function
6440 :version "23.1"
6441 :group 'windows)
6443 (defcustom split-height-threshold 80
6444 "Minimum height for splitting windows sensibly.
6445 If this is an integer, `split-window-sensibly' may split a window
6446 vertically only if it has at least this many lines. If this is
6447 nil, `split-window-sensibly' is not allowed to split a window
6448 vertically. If, however, a window is the only window on its
6449 frame, `split-window-sensibly' may split it vertically
6450 disregarding the value of this variable."
6451 :type '(choice (const nil) (integer :tag "lines"))
6452 :version "23.1"
6453 :group 'windows)
6455 (defcustom split-width-threshold 160
6456 "Minimum width for splitting windows sensibly.
6457 If this is an integer, `split-window-sensibly' may split a window
6458 horizontally only if it has at least this many columns. If this
6459 is nil, `split-window-sensibly' is not allowed to split a window
6460 horizontally."
6461 :type '(choice (const nil) (integer :tag "columns"))
6462 :version "23.1"
6463 :group 'windows)
6465 (defun window-splittable-p (window &optional horizontal)
6466 "Return non-nil if `split-window-sensibly' may split WINDOW.
6467 Optional argument HORIZONTAL nil or omitted means check whether
6468 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
6469 non-nil means check whether WINDOW may be split horizontally.
6471 WINDOW may be split vertically when the following conditions
6472 hold:
6473 - `window-size-fixed' is either nil or equals `width' for the
6474 buffer of WINDOW.
6475 - `split-height-threshold' is an integer and WINDOW is at least as
6476 high as `split-height-threshold'.
6477 - When WINDOW is split evenly, the emanating windows are at least
6478 `window-min-height' lines tall and can accommodate at least one
6479 line plus - if WINDOW has one - a mode line.
6481 WINDOW may be split horizontally when the following conditions
6482 hold:
6483 - `window-size-fixed' is either nil or equals `height' for the
6484 buffer of WINDOW.
6485 - `split-width-threshold' is an integer and WINDOW is at least as
6486 wide as `split-width-threshold'.
6487 - When WINDOW is split evenly, the emanating windows are at least
6488 `window-min-width' or two (whichever is larger) columns wide."
6489 (when (and (window-live-p window)
6490 (not (window-parameter window 'window-side)))
6491 (with-current-buffer (window-buffer window)
6492 (if horizontal
6493 ;; A window can be split horizontally when its width is not
6494 ;; fixed, it is at least `split-width-threshold' columns wide
6495 ;; and at least twice as wide as `window-min-width' and 2 (the
6496 ;; latter value is hardcoded).
6497 (and (memq window-size-fixed '(nil height))
6498 ;; Testing `window-full-width-p' here hardly makes any
6499 ;; sense nowadays. This can be done more intuitively by
6500 ;; setting up `split-width-threshold' appropriately.
6501 (numberp split-width-threshold)
6502 (>= (window-width window)
6503 (max split-width-threshold
6504 (* 2 (max window-min-width 2)))))
6505 ;; A window can be split vertically when its height is not
6506 ;; fixed, it is at least `split-height-threshold' lines high,
6507 ;; and it is at least twice as high as `window-min-height' and 2
6508 ;; if it has a mode line or 1.
6509 (and (memq window-size-fixed '(nil width))
6510 (numberp split-height-threshold)
6511 (>= (window-height window)
6512 (max split-height-threshold
6513 (* 2 (max window-min-height
6514 (if mode-line-format 2 1))))))))))
6516 (defun split-window-sensibly (&optional window)
6517 "Split WINDOW in a way suitable for `display-buffer'.
6518 WINDOW defaults to the currently selected window.
6519 If `split-height-threshold' specifies an integer, WINDOW is at
6520 least `split-height-threshold' lines tall and can be split
6521 vertically, split WINDOW into two windows one above the other and
6522 return the lower window. Otherwise, if `split-width-threshold'
6523 specifies an integer, WINDOW is at least `split-width-threshold'
6524 columns wide and can be split horizontally, split WINDOW into two
6525 windows side by side and return the window on the right. If this
6526 can't be done either and WINDOW is the only window on its frame,
6527 try to split WINDOW vertically disregarding any value specified
6528 by `split-height-threshold'. If that succeeds, return the lower
6529 window. Return nil otherwise.
6531 By default `display-buffer' routines call this function to split
6532 the largest or least recently used window. To change the default
6533 customize the option `split-window-preferred-function'.
6535 You can enforce this function to not split WINDOW horizontally,
6536 by setting (or binding) the variable `split-width-threshold' to
6537 nil. If, in addition, you set `split-height-threshold' to zero,
6538 chances increase that this function does split WINDOW vertically.
6540 In order to not split WINDOW vertically, set (or bind) the
6541 variable `split-height-threshold' to nil. Additionally, you can
6542 set `split-width-threshold' to zero to make a horizontal split
6543 more likely to occur.
6545 Have a look at the function `window-splittable-p' if you want to
6546 know how `split-window-sensibly' determines whether WINDOW can be
6547 split."
6548 (let ((window (or window (selected-window))))
6549 (or (and (window-splittable-p window)
6550 ;; Split window vertically.
6551 (with-selected-window window
6552 (split-window-below)))
6553 (and (window-splittable-p window t)
6554 ;; Split window horizontally.
6555 (with-selected-window window
6556 (split-window-right)))
6557 (and (eq window (frame-root-window (window-frame window)))
6558 (not (window-minibuffer-p window))
6559 ;; If WINDOW is the only window on its frame and is not the
6560 ;; minibuffer window, try to split it vertically disregarding
6561 ;; the value of `split-height-threshold'.
6562 (let ((split-height-threshold 0))
6563 (when (window-splittable-p window)
6564 (with-selected-window window
6565 (split-window-below))))))))
6567 (defun window--try-to-split-window (window &optional alist)
6568 "Try to split WINDOW.
6569 Return value returned by `split-window-preferred-function' if it
6570 represents a live window, nil otherwise."
6571 (and (window-live-p window)
6572 (not (frame-parameter (window-frame window) 'unsplittable))
6573 (let* ((window-combination-limit
6574 ;; When `window-combination-limit' equals
6575 ;; `display-buffer' or equals `resize-window' and a
6576 ;; `window-height' or `window-width' alist entry are
6577 ;; present, bind it to t so resizing steals space
6578 ;; preferably from the window that was split.
6579 (if (or (eq window-combination-limit 'display-buffer)
6580 (and (eq window-combination-limit 'window-size)
6581 (or (cdr (assq 'window-height alist))
6582 (cdr (assq 'window-width alist)))))
6584 window-combination-limit))
6585 (new-window
6586 ;; Since `split-window-preferred-function' might
6587 ;; throw an error use `condition-case'.
6588 (condition-case nil
6589 (funcall split-window-preferred-function window)
6590 (error nil))))
6591 (and (window-live-p new-window) new-window))))
6593 (defun window--frame-usable-p (frame)
6594 "Return FRAME if it can be used to display a buffer."
6595 (when (frame-live-p frame)
6596 (let ((window (frame-root-window frame)))
6597 ;; `frame-root-window' may be an internal window which is considered
6598 ;; "dead" by `window-live-p'. Hence if `window' is not live we
6599 ;; implicitly know that `frame' has a visible window we can use.
6600 (unless (and (window-live-p window)
6601 (or (window-minibuffer-p window)
6602 ;; If the window is soft-dedicated, the frame is usable.
6603 ;; Actually, even if the window is really dedicated,
6604 ;; the frame is still usable by splitting it.
6605 ;; At least Emacs-22 allowed it, and it is desirable
6606 ;; when displaying same-frame windows.
6607 nil ; (eq t (window-dedicated-p window))
6609 frame))))
6611 (defcustom even-window-sizes t
6612 "If non-nil `display-buffer' will try to even window sizes.
6613 Otherwise `display-buffer' will leave the window configuration
6614 alone. Special values are `height-only' to even heights only and
6615 `width-only' to even widths only. Any other value means to even
6616 any of them."
6617 :type '(choice
6618 (const :tag "Never" nil)
6619 (const :tag "Side-by-side windows only" width-only)
6620 (const :tag "Windows above or below only" height-only)
6621 (const :tag "Always" t))
6622 :version "25.1"
6623 :group 'windows)
6624 (defvaralias 'even-window-heights 'even-window-sizes)
6626 (defun window--even-window-sizes (window)
6627 "Even sizes of WINDOW and selected window.
6628 Even only if these windows are the only children of their parent,
6629 `even-window-sizes' has the appropriate value and the selected
6630 window is larger than WINDOW."
6631 (when (and (= (window-child-count (window-parent window)) 2)
6632 (eq (window-parent) (window-parent window)))
6633 (cond
6634 ((and (not (memq even-window-sizes '(nil height-only)))
6635 (window-combined-p window t)
6636 (> (window-total-width) (window-total-width window)))
6637 (condition-case nil
6638 (enlarge-window
6639 (/ (- (window-total-width window) (window-total-width)) 2) t)
6640 (error nil)))
6641 ((and (not (memq even-window-sizes '(nil width-only)))
6642 (window-combined-p window)
6643 (> (window-total-height) (window-total-height window)))
6644 (condition-case nil
6645 (enlarge-window
6646 (/ (- (window-total-height window) (window-total-height)) 2))
6647 (error nil))))))
6649 (defun window--display-buffer (buffer window type &optional alist dedicated)
6650 "Display BUFFER in WINDOW.
6651 TYPE must be one of the symbols `reuse', `window' or `frame' and
6652 is passed unaltered to `display-buffer-record-window'. ALIST is
6653 the alist argument of `display-buffer'. Set `window-dedicated-p'
6654 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
6655 live."
6656 (when (and (buffer-live-p buffer) (window-live-p window))
6657 (display-buffer-record-window type window buffer)
6658 (unless (eq buffer (window-buffer window))
6659 (set-window-dedicated-p window nil)
6660 (set-window-buffer window buffer))
6661 (when dedicated
6662 (set-window-dedicated-p window dedicated))
6663 (when (memq type '(window frame))
6664 (set-window-prev-buffers window nil))
6665 (let ((quit-restore (window-parameter window 'quit-restore))
6666 (height (cdr (assq 'window-height alist)))
6667 (width (cdr (assq 'window-width alist)))
6668 (size (cdr (assq 'window-size alist)))
6669 (preserve-size (cdr (assq 'preserve-size alist))))
6670 (cond
6671 ((or (eq type 'frame)
6672 (and (eq (car quit-restore) 'same)
6673 (eq (nth 1 quit-restore) 'frame)))
6674 ;; Adjust size of frame if asked for.
6675 (cond
6676 ((not size))
6677 ((consp size)
6678 (let ((width (car size))
6679 (height (cdr size))
6680 (frame (window-frame window)))
6681 (when (and (numberp width) (numberp height))
6682 (set-frame-height
6683 frame (+ (frame-height frame)
6684 (- height (window-total-height window))))
6685 (set-frame-width
6686 frame (+ (frame-width frame)
6687 (- width (window-total-width window)))))))
6688 ((functionp size)
6689 (ignore-errors (funcall size window)))))
6690 ((or (eq type 'window)
6691 (and (eq (car quit-restore) 'same)
6692 (eq (nth 1 quit-restore) 'window)))
6693 ;; Adjust height of window if asked for.
6694 (cond
6695 ((not height))
6696 ((numberp height)
6697 (let* ((new-height
6698 (if (integerp height)
6699 height
6700 (round
6701 (* (window-total-height (frame-root-window window))
6702 height))))
6703 (delta (- new-height (window-total-height window))))
6704 (when (and (window--resizable-p window delta nil 'safe)
6705 (window-combined-p window))
6706 (window-resize window delta nil 'safe))))
6707 ((functionp height)
6708 (ignore-errors (funcall height window))))
6709 ;; Adjust width of window if asked for.
6710 (cond
6711 ((not width))
6712 ((numberp width)
6713 (let* ((new-width
6714 (if (integerp width)
6715 width
6716 (round
6717 (* (window-total-width (frame-root-window window))
6718 width))))
6719 (delta (- new-width (window-total-width window))))
6720 (when (and (window--resizable-p window delta t 'safe)
6721 (window-combined-p window t))
6722 (window-resize window delta t 'safe))))
6723 ((functionp width)
6724 (ignore-errors (funcall width window))))
6725 ;; Preserve window size if asked for.
6726 (when (consp preserve-size)
6727 (window-preserve-size window t (car preserve-size))
6728 (window-preserve-size window nil (cdr preserve-size)))))
6729 ;; Assign any window parameters specified.
6730 (let ((parameters (cdr (assq 'window-parameters alist))))
6731 (dolist (parameter parameters)
6732 (set-window-parameter
6733 window (car parameter) (cdr parameter)))))
6734 window))
6736 (defun window--maybe-raise-frame (frame)
6737 (let ((visible (frame-visible-p frame)))
6738 (unless (or (not visible)
6739 ;; Assume the selected frame is already visible enough.
6740 (eq frame (selected-frame))
6741 ;; Assume the frame from which we invoked the
6742 ;; minibuffer is visible.
6743 (and (minibuffer-window-active-p (selected-window))
6744 (eq frame (window-frame (minibuffer-selected-window)))))
6745 (raise-frame frame))))
6747 ;; FIXME: Not implemented.
6748 ;; FIXME: By the way, there could be more levels of dedication:
6749 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
6750 ;; the window hasn't been used for something else yet.
6751 ;; - `soft' (`softly') dedicated only allows reuse when asked explicitly.
6752 ;; - `strongly' never allows reuse.
6753 (defvar display-buffer-mark-dedicated nil
6754 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
6755 The actual non-nil value of this variable will be copied to the
6756 `window-dedicated-p' flag.")
6758 (defconst display-buffer--action-function-custom-type
6759 '(choice :tag "Function"
6760 (const :tag "--" ignore) ; default for insertion
6761 (const display-buffer-reuse-window)
6762 (const display-buffer-pop-up-window)
6763 (const display-buffer-same-window)
6764 (const display-buffer-pop-up-frame)
6765 (const display-buffer-below-selected)
6766 (const display-buffer-at-bottom)
6767 (const display-buffer-in-previous-window)
6768 (const display-buffer-use-some-window)
6769 (const display-buffer-use-some-frame)
6770 (function :tag "Other function"))
6771 "Custom type for `display-buffer' action functions.")
6773 (defconst display-buffer--action-custom-type
6774 `(cons :tag "Action"
6775 (choice :tag "Action functions"
6776 ,display-buffer--action-function-custom-type
6777 (repeat
6778 :tag "List of functions"
6779 ,display-buffer--action-function-custom-type))
6780 (alist :tag "Action arguments"
6781 :key-type symbol
6782 :value-type (sexp :tag "Value")))
6783 "Custom type for `display-buffer' actions.")
6785 (defvar display-buffer-overriding-action '(nil . nil)
6786 "Overriding action to perform to display a buffer.
6787 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6788 function or a list of functions. Each function should accept two
6789 arguments: a buffer to display and an alist similar to ALIST.
6790 See `display-buffer' for details.")
6791 (put 'display-buffer-overriding-action 'risky-local-variable t)
6793 (defcustom display-buffer-alist nil
6794 "Alist of conditional actions for `display-buffer'.
6795 This is a list of elements (CONDITION . ACTION), where:
6797 CONDITION is either a regexp matching buffer names, or a
6798 function that takes two arguments - a buffer name and the
6799 ACTION argument of `display-buffer' - and returns a boolean.
6801 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
6802 function or a list of functions. Each such function should
6803 accept two arguments: a buffer to display and an alist of the
6804 same form as ALIST. See `display-buffer' for details.
6806 `display-buffer' scans this alist until it either finds a
6807 matching regular expression or the function specified by a
6808 condition returns non-nil. In any of these cases, it adds the
6809 associated action to the list of actions it will try."
6810 :type `(alist :key-type
6811 (choice :tag "Condition"
6812 regexp
6813 (function :tag "Matcher function"))
6814 :value-type ,display-buffer--action-custom-type)
6815 :risky t
6816 :version "24.1"
6817 :group 'windows)
6819 (defcustom display-buffer-base-action '(nil . nil)
6820 "User-specified default action for `display-buffer'.
6821 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6822 function or a list of functions. Each function should accept two
6823 arguments: a buffer to display and an alist similar to ALIST.
6824 See `display-buffer' for details."
6825 :type display-buffer--action-custom-type
6826 :risky t
6827 :version "24.1"
6828 :group 'windows)
6830 (defconst display-buffer-fallback-action
6831 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
6832 display-buffer-reuse-window
6833 display-buffer--maybe-pop-up-frame-or-window
6834 display-buffer-in-previous-window
6835 display-buffer-use-some-window
6836 ;; If all else fails, pop up a new frame.
6837 display-buffer-pop-up-frame))
6838 "Default fallback action for `display-buffer'.
6839 This is the action used by `display-buffer' if no other actions
6840 specified, e.g. by the user options `display-buffer-alist' or
6841 `display-buffer-base-action'. See `display-buffer'.")
6842 (put 'display-buffer-fallback-action 'risky-local-variable t)
6844 (defun display-buffer-assq-regexp (buffer-name alist action)
6845 "Retrieve ALIST entry corresponding to BUFFER-NAME.
6846 ACTION is the action argument passed to `display-buffer'."
6847 (catch 'match
6848 (dolist (entry alist)
6849 (let ((key (car entry)))
6850 (when (or (and (stringp key)
6851 (string-match-p key buffer-name))
6852 (and (functionp key)
6853 (funcall key buffer-name action)))
6854 (throw 'match (cdr entry)))))))
6856 (defvar display-buffer--same-window-action
6857 '(display-buffer-same-window
6858 (inhibit-same-window . nil))
6859 "A `display-buffer' action for displaying in the same window.")
6860 (put 'display-buffer--same-window-action 'risky-local-variable t)
6862 (defvar display-buffer--other-frame-action
6863 '((display-buffer-reuse-window
6864 display-buffer-pop-up-frame)
6865 (reusable-frames . 0)
6866 (inhibit-same-window . t))
6867 "A `display-buffer' action for displaying in another frame.")
6868 (put 'display-buffer--other-frame-action 'risky-local-variable t)
6870 (defun display-buffer (buffer-or-name &optional action frame)
6871 "Display BUFFER-OR-NAME in some window, without selecting it.
6872 BUFFER-OR-NAME must be a buffer or the name of an existing
6873 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
6874 or nil if no such window is found.
6876 Optional argument ACTION, if non-nil, should specify a display
6877 action. Its form is described below.
6879 Optional argument FRAME, if non-nil, acts like an additional
6880 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
6881 specifying the frame(s) to search for a window that is already
6882 displaying the buffer. See `display-buffer-reuse-window'.
6884 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
6885 where FUNCTION is either a function or a list of functions, and
6886 ALIST is an arbitrary association list (alist).
6888 Each such FUNCTION should accept two arguments: the buffer to
6889 display and an alist. Based on those arguments, it should
6890 display the buffer and return the window. If the caller is
6891 prepared to handle the case of not displaying the buffer
6892 and returning nil from `display-buffer' it should pass
6893 \(allow-no-window . t) as an element of the ALIST.
6895 The `display-buffer' function builds a function list and an alist
6896 by combining the functions and alists specified in
6897 `display-buffer-overriding-action', `display-buffer-alist', the
6898 ACTION argument, `display-buffer-base-action', and
6899 `display-buffer-fallback-action' (in order). Then it calls each
6900 function in the combined function list in turn, passing the
6901 buffer as the first argument and the combined alist as the second
6902 argument, until one of the functions returns non-nil.
6904 If ACTION is nil, the function list and the alist are built using
6905 only the other variables mentioned above.
6907 Available action functions include:
6908 `display-buffer-same-window'
6909 `display-buffer-reuse-window'
6910 `display-buffer-pop-up-frame'
6911 `display-buffer-pop-up-window'
6912 `display-buffer-in-previous-window'
6913 `display-buffer-use-some-window'
6914 `display-buffer-use-some-frame'
6916 Recognized alist entries include:
6918 `inhibit-same-window' -- A non-nil value prevents the same
6919 window from being used for display.
6921 `inhibit-switch-frame' -- A non-nil value prevents any other
6922 frame from being raised or selected,
6923 even if the window is displayed there.
6925 `reusable-frames' -- Value specifies frame(s) to search for a
6926 window that already displays the buffer.
6927 See `display-buffer-reuse-window'.
6929 `pop-up-frame-parameters' -- Value specifies an alist of frame
6930 parameters to give a new frame, if
6931 one is created.
6933 `window-height' -- Value specifies either an integer (the number
6934 of lines of a new window), a floating point number (the
6935 fraction of a new window with respect to the height of the
6936 frame's root window) or a function to be called with one
6937 argument - a new window. The function is supposed to adjust
6938 the height of the window; its return value is ignored.
6939 Suitable functions are `shrink-window-if-larger-than-buffer'
6940 and `fit-window-to-buffer'.
6942 `window-width' -- Value specifies either an integer (the number
6943 of columns of a new window), a floating point number (the
6944 fraction of a new window with respect to the width of the
6945 frame's root window) or a function to be called with one
6946 argument - a new window. The function is supposed to adjust
6947 the width of the window; its return value is ignored.
6949 `allow-no-window' -- A non-nil value indicates readiness for the case
6950 of not displaying the buffer and FUNCTION can safely return
6951 a non-window value to suppress displaying.
6953 `preserve-size' -- Value should be either (t . nil) to
6954 preserve the width of the window, (nil . t) to preserve its
6955 height or (t . t) to preserve both.
6957 `window-parameters' -- Value specifies an alist of window
6958 parameters to give the chosen window.
6960 The ACTION argument to `display-buffer' can also have a non-nil
6961 and non-list value. This means to display the buffer in a window
6962 other than the selected one, even if it is already displayed in
6963 the selected window. If called interactively with a prefix
6964 argument, ACTION is t."
6965 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6966 (if current-prefix-arg t)))
6967 (let ((buffer (if (bufferp buffer-or-name)
6968 buffer-or-name
6969 (get-buffer buffer-or-name)))
6970 ;; Make sure that when we split windows the old window keeps
6971 ;; point, bug#14829.
6972 (split-window-keep-point t)
6973 ;; Handle the old form of the first argument.
6974 (inhibit-same-window (and action (not (listp action)))))
6975 (unless (listp action) (setq action nil))
6976 (if display-buffer-function
6977 ;; If `display-buffer-function' is defined, let it do the job.
6978 (funcall display-buffer-function buffer inhibit-same-window)
6979 ;; Otherwise, use the defined actions.
6980 (let* ((user-action
6981 (display-buffer-assq-regexp
6982 (buffer-name buffer) display-buffer-alist action))
6983 (special-action (display-buffer--special-action buffer))
6984 ;; Extra actions from the arguments to this function:
6985 (extra-action
6986 (cons nil (append (if inhibit-same-window
6987 '((inhibit-same-window . t)))
6988 (if frame
6989 `((reusable-frames . ,frame))))))
6990 ;; Construct action function list and action alist.
6991 (actions (list display-buffer-overriding-action
6992 user-action special-action action extra-action
6993 display-buffer-base-action
6994 display-buffer-fallback-action))
6995 (functions (apply 'append
6996 (mapcar (lambda (x)
6997 (setq x (car x))
6998 (if (functionp x) (list x) x))
6999 actions)))
7000 (alist (apply 'append (mapcar 'cdr actions)))
7001 window)
7002 (unless (buffer-live-p buffer)
7003 (error "Invalid buffer"))
7004 (while (and functions (not window))
7005 (setq window (funcall (car functions) buffer alist)
7006 functions (cdr functions)))
7007 (and (windowp window) window)))))
7009 (defun display-buffer-other-frame (buffer)
7010 "Display buffer BUFFER preferably in another frame.
7011 This uses the function `display-buffer' as a subroutine; see
7012 its documentation for additional customization information."
7013 (interactive "BDisplay buffer in other frame: ")
7014 (display-buffer buffer display-buffer--other-frame-action t))
7016 ;;; `display-buffer' action functions:
7018 (defun display-buffer-use-some-frame (buffer alist)
7019 "Display BUFFER in an existing frame that meets a predicate
7020 \(by default any frame other than the current frame). If
7021 successful, return the window used; otherwise return nil.
7023 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
7024 raising the frame.
7026 If ALIST has a non-nil `frame-predicate' entry, its value is a
7027 function taking one argument (a frame), returning non-nil if the
7028 frame is a candidate; this function replaces the default
7029 predicate.
7031 If ALIST has a non-nil `inhibit-same-window' entry, avoid using
7032 the currently selected window (only useful with a frame-predicate
7033 that allows the selected frame)."
7034 (let* ((predicate (or (cdr (assq 'frame-predicate alist))
7035 (lambda (frame)
7036 (and
7037 (not (eq frame (selected-frame)))
7038 (not (window-dedicated-p
7040 (get-lru-window frame)
7041 (frame-first-window frame)))))
7043 (frame (car (filtered-frame-list predicate)))
7044 (window (and frame (get-lru-window frame nil (cdr (assq 'inhibit-same-window alist))))))
7045 (when window
7046 (prog1
7047 (window--display-buffer
7048 buffer window 'frame alist display-buffer-mark-dedicated)
7049 (unless (cdr (assq 'inhibit-switch-frame alist))
7050 (window--maybe-raise-frame frame))))))
7052 (defun display-buffer-same-window (buffer alist)
7053 "Display BUFFER in the selected window.
7054 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
7055 if the selected window is a minibuffer window or is dedicated to
7056 another buffer; in that case, return nil. Otherwise, return the
7057 selected window."
7058 (unless (or (cdr (assq 'inhibit-same-window alist))
7059 (window-minibuffer-p)
7060 (window-dedicated-p))
7061 (window--display-buffer buffer (selected-window) 'reuse alist)))
7063 (defun display-buffer--maybe-same-window (buffer alist)
7064 "Conditionally display BUFFER in the selected window.
7065 If `same-window-p' returns non-nil for BUFFER's name, call
7066 `display-buffer-same-window' and return its value. Otherwise,
7067 return nil."
7068 (and (same-window-p (buffer-name buffer))
7069 (display-buffer-same-window buffer alist)))
7071 (defun display-buffer-reuse-window (buffer alist)
7072 "Return a window that is already displaying BUFFER.
7073 Return nil if no usable window is found.
7075 If ALIST has a non-nil `inhibit-same-window' entry, the selected
7076 window is not eligible for reuse.
7078 If ALIST contains a `reusable-frames' entry, its value determines
7079 which frames to search for a reusable window:
7080 nil -- the selected frame (actually the last non-minibuffer frame)
7081 A frame -- just that frame
7082 `visible' -- all visible frames
7083 0 -- all frames on the current terminal
7084 t -- all frames.
7086 If ALIST contains no `reusable-frames' entry, search just the
7087 selected frame if `display-buffer-reuse-frames' and
7088 `pop-up-frames' are both nil; search all frames on the current
7089 terminal if either of those variables is non-nil.
7091 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7092 event that a window on another frame is chosen, avoid raising
7093 that frame."
7094 (let* ((alist-entry (assq 'reusable-frames alist))
7095 (frames (cond (alist-entry (cdr alist-entry))
7096 ((if (eq pop-up-frames 'graphic-only)
7097 (display-graphic-p)
7098 pop-up-frames)
7100 (display-buffer-reuse-frames 0)
7101 (t (last-nonminibuffer-frame))))
7102 (window (if (and (eq buffer (window-buffer))
7103 (not (cdr (assq 'inhibit-same-window alist))))
7104 (selected-window)
7105 (car (delq (selected-window)
7106 (get-buffer-window-list buffer 'nomini
7107 frames))))))
7108 (when (window-live-p window)
7109 (prog1 (window--display-buffer buffer window 'reuse alist)
7110 (unless (cdr (assq 'inhibit-switch-frame alist))
7111 (window--maybe-raise-frame (window-frame window)))))))
7113 (defun display-buffer-reuse-mode-window (buffer alist)
7114 "Return a window based on the mode of the buffer it displays.
7115 Display BUFFER in the returned window. Return nil if no usable
7116 window is found.
7118 If ALIST contains a `mode' entry, its value is a major mode (a
7119 symbol) or a list of modes. A window is a candidate if it
7120 displays a buffer that derives from one of the given modes. When
7121 ALIST contains no `mode' entry, the current major mode of BUFFER
7122 is used.
7124 The behavior is also controlled by entries for
7125 `inhibit-same-window', `reusable-frames' and
7126 `inhibit-switch-frame' as is done in the function
7127 `display-buffer-reuse-window'."
7128 (let* ((alist-entry (assq 'reusable-frames alist))
7129 (alist-mode-entry (assq 'mode alist))
7130 (frames (cond (alist-entry (cdr alist-entry))
7131 ((if (eq pop-up-frames 'graphic-only)
7132 (display-graphic-p)
7133 pop-up-frames)
7135 (display-buffer-reuse-frames 0)
7136 (t (last-nonminibuffer-frame))))
7137 (inhibit-same-window-p (cdr (assq 'inhibit-same-window alist)))
7138 (windows (window-list-1 nil 'nomini frames))
7139 (buffer-mode (with-current-buffer buffer major-mode))
7140 (allowed-modes (if alist-mode-entry
7141 (cdr alist-mode-entry)
7142 buffer-mode))
7143 (curwin (selected-window))
7144 (curframe (selected-frame)))
7145 (unless (listp allowed-modes)
7146 (setq allowed-modes (list allowed-modes)))
7147 (let (same-mode-same-frame
7148 same-mode-other-frame
7149 derived-mode-same-frame
7150 derived-mode-other-frame)
7151 (dolist (window windows)
7152 (let ((mode?
7153 (with-current-buffer (window-buffer window)
7154 (cond ((memq major-mode allowed-modes)
7155 'same)
7156 ((derived-mode-p allowed-modes)
7157 'derived)))))
7158 (when (and mode?
7159 (not (and inhibit-same-window-p
7160 (eq window curwin))))
7161 (push window (if (eq curframe (window-frame window))
7162 (if (eq mode? 'same)
7163 same-mode-same-frame
7164 derived-mode-same-frame)
7165 (if (eq mode? 'same)
7166 same-mode-other-frame
7167 derived-mode-other-frame))))))
7168 (let ((window (car (nconc same-mode-same-frame
7169 same-mode-other-frame
7170 derived-mode-same-frame
7171 derived-mode-other-frame))))
7172 (when (window-live-p window)
7173 (prog1 (window--display-buffer buffer window 'reuse alist)
7174 (unless (cdr (assq 'inhibit-switch-frame alist))
7175 (window--maybe-raise-frame (window-frame window)))))))))
7177 (defun display-buffer--special-action (buffer)
7178 "Return special display action for BUFFER, if any.
7179 If `special-display-p' returns non-nil for BUFFER, return an
7180 appropriate display action involving `special-display-function'.
7181 See `display-buffer' for the format of display actions."
7182 (and special-display-function
7183 ;; `special-display-p' returns either t or a list of frame
7184 ;; parameters to pass to `special-display-function'.
7185 (let ((pars (special-display-p (buffer-name buffer))))
7186 (when pars
7187 (list (list #'display-buffer-reuse-window
7188 `(lambda (buffer _alist)
7189 (funcall special-display-function
7190 buffer ',(if (listp pars) pars)))))))))
7192 (defun display-buffer-pop-up-frame (buffer alist)
7193 "Display BUFFER in a new frame.
7194 This works by calling `pop-up-frame-function'. If successful,
7195 return the window used; otherwise return nil.
7197 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
7198 raising the new frame.
7200 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
7201 corresponding value is an alist of frame parameters to give the
7202 new frame."
7203 (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
7204 (pop-up-frame-alist (append params pop-up-frame-alist))
7205 (fun pop-up-frame-function)
7206 frame window)
7207 (when (and fun
7208 ;; Make BUFFER current so `make-frame' will use it as the
7209 ;; new frame's buffer (Bug#15133).
7210 (with-current-buffer buffer
7211 (setq frame (funcall fun)))
7212 (setq window (frame-selected-window frame)))
7213 (prog1 (window--display-buffer
7214 buffer window 'frame alist display-buffer-mark-dedicated)
7215 (unless (cdr (assq 'inhibit-switch-frame alist))
7216 (window--maybe-raise-frame frame))))))
7218 (defun display-buffer-pop-up-window (buffer alist)
7219 "Display BUFFER by popping up a new window.
7220 The new window is created on the selected frame, or in
7221 `last-nonminibuffer-frame' if no windows can be created there.
7222 If successful, return the new window; otherwise return nil.
7224 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7225 event that the new window is created on another frame, avoid
7226 raising the frame."
7227 (let ((frame (or (window--frame-usable-p (selected-frame))
7228 (window--frame-usable-p (last-nonminibuffer-frame))))
7229 window)
7230 (when (and (or (not (frame-parameter frame 'unsplittable))
7231 ;; If the selected frame cannot be split, look at
7232 ;; `last-nonminibuffer-frame'.
7233 (and (eq frame (selected-frame))
7234 (setq frame (last-nonminibuffer-frame))
7235 (window--frame-usable-p frame)
7236 (not (frame-parameter frame 'unsplittable))))
7237 ;; Attempt to split largest or least recently used window.
7238 (setq window (or (window--try-to-split-window
7239 (get-largest-window frame t) alist)
7240 (window--try-to-split-window
7241 (get-lru-window frame t) alist))))
7242 (prog1 (window--display-buffer
7243 buffer window 'window alist display-buffer-mark-dedicated)
7244 (unless (cdr (assq 'inhibit-switch-frame alist))
7245 (window--maybe-raise-frame (window-frame window)))))))
7247 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
7248 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
7249 If `pop-up-frames' is non-nil (and not `graphic-only' on a
7250 text-only terminal), try with `display-buffer-pop-up-frame'.
7252 If that cannot be done, and `pop-up-windows' is non-nil, try
7253 again with `display-buffer-pop-up-window'."
7254 (or (and (if (eq pop-up-frames 'graphic-only)
7255 (display-graphic-p)
7256 pop-up-frames)
7257 (display-buffer-pop-up-frame buffer alist))
7258 (and pop-up-windows
7259 (display-buffer-pop-up-window buffer alist))))
7261 (defun display-buffer-below-selected (buffer alist)
7262 "Try displaying BUFFER in a window below the selected window.
7263 If there is a window below the selected one and that window
7264 already displays BUFFER, use that window. Otherwise, try to
7265 create a new window below the selected one and show BUFFER there.
7266 If that attempt fails as well and there is a non-dedicated window
7267 below the selected one, use that window."
7268 (let (window)
7269 (or (and (setq window (window-in-direction 'below))
7270 (eq buffer (window-buffer window))
7271 (window--display-buffer buffer window 'reuse alist))
7272 (and (not (frame-parameter nil 'unsplittable))
7273 (let ((split-height-threshold 0)
7274 split-width-threshold)
7275 (setq window (window--try-to-split-window (selected-window) alist)))
7276 (window--display-buffer
7277 buffer window 'window alist display-buffer-mark-dedicated))
7278 (and (setq window (window-in-direction 'below))
7279 (not (window-dedicated-p window))
7280 (window--display-buffer
7281 buffer window 'reuse alist display-buffer-mark-dedicated)))))
7283 (defun display-buffer-at-bottom (buffer alist)
7284 "Try displaying BUFFER in a window at the bottom of the selected frame.
7285 This either reuses such a window provided it shows BUFFER
7286 already, splits a window at the bottom of the frame or the
7287 frame's root window, or reuses some window at the bottom of the
7288 selected frame."
7289 (let (bottom-window bottom-window-shows-buffer window)
7290 (walk-window-tree
7291 (lambda (window)
7292 (cond
7293 ((window-in-direction 'below window))
7294 ((and (not bottom-window-shows-buffer)
7295 (eq buffer (window-buffer window)))
7296 (setq bottom-window-shows-buffer t)
7297 (setq bottom-window window))
7298 ((not bottom-window)
7299 (setq bottom-window window)))
7300 nil nil 'nomini))
7301 (or (and bottom-window-shows-buffer
7302 (window--display-buffer
7303 buffer bottom-window 'reuse alist display-buffer-mark-dedicated))
7304 (and (not (frame-parameter nil 'unsplittable))
7305 (let (split-width-threshold)
7306 (setq window (window--try-to-split-window bottom-window alist)))
7307 (window--display-buffer
7308 buffer window 'window alist display-buffer-mark-dedicated))
7309 (and (not (frame-parameter nil 'unsplittable))
7310 (setq window (split-window-no-error (window-main-window)))
7311 (window--display-buffer
7312 buffer window 'window alist display-buffer-mark-dedicated))
7313 (and (setq window bottom-window)
7314 (not (window-dedicated-p window))
7315 (window--display-buffer
7316 buffer window 'reuse alist display-buffer-mark-dedicated)))))
7318 (defun display-buffer-in-previous-window (buffer alist)
7319 "Display BUFFER in a window previously showing it.
7320 If ALIST has a non-nil `inhibit-same-window' entry, the selected
7321 window is not eligible for reuse.
7323 If ALIST contains a `reusable-frames' entry, its value determines
7324 which frames to search for a reusable window:
7325 nil -- the selected frame (actually the last non-minibuffer frame)
7326 A frame -- just that frame
7327 `visible' -- all visible frames
7328 0 -- all frames on the current terminal
7329 t -- all frames.
7331 If ALIST contains no `reusable-frames' entry, search just the
7332 selected frame if `display-buffer-reuse-frames' and
7333 `pop-up-frames' are both nil; search all frames on the current
7334 terminal if either of those variables is non-nil.
7336 If ALIST has a `previous-window' entry, the window specified by
7337 that entry will override any other window found by the methods
7338 above, even if that window never showed BUFFER before."
7339 (let* ((alist-entry (assq 'reusable-frames alist))
7340 (inhibit-same-window
7341 (cdr (assq 'inhibit-same-window alist)))
7342 (frames (cond
7343 (alist-entry (cdr alist-entry))
7344 ((if (eq pop-up-frames 'graphic-only)
7345 (display-graphic-p)
7346 pop-up-frames)
7348 (display-buffer-reuse-frames 0)
7349 (t (last-nonminibuffer-frame))))
7350 best-window second-best-window window)
7351 ;; Scan windows whether they have shown the buffer recently.
7352 (catch 'best
7353 (dolist (window (window-list-1 (frame-first-window) 'nomini frames))
7354 (when (and (assq buffer (window-prev-buffers window))
7355 (not (window-dedicated-p window)))
7356 (if (eq window (selected-window))
7357 (unless inhibit-same-window
7358 (setq second-best-window window))
7359 (setq best-window window)
7360 (throw 'best t)))))
7361 ;; When ALIST has a `previous-window' entry, that entry may override
7362 ;; anything we found so far.
7363 (when (and (setq window (cdr (assq 'previous-window alist)))
7364 (window-live-p window)
7365 (not (window-dedicated-p window)))
7366 (if (eq window (selected-window))
7367 (unless inhibit-same-window
7368 (setq second-best-window window))
7369 (setq best-window window)))
7370 ;; Return best or second best window found.
7371 (when (setq window (or best-window second-best-window))
7372 (window--display-buffer buffer window 'reuse alist))))
7374 (defun display-buffer-use-some-window (buffer alist)
7375 "Display BUFFER in an existing window.
7376 Search for a usable window, set that window to the buffer, and
7377 return the window. If no suitable window is found, return nil.
7379 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7380 event that a window in another frame is chosen, avoid raising
7381 that frame."
7382 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
7383 (frame (or (window--frame-usable-p (selected-frame))
7384 (window--frame-usable-p (last-nonminibuffer-frame))))
7385 (window
7386 ;; Reuse an existing window.
7387 (or (get-lru-window frame nil not-this-window)
7388 (let ((window (get-buffer-window buffer 'visible)))
7389 (unless (and not-this-window
7390 (eq window (selected-window)))
7391 window))
7392 (get-largest-window 'visible nil not-this-window)
7393 (let ((window (get-buffer-window buffer 0)))
7394 (unless (and not-this-window
7395 (eq window (selected-window)))
7396 window))
7397 (get-largest-window 0 nil not-this-window)))
7398 (quit-restore (and (window-live-p window)
7399 (window-parameter window 'quit-restore)))
7400 (quad (nth 1 quit-restore)))
7401 (when (window-live-p window)
7402 ;; If the window was used by `display-buffer' before, try to
7403 ;; resize it to its old height but don't signal an error.
7404 (when (and (listp quad)
7405 (integerp (nth 3 quad))
7406 (> (nth 3 quad) (window-total-height window)))
7407 (condition-case nil
7408 (window-resize window (- (nth 3 quad) (window-total-height window)))
7409 (error nil)))
7411 (prog1
7412 (window--display-buffer buffer window 'reuse alist)
7413 (window--even-window-sizes window)
7414 (unless (cdr (assq 'inhibit-switch-frame alist))
7415 (window--maybe-raise-frame (window-frame window)))))))
7417 (defun display-buffer-no-window (_buffer alist)
7418 "Display BUFFER in no window.
7419 If ALIST has a non-nil `allow-no-window' entry, then don't display
7420 a window at all. This makes possible to override the default action
7421 and avoid displaying the buffer. It is assumed that when the caller
7422 specifies a non-nil `allow-no-window' then it can handle a nil value
7423 returned from `display-buffer' in this case."
7424 (when (cdr (assq 'allow-no-window alist))
7425 'fail))
7427 ;;; Display + selection commands:
7428 (defun pop-to-buffer (buffer-or-name &optional action norecord)
7429 "Display buffer specified by BUFFER-OR-NAME and select its window.
7430 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
7431 If it is a string not naming an existent buffer, create a buffer
7432 with that name. If BUFFER-OR-NAME is nil, choose some other
7433 buffer. In either case, make that buffer current and return it.
7435 This uses `display-buffer' as a subroutine. The optional ACTION
7436 argument is passed to `display-buffer' as its ACTION argument.
7437 See `display-buffer' for more information. ACTION is t if called
7438 interactively with a prefix argument, which means to pop to a
7439 window other than the selected one even if the buffer is already
7440 displayed in the selected window.
7442 If a suitable window is found, select that window. If it is not
7443 on the selected frame, raise that window's frame and give it
7444 input focus.
7446 Optional third arg NORECORD non-nil means do not put this buffer
7447 at the front of the list of recently selected ones."
7448 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
7449 (if current-prefix-arg t)))
7450 (let* ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))
7451 (old-frame (selected-frame))
7452 (window (display-buffer buffer action)))
7453 ;; Don't assume that `display-buffer' has supplied us with a window
7454 ;; (Bug#24332).
7455 (if window
7456 (let ((frame (window-frame window)))
7457 ;; If we chose another frame, make sure it gets input focus.
7458 (unless (eq frame old-frame)
7459 (select-frame-set-input-focus frame norecord))
7460 ;; Make sure the window is selected (Bug#8615), (Bug#6954)
7461 (select-window window norecord))
7462 ;; If `display-buffer' failed to supply a window, just make the
7463 ;; buffer current.
7464 (set-buffer buffer))
7465 ;; Return BUFFER even when we got no window.
7466 buffer))
7468 (defun pop-to-buffer-same-window (buffer &optional norecord)
7469 "Select buffer BUFFER in some window, preferably the same one.
7470 BUFFER may be a buffer, a string (a buffer name), or nil. If it
7471 is a string not naming an existent buffer, create a buffer with
7472 that name. If BUFFER is nil, choose some other buffer. Return
7473 the buffer.
7475 Optional argument NORECORD, if non-nil means do not put this
7476 buffer at the front of the list of recently selected ones.
7478 Unlike `pop-to-buffer', this function prefers using the selected
7479 window over popping up a new window or frame."
7480 (pop-to-buffer buffer display-buffer--same-window-action norecord))
7482 (defun read-buffer-to-switch (prompt)
7483 "Read the name of a buffer to switch to, prompting with PROMPT.
7484 Return the name of the buffer as a string.
7486 This function is intended for the `switch-to-buffer' family of
7487 commands since these need to omit the name of the current buffer
7488 from the list of completions and default values."
7489 (let ((rbts-completion-table (internal-complete-buffer-except)))
7490 (minibuffer-with-setup-hook
7491 (lambda ()
7492 (setq minibuffer-completion-table rbts-completion-table)
7493 ;; Since rbts-completion-table is built dynamically, we
7494 ;; can't just add it to the default value of
7495 ;; icomplete-with-completion-tables, so we add it
7496 ;; here manually.
7497 (if (and (boundp 'icomplete-with-completion-tables)
7498 (listp icomplete-with-completion-tables))
7499 (set (make-local-variable 'icomplete-with-completion-tables)
7500 (cons rbts-completion-table
7501 icomplete-with-completion-tables))))
7502 (read-buffer prompt (other-buffer (current-buffer))
7503 (confirm-nonexistent-file-or-buffer)))))
7505 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
7506 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
7507 If BUFFER-OR-NAME is nil, return the buffer returned by
7508 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
7509 exists, return that buffer. If no such buffer exists, create a
7510 buffer with the name BUFFER-OR-NAME and return that buffer."
7511 (if buffer-or-name
7512 (or (get-buffer buffer-or-name)
7513 (let ((buffer (get-buffer-create buffer-or-name)))
7514 (set-buffer-major-mode buffer)
7515 buffer))
7516 (other-buffer)))
7518 (defcustom switch-to-buffer-preserve-window-point t
7519 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
7520 If this is nil, `switch-to-buffer' displays the buffer at that
7521 buffer's `point'. If this is `already-displayed', it tries to
7522 display the buffer at its previous position in the selected
7523 window, provided the buffer is currently displayed in some other
7524 window on any visible or iconified frame. If this is t, it
7525 unconditionally tries to display the buffer at its previous
7526 position in the selected window.
7528 This variable is ignored if the buffer is already displayed in
7529 the selected window or never appeared in it before, or if
7530 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
7531 :type '(choice
7532 (const :tag "Never" nil)
7533 (const :tag "If already displayed elsewhere" already-displayed)
7534 (const :tag "Always" t))
7535 :group 'windows
7536 :version "26.1")
7538 (defcustom switch-to-buffer-in-dedicated-window nil
7539 "Allow switching to buffer in strongly dedicated windows.
7540 If non-nil, allow `switch-to-buffer' to proceed when called
7541 interactively and the selected window is strongly dedicated to
7542 its buffer.
7544 The following values are recognized:
7546 nil - disallow switching; signal an error
7548 prompt - prompt user whether to allow switching
7550 pop - perform `pop-to-buffer' instead
7552 t - undedicate selected window and switch
7554 When called non-interactively, `switch-to-buffer' always signals
7555 an error when the selected window is dedicated to its buffer and
7556 FORCE-SAME-WINDOW is non-nil."
7557 :type '(choice
7558 (const :tag "Disallow" nil)
7559 (const :tag "Prompt" prompt)
7560 (const :tag "Pop" pop)
7561 (const :tag "Allow" t))
7562 :group 'windows
7563 :version "25.1")
7565 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
7566 "Display buffer BUFFER-OR-NAME in the selected window.
7568 WARNING: This is NOT the way to work on another buffer temporarily
7569 within a Lisp program! Use `set-buffer' instead. That avoids
7570 messing with the window-buffer correspondences.
7572 If the selected window cannot display the specified buffer
7573 because it is a minibuffer window or strongly dedicated to
7574 another buffer, call `pop-to-buffer' to select the buffer in
7575 another window. In interactive use, if the selected window is
7576 strongly dedicated to its buffer, the value of the option
7577 `switch-to-buffer-in-dedicated-window' specifies how to proceed.
7579 If called interactively, read the buffer name using the
7580 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7581 determines whether to request confirmation before creating a new
7582 buffer.
7584 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
7585 If BUFFER-OR-NAME is a string that does not identify an existing
7586 buffer, create a buffer with that name. If BUFFER-OR-NAME is
7587 nil, switch to the buffer returned by `other-buffer'.
7589 If optional argument NORECORD is non-nil, do not put the buffer
7590 at the front of the buffer list, and do not make the window
7591 displaying it the most recently selected one.
7593 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
7594 must be displayed in the selected window when called
7595 non-interactively; if that is impossible, signal an error rather
7596 than calling `pop-to-buffer'.
7598 The option `switch-to-buffer-preserve-window-point' can be used
7599 to make the buffer appear at its last position in the selected
7600 window.
7602 Return the buffer switched to."
7603 (interactive
7604 (let ((force-same-window
7605 (cond
7606 ((window-minibuffer-p) nil)
7607 ((not (eq (window-dedicated-p) t)) 'force-same-window)
7608 ((pcase switch-to-buffer-in-dedicated-window
7609 (`nil (user-error
7610 "Cannot switch buffers in a dedicated window"))
7611 (`prompt
7612 (if (y-or-n-p
7613 (format "Window is dedicated to %s; undedicate it"
7614 (window-buffer)))
7615 (progn
7616 (set-window-dedicated-p nil nil)
7617 'force-same-window)
7618 (user-error
7619 "Cannot switch buffers in a dedicated window")))
7620 (`pop nil)
7621 (_ (set-window-dedicated-p nil nil) 'force-same-window))))))
7622 (list (read-buffer-to-switch "Switch to buffer: ") nil force-same-window)))
7623 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
7624 (cond
7625 ;; Don't call set-window-buffer if it's not needed since it
7626 ;; might signal an error (e.g. if the window is dedicated).
7627 ((eq buffer (window-buffer)))
7628 ((window-minibuffer-p)
7629 (if force-same-window
7630 (user-error "Cannot switch buffers in minibuffer window")
7631 (pop-to-buffer buffer norecord)))
7632 ((eq (window-dedicated-p) t)
7633 (if force-same-window
7634 (user-error "Cannot switch buffers in a dedicated window")
7635 (pop-to-buffer buffer norecord)))
7637 (let* ((entry (assq buffer (window-prev-buffers)))
7638 (displayed (and (eq switch-to-buffer-preserve-window-point
7639 'already-displayed)
7640 (get-buffer-window buffer 0))))
7641 (set-window-buffer nil buffer)
7642 (when (and entry
7643 (or (eq switch-to-buffer-preserve-window-point t)
7644 displayed))
7645 ;; Try to restore start and point of buffer in the selected
7646 ;; window (Bug#4041).
7647 (set-window-start (selected-window) (nth 1 entry) t)
7648 (set-window-point nil (nth 2 entry))))))
7650 (unless norecord
7651 (select-window (selected-window)))
7652 (set-buffer buffer)))
7654 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
7655 "Select the buffer specified by BUFFER-OR-NAME in another window.
7656 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
7657 nil. Return the buffer switched to.
7659 If called interactively, prompt for the buffer name using the
7660 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7661 determines whether to request confirmation before creating a new
7662 buffer.
7664 If BUFFER-OR-NAME is a string and does not identify an existing
7665 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
7666 nil, switch to the buffer returned by `other-buffer'.
7668 Optional second argument NORECORD non-nil means do not put this
7669 buffer at the front of the list of recently selected ones.
7671 This uses the function `display-buffer' as a subroutine; see its
7672 documentation for additional customization information."
7673 (interactive
7674 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
7675 (let ((pop-up-windows t))
7676 (pop-to-buffer buffer-or-name t norecord)))
7678 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
7679 "Switch to buffer BUFFER-OR-NAME in another frame.
7680 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
7681 nil. Return the buffer switched to.
7683 If called interactively, prompt for the buffer name using the
7684 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7685 determines whether to request confirmation before creating a new
7686 buffer.
7688 If BUFFER-OR-NAME is a string and does not identify an existing
7689 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
7690 nil, switch to the buffer returned by `other-buffer'.
7692 Optional second arg NORECORD non-nil means do not put this
7693 buffer at the front of the list of recently selected ones.
7695 This uses the function `display-buffer' as a subroutine; see its
7696 documentation for additional customization information."
7697 (interactive
7698 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
7699 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
7701 (defun set-window-text-height (window height)
7702 "Set the height in lines of the text display area of WINDOW to HEIGHT.
7703 WINDOW must be a live window and defaults to the selected one.
7704 HEIGHT doesn't include the mode line or header line, if any, or
7705 any partial-height lines in the text display area.
7707 Note that the current implementation of this function cannot
7708 always set the height exactly, but attempts to be conservative,
7709 by allocating more lines than are actually needed in the case
7710 where some error may be present."
7711 (setq window (window-normalize-window window t))
7712 (let ((delta (- height (window-text-height window))))
7713 (unless (zerop delta)
7714 ;; Setting window-min-height to a value like 1 can lead to very
7715 ;; bizarre displays because it also allows Emacs to make *other*
7716 ;; windows one line tall, which means that there's no more space
7717 ;; for the mode line.
7718 (let ((window-min-height (min 2 height)))
7719 (window-resize window delta)))))
7721 (defun enlarge-window-horizontally (delta)
7722 "Make selected window DELTA columns wider.
7723 Interactively, if no argument is given, make selected window one
7724 column wider."
7725 (interactive "p")
7726 (enlarge-window delta t))
7728 (defun shrink-window-horizontally (delta)
7729 "Make selected window DELTA columns narrower.
7730 Interactively, if no argument is given, make selected window one
7731 column narrower."
7732 (interactive "p")
7733 (shrink-window delta t))
7735 (defun count-screen-lines (&optional beg end count-final-newline window)
7736 "Return the number of screen lines in the region.
7737 The number of screen lines may be different from the number of actual lines,
7738 due to line breaking, display table, etc.
7740 Optional arguments BEG and END default to `point-min' and `point-max'
7741 respectively.
7743 If region ends with a newline, ignore it unless optional third argument
7744 COUNT-FINAL-NEWLINE is non-nil.
7746 The optional fourth argument WINDOW specifies the window used for obtaining
7747 parameters such as width, horizontal scrolling, and so on. The default is
7748 to use the selected window's parameters.
7750 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
7751 regardless of which buffer is displayed in WINDOW. This makes possible to use
7752 `count-screen-lines' in any buffer, whether or not it is currently displayed
7753 in some window."
7754 (unless beg
7755 (setq beg (point-min)))
7756 (unless end
7757 (setq end (point-max)))
7758 (if (= beg end)
7760 (save-excursion
7761 (save-restriction
7762 (widen)
7763 (narrow-to-region (min beg end)
7764 (if (and (not count-final-newline)
7765 (= ?\n (char-before (max beg end))))
7766 (1- (max beg end))
7767 (max beg end)))
7768 (goto-char (point-min))
7769 (1+ (vertical-motion (buffer-size) window))))))
7771 (defun window-buffer-height (window)
7772 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
7773 WINDOW must be a live window and defaults to the selected one."
7774 (setq window (window-normalize-window window t))
7775 (with-current-buffer (window-buffer window)
7776 (max 1
7777 (count-screen-lines (point-min) (point-max)
7778 ;; If buffer ends with a newline, ignore it when
7779 ;; counting height unless point is after it.
7780 (eobp)
7781 window))))
7783 ;;; Resizing windows and frames to fit their contents exactly.
7784 (defcustom fit-window-to-buffer-horizontally nil
7785 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
7786 If this is nil, `fit-window-to-buffer' never resizes windows
7787 horizontally. If this is `only', it can resize windows
7788 horizontally only. Any other value means `fit-window-to-buffer'
7789 can resize windows in both dimensions."
7790 :type 'boolean
7791 :version "24.4"
7792 :group 'help)
7794 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
7795 ;; counting title bar and outer borders.
7796 (defcustom fit-frame-to-buffer nil
7797 "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
7798 A frame is fit if and only if its root window is a live window
7799 and this option is non-nil. If this is `horizontally', frames
7800 are resized horizontally only. If this is `vertically', frames
7801 are resized vertically only. Any other non-nil value means
7802 frames can be resized in both dimensions."
7803 :type 'boolean
7804 :version "24.4"
7805 :group 'help)
7807 (defcustom fit-frame-to-buffer-margins '(nil nil nil nil)
7808 "Margins around frame for `fit-frame-to-buffer'.
7809 This specifies the numbers of pixels to be left free on the left,
7810 above, on the right, and below a frame fitted to its buffer. Set
7811 this to avoid obscuring other desktop objects like the taskbar.
7812 The default is nil for each side, which means to not add margins.
7814 The value specified here can be overridden for a specific frame
7815 by that frame's `fit-frame-to-buffer-margins' parameter, if
7816 present. See also `fit-frame-to-buffer-sizes'."
7817 :version "24.4"
7818 :type '(list
7819 (choice
7820 :tag "Left"
7821 :value nil
7822 :format "%[LeftMargin%] %v "
7823 (const :tag "None" :format "%t" nil)
7824 (integer :tag "Pixels" :size 5))
7825 (choice
7826 :tag "Top"
7827 :value nil
7828 :format "%[TopMargin%] %v "
7829 (const :tag "None" :format "%t" nil)
7830 (integer :tag "Pixels" :size 5))
7831 (choice
7832 :tag "Right"
7833 :value nil
7834 :format "%[RightMargin%] %v "
7835 (const :tag "None" :format "%t" nil)
7836 (integer :tag "Pixels" :size 5))
7837 (choice
7838 :tag "Bottom"
7839 :value nil
7840 :format "%[BottomMargin%] %v "
7841 (const :tag "None" :format "%t" nil)
7842 (integer :tag "Pixels" :size 5)))
7843 :group 'help)
7845 (defcustom fit-frame-to-buffer-sizes '(nil nil nil nil)
7846 "Size boundaries of frame for `fit-frame-to-buffer'.
7847 This list specifies the total maximum and minimum lines and
7848 maximum and minimum columns of the root window of any frame that
7849 shall be fit to its buffer. If any of these values is non-nil,
7850 it overrides the corresponding argument of `fit-frame-to-buffer'.
7852 On window systems where the menubar can wrap, fitting a frame to
7853 its buffer may swallow the last line(s). Specifying an
7854 appropriate minimum width value here can avoid such wrapping.
7856 See also `fit-frame-to-buffer-margins'."
7857 :version "24.4"
7858 :type '(list
7859 (choice
7860 :tag "Maximum Height"
7861 :value nil
7862 :format "%[MaxHeight%] %v "
7863 (const :tag "None" :format "%t" nil)
7864 (integer :tag "Lines" :size 5))
7865 (choice
7866 :tag "Minimum Height"
7867 :value nil
7868 :format "%[MinHeight%] %v "
7869 (const :tag "None" :format "%t" nil)
7870 (integer :tag "Lines" :size 5))
7871 (choice
7872 :tag "Maximum Width"
7873 :value nil
7874 :format "%[MaxWidth%] %v "
7875 (const :tag "None" :format "%t" nil)
7876 (integer :tag "Columns" :size 5))
7877 (choice
7878 :tag "Minimum Width"
7879 :value nil
7880 :format "%[MinWidth%] %v\n"
7881 (const :tag "None" :format "%t" nil)
7882 (integer :tag "Columns" :size 5)))
7883 :group 'help)
7885 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
7887 (defun window--sanitize-margin (margin left right)
7888 "Return MARGIN if it's a number between LEFT and RIGHT."
7889 (when (and (numberp margin)
7890 (<= left (- right margin)) (<= margin right))
7891 margin))
7893 (declare-function tool-bar-height "xdisp.c" (&optional frame pixelwise))
7895 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only)
7896 "Adjust size of FRAME to display the contents of its buffer exactly.
7897 FRAME can be any live frame and defaults to the selected one.
7898 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
7899 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
7900 FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
7901 `window-min-height' and `window-min-width' respectively.
7903 If the optional argument ONLY is `vertically', resize the frame
7904 vertically only. If ONLY is `horizontally', resize the frame
7905 horizontally only.
7907 The new position and size of FRAME can be additionally determined
7908 by customizing the options `fit-frame-to-buffer-sizes' and
7909 `fit-frame-to-buffer-margins' or the corresponding parameters of
7910 FRAME."
7911 (interactive)
7912 (unless (and (fboundp 'x-display-pixel-height)
7913 ;; We need the respective sizes now.
7914 (fboundp 'display-monitor-attributes-list))
7915 (user-error "Cannot resize frame in non-graphic Emacs"))
7916 (setq frame (window-normalize-frame frame))
7917 (when (window-live-p (frame-root-window frame))
7918 (with-selected-window (frame-root-window frame)
7919 (let* ((char-width (frame-char-width))
7920 (char-height (frame-char-height))
7921 (monitor-attributes (car (display-monitor-attributes-list
7922 (frame-parameter frame 'display))))
7923 (geometry (cdr (assq 'geometry monitor-attributes)))
7924 (display-width (- (nth 2 geometry) (nth 0 geometry)))
7925 (display-height (- (nth 3 geometry) (nth 1 geometry)))
7926 (workarea (cdr (assq 'workarea monitor-attributes)))
7927 ;; Handle margins.
7928 (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins)
7929 fit-frame-to-buffer-margins))
7930 (left-margin (if (nth 0 margins)
7931 (or (window--sanitize-margin
7932 (nth 0 margins) 0 display-width)
7934 (nth 0 workarea)))
7935 (top-margin (if (nth 1 margins)
7936 (or (window--sanitize-margin
7937 (nth 1 margins) 0 display-height)
7939 (nth 1 workarea)))
7940 (workarea-width (nth 2 workarea))
7941 (right-margin (if (nth 2 margins)
7942 (- display-width
7943 (or (window--sanitize-margin
7944 (nth 2 margins) left-margin display-width)
7946 (nth 2 workarea)))
7947 (workarea-height (nth 3 workarea))
7948 (bottom-margin (if (nth 3 margins)
7949 (- display-height
7950 (or (window--sanitize-margin
7951 (nth 3 margins) top-margin display-height)
7953 (nth 3 workarea)))
7954 ;; The pixel width of FRAME (which does not include the
7955 ;; window manager's decorations).
7956 (frame-width (frame-pixel-width))
7957 ;; The pixel width of the body of FRAME's root window.
7958 (window-body-width (window-body-width nil t))
7959 ;; The difference in pixels between total and body width of
7960 ;; FRAME's window.
7961 (window-extra-width (- (window-pixel-width) window-body-width))
7962 ;; The difference in pixels between the frame's pixel width
7963 ;; and the window's body width. This is the space we can't
7964 ;; use for fitting.
7965 (extra-width (- frame-width window-body-width))
7966 ;; The pixel position of FRAME's left border. We usually
7967 ;; try to leave this alone.
7968 (left
7969 (let ((left (frame-parameter nil 'left)))
7970 (if (consp left)
7971 (funcall (car left) (cadr left))
7972 left)))
7973 ;; The pixel height of FRAME (which does not include title
7974 ;; line, decorations, and sometimes neither the menu nor
7975 ;; the toolbar).
7976 (frame-height (frame-pixel-height))
7977 ;; The pixel height of FRAME's root window (we don't care
7978 ;; about the window's body height since the return value of
7979 ;; `window-text-pixel-size' includes header and mode line).
7980 (window-height (window-pixel-height))
7981 ;; The difference in pixels between the frame's pixel
7982 ;; height and the window's height.
7983 (extra-height (- frame-height window-height))
7984 ;; The pixel position of FRAME's top border.
7985 (top
7986 (let ((top (frame-parameter nil 'top)))
7987 (if (consp top)
7988 (funcall (car top) (cadr top))
7989 top)))
7990 ;; Sanitize minimum and maximum sizes.
7991 (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes)
7992 fit-frame-to-buffer-sizes))
7993 (max-height
7994 (cond
7995 ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height))
7996 ((numberp max-height) (* max-height char-height))
7997 (t display-height)))
7998 (min-height
7999 (cond
8000 ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height))
8001 ((numberp min-height) (* min-height char-height))
8002 (t (* window-min-height char-height))))
8003 (max-width
8004 (cond
8005 ((numberp (nth 2 sizes))
8006 (- (* (nth 2 sizes) char-width) window-extra-width))
8007 ((numberp max-width)
8008 (- (* max-width char-width) window-extra-width))
8009 (t display-width)))
8010 (min-width
8011 (cond
8012 ((numberp (nth 3 sizes))
8013 (- (* (nth 3 sizes) char-width) window-extra-width))
8014 ((numberp min-width)
8015 (- (* min-width char-width) window-extra-width))
8016 (t (* window-min-width char-width))))
8017 ;; Note: Currently, for a new frame the sizes of the header
8018 ;; and mode line may be estimated incorrectly
8019 (value (window-text-pixel-size
8020 nil t t workarea-width workarea-height t))
8021 (width (+ (car value) (window-right-divider-width)))
8022 (height
8023 (+ (cdr value)
8024 (window-bottom-divider-width)
8025 (window-scroll-bar-height))))
8026 ;; Don't change height or width when the window's size is fixed
8027 ;; in either direction or ONLY forbids it.
8028 (cond
8029 ((or (eq window-size-fixed 'width) (eq only 'vertically))
8030 (setq width nil))
8031 ((or (eq window-size-fixed 'height) (eq only 'horizontally))
8032 (setq height nil)))
8033 ;; Fit width to constraints.
8034 (when width
8035 (unless frame-resize-pixelwise
8036 ;; Round to character sizes.
8037 (setq width (* (/ (+ width char-width -1) char-width)
8038 char-width)))
8039 ;; Fit to maximum and minimum widths.
8040 (setq width (max (min width max-width) min-width))
8041 ;; Add extra width.
8042 (setq width (+ width extra-width))
8043 ;; Preserve margins.
8044 (let ((right (+ left width)))
8045 (cond
8046 ((> right right-margin)
8047 ;; Move frame to left (we don't know its real width).
8048 (setq left (max left-margin (- left (- right right-margin)))))
8049 ((< left left-margin)
8050 ;; Move frame to right.
8051 (setq left left-margin)))))
8052 ;; Fit height to constraints.
8053 (when height
8054 (unless frame-resize-pixelwise
8055 (setq height (* (/ (+ height char-height -1) char-height)
8056 char-height)))
8057 ;; Fit to maximum and minimum heights.
8058 (setq height (max (min height max-height) min-height))
8059 ;; Add extra height.
8060 (setq height (+ height extra-height))
8061 ;; Preserve margins.
8062 (let ((bottom (+ top height)))
8063 (cond
8064 ((> bottom bottom-margin)
8065 ;; Move frame up (we don't know its real height).
8066 (setq top (max top-margin (- top (- bottom bottom-margin)))))
8067 ((< top top-margin)
8068 ;; Move frame down.
8069 (setq top top-margin)))))
8070 ;; Apply changes.
8071 (set-frame-position frame left top)
8072 ;; Clumsily try to translate our calculations to what
8073 ;; `set-frame-size' wants.
8074 (when width
8075 (setq width (- (+ (frame-text-width) width)
8076 extra-width window-body-width)))
8077 (when height
8078 (setq height (- (+ (frame-text-height) height)
8079 extra-height window-height)))
8080 (set-frame-size
8081 frame
8082 (if width
8083 (if frame-resize-pixelwise
8084 width
8085 (/ width char-width))
8086 (frame-text-width))
8087 (if height
8088 (if frame-resize-pixelwise
8089 height
8090 (/ height char-height))
8091 (frame-text-height))
8092 frame-resize-pixelwise)))))
8094 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width preserve-size)
8095 "Adjust size of WINDOW to display its buffer's contents exactly.
8096 WINDOW must be a live window and defaults to the selected one.
8098 If WINDOW is part of a vertical combination, adjust WINDOW's
8099 height. The new height is calculated from the actual height of
8100 the accessible portion of its buffer. The optional argument
8101 MAX-HEIGHT specifies a maximum height and defaults to the height
8102 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
8103 minimum height and defaults to `window-min-height'. Both
8104 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
8105 and header line and a bottom divider, if any.
8107 If WINDOW is part of a horizontal combination and the value of
8108 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
8109 WINDOW's width. The new width of WINDOW is calculated from the
8110 maximum length of its buffer's lines that follow the current
8111 start position of WINDOW. The optional argument MAX-WIDTH
8112 specifies a maximum width and defaults to the width of WINDOW's
8113 frame. The optional argument MIN-WIDTH specifies a minimum width
8114 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
8115 are specified in columns and include fringes, margins, a
8116 scrollbar and a vertical divider, if any.
8118 If the optional argument `preserve-size' is non-nil, preserve the
8119 size of WINDOW (see `window-preserve-size').
8121 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
8122 If WINDOW is its frame's root window and the option
8123 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
8124 adjust the frame's size.
8126 Note that even if this function makes WINDOW large enough to show
8127 _all_ parts of its buffer you might not see the first part when
8128 WINDOW was scrolled. If WINDOW is resized horizontally, you will
8129 not see the top of its buffer unless WINDOW starts at its minimum
8130 accessible position."
8131 (interactive)
8132 (setq window (window-normalize-window window t))
8133 (if (eq window (frame-root-window window))
8134 (when fit-frame-to-buffer
8135 ;; Fit WINDOW's frame to buffer.
8136 (fit-frame-to-buffer
8137 (window-frame window)
8138 max-height min-height max-width min-width
8139 (and (memq fit-frame-to-buffer '(vertically horizontally))
8140 fit-frame-to-buffer)))
8141 (with-selected-window window
8142 (let* ((pixelwise window-resize-pixelwise)
8143 (char-height (frame-char-height))
8144 (char-width (frame-char-width))
8145 (total-height (window-size window nil pixelwise))
8146 (body-height (window-body-height window pixelwise))
8147 (body-width (window-body-width window pixelwise))
8148 (min-height
8149 ;; Sanitize MIN-HEIGHT.
8150 (if (numberp min-height)
8151 ;; Can't get smaller than `window-safe-min-height'.
8152 (max (if pixelwise
8153 (* char-height min-height)
8154 min-height)
8155 (if pixelwise
8156 (window-safe-min-pixel-height window)
8157 window-safe-min-height))
8158 ;; Preserve header and mode line if present.
8159 (max (if pixelwise
8160 (* char-height window-min-height)
8161 window-min-height)
8162 (window-min-size window nil window pixelwise))))
8163 (max-height
8164 ;; Sanitize MAX-HEIGHT.
8165 (if (numberp max-height)
8166 (min
8167 (+ total-height
8168 (window-max-delta
8169 window nil window nil t nil pixelwise))
8170 (if pixelwise
8171 (* char-height max-height)
8172 max-height))
8173 (+ total-height (window-max-delta
8174 window nil window nil t nil pixelwise))))
8175 height)
8176 (cond
8177 ;; If WINDOW is vertically combined, try to resize it
8178 ;; vertically.
8179 ((and (not (eq fit-window-to-buffer-horizontally 'only))
8180 (not (window-size-fixed-p window 'preserved))
8181 (window-combined-p))
8182 ;; Vertically we always want to fit the entire buffer.
8183 ;; WINDOW'S height can't get larger than its frame's pixel
8184 ;; height. Its width remains fixed.
8185 (setq height (+ (cdr (window-text-pixel-size
8186 nil nil t nil (frame-pixel-height) t))
8187 (window-scroll-bar-height window)
8188 (window-bottom-divider-width)))
8189 ;; Round height.
8190 (unless pixelwise
8191 (setq height (/ (+ height char-height -1) char-height)))
8192 (unless (= height total-height)
8193 (window-preserve-size window)
8194 (window-resize-no-error
8195 window
8196 (- (max min-height (min max-height height)) total-height)
8197 nil window pixelwise)
8198 (when preserve-size
8199 (window-preserve-size window nil t))))
8200 ;; If WINDOW is horizontally combined, try to resize it
8201 ;; horizontally.
8202 ((and fit-window-to-buffer-horizontally
8203 (not (window-size-fixed-p window t 'preserved))
8204 (window-combined-p nil t))
8205 (let* ((total-width (window-size window t pixelwise))
8206 (min-width
8207 ;; Sanitize MIN-WIDTH.
8208 (if (numberp min-width)
8209 ;; Can't get smaller than `window-safe-min-width'.
8210 (max (if pixelwise
8211 (* char-width min-width)
8212 min-width)
8213 (if pixelwise
8214 (window-safe-min-pixel-width)
8215 window-safe-min-width))
8216 ;; Preserve fringes, margins, scrollbars if present.
8217 (max (if pixelwise
8218 (* char-width window-min-width)
8219 window-min-width)
8220 (window-min-size nil nil window pixelwise))))
8221 (max-width
8222 ;; Sanitize MAX-WIDTH.
8223 (if (numberp max-width)
8224 (min (+ total-width
8225 (window-max-delta
8226 window t window nil t nil pixelwise))
8227 (if pixelwise
8228 (* char-width max-width)
8229 max-width))
8230 (+ total-width (window-max-delta
8231 window t window nil t nil pixelwise))))
8232 ;; When fitting horizontally, assume that WINDOW's
8233 ;; start position remains unaltered. WINDOW can't get
8234 ;; wider than its frame's pixel width, its height
8235 ;; remains unaltered.
8236 (width (+ (car (window-text-pixel-size
8237 nil (window-start) (point-max)
8238 (frame-pixel-width)
8239 ;; Add one char-height to assure that
8240 ;; we're on the safe side. This
8241 ;; overshoots when the first line below
8242 ;; the bottom is wider than the window.
8243 (* body-height
8244 (if pixelwise 1 char-height))))
8245 (window-right-divider-width))))
8246 (unless pixelwise
8247 (setq width (/ (+ width char-width -1) char-width)))
8248 (unless (= width body-width)
8249 (window-preserve-size window t)
8250 (window-resize-no-error
8251 window
8252 (- (max min-width
8253 (min max-width
8254 (+ total-width (- width body-width))))
8255 total-width)
8256 t window pixelwise)
8257 (when preserve-size
8258 (window-preserve-size window t t))))))))))
8260 (defun window-safely-shrinkable-p (&optional window)
8261 "Return t if WINDOW can be shrunk without shrinking other windows.
8262 WINDOW defaults to the selected window."
8263 (with-selected-window (or window (selected-window))
8264 (let ((edges (window-edges)))
8265 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
8266 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
8268 (defun shrink-window-if-larger-than-buffer (&optional window)
8269 "Shrink height of WINDOW if its buffer doesn't need so many lines.
8270 More precisely, shrink WINDOW vertically to be as small as
8271 possible, while still showing the full contents of its buffer.
8272 WINDOW must be a live window and defaults to the selected one.
8274 Do not shrink WINDOW to less than `window-min-height' lines. Do
8275 nothing if the buffer contains more lines than the present window
8276 height, or if some of the window's contents are scrolled out of
8277 view, or if shrinking this window would also shrink another
8278 window, or if the window is the only window of its frame.
8280 Return non-nil if the window was shrunk, nil otherwise."
8281 (interactive)
8282 (setq window (window-normalize-window window t))
8283 ;; Make sure that WINDOW is vertically combined and `point-min' is
8284 ;; visible (for whatever reason that's needed). The remaining issues
8285 ;; should be taken care of by `fit-window-to-buffer'.
8286 (when (and (window-combined-p window)
8287 (pos-visible-in-window-p (point-min) window))
8288 (fit-window-to-buffer window (window-total-height window))))
8290 (defun kill-buffer-and-window ()
8291 "Kill the current buffer and delete the selected window."
8292 (interactive)
8293 (let ((window-to-delete (selected-window))
8294 (buffer-to-kill (current-buffer))
8295 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
8296 (unwind-protect
8297 (progn
8298 (add-hook 'kill-buffer-hook delete-window-hook t t)
8299 (if (kill-buffer (current-buffer))
8300 ;; If `delete-window' failed before, we rerun it to regenerate
8301 ;; the error so it can be seen in the echo area.
8302 (when (eq (selected-window) window-to-delete)
8303 (delete-window))))
8304 ;; If the buffer is not dead for some reason (probably because
8305 ;; of a `quit' signal), remove the hook again.
8306 (ignore-errors
8307 (with-current-buffer buffer-to-kill
8308 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
8312 ;; Groups of windows (Follow Mode).
8314 ;; This section of functions extends the functionality of some window
8315 ;; manipulating commands to groups of windows cooperatively
8316 ;; displaying a buffer, typically with Follow Mode.
8318 ;; The xxx-function variables are permanent locals so that their local
8319 ;; status is undone only when explicitly programmed, not when a buffer
8320 ;; is reverted or a mode function is called.
8322 (defvar window-group-start-function nil)
8323 (make-variable-buffer-local 'window-group-start-function)
8324 (put 'window-group-start-function 'permanent-local t)
8325 (defun window-group-start (&optional window)
8326 "Return position at which display currently starts in the group of
8327 windows containing WINDOW. When a grouping mode (such as Follow Mode)
8328 is not active, this function is identical to `window-start'.
8330 WINDOW must be a live window and defaults to the selected one.
8331 This is updated by redisplay or by calling `set-window*-start'."
8332 (if (functionp window-group-start-function)
8333 (funcall window-group-start-function window)
8334 (window-start window)))
8336 (defvar window-group-end-function nil)
8337 (make-variable-buffer-local 'window-group-end-function)
8338 (put 'window-group-end-function 'permanent-local t)
8339 (defun window-group-end (&optional window update)
8340 "Return position at which display currently ends in the group of
8341 windows containing WINDOW. When a grouping mode (such as Follow Mode)
8342 is not active, this function is identical to `window-end'.
8344 WINDOW must be a live window and defaults to the selected one.
8345 This is updated by redisplay, when it runs to completion.
8346 Simply changing the buffer text or setting `window-group-start'
8347 does not update this value.
8348 Return nil if there is no recorded value. (This can happen if the
8349 last redisplay of WINDOW was preempted, and did not finish.)
8350 If UPDATE is non-nil, compute the up-to-date position
8351 if it isn't already recorded."
8352 (if (functionp window-group-end-function)
8353 (funcall window-group-end-function window update)
8354 (window-end window update)))
8356 (defvar set-window-group-start-function nil)
8357 (make-variable-buffer-local 'set-window-group-start-function)
8358 (put 'set-window-group-start-function 'permanent-local t)
8359 (defun set-window-group-start (window pos &optional noforce)
8360 "Make display in the group of windows containing WINDOW start at
8361 position POS in WINDOW's buffer. When a grouping mode (such as Follow
8362 Mode) is not active, this function is identical to `set-window-start'.
8364 WINDOW must be a live window and defaults to the selected one. Return
8365 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
8366 overriding motion of point in order to display at this exact start."
8367 (if (functionp set-window-group-start-function)
8368 (funcall set-window-group-start-function window pos noforce)
8369 (set-window-start window pos noforce)))
8371 (defvar recenter-window-group-function nil)
8372 (make-variable-buffer-local 'recenter-window-group-function)
8373 (put 'recenter-window-group-function 'permanent-local t)
8374 (defun recenter-window-group (&optional arg)
8375 "Center point in the group of windows containing the selected window
8376 and maybe redisplay frame. When a grouping mode (such as Follow Mode)
8377 is not active, this function is identical to `recenter'.
8379 With a numeric prefix argument ARG, recenter putting point on screen line ARG
8380 relative to the first window in the selected window group. If ARG is
8381 negative, it counts up from the bottom of the last window in the
8382 group. (ARG should be less than the total height of the window group.)
8384 If ARG is omitted or nil, then recenter with point on the middle line of
8385 the selected window group; if the variable `recenter-redisplay' is
8386 non-nil, also erase the entire frame and redraw it (when
8387 `auto-resize-tool-bars' is set to `grow-only', this resets the
8388 tool-bar's height to the minimum height needed); if
8389 `recenter-redisplay' has the special value `tty', then only tty frames
8390 are redrawn.
8392 Just C-u as prefix means put point in the center of the window
8393 and redisplay normally--don't erase and redraw the frame."
8394 (if (functionp recenter-window-group-function)
8395 (funcall recenter-window-group-function arg)
8396 (recenter arg)))
8398 (defvar pos-visible-in-window-group-p-function nil)
8399 (make-variable-buffer-local 'pos-visible-in-window-group-p-function)
8400 (put 'pos-visible-in-window-group-p-function 'permanent-local t)
8401 (defun pos-visible-in-window-group-p (&optional pos window partially)
8402 "Return non-nil if position POS is currently on the frame in the
8403 window group containing WINDOW. When a grouping mode (such as Follow
8404 Mode) is not active, this function is identical to
8405 `pos-visible-in-window-p'.
8407 WINDOW must be a live window and defaults to the selected one.
8409 Return nil if that position is scrolled vertically out of view. If a
8410 character is only partially visible, nil is returned, unless the
8411 optional argument PARTIALLY is non-nil. If POS is only out of view
8412 because of horizontal scrolling, return non-nil. If POS is t, it
8413 specifies the position of the last visible glyph in the window group.
8414 POS defaults to point in WINDOW; WINDOW defaults to the selected
8415 window.
8417 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
8418 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
8419 where X and Y are the pixel coordinates relative to the top left corner
8420 of the window. The remaining elements are omitted if the character after
8421 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
8422 off-window at the top and bottom of the screen line (\"row\") containing
8423 POS, ROWH is the visible height of that row, and VPOS is the row number
8424 \(zero-based)."
8425 (if (functionp pos-visible-in-window-group-p-function)
8426 (funcall pos-visible-in-window-group-p-function pos window partially)
8427 (pos-visible-in-window-p pos window partially)))
8429 (defvar selected-window-group-function nil)
8430 (make-variable-buffer-local 'selected-window-group-function)
8431 (put 'selected-window-group-function 'permanent-local t)
8432 (defun selected-window-group ()
8433 "Return the list of windows in the group containing the selected window.
8434 When a grouping mode (such as Follow Mode) is not active, the
8435 result is a list containing only the selected window."
8436 (if (functionp selected-window-group-function)
8437 (funcall selected-window-group-function)
8438 (list (selected-window))))
8440 (defvar move-to-window-group-line-function nil)
8441 (make-variable-buffer-local 'move-to-window-group-line-function)
8442 (put 'move-to-window-group-line-function 'permanent-local t)
8443 (defun move-to-window-group-line (arg)
8444 "Position point relative to the the current group of windows.
8445 When a grouping mode (such as Follow Mode) is not active, this
8446 function is identical to `move-to-window-line'.
8448 ARG nil means position point at center of the window group.
8449 Else, ARG specifies the vertical position within the window
8450 group; zero means top of first window in the group, negative
8451 means relative to the bottom of the last window in the group."
8452 (if (functionp move-to-window-group-line-function)
8453 (funcall move-to-window-group-line-function arg)
8454 (move-to-window-line arg)))
8457 (defvar recenter-last-op nil
8458 "Indicates the last recenter operation performed.
8459 Possible values: `top', `middle', `bottom', integer or float numbers.
8460 It can also be nil, which means the first value in `recenter-positions'.")
8462 (defcustom recenter-positions '(middle top bottom)
8463 "Cycling order for `recenter-top-bottom'.
8464 A list of elements with possible values `top', `middle', `bottom',
8465 integer or float numbers that define the cycling order for
8466 the command `recenter-top-bottom'.
8468 Top and bottom destinations are `scroll-margin' lines from the true
8469 window top and bottom. Middle redraws the frame and centers point
8470 vertically within the window. Integer number moves current line to
8471 the specified absolute window-line. Float number between 0.0 and 1.0
8472 means the percentage of the screen space from the top. The default
8473 cycling order is middle -> top -> bottom."
8474 :type '(repeat (choice
8475 (const :tag "Top" top)
8476 (const :tag "Middle" middle)
8477 (const :tag "Bottom" bottom)
8478 (integer :tag "Line number")
8479 (float :tag "Percentage")))
8480 :version "23.2"
8481 :group 'windows)
8483 (defun recenter-top-bottom (&optional arg)
8484 "Move current buffer line to the specified window line.
8485 With no prefix argument, successive calls place point according
8486 to the cycling order defined by `recenter-positions'.
8488 A prefix argument is handled like `recenter':
8489 With numeric prefix ARG, move current line to window-line ARG.
8490 With plain `C-u', move current line to window center."
8491 (interactive "P")
8492 (cond
8493 (arg (recenter arg)) ; Always respect ARG.
8495 (setq recenter-last-op
8496 (if (eq this-command last-command)
8497 (car (or (cdr (member recenter-last-op recenter-positions))
8498 recenter-positions))
8499 (car recenter-positions)))
8500 (let ((this-scroll-margin
8501 (min (max 0 scroll-margin)
8502 (truncate (/ (window-body-height) 4.0)))))
8503 (cond ((eq recenter-last-op 'middle)
8504 (recenter))
8505 ((eq recenter-last-op 'top)
8506 (recenter this-scroll-margin))
8507 ((eq recenter-last-op 'bottom)
8508 (recenter (- -1 this-scroll-margin)))
8509 ((integerp recenter-last-op)
8510 (recenter recenter-last-op))
8511 ((floatp recenter-last-op)
8512 (recenter (round (* recenter-last-op (window-height))))))))))
8514 (define-key global-map [?\C-l] 'recenter-top-bottom)
8516 (defun move-to-window-line-top-bottom (&optional arg)
8517 "Position point relative to window.
8519 With a prefix argument ARG, acts like `move-to-window-line'.
8521 With no argument, positions point at center of window.
8522 Successive calls position point at positions defined
8523 by `recenter-positions'."
8524 (interactive "P")
8525 (cond
8526 (arg (move-to-window-line arg)) ; Always respect ARG.
8528 (setq recenter-last-op
8529 (if (eq this-command last-command)
8530 (car (or (cdr (member recenter-last-op recenter-positions))
8531 recenter-positions))
8532 (car recenter-positions)))
8533 (let ((this-scroll-margin
8534 (min (max 0 scroll-margin)
8535 (truncate (/ (window-body-height) 4.0)))))
8536 (cond ((eq recenter-last-op 'middle)
8537 (call-interactively 'move-to-window-line))
8538 ((eq recenter-last-op 'top)
8539 (move-to-window-line this-scroll-margin))
8540 ((eq recenter-last-op 'bottom)
8541 (move-to-window-line (- -1 this-scroll-margin)))
8542 ((integerp recenter-last-op)
8543 (move-to-window-line recenter-last-op))
8544 ((floatp recenter-last-op)
8545 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
8547 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
8549 ;;; Scrolling commands.
8551 ;;; Scrolling commands which do not signal errors at top/bottom
8552 ;;; of buffer at first key-press (instead move to top/bottom
8553 ;;; of buffer).
8555 (defcustom scroll-error-top-bottom nil
8556 "Move point to top/bottom of buffer before signaling a scrolling error.
8557 A value of nil means just signal an error if no more scrolling possible.
8558 A value of t means point moves to the beginning or the end of the buffer
8559 \(depending on scrolling direction) when no more scrolling possible.
8560 When point is already on that position, then signal an error."
8561 :type 'boolean
8562 :group 'windows
8563 :version "24.1")
8565 (defun scroll-up-command (&optional arg)
8566 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
8567 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
8568 scroll window further, move cursor to the bottom line.
8569 When point is already on that position, then signal an error.
8570 A near full screen is `next-screen-context-lines' less than a full screen.
8571 Negative ARG means scroll downward.
8572 If ARG is the atom `-', scroll downward by nearly full screen."
8573 (interactive "^P")
8574 (cond
8575 ((null scroll-error-top-bottom)
8576 (scroll-up arg))
8577 ((eq arg '-)
8578 (scroll-down-command nil))
8579 ((< (prefix-numeric-value arg) 0)
8580 (scroll-down-command (- (prefix-numeric-value arg))))
8581 ((eobp)
8582 (scroll-up arg)) ; signal error
8584 (condition-case nil
8585 (scroll-up arg)
8586 (end-of-buffer
8587 (if arg
8588 ;; When scrolling by ARG lines can't be done,
8589 ;; move by ARG lines instead.
8590 (forward-line arg)
8591 ;; When ARG is nil for full-screen scrolling,
8592 ;; move to the bottom of the buffer.
8593 (goto-char (point-max))))))))
8595 (put 'scroll-up-command 'scroll-command t)
8597 (defun scroll-down-command (&optional arg)
8598 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
8599 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
8600 scroll window further, move cursor to the top line.
8601 When point is already on that position, then signal an error.
8602 A near full screen is `next-screen-context-lines' less than a full screen.
8603 Negative ARG means scroll upward.
8604 If ARG is the atom `-', scroll upward by nearly full screen."
8605 (interactive "^P")
8606 (cond
8607 ((null scroll-error-top-bottom)
8608 (scroll-down arg))
8609 ((eq arg '-)
8610 (scroll-up-command nil))
8611 ((< (prefix-numeric-value arg) 0)
8612 (scroll-up-command (- (prefix-numeric-value arg))))
8613 ((bobp)
8614 (scroll-down arg)) ; signal error
8616 (condition-case nil
8617 (scroll-down arg)
8618 (beginning-of-buffer
8619 (if arg
8620 ;; When scrolling by ARG lines can't be done,
8621 ;; move by ARG lines instead.
8622 (forward-line (- arg))
8623 ;; When ARG is nil for full-screen scrolling,
8624 ;; move to the top of the buffer.
8625 (goto-char (point-min))))))))
8627 (put 'scroll-down-command 'scroll-command t)
8629 ;;; Scrolling commands which scroll a line instead of full screen.
8631 (defun scroll-up-line (&optional arg)
8632 "Scroll text of selected window upward ARG lines; or one line if no ARG.
8633 If ARG is omitted or nil, scroll upward by one line.
8634 This is different from `scroll-up-command' that scrolls a full screen."
8635 (interactive "p")
8636 (scroll-up (or arg 1)))
8638 (put 'scroll-up-line 'scroll-command t)
8640 (defun scroll-down-line (&optional arg)
8641 "Scroll text of selected window down ARG lines; or one line if no ARG.
8642 If ARG is omitted or nil, scroll down by one line.
8643 This is different from `scroll-down-command' that scrolls a full screen."
8644 (interactive "p")
8645 (scroll-down (or arg 1)))
8647 (put 'scroll-down-line 'scroll-command t)
8650 (defun scroll-other-window-down (&optional lines)
8651 "Scroll the \"other window\" down.
8652 For more details, see the documentation for `scroll-other-window'."
8653 (interactive "P")
8654 (scroll-other-window
8655 ;; Just invert the argument's meaning.
8656 ;; We can do that without knowing which window it will be.
8657 (if (eq lines '-) nil
8658 (if (null lines) '-
8659 (- (prefix-numeric-value lines))))))
8661 (defun beginning-of-buffer-other-window (arg)
8662 "Move point to the beginning of the buffer in the other window.
8663 Leave mark at previous position.
8664 With arg N, put point N/10 of the way from the true beginning."
8665 (interactive "P")
8666 (let ((orig-window (selected-window))
8667 (window (other-window-for-scrolling)))
8668 ;; We use unwind-protect rather than save-window-excursion
8669 ;; because the latter would preserve the things we want to change.
8670 (unwind-protect
8671 (progn
8672 (select-window window)
8673 ;; Set point and mark in that window's buffer.
8674 (with-no-warnings
8675 (beginning-of-buffer arg))
8676 ;; Set point accordingly.
8677 (recenter '(t)))
8678 (select-window orig-window))))
8680 (defun end-of-buffer-other-window (arg)
8681 "Move point to the end of the buffer in the other window.
8682 Leave mark at previous position.
8683 With arg N, put point N/10 of the way from the true end."
8684 (interactive "P")
8685 ;; See beginning-of-buffer-other-window for comments.
8686 (let ((orig-window (selected-window))
8687 (window (other-window-for-scrolling)))
8688 (unwind-protect
8689 (progn
8690 (select-window window)
8691 (with-no-warnings
8692 (end-of-buffer arg))
8693 (recenter '(t)))
8694 (select-window orig-window))))
8696 (defvar mouse-autoselect-window-timer nil
8697 "Timer used by delayed window autoselection.")
8699 (defvar mouse-autoselect-window-position-1 nil
8700 "First mouse position recorded by delayed window autoselection.")
8702 (defvar mouse-autoselect-window-position nil
8703 "Last mouse position recorded by delayed window autoselection.")
8705 (defvar mouse-autoselect-window-window nil
8706 "Last window recorded by delayed window autoselection.")
8708 (defvar mouse-autoselect-window-state nil
8709 "When non-nil, special state of delayed window autoselection.
8710 Possible values are `suspend' (suspend autoselection after a menu or
8711 scrollbar interaction) and `select' (the next invocation of
8712 `handle-select-window' shall select the window immediately).")
8714 (defun mouse-autoselect-window-cancel (&optional force)
8715 "Cancel delayed window autoselection.
8716 Optional argument FORCE means cancel unconditionally."
8717 (unless (and (not force)
8718 ;; Don't cancel for select-window or select-frame events
8719 ;; or when the user drags a scroll bar.
8720 (or (memq this-command
8721 '(handle-select-window handle-switch-frame))
8722 (and (eq this-command 'scroll-bar-toolkit-scroll)
8723 (memq (nth 4 (event-end last-input-event))
8724 '(handle end-scroll)))))
8725 (setq mouse-autoselect-window-state nil)
8726 (setq mouse-autoselect-window-position-1 nil)
8727 (when (timerp mouse-autoselect-window-timer)
8728 (cancel-timer mouse-autoselect-window-timer))
8729 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
8731 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
8732 "Start delayed window autoselection.
8733 MOUSE-POSITION is the last position where the mouse was seen as returned
8734 by `mouse-position'. Optional argument WINDOW non-nil denotes the
8735 window where the mouse was seen. Optional argument SUSPEND non-nil
8736 means suspend autoselection."
8737 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
8738 (setq mouse-autoselect-window-position mouse-position)
8739 (when window (setq mouse-autoselect-window-window window))
8740 (setq mouse-autoselect-window-state (when suspend 'suspend))
8741 ;; Install timer which runs `mouse-autoselect-window-select' after
8742 ;; `mouse-autoselect-window' seconds.
8743 (setq mouse-autoselect-window-timer
8744 (run-at-time
8745 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
8747 (defun mouse-autoselect-window-select ()
8748 "Select window with delayed window autoselection.
8749 If the mouse position has stabilized in a non-selected window, select
8750 that window. The minibuffer window is selected only if the minibuffer
8751 is active. This function is run by `mouse-autoselect-window-timer'."
8752 (let* ((mouse-position (mouse-position))
8753 (mouse-x (and (numberp (cadr mouse-position))
8754 (cadr mouse-position)))
8755 (mouse-y (and (numberp (cddr mouse-position))
8756 (cddr mouse-position)))
8757 (frame (and mouse-x mouse-y (car mouse-position)))
8758 (window (and frame (window-at mouse-x mouse-y frame))))
8759 (cond
8760 ((or (and (fboundp 'menu-or-popup-active-p) (menu-or-popup-active-p))
8761 (and window
8762 (let ((coords (coordinates-in-window-p
8763 (cdr mouse-position) window)))
8764 (and (not (consp coords))
8765 (not (memq coords '(left-margin right-margin)))))))
8766 ;; A menu / popup dialog is active or the mouse is not on the
8767 ;; text region of WINDOW: Suspend autoselection temporarily.
8768 (mouse-autoselect-window-start mouse-position nil t))
8769 ((or (eq mouse-autoselect-window-state 'suspend)
8770 ;; When the mouse is at its first recorded position, restart
8771 ;; delayed autoselection. This works around a scenario with
8772 ;; two two-window frames with identical dimensions: select the
8773 ;; first window of the first frame, switch to the second
8774 ;; frame, move the mouse to its second window, minimize the
8775 ;; second frame. Now the second window of the first frame
8776 ;; gets selected although the mouse never really "moved" into
8777 ;; that window.
8778 (and (numberp mouse-autoselect-window)
8779 (equal (mouse-position) mouse-autoselect-window-position-1)))
8780 ;; Delayed autoselection was temporarily suspended, reenable it.
8781 (mouse-autoselect-window-start mouse-position))
8782 ((and window
8783 (or (not (numberp mouse-autoselect-window))
8784 (and (>= mouse-autoselect-window 0)
8785 ;; If `mouse-autoselect-window' is non-negative,
8786 ;; select window if it's the same as before.
8787 (eq window mouse-autoselect-window-window))
8788 ;; Otherwise select window iff the mouse is at the same
8789 ;; position as before. Observe that the first test
8790 ;; after starting autoselection usually fails since the
8791 ;; value of `mouse-autoselect-window-position' recorded
8792 ;; there is the position where the mouse has entered the
8793 ;; new window and not necessarily where the mouse has
8794 ;; stopped moving.
8795 (equal mouse-position mouse-autoselect-window-position))
8796 ;; The minibuffer is a candidate window if it's active.
8797 (or (not (window-minibuffer-p window))
8798 (eq window (active-minibuffer-window))))
8799 ;; Mouse position has stabilized in non-selected window: Cancel
8800 ;; delayed autoselection and try to select that window.
8801 (mouse-autoselect-window-cancel t)
8802 ;; Use `unread-command-events' in order to execute pre- and
8803 ;; post-command hooks and trigger idle timers. To avoid delaying
8804 ;; autoselection again, set `mouse-autoselect-window-state'."
8805 (setq mouse-autoselect-window-state 'select)
8806 (setq unread-command-events
8807 (cons (list 'select-window (list window))
8808 unread-command-events)))
8809 ((or (not (numberp mouse-autoselect-window))
8810 (equal mouse-position mouse-autoselect-window-position))
8811 ;; Mouse position has stabilized at
8812 ;; `mouse-autoselect-window-position': Cancel delayed
8813 ;; autoselection.
8814 (mouse-autoselect-window-cancel t))
8815 (window
8816 ;; Mouse position has not stabilized yet, resume delayed
8817 ;; autoselection.
8818 (mouse-autoselect-window-start mouse-position window)))))
8820 (defun handle-select-window (event)
8821 "Handle select-window events."
8822 (interactive "^e")
8823 (let* ((window (posn-window (event-start event)))
8824 (frame (and (window-live-p window) (window-frame window)))
8825 (old-frame (selected-frame)))
8826 (unless (or (not (window-live-p window))
8827 ;; Don't switch when autoselection shall be delayed.
8828 (and (numberp mouse-autoselect-window)
8829 (not (eq mouse-autoselect-window-state 'select))
8830 (let ((position (mouse-position)))
8831 ;; Cancel any delayed autoselection.
8832 (mouse-autoselect-window-cancel t)
8833 ;; Start delayed autoselection from current mouse
8834 ;; position and window.
8835 (setq mouse-autoselect-window-position-1 position)
8836 (mouse-autoselect-window-start position window)
8837 ;; Executing a command cancels delayed autoselection.
8838 (add-hook
8839 'pre-command-hook 'mouse-autoselect-window-cancel)))
8840 ;; Don't switch to a `no-accept-focus' frame unless it's
8841 ;; already selected.
8842 (and (not (eq frame (selected-frame)))
8843 (frame-parameter frame 'no-accept-focus))
8844 ;; Don't switch to minibuffer window unless it's active.
8845 (and (window-minibuffer-p window)
8846 (not (minibuffer-window-active-p window))))
8847 ;; Reset state of delayed autoselection.
8848 (setq mouse-autoselect-window-state nil)
8849 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
8850 (run-hooks 'mouse-leave-buffer-hook)
8851 ;; Clear echo area.
8852 (message nil)
8853 ;; Select the window before giving the frame focus since otherwise
8854 ;; we might get two windows with an active cursor.
8855 (select-window window)
8856 (cond
8857 ((or (not (memq (window-system frame) '(x w32 ns)))
8858 (not focus-follows-mouse)
8859 ;; Focus FRAME if it's either a child frame or an ancestor
8860 ;; of the frame switched from.
8861 (and (not (frame-parameter frame 'parent-frame))
8862 (not (frame-ancestor-p frame old-frame)))))
8863 ((eq focus-follows-mouse 'auto-raise)
8864 ;; Focus and auto-raise frame.
8865 (x-focus-frame frame)
8866 ;; This doesn't seem to work when we move from a normal frame
8867 ;; right into the child frame of another frame - we should raise
8868 ;; that child frame's ancestor frame first ...
8869 (raise-frame frame))
8871 ;; Just focus frame.
8872 (x-focus-frame frame t))))))
8874 (defun truncated-partial-width-window-p (&optional window)
8875 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
8876 WINDOW must be a live window and defaults to the selected one.
8877 Return nil if WINDOW is not a partial-width window
8878 (regardless of the value of `truncate-lines').
8879 Otherwise, consult the value of `truncate-partial-width-windows'
8880 for the buffer shown in WINDOW."
8881 (setq window (window-normalize-window window t))
8882 (unless (window-full-width-p window)
8883 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
8884 (window-buffer window))))
8885 (if (integerp t-p-w-w)
8886 (< (window-width window) t-p-w-w)
8887 t-p-w-w))))
8890 ;; Automatically inform subprocesses of changes to window size.
8892 (defcustom window-adjust-process-window-size-function
8893 'window-adjust-process-window-size-smallest
8894 "Control how Emacs chooses inferior process window sizes.
8895 Emacs uses this function to tell processes the space they have
8896 available for displaying their output. After each window
8897 configuration change, Emacs calls the value of
8898 `window-adjust-process-window-size-function' for each process
8899 with a buffer being displayed in at least one window.
8900 This function is responsible for combining the sizes of the
8901 displayed windows and returning a cons (WIDTH . HEIGHT)
8902 describing the width and height with which Emacs will call
8903 `set-process-window-size' for that process. If the function
8904 returns nil, Emacs does not call `set-process-window-size'.
8906 This function is called with the process buffer as the current
8907 buffer and with two arguments: the process and a list of windows
8908 displaying process. Modes can make this variable buffer-local;
8909 additionally, the `adjust-window-size-function' process property
8910 overrides the global or buffer-local value of
8911 `window-adjust-process-window-size-function'."
8912 :type '(choice
8913 (const :tag "Minimum area of any window"
8914 window-adjust-process-window-size-smallest)
8915 (const :tag "Maximum area of any window"
8916 window-adjust-process-window-size-largest)
8917 (const :tag "Do not adjust process window sizes" ignore)
8918 function)
8919 :group 'windows
8920 :version "25.1")
8922 (defun window-adjust-process-window-size (reducer windows)
8923 "Adjust the window sizes of a process.
8924 WINDOWS is a list of windows associated with that process. REDUCER is
8925 a two-argument function used to combine the widths and heights of
8926 the given windows."
8927 (when windows
8928 (let ((width (window-max-chars-per-line (car windows)))
8929 (height (window-body-height (car windows))))
8930 (dolist (window (cdr windows))
8931 (setf width (funcall reducer width (window-max-chars-per-line window)))
8932 (setf height (funcall reducer height (window-body-height window))))
8933 (cons width height))))
8935 (defun window-adjust-process-window-size-smallest (_process windows)
8936 "Adjust the process window size of PROCESS.
8937 WINDOWS is a list of windows associated with PROCESS. Choose the
8938 smallest area available for displaying PROCESS's output."
8939 (window-adjust-process-window-size #'min windows))
8941 (defun window-adjust-process-window-size-largest (_process windows)
8942 "Adjust the process window size of PROCESS.
8943 WINDOWS is a list of windows associated with PROCESS. Choose the
8944 largest area available for displaying PROCESS's output."
8945 (window-adjust-process-window-size #'max windows))
8947 (defun window--process-window-list ()
8948 "Return an alist mapping processes to associated windows.
8949 A window is associated with a process if that window is
8950 displaying that processes's buffer."
8951 (let ((processes (process-list))
8952 (process-windows nil))
8953 (if processes
8954 (walk-windows
8955 (lambda (window)
8956 (let ((buffer (window-buffer window))
8957 (iter processes))
8958 (while (let ((process (car iter)))
8959 (if (and (process-live-p process)
8960 (eq buffer (process-buffer process)))
8961 (let ((procwin (assq process process-windows)))
8962 ;; Add this window to the list of windows
8963 ;; displaying process.
8964 (if procwin
8965 (push window (cdr procwin))
8966 (push (list process window) process-windows))
8967 ;; We found our process for this window, so
8968 ;; stop iterating over the process list.
8969 nil)
8970 (setf iter (cdr iter)))))))
8971 1 t))
8972 process-windows))
8974 (defun window--adjust-process-windows ()
8975 "Update process window sizes to match the current window configuration."
8976 (when (fboundp 'process-list)
8977 (dolist (procwin (window--process-window-list))
8978 (let ((process (car procwin)))
8979 (with-demoted-errors "Error adjusting window size: %S"
8980 (with-current-buffer (process-buffer process)
8981 (let ((size (funcall
8982 (or (process-get process 'adjust-window-size-function)
8983 window-adjust-process-window-size-function)
8984 process (cdr procwin))))
8985 (when size
8986 (set-process-window-size process (cdr size) (car size))))))))))
8988 (add-hook 'window-configuration-change-hook 'window--adjust-process-windows)
8991 ;; Some of these are in tutorial--default-keys, so update that if you
8992 ;; change these.
8993 (define-key ctl-x-map "0" 'delete-window)
8994 (define-key ctl-x-map "1" 'delete-other-windows)
8995 (define-key ctl-x-map "2" 'split-window-below)
8996 (define-key ctl-x-map "3" 'split-window-right)
8997 (define-key ctl-x-map "o" 'other-window)
8998 (define-key ctl-x-map "^" 'enlarge-window)
8999 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
9000 (define-key ctl-x-map "{" 'shrink-window-horizontally)
9001 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
9002 (define-key ctl-x-map "+" 'balance-windows)
9003 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
9005 ;;; window.el ends here