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