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