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