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