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