Normalize SIDE argument of split-window (Bug#8916).
[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* ((side (cond
3018 ((not side) 'below)
3019 ((memq side '(below above right left)) side)
3020 (t 'right)))
3021 (horizontal (not (memq side '(nil below above))))
3022 (frame (window-frame window))
3023 (parent (window-parent window))
3024 (function (window-parameter window 'split-window))
3025 (window-side (window-parameter window 'window-side))
3026 ;; Rebind `window-nest' since in some cases we may have to
3027 ;; override its value.
3028 (window-nest window-nest)
3029 atom-root)
3030
3031 (window-check frame)
3032 (catch 'done
3033 (cond
3034 ;; Ignore window parameters if either `ignore-window-parameters'
3035 ;; is t or the `split-window' parameter equals t.
3036 ((or ignore-window-parameters (eq function t)))
3037 ((functionp function)
3038 ;; The `split-window' parameter specifies the function to call.
3039 ;; If that function is `ignore', do nothing.
3040 (throw 'done (funcall function window size side)))
3041 ;; If WINDOW is a subwindow of an atomic window, split the root
3042 ;; window of that atomic window instead.
3043 ((and (window-parameter window 'window-atom)
3044 (setq atom-root (window-atom-root window))
3045 (not (eq atom-root window)))
3046 (throw 'done (split-window atom-root size side))))
3047
3048 (when (and window-side
3049 (or (not parent)
3050 (not (window-parameter parent 'window-side))))
3051 ;; WINDOW is a side root window. To make sure that a new parent
3052 ;; window gets created set `window-nest' to t.
3053 (setq window-nest t))
3054
3055 (when (and window-splits size (> size 0))
3056 ;; If `window-splits' is non-nil and SIZE is a non-negative
3057 ;; integer, we cannot reasonably resize other windows. Rather
3058 ;; bind `window-nest' to t to make sure that subsequent window
3059 ;; deletions are handled correctly.
3060 (setq window-nest t))
3061
3062 (let* ((parent-size
3063 ;; `parent-size' is the size of WINDOW's parent, provided
3064 ;; it has one.
3065 (when parent (window-total-size parent horizontal)))
3066 ;; `resize' non-nil means we are supposed to resize other
3067 ;; windows in WINDOW's combination.
3068 (resize
3069 (and window-splits (not window-nest)
3070 ;; Resize makes sense in iso-combinations only.
3071 (window-iso-combined-p window horizontal)))
3072 ;; `old-size' is the current size of WINDOW.
3073 (old-size (window-total-size window horizontal))
3074 ;; `new-size' is the specified or calculated size of the
3075 ;; new window.
3076 (new-size
3077 (cond
3078 ((not size)
3079 (max (window-split-min-size horizontal)
3080 (if resize
3081 ;; When resizing try to give the new window the
3082 ;; average size of a window in its combination.
3083 (min (- parent-size
3084 (window-min-size parent horizontal))
3085 (/ parent-size
3086 (1+ (window-iso-combinations
3087 parent horizontal))))
3088 ;; Else try to give the new window half the size
3089 ;; of WINDOW (plus an eventual odd line).
3090 (+ (/ old-size 2) (% old-size 2)))))
3091 ((>= size 0)
3092 ;; SIZE non-negative specifies the new size of WINDOW.
3093
3094 ;; Note: Specifying a non-negative SIZE is practically
3095 ;; always done as workaround for making the new window
3096 ;; appear above or on the left of the new window (the
3097 ;; ispell window is a typical example of that). In all
3098 ;; these cases the SIDE argument should be set to 'above
3099 ;; or 'left in order to support the 'resize option.
3100 ;; Here we have to nest the windows instead, see above.
3101 (- old-size size))
3102 (t
3103 ;; SIZE negative specifies the size of the new window.
3104 (- size))))
3105 new-parent new-normal)
3106
3107 ;; Check SIZE.
3108 (cond
3109 ((not size)
3110 (cond
3111 (resize
3112 ;; SIZE unspecified, resizing.
3113 (when (and (not (window-sizable-p parent (- new-size) horizontal))
3114 ;; Try again with minimum split size.
3115 (setq new-size
3116 (max new-size (window-split-min-size horizontal)))
3117 (not (window-sizable-p parent (- new-size) horizontal)))
3118 (error "Window %s too small for splitting" parent)))
3119 ((> (+ new-size (window-min-size window horizontal)) old-size)
3120 ;; SIZE unspecified, no resizing.
3121 (error "Window %s too small for splitting" window))))
3122 ((and (>= size 0)
3123 (or (>= size old-size)
3124 (< new-size (if horizontal
3125 window-safe-min-width
3126 window-safe-min-width))))
3127 ;; SIZE specified as new size of old window. If the new size
3128 ;; is larger than the old size or the size of the new window
3129 ;; would be less than the safe minimum, signal an error.
3130 (error "Window %s too small for splitting" window))
3131 (resize
3132 ;; SIZE specified, resizing.
3133 (unless (window-sizable-p parent (- new-size) horizontal)
3134 ;; If we cannot resize the parent give up.
3135 (error "Window %s too small for splitting" parent)))
3136 ((or (< new-size
3137 (if horizontal window-safe-min-width window-safe-min-height))
3138 (< (- old-size new-size)
3139 (if horizontal window-safe-min-width window-safe-min-height)))
3140 ;; SIZE specification violates minimum size restrictions.
3141 (error "Window %s too small for splitting" window)))
3142
3143 (resize-window-reset frame horizontal)
3144
3145 (setq new-parent
3146 ;; Make new-parent non-nil if we need a new parent window;
3147 ;; either because we want to nest or because WINDOW is not
3148 ;; iso-combined.
3149 (or window-nest (not (window-iso-combined-p window horizontal))))
3150 (setq new-normal
3151 ;; Make new-normal the normal size of the new window.
3152 (cond
3153 (size (/ (float new-size) (if new-parent old-size parent-size)))
3154 (new-parent 0.5)
3155 (resize (/ 1.0 (1+ (window-iso-combinations parent horizontal))))
3156 (t (/ (window-normal-size window horizontal) 2.0))))
3157
3158 (if resize
3159 ;; Try to get space from OLD's siblings. We could go "up" and
3160 ;; try getting additional space from surrounding windows but
3161 ;; we won't be able to return space to those windows when we
3162 ;; delete the one we create here. Hence we do not go up.
3163 (progn
3164 (resize-subwindows parent (- new-size) horizontal)
3165 (let* ((normal (- 1.0 new-normal))
3166 (sub (window-child parent)))
3167 (while sub
3168 (set-window-new-normal
3169 sub (* (window-normal-size sub horizontal) normal))
3170 (setq sub (window-right sub)))))
3171 ;; Get entire space from WINDOW.
3172 (set-window-new-total window (- old-size new-size))
3173 (resize-this-window window (- new-size) horizontal)
3174 (set-window-new-normal
3175 window (- (if new-parent 1.0 (window-normal-size window horizontal))
3176 new-normal)))
3177
3178 (let* ((new (split-window-internal window new-size side new-normal)))
3179 ;; Inherit window-side parameters, if any.
3180 (when (and window-side new-parent)
3181 (set-window-parameter (window-parent new) 'window-side window-side)
3182 (set-window-parameter new 'window-side window-side))
3183
3184 (run-window-configuration-change-hook frame)
3185 (window-check frame)
3186 ;; Always return the new window.
3187 new)))))
3188
3189 ;; I think this should be the default; I think people will prefer it--rms.
3190 (defcustom split-window-keep-point t
3191 "If non-nil, \\[split-window-above-each-other] keeps the original point \
3192 in both children.
3193 This is often more convenient for editing.
3194 If nil, adjust point in each of the two windows to minimize redisplay.
3195 This is convenient on slow terminals, but point can move strangely.
3196
3197 This option applies only to `split-window-above-each-other' and
3198 functions that call it. `split-window' always keeps the original
3199 point in both children."
3200 :type 'boolean
3201 :group 'windows)
3202
3203 (defun split-window-above-each-other (&optional size)
3204 "Split selected window into two windows, one above the other.
3205 The upper window gets SIZE lines and the lower one gets the rest.
3206 SIZE negative means the lower window gets -SIZE lines and the
3207 upper one the rest. With no argument, split windows equally or
3208 close to it. Both windows display the same buffer, now current.
3209
3210 If the variable `split-window-keep-point' is non-nil, both new
3211 windows will get the same value of point as the selected window.
3212 This is often more convenient for editing. The upper window is
3213 the selected window.
3214
3215 Otherwise, we choose window starts so as to minimize the amount of
3216 redisplay; this is convenient on slow terminals. The new selected
3217 window is the one that the current value of point appears in. The
3218 value of point can change if the text around point is hidden by the
3219 new mode line.
3220
3221 Regardless of the value of `split-window-keep-point', the upper
3222 window is the original one and the return value is the new, lower
3223 window."
3224 (interactive "P")
3225 (let ((old-window (selected-window))
3226 (old-point (point))
3227 (size (and size (prefix-numeric-value size)))
3228 moved-by-window-height moved new-window bottom)
3229 (when (and size (< size 0) (< (- size) window-min-height))
3230 ;; `split-window' would not signal an error here.
3231 (error "Size of new window too small"))
3232 (setq new-window (split-window nil size))
3233 (unless split-window-keep-point
3234 (with-current-buffer (window-buffer)
3235 (goto-char (window-start))
3236 (setq moved (vertical-motion (window-height)))
3237 (set-window-start new-window (point))
3238 (when (> (point) (window-point new-window))
3239 (set-window-point new-window (point)))
3240 (when (= moved (window-height))
3241 (setq moved-by-window-height t)
3242 (vertical-motion -1))
3243 (setq bottom (point)))
3244 (and moved-by-window-height
3245 (<= bottom (point))
3246 (set-window-point old-window (1- bottom)))
3247 (and moved-by-window-height
3248 (<= (window-start new-window) old-point)
3249 (set-window-point new-window old-point)
3250 (select-window new-window)))
3251 ;; Always copy quit-restore parameter in interactive use.
3252 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3253 (when quit-restore
3254 (set-window-parameter new-window 'quit-restore quit-restore)))
3255 new-window))
3256
3257 (defalias 'split-window-vertically 'split-window-above-each-other)
3258
3259 (defun split-window-side-by-side (&optional size)
3260 "Split selected window into two windows side by side.
3261 The selected window becomes the left one and gets SIZE columns.
3262 SIZE negative means the right window gets -SIZE lines.
3263
3264 SIZE includes the width of the window's scroll bar; if there are
3265 no scroll bars, it includes the width of the divider column to
3266 the window's right, if any. SIZE omitted or nil means split
3267 window equally.
3268
3269 The selected window remains selected. Return the new window."
3270 (interactive "P")
3271 (let ((old-window (selected-window))
3272 (size (and size (prefix-numeric-value size)))
3273 new-window)
3274 (when (and size (< size 0) (< (- size) window-min-width))
3275 ;; `split-window' would not signal an error here.
3276 (error "Size of new window too small"))
3277 (setq new-window (split-window nil size t))
3278 ;; Always copy quit-restore parameter in interactive use.
3279 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3280 (when quit-restore
3281 (set-window-parameter new-window 'quit-restore quit-restore)))
3282 new-window))
3283
3284 (defalias 'split-window-horizontally 'split-window-side-by-side)
3285 \f
3286 ;;; Balancing windows.
3287
3288 ;; The following routine uses the recycled code from an old version of
3289 ;; `resize-subwindows'. It's not very pretty, but coding it the way the
3290 ;; new `resize-subwindows' code does would hardly make it any shorter or
3291 ;; more readable (FWIW we'd need three loops - one to calculate the
3292 ;; minimum sizes per window, one to enlarge or shrink windows until the
3293 ;; new parent-size matches, and one where we shrink the largest/enlarge
3294 ;; the smallest window).
3295 (defun balance-windows-2 (window horizontal)
3296 "Subroutine of `balance-windows-1'.
3297 WINDOW must be an iso-combination."
3298 (let* ((first (window-child window))
3299 (sub first)
3300 (number-of-children 0)
3301 (parent-size (window-new-total window))
3302 (total-sum parent-size)
3303 found failed size sub-total sub-delta sub-amount rest)
3304 (while sub
3305 (setq number-of-children (1+ number-of-children))
3306 (when (window-size-fixed-p sub horizontal)
3307 (setq total-sum
3308 (- total-sum (window-total-size sub horizontal)))
3309 (set-window-new-normal sub 'ignore))
3310 (setq sub (window-right sub)))
3311
3312 (setq failed t)
3313 (while (and failed (> number-of-children 0))
3314 (setq size (/ total-sum number-of-children))
3315 (setq failed nil)
3316 (setq sub first)
3317 (while (and sub (not failed))
3318 ;; Ignore subwindows that should be ignored or are stuck.
3319 (unless (resize-subwindows-skip-p sub)
3320 (setq found t)
3321 (setq sub-total (window-total-size sub horizontal))
3322 (setq sub-delta (- size sub-total))
3323 (setq sub-amount
3324 (window-sizable sub sub-delta horizontal))
3325 ;; Register the new total size for this subwindow.
3326 (set-window-new-total sub (+ sub-total sub-amount))
3327 (unless (= sub-amount sub-delta)
3328 (setq total-sum (- total-sum sub-total sub-amount))
3329 (setq number-of-children (1- number-of-children))
3330 ;; We failed and need a new round.
3331 (setq failed t)
3332 (set-window-new-normal sub 'skip)))
3333 (setq sub (window-right sub))))
3334
3335 (setq rest (% total-sum number-of-children))
3336 ;; Fix rounding by trying to enlarge non-stuck windows by one line
3337 ;; (column) until `rest' is zero.
3338 (setq sub first)
3339 (while (and sub (> rest 0))
3340 (unless (resize-subwindows-skip-p window)
3341 (set-window-new-total sub 1 t)
3342 (setq rest (1- rest)))
3343 (setq sub (window-right sub)))
3344
3345 ;; Fix rounding by trying to enlarge stuck windows by one line
3346 ;; (column) until `rest' equals zero.
3347 (setq sub first)
3348 (while (and sub (> rest 0))
3349 (unless (eq (window-new-normal sub) 'ignore)
3350 (set-window-new-total sub 1 t)
3351 (setq rest (1- rest)))
3352 (setq sub (window-right sub)))
3353
3354 (setq sub first)
3355 (while sub
3356 ;; Record new normal sizes.
3357 (set-window-new-normal
3358 sub (/ (if (eq (window-new-normal sub) 'ignore)
3359 (window-total-size sub horizontal)
3360 (window-new-total sub))
3361 (float parent-size)))
3362 ;; Recursively balance each subwindow's subwindows.
3363 (balance-windows-1 sub horizontal)
3364 (setq sub (window-right sub)))))
3365
3366 (defun balance-windows-1 (window &optional horizontal)
3367 "Subroutine of `balance-windows'."
3368 (if (window-child window)
3369 (let ((sub (window-child window)))
3370 (if (window-iso-combined-p sub horizontal)
3371 (balance-windows-2 window horizontal)
3372 (let ((size (window-new-total window)))
3373 (while sub
3374 (set-window-new-total sub size)
3375 (balance-windows-1 sub horizontal)
3376 (setq sub (window-right sub))))))))
3377
3378 (defun balance-windows (&optional window-or-frame)
3379 "Balance the sizes of subwindows of WINDOW-OR-FRAME.
3380 WINDOW-OR-FRAME is optional and defaults to the selected frame.
3381 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
3382 subwindows of that frame's root window. If WINDOW-OR-FRAME
3383 denots a window, balance the sizes of all subwindows of that
3384 window."
3385 (interactive)
3386 (let* ((window
3387 (cond
3388 ((or (not window-or-frame)
3389 (frame-live-p window-or-frame))
3390 (frame-root-window window-or-frame))
3391 ((or (window-live-p window-or-frame)
3392 (window-child window-or-frame))
3393 window-or-frame)
3394 (t
3395 (error "Not a window or frame %s" window-or-frame))))
3396 (frame (window-frame window)))
3397 ;; Balance vertically.
3398 (resize-window-reset (window-frame window))
3399 (balance-windows-1 window)
3400 (resize-window-apply frame)
3401 ;; Balance horizontally.
3402 (resize-window-reset (window-frame window) t)
3403 (balance-windows-1 window t)
3404 (resize-window-apply frame t)))
3405
3406 (defun window-fixed-size-p (&optional window direction)
3407 "Return t if WINDOW cannot be resized in DIRECTION.
3408 WINDOW defaults to the selected window. DIRECTION can be
3409 nil (i.e. any), `height' or `width'."
3410 (with-current-buffer (window-buffer window)
3411 (when (and (boundp 'window-size-fixed) window-size-fixed)
3412 (not (and direction
3413 (member (cons direction window-size-fixed)
3414 '((height . width) (width . height))))))))
3415
3416 ;;; A different solution to balance-windows.
3417 (defvar window-area-factor 1
3418 "Factor by which the window area should be over-estimated.
3419 This is used by `balance-windows-area'.
3420 Changing this globally has no effect.")
3421 (make-variable-buffer-local 'window-area-factor)
3422
3423 (defun balance-windows-area-adjust (window delta horizontal)
3424 "Wrapper around `resize-window' with error checking.
3425 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
3426 ;; `resize-window' may fail if delta is too large.
3427 (while (>= (abs delta) 1)
3428 (condition-case nil
3429 (progn
3430 (resize-window window delta horizontal)
3431 (setq delta 0))
3432 (error
3433 ;;(message "adjust: %s" (error-message-string err))
3434 (setq delta (/ delta 2))))))
3435
3436 (defun balance-windows-area ()
3437 "Make all visible windows the same area (approximately).
3438 See also `window-area-factor' to change the relative size of
3439 specific buffers."
3440 (interactive)
3441 (let* ((unchanged 0) (carry 0) (round 0)
3442 ;; Remove fixed-size windows.
3443 (wins (delq nil (mapcar (lambda (win)
3444 (if (not (window-fixed-size-p win)) win))
3445 (window-list nil 'nomini))))
3446 (changelog nil)
3447 next)
3448 ;; Resizing a window changes the size of surrounding windows in complex
3449 ;; ways, so it's difficult to balance them all. The introduction of
3450 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
3451 ;; very difficult to do. `balance-window' above takes an off-line
3452 ;; approach: get the whole window tree, then balance it, then try to
3453 ;; adjust the windows so they fit the result.
3454 ;; Here, instead, we take a "local optimization" approach, where we just
3455 ;; go through all the windows several times until nothing needs to be
3456 ;; changed. The main problem with this approach is that it's difficult
3457 ;; to make sure it terminates, so we use some heuristic to try and break
3458 ;; off infinite loops.
3459 ;; After a round without any change, we allow a second, to give a chance
3460 ;; to the carry to propagate a minor imbalance from the end back to
3461 ;; the beginning.
3462 (while (< unchanged 2)
3463 ;; (message "New round")
3464 (setq unchanged (1+ unchanged) round (1+ round))
3465 (dolist (win wins)
3466 (setq next win)
3467 (while (progn (setq next (next-window next))
3468 (window-fixed-size-p next)))
3469 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
3470 (let* ((horiz
3471 (< (car (window-edges win)) (car (window-edges next))))
3472 (areadiff (/ (- (* (window-height next) (window-width next)
3473 (buffer-local-value 'window-area-factor
3474 (window-buffer next)))
3475 (* (window-height win) (window-width win)
3476 (buffer-local-value 'window-area-factor
3477 (window-buffer win))))
3478 (max (buffer-local-value 'window-area-factor
3479 (window-buffer win))
3480 (buffer-local-value 'window-area-factor
3481 (window-buffer next)))))
3482 (edgesize (if horiz
3483 (+ (window-height win) (window-height next))
3484 (+ (window-width win) (window-width next))))
3485 (diff (/ areadiff edgesize)))
3486 (when (zerop diff)
3487 ;; Maybe diff is actually closer to 1 than to 0.
3488 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
3489 (when (and (zerop diff) (not (zerop areadiff)))
3490 (setq diff (/ (+ areadiff carry) edgesize))
3491 ;; Change things smoothly.
3492 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
3493 (if (zerop diff)
3494 ;; Make sure negligible differences don't accumulate to
3495 ;; become significant.
3496 (setq carry (+ carry areadiff))
3497 ;; This used `adjust-window-trailing-edge' before and uses
3498 ;; `resize-window' now. Error wrapping is still needed.
3499 (balance-windows-area-adjust win diff horiz)
3500 ;; (sit-for 0.5)
3501 (let ((change (cons win (window-edges win))))
3502 ;; If the same change has been seen already for this window,
3503 ;; we're most likely in an endless loop, so don't count it as
3504 ;; a change.
3505 (unless (member change changelog)
3506 (push change changelog)
3507 (setq unchanged 0 carry 0)))))))
3508 ;; We've now basically balanced all the windows.
3509 ;; But there may be some minor off-by-one imbalance left over,
3510 ;; so let's do some fine tuning.
3511 ;; (bw-finetune wins)
3512 ;; (message "Done in %d rounds" round)
3513 ))
3514
3515 ;;; Window states, how to get them and how to put them in a window.
3516 (defsubst window-list-no-nils (&rest args)
3517 "Like LIST but do not add nil elements of ARGS."
3518 (delq nil (apply 'list args)))
3519
3520 (defvar window-state-ignored-parameters '(quit-restore)
3521 "List of window parameters ignored by `window-state-get'.")
3522
3523 (defun window-state-get-1 (window &optional markers)
3524 "Helper function for `window-state-get'."
3525 (let* ((type
3526 (cond
3527 ((window-vchild window) 'vc)
3528 ((window-hchild window) 'hc)
3529 (t 'leaf)))
3530 (buffer (window-buffer window))
3531 (selected (eq window (selected-window)))
3532 (head
3533 (window-list-no-nils
3534 type
3535 (unless (window-next window) (cons 'last t))
3536 (cons 'clone-number (window-clone-number window))
3537 (cons 'total-height (window-total-size window))
3538 (cons 'total-width (window-total-size window t))
3539 (cons 'normal-height (window-normal-size window))
3540 (cons 'normal-width (window-normal-size window t))
3541 (cons 'splits (window-splits window))
3542 (cons 'nest (window-nest window))
3543 (let (list)
3544 (dolist (parameter (window-parameters window))
3545 (unless (memq (car parameter)
3546 window-state-ignored-parameters)
3547 (setq list (cons parameter list))))
3548 (when list
3549 (cons 'parameters list)))
3550 (when buffer
3551 ;; All buffer related things go in here - make the buffer
3552 ;; current when retrieving `point' and `mark'.
3553 (with-current-buffer (window-buffer window)
3554 (let ((point (if selected (point) (window-point window)))
3555 (start (window-start window))
3556 (mark (mark)))
3557 (window-list-no-nils
3558 'buffer (buffer-name buffer)
3559 (cons 'selected selected)
3560 (when window-size-fixed (cons 'size-fixed window-size-fixed))
3561 (cons 'hscroll (window-hscroll window))
3562 (cons 'fringes (window-fringes window))
3563 (cons 'margins (window-margins window))
3564 (cons 'scroll-bars (window-scroll-bars window))
3565 (cons 'vscroll (window-vscroll window))
3566 (cons 'dedicated (window-dedicated-p window))
3567 (cons 'point (if markers (copy-marker point) point))
3568 (cons 'start (if markers (copy-marker start) start))
3569 (when mark
3570 (cons 'mark (if markers (copy-marker mark) mark)))))))))
3571 (tail
3572 (when (memq type '(vc hc))
3573 (let (list)
3574 (setq window (window-child window))
3575 (while window
3576 (setq list (cons (window-state-get-1 window markers) list))
3577 (setq window (window-right window)))
3578 (nreverse list)))))
3579 (append head tail)))
3580
3581 (defun window-state-get (&optional window markers)
3582 "Return state of WINDOW as a Lisp object.
3583 WINDOW can be any window and defaults to the root window of the
3584 selected frame.
3585
3586 Optional argument MARKERS non-nil means use markers for sampling
3587 positions like `window-point' or `window-start'. MARKERS should
3588 be non-nil only if the value is used for putting the state back
3589 in the same session (note that markers slow down processing).
3590
3591 The return value can be used as argument for `window-state-put'
3592 to put the state recorded here into an arbitrary window. The
3593 value can be also stored on disk and read back in a new session."
3594 (setq window
3595 (if window
3596 (if (window-any-p window)
3597 window
3598 (error "%s is not a live or internal window" window))
3599 (frame-root-window)))
3600 ;; The return value is a cons whose car specifies some constraints on
3601 ;; the size of WINDOW. The cdr lists the states of the subwindows of
3602 ;; WINDOW.
3603 (cons
3604 ;; Frame related things would go into a function, say `frame-state',
3605 ;; calling `window-state-get' to insert the frame's root window.
3606 (window-list-no-nils
3607 (cons 'min-height (window-min-size window))
3608 (cons 'min-width (window-min-size window t))
3609 (cons 'min-height-ignore (window-min-size window nil t))
3610 (cons 'min-width-ignore (window-min-size window t t))
3611 (cons 'min-height-safe (window-min-size window nil 'safe))
3612 (cons 'min-width-safe (window-min-size window t 'safe))
3613 ;; These are probably not needed.
3614 (when (window-size-fixed-p window) (cons 'fixed-height t))
3615 (when (window-size-fixed-p window t) (cons 'fixed-width t)))
3616 (window-state-get-1 window markers)))
3617
3618 (defvar window-state-put-list nil
3619 "Helper variable for `window-state-put'.")
3620
3621 (defun window-state-put-1 (state &optional window ignore totals)
3622 "Helper function for `window-state-put'."
3623 (let ((type (car state)))
3624 (setq state (cdr state))
3625 (cond
3626 ((eq type 'leaf)
3627 ;; For a leaf window just add unprocessed entries to
3628 ;; `window-state-put-list'.
3629 (setq window-state-put-list
3630 (cons (cons window state) window-state-put-list)))
3631 ((memq type '(vc hc))
3632 (let* ((horizontal (eq type 'hc))
3633 (total (window-total-size window horizontal))
3634 (first t)
3635 size new)
3636 (dolist (item state)
3637 ;; Find the next child window. WINDOW always points to the
3638 ;; real window that we want to fill with what we find here.
3639 (when (memq (car item) '(leaf vc hc))
3640 (if (assq 'last item)
3641 ;; The last child window. Below `window-state-put-1'
3642 ;; will put into it whatever ITEM has in store.
3643 (setq new nil)
3644 ;; Not the last child window, prepare for splitting
3645 ;; WINDOW. SIZE is the new (and final) size of the old
3646 ;; window.
3647 (setq size
3648 (if totals
3649 ;; Use total size.
3650 (cdr (assq (if horizontal 'total-width 'total-height) item))
3651 ;; Use normalized size and round.
3652 (round (* total
3653 (cdr (assq
3654 (if horizontal 'normal-width 'normal-height)
3655 item))))))
3656
3657 ;; Use safe sizes, we try to resize later.
3658 (setq size (max size (if horizontal
3659 window-safe-min-height
3660 window-safe-min-width)))
3661
3662 (if (window-sizable-p window (- size) horizontal 'safe)
3663 (let* ((window-nest (assq 'nest item)))
3664 ;; We must inherit the nesting, otherwise we might mess
3665 ;; up handling of atomic and side window.
3666 (setq new (split-window window size horizontal)))
3667 ;; Give up if we can't resize window down to safe sizes.
3668 (error "Cannot resize window %s" window))
3669
3670 (when first
3671 (setq first nil)
3672 ;; When creating the first child window add for parent
3673 ;; unprocessed entries to `window-state-put-list'.
3674 (setq window-state-put-list
3675 (cons (cons (window-parent window) state)
3676 window-state-put-list))))
3677
3678 ;; Now process the current window (either the one we've just
3679 ;; split or the last child of its parent).
3680 (window-state-put-1 item window ignore totals)
3681 ;; Continue with the last window split off.
3682 (setq window new))))))))
3683
3684 (defun window-state-put-2 (ignore)
3685 "Helper function for `window-state-put'."
3686 (dolist (item window-state-put-list)
3687 (let ((window (car item))
3688 (clone-number (cdr (assq 'clone-number item)))
3689 (splits (cdr (assq 'splits item)))
3690 (nest (cdr (assq 'nest item)))
3691 (parameters (cdr (assq 'parameters item)))
3692 (state (cdr (assq 'buffer item))))
3693 ;; Put in clone-number.
3694 (when clone-number (set-window-clone-number window clone-number))
3695 (when splits (set-window-splits window splits))
3696 (when nest (set-window-nest window nest))
3697 ;; Process parameters.
3698 (when parameters
3699 (dolist (parameter parameters)
3700 (set-window-parameter window (car parameter) (cdr parameter))))
3701 ;; Process buffer related state.
3702 (when state
3703 ;; We don't want to raise an error here so we create a buffer if
3704 ;; there's none.
3705 (set-window-buffer window (get-buffer-create (car state)))
3706 (with-current-buffer (window-buffer window)
3707 (set-window-hscroll window (cdr (assq 'hscroll state)))
3708 (apply 'set-window-fringes
3709 (cons window (cdr (assq 'fringes state))))
3710 (let ((margins (cdr (assq 'margins state))))
3711 (set-window-margins window (car margins) (cdr margins)))
3712 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
3713 (set-window-scroll-bars
3714 window (car scroll-bars) (nth 2 scroll-bars) (nth 3 scroll-bars)))
3715 (set-window-vscroll window (cdr (assq 'vscroll state)))
3716 ;; Adjust vertically.
3717 (if (memq window-size-fixed '(t height))
3718 ;; A fixed height window, try to restore the original size.
3719 (let ((delta (- (cdr (assq 'total-height item))
3720 (window-total-height window)))
3721 window-size-fixed)
3722 (when (window-resizable-p window delta)
3723 (resize-window window delta)))
3724 ;; Else check whether the window is not high enough.
3725 (let* ((min-size (window-min-size window nil ignore))
3726 (delta (- min-size (window-total-size window))))
3727 (when (and (> delta 0)
3728 (window-resizable-p window delta nil ignore))
3729 (resize-window window delta nil ignore))))
3730 ;; Adjust horizontally.
3731 (if (memq window-size-fixed '(t width))
3732 ;; A fixed width window, try to restore the original size.
3733 (let ((delta (- (cdr (assq 'total-width item))
3734 (window-total-width window)))
3735 window-size-fixed)
3736 (when (window-resizable-p window delta)
3737 (resize-window window delta)))
3738 ;; Else check whether the window is not wide enough.
3739 (let* ((min-size (window-min-size window t ignore))
3740 (delta (- min-size (window-total-size window t))))
3741 (when (and (> delta 0)
3742 (window-resizable-p window delta t ignore))
3743 (resize-window window delta t ignore))))
3744 ;; Set dedicated status.
3745 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
3746 ;; Install positions (maybe we should do this after all windows
3747 ;; have been created and sized).
3748 (ignore-errors
3749 (set-window-start window (cdr (assq 'start state)))
3750 (set-window-point window (cdr (assq 'point state)))
3751 ;; I'm not sure whether we should set the mark here, but maybe
3752 ;; it can be used.
3753 (let ((mark (cdr (assq 'mark state))))
3754 (when mark (set-mark mark))))
3755 ;; Select window if it's the selected one.
3756 (when (cdr (assq 'selected state))
3757 (select-window window)))))))
3758
3759 (defun window-state-put (state &optional window ignore)
3760 "Put window state STATE into WINDOW.
3761 STATE should be the state of a window returned by an earlier
3762 invocation of `window-state-get'. Optional argument WINDOW must
3763 specify a live window and defaults to the selected one.
3764
3765 Optional argument IGNORE non-nil means ignore minimum window
3766 sizes and fixed size restrictions. IGNORE equal `safe' means
3767 subwindows can get as small as `window-safe-min-height' and
3768 `window-safe-min-width'."
3769 (setq window (normalize-live-window window))
3770 (let* ((frame (window-frame window))
3771 (head (car state))
3772 ;; We check here (1) whether the total sizes of root window of
3773 ;; STATE and that of WINDOW are equal so we can avoid
3774 ;; calculating new sizes, and (2) if we do have to resize
3775 ;; whether we can do so without violating size restrictions.
3776 (totals
3777 (and (= (window-total-size window)
3778 (cdr (assq 'total-height state)))
3779 (= (window-total-size window t)
3780 (cdr (assq 'total-width state)))))
3781 (min-height (cdr (assq 'min-height head)))
3782 (min-width (cdr (assq 'min-width head)))
3783 window-splits selected)
3784 (if (and (not totals)
3785 (or (> min-height (window-total-size window))
3786 (> min-width (window-total-size window t)))
3787 (or (not ignore)
3788 (and (setq min-height
3789 (cdr (assq 'min-height-ignore head)))
3790 (setq min-width
3791 (cdr (assq 'min-width-ignore head)))
3792 (or (> min-height (window-total-size window))
3793 (> min-width (window-total-size window t)))
3794 (or (not (eq ignore 'safe))
3795 (and (setq min-height
3796 (cdr (assq 'min-height-safe head)))
3797 (setq min-width
3798 (cdr (assq 'min-width-safe head)))
3799 (or (> min-height
3800 (window-total-size window))
3801 (> min-width
3802 (window-total-size window t))))))))
3803 ;; The check above might not catch all errors due to rounding
3804 ;; issues - so IGNORE equal 'safe might not always produce the
3805 ;; minimum possible state. But such configurations hardly make
3806 ;; sense anyway.
3807 (error "Window %s too small to accomodate state" window)
3808 (setq state (cdr state))
3809 (setq window-state-put-list nil)
3810 ;; Work on the windows of a temporary buffer to make sure that
3811 ;; splitting proceeds regardless of any buffer local values of
3812 ;; `window-size-fixed'. Release that buffer after the buffers of
3813 ;; all live windows have been set by `window-state-put-2'.
3814 (with-temp-buffer
3815 (set-window-buffer window (current-buffer))
3816 (window-state-put-1 state window nil totals)
3817 (window-state-put-2 ignore))
3818 (window-check frame))))
3819 \f
3820 ;;; Displaying buffers.
3821 (defconst display-buffer-default-specifiers
3822 '((reuse-window nil same visible)
3823 (pop-up-window (largest . nil) (lru . nil))
3824 (pop-up-window-min-height . 40)
3825 (pop-up-window-min-width . 80)
3826 (reuse-window other nil nil)
3827 (reuse-window nil other visible)
3828 (reuse-window nil nil t)
3829 (reuse-window-even-sizes . t))
3830 "Buffer display default specifiers.
3831 The value specified here is used when no other specifiers have
3832 been specified by the user or the application. Consult the
3833 documentation of `display-buffer-alist' for a description of
3834 buffer display specifiers.")
3835
3836 (defconst display-buffer-macro-specifiers
3837 '((same-window
3838 ;; Use the same window.
3839 (reuse-window same nil nil))
3840 (same-frame
3841 ;; Avoid other frames.
3842 (reuse-window nil same nil)
3843 (pop-up-window (largest . nil) (lru . nil))
3844 (reuse-window nil other nil))
3845 ;; (other-window
3846 ;; ;; Avoid selected window.
3847 ;; (reuse-window other same visible)
3848 ;; (pop-up-window (largest . nil) (lru . nil))
3849 ;; (reuse-window other other visible))
3850 (same-frame-other-window
3851 ;; Avoid other frames and selected window.
3852 (reuse-window other same nil)
3853 (pop-up-window (largest . nil) (lru . nil))
3854 (reuse-window other other nil))
3855 (other-frame
3856 ;; Avoid selected frame.
3857 (reuse-window nil same other)
3858 (pop-up-frame)
3859 (reuse-window nil other other))
3860 (default
3861 ;; The default specifiers.
3862 display-buffer-default-specifiers))
3863 "Buffer display macro specifiers.")
3864
3865 (defcustom display-buffer-alist
3866 '((((regexp . ".*"))
3867 ;; Reuse window showing same buffer on same frame.
3868 reuse-window (reuse-window nil same nil)
3869 ;; Pop up window.
3870 pop-up-window
3871 ;; Split largest or lru window.
3872 (pop-up-window (largest . nil) (lru . nil))
3873 (pop-up-window-min-height . 40) ; split-height-threshold / 2
3874 (pop-up-window-min-width . 80) ; split-width-threshold / 2
3875 ;; Reuse any but selected window on same frame.
3876 reuse-window (reuse-window other nil nil)
3877 (reuse-window-even-sizes . t)))
3878 "List associating buffer identifiers with display specifiers.
3879 The car of each element of this list is built from a set of cons
3880 cells called buffer identifiers. `display-buffer' shows a buffer
3881 according to the display specifiers in the element's cdr
3882 \(elements are true lists) if at least one of the identifiers
3883 matches the first or third argument of `display-buffer'. Such a
3884 match occurs in one of the following three cases:
3885
3886 - The car of the buffer identifier is the symbol `name' and its
3887 cdr is a string equalling the name of the buffer specified by
3888 the first \(BUFFER-OR-NAME) argument of `display-buffer'.
3889
3890 - The car is the symbol `regexp' and the cdr is a regular
3891 expression matching the name of the buffer specified by the
3892 first \(BUFFER-OR-NAME) argument of `display-buffer'.
3893
3894 - The car is the symbol `label' and the cdr is a symbol equalling
3895 the third \(LABEL) argument of `display-buffer'.
3896
3897 Display specifiers are either symbols, cons cells, or lists.
3898 Five specifiers have been reserved to indicate the basic method
3899 for displaying the buffer: `reuse-window', `pop-up-window',
3900 `pop-up-frame', `use-side-window', and `fun-with-args'.
3901
3902 A list whose car is the symbol `reuse-window' indicates that an
3903 existing window shall be reused for displaying the buffer. The
3904 second element of this list specifies the window to use and can
3905 be one of the following symbols:
3906
3907 nil stands for any window.
3908
3909 `same' stands for the selected window.
3910
3911 `other' stands for any but the selected window.
3912
3913 The third element specifies whether the buffer shown in a window
3914 that shall be reused must be the same buffer that shall be
3915 displayed or another buffer and can be one of the following:
3916
3917 nil means to not care about the window's buffer.
3918
3919 `same' means the window must show the buffer already.
3920
3921 `other' means the window must not show the buffer yet.
3922
3923 The fourth element specifies the set of frames to search for a
3924 suitable window and can be one of the following:
3925
3926 nil to reuse a window on the selected frame.
3927
3928 `visible' to search visible frames on the current terminal.
3929
3930 `other' stands for any visible frame but the selected one.
3931
3932 0 \(the number zero) to search visible and iconified frames on
3933 the current terminal.
3934
3935 t to search arbitrary frames including invisible ones.
3936
3937 If more than one window fits the constraints imposed by these
3938 elements, the least recently used candidate is chosen. A side
3939 window is never reused unless it already shows the buffer.
3940
3941 The following two specifiers are useful when the method equals
3942 `reuse-window':
3943
3944 - A cons cell whose car is the symbol `reuse-window-even-sizes'
3945 and whose cdr is non-nil means to even out the sizes of a
3946 reused window and the selected window provided they (1) appear
3947 adjacent to each other and (2) the selected window is larger
3948 than the window chosen. If the cdr is nil, this means that the
3949 window sizes are left alone.
3950
3951 - A cons cell whose car is the symbol `reuse-window-dedicated'
3952 and whose cdr is non-nil means that a window can be reused even
3953 if it's weakly dedicated to its buffer. If the cdr is t, a
3954 strongly dedicated window can be reused to show the buffer.
3955 Any other non-nil value means only weakly dedicated windows can
3956 be reused. If the cdr is nil, dedicated windows are not
3957 reused.
3958
3959 This specifier should be used in emergency cases only since
3960 windows are usually made dedicated in order to prevent
3961 `display-buffer' from reusing them.
3962
3963 A list whose car is the symbol `pop-up-window' and whose cdr is
3964 built from cons cells representing window/side tuples indicates
3965 that a new window shall be made for displaying the buffer on the
3966 selected frame.
3967
3968 Window/side tuples are cons cells. The car of such a tuple
3969 identifies the window that shall be split. Possible values are
3970 `largest', `lru', `selected', and `root' to split the largest,
3971 least recently used, selected or root window of the selected
3972 frame.
3973
3974 If the frame has side windows, these values do allow to split
3975 only the selected frame's main window or one of its subwindows.
3976 Setting the car to one of `left', `top', `right' and `bottom'
3977 splits the corresponding side window, provided such a window
3978 exists.
3979
3980 The cdr of each pair specifies on which side of the window to
3981 split the new window shall appear and can be one of `below',
3982 `right', `above', or `left' with the obvious meanings. If the
3983 cdr is nil, the window is split in a fashion suitable for its
3984 current dimensions. If the cdr specifies a function, that
3985 function is called with one argument - the window to split. The
3986 function is supposed to split that window and return the new
3987 window.
3988
3989 `display-buffer' scans these tuples until it can either produce a
3990 suitable window or fails. The default value for
3991 `display-buffer-alist' contains the tuples \(largest . nil) and
3992 \(lru . nil) in order to split the largest window first and, if
3993 that fails, the least recently used one.
3994
3995 The following specifiers are useful if the method specifier is
3996 `pop-up-window'.
3997
3998 - A cons cell whose car is the symbol `pop-up-window-min-height'
3999 specifiies the minimum height of the new window. If the cdr is
4000 an integer number, it specifies the minimum number of lines of
4001 the window. A floating point number gives the minimum fraction
4002 of the window height with respect to the height of the frame's
4003 root window. A new window is created only if it can be made at
4004 least as high as specified by the number. If the cdr is nil,
4005 this means to use the value of `window-min-height'.
4006
4007 - A cons cell whose car is the symbol `pop-up-window-min-width'
4008 specifies the minimum width of the new window. If the cdr is
4009 an integer number, it specifies the minimum number of columns
4010 of the window. A floating point number gives the minimum
4011 fraction of the window width with respect to the width of the
4012 frame's root window. A new window is created only if it can be
4013 made at least as wide as specified by the number. If the cdr
4014 is nil, this means to use the value of `window-min-width'.
4015
4016 - A cons cell whose car is `pop-up-window-set-height' with
4017 the following interpretations for the cdr:
4018
4019 - nil means leave the height of the new window alone.
4020
4021 - A number specifies the desired height of the new window. An
4022 integer number specifies the number of lines of the window.
4023 A floating point number gives the fraction of the window
4024 height with respect to the height of the frame's root window.
4025
4026 - If the cdr specifies a function, that function is called with
4027 one argument - the new window. The function is supposed to
4028 adjust the height of the window; its return value is ignored.
4029 Suitable functions are `shrink-window-if-larger-than-buffer'
4030 and `fit-window-to-buffer'.
4031
4032 - A cons cell whose car equals `pop-up-window-set-width' with
4033 the following interpretations for the cdr:
4034
4035 - nil means leave the width of the new window alone.
4036
4037 - A number specifies the desired width of the new window. An
4038 integer number specifies the number of columns of the window.
4039 A floating point number gives the fraction of the window
4040 width with respect to the width of the frame's root window.
4041
4042 - If the cdr specifies a function, that function is called with
4043 one argument - the new window. The function is supposed to
4044 adjust the width of the window; its return value is ignored.
4045
4046 Observe that specifying `pop-up-window-set-height' or
4047 `pop-up-window-set-width' may override restrictions given by
4048 the `pop-up-window-min-height' and `pop-up-window-min-width'
4049 specifiers.
4050
4051 - A cons cell whose car is `pop-up-window-split-unsplittable' and
4052 whose cdr is non-nil allows to make a new window on an
4053 unsplittable frame. If the cdr is nil, unsplittable frames are
4054 not split. This specifier should be used in special cases only
4055 since frames are usually made unsplittable in order to prevent
4056 `display-buffer' from splitting them.
4057
4058 A list whose car is the symbol `pop-up-frame' specifies that a
4059 new frame shall be made for displaying the buffer. The second
4060 element, if non-nil, allows popping up a new frame on graphic
4061 displays only.
4062
4063 The following specifiers are useful if the method specifier is
4064 `pop-up-frame'.
4065
4066 - A list whose car is the symbol `pop-up-frame-function' together
4067 with a valid function as cdr specifies the function for
4068 creating a new frame. If the cdr is nil, the default function
4069 `make-frame' is called. The function is called with the
4070 parameters and values provided by the specifier described next.
4071
4072 - A list whose car is the symbol `pop-up-frame-alist' followed by
4073 an arbitrary number of frame parameter/value tuples, each given
4074 as a cons cell, specifies the parameters passed to the pop-up
4075 frame function.
4076
4077 A list of three elements whose car is the symbol
4078 `use-side-window' specifies that the buffer shall be displayed in
4079 a side window of the selected frame. The second element denotes
4080 the side of the frame where the window appears or shall be made.
4081 The third element denotes the slot used by the window. If a side
4082 window with the specified slot exists already, that window is
4083 reused. If no such window exists it is created.
4084
4085 The following specifiers are useful in connection with the
4086 `use-side-window' method specifier: `reuse-window-dedicated',
4087 `pop-up-window-min-height', `pop-up-window-min-width',
4088 `pop-up-window-set-height' and `pop-up-window-set-width'.
4089
4090 A list whose car is the symbol `fun-with-args' specifies that the
4091 function specified in the second element of the list is
4092 responsible for displaying the buffer. `display-buffer' calls
4093 this function with the buffer as first argument and the remaining
4094 elements of the list as second argument.
4095
4096 The function should choose or create a window, display the buffer
4097 in it, and return the window. It is also responsible for giving
4098 the variable `display-buffer-window' and the `quit-restore'
4099 parameter of the window used a meaningful value.
4100
4101 Within the body of this function avoid calling `display-buffer'
4102 with the same buffer as argument since this may lead to endless
4103 recursion.
4104
4105 Instead of supplying basic method specifiers, it's sometimes more
4106 convenient to use macro specifiers. They provide some commonly
4107 used display methods but do not support the fine control provided
4108 by the basic method specifiers. Macro specifiers are symbols.
4109 The following macro specifiers are provided:
4110
4111 `same-window' to display the buffer in the selected window.
4112
4113 `same-frame' to display the buffer on the selected frame.
4114
4115 `other-window' to display the buffer in any window but the
4116 selected one.
4117
4118 `same-frame-other-window' as `other-window' but stay on the
4119 selected frame.
4120
4121 `other-frame' to display the buffer on another visible
4122 frame.
4123
4124 `default' to use the default value of `display-buffer-alist'.
4125
4126 One specifier is useful with any method specifier: A list whose
4127 car is the symbol `dedicate' and whose cdr is non-nil will
4128 dedicate the window to its buffer. The following values are
4129 supported:
4130
4131 - nil to not dedicate the window to the buffer.
4132
4133 - `weak' to weakly dedicate the window to the buffer.
4134
4135 - t to strongly dedicate the window to the buffer.
4136
4137 A cons cell whose car is `other-window-means-other-frame' and
4138 whose cdr is non-nil means that you want calls of
4139 `display-buffer' with the second argument t or the symbol
4140 `other-window' to display the buffer in another frame. This
4141 means, for example, that you prefer functions like
4142 `find-file-other-window' or `switch-to-buffer-other-window' to
4143 make a new frame instead of a new window on the selected frame.
4144
4145 Usually, applications are free to override the specifiers of
4146 `display-buffer-alist' by passing their own specifiers as second
4147 argument of `display-buffer'. For every `display-buffer-alist'
4148 entry you can, however, add a cons cell whose car is the symbol
4149 `override' and whose cdr is non-nil, to explicitly override any
4150 value supplied by the application.
4151
4152 Overriding specifiers supplied by the calling application is, in
4153 general, not advisable. It permits, for example, to change the
4154 semantics of a function like `display-buffer-other-window' by
4155 using the location specifiers `same-window' or `other-frame'."
4156 :risky t
4157 :type
4158 '(repeat
4159 :offset 9
4160 ;; Associations of buffer identifiers and display specifiers.
4161 (list
4162 :format "%v"
4163 ;; Buffer identifiers.
4164 (repeat
4165 :tag "Buffer identifiers"
4166 (choice
4167 :tag "Identifier"
4168 :format "%[%t%] %v" :size 15
4169 (cons
4170 :tag "Name"
4171 :format "%v"
4172 :help-echo "A buffer name."
4173 (const :format "" name)
4174 (string :format "Name: %v\n" :size 32))
4175 (cons
4176 :tag "Regexp"
4177 :format "%v"
4178 :help-echo "A regular expression matching buffer names."
4179 (const :format "" regexp)
4180 (string :format "Regexp: %v\n" :size 32))
4181 (cons
4182 :tag "Label"
4183 :format "%v"
4184 :help-echo "A symbol equalling the buffer display label."
4185 (const :format "" symbol)
4186 (symbol :format "Label: %v\n" :size 32))))
4187
4188 ;; Display specifiers.
4189 (repeat
4190 :offset 9
4191 :tag "Display specifiers"
4192 :inline t
4193 (list
4194 :inline t
4195 :format "%v"
4196 (choice
4197 :tag "Method"
4198 :value (reuse-window
4199 (reuse-window nil same nil)
4200 (reuse-window-even-sizes . t))
4201 :inline t
4202 :help-echo "Method for displaying the buffer."
4203 :format "%[Method%] %v" :size 15
4204
4205 ;; Reuse window specifiers.
4206 (list
4207 :tag "Reuse window"
4208 :value (reuse-window
4209 (reuse-window nil same nil)
4210 (reuse-window-even-sizes . t))
4211 :format "%t\n%v"
4212 :inline t
4213 ;; For customization purposes only.
4214 (const :format "" reuse-window)
4215 (set
4216 :format "%v"
4217 :inline t
4218 ;; The window to reuse.
4219 (list
4220 :format "%v\n"
4221 (const :format "" reuse-window)
4222 ;; The window type.
4223 (choice
4224 :tag "Window"
4225 :help-echo "Window to reuse."
4226 :value nil
4227 :format "%[Window%] %v" :size 15
4228 (const :tag "Any" :format "%t" nil)
4229 (const :tag "Selected only" :format "%t" same)
4230 (const :tag "Any but selected" :format "%t" other))
4231 ;; The window's buffer.
4232 (choice
4233 :tag "Buffer"
4234 :help-echo "Buffer shown by reused window."
4235 :value t
4236 :format " %[Buffer%] %v" :size 15
4237 (const :tag "Any buffer" :format "%t" nil)
4238 (const :tag "Same buffer" :format "%t" same)
4239 (const :tag "Other buffer" :format "%t" other))
4240 ;; The window's frame.
4241 (choice
4242 :help-echo "Frame to search for a window to reuse."
4243 :tag "Frame"
4244 :value nil
4245 :format " %[Frame%] %v" :size 15
4246 (const :tag "Selected frame only" :format "%t" nil)
4247 (const :tag "Visible frames" :format "%t" visible)
4248 (const :tag "Visible but unselected" :format "%t" other)
4249 (const :tag "Visible and iconified" :format "%t" 0)
4250 (const :tag "Any frame" :format "%t" t)))
4251 ;; Whether window sizes should be evened out.
4252 (cons
4253 :format "%v\n"
4254 :tag "Even window sizes"
4255 (const :format "" reuse-window-even-sizes)
4256 (choice
4257 :tag "Even window sizes"
4258 :help-echo "Whether to even sizes of selected and reused window."
4259 :value t
4260 :format "%[Even window sizes%] %v" :size 15
4261 (const :tag "Off" :format "%t" nil)
4262 (const :tag "Even window sizes" :format "%t" t)))
4263 ;; Whether to reuse a dedicated window
4264 (cons
4265 :format "%v\n"
4266 (const :format "" reuse-window-dedicated)
4267 (choice
4268 :tag "Reuse dedicated window" :value nil
4269 :help-echo "Reuse a window even if it is dedicated to its buffer."
4270 :format "%[Reuse dedicated window%] %v" :size 15
4271 (const :tag "Off" :format "%t" nil)
4272 (const :tag "Reuse weakly dedicated windows" :format "%t" weak)
4273 (const :tag "Reuse any dedicated window" :format "%t" t)))))
4274
4275 ;; Pop-up window specifiers.
4276 (list
4277 :tag "Pop-up window"
4278 :value (pop-up-window (pop-up-window (largest . nil) (lru . nil)))
4279 :format "%t\n%v"
4280 :inline t
4281 (const :format "" pop-up-window)
4282 (set
4283 :format "%v"
4284 :inline t
4285 ;; Pop-up window list.
4286 (list
4287 :format "%v"
4288 :value (pop-up-window (largest . nil) (lru . nil))
4289 (const :format "" pop-up-window)
4290 (repeat
4291 :tag "Window / Side tuples"
4292 :inline t
4293 (cons
4294 :format "%v\n"
4295 (choice
4296 :tag "Window"
4297 :help-echo "The window to split."
4298 :value largest
4299 :format "%[Window%] %v"
4300 (const :tag "Largest" :format "%t" largest)
4301 (const :tag "Least recently used" :format "%t" lru)
4302 (const :tag "Selected" :format "%t" selected)
4303 (const :tag "Root" :format "%t" root)
4304 (const :tag "Left" :format "%t" left)
4305 (const :tag "Top" :format "%t" top)
4306 (const :tag "Right" :format "%t" right)
4307 (const :tag "Bottom" :format "%t" bottom))
4308 (choice
4309 :tag "Side"
4310 :help-echo "The position of the new window with respect to the window to split."
4311 :value nil
4312 :format " %[Side%] %v"
4313 (const :tag "Dynamic" :format "%t" nil)
4314 (const :tag "Below" :format "%t" below)
4315 (const :tag "Right" :format "%t" right)
4316 (const :tag "Above" :format "%t" above)
4317 (const :tag "Left" :format "%t" left)
4318 (function
4319 :tag "Function" :format "%v" :size 25)))))
4320 ;; Minimum height of pop-up windows.
4321 (cons
4322 :format "%v\n"
4323 (const :format "" pop-up-window-min-height)
4324 (choice
4325 :help-echo "Minimum height of popped-up window."
4326 :format "%[Minimum height%] %v"
4327 (const :tag "Default" :format "%t" :value nil)
4328 (integer :tag "Number of lines" :value 12 :size 5)
4329 (float :tag "Fraction of frame height" :value .25 :size 5)))
4330 ;; Minimum width of pop-up windows.
4331 (cons
4332 :format "%v\n"
4333 (const :format "" pop-up-window-min-width)
4334 (choice
4335 :help-echo "Minimum width of popped-up window."
4336 :format "%[Minimum width%] %v"
4337 (const :tag "Default" :format "%t" :value nil)
4338 (integer :tag "Number of columns" :value 12 :size 5)
4339 (float :tag "Fraction of frame width" :value .25 :size 5)))
4340 ;; Desired height of pop-up windows.
4341 (cons
4342 :format "%v\n"
4343 (const :format "" pop-up-window-set-height)
4344 (choice
4345 :help-echo "Desired height of popped-up window."
4346 :format "%[Desired height%] %v"
4347 (const :tag "Default" :format "%t" :value nil)
4348 (integer :tag "Number of lines" :value 12 :size 5)
4349 (float :tag "Fraction of frame height" :value .25 :size 5)
4350 (function :tag "Function" :size 25)))
4351 ;; Desired width of pop-up windows.
4352 (cons
4353 :format "%v\n"
4354 (const :format "" pop-up-window-set-width)
4355 (choice
4356 :help-echo "Desired width of popped-up window."
4357 :format "%[Desired width%] %v"
4358 (const :tag "Default" :format "%t" :value nil)
4359 (integer :tag "Number of column" :value 12 :size 5)
4360 (float :tag "Fraction of frame width" :value .25 :size 5)
4361 (function :tag "Function" :size 25)))
4362 ;; Split unsplittable frames.
4363 (cons
4364 :format "%v\n"
4365 (const :format "" pop-up-window-unsplittable)
4366 (choice
4367 :help-echo "Allow popping up a window on \"unsplittable\" frames."
4368 :format "%[Split unsplittable frame%] %v"
4369 (const :tag "Off" :format "%t" nil)
4370 (const :tag "Allow" :format "%t" t)))))
4371
4372 ;; Pop-up frame specifiers.
4373 (list
4374 :tag "Pop-up frame"
4375 :value (pop-up-frame
4376 (pop-up-frame))
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-window."
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 'same-frame-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' means search for
5438 ;; a window showing the buffer on some visible or iconfied frame.
5439 ;; `last-nonminibuffer-frame' set and not the same frame means
5440 ;; search that frame.
5441 (let ((frames (or (and (or use-pop-up-frames
5442 display-buffer-reuse-frames
5443 (not (last-nonminibuffer-frame)))
5444 ;; All visible or iconfied frames.
5445 0)
5446 ;; Same frame.
5447 (last-nonminibuffer-frame))))
5448 (when frames
5449 (setq specifiers
5450 (cons (list 'reuse-window 'other 'same frames)
5451 specifiers))))
5452
5453 ;; `same-window-p' group.
5454 (when (same-window-p buffer-name)
5455 ;; Try to reuse the same (selected) window.
5456 (setq specifiers
5457 (cons (list 'reuse-window 'same nil nil)
5458 specifiers)))
5459
5460 ;; Prepend "reuse window on same frame if showing the buffer
5461 ;; already" specifier. It will be overriden by the application
5462 ;; supplied 'other-window specifier.
5463 (setq specifiers (cons (list 'reuse-window nil 'same nil)
5464 specifiers))
5465
5466 specifiers)))
5467
5468 (defun display-buffer-normalize-alist-1 (specifiers label)
5469 "Subroutine of `display-buffer-normalize-alist'.
5470 SPECIFIERS is a list of buffer display specfiers. LABEL is the
5471 same argument of `display-buffer'."
5472 (let (normalized entry)
5473 (cond
5474 ((not specifiers)
5475 nil)
5476 ((listp specifiers)
5477 ;; If SPECIFIERS is a list, we assume it is a list of specifiers.
5478 (dolist (specifier specifiers)
5479 (cond
5480 ((consp specifier)
5481 (setq normalized (cons specifier normalized)))
5482 ((symbolp specifier)
5483 ;; Might be a macro specifier, try to expand it (the cdr is a
5484 ;; list and we have to reverse it later, so do it one at a
5485 ;; time).
5486 (let ((entry (assq specifier display-buffer-macro-specifiers)))
5487 (dolist (item (cdr entry))
5488 (setq normalized (cons item normalized)))))))
5489 ;; Reverse list.
5490 (nreverse normalized))
5491 ((setq entry (assq specifiers display-buffer-macro-specifiers))
5492 ;; A macro specifier.
5493 (cdr entry)))))
5494
5495 (defun display-buffer-normalize-alist (buffer-name label)
5496 "Normalize `display-buffer-alist'.
5497 BUFFER-NAME must be the name of the buffer that shall be displayed.
5498 LABEL the corresponding argument of `display-buffer'."
5499 (let (list-1 list-2)
5500 (dolist (entry display-buffer-alist)
5501 (when (and (listp entry)
5502 (catch 'match
5503 (dolist (id (car entry))
5504 (when (consp id)
5505 (let ((type (car id))
5506 (value (cdr id)))
5507 (when (or (and (eq type 'name) (stringp value)
5508 (equal value buffer-name))
5509 (and (eq type 'regexp) (stringp value)
5510 (string-match-p value buffer-name))
5511 (and (eq type 'label) (eq value label)))
5512 (throw 'match t)))))))
5513 (let* ((specifiers (cdr entry))
5514 (normalized
5515 (display-buffer-normalize-alist-1 specifiers label)))
5516 (if (assq 'override specifiers)
5517 (setq list-1
5518 (if list-1
5519 (append list-1 normalized)
5520 normalized))
5521 (setq list-2
5522 (if list-2
5523 (append list-2 normalized)
5524 normalized))))))
5525
5526 (cons list-1 list-2)))
5527
5528 (defvar display-buffer-normalize-options-inhibit nil
5529 "If non-nil, `display-buffer' doesn't process obsolete options.")
5530
5531 (defun display-buffer-normalize-specifiers (buffer-name specifiers label)
5532 "Return normalized specifiers for a buffer matching BUFFER-NAME or LABEL.
5533 BUFFER-NAME must be a string specifying a valid buffer name.
5534 SPECIFIERS and LABEL are the homonymous arguments of
5535 `display-buffer'.
5536
5537 The method for displaying the buffer specified by BUFFER-NAME or
5538 LABEL is established by appending the following four lists of
5539 specifiers:
5540
5541 - The specifiers in `display-buffer-alist' whose buffer
5542 identifier matches BUFFER-NAME or LABEL and whose 'override
5543 component is set.
5544
5545 - SPECIFIERS.
5546
5547 - The specifiers in `display-buffer-alist' whose buffer
5548 identifier matches BUFFER-NAME or LABEL and whose 'override
5549 component is not set.
5550
5551 - `display-buffer-default-specifiers'."
5552 (let* ((list (display-buffer-normalize-alist buffer-name label))
5553 (other-frame (assq 'other-window-means-other-frame
5554 (or (car list) (cdr list)))))
5555 (append
5556 ;; Overriding user specifiers.
5557 (car list)
5558 ;; Application specifiers.
5559 (display-buffer-normalize-argument
5560 buffer-name specifiers label other-frame)
5561 ;; Emacs 23 compatibility specifiers.
5562 (unless display-buffer-normalize-options-inhibit
5563 (display-buffer-normalize-options buffer-name))
5564 ;; Non-overriding user specifiers.
5565 (cdr list)
5566 ;; Default specifiers.
5567 display-buffer-default-specifiers)))
5568
5569 ;; Minibuffer-only frames should be documented better. They really
5570 ;; deserve a separate section in the manual. Also
5571 ;; `last-nonminibuffer-frame' is nowhere documented in the manual.
5572 (defun display-buffer-frame (&optional frame)
5573 "Return FRAME if it is live and not a minibuffer-only frame.
5574 Return the value of `last-nonminibuffer-frame' otherwise."
5575 (setq frame (normalize-live-frame frame))
5576 (if (and (frame-live-p frame)
5577 ;; A not very nice way to get that information.
5578 (not (window-minibuffer-p (frame-root-window frame))))
5579 frame
5580 (last-nonminibuffer-frame)))
5581
5582 (defun display-buffer (&optional buffer-or-name specifiers label)
5583 "Make the buffer specified by BUFFER-OR-NAME appear in some window.
5584 Optional argument BUFFER-OR-NAME may be a buffer, a string \(a
5585 buffer name), or nil. If BUFFER-OR-NAME is a string not naming
5586 an existent buffer, create a buffer with that name. If
5587 BUFFER-OR-NAME is nil or omitted, display the current buffer.
5588 Interactively, prompt for the buffer name using the minibuffer.
5589
5590 Return the window chosen to display the buffer or nil if no such
5591 window is found. Do not change the selected window unless the
5592 buffer is shown on a different frame than the selected one.
5593
5594 Optional argument SPECIFIERS must be a list of buffer display
5595 specifiers, see the documentation of `display-buffer-alist' for a
5596 description.
5597
5598 For convenience, SPECIFIERS may also consist of a single buffer
5599 display location specifier or t, where the latter means to
5600 display the buffer in any but the selected window. If SPECIFIERS
5601 is nil or omitted, this means to exclusively use the specifiers
5602 provided by `display-buffer-alist'. If the value of the latter
5603 is nil too, all specifiers are provided by the constant
5604 `display-buffer-default-specifiers'.
5605
5606 As a special case, the `reuse-window' specifier allows to specify
5607 as second element an arbitrary window, as third element an
5608 arbitrary buffer, and as fourth element an arbitrary frame. As
5609 first element of a window/side pair of the `pop-up-window'
5610 specifier you can specifiy an arbitrary window.
5611
5612 The optional third argument LABEL, if non-nil, must be a symbol
5613 specifiying the buffer display label. Applications should set
5614 this when the buffer shall be displayed in some special way but
5615 BUFFER-OR-NAME does not identify the buffer as special. Typical
5616 buffers that fit into this category are those whose names are
5617 derived from the name of the file they are visiting. A user can
5618 override SPECIFIERS by adding an entry to `display-buffer-alist'
5619 whose car contains LABEL and whose cdr specifies the preferred
5620 alternative display method.
5621
5622 The method to display the buffer is derived by combining the
5623 values of `display-buffer-alist' and SPECIFIERS. Highest
5624 priority is given to overriding elements of
5625 `display-buffer-alist'. Next come the elements specified by
5626 SPECIFIERS, followed by the non-overriding elements of
5627 `display-buffer-alist'.
5628
5629 The result must be a list of valid buffer display specifiers. If
5630 `display-buffer-function' is non-nil, call it with the buffer and
5631 this list as arguments."
5632 (interactive "BDisplay buffer:\nP")
5633 (let* ((buffer (normalize-buffer-to-display buffer-or-name))
5634 (buffer-name (buffer-name buffer))
5635 (specifiers
5636 ;; Normalize specifiers.
5637 (display-buffer-normalize-specifiers buffer-name specifiers label))
5638 ;; Don't use a minibuffer frame.
5639 (frame (display-buffer-frame))
5640 ;; `window' is the window we use for showing `buffer'.
5641 window specifier method)
5642 ;; Reset this.
5643 (setq display-buffer-window nil)
5644 (if display-buffer-function
5645 ;; Let `display-buffer-function' do the job.
5646 (funcall display-buffer-function buffer specifiers)
5647 ;; Retrieve the next location specifier while there a specifiers
5648 ;; left and we don't have a valid window.
5649 (while (and specifiers (not (window-live-p window)))
5650 (setq specifier (car specifiers))
5651 (setq specifiers (cdr specifiers))
5652 (setq method (car specifier))
5653 (setq window
5654 (cond
5655 ((eq method 'reuse-window)
5656 (display-buffer-reuse-window
5657 buffer (cdr specifier) specifiers))
5658 ((eq method 'pop-up-window)
5659 (display-buffer-pop-up-window
5660 buffer (cdr specifier) specifiers))
5661 ((eq method 'pop-up-frame)
5662 (display-buffer-pop-up-frame
5663 buffer (cdr specifier) specifiers))
5664 ((eq method 'use-side-window)
5665 (display-buffer-in-side-window
5666 buffer (nth 1 specifier) (nth 2 specifier) specifiers))
5667 ((eq method 'fun-with-args)
5668 (apply (cadr specifier) buffer (cddr specifier))))))
5669
5670 ;; If we don't have a window yet, try a fallback method. All
5671 ;; specifiers have been used up by now.
5672 (or (and (window-live-p window) window)
5673 ;; Try reusing a window showing BUFFER on any visible or
5674 ;; iconfied frame.
5675 (display-buffer-reuse-window buffer '(nil buffer 0))
5676 ;; Try reusing a window not showing BUFFER on any visible or
5677 ;; iconified frame.
5678 (display-buffer-reuse-window buffer '(nil other 0))
5679 ;; Try making a new frame.
5680 (display-buffer-pop-up-frame buffer)
5681 ;; Try using a weakly dedicated window.
5682 (display-buffer-reuse-window
5683 buffer '(nil nil t) '((reuse-window-dedicated . weak)))
5684 ;; Try using a strongly dedicated window.
5685 (display-buffer-reuse-window
5686 buffer '(nil nil t) '((reuse-window-dedicated . t)))))))
5687
5688 (defsubst display-buffer-same-window (&optional buffer-or-name label)
5689 "Display buffer specified by BUFFER-OR-NAME in the selected window.
5690 Another window will be used only if the buffer can't be shown in
5691 the selected window, usually because it is dedicated to another
5692 buffer. Optional argument BUFFER-OR-NAME and LABEL are as for
5693 `display-buffer'."
5694 (interactive "BDisplay buffer in same window:\nP")
5695 (display-buffer buffer-or-name 'same-window label))
5696
5697 (defsubst display-buffer-same-frame (&optional buffer-or-name label)
5698 "Display buffer specified by BUFFER-OR-NAME in a window on the same frame.
5699 Another frame will be used only if there is no other choice.
5700 Optional argument BUFFER-OR-NAME and LABEL are as for
5701 `display-buffer'."
5702 (interactive "BDisplay buffer on same frame:\nP")
5703 (display-buffer buffer-or-name 'same-frame label))
5704
5705 (defsubst display-buffer-other-window (&optional buffer-or-name label)
5706 "Display buffer specified by BUFFER-OR-NAME in another window.
5707 The selected window will be used only if there is no other
5708 choice. Windows on the selected frame are preferred to windows
5709 on other frames. Optional argument BUFFER-OR-NAME and LABEL are as
5710 for `display-buffer'."
5711 (interactive "BDisplay buffer in another window:\nP")
5712 (display-buffer buffer-or-name 'other-window label))
5713
5714 (defun display-buffer-same-frame-other-window (&optional buffer-or-name label)
5715 "Display buffer specified by BUFFER-OR-NAME in another window on the same frame.
5716 The selected window or another frame will be used only if there
5717 is no other choice. Optional argument BUFFER-OR-NAME and LABEL are
5718 as for `display-buffer'."
5719 (interactive "BDisplay buffer in another window on same frame:\nP")
5720 (display-buffer buffer-or-name 'same-frame-other-window label))
5721
5722 (defun display-buffer-other-frame (&optional buffer-or-name label)
5723 "Display buffer specified by BUFFER-OR-NAME on another frame.
5724 The selected frame will be used only if there is no other choice.
5725 Optional argument BUFFER-OR-NAME and LABEL are as for
5726 `display-buffer'.
5727
5728 If this command uses another frame, it will also select that frame."
5729 (interactive "BDisplay buffer in other frame: ")
5730 (display-buffer buffer-or-name 'other-frame label))
5731
5732 (defun pop-to-buffer (&optional buffer-or-name specifiers norecord label)
5733 "Display buffer specified by BUFFER-OR-NAME and select the window used.
5734 Optional argument BUFFER-OR-NAME may be a buffer, a string \(a
5735 buffer name), or nil. If BUFFER-OR-NAME is a string not naming
5736 an existent buffer, create a buffer with that name. If
5737 BUFFER-OR-NAME is nil or omitted, display the current buffer.
5738 Interactively, prompt for the buffer name using the minibuffer.
5739
5740 Optional second argument SPECIFIERS must be a list of buffer
5741 display specifiers, a single location specifier, `t' which means
5742 the latter means to display the buffer in any but the selected
5743 window, or nil which means to exclusively apply the specifiers
5744 customized by the user.
5745
5746 Optional argument NORECORD non-nil means do not put the buffer
5747 specified by BUFFER-OR-NAME at the front of the buffer list and
5748 do not make the window displaying it the most recently selected
5749 one.
5750
5751 The optional argument LABEL, if non-nil, is a symbol specifying the
5752 display purpose. Applications should set this when the buffer
5753 shall be displayed in a special way but BUFFER-OR-NAME does not
5754 identify the buffer as special. Buffers that typically fit into
5755 this category are those whose names have been derived from the
5756 name of the file they are visiting.
5757
5758 Return the buffer specified by BUFFER-OR-NAME or nil if
5759 displaying the buffer failed.
5760
5761 This uses the function `display-buffer' as a subroutine; see the
5762 documentations of `display-buffer' and `display-buffer-alist' for
5763 additional information."
5764 (interactive "BPop to buffer:\nP")
5765 (let ((buffer (normalize-buffer-to-display buffer-or-name))
5766 (old-window (selected-window))
5767 (old-frame (selected-frame))
5768 new-window new-frame)
5769 (set-buffer buffer)
5770 (setq new-window (display-buffer buffer specifiers label))
5771 (unless (eq new-window old-window)
5772 ;; `display-buffer' has chosen another window, select it.
5773 (select-window new-window norecord)
5774 (setq new-frame (window-frame new-window))
5775 (unless (eq new-frame old-frame)
5776 ;; `display-buffer' has chosen another frame, make sure it gets
5777 ;; input focus and is risen.
5778 (select-frame-set-input-focus new-frame)))
5779
5780 buffer))
5781
5782 (defsubst pop-to-buffer-same-window (&optional buffer-or-name norecord label)
5783 "Pop to buffer specified by BUFFER-OR-NAME in the selected window.
5784 Another window will be used only if the buffer can't be shown in
5785 the selected window, usually because it is dedicated to another
5786 buffer. Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are
5787 as for `pop-to-buffer'."
5788 (interactive "BPop to buffer in selected window:\nP")
5789 (pop-to-buffer buffer-or-name 'same-window norecord label))
5790
5791 (defsubst pop-to-buffer-same-frame (&optional buffer-or-name norecord label)
5792 "Pop to buffer specified by BUFFER-OR-NAME in a window on the selected frame.
5793 Another frame will be used only if there is no other choice.
5794 Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are as for
5795 `pop-to-buffer'."
5796 (interactive "BPop to buffer on same frame:\nP")
5797 (pop-to-buffer buffer-or-name 'same-frame norecord label))
5798
5799 (defsubst pop-to-buffer-other-window (&optional buffer-or-name norecord label)
5800 "Pop to buffer specified by BUFFER-OR-NAME in another window.
5801 The selected window will be used only if there is no other
5802 choice. Windows on the selected frame are preferred to windows
5803 on other frames. Optional arguments BUFFER-OR-NAME, NORECORD and
5804 LABEL are as for `pop-to-buffer'."
5805 (interactive "BPop to buffer in another window:\nP")
5806 (pop-to-buffer buffer-or-name 'other-window norecord))
5807
5808 (defsubst pop-to-buffer-same-frame-other-window (&optional buffer-or-name norecord label)
5809 "Pop to buffer specified by BUFFER-OR-NAME in another window on the selected frame.
5810 The selected window or another frame will be used only if there
5811 is no other choice. Optional arguments BUFFER-OR-NAME, NORECORD
5812 and LABEL are as for `pop-to-buffer'."
5813 (interactive "BPop to buffer in another window on same frame:\nP")
5814 (pop-to-buffer buffer-or-name 'same-frame-other-window norecord label))
5815
5816 (defsubst pop-to-buffer-other-frame (&optional buffer-or-name norecord label)
5817 "Pop to buffer specified by BUFFER-OR-NAME on another frame.
5818 The selected frame will be used only if there's no other choice.
5819 Optional arguments BUFFER-OR-NAME, NORECORD and LABEL are as for
5820 `pop-to-buffer'."
5821 (interactive "BPop to buffer on another frame:\nP")
5822 (pop-to-buffer buffer-or-name 'other-frame norecord label))
5823
5824 (defun read-buffer-to-switch (prompt)
5825 "Read the name of a buffer to switch to, prompting with PROMPT.
5826 Return the neame of the buffer as a string.
5827
5828 This function is intended for the `switch-to-buffer' family of
5829 commands since these need to omit the name of the current buffer
5830 from the list of completions and default values."
5831 (let ((rbts-completion-table (internal-complete-buffer-except)))
5832 (minibuffer-with-setup-hook
5833 (lambda ()
5834 (setq minibuffer-completion-table rbts-completion-table)
5835 ;; Since rbts-completion-table is built dynamically, we
5836 ;; can't just add it to the default value of
5837 ;; icomplete-with-completion-tables, so we add it
5838 ;; here manually.
5839 (if (and (boundp 'icomplete-with-completion-tables)
5840 (listp icomplete-with-completion-tables))
5841 (set (make-local-variable 'icomplete-with-completion-tables)
5842 (cons rbts-completion-table
5843 icomplete-with-completion-tables))))
5844 (read-buffer prompt (other-buffer (current-buffer))
5845 (confirm-nonexistent-file-or-buffer)))))
5846
5847 (defun normalize-buffer-to-switch-to (buffer-or-name)
5848 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
5849 If BUFFER-OR-NAME is nil, return the buffer returned by
5850 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
5851 exists, return that buffer. If no such buffer exists, create a
5852 buffer with the name BUFFER-OR-NAME and return that buffer."
5853 (if buffer-or-name
5854 (or (get-buffer buffer-or-name)
5855 (let ((buffer (get-buffer-create buffer-or-name)))
5856 (set-buffer-major-mode buffer)
5857 buffer))
5858 (other-buffer)))
5859
5860 (defun switch-to-buffer (buffer-or-name &optional norecord)
5861 "Switch to buffer BUFFER-OR-NAME in the selected window.
5862 If called interactively, prompt for the buffer name using the
5863 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5864 determines whether to request confirmation before creating a new
5865 buffer.
5866
5867 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
5868 nil. If BUFFER-OR-NAME is a string that does not identify an
5869 existing buffer, create a buffer with that name. If
5870 BUFFER-OR-NAME is nil, switch to the buffer returned by
5871 `other-buffer'.
5872
5873 Optional argument NORECORD non-nil means do not put the buffer
5874 specified by BUFFER-OR-NAME at the front of the buffer list and
5875 do not make the window displaying it the most recently selected
5876 one. Return the buffer switched to.
5877
5878 This function is intended for interactive use only. Lisp
5879 functions should call `pop-to-buffer-same-window' instead."
5880 (interactive
5881 (list (read-buffer-to-switch "Switch to buffer: ")))
5882 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5883 (if (and (or (window-minibuffer-p) (eq (window-dedicated-p) t))
5884 (not (eq buffer (window-buffer))))
5885 ;; Cannot switch to another buffer in a minibuffer or strongly
5886 ;; dedicated window that does not show the buffer already. Call
5887 ;; `pop-to-buffer' instead.
5888 (pop-to-buffer buffer 'same-window norecord)
5889 (unless (eq buffer (window-buffer))
5890 ;; I'm not sure why we should NOT call `set-window-buffer' here,
5891 ;; but let's keep things as they are (otherwise we could always
5892 ;; call `pop-to-buffer-same-window' here).
5893 (set-window-buffer nil buffer))
5894 (unless norecord
5895 (select-window (selected-window)))
5896 (set-buffer buffer))))
5897
5898 (defun switch-to-buffer-same-frame (buffer-or-name &optional norecord)
5899 "Switch to buffer BUFFER-OR-NAME in a window on the selected frame.
5900 Another frame will be used only if there is no other choice.
5901 Arguments BUFFER-OR-NAME and NORECORD have the same meaning as
5902 for `switch-to-buffer'.
5903
5904 This function is intended for interactive use only. Lisp
5905 functions should call `pop-to-buffer-same-frame' instead."
5906 (interactive
5907 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5908 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5909 (pop-to-buffer buffer 'same-frame norecord)))
5910
5911 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
5912 "Switch to buffer BUFFER-OR-NAME in another window.
5913 The selected window will be used only if there is no other
5914 choice. Windows on the selected frame are preferred to windows
5915 on other frames. Arguments BUFFER-OR-NAME and NORECORD have the
5916 same meaning as for `switch-to-buffer'.
5917
5918 This function is intended for interactive use only. Lisp
5919 functions should call `pop-to-buffer-other-window' instead."
5920 (interactive
5921 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5922 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5923 (pop-to-buffer buffer 'other-window norecord)))
5924
5925 (defun switch-to-buffer-other-window-same-frame (buffer-or-name &optional norecord)
5926 "Switch to buffer BUFFER-OR-NAME in another window on the selected frame.
5927 The selected window or another frame will be used only if there
5928 is no other choice. Arguments BUFFER-OR-NAME and NORECORD have
5929 the same meaning as for `switch-to-buffer'.
5930
5931 This function is intended for interactive use only. Lisp
5932 functions should call `pop-to-buffer-other-window-same-frame'
5933 instead."
5934 (interactive
5935 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5936 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5937 (pop-to-buffer buffer 'same-frame-other-window norecord)))
5938
5939 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
5940 "Switch to buffer BUFFER-OR-NAME on another frame.
5941 The same frame will be used only if there is no other choice.
5942 Arguments BUFFER-OR-NAME and NORECORD have the same meaning
5943 as for `switch-to-buffer'.
5944
5945 This function is intended for interactive use only. Lisp
5946 functions should call `pop-to-buffer-other-frame' instead."
5947 (interactive
5948 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
5949 (let ((buffer (normalize-buffer-to-switch-to buffer-or-name)))
5950 (pop-to-buffer buffer 'other-frame norecord)))
5951
5952 ;;; Obsolete definitions of `display-buffer' below.
5953 (defcustom same-window-buffer-names nil
5954 "List of names of buffers that should appear in the \"same\" window.
5955 `display-buffer' and `pop-to-buffer' show a buffer whose name is
5956 on this list in the selected rather than some other window.
5957
5958 An element of this list can be a cons cell instead of just a
5959 string. In that case, the cell's car must be a string specifying
5960 the buffer name. This is for compatibility with
5961 `special-display-buffer-names'; the cdr of the cons cell is
5962 ignored.
5963
5964 See also `same-window-regexps'."
5965 :type '(repeat (string :format "%v"))
5966 :group 'windows)
5967 (make-obsolete-variable
5968 'same-window-buffer-names
5969 "use 2nd arg of `display-buffer' instead." "24.1")
5970
5971 (defcustom same-window-regexps nil
5972 "List of regexps saying which buffers should appear in the \"same\" window.
5973 `display-buffer' and `pop-to-buffer' show a buffer whose name
5974 matches a regexp on this list in the selected rather than some
5975 other window.
5976
5977 An element of this list can be a cons cell instead of just a
5978 string. In that case, the cell's car must be a regexp matching
5979 the buffer name. This is for compatibility with
5980 `special-display-regexps'; the cdr of the cons cell is ignored.
5981
5982 See also `same-window-buffer-names'."
5983 :type '(repeat (regexp :format "%v"))
5984 :group 'windows)
5985 (make-obsolete-variable
5986 'same-window-regexps
5987 "use 2nd arg of `display-buffer' instead." "24.1")
5988
5989 (defun same-window-p (buffer-name)
5990 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5991 This function returns non-nil if `display-buffer' or
5992 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
5993 selected rather than \(as usual\) some other window. See
5994 `same-window-buffer-names' and `same-window-regexps'."
5995 (let ((buffer-names (with-no-warnings same-window-buffer-names))
5996 (regexps (with-no-warnings same-window-regexps)))
5997 (cond
5998 ((not (stringp buffer-name)))
5999 ;; The elements of `same-window-buffer-names' can be buffer
6000 ;; names or cons cells whose cars are buffer names.
6001 ((member buffer-name buffer-names))
6002 ((assoc buffer-name buffer-names))
6003 ((catch 'found
6004 (dolist (regexp regexps)
6005 ;; The elements of `same-window-regexps' can be regexps
6006 ;; or cons cells whose cars are regexps.
6007 (when (or (and (stringp regexp)
6008 (string-match regexp buffer-name))
6009 (and (consp regexp) (stringp (car regexp))
6010 (string-match-p (car regexp) buffer-name)))
6011 (throw 'found t))))))))
6012 (make-obsolete
6013 'same-window-p "pass argument to buffer display function instead." "24.1")
6014
6015 (defcustom special-display-frame-alist
6016 '((height . 14) (width . 80) (unsplittable . t))
6017 "Alist of parameters for special frames.
6018 Special frames are used for buffers whose names are listed in
6019 `special-display-buffer-names' and for buffers whose names match
6020 one of the regular expressions in `special-display-regexps'.
6021
6022 This variable can be set in your init file, like this:
6023
6024 (setq special-display-frame-alist '((width . 80) (height . 20)))
6025
6026 These supersede the values given in `default-frame-alist'."
6027 :type '(repeat (cons :format "%v"
6028 (symbol :tag "Parameter")
6029 (sexp :tag "Value")))
6030 :group 'frames)
6031 (make-obsolete-variable
6032 'special-display-frame-alist
6033 "use 2nd arg of `display-buffer' instead." "24.1")
6034
6035 (defun special-display-popup-frame (buffer &optional args)
6036 "Display BUFFER in a special frame and return the window chosen.
6037 If BUFFER is already displayed in a visible or iconified frame,
6038 raise that frame. Otherwise, display BUFFER in a way as
6039 specified by optional argument ARGS.
6040
6041 If ARGS is an alist, use it as a list of frame parameters. If
6042 these parameters contain \(same-window . t), display BUFFER in
6043 the selected window. If they contain \(same-frame . t), display
6044 BUFFER in a window on the selected frame.
6045
6046 If ARGS is a list whose car is a symbol, use (car ARGS) as a
6047 function to do the work. Pass it BUFFER as first argument,
6048 and (cdr ARGS) as second."
6049 (if (and args (symbolp (car args)))
6050 (apply (car args) buffer (cdr args))
6051 (let ((window (get-buffer-window buffer 0)))
6052 (or
6053 ;; If we have a window already, make it visible.
6054 (when window
6055 (let ((frame (window-frame window)))
6056 (make-frame-visible frame)
6057 (raise-frame frame)
6058 window))
6059 ;; Reuse the current window if the user requested it.
6060 (when (cdr (assq 'same-window args))
6061 (display-buffer-reuse-window
6062 buffer '(same nil nil) '((reuse-dedicated . weak))))
6063 ;; Stay on the same frame if requested.
6064 (when (or (cdr (assq 'same-frame args))
6065 (cdr (assq 'same-window args)))
6066 (or (display-buffer-pop-up-window
6067 buffer '((largest . nil) (lru . nil)))
6068 (display-buffer-reuse-window
6069 buffer '(nil nil nil))))
6070 ;; If no window yet, make one in a new frame.
6071 (let ((frame
6072 (with-current-buffer buffer
6073 (make-frame
6074 (append args (with-no-warnings
6075 special-display-frame-alist))))))
6076 (set-window-buffer (frame-selected-window frame) buffer)
6077 (set-window-dedicated-p (frame-selected-window frame) t)
6078 (frame-selected-window frame))))))
6079 (make-obsolete
6080 'special-display-popup-frame
6081 "use 2nd arg of `display-buffer' instead." "24.1")
6082
6083 (defcustom special-display-function 'special-display-popup-frame
6084 "Function to call for displaying special buffers.
6085 This function is called with two arguments - the buffer and,
6086 optionally, a list - and should return a window displaying that
6087 buffer. The default value usually makes a separate frame for the
6088 buffer using `special-display-frame-alist' to specify the frame
6089 parameters. See the definition of `special-display-popup-frame'
6090 for how to specify such a function.
6091
6092 A buffer is special when its name is either listed in
6093 `special-display-buffer-names' or matches a regexp in
6094 `special-display-regexps'."
6095 :type 'function
6096 :group 'windows
6097 :group 'frames)
6098 (make-obsolete-variable
6099 'special-display-function
6100 "use 2nd arg of `display-buffer' instead." "24.1")
6101
6102 (defcustom special-display-buffer-names nil
6103 "List of names of buffers that should be displayed specially.
6104 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
6105 its name is in this list, displays the buffer in a way specified
6106 by `special-display-function'. `special-display-popup-frame'
6107 \(the default for `special-display-function') usually displays
6108 the buffer in a separate frame made with the parameters specified
6109 by `special-display-frame-alist'. If `special-display-function'
6110 has been set to some other function, that function is called with
6111 the buffer as first, and nil as second argument.
6112
6113 Alternatively, an element of this list can be specified as
6114 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
6115 name and FRAME-PARAMETERS an alist of \(PARAMETER . VALUE) pairs.
6116 `special-display-popup-frame' will interpret such pairs as frame
6117 parameters when it creates a special frame, overriding the
6118 corresponding values from `special-display-frame-alist'.
6119
6120 As a special case, if FRAME-PARAMETERS contains (same-window . t)
6121 `special-display-popup-frame' displays that buffer in the
6122 selected window. If FRAME-PARAMETERS contains (same-frame . t),
6123 it displays that buffer in a window on the selected frame.
6124
6125 If `special-display-function' specifies some other function than
6126 `special-display-popup-frame', that function is called with the
6127 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
6128 argument.
6129
6130 Finally, an element of this list can be also specified as
6131 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
6132 `special-display-popup-frame' will call FUNCTION with the buffer
6133 named BUFFER-NAME as first argument, and OTHER-ARGS as the
6134 second. If `special-display-function' specifies some other
6135 function, that function is called with the buffer named
6136 BUFFER-NAME as first, and the element's cdr as second argument.
6137
6138 If this variable appears \"not to work\", because you added a
6139 name to it but the corresponding buffer is displayed in the
6140 selected window, look at the values of `same-window-buffer-names'
6141 and `same-window-regexps'. Those variables take precedence over
6142 this one.
6143
6144 See also `special-display-regexps'."
6145 :type '(repeat
6146 (choice :tag "Buffer"
6147 :value ""
6148 (string :format "%v")
6149 (cons :tag "With parameters"
6150 :format "%v"
6151 :value ("" . nil)
6152 (string :format "%v")
6153 (repeat :tag "Parameters"
6154 (cons :format "%v"
6155 (symbol :tag "Parameter")
6156 (sexp :tag "Value"))))
6157 (list :tag "With function"
6158 :format "%v"
6159 :value ("" . nil)
6160 (string :format "%v")
6161 (function :tag "Function")
6162 (repeat :tag "Arguments" (sexp)))))
6163 :group 'windows
6164 :group 'frames)
6165 (make-obsolete-variable
6166 'special-display-buffer-names
6167 "use 2nd arg of `display-buffer' instead." "24.1")
6168
6169 ;;;###autoload
6170 (put 'special-display-buffer-names 'risky-local-variable t)
6171
6172 (defcustom special-display-regexps nil
6173 "List of regexps saying which buffers should be displayed specially.
6174 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
6175 any regexp in this list matches its name, displays it specially
6176 using `special-display-function'. `special-display-popup-frame'
6177 \(the default for `special-display-function') usually displays
6178 the buffer in a separate frame made with the parameters specified
6179 by `special-display-frame-alist'. If `special-display-function'
6180 has been set to some other function, that function is called with
6181 the buffer as first, and nil as second argument.
6182
6183 Alternatively, an element of this list can be specified as
6184 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
6185 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
6186 `special-display-popup-frame' will then interpret these pairs as
6187 frame parameters when creating a special frame for a buffer whose
6188 name matches REGEXP, overriding the corresponding values from
6189 `special-display-frame-alist'.
6190
6191 As a special case, if FRAME-PARAMETERS contains (same-window . t)
6192 `special-display-popup-frame' displays buffers matching REGEXP in
6193 the selected window. \(same-frame . t) in FRAME-PARAMETERS means
6194 to display such buffers in a window on the selected frame.
6195
6196 If `special-display-function' specifies some other function than
6197 `special-display-popup-frame', that function is called with the
6198 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
6199 as second argument.
6200
6201 Finally, an element of this list can be also specified as
6202 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
6203 will then call FUNCTION with the buffer whose name matched
6204 REGEXP as first, and OTHER-ARGS as second argument. If
6205 `special-display-function' specifies some other function, that
6206 function is called with the buffer whose name matched REGEXP
6207 as first, and the element's cdr as second argument.
6208
6209 If this variable appears \"not to work\", because you added a
6210 name to it but the corresponding buffer is displayed in the
6211 selected window, look at the values of `same-window-buffer-names'
6212 and `same-window-regexps'. Those variables take precedence over
6213 this one.
6214
6215 See also `special-display-buffer-names'."
6216 :type '(repeat
6217 (choice :tag "Buffer"
6218 :value ""
6219 (regexp :format "%v")
6220 (cons :tag "With parameters"
6221 :format "%v"
6222 :value ("" . nil)
6223 (regexp :format "%v")
6224 (repeat :tag "Parameters"
6225 (cons :format "%v"
6226 (symbol :tag "Parameter")
6227 (sexp :tag "Value"))))
6228 (list :tag "With function"
6229 :format "%v"
6230 :value ("" . nil)
6231 (regexp :format "%v")
6232 (function :tag "Function")
6233 (repeat :tag "Arguments" (sexp)))))
6234 :group 'windows
6235 :group 'frames)
6236 (make-obsolete-variable
6237 'special-display-regexps
6238 "use 2nd arg of `display-buffer' instead." "24.1")
6239
6240 (defun special-display-p (buffer-name)
6241 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
6242 More precisely, return t if `special-display-buffer-names' or
6243 `special-display-regexps' contain a string entry equaling or
6244 matching BUFFER-NAME. If `special-display-buffer-names' or
6245 `special-display-regexps' contain a list entry whose car equals
6246 or matches BUFFER-NAME, the return value is the cdr of that
6247 entry."
6248 (let ((buffer-names (with-no-warnings special-display-buffer-names))
6249 (regexps (with-no-warnings special-display-regexps))
6250 tmp)
6251 (cond
6252 ((not (stringp buffer-name)))
6253 ((member buffer-name buffer-names)
6254 t)
6255 ((setq tmp (assoc buffer-name buffer-names))
6256 (cdr tmp))
6257 ((catch 'found
6258 (dolist (regexp regexps)
6259 (cond
6260 ((stringp regexp)
6261 (when (string-match-p regexp buffer-name)
6262 (throw 'found t)))
6263 ((and (consp regexp) (stringp (car regexp))
6264 (string-match-p (car regexp) buffer-name))
6265 (throw 'found (cdr regexp))))))))))
6266 (make-obsolete
6267 'special-display-p
6268 "pass argument to buffer display function instead." "24.1")
6269
6270 (defcustom pop-up-frame-alist nil
6271 "Alist of parameters for automatically generated new frames.
6272 You can set this in your init file; for example,
6273
6274 (setq pop-up-frame-alist '((width . 80) (height . 20)))
6275
6276 If non-nil, the value you specify here is used by the default
6277 `pop-up-frame-function' for the creation of new frames.
6278
6279 Since `pop-up-frame-function' is used by `display-buffer' for
6280 making new frames, any value specified here by default affects
6281 the automatic generation of new frames via `display-buffer' and
6282 all functions based on it. The behavior of `make-frame' is not
6283 affected by this variable."
6284 :type '(repeat (cons :format "%v"
6285 (symbol :tag "Parameter")
6286 (sexp :tag "Value")))
6287 :group 'frames)
6288 (make-obsolete-variable
6289 'pop-up-frame-alist
6290 "use 2nd arg of `display-buffer' instead." "24.1")
6291
6292 (defcustom pop-up-frame-function
6293 (lambda () (make-frame pop-up-frame-alist))
6294 "Function used by `display-buffer' for creating a new frame.
6295 This function is called with no arguments and should return a new
6296 frame. The default value calls `make-frame' with the argument
6297 `pop-up-frame-alist'."
6298 :type 'function
6299 :group 'frames)
6300 (make-obsolete-variable
6301 'pop-up-frame-function
6302 "use 2nd arg of `display-buffer' instead." "24.1")
6303
6304 (defcustom pop-up-frames nil
6305 "Whether `display-buffer' should make a separate frame.
6306 If nil, never make a separate frame.
6307 If the value is `graphic-only', make a separate frame
6308 on graphic displays only.
6309 Any other non-nil value means always make a separate frame."
6310 :type '(choice
6311 (const :tag "Never" nil)
6312 (const :tag "On graphic displays only" graphic-only)
6313 (const :tag "Always" t))
6314 :group 'windows
6315 :group 'frames)
6316 (make-obsolete-variable
6317 'pop-up-frames
6318 "use 2nd arg of `display-buffer' instead." "24.1")
6319
6320 (defcustom display-buffer-reuse-frames nil
6321 "Set and non-nil means `display-buffer' should reuse frames.
6322 If the buffer in question is already displayed in a frame, raise
6323 that frame."
6324 :type 'boolean
6325 :version "21.1"
6326 :group 'windows
6327 :group 'frames)
6328 (make-obsolete-variable
6329 'display-buffer-reuse-frames
6330 "use 2nd arg of `display-buffer' instead." "24.1")
6331
6332 (defcustom pop-up-windows 'unset ; t
6333 "Set and non-nil means `display-buffer' should make a new window."
6334 :type 'boolean
6335 :version "24.1"
6336 :group 'windows)
6337 (make-obsolete-variable
6338 'pop-up-windows
6339 "use 2nd arg of `display-buffer' instead." "24.1")
6340
6341 (defcustom split-window-preferred-function 'split-window-sensibly
6342 "Function called by `display-buffer' to split a window.
6343 This function is called with a window as single argument and is
6344 supposed to split that window and return the new window. If the
6345 window can (or shall) not be split, it is supposed to return nil.
6346
6347 The default is to call the function `split-window-sensibly' which
6348 tries to split the window in a way which seems most suitable.
6349 You can customize the options `split-height-threshold' and/or
6350 `split-width-threshold' in order to have `split-window-sensibly'
6351 prefer either vertical or horizontal splitting.
6352
6353 If you set this to any other function, bear in mind that
6354 `display-buffer' may call that function repeatedly; the option
6355 `pop-up-windows' controls which windows may become the argument
6356 of this function.
6357
6358 The window selected at the time `display-buffer' was invoked is
6359 still selected when this function is called. Hence you can
6360 compare the window argument with the value of `selected-window'
6361 if you intend to split the selected window instead or if you do
6362 not want to split the selected window."
6363 :type 'function
6364 :version "23.1"
6365 :group 'windows)
6366 (make-obsolete-variable
6367 'split-window-preferred-function
6368 "use 2nd arg of `display-buffer' instead." "24.1")
6369
6370 (defcustom split-height-threshold 80
6371 "Minimum height for splitting a window to display a buffer.
6372 If this is an integer, `display-buffer' can split a window
6373 vertically only if it has at least this many lines. If this is
6374 nil, `display-buffer' does not split windows vertically. If a
6375 window is the only window on its frame, `display-buffer' may
6376 split it vertically disregarding the value of this variable."
6377 :type '(choice (const nil) (integer :tag "lines"))
6378 :version "23.1"
6379 :group 'windows)
6380 (make-obsolete-variable
6381 'split-height-threshold
6382 "use 2nd arg of `display-buffer' instead." "24.1")
6383
6384 (defcustom split-width-threshold 160
6385 "Minimum width for splitting a window to display a buffer.
6386 If this is an integer, `display-buffer' can split a window
6387 horizontally only if it has at least this many columns. If this
6388 is nil, `display-buffer' cannot split windows horizontally."
6389 :type '(choice (const nil) (integer :tag "columns"))
6390 :version "23.1"
6391 :group 'windows)
6392 (make-obsolete-variable
6393 'split-width-threshold
6394 "use 2nd arg of `display-buffer' instead." "24.1")
6395
6396 (defcustom even-window-heights 'unset ; t
6397 "If set and non-nil `display-buffer' will try to even window heights.
6398 Otherwise `display-buffer' will leave the window configuration
6399 alone. Heights are evened only when `display-buffer' reuses a
6400 window that appears above or below the selected window."
6401 :type 'boolean
6402 :version "24.1"
6403 :group 'windows)
6404 (make-obsolete-variable
6405 'even-window-heights
6406 "use 2nd arg of `display-buffer' instead." "24.1")
6407
6408 (defvar display-buffer-mark-dedicated nil
6409 "Non-nil means `display-buffer' marks the windows it creates as dedicated.
6410 The actual non-nil value of this variable will be copied to the
6411 `window-dedicated-p' flag.")
6412 (make-obsolete-variable
6413 'display-buffer-mark-dedicated
6414 "use 2nd arg of `display-buffer' instead." "24.1")
6415
6416 (defun window-splittable-p (window &optional horizontal)
6417 "Return non-nil if `split-window-sensibly' may split WINDOW.
6418 Optional argument HORIZONTAL nil or omitted means check whether
6419 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
6420 non-nil means check whether WINDOW may be split horizontally.
6421
6422 WINDOW may be split vertically when the following conditions
6423 hold:
6424 - `window-size-fixed' is either nil or equals `width' for the
6425 buffer of WINDOW.
6426 - `split-height-threshold' is an integer and WINDOW is at least as
6427 high as `split-height-threshold'.
6428 - When WINDOW is split evenly, the emanating windows are at least
6429 `window-min-height' lines tall and can accommodate at least one
6430 line plus - if WINDOW has one - a mode line.
6431
6432 WINDOW may be split horizontally when the following conditions
6433 hold:
6434 - `window-size-fixed' is either nil or equals `height' for the
6435 buffer of WINDOW.
6436 - `split-width-threshold' is an integer and WINDOW is at least as
6437 wide as `split-width-threshold'.
6438 - When WINDOW is split evenly, the emanating windows are at least
6439 `window-min-width' or two (whichever is larger) columns wide."
6440 (when (window-live-p window)
6441 (with-current-buffer (window-buffer window)
6442 (if horizontal
6443 ;; A window can be split horizontally when its width is not
6444 ;; fixed, it is at least `split-width-threshold' columns wide
6445 ;; and at least twice as wide as `window-min-width' and 2 (the
6446 ;; latter value is hardcoded).
6447 (and (memq window-size-fixed '(nil height))
6448 ;; Testing `window-full-width-p' here hardly makes any
6449 ;; sense nowadays. This can be done more intuitively by
6450 ;; setting up `split-width-threshold' appropriately.
6451 (numberp split-width-threshold)
6452 (>= (window-width window)
6453 (max split-width-threshold
6454 (* 2 (max window-min-width 2)))))
6455 ;; A window can be split vertically when its height is not
6456 ;; fixed, it is at least `split-height-threshold' lines high,
6457 ;; and it is at least twice as high as `window-min-height' and 2
6458 ;; if it has a modeline or 1.
6459 (and (memq window-size-fixed '(nil width))
6460 (numberp split-height-threshold)
6461 (>= (window-height window)
6462 (max split-height-threshold
6463 (* 2 (max window-min-height
6464 (if mode-line-format 2 1))))))))))
6465 (make-obsolete
6466 'window-splittable-p "use 2nd arg of `display-buffer' instead." "24.1")
6467
6468 (defun split-window-sensibly (window)
6469 "Split WINDOW in a way suitable for `display-buffer'.
6470 If `split-height-threshold' specifies an integer, WINDOW is at
6471 least `split-height-threshold' lines tall and can be split
6472 vertically, split WINDOW into two windows one above the other and
6473 return the lower window. Otherwise, if `split-width-threshold'
6474 specifies an integer, WINDOW is at least `split-width-threshold'
6475 columns wide and can be split horizontally, split WINDOW into two
6476 windows side by side and return the window on the right. If this
6477 can't be done either and WINDOW is the only window on its frame,
6478 try to split WINDOW vertically disregarding any value specified
6479 by `split-height-threshold'. If that succeeds, return the lower
6480 window. Return nil otherwise.
6481
6482 By default `display-buffer' routines call this function to split
6483 the largest or least recently used window. To change the default
6484 customize the option `split-window-preferred-function'.
6485
6486 You can enforce this function to not split WINDOW horizontally,
6487 by setting \(or binding) the variable `split-width-threshold' to
6488 nil. If, in addition, you set `split-height-threshold' to zero,
6489 chances increase that this function does split WINDOW vertically.
6490
6491 In order to not split WINDOW vertically, set \(or bind) the
6492 variable `split-height-threshold' to nil. Additionally, you can
6493 set `split-width-threshold' to zero to make a horizontal split
6494 more likely to occur.
6495
6496 Have a look at the function `window-splittable-p' if you want to
6497 know how `split-window-sensibly' determines whether WINDOW can be
6498 split."
6499 (or (and (with-no-warnings (window-splittable-p window))
6500 ;; Split window vertically.
6501 (with-selected-window window
6502 (split-window-vertically)))
6503 (and (with-no-warnings (window-splittable-p window t))
6504 ;; Split window horizontally.
6505 (with-selected-window window
6506 (split-window-horizontally)))
6507 (and (eq window (frame-root-window (window-frame window)))
6508 (not (window-minibuffer-p window))
6509 ;; If WINDOW is the only window on its frame and is not the
6510 ;; minibuffer window, try to split it vertically disregarding
6511 ;; the value of `split-height-threshold'.
6512 (let ((split-height-threshold 0))
6513 (when (with-no-warnings (window-splittable-p window))
6514 (with-selected-window window
6515 (split-window-vertically)))))))
6516 (make-obsolete
6517 'split-window-sensibly "use 2nd arg of `display-buffer' instead." "24.1")
6518
6519 ;; Functions for converting Emacs 23 buffer display options to buffer
6520 ;; display specifiers.
6521 (defun display-buffer-alist-add (identifiers specifiers &optional no-custom)
6522 "Helper function for `display-buffer-alist-set'."
6523 (unless identifiers
6524 (setq identifiers '((regexp . ".*"))))
6525 (unless (atom specifiers)
6526 (setq specifiers (delq nil specifiers)))
6527
6528 (if no-custom
6529 (setq display-buffer-alist
6530 (cons (cons identifiers specifiers) display-buffer-alist))
6531 (customize-set-variable
6532 'display-buffer-alist
6533 (cons (cons identifiers specifiers) display-buffer-alist))))
6534
6535 (defun display-buffer-alist-set (&optional no-custom add)
6536 "Set `display-buffer-alist' from Emacs 23 buffer display options.
6537 Optional argument NO-CUSTOM nil means use `customize-set-variable'
6538 to set the value of `display-buffer-alist'. NO-CUSTOM non-nil
6539 means to use `setq' instead.
6540
6541 Optional argument ADD nil means to replace the actual value of
6542 `display-buffer-alist' with the value calculated here. ADD
6543 non-nil means prepend the value calculated here to the current
6544 value of `display-buffer-alist'."
6545 (unless add
6546 (if no-custom
6547 (setq display-buffer-alist nil)
6548 (customize-set-variable 'display-buffer-alist nil)))
6549
6550 ;; Disable warnings, there are too many obsolete options here.
6551 (with-no-warnings
6552 ;; `pop-up-windows'
6553 (display-buffer-alist-add
6554 nil
6555 (let ((fun (unless (eq split-window-preferred-function
6556 'split-window-sensibly)
6557 ;; `split-window-sensibly' has been merged into the
6558 ;; `display-buffer-split-window' code as `nil'.
6559 split-window-preferred-function))
6560 (min-height
6561 (if (numberp split-height-threshold)
6562 (/ split-height-threshold 2)
6563 ;; Undocumented hack.
6564 1.0))
6565 (min-width
6566 (if (numberp split-width-threshold)
6567 (/ split-width-threshold 2)
6568 ;; Undocumented hack.
6569 1.0)))
6570 (list
6571 'pop-up-window
6572 (when pop-up-windows ; unset qualifies as t
6573 (list
6574 'pop-up-window
6575 (cons 'largest fun)
6576 (cons 'lru fun)))
6577 (cons 'pop-up-window-min-height min-height)
6578 (cons 'pop-up-window-min-width min-width)))
6579 no-custom)
6580
6581 ;; `pop-up-frames'
6582 (display-buffer-alist-add
6583 nil
6584 (list
6585 'pop-up-frame
6586 (when pop-up-frames
6587 (list 'pop-up-frame pop-up-frames))
6588 (when pop-up-frame-function
6589 (cons 'pop-up-frame-function pop-up-frame-function))
6590 (when pop-up-frame-alist
6591 (cons 'pop-up-frame-alist pop-up-frame-alist)))
6592 no-custom)
6593
6594 ;; `special-display-regexps'
6595 (dolist (entry special-display-regexps)
6596 (cond
6597 ((stringp entry)
6598 ;; Plain string.
6599 (display-buffer-alist-add
6600 `((regexp . ,entry))
6601 (list
6602 'fun-with-args
6603 (list 'fun-with-args special-display-function
6604 special-display-frame-alist))
6605 no-custom))
6606 ((consp entry)
6607 (let ((name (car entry))
6608 (rest (cdr entry)))
6609 (cond
6610 ((functionp (car rest))
6611 ;; A function.
6612 (display-buffer-alist-add
6613 `((name . ,name))
6614 (list
6615 'fun-with-args
6616 ;; Weary.
6617 (list 'fun-with-args (car rest) (cadr rest)))
6618 no-custom))
6619 ((listp rest)
6620 ;; A list of parameters.
6621 (cond
6622 ((assq 'same-window rest)
6623 (display-buffer-alist-add
6624 `((name . ,name))
6625 (list 'reuse-window
6626 (list 'reuse-window 'same)
6627 (list 'reuse-window-dedicated 'weak))
6628 no-custom))
6629 ((assq 'same-frame rest)
6630 (display-buffer-alist-add
6631 `((name . ,name)) (list 'same-frame) no-custom))
6632 (t
6633 (display-buffer-alist-add
6634 `((name . ,name))
6635 (list
6636 'fun-with-args
6637 (list 'fun-with-args special-display-function
6638 special-display-frame-alist))
6639 no-custom)))))))))
6640
6641 ;; `special-display-buffer-names'
6642 (dolist (entry special-display-buffer-names)
6643 (cond
6644 ((stringp entry)
6645 ;; Plain string.
6646 (display-buffer-alist-add
6647 `((name . ,entry))
6648 (list
6649 'fun-with-args
6650 (list 'fun-with-args special-display-function
6651 special-display-frame-alist))
6652 no-custom))
6653 ((consp entry)
6654 (let ((name (car entry))
6655 (rest (cdr entry)))
6656 (cond
6657 ((functionp (car rest))
6658 ;; A function.
6659 (display-buffer-alist-add
6660 `((name . ,name))
6661 (list
6662 'fun-with-args
6663 ;; Weary.
6664 (list 'fun-with-args (car rest) (cadr rest)))
6665 no-custom))
6666 ((listp rest)
6667 ;; A list of parameters.
6668 (cond
6669 ((assq 'same-window rest)
6670 (display-buffer-alist-add
6671 `((name . ,name))
6672 (list 'reuse-window
6673 (list 'reuse-window 'same)
6674 (list 'reuse-window-dedicated 'weak))
6675 no-custom))
6676 ((assq 'same-frame rest)
6677 (display-buffer-alist-add
6678 `((name . ,name)) (list 'same-frame) no-custom))
6679 (t
6680 (display-buffer-alist-add
6681 `((name . ,name))
6682 (list
6683 'fun-with-args
6684 (list 'fun-with-args special-display-function
6685 special-display-frame-alist))
6686 no-custom)))))))))
6687
6688 ;; `same-window-regexps'
6689 (dolist (entry same-window-regexps)
6690 (cond
6691 ((stringp entry)
6692 (display-buffer-alist-add
6693 `((regexp . ,entry))
6694 (list 'reuse-window (list 'reuse-window 'same))
6695 no-custom))
6696 ((consp entry)
6697 (display-buffer-alist-add
6698 `((regexp . ,(car entry)))
6699 (list 'reuse-window (list 'reuse-window 'same))
6700 no-custom))))
6701
6702 ;; `same-window-buffer-names'
6703 (dolist (entry same-window-buffer-names)
6704 (cond
6705 ((stringp entry)
6706 (display-buffer-alist-add
6707 `((name . ,entry))
6708 (list 'reuse-window (list 'reuse-window 'same))
6709 no-custom))
6710 ((consp entry)
6711 (display-buffer-alist-add
6712 `((name . ,(car entry)))
6713 (list 'reuse-window (list 'reuse-window 'same))
6714 no-custom))))
6715
6716 ;; `reuse-window'
6717 (display-buffer-alist-add
6718 nil
6719 (list
6720 'reuse-window
6721 (list 'reuse-window nil 'same
6722 (when (or display-buffer-reuse-frames pop-up-frames)
6723 ;; "0" (all visible and iconified frames) is hardcoded in
6724 ;; Emacs 23.
6725 0))
6726 (unless (memq even-window-heights '(nil unset))
6727 (cons 'reuse-window-even-sizes t)))
6728 no-custom)
6729
6730 ;; `display-buffer-mark-dedicated'
6731 (when display-buffer-mark-dedicated
6732 (display-buffer-alist-add
6733 nil
6734 (list
6735 (cons 'dedicated display-buffer-mark-dedicated))
6736 no-custom)))
6737
6738 display-buffer-alist)
6739 \f
6740 (defun set-window-text-height (window height)
6741 "Set the height in lines of the text display area of WINDOW to HEIGHT.
6742 WINDOW must be a live window. HEIGHT doesn't include the mode
6743 line or header line, if any, or any partial-height lines in the
6744 text display area.
6745
6746 Note that the current implementation of this function cannot
6747 always set the height exactly, but attempts to be conservative,
6748 by allocating more lines than are actually needed in the case
6749 where some error may be present."
6750 (setq window (normalize-live-window window))
6751 (let ((delta (- height (window-text-height window))))
6752 (unless (zerop delta)
6753 ;; Setting window-min-height to a value like 1 can lead to very
6754 ;; bizarre displays because it also allows Emacs to make *other*
6755 ;; windows 1-line tall, which means that there's no more space for
6756 ;; the modeline.
6757 (let ((window-min-height (min 2 height))) ; One text line plus a modeline.
6758 (resize-window window delta)))))
6759
6760 (defun enlarge-window-horizontally (delta)
6761 "Make selected window DELTA columns wider.
6762 Interactively, if no argument is given, make selected window one
6763 column wider."
6764 (interactive "p")
6765 (enlarge-window delta t))
6766
6767 (defun shrink-window-horizontally (delta)
6768 "Make selected window DELTA columns narrower.
6769 Interactively, if no argument is given, make selected window one
6770 column narrower."
6771 (interactive "p")
6772 (shrink-window delta t))
6773
6774 (defun count-screen-lines (&optional beg end count-final-newline window)
6775 "Return the number of screen lines in the region.
6776 The number of screen lines may be different from the number of actual lines,
6777 due to line breaking, display table, etc.
6778
6779 Optional arguments BEG and END default to `point-min' and `point-max'
6780 respectively.
6781
6782 If region ends with a newline, ignore it unless optional third argument
6783 COUNT-FINAL-NEWLINE is non-nil.
6784
6785 The optional fourth argument WINDOW specifies the window used for obtaining
6786 parameters such as width, horizontal scrolling, and so on. The default is
6787 to use the selected window's parameters.
6788
6789 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6790 regardless of which buffer is displayed in WINDOW. This makes possible to use
6791 `count-screen-lines' in any buffer, whether or not it is currently displayed
6792 in some window."
6793 (unless beg
6794 (setq beg (point-min)))
6795 (unless end
6796 (setq end (point-max)))
6797 (if (= beg end)
6798 0
6799 (save-excursion
6800 (save-restriction
6801 (widen)
6802 (narrow-to-region (min beg end)
6803 (if (and (not count-final-newline)
6804 (= ?\n (char-before (max beg end))))
6805 (1- (max beg end))
6806 (max beg end)))
6807 (goto-char (point-min))
6808 (1+ (vertical-motion (buffer-size) window))))))
6809
6810 (defun window-buffer-height (window)
6811 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
6812 (with-current-buffer (window-buffer window)
6813 (max 1
6814 (count-screen-lines (point-min) (point-max)
6815 ;; If buffer ends with a newline, ignore it when
6816 ;; counting height unless point is after it.
6817 (eobp)
6818 window))))
6819
6820 ;;; Resizing buffers to fit their contents exactly.
6821 (defun fit-window-to-buffer (&optional window max-height min-height override)
6822 "Adjust height of WINDOW to display its buffer's contents exactly.
6823 WINDOW can be any live window and defaults to the selected one.
6824
6825 Optional argument MAX-HEIGHT specifies the maximum height of
6826 WINDOW and defaults to the height of WINDOW's frame. Optional
6827 argument MIN-HEIGHT specifies the minimum height of WINDOW and
6828 defaults to `window-min-height'. Both, MAX-HEIGHT and MIN-HEIGHT
6829 are specified in lines and include the mode line and header line,
6830 if any.
6831
6832 Optional argument OVERRIDE non-nil means override restrictions
6833 imposed by `window-min-height' and `window-min-width' on the size
6834 of WINDOW.
6835
6836 Return the number of lines by which WINDOW was enlarged or
6837 shrunk. If an error occurs during resizing, return nil but don't
6838 signal an error.
6839
6840 Note that even if this function makes WINDOW large enough to show
6841 _all_ lines of its buffer you might not see the first lines when
6842 WINDOW was scrolled."
6843 (interactive)
6844 ;; Do all the work in WINDOW and its buffer and restore the selected
6845 ;; window and the current buffer when we're done.
6846 (setq window (normalize-live-window window))
6847 ;; Can't resize a full height or fixed-size window.
6848 (unless (or (window-size-fixed-p window)
6849 (window-full-height-p window))
6850 ;; `with-selected-window' should orderly restore the current buffer.
6851 (with-selected-window window
6852 ;; We are in WINDOW's buffer now.
6853 (let* ( ;; Adjust MIN-HEIGHT.
6854 (min-height
6855 (if override
6856 (window-min-size window nil window)
6857 (max (or min-height window-min-height)
6858 window-safe-min-height)))
6859 (max-window-height
6860 (window-total-size (frame-root-window window)))
6861 ;; Adjust MAX-HEIGHT.
6862 (max-height
6863 (if (or override (not max-height))
6864 max-window-height
6865 (min max-height max-window-height)))
6866 ;; Make `desired-height' the height necessary to show
6867 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
6868 ;; and MAX-HEIGHT.
6869 (desired-height
6870 (max
6871 (min
6872 (+ (count-screen-lines)
6873 ;; For non-minibuffers count the mode line, if any.
6874 (if (and (not (window-minibuffer-p window))
6875 mode-line-format)
6876 1
6877 0)
6878 ;; Count the header line, if any.
6879 (if header-line-format 1 0))
6880 max-height)
6881 min-height))
6882 (desired-delta
6883 (- desired-height (window-total-size window)))
6884 (delta
6885 (if (> desired-delta 0)
6886 (min desired-delta
6887 (window-max-delta window nil window))
6888 (max desired-delta
6889 (- (window-min-delta window nil window))))))
6890 ;; This `condition-case' shouldn't be necessary, but who knows?
6891 (condition-case nil
6892 (if (zerop delta)
6893 ;; Return zero if DELTA became zero in the proces.
6894 0
6895 ;; Don't try to redisplay with the cursor at the end on its
6896 ;; own line--that would force a scroll and spoil things.
6897 (when (and (eobp) (bolp) (not (bobp)))
6898 ;; It's silly to put `point' at the end of the previous
6899 ;; line and so maybe force horizontal scrolling.
6900 (set-window-point window (line-beginning-position 0)))
6901 ;; Call `resize-window' with OVERRIDE argument equal WINDOW.
6902 (resize-window window delta nil window)
6903 ;; Check if the last line is surely fully visible. If
6904 ;; not, enlarge the window.
6905 (let ((end (save-excursion
6906 (goto-char (point-max))
6907 (when (and (bolp) (not (bobp)))
6908 ;; Don't include final newline.
6909 (backward-char 1))
6910 (when truncate-lines
6911 ;; If line-wrapping is turned off, test the
6912 ;; beginning of the last line for
6913 ;; visibility instead of the end, as the
6914 ;; end of the line could be invisible by
6915 ;; virtue of extending past the edge of the
6916 ;; window.
6917 (forward-line 0))
6918 (point))))
6919 (set-window-vscroll window 0)
6920 ;; This loop might in some rare pathological cases raise
6921 ;; an error - another reason for the `condition-case'.
6922 (while (and (< desired-height max-height)
6923 (= desired-height (window-total-size))
6924 (not (pos-visible-in-window-p end)))
6925 (resize-window window 1 nil window)
6926 (setq desired-height (1+ desired-height)))))
6927 (error (setq delta nil)))
6928 delta))))
6929
6930 (defun window-safely-shrinkable-p (&optional window)
6931 "Return t if WINDOW can be shrunk without shrinking other windows.
6932 WINDOW defaults to the selected window."
6933 (with-selected-window (or window (selected-window))
6934 (let ((edges (window-edges)))
6935 ;; The following doesn't satisfy the doc-string's claim when
6936 ;; window and previous-/next-window are not part of the same
6937 ;; combination but still share a common edge. Using
6938 ;; `window-iso-combined-p' instead should handle that.
6939 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
6940 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
6941 (make-obsolete
6942 'window-safely-shrinkable-p "use `window-iso-combined-p' instead." "24.1")
6943
6944 (defun shrink-window-if-larger-than-buffer (&optional window)
6945 "Shrink height of WINDOW if its buffer doesn't need so many lines.
6946 More precisely, shrink WINDOW vertically to be as small as
6947 possible, while still showing the full contents of its buffer.
6948 WINDOW defaults to the selected window.
6949
6950 Do not shrink WINDOW to less than `window-min-height' lines. Do
6951 nothing if the buffer contains more lines than the present window
6952 height, or if some of the window's contents are scrolled out of
6953 view, or if shrinking this window would also shrink another
6954 window, or if the window is the only window of its frame.
6955
6956 Return non-nil if the window was shrunk, nil otherwise."
6957 (interactive)
6958 (setq window (normalize-live-window window))
6959 ;; Make sure that WINDOW is vertically combined and `point-min' is
6960 ;; visible (for whatever reason that's needed). The remaining issues
6961 ;; should be taken care of by `fit-window-to-buffer'.
6962 (when (and (window-iso-combined-p window)
6963 (pos-visible-in-window-p (point-min) window))
6964 (fit-window-to-buffer window (window-total-size window))))
6965 \f
6966 (defun kill-buffer-and-window ()
6967 "Kill the current buffer and delete the selected window."
6968 (interactive)
6969 (let ((window-to-delete (selected-window))
6970 (buffer-to-kill (current-buffer))
6971 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
6972 (unwind-protect
6973 (progn
6974 (add-hook 'kill-buffer-hook delete-window-hook t t)
6975 (if (kill-buffer (current-buffer))
6976 ;; If `delete-window' failed before, we rerun it to regenerate
6977 ;; the error so it can be seen in the echo area.
6978 (when (eq (selected-window) window-to-delete)
6979 (delete-window))))
6980 ;; If the buffer is not dead for some reason (probably because
6981 ;; of a `quit' signal), remove the hook again.
6982 (ignore-errors
6983 (with-current-buffer buffer-to-kill
6984 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
6985
6986 (defun quit-window (&optional kill window)
6987 "Quit WINDOW and bury its buffer.
6988 With a prefix argument, kill the buffer instead. WINDOW defaults
6989 to the selected window.
6990
6991 If WINDOW is non-nil, dedicated, or a minibuffer window, delete
6992 it and, if it's alone on its frame, its frame too. Otherwise, or
6993 if deleting WINDOW fails in any of the preceding cases, display
6994 another buffer in WINDOW using `switch-to-buffer'.
6995
6996 Optional argument KILL non-nil means kill WINDOW's buffer.
6997 Otherwise, bury WINDOW's buffer, see `bury-buffer'."
6998 (interactive "P")
6999 (let ((buffer (window-buffer window)))
7000 (if (or window
7001 (window-minibuffer-p window)
7002 (window-dedicated-p window))
7003 ;; WINDOW is either non-nil, a minibuffer window, or dedicated;
7004 ;; try to delete it.
7005 (let* ((window (or window (selected-window)))
7006 (frame (window-frame window)))
7007 (if (frame-root-window-p window)
7008 ;; WINDOW is alone on its frame.
7009 (delete-frame frame)
7010 ;; There are other windows on its frame, delete WINDOW.
7011 (delete-window window)))
7012 ;; Otherwise, switch to another buffer in the selected window.
7013 (switch-to-buffer nil))
7014
7015 ;; Deal with the buffer.
7016 (if kill
7017 (kill-buffer buffer)
7018 (bury-buffer buffer))))
7019 \f
7020 (defvar recenter-last-op nil
7021 "Indicates the last recenter operation performed.
7022 Possible values: `top', `middle', `bottom', integer or float numbers.")
7023
7024 (defcustom recenter-positions '(middle top bottom)
7025 "Cycling order for `recenter-top-bottom'.
7026 A list of elements with possible values `top', `middle', `bottom',
7027 integer or float numbers that define the cycling order for
7028 the command `recenter-top-bottom'.
7029
7030 Top and bottom destinations are `scroll-margin' lines the from true
7031 window top and bottom. Middle redraws the frame and centers point
7032 vertically within the window. Integer number moves current line to
7033 the specified absolute window-line. Float number between 0.0 and 1.0
7034 means the percentage of the screen space from the top. The default
7035 cycling order is middle -> top -> bottom."
7036 :type '(repeat (choice
7037 (const :tag "Top" top)
7038 (const :tag "Middle" middle)
7039 (const :tag "Bottom" bottom)
7040 (integer :tag "Line number")
7041 (float :tag "Percentage")))
7042 :version "23.2"
7043 :group 'windows)
7044
7045 (defun recenter-top-bottom (&optional arg)
7046 "Move current buffer line to the specified window line.
7047 With no prefix argument, successive calls place point according
7048 to the cycling order defined by `recenter-positions'.
7049
7050 A prefix argument is handled like `recenter':
7051 With numeric prefix ARG, move current line to window-line ARG.
7052 With plain `C-u', move current line to window center."
7053 (interactive "P")
7054 (cond
7055 (arg (recenter arg)) ; Always respect ARG.
7056 (t
7057 (setq recenter-last-op
7058 (if (eq this-command last-command)
7059 (car (or (cdr (member recenter-last-op recenter-positions))
7060 recenter-positions))
7061 (car recenter-positions)))
7062 (let ((this-scroll-margin
7063 (min (max 0 scroll-margin)
7064 (truncate (/ (window-body-height) 4.0)))))
7065 (cond ((eq recenter-last-op 'middle)
7066 (recenter))
7067 ((eq recenter-last-op 'top)
7068 (recenter this-scroll-margin))
7069 ((eq recenter-last-op 'bottom)
7070 (recenter (- -1 this-scroll-margin)))
7071 ((integerp recenter-last-op)
7072 (recenter recenter-last-op))
7073 ((floatp recenter-last-op)
7074 (recenter (round (* recenter-last-op (window-height))))))))))
7075
7076 (define-key global-map [?\C-l] 'recenter-top-bottom)
7077
7078 (defun move-to-window-line-top-bottom (&optional arg)
7079 "Position point relative to window.
7080
7081 With a prefix argument ARG, acts like `move-to-window-line'.
7082
7083 With no argument, positions point at center of window.
7084 Successive calls position point at positions defined
7085 by `recenter-positions'."
7086 (interactive "P")
7087 (cond
7088 (arg (move-to-window-line arg)) ; Always respect ARG.
7089 (t
7090 (setq recenter-last-op
7091 (if (eq this-command last-command)
7092 (car (or (cdr (member recenter-last-op recenter-positions))
7093 recenter-positions))
7094 (car recenter-positions)))
7095 (let ((this-scroll-margin
7096 (min (max 0 scroll-margin)
7097 (truncate (/ (window-body-height) 4.0)))))
7098 (cond ((eq recenter-last-op 'middle)
7099 (call-interactively 'move-to-window-line))
7100 ((eq recenter-last-op 'top)
7101 (move-to-window-line this-scroll-margin))
7102 ((eq recenter-last-op 'bottom)
7103 (move-to-window-line (- -1 this-scroll-margin)))
7104 ((integerp recenter-last-op)
7105 (move-to-window-line recenter-last-op))
7106 ((floatp recenter-last-op)
7107 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
7108
7109 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
7110 \f
7111 ;;; Scrolling commands.
7112
7113 ;;; Scrolling commands which does not signal errors at top/bottom
7114 ;;; of buffer at first key-press (instead moves to top/bottom
7115 ;;; of buffer).
7116
7117 (defcustom scroll-error-top-bottom nil
7118 "Move point to top/bottom of buffer before signalling a scrolling error.
7119 A value of nil means just signal an error if no more scrolling possible.
7120 A value of t means point moves to the beginning or the end of the buffer
7121 \(depending on scrolling direction) when no more scrolling possible.
7122 When point is already on that position, then signal an error."
7123 :type 'boolean
7124 :group 'scrolling
7125 :version "24.1")
7126
7127 (defun scroll-up-command (&optional arg)
7128 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
7129 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
7130 scroll window further, move cursor to the bottom line.
7131 When point is already on that position, then signal an error.
7132 A near full screen is `next-screen-context-lines' less than a full screen.
7133 Negative ARG means scroll downward.
7134 If ARG is the atom `-', scroll downward by nearly full screen."
7135 (interactive "^P")
7136 (cond
7137 ((null scroll-error-top-bottom)
7138 (scroll-up arg))
7139 ((eq arg '-)
7140 (scroll-down-command nil))
7141 ((< (prefix-numeric-value arg) 0)
7142 (scroll-down-command (- (prefix-numeric-value arg))))
7143 ((eobp)
7144 (scroll-up arg)) ; signal error
7145 (t
7146 (condition-case nil
7147 (scroll-up arg)
7148 (end-of-buffer
7149 (if arg
7150 ;; When scrolling by ARG lines can't be done,
7151 ;; move by ARG lines instead.
7152 (forward-line arg)
7153 ;; When ARG is nil for full-screen scrolling,
7154 ;; move to the bottom of the buffer.
7155 (goto-char (point-max))))))))
7156
7157 (put 'scroll-up-command 'scroll-command t)
7158
7159 (defun scroll-down-command (&optional arg)
7160 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
7161 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
7162 scroll window further, move cursor to the top line.
7163 When point is already on that position, then signal an error.
7164 A near full screen is `next-screen-context-lines' less than a full screen.
7165 Negative ARG means scroll upward.
7166 If ARG is the atom `-', scroll upward by nearly full screen."
7167 (interactive "^P")
7168 (cond
7169 ((null scroll-error-top-bottom)
7170 (scroll-down arg))
7171 ((eq arg '-)
7172 (scroll-up-command nil))
7173 ((< (prefix-numeric-value arg) 0)
7174 (scroll-up-command (- (prefix-numeric-value arg))))
7175 ((bobp)
7176 (scroll-down arg)) ; signal error
7177 (t
7178 (condition-case nil
7179 (scroll-down arg)
7180 (beginning-of-buffer
7181 (if arg
7182 ;; When scrolling by ARG lines can't be done,
7183 ;; move by ARG lines instead.
7184 (forward-line (- arg))
7185 ;; When ARG is nil for full-screen scrolling,
7186 ;; move to the top of the buffer.
7187 (goto-char (point-min))))))))
7188
7189 (put 'scroll-down-command 'scroll-command t)
7190
7191 ;;; Scrolling commands which scroll a line instead of full screen.
7192
7193 (defun scroll-up-line (&optional arg)
7194 "Scroll text of selected window upward ARG lines; or one line if no ARG.
7195 If ARG is omitted or nil, scroll upward by one line.
7196 This is different from `scroll-up-command' that scrolls a full screen."
7197 (interactive "p")
7198 (scroll-up (or arg 1)))
7199
7200 (put 'scroll-up-line 'scroll-command t)
7201
7202 (defun scroll-down-line (&optional arg)
7203 "Scroll text of selected window down ARG lines; or one line if no ARG.
7204 If ARG is omitted or nil, scroll down by one line.
7205 This is different from `scroll-down-command' that scrolls a full screen."
7206 (interactive "p")
7207 (scroll-down (or arg 1)))
7208
7209 (put 'scroll-down-line 'scroll-command t)
7210
7211 \f
7212 (defun scroll-other-window-down (lines)
7213 "Scroll the \"other window\" down.
7214 For more details, see the documentation for `scroll-other-window'."
7215 (interactive "P")
7216 (scroll-other-window
7217 ;; Just invert the argument's meaning.
7218 ;; We can do that without knowing which window it will be.
7219 (if (eq lines '-) nil
7220 (if (null lines) '-
7221 (- (prefix-numeric-value lines))))))
7222
7223 (defun beginning-of-buffer-other-window (arg)
7224 "Move point to the beginning of the buffer in the other window.
7225 Leave mark at previous position.
7226 With arg N, put point N/10 of the way from the true beginning."
7227 (interactive "P")
7228 (let ((orig-window (selected-window))
7229 (window (other-window-for-scrolling)))
7230 ;; We use unwind-protect rather than save-window-excursion
7231 ;; because the latter would preserve the things we want to change.
7232 (unwind-protect
7233 (progn
7234 (select-window window)
7235 ;; Set point and mark in that window's buffer.
7236 (with-no-warnings
7237 (beginning-of-buffer arg))
7238 ;; Set point accordingly.
7239 (recenter '(t)))
7240 (select-window orig-window))))
7241
7242 (defun end-of-buffer-other-window (arg)
7243 "Move point to the end of the buffer in the other window.
7244 Leave mark at previous position.
7245 With arg N, put point N/10 of the way from the true end."
7246 (interactive "P")
7247 ;; See beginning-of-buffer-other-window for comments.
7248 (let ((orig-window (selected-window))
7249 (window (other-window-for-scrolling)))
7250 (unwind-protect
7251 (progn
7252 (select-window window)
7253 (with-no-warnings
7254 (end-of-buffer arg))
7255 (recenter '(t)))
7256 (select-window orig-window))))
7257 \f
7258 (defvar mouse-autoselect-window-timer nil
7259 "Timer used by delayed window autoselection.")
7260
7261 (defvar mouse-autoselect-window-position nil
7262 "Last mouse position recorded by delayed window autoselection.")
7263
7264 (defvar mouse-autoselect-window-window nil
7265 "Last window recorded by delayed window autoselection.")
7266
7267 (defvar mouse-autoselect-window-state nil
7268 "When non-nil, special state of delayed window autoselection.
7269 Possible values are `suspend' \(suspend autoselection after a menu or
7270 scrollbar interaction\) and `select' \(the next invocation of
7271 'handle-select-window' shall select the window immediately\).")
7272
7273 (defun mouse-autoselect-window-cancel (&optional force)
7274 "Cancel delayed window autoselection.
7275 Optional argument FORCE means cancel unconditionally."
7276 (unless (and (not force)
7277 ;; Don't cancel for select-window or select-frame events
7278 ;; or when the user drags a scroll bar.
7279 (or (memq this-command
7280 '(handle-select-window handle-switch-frame))
7281 (and (eq this-command 'scroll-bar-toolkit-scroll)
7282 (memq (nth 4 (event-end last-input-event))
7283 '(handle end-scroll)))))
7284 (setq mouse-autoselect-window-state nil)
7285 (when (timerp mouse-autoselect-window-timer)
7286 (cancel-timer mouse-autoselect-window-timer))
7287 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
7288
7289 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
7290 "Start delayed window autoselection.
7291 MOUSE-POSITION is the last position where the mouse was seen as returned
7292 by `mouse-position'. Optional argument WINDOW non-nil denotes the
7293 window where the mouse was seen. Optional argument SUSPEND non-nil
7294 means suspend autoselection."
7295 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
7296 (setq mouse-autoselect-window-position mouse-position)
7297 (when window (setq mouse-autoselect-window-window window))
7298 (setq mouse-autoselect-window-state (when suspend 'suspend))
7299 ;; Install timer which runs `mouse-autoselect-window-select' after
7300 ;; `mouse-autoselect-window' seconds.
7301 (setq mouse-autoselect-window-timer
7302 (run-at-time
7303 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
7304
7305 (defun mouse-autoselect-window-select ()
7306 "Select window with delayed window autoselection.
7307 If the mouse position has stabilized in a non-selected window, select
7308 that window. The minibuffer window is selected only if the minibuffer is
7309 active. This function is run by `mouse-autoselect-window-timer'."
7310 (ignore-errors
7311 (let* ((mouse-position (mouse-position))
7312 (window
7313 (ignore-errors
7314 (window-at (cadr mouse-position) (cddr mouse-position)
7315 (car mouse-position)))))
7316 (cond
7317 ((or (menu-or-popup-active-p)
7318 (and window
7319 (not (coordinates-in-window-p (cdr mouse-position) window))))
7320 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
7321 ;; of WINDOW, temporarily suspend delayed autoselection.
7322 (mouse-autoselect-window-start mouse-position nil t))
7323 ((eq mouse-autoselect-window-state 'suspend)
7324 ;; Delayed autoselection was temporarily suspended, reenable it.
7325 (mouse-autoselect-window-start mouse-position))
7326 ((and window (not (eq window (selected-window)))
7327 (or (not (numberp mouse-autoselect-window))
7328 (and (> mouse-autoselect-window 0)
7329 ;; If `mouse-autoselect-window' is positive, select
7330 ;; window if the window is the same as before.
7331 (eq window mouse-autoselect-window-window))
7332 ;; Otherwise select window if the mouse is at the same
7333 ;; position as before. Observe that the first test after
7334 ;; starting autoselection usually fails since the value of
7335 ;; `mouse-autoselect-window-position' recorded there is the
7336 ;; position where the mouse has entered the new window and
7337 ;; not necessarily where the mouse has stopped moving.
7338 (equal mouse-position mouse-autoselect-window-position))
7339 ;; The minibuffer is a candidate window if it's active.
7340 (or (not (window-minibuffer-p window))
7341 (eq window (active-minibuffer-window))))
7342 ;; Mouse position has stabilized in non-selected window: Cancel
7343 ;; delayed autoselection and try to select that window.
7344 (mouse-autoselect-window-cancel t)
7345 ;; Select window where mouse appears unless the selected window is the
7346 ;; minibuffer. Use `unread-command-events' in order to execute pre-
7347 ;; and post-command hooks and trigger idle timers. To avoid delaying
7348 ;; autoselection again, set `mouse-autoselect-window-state'."
7349 (unless (window-minibuffer-p (selected-window))
7350 (setq mouse-autoselect-window-state 'select)
7351 (setq unread-command-events
7352 (cons (list 'select-window (list window))
7353 unread-command-events))))
7354 ((or (and window (eq window (selected-window)))
7355 (not (numberp mouse-autoselect-window))
7356 (equal mouse-position mouse-autoselect-window-position))
7357 ;; Mouse position has either stabilized in the selected window or at
7358 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
7359 (mouse-autoselect-window-cancel t))
7360 (t
7361 ;; Mouse position has not stabilized yet, resume delayed
7362 ;; autoselection.
7363 (mouse-autoselect-window-start mouse-position window))))))
7364
7365 (defun handle-select-window (event)
7366 "Handle select-window events."
7367 (interactive "e")
7368 (let ((window (posn-window (event-start event))))
7369 (unless (or (not (window-live-p window))
7370 ;; Don't switch if we're currently in the minibuffer.
7371 ;; This tries to work around problems where the
7372 ;; minibuffer gets unselected unexpectedly, and where
7373 ;; you then have to move your mouse all the way down to
7374 ;; the minibuffer to select it.
7375 (window-minibuffer-p (selected-window))
7376 ;; Don't switch to minibuffer window unless it's active.
7377 (and (window-minibuffer-p window)
7378 (not (minibuffer-window-active-p window)))
7379 ;; Don't switch when autoselection shall be delayed.
7380 (and (numberp mouse-autoselect-window)
7381 (not (zerop mouse-autoselect-window))
7382 (not (eq mouse-autoselect-window-state 'select))
7383 (progn
7384 ;; Cancel any delayed autoselection.
7385 (mouse-autoselect-window-cancel t)
7386 ;; Start delayed autoselection from current mouse
7387 ;; position and window.
7388 (mouse-autoselect-window-start (mouse-position) window)
7389 ;; Executing a command cancels delayed autoselection.
7390 (add-hook
7391 'pre-command-hook 'mouse-autoselect-window-cancel))))
7392 (when mouse-autoselect-window
7393 ;; Reset state of delayed autoselection.
7394 (setq mouse-autoselect-window-state nil)
7395 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
7396 (run-hooks 'mouse-leave-buffer-hook))
7397 (select-window window))))
7398
7399 (defun truncated-partial-width-window-p (&optional window)
7400 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
7401 WINDOW defaults to the selected window.
7402 Return nil if WINDOW is not a partial-width window
7403 (regardless of the value of `truncate-lines').
7404 Otherwise, consult the value of `truncate-partial-width-windows'
7405 for the buffer shown in WINDOW."
7406 (unless window
7407 (setq window (selected-window)))
7408 (unless (window-full-width-p window)
7409 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
7410 (window-buffer window))))
7411 (if (integerp t-p-w-w)
7412 (< (window-width window) t-p-w-w)
7413 t-p-w-w))))
7414 \f
7415 (define-key ctl-x-map "0" 'delete-window)
7416 (define-key ctl-x-map "1" 'delete-other-windows)
7417 (define-key ctl-x-map "2" 'split-window-above-each-other)
7418 (define-key ctl-x-map "3" 'split-window-side-by-side)
7419 (define-key ctl-x-map "o" 'other-window)
7420 (define-key ctl-x-map "^" 'enlarge-window)
7421 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
7422 (define-key ctl-x-map "{" 'shrink-window-horizontally)
7423 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
7424 (define-key ctl-x-map "+" 'balance-windows)
7425 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
7426
7427 ;;; window.el ends here