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