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