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