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