]> code.delx.au - gnu-emacs/blob - lisp/window.el
Update doc-string of display-buffer-alist.
[gnu-emacs] / lisp / window.el
1 ;;; window.el --- GNU Emacs window commands aside from those written in C
2
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2011
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8 ;; Package: emacs
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Commentary:
26
27 ;; Window tree functions.
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 (defmacro save-selected-window (&rest body)
34 "Execute BODY, then select the previously selected window.
35 The value returned is the value of the last form in BODY.
36
37 This macro saves and restores the selected window, as well as the
38 selected window in each frame. If the previously selected window
39 is no longer live, then whatever window is selected at the end of
40 BODY remains selected. If the previously selected window of some
41 frame is no longer live at the end of BODY, that frame's selected
42 window is left alone.
43
44 This macro saves and restores the current buffer, since otherwise
45 its normal operation could make a different buffer current. The
46 order of recently selected windows and the buffer list ordering
47 are not altered by this macro (unless they are altered in BODY)."
48 (declare (indent 0) (debug t))
49 `(let ((save-selected-window-window (selected-window))
50 ;; It is necessary to save all of these, because calling
51 ;; select-window changes frame-selected-window for whatever
52 ;; frame that window is in.
53 (save-selected-window-alist
54 (mapcar (lambda (frame) (cons frame (frame-selected-window frame)))
55 (frame-list))))
56 (save-current-buffer
57 (unwind-protect
58 (progn ,@body)
59 (dolist (elt save-selected-window-alist)
60 (and (frame-live-p (car elt))
61 (window-live-p (cdr elt))
62 (set-frame-selected-window (car elt) (cdr elt) 'norecord)))
63 (when (window-live-p save-selected-window-window)
64 (select-window save-selected-window-window 'norecord))))))
65
66 ;; The following two functions are like `window-next' and `window-prev'
67 ;; but the WINDOW argument is _not_ optional (so they don't substitute
68 ;; the selected window for nil), and they return nil when WINDOW doesn't
69 ;; have a parent (like a frame's root window or a minibuffer window).
70 (defsubst window-right (window)
71 "Return WINDOW's right sibling.
72 Return nil if WINDOW is the root window of its frame. WINDOW can
73 be any window."
74 (and window (window-parent window) (window-next window)))
75
76 (defsubst window-left (window)
77 "Return WINDOW's left sibling.
78 Return nil if WINDOW is the root window of its frame. WINDOW can
79 be any window."
80 (and window (window-parent window) (window-prev window)))
81
82 (defsubst window-child (window)
83 "Return WINDOW's first child window."
84 (or (window-vchild window) (window-hchild window)))
85
86 (defun window-child-count (window)
87 "Return number of WINDOW's child windows."
88 (let ((count 0))
89 (when (and (windowp window) (setq window (window-child window)))
90 (while window
91 (setq count (1+ count))
92 (setq window (window-next window))))
93 count))
94
95 (defun window-last-child (window)
96 "Return last child window of WINDOW."
97 (when (and (windowp window) (setq window (window-child window)))
98 (while (window-next window)
99 (setq window (window-next window))))
100 window)
101
102 (defsubst window-any-p (object)
103 "Return t if OBJECT denotes a live or internal window."
104 (and (windowp object)
105 (or (window-buffer object) (window-child object))
106 t))
107
108 ;; The following four functions should probably go to subr.el.
109 (defsubst normalize-live-buffer (buffer-or-name)
110 "Return buffer specified by BUFFER-OR-NAME.
111 BUFFER-OR-NAME must be either a buffer or a string naming a live
112 buffer and defaults to the current buffer."
113 (cond
114 ((not buffer-or-name)
115 (current-buffer))
116 ((bufferp buffer-or-name)
117 (if (buffer-live-p buffer-or-name)
118 buffer-or-name
119 (error "Buffer %s is not a live buffer" buffer-or-name)))
120 ((get-buffer buffer-or-name))
121 (t
122 (error "No such buffer %s" buffer-or-name))))
123
124 (defsubst normalize-live-frame (frame)
125 "Return frame specified by FRAME.
126 FRAME must be a live frame and defaults to the selected frame."
127 (if frame
128 (if (frame-live-p frame)
129 frame
130 (error "%s is not a live frame" frame))
131 (selected-frame)))
132
133 (defsubst normalize-any-window (window)
134 "Return window specified by WINDOW.
135 WINDOW must be a window that has not been deleted and defaults to
136 the selected window."
137 (if window
138 (if (window-any-p window)
139 window
140 (error "%s is not a window" window))
141 (selected-window)))
142
143 (defsubst normalize-live-window (window)
144 "Return live window specified by WINDOW.
145 WINDOW must be a live window and defaults to the selected one."
146 (if window
147 (if (and (windowp window) (window-buffer window))
148 window
149 (error "%s is not a live window" window))
150 (selected-window)))
151
152 (defvar ignore-window-parameters nil
153 "If non-nil, standard functions ignore window parameters.
154 The functions currently affected by this are `split-window',
155 `delete-window', `delete-other-windows' and `other-window'.
156
157 An application may bind this to a non-nil value around calls to
158 these functions to inhibit processing of window parameters.")
159
160 (defconst window-safe-min-height 1
161 "The absolut minimum number of lines of a window.
162 Anything less might crash Emacs.")
163
164 (defcustom window-min-height 4
165 "The minimum number of lines of any window.
166 The value has to accomodate a mode- or header-line if present. A
167 value less than `window-safe-min-height' is ignored. The value
168 of this variable is honored when windows are resized or split.
169
170 Applications should never rebind this variable. To resize a
171 window to a height less than the one specified here, an
172 application should instead call `resize-window' with a non-nil
173 IGNORE argument. In order to have `split-window' make a window
174 shorter, explictly specify the SIZE argument of that function."
175 :type 'integer
176 :version "24.1"
177 :group 'windows)
178
179 (defconst window-safe-min-width 2
180 "The absolut minimum number of columns of a window.
181 Anything less might crash Emacs.")
182
183 (defcustom window-min-width 10
184 "The minimum number of columns of any window.
185 The value has to accomodate margins, fringes, or scrollbars if
186 present. A value less than `window-safe-min-width' is ignored.
187 The value of this variable is honored when windows are resized or
188 split.
189
190 Applications should never rebind this variable. To resize a
191 window to a width less than the one specified here, an
192 application should instead call `resize-window' with a non-nil
193 IGNORE argument. In order to have `split-window' make a window
194 narrower, explictly specify the SIZE argument of that function."
195 :type 'integer
196 :version "24.1"
197 :group 'windows)
198
199 (defun window-iso-combination-p (&optional window horizontal)
200 "If WINDOW is a vertical combination return WINDOW's first child.
201 WINDOW can be any window and defaults to the selected one.
202 Optional argument HORIZONTAL non-nil means return WINDOW's first
203 child if WINDOW is a horizontal combination."
204 (setq window (normalize-any-window window))
205 (if horizontal
206 (window-hchild window)
207 (window-vchild window)))
208
209 (defsubst window-iso-combined-p (&optional window horizontal)
210 "Return non-nil if and only if WINDOW is vertically combined.
211 WINDOW can be any window and defaults to the selected one.
212 Optional argument HORIZONTAL non-nil means return non-nil if and
213 only if WINDOW is horizontally combined."
214 (setq window (normalize-any-window window))
215 (let ((parent (window-parent window)))
216 (and parent (window-iso-combination-p parent horizontal))))
217
218 (defun window-iso-combinations (&optional window horizontal)
219 "Return largest number of vertically arranged subwindows of WINDOW.
220 WINDOW can be any window and defaults to the selected one.
221 Optional argument HORIZONTAL non-nil means to return the largest
222 number of horizontally arranged subwindows of WINDOW."
223 (setq window (normalize-any-window window))
224 (cond
225 ((window-live-p window)
226 ;; If WINDOW is live, return 1.
227 1)
228 ((window-iso-combination-p window horizontal)
229 ;; If WINDOW is iso-combined, return the sum of the values for all
230 ;; subwindows of WINDOW.
231 (let ((child (window-child window))
232 (count 0))
233 (while child
234 (setq count
235 (+ (window-iso-combinations child horizontal)
236 count))
237 (setq child (window-right child)))
238 count))
239 (t
240 ;; If WINDOW is not iso-combined, return the maximum value of any
241 ;; subwindow of WINDOW.
242 (let ((child (window-child window))
243 (count 1))
244 (while child
245 (setq count
246 (max (window-iso-combinations child horizontal)
247 count))
248 (setq child (window-right child)))
249 count))))
250
251 (defun walk-window-tree-1 (proc walk-window-tree-window any &optional sub-only)
252 "Helper function for `walk-window-tree' and `walk-window-subtree'."
253 (let (walk-window-tree-buffer)
254 (while walk-window-tree-window
255 (setq walk-window-tree-buffer
256 (window-buffer walk-window-tree-window))
257 (when (or walk-window-tree-buffer any)
258 (funcall proc walk-window-tree-window))
259 (unless walk-window-tree-buffer
260 (walk-window-tree-1
261 proc (window-hchild walk-window-tree-window) any)
262 (walk-window-tree-1
263 proc (window-vchild walk-window-tree-window) any))
264 (if sub-only
265 (setq walk-window-tree-window nil)
266 (setq walk-window-tree-window
267 (window-right walk-window-tree-window))))))
268
269 (defun walk-window-tree (proc &optional frame any)
270 "Run function PROC on each live window of FRAME.
271 PROC must be a function with one argument - a window. FRAME must
272 be a live frame and defaults to the selected one. ANY, if
273 non-nil means to run PROC on all live and internal windows of
274 FRAME.
275
276 This function performs a pre-order, depth-first traversal of the
277 window tree. If PROC changes the window tree, the result is
278 unpredictable."
279 (let ((walk-window-tree-frame (normalize-live-frame frame)))
280 (walk-window-tree-1
281 proc (frame-root-window walk-window-tree-frame) any)))
282
283 (defun walk-window-subtree (proc &optional window any)
284 "Run function PROC on each live subwindow of WINDOW.
285 WINDOW defaults to the selected window. PROC must be a function
286 with one argument - a window. ANY, if non-nil means to run PROC
287 on all live and internal subwindows of WINDOW.
288
289 This function performs a pre-order, depth-first traversal of the
290 window tree rooted at WINDOW. If PROC changes that window tree,
291 the result is unpredictable."
292 (setq window (normalize-any-window window))
293 (walk-window-tree-1 proc window any t))
294
295 (defun windows-with-parameter (parameter &optional value frame any values)
296 "Return a list of all windows on FRAME with PARAMETER non-nil.
297 FRAME defaults to the selected frame. Optional argument VALUE
298 non-nil means only return windows whose window-parameter value of
299 PARAMETER equals VALUE \(comparison is done using `equal').
300 Optional argument ANY non-nil means consider internal windows
301 too. Optional argument VALUES non-nil means return a list of cons
302 cells whose car is the value of the parameter and whose cdr is
303 the window."
304 (let (this-value windows)
305 (walk-window-tree
306 (lambda (window)
307 (when (and (setq this-value (window-parameter window parameter))
308 (or (not value) (or (equal value this-value))))
309 (setq windows
310 (if values
311 (cons (cons this-value window) windows)
312 (cons window windows)))))
313 frame any)
314
315 (nreverse windows)))
316
317 (defun window-with-parameter (parameter &optional value frame any)
318 "Return first window on FRAME with PARAMETER non-nil.
319 FRAME defaults to the selected frame. Optional argument VALUE
320 non-nil means only return a window whose window-parameter value
321 for PARAMETER equals VALUE \(comparison is done with `equal').
322 Optional argument ANY non-nil means consider internal windows
323 too."
324 (let (this-value windows)
325 (catch 'found
326 (walk-window-tree
327 (lambda (window)
328 (when (and (setq this-value (window-parameter window parameter))
329 (or (not value) (equal value this-value)))
330 (throw 'found window)))
331 frame any))))
332
333 ;;; Atomic windows.
334 (defun window-atom-root (&optional window)
335 "Return root of atomic window WINDOW is a part of.
336 WINDOW can be any window and defaults to the selected one.
337 Return nil if WINDOW is not part of a atomic window."
338 (setq window (normalize-any-window window))
339 (let (root)
340 (while (and window (window-parameter window 'window-atom))
341 (setq root window)
342 (setq window (window-parent window)))
343 root))
344
345 (defun make-window-atom (window)
346 "Make WINDOW an atomic window.
347 WINDOW must be an internal window. Return WINDOW."
348 (if (not (window-child window))
349 (error "Window %s is not an internal window" window)
350 (walk-window-subtree
351 (lambda (window)
352 (set-window-parameter window 'window-atom t))
353 window t)
354 window))
355
356 (defun window-atom-check-1 (window)
357 "Subroutine of `window-atom-check'."
358 (when window
359 (if (window-parameter window 'window-atom)
360 (let ((count 0))
361 (when (or (catch 'reset
362 (walk-window-subtree
363 (lambda (window)
364 (if (window-parameter window 'window-atom)
365 (setq count (1+ count))
366 (throw 'reset t)))
367 window t))
368 ;; count >= 1 must hold here. If there's no other
369 ;; window around dissolve this atomic window.
370 (= count 1))
371 ;; Dissolve atomic window.
372 (walk-window-subtree
373 (lambda (window)
374 (set-window-parameter window 'window-atom nil))
375 window t)))
376 ;; Check children.
377 (unless (window-buffer window)
378 (window-atom-check-1 (window-hchild window))
379 (window-atom-check-1 (window-vchild window))))
380 ;; Check right sibling
381 (window-atom-check-1 (window-right window))))
382
383 (defun window-atom-check (&optional frame)
384 "Check atomicity of all windows on FRAME.
385 FRAME defaults to the selected frame. If an atomic window is
386 wrongly configured, reset the atomicity of all its subwindows to
387 nil. An atomic window is wrongly configured if it has no
388 subwindows or one of its subwindows is not atomic."
389 (window-atom-check-1 (frame-root-window frame)))
390
391 ;; Side windows.
392 (defvar window-sides '(left top right bottom)
393 "Window sides.")
394
395 (defcustom window-sides-vertical nil
396 "If non-nil, left and right side windows are full height.
397 Otherwise, top and bottom side windows are full width."
398 :type 'boolean
399 :group 'windows
400 :version "24.1")
401
402 (defcustom window-sides-slots '(nil nil nil nil)
403 "Maximum number of side window slots.
404 The value is a list of four elements specifying the number of
405 side window slots on \(in this order) the left, top, right and
406 bottom side of each frame. If an element is a number, this means
407 to display at most that many side windows on the corresponding
408 side. If an element is nil, this means there's no bound on the
409 number of slots on that side."
410 :risky t
411 :type
412 '(list
413 :value (nil nil nil nil)
414 (choice
415 :tag "Left"
416 :help-echo "Maximum slots of left side window."
417 :value nil
418 :format "%[Left%] %v\n"
419 (const :tag "Unlimited" :format "%t" nil)
420 (integer :tag "Number" :value 2 :size 5))
421 (choice
422 :tag "Top"
423 :help-echo "Maximum slots of top side window."
424 :value nil
425 :format "%[Top%] %v\n"
426 (const :tag "Unlimited" :format "%t" nil)
427 (integer :tag "Number" :value 3 :size 5))
428 (choice
429 :tag "Right"
430 :help-echo "Maximum slots of right side window."
431 :value nil
432 :format "%[Right%] %v\n"
433 (const :tag "Unlimited" :format "%t" nil)
434 (integer :tag "Number" :value 2 :size 5))
435 (choice
436 :tag "Bottom"
437 :help-echo "Maximum slots of bottom side window."
438 :value nil
439 :format "%[Bottom%] %v\n"
440 (const :tag "Unlimited" :format "%t" nil)
441 (integer :tag "Number" :value 3 :size 5)))
442 :group 'windows)
443
444 (defun window-side-check (&optional frame)
445 "Check the window-side parameter of all windows on FRAME.
446 FRAME defaults to the selected frame. If the configuration is
447 invalid, reset all window-side parameters to nil.
448
449 A valid configuration has to preserve the following invariant:
450
451 - If a window has a non-nil window-side parameter, it must have a
452 parent window and the parent window's window-side parameter
453 must be either nil or the same as for window.
454
455 - If windows with non-nil window-side parameters exist, there
456 must be at most one window of each side and non-side with a
457 parent whose window-side parameter is nil and there must be no
458 leaf window whose window-side parameter is nil."
459 (let (normal none left top right bottom
460 side parent parent-side code)
461 (when (or (catch 'reset
462 (walk-window-tree
463 (lambda (window)
464 (setq side (window-parameter window 'window-side))
465 (setq parent (window-parent window))
466 (setq parent-side
467 (and parent (window-parameter parent 'window-side)))
468 ;; The following `cond' seems a bit tedious, but I'd
469 ;; rather stick to using just the stack.
470 (cond
471 (parent-side
472 (when (not (eq parent-side side))
473 ;; A parent whose window-side is non-nil must
474 ;; have a child with the same window-side.
475 (throw 'reset t)))
476 ;; Now check that there's more than one main window
477 ;; for any of none, left, top, right and bottom.
478 ((eq side 'none)
479 (if none
480 (throw 'reset t)
481 (setq none t)))
482 ((eq side 'left)
483 (if left
484 (throw 'reset t)
485 (setq left t)))
486 ((eq side 'top)
487 (if top
488 (throw 'reset t)
489 (setq top t)))
490 ((eq side 'right)
491 (if right
492 (throw 'reset t)
493 (setq right t)))
494 ((eq side 'bottom)
495 (if bottom
496 (throw 'reset t)
497 (setq bottom t)))
498 ((window-buffer window)
499 ;; A leaf window without window-side parameter,
500 ;; record its existence.
501 (setq normal t))))
502 frame t))
503 (if none
504 ;; At least one non-side window exists, so there must
505 ;; be at least one side-window and no normal window.
506 (or (not (or left top right bottom)) normal)
507 ;; No non-side window exists, so there must be no side
508 ;; window either.
509 (or left top right bottom)))
510 (walk-window-tree
511 (lambda (window)
512 (set-window-parameter window 'window-side nil))
513 frame t))))
514
515 (defun window-check (&optional frame)
516 "Check atomic and side windows on FRAME.
517 FRAME defaults to the selected frame."
518 (window-side-check frame)
519 (window-atom-check frame))
520
521 ;;; Window sizes.
522 (defvar window-size-fixed nil
523 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
524 If the value is `height', then only the window's height is fixed.
525 If the value is `width', then only the window's width is fixed.
526 Any other non-nil value fixes both the width and the height.
527
528 Emacs won't change the size of any window displaying that buffer,
529 unless it has no other choice \(like when deleting a neighboring
530 window).")
531 (make-variable-buffer-local 'window-size-fixed)
532
533 (defsubst window-size-ignore (window ignore)
534 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
535 (if (window-any-p ignore) (eq window ignore) ignore))
536
537 (defun window-min-size (&optional window horizontal ignore)
538 "Return the minimum number of lines of WINDOW.
539 WINDOW can be an arbitrary window and defaults to the selected
540 one. Optional argument HORIZONTAL non-nil means return the
541 minimum number of columns of WINDOW.
542
543 Optional argument IGNORE non-nil means ignore any restrictions
544 imposed by fixed size windows, `window-min-height' or
545 `window-min-width' settings. IGNORE equal `safe' means live
546 windows may get as small as `window-safe-min-height' lines and
547 `window-safe-min-width' columns. IGNORE a window means ignore
548 restrictions for that window only."
549 (window-min-size-1
550 (normalize-any-window window) horizontal ignore))
551
552 (defun window-min-size-1 (window horizontal ignore)
553 "Internal function of `window-min-size'."
554 (let ((sub (window-child window)))
555 (if sub
556 (let ((value 0))
557 ;; WINDOW is an internal window.
558 (if (window-iso-combined-p sub horizontal)
559 ;; The minimum size of an iso-combination is the sum of
560 ;; the minimum sizes of its subwindows.
561 (while sub
562 (setq value (+ value
563 (window-min-size-1 sub horizontal ignore)))
564 (setq sub (window-right sub)))
565 ;; The minimum size of an ortho-combination is the maximum of
566 ;; the minimum sizes of its subwindows.
567 (while sub
568 (setq value (max value
569 (window-min-size-1 sub horizontal ignore)))
570 (setq sub (window-right sub))))
571 value)
572 (with-current-buffer (window-buffer window)
573 (cond
574 ((and (not (window-size-ignore window ignore))
575 (window-size-fixed-p window horizontal))
576 ;; The minimum size of a fixed size window is its size.
577 (window-total-size window horizontal))
578 ((or (eq ignore 'safe) (eq ignore window))
579 ;; If IGNORE equals `safe' or WINDOW return the safe values.
580 (if horizontal window-safe-min-width window-safe-min-height))
581 (horizontal
582 ;; For the minimum width of a window take fringes and
583 ;; scroll-bars into account. This is questionable and should
584 ;; be removed as soon as we are able to split (and resize)
585 ;; windows such that the new (or resized) windows can get a
586 ;; size less than the user-specified `window-min-height' and
587 ;; `window-min-width'.
588 (let ((frame (window-frame window))
589 (fringes (window-fringes window))
590 (scroll-bars (window-scroll-bars window)))
591 (max
592 (+ window-safe-min-width
593 (ceiling (car fringes) (frame-char-width frame))
594 (ceiling (cadr fringes) (frame-char-width frame))
595 (cond
596 ((memq (nth 2 scroll-bars) '(left right))
597 (nth 1 scroll-bars))
598 ((memq (frame-parameter frame 'vertical-scroll-bars)
599 '(left right))
600 (ceiling (or (frame-parameter frame 'scroll-bar-width) 14)
601 (frame-char-width)))
602 (t 0)))
603 (if (and (not (window-size-ignore window ignore))
604 (numberp window-min-width))
605 window-min-width
606 0))))
607 (t
608 ;; For the minimum height of a window take any mode- or
609 ;; header-line into account.
610 (max (+ window-safe-min-height
611 (if header-line-format 1 0)
612 (if mode-line-format 1 0))
613 (if (and (not (window-size-ignore window ignore))
614 (numberp window-min-height))
615 window-min-height
616 0))))))))
617
618 (defun window-sizable (window delta &optional horizontal ignore)
619 "Return DELTA if DELTA lines can be added to WINDOW.
620 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
621 columns can be added to WINDOW. A return value of zero means
622 that no lines (or columns) can be added to WINDOW.
623
624 This function looks only at WINDOW and its subwindows. The
625 function `window-resizable' looks at other windows as well.
626
627 DELTA positive means WINDOW shall be enlarged by DELTA lines or
628 columns. If WINDOW cannot be enlarged by DELTA lines or columns
629 return the maximum value in the range 0..DELTA by which WINDOW
630 can be enlarged.
631
632 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
633 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
634 return the minimum value in the range DELTA..0 by which WINDOW
635 can be shrunk.
636
637 Optional argument IGNORE non-nil means ignore any restrictions
638 imposed by fixed size windows, `window-min-height' or
639 `window-min-width' settings. IGNORE equal `safe' means live
640 windows may get as small as `window-safe-min-height' lines and
641 `window-safe-min-width' columns. IGNORE any window means ignore
642 restrictions for that window only."
643 (setq window (normalize-any-window window))
644 (cond
645 ((< delta 0)
646 (max (- (window-min-size window horizontal ignore)
647 (window-total-size window horizontal))
648 delta))
649 ((window-size-ignore window ignore)
650 delta)
651 ((> delta 0)
652 (if (window-size-fixed-p window horizontal)
653 0
654 delta))
655 (t 0)))
656
657 (defsubst window-sizable-p (window delta &optional horizontal ignore)
658 "Return t if WINDOW can be resized by DELTA lines.
659 For the meaning of the arguments of this function see the
660 doc-string of `window-sizable'."
661 (setq window (normalize-any-window window))
662 (if (> delta 0)
663 (>= (window-sizable window delta horizontal ignore) delta)
664 (<= (window-sizable window delta horizontal ignore) delta)))
665
666 (defun window-size-fixed-1 (window horizontal)
667 "Internal function for `window-size-fixed-p'."
668 (let ((sub (window-child window)))
669 (catch 'fixed
670 (if sub
671 ;; WINDOW is an internal window.
672 (if (window-iso-combined-p sub horizontal)
673 ;; An iso-combination is fixed size if all its subwindows
674 ;; are fixed-size.
675 (progn
676 (while sub
677 (unless (window-size-fixed-1 sub horizontal)
678 ;; We found a non-fixed-size subwindow, so WINDOW's
679 ;; size is not fixed.
680 (throw 'fixed nil))
681 (setq sub (window-right sub)))
682 ;; All subwindows are fixed-size, so WINDOW's size is
683 ;; fixed.
684 (throw 'fixed t))
685 ;; An ortho-combination is fixed-size if at least one of its
686 ;; subwindows is fixed-size.
687 (while sub
688 (when (window-size-fixed-1 sub horizontal)
689 ;; We found a fixed-size subwindow, so WINDOW's size is
690 ;; fixed.
691 (throw 'fixed t))
692 (setq sub (window-right sub))))
693 ;; WINDOW is a live window.
694 (with-current-buffer (window-buffer window)
695 (if horizontal
696 (memq window-size-fixed '(width t))
697 (memq window-size-fixed '(height t))))))))
698
699 (defun window-size-fixed-p (&optional window horizontal)
700 "Return non-nil if WINDOW's height is fixed.
701 WINDOW can be an arbitrary window and defaults to the selected
702 window. Optional argument HORIZONTAL non-nil means return
703 non-nil if WINDOW's width is fixed.
704
705 If this function returns nil, this does not necessarily mean that
706 WINDOW can be resized in the desired direction. The functions
707 `window-resizable' and `window-resizable-p' will tell that."
708 (window-size-fixed-1
709 (normalize-any-window window) horizontal))
710
711 (defun window-min-delta-1 (window delta &optional horizontal ignore trail noup)
712 "Internal function for `window-min-delta'."
713 (if (not (window-parent window))
714 ;; If we can't go up, return zero.
715 0
716 ;; Else try to find a non-fixed-size sibling of WINDOW.
717 (let* ((parent (window-parent window))
718 (sub (window-child parent)))
719 (catch 'done
720 (if (window-iso-combined-p sub horizontal)
721 ;; In an iso-combination throw DELTA if we find at least one
722 ;; subwindow and that subwindow is either not of fixed-size
723 ;; or we can ignore fixed-sizeness.
724 (let ((skip (eq trail 'after)))
725 (while sub
726 (cond
727 ((eq sub window)
728 (setq skip (eq trail 'before)))
729 (skip)
730 ((and (not (window-size-ignore window ignore))
731 (window-size-fixed-p sub horizontal)))
732 (t
733 ;; We found a non-fixed-size subwindow.
734 (throw 'done delta)))
735 (setq sub (window-right sub))))
736 ;; In an ortho-combination set DELTA to the minimum value by
737 ;; which other subwindows can shrink.
738 (while sub
739 (unless (eq sub window)
740 (setq delta
741 (min delta
742 (- (window-total-size sub horizontal)
743 (window-min-size sub horizontal ignore)))))
744 (setq sub (window-right sub))))
745 (if noup
746 delta
747 (window-min-delta-1 parent delta horizontal ignore trail))))))
748
749 (defun window-min-delta (&optional window horizontal ignore trail noup nodown)
750 "Return number of lines by which WINDOW can be shrunk.
751 WINDOW can be an arbitrary window and defaults to the selected
752 window. Return zero if WINDOW cannot be shrunk.
753
754 Optional argument HORIZONTAL non-nil means return number of
755 columns by which WINDOW can be shrunk.
756
757 Optional argument IGNORE non-nil means ignore any restrictions
758 imposed by fixed size windows, `window-min-height' or
759 `window-min-width' settings. IGNORE a window means ignore
760 restrictions for that window only. IGNORE equal `safe' means
761 live windows may get as small as `window-safe-min-height' lines
762 and `window-safe-min-width' columns.
763
764 Optional argument TRAIL `before' means only windows to the left
765 of or above WINDOW can be enlarged. Optional argument TRAIL
766 `after' means only windows to the right of or below WINDOW can be
767 enlarged.
768
769 Optional argument NOUP non-nil means don't go up in the window
770 tree but try to enlarge windows within WINDOW's combination only.
771
772 Optional argument NODOWN non-nil means don't check whether WINDOW
773 itself \(and its subwindows) can be shrunk; check only whether at
774 least one other windows can be enlarged appropriately."
775 (setq window (normalize-any-window window))
776 (let ((size (window-total-size window horizontal))
777 (minimum (window-min-size window horizontal ignore)))
778 (cond
779 (nodown
780 ;; If NODOWN is t, try to recover the entire size of WINDOW.
781 (window-min-delta-1 window size horizontal ignore trail noup))
782 ((= size minimum)
783 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
784 ;; there's nothing to recover.
785 0)
786 (t
787 ;; Otherwise, try to recover whatever WINDOW is larger than its
788 ;; minimum size.
789 (window-min-delta-1
790 window (- size minimum) horizontal ignore trail noup)))))
791
792 (defun window-max-delta-1 (window delta &optional horizontal ignore trail noup)
793 "Internal function of `window-max-delta'."
794 (if (not (window-parent window))
795 ;; Can't go up. Return DELTA.
796 delta
797 (let* ((parent (window-parent window))
798 (sub (window-child parent)))
799 (catch 'fixed
800 (if (window-iso-combined-p sub horizontal)
801 ;; For an iso-combination calculate how much we can get from
802 ;; other subwindows.
803 (let ((skip (eq trail 'after)))
804 (while sub
805 (cond
806 ((eq sub window)
807 (setq skip (eq trail 'before)))
808 (skip)
809 (t
810 (setq delta
811 (+ delta
812 (- (window-total-size sub horizontal)
813 (window-min-size sub horizontal ignore))))))
814 (setq sub (window-right sub))))
815 ;; For an ortho-combination throw DELTA when at least one
816 ;; subwindow is fixed-size.
817 (while sub
818 (when (and (not (eq sub window))
819 (not (window-size-ignore sub ignore))
820 (window-size-fixed-p sub horizontal))
821 (throw 'fixed delta))
822 (setq sub (window-right sub))))
823 (if noup
824 ;; When NOUP is nil, DELTA is all we can get.
825 delta
826 ;; Else try with parent of WINDOW, passing the DELTA we
827 ;; recovered so far.
828 (window-max-delta-1 parent delta horizontal ignore trail))))))
829
830 (defun window-max-delta (&optional window horizontal ignore trail noup nodown)
831 "Return maximum number of lines WINDOW by which WINDOW can be enlarged.
832 WINDOW can be an arbitrary window and defaults to the selected
833 window. The return value is zero if WINDOW cannot be enlarged.
834
835 Optional argument HORIZONTAL non-nil means return maximum number
836 of columns by which WINDOW can be enlarged.
837
838 Optional argument IGNORE non-nil means ignore any restrictions
839 imposed by fixed size windows, `window-min-height' or
840 `window-min-width' settings. IGNORE a window means ignore
841 restrictions for that window only. IGNORE equal `safe' means
842 live windows may get as small as `window-safe-min-height' lines
843 and `window-safe-min-width' columns.
844
845 Optional argument TRAIL `before' means only windows to the left
846 of or below WINDOW can be shrunk. Optional argument TRAIL
847 `after' means only windows to the right of or above WINDOW can be
848 shrunk.
849
850 Optional argument NOUP non-nil means don't go up in the window
851 tree but try to obtain the entire space from windows within
852 WINDOW's combination.
853
854 Optional argument NODOWN non-nil means do not check whether
855 WINDOW itself \(and its subwindows) can be enlarged; check only
856 whether other windows can be shrunk appropriately."
857 (setq window (normalize-any-window window))
858 (if (and (not (window-size-ignore window ignore))
859 (not nodown) (window-size-fixed-p window horizontal))
860 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
861 ;; size.
862 0
863 ;; WINDOW has no fixed size.
864 (window-max-delta-1 window 0 horizontal ignore trail noup)))
865
866 ;; Make NOUP also inhibit the min-size check.
867 (defun window-resizable (window delta &optional horizontal ignore trail noup nodown)
868 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
869 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
870 can be resized horizontally by DELTA columns. A return value of
871 zero means that WINDOW is not resizable.
872
873 DELTA positive means WINDOW shall be enlarged by DELTA lines or
874 columns. If WINDOW cannot be enlarged by DELTA lines or columns
875 return the maximum value in the range 0..DELTA by which WINDOW
876 can be enlarged.
877
878 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
879 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
880 return the minimum value in the range DELTA..0 that can be used
881 for shrinking WINDOW.
882
883 Optional argument IGNORE non-nil means ignore any restrictions
884 imposed by fixed size windows, `window-min-height' or
885 `window-min-width' settings. IGNORE a window means ignore
886 restrictions for that window only. IGNORE equal `safe' means
887 live windows may get as small as `window-safe-min-height' lines
888 and `window-safe-min-width' columns.
889
890 Optional argument TRAIL `before' means only windows to the left
891 of or below WINDOW can be shrunk. Optional argument TRAIL
892 `after' means only windows to the right of or above WINDOW can be
893 shrunk.
894
895 Optional argument NOUP non-nil means don't go up in the window
896 tree but try to distribute the space among the other windows
897 within WINDOW's combination.
898
899 Optional argument NODOWN non-nil means don't check whether WINDOW
900 and its subwindows can be resized."
901 (setq window (normalize-any-window window))
902 (cond
903 ((< delta 0)
904 (max (- (window-min-delta window horizontal ignore trail noup nodown))
905 delta))
906 ((> delta 0)
907 (min (window-max-delta window horizontal ignore trail noup nodown)
908 delta))
909 (t 0)))
910
911 (defun window-resizable-p (window delta &optional horizontal ignore trail noup nodown)
912 "Return t if WINDOW can be resized vertically by DELTA lines.
913 For the meaning of the arguments of this function see the
914 doc-string of `window-resizable'."
915 (setq window (normalize-any-window window))
916 (if (> delta 0)
917 (>= (window-resizable window delta horizontal ignore trail noup nodown)
918 delta)
919 (<= (window-resizable window delta horizontal ignore trail noup nodown)
920 delta)))
921
922 (defsubst window-total-height (&optional window)
923 "Return the total number of lines of WINDOW.
924 WINDOW can be any window and defaults to the selected one. The
925 return value includes WINDOW's mode line and header line, if any.
926 If WINDOW is internal the return value is the sum of the total
927 number of lines of WINDOW's child windows if these are vertically
928 combined and the height of WINDOW's first child otherwise.
929
930 Note: This function does not take into account the value of
931 `line-spacing' when calculating the number of lines in WINDOW."
932 (window-total-size window))
933
934 ;; Eventually we should make `window-height' obsolete.
935 (defalias 'window-height 'window-total-height)
936
937 ;; See discussion in bug#4543.
938 (defsubst window-full-height-p (&optional window)
939 "Return t if WINDOW is as high as the containing frame.
940 More precisely, return t if and only if the total height of
941 WINDOW equals the total height of the root window of WINDOW's
942 frame. WINDOW can be any window and defaults to the selected
943 one."
944 (setq window (normalize-any-window window))
945 (= (window-total-size window)
946 (window-total-size (frame-root-window window))))
947
948 (defsubst window-total-width (&optional window)
949 "Return the total number of columns of WINDOW.
950 WINDOW can be any window and defaults to the selected one. The
951 return value includes any vertical dividers or scrollbars of
952 WINDOW. If WINDOW is internal, the return value is the sum of
953 the total number of columns of WINDOW's child windows if these
954 are horizontally combined and the width of WINDOW's first child
955 otherwise."
956 (window-total-size window t))
957
958 (defsubst window-full-width-p (&optional window)
959 "Return t if WINDOW is as wide as the containing frame.
960 More precisely, return t if and only if the total width of WINDOW
961 equals the total width of the root window of WINDOW's frame.
962 WINDOW can be any window and defaults to the selected one."
963 (setq window (normalize-any-window window))
964 (= (window-total-size window t)
965 (window-total-size (frame-root-window window) t)))
966
967 (defsubst window-body-height (&optional window)
968 "Return the number of lines of WINDOW's body.
969 WINDOW must be a live window and defaults to the selected one.
970
971 The return value does not include WINDOW's mode line and header
972 line, if any. If a line at the bottom of the window is only
973 partially visible, that line is included in the return value. If
974 you do not want to include a partially visible bottom line in the
975 return value, use `window-text-height' instead."
976 (window-body-size window))
977
978 (defsubst window-body-width (&optional window)
979 "Return the number of columns of WINDOW's body.
980 WINDOW must be a live window and defaults to the selected one.
981
982 The return value does not include any vertical dividers or scroll
983 bars owned by WINDOW. On a window-system the return value does
984 not include the number of columns used for WINDOW's fringes or
985 display margins either."
986 (window-body-size window t))
987
988 ;; Eventually we should make `window-height' obsolete.
989 (defalias 'window-width 'window-body-width)
990
991 (defun window-current-scroll-bars (&optional window)
992 "Return the current scroll bar settings for WINDOW.
993 WINDOW must be a live window and defaults to the selected one.
994
995 The return value is a cons cell (VERTICAL . HORIZONTAL) where
996 VERTICAL specifies the current location of the vertical scroll
997 bars (`left', `right', or nil), and HORIZONTAL specifies the
998 current location of the horizontal scroll bars (`top', `bottom',
999 or nil).
1000
1001 Unlike `window-scroll-bars', this function reports the scroll bar
1002 type actually used, once frame defaults and `scroll-bar-mode' are
1003 taken into account."
1004 (setq window (normalize-live-window window))
1005 (let ((vert (nth 2 (window-scroll-bars window)))
1006 (hor nil))
1007 (when (or (eq vert t) (eq hor t))
1008 (let ((fcsb (frame-current-scroll-bars (window-frame window))))
1009 (if (eq vert t)
1010 (setq vert (car fcsb)))
1011 (if (eq hor t)
1012 (setq hor (cdr fcsb)))))
1013 (cons vert hor)))
1014
1015 (defun walk-windows (proc &optional minibuf all-frames)
1016 "Cycle through all live windows, calling PROC for each one.
1017 PROC must specify a function with a window as its sole argument.
1018 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1019 windows to include in the walk.
1020
1021 MINIBUF t means include the minibuffer window even if the
1022 minibuffer is not active. MINIBUF nil or omitted means include
1023 the minibuffer window only if the minibuffer is active. Any
1024 other value means do not include the minibuffer window even if
1025 the minibuffer is active.
1026
1027 ALL-FRAMES nil or omitted means consider all windows on the
1028 selected frame, plus the minibuffer window if specified by the
1029 MINIBUF argument. If the minibuffer counts, consider all windows
1030 on all frames that share that minibuffer too. The following
1031 non-nil values of ALL-FRAMES have special meanings:
1032
1033 - t means consider all windows on all existing frames.
1034
1035 - `visible' means consider all windows on all visible frames on
1036 the current terminal.
1037
1038 - 0 (the number zero) means consider all windows on all visible
1039 and iconified frames on the current terminal.
1040
1041 - A frame means consider all windows on that frame only.
1042
1043 Anything else means consider all windows on the selected frame
1044 and no others.
1045
1046 This function changes neither the order of recently selected
1047 windows nor the buffer list."
1048 ;; If we start from the minibuffer window, don't fail to come
1049 ;; back to it.
1050 (when (window-minibuffer-p (selected-window))
1051 (setq minibuf t))
1052 ;; Make sure to not mess up the order of recently selected
1053 ;; windows. Use `save-selected-window' and `select-window'
1054 ;; with second argument non-nil for this purpose.
1055 (save-selected-window
1056 (when (framep all-frames)
1057 (select-window (frame-first-window all-frames) 'norecord))
1058 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
1059 (funcall proc walk-windows-window))))
1060
1061 (defun window-in-direction-2 (window posn &optional horizontal)
1062 "Support function for `window-in-direction'."
1063 (if horizontal
1064 (let ((top (window-top-line window)))
1065 (if (> top posn)
1066 (- top posn)
1067 (- posn top (window-total-height window))))
1068 (let ((left (window-left-column window)))
1069 (if (> left posn)
1070 (- left posn)
1071 (- posn left (window-total-width window))))))
1072
1073 (defun window-in-direction (direction &optional window ignore)
1074 "Return window in DIRECTION as seen from WINDOW.
1075 DIRECTION must be one of `above', `below', `left' or `right'.
1076 WINDOW must be a live window and defaults to the selected one.
1077 IGNORE, when non-nil means a window can be returned even if its
1078 `no-other-window' parameter is non-nil."
1079 (setq window (normalize-live-window window))
1080 (unless (memq direction '(above below left right))
1081 (error "Wrong direction %s" direction))
1082 (let* ((frame (window-frame window))
1083 (hor (memq direction '(left right)))
1084 (first (if hor
1085 (window-left-column window)
1086 (window-top-line window)))
1087 (last (+ first (if hor
1088 (window-total-width window)
1089 (window-total-height window))))
1090 (posn-cons (nth 6 (posn-at-point (window-point window) window)))
1091 ;; The column / row value of `posn-at-point' can be nil for the
1092 ;; mini-window, guard against that.
1093 (posn (if hor
1094 (+ (or (cdr posn-cons) 1) (window-top-line window))
1095 (+ (or (car posn-cons) 1) (window-left-column window))))
1096 (best-edge
1097 (cond
1098 ((eq direction 'below) (frame-height frame))
1099 ((eq direction 'right) (frame-width frame))
1100 (t -1)))
1101 (best-edge-2 best-edge)
1102 (best-diff-2 (if hor (frame-height frame) (frame-width frame)))
1103 best best-2 best-diff-2-new)
1104 (walk-window-tree
1105 (lambda (w)
1106 (let* ((w-top (window-top-line w))
1107 (w-left (window-left-column w)))
1108 (cond
1109 ((or (eq window w)
1110 ;; Ignore ourselves.
1111 (and (window-parameter w 'no-other-window)
1112 ;; Ignore W unless IGNORE is non-nil.
1113 (not ignore))))
1114 (hor
1115 (cond
1116 ((and (<= w-top posn)
1117 (< posn (+ w-top (window-total-height w))))
1118 ;; W is to the left or right of WINDOW and covers POSN.
1119 (when (or (and (eq direction 'left)
1120 (<= w-left first) (> w-left best-edge))
1121 (and (eq direction 'right)
1122 (>= w-left last) (< w-left best-edge)))
1123 (setq best-edge w-left)
1124 (setq best w)))
1125 ((and (or (and (eq direction 'left)
1126 (<= (+ w-left (window-total-width w)) first))
1127 (and (eq direction 'right) (<= last w-left)))
1128 ;; W is to the left or right of WINDOW but does not
1129 ;; cover POSN.
1130 (setq best-diff-2-new
1131 (window-in-direction-2 w posn hor))
1132 (or (< best-diff-2-new best-diff-2)
1133 (and (= best-diff-2-new best-diff-2)
1134 (if (eq direction 'left)
1135 (> w-left best-edge-2)
1136 (< w-left best-edge-2)))))
1137 (setq best-edge-2 w-left)
1138 (setq best-diff-2 best-diff-2-new)
1139 (setq best-2 w))))
1140 (t
1141 (cond
1142 ((and (<= w-left posn)
1143 (< posn (+ w-left (window-total-width w))))
1144 ;; W is above or below WINDOW and covers POSN.
1145 (when (or (and (eq direction 'above)
1146 (<= w-top first) (> w-top best-edge))
1147 (and (eq direction 'below)
1148 (>= w-top first) (< w-top best-edge)))
1149 (setq best-edge w-top)
1150 (setq best w)))
1151 ((and (or (and (eq direction 'above)
1152 (<= (+ w-top (window-total-height w)) first))
1153 (and (eq direction 'below) (<= last w-top)))
1154 ;; W is above or below WINDOW but does not cover POSN.
1155 (setq best-diff-2-new
1156 (window-in-direction-2 w posn hor))
1157 (or (< best-diff-2-new best-diff-2)
1158 (and (= best-diff-2-new best-diff-2)
1159 (if (eq direction 'above)
1160 (> w-top best-edge-2)
1161 (< w-top best-edge-2)))))
1162 (setq best-edge-2 w-top)
1163 (setq best-diff-2 best-diff-2-new)
1164 (setq best-2 w)))))))
1165 (window-frame window))
1166 (or best best-2)))
1167
1168 (defun get-window-with-predicate (predicate &optional minibuf
1169 all-frames default)
1170 "Return a live window satisfying PREDICATE.
1171 More precisely, cycle through all windows calling the function
1172 PREDICATE on each one of them with the window as its sole
1173 argument. Return the first window for which PREDICATE returns
1174 non-nil. If no window satisfies PREDICATE, return DEFAULT.
1175
1176 ALL-FRAMES nil or omitted means consider all windows on the selected
1177 frame, plus the minibuffer window if specified by the MINIBUF
1178 argument. If the minibuffer counts, consider all windows on all
1179 frames that share that minibuffer too. The following non-nil
1180 values of ALL-FRAMES have special meanings:
1181
1182 - t means consider all windows on all existing frames.
1183
1184 - `visible' means consider all windows on all visible frames on
1185 the current terminal.
1186
1187 - 0 (the number zero) means consider all windows on all visible
1188 and iconified frames on the current terminal.
1189
1190 - A frame means consider all windows on that frame only.
1191
1192 Anything else means consider all windows on the selected frame
1193 and no others."
1194 (catch 'found
1195 (dolist (window (window-list-1 nil minibuf all-frames))
1196 (when (funcall predicate window)
1197 (throw 'found window)))
1198 default))
1199
1200 (defalias 'some-window 'get-window-with-predicate)
1201
1202 (defun get-lru-window (&optional all-frames dedicated)
1203 "Return the least recently used window on frames specified by ALL-FRAMES.
1204 Return a full-width window if possible. A minibuffer window is
1205 never a candidate. A dedicated window is never a candidate
1206 unless DEDICATED is non-nil, so if all windows are dedicated, the
1207 value is nil. Avoid returning the selected window if possible.
1208
1209 The following non-nil values of the optional argument ALL-FRAMES
1210 have special meanings:
1211
1212 - t means consider all windows on all existing frames.
1213
1214 - `visible' means consider all windows on all visible frames on
1215 the current terminal.
1216
1217 - 0 (the number zero) means consider all windows on all visible
1218 and iconified frames on the current terminal.
1219
1220 - A frame means consider all windows on that frame only.
1221
1222 Any other value of ALL-FRAMES means consider all windows on the
1223 selected frame and no others."
1224 (let (best-window best-time second-best-window second-best-time time)
1225 (dolist (window (window-list-1 nil nil all-frames))
1226 (when (or dedicated (not (window-dedicated-p window)))
1227 (setq time (window-use-time window))
1228 (if (or (eq window (selected-window))
1229 (not (window-full-width-p window)))
1230 (when (or (not second-best-time) (< time second-best-time))
1231 (setq second-best-time time)
1232 (setq second-best-window window))
1233 (when (or (not best-time) (< time best-time))
1234 (setq best-time time)
1235 (setq best-window window)))))
1236 (or best-window second-best-window)))
1237
1238 (defun get-mru-window (&optional all-frames)
1239 "Return the most recently used window on frames specified by ALL-FRAMES.
1240 Do not return a minibuffer window.
1241
1242 The following non-nil values of the optional argument ALL-FRAMES
1243 have special meanings:
1244
1245 - t means consider all windows on all existing frames.
1246
1247 - `visible' means consider all windows on all visible frames on
1248 the current terminal.
1249
1250 - 0 (the number zero) means consider all windows on all visible
1251 and iconified frames on the current terminal.
1252
1253 - A frame means consider all windows on that frame only.
1254
1255 Any other value of ALL-FRAMES means consider all windows on the
1256 selected frame and no others."
1257 (let (best-window best-time time)
1258 (dolist (window (window-list-1 nil nil all-frames))
1259 (setq time (window-use-time window))
1260 (when (or (not best-time) (> time best-time))
1261 (setq best-time time)
1262 (setq best-window window)))
1263 best-window))
1264
1265 (defun get-largest-window (&optional all-frames dedicated)
1266 "Return the largest window on frames specified by ALL-FRAMES.
1267 A minibuffer window is never a candidate. A dedicated window is
1268 never a candidate unless DEDICATED is non-nil, so if all windows
1269 are dedicated, the value is nil.
1270
1271 The following non-nil values of the optional argument ALL-FRAMES
1272 have special meanings:
1273
1274 - t means consider all windows on all existing frames.
1275
1276 - `visible' means consider all windows on all visible frames on
1277 the current terminal.
1278
1279 - 0 (the number zero) means consider all windows on all visible
1280 and iconified frames on the current terminal.
1281
1282 - A frame means consider all windows on that frame only.
1283
1284 Any other value of ALL-FRAMES means consider all windows on the
1285 selected frame and no others."
1286 (let ((best-size 0)
1287 best-window size)
1288 (dolist (window (window-list-1 nil nil all-frames))
1289 (when (or dedicated (not (window-dedicated-p window)))
1290 (setq size (* (window-total-size window)
1291 (window-total-size window t)))
1292 (when (> size best-size)
1293 (setq best-size size)
1294 (setq best-window window))))
1295 best-window))
1296
1297 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
1298 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
1299 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
1300 and defaults to the current buffer.
1301
1302 Any windows showing BUFFER-OR-NAME on the selected frame are listed
1303 first.
1304
1305 MINIBUF t means include the minibuffer window even if the
1306 minibuffer is not active. MINIBUF nil or omitted means include
1307 the minibuffer window only if the minibuffer is active. Any
1308 other value means do not include the minibuffer window even if
1309 the minibuffer is active.
1310
1311 ALL-FRAMES nil or omitted means consider all windows on the
1312 selected frame, plus the minibuffer window if specified by the
1313 MINIBUF argument. If the minibuffer counts, consider all windows
1314 on all frames that share that minibuffer too. The following
1315 non-nil values of ALL-FRAMES have special meanings:
1316
1317 - t means consider all windows on all existing frames.
1318
1319 - `visible' means consider all windows on all visible frames on
1320 the current terminal.
1321
1322 - 0 (the number zero) means consider all windows on all visible
1323 and iconified frames on the current terminal.
1324
1325 - A frame means consider all windows on that frame only.
1326
1327 Anything else means consider all windows on the selected frame
1328 and no others."
1329 (let ((buffer (normalize-live-buffer buffer-or-name))
1330 windows)
1331 (dolist (window (window-list-1 (frame-first-window) minibuf all-frames))
1332 (when (eq (window-buffer window) buffer)
1333 (setq windows (cons window windows))))
1334 (nreverse windows)))
1335
1336 (defun minibuffer-window-active-p (window)
1337 "Return t if WINDOW is the currently active minibuffer window."
1338 (eq window (active-minibuffer-window)))
1339
1340 (defun count-windows (&optional minibuf)
1341 "Return the number of live windows on the selected frame.
1342 The optional argument MINIBUF specifies whether the minibuffer
1343 window shall be counted. See `walk-windows' for the precise
1344 meaning of this argument."
1345 (length (window-list-1 nil minibuf)))
1346 \f
1347 ;;; Resizing windows.
1348 (defun resize-window-reset (&optional frame horizontal)
1349 "Reset resize values for all windows on FRAME.
1350 FRAME defaults to the selected frame.
1351
1352 This function stores the current value of `window-total-size' applied
1353 with argument HORIZONTAL in the new total size of all windows on
1354 FRAME. It also resets the new normal size of each of these
1355 windows."
1356 (resize-window-reset-1
1357 (frame-root-window (normalize-live-frame frame)) horizontal))
1358
1359 (defun resize-window-reset-1 (window horizontal)
1360 "Internal function of `resize-window-reset'."
1361 ;; Register old size in the new total size.
1362 (set-window-new-total window (window-total-size window horizontal))
1363 ;; Reset new normal size.
1364 (set-window-new-normal window)
1365 (when (window-child window)
1366 (resize-window-reset-1 (window-child window) horizontal))
1367 (when (window-right window)
1368 (resize-window-reset-1 (window-right window) horizontal)))
1369
1370 ;; The following routine is used to manually resize the minibuffer
1371 ;; window and is currently used, for example, by ispell.el.
1372 (defun resize-mini-window (window delta)
1373 "Resize minibuffer window WINDOW by DELTA lines.
1374 If WINDOW cannot be resized by DELTA lines make it as large \(or
1375 as small) as possible but don't signal an error."
1376 (when (window-minibuffer-p window)
1377 (let* ((frame (window-frame window))
1378 (root (frame-root-window frame))
1379 (height (window-total-size window))
1380 (min-delta
1381 (- (window-total-size root)
1382 (window-min-size root))))
1383 ;; Sanitize DELTA.
1384 (cond
1385 ((<= (+ height delta) 0)
1386 (setq delta (- (- height 1))))
1387 ((> delta min-delta)
1388 (setq delta min-delta)))
1389
1390 ;; Resize now.
1391 (resize-window-reset frame)
1392 ;; Ideally we should be able to resize just the last subwindow of
1393 ;; root here. See the comment in `resize-root-window-vertically'
1394 ;; for why we do not do that.
1395 (resize-this-window root (- delta) nil nil t)
1396 (set-window-new-total window (+ height delta))
1397 ;; The following routine catches the case where we want to resize
1398 ;; a minibuffer-only frame.
1399 (resize-mini-window-internal window))))
1400
1401 (defun resize-window (window delta &optional horizontal ignore)
1402 "Resize WINDOW vertically by DELTA lines.
1403 WINDOW can be an arbitrary window and defaults to the selected
1404 one. An attempt to resize the root window of a frame will raise
1405 an error though.
1406
1407 DELTA a positive number means WINDOW shall be enlarged by DELTA
1408 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
1409 lines.
1410
1411 Optional argument HORIZONTAL non-nil means resize WINDOW
1412 horizontally by DELTA columns. In this case a positive DELTA
1413 means enlarge WINDOW by DELTA columns. DELTA negative means
1414 WINDOW shall be shrunk by -DELTA columns.
1415
1416 Optional argument IGNORE non-nil means ignore any restrictions
1417 imposed by fixed size windows, `window-min-height' or
1418 `window-min-width' settings. IGNORE any window means ignore
1419 restrictions for that window only. IGNORE equal `safe' means
1420 live windows may get as small as `window-safe-min-height' lines
1421 and `window-safe-min-width' columns.
1422
1423 This function resizes other windows proportionally and never
1424 deletes any windows. If you want to move only the low (right)
1425 edge of WINDOW consider using `adjust-window-trailing-edge'
1426 instead."
1427 (setq window (normalize-any-window window))
1428 (let* ((frame (window-frame window))
1429 sibling)
1430 (cond
1431 ((eq window (frame-root-window frame))
1432 (error "Cannot resize the root window of a frame"))
1433 ((window-minibuffer-p window)
1434 (resize-mini-window window delta))
1435 ((window-resizable-p window delta horizontal ignore)
1436 (resize-window-reset frame horizontal)
1437 (resize-this-window window delta horizontal ignore t)
1438 (if (and (not (window-splits window))
1439 (window-iso-combined-p window horizontal)
1440 (setq sibling (or (window-right window) (window-left window)))
1441 (window-sizable-p sibling (- delta) horizontal ignore))
1442 ;; If window-splits returns nil for WINDOW, WINDOW is part of
1443 ;; an iso-combination, and WINDOW's neighboring right or left
1444 ;; sibling can be resized as requested, resize that sibling.
1445 (let ((normal-delta
1446 (/ (float delta)
1447 (window-total-size (window-parent window) horizontal))))
1448 (resize-this-window sibling (- delta) horizontal nil t)
1449 (set-window-new-normal
1450 window (+ (window-normal-size window horizontal)
1451 normal-delta))
1452 (set-window-new-normal
1453 sibling (- (window-normal-size sibling horizontal)
1454 normal-delta)))
1455 ;; Otherwise, resize all other windows in the same combination.
1456 (resize-other-windows window delta horizontal ignore))
1457 (resize-window-apply frame horizontal))
1458 (t
1459 (error "Cannot resize window %s" window)))))
1460
1461 (defsubst resize-subwindows-skip-p (window)
1462 "Return non-nil if WINDOW shall be skipped by resizing routines."
1463 (memq (window-new-normal window) '(ignore stuck skip)))
1464
1465 (defun resize-subwindows-normal (parent horizontal window this-delta &optional trail other-delta)
1466 "Set the new normal height of subwindows of window PARENT.
1467 HORIZONTAL non-nil means set the new normal width of these
1468 windows. WINDOW specifies a subwindow of PARENT that has been
1469 resized by THIS-DELTA lines \(columns).
1470
1471 Optional argument TRAIL either 'before or 'after means set values
1472 for windows before or after WINDOW only. Optional argument
1473 OTHER-DELTA a number specifies that this many lines \(columns)
1474 have been obtained from \(or returned to) an ancestor window of
1475 PARENT in order to resize WINDOW."
1476 (let* ((delta-normal
1477 (if (and (= (- this-delta) (window-total-size window horizontal))
1478 (zerop other-delta))
1479 ;; When WINDOW gets deleted and we can return its entire
1480 ;; space to its siblings, use WINDOW's normal size as the
1481 ;; normal delta.
1482 (- (window-normal-size window horizontal))
1483 ;; In any other case calculate the normal delta from the
1484 ;; relation of THIS-DELTA to the total size of PARENT.
1485 (/ (float this-delta) (window-total-size parent horizontal))))
1486 (sub (window-child parent))
1487 (parent-normal 0.0)
1488 (skip (eq trail 'after)))
1489
1490 ;; Set parent-normal to the sum of the normal sizes of all
1491 ;; subwindows of PARENT that shall be resized, excluding only WINDOW
1492 ;; and any windows specified by the optional TRAIL argument.
1493 (while sub
1494 (cond
1495 ((eq sub window)
1496 (setq skip (eq trail 'before)))
1497 (skip)
1498 (t
1499 (setq parent-normal
1500 (+ parent-normal (window-normal-size sub horizontal)))))
1501 (setq sub (window-right sub)))
1502
1503 ;; Set the new normal size of all subwindows of PARENT from what
1504 ;; they should have contributed for recovering THIS-DELTA lines
1505 ;; (columns).
1506 (setq sub (window-child parent))
1507 (setq skip (eq trail 'after))
1508 (while sub
1509 (cond
1510 ((eq sub window)
1511 (setq skip (eq trail 'before)))
1512 (skip)
1513 (t
1514 (let ((old-normal (window-normal-size sub horizontal)))
1515 (set-window-new-normal
1516 sub (min 1.0 ; Don't get larger than 1.
1517 (max (- old-normal
1518 (* (/ old-normal parent-normal)
1519 delta-normal))
1520 ;; Don't drop below 0.
1521 0.0))))))
1522 (setq sub (window-right sub)))
1523
1524 (when (numberp other-delta)
1525 ;; Set the new normal size of windows from what they should have
1526 ;; contributed for recovering OTHER-DELTA lines (columns).
1527 (setq delta-normal (/ (float (window-total-size parent horizontal))
1528 (+ (window-total-size parent horizontal)
1529 other-delta)))
1530 (setq sub (window-child parent))
1531 (setq skip (eq trail 'after))
1532 (while sub
1533 (cond
1534 ((eq sub window)
1535 (setq skip (eq trail 'before)))
1536 (skip)
1537 (t
1538 (set-window-new-normal
1539 sub (min 1.0 ; Don't get larger than 1.
1540 (max (* (window-new-normal sub) delta-normal)
1541 ;; Don't drop below 0.
1542 0.0)))))
1543 (setq sub (window-right sub))))
1544
1545 ;; Set the new normal size of WINDOW to what is left by the sum of
1546 ;; the normal sizes of its siblings.
1547 (set-window-new-normal
1548 window
1549 (let ((sum 0))
1550 (setq sub (window-child parent))
1551 (while sub
1552 (cond
1553 ((eq sub window))
1554 ((not (numberp (window-new-normal sub)))
1555 (setq sum (+ sum (window-normal-size sub horizontal))))
1556 (t
1557 (setq sum (+ sum (window-new-normal sub)))))
1558 (setq sub (window-right sub)))
1559 ;; Don't get larger than 1 or smaller than 0.
1560 (min 1.0 (max (- 1.0 sum) 0.0))))))
1561
1562 (defun resize-subwindows (parent delta &optional horizontal window ignore trail edge)
1563 "Resize subwindows of window PARENT vertically by DELTA lines.
1564 PARENT must be a vertically combined internal window.
1565
1566 Optional argument HORIZONTAL non-nil means resize subwindows of
1567 PARENT horizontally by DELTA columns. In this case PARENT must
1568 be a horizontally combined internal window.
1569
1570 WINDOW, if specified, must denote a child window of PARENT that
1571 is resized by DELTA lines.
1572
1573 Optional argument IGNORE non-nil means ignore any restrictions
1574 imposed by fixed size windows, `window-min-height' or
1575 `window-min-width' settings. IGNORE equal `safe' means live
1576 windows may get as small as `window-safe-min-height' lines and
1577 `window-safe-min-width' columns. IGNORE any window means ignore
1578 restrictions for that window only.
1579
1580 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
1581 of windows that shall be resized. If TRAIL equals `before',
1582 resize only windows on the left or above EDGE. If TRAIL equals
1583 `after', resize only windows on the right or below EDGE. Also,
1584 preferably only resize windows adjacent to EDGE.
1585
1586 Return the symbol `normalized' if new normal sizes have been
1587 already set by this routine."
1588 (let* ((first (window-child parent))
1589 (sub first)
1590 (parent-total (+ (window-total-size parent horizontal) delta))
1591 best-window best-value)
1592
1593 (if (and edge (memq trail '(before after))
1594 (progn
1595 (setq sub first)
1596 (while (and (window-right sub)
1597 (or (and (eq trail 'before)
1598 (not (resize-subwindows-skip-p
1599 (window-right sub))))
1600 (and (eq trail 'after)
1601 (resize-subwindows-skip-p sub))))
1602 (setq sub (window-right sub)))
1603 sub)
1604 (if horizontal
1605 (if (eq trail 'before)
1606 (= (+ (window-left-column sub)
1607 (window-total-size sub t))
1608 edge)
1609 (= (window-left-column sub) edge))
1610 (if (eq trail 'before)
1611 (= (+ (window-top-line sub)
1612 (window-total-size sub))
1613 edge)
1614 (= (window-top-line sub) edge)))
1615 (window-sizable-p sub delta horizontal ignore))
1616 ;; Resize only windows adjacent to EDGE.
1617 (progn
1618 (resize-this-window sub delta horizontal ignore t trail edge)
1619 (if (and window (eq (window-parent sub) parent))
1620 (progn
1621 ;; Assign new normal sizes.
1622 (set-window-new-normal
1623 sub (/ (float (window-new-total sub)) parent-total))
1624 (set-window-new-normal
1625 window (- (window-normal-size window horizontal)
1626 (- (window-new-normal sub)
1627 (window-normal-size sub horizontal)))))
1628 (resize-subwindows-normal parent horizontal sub 0 trail delta))
1629 ;; Return 'normalized to notify `resize-other-windows' that
1630 ;; normal sizes have been already set.
1631 'normalized)
1632 ;; Resize all windows proportionally.
1633 (setq sub first)
1634 (while sub
1635 (cond
1636 ((or (resize-subwindows-skip-p sub)
1637 ;; Ignore windows to skip and fixed-size subwindows - in
1638 ;; the latter case make it a window to skip.
1639 (and (not ignore)
1640 (window-size-fixed-p sub horizontal)
1641 (set-window-new-normal sub 'ignore))))
1642 ((< delta 0)
1643 ;; When shrinking store the number of lines/cols we can get
1644 ;; from this window here together with the total/normal size
1645 ;; factor.
1646 (set-window-new-normal
1647 sub
1648 (cons
1649 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
1650 (window-min-delta sub horizontal ignore trail t) ; t)
1651 (- (/ (float (window-total-size sub horizontal))
1652 parent-total)
1653 (window-normal-size sub horizontal)))))
1654 ((> delta 0)
1655 ;; When enlarging store the total/normal size factor only
1656 (set-window-new-normal
1657 sub
1658 (- (/ (float (window-total-size sub horizontal))
1659 parent-total)
1660 (window-normal-size sub horizontal)))))
1661
1662 (setq sub (window-right sub)))
1663
1664 (cond
1665 ((< delta 0)
1666 ;; Shrink windows by delta.
1667 (setq best-window t)
1668 (while (and best-window (not (zerop delta)))
1669 (setq sub first)
1670 (setq best-window nil)
1671 (setq best-value most-negative-fixnum)
1672 (while sub
1673 (when (and (consp (window-new-normal sub))
1674 (not (zerop (car (window-new-normal sub))))
1675 (> (cdr (window-new-normal sub)) best-value))
1676 (setq best-window sub)
1677 (setq best-value (cdr (window-new-normal sub))))
1678
1679 (setq sub (window-right sub)))
1680
1681 (when best-window
1682 (setq delta (1+ delta)))
1683 (set-window-new-total best-window -1 t)
1684 (set-window-new-normal
1685 best-window
1686 (if (= (car (window-new-normal best-window)) 1)
1687 'skip ; We can't shrink best-window any further.
1688 (cons (1- (car (window-new-normal best-window)))
1689 (- (/ (float (window-new-total best-window))
1690 parent-total)
1691 (window-normal-size best-window horizontal)))))))
1692 ((> delta 0)
1693 ;; Enlarge windows by delta.
1694 (setq best-window t)
1695 (while (and best-window (not (zerop delta)))
1696 (setq sub first)
1697 (setq best-window nil)
1698 (setq best-value most-positive-fixnum)
1699 (while sub
1700 (when (and (numberp (window-new-normal sub))
1701 (< (window-new-normal sub) best-value))
1702 (setq best-window sub)
1703 (setq best-value (window-new-normal sub)))
1704
1705 (setq sub (window-right sub)))
1706
1707 (when best-window
1708 (setq delta (1- delta)))
1709 (set-window-new-total best-window 1 t)
1710 (set-window-new-normal
1711 best-window
1712 (- (/ (float (window-new-total best-window))
1713 parent-total)
1714 (window-normal-size best-window horizontal))))))
1715
1716 (when best-window
1717 (setq sub first)
1718 (while sub
1719 (when (or (consp (window-new-normal sub))
1720 (numberp (window-new-normal sub)))
1721 ;; Reset new normal size fields so `resize-window-apply'
1722 ;; won't use them to apply new sizes.
1723 (set-window-new-normal sub))
1724
1725 (unless (eq (window-new-normal sub) 'ignore)
1726 ;; Resize this subwindow's subwindows (back-engineering
1727 ;; delta from sub's old and new total sizes).
1728 (let ((delta (- (window-new-total sub)
1729 (window-total-size sub horizontal))))
1730 (unless (and (zerop delta) (not trail))
1731 ;; For the TRAIL non-nil case we have to resize SUB
1732 ;; recursively even if it's size does not change.
1733 (resize-this-window
1734 sub delta horizontal ignore nil trail edge))))
1735 (setq sub (window-right sub)))))))
1736
1737 (defun resize-other-windows (window delta &optional horizontal ignore trail edge)
1738 "Resize other windows when WINDOW is resized vertically by DELTA lines.
1739 Optional argument HORIZONTAL non-nil means resize other windows
1740 when WINDOW is resized horizontally by DELTA columns. WINDOW
1741 itself is not resized by this function.
1742
1743 Optional argument IGNORE non-nil means ignore any restrictions
1744 imposed by fixed size windows, `window-min-height' or
1745 `window-min-width' settings. IGNORE equal `safe' means live
1746 windows may get as small as `window-safe-min-height' lines and
1747 `window-safe-min-width' columns. IGNORE any window means ignore
1748 restrictions for that window only.
1749
1750 Optional arguments TRAIL and EDGE, when non-nil, refine the set
1751 of windows that shall be resized. If TRAIL equals `before',
1752 resize only windows on the left or above EDGE. If TRAIL equals
1753 `after', resize only windows on the right or below EDGE. Also,
1754 preferably only resize windows adjacent to EDGE."
1755 (when (window-parent window)
1756 (let* ((parent (window-parent window))
1757 (sub (window-child parent)))
1758 (if (window-iso-combined-p sub horizontal)
1759 ;; In an iso-combination try to extract DELTA from WINDOW's
1760 ;; siblings.
1761 (let ((first sub)
1762 (skip (eq trail 'after))
1763 this-delta other-delta)
1764 ;; Decide which windows shall be left alone.
1765 (while sub
1766 (cond
1767 ((eq sub window)
1768 ;; Make sure WINDOW is left alone when
1769 ;; resizing its siblings.
1770 (set-window-new-normal sub 'ignore)
1771 (setq skip (eq trail 'before)))
1772 (skip
1773 ;; Make sure this sibling is left alone when
1774 ;; resizing its siblings.
1775 (set-window-new-normal sub 'ignore))
1776 ((or (window-size-ignore sub ignore)
1777 (not (window-size-fixed-p sub horizontal)))
1778 ;; Set this-delta to t to signal that we found a sibling
1779 ;; of WINDOW whose size is not fixed.
1780 (setq this-delta t)))
1781
1782 (setq sub (window-right sub)))
1783
1784 ;; Set this-delta to what we can get from WINDOW's siblings.
1785 (if (= (- delta) (window-total-size window horizontal))
1786 ;; A deletion, presumably. We must handle this case
1787 ;; specially since `window-resizable' can't be used.
1788 (if this-delta
1789 ;; There's at least one resizable sibling we can
1790 ;; give WINDOW's size to.
1791 (setq this-delta delta)
1792 ;; No resizable sibling exists.
1793 (setq this-delta 0))
1794 ;; Any other form of resizing.
1795 (setq this-delta
1796 (window-resizable window delta horizontal ignore trail t)))
1797
1798 ;; Set other-delta to what we still have to get from
1799 ;; ancestor windows of parent.
1800 (setq other-delta (- delta this-delta))
1801 (unless (zerop other-delta)
1802 ;; Unless we got everything from WINDOW's siblings, PARENT
1803 ;; must be resized by other-delta lines or columns.
1804 (set-window-new-total parent other-delta 'add))
1805
1806 (if (zerop this-delta)
1807 ;; We haven't got anything from WINDOW's siblings but we
1808 ;; must update the normal sizes to respect other-delta.
1809 (resize-subwindows-normal
1810 parent horizontal window this-delta trail other-delta)
1811 ;; We did get something from WINDOW's siblings which means
1812 ;; we have to resize their subwindows.
1813 (unless (eq (resize-subwindows parent (- this-delta) horizontal
1814 window ignore trail edge)
1815 ;; `resize-subwindows' returning 'normalized,
1816 ;; means it has set the normal sizes already.
1817 'normalized)
1818 ;; Set the normal sizes.
1819 (resize-subwindows-normal
1820 parent horizontal window this-delta trail other-delta))
1821 ;; Set DELTA to what we still have to get from ancestor
1822 ;; windows.
1823 (setq delta other-delta)))
1824
1825 ;; In an ortho-combination all siblings of WINDOW must be
1826 ;; resized by DELTA.
1827 (set-window-new-total parent delta 'add)
1828 (while sub
1829 (unless (eq sub window)
1830 (resize-this-window sub delta horizontal ignore t))
1831 (setq sub (window-right sub))))
1832
1833 (unless (zerop delta)
1834 ;; "Go up."
1835 (resize-other-windows parent delta horizontal ignore trail edge)))))
1836
1837 (defun resize-this-window (window delta &optional horizontal ignore add trail edge)
1838 "Resize WINDOW vertically by DELTA lines.
1839 Optional argument HORIZONTAL non-nil means resize WINDOW
1840 horizontally by DELTA columns.
1841
1842 Optional argument IGNORE non-nil means ignore any restrictions
1843 imposed by fixed size windows, `window-min-height' or
1844 `window-min-width' settings. IGNORE equal `safe' means live
1845 windows may get as small as `window-safe-min-height' lines and
1846 `window-safe-min-width' columns. IGNORE any window means ignore
1847 restrictions for that window only.
1848
1849 Optional argument ADD non-nil means add DELTA to the new total
1850 size of WINDOW.
1851
1852 Optional arguments TRAIL and EDGE, when non-nil, refine the set
1853 of windows that shall be resized. If TRAIL equals `before',
1854 resize only windows on the left or above EDGE. If TRAIL equals
1855 `after', resize only windows on the right or below EDGE. Also,
1856 preferably only resize windows adjacent to EDGE.
1857
1858 This function recursively resizes WINDOW's subwindows to fit the
1859 new size. Make sure that WINDOW is `window-resizable' before
1860 calling this function. Note that this function does not resize
1861 siblings of WINDOW or WINDOW's parent window. You have to
1862 eventually call `resize-window-apply' in order to make resizing
1863 actually take effect."
1864 (when add
1865 ;; Add DELTA to the new total size of WINDOW.
1866 (set-window-new-total window delta t))
1867
1868 (let ((sub (window-child window)))
1869 (cond
1870 ((not sub))
1871 ((window-iso-combined-p sub horizontal)
1872 ;; In an iso-combination resize subwindows according to their
1873 ;; normal sizes.
1874 (resize-subwindows window delta horizontal nil ignore trail edge))
1875 ;; In an ortho-combination resize each subwindow by DELTA.
1876 (t
1877 (while sub
1878 (resize-this-window sub delta horizontal ignore t trail edge)
1879 (setq sub (window-right sub)))))))
1880
1881 (defun resize-root-window (window delta horizontal ignore)
1882 "Resize root window WINDOW vertically by DELTA lines.
1883 HORIZONTAL non-nil means resize root window WINDOW horizontally
1884 by DELTA columns.
1885
1886 IGNORE non-nil means ignore any restrictions imposed by fixed
1887 size windows, `window-min-height' or `window-min-width' settings.
1888
1889 This function is only called by the frame resizing routines. It
1890 resizes windows proportionally and never deletes any windows."
1891 (when (and (windowp window) (numberp delta)
1892 (window-sizable-p window delta horizontal ignore))
1893 (resize-window-reset (window-frame window) horizontal)
1894 (resize-this-window window delta horizontal ignore t)))
1895
1896 (defun resize-root-window-vertically (window delta)
1897 "Resize root window WINDOW vertically by DELTA lines.
1898 If DELTA is less than zero and we can't shrink WINDOW by DELTA
1899 lines, shrink it as much as possible. If DELTA is greater than
1900 zero, this function can resize fixed-size subwindows in order to
1901 recover the necessary lines.
1902
1903 Return the number of lines that were recovered.
1904
1905 This function is only called by the minibuffer window resizing
1906 routines. It resizes windows proportionally and never deletes
1907 any windows."
1908 (when (numberp delta)
1909 (let (ignore)
1910 (cond
1911 ((< delta 0)
1912 (setq delta (window-sizable window delta)))
1913 ((> delta 0)
1914 (unless (window-sizable window delta)
1915 (setq ignore t))))
1916
1917 (resize-window-reset (window-frame window))
1918 ;; Ideally, we would resize just the last window in a combination
1919 ;; but that's not feasible for the following reason: If we grow
1920 ;; the minibuffer window and the last window cannot be shrunk any
1921 ;; more, we shrink another window instead. But if we then shrink
1922 ;; the minibuffer window again, the last window might get enlarged
1923 ;; and the state after shrinking is not the state before growing.
1924 ;; So, in practice, we'd need a history variable to record how to
1925 ;; proceed. But I'm not sure how such a variable could work with
1926 ;; repeated minibuffer window growing steps.
1927 (resize-this-window window delta nil ignore t)
1928 delta)))
1929
1930 (defun adjust-window-trailing-edge (window delta &optional horizontal)
1931 "Move WINDOW's bottom edge by DELTA lines.
1932 Optional argument HORIZONTAL non-nil means move WINDOW's right
1933 edge by DELTA columns. WINDOW defaults to the selected window.
1934
1935 If DELTA is greater zero, then move the edge downwards or to the
1936 right. If DELTA is less than zero, move the edge upwards or to
1937 the left. If the edge can't be moved by DELTA lines or columns,
1938 move it as far as possible in the desired direction."
1939 (setq window (normalize-any-window window))
1940 (let ((frame (window-frame window))
1941 (right window)
1942 left this-delta min-delta max-delta failed)
1943 ;; Find the edge we want to move.
1944 (while (and (or (not (window-iso-combined-p right horizontal))
1945 (not (window-right right)))
1946 (setq right (window-parent right))))
1947 (cond
1948 ((and (not right) (not horizontal) (not resize-mini-windows)
1949 (eq (window-frame (minibuffer-window frame)) frame))
1950 (resize-mini-window (minibuffer-window frame) (- delta)))
1951 ((or (not (setq left right)) (not (setq right (window-right right))))
1952 (if horizontal
1953 (error "No window on the right of this one")
1954 (error "No window below this one")))
1955 (t
1956 ;; Set LEFT to the first resizable window on the left. This step is
1957 ;; needed to handle fixed-size windows.
1958 (while (and left (window-size-fixed-p left horizontal))
1959 (setq left
1960 (or (window-left left)
1961 (progn
1962 (while (and (setq left (window-parent left))
1963 (not (window-iso-combined-p left horizontal))))
1964 (window-left left)))))
1965 (unless left
1966 (if horizontal
1967 (error "No resizable window on the left of this one")
1968 (error "No resizable window above this one")))
1969
1970 ;; Set RIGHT to the first resizable window on the right. This step
1971 ;; is needed to handle fixed-size windows.
1972 (while (and right (window-size-fixed-p right horizontal))
1973 (setq right
1974 (or (window-right right)
1975 (progn
1976 (while (and (setq right (window-parent right))
1977 (not (window-iso-combined-p right horizontal))))
1978 (window-right right)))))
1979 (unless right
1980 (if horizontal
1981 (error "No resizable window on the right of this one")
1982 (error "No resizable window below this one")))
1983
1984 ;; LEFT and RIGHT (which might be both internal windows) are now the
1985 ;; two windows we want to resize.
1986 (cond
1987 ((> delta 0)
1988 (setq max-delta (window-max-delta-1 left 0 horizontal nil 'after))
1989 (setq min-delta (window-min-delta-1 right (- delta) horizontal nil 'before))
1990 (when (or (< max-delta delta) (> min-delta (- delta)))
1991 ;; We can't get the whole DELTA - move as far as possible.
1992 (setq delta (min max-delta (- min-delta))))
1993 (unless (zerop delta)
1994 ;; Start resizing.
1995 (resize-window-reset frame horizontal)
1996 ;; Try to enlarge LEFT first.
1997 (setq this-delta (window-resizable left delta horizontal))
1998 (unless (zerop this-delta)
1999 (resize-this-window
2000 left this-delta horizontal nil t 'before
2001 (if horizontal
2002 (+ (window-left-column left) (window-total-size left t))
2003 (+ (window-top-line left) (window-total-size left)))))
2004 ;; Shrink windows on right of LEFT.
2005 (resize-other-windows
2006 left delta horizontal nil 'after
2007 (if horizontal
2008 (window-left-column right)
2009 (window-top-line right)))))
2010 ((< delta 0)
2011 (setq max-delta (window-max-delta-1 right 0 horizontal nil 'before))
2012 (setq min-delta (window-min-delta-1 left delta horizontal nil 'after))
2013 (when (or (< max-delta (- delta)) (> min-delta delta))
2014 ;; We can't get the whole DELTA - move as far as possible.
2015 (setq delta (max (- max-delta) min-delta)))
2016 (unless (zerop delta)
2017 ;; Start resizing.
2018 (resize-window-reset frame horizontal)
2019 ;; Try to enlarge RIGHT.
2020 (setq this-delta (window-resizable right (- delta) horizontal))
2021 (unless (zerop this-delta)
2022 (resize-this-window
2023 right this-delta horizontal nil t 'after
2024 (if horizontal
2025 (window-left-column right)
2026 (window-top-line right))))
2027 ;; Shrink windows on left of RIGHT.
2028 (resize-other-windows
2029 right (- delta) horizontal nil 'before
2030 (if horizontal
2031 (+ (window-left-column left) (window-total-size left t))
2032 (+ (window-top-line left) (window-total-size left)))))))
2033 (unless (zerop delta)
2034 ;; Don't report an error in the standard case.
2035 (unless (resize-window-apply frame horizontal)
2036 ;; But do report an error if applying the changes fails.
2037 (error "Failed adjusting window %s" window)))))))
2038
2039 (defun enlarge-window (delta &optional horizontal)
2040 "Make selected window DELTA lines taller.
2041 Interactively, if no argument is given, make the selected window
2042 one line taller. If optional argument HORIZONTAL is non-nil,
2043 make selected window wider by DELTA columns. If DELTA is
2044 negative, shrink selected window by -DELTA lines or columns.
2045 Return nil."
2046 (interactive "p")
2047 (cond
2048 ((zerop delta))
2049 ((window-size-fixed-p nil horizontal)
2050 (error "Selected window has fixed size"))
2051 ((window-resizable-p nil delta horizontal)
2052 (resize-window nil delta horizontal))
2053 (t
2054 (resize-window
2055 nil (if (> delta 0)
2056 (window-max-delta nil horizontal)
2057 (- (window-min-delta nil horizontal)))
2058 horizontal))))
2059
2060 (defun shrink-window (delta &optional horizontal)
2061 "Make selected window DELTA lines smaller.
2062 Interactively, if no argument is given, make the selected window
2063 one line smaller. If optional argument HORIZONTAL is non-nil,
2064 make selected window narrower by DELTA columns. If DELTA is
2065 negative, enlarge selected window by -DELTA lines or columns.
2066 Return nil."
2067 (interactive "p")
2068 (cond
2069 ((zerop delta))
2070 ((window-size-fixed-p nil horizontal)
2071 (error "Selected window has fixed size"))
2072 ((window-resizable-p nil (- delta) horizontal)
2073 (resize-window nil (- delta) horizontal))
2074 (t
2075 (resize-window
2076 nil (if (> delta 0)
2077 (- (window-min-delta nil horizontal))
2078 (window-max-delta nil horizontal))
2079 horizontal))))
2080
2081 (defun maximize-window (&optional window)
2082 "Maximize WINDOW.
2083 Make WINDOW as large as possible without deleting any windows.
2084 WINDOW can be any window and defaults to the selected window."
2085 (interactive)
2086 (setq window (normalize-any-window window))
2087 (resize-window window (window-max-delta window))
2088 (resize-window window (window-max-delta window t) t))
2089
2090 (defun minimize-window (&optional window)
2091 "Minimize WINDOW.
2092 Make WINDOW as small as possible without deleting any windows.
2093 WINDOW can be any window and defaults to the selected window."
2094 (interactive)
2095 (setq window (normalize-any-window window))
2096 (resize-window window (- (window-min-delta window)))
2097 (resize-window window (- (window-min-delta window t)) t))
2098 \f
2099 (defsubst frame-root-window-p (window)
2100 "Return non-nil if WINDOW is the root window of its frame."
2101 (eq window (frame-root-window window)))
2102
2103 (defun window-tree-1 (window &optional next)
2104 "Return window tree rooted at WINDOW.
2105 Optional argument NEXT non-nil means include windows right
2106 siblings in the return value.
2107
2108 See the documentation of `window-tree' for a description of the
2109 return value."
2110 (let (list)
2111 (while window
2112 (setq list
2113 (cons
2114 (cond
2115 ((window-vchild window)
2116 (cons t (cons (window-edges window)
2117 (window-tree-1 (window-vchild window) t))))
2118 ((window-hchild window)
2119 (cons nil (cons (window-edges window)
2120 (window-tree-1 (window-hchild window) t))))
2121 (t window))
2122 list))
2123 (setq window (when next (window-next window))))
2124 (nreverse list)))
2125
2126 (defun window-tree (&optional frame)
2127 "Return the window tree of frame FRAME.
2128 FRAME must be a live frame and defaults to the selected frame.
2129 The return value is a list of the form (ROOT MINI), where ROOT
2130 represents the window tree of the frame's root window, and MINI
2131 is the frame's minibuffer window.
2132
2133 If the root window is not split, ROOT is the root window itself.
2134 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
2135 for a horizontal split, and t for a vertical split. EDGES gives
2136 the combined size and position of the subwindows in the split,
2137 and the rest of the elements are the subwindows in the split.
2138 Each of the subwindows may again be a window or a list
2139 representing a window split, and so on. EDGES is a list \(LEFT
2140 TOP RIGHT BOTTOM) as returned by `window-edges'."
2141 (setq frame (normalize-live-frame frame))
2142 (window-tree-1 (frame-root-window frame) t))
2143 \f
2144 (defun other-window (count &optional all-frames)
2145 "Select another window in cyclic ordering of windows.
2146 COUNT specifies the number of windows to skip, starting with the
2147 selected window, before making the selection. If COUNT is
2148 positive, skip COUNT windows forwards. If COUNT is negative,
2149 skip -COUNT windows backwards. COUNT zero means do not skip any
2150 window, so select the selected window. In an interactive call,
2151 COUNT is the numeric prefix argument. Return nil.
2152
2153 If the `other-window' parameter of WINDOW is a function and
2154 `ignore-window-parameters' is nil, call that function with the
2155 arguments COUNT and ALL-FRAMES.
2156
2157 This function does not select a window whose `no-other-window'
2158 window parameter is non-nil.
2159
2160 This function uses `next-window' for finding the window to
2161 select. The argument ALL-FRAMES has the same meaning as in
2162 `next-window', but the MINIBUF argument of `next-window' is
2163 always effectively nil."
2164 (interactive "p")
2165 (let* ((window (selected-window))
2166 (function (and (not ignore-window-parameters)
2167 (window-parameter window 'other-window)))
2168 old-window old-count)
2169 (if (functionp function)
2170 (funcall function count all-frames)
2171 ;; `next-window' and `previous-window' may return a window we are
2172 ;; not allowed to select. Hence we need an exit strategy in case
2173 ;; all windows are non-selectable.
2174 (catch 'exit
2175 (while (> count 0)
2176 (setq window (next-window window nil all-frames))
2177 (cond
2178 ((eq window old-window)
2179 (when (= count old-count)
2180 ;; Keep out of infinite loops. When COUNT has not changed
2181 ;; since we last looked at `window' we're probably in one.
2182 (throw 'exit nil)))
2183 ((window-parameter window 'no-other-window)
2184 (unless old-window
2185 ;; The first non-selectable window `next-window' got us:
2186 ;; Remember it and the current value of COUNT.
2187 (setq old-window window)
2188 (setq old-count count)))
2189 (t
2190 (setq count (1- count)))))
2191 (while (< count 0)
2192 (setq window (previous-window window nil all-frames))
2193 (cond
2194 ((eq window old-window)
2195 (when (= count old-count)
2196 ;; Keep out of infinite loops. When COUNT has not changed
2197 ;; since we last looked at `window' we're probably in one.
2198 (throw 'exit nil)))
2199 ((window-parameter window 'no-other-window)
2200 (unless old-window
2201 ;; The first non-selectable window `previous-window' got
2202 ;; us: Remember it and the current value of COUNT.
2203 (setq old-window window)
2204 (setq old-count count)))
2205 (t
2206 (setq count (1+ count)))))
2207
2208 (select-window window)
2209 ;; Always return nil.
2210 nil))))
2211
2212 ;; This should probably return non-nil when the selected window is part
2213 ;; of an atomic window whose root is the frame's root window.
2214 (defun one-window-p (&optional nomini all-frames)
2215 "Return non-nil if the selected window is the only window.
2216 Optional arg NOMINI non-nil means don't count the minibuffer
2217 even if it is active. Otherwise, the minibuffer is counted
2218 when it is active.
2219
2220 Optional argument ALL-FRAMES specifies the set of frames to
2221 consider, see also `next-window'. ALL-FRAMES nil or omitted
2222 means consider windows on the selected frame only, plus the
2223 minibuffer window if specified by the NOMINI argument. If the
2224 minibuffer counts, consider all windows on all frames that share
2225 that minibuffer too. The remaining non-nil values of ALL-FRAMES
2226 with a special meaning are:
2227
2228 - t means consider all windows on all existing frames.
2229
2230 - `visible' means consider all windows on all visible frames on
2231 the current terminal.
2232
2233 - 0 (the number zero) means consider all windows on all visible
2234 and iconified frames on the current terminal.
2235
2236 - A frame means consider all windows on that frame only.
2237
2238 Anything else means consider all windows on the selected frame
2239 and no others."
2240 (let ((base-window (selected-window)))
2241 (if (and nomini (eq base-window (minibuffer-window)))
2242 (setq base-window (next-window base-window)))
2243 (eq base-window
2244 (next-window base-window (if nomini 'arg) all-frames))))
2245 \f
2246 ;;; Deleting windows.
2247 (defun window-deletable-p (&optional window)
2248 "Return t if WINDOW can be safely deleted from its frame.
2249 Return `frame' if deleting WINDOW should delete its frame
2250 instead."
2251 (setq window (normalize-any-window window))
2252 (unless ignore-window-parameters
2253 ;; Handle atomicity.
2254 (when (window-parameter window 'window-atom)
2255 (setq window (window-atom-root window))))
2256 (let ((parent (window-parent window))
2257 (frame (window-frame window))
2258 (dedicated (and (window-buffer window) (window-dedicated-p window)))
2259 (quit-restore (window-parameter window 'quit-restore)))
2260 (cond
2261 ((frame-root-window-p window)
2262 (when (and (or dedicated
2263 (and (eq (car-safe quit-restore) 'new-frame)
2264 (eq (nth 1 quit-restore) (window-buffer window))))
2265 (other-visible-frames-p frame))
2266 ;; WINDOW is the root window of its frame. Return `frame' but
2267 ;; only if WINDOW is (1) either dedicated or quit-restore's car
2268 ;; is new-frame and the window still displays the same buffer
2269 ;; and (2) there are other frames left.
2270 'frame))
2271 ((and (not ignore-window-parameters)
2272 (eq (window-parameter window 'window-side) 'none)
2273 (or (not parent)
2274 (not (eq (window-parameter parent 'window-side) 'none))))
2275 ;; Can't delete last main window.
2276 nil)
2277 (t))))
2278
2279 (defun window-or-subwindow-p (subwindow window)
2280 "Return t if SUBWINDOW is either WINDOW or a subwindow of WINDOW."
2281 (or (eq subwindow window)
2282 (let ((parent (window-parent subwindow)))
2283 (catch 'done
2284 (while parent
2285 (if (eq parent window)
2286 (throw 'done t)
2287 (setq parent (window-parent parent))))))))
2288
2289 (defun delete-window (&optional window)
2290 "Delete WINDOW.
2291 WINDOW can be an arbitrary window and defaults to the selected
2292 one. Return nil.
2293
2294 If the variable `ignore-window-parameters' is non-nil or the
2295 `delete-window' parameter of WINDOW equals t, do not process any
2296 parameters of WINDOW. Otherwise, if the `delete-window'
2297 parameter of WINDOW specifies a function, call that function with
2298 WINDOW as its sole argument and return the value returned by that
2299 function.
2300
2301 Otherwise, if WINDOW is part of an atomic window, call
2302 `delete-window' with the root of the atomic window as its
2303 argument. If WINDOW is the only window on its frame or the last
2304 non-side window, signal an error."
2305 (interactive)
2306 (setq window (normalize-any-window window))
2307 (let* ((frame (window-frame window))
2308 (function (window-parameter window 'delete-window))
2309 (parent (window-parent window))
2310 atom-root)
2311 (window-check frame)
2312 (catch 'done
2313 ;; Handle window parameters.
2314 (cond
2315 ;; Ignore window parameters if `ignore-window-parameters' tells
2316 ;; us so or `delete-window' equals t.
2317 ((or ignore-window-parameters (eq function t)))
2318 ((functionp function)
2319 ;; The `delete-window' parameter specifies the function to call.
2320 ;; If that function is `ignore' nothing is done. It's up to the
2321 ;; function called here to avoid infinite recursion.
2322 (throw 'done (funcall function window)))
2323 ((and (window-parameter window 'window-atom)
2324 (setq atom-root (window-atom-root window))
2325 (not (eq atom-root window)))
2326 (throw 'done (delete-window atom-root)))
2327 ((and (eq (window-parameter window 'window-side) 'none)
2328 (or (not parent)
2329 (not (eq (window-parameter parent 'window-side) 'none))))
2330 (error "Attempt to delete last non-side window"))
2331 ((not parent)
2332 (error "Attempt to delete minibuffer or sole ordinary window")))
2333
2334 (let* ((horizontal (window-hchild parent))
2335 (size (window-total-size window horizontal))
2336 (frame-selected
2337 (window-or-subwindow-p (frame-selected-window frame) window))
2338 ;; Emacs 23 preferably gives WINDOW's space to its left
2339 ;; sibling.
2340 (sibling (or (window-left window) (window-right window))))
2341 (resize-window-reset frame horizontal)
2342 (cond
2343 ((and (not (window-splits window))
2344 sibling (window-sizable-p sibling size))
2345 ;; Resize WINDOW's sibling.
2346 (resize-this-window sibling size horizontal nil t)
2347 (set-window-new-normal
2348 sibling (+ (window-normal-size sibling horizontal)
2349 (window-normal-size window horizontal))))
2350 ((window-resizable-p window (- size) horizontal nil nil nil t)
2351 ;; Can do without resizing fixed-size windows.
2352 (resize-other-windows window (- size) horizontal))
2353 (t
2354 ;; Can't do without resizing fixed-size windows.
2355 (resize-other-windows window (- size) horizontal t)))
2356 ;; Actually delete WINDOW.
2357 (delete-window-internal window)
2358 (when (and frame-selected
2359 (window-parameter
2360 (frame-selected-window frame) 'no-other-window))
2361 ;; `delete-window-internal' has selected a window that should
2362 ;; not be selected, fix this here.
2363 (other-window -1 frame))
2364 (run-window-configuration-change-hook frame)
2365 (window-check frame)
2366 ;; Always return nil.
2367 nil))))
2368
2369 (defun delete-other-windows (&optional window)
2370 "Make WINDOW fill its frame.
2371 WINDOW may be any window and defaults to the selected one.
2372 Return nil.
2373
2374 If the variable `ignore-window-parameters' is non-nil or the
2375 `delete-other-windows' parameter of WINDOW equals t, do not
2376 process any parameters of WINDOW. Otherwise, if the
2377 `delete-other-windows' parameter of WINDOW specifies a function,
2378 call that function with WINDOW as its sole argument and return
2379 the value returned by that function.
2380
2381 Otherwise, if WINDOW is part of an atomic window, call this
2382 function with the root of the atomic window as its argument. If
2383 WINDOW is a non-side window, make WINDOW the only non-side window
2384 on the frame. Side windows are not deleted. If WINDOW is a side
2385 window signal an error."
2386 (interactive)
2387 (setq window (normalize-any-window window))
2388 (let* ((frame (window-frame window))
2389 (function (window-parameter window 'delete-other-windows))
2390 (window-side (window-parameter window 'window-side))
2391 atom-root side-main)
2392 (window-check frame)
2393 (catch 'done
2394 (cond
2395 ;; Ignore window parameters if `ignore-window-parameters' is t or
2396 ;; `delete-other-windows' is t.
2397 ((or ignore-window-parameters (eq function t)))
2398 ((functionp function)
2399 ;; The `delete-other-windows' parameter specifies the function
2400 ;; to call. If the function is `ignore' no windows are deleted.
2401 ;; It's up to the function called to avoid infinite recursion.
2402 (throw 'done (funcall function window)))
2403 ((and (window-parameter window 'window-atom)
2404 (setq atom-root (window-atom-root window))
2405 (not (eq atom-root window)))
2406 (throw 'done (delete-other-windows atom-root)))
2407 ((eq window-side 'none)
2408 ;; Set side-main to the major non-side window.
2409 (setq side-main (window-with-parameter 'window-side 'none nil t)))
2410 ((memq window-side window-sides)
2411 (error "Cannot make side window the only window")))
2412 ;; If WINDOW is the main non-side window, do nothing.
2413 (unless (eq window side-main)
2414 (delete-other-windows-internal window side-main)
2415 (run-window-configuration-change-hook frame)
2416 (window-check frame))
2417 ;; Always return nil.
2418 nil)))
2419
2420 (defun delete-other-windows-vertically (&optional window)
2421 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
2422 This may be a useful alternative binding for \\[delete-other-windows]
2423 if you often split windows horizontally."
2424 (interactive)
2425 (let* ((window (or window (selected-window)))
2426 (edges (window-edges window))
2427 (w window) delenda)
2428 (while (not (eq (setq w (next-window w 1)) window))
2429 (let ((e (window-edges w)))
2430 (when (and (= (car e) (car edges))
2431 (= (caddr e) (caddr edges)))
2432 (push w delenda))))
2433 (mapc 'delete-window delenda)))
2434
2435 ;;; Windows and buffers.
2436
2437 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
2438 ;; for (1) determining which buffer to show in the window when its
2439 ;; buffer shall be buried or killed and (2) which buffer to show for
2440 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2441
2442 ;; `prev-buffers' consists of <buffer, window-start, window-point>
2443 ;; triples. The entries on this list are ordered by the time their
2444 ;; buffer has been removed from the window, the most recently removed
2445 ;; buffer's entry being first. The window-start and window-point
2446 ;; components are `window-start' and `window-point' at the time the
2447 ;; buffer was removed from the window which implies that the entry must
2448 ;; be added when `set-window-buffer' removes the buffer from the window.
2449
2450 ;; `next-buffers' is the list of buffers that have been replaced
2451 ;; recently by `switch-to-prev-buffer'. These buffers are the least
2452 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
2453 ;; candidates of `switch-to-next-buffer' to switch to. This list is
2454 ;; reset to nil by any action changing the window's buffer with the
2455 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
2456 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
2457 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
2458
2459 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
2460 ;; if such a buffer was killed while the window was hidden within a
2461 ;; window configuration. Such killed buffers get removed whenever
2462 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
2463
2464 ;; The following function is called by `set-window-buffer' _before_ it
2465 ;; replaces the buffer of the argument window with the new buffer.
2466 (defun record-window-buffer (&optional window)
2467 "Record WINDOW's buffer.
2468 WINDOW must be a live window and defaults to the selected one."
2469 (let* ((window (normalize-live-window window))
2470 (buffer (window-buffer window))
2471 (entry (assq buffer (window-prev-buffers window))))
2472 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
2473 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2474 (set-window-next-buffers window nil)
2475
2476 (when entry
2477 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
2478 (set-window-prev-buffers
2479 window (assq-delete-all buffer (window-prev-buffers window))))
2480
2481 ;; Don't record insignificant buffers.
2482 (unless (eq (aref (buffer-name buffer) 0) ?\s)
2483 ;; Add an entry for buffer to WINDOW's previous buffers.
2484 (with-current-buffer buffer
2485 (let ((start (window-start window))
2486 (point (window-point window)))
2487 (setq entry
2488 (cons buffer
2489 (if entry
2490 ;; We have an entry, update marker positions.
2491 (list (set-marker (nth 1 entry) start)
2492 (set-marker (nth 2 entry) point))
2493 ;; Make new markers.
2494 (list (copy-marker start)
2495 (copy-marker point)))))
2496
2497 (set-window-prev-buffers
2498 window (cons entry (window-prev-buffers window))))))))
2499
2500 (defun unrecord-window-buffer (&optional window buffer)
2501 "Unrecord BUFFER in WINDOW.
2502 WINDOW must be a live window and defaults to the selected one.
2503 BUFFER must be a live buffer and defaults to the buffer of
2504 WINDOW."
2505 (let* ((window (normalize-live-window window))
2506 (buffer (or buffer (window-buffer window))))
2507 (set-window-prev-buffers
2508 window (assq-delete-all buffer (window-prev-buffers window)))
2509 (set-window-next-buffers
2510 window (delq buffer (window-next-buffers window)))))
2511
2512 (defun set-window-buffer-start-and-point (window buffer &optional start point)
2513 "Set WINDOW's buffer to BUFFER.
2514 Optional argument START non-nil means set WINDOW's start position
2515 to START. Optional argument POINT non-nil means set WINDOW's
2516 point to POINT. If WINDOW is selected this also sets BUFFER's
2517 `point' to POINT. If WINDOW is selected and the buffer it showed
2518 before was current this also makes BUFFER the current buffer."
2519 (let ((selected (eq window (selected-window)))
2520 (current (eq (window-buffer window) (current-buffer))))
2521 (set-window-buffer window buffer)
2522 (when (and selected current)
2523 (set-buffer buffer))
2524 (when start
2525 (set-window-start window start))
2526 (when point
2527 (if selected
2528 (with-current-buffer buffer
2529 (goto-char point))
2530 (set-window-point window point)))))
2531
2532 (defun switch-to-prev-buffer (&optional window bury-or-kill)
2533 "In WINDOW switch to previous buffer.
2534 WINDOW must be a live window and defaults to the selected one.
2535
2536 Optional argument BURY-OR-KILL non-nil means the buffer currently
2537 shown in WINDOW is about to be buried or killed and consequently
2538 shall not be switched to in future invocations of this command."
2539 (interactive)
2540 (let* ((window (normalize-live-window window))
2541 (old-buffer (window-buffer window))
2542 ;; Save this since it's destroyed by `set-window-buffer'.
2543 (next-buffers (window-next-buffers window))
2544 entry new-buffer killed-buffers deletable visible)
2545 (cond
2546 ;; When BURY-OR-KILL is non-nil, there's no previous buffer for
2547 ;; this window, and we can delete the window (or the frame) do
2548 ;; that.
2549 ((and bury-or-kill
2550 (or (not (window-prev-buffers window))
2551 (and (eq (caar (window-prev-buffers window)) old-buffer)
2552 (not (cdr (car (window-prev-buffers window))))))
2553 (setq deletable (window-deletable-p window)))
2554 (if (eq deletable 'frame)
2555 (delete-frame (window-frame window))
2556 (delete-window window)))
2557 ((window-dedicated-p window)
2558 (error "Window %s is dedicated to buffer %s" window old-buffer)))
2559
2560 (unless deletable
2561 (catch 'found
2562 ;; Scan WINDOW's previous buffers first, skipping entries of next
2563 ;; buffers.
2564 (dolist (entry (window-prev-buffers window))
2565 (when (and (setq new-buffer (car entry))
2566 (or (buffer-live-p new-buffer)
2567 (not (setq killed-buffers
2568 (cons new-buffer killed-buffers))))
2569 (not (eq new-buffer old-buffer))
2570 (or bury-or-kill
2571 (not (memq new-buffer next-buffers))))
2572 (set-window-buffer-start-and-point
2573 window new-buffer (nth 1 entry) (nth 2 entry))
2574 (throw 'found t)))
2575 ;; Scan reverted buffer list of WINDOW's frame next, skipping
2576 ;; entries of next buffers. Note that when we bury or kill a
2577 ;; buffer we don't reverse the global buffer list to avoid showing
2578 ;; a buried buffer instead. Otherwise, we must reverse the global
2579 ;; buffer list in order to make sure that switching to the
2580 ;; previous/next buffer traverse it in opposite directions.
2581 (dolist (buffer (if bury-or-kill
2582 (buffer-list (window-frame window))
2583 (nreverse (buffer-list (window-frame window)))))
2584 (when (and (buffer-live-p buffer)
2585 (not (eq buffer old-buffer))
2586 (not (eq (aref (buffer-name buffer) 0) ?\s))
2587 (or bury-or-kill (not (memq buffer next-buffers))))
2588 (if (get-buffer-window buffer)
2589 ;; Try to avoid showing a buffer visible in some other window.
2590 (setq visible buffer)
2591 (setq new-buffer buffer)
2592 (set-window-buffer-start-and-point window new-buffer)
2593 (throw 'found t))))
2594 (unless bury-or-kill
2595 ;; Scan reverted next buffers last (must not use nreverse
2596 ;; here!).
2597 (dolist (buffer (reverse next-buffers))
2598 ;; Actually, buffer _must_ be live here since otherwise it
2599 ;; would have been caught in the scan of previous buffers.
2600 (when (and (or (buffer-live-p buffer)
2601 (not (setq killed-buffers
2602 (cons buffer killed-buffers))))
2603 (not (eq buffer old-buffer))
2604 (setq entry (assq buffer (window-prev-buffers window))))
2605 (setq new-buffer buffer)
2606 (set-window-buffer-start-and-point
2607 window new-buffer (nth 1 entry) (nth 2 entry))
2608 (throw 'found t))))
2609
2610 ;; Show a buffer visible in another window.
2611 (when visible
2612 (setq new-buffer visible)
2613 (set-window-buffer-start-and-point window new-buffer)))
2614
2615 (if bury-or-kill
2616 ;; Remove `old-buffer' from WINDOW's previous and (restored list
2617 ;; of) next buffers.
2618 (progn
2619 (set-window-prev-buffers
2620 window (assq-delete-all old-buffer (window-prev-buffers window)))
2621 (set-window-next-buffers window (delq old-buffer next-buffers)))
2622 ;; Move `old-buffer' to head of WINDOW's restored list of next
2623 ;; buffers.
2624 (set-window-next-buffers
2625 window (cons old-buffer (delq old-buffer next-buffers)))))
2626
2627 ;; Remove killed buffers from WINDOW's previous and next buffers.
2628 (when killed-buffers
2629 (dolist (buffer killed-buffers)
2630 (set-window-prev-buffers
2631 window (assq-delete-all buffer (window-prev-buffers window)))
2632 (set-window-next-buffers
2633 window (delq buffer (window-next-buffers window)))))
2634
2635 ;; Return new-buffer.
2636 new-buffer))
2637
2638 (defun switch-to-next-buffer (&optional window)
2639 "In WINDOW switch to next buffer.
2640 WINDOW must be a live window and defaults to the selected one."
2641 (interactive)
2642 (let* ((window (normalize-live-window window))
2643 (old-buffer (window-buffer window))
2644 (next-buffers (window-next-buffers window))
2645 new-buffer entry killed-buffers visible)
2646 (when (window-dedicated-p window)
2647 (error "Window %s is dedicated to buffer %s" window old-buffer))
2648
2649 (catch 'found
2650 ;; Scan WINDOW's next buffers first.
2651 (dolist (buffer next-buffers)
2652 (when (and (or (buffer-live-p buffer)
2653 (not (setq killed-buffers
2654 (cons buffer killed-buffers))))
2655 (not (eq buffer old-buffer))
2656 (setq entry (assq buffer (window-prev-buffers window))))
2657 (setq new-buffer buffer)
2658 (set-window-buffer-start-and-point
2659 window new-buffer (nth 1 entry) (nth 2 entry))
2660 (throw 'found t)))
2661 ;; Scan the buffer list of WINDOW's frame next, skipping previous
2662 ;; buffers entries.
2663 (dolist (buffer (buffer-list (window-frame window)))
2664 (when (and (buffer-live-p buffer) (not (eq buffer old-buffer))
2665 (not (eq (aref (buffer-name buffer) 0) ?\s))
2666 (not (assq buffer (window-prev-buffers window))))
2667 (if (get-buffer-window buffer)
2668 ;; Try to avoid showing a buffer visible in some other window.
2669 (setq visible buffer)
2670 (setq new-buffer buffer)
2671 (set-window-buffer-start-and-point window new-buffer)
2672 (throw 'found t))))
2673 ;; Scan WINDOW's reverted previous buffers last (must not use
2674 ;; nreverse here!)
2675 (dolist (entry (reverse (window-prev-buffers window)))
2676 (when (and (setq new-buffer (car entry))
2677 (or (buffer-live-p new-buffer)
2678 (not (setq killed-buffers
2679 (cons new-buffer killed-buffers))))
2680 (not (eq new-buffer old-buffer)))
2681 (set-window-buffer-start-and-point
2682 window new-buffer (nth 1 entry) (nth 2 entry))
2683 (throw 'found t)))
2684
2685 ;; Show a buffer visible in another window.
2686 (when visible
2687 (setq new-buffer visible)
2688 (set-window-buffer-start-and-point window new-buffer)))
2689
2690 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
2691 (set-window-next-buffers window (delq new-buffer next-buffers))
2692
2693 ;; Remove killed buffers from WINDOW's previous and next buffers.
2694 (when killed-buffers
2695 (dolist (buffer killed-buffers)
2696 (set-window-prev-buffers
2697 window (assq-delete-all buffer (window-prev-buffers window)))
2698 (set-window-next-buffers
2699 window (delq buffer (window-next-buffers window)))))
2700
2701 ;; Return new-buffer.
2702 new-buffer))
2703
2704 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
2705 "Search LIST for a valid buffer to display in FRAME.
2706 Return nil when all buffers in LIST are undesirable for display,
2707 otherwise return the first suitable buffer in LIST.
2708
2709 Buffers not visible in windows are preferred to visible buffers,
2710 unless VISIBLE-OK is non-nil.
2711 If the optional argument FRAME is nil, it defaults to the selected frame.
2712 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
2713 ;; This logic is more or less copied from other-buffer.
2714 (setq frame (or frame (selected-frame)))
2715 (let ((pred (frame-parameter frame 'buffer-predicate))
2716 found buf)
2717 (while (and (not found) list)
2718 (setq buf (car list))
2719 (if (and (not (eq buffer buf))
2720 (buffer-live-p buf)
2721 (or (null pred) (funcall pred buf))
2722 (not (eq (aref (buffer-name buf) 0) ?\s))
2723 (or visible-ok (null (get-buffer-window buf 'visible))))
2724 (setq found buf)
2725 (setq list (cdr list))))
2726 (car list)))
2727
2728 (defun last-buffer (&optional buffer visible-ok frame)
2729 "Return the last buffer in FRAME's buffer list.
2730 If BUFFER is the last buffer, return the preceding buffer
2731 instead. Buffers not visible in windows are preferred to visible
2732 buffers, unless optional argument VISIBLE-OK is non-nil.
2733 Optional third argument FRAME nil or omitted means use the
2734 selected frame's buffer list. If no such buffer exists, return
2735 the buffer `*scratch*', creating it if necessary."
2736 (setq frame (or frame (selected-frame)))
2737 (or (get-next-valid-buffer (nreverse (buffer-list frame))
2738 buffer visible-ok frame)
2739 (get-buffer "*scratch*")
2740 (let ((scratch (get-buffer-create "*scratch*")))
2741 (set-buffer-major-mode scratch)
2742 scratch)))
2743
2744 (defun bury-buffer (&optional buffer-or-name)
2745 "Put BUFFER-OR-NAME at the end of the list of all buffers.
2746 There it is the least likely candidate for `other-buffer' to
2747 return; thus, the least likely buffer for \\[switch-to-buffer] to
2748 select by default.
2749
2750 You can specify a buffer name as BUFFER-OR-NAME, or an actual
2751 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
2752 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
2753 remove the current buffer from the selected window if it is
2754 displayed there."
2755 (interactive)
2756 (let* ((buffer (normalize-live-buffer buffer-or-name)))
2757 ;; If `buffer-or-name' is not on the selected frame we unrecord it
2758 ;; although it's not "here" (call it a feature).
2759 (unrecord-buffer buffer)
2760 ;; Handle case where `buffer-or-name' is nil and the current buffer
2761 ;; is shown in the selected window.
2762 (cond
2763 ((or buffer-or-name (not (eq buffer (window-buffer)))))
2764 ((not (window-dedicated-p))
2765 (switch-to-prev-buffer nil 'bury))
2766 ((frame-root-window-p (selected-window))
2767 (iconify-frame (window-frame (selected-window))))
2768 ((window-deletable-p)
2769 (delete-window)))
2770 ;; Always return nil.
2771 nil))
2772
2773 (defun unbury-buffer ()
2774 "Switch to the last buffer in the buffer list."
2775 (interactive)
2776 (switch-to-buffer (last-buffer)))
2777
2778 (defun next-buffer ()
2779 "In selected window switch to next buffer."
2780 (interactive)
2781 (switch-to-next-buffer))
2782
2783 (defun previous-buffer ()
2784 "In selected window switch to previous buffer."
2785 (interactive)
2786 (switch-to-prev-buffer))
2787
2788 (defun delete-windows-on (&optional buffer-or-name frame)
2789 "Delete all windows showing BUFFER-OR-NAME.
2790 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2791 and defaults to the current buffer.
2792
2793 The following non-nil values of the optional argument FRAME
2794 have special meanings:
2795
2796 - t means consider all windows on the selected frame only.
2797
2798 - `visible' means consider all windows on all visible frames on
2799 the current terminal.
2800
2801 - 0 (the number zero) means consider all windows on all visible
2802 and iconified frames on the current terminal.
2803
2804 - A frame means consider all windows on that frame only.
2805
2806 Any other value of FRAME means consider all windows on all
2807 frames.
2808
2809 When a window showing BUFFER-OR-NAME is dedicated and the only
2810 window of its frame, that frame is deleted when there are other
2811 frames left."
2812 (interactive "BDelete windows on (buffer):\nP")
2813 (let ((buffer (normalize-live-buffer buffer-or-name))
2814 ;; Handle the "inverted" meaning of the FRAME argument wrt other
2815 ;; `window-list-1' based function.
2816 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
2817 (dolist (window (window-list-1 nil nil all-frames))
2818 (if (eq (window-buffer window) buffer)
2819 (let ((deletable (window-deletable-p window)))
2820 (cond
2821 ((eq deletable 'frame)
2822 ;; Delete frame.
2823 (delete-frame (window-frame window)))
2824 (deletable
2825 ;; Delete window only.
2826 (delete-window window))
2827 (t
2828 ;; In window switch to previous buffer.
2829 (set-window-dedicated-p window nil)
2830 (switch-to-prev-buffer window 'bury))))
2831 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
2832 (unrecord-window-buffer window buffer)))))
2833
2834 (defun replace-buffer-in-windows (&optional buffer-or-name)
2835 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2836 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2837 and defaults to the current buffer.
2838
2839 When a window showing BUFFER-OR-NAME is either dedicated, or the
2840 window has no previous buffer, that window is deleted. If that
2841 window is the only window on its frame, the frame is deleted too
2842 when there are other frames left. If there are no other frames
2843 left, some other buffer is displayed in that window.
2844
2845 This function removes the buffer denoted by BUFFER-OR-NAME from
2846 all window-local buffer lists."
2847 (let ((buffer (normalize-live-buffer buffer-or-name)))
2848 (dolist (window (window-list-1 nil nil t))
2849 (if (eq (window-buffer window) buffer)
2850 (let ((deletable (window-deletable-p window)))
2851 (cond
2852 ((eq deletable 'frame)
2853 ;; Delete frame.
2854 (delete-frame (window-frame window)))
2855 ((and (window-dedicated-p window) deletable)
2856 ;; Delete window.
2857 (delete-window window))
2858 (t
2859 ;; Switch to another buffer in window.
2860 (set-window-dedicated-p window nil)
2861 (switch-to-prev-buffer window 'kill))))
2862 ;; Unrecord BUFFER in WINDOW.
2863 (unrecord-window-buffer window buffer)))))
2864
2865 (defun quit-restore-window (&optional window kill)
2866 "Quit WINDOW in some way.
2867 WINDOW must be a live window and defaults to the selected window.
2868 Return nil.
2869
2870 According to information stored in WINDOW's `quit-restore' window
2871 parameter either \(1) delete WINDOW and its frame, \(2) delete
2872 WINDOW, \(3) restore the buffer previously displayed in WINDOW,
2873 or \(4) make WINDOW display some other buffer than the present
2874 one. If non-nil, reset `quit-restore' parameter to nil.
2875
2876 Optional argument KILL non-nil means in addition kill WINDOW's
2877 buffer. If KILL is nil, put WINDOW's buffer at the end of the
2878 buffer list. Interactively, KILL is the prefix argument."
2879 (interactive "i\nP")
2880 (setq window (normalize-live-window window))
2881 (let ((buffer (window-buffer window))
2882 (quit-restore (window-parameter window 'quit-restore))
2883 deletable resize)
2884 (cond
2885 ((and (or (and (memq (car-safe quit-restore) '(new-window new-frame))
2886 ;; Check that WINDOW's buffer is still the same.
2887 (eq (window-buffer window) (nth 1 quit-restore)))
2888 (window-dedicated-p window))
2889 (setq deletable (window-deletable-p window)))
2890 ;; WINDOW can be deleted.
2891 (unrecord-buffer buffer)
2892 (if (eq deletable 'frame)
2893 ;; WINDOW's frame can be deleted.
2894 (delete-frame (window-frame window))
2895 ;; Just delete WINDOW.
2896 (delete-window window))
2897 ;; If the previously selected window is still alive, select it.
2898 (when (window-live-p (nth 2 quit-restore))
2899 (select-window (nth 2 quit-restore))))
2900 ((and (buffer-live-p (nth 0 quit-restore))
2901 ;; The buffer currently shown in WINDOW must still be the
2902 ;; buffer shown when its `quit-restore' parameter was created
2903 ;; in the first place.
2904 (eq (window-buffer window) (nth 3 quit-restore)))
2905 (setq resize (with-current-buffer buffer temp-buffer-resize-mode))
2906 ;; Unrecord buffer.
2907 (unrecord-buffer buffer)
2908 (unrecord-window-buffer window buffer)
2909 ;; Display buffer stored in the quit-restore parameter.
2910 (set-window-dedicated-p window nil)
2911 (set-window-buffer window (nth 0 quit-restore))
2912 (set-window-start window (nth 1 quit-restore))
2913 (set-window-point window (nth 2 quit-restore))
2914 (when (and resize (/= (nth 4 quit-restore) (window-total-size window)))
2915 (resize-window
2916 window (- (nth 4 quit-restore) (window-total-size window))))
2917 ;; Reset the quit-restore parameter.
2918 (set-window-parameter window 'quit-restore nil)
2919 (when (window-live-p (nth 5 quit-restore))
2920 (select-window (nth 5 quit-restore))))
2921 (t
2922 ;; Otherwise, show another buffer in WINDOW and reset the
2923 ;; quit-restore parameter.
2924 (set-window-parameter window 'quit-restore nil)
2925 (unrecord-buffer buffer)
2926 (switch-to-prev-buffer window 'bury-or-kill)))
2927
2928 ;; Kill WINDOW's old-buffer if requested
2929 (when kill (kill-buffer buffer))
2930 nil))
2931 \f
2932 ;;; Splitting windows.
2933 (defsubst window-split-min-size (&optional horizontal)
2934 "Return minimum height of any window when splitting windows.
2935 Optional argument HORIZONTAL non-nil means return minimum width."
2936 (if horizontal
2937 (max window-min-width window-safe-min-width)
2938 (max window-min-height window-safe-min-height)))
2939
2940 (defun split-window (&optional window size side)
2941 "Make a new window adjacent to WINDOW.
2942 WINDOW can be any window and defaults to the selected one.
2943 Return the new window which is always a live window.
2944
2945 Optional argument SIZE a positive number means make WINDOW SIZE
2946 lines or columns tall. If SIZE is negative, make the new window
2947 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
2948 absolute value can be less than `window-min-height' or
2949 `window-min-width'; so this command can make a new window as
2950 small as one line or two columns. SIZE defaults to half of
2951 WINDOW's size. Interactively, SIZE is the prefix argument.
2952
2953 Optional third argument SIDE nil (or `below') specifies that the
2954 new window shall be located below WINDOW. SIDE `above' means the
2955 new window shall be located above WINDOW. In both cases SIZE
2956 specifies the new number of lines for WINDOW \(or the new window
2957 if SIZE is negative) including space reserved for the mode and/or
2958 header line.
2959
2960 SIDE t (or `right') specifies that the new window shall be
2961 located on the right side of WINDOW. SIDE `left' means the new
2962 window shall be located on the left of WINDOW. In both cases
2963 SIZE specifies the new number of columns for WINDOW \(or the new
2964 window provided SIZE is negative) including space reserved for
2965 fringes and the scrollbar or a divider column. Any other non-nil
2966 value for SIDE is currently handled like t (or `right').
2967
2968 If the variable `ignore-window-parameters' is non-nil or the
2969 `split-window' parameter of WINDOW equals t, do not process any
2970 parameters of WINDOW. Otherwise, if the `split-window' parameter
2971 of WINDOW specifies a function, call that function with all three
2972 arguments and return the value returned by that function.
2973
2974 Otherwise, if WINDOW is part of an atomic window, \"split\" the
2975 root of that atomic window. The new window does not become a
2976 member of that atomic window.
2977
2978 If WINDOW is live, properties of the new window like margins and
2979 scrollbars are inherited from WINDOW. If WINDOW is an internal
2980 window, these properties as well as the buffer displayed in the
2981 new window are inherited from the window selected on WINDOW's
2982 frame. The selected window is not changed by this function."
2983 (interactive "i")
2984 (setq window (normalize-any-window window))
2985 (let* ((horizontal (not (memq side '(nil below above))))
2986 (frame (window-frame window))
2987 (parent (window-parent window))
2988 (function (window-parameter window 'split-window))
2989 (window-side (window-parameter window 'window-side))
2990 ;; Rebind `window-nest' since in some cases we may have to
2991 ;; override its value.
2992 (window-nest window-nest)
2993 atom-root)
2994
2995 (window-check frame)
2996 (catch 'done
2997 (cond
2998 ;; Ignore window parameters if either `ignore-window-parameters'
2999 ;; is t or the `split-window' parameter equals t.
3000 ((or ignore-window-parameters (eq function t)))
3001 ((functionp function)
3002 ;; The `split-window' parameter specifies the function to call.
3003 ;; If that function is `ignore', do nothing.
3004 (throw 'done (funcall function window size side)))
3005 ;; If WINDOW is a subwindow of an atomic window, split the root
3006 ;; window of that atomic window instead.
3007 ((and (window-parameter window 'window-atom)
3008 (setq atom-root (window-atom-root window))
3009 (not (eq atom-root window)))
3010 (throw 'done (split-window atom-root size side))))
3011
3012 (when (and window-side
3013 (or (not parent)
3014 (not (window-parameter parent 'window-side))))
3015 ;; WINDOW is a side root window. To make sure that a new parent
3016 ;; window gets created set `window-nest' to t.
3017 (setq window-nest t))
3018
3019 (when (and window-splits size (> size 0))
3020 ;; If `window-splits' is non-nil and SIZE is a non-negative
3021 ;; integer, we cannot reasonably resize other windows. Rather
3022 ;; bind `window-nest' to t to make sure that subsequent window
3023 ;; deletions are handled correctly.
3024 (setq window-nest t))
3025
3026 (let* ((parent-size
3027 ;; `parent-size' is the size of WINDOW's parent, provided
3028 ;; it has one.
3029 (when parent (window-total-size parent horizontal)))
3030 ;; `resize' non-nil means we are supposed to resize other
3031 ;; windows in WINDOW's combination.
3032 (resize
3033 (and window-splits (not window-nest)
3034 ;; Resize makes sense in iso-combinations only.
3035 (window-iso-combined-p window horizontal)))
3036 ;; `old-size' is the current size of WINDOW.
3037 (old-size (window-total-size window horizontal))
3038 ;; `new-size' is the specified or calculated size of the
3039 ;; new window.
3040 (new-size
3041 (cond
3042 ((not size)
3043 (max (window-split-min-size horizontal)
3044 (if resize
3045 ;; When resizing try to give the new window the
3046 ;; average size of a window in its combination.
3047 (min (- parent-size
3048 (window-min-size parent horizontal))
3049 (/ parent-size
3050 (1+ (window-iso-combinations
3051 parent horizontal))))
3052 ;; Else try to give the new window half the size
3053 ;; of WINDOW (plus an eventual odd line).
3054 (+ (/ old-size 2) (% old-size 2)))))
3055 ((>= size 0)
3056 ;; SIZE non-negative specifies the new size of WINDOW.
3057
3058 ;; Note: Specifying a non-negative SIZE is practically
3059 ;; always done as workaround for making the new window
3060 ;; appear above or on the left of the new window (the
3061 ;; ispell window is a typical example of that). In all
3062 ;; these cases the SIDE argument should be set to 'above
3063 ;; or 'left in order to support the 'resize option.
3064 ;; Here we have to nest the windows instead, see above.
3065 (- old-size size))
3066 (t
3067 ;; SIZE negative specifies the size of the new window.
3068 (- size))))
3069 new-parent new-normal)
3070
3071 ;; Check SIZE.
3072 (cond
3073 ((not size)
3074 (cond
3075 (resize
3076 ;; SIZE unspecified, resizing.
3077 (when (and (not (window-sizable-p parent (- new-size) horizontal))
3078 ;; Try again with minimum split size.
3079 (setq new-size
3080 (max new-size (window-split-min-size horizontal)))
3081 (not (window-sizable-p parent (- new-size) horizontal)))
3082 (error "Window %s too small for splitting" parent)))
3083 ((> (+ new-size (window-min-size window horizontal)) old-size)
3084 ;; SIZE unspecified, no resizing.
3085 (error "Window %s too small for splitting" window))))
3086 ((and (>= size 0)
3087 (or (>= size old-size)
3088 (< new-size (if horizontal
3089 window-safe-min-width
3090 window-safe-min-width))))
3091 ;; SIZE specified as new size of old window. If the new size
3092 ;; is larger than the old size or the size of the new window
3093 ;; would be less than the safe minimum, signal an error.
3094 (error "Window %s too small for splitting" window))
3095 (resize
3096 ;; SIZE specified, resizing.
3097 (unless (window-sizable-p parent (- new-size) horizontal)
3098 ;; If we cannot resize the parent give up.
3099 (error "Window %s too small for splitting" parent)))
3100 ((or (< new-size
3101 (if horizontal window-safe-min-width window-safe-min-height))
3102 (< (- old-size new-size)
3103 (if horizontal window-safe-min-width window-safe-min-height)))
3104 ;; SIZE specification violates minimum size restrictions.
3105 (error "Window %s too small for splitting" window)))
3106
3107 (resize-window-reset frame horizontal)
3108
3109 (setq new-parent
3110 ;; Make new-parent non-nil if we need a new parent window;
3111 ;; either because we want to nest or because WINDOW is not
3112 ;; iso-combined.
3113 (or window-nest (not (window-iso-combined-p window horizontal))))
3114 (setq new-normal
3115 ;; Make new-normal the normal size of the new window.
3116 (cond
3117 (size (/ (float new-size) (if new-parent old-size parent-size)))
3118 (new-parent 0.5)
3119 (resize (/ 1.0 (1+ (window-iso-combinations parent horizontal))))
3120 (t (/ (window-normal-size window horizontal) 2.0))))
3121
3122 (if resize
3123 ;; Try to get space from OLD's siblings. We could go "up" and
3124 ;; try getting additional space from surrounding windows but
3125 ;; we won't be able to return space to those windows when we
3126 ;; delete the one we create here. Hence we do not go up.
3127 (progn
3128 (resize-subwindows parent (- new-size) horizontal)
3129 (let* ((normal (- 1.0 new-normal))
3130 (sub (window-child parent)))
3131 (while sub
3132 (set-window-new-normal
3133 sub (* (window-normal-size sub horizontal) normal))
3134 (setq sub (window-right sub)))))
3135 ;; Get entire space from WINDOW.
3136 (set-window-new-total window (- old-size new-size))
3137 (resize-this-window window (- new-size) horizontal)
3138 (set-window-new-normal
3139 window (- (if new-parent 1.0 (window-normal-size window horizontal))
3140 new-normal)))
3141
3142 (let* ((new (split-window-internal window new-size side new-normal)))
3143 ;; Inherit window-side parameters, if any.
3144 (when (and window-side new-parent)
3145 (set-window-parameter (window-parent new) 'window-side window-side)
3146 (set-window-parameter new 'window-side window-side))
3147
3148 (run-window-configuration-change-hook frame)
3149 (window-check frame)
3150 ;; Always return the new window.
3151 new)))))
3152
3153 ;; I think this should be the default; I think people will prefer it--rms.
3154 (defcustom split-window-keep-point t
3155 "If non-nil, \\[split-window-above-each-other] keeps the original point \
3156 in both children.
3157 This is often more convenient for editing.
3158 If nil, adjust point in each of the two windows to minimize redisplay.
3159 This is convenient on slow terminals, but point can move strangely.
3160
3161 This option applies only to `split-window-above-each-other' and
3162 functions that call it. `split-window' always keeps the original
3163 point in both children."
3164 :type 'boolean
3165 :group 'windows)
3166
3167 (defun split-window-above-each-other (&optional size)
3168 "Split selected window into two windows, one above the other.
3169 The upper window gets SIZE lines and the lower one gets the rest.
3170 SIZE negative means the lower window gets -SIZE lines and the
3171 upper one the rest. With no argument, split windows equally or
3172 close to it. Both windows display the same buffer, now current.
3173
3174 If the variable `split-window-keep-point' is non-nil, both new
3175 windows will get the same value of point as the selected window.
3176 This is often more convenient for editing. The upper window is
3177 the selected window.
3178
3179 Otherwise, we choose window starts so as to minimize the amount of
3180 redisplay; this is convenient on slow terminals. The new selected
3181 window is the one that the current value of point appears in. The
3182 value of point can change if the text around point is hidden by the
3183 new mode line.
3184
3185 Regardless of the value of `split-window-keep-point', the upper
3186 window is the original one and the return value is the new, lower
3187 window."
3188 (interactive "P")
3189 (let ((old-window (selected-window))
3190 (old-point (point))
3191 (size (and size (prefix-numeric-value size)))
3192 moved-by-window-height moved new-window bottom)
3193 (when (and size (< size 0) (< (- size) window-min-height))
3194 ;; `split-window' would not signal an error here.
3195 (error "Size of new window too small"))
3196 (setq new-window (split-window nil size))
3197 (unless split-window-keep-point
3198 (with-current-buffer (window-buffer)
3199 (goto-char (window-start))
3200 (setq moved (vertical-motion (window-height)))
3201 (set-window-start new-window (point))
3202 (when (> (point) (window-point new-window))
3203 (set-window-point new-window (point)))
3204 (when (= moved (window-height))
3205 (setq moved-by-window-height t)
3206 (vertical-motion -1))
3207 (setq bottom (point)))
3208 (and moved-by-window-height
3209 (<= bottom (point))
3210 (set-window-point old-window (1- bottom)))
3211 (and moved-by-window-height
3212 (<= (window-start new-window) old-point)
3213 (set-window-point new-window old-point)
3214 (select-window new-window)))
3215 ;; Always copy quit-restore parameter in interactive use.
3216 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3217 (when quit-restore
3218 (set-window-parameter new-window 'quit-restore quit-restore)))
3219 new-window))
3220
3221 (defalias 'split-window-vertically 'split-window-above-each-other)
3222
3223 (defun split-window-side-by-side (&optional size)
3224 "Split selected window into two windows side by side.
3225 The selected window becomes the left one and gets SIZE columns.
3226 SIZE negative means the right window gets -SIZE lines.
3227
3228 SIZE includes the width of the window's scroll bar; if there are
3229 no scroll bars, it includes the width of the divider column to
3230 the window's right, if any. SIZE omitted or nil means split
3231 window equally.
3232
3233 The selected window remains selected. Return the new window."
3234 (interactive "P")
3235 (let ((old-window (selected-window))
3236 (size (and size (prefix-numeric-value size)))
3237 new-window)
3238 (when (and size (< size 0) (< (- size) window-min-width))
3239 ;; `split-window' would not signal an error here.
3240 (error "Size of new window too small"))
3241 (setq new-window (split-window nil size t))
3242 ;; Always copy quit-restore parameter in interactive use.
3243 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3244 (when quit-restore
3245 (set-window-parameter new-window 'quit-restore quit-restore)))
3246 new-window))
3247
3248 (defalias 'split-window-horizontally 'split-window-side-by-side)
3249 \f
3250 ;;; Balancing windows.
3251
3252 ;; The following routine uses the recycled code from an old version of
3253 ;; `resize-subwindows'. It's not very pretty, but coding it the way the
3254 ;; new `resize-subwindows' code does would hardly make it any shorter or
3255 ;; more readable (FWIW we'd need three loops - one to calculate the
3256 ;; minimum sizes per window, one to enlarge or shrink windows until the
3257 ;; new parent-size matches, and one where we shrink the largest/enlarge
3258 ;; the smallest window).
3259 (defun balance-windows-2 (window horizontal)
3260 "Subroutine of `balance-windows-1'.
3261 WINDOW must be an iso-combination."
3262 (let* ((first (window-child window))
3263 (sub first)
3264 (number-of-children 0)
3265 (parent-size (window-new-total window))
3266 (total-sum parent-size)
3267 found failed size sub-total sub-delta sub-amount rest)
3268 (while sub
3269 (setq number-of-children (1+ number-of-children))
3270 (when (window-size-fixed-p sub horizontal)
3271 (setq total-sum
3272 (- total-sum (window-total-size sub horizontal)))
3273 (set-window-new-normal sub 'ignore))
3274 (setq sub (window-right sub)))
3275
3276 (setq failed t)
3277 (while (and failed (> number-of-children 0))
3278 (setq size (/ total-sum number-of-children))
3279 (setq failed nil)
3280 (setq sub first)
3281 (while (and sub (not failed))
3282 ;; Ignore subwindows that should be ignored or are stuck.
3283 (unless (resize-subwindows-skip-p sub)
3284 (setq found t)
3285 (setq sub-total (window-total-size sub horizontal))
3286 (setq sub-delta (- size sub-total))
3287 (setq sub-amount
3288 (window-sizable sub sub-delta horizontal))
3289 ;; Register the new total size for this subwindow.
3290 (set-window-new-total sub (+ sub-total sub-amount))
3291 (unless (= sub-amount sub-delta)
3292 (setq total-sum (- total-sum sub-total sub-amount))
3293 (setq number-of-children (1- number-of-children))
3294 ;; We failed and need a new round.
3295 (setq failed t)
3296 (set-window-new-normal sub 'skip)))
3297 (setq sub (window-right sub))))
3298
3299 (setq rest (% total-sum number-of-children))
3300 ;; Fix rounding by trying to enlarge non-stuck windows by one line
3301 ;; (column) until `rest' is zero.
3302 (setq sub first)
3303 (while (and sub (> rest 0))
3304 (unless (resize-subwindows-skip-p window)
3305 (set-window-new-total sub 1 t)
3306 (setq rest (1- rest)))
3307 (setq sub (window-right sub)))
3308
3309 ;; Fix rounding by trying to enlarge stuck windows by one line
3310 ;; (column) until `rest' equals zero.
3311 (setq sub first)
3312 (while (and sub (> rest 0))
3313 (unless (eq (window-new-normal sub) 'ignore)
3314 (set-window-new-total sub 1 t)
3315 (setq rest (1- rest)))
3316 (setq sub (window-right sub)))
3317
3318 (setq sub first)
3319 (while sub
3320 ;; Record new normal sizes.
3321 (set-window-new-normal
3322 sub (/ (if (eq (window-new-normal sub) 'ignore)
3323 (window-total-size sub horizontal)
3324 (window-new-total sub))
3325 (float parent-size)))
3326 ;; Recursively balance each subwindow's subwindows.
3327 (balance-windows-1 sub horizontal)
3328 (setq sub (window-right sub)))))
3329
3330 (defun balance-windows-1 (window &optional horizontal)
3331 "Subroutine of `balance-windows'."
3332 (if (window-child window)
3333 (let ((sub (window-child window)))
3334 (if (window-iso-combined-p sub horizontal)
3335 (balance-windows-2 window horizontal)
3336 (let ((size (window-new-total window)))
3337 (while sub
3338 (set-window-new-total sub size)
3339 (balance-windows-1 sub horizontal)
3340 (setq sub (window-right sub))))))))
3341
3342 (defun balance-windows (&optional window-or-frame)
3343 "Balance the sizes of subwindows of WINDOW-OR-FRAME.
3344 WINDOW-OR-FRAME is optional and defaults to the selected frame.
3345 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
3346 subwindows of that frame's root window. If WINDOW-OR-FRAME
3347 denots a window, balance the sizes of all subwindows of that
3348 window."
3349 (interactive)
3350 (let* ((window
3351 (cond
3352 ((or (not window-or-frame)
3353 (frame-live-p window-or-frame))
3354 (frame-root-window window-or-frame))
3355 ((or (window-live-p window-or-frame)
3356 (window-child window-or-frame))
3357 window-or-frame)
3358 (t
3359 (error "Not a window or frame %s" window-or-frame))))
3360 (frame (window-frame window)))
3361 ;; Balance vertically.
3362 (resize-window-reset (window-frame window))
3363 (balance-windows-1 window)
3364 (resize-window-apply frame)
3365 ;; Balance horizontally.
3366 (resize-window-reset (window-frame window) t)
3367 (balance-windows-1 window t)
3368 (resize-window-apply frame t)))
3369
3370 (defun window-fixed-size-p (&optional window direction)
3371 "Return t if WINDOW cannot be resized in DIRECTION.
3372 WINDOW defaults to the selected window. DIRECTION can be
3373 nil (i.e. any), `height' or `width'."
3374 (with-current-buffer (window-buffer window)
3375 (when (and (boundp 'window-size-fixed) window-size-fixed)
3376 (not (and direction
3377 (member (cons direction window-size-fixed)
3378 '((height . width) (width . height))))))))
3379
3380 ;;; A different solution to balance-windows.
3381 (defvar window-area-factor 1
3382 "Factor by which the window area should be over-estimated.
3383 This is used by `balance-windows-area'.
3384 Changing this globally has no effect.")
3385 (make-variable-buffer-local 'window-area-factor)
3386
3387 (defun balance-windows-area-adjust (window delta horizontal)
3388 "Wrapper around `resize-window' with error checking.
3389 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
3390 ;; `resize-window' may fail if delta is too large.
3391 (while (>= (abs delta) 1)
3392 (condition-case nil
3393 (progn
3394 (resize-window window delta horizontal)
3395 (setq delta 0))
3396 (error
3397 ;;(message "adjust: %s" (error-message-string err))
3398 (setq delta (/ delta 2))))))
3399
3400 (defun balance-windows-area ()
3401 "Make all visible windows the same area (approximately).
3402 See also `window-area-factor' to change the relative size of
3403 specific buffers."
3404 (interactive)
3405 (let* ((unchanged 0) (carry 0) (round 0)
3406 ;; Remove fixed-size windows.
3407 (wins (delq nil (mapcar (lambda (win)
3408 (if (not (window-fixed-size-p win)) win))
3409 (window-list nil 'nomini))))
3410 (changelog nil)
3411 next)
3412 ;; Resizing a window changes the size of surrounding windows in complex
3413 ;; ways, so it's difficult to balance them all. The introduction of
3414 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
3415 ;; very difficult to do. `balance-window' above takes an off-line
3416 ;; approach: get the whole window tree, then balance it, then try to
3417 ;; adjust the windows so they fit the result.
3418 ;; Here, instead, we take a "local optimization" approach, where we just
3419 ;; go through all the windows several times until nothing needs to be
3420 ;; changed. The main problem with this approach is that it's difficult
3421 ;; to make sure it terminates, so we use some heuristic to try and break
3422 ;; off infinite loops.
3423 ;; After a round without any change, we allow a second, to give a chance
3424 ;; to the carry to propagate a minor imbalance from the end back to
3425 ;; the beginning.
3426 (while (< unchanged 2)
3427 ;; (message "New round")
3428 (setq unchanged (1+ unchanged) round (1+ round))
3429 (dolist (win wins)
3430 (setq next win)
3431 (while (progn (setq next (next-window next))
3432 (window-fixed-size-p next)))
3433 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
3434 (let* ((horiz
3435 (< (car (window-edges win)) (car (window-edges next))))
3436 (areadiff (/ (- (* (window-height next) (window-width next)
3437 (buffer-local-value 'window-area-factor
3438 (window-buffer next)))
3439 (* (window-height win) (window-width win)
3440 (buffer-local-value 'window-area-factor
3441 (window-buffer win))))
3442 (max (buffer-local-value 'window-area-factor
3443 (window-buffer win))
3444 (buffer-local-value 'window-area-factor
3445 (window-buffer next)))))
3446 (edgesize (if horiz
3447 (+ (window-height win) (window-height next))
3448 (+ (window-width win) (window-width next))))
3449 (diff (/ areadiff edgesize)))
3450 (when (zerop diff)
3451 ;; Maybe diff is actually closer to 1 than to 0.
3452 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
3453 (when (and (zerop diff) (not (zerop areadiff)))
3454 (setq diff (/ (+ areadiff carry) edgesize))
3455 ;; Change things smoothly.
3456 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
3457 (if (zerop diff)
3458 ;; Make sure negligible differences don't accumulate to
3459 ;; become significant.
3460 (setq carry (+ carry areadiff))
3461 ;; This used `adjust-window-trailing-edge' before and uses
3462 ;; `resize-window' now. Error wrapping is still needed.
3463 (balance-windows-area-adjust win diff horiz)
3464 ;; (sit-for 0.5)
3465 (let ((change (cons win (window-edges win))))
3466 ;; If the same change has been seen already for this window,
3467 ;; we're most likely in an endless loop, so don't count it as
3468 ;; a change.
3469 (unless (member change changelog)
3470 (push change changelog)
3471 (setq unchanged 0 carry 0)))))))
3472 ;; We've now basically balanced all the windows.
3473 ;; But there may be some minor off-by-one imbalance left over,
3474 ;; so let's do some fine tuning.
3475 ;; (bw-finetune wins)
3476 ;; (message "Done in %d rounds" round)
3477 ))
3478 \f
3479 ;;; Displaying buffers.
3480 (defconst display-buffer-default-specifiers
3481 '((reuse-window nil same visible)
3482 (pop-up-window (largest . nil) (lru . nil))
3483 (pop-up-frame)
3484 (pop-up-frame-alist
3485 (height . 24) (width . 80) (unsplittable . t))
3486 (reuse-window nil other visible)
3487 (reuse-window-even-sizes . t))
3488 "Buffer display default specifiers.
3489 The value specified here is used when no other specifiers have
3490 been specified by the user or the application. Consult the
3491 documentation of `display-buffer-alist' for a description of
3492 buffer display specifiers.")
3493
3494 (defconst display-buffer-macro-specifiers
3495 '((same-window
3496 ;; Use the same window.
3497 (reuse-window same nil nil))
3498 (same-frame
3499 ;; Avoid other frames.
3500 (reuse-window nil same nil)
3501 (pop-up-window (largest . nil) (lru . nil))
3502 (reuse-window nil other nil))
3503 ;; (other-window
3504 ;; ;; Avoid selected window.
3505 ;; (reuse-window other same visible)
3506 ;; (pop-up-window (largest . nil) (lru . nil))
3507 ;; (reuse-window other other visible))
3508 (same-frame-other-window
3509 ;; Avoid other frames and selected window.
3510 (reuse-window other same nil)
3511 (pop-up-window (largest . nil) (lru . nil))
3512 (reuse-window other other nil))
3513 (other-frame
3514 ;; Avoid selected frame.
3515 (reuse-window nil same other)
3516 (pop-up-frame)
3517 (reuse-window nil other other))
3518 (default
3519 ;; The default specifiers.
3520 display-buffer-default-specifiers))
3521 "Buffer display macro specifiers.")
3522
3523 (defcustom display-buffer-alist
3524 '((((regexp . ".*"))
3525 ;; Reuse window showing same buffer.
3526 reuse-window (reuse-window nil same visible)
3527 ;; Pop up window.
3528 pop-up-window
3529 ;; Split largest or lru window.
3530 (pop-up-window (largest . nil) (lru . nil))
3531 (pop-up-window-min-height . 40) ; split-height-threshold / 2
3532 (pop-up-window-min-width . 80) ; split-width-threshold / 2
3533 ;; Reuse any but selected window on same frame.
3534 reuse-window (reuse-window other nil nil)
3535 (reuse-window-even-sizes . t)))
3536 "List associating buffer identifiers with display specifiers.
3537 The car of each element of this list is built from a set of cons
3538 cells called buffer identifiers. `display-buffer' shows a buffer
3539 according to the display specifiers in the element's cdr
3540 \(elements are true lists) if at least one of the identifiers
3541 matches the first or third argument of `display-buffer'. Such a
3542 match occurs in one of the following three cases:
3543
3544 - The car of the buffer identifier is the symbol `name' and its
3545 cdr is a string equalling the name of the buffer specified by
3546 the first \(BUFFER-OR-NAME) argument of `display-buffer'.
3547
3548 - The car is the symbol `regexp' and the cdr is a regular
3549 expression matching the name of the buffer specified by the
3550 first \(BUFFER-OR-NAME) argument of `display-buffer'.
3551
3552 - The car is the symbol `label' and the cdr is a symbol equalling
3553 the third \(LABEL) argument of `display-buffer'.
3554
3555 Display specifiers are either symbols, cons cells, or lists.
3556 Five specifiers have been reserved to indicate the basic method
3557 for displaying the buffer: `reuse-window', `pop-up-window',
3558 `pop-up-frame', `use-side-window', and `fun-with-args'.
3559
3560 A list whose car is the symbol `reuse-window' indicates that an
3561 existing window shall be reused for displaying the buffer. The
3562 second element of this list specifies the window to use and can
3563 be one of the following symbols:
3564
3565 nil stands for any window.
3566
3567 `same' stands for the selected window.
3568
3569 `other' stands for any but the selected window.
3570
3571 The third element specifies whether the buffer shown in a window
3572 that shall be reused must be the same buffer that shall be
3573 displayed or another buffer and can be one of the following:
3574
3575 nil means to not care about the window's buffer.
3576
3577 `same' means the window must show the buffer already.
3578
3579 `other' means the window must not show the buffer yet.
3580
3581 The fourth element specifies the set of frames to search for a
3582 suitable window and can be one of the following:
3583
3584 nil to reuse a window on the selected frame.
3585
3586 `visible' to search visible frames on the current terminal.
3587
3588 `other' stands for any visible frame but the selected one.
3589
3590 0 \(the number zero) to search visible and iconified frames on
3591 the current terminal.
3592
3593 t to search arbitrary frames including invisible ones.
3594
3595 If more than one window fits the constraints imposed by these
3596 elements, the least recently used candidate is chosen. A side
3597 window is never reused unless it already shows the buffer.
3598
3599 The following two specifiers are useful when the method equals
3600 `reuse-window':
3601
3602 - A cons cell whose car is the symbol `reuse-window-even-sizes'
3603 and whose cdr is non-nil means to even out the sizes of a
3604 reused window and the selected window provided they (1) appear
3605 adjacent to each other and (2) the selected window is larger
3606 than the window chosen. If the cdr is nil, this means that the
3607 window sizes are left alone.
3608
3609 - A cons cell whose car is the symbol `reuse-window-dedicated'
3610 and whose cdr is non-nil means that a window can be reused even
3611 if it's weakly dedicated to its buffer. If the cdr is t, a
3612 strongly dedicated window can be reused to show the buffer.
3613 Any other non-nil value means only weakly dedicated windows can
3614 be reused. If the cdr is nil, dedicated windows are not
3615 reused.
3616
3617 This specifier should be used in emergency cases only since
3618 windows are usually made dedicated in order to prevent
3619 `display-buffer' from reusing them.
3620
3621 A list whose car is the symbol `pop-up-window' and whose cdr is
3622 built from cons cells representing window/side tuples indicates
3623 that a new window shall be made for displaying the buffer on the
3624 selected frame.
3625
3626 Window/side tuples are cons cells. The car of such a tuple
3627 identifies the window that shall be split. Possible values are
3628 `largest', `lru', `selected', and `root' to split the largest,
3629 least recently used, selected or root window of the selected
3630 frame.
3631
3632 If the frame has side windows, these values do allow to split
3633 only the selected frame's main window or one of its subwindows.
3634 Setting the car to one of `left', `top', `right' and `bottom'
3635 splits the corresponding side window, provided such a window
3636 exists.
3637
3638 The cdr of each pair specifies on which side of the window to
3639 split the new window shall appear and can be one of `below',
3640 `right', `above', or `left' with the obvious meanings. If the
3641 cdr is nil, the window is split in a fashion suitable for its
3642 current dimensions. If the cdr specifies a function, that
3643 function is called with one argument - the window to split. The
3644 function is supposed to split that window and return the new
3645 window.
3646
3647 `display-buffer' scans these tuples until it can either produce a
3648 suitable window or fails. The default value for
3649 `display-buffer-alist' contains the tuples \(largest . nil) and
3650 \(lru . nil) in order to split the largest window first and, if
3651 that fails, the least recently used one.
3652
3653 The following specifiers are useful if the method specifier is
3654 `pop-up-window'.
3655
3656 - A cons cell whose car is the symbol `pop-up-window-min-height'
3657 specifiies the minimum height of the new window. If the cdr is
3658 an integer number, it specifies the minimum number of lines of
3659 the window. A floating point number gives the minimum fraction
3660 of the window height with respect to the height of the frame's
3661 root window. A new window is created only if it can be made at
3662 least as high as specified by the number. If the cdr is nil,
3663 this means to use the value of `window-min-height'.
3664
3665 - A cons cell whose car is the symbol `pop-up-window-min-width'
3666 specifies the minimum width of the new window. If the cdr is
3667 an integer number, it specifies the minimum number of columns
3668 of the window. A floating point number gives the minimum
3669 fraction of the window width with respect to the width of the
3670 frame's root window. A new window is created only if it can be
3671 made at least as wide as specified by the number. If the cdr
3672 is nil, this means to use the value of `window-min-width'.
3673
3674 - A cons cell whose car is `pop-up-window-set-height' with
3675 the following interpretations for the cdr:
3676
3677 - nil means leave the height of the new window alone.
3678
3679 - A number specifies the desired height of the new window. An
3680 integer number specifies the number of lines of the window.
3681 A floating point number gives the fraction of the window
3682 height with respect to the height of the frame's root window.
3683
3684 - If the cdr specifies a function, that function is called with
3685 one argument - the new window. The function is supposed to
3686 adjust the height of the window; its return value is ignored.
3687 Suitable functions are `shrink-window-if-larger-than-buffer'
3688 and `fit-window-to-buffer'.
3689
3690 - A cons cell whose car equals `pop-up-window-set-width' with
3691 the following interpretations for the cdr:
3692
3693 - nil means leave the width of the new window alone.
3694
3695 - A number specifies the desired width of the new window. An
3696 integer number specifies the number of columns of the window.
3697 A floating point number gives the fraction of the window
3698 width with respect to the width of the frame's root window.
3699
3700 - If the cdr specifies a function, that function is called with
3701 one argument - the new window. The function is supposed to
3702 adjust the width of the window; its return value is ignored.
3703
3704 Observe that specifying `pop-up-window-set-height' or
3705 `pop-up-window-set-width' may override restrictions given by
3706 the `pop-up-window-min-height' and `pop-up-window-min-width'
3707 specifiers.
3708
3709 - A cons cell whose car is `pop-up-window-split-unsplittable' and
3710 whose cdr is non-nil allows to make a new window on an
3711 unsplittable frame. If the cdr is nil, unsplittable frames are
3712 not split. This specifier should be used in special cases only
3713 since frames are usually made unsplittable in order to prevent
3714 `display-buffer' from splitting them.
3715
3716 A list whose car is the symbol `pop-up-frame' specifies that a
3717 new frame shall be made for displaying the buffer. The second
3718 element, if non-nil, allows popping up a new frame on graphic
3719 displays only.
3720
3721 The following specifiers are useful if the method specifier is
3722 `pop-up-frame'.
3723
3724 - A list whose car is the symbol `pop-up-frame-function' together
3725 with a valid function as cdr specifies the function for
3726 creating a new frame. If the cdr is nil, the default function
3727 `make-frame' is called. The function is called with the
3728 parameters and values provided by the specifier described next.
3729
3730 - A list whose car is the symbol `pop-up-frame-alist' followed by
3731 an arbitrary number of frame parameter/value tuples, each given
3732 as a cons cell, specifies the parameters passed to the pop-up
3733 frame function.
3734
3735 A list of three elements whose car is the symbol
3736 `use-side-window' specifies that the buffer shall be displayed in
3737 a side window of the selected frame. The second element denotes
3738 the side of the frame where the window appears or shall be made.
3739 The third element denotes the slot used by the window. If a side
3740 window with the specified slot exists already, that window is
3741 reused. If no such window exists it is created.
3742
3743 The following specifiers are useful in connection with the
3744 `use-side-window' method specifier: `reuse-window-dedicated',
3745 `pop-up-window-min-height', `pop-up-window-min-width',
3746 `pop-up-window-set-height' and `pop-up-window-set-width'.
3747
3748 A list whose car is the symbol `fun-with-args' specifies that the
3749 function specified in the second element of the list is
3750 responsible for displaying the buffer. `display-buffer' calls
3751 this function with the buffer as first argument and the remaining
3752 elements of the list as second argument.
3753
3754 The function should choose or create a window, display the buffer
3755 in it, and return the window. It is also responsible for giving
3756 the variable `display-buffer-window' and the `quit-restore'
3757 parameter of the window used a meaningful value.
3758
3759 Within the body of this function avoid calling `display-buffer'
3760 with the same buffer as argument since this may lead to endless
3761 recursion.
3762
3763 Instead of supplying basic method specifiers, it's sometimes more
3764 convenient to use macro specifiers. They provide some commonly
3765 used display methods but do not support the fine control provided
3766 by the basic method specifiers. Macro specifiers are symbols.
3767 The following macro specifiers are provided:
3768
3769 `same-window' to display the buffer in the selected window.
3770
3771 `same-frame' to display the buffer on the selected frame.
3772
3773 `other-window' to display the buffer in any window but the
3774 selected one.
3775
3776 `same-frame-other-window' as `other-window' but stay on the
3777 selected frame.
3778
3779 `other-frame' to display the buffer on another visible
3780 frame.
3781
3782 `default' to use the default value of `display-buffer-alist'.
3783
3784 One specifier is useful with any method specifier: A list whose
3785 car is the symbol `dedicate' and whose cdr is non-nil will
3786 dedicate the window to its buffer. The following values are
3787 supported:
3788
3789 - nil to not dedicate the window to the buffer.
3790
3791 - `weak' to weakly dedicate the window to the buffer.
3792
3793 - t to strongly dedicate the window to the buffer.
3794
3795 A cons cell whose car is `other-window-means-other-frame' and
3796 whose cdr is non-nil means that you want calls of
3797 `display-buffer' with the second argument t or the symbol
3798 `other-window' to display the buffer in another frame. This
3799 means, for example, that you prefer functions like
3800 `find-file-other-window' or `switch-to-buffer-other-window' to
3801 make a new frame instead of a new window on the selected frame.
3802
3803 Usually, applications are free to override the specifiers of
3804 `display-buffer-alist' by passing their own specifiers as second
3805 argument of `display-buffer'. For every `display-buffer-alist'
3806 entry you can, however, add a cons cell whose car is the symbol
3807 `override' and whose cdr is non-nil, to explicitly override any
3808 value supplied by the application.
3809
3810 Overriding specifiers supplied by the calling application is, in
3811 general, not advisable. It permits, for example, to change the
3812 semantics of a function like `display-buffer-other-window' by
3813 using the location specifiers `same-window' or `other-frame'."
3814 :risky t
3815 :type
3816 '(repeat
3817 :offset 9
3818 ;; Associations of buffer identifiers and display specifiers.
3819 (list
3820 :format "%v"
3821 ;; Buffer identifiers.
3822 (repeat
3823 :tag "Buffer identifiers"
3824 (choice
3825 :tag "Identifier"
3826 :format "%[%t%] %v" :size 15
3827 (cons
3828 :tag "Name"
3829 :format "%v"
3830 :help-echo "A buffer name."
3831 (const :format "" name)
3832 (string :format "Name: %v\n" :size 32))
3833 (cons
3834 :tag "Regexp"
3835 :format "%v"
3836 :help-echo "A regular expression matching buffer names."
3837 (const :format "" regexp)
3838 (string :format "Regexp: %v\n" :size 32))
3839 (cons
3840 :tag "Label"
3841 :format "%v"
3842 :help-echo "A symbol equalling the buffer display label."
3843 (const :format "" symbol)
3844 (symbol :format "Label: %v\n" :size 32))))
3845
3846 ;; Display specifiers.
3847 (repeat
3848 :offset 9
3849 :tag "Display specifiers"
3850 :inline t
3851 (list
3852 :inline t
3853 :format "%v"
3854 (choice
3855 :tag "Method"
3856 :value (reuse-window
3857 (reuse-window nil same nil)
3858 (reuse-window-even-sizes . t))
3859 :inline t
3860 :help-echo "Method for displaying the buffer."
3861 :format "%[Method%] %v" :size 15
3862
3863 ;; Reuse window specifiers.
3864 (list
3865 :tag "Reuse window"
3866 :value (reuse-window
3867 (reuse-window nil same nil)
3868 (reuse-window-even-sizes . t))
3869 :format "%t\n%v"
3870 :inline t
3871 ;; For customization purposes only.
3872 (const :format "" reuse-window)
3873 (set
3874 :format "%v"
3875 :inline t
3876 ;; The window to reuse.
3877 (list
3878 :format "%v\n"
3879 (const :format "" reuse-window)
3880 ;; The window type.
3881 (choice
3882 :tag "Window"
3883 :help-echo "Window to reuse."
3884 :value nil
3885 :format "%[Window%] %v" :size 15
3886 (const :tag "Any" :format "%t" nil)
3887 (const :tag "Selected only" :format "%t" same)
3888 (const :tag "Any but selected" :format "%t" other))
3889 ;; The window's buffer.
3890 (choice
3891 :tag "Buffer"
3892 :help-echo "Buffer shown by reused window."
3893 :value t
3894 :format " %[Buffer%] %v" :size 15
3895 (const :tag "Any buffer" :format "%t" nil)
3896 (const :tag "Same buffer" :format "%t" same)
3897 (const :tag "Other buffer" :format "%t" other))
3898 ;; The window's frame.
3899 (choice
3900 :help-echo "Frame to search for a window to reuse."
3901 :tag "Frame"
3902 :value nil
3903 :format " %[Frame%] %v" :size 15
3904 (const :tag "Selected frame only" :format "%t" nil)
3905 (const :tag "Visible frames" :format "%t" visible)
3906 (const :tag "Visible but unselected" :format "%t" other)
3907 (const :tag "Visible and iconified" :format "%t" 0)
3908 (const :tag "Any frame" :format "%t" t)))
3909 ;; Whether window sizes should be evened out.
3910 (cons
3911 :format "%v\n"
3912 :tag "Even window sizes"
3913 (const :format "" reuse-window-even-sizes)
3914 (choice
3915 :tag "Even window sizes"
3916 :help-echo "Whether to even sizes of selected and reused window."
3917 :value t
3918 :format "%[Even window sizes%] %v" :size 15
3919 (const :tag "Off" :format "%t" nil)
3920 (const :tag "Even window sizes" :format "%t" t)))
3921 ;; Whether to reuse a dedicated window
3922 (cons
3923 :format "%v\n"
3924 (const :format "" reuse-window-dedicated)
3925 (choice
3926 :tag "Reuse dedicated window" :value nil
3927 :help-echo "Reuse a window even if it is dedicated to its buffer."
3928 :format "%[Reuse dedicated window%] %v" :size 15
3929 (const :tag "Off" :format "%t" nil)
3930 (const :tag "Reuse weakly dedicated windows" :format "%t" weak)
3931 (const :tag "Reuse any dedicated window" :format "%t" t)))))
3932
3933 ;; Pop-up window specifiers.
3934 (list
3935 :tag "Pop-up window"
3936 :value (pop-up-window (pop-up-window (largest . nil) (lru . nil)))
3937 :format "%t\n%v"
3938 :inline t
3939 (const :format "" pop-up-window)
3940 (set
3941 :format "%v"
3942 :inline t
3943 ;; Pop-up window list.
3944 (list
3945 :format "%v"
3946 :value (pop-up-window (largest . nil) (lru . nil))
3947 (const :format "" pop-up-window)
3948 (repeat
3949 :tag "Window / Side tuples"
3950 :inline t
3951 (cons
3952 :format "%v\n"
3953 (choice
3954 :tag "Window"
3955 :help-echo "The window to split."
3956 :value largest
3957 :format "%[Window%] %v"
3958 (const :tag "Largest" :format "%t" largest)
3959 (const :tag "Least recently used" :format "%t" lru)
3960 (const :tag "Selected" :format "%t" selected)
3961 (const :tag "Root" :format "%t" root)
3962 (const :tag "Left" :format "%t" left)
3963 (const :tag "Top" :format "%t" top)
3964 (const :tag "Right" :format "%t" right)
3965 (const :tag "Bottom" :format "%t" bottom))
3966 (choice
3967 :tag "Side"
3968 :help-echo "The position of the new window with respect to the window to split."
3969 :value nil
3970 :format " %[Side%] %v"
3971 (const :tag "Dynamic" :format "%t" nil)
3972 (const :tag "Below" :format "%t" below)
3973 (const :tag "Right" :format "%t" right)
3974 (const :tag "Above" :format "%t" above)
3975 (const :tag "Left" :format "%t" left)
3976 (function
3977 :tag "Function" :format "%v" :size 25)))))
3978 ;; Minimum height of pop-up windows.
3979 (cons
3980 :format "%v\n"
3981 (const :format "" pop-up-window-min-height)
3982 (choice
3983 :help-echo "Minimum height of popped-up window."
3984 :format "%[Minimum height%] %v"
3985 (const :tag "Default" :format "%t" :value nil)
3986 (integer :tag "Number of lines" :value 12 :size 5)
3987 (float :tag "Fraction of frame height" :value .25 :size 5)))
3988 ;; Minimum width of pop-up windows.
3989 (cons
3990 :format "%v\n"
3991 (const :format "" pop-up-window-min-width)
3992 (choice
3993 :help-echo "Minimum width of popped-up window."
3994 :format "%[Minimum width%] %v"
3995 (const :tag "Default" :format "%t" :value nil)
3996 (integer :tag "Number of columns" :value 12 :size 5)
3997 (float :tag "Fraction of frame width" :value .25 :size 5)))
3998 ;; Desired height of pop-up windows.
3999 (cons
4000 :format "%v\n"
4001 (const :format "" pop-up-window-set-height)
4002 (choice
4003 :help-echo "Desired height of popped-up window."
4004 :format "%[Desired height%] %v"
4005 (const :tag "Default" :format "%t" :value nil)
4006 (integer :tag "Number of lines" :value 12 :size 5)
4007 (float :tag "Fraction of frame height" :value .25 :size 5)
4008 (function :tag "Function" :size 25)))
4009 ;; Desired width of pop-up windows.
4010 (cons
4011 :format "%v\n"
4012 (const :format "" pop-up-window-set-width)
4013 (choice
4014 :help-echo "Desired width of popped-up window."
4015 :format "%[Desired width%] %v"
4016 (const :tag "Default" :format "%t" :value nil)
4017 (integer :tag "Number of column" :value 12 :size 5)
4018 (float :tag "Fraction of frame width" :value .25 :size 5)
4019 (function :tag "Function" :size 25)))
4020 ;; Split unsplittable frames.
4021 (cons
4022 :format "%v\n"
4023 (const :format "" pop-up-window-unsplittable)
4024 (choice
4025 :help-echo "Allow popping up a window on \"unsplittable\" frames."
4026 :format "%[Split unsplittable frame%] %v"
4027 (const :tag "Off" :format "%t" nil)
4028 (const :tag "Allow" :format "%t" t)))))
4029
4030 ;; Pop-up frame specifiers.
4031 (list
4032 :tag "Pop-up frame"
4033 :value (pop-up-frame
4034 (pop-up-frame)
4035 (pop-up-frame-alist
4036 (height . 24) (width . 80) (unsplittable . t)))
4037 :format "%t\n%v"
4038 :inline t
4039 (const :format "" pop-up-frame)
4040 (set
4041 :format "%v"
4042 :inline t
4043 ;; Pop-up frame.
4044 (list
4045 :tag "Pop-up a new frame"
4046 :value (pop-up-frame)
4047 :format "%v"
4048 (const :format "" pop-up-frame)
4049 (choice
4050 :tag "Pop-up a new frame"
4051 :help-echo "Whether to pop-up a new frame on a display."
4052 :format "%[Display%] %v\n" :size 15
4053 (const :tag "On any display" :format "%t" nil)
4054 (const :tag "On graphic displays only" :format "%t" t)))
4055 ;; Pop-up frame function.
4056 (cons
4057 :format "%v\n"
4058 (const :format "" pop-up-frame-function)
4059 (choice
4060 :tag "Pop-up frame function"
4061 :value nil
4062 :help-echo "Function to use to pop-up a new frame."
4063 :format "%[Function%] %v" :size 15
4064 (const :tag "Default" :format "%t" nil)
4065 (function
4066 :value make-frame
4067 :format "%t: %v"
4068 :size 25)))
4069 ;; Pop-up frame alist.
4070 (list
4071 :format "%v"
4072 (const :format "" pop-up-frame-alist)
4073 (repeat
4074 :tag "Parameter / Value tuples"
4075 :inline t
4076 (cons
4077 :format "%v\n"
4078 (symbol
4079 :tag "Parameter"
4080 :format "Parameter: %v"
4081 :size 16)
4082 (sexp
4083 :tag "Value"
4084 :format " Value: %v"
4085 :size 8))))))
4086
4087 ;; Use side-window specifiers.
4088 (list
4089 :tag "Use side-window"
4090 :value (use-side-window (use-side-window bottom 0))
4091 :format "%t\n%v"
4092 :inline t
4093 ;; For customization purposes only.
4094 (const :format "" use-side-window)
4095 (set
4096 :format "%v"
4097 :inline t
4098 ;; Side and slot.
4099 (list
4100 :format "%v\n"
4101 :value (use-side-window bottom 0)
4102 (const :format "" use-side-window)
4103 ;; The side.
4104 (choice
4105 :tag "Side"
4106 :help-echo "Side of frame."
4107 :value bottom
4108 :format "%[Side%] %v" :size 15
4109 (const :tag "Left" :format "%t" left)
4110 (const :tag "Top" :format "%t" top)
4111 (const :tag "Right" :format "%t" right)
4112 (const :tag "Bottom" :format "%t" bottom))
4113 ;; The slot
4114 (number
4115 :tag "Slot"
4116 :help-echo "The slot (an arbitrary number, where 0 stands for the center slot)."
4117 :value 0
4118 :format " Slot: %v" :size 8))
4119 ;; Whether to reuse a dedicated side window
4120 (cons
4121 :format "%v\n"
4122 (const :format "" reuse-window-dedicated)
4123 (choice
4124 :tag "Reuse dedicated side window" :value nil
4125 :help-echo "Reuse a side window even if it is dedicated to its buffer."
4126 :format "%[Reuse dedicated side window%] %v" :size 15
4127 (const :tag "Off" :format "%t" nil)
4128 (const :tag "Reuse weakly dedicated side windows" :format "%t" weak)
4129 (const :tag "Reuse any dedicated side window" :format "%t" t)))
4130 ;; Minimum height of pop-up side windows.
4131 (cons
4132 :format "%v\n"
4133 (const :format "" pop-up-window-min-height)
4134 (choice
4135 :help-echo "Minimum height of popped-up side window."
4136 :format "%[Minimum height%] %v"
4137 (const :tag "Default" :format "%t" :value nil)
4138 (integer :tag "Number of lines" :value 12 :size 5)
4139 (float :tag "Fraction of frame height" :value .25 :size 5)))
4140 ;; Minimum width of pop-up windows.
4141 (cons
4142 :format "%v\n"
4143 (const :format "" pop-up-window-min-width)
4144 (choice
4145 :help-echo "Minimum width of popped-up side window."
4146 :format "%[Minimum width%] %v"
4147 (const :tag "Default" :format "%t" :value nil)
4148 (integer :tag "Number of columns" :value 12 :size 5)
4149 (float :tag "Fraction of frame width" :value .25 :size 5)))
4150 ;; Desired height of pop-up windows.
4151 (cons
4152 :format "%v\n"
4153 (const :format "" pop-up-window-set-height)
4154 (choice
4155 :help-echo "Desired height of popped-up side window."
4156 :format "%[Desired height%] %v"
4157 (const :tag "Default" :format "%t" :value nil)
4158 (integer :tag "Number of lines" :value 12 :size 5)
4159 (float :tag "Fraction of frame height" :value .25 :size 5)
4160 (function :tag "Function" :size 25)))
4161 ;; Desired width of pop-up windows.
4162 (cons
4163 :format "%v\n"
4164 (const :format "" pop-up-window-set-width)
4165 (choice
4166 :help-echo "Desired width of popped-up side window."
4167 :format "%[Desired width%] %v"
4168 (const :tag "Default" :format "%t" :value nil)
4169 (integer :tag "Number of column" :value 12 :size 5)
4170 (float :tag "Fraction of frame width" :value .25 :size 5)
4171 (function :tag "Function" :size 25)))))
4172
4173 ;; Function with argument specifiers.
4174 (list
4175 :tag "Function with arguments"
4176 :value (fun-with-args (fun-with-args 'ignore))
4177 :format "%t\n%v"
4178 :inline t
4179 ;; For customization purposes only.
4180 (const :format "" fun-with-args)
4181 (set
4182 :format "%v"
4183 :inline t
4184 (list
4185 :format "%v"
4186 :value (fun-with-args 'ignore)
4187 (const :format "" fun-with-args)
4188 (function :tag "Function" :format "%t: %v\n" :size 25)
4189 (list
4190 :format "%v"
4191 (repeat
4192 :tag "Arguments"
4193 :inline t
4194 (sexp
4195 :format "%v\n"
4196 :size 16))))))
4197
4198 ;; Macro specifiers.
4199 (list
4200 :tag "Same frame only"
4201 :format "%t%v"
4202 :inline t
4203 (const :format "\n" same-frame))
4204 (list
4205 :tag "Other window"
4206 :format "%t%v"
4207 :inline t
4208 (const :format "\n" other-window))
4209 (list
4210 :tag "Same frame other window"
4211 :format "%t%v"
4212 :inline t
4213 (const :format "\n" same-frame-other-window))
4214 (list
4215 :tag "Other frame only"
4216 :format "%t%v"
4217 :inline t
4218 (const :format "\n" other-frame))
4219 (list
4220 :tag "Default"
4221 :format "%t%v"
4222 :inline t
4223 (const :format "\n" default)))))
4224
4225 (set
4226 :format "%v"
4227 :inline t
4228 ;; Dedicate window to buffer.
4229 (cons
4230 :format "%v"
4231 (const :format "" dedicate)
4232 (choice
4233 :help-echo "Mark window as dedicated to its buffer."
4234 :format "%[Dedicate window to buffer%] %v\n" :size 15
4235 (const :tag "Off" :format "%t" nil)
4236 (const :tag "Weak" :format "%t" weak)
4237 (const :tag "Strong" :format "%t" t)))
4238 ;; No other window.
4239 (cons
4240 :format "%v"
4241 (const :format "" no-other-window)
4242 (choice
4243 :help-echo "Whether `other-window' shall ignore the window."
4244 :format "%[No other window%] %v\n" :size 15
4245 (const :tag "Off" :format "%t" nil)
4246 (const :tag "Ignore" :format "%t" t)))
4247 ;; Other window means other frame.
4248 (cons
4249 :format "%v"
4250 (const :format "" other-window-means-other-frame)
4251 (choice
4252 :help-echo "Whether other window means same or other frame."
4253 :format "%[Same or other frame%] %v\n" :size 15
4254 (const :tag "Same frame" :format "%t" nil)
4255 (const :tag "Other frame" :format "%t" t)))
4256 ;; Overriding.
4257 (cons
4258 :format "%v\n"
4259 (const :format "" override)
4260 (choice
4261 :help-echo "Override application supplied specifiers."
4262 :format "%[Override%] %v"
4263 (const :tag "Off" :format "%t" nil)
4264 (const :tag "Override" :format "%t" t))))))
4265 :group 'windows
4266 :group 'frames)
4267
4268 (defcustom display-buffer-function nil
4269 "If non-nil, function to call to display a buffer.
4270 `display-buffer' calls this function with two arguments, the
4271 buffer to display and a list of buffer display specifiers, see
4272 `display-buffer-alist'.
4273
4274 The function is supposed to choose or create a window, display
4275 the specified buffer in it, and return the window. It is also
4276 responsible for giving the variable `display-buffer-window' and
4277 the `quit-restore' parameter of the window used a meaningful
4278 value.
4279
4280 The function specified here overrides all specifiers of the
4281 variable `display-buffer-alist' any specifiers passed to
4282 `display-buffer'.
4283
4284 If you call `display-buffer' within the body of the function,
4285 bind the value of `display-buffer-function' to nil around that
4286 call to avoid that the function recursively calls itself."
4287 :type '(choice
4288 (const nil)
4289 (function :tag "Function"))
4290 :group 'windows)
4291
4292 ;; The following is a global variable which is used externally (by
4293 ;; help.el) to (1) know which window was used for displaying a buffer
4294 ;; and (2) whether the window was new or reused.
4295 (defvar display-buffer-window nil
4296 "Window used by `display-buffer' and related information.
4297 After `display-buffer' displays a buffer in some window this
4298 variable is a cons cell whose car denotes the window used to
4299 display the buffer. The cdr is supposed to be one of the symbols
4300 `reuse-buffer-window', `reuse-other-window', `new-window' or
4301 `new-frame'.
4302
4303 If the buffer display location specifier is one of 'same-window,
4304 'same-frame, or 'other-frame, the `display-buffer' routines
4305 assign the value of this variable. If the location specifier is
4306 a function, that function becomes responsible for assigning a
4307 meaningful value to this variable. See the functions
4308 `display-buffer-reuse-window', `display-buffer-pop-up-window' and
4309 `display-buffer-pop-up-frame' for how this can be done.")
4310
4311 (defun display-buffer-even-window-sizes (window specifiers)
4312 "Even sizes of WINDOW and selected window according to SPECIFIERS.
4313 SPECIFIERS must be a list of buffer display specifiers, see the
4314 documentation of `display-buffer-alist' for a description.
4315
4316 Sizes are evened out if and only if WINDOW and the selected
4317 window appear next to each other and the selected window is
4318 larger than WINDOW."
4319 (cond
4320 ((or (not (cdr (assq 'reuse-window-even-sizes specifiers)))
4321 ;; Don't resize minibuffer windows.
4322 (window-minibuffer-p)
4323 ;; WINDOW must be adjacent to the selected one.
4324 (not (or (eq window (window-prev))
4325 (eq window (window-next))))))
4326 ((and (window-iso-combined-p window)
4327 ;; Resize iff the selected window is higher than WINDOW.
4328 (> (window-total-height) (window-total-height window)))
4329 ;; Don't throw an error if we can't even window heights for
4330 ;; whatever reason. In any case, enlarging the selected window
4331 ;; might fail anyway if there are other windows above or below
4332 ;; WINDOW and the selected one. But for a simple two windows
4333 ;; configuration the present behavior is good enough so why care?
4334 (ignore-errors
4335 (resize-window
4336 window (/ (- (window-total-height) (window-total-height window))
4337 2))))
4338 ((and (window-iso-combined-p window t)
4339 ;; Resize iff the selected window is wider than WINDOW.
4340 (> (window-total-width) (window-total-width window)))
4341 ;; Don't throw an error if we can't even window widths, see
4342 ;; comment above.
4343 (ignore-errors
4344 (resize-window
4345 window (/ (- (window-total-width) (window-total-width window))
4346 2) t)))))
4347
4348 (defun display-buffer-set-height (window specifiers)
4349 "Adjust height of WINDOW according to SPECIFIERS.
4350 SPECIFIERS must be a list of buffer display specifiers, see the
4351 documentation of `display-buffer-alist' for a description."
4352 (let ((set-height (cdr (assq 'pop-up-window-set-height specifiers))))
4353 (cond
4354 ((numberp set-height)
4355 (let* ((height (if (integerp set-height)
4356 set-height
4357 (round
4358 (* (window-total-size (frame-root-window window))
4359 set-height))))
4360 (delta (- height (window-total-size window))))
4361 (when (and (window-resizable-p window delta nil 'safe)
4362 (window-iso-combined-p window))
4363 (resize-window window delta nil 'safe))))
4364 ((functionp set-height)
4365 (ignore-errors (funcall set-height window))))))
4366
4367 (defun display-buffer-set-width (window specifiers)
4368 "Adjust width of WINDOW according to SPECIFIERS.
4369 SPECIFIERS must be a list of buffer display specifiers, see the
4370 documentation of `display-buffer-alist' for a description."
4371 (let ((set-width (cdr (assq 'pop-up-window-set-width specifiers))))
4372 (cond
4373 ((numberp set-width)
4374 (let* ((width (if (integerp set-width)
4375 set-width
4376 (round
4377 (* (window-total-size (frame-root-window window) t)
4378 set-width))))
4379 (delta (- width (window-total-size window t))))
4380 (when (and (window-resizable-p window delta t 'safe)
4381 (window-iso-combined-p window t))
4382 (resize-window window delta t 'safe))))
4383 ((functionp set-width)
4384 (ignore-errors (funcall set-width window))))))
4385
4386 ;; We have to work around the deficiency that the command loop does not
4387 ;; preserve the selected window when it is on a frame that hasn't been
4388 ;; raised or given input focus. So we have to (1) select the window
4389 ;; used for displaying a buffer and (2) raise its frame if necessary,
4390 ;; thus defeating one primary principle of `display-buffer' namely to
4391 ;; _not_ select the window chosen for displaying the buffer :-(
4392 (defun display-buffer-select-window (window &optional norecord)
4393 "Select WINDOW and raise its frame if necessary."
4394 (let ((old-frame (selected-frame))
4395 (new-frame (window-frame window)))
4396 ;; Select WINDOW _before_ raising the frame to assure that the mouse
4397 ;; cursor moves into the correct window.
4398 (select-window window norecord)
4399 (unless (eq old-frame new-frame)
4400 (select-frame-set-input-focus new-frame))))
4401
4402 (defun display-buffer-in-window (buffer window specifiers)
4403 "Display BUFFER in WINDOW and raise its frame if needed.
4404 WINDOW must be a live window and defaults to the selected one.
4405 Return WINDOW.
4406
4407 SPECIFIERS must be a list of buffer display specifiers, see the
4408 documentation of `display-buffer-alist' for a description."
4409 (setq buffer (normalize-live-buffer buffer))
4410 (setq window (normalize-live-window window))
4411 (let* ((old-frame (selected-frame))
4412 (new-frame (window-frame window))
4413 (dedicated (cdr (assq 'dedicated specifiers)))
4414 (no-other-window (cdr (assq 'no-other-window specifiers))))
4415 ;; Show BUFFER in WINDOW.
4416 (set-window-dedicated-p window nil)
4417 (set-window-buffer window buffer)
4418 (when dedicated
4419 (set-window-dedicated-p window dedicated))
4420 (when no-other-window
4421 (set-window-parameter window 'no-other-window t))
4422 (unless (eq old-frame new-frame)
4423 (display-buffer-select-window window))
4424 ;; Return window.
4425 window))
4426
4427 (defun display-buffer-reuse-window (buffer method &optional specifiers)
4428 "Display BUFFER in an existing window.
4429 METHOD must be a list in the form of the cdr of a `reuse-window'
4430 buffer display specifier, see `display-buffer-alist' for an
4431 explanation. The first element must specifiy the window to use,
4432 and can be either nil, `same', `other', or a live window. The
4433 second element must specify the window's buffer and can be either
4434 nil, `same', `other', or a live buffer. The third element is the
4435 frame to use - either nil, 0, `visible', `other', t, or a live
4436 frame.
4437
4438 Optional argument SPECIFIERS must be a list of valid display
4439 specifiers. Return the window chosen to display BUFFER, nil if
4440 none was found."
4441 (let* ((method-window (nth 0 method))
4442 (method-buffer (nth 1 method))
4443 (method-frame (nth 2 method))
4444 (reuse-dedicated (assq 'reuse-window-dedicated specifiers))
4445 windows other-frame dedicated time best-window best-time)
4446 (when (eq method-frame 'other)
4447 ;; `other' is not handled by `window-list-1'.
4448 (setq other-frame t)
4449 (setq method-frame t))
4450 (dolist (window (window-list-1 nil 'nomini method-frame))
4451 (let ((window-buffer (window-buffer window)))
4452 (when (and (not (window-minibuffer-p window))
4453 ;; Don't reuse a side window.
4454 (or (not (eq (window-parameter window 'window-side) 'side))
4455 (eq window-buffer buffer))
4456 (or (not method-window)
4457 (and (eq method-window 'same)
4458 (eq window (selected-window)))
4459 (and (eq method-window 'other)
4460 (not (eq window (selected-window))))
4461 ;; Special case for applications that specifiy
4462 ;; the window explicitly.
4463 (eq method-window window))
4464 (or (not method-buffer)
4465 (and (eq method-buffer 'same)
4466 (eq window-buffer buffer))
4467 (and (eq method-buffer 'other)
4468 (not (eq window-buffer buffer)))
4469 ;; Special case for applications that specifiy
4470 ;; the window's buffer explicitly.
4471 (eq method-buffer window-buffer))
4472 (or (not other-frame)
4473 (not (eq (window-frame window) (selected-frame))))
4474 ;; Handle dedicatedness.
4475 (or (eq window-buffer buffer)
4476 ;; The window does not show the same buffer.
4477 (not (setq dedicated (window-dedicated-p window)))
4478 ;; If the window is weakly dedicated to its
4479 ;; buffer, reuse-dedicated must be non-nil.
4480 (and (not (eq dedicated t)) reuse-dedicated)
4481 ;; If the window is strongly dedicated to its
4482 ;; buffer, reuse-dedicated must be t.
4483 (eq reuse-dedicated t)))
4484 (setq windows (cons window windows)))))
4485
4486 (if (eq method-buffer 'same)
4487 ;; When reusing a window on the same buffer use the lru one.
4488 (dolist (window windows)
4489 (setq time (window-use-time window))
4490 (when (or (not best-window) (< time best-time))
4491 (setq best-window window)
4492 (setq best-time time)))
4493 ;; Otherwise, sort windows according to their use-time.
4494 (setq windows
4495 (sort windows
4496 #'(lambda (window-1 window-2)
4497 (<= (window-use-time window-1)
4498 (window-use-time window-2)))))
4499 (setq best-window
4500 ;; Try to get a full-width window (this is silly and can
4501 ;; get us to another frame but let's ignore these issues
4502 ;; for the moment).
4503 (catch 'found
4504 (dolist (window windows)
4505 (when (window-full-width-p window)
4506 (throw 'found window)))
4507 ;; If there's no full-width window return the lru window.
4508 (car windows))))
4509
4510 (when best-window
4511 (display-buffer-even-window-sizes best-window specifiers)
4512 ;; Never change the quit-restore parameter of a window here.
4513 (if (eq (window-buffer best-window) buffer)
4514 (setq display-buffer-window
4515 (cons best-window 'reuse-buffer-window))
4516 (setq display-buffer-window
4517 (cons best-window 'reuse-other-window))
4518 (unless (window-parameter best-window 'quit-restore)
4519 ;; Don't overwrite an existing quit-restore entry.
4520 (set-window-parameter
4521 best-window 'quit-restore
4522 (list (window-buffer best-window) (window-start best-window)
4523 (window-point best-window) buffer
4524 (window-total-size best-window) (selected-window)))))
4525
4526 (display-buffer-in-window buffer best-window specifiers))))
4527
4528 (defconst display-buffer-split-specifiers '(largest lru selected root left top right bottom)
4529 "List of symbols identifying window that shall be split.")
4530
4531 (defconst display-buffer-side-specifiers '(below right above left nil)
4532 "List of symbols identifying side of split-off window.")
4533
4534 (defun display-buffer-split-window-1 (window side min-size)
4535 "Subroutine of `display-buffer-split-window'."
4536 (let* ((horizontal (memq side '(left right)))
4537 (parent (window-parent window))
4538 (resize (and window-splits (window-iso-combined-p window horizontal)))
4539 (old-size
4540 ;; We either resize WINDOW or its parent.
4541 (window-total-size (if resize parent window) horizontal))
4542 new-size)
4543 ;; We don't call split-window-vertically/-horizontally any more
4544 ;; here. If for some reason it's needed we can always do so
4545 ;; (provided we give it an optional SIDE argument).
4546 (cond
4547 (resize
4548 ;; When we resize a combination, the new window must be at least
4549 ;; MIN-SIZE large after the split.
4550 (setq new-size
4551 (max min-size
4552 (min (- old-size (window-min-size parent horizontal))
4553 (/ old-size
4554 ;; Try to make the size of the new window
4555 ;; proportional to the number of iso-arranged
4556 ;; windows in the combination.
4557 (1+ (window-iso-combinations parent horizontal))))))
4558 (when (window-sizable-p parent (- new-size) horizontal)
4559 (split-window window (- new-size) side)))
4560 ((window-live-p window)
4561 (setq new-size (/ old-size 2))
4562 ;; When WINDOW is live, the old _and_ the new window must be at
4563 ;; least MIN-SIZE large after the split.
4564 (when (and (>= new-size min-size)
4565 (window-sizable-p window (- new-size) horizontal))
4566 ;; Do an even split to make Stepan happy.
4567 (split-window window nil side)))
4568 (t
4569 ;; When WINDOW is internal, the new window must be at least
4570 ;; MIN-SIZE large after the split.
4571 (setq new-size
4572 (max min-size
4573 (/ old-size
4574 ;; Try to make the size of the new window
4575 ;; proportional to the number of iso-arranged
4576 ;; subwindows of WINDOW.
4577 (1+ (window-iso-combinations window horizontal)))))
4578 (when (window-sizable-p window (- new-size) horizontal)
4579 (split-window window (- new-size) side))))))
4580
4581 (defun display-buffer-split-window (window &optional side specifiers)
4582 "Split WINDOW in a way suitable for `display-buffer'.
4583 Optional argument SIDE must be a side specifier \(one of the
4584 symbols below, right, above, left, or nil). SPECIFIERS must be a
4585 list of buffer display specifiers, see the documentation of
4586 `display-buffer-alist' for a description.
4587
4588 Return the new window, nil if it could not be created."
4589 (let ((min-height (cdr (assq 'pop-up-window-min-height specifiers)))
4590 (min-width (cdr (assq 'pop-up-window-min-width specifiers)))
4591 size)
4592 ;; Normalize min-height and min-width, we might need both.
4593 (setq min-height
4594 ;; If min-height is specified, it can be as small as
4595 ;; `window-safe-min-height'.
4596 (cond
4597 ((and (integerp min-height)
4598 (>= min-height window-safe-min-height))
4599 min-height)
4600 ((and (floatp min-height)
4601 (<= min-height 1)
4602 (let* ((root-height (window-total-height
4603 (frame-root-window
4604 (window-frame window))))
4605 (height (round (* min-height root-height))))
4606 (when (>= height window-safe-min-height)
4607 height))))
4608 (t window-min-height)))
4609 (setq min-width
4610 ;; If min-width is specified, it can be as small as
4611 ;; `window-safe-min-width'.
4612 (cond
4613 ((and (integerp min-width)
4614 (>= min-width window-safe-min-width))
4615 min-width)
4616 ((and (floatp min-width)
4617 (<= min-width 1)
4618 (let* ((root-width (window-total-width
4619 (frame-root-window
4620 (window-frame window))))
4621 (width (round (* min-width root-width))))
4622 (when (>= width window-safe-min-width)
4623 width))))
4624 (t window-min-width)))
4625
4626 (or (and (memq side '(nil above below))
4627 (display-buffer-split-window-1
4628 window (or side 'below) min-height))
4629 ;; If SIDE is nil and vertical splitting failed, we try again
4630 ;; splitting horizontally this time.
4631 (and (memq side '(nil left right))
4632 (display-buffer-split-window-1
4633 window (or side 'right) min-width))
4634 ;; If WINDOW is live and the root window of its frame, try once
4635 ;; more splitting vertically, disregarding the min-height
4636 ;; specifier this time and using `window-min-height' instead.
4637 (and (memq side '(nil above below))
4638 (<= window-min-height min-height)
4639 (window-live-p window)
4640 (eq window (frame-root-window window))
4641 (display-buffer-split-window-1
4642 window (or side 'below) window-min-height)))))
4643
4644 (defun display-buffer-split-atom-window (window &optional side nest specifiers)
4645 "Make WINDOW part of an atomic window."
4646 (let ((ignore-window-parameters t)
4647 (window-nest t)
4648 (selected-window (selected-window))
4649 root new new-parent)
4650
4651 ;; We are in an atomic window.
4652 (when (and (window-parameter window 'window-atom) (not nest))
4653 ;; Split the root window.
4654 (setq window (window-atom-root window)))
4655
4656 (when (setq new (display-buffer-split-window window side specifiers))
4657 (setq new-parent (window-parent window))
4658 ;; WINDOW is or becomes atomic.
4659 (unless (window-parameter window 'window-atom)
4660 (walk-window-subtree
4661 (lambda (window)
4662 (set-window-parameter window 'window-atom t))
4663 window t))
4664 ;; New window and any new parent get their window-atom parameter
4665 ;; set too.
4666 (set-window-parameter new 'window-atom t)
4667 (set-window-parameter new-parent 'window-atom t)
4668 new)))
4669
4670 (defun display-buffer-pop-up-window (buffer methods &optional specifiers)
4671 "Display BUFFER in a new window.
4672 Return the window displaying BUFFER, nil if popping up the window
4673 failed. METHODS must be a list of window/side tuples like those
4674 forming the cdr of the `pop-up-window' buffer display specifier.
4675 As a special case, the car of such a tuple can be also a live
4676 window.
4677
4678 Optional argument SPECIFIERS must be a list of buffer display
4679 specifiers, see the doc-string of `display-buffer-alist' for a
4680 description."
4681 (let* ((frame (display-buffer-frame))
4682 (selected-window (frame-selected-window frame))
4683 window side atomic)
4684 (unless (and (cdr (assq 'unsplittable (frame-parameters frame)))
4685 ;; Don't split an unsplittable frame unless
4686 ;; SPECIFIERS allow it.
4687 (not (cdr (assq 'split-unsplittable-frame specifiers))))
4688 (catch 'done
4689 (dolist (method methods)
4690 (setq window (car method))
4691 (setq side (cdr method))
4692 (and (setq window
4693 (cond
4694 ((eq window 'largest)
4695 (get-largest-window frame t))
4696 ((eq window 'lru)
4697 (get-lru-window frame t))
4698 ((eq window 'selected)
4699 (frame-selected-window frame))
4700 ((eq window 'root)
4701 ;; If there are side windows, split the main
4702 ;; window else the frame root window.
4703 (or (window-with-parameter 'window-side 'none nil t)
4704 (frame-root-window frame)))
4705 ((memq window window-sides)
4706 ;; This should gets us the "root" side
4707 ;; window if there exists more than one.
4708 (window-with-parameter 'window-side window nil t))
4709 ((windowp window)
4710 ;; A window, directly specified.
4711 window)))
4712 ;; The window must be on the selected frame,
4713 (eq (window-frame window) frame)
4714 ;; and must be neither a minibuffer window,
4715 (not (window-minibuffer-p window))
4716 ;; nor a side window.
4717 (not (eq (window-parameter window 'window-side) 'side))
4718 (setq window
4719 (cond
4720 ((memq side display-buffer-side-specifiers)
4721 (if (and (window-buffer window)
4722 (setq atomic (cdr (assq 'atomic specifiers))))
4723 (display-buffer-split-atom-window
4724 window side (eq atomic 'nest) specifiers)
4725 (display-buffer-split-window window side specifiers)))
4726 ((functionp side)
4727 (ignore-errors
4728 ;; Don't pass any specifiers to this function.
4729 (funcall side window)))))
4730 (throw 'done window))))
4731
4732 (when window
4733 ;; Adjust sizes if asked for.
4734 (display-buffer-set-height window specifiers)
4735 (display-buffer-set-width window specifiers)
4736 (set-window-parameter
4737 window 'quit-restore (list 'new-window buffer selected-window))
4738 (setq display-buffer-window (cons window 'new-window))
4739 (display-buffer-in-window buffer window specifiers)
4740 (set-window-prev-buffers window nil)
4741 window))))
4742
4743 (defun display-buffer-pop-up-frame (buffer &optional graphic-only specifiers)
4744 "Make a new frame for displaying BUFFER.
4745 Return the window displaying BUFFER if creating the new frame was
4746 successful, nil otherwise. Optional argument GRAPHIC-ONLY
4747 non-nil means to make a new frame on graphic displays only.
4748
4749 SPECIFIERS must be a list of buffer display specifiers, see the
4750 documentation of `display-buffer-alist' for a description."
4751 (unless (or (and graphic-only (not (display-graphic-p)))
4752 noninteractive)
4753 (let* ((selected-window (selected-window))
4754 (function (or (cdr (assq 'pop-up-frame-function specifiers))
4755 'make-frame))
4756 (parameters
4757 (when (symbolp function)
4758 (cdr (assq 'pop-up-frame-alist specifiers))))
4759 (frame
4760 (if (symbolp function)
4761 (funcall function parameters)
4762 (funcall function))))
4763 (when frame
4764 (let ((window (frame-selected-window frame)))
4765 (set-window-parameter
4766 window 'quit-restore (list 'new-frame buffer selected-window))
4767 (setq display-buffer-window (cons window 'new-frame))
4768 (display-buffer-in-window buffer window specifiers))))))
4769
4770 (defun display-buffer-pop-up-side-window (buffer side slot &optional specifiers)
4771 "Display BUFFER in a new window on SIDE of the selected frame.
4772 SLOT specifies the slot to use. SPECIFIERS must be a list of
4773 buffer display specifiers.
4774
4775 Return the window displaying BUFFER, nil if popping up the window
4776 failed."
4777 (let* ((root (frame-root-window))
4778 (main (window-with-parameter 'window-side 'none nil t))
4779 (left-or-right (memq side '(left right)))
4780 (main-or-root
4781 (if (and main
4782 (or (and left-or-right (not window-sides-vertical))
4783 (and (not left-or-right) window-sides-vertical)))
4784 main
4785 root))
4786 (selected-window (selected-window))
4787 (on-side (cond
4788 ((eq side 'top) 'above)
4789 ((eq side 'bottom) 'below)
4790 (t side)))
4791 (window
4792 (display-buffer-split-window main-or-root on-side specifiers))
4793 fun)
4794 (when window
4795 (unless main
4796 (walk-window-subtree
4797 (lambda (window)
4798 ;; Make all main-or-root subwindows main windows.
4799 (set-window-parameter window 'window-side 'none))
4800 main-or-root t))
4801 ;; Make sure that parent's window-side is nil.
4802 (set-window-parameter (window-parent window) 'window-side nil)
4803 ;; Initialize side.
4804 (set-window-parameter window 'window-side side)
4805 ;; Adjust sizes if asked for.
4806 (display-buffer-set-height window specifiers)
4807 (display-buffer-set-width window specifiers)
4808 ;; Set window parameters.
4809 (set-window-parameter
4810 window 'quit-restore (list 'new-window buffer selected-window))
4811 (setq display-buffer-window (cons window 'new-window))
4812 (set-window-parameter window 'window-slot slot)
4813 (display-buffer-in-window buffer window specifiers)
4814 (set-window-prev-buffers window nil)
4815 window)))
4816
4817 (defun display-buffer-in-side-window (buffer side &optional slot specifiers)
4818 "Display BUFFER in a window on SIDE of the selected frame.
4819 SLOT, if non-nil, specifies the window slot where to display the
4820 BUFFER. SLOT zero or nil means use the central slot on SIDE.
4821 SLOT negative means use a slot preceding the central window.
4822 SLOT positive means use a slot following the central window.
4823
4824 SPECIFIERS must be a list of buffer display specifiers."
4825 (unless (memq side window-sides)
4826 (error "Invalid side %s specified" side))
4827 (let* ((major (window-with-parameter 'window-side side nil t))
4828 ;; `major' is the major window on SIDE, `windows' the life
4829 ;; windows on SIDE.
4830 (windows (when major (windows-with-parameter 'window-side side)))
4831 (slots (when major (window-child-count major)))
4832 (max-slots
4833 (nth (cond
4834 ((eq side 'left) 0)
4835 ((eq side 'top) 1)
4836 ((eq side 'right) 2)
4837 ((eq side 'bottom) 3))
4838 window-sides-slots))
4839 (selected-window (selected-window))
4840 window this-window this-slot prev-window next-window
4841 best-window best-slot abs-slot)
4842
4843 (unless (numberp slot)
4844 (setq slot 0))
4845 (if (not windows)
4846 ;; No suitable side window exists, make one.
4847 (display-buffer-pop-up-side-window buffer side slot specifiers)
4848 ;; Scan windows on SIDE.
4849 (catch 'found
4850 (dolist (window windows)
4851 (setq this-slot (window-parameter window 'window-slot))
4852 (cond
4853 ((not (numberp this-slot)))
4854 ((and (= this-slot slot)
4855 ;; Dedicatedness check.
4856 (or (not (window-dedicated-p window))
4857 (assq 'reuse-window-dedicated specifiers)))
4858 ;; Window with matching SLOT, use it.
4859 (setq this-window window)
4860 (throw 'found t))
4861 (t
4862 (setq abs-slot (abs (- (abs slot) (abs this-slot))))
4863 (unless (and best-slot (<= best-slot abs-slot))
4864 (setq best-window window)
4865 (setq best-slot abs-slot))
4866 (cond
4867 ((<= this-slot slot)
4868 (setq prev-window window))
4869 ((not next-window)
4870 (setq next-window window)))))))
4871
4872 ;; `this-window' is the first window with the same SLOT.
4873 ;; `prev-window' is the window with the largest slot < SLOT. A new
4874 ;; window will be created after it.
4875 ;; `next-window' is the window with the smallest slot > SLOT. A new
4876 ;; window will be created before it.
4877 ;; `best-window' is the window with the smallest absolute difference
4878 ;; of its slot and SLOT.
4879 (or (and this-window
4880 ;; Reuse this window.
4881 (prog1
4882 (setq window this-window)
4883 (if (eq (window-buffer window) buffer)
4884 (setq display-buffer-window
4885 (cons window 'reuse-buffer-window))
4886 (setq display-buffer-window
4887 (cons window 'reuse-other-window))
4888 (unless (window-parameter window 'quit-restore)
4889 ;; Don't overwrite an existing quit-restore entry.
4890 (set-window-parameter
4891 window 'quit-restore
4892 (list (window-buffer window) (window-start window)
4893 (window-point window) buffer
4894 (window-total-size window) (selected-window)))))))
4895 (and (or (not max-slots) (< slots max-slots))
4896 (or (and next-window
4897 ;; Make new window before next-window.
4898 (let ((next-side
4899 (if (memq side '(left right)) 'above 'left)))
4900 (setq window (display-buffer-split-window
4901 next-window next-side specifiers))))
4902 (and prev-window
4903 ;; Make new window after prev-window.
4904 (let ((prev-side
4905 (if (memq side '(left right)) 'below 'right)))
4906 (setq window (display-buffer-split-window
4907 prev-window prev-side specifiers)))))
4908 (progn
4909 (display-buffer-set-height window specifiers)
4910 (display-buffer-set-width window specifiers)
4911 (set-window-parameter
4912 window 'quit-restore
4913 (list 'new-window buffer selected-window))
4914 (setq display-buffer-window (cons window 'new-window))
4915 window))
4916 (and best-window
4917 (setq window best-window)
4918 ;; Reuse best window (the window nearest to SLOT).
4919 (if (eq (window-buffer window) buffer)
4920 (setq display-buffer-window
4921 (cons window 'reuse-buffer-window))
4922 (setq display-buffer-window
4923 (cons window 'reuse-other-window))
4924
4925 (unless (window-parameter window 'quit-restore)
4926 ;; Don't overwrite an existing quit-restore entry.
4927 (set-window-parameter
4928 window 'quit-restore
4929 (list (window-buffer window) (window-start window)
4930 (window-point window) buffer
4931 (window-total-size window) (selected-window)))))
4932 window))
4933
4934 (when window
4935 (unless (window-parameter window 'window-slot)
4936 ;; Don't change exisiting slot value.
4937 (set-window-parameter window 'window-slot slot))
4938 (display-buffer-in-window buffer window specifiers)))))
4939
4940 (defun display-buffer-other-window-means-other-frame (buffer-or-name &optional label)
4941 "Return non-nil if BUFFER shall be preferably displayed in another frame.
4942 BUFFER must be a live buffer or the name of a live buffer.
4943
4944 Return nil if BUFFER shall be preferably displayed in another
4945 window on the selected frame. Return non-nil if BUFFER shall be
4946 preferably displayed in a window on any but the selected frame.
4947
4948 Optional argument LABEL is like the same argument of
4949 `display-buffer'.
4950
4951 The calculation of the return value is exclusively based on the
4952 user preferences expressed in `display-buffer-alist'."
4953 (let* ((buffer (normalize-live-buffer buffer-or-name))
4954 (list (display-buffer-normalize-specifiers-3
4955 (buffer-name buffer) label))
4956 (value (assq 'other-window-means-other-frame
4957 (or (car list) (cdr list)))))
4958 (when value (cdr value))))
4959
4960 (defun normalize-buffer-to-display (buffer-or-name)
4961 "Normalize BUFFER-OR-NAME argument for buffer display functions.
4962 If BUFFER-OR-NAME is nil, return the curent buffer. Else, if a
4963 buffer specified by BUFFER-OR-NAME exists, return that buffer.
4964 If no such buffer exists, create a buffer with the name
4965 BUFFER-OR-NAME and return that buffer."
4966 (if buffer-or-name
4967 (or (get-buffer buffer-or-name)
4968 (let ((buffer (get-buffer-create buffer-or-name)))
4969 (set-buffer-major-mode buffer)
4970 buffer))
4971 (current-buffer)))
4972
4973 (defun display-buffer-normalize-specifiers-1 (specifiers buffer-name label)
4974 "Subroutine of `display-buffer-normalize-specifiers'.
4975 SPECIFIERS is a list of buffer display specfiers. BUFFER-NAME is
4976 the name of the buffer that shall be displayed, LABEL the same
4977 argument of `display-buffer'."
4978 (let (normalized entry)
4979 (cond
4980 ((not specifiers)
4981 nil)
4982 ((listp specifiers)
4983 ;; If SPECIFIERS is a list, we assume it is a list of specifiers.
4984 (dolist (specifier specifiers)
4985 (cond
4986 ((consp specifier)
4987 (setq normalized (cons specifier normalized)))
4988 ((eq specifier 'other-window)
4989 ;; `other-window' must be treated separately.
4990 (let* ((other-frame (display-buffer-other-window-means-other-frame
4991 buffer-name label))
4992 (entry (assq (if other-frame 'other-frame 'other-window)
4993 display-buffer-macro-specifiers)))
4994 (dolist (item (cdr entry))
4995 (setq normalized (cons item normalized)))))
4996 ((symbolp specifier)
4997 ;; Might be a macro specifier, try to expand it (the cdr is a
4998 ;; list and we have to reverse it later, so do it one at a
4999 ;; time).
5000 (let ((entry (assq specifier display-buffer-macro-specifiers)))
5001 (dolist (item (cdr entry))
5002 (setq normalized (cons item normalized)))))))
5003 ;; Reverse list.
5004 (nreverse normalized))
5005 ((setq entry (assq specifiers display-buffer-macro-specifiers))
5006 ;; A macro specifier.
5007 (cdr entry))
5008 ((or (display-buffer-other-window-means-other-frame buffer-name label)
5009 (with-no-warnings (not pop-up-frames)))
5010 (cdr (assq 'other-frame display-buffer-macro-specifiers)))
5011 (t
5012 ;; In any other case pop up a new window.
5013 (cdr (assq 'same-frame-other-window display-buffer-macro-specifiers))))))
5014
5015 (defun display-buffer-normalize-specifiers-2 (&optional buffer-or-name)
5016 "Subroutine of `display-buffer-normalize-specifiers'.
5017 BUFFER-OR-NAME is the buffer to display. This routine provides a
5018 compatibility layer for the now obsolete Emacs 23 buffer display
5019 options."
5020 (let* ((buffer (normalize-live-buffer buffer-or-name))
5021 (buffer-name (buffer-name buffer))
5022 specifiers)
5023 ;; Disable warnings, there are too many obsolete options here.
5024 (with-no-warnings
5025 ;; `even-window-heights', unless nil or unset.
5026 (unless (memq even-window-heights '(nil unset))
5027 (setq specifiers
5028 (cons (cons 'reuse-window-even-sizes t) specifiers)))
5029
5030 ;; `display-buffer-mark-dedicated'
5031 (when display-buffer-mark-dedicated
5032 (setq specifiers
5033 (cons (cons 'dedicate display-buffer-mark-dedicated)
5034 specifiers)))
5035
5036 ;; `pop-up-window' group. Anything is added here iff
5037 ;; `pop-up-windows' is neither nil nor unset.
5038 (let ((pop-up-window (not (memq pop-up-windows '(nil unset))))
5039 (fun (unless (eq split-window-preferred-function
5040 'split-window-sensibly)
5041 split-window-preferred-function))
5042 (min-height (if (numberp split-height-threshold)
5043 (/ split-height-threshold 2)
5044 1.0))
5045 (min-width (if (numberp split-width-threshold)
5046 (/ split-width-threshold 2)
5047 1.0)))
5048 (when pop-up-window
5049 ;; `split-height-threshold'
5050 (setq specifiers
5051 (cons (cons 'pop-up-window-min-height min-height)
5052 specifiers))
5053 ;; `split-width-threshold'
5054 (setq specifiers
5055 (cons (cons 'pop-up-window-min-width min-width)
5056 specifiers))
5057 ;; `pop-up-window'
5058 (setq specifiers
5059 (cons (list 'pop-up-window
5060 (cons 'largest fun) (cons 'lru fun))
5061 specifiers))))
5062
5063 ;; `special-display-p' group.
5064 (when special-display-function
5065 ;; `special-display-p' returns either t or a list of frame
5066 ;; parameters to pass to `special-display-function'.
5067 (let ((pars (special-display-p buffer-name)))
5068 (when pars
5069 (setq specifiers
5070 (cons (list 'fun-with-args special-display-function
5071 (when (listp pars) pars))
5072 specifiers)))))
5073
5074 ;; `pop-up-frame' group. Add things if `pop-up-frames' is non-nil
5075 ;; (we ignore the problem that callers usually don't care about
5076 ;; graphic-only).
5077 (when pop-up-frames
5078 ;; `pop-up-frame-function'. If `pop-up-frame-function' uses the
5079 ;; now obsolete `pop-up-frame-alist' it will continue to do so.
5080 (setq specifiers
5081 (cons (cons 'pop-up-frame-function pop-up-frame-function)
5082 specifiers))
5083 ;; `pop-up-frame'
5084 (setq specifiers
5085 (cons (list 'pop-up-frame pop-up-frames) specifiers)))
5086
5087 ;; `same-window-p' group.
5088 (when (same-window-p buffer-name)
5089 ;; Try to reuse the same (selected) window.
5090 (setq specifiers
5091 (cons (list 'reuse-window 'same nil nil)
5092 specifiers)))
5093
5094 ;; `pop-up-windows' and `pop-up-frames' both nil means means we
5095 ;; are supposed to reuse any window (unless we find one showing
5096 ;; the same buffer already).
5097 (unless (or pop-up-windows pop-up-frames)
5098 ;; `reuse-window' showing any buffer on same frame.
5099 (setq specifiers
5100 (cons (list 'reuse-window nil nil nil)
5101 specifiers)))
5102
5103 ;; `display-buffer-reuse-frames' or `pop-up-frames' non-nil means
5104 ;; we are supposed to reuse a window showing the same buffer on
5105 ;; another frame.
5106 (when (or display-buffer-reuse-frames pop-up-frames)
5107 ;; `reuse-window' showing same buffer on visible frame.
5108 (setq specifiers
5109 (cons (list 'reuse-window nil 'same 0) specifiers)))
5110
5111 ;; Prepend "reuse window on same frame if showing the buffer
5112 ;; already" specifier.
5113 (setq specifiers (cons (list 'reuse-window nil 'same nil)
5114 specifiers))
5115
5116 specifiers)))
5117
5118 (defun display-buffer-normalize-specifiers-3 (buffer-name label)
5119 "Subroutine of `display-buffer-normalize-specifiers'."
5120 (let (list-1 list-2)
5121 (dolist (entry display-buffer-alist)
5122 (when (and (listp entry)
5123 (catch 'match
5124 (dolist (id (car entry))
5125 (when (consp id)
5126 (let ((type (car id))
5127 (value (cdr id)))
5128 (when (or (and (eq type 'name) (stringp value)
5129 (equal value buffer-name))
5130 (and (eq type 'regexp) (stringp value)
5131 (string-match-p value buffer-name))
5132 (and (eq type 'label) (eq value label)))
5133 (throw 'match t)))))))
5134 (let* ((raw (cdr entry))
5135 (normalized
5136 (display-buffer-normalize-specifiers-1 raw buffer-name label)))
5137 (if (assq 'override raw)
5138 (setq list-1
5139 (if list-1
5140 (append list-1 normalized)
5141 normalized))
5142 (setq list-2
5143 (if list-2
5144 (append list-2 normalized)
5145 normalized))))))
5146
5147 (cons list-1 list-2)))
5148
5149 (defun display-buffer-normalize-specifiers (buffer-name specifiers label)
5150 "Return normalized specifiers for a buffer matching BUFFER-NAME or LABEL.
5151 BUFFER-NAME must be a string specifying a valid buffer name.
5152 SPECIFIERS and LABEL are the homonymous arguments of
5153 `display-buffer'.
5154
5155 The method for displaying the buffer specified by BUFFER-NAME or
5156 LABEL is established by appending the following four lists of
5157 specifiers:
5158
5159 - The specifiers in `display-buffer-alist' whose buffer
5160 identifier matches BUFFER-NAME or LABEL and whose 'override
5161 component is set.
5162
5163 - SPECIFIERS.
5164
5165 - The specifiers in `display-buffer-alist' whose buffer
5166 identifier matches BUFFER-NAME or LABEL and whose 'override
5167 component is not set.
5168
5169 - `display-buffer-default-specifiers'."
5170 (let* ((list (display-buffer-normalize-specifiers-3 buffer-name label)))
5171 (append
5172 ;; Overriding user specifiers.
5173 (car list)
5174 ;; Application specifiers.
5175 (display-buffer-normalize-specifiers-1 specifiers buffer-name label)
5176 ;; Emacs 23 compatibility specifiers.
5177 (display-buffer-normalize-specifiers-2 buffer-name)
5178 ;; Non-overriding user specifiers.
5179 (cdr list)
5180 ;; Default specifiers.
5181 display-buffer-default-specifiers)))
5182
5183 ;; Minibuffer-only frames should be documented better. They really
5184 ;; deserve a separate section in the manual. Also
5185 ;; `last-nonminibuffer-frame' is nowhere documented in the manual.
5186 (defun display-buffer-frame (&optional frame)
5187 "Return FRAME if it is live and not a minibuffer-only frame.
5188 Return the value of `last-nonminibuffer-frame' otherwise."
5189 (setq frame (normalize-live-frame frame))
5190 (if (and (frame-live-p frame)
5191 ;; A not very nice way to get that information.
5192 (not (window-minibuffer-p (frame-root-window frame))))
5193 frame
5194 (last-nonminibuffer-frame)))
5195
5196 (defun display-buffer (&optional buffer-or-name specifiers label)
5197 "Make the buffer specified by BUFFER-OR-NAME appear in some window.
5198 Optional argument BUFFER-OR-NAME may be a buffer, a string \(a
5199 buffer name), or nil. If BUFFER-OR-NAME is a string not naming
5200 an existent buffer, create a buffer with that name. If
5201 BUFFER-OR-NAME is nil or omitted, display the current buffer.
5202 Interactively, prompt for the buffer name using the minibuffer.
5203
5204 Return the window chosen to display the buffer or nil if no such
5205 window is found. Do not change the selected window unless the
5206 buffer is shown on a different frame than the selected one.
5207
5208 Optional argument SPECIFIERS must be a list of buffer display
5209 specifiers, see the documentation of `display-buffer-alist' for a
5210 description.
5211
5212 For convenience, SPECIFIERS may also consist of a single buffer
5213 display location specifier or t, where the latter means to
5214 display the buffer in any but the selected window. If SPECIFIERS
5215 is nil or omitted, this means to exclusively use the specifiers
5216 provided by `display-buffer-alist'. If the value of the latter
5217 is nil too, all specifiers are provided by the constant
5218 `display-buffer-default-specifiers'.
5219
5220 As a special case, the `reuse-window' specifier allows to specify
5221 as second element an arbitrary window, as third element an
5222 arbitrary buffer, and as fourth element an arbitrary frame. As
5223 first element of a window/side pair of the `pop-up-window'
5224 specifier you can specifiy an arbitrary window.
5225
5226 The optional third argument LABEL, if non-nil, must be a symbol
5227 specifiying the buffer display label. Applications should set
5228 this when the buffer shall be displayed in some special way but
5229 BUFFER-OR-NAME does not identify the buffer as special. Typical
5230 buffers that fit into this category are those whose names are
5231 derived from the name of the file they are visiting. A user can
5232 override SPECIFIERS by adding an entry to `display-buffer-alist'
5233 whose car contains LABEL and whose cdr specifies the preferred
5234 alternative display method.
5235
5236 The method to display the buffer is derived by combining the
5237 values of `display-buffer-alist' and SPECIFIERS. Highest
5238 priority is given to overriding elements of
5239 `display-buffer-alist'. Next come the elements specified by
5240 SPECIFIERS, followed by the non-overriding elements of
5241 `display-buffer-alist'.
5242
5243 The result must be a list of valid buffer display specifiers. If
5244 `display-buffer-function' is non-nil, call it with the buffer and
5245 this list as arguments."
5246 (interactive "BDisplay buffer:\nP")
5247 (let* ((buffer (normalize-buffer-to-display buffer-or-name))
5248 (buffer-name (buffer-name buffer))
5249 (specifiers
5250 ;; Normalize specifiers.
5251 (display-buffer-normalize-specifiers buffer-name specifiers label))
5252 ;; Don't use a minibuffer frame.
5253 (frame (display-buffer-frame))
5254 ;; `window' is the window we use for showing `buffer'.
5255 window specifier method)
5256 ;; Reset this.
5257 (setq display-buffer-window nil)
5258 (if display-buffer-function
5259 ;; Let `display-buffer-function' do the job.
5260 (funcall display-buffer-function buffer specifiers)
5261 ;; Retrieve the next location specifier while there a specifiers
5262 ;; left and we don't have a valid window.
5263 (while (and specifiers (not (window-live-p window)))
5264 (setq specifier (car specifiers))
5265 (setq specifiers (cdr specifiers))
5266 (setq method (car specifier))
5267 (setq window
5268 (cond
5269 ((eq method 'reuse-window)
5270 (display-buffer-reuse-window
5271 buffer (cdr specifier) specifiers))
5272 ((eq method 'pop-up-window)
5273 (display-buffer-pop-up-window
5274 buffer (cdr specifier) specifiers))
5275 ((eq method 'pop-up-frame)
5276 (display-buffer-pop-up-frame
5277 buffer (cdr specifier) specifiers))
5278 ((eq method 'use-side-window)
5279 (display-buffer-in-side-window
5280 buffer (nth 1 specifier) (nth 2 specifier) specifiers))
5281 ((eq method 'fun-with-args)
5282 (apply (cadr specifier) buffer (cddr specifier))))))
5283
5284 ;; If we don't have a window yet, try a fallback method. All
5285 ;; specifiers have been used up by now.
5286 (or (and (window-live-p window) window)
5287 ;; Try reusing a window showing BUFFER on any visible or
5288 ;; iconfied frame.
5289 (display-buffer-reuse-window buffer '(nil buffer 0))
5290 ;; Try reusing a window not showing BUFFER on any visible or
5291 ;; iconified frame.
5292 (display-buffer-reuse-window buffer '(nil other 0))
5293 ;; Try making a new frame.
5294 (display-buffer-pop-up-frame buffer)
5295 ;; Try using a weakly dedicated window.
5296 (display-buffer-reuse-window
5297 buffer '(nil nil t) '((reuse-window-dedicated . weak)))
5298 ;; Try using a strongly dedicated window.
5299 (display-buffer-reuse-window
5300 buffer '(nil nil t) '((reuse-window-dedicated . t)))))))
5301
5302 (defsubst display-buffer-same-window (&optional buffer-or-name label)
5303 "Display buffer specified by BUFFER-OR-NAME in the selected window.
5304 Another window will be used only if the buffer can't be shown in
5305 the selected window, usually because it is dedicated to another
5306 buffer. Optional argument BUFFER-OR-NAME and LABEL are as for
5307 `display-buffer'."
5308 (interactive "BDisplay buffer in same window:\nP")
5309 (display-buffer buffer-or-name 'same-window label))
5310
5311 (defsubst display-buffer-same-frame (&optional buffer-or-name label)
5312 "Display buffer specified by BUFFER-OR-NAME in a window on the same frame.
5313 Another frame will be used only if there is no other choice.
5314 Optional argument BUFFER-OR-NAME and LABEL are as for
5315 `display-buffer'."
5316 (interactive "BDisplay buffer on same frame:\nP")
5317 (display-buffer buffer-or-name 'same-frame label))
5318
5319 (defsubst display-buffer-other-window (&optional buffer-or-name label)
5320 "Display buffer specified by BUFFER-OR-NAME in another window.
5321 The selected window will be used only if there is no other
5322 choice. Windows on the selected frame are preferred to windows
5323 on other frames. Optional argument BUFFER-OR-NAME and LABEL are as
5324 for `display-buffer'."
5325 (interactive "BDisplay buffer in another window:\nP")
5326 (display-buffer buffer-or-name 'other-window label))
5327
5328 (defun display-buffer-same-frame-other-window (&optional buffer-or-name label)
5329 "Display buffer specified by BUFFER-OR-NAME in another window on the same frame.
5330 The selected window or another frame will be used only if there
5331 is no other choice. Optional argument BUFFER-OR-NAME and LABEL are
5332 as for `display-buffer'."
5333 (interactive "BDisplay buffer in another window on same frame:\nP")
5334 (display-buffer buffer-or-name 'same-frame-other-window label))
5335
5336 (defun display-buffer-other-frame (&optional buffer-or-name label)
5337 "Display buffer specified by BUFFER-OR-NAME on another frame.
5338 The selected frame will be used only if there is no other choice.
5339 Optional argument BUFFER-OR-NAME and LABEL are as for
5340 `display-buffer'.
5341
5342 If this command uses another frame, it will also select that frame."
5343 (interactive "BDisplay buffer in other frame: ")
5344 (display-buffer buffer-or-name 'other-frame label))
5345
5346 (defun pop-to-buffer (&optional buffer-or-name specifiers norecord label)
5347 "Display buffer specified by BUFFER-OR-NAME and select the window used.
5348 Optional argument BUFFER-OR-NAME may be a buffer, a string \(a
5349 buffer name), or nil. If BUFFER-OR-NAME is a string not naming
5350 an existent buffer, create a buffer with that name. If
5351 BUFFER-OR-NAME is nil or omitted, display the current buffer.
5352 Interactively, prompt for the buffer name using the minibuffer.
5353
5354 Optional second argument SPECIFIERS must be a list of buffer
5355 display specifiers, a single location specifier, `t' which means
5356 the latter means to display the buffer in any but the selected
5357 window, or nil which means to exclusively apply the specifiers
5358 customized by the user.
5359
5360 Optional argument NORECORD non-nil means do not put the buffer
5361 specified by BUFFER-OR-NAME at the front of the buffer list and
5362 do not make the window displaying it the most recently selected
5363 one.
5364
5365 The optional argument LABEL, if non-nil, is a symbol specifying the
5366 display purpose. Applications should set this when the buffer
5367 shall be displayed in a special way but BUFFER-OR-NAME does not
5368 identify the buffer as special. Buffers that typically fit into
5369 this category are those whose names have been derived from the
5370 name of the file they are visiting.
5371
5372 Return the buffer specified by BUFFER-OR-NAME or nil if
5373 displaying the buffer failed.
5374
5375 This uses the function `display-buffer' as a subroutine; see the
5376 documentations of `display-buffer' and `display-buffer-alist' for
5377 additional information."
5378 (interactive "BPop to buffer:\nP")
5379 (let ((buffer (normalize-buffer-to-display buffer-or-name))
5380 window)
5381 (set-buffer buffer)
5382 (when (setq window (display-buffer buffer specifiers label))
5383 (select-window window norecord)
5384 buffer)))
5385
5386 (defsubst pop-to-buffer-same-window (&optional buffer-or-name norecord label)
5387 "Pop to buffer specified by BUFFER-OR-NAME in the selected window.
5388 Another window will be used only if the buffer can't be shown in
5389 the selected window, usually because it is dedicated to another
5390 buffer. Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are
5391 as for `pop-to-buffer'."
5392 (interactive "BPop to buffer in selected window:\nP")
5393 (pop-to-buffer buffer-or-name 'same-window norecord label))
5394
5395 (defsubst pop-to-buffer-same-frame (&optional buffer-or-name norecord label)
5396 "Pop to buffer specified by BUFFER-OR-NAME in a window on the selected frame.
5397 Another frame will be used only if there is no other choice.
5398 Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are as for
5399 `pop-to-buffer'."
5400 (interactive "BPop to buffer on same frame:\nP")
5401 (pop-to-buffer buffer-or-name 'same-frame norecord label))
5402
5403 (defsubst pop-to-buffer-other-window (&optional buffer-or-name norecord label)
5404 "Pop to buffer specified by BUFFER-OR-NAME in another window.
5405 The selected window will be used only if there is no other
5406 choice. Windows on the selected frame are preferred to windows
5407 on other frames. Optional arguments BUFFER-OR-NAME, NORECORD and
5408 LABEL are as for `pop-to-buffer'."
5409 (interactive "BPop to buffer in another window:\nP")
5410 (pop-to-buffer buffer-or-name 'other-window norecord))
5411
5412 (defsubst pop-to-buffer-same-frame-other-window (&optional buffer-or-name norecord label)
5413 "Pop to buffer specified by BUFFER-OR-NAME in another window on the selected frame.
5414 The selected window or another frame will be used only if there
5415 is no other choice. Optional arguments BUFFER-OR-NAME, NORECORD
5416 and LABEL are as for `pop-to-buffer'."
5417 (interactive "BPop to buffer in another window on same frame:\nP")
5418 (pop-to-buffer buffer-or-name 'same-frame-other-window norecord label))
5419
5420 (defsubst pop-to-buffer-other-frame (&optional buffer-or-name norecord label)
5421 "Pop to buffer specified by BUFFER-OR-NAME on another frame.
5422 The selected frame will be used only if there's no other choice.
5423 Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are as for
5424 `pop-to-buffer'."
5425 (interactive "BPop to buffer on another frame:\nP")
5426 (pop-to-buffer buffer-or-name 'other-frame norecord label))
5427
5428 (defun read-buffer-to-switch (prompt)
5429 "Read the name of a buffer to switch to, prompting with PROMPT.
5430 Return the neame of the buffer as a string.
5431
5432 This function is intended for the `switch-to-buffer' family of
5433 commands since these need to omit the name of the current buffer
5434 from the list of completions and default values."
5435 (let ((rbts-completion-table (internal-complete-buffer-except)))
5436 (minibuffer-with-setup-hook
5437 (lambda ()
5438 (setq minibuffer-completion-table rbts-completion-table)
5439 ;; Since rbts-completion-table is built dynamically, we
5440 ;; can't just add it to the default value of
5441 ;; icomplete-with-completion-tables, so we add it
5442 ;; here manually.
5443 (if (and (boundp 'icomplete-with-completion-tables)
5444 (listp icomplete-with-completion-tables))
5445 (set (make-local-variable 'icomplete-with-completion-tables)
5446 (cons rbts-completion-table
5447 icomplete-with-completion-tables))))
5448 (read-buffer prompt (other-buffer (current-buffer))
5449 (confirm-nonexistent-file-or-buffer)))))
5450
5451 (defun normalize-buffer-to-switch-to (buffer-or-name)
5452 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
5453 If BUFFER-OR-NAME is nil, return the buffer returned by
5454 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
5455 exists, return that buffer. If no such buffer exists, create a
5456 buffer with the name BUFFER-OR-NAME and return that buffer."
5457 (if buffer-or-name
5458 (or (get-buffer buffer-or-name)
5459 (let ((buffer (get-buffer-create buffer-or-name)))
5460 (set-buffer-major-mode buffer)
5461 buffer))
5462 (other-buffer)))
5463
5464 (defun switch-to-buffer (buffer-or-name &optional norecord)
5465 "Switch to buffer BUFFER-OR-NAME in the selected window.
5466 If called interactively, prompt for the buffer name using the
5467 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5468 determines whether to request confirmation before creating a new
5469 buffer.
5470
5471 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
5472 nil. If BUFFER-OR-NAME is a string that does not identify an
5473 existing buffer, create a buffer with that name. If
5474 BUFFER-OR-NAME is nil, switch to the buffer returned by
5475 `other-buffer'.
5476
5477 Optional argument NORECORD non-nil means do not put the buffer
5478 specified by BUFFER-OR-NAME at the front of the buffer list and
5479 do not make the window displaying it the most recently selected
5480 one. Return the buffer switched to.
5481
5482 This function is intended for interactive use only. Lisp
5483 functions should call `pop-to-buffer-same-window' instead."
5484 (interactive
5485 (list (read-buffer-to-switch "Switch to buffer: ")))
5486 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5487 (if (and (or (window-minibuffer-p) (eq (window-dedicated-p) t))
5488 (not (eq buffer (window-buffer))))
5489 ;; Cannot switch to another buffer in a minibuffer or strongly
5490 ;; dedicated window that does not show the buffer already. Call
5491 ;; `pop-to-buffer' instead.
5492 (pop-to-buffer buffer 'same-window norecord)
5493 (unless (eq buffer (window-buffer))
5494 ;; I'm not sure why we should NOT call `set-window-buffer' here,
5495 ;; but let's keep things as they are (otherwise we could always
5496 ;; call `pop-to-buffer-same-window' here).
5497 (set-window-buffer nil buffer))
5498 (unless norecord
5499 (select-window (selected-window)))
5500 (set-buffer buffer))))
5501
5502 (defun switch-to-buffer-same-frame (buffer-or-name &optional norecord)
5503 "Switch to buffer BUFFER-OR-NAME in a window on the selected frame.
5504 Another frame will be used only if there is no other choice.
5505 Arguments BUFFER-OR-NAME and NORECORD have the same meaning as
5506 for `switch-to-buffer'.
5507
5508 This function is intended for interactive use only. Lisp
5509 functions should call `pop-to-buffer-same-frame' instead."
5510 (interactive
5511 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5512 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5513 (pop-to-buffer buffer 'same-frame norecord)))
5514
5515 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
5516 "Switch to buffer BUFFER-OR-NAME in another window.
5517 The selected window will be used only if there is no other
5518 choice. Windows on the selected frame are preferred to windows
5519 on other frames. Arguments BUFFER-OR-NAME and NORECORD have the
5520 same meaning as for `switch-to-buffer'.
5521
5522 This function is intended for interactive use only. Lisp
5523 functions should call `pop-to-buffer-other-window' instead."
5524 (interactive
5525 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5526 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5527 (pop-to-buffer buffer 'other-window norecord)))
5528
5529 (defun switch-to-buffer-other-window-same-frame (buffer-or-name &optional norecord)
5530 "Switch to buffer BUFFER-OR-NAME in another window on the selected frame.
5531 The selected window or another frame will be used only if there
5532 is no other choice. Arguments BUFFER-OR-NAME and NORECORD have
5533 the same meaning as for `switch-to-buffer'.
5534
5535 This function is intended for interactive use only. Lisp
5536 functions should call `pop-to-buffer-other-window-same-frame'
5537 instead."
5538 (interactive
5539 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5540 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5541 (pop-to-buffer buffer 'same-frame-other-window norecord)))
5542
5543 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
5544 "Switch to buffer BUFFER-OR-NAME on another frame.
5545 The same frame will be used only if there is no other choice.
5546 Arguments BUFFER-OR-NAME and NORECORD have the same meaning
5547 as for `switch-to-buffer'.
5548
5549 This function is intended for interactive use only. Lisp
5550 functions should call `pop-to-buffer-other-frame' instead."
5551 (interactive
5552 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
5553 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5554 (pop-to-buffer buffer 'other-frame norecord)))
5555
5556 ;;; Obsolete definitions of `display-buffer' below.
5557 (defcustom same-window-buffer-names nil
5558 "List of names of buffers that should appear in the \"same\" window.
5559 `display-buffer' and `pop-to-buffer' show a buffer whose name is
5560 on this list in the selected rather than some other window.
5561
5562 An element of this list can be a cons cell instead of just a
5563 string. In that case, the cell's car must be a string specifying
5564 the buffer name. This is for compatibility with
5565 `special-display-buffer-names'; the cdr of the cons cell is
5566 ignored.
5567
5568 See also `same-window-regexps'."
5569 :type '(repeat (string :format "%v"))
5570 :group 'windows)
5571 (make-obsolete-variable
5572 'same-window-buffer-names
5573 "use 2nd arg of `display-buffer' instead." "24.1")
5574
5575 (defcustom same-window-regexps nil
5576 "List of regexps saying which buffers should appear in the \"same\" window.
5577 `display-buffer' and `pop-to-buffer' show a buffer whose name
5578 matches a regexp on this list in the selected rather than some
5579 other window.
5580
5581 An element of this list can be a cons cell instead of just a
5582 string. In that case, the cell's car must be a regexp matching
5583 the buffer name. This is for compatibility with
5584 `special-display-regexps'; the cdr of the cons cell is ignored.
5585
5586 See also `same-window-buffer-names'."
5587 :type '(repeat (regexp :format "%v"))
5588 :group 'windows)
5589 (make-obsolete-variable
5590 'same-window-regexps
5591 "use 2nd arg of `display-buffer' instead." "24.1")
5592
5593 (defun same-window-p (buffer-name)
5594 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5595 This function returns non-nil if `display-buffer' or
5596 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
5597 selected rather than \(as usual\) some other window. See
5598 `same-window-buffer-names' and `same-window-regexps'."
5599 (let ((buffer-names (with-no-warnings same-window-buffer-names))
5600 (regexps (with-no-warnings same-window-regexps)))
5601 (cond
5602 ((not (stringp buffer-name)))
5603 ;; The elements of `same-window-buffer-names' can be buffer
5604 ;; names or cons cells whose cars are buffer names.
5605 ((member buffer-name buffer-names))
5606 ((assoc buffer-name buffer-names))
5607 ((catch 'found
5608 (dolist (regexp regexps)
5609 ;; The elements of `same-window-regexps' can be regexps
5610 ;; or cons cells whose cars are regexps.
5611 (when (or (and (stringp regexp)
5612 (string-match regexp buffer-name))
5613 (and (consp regexp) (stringp (car regexp))
5614 (string-match-p (car regexp) buffer-name)))
5615 (throw 'found t))))))))
5616 (make-obsolete
5617 'same-window-p "pass argument to buffer display function instead." "24.1")
5618
5619 (defcustom special-display-frame-alist
5620 '((height . 14) (width . 80) (unsplittable . t))
5621 "Alist of parameters for special frames.
5622 Special frames are used for buffers whose names are listed in
5623 `special-display-buffer-names' and for buffers whose names match
5624 one of the regular expressions in `special-display-regexps'.
5625
5626 This variable can be set in your init file, like this:
5627
5628 (setq special-display-frame-alist '((width . 80) (height . 20)))
5629
5630 These supersede the values given in `default-frame-alist'."
5631 :type '(repeat (cons :format "%v"
5632 (symbol :tag "Parameter")
5633 (sexp :tag "Value")))
5634 :group 'frames)
5635 (make-obsolete-variable
5636 'special-display-frame-alist
5637 "use 2nd arg of `display-buffer' instead." "24.1")
5638
5639 (defun special-display-popup-frame (buffer &optional args)
5640 "Display BUFFER in a special frame and return the window chosen.
5641 If BUFFER is already displayed in a visible or iconified frame,
5642 raise that frame. Otherwise, display BUFFER in a way as
5643 specified by optional argument ARGS.
5644
5645 If ARGS is an alist, use it as a list of frame parameters. If
5646 these parameters contain \(same-window . t), display BUFFER in
5647 the selected window. If they contain \(same-frame . t), display
5648 BUFFER in a window on the selected frame.
5649
5650 If ARGS is a list whose car is a symbol, use (car ARGS) as a
5651 function to do the work. Pass it BUFFER as first argument,
5652 and (cdr ARGS) as second."
5653 (if (and args (symbolp (car args)))
5654 (apply (car args) buffer (cdr args))
5655 (let ((window (get-buffer-window buffer 0)))
5656 (or
5657 ;; If we have a window already, make it visible.
5658 (when window
5659 (let ((frame (window-frame window)))
5660 (make-frame-visible frame)
5661 (raise-frame frame)
5662 window))
5663 ;; Reuse the current window if the user requested it.
5664 (when (cdr (assq 'same-window args))
5665 (display-buffer-reuse-window
5666 buffer '(same nil nil) '((reuse-dedicated . 'weak))))
5667 ;; Stay on the same frame if requested.
5668 (when (or (cdr (assq 'same-frame args))
5669 (cdr (assq 'same-window args)))
5670 (or (display-buffer-pop-up-window
5671 buffer '((largest . nil) (lru . nil)))
5672 (display-buffer-reuse-window
5673 buffer '(nil nil nil))))
5674 ;; If no window yet, make one in a new frame.
5675 (let ((frame
5676 (with-current-buffer buffer
5677 (make-frame
5678 (append args (with-no-warnings
5679 special-display-frame-alist))))))
5680 (set-window-buffer (frame-selected-window frame) buffer)
5681 (set-window-dedicated-p (frame-selected-window frame) t)
5682 (frame-selected-window frame))))))
5683 (make-obsolete
5684 'special-display-popup-frame
5685 "use 2nd arg of `display-buffer' instead." "24.1")
5686
5687 (defcustom special-display-function 'special-display-popup-frame
5688 "Function to call for displaying special buffers.
5689 This function is called with two arguments - the buffer and,
5690 optionally, a list - and should return a window displaying that
5691 buffer. The default value usually makes a separate frame for the
5692 buffer using `special-display-frame-alist' to specify the frame
5693 parameters. See the definition of `special-display-popup-frame'
5694 for how to specify such a function.
5695
5696 A buffer is special when its name is either listed in
5697 `special-display-buffer-names' or matches a regexp in
5698 `special-display-regexps'."
5699 :type 'function
5700 :group 'windows
5701 :group 'frames)
5702 (make-obsolete-variable
5703 'special-display-function
5704 "use 2nd arg of `display-buffer' instead." "24.1")
5705
5706 (defcustom special-display-buffer-names nil
5707 "List of names of buffers that should be displayed specially.
5708 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5709 its name is in this list, displays the buffer in a way specified
5710 by `special-display-function'. `special-display-popup-frame'
5711 \(the default for `special-display-function') usually displays
5712 the buffer in a separate frame made with the parameters specified
5713 by `special-display-frame-alist'. If `special-display-function'
5714 has been set to some other function, that function is called with
5715 the buffer as first, and nil as second argument.
5716
5717 Alternatively, an element of this list can be specified as
5718 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
5719 name and FRAME-PARAMETERS an alist of \(PARAMETER . VALUE) pairs.
5720 `special-display-popup-frame' will interpret such pairs as frame
5721 parameters when it creates a special frame, overriding the
5722 corresponding values from `special-display-frame-alist'.
5723
5724 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5725 `special-display-popup-frame' displays that buffer in the
5726 selected window. If FRAME-PARAMETERS contains (same-frame . t),
5727 it displays that buffer in a window on the selected frame.
5728
5729 If `special-display-function' specifies some other function than
5730 `special-display-popup-frame', that function is called with the
5731 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5732 argument.
5733
5734 Finally, an element of this list can be also specified as
5735 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5736 `special-display-popup-frame' will call FUNCTION with the buffer
5737 named BUFFER-NAME as first argument, and OTHER-ARGS as the
5738 second. If `special-display-function' specifies some other
5739 function, that function is called with the buffer named
5740 BUFFER-NAME as first, and the element's cdr as second argument.
5741
5742 If this variable appears \"not to work\", because you added a
5743 name to it but the corresponding buffer is displayed in the
5744 selected window, look at the values of `same-window-buffer-names'
5745 and `same-window-regexps'. Those variables take precedence over
5746 this one.
5747
5748 See also `special-display-regexps'."
5749 :type '(repeat
5750 (choice :tag "Buffer"
5751 :value ""
5752 (string :format "%v")
5753 (cons :tag "With parameters"
5754 :format "%v"
5755 :value ("" . nil)
5756 (string :format "%v")
5757 (repeat :tag "Parameters"
5758 (cons :format "%v"
5759 (symbol :tag "Parameter")
5760 (sexp :tag "Value"))))
5761 (list :tag "With function"
5762 :format "%v"
5763 :value ("" . nil)
5764 (string :format "%v")
5765 (function :tag "Function")
5766 (repeat :tag "Arguments" (sexp)))))
5767 :group 'windows
5768 :group 'frames)
5769 (make-obsolete-variable
5770 'special-display-buffer-names
5771 "use 2nd arg of `display-buffer' instead." "24.1")
5772
5773 ;;;###autoload
5774 (put 'special-display-buffer-names 'risky-local-variable t)
5775
5776 (defcustom special-display-regexps nil
5777 "List of regexps saying which buffers should be displayed specially.
5778 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5779 any regexp in this list matches its name, displays it specially
5780 using `special-display-function'. `special-display-popup-frame'
5781 \(the default for `special-display-function') usually displays
5782 the buffer in a separate frame made with the parameters specified
5783 by `special-display-frame-alist'. If `special-display-function'
5784 has been set to some other function, that function is called with
5785 the buffer as first, and nil as second argument.
5786
5787 Alternatively, an element of this list can be specified as
5788 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
5789 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5790 `special-display-popup-frame' will then interpret these pairs as
5791 frame parameters when creating a special frame for a buffer whose
5792 name matches REGEXP, overriding the corresponding values from
5793 `special-display-frame-alist'.
5794
5795 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5796 `special-display-popup-frame' displays buffers matching REGEXP in
5797 the selected window. \(same-frame . t) in FRAME-PARAMETERS means
5798 to display such buffers in a window on the selected frame.
5799
5800 If `special-display-function' specifies some other function than
5801 `special-display-popup-frame', that function is called with the
5802 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
5803 as second argument.
5804
5805 Finally, an element of this list can be also specified as
5806 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
5807 will then call FUNCTION with the buffer whose name matched
5808 REGEXP as first, and OTHER-ARGS as second argument. If
5809 `special-display-function' specifies some other function, that
5810 function is called with the buffer whose name matched REGEXP
5811 as first, and the element's cdr as second argument.
5812
5813 If this variable appears \"not to work\", because you added a
5814 name to it but the corresponding buffer is displayed in the
5815 selected window, look at the values of `same-window-buffer-names'
5816 and `same-window-regexps'. Those variables take precedence over
5817 this one.
5818
5819 See also `special-display-buffer-names'."
5820 :type '(repeat
5821 (choice :tag "Buffer"
5822 :value ""
5823 (regexp :format "%v")
5824 (cons :tag "With parameters"
5825 :format "%v"
5826 :value ("" . nil)
5827 (regexp :format "%v")
5828 (repeat :tag "Parameters"
5829 (cons :format "%v"
5830 (symbol :tag "Parameter")
5831 (sexp :tag "Value"))))
5832 (list :tag "With function"
5833 :format "%v"
5834 :value ("" . nil)
5835 (regexp :format "%v")
5836 (function :tag "Function")
5837 (repeat :tag "Arguments" (sexp)))))
5838 :group 'windows
5839 :group 'frames)
5840 (make-obsolete-variable
5841 'special-display-regexps
5842 "use 2nd arg of `display-buffer' instead." "24.1")
5843
5844 (defun special-display-p (buffer-name)
5845 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
5846 More precisely, return t if `special-display-buffer-names' or
5847 `special-display-regexps' contain a string entry equaling or
5848 matching BUFFER-NAME. If `special-display-buffer-names' or
5849 `special-display-regexps' contain a list entry whose car equals
5850 or matches BUFFER-NAME, the return value is the cdr of that
5851 entry."
5852 (let ((buffer-names (with-no-warnings special-display-buffer-names))
5853 (regexps (with-no-warnings special-display-regexps))
5854 tmp)
5855 (cond
5856 ((not (stringp buffer-name)))
5857 ((member buffer-name buffer-names)
5858 t)
5859 ((setq tmp (assoc buffer-name buffer-names))
5860 (cdr tmp))
5861 ((catch 'found
5862 (dolist (regexp regexps)
5863 (cond
5864 ((stringp regexp)
5865 (when (string-match-p regexp buffer-name)
5866 (throw 'found t)))
5867 ((and (consp regexp) (stringp (car regexp))
5868 (string-match-p (car regexp) buffer-name))
5869 (throw 'found (cdr regexp))))))))))
5870 (make-obsolete
5871 'special-display-p
5872 "pass argument to buffer display function instead." "24.1")
5873
5874 (defcustom pop-up-frame-alist nil
5875 "Alist of parameters for automatically generated new frames.
5876 You can set this in your init file; for example,
5877
5878 (setq pop-up-frame-alist '((width . 80) (height . 20)))
5879
5880 If non-nil, the value you specify here is used by the default
5881 `pop-up-frame-function' for the creation of new frames.
5882
5883 Since `pop-up-frame-function' is used by `display-buffer' for
5884 making new frames, any value specified here by default affects
5885 the automatic generation of new frames via `display-buffer' and
5886 all functions based on it. The behavior of `make-frame' is not
5887 affected by this variable."
5888 :type '(repeat (cons :format "%v"
5889 (symbol :tag "Parameter")
5890 (sexp :tag "Value")))
5891 :group 'frames)
5892 (make-obsolete-variable
5893 'pop-up-frame-alist
5894 "use 2nd arg of `display-buffer' instead." "24.1")
5895
5896 (defcustom pop-up-frame-function
5897 (lambda () (make-frame pop-up-frame-alist))
5898 "Function used by `display-buffer' for creating a new frame.
5899 This function is called with no arguments and should return a new
5900 frame. The default value calls `make-frame' with the argument
5901 `pop-up-frame-alist'."
5902 :type 'function
5903 :group 'frames)
5904 (make-obsolete-variable
5905 'pop-up-frame-function
5906 "use 2nd arg of `display-buffer' instead." "24.1")
5907
5908 (defcustom pop-up-frames nil
5909 "Whether `display-buffer' should make a separate frame.
5910 If nil, never make a separate frame.
5911 If the value is `graphic-only', make a separate frame
5912 on graphic displays only.
5913 Any other non-nil value means always make a separate frame."
5914 :type '(choice
5915 (const :tag "Never" nil)
5916 (const :tag "On graphic displays only" graphic-only)
5917 (const :tag "Always" t))
5918 :group 'windows
5919 :group 'frames)
5920 (make-obsolete-variable
5921 'pop-up-frames
5922 "use 2nd arg of `display-buffer' instead." "24.1")
5923
5924 (defcustom display-buffer-reuse-frames nil
5925 "Set and non-nil means `display-buffer' should reuse frames.
5926 If the buffer in question is already displayed in a frame, raise
5927 that frame."
5928 :type 'boolean
5929 :version "21.1"
5930 :group 'windows
5931 :group 'frames)
5932 (make-obsolete-variable
5933 'display-buffer-reuse-frames
5934 "use 2nd arg of `display-buffer' instead." "24.1")
5935
5936 (defcustom pop-up-windows 'unset ; t
5937 "Set and non-nil means `display-buffer' should make a new window."
5938 :type 'boolean
5939 :version "24.1"
5940 :group 'windows)
5941 (make-obsolete-variable
5942 'pop-up-windows
5943 "use 2nd arg of `display-buffer' instead." "24.1")
5944
5945 (defcustom split-window-preferred-function 'split-window-sensibly
5946 "Function called by `display-buffer' to split a window.
5947 This function is called with a window as single argument and is
5948 supposed to split that window and return the new window. If the
5949 window can (or shall) not be split, it is supposed to return nil.
5950
5951 The default is to call the function `split-window-sensibly' which
5952 tries to split the window in a way which seems most suitable.
5953 You can customize the options `split-height-threshold' and/or
5954 `split-width-threshold' in order to have `split-window-sensibly'
5955 prefer either vertical or horizontal splitting.
5956
5957 If you set this to any other function, bear in mind that
5958 `display-buffer' may call that function repeatedly; the option
5959 `pop-up-windows' controls which windows may become the argument
5960 of this function.
5961
5962 The window selected at the time `display-buffer' was invoked is
5963 still selected when this function is called. Hence you can
5964 compare the window argument with the value of `selected-window'
5965 if you intend to split the selected window instead or if you do
5966 not want to split the selected window."
5967 :type 'function
5968 :version "23.1"
5969 :group 'windows)
5970 (make-obsolete-variable
5971 'split-window-preferred-function
5972 "use 2nd arg of `display-buffer' instead." "24.1")
5973
5974 (defcustom split-height-threshold 80
5975 "Minimum height for splitting a window to display a buffer.
5976 If this is an integer, `display-buffer' can split a window
5977 vertically only if it has at least this many lines. If this is
5978 nil, `display-buffer' does not split windows vertically. If a
5979 window is the only window on its frame, `display-buffer' may
5980 split it vertically disregarding the value of this variable."
5981 :type '(choice (const nil) (integer :tag "lines"))
5982 :version "23.1"
5983 :group 'windows)
5984 (make-obsolete-variable
5985 'split-height-threshold
5986 "use 2nd arg of `display-buffer' instead." "24.1")
5987
5988 (defcustom split-width-threshold 160
5989 "Minimum width for splitting a window to display a buffer.
5990 If this is an integer, `display-buffer' can split a window
5991 horizontally only if it has at least this many columns. If this
5992 is nil, `display-buffer' cannot split windows horizontally."
5993 :type '(choice (const nil) (integer :tag "columns"))
5994 :version "23.1"
5995 :group 'windows)
5996 (make-obsolete-variable
5997 'split-width-threshold
5998 "use 2nd arg of `display-buffer' instead." "24.1")
5999
6000 (defcustom even-window-heights 'unset ; t
6001 "If set and non-nil `display-buffer' will try to even window heights.
6002 Otherwise `display-buffer' will leave the window configuration
6003 alone. Heights are evened only when `display-buffer' reuses a
6004 window that appears above or below the selected window."
6005 :type 'boolean
6006 :version "24.1"
6007 :group 'windows)
6008 (make-obsolete-variable
6009 'even-window-heights
6010 "use 2nd arg of `display-buffer' instead." "24.1")
6011
6012 (defvar display-buffer-mark-dedicated nil
6013 "Non-nil means `display-buffer' marks the windows it creates as dedicated.
6014 The actual non-nil value of this variable will be copied to the
6015 `window-dedicated-p' flag.")
6016 (make-obsolete-variable
6017 'display-buffer-mark-dedicated
6018 "use 2nd arg of `display-buffer' instead." "24.1")
6019
6020 (defun window-splittable-p (window &optional horizontal)
6021 "Return non-nil if `split-window-sensibly' may split WINDOW.
6022 Optional argument HORIZONTAL nil or omitted means check whether
6023 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
6024 non-nil means check whether WINDOW may be split horizontally.
6025
6026 WINDOW may be split vertically when the following conditions
6027 hold:
6028 - `window-size-fixed' is either nil or equals `width' for the
6029 buffer of WINDOW.
6030 - `split-height-threshold' is an integer and WINDOW is at least as
6031 high as `split-height-threshold'.
6032 - When WINDOW is split evenly, the emanating windows are at least
6033 `window-min-height' lines tall and can accommodate at least one
6034 line plus - if WINDOW has one - a mode line.
6035
6036 WINDOW may be split horizontally when the following conditions
6037 hold:
6038 - `window-size-fixed' is either nil or equals `height' for the
6039 buffer of WINDOW.
6040 - `split-width-threshold' is an integer and WINDOW is at least as
6041 wide as `split-width-threshold'.
6042 - When WINDOW is split evenly, the emanating windows are at least
6043 `window-min-width' or two (whichever is larger) columns wide."
6044 (when (window-live-p window)
6045 (with-current-buffer (window-buffer window)
6046 (if horizontal
6047 ;; A window can be split horizontally when its width is not
6048 ;; fixed, it is at least `split-width-threshold' columns wide
6049 ;; and at least twice as wide as `window-min-width' and 2 (the
6050 ;; latter value is hardcoded).
6051 (and (memq window-size-fixed '(nil height))
6052 ;; Testing `window-full-width-p' here hardly makes any
6053 ;; sense nowadays. This can be done more intuitively by
6054 ;; setting up `split-width-threshold' appropriately.
6055 (numberp split-width-threshold)
6056 (>= (window-width window)
6057 (max split-width-threshold
6058 (* 2 (max window-min-width 2)))))
6059 ;; A window can be split vertically when its height is not
6060 ;; fixed, it is at least `split-height-threshold' lines high,
6061 ;; and it is at least twice as high as `window-min-height' and 2
6062 ;; if it has a modeline or 1.
6063 (and (memq window-size-fixed '(nil width))
6064 (numberp split-height-threshold)
6065 (>= (window-height window)
6066 (max split-height-threshold
6067 (* 2 (max window-min-height
6068 (if mode-line-format 2 1))))))))))
6069 (make-obsolete
6070 'window-splittable-p "use 2nd arg of `display-buffer' instead." "24.1")
6071
6072 (defun split-window-sensibly (window)
6073 "Split WINDOW in a way suitable for `display-buffer'.
6074 If `split-height-threshold' specifies an integer, WINDOW is at
6075 least `split-height-threshold' lines tall and can be split
6076 vertically, split WINDOW into two windows one above the other and
6077 return the lower window. Otherwise, if `split-width-threshold'
6078 specifies an integer, WINDOW is at least `split-width-threshold'
6079 columns wide and can be split horizontally, split WINDOW into two
6080 windows side by side and return the window on the right. If this
6081 can't be done either and WINDOW is the only window on its frame,
6082 try to split WINDOW vertically disregarding any value specified
6083 by `split-height-threshold'. If that succeeds, return the lower
6084 window. Return nil otherwise.
6085
6086 By default `display-buffer' routines call this function to split
6087 the largest or least recently used window. To change the default
6088 customize the option `split-window-preferred-function'.
6089
6090 You can enforce this function to not split WINDOW horizontally,
6091 by setting \(or binding) the variable `split-width-threshold' to
6092 nil. If, in addition, you set `split-height-threshold' to zero,
6093 chances increase that this function does split WINDOW vertically.
6094
6095 In order to not split WINDOW vertically, set \(or bind) the
6096 variable `split-height-threshold' to nil. Additionally, you can
6097 set `split-width-threshold' to zero to make a horizontal split
6098 more likely to occur.
6099
6100 Have a look at the function `window-splittable-p' if you want to
6101 know how `split-window-sensibly' determines whether WINDOW can be
6102 split."
6103 (or (and (with-no-warnings (window-splittable-p window))
6104 ;; Split window vertically.
6105 (with-selected-window window
6106 (split-window-vertically)))
6107 (and (with-no-warnings (window-splittable-p window t))
6108 ;; Split window horizontally.
6109 (with-selected-window window
6110 (split-window-horizontally)))
6111 (and (eq window (frame-root-window (window-frame window)))
6112 (not (window-minibuffer-p window))
6113 ;; If WINDOW is the only window on its frame and is not the
6114 ;; minibuffer window, try to split it vertically disregarding
6115 ;; the value of `split-height-threshold'.
6116 (let ((split-height-threshold 0))
6117 (when (with-no-warnings (window-splittable-p window))
6118 (with-selected-window window
6119 (split-window-vertically)))))))
6120 (make-obsolete
6121 'split-window-sensibly "use 2nd arg of `display-buffer' instead." "24.1")
6122
6123 ;; Functions for converting Emacs 23 buffer display options to buffer
6124 ;; display specifiers.
6125 (defun display-buffer-alist-add (identifiers specifiers &optional no-custom)
6126 "Helper function for `display-buffer-alist-set'."
6127 (unless identifiers
6128 (setq identifiers '((regexp . ".*"))))
6129 (unless (atom specifiers)
6130 (setq specifiers (delq nil specifiers)))
6131
6132 (if no-custom
6133 (setq display-buffer-alist
6134 (cons (cons identifiers specifiers) display-buffer-alist))
6135 (customize-set-variable
6136 'display-buffer-alist
6137 (cons (cons identifiers specifiers) display-buffer-alist))))
6138
6139 (defun display-buffer-alist-set (&optional no-custom add)
6140 "Set `display-buffer-alist' from Emacs 23 buffer display options.
6141 Optional argument NO-CUSTOM nil means use `customize-set-variable'
6142 to set the value of `display-buffer-alist'. NO-CUSTOM non-nil
6143 means to use `setq' instead.
6144
6145 Optional argument ADD nil means to replace the actual value of
6146 `display-buffer-alist' with the value calculated here. ADD
6147 non-nil means prepend the value calculated here to the current
6148 value of `display-buffer-alist'."
6149 (unless add
6150 (if no-custom
6151 (setq display-buffer-alist nil)
6152 (customize-set-variable 'display-buffer-alist nil)))
6153
6154 ;; Disable warnings, there are too many obsolete options here.
6155 (with-no-warnings
6156 ;; `pop-up-windows'
6157 (display-buffer-alist-add
6158 nil
6159 (let ((fun (unless (eq split-window-preferred-function
6160 'split-window-sensibly)
6161 ;; `split-window-sensibly' has been merged into the
6162 ;; `display-buffer-split-window' code as `nil'.
6163 split-window-preferred-function))
6164 (min-height
6165 (if (numberp split-height-threshold)
6166 (/ split-height-threshold 2)
6167 ;; Undocumented hack.
6168 1.0))
6169 (min-width
6170 (if (numberp split-width-threshold)
6171 (/ split-width-threshold 2)
6172 ;; Undocumented hack.
6173 1.0)))
6174 (list
6175 'pop-up-window
6176 (when pop-up-windows ; unset qualifies as t
6177 (list
6178 'pop-up-window
6179 (cons 'largest fun)
6180 (cons 'lru fun)))
6181 (cons 'pop-up-window-min-height min-height)
6182 (cons 'pop-up-window-min-width min-width)))
6183 no-custom)
6184
6185 ;; `pop-up-frames'
6186 (display-buffer-alist-add
6187 nil
6188 (list
6189 'pop-up-frame
6190 (when pop-up-frames
6191 (list 'pop-up-frame pop-up-frames))
6192 (when pop-up-frame-function
6193 (cons 'pop-up-frame-function pop-up-frame-function))
6194 (when pop-up-frame-alist
6195 (cons 'pop-up-frame-alist pop-up-frame-alist)))
6196 no-custom)
6197
6198 ;; `special-display-regexps'
6199 (dolist (entry special-display-regexps)
6200 (cond
6201 ((stringp entry)
6202 ;; Plain string.
6203 (display-buffer-alist-add
6204 `((regexp . ,entry))
6205 (list
6206 'fun-with-args
6207 (list 'fun-with-args special-display-function
6208 special-display-frame-alist))
6209 no-custom))
6210 ((consp entry)
6211 (let ((name (car entry))
6212 (rest (cdr entry)))
6213 (cond
6214 ((functionp (car rest))
6215 ;; A function.
6216 (display-buffer-alist-add
6217 `((name . ,name))
6218 (list
6219 'fun-with-args
6220 ;; Weary.
6221 (list 'fun-with-args (car rest) (cadr rest)))
6222 no-custom))
6223 ((listp rest)
6224 ;; A list of parameters.
6225 (cond
6226 ((assq 'same-window rest)
6227 (display-buffer-alist-add
6228 `((name . ,name))
6229 (list 'reuse-window
6230 (list 'reuse-window 'same)
6231 (list 'reuse-window-dedicated 'weak))
6232 no-custom))
6233 ((assq 'same-frame rest)
6234 (display-buffer-alist-add
6235 `((name . ,name)) (list 'same-frame) no-custom))
6236 (t
6237 (display-buffer-alist-add
6238 `((name . ,name))
6239 (list
6240 'fun-with-args
6241 (list 'fun-with-args special-display-function
6242 special-display-frame-alist))
6243 no-custom)))))))))
6244
6245 ;; `special-display-buffer-names'
6246 (dolist (entry special-display-buffer-names)
6247 (cond
6248 ((stringp entry)
6249 ;; Plain string.
6250 (display-buffer-alist-add
6251 `((name . ,entry))
6252 (list
6253 'fun-with-args
6254 (list 'fun-with-args special-display-function
6255 special-display-frame-alist))
6256 no-custom))
6257 ((consp entry)
6258 (let ((name (car entry))
6259 (rest (cdr entry)))
6260 (cond
6261 ((functionp (car rest))
6262 ;; A function.
6263 (display-buffer-alist-add
6264 `((name . ,name))
6265 (list
6266 'fun-with-args
6267 ;; Weary.
6268 (list 'fun-with-args (car rest) (cadr rest)))
6269 no-custom))
6270 ((listp rest)
6271 ;; A list of parameters.
6272 (cond
6273 ((assq 'same-window rest)
6274 (display-buffer-alist-add
6275 `((name . ,name))
6276 (list 'reuse-window
6277 (list 'reuse-window 'same)
6278 (list 'reuse-window-dedicated 'weak))
6279 no-custom))
6280 ((assq 'same-frame rest)
6281 (display-buffer-alist-add
6282 `((name . ,name)) (list 'same-frame) no-custom))
6283 (t
6284 (display-buffer-alist-add
6285 `((name . ,name))
6286 (list
6287 'fun-with-args
6288 (list 'fun-with-args special-display-function
6289 special-display-frame-alist))
6290 no-custom)))))))))
6291
6292 ;; `same-window-regexps'
6293 (dolist (entry same-window-regexps)
6294 (cond
6295 ((stringp entry)
6296 (display-buffer-alist-add
6297 `((regexp . ,entry))
6298 (list 'reuse-window (list 'reuse-window 'same))
6299 no-custom))
6300 ((consp entry)
6301 (display-buffer-alist-add
6302 `((regexp . ,(car entry)))
6303 (list 'reuse-window (list 'reuse-window 'same))
6304 no-custom))))
6305
6306 ;; `same-window-buffer-names'
6307 (dolist (entry same-window-buffer-names)
6308 (cond
6309 ((stringp entry)
6310 (display-buffer-alist-add
6311 `((name . ,entry))
6312 (list 'reuse-window (list 'reuse-window 'same))
6313 no-custom))
6314 ((consp entry)
6315 (display-buffer-alist-add
6316 `((name . ,(car entry)))
6317 (list 'reuse-window (list 'reuse-window 'same))
6318 no-custom))))
6319
6320 ;; `reuse-window'
6321 (display-buffer-alist-add
6322 nil
6323 (list
6324 'reuse-window
6325 (list 'reuse-window nil 'same
6326 (when (or display-buffer-reuse-frames pop-up-frames)
6327 ;; "0" (all visible and iconified frames) is hardcoded in
6328 ;; Emacs 23.
6329 0))
6330 (unless (memq even-window-heights '(nil unset))
6331 (cons 'reuse-window-even-sizes t)))
6332 no-custom)
6333
6334 ;; `display-buffer-mark-dedicated'
6335 (when display-buffer-mark-dedicated
6336 (display-buffer-alist-add
6337 nil
6338 (list
6339 (cons 'dedicated display-buffer-mark-dedicated))
6340 no-custom)))
6341
6342 display-buffer-alist)
6343 \f
6344 (defun set-window-text-height (window height)
6345 "Set the height in lines of the text display area of WINDOW to HEIGHT.
6346 WINDOW must be a live window. HEIGHT doesn't include the mode
6347 line or header line, if any, or any partial-height lines in the
6348 text display area.
6349
6350 Note that the current implementation of this function cannot
6351 always set the height exactly, but attempts to be conservative,
6352 by allocating more lines than are actually needed in the case
6353 where some error may be present."
6354 (setq window (normalize-live-window window))
6355 (let ((delta (- height (window-text-height window))))
6356 (unless (zerop delta)
6357 ;; Setting window-min-height to a value like 1 can lead to very
6358 ;; bizarre displays because it also allows Emacs to make *other*
6359 ;; windows 1-line tall, which means that there's no more space for
6360 ;; the modeline.
6361 (let ((window-min-height (min 2 height))) ; One text line plus a modeline.
6362 (resize-window window delta)))))
6363
6364 (defun enlarge-window-horizontally (delta)
6365 "Make selected window DELTA columns wider.
6366 Interactively, if no argument is given, make selected window one
6367 column wider."
6368 (interactive "p")
6369 (enlarge-window delta t))
6370
6371 (defun shrink-window-horizontally (delta)
6372 "Make selected window DELTA columns narrower.
6373 Interactively, if no argument is given, make selected window one
6374 column narrower."
6375 (interactive "p")
6376 (shrink-window delta t))
6377
6378 (defun count-screen-lines (&optional beg end count-final-newline window)
6379 "Return the number of screen lines in the region.
6380 The number of screen lines may be different from the number of actual lines,
6381 due to line breaking, display table, etc.
6382
6383 Optional arguments BEG and END default to `point-min' and `point-max'
6384 respectively.
6385
6386 If region ends with a newline, ignore it unless optional third argument
6387 COUNT-FINAL-NEWLINE is non-nil.
6388
6389 The optional fourth argument WINDOW specifies the window used for obtaining
6390 parameters such as width, horizontal scrolling, and so on. The default is
6391 to use the selected window's parameters.
6392
6393 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6394 regardless of which buffer is displayed in WINDOW. This makes possible to use
6395 `count-screen-lines' in any buffer, whether or not it is currently displayed
6396 in some window."
6397 (unless beg
6398 (setq beg (point-min)))
6399 (unless end
6400 (setq end (point-max)))
6401 (if (= beg end)
6402 0
6403 (save-excursion
6404 (save-restriction
6405 (widen)
6406 (narrow-to-region (min beg end)
6407 (if (and (not count-final-newline)
6408 (= ?\n (char-before (max beg end))))
6409 (1- (max beg end))
6410 (max beg end)))
6411 (goto-char (point-min))
6412 (1+ (vertical-motion (buffer-size) window))))))
6413
6414 (defun window-buffer-height (window)
6415 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
6416 (with-current-buffer (window-buffer window)
6417 (max 1
6418 (count-screen-lines (point-min) (point-max)
6419 ;; If buffer ends with a newline, ignore it when
6420 ;; counting height unless point is after it.
6421 (eobp)
6422 window))))
6423
6424 ;;; Resizing buffers to fit their contents exactly.
6425 (defun fit-window-to-buffer (&optional window max-height min-height override)
6426 "Adjust height of WINDOW to display its buffer's contents exactly.
6427 WINDOW can be any live window and defaults to the selected one.
6428
6429 Optional argument MAX-HEIGHT specifies the maximum height of
6430 WINDOW and defaults to the height of WINDOW's frame. Optional
6431 argument MIN-HEIGHT specifies the minimum height of WINDOW and
6432 defaults to `window-min-height'. Both, MAX-HEIGHT and MIN-HEIGHT
6433 are specified in lines and include the mode line and header line,
6434 if any.
6435
6436 Optional argument OVERRIDE non-nil means override restrictions
6437 imposed by `window-min-height' and `window-min-width' on the size
6438 of WINDOW.
6439
6440 Return the number of lines by which WINDOW was enlarged or
6441 shrunk. If an error occurs during resizing, return nil but don't
6442 signal an error.
6443
6444 Note that even if this function makes WINDOW large enough to show
6445 _all_ lines of its buffer you might not see the first lines when
6446 WINDOW was scrolled."
6447 (interactive)
6448 ;; Do all the work in WINDOW and its buffer and restore the selected
6449 ;; window and the current buffer when we're done.
6450 (setq window (normalize-live-window window))
6451 ;; Can't resize a full height or fixed-size window.
6452 (unless (or (window-size-fixed-p window)
6453 (window-full-height-p window))
6454 ;; `with-selected-window' should orderly restore the current buffer.
6455 (with-selected-window window
6456 ;; We are in WINDOW's buffer now.
6457 (let* ( ;; Adjust MIN-HEIGHT.
6458 (min-height
6459 (if override
6460 (window-min-size window nil window)
6461 (max (or min-height window-min-height)
6462 window-safe-min-height)))
6463 (max-window-height
6464 (window-total-size (frame-root-window window)))
6465 ;; Adjust MAX-HEIGHT.
6466 (max-height
6467 (if (or override (not max-height))
6468 max-window-height
6469 (min max-height max-window-height)))
6470 ;; Make `desired-height' the height necessary to show
6471 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
6472 ;; and MAX-HEIGHT.
6473 (desired-height
6474 (max
6475 (min
6476 (+ (count-screen-lines)
6477 ;; For non-minibuffers count the mode line, if any.
6478 (if (and (not (window-minibuffer-p window))
6479 mode-line-format)
6480 1
6481 0)
6482 ;; Count the header line, if any.
6483 (if header-line-format 1 0))
6484 max-height)
6485 min-height))
6486 (desired-delta
6487 (- desired-height (window-total-size window)))
6488 (delta
6489 (if (> desired-delta 0)
6490 (min desired-delta
6491 (window-max-delta window nil window))
6492 (max desired-delta
6493 (- (window-min-delta window nil window))))))
6494 ;; This `condition-case' shouldn't be necessary, but who knows?
6495 (condition-case nil
6496 (if (zerop delta)
6497 ;; Return zero if DELTA became zero in the proces.
6498 0
6499 ;; Don't try to redisplay with the cursor at the end on its
6500 ;; own line--that would force a scroll and spoil things.
6501 (when (and (eobp) (bolp) (not (bobp)))
6502 ;; It's silly to put `point' at the end of the previous
6503 ;; line and so maybe force horizontal scrolling.
6504 (set-window-point window (line-beginning-position 0)))
6505 ;; Call `resize-window' with OVERRIDE argument equal WINDOW.
6506 (resize-window window delta nil window)
6507 ;; Check if the last line is surely fully visible. If
6508 ;; not, enlarge the window.
6509 (let ((end (save-excursion
6510 (goto-char (point-max))
6511 (when (and (bolp) (not (bobp)))
6512 ;; Don't include final newline.
6513 (backward-char 1))
6514 (when truncate-lines
6515 ;; If line-wrapping is turned off, test the
6516 ;; beginning of the last line for
6517 ;; visibility instead of the end, as the
6518 ;; end of the line could be invisible by
6519 ;; virtue of extending past the edge of the
6520 ;; window.
6521 (forward-line 0))
6522 (point))))
6523 (set-window-vscroll window 0)
6524 ;; This loop might in some rare pathological cases raise
6525 ;; an error - another reason for the `condition-case'.
6526 (while (and (< desired-height max-height)
6527 (= desired-height (window-total-size))
6528 (not (pos-visible-in-window-p end)))
6529 (resize-window window 1 nil window)
6530 (setq desired-height (1+ desired-height)))))
6531 (error (setq delta nil)))
6532 delta))))
6533
6534 (defun window-safely-shrinkable-p (&optional window)
6535 "Return t if WINDOW can be shrunk without shrinking other windows.
6536 WINDOW defaults to the selected window."
6537 (with-selected-window (or window (selected-window))
6538 (let ((edges (window-edges)))
6539 ;; The following doesn't satisfy the doc-string's claim when
6540 ;; window and previous-/next-window are not part of the same
6541 ;; combination but still share a common edge. Using
6542 ;; `window-iso-combined-p' instead should handle that.
6543 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
6544 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
6545 (make-obsolete
6546 'window-safely-shrinkable-p "use `window-iso-combined-p' instead." "24.1")
6547
6548 (defun shrink-window-if-larger-than-buffer (&optional window)
6549 "Shrink height of WINDOW if its buffer doesn't need so many lines.
6550 More precisely, shrink WINDOW vertically to be as small as
6551 possible, while still showing the full contents of its buffer.
6552 WINDOW defaults to the selected window.
6553
6554 Do not shrink WINDOW to less than `window-min-height' lines. Do
6555 nothing if the buffer contains more lines than the present window
6556 height, or if some of the window's contents are scrolled out of
6557 view, or if shrinking this window would also shrink another
6558 window, or if the window is the only window of its frame.
6559
6560 Return non-nil if the window was shrunk, nil otherwise."
6561 (interactive)
6562 (setq window (normalize-live-window window))
6563 ;; Make sure that WINDOW is vertically combined and `point-min' is
6564 ;; visible (for whatever reason that's needed). The remaining issues
6565 ;; should be taken care of by `fit-window-to-buffer'.
6566 (when (and (window-iso-combined-p window)
6567 (pos-visible-in-window-p (point-min) window))
6568 (fit-window-to-buffer window (window-total-size window))))
6569 \f
6570 (defun kill-buffer-and-window ()
6571 "Kill the current buffer and delete the selected window."
6572 (interactive)
6573 (let ((window-to-delete (selected-window))
6574 (buffer-to-kill (current-buffer))
6575 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
6576 (unwind-protect
6577 (progn
6578 (add-hook 'kill-buffer-hook delete-window-hook t t)
6579 (if (kill-buffer (current-buffer))
6580 ;; If `delete-window' failed before, we rerun it to regenerate
6581 ;; the error so it can be seen in the echo area.
6582 (when (eq (selected-window) window-to-delete)
6583 (delete-window))))
6584 ;; If the buffer is not dead for some reason (probably because
6585 ;; of a `quit' signal), remove the hook again.
6586 (ignore-errors
6587 (with-current-buffer buffer-to-kill
6588 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
6589
6590 (defun quit-window (&optional kill window)
6591 "Quit WINDOW and bury its buffer.
6592 With a prefix argument, kill the buffer instead. WINDOW defaults
6593 to the selected window.
6594
6595 If WINDOW is non-nil, dedicated, or a minibuffer window, delete
6596 it and, if it's alone on its frame, its frame too. Otherwise, or
6597 if deleting WINDOW fails in any of the preceding cases, display
6598 another buffer in WINDOW using `switch-to-buffer'.
6599
6600 Optional argument KILL non-nil means kill WINDOW's buffer.
6601 Otherwise, bury WINDOW's buffer, see `bury-buffer'."
6602 (interactive "P")
6603 (let ((buffer (window-buffer window)))
6604 (if (or window
6605 (window-minibuffer-p window)
6606 (window-dedicated-p window))
6607 ;; WINDOW is either non-nil, a minibuffer window, or dedicated;
6608 ;; try to delete it.
6609 (let* ((window (or window (selected-window)))
6610 (frame (window-frame window)))
6611 (if (frame-root-window-p window)
6612 ;; WINDOW is alone on its frame.
6613 (delete-frame frame)
6614 ;; There are other windows on its frame, delete WINDOW.
6615 (delete-window window)))
6616 ;; Otherwise, switch to another buffer in the selected window.
6617 (switch-to-buffer nil))
6618
6619 ;; Deal with the buffer.
6620 (if kill
6621 (kill-buffer buffer)
6622 (bury-buffer buffer))))
6623 \f
6624 (defvar recenter-last-op nil
6625 "Indicates the last recenter operation performed.
6626 Possible values: `top', `middle', `bottom', integer or float numbers.")
6627
6628 (defcustom recenter-positions '(middle top bottom)
6629 "Cycling order for `recenter-top-bottom'.
6630 A list of elements with possible values `top', `middle', `bottom',
6631 integer or float numbers that define the cycling order for
6632 the command `recenter-top-bottom'.
6633
6634 Top and bottom destinations are `scroll-margin' lines the from true
6635 window top and bottom. Middle redraws the frame and centers point
6636 vertically within the window. Integer number moves current line to
6637 the specified absolute window-line. Float number between 0.0 and 1.0
6638 means the percentage of the screen space from the top. The default
6639 cycling order is middle -> top -> bottom."
6640 :type '(repeat (choice
6641 (const :tag "Top" top)
6642 (const :tag "Middle" middle)
6643 (const :tag "Bottom" bottom)
6644 (integer :tag "Line number")
6645 (float :tag "Percentage")))
6646 :version "23.2"
6647 :group 'windows)
6648
6649 (defun recenter-top-bottom (&optional arg)
6650 "Move current buffer line to the specified window line.
6651 With no prefix argument, successive calls place point according
6652 to the cycling order defined by `recenter-positions'.
6653
6654 A prefix argument is handled like `recenter':
6655 With numeric prefix ARG, move current line to window-line ARG.
6656 With plain `C-u', move current line to window center."
6657 (interactive "P")
6658 (cond
6659 (arg (recenter arg)) ; Always respect ARG.
6660 (t
6661 (setq recenter-last-op
6662 (if (eq this-command last-command)
6663 (car (or (cdr (member recenter-last-op recenter-positions))
6664 recenter-positions))
6665 (car recenter-positions)))
6666 (let ((this-scroll-margin
6667 (min (max 0 scroll-margin)
6668 (truncate (/ (window-body-height) 4.0)))))
6669 (cond ((eq recenter-last-op 'middle)
6670 (recenter))
6671 ((eq recenter-last-op 'top)
6672 (recenter this-scroll-margin))
6673 ((eq recenter-last-op 'bottom)
6674 (recenter (- -1 this-scroll-margin)))
6675 ((integerp recenter-last-op)
6676 (recenter recenter-last-op))
6677 ((floatp recenter-last-op)
6678 (recenter (round (* recenter-last-op (window-height))))))))))
6679
6680 (define-key global-map [?\C-l] 'recenter-top-bottom)
6681
6682 (defun move-to-window-line-top-bottom (&optional arg)
6683 "Position point relative to window.
6684
6685 With a prefix argument ARG, acts like `move-to-window-line'.
6686
6687 With no argument, positions point at center of window.
6688 Successive calls position point at positions defined
6689 by `recenter-positions'."
6690 (interactive "P")
6691 (cond
6692 (arg (move-to-window-line arg)) ; Always respect ARG.
6693 (t
6694 (setq recenter-last-op
6695 (if (eq this-command last-command)
6696 (car (or (cdr (member recenter-last-op recenter-positions))
6697 recenter-positions))
6698 (car recenter-positions)))
6699 (let ((this-scroll-margin
6700 (min (max 0 scroll-margin)
6701 (truncate (/ (window-body-height) 4.0)))))
6702 (cond ((eq recenter-last-op 'middle)
6703 (call-interactively 'move-to-window-line))
6704 ((eq recenter-last-op 'top)
6705 (move-to-window-line this-scroll-margin))
6706 ((eq recenter-last-op 'bottom)
6707 (move-to-window-line (- -1 this-scroll-margin)))
6708 ((integerp recenter-last-op)
6709 (move-to-window-line recenter-last-op))
6710 ((floatp recenter-last-op)
6711 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
6712
6713 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
6714 \f
6715 ;;; Scrolling commands.
6716
6717 ;;; Scrolling commands which does not signal errors at top/bottom
6718 ;;; of buffer at first key-press (instead moves to top/bottom
6719 ;;; of buffer).
6720
6721 (defcustom scroll-error-top-bottom nil
6722 "Move point to top/bottom of buffer before signalling a scrolling error.
6723 A value of nil means just signal an error if no more scrolling possible.
6724 A value of t means point moves to the beginning or the end of the buffer
6725 \(depending on scrolling direction) when no more scrolling possible.
6726 When point is already on that position, then signal an error."
6727 :type 'boolean
6728 :group 'scrolling
6729 :version "24.1")
6730
6731 (defun scroll-up-command (&optional arg)
6732 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
6733 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
6734 scroll window further, move cursor to the bottom line.
6735 When point is already on that position, then signal an error.
6736 A near full screen is `next-screen-context-lines' less than a full screen.
6737 Negative ARG means scroll downward.
6738 If ARG is the atom `-', scroll downward by nearly full screen."
6739 (interactive "^P")
6740 (cond
6741 ((null scroll-error-top-bottom)
6742 (scroll-up arg))
6743 ((eq arg '-)
6744 (scroll-down-command nil))
6745 ((< (prefix-numeric-value arg) 0)
6746 (scroll-down-command (- (prefix-numeric-value arg))))
6747 ((eobp)
6748 (scroll-up arg)) ; signal error
6749 (t
6750 (condition-case nil
6751 (scroll-up arg)
6752 (end-of-buffer
6753 (if arg
6754 ;; When scrolling by ARG lines can't be done,
6755 ;; move by ARG lines instead.
6756 (forward-line arg)
6757 ;; When ARG is nil for full-screen scrolling,
6758 ;; move to the bottom of the buffer.
6759 (goto-char (point-max))))))))
6760
6761 (put 'scroll-up-command 'scroll-command t)
6762
6763 (defun scroll-down-command (&optional arg)
6764 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
6765 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
6766 scroll window further, move cursor to the top line.
6767 When point is already on that position, then signal an error.
6768 A near full screen is `next-screen-context-lines' less than a full screen.
6769 Negative ARG means scroll upward.
6770 If ARG is the atom `-', scroll upward by nearly full screen."
6771 (interactive "^P")
6772 (cond
6773 ((null scroll-error-top-bottom)
6774 (scroll-down arg))
6775 ((eq arg '-)
6776 (scroll-up-command nil))
6777 ((< (prefix-numeric-value arg) 0)
6778 (scroll-up-command (- (prefix-numeric-value arg))))
6779 ((bobp)
6780 (scroll-down arg)) ; signal error
6781 (t
6782 (condition-case nil
6783 (scroll-down arg)
6784 (beginning-of-buffer
6785 (if arg
6786 ;; When scrolling by ARG lines can't be done,
6787 ;; move by ARG lines instead.
6788 (forward-line (- arg))
6789 ;; When ARG is nil for full-screen scrolling,
6790 ;; move to the top of the buffer.
6791 (goto-char (point-min))))))))
6792
6793 (put 'scroll-down-command 'scroll-command t)
6794
6795 ;;; Scrolling commands which scroll a line instead of full screen.
6796
6797 (defun scroll-up-line (&optional arg)
6798 "Scroll text of selected window upward ARG lines; or one line if no ARG.
6799 If ARG is omitted or nil, scroll upward by one line.
6800 This is different from `scroll-up-command' that scrolls a full screen."
6801 (interactive "p")
6802 (scroll-up (or arg 1)))
6803
6804 (put 'scroll-up-line 'scroll-command t)
6805
6806 (defun scroll-down-line (&optional arg)
6807 "Scroll text of selected window down ARG lines; or one line if no ARG.
6808 If ARG is omitted or nil, scroll down by one line.
6809 This is different from `scroll-down-command' that scrolls a full screen."
6810 (interactive "p")
6811 (scroll-down (or arg 1)))
6812
6813 (put 'scroll-down-line 'scroll-command t)
6814
6815 \f
6816 (defun scroll-other-window-down (lines)
6817 "Scroll the \"other window\" down.
6818 For more details, see the documentation for `scroll-other-window'."
6819 (interactive "P")
6820 (scroll-other-window
6821 ;; Just invert the argument's meaning.
6822 ;; We can do that without knowing which window it will be.
6823 (if (eq lines '-) nil
6824 (if (null lines) '-
6825 (- (prefix-numeric-value lines))))))
6826
6827 (defun beginning-of-buffer-other-window (arg)
6828 "Move point to the beginning of the buffer in the other window.
6829 Leave mark at previous position.
6830 With arg N, put point N/10 of the way from the true beginning."
6831 (interactive "P")
6832 (let ((orig-window (selected-window))
6833 (window (other-window-for-scrolling)))
6834 ;; We use unwind-protect rather than save-window-excursion
6835 ;; because the latter would preserve the things we want to change.
6836 (unwind-protect
6837 (progn
6838 (select-window window)
6839 ;; Set point and mark in that window's buffer.
6840 (with-no-warnings
6841 (beginning-of-buffer arg))
6842 ;; Set point accordingly.
6843 (recenter '(t)))
6844 (select-window orig-window))))
6845
6846 (defun end-of-buffer-other-window (arg)
6847 "Move point to the end of the buffer in the other window.
6848 Leave mark at previous position.
6849 With arg N, put point N/10 of the way from the true end."
6850 (interactive "P")
6851 ;; See beginning-of-buffer-other-window for comments.
6852 (let ((orig-window (selected-window))
6853 (window (other-window-for-scrolling)))
6854 (unwind-protect
6855 (progn
6856 (select-window window)
6857 (with-no-warnings
6858 (end-of-buffer arg))
6859 (recenter '(t)))
6860 (select-window orig-window))))
6861 \f
6862 (defvar mouse-autoselect-window-timer nil
6863 "Timer used by delayed window autoselection.")
6864
6865 (defvar mouse-autoselect-window-position nil
6866 "Last mouse position recorded by delayed window autoselection.")
6867
6868 (defvar mouse-autoselect-window-window nil
6869 "Last window recorded by delayed window autoselection.")
6870
6871 (defvar mouse-autoselect-window-state nil
6872 "When non-nil, special state of delayed window autoselection.
6873 Possible values are `suspend' \(suspend autoselection after a menu or
6874 scrollbar interaction\) and `select' \(the next invocation of
6875 'handle-select-window' shall select the window immediately\).")
6876
6877 (defun mouse-autoselect-window-cancel (&optional force)
6878 "Cancel delayed window autoselection.
6879 Optional argument FORCE means cancel unconditionally."
6880 (unless (and (not force)
6881 ;; Don't cancel for select-window or select-frame events
6882 ;; or when the user drags a scroll bar.
6883 (or (memq this-command
6884 '(handle-select-window handle-switch-frame))
6885 (and (eq this-command 'scroll-bar-toolkit-scroll)
6886 (memq (nth 4 (event-end last-input-event))
6887 '(handle end-scroll)))))
6888 (setq mouse-autoselect-window-state nil)
6889 (when (timerp mouse-autoselect-window-timer)
6890 (cancel-timer mouse-autoselect-window-timer))
6891 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
6892
6893 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
6894 "Start delayed window autoselection.
6895 MOUSE-POSITION is the last position where the mouse was seen as returned
6896 by `mouse-position'. Optional argument WINDOW non-nil denotes the
6897 window where the mouse was seen. Optional argument SUSPEND non-nil
6898 means suspend autoselection."
6899 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
6900 (setq mouse-autoselect-window-position mouse-position)
6901 (when window (setq mouse-autoselect-window-window window))
6902 (setq mouse-autoselect-window-state (when suspend 'suspend))
6903 ;; Install timer which runs `mouse-autoselect-window-select' after
6904 ;; `mouse-autoselect-window' seconds.
6905 (setq mouse-autoselect-window-timer
6906 (run-at-time
6907 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
6908
6909 (defun mouse-autoselect-window-select ()
6910 "Select window with delayed window autoselection.
6911 If the mouse position has stabilized in a non-selected window, select
6912 that window. The minibuffer window is selected only if the minibuffer is
6913 active. This function is run by `mouse-autoselect-window-timer'."
6914 (ignore-errors
6915 (let* ((mouse-position (mouse-position))
6916 (window
6917 (ignore-errors
6918 (window-at (cadr mouse-position) (cddr mouse-position)
6919 (car mouse-position)))))
6920 (cond
6921 ((or (menu-or-popup-active-p)
6922 (and window
6923 (not (coordinates-in-window-p (cdr mouse-position) window))))
6924 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
6925 ;; of WINDOW, temporarily suspend delayed autoselection.
6926 (mouse-autoselect-window-start mouse-position nil t))
6927 ((eq mouse-autoselect-window-state 'suspend)
6928 ;; Delayed autoselection was temporarily suspended, reenable it.
6929 (mouse-autoselect-window-start mouse-position))
6930 ((and window (not (eq window (selected-window)))
6931 (or (not (numberp mouse-autoselect-window))
6932 (and (> mouse-autoselect-window 0)
6933 ;; If `mouse-autoselect-window' is positive, select
6934 ;; window if the window is the same as before.
6935 (eq window mouse-autoselect-window-window))
6936 ;; Otherwise select window if the mouse is at the same
6937 ;; position as before. Observe that the first test after
6938 ;; starting autoselection usually fails since the value of
6939 ;; `mouse-autoselect-window-position' recorded there is the
6940 ;; position where the mouse has entered the new window and
6941 ;; not necessarily where the mouse has stopped moving.
6942 (equal mouse-position mouse-autoselect-window-position))
6943 ;; The minibuffer is a candidate window if it's active.
6944 (or (not (window-minibuffer-p window))
6945 (eq window (active-minibuffer-window))))
6946 ;; Mouse position has stabilized in non-selected window: Cancel
6947 ;; delayed autoselection and try to select that window.
6948 (mouse-autoselect-window-cancel t)
6949 ;; Select window where mouse appears unless the selected window is the
6950 ;; minibuffer. Use `unread-command-events' in order to execute pre-
6951 ;; and post-command hooks and trigger idle timers. To avoid delaying
6952 ;; autoselection again, set `mouse-autoselect-window-state'."
6953 (unless (window-minibuffer-p (selected-window))
6954 (setq mouse-autoselect-window-state 'select)
6955 (setq unread-command-events
6956 (cons (list 'select-window (list window))
6957 unread-command-events))))
6958 ((or (and window (eq window (selected-window)))
6959 (not (numberp mouse-autoselect-window))
6960 (equal mouse-position mouse-autoselect-window-position))
6961 ;; Mouse position has either stabilized in the selected window or at
6962 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
6963 (mouse-autoselect-window-cancel t))
6964 (t
6965 ;; Mouse position has not stabilized yet, resume delayed
6966 ;; autoselection.
6967 (mouse-autoselect-window-start mouse-position window))))))
6968
6969 (defun handle-select-window (event)
6970 "Handle select-window events."
6971 (interactive "e")
6972 (let ((window (posn-window (event-start event))))
6973 (unless (or (not (window-live-p window))
6974 ;; Don't switch if we're currently in the minibuffer.
6975 ;; This tries to work around problems where the
6976 ;; minibuffer gets unselected unexpectedly, and where
6977 ;; you then have to move your mouse all the way down to
6978 ;; the minibuffer to select it.
6979 (window-minibuffer-p (selected-window))
6980 ;; Don't switch to minibuffer window unless it's active.
6981 (and (window-minibuffer-p window)
6982 (not (minibuffer-window-active-p window)))
6983 ;; Don't switch when autoselection shall be delayed.
6984 (and (numberp mouse-autoselect-window)
6985 (not (zerop mouse-autoselect-window))
6986 (not (eq mouse-autoselect-window-state 'select))
6987 (progn
6988 ;; Cancel any delayed autoselection.
6989 (mouse-autoselect-window-cancel t)
6990 ;; Start delayed autoselection from current mouse
6991 ;; position and window.
6992 (mouse-autoselect-window-start (mouse-position) window)
6993 ;; Executing a command cancels delayed autoselection.
6994 (add-hook
6995 'pre-command-hook 'mouse-autoselect-window-cancel))))
6996 (when mouse-autoselect-window
6997 ;; Reset state of delayed autoselection.
6998 (setq mouse-autoselect-window-state nil)
6999 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
7000 (run-hooks 'mouse-leave-buffer-hook))
7001 (select-window window))))
7002
7003 (defun truncated-partial-width-window-p (&optional window)
7004 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
7005 WINDOW defaults to the selected window.
7006 Return nil if WINDOW is not a partial-width window
7007 (regardless of the value of `truncate-lines').
7008 Otherwise, consult the value of `truncate-partial-width-windows'
7009 for the buffer shown in WINDOW."
7010 (unless window
7011 (setq window (selected-window)))
7012 (unless (window-full-width-p window)
7013 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
7014 (window-buffer window))))
7015 (if (integerp t-p-w-w)
7016 (< (window-width window) t-p-w-w)
7017 t-p-w-w))))
7018 \f
7019 (define-key ctl-x-map "0" 'delete-window)
7020 (define-key ctl-x-map "1" 'delete-other-windows)
7021 (define-key ctl-x-map "2" 'split-window-above-each-other)
7022 (define-key ctl-x-map "3" 'split-window-side-by-side)
7023 (define-key ctl-x-map "o" 'other-window)
7024 (define-key ctl-x-map "^" 'enlarge-window)
7025 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
7026 (define-key ctl-x-map "{" 'shrink-window-horizontally)
7027 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
7028 (define-key ctl-x-map "+" 'balance-windows)
7029 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
7030
7031 ;;; window.el ends here