* window.el (window-normalize-window): Rename from window-normalize-any-window.
[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-sibling' and
67 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
68 ;; they don't substitute the selected window for nil), and they return
69 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
70 ;; a minibuffer window).
71 (defsubst window-right (window)
72 "Return WINDOW's right sibling.
73 Return nil if WINDOW is the root window of its frame. WINDOW can
74 be any window."
75 (and window (window-parent window) (window-next-sibling window)))
76
77 (defsubst window-left (window)
78 "Return WINDOW's left sibling.
79 Return nil if WINDOW is the root window of its frame. WINDOW can
80 be any window."
81 (and window (window-parent window) (window-prev-sibling window)))
82
83 (defsubst window-child (window)
84 "Return WINDOW's first child window."
85 (or (window-top-child window) (window-left-child window)))
86
87 (defun window-child-count (window)
88 "Return number of WINDOW's child windows."
89 (let ((count 0))
90 (when (and (windowp window) (setq window (window-child window)))
91 (while window
92 (setq count (1+ count))
93 (setq window (window-next-sibling window))))
94 count))
95
96 (defun window-last-child (window)
97 "Return last child window of WINDOW."
98 (when (and (windowp window) (setq window (window-child window)))
99 (while (window-next-sibling window)
100 (setq window (window-next-sibling window))))
101 window)
102
103 (defsubst window-valid-p (object)
104 "Return t if OBJECT denotes a live window or internal window.
105 Otherwise, return nil; this includes the case where OBJECT is a
106 deleted window."
107 (and (windowp object)
108 (or (window-buffer object) (window-child object))
109 t))
110
111 (defsubst window-normalize-buffer (buffer-or-name)
112 "Return buffer specified by BUFFER-OR-NAME.
113 BUFFER-OR-NAME must be either a buffer or a string naming a live
114 buffer and defaults to the current buffer."
115 (cond
116 ((not buffer-or-name)
117 (current-buffer))
118 ((bufferp buffer-or-name)
119 (if (buffer-live-p buffer-or-name)
120 buffer-or-name
121 (error "Buffer %s is not a live buffer" buffer-or-name)))
122 ((get-buffer buffer-or-name))
123 (t
124 (error "No such buffer %s" buffer-or-name))))
125
126 (defsubst window-normalize-frame (frame)
127 "Return frame specified by FRAME.
128 FRAME must be a live frame and defaults to the selected frame."
129 (if frame
130 (if (frame-live-p frame)
131 frame
132 (error "%s is not a live frame" frame))
133 (selected-frame)))
134
135 (defsubst window-normalize-window (window &optional live-only)
136 "Return window specified by WINDOW.
137 If WINDOW is nil, return `selected-window'.
138 If WINDOW is a live window or internal window, return WINDOW;
139 if LIVE-ONLY is non-nil, return WINDOW for a live window only.
140 Otherwise, signal an error."
141 (cond ((null window)
142 (selected-window))
143 (live-only
144 (if (window-live-p window)
145 window
146 (error "%s is not a live window" window)))
147 ((if (window-valid-p window)
148 window
149 (error "%s is not a window" window)))))
150
151 (defvar ignore-window-parameters nil
152 "If non-nil, standard functions ignore window parameters.
153 The functions currently affected by this are `split-window',
154 `delete-window', `delete-other-windows' and `other-window'.
155
156 An application may bind this to a non-nil value around calls to
157 these functions to inhibit processing of window parameters.")
158
159 (defconst window-safe-min-height 1
160 "The absolute minimum number of lines of a window.
161 Anything less might crash Emacs.")
162
163 (defcustom window-min-height 4
164 "The minimum number of lines of any window.
165 The value has to accommodate a mode- or header-line if present.
166 A value less than `window-safe-min-height' is ignored. The value
167 of this variable is honored when windows are resized or split.
168
169 Applications should never rebind this variable. To resize a
170 window to a height less than the one specified here, an
171 application should instead call `window-resize' with a non-nil
172 IGNORE argument. In order to have `split-window' make a window
173 shorter, explictly specify the SIZE argument of that function."
174 :type 'integer
175 :version "24.1"
176 :group 'windows)
177
178 (defconst window-safe-min-width 2
179 "The absolute minimum number of columns of a window.
180 Anything less might crash Emacs.")
181
182 (defcustom window-min-width 10
183 "The minimum number of columns of any window.
184 The value has to accommodate margins, fringes, or scrollbars if
185 present. A value less than `window-safe-min-width' is ignored.
186 The value of this variable is honored when windows are resized or
187 split.
188
189 Applications should never rebind this variable. To resize a
190 window to a width less than the one specified here, an
191 application should instead call `window-resize' with a non-nil
192 IGNORE argument. In order to have `split-window' make a window
193 narrower, explictly specify the SIZE argument of that function."
194 :type 'integer
195 :version "24.1"
196 :group 'windows)
197
198 (defun window-combination-p (&optional window horizontal)
199 "If WINDOW is a vertical combination return WINDOW's first child.
200 WINDOW can be any window and defaults to the selected one.
201 Optional argument HORIZONTAL non-nil means return WINDOW's first
202 child if WINDOW is a horizontal combination."
203 (setq window (window-normalize-window window))
204 (if horizontal
205 (window-left-child window)
206 (window-top-child window)))
207
208 (defsubst window-combined-p (&optional window horizontal)
209 "Return non-nil if and only if WINDOW is vertically combined.
210 WINDOW can be any window and defaults to the selected one.
211 Optional argument HORIZONTAL non-nil means return non-nil if and
212 only if WINDOW is horizontally combined."
213 (setq window (window-normalize-window window))
214 (let ((parent (window-parent window)))
215 (and parent (window-combination-p parent horizontal))))
216
217 (defun window-combinations (&optional window horizontal)
218 "Return largest number of vertically arranged subwindows of WINDOW.
219 WINDOW can be any window and defaults to the selected one.
220 Optional argument HORIZONTAL non-nil means to return the largest
221 number of horizontally arranged subwindows of WINDOW."
222 (setq window (window-normalize-window window))
223 (cond
224 ((window-live-p window)
225 ;; If WINDOW is live, return 1.
226 1)
227 ((window-combination-p window horizontal)
228 ;; If WINDOW is iso-combined, return the sum of the values for all
229 ;; subwindows of WINDOW.
230 (let ((child (window-child window))
231 (count 0))
232 (while child
233 (setq count
234 (+ (window-combinations child horizontal)
235 count))
236 (setq child (window-right child)))
237 count))
238 (t
239 ;; If WINDOW is not iso-combined, return the maximum value of any
240 ;; subwindow of WINDOW.
241 (let ((child (window-child window))
242 (count 1))
243 (while child
244 (setq count
245 (max (window-combinations child horizontal)
246 count))
247 (setq child (window-right child)))
248 count))))
249
250 (defun walk-window-tree-1 (proc walk-window-tree-window any &optional sub-only)
251 "Helper function for `walk-window-tree' and `walk-window-subtree'."
252 (let (walk-window-tree-buffer)
253 (while walk-window-tree-window
254 (setq walk-window-tree-buffer
255 (window-buffer walk-window-tree-window))
256 (when (or walk-window-tree-buffer any)
257 (funcall proc walk-window-tree-window))
258 (unless walk-window-tree-buffer
259 (walk-window-tree-1
260 proc (window-left-child walk-window-tree-window) any)
261 (walk-window-tree-1
262 proc (window-top-child walk-window-tree-window) any))
263 (if sub-only
264 (setq walk-window-tree-window nil)
265 (setq walk-window-tree-window
266 (window-right walk-window-tree-window))))))
267
268 (defun walk-window-tree (proc &optional frame any)
269 "Run function PROC on each live window of FRAME.
270 PROC must be a function with one argument - a window. FRAME must
271 be a live frame and defaults to the selected one. ANY, if
272 non-nil means to run PROC on all live and internal windows of
273 FRAME.
274
275 This function performs a pre-order, depth-first traversal of the
276 window tree. If PROC changes the window tree, the result is
277 unpredictable."
278 (let ((walk-window-tree-frame (window-normalize-frame frame)))
279 (walk-window-tree-1
280 proc (frame-root-window walk-window-tree-frame) any)))
281
282 (defun walk-window-subtree (proc &optional window any)
283 "Run function PROC on each live subwindow of WINDOW.
284 WINDOW defaults to the selected window. PROC must be a function
285 with one argument - a window. ANY, if non-nil means to run PROC
286 on all live and internal subwindows of WINDOW.
287
288 This function performs a pre-order, depth-first traversal of the
289 window tree rooted at WINDOW. If PROC changes that window tree,
290 the result is unpredictable."
291 (setq window (window-normalize-window window))
292 (walk-window-tree-1 proc window any t))
293
294 (defun windows-with-parameter (parameter &optional value frame any values)
295 "Return a list of all windows on FRAME with PARAMETER non-nil.
296 FRAME defaults to the selected frame. Optional argument VALUE
297 non-nil means only return windows whose window-parameter value of
298 PARAMETER equals VALUE \(comparison is done using `equal').
299 Optional argument ANY non-nil means consider internal windows
300 too. Optional argument VALUES non-nil means return a list of cons
301 cells whose car is the value of the parameter and whose cdr is
302 the window."
303 (let (this-value windows)
304 (walk-window-tree
305 (lambda (window)
306 (when (and (setq this-value (window-parameter window parameter))
307 (or (not value) (or (equal value this-value))))
308 (setq windows
309 (if values
310 (cons (cons this-value window) windows)
311 (cons window windows)))))
312 frame any)
313
314 (nreverse windows)))
315
316 (defun window-with-parameter (parameter &optional value frame any)
317 "Return first window on FRAME with PARAMETER non-nil.
318 FRAME defaults to the selected frame. Optional argument VALUE
319 non-nil means only return a window whose window-parameter value
320 for PARAMETER equals VALUE \(comparison is done with `equal').
321 Optional argument ANY non-nil means consider internal windows
322 too."
323 (let (this-value windows)
324 (catch 'found
325 (walk-window-tree
326 (lambda (window)
327 (when (and (setq this-value (window-parameter window parameter))
328 (or (not value) (equal value this-value)))
329 (throw 'found window)))
330 frame any))))
331
332 ;;; Atomic windows.
333 (defun window-atom-root (&optional window)
334 "Return root of atomic window WINDOW is a part of.
335 WINDOW can be any window and defaults to the selected one.
336 Return nil if WINDOW is not part of a atomic window."
337 (setq window (window-normalize-window window))
338 (let (root)
339 (while (and window (window-parameter window 'window-atom))
340 (setq root window)
341 (setq window (window-parent window)))
342 root))
343
344 (defun window-make-atom (window)
345 "Make WINDOW an atomic window.
346 WINDOW must be an internal window. Return WINDOW."
347 (if (not (window-child window))
348 (error "Window %s is not an internal window" window)
349 (walk-window-subtree
350 (lambda (window)
351 (set-window-parameter window 'window-atom t))
352 window t)
353 window))
354
355 (defun window-atom-check-1 (window)
356 "Subroutine of `window-atom-check'."
357 (when window
358 (if (window-parameter window 'window-atom)
359 (let ((count 0))
360 (when (or (catch 'reset
361 (walk-window-subtree
362 (lambda (window)
363 (if (window-parameter window 'window-atom)
364 (setq count (1+ count))
365 (throw 'reset t)))
366 window t))
367 ;; count >= 1 must hold here. If there's no other
368 ;; window around dissolve this atomic window.
369 (= count 1))
370 ;; Dissolve atomic window.
371 (walk-window-subtree
372 (lambda (window)
373 (set-window-parameter window 'window-atom nil))
374 window t)))
375 ;; Check children.
376 (unless (window-buffer window)
377 (window-atom-check-1 (window-left-child window))
378 (window-atom-check-1 (window-top-child window))))
379 ;; Check right sibling
380 (window-atom-check-1 (window-right window))))
381
382 (defun window-atom-check (&optional frame)
383 "Check atomicity of all windows on FRAME.
384 FRAME defaults to the selected frame. If an atomic window is
385 wrongly configured, reset the atomicity of all its subwindows to
386 nil. An atomic window is wrongly configured if it has no
387 subwindows or one of its subwindows is not atomic."
388 (window-atom-check-1 (frame-root-window frame)))
389
390 ;; Side windows.
391 (defvar window-sides '(left top right bottom)
392 "Window sides.")
393
394 (defcustom window-sides-vertical nil
395 "If non-nil, left and right side windows are full height.
396 Otherwise, top and bottom side windows are full width."
397 :type 'boolean
398 :group 'windows
399 :version "24.1")
400
401 (defcustom window-sides-slots '(nil nil nil nil)
402 "Maximum number of side window slots.
403 The value is a list of four elements specifying the number of
404 side window slots on \(in this order) the left, top, right and
405 bottom side of each frame. If an element is a number, this means
406 to display at most that many side windows on the corresponding
407 side. If an element is nil, this means there's no bound on the
408 number of slots on that side."
409 :risky t
410 :type
411 '(list
412 :value (nil nil nil nil)
413 (choice
414 :tag "Left"
415 :help-echo "Maximum slots of left side window."
416 :value nil
417 :format "%[Left%] %v\n"
418 (const :tag "Unlimited" :format "%t" nil)
419 (integer :tag "Number" :value 2 :size 5))
420 (choice
421 :tag "Top"
422 :help-echo "Maximum slots of top side window."
423 :value nil
424 :format "%[Top%] %v\n"
425 (const :tag "Unlimited" :format "%t" nil)
426 (integer :tag "Number" :value 3 :size 5))
427 (choice
428 :tag "Right"
429 :help-echo "Maximum slots of right side window."
430 :value nil
431 :format "%[Right%] %v\n"
432 (const :tag "Unlimited" :format "%t" nil)
433 (integer :tag "Number" :value 2 :size 5))
434 (choice
435 :tag "Bottom"
436 :help-echo "Maximum slots of bottom side window."
437 :value nil
438 :format "%[Bottom%] %v\n"
439 (const :tag "Unlimited" :format "%t" nil)
440 (integer :tag "Number" :value 3 :size 5)))
441 :group 'windows)
442
443 (defun window-side-check (&optional frame)
444 "Check the window-side parameter of all windows on FRAME.
445 FRAME defaults to the selected frame. If the configuration is
446 invalid, reset all window-side parameters to nil.
447
448 A valid configuration has to preserve the following invariant:
449
450 - If a window has a non-nil window-side parameter, it must have a
451 parent window and the parent window's window-side parameter
452 must be either nil or the same as for window.
453
454 - If windows with non-nil window-side parameters exist, there
455 must be at most one window of each side and non-side with a
456 parent whose window-side parameter is nil and there must be no
457 leaf window whose window-side parameter is nil."
458 (let (normal none left top right bottom
459 side parent parent-side code)
460 (when (or (catch 'reset
461 (walk-window-tree
462 (lambda (window)
463 (setq side (window-parameter window 'window-side))
464 (setq parent (window-parent window))
465 (setq parent-side
466 (and parent (window-parameter parent 'window-side)))
467 ;; The following `cond' seems a bit tedious, but I'd
468 ;; rather stick to using just the stack.
469 (cond
470 (parent-side
471 (when (not (eq parent-side side))
472 ;; A parent whose window-side is non-nil must
473 ;; have a child with the same window-side.
474 (throw 'reset t)))
475 ;; Now check that there's more than one main window
476 ;; for any of none, left, top, right and bottom.
477 ((eq side 'none)
478 (if none
479 (throw 'reset t)
480 (setq none t)))
481 ((eq side 'left)
482 (if left
483 (throw 'reset t)
484 (setq left t)))
485 ((eq side 'top)
486 (if top
487 (throw 'reset t)
488 (setq top t)))
489 ((eq side 'right)
490 (if right
491 (throw 'reset t)
492 (setq right t)))
493 ((eq side 'bottom)
494 (if bottom
495 (throw 'reset t)
496 (setq bottom t)))
497 ((window-buffer window)
498 ;; A leaf window without window-side parameter,
499 ;; record its existence.
500 (setq normal t))))
501 frame t))
502 (if none
503 ;; At least one non-side window exists, so there must
504 ;; be at least one side-window and no normal window.
505 (or (not (or left top right bottom)) normal)
506 ;; No non-side window exists, so there must be no side
507 ;; window either.
508 (or left top right bottom)))
509 (walk-window-tree
510 (lambda (window)
511 (set-window-parameter window 'window-side nil))
512 frame t))))
513
514 (defun window-check (&optional frame)
515 "Check atomic and side windows on FRAME.
516 FRAME defaults to the selected frame."
517 (window-side-check frame)
518 (window-atom-check frame))
519
520 ;;; Window sizes.
521 (defvar window-size-fixed nil
522 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
523 If the value is `height', then only the window's height is fixed.
524 If the value is `width', then only the window's width is fixed.
525 Any other non-nil value fixes both the width and the height.
526
527 Emacs won't change the size of any window displaying that buffer,
528 unless it has no other choice \(like when deleting a neighboring
529 window).")
530 (make-variable-buffer-local 'window-size-fixed)
531
532 (defsubst window-size-ignore (window ignore)
533 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
534 (if (window-valid-p ignore) (eq window ignore) ignore))
535
536 (defun window-min-size (&optional window horizontal ignore)
537 "Return the minimum number of lines of WINDOW.
538 WINDOW can be an arbitrary window and defaults to the selected
539 one. Optional argument HORIZONTAL non-nil means return the
540 minimum number of columns of WINDOW.
541
542 Optional argument IGNORE non-nil means ignore any restrictions
543 imposed by fixed size windows, `window-min-height' or
544 `window-min-width' settings. IGNORE equal `safe' means live
545 windows may get as small as `window-safe-min-height' lines and
546 `window-safe-min-width' columns. IGNORE a window means ignore
547 restrictions for that window only."
548 (window-min-size-1
549 (window-normalize-window window) horizontal ignore))
550
551 (defun window-min-size-1 (window horizontal ignore)
552 "Internal function of `window-min-size'."
553 (let ((sub (window-child window)))
554 (if sub
555 (let ((value 0))
556 ;; WINDOW is an internal window.
557 (if (window-combined-p sub horizontal)
558 ;; The minimum size of an iso-combination is the sum of
559 ;; the minimum sizes of its subwindows.
560 (while sub
561 (setq value (+ value
562 (window-min-size-1 sub horizontal ignore)))
563 (setq sub (window-right sub)))
564 ;; The minimum size of an ortho-combination is the maximum of
565 ;; the minimum sizes of its subwindows.
566 (while sub
567 (setq value (max value
568 (window-min-size-1 sub horizontal ignore)))
569 (setq sub (window-right sub))))
570 value)
571 (with-current-buffer (window-buffer window)
572 (cond
573 ((and (not (window-size-ignore window ignore))
574 (window-size-fixed-p window horizontal))
575 ;; The minimum size of a fixed size window is its size.
576 (window-total-size window horizontal))
577 ((or (eq ignore 'safe) (eq ignore window))
578 ;; If IGNORE equals `safe' or WINDOW return the safe values.
579 (if horizontal window-safe-min-width window-safe-min-height))
580 (horizontal
581 ;; For the minimum width of a window take fringes and
582 ;; scroll-bars into account. This is questionable and should
583 ;; be removed as soon as we are able to split (and resize)
584 ;; windows such that the new (or resized) windows can get a
585 ;; size less than the user-specified `window-min-height' and
586 ;; `window-min-width'.
587 (let ((frame (window-frame window))
588 (fringes (window-fringes window))
589 (scroll-bars (window-scroll-bars window)))
590 (max
591 (+ window-safe-min-width
592 (ceiling (car fringes) (frame-char-width frame))
593 (ceiling (cadr fringes) (frame-char-width frame))
594 (cond
595 ((memq (nth 2 scroll-bars) '(left right))
596 (nth 1 scroll-bars))
597 ((memq (frame-parameter frame 'vertical-scroll-bars)
598 '(left right))
599 (ceiling (or (frame-parameter frame 'scroll-bar-width) 14)
600 (frame-char-width)))
601 (t 0)))
602 (if (and (not (window-size-ignore window ignore))
603 (numberp window-min-width))
604 window-min-width
605 0))))
606 (t
607 ;; For the minimum height of a window take any mode- or
608 ;; header-line into account.
609 (max (+ window-safe-min-height
610 (if header-line-format 1 0)
611 (if mode-line-format 1 0))
612 (if (and (not (window-size-ignore window ignore))
613 (numberp window-min-height))
614 window-min-height
615 0))))))))
616
617 (defun window-sizable (window delta &optional horizontal ignore)
618 "Return DELTA if DELTA lines can be added to WINDOW.
619 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
620 columns can be added to WINDOW. A return value of zero means
621 that no lines (or columns) can be added to WINDOW.
622
623 This function looks only at WINDOW and its subwindows. The
624 function `window-resizable' looks at other windows as well.
625
626 DELTA positive means WINDOW shall be enlarged by DELTA lines or
627 columns. If WINDOW cannot be enlarged by DELTA lines or columns
628 return the maximum value in the range 0..DELTA by which WINDOW
629 can be enlarged.
630
631 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
632 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
633 return the minimum value in the range DELTA..0 by which WINDOW
634 can be shrunk.
635
636 Optional argument IGNORE non-nil means ignore any restrictions
637 imposed by fixed size windows, `window-min-height' or
638 `window-min-width' settings. IGNORE equal `safe' means live
639 windows may get as small as `window-safe-min-height' lines and
640 `window-safe-min-width' columns. IGNORE any window means ignore
641 restrictions for that window only."
642 (setq window (window-normalize-window window))
643 (cond
644 ((< delta 0)
645 (max (- (window-min-size window horizontal ignore)
646 (window-total-size window horizontal))
647 delta))
648 ((window-size-ignore window ignore)
649 delta)
650 ((> delta 0)
651 (if (window-size-fixed-p window horizontal)
652 0
653 delta))
654 (t 0)))
655
656 (defsubst window-sizable-p (window delta &optional horizontal ignore)
657 "Return t if WINDOW can be resized by DELTA lines.
658 For the meaning of the arguments of this function see the
659 doc-string of `window-sizable'."
660 (setq window (window-normalize-window window))
661 (if (> delta 0)
662 (>= (window-sizable window delta horizontal ignore) delta)
663 (<= (window-sizable window delta horizontal ignore) delta)))
664
665 (defun window-size-fixed-1 (window horizontal)
666 "Internal function for `window-size-fixed-p'."
667 (let ((sub (window-child window)))
668 (catch 'fixed
669 (if sub
670 ;; WINDOW is an internal window.
671 (if (window-combined-p sub horizontal)
672 ;; An iso-combination is fixed size if all its subwindows
673 ;; are fixed-size.
674 (progn
675 (while sub
676 (unless (window-size-fixed-1 sub horizontal)
677 ;; We found a non-fixed-size subwindow, so WINDOW's
678 ;; size is not fixed.
679 (throw 'fixed nil))
680 (setq sub (window-right sub)))
681 ;; All subwindows are fixed-size, so WINDOW's size is
682 ;; fixed.
683 (throw 'fixed t))
684 ;; An ortho-combination is fixed-size if at least one of its
685 ;; subwindows is fixed-size.
686 (while sub
687 (when (window-size-fixed-1 sub horizontal)
688 ;; We found a fixed-size subwindow, so WINDOW's size is
689 ;; fixed.
690 (throw 'fixed t))
691 (setq sub (window-right sub))))
692 ;; WINDOW is a live window.
693 (with-current-buffer (window-buffer window)
694 (if horizontal
695 (memq window-size-fixed '(width t))
696 (memq window-size-fixed '(height t))))))))
697
698 (defun window-size-fixed-p (&optional window horizontal)
699 "Return non-nil if WINDOW's height is fixed.
700 WINDOW can be an arbitrary window and defaults to the selected
701 window. Optional argument HORIZONTAL non-nil means return
702 non-nil if WINDOW's width is fixed.
703
704 If this function returns nil, this does not necessarily mean that
705 WINDOW can be resized in the desired direction. The functions
706 `window-resizable' and `window-resizable-p' will tell that."
707 (window-size-fixed-1
708 (window-normalize-window window) horizontal))
709
710 (defun window-min-delta-1 (window delta &optional horizontal ignore trail noup)
711 "Internal function for `window-min-delta'."
712 (if (not (window-parent window))
713 ;; If we can't go up, return zero.
714 0
715 ;; Else try to find a non-fixed-size sibling of WINDOW.
716 (let* ((parent (window-parent window))
717 (sub (window-child parent)))
718 (catch 'done
719 (if (window-combined-p sub horizontal)
720 ;; In an iso-combination throw DELTA if we find at least one
721 ;; subwindow and that subwindow is either not of fixed-size
722 ;; or we can ignore fixed-sizeness.
723 (let ((skip (eq trail 'after)))
724 (while sub
725 (cond
726 ((eq sub window)
727 (setq skip (eq trail 'before)))
728 (skip)
729 ((and (not (window-size-ignore window ignore))
730 (window-size-fixed-p sub horizontal)))
731 (t
732 ;; We found a non-fixed-size subwindow.
733 (throw 'done delta)))
734 (setq sub (window-right sub))))
735 ;; In an ortho-combination set DELTA to the minimum value by
736 ;; which other subwindows can shrink.
737 (while sub
738 (unless (eq sub window)
739 (setq delta
740 (min delta
741 (- (window-total-size sub horizontal)
742 (window-min-size sub horizontal ignore)))))
743 (setq sub (window-right sub))))
744 (if noup
745 delta
746 (window-min-delta-1 parent delta horizontal ignore trail))))))
747
748 (defun window-min-delta (&optional window horizontal ignore trail noup nodown)
749 "Return number of lines by which WINDOW can be shrunk.
750 WINDOW can be an arbitrary window and defaults to the selected
751 window. Return zero if WINDOW cannot be shrunk.
752
753 Optional argument HORIZONTAL non-nil means return number of
754 columns by which WINDOW can be shrunk.
755
756 Optional argument IGNORE non-nil means ignore any restrictions
757 imposed by fixed size windows, `window-min-height' or
758 `window-min-width' settings. IGNORE a window means ignore
759 restrictions for that window only. IGNORE equal `safe' means
760 live windows may get as small as `window-safe-min-height' lines
761 and `window-safe-min-width' columns.
762
763 Optional argument TRAIL `before' means only windows to the left
764 of or above WINDOW can be enlarged. Optional argument TRAIL
765 `after' means only windows to the right of or below WINDOW can be
766 enlarged.
767
768 Optional argument NOUP non-nil means don't go up in the window
769 tree but try to enlarge windows within WINDOW's combination only.
770
771 Optional argument NODOWN non-nil means don't check whether WINDOW
772 itself \(and its subwindows) can be shrunk; check only whether at
773 least one other windows can be enlarged appropriately."
774 (setq window (window-normalize-window window))
775 (let ((size (window-total-size window horizontal))
776 (minimum (window-min-size window horizontal ignore)))
777 (cond
778 (nodown
779 ;; If NODOWN is t, try to recover the entire size of WINDOW.
780 (window-min-delta-1 window size horizontal ignore trail noup))
781 ((= size minimum)
782 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
783 ;; there's nothing to recover.
784 0)
785 (t
786 ;; Otherwise, try to recover whatever WINDOW is larger than its
787 ;; minimum size.
788 (window-min-delta-1
789 window (- size minimum) horizontal ignore trail noup)))))
790
791 (defun window-max-delta-1 (window delta &optional horizontal ignore trail noup)
792 "Internal function of `window-max-delta'."
793 (if (not (window-parent window))
794 ;; Can't go up. Return DELTA.
795 delta
796 (let* ((parent (window-parent window))
797 (sub (window-child parent)))
798 (catch 'fixed
799 (if (window-combined-p sub horizontal)
800 ;; For an iso-combination calculate how much we can get from
801 ;; other subwindows.
802 (let ((skip (eq trail 'after)))
803 (while sub
804 (cond
805 ((eq sub window)
806 (setq skip (eq trail 'before)))
807 (skip)
808 (t
809 (setq delta
810 (+ delta
811 (- (window-total-size sub horizontal)
812 (window-min-size sub horizontal ignore))))))
813 (setq sub (window-right sub))))
814 ;; For an ortho-combination throw DELTA when at least one
815 ;; subwindow is fixed-size.
816 (while sub
817 (when (and (not (eq sub window))
818 (not (window-size-ignore sub ignore))
819 (window-size-fixed-p sub horizontal))
820 (throw 'fixed delta))
821 (setq sub (window-right sub))))
822 (if noup
823 ;; When NOUP is nil, DELTA is all we can get.
824 delta
825 ;; Else try with parent of WINDOW, passing the DELTA we
826 ;; recovered so far.
827 (window-max-delta-1 parent delta horizontal ignore trail))))))
828
829 (defun window-max-delta (&optional window horizontal ignore trail noup nodown)
830 "Return maximum number of lines WINDOW by which WINDOW can be enlarged.
831 WINDOW can be an arbitrary window and defaults to the selected
832 window. The return value is zero if WINDOW cannot be enlarged.
833
834 Optional argument HORIZONTAL non-nil means return maximum number
835 of columns by which WINDOW can be enlarged.
836
837 Optional argument IGNORE non-nil means ignore any restrictions
838 imposed by fixed size windows, `window-min-height' or
839 `window-min-width' settings. IGNORE a window means ignore
840 restrictions for that window only. IGNORE equal `safe' means
841 live windows may get as small as `window-safe-min-height' lines
842 and `window-safe-min-width' columns.
843
844 Optional argument TRAIL `before' means only windows to the left
845 of or below WINDOW can be shrunk. Optional argument TRAIL
846 `after' means only windows to the right of or above WINDOW can be
847 shrunk.
848
849 Optional argument NOUP non-nil means don't go up in the window
850 tree but try to obtain the entire space from windows within
851 WINDOW's combination.
852
853 Optional argument NODOWN non-nil means do not check whether
854 WINDOW itself \(and its subwindows) can be enlarged; check only
855 whether other windows can be shrunk appropriately."
856 (setq window (window-normalize-window window))
857 (if (and (not (window-size-ignore window ignore))
858 (not nodown) (window-size-fixed-p window horizontal))
859 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
860 ;; size.
861 0
862 ;; WINDOW has no fixed size.
863 (window-max-delta-1 window 0 horizontal ignore trail noup)))
864
865 ;; Make NOUP also inhibit the min-size check.
866 (defun window-resizable (window delta &optional horizontal ignore trail noup nodown)
867 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
868 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
869 can be resized horizontally by DELTA columns. A return value of
870 zero means that WINDOW is not resizable.
871
872 DELTA positive means WINDOW shall be enlarged by DELTA lines or
873 columns. If WINDOW cannot be enlarged by DELTA lines or columns
874 return the maximum value in the range 0..DELTA by which WINDOW
875 can be enlarged.
876
877 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
878 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
879 return the minimum value in the range DELTA..0 that can be used
880 for shrinking WINDOW.
881
882 Optional argument IGNORE non-nil means ignore any restrictions
883 imposed by fixed size windows, `window-min-height' or
884 `window-min-width' settings. IGNORE a window means ignore
885 restrictions for that window only. IGNORE equal `safe' means
886 live windows may get as small as `window-safe-min-height' lines
887 and `window-safe-min-width' columns.
888
889 Optional argument TRAIL `before' means only windows to the left
890 of or below WINDOW can be shrunk. Optional argument TRAIL
891 `after' means only windows to the right of or above WINDOW can be
892 shrunk.
893
894 Optional argument NOUP non-nil means don't go up in the window
895 tree but try to distribute the space among the other windows
896 within WINDOW's combination.
897
898 Optional argument NODOWN non-nil means don't check whether WINDOW
899 and its subwindows can be resized."
900 (setq window (window-normalize-window window))
901 (cond
902 ((< delta 0)
903 (max (- (window-min-delta window horizontal ignore trail noup nodown))
904 delta))
905 ((> delta 0)
906 (min (window-max-delta window horizontal ignore trail noup nodown)
907 delta))
908 (t 0)))
909
910 (defun window-resizable-p (window delta &optional horizontal ignore trail noup nodown)
911 "Return t if WINDOW can be resized vertically by DELTA lines.
912 For the meaning of the arguments of this function see the
913 doc-string of `window-resizable'."
914 (setq window (window-normalize-window window))
915 (if (> delta 0)
916 (>= (window-resizable window delta horizontal ignore trail noup nodown)
917 delta)
918 (<= (window-resizable window delta horizontal ignore trail noup nodown)
919 delta)))
920
921 (defsubst window-total-height (&optional window)
922 "Return the total number of lines of WINDOW.
923 WINDOW can be any window and defaults to the selected one. The
924 return value includes WINDOW's mode line and header line, if any.
925 If WINDOW is internal the return value is the sum of the total
926 number of lines of WINDOW's child windows if these are vertically
927 combined and the height of WINDOW's first child otherwise.
928
929 Note: This function does not take into account the value of
930 `line-spacing' when calculating the number of lines in WINDOW."
931 (window-total-size window))
932
933 ;; Eventually we should make `window-height' obsolete.
934 (defalias 'window-height 'window-total-height)
935
936 ;; See discussion in bug#4543.
937 (defsubst window-full-height-p (&optional window)
938 "Return t if WINDOW is as high as the containing frame.
939 More precisely, return t if and only if the total height of
940 WINDOW equals the total height of the root window of WINDOW's
941 frame. WINDOW can be any window and defaults to the selected
942 one."
943 (setq window (window-normalize-window window))
944 (= (window-total-size window)
945 (window-total-size (frame-root-window window))))
946
947 (defsubst window-total-width (&optional window)
948 "Return the total number of columns of WINDOW.
949 WINDOW can be any window and defaults to the selected one. The
950 return value includes any vertical dividers or scrollbars of
951 WINDOW. If WINDOW is internal, the return value is the sum of
952 the total number of columns of WINDOW's child windows if these
953 are horizontally combined and the width of WINDOW's first child
954 otherwise."
955 (window-total-size window t))
956
957 (defsubst window-full-width-p (&optional window)
958 "Return t if WINDOW is as wide as the containing frame.
959 More precisely, return t if and only if the total width of WINDOW
960 equals the total width of the root window of WINDOW's frame.
961 WINDOW can be any window and defaults to the selected one."
962 (setq window (window-normalize-window window))
963 (= (window-total-size window t)
964 (window-total-size (frame-root-window window) t)))
965
966 (defsubst window-body-height (&optional window)
967 "Return the number of lines of WINDOW's body.
968 WINDOW must be a live window and defaults to the selected one.
969
970 The return value does not include WINDOW's mode line and header
971 line, if any. If a line at the bottom of the window is only
972 partially visible, that line is included in the return value. If
973 you do not want to include a partially visible bottom line in the
974 return value, use `window-text-height' instead.
975
976 Note that the return value is measured in canonical units, i.e. for
977 the default frame's face. If the window shows some characters with
978 non-default face, e.g., if the font of some characters is larger or
979 smaller than the default font, the value returned by this function
980 will not match the actual number of lines shown in the window. To
981 get the actual number of lines, use `posn-at-point'."
982 (window-body-size window))
983
984 (defsubst window-body-width (&optional window)
985 "Return the number of columns of WINDOW's body.
986 WINDOW must be a live window and defaults to the selected one.
987
988 The return value does not include any vertical dividers or scroll
989 bars owned by WINDOW. On a window-system the return value does
990 not include the number of columns used for WINDOW's fringes or
991 display margins either.
992
993 Note that the return value is measured in canonical units, i.e. for
994 the default frame's face. If the window shows some characters with
995 non-default face, e.g., if the font of some characters is larger or
996 smaller than the default font, the value returned by this function
997 will not match the actual number of characters per line shown in the
998 window. To get the actual number of columns, use `posn-at-point'."
999 (window-body-size window t))
1000
1001 ;; Eventually we should make `window-height' obsolete.
1002 (defalias 'window-width 'window-body-width)
1003
1004 (defun window-current-scroll-bars (&optional window)
1005 "Return the current scroll bar settings for WINDOW.
1006 WINDOW must be a live window and defaults to the selected one.
1007
1008 The return value is a cons cell (VERTICAL . HORIZONTAL) where
1009 VERTICAL specifies the current location of the vertical scroll
1010 bars (`left', `right', or nil), and HORIZONTAL specifies the
1011 current location of the horizontal scroll bars (`top', `bottom',
1012 or nil).
1013
1014 Unlike `window-scroll-bars', this function reports the scroll bar
1015 type actually used, once frame defaults and `scroll-bar-mode' are
1016 taken into account."
1017 (setq window (window-normalize-window window t))
1018 (let ((vert (nth 2 (window-scroll-bars window)))
1019 (hor nil))
1020 (when (or (eq vert t) (eq hor t))
1021 (let ((fcsb (frame-current-scroll-bars (window-frame window))))
1022 (if (eq vert t)
1023 (setq vert (car fcsb)))
1024 (if (eq hor t)
1025 (setq hor (cdr fcsb)))))
1026 (cons vert hor)))
1027
1028 (defun walk-windows (proc &optional minibuf all-frames)
1029 "Cycle through all live windows, calling PROC for each one.
1030 PROC must specify a function with a window as its sole argument.
1031 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1032 windows to include in the walk.
1033
1034 MINIBUF t means include the minibuffer window even if the
1035 minibuffer is not active. MINIBUF nil or omitted means include
1036 the minibuffer window only if the minibuffer is active. Any
1037 other value means do not include the minibuffer window even if
1038 the minibuffer is active.
1039
1040 ALL-FRAMES nil or omitted means consider all windows on the
1041 selected frame, plus the minibuffer window if specified by the
1042 MINIBUF argument. If the minibuffer counts, consider all windows
1043 on all frames that share that minibuffer too. The following
1044 non-nil values of ALL-FRAMES have special meanings:
1045
1046 - t means consider all windows on all existing frames.
1047
1048 - `visible' means consider all windows on all visible frames on
1049 the current terminal.
1050
1051 - 0 (the number zero) means consider all windows on all visible
1052 and iconified frames on the current terminal.
1053
1054 - A frame means consider all windows on that frame only.
1055
1056 Anything else means consider all windows on the selected frame
1057 and no others.
1058
1059 This function changes neither the order of recently selected
1060 windows nor the buffer list."
1061 ;; If we start from the minibuffer window, don't fail to come
1062 ;; back to it.
1063 (when (window-minibuffer-p (selected-window))
1064 (setq minibuf t))
1065 ;; Make sure to not mess up the order of recently selected
1066 ;; windows. Use `save-selected-window' and `select-window'
1067 ;; with second argument non-nil for this purpose.
1068 (save-selected-window
1069 (when (framep all-frames)
1070 (select-window (frame-first-window all-frames) 'norecord))
1071 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
1072 (funcall proc walk-windows-window))))
1073
1074 (defun window-point-1 (&optional window)
1075 "Return value of WINDOW's point.
1076 WINDOW can be any live window and defaults to the selected one.
1077
1078 This function is like `window-point' with one exception: If
1079 WINDOW is selected, it returns the value of `point' of WINDOW's
1080 buffer regardless of whether that buffer is current or not."
1081 (setq window (window-normalize-window window t))
1082 (if (eq window (selected-window))
1083 (with-current-buffer (window-buffer window)
1084 (point))
1085 (window-point window)))
1086
1087 (defun set-window-point-1 (window pos)
1088 "Set value of WINDOW's point to POS.
1089 WINDOW can be any live window and defaults to the selected one.
1090
1091 This function is like `set-window-point' with one exception: If
1092 WINDOW is selected, it moves `point' of WINDOW's buffer to POS
1093 regardless of whether that buffer is current or not."
1094 (setq window (window-normalize-window window t))
1095 (if (eq window (selected-window))
1096 (with-current-buffer (window-buffer window)
1097 (goto-char pos))
1098 (set-window-point window pos)))
1099
1100 (defun window-at-side-p (&optional window side)
1101 "Return t if WINDOW is at SIDE of its containing frame.
1102 WINDOW can be any window and defaults to the selected one. SIDE
1103 can be any of the symbols `left', `top', `right' or `bottom'.
1104 The default value nil is handled like `bottom'."
1105 (setq window (window-normalize-window window))
1106 (let ((edge
1107 (cond
1108 ((eq side 'left) 0)
1109 ((eq side 'top) 1)
1110 ((eq side 'right) 2)
1111 ((memq side '(bottom nil)) 3))))
1112 (= (nth edge (window-edges window))
1113 (nth edge (window-edges (frame-root-window window))))))
1114
1115 (defun windows-at-side (&optional frame side)
1116 "Return list of all windows on SIDE of FRAME.
1117 FRAME must be a live frame and defaults to the selected frame.
1118 SIDE can be any of the symbols `left', `top', `right' or
1119 `bottom'. The default value nil is handled like `bottom'."
1120 (setq frame (window-normalize-frame frame))
1121 (let (windows)
1122 (walk-window-tree
1123 (lambda (window)
1124 (when (window-at-side-p window side)
1125 (setq windows (cons window windows))))
1126 frame)
1127 (nreverse windows)))
1128
1129 (defun window-in-direction-2 (window posn &optional horizontal)
1130 "Support function for `window-in-direction'."
1131 (if horizontal
1132 (let ((top (window-top-line window)))
1133 (if (> top posn)
1134 (- top posn)
1135 (- posn top (window-total-height window))))
1136 (let ((left (window-left-column window)))
1137 (if (> left posn)
1138 (- left posn)
1139 (- posn left (window-total-width window))))))
1140
1141 (defun window-in-direction (direction &optional window ignore)
1142 "Return window in DIRECTION as seen from WINDOW.
1143 DIRECTION must be one of `above', `below', `left' or `right'.
1144 WINDOW must be a live window and defaults to the selected one.
1145 IGNORE, when non-nil means a window can be returned even if its
1146 `no-other-window' parameter is non-nil."
1147 (setq window (window-normalize-window window t))
1148 (unless (memq direction '(above below left right))
1149 (error "Wrong direction %s" direction))
1150 (let* ((frame (window-frame window))
1151 (hor (memq direction '(left right)))
1152 (first (if hor
1153 (window-left-column window)
1154 (window-top-line window)))
1155 (last (+ first (if hor
1156 (window-total-width window)
1157 (window-total-height window))))
1158 (posn-cons (nth 6 (posn-at-point (window-point-1 window) window)))
1159 ;; The column / row value of `posn-at-point' can be nil for the
1160 ;; mini-window, guard against that.
1161 (posn (if hor
1162 (+ (or (cdr posn-cons) 1) (window-top-line window))
1163 (+ (or (car posn-cons) 1) (window-left-column window))))
1164 (best-edge
1165 (cond
1166 ((eq direction 'below) (frame-height frame))
1167 ((eq direction 'right) (frame-width frame))
1168 (t -1)))
1169 (best-edge-2 best-edge)
1170 (best-diff-2 (if hor (frame-height frame) (frame-width frame)))
1171 best best-2 best-diff-2-new)
1172 (walk-window-tree
1173 (lambda (w)
1174 (let* ((w-top (window-top-line w))
1175 (w-left (window-left-column w)))
1176 (cond
1177 ((or (eq window w)
1178 ;; Ignore ourselves.
1179 (and (window-parameter w 'no-other-window)
1180 ;; Ignore W unless IGNORE is non-nil.
1181 (not ignore))))
1182 (hor
1183 (cond
1184 ((and (<= w-top posn)
1185 (< posn (+ w-top (window-total-height w))))
1186 ;; W is to the left or right of WINDOW and covers POSN.
1187 (when (or (and (eq direction 'left)
1188 (<= w-left first) (> w-left best-edge))
1189 (and (eq direction 'right)
1190 (>= w-left last) (< w-left best-edge)))
1191 (setq best-edge w-left)
1192 (setq best w)))
1193 ((and (or (and (eq direction 'left)
1194 (<= (+ w-left (window-total-width w)) first))
1195 (and (eq direction 'right) (<= last w-left)))
1196 ;; W is to the left or right of WINDOW but does not
1197 ;; cover POSN.
1198 (setq best-diff-2-new
1199 (window-in-direction-2 w posn hor))
1200 (or (< best-diff-2-new best-diff-2)
1201 (and (= best-diff-2-new best-diff-2)
1202 (if (eq direction 'left)
1203 (> w-left best-edge-2)
1204 (< w-left best-edge-2)))))
1205 (setq best-edge-2 w-left)
1206 (setq best-diff-2 best-diff-2-new)
1207 (setq best-2 w))))
1208 (t
1209 (cond
1210 ((and (<= w-left posn)
1211 (< posn (+ w-left (window-total-width w))))
1212 ;; W is above or below WINDOW and covers POSN.
1213 (when (or (and (eq direction 'above)
1214 (<= w-top first) (> w-top best-edge))
1215 (and (eq direction 'below)
1216 (>= w-top first) (< w-top best-edge)))
1217 (setq best-edge w-top)
1218 (setq best w)))
1219 ((and (or (and (eq direction 'above)
1220 (<= (+ w-top (window-total-height w)) first))
1221 (and (eq direction 'below) (<= last w-top)))
1222 ;; W is above or below WINDOW but does not cover POSN.
1223 (setq best-diff-2-new
1224 (window-in-direction-2 w posn hor))
1225 (or (< best-diff-2-new best-diff-2)
1226 (and (= best-diff-2-new best-diff-2)
1227 (if (eq direction 'above)
1228 (> w-top best-edge-2)
1229 (< w-top best-edge-2)))))
1230 (setq best-edge-2 w-top)
1231 (setq best-diff-2 best-diff-2-new)
1232 (setq best-2 w)))))))
1233 (window-frame window))
1234 (or best best-2)))
1235
1236 (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
1237 "Return a live window satisfying PREDICATE.
1238 More precisely, cycle through all windows calling the function
1239 PREDICATE on each one of them with the window as its sole
1240 argument. Return the first window for which PREDICATE returns
1241 non-nil. Windows are scanned starting with the window following
1242 the selcted window. If no window satisfies PREDICATE, return
1243 DEFAULT.
1244
1245 MINIBUF t means include the minibuffer window even if the
1246 minibuffer is not active. MINIBUF nil or omitted means include
1247 the minibuffer window only if the minibuffer is active. Any
1248 other value means do not include the minibuffer window even if
1249 the minibuffer is active.
1250
1251 ALL-FRAMES nil or omitted means consider all windows on the selected
1252 frame, plus the minibuffer window if specified by the MINIBUF
1253 argument. If the minibuffer counts, consider all windows on all
1254 frames that share that minibuffer too. The following non-nil
1255 values of ALL-FRAMES have special meanings:
1256
1257 - t means consider all windows on all existing frames.
1258
1259 - `visible' means consider all windows on all visible frames on
1260 the current terminal.
1261
1262 - 0 (the number zero) means consider all windows on all visible
1263 and iconified frames on the current terminal.
1264
1265 - A frame means consider all windows on that frame only.
1266
1267 Anything else means consider all windows on the selected frame
1268 and no others."
1269 (catch 'found
1270 (dolist (window (window-list-1
1271 (next-window nil minibuf all-frames)
1272 minibuf all-frames))
1273 (when (funcall predicate window)
1274 (throw 'found window)))
1275 default))
1276
1277 (defalias 'some-window 'get-window-with-predicate)
1278
1279 (defun get-lru-window (&optional all-frames dedicated)
1280 "Return the least recently used window on frames specified by ALL-FRAMES.
1281 Return a full-width window if possible. A minibuffer window is
1282 never a candidate. A dedicated window is never a candidate
1283 unless DEDICATED is non-nil, so if all windows are dedicated, the
1284 value is nil. Avoid returning the selected window if possible.
1285
1286 The following non-nil values of the optional argument ALL-FRAMES
1287 have special meanings:
1288
1289 - t means consider all windows on all existing frames.
1290
1291 - `visible' means consider all windows on all visible frames on
1292 the current terminal.
1293
1294 - 0 (the number zero) means consider all windows on all visible
1295 and iconified frames on the current terminal.
1296
1297 - A frame means consider all windows on that frame only.
1298
1299 Any other value of ALL-FRAMES means consider all windows on the
1300 selected frame and no others."
1301 (let (best-window best-time second-best-window second-best-time time)
1302 (dolist (window (window-list-1 nil 'nomini all-frames))
1303 (when (or dedicated (not (window-dedicated-p window)))
1304 (setq time (window-use-time window))
1305 (if (or (eq window (selected-window))
1306 (not (window-full-width-p window)))
1307 (when (or (not second-best-time) (< time second-best-time))
1308 (setq second-best-time time)
1309 (setq second-best-window window))
1310 (when (or (not best-time) (< time best-time))
1311 (setq best-time time)
1312 (setq best-window window)))))
1313 (or best-window second-best-window)))
1314
1315 (defun get-mru-window (&optional all-frames)
1316 "Return the most recently used window on frames specified by ALL-FRAMES.
1317 Do not return a minibuffer window.
1318
1319 The following non-nil values of the optional argument ALL-FRAMES
1320 have special meanings:
1321
1322 - t means consider all windows on all existing frames.
1323
1324 - `visible' means consider all windows on all visible frames on
1325 the current terminal.
1326
1327 - 0 (the number zero) means consider all windows on all visible
1328 and iconified frames on the current terminal.
1329
1330 - A frame means consider all windows on that frame only.
1331
1332 Any other value of ALL-FRAMES means consider all windows on the
1333 selected frame and no others."
1334 (let (best-window best-time time)
1335 (dolist (window (window-list-1 nil 'nomini all-frames))
1336 (setq time (window-use-time window))
1337 (when (or (not best-time) (> time best-time))
1338 (setq best-time time)
1339 (setq best-window window)))
1340 best-window))
1341
1342 (defun get-largest-window (&optional all-frames dedicated)
1343 "Return the largest window on frames specified by ALL-FRAMES.
1344 A minibuffer window is never a candidate. A dedicated window is
1345 never a candidate unless DEDICATED is non-nil, so if all windows
1346 are dedicated, the value is nil.
1347
1348 The following non-nil values of the optional argument ALL-FRAMES
1349 have special meanings:
1350
1351 - t means consider all windows on all existing frames.
1352
1353 - `visible' means consider all windows on all visible frames on
1354 the current terminal.
1355
1356 - 0 (the number zero) means consider all windows on all visible
1357 and iconified frames on the current terminal.
1358
1359 - A frame means consider all windows on that frame only.
1360
1361 Any other value of ALL-FRAMES means consider all windows on the
1362 selected frame and no others."
1363 (let ((best-size 0)
1364 best-window size)
1365 (dolist (window (window-list-1 nil 'nomini all-frames))
1366 (when (or dedicated (not (window-dedicated-p window)))
1367 (setq size (* (window-total-size window)
1368 (window-total-size window t)))
1369 (when (> size best-size)
1370 (setq best-size size)
1371 (setq best-window window))))
1372 best-window))
1373
1374 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
1375 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
1376 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
1377 and defaults to the current buffer. Windows are scanned starting
1378 with the selected window.
1379
1380 MINIBUF t means include the minibuffer window even if the
1381 minibuffer is not active. MINIBUF nil or omitted means include
1382 the minibuffer window only if the minibuffer is active. Any
1383 other value means do not include the minibuffer window even if
1384 the minibuffer is active.
1385
1386 ALL-FRAMES nil or omitted means consider all windows on the
1387 selected frame, plus the minibuffer window if specified by the
1388 MINIBUF argument. If the minibuffer counts, consider all windows
1389 on all frames that share that minibuffer too. The following
1390 non-nil values of ALL-FRAMES have special meanings:
1391
1392 - t means consider all windows on all existing frames.
1393
1394 - `visible' means consider all windows on all visible frames on
1395 the current terminal.
1396
1397 - 0 (the number zero) means consider all windows on all visible
1398 and iconified frames on the current terminal.
1399
1400 - A frame means consider all windows on that frame only.
1401
1402 Anything else means consider all windows on the selected frame
1403 and no others."
1404 (let ((buffer (window-normalize-buffer buffer-or-name))
1405 windows)
1406 (dolist (window (window-list-1 (selected-window) minibuf all-frames))
1407 (when (eq (window-buffer window) buffer)
1408 (setq windows (cons window windows))))
1409 (nreverse windows)))
1410
1411 (defun minibuffer-window-active-p (window)
1412 "Return t if WINDOW is the currently active minibuffer window."
1413 (eq window (active-minibuffer-window)))
1414
1415 (defun count-windows (&optional minibuf)
1416 "Return the number of live windows on the selected frame.
1417 The optional argument MINIBUF specifies whether the minibuffer
1418 window shall be counted. See `walk-windows' for the precise
1419 meaning of this argument."
1420 (length (window-list-1 nil minibuf)))
1421 \f
1422 ;;; Resizing windows.
1423 (defun window--resize-reset (&optional frame horizontal)
1424 "Reset resize values for all windows on FRAME.
1425 FRAME defaults to the selected frame.
1426
1427 This function stores the current value of `window-total-size' applied
1428 with argument HORIZONTAL in the new total size of all windows on
1429 FRAME. It also resets the new normal size of each of these
1430 windows."
1431 (window--resize-reset-1
1432 (frame-root-window (window-normalize-frame frame)) horizontal))
1433
1434 (defun window--resize-reset-1 (window horizontal)
1435 "Internal function of `window--resize-reset'."
1436 ;; Register old size in the new total size.
1437 (set-window-new-total window (window-total-size window horizontal))
1438 ;; Reset new normal size.
1439 (set-window-new-normal window)
1440 (when (window-child window)
1441 (window--resize-reset-1 (window-child window) horizontal))
1442 (when (window-right window)
1443 (window--resize-reset-1 (window-right window) horizontal)))
1444
1445 ;; The following routine is used to manually resize the minibuffer
1446 ;; window and is currently used, for example, by ispell.el.
1447 (defun window--resize-mini-window (window delta)
1448 "Resize minibuffer window WINDOW by DELTA lines.
1449 If WINDOW cannot be resized by DELTA lines make it as large \(or
1450 as small) as possible but don't signal an error."
1451 (when (window-minibuffer-p window)
1452 (let* ((frame (window-frame window))
1453 (root (frame-root-window frame))
1454 (height (window-total-size window))
1455 (min-delta
1456 (- (window-total-size root)
1457 (window-min-size root))))
1458 ;; Sanitize DELTA.
1459 (cond
1460 ((<= (+ height delta) 0)
1461 (setq delta (- (- height 1))))
1462 ((> delta min-delta)
1463 (setq delta min-delta)))
1464
1465 ;; Resize now.
1466 (window--resize-reset frame)
1467 ;; Ideally we should be able to resize just the last subwindow of
1468 ;; root here. See the comment in `resize-root-window-vertically'
1469 ;; for why we do not do that.
1470 (window--resize-this-window root (- delta) nil nil t)
1471 (set-window-new-total window (+ height delta))
1472 ;; The following routine catches the case where we want to resize
1473 ;; a minibuffer-only frame.
1474 (resize-mini-window-internal window))))
1475
1476 (defun window-resize (window delta &optional horizontal ignore)
1477 "Resize WINDOW vertically by DELTA lines.
1478 WINDOW can be an arbitrary window and defaults to the selected
1479 one. An attempt to resize the root window of a frame will raise
1480 an error though.
1481
1482 DELTA a positive number means WINDOW shall be enlarged by DELTA
1483 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
1484 lines.
1485
1486 Optional argument HORIZONTAL non-nil means resize WINDOW
1487 horizontally by DELTA columns. In this case a positive DELTA
1488 means enlarge WINDOW by DELTA columns. DELTA negative means
1489 WINDOW shall be shrunk by -DELTA columns.
1490
1491 Optional argument IGNORE non-nil means ignore any restrictions
1492 imposed by fixed size windows, `window-min-height' or
1493 `window-min-width' settings. IGNORE any window means ignore
1494 restrictions for that window only. IGNORE equal `safe' means
1495 live windows may get as small as `window-safe-min-height' lines
1496 and `window-safe-min-width' columns.
1497
1498 This function resizes other windows proportionally and never
1499 deletes any windows. If you want to move only the low (right)
1500 edge of WINDOW consider using `adjust-window-trailing-edge'
1501 instead."
1502 (setq window (window-normalize-window window))
1503 (let* ((frame (window-frame window))
1504 sibling)
1505 (cond
1506 ((eq window (frame-root-window frame))
1507 (error "Cannot resize the root window of a frame"))
1508 ((window-minibuffer-p window)
1509 (window--resize-mini-window window delta))
1510 ((window-resizable-p window delta horizontal ignore)
1511 (window--resize-reset frame horizontal)
1512 (window--resize-this-window window delta horizontal ignore t)
1513 (if (and (not (window-splits window))
1514 (window-combined-p window horizontal)
1515 (setq sibling (or (window-right window) (window-left window)))
1516 (window-sizable-p sibling (- delta) horizontal ignore))
1517 ;; If window-splits returns nil for WINDOW, WINDOW is part of
1518 ;; an iso-combination, and WINDOW's neighboring right or left
1519 ;; sibling can be resized as requested, resize that sibling.
1520 (let ((normal-delta
1521 (/ (float delta)
1522 (window-total-size (window-parent window) horizontal))))
1523 (window--resize-this-window sibling (- delta) horizontal nil t)
1524 (set-window-new-normal
1525 window (+ (window-normal-size window horizontal)
1526 normal-delta))
1527 (set-window-new-normal
1528 sibling (- (window-normal-size sibling horizontal)
1529 normal-delta)))
1530 ;; Otherwise, resize all other windows in the same combination.
1531 (window--resize-siblings window delta horizontal ignore))
1532 (window-resize-apply frame horizontal))
1533 (t
1534 (error "Cannot resize window %s" window)))))
1535
1536 (defsubst window--resize-subwindows-skip-p (window)
1537 "Return non-nil if WINDOW shall be skipped by resizing routines."
1538 (memq (window-new-normal window) '(ignore stuck skip)))
1539
1540 (defun window--resize-subwindows-normal (parent horizontal window this-delta &optional trail other-delta)
1541 "Set the new normal height of subwindows of window PARENT.
1542 HORIZONTAL non-nil means set the new normal width of these
1543 windows. WINDOW specifies a subwindow of PARENT that has been
1544 resized by THIS-DELTA lines \(columns).
1545
1546 Optional argument TRAIL either 'before or 'after means set values
1547 for windows before or after WINDOW only. Optional argument
1548 OTHER-DELTA a number specifies that this many lines \(columns)
1549 have been obtained from \(or returned to) an ancestor window of
1550 PARENT in order to resize WINDOW."
1551 (let* ((delta-normal
1552 (if (and (= (- this-delta) (window-total-size window horizontal))
1553 (zerop other-delta))
1554 ;; When WINDOW gets deleted and we can return its entire
1555 ;; space to its siblings, use WINDOW's normal size as the
1556 ;; normal delta.
1557 (- (window-normal-size window horizontal))
1558 ;; In any other case calculate the normal delta from the
1559 ;; relation of THIS-DELTA to the total size of PARENT.
1560 (/ (float this-delta) (window-total-size parent horizontal))))
1561 (sub (window-child parent))
1562 (parent-normal 0.0)
1563 (skip (eq trail 'after)))
1564
1565 ;; Set parent-normal to the sum of the normal sizes of all
1566 ;; subwindows of PARENT that shall be resized, excluding only WINDOW
1567 ;; and any windows specified by the optional TRAIL argument.
1568 (while sub
1569 (cond
1570 ((eq sub window)
1571 (setq skip (eq trail 'before)))
1572 (skip)
1573 (t
1574 (setq parent-normal
1575 (+ parent-normal (window-normal-size sub horizontal)))))
1576 (setq sub (window-right sub)))
1577
1578 ;; Set the new normal size of all subwindows of PARENT from what
1579 ;; they should have contributed for recovering THIS-DELTA lines
1580 ;; (columns).
1581 (setq sub (window-child parent))
1582 (setq skip (eq trail 'after))
1583 (while sub
1584 (cond
1585 ((eq sub window)
1586 (setq skip (eq trail 'before)))
1587 (skip)
1588 (t
1589 (let ((old-normal (window-normal-size sub horizontal)))
1590 (set-window-new-normal
1591 sub (min 1.0 ; Don't get larger than 1.
1592 (max (- old-normal
1593 (* (/ old-normal parent-normal)
1594 delta-normal))
1595 ;; Don't drop below 0.
1596 0.0))))))
1597 (setq sub (window-right sub)))
1598
1599 (when (numberp other-delta)
1600 ;; Set the new normal size of windows from what they should have
1601 ;; contributed for recovering OTHER-DELTA lines (columns).
1602 (setq delta-normal (/ (float (window-total-size parent horizontal))
1603 (+ (window-total-size parent horizontal)
1604 other-delta)))
1605 (setq sub (window-child parent))
1606 (setq skip (eq trail 'after))
1607 (while sub
1608 (cond
1609 ((eq sub window)
1610 (setq skip (eq trail 'before)))
1611 (skip)
1612 (t
1613 (set-window-new-normal
1614 sub (min 1.0 ; Don't get larger than 1.
1615 (max (* (window-new-normal sub) delta-normal)
1616 ;; Don't drop below 0.
1617 0.0)))))
1618 (setq sub (window-right sub))))
1619
1620 ;; Set the new normal size of WINDOW to what is left by the sum of
1621 ;; the normal sizes of its siblings.
1622 (set-window-new-normal
1623 window
1624 (let ((sum 0))
1625 (setq sub (window-child parent))
1626 (while sub
1627 (cond
1628 ((eq sub window))
1629 ((not (numberp (window-new-normal sub)))
1630 (setq sum (+ sum (window-normal-size sub horizontal))))
1631 (t
1632 (setq sum (+ sum (window-new-normal sub)))))
1633 (setq sub (window-right sub)))
1634 ;; Don't get larger than 1 or smaller than 0.
1635 (min 1.0 (max (- 1.0 sum) 0.0))))))
1636
1637 (defun window--resize-subwindows (parent delta &optional horizontal window ignore trail edge)
1638 "Resize subwindows of window PARENT vertically by DELTA lines.
1639 PARENT must be a vertically combined internal window.
1640
1641 Optional argument HORIZONTAL non-nil means resize subwindows of
1642 PARENT horizontally by DELTA columns. In this case PARENT must
1643 be a horizontally combined internal window.
1644
1645 WINDOW, if specified, must denote a child window of PARENT that
1646 is resized by DELTA lines.
1647
1648 Optional argument IGNORE non-nil means ignore any restrictions
1649 imposed by fixed size windows, `window-min-height' or
1650 `window-min-width' settings. IGNORE equal `safe' means live
1651 windows may get as small as `window-safe-min-height' lines and
1652 `window-safe-min-width' columns. IGNORE any window means ignore
1653 restrictions for that window only.
1654
1655 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
1656 of windows that shall be resized. If TRAIL equals `before',
1657 resize only windows on the left or above EDGE. If TRAIL equals
1658 `after', resize only windows on the right or below EDGE. Also,
1659 preferably only resize windows adjacent to EDGE.
1660
1661 Return the symbol `normalized' if new normal sizes have been
1662 already set by this routine."
1663 (let* ((first (window-child parent))
1664 (sub first)
1665 (parent-total (+ (window-total-size parent horizontal) delta))
1666 best-window best-value)
1667
1668 (if (and edge (memq trail '(before after))
1669 (progn
1670 (setq sub first)
1671 (while (and (window-right sub)
1672 (or (and (eq trail 'before)
1673 (not (window--resize-subwindows-skip-p
1674 (window-right sub))))
1675 (and (eq trail 'after)
1676 (window--resize-subwindows-skip-p sub))))
1677 (setq sub (window-right sub)))
1678 sub)
1679 (if horizontal
1680 (if (eq trail 'before)
1681 (= (+ (window-left-column sub)
1682 (window-total-size sub t))
1683 edge)
1684 (= (window-left-column sub) edge))
1685 (if (eq trail 'before)
1686 (= (+ (window-top-line sub)
1687 (window-total-size sub))
1688 edge)
1689 (= (window-top-line sub) edge)))
1690 (window-sizable-p sub delta horizontal ignore))
1691 ;; Resize only windows adjacent to EDGE.
1692 (progn
1693 (window--resize-this-window
1694 sub delta horizontal ignore t trail edge)
1695 (if (and window (eq (window-parent sub) parent))
1696 (progn
1697 ;; Assign new normal sizes.
1698 (set-window-new-normal
1699 sub (/ (float (window-new-total sub)) parent-total))
1700 (set-window-new-normal
1701 window (- (window-normal-size window horizontal)
1702 (- (window-new-normal sub)
1703 (window-normal-size sub horizontal)))))
1704 (window--resize-subwindows-normal
1705 parent horizontal sub 0 trail delta))
1706 ;; Return 'normalized to notify `window--resize-siblings' that
1707 ;; normal sizes have been already set.
1708 'normalized)
1709 ;; Resize all windows proportionally.
1710 (setq sub first)
1711 (while sub
1712 (cond
1713 ((or (window--resize-subwindows-skip-p sub)
1714 ;; Ignore windows to skip and fixed-size subwindows - in
1715 ;; the latter case make it a window to skip.
1716 (and (not ignore)
1717 (window-size-fixed-p sub horizontal)
1718 (set-window-new-normal sub 'ignore))))
1719 ((< delta 0)
1720 ;; When shrinking store the number of lines/cols we can get
1721 ;; from this window here together with the total/normal size
1722 ;; factor.
1723 (set-window-new-normal
1724 sub
1725 (cons
1726 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
1727 (window-min-delta sub horizontal ignore trail t) ; t)
1728 (- (/ (float (window-total-size sub horizontal))
1729 parent-total)
1730 (window-normal-size sub horizontal)))))
1731 ((> delta 0)
1732 ;; When enlarging store the total/normal size factor only
1733 (set-window-new-normal
1734 sub
1735 (- (/ (float (window-total-size sub horizontal))
1736 parent-total)
1737 (window-normal-size sub horizontal)))))
1738
1739 (setq sub (window-right sub)))
1740
1741 (cond
1742 ((< delta 0)
1743 ;; Shrink windows by delta.
1744 (setq best-window t)
1745 (while (and best-window (not (zerop delta)))
1746 (setq sub first)
1747 (setq best-window nil)
1748 (setq best-value most-negative-fixnum)
1749 (while sub
1750 (when (and (consp (window-new-normal sub))
1751 (not (zerop (car (window-new-normal sub))))
1752 (> (cdr (window-new-normal sub)) best-value))
1753 (setq best-window sub)
1754 (setq best-value (cdr (window-new-normal sub))))
1755
1756 (setq sub (window-right sub)))
1757
1758 (when best-window
1759 (setq delta (1+ delta)))
1760 (set-window-new-total best-window -1 t)
1761 (set-window-new-normal
1762 best-window
1763 (if (= (car (window-new-normal best-window)) 1)
1764 'skip ; We can't shrink best-window any further.
1765 (cons (1- (car (window-new-normal best-window)))
1766 (- (/ (float (window-new-total best-window))
1767 parent-total)
1768 (window-normal-size best-window horizontal)))))))
1769 ((> delta 0)
1770 ;; Enlarge windows by delta.
1771 (setq best-window t)
1772 (while (and best-window (not (zerop delta)))
1773 (setq sub first)
1774 (setq best-window nil)
1775 (setq best-value most-positive-fixnum)
1776 (while sub
1777 (when (and (numberp (window-new-normal sub))
1778 (< (window-new-normal sub) best-value))
1779 (setq best-window sub)
1780 (setq best-value (window-new-normal sub)))
1781
1782 (setq sub (window-right sub)))
1783
1784 (when best-window
1785 (setq delta (1- delta)))
1786 (set-window-new-total best-window 1 t)
1787 (set-window-new-normal
1788 best-window
1789 (- (/ (float (window-new-total best-window))
1790 parent-total)
1791 (window-normal-size best-window horizontal))))))
1792
1793 (when best-window
1794 (setq sub first)
1795 (while sub
1796 (when (or (consp (window-new-normal sub))
1797 (numberp (window-new-normal sub)))
1798 ;; Reset new normal size fields so `window-resize-apply'
1799 ;; won't use them to apply new sizes.
1800 (set-window-new-normal sub))
1801
1802 (unless (eq (window-new-normal sub) 'ignore)
1803 ;; Resize this subwindow's subwindows (back-engineering
1804 ;; delta from sub's old and new total sizes).
1805 (let ((delta (- (window-new-total sub)
1806 (window-total-size sub horizontal))))
1807 (unless (and (zerop delta) (not trail))
1808 ;; For the TRAIL non-nil case we have to resize SUB
1809 ;; recursively even if it's size does not change.
1810 (window--resize-this-window
1811 sub delta horizontal ignore nil trail edge))))
1812 (setq sub (window-right sub)))))))
1813
1814 (defun window--resize-siblings (window delta &optional horizontal ignore trail edge)
1815 "Resize other windows when WINDOW is resized vertically by DELTA lines.
1816 Optional argument HORIZONTAL non-nil means resize other windows
1817 when WINDOW is resized horizontally by DELTA columns. WINDOW
1818 itself is not resized by this function.
1819
1820 Optional argument IGNORE non-nil means ignore any restrictions
1821 imposed by fixed size windows, `window-min-height' or
1822 `window-min-width' settings. IGNORE equal `safe' means live
1823 windows may get as small as `window-safe-min-height' lines and
1824 `window-safe-min-width' columns. IGNORE any window means ignore
1825 restrictions for that window only.
1826
1827 Optional arguments TRAIL and EDGE, when non-nil, refine the set
1828 of windows that shall be resized. If TRAIL equals `before',
1829 resize only windows on the left or above EDGE. If TRAIL equals
1830 `after', resize only windows on the right or below EDGE. Also,
1831 preferably only resize windows adjacent to EDGE."
1832 (when (window-parent window)
1833 (let* ((parent (window-parent window))
1834 (sub (window-child parent)))
1835 (if (window-combined-p sub horizontal)
1836 ;; In an iso-combination try to extract DELTA from WINDOW's
1837 ;; siblings.
1838 (let ((first sub)
1839 (skip (eq trail 'after))
1840 this-delta other-delta)
1841 ;; Decide which windows shall be left alone.
1842 (while sub
1843 (cond
1844 ((eq sub window)
1845 ;; Make sure WINDOW is left alone when
1846 ;; resizing its siblings.
1847 (set-window-new-normal sub 'ignore)
1848 (setq skip (eq trail 'before)))
1849 (skip
1850 ;; Make sure this sibling is left alone when
1851 ;; resizing its siblings.
1852 (set-window-new-normal sub 'ignore))
1853 ((or (window-size-ignore sub ignore)
1854 (not (window-size-fixed-p sub horizontal)))
1855 ;; Set this-delta to t to signal that we found a sibling
1856 ;; of WINDOW whose size is not fixed.
1857 (setq this-delta t)))
1858
1859 (setq sub (window-right sub)))
1860
1861 ;; Set this-delta to what we can get from WINDOW's siblings.
1862 (if (= (- delta) (window-total-size window horizontal))
1863 ;; A deletion, presumably. We must handle this case
1864 ;; specially since `window-resizable' can't be used.
1865 (if this-delta
1866 ;; There's at least one resizable sibling we can
1867 ;; give WINDOW's size to.
1868 (setq this-delta delta)
1869 ;; No resizable sibling exists.
1870 (setq this-delta 0))
1871 ;; Any other form of resizing.
1872 (setq this-delta
1873 (window-resizable window delta horizontal ignore trail t)))
1874
1875 ;; Set other-delta to what we still have to get from
1876 ;; ancestor windows of parent.
1877 (setq other-delta (- delta this-delta))
1878 (unless (zerop other-delta)
1879 ;; Unless we got everything from WINDOW's siblings, PARENT
1880 ;; must be resized by other-delta lines or columns.
1881 (set-window-new-total parent other-delta 'add))
1882
1883 (if (zerop this-delta)
1884 ;; We haven't got anything from WINDOW's siblings but we
1885 ;; must update the normal sizes to respect other-delta.
1886 (window--resize-subwindows-normal
1887 parent horizontal window this-delta trail other-delta)
1888 ;; We did get something from WINDOW's siblings which means
1889 ;; we have to resize their subwindows.
1890 (unless (eq (window--resize-subwindows
1891 parent (- this-delta) horizontal
1892 window ignore trail edge)
1893 ;; If `window--resize-subwindows' returns
1894 ;; 'normalized, this means it has set the
1895 ;; normal sizes already.
1896 'normalized)
1897 ;; Set the normal sizes.
1898 (window--resize-subwindows-normal
1899 parent horizontal window this-delta trail other-delta))
1900 ;; Set DELTA to what we still have to get from ancestor
1901 ;; windows.
1902 (setq delta other-delta)))
1903
1904 ;; In an ortho-combination all siblings of WINDOW must be
1905 ;; resized by DELTA.
1906 (set-window-new-total parent delta 'add)
1907 (while sub
1908 (unless (eq sub window)
1909 (window--resize-this-window sub delta horizontal ignore t))
1910 (setq sub (window-right sub))))
1911
1912 (unless (zerop delta)
1913 ;; "Go up."
1914 (window--resize-siblings
1915 parent delta horizontal ignore trail edge)))))
1916
1917 (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge)
1918 "Resize WINDOW vertically by DELTA lines.
1919 Optional argument HORIZONTAL non-nil means resize WINDOW
1920 horizontally by DELTA columns.
1921
1922 Optional argument IGNORE non-nil means ignore any restrictions
1923 imposed by fixed size windows, `window-min-height' or
1924 `window-min-width' settings. IGNORE equal `safe' means live
1925 windows may get as small as `window-safe-min-height' lines and
1926 `window-safe-min-width' columns. IGNORE any window means ignore
1927 restrictions for that window only.
1928
1929 Optional argument ADD non-nil means add DELTA to the new total
1930 size of WINDOW.
1931
1932 Optional arguments TRAIL and EDGE, when non-nil, refine the set
1933 of windows that shall be resized. If TRAIL equals `before',
1934 resize only windows on the left or above EDGE. If TRAIL equals
1935 `after', resize only windows on the right or below EDGE. Also,
1936 preferably only resize windows adjacent to EDGE.
1937
1938 This function recursively resizes WINDOW's subwindows to fit the
1939 new size. Make sure that WINDOW is `window-resizable' before
1940 calling this function. Note that this function does not resize
1941 siblings of WINDOW or WINDOW's parent window. You have to
1942 eventually call `window-resize-apply' in order to make resizing
1943 actually take effect."
1944 (when add
1945 ;; Add DELTA to the new total size of WINDOW.
1946 (set-window-new-total window delta t))
1947
1948 (let ((sub (window-child window)))
1949 (cond
1950 ((not sub))
1951 ((window-combined-p sub horizontal)
1952 ;; In an iso-combination resize subwindows according to their
1953 ;; normal sizes.
1954 (window--resize-subwindows
1955 window delta horizontal nil ignore trail edge))
1956 ;; In an ortho-combination resize each subwindow by DELTA.
1957 (t
1958 (while sub
1959 (window--resize-this-window
1960 sub delta horizontal ignore t trail edge)
1961 (setq sub (window-right sub)))))))
1962
1963 (defun window--resize-root-window (window delta horizontal ignore)
1964 "Resize root window WINDOW vertically by DELTA lines.
1965 HORIZONTAL non-nil means resize root window WINDOW horizontally
1966 by DELTA columns.
1967
1968 IGNORE non-nil means ignore any restrictions imposed by fixed
1969 size windows, `window-min-height' or `window-min-width' settings.
1970
1971 This function is only called by the frame resizing routines. It
1972 resizes windows proportionally and never deletes any windows."
1973 (when (and (windowp window) (numberp delta)
1974 (window-sizable-p window delta horizontal ignore))
1975 (window--resize-reset (window-frame window) horizontal)
1976 (window--resize-this-window window delta horizontal ignore t)))
1977
1978 (defun window--resize-root-window-vertically (window delta)
1979 "Resize root window WINDOW vertically by DELTA lines.
1980 If DELTA is less than zero and we can't shrink WINDOW by DELTA
1981 lines, shrink it as much as possible. If DELTA is greater than
1982 zero, this function can resize fixed-size subwindows in order to
1983 recover the necessary lines.
1984
1985 Return the number of lines that were recovered.
1986
1987 This function is only called by the minibuffer window resizing
1988 routines. It resizes windows proportionally and never deletes
1989 any windows."
1990 (when (numberp delta)
1991 (let (ignore)
1992 (cond
1993 ((< delta 0)
1994 (setq delta (window-sizable window delta)))
1995 ((> delta 0)
1996 (unless (window-sizable window delta)
1997 (setq ignore t))))
1998
1999 (window--resize-reset (window-frame window))
2000 ;; Ideally, we would resize just the last window in a combination
2001 ;; but that's not feasible for the following reason: If we grow
2002 ;; the minibuffer window and the last window cannot be shrunk any
2003 ;; more, we shrink another window instead. But if we then shrink
2004 ;; the minibuffer window again, the last window might get enlarged
2005 ;; and the state after shrinking is not the state before growing.
2006 ;; So, in practice, we'd need a history variable to record how to
2007 ;; proceed. But I'm not sure how such a variable could work with
2008 ;; repeated minibuffer window growing steps.
2009 (window--resize-this-window window delta nil ignore t)
2010 delta)))
2011
2012 (defun adjust-window-trailing-edge (window delta &optional horizontal)
2013 "Move WINDOW's bottom edge by DELTA lines.
2014 Optional argument HORIZONTAL non-nil means move WINDOW's right
2015 edge by DELTA columns. WINDOW defaults to the selected window.
2016
2017 If DELTA is greater zero, then move the edge downwards or to the
2018 right. If DELTA is less than zero, move the edge upwards or to
2019 the left. If the edge can't be moved by DELTA lines or columns,
2020 move it as far as possible in the desired direction."
2021 (setq window (window-normalize-window window))
2022 (let ((frame (window-frame window))
2023 (right window)
2024 left this-delta min-delta max-delta failed)
2025 ;; Find the edge we want to move.
2026 (while (and (or (not (window-combined-p right horizontal))
2027 (not (window-right right)))
2028 (setq right (window-parent right))))
2029 (cond
2030 ((and (not right) (not horizontal) (not resize-mini-windows)
2031 (eq (window-frame (minibuffer-window frame)) frame))
2032 (window--resize-mini-window (minibuffer-window frame) (- delta)))
2033 ((or (not (setq left right)) (not (setq right (window-right right))))
2034 (if horizontal
2035 (error "No window on the right of this one")
2036 (error "No window below this one")))
2037 (t
2038 ;; Set LEFT to the first resizable window on the left. This step is
2039 ;; needed to handle fixed-size windows.
2040 (while (and left (window-size-fixed-p left horizontal))
2041 (setq left
2042 (or (window-left left)
2043 (progn
2044 (while (and (setq left (window-parent left))
2045 (not (window-combined-p left horizontal))))
2046 (window-left left)))))
2047 (unless left
2048 (if horizontal
2049 (error "No resizable window on the left of this one")
2050 (error "No resizable window above this one")))
2051
2052 ;; Set RIGHT to the first resizable window on the right. This step
2053 ;; is needed to handle fixed-size windows.
2054 (while (and right (window-size-fixed-p right horizontal))
2055 (setq right
2056 (or (window-right right)
2057 (progn
2058 (while (and (setq right (window-parent right))
2059 (not (window-combined-p right horizontal))))
2060 (window-right right)))))
2061 (unless right
2062 (if horizontal
2063 (error "No resizable window on the right of this one")
2064 (error "No resizable window below this one")))
2065
2066 ;; LEFT and RIGHT (which might be both internal windows) are now the
2067 ;; two windows we want to resize.
2068 (cond
2069 ((> delta 0)
2070 (setq max-delta (window-max-delta-1 left 0 horizontal nil 'after))
2071 (setq min-delta (window-min-delta-1 right (- delta) horizontal nil 'before))
2072 (when (or (< max-delta delta) (> min-delta (- delta)))
2073 ;; We can't get the whole DELTA - move as far as possible.
2074 (setq delta (min max-delta (- min-delta))))
2075 (unless (zerop delta)
2076 ;; Start resizing.
2077 (window--resize-reset frame horizontal)
2078 ;; Try to enlarge LEFT first.
2079 (setq this-delta (window-resizable left delta horizontal))
2080 (unless (zerop this-delta)
2081 (window--resize-this-window
2082 left this-delta horizontal nil t 'before
2083 (if horizontal
2084 (+ (window-left-column left) (window-total-size left t))
2085 (+ (window-top-line left) (window-total-size left)))))
2086 ;; Shrink windows on right of LEFT.
2087 (window--resize-siblings
2088 left delta horizontal nil 'after
2089 (if horizontal
2090 (window-left-column right)
2091 (window-top-line right)))))
2092 ((< delta 0)
2093 (setq max-delta (window-max-delta-1 right 0 horizontal nil 'before))
2094 (setq min-delta (window-min-delta-1 left delta horizontal nil 'after))
2095 (when (or (< max-delta (- delta)) (> min-delta delta))
2096 ;; We can't get the whole DELTA - move as far as possible.
2097 (setq delta (max (- max-delta) min-delta)))
2098 (unless (zerop delta)
2099 ;; Start resizing.
2100 (window--resize-reset frame horizontal)
2101 ;; Try to enlarge RIGHT.
2102 (setq this-delta (window-resizable right (- delta) horizontal))
2103 (unless (zerop this-delta)
2104 (window--resize-this-window
2105 right this-delta horizontal nil t 'after
2106 (if horizontal
2107 (window-left-column right)
2108 (window-top-line right))))
2109 ;; Shrink windows on left of RIGHT.
2110 (window--resize-siblings
2111 right (- delta) horizontal nil 'before
2112 (if horizontal
2113 (+ (window-left-column left) (window-total-size left t))
2114 (+ (window-top-line left) (window-total-size left)))))))
2115 (unless (zerop delta)
2116 ;; Don't report an error in the standard case.
2117 (unless (window-resize-apply frame horizontal)
2118 ;; But do report an error if applying the changes fails.
2119 (error "Failed adjusting window %s" window)))))))
2120
2121 (defun enlarge-window (delta &optional horizontal)
2122 "Make selected window DELTA lines taller.
2123 Interactively, if no argument is given, make the selected window
2124 one line taller. If optional argument HORIZONTAL is non-nil,
2125 make selected window wider by DELTA columns. If DELTA is
2126 negative, shrink selected window by -DELTA lines or columns.
2127 Return nil."
2128 (interactive "p")
2129 (cond
2130 ((zerop delta))
2131 ((window-size-fixed-p nil horizontal)
2132 (error "Selected window has fixed size"))
2133 ((window-resizable-p nil delta horizontal)
2134 (window-resize nil delta horizontal))
2135 (t
2136 (window-resize
2137 nil (if (> delta 0)
2138 (window-max-delta nil horizontal)
2139 (- (window-min-delta nil horizontal)))
2140 horizontal))))
2141
2142 (defun shrink-window (delta &optional horizontal)
2143 "Make selected window DELTA lines smaller.
2144 Interactively, if no argument is given, make the selected window
2145 one line smaller. If optional argument HORIZONTAL is non-nil,
2146 make selected window narrower by DELTA columns. If DELTA is
2147 negative, enlarge selected window by -DELTA lines or columns.
2148 Return nil."
2149 (interactive "p")
2150 (cond
2151 ((zerop delta))
2152 ((window-size-fixed-p nil horizontal)
2153 (error "Selected window has fixed size"))
2154 ((window-resizable-p nil (- delta) horizontal)
2155 (window-resize nil (- delta) horizontal))
2156 (t
2157 (window-resize
2158 nil (if (> delta 0)
2159 (- (window-min-delta nil horizontal))
2160 (window-max-delta nil horizontal))
2161 horizontal))))
2162
2163 (defun maximize-window (&optional window)
2164 "Maximize WINDOW.
2165 Make WINDOW as large as possible without deleting any windows.
2166 WINDOW can be any window and defaults to the selected window."
2167 (interactive)
2168 (setq window (window-normalize-window window))
2169 (window-resize window (window-max-delta window))
2170 (window-resize window (window-max-delta window t) t))
2171
2172 (defun minimize-window (&optional window)
2173 "Minimize WINDOW.
2174 Make WINDOW as small as possible without deleting any windows.
2175 WINDOW can be any window and defaults to the selected window."
2176 (interactive)
2177 (setq window (window-normalize-window window))
2178 (window-resize window (- (window-min-delta window)))
2179 (window-resize window (- (window-min-delta window t)) t))
2180 \f
2181 (defsubst frame-root-window-p (window)
2182 "Return non-nil if WINDOW is the root window of its frame."
2183 (eq window (frame-root-window window)))
2184
2185 (defun window-tree-1 (window &optional next)
2186 "Return window tree rooted at WINDOW.
2187 Optional argument NEXT non-nil means include windows right
2188 siblings in the return value.
2189
2190 See the documentation of `window-tree' for a description of the
2191 return value."
2192 (let (list)
2193 (while window
2194 (setq list
2195 (cons
2196 (cond
2197 ((window-top-child window)
2198 (cons t (cons (window-edges window)
2199 (window-tree-1 (window-top-child window) t))))
2200 ((window-left-child window)
2201 (cons nil (cons (window-edges window)
2202 (window-tree-1 (window-left-child window) t))))
2203 (t window))
2204 list))
2205 (setq window (when next (window-next-sibling window))))
2206 (nreverse list)))
2207
2208 (defun window-tree (&optional frame)
2209 "Return the window tree of frame FRAME.
2210 FRAME must be a live frame and defaults to the selected frame.
2211 The return value is a list of the form (ROOT MINI), where ROOT
2212 represents the window tree of the frame's root window, and MINI
2213 is the frame's minibuffer window.
2214
2215 If the root window is not split, ROOT is the root window itself.
2216 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
2217 for a horizontal split, and t for a vertical split. EDGES gives
2218 the combined size and position of the subwindows in the split,
2219 and the rest of the elements are the subwindows in the split.
2220 Each of the subwindows may again be a window or a list
2221 representing a window split, and so on. EDGES is a list \(LEFT
2222 TOP RIGHT BOTTOM) as returned by `window-edges'."
2223 (setq frame (window-normalize-frame frame))
2224 (window-tree-1 (frame-root-window frame) t))
2225 \f
2226 (defun other-window (count &optional all-frames)
2227 "Select another window in cyclic ordering of windows.
2228 COUNT specifies the number of windows to skip, starting with the
2229 selected window, before making the selection. If COUNT is
2230 positive, skip COUNT windows forwards. If COUNT is negative,
2231 skip -COUNT windows backwards. COUNT zero means do not skip any
2232 window, so select the selected window. In an interactive call,
2233 COUNT is the numeric prefix argument. Return nil.
2234
2235 If the `other-window' parameter of WINDOW is a function and
2236 `ignore-window-parameters' is nil, call that function with the
2237 arguments COUNT and ALL-FRAMES.
2238
2239 This function does not select a window whose `no-other-window'
2240 window parameter is non-nil.
2241
2242 This function uses `next-window' for finding the window to
2243 select. The argument ALL-FRAMES has the same meaning as in
2244 `next-window', but the MINIBUF argument of `next-window' is
2245 always effectively nil."
2246 (interactive "p")
2247 (let* ((window (selected-window))
2248 (function (and (not ignore-window-parameters)
2249 (window-parameter window 'other-window)))
2250 old-window old-count)
2251 (if (functionp function)
2252 (funcall function count all-frames)
2253 ;; `next-window' and `previous-window' may return a window we are
2254 ;; not allowed to select. Hence we need an exit strategy in case
2255 ;; all windows are non-selectable.
2256 (catch 'exit
2257 (while (> count 0)
2258 (setq window (next-window window nil all-frames))
2259 (cond
2260 ((eq window old-window)
2261 (when (= count old-count)
2262 ;; Keep out of infinite loops. When COUNT has not changed
2263 ;; since we last looked at `window' we're probably in one.
2264 (throw 'exit nil)))
2265 ((window-parameter window 'no-other-window)
2266 (unless old-window
2267 ;; The first non-selectable window `next-window' got us:
2268 ;; Remember it and the current value of COUNT.
2269 (setq old-window window)
2270 (setq old-count count)))
2271 (t
2272 (setq count (1- count)))))
2273 (while (< count 0)
2274 (setq window (previous-window window nil all-frames))
2275 (cond
2276 ((eq window old-window)
2277 (when (= count old-count)
2278 ;; Keep out of infinite loops. When COUNT has not changed
2279 ;; since we last looked at `window' we're probably in one.
2280 (throw 'exit nil)))
2281 ((window-parameter window 'no-other-window)
2282 (unless old-window
2283 ;; The first non-selectable window `previous-window' got
2284 ;; us: Remember it and the current value of COUNT.
2285 (setq old-window window)
2286 (setq old-count count)))
2287 (t
2288 (setq count (1+ count)))))
2289
2290 (select-window window)
2291 ;; Always return nil.
2292 nil))))
2293
2294 ;; This should probably return non-nil when the selected window is part
2295 ;; of an atomic window whose root is the frame's root window.
2296 (defun one-window-p (&optional nomini all-frames)
2297 "Return non-nil if the selected window is the only window.
2298 Optional arg NOMINI non-nil means don't count the minibuffer
2299 even if it is active. Otherwise, the minibuffer is counted
2300 when it is active.
2301
2302 Optional argument ALL-FRAMES specifies the set of frames to
2303 consider, see also `next-window'. ALL-FRAMES nil or omitted
2304 means consider windows on the selected frame only, plus the
2305 minibuffer window if specified by the NOMINI argument. If the
2306 minibuffer counts, consider all windows on all frames that share
2307 that minibuffer too. The remaining non-nil values of ALL-FRAMES
2308 with a special meaning are:
2309
2310 - t means consider all windows on all existing frames.
2311
2312 - `visible' means consider all windows on all visible frames on
2313 the current terminal.
2314
2315 - 0 (the number zero) means consider all windows on all visible
2316 and iconified frames on the current terminal.
2317
2318 - A frame means consider all windows on that frame only.
2319
2320 Anything else means consider all windows on the selected frame
2321 and no others."
2322 (let ((base-window (selected-window)))
2323 (if (and nomini (eq base-window (minibuffer-window)))
2324 (setq base-window (next-window base-window)))
2325 (eq base-window
2326 (next-window base-window (if nomini 'arg) all-frames))))
2327 \f
2328 ;;; Deleting windows.
2329 (defun window-deletable-p (&optional window)
2330 "Return t if WINDOW can be safely deleted from its frame.
2331 Return `frame' if deleting WINDOW should also delete its
2332 frame."
2333 (setq window (window-normalize-window window))
2334
2335 (unless ignore-window-parameters
2336 ;; Handle atomicity.
2337 (when (window-parameter window 'window-atom)
2338 (setq window (window-atom-root window))))
2339
2340 (let* ((parent (window-parent window))
2341 (frame (window-frame window))
2342 (buffer (window-buffer window)))
2343 (cond
2344 ((frame-root-window-p window)
2345 ;; WINDOW's frame can be deleted only if there are other frames
2346 ;; on the same terminal.
2347 (unless (eq frame (next-frame frame 0))
2348 'frame))
2349 ((or ignore-window-parameters
2350 (not (eq (window-parameter window 'window-side) 'none))
2351 (and parent (eq (window-parameter parent 'window-side) 'none)))
2352 ;; WINDOW can be deleted unless it is the main window of its
2353 ;; frame.
2354 t))))
2355
2356 (defun window-or-subwindow-p (subwindow window)
2357 "Return t if SUBWINDOW is either WINDOW or a subwindow of WINDOW."
2358 (or (eq subwindow window)
2359 (let ((parent (window-parent subwindow)))
2360 (catch 'done
2361 (while parent
2362 (if (eq parent window)
2363 (throw 'done t)
2364 (setq parent (window-parent parent))))))))
2365
2366 (defun delete-window (&optional window)
2367 "Delete WINDOW.
2368 WINDOW can be an arbitrary window and defaults to the selected
2369 one. Return nil.
2370
2371 If the variable `ignore-window-parameters' is non-nil or the
2372 `delete-window' parameter of WINDOW equals t, do not process any
2373 parameters of WINDOW. Otherwise, if the `delete-window'
2374 parameter of WINDOW specifies a function, call that function with
2375 WINDOW as its sole argument and return the value returned by that
2376 function.
2377
2378 Otherwise, if WINDOW is part of an atomic window, call
2379 `delete-window' with the root of the atomic window as its
2380 argument. If WINDOW is the only window on its frame or the last
2381 non-side window, signal an error."
2382 (interactive)
2383 (setq window (window-normalize-window window))
2384 (let* ((frame (window-frame window))
2385 (function (window-parameter window 'delete-window))
2386 (parent (window-parent window))
2387 atom-root)
2388 (window-check frame)
2389 (catch 'done
2390 ;; Handle window parameters.
2391 (cond
2392 ;; Ignore window parameters if `ignore-window-parameters' tells
2393 ;; us so or `delete-window' equals t.
2394 ((or ignore-window-parameters (eq function t)))
2395 ((functionp function)
2396 ;; The `delete-window' parameter specifies the function to call.
2397 ;; If that function is `ignore' nothing is done. It's up to the
2398 ;; function called here to avoid infinite recursion.
2399 (throw 'done (funcall function window)))
2400 ((and (window-parameter window 'window-atom)
2401 (setq atom-root (window-atom-root window))
2402 (not (eq atom-root window)))
2403 (throw 'done (delete-window atom-root)))
2404 ((and (eq (window-parameter window 'window-side) 'none)
2405 (or (not parent)
2406 (not (eq (window-parameter parent 'window-side) 'none))))
2407 (error "Attempt to delete last non-side window"))
2408 ((not parent)
2409 (error "Attempt to delete minibuffer or sole ordinary window")))
2410
2411 (let* ((horizontal (window-left-child parent))
2412 (size (window-total-size window horizontal))
2413 (frame-selected
2414 (window-or-subwindow-p (frame-selected-window frame) window))
2415 ;; Emacs 23 preferably gives WINDOW's space to its left
2416 ;; sibling.
2417 (sibling (or (window-left window) (window-right window))))
2418 (window--resize-reset frame horizontal)
2419 (cond
2420 ((and (not (window-splits window))
2421 sibling (window-sizable-p sibling size))
2422 ;; Resize WINDOW's sibling.
2423 (window--resize-this-window sibling size horizontal nil t)
2424 (set-window-new-normal
2425 sibling (+ (window-normal-size sibling horizontal)
2426 (window-normal-size window horizontal))))
2427 ((window-resizable-p window (- size) horizontal nil nil nil t)
2428 ;; Can do without resizing fixed-size windows.
2429 (window--resize-siblings window (- size) horizontal))
2430 (t
2431 ;; Can't do without resizing fixed-size windows.
2432 (window--resize-siblings window (- size) horizontal t)))
2433 ;; Actually delete WINDOW.
2434 (delete-window-internal window)
2435 (when (and frame-selected
2436 (window-parameter
2437 (frame-selected-window frame) 'no-other-window))
2438 ;; `delete-window-internal' has selected a window that should
2439 ;; not be selected, fix this here.
2440 (other-window -1 frame))
2441 (run-window-configuration-change-hook frame)
2442 (window-check frame)
2443 ;; Always return nil.
2444 nil))))
2445
2446 (defun delete-other-windows (&optional window)
2447 "Make WINDOW fill its frame.
2448 WINDOW may be any window and defaults to the selected one.
2449 Return nil.
2450
2451 If the variable `ignore-window-parameters' is non-nil or the
2452 `delete-other-windows' parameter of WINDOW equals t, do not
2453 process any parameters of WINDOW. Otherwise, if the
2454 `delete-other-windows' parameter of WINDOW specifies a function,
2455 call that function with WINDOW as its sole argument and return
2456 the value returned by that function.
2457
2458 Otherwise, if WINDOW is part of an atomic window, call this
2459 function with the root of the atomic window as its argument. If
2460 WINDOW is a non-side window, make WINDOW the only non-side window
2461 on the frame. Side windows are not deleted. If WINDOW is a side
2462 window signal an error."
2463 (interactive)
2464 (setq window (window-normalize-window window))
2465 (let* ((frame (window-frame window))
2466 (function (window-parameter window 'delete-other-windows))
2467 (window-side (window-parameter window 'window-side))
2468 atom-root side-main)
2469 (window-check frame)
2470 (catch 'done
2471 (cond
2472 ;; Ignore window parameters if `ignore-window-parameters' is t or
2473 ;; `delete-other-windows' is t.
2474 ((or ignore-window-parameters (eq function t)))
2475 ((functionp function)
2476 ;; The `delete-other-windows' parameter specifies the function
2477 ;; to call. If the function is `ignore' no windows are deleted.
2478 ;; It's up to the function called to avoid infinite recursion.
2479 (throw 'done (funcall function window)))
2480 ((and (window-parameter window 'window-atom)
2481 (setq atom-root (window-atom-root window))
2482 (not (eq atom-root window)))
2483 (throw 'done (delete-other-windows atom-root)))
2484 ((eq window-side 'none)
2485 ;; Set side-main to the major non-side window.
2486 (setq side-main (window-with-parameter 'window-side 'none nil t)))
2487 ((memq window-side window-sides)
2488 (error "Cannot make side window the only window")))
2489 ;; If WINDOW is the main non-side window, do nothing.
2490 (unless (eq window side-main)
2491 (delete-other-windows-internal window side-main)
2492 (run-window-configuration-change-hook frame)
2493 (window-check frame))
2494 ;; Always return nil.
2495 nil)))
2496
2497 (defun delete-other-windows-vertically (&optional window)
2498 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
2499 This may be a useful alternative binding for \\[delete-other-windows]
2500 if you often split windows horizontally."
2501 (interactive)
2502 (let* ((window (or window (selected-window)))
2503 (edges (window-edges window))
2504 (w window) delenda)
2505 (while (not (eq (setq w (next-window w 1)) window))
2506 (let ((e (window-edges w)))
2507 (when (and (= (car e) (car edges))
2508 (= (caddr e) (caddr edges)))
2509 (push w delenda))))
2510 (mapc 'delete-window delenda)))
2511
2512 ;;; Windows and buffers.
2513
2514 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
2515 ;; for (1) determining which buffer to show in the window when its
2516 ;; buffer shall be buried or killed and (2) which buffer to show for
2517 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2518
2519 ;; `prev-buffers' consists of <buffer, window-start, window-point>
2520 ;; triples. The entries on this list are ordered by the time their
2521 ;; buffer has been removed from the window, the most recently removed
2522 ;; buffer's entry being first. The window-start and window-point
2523 ;; components are `window-start' and `window-point' at the time the
2524 ;; buffer was removed from the window which implies that the entry must
2525 ;; be added when `set-window-buffer' removes the buffer from the window.
2526
2527 ;; `next-buffers' is the list of buffers that have been replaced
2528 ;; recently by `switch-to-prev-buffer'. These buffers are the least
2529 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
2530 ;; candidates of `switch-to-next-buffer' to switch to. This list is
2531 ;; reset to nil by any action changing the window's buffer with the
2532 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
2533 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
2534 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
2535
2536 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
2537 ;; if such a buffer was killed while the window was hidden within a
2538 ;; window configuration. Such killed buffers get removed whenever
2539 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
2540
2541 ;; The following function is called by `set-window-buffer' _before_ it
2542 ;; replaces the buffer of the argument window with the new buffer.
2543 (defun record-window-buffer (&optional window)
2544 "Record WINDOW's buffer.
2545 WINDOW must be a live window and defaults to the selected one."
2546 (let* ((window (window-normalize-window window t))
2547 (buffer (window-buffer window))
2548 (entry (assq buffer (window-prev-buffers window))))
2549 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
2550 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2551 (set-window-next-buffers window nil)
2552
2553 (when entry
2554 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
2555 (set-window-prev-buffers
2556 window (assq-delete-all buffer (window-prev-buffers window))))
2557
2558 ;; Don't record insignificant buffers.
2559 (unless (eq (aref (buffer-name buffer) 0) ?\s)
2560 ;; Add an entry for buffer to WINDOW's previous buffers.
2561 (with-current-buffer buffer
2562 (let ((start (window-start window))
2563 (point (window-point-1 window)))
2564 (setq entry
2565 (cons buffer
2566 (if entry
2567 ;; We have an entry, update marker positions.
2568 (list (set-marker (nth 1 entry) start)
2569 (set-marker (nth 2 entry) point))
2570 ;; Make new markers.
2571 (list (copy-marker start)
2572 (copy-marker point)))))
2573
2574 (set-window-prev-buffers
2575 window (cons entry (window-prev-buffers window))))))))
2576
2577 (defun unrecord-window-buffer (&optional window buffer)
2578 "Unrecord BUFFER in WINDOW.
2579 WINDOW must be a live window and defaults to the selected one.
2580 BUFFER must be a live buffer and defaults to the buffer of
2581 WINDOW."
2582 (let* ((window (window-normalize-window window t))
2583 (buffer (or buffer (window-buffer window))))
2584 (set-window-prev-buffers
2585 window (assq-delete-all buffer (window-prev-buffers window)))
2586 (set-window-next-buffers
2587 window (delq buffer (window-next-buffers window)))))
2588
2589 (defun set-window-buffer-start-and-point (window buffer &optional start point)
2590 "Set WINDOW's buffer to BUFFER.
2591 Optional argument START non-nil means set WINDOW's start position
2592 to START. Optional argument POINT non-nil means set WINDOW's
2593 point to POINT. If WINDOW is selected this also sets BUFFER's
2594 `point' to POINT. If WINDOW is selected and the buffer it showed
2595 before was current this also makes BUFFER the current buffer."
2596 (let ((selected (eq window (selected-window)))
2597 (current (eq (window-buffer window) (current-buffer))))
2598 (set-window-buffer window buffer)
2599 (when (and selected current)
2600 (set-buffer buffer))
2601 (when start
2602 ;; Don't force window-start here (even if POINT is nil).
2603 (set-window-start window start t))
2604 (when point
2605 (set-window-point-1 window point))))
2606
2607 (defun switch-to-prev-buffer (&optional window bury-or-kill)
2608 "In WINDOW switch to previous buffer.
2609 WINDOW must be a live window and defaults to the selected one.
2610
2611 Optional argument BURY-OR-KILL non-nil means the buffer currently
2612 shown in WINDOW is about to be buried or killed and consequently
2613 shall not be switched to in future invocations of this command."
2614 (interactive)
2615 (let* ((window (window-normalize-window window t))
2616 (old-buffer (window-buffer window))
2617 ;; Save this since it's destroyed by `set-window-buffer'.
2618 (next-buffers (window-next-buffers window))
2619 entry new-buffer killed-buffers visible)
2620 (when (window-dedicated-p window)
2621 (error "Window %s is dedicated to buffer %s" window old-buffer))
2622
2623 (catch 'found
2624 ;; Scan WINDOW's previous buffers first, skipping entries of next
2625 ;; buffers.
2626 (dolist (entry (window-prev-buffers window))
2627 (when (and (setq new-buffer (car entry))
2628 (or (buffer-live-p new-buffer)
2629 (not (setq killed-buffers
2630 (cons new-buffer killed-buffers))))
2631 (not (eq new-buffer old-buffer))
2632 (or bury-or-kill
2633 (not (memq new-buffer next-buffers))))
2634 (set-window-buffer-start-and-point
2635 window new-buffer (nth 1 entry) (nth 2 entry))
2636 (throw 'found t)))
2637 ;; Scan reverted buffer list of WINDOW's frame next, skipping
2638 ;; entries of next buffers. Note that when we bury or kill a
2639 ;; buffer we don't reverse the global buffer list to avoid showing
2640 ;; a buried buffer instead. Otherwise, we must reverse the global
2641 ;; buffer list in order to make sure that switching to the
2642 ;; previous/next buffer traverse it in opposite directions.
2643 (dolist (buffer (if bury-or-kill
2644 (buffer-list (window-frame window))
2645 (nreverse (buffer-list (window-frame window)))))
2646 (when (and (buffer-live-p buffer)
2647 (not (eq buffer old-buffer))
2648 (not (eq (aref (buffer-name buffer) 0) ?\s))
2649 (or bury-or-kill (not (memq buffer next-buffers))))
2650 (if (get-buffer-window buffer)
2651 ;; Try to avoid showing a buffer visible in some other window.
2652 (setq visible buffer)
2653 (setq new-buffer buffer)
2654 (set-window-buffer-start-and-point window new-buffer)
2655 (throw 'found t))))
2656 (unless bury-or-kill
2657 ;; Scan reverted next buffers last (must not use nreverse
2658 ;; here!).
2659 (dolist (buffer (reverse next-buffers))
2660 ;; Actually, buffer _must_ be live here since otherwise it
2661 ;; would have been caught in the scan of previous buffers.
2662 (when (and (or (buffer-live-p buffer)
2663 (not (setq killed-buffers
2664 (cons buffer killed-buffers))))
2665 (not (eq buffer old-buffer))
2666 (setq entry (assq buffer (window-prev-buffers window))))
2667 (setq new-buffer buffer)
2668 (set-window-buffer-start-and-point
2669 window new-buffer (nth 1 entry) (nth 2 entry))
2670 (throw 'found t))))
2671
2672 ;; Show a buffer visible in another window.
2673 (when visible
2674 (setq new-buffer visible)
2675 (set-window-buffer-start-and-point window new-buffer)))
2676
2677 (if bury-or-kill
2678 ;; Remove `old-buffer' from WINDOW's previous and (restored list
2679 ;; of) next buffers.
2680 (progn
2681 (set-window-prev-buffers
2682 window (assq-delete-all old-buffer (window-prev-buffers window)))
2683 (set-window-next-buffers window (delq old-buffer next-buffers)))
2684 ;; Move `old-buffer' to head of WINDOW's restored list of next
2685 ;; buffers.
2686 (set-window-next-buffers
2687 window (cons old-buffer (delq old-buffer next-buffers))))
2688
2689 ;; Remove killed buffers from WINDOW's previous and next buffers.
2690 (when killed-buffers
2691 (dolist (buffer killed-buffers)
2692 (set-window-prev-buffers
2693 window (assq-delete-all buffer (window-prev-buffers window)))
2694 (set-window-next-buffers
2695 window (delq buffer (window-next-buffers window)))))
2696
2697 ;; Return new-buffer.
2698 new-buffer))
2699
2700 (defun switch-to-next-buffer (&optional window)
2701 "In WINDOW switch to next buffer.
2702 WINDOW must be a live window and defaults to the selected one."
2703 (interactive)
2704 (let* ((window (window-normalize-window window t))
2705 (old-buffer (window-buffer window))
2706 (next-buffers (window-next-buffers window))
2707 new-buffer entry killed-buffers visible)
2708 (when (window-dedicated-p window)
2709 (error "Window %s is dedicated to buffer %s" window old-buffer))
2710
2711 (catch 'found
2712 ;; Scan WINDOW's next buffers first.
2713 (dolist (buffer next-buffers)
2714 (when (and (or (buffer-live-p buffer)
2715 (not (setq killed-buffers
2716 (cons buffer killed-buffers))))
2717 (not (eq buffer old-buffer))
2718 (setq entry (assq buffer (window-prev-buffers window))))
2719 (setq new-buffer buffer)
2720 (set-window-buffer-start-and-point
2721 window new-buffer (nth 1 entry) (nth 2 entry))
2722 (throw 'found t)))
2723 ;; Scan the buffer list of WINDOW's frame next, skipping previous
2724 ;; buffers entries.
2725 (dolist (buffer (buffer-list (window-frame window)))
2726 (when (and (buffer-live-p buffer) (not (eq buffer old-buffer))
2727 (not (eq (aref (buffer-name buffer) 0) ?\s))
2728 (not (assq buffer (window-prev-buffers window))))
2729 (if (get-buffer-window buffer)
2730 ;; Try to avoid showing a buffer visible in some other window.
2731 (setq visible buffer)
2732 (setq new-buffer buffer)
2733 (set-window-buffer-start-and-point window new-buffer)
2734 (throw 'found t))))
2735 ;; Scan WINDOW's reverted previous buffers last (must not use
2736 ;; nreverse here!)
2737 (dolist (entry (reverse (window-prev-buffers window)))
2738 (when (and (setq new-buffer (car entry))
2739 (or (buffer-live-p new-buffer)
2740 (not (setq killed-buffers
2741 (cons new-buffer killed-buffers))))
2742 (not (eq new-buffer old-buffer)))
2743 (set-window-buffer-start-and-point
2744 window new-buffer (nth 1 entry) (nth 2 entry))
2745 (throw 'found t)))
2746
2747 ;; Show a buffer visible in another window.
2748 (when visible
2749 (setq new-buffer visible)
2750 (set-window-buffer-start-and-point window new-buffer)))
2751
2752 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
2753 (set-window-next-buffers window (delq new-buffer next-buffers))
2754
2755 ;; Remove killed buffers from WINDOW's previous and next buffers.
2756 (when killed-buffers
2757 (dolist (buffer killed-buffers)
2758 (set-window-prev-buffers
2759 window (assq-delete-all buffer (window-prev-buffers window)))
2760 (set-window-next-buffers
2761 window (delq buffer (window-next-buffers window)))))
2762
2763 ;; Return new-buffer.
2764 new-buffer))
2765
2766 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
2767 "Search LIST for a valid buffer to display in FRAME.
2768 Return nil when all buffers in LIST are undesirable for display,
2769 otherwise return the first suitable buffer in LIST.
2770
2771 Buffers not visible in windows are preferred to visible buffers,
2772 unless VISIBLE-OK is non-nil.
2773 If the optional argument FRAME is nil, it defaults to the selected frame.
2774 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
2775 ;; This logic is more or less copied from other-buffer.
2776 (setq frame (or frame (selected-frame)))
2777 (let ((pred (frame-parameter frame 'buffer-predicate))
2778 found buf)
2779 (while (and (not found) list)
2780 (setq buf (car list))
2781 (if (and (not (eq buffer buf))
2782 (buffer-live-p buf)
2783 (or (null pred) (funcall pred buf))
2784 (not (eq (aref (buffer-name buf) 0) ?\s))
2785 (or visible-ok (null (get-buffer-window buf 'visible))))
2786 (setq found buf)
2787 (setq list (cdr list))))
2788 (car list)))
2789
2790 (defun last-buffer (&optional buffer visible-ok frame)
2791 "Return the last buffer in FRAME's buffer list.
2792 If BUFFER is the last buffer, return the preceding buffer
2793 instead. Buffers not visible in windows are preferred to visible
2794 buffers, unless optional argument VISIBLE-OK is non-nil.
2795 Optional third argument FRAME nil or omitted means use the
2796 selected frame's buffer list. If no such buffer exists, return
2797 the buffer `*scratch*', creating it if necessary."
2798 (setq frame (or frame (selected-frame)))
2799 (or (get-next-valid-buffer (nreverse (buffer-list frame))
2800 buffer visible-ok frame)
2801 (get-buffer "*scratch*")
2802 (let ((scratch (get-buffer-create "*scratch*")))
2803 (set-buffer-major-mode scratch)
2804 scratch)))
2805
2806 (defcustom frame-auto-hide-function #'iconify-frame
2807 "Function called to automatically hide frames.
2808 The function is called with one argument - a frame.
2809
2810 Functions affected by this option are those that bury a buffer
2811 shown in a separate frame like `quit-window' and `bury-buffer'."
2812 :type '(choice (const :tag "Iconify" iconify-frame)
2813 (const :tag "Delete" delete-frame)
2814 (const :tag "Do nothing" ignore)
2815 function)
2816 :group 'windows
2817 :group 'frames
2818 :version "24.1")
2819
2820 (defun window--delete (&optional window dedicated-only kill)
2821 "Delete WINDOW if possible.
2822 WINDOW must be a live window and defaults to the selected one.
2823 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
2824 only if it's dedicated to its buffer. Optional argument KILL
2825 means the buffer shown in window will be killed. Return non-nil
2826 if WINDOW gets deleted or its frame is auto-hidden."
2827 (setq window (window-normalize-window window t))
2828 (unless (and dedicated-only (not (window-dedicated-p window)))
2829 (let* ((buffer (window-buffer window))
2830 (deletable (window-deletable-p window)))
2831 (cond
2832 ((eq deletable 'frame)
2833 (let ((frame (window-frame window)))
2834 (cond
2835 (kill
2836 (delete-frame frame))
2837 ((functionp frame-auto-hide-function)
2838 (funcall frame-auto-hide-function frame))))
2839 'frame)
2840 (deletable
2841 (delete-window window)
2842 t)))))
2843
2844 (defun bury-buffer (&optional buffer-or-name)
2845 "Put BUFFER-OR-NAME at the end of the list of all buffers.
2846 There it is the least likely candidate for `other-buffer' to
2847 return; thus, the least likely buffer for \\[switch-to-buffer] to
2848 select by default.
2849
2850 You can specify a buffer name as BUFFER-OR-NAME, or an actual
2851 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
2852 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
2853 remove the current buffer from the selected window if it is
2854 displayed there."
2855 (interactive)
2856 (let* ((buffer (window-normalize-buffer buffer-or-name)))
2857 ;; If `buffer-or-name' is not on the selected frame we unrecord it
2858 ;; although it's not "here" (call it a feature).
2859 (bury-buffer-internal buffer)
2860 ;; Handle case where `buffer-or-name' is nil and the current buffer
2861 ;; is shown in the selected window.
2862 (cond
2863 ((or buffer-or-name (not (eq buffer (window-buffer)))))
2864 ((window--delete nil t))
2865 (t
2866 ;; Switch to another buffer in window.
2867 (set-window-dedicated-p nil nil)
2868 (switch-to-prev-buffer nil 'bury)))
2869
2870 ;; Always return nil.
2871 nil))
2872
2873 (defun unbury-buffer ()
2874 "Switch to the last buffer in the buffer list."
2875 (interactive)
2876 (switch-to-buffer (last-buffer)))
2877
2878 (defun next-buffer ()
2879 "In selected window switch to next buffer."
2880 (interactive)
2881 (if (window-minibuffer-p)
2882 (error "Cannot switch buffers in minibuffer window"))
2883 (switch-to-next-buffer))
2884
2885 (defun previous-buffer ()
2886 "In selected window switch to previous buffer."
2887 (interactive)
2888 (if (window-minibuffer-p)
2889 (error "Cannot switch buffers in minibuffer window"))
2890 (switch-to-prev-buffer))
2891
2892 (defun delete-windows-on (&optional buffer-or-name frame)
2893 "Delete all windows showing BUFFER-OR-NAME.
2894 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2895 and defaults to the current buffer.
2896
2897 The following non-nil values of the optional argument FRAME
2898 have special meanings:
2899
2900 - t means consider all windows on the selected frame only.
2901
2902 - `visible' means consider all windows on all visible frames on
2903 the current terminal.
2904
2905 - 0 (the number zero) means consider all windows on all visible
2906 and iconified frames on the current terminal.
2907
2908 - A frame means consider all windows on that frame only.
2909
2910 Any other value of FRAME means consider all windows on all
2911 frames.
2912
2913 When a window showing BUFFER-OR-NAME is dedicated and the only
2914 window of its frame, that frame is deleted when there are other
2915 frames left."
2916 (interactive "BDelete windows on (buffer):\nP")
2917 (let ((buffer (window-normalize-buffer buffer-or-name))
2918 ;; Handle the "inverted" meaning of the FRAME argument wrt other
2919 ;; `window-list-1' based function.
2920 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
2921 (dolist (window (window-list-1 nil nil all-frames))
2922 (if (eq (window-buffer window) buffer)
2923 (let ((deletable (window-deletable-p window)))
2924 (cond
2925 ((and (eq deletable 'frame) (window-dedicated-p window))
2926 ;; Delete frame if and only if window is dedicated.
2927 (delete-frame (window-frame window)))
2928 ((eq deletable t)
2929 ;; Delete window.
2930 (delete-window window))
2931 (t
2932 ;; In window switch to previous buffer.
2933 (set-window-dedicated-p window nil)
2934 (switch-to-prev-buffer window 'bury))))
2935 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
2936 (unrecord-window-buffer window buffer)))))
2937
2938 (defun replace-buffer-in-windows (&optional buffer-or-name)
2939 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2940 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2941 and defaults to the current buffer.
2942
2943 When a window showing BUFFER-OR-NAME is dedicated, that window is
2944 deleted. If that window is the only window on its frame, the
2945 frame is deleted too when there are other frames left. If there
2946 are no other frames left, some other buffer is displayed in that
2947 window.
2948
2949 This function removes the buffer denoted by BUFFER-OR-NAME from
2950 all window-local buffer lists."
2951 (let ((buffer (window-normalize-buffer buffer-or-name)))
2952 (dolist (window (window-list-1 nil nil t))
2953 (if (eq (window-buffer window) buffer)
2954 (unless (window--delete window t t)
2955 ;; Switch to another buffer in window.
2956 (set-window-dedicated-p window nil)
2957 (switch-to-prev-buffer window 'kill))
2958 ;; Unrecord BUFFER in WINDOW.
2959 (unrecord-window-buffer window buffer)))))
2960
2961 (defun quit-window (&optional kill window)
2962 "Quit WINDOW and bury its buffer.
2963 WINDOW must be a live window and defaults to the selected one.
2964 With prefix argument KILL non-nil, kill the buffer instead of
2965 burying it.
2966
2967 According to information stored in WINDOW's `quit-restore' window
2968 parameter either \(1) delete WINDOW and its frame, \(2) delete
2969 WINDOW, \(3) restore the buffer previously displayed in WINDOW,
2970 or \(4) make WINDOW display some other buffer than the present
2971 one. If non-nil, reset `quit-restore' parameter to nil."
2972 (interactive "P")
2973 (setq window (window-normalize-window window t))
2974 (let* ((buffer (window-buffer window))
2975 (quit-restore (window-parameter window 'quit-restore))
2976 (prev-buffer
2977 (let* ((prev-buffers (window-prev-buffers window))
2978 (prev-buffer (caar prev-buffers)))
2979 (and (or (not (eq prev-buffer buffer))
2980 (and (cdr prev-buffers)
2981 (not (eq (setq prev-buffer (cadr prev-buffers))
2982 buffer))))
2983 prev-buffer)))
2984 quad resize)
2985 (cond
2986 ((and (not prev-buffer)
2987 (memq (nth 1 quit-restore) '(window frame))
2988 (eq (nth 3 quit-restore) buffer)
2989 ;; Delete WINDOW if possible.
2990 (window--delete window nil kill))
2991 ;; If the previously selected window is still alive, select it.
2992 (when (window-live-p (nth 2 quit-restore))
2993 (select-window (nth 2 quit-restore))))
2994 ((and (listp (setq quad (nth 1 quit-restore)))
2995 (buffer-live-p (car quad))
2996 (eq (nth 3 quit-restore) buffer))
2997 ;; Show another buffer stored in quit-restore parameter.
2998 (setq resize (with-current-buffer buffer
2999 (and temp-buffer-resize-mode
3000 (/= (nth 3 quad) (window-total-size window)))))
3001 (set-window-dedicated-p window nil)
3002 (when resize
3003 ;; Try to resize WINDOW to its old height but don't signal an
3004 ;; error.
3005 (condition-case nil
3006 (window-resize window (- (nth 3 quad) (window-total-size window)))
3007 (error nil)))
3008 ;; Restore WINDOW's previous buffer, start and point position.
3009 (set-window-buffer-start-and-point
3010 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
3011 ;; Unrecord WINDOW's buffer here (Bug#9937) to make sure it's not
3012 ;; re-recorded by `set-window-buffer'.
3013 (unrecord-window-buffer window buffer)
3014 ;; Reset the quit-restore parameter.
3015 (set-window-parameter window 'quit-restore nil)
3016 ;; Select old window.
3017 (when (window-live-p (nth 2 quit-restore))
3018 (select-window (nth 2 quit-restore))))
3019 (t
3020 ;; Show some other buffer in WINDOW and reset the quit-restore
3021 ;; parameter.
3022 (set-window-parameter window 'quit-restore nil)
3023 ;; Make sure that WINDOW is no more dedicated.
3024 (set-window-dedicated-p window nil)
3025 (switch-to-prev-buffer window 'bury-or-kill)))
3026
3027 ;; Kill WINDOW's old-buffer if requested
3028 (if kill
3029 (kill-buffer buffer)
3030 (bury-buffer-internal buffer))))
3031
3032 (defun quit-windows-on (&optional buffer-or-name kill frame)
3033 "Quit all windows showing BUFFER-OR-NAME.
3034 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3035 and defaults to the current buffer. Optional argument KILL
3036 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
3037 BUFFER-OR-NAME. Optional argument FRAME is handled as by
3038 `delete-windows-on'.
3039
3040 This function calls `quit-window' on all candidate windows
3041 showing BUFFER-OR-NAME."
3042 (interactive "BQuit windows on (buffer):\nP")
3043 (let ((buffer (window-normalize-buffer buffer-or-name))
3044 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3045 ;; `window-list-1' based function.
3046 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
3047 (dolist (window (window-list-1 nil nil all-frames))
3048 (if (eq (window-buffer window) buffer)
3049 (quit-window kill window)
3050 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3051 (unrecord-window-buffer window buffer)))))
3052 \f
3053 ;;; Splitting windows.
3054 (defsubst window-split-min-size (&optional horizontal)
3055 "Return minimum height of any window when splitting windows.
3056 Optional argument HORIZONTAL non-nil means return minimum width."
3057 (if horizontal
3058 (max window-min-width window-safe-min-width)
3059 (max window-min-height window-safe-min-height)))
3060
3061 (defun split-window (&optional window size side)
3062 "Make a new window adjacent to WINDOW.
3063 WINDOW can be any window and defaults to the selected one.
3064 Return the new window which is always a live window.
3065
3066 Optional argument SIZE a positive number means make WINDOW SIZE
3067 lines or columns tall. If SIZE is negative, make the new window
3068 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
3069 absolute value can be less than `window-min-height' or
3070 `window-min-width'; so this command can make a new window as
3071 small as one line or two columns. SIZE defaults to half of
3072 WINDOW's size. Interactively, SIZE is the prefix argument.
3073
3074 Optional third argument SIDE nil (or `below') specifies that the
3075 new window shall be located below WINDOW. SIDE `above' means the
3076 new window shall be located above WINDOW. In both cases SIZE
3077 specifies the new number of lines for WINDOW \(or the new window
3078 if SIZE is negative) including space reserved for the mode and/or
3079 header line.
3080
3081 SIDE t (or `right') specifies that the new window shall be
3082 located on the right side of WINDOW. SIDE `left' means the new
3083 window shall be located on the left of WINDOW. In both cases
3084 SIZE specifies the new number of columns for WINDOW \(or the new
3085 window provided SIZE is negative) including space reserved for
3086 fringes and the scrollbar or a divider column. Any other non-nil
3087 value for SIDE is currently handled like t (or `right').
3088
3089 If the variable `ignore-window-parameters' is non-nil or the
3090 `split-window' parameter of WINDOW equals t, do not process any
3091 parameters of WINDOW. Otherwise, if the `split-window' parameter
3092 of WINDOW specifies a function, call that function with all three
3093 arguments and return the value returned by that function.
3094
3095 Otherwise, if WINDOW is part of an atomic window, \"split\" the
3096 root of that atomic window. The new window does not become a
3097 member of that atomic window.
3098
3099 If WINDOW is live, properties of the new window like margins and
3100 scrollbars are inherited from WINDOW. If WINDOW is an internal
3101 window, these properties as well as the buffer displayed in the
3102 new window are inherited from the window selected on WINDOW's
3103 frame. The selected window is not changed by this function."
3104 (interactive "i")
3105 (setq window (window-normalize-window window))
3106 (let* ((side (cond
3107 ((not side) 'below)
3108 ((memq side '(below above right left)) side)
3109 (t 'right)))
3110 (horizontal (not (memq side '(nil below above))))
3111 (frame (window-frame window))
3112 (parent (window-parent window))
3113 (function (window-parameter window 'split-window))
3114 (window-side (window-parameter window 'window-side))
3115 ;; Rebind `window-nest' since in some cases we may have to
3116 ;; override its value.
3117 (window-nest window-nest)
3118 atom-root)
3119
3120 (window-check frame)
3121 (catch 'done
3122 (cond
3123 ;; Ignore window parameters if either `ignore-window-parameters'
3124 ;; is t or the `split-window' parameter equals t.
3125 ((or ignore-window-parameters (eq function t)))
3126 ((functionp function)
3127 ;; The `split-window' parameter specifies the function to call.
3128 ;; If that function is `ignore', do nothing.
3129 (throw 'done (funcall function window size side)))
3130 ;; If WINDOW is a subwindow of an atomic window, split the root
3131 ;; window of that atomic window instead.
3132 ((and (window-parameter window 'window-atom)
3133 (setq atom-root (window-atom-root window))
3134 (not (eq atom-root window)))
3135 (throw 'done (split-window atom-root size side))))
3136
3137 (when (and window-side
3138 (or (not parent)
3139 (not (window-parameter parent 'window-side))))
3140 ;; WINDOW is a side root window. To make sure that a new parent
3141 ;; window gets created set `window-nest' to t.
3142 (setq window-nest t))
3143
3144 (when (and window-splits size (> size 0))
3145 ;; If `window-splits' is non-nil and SIZE is a non-negative
3146 ;; integer, we cannot reasonably resize other windows. Rather
3147 ;; bind `window-nest' to t to make sure that subsequent window
3148 ;; deletions are handled correctly.
3149 (setq window-nest t))
3150
3151 (let* ((parent-size
3152 ;; `parent-size' is the size of WINDOW's parent, provided
3153 ;; it has one.
3154 (when parent (window-total-size parent horizontal)))
3155 ;; `resize' non-nil means we are supposed to resize other
3156 ;; windows in WINDOW's combination.
3157 (resize
3158 (and window-splits (not window-nest)
3159 ;; Resize makes sense in iso-combinations only.
3160 (window-combined-p window horizontal)))
3161 ;; `old-size' is the current size of WINDOW.
3162 (old-size (window-total-size window horizontal))
3163 ;; `new-size' is the specified or calculated size of the
3164 ;; new window.
3165 (new-size
3166 (cond
3167 ((not size)
3168 (max (window-split-min-size horizontal)
3169 (if resize
3170 ;; When resizing try to give the new window the
3171 ;; average size of a window in its combination.
3172 (min (- parent-size
3173 (window-min-size parent horizontal))
3174 (/ parent-size
3175 (1+ (window-combinations
3176 parent horizontal))))
3177 ;; Else try to give the new window half the size
3178 ;; of WINDOW (plus an eventual odd line).
3179 (+ (/ old-size 2) (% old-size 2)))))
3180 ((>= size 0)
3181 ;; SIZE non-negative specifies the new size of WINDOW.
3182
3183 ;; Note: Specifying a non-negative SIZE is practically
3184 ;; always done as workaround for making the new window
3185 ;; appear above or on the left of the new window (the
3186 ;; ispell window is a typical example of that). In all
3187 ;; these cases the SIDE argument should be set to 'above
3188 ;; or 'left in order to support the 'resize option.
3189 ;; Here we have to nest the windows instead, see above.
3190 (- old-size size))
3191 (t
3192 ;; SIZE negative specifies the size of the new window.
3193 (- size))))
3194 new-parent new-normal)
3195
3196 ;; Check SIZE.
3197 (cond
3198 ((not size)
3199 (cond
3200 (resize
3201 ;; SIZE unspecified, resizing.
3202 (when (and (not (window-sizable-p parent (- new-size) horizontal))
3203 ;; Try again with minimum split size.
3204 (setq new-size
3205 (max new-size (window-split-min-size horizontal)))
3206 (not (window-sizable-p parent (- new-size) horizontal)))
3207 (error "Window %s too small for splitting" parent)))
3208 ((> (+ new-size (window-min-size window horizontal)) old-size)
3209 ;; SIZE unspecified, no resizing.
3210 (error "Window %s too small for splitting" window))))
3211 ((and (>= size 0)
3212 (or (>= size old-size)
3213 (< new-size (if horizontal
3214 window-safe-min-width
3215 window-safe-min-width))))
3216 ;; SIZE specified as new size of old window. If the new size
3217 ;; is larger than the old size or the size of the new window
3218 ;; would be less than the safe minimum, signal an error.
3219 (error "Window %s too small for splitting" window))
3220 (resize
3221 ;; SIZE specified, resizing.
3222 (unless (window-sizable-p parent (- new-size) horizontal)
3223 ;; If we cannot resize the parent give up.
3224 (error "Window %s too small for splitting" parent)))
3225 ((or (< new-size
3226 (if horizontal window-safe-min-width window-safe-min-height))
3227 (< (- old-size new-size)
3228 (if horizontal window-safe-min-width window-safe-min-height)))
3229 ;; SIZE specification violates minimum size restrictions.
3230 (error "Window %s too small for splitting" window)))
3231
3232 (window--resize-reset frame horizontal)
3233
3234 (setq new-parent
3235 ;; Make new-parent non-nil if we need a new parent window;
3236 ;; either because we want to nest or because WINDOW is not
3237 ;; iso-combined.
3238 (or window-nest (not (window-combined-p window horizontal))))
3239 (setq new-normal
3240 ;; Make new-normal the normal size of the new window.
3241 (cond
3242 (size (/ (float new-size) (if new-parent old-size parent-size)))
3243 (new-parent 0.5)
3244 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
3245 (t (/ (window-normal-size window horizontal) 2.0))))
3246
3247 (if resize
3248 ;; Try to get space from OLD's siblings. We could go "up" and
3249 ;; try getting additional space from surrounding windows but
3250 ;; we won't be able to return space to those windows when we
3251 ;; delete the one we create here. Hence we do not go up.
3252 (progn
3253 (window--resize-subwindows parent (- new-size) horizontal)
3254 (let* ((normal (- 1.0 new-normal))
3255 (sub (window-child parent)))
3256 (while sub
3257 (set-window-new-normal
3258 sub (* (window-normal-size sub horizontal) normal))
3259 (setq sub (window-right sub)))))
3260 ;; Get entire space from WINDOW.
3261 (set-window-new-total window (- old-size new-size))
3262 (window--resize-this-window window (- new-size) horizontal)
3263 (set-window-new-normal
3264 window (- (if new-parent 1.0 (window-normal-size window horizontal))
3265 new-normal)))
3266
3267 (let* ((new (split-window-internal window new-size side new-normal)))
3268 ;; Inherit window-side parameters, if any.
3269 (when (and window-side new-parent)
3270 (set-window-parameter (window-parent new) 'window-side window-side)
3271 (set-window-parameter new 'window-side window-side))
3272
3273 (run-window-configuration-change-hook frame)
3274 (window-check frame)
3275 ;; Always return the new window.
3276 new)))))
3277
3278 ;; I think this should be the default; I think people will prefer it--rms.
3279 (defcustom split-window-keep-point t
3280 "If non-nil, \\[split-window-below] preserves point in the new window.
3281 If nil, adjust point in the two windows to minimize redisplay.
3282 This option applies only to `split-window-below' and functions
3283 that call it. The low-level `split-window' function always keeps
3284 the original point in both windows."
3285 :type 'boolean
3286 :group 'windows)
3287
3288 (defun split-window-below (&optional size)
3289 "Split the selected window into two windows, one above the other.
3290 The selected window is above. The newly split-off window is
3291 below, and displays the same buffer. Return the new window.
3292
3293 If optional argument SIZE is omitted or nil, both windows get the
3294 same height, or close to it. If SIZE is positive, the upper
3295 \(selected) window gets SIZE lines. If SIZE is negative, the
3296 lower (new) window gets -SIZE lines.
3297
3298 If the variable `split-window-keep-point' is non-nil, both
3299 windows get the same value of point as the selected window.
3300 Otherwise, the window starts are chosen so as to minimize the
3301 amount of redisplay; this is convenient on slow terminals."
3302 (interactive "P")
3303 (let ((old-window (selected-window))
3304 (old-point (point))
3305 (size (and size (prefix-numeric-value size)))
3306 moved-by-window-height moved new-window bottom)
3307 (when (and size (< size 0) (< (- size) window-min-height))
3308 ;; `split-window' would not signal an error here.
3309 (error "Size of new window too small"))
3310 (setq new-window (split-window nil size))
3311 (unless split-window-keep-point
3312 (with-current-buffer (window-buffer)
3313 (goto-char (window-start))
3314 (setq moved (vertical-motion (window-height)))
3315 (set-window-start new-window (point))
3316 (when (> (point) (window-point new-window))
3317 (set-window-point new-window (point)))
3318 (when (= moved (window-height))
3319 (setq moved-by-window-height t)
3320 (vertical-motion -1))
3321 (setq bottom (point)))
3322 (and moved-by-window-height
3323 (<= bottom (point))
3324 (set-window-point old-window (1- bottom)))
3325 (and moved-by-window-height
3326 (<= (window-start new-window) old-point)
3327 (set-window-point new-window old-point)
3328 (select-window new-window)))
3329 ;; Always copy quit-restore parameter in interactive use.
3330 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3331 (when quit-restore
3332 (set-window-parameter new-window 'quit-restore quit-restore)))
3333 new-window))
3334
3335 (defalias 'split-window-vertically 'split-window-below)
3336
3337 (defun split-window-right (&optional size)
3338 "Split the selected window into two side-by-side windows.
3339 The selected window is on the left. The newly split-off window
3340 is on the right, and displays the same buffer. Return the new
3341 window.
3342
3343 If optional argument SIZE is omitted or nil, both windows get the
3344 same width, or close to it. If SIZE is positive, the left-hand
3345 \(selected) window gets SIZE columns. If SIZE is negative, the
3346 right-hand (new) window gets -SIZE columns. Here, SIZE includes
3347 the width of the window's scroll bar; if there are no scroll
3348 bars, it includes the width of the divider column to the window's
3349 right, if any."
3350 (interactive "P")
3351 (let ((old-window (selected-window))
3352 (size (and size (prefix-numeric-value size)))
3353 new-window)
3354 (when (and size (< size 0) (< (- size) window-min-width))
3355 ;; `split-window' would not signal an error here.
3356 (error "Size of new window too small"))
3357 (setq new-window (split-window nil size t))
3358 ;; Always copy quit-restore parameter in interactive use.
3359 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3360 (when quit-restore
3361 (set-window-parameter new-window 'quit-restore quit-restore)))
3362 new-window))
3363
3364 (defalias 'split-window-horizontally 'split-window-right)
3365 \f
3366 ;;; Balancing windows.
3367
3368 ;; The following routine uses the recycled code from an old version of
3369 ;; `window--resize-subwindows'. It's not very pretty, but coding it the way the
3370 ;; new `window--resize-subwindows' code does would hardly make it any shorter or
3371 ;; more readable (FWIW we'd need three loops - one to calculate the
3372 ;; minimum sizes per window, one to enlarge or shrink windows until the
3373 ;; new parent-size matches, and one where we shrink the largest/enlarge
3374 ;; the smallest window).
3375 (defun balance-windows-2 (window horizontal)
3376 "Subroutine of `balance-windows-1'.
3377 WINDOW must be a vertical combination (horizontal if HORIZONTAL
3378 is non-nil."
3379 (let* ((first (window-child window))
3380 (sub first)
3381 (number-of-children 0)
3382 (parent-size (window-new-total window))
3383 (total-sum parent-size)
3384 found failed size sub-total sub-delta sub-amount rest)
3385 (while sub
3386 (setq number-of-children (1+ number-of-children))
3387 (when (window-size-fixed-p sub horizontal)
3388 (setq total-sum
3389 (- total-sum (window-total-size sub horizontal)))
3390 (set-window-new-normal sub 'ignore))
3391 (setq sub (window-right sub)))
3392
3393 (setq failed t)
3394 (while (and failed (> number-of-children 0))
3395 (setq size (/ total-sum number-of-children))
3396 (setq failed nil)
3397 (setq sub first)
3398 (while (and sub (not failed))
3399 ;; Ignore subwindows that should be ignored or are stuck.
3400 (unless (window--resize-subwindows-skip-p sub)
3401 (setq found t)
3402 (setq sub-total (window-total-size sub horizontal))
3403 (setq sub-delta (- size sub-total))
3404 (setq sub-amount
3405 (window-sizable sub sub-delta horizontal))
3406 ;; Register the new total size for this subwindow.
3407 (set-window-new-total sub (+ sub-total sub-amount))
3408 (unless (= sub-amount sub-delta)
3409 (setq total-sum (- total-sum sub-total sub-amount))
3410 (setq number-of-children (1- number-of-children))
3411 ;; We failed and need a new round.
3412 (setq failed t)
3413 (set-window-new-normal sub 'skip)))
3414 (setq sub (window-right sub))))
3415
3416 (setq rest (% total-sum number-of-children))
3417 ;; Fix rounding by trying to enlarge non-stuck windows by one line
3418 ;; (column) until `rest' is zero.
3419 (setq sub first)
3420 (while (and sub (> rest 0))
3421 (unless (window--resize-subwindows-skip-p window)
3422 (set-window-new-total sub 1 t)
3423 (setq rest (1- rest)))
3424 (setq sub (window-right sub)))
3425
3426 ;; Fix rounding by trying to enlarge stuck windows by one line
3427 ;; (column) until `rest' equals zero.
3428 (setq sub first)
3429 (while (and sub (> rest 0))
3430 (unless (eq (window-new-normal sub) 'ignore)
3431 (set-window-new-total sub 1 t)
3432 (setq rest (1- rest)))
3433 (setq sub (window-right sub)))
3434
3435 (setq sub first)
3436 (while sub
3437 ;; Record new normal sizes.
3438 (set-window-new-normal
3439 sub (/ (if (eq (window-new-normal sub) 'ignore)
3440 (window-total-size sub horizontal)
3441 (window-new-total sub))
3442 (float parent-size)))
3443 ;; Recursively balance each subwindow's subwindows.
3444 (balance-windows-1 sub horizontal)
3445 (setq sub (window-right sub)))))
3446
3447 (defun balance-windows-1 (window &optional horizontal)
3448 "Subroutine of `balance-windows'."
3449 (if (window-child window)
3450 (let ((sub (window-child window)))
3451 (if (window-combined-p sub horizontal)
3452 (balance-windows-2 window horizontal)
3453 (let ((size (window-new-total window)))
3454 (while sub
3455 (set-window-new-total sub size)
3456 (balance-windows-1 sub horizontal)
3457 (setq sub (window-right sub))))))))
3458
3459 (defun balance-windows (&optional window-or-frame)
3460 "Balance the sizes of subwindows of WINDOW-OR-FRAME.
3461 WINDOW-OR-FRAME is optional and defaults to the selected frame.
3462 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
3463 subwindows of that frame's root window. If WINDOW-OR-FRAME
3464 denots a window, balance the sizes of all subwindows of that
3465 window."
3466 (interactive)
3467 (let* ((window
3468 (cond
3469 ((or (not window-or-frame)
3470 (frame-live-p window-or-frame))
3471 (frame-root-window window-or-frame))
3472 ((or (window-live-p window-or-frame)
3473 (window-child window-or-frame))
3474 window-or-frame)
3475 (t
3476 (error "Not a window or frame %s" window-or-frame))))
3477 (frame (window-frame window)))
3478 ;; Balance vertically.
3479 (window--resize-reset (window-frame window))
3480 (balance-windows-1 window)
3481 (window-resize-apply frame)
3482 ;; Balance horizontally.
3483 (window--resize-reset (window-frame window) t)
3484 (balance-windows-1 window t)
3485 (window-resize-apply frame t)))
3486
3487 (defun window-fixed-size-p (&optional window direction)
3488 "Return t if WINDOW cannot be resized in DIRECTION.
3489 WINDOW defaults to the selected window. DIRECTION can be
3490 nil (i.e. any), `height' or `width'."
3491 (with-current-buffer (window-buffer window)
3492 (when (and (boundp 'window-size-fixed) window-size-fixed)
3493 (not (and direction
3494 (member (cons direction window-size-fixed)
3495 '((height . width) (width . height))))))))
3496
3497 ;;; A different solution to balance-windows.
3498 (defvar window-area-factor 1
3499 "Factor by which the window area should be over-estimated.
3500 This is used by `balance-windows-area'.
3501 Changing this globally has no effect.")
3502 (make-variable-buffer-local 'window-area-factor)
3503
3504 (defun balance-windows-area-adjust (window delta horizontal)
3505 "Wrapper around `window-resize' with error checking.
3506 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
3507 ;; `window-resize' may fail if delta is too large.
3508 (while (>= (abs delta) 1)
3509 (condition-case nil
3510 (progn
3511 (window-resize window delta horizontal)
3512 (setq delta 0))
3513 (error
3514 ;;(message "adjust: %s" (error-message-string err))
3515 (setq delta (/ delta 2))))))
3516
3517 (defun balance-windows-area ()
3518 "Make all visible windows the same area (approximately).
3519 See also `window-area-factor' to change the relative size of
3520 specific buffers."
3521 (interactive)
3522 (let* ((unchanged 0) (carry 0) (round 0)
3523 ;; Remove fixed-size windows.
3524 (wins (delq nil (mapcar (lambda (win)
3525 (if (not (window-fixed-size-p win)) win))
3526 (window-list nil 'nomini))))
3527 (changelog nil)
3528 next)
3529 ;; Resizing a window changes the size of surrounding windows in complex
3530 ;; ways, so it's difficult to balance them all. The introduction of
3531 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
3532 ;; very difficult to do. `balance-window' above takes an off-line
3533 ;; approach: get the whole window tree, then balance it, then try to
3534 ;; adjust the windows so they fit the result.
3535 ;; Here, instead, we take a "local optimization" approach, where we just
3536 ;; go through all the windows several times until nothing needs to be
3537 ;; changed. The main problem with this approach is that it's difficult
3538 ;; to make sure it terminates, so we use some heuristic to try and break
3539 ;; off infinite loops.
3540 ;; After a round without any change, we allow a second, to give a chance
3541 ;; to the carry to propagate a minor imbalance from the end back to
3542 ;; the beginning.
3543 (while (< unchanged 2)
3544 ;; (message "New round")
3545 (setq unchanged (1+ unchanged) round (1+ round))
3546 (dolist (win wins)
3547 (setq next win)
3548 (while (progn (setq next (next-window next))
3549 (window-fixed-size-p next)))
3550 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
3551 (let* ((horiz
3552 (< (car (window-edges win)) (car (window-edges next))))
3553 (areadiff (/ (- (* (window-height next) (window-width next)
3554 (buffer-local-value 'window-area-factor
3555 (window-buffer next)))
3556 (* (window-height win) (window-width win)
3557 (buffer-local-value 'window-area-factor
3558 (window-buffer win))))
3559 (max (buffer-local-value 'window-area-factor
3560 (window-buffer win))
3561 (buffer-local-value 'window-area-factor
3562 (window-buffer next)))))
3563 (edgesize (if horiz
3564 (+ (window-height win) (window-height next))
3565 (+ (window-width win) (window-width next))))
3566 (diff (/ areadiff edgesize)))
3567 (when (zerop diff)
3568 ;; Maybe diff is actually closer to 1 than to 0.
3569 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
3570 (when (and (zerop diff) (not (zerop areadiff)))
3571 (setq diff (/ (+ areadiff carry) edgesize))
3572 ;; Change things smoothly.
3573 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
3574 (if (zerop diff)
3575 ;; Make sure negligible differences don't accumulate to
3576 ;; become significant.
3577 (setq carry (+ carry areadiff))
3578 ;; This used `adjust-window-trailing-edge' before and uses
3579 ;; `window-resize' now. Error wrapping is still needed.
3580 (balance-windows-area-adjust win diff horiz)
3581 ;; (sit-for 0.5)
3582 (let ((change (cons win (window-edges win))))
3583 ;; If the same change has been seen already for this window,
3584 ;; we're most likely in an endless loop, so don't count it as
3585 ;; a change.
3586 (unless (member change changelog)
3587 (push change changelog)
3588 (setq unchanged 0 carry 0)))))))
3589 ;; We've now basically balanced all the windows.
3590 ;; But there may be some minor off-by-one imbalance left over,
3591 ;; so let's do some fine tuning.
3592 ;; (bw-finetune wins)
3593 ;; (message "Done in %d rounds" round)
3594 ))
3595
3596 ;;; Window states, how to get them and how to put them in a window.
3597 (defsubst window-list-no-nils (&rest args)
3598 "Like LIST but do not add nil elements of ARGS."
3599 (delq nil (apply 'list args)))
3600
3601 (defvar window-state-ignored-parameters '(quit-restore)
3602 "List of window parameters ignored by `window-state-get'.")
3603
3604 (defun window-state-get-1 (window &optional markers)
3605 "Helper function for `window-state-get'."
3606 (let* ((type
3607 (cond
3608 ((window-top-child window) 'vc)
3609 ((window-left-child window) 'hc)
3610 (t 'leaf)))
3611 (buffer (window-buffer window))
3612 (selected (eq window (selected-window)))
3613 (head
3614 (window-list-no-nils
3615 type
3616 (unless (window-next-sibling window) (cons 'last t))
3617 (cons 'total-height (window-total-size window))
3618 (cons 'total-width (window-total-size window t))
3619 (cons 'normal-height (window-normal-size window))
3620 (cons 'normal-width (window-normal-size window t))
3621 (cons 'splits (window-splits window))
3622 (cons 'nest (window-nest window))
3623 (let (list)
3624 (dolist (parameter (window-parameters window))
3625 (unless (memq (car parameter)
3626 window-state-ignored-parameters)
3627 (setq list (cons parameter list))))
3628 (unless (window-parameter window 'clone-of)
3629 ;; Make a clone-of parameter.
3630 (setq list (cons (cons 'clone-of window) list)))
3631 (when list
3632 (cons 'parameters list)))
3633 (when buffer
3634 ;; All buffer related things go in here - make the buffer
3635 ;; current when retrieving `point' and `mark'.
3636 (with-current-buffer (window-buffer window)
3637 (let ((point (window-point-1 window))
3638 (start (window-start window))
3639 (mark (mark)))
3640 (window-list-no-nils
3641 'buffer (buffer-name buffer)
3642 (cons 'selected selected)
3643 (when window-size-fixed (cons 'size-fixed window-size-fixed))
3644 (cons 'hscroll (window-hscroll window))
3645 (cons 'fringes (window-fringes window))
3646 (cons 'margins (window-margins window))
3647 (cons 'scroll-bars (window-scroll-bars window))
3648 (cons 'vscroll (window-vscroll window))
3649 (cons 'dedicated (window-dedicated-p window))
3650 (cons 'point (if markers (copy-marker point) point))
3651 (cons 'start (if markers (copy-marker start) start))
3652 (when mark
3653 (cons 'mark (if markers (copy-marker mark) mark)))))))))
3654 (tail
3655 (when (memq type '(vc hc))
3656 (let (list)
3657 (setq window (window-child window))
3658 (while window
3659 (setq list (cons (window-state-get-1 window markers) list))
3660 (setq window (window-right window)))
3661 (nreverse list)))))
3662 (append head tail)))
3663
3664 (defun window-state-get (&optional window markers)
3665 "Return state of WINDOW as a Lisp object.
3666 WINDOW can be any window and defaults to the root window of the
3667 selected frame.
3668
3669 Optional argument MARKERS non-nil means use markers for sampling
3670 positions like `window-point' or `window-start'. MARKERS should
3671 be non-nil only if the value is used for putting the state back
3672 in the same session (note that markers slow down processing).
3673
3674 The return value can be used as argument for `window-state-put'
3675 to put the state recorded here into an arbitrary window. The
3676 value can be also stored on disk and read back in a new session."
3677 (setq window
3678 (if window
3679 (if (window-valid-p window)
3680 window
3681 (error "%s is not a live or internal window" window))
3682 (frame-root-window)))
3683 ;; The return value is a cons whose car specifies some constraints on
3684 ;; the size of WINDOW. The cdr lists the states of the subwindows of
3685 ;; WINDOW.
3686 (cons
3687 ;; Frame related things would go into a function, say `frame-state',
3688 ;; calling `window-state-get' to insert the frame's root window.
3689 (window-list-no-nils
3690 (cons 'min-height (window-min-size window))
3691 (cons 'min-width (window-min-size window t))
3692 (cons 'min-height-ignore (window-min-size window nil t))
3693 (cons 'min-width-ignore (window-min-size window t t))
3694 (cons 'min-height-safe (window-min-size window nil 'safe))
3695 (cons 'min-width-safe (window-min-size window t 'safe))
3696 ;; These are probably not needed.
3697 (when (window-size-fixed-p window) (cons 'fixed-height t))
3698 (when (window-size-fixed-p window t) (cons 'fixed-width t)))
3699 (window-state-get-1 window markers)))
3700
3701 (defvar window-state-put-list nil
3702 "Helper variable for `window-state-put'.")
3703
3704 (defun window-state-put-1 (state &optional window ignore totals)
3705 "Helper function for `window-state-put'."
3706 (let ((type (car state)))
3707 (setq state (cdr state))
3708 (cond
3709 ((eq type 'leaf)
3710 ;; For a leaf window just add unprocessed entries to
3711 ;; `window-state-put-list'.
3712 (setq window-state-put-list
3713 (cons (cons window state) window-state-put-list)))
3714 ((memq type '(vc hc))
3715 (let* ((horizontal (eq type 'hc))
3716 (total (window-total-size window horizontal))
3717 (first t)
3718 size new)
3719 (dolist (item state)
3720 ;; Find the next child window. WINDOW always points to the
3721 ;; real window that we want to fill with what we find here.
3722 (when (memq (car item) '(leaf vc hc))
3723 (if (assq 'last item)
3724 ;; The last child window. Below `window-state-put-1'
3725 ;; will put into it whatever ITEM has in store.
3726 (setq new nil)
3727 ;; Not the last child window, prepare for splitting
3728 ;; WINDOW. SIZE is the new (and final) size of the old
3729 ;; window.
3730 (setq size
3731 (if totals
3732 ;; Use total size.
3733 (cdr (assq (if horizontal 'total-width 'total-height) item))
3734 ;; Use normalized size and round.
3735 (round (* total
3736 (cdr (assq
3737 (if horizontal 'normal-width 'normal-height)
3738 item))))))
3739
3740 ;; Use safe sizes, we try to resize later.
3741 (setq size (max size (if horizontal
3742 window-safe-min-height
3743 window-safe-min-width)))
3744
3745 (if (window-sizable-p window (- size) horizontal 'safe)
3746 (let* ((window-nest (assq 'nest item)))
3747 ;; We must inherit the nesting, otherwise we might mess
3748 ;; up handling of atomic and side window.
3749 (setq new (split-window window size horizontal)))
3750 ;; Give up if we can't resize window down to safe sizes.
3751 (error "Cannot resize window %s" window))
3752
3753 (when first
3754 (setq first nil)
3755 ;; When creating the first child window add for parent
3756 ;; unprocessed entries to `window-state-put-list'.
3757 (setq window-state-put-list
3758 (cons (cons (window-parent window) state)
3759 window-state-put-list))))
3760
3761 ;; Now process the current window (either the one we've just
3762 ;; split or the last child of its parent).
3763 (window-state-put-1 item window ignore totals)
3764 ;; Continue with the last window split off.
3765 (setq window new))))))))
3766
3767 (defun window-state-put-2 (ignore)
3768 "Helper function for `window-state-put'."
3769 (dolist (item window-state-put-list)
3770 (let ((window (car item))
3771 (splits (cdr (assq 'splits item)))
3772 (nest (cdr (assq 'nest item)))
3773 (parameters (cdr (assq 'parameters item)))
3774 (state (cdr (assq 'buffer item))))
3775 (when splits (set-window-splits window splits))
3776 (when nest (set-window-nest window nest))
3777 ;; Process parameters.
3778 (when parameters
3779 (dolist (parameter parameters)
3780 (set-window-parameter window (car parameter) (cdr parameter))))
3781 ;; Process buffer related state.
3782 (when state
3783 ;; We don't want to raise an error here so we create a buffer if
3784 ;; there's none.
3785 (set-window-buffer window (get-buffer-create (car state)))
3786 (with-current-buffer (window-buffer window)
3787 (set-window-hscroll window (cdr (assq 'hscroll state)))
3788 (apply 'set-window-fringes
3789 (cons window (cdr (assq 'fringes state))))
3790 (let ((margins (cdr (assq 'margins state))))
3791 (set-window-margins window (car margins) (cdr margins)))
3792 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
3793 (set-window-scroll-bars
3794 window (car scroll-bars) (nth 2 scroll-bars) (nth 3 scroll-bars)))
3795 (set-window-vscroll window (cdr (assq 'vscroll state)))
3796 ;; Adjust vertically.
3797 (if (memq window-size-fixed '(t height))
3798 ;; A fixed height window, try to restore the original size.
3799 (let ((delta (- (cdr (assq 'total-height item))
3800 (window-total-height window)))
3801 window-size-fixed)
3802 (when (window-resizable-p window delta)
3803 (window-resize window delta)))
3804 ;; Else check whether the window is not high enough.
3805 (let* ((min-size (window-min-size window nil ignore))
3806 (delta (- min-size (window-total-size window))))
3807 (when (and (> delta 0)
3808 (window-resizable-p window delta nil ignore))
3809 (window-resize window delta nil ignore))))
3810 ;; Adjust horizontally.
3811 (if (memq window-size-fixed '(t width))
3812 ;; A fixed width window, try to restore the original size.
3813 (let ((delta (- (cdr (assq 'total-width item))
3814 (window-total-width window)))
3815 window-size-fixed)
3816 (when (window-resizable-p window delta)
3817 (window-resize window delta)))
3818 ;; Else check whether the window is not wide enough.
3819 (let* ((min-size (window-min-size window t ignore))
3820 (delta (- min-size (window-total-size window t))))
3821 (when (and (> delta 0)
3822 (window-resizable-p window delta t ignore))
3823 (window-resize window delta t ignore))))
3824 ;; Set dedicated status.
3825 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
3826 ;; Install positions (maybe we should do this after all windows
3827 ;; have been created and sized).
3828 (ignore-errors
3829 (set-window-start window (cdr (assq 'start state)))
3830 (set-window-point window (cdr (assq 'point state)))
3831 ;; I'm not sure whether we should set the mark here, but maybe
3832 ;; it can be used.
3833 (let ((mark (cdr (assq 'mark state))))
3834 (when mark (set-mark mark))))
3835 ;; Select window if it's the selected one.
3836 (when (cdr (assq 'selected state))
3837 (select-window window)))))))
3838
3839 (defun window-state-put (state &optional window ignore)
3840 "Put window state STATE into WINDOW.
3841 STATE should be the state of a window returned by an earlier
3842 invocation of `window-state-get'. Optional argument WINDOW must
3843 specify a live window and defaults to the selected one.
3844
3845 Optional argument IGNORE non-nil means ignore minimum window
3846 sizes and fixed size restrictions. IGNORE equal `safe' means
3847 subwindows can get as small as `window-safe-min-height' and
3848 `window-safe-min-width'."
3849 (setq window (window-normalize-window window t))
3850 (let* ((frame (window-frame window))
3851 (head (car state))
3852 ;; We check here (1) whether the total sizes of root window of
3853 ;; STATE and that of WINDOW are equal so we can avoid
3854 ;; calculating new sizes, and (2) if we do have to resize
3855 ;; whether we can do so without violating size restrictions.
3856 (totals
3857 (and (= (window-total-size window)
3858 (cdr (assq 'total-height state)))
3859 (= (window-total-size window t)
3860 (cdr (assq 'total-width state)))))
3861 (min-height (cdr (assq 'min-height head)))
3862 (min-width (cdr (assq 'min-width head)))
3863 window-splits selected)
3864 (if (and (not totals)
3865 (or (> min-height (window-total-size window))
3866 (> min-width (window-total-size window t)))
3867 (or (not ignore)
3868 (and (setq min-height
3869 (cdr (assq 'min-height-ignore head)))
3870 (setq min-width
3871 (cdr (assq 'min-width-ignore head)))
3872 (or (> min-height (window-total-size window))
3873 (> min-width (window-total-size window t)))
3874 (or (not (eq ignore 'safe))
3875 (and (setq min-height
3876 (cdr (assq 'min-height-safe head)))
3877 (setq min-width
3878 (cdr (assq 'min-width-safe head)))
3879 (or (> min-height
3880 (window-total-size window))
3881 (> min-width
3882 (window-total-size window t))))))))
3883 ;; The check above might not catch all errors due to rounding
3884 ;; issues - so IGNORE equal 'safe might not always produce the
3885 ;; minimum possible state. But such configurations hardly make
3886 ;; sense anyway.
3887 (error "Window %s too small to accommodate state" window)
3888 (setq state (cdr state))
3889 (setq window-state-put-list nil)
3890 ;; Work on the windows of a temporary buffer to make sure that
3891 ;; splitting proceeds regardless of any buffer local values of
3892 ;; `window-size-fixed'. Release that buffer after the buffers of
3893 ;; all live windows have been set by `window-state-put-2'.
3894 (with-temp-buffer
3895 (set-window-buffer window (current-buffer))
3896 (window-state-put-1 state window nil totals)
3897 (window-state-put-2 ignore))
3898 (window-check frame))))
3899 \f
3900 (defun display-buffer-record-window (type window buffer)
3901 "Record information for window used by `display-buffer'.
3902 TYPE specifies the type of the calling operation and must be one
3903 of the symbols 'reuse \(when WINDOW existed already and was
3904 reused for displaying BUFFER), 'window \(when WINDOW was created
3905 on an already existing frame), or 'frame \(when WINDOW was
3906 created on a new frame). WINDOW is the window used for or created
3907 by the `display-buffer' routines. BUFFER is the buffer that
3908 shall be displayed.
3909
3910 This function installs or updates the quit-restore parameter of
3911 WINDOW. The quit-restore parameter is a list of four elements:
3912 The first element is one of the symbols 'window, 'frame, 'same or
3913 'other. The second element is either one of the symbols 'window
3914 or 'frame or a list whose elements are the buffer previously
3915 shown in the window, that buffer's window start and window point,
3916 and the window's height. The third element is the window
3917 selected at the time the parameter was created. The fourth
3918 element is BUFFER."
3919 (cond
3920 ((eq type 'reuse)
3921 (if (eq (window-buffer window) buffer)
3922 ;; WINDOW shows BUFFER already.
3923 (when (consp (window-parameter window 'quit-restore))
3924 ;; If WINDOW has a quit-restore parameter, reset its car.
3925 (setcar (window-parameter window 'quit-restore) 'same))
3926 ;; WINDOW shows another buffer.
3927 (set-window-parameter
3928 window 'quit-restore
3929 (list 'other
3930 ;; A quadruple of WINDOW's buffer, start, point and height.
3931 (list (window-buffer window) (window-start window)
3932 (window-point-1 window) (window-total-size window))
3933 (selected-window) buffer))))
3934 ((eq type 'window)
3935 ;; WINDOW has been created on an existing frame.
3936 (set-window-parameter
3937 window 'quit-restore
3938 (list 'window 'window (selected-window) buffer)))
3939 ((eq type 'frame)
3940 ;; WINDOW has been created on a new frame.
3941 (set-window-parameter
3942 window 'quit-restore
3943 (list 'frame 'frame (selected-window) buffer)))))
3944
3945 (defcustom display-buffer-function nil
3946 "If non-nil, function to call to handle `display-buffer'.
3947 It will receive two args, the buffer and a flag which if non-nil
3948 means that the currently selected window is not acceptable. It
3949 should choose or create a window, display the specified buffer in
3950 it, and return the window.
3951
3952 The function specified here is responsible for setting the
3953 quit-restore and help-setup parameters of the window used."
3954 :type '(choice
3955 (const nil)
3956 (function :tag "function"))
3957 :group 'windows)
3958
3959 (defcustom pop-up-frame-alist nil
3960 "Alist of parameters for automatically generated new frames.
3961 You can set this in your init file; for example,
3962
3963 (setq pop-up-frame-alist '((width . 80) (height . 20)))
3964
3965 If non-nil, the value you specify here is used by the default
3966 `pop-up-frame-function' for the creation of new frames.
3967
3968 Since `pop-up-frame-function' is used by `display-buffer' for
3969 making new frames, any value specified here by default affects
3970 the automatic generation of new frames via `display-buffer' and
3971 all functions based on it. The behavior of `make-frame' is not
3972 affected by this variable."
3973 :type '(repeat (cons :format "%v"
3974 (symbol :tag "Parameter")
3975 (sexp :tag "Value")))
3976 :group 'frames)
3977
3978 (defcustom pop-up-frame-function
3979 (lambda () (make-frame pop-up-frame-alist))
3980 "Function used by `display-buffer' for creating a new frame.
3981 This function is called with no arguments and should return a new
3982 frame. The default value calls `make-frame' with the argument
3983 `pop-up-frame-alist'."
3984 :type 'function
3985 :group 'frames)
3986
3987 (defcustom special-display-buffer-names nil
3988 "List of names of buffers that should be displayed specially.
3989 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
3990 its name is in this list, displays the buffer in a way specified
3991 by `special-display-function'. `special-display-popup-frame'
3992 \(the default for `special-display-function') usually displays
3993 the buffer in a separate frame made with the parameters specified
3994 by `special-display-frame-alist'. If `special-display-function'
3995 has been set to some other function, that function is called with
3996 the buffer as first, and nil as second argument.
3997
3998 Alternatively, an element of this list can be specified as
3999 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
4000 name and FRAME-PARAMETERS an alist of \(PARAMETER . VALUE) pairs.
4001 `special-display-popup-frame' will interpret such pairs as frame
4002 parameters when it creates a special frame, overriding the
4003 corresponding values from `special-display-frame-alist'.
4004
4005 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4006 `special-display-popup-frame' displays that buffer in the
4007 selected window. If FRAME-PARAMETERS contains (same-frame . t),
4008 it displays that buffer in a window on the selected frame.
4009
4010 If `special-display-function' specifies some other function than
4011 `special-display-popup-frame', that function is called with the
4012 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
4013 argument.
4014
4015 Finally, an element of this list can be also specified as
4016 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
4017 `special-display-popup-frame' will call FUNCTION with the buffer
4018 named BUFFER-NAME as first argument, and OTHER-ARGS as the
4019 second.
4020
4021 Any alternative function specified here is responsible for
4022 setting up the quit-restore parameter of the window used.
4023
4024 If this variable appears \"not to work\", because you added a
4025 name to it but the corresponding buffer is displayed in the
4026 selected window, look at the values of `same-window-buffer-names'
4027 and `same-window-regexps'. Those variables take precedence over
4028 this one.
4029
4030 See also `special-display-regexps'."
4031 :type '(repeat
4032 (choice :tag "Buffer"
4033 :value ""
4034 (string :format "%v")
4035 (cons :tag "With parameters"
4036 :format "%v"
4037 :value ("" . nil)
4038 (string :format "%v")
4039 (repeat :tag "Parameters"
4040 (cons :format "%v"
4041 (symbol :tag "Parameter")
4042 (sexp :tag "Value"))))
4043 (list :tag "With function"
4044 :format "%v"
4045 :value ("" . nil)
4046 (string :format "%v")
4047 (function :tag "Function")
4048 (repeat :tag "Arguments" (sexp)))))
4049 :group 'windows
4050 :group 'frames)
4051
4052 ;;;###autoload
4053 (put 'special-display-buffer-names 'risky-local-variable t)
4054
4055 (defcustom special-display-regexps nil
4056 "List of regexps saying which buffers should be displayed specially.
4057 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4058 any regexp in this list matches its name, displays it specially
4059 using `special-display-function'. `special-display-popup-frame'
4060 \(the default for `special-display-function') usually displays
4061 the buffer in a separate frame made with the parameters specified
4062 by `special-display-frame-alist'. If `special-display-function'
4063 has been set to some other function, that function is called with
4064 the buffer as first, and nil as second argument.
4065
4066 Alternatively, an element of this list can be specified as
4067 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
4068 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4069 `special-display-popup-frame' will then interpret these pairs as
4070 frame parameters when creating a special frame for a buffer whose
4071 name matches REGEXP, overriding the corresponding values from
4072 `special-display-frame-alist'.
4073
4074 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4075 `special-display-popup-frame' displays buffers matching REGEXP in
4076 the selected window. \(same-frame . t) in FRAME-PARAMETERS means
4077 to display such buffers in a window on the selected frame.
4078
4079 If `special-display-function' specifies some other function than
4080 `special-display-popup-frame', that function is called with the
4081 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
4082 as second argument.
4083
4084 Finally, an element of this list can be also specified as
4085 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
4086 will then call FUNCTION with the buffer whose name matched
4087 REGEXP as first, and OTHER-ARGS as second argument.
4088
4089 Any alternative function specified here is responsible for
4090 setting up the quit-restore parameter of the window used.
4091
4092 If this variable appears \"not to work\", because you added a
4093 name to it but the corresponding buffer is displayed in the
4094 selected window, look at the values of `same-window-buffer-names'
4095 and `same-window-regexps'. Those variables take precedence over
4096 this one.
4097
4098 See also `special-display-buffer-names'."
4099 :type '(repeat
4100 (choice :tag "Buffer"
4101 :value ""
4102 (regexp :format "%v")
4103 (cons :tag "With parameters"
4104 :format "%v"
4105 :value ("" . nil)
4106 (regexp :format "%v")
4107 (repeat :tag "Parameters"
4108 (cons :format "%v"
4109 (symbol :tag "Parameter")
4110 (sexp :tag "Value"))))
4111 (list :tag "With function"
4112 :format "%v"
4113 :value ("" . nil)
4114 (regexp :format "%v")
4115 (function :tag "Function")
4116 (repeat :tag "Arguments" (sexp)))))
4117 :group 'windows
4118 :group 'frames)
4119
4120 (defun special-display-p (buffer-name)
4121 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
4122 More precisely, return t if `special-display-buffer-names' or
4123 `special-display-regexps' contain a string entry equaling or
4124 matching BUFFER-NAME. If `special-display-buffer-names' or
4125 `special-display-regexps' contain a list entry whose car equals
4126 or matches BUFFER-NAME, the return value is the cdr of that
4127 entry."
4128 (let (tmp)
4129 (cond
4130 ((not (stringp buffer-name)))
4131 ((member buffer-name special-display-buffer-names)
4132 t)
4133 ((setq tmp (assoc buffer-name special-display-buffer-names))
4134 (cdr tmp))
4135 ((catch 'found
4136 (dolist (regexp special-display-regexps)
4137 (cond
4138 ((stringp regexp)
4139 (when (string-match-p regexp buffer-name)
4140 (throw 'found t)))
4141 ((and (consp regexp) (stringp (car regexp))
4142 (string-match-p (car regexp) buffer-name))
4143 (throw 'found (cdr regexp))))))))))
4144
4145 (defcustom special-display-frame-alist
4146 '((height . 14) (width . 80) (unsplittable . t))
4147 "Alist of parameters for special frames.
4148 Special frames are used for buffers whose names are listed in
4149 `special-display-buffer-names' and for buffers whose names match
4150 one of the regular expressions in `special-display-regexps'.
4151
4152 This variable can be set in your init file, like this:
4153
4154 (setq special-display-frame-alist '((width . 80) (height . 20)))
4155
4156 These supersede the values given in `default-frame-alist'."
4157 :type '(repeat (cons :format "%v"
4158 (symbol :tag "Parameter")
4159 (sexp :tag "Value")))
4160 :group 'frames)
4161
4162 (defun special-display-popup-frame (buffer &optional args)
4163 "Display BUFFER and return the window chosen.
4164 If BUFFER is already displayed in a visible or iconified frame,
4165 raise that frame. Otherwise, display BUFFER in a new frame.
4166
4167 Optional argument ARGS is a list specifying additional
4168 information.
4169
4170 If ARGS is an alist, use it as a list of frame parameters. If
4171 these parameters contain \(same-window . t), display BUFFER in
4172 the selected window. If they contain \(same-frame . t), display
4173 BUFFER in a window of the selected frame.
4174
4175 If ARGS is a list whose car is a symbol, use (car ARGS) as a
4176 function to do the work. Pass it BUFFER as first argument,
4177 and (cdr ARGS) as second."
4178 (if (and args (symbolp (car args)))
4179 (apply (car args) buffer (cdr args))
4180 (let ((window (get-buffer-window buffer 0)))
4181 (or
4182 ;; If we have a window already, make it visible.
4183 (when window
4184 (let ((frame (window-frame window)))
4185 (make-frame-visible frame)
4186 (raise-frame frame)
4187 (display-buffer-record-window 'reuse window buffer)
4188 window))
4189 ;; Reuse the current window if the user requested it.
4190 (when (cdr (assq 'same-window args))
4191 (condition-case nil
4192 (progn (switch-to-buffer buffer nil t) (selected-window))
4193 (error nil)))
4194 ;; Stay on the same frame if requested.
4195 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
4196 (let* ((pop-up-windows t)
4197 pop-up-frames
4198 special-display-buffer-names special-display-regexps)
4199 (display-buffer buffer)))
4200 ;; If no window yet, make one in a new frame.
4201 (let* ((frame
4202 (with-current-buffer buffer
4203 (make-frame (append args special-display-frame-alist))))
4204 (window (frame-selected-window frame)))
4205 (display-buffer-record-window 'frame window buffer)
4206 (set-window-buffer window buffer)
4207 ;; Reset list of WINDOW's previous buffers to nil.
4208 (set-window-prev-buffers window nil)
4209 (set-window-dedicated-p window t)
4210 window)))))
4211
4212 (defcustom special-display-function 'special-display-popup-frame
4213 "Function to call for displaying special buffers.
4214 This function is called with two arguments - the buffer and,
4215 optionally, a list - and should return a window displaying that
4216 buffer. The default value usually makes a separate frame for the
4217 buffer using `special-display-frame-alist' to specify the frame
4218 parameters. See the definition of `special-display-popup-frame'
4219 for how to specify such a function.
4220
4221 A buffer is special when its name is either listed in
4222 `special-display-buffer-names' or matches a regexp in
4223 `special-display-regexps'.
4224
4225 The function specified here is responsible for setting the
4226 quit-restore and help-setup parameters of the window used."
4227 :type 'function
4228 :group 'frames)
4229
4230 (defcustom same-window-buffer-names nil
4231 "List of names of buffers that should appear in the \"same\" window.
4232 `display-buffer' and `pop-to-buffer' show a buffer whose name is
4233 on this list in the selected rather than some other window.
4234
4235 An element of this list can be a cons cell instead of just a
4236 string. In that case, the cell's car must be a string specifying
4237 the buffer name. This is for compatibility with
4238 `special-display-buffer-names'; the cdr of the cons cell is
4239 ignored.
4240
4241 See also `same-window-regexps'."
4242 :type '(repeat (string :format "%v"))
4243 :group 'windows)
4244
4245 (defcustom same-window-regexps nil
4246 "List of regexps saying which buffers should appear in the \"same\" window.
4247 `display-buffer' and `pop-to-buffer' show a buffer whose name
4248 matches a regexp on this list in the selected rather than some
4249 other window.
4250
4251 An element of this list can be a cons cell instead of just a
4252 string. In that case, the cell's car must be a regexp matching
4253 the buffer name. This is for compatibility with
4254 `special-display-regexps'; the cdr of the cons cell is ignored.
4255
4256 See also `same-window-buffer-names'."
4257 :type '(repeat (regexp :format "%v"))
4258 :group 'windows)
4259
4260 (defun same-window-p (buffer-name)
4261 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
4262 This function returns non-nil if `display-buffer' or
4263 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
4264 selected rather than \(as usual\) some other window. See
4265 `same-window-buffer-names' and `same-window-regexps'."
4266 (cond
4267 ((not (stringp buffer-name)))
4268 ;; The elements of `same-window-buffer-names' can be buffer
4269 ;; names or cons cells whose cars are buffer names.
4270 ((member buffer-name same-window-buffer-names))
4271 ((assoc buffer-name same-window-buffer-names))
4272 ((catch 'found
4273 (dolist (regexp same-window-regexps)
4274 ;; The elements of `same-window-regexps' can be regexps
4275 ;; or cons cells whose cars are regexps.
4276 (when (or (and (stringp regexp)
4277 (string-match regexp buffer-name))
4278 (and (consp regexp) (stringp (car regexp))
4279 (string-match-p (car regexp) buffer-name)))
4280 (throw 'found t)))))))
4281
4282 (defcustom pop-up-frames nil
4283 "Whether `display-buffer' should make a separate frame.
4284 If nil, never make a separate frame.
4285 If the value is `graphic-only', make a separate frame
4286 on graphic displays only.
4287 Any other non-nil value means always make a separate frame."
4288 :type '(choice
4289 (const :tag "Never" nil)
4290 (const :tag "On graphic displays only" graphic-only)
4291 (const :tag "Always" t))
4292 :group 'windows)
4293
4294 (defcustom display-buffer-reuse-frames nil
4295 "Non-nil means `display-buffer' should reuse frames.
4296 If the buffer in question is already displayed in a frame, raise
4297 that frame."
4298 :type 'boolean
4299 :version "21.1"
4300 :group 'windows)
4301
4302 (defcustom pop-up-windows t
4303 "Non-nil means `display-buffer' should make a new window."
4304 :type 'boolean
4305 :group 'windows)
4306
4307 (defcustom split-window-preferred-function 'split-window-sensibly
4308 "Function called by `display-buffer' routines to split a window.
4309 This function is called with a window as single argument and is
4310 supposed to split that window and return the new window. If the
4311 window can (or shall) not be split, it is supposed to return nil.
4312 The default is to call the function `split-window-sensibly' which
4313 tries to split the window in a way which seems most suitable.
4314 You can customize the options `split-height-threshold' and/or
4315 `split-width-threshold' in order to have `split-window-sensibly'
4316 prefer either vertical or horizontal splitting.
4317
4318 If you set this to any other function, bear in mind that the
4319 `display-buffer' routines may call this function two times. The
4320 argument of the first call is the largest window on its frame.
4321 If that call fails to return a live window, the function is
4322 called again with the least recently used window as argument. If
4323 that call fails too, `display-buffer' will use an existing window
4324 to display its buffer.
4325
4326 The window selected at the time `display-buffer' was invoked is
4327 still selected when this function is called. Hence you can
4328 compare the window argument with the value of `selected-window'
4329 if you intend to split the selected window instead or if you do
4330 not want to split the selected window."
4331 :type 'function
4332 :version "23.1"
4333 :group 'windows)
4334
4335 (defcustom split-height-threshold 80
4336 "Minimum height for splitting windows sensibly.
4337 If this is an integer, `split-window-sensibly' may split a window
4338 vertically only if it has at least this many lines. If this is
4339 nil, `split-window-sensibly' is not allowed to split a window
4340 vertically. If, however, a window is the only window on its
4341 frame, `split-window-sensibly' may split it vertically
4342 disregarding the value of this variable."
4343 :type '(choice (const nil) (integer :tag "lines"))
4344 :version "23.1"
4345 :group 'windows)
4346
4347 (defcustom split-width-threshold 160
4348 "Minimum width for splitting windows sensibly.
4349 If this is an integer, `split-window-sensibly' may split a window
4350 horizontally only if it has at least this many columns. If this
4351 is nil, `split-window-sensibly' is not allowed to split a window
4352 horizontally."
4353 :type '(choice (const nil) (integer :tag "columns"))
4354 :version "23.1"
4355 :group 'windows)
4356
4357 (defun window-splittable-p (window &optional horizontal)
4358 "Return non-nil if `split-window-sensibly' may split WINDOW.
4359 Optional argument HORIZONTAL nil or omitted means check whether
4360 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
4361 non-nil means check whether WINDOW may be split horizontally.
4362
4363 WINDOW may be split vertically when the following conditions
4364 hold:
4365 - `window-size-fixed' is either nil or equals `width' for the
4366 buffer of WINDOW.
4367 - `split-height-threshold' is an integer and WINDOW is at least as
4368 high as `split-height-threshold'.
4369 - When WINDOW is split evenly, the emanating windows are at least
4370 `window-min-height' lines tall and can accommodate at least one
4371 line plus - if WINDOW has one - a mode line.
4372
4373 WINDOW may be split horizontally when the following conditions
4374 hold:
4375 - `window-size-fixed' is either nil or equals `height' for the
4376 buffer of WINDOW.
4377 - `split-width-threshold' is an integer and WINDOW is at least as
4378 wide as `split-width-threshold'.
4379 - When WINDOW is split evenly, the emanating windows are at least
4380 `window-min-width' or two (whichever is larger) columns wide."
4381 (when (window-live-p window)
4382 (with-current-buffer (window-buffer window)
4383 (if horizontal
4384 ;; A window can be split horizontally when its width is not
4385 ;; fixed, it is at least `split-width-threshold' columns wide
4386 ;; and at least twice as wide as `window-min-width' and 2 (the
4387 ;; latter value is hardcoded).
4388 (and (memq window-size-fixed '(nil height))
4389 ;; Testing `window-full-width-p' here hardly makes any
4390 ;; sense nowadays. This can be done more intuitively by
4391 ;; setting up `split-width-threshold' appropriately.
4392 (numberp split-width-threshold)
4393 (>= (window-width window)
4394 (max split-width-threshold
4395 (* 2 (max window-min-width 2)))))
4396 ;; A window can be split vertically when its height is not
4397 ;; fixed, it is at least `split-height-threshold' lines high,
4398 ;; and it is at least twice as high as `window-min-height' and 2
4399 ;; if it has a modeline or 1.
4400 (and (memq window-size-fixed '(nil width))
4401 (numberp split-height-threshold)
4402 (>= (window-height window)
4403 (max split-height-threshold
4404 (* 2 (max window-min-height
4405 (if mode-line-format 2 1))))))))))
4406
4407 (defun split-window-sensibly (window)
4408 "Split WINDOW in a way suitable for `display-buffer'.
4409 If `split-height-threshold' specifies an integer, WINDOW is at
4410 least `split-height-threshold' lines tall and can be split
4411 vertically, split WINDOW into two windows one above the other and
4412 return the lower window. Otherwise, if `split-width-threshold'
4413 specifies an integer, WINDOW is at least `split-width-threshold'
4414 columns wide and can be split horizontally, split WINDOW into two
4415 windows side by side and return the window on the right. If this
4416 can't be done either and WINDOW is the only window on its frame,
4417 try to split WINDOW vertically disregarding any value specified
4418 by `split-height-threshold'. If that succeeds, return the lower
4419 window. Return nil otherwise.
4420
4421 By default `display-buffer' routines call this function to split
4422 the largest or least recently used window. To change the default
4423 customize the option `split-window-preferred-function'.
4424
4425 You can enforce this function to not split WINDOW horizontally,
4426 by setting \(or binding) the variable `split-width-threshold' to
4427 nil. If, in addition, you set `split-height-threshold' to zero,
4428 chances increase that this function does split WINDOW vertically.
4429
4430 In order to not split WINDOW vertically, set \(or bind) the
4431 variable `split-height-threshold' to nil. Additionally, you can
4432 set `split-width-threshold' to zero to make a horizontal split
4433 more likely to occur.
4434
4435 Have a look at the function `window-splittable-p' if you want to
4436 know how `split-window-sensibly' determines whether WINDOW can be
4437 split."
4438 (or (and (window-splittable-p window)
4439 ;; Split window vertically.
4440 (with-selected-window window
4441 (split-window-below)))
4442 (and (window-splittable-p window t)
4443 ;; Split window horizontally.
4444 (with-selected-window window
4445 (split-window-right)))
4446 (and (eq window (frame-root-window (window-frame window)))
4447 (not (window-minibuffer-p window))
4448 ;; If WINDOW is the only window on its frame and is not the
4449 ;; minibuffer window, try to split it vertically disregarding
4450 ;; the value of `split-height-threshold'.
4451 (let ((split-height-threshold 0))
4452 (when (window-splittable-p window)
4453 (with-selected-window window
4454 (split-window-below)))))))
4455
4456 (defun window--try-to-split-window (window)
4457 "Try to split WINDOW.
4458 Return value returned by `split-window-preferred-function' if it
4459 represents a live window, nil otherwise."
4460 (and (window-live-p window)
4461 (not (frame-parameter (window-frame window) 'unsplittable))
4462 (let ((new-window
4463 ;; Since `split-window-preferred-function' might
4464 ;; throw an error use `condition-case'.
4465 (condition-case nil
4466 (funcall split-window-preferred-function window)
4467 (error nil))))
4468 (and (window-live-p new-window) new-window))))
4469
4470 (defun window--frame-usable-p (frame)
4471 "Return FRAME if it can be used to display a buffer."
4472 (when (frame-live-p frame)
4473 (let ((window (frame-root-window frame)))
4474 ;; `frame-root-window' may be an internal window which is considered
4475 ;; "dead" by `window-live-p'. Hence if `window' is not live we
4476 ;; implicitly know that `frame' has a visible window we can use.
4477 (unless (and (window-live-p window)
4478 (or (window-minibuffer-p window)
4479 ;; If the window is soft-dedicated, the frame is usable.
4480 ;; Actually, even if the window is really dedicated,
4481 ;; the frame is still usable by splitting it.
4482 ;; At least Emacs-22 allowed it, and it is desirable
4483 ;; when displaying same-frame windows.
4484 nil ; (eq t (window-dedicated-p window))
4485 ))
4486 frame))))
4487
4488 (defcustom even-window-heights t
4489 "If non-nil `display-buffer' will try to even window heights.
4490 Otherwise `display-buffer' will leave the window configuration
4491 alone. Heights are evened only when `display-buffer' chooses a
4492 window that appears above or below the selected window."
4493 :type 'boolean
4494 :group 'windows)
4495
4496 (defun window--even-window-heights (window)
4497 "Even heights of WINDOW and selected window.
4498 Do this only if these windows are vertically adjacent to each
4499 other, `even-window-heights' is non-nil, and the selected window
4500 is higher than WINDOW."
4501 (when (and even-window-heights
4502 (not (eq window (selected-window)))
4503 ;; Don't resize minibuffer windows.
4504 (not (window-minibuffer-p (selected-window)))
4505 (> (window-height (selected-window)) (window-height window))
4506 (eq (window-frame window) (window-frame (selected-window)))
4507 (let ((sel-edges (window-edges (selected-window)))
4508 (win-edges (window-edges window)))
4509 (and (= (nth 0 sel-edges) (nth 0 win-edges))
4510 (= (nth 2 sel-edges) (nth 2 win-edges))
4511 (or (= (nth 1 sel-edges) (nth 3 win-edges))
4512 (= (nth 3 sel-edges) (nth 1 win-edges))))))
4513 (let ((window-min-height 1))
4514 ;; Don't throw an error if we can't even window heights for
4515 ;; whatever reason.
4516 (condition-case nil
4517 (enlarge-window (/ (- (window-height window) (window-height)) 2))
4518 (error nil)))))
4519
4520 (defun window--display-buffer-1 (window)
4521 "Raise the frame containing WINDOW.
4522 Do not raise the selected frame. Return WINDOW."
4523 (let* ((frame (window-frame window))
4524 (visible (frame-visible-p frame)))
4525 (unless (or (not visible)
4526 ;; Assume the selected frame is already visible enough.
4527 (eq frame (selected-frame))
4528 ;; Assume the frame from which we invoked the minibuffer
4529 ;; is visible.
4530 (and (minibuffer-window-active-p (selected-window))
4531 (eq frame (window-frame (minibuffer-selected-window)))))
4532 (raise-frame frame))
4533 window))
4534
4535 (defun window--display-buffer-2 (buffer window &optional dedicated)
4536 "Display BUFFER in WINDOW and make its frame visible.
4537 Set `window-dedicated-p' to DEDICATED if non-nil.
4538 Return WINDOW."
4539 (when (and (buffer-live-p buffer) (window-live-p window))
4540 (set-window-buffer window buffer)
4541 (when dedicated
4542 (set-window-dedicated-p window dedicated))
4543 (window--display-buffer-1 window)))
4544
4545 (defvar display-buffer-mark-dedicated nil
4546 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
4547 The actual non-nil value of this variable will be copied to the
4548 `window-dedicated-p' flag.")
4549
4550 (defun window-normalize-buffer-to-display (buffer-or-name)
4551 "Normalize BUFFER-OR-NAME argument for buffer display functions.
4552 If BUFFER-OR-NAME is nil, return the curent buffer. Else, if a
4553 buffer specified by BUFFER-OR-NAME exists, return that buffer.
4554 If no such buffer exists, create a buffer with the name
4555 BUFFER-OR-NAME and return that buffer."
4556 (if buffer-or-name
4557 (or (get-buffer buffer-or-name)
4558 (let ((buffer (get-buffer-create buffer-or-name)))
4559 (set-buffer-major-mode buffer)
4560 buffer))
4561 (current-buffer)))
4562
4563 (defconst display-buffer--action-function-custom-type
4564 '(choice :tag "Function"
4565 (const :tag "--" ignore) ; default for insertion
4566 (const display-buffer-reuse-window)
4567 (const display-buffer-use-some-window)
4568 (const display-buffer-same-window)
4569 (const display-buffer-pop-up-frame)
4570 (const display-buffer-use-some-window)
4571 (function :tag "Other function"))
4572 "Custom type for `display-buffer' action functions.")
4573
4574 (defconst display-buffer--action-custom-type
4575 `(cons :tag "Action"
4576 (choice :tag "Action functions"
4577 ,display-buffer--action-function-custom-type
4578 (repeat
4579 :tag "List of functions"
4580 ,display-buffer--action-function-custom-type))
4581 (alist :tag "Action arguments"
4582 :key-type symbol
4583 :value-type (sexp :tag "Value")))
4584 "Custom type for `display-buffer' actions.")
4585
4586 (defvar display-buffer-overriding-action '(nil . nil)
4587 "Overriding action to perform to display a buffer.
4588 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
4589 function or a list of functions. Each function should accept 2
4590 arguments: a buffer to display and an alist similar to ALIST.
4591 See `display-buffer' for details.")
4592 (put 'display-buffer-overriding-action 'risky-local-variable t)
4593
4594 (defcustom display-buffer-alist nil
4595 "Alist of conditional actions for `display-buffer'.
4596 This is a list of elements (CONDITION . ACTION), where:
4597
4598 CONDITION is either a regexp matching buffer names, or a function
4599 that takes a buffer and returns a boolean.
4600
4601 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
4602 function or a list of functions. Each such function should
4603 accept 2 arguments: a buffer to display and an alist of the
4604 same form as ALIST. See `display-buffer' for details."
4605 :type `(alist :key-type
4606 (choice :tag "Condition"
4607 regexp
4608 (function :tag "Matcher function"))
4609 :value-type ,display-buffer--action-custom-type)
4610 :risky t
4611 :version "24.1"
4612 :group 'windows)
4613
4614 (defcustom display-buffer-base-action '(nil . nil)
4615 "User-specified default action for `display-buffer'.
4616 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
4617 function or a list of functions. Each function should accept 2
4618 arguments: a buffer to display and an alist similar to ALIST.
4619 See `display-buffer' for details."
4620 :type display-buffer--action-custom-type
4621 :risky t
4622 :version "24.1"
4623 :group 'windows)
4624
4625 (defconst display-buffer-fallback-action
4626 '((display-buffer--maybe-same-window
4627 display-buffer-reuse-window
4628 display-buffer--special
4629 display-buffer--maybe-pop-up-frame-or-window
4630 display-buffer-use-some-window
4631 ;; If all else fails, pop up a new frame.
4632 display-buffer-pop-up-frame))
4633 "Default fallback action for `display-buffer'.
4634 This is the action used by `display-buffer' if no other actions
4635 specified, e.g. by the user options `display-buffer-alist' or
4636 `display-buffer-base-action'. See `display-buffer'.")
4637 (put 'display-buffer-fallback-action 'risky-local-variable t)
4638
4639 (defun display-buffer-assq-regexp (buffer-name alist)
4640 "Retrieve ALIST entry corresponding to BUFFER-NAME."
4641 (catch 'match
4642 (dolist (entry alist)
4643 (let ((key (car entry))
4644 (value (cdr entry)))
4645 (when (or (and (stringp key)
4646 (string-match-p key buffer-name))
4647 (and (symbolp key) (functionp key)
4648 (funcall key buffer-name alist)))
4649 (throw 'match (cdr entry)))))))
4650
4651 (defvar display-buffer--same-window-action
4652 '(display-buffer-same-window
4653 (inhibit-same-window . nil))
4654 "A `display-buffer' action for displaying in the same window.")
4655 (put 'display-buffer--same-window-action 'risky-local-variable t)
4656
4657 (defvar display-buffer--other-frame-action
4658 '((display-buffer-reuse-window
4659 display-buffer--special
4660 display-buffer-pop-up-frame)
4661 (reusable-frames . 0)
4662 (inhibit-same-window . t))
4663 "A `display-buffer' action for displaying in another frame.")
4664 (put 'display-buffer--other-frame-action 'risky-local-variable t)
4665
4666 (defun display-buffer (&optional buffer-or-name action frame)
4667 "Display BUFFER-OR-NAME in some window, without selecting it.
4668 BUFFER-OR-NAME must be a buffer or the name of an existing
4669 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
4670 or nil if no such window is found.
4671
4672 Optional argument ACTION should have the form (FUNCTION . ALIST).
4673 FUNCTION is either a function or a list of functions. Each such
4674 function is called with 2 arguments: the buffer to display and an
4675 alist. It should either display the buffer and return the
4676 window, or return nil if unable to display the buffer.
4677
4678 The `display-buffer' function builds a function list and an alist
4679 from `display-buffer-overriding-action', `display-buffer-alist',
4680 the ACTION argument, `display-buffer-base-action', and
4681 `display-buffer-fallback-action' (in that order). Then it calls
4682 each function in the combined function list in turn, passing the
4683 buffer as the first argument and the combined alist as the second
4684 argument, until one of the functions returns non-nil.
4685
4686 Available action functions include:
4687 `display-buffer-same-window'
4688 `display-buffer-reuse-window'
4689 `display-buffer-pop-up-frame'
4690 `display-buffer-pop-up-window'
4691 `display-buffer-use-some-window'
4692
4693 Recognized alist entries include:
4694
4695 `inhibit-same-window' -- A non-nil value prevents the same
4696 window from being used for display.
4697
4698 `reusable-frames' -- Value specifies frame(s) to search for a
4699 window that already displays the buffer.
4700 See `display-buffer-reuse-window'.
4701
4702 The ACTION argument to `display-buffer' can also have a non-nil
4703 and non-list value. This means to display the buffer in a window
4704 other than the selected one, even if it is already displayed in
4705 the selected window. If called interactively with a prefix
4706 argument, ACTION is t.
4707
4708 Optional argument FRAME, if non-nil, acts like an additional
4709 ALIST entry (reusable-frames . FRAME), specifying the frame(s) to
4710 search for a window that is already displaying the buffer. See
4711 `display-buffer-reuse-window'."
4712 (interactive (list (read-buffer "Display buffer: " (other-buffer))
4713 (if current-prefix-arg t)))
4714 (let ((buffer (window-normalize-buffer-to-display buffer-or-name))
4715 ;; Handle the old form of the first argument.
4716 (inhibit-same-window (and action (not (listp action)))))
4717 (unless (listp action) (setq action nil))
4718 (if display-buffer-function
4719 ;; If `display-buffer-function' is defined, let it do the job.
4720 (funcall display-buffer-function buffer inhibit-same-window)
4721 ;; Otherwise, use the defined actions.
4722 (let* ((user-action
4723 (display-buffer-assq-regexp (buffer-name buffer)
4724 display-buffer-alist))
4725 ;; Extra actions from the arguments to this function:
4726 (extra-action
4727 (cons nil (append (if inhibit-same-window
4728 '((inhibit-same-window . t)))
4729 (if frame
4730 `((reusable-frames . ,frame))))))
4731 ;; Construct action function list and action alist.
4732 (actions (list display-buffer-overriding-action
4733 user-action action extra-action
4734 display-buffer-base-action
4735 display-buffer-fallback-action))
4736 (functions (apply 'append
4737 (mapcar (lambda (x)
4738 (setq x (car x))
4739 (if (functionp x) (list x) x))
4740 actions)))
4741 (alist (apply 'append (mapcar 'cdr actions)))
4742 window)
4743 (unless (buffer-live-p buffer)
4744 (error "Invalid buffer"))
4745 (while (and functions (not window))
4746 (setq window (funcall (car functions) buffer alist)
4747 functions (cdr functions)))
4748 window))))
4749
4750 (defun display-buffer-other-frame (buffer)
4751 "Display buffer BUFFER in another frame.
4752 This uses the function `display-buffer' as a subroutine; see
4753 its documentation for additional customization information."
4754 (interactive "BDisplay buffer in other frame: ")
4755 (display-buffer buffer display-buffer--other-frame-action t))
4756
4757 ;;; `display-buffer' action functions:
4758
4759 (defun display-buffer-same-window (buffer alist)
4760 "Display BUFFER in the selected window.
4761 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
4762 if the selected window is a minibuffer window or is dedicated to
4763 another buffer; in that case, return nil. Otherwise, return the
4764 selected window."
4765 (unless (or (cdr (assq 'inhibit-same-window alist))
4766 (window-minibuffer-p)
4767 (window-dedicated-p))
4768 (display-buffer-record-window 'reuse (selected-window) buffer)
4769 (window--display-buffer-2 buffer (selected-window))))
4770
4771 (defun display-buffer--maybe-same-window (buffer alist)
4772 "Conditionally display BUFFER in the selected window.
4773 If `same-window-p' returns non-nil for BUFFER's name, call
4774 `display-buffer-same-window' and return its value. Otherwise,
4775 return nil."
4776 (and (same-window-p (buffer-name buffer))
4777 (display-buffer-same-window buffer alist)))
4778
4779 (defun display-buffer-reuse-window (buffer alist)
4780 "Return a window that is already displaying BUFFER.
4781 Return nil if no usable window is found.
4782
4783 If ALIST has a non-nil `inhibit-same-window' entry, the selected
4784 window is not eligible for reuse.
4785
4786 If ALIST contains a `reusable-frames' entry, its value determines
4787 which frames to search for a reusable window:
4788 nil -- the selected frame (actually the last non-minibuffer frame)
4789 A frame -- just that frame
4790 `visible' -- all visible frames
4791 0 -- all frames on the current terminal
4792 t -- all frames.
4793
4794 If ALIST contains no `reusable-frames' entry, search just the
4795 selected frame if `display-buffer-reuse-frames' and
4796 `pop-up-frames' are both nil; search all frames on the current
4797 terminal if either of those variables is non-nil."
4798 (let* ((alist-entry (assq 'reusable-frames alist))
4799 (frames (cond (alist-entry (cdr alist-entry))
4800 ((if (eq pop-up-frames 'graphic-only)
4801 (display-graphic-p)
4802 pop-up-frames)
4803 0)
4804 (display-buffer-reuse-frames 0)
4805 (t (last-nonminibuffer-frame))))
4806 (window (if (and (eq buffer (window-buffer))
4807 (not (cdr (assq 'inhibit-same-window alist))))
4808 (selected-window)
4809 (car (delq (selected-window)
4810 (get-buffer-window-list buffer 'nomini
4811 frames))))))
4812 (when window
4813 (display-buffer-record-window 'reuse window buffer)
4814 (window--display-buffer-1 window))))
4815
4816 (defun display-buffer--special (buffer alist)
4817 "Try to display BUFFER using `special-display-function'.
4818 Call `special-display-p' on BUFFER's name, and if that returns
4819 non-nil, call `special-display-function' on BUFFER."
4820 (and special-display-function
4821 ;; `special-display-p' returns either t or a list of frame
4822 ;; parameters to pass to `special-display-function'.
4823 (let ((pars (special-display-p (buffer-name buffer))))
4824 (when pars
4825 (funcall special-display-function
4826 buffer (if (listp pars) pars))))))
4827
4828 (defun display-buffer-pop-up-frame (buffer alist)
4829 "Display BUFFER in a new frame.
4830 This works by calling `pop-up-frame-function'. If successful,
4831 return the window used; otherwise return nil."
4832 (let ((fun pop-up-frame-function)
4833 frame window)
4834 (when (and fun
4835 (setq frame (funcall fun))
4836 (setq window (frame-selected-window frame)))
4837 (display-buffer-record-window 'frame window buffer)
4838 (window--display-buffer-2 buffer window display-buffer-mark-dedicated)
4839 ;; Reset list of WINDOW's previous buffers to nil.
4840 (set-window-prev-buffers window nil)
4841 window)))
4842
4843 (defun display-buffer-pop-up-window (buffer alist)
4844 "Display BUFFER by popping up a new window.
4845 The new window is created on the selected frame, or in
4846 `last-nonminibuffer-frame' if no windows can be created there.
4847 If sucessful, return the new window; otherwise return nil."
4848 (let ((frame (or (window--frame-usable-p (selected-frame))
4849 (window--frame-usable-p (last-nonminibuffer-frame))))
4850 window)
4851 (when (and (or (not (frame-parameter frame 'unsplittable))
4852 ;; If the selected frame cannot be split, look at
4853 ;; `last-nonminibuffer-frame'.
4854 (and (eq frame (selected-frame))
4855 (setq frame (last-nonminibuffer-frame))
4856 (window--frame-usable-p frame)
4857 (not (frame-parameter frame 'unsplittable))))
4858 ;; Attempt to split largest or least recently used window.
4859 (setq window (or (window--try-to-split-window
4860 (get-largest-window frame t))
4861 (window--try-to-split-window
4862 (get-lru-window frame t)))))
4863 (display-buffer-record-window 'window window buffer)
4864 (window--display-buffer-2 buffer window display-buffer-mark-dedicated)
4865 ;; Reset list of WINDOW's previous buffers to nil.
4866 (set-window-prev-buffers window nil)
4867 window)))
4868
4869 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
4870 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
4871
4872 If `pop-up-frames' is non-nil (and not `graphic-only' on a
4873 text-only terminal), try with `display-buffer-pop-up-frame'.
4874
4875 If that cannot be done, and `pop-up-windows' is non-nil, try
4876 again with `display-buffer-pop-up-window'."
4877 (or (and (if (eq pop-up-frames 'graphic-only)
4878 (display-graphic-p)
4879 pop-up-frames)
4880 (display-buffer-pop-up-frame buffer alist))
4881 (and pop-up-windows
4882 (display-buffer-pop-up-window buffer alist))))
4883
4884 (defun display-buffer-use-some-window (buffer alist)
4885 "Display BUFFER in an existing window.
4886 Search for a usable window, set that window to the buffer, and
4887 return the window. If no suitable window is found, return nil."
4888 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
4889 (window-to-undedicate
4890 ;; When NOT-THIS-WINDOW is non-nil, temporarily dedicate the
4891 ;; selected window to its buffer, to prevent any of the
4892 ;; `get-' routines below from choosing it. (Bug#1415)
4893 (and not-this-window (not (window-dedicated-p))
4894 (set-window-dedicated-p (selected-window) t)
4895 (selected-window)))
4896 (frame (or (window--frame-usable-p (selected-frame))
4897 (window--frame-usable-p (last-nonminibuffer-frame))))
4898 window)
4899 (unwind-protect
4900 (setq window
4901 ;; Reuse an existing window.
4902 (or (get-lru-window frame)
4903 (let ((window (get-buffer-window buffer 'visible)))
4904 (unless (and not-this-window
4905 (eq window (selected-window)))
4906 window))
4907 (get-largest-window 'visible)
4908 (let ((window (get-buffer-window buffer 0)))
4909 (unless (and not-this-window
4910 (eq window (selected-window)))
4911 window))
4912 (get-largest-window 0)))
4913 (when (window-live-p window-to-undedicate)
4914 ;; Restore dedicated status of selected window.
4915 (set-window-dedicated-p window-to-undedicate nil)))
4916 (when window
4917 (display-buffer-record-window 'reuse window buffer)
4918 (window--even-window-heights window)
4919 (window--display-buffer-2 buffer window))))
4920
4921 ;;; Display + selection commands:
4922
4923 (defun pop-to-buffer (buffer &optional action norecord)
4924 "Select buffer BUFFER in some window, preferably a different one.
4925 BUFFER may be a buffer, a string (a buffer name), or nil. If it
4926 is a string not naming an existent buffer, create a buffer with
4927 that name. If BUFFER is nil, choose some other buffer. Return
4928 the buffer.
4929
4930 This uses `display-buffer' as a subroutine. The optional ACTION
4931 argument is passed to `display-buffer' as its ACTION argument.
4932 See `display-buffer' for more information. ACTION is t if called
4933 interactively with a prefix argument, which means to pop to a
4934 window other than the selected one even if the buffer is already
4935 displayed in the selected window.
4936
4937 If the window to show BUFFER is not on the selected
4938 frame, raise that window's frame and give it input focus.
4939
4940 Optional third arg NORECORD non-nil means do not put this buffer
4941 at the front of the list of recently selected ones."
4942 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
4943 (if current-prefix-arg t)))
4944 (setq buffer (window-normalize-buffer-to-switch-to buffer))
4945 (set-buffer buffer)
4946 (let* ((old-window (selected-window))
4947 (old-frame (selected-frame))
4948 (window (display-buffer buffer action))
4949 (frame (window-frame window)))
4950 ;; If we chose another frame, make sure it gets input focus.
4951 (unless (eq frame old-frame)
4952 (select-frame-set-input-focus frame norecord))
4953 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
4954 (select-window window norecord)
4955 buffer))
4956
4957 (defun pop-to-buffer-same-window (buffer &optional norecord)
4958 "Select buffer BUFFER in some window, preferably the same one.
4959 This function behaves much like `switch-to-buffer', except it
4960 displays with `special-display-function' if BUFFER has a match in
4961 `special-display-buffer-names' or `special-display-regexps'.
4962
4963 Unlike `pop-to-buffer', this function prefers using the selected
4964 window over popping up a new window or frame.
4965
4966 BUFFER may be a buffer, a string (a buffer name), or nil. If it
4967 is a string not naming an existent buffer, create a buffer with
4968 that name. If BUFFER is nil, choose some other buffer. Return
4969 the buffer.
4970
4971 NORECORD, if non-nil means do not put this buffer at the front of
4972 the list of recently selected ones."
4973 (pop-to-buffer buffer
4974 '((display-buffer--special
4975 display-buffer-same-window)
4976 (inhibit-same-window . nil))
4977 norecord))
4978
4979 (defun read-buffer-to-switch (prompt)
4980 "Read the name of a buffer to switch to, prompting with PROMPT.
4981 Return the neame of the buffer as a string.
4982
4983 This function is intended for the `switch-to-buffer' family of
4984 commands since these need to omit the name of the current buffer
4985 from the list of completions and default values."
4986 (let ((rbts-completion-table (internal-complete-buffer-except)))
4987 (minibuffer-with-setup-hook
4988 (lambda ()
4989 (setq minibuffer-completion-table rbts-completion-table)
4990 ;; Since rbts-completion-table is built dynamically, we
4991 ;; can't just add it to the default value of
4992 ;; icomplete-with-completion-tables, so we add it
4993 ;; here manually.
4994 (if (and (boundp 'icomplete-with-completion-tables)
4995 (listp icomplete-with-completion-tables))
4996 (set (make-local-variable 'icomplete-with-completion-tables)
4997 (cons rbts-completion-table
4998 icomplete-with-completion-tables))))
4999 (read-buffer prompt (other-buffer (current-buffer))
5000 (confirm-nonexistent-file-or-buffer)))))
5001
5002 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
5003 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
5004 If BUFFER-OR-NAME is nil, return the buffer returned by
5005 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
5006 exists, return that buffer. If no such buffer exists, create a
5007 buffer with the name BUFFER-OR-NAME and return that buffer."
5008 (if buffer-or-name
5009 (or (get-buffer buffer-or-name)
5010 (let ((buffer (get-buffer-create buffer-or-name)))
5011 (set-buffer-major-mode buffer)
5012 buffer))
5013 (other-buffer)))
5014
5015 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
5016 "Switch to buffer BUFFER-OR-NAME in the selected window.
5017 If called interactively, prompt for the buffer name using the
5018 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5019 determines whether to request confirmation before creating a new
5020 buffer.
5021
5022 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
5023 nil. If BUFFER-OR-NAME is a string that does not identify an
5024 existing buffer, create a buffer with that name. If
5025 BUFFER-OR-NAME is nil, switch to the buffer returned by
5026 `other-buffer'.
5027
5028 Optional argument NORECORD non-nil means do not put the buffer
5029 specified by BUFFER-OR-NAME at the front of the buffer list and
5030 do not make the window displaying it the most recently selected
5031 one.
5032
5033 If FORCE-SAME-WINDOW is non-nil, BUFFER-OR-NAME must be displayed
5034 in the selected window; signal an error if that is
5035 impossible (e.g. if the selected window is minibuffer-only). If
5036 nil, BUFFER-OR-NAME may be displayed in another window.
5037
5038 Return the buffer switched to."
5039 (interactive
5040 (list (read-buffer-to-switch "Switch to buffer: ") nil 'force-same-window))
5041 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
5042 (if (null force-same-window)
5043 (pop-to-buffer buffer display-buffer--same-window-action norecord)
5044 (cond
5045 ;; Don't call set-window-buffer if it's not needed since it
5046 ;; might signal an error (e.g. if the window is dedicated).
5047 ((eq buffer (window-buffer)))
5048 ((window-minibuffer-p)
5049 (error "Cannot switch buffers in minibuffer window"))
5050 ((eq (window-dedicated-p) t)
5051 (error "Cannot switch buffers in a dedicated window"))
5052 (t (set-window-buffer nil buffer)))
5053
5054 (unless norecord
5055 (select-window (selected-window)))
5056 (set-buffer buffer))))
5057
5058 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
5059 "Select the buffer specified by BUFFER-OR-NAME in another window.
5060 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
5061 nil. Return the buffer switched to.
5062
5063 If called interactively, prompt for the buffer name using the
5064 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5065 determines whether to request confirmation before creating a new
5066 buffer.
5067
5068 If BUFFER-OR-NAME is a string and does not identify an existing
5069 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5070 nil, switch to the buffer returned by `other-buffer'.
5071
5072 Optional second argument NORECORD non-nil means do not put this
5073 buffer at the front of the list of recently selected ones.
5074
5075 This uses the function `display-buffer' as a subroutine; see its
5076 documentation for additional customization information."
5077 (interactive
5078 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5079 (let ((pop-up-windows t))
5080 (pop-to-buffer buffer-or-name t norecord)))
5081
5082 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
5083 "Switch to buffer BUFFER-OR-NAME in another frame.
5084 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
5085 nil. Return the buffer switched to.
5086
5087 If called interactively, prompt for the buffer name using the
5088 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5089 determines whether to request confirmation before creating a new
5090 buffer.
5091
5092 If BUFFER-OR-NAME is a string and does not identify an existing
5093 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5094 nil, switch to the buffer returned by `other-buffer'.
5095
5096 Optional second arg NORECORD non-nil means do not put this
5097 buffer at the front of the list of recently selected ones.
5098
5099 This uses the function `display-buffer' as a subroutine; see its
5100 documentation for additional customization information."
5101 (interactive
5102 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
5103 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
5104 \f
5105 (defun set-window-text-height (window height)
5106 "Set the height in lines of the text display area of WINDOW to HEIGHT.
5107 WINDOW must be a live window. HEIGHT doesn't include the mode
5108 line or header line, if any, or any partial-height lines in the
5109 text display area.
5110
5111 Note that the current implementation of this function cannot
5112 always set the height exactly, but attempts to be conservative,
5113 by allocating more lines than are actually needed in the case
5114 where some error may be present."
5115 (setq window (window-normalize-window window t))
5116 (let ((delta (- height (window-text-height window))))
5117 (unless (zerop delta)
5118 ;; Setting window-min-height to a value like 1 can lead to very
5119 ;; bizarre displays because it also allows Emacs to make *other*
5120 ;; windows 1-line tall, which means that there's no more space for
5121 ;; the modeline.
5122 (let ((window-min-height (min 2 height))) ; One text line plus a modeline.
5123 (window-resize window delta)))))
5124
5125 (defun enlarge-window-horizontally (delta)
5126 "Make selected window DELTA columns wider.
5127 Interactively, if no argument is given, make selected window one
5128 column wider."
5129 (interactive "p")
5130 (enlarge-window delta t))
5131
5132 (defun shrink-window-horizontally (delta)
5133 "Make selected window DELTA columns narrower.
5134 Interactively, if no argument is given, make selected window one
5135 column narrower."
5136 (interactive "p")
5137 (shrink-window delta t))
5138
5139 (defun count-screen-lines (&optional beg end count-final-newline window)
5140 "Return the number of screen lines in the region.
5141 The number of screen lines may be different from the number of actual lines,
5142 due to line breaking, display table, etc.
5143
5144 Optional arguments BEG and END default to `point-min' and `point-max'
5145 respectively.
5146
5147 If region ends with a newline, ignore it unless optional third argument
5148 COUNT-FINAL-NEWLINE is non-nil.
5149
5150 The optional fourth argument WINDOW specifies the window used for obtaining
5151 parameters such as width, horizontal scrolling, and so on. The default is
5152 to use the selected window's parameters.
5153
5154 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
5155 regardless of which buffer is displayed in WINDOW. This makes possible to use
5156 `count-screen-lines' in any buffer, whether or not it is currently displayed
5157 in some window."
5158 (unless beg
5159 (setq beg (point-min)))
5160 (unless end
5161 (setq end (point-max)))
5162 (if (= beg end)
5163 0
5164 (save-excursion
5165 (save-restriction
5166 (widen)
5167 (narrow-to-region (min beg end)
5168 (if (and (not count-final-newline)
5169 (= ?\n (char-before (max beg end))))
5170 (1- (max beg end))
5171 (max beg end)))
5172 (goto-char (point-min))
5173 (1+ (vertical-motion (buffer-size) window))))))
5174
5175 (defun window-buffer-height (window)
5176 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
5177 (with-current-buffer (window-buffer window)
5178 (max 1
5179 (count-screen-lines (point-min) (point-max)
5180 ;; If buffer ends with a newline, ignore it when
5181 ;; counting height unless point is after it.
5182 (eobp)
5183 window))))
5184
5185 ;;; Resizing buffers to fit their contents exactly.
5186 (defun fit-window-to-buffer (&optional window max-height min-height override)
5187 "Adjust height of WINDOW to display its buffer's contents exactly.
5188 WINDOW can be any live window and defaults to the selected one.
5189
5190 Optional argument MAX-HEIGHT specifies the maximum height of
5191 WINDOW and defaults to the height of WINDOW's frame. Optional
5192 argument MIN-HEIGHT specifies the minimum height of WINDOW and
5193 defaults to `window-min-height'. Both, MAX-HEIGHT and MIN-HEIGHT
5194 are specified in lines and include the mode line and header line,
5195 if any.
5196
5197 Optional argument OVERRIDE non-nil means override restrictions
5198 imposed by `window-min-height' and `window-min-width' on the size
5199 of WINDOW.
5200
5201 Return the number of lines by which WINDOW was enlarged or
5202 shrunk. If an error occurs during resizing, return nil but don't
5203 signal an error.
5204
5205 Note that even if this function makes WINDOW large enough to show
5206 _all_ lines of its buffer you might not see the first lines when
5207 WINDOW was scrolled."
5208 (interactive)
5209 ;; Do all the work in WINDOW and its buffer and restore the selected
5210 ;; window and the current buffer when we're done.
5211 (setq window (window-normalize-window window t))
5212 ;; Can't resize a full height or fixed-size window.
5213 (unless (or (window-size-fixed-p window)
5214 (window-full-height-p window))
5215 ;; `with-selected-window' should orderly restore the current buffer.
5216 (with-selected-window window
5217 ;; We are in WINDOW's buffer now.
5218 (let* (;; Adjust MIN-HEIGHT.
5219 (min-height
5220 (if override
5221 (window-min-size window nil window)
5222 (max (or min-height window-min-height)
5223 window-safe-min-height)))
5224 (max-window-height
5225 (window-total-size (frame-root-window window)))
5226 ;; Adjust MAX-HEIGHT.
5227 (max-height
5228 (if (or override (not max-height))
5229 max-window-height
5230 (min max-height max-window-height)))
5231 ;; Make `desired-height' the height necessary to show
5232 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
5233 ;; and MAX-HEIGHT.
5234 (desired-height
5235 (max
5236 (min
5237 (+ (count-screen-lines)
5238 ;; For non-minibuffers count the mode line, if any.
5239 (if (and (not (window-minibuffer-p window))
5240 mode-line-format)
5241 1
5242 0)
5243 ;; Count the header line, if any.
5244 (if header-line-format 1 0))
5245 max-height)
5246 min-height))
5247 (desired-delta
5248 (- desired-height (window-total-size window)))
5249 (delta
5250 (if (> desired-delta 0)
5251 (min desired-delta
5252 (window-max-delta window nil window))
5253 (max desired-delta
5254 (- (window-min-delta window nil window))))))
5255 ;; This `condition-case' shouldn't be necessary, but who knows?
5256 (condition-case nil
5257 (if (zerop delta)
5258 ;; Return zero if DELTA became zero in the proces.
5259 0
5260 ;; Don't try to redisplay with the cursor at the end on its
5261 ;; own line--that would force a scroll and spoil things.
5262 (when (and (eobp) (bolp) (not (bobp)))
5263 ;; It's silly to put `point' at the end of the previous
5264 ;; line and so maybe force horizontal scrolling.
5265 (set-window-point window (line-beginning-position 0)))
5266 ;; Call `window-resize' with OVERRIDE argument equal WINDOW.
5267 (window-resize window delta nil window)
5268 ;; Check if the last line is surely fully visible. If
5269 ;; not, enlarge the window.
5270 (let ((end (save-excursion
5271 (goto-char (point-max))
5272 (when (and (bolp) (not (bobp)))
5273 ;; Don't include final newline.
5274 (backward-char 1))
5275 (when truncate-lines
5276 ;; If line-wrapping is turned off, test the
5277 ;; beginning of the last line for
5278 ;; visibility instead of the end, as the
5279 ;; end of the line could be invisible by
5280 ;; virtue of extending past the edge of the
5281 ;; window.
5282 (forward-line 0))
5283 (point))))
5284 (set-window-vscroll window 0)
5285 ;; This loop might in some rare pathological cases raise
5286 ;; an error - another reason for the `condition-case'.
5287 (while (and (< desired-height max-height)
5288 (= desired-height (window-total-size))
5289 (not (pos-visible-in-window-p end)))
5290 (window-resize window 1 nil window)
5291 (setq desired-height (1+ desired-height)))))
5292 (error (setq delta nil)))
5293 delta))))
5294
5295 (defun window-safely-shrinkable-p (&optional window)
5296 "Return t if WINDOW can be shrunk without shrinking other windows.
5297 WINDOW defaults to the selected window."
5298 (with-selected-window (or window (selected-window))
5299 (let ((edges (window-edges)))
5300 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
5301 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
5302
5303 (defun shrink-window-if-larger-than-buffer (&optional window)
5304 "Shrink height of WINDOW if its buffer doesn't need so many lines.
5305 More precisely, shrink WINDOW vertically to be as small as
5306 possible, while still showing the full contents of its buffer.
5307 WINDOW defaults to the selected window.
5308
5309 Do not shrink WINDOW to less than `window-min-height' lines. Do
5310 nothing if the buffer contains more lines than the present window
5311 height, or if some of the window's contents are scrolled out of
5312 view, or if shrinking this window would also shrink another
5313 window, or if the window is the only window of its frame.
5314
5315 Return non-nil if the window was shrunk, nil otherwise."
5316 (interactive)
5317 (setq window (window-normalize-window window t))
5318 ;; Make sure that WINDOW is vertically combined and `point-min' is
5319 ;; visible (for whatever reason that's needed). The remaining issues
5320 ;; should be taken care of by `fit-window-to-buffer'.
5321 (when (and (window-combined-p window)
5322 (pos-visible-in-window-p (point-min) window))
5323 (fit-window-to-buffer window (window-total-size window))))
5324 \f
5325 (defun kill-buffer-and-window ()
5326 "Kill the current buffer and delete the selected window."
5327 (interactive)
5328 (let ((window-to-delete (selected-window))
5329 (buffer-to-kill (current-buffer))
5330 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
5331 (unwind-protect
5332 (progn
5333 (add-hook 'kill-buffer-hook delete-window-hook t t)
5334 (if (kill-buffer (current-buffer))
5335 ;; If `delete-window' failed before, we rerun it to regenerate
5336 ;; the error so it can be seen in the echo area.
5337 (when (eq (selected-window) window-to-delete)
5338 (delete-window))))
5339 ;; If the buffer is not dead for some reason (probably because
5340 ;; of a `quit' signal), remove the hook again.
5341 (ignore-errors
5342 (with-current-buffer buffer-to-kill
5343 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
5344
5345 \f
5346 (defvar recenter-last-op nil
5347 "Indicates the last recenter operation performed.
5348 Possible values: `top', `middle', `bottom', integer or float numbers.")
5349
5350 (defcustom recenter-positions '(middle top bottom)
5351 "Cycling order for `recenter-top-bottom'.
5352 A list of elements with possible values `top', `middle', `bottom',
5353 integer or float numbers that define the cycling order for
5354 the command `recenter-top-bottom'.
5355
5356 Top and bottom destinations are `scroll-margin' lines the from true
5357 window top and bottom. Middle redraws the frame and centers point
5358 vertically within the window. Integer number moves current line to
5359 the specified absolute window-line. Float number between 0.0 and 1.0
5360 means the percentage of the screen space from the top. The default
5361 cycling order is middle -> top -> bottom."
5362 :type '(repeat (choice
5363 (const :tag "Top" top)
5364 (const :tag "Middle" middle)
5365 (const :tag "Bottom" bottom)
5366 (integer :tag "Line number")
5367 (float :tag "Percentage")))
5368 :version "23.2"
5369 :group 'windows)
5370
5371 (defun recenter-top-bottom (&optional arg)
5372 "Move current buffer line to the specified window line.
5373 With no prefix argument, successive calls place point according
5374 to the cycling order defined by `recenter-positions'.
5375
5376 A prefix argument is handled like `recenter':
5377 With numeric prefix ARG, move current line to window-line ARG.
5378 With plain `C-u', move current line to window center."
5379 (interactive "P")
5380 (cond
5381 (arg (recenter arg)) ; Always respect ARG.
5382 (t
5383 (setq recenter-last-op
5384 (if (eq this-command last-command)
5385 (car (or (cdr (member recenter-last-op recenter-positions))
5386 recenter-positions))
5387 (car recenter-positions)))
5388 (let ((this-scroll-margin
5389 (min (max 0 scroll-margin)
5390 (truncate (/ (window-body-height) 4.0)))))
5391 (cond ((eq recenter-last-op 'middle)
5392 (recenter))
5393 ((eq recenter-last-op 'top)
5394 (recenter this-scroll-margin))
5395 ((eq recenter-last-op 'bottom)
5396 (recenter (- -1 this-scroll-margin)))
5397 ((integerp recenter-last-op)
5398 (recenter recenter-last-op))
5399 ((floatp recenter-last-op)
5400 (recenter (round (* recenter-last-op (window-height))))))))))
5401
5402 (define-key global-map [?\C-l] 'recenter-top-bottom)
5403
5404 (defun move-to-window-line-top-bottom (&optional arg)
5405 "Position point relative to window.
5406
5407 With a prefix argument ARG, acts like `move-to-window-line'.
5408
5409 With no argument, positions point at center of window.
5410 Successive calls position point at positions defined
5411 by `recenter-positions'."
5412 (interactive "P")
5413 (cond
5414 (arg (move-to-window-line arg)) ; Always respect ARG.
5415 (t
5416 (setq recenter-last-op
5417 (if (eq this-command last-command)
5418 (car (or (cdr (member recenter-last-op recenter-positions))
5419 recenter-positions))
5420 (car recenter-positions)))
5421 (let ((this-scroll-margin
5422 (min (max 0 scroll-margin)
5423 (truncate (/ (window-body-height) 4.0)))))
5424 (cond ((eq recenter-last-op 'middle)
5425 (call-interactively 'move-to-window-line))
5426 ((eq recenter-last-op 'top)
5427 (move-to-window-line this-scroll-margin))
5428 ((eq recenter-last-op 'bottom)
5429 (move-to-window-line (- -1 this-scroll-margin)))
5430 ((integerp recenter-last-op)
5431 (move-to-window-line recenter-last-op))
5432 ((floatp recenter-last-op)
5433 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
5434
5435 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
5436 \f
5437 ;;; Scrolling commands.
5438
5439 ;;; Scrolling commands which does not signal errors at top/bottom
5440 ;;; of buffer at first key-press (instead moves to top/bottom
5441 ;;; of buffer).
5442
5443 (defcustom scroll-error-top-bottom nil
5444 "Move point to top/bottom of buffer before signalling a scrolling error.
5445 A value of nil means just signal an error if no more scrolling possible.
5446 A value of t means point moves to the beginning or the end of the buffer
5447 \(depending on scrolling direction) when no more scrolling possible.
5448 When point is already on that position, then signal an error."
5449 :type 'boolean
5450 :group 'scrolling
5451 :version "24.1")
5452
5453 (defun scroll-up-command (&optional arg)
5454 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
5455 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
5456 scroll window further, move cursor to the bottom line.
5457 When point is already on that position, then signal an error.
5458 A near full screen is `next-screen-context-lines' less than a full screen.
5459 Negative ARG means scroll downward.
5460 If ARG is the atom `-', scroll downward by nearly full screen."
5461 (interactive "^P")
5462 (cond
5463 ((null scroll-error-top-bottom)
5464 (scroll-up arg))
5465 ((eq arg '-)
5466 (scroll-down-command nil))
5467 ((< (prefix-numeric-value arg) 0)
5468 (scroll-down-command (- (prefix-numeric-value arg))))
5469 ((eobp)
5470 (scroll-up arg)) ; signal error
5471 (t
5472 (condition-case nil
5473 (scroll-up arg)
5474 (end-of-buffer
5475 (if arg
5476 ;; When scrolling by ARG lines can't be done,
5477 ;; move by ARG lines instead.
5478 (forward-line arg)
5479 ;; When ARG is nil for full-screen scrolling,
5480 ;; move to the bottom of the buffer.
5481 (goto-char (point-max))))))))
5482
5483 (put 'scroll-up-command 'scroll-command t)
5484
5485 (defun scroll-down-command (&optional arg)
5486 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
5487 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
5488 scroll window further, move cursor to the top line.
5489 When point is already on that position, then signal an error.
5490 A near full screen is `next-screen-context-lines' less than a full screen.
5491 Negative ARG means scroll upward.
5492 If ARG is the atom `-', scroll upward by nearly full screen."
5493 (interactive "^P")
5494 (cond
5495 ((null scroll-error-top-bottom)
5496 (scroll-down arg))
5497 ((eq arg '-)
5498 (scroll-up-command nil))
5499 ((< (prefix-numeric-value arg) 0)
5500 (scroll-up-command (- (prefix-numeric-value arg))))
5501 ((bobp)
5502 (scroll-down arg)) ; signal error
5503 (t
5504 (condition-case nil
5505 (scroll-down arg)
5506 (beginning-of-buffer
5507 (if arg
5508 ;; When scrolling by ARG lines can't be done,
5509 ;; move by ARG lines instead.
5510 (forward-line (- arg))
5511 ;; When ARG is nil for full-screen scrolling,
5512 ;; move to the top of the buffer.
5513 (goto-char (point-min))))))))
5514
5515 (put 'scroll-down-command 'scroll-command t)
5516
5517 ;;; Scrolling commands which scroll a line instead of full screen.
5518
5519 (defun scroll-up-line (&optional arg)
5520 "Scroll text of selected window upward ARG lines; or one line if no ARG.
5521 If ARG is omitted or nil, scroll upward by one line.
5522 This is different from `scroll-up-command' that scrolls a full screen."
5523 (interactive "p")
5524 (scroll-up (or arg 1)))
5525
5526 (put 'scroll-up-line 'scroll-command t)
5527
5528 (defun scroll-down-line (&optional arg)
5529 "Scroll text of selected window down ARG lines; or one line if no ARG.
5530 If ARG is omitted or nil, scroll down by one line.
5531 This is different from `scroll-down-command' that scrolls a full screen."
5532 (interactive "p")
5533 (scroll-down (or arg 1)))
5534
5535 (put 'scroll-down-line 'scroll-command t)
5536
5537 \f
5538 (defun scroll-other-window-down (lines)
5539 "Scroll the \"other window\" down.
5540 For more details, see the documentation for `scroll-other-window'."
5541 (interactive "P")
5542 (scroll-other-window
5543 ;; Just invert the argument's meaning.
5544 ;; We can do that without knowing which window it will be.
5545 (if (eq lines '-) nil
5546 (if (null lines) '-
5547 (- (prefix-numeric-value lines))))))
5548
5549 (defun beginning-of-buffer-other-window (arg)
5550 "Move point to the beginning of the buffer in the other window.
5551 Leave mark at previous position.
5552 With arg N, put point N/10 of the way from the true beginning."
5553 (interactive "P")
5554 (let ((orig-window (selected-window))
5555 (window (other-window-for-scrolling)))
5556 ;; We use unwind-protect rather than save-window-excursion
5557 ;; because the latter would preserve the things we want to change.
5558 (unwind-protect
5559 (progn
5560 (select-window window)
5561 ;; Set point and mark in that window's buffer.
5562 (with-no-warnings
5563 (beginning-of-buffer arg))
5564 ;; Set point accordingly.
5565 (recenter '(t)))
5566 (select-window orig-window))))
5567
5568 (defun end-of-buffer-other-window (arg)
5569 "Move point to the end of the buffer in the other window.
5570 Leave mark at previous position.
5571 With arg N, put point N/10 of the way from the true end."
5572 (interactive "P")
5573 ;; See beginning-of-buffer-other-window for comments.
5574 (let ((orig-window (selected-window))
5575 (window (other-window-for-scrolling)))
5576 (unwind-protect
5577 (progn
5578 (select-window window)
5579 (with-no-warnings
5580 (end-of-buffer arg))
5581 (recenter '(t)))
5582 (select-window orig-window))))
5583 \f
5584 (defvar mouse-autoselect-window-timer nil
5585 "Timer used by delayed window autoselection.")
5586
5587 (defvar mouse-autoselect-window-position nil
5588 "Last mouse position recorded by delayed window autoselection.")
5589
5590 (defvar mouse-autoselect-window-window nil
5591 "Last window recorded by delayed window autoselection.")
5592
5593 (defvar mouse-autoselect-window-state nil
5594 "When non-nil, special state of delayed window autoselection.
5595 Possible values are `suspend' \(suspend autoselection after a menu or
5596 scrollbar interaction\) and `select' \(the next invocation of
5597 'handle-select-window' shall select the window immediately\).")
5598
5599 (defun mouse-autoselect-window-cancel (&optional force)
5600 "Cancel delayed window autoselection.
5601 Optional argument FORCE means cancel unconditionally."
5602 (unless (and (not force)
5603 ;; Don't cancel for select-window or select-frame events
5604 ;; or when the user drags a scroll bar.
5605 (or (memq this-command
5606 '(handle-select-window handle-switch-frame))
5607 (and (eq this-command 'scroll-bar-toolkit-scroll)
5608 (memq (nth 4 (event-end last-input-event))
5609 '(handle end-scroll)))))
5610 (setq mouse-autoselect-window-state nil)
5611 (when (timerp mouse-autoselect-window-timer)
5612 (cancel-timer mouse-autoselect-window-timer))
5613 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
5614
5615 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
5616 "Start delayed window autoselection.
5617 MOUSE-POSITION is the last position where the mouse was seen as returned
5618 by `mouse-position'. Optional argument WINDOW non-nil denotes the
5619 window where the mouse was seen. Optional argument SUSPEND non-nil
5620 means suspend autoselection."
5621 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
5622 (setq mouse-autoselect-window-position mouse-position)
5623 (when window (setq mouse-autoselect-window-window window))
5624 (setq mouse-autoselect-window-state (when suspend 'suspend))
5625 ;; Install timer which runs `mouse-autoselect-window-select' after
5626 ;; `mouse-autoselect-window' seconds.
5627 (setq mouse-autoselect-window-timer
5628 (run-at-time
5629 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
5630
5631 (defun mouse-autoselect-window-select ()
5632 "Select window with delayed window autoselection.
5633 If the mouse position has stabilized in a non-selected window, select
5634 that window. The minibuffer window is selected only if the minibuffer is
5635 active. This function is run by `mouse-autoselect-window-timer'."
5636 (ignore-errors
5637 (let* ((mouse-position (mouse-position))
5638 (window
5639 (ignore-errors
5640 (window-at (cadr mouse-position) (cddr mouse-position)
5641 (car mouse-position)))))
5642 (cond
5643 ((or (menu-or-popup-active-p)
5644 (and window
5645 (not (coordinates-in-window-p (cdr mouse-position) window))))
5646 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
5647 ;; of WINDOW, temporarily suspend delayed autoselection.
5648 (mouse-autoselect-window-start mouse-position nil t))
5649 ((eq mouse-autoselect-window-state 'suspend)
5650 ;; Delayed autoselection was temporarily suspended, reenable it.
5651 (mouse-autoselect-window-start mouse-position))
5652 ((and window (not (eq window (selected-window)))
5653 (or (not (numberp mouse-autoselect-window))
5654 (and (> mouse-autoselect-window 0)
5655 ;; If `mouse-autoselect-window' is positive, select
5656 ;; window if the window is the same as before.
5657 (eq window mouse-autoselect-window-window))
5658 ;; Otherwise select window if the mouse is at the same
5659 ;; position as before. Observe that the first test after
5660 ;; starting autoselection usually fails since the value of
5661 ;; `mouse-autoselect-window-position' recorded there is the
5662 ;; position where the mouse has entered the new window and
5663 ;; not necessarily where the mouse has stopped moving.
5664 (equal mouse-position mouse-autoselect-window-position))
5665 ;; The minibuffer is a candidate window if it's active.
5666 (or (not (window-minibuffer-p window))
5667 (eq window (active-minibuffer-window))))
5668 ;; Mouse position has stabilized in non-selected window: Cancel
5669 ;; delayed autoselection and try to select that window.
5670 (mouse-autoselect-window-cancel t)
5671 ;; Select window where mouse appears unless the selected window is the
5672 ;; minibuffer. Use `unread-command-events' in order to execute pre-
5673 ;; and post-command hooks and trigger idle timers. To avoid delaying
5674 ;; autoselection again, set `mouse-autoselect-window-state'."
5675 (unless (window-minibuffer-p (selected-window))
5676 (setq mouse-autoselect-window-state 'select)
5677 (setq unread-command-events
5678 (cons (list 'select-window (list window))
5679 unread-command-events))))
5680 ((or (and window (eq window (selected-window)))
5681 (not (numberp mouse-autoselect-window))
5682 (equal mouse-position mouse-autoselect-window-position))
5683 ;; Mouse position has either stabilized in the selected window or at
5684 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
5685 (mouse-autoselect-window-cancel t))
5686 (t
5687 ;; Mouse position has not stabilized yet, resume delayed
5688 ;; autoselection.
5689 (mouse-autoselect-window-start mouse-position window))))))
5690
5691 (defun handle-select-window (event)
5692 "Handle select-window events."
5693 (interactive "e")
5694 (let ((window (posn-window (event-start event))))
5695 (unless (or (not (window-live-p window))
5696 ;; Don't switch if we're currently in the minibuffer.
5697 ;; This tries to work around problems where the
5698 ;; minibuffer gets unselected unexpectedly, and where
5699 ;; you then have to move your mouse all the way down to
5700 ;; the minibuffer to select it.
5701 (window-minibuffer-p (selected-window))
5702 ;; Don't switch to minibuffer window unless it's active.
5703 (and (window-minibuffer-p window)
5704 (not (minibuffer-window-active-p window)))
5705 ;; Don't switch when autoselection shall be delayed.
5706 (and (numberp mouse-autoselect-window)
5707 (not (zerop mouse-autoselect-window))
5708 (not (eq mouse-autoselect-window-state 'select))
5709 (progn
5710 ;; Cancel any delayed autoselection.
5711 (mouse-autoselect-window-cancel t)
5712 ;; Start delayed autoselection from current mouse
5713 ;; position and window.
5714 (mouse-autoselect-window-start (mouse-position) window)
5715 ;; Executing a command cancels delayed autoselection.
5716 (add-hook
5717 'pre-command-hook 'mouse-autoselect-window-cancel))))
5718 (when mouse-autoselect-window
5719 ;; Reset state of delayed autoselection.
5720 (setq mouse-autoselect-window-state nil)
5721 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
5722 (run-hooks 'mouse-leave-buffer-hook))
5723 (select-window window))))
5724
5725 (defun truncated-partial-width-window-p (&optional window)
5726 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
5727 WINDOW defaults to the selected window.
5728 Return nil if WINDOW is not a partial-width window
5729 (regardless of the value of `truncate-lines').
5730 Otherwise, consult the value of `truncate-partial-width-windows'
5731 for the buffer shown in WINDOW."
5732 (unless window
5733 (setq window (selected-window)))
5734 (unless (window-full-width-p window)
5735 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
5736 (window-buffer window))))
5737 (if (integerp t-p-w-w)
5738 (< (window-width window) t-p-w-w)
5739 t-p-w-w))))
5740 \f
5741 ;; Some of these are in tutorial--default-keys, so update that if you
5742 ;; change these.
5743 (define-key ctl-x-map "0" 'delete-window)
5744 (define-key ctl-x-map "1" 'delete-other-windows)
5745 (define-key ctl-x-map "2" 'split-window-below)
5746 (define-key ctl-x-map "3" 'split-window-right)
5747 (define-key ctl-x-map "o" 'other-window)
5748 (define-key ctl-x-map "^" 'enlarge-window)
5749 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
5750 (define-key ctl-x-map "{" 'shrink-window-horizontally)
5751 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
5752 (define-key ctl-x-map "+" 'balance-windows)
5753 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
5754
5755 ;;; window.el ends here