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