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