Use rx for `gnu' compile pattern
[bpt/emacs.git] / lisp / window.el
CommitLineData
3c448ab6
MR
1;;; window.el --- GNU Emacs window commands aside from those written in C
2
ba318903
PE
3;; Copyright (C) 1985, 1989, 1992-1994, 2000-2014 Free Software
4;; Foundation, Inc.
3c448ab6 5
34dc21db 6;; Maintainer: emacs-devel@gnu.org
3c448ab6 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
662a9d0e
SM
31(defun internal--before-save-selected-window ()
32 (cons (selected-window)
33 ;; We save and restore all frames' selected windows, because
34 ;; `select-window' can change the frame-selected-window of
35 ;; whatever frame that window is in. Each text terminal's
36 ;; top-frame is preserved by putting it last in the list.
37 (apply #'append
38 (mapcar (lambda (terminal)
39 (let ((frames (frames-on-display-list terminal))
40 (top-frame (tty-top-frame terminal))
41 alist)
42 (if top-frame
43 (setq frames
44 (cons top-frame
45 (delq top-frame frames))))
46 (dolist (f frames)
47 (push (cons f (frame-selected-window f))
48 alist))
49 alist))
50 (terminal-list)))))
51
52(defun internal--after-save-selected-window (state)
53 (dolist (elt (cdr state))
54 (and (frame-live-p (car elt))
55 (window-live-p (cdr elt))
56 (set-frame-selected-window (car elt) (cdr elt) 'norecord)))
57 (when (window-live-p (car state))
58 (select-window (car state) 'norecord)))
59
3c448ab6
MR
60(defmacro save-selected-window (&rest body)
61 "Execute BODY, then select the previously selected window.
62The value returned is the value of the last form in BODY.
63
64This macro saves and restores the selected window, as well as the
65selected window in each frame. If the previously selected window
66is no longer live, then whatever window is selected at the end of
67BODY remains selected. If the previously selected window of some
68frame is no longer live at the end of BODY, that frame's selected
69window is left alone.
70
71This macro saves and restores the current buffer, since otherwise
72its normal operation could make a different buffer current. The
73order of recently selected windows and the buffer list ordering
74are not altered by this macro (unless they are altered in BODY)."
f291fe60 75 (declare (indent 0) (debug t))
662a9d0e 76 `(let ((save-selected-window--state (internal--before-save-selected-window)))
3c448ab6
MR
77 (save-current-buffer
78 (unwind-protect
79 (progn ,@body)
662a9d0e 80 (internal--after-save-selected-window save-selected-window--state)))))
3c448ab6 81
c5e28e39
MR
82(defvar temp-buffer-window-setup-hook nil
83 "Normal hook run by `with-temp-buffer-window' before buffer display.
84This hook is run by `with-temp-buffer-window' with the buffer to be
85displayed current.")
86
87(defvar temp-buffer-window-show-hook nil
88 "Normal hook run by `with-temp-buffer-window' after buffer display.
89This hook is run by `with-temp-buffer-window' with the buffer
90displayed and current and its window selected.")
91
92(defun temp-buffer-window-setup (buffer-or-name)
42019c2e 93 "Set up temporary buffer specified by BUFFER-OR-NAME.
c5e28e39
MR
94Return the buffer."
95 (let ((old-dir default-directory)
96 (buffer (get-buffer-create buffer-or-name)))
97 (with-current-buffer buffer
98 (kill-all-local-variables)
99 (setq default-directory old-dir)
100 (delete-all-overlays)
101 (setq buffer-read-only nil)
102 (setq buffer-file-name nil)
103 (setq buffer-undo-list t)
104 (let ((inhibit-read-only t)
105 (inhibit-modification-hooks t))
106 (erase-buffer)
107 (run-hooks 'temp-buffer-window-setup-hook))
108 ;; Return the buffer.
109 buffer)))
110
111(defun temp-buffer-window-show (&optional buffer action)
112 "Show temporary buffer BUFFER in a window.
113Return the window showing BUFFER. Pass ACTION as action argument
114to `display-buffer'."
115 (let (window frame)
116 (with-current-buffer buffer
117 (set-buffer-modified-p nil)
118 (setq buffer-read-only t)
119 (goto-char (point-min))
8e17c9ba
MR
120 (when (let ((window-combination-limit
121 ;; When `window-combination-limit' equals
122 ;; `temp-buffer' or `temp-buffer-resize' and
123 ;; `temp-buffer-resize-mode' is enabled in this
124 ;; buffer bind it to t so resizing steals space
125 ;; preferably from the window that was split.
126 (if (or (eq window-combination-limit 'temp-buffer)
127 (and (eq window-combination-limit
128 'temp-buffer-resize)
129 temp-buffer-resize-mode))
130 t
131 window-combination-limit)))
132 (setq window (display-buffer buffer action)))
c5e28e39
MR
133 (setq frame (window-frame window))
134 (unless (eq frame (selected-frame))
135 (raise-frame frame))
136 (setq minibuffer-scroll-window window)
137 (set-window-hscroll window 0)
138 (with-selected-window window
139 (run-hooks 'temp-buffer-window-show-hook)
140 (when temp-buffer-resize-mode
141 (resize-temp-buffer-window window)))
142 ;; Return the window.
143 window))))
144
145(defmacro with-temp-buffer-window (buffer-or-name action quit-function &rest body)
b92631bf 146 "Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
9dda75ad
MR
147BUFFER-OR-NAME must specify either a live buffer, or the name of
148a buffer (if it does not exist, this macro creates it).
149
150Make the buffer specified by BUFFER-OR-NAME empty before running
b92631bf
MR
151BODY and bind `standard-output' to that buffer, so that output
152generated with `prin1' and similar functions in BODY goes into
153that buffer. Do not make that buffer current for running the
154forms in BODY. Use `with-current-buffer-window' instead if you
155need to run BODY with that buffer current.
9dda75ad
MR
156
157At the end of BODY, mark the specified buffer unmodified and
158read-only, and display it in a window (but do not select it).
159The display happens by calling `display-buffer' passing it the
160ACTION argument. If `temp-buffer-resize-mode' is enabled, the
b92631bf 161corresponding window may be resized automatically.
9dda75ad
MR
162
163Return the value returned by BODY, unless QUIT-FUNCTION specifies
164a function. In that case, run that function with two arguments -
c5e28e39 165the window showing the specified buffer and the value returned by
9dda75ad 166BODY - and return the value returned by that function.
c5e28e39
MR
167
168If the buffer is displayed on a new frame, the window manager may
169decide to select that frame. In that case, it's usually a good
65463c40
GM
170strategy if QUIT-FUNCTION selects the window showing the buffer
171before reading any value from the minibuffer; for example, when
172asking a `yes-or-no-p' question.
38785e75 173
95f0501e 174This runs the hook `temp-buffer-window-setup-hook' before BODY,
9dda75ad
MR
175with the specified buffer temporarily current. It runs the hook
176`temp-buffer-window-show-hook' after displaying the buffer, with
177that buffer temporarily current, and the window that was used to
95f0501e
GM
178display it temporarily selected.
179
9dda75ad 180This construct is similar to `with-output-to-temp-buffer' but,
b92631bf
MR
181neither runs `temp-buffer-setup-hook' which usually puts the
182buffer in Help mode, nor `temp-buffer-show-function' (the ACTION
183argument replaces this)."
184 (declare (debug t))
185 (let ((buffer (make-symbol "buffer"))
186 (window (make-symbol "window"))
187 (value (make-symbol "value")))
188 `(let* ((,buffer (temp-buffer-window-setup ,buffer-or-name))
189 (standard-output ,buffer)
190 ,window ,value)
191 (setq ,value (progn ,@body))
192 (with-current-buffer ,buffer
193 (setq ,window (temp-buffer-window-show ,buffer ,action)))
194
195 (if (functionp ,quit-function)
196 (funcall ,quit-function ,window ,value)
197 ,value))))
198
199(defmacro with-current-buffer-window (buffer-or-name action quit-function &rest body)
200 "Evaluate BODY with a buffer BUFFER-OR-NAME current and show that buffer.
201This construct is like `with-temp-buffer-window' but unlike that
202makes the buffer specified by BUFFER-OR-NAME current for running
203BODY."
c5e28e39
MR
204 (declare (debug t))
205 (let ((buffer (make-symbol "buffer"))
206 (window (make-symbol "window"))
207 (value (make-symbol "value")))
208 `(let* ((,buffer (temp-buffer-window-setup ,buffer-or-name))
209 (standard-output ,buffer)
210 ,window ,value)
211 (with-current-buffer ,buffer
9dda75ad 212 (setq ,value (progn ,@body))
c5e28e39
MR
213 (setq ,window (temp-buffer-window-show ,buffer ,action)))
214
215 (if (functionp ,quit-function)
216 (funcall ,quit-function ,window ,value)
217 ,value))))
218
d68443dc
MR
219;; The following two functions are like `window-next-sibling' and
220;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
221;; they don't substitute the selected window for nil), and they return
222;; nil when WINDOW doesn't have a parent (like a frame's root window or
223;; a minibuffer window).
4b0d61e3 224(defun window-right (window)
85cc1f11
MR
225 "Return WINDOW's right sibling.
226Return nil if WINDOW is the root window of its frame. WINDOW can
227be any window."
d68443dc 228 (and window (window-parent window) (window-next-sibling window)))
85cc1f11 229
4b0d61e3 230(defun window-left (window)
85cc1f11
MR
231 "Return WINDOW's left sibling.
232Return nil if WINDOW is the root window of its frame. WINDOW can
233be any window."
d68443dc 234 (and window (window-parent window) (window-prev-sibling window)))
85cc1f11 235
4b0d61e3 236(defun window-child (window)
85c2386b
MR
237 "Return WINDOW's first child window.
238WINDOW can be any window."
d68443dc 239 (or (window-top-child window) (window-left-child window)))
85cc1f11
MR
240
241(defun window-child-count (window)
85c2386b
MR
242 "Return number of WINDOW's child windows.
243WINDOW can be any window."
85cc1f11
MR
244 (let ((count 0))
245 (when (and (windowp window) (setq window (window-child window)))
246 (while window
247 (setq count (1+ count))
d68443dc 248 (setq window (window-next-sibling window))))
85cc1f11
MR
249 count))
250
251(defun window-last-child (window)
85c2386b
MR
252 "Return last child window of WINDOW.
253WINDOW can be any window."
85cc1f11 254 (when (and (windowp window) (setq window (window-child window)))
d68443dc
MR
255 (while (window-next-sibling window)
256 (setq window (window-next-sibling window))))
85cc1f11
MR
257 window)
258
4b0d61e3 259(defun window-normalize-buffer (buffer-or-name)
85cc1f11
MR
260 "Return buffer specified by BUFFER-OR-NAME.
261BUFFER-OR-NAME must be either a buffer or a string naming a live
262buffer and defaults to the current buffer."
263 (cond
264 ((not buffer-or-name)
265 (current-buffer))
266 ((bufferp buffer-or-name)
267 (if (buffer-live-p buffer-or-name)
268 buffer-or-name
269 (error "Buffer %s is not a live buffer" buffer-or-name)))
270 ((get-buffer buffer-or-name))
271 (t
272 (error "No such buffer %s" buffer-or-name))))
273
4b0d61e3 274(defun window-normalize-frame (frame)
85cc1f11
MR
275 "Return frame specified by FRAME.
276FRAME must be a live frame and defaults to the selected frame."
277 (if frame
278 (if (frame-live-p frame)
279 frame
280 (error "%s is not a live frame" frame))
281 (selected-frame)))
282
4b0d61e3 283(defun window-normalize-window (window &optional live-only)
85c2386b
MR
284 "Return the window specified by WINDOW.
285If WINDOW is nil, return the selected window. Otherwise, if
286WINDOW is a live or an internal window, return WINDOW; if
287LIVE-ONLY is non-nil, return WINDOW for a live window only.
447f16b8 288Otherwise, signal an error."
85c2386b
MR
289 (cond
290 ((null window)
291 (selected-window))
292 (live-only
293 (if (window-live-p window)
294 window
295 (error "%s is not a live window" window)))
296 ((window-valid-p window)
297 window)
298 (t
299 (error "%s is not a valid window" window))))
85cc1f11 300
880e6158
MR
301;; Maybe this should go to frame.el.
302(defun frame-char-size (&optional window-or-frame horizontal)
303 "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
304If WINDOW-OR-FRAME is a live frame, return the value of
305`frame-char-height' for that frame. If WINDOW-OR-FRAME is a
306valid window, return the value of `frame-char-height' for that
307window's frame. In any other case, return the value of
308`frame-char-height' for the selected frame.
309
310Optional argument HORIZONTAL non-nil means to return the value of
311`frame-char-width' for WINDOW-OR-FRAME."
312 (let ((frame
313 (cond
314 ((window-valid-p window-or-frame)
315 (window-frame window-or-frame))
316 ((frame-live-p window-or-frame)
317 window-or-frame)
318 (t (selected-frame)))))
319 (if horizontal
320 (frame-char-width frame)
321 (frame-char-height frame))))
322
85cc1f11
MR
323(defvar ignore-window-parameters nil
324 "If non-nil, standard functions ignore window parameters.
325The functions currently affected by this are `split-window',
326`delete-window', `delete-other-windows' and `other-window'.
327
328An application may bind this to a non-nil value around calls to
329these functions to inhibit processing of window parameters.")
330
880e6158 331;; This must go to C, finally (or get removed).
a1511caf 332(defconst window-safe-min-height 1
880e6158 333 "The absolute minimum number of lines of any window.
a1511caf
MR
334Anything less might crash Emacs.")
335
880e6158
MR
336(defun window-safe-min-pixel-height (&optional window)
337 "Return the absolute minimum pixel height of WINDOW."
338 (* window-safe-min-height
339 (frame-char-size (window-normalize-window window))))
340
562dd5e9 341(defcustom window-min-height 4
e1a2cb1c
MR
342 "The minimum total height, in lines, of any window.
343The value has to accommodate one text line, a mode and header
344line, and a bottom divider, if present. A value less than
345`window-safe-min-height' is ignored. The value of this variable
346is honored when windows are resized or split.
562dd5e9
MR
347
348Applications should never rebind this variable. To resize a
349window to a height less than the one specified here, an
d615d6d2 350application should instead call `window-resize' with a non-nil
562dd5e9 351IGNORE argument. In order to have `split-window' make a window
e4769531 352shorter, explicitly specify the SIZE argument of that function."
562dd5e9
MR
353 :type 'integer
354 :version "24.1"
355 :group 'windows)
356
880e6158
MR
357(defun window-min-pixel-height (&optional window)
358 "Return the minimum pixel height of window WINDOW."
359 (* (max window-min-height window-safe-min-height)
360 (frame-char-size window)))
361
362;; This must go to C, finally (or get removed).
a1511caf 363(defconst window-safe-min-width 2
8da11505 364 "The absolute minimum number of columns of a window.
a1511caf
MR
365Anything less might crash Emacs.")
366
880e6158
MR
367(defun window-safe-min-pixel-width (&optional window)
368 "Return the absolute minimum pixel width of WINDOW."
369 (* window-safe-min-width
370 (frame-char-size (window-normalize-window window) t)))
371
562dd5e9 372(defcustom window-min-width 10
e1a2cb1c
MR
373 "The minimum total width, in columns, of any window.
374The value has to accommodate two text columns as well as margins,
375fringes, a scroll bar and a right divider, if present. A value
376less than `window-safe-min-width' is ignored. The value of this
377variable is honored when windows are resized or split.
562dd5e9
MR
378
379Applications should never rebind this variable. To resize a
380window to a width less than the one specified here, an
d615d6d2 381application should instead call `window-resize' with a non-nil
562dd5e9 382IGNORE argument. In order to have `split-window' make a window
e4769531 383narrower, explicitly specify the SIZE argument of that function."
562dd5e9
MR
384 :type 'integer
385 :version "24.1"
386 :group 'windows)
387
880e6158
MR
388(defun window-min-pixel-width (&optional window)
389 "Return the minimum pixel width of window WINDOW."
390 (* (max window-min-width window-safe-min-width)
391 (frame-char-size window t)))
392
393(defun window-safe-min-pixel-size (&optional window horizontal)
394 "Return the absolute minimum pixel height of WINDOW.
395Optional argument HORIZONTAL non-nil means return the absolute
396minimum pixel width of WINDOW."
397 (if horizontal
398 (window-safe-min-pixel-width window)
399 (window-safe-min-pixel-height window)))
400
4b0d61e3 401(defun window-combined-p (&optional window horizontal)
49745b39 402 "Return non-nil if WINDOW has siblings in a given direction.
85c2386b 403WINDOW must be a valid window and defaults to the selected one.
49745b39 404
880e6158
MR
405HORIZONTAL determines a direction for the window combination. If
406HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
407a vertical window combination. If HORIZONTAL is non-nil, return
408non-nil if WINDOW is part of a horizontal window combination."
447f16b8 409 (setq window (window-normalize-window window))
85cc1f11 410 (let ((parent (window-parent window)))
49745b39
CY
411 (and parent
412 (if horizontal
413 (window-left-child parent)
414 (window-top-child parent)))))
85cc1f11 415
880e6158
MR
416(defun window-combination-p (&optional window horizontal)
417 "Return WINDOW's first child if WINDOW is a vertical combination.
418WINDOW can be any window and defaults to the selected one.
419Optional argument HORIZONTAL non-nil means return WINDOW's first
420child if WINDOW is a horizontal combination."
421 (setq window (window-normalize-window window))
422 (if horizontal
423 (window-left-child window)
424 (window-top-child window)))
425
be7f5545
MR
426(defun window-combinations (window &optional horizontal)
427 "Return largest number of windows vertically arranged within WINDOW.
85c2386b 428WINDOW must be a valid window and defaults to the selected one.
49745b39 429If HORIZONTAL is non-nil, return the largest number of
be7f5545 430windows horizontally arranged within WINDOW."
447f16b8 431 (setq window (window-normalize-window window))
85cc1f11
MR
432 (cond
433 ((window-live-p window)
434 ;; If WINDOW is live, return 1.
435 1)
49745b39
CY
436 ((if horizontal
437 (window-left-child window)
438 (window-top-child window))
85cc1f11 439 ;; If WINDOW is iso-combined, return the sum of the values for all
be7f5545 440 ;; child windows of WINDOW.
85cc1f11
MR
441 (let ((child (window-child window))
442 (count 0))
443 (while child
444 (setq count
3d8daefe 445 (+ (window-combinations child horizontal)
85cc1f11
MR
446 count))
447 (setq child (window-right child)))
448 count))
449 (t
450 ;; If WINDOW is not iso-combined, return the maximum value of any
be7f5545 451 ;; child window of WINDOW.
85cc1f11
MR
452 (let ((child (window-child window))
453 (count 1))
454 (while child
455 (setq count
3d8daefe 456 (max (window-combinations child horizontal)
85cc1f11
MR
457 count))
458 (setq child (window-right child)))
459 count))))
460
c7635a97 461(defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only)
85cc1f11
MR
462 "Helper function for `walk-window-tree' and `walk-window-subtree'."
463 (let (walk-window-tree-buffer)
464 (while walk-window-tree-window
465 (setq walk-window-tree-buffer
466 (window-buffer walk-window-tree-window))
467 (when (or walk-window-tree-buffer any)
c7635a97 468 (funcall fun walk-window-tree-window))
85cc1f11
MR
469 (unless walk-window-tree-buffer
470 (walk-window-tree-1
c7635a97 471 fun (window-left-child walk-window-tree-window) any)
85cc1f11 472 (walk-window-tree-1
c7635a97 473 fun (window-top-child walk-window-tree-window) any))
85cc1f11
MR
474 (if sub-only
475 (setq walk-window-tree-window nil)
476 (setq walk-window-tree-window
477 (window-right walk-window-tree-window))))))
478
ea95074e 479(defun walk-window-tree (fun &optional frame any minibuf)
c7635a97
CY
480 "Run function FUN on each live window of FRAME.
481FUN must be a function with one argument - a window. FRAME must
85cc1f11 482be a live frame and defaults to the selected one. ANY, if
ea95074e 483non-nil, means to run FUN on all live and internal windows of
85cc1f11
MR
484FRAME.
485
ea95074e
MR
486Optional argument MINIBUF t means run FUN on FRAME's minibuffer
487window even if it isn't active. MINIBUF nil or omitted means run
488FUN on FRAME's minibuffer window only if it's active. In both
489cases the minibuffer window must be part of FRAME. MINIBUF
490neither nil nor t means never run FUN on the minibuffer window.
491
85cc1f11 492This function performs a pre-order, depth-first traversal of the
c7635a97 493window tree. If FUN changes the window tree, the result is
85cc1f11 494unpredictable."
ea95074e
MR
495 (setq frame (window-normalize-frame frame))
496 (walk-window-tree-1 fun (frame-root-window frame) any)
497 (when (memq minibuf '(nil t))
498 ;; Run FUN on FRAME's minibuffer window if requested.
499 (let ((minibuffer-window (minibuffer-window frame)))
500 (when (and (window-live-p minibuffer-window)
501 (eq (window-frame minibuffer-window) frame)
502 (or (eq minibuf t)
503 (minibuffer-window-active-p minibuffer-window)))
504 (funcall fun minibuffer-window)))))
85cc1f11 505
c7635a97
CY
506(defun walk-window-subtree (fun &optional window any)
507 "Run function FUN on the subtree of windows rooted at WINDOW.
508WINDOW defaults to the selected window. FUN must be a function
509with one argument - a window. By default, run FUN only on live
be7f5545 510windows of the subtree. If the optional argument ANY is non-nil,
c7635a97
CY
511run FUN on all live and internal windows of the subtree. If
512WINDOW is live, run FUN on WINDOW only.
85cc1f11
MR
513
514This function performs a pre-order, depth-first traversal of the
c7635a97 515subtree rooted at WINDOW. If FUN changes that tree, the result
be7f5545 516is unpredictable."
447f16b8 517 (setq window (window-normalize-window window))
c7635a97 518 (walk-window-tree-1 fun window any t))
85cc1f11 519
ea95074e 520(defun window-with-parameter (parameter &optional value frame any minibuf)
85cc1f11
MR
521 "Return first window on FRAME with PARAMETER non-nil.
522FRAME defaults to the selected frame. Optional argument VALUE
523non-nil means only return a window whose window-parameter value
382c953b 524for PARAMETER equals VALUE (comparison is done with `equal').
85cc1f11 525Optional argument ANY non-nil means consider internal windows
ea95074e
MR
526too.
527
528Optional argument MINIBUF t means consider FRAME's minibuffer
529window even if it isn't active. MINIBUF nil or omitted means
530consider FRAME's minibuffer window only if it's active. In both
531cases the minibuffer window must be part of FRAME. MINIBUF
532neither nil nor t means never consider the minibuffer window."
cb882333 533 (let (this-value)
85cc1f11
MR
534 (catch 'found
535 (walk-window-tree
536 (lambda (window)
537 (when (and (setq this-value (window-parameter window parameter))
538 (or (not value) (equal value this-value)))
539 (throw 'found window)))
ea95074e 540 frame any minibuf))))
85cc1f11
MR
541
542;;; Atomic windows.
543(defun window-atom-root (&optional window)
544 "Return root of atomic window WINDOW is a part of.
85c2386b 545WINDOW must be a valid window and defaults to the selected one.
382c953b 546Return nil if WINDOW is not part of an atomic window."
447f16b8 547 (setq window (window-normalize-window window))
85cc1f11
MR
548 (let (root)
549 (while (and window (window-parameter window 'window-atom))
550 (setq root window)
551 (setq window (window-parent window)))
552 root))
553
5386012d 554(defun window-make-atom (window)
85cc1f11
MR
555 "Make WINDOW an atomic window.
556WINDOW must be an internal window. Return WINDOW."
557 (if (not (window-child window))
558 (error "Window %s is not an internal window" window)
559 (walk-window-subtree
560 (lambda (window)
c660a885
MR
561 (unless (window-parameter window 'window-atom)
562 (set-window-parameter window 'window-atom t)))
85cc1f11
MR
563 window t)
564 window))
565
caceae25
MR
566(defun display-buffer-in-atom-window (buffer alist)
567 "Display BUFFER in an atomic window.
568This function displays BUFFER in a new window that will be
569combined with an existing window to form an atomic window. If
570the existing window is already part of an atomic window, add the
571new window to that atomic window. Operations like `split-window'
572or `delete-window', when applied to a constituent of an atomic
573window, are applied atomically to the root of that atomic window.
574
575ALIST is an association list of symbols and values. The
576following symbols can be used.
577
578`window' specifies the existing window the new window shall be
579 combined with. Use `window-atom-root' to make the new window a
580 sibling of an atomic window's root. If an internal window is
581 specified here, all children of that window become part of the
582 atomic window too. If no window is specified, the new window
c660a885
MR
583 becomes a sibling of the selected window. By default, the
584 `window-atom' parameter of the existing window is set to `main'
585 provided it is live and was not set before.
caceae25
MR
586
587`side' denotes the side of the existing window where the new
588 window shall be located. Valid values are `below', `right',
c660a885
MR
589 `above' and `left'. The default is `below'. By default, the
590 `window-atom' parameter of the new window is set to this value.
caceae25
MR
591
592The return value is the new window, nil when creating that window
593failed."
c660a885
MR
594 (let* ((ignore-window-parameters t)
595 (window-combination-limit t)
596 (window-combination-resize 'atom)
597 (window (cdr (assq 'window alist)))
598 (side (cdr (assq 'side alist)))
599 (atom (when window (window-parameter window 'window-atom)))
600 root new)
caceae25 601 (setq window (window-normalize-window window))
c660a885
MR
602 (setq root (window-atom-root window))
603 ;; Split off new window.
caceae25 604 (when (setq new (split-window window nil side))
c660a885
MR
605 (window-make-atom
606 (if (and root (not (eq root window)))
607 ;; When WINDOW was part of an atomic window and we did not
608 ;; split its root, root atomic window at old root.
609 root
610 ;; Otherwise, root atomic window at WINDOW's new parent.
611 (window-parent window)))
612 ;; Assign `window-atom' parameters, if needed.
613 (when (and (not atom) (window-live-p window))
614 (set-window-parameter window 'window-atom 'main))
615 (set-window-parameter new 'window-atom side)
caceae25
MR
616 ;; Display BUFFER in NEW and return NEW.
617 (window--display-buffer
5938d519 618 buffer new 'window alist display-buffer-mark-dedicated))))
caceae25 619
54f9154c
MR
620(defun window--atom-check-1 (window)
621 "Subroutine of `window--atom-check'."
85cc1f11
MR
622 (when window
623 (if (window-parameter window 'window-atom)
624 (let ((count 0))
625 (when (or (catch 'reset
626 (walk-window-subtree
627 (lambda (window)
628 (if (window-parameter window 'window-atom)
629 (setq count (1+ count))
630 (throw 'reset t)))
631 window t))
632 ;; count >= 1 must hold here. If there's no other
633 ;; window around dissolve this atomic window.
634 (= count 1))
635 ;; Dissolve atomic window.
636 (walk-window-subtree
637 (lambda (window)
638 (set-window-parameter window 'window-atom nil))
639 window t)))
640 ;; Check children.
641 (unless (window-buffer window)
54f9154c
MR
642 (window--atom-check-1 (window-left-child window))
643 (window--atom-check-1 (window-top-child window))))
85cc1f11 644 ;; Check right sibling
54f9154c 645 (window--atom-check-1 (window-right window))))
85cc1f11 646
54f9154c 647(defun window--atom-check (&optional frame)
85cc1f11
MR
648 "Check atomicity of all windows on FRAME.
649FRAME defaults to the selected frame. If an atomic window is
be7f5545
MR
650wrongly configured, reset the atomicity of all its windows on
651FRAME to nil. An atomic window is wrongly configured if it has
652no child windows or one of its child windows is not atomic."
54f9154c 653 (window--atom-check-1 (frame-root-window frame)))
85cc1f11
MR
654
655;; Side windows.
656(defvar window-sides '(left top right bottom)
657 "Window sides.")
658
659(defcustom window-sides-vertical nil
660 "If non-nil, left and right side windows are full height.
661Otherwise, top and bottom side windows are full width."
662 :type 'boolean
663 :group 'windows
664 :version "24.1")
665
666(defcustom window-sides-slots '(nil nil nil nil)
667 "Maximum number of side window slots.
668The value is a list of four elements specifying the number of
382c953b 669side window slots on (in this order) the left, top, right and
85cc1f11
MR
670bottom side of each frame. If an element is a number, this means
671to display at most that many side windows on the corresponding
672side. If an element is nil, this means there's no bound on the
673number of slots on that side."
2bed3f04 674 :version "24.1"
85cc1f11
MR
675 :risky t
676 :type
677 '(list
678 :value (nil nil nil nil)
679 (choice
680 :tag "Left"
681 :help-echo "Maximum slots of left side window."
682 :value nil
683 :format "%[Left%] %v\n"
684 (const :tag "Unlimited" :format "%t" nil)
685 (integer :tag "Number" :value 2 :size 5))
686 (choice
687 :tag "Top"
688 :help-echo "Maximum slots of top side window."
689 :value nil
690 :format "%[Top%] %v\n"
691 (const :tag "Unlimited" :format "%t" nil)
692 (integer :tag "Number" :value 3 :size 5))
693 (choice
694 :tag "Right"
695 :help-echo "Maximum slots of right side window."
696 :value nil
697 :format "%[Right%] %v\n"
698 (const :tag "Unlimited" :format "%t" nil)
699 (integer :tag "Number" :value 2 :size 5))
700 (choice
701 :tag "Bottom"
702 :help-echo "Maximum slots of bottom side window."
703 :value nil
704 :format "%[Bottom%] %v\n"
705 (const :tag "Unlimited" :format "%t" nil)
706 (integer :tag "Number" :value 3 :size 5)))
707 :group 'windows)
708
caceae25
MR
709(defun window--major-non-side-window (&optional frame)
710 "Return the major non-side window of frame FRAME.
711The optional argument FRAME must be a live frame and defaults to
712the selected one.
713
714If FRAME has at least one side window, the major non-side window
715is either an internal non-side window such that all other
716non-side windows on FRAME descend from it, or the single live
717non-side window of FRAME. If FRAME has no side windows, return
718its root window."
719 (let ((frame (window-normalize-frame frame))
720 major sibling)
721 ;; Set major to the _last_ window found by `walk-window-tree' that
722 ;; is not a side window but has a side window as its sibling.
723 (walk-window-tree
724 (lambda (window)
725 (and (not (window-parameter window 'window-side))
726 (or (and (setq sibling (window-prev-sibling window))
727 (window-parameter sibling 'window-side))
728 (and (setq sibling (window-next-sibling window))
729 (window-parameter sibling 'window-side)))
730 (setq major window)))
c660a885 731 frame t 'nomini)
caceae25
MR
732 (or major (frame-root-window frame))))
733
734(defun window--major-side-window (side)
735 "Return major side window on SIDE.
736SIDE must be one of the symbols `left', `top', `right' or
737`bottom'. Return nil if no such window exists."
738 (let ((root (frame-root-window))
739 window)
740 ;; (1) If a window on the opposite side exists, return that window's
741 ;; sibling.
742 ;; (2) If the new window shall span the entire side, return the
743 ;; frame's root window.
744 ;; (3) If a window on an orthogonal side exists, return that
745 ;; window's sibling.
746 ;; (4) Otherwise return the frame's root window.
747 (cond
748 ((or (and (eq side 'left)
749 (setq window (window-with-parameter 'window-side 'right nil t)))
750 (and (eq side 'top)
751 (setq window (window-with-parameter 'window-side 'bottom nil t))))
752 (window-prev-sibling window))
753 ((or (and (eq side 'right)
754 (setq window (window-with-parameter 'window-side 'left nil t)))
755 (and (eq side 'bottom)
756 (setq window (window-with-parameter 'window-side 'top nil t))))
757 (window-next-sibling window))
758 ((memq side '(left right))
759 (cond
760 (window-sides-vertical
761 root)
762 ((setq window (window-with-parameter 'window-side 'top nil t))
763 (window-next-sibling window))
764 ((setq window (window-with-parameter 'window-side 'bottom nil t))
765 (window-prev-sibling window))
766 (t root)))
767 ((memq side '(top bottom))
768 (cond
769 ((not window-sides-vertical)
770 root)
771 ((setq window (window-with-parameter 'window-side 'left nil t))
772 (window-next-sibling window))
773 ((setq window (window-with-parameter 'window-side 'right nil t))
774 (window-prev-sibling window))
775 (t root))))))
776
777(defun display-buffer-in-major-side-window (buffer side slot &optional alist)
778 "Display BUFFER in a new window on SIDE of the selected frame.
779SIDE must be one of `left', `top', `right' or `bottom'. SLOT
780specifies the slot to use. ALIST is an association list of
781symbols and values as passed to `display-buffer-in-side-window'.
782This function may be called only if no window on SIDE exists yet.
783The new window automatically becomes the \"major\" side window on
784SIDE. Return the new window, nil if its creation window failed."
9d3aa82c 785 (let* ((left-or-right (memq side '(left right)))
caceae25 786 (major (window--major-side-window side))
caceae25
MR
787 (on-side (cond
788 ((eq side 'top) 'above)
789 ((eq side 'bottom) 'below)
790 (t side)))
791 ;; The following two bindings will tell `split-window' to take
792 ;; the space for the new window from `major' and not make a new
793 ;; parent window unless needed.
794 (window-combination-resize 'side)
795 (window-combination-limit nil)
9d3aa82c 796 (new (split-window major nil on-side)))
caceae25
MR
797 (when new
798 ;; Initialize `window-side' parameter of new window to SIDE.
799 (set-window-parameter new 'window-side side)
800 ;; Install `window-slot' parameter of new window.
801 (set-window-parameter new 'window-slot slot)
802 ;; Install `delete-window' parameter thus making sure that when
803 ;; the new window is deleted, a side window on the opposite side
804 ;; does not get resized.
805 (set-window-parameter new 'delete-window 'delete-side-window)
5938d519
MR
806 ;; Auto-adjust height/width of new window unless a size has been
807 ;; explicitly requested.
735135f9 808 (unless (if left-or-right
5938d519
MR
809 (cdr (assq 'window-width alist))
810 (cdr (assq 'window-height alist)))
811 (setq alist
812 (cons
813 (cons
814 (if left-or-right 'window-width 'window-height)
815 (/ (window-total-size (frame-root-window) left-or-right)
880e6158
MR
816 ;; By default use a fourth of the size of the frame's
817 ;; root window.
5938d519
MR
818 4))
819 alist)))
caceae25 820 ;; Install BUFFER in new window and return NEW.
5938d519 821 (window--display-buffer buffer new 'window alist 'side))))
caceae25
MR
822
823(defun delete-side-window (window)
824 "Delete side window WINDOW."
825 (let ((window-combination-resize
826 (window-parameter (window-parent window) 'window-side))
827 (ignore-window-parameters t))
828 (delete-window window)))
829
830(defun display-buffer-in-side-window (buffer alist)
3c29190f 831 "Display BUFFER in a side window of the selected frame.
caceae25 832ALIST is an association list of symbols and values. The
3c29190f 833following special symbols can be used in ALIST.
caceae25 834
3c29190f
MR
835`side' denotes the side of the frame where the new window shall
836 be located. Valid values are `bottom', `right', `top' and
837 `left'. The default is `bottom'.
caceae25
MR
838
839`slot' if non-nil, specifies the window slot where to display
840 BUFFER. A value of zero or nil means use the middle slot on
841 the specified side. A negative value means use a slot
842 preceding (that is, above or on the left of) the middle slot.
843 A positive value means use a slot following (that is, below or
844 on the right of) the middle slot. The default is zero."
845 (let ((side (or (cdr (assq 'side alist)) 'bottom))
9d3aa82c 846 (slot (or (cdr (assq 'slot alist)) 0)))
caceae25
MR
847 (cond
848 ((not (memq side '(top bottom left right)))
849 (error "Invalid side %s specified" side))
850 ((not (numberp slot))
851 (error "Invalid slot %s specified" slot)))
852
853 (let* ((major (window-with-parameter 'window-side side nil t))
854 ;; `major' is the major window on SIDE, `windows' the list of
855 ;; life windows on SIDE.
856 (windows
857 (when major
858 (let (windows)
859 (walk-window-tree
860 (lambda (window)
861 (when (eq (window-parameter window 'window-side) side)
c660a885
MR
862 (setq windows (cons window windows))))
863 nil nil 'nomini)
caceae25
MR
864 (nreverse windows))))
865 (slots (when major (max 1 (window-child-count major))))
866 (max-slots
867 (nth (cond
868 ((eq side 'left) 0)
869 ((eq side 'top) 1)
870 ((eq side 'right) 2)
871 ((eq side 'bottom) 3))
872 window-sides-slots))
caceae25 873 window this-window this-slot prev-window next-window
9d3aa82c 874 best-window best-slot abs-slot)
caceae25
MR
875
876 (cond
877 ((and (numberp max-slots) (<= max-slots 0))
878 ;; No side-slots available on this side. Don't create an error,
879 ;; just return nil.
880 nil)
881 ((not windows)
882 ;; No major window exists on this side, make one.
883 (display-buffer-in-major-side-window buffer side slot alist))
884 (t
885 ;; Scan windows on SIDE.
886 (catch 'found
887 (dolist (window windows)
888 (setq this-slot (window-parameter window 'window-slot))
889 (cond
890 ;; The following should not happen and probably be checked
891 ;; by window--side-check.
892 ((not (numberp this-slot)))
893 ((= this-slot slot)
894 ;; A window with a matching slot has been found.
895 (setq this-window window)
896 (throw 'found t))
897 (t
898 ;; Check if this window has a better slot value wrt the
899 ;; slot of the window we want.
900 (setq abs-slot
901 (if (or (and (> this-slot 0) (> slot 0))
902 (and (< this-slot 0) (< slot 0)))
903 (abs (- slot this-slot))
904 (+ (abs slot) (abs this-slot))))
905 (unless (and best-slot (<= best-slot abs-slot))
906 (setq best-window window)
907 (setq best-slot abs-slot))
908 (cond
909 ((<= this-slot slot)
910 (setq prev-window window))
911 ((not next-window)
912 (setq next-window window)))))))
913
914 ;; `this-window' is the first window with the same SLOT.
915 ;; `prev-window' is the window with the largest slot < SLOT. A new
916 ;; window will be created after it.
917 ;; `next-window' is the window with the smallest slot > SLOT. A new
918 ;; window will be created before it.
919 ;; `best-window' is the window with the smallest absolute difference
920 ;; of its slot and SLOT.
921
922 ;; Note: We dedicate the window used softly to its buffer to
923 ;; avoid that "other" (non-side) buffer display functions steal
924 ;; it from us. This must eventually become customizable via
925 ;; ALIST (or, better, avoided in the "other" functions).
926 (or (and this-window
927 ;; Reuse `this-window'.
5938d519 928 (window--display-buffer buffer this-window 'reuse alist 'side))
caceae25
MR
929 (and (or (not max-slots) (< slots max-slots))
930 (or (and next-window
931 ;; Make new window before `next-window'.
932 (let ((next-side
933 (if (memq side '(left right)) 'above 'left))
934 (window-combination-resize 'side))
935 (setq window (split-window next-window nil next-side))
936 ;; When the new window is deleted, its space
937 ;; is returned to other side windows.
938 (set-window-parameter
939 window 'delete-window 'delete-side-window)
940 window))
941 (and prev-window
942 ;; Make new window after `prev-window'.
943 (let ((prev-side
944 (if (memq side '(left right)) 'below 'right))
945 (window-combination-resize 'side))
946 (setq window (split-window prev-window nil prev-side))
947 ;; When the new window is deleted, its space
948 ;; is returned to other side windows.
949 (set-window-parameter
950 window 'delete-window 'delete-side-window)
951 window)))
952 (set-window-parameter window 'window-slot slot)
5938d519 953 (window--display-buffer buffer window 'window alist 'side))
caceae25
MR
954 (and best-window
955 ;; Reuse `best-window'.
956 (progn
957 ;; Give best-window the new slot value.
958 (set-window-parameter best-window 'window-slot slot)
5938d519
MR
959 (window--display-buffer
960 buffer best-window 'reuse alist 'side)))))))))
caceae25 961
54f9154c 962(defun window--side-check (&optional frame)
caceae25
MR
963 "Check the side window configuration of FRAME.
964FRAME defaults to the selected frame.
965
966A valid side window configuration preserves the following two
967invariants:
968
969- If there exists a window whose window-side parameter is
970 non-nil, there must exist at least one live window whose
971 window-side parameter is nil.
972
973- If a window W has a non-nil window-side parameter (i) it must
974 have a parent window and that parent's window-side parameter
975 must be either nil or the same as for W, and (ii) any child
976 window of W must have the same window-side parameter as W.
977
978If the configuration is invalid, reset the window-side parameters
979of all windows on FRAME to nil."
980 (let (left top right bottom none side parent parent-side)
85cc1f11
MR
981 (when (or (catch 'reset
982 (walk-window-tree
983 (lambda (window)
984 (setq side (window-parameter window 'window-side))
985 (setq parent (window-parent window))
986 (setq parent-side
987 (and parent (window-parameter parent 'window-side)))
988 ;; The following `cond' seems a bit tedious, but I'd
989 ;; rather stick to using just the stack.
990 (cond
991 (parent-side
992 (when (not (eq parent-side side))
993 ;; A parent whose window-side is non-nil must
994 ;; have a child with the same window-side.
995 (throw 'reset t)))
caceae25
MR
996 ((not side)
997 (when (window-buffer window)
998 ;; Record that we have at least one non-side,
999 ;; live window.
85cc1f11 1000 (setq none t)))
caceae25
MR
1001 ((if (memq side '(left top))
1002 (window-prev-sibling window)
1003 (window-next-sibling window))
1004 ;; Left and top major side windows must not have a
1005 ;; previous sibling, right and bottom major side
1006 ;; windows must not have a next sibling.
1007 (throw 'reset t))
1008 ;; Now check that there's no more than one major
1009 ;; window for any of left, top, right and bottom.
85cc1f11 1010 ((eq side 'left)
caceae25 1011 (if left (throw 'reset t) (setq left t)))
85cc1f11 1012 ((eq side 'top)
caceae25 1013 (if top (throw 'reset t) (setq top t)))
85cc1f11 1014 ((eq side 'right)
caceae25 1015 (if right (throw 'reset t) (setq right t)))
85cc1f11 1016 ((eq side 'bottom)
caceae25 1017 (if bottom (throw 'reset t) (setq bottom t)))
42019c2e 1018 (t
caceae25 1019 (throw 'reset t))))
c660a885 1020 frame t 'nomini))
caceae25
MR
1021 ;; If there's a side window, there must be at least one
1022 ;; non-side window.
1023 (and (or left top right bottom) (not none)))
85cc1f11
MR
1024 (walk-window-tree
1025 (lambda (window)
1026 (set-window-parameter window 'window-side nil))
c660a885 1027 frame t 'nomini))))
85cc1f11 1028
54f9154c 1029(defun window--check (&optional frame)
85cc1f11
MR
1030 "Check atomic and side windows on FRAME.
1031FRAME defaults to the selected frame."
54f9154c
MR
1032 (window--side-check frame)
1033 (window--atom-check frame))
85cc1f11 1034
8dd3e94f
MR
1035;; Dumping frame/window contents.
1036(defun window--dump-window (&optional window erase)
1037 "Dump WINDOW to buffer *window-frame-dump*.
1038WINDOW must be a valid window and defaults to the selected one.
1039Optional argument ERASE non-nil means erase *window-frame-dump*
1040before writing to it."
1041 (setq window (window-normalize-window window))
1042 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1043 (when erase (erase-buffer))
1044 (insert
1045 (format "%s parent: %s\n" window (window-parent window))
1046 (format "pixel left: %s top: %s size: %s x %s new: %s\n"
1047 (window-pixel-left window) (window-pixel-top window)
1048 (window-size window t t) (window-size window nil t)
1049 (window-new-pixel window))
1050 (format "char left: %s top: %s size: %s x %s new: %s\n"
1051 (window-left-column window) (window-top-line window)
1052 (window-total-size window t) (window-total-size window)
1053 (window-new-total window))
1054 (format "normal: %s x %s new: %s\n"
1055 (window-normal-size window t) (window-normal-size window)
1056 (window-new-normal window)))
1057 (when (window-live-p window)
1058 (let ((fringes (window-fringes window))
1059 (margins (window-margins window)))
1060 (insert
1061 (format "body pixel: %s x %s char: %s x %s\n"
1062 (window-body-width window t) (window-body-height window t)
1063 (window-body-width window) (window-body-height window))
1064 (format "width left fringe: %s left margin: %s right margin: %s\n"
1065 (car fringes) (or (car margins) 0) (or (cdr margins) 0))
1066 (format "width right fringe: %s scroll-bar: %s divider: %s\n"
1067 (cadr fringes)
1068 (window-scroll-bar-width window)
1069 (window-right-divider-width window))
1070 (format "height header-line: %s mode-line: %s divider: %s\n"
1071 (window-header-line-height window)
1072 (window-mode-line-height window)
1073 (window-bottom-divider-width window)))))
1074 (insert "\n")))
1075
1076(defun window--dump-frame (&optional window-or-frame)
1077 "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
1078WINDOW-OR-FRAME can be a frame or a window and defaults to the
1079selected frame. When WINDOW-OR-FRAME is a window, dump that
1080window's frame. The buffer *window-frame-dump* is erased before
1081dumping to it."
1082 (interactive)
1083 (let* ((window
1084 (cond
1085 ((or (not window-or-frame)
1086 (frame-live-p window-or-frame))
1087 (frame-root-window window-or-frame))
1088 ((or (window-live-p window-or-frame)
1089 (window-child window-or-frame))
1090 window-or-frame)
1091 (t
1092 (frame-root-window))))
1093 (frame (window-frame window)))
1094 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1095 (erase-buffer)
1096 (insert
1097 (format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
1098 (frame-pixel-width frame) (frame-pixel-height frame)
1099 (frame-total-cols frame) (frame-text-lines frame) ; (frame-total-lines frame)
1100 (frame-char-width frame) (frame-char-height frame))
1101 (format "frame text pixel: %s x %s cols/lines: %s x %s\n"
1102 (frame-text-width frame) (frame-text-height frame)
1103 (frame-text-cols frame) (frame-text-lines frame))
1104 (format "tool: %s scroll: %s fringe: %s border: %s right: %s bottom: %s\n\n"
1105 (tool-bar-height frame t)
1106 (frame-scroll-bar-width frame)
1107 (frame-fringe-width frame)
1108 (frame-border-width frame)
1109 (frame-right-divider-width frame)
1110 (frame-bottom-divider-width frame)))
1111 (walk-window-tree 'window--dump-window frame t t))))
1112
85cc1f11 1113;;; Window sizes.
6cb4da45 1114(defun window-total-size (&optional window horizontal round)
880e6158
MR
1115 "Return the total height or width of WINDOW.
1116WINDOW must be a valid window and defaults to the selected one.
1117
1118If HORIZONTAL is omitted or nil, return the total height of
1119WINDOW, in lines, like `window-total-height'. Otherwise return
6cb4da45
MR
1120the total width, in columns, like `window-total-width'.
1121
1122Optional argument ROUND is handled as for `window-total-height'
1123and `window-total-width'."
880e6158 1124 (if horizontal
6cb4da45
MR
1125 (window-total-width window round)
1126 (window-total-height window round)))
880e6158 1127
6cb4da45 1128(defun window-size (&optional window horizontal pixelwise round)
880e6158
MR
1129 "Return the height or width of WINDOW.
1130WINDOW must be a valid window and defaults to the selected one.
1131
1132If HORIZONTAL is omitted or nil, return the total height of
1133WINDOW, in lines, like `window-total-height'. Otherwise return
1134the total width, in columns, like `window-total-width'.
1135
1136Optional argument PIXELWISE means return the pixel size of WINDOW
6cb4da45
MR
1137like `window-pixel-height' and `window-pixel-width'.
1138
1139Optional argument ROUND is ignored if PIXELWISE is non-nil and
1140handled as for `window-total-height' and `window-total-width'
1141otherwise."
880e6158
MR
1142 (if horizontal
1143 (if pixelwise
1144 (window-pixel-width window)
6cb4da45 1145 (window-total-width window round))
880e6158
MR
1146 (if pixelwise
1147 (window-pixel-height window)
6cb4da45 1148 (window-total-height window round))))
880e6158 1149
85cc1f11
MR
1150(defvar window-size-fixed nil
1151 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
1152If the value is `height', then only the window's height is fixed.
1153If the value is `width', then only the window's width is fixed.
1154Any other non-nil value fixes both the width and the height.
1155
1156Emacs won't change the size of any window displaying that buffer,
382c953b 1157unless it has no other choice (like when deleting a neighboring
85cc1f11
MR
1158window).")
1159(make-variable-buffer-local 'window-size-fixed)
1160
842e3a93 1161(defun window--size-ignore-p (window ignore)
a1511caf 1162 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
24300f5f 1163 (if (window-valid-p ignore) (eq window ignore) ignore))
a1511caf 1164
880e6158
MR
1165(defun window-safe-min-size (&optional window horizontal pixelwise)
1166 "Return safe minimum size of WINDOW.
1167WINDOW must be a valid window and defaults to the selected one.
1168Optional argument HORIZONTAL non-nil means return the minimum
1169number of columns of WINDOW; otherwise return the minimum number
1170of WINDOW's lines.
1171
1172Optional argument PIXELWISE non-nil means return the minimum pixel-size
1173of WINDOW."
1174 (setq window (window-normalize-window window))
1175 (if pixelwise
1176 (if horizontal
1177 (* window-safe-min-width
1178 (frame-char-width (window-frame window)))
1179 (* window-safe-min-height
1180 (frame-char-height (window-frame window))))
1181 (if horizontal window-safe-min-width window-safe-min-height)))
1182
1183(defun window-min-size (&optional window horizontal ignore pixelwise)
2116e93c 1184 "Return the minimum size of WINDOW.
85c2386b
MR
1185WINDOW must be a valid window and defaults to the selected one.
1186Optional argument HORIZONTAL non-nil means return the minimum
1187number of columns of WINDOW; otherwise return the minimum number
1188of WINDOW's lines.
a1511caf 1189
2116e93c 1190Optional argument IGNORE, if non-nil, means ignore restrictions
a1511caf 1191imposed by fixed size windows, `window-min-height' or
2116e93c 1192`window-min-width' settings. If IGNORE equals `safe', live
a1511caf 1193windows may get as small as `window-safe-min-height' lines and
2116e93c
EZ
1194`window-safe-min-width' columns. If IGNORE is a window, ignore
1195restrictions for that window only. Any other non-nil value
880e6158
MR
1196means ignore all of the above restrictions for all windows.
1197
1198Optional argument PIXELWISE non-nil means return the minimum pixel-size
1199of WINDOW."
c56cad4a 1200 (window--min-size-1
880e6158 1201 (window-normalize-window window) horizontal ignore pixelwise))
a1511caf 1202
880e6158 1203(defun window--min-size-1 (window horizontal ignore pixelwise)
a1511caf
MR
1204 "Internal function of `window-min-size'."
1205 (let ((sub (window-child window)))
1206 (if sub
1207 (let ((value 0))
1208 ;; WINDOW is an internal window.
3d8daefe 1209 (if (window-combined-p sub horizontal)
a1511caf 1210 ;; The minimum size of an iso-combination is the sum of
be7f5545 1211 ;; the minimum sizes of its child windows.
a1511caf
MR
1212 (while sub
1213 (setq value (+ value
880e6158
MR
1214 (window--min-size-1
1215 sub horizontal ignore pixelwise)))
a1511caf 1216 (setq sub (window-right sub)))
c660a885
MR
1217 ;; The minimum size of an ortho-combination is the maximum
1218 ;; of the minimum sizes of its child windows.
a1511caf
MR
1219 (while sub
1220 (setq value (max value
880e6158
MR
1221 (window--min-size-1
1222 sub horizontal ignore pixelwise)))
a1511caf
MR
1223 (setq sub (window-right sub))))
1224 value)
1225 (with-current-buffer (window-buffer window)
1226 (cond
842e3a93 1227 ((and (not (window--size-ignore-p window ignore))
a1511caf
MR
1228 (window-size-fixed-p window horizontal))
1229 ;; The minimum size of a fixed size window is its size.
880e6158 1230 (window-size window horizontal pixelwise))
a1511caf
MR
1231 ((or (eq ignore 'safe) (eq ignore window))
1232 ;; If IGNORE equals `safe' or WINDOW return the safe values.
880e6158 1233 (window-safe-min-size window horizontal pixelwise))
a1511caf
MR
1234 (horizontal
1235 ;; For the minimum width of a window take fringes and
1236 ;; scroll-bars into account. This is questionable and should
1237 ;; be removed as soon as we are able to split (and resize)
1238 ;; windows such that the new (or resized) windows can get a
1239 ;; size less than the user-specified `window-min-height' and
1240 ;; `window-min-width'.
8dd3e94f
MR
1241 (let* ((char-size (frame-char-size window t))
1242 (fringes (window-fringes window))
1243 (pixel-width
1244 (+ (window-safe-min-size window t t)
1245 (car fringes) (cadr fringes)
1246 (window-scroll-bar-width window)
1247 (window-right-divider-width window))))
880e6158
MR
1248 (if pixelwise
1249 (max
8dd3e94f
MR
1250 (if window-resize-pixelwise
1251 pixel-width
1252 ;; Round up to next integral of columns.
1253 (* (ceiling pixel-width char-size) char-size))
880e6158
MR
1254 (if (window--size-ignore-p window ignore)
1255 0
1256 (window-min-pixel-width)))
1257 (max
8dd3e94f 1258 (ceiling pixel-width char-size)
880e6158
MR
1259 (if (window--size-ignore-p window ignore)
1260 0
1261 window-min-width)))))
8dd3e94f
MR
1262 ((let ((char-size (frame-char-size window))
1263 (pixel-height
1264 (+ (window-safe-min-size window nil t)
1265 (window-header-line-height window)
1266 (window-mode-line-height window)
1267 (window-bottom-divider-width window))))
1268 (if pixelwise
1269 (max
1270 (if window-resize-pixelwise
1271 pixel-height
1272 ;; Round up to next integral of lines.
1273 (* (ceiling pixel-height char-size) char-size))
1274 (if (window--size-ignore-p window ignore)
1275 0
1276 (window-min-pixel-height)))
1277 (max (ceiling pixel-height char-size)
1278 (if (window--size-ignore-p window ignore)
1279 0
1280 window-min-height))))))))))
a1511caf 1281
880e6158 1282(defun window-sizable (window delta &optional horizontal ignore pixelwise)
a1511caf 1283 "Return DELTA if DELTA lines can be added to WINDOW.
85c2386b 1284WINDOW must be a valid window and defaults to the selected one.
a1511caf
MR
1285Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1286columns can be added to WINDOW. A return value of zero means
1287that no lines (or columns) can be added to WINDOW.
1288
be7f5545
MR
1289This function looks only at WINDOW and, recursively, its child
1290windows. The function `window-resizable' looks at other windows
1291as well.
a1511caf
MR
1292
1293DELTA positive means WINDOW shall be enlarged by DELTA lines or
1294columns. If WINDOW cannot be enlarged by DELTA lines or columns
1295return the maximum value in the range 0..DELTA by which WINDOW
1296can be enlarged.
1297
1298DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1299columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1300return the minimum value in the range DELTA..0 by which WINDOW
1301can be shrunk.
1302
2116e93c 1303Optional argument IGNORE non-nil means ignore restrictions
a1511caf 1304imposed by fixed size windows, `window-min-height' or
2116e93c 1305`window-min-width' settings. If IGNORE equals `safe', live
a1511caf 1306windows may get as small as `window-safe-min-height' lines and
2116e93c
EZ
1307`window-safe-min-width' columns. If IGNORE is a window, ignore
1308restrictions for that window only. Any other non-nil value means
880e6158
MR
1309ignore all of the above restrictions for all windows.
1310
1311Optional argument PIXELWISE non-nil means interpret DELTA as
1312pixels."
447f16b8 1313 (setq window (window-normalize-window window))
a1511caf
MR
1314 (cond
1315 ((< delta 0)
880e6158
MR
1316 (max (- (window-min-size window horizontal ignore pixelwise)
1317 (window-size window horizontal pixelwise))
a1511caf 1318 delta))
842e3a93 1319 ((window--size-ignore-p window ignore)
a1511caf
MR
1320 delta)
1321 ((> delta 0)
1322 (if (window-size-fixed-p window horizontal)
1323 0
1324 delta))
1325 (t 0)))
1326
880e6158 1327(defun window-sizable-p (window delta &optional horizontal ignore pixelwise)
a1511caf 1328 "Return t if WINDOW can be resized by DELTA lines.
85c2386b 1329WINDOW must be a valid window and defaults to the selected one.
a1511caf
MR
1330For the meaning of the arguments of this function see the
1331doc-string of `window-sizable'."
447f16b8 1332 (setq window (window-normalize-window window))
a1511caf 1333 (if (> delta 0)
880e6158
MR
1334 (>= (window-sizable window delta horizontal ignore pixelwise)
1335 delta)
1336 (<= (window-sizable window delta horizontal ignore pixelwise)
1337 delta)))
a1511caf 1338
5e92ca23 1339(defun window--size-fixed-1 (window horizontal)
a1511caf
MR
1340 "Internal function for `window-size-fixed-p'."
1341 (let ((sub (window-child window)))
1342 (catch 'fixed
1343 (if sub
1344 ;; WINDOW is an internal window.
3d8daefe 1345 (if (window-combined-p sub horizontal)
be7f5545
MR
1346 ;; An iso-combination is fixed size if all its child
1347 ;; windows are fixed-size.
a1511caf
MR
1348 (progn
1349 (while sub
5e92ca23 1350 (unless (window--size-fixed-1 sub horizontal)
be7f5545
MR
1351 ;; We found a non-fixed-size child window, so
1352 ;; WINDOW's size is not fixed.
a1511caf
MR
1353 (throw 'fixed nil))
1354 (setq sub (window-right sub)))
be7f5545 1355 ;; All child windows are fixed-size, so WINDOW's size is
a1511caf
MR
1356 ;; fixed.
1357 (throw 'fixed t))
1358 ;; An ortho-combination is fixed-size if at least one of its
be7f5545 1359 ;; child windows is fixed-size.
a1511caf 1360 (while sub
5e92ca23 1361 (when (window--size-fixed-1 sub horizontal)
be7f5545
MR
1362 ;; We found a fixed-size child window, so WINDOW's size
1363 ;; is fixed.
a1511caf
MR
1364 (throw 'fixed t))
1365 (setq sub (window-right sub))))
1366 ;; WINDOW is a live window.
1367 (with-current-buffer (window-buffer window)
1368 (if horizontal
1369 (memq window-size-fixed '(width t))
1370 (memq window-size-fixed '(height t))))))))
1371
1372(defun window-size-fixed-p (&optional window horizontal)
1373 "Return non-nil if WINDOW's height is fixed.
85c2386b
MR
1374WINDOW must be a valid window and defaults to the selected one.
1375Optional argument HORIZONTAL non-nil means return non-nil if
1376WINDOW's width is fixed.
a1511caf
MR
1377
1378If this function returns nil, this does not necessarily mean that
2cffd681
MR
1379WINDOW can be resized in the desired direction. The function
1380`window-resizable' can tell that."
5e92ca23 1381 (window--size-fixed-1
447f16b8 1382 (window-normalize-window window) horizontal))
a1511caf 1383
880e6158 1384(defun window--min-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
a1511caf
MR
1385 "Internal function for `window-min-delta'."
1386 (if (not (window-parent window))
1387 ;; If we can't go up, return zero.
1388 0
1389 ;; Else try to find a non-fixed-size sibling of WINDOW.
1390 (let* ((parent (window-parent window))
1391 (sub (window-child parent)))
1392 (catch 'done
3d8daefe 1393 (if (window-combined-p sub horizontal)
a1511caf 1394 ;; In an iso-combination throw DELTA if we find at least one
be7f5545
MR
1395 ;; child window and that window is either not fixed-size or
1396 ;; we can ignore fixed-sizeness.
a1511caf
MR
1397 (let ((skip (eq trail 'after)))
1398 (while sub
1399 (cond
1400 ((eq sub window)
1401 (setq skip (eq trail 'before)))
1402 (skip)
842e3a93 1403 ((and (not (window--size-ignore-p window ignore))
a1511caf
MR
1404 (window-size-fixed-p sub horizontal)))
1405 (t
be7f5545 1406 ;; We found a non-fixed-size child window.
a1511caf
MR
1407 (throw 'done delta)))
1408 (setq sub (window-right sub))))
1409 ;; In an ortho-combination set DELTA to the minimum value by
be7f5545 1410 ;; which other child windows can shrink.
a1511caf
MR
1411 (while sub
1412 (unless (eq sub window)
1413 (setq delta
1414 (min delta
8dd3e94f
MR
1415 (max (- (window-size sub horizontal pixelwise 'ceiling)
1416 (window-min-size
1417 sub horizontal ignore pixelwise))
1418 0))))
a1511caf
MR
1419 (setq sub (window-right sub))))
1420 (if noup
1421 delta
880e6158
MR
1422 (window--min-delta-1
1423 parent delta horizontal ignore trail nil pixelwise))))))
a1511caf 1424
880e6158 1425(defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise)
a1511caf 1426 "Return number of lines by which WINDOW can be shrunk.
85c2386b
MR
1427WINDOW must be a valid window and defaults to the selected one.
1428Return zero if WINDOW cannot be shrunk.
a1511caf
MR
1429
1430Optional argument HORIZONTAL non-nil means return number of
1431columns by which WINDOW can be shrunk.
1432
2116e93c 1433Optional argument IGNORE non-nil means ignore restrictions
a1511caf 1434imposed by fixed size windows, `window-min-height' or
2116e93c
EZ
1435`window-min-width' settings. If IGNORE is a window, ignore
1436restrictions for that window only. If IGNORE equals `safe',
a1511caf 1437live windows may get as small as `window-safe-min-height' lines
2116e93c
EZ
1438and `window-safe-min-width' columns. Any other non-nil value
1439means ignore all of the above restrictions for all windows.
a1511caf 1440
2116e93c
EZ
1441Optional argument TRAIL restricts the windows that can be enlarged.
1442If its value is `before', only windows to the left of or above WINDOW
1443can be enlarged. If it is `after', only windows to the right of or
1444below WINDOW can be enlarged.
a1511caf
MR
1445
1446Optional argument NOUP non-nil means don't go up in the window
2116e93c 1447tree, but try to enlarge windows within WINDOW's combination only.
a1511caf
MR
1448
1449Optional argument NODOWN non-nil means don't check whether WINDOW
382c953b 1450itself (and its child windows) can be shrunk; check only whether
880e6158
MR
1451at least one other window can be enlarged appropriately.
1452
1453Optional argument PIXELWISE non-nil means return number of pixels
1454by which WINDOW can be shrunk."
447f16b8 1455 (setq window (window-normalize-window window))
6cb4da45 1456 (let ((size (window-size window horizontal pixelwise 'floor))
880e6158 1457 (minimum (window-min-size window horizontal ignore pixelwise)))
a1511caf
MR
1458 (cond
1459 (nodown
1460 ;; If NODOWN is t, try to recover the entire size of WINDOW.
880e6158
MR
1461 (window--min-delta-1
1462 window size horizontal ignore trail noup pixelwise))
1463 ((<= size minimum)
a1511caf
MR
1464 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1465 ;; there's nothing to recover.
1466 0)
1467 (t
1468 ;; Otherwise, try to recover whatever WINDOW is larger than its
1469 ;; minimum size.
c56cad4a 1470 (window--min-delta-1
880e6158 1471 window (- size minimum) horizontal ignore trail noup pixelwise)))))
a1511caf 1472
880e6158 1473(defun window--max-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
a1511caf
MR
1474 "Internal function of `window-max-delta'."
1475 (if (not (window-parent window))
1476 ;; Can't go up. Return DELTA.
1477 delta
1478 (let* ((parent (window-parent window))
1479 (sub (window-child parent)))
1480 (catch 'fixed
3d8daefe 1481 (if (window-combined-p sub horizontal)
a1511caf 1482 ;; For an iso-combination calculate how much we can get from
be7f5545 1483 ;; other child windows.
a1511caf
MR
1484 (let ((skip (eq trail 'after)))
1485 (while sub
1486 (cond
1487 ((eq sub window)
1488 (setq skip (eq trail 'before)))
1489 (skip)
1490 (t
1491 (setq delta
1492 (+ delta
8dd3e94f 1493 (max
04b577b6 1494 (- (window-size sub horizontal pixelwise 'floor)
8dd3e94f
MR
1495 (window-min-size
1496 sub horizontal ignore pixelwise))
1497 0)))))
a1511caf
MR
1498 (setq sub (window-right sub))))
1499 ;; For an ortho-combination throw DELTA when at least one
be7f5545 1500 ;; child window is fixed-size.
a1511caf
MR
1501 (while sub
1502 (when (and (not (eq sub window))
842e3a93 1503 (not (window--size-ignore-p sub ignore))
a1511caf
MR
1504 (window-size-fixed-p sub horizontal))
1505 (throw 'fixed delta))
1506 (setq sub (window-right sub))))
1507 (if noup
1508 ;; When NOUP is nil, DELTA is all we can get.
1509 delta
1510 ;; Else try with parent of WINDOW, passing the DELTA we
1511 ;; recovered so far.
880e6158
MR
1512 (window--max-delta-1
1513 parent delta horizontal ignore trail nil pixelwise))))))
a1511caf 1514
880e6158 1515(defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise)
2116e93c 1516 "Return maximum number of lines by which WINDOW can be enlarged.
85c2386b
MR
1517WINDOW must be a valid window and defaults to the selected one.
1518The return value is zero if WINDOW cannot be enlarged.
a1511caf
MR
1519
1520Optional argument HORIZONTAL non-nil means return maximum number
1521of columns by which WINDOW can be enlarged.
1522
2116e93c 1523Optional argument IGNORE non-nil means ignore restrictions
a1511caf 1524imposed by fixed size windows, `window-min-height' or
2116e93c
EZ
1525`window-min-width' settings. If IGNORE is a window, ignore
1526restrictions for that window only. If IGNORE equals `safe',
a1511caf 1527live windows may get as small as `window-safe-min-height' lines
2116e93c
EZ
1528and `window-safe-min-width' columns. Any other non-nil value means
1529ignore all of the above restrictions for all windows.
a1511caf 1530
2116e93c
EZ
1531Optional argument TRAIL restricts the windows that can be enlarged.
1532If its value is `before', only windows to the left of or above WINDOW
1533can be enlarged. If it is `after', only windows to the right of or
1534below WINDOW can be enlarged.
a1511caf
MR
1535
1536Optional argument NOUP non-nil means don't go up in the window
1537tree but try to obtain the entire space from windows within
1538WINDOW's combination.
1539
1540Optional argument NODOWN non-nil means do not check whether
382c953b 1541WINDOW itself (and its child windows) can be enlarged; check
880e6158
MR
1542only whether other windows can be shrunk appropriately.
1543
1544Optional argument PIXELWISE non-nil means return number of
1545pixels by which WINDOW can be enlarged."
447f16b8 1546 (setq window (window-normalize-window window))
842e3a93 1547 (if (and (not (window--size-ignore-p window ignore))
a1511caf
MR
1548 (not nodown) (window-size-fixed-p window horizontal))
1549 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1550 ;; size.
1551 0
1552 ;; WINDOW has no fixed size.
880e6158 1553 (window--max-delta-1 window 0 horizontal ignore trail noup pixelwise)))
a1511caf
MR
1554
1555;; Make NOUP also inhibit the min-size check.
880e6158 1556(defun window--resizable (window delta &optional horizontal ignore trail noup nodown pixelwise)
a1511caf 1557 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
85c2386b 1558WINDOW must be a valid window and defaults to the selected one.
a1511caf
MR
1559Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1560can be resized horizontally by DELTA columns. A return value of
1561zero means that WINDOW is not resizable.
1562
1563DELTA positive means WINDOW shall be enlarged by DELTA lines or
2cffd681 1564columns. If WINDOW cannot be enlarged by DELTA lines or columns,
a1511caf
MR
1565return the maximum value in the range 0..DELTA by which WINDOW
1566can be enlarged.
1567
1568DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1569columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1570return the minimum value in the range DELTA..0 that can be used
1571for shrinking WINDOW.
1572
2116e93c 1573Optional argument IGNORE non-nil means ignore restrictions
a1511caf 1574imposed by fixed size windows, `window-min-height' or
2116e93c
EZ
1575`window-min-width' settings. If IGNORE is a window, ignore
1576restrictions for that window only. If IGNORE equals `safe',
a1511caf 1577live windows may get as small as `window-safe-min-height' lines
2116e93c
EZ
1578and `window-safe-min-width' columns. Any other non-nil value
1579means ignore all of the above restrictions for all windows.
a1511caf
MR
1580
1581Optional argument TRAIL `before' means only windows to the left
1582of or below WINDOW can be shrunk. Optional argument TRAIL
1583`after' means only windows to the right of or above WINDOW can be
1584shrunk.
1585
1586Optional argument NOUP non-nil means don't go up in the window
2cffd681
MR
1587tree but check only whether space can be obtained from (or given
1588to) WINDOW's siblings.
a1511caf 1589
2cffd681
MR
1590Optional argument NODOWN non-nil means don't go down in the
1591window tree. This means do not check whether resizing would
880e6158
MR
1592violate size restrictions of WINDOW or its child windows.
1593
1594Optional argument PIXELWISE non-nil means interpret DELTA as
1595number of pixels."
447f16b8 1596 (setq window (window-normalize-window window))
a1511caf
MR
1597 (cond
1598 ((< delta 0)
880e6158
MR
1599 (max (- (window-min-delta
1600 window horizontal ignore trail noup nodown pixelwise))
a1511caf
MR
1601 delta))
1602 ((> delta 0)
880e6158
MR
1603 (min (window-max-delta
1604 window horizontal ignore trail noup nodown pixelwise)
a1511caf
MR
1605 delta))
1606 (t 0)))
1607
880e6158 1608(defun window--resizable-p (window delta &optional horizontal ignore trail noup nodown pixelwise)
a1511caf 1609 "Return t if WINDOW can be resized vertically by DELTA lines.
85c2386b 1610WINDOW must be a valid window and defaults to the selected one.
a1511caf 1611For the meaning of the arguments of this function see the
880e6158
MR
1612doc-string of `window--resizable'.
1613
1614Optional argument PIXELWISE non-nil means interpret DELTA as
1615pixels."
447f16b8 1616 (setq window (window-normalize-window window))
a1511caf 1617 (if (> delta 0)
880e6158
MR
1618 (>= (window--resizable
1619 window delta horizontal ignore trail noup nodown pixelwise)
a1511caf 1620 delta)
880e6158
MR
1621 (<= (window--resizable
1622 window delta horizontal ignore trail noup nodown pixelwise)
a1511caf
MR
1623 delta)))
1624
880e6158 1625(defun window-resizable (window delta &optional horizontal ignore pixelwise)
2cffd681 1626 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
85c2386b 1627WINDOW must be a valid window and defaults to the selected one.
2cffd681
MR
1628Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1629can be resized horizontally by DELTA columns. A return value of
1630zero means that WINDOW is not resizable.
1631
1632DELTA positive means WINDOW shall be enlarged by DELTA lines or
1633columns. If WINDOW cannot be enlarged by DELTA lines or columns
1634return the maximum value in the range 0..DELTA by which WINDOW
1635can be enlarged.
1636
1637DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1638columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1639return the minimum value in the range DELTA..0 that can be used
1640for shrinking WINDOW.
1641
2116e93c 1642Optional argument IGNORE non-nil means ignore restrictions
2cffd681 1643imposed by fixed size windows, `window-min-height' or
2116e93c
EZ
1644`window-min-width' settings. If IGNORE is a window, ignore
1645restrictions for that window only. If IGNORE equals `safe',
2cffd681 1646live windows may get as small as `window-safe-min-height' lines
2116e93c 1647and `window-safe-min-width' columns. Any other non-nil value
880e6158
MR
1648means ignore all of the above restrictions for all windows.
1649
1650Optional argument PIXELWISE non-nil means interpret DELTA as
1651pixels."
2cffd681 1652 (setq window (window-normalize-window window))
880e6158 1653 (window--resizable window delta horizontal ignore nil nil nil pixelwise))
2cffd681 1654
880e6158
MR
1655(defun window-resizable-p (window delta &optional horizontal ignore pixelwise)
1656 "Return t if WINDOW can be resized vertically by DELTA lines.
85c2386b 1657WINDOW must be a valid window and defaults to the selected one.
880e6158
MR
1658For the meaning of the arguments of this function see the
1659doc-string of `window-resizable'."
1660 (setq window (window-normalize-window window))
1661 (if (> delta 0)
1662 (>= (window--resizable
1663 window delta horizontal ignore nil nil nil pixelwise)
1664 delta)
1665 (<= (window--resizable
1666 window delta horizontal ignore nil nil nil pixelwise)
1667 delta)))
105216ed 1668
880e6158 1669;; Aliases of functions defined in window.c.
f3d1777e 1670(defalias 'window-height 'window-total-height)
880e6158
MR
1671(defalias 'window-width 'window-body-width)
1672
1673;; Eventually the following two should work pixelwise.
f3d1777e 1674
ccafbf06 1675;; See discussion in bug#4543.
4b0d61e3 1676(defun window-full-height-p (&optional window)
2116e93c 1677 "Return t if WINDOW is as high as its containing frame.
a1511caf
MR
1678More precisely, return t if and only if the total height of
1679WINDOW equals the total height of the root window of WINDOW's
85c2386b
MR
1680frame. WINDOW must be a valid window and defaults to the
1681selected one."
447f16b8 1682 (setq window (window-normalize-window window))
880e6158
MR
1683 (= (window-pixel-height window)
1684 (window-pixel-height (frame-root-window window))))
a1511caf 1685
4b0d61e3 1686(defun window-full-width-p (&optional window)
2116e93c 1687 "Return t if WINDOW is as wide as its containing frame.
a1511caf
MR
1688More precisely, return t if and only if the total width of WINDOW
1689equals the total width of the root window of WINDOW's frame.
85c2386b 1690WINDOW must be a valid window and defaults to the selected one."
447f16b8 1691 (setq window (window-normalize-window window))
880e6158
MR
1692 (= (window-pixel-width window)
1693 (window-pixel-width (frame-root-window window))))
a1511caf 1694
e1a2cb1c 1695(defun window-body-size (&optional window horizontal pixelwise)
105216ed 1696 "Return the height or width of WINDOW's text area.
85c2386b 1697WINDOW must be a live window and defaults to the selected one.
105216ed
CY
1698
1699If HORIZONTAL is omitted or nil, return the height of the text
1700area, like `window-body-height'. Otherwise, return the width of
e1a2cb1c
MR
1701the text area, like `window-body-width'. In either case, the
1702optional argument PIXELWISE is passed to the functions."
105216ed 1703 (if horizontal
e1a2cb1c
MR
1704 (window-body-width window pixelwise)
1705 (window-body-height window pixelwise)))
02c6f098 1706
3c448ab6
MR
1707(defun window-current-scroll-bars (&optional window)
1708 "Return the current scroll bar settings for WINDOW.
387522b2 1709WINDOW must be a live window and defaults to the selected one.
3c448ab6
MR
1710
1711The return value is a cons cell (VERTICAL . HORIZONTAL) where
1712VERTICAL specifies the current location of the vertical scroll
1713bars (`left', `right', or nil), and HORIZONTAL specifies the
1714current location of the horizontal scroll bars (`top', `bottom',
1715or nil).
1716
1717Unlike `window-scroll-bars', this function reports the scroll bar
1718type actually used, once frame defaults and `scroll-bar-mode' are
1719taken into account."
447f16b8 1720 (setq window (window-normalize-window window t))
3c448ab6
MR
1721 (let ((vert (nth 2 (window-scroll-bars window)))
1722 (hor nil))
1723 (when (or (eq vert t) (eq hor t))
387522b2 1724 (let ((fcsb (frame-current-scroll-bars (window-frame window))))
3c448ab6
MR
1725 (if (eq vert t)
1726 (setq vert (car fcsb)))
1727 (if (eq hor t)
1728 (setq hor (cdr fcsb)))))
1729 (cons vert hor)))
1730
c7635a97
CY
1731(defun walk-windows (fun &optional minibuf all-frames)
1732 "Cycle through all live windows, calling FUN for each one.
1733FUN must specify a function with a window as its sole argument.
3c448ab6 1734The optional arguments MINIBUF and ALL-FRAMES specify the set of
387522b2 1735windows to include in the walk.
3c448ab6
MR
1736
1737MINIBUF t means include the minibuffer window even if the
1738minibuffer is not active. MINIBUF nil or omitted means include
1739the minibuffer window only if the minibuffer is active. Any
1740other value means do not include the minibuffer window even if
1741the minibuffer is active.
1742
387522b2
MR
1743ALL-FRAMES nil or omitted means consider all windows on the
1744selected frame, plus the minibuffer window if specified by the
1745MINIBUF argument. If the minibuffer counts, consider all windows
1746on all frames that share that minibuffer too. The following
1747non-nil values of ALL-FRAMES have special meanings:
1748
1749- t means consider all windows on all existing frames.
1750
1751- `visible' means consider all windows on all visible frames on
1752 the current terminal.
1753
1754- 0 (the number zero) means consider all windows on all visible
1755 and iconified frames on the current terminal.
1756
1757- A frame means consider all windows on that frame only.
1758
1759Anything else means consider all windows on the selected frame
1760and no others.
3c448ab6
MR
1761
1762This function changes neither the order of recently selected
1763windows nor the buffer list."
1764 ;; If we start from the minibuffer window, don't fail to come
1765 ;; back to it.
290d5b58 1766 (when (window-minibuffer-p)
3c448ab6
MR
1767 (setq minibuf t))
1768 ;; Make sure to not mess up the order of recently selected
1769 ;; windows. Use `save-selected-window' and `select-window'
1770 ;; with second argument non-nil for this purpose.
1771 (save-selected-window
1772 (when (framep all-frames)
1773 (select-window (frame-first-window all-frames) 'norecord))
387522b2 1774 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
c7635a97 1775 (funcall fun walk-windows-window))))
387522b2 1776
e07b9a6d
MR
1777(defun window-at-side-p (&optional window side)
1778 "Return t if WINDOW is at SIDE of its containing frame.
85c2386b
MR
1779WINDOW must be a valid window and defaults to the selected one.
1780SIDE can be any of the symbols `left', `top', `right' or
1781`bottom'. The default value nil is handled like `bottom'."
447f16b8 1782 (setq window (window-normalize-window window))
e07b9a6d
MR
1783 (let ((edge
1784 (cond
1785 ((eq side 'left) 0)
1786 ((eq side 'top) 1)
1787 ((eq side 'right) 2)
1788 ((memq side '(bottom nil)) 3))))
880e6158
MR
1789 (= (nth edge (window-pixel-edges window))
1790 (nth edge (window-pixel-edges (frame-root-window window))))))
e07b9a6d 1791
54f9154c 1792(defun window-at-side-list (&optional frame side)
e07b9a6d
MR
1793 "Return list of all windows on SIDE of FRAME.
1794FRAME must be a live frame and defaults to the selected frame.
1795SIDE can be any of the symbols `left', `top', `right' or
1796`bottom'. The default value nil is handled like `bottom'."
1797 (setq frame (window-normalize-frame frame))
1798 (let (windows)
1799 (walk-window-tree
1800 (lambda (window)
1801 (when (window-at-side-p window side)
1802 (setq windows (cons window windows))))
ea95074e 1803 frame nil 'nomini)
e07b9a6d
MR
1804 (nreverse windows)))
1805
5e92ca23 1806(defun window--in-direction-2 (window posn &optional horizontal)
387522b2
MR
1807 "Support function for `window-in-direction'."
1808 (if horizontal
880e6158 1809 (let ((top (window-pixel-top window)))
387522b2
MR
1810 (if (> top posn)
1811 (- top posn)
880e6158
MR
1812 (- posn top (window-pixel-height window))))
1813 (let ((left (window-pixel-left window)))
387522b2
MR
1814 (if (> left posn)
1815 (- left posn)
880e6158 1816 (- posn left (window-pixel-width window))))))
387522b2 1817
ea95074e
MR
1818;; Predecessors to the below have been devised by Julian Assange in
1819;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
1820;; Neither of these allow to selectively ignore specific windows
1821;; (windows whose `no-other-window' parameter is non-nil) as targets of
1822;; the movement.
71e6691e 1823(defun window-in-direction (direction &optional window ignore sign wrap mini)
387522b2 1824 "Return window in DIRECTION as seen from WINDOW.
ea95074e
MR
1825More precisely, return the nearest window in direction DIRECTION
1826as seen from the position of `window-point' in window WINDOW.
387522b2
MR
1827DIRECTION must be one of `above', `below', `left' or `right'.
1828WINDOW must be a live window and defaults to the selected one.
ea95074e
MR
1829
1830Do not return a window whose `no-other-window' parameter is
1831non-nil. If the nearest window's `no-other-window' parameter is
1832non-nil, try to find another window in the indicated direction.
1833If, however, the optional argument IGNORE is non-nil, return that
1834window even if its `no-other-window' parameter is non-nil.
1835
71e6691e
MR
1836Optional argument SIGN a negative number means to use the right
1837or bottom edge of WINDOW as reference position instead of
1838`window-point'. SIGN a positive number means to use the left or
1839top edge of WINDOW as reference position.
1840
1841Optional argument WRAP non-nil means to wrap DIRECTION around
87132570
MR
1842frame borders. This means to return for WINDOW at the top of the
1843frame and DIRECTION `above' the minibuffer window if the frame
1844has one, and a window at the bottom of the frame otherwise.
71e6691e
MR
1845
1846Optional argument MINI nil means to return the minibuffer window
1847if and only if it is currently active. MINI non-nil means to
1848return the minibuffer window even when it's not active. However,
1849if WRAP non-nil, always act as if MINI were nil.
1850
ea95074e 1851Return nil if no suitable window can be found."
447f16b8 1852 (setq window (window-normalize-window window t))
387522b2
MR
1853 (unless (memq direction '(above below left right))
1854 (error "Wrong direction %s" direction))
1855 (let* ((frame (window-frame window))
1856 (hor (memq direction '(left right)))
1857 (first (if hor
880e6158
MR
1858 (window-pixel-left window)
1859 (window-pixel-top window)))
1860 (last (+ first (window-size window hor t)))
387522b2
MR
1861 ;; The column / row value of `posn-at-point' can be nil for the
1862 ;; mini-window, guard against that.
71e6691e
MR
1863 (posn
1864 (cond
1865 ((and (numberp sign) (< sign 0))
1866 (if hor
1867 (1- (+ (window-pixel-top window) (window-pixel-height window)))
1868 (1- (+ (window-pixel-left window) (window-pixel-width window)))))
1869 ((and (numberp sign) (> sign 0))
1870 (if hor
1871 (window-pixel-top window)
1872 (window-pixel-left window)))
1873 ((let ((posn-cons (nth 2 (posn-at-point (window-point window) window))))
1874 (if hor
1875 (+ (or (cdr posn-cons) 1) (window-pixel-top window))
1876 (+ (or (car posn-cons) 1) (window-pixel-left window)))))))
387522b2
MR
1877 (best-edge
1878 (cond
880e6158
MR
1879 ((eq direction 'below) (frame-pixel-height frame))
1880 ((eq direction 'right) (frame-pixel-width frame))
387522b2
MR
1881 (t -1)))
1882 (best-edge-2 best-edge)
880e6158 1883 (best-diff-2 (if hor (frame-pixel-height frame) (frame-pixel-width frame)))
387522b2
MR
1884 best best-2 best-diff-2-new)
1885 (walk-window-tree
1886 (lambda (w)
880e6158
MR
1887 (let* ((w-top (window-pixel-top w))
1888 (w-left (window-pixel-left w)))
387522b2
MR
1889 (cond
1890 ((or (eq window w)
1891 ;; Ignore ourselves.
1892 (and (window-parameter w 'no-other-window)
1893 ;; Ignore W unless IGNORE is non-nil.
1894 (not ignore))))
1895 (hor
1896 (cond
1897 ((and (<= w-top posn)
880e6158 1898 (< posn (+ w-top (window-pixel-height w))))
387522b2
MR
1899 ;; W is to the left or right of WINDOW and covers POSN.
1900 (when (or (and (eq direction 'left)
71e6691e
MR
1901 (or (and (<= w-left first) (> w-left best-edge))
1902 (and wrap
1903 (window-at-side-p window 'left)
1904 (window-at-side-p w 'right))))
387522b2 1905 (and (eq direction 'right)
71e6691e
MR
1906 (or (and (>= w-left last) (< w-left best-edge))
1907 (and wrap
1908 (window-at-side-p window 'right)
1909 (window-at-side-p w 'left)))))
387522b2
MR
1910 (setq best-edge w-left)
1911 (setq best w)))
1912 ((and (or (and (eq direction 'left)
880e6158 1913 (<= (+ w-left (window-pixel-width w)) first))
387522b2
MR
1914 (and (eq direction 'right) (<= last w-left)))
1915 ;; W is to the left or right of WINDOW but does not
1916 ;; cover POSN.
1917 (setq best-diff-2-new
5e92ca23 1918 (window--in-direction-2 w posn hor))
387522b2
MR
1919 (or (< best-diff-2-new best-diff-2)
1920 (and (= best-diff-2-new best-diff-2)
1921 (if (eq direction 'left)
1922 (> w-left best-edge-2)
1923 (< w-left best-edge-2)))))
1924 (setq best-edge-2 w-left)
1925 (setq best-diff-2 best-diff-2-new)
1926 (setq best-2 w))))
71e6691e
MR
1927 ((and (<= w-left posn)
1928 (< posn (+ w-left (window-pixel-width w))))
1929 ;; W is above or below WINDOW and covers POSN.
1930 (when (or (and (eq direction 'above)
1931 (or (and (<= w-top first) (> w-top best-edge))
1932 (and wrap
1933 (window-at-side-p window 'top)
1934 (if (active-minibuffer-window)
1935 (minibuffer-window-active-p w)
1936 (window-at-side-p w 'bottom)))))
1937 (and (eq direction 'below)
1938 (or (and (>= w-top first) (< w-top best-edge))
1939 (and wrap
1940 (if (active-minibuffer-window)
1941 (minibuffer-window-active-p window)
1942 (window-at-side-p window 'bottom))
1943 (window-at-side-p w 'top)))))
1944 (setq best-edge w-top)
1945 (setq best w)))
1946 ((and (or (and (eq direction 'above)
1947 (<= (+ w-top (window-pixel-height w)) first))
1948 (and (eq direction 'below) (<= last w-top)))
1949 ;; W is above or below WINDOW but does not cover POSN.
1950 (setq best-diff-2-new
1951 (window--in-direction-2 w posn hor))
1952 (or (< best-diff-2-new best-diff-2)
1953 (and (= best-diff-2-new best-diff-2)
1954 (if (eq direction 'above)
1955 (> w-top best-edge-2)
1956 (< w-top best-edge-2)))))
1957 (setq best-edge-2 w-top)
1958 (setq best-diff-2 best-diff-2-new)
1959 (setq best-2 w)))))
1960 frame nil (and mini t))
387522b2 1961 (or best best-2)))
3c448ab6 1962
4c43d97b 1963(defun get-window-with-predicate (predicate &optional minibuf all-frames default)
387522b2
MR
1964 "Return a live window satisfying PREDICATE.
1965More precisely, cycle through all windows calling the function
1966PREDICATE on each one of them with the window as its sole
1967argument. Return the first window for which PREDICATE returns
4c43d97b 1968non-nil. Windows are scanned starting with the window following
c80e3b4a 1969the selected window. If no window satisfies PREDICATE, return
4c43d97b
MR
1970DEFAULT.
1971
1972MINIBUF t means include the minibuffer window even if the
1973minibuffer is not active. MINIBUF nil or omitted means include
1974the minibuffer window only if the minibuffer is active. Any
1975other value means do not include the minibuffer window even if
1976the minibuffer is active.
387522b2
MR
1977
1978ALL-FRAMES nil or omitted means consider all windows on the selected
1979frame, plus the minibuffer window if specified by the MINIBUF
1980argument. If the minibuffer counts, consider all windows on all
1981frames that share that minibuffer too. The following non-nil
1982values of ALL-FRAMES have special meanings:
3c448ab6 1983
387522b2
MR
1984- t means consider all windows on all existing frames.
1985
1986- `visible' means consider all windows on all visible frames on
1987 the current terminal.
1988
1989- 0 (the number zero) means consider all windows on all visible
1990 and iconified frames on the current terminal.
1991
1992- A frame means consider all windows on that frame only.
1993
1994Anything else means consider all windows on the selected frame
1995and no others."
3c448ab6 1996 (catch 'found
4c43d97b
MR
1997 (dolist (window (window-list-1
1998 (next-window nil minibuf all-frames)
1999 minibuf all-frames))
387522b2
MR
2000 (when (funcall predicate window)
2001 (throw 'found window)))
3c448ab6
MR
2002 default))
2003
2004(defalias 'some-window 'get-window-with-predicate)
2005
51a5f9d8 2006(defun get-lru-window (&optional all-frames dedicated not-selected)
190b47e6
MR
2007 "Return the least recently used window on frames specified by ALL-FRAMES.
2008Return a full-width window if possible. A minibuffer window is
2009never a candidate. A dedicated window is never a candidate
2010unless DEDICATED is non-nil, so if all windows are dedicated, the
2011value is nil. Avoid returning the selected window if possible.
51a5f9d8
MR
2012Optional argument NOT-SELECTED non-nil means never return the
2013selected window.
190b47e6
MR
2014
2015The following non-nil values of the optional argument ALL-FRAMES
2016have special meanings:
2017
2018- t means consider all windows on all existing frames.
2019
2020- `visible' means consider all windows on all visible frames on
2021 the current terminal.
2022
2023- 0 (the number zero) means consider all windows on all visible
2024 and iconified frames on the current terminal.
2025
2026- A frame means consider all windows on that frame only.
2027
2028Any other value of ALL-FRAMES means consider all windows on the
2029selected frame and no others."
2030 (let (best-window best-time second-best-window second-best-time time)
02cfc6d6 2031 (dolist (window (window-list-1 nil 'nomini all-frames))
51a5f9d8
MR
2032 (when (and (or dedicated (not (window-dedicated-p window)))
2033 (or (not not-selected) (not (eq window (selected-window)))))
190b47e6
MR
2034 (setq time (window-use-time window))
2035 (if (or (eq window (selected-window))
2036 (not (window-full-width-p window)))
2037 (when (or (not second-best-time) (< time second-best-time))
2038 (setq second-best-time time)
2039 (setq second-best-window window))
2040 (when (or (not best-time) (< time best-time))
2041 (setq best-time time)
2042 (setq best-window window)))))
2043 (or best-window second-best-window)))
2044
51a5f9d8 2045(defun get-mru-window (&optional all-frames dedicated not-selected)
387522b2 2046 "Return the most recently used window on frames specified by ALL-FRAMES.
51a5f9d8
MR
2047A minibuffer window is never a candidate. A dedicated window is
2048never a candidate unless DEDICATED is non-nil, so if all windows
2049are dedicated, the value is nil. Optional argument NOT-SELECTED
2050non-nil means never return the selected window.
387522b2
MR
2051
2052The following non-nil values of the optional argument ALL-FRAMES
2053have special meanings:
2054
2055- t means consider all windows on all existing frames.
2056
2057- `visible' means consider all windows on all visible frames on
2058 the current terminal.
2059
2060- 0 (the number zero) means consider all windows on all visible
2061 and iconified frames on the current terminal.
2062
2063- A frame means consider all windows on that frame only.
2064
2065Any other value of ALL-FRAMES means consider all windows on the
2066selected frame and no others."
2067 (let (best-window best-time time)
02cfc6d6 2068 (dolist (window (window-list-1 nil 'nomini all-frames))
387522b2 2069 (setq time (window-use-time window))
51a5f9d8
MR
2070 (when (and (or dedicated (not (window-dedicated-p window)))
2071 (or (not not-selected) (not (eq window (selected-window))))
2072 (or (not best-time) (> time best-time)))
387522b2
MR
2073 (setq best-time time)
2074 (setq best-window window)))
2075 best-window))
2076
51a5f9d8 2077(defun get-largest-window (&optional all-frames dedicated not-selected)
190b47e6
MR
2078 "Return the largest window on frames specified by ALL-FRAMES.
2079A minibuffer window is never a candidate. A dedicated window is
2080never a candidate unless DEDICATED is non-nil, so if all windows
51a5f9d8
MR
2081are dedicated, the value is nil. Optional argument NOT-SELECTED
2082non-nil means never return the selected window.
190b47e6
MR
2083
2084The following non-nil values of the optional argument ALL-FRAMES
2085have special meanings:
2086
2087- t means consider all windows on all existing frames.
2088
2089- `visible' means consider all windows on all visible frames on
2090 the current terminal.
2091
2092- 0 (the number zero) means consider all windows on all visible
2093 and iconified frames on the current terminal.
2094
2095- A frame means consider all windows on that frame only.
2096
2097Any other value of ALL-FRAMES means consider all windows on the
2098selected frame and no others."
2099 (let ((best-size 0)
2100 best-window size)
02cfc6d6 2101 (dolist (window (window-list-1 nil 'nomini all-frames))
51a5f9d8
MR
2102 (when (and (or dedicated (not (window-dedicated-p window)))
2103 (or (not not-selected) (not (eq window (selected-window)))))
880e6158
MR
2104 (setq size (* (window-pixel-height window)
2105 (window-pixel-width window)))
190b47e6
MR
2106 (when (> size best-size)
2107 (setq best-size size)
2108 (setq best-window window))))
2109 best-window))
2110
3c448ab6
MR
2111(defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
2112 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2113BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4c43d97b
MR
2114and defaults to the current buffer. Windows are scanned starting
2115with the selected window.
190b47e6
MR
2116
2117MINIBUF t means include the minibuffer window even if the
2118minibuffer is not active. MINIBUF nil or omitted means include
2119the minibuffer window only if the minibuffer is active. Any
2120other value means do not include the minibuffer window even if
2121the minibuffer is active.
2122
2123ALL-FRAMES nil or omitted means consider all windows on the
2124selected frame, plus the minibuffer window if specified by the
2125MINIBUF argument. If the minibuffer counts, consider all windows
2126on all frames that share that minibuffer too. The following
2127non-nil values of ALL-FRAMES have special meanings:
2128
2129- t means consider all windows on all existing frames.
2130
2131- `visible' means consider all windows on all visible frames on
2132 the current terminal.
2133
2134- 0 (the number zero) means consider all windows on all visible
2135 and iconified frames on the current terminal.
2136
2137- A frame means consider all windows on that frame only.
2138
2139Anything else means consider all windows on the selected frame
2140and no others."
5386012d 2141 (let ((buffer (window-normalize-buffer buffer-or-name))
3c448ab6 2142 windows)
4c43d97b 2143 (dolist (window (window-list-1 (selected-window) minibuf all-frames))
190b47e6
MR
2144 (when (eq (window-buffer window) buffer)
2145 (setq windows (cons window windows))))
2146 (nreverse windows)))
3c448ab6
MR
2147
2148(defun minibuffer-window-active-p (window)
2149 "Return t if WINDOW is the currently active minibuffer window."
2150 (eq window (active-minibuffer-window)))
387522b2 2151
3c448ab6 2152(defun count-windows (&optional minibuf)
387522b2 2153 "Return the number of live windows on the selected frame.
3c448ab6
MR
2154The optional argument MINIBUF specifies whether the minibuffer
2155window shall be counted. See `walk-windows' for the precise
2156meaning of this argument."
387522b2 2157 (length (window-list-1 nil minibuf)))
9aab8e0d
MR
2158\f
2159;;; Resizing windows.
880e6158
MR
2160(defun window--size-to-pixel (window size &optional horizontal pixelwise round-maybe)
2161 "For WINDOW convert SIZE lines to pixels.
2162SIZE is supposed to specify a height of WINDOW in terms of text
2163lines. The return value is the number of pixels specifying that
2164height.
2165
2166WINDOW must be a valid window. Optional argument HORIZONTAL
2167non-nil means convert SIZE columns to pixels.
2168
2169Optional argument PIXELWISE non-nil means SIZE already specifies
2170pixels but may have to be adjusted to a multiple of the character
2171size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2172means round to the nearest multiple of the character size of
2173WINDOW's frame if the option `window-resize-pixelwise' is nil."
2174 (setq window (window-normalize-window window))
2175 (let ((char-size (frame-char-size window horizontal)))
2176 (if pixelwise
2177 (if (and round-maybe (not window-resize-pixelwise))
2178 (* (round size char-size) char-size)
2179 size)
2180 (* size char-size))))
2181
880e6158
MR
2182(defun window--pixel-to-total-1 (window horizontal char-size)
2183 "Subroutine of `window--pixel-to-total'."
2184 (let ((child (window-child window)))
2185 (if (window-combination-p window horizontal)
2186 ;; In an iso-combination distribute sizes proportionally.
2187 (let ((remainder (window-new-total window))
c44de18d 2188 size best-child rem best-rem)
880e6158
MR
2189 ;; Initialize total sizes to each child's floor.
2190 (while child
c44de18d 2191 (setq size (max (/ (window-size child horizontal t) char-size) 1))
880e6158
MR
2192 (set-window-new-total child size)
2193 (setq remainder (- remainder size))
2194 (setq child (window-next-sibling child)))
2195 ;; Distribute remainder.
2196 (while (> remainder 0)
2197 (setq child (window-last-child window))
2198 (setq best-child nil)
c44de18d 2199 (setq best-rem 0)
880e6158 2200 (while child
c44de18d
MR
2201 (when (and (<= (window-new-total child)
2202 (/ (window-size child horizontal t) char-size))
2203 (> (setq rem (% (window-size child horizontal t)
2204 char-size))
2205 best-rem))
2206 (setq best-child child)
2207 (setq best-rem rem))
880e6158
MR
2208 (setq child (window-prev-sibling child)))
2209 ;; We MUST have a best-child here.
2210 (set-window-new-total best-child 1 t)
2211 (setq remainder (1- remainder)))
2212 ;; Recurse.
2213 (setq child (window-child window))
2214 (while child
2215 (window--pixel-to-total-1 child horizontal char-size)
2216 (setq child (window-next-sibling child))))
2217 ;; In an ortho-combination assign new sizes directly.
2218 (let ((size (window-new-total window)))
2219 (while child
2220 (set-window-new-total child size)
2221 (window--pixel-to-total-1 child horizontal char-size)
2222 (setq child (window-next-sibling child)))))))
2223
2224(defun window--pixel-to-total (&optional frame horizontal)
2225 "On FRAME assign new total window heights from pixel heights.
2226FRAME must be a live frame and defaults to the selected frame.
2227
2228Optional argument HORIZONTAL non-nil means assign new total
2229window widths from pixel widths."
2230 (setq frame (window-normalize-frame frame))
c44de18d
MR
2231 (let* ((char-size (frame-char-size frame horizontal))
2232 (root (frame-root-window))
2233 (root-size (window-size root horizontal t))
2234 ;; We have to care about the minibuffer window only if it
2235 ;; appears together with the root window on this frame.
2236 (mini (let ((mini (minibuffer-window frame)))
2237 (and (eq (window-frame mini) frame)
2238 (not (eq mini root)) mini)))
2239 (mini-size (and mini (window-size mini horizontal t))))
2240 ;; We round the line/column sizes of windows here to the nearest
2241 ;; integer. In some cases this can make windows appear _larger_
2242 ;; than the containing frame (line/column-wise) because the latter's
2243 ;; sizes are not (yet) rounded. We might eventually fix that.
2244 (if (and mini (not horizontal))
2245 (let (lines)
2246 (set-window-new-total root (max (/ root-size char-size) 1))
2247 (set-window-new-total mini (max (/ mini-size char-size) 1))
2248 (setq lines (- (round (+ root-size mini-size) char-size)
2249 (+ (window-new-total root) (window-new-total mini))))
2250 (while (> lines 0)
2251 (if (>= (% root-size (window-new-total root))
2252 (% mini-size (window-new-total mini)))
2253 (set-window-new-total root 1 t)
2254 (set-window-new-total mini 1 t))
2255 (setq lines (1- lines))))
2256 (set-window-new-total root (round root-size char-size))
2257 (when mini
2258 ;; This is taken in the horizontal case only.
2259 (set-window-new-total mini (round mini-size char-size))))
880e6158 2260 (unless (window-buffer root)
c44de18d
MR
2261 (window--pixel-to-total-1 root horizontal char-size))
2262 ;; Apply the new sizes.
2263 (window-resize-apply-total frame horizontal)))
880e6158 2264
5386012d 2265(defun window--resize-reset (&optional frame horizontal)
9aab8e0d
MR
2266 "Reset resize values for all windows on FRAME.
2267FRAME defaults to the selected frame.
2268
880e6158 2269This function stores the current value of `window-size' applied
9aab8e0d
MR
2270with argument HORIZONTAL in the new total size of all windows on
2271FRAME. It also resets the new normal size of each of these
2272windows."
5386012d
MR
2273 (window--resize-reset-1
2274 (frame-root-window (window-normalize-frame frame)) horizontal))
9aab8e0d 2275
5386012d
MR
2276(defun window--resize-reset-1 (window horizontal)
2277 "Internal function of `window--resize-reset'."
9aab8e0d 2278 ;; Register old size in the new total size.
880e6158
MR
2279 (set-window-new-pixel window (window-size window horizontal t))
2280 (set-window-new-total window (window-size window horizontal))
9aab8e0d
MR
2281 ;; Reset new normal size.
2282 (set-window-new-normal window)
2283 (when (window-child window)
5386012d 2284 (window--resize-reset-1 (window-child window) horizontal))
9aab8e0d 2285 (when (window-right window)
5386012d 2286 (window--resize-reset-1 (window-right window) horizontal)))
9aab8e0d 2287
562dd5e9
MR
2288;; The following routine is used to manually resize the minibuffer
2289;; window and is currently used, for example, by ispell.el.
5386012d 2290(defun window--resize-mini-window (window delta)
880e6158
MR
2291 "Resize minibuffer window WINDOW by DELTA pixels.
2292If WINDOW cannot be resized by DELTA pixels make it as large (or
2116e93c 2293as small) as possible, but don't signal an error."
562dd5e9
MR
2294 (when (window-minibuffer-p window)
2295 (let* ((frame (window-frame window))
2296 (root (frame-root-window frame))
880e6158 2297 (height (window-pixel-height window))
562dd5e9 2298 (min-delta
880e6158
MR
2299 (- (window-pixel-height root)
2300 (window-min-size root nil nil t))))
562dd5e9
MR
2301 ;; Sanitize DELTA.
2302 (cond
2303 ((<= (+ height delta) 0)
880e6158 2304 (setq delta (- (frame-char-height (window-frame window)) height)))
562dd5e9
MR
2305 ((> delta min-delta)
2306 (setq delta min-delta)))
2307
880e6158
MR
2308 (unless (zerop delta)
2309 ;; Resize now.
2310 (window--resize-reset frame)
2311 ;; Ideally we should be able to resize just the last child of root
2312 ;; here. See the comment in `resize-root-window-vertically' for
2313 ;; why we do not do that.
2314 (window--resize-this-window root (- delta) nil nil t)
2315 (set-window-new-pixel window (+ height delta))
2316 ;; The following routine catches the case where we want to resize
2317 ;; a minibuffer-only frame.
2318 (when (resize-mini-window-internal window)
2319 (window--pixel-to-total frame)
2320 (run-window-configuration-change-hook frame))))))
2321
2322(defun window--resize-apply-p (frame &optional horizontal)
2323 "Return t when a window on FRAME shall be resized vertically.
2324Optional argument HORIZONTAL non-nil means return t when a window
2325shall be resized horizontally."
2326(catch 'apply
2327 (walk-window-tree
2328 (lambda (window)
2329 (unless (= (window-new-pixel window)
2330 (window-size window horizontal t))
2331 (throw 'apply t)))
2332 frame t)
2333 nil))
2334
2335(defun window-resize (window delta &optional horizontal ignore pixelwise)
562dd5e9
MR
2336 "Resize WINDOW vertically by DELTA lines.
2337WINDOW can be an arbitrary window and defaults to the selected
2338one. An attempt to resize the root window of a frame will raise
2339an error though.
2340
2341DELTA a positive number means WINDOW shall be enlarged by DELTA
2342lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2343lines.
2344
2345Optional argument HORIZONTAL non-nil means resize WINDOW
2346horizontally by DELTA columns. In this case a positive DELTA
2347means enlarge WINDOW by DELTA columns. DELTA negative means
2348WINDOW shall be shrunk by -DELTA columns.
2349
2116e93c 2350Optional argument IGNORE non-nil means ignore restrictions
562dd5e9 2351imposed by fixed size windows, `window-min-height' or
2116e93c
EZ
2352`window-min-width' settings. If IGNORE is a window, ignore
2353restrictions for that window only. If IGNORE equals `safe',
562dd5e9 2354live windows may get as small as `window-safe-min-height' lines
2116e93c
EZ
2355and `window-safe-min-width' columns. Any other non-nil value
2356means ignore all of the above restrictions for all windows.
562dd5e9 2357
880e6158
MR
2358Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2359pixels.
2360
562dd5e9
MR
2361This function resizes other windows proportionally and never
2362deletes any windows. If you want to move only the low (right)
2363edge of WINDOW consider using `adjust-window-trailing-edge'
2364instead."
447f16b8 2365 (setq window (window-normalize-window window))
562dd5e9 2366 (let* ((frame (window-frame window))
feeb6f53 2367 (minibuffer-window (minibuffer-window frame))
562dd5e9 2368 sibling)
880e6158
MR
2369 (setq delta (window--size-to-pixel
2370 window delta horizontal pixelwise t))
562dd5e9
MR
2371 (cond
2372 ((eq window (frame-root-window frame))
2373 (error "Cannot resize the root window of a frame"))
2374 ((window-minibuffer-p window)
41cfe0cb
MR
2375 (if horizontal
2376 (error "Cannot resize minibuffer window horizontally")
2377 (window--resize-mini-window window delta)))
feeb6f53
MR
2378 ((and (not horizontal)
2379 (window-full-height-p window)
2380 (eq (window-frame minibuffer-window) frame)
2381 (or (not resize-mini-windows)
2382 (eq minibuffer-window (active-minibuffer-window))))
2383 ;; If WINDOW is full height and either `resize-mini-windows' is
2384 ;; nil or the minibuffer window is active, resize the minibuffer
2385 ;; window.
2386 (window--resize-mini-window minibuffer-window (- delta)))
880e6158
MR
2387 ((window--resizable-p
2388 window delta horizontal ignore nil nil nil t)
5386012d
MR
2389 (window--resize-reset frame horizontal)
2390 (window--resize-this-window window delta horizontal ignore t)
a0c2d0ae 2391 (if (and (not window-combination-resize)
3d8daefe 2392 (window-combined-p window horizontal)
562dd5e9 2393 (setq sibling (or (window-right window) (window-left window)))
880e6158
MR
2394 (window-sizable-p
2395 sibling (- delta) horizontal ignore t))
a0c2d0ae 2396 ;; If window-combination-resize is nil, WINDOW is part of an
89d61221 2397 ;; iso-combination, and WINDOW's neighboring right or left
562dd5e9
MR
2398 ;; sibling can be resized as requested, resize that sibling.
2399 (let ((normal-delta
2400 (/ (float delta)
880e6158 2401 (window-size (window-parent window) horizontal t))))
5386012d 2402 (window--resize-this-window sibling (- delta) horizontal nil t)
562dd5e9
MR
2403 (set-window-new-normal
2404 window (+ (window-normal-size window horizontal)
2405 normal-delta))
2406 (set-window-new-normal
2407 sibling (- (window-normal-size sibling horizontal)
2408 normal-delta)))
2409 ;; Otherwise, resize all other windows in the same combination.
5386012d 2410 (window--resize-siblings window delta horizontal ignore))
880e6158 2411 (when (window--resize-apply-p frame horizontal)
8dd3e94f
MR
2412 (if (window-resize-apply frame horizontal)
2413 (progn
2414 (window--pixel-to-total frame horizontal)
2415 (run-window-configuration-change-hook frame))
2416 (error "Failed to apply resizing %s" window))))
562dd5e9
MR
2417 (t
2418 (error "Cannot resize window %s" window)))))
2419
880e6158 2420(defun window-resize-no-error (window delta &optional horizontal ignore pixelwise)
27fcfe31
MR
2421 "Resize WINDOW vertically if it is resizable by DELTA lines.
2422This function is like `window-resize' but does not signal an
2423error when WINDOW cannot be resized. For the meaning of the
880e6158
MR
2424optional arguments see the documentation of `window-resize'.
2425
2426Optional argument PIXELWISE non-nil means interpret DELTA as
2427pixels."
2428 (when (window--resizable-p
2429 window delta horizontal ignore nil nil nil pixelwise)
2430 (window-resize window delta horizontal ignore pixelwise)))
27fcfe31 2431
4b0d61e3 2432(defun window--resize-child-windows-skip-p (window)
9aab8e0d
MR
2433 "Return non-nil if WINDOW shall be skipped by resizing routines."
2434 (memq (window-new-normal window) '(ignore stuck skip)))
2435
be7f5545
MR
2436(defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
2437 "Recursively set new normal height of child windows of window PARENT.
9aab8e0d 2438HORIZONTAL non-nil means set the new normal width of these
be7f5545 2439windows. WINDOW specifies a child window of PARENT that has been
382c953b 2440resized by THIS-DELTA lines (columns).
9aab8e0d 2441
2116e93c
EZ
2442Optional argument TRAIL either `before' or `after' means set values
2443only for windows before or after WINDOW. Optional argument
2444OTHER-DELTA, a number, specifies that this many lines (columns)
382c953b 2445have been obtained from (or returned to) an ancestor window of
9aab8e0d
MR
2446PARENT in order to resize WINDOW."
2447 (let* ((delta-normal
880e6158
MR
2448 (if (and (= (- this-delta)
2449 (window-size window horizontal t))
9aab8e0d
MR
2450 (zerop other-delta))
2451 ;; When WINDOW gets deleted and we can return its entire
2452 ;; space to its siblings, use WINDOW's normal size as the
2453 ;; normal delta.
2454 (- (window-normal-size window horizontal))
2455 ;; In any other case calculate the normal delta from the
2456 ;; relation of THIS-DELTA to the total size of PARENT.
880e6158
MR
2457 (/ (float this-delta)
2458 (window-size parent horizontal t))))
9aab8e0d
MR
2459 (sub (window-child parent))
2460 (parent-normal 0.0)
2461 (skip (eq trail 'after)))
2462
be7f5545
MR
2463 ;; Set parent-normal to the sum of the normal sizes of all child
2464 ;; windows of PARENT that shall be resized, excluding only WINDOW
9aab8e0d
MR
2465 ;; and any windows specified by the optional TRAIL argument.
2466 (while sub
2467 (cond
2468 ((eq sub window)
2469 (setq skip (eq trail 'before)))
2470 (skip)
2471 (t
2472 (setq parent-normal
2473 (+ parent-normal (window-normal-size sub horizontal)))))
2474 (setq sub (window-right sub)))
2475
be7f5545 2476 ;; Set the new normal size of all child windows of PARENT from what
9aab8e0d
MR
2477 ;; they should have contributed for recovering THIS-DELTA lines
2478 ;; (columns).
2479 (setq sub (window-child parent))
2480 (setq skip (eq trail 'after))
2481 (while sub
2482 (cond
2483 ((eq sub window)
2484 (setq skip (eq trail 'before)))
2485 (skip)
2486 (t
2487 (let ((old-normal (window-normal-size sub horizontal)))
2488 (set-window-new-normal
2489 sub (min 1.0 ; Don't get larger than 1.
2490 (max (- old-normal
2491 (* (/ old-normal parent-normal)
2492 delta-normal))
2493 ;; Don't drop below 0.
2494 0.0))))))
2495 (setq sub (window-right sub)))
2496
2497 (when (numberp other-delta)
2498 ;; Set the new normal size of windows from what they should have
2499 ;; contributed for recovering OTHER-DELTA lines (columns).
880e6158
MR
2500 (setq delta-normal (/ (float (window-size parent horizontal t))
2501 (+ (window-size parent horizontal t)
9aab8e0d
MR
2502 other-delta)))
2503 (setq sub (window-child parent))
2504 (setq skip (eq trail 'after))
2505 (while sub
2506 (cond
2507 ((eq sub window)
2508 (setq skip (eq trail 'before)))
2509 (skip)
2510 (t
2511 (set-window-new-normal
2512 sub (min 1.0 ; Don't get larger than 1.
2513 (max (* (window-new-normal sub) delta-normal)
2514 ;; Don't drop below 0.
2515 0.0)))))
2516 (setq sub (window-right sub))))
2517
2518 ;; Set the new normal size of WINDOW to what is left by the sum of
2519 ;; the normal sizes of its siblings.
2520 (set-window-new-normal
2521 window
2522 (let ((sum 0))
2523 (setq sub (window-child parent))
2524 (while sub
2525 (cond
2526 ((eq sub window))
2527 ((not (numberp (window-new-normal sub)))
2528 (setq sum (+ sum (window-normal-size sub horizontal))))
2529 (t
2530 (setq sum (+ sum (window-new-normal sub)))))
2531 (setq sub (window-right sub)))
2532 ;; Don't get larger than 1 or smaller than 0.
2533 (min 1.0 (max (- 1.0 sum) 0.0))))))
2534
880e6158
MR
2535(defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge char-size)
2536 "Resize child windows of window PARENT vertically by DELTA pixels.
9aab8e0d
MR
2537PARENT must be a vertically combined internal window.
2538
880e6158
MR
2539Optional argument HORIZONTAL non-nil means resize child windows
2540of PARENT horizontally by DELTA pixels. In this case PARENT must
9aab8e0d
MR
2541be a horizontally combined internal window.
2542
2543WINDOW, if specified, must denote a child window of PARENT that
880e6158 2544is resized by DELTA pixels.
9aab8e0d 2545
2116e93c 2546Optional argument IGNORE non-nil means ignore restrictions
9aab8e0d 2547imposed by fixed size windows, `window-min-height' or
2116e93c 2548`window-min-width' settings. If IGNORE equals `safe', live
9aab8e0d 2549windows may get as small as `window-safe-min-height' lines and
2116e93c
EZ
2550`window-safe-min-width' columns. If IGNORE is a window, ignore
2551restrictions for that window only. Any other non-nil value means
2552ignore all of the above restrictions for all windows.
9aab8e0d
MR
2553
2554Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2555of windows that shall be resized. If TRAIL equals `before',
2556resize only windows on the left or above EDGE. If TRAIL equals
2557`after', resize only windows on the right or below EDGE. Also,
2558preferably only resize windows adjacent to EDGE.
2559
880e6158
MR
2560If the optional argument CHAR-SIZE is a positive integer, it specifies
2561the number of pixels by which windows are incrementally resized.
2562If CHAR-SIZE is nil, this means to use the value of
2563`frame-char-height' or `frame-char-width' of WINDOW's frame.
2564
9aab8e0d
MR
2565Return the symbol `normalized' if new normal sizes have been
2566already set by this routine."
2567 (let* ((first (window-child parent))
9c52dd5a 2568 (last (window-last-child parent))
880e6158
MR
2569 (parent-total (+ (window-size parent horizontal t)
2570 delta))
2571 (char-size (or char-size
2572 (and window-resize-pixelwise 1)
2573 (frame-char-size window horizontal)))
2574 sub best-window best-value best-delta)
9aab8e0d
MR
2575
2576 (if (and edge (memq trail '(before after))
2577 (progn
2578 (setq sub first)
2579 (while (and (window-right sub)
2580 (or (and (eq trail 'before)
be7f5545 2581 (not (window--resize-child-windows-skip-p
9aab8e0d
MR
2582 (window-right sub))))
2583 (and (eq trail 'after)
be7f5545 2584 (window--resize-child-windows-skip-p sub))))
9aab8e0d
MR
2585 (setq sub (window-right sub)))
2586 sub)
2587 (if horizontal
2588 (if (eq trail 'before)
880e6158 2589 (= (+ (window-pixel-left sub) (window-pixel-width sub))
9aab8e0d 2590 edge)
880e6158 2591 (= (window-pixel-left sub) edge))
9aab8e0d 2592 (if (eq trail 'before)
880e6158 2593 (= (+ (window-pixel-top sub) (window-pixel-height sub))
9aab8e0d 2594 edge)
880e6158
MR
2595 (= (window-pixel-top sub) edge)))
2596 (window-sizable-p sub delta horizontal ignore t))
9aab8e0d
MR
2597 ;; Resize only windows adjacent to EDGE.
2598 (progn
5386012d
MR
2599 (window--resize-this-window
2600 sub delta horizontal ignore t trail edge)
9aab8e0d
MR
2601 (if (and window (eq (window-parent sub) parent))
2602 (progn
2603 ;; Assign new normal sizes.
2604 (set-window-new-normal
880e6158 2605 sub (/ (float (window-new-pixel sub)) parent-total))
9aab8e0d
MR
2606 (set-window-new-normal
2607 window (- (window-normal-size window horizontal)
2608 (- (window-new-normal sub)
2609 (window-normal-size sub horizontal)))))
be7f5545 2610 (window--resize-child-windows-normal
5386012d
MR
2611 parent horizontal sub 0 trail delta))
2612 ;; Return 'normalized to notify `window--resize-siblings' that
9aab8e0d
MR
2613 ;; normal sizes have been already set.
2614 'normalized)
2615 ;; Resize all windows proportionally.
9c52dd5a 2616 (setq sub last)
9aab8e0d
MR
2617 (while sub
2618 (cond
be7f5545
MR
2619 ((or (window--resize-child-windows-skip-p sub)
2620 ;; Ignore windows to skip and fixed-size child windows -
2621 ;; in the latter case make it a window to skip.
9aab8e0d
MR
2622 (and (not ignore)
2623 (window-size-fixed-p sub horizontal)
2624 (set-window-new-normal sub 'ignore))))
2625 ((< delta 0)
2626 ;; When shrinking store the number of lines/cols we can get
2627 ;; from this window here together with the total/normal size
2628 ;; factor.
2629 (set-window-new-normal
2630 sub
2631 (cons
2632 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
880e6158
MR
2633 (window-min-delta sub horizontal ignore trail t nil t)
2634 (- (/ (float (window-size sub horizontal t))
9aab8e0d
MR
2635 parent-total)
2636 (window-normal-size sub horizontal)))))
2637 ((> delta 0)
2638 ;; When enlarging store the total/normal size factor only
2639 (set-window-new-normal
2640 sub
880e6158 2641 (- (/ (float (window-size sub horizontal t))
9aab8e0d
MR
2642 parent-total)
2643 (window-normal-size sub horizontal)))))
2644
9c52dd5a 2645 (setq sub (window-left sub)))
9aab8e0d
MR
2646
2647 (cond
2648 ((< delta 0)
2649 ;; Shrink windows by delta.
2650 (setq best-window t)
2651 (while (and best-window (not (zerop delta)))
9c52dd5a 2652 (setq sub last)
9aab8e0d
MR
2653 (setq best-window nil)
2654 (setq best-value most-negative-fixnum)
2655 (while sub
2656 (when (and (consp (window-new-normal sub))
8dd3e94f 2657 (not (<= (car (window-new-normal sub)) 0))
9aab8e0d
MR
2658 (> (cdr (window-new-normal sub)) best-value))
2659 (setq best-window sub)
2660 (setq best-value (cdr (window-new-normal sub))))
2661
9c52dd5a 2662 (setq sub (window-left sub)))
9aab8e0d
MR
2663
2664 (when best-window
880e6158
MR
2665 (setq best-delta (min (car (window-new-normal best-window))
2666 char-size (- delta)))
2667 (setq delta (+ delta best-delta))
2668 (set-window-new-pixel best-window (- best-delta) t)
2669 (set-window-new-normal
2670 best-window
2671 (if (= (car (window-new-normal best-window)) best-delta)
2672 'skip ; We can't shrink best-window any further.
8dd3e94f 2673 (cons (- (car (window-new-normal best-window)) best-delta)
880e6158
MR
2674 (- (/ (float (window-new-pixel best-window))
2675 parent-total)
2676 (window-normal-size best-window horizontal))))))))
9aab8e0d
MR
2677 ((> delta 0)
2678 ;; Enlarge windows by delta.
2679 (setq best-window t)
2680 (while (and best-window (not (zerop delta)))
9c52dd5a 2681 (setq sub last)
9aab8e0d
MR
2682 (setq best-window nil)
2683 (setq best-value most-positive-fixnum)
2684 (while sub
2685 (when (and (numberp (window-new-normal sub))
2686 (< (window-new-normal sub) best-value))
2687 (setq best-window sub)
2688 (setq best-value (window-new-normal sub)))
2689
9c52dd5a 2690 (setq sub (window-left sub)))
9aab8e0d
MR
2691
2692 (when best-window
880e6158
MR
2693 (setq best-delta (min delta char-size))
2694 (setq delta (- delta best-delta))
2695 (set-window-new-pixel best-window best-delta t)
2696 (set-window-new-normal
2697 best-window
2698 (- (/ (float (window-new-pixel best-window))
2699 parent-total)
2700 (window-normal-size best-window horizontal)))))))
9aab8e0d
MR
2701
2702 (when best-window
9c52dd5a 2703 (setq sub last)
9aab8e0d
MR
2704 (while sub
2705 (when (or (consp (window-new-normal sub))
2706 (numberp (window-new-normal sub)))
d615d6d2 2707 ;; Reset new normal size fields so `window-resize-apply'
9aab8e0d
MR
2708 ;; won't use them to apply new sizes.
2709 (set-window-new-normal sub))
2710
2711 (unless (eq (window-new-normal sub) 'ignore)
be7f5545 2712 ;; Resize this window's child windows (back-engineering
9aab8e0d 2713 ;; delta from sub's old and new total sizes).
880e6158
MR
2714 (let ((delta (- (window-new-pixel sub)
2715 (window-size sub horizontal t))))
9aab8e0d
MR
2716 (unless (and (zerop delta) (not trail))
2717 ;; For the TRAIL non-nil case we have to resize SUB
2718 ;; recursively even if it's size does not change.
5386012d 2719 (window--resize-this-window
9aab8e0d 2720 sub delta horizontal ignore nil trail edge))))
9c52dd5a 2721 (setq sub (window-left sub)))))))
9aab8e0d 2722
880e6158
MR
2723(defun window--resize-siblings (window delta &optional horizontal ignore trail edge char-size)
2724 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
9aab8e0d 2725Optional argument HORIZONTAL non-nil means resize other windows
880e6158 2726when WINDOW is resized horizontally by DELTA pixels. WINDOW
9aab8e0d
MR
2727itself is not resized by this function.
2728
2116e93c 2729Optional argument IGNORE non-nil means ignore restrictions
9aab8e0d 2730imposed by fixed size windows, `window-min-height' or
2116e93c 2731`window-min-width' settings. If IGNORE equals `safe', live
9aab8e0d 2732windows may get as small as `window-safe-min-height' lines and
2116e93c
EZ
2733`window-safe-min-width' columns. If IGNORE is a window, ignore
2734restrictions for that window only. Any other non-nil value means
2735ignore all of the above restrictions for all windows.
9aab8e0d
MR
2736
2737Optional arguments TRAIL and EDGE, when non-nil, refine the set
2738of windows that shall be resized. If TRAIL equals `before',
2739resize only windows on the left or above EDGE. If TRAIL equals
2740`after', resize only windows on the right or below EDGE. Also,
2741preferably only resize windows adjacent to EDGE."
2742 (when (window-parent window)
2743 (let* ((parent (window-parent window))
2744 (sub (window-child parent)))
3d8daefe 2745 (if (window-combined-p sub horizontal)
9aab8e0d
MR
2746 ;; In an iso-combination try to extract DELTA from WINDOW's
2747 ;; siblings.
cb882333 2748 (let ((skip (eq trail 'after))
9aab8e0d
MR
2749 this-delta other-delta)
2750 ;; Decide which windows shall be left alone.
2751 (while sub
2752 (cond
2753 ((eq sub window)
2754 ;; Make sure WINDOW is left alone when
2755 ;; resizing its siblings.
2756 (set-window-new-normal sub 'ignore)
2757 (setq skip (eq trail 'before)))
2758 (skip
2759 ;; Make sure this sibling is left alone when
2760 ;; resizing its siblings.
2761 (set-window-new-normal sub 'ignore))
842e3a93 2762 ((or (window--size-ignore-p sub ignore)
9aab8e0d
MR
2763 (not (window-size-fixed-p sub horizontal)))
2764 ;; Set this-delta to t to signal that we found a sibling
2765 ;; of WINDOW whose size is not fixed.
2766 (setq this-delta t)))
2767
2768 (setq sub (window-right sub)))
2769
2770 ;; Set this-delta to what we can get from WINDOW's siblings.
880e6158 2771 (if (= (- delta) (window-size window horizontal t))
9aab8e0d 2772 ;; A deletion, presumably. We must handle this case
2cffd681 2773 ;; specially since `window--resizable' can't be used.
9aab8e0d
MR
2774 (if this-delta
2775 ;; There's at least one resizable sibling we can
2776 ;; give WINDOW's size to.
2777 (setq this-delta delta)
2778 ;; No resizable sibling exists.
2779 (setq this-delta 0))
2780 ;; Any other form of resizing.
2781 (setq this-delta
880e6158
MR
2782 (window--resizable
2783 window delta horizontal ignore trail t nil t)))
9aab8e0d
MR
2784
2785 ;; Set other-delta to what we still have to get from
2786 ;; ancestor windows of parent.
2787 (setq other-delta (- delta this-delta))
2788 (unless (zerop other-delta)
2789 ;; Unless we got everything from WINDOW's siblings, PARENT
2790 ;; must be resized by other-delta lines or columns.
880e6158 2791 (set-window-new-pixel parent other-delta 'add))
9aab8e0d
MR
2792
2793 (if (zerop this-delta)
2794 ;; We haven't got anything from WINDOW's siblings but we
2795 ;; must update the normal sizes to respect other-delta.
be7f5545 2796 (window--resize-child-windows-normal
9aab8e0d
MR
2797 parent horizontal window this-delta trail other-delta)
2798 ;; We did get something from WINDOW's siblings which means
be7f5545
MR
2799 ;; we have to resize their child windows.
2800 (unless (eq (window--resize-child-windows
5386012d 2801 parent (- this-delta) horizontal
880e6158 2802 window ignore trail edge char-size)
be7f5545 2803 ;; If `window--resize-child-windows' returns
5386012d
MR
2804 ;; 'normalized, this means it has set the
2805 ;; normal sizes already.
9aab8e0d
MR
2806 'normalized)
2807 ;; Set the normal sizes.
be7f5545 2808 (window--resize-child-windows-normal
9aab8e0d
MR
2809 parent horizontal window this-delta trail other-delta))
2810 ;; Set DELTA to what we still have to get from ancestor
2811 ;; windows.
2812 (setq delta other-delta)))
2813
2814 ;; In an ortho-combination all siblings of WINDOW must be
2815 ;; resized by DELTA.
880e6158 2816 (set-window-new-pixel parent delta 'add)
9aab8e0d
MR
2817 (while sub
2818 (unless (eq sub window)
880e6158
MR
2819 (window--resize-this-window
2820 sub delta horizontal ignore t))
9aab8e0d
MR
2821 (setq sub (window-right sub))))
2822
2823 (unless (zerop delta)
2824 ;; "Go up."
5386012d 2825 (window--resize-siblings
880e6158 2826 parent delta horizontal ignore trail edge char-size)))))
9aab8e0d 2827
880e6158
MR
2828(defun window--resize-this-window (window delta &optional horizontal ignore add trail edge char-size)
2829 "Resize WINDOW vertically by DELTA pixels.
9aab8e0d 2830Optional argument HORIZONTAL non-nil means resize WINDOW
880e6158 2831horizontally by DELTA pixels.
9aab8e0d 2832
2116e93c 2833Optional argument IGNORE non-nil means ignore restrictions
9aab8e0d 2834imposed by fixed size windows, `window-min-height' or
2116e93c 2835`window-min-width' settings. If IGNORE equals `safe', live
9aab8e0d 2836windows may get as small as `window-safe-min-height' lines and
2116e93c
EZ
2837`window-safe-min-width' columns. If IGNORE is a window, ignore
2838restrictions for that window only. Any other non-nil value
2839means ignore all of the above restrictions for all windows.
9aab8e0d
MR
2840
2841Optional argument ADD non-nil means add DELTA to the new total
2842size of WINDOW.
2843
2844Optional arguments TRAIL and EDGE, when non-nil, refine the set
2845of windows that shall be resized. If TRAIL equals `before',
2846resize only windows on the left or above EDGE. If TRAIL equals
2847`after', resize only windows on the right or below EDGE. Also,
2848preferably only resize windows adjacent to EDGE.
2849
880e6158
MR
2850If the optional argument CHAR-SIZE is a positive integer, it specifies
2851the number of pixels by which windows are incrementally resized.
2852If CHAR-SIZE is nil, this means to use the value of
2853`frame-char-height' or `frame-char-width' of WINDOW's frame.
2854
be7f5545 2855This function recursively resizes WINDOW's child windows to fit the
2cffd681 2856new size. Make sure that WINDOW is `window--resizable' before
9aab8e0d
MR
2857calling this function. Note that this function does not resize
2858siblings of WINDOW or WINDOW's parent window. You have to
d615d6d2 2859eventually call `window-resize-apply' in order to make resizing
9aab8e0d
MR
2860actually take effect."
2861 (when add
2862 ;; Add DELTA to the new total size of WINDOW.
880e6158 2863 (set-window-new-pixel window delta t))
387522b2 2864
9aab8e0d
MR
2865 (let ((sub (window-child window)))
2866 (cond
2867 ((not sub))
3d8daefe 2868 ((window-combined-p sub horizontal)
be7f5545 2869 ;; In an iso-combination resize child windows according to their
9aab8e0d 2870 ;; normal sizes.
be7f5545 2871 (window--resize-child-windows
880e6158 2872 window delta horizontal nil ignore trail edge char-size))
be7f5545 2873 ;; In an ortho-combination resize each child window by DELTA.
9aab8e0d
MR
2874 (t
2875 (while sub
5386012d 2876 (window--resize-this-window
880e6158 2877 sub delta horizontal ignore t trail edge char-size)
9aab8e0d
MR
2878 (setq sub (window-right sub)))))))
2879
880e6158 2880(defun window--resize-root-window (window delta horizontal ignore pixelwise)
9aab8e0d
MR
2881 "Resize root window WINDOW vertically by DELTA lines.
2882HORIZONTAL non-nil means resize root window WINDOW horizontally
2883by DELTA columns.
2884
2885IGNORE non-nil means ignore any restrictions imposed by fixed
2886size windows, `window-min-height' or `window-min-width' settings.
2887
2888This function is only called by the frame resizing routines. It
2889resizes windows proportionally and never deletes any windows."
880e6158
MR
2890 (when (and (windowp window) (numberp delta))
2891 (let ((pixel-delta
2892 (if pixelwise
2893 delta
2894 (window--size-to-pixel window delta horizontal))))
2895 (when (window-sizable-p window pixel-delta horizontal ignore t)
2896 (window--resize-reset (window-frame window) horizontal)
2897 (window--resize-this-window
2898 window pixel-delta horizontal ignore t)))))
9aab8e0d 2899
880e6158 2900(defun window--resize-root-window-vertically (window delta pixelwise)
9aab8e0d
MR
2901 "Resize root window WINDOW vertically by DELTA lines.
2902If DELTA is less than zero and we can't shrink WINDOW by DELTA
2903lines, shrink it as much as possible. If DELTA is greater than
be7f5545 2904zero, this function can resize fixed-size windows in order to
880e6158
MR
2905recover the necessary lines. Return the number of lines that
2906were recovered.
9aab8e0d 2907
f224e500 2908Third argument PIXELWISE non-nil means to interpret DELTA as
880e6158 2909pixels and return the number of pixels that were recovered.
9aab8e0d 2910
880e6158
MR
2911This function is called by the minibuffer window resizing
2912routines."
2913 (let* ((frame (window-frame window))
2914 (pixel-delta
2915 (cond
2916 (pixelwise
2917 delta)
2918 ((numberp delta)
2919 (* (frame-char-height frame) delta))
2920 (t 0)))
2921 ignore)
9c52dd5a 2922 (cond
880e6158
MR
2923 ((zerop pixel-delta))
2924 ((< pixel-delta 0)
2925 (setq pixel-delta (window-sizable window pixel-delta nil nil pixelwise))
9c52dd5a
MR
2926 (window--resize-reset frame)
2927 ;; When shrinking the root window, emulate an edge drag in order
2928 ;; to not resize other windows if we can avoid it (Bug#12419).
2929 (window--resize-this-window
880e6158
MR
2930 window pixel-delta nil ignore t 'before
2931 (+ (window-pixel-top window) (window-pixel-height window)))
9c52dd5a
MR
2932 ;; Don't record new normal sizes to make sure that shrinking back
2933 ;; proportionally works as intended.
2934 (walk-window-tree
2935 (lambda (window) (set-window-new-normal window 'ignore)) frame t))
880e6158 2936 ((> pixel-delta 0)
9c52dd5a 2937 (window--resize-reset frame)
880e6158 2938 (unless (window-sizable window pixel-delta nil nil pixelwise)
9c52dd5a
MR
2939 (setq ignore t))
2940 ;; When growing the root window, resize proportionally. This
2941 ;; should give windows back their original sizes (hopefully).
880e6158
MR
2942 (window--resize-this-window
2943 window pixel-delta nil ignore t)))
9c52dd5a 2944 ;; Return the possibly adjusted DELTA.
880e6158
MR
2945 (if pixelwise
2946 pixel-delta
2947 (/ pixel-delta (frame-char-height frame)))))
9aab8e0d 2948
880e6158 2949(defun adjust-window-trailing-edge (window delta &optional horizontal pixelwise)
562dd5e9
MR
2950 "Move WINDOW's bottom edge by DELTA lines.
2951Optional argument HORIZONTAL non-nil means move WINDOW's right
85c2386b
MR
2952edge by DELTA columns. WINDOW must be a valid window and
2953defaults to the selected one.
562dd5e9 2954
880e6158
MR
2955Optional argument PIXELWISE non-nil means interpret DELTA as
2956number of pixels.
2957
2116e93c 2958If DELTA is greater than zero, move the edge downwards or to the
562dd5e9
MR
2959right. If DELTA is less than zero, move the edge upwards or to
2960the left. If the edge can't be moved by DELTA lines or columns,
2961move it as far as possible in the desired direction."
447f16b8 2962 (setq window (window-normalize-window window))
41cfe0cb
MR
2963 (let* ((frame (window-frame window))
2964 (minibuffer-window (minibuffer-window frame))
2965 (right window)
2966 left this-delta min-delta max-delta)
880e6158
MR
2967
2968 (unless pixelwise
2969 (setq pixelwise t)
2970 (setq delta (* delta (frame-char-size window horizontal))))
2971
562dd5e9 2972 ;; Find the edge we want to move.
3d8daefe 2973 (while (and (or (not (window-combined-p right horizontal))
562dd5e9
MR
2974 (not (window-right right)))
2975 (setq right (window-parent right))))
2976 (cond
41cfe0cb
MR
2977 ((and (not right) (not horizontal)
2978 ;; Resize the minibuffer window if it's on the same frame as
2979 ;; and immediately below WINDOW and it's either active or
2980 ;; `resize-mini-windows' is nil.
2981 (eq (window-frame minibuffer-window) frame)
880e6158
MR
2982 (= (nth 1 (window-pixel-edges minibuffer-window))
2983 (nth 3 (window-pixel-edges window)))
41cfe0cb
MR
2984 (or (not resize-mini-windows)
2985 (eq minibuffer-window (active-minibuffer-window))))
2986 (window--resize-mini-window minibuffer-window (- delta)))
562dd5e9
MR
2987 ((or (not (setq left right)) (not (setq right (window-right right))))
2988 (if horizontal
2989 (error "No window on the right of this one")
2990 (error "No window below this one")))
2991 (t
2992 ;; Set LEFT to the first resizable window on the left. This step is
2993 ;; needed to handle fixed-size windows.
2994 (while (and left (window-size-fixed-p left horizontal))
2995 (setq left
2996 (or (window-left left)
2997 (progn
2998 (while (and (setq left (window-parent left))
3d8daefe 2999 (not (window-combined-p left horizontal))))
562dd5e9
MR
3000 (window-left left)))))
3001 (unless left
3002 (if horizontal
3003 (error "No resizable window on the left of this one")
3004 (error "No resizable window above this one")))
3005
3006 ;; Set RIGHT to the first resizable window on the right. This step
3007 ;; is needed to handle fixed-size windows.
3008 (while (and right (window-size-fixed-p right horizontal))
3009 (setq right
3010 (or (window-right right)
3011 (progn
3012 (while (and (setq right (window-parent right))
3d8daefe 3013 (not (window-combined-p right horizontal))))
562dd5e9
MR
3014 (window-right right)))))
3015 (unless right
3016 (if horizontal
3017 (error "No resizable window on the right of this one")
3018 (error "No resizable window below this one")))
3019
3020 ;; LEFT and RIGHT (which might be both internal windows) are now the
3021 ;; two windows we want to resize.
3022 (cond
3023 ((> delta 0)
880e6158
MR
3024 (setq max-delta
3025 (window--max-delta-1
3026 left 0 horizontal nil 'after nil pixelwise))
3027 (setq min-delta
3028 (window--min-delta-1
3029 right (- delta) horizontal nil 'before nil pixelwise))
562dd5e9
MR
3030 (when (or (< max-delta delta) (> min-delta (- delta)))
3031 ;; We can't get the whole DELTA - move as far as possible.
3032 (setq delta (min max-delta (- min-delta))))
3033 (unless (zerop delta)
3034 ;; Start resizing.
5386012d 3035 (window--resize-reset frame horizontal)
562dd5e9 3036 ;; Try to enlarge LEFT first.
880e6158 3037 (setq this-delta (window--resizable
8dd3e94f 3038 left delta horizontal nil 'after nil nil pixelwise))
562dd5e9 3039 (unless (zerop this-delta)
5386012d 3040 (window--resize-this-window
562dd5e9
MR
3041 left this-delta horizontal nil t 'before
3042 (if horizontal
880e6158
MR
3043 (+ (window-pixel-left left) (window-pixel-width left))
3044 (+ (window-pixel-top left) (window-pixel-height left)))))
562dd5e9 3045 ;; Shrink windows on right of LEFT.
5386012d 3046 (window--resize-siblings
562dd5e9
MR
3047 left delta horizontal nil 'after
3048 (if horizontal
880e6158
MR
3049 (window-pixel-left right)
3050 (window-pixel-top right)))))
562dd5e9 3051 ((< delta 0)
880e6158
MR
3052 (setq max-delta
3053 (window--max-delta-1
3054 right 0 horizontal nil 'before nil pixelwise))
3055 (setq min-delta
3056 (window--min-delta-1
3057 left delta horizontal nil 'after nil pixelwise))
562dd5e9
MR
3058 (when (or (< max-delta (- delta)) (> min-delta delta))
3059 ;; We can't get the whole DELTA - move as far as possible.
3060 (setq delta (max (- max-delta) min-delta)))
3061 (unless (zerop delta)
3062 ;; Start resizing.
5386012d 3063 (window--resize-reset frame horizontal)
562dd5e9 3064 ;; Try to enlarge RIGHT.
880e6158
MR
3065 (setq this-delta
3066 (window--resizable
8dd3e94f 3067 right (- delta) horizontal nil 'before nil nil pixelwise))
562dd5e9 3068 (unless (zerop this-delta)
5386012d 3069 (window--resize-this-window
562dd5e9
MR
3070 right this-delta horizontal nil t 'after
3071 (if horizontal
880e6158
MR
3072 (window-pixel-left right)
3073 (window-pixel-top right))))
562dd5e9 3074 ;; Shrink windows on left of RIGHT.
5386012d 3075 (window--resize-siblings
562dd5e9
MR
3076 right (- delta) horizontal nil 'before
3077 (if horizontal
880e6158
MR
3078 (+ (window-pixel-left left) (window-pixel-width left))
3079 (+ (window-pixel-top left) (window-pixel-height left)))))))
562dd5e9
MR
3080 (unless (zerop delta)
3081 ;; Don't report an error in the standard case.
880e6158
MR
3082 (when (window--resize-apply-p frame horizontal)
3083 (if (window-resize-apply frame horizontal)
3084 (progn
3085 (window--pixel-to-total frame horizontal)
3086 (run-window-configuration-change-hook frame))
3087 ;; But do report an error if applying the changes fails.
3088 (error "Failed adjusting window %s" window))))))))
562dd5e9
MR
3089
3090(defun enlarge-window (delta &optional horizontal)
2116e93c 3091 "Make the selected window DELTA lines taller.
562dd5e9
MR
3092Interactively, if no argument is given, make the selected window
3093one line taller. If optional argument HORIZONTAL is non-nil,
3094make selected window wider by DELTA columns. If DELTA is
0ff7851c 3095negative, shrink selected window by -DELTA lines or columns."
562dd5e9 3096 (interactive "p")
feeb6f53
MR
3097 (let ((minibuffer-window (minibuffer-window)))
3098 (cond
3099 ((zerop delta))
3100 ((window-size-fixed-p nil horizontal)
3101 (error "Selected window has fixed size"))
3102 ((window-minibuffer-p)
3103 (if horizontal
3104 (error "Cannot resize minibuffer window horizontally")
3105 (window--resize-mini-window (selected-window) delta)))
3106 ((and (not horizontal)
3107 (window-full-height-p)
3108 (eq (window-frame minibuffer-window) (selected-frame))
3109 (not resize-mini-windows))
3110 ;; If the selected window is full height and `resize-mini-windows'
3111 ;; is nil, resize the minibuffer window.
3112 (window--resize-mini-window minibuffer-window (- delta)))
880e6158 3113 ((window--resizable-p nil delta horizontal)
feeb6f53
MR
3114 (window-resize nil delta horizontal))
3115 (t
3116 (window-resize
3117 nil (if (> delta 0)
3118 (window-max-delta nil horizontal)
3119 (- (window-min-delta nil horizontal)))
3120 horizontal)))))
562dd5e9
MR
3121
3122(defun shrink-window (delta &optional horizontal)
2116e93c 3123 "Make the selected window DELTA lines smaller.
562dd5e9
MR
3124Interactively, if no argument is given, make the selected window
3125one line smaller. If optional argument HORIZONTAL is non-nil,
3126make selected window narrower by DELTA columns. If DELTA is
3127negative, enlarge selected window by -DELTA lines or columns.
0ff7851c 3128Also see the `window-min-height' variable."
562dd5e9 3129 (interactive "p")
feeb6f53
MR
3130 (let ((minibuffer-window (minibuffer-window)))
3131 (cond
3132 ((zerop delta))
3133 ((window-size-fixed-p nil horizontal)
3134 (error "Selected window has fixed size"))
3135 ((window-minibuffer-p)
3136 (if horizontal
3137 (error "Cannot resize minibuffer window horizontally")
3138 (window--resize-mini-window (selected-window) (- delta))))
3139 ((and (not horizontal)
3140 (window-full-height-p)
3141 (eq (window-frame minibuffer-window) (selected-frame))
3142 (not resize-mini-windows))
3143 ;; If the selected window is full height and `resize-mini-windows'
3144 ;; is nil, resize the minibuffer window.
3145 (window--resize-mini-window minibuffer-window delta))
880e6158 3146 ((window--resizable-p nil (- delta) horizontal)
feeb6f53
MR
3147 (window-resize nil (- delta) horizontal))
3148 (t
3149 (window-resize
3150 nil (if (> delta 0)
3151 (- (window-min-delta nil horizontal))
3152 (window-max-delta nil horizontal))
3153 horizontal)))))
562dd5e9 3154
2d6b6005 3155(defun maximize-window (&optional window)
562dd5e9
MR
3156 "Maximize WINDOW.
3157Make WINDOW as large as possible without deleting any windows.
880e6158
MR
3158WINDOW must be a valid window and defaults to the selected one.
3159
3160If the option `window-resize-pixelwise' is non-nil maximize
3161WINDOW pixelwise."
562dd5e9 3162 (interactive)
447f16b8 3163 (setq window (window-normalize-window window))
880e6158
MR
3164 (window-resize
3165 window (window-max-delta window nil nil nil nil nil window-resize-pixelwise)
3166 nil nil window-resize-pixelwise)
3167 (window-resize
3168 window (window-max-delta window t nil nil nil nil window-resize-pixelwise)
3169 t nil window-resize-pixelwise))
3170
2d6b6005 3171(defun minimize-window (&optional window)
562dd5e9
MR
3172 "Minimize WINDOW.
3173Make WINDOW as small as possible without deleting any windows.
880e6158
MR
3174WINDOW must be a valid window and defaults to the selected one.
3175
3176If the option `window-resize-pixelwise' is non-nil minimize
3177WINDOW pixelwise."
562dd5e9 3178 (interactive)
447f16b8 3179 (setq window (window-normalize-window window))
880e6158
MR
3180 (window-resize
3181 window
3182 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise))
3183 nil nil window-resize-pixelwise)
3184 (window-resize
3185 window
3186 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise))
3187 t nil window-resize-pixelwise))
562dd5e9 3188\f
4b0d61e3 3189(defun frame-root-window-p (window)
9aab8e0d
MR
3190 "Return non-nil if WINDOW is the root window of its frame."
3191 (eq window (frame-root-window window)))
6198ccd0 3192
5e92ca23
MR
3193(defun window--subtree (window &optional next)
3194 "Return window subtree rooted at WINDOW.
3195Optional argument NEXT non-nil means include WINDOW's right
6198ccd0
MR
3196siblings in the return value.
3197
3198See the documentation of `window-tree' for a description of the
3199return value."
3200 (let (list)
3201 (while window
3202 (setq list
3203 (cons
3204 (cond
d68443dc 3205 ((window-top-child window)
6198ccd0 3206 (cons t (cons (window-edges window)
5e92ca23 3207 (window--subtree (window-top-child window) t))))
d68443dc 3208 ((window-left-child window)
6198ccd0 3209 (cons nil (cons (window-edges window)
5e92ca23 3210 (window--subtree (window-left-child window) t))))
6198ccd0
MR
3211 (t window))
3212 list))
d68443dc 3213 (setq window (when next (window-next-sibling window))))
6198ccd0
MR
3214 (nreverse list)))
3215
3216(defun window-tree (&optional frame)
3217 "Return the window tree of frame FRAME.
3218FRAME must be a live frame and defaults to the selected frame.
3219The return value is a list of the form (ROOT MINI), where ROOT
3220represents the window tree of the frame's root window, and MINI
3221is the frame's minibuffer window.
3222
3223If the root window is not split, ROOT is the root window itself.
3224Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3225for a horizontal split, and t for a vertical split. EDGES gives
be7f5545
MR
3226the combined size and position of the child windows in the split,
3227and the rest of the elements are the child windows in the split.
3228Each of the child windows may again be a window or a list
382c953b 3229representing a window split, and so on. EDGES is a list (LEFT
6198ccd0 3230TOP RIGHT BOTTOM) as returned by `window-edges'."
5386012d 3231 (setq frame (window-normalize-frame frame))
5e92ca23 3232 (window--subtree (frame-root-window frame) t))
9aab8e0d 3233\f
9397e56f
MR
3234(defun other-window (count &optional all-frames)
3235 "Select another window in cyclic ordering of windows.
3236COUNT specifies the number of windows to skip, starting with the
3237selected window, before making the selection. If COUNT is
3238positive, skip COUNT windows forwards. If COUNT is negative,
3239skip -COUNT windows backwards. COUNT zero means do not skip any
3240window, so select the selected window. In an interactive call,
3241COUNT is the numeric prefix argument. Return nil.
3242
9a9e9ef0
MR
3243If the `other-window' parameter of the selected window is a
3244function and `ignore-window-parameters' is nil, call that
3245function with the arguments COUNT and ALL-FRAMES.
9397e56f
MR
3246
3247This function does not select a window whose `no-other-window'
3248window parameter is non-nil.
3249
3250This function uses `next-window' for finding the window to
3251select. The argument ALL-FRAMES has the same meaning as in
3252`next-window', but the MINIBUF argument of `next-window' is
3253always effectively nil."
3254 (interactive "p")
3255 (let* ((window (selected-window))
3256 (function (and (not ignore-window-parameters)
3257 (window-parameter window 'other-window)))
3258 old-window old-count)
3259 (if (functionp function)
3260 (funcall function count all-frames)
3261 ;; `next-window' and `previous-window' may return a window we are
3262 ;; not allowed to select. Hence we need an exit strategy in case
3263 ;; all windows are non-selectable.
3264 (catch 'exit
3265 (while (> count 0)
3266 (setq window (next-window window nil all-frames))
3267 (cond
3268 ((eq window old-window)
3269 (when (= count old-count)
3270 ;; Keep out of infinite loops. When COUNT has not changed
3271 ;; since we last looked at `window' we're probably in one.
3272 (throw 'exit nil)))
3273 ((window-parameter window 'no-other-window)
3274 (unless old-window
3275 ;; The first non-selectable window `next-window' got us:
3276 ;; Remember it and the current value of COUNT.
3277 (setq old-window window)
3278 (setq old-count count)))
3279 (t
3280 (setq count (1- count)))))
3281 (while (< count 0)
3282 (setq window (previous-window window nil all-frames))
3283 (cond
3284 ((eq window old-window)
3285 (when (= count old-count)
3286 ;; Keep out of infinite loops. When COUNT has not changed
3287 ;; since we last looked at `window' we're probably in one.
3288 (throw 'exit nil)))
3289 ((window-parameter window 'no-other-window)
3290 (unless old-window
3291 ;; The first non-selectable window `previous-window' got
3292 ;; us: Remember it and the current value of COUNT.
3293 (setq old-window window)
3294 (setq old-count count)))
3295 (t
3296 (setq count (1+ count)))))
3297
3298 (select-window window)
3299 ;; Always return nil.
3300 nil))))
3301
387522b2
MR
3302;; This should probably return non-nil when the selected window is part
3303;; of an atomic window whose root is the frame's root window.
3304(defun one-window-p (&optional nomini all-frames)
3305 "Return non-nil if the selected window is the only window.
3306Optional arg NOMINI non-nil means don't count the minibuffer
3307even if it is active. Otherwise, the minibuffer is counted
3308when it is active.
3309
3310Optional argument ALL-FRAMES specifies the set of frames to
3311consider, see also `next-window'. ALL-FRAMES nil or omitted
3312means consider windows on the selected frame only, plus the
3313minibuffer window if specified by the NOMINI argument. If the
3314minibuffer counts, consider all windows on all frames that share
3315that minibuffer too. The remaining non-nil values of ALL-FRAMES
3316with a special meaning are:
3317
3318- t means consider all windows on all existing frames.
3319
3320- `visible' means consider all windows on all visible frames on
3321 the current terminal.
3322
3323- 0 (the number zero) means consider all windows on all visible
3324 and iconified frames on the current terminal.
3325
3326- A frame means consider all windows on that frame only.
3327
3328Anything else means consider all windows on the selected frame
3329and no others."
3330 (let ((base-window (selected-window)))
3331 (if (and nomini (eq base-window (minibuffer-window)))
3332 (setq base-window (next-window base-window)))
3333 (eq base-window
3334 (next-window base-window (if nomini 'arg) all-frames))))
3c448ab6 3335\f
9aab8e0d 3336;;; Deleting windows.
1b36ed6a 3337(defun window-deletable-p (&optional window)
9aab8e0d 3338 "Return t if WINDOW can be safely deleted from its frame.
85c2386b 3339WINDOW must be a valid window and defaults to the selected one.
c660a885 3340Return 'frame if deleting WINDOW should also delete its frame."
447f16b8 3341 (setq window (window-normalize-window window))
8b0874b5 3342
c660a885 3343 (unless (or ignore-window-parameters
f224e500 3344 (eq (window-parameter window 'delete-window) t))
9aab8e0d
MR
3345 ;; Handle atomicity.
3346 (when (window-parameter window 'window-atom)
3347 (setq window (window-atom-root window))))
8b0874b5 3348
caceae25 3349 (let ((frame (window-frame window)))
9aab8e0d
MR
3350 (cond
3351 ((frame-root-window-p window)
85e9c04b 3352 ;; WINDOW's frame can be deleted only if there are other frames
46b7967e
TN
3353 ;; on the same terminal, and it does not contain the active
3354 ;; minibuffer.
3355 (unless (or (eq frame (next-frame frame 0))
c660a885
MR
3356 ;; We can delete our frame only if no other frame
3357 ;; currently uses our minibuffer window.
3358 (catch 'other
3359 (dolist (other (frame-list))
3360 (when (and (not (eq other frame))
3361 (eq (window-frame (minibuffer-window other))
3362 frame))
3363 (throw 'other t))))
46b7967e
TN
3364 (let ((minibuf (active-minibuffer-window)))
3365 (and minibuf (eq frame (window-frame minibuf)))))
85e9c04b 3366 'frame))
1b36ed6a 3367 ((or ignore-window-parameters
caceae25
MR
3368 (not (eq window (window--major-non-side-window frame))))
3369 ;; WINDOW can be deleted unless it is the major non-side window of
3370 ;; its frame.
1f3c99ca 3371 t))))
9aab8e0d 3372
be7f5545
MR
3373(defun window--in-subtree-p (window root)
3374 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
3375 (or (eq window root)
3376 (let ((parent (window-parent window)))
9aab8e0d
MR
3377 (catch 'done
3378 (while parent
be7f5545 3379 (if (eq parent root)
9aab8e0d
MR
3380 (throw 'done t)
3381 (setq parent (window-parent parent))))))))
3382
562dd5e9
MR
3383(defun delete-window (&optional window)
3384 "Delete WINDOW.
85c2386b
MR
3385WINDOW must be a valid window and defaults to the selected one.
3386Return nil.
562dd5e9
MR
3387
3388If the variable `ignore-window-parameters' is non-nil or the
3389`delete-window' parameter of WINDOW equals t, do not process any
3390parameters of WINDOW. Otherwise, if the `delete-window'
3391parameter of WINDOW specifies a function, call that function with
3392WINDOW as its sole argument and return the value returned by that
3393function.
3394
3395Otherwise, if WINDOW is part of an atomic window, call
3396`delete-window' with the root of the atomic window as its
85c2386b
MR
3397argument. Signal an error if WINDOW is either the only window on
3398its frame, the last non-side window, or part of an atomic window
3399that is its frame's root window."
562dd5e9 3400 (interactive)
447f16b8 3401 (setq window (window-normalize-window window))
562dd5e9
MR
3402 (let* ((frame (window-frame window))
3403 (function (window-parameter window 'delete-window))
3404 (parent (window-parent window))
3405 atom-root)
54f9154c 3406 (window--check frame)
562dd5e9
MR
3407 (catch 'done
3408 ;; Handle window parameters.
3409 (cond
3410 ;; Ignore window parameters if `ignore-window-parameters' tells
3411 ;; us so or `delete-window' equals t.
3412 ((or ignore-window-parameters (eq function t)))
3413 ((functionp function)
3414 ;; The `delete-window' parameter specifies the function to call.
3415 ;; If that function is `ignore' nothing is done. It's up to the
3416 ;; function called here to avoid infinite recursion.
3417 (throw 'done (funcall function window)))
3418 ((and (window-parameter window 'window-atom)
3419 (setq atom-root (window-atom-root window))
3420 (not (eq atom-root window)))
caceae25
MR
3421 (if (eq atom-root (frame-root-window frame))
3422 (error "Root of atomic window is root window of its frame")
3423 (throw 'done (delete-window atom-root))))
562dd5e9 3424 ((not parent)
caceae25
MR
3425 (error "Attempt to delete minibuffer or sole ordinary window"))
3426 ((eq window (window--major-non-side-window frame))
3427 (error "Attempt to delete last non-side window")))
562dd5e9 3428
d68443dc 3429 (let* ((horizontal (window-left-child parent))
880e6158 3430 (size (window-size window horizontal t))
562dd5e9 3431 (frame-selected
be7f5545 3432 (window--in-subtree-p (frame-selected-window frame) window))
562dd5e9
MR
3433 ;; Emacs 23 preferably gives WINDOW's space to its left
3434 ;; sibling.
3435 (sibling (or (window-left window) (window-right window))))
5386012d 3436 (window--resize-reset frame horizontal)
562dd5e9 3437 (cond
a0c2d0ae 3438 ((and (not window-combination-resize)
880e6158 3439 sibling (window-sizable-p sibling size horizontal nil t))
562dd5e9 3440 ;; Resize WINDOW's sibling.
5386012d 3441 (window--resize-this-window sibling size horizontal nil t)
562dd5e9
MR
3442 (set-window-new-normal
3443 sibling (+ (window-normal-size sibling horizontal)
3444 (window-normal-size window horizontal))))
880e6158 3445 ((window--resizable-p window (- size) horizontal nil nil nil t t)
562dd5e9 3446 ;; Can do without resizing fixed-size windows.
5386012d 3447 (window--resize-siblings window (- size) horizontal))
562dd5e9
MR
3448 (t
3449 ;; Can't do without resizing fixed-size windows.
5386012d 3450 (window--resize-siblings window (- size) horizontal t)))
562dd5e9
MR
3451 ;; Actually delete WINDOW.
3452 (delete-window-internal window)
880e6158 3453 (window--pixel-to-total frame horizontal)
562dd5e9
MR
3454 (when (and frame-selected
3455 (window-parameter
3456 (frame-selected-window frame) 'no-other-window))
3457 ;; `delete-window-internal' has selected a window that should
3458 ;; not be selected, fix this here.
3459 (other-window -1 frame))
3460 (run-window-configuration-change-hook frame)
54f9154c 3461 (window--check frame)
562dd5e9
MR
3462 ;; Always return nil.
3463 nil))))
3464
3465(defun delete-other-windows (&optional window)
3466 "Make WINDOW fill its frame.
85c2386b 3467WINDOW must be a valid window and defaults to the selected one.
562dd5e9
MR
3468Return nil.
3469
3470If the variable `ignore-window-parameters' is non-nil or the
3471`delete-other-windows' parameter of WINDOW equals t, do not
3472process any parameters of WINDOW. Otherwise, if the
3473`delete-other-windows' parameter of WINDOW specifies a function,
3474call that function with WINDOW as its sole argument and return
3475the value returned by that function.
3476
3477Otherwise, if WINDOW is part of an atomic window, call this
3478function with the root of the atomic window as its argument. If
3479WINDOW is a non-side window, make WINDOW the only non-side window
382c953b 3480on the frame. Side windows are not deleted. If WINDOW is a side
562dd5e9
MR
3481window signal an error."
3482 (interactive)
447f16b8 3483 (setq window (window-normalize-window window))
562dd5e9
MR
3484 (let* ((frame (window-frame window))
3485 (function (window-parameter window 'delete-other-windows))
3486 (window-side (window-parameter window 'window-side))
3487 atom-root side-main)
54f9154c 3488 (window--check frame)
562dd5e9
MR
3489 (catch 'done
3490 (cond
3491 ;; Ignore window parameters if `ignore-window-parameters' is t or
3492 ;; `delete-other-windows' is t.
3493 ((or ignore-window-parameters (eq function t)))
3494 ((functionp function)
3495 ;; The `delete-other-windows' parameter specifies the function
3496 ;; to call. If the function is `ignore' no windows are deleted.
3497 ;; It's up to the function called to avoid infinite recursion.
3498 (throw 'done (funcall function window)))
3499 ((and (window-parameter window 'window-atom)
3500 (setq atom-root (window-atom-root window))
3501 (not (eq atom-root window)))
caceae25
MR
3502 (if (eq atom-root (frame-root-window frame))
3503 (error "Root of atomic window is root window of its frame")
3504 (throw 'done (delete-other-windows atom-root))))
562dd5e9 3505 ((memq window-side window-sides)
caceae25
MR
3506 (error "Cannot make side window the only window"))
3507 ((and (window-minibuffer-p window)
3508 (not (eq window (frame-root-window window))))
3509 (error "Can't expand minibuffer to full frame")))
3510
3511 ;; If WINDOW is the major non-side window, do nothing.
3512 (if (window-with-parameter 'window-side)
3513 (setq side-main (window--major-non-side-window frame))
3514 (setq side-main (frame-root-window frame)))
562dd5e9
MR
3515 (unless (eq window side-main)
3516 (delete-other-windows-internal window side-main)
3517 (run-window-configuration-change-hook frame)
54f9154c 3518 (window--check frame))
562dd5e9
MR
3519 ;; Always return nil.
3520 nil)))
9397e56f
MR
3521
3522(defun delete-other-windows-vertically (&optional window)
3523 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
3524This may be a useful alternative binding for \\[delete-other-windows]
3525 if you often split windows horizontally."
3526 (interactive)
3527 (let* ((window (or window (selected-window)))
3528 (edges (window-edges window))
3529 (w window) delenda)
3530 (while (not (eq (setq w (next-window w 1)) window))
3531 (let ((e (window-edges w)))
3532 (when (and (= (car e) (car edges))
36cec983 3533 (= (nth 2 e) (nth 2 edges)))
9397e56f
MR
3534 (push w delenda))))
3535 (mapc 'delete-window delenda)))
3536
3537;;; Windows and buffers.
3538
3539;; `prev-buffers' and `next-buffers' are two reserved window slots used
3540;; for (1) determining which buffer to show in the window when its
3541;; buffer shall be buried or killed and (2) which buffer to show for
3542;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3543
3544;; `prev-buffers' consists of <buffer, window-start, window-point>
3545;; triples. The entries on this list are ordered by the time their
3546;; buffer has been removed from the window, the most recently removed
3547;; buffer's entry being first. The window-start and window-point
3548;; components are `window-start' and `window-point' at the time the
3549;; buffer was removed from the window which implies that the entry must
3550;; be added when `set-window-buffer' removes the buffer from the window.
3551
3552;; `next-buffers' is the list of buffers that have been replaced
3553;; recently by `switch-to-prev-buffer'. These buffers are the least
3554;; preferred candidates of `switch-to-prev-buffer' and the preferred
3555;; candidates of `switch-to-next-buffer' to switch to. This list is
3556;; reset to nil by any action changing the window's buffer with the
3557;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
3558;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
3559;; `switch-to-next-buffer' pops the last pushed buffer from it.
3560
3561;; Both `prev-buffers' and `next-buffers' may reference killed buffers
3562;; if such a buffer was killed while the window was hidden within a
3563;; window configuration. Such killed buffers get removed whenever
3564;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
3565
3566;; The following function is called by `set-window-buffer' _before_ it
3567;; replaces the buffer of the argument window with the new buffer.
3568(defun record-window-buffer (&optional window)
3569 "Record WINDOW's buffer.
3570WINDOW must be a live window and defaults to the selected one."
447f16b8 3571 (let* ((window (window-normalize-window window t))
9397e56f
MR
3572 (buffer (window-buffer window))
3573 (entry (assq buffer (window-prev-buffers window))))
3574 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
3575 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3576 (set-window-next-buffers window nil)
3577
3578 (when entry
3579 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
3580 (set-window-prev-buffers
3581 window (assq-delete-all buffer (window-prev-buffers window))))
3582
3583 ;; Don't record insignificant buffers.
3584 (unless (eq (aref (buffer-name buffer) 0) ?\s)
3585 ;; Add an entry for buffer to WINDOW's previous buffers.
3586 (with-current-buffer buffer
3587 (let ((start (window-start window))
5481664a 3588 (point (window-point window)))
9397e56f
MR
3589 (setq entry
3590 (cons buffer
3591 (if entry
3592 ;; We have an entry, update marker positions.
3593 (list (set-marker (nth 1 entry) start)
3594 (set-marker (nth 2 entry) point))
3595 ;; Make new markers.
3596 (list (copy-marker start)
92346275
MR
3597 (copy-marker
3598 ;; Preserve window-point-insertion-type
3599 ;; (Bug#12588).
3600 point window-point-insertion-type)))))
9397e56f 3601 (set-window-prev-buffers
c660a885
MR
3602 window (cons entry (window-prev-buffers window)))))
3603
3604 (run-hooks 'buffer-list-update-hook))))
9397e56f
MR
3605
3606(defun unrecord-window-buffer (&optional window buffer)
3607 "Unrecord BUFFER in WINDOW.
3608WINDOW must be a live window and defaults to the selected one.
3609BUFFER must be a live buffer and defaults to the buffer of
3610WINDOW."
447f16b8 3611 (let* ((window (window-normalize-window window t))
9397e56f
MR
3612 (buffer (or buffer (window-buffer window))))
3613 (set-window-prev-buffers
3614 window (assq-delete-all buffer (window-prev-buffers window)))
3615 (set-window-next-buffers
3616 window (delq buffer (window-next-buffers window)))))
3617
3618(defun set-window-buffer-start-and-point (window buffer &optional start point)
3619 "Set WINDOW's buffer to BUFFER.
85c2386b 3620WINDOW must be a live window and defaults to the selected one.
9397e56f
MR
3621Optional argument START non-nil means set WINDOW's start position
3622to START. Optional argument POINT non-nil means set WINDOW's
3623point to POINT. If WINDOW is selected this also sets BUFFER's
3624`point' to POINT. If WINDOW is selected and the buffer it showed
3625before was current this also makes BUFFER the current buffer."
85c2386b 3626 (setq window (window-normalize-window window t))
9397e56f
MR
3627 (let ((selected (eq window (selected-window)))
3628 (current (eq (window-buffer window) (current-buffer))))
3629 (set-window-buffer window buffer)
3630 (when (and selected current)
3631 (set-buffer buffer))
3632 (when start
cf4eacfd
MR
3633 ;; Don't force window-start here (even if POINT is nil).
3634 (set-window-start window start t))
9397e56f 3635 (when point
5481664a 3636 (set-window-point window point))))
9397e56f 3637
dcb6e7b3
MR
3638(defcustom switch-to-visible-buffer t
3639 "If non-nil, allow switching to an already visible buffer.
3640If this variable is non-nil, `switch-to-prev-buffer' and
3641`switch-to-next-buffer' may switch to an already visible buffer
43bcfda6
MR
3642provided the buffer was shown before in the window specified as
3643argument to those functions. If this variable is nil,
3644`switch-to-prev-buffer' and `switch-to-next-buffer' always try to
3645avoid switching to a buffer that is already visible in another
3646window on the same frame."
dcb6e7b3
MR
3647 :type 'boolean
3648 :version "24.1"
3649 :group 'windows)
3650
9397e56f
MR
3651(defun switch-to-prev-buffer (&optional window bury-or-kill)
3652 "In WINDOW switch to previous buffer.
3653WINDOW must be a live window and defaults to the selected one.
502e3f89
MR
3654Return the buffer switched to, nil if no suitable buffer could be
3655found.
9397e56f
MR
3656
3657Optional argument BURY-OR-KILL non-nil means the buffer currently
3658shown in WINDOW is about to be buried or killed and consequently
78dd6ab1
MR
3659shall not be switched to in future invocations of this command.
3660
3661As a special case, if BURY-OR-KILL equals `append', this means to
3662move the buffer to the end of WINDOW's previous buffers list so a
3663future invocation of `switch-to-prev-buffer' less likely switches
3664to it."
9397e56f 3665 (interactive)
447f16b8 3666 (let* ((window (window-normalize-window window t))
dcb6e7b3 3667 (frame (window-frame window))
9397e56f
MR
3668 (old-buffer (window-buffer window))
3669 ;; Save this since it's destroyed by `set-window-buffer'.
3670 (next-buffers (window-next-buffers window))
862382df 3671 (pred (frame-parameter frame 'buffer-predicate))
78dd6ab1
MR
3672 entry new-buffer killed-buffers visible)
3673 (when (window-minibuffer-p window)
3674 ;; Don't switch in minibuffer window.
3675 (unless (setq window (minibuffer-selected-window))
3676 (error "Window %s is a minibuffer window" window)))
3677
1b36ed6a 3678 (when (window-dedicated-p window)
78dd6ab1 3679 ;; Don't switch in dedicated window.
1b36ed6a
MR
3680 (error "Window %s is dedicated to buffer %s" window old-buffer))
3681
3682 (catch 'found
3683 ;; Scan WINDOW's previous buffers first, skipping entries of next
3684 ;; buffers.
3685 (dolist (entry (window-prev-buffers window))
78dd6ab1
MR
3686 (when (and (setq new-buffer (car entry))
3687 (or (buffer-live-p new-buffer)
1b36ed6a 3688 (not (setq killed-buffers
78dd6ab1
MR
3689 (cons new-buffer killed-buffers))))
3690 (not (eq new-buffer old-buffer))
3691 (or (null pred) (funcall pred new-buffer))
3692 ;; When BURY-OR-KILL is nil, avoid switching to a
3693 ;; buffer in WINDOW's next buffers list.
3694 (or bury-or-kill (not (memq new-buffer next-buffers))))
dcb6e7b3 3695 (if (and (not switch-to-visible-buffer)
78dd6ab1
MR
3696 (get-buffer-window new-buffer frame))
3697 ;; Try to avoid showing a buffer visible in some other
3698 ;; window.
3699 (setq visible new-buffer)
502e3f89
MR
3700 (set-window-buffer-start-and-point
3701 window new-buffer (nth 1 entry) (nth 2 entry))
3702 (throw 'found t))))
1b36ed6a
MR
3703 ;; Scan reverted buffer list of WINDOW's frame next, skipping
3704 ;; entries of next buffers. Note that when we bury or kill a
3705 ;; buffer we don't reverse the global buffer list to avoid showing
3706 ;; a buried buffer instead. Otherwise, we must reverse the global
3707 ;; buffer list in order to make sure that switching to the
3708 ;; previous/next buffer traverse it in opposite directions.
3709 (dolist (buffer (if bury-or-kill
dcb6e7b3
MR
3710 (buffer-list frame)
3711 (nreverse (buffer-list frame))))
1b36ed6a
MR
3712 (when (and (buffer-live-p buffer)
3713 (not (eq buffer old-buffer))
862382df 3714 (or (null pred) (funcall pred buffer))
78dd6ab1 3715 (not (eq (aref (buffer-name buffer) 0) ?\s))
1b36ed6a 3716 (or bury-or-kill (not (memq buffer next-buffers))))
dcb6e7b3 3717 (if (get-buffer-window buffer frame)
1b36ed6a 3718 ;; Try to avoid showing a buffer visible in some other window.
dcb6e7b3
MR
3719 (unless visible
3720 (setq visible buffer))
1b36ed6a
MR
3721 (setq new-buffer buffer)
3722 (set-window-buffer-start-and-point window new-buffer)
3723 (throw 'found t))))
3724 (unless bury-or-kill
3725 ;; Scan reverted next buffers last (must not use nreverse
3726 ;; here!).
3727 (dolist (buffer (reverse next-buffers))
3728 ;; Actually, buffer _must_ be live here since otherwise it
3729 ;; would have been caught in the scan of previous buffers.
3730 (when (and (or (buffer-live-p buffer)
9397e56f 3731 (not (setq killed-buffers
1b36ed6a 3732 (cons buffer killed-buffers))))
9397e56f 3733 (not (eq buffer old-buffer))
862382df 3734 (or (null pred) (funcall pred buffer))
1b36ed6a 3735 (setq entry (assq buffer (window-prev-buffers window))))
9397e56f 3736 (setq new-buffer buffer)
1b36ed6a
MR
3737 (set-window-buffer-start-and-point
3738 window new-buffer (nth 1 entry) (nth 2 entry))
9397e56f 3739 (throw 'found t))))
1b36ed6a
MR
3740
3741 ;; Show a buffer visible in another window.
3742 (when visible
3743 (setq new-buffer visible)
3744 (set-window-buffer-start-and-point window new-buffer)))
3745
3746 (if bury-or-kill
78dd6ab1
MR
3747 (let ((entry (and (eq bury-or-kill 'append)
3748 (assq old-buffer (window-prev-buffers window)))))
3749 ;; Remove `old-buffer' from WINDOW's previous and (restored list
3750 ;; of) next buffers.
1b36ed6a
MR
3751 (set-window-prev-buffers
3752 window (assq-delete-all old-buffer (window-prev-buffers window)))
78dd6ab1
MR
3753 (set-window-next-buffers window (delq old-buffer next-buffers))
3754 (when entry
3755 ;; Append old-buffer's entry to list of WINDOW's previous
3756 ;; buffers so it's less likely to get switched to soon but
3757 ;; `display-buffer-in-previous-window' can nevertheless find
3758 ;; it.
3759 (set-window-prev-buffers
3760 window (append (window-prev-buffers window) (list entry)))))
1b36ed6a
MR
3761 ;; Move `old-buffer' to head of WINDOW's restored list of next
3762 ;; buffers.
3763 (set-window-next-buffers
3764 window (cons old-buffer (delq old-buffer next-buffers))))
9397e56f
MR
3765
3766 ;; Remove killed buffers from WINDOW's previous and next buffers.
3767 (when killed-buffers
3768 (dolist (buffer killed-buffers)
3769 (set-window-prev-buffers
3770 window (assq-delete-all buffer (window-prev-buffers window)))
3771 (set-window-next-buffers
3772 window (delq buffer (window-next-buffers window)))))
3773
3774 ;; Return new-buffer.
3775 new-buffer))
3776
3777(defun switch-to-next-buffer (&optional window)
3778 "In WINDOW switch to next buffer.
502e3f89
MR
3779WINDOW must be a live window and defaults to the selected one.
3780Return the buffer switched to, nil if no suitable buffer could be
3781found."
9397e56f 3782 (interactive)
447f16b8 3783 (let* ((window (window-normalize-window window t))
dcb6e7b3 3784 (frame (window-frame window))
9397e56f
MR
3785 (old-buffer (window-buffer window))
3786 (next-buffers (window-next-buffers window))
862382df 3787 (pred (frame-parameter frame 'buffer-predicate))
78dd6ab1
MR
3788 new-buffer entry killed-buffers visible)
3789 (when (window-minibuffer-p window)
3790 ;; Don't switch in minibuffer window.
3791 (unless (setq window (minibuffer-selected-window))
3792 (error "Window %s is a minibuffer window" window)))
3793
9397e56f 3794 (when (window-dedicated-p window)
78dd6ab1 3795 ;; Don't switch in dedicated window.
9397e56f
MR
3796 (error "Window %s is dedicated to buffer %s" window old-buffer))
3797
3798 (catch 'found
3799 ;; Scan WINDOW's next buffers first.
3800 (dolist (buffer next-buffers)
3801 (when (and (or (buffer-live-p buffer)
3802 (not (setq killed-buffers
3803 (cons buffer killed-buffers))))
3804 (not (eq buffer old-buffer))
862382df 3805 (or (null pred) (funcall pred buffer))
9397e56f
MR
3806 (setq entry (assq buffer (window-prev-buffers window))))
3807 (setq new-buffer buffer)
3808 (set-window-buffer-start-and-point
3809 window new-buffer (nth 1 entry) (nth 2 entry))
3810 (throw 'found t)))
3811 ;; Scan the buffer list of WINDOW's frame next, skipping previous
3812 ;; buffers entries.
dcb6e7b3 3813 (dolist (buffer (buffer-list frame))
78dd6ab1
MR
3814 (when (and (buffer-live-p buffer)
3815 (not (eq buffer old-buffer))
862382df 3816 (or (null pred) (funcall pred buffer))
78dd6ab1 3817 (not (eq (aref (buffer-name buffer) 0) ?\s))
9397e56f 3818 (not (assq buffer (window-prev-buffers window))))
dcb6e7b3 3819 (if (get-buffer-window buffer frame)
9397e56f
MR
3820 ;; Try to avoid showing a buffer visible in some other window.
3821 (setq visible buffer)
3822 (setq new-buffer buffer)
3823 (set-window-buffer-start-and-point window new-buffer)
3824 (throw 'found t))))
3825 ;; Scan WINDOW's reverted previous buffers last (must not use
3826 ;; nreverse here!)
3827 (dolist (entry (reverse (window-prev-buffers window)))
78dd6ab1
MR
3828 (when (and (setq new-buffer (car entry))
3829 (or (buffer-live-p new-buffer)
9397e56f 3830 (not (setq killed-buffers
78dd6ab1
MR
3831 (cons new-buffer killed-buffers))))
3832 (not (eq new-buffer old-buffer))
3833 (or (null pred) (funcall pred new-buffer)))
dcb6e7b3 3834 (if (and (not switch-to-visible-buffer)
78dd6ab1 3835 (get-buffer-window new-buffer frame))
dcb6e7b3
MR
3836 ;; Try to avoid showing a buffer visible in some other window.
3837 (unless visible
78dd6ab1 3838 (setq visible new-buffer))
dcb6e7b3
MR
3839 (set-window-buffer-start-and-point
3840 window new-buffer (nth 1 entry) (nth 2 entry))
3841 (throw 'found t))))
9397e56f
MR
3842
3843 ;; Show a buffer visible in another window.
3844 (when visible
3845 (setq new-buffer visible)
3846 (set-window-buffer-start-and-point window new-buffer)))
3847
3848 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
3849 (set-window-next-buffers window (delq new-buffer next-buffers))
3850
3851 ;; Remove killed buffers from WINDOW's previous and next buffers.
3852 (when killed-buffers
3853 (dolist (buffer killed-buffers)
3854 (set-window-prev-buffers
3855 window (assq-delete-all buffer (window-prev-buffers window)))
3856 (set-window-next-buffers
3857 window (delq buffer (window-next-buffers window)))))
3858
3859 ;; Return new-buffer.
3860 new-buffer))
3861
3862(defun get-next-valid-buffer (list &optional buffer visible-ok frame)
3863 "Search LIST for a valid buffer to display in FRAME.
3864Return nil when all buffers in LIST are undesirable for display,
3865otherwise return the first suitable buffer in LIST.
3866
3867Buffers not visible in windows are preferred to visible buffers,
3868unless VISIBLE-OK is non-nil.
3869If the optional argument FRAME is nil, it defaults to the selected frame.
3870If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
3871 ;; This logic is more or less copied from other-buffer.
3872 (setq frame (or frame (selected-frame)))
3873 (let ((pred (frame-parameter frame 'buffer-predicate))
3874 found buf)
3875 (while (and (not found) list)
3876 (setq buf (car list))
3877 (if (and (not (eq buffer buf))
3878 (buffer-live-p buf)
3879 (or (null pred) (funcall pred buf))
3880 (not (eq (aref (buffer-name buf) 0) ?\s))
3881 (or visible-ok (null (get-buffer-window buf 'visible))))
3882 (setq found buf)
3883 (setq list (cdr list))))
3884 (car list)))
3885
3886(defun last-buffer (&optional buffer visible-ok frame)
3887 "Return the last buffer in FRAME's buffer list.
3888If BUFFER is the last buffer, return the preceding buffer
3889instead. Buffers not visible in windows are preferred to visible
3890buffers, unless optional argument VISIBLE-OK is non-nil.
3891Optional third argument FRAME nil or omitted means use the
3892selected frame's buffer list. If no such buffer exists, return
3893the buffer `*scratch*', creating it if necessary."
3894 (setq frame (or frame (selected-frame)))
3895 (or (get-next-valid-buffer (nreverse (buffer-list frame))
3896 buffer visible-ok frame)
3897 (get-buffer "*scratch*")
3898 (let ((scratch (get-buffer-create "*scratch*")))
3899 (set-buffer-major-mode scratch)
3900 scratch)))
3901
5a4cf282
MR
3902(defcustom frame-auto-hide-function #'iconify-frame
3903 "Function called to automatically hide frames.
3904The function is called with one argument - a frame.
3905
3906Functions affected by this option are those that bury a buffer
3907shown in a separate frame like `quit-window' and `bury-buffer'."
3908 :type '(choice (const :tag "Iconify" iconify-frame)
3909 (const :tag "Delete" delete-frame)
3910 (const :tag "Do nothing" ignore)
3911 function)
3912 :group 'windows
49677495
MR
3913 :group 'frames
3914 :version "24.1")
0e2070b5
MR
3915
3916(defun window--delete (&optional window dedicated-only kill)
3917 "Delete WINDOW if possible.
3918WINDOW must be a live window and defaults to the selected one.
3919Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
3920only if it's dedicated to its buffer. Optional argument KILL
3921means the buffer shown in window will be killed. Return non-nil
c557cd6b 3922if WINDOW gets deleted or its frame is auto-hidden."
447f16b8 3923 (setq window (window-normalize-window window t))
0e2070b5 3924 (unless (and dedicated-only (not (window-dedicated-p window)))
cb882333 3925 (let ((deletable (window-deletable-p window)))
0e2070b5
MR
3926 (cond
3927 ((eq deletable 'frame)
3928 (let ((frame (window-frame window)))
c557cd6b
MR
3929 (cond
3930 (kill
3931 (delete-frame frame))
3932 ((functionp frame-auto-hide-function)
3933 (funcall frame-auto-hide-function frame))))
0e2070b5
MR
3934 'frame)
3935 (deletable
3936 (delete-window window)
3937 t)))))
3938
9397e56f
MR
3939(defun bury-buffer (&optional buffer-or-name)
3940 "Put BUFFER-OR-NAME at the end of the list of all buffers.
3941There it is the least likely candidate for `other-buffer' to
3942return; thus, the least likely buffer for \\[switch-to-buffer] to
3943select by default.
3944
3945You can specify a buffer name as BUFFER-OR-NAME, or an actual
3946buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
3947current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
3948remove the current buffer from the selected window if it is
3949displayed there."
3950 (interactive)
5386012d 3951 (let* ((buffer (window-normalize-buffer buffer-or-name)))
9397e56f
MR
3952 ;; If `buffer-or-name' is not on the selected frame we unrecord it
3953 ;; although it's not "here" (call it a feature).
e4ed06f1 3954 (bury-buffer-internal buffer)
9397e56f
MR
3955 ;; Handle case where `buffer-or-name' is nil and the current buffer
3956 ;; is shown in the selected window.
3957 (cond
3958 ((or buffer-or-name (not (eq buffer (window-buffer)))))
0e2070b5
MR
3959 ((window--delete nil t))
3960 (t
3961 ;; Switch to another buffer in window.
3962 (set-window-dedicated-p nil nil)
1885e5b8 3963 (switch-to-prev-buffer nil 'bury)))
1b36ed6a 3964
9397e56f
MR
3965 ;; Always return nil.
3966 nil))
3967
3968(defun unbury-buffer ()
3969 "Switch to the last buffer in the buffer list."
3970 (interactive)
3971 (switch-to-buffer (last-buffer)))
3972
3973(defun next-buffer ()
3974 "In selected window switch to next buffer."
3975 (interactive)
85c2386b
MR
3976 (cond
3977 ((window-minibuffer-p)
3978 (error "Cannot switch buffers in minibuffer window"))
3979 ((eq (window-dedicated-p) t)
3980 (error "Window is strongly dedicated to its buffer"))
3981 (t
3982 (switch-to-next-buffer))))
9397e56f
MR
3983
3984(defun previous-buffer ()
3985 "In selected window switch to previous buffer."
3986 (interactive)
85c2386b
MR
3987 (cond
3988 ((window-minibuffer-p)
3989 (error "Cannot switch buffers in minibuffer window"))
3990 ((eq (window-dedicated-p) t)
3991 (error "Window is strongly dedicated to its buffer"))
3992 (t
3993 (switch-to-prev-buffer))))
9397e56f
MR
3994
3995(defun delete-windows-on (&optional buffer-or-name frame)
3996 "Delete all windows showing BUFFER-OR-NAME.
3997BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3998and defaults to the current buffer.
3999
4000The following non-nil values of the optional argument FRAME
4001have special meanings:
4002
4003- t means consider all windows on the selected frame only.
4004
4005- `visible' means consider all windows on all visible frames on
4006 the current terminal.
4007
4008- 0 (the number zero) means consider all windows on all visible
4009 and iconified frames on the current terminal.
4010
4011- A frame means consider all windows on that frame only.
4012
4013Any other value of FRAME means consider all windows on all
4014frames.
4015
4016When a window showing BUFFER-OR-NAME is dedicated and the only
4017window of its frame, that frame is deleted when there are other
4018frames left."
4019 (interactive "BDelete windows on (buffer):\nP")
5386012d 4020 (let ((buffer (window-normalize-buffer buffer-or-name))
9397e56f
MR
4021 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4022 ;; `window-list-1' based function.
4023 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4024 (dolist (window (window-list-1 nil nil all-frames))
4025 (if (eq (window-buffer window) buffer)
1b36ed6a 4026 (let ((deletable (window-deletable-p window)))
9397e56f 4027 (cond
1b36ed6a
MR
4028 ((and (eq deletable 'frame) (window-dedicated-p window))
4029 ;; Delete frame if and only if window is dedicated.
9397e56f 4030 (delete-frame (window-frame window)))
1b36ed6a
MR
4031 ((eq deletable t)
4032 ;; Delete window.
9397e56f
MR
4033 (delete-window window))
4034 (t
4035 ;; In window switch to previous buffer.
4036 (set-window-dedicated-p window nil)
4037 (switch-to-prev-buffer window 'bury))))
4038 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4039 (unrecord-window-buffer window buffer)))))
4040
4041(defun replace-buffer-in-windows (&optional buffer-or-name)
4042 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
4043BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4044and defaults to the current buffer.
4045
0e2070b5
MR
4046When a window showing BUFFER-OR-NAME is dedicated, that window is
4047deleted. If that window is the only window on its frame, the
4048frame is deleted too when there are other frames left. If there
4049are no other frames left, some other buffer is displayed in that
4050window.
9397e56f
MR
4051
4052This function removes the buffer denoted by BUFFER-OR-NAME from
4053all window-local buffer lists."
24901d61 4054 (interactive "bBuffer to replace: ")
5386012d 4055 (let ((buffer (window-normalize-buffer buffer-or-name)))
9397e56f
MR
4056 (dolist (window (window-list-1 nil nil t))
4057 (if (eq (window-buffer window) buffer)
0e2070b5
MR
4058 (unless (window--delete window t t)
4059 ;; Switch to another buffer in window.
4060 (set-window-dedicated-p window nil)
4061 (switch-to-prev-buffer window 'kill))
9397e56f
MR
4062 ;; Unrecord BUFFER in WINDOW.
4063 (unrecord-window-buffer window buffer)))))
4064
78dd6ab1
MR
4065(defun quit-restore-window (&optional window bury-or-kill)
4066 "Quit WINDOW and deal with its buffer.
cf4eacfd 4067WINDOW must be a live window and defaults to the selected one.
9397e56f
MR
4068
4069According to information stored in WINDOW's `quit-restore' window
382c953b
JB
4070parameter either (1) delete WINDOW and its frame, (2) delete
4071WINDOW, (3) restore the buffer previously displayed in WINDOW,
4072or (4) make WINDOW display some other buffer than the present
78dd6ab1
MR
4073one. If non-nil, reset `quit-restore' parameter to nil.
4074
4075Optional second argument BURY-OR-KILL tells how to proceed with
4076the buffer of WINDOW. The following values are handled:
4077
4078`nil' means to not handle the buffer in a particular way. This
4079 means that if WINDOW is not deleted by this function, invoking
4080 `switch-to-prev-buffer' will usually show the buffer again.
4081
4082`append' means that if WINDOW is not deleted, move its buffer to
4083 the end of WINDOW's previous buffers so it's less likely that a
4084 future invocation of `switch-to-prev-buffer' will switch to it.
4085 Also, move the buffer to the end of the frame's buffer list.
4086
4087`bury' means that if WINDOW is not deleted, remove its buffer
4088 from WINDOW'S list of previous buffers. Also, move the buffer
4089 to the end of the frame's buffer list. This value provides the
4090 most reliable remedy to not have `switch-to-prev-buffer' switch
4091 to this buffer again without killing the buffer.
4092
4093`kill' means to kill WINDOW's buffer."
447f16b8 4094 (setq window (window-normalize-window window t))
cf4eacfd
MR
4095 (let* ((buffer (window-buffer window))
4096 (quit-restore (window-parameter window 'quit-restore))
4097 (prev-buffer
4098 (let* ((prev-buffers (window-prev-buffers window))
4099 (prev-buffer (caar prev-buffers)))
4100 (and (or (not (eq prev-buffer buffer))
4101 (and (cdr prev-buffers)
4102 (not (eq (setq prev-buffer (cadr prev-buffers))
4103 buffer))))
4104 prev-buffer)))
78dd6ab1 4105 quad entry)
9397e56f 4106 (cond
cf4eacfd 4107 ((and (not prev-buffer)
218e997a
MR
4108 (or (eq (nth 1 quit-restore) 'frame)
4109 (and (eq (nth 1 quit-restore) 'window)
4110 ;; If the window has been created on an existing
4111 ;; frame and ended up as the sole window on that
4112 ;; frame, do not delete it (Bug#12764).
4113 (not (eq window (frame-root-window window)))))
0e2070b5
MR
4114 (eq (nth 3 quit-restore) buffer)
4115 ;; Delete WINDOW if possible.
78dd6ab1 4116 (window--delete window nil (eq bury-or-kill 'kill)))
9397e56f
MR
4117 ;; If the previously selected window is still alive, select it.
4118 (when (window-live-p (nth 2 quit-restore))
4119 (select-window (nth 2 quit-restore))))
cf4eacfd
MR
4120 ((and (listp (setq quad (nth 1 quit-restore)))
4121 (buffer-live-p (car quad))
4122 (eq (nth 3 quit-restore) buffer))
4123 ;; Show another buffer stored in quit-restore parameter.
78dd6ab1 4124 (when (and (integerp (nth 3 quad))
880e6158 4125 (/= (nth 3 quad) (window-total-height window)))
cf4eacfd
MR
4126 ;; Try to resize WINDOW to its old height but don't signal an
4127 ;; error.
4128 (condition-case nil
880e6158 4129 (window-resize window (- (nth 3 quad) (window-total-height window)))
cf4eacfd 4130 (error nil)))
78dd6ab1 4131 (set-window-dedicated-p window nil)
1885e5b8 4132 ;; Restore WINDOW's previous buffer, start and point position.
cf4eacfd
MR
4133 (set-window-buffer-start-and-point
4134 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
78dd6ab1
MR
4135 ;; Deal with the buffer we just removed from WINDOW.
4136 (setq entry (and (eq bury-or-kill 'append)
4137 (assq buffer (window-prev-buffers window))))
4138 (when bury-or-kill
4139 ;; Remove buffer from WINDOW's previous and next buffers.
4140 (set-window-prev-buffers
4141 window (assq-delete-all buffer (window-prev-buffers window)))
4142 (set-window-next-buffers
4143 window (delq buffer (window-next-buffers window))))
4144 (when entry
4145 ;; Append old buffer's entry to list of WINDOW's previous
4146 ;; buffers so it's less likely to get switched to soon but
4147 ;; `display-buffer-in-previous-window' can nevertheless find it.
4148 (set-window-prev-buffers
4149 window (append (window-prev-buffers window) (list entry))))
9397e56f
MR
4150 ;; Reset the quit-restore parameter.
4151 (set-window-parameter window 'quit-restore nil)
cf4eacfd
MR
4152 ;; Select old window.
4153 (when (window-live-p (nth 2 quit-restore))
4154 (select-window (nth 2 quit-restore))))
9397e56f 4155 (t
cf4eacfd
MR
4156 ;; Show some other buffer in WINDOW and reset the quit-restore
4157 ;; parameter.
9397e56f 4158 (set-window-parameter window 'quit-restore nil)
b4d72fcf
MR
4159 ;; Make sure that WINDOW is no more dedicated.
4160 (set-window-dedicated-p window nil)
78dd6ab1
MR
4161 (switch-to-prev-buffer window bury-or-kill)))
4162
4163 ;; Deal with the buffer.
4164 (cond
4165 ((not (buffer-live-p buffer)))
4166 ((eq bury-or-kill 'kill)
4167 (kill-buffer buffer))
4168 (bury-or-kill
4169 (bury-buffer-internal buffer)))))
9397e56f 4170
78dd6ab1
MR
4171(defun quit-window (&optional kill window)
4172 "Quit WINDOW and bury its buffer.
4173WINDOW must be a live window and defaults to the selected one.
4174With prefix argument KILL non-nil, kill the buffer instead of
4175burying it.
4176
4177According to information stored in WINDOW's `quit-restore' window
4178parameter either (1) delete WINDOW and its frame, (2) delete
4179WINDOW, (3) restore the buffer previously displayed in WINDOW,
4180or (4) make WINDOW display some other buffer than the present
4181one. If non-nil, reset `quit-restore' parameter to nil."
4182 (interactive "P")
4183 (quit-restore-window window (if kill 'kill 'bury)))
366ca7f3
MR
4184
4185(defun quit-windows-on (&optional buffer-or-name kill frame)
4186 "Quit all windows showing BUFFER-OR-NAME.
4187BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4188and defaults to the current buffer. Optional argument KILL
4189non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4190BUFFER-OR-NAME. Optional argument FRAME is handled as by
4191`delete-windows-on'.
4192
4193This function calls `quit-window' on all candidate windows
4194showing BUFFER-OR-NAME."
4195 (interactive "BQuit windows on (buffer):\nP")
4196 (let ((buffer (window-normalize-buffer buffer-or-name))
4197 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4198 ;; `window-list-1' based function.
4199 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4200 (dolist (window (window-list-1 nil nil all-frames))
4201 (if (eq (window-buffer window) buffer)
4202 (quit-window kill window)
4203 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4204 (unrecord-window-buffer window buffer)))))
562dd5e9
MR
4205\f
4206;;; Splitting windows.
880e6158 4207(defun window-split-min-size (&optional horizontal pixelwise)
562dd5e9
MR
4208 "Return minimum height of any window when splitting windows.
4209Optional argument HORIZONTAL non-nil means return minimum width."
880e6158
MR
4210 (cond
4211 (pixelwise
4212 (if horizontal
4213 (window-min-pixel-width)
4214 (window-min-pixel-height)))
4215 (horizontal
4216 (max window-min-width window-safe-min-width))
4217 (t
4218 (max window-min-height window-safe-min-height))))
562dd5e9 4219
880e6158 4220(defun split-window (&optional window size side pixelwise)
562dd5e9 4221 "Make a new window adjacent to WINDOW.
85c2386b 4222WINDOW must be a valid window and defaults to the selected one.
562dd5e9
MR
4223Return the new window which is always a live window.
4224
4225Optional argument SIZE a positive number means make WINDOW SIZE
4226lines or columns tall. If SIZE is negative, make the new window
4227-SIZE lines or columns tall. If and only if SIZE is non-nil, its
4228absolute value can be less than `window-min-height' or
4229`window-min-width'; so this command can make a new window as
4230small as one line or two columns. SIZE defaults to half of
0aa3616e 4231WINDOW's size.
562dd5e9
MR
4232
4233Optional third argument SIDE nil (or `below') specifies that the
4234new window shall be located below WINDOW. SIDE `above' means the
4235new window shall be located above WINDOW. In both cases SIZE
382c953b 4236specifies the new number of lines for WINDOW (or the new window
562dd5e9
MR
4237if SIZE is negative) including space reserved for the mode and/or
4238header line.
4239
4240SIDE t (or `right') specifies that the new window shall be
4241located on the right side of WINDOW. SIDE `left' means the new
4242window shall be located on the left of WINDOW. In both cases
382c953b 4243SIZE specifies the new number of columns for WINDOW (or the new
562dd5e9
MR
4244window provided SIZE is negative) including space reserved for
4245fringes and the scrollbar or a divider column. Any other non-nil
4246value for SIDE is currently handled like t (or `right').
4247
f224e500 4248PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
880e6158 4249
562dd5e9
MR
4250If the variable `ignore-window-parameters' is non-nil or the
4251`split-window' parameter of WINDOW equals t, do not process any
4252parameters of WINDOW. Otherwise, if the `split-window' parameter
4253of WINDOW specifies a function, call that function with all three
4254arguments and return the value returned by that function.
4255
4256Otherwise, if WINDOW is part of an atomic window, \"split\" the
4257root of that atomic window. The new window does not become a
4258member of that atomic window.
4259
4260If WINDOW is live, properties of the new window like margins and
4261scrollbars are inherited from WINDOW. If WINDOW is an internal
4262window, these properties as well as the buffer displayed in the
4263new window are inherited from the window selected on WINDOW's
4264frame. The selected window is not changed by this function."
447f16b8 4265 (setq window (window-normalize-window window))
130e3e11
MR
4266 (let* ((side (cond
4267 ((not side) 'below)
4268 ((memq side '(below above right left)) side)
4269 (t 'right)))
caceae25 4270 (horizontal (not (memq side '(below above))))
562dd5e9
MR
4271 (frame (window-frame window))
4272 (parent (window-parent window))
4273 (function (window-parameter window 'split-window))
4274 (window-side (window-parameter window 'window-side))
8e17c9ba
MR
4275 ;; Rebind the following two variables since in some cases we
4276 ;; have to override their value.
b6f67890 4277 (window-combination-limit window-combination-limit)
caceae25 4278 (window-combination-resize window-combination-resize)
880e6158
MR
4279 (char-size (frame-char-size window horizontal))
4280 (pixel-size
4281 (when (numberp size)
4282 (window--size-to-pixel window size horizontal pixelwise t)))
562dd5e9 4283 atom-root)
54f9154c 4284 (window--check frame)
562dd5e9
MR
4285 (catch 'done
4286 (cond
4287 ;; Ignore window parameters if either `ignore-window-parameters'
4288 ;; is t or the `split-window' parameter equals t.
4289 ((or ignore-window-parameters (eq function t)))
4290 ((functionp function)
4291 ;; The `split-window' parameter specifies the function to call.
4292 ;; If that function is `ignore', do nothing.
4293 (throw 'done (funcall function window size side)))
be7f5545
MR
4294 ;; If WINDOW is part of an atomic window, split the root window
4295 ;; of that atomic window instead.
562dd5e9
MR
4296 ((and (window-parameter window 'window-atom)
4297 (setq atom-root (window-atom-root window))
4298 (not (eq atom-root window)))
880e6158 4299 (throw 'done (split-window atom-root size side pixelwise)))
caceae25
MR
4300 ;; If WINDOW is a side window or its first or last child is a
4301 ;; side window, throw an error unless `window-combination-resize'
4302 ;; equals 'side.
4303 ((and (not (eq window-combination-resize 'side))
4304 (or (window-parameter window 'window-side)
4305 (and (window-child window)
4306 (or (window-parameter
4307 (window-child window) 'window-side)
4308 (window-parameter
4309 (window-last-child window) 'window-side)))))
4310 (error "Cannot split side window or parent of side window"))
4311 ;; If `window-combination-resize' is 'side and window has a side
4312 ;; window sibling, bind `window-combination-limit' to t.
4313 ((and (not (eq window-combination-resize 'side))
4314 (or (and (window-prev-sibling window)
4315 (window-parameter
4316 (window-prev-sibling window) 'window-side))
4317 (and (window-next-sibling window)
4318 (window-parameter
4319 (window-next-sibling window) 'window-side))))
4320 (setq window-combination-limit t)))
4321
4322 ;; If `window-combination-resize' is t and SIZE is non-negative,
4323 ;; bind `window-combination-limit' to t.
880e6158
MR
4324 (when (and (eq window-combination-resize t)
4325 pixel-size (> pixel-size 0))
b6f67890 4326 (setq window-combination-limit t))
562dd5e9 4327
880e6158
MR
4328 (let* ((parent-pixel-size
4329 ;; `parent-pixel-size' is the pixel size of WINDOW's
4330 ;; parent, provided it has one.
4331 (when parent (window-size parent horizontal t)))
562dd5e9
MR
4332 ;; `resize' non-nil means we are supposed to resize other
4333 ;; windows in WINDOW's combination.
4334 (resize
caceae25
MR
4335 (and window-combination-resize
4336 (or (window-parameter window 'window-side)
4337 (not (eq window-combination-resize 'side)))
8e17c9ba 4338 (not (eq window-combination-limit t))
562dd5e9 4339 ;; Resize makes sense in iso-combinations only.
3d8daefe 4340 (window-combined-p window horizontal)))
880e6158
MR
4341 ;; `old-pixel-size' is the current pixel size of WINDOW.
4342 (old-pixel-size (window-size window horizontal t))
562dd5e9
MR
4343 ;; `new-size' is the specified or calculated size of the
4344 ;; new window.
2d6b6005 4345 new-pixel-size new-parent new-normal)
880e6158
MR
4346 (cond
4347 ((not pixel-size)
4348 (setq new-pixel-size
4349 (if resize
4350 ;; When resizing try to give the new window the
4351 ;; average size of a window in its combination.
4352 (min (- parent-pixel-size
4353 (window-min-size parent horizontal nil t))
4354 (/ parent-pixel-size
4355 (1+ (window-combinations parent horizontal))))
4356 ;; Else try to give the new window half the size
4357 ;; of WINDOW (plus an eventual odd pixel).
4358 (/ old-pixel-size 2)))
4359 (unless window-resize-pixelwise
4360 ;; Round to nearest char-size multiple.
4361 (setq new-pixel-size
4362 (* char-size (round new-pixel-size char-size)))))
4363 ((>= pixel-size 0)
4364 ;; SIZE non-negative specifies the new size of WINDOW.
4365
4366 ;; Note: Specifying a non-negative SIZE is practically
4367 ;; always done as workaround for making the new window
4368 ;; appear above or on the left of the new window (the
4369 ;; ispell window is a typical example of that). In all
4370 ;; these cases the SIDE argument should be set to 'above
4371 ;; or 'left in order to support the 'resize option.
4372 ;; Here we have to nest the windows instead, see above.
4373 (setq new-pixel-size (- old-pixel-size pixel-size)))
4374 (t
4375 ;; SIZE negative specifies the size of the new window.
4376 (setq new-pixel-size (- pixel-size))))
562dd5e9
MR
4377
4378 ;; Check SIZE.
4379 (cond
880e6158 4380 ((not pixel-size)
562dd5e9
MR
4381 (cond
4382 (resize
4383 ;; SIZE unspecified, resizing.
880e6158
MR
4384 (when (and (not (window-sizable-p
4385 parent (- new-pixel-size) horizontal nil t))
562dd5e9 4386 ;; Try again with minimum split size.
880e6158
MR
4387 (setq new-pixel-size
4388 (max new-pixel-size
4389 (window-split-min-size horizontal t)))
4390 (not (window-sizable-p
4391 parent (- new-pixel-size) horizontal nil t)))
4392 (error "Window %s too small for splitting 1" parent)))
4393 ((> (+ new-pixel-size (window-min-size window horizontal nil t))
4394 old-pixel-size)
562dd5e9 4395 ;; SIZE unspecified, no resizing.
880e6158
MR
4396 (error "Window %s too small for splitting 2" window))))
4397 ((and (>= pixel-size 0)
4398 (or (>= pixel-size old-pixel-size)
4399 (< new-pixel-size
4400 (window-safe-min-pixel-size window horizontal))))
562dd5e9
MR
4401 ;; SIZE specified as new size of old window. If the new size
4402 ;; is larger than the old size or the size of the new window
4403 ;; would be less than the safe minimum, signal an error.
880e6158 4404 (error "Window %s too small for splitting 3" window))
562dd5e9
MR
4405 (resize
4406 ;; SIZE specified, resizing.
880e6158
MR
4407 (unless (window-sizable-p
4408 parent (- new-pixel-size) horizontal nil t)
562dd5e9 4409 ;; If we cannot resize the parent give up.
880e6158
MR
4410 (error "Window %s too small for splitting 4" parent)))
4411 ((or (< new-pixel-size
4412 (window-safe-min-pixel-size window horizontal))
4413 (< (- old-pixel-size new-pixel-size)
4414 (window-safe-min-pixel-size window horizontal)))
562dd5e9 4415 ;; SIZE specification violates minimum size restrictions.
880e6158 4416 (error "Window %s too small for splitting 5" window)))
562dd5e9 4417
5386012d 4418 (window--resize-reset frame horizontal)
562dd5e9
MR
4419
4420 (setq new-parent
4421 ;; Make new-parent non-nil if we need a new parent window;
4422 ;; either because we want to nest or because WINDOW is not
4423 ;; iso-combined.
8e17c9ba 4424 (or (eq window-combination-limit t)
b6f67890 4425 (not (window-combined-p window horizontal))))
562dd5e9
MR
4426 (setq new-normal
4427 ;; Make new-normal the normal size of the new window.
4428 (cond
880e6158
MR
4429 (pixel-size (/ (float new-pixel-size)
4430 (if new-parent old-pixel-size parent-pixel-size)))
562dd5e9 4431 (new-parent 0.5)
3d8daefe 4432 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
562dd5e9
MR
4433 (t (/ (window-normal-size window horizontal) 2.0))))
4434
4435 (if resize
4436 ;; Try to get space from OLD's siblings. We could go "up" and
4437 ;; try getting additional space from surrounding windows but
4438 ;; we won't be able to return space to those windows when we
4439 ;; delete the one we create here. Hence we do not go up.
4440 (progn
880e6158
MR
4441 (window--resize-child-windows
4442 parent (- new-pixel-size) horizontal)
562dd5e9
MR
4443 (let* ((normal (- 1.0 new-normal))
4444 (sub (window-child parent)))
4445 (while sub
4446 (set-window-new-normal
4447 sub (* (window-normal-size sub horizontal) normal))
4448 (setq sub (window-right sub)))))
4449 ;; Get entire space from WINDOW.
880e6158
MR
4450 (set-window-new-pixel
4451 window (- old-pixel-size new-pixel-size))
4452;; (set-window-new-pixel window (- old-pixel-size new-pixel-size))
4453;; (set-window-new-total
4454;; window (- old-size new-size))
4455 (window--resize-this-window window (- new-pixel-size) horizontal)
562dd5e9
MR
4456 (set-window-new-normal
4457 window (- (if new-parent 1.0 (window-normal-size window horizontal))
4458 new-normal)))
4459
880e6158
MR
4460 (let* ((new (split-window-internal window new-pixel-size side new-normal)))
4461 (window--pixel-to-total frame horizontal)
caceae25 4462 ;; Assign window-side parameters, if any.
c660a885
MR
4463 (cond
4464 ((eq window-combination-resize 'side)
caceae25
MR
4465 (let ((window-side
4466 (cond
4467 (window-side window-side)
4468 ((eq side 'above) 'top)
4469 ((eq side 'below) 'bottom)
4470 (t side))))
4471 ;; We made a new side window.
4472 (set-window-parameter new 'window-side window-side)
4473 (when (and new-parent (window-parameter window 'window-side))
4474 ;; We've been splitting a side root window. Give the
4475 ;; new parent the same window-side parameter.
4476 (set-window-parameter
4477 (window-parent new) 'window-side window-side))))
c660a885
MR
4478 ((eq window-combination-resize 'atom)
4479 ;; Make sure `window--check-frame' won't destroy an existing
4480 ;; atomic window in case the new window gets nested inside.
4481 (unless (window-parameter window 'window-atom)
4482 (set-window-parameter window 'window-atom t))
4483 (when new-parent
4484 (set-window-parameter (window-parent new) 'window-atom t))
4485 (set-window-parameter new 'window-atom t)))
562dd5e9
MR
4486
4487 (run-window-configuration-change-hook frame)
880e6158 4488 (run-window-scroll-functions new)
54f9154c 4489 (window--check frame)
562dd5e9
MR
4490 ;; Always return the new window.
4491 new)))))
4492
4493;; I think this should be the default; I think people will prefer it--rms.
4494(defcustom split-window-keep-point t
2d197ffb
CY
4495 "If non-nil, \\[split-window-below] preserves point in the new window.
4496If nil, adjust point in the two windows to minimize redisplay.
4497This option applies only to `split-window-below' and functions
4498that call it. The low-level `split-window' function always keeps
4499the original point in both windows."
562dd5e9
MR
4500 :type 'boolean
4501 :group 'windows)
4502
2d197ffb
CY
4503(defun split-window-below (&optional size)
4504 "Split the selected window into two windows, one above the other.
4505The selected window is above. The newly split-off window is
4506below, and displays the same buffer. Return the new window.
4507
4508If optional argument SIZE is omitted or nil, both windows get the
4509same height, or close to it. If SIZE is positive, the upper
4510\(selected) window gets SIZE lines. If SIZE is negative, the
4511lower (new) window gets -SIZE lines.
4512
4513If the variable `split-window-keep-point' is non-nil, both
4514windows get the same value of point as the selected window.
4515Otherwise, the window starts are chosen so as to minimize the
4516amount of redisplay; this is convenient on slow terminals."
562dd5e9
MR
4517 (interactive "P")
4518 (let ((old-window (selected-window))
5481664a 4519 (old-point (window-point))
562dd5e9
MR
4520 (size (and size (prefix-numeric-value size)))
4521 moved-by-window-height moved new-window bottom)
4522 (when (and size (< size 0) (< (- size) window-min-height))
4523 ;; `split-window' would not signal an error here.
4524 (error "Size of new window too small"))
4525 (setq new-window (split-window nil size))
4526 (unless split-window-keep-point
4527 (with-current-buffer (window-buffer)
c491fa41
MR
4528 ;; Use `save-excursion' around vertical movements below
4529 ;; (Bug#10971). Note: When the selected window's buffer has a
4530 ;; header line, up to two lines of the buffer may not show up
4531 ;; in the resulting configuration.
4532 (save-excursion
4533 (goto-char (window-start))
4534 (setq moved (vertical-motion (window-height)))
4535 (set-window-start new-window (point))
4536 (when (> (point) (window-point new-window))
4537 (set-window-point new-window (point)))
4538 (when (= moved (window-height))
4539 (setq moved-by-window-height t)
4540 (vertical-motion -1))
4541 (setq bottom (point)))
4542 (and moved-by-window-height
4543 (<= bottom (point))
5481664a 4544 (set-window-point old-window (1- bottom)))
c491fa41
MR
4545 (and moved-by-window-height
4546 (<= (window-start new-window) old-point)
4547 (set-window-point new-window old-point)
4548 (select-window new-window))))
9397e56f
MR
4549 ;; Always copy quit-restore parameter in interactive use.
4550 (let ((quit-restore (window-parameter old-window 'quit-restore)))
4551 (when quit-restore
4552 (set-window-parameter new-window 'quit-restore quit-restore)))
4553 new-window))
562dd5e9 4554
2d197ffb 4555(defalias 'split-window-vertically 'split-window-below)
562dd5e9 4556
2d197ffb
CY
4557(defun split-window-right (&optional size)
4558 "Split the selected window into two side-by-side windows.
4559The selected window is on the left. The newly split-off window
4560is on the right, and displays the same buffer. Return the new
4561window.
562dd5e9 4562
2d197ffb
CY
4563If optional argument SIZE is omitted or nil, both windows get the
4564same width, or close to it. If SIZE is positive, the left-hand
4565\(selected) window gets SIZE columns. If SIZE is negative, the
4566right-hand (new) window gets -SIZE columns. Here, SIZE includes
4567the width of the window's scroll bar; if there are no scroll
4568bars, it includes the width of the divider column to the window's
4569right, if any."
562dd5e9
MR
4570 (interactive "P")
4571 (let ((old-window (selected-window))
4572 (size (and size (prefix-numeric-value size)))
4573 new-window)
4574 (when (and size (< size 0) (< (- size) window-min-width))
4575 ;; `split-window' would not signal an error here.
4576 (error "Size of new window too small"))
9397e56f
MR
4577 (setq new-window (split-window nil size t))
4578 ;; Always copy quit-restore parameter in interactive use.
4579 (let ((quit-restore (window-parameter old-window 'quit-restore)))
4580 (when quit-restore
4581 (set-window-parameter new-window 'quit-restore quit-restore)))
4582 new-window))
562dd5e9 4583
2d197ffb 4584(defalias 'split-window-horizontally 'split-window-right)
562dd5e9 4585\f
6198ccd0
MR
4586;;; Balancing windows.
4587
4588;; The following routine uses the recycled code from an old version of
be7f5545
MR
4589;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
4590;; new `window--resize-child-windows' code does would hardly make it any shorter or
6198ccd0
MR
4591;; more readable (FWIW we'd need three loops - one to calculate the
4592;; minimum sizes per window, one to enlarge or shrink windows until the
4593;; new parent-size matches, and one where we shrink the largest/enlarge
4594;; the smallest window).
4595(defun balance-windows-2 (window horizontal)
4596 "Subroutine of `balance-windows-1'.
3d8daefe 4597WINDOW must be a vertical combination (horizontal if HORIZONTAL
85c2386b 4598is non-nil)."
880e6158
MR
4599 (let* ((char-size (if window-resize-pixelwise
4600 1
4601 (frame-char-size window horizontal)))
4602 (first (window-child window))
6198ccd0
MR
4603 (sub first)
4604 (number-of-children 0)
880e6158 4605 (parent-size (window-new-pixel window))
6198ccd0 4606 (total-sum parent-size)
cb882333 4607 failed size sub-total sub-delta sub-amount rest)
6198ccd0
MR
4608 (while sub
4609 (setq number-of-children (1+ number-of-children))
4610 (when (window-size-fixed-p sub horizontal)
4611 (setq total-sum
880e6158 4612 (- total-sum (window-size sub horizontal t)))
6198ccd0
MR
4613 (set-window-new-normal sub 'ignore))
4614 (setq sub (window-right sub)))
3c448ab6 4615
6198ccd0
MR
4616 (setq failed t)
4617 (while (and failed (> number-of-children 0))
4618 (setq size (/ total-sum number-of-children))
4619 (setq failed nil)
4620 (setq sub first)
4621 (while (and sub (not failed))
be7f5545
MR
4622 ;; Ignore child windows that should be ignored or are stuck.
4623 (unless (window--resize-child-windows-skip-p sub)
880e6158 4624 (setq sub-total (window-size sub horizontal t))
6198ccd0
MR
4625 (setq sub-delta (- size sub-total))
4626 (setq sub-amount
880e6158 4627 (window-sizable sub sub-delta horizontal nil t))
be7f5545 4628 ;; Register the new total size for this child window.
880e6158 4629 (set-window-new-pixel sub (+ sub-total sub-amount))
6198ccd0
MR
4630 (unless (= sub-amount sub-delta)
4631 (setq total-sum (- total-sum sub-total sub-amount))
4632 (setq number-of-children (1- number-of-children))
4633 ;; We failed and need a new round.
4634 (setq failed t)
4635 (set-window-new-normal sub 'skip)))
4636 (setq sub (window-right sub))))
3c448ab6 4637
880e6158 4638 ;; How can we be sure that `number-of-children' is NOT zero here ?
6198ccd0
MR
4639 (setq rest (% total-sum number-of-children))
4640 ;; Fix rounding by trying to enlarge non-stuck windows by one line
4641 ;; (column) until `rest' is zero.
4642 (setq sub first)
4643 (while (and sub (> rest 0))
be7f5545 4644 (unless (window--resize-child-windows-skip-p window)
7e1899d7 4645 (set-window-new-pixel sub (min rest char-size) t)
880e6158 4646 (setq rest (- rest char-size)))
6198ccd0 4647 (setq sub (window-right sub)))
3c448ab6 4648
6198ccd0
MR
4649 ;; Fix rounding by trying to enlarge stuck windows by one line
4650 ;; (column) until `rest' equals zero.
4651 (setq sub first)
4652 (while (and sub (> rest 0))
4653 (unless (eq (window-new-normal sub) 'ignore)
7e1899d7 4654 (set-window-new-pixel sub (min rest char-size) t)
880e6158 4655 (setq rest (- rest char-size)))
6198ccd0
MR
4656 (setq sub (window-right sub)))
4657
4658 (setq sub first)
4659 (while sub
4660 ;; Record new normal sizes.
4661 (set-window-new-normal
4662 sub (/ (if (eq (window-new-normal sub) 'ignore)
880e6158
MR
4663 (window-size sub horizontal t)
4664 (window-new-pixel sub))
6198ccd0 4665 (float parent-size)))
be7f5545 4666 ;; Recursively balance each window's child windows.
6198ccd0
MR
4667 (balance-windows-1 sub horizontal)
4668 (setq sub (window-right sub)))))
4669
4670(defun balance-windows-1 (window &optional horizontal)
4671 "Subroutine of `balance-windows'."
4672 (if (window-child window)
4673 (let ((sub (window-child window)))
3d8daefe 4674 (if (window-combined-p sub horizontal)
6198ccd0 4675 (balance-windows-2 window horizontal)
880e6158 4676 (let ((size (window-new-pixel window)))
6198ccd0 4677 (while sub
880e6158 4678 (set-window-new-pixel sub size)
6198ccd0
MR
4679 (balance-windows-1 sub horizontal)
4680 (setq sub (window-right sub))))))))
4681
4682(defun balance-windows (&optional window-or-frame)
be7f5545 4683 "Balance the sizes of windows of WINDOW-OR-FRAME.
6198ccd0
MR
4684WINDOW-OR-FRAME is optional and defaults to the selected frame.
4685If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
4c36be58 4686windows of that frame. If WINDOW-OR-FRAME denotes a window,
be7f5545 4687recursively balance the sizes of all child windows of that
6198ccd0
MR
4688window."
4689 (interactive)
4690 (let* ((window
4691 (cond
4692 ((or (not window-or-frame)
4693 (frame-live-p window-or-frame))
4694 (frame-root-window window-or-frame))
4695 ((or (window-live-p window-or-frame)
4696 (window-child window-or-frame))
4697 window-or-frame)
4698 (t
4699 (error "Not a window or frame %s" window-or-frame))))
4700 (frame (window-frame window)))
4701 ;; Balance vertically.
5386012d 4702 (window--resize-reset (window-frame window))
6198ccd0 4703 (balance-windows-1 window)
880e6158
MR
4704 (when (window--resize-apply-p frame)
4705 (window-resize-apply frame)
4706 (window--pixel-to-total frame)
4707 (run-window-configuration-change-hook frame))
6198ccd0 4708 ;; Balance horizontally.
5386012d 4709 (window--resize-reset (window-frame window) t)
6198ccd0 4710 (balance-windows-1 window t)
880e6158 4711 (when (window--resize-apply-p frame t)
3669b636 4712 (window-resize-apply frame t)
880e6158
MR
4713 (window--pixel-to-total frame t)
4714 (run-window-configuration-change-hook frame))))
3c448ab6
MR
4715
4716(defun window-fixed-size-p (&optional window direction)
4717 "Return t if WINDOW cannot be resized in DIRECTION.
4718WINDOW defaults to the selected window. DIRECTION can be
4719nil (i.e. any), `height' or `width'."
4720 (with-current-buffer (window-buffer window)
4721 (when (and (boundp 'window-size-fixed) window-size-fixed)
4722 (not (and direction
4723 (member (cons direction window-size-fixed)
4724 '((height . width) (width . height))))))))
4725
4726;;; A different solution to balance-windows.
3c448ab6
MR
4727(defvar window-area-factor 1
4728 "Factor by which the window area should be over-estimated.
4729This is used by `balance-windows-area'.
4730Changing this globally has no effect.")
4731(make-variable-buffer-local 'window-area-factor)
4732
880e6158 4733(defun balance-windows-area-adjust (window delta horizontal pixelwise)
d615d6d2 4734 "Wrapper around `window-resize' with error checking.
6198ccd0 4735Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
d615d6d2 4736 ;; `window-resize' may fail if delta is too large.
6198ccd0
MR
4737 (while (>= (abs delta) 1)
4738 (condition-case nil
4739 (progn
880e6158
MR
4740 ;; It was wrong to use `window-resize' here. Somehow
4741 ;; `balance-windows-area' depends on resizing windows
4742 ;; asymmetrically.
4743 (adjust-window-trailing-edge window delta horizontal pixelwise)
6198ccd0
MR
4744 (setq delta 0))
4745 (error
4746 ;;(message "adjust: %s" (error-message-string err))
4747 (setq delta (/ delta 2))))))
4748
3c448ab6
MR
4749(defun balance-windows-area ()
4750 "Make all visible windows the same area (approximately).
4751See also `window-area-factor' to change the relative size of
4752specific buffers."
4753 (interactive)
4754 (let* ((unchanged 0) (carry 0) (round 0)
4755 ;; Remove fixed-size windows.
4756 (wins (delq nil (mapcar (lambda (win)
4757 (if (not (window-fixed-size-p win)) win))
4758 (window-list nil 'nomini))))
4759 (changelog nil)
880e6158
MR
4760 (pixelwise window-resize-pixelwise)
4761 next)
3c448ab6
MR
4762 ;; Resizing a window changes the size of surrounding windows in complex
4763 ;; ways, so it's difficult to balance them all. The introduction of
4764 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
4765 ;; very difficult to do. `balance-window' above takes an off-line
4766 ;; approach: get the whole window tree, then balance it, then try to
4767 ;; adjust the windows so they fit the result.
4768 ;; Here, instead, we take a "local optimization" approach, where we just
4769 ;; go through all the windows several times until nothing needs to be
4770 ;; changed. The main problem with this approach is that it's difficult
4771 ;; to make sure it terminates, so we use some heuristic to try and break
4772 ;; off infinite loops.
4773 ;; After a round without any change, we allow a second, to give a chance
4774 ;; to the carry to propagate a minor imbalance from the end back to
4775 ;; the beginning.
4776 (while (< unchanged 2)
4777 ;; (message "New round")
4778 (setq unchanged (1+ unchanged) round (1+ round))
4779 (dolist (win wins)
4780 (setq next win)
4781 (while (progn (setq next (next-window next))
4782 (window-fixed-size-p next)))
4783 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
4784 (let* ((horiz
880e6158 4785 (< (car (window-pixel-edges win)) (car (window-pixel-edges next))))
18cee9ec
MR
4786 (areadiff (/ (- (* (window-size next nil pixelwise)
4787 (window-size next t pixelwise)
3c448ab6
MR
4788 (buffer-local-value 'window-area-factor
4789 (window-buffer next)))
18cee9ec
MR
4790 (* (window-size win nil pixelwise)
4791 (window-size win t pixelwise)
3c448ab6
MR
4792 (buffer-local-value 'window-area-factor
4793 (window-buffer win))))
4794 (max (buffer-local-value 'window-area-factor
4795 (window-buffer win))
4796 (buffer-local-value 'window-area-factor
4797 (window-buffer next)))))
4798 (edgesize (if horiz
18cee9ec
MR
4799 (+ (window-size win nil pixelwise)
4800 (window-size next nil pixelwise))
4801 (+ (window-size win t pixelwise)
4802 (window-size next t pixelwise))))
3c448ab6
MR
4803 (diff (/ areadiff edgesize)))
4804 (when (zerop diff)
4805 ;; Maybe diff is actually closer to 1 than to 0.
4806 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
4807 (when (and (zerop diff) (not (zerop areadiff)))
4808 (setq diff (/ (+ areadiff carry) edgesize))
4809 ;; Change things smoothly.
4810 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
4811 (if (zerop diff)
4812 ;; Make sure negligible differences don't accumulate to
4813 ;; become significant.
4814 (setq carry (+ carry areadiff))
6198ccd0 4815 ;; This used `adjust-window-trailing-edge' before and uses
d615d6d2 4816 ;; `window-resize' now. Error wrapping is still needed.
880e6158 4817 (balance-windows-area-adjust win diff horiz pixelwise)
3c448ab6 4818 ;; (sit-for 0.5)
880e6158 4819 (let ((change (cons win (window-pixel-edges win))))
3c448ab6
MR
4820 ;; If the same change has been seen already for this window,
4821 ;; we're most likely in an endless loop, so don't count it as
4822 ;; a change.
4823 (unless (member change changelog)
4824 (push change changelog)
4825 (setq unchanged 0 carry 0)))))))
4826 ;; We've now basically balanced all the windows.
4827 ;; But there may be some minor off-by-one imbalance left over,
4828 ;; so let's do some fine tuning.
4829 ;; (bw-finetune wins)
4830 ;; (message "Done in %d rounds" round)
4831 ))
9d89fec7
MR
4832
4833;;; Window states, how to get them and how to put them in a window.
34a02f46 4834(defun window--state-get-1 (window &optional writable)
9d89fec7
MR
4835 "Helper function for `window-state-get'."
4836 (let* ((type
4837 (cond
d68443dc
MR
4838 ((window-top-child window) 'vc)
4839 ((window-left-child window) 'hc)
9d89fec7
MR
4840 (t 'leaf)))
4841 (buffer (window-buffer window))
4842 (selected (eq window (selected-window)))
4843 (head
4b0d61e3
SM
4844 `(,type
4845 ,@(unless (window-next-sibling window) `((last . t)))
880e6158
MR
4846 (pixel-width . ,(window-pixel-width window))
4847 (pixel-height . ,(window-pixel-height window))
4848 (total-width . ,(window-total-width window))
4849 (total-height . ,(window-total-height window))
4b0d61e3
SM
4850 (normal-height . ,(window-normal-size window))
4851 (normal-width . ,(window-normal-size window t))
8bbdea0f 4852 ,@(unless (window-live-p window)
ab5340fe 4853 `((combination-limit . ,(window-combination-limit window))))
34a02f46
MR
4854 ,@(let ((parameters (window-parameters window))
4855 list)
4856 ;; Make copies of those window parameters whose
4857 ;; persistence property is `writable' if WRITABLE is
4858 ;; non-nil and non-nil if WRITABLE is nil.
4859 (dolist (par parameters)
4860 (let ((pers (cdr (assq (car par)
4861 window-persistent-parameters))))
4862 (when (and pers (or (not writable) (eq pers 'writable)))
4863 (setq list (cons (cons (car par) (cdr par)) list)))))
4864 ;; Add `clone-of' parameter if necessary.
4865 (let ((pers (cdr (assq 'clone-of
4866 window-persistent-parameters))))
4867 (when (and pers (or (not writable) (eq pers 'writable))
4868 (not (assq 'clone-of list)))
4869 (setq list (cons (cons 'clone-of window) list))))
4b0d61e3
SM
4870 (when list
4871 `((parameters . ,list))))
4872 ,@(when buffer
1edf595d 4873 ;; All buffer related things go in here.
5481664a 4874 (let ((point (window-point window))
1edf595d
MR
4875 (start (window-start window)))
4876 `((buffer
4877 ,(buffer-name buffer)
4878 (selected . ,selected)
4879 (hscroll . ,(window-hscroll window))
4880 (fringes . ,(window-fringes window))
4881 (margins . ,(window-margins window))
4882 (scroll-bars . ,(window-scroll-bars window))
4883 (vscroll . ,(window-vscroll window))
4884 (dedicated . ,(window-dedicated-p window))
de8c03dc
SM
4885 (point . ,(if writable point
4886 (copy-marker point
4887 (buffer-local-value
4888 'window-point-insertion-type
4889 buffer))))
1edf595d 4890 (start . ,(if writable start (copy-marker start)))))))))
9d89fec7
MR
4891 (tail
4892 (when (memq type '(vc hc))
4893 (let (list)
4894 (setq window (window-child window))
4895 (while window
34a02f46 4896 (setq list (cons (window--state-get-1 window writable) list))
9d89fec7
MR
4897 (setq window (window-right window)))
4898 (nreverse list)))))
4899 (append head tail)))
4900
34a02f46 4901(defun window-state-get (&optional window writable)
9d89fec7
MR
4902 "Return state of WINDOW as a Lisp object.
4903WINDOW can be any window and defaults to the root window of the
4904selected frame.
4905
34a02f46
MR
4906Optional argument WRITABLE non-nil means do not use markers for
4907sampling `window-point' and `window-start'. Together, WRITABLE
4908and the variable `window-persistent-parameters' specify which
4909window parameters are saved by this function. WRITABLE should be
4910non-nil when the return value shall be written to a file and read
4911back in another session. Otherwise, an application may run into
4912an `invalid-read-syntax' error while attempting to read back the
4913value from file.
9d89fec7
MR
4914
4915The return value can be used as argument for `window-state-put'
4916to put the state recorded here into an arbitrary window. The
4917value can be also stored on disk and read back in a new session."
4918 (setq window
4919 (if window
24300f5f 4920 (if (window-valid-p window)
9d89fec7
MR
4921 window
4922 (error "%s is not a live or internal window" window))
4923 (frame-root-window)))
4924 ;; The return value is a cons whose car specifies some constraints on
be7f5545
MR
4925 ;; the size of WINDOW. The cdr lists the states of the child windows
4926 ;; of WINDOW.
9d89fec7
MR
4927 (cons
4928 ;; Frame related things would go into a function, say `frame-state',
4929 ;; calling `window-state-get' to insert the frame's root window.
4b0d61e3
SM
4930 `((min-height . ,(window-min-size window))
4931 (min-width . ,(window-min-size window t))
4932 (min-height-ignore . ,(window-min-size window nil t))
4933 (min-width-ignore . ,(window-min-size window t t))
4934 (min-height-safe . ,(window-min-size window nil 'safe))
880e6158
MR
4935 (min-width-safe . ,(window-min-size window t 'safe))
4936 (min-pixel-height . ,(window-min-size window nil nil t))
4937 (min-pixel-width . ,(window-min-size window t nil t))
4938 (min-pixel-height-ignore . ,(window-min-size window nil t t))
4939 (min-pixel-width-ignore . ,(window-min-size window t t t))
4940 (min-pixel-height-safe . ,(window-min-size window nil 'safe t))
4941 (min-pixel-width-safe . ,(window-min-size window t 'safe t)))
34a02f46 4942 (window--state-get-1 window writable)))
9d89fec7
MR
4943
4944(defvar window-state-put-list nil
4945 "Helper variable for `window-state-put'.")
4946
34ada5f4
MR
4947(defvar window-state-put-stale-windows nil
4948 "Helper variable for `window-state-put'.")
4949
880e6158 4950(defun window--state-put-1 (state &optional window ignore totals pixelwise)
9d89fec7
MR
4951 "Helper function for `window-state-put'."
4952 (let ((type (car state)))
4953 (setq state (cdr state))
4954 (cond
4955 ((eq type 'leaf)
4956 ;; For a leaf window just add unprocessed entries to
4957 ;; `window-state-put-list'.
c7635a97 4958 (push (cons window state) window-state-put-list))
9d89fec7
MR
4959 ((memq type '(vc hc))
4960 (let* ((horizontal (eq type 'hc))
880e6158 4961 (total (window-size window horizontal pixelwise))
9d89fec7
MR
4962 (first t)
4963 size new)
4964 (dolist (item state)
4965 ;; Find the next child window. WINDOW always points to the
4966 ;; real window that we want to fill with what we find here.
4967 (when (memq (car item) '(leaf vc hc))
4968 (if (assq 'last item)
c56cad4a 4969 ;; The last child window. Below `window--state-put-1'
9d89fec7
MR
4970 ;; will put into it whatever ITEM has in store.
4971 (setq new nil)
4972 ;; Not the last child window, prepare for splitting
4973 ;; WINDOW. SIZE is the new (and final) size of the old
4974 ;; window.
4975 (setq size
4976 (if totals
4977 ;; Use total size.
880e6158
MR
4978 (if pixelwise
4979 (cdr (assq (if horizontal
4980 'pixel-width
4981 'pixel-height)
4982 item))
4983 (cdr (assq (if horizontal
4984 'total-width
4985 'total-height)
4986 item)))
9d89fec7 4987 ;; Use normalized size and round.
880e6158
MR
4988 (round
4989 (* total
4990 (cdr (assq (if horizontal 'normal-width 'normal-height)
4991 item))))))
9d89fec7
MR
4992
4993 ;; Use safe sizes, we try to resize later.
880e6158
MR
4994 (setq size (max size
4995 (if horizontal
4996 (* window-safe-min-width
4997 (if pixelwise
4998 (frame-char-width (window-frame window))
4999 1))
5000 (* window-safe-min-height
5001 (if pixelwise
5002 (frame-char-height (window-frame window))
5003 1)))))
5004 (if (window-sizable-p window (- size) horizontal 'safe pixelwise)
b6f67890
MR
5005 (let* ((window-combination-limit
5006 (assq 'combination-limit item)))
301b181a 5007 ;; We must inherit the combination limit, otherwise
b6f67890
MR
5008 ;; we might mess up handling of atomic and side
5009 ;; window.
880e6158 5010 (setq new (split-window window size horizontal pixelwise)))
9d89fec7
MR
5011 ;; Give up if we can't resize window down to safe sizes.
5012 (error "Cannot resize window %s" window))
5013
5014 (when first
5015 (setq first nil)
5016 ;; When creating the first child window add for parent
5017 ;; unprocessed entries to `window-state-put-list'.
5018 (setq window-state-put-list
5019 (cons (cons (window-parent window) state)
5020 window-state-put-list))))
5021
5022 ;; Now process the current window (either the one we've just
5023 ;; split or the last child of its parent).
c56cad4a 5024 (window--state-put-1 item window ignore totals)
9d89fec7
MR
5025 ;; Continue with the last window split off.
5026 (setq window new))))))))
5027
880e6158 5028(defun window--state-put-2 (ignore pixelwise)
9d89fec7
MR
5029 "Helper function for `window-state-put'."
5030 (dolist (item window-state-put-list)
5031 (let ((window (car item))
b6f67890 5032 (combination-limit (cdr (assq 'combination-limit item)))
9d89fec7
MR
5033 (parameters (cdr (assq 'parameters item)))
5034 (state (cdr (assq 'buffer item))))
b6f67890
MR
5035 (when combination-limit
5036 (set-window-combination-limit window combination-limit))
34a02f46
MR
5037 ;; Reset window's parameters and assign saved ones (we might want
5038 ;; a `remove-window-parameters' function here).
5039 (dolist (parameter (window-parameters window))
5040 (set-window-parameter window (car parameter) nil))
9d89fec7
MR
5041 (when parameters
5042 (dolist (parameter parameters)
34a02f46 5043 (set-window-parameter window (car parameter) (cdr parameter))))
9d89fec7
MR
5044 ;; Process buffer related state.
5045 (when state
34ada5f4
MR
5046 (let ((buffer (get-buffer (car state))))
5047 (if buffer
c089653d
MR
5048 (with-current-buffer buffer
5049 (set-window-buffer window buffer)
5050 (set-window-hscroll window (cdr (assq 'hscroll state)))
5051 (apply 'set-window-fringes
5052 (cons window (cdr (assq 'fringes state))))
5053 (let ((margins (cdr (assq 'margins state))))
5054 (set-window-margins window (car margins) (cdr margins)))
5055 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
5056 (set-window-scroll-bars
5057 window (car scroll-bars) (nth 2 scroll-bars)
5058 (nth 3 scroll-bars)))
5059 (set-window-vscroll window (cdr (assq 'vscroll state)))
5060 ;; Adjust vertically.
5061 (if (memq window-size-fixed '(t height))
5062 ;; A fixed height window, try to restore the
5063 ;; original size.
880e6158
MR
5064 (let ((delta
5065 (- (cdr (assq
5066 (if pixelwise 'pixel-height 'total-height)
5067 item))
5068 (window-size window nil pixelwise)))
c089653d 5069 window-size-fixed)
880e6158
MR
5070 (when (window--resizable-p
5071 window delta nil nil nil nil nil pixelwise)
5072 (window-resize window delta nil nil pixelwise)))
c089653d 5073 ;; Else check whether the window is not high enough.
880e6158
MR
5074 (let* ((min-size
5075 (window-min-size window nil ignore pixelwise))
5076 (delta
5077 (- min-size (window-size window nil pixelwise))))
c089653d 5078 (when (and (> delta 0)
880e6158
MR
5079 (window--resizable-p
5080 window delta nil ignore nil nil nil pixelwise))
5081 (window-resize window delta nil ignore pixelwise))))
c089653d
MR
5082 ;; Adjust horizontally.
5083 (if (memq window-size-fixed '(t width))
5084 ;; A fixed width window, try to restore the original
5085 ;; size.
880e6158
MR
5086 (let ((delta
5087 (- (cdr (assq
5088 (if pixelwise 'pixel-width 'total-width)
5089 item))
5090 (window-size window t pixelwise)))
c089653d 5091 window-size-fixed)
880e6158
MR
5092 (when (window--resizable-p
5093 window delta nil nil nil nil nil pixelwise)
5094 (window-resize window delta nil nil pixelwise)))
c089653d 5095 ;; Else check whether the window is not wide enough.
880e6158
MR
5096 (let* ((min-size (window-min-size window t ignore pixelwise))
5097 (delta (- min-size (window-size window t pixelwise))))
c089653d 5098 (when (and (> delta 0)
880e6158
MR
5099 (window--resizable-p
5100 window delta t ignore nil nil nil pixelwise))
5101 (window-resize window delta t ignore pixelwise))))
c089653d
MR
5102 ;; Set dedicated status.
5103 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
5104 ;; Install positions (maybe we should do this after all
5105 ;; windows have been created and sized).
5106 (ignore-errors
5107 (set-window-start window (cdr (assq 'start state)))
5108 (set-window-point window (cdr (assq 'point state))))
5109 ;; Select window if it's the selected one.
5110 (when (cdr (assq 'selected state))
5111 (select-window window)))
5112 ;; We don't want to raise an error in case the buffer does
5113 ;; not exist anymore, so we switch to a previous one and
5114 ;; save the window with the intention of deleting it later
5115 ;; if possible.
34ada5f4 5116 (switch-to-prev-buffer window)
c089653d 5117 (push window window-state-put-stale-windows)))))))
9d89fec7
MR
5118
5119(defun window-state-put (state &optional window ignore)
5120 "Put window state STATE into WINDOW.
5121STATE should be the state of a window returned by an earlier
5122invocation of `window-state-get'. Optional argument WINDOW must
8e009b78
MR
5123specify a valid window and defaults to the selected one. If
5124WINDOW is not live, replace WINDOW by a live one before putting
5125STATE into it.
9d89fec7
MR
5126
5127Optional argument IGNORE non-nil means ignore minimum window
5128sizes and fixed size restrictions. IGNORE equal `safe' means
be7f5545 5129windows can get as small as `window-safe-min-height' and
9d89fec7 5130`window-safe-min-width'."
34ada5f4 5131 (setq window-state-put-stale-windows nil)
8e009b78
MR
5132 (setq window (window-normalize-window window))
5133
5134 ;; When WINDOW is internal, reduce it to a live one to put STATE into,
5135 ;; see Bug#16793.
5136 (unless (window-live-p window)
5137 (let ((root (frame-root-window window)))
5138 (if (eq window root)
5139 (setq window (frame-first-window root))
5140 (setq root window)
5141 (setq window (catch 'live
5142 (walk-window-subtree
5143 (lambda (window)
5144 (when (window-live-p window)
5145 (throw 'live window)))
5146 root))))
5147 (delete-other-windows-internal window root)))
5148
9d89fec7
MR
5149 (let* ((frame (window-frame window))
5150 (head (car state))
5151 ;; We check here (1) whether the total sizes of root window of
5152 ;; STATE and that of WINDOW are equal so we can avoid
5153 ;; calculating new sizes, and (2) if we do have to resize
5154 ;; whether we can do so without violating size restrictions.
880e6158
MR
5155 (pixelwise (and (cdr (assq 'pixel-width state))
5156 (cdr (assq 'pixel-height state))))
5157 (totals (or (and pixelwise
5158 (= (window-pixel-width window)
5159 (cdr (assq 'pixel-width state)))
5160 (= (window-pixel-height window)
5161 (cdr (assq 'pixel-height state))))
5162 (and (= (window-total-width window)
5163 (cdr (assq 'total-width state)))
5164 (= (window-total-height window)
5165 (cdr (assq 'total-height state))))))
5166 (min-height (cdr (assq
5167 (if pixelwise 'min-pixel-height 'min-height)
5168 head)))
5169 (min-width (cdr (assq
5170 (if pixelwise 'min-pixel-width 'min-weight)
5171 head))))
9d89fec7 5172 (if (and (not totals)
880e6158
MR
5173 (or (> min-height (window-size window nil pixelwise))
5174 (> min-width (window-size window t pixelwise)))
9d89fec7
MR
5175 (or (not ignore)
5176 (and (setq min-height
880e6158
MR
5177 (cdr (assq
5178 (if pixelwise
5179 'min-pixel-height-ignore
5180 'min-height-ignore)
5181 head)))
9d89fec7 5182 (setq min-width
880e6158
MR
5183 (cdr (assq
5184 (if pixelwise
5185 'min-pixel-width-ignore
5186 'min-width-ignore)
5187 head)))
5188 (or (> min-height
5189 (window-size window nil pixelwise))
5190 (> min-width
5191 (window-size window t pixelwise)))
9d89fec7
MR
5192 (or (not (eq ignore 'safe))
5193 (and (setq min-height
880e6158
MR
5194 (cdr (assq
5195 (if pixelwise
5196 'min-pixel-height-safe
5197 'min-height-safe)
5198 head)))
9d89fec7 5199 (setq min-width
880e6158
MR
5200 (cdr (assq
5201 (if pixelwise
5202 'min-pixel-width-safe
5203 'min-width-safe)
5204 head)))
9d89fec7 5205 (or (> min-height
880e6158 5206 (window-size window nil pixelwise))
9d89fec7 5207 (> min-width
880e6158 5208 (window-size window t pixelwise))))))))
9d89fec7
MR
5209 ;; The check above might not catch all errors due to rounding
5210 ;; issues - so IGNORE equal 'safe might not always produce the
5211 ;; minimum possible state. But such configurations hardly make
5212 ;; sense anyway.
a91adc7e 5213 (error "Window %s too small to accommodate state" window)
9d89fec7
MR
5214 (setq state (cdr state))
5215 (setq window-state-put-list nil)
5216 ;; Work on the windows of a temporary buffer to make sure that
5217 ;; splitting proceeds regardless of any buffer local values of
5218 ;; `window-size-fixed'. Release that buffer after the buffers of
c56cad4a 5219 ;; all live windows have been set by `window--state-put-2'.
9d89fec7
MR
5220 (with-temp-buffer
5221 (set-window-buffer window (current-buffer))
880e6158
MR
5222 (window--state-put-1 state window nil totals pixelwise)
5223 (window--state-put-2 ignore pixelwise))
34ada5f4
MR
5224 (while window-state-put-stale-windows
5225 (let ((window (pop window-state-put-stale-windows)))
5226 (when (eq (window-deletable-p window) t)
5227 (delete-window window))))
54f9154c 5228 (window--check frame))))
9481c002 5229\f
f818cd2a
MR
5230(defun display-buffer-record-window (type window buffer)
5231 "Record information for window used by `display-buffer'.
cf4eacfd 5232TYPE specifies the type of the calling operation and must be one
382c953b
JB
5233of the symbols 'reuse (when WINDOW existed already and was
5234reused for displaying BUFFER), 'window (when WINDOW was created
5235on an already existing frame), or 'frame (when WINDOW was
5236created on a new frame). WINDOW is the window used for or created
cf4eacfd
MR
5237by the `display-buffer' routines. BUFFER is the buffer that
5238shall be displayed.
5239
5240This function installs or updates the quit-restore parameter of
5241WINDOW. The quit-restore parameter is a list of four elements:
5242The first element is one of the symbols 'window, 'frame, 'same or
5243'other. The second element is either one of the symbols 'window
5244or 'frame or a list whose elements are the buffer previously
5245shown in the window, that buffer's window start and window point,
5246and the window's height. The third element is the window
5247selected at the time the parameter was created. The fourth
5248element is BUFFER."
f818cd2a 5249 (cond
cf4eacfd
MR
5250 ((eq type 'reuse)
5251 (if (eq (window-buffer window) buffer)
5252 ;; WINDOW shows BUFFER already.
5253 (when (consp (window-parameter window 'quit-restore))
5254 ;; If WINDOW has a quit-restore parameter, reset its car.
5255 (setcar (window-parameter window 'quit-restore) 'same))
5256 ;; WINDOW shows another buffer.
92346275
MR
5257 (with-current-buffer (window-buffer window)
5258 (set-window-parameter
5259 window 'quit-restore
5260 (list 'other
5261 ;; A quadruple of WINDOW's buffer, start, point and height.
5262 (list (current-buffer) (window-start window)
5263 ;; Preserve window-point-insertion-type (Bug#12588).
5264 (copy-marker
5265 (window-point window) window-point-insertion-type)
880e6158 5266 (window-total-height window))
92346275 5267 (selected-window) buffer)))))
cf4eacfd
MR
5268 ((eq type 'window)
5269 ;; WINDOW has been created on an existing frame.
f818cd2a 5270 (set-window-parameter
cf4eacfd
MR
5271 window 'quit-restore
5272 (list 'window 'window (selected-window) buffer)))
5273 ((eq type 'frame)
5274 ;; WINDOW has been created on a new frame.
f818cd2a 5275 (set-window-parameter
cf4eacfd
MR
5276 window 'quit-restore
5277 (list 'frame 'frame (selected-window) buffer)))))
9481c002 5278
f818cd2a
MR
5279(defcustom display-buffer-function nil
5280 "If non-nil, function to call to handle `display-buffer'.
5281It will receive two args, the buffer and a flag which if non-nil
5282means that the currently selected window is not acceptable. It
5283should choose or create a window, display the specified buffer in
5284it, and return the window.
5285
e1c2c6f2
MR
5286The specified function should call `display-buffer-record-window'
5287with corresponding arguments to set up the quit-restore parameter
5288of the window used."
f818cd2a
MR
5289 :type '(choice
5290 (const nil)
5291 (function :tag "function"))
3c448ab6 5292 :group 'windows)
9481c002 5293
71ce58e7
CY
5294(make-obsolete-variable 'display-buffer-function
5295 'display-buffer-alist "24.3")
5296
d97af5a0
CY
5297;; Eventually, we want to turn this into a defvar; instead of
5298;; customizing this, the user should use a `pop-up-frame-parameters'
5299;; alist entry in `display-buffer-base-action'.
f818cd2a
MR
5300(defcustom pop-up-frame-alist nil
5301 "Alist of parameters for automatically generated new frames.
f818cd2a
MR
5302If non-nil, the value you specify here is used by the default
5303`pop-up-frame-function' for the creation of new frames.
9481c002 5304
f818cd2a
MR
5305Since `pop-up-frame-function' is used by `display-buffer' for
5306making new frames, any value specified here by default affects
5307the automatic generation of new frames via `display-buffer' and
5308all functions based on it. The behavior of `make-frame' is not
5309affected by this variable."
9481c002 5310 :type '(repeat (cons :format "%v"
f818cd2a
MR
5311 (symbol :tag "Parameter")
5312 (sexp :tag "Value")))
9481c002 5313 :group 'frames)
9481c002 5314
f818cd2a
MR
5315(defcustom pop-up-frame-function
5316 (lambda () (make-frame pop-up-frame-alist))
5317 "Function used by `display-buffer' for creating a new frame.
5318This function is called with no arguments and should return a new
5319frame. The default value calls `make-frame' with the argument
5320`pop-up-frame-alist'."
9481c002 5321 :type 'function
9481c002 5322 :group 'frames)
3c448ab6 5323
56f31926
MR
5324(defcustom special-display-buffer-names nil
5325 "List of names of buffers that should be displayed specially.
5326Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5327its name is in this list, displays the buffer in a way specified
5328by `special-display-function'. `special-display-popup-frame'
5329\(the default for `special-display-function') usually displays
5330the buffer in a separate frame made with the parameters specified
5331by `special-display-frame-alist'. If `special-display-function'
5332has been set to some other function, that function is called with
5333the buffer as first, and nil as second argument.
5334
5335Alternatively, an element of this list can be specified as
5336\(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
382c953b 5337name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
56f31926
MR
5338`special-display-popup-frame' will interpret such pairs as frame
5339parameters when it creates a special frame, overriding the
5340corresponding values from `special-display-frame-alist'.
5341
5342As a special case, if FRAME-PARAMETERS contains (same-window . t)
5343`special-display-popup-frame' displays that buffer in the
5344selected window. If FRAME-PARAMETERS contains (same-frame . t),
5345it displays that buffer in a window on the selected frame.
5346
5347If `special-display-function' specifies some other function than
5348`special-display-popup-frame', that function is called with the
5349buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5350argument.
5351
5352Finally, an element of this list can be also specified as
5353\(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5354`special-display-popup-frame' will call FUNCTION with the buffer
5355named BUFFER-NAME as first argument, and OTHER-ARGS as the
0563dae9
MR
5356second.
5357
5358Any alternative function specified here is responsible for
5359setting up the quit-restore parameter of the window used.
56f31926
MR
5360
5361If this variable appears \"not to work\", because you added a
5362name to it but the corresponding buffer is displayed in the
5363selected window, look at the values of `same-window-buffer-names'
5364and `same-window-regexps'. Those variables take precedence over
5365this one.
5366
5367See also `special-display-regexps'."
5368 :type '(repeat
5369 (choice :tag "Buffer"
5370 :value ""
5371 (string :format "%v")
5372 (cons :tag "With parameters"
5373 :format "%v"
5374 :value ("" . nil)
5375 (string :format "%v")
5376 (repeat :tag "Parameters"
5377 (cons :format "%v"
5378 (symbol :tag "Parameter")
5379 (sexp :tag "Value"))))
5380 (list :tag "With function"
5381 :format "%v"
5382 :value ("" . nil)
5383 (string :format "%v")
5384 (function :tag "Function")
5385 (repeat :tag "Arguments" (sexp)))))
5386 :group 'windows
5387 :group 'frames)
77f1f99c 5388(make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3")
ac549fa5
GM
5389(put 'special-display-buffer-names 'risky-local-variable t)
5390
56f31926
MR
5391(defcustom special-display-regexps nil
5392 "List of regexps saying which buffers should be displayed specially.
5393Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5394any regexp in this list matches its name, displays it specially
5395using `special-display-function'. `special-display-popup-frame'
5396\(the default for `special-display-function') usually displays
5397the buffer in a separate frame made with the parameters specified
5398by `special-display-frame-alist'. If `special-display-function'
5399has been set to some other function, that function is called with
5400the buffer as first, and nil as second argument.
5401
5402Alternatively, an element of this list can be specified as
5403\(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
5404FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5405`special-display-popup-frame' will then interpret these pairs as
5406frame parameters when creating a special frame for a buffer whose
5407name matches REGEXP, overriding the corresponding values from
5408`special-display-frame-alist'.
5409
5410As a special case, if FRAME-PARAMETERS contains (same-window . t)
5411`special-display-popup-frame' displays buffers matching REGEXP in
382c953b 5412the selected window. (same-frame . t) in FRAME-PARAMETERS means
56f31926
MR
5413to display such buffers in a window on the selected frame.
5414
5415If `special-display-function' specifies some other function than
5416`special-display-popup-frame', that function is called with the
5417buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
5418as second argument.
5419
5420Finally, an element of this list can be also specified as
5421\(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
5422will then call FUNCTION with the buffer whose name matched
0563dae9
MR
5423REGEXP as first, and OTHER-ARGS as second argument.
5424
5425Any alternative function specified here is responsible for
5426setting up the quit-restore parameter of the window used.
56f31926
MR
5427
5428If this variable appears \"not to work\", because you added a
5429name to it but the corresponding buffer is displayed in the
5430selected window, look at the values of `same-window-buffer-names'
5431and `same-window-regexps'. Those variables take precedence over
5432this one.
5433
5434See also `special-display-buffer-names'."
5435 :type '(repeat
5436 (choice :tag "Buffer"
5437 :value ""
5438 (regexp :format "%v")
5439 (cons :tag "With parameters"
5440 :format "%v"
5441 :value ("" . nil)
5442 (regexp :format "%v")
5443 (repeat :tag "Parameters"
5444 (cons :format "%v"
5445 (symbol :tag "Parameter")
5446 (sexp :tag "Value"))))
5447 (list :tag "With function"
5448 :format "%v"
5449 :value ("" . nil)
5450 (regexp :format "%v")
5451 (function :tag "Function")
5452 (repeat :tag "Arguments" (sexp)))))
5453 :group 'windows
5454 :group 'frames)
77f1f99c
CY
5455(make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3")
5456(put 'special-display-regexps 'risky-local-variable t)
56f31926 5457
3c448ab6
MR
5458(defun special-display-p (buffer-name)
5459 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
56f31926
MR
5460More precisely, return t if `special-display-buffer-names' or
5461`special-display-regexps' contain a string entry equaling or
5462matching BUFFER-NAME. If `special-display-buffer-names' or
5463`special-display-regexps' contain a list entry whose car equals
5464or matches BUFFER-NAME, the return value is the cdr of that
5465entry."
f818cd2a 5466 (let (tmp)
98722073 5467 (cond
f818cd2a 5468 ((member buffer-name special-display-buffer-names)
98722073 5469 t)
f818cd2a 5470 ((setq tmp (assoc buffer-name special-display-buffer-names))
98722073
MR
5471 (cdr tmp))
5472 ((catch 'found
f818cd2a 5473 (dolist (regexp special-display-regexps)
98722073
MR
5474 (cond
5475 ((stringp regexp)
5476 (when (string-match-p regexp buffer-name)
5477 (throw 'found t)))
5478 ((and (consp regexp) (stringp (car regexp))
5479 (string-match-p (car regexp) buffer-name))
5480 (throw 'found (cdr regexp))))))))))
9481c002 5481
f818cd2a
MR
5482(defcustom special-display-frame-alist
5483 '((height . 14) (width . 80) (unsplittable . t))
5484 "Alist of parameters for special frames.
5485Special frames are used for buffers whose names are listed in
5486`special-display-buffer-names' and for buffers whose names match
5487one of the regular expressions in `special-display-regexps'.
9481c002 5488
f818cd2a 5489This variable can be set in your init file, like this:
9481c002 5490
f818cd2a
MR
5491 (setq special-display-frame-alist '((width . 80) (height . 20)))
5492
5493These supersede the values given in `default-frame-alist'."
5494 :type '(repeat (cons :format "%v"
5495 (symbol :tag "Parameter")
5496 (sexp :tag "Value")))
5497 :group 'frames)
77f1f99c 5498(make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3")
f818cd2a
MR
5499
5500(defun special-display-popup-frame (buffer &optional args)
31cd32c9 5501 "Pop up a frame displaying BUFFER and return its window.
f818cd2a
MR
5502If BUFFER is already displayed in a visible or iconified frame,
5503raise that frame. Otherwise, display BUFFER in a new frame.
5504
5505Optional argument ARGS is a list specifying additional
5506information.
5507
5508If ARGS is an alist, use it as a list of frame parameters. If
382c953b
JB
5509these parameters contain (same-window . t), display BUFFER in
5510the selected window. If they contain (same-frame . t), display
f818cd2a
MR
5511BUFFER in a window of the selected frame.
5512
5513If ARGS is a list whose car is a symbol, use (car ARGS) as a
fdc2806d
CY
5514function to do the work. Pass it BUFFER as first argument, and
5515pass the elements of (cdr ARGS) as the remaining arguments."
f818cd2a
MR
5516 (if (and args (symbolp (car args)))
5517 (apply (car args) buffer (cdr args))
5518 (let ((window (get-buffer-window buffer 0)))
5519 (or
5520 ;; If we have a window already, make it visible.
5521 (when window
5522 (let ((frame (window-frame window)))
5523 (make-frame-visible frame)
5524 (raise-frame frame)
cf4eacfd 5525 (display-buffer-record-window 'reuse window buffer)
f818cd2a
MR
5526 window))
5527 ;; Reuse the current window if the user requested it.
5528 (when (cdr (assq 'same-window args))
5529 (condition-case nil
5530 (progn (switch-to-buffer buffer nil t) (selected-window))
5531 (error nil)))
5532 ;; Stay on the same frame if requested.
5533 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
5534 (let* ((pop-up-windows t)
5535 pop-up-frames
5536 special-display-buffer-names special-display-regexps)
5537 (display-buffer buffer)))
5538 ;; If no window yet, make one in a new frame.
e75852fd
MR
5539 (let* ((frame
5540 (with-current-buffer buffer
5541 (make-frame (append args special-display-frame-alist))))
5542 (window (frame-selected-window frame)))
5543 (display-buffer-record-window 'frame window buffer)
c5e28e39
MR
5544 (unless (eq buffer (window-buffer window))
5545 (set-window-buffer window buffer)
5546 (set-window-prev-buffers window nil))
e75852fd
MR
5547 (set-window-dedicated-p window t)
5548 window)))))
f818cd2a
MR
5549
5550(defcustom special-display-function 'special-display-popup-frame
5551 "Function to call for displaying special buffers.
5552This function is called with two arguments - the buffer and,
5553optionally, a list - and should return a window displaying that
5554buffer. The default value usually makes a separate frame for the
5555buffer using `special-display-frame-alist' to specify the frame
5556parameters. See the definition of `special-display-popup-frame'
5557for how to specify such a function.
5558
5559A buffer is special when its name is either listed in
5560`special-display-buffer-names' or matches a regexp in
5561`special-display-regexps'.
5562
e1c2c6f2
MR
5563The specified function should call `display-buffer-record-window'
5564with corresponding arguments to set up the quit-restore parameter
5565of the window used."
f818cd2a
MR
5566 :type 'function
5567 :group 'frames)
77f1f99c 5568(make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3")
f818cd2a
MR
5569
5570(defcustom same-window-buffer-names nil
5571 "List of names of buffers that should appear in the \"same\" window.
5572`display-buffer' and `pop-to-buffer' show a buffer whose name is
5573on this list in the selected rather than some other window.
5574
5575An element of this list can be a cons cell instead of just a
5576string. In that case, the cell's car must be a string specifying
5577the buffer name. This is for compatibility with
5578`special-display-buffer-names'; the cdr of the cons cell is
5579ignored.
5580
5581See also `same-window-regexps'."
5582 :type '(repeat (string :format "%v"))
5583 :group 'windows)
5584
5585(defcustom same-window-regexps nil
5586 "List of regexps saying which buffers should appear in the \"same\" window.
5587`display-buffer' and `pop-to-buffer' show a buffer whose name
5588matches a regexp on this list in the selected rather than some
5589other window.
5590
5591An element of this list can be a cons cell instead of just a
5592string. In that case, the cell's car must be a regexp matching
5593the buffer name. This is for compatibility with
5594`special-display-regexps'; the cdr of the cons cell is ignored.
9481c002 5595
f818cd2a
MR
5596See also `same-window-buffer-names'."
5597 :type '(repeat (regexp :format "%v"))
5598 :group 'windows)
9481c002 5599
f818cd2a
MR
5600(defun same-window-p (buffer-name)
5601 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5602This function returns non-nil if `display-buffer' or
5603`pop-to-buffer' would show a buffer named BUFFER-NAME in the
382c953b 5604selected rather than (as usual) some other window. See
f818cd2a
MR
5605`same-window-buffer-names' and `same-window-regexps'."
5606 (cond
5607 ((not (stringp buffer-name)))
5608 ;; The elements of `same-window-buffer-names' can be buffer
5609 ;; names or cons cells whose cars are buffer names.
5610 ((member buffer-name same-window-buffer-names))
5611 ((assoc buffer-name same-window-buffer-names))
5612 ((catch 'found
5613 (dolist (regexp same-window-regexps)
5614 ;; The elements of `same-window-regexps' can be regexps
5615 ;; or cons cells whose cars are regexps.
5616 (when (or (and (stringp regexp)
cb882333 5617 (string-match-p regexp buffer-name))
f818cd2a
MR
5618 (and (consp regexp) (stringp (car regexp))
5619 (string-match-p (car regexp) buffer-name)))
5620 (throw 'found t)))))))
3c448ab6 5621
d1067961 5622(defcustom pop-up-frames nil
3c448ab6 5623 "Whether `display-buffer' should make a separate frame.
d1f18ec0 5624If nil, never make a separate frame.
3c448ab6
MR
5625If the value is `graphic-only', make a separate frame
5626on graphic displays only.
5627Any other non-nil value means always make a separate frame."
5628 :type '(choice
5629 (const :tag "Never" nil)
5630 (const :tag "On graphic displays only" graphic-only)
5631 (const :tag "Always" t))
f818cd2a 5632 :group 'windows)
3c448ab6 5633
d1067961 5634(defcustom display-buffer-reuse-frames nil
f818cd2a 5635 "Non-nil means `display-buffer' should reuse frames.
3c448ab6
MR
5636If the buffer in question is already displayed in a frame, raise
5637that frame."
5638 :type 'boolean
d1067961 5639 :version "21.1"
f818cd2a 5640 :group 'windows)
24777832
CY
5641
5642(make-obsolete-variable
5643 'display-buffer-reuse-frames
5644 "use a `reusable-frames' alist entry in `display-buffer-alist'."
5645 "24.3")
3c448ab6 5646
4dc2a129
MR
5647(defcustom pop-up-windows t
5648 "Non-nil means `display-buffer' should make a new window."
3c448ab6
MR
5649 :type 'boolean
5650 :group 'windows)
5651
8b10a2d1 5652(defcustom split-window-preferred-function 'split-window-sensibly
f818cd2a 5653 "Function called by `display-buffer' routines to split a window.
8b10a2d1
MR
5654This function is called with a window as single argument and is
5655supposed to split that window and return the new window. If the
5656window can (or shall) not be split, it is supposed to return nil.
5657The default is to call the function `split-window-sensibly' which
5658tries to split the window in a way which seems most suitable.
5659You can customize the options `split-height-threshold' and/or
5660`split-width-threshold' in order to have `split-window-sensibly'
5661prefer either vertical or horizontal splitting.
5662
f818cd2a
MR
5663If you set this to any other function, bear in mind that the
5664`display-buffer' routines may call this function two times. The
5665argument of the first call is the largest window on its frame.
5666If that call fails to return a live window, the function is
5667called again with the least recently used window as argument. If
5668that call fails too, `display-buffer' will use an existing window
5669to display its buffer.
8b10a2d1
MR
5670
5671The window selected at the time `display-buffer' was invoked is
5672still selected when this function is called. Hence you can
5673compare the window argument with the value of `selected-window'
5674if you intend to split the selected window instead or if you do
5675not want to split the selected window."
5676 :type 'function
3c448ab6
MR
5677 :version "23.1"
5678 :group 'windows)
5679
8b10a2d1 5680(defcustom split-height-threshold 80
f818cd2a
MR
5681 "Minimum height for splitting windows sensibly.
5682If this is an integer, `split-window-sensibly' may split a window
8b10a2d1 5683vertically only if it has at least this many lines. If this is
f818cd2a
MR
5684nil, `split-window-sensibly' is not allowed to split a window
5685vertically. If, however, a window is the only window on its
5686frame, `split-window-sensibly' may split it vertically
5687disregarding the value of this variable."
8b10a2d1 5688 :type '(choice (const nil) (integer :tag "lines"))
3c448ab6
MR
5689 :version "23.1"
5690 :group 'windows)
5691
8b10a2d1 5692(defcustom split-width-threshold 160
f818cd2a
MR
5693 "Minimum width for splitting windows sensibly.
5694If this is an integer, `split-window-sensibly' may split a window
8b10a2d1 5695horizontally only if it has at least this many columns. If this
f818cd2a
MR
5696is nil, `split-window-sensibly' is not allowed to split a window
5697horizontally."
8b10a2d1 5698 :type '(choice (const nil) (integer :tag "columns"))
3c448ab6
MR
5699 :version "23.1"
5700 :group 'windows)
9481c002 5701
8b10a2d1
MR
5702(defun window-splittable-p (window &optional horizontal)
5703 "Return non-nil if `split-window-sensibly' may split WINDOW.
5704Optional argument HORIZONTAL nil or omitted means check whether
5705`split-window-sensibly' may split WINDOW vertically. HORIZONTAL
5706non-nil means check whether WINDOW may be split horizontally.
3c448ab6 5707
8b10a2d1 5708WINDOW may be split vertically when the following conditions
3c448ab6 5709hold:
3c448ab6
MR
5710- `window-size-fixed' is either nil or equals `width' for the
5711 buffer of WINDOW.
8b10a2d1 5712- `split-height-threshold' is an integer and WINDOW is at least as
3c448ab6 5713 high as `split-height-threshold'.
3c448ab6
MR
5714- When WINDOW is split evenly, the emanating windows are at least
5715 `window-min-height' lines tall and can accommodate at least one
5716 line plus - if WINDOW has one - a mode line.
5717
8b10a2d1 5718WINDOW may be split horizontally when the following conditions
3c448ab6 5719hold:
3c448ab6
MR
5720- `window-size-fixed' is either nil or equals `height' for the
5721 buffer of WINDOW.
8b10a2d1 5722- `split-width-threshold' is an integer and WINDOW is at least as
3c448ab6 5723 wide as `split-width-threshold'.
3c448ab6
MR
5724- When WINDOW is split evenly, the emanating windows are at least
5725 `window-min-width' or two (whichever is larger) columns wide."
5726 (when (window-live-p window)
5727 (with-current-buffer (window-buffer window)
5728 (if horizontal
5729 ;; A window can be split horizontally when its width is not
5730 ;; fixed, it is at least `split-width-threshold' columns wide
5731 ;; and at least twice as wide as `window-min-width' and 2 (the
5732 ;; latter value is hardcoded).
5733 (and (memq window-size-fixed '(nil height))
5734 ;; Testing `window-full-width-p' here hardly makes any
5735 ;; sense nowadays. This can be done more intuitively by
5736 ;; setting up `split-width-threshold' appropriately.
5737 (numberp split-width-threshold)
5738 (>= (window-width window)
5739 (max split-width-threshold
5740 (* 2 (max window-min-width 2)))))
5741 ;; A window can be split vertically when its height is not
5742 ;; fixed, it is at least `split-height-threshold' lines high,
5743 ;; and it is at least twice as high as `window-min-height' and 2
37269466 5744 ;; if it has a mode line or 1.
3c448ab6
MR
5745 (and (memq window-size-fixed '(nil width))
5746 (numberp split-height-threshold)
5747 (>= (window-height window)
5748 (max split-height-threshold
5749 (* 2 (max window-min-height
5750 (if mode-line-format 2 1))))))))))
5751
2dc2a609 5752(defun split-window-sensibly (&optional window)
8b10a2d1 5753 "Split WINDOW in a way suitable for `display-buffer'.
2dc2a609 5754WINDOW defaults to the currently selected window.
8b10a2d1
MR
5755If `split-height-threshold' specifies an integer, WINDOW is at
5756least `split-height-threshold' lines tall and can be split
5757vertically, split WINDOW into two windows one above the other and
5758return the lower window. Otherwise, if `split-width-threshold'
5759specifies an integer, WINDOW is at least `split-width-threshold'
5760columns wide and can be split horizontally, split WINDOW into two
5761windows side by side and return the window on the right. If this
5762can't be done either and WINDOW is the only window on its frame,
5763try to split WINDOW vertically disregarding any value specified
5764by `split-height-threshold'. If that succeeds, return the lower
5765window. Return nil otherwise.
5766
5767By default `display-buffer' routines call this function to split
5768the largest or least recently used window. To change the default
5769customize the option `split-window-preferred-function'.
5770
5771You can enforce this function to not split WINDOW horizontally,
382c953b 5772by setting (or binding) the variable `split-width-threshold' to
8b10a2d1
MR
5773nil. If, in addition, you set `split-height-threshold' to zero,
5774chances increase that this function does split WINDOW vertically.
5775
382c953b 5776In order to not split WINDOW vertically, set (or bind) the
8b10a2d1
MR
5777variable `split-height-threshold' to nil. Additionally, you can
5778set `split-width-threshold' to zero to make a horizontal split
5779more likely to occur.
5780
5781Have a look at the function `window-splittable-p' if you want to
5782know how `split-window-sensibly' determines whether WINDOW can be
5783split."
2dc2a609
TH
5784 (let ((window (or window (selected-window))))
5785 (or (and (window-splittable-p window)
5786 ;; Split window vertically.
5787 (with-selected-window window
5788 (split-window-below)))
5789 (and (window-splittable-p window t)
5790 ;; Split window horizontally.
5791 (with-selected-window window
5792 (split-window-right)))
5793 (and (eq window (frame-root-window (window-frame window)))
5794 (not (window-minibuffer-p window))
5795 ;; If WINDOW is the only window on its frame and is not the
5796 ;; minibuffer window, try to split it vertically disregarding
5797 ;; the value of `split-height-threshold'.
5798 (let ((split-height-threshold 0))
5799 (when (window-splittable-p window)
5800 (with-selected-window window
5801 (split-window-below))))))))
9481c002 5802
5938d519 5803(defun window--try-to-split-window (window &optional alist)
f818cd2a
MR
5804 "Try to split WINDOW.
5805Return value returned by `split-window-preferred-function' if it
5806represents a live window, nil otherwise."
5807 (and (window-live-p window)
5808 (not (frame-parameter (window-frame window) 'unsplittable))
8e17c9ba
MR
5809 (let* ((window-combination-limit
5810 ;; When `window-combination-limit' equals
5938d519
MR
5811 ;; `display-buffer' or equals `resize-window' and a
5812 ;; `window-height' or `window-width' alist entry are
5813 ;; present, bind it to t so resizing steals space
5814 ;; preferably from the window that was split.
5815 (if (or (eq window-combination-limit 'display-buffer)
5816 (and (eq window-combination-limit 'window-size)
5817 (or (cdr (assq 'window-height alist))
5818 (cdr (assq 'window-width alist)))))
8e17c9ba
MR
5819 t
5820 window-combination-limit))
5821 (new-window
5822 ;; Since `split-window-preferred-function' might
5823 ;; throw an error use `condition-case'.
5824 (condition-case nil
5825 (funcall split-window-preferred-function window)
5826 (error nil))))
f818cd2a
MR
5827 (and (window-live-p new-window) new-window))))
5828
5829(defun window--frame-usable-p (frame)
5830 "Return FRAME if it can be used to display a buffer."
5831 (when (frame-live-p frame)
5832 (let ((window (frame-root-window frame)))
5833 ;; `frame-root-window' may be an internal window which is considered
5834 ;; "dead" by `window-live-p'. Hence if `window' is not live we
5835 ;; implicitly know that `frame' has a visible window we can use.
5836 (unless (and (window-live-p window)
5837 (or (window-minibuffer-p window)
5838 ;; If the window is soft-dedicated, the frame is usable.
5839 ;; Actually, even if the window is really dedicated,
5840 ;; the frame is still usable by splitting it.
5841 ;; At least Emacs-22 allowed it, and it is desirable
5842 ;; when displaying same-frame windows.
5843 nil ; (eq t (window-dedicated-p window))
5844 ))
5845 frame))))
5846
5847(defcustom even-window-heights t
5848 "If non-nil `display-buffer' will try to even window heights.
5849Otherwise `display-buffer' will leave the window configuration
5850alone. Heights are evened only when `display-buffer' chooses a
5851window that appears above or below the selected window."
5852 :type 'boolean
5853 :group 'windows)
5854
5855(defun window--even-window-heights (window)
5856 "Even heights of WINDOW and selected window.
5857Do this only if these windows are vertically adjacent to each
5858other, `even-window-heights' is non-nil, and the selected window
5859is higher than WINDOW."
5860 (when (and even-window-heights
9aba119d
MR
5861 ;; Even iff WINDOW forms a vertical combination with the
5862 ;; selected window, and WINDOW's height exceeds that of the
5863 ;; selected window, see also bug#11880.
5864 (window-combined-p window)
5865 (= (window-child-count (window-parent window)) 2)
5866 (eq (window-parent) (window-parent window))
5867 (> (window-total-height) (window-total-height window)))
5868 ;; Don't throw an error if we can't even window heights for
5869 ;; whatever reason.
5870 (condition-case nil
5871 (enlarge-window
5872 (/ (- (window-total-height window) (window-total-height)) 2))
5873 (error nil))))
f818cd2a 5874
5938d519 5875(defun window--display-buffer (buffer window type &optional alist dedicated)
0ff7851c 5876 "Display BUFFER in WINDOW.
51a5f9d8 5877TYPE must be one of the symbols `reuse', `window' or `frame' and
0ff7851c
MR
5878is passed unaltered to `display-buffer-record-window'. ALIST is
5879the alist argument of `display-buffer'. Set `window-dedicated-p'
5880to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
5881live."
f818cd2a 5882 (when (and (buffer-live-p buffer) (window-live-p window))
caceae25 5883 (display-buffer-record-window type window buffer)
90749b53
CY
5884 (unless (eq buffer (window-buffer window))
5885 (set-window-dedicated-p window nil)
90749b53
CY
5886 (set-window-buffer window buffer)
5887 (when dedicated
5888 (set-window-dedicated-p window dedicated))
5889 (when (memq type '(window frame))
5890 (set-window-prev-buffers window nil)))
2d6b6005 5891 (let ((parameter (window-parameter window 'quit-restore))
5938d519 5892 (height (cdr (assq 'window-height alist)))
880e6158
MR
5893 (width (cdr (assq 'window-width alist)))
5894 (size (cdr (assq 'window-size alist))))
5895 (cond
5896 ((or (eq type 'frame)
5897 (and (eq (car parameter) 'same)
5898 (eq (nth 1 parameter) 'frame)))
5899 ;; Adjust size of frame if asked for.
5900 (cond
5901 ((not size))
5902 ((consp size)
5903 (let ((width (car size))
5904 (height (cdr size))
2d6b6005 5905 (frame (window-frame window)))
880e6158
MR
5906 (when (and (numberp width) (numberp height))
5907 (set-frame-height
5908 frame (+ (frame-height frame)
5909 (- height (window-total-height window))))
5910 (set-frame-width
5911 frame (+ (frame-width frame)
5912 (- width (window-total-width window)))))))
5913 ((functionp size)
5914 (ignore-errors (funcall size window)))))
5915 ((or (eq type 'window)
5916 (and (eq (car parameter) 'same)
5917 (eq (nth 1 parameter) 'window)))
0ff7851c 5918 ;; Adjust height of window if asked for.
5938d519
MR
5919 (cond
5920 ((not height))
5921 ((numberp height)
5922 (let* ((new-height
5923 (if (integerp height)
5924 height
5925 (round
880e6158 5926 (* (window-total-height (frame-root-window window))
5938d519 5927 height))))
880e6158
MR
5928 (delta (- new-height (window-total-height window))))
5929 (when (and (window--resizable-p window delta nil 'safe)
0ff7851c
MR
5930 (window-combined-p window))
5931 (window-resize window delta nil 'safe))))
5938d519
MR
5932 ((functionp height)
5933 (ignore-errors (funcall height window))))
0ff7851c 5934 ;; Adjust width of window if asked for.
5938d519
MR
5935 (cond
5936 ((not width))
5937 ((numberp width)
5938 (let* ((new-width
5939 (if (integerp width)
5940 width
5941 (round
880e6158 5942 (* (window-total-width (frame-root-window window))
5938d519 5943 width))))
880e6158
MR
5944 (delta (- new-width (window-total-width window))))
5945 (when (and (window--resizable-p window delta t 'safe)
0ff7851c
MR
5946 (window-combined-p window t))
5947 (window-resize window delta t 'safe))))
5938d519 5948 ((functionp width)
880e6158 5949 (ignore-errors (funcall width window)))))))
0ff7851c 5950
90749b53
CY
5951 window))
5952
5953(defun window--maybe-raise-frame (frame)
5954 (let ((visible (frame-visible-p frame)))
5955 (unless (or (not visible)
5956 ;; Assume the selected frame is already visible enough.
5957 (eq frame (selected-frame))
5958 ;; Assume the frame from which we invoked the
5959 ;; minibuffer is visible.
5960 (and (minibuffer-window-active-p (selected-window))
5961 (eq frame (window-frame (minibuffer-selected-window)))))
5962 (raise-frame frame))))
f818cd2a 5963
24510c22
SM
5964;; FIXME: Not implemented.
5965;; FIXME: By the way, there could be more levels of dedication:
5966;; - `barely' dedicated doesn't prevent reuse of the window, only records that
5967;; the window hasn't been used for something else yet.
5968;; - `softly' dedicated only allows reuse when asked explicitly.
5969;; - `strongly' never allows reuse.
f818cd2a
MR
5970(defvar display-buffer-mark-dedicated nil
5971 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
5972The actual non-nil value of this variable will be copied to the
5973`window-dedicated-p' flag.")
5974
fa5660f9
CY
5975(defconst display-buffer--action-function-custom-type
5976 '(choice :tag "Function"
5977 (const :tag "--" ignore) ; default for insertion
fa5660f9 5978 (const display-buffer-reuse-window)
d9558cad 5979 (const display-buffer-pop-up-window)
fa5660f9
CY
5980 (const display-buffer-same-window)
5981 (const display-buffer-pop-up-frame)
880e6158 5982 (const display-buffer-in-previous-window)
fa5660f9
CY
5983 (const display-buffer-use-some-window)
5984 (function :tag "Other function"))
5985 "Custom type for `display-buffer' action functions.")
5986
5987(defconst display-buffer--action-custom-type
5988 `(cons :tag "Action"
5989 (choice :tag "Action functions"
5990 ,display-buffer--action-function-custom-type
5991 (repeat
5992 :tag "List of functions"
5993 ,display-buffer--action-function-custom-type))
5994 (alist :tag "Action arguments"
5995 :key-type symbol
5996 :value-type (sexp :tag "Value")))
5997 "Custom type for `display-buffer' actions.")
5998
cbb0f9ab
CY
5999(defvar display-buffer-overriding-action '(nil . nil)
6000 "Overriding action to perform to display a buffer.
6001It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
382c953b 6002function or a list of functions. Each function should accept two
cbb0f9ab
CY
6003arguments: a buffer to display and an alist similar to ALIST.
6004See `display-buffer' for details.")
6005(put 'display-buffer-overriding-action 'risky-local-variable t)
6006
fa5660f9 6007(defcustom display-buffer-alist nil
89894cd8
CY
6008 "Alist of conditional actions for `display-buffer'.
6009This is a list of elements (CONDITION . ACTION), where:
6010
0ff7851c
MR
6011 CONDITION is either a regexp matching buffer names, or a
6012 function that takes two arguments - a buffer name and the
6013 ACTION argument of `display-buffer' - and returns a boolean.
89894cd8 6014
8319e0bf
CY
6015 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
6016 function or a list of functions. Each such function should
382c953b 6017 accept two arguments: a buffer to display and an alist of the
f0cfa5fe 6018 same form as ALIST. See `display-buffer' for details.
0ff7851c
MR
6019
6020`display-buffer' scans this alist until it either finds a
6021matching regular expression or the function specified by a
9139632a
JL
6022condition returns non-nil. In any of these cases, it adds the
6023associated action to the list of actions it will try."
fa5660f9
CY
6024 :type `(alist :key-type
6025 (choice :tag "Condition"
6026 regexp
6027 (function :tag "Matcher function"))
6028 :value-type ,display-buffer--action-custom-type)
6029 :risky t
6030 :version "24.1"
6031 :group 'windows)
89894cd8 6032
cbb0f9ab
CY
6033(defcustom display-buffer-base-action '(nil . nil)
6034 "User-specified default action for `display-buffer'.
8319e0bf 6035It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
382c953b 6036function or a list of functions. Each function should accept two
fa5660f9
CY
6037arguments: a buffer to display and an alist similar to ALIST.
6038See `display-buffer' for details."
6039 :type display-buffer--action-custom-type
6040 :risky t
6041 :version "24.1"
6042 :group 'windows)
89894cd8 6043
cbb0f9ab 6044(defconst display-buffer-fallback-action
0a9f9ab5 6045 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
cbb0f9ab 6046 display-buffer-reuse-window
cbb0f9ab 6047 display-buffer--maybe-pop-up-frame-or-window
880e6158 6048 display-buffer-in-previous-window
cbb0f9ab
CY
6049 display-buffer-use-some-window
6050 ;; If all else fails, pop up a new frame.
6051 display-buffer-pop-up-frame))
6052 "Default fallback action for `display-buffer'.
6053This is the action used by `display-buffer' if no other actions
6054specified, e.g. by the user options `display-buffer-alist' or
6055`display-buffer-base-action'. See `display-buffer'.")
6056(put 'display-buffer-fallback-action 'risky-local-variable t)
f818cd2a 6057
0ff7851c
MR
6058(defun display-buffer-assq-regexp (buffer-name alist action)
6059 "Retrieve ALIST entry corresponding to BUFFER-NAME.
6060ACTION is the action argument passed to `display-buffer'."
f818cd2a
MR
6061 (catch 'match
6062 (dolist (entry alist)
cb882333 6063 (let ((key (car entry)))
f818cd2a
MR
6064 (when (or (and (stringp key)
6065 (string-match-p key buffer-name))
0ff7851c
MR
6066 (and (functionp key)
6067 (funcall key buffer-name action)))
f818cd2a
MR
6068 (throw 'match (cdr entry)))))))
6069
8319e0bf
CY
6070(defvar display-buffer--same-window-action
6071 '(display-buffer-same-window
6072 (inhibit-same-window . nil))
6073 "A `display-buffer' action for displaying in the same window.")
6074(put 'display-buffer--same-window-action 'risky-local-variable t)
6075
6076(defvar display-buffer--other-frame-action
6077 '((display-buffer-reuse-window
8319e0bf
CY
6078 display-buffer-pop-up-frame)
6079 (reusable-frames . 0)
6080 (inhibit-same-window . t))
6081 "A `display-buffer' action for displaying in another frame.")
6082(put 'display-buffer--other-frame-action 'risky-local-variable t)
6083
d45ba96b 6084(defun display-buffer (buffer-or-name &optional action frame)
3199b96f 6085 "Display BUFFER-OR-NAME in some window, without selecting it.
89894cd8
CY
6086BUFFER-OR-NAME must be a buffer or the name of an existing
6087buffer. Return the window chosen for displaying BUFFER-OR-NAME,
6088or nil if no such window is found.
6089
7b9abf24
EZ
6090Optional argument ACTION, if non-nil, should specify a display
6091action. Its form is described below.
6092
6093Optional argument FRAME, if non-nil, acts like an additional
6094ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
6095specifying the frame(s) to search for a window that is already
6096displaying the buffer. See `display-buffer-reuse-window'
6097
6098If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
6099where FUNCTION is either a function or a list of functions, and
d4bd55e7
CY
6100ALIST is an arbitrary association list (alist).
6101
6102Each such FUNCTION should accept two arguments: the buffer to
f130cb76
LL
6103display and an alist. Based on those arguments, it should
6104display the buffer and return the window. If the caller is
9139632a
JL
6105prepared to handle the case of not displaying the buffer
6106and returning nil from `display-buffer' it should pass
6107\(allow-no-window . t) as an element of the ALIST.
89894cd8 6108
cbb0f9ab 6109The `display-buffer' function builds a function list and an alist
d4bd55e7
CY
6110by combining the functions and alists specified in
6111`display-buffer-overriding-action', `display-buffer-alist', the
6112ACTION argument, `display-buffer-base-action', and
6113`display-buffer-fallback-action' (in order). Then it calls each
6114function in the combined function list in turn, passing the
cbb0f9ab
CY
6115buffer as the first argument and the combined alist as the second
6116argument, until one of the functions returns non-nil.
8319e0bf 6117
7b9abf24
EZ
6118If ACTION is nil, the function list and the alist are built using
6119only the other variables mentioned above.
6120
8319e0bf
CY
6121Available action functions include:
6122 `display-buffer-same-window'
8319e0bf
CY
6123 `display-buffer-reuse-window'
6124 `display-buffer-pop-up-frame'
6125 `display-buffer-pop-up-window'
880e6158 6126 `display-buffer-in-previous-window'
8319e0bf
CY
6127 `display-buffer-use-some-window'
6128
6129Recognized alist entries include:
6130
6131 `inhibit-same-window' -- A non-nil value prevents the same
6132 window from being used for display.
6133
90749b53
CY
6134 `inhibit-switch-frame' -- A non-nil value prevents any other
6135 frame from being raised or selected,
6136 even if the window is displayed there.
6137
8319e0bf
CY
6138 `reusable-frames' -- Value specifies frame(s) to search for a
6139 window that already displays the buffer.
6140 See `display-buffer-reuse-window'.
2a7bdc1a 6141
d97af5a0
CY
6142 `pop-up-frame-parameters' -- Value specifies an alist of frame
6143 parameters to give a new frame, if
6144 one is created.
6145
df171c23
MR
6146 `window-height' -- Value specifies either an integer (the number
6147 of lines of a new window), a floating point number (the
6148 fraction of a new window with respect to the height of the
6149 frame's root window) or a function to be called with one
6150 argument - a new window. The function is supposed to adjust
6151 the height of the window; its return value is ignored.
6152 Suitable functions are `shrink-window-if-larger-than-buffer'
6153 and `fit-window-to-buffer'.
6154
6155 `window-width' -- Value specifies either an integer (the number
6156 of columns of a new window), a floating point number (the
6157 fraction of a new window with respect to the width of the
6158 frame's root window) or a function to be called with one
6159 argument - a new window. The function is supposed to adjust
6160 the width of the window; its return value is ignored.
6161
9139632a
JL
6162 `allow-no-window' -- A non-nil value indicates readiness for the case
6163 of not displaying the buffer and FUNCTION can safely return
6164 a non-window value to suppress displaying.
6165
2a7bdc1a
CY
6166The ACTION argument to `display-buffer' can also have a non-nil
6167and non-list value. This means to display the buffer in a window
6168other than the selected one, even if it is already displayed in
6169the selected window. If called interactively with a prefix
7b9abf24 6170argument, ACTION is t."
c3313451
CY
6171 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6172 (if current-prefix-arg t)))
d45ba96b
MR
6173 (let ((buffer (if (bufferp buffer-or-name)
6174 buffer-or-name
6175 (get-buffer buffer-or-name)))
f47ad11b
MR
6176 ;; Make sure that when we split windows the old window keeps
6177 ;; point, bug#14829.
6178 (split-window-keep-point t)
89894cd8
CY
6179 ;; Handle the old form of the first argument.
6180 (inhibit-same-window (and action (not (listp action)))))
6181 (unless (listp action) (setq action nil))
6182 (if display-buffer-function
6183 ;; If `display-buffer-function' is defined, let it do the job.
6184 (funcall display-buffer-function buffer inhibit-same-window)
6185 ;; Otherwise, use the defined actions.
6186 (let* ((user-action
0ff7851c
MR
6187 (display-buffer-assq-regexp
6188 (buffer-name buffer) display-buffer-alist action))
0a9f9ab5 6189 (special-action (display-buffer--special-action buffer))
89894cd8
CY
6190 ;; Extra actions from the arguments to this function:
6191 (extra-action
6192 (cons nil (append (if inhibit-same-window
6193 '((inhibit-same-window . t)))
6194 (if frame
8319e0bf 6195 `((reusable-frames . ,frame))))))
89894cd8
CY
6196 ;; Construct action function list and action alist.
6197 (actions (list display-buffer-overriding-action
0a9f9ab5 6198 user-action special-action action extra-action
cbb0f9ab
CY
6199 display-buffer-base-action
6200 display-buffer-fallback-action))
89894cd8
CY
6201 (functions (apply 'append
6202 (mapcar (lambda (x)
6203 (setq x (car x))
c3313451 6204 (if (functionp x) (list x) x))
89894cd8
CY
6205 actions)))
6206 (alist (apply 'append (mapcar 'cdr actions)))
6207 window)
6208 (unless (buffer-live-p buffer)
6209 (error "Invalid buffer"))
6210 (while (and functions (not window))
6211 (setq window (funcall (car functions) buffer alist)
6212 functions (cdr functions)))
f130cb76 6213 (and (windowp window) window)))))
f818cd2a
MR
6214
6215(defun display-buffer-other-frame (buffer)
e6f759f9 6216 "Display buffer BUFFER preferably in another frame.
f818cd2a
MR
6217This uses the function `display-buffer' as a subroutine; see
6218its documentation for additional customization information."
6219 (interactive "BDisplay buffer in other frame: ")
8319e0bf 6220 (display-buffer buffer display-buffer--other-frame-action t))
f818cd2a 6221
89894cd8 6222;;; `display-buffer' action functions:
437014c8 6223
89894cd8 6224(defun display-buffer-same-window (buffer alist)
8319e0bf
CY
6225 "Display BUFFER in the selected window.
6226This fails if ALIST has a non-nil `inhibit-same-window' entry, or
6227if the selected window is a minibuffer window or is dedicated to
6228another buffer; in that case, return nil. Otherwise, return the
6229selected window."
89894cd8
CY
6230 (unless (or (cdr (assq 'inhibit-same-window alist))
6231 (window-minibuffer-p)
6232 (window-dedicated-p))
5938d519 6233 (window--display-buffer buffer (selected-window) 'reuse alist)))
89894cd8 6234
0d3ff375 6235(defun display-buffer--maybe-same-window (buffer alist)
8319e0bf
CY
6236 "Conditionally display BUFFER in the selected window.
6237If `same-window-p' returns non-nil for BUFFER's name, call
6238`display-buffer-same-window' and return its value. Otherwise,
6239return nil."
89894cd8
CY
6240 (and (same-window-p (buffer-name buffer))
6241 (display-buffer-same-window buffer alist)))
6242
6243(defun display-buffer-reuse-window (buffer alist)
6244 "Return a window that is already displaying BUFFER.
8319e0bf
CY
6245Return nil if no usable window is found.
6246
6247If ALIST has a non-nil `inhibit-same-window' entry, the selected
6248window is not eligible for reuse.
6249
6250If ALIST contains a `reusable-frames' entry, its value determines
6251which frames to search for a reusable window:
6252 nil -- the selected frame (actually the last non-minibuffer frame)
6253 A frame -- just that frame
6254 `visible' -- all visible frames
6255 0 -- all frames on the current terminal
6256 t -- all frames.
6257
6258If ALIST contains no `reusable-frames' entry, search just the
6259selected frame if `display-buffer-reuse-frames' and
6260`pop-up-frames' are both nil; search all frames on the current
90749b53
CY
6261terminal if either of those variables is non-nil.
6262
6263If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6264event that a window on another frame is chosen, avoid raising
6265that frame."
8319e0bf
CY
6266 (let* ((alist-entry (assq 'reusable-frames alist))
6267 (frames (cond (alist-entry (cdr alist-entry))
6268 ((if (eq pop-up-frames 'graphic-only)
6269 (display-graphic-p)
6270 pop-up-frames)
6271 0)
6272 (display-buffer-reuse-frames 0)
6273 (t (last-nonminibuffer-frame))))
6274 (window (if (and (eq buffer (window-buffer))
6275 (not (cdr (assq 'inhibit-same-window alist))))
6276 (selected-window)
6277 (car (delq (selected-window)
6278 (get-buffer-window-list buffer 'nomini
6279 frames))))))
90749b53 6280 (when (window-live-p window)
5938d519 6281 (prog1 (window--display-buffer buffer window 'reuse alist)
90749b53
CY
6282 (unless (cdr (assq 'inhibit-switch-frame alist))
6283 (window--maybe-raise-frame (window-frame window)))))))
89894cd8 6284
0a9f9ab5 6285(defun display-buffer--special-action (buffer)
4ad3bc2a
CY
6286 "Return special display action for BUFFER, if any.
6287If `special-display-p' returns non-nil for BUFFER, return an
6288appropriate display action involving `special-display-function'.
6289See `display-buffer' for the format of display actions."
8319e0bf
CY
6290 (and special-display-function
6291 ;; `special-display-p' returns either t or a list of frame
6292 ;; parameters to pass to `special-display-function'.
6293 (let ((pars (special-display-p (buffer-name buffer))))
6294 (when pars
0a9f9ab5
SM
6295 (list (list #'display-buffer-reuse-window
6296 `(lambda (buffer _alist)
6297 (funcall special-display-function
6298 buffer ',(if (listp pars) pars)))))))))
8319e0bf 6299
90749b53 6300(defun display-buffer-pop-up-frame (buffer alist)
89894cd8 6301 "Display BUFFER in a new frame.
8319e0bf 6302This works by calling `pop-up-frame-function'. If successful,
90749b53
CY
6303return the window used; otherwise return nil.
6304
6305If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
d97af5a0
CY
6306raising the new frame.
6307
6308If ALIST has a non-nil `pop-up-frame-parameters' entry, the
6309corresponding value is an alist of frame parameters to give the
6310new frame."
6311 (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
6312 (pop-up-frame-alist (append params pop-up-frame-alist))
6313 (fun pop-up-frame-function)
6314 frame window)
89894cd8 6315 (when (and fun
7fd5f65e
MR
6316 ;; Make BUFFER current so `make-frame' will use it as the
6317 ;; new frame's buffer (Bug#15133).
6318 (with-current-buffer buffer
6319 (setq frame (funcall fun)))
89894cd8 6320 (setq window (frame-selected-window frame)))
5938d519
MR
6321 (prog1 (window--display-buffer
6322 buffer window 'frame alist display-buffer-mark-dedicated)
90749b53
CY
6323 (unless (cdr (assq 'inhibit-switch-frame alist))
6324 (window--maybe-raise-frame frame))))))
89894cd8 6325
90749b53 6326(defun display-buffer-pop-up-window (buffer alist)
89894cd8
CY
6327 "Display BUFFER by popping up a new window.
6328The new window is created on the selected frame, or in
6329`last-nonminibuffer-frame' if no windows can be created there.
90749b53
CY
6330If successful, return the new window; otherwise return nil.
6331
6332If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6333event that the new window is created on another frame, avoid
6334raising the frame."
89894cd8
CY
6335 (let ((frame (or (window--frame-usable-p (selected-frame))
6336 (window--frame-usable-p (last-nonminibuffer-frame))))
6337 window)
6338 (when (and (or (not (frame-parameter frame 'unsplittable))
6339 ;; If the selected frame cannot be split, look at
6340 ;; `last-nonminibuffer-frame'.
6341 (and (eq frame (selected-frame))
6342 (setq frame (last-nonminibuffer-frame))
6343 (window--frame-usable-p frame)
6344 (not (frame-parameter frame 'unsplittable))))
6345 ;; Attempt to split largest or least recently used window.
6346 (setq window (or (window--try-to-split-window
5938d519 6347 (get-largest-window frame t) alist)
89894cd8 6348 (window--try-to-split-window
5938d519
MR
6349 (get-lru-window frame t) alist))))
6350 (prog1 (window--display-buffer
6351 buffer window 'window alist display-buffer-mark-dedicated)
90749b53
CY
6352 (unless (cdr (assq 'inhibit-switch-frame alist))
6353 (window--maybe-raise-frame (window-frame window)))))))
89894cd8 6354
8319e0bf
CY
6355(defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
6356 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
6357
6358If `pop-up-frames' is non-nil (and not `graphic-only' on a
6359text-only terminal), try with `display-buffer-pop-up-frame'.
6360
6361If that cannot be done, and `pop-up-windows' is non-nil, try
6362again with `display-buffer-pop-up-window'."
6363 (or (and (if (eq pop-up-frames 'graphic-only)
6364 (display-graphic-p)
6365 pop-up-frames)
6366 (display-buffer-pop-up-frame buffer alist))
6367 (and pop-up-windows
6368 (display-buffer-pop-up-window buffer alist))))
89894cd8 6369
5938d519 6370(defun display-buffer-below-selected (buffer alist)
78dd6ab1
MR
6371 "Try displaying BUFFER in a window below the selected window.
6372This either splits the selected window or reuses the window below
6373the selected one."
6374 (let (window)
6375 (or (and (not (frame-parameter nil 'unsplittable))
d144ef06
MR
6376 (let ((split-height-threshold 0)
6377 split-width-threshold)
f93cc74f 6378 (setq window (window--try-to-split-window (selected-window) alist)))
78dd6ab1 6379 (window--display-buffer
5938d519 6380 buffer window 'window alist display-buffer-mark-dedicated))
78dd6ab1
MR
6381 (and (setq window (window-in-direction 'below))
6382 (not (window-dedicated-p window))
6383 (window--display-buffer
5938d519 6384 buffer window 'reuse alist display-buffer-mark-dedicated)))))
8e17c9ba 6385
5938d519 6386(defun display-buffer-at-bottom (buffer alist)
735135f9 6387 "Try displaying BUFFER in a window at the bottom of the selected frame.
8e17c9ba
MR
6388This either splits the window at the bottom of the frame or the
6389frame's root window, or reuses an existing window at the bottom
6390of the selected frame."
6391 (let (bottom-window window)
c660a885
MR
6392 (walk-window-tree
6393 (lambda (window) (setq bottom-window window)) nil nil 'nomini)
8e17c9ba 6394 (or (and (not (frame-parameter nil 'unsplittable))
00139435
MR
6395 (let (split-width-threshold)
6396 (setq window (window--try-to-split-window bottom-window alist)))
8e17c9ba 6397 (window--display-buffer
5938d519 6398 buffer window 'window alist display-buffer-mark-dedicated))
8e17c9ba
MR
6399 (and (not (frame-parameter nil 'unsplittable))
6400 (setq window
6401 (condition-case nil
c660a885 6402 (split-window (window--major-non-side-window))
8e17c9ba
MR
6403 (error nil)))
6404 (window--display-buffer
5938d519 6405 buffer window 'window alist display-buffer-mark-dedicated))
8e17c9ba
MR
6406 (and (setq window bottom-window)
6407 (not (window-dedicated-p window))
6408 (window--display-buffer
5938d519 6409 buffer window 'reuse alist display-buffer-mark-dedicated)))))
78dd6ab1 6410
fa2bcf43
MR
6411(defun display-buffer-in-previous-window (buffer alist)
6412 "Display BUFFER in a window previously showing it.
6413If ALIST has a non-nil `inhibit-same-window' entry, the selected
6414window is not eligible for reuse.
6415
6416If ALIST contains a `reusable-frames' entry, its value determines
6417which frames to search for a reusable window:
6418 nil -- the selected frame (actually the last non-minibuffer frame)
6419 A frame -- just that frame
6420 `visible' -- all visible frames
6421 0 -- all frames on the current terminal
6422 t -- all frames.
6423
6424If ALIST contains no `reusable-frames' entry, search just the
6425selected frame if `display-buffer-reuse-frames' and
6426`pop-up-frames' are both nil; search all frames on the current
6427terminal if either of those variables is non-nil.
6428
6429If ALIST has a `previous-window' entry, the window specified by
6430that entry will override any other window found by the methods
6431above, even if that window never showed BUFFER before."
6432 (let* ((alist-entry (assq 'reusable-frames alist))
6433 (inhibit-same-window
6434 (cdr (assq 'inhibit-same-window alist)))
6435 (frames (cond
6436 (alist-entry (cdr alist-entry))
6437 ((if (eq pop-up-frames 'graphic-only)
6438 (display-graphic-p)
6439 pop-up-frames)
6440 0)
6441 (display-buffer-reuse-frames 0)
6442 (t (last-nonminibuffer-frame))))
9d3aa82c 6443 best-window second-best-window window)
fa2bcf43
MR
6444 ;; Scan windows whether they have shown the buffer recently.
6445 (catch 'best
6446 (dolist (window (window-list-1 (frame-first-window) 'nomini frames))
6447 (when (and (assq buffer (window-prev-buffers window))
6448 (not (window-dedicated-p window)))
6449 (if (eq window (selected-window))
6450 (unless inhibit-same-window
6451 (setq second-best-window window))
6452 (setq best-window window)
6453 (throw 'best t)))))
6454 ;; When ALIST has a `previous-window' entry, that entry may override
6455 ;; anything we found so far.
6456 (when (and (setq window (cdr (assq 'previous-window alist)))
6457 (window-live-p window)
6458 (not (window-dedicated-p window)))
6459 (if (eq window (selected-window))
6460 (unless inhibit-same-window
6461 (setq second-best-window window))
6462 (setq best-window window)))
6463 ;; Return best or second best window found.
6464 (when (setq window (or best-window second-best-window))
5938d519 6465 (window--display-buffer buffer window 'reuse alist))))
fa2bcf43 6466
89894cd8
CY
6467(defun display-buffer-use-some-window (buffer alist)
6468 "Display BUFFER in an existing window.
6469Search for a usable window, set that window to the buffer, and
90749b53
CY
6470return the window. If no suitable window is found, return nil.
6471
6472If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6473event that a window in another frame is chosen, avoid raising
6474that frame."
89894cd8 6475 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
89894cd8
CY
6476 (frame (or (window--frame-usable-p (selected-frame))
6477 (window--frame-usable-p (last-nonminibuffer-frame))))
51a5f9d8
MR
6478 (window
6479 ;; Reuse an existing window.
6480 (or (get-lru-window frame nil not-this-window)
6481 (let ((window (get-buffer-window buffer 'visible)))
6482 (unless (and not-this-window
6483 (eq window (selected-window)))
6484 window))
6485 (get-largest-window 'visible nil not-this-window)
6486 (let ((window (get-buffer-window buffer 0)))
6487 (unless (and not-this-window
6488 (eq window (selected-window)))
6489 window))
880e6158
MR
6490 (get-largest-window 0 nil not-this-window)))
6491 (quit-restore (and (window-live-p window)
6492 (window-parameter window 'quit-restore)))
6493 (quad (nth 1 quit-restore)))
90749b53 6494 (when (window-live-p window)
880e6158
MR
6495 ;; If the window was used by `display-buffer' before, try to
6496 ;; resize it to its old height but don't signal an error.
6497 (when (and (listp quad)
6498 (integerp (nth 3 quad))
6499 (/= (nth 3 quad) (window-total-height window)))
6500 (condition-case nil
6501 (window-resize window (- (nth 3 quad) (window-total-height window)))
6502 (error nil)))
6503
9aba119d 6504 (prog1
5938d519 6505 (window--display-buffer buffer window 'reuse alist)
9aba119d 6506 (window--even-window-heights window)
90749b53
CY
6507 (unless (cdr (assq 'inhibit-switch-frame alist))
6508 (window--maybe-raise-frame (window-frame window)))))))
437014c8 6509
2d6b6005 6510(defun display-buffer-no-window (_buffer alist)
9139632a
JL
6511 "Display BUFFER in no window.
6512If ALIST has a non-nil `allow-no-window' entry, then don't display
6513a window at all. This makes possible to override the default action
6514and avoid displaying the buffer. It is assumed that when the caller
6515specifies a non-nil `allow-no-window' then it can handle a nil value
6516returned from `display-buffer' in this case."
6517 (when (cdr (assq 'allow-no-window alist))
6518 'fail))
6519
437014c8 6520;;; Display + selection commands:
c3313451
CY
6521(defun pop-to-buffer (buffer &optional action norecord)
6522 "Select buffer BUFFER in some window, preferably a different one.
6523BUFFER may be a buffer, a string (a buffer name), or nil. If it
6524is a string not naming an existent buffer, create a buffer with
6525that name. If BUFFER is nil, choose some other buffer. Return
6526the buffer.
6527
6528This uses `display-buffer' as a subroutine. The optional ACTION
6529argument is passed to `display-buffer' as its ACTION argument.
6530See `display-buffer' for more information. ACTION is t if called
6531interactively with a prefix argument, which means to pop to a
6532window other than the selected one even if the buffer is already
6533displayed in the selected window.
6534
6535If the window to show BUFFER is not on the selected
f818cd2a
MR
6536frame, raise that window's frame and give it input focus.
6537
f818cd2a
MR
6538Optional third arg NORECORD non-nil means do not put this buffer
6539at the front of the list of recently selected ones."
c3313451
CY
6540 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
6541 (if current-prefix-arg t)))
8319e0bf 6542 (setq buffer (window-normalize-buffer-to-switch-to buffer))
c660a885
MR
6543 ;; This should be done by `select-window' below.
6544 ;; (set-buffer buffer)
cb882333 6545 (let* ((old-frame (selected-frame))
8319e0bf 6546 (window (display-buffer buffer action))
c3313451 6547 (frame (window-frame window)))
97adfb97
CY
6548 ;; If we chose another frame, make sure it gets input focus.
6549 (unless (eq frame old-frame)
c3313451 6550 (select-frame-set-input-focus frame norecord))
97adfb97
CY
6551 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
6552 (select-window window norecord)
c3313451 6553 buffer))
f818cd2a 6554
72258fe5
CY
6555(defun pop-to-buffer-same-window (buffer &optional norecord)
6556 "Select buffer BUFFER in some window, preferably the same one.
72258fe5
CY
6557BUFFER may be a buffer, a string (a buffer name), or nil. If it
6558is a string not naming an existent buffer, create a buffer with
6559that name. If BUFFER is nil, choose some other buffer. Return
6560the buffer.
6561
6c8413fc
MR
6562Optional argument NORECORD, if non-nil means do not put this
6563buffer at the front of the list of recently selected ones.
6564
6565Unlike `pop-to-buffer', this function prefers using the selected
6566window over popping up a new window or frame."
24510c22 6567 (pop-to-buffer buffer display-buffer--same-window-action norecord))
72258fe5 6568
f818cd2a
MR
6569(defun read-buffer-to-switch (prompt)
6570 "Read the name of a buffer to switch to, prompting with PROMPT.
e1dbe924 6571Return the name of the buffer as a string.
f818cd2a
MR
6572
6573This function is intended for the `switch-to-buffer' family of
6574commands since these need to omit the name of the current buffer
6575from the list of completions and default values."
6576 (let ((rbts-completion-table (internal-complete-buffer-except)))
6577 (minibuffer-with-setup-hook
6578 (lambda ()
6579 (setq minibuffer-completion-table rbts-completion-table)
6580 ;; Since rbts-completion-table is built dynamically, we
6581 ;; can't just add it to the default value of
6582 ;; icomplete-with-completion-tables, so we add it
6583 ;; here manually.
6584 (if (and (boundp 'icomplete-with-completion-tables)
6585 (listp icomplete-with-completion-tables))
6586 (set (make-local-variable 'icomplete-with-completion-tables)
6587 (cons rbts-completion-table
6588 icomplete-with-completion-tables))))
6589 (read-buffer prompt (other-buffer (current-buffer))
6590 (confirm-nonexistent-file-or-buffer)))))
6591
6592(defun window-normalize-buffer-to-switch-to (buffer-or-name)
6593 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
6594If BUFFER-OR-NAME is nil, return the buffer returned by
6595`other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
6596exists, return that buffer. If no such buffer exists, create a
6597buffer with the name BUFFER-OR-NAME and return that buffer."
6598 (if buffer-or-name
6599 (or (get-buffer buffer-or-name)
6600 (let ((buffer (get-buffer-create buffer-or-name)))
6601 (set-buffer-major-mode buffer)
6602 buffer))
6603 (other-buffer)))
6604
9d7f027b
MR
6605(defcustom switch-to-buffer-preserve-window-point nil
6606 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
6607If this is nil, `switch-to-buffer' displays the buffer at that
6608buffer's `point'. If this is `already-displayed', it tries to
791ef5f8 6609display the buffer at its previous position in the selected
9d7f027b
MR
6610window, provided the buffer is currently displayed in some other
6611window on any visible or iconified frame. If this is t, it
6612unconditionally tries to display the buffer at its previous
6613position in the selected window.
6614
43bcfda6
MR
6615This variable is ignored if the buffer is already displayed in
6616the selected window or never appeared in it before, or if
9d7f027b
MR
6617`switch-to-buffer' calls `pop-to-buffer' to display the buffer."
6618 :type '(choice
6619 (const :tag "Never" nil)
6620 (const :tag "If already displayed elsewhere" already-displayed)
6621 (const :tag "Always" t))
6622 :group 'windows
6623 :version "24.3")
6624
f818cd2a 6625(defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
d1c0cddf
SM
6626 "Display buffer BUFFER-OR-NAME in the selected window.
6627
6628WARNING: This is NOT the way to work on another buffer temporarily
6629within a Lisp program! Use `set-buffer' instead. That avoids
6630messing with the window-buffer correspondences.
6631
cee2e90d
CY
6632If the selected window cannot display the specified
6633buffer (e.g. if it is a minibuffer window or strongly dedicated
6634to another buffer), call `pop-to-buffer' to select the buffer in
6635another window.
6636
6637If called interactively, read the buffer name using the
f818cd2a
MR
6638minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6639determines whether to request confirmation before creating a new
6640buffer.
6641
cee2e90d
CY
6642BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
6643If BUFFER-OR-NAME is a string that does not identify an existing
6644buffer, create a buffer with that name. If BUFFER-OR-NAME is
6645nil, switch to the buffer returned by `other-buffer'.
6646
6647If optional argument NORECORD is non-nil, do not put the buffer
6648at the front of the buffer list, and do not make the window
6649displaying it the most recently selected one.
6650
6651If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
6652must be displayed in the selected window; if that is impossible,
6653signal an error rather than calling `pop-to-buffer'.
f818cd2a 6654
9d7f027b
MR
6655The option `switch-to-buffer-preserve-window-point' can be used
6656to make the buffer appear at its last position in the selected
6657window.
6658
f818cd2a
MR
6659Return the buffer switched to."
6660 (interactive
acc825c5 6661 (list (read-buffer-to-switch "Switch to buffer: ") nil 'force-same-window))
f818cd2a 6662 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
24510c22
SM
6663 (cond
6664 ;; Don't call set-window-buffer if it's not needed since it
6665 ;; might signal an error (e.g. if the window is dedicated).
6666 ((eq buffer (window-buffer)))
6667 ((window-minibuffer-p)
6668 (if force-same-window
71873e2b 6669 (user-error "Cannot switch buffers in minibuffer window")
24510c22
SM
6670 (pop-to-buffer buffer norecord)))
6671 ((eq (window-dedicated-p) t)
6672 (if force-same-window
71873e2b 6673 (user-error "Cannot switch buffers in a dedicated window")
24510c22 6674 (pop-to-buffer buffer norecord)))
9d7f027b
MR
6675 (t
6676 (let* ((entry (assq buffer (window-prev-buffers)))
6677 (displayed (and (eq switch-to-buffer-preserve-window-point
6678 'already-displayed)
6679 (get-buffer-window buffer 0))))
6680 (set-window-buffer nil buffer)
6681 (when (and entry
6682 (or (eq switch-to-buffer-preserve-window-point t)
6683 displayed))
6684 ;; Try to restore start and point of buffer in the selected
6685 ;; window (Bug#4041).
6686 (set-window-start (selected-window) (nth 1 entry) t)
6687 (set-window-point nil (nth 2 entry))))))
24510c22
SM
6688
6689 (unless norecord
6690 (select-window (selected-window)))
6691 (set-buffer buffer)))
f818cd2a
MR
6692
6693(defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
6694 "Select the buffer specified by BUFFER-OR-NAME in another window.
382c953b 6695BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
f818cd2a
MR
6696nil. Return the buffer switched to.
6697
6698If called interactively, prompt for the buffer name using the
6699minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6700determines whether to request confirmation before creating a new
6701buffer.
6702
6703If BUFFER-OR-NAME is a string and does not identify an existing
6704buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6705nil, switch to the buffer returned by `other-buffer'.
6706
6707Optional second argument NORECORD non-nil means do not put this
6708buffer at the front of the list of recently selected ones.
6709
6710This uses the function `display-buffer' as a subroutine; see its
6711documentation for additional customization information."
6712 (interactive
6713 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
8319e0bf
CY
6714 (let ((pop-up-windows t))
6715 (pop-to-buffer buffer-or-name t norecord)))
f818cd2a
MR
6716
6717(defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
6718 "Switch to buffer BUFFER-OR-NAME in another frame.
382c953b 6719BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
f818cd2a
MR
6720nil. Return the buffer switched to.
6721
6722If called interactively, prompt for the buffer name using the
6723minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6724determines whether to request confirmation before creating a new
6725buffer.
6726
6727If BUFFER-OR-NAME is a string and does not identify an existing
6728buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6729nil, switch to the buffer returned by `other-buffer'.
6730
6731Optional second arg NORECORD non-nil means do not put this
6732buffer at the front of the list of recently selected ones.
6733
6734This uses the function `display-buffer' as a subroutine; see its
6735documentation for additional customization information."
6736 (interactive
6737 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
8319e0bf 6738 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
3c448ab6
MR
6739\f
6740(defun set-window-text-height (window height)
9992ea0c 6741 "Set the height in lines of the text display area of WINDOW to HEIGHT.
85c2386b
MR
6742WINDOW must be a live window and defaults to the selected one.
6743HEIGHT doesn't include the mode line or header line, if any, or
6744any partial-height lines in the text display area.
3c448ab6
MR
6745
6746Note that the current implementation of this function cannot
6747always set the height exactly, but attempts to be conservative,
6748by allocating more lines than are actually needed in the case
6749where some error may be present."
447f16b8 6750 (setq window (window-normalize-window window t))
3c448ab6
MR
6751 (let ((delta (- height (window-text-height window))))
6752 (unless (zerop delta)
6753 ;; Setting window-min-height to a value like 1 can lead to very
6754 ;; bizarre displays because it also allows Emacs to make *other*
37269466
CY
6755 ;; windows one line tall, which means that there's no more space
6756 ;; for the mode line.
6757 (let ((window-min-height (min 2 height)))
d615d6d2 6758 (window-resize window delta)))))
3c448ab6 6759
6198ccd0
MR
6760(defun enlarge-window-horizontally (delta)
6761 "Make selected window DELTA columns wider.
3c448ab6
MR
6762Interactively, if no argument is given, make selected window one
6763column wider."
6764 (interactive "p")
6198ccd0 6765 (enlarge-window delta t))
3c448ab6 6766
6198ccd0
MR
6767(defun shrink-window-horizontally (delta)
6768 "Make selected window DELTA columns narrower.
3c448ab6
MR
6769Interactively, if no argument is given, make selected window one
6770column narrower."
6771 (interactive "p")
6198ccd0 6772 (shrink-window delta t))
3c448ab6
MR
6773
6774(defun count-screen-lines (&optional beg end count-final-newline window)
6775 "Return the number of screen lines in the region.
6776The number of screen lines may be different from the number of actual lines,
6777due to line breaking, display table, etc.
6778
6779Optional arguments BEG and END default to `point-min' and `point-max'
6780respectively.
6781
6782If region ends with a newline, ignore it unless optional third argument
6783COUNT-FINAL-NEWLINE is non-nil.
6784
6785The optional fourth argument WINDOW specifies the window used for obtaining
6786parameters such as width, horizontal scrolling, and so on. The default is
6787to use the selected window's parameters.
6788
6789Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6790regardless of which buffer is displayed in WINDOW. This makes possible to use
6791`count-screen-lines' in any buffer, whether or not it is currently displayed
6792in some window."
6793 (unless beg
6794 (setq beg (point-min)))
6795 (unless end
6796 (setq end (point-max)))
6797 (if (= beg end)
6798 0
6799 (save-excursion
6800 (save-restriction
6801 (widen)
6802 (narrow-to-region (min beg end)
6803 (if (and (not count-final-newline)
6804 (= ?\n (char-before (max beg end))))
6805 (1- (max beg end))
6806 (max beg end)))
6807 (goto-char (point-min))
6808 (1+ (vertical-motion (buffer-size) window))))))
6809
6198ccd0 6810(defun window-buffer-height (window)
85c2386b
MR
6811 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
6812WINDOW must be a live window and defaults to the selected one."
6813 (setq window (window-normalize-window window t))
6198ccd0
MR
6814 (with-current-buffer (window-buffer window)
6815 (max 1
6816 (count-screen-lines (point-min) (point-max)
6817 ;; If buffer ends with a newline, ignore it when
6818 ;; counting height unless point is after it.
6819 (eobp)
6820 window))))
6821
880e6158
MR
6822;;; Resizing windows and frames to fit their contents exactly.
6823(defcustom fit-window-to-buffer-horizontally nil
6824 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
6825If this is nil, `fit-window-to-buffer' never resizes windows
6826horizontally. If this is `only', it can resize windows
6827horizontally only. Any other value means `fit-window-to-buffer'
6828can resize windows in both dimensions."
6829 :type 'boolean
6830 :version "24.4"
6831 :group 'help)
6832
6833;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
6834;; counting title bar and outer borders.
5938d519 6835(defcustom fit-frame-to-buffer nil
cac0a1d6 6836 "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
880e6158
MR
6837A frame is fit if and only if its root window is a live window
6838and this option is non-nil. If this is `horizontally', frames
6839are resized horizontally only. If this is `vertically', frames
6840are resized vertically only. Any other non-nil value means
cac0a1d6 6841frames can be resized in both dimensions."
5938d519 6842 :type 'boolean
880e6158 6843 :version "24.4"
5938d519
MR
6844 :group 'help)
6845
880e6158
MR
6846(defcustom fit-frame-to-buffer-margins '(nil nil nil nil)
6847 "Margins around frame for `fit-frame-to-buffer'.
1920914a
PE
6848This specifies the numbers of pixels to be left free on the left,
6849above, on the right, and below a frame fitted to its buffer. Set
6850this to avoid obscuring other desktop objects like the taskbar.
6851The default is nil for each side, which means to not add margins.
cac0a1d6
MR
6852
6853The value specified here can be overridden for a specific frame
6854by that frame's `fit-frame-to-buffer-margins' parameter, if
6855present. See also `fit-frame-to-buffer-sizes'."
880e6158
MR
6856 :version "24.4"
6857 :type '(list
6858 (choice
6859 :tag "Left"
6860 :value nil
6861 :format "%[LeftMargin%] %v "
6862 (const :tag "None" :format "%t" nil)
6863 (integer :tag "Pixels" :size 5))
6864 (choice
6865 :tag "Top"
6866 :value nil
6867 :format "%[TopMargin%] %v "
6868 (const :tag "None" :format "%t" nil)
6869 (integer :tag "Pixels" :size 5))
6870 (choice
6871 :tag "Right"
6872 :value nil
6873 :format "%[RightMargin%] %v "
6874 (const :tag "None" :format "%t" nil)
6875 (integer :tag "Pixels" :size 5))
6876 (choice
6877 :tag "Bottom"
6878 :value nil
6879 :format "%[BottomMargin%] %v "
6880 (const :tag "None" :format "%t" nil)
6881 (integer :tag "Pixels" :size 5)))
6882 :group 'help)
6883
6884(defcustom fit-frame-to-buffer-sizes '(nil nil nil nil)
6885 "Size boundaries of frame for `fit-frame-to-buffer'.
6886This list specifies the total maximum and minimum lines and
6887maximum and minimum columns of the root window of any frame that
6888shall be fit to its buffer. If any of these values is non-nil,
6889it overrides the corresponding argument of `fit-frame-to-buffer'.
6890
6891On window systems where the menubar can wrap, fitting a frame to
6892its buffer may swallow the last line(s). Specifying an
6893appropriate minimum width value here can avoid such wrapping.
6894
6895See also `fit-frame-to-buffer-margins'."
6896 :version "24.4"
6897 :type '(list
6898 (choice
6899 :tag "Maximum Height"
6900 :value nil
6901 :format "%[MaxHeight%] %v "
6902 (const :tag "None" :format "%t" nil)
6903 (integer :tag "Lines" :size 5))
6904 (choice
6905 :tag "Minimum Height"
6906 :value nil
6907 :format "%[MinHeight%] %v "
6908 (const :tag "None" :format "%t" nil)
6909 (integer :tag "Lines" :size 5))
6910 (choice
6911 :tag "Maximum Width"
6912 :value nil
6913 :format "%[MaxWidth%] %v "
6914 (const :tag "None" :format "%t" nil)
6915 (integer :tag "Columns" :size 5))
6916 (choice
6917 :tag "Minimum Width"
6918 :value nil
6919 :format "%[MinWidth%] %v\n"
6920 (const :tag "None" :format "%t" nil)
6921 (integer :tag "Columns" :size 5)))
6922 :group 'help)
5938d519 6923
e740f9d2 6924(declare-function x-display-pixel-height "xfns.c" (&optional terminal))
e740f9d2 6925
880e6158
MR
6926(defun window--sanitize-margin (margin left right)
6927 "Return MARGIN if it's a number between LEFT and RIGHT."
6928 (when (and (numberp margin)
6929 (<= left (- right margin)) (<= margin right))
6930 margin))
5938d519 6931
cac0a1d6 6932(defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only)
880e6158
MR
6933 "Adjust size of FRAME to display the contents of its buffer exactly.
6934FRAME can be any live frame and defaults to the selected one.
6935Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
6936MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
25a700d0
MR
6937FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
6938`window-min-height' and `window-min-width' respectively.
880e6158 6939
cac0a1d6
MR
6940If the optional argument ONLY is `vertically', resize the frame
6941vertically only. If ONLY is `horizontally', resize the frame
6942horizontally only.
6943
6944The new position and size of FRAME can be additionally determined
6945by customizing the options `fit-frame-to-buffer-sizes' and
880e6158
MR
6946`fit-frame-to-buffer-margins' or the corresponding parameters of
6947FRAME."
5938d519 6948 (interactive)
25a700d0
MR
6949 (unless (and (fboundp 'x-display-pixel-height)
6950 ;; We need the respective sizes now.
6951 (fboundp 'display-monitor-attributes-list))
6952 (user-error "Cannot resize frame in non-graphic Emacs"))
5938d519 6953 (setq frame (window-normalize-frame frame))
cac0a1d6 6954 (when (window-live-p (frame-root-window frame))
880e6158
MR
6955 (with-selected-window (frame-root-window frame)
6956 (let* ((window (frame-root-window frame))
6957 (char-width (frame-char-width))
6958 (char-height (frame-char-height))
6959 (monitor-attributes (car (display-monitor-attributes-list
6960 (frame-parameter frame 'display))))
6961 (geometry (cdr (assq 'geometry monitor-attributes)))
6962 (display-width (- (nth 2 geometry) (nth 0 geometry)))
6963 (display-height (- (nth 3 geometry) (nth 1 geometry)))
6964 (workarea (cdr (assq 'workarea monitor-attributes)))
6965 ;; Handle margins.
6966 (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins)
6967 fit-frame-to-buffer-margins))
1dcb169c
MR
6968 (left-margin (if (nth 0 margins)
6969 (or (window--sanitize-margin
6970 (nth 0 margins) 0 display-width)
6971 0)
6972 (nth 0 workarea)))
6973 (top-margin (if (nth 1 margins)
6974 (or (window--sanitize-margin
6975 (nth 1 margins) 0 display-height)
6976 0)
6977 (nth 1 workarea)))
880e6158 6978 (workarea-width (nth 2 workarea))
1dcb169c
MR
6979 (right-margin (if (nth 2 margins)
6980 (- display-width
6981 (or (window--sanitize-margin
6982 (nth 2 margins) left-margin display-width)
6983 0))
6984 (nth 2 workarea)))
880e6158 6985 (workarea-height (nth 3 workarea))
1dcb169c
MR
6986 (bottom-margin (if (nth 3 margins)
6987 (- display-height
6988 (or (window--sanitize-margin
6989 (nth 3 margins) top-margin display-height)
6990 0))
6991 (nth 3 workarea)))
880e6158
MR
6992 ;; The pixel width of FRAME (which does not include the
6993 ;; window manager's decorations).
6994 (frame-width (frame-pixel-width))
6995 ;; The pixel width of the body of FRAME's root window.
6996 (window-body-width (window-body-width nil t))
6997 ;; The difference in pixels between total and body width of
6998 ;; FRAME's window.
6999 (window-extra-width (- (window-pixel-width) window-body-width))
7000 ;; The difference in pixels between the frame's pixel width
7001 ;; and the window's body width. This is the space we can't
7002 ;; use for fitting.
7003 (extra-width (- frame-width window-body-width))
7004 ;; The maximum width we can use for fitting.
7005 (fit-width (- workarea-width extra-width))
7006 ;; The pixel position of FRAME's left border. We usually
7007 ;; try to leave this alone.
7008 (left
7009 (let ((left (frame-parameter nil 'left)))
7010 (if (consp left)
7011 (funcall (car left) (cadr left))
7012 left)))
7013 ;; The pixel height of FRAME (which does not include title
7014 ;; line, decorations, and sometimes neither the menu nor
7015 ;; the toolbar).
7016 (frame-height (frame-pixel-height))
7017 ;; The pixel height of FRAME's root window (we don't care
7018 ;; about the window's body height since the return value of
7019 ;; `window-text-pixel-size' includes header and mode line).
7020 (window-height (window-pixel-height))
7021 ;; The difference in pixels between the frame's pixel
7022 ;; height and the window's height.
7023 (extra-height (- frame-height window-height))
7024 ;; When tool-bar-mode is enabled and we just created a new
7025 ;; frame, reserve lines for toolbar resizing. Needed
7026 ;; because for reasons unknown to me Emacs (1) reserves one
7027 ;; line for the toolbar when making the initial frame and
7028 ;; toolbars are enabled, and (2) later adds the remaining
7029 ;; lines needed. Our code runs IN BETWEEN (1) and (2).
7030 ;; YMMV when you're on a system that behaves differently.
7031 (toolbar-extra-height
7032 (let ((quit-restore (window-parameter window 'quit-restore))
7033 ;; This may have to change when we allow arbitrary
7034 ;; pixel height toolbars.
7035 (lines (tool-bar-height)))
7036 (* char-height
7037 (if (and quit-restore (eq (car quit-restore) 'frame)
7038 (not (zerop lines)))
7039 (1- lines)
7040 0))))
1dcb169c 7041 ;; The pixel position of FRAME's top border.
880e6158
MR
7042 (top
7043 (let ((top (frame-parameter nil 'top)))
7044 (if (consp top)
7045 (funcall (car top) (cadr top))
7046 top)))
7047 ;; Sanitize minimum and maximum sizes.
7048 (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes)
7049 fit-frame-to-buffer-sizes))
7050 (max-height
7051 (cond
7052 ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height))
1dcb169c
MR
7053 ((numberp max-height) (* max-height char-height))
7054 (t display-height)))
880e6158
MR
7055 (min-height
7056 (cond
7057 ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height))
25a700d0
MR
7058 ((numberp min-height) (* min-height char-height))
7059 (t (* window-min-height char-height))))
880e6158
MR
7060 (max-width
7061 (cond
7062 ((numberp (nth 2 sizes))
7063 (- (* (nth 2 sizes) char-width) window-extra-width))
7064 ((numberp max-width)
1dcb169c 7065 (- (* max-width char-width) window-extra-width))
8960529e 7066 (t display-width)))
880e6158
MR
7067 (min-width
7068 (cond
7069 ((numberp (nth 3 sizes))
7070 (- (* (nth 3 sizes) char-width) window-extra-width))
7071 ((numberp min-width)
25a700d0
MR
7072 (- (* min-width char-width) window-extra-width))
7073 (t (* window-min-width char-width))))
880e6158
MR
7074 ;; Note: Currently, for a new frame the sizes of the header
7075 ;; and mode line may be estimated incorrectly
7076 (value (window-text-pixel-size
7077 nil t t workarea-width workarea-height t))
7078 (width (+ (car value) (window-right-divider-width)))
1dcb169c 7079 (height (+ (cdr value) (window-bottom-divider-width))))
880e6158 7080 ;; Don't change height or width when the window's size is fixed
cac0a1d6 7081 ;; in either direction or ONLY forbids it.
5938d519 7082 (cond
cac0a1d6 7083 ((or (eq window-size-fixed 'width) (eq only 'vertically))
1dcb169c 7084 (setq width nil))
cac0a1d6 7085 ((or (eq window-size-fixed 'height) (eq only 'horizontally))
880e6158 7086 (setq height nil)))
1dcb169c 7087 ;; Fit width to constraints.
880e6158 7088 (when width
1dcb169c
MR
7089 (unless frame-resize-pixelwise
7090 ;; Round to character sizes.
7091 (setq width (* (/ (+ width char-width -1) char-width)
7092 char-width)))
880e6158 7093 ;; Fit to maximum and minimum widths.
1dcb169c 7094 (setq width (max (min width max-width) min-width))
880e6158
MR
7095 ;; Add extra width.
7096 (setq width (+ width extra-width))
1dcb169c
MR
7097 ;; Preserve margins.
7098 (let ((right (+ left width)))
880e6158 7099 (cond
25a700d0
MR
7100 ((> right right-margin)
7101 ;; Move frame to left (we don't know its real width).
1dcb169c 7102 (setq left (max left-margin (- left (- right right-margin)))))
880e6158
MR
7103 ((< left left-margin)
7104 ;; Move frame to right.
7105 (setq left left-margin)))))
1dcb169c 7106 ;; Fit height to constraints.
880e6158 7107 (when height
1dcb169c
MR
7108 (unless frame-resize-pixelwise
7109 (setq height (* (/ (+ height char-height -1) char-height)
7110 char-height)))
880e6158 7111 ;; Fit to maximum and minimum heights.
1dcb169c 7112 (setq height (max (min height max-height) min-height))
880e6158
MR
7113 ;; Add extra height.
7114 (setq height (+ height extra-height))
1dcb169c
MR
7115 ;; Preserve margins.
7116 (let ((bottom (+ top height)))
880e6158 7117 (cond
25a700d0
MR
7118 ((> bottom bottom-margin)
7119 ;; Move frame up (we don't know its real height).
1dcb169c 7120 (setq top (max top-margin (- top (- bottom bottom-margin)))))
880e6158
MR
7121 ((< top top-margin)
7122 ;; Move frame down.
7123 (setq top top-margin)))))
7124 ;; Apply changes.
7125 (set-frame-position frame left top)
7126 ;; Clumsily try to translate our calculations to what
7127 ;; `set-frame-size' wants.
7128 (when width
7129 (setq width (- (+ (frame-text-width) width)
7130 extra-width window-body-width)))
7131 (when height
7132 (setq height (- (+ (frame-text-height) height)
7133 extra-height window-height)))
7134 (set-frame-size
7135 frame
7136 (if width
25a700d0 7137 (if frame-resize-pixelwise
880e6158
MR
7138 width
7139 (/ width char-width))
7140 (frame-text-width))
7141 (if height
25a700d0 7142 (if frame-resize-pixelwise
880e6158
MR
7143 height
7144 (/ height char-height))
7145 (frame-text-height))
25a700d0 7146 frame-resize-pixelwise)))))
880e6158
MR
7147
7148(defun fit-window-to-buffer (&optional window max-height min-height max-width min-width)
7149 "Adjust size of WINDOW to display its buffer's contents exactly.
85c2386b 7150WINDOW must be a live window and defaults to the selected one.
6198ccd0 7151
880e6158 7152If WINDOW is part of a vertical combination, adjust WINDOW's
cac0a1d6 7153height. The new height is calculated from the actual height of
880e6158
MR
7154the accessible portion of its buffer. The optional argument
7155MAX-HEIGHT specifies a maximum height and defaults to the height
7156of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
7157minimum height and defaults to `window-min-height'. Both
cac0a1d6
MR
7158MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
7159and header line and a bottom divider, if any.
880e6158
MR
7160
7161If WINDOW is part of a horizontal combination and the value of
7162the option `fit-window-to-buffer-horizontally' is non-nil, adjust
7163WINDOW's height. The new width of WINDOW is calculated from the
7164maximum length of its buffer's lines that follow the current
7165start position of WINDOW. The optional argument MAX-WIDTH
7166specifies a maximum width and defaults to the width of WINDOW's
7167frame. The optional argument MIN-WIDTH specifies a minimum width
7168and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
cac0a1d6
MR
7169are specified in columns and include fringes, margins, a
7170scrollbar and a vertical divider, if any.
880e6158
MR
7171
7172Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
cac0a1d6 7173If WINDOW is its frame's root window and the option
880e6158
MR
7174`fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
7175adjust the frame's size.
6198ccd0
MR
7176
7177Note that even if this function makes WINDOW large enough to show
880e6158
MR
7178_all_ parts of its buffer you might not see the first part when
7179WINDOW was scrolled. If WINDOW is resized horizontally, you will
7180not see the top of its buffer unless WINDOW starts at its minimum
7181accessible position."
f7baca20 7182 (interactive)
447f16b8 7183 (setq window (window-normalize-window window t))
880e6158
MR
7184 (if (eq window (frame-root-window window))
7185 (when fit-frame-to-buffer
7186 ;; Fit WINDOW's frame to buffer.
7187 (fit-frame-to-buffer
7188 (window-frame window)
cac0a1d6
MR
7189 max-height min-height max-width min-width
7190 (and (memq fit-frame-to-buffer '(vertically horizontally))
7191 fit-frame-to-buffer)))
6198ccd0 7192 (with-selected-window window
880e6158 7193 (let* ((pixelwise window-resize-pixelwise)
880e6158
MR
7194 (char-height (frame-char-height))
7195 (char-width (frame-char-width))
880e6158
MR
7196 (total-height (window-size window nil pixelwise))
7197 (body-height (window-body-height window pixelwise))
7198 (body-width (window-body-width window pixelwise))
6198ccd0 7199 (min-height
880e6158 7200 ;; Sanitize MIN-HEIGHT.
c5e28e39
MR
7201 (if (numberp min-height)
7202 ;; Can't get smaller than `window-safe-min-height'.
880e6158
MR
7203 (max (if pixelwise
7204 (* char-height min-height)
7205 min-height)
7206 (if pixelwise
7207 (window-safe-min-pixel-height window)
7208 window-safe-min-height))
c5e28e39 7209 ;; Preserve header and mode line if present.
25a700d0
MR
7210 (max (if pixelwise
7211 (* char-height window-min-height)
7212 window-min-height)
7213 (window-min-size nil nil t pixelwise))))
6198ccd0 7214 (max-height
880e6158 7215 ;; Sanitize MAX-HEIGHT.
c5e28e39 7216 (if (numberp max-height)
880e6158
MR
7217 (min
7218 (+ total-height
7219 (window-max-delta
7220 window nil nil nil nil nil pixelwise))
7221 (if pixelwise
7222 (* char-height max-height)
7223 max-height))
7224 (+ total-height (window-max-delta
7225 window nil nil nil nil nil pixelwise))))
7226 height)
7227 (cond
7228 ;; If WINDOW is vertically combined, try to resize it
7229 ;; vertically.
7230 ((and (not (eq fit-window-to-buffer-horizontally 'only))
7231 (not (window-size-fixed-p window))
7232 (window-combined-p))
7233 ;; Vertically we always want to fit the entire buffer.
7234 ;; WINDOW'S height can't get larger than its frame's pixel
7235 ;; height. Its width remains fixed.
7236 (setq height (+ (cdr (window-text-pixel-size
7237 nil nil t nil (frame-pixel-height) t))
7238 (window-bottom-divider-width)))
7239 ;; Round height.
7240 (unless pixelwise
6cb4da45 7241 (setq height (/ (+ height char-height -1) char-height)))
880e6158
MR
7242 (unless (= height total-height)
7243 (window-resize-no-error
7244 window
7245 (- (max min-height (min max-height height)) total-height)
7246 nil window pixelwise)))
7247 ;; If WINDOW is horizontally combined, try to resize it
7248 ;; horizontally.
7249 ((and fit-window-to-buffer-horizontally
7250 (not (window-size-fixed-p window t))
7251 (window-combined-p nil t))
b923819c 7252 (let* ((total-width (window-size window t pixelwise))
880e6158
MR
7253 (min-width
7254 ;; Sanitize MIN-WIDTH.
7255 (if (numberp min-width)
7256 ;; Can't get smaller than `window-safe-min-width'.
7257 (max (if pixelwise
7258 (* char-width min-width)
7259 min-width)
7260 (if pixelwise
7261 (window-safe-min-pixel-width)
7262 window-safe-min-width))
f224e500 7263 ;; Preserve fringes, margins, scrollbars if present.
25a700d0
MR
7264 (max (if pixelwise
7265 (* char-width window-min-width)
7266 window-min-width)
7267 (window-min-size nil nil t pixelwise))))
880e6158
MR
7268 (max-width
7269 ;; Sanitize MAX-WIDTH.
7270 (if (numberp max-width)
7271 (min (+ total-width
7272 (window-max-delta
7273 nil t nil nil nil nil pixelwise))
7274 (if pixelwise
7275 (* char-width max-width)
7276 max-width))
7277 (+ total-width (window-max-delta
7278 nil t nil nil nil nil pixelwise))))
7279 ;; When fitting vertically, assume that WINDOW's start
7280 ;; position remains unaltered. WINDOW can't get wider
7281 ;; than its frame's pixel width, its height remains
7282 ;; unaltered.
7283 (width (+ (car (window-text-pixel-size
7284 nil (window-start) (point-max)
7285 (frame-pixel-width)
7286 ;; Add one char-height to assure that
7287 ;; we're on the safe side. This
7288 ;; overshoots when the first line below
7289 ;; the bottom is wider than the window.
7290 (* body-height
7291 (if pixelwise char-height 1))))
7292 (window-right-divider-width))))
7293 (unless pixelwise
6cb4da45 7294 (setq width (/ (+ width char-width -1) char-width)))
880e6158
MR
7295 (unless (= width body-width)
7296 (window-resize-no-error
7297 window
7298 (- (max min-width
7299 (min max-width
7300 (+ total-width (- width body-width))))
7301 total-width)
7302 t window pixelwise)))))))))
ef654460 7303
39cffb44
MR
7304(defun window-safely-shrinkable-p (&optional window)
7305 "Return t if WINDOW can be shrunk without shrinking other windows.
7306WINDOW defaults to the selected window."
7307 (with-selected-window (or window (selected-window))
7308 (let ((edges (window-edges)))
39cffb44
MR
7309 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
7310 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
39cffb44 7311
3c448ab6
MR
7312(defun shrink-window-if-larger-than-buffer (&optional window)
7313 "Shrink height of WINDOW if its buffer doesn't need so many lines.
7314More precisely, shrink WINDOW vertically to be as small as
7315possible, while still showing the full contents of its buffer.
85c2386b 7316WINDOW must be a live window and defaults to the selected one.
3c448ab6 7317
6198ccd0
MR
7318Do not shrink WINDOW to less than `window-min-height' lines. Do
7319nothing if the buffer contains more lines than the present window
7320height, or if some of the window's contents are scrolled out of
7321view, or if shrinking this window would also shrink another
7322window, or if the window is the only window of its frame.
3c448ab6
MR
7323
7324Return non-nil if the window was shrunk, nil otherwise."
7325 (interactive)
447f16b8 7326 (setq window (window-normalize-window window t))
6198ccd0
MR
7327 ;; Make sure that WINDOW is vertically combined and `point-min' is
7328 ;; visible (for whatever reason that's needed). The remaining issues
7329 ;; should be taken care of by `fit-window-to-buffer'.
3d8daefe 7330 (when (and (window-combined-p window)
6198ccd0 7331 (pos-visible-in-window-p (point-min) window))
880e6158 7332 (fit-window-to-buffer window (window-total-height window))))
6198ccd0 7333\f
3c448ab6
MR
7334(defun kill-buffer-and-window ()
7335 "Kill the current buffer and delete the selected window."
7336 (interactive)
7337 (let ((window-to-delete (selected-window))
7338 (buffer-to-kill (current-buffer))
6198ccd0 7339 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
3c448ab6
MR
7340 (unwind-protect
7341 (progn
7342 (add-hook 'kill-buffer-hook delete-window-hook t t)
7343 (if (kill-buffer (current-buffer))
7344 ;; If `delete-window' failed before, we rerun it to regenerate
7345 ;; the error so it can be seen in the echo area.
7346 (when (eq (selected-window) window-to-delete)
7347 (delete-window))))
7348 ;; If the buffer is not dead for some reason (probably because
7349 ;; of a `quit' signal), remove the hook again.
6198ccd0
MR
7350 (ignore-errors
7351 (with-current-buffer buffer-to-kill
7352 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
3c448ab6 7353
74f806a1 7354\f
3c448ab6
MR
7355(defvar recenter-last-op nil
7356 "Indicates the last recenter operation performed.
0116abbd
JL
7357Possible values: `top', `middle', `bottom', integer or float numbers.")
7358
7359(defcustom recenter-positions '(middle top bottom)
7360 "Cycling order for `recenter-top-bottom'.
7361A list of elements with possible values `top', `middle', `bottom',
7362integer or float numbers that define the cycling order for
7363the command `recenter-top-bottom'.
7364
382c953b 7365Top and bottom destinations are `scroll-margin' lines from the true
0116abbd
JL
7366window top and bottom. Middle redraws the frame and centers point
7367vertically within the window. Integer number moves current line to
7368the specified absolute window-line. Float number between 0.0 and 1.0
7369means the percentage of the screen space from the top. The default
7370cycling order is middle -> top -> bottom."
7371 :type '(repeat (choice
7372 (const :tag "Top" top)
7373 (const :tag "Middle" middle)
7374 (const :tag "Bottom" bottom)
7375 (integer :tag "Line number")
7376 (float :tag "Percentage")))
7377 :version "23.2"
7378 :group 'windows)
3c448ab6
MR
7379
7380(defun recenter-top-bottom (&optional arg)
0116abbd
JL
7381 "Move current buffer line to the specified window line.
7382With no prefix argument, successive calls place point according
7383to the cycling order defined by `recenter-positions'.
3c448ab6
MR
7384
7385A prefix argument is handled like `recenter':
7386 With numeric prefix ARG, move current line to window-line ARG.
0116abbd 7387 With plain `C-u', move current line to window center."
3c448ab6
MR
7388 (interactive "P")
7389 (cond
0116abbd 7390 (arg (recenter arg)) ; Always respect ARG.
3c448ab6 7391 (t
0116abbd
JL
7392 (setq recenter-last-op
7393 (if (eq this-command last-command)
7394 (car (or (cdr (member recenter-last-op recenter-positions))
7395 recenter-positions))
7396 (car recenter-positions)))
3c448ab6
MR
7397 (let ((this-scroll-margin
7398 (min (max 0 scroll-margin)
7399 (truncate (/ (window-body-height) 4.0)))))
7400 (cond ((eq recenter-last-op 'middle)
0116abbd 7401 (recenter))
3c448ab6 7402 ((eq recenter-last-op 'top)
0116abbd
JL
7403 (recenter this-scroll-margin))
7404 ((eq recenter-last-op 'bottom)
7405 (recenter (- -1 this-scroll-margin)))
7406 ((integerp recenter-last-op)
7407 (recenter recenter-last-op))
7408 ((floatp recenter-last-op)
7409 (recenter (round (* recenter-last-op (window-height))))))))))
3c448ab6
MR
7410
7411(define-key global-map [?\C-l] 'recenter-top-bottom)
216349f8 7412
216349f8
SM
7413(defun move-to-window-line-top-bottom (&optional arg)
7414 "Position point relative to window.
7415
0f202d5d 7416With a prefix argument ARG, acts like `move-to-window-line'.
216349f8
SM
7417
7418With no argument, positions point at center of window.
0116abbd
JL
7419Successive calls position point at positions defined
7420by `recenter-positions'."
216349f8
SM
7421 (interactive "P")
7422 (cond
0116abbd 7423 (arg (move-to-window-line arg)) ; Always respect ARG.
216349f8 7424 (t
0116abbd
JL
7425 (setq recenter-last-op
7426 (if (eq this-command last-command)
7427 (car (or (cdr (member recenter-last-op recenter-positions))
7428 recenter-positions))
7429 (car recenter-positions)))
216349f8
SM
7430 (let ((this-scroll-margin
7431 (min (max 0 scroll-margin)
7432 (truncate (/ (window-body-height) 4.0)))))
0f202d5d 7433 (cond ((eq recenter-last-op 'middle)
0116abbd 7434 (call-interactively 'move-to-window-line))
0f202d5d 7435 ((eq recenter-last-op 'top)
0116abbd
JL
7436 (move-to-window-line this-scroll-margin))
7437 ((eq recenter-last-op 'bottom)
7438 (move-to-window-line (- -1 this-scroll-margin)))
7439 ((integerp recenter-last-op)
7440 (move-to-window-line recenter-last-op))
7441 ((floatp recenter-last-op)
7442 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
216349f8
SM
7443
7444(define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
3c448ab6 7445\f
74f806a1
JL
7446;;; Scrolling commands.
7447
0a9f9ab5
SM
7448;;; Scrolling commands which do not signal errors at top/bottom
7449;;; of buffer at first key-press (instead move to top/bottom
74f806a1
JL
7450;;; of buffer).
7451
7452(defcustom scroll-error-top-bottom nil
8350f087 7453 "Move point to top/bottom of buffer before signaling a scrolling error.
74f806a1
JL
7454A value of nil means just signal an error if no more scrolling possible.
7455A value of t means point moves to the beginning or the end of the buffer
7456\(depending on scrolling direction) when no more scrolling possible.
7457When point is already on that position, then signal an error."
7458 :type 'boolean
60efac0f 7459 :group 'windows
74f806a1
JL
7460 :version "24.1")
7461
7462(defun scroll-up-command (&optional arg)
7463 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
7464If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
7465scroll window further, move cursor to the bottom line.
7466When point is already on that position, then signal an error.
7467A near full screen is `next-screen-context-lines' less than a full screen.
7468Negative ARG means scroll downward.
7469If ARG is the atom `-', scroll downward by nearly full screen."
7470 (interactive "^P")
7471 (cond
7472 ((null scroll-error-top-bottom)
7473 (scroll-up arg))
7474 ((eq arg '-)
7475 (scroll-down-command nil))
7476 ((< (prefix-numeric-value arg) 0)
7477 (scroll-down-command (- (prefix-numeric-value arg))))
7478 ((eobp)
7479 (scroll-up arg)) ; signal error
7480 (t
7481 (condition-case nil
7482 (scroll-up arg)
7483 (end-of-buffer
7484 (if arg
7485 ;; When scrolling by ARG lines can't be done,
7486 ;; move by ARG lines instead.
7487 (forward-line arg)
7488 ;; When ARG is nil for full-screen scrolling,
7489 ;; move to the bottom of the buffer.
7490 (goto-char (point-max))))))))
7491
7492(put 'scroll-up-command 'scroll-command t)
7493
7494(defun scroll-down-command (&optional arg)
7495 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
7496If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
7497scroll window further, move cursor to the top line.
7498When point is already on that position, then signal an error.
7499A near full screen is `next-screen-context-lines' less than a full screen.
7500Negative ARG means scroll upward.
7501If ARG is the atom `-', scroll upward by nearly full screen."
7502 (interactive "^P")
7503 (cond
7504 ((null scroll-error-top-bottom)
7505 (scroll-down arg))
7506 ((eq arg '-)
7507 (scroll-up-command nil))
7508 ((< (prefix-numeric-value arg) 0)
7509 (scroll-up-command (- (prefix-numeric-value arg))))
7510 ((bobp)
7511 (scroll-down arg)) ; signal error
7512 (t
7513 (condition-case nil
7514 (scroll-down arg)
7515 (beginning-of-buffer
7516 (if arg
7517 ;; When scrolling by ARG lines can't be done,
7518 ;; move by ARG lines instead.
7519 (forward-line (- arg))
7520 ;; When ARG is nil for full-screen scrolling,
7521 ;; move to the top of the buffer.
7522 (goto-char (point-min))))))))
7523
7524(put 'scroll-down-command 'scroll-command t)
7525
7526;;; Scrolling commands which scroll a line instead of full screen.
7527
7528(defun scroll-up-line (&optional arg)
7529 "Scroll text of selected window upward ARG lines; or one line if no ARG.
7530If ARG is omitted or nil, scroll upward by one line.
7531This is different from `scroll-up-command' that scrolls a full screen."
7532 (interactive "p")
7533 (scroll-up (or arg 1)))
7534
7535(put 'scroll-up-line 'scroll-command t)
7536
7537(defun scroll-down-line (&optional arg)
7538 "Scroll text of selected window down ARG lines; or one line if no ARG.
7539If ARG is omitted or nil, scroll down by one line.
7540This is different from `scroll-down-command' that scrolls a full screen."
7541 (interactive "p")
7542 (scroll-down (or arg 1)))
7543
7544(put 'scroll-down-line 'scroll-command t)
7545
7546\f
011474aa 7547(defun scroll-other-window-down (&optional lines)
74f806a1
JL
7548 "Scroll the \"other window\" down.
7549For more details, see the documentation for `scroll-other-window'."
7550 (interactive "P")
7551 (scroll-other-window
7552 ;; Just invert the argument's meaning.
7553 ;; We can do that without knowing which window it will be.
7554 (if (eq lines '-) nil
7555 (if (null lines) '-
7556 (- (prefix-numeric-value lines))))))
7557
7558(defun beginning-of-buffer-other-window (arg)
7559 "Move point to the beginning of the buffer in the other window.
7560Leave mark at previous position.
7561With arg N, put point N/10 of the way from the true beginning."
7562 (interactive "P")
7563 (let ((orig-window (selected-window))
7564 (window (other-window-for-scrolling)))
7565 ;; We use unwind-protect rather than save-window-excursion
7566 ;; because the latter would preserve the things we want to change.
7567 (unwind-protect
7568 (progn
7569 (select-window window)
7570 ;; Set point and mark in that window's buffer.
7571 (with-no-warnings
7572 (beginning-of-buffer arg))
7573 ;; Set point accordingly.
7574 (recenter '(t)))
7575 (select-window orig-window))))
7576
7577(defun end-of-buffer-other-window (arg)
7578 "Move point to the end of the buffer in the other window.
7579Leave mark at previous position.
7580With arg N, put point N/10 of the way from the true end."
7581 (interactive "P")
7582 ;; See beginning-of-buffer-other-window for comments.
7583 (let ((orig-window (selected-window))
7584 (window (other-window-for-scrolling)))
7585 (unwind-protect
7586 (progn
7587 (select-window window)
7588 (with-no-warnings
7589 (end-of-buffer arg))
7590 (recenter '(t)))
7591 (select-window orig-window))))
74f806a1 7592\f
3c448ab6
MR
7593(defvar mouse-autoselect-window-timer nil
7594 "Timer used by delayed window autoselection.")
7595
7596(defvar mouse-autoselect-window-position nil
7597 "Last mouse position recorded by delayed window autoselection.")
7598
7599(defvar mouse-autoselect-window-window nil
7600 "Last window recorded by delayed window autoselection.")
7601
7602(defvar mouse-autoselect-window-state nil
7603 "When non-nil, special state of delayed window autoselection.
382c953b
JB
7604Possible values are `suspend' (suspend autoselection after a menu or
7605scrollbar interaction) and `select' (the next invocation of
7606`handle-select-window' shall select the window immediately).")
3c448ab6
MR
7607
7608(defun mouse-autoselect-window-cancel (&optional force)
7609 "Cancel delayed window autoselection.
7610Optional argument FORCE means cancel unconditionally."
7611 (unless (and (not force)
7612 ;; Don't cancel for select-window or select-frame events
7613 ;; or when the user drags a scroll bar.
7614 (or (memq this-command
7615 '(handle-select-window handle-switch-frame))
7616 (and (eq this-command 'scroll-bar-toolkit-scroll)
7617 (memq (nth 4 (event-end last-input-event))
7618 '(handle end-scroll)))))
7619 (setq mouse-autoselect-window-state nil)
7620 (when (timerp mouse-autoselect-window-timer)
7621 (cancel-timer mouse-autoselect-window-timer))
7622 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
7623
7624(defun mouse-autoselect-window-start (mouse-position &optional window suspend)
7625 "Start delayed window autoselection.
7626MOUSE-POSITION is the last position where the mouse was seen as returned
7627by `mouse-position'. Optional argument WINDOW non-nil denotes the
7628window where the mouse was seen. Optional argument SUSPEND non-nil
7629means suspend autoselection."
7630 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
7631 (setq mouse-autoselect-window-position mouse-position)
7632 (when window (setq mouse-autoselect-window-window window))
7633 (setq mouse-autoselect-window-state (when suspend 'suspend))
7634 ;; Install timer which runs `mouse-autoselect-window-select' after
7635 ;; `mouse-autoselect-window' seconds.
7636 (setq mouse-autoselect-window-timer
7637 (run-at-time
7638 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
7639
7640(defun mouse-autoselect-window-select ()
7641 "Select window with delayed window autoselection.
7642If the mouse position has stabilized in a non-selected window, select
382c953b
JB
7643that window. The minibuffer window is selected only if the minibuffer
7644is active. This function is run by `mouse-autoselect-window-timer'."
6198ccd0
MR
7645 (ignore-errors
7646 (let* ((mouse-position (mouse-position))
7647 (window
7648 (ignore-errors
7649 (window-at (cadr mouse-position) (cddr mouse-position)
7650 (car mouse-position)))))
7651 (cond
e740f9d2 7652 ((or (and (fboundp 'menu-or-popup-active-p) (menu-or-popup-active-p))
6198ccd0 7653 (and window
3dfc5cd6
MR
7654 (let ((coords (coordinates-in-window-p
7655 (cdr mouse-position) window)))
7656 (and (not (consp coords))
7657 (not (memq coords '(left-margin right-margin)))))))
c660a885
MR
7658 ;; A menu / popup dialog is active or the mouse is not on the
7659 ;; text region of WINDOW: Suspend autoselection temporarily.
6198ccd0
MR
7660 (mouse-autoselect-window-start mouse-position nil t))
7661 ((eq mouse-autoselect-window-state 'suspend)
7662 ;; Delayed autoselection was temporarily suspended, reenable it.
7663 (mouse-autoselect-window-start mouse-position))
7664 ((and window (not (eq window (selected-window)))
7665 (or (not (numberp mouse-autoselect-window))
7666 (and (> mouse-autoselect-window 0)
7667 ;; If `mouse-autoselect-window' is positive, select
7668 ;; window if the window is the same as before.
7669 (eq window mouse-autoselect-window-window))
7670 ;; Otherwise select window if the mouse is at the same
7671 ;; position as before. Observe that the first test after
7672 ;; starting autoselection usually fails since the value of
7673 ;; `mouse-autoselect-window-position' recorded there is the
7674 ;; position where the mouse has entered the new window and
7675 ;; not necessarily where the mouse has stopped moving.
7676 (equal mouse-position mouse-autoselect-window-position))
7677 ;; The minibuffer is a candidate window if it's active.
7678 (or (not (window-minibuffer-p window))
7679 (eq window (active-minibuffer-window))))
7680 ;; Mouse position has stabilized in non-selected window: Cancel
7681 ;; delayed autoselection and try to select that window.
7682 (mouse-autoselect-window-cancel t)
7683 ;; Select window where mouse appears unless the selected window is the
7684 ;; minibuffer. Use `unread-command-events' in order to execute pre-
7685 ;; and post-command hooks and trigger idle timers. To avoid delaying
7686 ;; autoselection again, set `mouse-autoselect-window-state'."
290d5b58 7687 (unless (window-minibuffer-p)
6198ccd0
MR
7688 (setq mouse-autoselect-window-state 'select)
7689 (setq unread-command-events
7690 (cons (list 'select-window (list window))
7691 unread-command-events))))
7692 ((or (and window (eq window (selected-window)))
7693 (not (numberp mouse-autoselect-window))
7694 (equal mouse-position mouse-autoselect-window-position))
7695 ;; Mouse position has either stabilized in the selected window or at
7696 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
7697 (mouse-autoselect-window-cancel t))
7698 (t
7699 ;; Mouse position has not stabilized yet, resume delayed
7700 ;; autoselection.
7701 (mouse-autoselect-window-start mouse-position window))))))
3c448ab6
MR
7702
7703(defun handle-select-window (event)
7704 "Handle select-window events."
7705 (interactive "e")
7706 (let ((window (posn-window (event-start event))))
7707 (unless (or (not (window-live-p window))
7708 ;; Don't switch if we're currently in the minibuffer.
7709 ;; This tries to work around problems where the
7710 ;; minibuffer gets unselected unexpectedly, and where
7711 ;; you then have to move your mouse all the way down to
7712 ;; the minibuffer to select it.
290d5b58 7713 (window-minibuffer-p)
3c448ab6
MR
7714 ;; Don't switch to minibuffer window unless it's active.
7715 (and (window-minibuffer-p window)
7716 (not (minibuffer-window-active-p window)))
7717 ;; Don't switch when autoselection shall be delayed.
7718 (and (numberp mouse-autoselect-window)
7719 (not (zerop mouse-autoselect-window))
7720 (not (eq mouse-autoselect-window-state 'select))
7721 (progn
7722 ;; Cancel any delayed autoselection.
7723 (mouse-autoselect-window-cancel t)
7724 ;; Start delayed autoselection from current mouse
7725 ;; position and window.
7726 (mouse-autoselect-window-start (mouse-position) window)
7727 ;; Executing a command cancels delayed autoselection.
7728 (add-hook
7729 'pre-command-hook 'mouse-autoselect-window-cancel))))
7730 (when mouse-autoselect-window
7731 ;; Reset state of delayed autoselection.
7732 (setq mouse-autoselect-window-state nil)
7733 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
7734 (run-hooks 'mouse-leave-buffer-hook))
b1bac16e
MR
7735 ;; Clear echo area.
7736 (message nil)
3c448ab6
MR
7737 (select-window window))))
7738
3c448ab6
MR
7739(defun truncated-partial-width-window-p (&optional window)
7740 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
85c2386b 7741WINDOW must be a live window and defaults to the selected one.
3c448ab6
MR
7742Return nil if WINDOW is not a partial-width window
7743 (regardless of the value of `truncate-lines').
7744Otherwise, consult the value of `truncate-partial-width-windows'
7745 for the buffer shown in WINDOW."
85c2386b 7746 (setq window (window-normalize-window window t))
3c448ab6
MR
7747 (unless (window-full-width-p window)
7748 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
7749 (window-buffer window))))
7750 (if (integerp t-p-w-w)
7751 (< (window-width window) t-p-w-w)
7752 t-p-w-w))))
562dd5e9 7753\f
59ee0542
GM
7754;; Some of these are in tutorial--default-keys, so update that if you
7755;; change these.
562dd5e9
MR
7756(define-key ctl-x-map "0" 'delete-window)
7757(define-key ctl-x-map "1" 'delete-other-windows)
2d197ffb
CY
7758(define-key ctl-x-map "2" 'split-window-below)
7759(define-key ctl-x-map "3" 'split-window-right)
9397e56f 7760(define-key ctl-x-map "o" 'other-window)
562dd5e9 7761(define-key ctl-x-map "^" 'enlarge-window)
3c448ab6
MR
7762(define-key ctl-x-map "}" 'enlarge-window-horizontally)
7763(define-key ctl-x-map "{" 'shrink-window-horizontally)
7764(define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
7765(define-key ctl-x-map "+" 'balance-windows)
7766(define-key ctl-x-4-map "0" 'kill-buffer-and-window)
7767
3c448ab6 7768;;; window.el ends here