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