Merge from emacs-24 branch; up to 2012-05-01T18:47:23Z!rgm@gnu.org
[emacs.git] / lisp / window.el
blobd58fa81a8deb12ad21b32a1c0fa017fe27354453
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 (and (integerp (nth 3 quad))
3073 (/= (nth 3 quad) (window-total-size window))))
3074 (set-window-dedicated-p window nil)
3075 (when resize
3076 ;; Try to resize WINDOW to its old height but don't signal an
3077 ;; error.
3078 (condition-case nil
3079 (window-resize window (- (nth 3 quad) (window-total-size window)))
3080 (error nil)))
3081 ;; Restore WINDOW's previous buffer, start and point position.
3082 (set-window-buffer-start-and-point
3083 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
3084 ;; Unrecord WINDOW's buffer here (Bug#9937) to make sure it's not
3085 ;; re-recorded by `set-window-buffer'.
3086 (unrecord-window-buffer window buffer)
3087 ;; Reset the quit-restore parameter.
3088 (set-window-parameter window 'quit-restore nil)
3089 ;; Select old window.
3090 (when (window-live-p (nth 2 quit-restore))
3091 (select-window (nth 2 quit-restore))))
3093 ;; Show some other buffer in WINDOW and reset the quit-restore
3094 ;; parameter.
3095 (set-window-parameter window 'quit-restore nil)
3096 ;; Make sure that WINDOW is no more dedicated.
3097 (set-window-dedicated-p window nil)
3098 (switch-to-prev-buffer window 'bury-or-kill)))
3100 ;; Kill WINDOW's old-buffer if requested
3101 (if kill
3102 (kill-buffer buffer)
3103 (bury-buffer-internal buffer))))
3105 (defun quit-windows-on (&optional buffer-or-name kill frame)
3106 "Quit all windows showing BUFFER-OR-NAME.
3107 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3108 and defaults to the current buffer. Optional argument KILL
3109 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
3110 BUFFER-OR-NAME. Optional argument FRAME is handled as by
3111 `delete-windows-on'.
3113 This function calls `quit-window' on all candidate windows
3114 showing BUFFER-OR-NAME."
3115 (interactive "BQuit windows on (buffer):\nP")
3116 (let ((buffer (window-normalize-buffer buffer-or-name))
3117 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3118 ;; `window-list-1' based function.
3119 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
3120 (dolist (window (window-list-1 nil nil all-frames))
3121 (if (eq (window-buffer window) buffer)
3122 (quit-window kill window)
3123 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3124 (unrecord-window-buffer window buffer)))))
3126 ;;; Splitting windows.
3127 (defun window-split-min-size (&optional horizontal)
3128 "Return minimum height of any window when splitting windows.
3129 Optional argument HORIZONTAL non-nil means return minimum width."
3130 (if horizontal
3131 (max window-min-width window-safe-min-width)
3132 (max window-min-height window-safe-min-height)))
3134 (defun split-window (&optional window size side)
3135 "Make a new window adjacent to WINDOW.
3136 WINDOW can be any window and defaults to the selected one.
3137 Return the new window which is always a live window.
3139 Optional argument SIZE a positive number means make WINDOW SIZE
3140 lines or columns tall. If SIZE is negative, make the new window
3141 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
3142 absolute value can be less than `window-min-height' or
3143 `window-min-width'; so this command can make a new window as
3144 small as one line or two columns. SIZE defaults to half of
3145 WINDOW's size. Interactively, SIZE is the prefix argument.
3147 Optional third argument SIDE nil (or `below') specifies that the
3148 new window shall be located below WINDOW. SIDE `above' means the
3149 new window shall be located above WINDOW. In both cases SIZE
3150 specifies the new number of lines for WINDOW (or the new window
3151 if SIZE is negative) including space reserved for the mode and/or
3152 header line.
3154 SIDE t (or `right') specifies that the new window shall be
3155 located on the right side of WINDOW. SIDE `left' means the new
3156 window shall be located on the left of WINDOW. In both cases
3157 SIZE specifies the new number of columns for WINDOW (or the new
3158 window provided SIZE is negative) including space reserved for
3159 fringes and the scrollbar or a divider column. Any other non-nil
3160 value for SIDE is currently handled like t (or `right').
3162 If the variable `ignore-window-parameters' is non-nil or the
3163 `split-window' parameter of WINDOW equals t, do not process any
3164 parameters of WINDOW. Otherwise, if the `split-window' parameter
3165 of WINDOW specifies a function, call that function with all three
3166 arguments and return the value returned by that function.
3168 Otherwise, if WINDOW is part of an atomic window, \"split\" the
3169 root of that atomic window. The new window does not become a
3170 member of that atomic window.
3172 If WINDOW is live, properties of the new window like margins and
3173 scrollbars are inherited from WINDOW. If WINDOW is an internal
3174 window, these properties as well as the buffer displayed in the
3175 new window are inherited from the window selected on WINDOW's
3176 frame. The selected window is not changed by this function."
3177 (interactive "i")
3178 (setq window (window-normalize-window window))
3179 (let* ((side (cond
3180 ((not side) 'below)
3181 ((memq side '(below above right left)) side)
3182 (t 'right)))
3183 (horizontal (not (memq side '(nil below above))))
3184 (frame (window-frame window))
3185 (parent (window-parent window))
3186 (function (window-parameter window 'split-window))
3187 (window-side (window-parameter window 'window-side))
3188 ;; Rebind `window-combination-limit' since in some cases we may
3189 ;; have to override its value.
3190 (window-combination-limit window-combination-limit)
3191 atom-root)
3193 (window--check frame)
3194 (catch 'done
3195 (cond
3196 ;; Ignore window parameters if either `ignore-window-parameters'
3197 ;; is t or the `split-window' parameter equals t.
3198 ((or ignore-window-parameters (eq function t)))
3199 ((functionp function)
3200 ;; The `split-window' parameter specifies the function to call.
3201 ;; If that function is `ignore', do nothing.
3202 (throw 'done (funcall function window size side)))
3203 ;; If WINDOW is part of an atomic window, split the root window
3204 ;; of that atomic window instead.
3205 ((and (window-parameter window 'window-atom)
3206 (setq atom-root (window-atom-root window))
3207 (not (eq atom-root window)))
3208 (throw 'done (split-window atom-root size side))))
3210 (when (and window-side
3211 (or (not parent)
3212 (not (window-parameter parent 'window-side))))
3213 ;; WINDOW is a side root window. To make sure that a new parent
3214 ;; window gets created set `window-combination-limit' to t.
3215 (setq window-combination-limit t))
3217 (when (and window-combination-resize size (> size 0))
3218 ;; If `window-combination-resize' is non-nil and SIZE is a
3219 ;; non-negative integer, we cannot reasonably resize other
3220 ;; windows. Rather bind `window-combination-limit' to t to make
3221 ;; sure that subsequent window deletions are handled correctly.
3222 (setq window-combination-limit t))
3224 (let* ((parent-size
3225 ;; `parent-size' is the size of WINDOW's parent, provided
3226 ;; it has one.
3227 (when parent (window-total-size parent horizontal)))
3228 ;; `resize' non-nil means we are supposed to resize other
3229 ;; windows in WINDOW's combination.
3230 (resize
3231 (and window-combination-resize (not window-combination-limit)
3232 ;; Resize makes sense in iso-combinations only.
3233 (window-combined-p window horizontal)))
3234 ;; `old-size' is the current size of WINDOW.
3235 (old-size (window-total-size window horizontal))
3236 ;; `new-size' is the specified or calculated size of the
3237 ;; new window.
3238 (new-size
3239 (cond
3240 ((not size)
3241 (max (window-split-min-size horizontal)
3242 (if resize
3243 ;; When resizing try to give the new window the
3244 ;; average size of a window in its combination.
3245 (min (- parent-size
3246 (window-min-size parent horizontal))
3247 (/ parent-size
3248 (1+ (window-combinations
3249 parent horizontal))))
3250 ;; Else try to give the new window half the size
3251 ;; of WINDOW (plus an eventual odd line).
3252 (+ (/ old-size 2) (% old-size 2)))))
3253 ((>= size 0)
3254 ;; SIZE non-negative specifies the new size of WINDOW.
3256 ;; Note: Specifying a non-negative SIZE is practically
3257 ;; always done as workaround for making the new window
3258 ;; appear above or on the left of the new window (the
3259 ;; ispell window is a typical example of that). In all
3260 ;; these cases the SIDE argument should be set to 'above
3261 ;; or 'left in order to support the 'resize option.
3262 ;; Here we have to nest the windows instead, see above.
3263 (- old-size size))
3265 ;; SIZE negative specifies the size of the new window.
3266 (- size))))
3267 new-parent new-normal)
3269 ;; Check SIZE.
3270 (cond
3271 ((not size)
3272 (cond
3273 (resize
3274 ;; SIZE unspecified, resizing.
3275 (when (and (not (window-sizable-p parent (- new-size) horizontal))
3276 ;; Try again with minimum split size.
3277 (setq new-size
3278 (max new-size (window-split-min-size horizontal)))
3279 (not (window-sizable-p parent (- new-size) horizontal)))
3280 (error "Window %s too small for splitting" parent)))
3281 ((> (+ new-size (window-min-size window horizontal)) old-size)
3282 ;; SIZE unspecified, no resizing.
3283 (error "Window %s too small for splitting" window))))
3284 ((and (>= size 0)
3285 (or (>= size old-size)
3286 (< new-size (if horizontal
3287 window-safe-min-width
3288 window-safe-min-width))))
3289 ;; SIZE specified as new size of old window. If the new size
3290 ;; is larger than the old size or the size of the new window
3291 ;; would be less than the safe minimum, signal an error.
3292 (error "Window %s too small for splitting" window))
3293 (resize
3294 ;; SIZE specified, resizing.
3295 (unless (window-sizable-p parent (- new-size) horizontal)
3296 ;; If we cannot resize the parent give up.
3297 (error "Window %s too small for splitting" parent)))
3298 ((or (< new-size
3299 (if horizontal window-safe-min-width window-safe-min-height))
3300 (< (- old-size new-size)
3301 (if horizontal window-safe-min-width window-safe-min-height)))
3302 ;; SIZE specification violates minimum size restrictions.
3303 (error "Window %s too small for splitting" window)))
3305 (window--resize-reset frame horizontal)
3307 (setq new-parent
3308 ;; Make new-parent non-nil if we need a new parent window;
3309 ;; either because we want to nest or because WINDOW is not
3310 ;; iso-combined.
3311 (or window-combination-limit
3312 (not (window-combined-p window horizontal))))
3313 (setq new-normal
3314 ;; Make new-normal the normal size of the new window.
3315 (cond
3316 (size (/ (float new-size) (if new-parent old-size parent-size)))
3317 (new-parent 0.5)
3318 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
3319 (t (/ (window-normal-size window horizontal) 2.0))))
3321 (if resize
3322 ;; Try to get space from OLD's siblings. We could go "up" and
3323 ;; try getting additional space from surrounding windows but
3324 ;; we won't be able to return space to those windows when we
3325 ;; delete the one we create here. Hence we do not go up.
3326 (progn
3327 (window--resize-child-windows parent (- new-size) horizontal)
3328 (let* ((normal (- 1.0 new-normal))
3329 (sub (window-child parent)))
3330 (while sub
3331 (set-window-new-normal
3332 sub (* (window-normal-size sub horizontal) normal))
3333 (setq sub (window-right sub)))))
3334 ;; Get entire space from WINDOW.
3335 (set-window-new-total window (- old-size new-size))
3336 (window--resize-this-window window (- new-size) horizontal)
3337 (set-window-new-normal
3338 window (- (if new-parent 1.0 (window-normal-size window horizontal))
3339 new-normal)))
3341 (let* ((new (split-window-internal window new-size side new-normal)))
3342 ;; Inherit window-side parameters, if any.
3343 (when (and window-side new-parent)
3344 (set-window-parameter (window-parent new) 'window-side window-side)
3345 (set-window-parameter new 'window-side window-side))
3347 (run-window-configuration-change-hook frame)
3348 (window--check frame)
3349 ;; Always return the new window.
3350 new)))))
3352 ;; I think this should be the default; I think people will prefer it--rms.
3353 (defcustom split-window-keep-point t
3354 "If non-nil, \\[split-window-below] preserves point in the new window.
3355 If nil, adjust point in the two windows to minimize redisplay.
3356 This option applies only to `split-window-below' and functions
3357 that call it. The low-level `split-window' function always keeps
3358 the original point in both windows."
3359 :type 'boolean
3360 :group 'windows)
3362 (defun split-window-below (&optional size)
3363 "Split the selected window into two windows, one above the other.
3364 The selected window is above. The newly split-off window is
3365 below, and displays the same buffer. Return the new window.
3367 If optional argument SIZE is omitted or nil, both windows get the
3368 same height, or close to it. If SIZE is positive, the upper
3369 \(selected) window gets SIZE lines. If SIZE is negative, the
3370 lower (new) window gets -SIZE lines.
3372 If the variable `split-window-keep-point' is non-nil, both
3373 windows get the same value of point as the selected window.
3374 Otherwise, the window starts are chosen so as to minimize the
3375 amount of redisplay; this is convenient on slow terminals."
3376 (interactive "P")
3377 (let ((old-window (selected-window))
3378 (old-point (window-point-1))
3379 (size (and size (prefix-numeric-value size)))
3380 moved-by-window-height moved new-window bottom)
3381 (when (and size (< size 0) (< (- size) window-min-height))
3382 ;; `split-window' would not signal an error here.
3383 (error "Size of new window too small"))
3384 (setq new-window (split-window nil size))
3385 (unless split-window-keep-point
3386 (with-current-buffer (window-buffer)
3387 ;; Use `save-excursion' around vertical movements below
3388 ;; (Bug#10971). Note: When the selected window's buffer has a
3389 ;; header line, up to two lines of the buffer may not show up
3390 ;; in the resulting configuration.
3391 (save-excursion
3392 (goto-char (window-start))
3393 (setq moved (vertical-motion (window-height)))
3394 (set-window-start new-window (point))
3395 (when (> (point) (window-point new-window))
3396 (set-window-point new-window (point)))
3397 (when (= moved (window-height))
3398 (setq moved-by-window-height t)
3399 (vertical-motion -1))
3400 (setq bottom (point)))
3401 (and moved-by-window-height
3402 (<= bottom (point))
3403 (set-window-point-1 old-window (1- bottom)))
3404 (and moved-by-window-height
3405 (<= (window-start new-window) old-point)
3406 (set-window-point new-window old-point)
3407 (select-window new-window))))
3408 ;; Always copy quit-restore parameter in interactive use.
3409 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3410 (when quit-restore
3411 (set-window-parameter new-window 'quit-restore quit-restore)))
3412 new-window))
3414 (defalias 'split-window-vertically 'split-window-below)
3416 (defun split-window-right (&optional size)
3417 "Split the selected window into two side-by-side windows.
3418 The selected window is on the left. The newly split-off window
3419 is on the right, and displays the same buffer. Return the new
3420 window.
3422 If optional argument SIZE is omitted or nil, both windows get the
3423 same width, or close to it. If SIZE is positive, the left-hand
3424 \(selected) window gets SIZE columns. If SIZE is negative, the
3425 right-hand (new) window gets -SIZE columns. Here, SIZE includes
3426 the width of the window's scroll bar; if there are no scroll
3427 bars, it includes the width of the divider column to the window's
3428 right, if any."
3429 (interactive "P")
3430 (let ((old-window (selected-window))
3431 (size (and size (prefix-numeric-value size)))
3432 new-window)
3433 (when (and size (< size 0) (< (- size) window-min-width))
3434 ;; `split-window' would not signal an error here.
3435 (error "Size of new window too small"))
3436 (setq new-window (split-window nil size t))
3437 ;; Always copy quit-restore parameter in interactive use.
3438 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3439 (when quit-restore
3440 (set-window-parameter new-window 'quit-restore quit-restore)))
3441 new-window))
3443 (defalias 'split-window-horizontally 'split-window-right)
3445 ;;; Balancing windows.
3447 ;; The following routine uses the recycled code from an old version of
3448 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
3449 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
3450 ;; more readable (FWIW we'd need three loops - one to calculate the
3451 ;; minimum sizes per window, one to enlarge or shrink windows until the
3452 ;; new parent-size matches, and one where we shrink the largest/enlarge
3453 ;; the smallest window).
3454 (defun balance-windows-2 (window horizontal)
3455 "Subroutine of `balance-windows-1'.
3456 WINDOW must be a vertical combination (horizontal if HORIZONTAL
3457 is non-nil."
3458 (let* ((first (window-child window))
3459 (sub first)
3460 (number-of-children 0)
3461 (parent-size (window-new-total window))
3462 (total-sum parent-size)
3463 failed size sub-total sub-delta sub-amount rest)
3464 (while sub
3465 (setq number-of-children (1+ number-of-children))
3466 (when (window-size-fixed-p sub horizontal)
3467 (setq total-sum
3468 (- total-sum (window-total-size sub horizontal)))
3469 (set-window-new-normal sub 'ignore))
3470 (setq sub (window-right sub)))
3472 (setq failed t)
3473 (while (and failed (> number-of-children 0))
3474 (setq size (/ total-sum number-of-children))
3475 (setq failed nil)
3476 (setq sub first)
3477 (while (and sub (not failed))
3478 ;; Ignore child windows that should be ignored or are stuck.
3479 (unless (window--resize-child-windows-skip-p sub)
3480 (setq sub-total (window-total-size sub horizontal))
3481 (setq sub-delta (- size sub-total))
3482 (setq sub-amount
3483 (window-sizable sub sub-delta horizontal))
3484 ;; Register the new total size for this child window.
3485 (set-window-new-total sub (+ sub-total sub-amount))
3486 (unless (= sub-amount sub-delta)
3487 (setq total-sum (- total-sum sub-total sub-amount))
3488 (setq number-of-children (1- number-of-children))
3489 ;; We failed and need a new round.
3490 (setq failed t)
3491 (set-window-new-normal sub 'skip)))
3492 (setq sub (window-right sub))))
3494 (setq rest (% total-sum number-of-children))
3495 ;; Fix rounding by trying to enlarge non-stuck windows by one line
3496 ;; (column) until `rest' is zero.
3497 (setq sub first)
3498 (while (and sub (> rest 0))
3499 (unless (window--resize-child-windows-skip-p window)
3500 (set-window-new-total sub 1 t)
3501 (setq rest (1- rest)))
3502 (setq sub (window-right sub)))
3504 ;; Fix rounding by trying to enlarge stuck windows by one line
3505 ;; (column) until `rest' equals zero.
3506 (setq sub first)
3507 (while (and sub (> rest 0))
3508 (unless (eq (window-new-normal sub) 'ignore)
3509 (set-window-new-total sub 1 t)
3510 (setq rest (1- rest)))
3511 (setq sub (window-right sub)))
3513 (setq sub first)
3514 (while sub
3515 ;; Record new normal sizes.
3516 (set-window-new-normal
3517 sub (/ (if (eq (window-new-normal sub) 'ignore)
3518 (window-total-size sub horizontal)
3519 (window-new-total sub))
3520 (float parent-size)))
3521 ;; Recursively balance each window's child windows.
3522 (balance-windows-1 sub horizontal)
3523 (setq sub (window-right sub)))))
3525 (defun balance-windows-1 (window &optional horizontal)
3526 "Subroutine of `balance-windows'."
3527 (if (window-child window)
3528 (let ((sub (window-child window)))
3529 (if (window-combined-p sub horizontal)
3530 (balance-windows-2 window horizontal)
3531 (let ((size (window-new-total window)))
3532 (while sub
3533 (set-window-new-total sub size)
3534 (balance-windows-1 sub horizontal)
3535 (setq sub (window-right sub))))))))
3537 (defun balance-windows (&optional window-or-frame)
3538 "Balance the sizes of windows of WINDOW-OR-FRAME.
3539 WINDOW-OR-FRAME is optional and defaults to the selected frame.
3540 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
3541 windows of that frame. If WINDOW-OR-FRAME denotes a window,
3542 recursively balance the sizes of all child windows of that
3543 window."
3544 (interactive)
3545 (let* ((window
3546 (cond
3547 ((or (not window-or-frame)
3548 (frame-live-p window-or-frame))
3549 (frame-root-window window-or-frame))
3550 ((or (window-live-p window-or-frame)
3551 (window-child window-or-frame))
3552 window-or-frame)
3554 (error "Not a window or frame %s" window-or-frame))))
3555 (frame (window-frame window)))
3556 ;; Balance vertically.
3557 (window--resize-reset (window-frame window))
3558 (balance-windows-1 window)
3559 (window-resize-apply frame)
3560 ;; Balance horizontally.
3561 (window--resize-reset (window-frame window) t)
3562 (balance-windows-1 window t)
3563 (window-resize-apply frame t)))
3565 (defun window-fixed-size-p (&optional window direction)
3566 "Return t if WINDOW cannot be resized in DIRECTION.
3567 WINDOW defaults to the selected window. DIRECTION can be
3568 nil (i.e. any), `height' or `width'."
3569 (with-current-buffer (window-buffer window)
3570 (when (and (boundp 'window-size-fixed) window-size-fixed)
3571 (not (and direction
3572 (member (cons direction window-size-fixed)
3573 '((height . width) (width . height))))))))
3575 ;;; A different solution to balance-windows.
3576 (defvar window-area-factor 1
3577 "Factor by which the window area should be over-estimated.
3578 This is used by `balance-windows-area'.
3579 Changing this globally has no effect.")
3580 (make-variable-buffer-local 'window-area-factor)
3582 (defun balance-windows-area-adjust (window delta horizontal)
3583 "Wrapper around `window-resize' with error checking.
3584 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
3585 ;; `window-resize' may fail if delta is too large.
3586 (while (>= (abs delta) 1)
3587 (condition-case nil
3588 (progn
3589 (window-resize window delta horizontal)
3590 (setq delta 0))
3591 (error
3592 ;;(message "adjust: %s" (error-message-string err))
3593 (setq delta (/ delta 2))))))
3595 (defun balance-windows-area ()
3596 "Make all visible windows the same area (approximately).
3597 See also `window-area-factor' to change the relative size of
3598 specific buffers."
3599 (interactive)
3600 (let* ((unchanged 0) (carry 0) (round 0)
3601 ;; Remove fixed-size windows.
3602 (wins (delq nil (mapcar (lambda (win)
3603 (if (not (window-fixed-size-p win)) win))
3604 (window-list nil 'nomini))))
3605 (changelog nil)
3606 next)
3607 ;; Resizing a window changes the size of surrounding windows in complex
3608 ;; ways, so it's difficult to balance them all. The introduction of
3609 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
3610 ;; very difficult to do. `balance-window' above takes an off-line
3611 ;; approach: get the whole window tree, then balance it, then try to
3612 ;; adjust the windows so they fit the result.
3613 ;; Here, instead, we take a "local optimization" approach, where we just
3614 ;; go through all the windows several times until nothing needs to be
3615 ;; changed. The main problem with this approach is that it's difficult
3616 ;; to make sure it terminates, so we use some heuristic to try and break
3617 ;; off infinite loops.
3618 ;; After a round without any change, we allow a second, to give a chance
3619 ;; to the carry to propagate a minor imbalance from the end back to
3620 ;; the beginning.
3621 (while (< unchanged 2)
3622 ;; (message "New round")
3623 (setq unchanged (1+ unchanged) round (1+ round))
3624 (dolist (win wins)
3625 (setq next win)
3626 (while (progn (setq next (next-window next))
3627 (window-fixed-size-p next)))
3628 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
3629 (let* ((horiz
3630 (< (car (window-edges win)) (car (window-edges next))))
3631 (areadiff (/ (- (* (window-height next) (window-width next)
3632 (buffer-local-value 'window-area-factor
3633 (window-buffer next)))
3634 (* (window-height win) (window-width win)
3635 (buffer-local-value 'window-area-factor
3636 (window-buffer win))))
3637 (max (buffer-local-value 'window-area-factor
3638 (window-buffer win))
3639 (buffer-local-value 'window-area-factor
3640 (window-buffer next)))))
3641 (edgesize (if horiz
3642 (+ (window-height win) (window-height next))
3643 (+ (window-width win) (window-width next))))
3644 (diff (/ areadiff edgesize)))
3645 (when (zerop diff)
3646 ;; Maybe diff is actually closer to 1 than to 0.
3647 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
3648 (when (and (zerop diff) (not (zerop areadiff)))
3649 (setq diff (/ (+ areadiff carry) edgesize))
3650 ;; Change things smoothly.
3651 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
3652 (if (zerop diff)
3653 ;; Make sure negligible differences don't accumulate to
3654 ;; become significant.
3655 (setq carry (+ carry areadiff))
3656 ;; This used `adjust-window-trailing-edge' before and uses
3657 ;; `window-resize' now. Error wrapping is still needed.
3658 (balance-windows-area-adjust win diff horiz)
3659 ;; (sit-for 0.5)
3660 (let ((change (cons win (window-edges win))))
3661 ;; If the same change has been seen already for this window,
3662 ;; we're most likely in an endless loop, so don't count it as
3663 ;; a change.
3664 (unless (member change changelog)
3665 (push change changelog)
3666 (setq unchanged 0 carry 0)))))))
3667 ;; We've now basically balanced all the windows.
3668 ;; But there may be some minor off-by-one imbalance left over,
3669 ;; so let's do some fine tuning.
3670 ;; (bw-finetune wins)
3671 ;; (message "Done in %d rounds" round)
3674 ;;; Window states, how to get them and how to put them in a window.
3675 (defun window--state-get-1 (window &optional writable)
3676 "Helper function for `window-state-get'."
3677 (let* ((type
3678 (cond
3679 ((window-top-child window) 'vc)
3680 ((window-left-child window) 'hc)
3681 (t 'leaf)))
3682 (buffer (window-buffer window))
3683 (selected (eq window (selected-window)))
3684 (head
3685 `(,type
3686 ,@(unless (window-next-sibling window) `((last . t)))
3687 (total-height . ,(window-total-size window))
3688 (total-width . ,(window-total-size window t))
3689 (normal-height . ,(window-normal-size window))
3690 (normal-width . ,(window-normal-size window t))
3691 (combination-limit . ,(window-combination-limit window))
3692 ,@(let ((parameters (window-parameters window))
3693 list)
3694 ;; Make copies of those window parameters whose
3695 ;; persistence property is `writable' if WRITABLE is
3696 ;; non-nil and non-nil if WRITABLE is nil.
3697 (dolist (par parameters)
3698 (let ((pers (cdr (assq (car par)
3699 window-persistent-parameters))))
3700 (when (and pers (or (not writable) (eq pers 'writable)))
3701 (setq list (cons (cons (car par) (cdr par)) list)))))
3702 ;; Add `clone-of' parameter if necessary.
3703 (let ((pers (cdr (assq 'clone-of
3704 window-persistent-parameters))))
3705 (when (and pers (or (not writable) (eq pers 'writable))
3706 (not (assq 'clone-of list)))
3707 (setq list (cons (cons 'clone-of window) list))))
3708 (when list
3709 `((parameters . ,list))))
3710 ,@(when buffer
3711 ;; All buffer related things go in here.
3712 (let ((point (window-point-1 window))
3713 (start (window-start window)))
3714 `((buffer
3715 ,(buffer-name buffer)
3716 (selected . ,selected)
3717 (hscroll . ,(window-hscroll window))
3718 (fringes . ,(window-fringes window))
3719 (margins . ,(window-margins window))
3720 (scroll-bars . ,(window-scroll-bars window))
3721 (vscroll . ,(window-vscroll window))
3722 (dedicated . ,(window-dedicated-p window))
3723 (point . ,(if writable point
3724 (copy-marker point
3725 (buffer-local-value
3726 'window-point-insertion-type
3727 buffer))))
3728 (start . ,(if writable start (copy-marker start)))))))))
3729 (tail
3730 (when (memq type '(vc hc))
3731 (let (list)
3732 (setq window (window-child window))
3733 (while window
3734 (setq list (cons (window--state-get-1 window writable) list))
3735 (setq window (window-right window)))
3736 (nreverse list)))))
3737 (append head tail)))
3739 (defun window-state-get (&optional window writable)
3740 "Return state of WINDOW as a Lisp object.
3741 WINDOW can be any window and defaults to the root window of the
3742 selected frame.
3744 Optional argument WRITABLE non-nil means do not use markers for
3745 sampling `window-point' and `window-start'. Together, WRITABLE
3746 and the variable `window-persistent-parameters' specify which
3747 window parameters are saved by this function. WRITABLE should be
3748 non-nil when the return value shall be written to a file and read
3749 back in another session. Otherwise, an application may run into
3750 an `invalid-read-syntax' error while attempting to read back the
3751 value from file.
3753 The return value can be used as argument for `window-state-put'
3754 to put the state recorded here into an arbitrary window. The
3755 value can be also stored on disk and read back in a new session."
3756 (setq window
3757 (if window
3758 (if (window-valid-p window)
3759 window
3760 (error "%s is not a live or internal window" window))
3761 (frame-root-window)))
3762 ;; The return value is a cons whose car specifies some constraints on
3763 ;; the size of WINDOW. The cdr lists the states of the child windows
3764 ;; of WINDOW.
3765 (cons
3766 ;; Frame related things would go into a function, say `frame-state',
3767 ;; calling `window-state-get' to insert the frame's root window.
3768 `((min-height . ,(window-min-size window))
3769 (min-width . ,(window-min-size window t))
3770 (min-height-ignore . ,(window-min-size window nil t))
3771 (min-width-ignore . ,(window-min-size window t t))
3772 (min-height-safe . ,(window-min-size window nil 'safe))
3773 (min-width-safe . ,(window-min-size window t 'safe)))
3774 (window--state-get-1 window writable)))
3776 (defvar window-state-put-list nil
3777 "Helper variable for `window-state-put'.")
3779 (defun window--state-put-1 (state &optional window ignore totals)
3780 "Helper function for `window-state-put'."
3781 (let ((type (car state)))
3782 (setq state (cdr state))
3783 (cond
3784 ((eq type 'leaf)
3785 ;; For a leaf window just add unprocessed entries to
3786 ;; `window-state-put-list'.
3787 (push (cons window state) window-state-put-list))
3788 ((memq type '(vc hc))
3789 (let* ((horizontal (eq type 'hc))
3790 (total (window-total-size window horizontal))
3791 (first t)
3792 size new)
3793 (dolist (item state)
3794 ;; Find the next child window. WINDOW always points to the
3795 ;; real window that we want to fill with what we find here.
3796 (when (memq (car item) '(leaf vc hc))
3797 (if (assq 'last item)
3798 ;; The last child window. Below `window--state-put-1'
3799 ;; will put into it whatever ITEM has in store.
3800 (setq new nil)
3801 ;; Not the last child window, prepare for splitting
3802 ;; WINDOW. SIZE is the new (and final) size of the old
3803 ;; window.
3804 (setq size
3805 (if totals
3806 ;; Use total size.
3807 (cdr (assq (if horizontal 'total-width 'total-height) item))
3808 ;; Use normalized size and round.
3809 (round (* total
3810 (cdr (assq
3811 (if horizontal 'normal-width 'normal-height)
3812 item))))))
3814 ;; Use safe sizes, we try to resize later.
3815 (setq size (max size (if horizontal
3816 window-safe-min-height
3817 window-safe-min-width)))
3819 (if (window-sizable-p window (- size) horizontal 'safe)
3820 (let* ((window-combination-limit
3821 (assq 'combination-limit item)))
3822 ;; We must inherit the combination limit, otherwise
3823 ;; we might mess up handling of atomic and side
3824 ;; window.
3825 (setq new (split-window window size horizontal)))
3826 ;; Give up if we can't resize window down to safe sizes.
3827 (error "Cannot resize window %s" window))
3829 (when first
3830 (setq first nil)
3831 ;; When creating the first child window add for parent
3832 ;; unprocessed entries to `window-state-put-list'.
3833 (setq window-state-put-list
3834 (cons (cons (window-parent window) state)
3835 window-state-put-list))))
3837 ;; Now process the current window (either the one we've just
3838 ;; split or the last child of its parent).
3839 (window--state-put-1 item window ignore totals)
3840 ;; Continue with the last window split off.
3841 (setq window new))))))))
3843 (defun window--state-put-2 (ignore)
3844 "Helper function for `window-state-put'."
3845 (dolist (item window-state-put-list)
3846 (let ((window (car item))
3847 (combination-limit (cdr (assq 'combination-limit item)))
3848 (parameters (cdr (assq 'parameters item)))
3849 (state (cdr (assq 'buffer item))))
3850 (when combination-limit
3851 (set-window-combination-limit window combination-limit))
3852 ;; Reset window's parameters and assign saved ones (we might want
3853 ;; a `remove-window-parameters' function here).
3854 (dolist (parameter (window-parameters window))
3855 (set-window-parameter window (car parameter) nil))
3856 (when parameters
3857 (dolist (parameter parameters)
3858 (set-window-parameter window (car parameter) (cdr parameter))))
3859 ;; Process buffer related state.
3860 (when state
3861 ;; We don't want to raise an error here so we create a buffer if
3862 ;; there's none.
3863 (set-window-buffer window (get-buffer-create (car state)))
3864 (with-current-buffer (window-buffer window)
3865 (set-window-hscroll window (cdr (assq 'hscroll state)))
3866 (apply 'set-window-fringes
3867 (cons window (cdr (assq 'fringes state))))
3868 (let ((margins (cdr (assq 'margins state))))
3869 (set-window-margins window (car margins) (cdr margins)))
3870 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
3871 (set-window-scroll-bars
3872 window (car scroll-bars) (nth 2 scroll-bars) (nth 3 scroll-bars)))
3873 (set-window-vscroll window (cdr (assq 'vscroll state)))
3874 ;; Adjust vertically.
3875 (if (memq window-size-fixed '(t height))
3876 ;; A fixed height window, try to restore the original size.
3877 (let ((delta (- (cdr (assq 'total-height item))
3878 (window-total-height window)))
3879 window-size-fixed)
3880 (when (window--resizable-p window delta)
3881 (window-resize window delta)))
3882 ;; Else check whether the window is not high enough.
3883 (let* ((min-size (window-min-size window nil ignore))
3884 (delta (- min-size (window-total-size window))))
3885 (when (and (> delta 0)
3886 (window--resizable-p window delta nil ignore))
3887 (window-resize window delta nil ignore))))
3888 ;; Adjust horizontally.
3889 (if (memq window-size-fixed '(t width))
3890 ;; A fixed width window, try to restore the original size.
3891 (let ((delta (- (cdr (assq 'total-width item))
3892 (window-total-width window)))
3893 window-size-fixed)
3894 (when (window--resizable-p window delta)
3895 (window-resize window delta)))
3896 ;; Else check whether the window is not wide enough.
3897 (let* ((min-size (window-min-size window t ignore))
3898 (delta (- min-size (window-total-size window t))))
3899 (when (and (> delta 0)
3900 (window--resizable-p window delta t ignore))
3901 (window-resize window delta t ignore))))
3902 ;; Set dedicated status.
3903 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
3904 ;; Install positions (maybe we should do this after all windows
3905 ;; have been created and sized).
3906 (ignore-errors
3907 (set-window-start window (cdr (assq 'start state)))
3908 (set-window-point window (cdr (assq 'point state))))
3909 ;; Select window if it's the selected one.
3910 (when (cdr (assq 'selected state))
3911 (select-window window)))))))
3913 (defun window-state-put (state &optional window ignore)
3914 "Put window state STATE into WINDOW.
3915 STATE should be the state of a window returned by an earlier
3916 invocation of `window-state-get'. Optional argument WINDOW must
3917 specify a live window and defaults to the selected one.
3919 Optional argument IGNORE non-nil means ignore minimum window
3920 sizes and fixed size restrictions. IGNORE equal `safe' means
3921 windows can get as small as `window-safe-min-height' and
3922 `window-safe-min-width'."
3923 (setq window (window-normalize-window window t))
3924 (let* ((frame (window-frame window))
3925 (head (car state))
3926 ;; We check here (1) whether the total sizes of root window of
3927 ;; STATE and that of WINDOW are equal so we can avoid
3928 ;; calculating new sizes, and (2) if we do have to resize
3929 ;; whether we can do so without violating size restrictions.
3930 (totals
3931 (and (= (window-total-size window)
3932 (cdr (assq 'total-height state)))
3933 (= (window-total-size window t)
3934 (cdr (assq 'total-width state)))))
3935 (min-height (cdr (assq 'min-height head)))
3936 (min-width (cdr (assq 'min-width head))))
3937 (if (and (not totals)
3938 (or (> min-height (window-total-size window))
3939 (> min-width (window-total-size window t)))
3940 (or (not ignore)
3941 (and (setq min-height
3942 (cdr (assq 'min-height-ignore head)))
3943 (setq min-width
3944 (cdr (assq 'min-width-ignore head)))
3945 (or (> min-height (window-total-size window))
3946 (> min-width (window-total-size window t)))
3947 (or (not (eq ignore 'safe))
3948 (and (setq min-height
3949 (cdr (assq 'min-height-safe head)))
3950 (setq min-width
3951 (cdr (assq 'min-width-safe head)))
3952 (or (> min-height
3953 (window-total-size window))
3954 (> min-width
3955 (window-total-size window t))))))))
3956 ;; The check above might not catch all errors due to rounding
3957 ;; issues - so IGNORE equal 'safe might not always produce the
3958 ;; minimum possible state. But such configurations hardly make
3959 ;; sense anyway.
3960 (error "Window %s too small to accommodate state" window)
3961 (setq state (cdr state))
3962 (setq window-state-put-list nil)
3963 ;; Work on the windows of a temporary buffer to make sure that
3964 ;; splitting proceeds regardless of any buffer local values of
3965 ;; `window-size-fixed'. Release that buffer after the buffers of
3966 ;; all live windows have been set by `window--state-put-2'.
3967 (with-temp-buffer
3968 (set-window-buffer window (current-buffer))
3969 (window--state-put-1 state window nil totals)
3970 (window--state-put-2 ignore))
3971 (window--check frame))))
3973 (defun display-buffer-record-window (type window buffer)
3974 "Record information for window used by `display-buffer'.
3975 TYPE specifies the type of the calling operation and must be one
3976 of the symbols 'reuse (when WINDOW existed already and was
3977 reused for displaying BUFFER), 'window (when WINDOW was created
3978 on an already existing frame), or 'frame (when WINDOW was
3979 created on a new frame). WINDOW is the window used for or created
3980 by the `display-buffer' routines. BUFFER is the buffer that
3981 shall be displayed.
3983 This function installs or updates the quit-restore parameter of
3984 WINDOW. The quit-restore parameter is a list of four elements:
3985 The first element is one of the symbols 'window, 'frame, 'same or
3986 'other. The second element is either one of the symbols 'window
3987 or 'frame or a list whose elements are the buffer previously
3988 shown in the window, that buffer's window start and window point,
3989 and the window's height. The third element is the window
3990 selected at the time the parameter was created. The fourth
3991 element is BUFFER."
3992 (cond
3993 ((eq type 'reuse)
3994 (if (eq (window-buffer window) buffer)
3995 ;; WINDOW shows BUFFER already.
3996 (when (consp (window-parameter window 'quit-restore))
3997 ;; If WINDOW has a quit-restore parameter, reset its car.
3998 (setcar (window-parameter window 'quit-restore) 'same))
3999 ;; WINDOW shows another buffer.
4000 (set-window-parameter
4001 window 'quit-restore
4002 (list 'other
4003 ;; A quadruple of WINDOW's buffer, start, point and height.
4004 (list (window-buffer window) (window-start window)
4005 (window-point-1 window) (window-total-size window))
4006 (selected-window) buffer))))
4007 ((eq type 'window)
4008 ;; WINDOW has been created on an existing frame.
4009 (set-window-parameter
4010 window 'quit-restore
4011 (list 'window 'window (selected-window) buffer)))
4012 ((eq type 'frame)
4013 ;; WINDOW has been created on a new frame.
4014 (set-window-parameter
4015 window 'quit-restore
4016 (list 'frame 'frame (selected-window) buffer)))))
4018 (defcustom display-buffer-function nil
4019 "If non-nil, function to call to handle `display-buffer'.
4020 It will receive two args, the buffer and a flag which if non-nil
4021 means that the currently selected window is not acceptable. It
4022 should choose or create a window, display the specified buffer in
4023 it, and return the window.
4025 The specified function should call `display-buffer-record-window'
4026 with corresponding arguments to set up the quit-restore parameter
4027 of the window used."
4028 :type '(choice
4029 (const nil)
4030 (function :tag "function"))
4031 :group 'windows)
4033 (defcustom pop-up-frame-alist nil
4034 "Alist of parameters for automatically generated new frames.
4035 You can set this in your init file; for example,
4037 (setq pop-up-frame-alist '((width . 80) (height . 20)))
4039 If non-nil, the value you specify here is used by the default
4040 `pop-up-frame-function' for the creation of new frames.
4042 Since `pop-up-frame-function' is used by `display-buffer' for
4043 making new frames, any value specified here by default affects
4044 the automatic generation of new frames via `display-buffer' and
4045 all functions based on it. The behavior of `make-frame' is not
4046 affected by this variable."
4047 :type '(repeat (cons :format "%v"
4048 (symbol :tag "Parameter")
4049 (sexp :tag "Value")))
4050 :group 'frames)
4052 (defcustom pop-up-frame-function
4053 (lambda () (make-frame pop-up-frame-alist))
4054 "Function used by `display-buffer' for creating a new frame.
4055 This function is called with no arguments and should return a new
4056 frame. The default value calls `make-frame' with the argument
4057 `pop-up-frame-alist'."
4058 :type 'function
4059 :group 'frames)
4061 (defcustom special-display-buffer-names nil
4062 "List of names of buffers that should be displayed specially.
4063 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4064 its name is in this list, displays the buffer in a way specified
4065 by `special-display-function'. `special-display-popup-frame'
4066 \(the default for `special-display-function') usually displays
4067 the buffer in a separate frame made with the parameters specified
4068 by `special-display-frame-alist'. If `special-display-function'
4069 has been set to some other function, that function is called with
4070 the buffer as first, and nil as second argument.
4072 Alternatively, an element of this list can be specified as
4073 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
4074 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4075 `special-display-popup-frame' will interpret such pairs as frame
4076 parameters when it creates a special frame, overriding the
4077 corresponding values from `special-display-frame-alist'.
4079 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4080 `special-display-popup-frame' displays that buffer in the
4081 selected window. If FRAME-PARAMETERS contains (same-frame . t),
4082 it displays that buffer in a window on the selected frame.
4084 If `special-display-function' specifies some other function than
4085 `special-display-popup-frame', that function is called with the
4086 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
4087 argument.
4089 Finally, an element of this list can be also specified as
4090 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
4091 `special-display-popup-frame' will call FUNCTION with the buffer
4092 named BUFFER-NAME as first argument, and OTHER-ARGS as the
4093 second.
4095 Any alternative function specified here is responsible for
4096 setting up the quit-restore parameter of the window used.
4098 If this variable appears \"not to work\", because you added a
4099 name to it but the corresponding buffer is displayed in the
4100 selected window, look at the values of `same-window-buffer-names'
4101 and `same-window-regexps'. Those variables take precedence over
4102 this one.
4104 See also `special-display-regexps'."
4105 :type '(repeat
4106 (choice :tag "Buffer"
4107 :value ""
4108 (string :format "%v")
4109 (cons :tag "With parameters"
4110 :format "%v"
4111 :value ("" . nil)
4112 (string :format "%v")
4113 (repeat :tag "Parameters"
4114 (cons :format "%v"
4115 (symbol :tag "Parameter")
4116 (sexp :tag "Value"))))
4117 (list :tag "With function"
4118 :format "%v"
4119 :value ("" . nil)
4120 (string :format "%v")
4121 (function :tag "Function")
4122 (repeat :tag "Arguments" (sexp)))))
4123 :group 'windows
4124 :group 'frames)
4126 ;;;###autoload
4127 (put 'special-display-buffer-names 'risky-local-variable t)
4129 (defcustom special-display-regexps nil
4130 "List of regexps saying which buffers should be displayed specially.
4131 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4132 any regexp in this list matches its name, displays it specially
4133 using `special-display-function'. `special-display-popup-frame'
4134 \(the default for `special-display-function') usually displays
4135 the buffer in a separate frame made with the parameters specified
4136 by `special-display-frame-alist'. If `special-display-function'
4137 has been set to some other function, that function is called with
4138 the buffer as first, and nil as second argument.
4140 Alternatively, an element of this list can be specified as
4141 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
4142 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4143 `special-display-popup-frame' will then interpret these pairs as
4144 frame parameters when creating a special frame for a buffer whose
4145 name matches REGEXP, overriding the corresponding values from
4146 `special-display-frame-alist'.
4148 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4149 `special-display-popup-frame' displays buffers matching REGEXP in
4150 the selected window. (same-frame . t) in FRAME-PARAMETERS means
4151 to display such buffers in a window on the selected frame.
4153 If `special-display-function' specifies some other function than
4154 `special-display-popup-frame', that function is called with the
4155 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
4156 as second argument.
4158 Finally, an element of this list can be also specified as
4159 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
4160 will then call FUNCTION with the buffer whose name matched
4161 REGEXP as first, and OTHER-ARGS as second argument.
4163 Any alternative function specified here is responsible for
4164 setting up the quit-restore parameter of the window used.
4166 If this variable appears \"not to work\", because you added a
4167 name to it but the corresponding buffer is displayed in the
4168 selected window, look at the values of `same-window-buffer-names'
4169 and `same-window-regexps'. Those variables take precedence over
4170 this one.
4172 See also `special-display-buffer-names'."
4173 :type '(repeat
4174 (choice :tag "Buffer"
4175 :value ""
4176 (regexp :format "%v")
4177 (cons :tag "With parameters"
4178 :format "%v"
4179 :value ("" . nil)
4180 (regexp :format "%v")
4181 (repeat :tag "Parameters"
4182 (cons :format "%v"
4183 (symbol :tag "Parameter")
4184 (sexp :tag "Value"))))
4185 (list :tag "With function"
4186 :format "%v"
4187 :value ("" . nil)
4188 (regexp :format "%v")
4189 (function :tag "Function")
4190 (repeat :tag "Arguments" (sexp)))))
4191 :group 'windows
4192 :group 'frames)
4194 (defun special-display-p (buffer-name)
4195 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
4196 More precisely, return t if `special-display-buffer-names' or
4197 `special-display-regexps' contain a string entry equaling or
4198 matching BUFFER-NAME. If `special-display-buffer-names' or
4199 `special-display-regexps' contain a list entry whose car equals
4200 or matches BUFFER-NAME, the return value is the cdr of that
4201 entry."
4202 (let (tmp)
4203 (cond
4204 ((member buffer-name special-display-buffer-names)
4206 ((setq tmp (assoc buffer-name special-display-buffer-names))
4207 (cdr tmp))
4208 ((catch 'found
4209 (dolist (regexp special-display-regexps)
4210 (cond
4211 ((stringp regexp)
4212 (when (string-match-p regexp buffer-name)
4213 (throw 'found t)))
4214 ((and (consp regexp) (stringp (car regexp))
4215 (string-match-p (car regexp) buffer-name))
4216 (throw 'found (cdr regexp))))))))))
4218 (defcustom special-display-frame-alist
4219 '((height . 14) (width . 80) (unsplittable . t))
4220 "Alist of parameters for special frames.
4221 Special frames are used for buffers whose names are listed in
4222 `special-display-buffer-names' and for buffers whose names match
4223 one of the regular expressions in `special-display-regexps'.
4225 This variable can be set in your init file, like this:
4227 (setq special-display-frame-alist '((width . 80) (height . 20)))
4229 These supersede the values given in `default-frame-alist'."
4230 :type '(repeat (cons :format "%v"
4231 (symbol :tag "Parameter")
4232 (sexp :tag "Value")))
4233 :group 'frames)
4235 (defun special-display-popup-frame (buffer &optional args)
4236 "Display BUFFER and return the window chosen.
4237 If BUFFER is already displayed in a visible or iconified frame,
4238 raise that frame. Otherwise, display BUFFER in a new frame.
4240 Optional argument ARGS is a list specifying additional
4241 information.
4243 If ARGS is an alist, use it as a list of frame parameters. If
4244 these parameters contain (same-window . t), display BUFFER in
4245 the selected window. If they contain (same-frame . t), display
4246 BUFFER in a window of the selected frame.
4248 If ARGS is a list whose car is a symbol, use (car ARGS) as a
4249 function to do the work. Pass it BUFFER as first argument,
4250 and (cdr ARGS) as second."
4251 (if (and args (symbolp (car args)))
4252 (apply (car args) buffer (cdr args))
4253 (let ((window (get-buffer-window buffer 0)))
4255 ;; If we have a window already, make it visible.
4256 (when window
4257 (let ((frame (window-frame window)))
4258 (make-frame-visible frame)
4259 (raise-frame frame)
4260 (display-buffer-record-window 'reuse window buffer)
4261 window))
4262 ;; Reuse the current window if the user requested it.
4263 (when (cdr (assq 'same-window args))
4264 (condition-case nil
4265 (progn (switch-to-buffer buffer nil t) (selected-window))
4266 (error nil)))
4267 ;; Stay on the same frame if requested.
4268 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
4269 (let* ((pop-up-windows t)
4270 pop-up-frames
4271 special-display-buffer-names special-display-regexps)
4272 (display-buffer buffer)))
4273 ;; If no window yet, make one in a new frame.
4274 (let* ((frame
4275 (with-current-buffer buffer
4276 (make-frame (append args special-display-frame-alist))))
4277 (window (frame-selected-window frame)))
4278 (display-buffer-record-window 'frame window buffer)
4279 (set-window-dedicated-p window t)
4280 window)))))
4282 (defcustom special-display-function 'special-display-popup-frame
4283 "Function to call for displaying special buffers.
4284 This function is called with two arguments - the buffer and,
4285 optionally, a list - and should return a window displaying that
4286 buffer. The default value usually makes a separate frame for the
4287 buffer using `special-display-frame-alist' to specify the frame
4288 parameters. See the definition of `special-display-popup-frame'
4289 for how to specify such a function.
4291 A buffer is special when its name is either listed in
4292 `special-display-buffer-names' or matches a regexp in
4293 `special-display-regexps'.
4295 The specified function should call `display-buffer-record-window'
4296 with corresponding arguments to set up the quit-restore parameter
4297 of the window used."
4298 :type 'function
4299 :group 'frames)
4301 (defcustom same-window-buffer-names nil
4302 "List of names of buffers that should appear in the \"same\" window.
4303 `display-buffer' and `pop-to-buffer' show a buffer whose name is
4304 on this list in the selected rather than some other window.
4306 An element of this list can be a cons cell instead of just a
4307 string. In that case, the cell's car must be a string specifying
4308 the buffer name. This is for compatibility with
4309 `special-display-buffer-names'; the cdr of the cons cell is
4310 ignored.
4312 See also `same-window-regexps'."
4313 :type '(repeat (string :format "%v"))
4314 :group 'windows)
4316 (defcustom same-window-regexps nil
4317 "List of regexps saying which buffers should appear in the \"same\" window.
4318 `display-buffer' and `pop-to-buffer' show a buffer whose name
4319 matches a regexp on this list in the selected rather than some
4320 other window.
4322 An element of this list can be a cons cell instead of just a
4323 string. In that case, the cell's car must be a regexp matching
4324 the buffer name. This is for compatibility with
4325 `special-display-regexps'; the cdr of the cons cell is ignored.
4327 See also `same-window-buffer-names'."
4328 :type '(repeat (regexp :format "%v"))
4329 :group 'windows)
4331 (defun same-window-p (buffer-name)
4332 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
4333 This function returns non-nil if `display-buffer' or
4334 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
4335 selected rather than (as usual) some other window. See
4336 `same-window-buffer-names' and `same-window-regexps'."
4337 (cond
4338 ((not (stringp buffer-name)))
4339 ;; The elements of `same-window-buffer-names' can be buffer
4340 ;; names or cons cells whose cars are buffer names.
4341 ((member buffer-name same-window-buffer-names))
4342 ((assoc buffer-name same-window-buffer-names))
4343 ((catch 'found
4344 (dolist (regexp same-window-regexps)
4345 ;; The elements of `same-window-regexps' can be regexps
4346 ;; or cons cells whose cars are regexps.
4347 (when (or (and (stringp regexp)
4348 (string-match-p regexp buffer-name))
4349 (and (consp regexp) (stringp (car regexp))
4350 (string-match-p (car regexp) buffer-name)))
4351 (throw 'found t)))))))
4353 (defcustom pop-up-frames nil
4354 "Whether `display-buffer' should make a separate frame.
4355 If nil, never make a separate frame.
4356 If the value is `graphic-only', make a separate frame
4357 on graphic displays only.
4358 Any other non-nil value means always make a separate frame."
4359 :type '(choice
4360 (const :tag "Never" nil)
4361 (const :tag "On graphic displays only" graphic-only)
4362 (const :tag "Always" t))
4363 :group 'windows)
4365 (defcustom display-buffer-reuse-frames nil
4366 "Non-nil means `display-buffer' should reuse frames.
4367 If the buffer in question is already displayed in a frame, raise
4368 that frame."
4369 :type 'boolean
4370 :version "21.1"
4371 :group 'windows)
4373 (defcustom pop-up-windows t
4374 "Non-nil means `display-buffer' should make a new window."
4375 :type 'boolean
4376 :group 'windows)
4378 (defcustom split-window-preferred-function 'split-window-sensibly
4379 "Function called by `display-buffer' routines to split a window.
4380 This function is called with a window as single argument and is
4381 supposed to split that window and return the new window. If the
4382 window can (or shall) not be split, it is supposed to return nil.
4383 The default is to call the function `split-window-sensibly' which
4384 tries to split the window in a way which seems most suitable.
4385 You can customize the options `split-height-threshold' and/or
4386 `split-width-threshold' in order to have `split-window-sensibly'
4387 prefer either vertical or horizontal splitting.
4389 If you set this to any other function, bear in mind that the
4390 `display-buffer' routines may call this function two times. The
4391 argument of the first call is the largest window on its frame.
4392 If that call fails to return a live window, the function is
4393 called again with the least recently used window as argument. If
4394 that call fails too, `display-buffer' will use an existing window
4395 to display its buffer.
4397 The window selected at the time `display-buffer' was invoked is
4398 still selected when this function is called. Hence you can
4399 compare the window argument with the value of `selected-window'
4400 if you intend to split the selected window instead or if you do
4401 not want to split the selected window."
4402 :type 'function
4403 :version "23.1"
4404 :group 'windows)
4406 (defcustom split-height-threshold 80
4407 "Minimum height for splitting windows sensibly.
4408 If this is an integer, `split-window-sensibly' may split a window
4409 vertically only if it has at least this many lines. If this is
4410 nil, `split-window-sensibly' is not allowed to split a window
4411 vertically. If, however, a window is the only window on its
4412 frame, `split-window-sensibly' may split it vertically
4413 disregarding the value of this variable."
4414 :type '(choice (const nil) (integer :tag "lines"))
4415 :version "23.1"
4416 :group 'windows)
4418 (defcustom split-width-threshold 160
4419 "Minimum width for splitting windows sensibly.
4420 If this is an integer, `split-window-sensibly' may split a window
4421 horizontally only if it has at least this many columns. If this
4422 is nil, `split-window-sensibly' is not allowed to split a window
4423 horizontally."
4424 :type '(choice (const nil) (integer :tag "columns"))
4425 :version "23.1"
4426 :group 'windows)
4428 (defun window-splittable-p (window &optional horizontal)
4429 "Return non-nil if `split-window-sensibly' may split WINDOW.
4430 Optional argument HORIZONTAL nil or omitted means check whether
4431 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
4432 non-nil means check whether WINDOW may be split horizontally.
4434 WINDOW may be split vertically when the following conditions
4435 hold:
4436 - `window-size-fixed' is either nil or equals `width' for the
4437 buffer of WINDOW.
4438 - `split-height-threshold' is an integer and WINDOW is at least as
4439 high as `split-height-threshold'.
4440 - When WINDOW is split evenly, the emanating windows are at least
4441 `window-min-height' lines tall and can accommodate at least one
4442 line plus - if WINDOW has one - a mode line.
4444 WINDOW may be split horizontally when the following conditions
4445 hold:
4446 - `window-size-fixed' is either nil or equals `height' for the
4447 buffer of WINDOW.
4448 - `split-width-threshold' is an integer and WINDOW is at least as
4449 wide as `split-width-threshold'.
4450 - When WINDOW is split evenly, the emanating windows are at least
4451 `window-min-width' or two (whichever is larger) columns wide."
4452 (when (window-live-p window)
4453 (with-current-buffer (window-buffer window)
4454 (if horizontal
4455 ;; A window can be split horizontally when its width is not
4456 ;; fixed, it is at least `split-width-threshold' columns wide
4457 ;; and at least twice as wide as `window-min-width' and 2 (the
4458 ;; latter value is hardcoded).
4459 (and (memq window-size-fixed '(nil height))
4460 ;; Testing `window-full-width-p' here hardly makes any
4461 ;; sense nowadays. This can be done more intuitively by
4462 ;; setting up `split-width-threshold' appropriately.
4463 (numberp split-width-threshold)
4464 (>= (window-width window)
4465 (max split-width-threshold
4466 (* 2 (max window-min-width 2)))))
4467 ;; A window can be split vertically when its height is not
4468 ;; fixed, it is at least `split-height-threshold' lines high,
4469 ;; and it is at least twice as high as `window-min-height' and 2
4470 ;; if it has a mode line or 1.
4471 (and (memq window-size-fixed '(nil width))
4472 (numberp split-height-threshold)
4473 (>= (window-height window)
4474 (max split-height-threshold
4475 (* 2 (max window-min-height
4476 (if mode-line-format 2 1))))))))))
4478 (defun split-window-sensibly (&optional window)
4479 "Split WINDOW in a way suitable for `display-buffer'.
4480 WINDOW defaults to the currently selected window.
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 (let ((window (or window (selected-window))))
4511 (or (and (window-splittable-p window)
4512 ;; Split window vertically.
4513 (with-selected-window window
4514 (split-window-below)))
4515 (and (window-splittable-p window t)
4516 ;; Split window horizontally.
4517 (with-selected-window window
4518 (split-window-right)))
4519 (and (eq window (frame-root-window (window-frame window)))
4520 (not (window-minibuffer-p window))
4521 ;; If WINDOW is the only window on its frame and is not the
4522 ;; minibuffer window, try to split it vertically disregarding
4523 ;; the value of `split-height-threshold'.
4524 (let ((split-height-threshold 0))
4525 (when (window-splittable-p window)
4526 (with-selected-window window
4527 (split-window-below))))))))
4529 (defun window--try-to-split-window (window)
4530 "Try to split WINDOW.
4531 Return value returned by `split-window-preferred-function' if it
4532 represents a live window, nil otherwise."
4533 (and (window-live-p window)
4534 (not (frame-parameter (window-frame window) 'unsplittable))
4535 (let ((new-window
4536 ;; Since `split-window-preferred-function' might
4537 ;; throw an error use `condition-case'.
4538 (condition-case nil
4539 (funcall split-window-preferred-function window)
4540 (error nil))))
4541 (and (window-live-p new-window) new-window))))
4543 (defun window--frame-usable-p (frame)
4544 "Return FRAME if it can be used to display a buffer."
4545 (when (frame-live-p frame)
4546 (let ((window (frame-root-window frame)))
4547 ;; `frame-root-window' may be an internal window which is considered
4548 ;; "dead" by `window-live-p'. Hence if `window' is not live we
4549 ;; implicitly know that `frame' has a visible window we can use.
4550 (unless (and (window-live-p window)
4551 (or (window-minibuffer-p window)
4552 ;; If the window is soft-dedicated, the frame is usable.
4553 ;; Actually, even if the window is really dedicated,
4554 ;; the frame is still usable by splitting it.
4555 ;; At least Emacs-22 allowed it, and it is desirable
4556 ;; when displaying same-frame windows.
4557 nil ; (eq t (window-dedicated-p window))
4559 frame))))
4561 (defcustom even-window-heights t
4562 "If non-nil `display-buffer' will try to even window heights.
4563 Otherwise `display-buffer' will leave the window configuration
4564 alone. Heights are evened only when `display-buffer' chooses a
4565 window that appears above or below the selected window."
4566 :type 'boolean
4567 :group 'windows)
4569 (defun window--even-window-heights (window)
4570 "Even heights of WINDOW and selected window.
4571 Do this only if these windows are vertically adjacent to each
4572 other, `even-window-heights' is non-nil, and the selected window
4573 is higher than WINDOW."
4574 (when (and even-window-heights
4575 (not (eq window (selected-window)))
4576 ;; Don't resize minibuffer windows.
4577 (not (window-minibuffer-p (selected-window)))
4578 (> (window-height (selected-window)) (window-height window))
4579 (eq (window-frame window) (window-frame (selected-window)))
4580 (let ((sel-edges (window-edges (selected-window)))
4581 (win-edges (window-edges window)))
4582 (and (= (nth 0 sel-edges) (nth 0 win-edges))
4583 (= (nth 2 sel-edges) (nth 2 win-edges))
4584 (or (= (nth 1 sel-edges) (nth 3 win-edges))
4585 (= (nth 3 sel-edges) (nth 1 win-edges))))))
4586 (let ((window-min-height 1))
4587 ;; Don't throw an error if we can't even window heights for
4588 ;; whatever reason.
4589 (condition-case nil
4590 (enlarge-window (/ (- (window-height window) (window-height)) 2))
4591 (error nil)))))
4593 (defun window--display-buffer (buffer window type &optional dedicated)
4594 "Display BUFFER in WINDOW and make its frame visible.
4595 TYPE must be one of the symbols `reuse', `window' or `frame' and
4596 is passed unaltered to `display-buffer-record-window'. Set
4597 `window-dedicated-p' to DEDICATED if non-nil. Return WINDOW if
4598 BUFFER and WINDOW are live."
4599 (when (and (buffer-live-p buffer) (window-live-p window))
4600 (let* ((frame (window-frame window))
4601 (visible (frame-visible-p frame)))
4602 (unless (eq buffer (window-buffer window))
4603 (set-window-dedicated-p window nil)
4604 (display-buffer-record-window type window buffer)
4605 (set-window-buffer window buffer)
4606 (when dedicated
4607 (set-window-dedicated-p window dedicated))
4608 (when (memq type '(window frame))
4609 (set-window-prev-buffers window nil)))
4611 (unless (or (not visible)
4612 ;; Assume the selected frame is already visible enough.
4613 (eq frame (selected-frame))
4614 ;; Assume the frame from which we invoked the minibuffer
4615 ;; is visible.
4616 (and (minibuffer-window-active-p (selected-window))
4617 (eq frame (window-frame (minibuffer-selected-window)))))
4618 (raise-frame frame))
4620 window)))
4622 ;; FIXME: Not implemented.
4623 ;; FIXME: By the way, there could be more levels of dedication:
4624 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
4625 ;; the window hasn't been used for something else yet.
4626 ;; - `softly' dedicated only allows reuse when asked explicitly.
4627 ;; - `strongly' never allows reuse.
4628 (defvar display-buffer-mark-dedicated nil
4629 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
4630 The actual non-nil value of this variable will be copied to the
4631 `window-dedicated-p' flag.")
4633 (defconst display-buffer--action-function-custom-type
4634 '(choice :tag "Function"
4635 (const :tag "--" ignore) ; default for insertion
4636 (const display-buffer-reuse-window)
4637 (const display-buffer-pop-up-window)
4638 (const display-buffer-same-window)
4639 (const display-buffer-pop-up-frame)
4640 (const display-buffer-use-some-window)
4641 (function :tag "Other function"))
4642 "Custom type for `display-buffer' action functions.")
4644 (defconst display-buffer--action-custom-type
4645 `(cons :tag "Action"
4646 (choice :tag "Action functions"
4647 ,display-buffer--action-function-custom-type
4648 (repeat
4649 :tag "List of functions"
4650 ,display-buffer--action-function-custom-type))
4651 (alist :tag "Action arguments"
4652 :key-type symbol
4653 :value-type (sexp :tag "Value")))
4654 "Custom type for `display-buffer' actions.")
4656 (defvar display-buffer-overriding-action '(nil . nil)
4657 "Overriding action to perform to display a buffer.
4658 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
4659 function or a list of functions. Each function should accept two
4660 arguments: a buffer to display and an alist similar to ALIST.
4661 See `display-buffer' for details.")
4662 (put 'display-buffer-overriding-action 'risky-local-variable t)
4664 (defcustom display-buffer-alist nil
4665 "Alist of conditional actions for `display-buffer'.
4666 This is a list of elements (CONDITION . ACTION), where:
4668 CONDITION is either a regexp matching buffer names, or a function
4669 that takes a buffer and returns a boolean.
4671 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
4672 function or a list of functions. Each such function should
4673 accept two arguments: a buffer to display and an alist of the
4674 same form as ALIST. See `display-buffer' for details."
4675 :type `(alist :key-type
4676 (choice :tag "Condition"
4677 regexp
4678 (function :tag "Matcher function"))
4679 :value-type ,display-buffer--action-custom-type)
4680 :risky t
4681 :version "24.1"
4682 :group 'windows)
4684 (defcustom display-buffer-base-action '(nil . nil)
4685 "User-specified default action for `display-buffer'.
4686 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
4687 function or a list of functions. Each function should accept two
4688 arguments: a buffer to display and an alist similar to ALIST.
4689 See `display-buffer' for details."
4690 :type display-buffer--action-custom-type
4691 :risky t
4692 :version "24.1"
4693 :group 'windows)
4695 (defconst display-buffer-fallback-action
4696 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
4697 display-buffer-reuse-window
4698 display-buffer--maybe-pop-up-frame-or-window
4699 display-buffer-use-some-window
4700 ;; If all else fails, pop up a new frame.
4701 display-buffer-pop-up-frame))
4702 "Default fallback action for `display-buffer'.
4703 This is the action used by `display-buffer' if no other actions
4704 specified, e.g. by the user options `display-buffer-alist' or
4705 `display-buffer-base-action'. See `display-buffer'.")
4706 (put 'display-buffer-fallback-action 'risky-local-variable t)
4708 (defun display-buffer-assq-regexp (buffer-name alist)
4709 "Retrieve ALIST entry corresponding to BUFFER-NAME."
4710 (catch 'match
4711 (dolist (entry alist)
4712 (let ((key (car entry)))
4713 (when (or (and (stringp key)
4714 (string-match-p key buffer-name))
4715 (and (symbolp key) (functionp key)
4716 (funcall key buffer-name alist)))
4717 (throw 'match (cdr entry)))))))
4719 (defvar display-buffer--same-window-action
4720 '(display-buffer-same-window
4721 (inhibit-same-window . nil))
4722 "A `display-buffer' action for displaying in the same window.")
4723 (put 'display-buffer--same-window-action 'risky-local-variable t)
4725 (defvar display-buffer--other-frame-action
4726 '((display-buffer-reuse-window
4727 display-buffer-pop-up-frame)
4728 (reusable-frames . 0)
4729 (inhibit-same-window . t))
4730 "A `display-buffer' action for displaying in another frame.")
4731 (put 'display-buffer--other-frame-action 'risky-local-variable t)
4733 (defun display-buffer (buffer-or-name &optional action frame)
4734 "Display BUFFER-OR-NAME in some window, without selecting it.
4735 BUFFER-OR-NAME must be a buffer or the name of an existing
4736 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
4737 or nil if no such window is found.
4739 Optional argument ACTION should have the form (FUNCTION . ALIST).
4740 FUNCTION is either a function or a list of functions.
4741 ALIST is an arbitrary association list (alist).
4743 Each such FUNCTION should accept two arguments: the buffer to
4744 display and an alist. Based on those arguments, it should either
4745 display the buffer and return the window, or return nil if unable
4746 to display the buffer.
4748 The `display-buffer' function builds a function list and an alist
4749 by combining the functions and alists specified in
4750 `display-buffer-overriding-action', `display-buffer-alist', the
4751 ACTION argument, `display-buffer-base-action', and
4752 `display-buffer-fallback-action' (in order). Then it calls each
4753 function in the combined function list in turn, passing the
4754 buffer as the first argument and the combined alist as the second
4755 argument, until one of the functions returns non-nil.
4757 Available action functions include:
4758 `display-buffer-same-window'
4759 `display-buffer-reuse-window'
4760 `display-buffer-pop-up-frame'
4761 `display-buffer-pop-up-window'
4762 `display-buffer-use-some-window'
4764 Recognized alist entries include:
4766 `inhibit-same-window' -- A non-nil value prevents the same
4767 window from being used for display.
4769 `reusable-frames' -- Value specifies frame(s) to search for a
4770 window that already displays the buffer.
4771 See `display-buffer-reuse-window'.
4773 The ACTION argument to `display-buffer' can also have a non-nil
4774 and non-list value. This means to display the buffer in a window
4775 other than the selected one, even if it is already displayed in
4776 the selected window. If called interactively with a prefix
4777 argument, ACTION is t.
4779 Optional argument FRAME, if non-nil, acts like an additional
4780 ALIST entry (reusable-frames . FRAME), specifying the frame(s) to
4781 search for a window that is already displaying the buffer. See
4782 `display-buffer-reuse-window'."
4783 (interactive (list (read-buffer "Display buffer: " (other-buffer))
4784 (if current-prefix-arg t)))
4785 (let ((buffer (if (bufferp buffer-or-name)
4786 buffer-or-name
4787 (get-buffer buffer-or-name)))
4788 ;; Handle the old form of the first argument.
4789 (inhibit-same-window (and action (not (listp action)))))
4790 (unless (listp action) (setq action nil))
4791 (if display-buffer-function
4792 ;; If `display-buffer-function' is defined, let it do the job.
4793 (funcall display-buffer-function buffer inhibit-same-window)
4794 ;; Otherwise, use the defined actions.
4795 (let* ((user-action
4796 (display-buffer-assq-regexp (buffer-name buffer)
4797 display-buffer-alist))
4798 (special-action (display-buffer--special-action buffer))
4799 ;; Extra actions from the arguments to this function:
4800 (extra-action
4801 (cons nil (append (if inhibit-same-window
4802 '((inhibit-same-window . t)))
4803 (if frame
4804 `((reusable-frames . ,frame))))))
4805 ;; Construct action function list and action alist.
4806 (actions (list display-buffer-overriding-action
4807 user-action special-action action extra-action
4808 display-buffer-base-action
4809 display-buffer-fallback-action))
4810 (functions (apply 'append
4811 (mapcar (lambda (x)
4812 (setq x (car x))
4813 (if (functionp x) (list x) x))
4814 actions)))
4815 (alist (apply 'append (mapcar 'cdr actions)))
4816 window)
4817 (unless (buffer-live-p buffer)
4818 (error "Invalid buffer"))
4819 (while (and functions (not window))
4820 (setq window (funcall (car functions) buffer alist)
4821 functions (cdr functions)))
4822 window))))
4824 (defun display-buffer-other-frame (buffer)
4825 "Display buffer BUFFER in another frame.
4826 This uses the function `display-buffer' as a subroutine; see
4827 its documentation for additional customization information."
4828 (interactive "BDisplay buffer in other frame: ")
4829 (display-buffer buffer display-buffer--other-frame-action t))
4831 ;;; `display-buffer' action functions:
4833 (defun display-buffer-same-window (buffer alist)
4834 "Display BUFFER in the selected window.
4835 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
4836 if the selected window is a minibuffer window or is dedicated to
4837 another buffer; in that case, return nil. Otherwise, return the
4838 selected window."
4839 (unless (or (cdr (assq 'inhibit-same-window alist))
4840 (window-minibuffer-p)
4841 (window-dedicated-p))
4842 (window--display-buffer buffer (selected-window) 'reuse)))
4844 (defun display-buffer--maybe-same-window (buffer alist)
4845 "Conditionally display BUFFER in the selected window.
4846 If `same-window-p' returns non-nil for BUFFER's name, call
4847 `display-buffer-same-window' and return its value. Otherwise,
4848 return nil."
4849 (and (same-window-p (buffer-name buffer))
4850 (display-buffer-same-window buffer alist)))
4852 (defun display-buffer-reuse-window (buffer alist)
4853 "Return a window that is already displaying BUFFER.
4854 Return nil if no usable window is found.
4856 If ALIST has a non-nil `inhibit-same-window' entry, the selected
4857 window is not eligible for reuse.
4859 If ALIST contains a `reusable-frames' entry, its value determines
4860 which frames to search for a reusable window:
4861 nil -- the selected frame (actually the last non-minibuffer frame)
4862 A frame -- just that frame
4863 `visible' -- all visible frames
4864 0 -- all frames on the current terminal
4865 t -- all frames.
4867 If ALIST contains no `reusable-frames' entry, search just the
4868 selected frame if `display-buffer-reuse-frames' and
4869 `pop-up-frames' are both nil; search all frames on the current
4870 terminal if either of those variables is non-nil."
4871 (let* ((alist-entry (assq 'reusable-frames alist))
4872 (frames (cond (alist-entry (cdr alist-entry))
4873 ((if (eq pop-up-frames 'graphic-only)
4874 (display-graphic-p)
4875 pop-up-frames)
4877 (display-buffer-reuse-frames 0)
4878 (t (last-nonminibuffer-frame))))
4879 (window (if (and (eq buffer (window-buffer))
4880 (not (cdr (assq 'inhibit-same-window alist))))
4881 (selected-window)
4882 (car (delq (selected-window)
4883 (get-buffer-window-list buffer 'nomini
4884 frames))))))
4885 (when window
4886 (window--display-buffer buffer window 'reuse))))
4888 (defun display-buffer--special-action (buffer)
4889 "Return special display action for BUFFER, if any.
4890 If `special-display-p' returns non-nil for BUFFER, return an
4891 appropriate display action involving `special-display-function'.
4892 See `display-buffer' for the format of display actions."
4893 (and special-display-function
4894 ;; `special-display-p' returns either t or a list of frame
4895 ;; parameters to pass to `special-display-function'.
4896 (let ((pars (special-display-p (buffer-name buffer))))
4897 (when pars
4898 (list (list #'display-buffer-reuse-window
4899 `(lambda (buffer _alist)
4900 (funcall special-display-function
4901 buffer ',(if (listp pars) pars)))))))))
4903 (defun display-buffer-pop-up-frame (buffer _alist)
4904 "Display BUFFER in a new frame.
4905 This works by calling `pop-up-frame-function'. If successful,
4906 return the window used; otherwise return nil."
4907 (let ((fun pop-up-frame-function)
4908 frame window)
4909 (when (and fun
4910 (setq frame (funcall fun))
4911 (setq window (frame-selected-window frame)))
4912 (window--display-buffer
4913 buffer window 'frame display-buffer-mark-dedicated))))
4915 (defun display-buffer-pop-up-window (buffer _alist)
4916 "Display BUFFER by popping up a new window.
4917 The new window is created on the selected frame, or in
4918 `last-nonminibuffer-frame' if no windows can be created there.
4919 If successful, return the new window; otherwise return nil."
4920 (let ((frame (or (window--frame-usable-p (selected-frame))
4921 (window--frame-usable-p (last-nonminibuffer-frame))))
4922 window)
4923 (when (and (or (not (frame-parameter frame 'unsplittable))
4924 ;; If the selected frame cannot be split, look at
4925 ;; `last-nonminibuffer-frame'.
4926 (and (eq frame (selected-frame))
4927 (setq frame (last-nonminibuffer-frame))
4928 (window--frame-usable-p frame)
4929 (not (frame-parameter frame 'unsplittable))))
4930 ;; Attempt to split largest or least recently used window.
4931 (setq window (or (window--try-to-split-window
4932 (get-largest-window frame t))
4933 (window--try-to-split-window
4934 (get-lru-window frame t)))))
4935 (window--display-buffer
4936 buffer window 'window display-buffer-mark-dedicated))))
4938 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
4939 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
4941 If `pop-up-frames' is non-nil (and not `graphic-only' on a
4942 text-only terminal), try with `display-buffer-pop-up-frame'.
4944 If that cannot be done, and `pop-up-windows' is non-nil, try
4945 again with `display-buffer-pop-up-window'."
4946 (or (and (if (eq pop-up-frames 'graphic-only)
4947 (display-graphic-p)
4948 pop-up-frames)
4949 (display-buffer-pop-up-frame buffer alist))
4950 (and pop-up-windows
4951 (display-buffer-pop-up-window buffer alist))))
4953 (defun display-buffer-use-some-window (buffer alist)
4954 "Display BUFFER in an existing window.
4955 Search for a usable window, set that window to the buffer, and
4956 return the window. If no suitable window is found, return nil."
4957 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
4958 (frame (or (window--frame-usable-p (selected-frame))
4959 (window--frame-usable-p (last-nonminibuffer-frame))))
4960 (window
4961 ;; Reuse an existing window.
4962 (or (get-lru-window frame nil not-this-window)
4963 (let ((window (get-buffer-window buffer 'visible)))
4964 (unless (and not-this-window
4965 (eq window (selected-window)))
4966 window))
4967 (get-largest-window 'visible nil not-this-window)
4968 (let ((window (get-buffer-window buffer 0)))
4969 (unless (and not-this-window
4970 (eq window (selected-window)))
4971 window))
4972 (get-largest-window 0 not-this-window))))
4973 (when window
4974 (window--even-window-heights window)
4975 (window--display-buffer buffer window 'reuse))))
4977 ;;; Display + selection commands:
4978 (defun pop-to-buffer (buffer &optional action norecord)
4979 "Select buffer BUFFER in some window, preferably a different one.
4980 BUFFER may be a buffer, a string (a buffer name), or nil. If it
4981 is a string not naming an existent buffer, create a buffer with
4982 that name. If BUFFER is nil, choose some other buffer. Return
4983 the buffer.
4985 This uses `display-buffer' as a subroutine. The optional ACTION
4986 argument is passed to `display-buffer' as its ACTION argument.
4987 See `display-buffer' for more information. ACTION is t if called
4988 interactively with a prefix argument, which means to pop to a
4989 window other than the selected one even if the buffer is already
4990 displayed in the selected window.
4992 If the window to show BUFFER is not on the selected
4993 frame, raise that window's frame and give it input focus.
4995 Optional third arg NORECORD non-nil means do not put this buffer
4996 at the front of the list of recently selected ones."
4997 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
4998 (if current-prefix-arg t)))
4999 (setq buffer (window-normalize-buffer-to-switch-to buffer))
5000 (set-buffer buffer)
5001 (let* ((old-frame (selected-frame))
5002 (window (display-buffer buffer action))
5003 (frame (window-frame window)))
5004 ;; If we chose another frame, make sure it gets input focus.
5005 (unless (eq frame old-frame)
5006 (select-frame-set-input-focus frame norecord))
5007 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
5008 (select-window window norecord)
5009 buffer))
5011 (defun pop-to-buffer-same-window (buffer &optional norecord)
5012 "Select buffer BUFFER in some window, preferably the same one.
5013 This function behaves much like `switch-to-buffer', except it
5014 displays with `special-display-function' if BUFFER has a match in
5015 `special-display-buffer-names' or `special-display-regexps'.
5017 Unlike `pop-to-buffer', this function prefers using the selected
5018 window over popping up a new window or frame.
5020 BUFFER may be a buffer, a string (a buffer name), or nil. If it
5021 is a string not naming an existent buffer, create a buffer with
5022 that name. If BUFFER is nil, choose some other buffer. Return
5023 the buffer.
5025 NORECORD, if non-nil means do not put this buffer at the front of
5026 the list of recently selected ones."
5027 (pop-to-buffer buffer display-buffer--same-window-action norecord))
5029 (defun read-buffer-to-switch (prompt)
5030 "Read the name of a buffer to switch to, prompting with PROMPT.
5031 Return the name of the buffer as a string.
5033 This function is intended for the `switch-to-buffer' family of
5034 commands since these need to omit the name of the current buffer
5035 from the list of completions and default values."
5036 (let ((rbts-completion-table (internal-complete-buffer-except)))
5037 (minibuffer-with-setup-hook
5038 (lambda ()
5039 (setq minibuffer-completion-table rbts-completion-table)
5040 ;; Since rbts-completion-table is built dynamically, we
5041 ;; can't just add it to the default value of
5042 ;; icomplete-with-completion-tables, so we add it
5043 ;; here manually.
5044 (if (and (boundp 'icomplete-with-completion-tables)
5045 (listp icomplete-with-completion-tables))
5046 (set (make-local-variable 'icomplete-with-completion-tables)
5047 (cons rbts-completion-table
5048 icomplete-with-completion-tables))))
5049 (read-buffer prompt (other-buffer (current-buffer))
5050 (confirm-nonexistent-file-or-buffer)))))
5052 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
5053 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
5054 If BUFFER-OR-NAME is nil, return the buffer returned by
5055 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
5056 exists, return that buffer. If no such buffer exists, create a
5057 buffer with the name BUFFER-OR-NAME and return that buffer."
5058 (if buffer-or-name
5059 (or (get-buffer buffer-or-name)
5060 (let ((buffer (get-buffer-create buffer-or-name)))
5061 (set-buffer-major-mode buffer)
5062 buffer))
5063 (other-buffer)))
5065 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
5066 "Switch to buffer BUFFER-OR-NAME in the selected window.
5067 If called interactively, prompt for the buffer name using the
5068 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5069 determines whether to request confirmation before creating a new
5070 buffer.
5072 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5073 nil. If BUFFER-OR-NAME is a string that does not identify an
5074 existing buffer, create a buffer with that name. If
5075 BUFFER-OR-NAME is nil, switch to the buffer returned by
5076 `other-buffer'.
5078 Optional argument NORECORD non-nil means do not put the buffer
5079 specified by BUFFER-OR-NAME at the front of the buffer list and
5080 do not make the window displaying it the most recently selected
5081 one.
5083 If FORCE-SAME-WINDOW is non-nil, BUFFER-OR-NAME must be displayed
5084 in the selected window; signal an error if that is
5085 impossible (e.g. if the selected window is minibuffer-only). If
5086 nil, BUFFER-OR-NAME may be displayed in another window.
5088 Return the buffer switched to."
5089 (interactive
5090 (list (read-buffer-to-switch "Switch to buffer: ") nil 'force-same-window))
5091 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
5092 (cond
5093 ;; Don't call set-window-buffer if it's not needed since it
5094 ;; might signal an error (e.g. if the window is dedicated).
5095 ((eq buffer (window-buffer)))
5096 ((window-minibuffer-p)
5097 (if force-same-window
5098 (user-error "Cannot switch buffers in minibuffer window")
5099 (pop-to-buffer buffer norecord)))
5100 ((eq (window-dedicated-p) t)
5101 (if force-same-window
5102 (user-error "Cannot switch buffers in a dedicated window")
5103 (pop-to-buffer buffer norecord)))
5104 (t (set-window-buffer nil buffer)))
5106 (unless norecord
5107 (select-window (selected-window)))
5108 (set-buffer buffer)))
5110 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
5111 "Select the buffer specified by BUFFER-OR-NAME in another window.
5112 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5113 nil. Return the buffer switched to.
5115 If called interactively, prompt for the buffer name using the
5116 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5117 determines whether to request confirmation before creating a new
5118 buffer.
5120 If BUFFER-OR-NAME is a string and does not identify an existing
5121 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5122 nil, switch to the buffer returned by `other-buffer'.
5124 Optional second argument NORECORD non-nil means do not put this
5125 buffer at the front of the list of recently selected ones.
5127 This uses the function `display-buffer' as a subroutine; see its
5128 documentation for additional customization information."
5129 (interactive
5130 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5131 (let ((pop-up-windows t))
5132 (pop-to-buffer buffer-or-name t norecord)))
5134 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
5135 "Switch to buffer BUFFER-OR-NAME in another frame.
5136 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5137 nil. Return the buffer switched to.
5139 If called interactively, prompt for the buffer name using the
5140 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5141 determines whether to request confirmation before creating a new
5142 buffer.
5144 If BUFFER-OR-NAME is a string and does not identify an existing
5145 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5146 nil, switch to the buffer returned by `other-buffer'.
5148 Optional second arg NORECORD non-nil means do not put this
5149 buffer at the front of the list of recently selected ones.
5151 This uses the function `display-buffer' as a subroutine; see its
5152 documentation for additional customization information."
5153 (interactive
5154 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
5155 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
5157 (defun set-window-text-height (window height)
5158 "Set the height in lines of the text display area of WINDOW to HEIGHT.
5159 WINDOW must be a live window. HEIGHT doesn't include the mode
5160 line or header line, if any, or any partial-height lines in the
5161 text display area.
5163 Note that the current implementation of this function cannot
5164 always set the height exactly, but attempts to be conservative,
5165 by allocating more lines than are actually needed in the case
5166 where some error may be present."
5167 (setq window (window-normalize-window window t))
5168 (let ((delta (- height (window-text-height window))))
5169 (unless (zerop delta)
5170 ;; Setting window-min-height to a value like 1 can lead to very
5171 ;; bizarre displays because it also allows Emacs to make *other*
5172 ;; windows one line tall, which means that there's no more space
5173 ;; for the mode line.
5174 (let ((window-min-height (min 2 height)))
5175 (window-resize window delta)))))
5177 (defun enlarge-window-horizontally (delta)
5178 "Make selected window DELTA columns wider.
5179 Interactively, if no argument is given, make selected window one
5180 column wider."
5181 (interactive "p")
5182 (enlarge-window delta t))
5184 (defun shrink-window-horizontally (delta)
5185 "Make selected window DELTA columns narrower.
5186 Interactively, if no argument is given, make selected window one
5187 column narrower."
5188 (interactive "p")
5189 (shrink-window delta t))
5191 (defun count-screen-lines (&optional beg end count-final-newline window)
5192 "Return the number of screen lines in the region.
5193 The number of screen lines may be different from the number of actual lines,
5194 due to line breaking, display table, etc.
5196 Optional arguments BEG and END default to `point-min' and `point-max'
5197 respectively.
5199 If region ends with a newline, ignore it unless optional third argument
5200 COUNT-FINAL-NEWLINE is non-nil.
5202 The optional fourth argument WINDOW specifies the window used for obtaining
5203 parameters such as width, horizontal scrolling, and so on. The default is
5204 to use the selected window's parameters.
5206 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
5207 regardless of which buffer is displayed in WINDOW. This makes possible to use
5208 `count-screen-lines' in any buffer, whether or not it is currently displayed
5209 in some window."
5210 (unless beg
5211 (setq beg (point-min)))
5212 (unless end
5213 (setq end (point-max)))
5214 (if (= beg end)
5216 (save-excursion
5217 (save-restriction
5218 (widen)
5219 (narrow-to-region (min beg end)
5220 (if (and (not count-final-newline)
5221 (= ?\n (char-before (max beg end))))
5222 (1- (max beg end))
5223 (max beg end)))
5224 (goto-char (point-min))
5225 (1+ (vertical-motion (buffer-size) window))))))
5227 (defun window-buffer-height (window)
5228 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
5229 (with-current-buffer (window-buffer window)
5230 (max 1
5231 (count-screen-lines (point-min) (point-max)
5232 ;; If buffer ends with a newline, ignore it when
5233 ;; counting height unless point is after it.
5234 (eobp)
5235 window))))
5237 ;;; Resizing buffers to fit their contents exactly.
5238 (defun fit-window-to-buffer (&optional window max-height min-height override)
5239 "Adjust height of WINDOW to display its buffer's contents exactly.
5240 WINDOW can be any live window and defaults to the selected one.
5242 Optional argument MAX-HEIGHT specifies the maximum height of
5243 WINDOW and defaults to the height of WINDOW's frame. Optional
5244 argument MIN-HEIGHT specifies the minimum height of WINDOW and
5245 defaults to `window-min-height'. Both MAX-HEIGHT and MIN-HEIGHT
5246 are specified in lines and include the mode line and header line,
5247 if any.
5249 Optional argument OVERRIDE non-nil means override restrictions
5250 imposed by `window-min-height' and `window-min-width' on the size
5251 of WINDOW.
5253 Return the number of lines by which WINDOW was enlarged or
5254 shrunk. If an error occurs during resizing, return nil but don't
5255 signal an error.
5257 Note that even if this function makes WINDOW large enough to show
5258 _all_ lines of its buffer you might not see the first lines when
5259 WINDOW was scrolled."
5260 (interactive)
5261 ;; Do all the work in WINDOW and its buffer and restore the selected
5262 ;; window and the current buffer when we're done.
5263 (setq window (window-normalize-window window t))
5264 ;; Can't resize a full height or fixed-size window.
5265 (unless (or (window-size-fixed-p window)
5266 (window-full-height-p window))
5267 ;; `with-selected-window' should orderly restore the current buffer.
5268 (with-selected-window window
5269 ;; We are in WINDOW's buffer now.
5270 (let* (;; Adjust MIN-HEIGHT.
5271 (min-height
5272 (if override
5273 (window-min-size window nil window)
5274 (max (or min-height window-min-height)
5275 window-safe-min-height)))
5276 (max-window-height
5277 (window-total-size (frame-root-window window)))
5278 ;; Adjust MAX-HEIGHT.
5279 (max-height
5280 (if (or override (not max-height))
5281 max-window-height
5282 (min max-height max-window-height)))
5283 ;; Make `desired-height' the height necessary to show
5284 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
5285 ;; and MAX-HEIGHT.
5286 (desired-height
5287 (max
5288 (min
5289 (+ (count-screen-lines)
5290 ;; For non-minibuffers count the mode line, if any.
5291 (if (and (not (window-minibuffer-p window))
5292 mode-line-format)
5295 ;; Count the header line, if any.
5296 (if header-line-format 1 0))
5297 max-height)
5298 min-height))
5299 (desired-delta
5300 (- desired-height (window-total-size window)))
5301 (delta
5302 (if (> desired-delta 0)
5303 (min desired-delta
5304 (window-max-delta window nil window))
5305 (max desired-delta
5306 (- (window-min-delta window nil window))))))
5307 ;; This `condition-case' shouldn't be necessary, but who knows?
5308 (condition-case nil
5309 (if (zerop delta)
5310 ;; Return zero if DELTA became zero in the process.
5312 ;; Don't try to redisplay with the cursor at the end on its
5313 ;; own line--that would force a scroll and spoil things.
5314 (when (and (eobp) (bolp) (not (bobp)))
5315 ;; It's silly to put `point' at the end of the previous
5316 ;; line and so maybe force horizontal scrolling.
5317 (set-window-point window (line-beginning-position 0)))
5318 ;; Call `window-resize' with OVERRIDE argument equal WINDOW.
5319 (window-resize window delta nil window)
5320 ;; Check if the last line is surely fully visible. If
5321 ;; not, enlarge the window.
5322 (let ((end (save-excursion
5323 (goto-char (point-max))
5324 (when (and (bolp) (not (bobp)))
5325 ;; Don't include final newline.
5326 (backward-char 1))
5327 (when truncate-lines
5328 ;; If line-wrapping is turned off, test the
5329 ;; beginning of the last line for
5330 ;; visibility instead of the end, as the
5331 ;; end of the line could be invisible by
5332 ;; virtue of extending past the edge of the
5333 ;; window.
5334 (forward-line 0))
5335 (point))))
5336 (set-window-vscroll window 0)
5337 ;; This loop might in some rare pathological cases raise
5338 ;; an error - another reason for the `condition-case'.
5339 (while (and (< desired-height max-height)
5340 (= desired-height (window-total-size))
5341 (not (pos-visible-in-window-p end)))
5342 (window-resize window 1 nil window)
5343 (setq desired-height (1+ desired-height)))))
5344 (error (setq delta nil)))
5345 delta))))
5347 (defun window-safely-shrinkable-p (&optional window)
5348 "Return t if WINDOW can be shrunk without shrinking other windows.
5349 WINDOW defaults to the selected window."
5350 (with-selected-window (or window (selected-window))
5351 (let ((edges (window-edges)))
5352 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
5353 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
5355 (defun shrink-window-if-larger-than-buffer (&optional window)
5356 "Shrink height of WINDOW if its buffer doesn't need so many lines.
5357 More precisely, shrink WINDOW vertically to be as small as
5358 possible, while still showing the full contents of its buffer.
5359 WINDOW defaults to the selected window.
5361 Do not shrink WINDOW to less than `window-min-height' lines. Do
5362 nothing if the buffer contains more lines than the present window
5363 height, or if some of the window's contents are scrolled out of
5364 view, or if shrinking this window would also shrink another
5365 window, or if the window is the only window of its frame.
5367 Return non-nil if the window was shrunk, nil otherwise."
5368 (interactive)
5369 (setq window (window-normalize-window window t))
5370 ;; Make sure that WINDOW is vertically combined and `point-min' is
5371 ;; visible (for whatever reason that's needed). The remaining issues
5372 ;; should be taken care of by `fit-window-to-buffer'.
5373 (when (and (window-combined-p window)
5374 (pos-visible-in-window-p (point-min) window))
5375 (fit-window-to-buffer window (window-total-size window))))
5377 (defun kill-buffer-and-window ()
5378 "Kill the current buffer and delete the selected window."
5379 (interactive)
5380 (let ((window-to-delete (selected-window))
5381 (buffer-to-kill (current-buffer))
5382 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
5383 (unwind-protect
5384 (progn
5385 (add-hook 'kill-buffer-hook delete-window-hook t t)
5386 (if (kill-buffer (current-buffer))
5387 ;; If `delete-window' failed before, we rerun it to regenerate
5388 ;; the error so it can be seen in the echo area.
5389 (when (eq (selected-window) window-to-delete)
5390 (delete-window))))
5391 ;; If the buffer is not dead for some reason (probably because
5392 ;; of a `quit' signal), remove the hook again.
5393 (ignore-errors
5394 (with-current-buffer buffer-to-kill
5395 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
5398 (defvar recenter-last-op nil
5399 "Indicates the last recenter operation performed.
5400 Possible values: `top', `middle', `bottom', integer or float numbers.")
5402 (defcustom recenter-positions '(middle top bottom)
5403 "Cycling order for `recenter-top-bottom'.
5404 A list of elements with possible values `top', `middle', `bottom',
5405 integer or float numbers that define the cycling order for
5406 the command `recenter-top-bottom'.
5408 Top and bottom destinations are `scroll-margin' lines from the true
5409 window top and bottom. Middle redraws the frame and centers point
5410 vertically within the window. Integer number moves current line to
5411 the specified absolute window-line. Float number between 0.0 and 1.0
5412 means the percentage of the screen space from the top. The default
5413 cycling order is middle -> top -> bottom."
5414 :type '(repeat (choice
5415 (const :tag "Top" top)
5416 (const :tag "Middle" middle)
5417 (const :tag "Bottom" bottom)
5418 (integer :tag "Line number")
5419 (float :tag "Percentage")))
5420 :version "23.2"
5421 :group 'windows)
5423 (defun recenter-top-bottom (&optional arg)
5424 "Move current buffer line to the specified window line.
5425 With no prefix argument, successive calls place point according
5426 to the cycling order defined by `recenter-positions'.
5428 A prefix argument is handled like `recenter':
5429 With numeric prefix ARG, move current line to window-line ARG.
5430 With plain `C-u', move current line to window center."
5431 (interactive "P")
5432 (cond
5433 (arg (recenter arg)) ; Always respect ARG.
5435 (setq recenter-last-op
5436 (if (eq this-command last-command)
5437 (car (or (cdr (member recenter-last-op recenter-positions))
5438 recenter-positions))
5439 (car recenter-positions)))
5440 (let ((this-scroll-margin
5441 (min (max 0 scroll-margin)
5442 (truncate (/ (window-body-height) 4.0)))))
5443 (cond ((eq recenter-last-op 'middle)
5444 (recenter))
5445 ((eq recenter-last-op 'top)
5446 (recenter this-scroll-margin))
5447 ((eq recenter-last-op 'bottom)
5448 (recenter (- -1 this-scroll-margin)))
5449 ((integerp recenter-last-op)
5450 (recenter recenter-last-op))
5451 ((floatp recenter-last-op)
5452 (recenter (round (* recenter-last-op (window-height))))))))))
5454 (define-key global-map [?\C-l] 'recenter-top-bottom)
5456 (defun move-to-window-line-top-bottom (&optional arg)
5457 "Position point relative to window.
5459 With a prefix argument ARG, acts like `move-to-window-line'.
5461 With no argument, positions point at center of window.
5462 Successive calls position point at positions defined
5463 by `recenter-positions'."
5464 (interactive "P")
5465 (cond
5466 (arg (move-to-window-line arg)) ; Always respect ARG.
5468 (setq recenter-last-op
5469 (if (eq this-command last-command)
5470 (car (or (cdr (member recenter-last-op recenter-positions))
5471 recenter-positions))
5472 (car recenter-positions)))
5473 (let ((this-scroll-margin
5474 (min (max 0 scroll-margin)
5475 (truncate (/ (window-body-height) 4.0)))))
5476 (cond ((eq recenter-last-op 'middle)
5477 (call-interactively 'move-to-window-line))
5478 ((eq recenter-last-op 'top)
5479 (move-to-window-line this-scroll-margin))
5480 ((eq recenter-last-op 'bottom)
5481 (move-to-window-line (- -1 this-scroll-margin)))
5482 ((integerp recenter-last-op)
5483 (move-to-window-line recenter-last-op))
5484 ((floatp recenter-last-op)
5485 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
5487 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
5489 ;;; Scrolling commands.
5491 ;;; Scrolling commands which do not signal errors at top/bottom
5492 ;;; of buffer at first key-press (instead move to top/bottom
5493 ;;; of buffer).
5495 (defcustom scroll-error-top-bottom nil
5496 "Move point to top/bottom of buffer before signaling a scrolling error.
5497 A value of nil means just signal an error if no more scrolling possible.
5498 A value of t means point moves to the beginning or the end of the buffer
5499 \(depending on scrolling direction) when no more scrolling possible.
5500 When point is already on that position, then signal an error."
5501 :type 'boolean
5502 :group 'windows
5503 :version "24.1")
5505 (defun scroll-up-command (&optional arg)
5506 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
5507 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
5508 scroll window further, move cursor to the bottom line.
5509 When point is already on that position, then signal an error.
5510 A near full screen is `next-screen-context-lines' less than a full screen.
5511 Negative ARG means scroll downward.
5512 If ARG is the atom `-', scroll downward by nearly full screen."
5513 (interactive "^P")
5514 (cond
5515 ((null scroll-error-top-bottom)
5516 (scroll-up arg))
5517 ((eq arg '-)
5518 (scroll-down-command nil))
5519 ((< (prefix-numeric-value arg) 0)
5520 (scroll-down-command (- (prefix-numeric-value arg))))
5521 ((eobp)
5522 (scroll-up arg)) ; signal error
5524 (condition-case nil
5525 (scroll-up arg)
5526 (end-of-buffer
5527 (if arg
5528 ;; When scrolling by ARG lines can't be done,
5529 ;; move by ARG lines instead.
5530 (forward-line arg)
5531 ;; When ARG is nil for full-screen scrolling,
5532 ;; move to the bottom of the buffer.
5533 (goto-char (point-max))))))))
5535 (put 'scroll-up-command 'scroll-command t)
5537 (defun scroll-down-command (&optional arg)
5538 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
5539 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
5540 scroll window further, move cursor to the top line.
5541 When point is already on that position, then signal an error.
5542 A near full screen is `next-screen-context-lines' less than a full screen.
5543 Negative ARG means scroll upward.
5544 If ARG is the atom `-', scroll upward by nearly full screen."
5545 (interactive "^P")
5546 (cond
5547 ((null scroll-error-top-bottom)
5548 (scroll-down arg))
5549 ((eq arg '-)
5550 (scroll-up-command nil))
5551 ((< (prefix-numeric-value arg) 0)
5552 (scroll-up-command (- (prefix-numeric-value arg))))
5553 ((bobp)
5554 (scroll-down arg)) ; signal error
5556 (condition-case nil
5557 (scroll-down arg)
5558 (beginning-of-buffer
5559 (if arg
5560 ;; When scrolling by ARG lines can't be done,
5561 ;; move by ARG lines instead.
5562 (forward-line (- arg))
5563 ;; When ARG is nil for full-screen scrolling,
5564 ;; move to the top of the buffer.
5565 (goto-char (point-min))))))))
5567 (put 'scroll-down-command 'scroll-command t)
5569 ;;; Scrolling commands which scroll a line instead of full screen.
5571 (defun scroll-up-line (&optional arg)
5572 "Scroll text of selected window upward ARG lines; or one line if no ARG.
5573 If ARG is omitted or nil, scroll upward by one line.
5574 This is different from `scroll-up-command' that scrolls a full screen."
5575 (interactive "p")
5576 (scroll-up (or arg 1)))
5578 (put 'scroll-up-line 'scroll-command t)
5580 (defun scroll-down-line (&optional arg)
5581 "Scroll text of selected window down ARG lines; or one line if no ARG.
5582 If ARG is omitted or nil, scroll down by one line.
5583 This is different from `scroll-down-command' that scrolls a full screen."
5584 (interactive "p")
5585 (scroll-down (or arg 1)))
5587 (put 'scroll-down-line 'scroll-command t)
5590 (defun scroll-other-window-down (lines)
5591 "Scroll the \"other window\" down.
5592 For more details, see the documentation for `scroll-other-window'."
5593 (interactive "P")
5594 (scroll-other-window
5595 ;; Just invert the argument's meaning.
5596 ;; We can do that without knowing which window it will be.
5597 (if (eq lines '-) nil
5598 (if (null lines) '-
5599 (- (prefix-numeric-value lines))))))
5601 (defun beginning-of-buffer-other-window (arg)
5602 "Move point to the beginning of the buffer in the other window.
5603 Leave mark at previous position.
5604 With arg N, put point N/10 of the way from the true beginning."
5605 (interactive "P")
5606 (let ((orig-window (selected-window))
5607 (window (other-window-for-scrolling)))
5608 ;; We use unwind-protect rather than save-window-excursion
5609 ;; because the latter would preserve the things we want to change.
5610 (unwind-protect
5611 (progn
5612 (select-window window)
5613 ;; Set point and mark in that window's buffer.
5614 (with-no-warnings
5615 (beginning-of-buffer arg))
5616 ;; Set point accordingly.
5617 (recenter '(t)))
5618 (select-window orig-window))))
5620 (defun end-of-buffer-other-window (arg)
5621 "Move point to the end of the buffer in the other window.
5622 Leave mark at previous position.
5623 With arg N, put point N/10 of the way from the true end."
5624 (interactive "P")
5625 ;; See beginning-of-buffer-other-window for comments.
5626 (let ((orig-window (selected-window))
5627 (window (other-window-for-scrolling)))
5628 (unwind-protect
5629 (progn
5630 (select-window window)
5631 (with-no-warnings
5632 (end-of-buffer arg))
5633 (recenter '(t)))
5634 (select-window orig-window))))
5636 (defvar mouse-autoselect-window-timer nil
5637 "Timer used by delayed window autoselection.")
5639 (defvar mouse-autoselect-window-position nil
5640 "Last mouse position recorded by delayed window autoselection.")
5642 (defvar mouse-autoselect-window-window nil
5643 "Last window recorded by delayed window autoselection.")
5645 (defvar mouse-autoselect-window-state nil
5646 "When non-nil, special state of delayed window autoselection.
5647 Possible values are `suspend' (suspend autoselection after a menu or
5648 scrollbar interaction) and `select' (the next invocation of
5649 `handle-select-window' shall select the window immediately).")
5651 (defun mouse-autoselect-window-cancel (&optional force)
5652 "Cancel delayed window autoselection.
5653 Optional argument FORCE means cancel unconditionally."
5654 (unless (and (not force)
5655 ;; Don't cancel for select-window or select-frame events
5656 ;; or when the user drags a scroll bar.
5657 (or (memq this-command
5658 '(handle-select-window handle-switch-frame))
5659 (and (eq this-command 'scroll-bar-toolkit-scroll)
5660 (memq (nth 4 (event-end last-input-event))
5661 '(handle end-scroll)))))
5662 (setq mouse-autoselect-window-state nil)
5663 (when (timerp mouse-autoselect-window-timer)
5664 (cancel-timer mouse-autoselect-window-timer))
5665 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
5667 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
5668 "Start delayed window autoselection.
5669 MOUSE-POSITION is the last position where the mouse was seen as returned
5670 by `mouse-position'. Optional argument WINDOW non-nil denotes the
5671 window where the mouse was seen. Optional argument SUSPEND non-nil
5672 means suspend autoselection."
5673 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
5674 (setq mouse-autoselect-window-position mouse-position)
5675 (when window (setq mouse-autoselect-window-window window))
5676 (setq mouse-autoselect-window-state (when suspend 'suspend))
5677 ;; Install timer which runs `mouse-autoselect-window-select' after
5678 ;; `mouse-autoselect-window' seconds.
5679 (setq mouse-autoselect-window-timer
5680 (run-at-time
5681 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
5683 (defun mouse-autoselect-window-select ()
5684 "Select window with delayed window autoselection.
5685 If the mouse position has stabilized in a non-selected window, select
5686 that window. The minibuffer window is selected only if the minibuffer
5687 is active. This function is run by `mouse-autoselect-window-timer'."
5688 (ignore-errors
5689 (let* ((mouse-position (mouse-position))
5690 (window
5691 (ignore-errors
5692 (window-at (cadr mouse-position) (cddr mouse-position)
5693 (car mouse-position)))))
5694 (cond
5695 ((or (menu-or-popup-active-p)
5696 (and window
5697 (not (coordinates-in-window-p (cdr mouse-position) window))))
5698 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
5699 ;; of WINDOW, temporarily suspend delayed autoselection.
5700 (mouse-autoselect-window-start mouse-position nil t))
5701 ((eq mouse-autoselect-window-state 'suspend)
5702 ;; Delayed autoselection was temporarily suspended, reenable it.
5703 (mouse-autoselect-window-start mouse-position))
5704 ((and window (not (eq window (selected-window)))
5705 (or (not (numberp mouse-autoselect-window))
5706 (and (> mouse-autoselect-window 0)
5707 ;; If `mouse-autoselect-window' is positive, select
5708 ;; window if the window is the same as before.
5709 (eq window mouse-autoselect-window-window))
5710 ;; Otherwise select window if the mouse is at the same
5711 ;; position as before. Observe that the first test after
5712 ;; starting autoselection usually fails since the value of
5713 ;; `mouse-autoselect-window-position' recorded there is the
5714 ;; position where the mouse has entered the new window and
5715 ;; not necessarily where the mouse has stopped moving.
5716 (equal mouse-position mouse-autoselect-window-position))
5717 ;; The minibuffer is a candidate window if it's active.
5718 (or (not (window-minibuffer-p window))
5719 (eq window (active-minibuffer-window))))
5720 ;; Mouse position has stabilized in non-selected window: Cancel
5721 ;; delayed autoselection and try to select that window.
5722 (mouse-autoselect-window-cancel t)
5723 ;; Select window where mouse appears unless the selected window is the
5724 ;; minibuffer. Use `unread-command-events' in order to execute pre-
5725 ;; and post-command hooks and trigger idle timers. To avoid delaying
5726 ;; autoselection again, set `mouse-autoselect-window-state'."
5727 (unless (window-minibuffer-p (selected-window))
5728 (setq mouse-autoselect-window-state 'select)
5729 (setq unread-command-events
5730 (cons (list 'select-window (list window))
5731 unread-command-events))))
5732 ((or (and window (eq window (selected-window)))
5733 (not (numberp mouse-autoselect-window))
5734 (equal mouse-position mouse-autoselect-window-position))
5735 ;; Mouse position has either stabilized in the selected window or at
5736 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
5737 (mouse-autoselect-window-cancel t))
5739 ;; Mouse position has not stabilized yet, resume delayed
5740 ;; autoselection.
5741 (mouse-autoselect-window-start mouse-position window))))))
5743 (defun handle-select-window (event)
5744 "Handle select-window events."
5745 (interactive "e")
5746 (let ((window (posn-window (event-start event))))
5747 (unless (or (not (window-live-p window))
5748 ;; Don't switch if we're currently in the minibuffer.
5749 ;; This tries to work around problems where the
5750 ;; minibuffer gets unselected unexpectedly, and where
5751 ;; you then have to move your mouse all the way down to
5752 ;; the minibuffer to select it.
5753 (window-minibuffer-p (selected-window))
5754 ;; Don't switch to minibuffer window unless it's active.
5755 (and (window-minibuffer-p window)
5756 (not (minibuffer-window-active-p window)))
5757 ;; Don't switch when autoselection shall be delayed.
5758 (and (numberp mouse-autoselect-window)
5759 (not (zerop mouse-autoselect-window))
5760 (not (eq mouse-autoselect-window-state 'select))
5761 (progn
5762 ;; Cancel any delayed autoselection.
5763 (mouse-autoselect-window-cancel t)
5764 ;; Start delayed autoselection from current mouse
5765 ;; position and window.
5766 (mouse-autoselect-window-start (mouse-position) window)
5767 ;; Executing a command cancels delayed autoselection.
5768 (add-hook
5769 'pre-command-hook 'mouse-autoselect-window-cancel))))
5770 (when mouse-autoselect-window
5771 ;; Reset state of delayed autoselection.
5772 (setq mouse-autoselect-window-state nil)
5773 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
5774 (run-hooks 'mouse-leave-buffer-hook))
5775 ;; Clear echo area.
5776 (message nil)
5777 (select-window window))))
5779 (defun truncated-partial-width-window-p (&optional window)
5780 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
5781 WINDOW defaults to the selected window.
5782 Return nil if WINDOW is not a partial-width window
5783 (regardless of the value of `truncate-lines').
5784 Otherwise, consult the value of `truncate-partial-width-windows'
5785 for the buffer shown in WINDOW."
5786 (unless window
5787 (setq window (selected-window)))
5788 (unless (window-full-width-p window)
5789 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
5790 (window-buffer window))))
5791 (if (integerp t-p-w-w)
5792 (< (window-width window) t-p-w-w)
5793 t-p-w-w))))
5795 ;; Some of these are in tutorial--default-keys, so update that if you
5796 ;; change these.
5797 (define-key ctl-x-map "0" 'delete-window)
5798 (define-key ctl-x-map "1" 'delete-other-windows)
5799 (define-key ctl-x-map "2" 'split-window-below)
5800 (define-key ctl-x-map "3" 'split-window-right)
5801 (define-key ctl-x-map "o" 'other-window)
5802 (define-key ctl-x-map "^" 'enlarge-window)
5803 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
5804 (define-key ctl-x-map "{" 'shrink-window-horizontally)
5805 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
5806 (define-key ctl-x-map "+" 'balance-windows)
5807 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
5809 ;;; window.el ends here