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