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