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