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