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