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