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