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