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