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