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