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