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