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