Consistently check windows for validity/liveness (Bug#11984, Bug#12025, Bug#12026).
[emacs.git] / lisp / window.el
blobd7129df858515fbbc6fd6f631b5dc67f1ee70fa8
1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2012
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
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 (defmacro save-selected-window (&rest body)
32 "Execute BODY, then select the previously selected window.
33 The value returned is the value of the last form in BODY.
35 This macro saves and restores the selected window, as well as the
36 selected window in each frame. If the previously selected window
37 is no longer live, then whatever window is selected at the end of
38 BODY remains selected. If the previously selected window of some
39 frame is no longer live at the end of BODY, that frame's selected
40 window is left alone.
42 This macro saves and restores the current buffer, since otherwise
43 its normal operation could make a different buffer current. The
44 order of recently selected windows and the buffer list ordering
45 are not altered by this macro (unless they are altered in BODY)."
46 (declare (indent 0) (debug t))
47 `(let ((save-selected-window-window (selected-window))
48 ;; We save and restore all frames' selected windows, because
49 ;; `select-window' can change the frame-selected-window of
50 ;; whatever frame that window is in. Each text terminal's
51 ;; top-frame is preserved by putting it last in the list.
52 (save-selected-window-alist
53 (apply 'append
54 (mapcar (lambda (terminal)
55 (let ((frames (frames-on-display-list terminal))
56 (top-frame (tty-top-frame terminal))
57 alist)
58 (if top-frame
59 (setq frames
60 (cons top-frame
61 (delq top-frame frames))))
62 (dolist (f frames)
63 (push (cons f (frame-selected-window f))
64 alist))))
65 (terminal-list)))))
66 (save-current-buffer
67 (unwind-protect
68 (progn ,@body)
69 (dolist (elt save-selected-window-alist)
70 (and (frame-live-p (car elt))
71 (window-live-p (cdr elt))
72 (set-frame-selected-window (car elt) (cdr elt) 'norecord)))
73 (when (window-live-p save-selected-window-window)
74 (select-window save-selected-window-window 'norecord))))))
76 ;; The following two functions are like `window-next-sibling' and
77 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
78 ;; they don't substitute the selected window for nil), and they return
79 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
80 ;; a minibuffer window).
81 (defun window-right (window)
82 "Return WINDOW's right sibling.
83 Return nil if WINDOW is the root window of its frame. WINDOW can
84 be any window."
85 (and window (window-parent window) (window-next-sibling window)))
87 (defun window-left (window)
88 "Return WINDOW's left sibling.
89 Return nil if WINDOW is the root window of its frame. WINDOW can
90 be any window."
91 (and window (window-parent window) (window-prev-sibling window)))
93 (defun window-child (window)
94 "Return WINDOW's first child window."
95 (or (window-top-child window) (window-left-child window)))
97 (defun window-child-count (window)
98 "Return number of WINDOW's child windows."
99 (let ((count 0))
100 (when (and (windowp window) (setq window (window-child window)))
101 (while window
102 (setq count (1+ count))
103 (setq window (window-next-sibling window))))
104 count))
106 (defun window-last-child (window)
107 "Return last child window of WINDOW."
108 (when (and (windowp window) (setq window (window-child window)))
109 (while (window-next-sibling window)
110 (setq window (window-next-sibling window))))
111 window)
113 (defun window-normalize-buffer (buffer-or-name)
114 "Return buffer specified by BUFFER-OR-NAME.
115 BUFFER-OR-NAME must be either a buffer or a string naming a live
116 buffer and defaults to the current buffer."
117 (cond
118 ((not buffer-or-name)
119 (current-buffer))
120 ((bufferp buffer-or-name)
121 (if (buffer-live-p buffer-or-name)
122 buffer-or-name
123 (error "Buffer %s is not a live buffer" buffer-or-name)))
124 ((get-buffer buffer-or-name))
126 (error "No such buffer %s" buffer-or-name))))
128 (defun window-normalize-frame (frame)
129 "Return frame specified by FRAME.
130 FRAME must be a live frame and defaults to the selected frame."
131 (if frame
132 (if (frame-live-p frame)
133 frame
134 (error "%s is not a live frame" frame))
135 (selected-frame)))
137 (defun window-normalize-window (window &optional live-only)
138 "Return window specified by WINDOW.
139 If WINDOW is nil, return `selected-window'.
140 If WINDOW is a live window or internal window, return WINDOW;
141 if LIVE-ONLY is non-nil, return WINDOW for a live window only.
142 Otherwise, signal an error."
143 (cond ((null window)
144 (selected-window))
145 (live-only
146 (if (window-live-p window)
147 window
148 (error "%s is not a live window" window)))
149 ((if (window-valid-p window)
150 window
151 (error "%s is not a window" window)))))
153 (defvar ignore-window-parameters nil
154 "If non-nil, standard functions ignore window parameters.
155 The functions currently affected by this are `split-window',
156 `delete-window', `delete-other-windows' and `other-window'.
158 An application may bind this to a non-nil value around calls to
159 these functions to inhibit processing of window parameters.")
161 (defconst window-safe-min-height 1
162 "The absolute minimum number of lines of a window.
163 Anything less might crash Emacs.")
165 (defcustom window-min-height 4
166 "The minimum number of lines of any window.
167 The value has to accommodate a mode- or header-line if present.
168 A value less than `window-safe-min-height' is ignored. The value
169 of this variable is honored when windows are resized or split.
171 Applications should never rebind this variable. To resize a
172 window to a height less than the one specified here, an
173 application should instead call `window-resize' with a non-nil
174 IGNORE argument. In order to have `split-window' make a window
175 shorter, explicitly specify the SIZE argument of that function."
176 :type 'integer
177 :version "24.1"
178 :group 'windows)
180 (defconst window-safe-min-width 2
181 "The absolute minimum number of columns of a window.
182 Anything less might crash Emacs.")
184 (defcustom window-min-width 10
185 "The minimum number of columns of any window.
186 The value has to accommodate margins, fringes, or scrollbars if
187 present. A value less than `window-safe-min-width' is ignored.
188 The value of this variable is honored when windows are resized or
189 split.
191 Applications should never rebind this variable. To resize a
192 window to a width less than the one specified here, an
193 application should instead call `window-resize' with a non-nil
194 IGNORE argument. In order to have `split-window' make a window
195 narrower, explicitly specify the SIZE argument of that function."
196 :type 'integer
197 :version "24.1"
198 :group 'windows)
200 (defun window-combined-p (&optional window horizontal)
201 "Return non-nil if WINDOW has siblings in a given direction.
202 If WINDOW is omitted or nil, it defaults to the selected window.
204 HORIZONTAL determines a direction for the window combination.
205 If HORIZONTAL is omitted or nil, return non-nil if WINDOW is part
206 of a vertical window combination.
207 If HORIZONTAL is non-nil, return non-nil if WINDOW is part of a
208 horizontal window combination."
209 (setq window (window-normalize-window window))
210 (let ((parent (window-parent window)))
211 (and parent
212 (if horizontal
213 (window-left-child parent)
214 (window-top-child parent)))))
216 (defun window-combinations (window &optional horizontal)
217 "Return largest number of windows vertically arranged within WINDOW.
218 If WINDOW is omitted or nil, it defaults to the selected window.
219 If HORIZONTAL is non-nil, return the largest number of
220 windows horizontally arranged within WINDOW."
221 (setq window (window-normalize-window window))
222 (cond
223 ((window-live-p window)
224 ;; If WINDOW is live, return 1.
226 ((if horizontal
227 (window-left-child window)
228 (window-top-child window))
229 ;; If WINDOW is iso-combined, return the sum of the values for all
230 ;; child windows of WINDOW.
231 (let ((child (window-child window))
232 (count 0))
233 (while child
234 (setq count
235 (+ (window-combinations child horizontal)
236 count))
237 (setq child (window-right child)))
238 count))
240 ;; If WINDOW is not iso-combined, return the maximum value of any
241 ;; child window of WINDOW.
242 (let ((child (window-child window))
243 (count 1))
244 (while child
245 (setq count
246 (max (window-combinations child horizontal)
247 count))
248 (setq child (window-right child)))
249 count))))
251 (defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only)
252 "Helper function for `walk-window-tree' and `walk-window-subtree'."
253 (let (walk-window-tree-buffer)
254 (while walk-window-tree-window
255 (setq walk-window-tree-buffer
256 (window-buffer walk-window-tree-window))
257 (when (or walk-window-tree-buffer any)
258 (funcall fun walk-window-tree-window))
259 (unless walk-window-tree-buffer
260 (walk-window-tree-1
261 fun (window-left-child walk-window-tree-window) any)
262 (walk-window-tree-1
263 fun (window-top-child walk-window-tree-window) any))
264 (if sub-only
265 (setq walk-window-tree-window nil)
266 (setq walk-window-tree-window
267 (window-right walk-window-tree-window))))))
269 (defun walk-window-tree (fun &optional frame any)
270 "Run function FUN on each live window of FRAME.
271 FUN must be a function with one argument - a window. FRAME must
272 be a live frame and defaults to the selected one. ANY, if
273 non-nil means to run FUN on all live and internal windows of
274 FRAME.
276 This function performs a pre-order, depth-first traversal of the
277 window tree. If FUN changes the window tree, the result is
278 unpredictable."
279 (let ((walk-window-tree-frame (window-normalize-frame frame)))
280 (walk-window-tree-1
281 fun (frame-root-window walk-window-tree-frame) any)))
283 (defun walk-window-subtree (fun &optional window any)
284 "Run function FUN on the subtree of windows rooted at WINDOW.
285 WINDOW defaults to the selected window. FUN must be a function
286 with one argument - a window. By default, run FUN only on live
287 windows of the subtree. If the optional argument ANY is non-nil,
288 run FUN on all live and internal windows of the subtree. If
289 WINDOW is live, run FUN on WINDOW only.
291 This function performs a pre-order, depth-first traversal of the
292 subtree rooted at WINDOW. If FUN changes that tree, the result
293 is unpredictable."
294 (setq window (window-normalize-window window))
295 (walk-window-tree-1 fun window any t))
297 (defun window-with-parameter (parameter &optional value frame any)
298 "Return first window on FRAME with PARAMETER non-nil.
299 FRAME defaults to the selected frame. Optional argument VALUE
300 non-nil means only return a window whose window-parameter value
301 for PARAMETER equals VALUE (comparison is done with `equal').
302 Optional argument ANY non-nil means consider internal windows
303 too."
304 (let (this-value)
305 (catch 'found
306 (walk-window-tree
307 (lambda (window)
308 (when (and (setq this-value (window-parameter window parameter))
309 (or (not value) (equal value this-value)))
310 (throw 'found window)))
311 frame any))))
313 ;;; Atomic windows.
314 (defun window-atom-root (&optional window)
315 "Return root of atomic window WINDOW is a part of.
316 WINDOW can be any window and defaults to the selected one.
317 Return nil if WINDOW is not part of an atomic window."
318 (setq window (window-normalize-window window))
319 (let (root)
320 (while (and window (window-parameter window 'window-atom))
321 (setq root window)
322 (setq window (window-parent window)))
323 root))
325 (defun window-make-atom (window)
326 "Make WINDOW an atomic window.
327 WINDOW must be an internal window. Return WINDOW."
328 (if (not (window-child window))
329 (error "Window %s is not an internal window" window)
330 (walk-window-subtree
331 (lambda (window)
332 (set-window-parameter window 'window-atom t))
333 window t)
334 window))
336 (defun window--atom-check-1 (window)
337 "Subroutine of `window--atom-check'."
338 (when window
339 (if (window-parameter window 'window-atom)
340 (let ((count 0))
341 (when (or (catch 'reset
342 (walk-window-subtree
343 (lambda (window)
344 (if (window-parameter window 'window-atom)
345 (setq count (1+ count))
346 (throw 'reset t)))
347 window t))
348 ;; count >= 1 must hold here. If there's no other
349 ;; window around dissolve this atomic window.
350 (= count 1))
351 ;; Dissolve atomic window.
352 (walk-window-subtree
353 (lambda (window)
354 (set-window-parameter window 'window-atom nil))
355 window t)))
356 ;; Check children.
357 (unless (window-buffer window)
358 (window--atom-check-1 (window-left-child window))
359 (window--atom-check-1 (window-top-child window))))
360 ;; Check right sibling
361 (window--atom-check-1 (window-right window))))
363 (defun window--atom-check (&optional frame)
364 "Check atomicity of all windows on FRAME.
365 FRAME defaults to the selected frame. If an atomic window is
366 wrongly configured, reset the atomicity of all its windows on
367 FRAME to nil. An atomic window is wrongly configured if it has
368 no child windows or one of its child windows is not atomic."
369 (window--atom-check-1 (frame-root-window frame)))
371 ;; Side windows.
372 (defvar window-sides '(left top right bottom)
373 "Window sides.")
375 (defcustom window-sides-vertical nil
376 "If non-nil, left and right side windows are full height.
377 Otherwise, top and bottom side windows are full width."
378 :type 'boolean
379 :group 'windows
380 :version "24.1")
382 (defcustom window-sides-slots '(nil nil nil nil)
383 "Maximum number of side window slots.
384 The value is a list of four elements specifying the number of
385 side window slots on (in this order) the left, top, right and
386 bottom side of each frame. If an element is a number, this means
387 to display at most that many side windows on the corresponding
388 side. If an element is nil, this means there's no bound on the
389 number of slots on that side."
390 :version "24.1"
391 :risky t
392 :type
393 '(list
394 :value (nil nil nil nil)
395 (choice
396 :tag "Left"
397 :help-echo "Maximum slots of left side window."
398 :value nil
399 :format "%[Left%] %v\n"
400 (const :tag "Unlimited" :format "%t" nil)
401 (integer :tag "Number" :value 2 :size 5))
402 (choice
403 :tag "Top"
404 :help-echo "Maximum slots of top side window."
405 :value nil
406 :format "%[Top%] %v\n"
407 (const :tag "Unlimited" :format "%t" nil)
408 (integer :tag "Number" :value 3 :size 5))
409 (choice
410 :tag "Right"
411 :help-echo "Maximum slots of right side window."
412 :value nil
413 :format "%[Right%] %v\n"
414 (const :tag "Unlimited" :format "%t" nil)
415 (integer :tag "Number" :value 2 :size 5))
416 (choice
417 :tag "Bottom"
418 :help-echo "Maximum slots of bottom side window."
419 :value nil
420 :format "%[Bottom%] %v\n"
421 (const :tag "Unlimited" :format "%t" nil)
422 (integer :tag "Number" :value 3 :size 5)))
423 :group 'windows)
425 (defun window--side-check (&optional frame)
426 "Check the window-side parameter of all windows on FRAME.
427 FRAME defaults to the selected frame. If the configuration is
428 invalid, reset all window-side parameters to nil.
430 A valid configuration has to preserve the following invariant:
432 - If a window has a non-nil window-side parameter, it must have a
433 parent window and the parent window's window-side parameter
434 must be either nil or the same as for window.
436 - If windows with non-nil window-side parameters exist, there
437 must be at most one window of each side and non-side with a
438 parent whose window-side parameter is nil and there must be no
439 leaf window whose window-side parameter is nil."
440 (let (normal none left top right bottom
441 side parent parent-side)
442 (when (or (catch 'reset
443 (walk-window-tree
444 (lambda (window)
445 (setq side (window-parameter window 'window-side))
446 (setq parent (window-parent window))
447 (setq parent-side
448 (and parent (window-parameter parent 'window-side)))
449 ;; The following `cond' seems a bit tedious, but I'd
450 ;; rather stick to using just the stack.
451 (cond
452 (parent-side
453 (when (not (eq parent-side side))
454 ;; A parent whose window-side is non-nil must
455 ;; have a child with the same window-side.
456 (throw 'reset t)))
457 ;; Now check that there's more than one main window
458 ;; for any of none, left, top, right and bottom.
459 ((eq side 'none)
460 (if none
461 (throw 'reset t)
462 (setq none t)))
463 ((eq side 'left)
464 (if left
465 (throw 'reset t)
466 (setq left t)))
467 ((eq side 'top)
468 (if top
469 (throw 'reset t)
470 (setq top t)))
471 ((eq side 'right)
472 (if right
473 (throw 'reset t)
474 (setq right t)))
475 ((eq side 'bottom)
476 (if bottom
477 (throw 'reset t)
478 (setq bottom t)))
479 ((window-buffer window)
480 ;; A leaf window without window-side parameter,
481 ;; record its existence.
482 (setq normal t))))
483 frame t))
484 (if none
485 ;; At least one non-side window exists, so there must
486 ;; be at least one side-window and no normal window.
487 (or (not (or left top right bottom)) normal)
488 ;; No non-side window exists, so there must be no side
489 ;; window either.
490 (or left top right bottom)))
491 (walk-window-tree
492 (lambda (window)
493 (set-window-parameter window 'window-side nil))
494 frame t))))
496 (defun window--check (&optional frame)
497 "Check atomic and side windows on FRAME.
498 FRAME defaults to the selected frame."
499 (window--side-check frame)
500 (window--atom-check frame))
502 ;;; Window sizes.
503 (defvar window-size-fixed nil
504 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
505 If the value is `height', then only the window's height is fixed.
506 If the value is `width', then only the window's width is fixed.
507 Any other non-nil value fixes both the width and the height.
509 Emacs won't change the size of any window displaying that buffer,
510 unless it has no other choice (like when deleting a neighboring
511 window).")
512 (make-variable-buffer-local 'window-size-fixed)
514 (defun window--size-ignore (window ignore)
515 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
516 (if (window-valid-p ignore) (eq window ignore) ignore))
518 (defun window-min-size (&optional window horizontal ignore)
519 "Return the minimum size of WINDOW.
520 WINDOW can be an arbitrary window and defaults to the selected
521 one. Optional argument HORIZONTAL non-nil means return the
522 minimum number of columns of WINDOW; otherwise return the minimum
523 number of WINDOW's lines.
525 Optional argument IGNORE, if non-nil, means ignore restrictions
526 imposed by fixed size windows, `window-min-height' or
527 `window-min-width' settings. If IGNORE equals `safe', live
528 windows may get as small as `window-safe-min-height' lines and
529 `window-safe-min-width' columns. If IGNORE is a window, ignore
530 restrictions for that window only. Any other non-nil value
531 means ignore all of the above restrictions for all windows."
532 (window--min-size-1
533 (window-normalize-window window) horizontal ignore))
535 (defun window--min-size-1 (window horizontal ignore)
536 "Internal function of `window-min-size'."
537 (let ((sub (window-child window)))
538 (if sub
539 (let ((value 0))
540 ;; WINDOW is an internal window.
541 (if (window-combined-p sub horizontal)
542 ;; The minimum size of an iso-combination is the sum of
543 ;; the minimum sizes of its child windows.
544 (while sub
545 (setq value (+ value
546 (window--min-size-1 sub horizontal ignore)))
547 (setq sub (window-right sub)))
548 ;; The minimum size of an ortho-combination is the maximum of
549 ;; the minimum sizes of its child windows.
550 (while sub
551 (setq value (max value
552 (window--min-size-1 sub horizontal ignore)))
553 (setq sub (window-right sub))))
554 value)
555 (with-current-buffer (window-buffer window)
556 (cond
557 ((and (not (window--size-ignore window ignore))
558 (window-size-fixed-p window horizontal))
559 ;; The minimum size of a fixed size window is its size.
560 (window-total-size window horizontal))
561 ((or (eq ignore 'safe) (eq ignore window))
562 ;; If IGNORE equals `safe' or WINDOW return the safe values.
563 (if horizontal window-safe-min-width window-safe-min-height))
564 (horizontal
565 ;; For the minimum width of a window take fringes and
566 ;; scroll-bars into account. This is questionable and should
567 ;; be removed as soon as we are able to split (and resize)
568 ;; windows such that the new (or resized) windows can get a
569 ;; size less than the user-specified `window-min-height' and
570 ;; `window-min-width'.
571 (let ((frame (window-frame window))
572 (fringes (window-fringes window))
573 (scroll-bars (window-scroll-bars window)))
574 (max
575 (+ window-safe-min-width
576 (ceiling (car fringes) (frame-char-width frame))
577 (ceiling (cadr fringes) (frame-char-width frame))
578 (cond
579 ((memq (nth 2 scroll-bars) '(left right))
580 (nth 1 scroll-bars))
581 ((memq (frame-parameter frame 'vertical-scroll-bars)
582 '(left right))
583 (ceiling (or (frame-parameter frame 'scroll-bar-width) 14)
584 (frame-char-width)))
585 (t 0)))
586 (if (and (not (window--size-ignore window ignore))
587 (numberp window-min-width))
588 window-min-width
589 0))))
591 ;; For the minimum height of a window take any mode- or
592 ;; header-line into account.
593 (max (+ window-safe-min-height
594 (if header-line-format 1 0)
595 (if mode-line-format 1 0))
596 (if (and (not (window--size-ignore window ignore))
597 (numberp window-min-height))
598 window-min-height
599 0))))))))
601 (defun window-sizable (window delta &optional horizontal ignore)
602 "Return DELTA if DELTA lines can be added to WINDOW.
603 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
604 columns can be added to WINDOW. A return value of zero means
605 that no lines (or columns) can be added to WINDOW.
607 This function looks only at WINDOW and, recursively, its child
608 windows. The function `window-resizable' looks at other windows
609 as well.
611 DELTA positive means WINDOW shall be enlarged by DELTA lines or
612 columns. If WINDOW cannot be enlarged by DELTA lines or columns
613 return the maximum value in the range 0..DELTA by which WINDOW
614 can be enlarged.
616 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
617 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
618 return the minimum value in the range DELTA..0 by which WINDOW
619 can be shrunk.
621 Optional argument IGNORE non-nil means ignore restrictions
622 imposed by fixed size windows, `window-min-height' or
623 `window-min-width' settings. If IGNORE equals `safe', live
624 windows may get as small as `window-safe-min-height' lines and
625 `window-safe-min-width' columns. If IGNORE is a window, ignore
626 restrictions for that window only. Any other non-nil value means
627 ignore all of the above restrictions for all windows."
628 (setq window (window-normalize-window window))
629 (cond
630 ((< delta 0)
631 (max (- (window-min-size window horizontal ignore)
632 (window-total-size window horizontal))
633 delta))
634 ((window--size-ignore window ignore)
635 delta)
636 ((> delta 0)
637 (if (window-size-fixed-p window horizontal)
639 delta))
640 (t 0)))
642 (defun window-sizable-p (window delta &optional horizontal ignore)
643 "Return t if WINDOW can be resized by DELTA lines.
644 For the meaning of the arguments of this function see the
645 doc-string of `window-sizable'."
646 (setq window (window-normalize-window window))
647 (if (> delta 0)
648 (>= (window-sizable window delta horizontal ignore) delta)
649 (<= (window-sizable window delta horizontal ignore) delta)))
651 (defun window--size-fixed-1 (window horizontal)
652 "Internal function for `window-size-fixed-p'."
653 (let ((sub (window-child window)))
654 (catch 'fixed
655 (if sub
656 ;; WINDOW is an internal window.
657 (if (window-combined-p sub horizontal)
658 ;; An iso-combination is fixed size if all its child
659 ;; windows are fixed-size.
660 (progn
661 (while sub
662 (unless (window--size-fixed-1 sub horizontal)
663 ;; We found a non-fixed-size child window, so
664 ;; WINDOW's size is not fixed.
665 (throw 'fixed nil))
666 (setq sub (window-right sub)))
667 ;; All child windows are fixed-size, so WINDOW's size is
668 ;; fixed.
669 (throw 'fixed t))
670 ;; An ortho-combination is fixed-size if at least one of its
671 ;; child windows is fixed-size.
672 (while sub
673 (when (window--size-fixed-1 sub horizontal)
674 ;; We found a fixed-size child window, so WINDOW's size
675 ;; is fixed.
676 (throw 'fixed t))
677 (setq sub (window-right sub))))
678 ;; WINDOW is a live window.
679 (with-current-buffer (window-buffer window)
680 (if horizontal
681 (memq window-size-fixed '(width t))
682 (memq window-size-fixed '(height t))))))))
684 (defun window-size-fixed-p (&optional window horizontal)
685 "Return non-nil if WINDOW's height is fixed.
686 WINDOW can be an arbitrary window and defaults to the selected
687 window. Optional argument HORIZONTAL non-nil means return
688 non-nil if WINDOW's width is fixed.
690 If this function returns nil, this does not necessarily mean that
691 WINDOW can be resized in the desired direction. The function
692 `window-resizable' can tell that."
693 (window--size-fixed-1
694 (window-normalize-window window) horizontal))
696 (defun window--min-delta-1 (window delta &optional horizontal ignore trail noup)
697 "Internal function for `window-min-delta'."
698 (if (not (window-parent window))
699 ;; If we can't go up, return zero.
701 ;; Else try to find a non-fixed-size sibling of WINDOW.
702 (let* ((parent (window-parent window))
703 (sub (window-child parent)))
704 (catch 'done
705 (if (window-combined-p sub horizontal)
706 ;; In an iso-combination throw DELTA if we find at least one
707 ;; child window and that window is either not fixed-size or
708 ;; we can ignore fixed-sizeness.
709 (let ((skip (eq trail 'after)))
710 (while sub
711 (cond
712 ((eq sub window)
713 (setq skip (eq trail 'before)))
714 (skip)
715 ((and (not (window--size-ignore window ignore))
716 (window-size-fixed-p sub horizontal)))
718 ;; We found a non-fixed-size child window.
719 (throw 'done delta)))
720 (setq sub (window-right sub))))
721 ;; In an ortho-combination set DELTA to the minimum value by
722 ;; which other child windows can shrink.
723 (while sub
724 (unless (eq sub window)
725 (setq delta
726 (min delta
727 (- (window-total-size sub horizontal)
728 (window-min-size sub horizontal ignore)))))
729 (setq sub (window-right sub))))
730 (if noup
731 delta
732 (window--min-delta-1 parent delta horizontal ignore trail))))))
734 (defun window-min-delta (&optional window horizontal ignore trail noup nodown)
735 "Return number of lines by which WINDOW can be shrunk.
736 WINDOW can be an arbitrary window and defaults to the selected
737 window. Return zero if WINDOW cannot be shrunk.
739 Optional argument HORIZONTAL non-nil means return number of
740 columns by which WINDOW can be shrunk.
742 Optional argument IGNORE non-nil means ignore restrictions
743 imposed by fixed size windows, `window-min-height' or
744 `window-min-width' settings. If IGNORE is a window, ignore
745 restrictions for that window only. If IGNORE equals `safe',
746 live windows may get as small as `window-safe-min-height' lines
747 and `window-safe-min-width' columns. Any other non-nil value
748 means ignore all of the above restrictions for all windows.
750 Optional argument TRAIL restricts the windows that can be enlarged.
751 If its value is `before', only windows to the left of or above WINDOW
752 can be enlarged. If it is `after', only windows to the right of or
753 below WINDOW can be enlarged.
755 Optional argument NOUP non-nil means don't go up in the window
756 tree, but try to enlarge windows within WINDOW's combination only.
758 Optional argument NODOWN non-nil means don't check whether WINDOW
759 itself (and its child windows) can be shrunk; check only whether
760 at least one other window can be enlarged appropriately."
761 (setq window (window-normalize-window window))
762 (let ((size (window-total-size window horizontal))
763 (minimum (window-min-size window horizontal ignore)))
764 (cond
765 (nodown
766 ;; If NODOWN is t, try to recover the entire size of WINDOW.
767 (window--min-delta-1 window size horizontal ignore trail noup))
768 ((= size minimum)
769 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
770 ;; there's nothing to recover.
773 ;; Otherwise, try to recover whatever WINDOW is larger than its
774 ;; minimum size.
775 (window--min-delta-1
776 window (- size minimum) horizontal ignore trail noup)))))
778 (defun window--max-delta-1 (window delta &optional horizontal ignore trail noup)
779 "Internal function of `window-max-delta'."
780 (if (not (window-parent window))
781 ;; Can't go up. Return DELTA.
782 delta
783 (let* ((parent (window-parent window))
784 (sub (window-child parent)))
785 (catch 'fixed
786 (if (window-combined-p sub horizontal)
787 ;; For an iso-combination calculate how much we can get from
788 ;; other child windows.
789 (let ((skip (eq trail 'after)))
790 (while sub
791 (cond
792 ((eq sub window)
793 (setq skip (eq trail 'before)))
794 (skip)
796 (setq delta
797 (+ delta
798 (- (window-total-size sub horizontal)
799 (window-min-size sub horizontal ignore))))))
800 (setq sub (window-right sub))))
801 ;; For an ortho-combination throw DELTA when at least one
802 ;; child window is fixed-size.
803 (while sub
804 (when (and (not (eq sub window))
805 (not (window--size-ignore sub ignore))
806 (window-size-fixed-p sub horizontal))
807 (throw 'fixed delta))
808 (setq sub (window-right sub))))
809 (if noup
810 ;; When NOUP is nil, DELTA is all we can get.
811 delta
812 ;; Else try with parent of WINDOW, passing the DELTA we
813 ;; recovered so far.
814 (window--max-delta-1 parent delta horizontal ignore trail))))))
816 (defun window-max-delta (&optional window horizontal ignore trail noup nodown)
817 "Return maximum number of lines by which WINDOW can be enlarged.
818 WINDOW can be an arbitrary window and defaults to the selected
819 window. The return value is zero if WINDOW cannot be enlarged.
821 Optional argument HORIZONTAL non-nil means return maximum number
822 of columns by which WINDOW can be enlarged.
824 Optional argument IGNORE non-nil means ignore restrictions
825 imposed by fixed size windows, `window-min-height' or
826 `window-min-width' settings. If IGNORE is a window, ignore
827 restrictions for that window only. If IGNORE equals `safe',
828 live windows may get as small as `window-safe-min-height' lines
829 and `window-safe-min-width' columns. Any other non-nil value means
830 ignore all of the above restrictions for all windows.
832 Optional argument TRAIL restricts the windows that can be enlarged.
833 If its value is `before', only windows to the left of or above WINDOW
834 can be enlarged. If it is `after', only windows to the right of or
835 below WINDOW can be enlarged.
837 Optional argument NOUP non-nil means don't go up in the window
838 tree but try to obtain the entire space from windows within
839 WINDOW's combination.
841 Optional argument NODOWN non-nil means do not check whether
842 WINDOW itself (and its child windows) can be enlarged; check
843 only whether other windows can be shrunk appropriately."
844 (setq window (window-normalize-window window))
845 (if (and (not (window--size-ignore window ignore))
846 (not nodown) (window-size-fixed-p window horizontal))
847 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
848 ;; size.
850 ;; WINDOW has no fixed size.
851 (window--max-delta-1 window 0 horizontal ignore trail noup)))
853 ;; Make NOUP also inhibit the min-size check.
854 (defun window--resizable (window delta &optional horizontal ignore trail noup nodown)
855 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
856 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
857 can be resized horizontally by DELTA columns. A return value of
858 zero means that WINDOW is not resizable.
860 DELTA positive means WINDOW shall be enlarged by DELTA lines or
861 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
862 return the maximum value in the range 0..DELTA by which WINDOW
863 can be enlarged.
865 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
866 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
867 return the minimum value in the range DELTA..0 that can be used
868 for shrinking WINDOW.
870 Optional argument IGNORE non-nil means ignore restrictions
871 imposed by fixed size windows, `window-min-height' or
872 `window-min-width' settings. If IGNORE is a window, ignore
873 restrictions for that window only. If IGNORE equals `safe',
874 live windows may get as small as `window-safe-min-height' lines
875 and `window-safe-min-width' columns. Any other non-nil value
876 means ignore all of the above restrictions for all windows.
878 Optional argument TRAIL `before' means only windows to the left
879 of or below WINDOW can be shrunk. Optional argument TRAIL
880 `after' means only windows to the right of or above WINDOW can be
881 shrunk.
883 Optional argument NOUP non-nil means don't go up in the window
884 tree but check only whether space can be obtained from (or given
885 to) WINDOW's siblings.
887 Optional argument NODOWN non-nil means don't go down in the
888 window tree. This means do not check whether resizing would
889 violate size restrictions of WINDOW or its child windows."
890 (setq window (window-normalize-window window))
891 (cond
892 ((< delta 0)
893 (max (- (window-min-delta window horizontal ignore trail noup nodown))
894 delta))
895 ((> delta 0)
896 (min (window-max-delta window horizontal ignore trail noup nodown)
897 delta))
898 (t 0)))
900 (defun window--resizable-p (window delta &optional horizontal ignore trail noup nodown)
901 "Return t if WINDOW can be resized vertically by DELTA lines.
902 For the meaning of the arguments of this function see the
903 doc-string of `window--resizable'."
904 (setq window (window-normalize-window window))
905 (if (> delta 0)
906 (>= (window--resizable window delta horizontal ignore trail noup nodown)
907 delta)
908 (<= (window--resizable window delta horizontal ignore trail noup nodown)
909 delta)))
911 (defun window-resizable (window delta &optional horizontal ignore)
912 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
913 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
914 can be resized horizontally by DELTA columns. A return value of
915 zero means that WINDOW is not resizable.
917 DELTA positive means WINDOW shall be enlarged by DELTA lines or
918 columns. If WINDOW cannot be enlarged by DELTA lines or columns
919 return the maximum value in the range 0..DELTA by which WINDOW
920 can be enlarged.
922 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
923 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
924 return the minimum value in the range DELTA..0 that can be used
925 for shrinking WINDOW.
927 Optional argument IGNORE non-nil means ignore restrictions
928 imposed by fixed size windows, `window-min-height' or
929 `window-min-width' settings. If IGNORE is a window, ignore
930 restrictions for that window only. If IGNORE equals `safe',
931 live windows may get as small as `window-safe-min-height' lines
932 and `window-safe-min-width' columns. Any other non-nil value
933 means ignore all of the above restrictions for all windows."
934 (setq window (window-normalize-window window))
935 (window--resizable window delta horizontal ignore))
937 (defun window-total-size (&optional window horizontal)
938 "Return the total height or width of WINDOW.
939 If WINDOW is omitted or nil, it defaults to the selected window.
941 If HORIZONTAL is omitted or nil, return the total height of
942 WINDOW, in lines, like `window-total-height'. Otherwise return
943 the total width, in columns, like `window-total-width'."
944 (if horizontal
945 (window-total-width window)
946 (window-total-height window)))
948 ;; Eventually we should make `window-height' obsolete.
949 (defalias 'window-height 'window-total-height)
951 ;; See discussion in bug#4543.
952 (defun window-full-height-p (&optional window)
953 "Return t if WINDOW is as high as its containing frame.
954 More precisely, return t if and only if the total height of
955 WINDOW equals the total height of the root window of WINDOW's
956 frame. WINDOW can be any window and defaults to the selected
957 one."
958 (setq window (window-normalize-window window))
959 (= (window-total-size window)
960 (window-total-size (frame-root-window window))))
962 (defun window-full-width-p (&optional window)
963 "Return t if WINDOW is as wide as its containing frame.
964 More precisely, return t if and only if the total width of WINDOW
965 equals the total width of the root window of WINDOW's frame.
966 WINDOW can be any window and defaults to the selected one."
967 (setq window (window-normalize-window window))
968 (= (window-total-size window t)
969 (window-total-size (frame-root-window window) t)))
971 (defun window-body-size (&optional window horizontal)
972 "Return the height or width of WINDOW's text area.
973 If WINDOW is omitted or nil, it defaults to the selected window.
974 Signal an error if the window is not live.
976 If HORIZONTAL is omitted or nil, return the height of the text
977 area, like `window-body-height'. Otherwise, return the width of
978 the text area, like `window-body-width'."
979 (if horizontal
980 (window-body-width window)
981 (window-body-height window)))
983 ;; Eventually we should make `window-height' obsolete.
984 (defalias 'window-width 'window-body-width)
986 (defun window-current-scroll-bars (&optional window)
987 "Return the current scroll bar settings for WINDOW.
988 WINDOW must be a live window and defaults to the selected one.
990 The return value is a cons cell (VERTICAL . HORIZONTAL) where
991 VERTICAL specifies the current location of the vertical scroll
992 bars (`left', `right', or nil), and HORIZONTAL specifies the
993 current location of the horizontal scroll bars (`top', `bottom',
994 or nil).
996 Unlike `window-scroll-bars', this function reports the scroll bar
997 type actually used, once frame defaults and `scroll-bar-mode' are
998 taken into account."
999 (setq window (window-normalize-window window t))
1000 (let ((vert (nth 2 (window-scroll-bars window)))
1001 (hor nil))
1002 (when (or (eq vert t) (eq hor t))
1003 (let ((fcsb (frame-current-scroll-bars (window-frame window))))
1004 (if (eq vert t)
1005 (setq vert (car fcsb)))
1006 (if (eq hor t)
1007 (setq hor (cdr fcsb)))))
1008 (cons vert hor)))
1010 (defun walk-windows (fun &optional minibuf all-frames)
1011 "Cycle through all live windows, calling FUN for each one.
1012 FUN must specify a function with a window as its sole argument.
1013 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1014 windows to include in the walk.
1016 MINIBUF t means include the minibuffer window even if the
1017 minibuffer is not active. MINIBUF nil or omitted means include
1018 the minibuffer window only if the minibuffer is active. Any
1019 other value means do not include the minibuffer window even if
1020 the minibuffer is active.
1022 ALL-FRAMES nil or omitted means consider all windows on the
1023 selected frame, plus the minibuffer window if specified by the
1024 MINIBUF argument. If the minibuffer counts, consider all windows
1025 on all frames that share that minibuffer too. The following
1026 non-nil values of ALL-FRAMES have special meanings:
1028 - t means consider all windows on all existing frames.
1030 - `visible' means consider all windows on all visible frames on
1031 the current terminal.
1033 - 0 (the number zero) means consider all windows on all visible
1034 and iconified frames on the current terminal.
1036 - A frame means consider all windows on that frame only.
1038 Anything else means consider all windows on the selected frame
1039 and no others.
1041 This function changes neither the order of recently selected
1042 windows nor the buffer list."
1043 ;; If we start from the minibuffer window, don't fail to come
1044 ;; back to it.
1045 (when (window-minibuffer-p (selected-window))
1046 (setq minibuf t))
1047 ;; Make sure to not mess up the order of recently selected
1048 ;; windows. Use `save-selected-window' and `select-window'
1049 ;; with second argument non-nil for this purpose.
1050 (save-selected-window
1051 (when (framep all-frames)
1052 (select-window (frame-first-window all-frames) 'norecord))
1053 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
1054 (funcall fun walk-windows-window))))
1056 (defun window-point-1 (&optional window)
1057 "Return value of WINDOW's point.
1058 WINDOW can be any live window and defaults to the selected one.
1060 This function is like `window-point' with one exception: If
1061 WINDOW is selected, it returns the value of `point' of WINDOW's
1062 buffer regardless of whether that buffer is current or not."
1063 (setq window (window-normalize-window window t))
1064 (if (eq window (selected-window))
1065 (with-current-buffer (window-buffer window)
1066 (point))
1067 (window-point window)))
1069 (defun set-window-point-1 (window pos)
1070 "Set value of WINDOW's point to POS.
1071 WINDOW can be any live window and defaults to the selected one.
1073 This function is like `set-window-point' with one exception: If
1074 WINDOW is selected, it moves `point' of WINDOW's buffer to POS
1075 regardless of whether that buffer is current or not."
1076 (setq window (window-normalize-window window t))
1077 (if (eq window (selected-window))
1078 (with-current-buffer (window-buffer window)
1079 (goto-char pos))
1080 (set-window-point window pos)))
1082 (defun window-at-side-p (&optional window side)
1083 "Return t if WINDOW is at SIDE of its containing frame.
1084 WINDOW can be any window and defaults to the selected one. SIDE
1085 can be any of the symbols `left', `top', `right' or `bottom'.
1086 The default value nil is handled like `bottom'."
1087 (setq window (window-normalize-window window))
1088 (let ((edge
1089 (cond
1090 ((eq side 'left) 0)
1091 ((eq side 'top) 1)
1092 ((eq side 'right) 2)
1093 ((memq side '(bottom nil)) 3))))
1094 (= (nth edge (window-edges window))
1095 (nth edge (window-edges (frame-root-window window))))))
1097 (defun window-at-side-list (&optional frame side)
1098 "Return list of all windows on SIDE of FRAME.
1099 FRAME must be a live frame and defaults to the selected frame.
1100 SIDE can be any of the symbols `left', `top', `right' or
1101 `bottom'. The default value nil is handled like `bottom'."
1102 (setq frame (window-normalize-frame frame))
1103 (let (windows)
1104 (walk-window-tree
1105 (lambda (window)
1106 (when (window-at-side-p window side)
1107 (setq windows (cons window windows))))
1108 frame)
1109 (nreverse windows)))
1111 (defun window--in-direction-2 (window posn &optional horizontal)
1112 "Support function for `window-in-direction'."
1113 (if horizontal
1114 (let ((top (window-top-line window)))
1115 (if (> top posn)
1116 (- top posn)
1117 (- posn top (window-total-height window))))
1118 (let ((left (window-left-column window)))
1119 (if (> left posn)
1120 (- left posn)
1121 (- posn left (window-total-width window))))))
1123 (defun window-in-direction (direction &optional window ignore)
1124 "Return window in DIRECTION as seen from WINDOW.
1125 DIRECTION must be one of `above', `below', `left' or `right'.
1126 WINDOW must be a live window and defaults to the selected one.
1127 IGNORE non-nil means a window can be returned even if its
1128 `no-other-window' parameter is non-nil."
1129 (setq window (window-normalize-window window t))
1130 (unless (memq direction '(above below left right))
1131 (error "Wrong direction %s" direction))
1132 (let* ((frame (window-frame window))
1133 (hor (memq direction '(left right)))
1134 (first (if hor
1135 (window-left-column window)
1136 (window-top-line window)))
1137 (last (+ first (if hor
1138 (window-total-width window)
1139 (window-total-height window))))
1140 (posn-cons (nth 6 (posn-at-point (window-point-1 window) window)))
1141 ;; The column / row value of `posn-at-point' can be nil for the
1142 ;; mini-window, guard against that.
1143 (posn (if hor
1144 (+ (or (cdr posn-cons) 1) (window-top-line window))
1145 (+ (or (car posn-cons) 1) (window-left-column window))))
1146 (best-edge
1147 (cond
1148 ((eq direction 'below) (frame-height frame))
1149 ((eq direction 'right) (frame-width frame))
1150 (t -1)))
1151 (best-edge-2 best-edge)
1152 (best-diff-2 (if hor (frame-height frame) (frame-width frame)))
1153 best best-2 best-diff-2-new)
1154 (walk-window-tree
1155 (lambda (w)
1156 (let* ((w-top (window-top-line w))
1157 (w-left (window-left-column w)))
1158 (cond
1159 ((or (eq window w)
1160 ;; Ignore ourselves.
1161 (and (window-parameter w 'no-other-window)
1162 ;; Ignore W unless IGNORE is non-nil.
1163 (not ignore))))
1164 (hor
1165 (cond
1166 ((and (<= w-top posn)
1167 (< posn (+ w-top (window-total-height w))))
1168 ;; W is to the left or right of WINDOW and covers POSN.
1169 (when (or (and (eq direction 'left)
1170 (<= w-left first) (> w-left best-edge))
1171 (and (eq direction 'right)
1172 (>= w-left last) (< w-left best-edge)))
1173 (setq best-edge w-left)
1174 (setq best w)))
1175 ((and (or (and (eq direction 'left)
1176 (<= (+ w-left (window-total-width w)) first))
1177 (and (eq direction 'right) (<= last w-left)))
1178 ;; W is to the left or right of WINDOW but does not
1179 ;; cover POSN.
1180 (setq best-diff-2-new
1181 (window--in-direction-2 w posn hor))
1182 (or (< best-diff-2-new best-diff-2)
1183 (and (= best-diff-2-new best-diff-2)
1184 (if (eq direction 'left)
1185 (> w-left best-edge-2)
1186 (< w-left best-edge-2)))))
1187 (setq best-edge-2 w-left)
1188 (setq best-diff-2 best-diff-2-new)
1189 (setq best-2 w))))
1191 (cond
1192 ((and (<= w-left posn)
1193 (< posn (+ w-left (window-total-width w))))
1194 ;; W is above or below WINDOW and covers POSN.
1195 (when (or (and (eq direction 'above)
1196 (<= w-top first) (> w-top best-edge))
1197 (and (eq direction 'below)
1198 (>= w-top first) (< w-top best-edge)))
1199 (setq best-edge w-top)
1200 (setq best w)))
1201 ((and (or (and (eq direction 'above)
1202 (<= (+ w-top (window-total-height w)) first))
1203 (and (eq direction 'below) (<= last w-top)))
1204 ;; W is above or below WINDOW but does not cover POSN.
1205 (setq best-diff-2-new
1206 (window--in-direction-2 w posn hor))
1207 (or (< best-diff-2-new best-diff-2)
1208 (and (= best-diff-2-new best-diff-2)
1209 (if (eq direction 'above)
1210 (> w-top best-edge-2)
1211 (< w-top best-edge-2)))))
1212 (setq best-edge-2 w-top)
1213 (setq best-diff-2 best-diff-2-new)
1214 (setq best-2 w)))))))
1215 (window-frame window))
1216 (or best best-2)))
1218 (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
1219 "Return a live window satisfying PREDICATE.
1220 More precisely, cycle through all windows calling the function
1221 PREDICATE on each one of them with the window as its sole
1222 argument. Return the first window for which PREDICATE returns
1223 non-nil. Windows are scanned starting with the window following
1224 the selected window. If no window satisfies PREDICATE, return
1225 DEFAULT.
1227 MINIBUF t means include the minibuffer window even if the
1228 minibuffer is not active. MINIBUF nil or omitted means include
1229 the minibuffer window only if the minibuffer is active. Any
1230 other value means do not include the minibuffer window even if
1231 the minibuffer is active.
1233 ALL-FRAMES nil or omitted means consider all windows on the selected
1234 frame, plus the minibuffer window if specified by the MINIBUF
1235 argument. If the minibuffer counts, consider all windows on all
1236 frames that share that minibuffer too. The following non-nil
1237 values of ALL-FRAMES have special meanings:
1239 - t means consider all windows on all existing frames.
1241 - `visible' means consider all windows on all visible frames on
1242 the current terminal.
1244 - 0 (the number zero) means consider all windows on all visible
1245 and iconified frames on the current terminal.
1247 - A frame means consider all windows on that frame only.
1249 Anything else means consider all windows on the selected frame
1250 and no others."
1251 (catch 'found
1252 (dolist (window (window-list-1
1253 (next-window nil minibuf all-frames)
1254 minibuf all-frames))
1255 (when (funcall predicate window)
1256 (throw 'found window)))
1257 default))
1259 (defalias 'some-window 'get-window-with-predicate)
1261 (defun get-lru-window (&optional all-frames dedicated not-selected)
1262 "Return the least recently used window on frames specified by ALL-FRAMES.
1263 Return a full-width window if possible. A minibuffer window is
1264 never a candidate. A dedicated window is never a candidate
1265 unless DEDICATED is non-nil, so if all windows are dedicated, the
1266 value is nil. Avoid returning the selected window if possible.
1267 Optional argument NOT-SELECTED non-nil means never return the
1268 selected window.
1270 The following non-nil values of the optional argument ALL-FRAMES
1271 have special meanings:
1273 - t means consider all windows on all existing frames.
1275 - `visible' means consider all windows on all visible frames on
1276 the current terminal.
1278 - 0 (the number zero) means consider all windows on all visible
1279 and iconified frames on the current terminal.
1281 - A frame means consider all windows on that frame only.
1283 Any other value of ALL-FRAMES means consider all windows on the
1284 selected frame and no others."
1285 (let (best-window best-time second-best-window second-best-time time)
1286 (dolist (window (window-list-1 nil 'nomini all-frames))
1287 (when (and (or dedicated (not (window-dedicated-p window)))
1288 (or (not not-selected) (not (eq window (selected-window)))))
1289 (setq time (window-use-time window))
1290 (if (or (eq window (selected-window))
1291 (not (window-full-width-p window)))
1292 (when (or (not second-best-time) (< time second-best-time))
1293 (setq second-best-time time)
1294 (setq second-best-window window))
1295 (when (or (not best-time) (< time best-time))
1296 (setq best-time time)
1297 (setq best-window window)))))
1298 (or best-window second-best-window)))
1300 (defun get-mru-window (&optional all-frames dedicated not-selected)
1301 "Return the most recently used window on frames specified by ALL-FRAMES.
1302 A minibuffer window is never a candidate. A dedicated window is
1303 never a candidate unless DEDICATED is non-nil, so if all windows
1304 are dedicated, the value is nil. Optional argument NOT-SELECTED
1305 non-nil means never return the selected window.
1307 The following non-nil values of the optional argument ALL-FRAMES
1308 have special meanings:
1310 - t means consider all windows on all existing frames.
1312 - `visible' means consider all windows on all visible frames on
1313 the current terminal.
1315 - 0 (the number zero) means consider all windows on all visible
1316 and iconified frames on the current terminal.
1318 - A frame means consider all windows on that frame only.
1320 Any other value of ALL-FRAMES means consider all windows on the
1321 selected frame and no others."
1322 (let (best-window best-time time)
1323 (dolist (window (window-list-1 nil 'nomini all-frames))
1324 (setq time (window-use-time window))
1325 (when (and (or dedicated (not (window-dedicated-p window)))
1326 (or (not not-selected) (not (eq window (selected-window))))
1327 (or (not best-time) (> time best-time)))
1328 (setq best-time time)
1329 (setq best-window window)))
1330 best-window))
1332 (defun get-largest-window (&optional all-frames dedicated not-selected)
1333 "Return the largest window on frames specified by ALL-FRAMES.
1334 A minibuffer window is never a candidate. A dedicated window is
1335 never a candidate unless DEDICATED is non-nil, so if all windows
1336 are dedicated, the value is nil. Optional argument NOT-SELECTED
1337 non-nil means never return the selected window.
1339 The following non-nil values of the optional argument ALL-FRAMES
1340 have special meanings:
1342 - t means consider all windows on all existing frames.
1344 - `visible' means consider all windows on all visible frames on
1345 the current terminal.
1347 - 0 (the number zero) means consider all windows on all visible
1348 and iconified frames on the current terminal.
1350 - A frame means consider all windows on that frame only.
1352 Any other value of ALL-FRAMES means consider all windows on the
1353 selected frame and no others."
1354 (let ((best-size 0)
1355 best-window size)
1356 (dolist (window (window-list-1 nil 'nomini all-frames))
1357 (when (and (or dedicated (not (window-dedicated-p window)))
1358 (or (not not-selected) (not (eq window (selected-window)))))
1359 (setq size (* (window-total-size window)
1360 (window-total-size window t)))
1361 (when (> size best-size)
1362 (setq best-size size)
1363 (setq best-window window))))
1364 best-window))
1366 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
1367 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
1368 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
1369 and defaults to the current buffer. Windows are scanned starting
1370 with the selected window.
1372 MINIBUF t means include the minibuffer window even if the
1373 minibuffer is not active. MINIBUF nil or omitted means include
1374 the minibuffer window only if the minibuffer is active. Any
1375 other value means do not include the minibuffer window even if
1376 the minibuffer is active.
1378 ALL-FRAMES nil or omitted means consider all windows on the
1379 selected frame, plus the minibuffer window if specified by the
1380 MINIBUF argument. If the minibuffer counts, consider all windows
1381 on all frames that share that minibuffer too. The following
1382 non-nil values of ALL-FRAMES have special meanings:
1384 - t means consider all windows on all existing frames.
1386 - `visible' means consider all windows on all visible frames on
1387 the current terminal.
1389 - 0 (the number zero) means consider all windows on all visible
1390 and iconified frames on the current terminal.
1392 - A frame means consider all windows on that frame only.
1394 Anything else means consider all windows on the selected frame
1395 and no others."
1396 (let ((buffer (window-normalize-buffer buffer-or-name))
1397 windows)
1398 (dolist (window (window-list-1 (selected-window) minibuf all-frames))
1399 (when (eq (window-buffer window) buffer)
1400 (setq windows (cons window windows))))
1401 (nreverse windows)))
1403 (defun minibuffer-window-active-p (window)
1404 "Return t if WINDOW is the currently active minibuffer window."
1405 (eq window (active-minibuffer-window)))
1407 (defun count-windows (&optional minibuf)
1408 "Return the number of live windows on the selected frame.
1409 The optional argument MINIBUF specifies whether the minibuffer
1410 window shall be counted. See `walk-windows' for the precise
1411 meaning of this argument."
1412 (length (window-list-1 nil minibuf)))
1414 ;;; Resizing windows.
1415 (defun window--resize-reset (&optional frame horizontal)
1416 "Reset resize values for all windows on FRAME.
1417 FRAME defaults to the selected frame.
1419 This function stores the current value of `window-total-size' applied
1420 with argument HORIZONTAL in the new total size of all windows on
1421 FRAME. It also resets the new normal size of each of these
1422 windows."
1423 (window--resize-reset-1
1424 (frame-root-window (window-normalize-frame frame)) horizontal))
1426 (defun window--resize-reset-1 (window horizontal)
1427 "Internal function of `window--resize-reset'."
1428 ;; Register old size in the new total size.
1429 (set-window-new-total window (window-total-size window horizontal))
1430 ;; Reset new normal size.
1431 (set-window-new-normal window)
1432 (when (window-child window)
1433 (window--resize-reset-1 (window-child window) horizontal))
1434 (when (window-right window)
1435 (window--resize-reset-1 (window-right window) horizontal)))
1437 ;; The following routine is used to manually resize the minibuffer
1438 ;; window and is currently used, for example, by ispell.el.
1439 (defun window--resize-mini-window (window delta)
1440 "Resize minibuffer window WINDOW by DELTA lines.
1441 If WINDOW cannot be resized by DELTA lines make it as large (or
1442 as small) as possible, but don't signal an error."
1443 (when (window-minibuffer-p window)
1444 (let* ((frame (window-frame window))
1445 (root (frame-root-window frame))
1446 (height (window-total-size window))
1447 (min-delta
1448 (- (window-total-size root)
1449 (window-min-size root))))
1450 ;; Sanitize DELTA.
1451 (cond
1452 ((<= (+ height delta) 0)
1453 (setq delta (- (- height 1))))
1454 ((> delta min-delta)
1455 (setq delta min-delta)))
1457 ;; Resize now.
1458 (window--resize-reset frame)
1459 ;; Ideally we should be able to resize just the last child of root
1460 ;; here. See the comment in `resize-root-window-vertically' for
1461 ;; why we do not do that.
1462 (window--resize-this-window root (- delta) nil nil t)
1463 (set-window-new-total window (+ height delta))
1464 ;; The following routine catches the case where we want to resize
1465 ;; a minibuffer-only frame.
1466 (resize-mini-window-internal window))))
1468 (defun window-resize (window delta &optional horizontal ignore)
1469 "Resize WINDOW vertically by DELTA lines.
1470 WINDOW can be an arbitrary window and defaults to the selected
1471 one. An attempt to resize the root window of a frame will raise
1472 an error though.
1474 DELTA a positive number means WINDOW shall be enlarged by DELTA
1475 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
1476 lines.
1478 Optional argument HORIZONTAL non-nil means resize WINDOW
1479 horizontally by DELTA columns. In this case a positive DELTA
1480 means enlarge WINDOW by DELTA columns. DELTA negative means
1481 WINDOW shall be shrunk by -DELTA columns.
1483 Optional argument IGNORE non-nil means ignore restrictions
1484 imposed by fixed size windows, `window-min-height' or
1485 `window-min-width' settings. If IGNORE is a window, ignore
1486 restrictions for that window only. If IGNORE equals `safe',
1487 live windows may get as small as `window-safe-min-height' lines
1488 and `window-safe-min-width' columns. Any other non-nil value
1489 means ignore all of the above restrictions for all windows.
1491 This function resizes other windows proportionally and never
1492 deletes any windows. If you want to move only the low (right)
1493 edge of WINDOW consider using `adjust-window-trailing-edge'
1494 instead."
1495 (setq window (window-normalize-window window))
1496 (let* ((frame (window-frame window))
1497 (minibuffer-window (minibuffer-window frame))
1498 sibling)
1499 (cond
1500 ((eq window (frame-root-window frame))
1501 (error "Cannot resize the root window of a frame"))
1502 ((window-minibuffer-p window)
1503 (if horizontal
1504 (error "Cannot resize minibuffer window horizontally")
1505 (window--resize-mini-window window delta)))
1506 ((and (not horizontal)
1507 (window-full-height-p window)
1508 (eq (window-frame minibuffer-window) frame)
1509 (or (not resize-mini-windows)
1510 (eq minibuffer-window (active-minibuffer-window))))
1511 ;; If WINDOW is full height and either `resize-mini-windows' is
1512 ;; nil or the minibuffer window is active, resize the minibuffer
1513 ;; window.
1514 (window--resize-mini-window minibuffer-window (- delta)))
1515 ((window--resizable-p window delta horizontal ignore)
1516 (window--resize-reset frame horizontal)
1517 (window--resize-this-window window delta horizontal ignore t)
1518 (if (and (not window-combination-resize)
1519 (window-combined-p window horizontal)
1520 (setq sibling (or (window-right window) (window-left window)))
1521 (window-sizable-p sibling (- delta) horizontal ignore))
1522 ;; If window-combination-resize is nil, WINDOW is part of an
1523 ;; iso-combination, and WINDOW's neighboring right or left
1524 ;; sibling can be resized as requested, resize that sibling.
1525 (let ((normal-delta
1526 (/ (float delta)
1527 (window-total-size (window-parent window) horizontal))))
1528 (window--resize-this-window sibling (- delta) horizontal nil t)
1529 (set-window-new-normal
1530 window (+ (window-normal-size window horizontal)
1531 normal-delta))
1532 (set-window-new-normal
1533 sibling (- (window-normal-size sibling horizontal)
1534 normal-delta)))
1535 ;; Otherwise, resize all other windows in the same combination.
1536 (window--resize-siblings window delta horizontal ignore))
1537 (window-resize-apply frame horizontal))
1539 (error "Cannot resize window %s" window)))))
1541 (defun window--resize-child-windows-skip-p (window)
1542 "Return non-nil if WINDOW shall be skipped by resizing routines."
1543 (memq (window-new-normal window) '(ignore stuck skip)))
1545 (defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
1546 "Recursively set new normal height of child windows of window PARENT.
1547 HORIZONTAL non-nil means set the new normal width of these
1548 windows. WINDOW specifies a child window of PARENT that has been
1549 resized by THIS-DELTA lines (columns).
1551 Optional argument TRAIL either `before' or `after' means set values
1552 only for windows before or after WINDOW. Optional argument
1553 OTHER-DELTA, a number, specifies that this many lines (columns)
1554 have been obtained from (or returned to) an ancestor window of
1555 PARENT in order to resize WINDOW."
1556 (let* ((delta-normal
1557 (if (and (= (- this-delta) (window-total-size window horizontal))
1558 (zerop other-delta))
1559 ;; When WINDOW gets deleted and we can return its entire
1560 ;; space to its siblings, use WINDOW's normal size as the
1561 ;; normal delta.
1562 (- (window-normal-size window horizontal))
1563 ;; In any other case calculate the normal delta from the
1564 ;; relation of THIS-DELTA to the total size of PARENT.
1565 (/ (float this-delta) (window-total-size parent horizontal))))
1566 (sub (window-child parent))
1567 (parent-normal 0.0)
1568 (skip (eq trail 'after)))
1570 ;; Set parent-normal to the sum of the normal sizes of all child
1571 ;; windows of PARENT that shall be resized, excluding only WINDOW
1572 ;; and any windows specified by the optional TRAIL argument.
1573 (while sub
1574 (cond
1575 ((eq sub window)
1576 (setq skip (eq trail 'before)))
1577 (skip)
1579 (setq parent-normal
1580 (+ parent-normal (window-normal-size sub horizontal)))))
1581 (setq sub (window-right sub)))
1583 ;; Set the new normal size of all child windows of PARENT from what
1584 ;; they should have contributed for recovering THIS-DELTA lines
1585 ;; (columns).
1586 (setq sub (window-child parent))
1587 (setq skip (eq trail 'after))
1588 (while sub
1589 (cond
1590 ((eq sub window)
1591 (setq skip (eq trail 'before)))
1592 (skip)
1594 (let ((old-normal (window-normal-size sub horizontal)))
1595 (set-window-new-normal
1596 sub (min 1.0 ; Don't get larger than 1.
1597 (max (- old-normal
1598 (* (/ old-normal parent-normal)
1599 delta-normal))
1600 ;; Don't drop below 0.
1601 0.0))))))
1602 (setq sub (window-right sub)))
1604 (when (numberp other-delta)
1605 ;; Set the new normal size of windows from what they should have
1606 ;; contributed for recovering OTHER-DELTA lines (columns).
1607 (setq delta-normal (/ (float (window-total-size parent horizontal))
1608 (+ (window-total-size parent horizontal)
1609 other-delta)))
1610 (setq sub (window-child parent))
1611 (setq skip (eq trail 'after))
1612 (while sub
1613 (cond
1614 ((eq sub window)
1615 (setq skip (eq trail 'before)))
1616 (skip)
1618 (set-window-new-normal
1619 sub (min 1.0 ; Don't get larger than 1.
1620 (max (* (window-new-normal sub) delta-normal)
1621 ;; Don't drop below 0.
1622 0.0)))))
1623 (setq sub (window-right sub))))
1625 ;; Set the new normal size of WINDOW to what is left by the sum of
1626 ;; the normal sizes of its siblings.
1627 (set-window-new-normal
1628 window
1629 (let ((sum 0))
1630 (setq sub (window-child parent))
1631 (while sub
1632 (cond
1633 ((eq sub window))
1634 ((not (numberp (window-new-normal sub)))
1635 (setq sum (+ sum (window-normal-size sub horizontal))))
1637 (setq sum (+ sum (window-new-normal sub)))))
1638 (setq sub (window-right sub)))
1639 ;; Don't get larger than 1 or smaller than 0.
1640 (min 1.0 (max (- 1.0 sum) 0.0))))))
1642 (defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge)
1643 "Resize child windows of window PARENT vertically by DELTA lines.
1644 PARENT must be a vertically combined internal window.
1646 Optional argument HORIZONTAL non-nil means resize child windows of
1647 PARENT horizontally by DELTA columns. In this case PARENT must
1648 be a horizontally combined internal window.
1650 WINDOW, if specified, must denote a child window of PARENT that
1651 is resized by DELTA lines.
1653 Optional argument IGNORE non-nil means ignore restrictions
1654 imposed by fixed size windows, `window-min-height' or
1655 `window-min-width' settings. If IGNORE equals `safe', live
1656 windows may get as small as `window-safe-min-height' lines and
1657 `window-safe-min-width' columns. If IGNORE is a window, ignore
1658 restrictions for that window only. Any other non-nil value means
1659 ignore all of the above restrictions for all windows.
1661 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
1662 of windows that shall be resized. If TRAIL equals `before',
1663 resize only windows on the left or above EDGE. If TRAIL equals
1664 `after', resize only windows on the right or below EDGE. Also,
1665 preferably only resize windows adjacent to EDGE.
1667 Return the symbol `normalized' if new normal sizes have been
1668 already set by this routine."
1669 (let* ((first (window-child parent))
1670 (sub first)
1671 (parent-total (+ (window-total-size parent horizontal) delta))
1672 best-window best-value)
1674 (if (and edge (memq trail '(before after))
1675 (progn
1676 (setq sub first)
1677 (while (and (window-right sub)
1678 (or (and (eq trail 'before)
1679 (not (window--resize-child-windows-skip-p
1680 (window-right sub))))
1681 (and (eq trail 'after)
1682 (window--resize-child-windows-skip-p sub))))
1683 (setq sub (window-right sub)))
1684 sub)
1685 (if horizontal
1686 (if (eq trail 'before)
1687 (= (+ (window-left-column sub)
1688 (window-total-size sub t))
1689 edge)
1690 (= (window-left-column sub) edge))
1691 (if (eq trail 'before)
1692 (= (+ (window-top-line sub)
1693 (window-total-size sub))
1694 edge)
1695 (= (window-top-line sub) edge)))
1696 (window-sizable-p sub delta horizontal ignore))
1697 ;; Resize only windows adjacent to EDGE.
1698 (progn
1699 (window--resize-this-window
1700 sub delta horizontal ignore t trail edge)
1701 (if (and window (eq (window-parent sub) parent))
1702 (progn
1703 ;; Assign new normal sizes.
1704 (set-window-new-normal
1705 sub (/ (float (window-new-total sub)) parent-total))
1706 (set-window-new-normal
1707 window (- (window-normal-size window horizontal)
1708 (- (window-new-normal sub)
1709 (window-normal-size sub horizontal)))))
1710 (window--resize-child-windows-normal
1711 parent horizontal sub 0 trail delta))
1712 ;; Return 'normalized to notify `window--resize-siblings' that
1713 ;; normal sizes have been already set.
1714 'normalized)
1715 ;; Resize all windows proportionally.
1716 (setq sub first)
1717 (while sub
1718 (cond
1719 ((or (window--resize-child-windows-skip-p sub)
1720 ;; Ignore windows to skip and fixed-size child windows -
1721 ;; in the latter case make it a window to skip.
1722 (and (not ignore)
1723 (window-size-fixed-p sub horizontal)
1724 (set-window-new-normal sub 'ignore))))
1725 ((< delta 0)
1726 ;; When shrinking store the number of lines/cols we can get
1727 ;; from this window here together with the total/normal size
1728 ;; factor.
1729 (set-window-new-normal
1731 (cons
1732 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
1733 (window-min-delta sub horizontal ignore trail t) ; t)
1734 (- (/ (float (window-total-size sub horizontal))
1735 parent-total)
1736 (window-normal-size sub horizontal)))))
1737 ((> delta 0)
1738 ;; When enlarging store the total/normal size factor only
1739 (set-window-new-normal
1741 (- (/ (float (window-total-size sub horizontal))
1742 parent-total)
1743 (window-normal-size sub horizontal)))))
1745 (setq sub (window-right sub)))
1747 (cond
1748 ((< delta 0)
1749 ;; Shrink windows by delta.
1750 (setq best-window t)
1751 (while (and best-window (not (zerop delta)))
1752 (setq sub first)
1753 (setq best-window nil)
1754 (setq best-value most-negative-fixnum)
1755 (while sub
1756 (when (and (consp (window-new-normal sub))
1757 (not (zerop (car (window-new-normal sub))))
1758 (> (cdr (window-new-normal sub)) best-value))
1759 (setq best-window sub)
1760 (setq best-value (cdr (window-new-normal sub))))
1762 (setq sub (window-right sub)))
1764 (when best-window
1765 (setq delta (1+ delta)))
1766 (set-window-new-total best-window -1 t)
1767 (set-window-new-normal
1768 best-window
1769 (if (= (car (window-new-normal best-window)) 1)
1770 'skip ; We can't shrink best-window any further.
1771 (cons (1- (car (window-new-normal best-window)))
1772 (- (/ (float (window-new-total best-window))
1773 parent-total)
1774 (window-normal-size best-window horizontal)))))))
1775 ((> delta 0)
1776 ;; Enlarge windows by delta.
1777 (setq best-window t)
1778 (while (and best-window (not (zerop delta)))
1779 (setq sub first)
1780 (setq best-window nil)
1781 (setq best-value most-positive-fixnum)
1782 (while sub
1783 (when (and (numberp (window-new-normal sub))
1784 (< (window-new-normal sub) best-value))
1785 (setq best-window sub)
1786 (setq best-value (window-new-normal sub)))
1788 (setq sub (window-right sub)))
1790 (when best-window
1791 (setq delta (1- delta)))
1792 (set-window-new-total best-window 1 t)
1793 (set-window-new-normal
1794 best-window
1795 (- (/ (float (window-new-total best-window))
1796 parent-total)
1797 (window-normal-size best-window horizontal))))))
1799 (when best-window
1800 (setq sub first)
1801 (while sub
1802 (when (or (consp (window-new-normal sub))
1803 (numberp (window-new-normal sub)))
1804 ;; Reset new normal size fields so `window-resize-apply'
1805 ;; won't use them to apply new sizes.
1806 (set-window-new-normal sub))
1808 (unless (eq (window-new-normal sub) 'ignore)
1809 ;; Resize this window's child windows (back-engineering
1810 ;; delta from sub's old and new total sizes).
1811 (let ((delta (- (window-new-total sub)
1812 (window-total-size sub horizontal))))
1813 (unless (and (zerop delta) (not trail))
1814 ;; For the TRAIL non-nil case we have to resize SUB
1815 ;; recursively even if it's size does not change.
1816 (window--resize-this-window
1817 sub delta horizontal ignore nil trail edge))))
1818 (setq sub (window-right sub)))))))
1820 (defun window--resize-siblings (window delta &optional horizontal ignore trail edge)
1821 "Resize other windows when WINDOW is resized vertically by DELTA lines.
1822 Optional argument HORIZONTAL non-nil means resize other windows
1823 when WINDOW is resized horizontally by DELTA columns. WINDOW
1824 itself is not resized by this function.
1826 Optional argument IGNORE non-nil means ignore restrictions
1827 imposed by fixed size windows, `window-min-height' or
1828 `window-min-width' settings. If IGNORE equals `safe', live
1829 windows may get as small as `window-safe-min-height' lines and
1830 `window-safe-min-width' columns. If IGNORE is a window, ignore
1831 restrictions for that window only. Any other non-nil value means
1832 ignore all of the above restrictions for all windows.
1834 Optional arguments TRAIL and EDGE, when non-nil, refine the set
1835 of windows that shall be resized. If TRAIL equals `before',
1836 resize only windows on the left or above EDGE. If TRAIL equals
1837 `after', resize only windows on the right or below EDGE. Also,
1838 preferably only resize windows adjacent to EDGE."
1839 (when (window-parent window)
1840 (let* ((parent (window-parent window))
1841 (sub (window-child parent)))
1842 (if (window-combined-p sub horizontal)
1843 ;; In an iso-combination try to extract DELTA from WINDOW's
1844 ;; siblings.
1845 (let ((skip (eq trail 'after))
1846 this-delta other-delta)
1847 ;; Decide which windows shall be left alone.
1848 (while sub
1849 (cond
1850 ((eq sub window)
1851 ;; Make sure WINDOW is left alone when
1852 ;; resizing its siblings.
1853 (set-window-new-normal sub 'ignore)
1854 (setq skip (eq trail 'before)))
1855 (skip
1856 ;; Make sure this sibling is left alone when
1857 ;; resizing its siblings.
1858 (set-window-new-normal sub 'ignore))
1859 ((or (window--size-ignore sub ignore)
1860 (not (window-size-fixed-p sub horizontal)))
1861 ;; Set this-delta to t to signal that we found a sibling
1862 ;; of WINDOW whose size is not fixed.
1863 (setq this-delta t)))
1865 (setq sub (window-right sub)))
1867 ;; Set this-delta to what we can get from WINDOW's siblings.
1868 (if (= (- delta) (window-total-size window horizontal))
1869 ;; A deletion, presumably. We must handle this case
1870 ;; specially since `window--resizable' can't be used.
1871 (if this-delta
1872 ;; There's at least one resizable sibling we can
1873 ;; give WINDOW's size to.
1874 (setq this-delta delta)
1875 ;; No resizable sibling exists.
1876 (setq this-delta 0))
1877 ;; Any other form of resizing.
1878 (setq this-delta
1879 (window--resizable window delta horizontal ignore trail t)))
1881 ;; Set other-delta to what we still have to get from
1882 ;; ancestor windows of parent.
1883 (setq other-delta (- delta this-delta))
1884 (unless (zerop other-delta)
1885 ;; Unless we got everything from WINDOW's siblings, PARENT
1886 ;; must be resized by other-delta lines or columns.
1887 (set-window-new-total parent other-delta 'add))
1889 (if (zerop this-delta)
1890 ;; We haven't got anything from WINDOW's siblings but we
1891 ;; must update the normal sizes to respect other-delta.
1892 (window--resize-child-windows-normal
1893 parent horizontal window this-delta trail other-delta)
1894 ;; We did get something from WINDOW's siblings which means
1895 ;; we have to resize their child windows.
1896 (unless (eq (window--resize-child-windows
1897 parent (- this-delta) horizontal
1898 window ignore trail edge)
1899 ;; If `window--resize-child-windows' returns
1900 ;; 'normalized, this means it has set the
1901 ;; normal sizes already.
1902 'normalized)
1903 ;; Set the normal sizes.
1904 (window--resize-child-windows-normal
1905 parent horizontal window this-delta trail other-delta))
1906 ;; Set DELTA to what we still have to get from ancestor
1907 ;; windows.
1908 (setq delta other-delta)))
1910 ;; In an ortho-combination all siblings of WINDOW must be
1911 ;; resized by DELTA.
1912 (set-window-new-total parent delta 'add)
1913 (while sub
1914 (unless (eq sub window)
1915 (window--resize-this-window sub delta horizontal ignore t))
1916 (setq sub (window-right sub))))
1918 (unless (zerop delta)
1919 ;; "Go up."
1920 (window--resize-siblings
1921 parent delta horizontal ignore trail edge)))))
1923 (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge)
1924 "Resize WINDOW vertically by DELTA lines.
1925 Optional argument HORIZONTAL non-nil means resize WINDOW
1926 horizontally by DELTA columns.
1928 Optional argument IGNORE non-nil means ignore restrictions
1929 imposed by fixed size windows, `window-min-height' or
1930 `window-min-width' settings. If IGNORE equals `safe', live
1931 windows may get as small as `window-safe-min-height' lines and
1932 `window-safe-min-width' columns. If IGNORE is a window, ignore
1933 restrictions for that window only. Any other non-nil value
1934 means ignore all of the above restrictions for all windows.
1936 Optional argument ADD non-nil means add DELTA to the new total
1937 size of WINDOW.
1939 Optional arguments TRAIL and EDGE, when non-nil, refine the set
1940 of windows that shall be resized. If TRAIL equals `before',
1941 resize only windows on the left or above EDGE. If TRAIL equals
1942 `after', resize only windows on the right or below EDGE. Also,
1943 preferably only resize windows adjacent to EDGE.
1945 This function recursively resizes WINDOW's child windows to fit the
1946 new size. Make sure that WINDOW is `window--resizable' before
1947 calling this function. Note that this function does not resize
1948 siblings of WINDOW or WINDOW's parent window. You have to
1949 eventually call `window-resize-apply' in order to make resizing
1950 actually take effect."
1951 (when add
1952 ;; Add DELTA to the new total size of WINDOW.
1953 (set-window-new-total window delta t))
1955 (let ((sub (window-child window)))
1956 (cond
1957 ((not sub))
1958 ((window-combined-p sub horizontal)
1959 ;; In an iso-combination resize child windows according to their
1960 ;; normal sizes.
1961 (window--resize-child-windows
1962 window delta horizontal nil ignore trail edge))
1963 ;; In an ortho-combination resize each child window by DELTA.
1965 (while sub
1966 (window--resize-this-window
1967 sub delta horizontal ignore t trail edge)
1968 (setq sub (window-right sub)))))))
1970 (defun window--resize-root-window (window delta horizontal ignore)
1971 "Resize root window WINDOW vertically by DELTA lines.
1972 HORIZONTAL non-nil means resize root window WINDOW horizontally
1973 by DELTA columns.
1975 IGNORE non-nil means ignore any restrictions imposed by fixed
1976 size windows, `window-min-height' or `window-min-width' settings.
1978 This function is only called by the frame resizing routines. It
1979 resizes windows proportionally and never deletes any windows."
1980 (when (and (windowp window) (numberp delta)
1981 (window-sizable-p window delta horizontal ignore))
1982 (window--resize-reset (window-frame window) horizontal)
1983 (window--resize-this-window window delta horizontal ignore t)))
1985 (defun window--resize-root-window-vertically (window delta)
1986 "Resize root window WINDOW vertically by DELTA lines.
1987 If DELTA is less than zero and we can't shrink WINDOW by DELTA
1988 lines, shrink it as much as possible. If DELTA is greater than
1989 zero, this function can resize fixed-size windows in order to
1990 recover the necessary lines.
1992 Return the number of lines that were recovered.
1994 This function is only called by the minibuffer window resizing
1995 routines. It resizes windows proportionally and never deletes
1996 any windows."
1997 (when (numberp delta)
1998 (let (ignore)
1999 (cond
2000 ((< delta 0)
2001 (setq delta (window-sizable window delta)))
2002 ((> delta 0)
2003 (unless (window-sizable window delta)
2004 (setq ignore t))))
2006 (window--resize-reset (window-frame window))
2007 ;; Ideally, we would resize just the last window in a combination
2008 ;; but that's not feasible for the following reason: If we grow
2009 ;; the minibuffer window and the last window cannot be shrunk any
2010 ;; more, we shrink another window instead. But if we then shrink
2011 ;; the minibuffer window again, the last window might get enlarged
2012 ;; and the state after shrinking is not the state before growing.
2013 ;; So, in practice, we'd need a history variable to record how to
2014 ;; proceed. But I'm not sure how such a variable could work with
2015 ;; repeated minibuffer window growing steps.
2016 (window--resize-this-window window delta nil ignore t)
2017 delta)))
2019 (defun adjust-window-trailing-edge (window delta &optional horizontal)
2020 "Move WINDOW's bottom edge by DELTA lines.
2021 Optional argument HORIZONTAL non-nil means move WINDOW's right
2022 edge by DELTA columns. WINDOW defaults to the selected window.
2024 If DELTA is greater than zero, move the edge downwards or to the
2025 right. If DELTA is less than zero, move the edge upwards or to
2026 the left. If the edge can't be moved by DELTA lines or columns,
2027 move it as far as possible in the desired direction."
2028 (setq window (window-normalize-window window))
2029 (let* ((frame (window-frame window))
2030 (minibuffer-window (minibuffer-window frame))
2031 (right window)
2032 left this-delta min-delta max-delta)
2033 ;; Find the edge we want to move.
2034 (while (and (or (not (window-combined-p right horizontal))
2035 (not (window-right right)))
2036 (setq right (window-parent right))))
2037 (cond
2038 ((and (not right) (not horizontal)
2039 ;; Resize the minibuffer window if it's on the same frame as
2040 ;; and immediately below WINDOW and it's either active or
2041 ;; `resize-mini-windows' is nil.
2042 (eq (window-frame minibuffer-window) frame)
2043 (= (nth 1 (window-edges minibuffer-window))
2044 (nth 3 (window-edges window)))
2045 (or (not resize-mini-windows)
2046 (eq minibuffer-window (active-minibuffer-window))))
2047 (window--resize-mini-window minibuffer-window (- delta)))
2048 ((or (not (setq left right)) (not (setq right (window-right right))))
2049 (if horizontal
2050 (error "No window on the right of this one")
2051 (error "No window below this one")))
2053 ;; Set LEFT to the first resizable window on the left. This step is
2054 ;; needed to handle fixed-size windows.
2055 (while (and left (window-size-fixed-p left horizontal))
2056 (setq left
2057 (or (window-left left)
2058 (progn
2059 (while (and (setq left (window-parent left))
2060 (not (window-combined-p left horizontal))))
2061 (window-left left)))))
2062 (unless left
2063 (if horizontal
2064 (error "No resizable window on the left of this one")
2065 (error "No resizable window above this one")))
2067 ;; Set RIGHT to the first resizable window on the right. This step
2068 ;; is needed to handle fixed-size windows.
2069 (while (and right (window-size-fixed-p right horizontal))
2070 (setq right
2071 (or (window-right right)
2072 (progn
2073 (while (and (setq right (window-parent right))
2074 (not (window-combined-p right horizontal))))
2075 (window-right right)))))
2076 (unless right
2077 (if horizontal
2078 (error "No resizable window on the right of this one")
2079 (error "No resizable window below this one")))
2081 ;; LEFT and RIGHT (which might be both internal windows) are now the
2082 ;; two windows we want to resize.
2083 (cond
2084 ((> delta 0)
2085 (setq max-delta (window--max-delta-1 left 0 horizontal nil 'after))
2086 (setq min-delta (window--min-delta-1 right (- delta) horizontal nil 'before))
2087 (when (or (< max-delta delta) (> min-delta (- delta)))
2088 ;; We can't get the whole DELTA - move as far as possible.
2089 (setq delta (min max-delta (- min-delta))))
2090 (unless (zerop delta)
2091 ;; Start resizing.
2092 (window--resize-reset frame horizontal)
2093 ;; Try to enlarge LEFT first.
2094 (setq this-delta (window--resizable left delta horizontal))
2095 (unless (zerop this-delta)
2096 (window--resize-this-window
2097 left this-delta horizontal nil t 'before
2098 (if horizontal
2099 (+ (window-left-column left) (window-total-size left t))
2100 (+ (window-top-line left) (window-total-size left)))))
2101 ;; Shrink windows on right of LEFT.
2102 (window--resize-siblings
2103 left delta horizontal nil 'after
2104 (if horizontal
2105 (window-left-column right)
2106 (window-top-line right)))))
2107 ((< delta 0)
2108 (setq max-delta (window--max-delta-1 right 0 horizontal nil 'before))
2109 (setq min-delta (window--min-delta-1 left delta horizontal nil 'after))
2110 (when (or (< max-delta (- delta)) (> min-delta delta))
2111 ;; We can't get the whole DELTA - move as far as possible.
2112 (setq delta (max (- max-delta) min-delta)))
2113 (unless (zerop delta)
2114 ;; Start resizing.
2115 (window--resize-reset frame horizontal)
2116 ;; Try to enlarge RIGHT.
2117 (setq this-delta (window--resizable right (- delta) horizontal))
2118 (unless (zerop this-delta)
2119 (window--resize-this-window
2120 right this-delta horizontal nil t 'after
2121 (if horizontal
2122 (window-left-column right)
2123 (window-top-line right))))
2124 ;; Shrink windows on left of RIGHT.
2125 (window--resize-siblings
2126 right (- delta) horizontal nil 'before
2127 (if horizontal
2128 (+ (window-left-column left) (window-total-size left t))
2129 (+ (window-top-line left) (window-total-size left)))))))
2130 (unless (zerop delta)
2131 ;; Don't report an error in the standard case.
2132 (unless (window-resize-apply frame horizontal)
2133 ;; But do report an error if applying the changes fails.
2134 (error "Failed adjusting window %s" window)))))))
2136 (defun enlarge-window (delta &optional horizontal)
2137 "Make the selected window DELTA lines taller.
2138 Interactively, if no argument is given, make the selected window
2139 one line taller. If optional argument HORIZONTAL is non-nil,
2140 make selected window wider by DELTA columns. If DELTA is
2141 negative, shrink selected window by -DELTA lines or columns.
2142 Return nil."
2143 (interactive "p")
2144 (let ((minibuffer-window (minibuffer-window)))
2145 (cond
2146 ((zerop delta))
2147 ((window-size-fixed-p nil horizontal)
2148 (error "Selected window has fixed size"))
2149 ((window-minibuffer-p)
2150 (if horizontal
2151 (error "Cannot resize minibuffer window horizontally")
2152 (window--resize-mini-window (selected-window) delta)))
2153 ((and (not horizontal)
2154 (window-full-height-p)
2155 (eq (window-frame minibuffer-window) (selected-frame))
2156 (not resize-mini-windows))
2157 ;; If the selected window is full height and `resize-mini-windows'
2158 ;; is nil, resize the minibuffer window.
2159 (window--resize-mini-window minibuffer-window (- delta)))
2160 ((window--resizable-p nil delta horizontal)
2161 (window-resize nil delta horizontal))
2163 (window-resize
2164 nil (if (> delta 0)
2165 (window-max-delta nil horizontal)
2166 (- (window-min-delta nil horizontal)))
2167 horizontal)))))
2169 (defun shrink-window (delta &optional horizontal)
2170 "Make the selected window DELTA lines smaller.
2171 Interactively, if no argument is given, make the selected window
2172 one line smaller. If optional argument HORIZONTAL is non-nil,
2173 make selected window narrower by DELTA columns. If DELTA is
2174 negative, enlarge selected window by -DELTA lines or columns.
2175 Also see the `window-min-height' variable.
2176 Return nil."
2177 (interactive "p")
2178 (let ((minibuffer-window (minibuffer-window)))
2179 (cond
2180 ((zerop delta))
2181 ((window-size-fixed-p nil horizontal)
2182 (error "Selected window has fixed size"))
2183 ((window-minibuffer-p)
2184 (if horizontal
2185 (error "Cannot resize minibuffer window horizontally")
2186 (window--resize-mini-window (selected-window) (- delta))))
2187 ((and (not horizontal)
2188 (window-full-height-p)
2189 (eq (window-frame minibuffer-window) (selected-frame))
2190 (not resize-mini-windows))
2191 ;; If the selected window is full height and `resize-mini-windows'
2192 ;; is nil, resize the minibuffer window.
2193 (window--resize-mini-window minibuffer-window delta))
2194 ((window--resizable-p nil (- delta) horizontal)
2195 (window-resize nil (- delta) horizontal))
2197 (window-resize
2198 nil (if (> delta 0)
2199 (- (window-min-delta nil horizontal))
2200 (window-max-delta nil horizontal))
2201 horizontal)))))
2203 (defun maximize-window (&optional window)
2204 "Maximize WINDOW.
2205 Make WINDOW as large as possible without deleting any windows.
2206 WINDOW can be any window and defaults to the selected window."
2207 (interactive)
2208 (setq window (window-normalize-window window))
2209 (window-resize window (window-max-delta window))
2210 (window-resize window (window-max-delta window t) t))
2212 (defun minimize-window (&optional window)
2213 "Minimize WINDOW.
2214 Make WINDOW as small as possible without deleting any windows.
2215 WINDOW can be any window and defaults to the selected window."
2216 (interactive)
2217 (setq window (window-normalize-window window))
2218 (window-resize window (- (window-min-delta window)))
2219 (window-resize window (- (window-min-delta window t)) t))
2221 (defun frame-root-window-p (window)
2222 "Return non-nil if WINDOW is the root window of its frame."
2223 (eq window (frame-root-window window)))
2225 (defun window--subtree (window &optional next)
2226 "Return window subtree rooted at WINDOW.
2227 Optional argument NEXT non-nil means include WINDOW's right
2228 siblings in the return value.
2230 See the documentation of `window-tree' for a description of the
2231 return value."
2232 (let (list)
2233 (while window
2234 (setq list
2235 (cons
2236 (cond
2237 ((window-top-child window)
2238 (cons t (cons (window-edges window)
2239 (window--subtree (window-top-child window) t))))
2240 ((window-left-child window)
2241 (cons nil (cons (window-edges window)
2242 (window--subtree (window-left-child window) t))))
2243 (t window))
2244 list))
2245 (setq window (when next (window-next-sibling window))))
2246 (nreverse list)))
2248 (defun window-tree (&optional frame)
2249 "Return the window tree of frame FRAME.
2250 FRAME must be a live frame and defaults to the selected frame.
2251 The return value is a list of the form (ROOT MINI), where ROOT
2252 represents the window tree of the frame's root window, and MINI
2253 is the frame's minibuffer window.
2255 If the root window is not split, ROOT is the root window itself.
2256 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
2257 for a horizontal split, and t for a vertical split. EDGES gives
2258 the combined size and position of the child windows in the split,
2259 and the rest of the elements are the child windows in the split.
2260 Each of the child windows may again be a window or a list
2261 representing a window split, and so on. EDGES is a list (LEFT
2262 TOP RIGHT BOTTOM) as returned by `window-edges'."
2263 (setq frame (window-normalize-frame frame))
2264 (window--subtree (frame-root-window frame) t))
2266 (defun other-window (count &optional all-frames)
2267 "Select another window in cyclic ordering of windows.
2268 COUNT specifies the number of windows to skip, starting with the
2269 selected window, before making the selection. If COUNT is
2270 positive, skip COUNT windows forwards. If COUNT is negative,
2271 skip -COUNT windows backwards. COUNT zero means do not skip any
2272 window, so select the selected window. In an interactive call,
2273 COUNT is the numeric prefix argument. Return nil.
2275 If the `other-window' parameter of the selected window is a
2276 function and `ignore-window-parameters' is nil, call that
2277 function with the arguments COUNT and ALL-FRAMES.
2279 This function does not select a window whose `no-other-window'
2280 window parameter is non-nil.
2282 This function uses `next-window' for finding the window to
2283 select. The argument ALL-FRAMES has the same meaning as in
2284 `next-window', but the MINIBUF argument of `next-window' is
2285 always effectively nil."
2286 (interactive "p")
2287 (let* ((window (selected-window))
2288 (function (and (not ignore-window-parameters)
2289 (window-parameter window 'other-window)))
2290 old-window old-count)
2291 (if (functionp function)
2292 (funcall function count all-frames)
2293 ;; `next-window' and `previous-window' may return a window we are
2294 ;; not allowed to select. Hence we need an exit strategy in case
2295 ;; all windows are non-selectable.
2296 (catch 'exit
2297 (while (> count 0)
2298 (setq window (next-window window nil all-frames))
2299 (cond
2300 ((eq window old-window)
2301 (when (= count old-count)
2302 ;; Keep out of infinite loops. When COUNT has not changed
2303 ;; since we last looked at `window' we're probably in one.
2304 (throw 'exit nil)))
2305 ((window-parameter window 'no-other-window)
2306 (unless old-window
2307 ;; The first non-selectable window `next-window' got us:
2308 ;; Remember it and the current value of COUNT.
2309 (setq old-window window)
2310 (setq old-count count)))
2312 (setq count (1- count)))))
2313 (while (< count 0)
2314 (setq window (previous-window window nil all-frames))
2315 (cond
2316 ((eq window old-window)
2317 (when (= count old-count)
2318 ;; Keep out of infinite loops. When COUNT has not changed
2319 ;; since we last looked at `window' we're probably in one.
2320 (throw 'exit nil)))
2321 ((window-parameter window 'no-other-window)
2322 (unless old-window
2323 ;; The first non-selectable window `previous-window' got
2324 ;; us: Remember it and the current value of COUNT.
2325 (setq old-window window)
2326 (setq old-count count)))
2328 (setq count (1+ count)))))
2330 (select-window window)
2331 ;; Always return nil.
2332 nil))))
2334 ;; This should probably return non-nil when the selected window is part
2335 ;; of an atomic window whose root is the frame's root window.
2336 (defun one-window-p (&optional nomini all-frames)
2337 "Return non-nil if the selected window is the only window.
2338 Optional arg NOMINI non-nil means don't count the minibuffer
2339 even if it is active. Otherwise, the minibuffer is counted
2340 when it is active.
2342 Optional argument ALL-FRAMES specifies the set of frames to
2343 consider, see also `next-window'. ALL-FRAMES nil or omitted
2344 means consider windows on the selected frame only, plus the
2345 minibuffer window if specified by the NOMINI argument. If the
2346 minibuffer counts, consider all windows on all frames that share
2347 that minibuffer too. The remaining non-nil values of ALL-FRAMES
2348 with a special meaning are:
2350 - t means consider all windows on all existing frames.
2352 - `visible' means consider all windows on all visible frames on
2353 the current terminal.
2355 - 0 (the number zero) means consider all windows on all visible
2356 and iconified frames on the current terminal.
2358 - A frame means consider all windows on that frame only.
2360 Anything else means consider all windows on the selected frame
2361 and no others."
2362 (let ((base-window (selected-window)))
2363 (if (and nomini (eq base-window (minibuffer-window)))
2364 (setq base-window (next-window base-window)))
2365 (eq base-window
2366 (next-window base-window (if nomini 'arg) all-frames))))
2368 ;;; Deleting windows.
2369 (defun window-deletable-p (&optional window)
2370 "Return t if WINDOW can be safely deleted from its frame.
2371 Return `frame' if deleting WINDOW should also delete its
2372 frame."
2373 (setq window (window-normalize-window window))
2375 (unless ignore-window-parameters
2376 ;; Handle atomicity.
2377 (when (window-parameter window 'window-atom)
2378 (setq window (window-atom-root window))))
2380 (let ((parent (window-parent window))
2381 (frame (window-frame window)))
2382 (cond
2383 ((frame-root-window-p window)
2384 ;; WINDOW's frame can be deleted only if there are other frames
2385 ;; on the same terminal, and it does not contain the active
2386 ;; minibuffer.
2387 (unless (or (eq frame (next-frame frame 0))
2388 (let ((minibuf (active-minibuffer-window)))
2389 (and minibuf (eq frame (window-frame minibuf)))))
2390 'frame))
2391 ((or ignore-window-parameters
2392 (not (eq (window-parameter window 'window-side) 'none))
2393 (and parent (eq (window-parameter parent 'window-side) 'none)))
2394 ;; WINDOW can be deleted unless it is the main window of its
2395 ;; frame.
2396 t))))
2398 (defun window--in-subtree-p (window root)
2399 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
2400 (or (eq window root)
2401 (let ((parent (window-parent window)))
2402 (catch 'done
2403 (while parent
2404 (if (eq parent root)
2405 (throw 'done t)
2406 (setq parent (window-parent parent))))))))
2408 (defun delete-window (&optional window)
2409 "Delete WINDOW.
2410 WINDOW can be an arbitrary window and defaults to the selected
2411 one. Return nil.
2413 If the variable `ignore-window-parameters' is non-nil or the
2414 `delete-window' parameter of WINDOW equals t, do not process any
2415 parameters of WINDOW. Otherwise, if the `delete-window'
2416 parameter of WINDOW specifies a function, call that function with
2417 WINDOW as its sole argument and return the value returned by that
2418 function.
2420 Otherwise, if WINDOW is part of an atomic window, call
2421 `delete-window' with the root of the atomic window as its
2422 argument. If WINDOW is the only window on its frame or the last
2423 non-side window, signal an error."
2424 (interactive)
2425 (setq window (window-normalize-window window))
2426 (let* ((frame (window-frame window))
2427 (function (window-parameter window 'delete-window))
2428 (parent (window-parent window))
2429 atom-root)
2430 (window--check frame)
2431 (catch 'done
2432 ;; Handle window parameters.
2433 (cond
2434 ;; Ignore window parameters if `ignore-window-parameters' tells
2435 ;; us so or `delete-window' equals t.
2436 ((or ignore-window-parameters (eq function t)))
2437 ((functionp function)
2438 ;; The `delete-window' parameter specifies the function to call.
2439 ;; If that function is `ignore' nothing is done. It's up to the
2440 ;; function called here to avoid infinite recursion.
2441 (throw 'done (funcall function window)))
2442 ((and (window-parameter window 'window-atom)
2443 (setq atom-root (window-atom-root window))
2444 (not (eq atom-root window)))
2445 (throw 'done (delete-window atom-root)))
2446 ((and (eq (window-parameter window 'window-side) 'none)
2447 (or (not parent)
2448 (not (eq (window-parameter parent 'window-side) 'none))))
2449 (error "Attempt to delete last non-side window"))
2450 ((not parent)
2451 (error "Attempt to delete minibuffer or sole ordinary window")))
2453 (let* ((horizontal (window-left-child parent))
2454 (size (window-total-size window horizontal))
2455 (frame-selected
2456 (window--in-subtree-p (frame-selected-window frame) window))
2457 ;; Emacs 23 preferably gives WINDOW's space to its left
2458 ;; sibling.
2459 (sibling (or (window-left window) (window-right window))))
2460 (window--resize-reset frame horizontal)
2461 (cond
2462 ((and (not window-combination-resize)
2463 sibling (window-sizable-p sibling size))
2464 ;; Resize WINDOW's sibling.
2465 (window--resize-this-window sibling size horizontal nil t)
2466 (set-window-new-normal
2467 sibling (+ (window-normal-size sibling horizontal)
2468 (window-normal-size window horizontal))))
2469 ((window--resizable-p window (- size) horizontal nil nil nil t)
2470 ;; Can do without resizing fixed-size windows.
2471 (window--resize-siblings window (- size) horizontal))
2473 ;; Can't do without resizing fixed-size windows.
2474 (window--resize-siblings window (- size) horizontal t)))
2475 ;; Actually delete WINDOW.
2476 (delete-window-internal window)
2477 (when (and frame-selected
2478 (window-parameter
2479 (frame-selected-window frame) 'no-other-window))
2480 ;; `delete-window-internal' has selected a window that should
2481 ;; not be selected, fix this here.
2482 (other-window -1 frame))
2483 (run-window-configuration-change-hook frame)
2484 (window--check frame)
2485 ;; Always return nil.
2486 nil))))
2488 (defun delete-other-windows (&optional window)
2489 "Make WINDOW fill its frame.
2490 WINDOW may be any window and defaults to the selected one.
2491 Return nil.
2493 If the variable `ignore-window-parameters' is non-nil or the
2494 `delete-other-windows' parameter of WINDOW equals t, do not
2495 process any parameters of WINDOW. Otherwise, if the
2496 `delete-other-windows' parameter of WINDOW specifies a function,
2497 call that function with WINDOW as its sole argument and return
2498 the value returned by that function.
2500 Otherwise, if WINDOW is part of an atomic window, call this
2501 function with the root of the atomic window as its argument. If
2502 WINDOW is a non-side window, make WINDOW the only non-side window
2503 on the frame. Side windows are not deleted. If WINDOW is a side
2504 window signal an error."
2505 (interactive)
2506 (setq window (window-normalize-window window))
2507 (let* ((frame (window-frame window))
2508 (function (window-parameter window 'delete-other-windows))
2509 (window-side (window-parameter window 'window-side))
2510 atom-root side-main)
2511 (window--check frame)
2512 (catch 'done
2513 (cond
2514 ;; Ignore window parameters if `ignore-window-parameters' is t or
2515 ;; `delete-other-windows' is t.
2516 ((or ignore-window-parameters (eq function t)))
2517 ((functionp function)
2518 ;; The `delete-other-windows' parameter specifies the function
2519 ;; to call. If the function is `ignore' no windows are deleted.
2520 ;; It's up to the function called to avoid infinite recursion.
2521 (throw 'done (funcall function window)))
2522 ((and (window-parameter window 'window-atom)
2523 (setq atom-root (window-atom-root window))
2524 (not (eq atom-root window)))
2525 (throw 'done (delete-other-windows atom-root)))
2526 ((eq window-side 'none)
2527 ;; Set side-main to the major non-side window.
2528 (setq side-main (window-with-parameter 'window-side 'none frame t)))
2529 ((memq window-side window-sides)
2530 (error "Cannot make side window the only window")))
2531 ;; If WINDOW is the main non-side window, do nothing.
2532 (unless (eq window side-main)
2533 (delete-other-windows-internal window side-main)
2534 (run-window-configuration-change-hook frame)
2535 (window--check frame))
2536 ;; Always return nil.
2537 nil)))
2539 (defun delete-other-windows-vertically (&optional window)
2540 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
2541 This may be a useful alternative binding for \\[delete-other-windows]
2542 if you often split windows horizontally."
2543 (interactive)
2544 (let* ((window (or window (selected-window)))
2545 (edges (window-edges window))
2546 (w window) delenda)
2547 (while (not (eq (setq w (next-window w 1)) window))
2548 (let ((e (window-edges w)))
2549 (when (and (= (car e) (car edges))
2550 (= (nth 2 e) (nth 2 edges)))
2551 (push w delenda))))
2552 (mapc 'delete-window delenda)))
2554 ;;; Windows and buffers.
2556 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
2557 ;; for (1) determining which buffer to show in the window when its
2558 ;; buffer shall be buried or killed and (2) which buffer to show for
2559 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2561 ;; `prev-buffers' consists of <buffer, window-start, window-point>
2562 ;; triples. The entries on this list are ordered by the time their
2563 ;; buffer has been removed from the window, the most recently removed
2564 ;; buffer's entry being first. The window-start and window-point
2565 ;; components are `window-start' and `window-point' at the time the
2566 ;; buffer was removed from the window which implies that the entry must
2567 ;; be added when `set-window-buffer' removes the buffer from the window.
2569 ;; `next-buffers' is the list of buffers that have been replaced
2570 ;; recently by `switch-to-prev-buffer'. These buffers are the least
2571 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
2572 ;; candidates of `switch-to-next-buffer' to switch to. This list is
2573 ;; reset to nil by any action changing the window's buffer with the
2574 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
2575 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
2576 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
2578 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
2579 ;; if such a buffer was killed while the window was hidden within a
2580 ;; window configuration. Such killed buffers get removed whenever
2581 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
2583 ;; The following function is called by `set-window-buffer' _before_ it
2584 ;; replaces the buffer of the argument window with the new buffer.
2585 (defun record-window-buffer (&optional window)
2586 "Record WINDOW's buffer.
2587 WINDOW must be a live window and defaults to the selected one."
2588 (let* ((window (window-normalize-window window t))
2589 (buffer (window-buffer window))
2590 (entry (assq buffer (window-prev-buffers window))))
2591 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
2592 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2593 (set-window-next-buffers window nil)
2595 (when entry
2596 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
2597 (set-window-prev-buffers
2598 window (assq-delete-all buffer (window-prev-buffers window))))
2600 ;; Don't record insignificant buffers.
2601 (unless (eq (aref (buffer-name buffer) 0) ?\s)
2602 ;; Add an entry for buffer to WINDOW's previous buffers.
2603 (with-current-buffer buffer
2604 (let ((start (window-start window))
2605 (point (window-point-1 window)))
2606 (setq entry
2607 (cons buffer
2608 (if entry
2609 ;; We have an entry, update marker positions.
2610 (list (set-marker (nth 1 entry) start)
2611 (set-marker (nth 2 entry) point))
2612 ;; Make new markers.
2613 (list (copy-marker start)
2614 (copy-marker point)))))
2616 (set-window-prev-buffers
2617 window (cons entry (window-prev-buffers window))))))))
2619 (defun unrecord-window-buffer (&optional window buffer)
2620 "Unrecord BUFFER in WINDOW.
2621 WINDOW must be a live window and defaults to the selected one.
2622 BUFFER must be a live buffer and defaults to the buffer of
2623 WINDOW."
2624 (let* ((window (window-normalize-window window t))
2625 (buffer (or buffer (window-buffer window))))
2626 (set-window-prev-buffers
2627 window (assq-delete-all buffer (window-prev-buffers window)))
2628 (set-window-next-buffers
2629 window (delq buffer (window-next-buffers window)))))
2631 (defun set-window-buffer-start-and-point (window buffer &optional start point)
2632 "Set WINDOW's buffer to BUFFER.
2633 Optional argument START non-nil means set WINDOW's start position
2634 to START. Optional argument POINT non-nil means set WINDOW's
2635 point to POINT. If WINDOW is selected this also sets BUFFER's
2636 `point' to POINT. If WINDOW is selected and the buffer it showed
2637 before was current this also makes BUFFER the current buffer."
2638 (let ((selected (eq window (selected-window)))
2639 (current (eq (window-buffer window) (current-buffer))))
2640 (set-window-buffer window buffer)
2641 (when (and selected current)
2642 (set-buffer buffer))
2643 (when start
2644 ;; Don't force window-start here (even if POINT is nil).
2645 (set-window-start window start t))
2646 (when point
2647 (set-window-point-1 window point))))
2649 (defcustom switch-to-visible-buffer t
2650 "If non-nil, allow switching to an already visible buffer.
2651 If this variable is non-nil, `switch-to-prev-buffer' and
2652 `switch-to-next-buffer' may switch to an already visible buffer
2653 provided the buffer was shown in the argument window before. If
2654 this variable is nil, `switch-to-prev-buffer' and
2655 `switch-to-next-buffer' always try to avoid switching to a buffer
2656 that is already visible in another window on the same frame."
2657 :type 'boolean
2658 :version "24.1"
2659 :group 'windows)
2661 (defun switch-to-prev-buffer (&optional window bury-or-kill)
2662 "In WINDOW switch to previous buffer.
2663 WINDOW must be a live window and defaults to the selected one.
2664 Return the buffer switched to, nil if no suitable buffer could be
2665 found.
2667 Optional argument BURY-OR-KILL non-nil means the buffer currently
2668 shown in WINDOW is about to be buried or killed and consequently
2669 shall not be switched to in future invocations of this command."
2670 (interactive)
2671 (let* ((window (window-normalize-window window t))
2672 (frame (window-frame window))
2673 (old-buffer (window-buffer window))
2674 ;; Save this since it's destroyed by `set-window-buffer'.
2675 (next-buffers (window-next-buffers window))
2676 entry buffer new-buffer killed-buffers visible)
2677 (when (window-dedicated-p window)
2678 (error "Window %s is dedicated to buffer %s" window old-buffer))
2680 (catch 'found
2681 ;; Scan WINDOW's previous buffers first, skipping entries of next
2682 ;; buffers.
2683 (dolist (entry (window-prev-buffers window))
2684 (when (and (setq buffer (car entry))
2685 (or (buffer-live-p buffer)
2686 (not (setq killed-buffers
2687 (cons buffer killed-buffers))))
2688 (not (eq buffer old-buffer))
2689 (or bury-or-kill (not (memq buffer next-buffers))))
2690 (if (and (not switch-to-visible-buffer)
2691 (get-buffer-window buffer frame))
2692 ;; Try to avoid showing a buffer visible in some other window.
2693 (setq visible buffer)
2694 (setq new-buffer buffer)
2695 (set-window-buffer-start-and-point
2696 window new-buffer (nth 1 entry) (nth 2 entry))
2697 (throw 'found t))))
2698 ;; Scan reverted buffer list of WINDOW's frame next, skipping
2699 ;; entries of next buffers. Note that when we bury or kill a
2700 ;; buffer we don't reverse the global buffer list to avoid showing
2701 ;; a buried buffer instead. Otherwise, we must reverse the global
2702 ;; buffer list in order to make sure that switching to the
2703 ;; previous/next buffer traverse it in opposite directions.
2704 (dolist (buffer (if bury-or-kill
2705 (buffer-list frame)
2706 (nreverse (buffer-list frame))))
2707 (when (and (buffer-live-p buffer)
2708 (not (eq buffer old-buffer))
2709 (not (eq (aref (buffer-name buffer) 0) ?\s))
2710 (or bury-or-kill (not (memq buffer next-buffers))))
2711 (if (get-buffer-window buffer frame)
2712 ;; Try to avoid showing a buffer visible in some other window.
2713 (unless visible
2714 (setq visible buffer))
2715 (setq new-buffer buffer)
2716 (set-window-buffer-start-and-point window new-buffer)
2717 (throw 'found t))))
2718 (unless bury-or-kill
2719 ;; Scan reverted next buffers last (must not use nreverse
2720 ;; here!).
2721 (dolist (buffer (reverse next-buffers))
2722 ;; Actually, buffer _must_ be live here since otherwise it
2723 ;; would have been caught in the scan of previous buffers.
2724 (when (and (or (buffer-live-p buffer)
2725 (not (setq killed-buffers
2726 (cons buffer killed-buffers))))
2727 (not (eq buffer old-buffer))
2728 (setq entry (assq buffer (window-prev-buffers window))))
2729 (setq new-buffer buffer)
2730 (set-window-buffer-start-and-point
2731 window new-buffer (nth 1 entry) (nth 2 entry))
2732 (throw 'found t))))
2734 ;; Show a buffer visible in another window.
2735 (when visible
2736 (setq new-buffer visible)
2737 (set-window-buffer-start-and-point window new-buffer)))
2739 (if bury-or-kill
2740 ;; Remove `old-buffer' from WINDOW's previous and (restored list
2741 ;; of) next buffers.
2742 (progn
2743 (set-window-prev-buffers
2744 window (assq-delete-all old-buffer (window-prev-buffers window)))
2745 (set-window-next-buffers window (delq old-buffer next-buffers)))
2746 ;; Move `old-buffer' to head of WINDOW's restored list of next
2747 ;; buffers.
2748 (set-window-next-buffers
2749 window (cons old-buffer (delq old-buffer next-buffers))))
2751 ;; Remove killed buffers from WINDOW's previous and next buffers.
2752 (when killed-buffers
2753 (dolist (buffer killed-buffers)
2754 (set-window-prev-buffers
2755 window (assq-delete-all buffer (window-prev-buffers window)))
2756 (set-window-next-buffers
2757 window (delq buffer (window-next-buffers window)))))
2759 ;; Return new-buffer.
2760 new-buffer))
2762 (defun switch-to-next-buffer (&optional window)
2763 "In WINDOW switch to next buffer.
2764 WINDOW must be a live window and defaults to the selected one.
2765 Return the buffer switched to, nil if no suitable buffer could be
2766 found."
2767 (interactive)
2768 (let* ((window (window-normalize-window window t))
2769 (frame (window-frame window))
2770 (old-buffer (window-buffer window))
2771 (next-buffers (window-next-buffers window))
2772 buffer new-buffer entry killed-buffers visible)
2773 (when (window-dedicated-p window)
2774 (error "Window %s is dedicated to buffer %s" window old-buffer))
2776 (catch 'found
2777 ;; Scan WINDOW's next buffers first.
2778 (dolist (buffer next-buffers)
2779 (when (and (or (buffer-live-p buffer)
2780 (not (setq killed-buffers
2781 (cons buffer killed-buffers))))
2782 (not (eq buffer old-buffer))
2783 (setq entry (assq buffer (window-prev-buffers window))))
2784 (setq new-buffer buffer)
2785 (set-window-buffer-start-and-point
2786 window new-buffer (nth 1 entry) (nth 2 entry))
2787 (throw 'found t)))
2788 ;; Scan the buffer list of WINDOW's frame next, skipping previous
2789 ;; buffers entries.
2790 (dolist (buffer (buffer-list frame))
2791 (when (and (buffer-live-p buffer) (not (eq buffer old-buffer))
2792 (not (eq (aref (buffer-name buffer) 0) ?\s))
2793 (not (assq buffer (window-prev-buffers window))))
2794 (if (get-buffer-window buffer frame)
2795 ;; Try to avoid showing a buffer visible in some other window.
2796 (setq visible buffer)
2797 (setq new-buffer buffer)
2798 (set-window-buffer-start-and-point window new-buffer)
2799 (throw 'found t))))
2800 ;; Scan WINDOW's reverted previous buffers last (must not use
2801 ;; nreverse here!)
2802 (dolist (entry (reverse (window-prev-buffers window)))
2803 (when (and (setq buffer (car entry))
2804 (or (buffer-live-p buffer)
2805 (not (setq killed-buffers
2806 (cons buffer killed-buffers))))
2807 (not (eq buffer old-buffer)))
2808 (if (and (not switch-to-visible-buffer)
2809 (get-buffer-window buffer frame))
2810 ;; Try to avoid showing a buffer visible in some other window.
2811 (unless visible
2812 (setq visible buffer))
2813 (setq new-buffer buffer)
2814 (set-window-buffer-start-and-point
2815 window new-buffer (nth 1 entry) (nth 2 entry))
2816 (throw 'found t))))
2818 ;; Show a buffer visible in another window.
2819 (when visible
2820 (setq new-buffer visible)
2821 (set-window-buffer-start-and-point window new-buffer)))
2823 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
2824 (set-window-next-buffers window (delq new-buffer next-buffers))
2826 ;; Remove killed buffers from WINDOW's previous and next buffers.
2827 (when killed-buffers
2828 (dolist (buffer killed-buffers)
2829 (set-window-prev-buffers
2830 window (assq-delete-all buffer (window-prev-buffers window)))
2831 (set-window-next-buffers
2832 window (delq buffer (window-next-buffers window)))))
2834 ;; Return new-buffer.
2835 new-buffer))
2837 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
2838 "Search LIST for a valid buffer to display in FRAME.
2839 Return nil when all buffers in LIST are undesirable for display,
2840 otherwise return the first suitable buffer in LIST.
2842 Buffers not visible in windows are preferred to visible buffers,
2843 unless VISIBLE-OK is non-nil.
2844 If the optional argument FRAME is nil, it defaults to the selected frame.
2845 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
2846 ;; This logic is more or less copied from other-buffer.
2847 (setq frame (or frame (selected-frame)))
2848 (let ((pred (frame-parameter frame 'buffer-predicate))
2849 found buf)
2850 (while (and (not found) list)
2851 (setq buf (car list))
2852 (if (and (not (eq buffer buf))
2853 (buffer-live-p buf)
2854 (or (null pred) (funcall pred buf))
2855 (not (eq (aref (buffer-name buf) 0) ?\s))
2856 (or visible-ok (null (get-buffer-window buf 'visible))))
2857 (setq found buf)
2858 (setq list (cdr list))))
2859 (car list)))
2861 (defun last-buffer (&optional buffer visible-ok frame)
2862 "Return the last buffer in FRAME's buffer list.
2863 If BUFFER is the last buffer, return the preceding buffer
2864 instead. Buffers not visible in windows are preferred to visible
2865 buffers, unless optional argument VISIBLE-OK is non-nil.
2866 Optional third argument FRAME nil or omitted means use the
2867 selected frame's buffer list. If no such buffer exists, return
2868 the buffer `*scratch*', creating it if necessary."
2869 (setq frame (or frame (selected-frame)))
2870 (or (get-next-valid-buffer (nreverse (buffer-list frame))
2871 buffer visible-ok frame)
2872 (get-buffer "*scratch*")
2873 (let ((scratch (get-buffer-create "*scratch*")))
2874 (set-buffer-major-mode scratch)
2875 scratch)))
2877 (defcustom frame-auto-hide-function #'iconify-frame
2878 "Function called to automatically hide frames.
2879 The function is called with one argument - a frame.
2881 Functions affected by this option are those that bury a buffer
2882 shown in a separate frame like `quit-window' and `bury-buffer'."
2883 :type '(choice (const :tag "Iconify" iconify-frame)
2884 (const :tag "Delete" delete-frame)
2885 (const :tag "Do nothing" ignore)
2886 function)
2887 :group 'windows
2888 :group 'frames
2889 :version "24.1")
2891 (defun window--delete (&optional window dedicated-only kill)
2892 "Delete WINDOW if possible.
2893 WINDOW must be a live window and defaults to the selected one.
2894 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
2895 only if it's dedicated to its buffer. Optional argument KILL
2896 means the buffer shown in window will be killed. Return non-nil
2897 if WINDOW gets deleted or its frame is auto-hidden."
2898 (setq window (window-normalize-window window t))
2899 (unless (and dedicated-only (not (window-dedicated-p window)))
2900 (let ((deletable (window-deletable-p window)))
2901 (cond
2902 ((eq deletable 'frame)
2903 (let ((frame (window-frame window)))
2904 (cond
2905 (kill
2906 (delete-frame frame))
2907 ((functionp frame-auto-hide-function)
2908 (funcall frame-auto-hide-function frame))))
2909 'frame)
2910 (deletable
2911 (delete-window window)
2912 t)))))
2914 (defun bury-buffer (&optional buffer-or-name)
2915 "Put BUFFER-OR-NAME at the end of the list of all buffers.
2916 There it is the least likely candidate for `other-buffer' to
2917 return; thus, the least likely buffer for \\[switch-to-buffer] to
2918 select by default.
2920 You can specify a buffer name as BUFFER-OR-NAME, or an actual
2921 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
2922 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
2923 remove the current buffer from the selected window if it is
2924 displayed there."
2925 (interactive)
2926 (let* ((buffer (window-normalize-buffer buffer-or-name)))
2927 ;; If `buffer-or-name' is not on the selected frame we unrecord it
2928 ;; although it's not "here" (call it a feature).
2929 (bury-buffer-internal buffer)
2930 ;; Handle case where `buffer-or-name' is nil and the current buffer
2931 ;; is shown in the selected window.
2932 (cond
2933 ((or buffer-or-name (not (eq buffer (window-buffer)))))
2934 ((window--delete nil t))
2936 ;; Switch to another buffer in window.
2937 (set-window-dedicated-p nil nil)
2938 (switch-to-prev-buffer nil 'bury)))
2940 ;; Always return nil.
2941 nil))
2943 (defun unbury-buffer ()
2944 "Switch to the last buffer in the buffer list."
2945 (interactive)
2946 (switch-to-buffer (last-buffer)))
2948 (defun next-buffer ()
2949 "In selected window switch to next buffer."
2950 (interactive)
2951 (if (window-minibuffer-p)
2952 (error "Cannot switch buffers in minibuffer window"))
2953 (switch-to-next-buffer))
2955 (defun previous-buffer ()
2956 "In selected window switch to previous buffer."
2957 (interactive)
2958 (if (window-minibuffer-p)
2959 (error "Cannot switch buffers in minibuffer window"))
2960 (switch-to-prev-buffer))
2962 (defun delete-windows-on (&optional buffer-or-name frame)
2963 "Delete all windows showing BUFFER-OR-NAME.
2964 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2965 and defaults to the current buffer.
2967 The following non-nil values of the optional argument FRAME
2968 have special meanings:
2970 - t means consider all windows on the selected frame only.
2972 - `visible' means consider all windows on all visible frames on
2973 the current terminal.
2975 - 0 (the number zero) means consider all windows on all visible
2976 and iconified frames on the current terminal.
2978 - A frame means consider all windows on that frame only.
2980 Any other value of FRAME means consider all windows on all
2981 frames.
2983 When a window showing BUFFER-OR-NAME is dedicated and the only
2984 window of its frame, that frame is deleted when there are other
2985 frames left."
2986 (interactive "BDelete windows on (buffer):\nP")
2987 (let ((buffer (window-normalize-buffer buffer-or-name))
2988 ;; Handle the "inverted" meaning of the FRAME argument wrt other
2989 ;; `window-list-1' based function.
2990 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
2991 (dolist (window (window-list-1 nil nil all-frames))
2992 (if (eq (window-buffer window) buffer)
2993 (let ((deletable (window-deletable-p window)))
2994 (cond
2995 ((and (eq deletable 'frame) (window-dedicated-p window))
2996 ;; Delete frame if and only if window is dedicated.
2997 (delete-frame (window-frame window)))
2998 ((eq deletable t)
2999 ;; Delete window.
3000 (delete-window window))
3002 ;; In window switch to previous buffer.
3003 (set-window-dedicated-p window nil)
3004 (switch-to-prev-buffer window 'bury))))
3005 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3006 (unrecord-window-buffer window buffer)))))
3008 (defun replace-buffer-in-windows (&optional buffer-or-name)
3009 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
3010 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3011 and defaults to the current buffer.
3013 When a window showing BUFFER-OR-NAME is dedicated, that window is
3014 deleted. If that window is the only window on its frame, the
3015 frame is deleted too when there are other frames left. If there
3016 are no other frames left, some other buffer is displayed in that
3017 window.
3019 This function removes the buffer denoted by BUFFER-OR-NAME from
3020 all window-local buffer lists."
3021 (interactive "bBuffer to replace: ")
3022 (let ((buffer (window-normalize-buffer buffer-or-name)))
3023 (dolist (window (window-list-1 nil nil t))
3024 (if (eq (window-buffer window) buffer)
3025 (unless (window--delete window t t)
3026 ;; Switch to another buffer in window.
3027 (set-window-dedicated-p window nil)
3028 (switch-to-prev-buffer window 'kill))
3029 ;; Unrecord BUFFER in WINDOW.
3030 (unrecord-window-buffer window buffer)))))
3032 (defun quit-window (&optional kill window)
3033 "Quit WINDOW and bury its buffer.
3034 WINDOW must be a live window and defaults to the selected one.
3035 With prefix argument KILL non-nil, kill the buffer instead of
3036 burying it.
3038 According to information stored in WINDOW's `quit-restore' window
3039 parameter either (1) delete WINDOW and its frame, (2) delete
3040 WINDOW, (3) restore the buffer previously displayed in WINDOW,
3041 or (4) make WINDOW display some other buffer than the present
3042 one. If non-nil, reset `quit-restore' parameter to nil."
3043 (interactive "P")
3044 (setq window (window-normalize-window window t))
3045 (let* ((buffer (window-buffer window))
3046 (quit-restore (window-parameter window 'quit-restore))
3047 (prev-buffer
3048 (let* ((prev-buffers (window-prev-buffers window))
3049 (prev-buffer (caar prev-buffers)))
3050 (and (or (not (eq prev-buffer buffer))
3051 (and (cdr prev-buffers)
3052 (not (eq (setq prev-buffer (cadr prev-buffers))
3053 buffer))))
3054 prev-buffer)))
3055 quad resize)
3056 (cond
3057 ((and (not prev-buffer)
3058 (memq (nth 1 quit-restore) '(window frame))
3059 (eq (nth 3 quit-restore) buffer)
3060 ;; Delete WINDOW if possible.
3061 (window--delete window nil kill))
3062 ;; If the previously selected window is still alive, select it.
3063 (when (window-live-p (nth 2 quit-restore))
3064 (select-window (nth 2 quit-restore))))
3065 ((and (listp (setq quad (nth 1 quit-restore)))
3066 (buffer-live-p (car quad))
3067 (eq (nth 3 quit-restore) buffer))
3068 ;; Show another buffer stored in quit-restore parameter.
3069 (setq resize (and (integerp (nth 3 quad))
3070 (/= (nth 3 quad) (window-total-size window))))
3071 (set-window-dedicated-p window nil)
3072 (when resize
3073 ;; Try to resize WINDOW to its old height but don't signal an
3074 ;; error.
3075 (condition-case nil
3076 (window-resize window (- (nth 3 quad) (window-total-size window)))
3077 (error nil)))
3078 ;; Restore WINDOW's previous buffer, start and point position.
3079 (set-window-buffer-start-and-point
3080 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
3081 ;; Unrecord WINDOW's buffer here (Bug#9937) to make sure it's not
3082 ;; re-recorded by `set-window-buffer'.
3083 (unrecord-window-buffer window buffer)
3084 ;; Reset the quit-restore parameter.
3085 (set-window-parameter window 'quit-restore nil)
3086 ;; Select old window.
3087 (when (window-live-p (nth 2 quit-restore))
3088 (select-window (nth 2 quit-restore))))
3090 ;; Show some other buffer in WINDOW and reset the quit-restore
3091 ;; parameter.
3092 (set-window-parameter window 'quit-restore nil)
3093 ;; Make sure that WINDOW is no more dedicated.
3094 (set-window-dedicated-p window nil)
3095 (switch-to-prev-buffer window 'bury-or-kill)))
3097 ;; Kill WINDOW's old-buffer if requested
3098 (if kill
3099 (kill-buffer buffer)
3100 (bury-buffer-internal buffer))))
3102 (defun quit-windows-on (&optional buffer-or-name kill frame)
3103 "Quit all windows showing BUFFER-OR-NAME.
3104 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3105 and defaults to the current buffer. Optional argument KILL
3106 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
3107 BUFFER-OR-NAME. Optional argument FRAME is handled as by
3108 `delete-windows-on'.
3110 This function calls `quit-window' on all candidate windows
3111 showing BUFFER-OR-NAME."
3112 (interactive "BQuit windows on (buffer):\nP")
3113 (let ((buffer (window-normalize-buffer buffer-or-name))
3114 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3115 ;; `window-list-1' based function.
3116 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
3117 (dolist (window (window-list-1 nil nil all-frames))
3118 (if (eq (window-buffer window) buffer)
3119 (quit-window kill window)
3120 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3121 (unrecord-window-buffer window buffer)))))
3123 ;;; Splitting windows.
3124 (defun window-split-min-size (&optional horizontal)
3125 "Return minimum height of any window when splitting windows.
3126 Optional argument HORIZONTAL non-nil means return minimum width."
3127 (if horizontal
3128 (max window-min-width window-safe-min-width)
3129 (max window-min-height window-safe-min-height)))
3131 (defun split-window (&optional window size side)
3132 "Make a new window adjacent to WINDOW.
3133 WINDOW can be any window and defaults to the selected one.
3134 Return the new window which is always a live window.
3136 Optional argument SIZE a positive number means make WINDOW SIZE
3137 lines or columns tall. If SIZE is negative, make the new window
3138 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
3139 absolute value can be less than `window-min-height' or
3140 `window-min-width'; so this command can make a new window as
3141 small as one line or two columns. SIZE defaults to half of
3142 WINDOW's size. Interactively, SIZE is the prefix argument.
3144 Optional third argument SIDE nil (or `below') specifies that the
3145 new window shall be located below WINDOW. SIDE `above' means the
3146 new window shall be located above WINDOW. In both cases SIZE
3147 specifies the new number of lines for WINDOW (or the new window
3148 if SIZE is negative) including space reserved for the mode and/or
3149 header line.
3151 SIDE t (or `right') specifies that the new window shall be
3152 located on the right side of WINDOW. SIDE `left' means the new
3153 window shall be located on the left of WINDOW. In both cases
3154 SIZE specifies the new number of columns for WINDOW (or the new
3155 window provided SIZE is negative) including space reserved for
3156 fringes and the scrollbar or a divider column. Any other non-nil
3157 value for SIDE is currently handled like t (or `right').
3159 If the variable `ignore-window-parameters' is non-nil or the
3160 `split-window' parameter of WINDOW equals t, do not process any
3161 parameters of WINDOW. Otherwise, if the `split-window' parameter
3162 of WINDOW specifies a function, call that function with all three
3163 arguments and return the value returned by that function.
3165 Otherwise, if WINDOW is part of an atomic window, \"split\" the
3166 root of that atomic window. The new window does not become a
3167 member of that atomic window.
3169 If WINDOW is live, properties of the new window like margins and
3170 scrollbars are inherited from WINDOW. If WINDOW is an internal
3171 window, these properties as well as the buffer displayed in the
3172 new window are inherited from the window selected on WINDOW's
3173 frame. The selected window is not changed by this function."
3174 (interactive "i")
3175 (setq window (window-normalize-window window))
3176 (let* ((side (cond
3177 ((not side) 'below)
3178 ((memq side '(below above right left)) side)
3179 (t 'right)))
3180 (horizontal (not (memq side '(nil below above))))
3181 (frame (window-frame window))
3182 (parent (window-parent window))
3183 (function (window-parameter window 'split-window))
3184 (window-side (window-parameter window 'window-side))
3185 ;; Rebind `window-combination-limit' since in some cases we may
3186 ;; have to override its value.
3187 (window-combination-limit window-combination-limit)
3188 atom-root)
3190 (window--check frame)
3191 (catch 'done
3192 (cond
3193 ;; Ignore window parameters if either `ignore-window-parameters'
3194 ;; is t or the `split-window' parameter equals t.
3195 ((or ignore-window-parameters (eq function t)))
3196 ((functionp function)
3197 ;; The `split-window' parameter specifies the function to call.
3198 ;; If that function is `ignore', do nothing.
3199 (throw 'done (funcall function window size side)))
3200 ;; If WINDOW is part of an atomic window, split the root window
3201 ;; of that atomic window instead.
3202 ((and (window-parameter window 'window-atom)
3203 (setq atom-root (window-atom-root window))
3204 (not (eq atom-root window)))
3205 (throw 'done (split-window atom-root size side))))
3207 (when (and window-side
3208 (or (not parent)
3209 (not (window-parameter parent 'window-side))))
3210 ;; WINDOW is a side root window. To make sure that a new parent
3211 ;; window gets created set `window-combination-limit' to t.
3212 (setq window-combination-limit t))
3214 (when (and window-combination-resize size (> size 0))
3215 ;; If `window-combination-resize' is non-nil and SIZE is a
3216 ;; non-negative integer, we cannot reasonably resize other
3217 ;; windows. Rather bind `window-combination-limit' to t to make
3218 ;; sure that subsequent window deletions are handled correctly.
3219 (setq window-combination-limit t))
3221 (let* ((parent-size
3222 ;; `parent-size' is the size of WINDOW's parent, provided
3223 ;; it has one.
3224 (when parent (window-total-size parent horizontal)))
3225 ;; `resize' non-nil means we are supposed to resize other
3226 ;; windows in WINDOW's combination.
3227 (resize
3228 (and window-combination-resize (not window-combination-limit)
3229 ;; Resize makes sense in iso-combinations only.
3230 (window-combined-p window horizontal)))
3231 ;; `old-size' is the current size of WINDOW.
3232 (old-size (window-total-size window horizontal))
3233 ;; `new-size' is the specified or calculated size of the
3234 ;; new window.
3235 (new-size
3236 (cond
3237 ((not size)
3238 (max (window-split-min-size horizontal)
3239 (if resize
3240 ;; When resizing try to give the new window the
3241 ;; average size of a window in its combination.
3242 (min (- parent-size
3243 (window-min-size parent horizontal))
3244 (/ parent-size
3245 (1+ (window-combinations
3246 parent horizontal))))
3247 ;; Else try to give the new window half the size
3248 ;; of WINDOW (plus an eventual odd line).
3249 (+ (/ old-size 2) (% old-size 2)))))
3250 ((>= size 0)
3251 ;; SIZE non-negative specifies the new size of WINDOW.
3253 ;; Note: Specifying a non-negative SIZE is practically
3254 ;; always done as workaround for making the new window
3255 ;; appear above or on the left of the new window (the
3256 ;; ispell window is a typical example of that). In all
3257 ;; these cases the SIDE argument should be set to 'above
3258 ;; or 'left in order to support the 'resize option.
3259 ;; Here we have to nest the windows instead, see above.
3260 (- old-size size))
3262 ;; SIZE negative specifies the size of the new window.
3263 (- size))))
3264 new-parent new-normal)
3266 ;; Check SIZE.
3267 (cond
3268 ((not size)
3269 (cond
3270 (resize
3271 ;; SIZE unspecified, resizing.
3272 (when (and (not (window-sizable-p parent (- new-size) horizontal))
3273 ;; Try again with minimum split size.
3274 (setq new-size
3275 (max new-size (window-split-min-size horizontal)))
3276 (not (window-sizable-p parent (- new-size) horizontal)))
3277 (error "Window %s too small for splitting" parent)))
3278 ((> (+ new-size (window-min-size window horizontal)) old-size)
3279 ;; SIZE unspecified, no resizing.
3280 (error "Window %s too small for splitting" window))))
3281 ((and (>= size 0)
3282 (or (>= size old-size)
3283 (< new-size (if horizontal
3284 window-safe-min-width
3285 window-safe-min-width))))
3286 ;; SIZE specified as new size of old window. If the new size
3287 ;; is larger than the old size or the size of the new window
3288 ;; would be less than the safe minimum, signal an error.
3289 (error "Window %s too small for splitting" window))
3290 (resize
3291 ;; SIZE specified, resizing.
3292 (unless (window-sizable-p parent (- new-size) horizontal)
3293 ;; If we cannot resize the parent give up.
3294 (error "Window %s too small for splitting" parent)))
3295 ((or (< new-size
3296 (if horizontal window-safe-min-width window-safe-min-height))
3297 (< (- old-size new-size)
3298 (if horizontal window-safe-min-width window-safe-min-height)))
3299 ;; SIZE specification violates minimum size restrictions.
3300 (error "Window %s too small for splitting" window)))
3302 (window--resize-reset frame horizontal)
3304 (setq new-parent
3305 ;; Make new-parent non-nil if we need a new parent window;
3306 ;; either because we want to nest or because WINDOW is not
3307 ;; iso-combined.
3308 (or window-combination-limit
3309 (not (window-combined-p window horizontal))))
3310 (setq new-normal
3311 ;; Make new-normal the normal size of the new window.
3312 (cond
3313 (size (/ (float new-size) (if new-parent old-size parent-size)))
3314 (new-parent 0.5)
3315 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
3316 (t (/ (window-normal-size window horizontal) 2.0))))
3318 (if resize
3319 ;; Try to get space from OLD's siblings. We could go "up" and
3320 ;; try getting additional space from surrounding windows but
3321 ;; we won't be able to return space to those windows when we
3322 ;; delete the one we create here. Hence we do not go up.
3323 (progn
3324 (window--resize-child-windows parent (- new-size) horizontal)
3325 (let* ((normal (- 1.0 new-normal))
3326 (sub (window-child parent)))
3327 (while sub
3328 (set-window-new-normal
3329 sub (* (window-normal-size sub horizontal) normal))
3330 (setq sub (window-right sub)))))
3331 ;; Get entire space from WINDOW.
3332 (set-window-new-total window (- old-size new-size))
3333 (window--resize-this-window window (- new-size) horizontal)
3334 (set-window-new-normal
3335 window (- (if new-parent 1.0 (window-normal-size window horizontal))
3336 new-normal)))
3338 (let* ((new (split-window-internal window new-size side new-normal)))
3339 ;; Inherit window-side parameters, if any.
3340 (when (and window-side new-parent)
3341 (set-window-parameter (window-parent new) 'window-side window-side)
3342 (set-window-parameter new 'window-side window-side))
3344 (run-window-configuration-change-hook frame)
3345 (window--check frame)
3346 ;; Always return the new window.
3347 new)))))
3349 ;; I think this should be the default; I think people will prefer it--rms.
3350 (defcustom split-window-keep-point t
3351 "If non-nil, \\[split-window-below] preserves point in the new window.
3352 If nil, adjust point in the two windows to minimize redisplay.
3353 This option applies only to `split-window-below' and functions
3354 that call it. The low-level `split-window' function always keeps
3355 the original point in both windows."
3356 :type 'boolean
3357 :group 'windows)
3359 (defun split-window-below (&optional size)
3360 "Split the selected window into two windows, one above the other.
3361 The selected window is above. The newly split-off window is
3362 below, and displays the same buffer. Return the new window.
3364 If optional argument SIZE is omitted or nil, both windows get the
3365 same height, or close to it. If SIZE is positive, the upper
3366 \(selected) window gets SIZE lines. If SIZE is negative, the
3367 lower (new) window gets -SIZE lines.
3369 If the variable `split-window-keep-point' is non-nil, both
3370 windows get the same value of point as the selected window.
3371 Otherwise, the window starts are chosen so as to minimize the
3372 amount of redisplay; this is convenient on slow terminals."
3373 (interactive "P")
3374 (let ((old-window (selected-window))
3375 (old-point (window-point-1))
3376 (size (and size (prefix-numeric-value size)))
3377 moved-by-window-height moved new-window bottom)
3378 (when (and size (< size 0) (< (- size) window-min-height))
3379 ;; `split-window' would not signal an error here.
3380 (error "Size of new window too small"))
3381 (setq new-window (split-window nil size))
3382 (unless split-window-keep-point
3383 (with-current-buffer (window-buffer)
3384 ;; Use `save-excursion' around vertical movements below
3385 ;; (Bug#10971). Note: When the selected window's buffer has a
3386 ;; header line, up to two lines of the buffer may not show up
3387 ;; in the resulting configuration.
3388 (save-excursion
3389 (goto-char (window-start))
3390 (setq moved (vertical-motion (window-height)))
3391 (set-window-start new-window (point))
3392 (when (> (point) (window-point new-window))
3393 (set-window-point new-window (point)))
3394 (when (= moved (window-height))
3395 (setq moved-by-window-height t)
3396 (vertical-motion -1))
3397 (setq bottom (point)))
3398 (and moved-by-window-height
3399 (<= bottom (point))
3400 (set-window-point-1 old-window (1- bottom)))
3401 (and moved-by-window-height
3402 (<= (window-start new-window) old-point)
3403 (set-window-point new-window old-point)
3404 (select-window new-window))))
3405 ;; Always copy quit-restore parameter in interactive use.
3406 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3407 (when quit-restore
3408 (set-window-parameter new-window 'quit-restore quit-restore)))
3409 new-window))
3411 (defalias 'split-window-vertically 'split-window-below)
3413 (defun split-window-right (&optional size)
3414 "Split the selected window into two side-by-side windows.
3415 The selected window is on the left. The newly split-off window
3416 is on the right, and displays the same buffer. Return the new
3417 window.
3419 If optional argument SIZE is omitted or nil, both windows get the
3420 same width, or close to it. If SIZE is positive, the left-hand
3421 \(selected) window gets SIZE columns. If SIZE is negative, the
3422 right-hand (new) window gets -SIZE columns. Here, SIZE includes
3423 the width of the window's scroll bar; if there are no scroll
3424 bars, it includes the width of the divider column to the window's
3425 right, if any."
3426 (interactive "P")
3427 (let ((old-window (selected-window))
3428 (size (and size (prefix-numeric-value size)))
3429 new-window)
3430 (when (and size (< size 0) (< (- size) window-min-width))
3431 ;; `split-window' would not signal an error here.
3432 (error "Size of new window too small"))
3433 (setq new-window (split-window nil size t))
3434 ;; Always copy quit-restore parameter in interactive use.
3435 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3436 (when quit-restore
3437 (set-window-parameter new-window 'quit-restore quit-restore)))
3438 new-window))
3440 (defalias 'split-window-horizontally 'split-window-right)
3442 ;;; Balancing windows.
3444 ;; The following routine uses the recycled code from an old version of
3445 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
3446 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
3447 ;; more readable (FWIW we'd need three loops - one to calculate the
3448 ;; minimum sizes per window, one to enlarge or shrink windows until the
3449 ;; new parent-size matches, and one where we shrink the largest/enlarge
3450 ;; the smallest window).
3451 (defun balance-windows-2 (window horizontal)
3452 "Subroutine of `balance-windows-1'.
3453 WINDOW must be a vertical combination (horizontal if HORIZONTAL
3454 is non-nil."
3455 (let* ((first (window-child window))
3456 (sub first)
3457 (number-of-children 0)
3458 (parent-size (window-new-total window))
3459 (total-sum parent-size)
3460 failed size sub-total sub-delta sub-amount rest)
3461 (while sub
3462 (setq number-of-children (1+ number-of-children))
3463 (when (window-size-fixed-p sub horizontal)
3464 (setq total-sum
3465 (- total-sum (window-total-size sub horizontal)))
3466 (set-window-new-normal sub 'ignore))
3467 (setq sub (window-right sub)))
3469 (setq failed t)
3470 (while (and failed (> number-of-children 0))
3471 (setq size (/ total-sum number-of-children))
3472 (setq failed nil)
3473 (setq sub first)
3474 (while (and sub (not failed))
3475 ;; Ignore child windows that should be ignored or are stuck.
3476 (unless (window--resize-child-windows-skip-p sub)
3477 (setq sub-total (window-total-size sub horizontal))
3478 (setq sub-delta (- size sub-total))
3479 (setq sub-amount
3480 (window-sizable sub sub-delta horizontal))
3481 ;; Register the new total size for this child window.
3482 (set-window-new-total sub (+ sub-total sub-amount))
3483 (unless (= sub-amount sub-delta)
3484 (setq total-sum (- total-sum sub-total sub-amount))
3485 (setq number-of-children (1- number-of-children))
3486 ;; We failed and need a new round.
3487 (setq failed t)
3488 (set-window-new-normal sub 'skip)))
3489 (setq sub (window-right sub))))
3491 (setq rest (% total-sum number-of-children))
3492 ;; Fix rounding by trying to enlarge non-stuck windows by one line
3493 ;; (column) until `rest' is zero.
3494 (setq sub first)
3495 (while (and sub (> rest 0))
3496 (unless (window--resize-child-windows-skip-p window)
3497 (set-window-new-total sub 1 t)
3498 (setq rest (1- rest)))
3499 (setq sub (window-right sub)))
3501 ;; Fix rounding by trying to enlarge stuck windows by one line
3502 ;; (column) until `rest' equals zero.
3503 (setq sub first)
3504 (while (and sub (> rest 0))
3505 (unless (eq (window-new-normal sub) 'ignore)
3506 (set-window-new-total sub 1 t)
3507 (setq rest (1- rest)))
3508 (setq sub (window-right sub)))
3510 (setq sub first)
3511 (while sub
3512 ;; Record new normal sizes.
3513 (set-window-new-normal
3514 sub (/ (if (eq (window-new-normal sub) 'ignore)
3515 (window-total-size sub horizontal)
3516 (window-new-total sub))
3517 (float parent-size)))
3518 ;; Recursively balance each window's child windows.
3519 (balance-windows-1 sub horizontal)
3520 (setq sub (window-right sub)))))
3522 (defun balance-windows-1 (window &optional horizontal)
3523 "Subroutine of `balance-windows'."
3524 (if (window-child window)
3525 (let ((sub (window-child window)))
3526 (if (window-combined-p sub horizontal)
3527 (balance-windows-2 window horizontal)
3528 (let ((size (window-new-total window)))
3529 (while sub
3530 (set-window-new-total sub size)
3531 (balance-windows-1 sub horizontal)
3532 (setq sub (window-right sub))))))))
3534 (defun balance-windows (&optional window-or-frame)
3535 "Balance the sizes of windows of WINDOW-OR-FRAME.
3536 WINDOW-OR-FRAME is optional and defaults to the selected frame.
3537 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
3538 windows of that frame. If WINDOW-OR-FRAME denotes a window,
3539 recursively balance the sizes of all child windows of that
3540 window."
3541 (interactive)
3542 (let* ((window
3543 (cond
3544 ((or (not window-or-frame)
3545 (frame-live-p window-or-frame))
3546 (frame-root-window window-or-frame))
3547 ((or (window-live-p window-or-frame)
3548 (window-child window-or-frame))
3549 window-or-frame)
3551 (error "Not a window or frame %s" window-or-frame))))
3552 (frame (window-frame window)))
3553 ;; Balance vertically.
3554 (window--resize-reset (window-frame window))
3555 (balance-windows-1 window)
3556 (window-resize-apply frame)
3557 ;; Balance horizontally.
3558 (window--resize-reset (window-frame window) t)
3559 (balance-windows-1 window t)
3560 (window-resize-apply frame t)))
3562 (defun window-fixed-size-p (&optional window direction)
3563 "Return t if WINDOW cannot be resized in DIRECTION.
3564 WINDOW defaults to the selected window. DIRECTION can be
3565 nil (i.e. any), `height' or `width'."
3566 (with-current-buffer (window-buffer window)
3567 (when (and (boundp 'window-size-fixed) window-size-fixed)
3568 (not (and direction
3569 (member (cons direction window-size-fixed)
3570 '((height . width) (width . height))))))))
3572 ;;; A different solution to balance-windows.
3573 (defvar window-area-factor 1
3574 "Factor by which the window area should be over-estimated.
3575 This is used by `balance-windows-area'.
3576 Changing this globally has no effect.")
3577 (make-variable-buffer-local 'window-area-factor)
3579 (defun balance-windows-area-adjust (window delta horizontal)
3580 "Wrapper around `window-resize' with error checking.
3581 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
3582 ;; `window-resize' may fail if delta is too large.
3583 (while (>= (abs delta) 1)
3584 (condition-case nil
3585 (progn
3586 (window-resize window delta horizontal)
3587 (setq delta 0))
3588 (error
3589 ;;(message "adjust: %s" (error-message-string err))
3590 (setq delta (/ delta 2))))))
3592 (defun balance-windows-area ()
3593 "Make all visible windows the same area (approximately).
3594 See also `window-area-factor' to change the relative size of
3595 specific buffers."
3596 (interactive)
3597 (let* ((unchanged 0) (carry 0) (round 0)
3598 ;; Remove fixed-size windows.
3599 (wins (delq nil (mapcar (lambda (win)
3600 (if (not (window-fixed-size-p win)) win))
3601 (window-list nil 'nomini))))
3602 (changelog nil)
3603 next)
3604 ;; Resizing a window changes the size of surrounding windows in complex
3605 ;; ways, so it's difficult to balance them all. The introduction of
3606 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
3607 ;; very difficult to do. `balance-window' above takes an off-line
3608 ;; approach: get the whole window tree, then balance it, then try to
3609 ;; adjust the windows so they fit the result.
3610 ;; Here, instead, we take a "local optimization" approach, where we just
3611 ;; go through all the windows several times until nothing needs to be
3612 ;; changed. The main problem with this approach is that it's difficult
3613 ;; to make sure it terminates, so we use some heuristic to try and break
3614 ;; off infinite loops.
3615 ;; After a round without any change, we allow a second, to give a chance
3616 ;; to the carry to propagate a minor imbalance from the end back to
3617 ;; the beginning.
3618 (while (< unchanged 2)
3619 ;; (message "New round")
3620 (setq unchanged (1+ unchanged) round (1+ round))
3621 (dolist (win wins)
3622 (setq next win)
3623 (while (progn (setq next (next-window next))
3624 (window-fixed-size-p next)))
3625 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
3626 (let* ((horiz
3627 (< (car (window-edges win)) (car (window-edges next))))
3628 (areadiff (/ (- (* (window-height next) (window-width next)
3629 (buffer-local-value 'window-area-factor
3630 (window-buffer next)))
3631 (* (window-height win) (window-width win)
3632 (buffer-local-value 'window-area-factor
3633 (window-buffer win))))
3634 (max (buffer-local-value 'window-area-factor
3635 (window-buffer win))
3636 (buffer-local-value 'window-area-factor
3637 (window-buffer next)))))
3638 (edgesize (if horiz
3639 (+ (window-height win) (window-height next))
3640 (+ (window-width win) (window-width next))))
3641 (diff (/ areadiff edgesize)))
3642 (when (zerop diff)
3643 ;; Maybe diff is actually closer to 1 than to 0.
3644 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
3645 (when (and (zerop diff) (not (zerop areadiff)))
3646 (setq diff (/ (+ areadiff carry) edgesize))
3647 ;; Change things smoothly.
3648 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
3649 (if (zerop diff)
3650 ;; Make sure negligible differences don't accumulate to
3651 ;; become significant.
3652 (setq carry (+ carry areadiff))
3653 ;; This used `adjust-window-trailing-edge' before and uses
3654 ;; `window-resize' now. Error wrapping is still needed.
3655 (balance-windows-area-adjust win diff horiz)
3656 ;; (sit-for 0.5)
3657 (let ((change (cons win (window-edges win))))
3658 ;; If the same change has been seen already for this window,
3659 ;; we're most likely in an endless loop, so don't count it as
3660 ;; a change.
3661 (unless (member change changelog)
3662 (push change changelog)
3663 (setq unchanged 0 carry 0)))))))
3664 ;; We've now basically balanced all the windows.
3665 ;; But there may be some minor off-by-one imbalance left over,
3666 ;; so let's do some fine tuning.
3667 ;; (bw-finetune wins)
3668 ;; (message "Done in %d rounds" round)
3671 ;;; Window states, how to get them and how to put them in a window.
3672 (defun window--state-get-1 (window &optional writable)
3673 "Helper function for `window-state-get'."
3674 (let* ((type
3675 (cond
3676 ((window-top-child window) 'vc)
3677 ((window-left-child window) 'hc)
3678 (t 'leaf)))
3679 (buffer (window-buffer window))
3680 (selected (eq window (selected-window)))
3681 (head
3682 `(,type
3683 ,@(unless (window-next-sibling window) `((last . t)))
3684 (total-height . ,(window-total-size window))
3685 (total-width . ,(window-total-size window t))
3686 (normal-height . ,(window-normal-size window))
3687 (normal-width . ,(window-normal-size window t))
3688 (combination-limit . ,(window-combination-limit window))
3689 ,@(let ((parameters (window-parameters window))
3690 list)
3691 ;; Make copies of those window parameters whose
3692 ;; persistence property is `writable' if WRITABLE is
3693 ;; non-nil and non-nil if WRITABLE is nil.
3694 (dolist (par parameters)
3695 (let ((pers (cdr (assq (car par)
3696 window-persistent-parameters))))
3697 (when (and pers (or (not writable) (eq pers 'writable)))
3698 (setq list (cons (cons (car par) (cdr par)) list)))))
3699 ;; Add `clone-of' parameter if necessary.
3700 (let ((pers (cdr (assq 'clone-of
3701 window-persistent-parameters))))
3702 (when (and pers (or (not writable) (eq pers 'writable))
3703 (not (assq 'clone-of list)))
3704 (setq list (cons (cons 'clone-of window) list))))
3705 (when list
3706 `((parameters . ,list))))
3707 ,@(when buffer
3708 ;; All buffer related things go in here.
3709 (let ((point (window-point-1 window))
3710 (start (window-start window)))
3711 `((buffer
3712 ,(buffer-name buffer)
3713 (selected . ,selected)
3714 (hscroll . ,(window-hscroll window))
3715 (fringes . ,(window-fringes window))
3716 (margins . ,(window-margins window))
3717 (scroll-bars . ,(window-scroll-bars window))
3718 (vscroll . ,(window-vscroll window))
3719 (dedicated . ,(window-dedicated-p window))
3720 (point . ,(if writable point
3721 (copy-marker point
3722 (buffer-local-value
3723 'window-point-insertion-type
3724 buffer))))
3725 (start . ,(if writable start (copy-marker start)))))))))
3726 (tail
3727 (when (memq type '(vc hc))
3728 (let (list)
3729 (setq window (window-child window))
3730 (while window
3731 (setq list (cons (window--state-get-1 window writable) list))
3732 (setq window (window-right window)))
3733 (nreverse list)))))
3734 (append head tail)))
3736 (defun window-state-get (&optional window writable)
3737 "Return state of WINDOW as a Lisp object.
3738 WINDOW can be any window and defaults to the root window of the
3739 selected frame.
3741 Optional argument WRITABLE non-nil means do not use markers for
3742 sampling `window-point' and `window-start'. Together, WRITABLE
3743 and the variable `window-persistent-parameters' specify which
3744 window parameters are saved by this function. WRITABLE should be
3745 non-nil when the return value shall be written to a file and read
3746 back in another session. Otherwise, an application may run into
3747 an `invalid-read-syntax' error while attempting to read back the
3748 value from file.
3750 The return value can be used as argument for `window-state-put'
3751 to put the state recorded here into an arbitrary window. The
3752 value can be also stored on disk and read back in a new session."
3753 (setq window
3754 (if window
3755 (if (window-valid-p window)
3756 window
3757 (error "%s is not a live or internal window" window))
3758 (frame-root-window)))
3759 ;; The return value is a cons whose car specifies some constraints on
3760 ;; the size of WINDOW. The cdr lists the states of the child windows
3761 ;; of WINDOW.
3762 (cons
3763 ;; Frame related things would go into a function, say `frame-state',
3764 ;; calling `window-state-get' to insert the frame's root window.
3765 `((min-height . ,(window-min-size window))
3766 (min-width . ,(window-min-size window t))
3767 (min-height-ignore . ,(window-min-size window nil t))
3768 (min-width-ignore . ,(window-min-size window t t))
3769 (min-height-safe . ,(window-min-size window nil 'safe))
3770 (min-width-safe . ,(window-min-size window t 'safe)))
3771 (window--state-get-1 window writable)))
3773 (defvar window-state-put-list nil
3774 "Helper variable for `window-state-put'.")
3776 (defun window--state-put-1 (state &optional window ignore totals)
3777 "Helper function for `window-state-put'."
3778 (let ((type (car state)))
3779 (setq state (cdr state))
3780 (cond
3781 ((eq type 'leaf)
3782 ;; For a leaf window just add unprocessed entries to
3783 ;; `window-state-put-list'.
3784 (push (cons window state) window-state-put-list))
3785 ((memq type '(vc hc))
3786 (let* ((horizontal (eq type 'hc))
3787 (total (window-total-size window horizontal))
3788 (first t)
3789 size new)
3790 (dolist (item state)
3791 ;; Find the next child window. WINDOW always points to the
3792 ;; real window that we want to fill with what we find here.
3793 (when (memq (car item) '(leaf vc hc))
3794 (if (assq 'last item)
3795 ;; The last child window. Below `window--state-put-1'
3796 ;; will put into it whatever ITEM has in store.
3797 (setq new nil)
3798 ;; Not the last child window, prepare for splitting
3799 ;; WINDOW. SIZE is the new (and final) size of the old
3800 ;; window.
3801 (setq size
3802 (if totals
3803 ;; Use total size.
3804 (cdr (assq (if horizontal 'total-width 'total-height) item))
3805 ;; Use normalized size and round.
3806 (round (* total
3807 (cdr (assq
3808 (if horizontal 'normal-width 'normal-height)
3809 item))))))
3811 ;; Use safe sizes, we try to resize later.
3812 (setq size (max size (if horizontal
3813 window-safe-min-height
3814 window-safe-min-width)))
3816 (if (window-sizable-p window (- size) horizontal 'safe)
3817 (let* ((window-combination-limit
3818 (assq 'combination-limit item)))
3819 ;; We must inherit the combination limit, otherwise
3820 ;; we might mess up handling of atomic and side
3821 ;; window.
3822 (setq new (split-window window size horizontal)))
3823 ;; Give up if we can't resize window down to safe sizes.
3824 (error "Cannot resize window %s" window))
3826 (when first
3827 (setq first nil)
3828 ;; When creating the first child window add for parent
3829 ;; unprocessed entries to `window-state-put-list'.
3830 (setq window-state-put-list
3831 (cons (cons (window-parent window) state)
3832 window-state-put-list))))
3834 ;; Now process the current window (either the one we've just
3835 ;; split or the last child of its parent).
3836 (window--state-put-1 item window ignore totals)
3837 ;; Continue with the last window split off.
3838 (setq window new))))))))
3840 (defun window--state-put-2 (ignore)
3841 "Helper function for `window-state-put'."
3842 (dolist (item window-state-put-list)
3843 (let ((window (car item))
3844 (combination-limit (cdr (assq 'combination-limit item)))
3845 (parameters (cdr (assq 'parameters item)))
3846 (state (cdr (assq 'buffer item))))
3847 (when combination-limit
3848 (set-window-combination-limit window combination-limit))
3849 ;; Reset window's parameters and assign saved ones (we might want
3850 ;; a `remove-window-parameters' function here).
3851 (dolist (parameter (window-parameters window))
3852 (set-window-parameter window (car parameter) nil))
3853 (when parameters
3854 (dolist (parameter parameters)
3855 (set-window-parameter window (car parameter) (cdr parameter))))
3856 ;; Process buffer related state.
3857 (when state
3858 ;; We don't want to raise an error here so we create a buffer if
3859 ;; there's none.
3860 (set-window-buffer window (get-buffer-create (car state)))
3861 (with-current-buffer (window-buffer window)
3862 (set-window-hscroll window (cdr (assq 'hscroll state)))
3863 (apply 'set-window-fringes
3864 (cons window (cdr (assq 'fringes state))))
3865 (let ((margins (cdr (assq 'margins state))))
3866 (set-window-margins window (car margins) (cdr margins)))
3867 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
3868 (set-window-scroll-bars
3869 window (car scroll-bars) (nth 2 scroll-bars) (nth 3 scroll-bars)))
3870 (set-window-vscroll window (cdr (assq 'vscroll state)))
3871 ;; Adjust vertically.
3872 (if (memq window-size-fixed '(t height))
3873 ;; A fixed height window, try to restore the original size.
3874 (let ((delta (- (cdr (assq 'total-height item))
3875 (window-total-height window)))
3876 window-size-fixed)
3877 (when (window--resizable-p window delta)
3878 (window-resize window delta)))
3879 ;; Else check whether the window is not high enough.
3880 (let* ((min-size (window-min-size window nil ignore))
3881 (delta (- min-size (window-total-size window))))
3882 (when (and (> delta 0)
3883 (window--resizable-p window delta nil ignore))
3884 (window-resize window delta nil ignore))))
3885 ;; Adjust horizontally.
3886 (if (memq window-size-fixed '(t width))
3887 ;; A fixed width window, try to restore the original size.
3888 (let ((delta (- (cdr (assq 'total-width item))
3889 (window-total-width window)))
3890 window-size-fixed)
3891 (when (window--resizable-p window delta)
3892 (window-resize window delta)))
3893 ;; Else check whether the window is not wide enough.
3894 (let* ((min-size (window-min-size window t ignore))
3895 (delta (- min-size (window-total-size window t))))
3896 (when (and (> delta 0)
3897 (window--resizable-p window delta t ignore))
3898 (window-resize window delta t ignore))))
3899 ;; Set dedicated status.
3900 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
3901 ;; Install positions (maybe we should do this after all windows
3902 ;; have been created and sized).
3903 (ignore-errors
3904 (set-window-start window (cdr (assq 'start state)))
3905 (set-window-point window (cdr (assq 'point state))))
3906 ;; Select window if it's the selected one.
3907 (when (cdr (assq 'selected state))
3908 (select-window window)))))))
3910 (defun window-state-put (state &optional window ignore)
3911 "Put window state STATE into WINDOW.
3912 STATE should be the state of a window returned by an earlier
3913 invocation of `window-state-get'. Optional argument WINDOW must
3914 specify a live window and defaults to the selected one.
3916 Optional argument IGNORE non-nil means ignore minimum window
3917 sizes and fixed size restrictions. IGNORE equal `safe' means
3918 windows can get as small as `window-safe-min-height' and
3919 `window-safe-min-width'."
3920 (setq window (window-normalize-window window t))
3921 (let* ((frame (window-frame window))
3922 (head (car state))
3923 ;; We check here (1) whether the total sizes of root window of
3924 ;; STATE and that of WINDOW are equal so we can avoid
3925 ;; calculating new sizes, and (2) if we do have to resize
3926 ;; whether we can do so without violating size restrictions.
3927 (totals
3928 (and (= (window-total-size window)
3929 (cdr (assq 'total-height state)))
3930 (= (window-total-size window t)
3931 (cdr (assq 'total-width state)))))
3932 (min-height (cdr (assq 'min-height head)))
3933 (min-width (cdr (assq 'min-width head))))
3934 (if (and (not totals)
3935 (or (> min-height (window-total-size window))
3936 (> min-width (window-total-size window t)))
3937 (or (not ignore)
3938 (and (setq min-height
3939 (cdr (assq 'min-height-ignore head)))
3940 (setq min-width
3941 (cdr (assq 'min-width-ignore head)))
3942 (or (> min-height (window-total-size window))
3943 (> min-width (window-total-size window t)))
3944 (or (not (eq ignore 'safe))
3945 (and (setq min-height
3946 (cdr (assq 'min-height-safe head)))
3947 (setq min-width
3948 (cdr (assq 'min-width-safe head)))
3949 (or (> min-height
3950 (window-total-size window))
3951 (> min-width
3952 (window-total-size window t))))))))
3953 ;; The check above might not catch all errors due to rounding
3954 ;; issues - so IGNORE equal 'safe might not always produce the
3955 ;; minimum possible state. But such configurations hardly make
3956 ;; sense anyway.
3957 (error "Window %s too small to accommodate state" window)
3958 (setq state (cdr state))
3959 (setq window-state-put-list nil)
3960 ;; Work on the windows of a temporary buffer to make sure that
3961 ;; splitting proceeds regardless of any buffer local values of
3962 ;; `window-size-fixed'. Release that buffer after the buffers of
3963 ;; all live windows have been set by `window--state-put-2'.
3964 (with-temp-buffer
3965 (set-window-buffer window (current-buffer))
3966 (window--state-put-1 state window nil totals)
3967 (window--state-put-2 ignore))
3968 (window--check frame))))
3970 (defun display-buffer-record-window (type window buffer)
3971 "Record information for window used by `display-buffer'.
3972 TYPE specifies the type of the calling operation and must be one
3973 of the symbols 'reuse (when WINDOW existed already and was
3974 reused for displaying BUFFER), 'window (when WINDOW was created
3975 on an already existing frame), or 'frame (when WINDOW was
3976 created on a new frame). WINDOW is the window used for or created
3977 by the `display-buffer' routines. BUFFER is the buffer that
3978 shall be displayed.
3980 This function installs or updates the quit-restore parameter of
3981 WINDOW. The quit-restore parameter is a list of four elements:
3982 The first element is one of the symbols 'window, 'frame, 'same or
3983 'other. The second element is either one of the symbols 'window
3984 or 'frame or a list whose elements are the buffer previously
3985 shown in the window, that buffer's window start and window point,
3986 and the window's height. The third element is the window
3987 selected at the time the parameter was created. The fourth
3988 element is BUFFER."
3989 (cond
3990 ((eq type 'reuse)
3991 (if (eq (window-buffer window) buffer)
3992 ;; WINDOW shows BUFFER already.
3993 (when (consp (window-parameter window 'quit-restore))
3994 ;; If WINDOW has a quit-restore parameter, reset its car.
3995 (setcar (window-parameter window 'quit-restore) 'same))
3996 ;; WINDOW shows another buffer.
3997 (set-window-parameter
3998 window 'quit-restore
3999 (list 'other
4000 ;; A quadruple of WINDOW's buffer, start, point and height.
4001 (list (window-buffer window) (window-start window)
4002 (window-point-1 window) (window-total-size window))
4003 (selected-window) buffer))))
4004 ((eq type 'window)
4005 ;; WINDOW has been created on an existing frame.
4006 (set-window-parameter
4007 window 'quit-restore
4008 (list 'window 'window (selected-window) buffer)))
4009 ((eq type 'frame)
4010 ;; WINDOW has been created on a new frame.
4011 (set-window-parameter
4012 window 'quit-restore
4013 (list 'frame 'frame (selected-window) buffer)))))
4015 (defcustom display-buffer-function nil
4016 "If non-nil, function to call to handle `display-buffer'.
4017 It will receive two args, the buffer and a flag which if non-nil
4018 means that the currently selected window is not acceptable. It
4019 should choose or create a window, display the specified buffer in
4020 it, and return the window.
4022 The specified function should call `display-buffer-record-window'
4023 with corresponding arguments to set up the quit-restore parameter
4024 of the window used."
4025 :type '(choice
4026 (const nil)
4027 (function :tag "function"))
4028 :group 'windows)
4030 (defcustom pop-up-frame-alist nil
4031 "Alist of parameters for automatically generated new frames.
4032 You can set this in your init file; for example,
4034 (setq pop-up-frame-alist '((width . 80) (height . 20)))
4036 If non-nil, the value you specify here is used by the default
4037 `pop-up-frame-function' for the creation of new frames.
4039 Since `pop-up-frame-function' is used by `display-buffer' for
4040 making new frames, any value specified here by default affects
4041 the automatic generation of new frames via `display-buffer' and
4042 all functions based on it. The behavior of `make-frame' is not
4043 affected by this variable."
4044 :type '(repeat (cons :format "%v"
4045 (symbol :tag "Parameter")
4046 (sexp :tag "Value")))
4047 :group 'frames)
4049 (defcustom pop-up-frame-function
4050 (lambda () (make-frame pop-up-frame-alist))
4051 "Function used by `display-buffer' for creating a new frame.
4052 This function is called with no arguments and should return a new
4053 frame. The default value calls `make-frame' with the argument
4054 `pop-up-frame-alist'."
4055 :type 'function
4056 :group 'frames)
4058 (defcustom special-display-buffer-names nil
4059 "List of names of buffers that should be displayed specially.
4060 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4061 its name is in this list, displays the buffer in a way specified
4062 by `special-display-function'. `special-display-popup-frame'
4063 \(the default for `special-display-function') usually displays
4064 the buffer in a separate frame made with the parameters specified
4065 by `special-display-frame-alist'. If `special-display-function'
4066 has been set to some other function, that function is called with
4067 the buffer as first, and nil as second argument.
4069 Alternatively, an element of this list can be specified as
4070 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
4071 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4072 `special-display-popup-frame' will interpret such pairs as frame
4073 parameters when it creates a special frame, overriding the
4074 corresponding values from `special-display-frame-alist'.
4076 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4077 `special-display-popup-frame' displays that buffer in the
4078 selected window. If FRAME-PARAMETERS contains (same-frame . t),
4079 it displays that buffer in a window on the selected frame.
4081 If `special-display-function' specifies some other function than
4082 `special-display-popup-frame', that function is called with the
4083 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
4084 argument.
4086 Finally, an element of this list can be also specified as
4087 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
4088 `special-display-popup-frame' will call FUNCTION with the buffer
4089 named BUFFER-NAME as first argument, and OTHER-ARGS as the
4090 second.
4092 Any alternative function specified here is responsible for
4093 setting up the quit-restore parameter of the window used.
4095 If this variable appears \"not to work\", because you added a
4096 name to it but the corresponding buffer is displayed in the
4097 selected window, look at the values of `same-window-buffer-names'
4098 and `same-window-regexps'. Those variables take precedence over
4099 this one.
4101 See also `special-display-regexps'."
4102 :type '(repeat
4103 (choice :tag "Buffer"
4104 :value ""
4105 (string :format "%v")
4106 (cons :tag "With parameters"
4107 :format "%v"
4108 :value ("" . nil)
4109 (string :format "%v")
4110 (repeat :tag "Parameters"
4111 (cons :format "%v"
4112 (symbol :tag "Parameter")
4113 (sexp :tag "Value"))))
4114 (list :tag "With function"
4115 :format "%v"
4116 :value ("" . nil)
4117 (string :format "%v")
4118 (function :tag "Function")
4119 (repeat :tag "Arguments" (sexp)))))
4120 :group 'windows
4121 :group 'frames)
4123 ;;;###autoload
4124 (put 'special-display-buffer-names 'risky-local-variable t)
4126 (defcustom special-display-regexps nil
4127 "List of regexps saying which buffers should be displayed specially.
4128 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4129 any regexp in this list matches its name, displays it specially
4130 using `special-display-function'. `special-display-popup-frame'
4131 \(the default for `special-display-function') usually displays
4132 the buffer in a separate frame made with the parameters specified
4133 by `special-display-frame-alist'. If `special-display-function'
4134 has been set to some other function, that function is called with
4135 the buffer as first, and nil as second argument.
4137 Alternatively, an element of this list can be specified as
4138 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
4139 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4140 `special-display-popup-frame' will then interpret these pairs as
4141 frame parameters when creating a special frame for a buffer whose
4142 name matches REGEXP, overriding the corresponding values from
4143 `special-display-frame-alist'.
4145 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4146 `special-display-popup-frame' displays buffers matching REGEXP in
4147 the selected window. (same-frame . t) in FRAME-PARAMETERS means
4148 to display such buffers in a window on the selected frame.
4150 If `special-display-function' specifies some other function than
4151 `special-display-popup-frame', that function is called with the
4152 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
4153 as second argument.
4155 Finally, an element of this list can be also specified as
4156 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
4157 will then call FUNCTION with the buffer whose name matched
4158 REGEXP as first, and OTHER-ARGS as second argument.
4160 Any alternative function specified here is responsible for
4161 setting up the quit-restore parameter of the window used.
4163 If this variable appears \"not to work\", because you added a
4164 name to it but the corresponding buffer is displayed in the
4165 selected window, look at the values of `same-window-buffer-names'
4166 and `same-window-regexps'. Those variables take precedence over
4167 this one.
4169 See also `special-display-buffer-names'."
4170 :type '(repeat
4171 (choice :tag "Buffer"
4172 :value ""
4173 (regexp :format "%v")
4174 (cons :tag "With parameters"
4175 :format "%v"
4176 :value ("" . nil)
4177 (regexp :format "%v")
4178 (repeat :tag "Parameters"
4179 (cons :format "%v"
4180 (symbol :tag "Parameter")
4181 (sexp :tag "Value"))))
4182 (list :tag "With function"
4183 :format "%v"
4184 :value ("" . nil)
4185 (regexp :format "%v")
4186 (function :tag "Function")
4187 (repeat :tag "Arguments" (sexp)))))
4188 :group 'windows
4189 :group 'frames)
4191 (defun special-display-p (buffer-name)
4192 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
4193 More precisely, return t if `special-display-buffer-names' or
4194 `special-display-regexps' contain a string entry equaling or
4195 matching BUFFER-NAME. If `special-display-buffer-names' or
4196 `special-display-regexps' contain a list entry whose car equals
4197 or matches BUFFER-NAME, the return value is the cdr of that
4198 entry."
4199 (let (tmp)
4200 (cond
4201 ((member buffer-name special-display-buffer-names)
4203 ((setq tmp (assoc buffer-name special-display-buffer-names))
4204 (cdr tmp))
4205 ((catch 'found
4206 (dolist (regexp special-display-regexps)
4207 (cond
4208 ((stringp regexp)
4209 (when (string-match-p regexp buffer-name)
4210 (throw 'found t)))
4211 ((and (consp regexp) (stringp (car regexp))
4212 (string-match-p (car regexp) buffer-name))
4213 (throw 'found (cdr regexp))))))))))
4215 (defcustom special-display-frame-alist
4216 '((height . 14) (width . 80) (unsplittable . t))
4217 "Alist of parameters for special frames.
4218 Special frames are used for buffers whose names are listed in
4219 `special-display-buffer-names' and for buffers whose names match
4220 one of the regular expressions in `special-display-regexps'.
4222 This variable can be set in your init file, like this:
4224 (setq special-display-frame-alist '((width . 80) (height . 20)))
4226 These supersede the values given in `default-frame-alist'."
4227 :type '(repeat (cons :format "%v"
4228 (symbol :tag "Parameter")
4229 (sexp :tag "Value")))
4230 :group 'frames)
4232 (defun special-display-popup-frame (buffer &optional args)
4233 "Pop up a frame displaying BUFFER and return its window.
4234 If BUFFER is already displayed in a visible or iconified frame,
4235 raise that frame. Otherwise, display BUFFER in a new frame.
4237 Optional argument ARGS is a list specifying additional
4238 information.
4240 If ARGS is an alist, use it as a list of frame parameters. If
4241 these parameters contain (same-window . t), display BUFFER in
4242 the selected window. If they contain (same-frame . t), display
4243 BUFFER in a window of the selected frame.
4245 If ARGS is a list whose car is a symbol, use (car ARGS) as a
4246 function to do the work. Pass it BUFFER as first argument,
4247 and (cdr ARGS) as second."
4248 (if (and args (symbolp (car args)))
4249 (apply (car args) buffer (cdr args))
4250 (let ((window (get-buffer-window buffer 0)))
4252 ;; If we have a window already, make it visible.
4253 (when window
4254 (let ((frame (window-frame window)))
4255 (make-frame-visible frame)
4256 (raise-frame frame)
4257 (display-buffer-record-window 'reuse window buffer)
4258 window))
4259 ;; Reuse the current window if the user requested it.
4260 (when (cdr (assq 'same-window args))
4261 (condition-case nil
4262 (progn (switch-to-buffer buffer nil t) (selected-window))
4263 (error nil)))
4264 ;; Stay on the same frame if requested.
4265 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
4266 (let* ((pop-up-windows t)
4267 pop-up-frames
4268 special-display-buffer-names special-display-regexps)
4269 (display-buffer buffer)))
4270 ;; If no window yet, make one in a new frame.
4271 (let* ((frame
4272 (with-current-buffer buffer
4273 (make-frame (append args special-display-frame-alist))))
4274 (window (frame-selected-window frame)))
4275 (display-buffer-record-window 'frame window buffer)
4276 (set-window-dedicated-p window t)
4277 window)))))
4279 (defcustom special-display-function 'special-display-popup-frame
4280 "Function to call for displaying special buffers.
4281 This function is called with two arguments - the buffer and,
4282 optionally, a list - and should return a window displaying that
4283 buffer. The default value usually makes a separate frame for the
4284 buffer using `special-display-frame-alist' to specify the frame
4285 parameters. See the definition of `special-display-popup-frame'
4286 for how to specify such a function.
4288 A buffer is special when its name is either listed in
4289 `special-display-buffer-names' or matches a regexp in
4290 `special-display-regexps'.
4292 The specified function should call `display-buffer-record-window'
4293 with corresponding arguments to set up the quit-restore parameter
4294 of the window used."
4295 :type 'function
4296 :group 'frames)
4298 (defcustom same-window-buffer-names nil
4299 "List of names of buffers that should appear in the \"same\" window.
4300 `display-buffer' and `pop-to-buffer' show a buffer whose name is
4301 on this list in the selected rather than some other window.
4303 An element of this list can be a cons cell instead of just a
4304 string. In that case, the cell's car must be a string specifying
4305 the buffer name. This is for compatibility with
4306 `special-display-buffer-names'; the cdr of the cons cell is
4307 ignored.
4309 See also `same-window-regexps'."
4310 :type '(repeat (string :format "%v"))
4311 :group 'windows)
4313 (defcustom same-window-regexps nil
4314 "List of regexps saying which buffers should appear in the \"same\" window.
4315 `display-buffer' and `pop-to-buffer' show a buffer whose name
4316 matches a regexp on this list in the selected rather than some
4317 other window.
4319 An element of this list can be a cons cell instead of just a
4320 string. In that case, the cell's car must be a regexp matching
4321 the buffer name. This is for compatibility with
4322 `special-display-regexps'; the cdr of the cons cell is ignored.
4324 See also `same-window-buffer-names'."
4325 :type '(repeat (regexp :format "%v"))
4326 :group 'windows)
4328 (defun same-window-p (buffer-name)
4329 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
4330 This function returns non-nil if `display-buffer' or
4331 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
4332 selected rather than (as usual) some other window. See
4333 `same-window-buffer-names' and `same-window-regexps'."
4334 (cond
4335 ((not (stringp buffer-name)))
4336 ;; The elements of `same-window-buffer-names' can be buffer
4337 ;; names or cons cells whose cars are buffer names.
4338 ((member buffer-name same-window-buffer-names))
4339 ((assoc buffer-name same-window-buffer-names))
4340 ((catch 'found
4341 (dolist (regexp same-window-regexps)
4342 ;; The elements of `same-window-regexps' can be regexps
4343 ;; or cons cells whose cars are regexps.
4344 (when (or (and (stringp regexp)
4345 (string-match-p regexp buffer-name))
4346 (and (consp regexp) (stringp (car regexp))
4347 (string-match-p (car regexp) buffer-name)))
4348 (throw 'found t)))))))
4350 (defcustom pop-up-frames nil
4351 "Whether `display-buffer' should make a separate frame.
4352 If nil, never make a separate frame.
4353 If the value is `graphic-only', make a separate frame
4354 on graphic displays only.
4355 Any other non-nil value means always make a separate frame."
4356 :type '(choice
4357 (const :tag "Never" nil)
4358 (const :tag "On graphic displays only" graphic-only)
4359 (const :tag "Always" t))
4360 :group 'windows)
4362 (defcustom display-buffer-reuse-frames nil
4363 "Non-nil means `display-buffer' should reuse frames.
4364 If the buffer in question is already displayed in a frame, raise
4365 that frame."
4366 :type 'boolean
4367 :version "21.1"
4368 :group 'windows)
4370 (defcustom pop-up-windows t
4371 "Non-nil means `display-buffer' should make a new window."
4372 :type 'boolean
4373 :group 'windows)
4375 (defcustom split-window-preferred-function 'split-window-sensibly
4376 "Function called by `display-buffer' routines to split a window.
4377 This function is called with a window as single argument and is
4378 supposed to split that window and return the new window. If the
4379 window can (or shall) not be split, it is supposed to return nil.
4380 The default is to call the function `split-window-sensibly' which
4381 tries to split the window in a way which seems most suitable.
4382 You can customize the options `split-height-threshold' and/or
4383 `split-width-threshold' in order to have `split-window-sensibly'
4384 prefer either vertical or horizontal splitting.
4386 If you set this to any other function, bear in mind that the
4387 `display-buffer' routines may call this function two times. The
4388 argument of the first call is the largest window on its frame.
4389 If that call fails to return a live window, the function is
4390 called again with the least recently used window as argument. If
4391 that call fails too, `display-buffer' will use an existing window
4392 to display its buffer.
4394 The window selected at the time `display-buffer' was invoked is
4395 still selected when this function is called. Hence you can
4396 compare the window argument with the value of `selected-window'
4397 if you intend to split the selected window instead or if you do
4398 not want to split the selected window."
4399 :type 'function
4400 :version "23.1"
4401 :group 'windows)
4403 (defcustom split-height-threshold 80
4404 "Minimum height for splitting windows sensibly.
4405 If this is an integer, `split-window-sensibly' may split a window
4406 vertically only if it has at least this many lines. If this is
4407 nil, `split-window-sensibly' is not allowed to split a window
4408 vertically. If, however, a window is the only window on its
4409 frame, `split-window-sensibly' may split it vertically
4410 disregarding the value of this variable."
4411 :type '(choice (const nil) (integer :tag "lines"))
4412 :version "23.1"
4413 :group 'windows)
4415 (defcustom split-width-threshold 160
4416 "Minimum width for splitting windows sensibly.
4417 If this is an integer, `split-window-sensibly' may split a window
4418 horizontally only if it has at least this many columns. If this
4419 is nil, `split-window-sensibly' is not allowed to split a window
4420 horizontally."
4421 :type '(choice (const nil) (integer :tag "columns"))
4422 :version "23.1"
4423 :group 'windows)
4425 (defun window-splittable-p (window &optional horizontal)
4426 "Return non-nil if `split-window-sensibly' may split WINDOW.
4427 Optional argument HORIZONTAL nil or omitted means check whether
4428 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
4429 non-nil means check whether WINDOW may be split horizontally.
4431 WINDOW may be split vertically when the following conditions
4432 hold:
4433 - `window-size-fixed' is either nil or equals `width' for the
4434 buffer of WINDOW.
4435 - `split-height-threshold' is an integer and WINDOW is at least as
4436 high as `split-height-threshold'.
4437 - When WINDOW is split evenly, the emanating windows are at least
4438 `window-min-height' lines tall and can accommodate at least one
4439 line plus - if WINDOW has one - a mode line.
4441 WINDOW may be split horizontally when the following conditions
4442 hold:
4443 - `window-size-fixed' is either nil or equals `height' for the
4444 buffer of WINDOW.
4445 - `split-width-threshold' is an integer and WINDOW is at least as
4446 wide as `split-width-threshold'.
4447 - When WINDOW is split evenly, the emanating windows are at least
4448 `window-min-width' or two (whichever is larger) columns wide."
4449 (when (window-live-p window)
4450 (with-current-buffer (window-buffer window)
4451 (if horizontal
4452 ;; A window can be split horizontally when its width is not
4453 ;; fixed, it is at least `split-width-threshold' columns wide
4454 ;; and at least twice as wide as `window-min-width' and 2 (the
4455 ;; latter value is hardcoded).
4456 (and (memq window-size-fixed '(nil height))
4457 ;; Testing `window-full-width-p' here hardly makes any
4458 ;; sense nowadays. This can be done more intuitively by
4459 ;; setting up `split-width-threshold' appropriately.
4460 (numberp split-width-threshold)
4461 (>= (window-width window)
4462 (max split-width-threshold
4463 (* 2 (max window-min-width 2)))))
4464 ;; A window can be split vertically when its height is not
4465 ;; fixed, it is at least `split-height-threshold' lines high,
4466 ;; and it is at least twice as high as `window-min-height' and 2
4467 ;; if it has a mode line or 1.
4468 (and (memq window-size-fixed '(nil width))
4469 (numberp split-height-threshold)
4470 (>= (window-height window)
4471 (max split-height-threshold
4472 (* 2 (max window-min-height
4473 (if mode-line-format 2 1))))))))))
4475 (defun split-window-sensibly (&optional window)
4476 "Split WINDOW in a way suitable for `display-buffer'.
4477 WINDOW defaults to the currently selected window.
4478 If `split-height-threshold' specifies an integer, WINDOW is at
4479 least `split-height-threshold' lines tall and can be split
4480 vertically, split WINDOW into two windows one above the other and
4481 return the lower window. Otherwise, if `split-width-threshold'
4482 specifies an integer, WINDOW is at least `split-width-threshold'
4483 columns wide and can be split horizontally, split WINDOW into two
4484 windows side by side and return the window on the right. If this
4485 can't be done either and WINDOW is the only window on its frame,
4486 try to split WINDOW vertically disregarding any value specified
4487 by `split-height-threshold'. If that succeeds, return the lower
4488 window. Return nil otherwise.
4490 By default `display-buffer' routines call this function to split
4491 the largest or least recently used window. To change the default
4492 customize the option `split-window-preferred-function'.
4494 You can enforce this function to not split WINDOW horizontally,
4495 by setting (or binding) the variable `split-width-threshold' to
4496 nil. If, in addition, you set `split-height-threshold' to zero,
4497 chances increase that this function does split WINDOW vertically.
4499 In order to not split WINDOW vertically, set (or bind) the
4500 variable `split-height-threshold' to nil. Additionally, you can
4501 set `split-width-threshold' to zero to make a horizontal split
4502 more likely to occur.
4504 Have a look at the function `window-splittable-p' if you want to
4505 know how `split-window-sensibly' determines whether WINDOW can be
4506 split."
4507 (let ((window (or window (selected-window))))
4508 (or (and (window-splittable-p window)
4509 ;; Split window vertically.
4510 (with-selected-window window
4511 (split-window-below)))
4512 (and (window-splittable-p window t)
4513 ;; Split window horizontally.
4514 (with-selected-window window
4515 (split-window-right)))
4516 (and (eq window (frame-root-window (window-frame window)))
4517 (not (window-minibuffer-p window))
4518 ;; If WINDOW is the only window on its frame and is not the
4519 ;; minibuffer window, try to split it vertically disregarding
4520 ;; the value of `split-height-threshold'.
4521 (let ((split-height-threshold 0))
4522 (when (window-splittable-p window)
4523 (with-selected-window window
4524 (split-window-below))))))))
4526 (defun window--try-to-split-window (window)
4527 "Try to split WINDOW.
4528 Return value returned by `split-window-preferred-function' if it
4529 represents a live window, nil otherwise."
4530 (and (window-live-p window)
4531 (not (frame-parameter (window-frame window) 'unsplittable))
4532 (let ((new-window
4533 ;; Since `split-window-preferred-function' might
4534 ;; throw an error use `condition-case'.
4535 (condition-case nil
4536 (funcall split-window-preferred-function window)
4537 (error nil))))
4538 (and (window-live-p new-window) new-window))))
4540 (defun window--frame-usable-p (frame)
4541 "Return FRAME if it can be used to display a buffer."
4542 (when (frame-live-p frame)
4543 (let ((window (frame-root-window frame)))
4544 ;; `frame-root-window' may be an internal window which is considered
4545 ;; "dead" by `window-live-p'. Hence if `window' is not live we
4546 ;; implicitly know that `frame' has a visible window we can use.
4547 (unless (and (window-live-p window)
4548 (or (window-minibuffer-p window)
4549 ;; If the window is soft-dedicated, the frame is usable.
4550 ;; Actually, even if the window is really dedicated,
4551 ;; the frame is still usable by splitting it.
4552 ;; At least Emacs-22 allowed it, and it is desirable
4553 ;; when displaying same-frame windows.
4554 nil ; (eq t (window-dedicated-p window))
4556 frame))))
4558 (defcustom even-window-heights t
4559 "If non-nil `display-buffer' will try to even window heights.
4560 Otherwise `display-buffer' will leave the window configuration
4561 alone. Heights are evened only when `display-buffer' chooses a
4562 window that appears above or below the selected window."
4563 :type 'boolean
4564 :group 'windows)
4566 (defun window--even-window-heights (window)
4567 "Even heights of WINDOW and selected window.
4568 Do this only if these windows are vertically adjacent to each
4569 other, `even-window-heights' is non-nil, and the selected window
4570 is higher than WINDOW."
4571 (when (and even-window-heights
4572 (not (eq window (selected-window)))
4573 ;; Don't resize minibuffer windows.
4574 (not (window-minibuffer-p (selected-window)))
4575 (> (window-height (selected-window)) (window-height window))
4576 (eq (window-frame window) (window-frame (selected-window)))
4577 (let ((sel-edges (window-edges (selected-window)))
4578 (win-edges (window-edges window)))
4579 (and (= (nth 0 sel-edges) (nth 0 win-edges))
4580 (= (nth 2 sel-edges) (nth 2 win-edges))
4581 (or (= (nth 1 sel-edges) (nth 3 win-edges))
4582 (= (nth 3 sel-edges) (nth 1 win-edges))))))
4583 (let ((window-min-height 1))
4584 ;; Don't throw an error if we can't even window heights for
4585 ;; whatever reason.
4586 (condition-case nil
4587 (enlarge-window (/ (- (window-height window) (window-height)) 2))
4588 (error nil)))))
4590 (defun window--display-buffer (buffer window type &optional dedicated)
4591 "Display BUFFER in WINDOW and make its frame visible.
4592 TYPE must be one of the symbols `reuse', `window' or `frame' and
4593 is passed unaltered to `display-buffer-record-window'. Set
4594 `window-dedicated-p' to DEDICATED if non-nil. Return WINDOW if
4595 BUFFER and WINDOW are live."
4596 (when (and (buffer-live-p buffer) (window-live-p window))
4597 (unless (eq buffer (window-buffer window))
4598 (set-window-dedicated-p window nil)
4599 (display-buffer-record-window type window buffer)
4600 (set-window-buffer window buffer)
4601 (when dedicated
4602 (set-window-dedicated-p window dedicated))
4603 (when (memq type '(window frame))
4604 (set-window-prev-buffers window nil)))
4605 window))
4607 (defun window--maybe-raise-frame (frame)
4608 (let ((visible (frame-visible-p frame)))
4609 (unless (or (not visible)
4610 ;; Assume the selected frame is already visible enough.
4611 (eq frame (selected-frame))
4612 ;; Assume the frame from which we invoked the
4613 ;; minibuffer is visible.
4614 (and (minibuffer-window-active-p (selected-window))
4615 (eq frame (window-frame (minibuffer-selected-window)))))
4616 (raise-frame frame))))
4618 ;; FIXME: Not implemented.
4619 ;; FIXME: By the way, there could be more levels of dedication:
4620 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
4621 ;; the window hasn't been used for something else yet.
4622 ;; - `softly' dedicated only allows reuse when asked explicitly.
4623 ;; - `strongly' never allows reuse.
4624 (defvar display-buffer-mark-dedicated nil
4625 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
4626 The actual non-nil value of this variable will be copied to the
4627 `window-dedicated-p' flag.")
4629 (defconst display-buffer--action-function-custom-type
4630 '(choice :tag "Function"
4631 (const :tag "--" ignore) ; default for insertion
4632 (const display-buffer-reuse-window)
4633 (const display-buffer-pop-up-window)
4634 (const display-buffer-same-window)
4635 (const display-buffer-pop-up-frame)
4636 (const display-buffer-use-some-window)
4637 (function :tag "Other function"))
4638 "Custom type for `display-buffer' action functions.")
4640 (defconst display-buffer--action-custom-type
4641 `(cons :tag "Action"
4642 (choice :tag "Action functions"
4643 ,display-buffer--action-function-custom-type
4644 (repeat
4645 :tag "List of functions"
4646 ,display-buffer--action-function-custom-type))
4647 (alist :tag "Action arguments"
4648 :key-type symbol
4649 :value-type (sexp :tag "Value")))
4650 "Custom type for `display-buffer' actions.")
4652 (defvar display-buffer-overriding-action '(nil . nil)
4653 "Overriding action to perform to display a buffer.
4654 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
4655 function or a list of functions. Each function should accept two
4656 arguments: a buffer to display and an alist similar to ALIST.
4657 See `display-buffer' for details.")
4658 (put 'display-buffer-overriding-action 'risky-local-variable t)
4660 (defcustom display-buffer-alist nil
4661 "Alist of conditional actions for `display-buffer'.
4662 This is a list of elements (CONDITION . ACTION), where:
4664 CONDITION is either a regexp matching buffer names, or a function
4665 that takes a buffer and returns a boolean.
4667 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
4668 function or a list of functions. Each such function should
4669 accept two arguments: a buffer to display and an alist of the
4670 same form as ALIST. See `display-buffer' for details."
4671 :type `(alist :key-type
4672 (choice :tag "Condition"
4673 regexp
4674 (function :tag "Matcher function"))
4675 :value-type ,display-buffer--action-custom-type)
4676 :risky t
4677 :version "24.1"
4678 :group 'windows)
4680 (defcustom display-buffer-base-action '(nil . nil)
4681 "User-specified default action for `display-buffer'.
4682 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
4683 function or a list of functions. Each function should accept two
4684 arguments: a buffer to display and an alist similar to ALIST.
4685 See `display-buffer' for details."
4686 :type display-buffer--action-custom-type
4687 :risky t
4688 :version "24.1"
4689 :group 'windows)
4691 (defconst display-buffer-fallback-action
4692 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
4693 display-buffer-reuse-window
4694 display-buffer--maybe-pop-up-frame-or-window
4695 display-buffer-use-some-window
4696 ;; If all else fails, pop up a new frame.
4697 display-buffer-pop-up-frame))
4698 "Default fallback action for `display-buffer'.
4699 This is the action used by `display-buffer' if no other actions
4700 specified, e.g. by the user options `display-buffer-alist' or
4701 `display-buffer-base-action'. See `display-buffer'.")
4702 (put 'display-buffer-fallback-action 'risky-local-variable t)
4704 (defun display-buffer-assq-regexp (buffer-name alist)
4705 "Retrieve ALIST entry corresponding to BUFFER-NAME."
4706 (catch 'match
4707 (dolist (entry alist)
4708 (let ((key (car entry)))
4709 (when (or (and (stringp key)
4710 (string-match-p key buffer-name))
4711 (and (symbolp key) (functionp key)
4712 (funcall key buffer-name alist)))
4713 (throw 'match (cdr entry)))))))
4715 (defvar display-buffer--same-window-action
4716 '(display-buffer-same-window
4717 (inhibit-same-window . nil))
4718 "A `display-buffer' action for displaying in the same window.")
4719 (put 'display-buffer--same-window-action 'risky-local-variable t)
4721 (defvar display-buffer--other-frame-action
4722 '((display-buffer-reuse-window
4723 display-buffer-pop-up-frame)
4724 (reusable-frames . 0)
4725 (inhibit-same-window . t))
4726 "A `display-buffer' action for displaying in another frame.")
4727 (put 'display-buffer--other-frame-action 'risky-local-variable t)
4729 (defun display-buffer (buffer-or-name &optional action frame)
4730 "Display BUFFER-OR-NAME in some window, without selecting it.
4731 BUFFER-OR-NAME must be a buffer or the name of an existing
4732 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
4733 or nil if no such window is found.
4735 Optional argument ACTION should have the form (FUNCTION . ALIST).
4736 FUNCTION is either a function or a list of functions.
4737 ALIST is an arbitrary association list (alist).
4739 Each such FUNCTION should accept two arguments: the buffer to
4740 display and an alist. Based on those arguments, it should either
4741 display the buffer and return the window, or return nil if unable
4742 to display the buffer.
4744 The `display-buffer' function builds a function list and an alist
4745 by combining the functions and alists specified in
4746 `display-buffer-overriding-action', `display-buffer-alist', the
4747 ACTION argument, `display-buffer-base-action', and
4748 `display-buffer-fallback-action' (in order). Then it calls each
4749 function in the combined function list in turn, passing the
4750 buffer as the first argument and the combined alist as the second
4751 argument, until one of the functions returns non-nil.
4753 Available action functions include:
4754 `display-buffer-same-window'
4755 `display-buffer-reuse-window'
4756 `display-buffer-pop-up-frame'
4757 `display-buffer-pop-up-window'
4758 `display-buffer-use-some-window'
4760 Recognized alist entries include:
4762 `inhibit-same-window' -- A non-nil value prevents the same
4763 window from being used for display.
4765 `inhibit-switch-frame' -- A non-nil value prevents any other
4766 frame from being raised or selected,
4767 even if the window is displayed there.
4769 `reusable-frames' -- Value specifies frame(s) to search for a
4770 window that already displays the buffer.
4771 See `display-buffer-reuse-window'.
4773 The ACTION argument to `display-buffer' can also have a non-nil
4774 and non-list value. This means to display the buffer in a window
4775 other than the selected one, even if it is already displayed in
4776 the selected window. If called interactively with a prefix
4777 argument, ACTION is t.
4779 Optional argument FRAME, if non-nil, acts like an additional
4780 ALIST entry (reusable-frames . FRAME), specifying the frame(s) to
4781 search for a window that is already displaying the buffer. See
4782 `display-buffer-reuse-window'."
4783 (interactive (list (read-buffer "Display buffer: " (other-buffer))
4784 (if current-prefix-arg t)))
4785 (let ((buffer (if (bufferp buffer-or-name)
4786 buffer-or-name
4787 (get-buffer buffer-or-name)))
4788 ;; Handle the old form of the first argument.
4789 (inhibit-same-window (and action (not (listp action)))))
4790 (unless (listp action) (setq action nil))
4791 (if display-buffer-function
4792 ;; If `display-buffer-function' is defined, let it do the job.
4793 (funcall display-buffer-function buffer inhibit-same-window)
4794 ;; Otherwise, use the defined actions.
4795 (let* ((user-action
4796 (display-buffer-assq-regexp (buffer-name buffer)
4797 display-buffer-alist))
4798 (special-action (display-buffer--special-action buffer))
4799 ;; Extra actions from the arguments to this function:
4800 (extra-action
4801 (cons nil (append (if inhibit-same-window
4802 '((inhibit-same-window . t)))
4803 (if frame
4804 `((reusable-frames . ,frame))))))
4805 ;; Construct action function list and action alist.
4806 (actions (list display-buffer-overriding-action
4807 user-action special-action action extra-action
4808 display-buffer-base-action
4809 display-buffer-fallback-action))
4810 (functions (apply 'append
4811 (mapcar (lambda (x)
4812 (setq x (car x))
4813 (if (functionp x) (list x) x))
4814 actions)))
4815 (alist (apply 'append (mapcar 'cdr actions)))
4816 window)
4817 (unless (buffer-live-p buffer)
4818 (error "Invalid buffer"))
4819 (while (and functions (not window))
4820 (setq window (funcall (car functions) buffer alist)
4821 functions (cdr functions)))
4822 window))))
4824 (defun display-buffer-other-frame (buffer)
4825 "Display buffer BUFFER in another frame.
4826 This uses the function `display-buffer' as a subroutine; see
4827 its documentation for additional customization information."
4828 (interactive "BDisplay buffer in other frame: ")
4829 (display-buffer buffer display-buffer--other-frame-action t))
4831 ;;; `display-buffer' action functions:
4833 (defun display-buffer-same-window (buffer alist)
4834 "Display BUFFER in the selected window.
4835 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
4836 if the selected window is a minibuffer window or is dedicated to
4837 another buffer; in that case, return nil. Otherwise, return the
4838 selected window."
4839 (unless (or (cdr (assq 'inhibit-same-window alist))
4840 (window-minibuffer-p)
4841 (window-dedicated-p))
4842 (window--display-buffer buffer (selected-window) 'reuse)))
4844 (defun display-buffer--maybe-same-window (buffer alist)
4845 "Conditionally display BUFFER in the selected window.
4846 If `same-window-p' returns non-nil for BUFFER's name, call
4847 `display-buffer-same-window' and return its value. Otherwise,
4848 return nil."
4849 (and (same-window-p (buffer-name buffer))
4850 (display-buffer-same-window buffer alist)))
4852 (defun display-buffer-reuse-window (buffer alist)
4853 "Return a window that is already displaying BUFFER.
4854 Return nil if no usable window is found.
4856 If ALIST has a non-nil `inhibit-same-window' entry, the selected
4857 window is not eligible for reuse.
4859 If ALIST contains a `reusable-frames' entry, its value determines
4860 which frames to search for a reusable window:
4861 nil -- the selected frame (actually the last non-minibuffer frame)
4862 A frame -- just that frame
4863 `visible' -- all visible frames
4864 0 -- all frames on the current terminal
4865 t -- all frames.
4867 If ALIST contains no `reusable-frames' entry, search just the
4868 selected frame if `display-buffer-reuse-frames' and
4869 `pop-up-frames' are both nil; search all frames on the current
4870 terminal if either of those variables is non-nil.
4872 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
4873 event that a window on another frame is chosen, avoid raising
4874 that frame."
4875 (let* ((alist-entry (assq 'reusable-frames alist))
4876 (frames (cond (alist-entry (cdr alist-entry))
4877 ((if (eq pop-up-frames 'graphic-only)
4878 (display-graphic-p)
4879 pop-up-frames)
4881 (display-buffer-reuse-frames 0)
4882 (t (last-nonminibuffer-frame))))
4883 (window (if (and (eq buffer (window-buffer))
4884 (not (cdr (assq 'inhibit-same-window alist))))
4885 (selected-window)
4886 (car (delq (selected-window)
4887 (get-buffer-window-list buffer 'nomini
4888 frames))))))
4889 (when (window-live-p window)
4890 (prog1 (window--display-buffer buffer window 'reuse)
4891 (unless (cdr (assq 'inhibit-switch-frame alist))
4892 (window--maybe-raise-frame (window-frame window)))))))
4894 (defun display-buffer--special-action (buffer)
4895 "Return special display action for BUFFER, if any.
4896 If `special-display-p' returns non-nil for BUFFER, return an
4897 appropriate display action involving `special-display-function'.
4898 See `display-buffer' for the format of display actions."
4899 (and special-display-function
4900 ;; `special-display-p' returns either t or a list of frame
4901 ;; parameters to pass to `special-display-function'.
4902 (let ((pars (special-display-p (buffer-name buffer))))
4903 (when pars
4904 (list (list #'display-buffer-reuse-window
4905 `(lambda (buffer _alist)
4906 (funcall special-display-function
4907 buffer ',(if (listp pars) pars)))))))))
4909 (defun display-buffer-pop-up-frame (buffer alist)
4910 "Display BUFFER in a new frame.
4911 This works by calling `pop-up-frame-function'. If successful,
4912 return the window used; otherwise return nil.
4914 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
4915 raising the new frame."
4916 (let ((fun pop-up-frame-function)
4917 frame window)
4918 (when (and fun
4919 (setq frame (funcall fun))
4920 (setq window (frame-selected-window frame)))
4921 (prog1 (window--display-buffer buffer window
4922 'frame display-buffer-mark-dedicated)
4923 (unless (cdr (assq 'inhibit-switch-frame alist))
4924 (window--maybe-raise-frame frame))))))
4926 (defun display-buffer-pop-up-window (buffer alist)
4927 "Display BUFFER by popping up a new window.
4928 The new window is created on the selected frame, or in
4929 `last-nonminibuffer-frame' if no windows can be created there.
4930 If successful, return the new window; otherwise return nil.
4932 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
4933 event that the new window is created on another frame, avoid
4934 raising the frame."
4935 (let ((frame (or (window--frame-usable-p (selected-frame))
4936 (window--frame-usable-p (last-nonminibuffer-frame))))
4937 window)
4938 (when (and (or (not (frame-parameter frame 'unsplittable))
4939 ;; If the selected frame cannot be split, look at
4940 ;; `last-nonminibuffer-frame'.
4941 (and (eq frame (selected-frame))
4942 (setq frame (last-nonminibuffer-frame))
4943 (window--frame-usable-p frame)
4944 (not (frame-parameter frame 'unsplittable))))
4945 ;; Attempt to split largest or least recently used window.
4946 (setq window (or (window--try-to-split-window
4947 (get-largest-window frame t))
4948 (window--try-to-split-window
4949 (get-lru-window frame t)))))
4950 (prog1 (window--display-buffer buffer window
4951 'window display-buffer-mark-dedicated)
4952 (unless (cdr (assq 'inhibit-switch-frame alist))
4953 (window--maybe-raise-frame (window-frame window)))))))
4955 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
4956 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
4958 If `pop-up-frames' is non-nil (and not `graphic-only' on a
4959 text-only terminal), try with `display-buffer-pop-up-frame'.
4961 If that cannot be done, and `pop-up-windows' is non-nil, try
4962 again with `display-buffer-pop-up-window'."
4963 (or (and (if (eq pop-up-frames 'graphic-only)
4964 (display-graphic-p)
4965 pop-up-frames)
4966 (display-buffer-pop-up-frame buffer alist))
4967 (and pop-up-windows
4968 (display-buffer-pop-up-window buffer alist))))
4970 (defun display-buffer-use-some-window (buffer alist)
4971 "Display BUFFER in an existing window.
4972 Search for a usable window, set that window to the buffer, and
4973 return the window. If no suitable window is found, return nil.
4975 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
4976 event that a window in another frame is chosen, avoid raising
4977 that frame."
4978 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
4979 (frame (or (window--frame-usable-p (selected-frame))
4980 (window--frame-usable-p (last-nonminibuffer-frame))))
4981 (window
4982 ;; Reuse an existing window.
4983 (or (get-lru-window frame nil not-this-window)
4984 (let ((window (get-buffer-window buffer 'visible)))
4985 (unless (and not-this-window
4986 (eq window (selected-window)))
4987 window))
4988 (get-largest-window 'visible nil not-this-window)
4989 (let ((window (get-buffer-window buffer 0)))
4990 (unless (and not-this-window
4991 (eq window (selected-window)))
4992 window))
4993 (get-largest-window 0 not-this-window))))
4994 (when (window-live-p window)
4995 (window--even-window-heights window)
4996 (prog1 (window--display-buffer buffer window 'reuse)
4997 (unless (cdr (assq 'inhibit-switch-frame alist))
4998 (window--maybe-raise-frame (window-frame window)))))))
5000 ;;; Display + selection commands:
5001 (defun pop-to-buffer (buffer &optional action norecord)
5002 "Select buffer BUFFER in some window, preferably a different one.
5003 BUFFER may be a buffer, a string (a buffer name), or nil. If it
5004 is a string not naming an existent buffer, create a buffer with
5005 that name. If BUFFER is nil, choose some other buffer. Return
5006 the buffer.
5008 This uses `display-buffer' as a subroutine. The optional ACTION
5009 argument is passed to `display-buffer' as its ACTION argument.
5010 See `display-buffer' for more information. ACTION is t if called
5011 interactively with a prefix argument, which means to pop to a
5012 window other than the selected one even if the buffer is already
5013 displayed in the selected window.
5015 If the window to show BUFFER is not on the selected
5016 frame, raise that window's frame and give it input focus.
5018 Optional third arg NORECORD non-nil means do not put this buffer
5019 at the front of the list of recently selected ones."
5020 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
5021 (if current-prefix-arg t)))
5022 (setq buffer (window-normalize-buffer-to-switch-to buffer))
5023 (set-buffer buffer)
5024 (let* ((old-frame (selected-frame))
5025 (window (display-buffer buffer action))
5026 (frame (window-frame window)))
5027 ;; If we chose another frame, make sure it gets input focus.
5028 (unless (eq frame old-frame)
5029 (select-frame-set-input-focus frame norecord))
5030 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
5031 (select-window window norecord)
5032 buffer))
5034 (defun pop-to-buffer-same-window (buffer &optional norecord)
5035 "Select buffer BUFFER in some window, preferably the same one.
5036 This function behaves much like `switch-to-buffer', except it
5037 displays with `special-display-function' if BUFFER has a match in
5038 `special-display-buffer-names' or `special-display-regexps'.
5040 Unlike `pop-to-buffer', this function prefers using the selected
5041 window over popping up a new window or frame.
5043 BUFFER may be a buffer, a string (a buffer name), or nil. If it
5044 is a string not naming an existent buffer, create a buffer with
5045 that name. If BUFFER is nil, choose some other buffer. Return
5046 the buffer.
5048 NORECORD, if non-nil means do not put this buffer at the front of
5049 the list of recently selected ones."
5050 (pop-to-buffer buffer display-buffer--same-window-action norecord))
5052 (defun read-buffer-to-switch (prompt)
5053 "Read the name of a buffer to switch to, prompting with PROMPT.
5054 Return the name of the buffer as a string.
5056 This function is intended for the `switch-to-buffer' family of
5057 commands since these need to omit the name of the current buffer
5058 from the list of completions and default values."
5059 (let ((rbts-completion-table (internal-complete-buffer-except)))
5060 (minibuffer-with-setup-hook
5061 (lambda ()
5062 (setq minibuffer-completion-table rbts-completion-table)
5063 ;; Since rbts-completion-table is built dynamically, we
5064 ;; can't just add it to the default value of
5065 ;; icomplete-with-completion-tables, so we add it
5066 ;; here manually.
5067 (if (and (boundp 'icomplete-with-completion-tables)
5068 (listp icomplete-with-completion-tables))
5069 (set (make-local-variable 'icomplete-with-completion-tables)
5070 (cons rbts-completion-table
5071 icomplete-with-completion-tables))))
5072 (read-buffer prompt (other-buffer (current-buffer))
5073 (confirm-nonexistent-file-or-buffer)))))
5075 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
5076 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
5077 If BUFFER-OR-NAME is nil, return the buffer returned by
5078 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
5079 exists, return that buffer. If no such buffer exists, create a
5080 buffer with the name BUFFER-OR-NAME and return that buffer."
5081 (if buffer-or-name
5082 (or (get-buffer buffer-or-name)
5083 (let ((buffer (get-buffer-create buffer-or-name)))
5084 (set-buffer-major-mode buffer)
5085 buffer))
5086 (other-buffer)))
5088 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
5089 "Switch to buffer BUFFER-OR-NAME in the selected window.
5090 If called interactively, prompt for the buffer name using the
5091 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5092 determines whether to request confirmation before creating a new
5093 buffer.
5095 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5096 nil. If BUFFER-OR-NAME is a string that does not identify an
5097 existing buffer, create a buffer with that name. If
5098 BUFFER-OR-NAME is nil, switch to the buffer returned by
5099 `other-buffer'.
5101 Optional argument NORECORD non-nil means do not put the buffer
5102 specified by BUFFER-OR-NAME at the front of the buffer list and
5103 do not make the window displaying it the most recently selected
5104 one.
5106 If FORCE-SAME-WINDOW is non-nil, BUFFER-OR-NAME must be displayed
5107 in the selected window; signal an error if that is
5108 impossible (e.g. if the selected window is minibuffer-only). If
5109 nil, BUFFER-OR-NAME may be displayed in another window.
5111 Return the buffer switched to."
5112 (interactive
5113 (list (read-buffer-to-switch "Switch to buffer: ") nil 'force-same-window))
5114 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
5115 (cond
5116 ;; Don't call set-window-buffer if it's not needed since it
5117 ;; might signal an error (e.g. if the window is dedicated).
5118 ((eq buffer (window-buffer)))
5119 ((window-minibuffer-p)
5120 (if force-same-window
5121 (user-error "Cannot switch buffers in minibuffer window")
5122 (pop-to-buffer buffer norecord)))
5123 ((eq (window-dedicated-p) t)
5124 (if force-same-window
5125 (user-error "Cannot switch buffers in a dedicated window")
5126 (pop-to-buffer buffer norecord)))
5127 (t (set-window-buffer nil buffer)))
5129 (unless norecord
5130 (select-window (selected-window)))
5131 (set-buffer buffer)))
5133 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
5134 "Select the buffer specified by BUFFER-OR-NAME in another window.
5135 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5136 nil. Return the buffer switched to.
5138 If called interactively, prompt for the buffer name using the
5139 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5140 determines whether to request confirmation before creating a new
5141 buffer.
5143 If BUFFER-OR-NAME is a string and does not identify an existing
5144 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5145 nil, switch to the buffer returned by `other-buffer'.
5147 Optional second argument NORECORD non-nil means do not put this
5148 buffer at the front of the list of recently selected ones.
5150 This uses the function `display-buffer' as a subroutine; see its
5151 documentation for additional customization information."
5152 (interactive
5153 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5154 (let ((pop-up-windows t))
5155 (pop-to-buffer buffer-or-name t norecord)))
5157 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
5158 "Switch to buffer BUFFER-OR-NAME in another frame.
5159 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5160 nil. Return the buffer switched to.
5162 If called interactively, prompt for the buffer name using the
5163 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5164 determines whether to request confirmation before creating a new
5165 buffer.
5167 If BUFFER-OR-NAME is a string and does not identify an existing
5168 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5169 nil, switch to the buffer returned by `other-buffer'.
5171 Optional second arg NORECORD non-nil means do not put this
5172 buffer at the front of the list of recently selected ones.
5174 This uses the function `display-buffer' as a subroutine; see its
5175 documentation for additional customization information."
5176 (interactive
5177 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
5178 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
5180 (defun set-window-text-height (window height)
5181 "Set the height in lines of the text display area of WINDOW to HEIGHT.
5182 WINDOW must be a live window. HEIGHT doesn't include the mode
5183 line or header line, if any, or any partial-height lines in the
5184 text display area.
5186 Note that the current implementation of this function cannot
5187 always set the height exactly, but attempts to be conservative,
5188 by allocating more lines than are actually needed in the case
5189 where some error may be present."
5190 (setq window (window-normalize-window window t))
5191 (let ((delta (- height (window-text-height window))))
5192 (unless (zerop delta)
5193 ;; Setting window-min-height to a value like 1 can lead to very
5194 ;; bizarre displays because it also allows Emacs to make *other*
5195 ;; windows one line tall, which means that there's no more space
5196 ;; for the mode line.
5197 (let ((window-min-height (min 2 height)))
5198 (window-resize window delta)))))
5200 (defun enlarge-window-horizontally (delta)
5201 "Make selected window DELTA columns wider.
5202 Interactively, if no argument is given, make selected window one
5203 column wider."
5204 (interactive "p")
5205 (enlarge-window delta t))
5207 (defun shrink-window-horizontally (delta)
5208 "Make selected window DELTA columns narrower.
5209 Interactively, if no argument is given, make selected window one
5210 column narrower."
5211 (interactive "p")
5212 (shrink-window delta t))
5214 (defun count-screen-lines (&optional beg end count-final-newline window)
5215 "Return the number of screen lines in the region.
5216 The number of screen lines may be different from the number of actual lines,
5217 due to line breaking, display table, etc.
5219 Optional arguments BEG and END default to `point-min' and `point-max'
5220 respectively.
5222 If region ends with a newline, ignore it unless optional third argument
5223 COUNT-FINAL-NEWLINE is non-nil.
5225 The optional fourth argument WINDOW specifies the window used for obtaining
5226 parameters such as width, horizontal scrolling, and so on. The default is
5227 to use the selected window's parameters.
5229 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
5230 regardless of which buffer is displayed in WINDOW. This makes possible to use
5231 `count-screen-lines' in any buffer, whether or not it is currently displayed
5232 in some window."
5233 (unless beg
5234 (setq beg (point-min)))
5235 (unless end
5236 (setq end (point-max)))
5237 (if (= beg end)
5239 (save-excursion
5240 (save-restriction
5241 (widen)
5242 (narrow-to-region (min beg end)
5243 (if (and (not count-final-newline)
5244 (= ?\n (char-before (max beg end))))
5245 (1- (max beg end))
5246 (max beg end)))
5247 (goto-char (point-min))
5248 (1+ (vertical-motion (buffer-size) window))))))
5250 (defun window-buffer-height (window)
5251 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
5252 (with-current-buffer (window-buffer window)
5253 (max 1
5254 (count-screen-lines (point-min) (point-max)
5255 ;; If buffer ends with a newline, ignore it when
5256 ;; counting height unless point is after it.
5257 (eobp)
5258 window))))
5260 ;;; Resizing buffers to fit their contents exactly.
5261 (defun fit-window-to-buffer (&optional window max-height min-height override)
5262 "Adjust height of WINDOW to display its buffer's contents exactly.
5263 WINDOW can be any live window and defaults to the selected one.
5265 Optional argument MAX-HEIGHT specifies the maximum height of
5266 WINDOW and defaults to the height of WINDOW's frame. Optional
5267 argument MIN-HEIGHT specifies the minimum height of WINDOW and
5268 defaults to `window-min-height'. Both MAX-HEIGHT and MIN-HEIGHT
5269 are specified in lines and include the mode line and header line,
5270 if any.
5272 Optional argument OVERRIDE non-nil means override restrictions
5273 imposed by `window-min-height' and `window-min-width' on the size
5274 of WINDOW.
5276 Return the number of lines by which WINDOW was enlarged or
5277 shrunk. If an error occurs during resizing, return nil but don't
5278 signal an error.
5280 Note that even if this function makes WINDOW large enough to show
5281 _all_ lines of its buffer you might not see the first lines when
5282 WINDOW was scrolled."
5283 (interactive)
5284 ;; Do all the work in WINDOW and its buffer and restore the selected
5285 ;; window and the current buffer when we're done.
5286 (setq window (window-normalize-window window t))
5287 ;; Can't resize a full height or fixed-size window.
5288 (unless (or (window-size-fixed-p window)
5289 (window-full-height-p window))
5290 ;; `with-selected-window' should orderly restore the current buffer.
5291 (with-selected-window window
5292 ;; We are in WINDOW's buffer now.
5293 (let* (;; Adjust MIN-HEIGHT.
5294 (min-height
5295 (if override
5296 (window-min-size window nil window)
5297 (max (or min-height window-min-height)
5298 window-safe-min-height)))
5299 (max-window-height
5300 (window-total-size (frame-root-window window)))
5301 ;; Adjust MAX-HEIGHT.
5302 (max-height
5303 (if (or override (not max-height))
5304 max-window-height
5305 (min max-height max-window-height)))
5306 ;; Make `desired-height' the height necessary to show
5307 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
5308 ;; and MAX-HEIGHT.
5309 (desired-height
5310 (max
5311 (min
5312 (+ (count-screen-lines)
5313 ;; For non-minibuffers count the mode line, if any.
5314 (if (and (not (window-minibuffer-p window))
5315 mode-line-format)
5318 ;; Count the header line, if any.
5319 (if header-line-format 1 0))
5320 max-height)
5321 min-height))
5322 (desired-delta
5323 (- desired-height (window-total-size window)))
5324 (delta
5325 (if (> desired-delta 0)
5326 (min desired-delta
5327 (window-max-delta window nil window))
5328 (max desired-delta
5329 (- (window-min-delta window nil window))))))
5330 ;; This `condition-case' shouldn't be necessary, but who knows?
5331 (condition-case nil
5332 (if (zerop delta)
5333 ;; Return zero if DELTA became zero in the process.
5335 ;; Don't try to redisplay with the cursor at the end on its
5336 ;; own line--that would force a scroll and spoil things.
5337 (when (and (eobp) (bolp) (not (bobp)))
5338 ;; It's silly to put `point' at the end of the previous
5339 ;; line and so maybe force horizontal scrolling.
5340 (set-window-point window (line-beginning-position 0)))
5341 ;; Call `window-resize' with OVERRIDE argument equal WINDOW.
5342 (window-resize window delta nil window)
5343 ;; Check if the last line is surely fully visible. If
5344 ;; not, enlarge the window.
5345 (let ((end (save-excursion
5346 (goto-char (point-max))
5347 (when (and (bolp) (not (bobp)))
5348 ;; Don't include final newline.
5349 (backward-char 1))
5350 (when truncate-lines
5351 ;; If line-wrapping is turned off, test the
5352 ;; beginning of the last line for
5353 ;; visibility instead of the end, as the
5354 ;; end of the line could be invisible by
5355 ;; virtue of extending past the edge of the
5356 ;; window.
5357 (forward-line 0))
5358 (point))))
5359 (set-window-vscroll window 0)
5360 ;; This loop might in some rare pathological cases raise
5361 ;; an error - another reason for the `condition-case'.
5362 (while (and (< desired-height max-height)
5363 (= desired-height (window-total-size))
5364 (not (pos-visible-in-window-p end)))
5365 (window-resize window 1 nil window)
5366 (setq desired-height (1+ desired-height)))))
5367 (error (setq delta nil)))
5368 delta))))
5370 (defun window-safely-shrinkable-p (&optional window)
5371 "Return t if WINDOW can be shrunk without shrinking other windows.
5372 WINDOW defaults to the selected window."
5373 (with-selected-window (or window (selected-window))
5374 (let ((edges (window-edges)))
5375 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
5376 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
5378 (defun shrink-window-if-larger-than-buffer (&optional window)
5379 "Shrink height of WINDOW if its buffer doesn't need so many lines.
5380 More precisely, shrink WINDOW vertically to be as small as
5381 possible, while still showing the full contents of its buffer.
5382 WINDOW defaults to the selected window.
5384 Do not shrink WINDOW to less than `window-min-height' lines. Do
5385 nothing if the buffer contains more lines than the present window
5386 height, or if some of the window's contents are scrolled out of
5387 view, or if shrinking this window would also shrink another
5388 window, or if the window is the only window of its frame.
5390 Return non-nil if the window was shrunk, nil otherwise."
5391 (interactive)
5392 (setq window (window-normalize-window window t))
5393 ;; Make sure that WINDOW is vertically combined and `point-min' is
5394 ;; visible (for whatever reason that's needed). The remaining issues
5395 ;; should be taken care of by `fit-window-to-buffer'.
5396 (when (and (window-combined-p window)
5397 (pos-visible-in-window-p (point-min) window))
5398 (fit-window-to-buffer window (window-total-size window))))
5400 (defun kill-buffer-and-window ()
5401 "Kill the current buffer and delete the selected window."
5402 (interactive)
5403 (let ((window-to-delete (selected-window))
5404 (buffer-to-kill (current-buffer))
5405 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
5406 (unwind-protect
5407 (progn
5408 (add-hook 'kill-buffer-hook delete-window-hook t t)
5409 (if (kill-buffer (current-buffer))
5410 ;; If `delete-window' failed before, we rerun it to regenerate
5411 ;; the error so it can be seen in the echo area.
5412 (when (eq (selected-window) window-to-delete)
5413 (delete-window))))
5414 ;; If the buffer is not dead for some reason (probably because
5415 ;; of a `quit' signal), remove the hook again.
5416 (ignore-errors
5417 (with-current-buffer buffer-to-kill
5418 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
5421 (defvar recenter-last-op nil
5422 "Indicates the last recenter operation performed.
5423 Possible values: `top', `middle', `bottom', integer or float numbers.")
5425 (defcustom recenter-positions '(middle top bottom)
5426 "Cycling order for `recenter-top-bottom'.
5427 A list of elements with possible values `top', `middle', `bottom',
5428 integer or float numbers that define the cycling order for
5429 the command `recenter-top-bottom'.
5431 Top and bottom destinations are `scroll-margin' lines from the true
5432 window top and bottom. Middle redraws the frame and centers point
5433 vertically within the window. Integer number moves current line to
5434 the specified absolute window-line. Float number between 0.0 and 1.0
5435 means the percentage of the screen space from the top. The default
5436 cycling order is middle -> top -> bottom."
5437 :type '(repeat (choice
5438 (const :tag "Top" top)
5439 (const :tag "Middle" middle)
5440 (const :tag "Bottom" bottom)
5441 (integer :tag "Line number")
5442 (float :tag "Percentage")))
5443 :version "23.2"
5444 :group 'windows)
5446 (defun recenter-top-bottom (&optional arg)
5447 "Move current buffer line to the specified window line.
5448 With no prefix argument, successive calls place point according
5449 to the cycling order defined by `recenter-positions'.
5451 A prefix argument is handled like `recenter':
5452 With numeric prefix ARG, move current line to window-line ARG.
5453 With plain `C-u', move current line to window center."
5454 (interactive "P")
5455 (cond
5456 (arg (recenter arg)) ; Always respect ARG.
5458 (setq recenter-last-op
5459 (if (eq this-command last-command)
5460 (car (or (cdr (member recenter-last-op recenter-positions))
5461 recenter-positions))
5462 (car recenter-positions)))
5463 (let ((this-scroll-margin
5464 (min (max 0 scroll-margin)
5465 (truncate (/ (window-body-height) 4.0)))))
5466 (cond ((eq recenter-last-op 'middle)
5467 (recenter))
5468 ((eq recenter-last-op 'top)
5469 (recenter this-scroll-margin))
5470 ((eq recenter-last-op 'bottom)
5471 (recenter (- -1 this-scroll-margin)))
5472 ((integerp recenter-last-op)
5473 (recenter recenter-last-op))
5474 ((floatp recenter-last-op)
5475 (recenter (round (* recenter-last-op (window-height))))))))))
5477 (define-key global-map [?\C-l] 'recenter-top-bottom)
5479 (defun move-to-window-line-top-bottom (&optional arg)
5480 "Position point relative to window.
5482 With a prefix argument ARG, acts like `move-to-window-line'.
5484 With no argument, positions point at center of window.
5485 Successive calls position point at positions defined
5486 by `recenter-positions'."
5487 (interactive "P")
5488 (cond
5489 (arg (move-to-window-line arg)) ; Always respect ARG.
5491 (setq recenter-last-op
5492 (if (eq this-command last-command)
5493 (car (or (cdr (member recenter-last-op recenter-positions))
5494 recenter-positions))
5495 (car recenter-positions)))
5496 (let ((this-scroll-margin
5497 (min (max 0 scroll-margin)
5498 (truncate (/ (window-body-height) 4.0)))))
5499 (cond ((eq recenter-last-op 'middle)
5500 (call-interactively 'move-to-window-line))
5501 ((eq recenter-last-op 'top)
5502 (move-to-window-line this-scroll-margin))
5503 ((eq recenter-last-op 'bottom)
5504 (move-to-window-line (- -1 this-scroll-margin)))
5505 ((integerp recenter-last-op)
5506 (move-to-window-line recenter-last-op))
5507 ((floatp recenter-last-op)
5508 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
5510 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
5512 ;;; Scrolling commands.
5514 ;;; Scrolling commands which do not signal errors at top/bottom
5515 ;;; of buffer at first key-press (instead move to top/bottom
5516 ;;; of buffer).
5518 (defcustom scroll-error-top-bottom nil
5519 "Move point to top/bottom of buffer before signaling a scrolling error.
5520 A value of nil means just signal an error if no more scrolling possible.
5521 A value of t means point moves to the beginning or the end of the buffer
5522 \(depending on scrolling direction) when no more scrolling possible.
5523 When point is already on that position, then signal an error."
5524 :type 'boolean
5525 :group 'windows
5526 :version "24.1")
5528 (defun scroll-up-command (&optional arg)
5529 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
5530 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
5531 scroll window further, move cursor to the bottom line.
5532 When point is already on that position, then signal an error.
5533 A near full screen is `next-screen-context-lines' less than a full screen.
5534 Negative ARG means scroll downward.
5535 If ARG is the atom `-', scroll downward by nearly full screen."
5536 (interactive "^P")
5537 (cond
5538 ((null scroll-error-top-bottom)
5539 (scroll-up arg))
5540 ((eq arg '-)
5541 (scroll-down-command nil))
5542 ((< (prefix-numeric-value arg) 0)
5543 (scroll-down-command (- (prefix-numeric-value arg))))
5544 ((eobp)
5545 (scroll-up arg)) ; signal error
5547 (condition-case nil
5548 (scroll-up arg)
5549 (end-of-buffer
5550 (if arg
5551 ;; When scrolling by ARG lines can't be done,
5552 ;; move by ARG lines instead.
5553 (forward-line arg)
5554 ;; When ARG is nil for full-screen scrolling,
5555 ;; move to the bottom of the buffer.
5556 (goto-char (point-max))))))))
5558 (put 'scroll-up-command 'scroll-command t)
5560 (defun scroll-down-command (&optional arg)
5561 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
5562 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
5563 scroll window further, move cursor to the top line.
5564 When point is already on that position, then signal an error.
5565 A near full screen is `next-screen-context-lines' less than a full screen.
5566 Negative ARG means scroll upward.
5567 If ARG is the atom `-', scroll upward by nearly full screen."
5568 (interactive "^P")
5569 (cond
5570 ((null scroll-error-top-bottom)
5571 (scroll-down arg))
5572 ((eq arg '-)
5573 (scroll-up-command nil))
5574 ((< (prefix-numeric-value arg) 0)
5575 (scroll-up-command (- (prefix-numeric-value arg))))
5576 ((bobp)
5577 (scroll-down arg)) ; signal error
5579 (condition-case nil
5580 (scroll-down arg)
5581 (beginning-of-buffer
5582 (if arg
5583 ;; When scrolling by ARG lines can't be done,
5584 ;; move by ARG lines instead.
5585 (forward-line (- arg))
5586 ;; When ARG is nil for full-screen scrolling,
5587 ;; move to the top of the buffer.
5588 (goto-char (point-min))))))))
5590 (put 'scroll-down-command 'scroll-command t)
5592 ;;; Scrolling commands which scroll a line instead of full screen.
5594 (defun scroll-up-line (&optional arg)
5595 "Scroll text of selected window upward ARG lines; or one line if no ARG.
5596 If ARG is omitted or nil, scroll upward by one line.
5597 This is different from `scroll-up-command' that scrolls a full screen."
5598 (interactive "p")
5599 (scroll-up (or arg 1)))
5601 (put 'scroll-up-line 'scroll-command t)
5603 (defun scroll-down-line (&optional arg)
5604 "Scroll text of selected window down ARG lines; or one line if no ARG.
5605 If ARG is omitted or nil, scroll down by one line.
5606 This is different from `scroll-down-command' that scrolls a full screen."
5607 (interactive "p")
5608 (scroll-down (or arg 1)))
5610 (put 'scroll-down-line 'scroll-command t)
5613 (defun scroll-other-window-down (lines)
5614 "Scroll the \"other window\" down.
5615 For more details, see the documentation for `scroll-other-window'."
5616 (interactive "P")
5617 (scroll-other-window
5618 ;; Just invert the argument's meaning.
5619 ;; We can do that without knowing which window it will be.
5620 (if (eq lines '-) nil
5621 (if (null lines) '-
5622 (- (prefix-numeric-value lines))))))
5624 (defun beginning-of-buffer-other-window (arg)
5625 "Move point to the beginning of the buffer in the other window.
5626 Leave mark at previous position.
5627 With arg N, put point N/10 of the way from the true beginning."
5628 (interactive "P")
5629 (let ((orig-window (selected-window))
5630 (window (other-window-for-scrolling)))
5631 ;; We use unwind-protect rather than save-window-excursion
5632 ;; because the latter would preserve the things we want to change.
5633 (unwind-protect
5634 (progn
5635 (select-window window)
5636 ;; Set point and mark in that window's buffer.
5637 (with-no-warnings
5638 (beginning-of-buffer arg))
5639 ;; Set point accordingly.
5640 (recenter '(t)))
5641 (select-window orig-window))))
5643 (defun end-of-buffer-other-window (arg)
5644 "Move point to the end of the buffer in the other window.
5645 Leave mark at previous position.
5646 With arg N, put point N/10 of the way from the true end."
5647 (interactive "P")
5648 ;; See beginning-of-buffer-other-window for comments.
5649 (let ((orig-window (selected-window))
5650 (window (other-window-for-scrolling)))
5651 (unwind-protect
5652 (progn
5653 (select-window window)
5654 (with-no-warnings
5655 (end-of-buffer arg))
5656 (recenter '(t)))
5657 (select-window orig-window))))
5659 (defvar mouse-autoselect-window-timer nil
5660 "Timer used by delayed window autoselection.")
5662 (defvar mouse-autoselect-window-position nil
5663 "Last mouse position recorded by delayed window autoselection.")
5665 (defvar mouse-autoselect-window-window nil
5666 "Last window recorded by delayed window autoselection.")
5668 (defvar mouse-autoselect-window-state nil
5669 "When non-nil, special state of delayed window autoselection.
5670 Possible values are `suspend' (suspend autoselection after a menu or
5671 scrollbar interaction) and `select' (the next invocation of
5672 `handle-select-window' shall select the window immediately).")
5674 (defun mouse-autoselect-window-cancel (&optional force)
5675 "Cancel delayed window autoselection.
5676 Optional argument FORCE means cancel unconditionally."
5677 (unless (and (not force)
5678 ;; Don't cancel for select-window or select-frame events
5679 ;; or when the user drags a scroll bar.
5680 (or (memq this-command
5681 '(handle-select-window handle-switch-frame))
5682 (and (eq this-command 'scroll-bar-toolkit-scroll)
5683 (memq (nth 4 (event-end last-input-event))
5684 '(handle end-scroll)))))
5685 (setq mouse-autoselect-window-state nil)
5686 (when (timerp mouse-autoselect-window-timer)
5687 (cancel-timer mouse-autoselect-window-timer))
5688 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
5690 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
5691 "Start delayed window autoselection.
5692 MOUSE-POSITION is the last position where the mouse was seen as returned
5693 by `mouse-position'. Optional argument WINDOW non-nil denotes the
5694 window where the mouse was seen. Optional argument SUSPEND non-nil
5695 means suspend autoselection."
5696 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
5697 (setq mouse-autoselect-window-position mouse-position)
5698 (when window (setq mouse-autoselect-window-window window))
5699 (setq mouse-autoselect-window-state (when suspend 'suspend))
5700 ;; Install timer which runs `mouse-autoselect-window-select' after
5701 ;; `mouse-autoselect-window' seconds.
5702 (setq mouse-autoselect-window-timer
5703 (run-at-time
5704 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
5706 (defun mouse-autoselect-window-select ()
5707 "Select window with delayed window autoselection.
5708 If the mouse position has stabilized in a non-selected window, select
5709 that window. The minibuffer window is selected only if the minibuffer
5710 is active. This function is run by `mouse-autoselect-window-timer'."
5711 (ignore-errors
5712 (let* ((mouse-position (mouse-position))
5713 (window
5714 (ignore-errors
5715 (window-at (cadr mouse-position) (cddr mouse-position)
5716 (car mouse-position)))))
5717 (cond
5718 ((or (menu-or-popup-active-p)
5719 (and window
5720 (not (coordinates-in-window-p (cdr mouse-position) window))))
5721 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
5722 ;; of WINDOW, temporarily suspend delayed autoselection.
5723 (mouse-autoselect-window-start mouse-position nil t))
5724 ((eq mouse-autoselect-window-state 'suspend)
5725 ;; Delayed autoselection was temporarily suspended, reenable it.
5726 (mouse-autoselect-window-start mouse-position))
5727 ((and window (not (eq window (selected-window)))
5728 (or (not (numberp mouse-autoselect-window))
5729 (and (> mouse-autoselect-window 0)
5730 ;; If `mouse-autoselect-window' is positive, select
5731 ;; window if the window is the same as before.
5732 (eq window mouse-autoselect-window-window))
5733 ;; Otherwise select window if the mouse is at the same
5734 ;; position as before. Observe that the first test after
5735 ;; starting autoselection usually fails since the value of
5736 ;; `mouse-autoselect-window-position' recorded there is the
5737 ;; position where the mouse has entered the new window and
5738 ;; not necessarily where the mouse has stopped moving.
5739 (equal mouse-position mouse-autoselect-window-position))
5740 ;; The minibuffer is a candidate window if it's active.
5741 (or (not (window-minibuffer-p window))
5742 (eq window (active-minibuffer-window))))
5743 ;; Mouse position has stabilized in non-selected window: Cancel
5744 ;; delayed autoselection and try to select that window.
5745 (mouse-autoselect-window-cancel t)
5746 ;; Select window where mouse appears unless the selected window is the
5747 ;; minibuffer. Use `unread-command-events' in order to execute pre-
5748 ;; and post-command hooks and trigger idle timers. To avoid delaying
5749 ;; autoselection again, set `mouse-autoselect-window-state'."
5750 (unless (window-minibuffer-p (selected-window))
5751 (setq mouse-autoselect-window-state 'select)
5752 (setq unread-command-events
5753 (cons (list 'select-window (list window))
5754 unread-command-events))))
5755 ((or (and window (eq window (selected-window)))
5756 (not (numberp mouse-autoselect-window))
5757 (equal mouse-position mouse-autoselect-window-position))
5758 ;; Mouse position has either stabilized in the selected window or at
5759 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
5760 (mouse-autoselect-window-cancel t))
5762 ;; Mouse position has not stabilized yet, resume delayed
5763 ;; autoselection.
5764 (mouse-autoselect-window-start mouse-position window))))))
5766 (defun handle-select-window (event)
5767 "Handle select-window events."
5768 (interactive "e")
5769 (let ((window (posn-window (event-start event))))
5770 (unless (or (not (window-live-p window))
5771 ;; Don't switch if we're currently in the minibuffer.
5772 ;; This tries to work around problems where the
5773 ;; minibuffer gets unselected unexpectedly, and where
5774 ;; you then have to move your mouse all the way down to
5775 ;; the minibuffer to select it.
5776 (window-minibuffer-p (selected-window))
5777 ;; Don't switch to minibuffer window unless it's active.
5778 (and (window-minibuffer-p window)
5779 (not (minibuffer-window-active-p window)))
5780 ;; Don't switch when autoselection shall be delayed.
5781 (and (numberp mouse-autoselect-window)
5782 (not (zerop mouse-autoselect-window))
5783 (not (eq mouse-autoselect-window-state 'select))
5784 (progn
5785 ;; Cancel any delayed autoselection.
5786 (mouse-autoselect-window-cancel t)
5787 ;; Start delayed autoselection from current mouse
5788 ;; position and window.
5789 (mouse-autoselect-window-start (mouse-position) window)
5790 ;; Executing a command cancels delayed autoselection.
5791 (add-hook
5792 'pre-command-hook 'mouse-autoselect-window-cancel))))
5793 (when mouse-autoselect-window
5794 ;; Reset state of delayed autoselection.
5795 (setq mouse-autoselect-window-state nil)
5796 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
5797 (run-hooks 'mouse-leave-buffer-hook))
5798 ;; Clear echo area.
5799 (message nil)
5800 (select-window window))))
5802 (defun truncated-partial-width-window-p (&optional window)
5803 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
5804 WINDOW defaults to the selected window.
5805 Return nil if WINDOW is not a partial-width window
5806 (regardless of the value of `truncate-lines').
5807 Otherwise, consult the value of `truncate-partial-width-windows'
5808 for the buffer shown in WINDOW."
5809 (unless window
5810 (setq window (selected-window)))
5811 (unless (window-full-width-p window)
5812 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
5813 (window-buffer window))))
5814 (if (integerp t-p-w-w)
5815 (< (window-width window) t-p-w-w)
5816 t-p-w-w))))
5818 ;; Some of these are in tutorial--default-keys, so update that if you
5819 ;; change these.
5820 (define-key ctl-x-map "0" 'delete-window)
5821 (define-key ctl-x-map "1" 'delete-other-windows)
5822 (define-key ctl-x-map "2" 'split-window-below)
5823 (define-key ctl-x-map "3" 'split-window-right)
5824 (define-key ctl-x-map "o" 'other-window)
5825 (define-key ctl-x-map "^" 'enlarge-window)
5826 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
5827 (define-key ctl-x-map "{" 'shrink-window-horizontally)
5828 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
5829 (define-key ctl-x-map "+" 'balance-windows)
5830 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
5832 ;;; window.el ends here