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