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