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