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