Don't let display-buffer pop up new frames by default (bug#8857).
[bpt/emacs.git] / 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 (resize-window (selected-window) delta horizontal))
2048
2049 (defun shrink-window (delta &optional horizontal)
2050 "Make selected window DELTA lines smaller.
2051 Interactively, if no argument is given, make the selected window
2052 one line smaller. If optional argument HORIZONTAL is non-nil,
2053 make selected window narrower by DELTA columns. If DELTA is
2054 negative, enlarge selected window by -DELTA lines or columns.
2055 Return nil."
2056 (interactive "p")
2057 (resize-window (selected-window) (- delta) horizontal))
2058
2059 (defun maximize-window (&optional window)
2060 "Maximize WINDOW.
2061 Make WINDOW as large as possible without deleting any windows.
2062 WINDOW can be any window and defaults to the selected window."
2063 (interactive)
2064 (setq window (normalize-any-window window))
2065 (resize-window window (window-max-delta window))
2066 (resize-window window (window-max-delta window t) t))
2067
2068 (defun minimize-window (&optional window)
2069 "Minimize WINDOW.
2070 Make WINDOW as small as possible without deleting any windows.
2071 WINDOW can be any window and defaults to the selected window."
2072 (interactive)
2073 (setq window (normalize-any-window window))
2074 (resize-window window (- (window-min-delta window)))
2075 (resize-window window (- (window-min-delta window t)) t))
2076 \f
2077 (defsubst frame-root-window-p (window)
2078 "Return non-nil if WINDOW is the root window of its frame."
2079 (eq window (frame-root-window window)))
2080
2081 (defun window-tree-1 (window &optional next)
2082 "Return window tree rooted at WINDOW.
2083 Optional argument NEXT non-nil means include windows right
2084 siblings in the return value.
2085
2086 See the documentation of `window-tree' for a description of the
2087 return value."
2088 (let (list)
2089 (while window
2090 (setq list
2091 (cons
2092 (cond
2093 ((window-vchild window)
2094 (cons t (cons (window-edges window)
2095 (window-tree-1 (window-vchild window) t))))
2096 ((window-hchild window)
2097 (cons nil (cons (window-edges window)
2098 (window-tree-1 (window-hchild window) t))))
2099 (t window))
2100 list))
2101 (setq window (when next (window-next window))))
2102 (nreverse list)))
2103
2104 (defun window-tree (&optional frame)
2105 "Return the window tree of frame FRAME.
2106 FRAME must be a live frame and defaults to the selected frame.
2107 The return value is a list of the form (ROOT MINI), where ROOT
2108 represents the window tree of the frame's root window, and MINI
2109 is the frame's minibuffer window.
2110
2111 If the root window is not split, ROOT is the root window itself.
2112 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
2113 for a horizontal split, and t for a vertical split. EDGES gives
2114 the combined size and position of the subwindows in the split,
2115 and the rest of the elements are the subwindows in the split.
2116 Each of the subwindows may again be a window or a list
2117 representing a window split, and so on. EDGES is a list \(LEFT
2118 TOP RIGHT BOTTOM) as returned by `window-edges'."
2119 (setq frame (normalize-live-frame frame))
2120 (window-tree-1 (frame-root-window frame) t))
2121 \f
2122 (defun other-window (count &optional all-frames)
2123 "Select another window in cyclic ordering of windows.
2124 COUNT specifies the number of windows to skip, starting with the
2125 selected window, before making the selection. If COUNT is
2126 positive, skip COUNT windows forwards. If COUNT is negative,
2127 skip -COUNT windows backwards. COUNT zero means do not skip any
2128 window, so select the selected window. In an interactive call,
2129 COUNT is the numeric prefix argument. Return nil.
2130
2131 If the `other-window' parameter of WINDOW is a function and
2132 `ignore-window-parameters' is nil, call that function with the
2133 arguments COUNT and ALL-FRAMES.
2134
2135 This function does not select a window whose `no-other-window'
2136 window parameter is non-nil.
2137
2138 This function uses `next-window' for finding the window to
2139 select. The argument ALL-FRAMES has the same meaning as in
2140 `next-window', but the MINIBUF argument of `next-window' is
2141 always effectively nil."
2142 (interactive "p")
2143 (let* ((window (selected-window))
2144 (function (and (not ignore-window-parameters)
2145 (window-parameter window 'other-window)))
2146 old-window old-count)
2147 (if (functionp function)
2148 (funcall function count all-frames)
2149 ;; `next-window' and `previous-window' may return a window we are
2150 ;; not allowed to select. Hence we need an exit strategy in case
2151 ;; all windows are non-selectable.
2152 (catch 'exit
2153 (while (> count 0)
2154 (setq window (next-window window nil all-frames))
2155 (cond
2156 ((eq window old-window)
2157 (when (= count old-count)
2158 ;; Keep out of infinite loops. When COUNT has not changed
2159 ;; since we last looked at `window' we're probably in one.
2160 (throw 'exit nil)))
2161 ((window-parameter window 'no-other-window)
2162 (unless old-window
2163 ;; The first non-selectable window `next-window' got us:
2164 ;; Remember it and the current value of COUNT.
2165 (setq old-window window)
2166 (setq old-count count)))
2167 (t
2168 (setq count (1- count)))))
2169 (while (< count 0)
2170 (setq window (previous-window window nil all-frames))
2171 (cond
2172 ((eq window old-window)
2173 (when (= count old-count)
2174 ;; Keep out of infinite loops. When COUNT has not changed
2175 ;; since we last looked at `window' we're probably in one.
2176 (throw 'exit nil)))
2177 ((window-parameter window 'no-other-window)
2178 (unless old-window
2179 ;; The first non-selectable window `previous-window' got
2180 ;; us: Remember it and the current value of COUNT.
2181 (setq old-window window)
2182 (setq old-count count)))
2183 (t
2184 (setq count (1+ count)))))
2185
2186 (select-window window)
2187 ;; Always return nil.
2188 nil))))
2189
2190 ;; This should probably return non-nil when the selected window is part
2191 ;; of an atomic window whose root is the frame's root window.
2192 (defun one-window-p (&optional nomini all-frames)
2193 "Return non-nil if the selected window is the only window.
2194 Optional arg NOMINI non-nil means don't count the minibuffer
2195 even if it is active. Otherwise, the minibuffer is counted
2196 when it is active.
2197
2198 Optional argument ALL-FRAMES specifies the set of frames to
2199 consider, see also `next-window'. ALL-FRAMES nil or omitted
2200 means consider windows on the selected frame only, plus the
2201 minibuffer window if specified by the NOMINI argument. If the
2202 minibuffer counts, consider all windows on all frames that share
2203 that minibuffer too. The remaining non-nil values of ALL-FRAMES
2204 with a special meaning are:
2205
2206 - t means consider all windows on all existing frames.
2207
2208 - `visible' means consider all windows on all visible frames on
2209 the current terminal.
2210
2211 - 0 (the number zero) means consider all windows on all visible
2212 and iconified frames on the current terminal.
2213
2214 - A frame means consider all windows on that frame only.
2215
2216 Anything else means consider all windows on the selected frame
2217 and no others."
2218 (let ((base-window (selected-window)))
2219 (if (and nomini (eq base-window (minibuffer-window)))
2220 (setq base-window (next-window base-window)))
2221 (eq base-window
2222 (next-window base-window (if nomini 'arg) all-frames))))
2223 \f
2224 ;;; Deleting windows.
2225 (defun window-deletable-p (&optional window)
2226 "Return t if WINDOW can be safely deleted from its frame.
2227 Return `frame' if deleting WINDOW should delete its frame
2228 instead."
2229 (setq window (normalize-any-window window))
2230 (unless ignore-window-parameters
2231 ;; Handle atomicity.
2232 (when (window-parameter window 'window-atom)
2233 (setq window (window-atom-root window))))
2234 (let ((parent (window-parent window))
2235 (frame (window-frame window))
2236 (dedicated (and (window-buffer window) (window-dedicated-p window)))
2237 (quit-restore (window-parameter window 'quit-restore)))
2238 (cond
2239 ((frame-root-window-p window)
2240 (when (and (or dedicated
2241 (and (eq (car-safe quit-restore) 'new-frame)
2242 (eq (nth 1 quit-restore) (window-buffer window))))
2243 (other-visible-frames-p frame))
2244 ;; WINDOW is the root window of its frame. Return `frame' but
2245 ;; only if WINDOW is (1) either dedicated or quit-restore's car
2246 ;; is new-frame and the window still displays the same buffer
2247 ;; and (2) there are other frames left.
2248 'frame))
2249 ((and (not ignore-window-parameters)
2250 (eq (window-parameter window 'window-side) 'none)
2251 (or (not parent)
2252 (not (eq (window-parameter parent 'window-side) 'none))))
2253 ;; Can't delete last main window.
2254 nil)
2255 (t))))
2256
2257 (defun window-or-subwindow-p (subwindow window)
2258 "Return t if SUBWINDOW is either WINDOW or a subwindow of WINDOW."
2259 (or (eq subwindow window)
2260 (let ((parent (window-parent subwindow)))
2261 (catch 'done
2262 (while parent
2263 (if (eq parent window)
2264 (throw 'done t)
2265 (setq parent (window-parent parent))))))))
2266
2267 (defun delete-window (&optional window)
2268 "Delete WINDOW.
2269 WINDOW can be an arbitrary window and defaults to the selected
2270 one. Return nil.
2271
2272 If the variable `ignore-window-parameters' is non-nil or the
2273 `delete-window' parameter of WINDOW equals t, do not process any
2274 parameters of WINDOW. Otherwise, if the `delete-window'
2275 parameter of WINDOW specifies a function, call that function with
2276 WINDOW as its sole argument and return the value returned by that
2277 function.
2278
2279 Otherwise, if WINDOW is part of an atomic window, call
2280 `delete-window' with the root of the atomic window as its
2281 argument. If WINDOW is the only window on its frame or the last
2282 non-side window, signal an error."
2283 (interactive)
2284 (setq window (normalize-any-window window))
2285 (let* ((frame (window-frame window))
2286 (function (window-parameter window 'delete-window))
2287 (parent (window-parent window))
2288 atom-root)
2289 (window-check frame)
2290 (catch 'done
2291 ;; Handle window parameters.
2292 (cond
2293 ;; Ignore window parameters if `ignore-window-parameters' tells
2294 ;; us so or `delete-window' equals t.
2295 ((or ignore-window-parameters (eq function t)))
2296 ((functionp function)
2297 ;; The `delete-window' parameter specifies the function to call.
2298 ;; If that function is `ignore' nothing is done. It's up to the
2299 ;; function called here to avoid infinite recursion.
2300 (throw 'done (funcall function window)))
2301 ((and (window-parameter window 'window-atom)
2302 (setq atom-root (window-atom-root window))
2303 (not (eq atom-root window)))
2304 (throw 'done (delete-window atom-root)))
2305 ((and (eq (window-parameter window 'window-side) 'none)
2306 (or (not parent)
2307 (not (eq (window-parameter parent 'window-side) 'none))))
2308 (error "Attempt to delete last non-side window"))
2309 ((not parent)
2310 (error "Attempt to delete minibuffer or sole ordinary window")))
2311
2312 (let* ((horizontal (window-hchild parent))
2313 (size (window-total-size window horizontal))
2314 (frame-selected
2315 (window-or-subwindow-p (frame-selected-window frame) window))
2316 ;; Emacs 23 preferably gives WINDOW's space to its left
2317 ;; sibling.
2318 (sibling (or (window-left window) (window-right window))))
2319 (resize-window-reset frame horizontal)
2320 (cond
2321 ((and (not (window-splits window))
2322 sibling (window-sizable-p sibling size))
2323 ;; Resize WINDOW's sibling.
2324 (resize-this-window sibling size horizontal nil t)
2325 (set-window-new-normal
2326 sibling (+ (window-normal-size sibling horizontal)
2327 (window-normal-size window horizontal))))
2328 ((window-resizable-p window (- size) horizontal nil nil nil t)
2329 ;; Can do without resizing fixed-size windows.
2330 (resize-other-windows window (- size) horizontal))
2331 (t
2332 ;; Can't do without resizing fixed-size windows.
2333 (resize-other-windows window (- size) horizontal t)))
2334 ;; Actually delete WINDOW.
2335 (delete-window-internal window)
2336 (when (and frame-selected
2337 (window-parameter
2338 (frame-selected-window frame) 'no-other-window))
2339 ;; `delete-window-internal' has selected a window that should
2340 ;; not be selected, fix this here.
2341 (other-window -1 frame))
2342 (run-window-configuration-change-hook frame)
2343 (window-check frame)
2344 ;; Always return nil.
2345 nil))))
2346
2347 (defun delete-other-windows (&optional window)
2348 "Make WINDOW fill its frame.
2349 WINDOW may be any window and defaults to the selected one.
2350 Return nil.
2351
2352 If the variable `ignore-window-parameters' is non-nil or the
2353 `delete-other-windows' parameter of WINDOW equals t, do not
2354 process any parameters of WINDOW. Otherwise, if the
2355 `delete-other-windows' parameter of WINDOW specifies a function,
2356 call that function with WINDOW as its sole argument and return
2357 the value returned by that function.
2358
2359 Otherwise, if WINDOW is part of an atomic window, call this
2360 function with the root of the atomic window as its argument. If
2361 WINDOW is a non-side window, make WINDOW the only non-side window
2362 on the frame. Side windows are not deleted. If WINDOW is a side
2363 window signal an error."
2364 (interactive)
2365 (setq window (normalize-any-window window))
2366 (let* ((frame (window-frame window))
2367 (function (window-parameter window 'delete-other-windows))
2368 (window-side (window-parameter window 'window-side))
2369 atom-root side-main)
2370 (window-check frame)
2371 (catch 'done
2372 (cond
2373 ;; Ignore window parameters if `ignore-window-parameters' is t or
2374 ;; `delete-other-windows' is t.
2375 ((or ignore-window-parameters (eq function t)))
2376 ((functionp function)
2377 ;; The `delete-other-windows' parameter specifies the function
2378 ;; to call. If the function is `ignore' no windows are deleted.
2379 ;; It's up to the function called to avoid infinite recursion.
2380 (throw 'done (funcall function window)))
2381 ((and (window-parameter window 'window-atom)
2382 (setq atom-root (window-atom-root window))
2383 (not (eq atom-root window)))
2384 (throw 'done (delete-other-windows atom-root)))
2385 ((eq window-side 'none)
2386 ;; Set side-main to the major non-side window.
2387 (setq side-main (window-with-parameter 'window-side 'none nil t)))
2388 ((memq window-side window-sides)
2389 (error "Cannot make side window the only window")))
2390 ;; If WINDOW is the main non-side window, do nothing.
2391 (unless (eq window side-main)
2392 (delete-other-windows-internal window side-main)
2393 (run-window-configuration-change-hook frame)
2394 (window-check frame))
2395 ;; Always return nil.
2396 nil)))
2397
2398 (defun delete-other-windows-vertically (&optional window)
2399 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
2400 This may be a useful alternative binding for \\[delete-other-windows]
2401 if you often split windows horizontally."
2402 (interactive)
2403 (let* ((window (or window (selected-window)))
2404 (edges (window-edges window))
2405 (w window) delenda)
2406 (while (not (eq (setq w (next-window w 1)) window))
2407 (let ((e (window-edges w)))
2408 (when (and (= (car e) (car edges))
2409 (= (caddr e) (caddr edges)))
2410 (push w delenda))))
2411 (mapc 'delete-window delenda)))
2412
2413 ;;; Windows and buffers.
2414
2415 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
2416 ;; for (1) determining which buffer to show in the window when its
2417 ;; buffer shall be buried or killed and (2) which buffer to show for
2418 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2419
2420 ;; `prev-buffers' consists of <buffer, window-start, window-point>
2421 ;; triples. The entries on this list are ordered by the time their
2422 ;; buffer has been removed from the window, the most recently removed
2423 ;; buffer's entry being first. The window-start and window-point
2424 ;; components are `window-start' and `window-point' at the time the
2425 ;; buffer was removed from the window which implies that the entry must
2426 ;; be added when `set-window-buffer' removes the buffer from the window.
2427
2428 ;; `next-buffers' is the list of buffers that have been replaced
2429 ;; recently by `switch-to-prev-buffer'. These buffers are the least
2430 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
2431 ;; candidates of `switch-to-next-buffer' to switch to. This list is
2432 ;; reset to nil by any action changing the window's buffer with the
2433 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
2434 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
2435 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
2436
2437 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
2438 ;; if such a buffer was killed while the window was hidden within a
2439 ;; window configuration. Such killed buffers get removed whenever
2440 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
2441
2442 ;; The following function is called by `set-window-buffer' _before_ it
2443 ;; replaces the buffer of the argument window with the new buffer.
2444 (defun record-window-buffer (&optional window)
2445 "Record WINDOW's buffer.
2446 WINDOW must be a live window and defaults to the selected one."
2447 (let* ((window (normalize-live-window window))
2448 (buffer (window-buffer window))
2449 (entry (assq buffer (window-prev-buffers window))))
2450 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
2451 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2452 (set-window-next-buffers window nil)
2453
2454 (when entry
2455 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
2456 (set-window-prev-buffers
2457 window (assq-delete-all buffer (window-prev-buffers window))))
2458
2459 ;; Don't record insignificant buffers.
2460 (unless (eq (aref (buffer-name buffer) 0) ?\s)
2461 ;; Add an entry for buffer to WINDOW's previous buffers.
2462 (with-current-buffer buffer
2463 (let ((start (window-start window))
2464 (point (window-point window)))
2465 (setq entry
2466 (cons buffer
2467 (if entry
2468 ;; We have an entry, update marker positions.
2469 (list (set-marker (nth 1 entry) start)
2470 (set-marker (nth 2 entry) point))
2471 ;; Make new markers.
2472 (list (copy-marker start)
2473 (copy-marker point)))))
2474
2475 (set-window-prev-buffers
2476 window (cons entry (window-prev-buffers window))))))))
2477
2478 (defun unrecord-window-buffer (&optional window buffer)
2479 "Unrecord BUFFER in WINDOW.
2480 WINDOW must be a live window and defaults to the selected one.
2481 BUFFER must be a live buffer and defaults to the buffer of
2482 WINDOW."
2483 (let* ((window (normalize-live-window window))
2484 (buffer (or buffer (window-buffer window))))
2485 (set-window-prev-buffers
2486 window (assq-delete-all buffer (window-prev-buffers window)))
2487 (set-window-next-buffers
2488 window (delq buffer (window-next-buffers window)))))
2489
2490 (defun set-window-buffer-start-and-point (window buffer &optional start point)
2491 "Set WINDOW's buffer to BUFFER.
2492 Optional argument START non-nil means set WINDOW's start position
2493 to START. Optional argument POINT non-nil means set WINDOW's
2494 point to POINT. If WINDOW is selected this also sets BUFFER's
2495 `point' to POINT. If WINDOW is selected and the buffer it showed
2496 before was current this also makes BUFFER the current buffer."
2497 (let ((selected (eq window (selected-window)))
2498 (current (eq (window-buffer window) (current-buffer))))
2499 (set-window-buffer window buffer)
2500 (when (and selected current)
2501 (set-buffer buffer))
2502 (when start
2503 (set-window-start window start))
2504 (when point
2505 (if selected
2506 (with-current-buffer buffer
2507 (goto-char point))
2508 (set-window-point window point)))))
2509
2510 (defun switch-to-prev-buffer (&optional window bury-or-kill)
2511 "In WINDOW switch to previous buffer.
2512 WINDOW must be a live window and defaults to the selected one.
2513
2514 Optional argument BURY-OR-KILL non-nil means the buffer currently
2515 shown in WINDOW is about to be buried or killed and consequently
2516 shall not be switched to in future invocations of this command."
2517 (interactive)
2518 (let* ((window (normalize-live-window window))
2519 (old-buffer (window-buffer window))
2520 ;; Save this since it's destroyed by `set-window-buffer'.
2521 (next-buffers (window-next-buffers window))
2522 entry new-buffer killed-buffers deletable visible)
2523 (cond
2524 ;; When BURY-OR-KILL is non-nil, there's no previous buffer for
2525 ;; this window, and we can delete the window (or the frame) do
2526 ;; that.
2527 ((and bury-or-kill
2528 (or (not (window-prev-buffers window))
2529 (and (eq (caar (window-prev-buffers window)) old-buffer)
2530 (not (cdr (car (window-prev-buffers window))))))
2531 (setq deletable (window-deletable-p window)))
2532 (if (eq deletable 'frame)
2533 (delete-frame (window-frame window))
2534 (delete-window window)))
2535 ((window-dedicated-p window)
2536 (error "Window %s is dedicated to buffer %s" window old-buffer)))
2537
2538 (unless deletable
2539 (catch 'found
2540 ;; Scan WINDOW's previous buffers first, skipping entries of next
2541 ;; buffers.
2542 (dolist (entry (window-prev-buffers window))
2543 (when (and (setq new-buffer (car entry))
2544 (or (buffer-live-p new-buffer)
2545 (not (setq killed-buffers
2546 (cons new-buffer killed-buffers))))
2547 (not (eq new-buffer old-buffer))
2548 (or bury-or-kill
2549 (not (memq new-buffer next-buffers))))
2550 (set-window-buffer-start-and-point
2551 window new-buffer (nth 1 entry) (nth 2 entry))
2552 (throw 'found t)))
2553 ;; Scan reverted buffer list of WINDOW's frame next, skipping
2554 ;; entries of next buffers. Note that when we bury or kill a
2555 ;; buffer we don't reverse the global buffer list to avoid showing
2556 ;; a buried buffer instead. Otherwise, we must reverse the global
2557 ;; buffer list in order to make sure that switching to the
2558 ;; previous/next buffer traverse it in opposite directions.
2559 (dolist (buffer (if bury-or-kill
2560 (buffer-list (window-frame window))
2561 (nreverse (buffer-list (window-frame window)))))
2562 (when (and (buffer-live-p buffer)
2563 (not (eq buffer old-buffer))
2564 (not (eq (aref (buffer-name buffer) 0) ?\s))
2565 (or bury-or-kill (not (memq buffer next-buffers))))
2566 (if (get-buffer-window buffer)
2567 ;; Try to avoid showing a buffer visible in some other window.
2568 (setq visible buffer)
2569 (setq new-buffer buffer)
2570 (set-window-buffer-start-and-point window new-buffer)
2571 (throw 'found t))))
2572 (unless bury-or-kill
2573 ;; Scan reverted next buffers last (must not use nreverse
2574 ;; here!).
2575 (dolist (buffer (reverse next-buffers))
2576 ;; Actually, buffer _must_ be live here since otherwise it
2577 ;; would have been caught in the scan of previous buffers.
2578 (when (and (or (buffer-live-p buffer)
2579 (not (setq killed-buffers
2580 (cons buffer killed-buffers))))
2581 (not (eq buffer old-buffer))
2582 (setq entry (assq buffer (window-prev-buffers window))))
2583 (setq new-buffer buffer)
2584 (set-window-buffer-start-and-point
2585 window new-buffer (nth 1 entry) (nth 2 entry))
2586 (throw 'found t))))
2587
2588 ;; Show a buffer visible in another window.
2589 (when visible
2590 (setq new-buffer visible)
2591 (set-window-buffer-start-and-point window new-buffer)))
2592
2593 (if bury-or-kill
2594 ;; Remove `old-buffer' from WINDOW's previous and (restored list
2595 ;; of) next buffers.
2596 (progn
2597 (set-window-prev-buffers
2598 window (assq-delete-all old-buffer (window-prev-buffers window)))
2599 (set-window-next-buffers window (delq old-buffer next-buffers)))
2600 ;; Move `old-buffer' to head of WINDOW's restored list of next
2601 ;; buffers.
2602 (set-window-next-buffers
2603 window (cons old-buffer (delq old-buffer next-buffers)))))
2604
2605 ;; Remove killed buffers from WINDOW's previous and next buffers.
2606 (when killed-buffers
2607 (dolist (buffer killed-buffers)
2608 (set-window-prev-buffers
2609 window (assq-delete-all buffer (window-prev-buffers window)))
2610 (set-window-next-buffers
2611 window (delq buffer (window-next-buffers window)))))
2612
2613 ;; Return new-buffer.
2614 new-buffer))
2615
2616 (defun switch-to-next-buffer (&optional window)
2617 "In WINDOW switch to next buffer.
2618 WINDOW must be a live window and defaults to the selected one."
2619 (interactive)
2620 (let* ((window (normalize-live-window window))
2621 (old-buffer (window-buffer window))
2622 (next-buffers (window-next-buffers window))
2623 new-buffer entry killed-buffers visible)
2624 (when (window-dedicated-p window)
2625 (error "Window %s is dedicated to buffer %s" window old-buffer))
2626
2627 (catch 'found
2628 ;; Scan WINDOW's next buffers first.
2629 (dolist (buffer next-buffers)
2630 (when (and (or (buffer-live-p buffer)
2631 (not (setq killed-buffers
2632 (cons buffer killed-buffers))))
2633 (not (eq buffer old-buffer))
2634 (setq entry (assq buffer (window-prev-buffers window))))
2635 (setq new-buffer buffer)
2636 (set-window-buffer-start-and-point
2637 window new-buffer (nth 1 entry) (nth 2 entry))
2638 (throw 'found t)))
2639 ;; Scan the buffer list of WINDOW's frame next, skipping previous
2640 ;; buffers entries.
2641 (dolist (buffer (buffer-list (window-frame window)))
2642 (when (and (buffer-live-p buffer) (not (eq buffer old-buffer))
2643 (not (eq (aref (buffer-name buffer) 0) ?\s))
2644 (not (assq buffer (window-prev-buffers window))))
2645 (if (get-buffer-window buffer)
2646 ;; Try to avoid showing a buffer visible in some other window.
2647 (setq visible buffer)
2648 (setq new-buffer buffer)
2649 (set-window-buffer-start-and-point window new-buffer)
2650 (throw 'found t))))
2651 ;; Scan WINDOW's reverted previous buffers last (must not use
2652 ;; nreverse here!)
2653 (dolist (entry (reverse (window-prev-buffers window)))
2654 (when (and (setq new-buffer (car entry))
2655 (or (buffer-live-p new-buffer)
2656 (not (setq killed-buffers
2657 (cons new-buffer killed-buffers))))
2658 (not (eq new-buffer old-buffer)))
2659 (set-window-buffer-start-and-point
2660 window new-buffer (nth 1 entry) (nth 2 entry))
2661 (throw 'found t)))
2662
2663 ;; Show a buffer visible in another window.
2664 (when visible
2665 (setq new-buffer visible)
2666 (set-window-buffer-start-and-point window new-buffer)))
2667
2668 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
2669 (set-window-next-buffers window (delq new-buffer next-buffers))
2670
2671 ;; Remove killed buffers from WINDOW's previous and next buffers.
2672 (when killed-buffers
2673 (dolist (buffer killed-buffers)
2674 (set-window-prev-buffers
2675 window (assq-delete-all buffer (window-prev-buffers window)))
2676 (set-window-next-buffers
2677 window (delq buffer (window-next-buffers window)))))
2678
2679 ;; Return new-buffer.
2680 new-buffer))
2681
2682 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
2683 "Search LIST for a valid buffer to display in FRAME.
2684 Return nil when all buffers in LIST are undesirable for display,
2685 otherwise return the first suitable buffer in LIST.
2686
2687 Buffers not visible in windows are preferred to visible buffers,
2688 unless VISIBLE-OK is non-nil.
2689 If the optional argument FRAME is nil, it defaults to the selected frame.
2690 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
2691 ;; This logic is more or less copied from other-buffer.
2692 (setq frame (or frame (selected-frame)))
2693 (let ((pred (frame-parameter frame 'buffer-predicate))
2694 found buf)
2695 (while (and (not found) list)
2696 (setq buf (car list))
2697 (if (and (not (eq buffer buf))
2698 (buffer-live-p buf)
2699 (or (null pred) (funcall pred buf))
2700 (not (eq (aref (buffer-name buf) 0) ?\s))
2701 (or visible-ok (null (get-buffer-window buf 'visible))))
2702 (setq found buf)
2703 (setq list (cdr list))))
2704 (car list)))
2705
2706 (defun last-buffer (&optional buffer visible-ok frame)
2707 "Return the last buffer in FRAME's buffer list.
2708 If BUFFER is the last buffer, return the preceding buffer
2709 instead. Buffers not visible in windows are preferred to visible
2710 buffers, unless optional argument VISIBLE-OK is non-nil.
2711 Optional third argument FRAME nil or omitted means use the
2712 selected frame's buffer list. If no such buffer exists, return
2713 the buffer `*scratch*', creating it if necessary."
2714 (setq frame (or frame (selected-frame)))
2715 (or (get-next-valid-buffer (nreverse (buffer-list frame))
2716 buffer visible-ok frame)
2717 (get-buffer "*scratch*")
2718 (let ((scratch (get-buffer-create "*scratch*")))
2719 (set-buffer-major-mode scratch)
2720 scratch)))
2721
2722 (defun bury-buffer (&optional buffer-or-name)
2723 "Put BUFFER-OR-NAME at the end of the list of all buffers.
2724 There it is the least likely candidate for `other-buffer' to
2725 return; thus, the least likely buffer for \\[switch-to-buffer] to
2726 select by default.
2727
2728 You can specify a buffer name as BUFFER-OR-NAME, or an actual
2729 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
2730 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
2731 remove the current buffer from the selected window if it is
2732 displayed there."
2733 (interactive)
2734 (let* ((buffer (normalize-live-buffer buffer-or-name)))
2735 ;; If `buffer-or-name' is not on the selected frame we unrecord it
2736 ;; although it's not "here" (call it a feature).
2737 (unrecord-buffer buffer)
2738 ;; Handle case where `buffer-or-name' is nil and the current buffer
2739 ;; is shown in the selected window.
2740 (cond
2741 ((or buffer-or-name (not (eq buffer (window-buffer)))))
2742 ((not (window-dedicated-p))
2743 (switch-to-prev-buffer nil 'bury))
2744 ((frame-root-window-p (selected-window))
2745 (iconify-frame (window-frame (selected-window))))
2746 ((window-deletable-p)
2747 (delete-window)))
2748 ;; Always return nil.
2749 nil))
2750
2751 (defun unbury-buffer ()
2752 "Switch to the last buffer in the buffer list."
2753 (interactive)
2754 (switch-to-buffer (last-buffer)))
2755
2756 (defun next-buffer ()
2757 "In selected window switch to next buffer."
2758 (interactive)
2759 (switch-to-next-buffer))
2760
2761 (defun previous-buffer ()
2762 "In selected window switch to previous buffer."
2763 (interactive)
2764 (switch-to-prev-buffer))
2765
2766 (defun delete-windows-on (&optional buffer-or-name frame)
2767 "Delete all windows showing BUFFER-OR-NAME.
2768 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2769 and defaults to the current buffer.
2770
2771 The following non-nil values of the optional argument FRAME
2772 have special meanings:
2773
2774 - t means consider all windows on the selected frame only.
2775
2776 - `visible' means consider all windows on all visible frames on
2777 the current terminal.
2778
2779 - 0 (the number zero) means consider all windows on all visible
2780 and iconified frames on the current terminal.
2781
2782 - A frame means consider all windows on that frame only.
2783
2784 Any other value of FRAME means consider all windows on all
2785 frames.
2786
2787 When a window showing BUFFER-OR-NAME is dedicated and the only
2788 window of its frame, that frame is deleted when there are other
2789 frames left."
2790 (interactive "BDelete windows on (buffer):\nP")
2791 (let ((buffer (normalize-live-buffer buffer-or-name))
2792 ;; Handle the "inverted" meaning of the FRAME argument wrt other
2793 ;; `window-list-1' based function.
2794 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
2795 (dolist (window (window-list-1 nil nil all-frames))
2796 (if (eq (window-buffer window) buffer)
2797 (let ((deletable (window-deletable-p window)))
2798 (cond
2799 ((eq deletable 'frame)
2800 ;; Delete frame.
2801 (delete-frame (window-frame window)))
2802 (deletable
2803 ;; Delete window only.
2804 (delete-window window))
2805 (t
2806 ;; In window switch to previous buffer.
2807 (set-window-dedicated-p window nil)
2808 (switch-to-prev-buffer window 'bury))))
2809 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
2810 (unrecord-window-buffer window buffer)))))
2811
2812 (defun replace-buffer-in-windows (&optional buffer-or-name)
2813 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2814 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2815 and defaults to the current buffer.
2816
2817 When a window showing BUFFER-OR-NAME is either dedicated, or the
2818 window has no previous buffer, that window is deleted. If that
2819 window is the only window on its frame, the frame is deleted too
2820 when there are other frames left. If there are no other frames
2821 left, some other buffer is displayed in that window.
2822
2823 This function removes the buffer denoted by BUFFER-OR-NAME from
2824 all window-local buffer lists."
2825 (let ((buffer (normalize-live-buffer buffer-or-name)))
2826 (dolist (window (window-list-1 nil nil t))
2827 (if (eq (window-buffer window) buffer)
2828 (let ((deletable (window-deletable-p window)))
2829 (cond
2830 ((eq deletable 'frame)
2831 ;; Delete frame.
2832 (delete-frame (window-frame window)))
2833 ((and (window-dedicated-p window) deletable)
2834 ;; Delete window.
2835 (delete-window window))
2836 (t
2837 ;; Switch to another buffer in window.
2838 (set-window-dedicated-p window nil)
2839 (switch-to-prev-buffer window 'kill))))
2840 ;; Unrecord BUFFER in WINDOW.
2841 (unrecord-window-buffer window buffer)))))
2842
2843 (defun quit-restore-window (&optional window kill)
2844 "Quit WINDOW in some way.
2845 WINDOW must be a live window and defaults to the selected window.
2846 Return nil.
2847
2848 According to information stored in WINDOW's `quit-restore' window
2849 parameter either \(1) delete WINDOW and its frame, \(2) delete
2850 WINDOW, \(3) restore the buffer previously displayed in WINDOW,
2851 or \(4) make WINDOW display some other buffer than the present
2852 one. If non-nil, reset `quit-restore' parameter to nil.
2853
2854 Optional argument KILL non-nil means in addition kill WINDOW's
2855 buffer. If KILL is nil, put WINDOW's buffer at the end of the
2856 buffer list. Interactively, KILL is the prefix argument."
2857 (interactive "i\nP")
2858 (setq window (normalize-live-window window))
2859 (let ((buffer (window-buffer window))
2860 (quit-restore (window-parameter window 'quit-restore))
2861 deletable resize)
2862 (cond
2863 ((and (or (and (memq (car-safe quit-restore) '(new-window new-frame))
2864 ;; Check that WINDOW's buffer is still the same.
2865 (eq (window-buffer window) (nth 1 quit-restore)))
2866 (window-dedicated-p window))
2867 (setq deletable (window-deletable-p window)))
2868 ;; WINDOW can be deleted.
2869 (unrecord-buffer buffer)
2870 (if (eq deletable 'frame)
2871 ;; WINDOW's frame can be deleted.
2872 (delete-frame (window-frame window))
2873 ;; Just delete WINDOW.
2874 (delete-window window))
2875 ;; If the previously selected window is still alive, select it.
2876 (when (window-live-p (nth 2 quit-restore))
2877 (select-window (nth 2 quit-restore))))
2878 ((and (buffer-live-p (nth 0 quit-restore))
2879 ;; The buffer currently shown in WINDOW must still be the
2880 ;; buffer shown when its `quit-restore' parameter was created
2881 ;; in the first place.
2882 (eq (window-buffer window) (nth 3 quit-restore)))
2883 (setq resize (with-current-buffer buffer temp-buffer-resize-mode))
2884 ;; Unrecord buffer.
2885 (unrecord-buffer buffer)
2886 (unrecord-window-buffer window buffer)
2887 ;; Display buffer stored in the quit-restore parameter.
2888 (set-window-dedicated-p window nil)
2889 (set-window-buffer window (nth 0 quit-restore))
2890 (set-window-start window (nth 1 quit-restore))
2891 (set-window-point window (nth 2 quit-restore))
2892 (when (and resize (/= (nth 4 quit-restore) (window-total-size window)))
2893 (resize-window
2894 window (- (nth 4 quit-restore) (window-total-size window))))
2895 ;; Reset the quit-restore parameter.
2896 (set-window-parameter window 'quit-restore nil)
2897 (when (window-live-p (nth 5 quit-restore))
2898 (select-window (nth 5 quit-restore))))
2899 (t
2900 ;; Otherwise, show another buffer in WINDOW and reset the
2901 ;; quit-restore parameter.
2902 (set-window-parameter window 'quit-restore nil)
2903 (unrecord-buffer buffer)
2904 (switch-to-prev-buffer window 'bury-or-kill)))
2905
2906 ;; Kill WINDOW's old-buffer if requested
2907 (when kill (kill-buffer buffer))
2908 nil))
2909 \f
2910 ;;; Splitting windows.
2911 (defsubst window-split-min-size (&optional horizontal)
2912 "Return minimum height of any window when splitting windows.
2913 Optional argument HORIZONTAL non-nil means return minimum width."
2914 (if horizontal
2915 (max window-min-width window-safe-min-width)
2916 (max window-min-height window-safe-min-height)))
2917
2918 (defun split-window (&optional window size side)
2919 "Make a new window adjacent to WINDOW.
2920 WINDOW can be any window and defaults to the selected one.
2921 Return the new window which is always a live window.
2922
2923 Optional argument SIZE a positive number means make WINDOW SIZE
2924 lines or columns tall. If SIZE is negative, make the new window
2925 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
2926 absolute value can be less than `window-min-height' or
2927 `window-min-width'; so this command can make a new window as
2928 small as one line or two columns. SIZE defaults to half of
2929 WINDOW's size. Interactively, SIZE is the prefix argument.
2930
2931 Optional third argument SIDE nil (or `below') specifies that the
2932 new window shall be located below WINDOW. SIDE `above' means the
2933 new window shall be located above WINDOW. In both cases SIZE
2934 specifies the new number of lines for WINDOW \(or the new window
2935 if SIZE is negative) including space reserved for the mode and/or
2936 header line.
2937
2938 SIDE t (or `right') specifies that the new window shall be
2939 located on the right side of WINDOW. SIDE `left' means the new
2940 window shall be located on the left of WINDOW. In both cases
2941 SIZE specifies the new number of columns for WINDOW \(or the new
2942 window provided SIZE is negative) including space reserved for
2943 fringes and the scrollbar or a divider column. Any other non-nil
2944 value for SIDE is currently handled like t (or `right').
2945
2946 If the variable `ignore-window-parameters' is non-nil or the
2947 `split-window' parameter of WINDOW equals t, do not process any
2948 parameters of WINDOW. Otherwise, if the `split-window' parameter
2949 of WINDOW specifies a function, call that function with all three
2950 arguments and return the value returned by that function.
2951
2952 Otherwise, if WINDOW is part of an atomic window, \"split\" the
2953 root of that atomic window. The new window does not become a
2954 member of that atomic window.
2955
2956 If WINDOW is live, properties of the new window like margins and
2957 scrollbars are inherited from WINDOW. If WINDOW is an internal
2958 window, these properties as well as the buffer displayed in the
2959 new window are inherited from the window selected on WINDOW's
2960 frame. The selected window is not changed by this function."
2961 (interactive "i")
2962 (setq window (normalize-any-window window))
2963 (let* ((horizontal (not (memq side '(nil below above))))
2964 (frame (window-frame window))
2965 (parent (window-parent window))
2966 (function (window-parameter window 'split-window))
2967 (window-side (window-parameter window 'window-side))
2968 ;; Rebind `window-nest' since in some cases we may have to
2969 ;; override its value.
2970 (window-nest window-nest)
2971 atom-root)
2972
2973 (window-check frame)
2974 (catch 'done
2975 (cond
2976 ;; Ignore window parameters if either `ignore-window-parameters'
2977 ;; is t or the `split-window' parameter equals t.
2978 ((or ignore-window-parameters (eq function t)))
2979 ((functionp function)
2980 ;; The `split-window' parameter specifies the function to call.
2981 ;; If that function is `ignore', do nothing.
2982 (throw 'done (funcall function window size side)))
2983 ;; If WINDOW is a subwindow of an atomic window, split the root
2984 ;; window of that atomic window instead.
2985 ((and (window-parameter window 'window-atom)
2986 (setq atom-root (window-atom-root window))
2987 (not (eq atom-root window)))
2988 (throw 'done (split-window atom-root size side))))
2989
2990 (when (and window-side
2991 (or (not parent)
2992 (not (window-parameter parent 'window-side))))
2993 ;; WINDOW is a side root window. To make sure that a new parent
2994 ;; window gets created set `window-nest' to t.
2995 (setq window-nest t))
2996
2997 (when (and window-splits size (> size 0))
2998 ;; If `window-splits' is non-nil and SIZE is a non-negative
2999 ;; integer, we cannot reasonably resize other windows. Rather
3000 ;; bind `window-nest' to t to make sure that subsequent window
3001 ;; deletions are handled correctly.
3002 (setq window-nest t))
3003
3004 (let* ((parent-size
3005 ;; `parent-size' is the size of WINDOW's parent, provided
3006 ;; it has one.
3007 (when parent (window-total-size parent horizontal)))
3008 ;; `resize' non-nil means we are supposed to resize other
3009 ;; windows in WINDOW's combination.
3010 (resize
3011 (and window-splits (not window-nest)
3012 ;; Resize makes sense in iso-combinations only.
3013 (window-iso-combined-p window horizontal)))
3014 ;; `old-size' is the current size of WINDOW.
3015 (old-size (window-total-size window horizontal))
3016 ;; `new-size' is the specified or calculated size of the
3017 ;; new window.
3018 (new-size
3019 (cond
3020 ((not size)
3021 (max (window-split-min-size horizontal)
3022 (if resize
3023 ;; When resizing try to give the new window the
3024 ;; average size of a window in its combination.
3025 (min (- parent-size
3026 (window-min-size parent horizontal))
3027 (/ parent-size
3028 (1+ (window-iso-combinations
3029 parent horizontal))))
3030 ;; Else try to give the new window half the size
3031 ;; of WINDOW (plus an eventual odd line).
3032 (+ (/ old-size 2) (% old-size 2)))))
3033 ((>= size 0)
3034 ;; SIZE non-negative specifies the new size of WINDOW.
3035
3036 ;; Note: Specifying a non-negative SIZE is practically
3037 ;; always done as workaround for making the new window
3038 ;; appear above or on the left of the new window (the
3039 ;; ispell window is a typical example of that). In all
3040 ;; these cases the SIDE argument should be set to 'above
3041 ;; or 'left in order to support the 'resize option.
3042 ;; Here we have to nest the windows instead, see above.
3043 (- old-size size))
3044 (t
3045 ;; SIZE negative specifies the size of the new window.
3046 (- size))))
3047 new-parent new-normal)
3048
3049 ;; Check SIZE.
3050 (cond
3051 ((not size)
3052 (cond
3053 (resize
3054 ;; SIZE unspecified, resizing.
3055 (when (and (not (window-sizable-p parent (- new-size) horizontal))
3056 ;; Try again with minimum split size.
3057 (setq new-size
3058 (max new-size (window-split-min-size horizontal)))
3059 (not (window-sizable-p parent (- new-size) horizontal)))
3060 (error "Window %s too small for splitting" parent)))
3061 ((> (+ new-size (window-min-size window horizontal)) old-size)
3062 ;; SIZE unspecified, no resizing.
3063 (error "Window %s too small for splitting" window))))
3064 ((and (>= size 0)
3065 (or (>= size old-size)
3066 (< new-size (if horizontal
3067 window-safe-min-width
3068 window-safe-min-width))))
3069 ;; SIZE specified as new size of old window. If the new size
3070 ;; is larger than the old size or the size of the new window
3071 ;; would be less than the safe minimum, signal an error.
3072 (error "Window %s too small for splitting" window))
3073 (resize
3074 ;; SIZE specified, resizing.
3075 (unless (window-sizable-p parent (- new-size) horizontal)
3076 ;; If we cannot resize the parent give up.
3077 (error "Window %s too small for splitting" parent)))
3078 ((or (< new-size
3079 (if horizontal window-safe-min-width window-safe-min-height))
3080 (< (- old-size new-size)
3081 (if horizontal window-safe-min-width window-safe-min-height)))
3082 ;; SIZE specification violates minimum size restrictions.
3083 (error "Window %s too small for splitting" window)))
3084
3085 (resize-window-reset frame horizontal)
3086
3087 (setq new-parent
3088 ;; Make new-parent non-nil if we need a new parent window;
3089 ;; either because we want to nest or because WINDOW is not
3090 ;; iso-combined.
3091 (or window-nest (not (window-iso-combined-p window horizontal))))
3092 (setq new-normal
3093 ;; Make new-normal the normal size of the new window.
3094 (cond
3095 (size (/ (float new-size) (if new-parent old-size parent-size)))
3096 (new-parent 0.5)
3097 (resize (/ 1.0 (1+ (window-iso-combinations parent horizontal))))
3098 (t (/ (window-normal-size window horizontal) 2.0))))
3099
3100 (if resize
3101 ;; Try to get space from OLD's siblings. We could go "up" and
3102 ;; try getting additional space from surrounding windows but
3103 ;; we won't be able to return space to those windows when we
3104 ;; delete the one we create here. Hence we do not go up.
3105 (progn
3106 (resize-subwindows parent (- new-size) horizontal)
3107 (let* ((normal (- 1.0 new-normal))
3108 (sub (window-child parent)))
3109 (while sub
3110 (set-window-new-normal
3111 sub (* (window-normal-size sub horizontal) normal))
3112 (setq sub (window-right sub)))))
3113 ;; Get entire space from WINDOW.
3114 (set-window-new-total window (- old-size new-size))
3115 (resize-this-window window (- new-size) horizontal)
3116 (set-window-new-normal
3117 window (- (if new-parent 1.0 (window-normal-size window horizontal))
3118 new-normal)))
3119
3120 (let* ((new (split-window-internal window new-size side new-normal)))
3121 ;; Inherit window-side parameters, if any.
3122 (when (and window-side new-parent)
3123 (set-window-parameter (window-parent new) 'window-side window-side)
3124 (set-window-parameter new 'window-side window-side))
3125
3126 (run-window-configuration-change-hook frame)
3127 (window-check frame)
3128 ;; Always return the new window.
3129 new)))))
3130
3131 ;; I think this should be the default; I think people will prefer it--rms.
3132 (defcustom split-window-keep-point t
3133 "If non-nil, \\[split-window-above-each-other] keeps the original point \
3134 in both children.
3135 This is often more convenient for editing.
3136 If nil, adjust point in each of the two windows to minimize redisplay.
3137 This is convenient on slow terminals, but point can move strangely.
3138
3139 This option applies only to `split-window-above-each-other' and
3140 functions that call it. `split-window' always keeps the original
3141 point in both children."
3142 :type 'boolean
3143 :group 'windows)
3144
3145 (defun split-window-above-each-other (&optional size)
3146 "Split selected window into two windows, one above the other.
3147 The upper window gets SIZE lines and the lower one gets the rest.
3148 SIZE negative means the lower window gets -SIZE lines and the
3149 upper one the rest. With no argument, split windows equally or
3150 close to it. Both windows display the same buffer, now current.
3151
3152 If the variable `split-window-keep-point' is non-nil, both new
3153 windows will get the same value of point as the selected window.
3154 This is often more convenient for editing. The upper window is
3155 the selected window.
3156
3157 Otherwise, we choose window starts so as to minimize the amount of
3158 redisplay; this is convenient on slow terminals. The new selected
3159 window is the one that the current value of point appears in. The
3160 value of point can change if the text around point is hidden by the
3161 new mode line.
3162
3163 Regardless of the value of `split-window-keep-point', the upper
3164 window is the original one and the return value is the new, lower
3165 window."
3166 (interactive "P")
3167 (let ((old-window (selected-window))
3168 (old-point (point))
3169 (size (and size (prefix-numeric-value size)))
3170 moved-by-window-height moved new-window bottom)
3171 (when (and size (< size 0) (< (- size) window-min-height))
3172 ;; `split-window' would not signal an error here.
3173 (error "Size of new window too small"))
3174 (setq new-window (split-window nil size))
3175 (unless split-window-keep-point
3176 (with-current-buffer (window-buffer)
3177 (goto-char (window-start))
3178 (setq moved (vertical-motion (window-height)))
3179 (set-window-start new-window (point))
3180 (when (> (point) (window-point new-window))
3181 (set-window-point new-window (point)))
3182 (when (= moved (window-height))
3183 (setq moved-by-window-height t)
3184 (vertical-motion -1))
3185 (setq bottom (point)))
3186 (and moved-by-window-height
3187 (<= bottom (point))
3188 (set-window-point old-window (1- bottom)))
3189 (and moved-by-window-height
3190 (<= (window-start new-window) old-point)
3191 (set-window-point new-window old-point)
3192 (select-window new-window)))
3193 ;; Always copy quit-restore parameter in interactive use.
3194 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3195 (when quit-restore
3196 (set-window-parameter new-window 'quit-restore quit-restore)))
3197 new-window))
3198
3199 (defalias 'split-window-vertically 'split-window-above-each-other)
3200
3201 (defun split-window-side-by-side (&optional size)
3202 "Split selected window into two windows side by side.
3203 The selected window becomes the left one and gets SIZE columns.
3204 SIZE negative means the right window gets -SIZE lines.
3205
3206 SIZE includes the width of the window's scroll bar; if there are
3207 no scroll bars, it includes the width of the divider column to
3208 the window's right, if any. SIZE omitted or nil means split
3209 window equally.
3210
3211 The selected window remains selected. Return the new window."
3212 (interactive "P")
3213 (let ((old-window (selected-window))
3214 (size (and size (prefix-numeric-value size)))
3215 new-window)
3216 (when (and size (< size 0) (< (- size) window-min-width))
3217 ;; `split-window' would not signal an error here.
3218 (error "Size of new window too small"))
3219 (setq new-window (split-window nil size t))
3220 ;; Always copy quit-restore parameter in interactive use.
3221 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3222 (when quit-restore
3223 (set-window-parameter new-window 'quit-restore quit-restore)))
3224 new-window))
3225
3226 (defalias 'split-window-horizontally 'split-window-side-by-side)
3227 \f
3228 ;;; Balancing windows.
3229
3230 ;; The following routine uses the recycled code from an old version of
3231 ;; `resize-subwindows'. It's not very pretty, but coding it the way the
3232 ;; new `resize-subwindows' code does would hardly make it any shorter or
3233 ;; more readable (FWIW we'd need three loops - one to calculate the
3234 ;; minimum sizes per window, one to enlarge or shrink windows until the
3235 ;; new parent-size matches, and one where we shrink the largest/enlarge
3236 ;; the smallest window).
3237 (defun balance-windows-2 (window horizontal)
3238 "Subroutine of `balance-windows-1'.
3239 WINDOW must be an iso-combination."
3240 (let* ((first (window-child window))
3241 (sub first)
3242 (number-of-children 0)
3243 (parent-size (window-new-total window))
3244 (total-sum parent-size)
3245 found failed size sub-total sub-delta sub-amount rest)
3246 (while sub
3247 (setq number-of-children (1+ number-of-children))
3248 (when (window-size-fixed-p sub horizontal)
3249 (setq total-sum
3250 (- total-sum (window-total-size sub horizontal)))
3251 (set-window-new-normal sub 'ignore))
3252 (setq sub (window-right sub)))
3253
3254 (setq failed t)
3255 (while (and failed (> number-of-children 0))
3256 (setq size (/ total-sum number-of-children))
3257 (setq failed nil)
3258 (setq sub first)
3259 (while (and sub (not failed))
3260 ;; Ignore subwindows that should be ignored or are stuck.
3261 (unless (resize-subwindows-skip-p sub)
3262 (setq found t)
3263 (setq sub-total (window-total-size sub horizontal))
3264 (setq sub-delta (- size sub-total))
3265 (setq sub-amount
3266 (window-sizable sub sub-delta horizontal))
3267 ;; Register the new total size for this subwindow.
3268 (set-window-new-total sub (+ sub-total sub-amount))
3269 (unless (= sub-amount sub-delta)
3270 (setq total-sum (- total-sum sub-total sub-amount))
3271 (setq number-of-children (1- number-of-children))
3272 ;; We failed and need a new round.
3273 (setq failed t)
3274 (set-window-new-normal sub 'skip)))
3275 (setq sub (window-right sub))))
3276
3277 (setq rest (% total-sum number-of-children))
3278 ;; Fix rounding by trying to enlarge non-stuck windows by one line
3279 ;; (column) until `rest' is zero.
3280 (setq sub first)
3281 (while (and sub (> rest 0))
3282 (unless (resize-subwindows-skip-p window)
3283 (set-window-new-total sub 1 t)
3284 (setq rest (1- rest)))
3285 (setq sub (window-right sub)))
3286
3287 ;; Fix rounding by trying to enlarge stuck windows by one line
3288 ;; (column) until `rest' equals zero.
3289 (setq sub first)
3290 (while (and sub (> rest 0))
3291 (unless (eq (window-new-normal sub) 'ignore)
3292 (set-window-new-total sub 1 t)
3293 (setq rest (1- rest)))
3294 (setq sub (window-right sub)))
3295
3296 (setq sub first)
3297 (while sub
3298 ;; Record new normal sizes.
3299 (set-window-new-normal
3300 sub (/ (if (eq (window-new-normal sub) 'ignore)
3301 (window-total-size sub horizontal)
3302 (window-new-total sub))
3303 (float parent-size)))
3304 ;; Recursively balance each subwindow's subwindows.
3305 (balance-windows-1 sub horizontal)
3306 (setq sub (window-right sub)))))
3307
3308 (defun balance-windows-1 (window &optional horizontal)
3309 "Subroutine of `balance-windows'."
3310 (if (window-child window)
3311 (let ((sub (window-child window)))
3312 (if (window-iso-combined-p sub horizontal)
3313 (balance-windows-2 window horizontal)
3314 (let ((size (window-new-total window)))
3315 (while sub
3316 (set-window-new-total sub size)
3317 (balance-windows-1 sub horizontal)
3318 (setq sub (window-right sub))))))))
3319
3320 (defun balance-windows (&optional window-or-frame)
3321 "Balance the sizes of subwindows of WINDOW-OR-FRAME.
3322 WINDOW-OR-FRAME is optional and defaults to the selected frame.
3323 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
3324 subwindows of that frame's root window. If WINDOW-OR-FRAME
3325 denots a window, balance the sizes of all subwindows of that
3326 window."
3327 (interactive)
3328 (let* ((window
3329 (cond
3330 ((or (not window-or-frame)
3331 (frame-live-p window-or-frame))
3332 (frame-root-window window-or-frame))
3333 ((or (window-live-p window-or-frame)
3334 (window-child window-or-frame))
3335 window-or-frame)
3336 (t
3337 (error "Not a window or frame %s" window-or-frame))))
3338 (frame (window-frame window)))
3339 ;; Balance vertically.
3340 (resize-window-reset (window-frame window))
3341 (balance-windows-1 window)
3342 (resize-window-apply frame)
3343 ;; Balance horizontally.
3344 (resize-window-reset (window-frame window) t)
3345 (balance-windows-1 window t)
3346 (resize-window-apply frame t)))
3347
3348 (defun window-fixed-size-p (&optional window direction)
3349 "Return t if WINDOW cannot be resized in DIRECTION.
3350 WINDOW defaults to the selected window. DIRECTION can be
3351 nil (i.e. any), `height' or `width'."
3352 (with-current-buffer (window-buffer window)
3353 (when (and (boundp 'window-size-fixed) window-size-fixed)
3354 (not (and direction
3355 (member (cons direction window-size-fixed)
3356 '((height . width) (width . height))))))))
3357
3358 ;;; A different solution to balance-windows.
3359 (defvar window-area-factor 1
3360 "Factor by which the window area should be over-estimated.
3361 This is used by `balance-windows-area'.
3362 Changing this globally has no effect.")
3363 (make-variable-buffer-local 'window-area-factor)
3364
3365 (defun balance-windows-area-adjust (window delta horizontal)
3366 "Wrapper around `resize-window' with error checking.
3367 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
3368 ;; `resize-window' may fail if delta is too large.
3369 (while (>= (abs delta) 1)
3370 (condition-case nil
3371 (progn
3372 (resize-window window delta horizontal)
3373 (setq delta 0))
3374 (error
3375 ;;(message "adjust: %s" (error-message-string err))
3376 (setq delta (/ delta 2))))))
3377
3378 (defun balance-windows-area ()
3379 "Make all visible windows the same area (approximately).
3380 See also `window-area-factor' to change the relative size of
3381 specific buffers."
3382 (interactive)
3383 (let* ((unchanged 0) (carry 0) (round 0)
3384 ;; Remove fixed-size windows.
3385 (wins (delq nil (mapcar (lambda (win)
3386 (if (not (window-fixed-size-p win)) win))
3387 (window-list nil 'nomini))))
3388 (changelog nil)
3389 next)
3390 ;; Resizing a window changes the size of surrounding windows in complex
3391 ;; ways, so it's difficult to balance them all. The introduction of
3392 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
3393 ;; very difficult to do. `balance-window' above takes an off-line
3394 ;; approach: get the whole window tree, then balance it, then try to
3395 ;; adjust the windows so they fit the result.
3396 ;; Here, instead, we take a "local optimization" approach, where we just
3397 ;; go through all the windows several times until nothing needs to be
3398 ;; changed. The main problem with this approach is that it's difficult
3399 ;; to make sure it terminates, so we use some heuristic to try and break
3400 ;; off infinite loops.
3401 ;; After a round without any change, we allow a second, to give a chance
3402 ;; to the carry to propagate a minor imbalance from the end back to
3403 ;; the beginning.
3404 (while (< unchanged 2)
3405 ;; (message "New round")
3406 (setq unchanged (1+ unchanged) round (1+ round))
3407 (dolist (win wins)
3408 (setq next win)
3409 (while (progn (setq next (next-window next))
3410 (window-fixed-size-p next)))
3411 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
3412 (let* ((horiz
3413 (< (car (window-edges win)) (car (window-edges next))))
3414 (areadiff (/ (- (* (window-height next) (window-width next)
3415 (buffer-local-value 'window-area-factor
3416 (window-buffer next)))
3417 (* (window-height win) (window-width win)
3418 (buffer-local-value 'window-area-factor
3419 (window-buffer win))))
3420 (max (buffer-local-value 'window-area-factor
3421 (window-buffer win))
3422 (buffer-local-value 'window-area-factor
3423 (window-buffer next)))))
3424 (edgesize (if horiz
3425 (+ (window-height win) (window-height next))
3426 (+ (window-width win) (window-width next))))
3427 (diff (/ areadiff edgesize)))
3428 (when (zerop diff)
3429 ;; Maybe diff is actually closer to 1 than to 0.
3430 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
3431 (when (and (zerop diff) (not (zerop areadiff)))
3432 (setq diff (/ (+ areadiff carry) edgesize))
3433 ;; Change things smoothly.
3434 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
3435 (if (zerop diff)
3436 ;; Make sure negligible differences don't accumulate to
3437 ;; become significant.
3438 (setq carry (+ carry areadiff))
3439 ;; This used `adjust-window-trailing-edge' before and uses
3440 ;; `resize-window' now. Error wrapping is still needed.
3441 (balance-windows-area-adjust win diff horiz)
3442 ;; (sit-for 0.5)
3443 (let ((change (cons win (window-edges win))))
3444 ;; If the same change has been seen already for this window,
3445 ;; we're most likely in an endless loop, so don't count it as
3446 ;; a change.
3447 (unless (member change changelog)
3448 (push change changelog)
3449 (setq unchanged 0 carry 0)))))))
3450 ;; We've now basically balanced all the windows.
3451 ;; But there may be some minor off-by-one imbalance left over,
3452 ;; so let's do some fine tuning.
3453 ;; (bw-finetune wins)
3454 ;; (message "Done in %d rounds" round)
3455 ))
3456
3457 \f
3458 \f
3459 (defconst display-buffer-default-specifiers
3460 '((reuse-window nil same visible)
3461 (pop-up-window (largest . nil) (lru . nil))
3462 (pop-up-frame)
3463 (pop-up-frame-alist
3464 (height . 24) (width . 80) (unsplittable . t))
3465 (reuse-window nil other visible)
3466 (reuse-window-even-sizes . t))
3467 "Buffer display default specifiers.
3468 The value specified here is used when no other specifiers have
3469 been specified by the user or the application. Consult the
3470 documentation of `display-buffer-alist' for a description of
3471 buffer display specifiers.")
3472
3473 (defconst display-buffer-macro-specifiers
3474 '((same-window
3475 ;; Use the same window.
3476 (reuse-window same nil nil))
3477 (same-frame
3478 ;; Avoid other frames.
3479 (reuse-window nil same nil)
3480 (pop-up-window (largest . nil) (lru . nil))
3481 (reuse-window nil other nil))
3482 (other-window
3483 ;; Avoid selected window.
3484 (reuse-window other same visible)
3485 (pop-up-window (largest . nil) (lru . nil))
3486 (pop-up-frame)
3487 (reuse-window other other visible))
3488 (same-frame-other-window
3489 ;; Avoid other frames and selected window.
3490 (reuse-window other same nil)
3491 (pop-up-window (largest . nil) (lru . nil))
3492 (reuse-window other other nil))
3493 (other-frame
3494 ;; Avoid selected frame.
3495 (reuse-window nil same other)
3496 (pop-up-frame)
3497 (reuse-window nil other other))
3498 (default
3499 ;; The default specifiers.
3500 display-buffer-default-specifiers))
3501 "Buffer display macro specifiers.")
3502
3503 (defcustom display-buffer-alist
3504 '((((regexp . ".*"))
3505 reuse-window (reuse-window nil same visible)
3506 pop-up-window
3507 (pop-up-window (largest . nil) (lru . nil))
3508 reuse-window (reuse-window other other nil)
3509 (reuse-window-even-sizes . t)))
3510 "List associating buffer identifiers with display specifiers.
3511 The car of each element of this list is built from a set of cons
3512 cells called buffer identifiers. `display-buffer' shows a buffer
3513 according to the display specifiers in the element's cdr
3514 \(elements are true lists) if at least one of the identifiers
3515 matches the first or third argument of `display-buffer'. Such a
3516 match occurs in one of the following three cases:
3517
3518 - The car of the buffer identifier is the symbol `name' and its
3519 cdr is a string equalling the name of the buffer specified by
3520 the first \(BUFFER-OR-NAME) argument of `display-buffer'.
3521
3522 - The car is the symbol `regexp' and the cdr is a regular
3523 expression matching the name of the buffer specified by the
3524 first \(BUFFER-OR-NAME) argument of `display-buffer'.
3525
3526 - The car is the symbol `label' and the cdr is a symbol equalling
3527 the third \(LABEL) argument of `display-buffer'.
3528
3529 Display specifiers are either symbols, cons cells, or lists.
3530 Five specifiers have been reserved to indicate the basic method
3531 for displaying the buffer: `reuse-window', `pop-up-window',
3532 `pop-up-frame', `use-side-window', and `fun-with-args'.
3533
3534 A list whose car is the symbol `reuse-window' indicates that an
3535 existing window shall be reused for displaying the buffer. The
3536 second element of this list specifies the window to use and can
3537 be one of the following symbols:
3538
3539 nil stands for any window.
3540
3541 `same' stands for the selected window.
3542
3543 `other' stands for any but the selected window.
3544
3545 The third element specifies whether the buffer shown in a window
3546 that shall be reused must be the same buffer that shall be
3547 displayed or another buffer and can be one of the following:
3548
3549 nil means to not care about the window's buffer.
3550
3551 `same' means the window must show the buffer already.
3552
3553 `other' means the window must not show the buffer yet.
3554
3555 The fourth element specifies the set of frames to search for a
3556 suitable window and can be one of the following:
3557
3558 nil to reuse a window on the selected frame.
3559
3560 `visible' to search visible frames on the current terminal.
3561
3562 `other' stands for any visible frame but the selected one.
3563
3564 0 \(the number zero) to search visible and iconified frames on
3565 the current terminal.
3566
3567 t to search arbitrary frames including invisible ones.
3568
3569 If more than one window fits the constraints imposed by these
3570 elements, the least recently used candidate is chosen. A side
3571 window is never reused unless it already shows the buffer.
3572
3573 The following two specifiers are useful when the method equals
3574 `reuse-window':
3575
3576 - A cons cell whose car is the symbol `reuse-window-even-sizes'
3577 and whose cdr is non-nil means to even out the sizes of a
3578 reused window and the selected window provided they (1) appear
3579 adjacent to each other and (2) the selected window is larger
3580 than the window chosen. If the cdr is nil, this means that the
3581 window sizes are left alone.
3582
3583 - A cons cell whose car is the symbol `reuse-window-dedicated'
3584 and whose cdr is non-nil means that a window can be reused even
3585 if it's weakly dedicated to its buffer. If the cdr is t, a
3586 strongly dedicated window can be reused to show the buffer.
3587 Any other non-nil value means only weakly dedicated windows can
3588 be reused. If the cdr is nil, dedicated windows are not
3589 reused.
3590
3591 This specifier should be used in emergency cases only since
3592 windows are usually made dedicated in order to prevent
3593 `display-buffer' from reusing them.
3594
3595 A list whose car is the symbol `pop-up-window' and whose cdr is
3596 built from cons cells representing window/side tuples indicates
3597 that a new window shall be made for displaying the buffer on the
3598 selected frame.
3599
3600 Window/side tuples are cons cells. The car of such a tuple
3601 identifies the window that shall be split. Possible values are
3602 `largest', `lru', `selected', and `root' to split the largest,
3603 least recently used, selected or root window of the selected
3604 frame.
3605
3606 If the frame has side windows, these values do allow to split
3607 only the selected frame's main window or one of its subwindows.
3608 Setting the car to one of `left', `top', `right' and `bottom'
3609 splits the corresponding side window, provided such a window
3610 exists.
3611
3612 The cdr of each pair specifies on which side of the window to
3613 split the new window shall appear and can be one of `below',
3614 `right', `above', or `left' with the obvious meanings. If the
3615 cdr is nil, the window is split in a fashion suitable for its
3616 current dimensions. If the cdr specifies a function, that
3617 function is called with one argument - the window to split. The
3618 function is supposed to split that window and return the new
3619 window.
3620
3621 `display-buffer' scans these tuples until it can either produce a
3622 suitable window or fails. The default value for
3623 `display-buffer-alist' contains the tuples \(largest . nil) and
3624 \(lru . nil) in order to split the largest window first and, if
3625 that fails, the least recently used one.
3626
3627 The following specifiers are useful if the method specifier is
3628 `pop-up-window'.
3629
3630 - A cons cell whose car is the symbol `pop-up-window-min-height'
3631 specifiies the minimum height of the new window. If the cdr is
3632 an integer number, it specifies the minimum number of lines of
3633 the window. A floating point number gives the minimum fraction
3634 of the window height with respect to the height of the frame's
3635 root window. A new window is created only if it can be made at
3636 least as high as specified by the number. If the cdr is nil,
3637 this means to use the value of `window-min-height'.
3638
3639 - A cons cell whose car is the symbol `pop-up-window-min-width'
3640 specifies the minimum width of the new window. If the cdr is
3641 an integer number, it specifies the minimum number of columns
3642 of the window. A floating point number gives the minimum
3643 fraction of the window width with respect to the width of the
3644 frame's root window. A new window is created only if it can be
3645 made at least as wide as specified by the number. If the cdr
3646 is nil, this means to use the value of `window-min-width'.
3647
3648 - A cons cell whose car is `pop-up-window-set-height' with
3649 the following interpretations for the cdr:
3650
3651 - nil means leave the height of the new window alone.
3652
3653 - A number specifies the desired height of the new window. An
3654 integer number specifies the number of lines of the window.
3655 A floating point number gives the fraction of the window
3656 height with respect to the height of the frame's root window.
3657
3658 - If the cdr specifies a function, that function is called with
3659 one argument - the new window. The function is supposed to
3660 adjust the height of the window; its return value is ignored.
3661 Suitable functions are `shrink-window-if-larger-than-buffer'
3662 and `fit-window-to-buffer'.
3663
3664 - A cons cell whose car equals `pop-up-window-set-width' with
3665 the following interpretations for the cdr:
3666
3667 - nil means leave the width of the new window alone.
3668
3669 - A number specifies the desired width of the new window. An
3670 integer number specifies the number of columns of the window.
3671 A floating point number gives the fraction of the window
3672 width with respect to the width of the frame's root window.
3673
3674 - If the cdr specifies a function, that function is called with
3675 one argument - the new window. The function is supposed to
3676 adjust the width of the window; its return value is ignored.
3677
3678 Observe that specifying `pop-up-window-set-height' or
3679 `pop-up-window-set-width' may override restrictions given by
3680 the `pop-up-window-min-height' and `pop-up-window-min-width'
3681 specifiers.
3682
3683 - A cons cell whose car is `pop-up-window-split-unsplittable' and
3684 whose cdr is non-nil allows to make a new window on an
3685 unsplittable frame. If the cdr is nil, unsplittable frames are
3686 not split. This specifier should be used in special cases only
3687 since frames are usually made unsplittable in order to prevent
3688 `display-buffer' from splitting them.
3689
3690 A list whose car is the symbol `pop-up-frame' specifies that a
3691 new frame shall be made for displaying the buffer. The second
3692 element, if non-nil, allows popping up a new frame on graphic
3693 displays only.
3694
3695 The following specifiers are useful if the method specifier is
3696 `pop-up-frame'.
3697
3698 - A list whose car is the symbol `pop-up-frame-function' together
3699 with a valid function as cdr specifies the function for
3700 creating a new frame. If the cdr is nil, the default function
3701 `make-frame' is called. The function is called with the
3702 parameters and values provided by the specifier described next.
3703
3704 - A list whose car is the symbol `pop-up-frame-alist' followed by
3705 an arbitrary number of frame parameter/value tuples, each given
3706 as a cons cell, specifies the parameters passed to the pop-up
3707 frame function.
3708
3709 A list of three elements whose car is the symbol
3710 `use-side-window' specifies that the buffer shall be displayed in
3711 a side window of the selected frame. The second element denotes
3712 the side of the frame where the window appears or shall be made.
3713 The third element denotes the slot used by the window. If a side
3714 window with the specified slot exists already, that window is
3715 reused. If no such window exists it is created.
3716
3717 The following specifiers are useful in connection with the
3718 `use-side-window' method specifier: `reuse-window-dedicated',
3719 `pop-up-window-min-height', `pop-up-window-min-width',
3720 `pop-up-window-set-height' and `pop-up-window-set-width'.
3721
3722 A list whose car is the symbol `fun-with-args' specifies that the
3723 function specified in the second element of the list is
3724 responsible for displaying the buffer. `display-buffer' calls
3725 this function with the buffer as first argument and the remaining
3726 elements of the list as second argument.
3727
3728 The function should choose or create a window, display the buffer
3729 in it, and return the window. It is also responsible for giving
3730 the variable `display-buffer-window' and the `quit-restore'
3731 parameter of the window used a meaningful value.
3732
3733 Within the body of this function avoid calling `display-buffer'
3734 with the same buffer as argument since this may lead to endless
3735 recursion.
3736
3737 Instead of supplying basic method specifiers, it's sometimes more
3738 convenient to use macro specifiers. They provide some commonly
3739 used display methods but do not support the fine control provided
3740 by the basic method specifiers. Macro specifiers are symbols.
3741 The following macro specifiers are provided:
3742
3743 `same-window' to display the buffer in the selected window.
3744
3745 `same-frame' to display the buffer on the selected frame.
3746
3747 `other-window' to display the buffer in any window but the
3748 selected one.
3749
3750 `same-frame-other-window' as `other-window' but stay on the
3751 selected frame.
3752
3753 `other-frame' to display the buffer on another visible
3754 frame.
3755
3756 `default' to use the default value of `display-buffer-alist'.
3757
3758 One specifier is useful with any method specifier: A list whose
3759 car is the symbol `dedicate' and whose cdr is non-nil will
3760 dedicate the window to its buffer. The following values are
3761 supported:
3762
3763 - nil to not dedicate the window to the buffer.
3764
3765 - `weak' to weakly dedicate the window to the buffer.
3766
3767 - t to strongly dedicate the window to the buffer.
3768
3769 Usually, applications are free to override the specifiers of
3770 `display-buffer-alist' by passing their own specifiers as second
3771 argument of `display-buffer'. For every `display-buffer-alist'
3772 entry you can, however, add a cons cell whose car is the symbol
3773 `override' and whose cdr is non-nil, to explicitly override any
3774 value supplied by the application.
3775
3776 Overriding specifiers supplied by the calling application is, in
3777 general, not advisable. It permits, for example, to change the
3778 semantics of a function like `display-buffer-other-window' by
3779 using the location specifiers `same-window' or `other-frame'."
3780 :risky t
3781 :type
3782 '(repeat
3783 :offset 9
3784 ;; Associations of buffer identifiers and display specifiers.
3785 (list
3786 :format "%v"
3787 ;; Buffer identifiers.
3788 (repeat
3789 :tag "Buffer identifiers"
3790 (choice
3791 :tag "Identifier"
3792 :format "%[%t%] %v" :size 15
3793 (cons
3794 :tag "Name"
3795 :format "%v"
3796 :help-echo "A buffer name."
3797 (const :format "" name)
3798 (string :format "Name: %v\n" :size 32))
3799 (cons
3800 :tag "Regexp"
3801 :format "%v"
3802 :help-echo "A regular expression matching buffer names."
3803 (const :format "" regexp)
3804 (string :format "Regexp: %v\n" :size 32))
3805 (cons
3806 :tag "Label"
3807 :format "%v"
3808 :help-echo "A symbol equalling the buffer display label."
3809 (const :format "" symbol)
3810 (symbol :format "Label: %v\n" :size 32))))
3811
3812 ;; Display specifiers.
3813 (repeat
3814 :offset 9
3815 :tag "Display specifiers"
3816 :inline t
3817 (list
3818 :inline t
3819 :format "%v"
3820 (choice
3821 :tag "Method"
3822 :value (reuse-window
3823 (reuse-window nil same nil)
3824 (reuse-window-even-sizes . t))
3825 :inline t
3826 :help-echo "Method for displaying the buffer."
3827 :format "%[Method%] %v" :size 15
3828
3829 ;; Reuse window specifiers.
3830 (list
3831 :tag "Reuse window"
3832 :value (reuse-window
3833 (reuse-window nil same nil)
3834 (reuse-window-even-sizes . t))
3835 :format "%t\n%v"
3836 :inline t
3837 ;; For customization purposes only.
3838 (const :format "" reuse-window)
3839 (set
3840 :format "%v"
3841 :inline t
3842 ;; The window to reuse.
3843 (list
3844 :format "%v\n"
3845 (const :format "" reuse-window)
3846 ;; The window type.
3847 (choice
3848 :tag "Window"
3849 :help-echo "Window to reuse."
3850 :value nil
3851 :format "%[Window%] %v" :size 15
3852 (const :tag "Any" :format "%t" nil)
3853 (const :tag "Selected only" :format "%t" same)
3854 (const :tag "Any but selected" :format "%t" other))
3855 ;; The window's buffer.
3856 (choice
3857 :tag "Buffer"
3858 :help-echo "Buffer shown by reused window."
3859 :value t
3860 :format " %[Buffer%] %v" :size 15
3861 (const :tag "Any buffer" :format "%t" nil)
3862 (const :tag "Same buffer" :format "%t" same)
3863 (const :tag "Other buffer" :format "%t" other))
3864 ;; The window's frame.
3865 (choice
3866 :help-echo "Frame to search for a window to reuse."
3867 :tag "Frame"
3868 :value nil
3869 :format " %[Frame%] %v" :size 15
3870 (const :tag "Selected frame only" :format "%t" nil)
3871 (const :tag "Visible frames" :format "%t" visible)
3872 (const :tag "Visible but unselected" :format "%t" other)
3873 (const :tag "Visible and iconified" :format "%t" 0)
3874 (const :tag "Any frame" :format "%t" t)))
3875 ;; Whether window sizes should be evened out.
3876 (cons
3877 :format "%v\n"
3878 :tag "Even window sizes"
3879 (const :format "" reuse-window-even-sizes)
3880 (choice
3881 :tag "Even window sizes"
3882 :help-echo "Whether to even sizes of selected and reused window."
3883 :value t
3884 :format "%[Even window sizes%] %v" :size 15
3885 (const :tag "Off" :format "%t" nil)
3886 (const :tag "Even window sizes" :format "%t" t)))
3887 ;; Whether to reuse a dedicated window
3888 (cons
3889 :format "%v\n"
3890 (const :format "" reuse-window-dedicated)
3891 (choice
3892 :tag "Reuse dedicated window" :value nil
3893 :help-echo "Reuse a window even if it is dedicated to its buffer."
3894 :format "%[Reuse dedicated window%] %v" :size 15
3895 (const :tag "Off" :format "%t" nil)
3896 (const :tag "Reuse weakly dedicated windows" :format "%t" weak)
3897 (const :tag "Reuse any dedicated window" :format "%t" t)))))
3898
3899 ;; Pop-up window specifiers.
3900 (list
3901 :tag "Pop-up window"
3902 :value (pop-up-window (pop-up-window (largest . nil) (lru . nil)))
3903 :format "%t\n%v"
3904 :inline t
3905 (const :format "" pop-up-window)
3906 (set
3907 :format "%v"
3908 :inline t
3909 ;; Pop-up window list.
3910 (list
3911 :format "%v"
3912 :value (pop-up-window (largest . nil) (lru . nil))
3913 (const :format "" pop-up-window)
3914 (repeat
3915 :tag "Window / Side tuples"
3916 :inline t
3917 (cons
3918 :format "%v\n"
3919 (choice
3920 :tag "Window"
3921 :help-echo "The window to split."
3922 :value largest
3923 :format "%[Window%] %v"
3924 (const :tag "Largest" :format "%t" largest)
3925 (const :tag "Least recently used" :format "%t" lru)
3926 (const :tag "Selected" :format "%t" selected)
3927 (const :tag "Root" :format "%t" root)
3928 (const :tag "Left" :format "%t" left)
3929 (const :tag "Top" :format "%t" top)
3930 (const :tag "Right" :format "%t" right)
3931 (const :tag "Bottom" :format "%t" bottom))
3932 (choice
3933 :tag "Side"
3934 :help-echo "The position of the new window with respect to the window to split."
3935 :value nil
3936 :format " %[Side%] %v"
3937 (const :tag "Dynamic" :format "%t" nil)
3938 (const :tag "Below" :format "%t" below)
3939 (const :tag "Right" :format "%t" right)
3940 (const :tag "Above" :format "%t" above)
3941 (const :tag "Left" :format "%t" left)
3942 (function
3943 :tag "Function" :format "%v" :size 25)))))
3944 ;; Minimum height of pop-up windows.
3945 (cons
3946 :format "%v\n"
3947 (const :format "" pop-up-window-min-height)
3948 (choice
3949 :help-echo "Minimum height of popped-up window."
3950 :format "%[Minimum height%] %v"
3951 (const :tag "Default" :format "%t" :value nil)
3952 (integer :tag "Number of lines" :value 12 :size 5)
3953 (float :tag "Fraction of frame height" :value .25 :size 5)))
3954 ;; Minimum width of pop-up windows.
3955 (cons
3956 :format "%v\n"
3957 (const :format "" pop-up-window-min-width)
3958 (choice
3959 :help-echo "Minimum width of popped-up window."
3960 :format "%[Minimum width%] %v"
3961 (const :tag "Default" :format "%t" :value nil)
3962 (integer :tag "Number of columns" :value 12 :size 5)
3963 (float :tag "Fraction of frame width" :value .25 :size 5)))
3964 ;; Desired height of pop-up windows.
3965 (cons
3966 :format "%v\n"
3967 (const :format "" pop-up-window-set-height)
3968 (choice
3969 :help-echo "Desired height of popped-up window."
3970 :format "%[Desired height%] %v"
3971 (const :tag "Default" :format "%t" :value nil)
3972 (integer :tag "Number of lines" :value 12 :size 5)
3973 (float :tag "Fraction of frame height" :value .25 :size 5)
3974 (function :tag "Function" :size 25)))
3975 ;; Desired width of pop-up windows.
3976 (cons
3977 :format "%v\n"
3978 (const :format "" pop-up-window-set-width)
3979 (choice
3980 :help-echo "Desired width of popped-up window."
3981 :format "%[Desired width%] %v"
3982 (const :tag "Default" :format "%t" :value nil)
3983 (integer :tag "Number of column" :value 12 :size 5)
3984 (float :tag "Fraction of frame width" :value .25 :size 5)
3985 (function :tag "Function" :size 25)))
3986 ;; Split unsplittable frames.
3987 (cons
3988 :format "%v\n"
3989 (const :format "" pop-up-window-unsplittable)
3990 (choice
3991 :help-echo "Allow popping up a window on \"unsplittable\" frames."
3992 :format "%[Split unsplittable frame%] %v"
3993 (const :tag "Off" :format "%t" nil)
3994 (const :tag "Allow" :format "%t" t)))))
3995
3996 ;; Pop-up frame specifiers.
3997 (list
3998 :tag "Pop-up frame"
3999 :value (pop-up-frame
4000 (pop-up-frame)
4001 (pop-up-frame-alist
4002 (height . 24) (width . 80) (unsplittable . t)))
4003 :format "%t\n%v"
4004 :inline t
4005 (const :format "" pop-up-frame)
4006 (set
4007 :format "%v"
4008 :inline t
4009 ;; Pop-up frame.
4010 (list
4011 :tag "Pop-up a new frame"
4012 :value (pop-up-frame)
4013 :format "%v"
4014 (const :format "" pop-up-frame)
4015 (choice
4016 :tag "Pop-up a new frame"
4017 :help-echo "Whether to pop-up a new frame on a display."
4018 :format "%[Display%] %v\n" :size 15
4019 (const :tag "On any display" :format "%t" nil)
4020 (const :tag "On graphic displays only" :format "%t" t)))
4021 ;; Pop-up frame function.
4022 (cons
4023 :format "%v\n"
4024 (const :format "" pop-up-frame-function)
4025 (choice
4026 :tag "Pop-up frame function"
4027 :value nil
4028 :help-echo "Function to use to pop-up a new frame."
4029 :format "%[Function%] %v" :size 15
4030 (const :tag "Default" :format "%t" nil)
4031 (function
4032 :value make-frame
4033 :format "%t: %v"
4034 :size 25)))
4035 ;; Pop-up frame alist.
4036 (list
4037 :format "%v"
4038 (const :format "" pop-up-frame-alist)
4039 (repeat
4040 :tag "Parameter / Value tuples"
4041 :inline t
4042 (cons
4043 :format "%v\n"
4044 (symbol
4045 :tag "Parameter"
4046 :format "Parameter: %v"
4047 :size 16)
4048 (sexp
4049 :tag "Value"
4050 :format " Value: %v"
4051 :size 8))))))
4052
4053 ;; Use side-window specifiers.
4054 (list
4055 :tag "Use side-window"
4056 :value (use-side-window (use-side-window bottom 0))
4057 :format "%t\n%v"
4058 :inline t
4059 ;; For customization purposes only.
4060 (const :format "" use-side-window)
4061 (set
4062 :format "%v"
4063 :inline t
4064 ;; Side and slot.
4065 (list
4066 :format "%v\n"
4067 :value (use-side-window bottom 0)
4068 (const :format "" use-side-window)
4069 ;; The side.
4070 (choice
4071 :tag "Side"
4072 :help-echo "Side of frame."
4073 :value bottom
4074 :format "%[Side%] %v" :size 15
4075 (const :tag "Left" :format "%t" left)
4076 (const :tag "Top" :format "%t" top)
4077 (const :tag "Right" :format "%t" right)
4078 (const :tag "Bottom" :format "%t" bottom))
4079 ;; The slot
4080 (number
4081 :tag "Slot"
4082 :help-echo "The slot (an arbitrary number, where 0 stands for the center slot)."
4083 :value 0
4084 :format " Slot: %v" :size 8))
4085 ;; Whether to reuse a dedicated side window
4086 (cons
4087 :format "%v\n"
4088 (const :format "" reuse-window-dedicated)
4089 (choice
4090 :tag "Reuse dedicated side window" :value nil
4091 :help-echo "Reuse a side window even if it is dedicated to its buffer."
4092 :format "%[Reuse dedicated side window%] %v" :size 15
4093 (const :tag "Off" :format "%t" nil)
4094 (const :tag "Reuse weakly dedicated side windows" :format "%t" weak)
4095 (const :tag "Reuse any dedicated side window" :format "%t" t)))
4096 ;; Minimum height of pop-up side windows.
4097 (cons
4098 :format "%v\n"
4099 (const :format "" pop-up-window-min-height)
4100 (choice
4101 :help-echo "Minimum height of popped-up side window."
4102 :format "%[Minimum height%] %v"
4103 (const :tag "Default" :format "%t" :value nil)
4104 (integer :tag "Number of lines" :value 12 :size 5)
4105 (float :tag "Fraction of frame height" :value .25 :size 5)))
4106 ;; Minimum width of pop-up windows.
4107 (cons
4108 :format "%v\n"
4109 (const :format "" pop-up-window-min-width)
4110 (choice
4111 :help-echo "Minimum width of popped-up side window."
4112 :format "%[Minimum width%] %v"
4113 (const :tag "Default" :format "%t" :value nil)
4114 (integer :tag "Number of columns" :value 12 :size 5)
4115 (float :tag "Fraction of frame width" :value .25 :size 5)))
4116 ;; Desired height of pop-up windows.
4117 (cons
4118 :format "%v\n"
4119 (const :format "" pop-up-window-set-height)
4120 (choice
4121 :help-echo "Desired height of popped-up side window."
4122 :format "%[Desired height%] %v"
4123 (const :tag "Default" :format "%t" :value nil)
4124 (integer :tag "Number of lines" :value 12 :size 5)
4125 (float :tag "Fraction of frame height" :value .25 :size 5)
4126 (function :tag "Function" :size 25)))
4127 ;; Desired width of pop-up windows.
4128 (cons
4129 :format "%v\n"
4130 (const :format "" pop-up-window-set-width)
4131 (choice
4132 :help-echo "Desired width of popped-up side window."
4133 :format "%[Desired width%] %v"
4134 (const :tag "Default" :format "%t" :value nil)
4135 (integer :tag "Number of column" :value 12 :size 5)
4136 (float :tag "Fraction of frame width" :value .25 :size 5)
4137 (function :tag "Function" :size 25)))))
4138
4139 ;; Function with argument specifiers.
4140 (list
4141 :tag "Function with arguments"
4142 :value (fun-with-args (fun-with-args 'ignore))
4143 :format "%t\n%v"
4144 :inline t
4145 ;; For customization purposes only.
4146 (const :format "" fun-with-args)
4147 (set
4148 :format "%v"
4149 :inline t
4150 (list
4151 :format "%v"
4152 :value (fun-with-args 'ignore)
4153 (const :format "" fun-with-args)
4154 (function :tag "Function" :format "%t: %v\n" :size 25)
4155 (list
4156 :format "%v"
4157 (repeat
4158 :tag "Arguments"
4159 :inline t
4160 (sexp
4161 :format "%v\n"
4162 :size 16))))))
4163
4164 ;; Macro specifiers.
4165 (list
4166 :tag "Same frame only"
4167 :format "%t%v"
4168 :inline t
4169 (const :format "\n" same-frame))
4170 (list
4171 :tag "Other window"
4172 :format "%t%v"
4173 :inline t
4174 (const :format "\n" other-window))
4175 (list
4176 :tag "Same frame other window"
4177 :format "%t%v"
4178 :inline t
4179 (const :format "\n" same-frame-other-window))
4180 (list
4181 :tag "Other frame only"
4182 :format "%t%v"
4183 :inline t
4184 (const :format "\n" other-frame))
4185 (list
4186 :tag "Default"
4187 :format "%t%v"
4188 :inline t
4189 (const :format "\n" default)))))
4190
4191 (set
4192 :format "%v"
4193 :inline t
4194 ;; Dedicate window to buffer.
4195 (cons
4196 :format "%v"
4197 (const :format "" dedicate)
4198 (choice
4199 :help-echo "Mark window as dedicated to its buffer."
4200 :format "%[Dedicate window to buffer%] %v\n" :size 15
4201 (const :tag "Off" :format "%t" nil)
4202 (const :tag "Weak" :format "%t" weak)
4203 (const :tag "Strong" :format "%t" t)))
4204 ;; No other window.
4205 (cons
4206 :format "%v"
4207 (const :format "" no-other-window)
4208 (choice
4209 :help-echo "Whether `other-window' shall ignore the window."
4210 :format "%[No other window%] %v\n" :size 15
4211 (const :tag "Off" :format "%t" nil)
4212 (const :tag "Ignore" :format "%t" t)))
4213 ;; Overriding.
4214 (cons
4215 :format "%v\n"
4216 (const :format "" override)
4217 (choice
4218 :help-echo "Override application supplied specifiers."
4219 :format "%[Override%] %v"
4220 (const :tag "Off" :format "%t" nil)
4221 (const :tag "Override" :format "%t" t))))))
4222 :group 'windows
4223 :group 'frames)
4224
4225 (defcustom display-buffer-function nil
4226 "If non-nil, function to call to display a buffer.
4227 `display-buffer' calls this function with two arguments, the
4228 buffer to display and a list of buffer display specifiers, see
4229 `display-buffer-alist'.
4230
4231 The function is supposed to choose or create a window, display
4232 the specified buffer in it, and return the window. It is also
4233 responsible for giving the variable `display-buffer-window' and
4234 the `quit-restore' parameter of the window used a meaningful
4235 value.
4236
4237 The function specified here overrides all specifiers of the
4238 variable `display-buffer-alist' any specifiers passed to
4239 `display-buffer'.
4240
4241 If you call `display-buffer' within the body of the function,
4242 bind the value of `display-buffer-function' to nil around that
4243 call to avoid that the function recursively calls itself."
4244 :type '(choice
4245 (const nil)
4246 (function :tag "Function"))
4247 :group 'windows)
4248
4249 ;; The following is a global variable which is used externally (by
4250 ;; help.el) to (1) know which window was used for displaying a buffer
4251 ;; and (2) whether the window was new or reused.
4252 (defvar display-buffer-window nil
4253 "Window used by `display-buffer' and related information.
4254 After `display-buffer' displays a buffer in some window this
4255 variable is a cons cell whose car denotes the window used to
4256 display the buffer. The cdr is supposed to be one of the symbols
4257 `reuse-buffer-window', `reuse-other-window', `new-window' or
4258 `new-frame'.
4259
4260 If the buffer display location specifier is one of 'same-window,
4261 'same-frame, or 'other-frame, the `display-buffer' routines
4262 assign the value of this variable. If the location specifier is
4263 a function, that function becomes responsible for assigning a
4264 meaningful value to this variable. See the functions
4265 `display-buffer-reuse-window', `display-buffer-pop-up-window' and
4266 `display-buffer-pop-up-frame' for how this can be done.")
4267
4268 (defun display-buffer-even-window-sizes (window specifiers)
4269 "Even sizes of WINDOW and selected window according to SPECIFIERS.
4270 SPECIFIERS must be a list of buffer display specifiers, see the
4271 documentation of `display-buffer-alist' for a description.
4272
4273 Sizes are evened out if and only if WINDOW and the selected
4274 window appear next to each other and the selected window is
4275 larger than WINDOW."
4276 (cond
4277 ((or (not (cdr (assq 'reuse-window-even-sizes specifiers)))
4278 ;; Don't resize minibuffer windows.
4279 (window-minibuffer-p)
4280 ;; WINDOW must be adjacent to the selected one.
4281 (not (or (eq window (window-prev))
4282 (eq window (window-next))))))
4283 ((and (window-iso-combined-p window)
4284 ;; Resize iff the selected window is higher than WINDOW.
4285 (> (window-total-height) (window-total-height window)))
4286 ;; Don't throw an error if we can't even window heights for
4287 ;; whatever reason. In any case, enlarging the selected window
4288 ;; might fail anyway if there are other windows above or below
4289 ;; WINDOW and the selected one. But for a simple two windows
4290 ;; configuration the present behavior is good enough so why care?
4291 (ignore-errors
4292 (resize-window
4293 window (/ (- (window-total-height) (window-total-height window))
4294 2))))
4295 ((and (window-iso-combined-p window t)
4296 ;; Resize iff the selected window is wider than WINDOW.
4297 (> (window-total-width) (window-total-width window)))
4298 ;; Don't throw an error if we can't even window widths, see
4299 ;; comment above.
4300 (ignore-errors
4301 (resize-window
4302 window (/ (- (window-total-width) (window-total-width window))
4303 2) t)))))
4304
4305 (defun display-buffer-set-height (window specifiers)
4306 "Adjust height of WINDOW according to SPECIFIERS.
4307 SPECIFIERS must be a list of buffer display specifiers, see the
4308 documentation of `display-buffer-alist' for a description."
4309 (let ((set-height (cdr (assq 'pop-up-window-set-height specifiers))))
4310 (cond
4311 ((numberp set-height)
4312 (let* ((height (if (integerp set-height)
4313 set-height
4314 (round
4315 (* (window-total-size (frame-root-window window))
4316 set-height))))
4317 (delta (- height (window-total-size window))))
4318 (when (and (window-resizable-p window delta nil 'safe)
4319 (window-iso-combined-p window))
4320 (resize-window window delta nil 'safe))))
4321 ((functionp set-height)
4322 (ignore-errors (funcall set-height window))))))
4323
4324 (defun display-buffer-set-width (window specifiers)
4325 "Adjust width of WINDOW according to SPECIFIERS.
4326 SPECIFIERS must be a list of buffer display specifiers, see the
4327 documentation of `display-buffer-alist' for a description."
4328 (let ((set-width (cdr (assq 'pop-up-window-set-width specifiers))))
4329 (cond
4330 ((numberp set-width)
4331 (let* ((width (if (integerp set-width)
4332 set-width
4333 (round
4334 (* (window-total-size (frame-root-window window) t)
4335 set-width))))
4336 (delta (- width (window-total-size window t))))
4337 (when (and (window-resizable-p window delta t 'safe)
4338 (window-iso-combined-p window t))
4339 (resize-window window delta t 'safe))))
4340 ((functionp set-width)
4341 (ignore-errors (funcall set-width window))))))
4342
4343 ;; We have to work around the deficiency that the command loop does not
4344 ;; preserve the selected window when it is on a frame that hasn't been
4345 ;; raised or given input focus. So we have to (1) select the window
4346 ;; used for displaying a buffer and (2) raise its frame if necessary,
4347 ;; thus defeating one primary principle of `display-buffer' namely to
4348 ;; _not_ select the window chosen for displaying the buffer :-(
4349 (defun display-buffer-select-window (window &optional norecord)
4350 "Select WINDOW and raise its frame if necessary."
4351 (let ((old-frame (selected-frame))
4352 (new-frame (window-frame window)))
4353 ;; Select WINDOW _before_ raising the frame to assure that the mouse
4354 ;; cursor moves into the correct window.
4355 (select-window window norecord)
4356 (unless (eq old-frame new-frame)
4357 (select-frame-set-input-focus new-frame))))
4358
4359 (defun display-buffer-in-window (buffer window specifiers)
4360 "Display BUFFER in WINDOW and raise its frame if needed.
4361 WINDOW must be a live window and defaults to the selected one.
4362 Return WINDOW.
4363
4364 SPECIFIERS must be a list of buffer display specifiers, see the
4365 documentation of `display-buffer-alist' for a description."
4366 (setq buffer (normalize-live-buffer buffer))
4367 (setq window (normalize-live-window window))
4368 (let* ((old-frame (selected-frame))
4369 (new-frame (window-frame window))
4370 (dedicated (cdr (assq 'dedicated specifiers)))
4371 (no-other-window (cdr (assq 'no-other-window specifiers))))
4372 ;; Show BUFFER in WINDOW.
4373 (set-window-dedicated-p window nil)
4374 (set-window-buffer window buffer)
4375 (when dedicated
4376 (set-window-dedicated-p window dedicated))
4377 (when no-other-window
4378 (set-window-parameter window 'no-other-window t))
4379 (unless (eq old-frame new-frame)
4380 (display-buffer-select-window window))
4381 ;; Return window.
4382 window))
4383
4384 (defun display-buffer-reuse-window (buffer method &optional specifiers)
4385 "Display BUFFER in an existing window.
4386 METHOD must be a list in the form of the cdr of a `reuse-window'
4387 buffer display specifier, see `display-buffer-alist' for an
4388 explanation. The first element must specifiy the window to use,
4389 and can be either nil, `same', `other', or a live window. The
4390 second element must specify the window's buffer and can be either
4391 nil, `same', `other', or a live buffer. The third element is the
4392 frame to use - either nil, 0, `visible', `other', t, or a live
4393 frame.
4394
4395 Optional argument SPECIFIERS must be a list of valid display
4396 specifiers. Return the window chosen to display BUFFER, nil if
4397 none was found."
4398 (let* ((method-window (nth 0 method))
4399 (method-buffer (nth 1 method))
4400 (method-frame (nth 2 method))
4401 (reuse-dedicated (assq 'reuse-window-dedicated specifiers))
4402 windows other-frame dedicated time best-window best-time)
4403 (when (eq method-frame 'other)
4404 ;; `other' is not handled by `window-list-1'.
4405 (setq other-frame t)
4406 (setq method-frame t))
4407 (dolist (window (window-list-1 nil 'nomini method-frame))
4408 (let ((window-buffer (window-buffer window)))
4409 (when (and (not (window-minibuffer-p window))
4410 ;; Don't reuse a side window.
4411 (or (not (eq (window-parameter window 'window-side) 'side))
4412 (eq window-buffer buffer))
4413 (or (not method-window)
4414 (and (eq method-window 'same)
4415 (eq window (selected-window)))
4416 (and (eq method-window 'other)
4417 (not (eq window (selected-window))))
4418 ;; Special case for applications that specifiy
4419 ;; the window explicitly.
4420 (eq method-window window))
4421 (or (not method-buffer)
4422 (and (eq method-buffer 'same)
4423 (eq window-buffer buffer))
4424 (and (eq method-buffer 'other)
4425 (not (eq window-buffer buffer)))
4426 ;; Special case for applications that specifiy
4427 ;; the window's buffer explicitly.
4428 (eq method-buffer window-buffer))
4429 (or (not other-frame)
4430 (not (eq (window-frame window) (selected-frame))))
4431 ;; Handle dedicatedness.
4432 (or (eq window-buffer buffer)
4433 ;; The window does not show the same buffer.
4434 (not (setq dedicated (window-dedicated-p window)))
4435 ;; If the window is weakly dedicated to its
4436 ;; buffer, reuse-dedicated must be non-nil.
4437 (and (not (eq dedicated t)) reuse-dedicated)
4438 ;; If the window is strongly dedicated to its
4439 ;; buffer, reuse-dedicated must be t.
4440 (eq reuse-dedicated t)))
4441 (setq windows (cons window windows)))))
4442
4443 (if (eq method-buffer 'same)
4444 ;; When reusing a window on the same buffer use the lru one.
4445 (dolist (window windows)
4446 (setq time (window-use-time window))
4447 (when (or (not best-window) (< time best-time))
4448 (setq best-window window)
4449 (setq best-time time)))
4450 ;; Otherwise, sort windows according to their use-time.
4451 (setq windows
4452 (sort windows
4453 #'(lambda (window-1 window-2)
4454 (<= (window-use-time window-1)
4455 (window-use-time window-2)))))
4456 (setq best-window
4457 ;; Try to get a full-width window (this is silly and can
4458 ;; get us to another frame but let's ignore these issues
4459 ;; for the moment).
4460 (catch 'found
4461 (dolist (window windows)
4462 (when (window-full-width-p window)
4463 (throw 'found window)))
4464 ;; If there's no full-width window return the lru window.
4465 (car windows))))
4466
4467 (when best-window
4468 (display-buffer-even-window-sizes best-window specifiers)
4469 ;; Never change the quit-restore parameter of a window here.
4470 (if (eq (window-buffer best-window) buffer)
4471 (setq display-buffer-window
4472 (cons best-window 'reuse-buffer-window))
4473 (setq display-buffer-window
4474 (cons best-window 'reuse-other-window))
4475 (unless (window-parameter best-window 'quit-restore)
4476 ;; Don't overwrite an existing quit-restore entry.
4477 (set-window-parameter
4478 best-window 'quit-restore
4479 (list (window-buffer best-window) (window-start best-window)
4480 (window-point best-window) buffer
4481 (window-total-size best-window) (selected-window)))))
4482
4483 (display-buffer-in-window buffer best-window specifiers))))
4484
4485 (defconst display-buffer-split-specifiers '(largest lru selected root left top right bottom)
4486 "List of symbols identifying window that shall be split.")
4487
4488 (defconst display-buffer-side-specifiers '(below right above left nil)
4489 "List of symbols identifying side of split-off window.")
4490
4491 (defun display-buffer-split-window-1 (window side min-size)
4492 "Subroutine of `display-buffer-split-window'."
4493 (let* ((horizontal (memq side '(left right)))
4494 (parent (window-parent window))
4495 (resize (and window-splits (window-iso-combined-p window horizontal)))
4496 (old-size
4497 ;; We either resize WINDOW or its parent.
4498 (window-total-size (if resize parent window) horizontal))
4499 new-size)
4500 ;; We don't call split-window-vertically/-horizontally any more
4501 ;; here. If for some reason it's needed we can always do so
4502 ;; (provided we give it an optional SIDE argument).
4503 (cond
4504 (resize
4505 ;; When we resize a combination, the new window must be at least
4506 ;; MIN-SIZE large after the split.
4507 (setq new-size
4508 (max min-size
4509 (min (- old-size (window-min-size parent horizontal))
4510 (/ old-size
4511 ;; Try to make the size of the new window
4512 ;; proportional to the number of iso-arranged
4513 ;; windows in the combination.
4514 (1+ (window-iso-combinations parent horizontal))))))
4515 (when (window-sizable-p parent (- new-size) horizontal)
4516 (split-window window (- new-size) side)))
4517 ((window-live-p window)
4518 (setq new-size (/ old-size 2))
4519 ;; When WINDOW is live, the old _and_ the new window must be at
4520 ;; least MIN-SIZE large after the split.
4521 (when (and (>= new-size min-size)
4522 (window-sizable-p window (- new-size) horizontal))
4523 ;; Do an even split to make Stepan happy.
4524 (split-window window nil side)))
4525 (t
4526 ;; When WINDOW is internal, the new window must be at least
4527 ;; MIN-SIZE large after the split.
4528 (setq new-size
4529 (max min-size
4530 (/ old-size
4531 ;; Try to make the size of the new window
4532 ;; proportional to the number of iso-arranged
4533 ;; subwindows of WINDOW.
4534 (1+ (window-iso-combinations window horizontal)))))
4535 (when (window-sizable-p window (- new-size) horizontal)
4536 (split-window window (- new-size) side))))))
4537
4538 (defun display-buffer-split-window (window &optional side specifiers)
4539 "Split WINDOW in a way suitable for `display-buffer'.
4540 Optional argument SIDE must be a side specifier \(one of the
4541 symbols below, right, above, left, or nil). SPECIFIERS must be a
4542 list of buffer display specifiers, see the documentation of
4543 `display-buffer-alist' for a description.
4544
4545 Return the new window, nil if it could not be created."
4546 (let ((min-height (cdr (assq 'pop-up-window-min-height specifiers)))
4547 (min-width (cdr (assq 'pop-up-window-min-width specifiers)))
4548 size)
4549 ;; Normalize min-height and min-width, we might need both.
4550 (setq min-height
4551 ;; If min-height is specified, it can be as small as
4552 ;; `window-safe-min-height'.
4553 (cond
4554 ((and (integerp min-height)
4555 (>= min-height window-safe-min-height))
4556 min-height)
4557 ((and (floatp min-height)
4558 (<= min-height 1)
4559 (let* ((root-height (window-total-height
4560 (frame-root-window
4561 (window-frame window))))
4562 (height (round (* min-height root-height))))
4563 (when (>= height window-safe-min-height)
4564 height))))
4565 (t window-min-height)))
4566 (setq min-width
4567 ;; If min-width is specified, it can be as small as
4568 ;; `window-safe-min-width'.
4569 (cond
4570 ((and (integerp min-width)
4571 (>= min-width window-safe-min-width))
4572 min-width)
4573 ((and (floatp min-width)
4574 (<= min-width 1)
4575 (let* ((root-width (window-total-width
4576 (frame-root-window
4577 (window-frame window))))
4578 (width (round (* min-width root-width))))
4579 (when (>= width window-safe-min-width)
4580 width))))
4581 (t window-min-width)))
4582
4583 (or (and (memq side '(nil above below))
4584 (display-buffer-split-window-1
4585 window (or side 'below) min-height))
4586 ;; If SIDE is nil and vertical splitting failed, we try again
4587 ;; splitting horizontally this time.
4588 (and (memq side '(nil left right))
4589 (display-buffer-split-window-1
4590 window (or side 'right) min-width))
4591 ;; If WINDOW is live and the root window of its frame, try once
4592 ;; more splitting vertically, disregarding the min-height
4593 ;; specifier this time and using `window-min-height' instead.
4594 (and (memq side '(nil above below))
4595 (<= window-min-height min-height)
4596 (window-live-p window)
4597 (eq window (frame-root-window window))
4598 (display-buffer-split-window-1
4599 window (or side 'below) window-min-height)))))
4600
4601 (defun display-buffer-split-atom-window (window &optional side nest specifiers)
4602 "Make WINDOW part of an atomic window."
4603 (let ((ignore-window-parameters t)
4604 (window-nest t)
4605 (selected-window (selected-window))
4606 root new new-parent)
4607
4608 ;; We are in an atomic window.
4609 (when (and (window-parameter window 'window-atom) (not nest))
4610 ;; Split the root window.
4611 (setq window (window-atom-root window)))
4612
4613 (when (setq new (display-buffer-split-window window side specifiers))
4614 (setq new-parent (window-parent window))
4615 ;; WINDOW is or becomes atomic.
4616 (unless (window-parameter window 'window-atom)
4617 (walk-window-subtree
4618 (lambda (window)
4619 (set-window-parameter window 'window-atom t))
4620 window t))
4621 ;; New window and any new parent get their window-atom parameter
4622 ;; set too.
4623 (set-window-parameter new 'window-atom t)
4624 (set-window-parameter new-parent 'window-atom t)
4625 new)))
4626
4627 (defun display-buffer-pop-up-window (buffer methods &optional specifiers)
4628 "Display BUFFER in a new window.
4629 Return the window displaying BUFFER, nil if popping up the window
4630 failed. METHODS must be a list of window/side tuples like those
4631 forming the cdr of the `pop-up-window' buffer display specifier.
4632 As a special case, the car of such a tuple can be also a live
4633 window.
4634
4635 Optional argument SPECIFIERS must be a list of buffer display
4636 specifiers, see the doc-string of `display-buffer-alist' for a
4637 description."
4638 (let* ((frame (display-buffer-frame))
4639 (selected-window (frame-selected-window frame))
4640 window side atomic)
4641 (unless (and (cdr (assq 'unsplittable (frame-parameters frame)))
4642 ;; Don't split an unsplittable frame unless
4643 ;; SPECIFIERS allow it.
4644 (not (cdr (assq 'split-unsplittable-frame specifiers))))
4645 (catch 'done
4646 (dolist (method methods)
4647 (setq window (car method))
4648 (setq side (cdr method))
4649 (and (setq window
4650 (cond
4651 ((eq window 'largest)
4652 (get-largest-window frame t))
4653 ((eq window 'lru)
4654 (get-lru-window frame t))
4655 ((eq window 'selected)
4656 (frame-selected-window frame))
4657 ((eq window 'root)
4658 ;; If there are side windows, split the main
4659 ;; window else the frame root window.
4660 (or (window-with-parameter 'window-side 'none nil t)
4661 (frame-root-window frame)))
4662 ((memq window window-sides)
4663 ;; This should gets us the "root" side
4664 ;; window if there exists more than one.
4665 (window-with-parameter 'window-side window nil t))
4666 ((windowp window)
4667 ;; A window, directly specified.
4668 window)))
4669 ;; The window must be on the selected frame,
4670 (eq (window-frame window) frame)
4671 ;; and must be neither a minibuffer window,
4672 (not (window-minibuffer-p window))
4673 ;; nor a side window.
4674 (not (eq (window-parameter window 'window-side) 'side))
4675 (setq window
4676 (cond
4677 ((memq side display-buffer-side-specifiers)
4678 (if (and (window-buffer window)
4679 (setq atomic (cdr (assq 'atomic specifiers))))
4680 (display-buffer-split-atom-window
4681 window side (eq atomic 'nest) specifiers)
4682 (display-buffer-split-window window side specifiers)))
4683 ((functionp side)
4684 (ignore-errors
4685 ;; Don't pass any specifiers to this function.
4686 (funcall side window)))))
4687 (throw 'done window))))
4688
4689 (when window
4690 ;; Adjust sizes if asked for.
4691 (display-buffer-set-height window specifiers)
4692 (display-buffer-set-width window specifiers)
4693 (set-window-parameter
4694 window 'quit-restore (list 'new-window buffer selected-window))
4695 (setq display-buffer-window (cons window 'new-window))
4696 (display-buffer-in-window buffer window specifiers)
4697 (set-window-prev-buffers window nil)
4698 window))))
4699
4700 (defun display-buffer-pop-up-frame (buffer &optional graphic-only specifiers)
4701 "Make a new frame for displaying BUFFER.
4702 Return the window displaying BUFFER if creating the new frame was
4703 successful, nil otherwise. Optional argument GRAPHIC-ONLY
4704 non-nil means to make a new frame on graphic displays only.
4705
4706 SPECIFIERS must be a list of buffer display specifiers, see the
4707 documentation of `display-buffer-alist' for a description."
4708 (unless (and graphic-only (not (display-graphic-p)))
4709 (let* ((selected-window (selected-window))
4710 (function (or (cdr (assq 'pop-up-frame-function specifiers))
4711 'make-frame))
4712 (parameters
4713 (when (symbolp function)
4714 (cdr (assq 'pop-up-frame-alist specifiers))))
4715 (frame
4716 (if (symbolp function)
4717 (funcall function parameters)
4718 (funcall function))))
4719 (when frame
4720 (let ((window (frame-selected-window frame)))
4721 (set-window-parameter
4722 window 'quit-restore (list 'new-frame buffer selected-window))
4723 (setq display-buffer-window (cons window 'new-frame))
4724 (display-buffer-in-window buffer window specifiers))))))
4725
4726 (defun display-buffer-pop-up-side-window (buffer side slot &optional specifiers)
4727 "Display BUFFER in a new window on SIDE of the selected frame.
4728 SLOT specifies the slot to use. SPECIFIERS must be a list of
4729 buffer display specifiers.
4730
4731 Return the window displaying BUFFER, nil if popping up the window
4732 failed."
4733 (let* ((root (frame-root-window))
4734 (main (window-with-parameter 'window-side 'none nil t))
4735 (left-or-right (memq side '(left right)))
4736 (main-or-root
4737 (if (and main
4738 (or (and left-or-right (not window-sides-vertical))
4739 (and (not left-or-right) window-sides-vertical)))
4740 main
4741 root))
4742 (selected-window (selected-window))
4743 (on-side (cond
4744 ((eq side 'top) 'above)
4745 ((eq side 'bottom) 'below)
4746 (t side)))
4747 (window
4748 (display-buffer-split-window main-or-root on-side specifiers))
4749 fun)
4750 (when window
4751 (unless main
4752 (walk-window-subtree
4753 (lambda (window)
4754 ;; Make all main-or-root subwindows main windows.
4755 (set-window-parameter window 'window-side 'none))
4756 main-or-root t))
4757 ;; Make sure that parent's window-side is nil.
4758 (set-window-parameter (window-parent window) 'window-side nil)
4759 ;; Initialize side.
4760 (set-window-parameter window 'window-side side)
4761 ;; Adjust sizes if asked for.
4762 (display-buffer-set-height window specifiers)
4763 (display-buffer-set-width window specifiers)
4764 ;; Set window parameters.
4765 (set-window-parameter
4766 window 'quit-restore (list 'new-window buffer selected-window))
4767 (setq display-buffer-window (cons window 'new-window))
4768 (set-window-parameter window 'window-slot slot)
4769 (display-buffer-in-window buffer window specifiers)
4770 (set-window-prev-buffers window nil)
4771 window)))
4772
4773 (defun display-buffer-in-side-window (buffer side &optional slot specifiers)
4774 "Display BUFFER in a window on SIDE of the selected frame.
4775 SLOT, if non-nil, specifies the window slot where to display the
4776 BUFFER. SLOT zero or nil means use the central slot on SIDE.
4777 SLOT negative means use a slot preceding the central window.
4778 SLOT positive means use a slot following the central window.
4779
4780 SPECIFIERS must be a list of buffer display specifiers."
4781 (unless (memq side window-sides)
4782 (error "Invalid side %s specified" side))
4783 (let* ((major (window-with-parameter 'window-side side nil t))
4784 ;; `major' is the major window on SIDE, `windows' the life
4785 ;; windows on SIDE.
4786 (windows (when major (windows-with-parameter 'window-side side)))
4787 (slots (when major (window-child-count major)))
4788 (max-slots
4789 (nth (cond
4790 ((eq side 'left) 0)
4791 ((eq side 'top) 1)
4792 ((eq side 'right) 2)
4793 ((eq side 'bottom) 3))
4794 window-sides-slots))
4795 (selected-window (selected-window))
4796 window this-window this-slot prev-window next-window
4797 best-window best-slot abs-slot)
4798
4799 (unless (numberp slot)
4800 (setq slot 0))
4801 (if (not windows)
4802 ;; No suitable side window exists, make one.
4803 (display-buffer-pop-up-side-window buffer side slot specifiers)
4804 ;; Scan windows on SIDE.
4805 (catch 'found
4806 (dolist (window windows)
4807 (setq this-slot (window-parameter window 'window-slot))
4808 (cond
4809 ((not (numberp this-slot)))
4810 ((and (= this-slot slot)
4811 ;; Dedicatedness check.
4812 (or (not (window-dedicated-p window))
4813 (assq 'reuse-window-dedicated specifiers)))
4814 ;; Window with matching SLOT, use it.
4815 (setq this-window window)
4816 (throw 'found t))
4817 (t
4818 (setq abs-slot (abs (- (abs slot) (abs this-slot))))
4819 (unless (and best-slot (<= best-slot abs-slot))
4820 (setq best-window window)
4821 (setq best-slot abs-slot))
4822 (cond
4823 ((<= this-slot slot)
4824 (setq prev-window window))
4825 ((not next-window)
4826 (setq next-window window)))))))
4827
4828 ;; `this-window' is the first window with the same SLOT.
4829 ;; `prev-window' is the window with the largest slot < SLOT. A new
4830 ;; window will be created after it.
4831 ;; `next-window' is the window with the smallest slot > SLOT. A new
4832 ;; window will be created before it.
4833 ;; `best-window' is the window with the smallest absolute difference
4834 ;; of its slot and SLOT.
4835 (or (and this-window
4836 ;; Reuse this window.
4837 (prog1
4838 (setq window this-window)
4839 (if (eq (window-buffer window) buffer)
4840 (setq display-buffer-window
4841 (cons window 'reuse-buffer-window))
4842 (setq display-buffer-window
4843 (cons window 'reuse-other-window))
4844 (unless (window-parameter window 'quit-restore)
4845 ;; Don't overwrite an existing quit-restore entry.
4846 (set-window-parameter
4847 window 'quit-restore
4848 (list (window-buffer window) (window-start window)
4849 (window-point window) buffer
4850 (window-total-size window) (selected-window)))))))
4851 (and (or (not max-slots) (< slots max-slots))
4852 (or (and next-window
4853 ;; Make new window before next-window.
4854 (let ((next-side
4855 (if (memq side '(left right)) 'above 'left)))
4856 (setq window (display-buffer-split-window
4857 next-window next-side specifiers))))
4858 (and prev-window
4859 ;; Make new window after prev-window.
4860 (let ((prev-side
4861 (if (memq side '(left right)) 'below 'right)))
4862 (setq window (display-buffer-split-window
4863 prev-window prev-side specifiers)))))
4864 (progn
4865 (display-buffer-set-height window specifiers)
4866 (display-buffer-set-width window specifiers)
4867 (set-window-parameter
4868 window 'quit-restore
4869 (list 'new-window buffer selected-window))
4870 (setq display-buffer-window (cons window 'new-window))
4871 window))
4872 (and best-window
4873 (setq window best-window)
4874 ;; Reuse best window (the window nearest to SLOT).
4875 (if (eq (window-buffer window) buffer)
4876 (setq display-buffer-window
4877 (cons window 'reuse-buffer-window))
4878 (setq display-buffer-window
4879 (cons window 'reuse-other-window))
4880
4881 (unless (window-parameter window 'quit-restore)
4882 ;; Don't overwrite an existing quit-restore entry.
4883 (set-window-parameter
4884 window 'quit-restore
4885 (list (window-buffer window) (window-start window)
4886 (window-point window) buffer
4887 (window-total-size window) (selected-window)))))
4888 window))
4889
4890 (when window
4891 (unless (window-parameter window 'window-slot)
4892 ;; Don't change exisiting slot value.
4893 (set-window-parameter window 'window-slot slot))
4894 (display-buffer-in-window buffer window specifiers)))))
4895
4896 (defun normalize-buffer-to-display (buffer-or-name)
4897 "Normalize BUFFER-OR-NAME argument for buffer display functions.
4898 If BUFFER-OR-NAME is nil, return the curent buffer. Else, if a
4899 buffer specified by BUFFER-OR-NAME exists, return that buffer.
4900 If no such buffer exists, create a buffer with the name
4901 BUFFER-OR-NAME and return that buffer."
4902 (if buffer-or-name
4903 (or (get-buffer buffer-or-name)
4904 (let ((buffer (get-buffer-create buffer-or-name)))
4905 (set-buffer-major-mode buffer)
4906 buffer))
4907 (current-buffer)))
4908
4909 (defun display-buffer-normalize-specifiers-1 (specifiers)
4910 "Subroutine of `display-buffer-normalize-specifiers'.
4911 SPECIFIERS is the SPECIFIERS argument of `display-buffer'."
4912 (let (normalized)
4913 (cond
4914 ((listp specifiers)
4915 (dolist (specifier specifiers)
4916 (cond
4917 ((consp specifier)
4918 (setq normalized (cons specifier normalized)))
4919 ((symbolp specifier)
4920 ;; Might be a macro specifier, try to expand it (the cdr is a
4921 ;; list and we have to reverse it later, so do it one at a
4922 ;; time).
4923 (let ((entry (assq specifier display-buffer-macro-specifiers)))
4924 (dolist (item (cdr entry))
4925 (setq normalized (cons item normalized)))))))
4926 ;; Reverse list.
4927 (setq normalized (nreverse normalized)))
4928 ;; The two cases below must come from the SPECIFIERS argument of
4929 ;; `display-buffer'.
4930 ((eq specifiers 't)
4931 ;; Historically t means "other window". Eventually we should get
4932 ;; rid of this.
4933 (setq normalized
4934 (cdr (assq 'other-window display-buffer-macro-specifiers))
4935 normalized))
4936 ((symbolp specifiers)
4937 ;; We allow scalar specifiers in calls of `display-buffer'.
4938 (let ((entry (assq specifiers display-buffer-macro-specifiers)))
4939 (when entry (setq normalized (cdr entry))))))
4940
4941 normalized))
4942
4943 (defun display-buffer-normalize-specifiers-2 (&optional buffer-or-name)
4944 "Subroutine of `display-buffer-normalize-specifiers'.
4945 BUFFER-OR-NAME is the buffer to display. This routine provides a
4946 compatibility layer for the now obsolete Emacs 23 buffer display
4947 options."
4948 (let* ((buffer (normalize-live-buffer buffer-or-name))
4949 (buffer-name (buffer-name buffer))
4950 specifiers)
4951 ;; Disable warnings, there are too many obsolete options here.
4952 (with-no-warnings
4953 ;; `display-buffer-mark-dedicated'
4954 (unless (memq display-buffer-mark-dedicated '(nil unset))
4955 (setq specifiers
4956 (cons (cons 'dedicate display-buffer-mark-dedicated)
4957 specifiers)))
4958
4959 ;; `pop-up-window' group. Anything is added here iff
4960 ;; `pop-up-windows' is neither nil nor unset.
4961 (let ((pop-up-window (not (memq pop-up-windows '(nil unset))))
4962 (fun (unless (eq split-window-preferred-function
4963 'split-window-sensibly)
4964 split-window-preferred-function))
4965 (min-height (if (numberp split-height-threshold)
4966 (/ split-height-threshold 2)
4967 1.0))
4968 (min-width (if (numberp split-width-threshold)
4969 (/ split-width-threshold 2)
4970 1.0)))
4971 (when pop-up-window
4972 ;; `split-height-threshold'
4973 (setq specifiers
4974 (cons (cons 'pop-up-window-min-height min-height)
4975 specifiers))
4976 ;; `split-width-threshold'
4977 (setq specifiers
4978 (cons (cons 'pop-up-window-min-width min-width)
4979 specifiers))
4980 ;; `pop-up-window'
4981 (setq specifiers
4982 (cons (list 'pop-up-window
4983 (cons 'largest fun) (cons 'lru fun))
4984 specifiers))))
4985
4986 ;; `pop-up-frame' group. Anything is added here iff
4987 ;; `pop-up-frames' is neither nil nor unset (we ignore the problem
4988 ;; that callers usually don't care about graphic-only).
4989 (unless (memq pop-up-frames '(nil unset))
4990 ;; `pop-up-frame-function'. If `pop-up-frame-function' uses the
4991 ;; now obsolete `pop-up-frame-alist' it will continue to do so.
4992 (setq specifiers
4993 (cons (cons 'pop-up-frame-function pop-up-frame-function)
4994 specifiers))
4995 ;; `pop-up-frame'
4996 (setq specifiers
4997 (cons (list 'pop-up-frame pop-up-frames) specifiers)))
4998
4999 ;; `special-display-regexps'
5000 (dolist (entry special-display-regexps)
5001 (cond
5002 ((stringp entry)
5003 ;; Plain string.
5004 (when (string-match-p entry buffer-name)
5005 (setq specifiers
5006 (cons
5007 (list 'fun-with-args special-display-function
5008 special-display-frame-alist)
5009 specifiers))))
5010 ((consp entry)
5011 (let ((name (car entry))
5012 (rest (cdr entry)))
5013 (cond
5014 ((not (string-match-p name buffer-name)))
5015 ((functionp (car rest))
5016 ;; A function.
5017 (setq specifiers
5018 (cons (list 'fun-with-args (car rest) (cadr rest))
5019 specifiers)))
5020 ((listp rest)
5021 ;; A list of parameters.
5022 (cond
5023 ((assq 'same-window rest)
5024 (setq specifiers
5025 (cons (list 'reuse-window 'same) specifiers))
5026 (setq specifiers
5027 (cons (list 'reuse-window-dedicated 'weak)
5028 specifiers)))
5029 ((assq 'same-frame rest)
5030 (setq specifiers
5031 (setq specifiers
5032 (cons (list 'same-frame) specifiers))))
5033 (t
5034 (setq specifiers
5035 (cons (list 'fun-with-args special-display-function
5036 special-display-frame-alist)
5037 specifiers))))))))))
5038
5039 ;; `special-display-buffer-names'
5040 (dolist (entry special-display-buffer-names)
5041 (cond
5042 ((stringp entry)
5043 ;; Plain string.
5044 (when (string-equal entry buffer-name)
5045 (setq specifiers
5046 (cons
5047 (list 'fun-with-args special-display-function
5048 special-display-frame-alist)
5049 specifiers))))
5050 ((consp entry)
5051 (let ((name (car entry))
5052 (rest (cdr entry)))
5053 (cond
5054 ((not (string-equal name buffer-name)))
5055 ((functionp (car rest))
5056 ;; A function.
5057 (setq specifiers
5058 (cons (list 'fun-with-args (car rest) (cadr rest))
5059 specifiers)))
5060 ((listp rest)
5061 ;; A list of parameters.
5062 (cond
5063 ((assq 'same-window rest)
5064 (setq specifiers
5065 (cons (list 'reuse-window 'same) specifiers))
5066 (setq specifiers
5067 (cons (list 'reuse-window-dedicated 'weak)
5068 specifiers)))
5069 ((assq 'same-frame rest)
5070 (setq specifiers
5071 (setq specifiers
5072 (cons (list 'same-frame) specifiers))))
5073 (t
5074 (setq specifiers
5075 (cons (list 'fun-with-args special-display-function
5076 special-display-frame-alist)
5077 specifiers))))))))))
5078
5079 ;; `same-window-regexps'
5080 (dolist (entry same-window-regexps)
5081 (cond
5082 ((stringp entry)
5083 (when (string-match-p entry buffer-name)
5084 (setq specifiers
5085 (cons (list 'reuse-window 'same) specifiers))))
5086 ((consp entry)
5087 (when (string-match-p (car entry) buffer-name)
5088 (setq specifiers
5089 (cons (list 'reuse-window 'same) specifiers))))))
5090
5091 ;; `same-window-buffer-names'
5092 (dolist (entry same-window-buffer-names)
5093 (cond
5094 ((stringp entry)
5095 (when (string-equal entry buffer-name)
5096 (setq specifiers
5097 (cons (list 'reuse-window 'same) specifiers))))
5098 ((consp entry)
5099 (when (string-equal (car entry) buffer-name)
5100 (setq specifiers
5101 (cons (list 'reuse-window 'same) specifiers))))))
5102
5103 ;; `pop-up-windows' and `pop-up-frames' nil means means we
5104 ;; are supposed to reuse any window (unless we find one showing
5105 ;; the same buffer already).
5106
5107 ;; This clause is needed because Emacs 23 options can be used to
5108 ;; suppress a certain behavior while `display-buffer-alist' can be
5109 ;; only used to enforce some behavior.
5110 (when (and (not pop-up-windows) (memq pop-up-frames '(nil unset)))
5111 ;; `even-window-heights'
5112 (when even-window-heights
5113 (setq specifiers
5114 (cons (cons 'reuse-window-even-sizes t) specifiers)))
5115 ;; `reuse-window' showing any buffer on same frame.
5116 (setq specifiers
5117 (cons (list 'reuse-window nil nil nil)
5118 specifiers)))
5119
5120 ;; `display-buffer-reuse-frames' or `pop-up-frames' set means we
5121 ;; are supposed to reuse a window showing the same buffer.
5122 (unless (and (memq display-buffer-reuse-frames '(nil unset))
5123 (memq pop-up-frames '(nil unset)))
5124 ;; `even-window-heights'
5125 (when even-window-heights
5126 (setq specifiers
5127 (cons (cons 'reuse-window-even-sizes t) specifiers)))
5128 ;; `reuse-window' showing same buffer on visible frame.
5129 (setq specifiers
5130 (cons (list 'reuse-window nil 'same 0)
5131 specifiers)))
5132
5133 specifiers)))
5134
5135 (defun display-buffer-normalize-specifiers (buffer-name specifiers label)
5136 "Return normalized specifiers for a buffer matching BUFFER-NAME or LABEL.
5137 BUFFER-NAME must be a string specifying a valid buffer name.
5138 SPECIFIERS and LABEL are the homonymous arguments of
5139 `display-buffer'.
5140
5141 The method for displaying the buffer specified by BUFFER-NAME or
5142 LABEL is established by appending the following four lists of
5143 specifiers:
5144
5145 - The specifiers in `display-buffer-alist' whose buffer
5146 identifier matches BUFFER-NAME or LABEL and whose 'override
5147 component is set.
5148
5149 - SPECIFIERS.
5150
5151 - The specifiers in `display-buffer-alist' whose buffer
5152 identifier matches BUFFER-NAME or LABEL and whose 'override
5153 component is not set.
5154
5155 - `display-buffer-default-specifiers'."
5156 (let (list-1 list-2)
5157 (dolist (entry display-buffer-alist)
5158 (when (and (listp entry)
5159 (catch 'match
5160 (dolist (id (car entry))
5161 (when (consp id)
5162 (let ((type (car id))
5163 (value (cdr id)))
5164 (when (or (and (eq type 'name) (stringp value)
5165 (equal value buffer-name))
5166 (and (eq type 'regexp) (stringp value)
5167 (string-match-p value buffer-name))
5168 (and (eq type 'label) (eq value label)))
5169 (throw 'match t)))))))
5170 (let* ((raw (cdr entry))
5171 (normalized (display-buffer-normalize-specifiers-1 raw)))
5172 (if (assq 'override raw)
5173 (setq list-1
5174 (if list-1
5175 (append list-1 normalized)
5176 normalized))
5177 (setq list-2
5178 (if list-2
5179 (append list-2 normalized)
5180 normalized))))))
5181
5182 (append
5183 ;; Overriding user specifiers.
5184 list-1
5185 ;; Application specifiers.
5186 (display-buffer-normalize-specifiers-1 specifiers)
5187 ;; Emacs 23 compatibility specifiers.
5188 (display-buffer-normalize-specifiers-2 buffer-name)
5189 ;; Non-overriding user specifiers.
5190 list-2
5191 ;; Default specifiers.
5192 display-buffer-default-specifiers)))
5193
5194 ;; Minibuffer-only frames should be documented better. They really
5195 ;; deserve a separate section in the manual. Also
5196 ;; `last-nonminibuffer-frame' is nowhere documented in the manual.
5197 (defun display-buffer-frame (&optional frame)
5198 "Return FRAME if it is live and not a minibuffer-only frame.
5199 Return the value of `last-nonminibuffer-frame' otherwise."
5200 (setq frame (normalize-live-frame frame))
5201 (if (and (frame-live-p frame)
5202 ;; A not very nice way to get that information.
5203 (not (window-minibuffer-p (frame-root-window frame))))
5204 frame
5205 (last-nonminibuffer-frame)))
5206
5207 (defun display-buffer (&optional buffer-or-name specifiers label)
5208 "Make the buffer specified by BUFFER-OR-NAME appear in some window.
5209 Optional argument BUFFER-OR-NAME may be a buffer, a string \(a
5210 buffer name), or nil. If BUFFER-OR-NAME is a string not naming
5211 an existent buffer, create a buffer with that name. If
5212 BUFFER-OR-NAME is nil or omitted, display the current buffer.
5213 Interactively, prompt for the buffer name using the minibuffer.
5214
5215 Return the window chosen to display the buffer or nil if no such
5216 window is found. Do not change the selected window unless the
5217 buffer is shown on a different frame than the selected one.
5218
5219 Optional argument SPECIFIERS must be a list of buffer display
5220 specifiers, see the documentation of `display-buffer-alist' for a
5221 description.
5222
5223 For convenience, SPECIFIERS may also consist of a single buffer
5224 display location specifier or t, where the latter means to
5225 display the buffer in any but the selected window. If SPECIFIERS
5226 is nil or omitted, this means to exclusively use the specifiers
5227 provided by `display-buffer-alist'. If the value of the latter
5228 is nil too, all specifiers are provided by the constant
5229 `display-buffer-default-specifiers'.
5230
5231 As a special case, the `reuse-window' specifier allows to specify
5232 as second element an arbitrary window, as third element an
5233 arbitrary buffer, and as fourth element an arbitrary frame. As
5234 first element of a window/side pair of the `pop-up-window'
5235 specifier you can specifiy an arbitrary window.
5236
5237 The optional third argument LABEL, if non-nil, must be a symbol
5238 specifiying the buffer display label. Applications should set
5239 this when the buffer shall be displayed in some special way but
5240 BUFFER-OR-NAME does not identify the buffer as special. Typical
5241 buffers that fit into this category are those whose names are
5242 derived from the name of the file they are visiting. A user can
5243 override SPECIFIERS by adding an entry to `display-buffer-alist'
5244 whose car contains LABEL and whose cdr specifies the preferred
5245 alternative display method.
5246
5247 The method to display the buffer is derived by combining the
5248 values of `display-buffer-alist' and SPECIFIERS. Highest
5249 priority is given to overriding elements of
5250 `display-buffer-alist'. Next come the elements specified by
5251 SPECIFIERS, followed by the non-overriding elements of
5252 `display-buffer-alist'.
5253
5254 The result must be a list of valid buffer display specifiers. If
5255 `display-buffer-function' is non-nil, call it with the buffer and
5256 this list as arguments."
5257 (interactive "BDisplay buffer:\nP")
5258 (let* ((buffer (normalize-buffer-to-display buffer-or-name))
5259 (buffer-name (buffer-name buffer))
5260 (specifiers
5261 ;; Normalize specifiers.
5262 (display-buffer-normalize-specifiers buffer-name specifiers label))
5263 ;; Don't use a minibuffer frame.
5264 (frame (display-buffer-frame))
5265 ;; `window' is the window we use for showing `buffer'.
5266 window specifier method)
5267 ;; Reset this.
5268 (setq display-buffer-window nil)
5269 (if display-buffer-function
5270 ;; Let `display-buffer-function' do the job.
5271 (funcall display-buffer-function buffer specifiers)
5272 ;; Retrieve the next location specifier while there a specifiers
5273 ;; left and we don't have a valid window.
5274 (while (and specifiers (not (window-live-p window)))
5275 (setq specifier (car specifiers))
5276 (setq specifiers (cdr specifiers))
5277 (setq method (car specifier))
5278 (setq window
5279 (cond
5280 ((eq method 'reuse-window)
5281 (display-buffer-reuse-window
5282 buffer (cdr specifier) specifiers))
5283 ((eq method 'pop-up-window)
5284 (display-buffer-pop-up-window
5285 buffer (cdr specifier) specifiers))
5286 ((eq method 'pop-up-frame)
5287 (display-buffer-pop-up-frame
5288 buffer (cdr specifier) specifiers))
5289 ((eq method 'use-side-window)
5290 (display-buffer-in-side-window
5291 buffer (nth 1 specifier) (nth 2 specifier) specifiers))
5292 ((eq method 'fun-with-args)
5293 (apply (cadr specifier) buffer (cddr specifier))))))
5294
5295 ;; If we don't have a window yet, try a fallback method. All
5296 ;; specifiers have been used up by now.
5297 (or (and (window-live-p window) window)
5298 ;; Try reusing a window showing BUFFER on any visible or
5299 ;; iconfied frame.
5300 (display-buffer-reuse-window buffer '(nil buffer 0))
5301 ;; Try reusing a window not showing BUFFER on any visible or
5302 ;; iconified frame.
5303 (display-buffer-reuse-window buffer '(nil other 0))
5304 ;; Try making a new frame (but not in batch mode).
5305 (and (not noninteractive) (display-buffer-pop-up-frame buffer))
5306 ;; Try using a weakly dedicated window.
5307 (display-buffer-reuse-window
5308 buffer '(nil nil t) '((reuse-window-dedicated . weak)))
5309 ;; Try using a strongly dedicated window.
5310 (display-buffer-reuse-window
5311 buffer '(nil nil t) '((reuse-window-dedicated . t)))))))
5312
5313 (defsubst display-buffer-same-window (&optional buffer-or-name label)
5314 "Display buffer specified by BUFFER-OR-NAME in the selected window.
5315 Another window will be used only if the buffer can't be shown in
5316 the selected window, usually because it is dedicated to another
5317 buffer. Optional argument BUFFER-OR-NAME and LABEL are as for
5318 `display-buffer'."
5319 (interactive "BDisplay buffer in same window:\nP")
5320 (display-buffer buffer-or-name 'same-window label))
5321
5322 (defsubst display-buffer-same-frame (&optional buffer-or-name label)
5323 "Display buffer specified by BUFFER-OR-NAME in a window on the same frame.
5324 Another frame will be used only if there is no other choice.
5325 Optional argument BUFFER-OR-NAME and LABEL are as for
5326 `display-buffer'."
5327 (interactive "BDisplay buffer on same frame:\nP")
5328 (display-buffer buffer-or-name 'same-frame label))
5329
5330 (defsubst display-buffer-other-window (&optional buffer-or-name label)
5331 "Display buffer specified by BUFFER-OR-NAME in another window.
5332 The selected window will be used only if there is no other
5333 choice. Windows on the selected frame are preferred to windows
5334 on other frames. Optional argument BUFFER-OR-NAME and LABEL are as
5335 for `display-buffer'."
5336 (interactive "BDisplay buffer in another window:\nP")
5337 (display-buffer buffer-or-name 'other-window label))
5338
5339 (defun display-buffer-same-frame-other-window (&optional buffer-or-name label)
5340 "Display buffer specified by BUFFER-OR-NAME in another window on the same frame.
5341 The selected window or another frame will be used only if there
5342 is no other choice. Optional argument BUFFER-OR-NAME and LABEL are
5343 as for `display-buffer'."
5344 (interactive "BDisplay buffer in another window on same frame:\nP")
5345 (display-buffer buffer-or-name 'same-frame-other-window label))
5346
5347 (defun display-buffer-other-frame (&optional buffer-or-name label)
5348 "Display buffer specified by BUFFER-OR-NAME on another frame.
5349 The selected frame will be used only if there is no other choice.
5350 Optional argument BUFFER-OR-NAME and LABEL are as for
5351 `display-buffer'.
5352
5353 If this command uses another frame, it will also select that frame."
5354 (interactive "BDisplay buffer in other frame: ")
5355 (display-buffer buffer-or-name 'other-frame label))
5356
5357 (defun pop-to-buffer (&optional buffer-or-name specifiers norecord label)
5358 "Display buffer specified by BUFFER-OR-NAME and select the window used.
5359 Optional argument BUFFER-OR-NAME may be a buffer, a string \(a
5360 buffer name), or nil. If BUFFER-OR-NAME is a string not naming
5361 an existent buffer, create a buffer with that name. If
5362 BUFFER-OR-NAME is nil or omitted, display the current buffer.
5363 Interactively, prompt for the buffer name using the minibuffer.
5364
5365 Optional second argument SPECIFIERS must be a list of buffer
5366 display specifiers, a single location specifier, `t' which means
5367 the latter means to display the buffer in any but the selected
5368 window, or nil which means to exclusively apply the specifiers
5369 customized by the user.
5370
5371 Optional argument NORECORD non-nil means do not put the buffer
5372 specified by BUFFER-OR-NAME at the front of the buffer list and
5373 do not make the window displaying it the most recently selected
5374 one.
5375
5376 The optional argument LABEL, if non-nil, is a symbol specifying the
5377 display purpose. Applications should set this when the buffer
5378 shall be displayed in a special way but BUFFER-OR-NAME does not
5379 identify the buffer as special. Buffers that typically fit into
5380 this category are those whose names have been derived from the
5381 name of the file they are visiting.
5382
5383 Return the buffer specified by BUFFER-OR-NAME or nil if
5384 displaying the buffer failed.
5385
5386 This uses the function `display-buffer' as a subroutine; see the
5387 documentations of `display-buffer' and `display-buffer-alist' for
5388 additional information."
5389 (interactive "BPop to buffer:\nP")
5390 (let ((buffer (normalize-buffer-to-display buffer-or-name))
5391 window)
5392 (set-buffer buffer)
5393 (when (setq window (display-buffer buffer specifiers label))
5394 (select-window window norecord)
5395 buffer)))
5396
5397 (defsubst pop-to-buffer-same-window (&optional buffer-or-name norecord label)
5398 "Pop to buffer specified by BUFFER-OR-NAME in the selected window.
5399 Another window will be used only if the buffer can't be shown in
5400 the selected window, usually because it is dedicated to another
5401 buffer. Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are
5402 as for `pop-to-buffer'."
5403 (interactive "BPop to buffer in selected window:\nP")
5404 (pop-to-buffer buffer-or-name 'same-window norecord label))
5405
5406 (defsubst pop-to-buffer-same-frame (&optional buffer-or-name norecord label)
5407 "Pop to buffer specified by BUFFER-OR-NAME in a window on the selected frame.
5408 Another frame will be used only if there is no other choice.
5409 Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are as for
5410 `pop-to-buffer'."
5411 (interactive "BPop to buffer on same frame:\nP")
5412 (pop-to-buffer buffer-or-name 'same-frame norecord label))
5413
5414 (defsubst pop-to-buffer-other-window (&optional buffer-or-name norecord label)
5415 "Pop to buffer specified by BUFFER-OR-NAME in another window.
5416 The selected window will be used only if there is no other
5417 choice. Windows on the selected frame are preferred to windows
5418 on other frames. Optional arguments BUFFER-OR-NAME, NORECORD and
5419 LABEL are as for `pop-to-buffer'."
5420 (interactive "BPop to buffer in another window:\nP")
5421 (pop-to-buffer buffer-or-name 'other-window norecord))
5422
5423 (defsubst pop-to-buffer-same-frame-other-window (&optional buffer-or-name norecord label)
5424 "Pop to buffer specified by BUFFER-OR-NAME in another window on the selected frame.
5425 The selected window or another frame will be used only if there
5426 is no other choice. Optional arguments BUFFER-OR-NAME, NORECORD
5427 and LABEL are as for `pop-to-buffer'."
5428 (interactive "BPop to buffer in another window on same frame:\nP")
5429 (pop-to-buffer buffer-or-name 'same-frame-other-window norecord label))
5430
5431 (defsubst pop-to-buffer-other-frame (&optional buffer-or-name norecord label)
5432 "Pop to buffer specified by BUFFER-OR-NAME on another frame.
5433 The selected frame will be used only if there's no other choice.
5434 Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are as for
5435 `pop-to-buffer'."
5436 (interactive "BPop to buffer on another frame:\nP")
5437 (pop-to-buffer buffer-or-name 'other-frame norecord label))
5438
5439 (defun read-buffer-to-switch (prompt)
5440 "Read the name of a buffer to switch to, prompting with PROMPT.
5441 Return the neame of the buffer as a string.
5442
5443 This function is intended for the `switch-to-buffer' family of
5444 commands since these need to omit the name of the current buffer
5445 from the list of completions and default values."
5446 (let ((rbts-completion-table (internal-complete-buffer-except)))
5447 (minibuffer-with-setup-hook
5448 (lambda ()
5449 (setq minibuffer-completion-table rbts-completion-table)
5450 ;; Since rbts-completion-table is built dynamically, we
5451 ;; can't just add it to the default value of
5452 ;; icomplete-with-completion-tables, so we add it
5453 ;; here manually.
5454 (if (and (boundp 'icomplete-with-completion-tables)
5455 (listp icomplete-with-completion-tables))
5456 (set (make-local-variable 'icomplete-with-completion-tables)
5457 (cons rbts-completion-table
5458 icomplete-with-completion-tables))))
5459 (read-buffer prompt (other-buffer (current-buffer))
5460 (confirm-nonexistent-file-or-buffer)))))
5461
5462 (defun normalize-buffer-to-switch-to (buffer-or-name)
5463 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
5464 If BUFFER-OR-NAME is nil, return the buffer returned by
5465 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
5466 exists, return that buffer. If no such buffer exists, create a
5467 buffer with the name BUFFER-OR-NAME and return that buffer."
5468 (if buffer-or-name
5469 (or (get-buffer buffer-or-name)
5470 (let ((buffer (get-buffer-create buffer-or-name)))
5471 (set-buffer-major-mode buffer)
5472 buffer))
5473 (other-buffer)))
5474
5475 (defun switch-to-buffer (buffer-or-name &optional norecord)
5476 "Switch to buffer BUFFER-OR-NAME in the selected window.
5477 If called interactively, prompt for the buffer name using the
5478 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5479 determines whether to request confirmation before creating a new
5480 buffer.
5481
5482 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
5483 nil. If BUFFER-OR-NAME is a string that does not identify an
5484 existing buffer, create a buffer with that name. If
5485 BUFFER-OR-NAME is nil, switch to the buffer returned by
5486 `other-buffer'.
5487
5488 Optional argument NORECORD non-nil means do not put the buffer
5489 specified by BUFFER-OR-NAME at the front of the buffer list and
5490 do not make the window displaying it the most recently selected
5491 one. Return the buffer switched to.
5492
5493 This function is intended for interactive use only. Lisp
5494 functions should call `pop-to-buffer-same-window' instead."
5495 (interactive
5496 (list (read-buffer-to-switch "Switch to buffer: ")))
5497 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5498 (if (and (or (window-minibuffer-p) (eq (window-dedicated-p) t))
5499 (not (eq buffer (window-buffer))))
5500 ;; Cannot switch to another buffer in a minibuffer or strongly
5501 ;; dedicated window that does not show the buffer already. Call
5502 ;; `pop-to-buffer' instead.
5503 (pop-to-buffer buffer 'same-window norecord)
5504 (unless (eq buffer (window-buffer))
5505 ;; I'm not sure why we should NOT call `set-window-buffer' here,
5506 ;; but let's keep things as they are (otherwise we could always
5507 ;; call `pop-to-buffer-same-window' here).
5508 (set-window-buffer nil buffer))
5509 (unless norecord
5510 (select-window (selected-window)))
5511 (set-buffer buffer))))
5512
5513 (defun switch-to-buffer-same-frame (buffer-or-name &optional norecord)
5514 "Switch to buffer BUFFER-OR-NAME in a window on the selected frame.
5515 Another frame will be used only if there is no other choice.
5516 Optional arguments BUFFER-OR-NAME and NORECORD have the same
5517 meaning as for `switch-to-buffer'.
5518
5519 This function is intended for interactive use only. Lisp
5520 functions should call `pop-to-buffer-same-frame' instead."
5521 (interactive
5522 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5523 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5524 (pop-to-buffer buffer 'same-frame norecord)))
5525
5526 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
5527 "Switch to buffer BUFFER-OR-NAME in another window.
5528 The selected window will be used only if there is no other
5529 choice. Windows on the selected frame are preferred to windows
5530 on other frames. Optional arguments BUFFER-OR-NAME and NORECORD
5531 have the same meaning as for `switch-to-buffer'.
5532
5533 This function is intended for interactive use only. Lisp
5534 functions should call `pop-to-buffer-other-window' instead."
5535 (interactive
5536 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5537 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5538 (pop-to-buffer buffer 'other-window norecord)))
5539
5540 (defun switch-to-buffer-other-window-same-frame (buffer-or-name &optional norecord)
5541 "Switch to buffer BUFFER-OR-NAME in another window on the selected frame.
5542 The selected window or another frame will be used only if there
5543 is no other choice. Optional arguments BUFFER-OR-NAME and
5544 NORECORD have the same meaning as for `switch-to-buffer'.
5545
5546 This function is intended for interactive use only. Lisp
5547 functions should call `pop-to-buffer-other-window-same-frame'
5548 instead."
5549 (interactive
5550 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5551 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5552 (pop-to-buffer buffer 'same-frame-other-window norecord)))
5553
5554 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
5555 "Switch to buffer BUFFER-OR-NAME on another frame.
5556 The same frame will be used only if there is no other choice.
5557 Optional arguments BUFFER-OR-NAME and NORECORD have the same
5558 meaning as for `switch-to-buffer'.
5559
5560 This function is intended for interactive use only. Lisp
5561 functions should call `pop-to-buffer-other-frame' instead."
5562 (interactive
5563 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
5564 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5565 (pop-to-buffer buffer 'other-frame norecord)))
5566
5567 ;;; Obsolete definitions of `display-buffer' below.
5568 (defcustom same-window-buffer-names nil
5569 "List of names of buffers that should appear in the \"same\" window.
5570 `display-buffer' and `pop-to-buffer' show a buffer whose name is
5571 on this list in the selected rather than some other window.
5572
5573 An element of this list can be a cons cell instead of just a
5574 string. In that case, the cell's car must be a string specifying
5575 the buffer name. This is for compatibility with
5576 `special-display-buffer-names'; the cdr of the cons cell is
5577 ignored.
5578
5579 See also `same-window-regexps'."
5580 :type '(repeat (string :format "%v"))
5581 :group 'windows)
5582 (make-obsolete-variable
5583 'same-window-buffer-names
5584 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
5585
5586 (defcustom same-window-regexps nil
5587 "List of regexps saying which buffers should appear in the \"same\" window.
5588 `display-buffer' and `pop-to-buffer' show a buffer whose name
5589 matches a regexp on this list in the selected rather than some
5590 other window.
5591
5592 An element of this list can be a cons cell instead of just a
5593 string. In that case, the cell's car must be a regexp matching
5594 the buffer name. This is for compatibility with
5595 `special-display-regexps'; the cdr of the cons cell is ignored.
5596
5597 See also `same-window-buffer-names'."
5598 :type '(repeat (regexp :format "%v"))
5599 :group 'windows)
5600 (make-obsolete-variable
5601 'same-window-regexps
5602 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
5603
5604 (defun same-window-p (buffer-name)
5605 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5606 This function returns non-nil if `display-buffer' or
5607 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
5608 selected rather than \(as usual\) some other window. See
5609 `same-window-buffer-names' and `same-window-regexps'."
5610 (let ((buffer-names (with-no-warnings special-display-buffer-names))
5611 (regexps (with-no-warnings special-display-regexps)))
5612 (cond
5613 ((not (stringp buffer-name)))
5614 ;; The elements of `same-window-buffer-names' can be buffer
5615 ;; names or cons cells whose cars are buffer names.
5616 ((member buffer-name buffer-names))
5617 ((assoc buffer-name buffer-names))
5618 ((catch 'found
5619 (dolist (regexp regexps)
5620 ;; The elements of `same-window-regexps' can be regexps
5621 ;; or cons cells whose cars are regexps.
5622 (when (or (and (stringp regexp)
5623 (string-match regexp buffer-name))
5624 (and (consp regexp) (stringp (car regexp))
5625 (string-match-p (car regexp) buffer-name)))
5626 (throw 'found t))))))))
5627 (make-obsolete
5628 'same-window-p "pass argument to buffer display function instead." "24.1")
5629
5630 (defcustom special-display-frame-alist
5631 '((height . 14) (width . 80) (unsplittable . t))
5632 "Alist of parameters for special frames.
5633 Special frames are used for buffers whose names are listed in
5634 `special-display-buffer-names' and for buffers whose names match
5635 one of the regular expressions in `special-display-regexps'.
5636
5637 This variable can be set in your init file, like this:
5638
5639 (setq special-display-frame-alist '((width . 80) (height . 20)))
5640
5641 These supersede the values given in `default-frame-alist'."
5642 :type '(repeat (cons :format "%v"
5643 (symbol :tag "Parameter")
5644 (sexp :tag "Value")))
5645 :group 'frames)
5646 (make-obsolete-variable
5647 'special-display-frame-alist
5648 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
5649
5650 (defun special-display-popup-frame (buffer &optional args)
5651 "Display BUFFER in a special frame and return the window chosen.
5652 If BUFFER is already displayed in a visible or iconified frame,
5653 raise that frame. Otherwise, display BUFFER in a way as
5654 specified by optional argument ARGS.
5655
5656 If ARGS is an alist, use it as a list of frame parameters. If
5657 these parameters contain \(same-window . t), display BUFFER in
5658 the selected window. If they contain \(same-frame . t), display
5659 BUFFER in a window on the selected frame.
5660
5661 If ARGS is a list whose car is a symbol, use (car ARGS) as a
5662 function to do the work. Pass it BUFFER as first argument,
5663 and (cdr ARGS) as second."
5664 (if (and args (symbolp (car args)))
5665 (apply (car args) buffer (cdr args))
5666 (let ((window (get-buffer-window buffer 0)))
5667 (or
5668 ;; If we have a window already, make it visible.
5669 (when window
5670 (let ((frame (window-frame window)))
5671 (make-frame-visible frame)
5672 (raise-frame frame)
5673 window))
5674 ;; Reuse the current window if the user requested it.
5675 (when (cdr (assq 'same-window args))
5676 (display-buffer-reuse-window
5677 buffer '(same nil nil) '((reuse-dedicated . 'weak))))
5678 ;; Stay on the same frame if requested.
5679 (when (or (cdr (assq 'same-frame args))
5680 (cdr (assq 'same-window args)))
5681 (or (display-buffer-pop-up-window
5682 buffer '((largest . nil) (lru . nil)))
5683 (display-buffer-reuse-window
5684 buffer '(nil nil nil))))
5685 ;; If no window yet, make one in a new frame.
5686 (let ((frame
5687 (with-current-buffer buffer
5688 (make-frame
5689 (append args (with-no-warnings
5690 special-display-frame-alist))))))
5691 (set-window-buffer (frame-selected-window frame) buffer)
5692 (set-window-dedicated-p (frame-selected-window frame) t)
5693 (frame-selected-window frame))))))
5694 (make-obsolete
5695 'special-display-popup-frame
5696 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
5697
5698 (defcustom special-display-function 'special-display-popup-frame
5699 "Function to call for displaying special buffers.
5700 This function is called with two arguments - the buffer and,
5701 optionally, a list - and should return a window displaying that
5702 buffer. The default value usually makes a separate frame for the
5703 buffer using `special-display-frame-alist' to specify the frame
5704 parameters. See the definition of `special-display-popup-frame'
5705 for how to specify such a function.
5706
5707 A buffer is special when its name is either listed in
5708 `special-display-buffer-names' or matches a regexp in
5709 `special-display-regexps'."
5710 :type 'function
5711 :group 'windows
5712 :group 'frames)
5713 (make-obsolete-variable
5714 'special-display-function
5715 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
5716
5717 (defcustom special-display-buffer-names nil
5718 "List of names of buffers that should be displayed specially.
5719 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5720 its name is in this list, displays the buffer in a way specified
5721 by `special-display-function'. `special-display-popup-frame'
5722 \(the default for `special-display-function') usually displays
5723 the buffer in a separate frame made with the parameters specified
5724 by `special-display-frame-alist'. If `special-display-function'
5725 has been set to some other function, that function is called with
5726 the buffer as first, and nil as second argument.
5727
5728 Alternatively, an element of this list can be specified as
5729 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
5730 name and FRAME-PARAMETERS an alist of \(PARAMETER . VALUE) pairs.
5731 `special-display-popup-frame' will interpret such pairs as frame
5732 parameters when it creates a special frame, overriding the
5733 corresponding values from `special-display-frame-alist'.
5734
5735 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5736 `special-display-popup-frame' displays that buffer in the
5737 selected window. If FRAME-PARAMETERS contains (same-frame . t),
5738 it displays that buffer in a window on the selected frame.
5739
5740 If `special-display-function' specifies some other function than
5741 `special-display-popup-frame', that function is called with the
5742 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5743 argument.
5744
5745 Finally, an element of this list can be also specified as
5746 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5747 `special-display-popup-frame' will call FUNCTION with the buffer
5748 named BUFFER-NAME as first argument, and OTHER-ARGS as the
5749 second. If `special-display-function' specifies some other
5750 function, that function is called with the buffer named
5751 BUFFER-NAME as first, and the element's cdr as second argument.
5752
5753 If this variable appears \"not to work\", because you added a
5754 name to it but the corresponding buffer is displayed in the
5755 selected window, look at the values of `same-window-buffer-names'
5756 and `same-window-regexps'. Those variables take precedence over
5757 this one.
5758
5759 See also `special-display-regexps'."
5760 :type '(repeat
5761 (choice :tag "Buffer"
5762 :value ""
5763 (string :format "%v")
5764 (cons :tag "With parameters"
5765 :format "%v"
5766 :value ("" . nil)
5767 (string :format "%v")
5768 (repeat :tag "Parameters"
5769 (cons :format "%v"
5770 (symbol :tag "Parameter")
5771 (sexp :tag "Value"))))
5772 (list :tag "With function"
5773 :format "%v"
5774 :value ("" . nil)
5775 (string :format "%v")
5776 (function :tag "Function")
5777 (repeat :tag "Arguments" (sexp)))))
5778 :group 'windows
5779 :group 'frames)
5780 (make-obsolete-variable
5781 'special-display-buffer-names
5782 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
5783
5784 ;;;###autoload
5785 (put 'special-display-buffer-names 'risky-local-variable t)
5786
5787 (defcustom special-display-regexps nil
5788 "List of regexps saying which buffers should be displayed specially.
5789 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5790 any regexp in this list matches its name, displays it specially
5791 using `special-display-function'. `special-display-popup-frame'
5792 \(the default for `special-display-function') usually displays
5793 the buffer in a separate frame made with the parameters specified
5794 by `special-display-frame-alist'. If `special-display-function'
5795 has been set to some other function, that function is called with
5796 the buffer as first, and nil as second argument.
5797
5798 Alternatively, an element of this list can be specified as
5799 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
5800 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5801 `special-display-popup-frame' will then interpret these pairs as
5802 frame parameters when creating a special frame for a buffer whose
5803 name matches REGEXP, overriding the corresponding values from
5804 `special-display-frame-alist'.
5805
5806 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5807 `special-display-popup-frame' displays buffers matching REGEXP in
5808 the selected window. \(same-frame . t) in FRAME-PARAMETERS means
5809 to display such buffers in a window on the selected frame.
5810
5811 If `special-display-function' specifies some other function than
5812 `special-display-popup-frame', that function is called with the
5813 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
5814 as second argument.
5815
5816 Finally, an element of this list can be also specified as
5817 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
5818 will then call FUNCTION with the buffer whose name matched
5819 REGEXP as first, and OTHER-ARGS as second argument. If
5820 `special-display-function' specifies some other function, that
5821 function is called with the buffer whose name matched REGEXP
5822 as first, and the element's cdr as second argument.
5823
5824 If this variable appears \"not to work\", because you added a
5825 name to it but the corresponding buffer is displayed in the
5826 selected window, look at the values of `same-window-buffer-names'
5827 and `same-window-regexps'. Those variables take precedence over
5828 this one.
5829
5830 See also `special-display-buffer-names'."
5831 :type '(repeat
5832 (choice :tag "Buffer"
5833 :value ""
5834 (regexp :format "%v")
5835 (cons :tag "With parameters"
5836 :format "%v"
5837 :value ("" . nil)
5838 (regexp :format "%v")
5839 (repeat :tag "Parameters"
5840 (cons :format "%v"
5841 (symbol :tag "Parameter")
5842 (sexp :tag "Value"))))
5843 (list :tag "With function"
5844 :format "%v"
5845 :value ("" . nil)
5846 (regexp :format "%v")
5847 (function :tag "Function")
5848 (repeat :tag "Arguments" (sexp)))))
5849 :group 'windows
5850 :group 'frames)
5851 (make-obsolete-variable
5852 'special-display-regexps
5853 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
5854
5855 (defun special-display-p (buffer-name)
5856 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
5857 More precisely, return t if `special-display-buffer-names' or
5858 `special-display-regexps' contain a string entry equaling or
5859 matching BUFFER-NAME. If `special-display-buffer-names' or
5860 `special-display-regexps' contain a list entry whose car equals
5861 or matches BUFFER-NAME, the return value is the cdr of that
5862 entry."
5863 (let ((buffer-names (with-no-warnings special-display-buffer-names))
5864 (regexps (with-no-warnings special-display-regexps))
5865 tmp)
5866 (cond
5867 ((not (stringp buffer-name)))
5868 ((member buffer-name buffer-names)
5869 t)
5870 ((setq tmp (assoc buffer-name buffer-names))
5871 (cdr tmp))
5872 ((catch 'found
5873 (dolist (regexp regexps)
5874 (cond
5875 ((stringp regexp)
5876 (when (string-match-p regexp buffer-name)
5877 (throw 'found t)))
5878 ((and (consp regexp) (stringp (car regexp))
5879 (string-match-p (car regexp) buffer-name))
5880 (throw 'found (cdr regexp))))))))))
5881 (make-obsolete
5882 'special-display-p
5883 "pass argument to buffer display function instead." "24.1")
5884
5885 (defcustom pop-up-frame-alist nil
5886 "Alist of parameters for automatically generated new frames.
5887 You can set this in your init file; for example,
5888
5889 (setq pop-up-frame-alist '((width . 80) (height . 20)))
5890
5891 If non-nil, the value you specify here is used by the default
5892 `pop-up-frame-function' for the creation of new frames.
5893
5894 Since `pop-up-frame-function' is used by `display-buffer' for
5895 making new frames, any value specified here by default affects
5896 the automatic generation of new frames via `display-buffer' and
5897 all functions based on it. The behavior of `make-frame' is not
5898 affected by this variable."
5899 :type '(repeat (cons :format "%v"
5900 (symbol :tag "Parameter")
5901 (sexp :tag "Value")))
5902 :group 'frames)
5903 (make-obsolete-variable
5904 'pop-up-frame-alist
5905 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
5906
5907 (defcustom pop-up-frame-function
5908 (lambda () (make-frame pop-up-frame-alist))
5909 "Function used by `display-buffer' for creating a new frame.
5910 This function is called with no arguments and should return a new
5911 frame. The default value calls `make-frame' with the argument
5912 `pop-up-frame-alist'."
5913 :type 'function
5914 :group 'frames)
5915 (make-obsolete-variable
5916 'pop-up-frame-function
5917 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
5918
5919 (defcustom pop-up-frames 'unset ; nil
5920 "Whether `display-buffer' should make a separate frame.
5921 If nil, never make a separate frame.
5922 If the value is `graphic-only', make a separate frame
5923 on graphic displays only.
5924 If this is the symbol unset, the option was not set and is
5925 ignored.
5926 Any other non-nil value means always make a separate frame."
5927 :type '(choice
5928 (const :tag "Unset" unset)
5929 (const :tag "Never" nil)
5930 (const :tag "On graphic displays only" graphic-only)
5931 (const :tag "Always" t))
5932 :version "24.1"
5933 :group 'windows
5934 :group 'frames)
5935 (make-obsolete-variable
5936 'pop-up-frames
5937 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
5938
5939 (defcustom display-buffer-reuse-frames 'unset ; nil
5940 "Set and non-nil means `display-buffer' should reuse frames.
5941 If the buffer in question is already displayed in a frame, raise
5942 that frame."
5943 :type 'boolean
5944 :version "24.1"
5945 :group 'windows
5946 :group 'frames)
5947 (make-obsolete-variable
5948 'display-buffer-reuse-frames
5949 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
5950
5951 (defcustom pop-up-windows 'unset ; t
5952 "Set and non-nil means `display-buffer' should make a new window."
5953 :type 'boolean
5954 :version "24.1"
5955 :group 'windows)
5956 (make-obsolete-variable
5957 'pop-up-windows
5958 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
5959
5960 (defcustom split-window-preferred-function 'split-window-sensibly
5961 "Function called by `display-buffer' to split a window.
5962 This function is called with a window as single argument and is
5963 supposed to split that window and return the new window. If the
5964 window can (or shall) not be split, it is supposed to return nil.
5965
5966 The default is to call the function `split-window-sensibly' which
5967 tries to split the window in a way which seems most suitable.
5968 You can customize the options `split-height-threshold' and/or
5969 `split-width-threshold' in order to have `split-window-sensibly'
5970 prefer either vertical or horizontal splitting.
5971
5972 If you set this to any other function, bear in mind that
5973 `display-buffer' may call that function repeatedly; the option
5974 `pop-up-windows' controls which windows may become the argument
5975 of this function.
5976
5977 The window selected at the time `display-buffer' was invoked is
5978 still selected when this function is called. Hence you can
5979 compare the window argument with the value of `selected-window'
5980 if you intend to split the selected window instead or if you do
5981 not want to split the selected window."
5982 :type 'function
5983 :version "23.1"
5984 :group 'windows)
5985 (make-obsolete-variable
5986 'split-window-preferred-function
5987 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
5988
5989 (defcustom split-height-threshold 80
5990 "Minimum height for splitting a window to display a buffer.
5991 If this is an integer, `display-buffer' can split a window
5992 vertically only if it has at least this many lines. If this is
5993 nil, `display-buffer' does not split windows vertically. If a
5994 window is the only window on its frame, `display-buffer' may
5995 split it vertically disregarding the value of this variable."
5996 :type '(choice (const nil) (integer :tag "lines"))
5997 :version "23.1"
5998 :group 'windows)
5999 (make-obsolete-variable
6000 'split-height-threshold
6001 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
6002
6003 (defcustom split-width-threshold 160
6004 "Minimum width for splitting a window to display a buffer.
6005 If this is an integer, `display-buffer' can split a window
6006 horizontally only if it has at least this many columns. If this
6007 is nil, `display-buffer' cannot split windows horizontally."
6008 :type '(choice (const nil) (integer :tag "columns"))
6009 :version "23.1"
6010 :group 'windows)
6011 (make-obsolete-variable
6012 'split-width-threshold
6013 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
6014
6015 (defcustom even-window-heights t
6016 "If non-nil `display-buffer' will try to even window heights.
6017 Otherwise `display-buffer' will leave the window configuration
6018 alone. Heights are evened only when `display-buffer' reuses a
6019 window that appears above or below the selected window."
6020 :type 'boolean
6021 :version "23.1"
6022 :group 'windows)
6023 (make-obsolete-variable
6024 'even-window-heights
6025 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
6026
6027 (defvar display-buffer-mark-dedicated 'unset ; nil
6028 "Set and non-nil means `display-buffer' marks the windows it creates as dedicated.
6029 The actual non-nil value of this variable will be copied to the
6030 `window-dedicated-p' flag.")
6031 (make-obsolete-variable
6032 'display-buffer-mark-dedicated
6033 "use `display-buffer-alist' or 2nd arg of `display-buffer' instead." "24.1")
6034
6035 (defun window-splittable-p (window &optional horizontal)
6036 "Return non-nil if `split-window-sensibly' may split WINDOW.
6037 Optional argument HORIZONTAL nil or omitted means check whether
6038 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
6039 non-nil means check whether WINDOW may be split horizontally.
6040
6041 WINDOW may be split vertically when the following conditions
6042 hold:
6043 - `window-size-fixed' is either nil or equals `width' for the
6044 buffer of WINDOW.
6045 - `split-height-threshold' is an integer and WINDOW is at least as
6046 high as `split-height-threshold'.
6047 - When WINDOW is split evenly, the emanating windows are at least
6048 `window-min-height' lines tall and can accommodate at least one
6049 line plus - if WINDOW has one - a mode line.
6050
6051 WINDOW may be split horizontally when the following conditions
6052 hold:
6053 - `window-size-fixed' is either nil or equals `height' for the
6054 buffer of WINDOW.
6055 - `split-width-threshold' is an integer and WINDOW is at least as
6056 wide as `split-width-threshold'.
6057 - When WINDOW is split evenly, the emanating windows are at least
6058 `window-min-width' or two (whichever is larger) columns wide."
6059 (when (window-live-p window)
6060 (with-current-buffer (window-buffer window)
6061 (if horizontal
6062 ;; A window can be split horizontally when its width is not
6063 ;; fixed, it is at least `split-width-threshold' columns wide
6064 ;; and at least twice as wide as `window-min-width' and 2 (the
6065 ;; latter value is hardcoded).
6066 (and (memq window-size-fixed '(nil height))
6067 ;; Testing `window-full-width-p' here hardly makes any
6068 ;; sense nowadays. This can be done more intuitively by
6069 ;; setting up `split-width-threshold' appropriately.
6070 (numberp split-width-threshold)
6071 (>= (window-width window)
6072 (max split-width-threshold
6073 (* 2 (max window-min-width 2)))))
6074 ;; A window can be split vertically when its height is not
6075 ;; fixed, it is at least `split-height-threshold' lines high,
6076 ;; and it is at least twice as high as `window-min-height' and 2
6077 ;; if it has a modeline or 1.
6078 (and (memq window-size-fixed '(nil width))
6079 (numberp split-height-threshold)
6080 (>= (window-height window)
6081 (max split-height-threshold
6082 (* 2 (max window-min-height
6083 (if mode-line-format 2 1))))))))))
6084 (make-obsolete
6085 'window-splittable-p "use 2nd arg of `display-buffer' instead." "24.1")
6086
6087 (defun split-window-sensibly (window)
6088 "Split WINDOW in a way suitable for `display-buffer'.
6089 If `split-height-threshold' specifies an integer, WINDOW is at
6090 least `split-height-threshold' lines tall and can be split
6091 vertically, split WINDOW into two windows one above the other and
6092 return the lower window. Otherwise, if `split-width-threshold'
6093 specifies an integer, WINDOW is at least `split-width-threshold'
6094 columns wide and can be split horizontally, split WINDOW into two
6095 windows side by side and return the window on the right. If this
6096 can't be done either and WINDOW is the only window on its frame,
6097 try to split WINDOW vertically disregarding any value specified
6098 by `split-height-threshold'. If that succeeds, return the lower
6099 window. Return nil otherwise.
6100
6101 By default `display-buffer' routines call this function to split
6102 the largest or least recently used window. To change the default
6103 customize the option `split-window-preferred-function'.
6104
6105 You can enforce this function to not split WINDOW horizontally,
6106 by setting \(or binding) the variable `split-width-threshold' to
6107 nil. If, in addition, you set `split-height-threshold' to zero,
6108 chances increase that this function does split WINDOW vertically.
6109
6110 In order to not split WINDOW vertically, set \(or bind) the
6111 variable `split-height-threshold' to nil. Additionally, you can
6112 set `split-width-threshold' to zero to make a horizontal split
6113 more likely to occur.
6114
6115 Have a look at the function `window-splittable-p' if you want to
6116 know how `split-window-sensibly' determines whether WINDOW can be
6117 split."
6118 (or (and (with-no-warnings (window-splittable-p window))
6119 ;; Split window vertically.
6120 (with-selected-window window
6121 (split-window-vertically)))
6122 (and (with-no-warnings (window-splittable-p window t))
6123 ;; Split window horizontally.
6124 (with-selected-window window
6125 (split-window-horizontally)))
6126 (and (eq window (frame-root-window (window-frame window)))
6127 (not (window-minibuffer-p window))
6128 ;; If WINDOW is the only window on its frame and is not the
6129 ;; minibuffer window, try to split it vertically disregarding
6130 ;; the value of `split-height-threshold'.
6131 (let ((split-height-threshold 0))
6132 (when (with-no-warnings (window-splittable-p window))
6133 (with-selected-window window
6134 (split-window-vertically)))))))
6135 (make-obsolete
6136 'split-window-sensibly "use 2nd arg of `display-buffer' instead." "24.1")
6137
6138 ;; Functions for converting Emacs 23 buffer display options to buffer
6139 ;; display specifiers.
6140 (defun display-buffer-alist-add (identifiers specifiers &optional no-custom)
6141 "Helper function for `display-buffer-alist-set'."
6142 (unless identifiers
6143 (setq identifiers '((regexp . ".*"))))
6144 (unless (atom specifiers)
6145 (setq specifiers (delq nil specifiers)))
6146
6147 (if no-custom
6148 (setq display-buffer-alist
6149 (cons (cons identifiers specifiers) display-buffer-alist))
6150 (customize-set-variable
6151 'display-buffer-alist
6152 (cons (cons identifiers specifiers) display-buffer-alist))))
6153
6154 (defun display-buffer-alist-set (&optional no-custom add)
6155 "Set `display-buffer-alist' from Emacs 23 buffer display options.
6156 Optional argument NO-CUSTOM nil means use `customize-set-variable'
6157 to set the value of `display-buffer-alist'. NO-CUSTOM non-nil
6158 means to use `setq' instead.
6159
6160 Optional argument ADD nil means to replace the actual value of
6161 `display-buffer-alist' with the value calculated here. ADD
6162 non-nil means prepend the value calculated here to the current
6163 value of `display-buffer-alist'."
6164 (unless add
6165 (if no-custom
6166 (setq display-buffer-alist nil)
6167 (customize-set-variable 'display-buffer-alist nil)))
6168
6169 ;; Disable warnings, there are too many obsolete options here.
6170 (with-no-warnings
6171 ;; `pop-up-windows'
6172 (display-buffer-alist-add
6173 nil
6174 (let ((fun (unless (eq split-window-preferred-function
6175 'split-window-sensibly)
6176 ;; `split-window-sensibly' has been merged into the
6177 ;; `display-buffer-split-window' code as `nil'.
6178 split-window-preferred-function))
6179 (min-height
6180 (if (numberp split-height-threshold)
6181 (/ split-height-threshold 2)
6182 ;; Undocumented hack.
6183 1.0))
6184 (min-width
6185 (if (numberp split-width-threshold)
6186 (/ split-width-threshold 2)
6187 ;; Undocumented hack.
6188 1.0)))
6189 (list
6190 'pop-up-window
6191 (when pop-up-windows ; unset qualifies as t
6192 (list
6193 'pop-up-window
6194 (cons 'largest fun)
6195 (cons 'lru fun)))
6196 (cons 'pop-up-window-min-height min-height)
6197 (cons 'pop-up-window-min-width min-width)))
6198 no-custom)
6199
6200 ;; `pop-up-frames'
6201 (display-buffer-alist-add
6202 nil
6203 (list
6204 'pop-up-frame
6205 (unless (memq pop-up-frames '(nil unset))
6206 (list 'pop-up-frame pop-up-frames))
6207 (when pop-up-frame-function
6208 (cons 'pop-up-frame-function pop-up-frame-function))
6209 (when pop-up-frame-alist
6210 (cons 'pop-up-frame-alist pop-up-frame-alist)))
6211 no-custom)
6212
6213 ;; `special-display-regexps'
6214 (dolist (entry special-display-regexps)
6215 (cond
6216 ((stringp entry)
6217 ;; Plain string.
6218 (display-buffer-alist-add
6219 `((regexp . ,entry))
6220 (list
6221 'fun-with-args
6222 (list 'fun-with-args special-display-function
6223 special-display-frame-alist))
6224 no-custom))
6225 ((consp entry)
6226 (let ((name (car entry))
6227 (rest (cdr entry)))
6228 (cond
6229 ((functionp (car rest))
6230 ;; A function.
6231 (display-buffer-alist-add
6232 `((name . ,name))
6233 (list
6234 'fun-with-args
6235 ;; Weary.
6236 (list 'fun-with-args (car rest) (cadr rest)))
6237 no-custom))
6238 ((listp rest)
6239 ;; A list of parameters.
6240 (cond
6241 ((assq 'same-window rest)
6242 (display-buffer-alist-add
6243 `((name . ,name))
6244 (list 'reuse-window
6245 (list 'reuse-window 'same)
6246 (list 'reuse-window-dedicated 'weak))
6247 no-custom))
6248 ((assq 'same-frame rest)
6249 (display-buffer-alist-add
6250 `((name . ,name)) (list 'same-frame) no-custom))
6251 (t
6252 (display-buffer-alist-add
6253 `((name . ,name))
6254 (list
6255 'fun-with-args
6256 (list 'fun-with-args special-display-function
6257 special-display-frame-alist))
6258 no-custom)))))))))
6259
6260 ;; `special-display-buffer-names'
6261 (dolist (entry special-display-buffer-names)
6262 (cond
6263 ((stringp entry)
6264 ;; Plain string.
6265 (display-buffer-alist-add
6266 `((name . ,entry))
6267 (list
6268 'fun-with-args
6269 (list 'fun-with-args special-display-function
6270 special-display-frame-alist))
6271 no-custom))
6272 ((consp entry)
6273 (let ((name (car entry))
6274 (rest (cdr entry)))
6275 (cond
6276 ((functionp (car rest))
6277 ;; A function.
6278 (display-buffer-alist-add
6279 `((name . ,name))
6280 (list
6281 'fun-with-args
6282 ;; Weary.
6283 (list 'fun-with-args (car rest) (cadr rest)))
6284 no-custom))
6285 ((listp rest)
6286 ;; A list of parameters.
6287 (cond
6288 ((assq 'same-window rest)
6289 (display-buffer-alist-add
6290 `((name . ,name))
6291 (list 'reuse-window
6292 (list 'reuse-window 'same)
6293 (list 'reuse-window-dedicated 'weak))
6294 no-custom))
6295 ((assq 'same-frame rest)
6296 (display-buffer-alist-add
6297 `((name . ,name)) (list 'same-frame) no-custom))
6298 (t
6299 (display-buffer-alist-add
6300 `((name . ,name))
6301 (list
6302 'fun-with-args
6303 (list 'fun-with-args special-display-function
6304 special-display-frame-alist))
6305 no-custom)))))))))
6306
6307 ;; `same-window-regexps'
6308 (dolist (entry same-window-regexps)
6309 (cond
6310 ((stringp entry)
6311 (display-buffer-alist-add
6312 `((regexp . ,entry))
6313 (list 'reuse-window (list 'reuse-window 'same))
6314 no-custom))
6315 ((consp entry)
6316 (display-buffer-alist-add
6317 `((regexp . ,(car entry)))
6318 (list 'reuse-window (list 'reuse-window 'same))
6319 no-custom))))
6320
6321 ;; `same-window-buffer-names'
6322 (dolist (entry same-window-buffer-names)
6323 (cond
6324 ((stringp entry)
6325 (display-buffer-alist-add
6326 `((name . ,entry))
6327 (list 'reuse-window (list 'reuse-window 'same))
6328 no-custom))
6329 ((consp entry)
6330 (display-buffer-alist-add
6331 `((name . ,(car entry)))
6332 (list 'reuse-window (list 'reuse-window 'same))
6333 no-custom))))
6334
6335 ;; `reuse-window'
6336 (display-buffer-alist-add
6337 nil
6338 (list
6339 'reuse-window
6340 (list 'reuse-window nil 'same
6341 (unless (and (memq display-buffer-reuse-frames '(nil unset))
6342 (memq pop-up-frames '(nil unset)))
6343 ;; "0" (all visible and iconified frames) is hardcoded in
6344 ;; Emacs 23.
6345 0))
6346 (when even-window-heights
6347 (cons 'reuse-window-even-sizes t)))
6348 no-custom)
6349
6350 ;; `display-buffer-mark-dedicated'
6351 (unless (memq display-buffer-mark-dedicated '(nil unset))
6352 (display-buffer-alist-add
6353 nil
6354 (list
6355 (cons 'dedicated display-buffer-mark-dedicated))
6356 no-custom)))
6357
6358 display-buffer-alist)
6359 \f
6360 (defun set-window-text-height (window height)
6361 "Set the height in lines of the text display area of WINDOW to HEIGHT.
6362 WINDOW must be a live window. HEIGHT doesn't include the mode
6363 line or header line, if any, or any partial-height lines in the
6364 text display area.
6365
6366 Note that the current implementation of this function cannot
6367 always set the height exactly, but attempts to be conservative,
6368 by allocating more lines than are actually needed in the case
6369 where some error may be present."
6370 (setq window (normalize-live-window window))
6371 (let ((delta (- height (window-text-height window))))
6372 (unless (zerop delta)
6373 ;; Setting window-min-height to a value like 1 can lead to very
6374 ;; bizarre displays because it also allows Emacs to make *other*
6375 ;; windows 1-line tall, which means that there's no more space for
6376 ;; the modeline.
6377 (let ((window-min-height (min 2 height))) ; One text line plus a modeline.
6378 (resize-window window delta)))))
6379
6380 (defun enlarge-window-horizontally (delta)
6381 "Make selected window DELTA columns wider.
6382 Interactively, if no argument is given, make selected window one
6383 column wider."
6384 (interactive "p")
6385 (enlarge-window delta t))
6386
6387 (defun shrink-window-horizontally (delta)
6388 "Make selected window DELTA columns narrower.
6389 Interactively, if no argument is given, make selected window one
6390 column narrower."
6391 (interactive "p")
6392 (shrink-window delta t))
6393
6394 (defun count-screen-lines (&optional beg end count-final-newline window)
6395 "Return the number of screen lines in the region.
6396 The number of screen lines may be different from the number of actual lines,
6397 due to line breaking, display table, etc.
6398
6399 Optional arguments BEG and END default to `point-min' and `point-max'
6400 respectively.
6401
6402 If region ends with a newline, ignore it unless optional third argument
6403 COUNT-FINAL-NEWLINE is non-nil.
6404
6405 The optional fourth argument WINDOW specifies the window used for obtaining
6406 parameters such as width, horizontal scrolling, and so on. The default is
6407 to use the selected window's parameters.
6408
6409 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6410 regardless of which buffer is displayed in WINDOW. This makes possible to use
6411 `count-screen-lines' in any buffer, whether or not it is currently displayed
6412 in some window."
6413 (unless beg
6414 (setq beg (point-min)))
6415 (unless end
6416 (setq end (point-max)))
6417 (if (= beg end)
6418 0
6419 (save-excursion
6420 (save-restriction
6421 (widen)
6422 (narrow-to-region (min beg end)
6423 (if (and (not count-final-newline)
6424 (= ?\n (char-before (max beg end))))
6425 (1- (max beg end))
6426 (max beg end)))
6427 (goto-char (point-min))
6428 (1+ (vertical-motion (buffer-size) window))))))
6429
6430 (defun window-buffer-height (window)
6431 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
6432 (with-current-buffer (window-buffer window)
6433 (max 1
6434 (count-screen-lines (point-min) (point-max)
6435 ;; If buffer ends with a newline, ignore it when
6436 ;; counting height unless point is after it.
6437 (eobp)
6438 window))))
6439
6440 ;;; Resizing buffers to fit their contents exactly.
6441 (defun fit-window-to-buffer (&optional window max-height min-height override)
6442 "Adjust height of WINDOW to display its buffer's contents exactly.
6443 WINDOW can be any live window and defaults to the selected one.
6444
6445 Optional argument MAX-HEIGHT specifies the maximum height of
6446 WINDOW and defaults to the height of WINDOW's frame. Optional
6447 argument MIN-HEIGHT specifies the minimum height of WINDOW and
6448 defaults to `window-min-height'. Both, MAX-HEIGHT and MIN-HEIGHT
6449 are specified in lines and include the mode line and header line,
6450 if any.
6451
6452 Optional argument OVERRIDE non-nil means override restrictions
6453 imposed by `window-min-height' and `window-min-width' on the size
6454 of WINDOW.
6455
6456 Return the number of lines by which WINDOW was enlarged or
6457 shrunk. If an error occurs during resizing, return nil but don't
6458 signal an error.
6459
6460 Note that even if this function makes WINDOW large enough to show
6461 _all_ lines of its buffer you might not see the first lines when
6462 WINDOW was scrolled."
6463 (interactive)
6464 ;; Do all the work in WINDOW and its buffer and restore the selected
6465 ;; window and the current buffer when we're done.
6466 (setq window (normalize-live-window window))
6467 ;; Can't resize a full height or fixed-size window.
6468 (unless (or (window-size-fixed-p window)
6469 (window-full-height-p window))
6470 ;; `with-selected-window' should orderly restore the current buffer.
6471 (with-selected-window window
6472 ;; We are in WINDOW's buffer now.
6473 (let* ( ;; Adjust MIN-HEIGHT.
6474 (min-height
6475 (if override
6476 (window-min-size window nil window)
6477 (max (or min-height window-min-height)
6478 window-safe-min-height)))
6479 (max-window-height
6480 (window-total-size (frame-root-window window)))
6481 ;; Adjust MAX-HEIGHT.
6482 (max-height
6483 (if (or override (not max-height))
6484 max-window-height
6485 (min max-height max-window-height)))
6486 ;; Make `desired-height' the height necessary to show
6487 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
6488 ;; and MAX-HEIGHT.
6489 (desired-height
6490 (max
6491 (min
6492 (+ (count-screen-lines)
6493 ;; For non-minibuffers count the mode line, if any.
6494 (if (and (not (window-minibuffer-p window))
6495 mode-line-format)
6496 1
6497 0)
6498 ;; Count the header line, if any.
6499 (if header-line-format 1 0))
6500 max-height)
6501 min-height))
6502 (desired-delta
6503 (- desired-height (window-total-size window)))
6504 (delta
6505 (if (> desired-delta 0)
6506 (min desired-delta
6507 (window-max-delta window nil window))
6508 (max desired-delta
6509 (- (window-min-delta window nil window))))))
6510 ;; This `condition-case' shouldn't be necessary, but who knows?
6511 (condition-case nil
6512 (if (zerop delta)
6513 ;; Return zero if DELTA became zero in the proces.
6514 0
6515 ;; Don't try to redisplay with the cursor at the end on its
6516 ;; own line--that would force a scroll and spoil things.
6517 (when (and (eobp) (bolp) (not (bobp)))
6518 ;; It's silly to put `point' at the end of the previous
6519 ;; line and so maybe force horizontal scrolling.
6520 (set-window-point window (line-beginning-position 0)))
6521 ;; Call `resize-window' with OVERRIDE argument equal WINDOW.
6522 (resize-window window delta nil window)
6523 ;; Check if the last line is surely fully visible. If
6524 ;; not, enlarge the window.
6525 (let ((end (save-excursion
6526 (goto-char (point-max))
6527 (when (and (bolp) (not (bobp)))
6528 ;; Don't include final newline.
6529 (backward-char 1))
6530 (when truncate-lines
6531 ;; If line-wrapping is turned off, test the
6532 ;; beginning of the last line for
6533 ;; visibility instead of the end, as the
6534 ;; end of the line could be invisible by
6535 ;; virtue of extending past the edge of the
6536 ;; window.
6537 (forward-line 0))
6538 (point))))
6539 (set-window-vscroll window 0)
6540 ;; This loop might in some rare pathological cases raise
6541 ;; an error - another reason for the `condition-case'.
6542 (while (and (< desired-height max-height)
6543 (= desired-height (window-total-size))
6544 (not (pos-visible-in-window-p end)))
6545 (resize-window window 1 nil window)
6546 (setq desired-height (1+ desired-height)))))
6547 (error (setq delta nil)))
6548 delta))))
6549
6550 (defun window-safely-shrinkable-p (&optional window)
6551 "Return t if WINDOW can be shrunk without shrinking other windows.
6552 WINDOW defaults to the selected window."
6553 (with-selected-window (or window (selected-window))
6554 (let ((edges (window-edges)))
6555 ;; The following doesn't satisfy the doc-string's claim when
6556 ;; window and previous-/next-window are not part of the same
6557 ;; combination but still share a common edge. Using
6558 ;; `window-iso-combined-p' instead should handle that.
6559 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
6560 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
6561 (make-obsolete
6562 'window-safely-shrinkable-p "use `window-iso-combined-p' instead." "24.1")
6563
6564 (defun shrink-window-if-larger-than-buffer (&optional window)
6565 "Shrink height of WINDOW if its buffer doesn't need so many lines.
6566 More precisely, shrink WINDOW vertically to be as small as
6567 possible, while still showing the full contents of its buffer.
6568 WINDOW defaults to the selected window.
6569
6570 Do not shrink WINDOW to less than `window-min-height' lines. Do
6571 nothing if the buffer contains more lines than the present window
6572 height, or if some of the window's contents are scrolled out of
6573 view, or if shrinking this window would also shrink another
6574 window, or if the window is the only window of its frame.
6575
6576 Return non-nil if the window was shrunk, nil otherwise."
6577 (interactive)
6578 (setq window (normalize-live-window window))
6579 ;; Make sure that WINDOW is vertically combined and `point-min' is
6580 ;; visible (for whatever reason that's needed). The remaining issues
6581 ;; should be taken care of by `fit-window-to-buffer'.
6582 (when (and (window-iso-combined-p window)
6583 (pos-visible-in-window-p (point-min) window))
6584 (fit-window-to-buffer window (window-total-size window))))
6585 \f
6586 (defun kill-buffer-and-window ()
6587 "Kill the current buffer and delete the selected window."
6588 (interactive)
6589 (let ((window-to-delete (selected-window))
6590 (buffer-to-kill (current-buffer))
6591 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
6592 (unwind-protect
6593 (progn
6594 (add-hook 'kill-buffer-hook delete-window-hook t t)
6595 (if (kill-buffer (current-buffer))
6596 ;; If `delete-window' failed before, we rerun it to regenerate
6597 ;; the error so it can be seen in the echo area.
6598 (when (eq (selected-window) window-to-delete)
6599 (delete-window))))
6600 ;; If the buffer is not dead for some reason (probably because
6601 ;; of a `quit' signal), remove the hook again.
6602 (ignore-errors
6603 (with-current-buffer buffer-to-kill
6604 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
6605
6606 (defun quit-window (&optional kill window)
6607 "Quit WINDOW and bury its buffer.
6608 With a prefix argument, kill the buffer instead. WINDOW defaults
6609 to the selected window.
6610
6611 If WINDOW is non-nil, dedicated, or a minibuffer window, delete
6612 it and, if it's alone on its frame, its frame too. Otherwise, or
6613 if deleting WINDOW fails in any of the preceding cases, display
6614 another buffer in WINDOW using `switch-to-buffer'.
6615
6616 Optional argument KILL non-nil means kill WINDOW's buffer.
6617 Otherwise, bury WINDOW's buffer, see `bury-buffer'."
6618 (interactive "P")
6619 (let ((buffer (window-buffer window)))
6620 (if (or window
6621 (window-minibuffer-p window)
6622 (window-dedicated-p window))
6623 ;; WINDOW is either non-nil, a minibuffer window, or dedicated;
6624 ;; try to delete it.
6625 (let* ((window (or window (selected-window)))
6626 (frame (window-frame window)))
6627 (if (frame-root-window-p window)
6628 ;; WINDOW is alone on its frame.
6629 (delete-frame frame)
6630 ;; There are other windows on its frame, delete WINDOW.
6631 (delete-window window)))
6632 ;; Otherwise, switch to another buffer in the selected window.
6633 (switch-to-buffer nil))
6634
6635 ;; Deal with the buffer.
6636 (if kill
6637 (kill-buffer buffer)
6638 (bury-buffer buffer))))
6639 \f
6640 (defvar recenter-last-op nil
6641 "Indicates the last recenter operation performed.
6642 Possible values: `top', `middle', `bottom', integer or float numbers.")
6643
6644 (defcustom recenter-positions '(middle top bottom)
6645 "Cycling order for `recenter-top-bottom'.
6646 A list of elements with possible values `top', `middle', `bottom',
6647 integer or float numbers that define the cycling order for
6648 the command `recenter-top-bottom'.
6649
6650 Top and bottom destinations are `scroll-margin' lines the from true
6651 window top and bottom. Middle redraws the frame and centers point
6652 vertically within the window. Integer number moves current line to
6653 the specified absolute window-line. Float number between 0.0 and 1.0
6654 means the percentage of the screen space from the top. The default
6655 cycling order is middle -> top -> bottom."
6656 :type '(repeat (choice
6657 (const :tag "Top" top)
6658 (const :tag "Middle" middle)
6659 (const :tag "Bottom" bottom)
6660 (integer :tag "Line number")
6661 (float :tag "Percentage")))
6662 :version "23.2"
6663 :group 'windows)
6664
6665 (defun recenter-top-bottom (&optional arg)
6666 "Move current buffer line to the specified window line.
6667 With no prefix argument, successive calls place point according
6668 to the cycling order defined by `recenter-positions'.
6669
6670 A prefix argument is handled like `recenter':
6671 With numeric prefix ARG, move current line to window-line ARG.
6672 With plain `C-u', move current line to window center."
6673 (interactive "P")
6674 (cond
6675 (arg (recenter arg)) ; Always respect ARG.
6676 (t
6677 (setq recenter-last-op
6678 (if (eq this-command last-command)
6679 (car (or (cdr (member recenter-last-op recenter-positions))
6680 recenter-positions))
6681 (car recenter-positions)))
6682 (let ((this-scroll-margin
6683 (min (max 0 scroll-margin)
6684 (truncate (/ (window-body-height) 4.0)))))
6685 (cond ((eq recenter-last-op 'middle)
6686 (recenter))
6687 ((eq recenter-last-op 'top)
6688 (recenter this-scroll-margin))
6689 ((eq recenter-last-op 'bottom)
6690 (recenter (- -1 this-scroll-margin)))
6691 ((integerp recenter-last-op)
6692 (recenter recenter-last-op))
6693 ((floatp recenter-last-op)
6694 (recenter (round (* recenter-last-op (window-height))))))))))
6695
6696 (define-key global-map [?\C-l] 'recenter-top-bottom)
6697
6698 (defun move-to-window-line-top-bottom (&optional arg)
6699 "Position point relative to window.
6700
6701 With a prefix argument ARG, acts like `move-to-window-line'.
6702
6703 With no argument, positions point at center of window.
6704 Successive calls position point at positions defined
6705 by `recenter-positions'."
6706 (interactive "P")
6707 (cond
6708 (arg (move-to-window-line arg)) ; Always respect ARG.
6709 (t
6710 (setq recenter-last-op
6711 (if (eq this-command last-command)
6712 (car (or (cdr (member recenter-last-op recenter-positions))
6713 recenter-positions))
6714 (car recenter-positions)))
6715 (let ((this-scroll-margin
6716 (min (max 0 scroll-margin)
6717 (truncate (/ (window-body-height) 4.0)))))
6718 (cond ((eq recenter-last-op 'middle)
6719 (call-interactively 'move-to-window-line))
6720 ((eq recenter-last-op 'top)
6721 (move-to-window-line this-scroll-margin))
6722 ((eq recenter-last-op 'bottom)
6723 (move-to-window-line (- -1 this-scroll-margin)))
6724 ((integerp recenter-last-op)
6725 (move-to-window-line recenter-last-op))
6726 ((floatp recenter-last-op)
6727 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
6728
6729 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
6730 \f
6731 ;;; Scrolling commands.
6732
6733 ;;; Scrolling commands which does not signal errors at top/bottom
6734 ;;; of buffer at first key-press (instead moves to top/bottom
6735 ;;; of buffer).
6736
6737 (defcustom scroll-error-top-bottom nil
6738 "Move point to top/bottom of buffer before signalling a scrolling error.
6739 A value of nil means just signal an error if no more scrolling possible.
6740 A value of t means point moves to the beginning or the end of the buffer
6741 \(depending on scrolling direction) when no more scrolling possible.
6742 When point is already on that position, then signal an error."
6743 :type 'boolean
6744 :group 'scrolling
6745 :version "24.1")
6746
6747 (defun scroll-up-command (&optional arg)
6748 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
6749 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
6750 scroll window further, move cursor to the bottom line.
6751 When point is already on that position, then signal an error.
6752 A near full screen is `next-screen-context-lines' less than a full screen.
6753 Negative ARG means scroll downward.
6754 If ARG is the atom `-', scroll downward by nearly full screen."
6755 (interactive "^P")
6756 (cond
6757 ((null scroll-error-top-bottom)
6758 (scroll-up arg))
6759 ((eq arg '-)
6760 (scroll-down-command nil))
6761 ((< (prefix-numeric-value arg) 0)
6762 (scroll-down-command (- (prefix-numeric-value arg))))
6763 ((eobp)
6764 (scroll-up arg)) ; signal error
6765 (t
6766 (condition-case nil
6767 (scroll-up arg)
6768 (end-of-buffer
6769 (if arg
6770 ;; When scrolling by ARG lines can't be done,
6771 ;; move by ARG lines instead.
6772 (forward-line arg)
6773 ;; When ARG is nil for full-screen scrolling,
6774 ;; move to the bottom of the buffer.
6775 (goto-char (point-max))))))))
6776
6777 (put 'scroll-up-command 'scroll-command t)
6778
6779 (defun scroll-down-command (&optional arg)
6780 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
6781 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
6782 scroll window further, move cursor to the top line.
6783 When point is already on that position, then signal an error.
6784 A near full screen is `next-screen-context-lines' less than a full screen.
6785 Negative ARG means scroll upward.
6786 If ARG is the atom `-', scroll upward by nearly full screen."
6787 (interactive "^P")
6788 (cond
6789 ((null scroll-error-top-bottom)
6790 (scroll-down arg))
6791 ((eq arg '-)
6792 (scroll-up-command nil))
6793 ((< (prefix-numeric-value arg) 0)
6794 (scroll-up-command (- (prefix-numeric-value arg))))
6795 ((bobp)
6796 (scroll-down arg)) ; signal error
6797 (t
6798 (condition-case nil
6799 (scroll-down arg)
6800 (beginning-of-buffer
6801 (if arg
6802 ;; When scrolling by ARG lines can't be done,
6803 ;; move by ARG lines instead.
6804 (forward-line (- arg))
6805 ;; When ARG is nil for full-screen scrolling,
6806 ;; move to the top of the buffer.
6807 (goto-char (point-min))))))))
6808
6809 (put 'scroll-down-command 'scroll-command t)
6810
6811 ;;; Scrolling commands which scroll a line instead of full screen.
6812
6813 (defun scroll-up-line (&optional arg)
6814 "Scroll text of selected window upward ARG lines; or one line if no ARG.
6815 If ARG is omitted or nil, scroll upward by one line.
6816 This is different from `scroll-up-command' that scrolls a full screen."
6817 (interactive "p")
6818 (scroll-up (or arg 1)))
6819
6820 (put 'scroll-up-line 'scroll-command t)
6821
6822 (defun scroll-down-line (&optional arg)
6823 "Scroll text of selected window down ARG lines; or one line if no ARG.
6824 If ARG is omitted or nil, scroll down by one line.
6825 This is different from `scroll-down-command' that scrolls a full screen."
6826 (interactive "p")
6827 (scroll-down (or arg 1)))
6828
6829 (put 'scroll-down-line 'scroll-command t)
6830
6831 \f
6832 (defun scroll-other-window-down (lines)
6833 "Scroll the \"other window\" down.
6834 For more details, see the documentation for `scroll-other-window'."
6835 (interactive "P")
6836 (scroll-other-window
6837 ;; Just invert the argument's meaning.
6838 ;; We can do that without knowing which window it will be.
6839 (if (eq lines '-) nil
6840 (if (null lines) '-
6841 (- (prefix-numeric-value lines))))))
6842
6843 (defun beginning-of-buffer-other-window (arg)
6844 "Move point to the beginning of the buffer in the other window.
6845 Leave mark at previous position.
6846 With arg N, put point N/10 of the way from the true beginning."
6847 (interactive "P")
6848 (let ((orig-window (selected-window))
6849 (window (other-window-for-scrolling)))
6850 ;; We use unwind-protect rather than save-window-excursion
6851 ;; because the latter would preserve the things we want to change.
6852 (unwind-protect
6853 (progn
6854 (select-window window)
6855 ;; Set point and mark in that window's buffer.
6856 (with-no-warnings
6857 (beginning-of-buffer arg))
6858 ;; Set point accordingly.
6859 (recenter '(t)))
6860 (select-window orig-window))))
6861
6862 (defun end-of-buffer-other-window (arg)
6863 "Move point to the end of the buffer in the other window.
6864 Leave mark at previous position.
6865 With arg N, put point N/10 of the way from the true end."
6866 (interactive "P")
6867 ;; See beginning-of-buffer-other-window for comments.
6868 (let ((orig-window (selected-window))
6869 (window (other-window-for-scrolling)))
6870 (unwind-protect
6871 (progn
6872 (select-window window)
6873 (with-no-warnings
6874 (end-of-buffer arg))
6875 (recenter '(t)))
6876 (select-window orig-window))))
6877 \f
6878 (defvar mouse-autoselect-window-timer nil
6879 "Timer used by delayed window autoselection.")
6880
6881 (defvar mouse-autoselect-window-position nil
6882 "Last mouse position recorded by delayed window autoselection.")
6883
6884 (defvar mouse-autoselect-window-window nil
6885 "Last window recorded by delayed window autoselection.")
6886
6887 (defvar mouse-autoselect-window-state nil
6888 "When non-nil, special state of delayed window autoselection.
6889 Possible values are `suspend' \(suspend autoselection after a menu or
6890 scrollbar interaction\) and `select' \(the next invocation of
6891 'handle-select-window' shall select the window immediately\).")
6892
6893 (defun mouse-autoselect-window-cancel (&optional force)
6894 "Cancel delayed window autoselection.
6895 Optional argument FORCE means cancel unconditionally."
6896 (unless (and (not force)
6897 ;; Don't cancel for select-window or select-frame events
6898 ;; or when the user drags a scroll bar.
6899 (or (memq this-command
6900 '(handle-select-window handle-switch-frame))
6901 (and (eq this-command 'scroll-bar-toolkit-scroll)
6902 (memq (nth 4 (event-end last-input-event))
6903 '(handle end-scroll)))))
6904 (setq mouse-autoselect-window-state nil)
6905 (when (timerp mouse-autoselect-window-timer)
6906 (cancel-timer mouse-autoselect-window-timer))
6907 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
6908
6909 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
6910 "Start delayed window autoselection.
6911 MOUSE-POSITION is the last position where the mouse was seen as returned
6912 by `mouse-position'. Optional argument WINDOW non-nil denotes the
6913 window where the mouse was seen. Optional argument SUSPEND non-nil
6914 means suspend autoselection."
6915 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
6916 (setq mouse-autoselect-window-position mouse-position)
6917 (when window (setq mouse-autoselect-window-window window))
6918 (setq mouse-autoselect-window-state (when suspend 'suspend))
6919 ;; Install timer which runs `mouse-autoselect-window-select' after
6920 ;; `mouse-autoselect-window' seconds.
6921 (setq mouse-autoselect-window-timer
6922 (run-at-time
6923 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
6924
6925 (defun mouse-autoselect-window-select ()
6926 "Select window with delayed window autoselection.
6927 If the mouse position has stabilized in a non-selected window, select
6928 that window. The minibuffer window is selected only if the minibuffer is
6929 active. This function is run by `mouse-autoselect-window-timer'."
6930 (ignore-errors
6931 (let* ((mouse-position (mouse-position))
6932 (window
6933 (ignore-errors
6934 (window-at (cadr mouse-position) (cddr mouse-position)
6935 (car mouse-position)))))
6936 (cond
6937 ((or (menu-or-popup-active-p)
6938 (and window
6939 (not (coordinates-in-window-p (cdr mouse-position) window))))
6940 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
6941 ;; of WINDOW, temporarily suspend delayed autoselection.
6942 (mouse-autoselect-window-start mouse-position nil t))
6943 ((eq mouse-autoselect-window-state 'suspend)
6944 ;; Delayed autoselection was temporarily suspended, reenable it.
6945 (mouse-autoselect-window-start mouse-position))
6946 ((and window (not (eq window (selected-window)))
6947 (or (not (numberp mouse-autoselect-window))
6948 (and (> mouse-autoselect-window 0)
6949 ;; If `mouse-autoselect-window' is positive, select
6950 ;; window if the window is the same as before.
6951 (eq window mouse-autoselect-window-window))
6952 ;; Otherwise select window if the mouse is at the same
6953 ;; position as before. Observe that the first test after
6954 ;; starting autoselection usually fails since the value of
6955 ;; `mouse-autoselect-window-position' recorded there is the
6956 ;; position where the mouse has entered the new window and
6957 ;; not necessarily where the mouse has stopped moving.
6958 (equal mouse-position mouse-autoselect-window-position))
6959 ;; The minibuffer is a candidate window if it's active.
6960 (or (not (window-minibuffer-p window))
6961 (eq window (active-minibuffer-window))))
6962 ;; Mouse position has stabilized in non-selected window: Cancel
6963 ;; delayed autoselection and try to select that window.
6964 (mouse-autoselect-window-cancel t)
6965 ;; Select window where mouse appears unless the selected window is the
6966 ;; minibuffer. Use `unread-command-events' in order to execute pre-
6967 ;; and post-command hooks and trigger idle timers. To avoid delaying
6968 ;; autoselection again, set `mouse-autoselect-window-state'."
6969 (unless (window-minibuffer-p (selected-window))
6970 (setq mouse-autoselect-window-state 'select)
6971 (setq unread-command-events
6972 (cons (list 'select-window (list window))
6973 unread-command-events))))
6974 ((or (and window (eq window (selected-window)))
6975 (not (numberp mouse-autoselect-window))
6976 (equal mouse-position mouse-autoselect-window-position))
6977 ;; Mouse position has either stabilized in the selected window or at
6978 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
6979 (mouse-autoselect-window-cancel t))
6980 (t
6981 ;; Mouse position has not stabilized yet, resume delayed
6982 ;; autoselection.
6983 (mouse-autoselect-window-start mouse-position window))))))
6984
6985 (defun handle-select-window (event)
6986 "Handle select-window events."
6987 (interactive "e")
6988 (let ((window (posn-window (event-start event))))
6989 (unless (or (not (window-live-p window))
6990 ;; Don't switch if we're currently in the minibuffer.
6991 ;; This tries to work around problems where the
6992 ;; minibuffer gets unselected unexpectedly, and where
6993 ;; you then have to move your mouse all the way down to
6994 ;; the minibuffer to select it.
6995 (window-minibuffer-p (selected-window))
6996 ;; Don't switch to minibuffer window unless it's active.
6997 (and (window-minibuffer-p window)
6998 (not (minibuffer-window-active-p window)))
6999 ;; Don't switch when autoselection shall be delayed.
7000 (and (numberp mouse-autoselect-window)
7001 (not (zerop mouse-autoselect-window))
7002 (not (eq mouse-autoselect-window-state 'select))
7003 (progn
7004 ;; Cancel any delayed autoselection.
7005 (mouse-autoselect-window-cancel t)
7006 ;; Start delayed autoselection from current mouse
7007 ;; position and window.
7008 (mouse-autoselect-window-start (mouse-position) window)
7009 ;; Executing a command cancels delayed autoselection.
7010 (add-hook
7011 'pre-command-hook 'mouse-autoselect-window-cancel))))
7012 (when mouse-autoselect-window
7013 ;; Reset state of delayed autoselection.
7014 (setq mouse-autoselect-window-state nil)
7015 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
7016 (run-hooks 'mouse-leave-buffer-hook))
7017 (select-window window))))
7018
7019 (defun truncated-partial-width-window-p (&optional window)
7020 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
7021 WINDOW defaults to the selected window.
7022 Return nil if WINDOW is not a partial-width window
7023 (regardless of the value of `truncate-lines').
7024 Otherwise, consult the value of `truncate-partial-width-windows'
7025 for the buffer shown in WINDOW."
7026 (unless window
7027 (setq window (selected-window)))
7028 (unless (window-full-width-p window)
7029 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
7030 (window-buffer window))))
7031 (if (integerp t-p-w-w)
7032 (< (window-width window) t-p-w-w)
7033 t-p-w-w))))
7034 \f
7035 (define-key ctl-x-map "0" 'delete-window)
7036 (define-key ctl-x-map "1" 'delete-other-windows)
7037 (define-key ctl-x-map "2" 'split-window-above-each-other)
7038 (define-key ctl-x-map "3" 'split-window-side-by-side)
7039 (define-key ctl-x-map "o" 'other-window)
7040 (define-key ctl-x-map "^" 'enlarge-window)
7041 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
7042 (define-key ctl-x-map "{" 'shrink-window-horizontally)
7043 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
7044 (define-key ctl-x-map "+" 'balance-windows)
7045 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
7046
7047 ;;; window.el ends here