* lisp/window.el (window--pixel-to-total): Remove unused `mini' var.
[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
2082(defun window--pixel-to-size (window size &optional horizontal round-up)
2083 "For WINDOW convert SIZE pixels to lines.
2084WINDOW must be a valid window and defaults to the selected one.
2085Optional argument HORIZONTAL non-nil means convert SIZE pixels to
2086columns. Optional argument ROUND-UP means to round up the return
2087value."
2088 (let ((char-size (frame-char-size
2089 (window-normalize-window window) horizontal)))
2090 (if round-up
2091 (/ (+ size char-size -1) char-size)
2092 (/ size char-size))))
2093
2094(defun window--pixel-to-total-1 (window horizontal char-size)
2095 "Subroutine of `window--pixel-to-total'."
2096 (let ((child (window-child window)))
2097 (if (window-combination-p window horizontal)
2098 ;; In an iso-combination distribute sizes proportionally.
2099 (let ((remainder (window-new-total window))
2100 size best-child best-size)
2101 ;; Initialize total sizes to each child's floor.
2102 (while child
2103 (setq size (window--pixel-to-size
2104 child (window-size child horizontal t)
2105 horizontal))
2106 (set-window-new-total child size)
2107 (setq remainder (- remainder size))
2108 (setq child (window-next-sibling child)))
2109 ;; Distribute remainder.
2110 (while (> remainder 0)
2111 (setq child (window-last-child window))
2112 (setq best-child nil)
2113 (setq best-size 0)
2114 ;; We want those auxiliary fields in the window structure to
2115 ;; avoid this.
2116 (while child
2117 (setq size (- (/ (window-size child horizontal t) char-size)
2118 (window-new-total child)))
2119 (when (> size best-size)
2120 (setq best-child child)
2121 (setq best-size size))
2122 (setq child (window-prev-sibling child)))
2123 ;; We MUST have a best-child here.
2124 (set-window-new-total best-child 1 t)
2125 (setq remainder (1- remainder)))
2126 ;; Recurse.
2127 (setq child (window-child window))
2128 (while child
2129 (window--pixel-to-total-1 child horizontal char-size)
2130 (setq child (window-next-sibling child))))
2131 ;; In an ortho-combination assign new sizes directly.
2132 (let ((size (window-new-total window)))
2133 (while child
2134 (set-window-new-total child size)
2135 (window--pixel-to-total-1 child horizontal char-size)
2136 (setq child (window-next-sibling child)))))))
2137
2138(defun window--pixel-to-total (&optional frame horizontal)
2139 "On FRAME assign new total window heights from pixel heights.
2140FRAME must be a live frame and defaults to the selected frame.
2141
2142Optional argument HORIZONTAL non-nil means assign new total
2143window widths from pixel widths."
2144 (setq frame (window-normalize-frame frame))
2145 (let ((root (frame-root-window))
2d6b6005 2146 (char-size (frame-char-size frame horizontal)))
880e6158
MR
2147 (set-window-new-total
2148 root (window--pixel-to-size
2149 root (window-size root horizontal t) horizontal))
2150 (unless (window-buffer root)
2151 (window--pixel-to-total-1 root horizontal char-size)))
2152 (window-resize-apply-total frame horizontal))
2153
5386012d 2154(defun window--resize-reset (&optional frame horizontal)
9aab8e0d
MR
2155 "Reset resize values for all windows on FRAME.
2156FRAME defaults to the selected frame.
2157
880e6158 2158This function stores the current value of `window-size' applied
9aab8e0d
MR
2159with argument HORIZONTAL in the new total size of all windows on
2160FRAME. It also resets the new normal size of each of these
2161windows."
5386012d
MR
2162 (window--resize-reset-1
2163 (frame-root-window (window-normalize-frame frame)) horizontal))
9aab8e0d 2164
5386012d
MR
2165(defun window--resize-reset-1 (window horizontal)
2166 "Internal function of `window--resize-reset'."
9aab8e0d 2167 ;; Register old size in the new total size.
880e6158
MR
2168 (set-window-new-pixel window (window-size window horizontal t))
2169 (set-window-new-total window (window-size window horizontal))
9aab8e0d
MR
2170 ;; Reset new normal size.
2171 (set-window-new-normal window)
2172 (when (window-child window)
5386012d 2173 (window--resize-reset-1 (window-child window) horizontal))
9aab8e0d 2174 (when (window-right window)
5386012d 2175 (window--resize-reset-1 (window-right window) horizontal)))
9aab8e0d 2176
562dd5e9
MR
2177;; The following routine is used to manually resize the minibuffer
2178;; window and is currently used, for example, by ispell.el.
5386012d 2179(defun window--resize-mini-window (window delta)
880e6158
MR
2180 "Resize minibuffer window WINDOW by DELTA pixels.
2181If WINDOW cannot be resized by DELTA pixels make it as large (or
2116e93c 2182as small) as possible, but don't signal an error."
562dd5e9
MR
2183 (when (window-minibuffer-p window)
2184 (let* ((frame (window-frame window))
2185 (root (frame-root-window frame))
880e6158 2186 (height (window-pixel-height window))
562dd5e9 2187 (min-delta
880e6158
MR
2188 (- (window-pixel-height root)
2189 (window-min-size root nil nil t))))
562dd5e9
MR
2190 ;; Sanitize DELTA.
2191 (cond
2192 ((<= (+ height delta) 0)
880e6158 2193 (setq delta (- (frame-char-height (window-frame window)) height)))
562dd5e9
MR
2194 ((> delta min-delta)
2195 (setq delta min-delta)))
2196
880e6158
MR
2197 (unless (zerop delta)
2198 ;; Resize now.
2199 (window--resize-reset frame)
2200 ;; Ideally we should be able to resize just the last child of root
2201 ;; here. See the comment in `resize-root-window-vertically' for
2202 ;; why we do not do that.
2203 (window--resize-this-window root (- delta) nil nil t)
2204 (set-window-new-pixel window (+ height delta))
2205 ;; The following routine catches the case where we want to resize
2206 ;; a minibuffer-only frame.
2207 (when (resize-mini-window-internal window)
2208 (window--pixel-to-total frame)
2209 (run-window-configuration-change-hook frame))))))
2210
2211(defun window--resize-apply-p (frame &optional horizontal)
2212 "Return t when a window on FRAME shall be resized vertically.
2213Optional argument HORIZONTAL non-nil means return t when a window
2214shall be resized horizontally."
2215(catch 'apply
2216 (walk-window-tree
2217 (lambda (window)
2218 (unless (= (window-new-pixel window)
2219 (window-size window horizontal t))
2220 (throw 'apply t)))
2221 frame t)
2222 nil))
2223
2224(defun window-resize (window delta &optional horizontal ignore pixelwise)
562dd5e9
MR
2225 "Resize WINDOW vertically by DELTA lines.
2226WINDOW can be an arbitrary window and defaults to the selected
2227one. An attempt to resize the root window of a frame will raise
2228an error though.
2229
2230DELTA a positive number means WINDOW shall be enlarged by DELTA
2231lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2232lines.
2233
2234Optional argument HORIZONTAL non-nil means resize WINDOW
2235horizontally by DELTA columns. In this case a positive DELTA
2236means enlarge WINDOW by DELTA columns. DELTA negative means
2237WINDOW shall be shrunk by -DELTA columns.
2238
2116e93c 2239Optional argument IGNORE non-nil means ignore restrictions
562dd5e9 2240imposed by fixed size windows, `window-min-height' or
2116e93c
EZ
2241`window-min-width' settings. If IGNORE is a window, ignore
2242restrictions for that window only. If IGNORE equals `safe',
562dd5e9 2243live windows may get as small as `window-safe-min-height' lines
2116e93c
EZ
2244and `window-safe-min-width' columns. Any other non-nil value
2245means ignore all of the above restrictions for all windows.
562dd5e9 2246
880e6158
MR
2247Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2248pixels.
2249
562dd5e9
MR
2250This function resizes other windows proportionally and never
2251deletes any windows. If you want to move only the low (right)
2252edge of WINDOW consider using `adjust-window-trailing-edge'
2253instead."
447f16b8 2254 (setq window (window-normalize-window window))
562dd5e9 2255 (let* ((frame (window-frame window))
feeb6f53 2256 (minibuffer-window (minibuffer-window frame))
562dd5e9 2257 sibling)
880e6158
MR
2258 (setq delta (window--size-to-pixel
2259 window delta horizontal pixelwise t))
562dd5e9
MR
2260 (cond
2261 ((eq window (frame-root-window frame))
2262 (error "Cannot resize the root window of a frame"))
2263 ((window-minibuffer-p window)
41cfe0cb
MR
2264 (if horizontal
2265 (error "Cannot resize minibuffer window horizontally")
2266 (window--resize-mini-window window delta)))
feeb6f53
MR
2267 ((and (not horizontal)
2268 (window-full-height-p window)
2269 (eq (window-frame minibuffer-window) frame)
2270 (or (not resize-mini-windows)
2271 (eq minibuffer-window (active-minibuffer-window))))
2272 ;; If WINDOW is full height and either `resize-mini-windows' is
2273 ;; nil or the minibuffer window is active, resize the minibuffer
2274 ;; window.
2275 (window--resize-mini-window minibuffer-window (- delta)))
880e6158
MR
2276 ((window--resizable-p
2277 window delta horizontal ignore nil nil nil t)
5386012d
MR
2278 (window--resize-reset frame horizontal)
2279 (window--resize-this-window window delta horizontal ignore t)
a0c2d0ae 2280 (if (and (not window-combination-resize)
3d8daefe 2281 (window-combined-p window horizontal)
562dd5e9 2282 (setq sibling (or (window-right window) (window-left window)))
880e6158
MR
2283 (window-sizable-p
2284 sibling (- delta) horizontal ignore t))
a0c2d0ae 2285 ;; If window-combination-resize is nil, WINDOW is part of an
89d61221 2286 ;; iso-combination, and WINDOW's neighboring right or left
562dd5e9
MR
2287 ;; sibling can be resized as requested, resize that sibling.
2288 (let ((normal-delta
2289 (/ (float delta)
880e6158 2290 (window-size (window-parent window) horizontal t))))
5386012d 2291 (window--resize-this-window sibling (- delta) horizontal nil t)
562dd5e9
MR
2292 (set-window-new-normal
2293 window (+ (window-normal-size window horizontal)
2294 normal-delta))
2295 (set-window-new-normal
2296 sibling (- (window-normal-size sibling horizontal)
2297 normal-delta)))
2298 ;; Otherwise, resize all other windows in the same combination.
5386012d 2299 (window--resize-siblings window delta horizontal ignore))
880e6158
MR
2300 (when (window--resize-apply-p frame horizontal)
2301 (window-resize-apply frame horizontal)
2302 (window--pixel-to-total frame horizontal)
2303 (run-window-configuration-change-hook frame)))
562dd5e9
MR
2304 (t
2305 (error "Cannot resize window %s" window)))))
2306
880e6158 2307(defun window-resize-no-error (window delta &optional horizontal ignore pixelwise)
27fcfe31
MR
2308 "Resize WINDOW vertically if it is resizable by DELTA lines.
2309This function is like `window-resize' but does not signal an
2310error when WINDOW cannot be resized. For the meaning of the
880e6158
MR
2311optional arguments see the documentation of `window-resize'.
2312
2313Optional argument PIXELWISE non-nil means interpret DELTA as
2314pixels."
2315 (when (window--resizable-p
2316 window delta horizontal ignore nil nil nil pixelwise)
2317 (window-resize window delta horizontal ignore pixelwise)))
27fcfe31 2318
4b0d61e3 2319(defun window--resize-child-windows-skip-p (window)
9aab8e0d
MR
2320 "Return non-nil if WINDOW shall be skipped by resizing routines."
2321 (memq (window-new-normal window) '(ignore stuck skip)))
2322
be7f5545
MR
2323(defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
2324 "Recursively set new normal height of child windows of window PARENT.
9aab8e0d 2325HORIZONTAL non-nil means set the new normal width of these
be7f5545 2326windows. WINDOW specifies a child window of PARENT that has been
382c953b 2327resized by THIS-DELTA lines (columns).
9aab8e0d 2328
2116e93c
EZ
2329Optional argument TRAIL either `before' or `after' means set values
2330only for windows before or after WINDOW. Optional argument
2331OTHER-DELTA, a number, specifies that this many lines (columns)
382c953b 2332have been obtained from (or returned to) an ancestor window of
9aab8e0d
MR
2333PARENT in order to resize WINDOW."
2334 (let* ((delta-normal
880e6158
MR
2335 (if (and (= (- this-delta)
2336 (window-size window horizontal t))
9aab8e0d
MR
2337 (zerop other-delta))
2338 ;; When WINDOW gets deleted and we can return its entire
2339 ;; space to its siblings, use WINDOW's normal size as the
2340 ;; normal delta.
2341 (- (window-normal-size window horizontal))
2342 ;; In any other case calculate the normal delta from the
2343 ;; relation of THIS-DELTA to the total size of PARENT.
880e6158
MR
2344 (/ (float this-delta)
2345 (window-size parent horizontal t))))
9aab8e0d
MR
2346 (sub (window-child parent))
2347 (parent-normal 0.0)
2348 (skip (eq trail 'after)))
2349
be7f5545
MR
2350 ;; Set parent-normal to the sum of the normal sizes of all child
2351 ;; windows of PARENT that shall be resized, excluding only WINDOW
9aab8e0d
MR
2352 ;; and any windows specified by the optional TRAIL argument.
2353 (while sub
2354 (cond
2355 ((eq sub window)
2356 (setq skip (eq trail 'before)))
2357 (skip)
2358 (t
2359 (setq parent-normal
2360 (+ parent-normal (window-normal-size sub horizontal)))))
2361 (setq sub (window-right sub)))
2362
be7f5545 2363 ;; Set the new normal size of all child windows of PARENT from what
9aab8e0d
MR
2364 ;; they should have contributed for recovering THIS-DELTA lines
2365 ;; (columns).
2366 (setq sub (window-child parent))
2367 (setq skip (eq trail 'after))
2368 (while sub
2369 (cond
2370 ((eq sub window)
2371 (setq skip (eq trail 'before)))
2372 (skip)
2373 (t
2374 (let ((old-normal (window-normal-size sub horizontal)))
2375 (set-window-new-normal
2376 sub (min 1.0 ; Don't get larger than 1.
2377 (max (- old-normal
2378 (* (/ old-normal parent-normal)
2379 delta-normal))
2380 ;; Don't drop below 0.
2381 0.0))))))
2382 (setq sub (window-right sub)))
2383
2384 (when (numberp other-delta)
2385 ;; Set the new normal size of windows from what they should have
2386 ;; contributed for recovering OTHER-DELTA lines (columns).
880e6158
MR
2387 (setq delta-normal (/ (float (window-size parent horizontal t))
2388 (+ (window-size parent horizontal t)
9aab8e0d
MR
2389 other-delta)))
2390 (setq sub (window-child parent))
2391 (setq skip (eq trail 'after))
2392 (while sub
2393 (cond
2394 ((eq sub window)
2395 (setq skip (eq trail 'before)))
2396 (skip)
2397 (t
2398 (set-window-new-normal
2399 sub (min 1.0 ; Don't get larger than 1.
2400 (max (* (window-new-normal sub) delta-normal)
2401 ;; Don't drop below 0.
2402 0.0)))))
2403 (setq sub (window-right sub))))
2404
2405 ;; Set the new normal size of WINDOW to what is left by the sum of
2406 ;; the normal sizes of its siblings.
2407 (set-window-new-normal
2408 window
2409 (let ((sum 0))
2410 (setq sub (window-child parent))
2411 (while sub
2412 (cond
2413 ((eq sub window))
2414 ((not (numberp (window-new-normal sub)))
2415 (setq sum (+ sum (window-normal-size sub horizontal))))
2416 (t
2417 (setq sum (+ sum (window-new-normal sub)))))
2418 (setq sub (window-right sub)))
2419 ;; Don't get larger than 1 or smaller than 0.
2420 (min 1.0 (max (- 1.0 sum) 0.0))))))
2421
880e6158
MR
2422(defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge char-size)
2423 "Resize child windows of window PARENT vertically by DELTA pixels.
9aab8e0d
MR
2424PARENT must be a vertically combined internal window.
2425
880e6158
MR
2426Optional argument HORIZONTAL non-nil means resize child windows
2427of PARENT horizontally by DELTA pixels. In this case PARENT must
9aab8e0d
MR
2428be a horizontally combined internal window.
2429
2430WINDOW, if specified, must denote a child window of PARENT that
880e6158 2431is resized by DELTA pixels.
9aab8e0d 2432
2116e93c 2433Optional argument IGNORE non-nil means ignore restrictions
9aab8e0d 2434imposed by fixed size windows, `window-min-height' or
2116e93c 2435`window-min-width' settings. If IGNORE equals `safe', live
9aab8e0d 2436windows may get as small as `window-safe-min-height' lines and
2116e93c
EZ
2437`window-safe-min-width' columns. If IGNORE is a window, ignore
2438restrictions for that window only. Any other non-nil value means
2439ignore all of the above restrictions for all windows.
9aab8e0d
MR
2440
2441Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2442of windows that shall be resized. If TRAIL equals `before',
2443resize only windows on the left or above EDGE. If TRAIL equals
2444`after', resize only windows on the right or below EDGE. Also,
2445preferably only resize windows adjacent to EDGE.
2446
880e6158
MR
2447If the optional argument CHAR-SIZE is a positive integer, it specifies
2448the number of pixels by which windows are incrementally resized.
2449If CHAR-SIZE is nil, this means to use the value of
2450`frame-char-height' or `frame-char-width' of WINDOW's frame.
2451
9aab8e0d
MR
2452Return the symbol `normalized' if new normal sizes have been
2453already set by this routine."
2454 (let* ((first (window-child parent))
9c52dd5a 2455 (last (window-last-child parent))
880e6158
MR
2456 (parent-total (+ (window-size parent horizontal t)
2457 delta))
2458 (char-size (or char-size
2459 (and window-resize-pixelwise 1)
2460 (frame-char-size window horizontal)))
2461 sub best-window best-value best-delta)
9aab8e0d
MR
2462
2463 (if (and edge (memq trail '(before after))
2464 (progn
2465 (setq sub first)
2466 (while (and (window-right sub)
2467 (or (and (eq trail 'before)
be7f5545 2468 (not (window--resize-child-windows-skip-p
9aab8e0d
MR
2469 (window-right sub))))
2470 (and (eq trail 'after)
be7f5545 2471 (window--resize-child-windows-skip-p sub))))
9aab8e0d
MR
2472 (setq sub (window-right sub)))
2473 sub)
2474 (if horizontal
2475 (if (eq trail 'before)
880e6158 2476 (= (+ (window-pixel-left sub) (window-pixel-width sub))
9aab8e0d 2477 edge)
880e6158 2478 (= (window-pixel-left sub) edge))
9aab8e0d 2479 (if (eq trail 'before)
880e6158 2480 (= (+ (window-pixel-top sub) (window-pixel-height sub))
9aab8e0d 2481 edge)
880e6158
MR
2482 (= (window-pixel-top sub) edge)))
2483 (window-sizable-p sub delta horizontal ignore t))
9aab8e0d
MR
2484 ;; Resize only windows adjacent to EDGE.
2485 (progn
5386012d
MR
2486 (window--resize-this-window
2487 sub delta horizontal ignore t trail edge)
9aab8e0d
MR
2488 (if (and window (eq (window-parent sub) parent))
2489 (progn
2490 ;; Assign new normal sizes.
2491 (set-window-new-normal
880e6158 2492 sub (/ (float (window-new-pixel sub)) parent-total))
9aab8e0d
MR
2493 (set-window-new-normal
2494 window (- (window-normal-size window horizontal)
2495 (- (window-new-normal sub)
2496 (window-normal-size sub horizontal)))))
be7f5545 2497 (window--resize-child-windows-normal
5386012d
MR
2498 parent horizontal sub 0 trail delta))
2499 ;; Return 'normalized to notify `window--resize-siblings' that
9aab8e0d
MR
2500 ;; normal sizes have been already set.
2501 'normalized)
2502 ;; Resize all windows proportionally.
9c52dd5a 2503 (setq sub last)
9aab8e0d
MR
2504 (while sub
2505 (cond
be7f5545
MR
2506 ((or (window--resize-child-windows-skip-p sub)
2507 ;; Ignore windows to skip and fixed-size child windows -
2508 ;; in the latter case make it a window to skip.
9aab8e0d
MR
2509 (and (not ignore)
2510 (window-size-fixed-p sub horizontal)
2511 (set-window-new-normal sub 'ignore))))
2512 ((< delta 0)
2513 ;; When shrinking store the number of lines/cols we can get
2514 ;; from this window here together with the total/normal size
2515 ;; factor.
2516 (set-window-new-normal
2517 sub
2518 (cons
2519 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
880e6158
MR
2520 (window-min-delta sub horizontal ignore trail t nil t)
2521 (- (/ (float (window-size sub horizontal t))
9aab8e0d
MR
2522 parent-total)
2523 (window-normal-size sub horizontal)))))
2524 ((> delta 0)
2525 ;; When enlarging store the total/normal size factor only
2526 (set-window-new-normal
2527 sub
880e6158 2528 (- (/ (float (window-size sub horizontal t))
9aab8e0d
MR
2529 parent-total)
2530 (window-normal-size sub horizontal)))))
2531
9c52dd5a 2532 (setq sub (window-left sub)))
9aab8e0d
MR
2533
2534 (cond
2535 ((< delta 0)
2536 ;; Shrink windows by delta.
2537 (setq best-window t)
2538 (while (and best-window (not (zerop delta)))
9c52dd5a 2539 (setq sub last)
9aab8e0d
MR
2540 (setq best-window nil)
2541 (setq best-value most-negative-fixnum)
2542 (while sub
2543 (when (and (consp (window-new-normal sub))
2544 (not (zerop (car (window-new-normal sub))))
2545 (> (cdr (window-new-normal sub)) best-value))
2546 (setq best-window sub)
2547 (setq best-value (cdr (window-new-normal sub))))
2548
9c52dd5a 2549 (setq sub (window-left sub)))
9aab8e0d
MR
2550
2551 (when best-window
880e6158
MR
2552 (setq best-delta (min (car (window-new-normal best-window))
2553 char-size (- delta)))
2554 (setq delta (+ delta best-delta))
2555 (set-window-new-pixel best-window (- best-delta) t)
2556 (set-window-new-normal
2557 best-window
2558 (if (= (car (window-new-normal best-window)) best-delta)
2559 'skip ; We can't shrink best-window any further.
2560 (cons (1- (car (window-new-normal best-window)))
2561 (- (/ (float (window-new-pixel best-window))
2562 parent-total)
2563 (window-normal-size best-window horizontal))))))))
9aab8e0d
MR
2564 ((> delta 0)
2565 ;; Enlarge windows by delta.
2566 (setq best-window t)
2567 (while (and best-window (not (zerop delta)))
9c52dd5a 2568 (setq sub last)
9aab8e0d
MR
2569 (setq best-window nil)
2570 (setq best-value most-positive-fixnum)
2571 (while sub
2572 (when (and (numberp (window-new-normal sub))
2573 (< (window-new-normal sub) best-value))
2574 (setq best-window sub)
2575 (setq best-value (window-new-normal sub)))
2576
9c52dd5a 2577 (setq sub (window-left sub)))
9aab8e0d
MR
2578
2579 (when best-window
880e6158
MR
2580 (setq best-delta (min delta char-size))
2581 (setq delta (- delta best-delta))
2582 (set-window-new-pixel best-window best-delta t)
2583 (set-window-new-normal
2584 best-window
2585 (- (/ (float (window-new-pixel best-window))
2586 parent-total)
2587 (window-normal-size best-window horizontal)))))))
9aab8e0d
MR
2588
2589 (when best-window
9c52dd5a 2590 (setq sub last)
9aab8e0d
MR
2591 (while sub
2592 (when (or (consp (window-new-normal sub))
2593 (numberp (window-new-normal sub)))
d615d6d2 2594 ;; Reset new normal size fields so `window-resize-apply'
9aab8e0d
MR
2595 ;; won't use them to apply new sizes.
2596 (set-window-new-normal sub))
2597
2598 (unless (eq (window-new-normal sub) 'ignore)
be7f5545 2599 ;; Resize this window's child windows (back-engineering
9aab8e0d 2600 ;; delta from sub's old and new total sizes).
880e6158
MR
2601 (let ((delta (- (window-new-pixel sub)
2602 (window-size sub horizontal t))))
9aab8e0d
MR
2603 (unless (and (zerop delta) (not trail))
2604 ;; For the TRAIL non-nil case we have to resize SUB
2605 ;; recursively even if it's size does not change.
5386012d 2606 (window--resize-this-window
9aab8e0d 2607 sub delta horizontal ignore nil trail edge))))
9c52dd5a 2608 (setq sub (window-left sub)))))))
9aab8e0d 2609
880e6158
MR
2610(defun window--resize-siblings (window delta &optional horizontal ignore trail edge char-size)
2611 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
9aab8e0d 2612Optional argument HORIZONTAL non-nil means resize other windows
880e6158 2613when WINDOW is resized horizontally by DELTA pixels. WINDOW
9aab8e0d
MR
2614itself is not resized by this function.
2615
2116e93c 2616Optional argument IGNORE non-nil means ignore restrictions
9aab8e0d 2617imposed by fixed size windows, `window-min-height' or
2116e93c 2618`window-min-width' settings. If IGNORE equals `safe', live
9aab8e0d 2619windows may get as small as `window-safe-min-height' lines and
2116e93c
EZ
2620`window-safe-min-width' columns. If IGNORE is a window, ignore
2621restrictions for that window only. Any other non-nil value means
2622ignore all of the above restrictions for all windows.
9aab8e0d
MR
2623
2624Optional arguments TRAIL and EDGE, when non-nil, refine the set
2625of windows that shall be resized. If TRAIL equals `before',
2626resize only windows on the left or above EDGE. If TRAIL equals
2627`after', resize only windows on the right or below EDGE. Also,
2628preferably only resize windows adjacent to EDGE."
2629 (when (window-parent window)
2630 (let* ((parent (window-parent window))
2631 (sub (window-child parent)))
3d8daefe 2632 (if (window-combined-p sub horizontal)
9aab8e0d
MR
2633 ;; In an iso-combination try to extract DELTA from WINDOW's
2634 ;; siblings.
cb882333 2635 (let ((skip (eq trail 'after))
9aab8e0d
MR
2636 this-delta other-delta)
2637 ;; Decide which windows shall be left alone.
2638 (while sub
2639 (cond
2640 ((eq sub window)
2641 ;; Make sure WINDOW is left alone when
2642 ;; resizing its siblings.
2643 (set-window-new-normal sub 'ignore)
2644 (setq skip (eq trail 'before)))
2645 (skip
2646 ;; Make sure this sibling is left alone when
2647 ;; resizing its siblings.
2648 (set-window-new-normal sub 'ignore))
842e3a93 2649 ((or (window--size-ignore-p sub ignore)
9aab8e0d
MR
2650 (not (window-size-fixed-p sub horizontal)))
2651 ;; Set this-delta to t to signal that we found a sibling
2652 ;; of WINDOW whose size is not fixed.
2653 (setq this-delta t)))
2654
2655 (setq sub (window-right sub)))
2656
2657 ;; Set this-delta to what we can get from WINDOW's siblings.
880e6158 2658 (if (= (- delta) (window-size window horizontal t))
9aab8e0d 2659 ;; A deletion, presumably. We must handle this case
2cffd681 2660 ;; specially since `window--resizable' can't be used.
9aab8e0d
MR
2661 (if this-delta
2662 ;; There's at least one resizable sibling we can
2663 ;; give WINDOW's size to.
2664 (setq this-delta delta)
2665 ;; No resizable sibling exists.
2666 (setq this-delta 0))
2667 ;; Any other form of resizing.
2668 (setq this-delta
880e6158
MR
2669 (window--resizable
2670 window delta horizontal ignore trail t nil t)))
9aab8e0d
MR
2671
2672 ;; Set other-delta to what we still have to get from
2673 ;; ancestor windows of parent.
2674 (setq other-delta (- delta this-delta))
2675 (unless (zerop other-delta)
2676 ;; Unless we got everything from WINDOW's siblings, PARENT
2677 ;; must be resized by other-delta lines or columns.
880e6158 2678 (set-window-new-pixel parent other-delta 'add))
9aab8e0d
MR
2679
2680 (if (zerop this-delta)
2681 ;; We haven't got anything from WINDOW's siblings but we
2682 ;; must update the normal sizes to respect other-delta.
be7f5545 2683 (window--resize-child-windows-normal
9aab8e0d
MR
2684 parent horizontal window this-delta trail other-delta)
2685 ;; We did get something from WINDOW's siblings which means
be7f5545
MR
2686 ;; we have to resize their child windows.
2687 (unless (eq (window--resize-child-windows
5386012d 2688 parent (- this-delta) horizontal
880e6158 2689 window ignore trail edge char-size)
be7f5545 2690 ;; If `window--resize-child-windows' returns
5386012d
MR
2691 ;; 'normalized, this means it has set the
2692 ;; normal sizes already.
9aab8e0d
MR
2693 'normalized)
2694 ;; Set the normal sizes.
be7f5545 2695 (window--resize-child-windows-normal
9aab8e0d
MR
2696 parent horizontal window this-delta trail other-delta))
2697 ;; Set DELTA to what we still have to get from ancestor
2698 ;; windows.
2699 (setq delta other-delta)))
2700
2701 ;; In an ortho-combination all siblings of WINDOW must be
2702 ;; resized by DELTA.
880e6158 2703 (set-window-new-pixel parent delta 'add)
9aab8e0d
MR
2704 (while sub
2705 (unless (eq sub window)
880e6158
MR
2706 (window--resize-this-window
2707 sub delta horizontal ignore t))
9aab8e0d
MR
2708 (setq sub (window-right sub))))
2709
2710 (unless (zerop delta)
2711 ;; "Go up."
5386012d 2712 (window--resize-siblings
880e6158 2713 parent delta horizontal ignore trail edge char-size)))))
9aab8e0d 2714
880e6158
MR
2715(defun window--resize-this-window (window delta &optional horizontal ignore add trail edge char-size)
2716 "Resize WINDOW vertically by DELTA pixels.
9aab8e0d 2717Optional argument HORIZONTAL non-nil means resize WINDOW
880e6158 2718horizontally by DELTA pixels.
9aab8e0d 2719
2116e93c 2720Optional argument IGNORE non-nil means ignore restrictions
9aab8e0d 2721imposed by fixed size windows, `window-min-height' or
2116e93c 2722`window-min-width' settings. If IGNORE equals `safe', live
9aab8e0d 2723windows may get as small as `window-safe-min-height' lines and
2116e93c
EZ
2724`window-safe-min-width' columns. If IGNORE is a window, ignore
2725restrictions for that window only. Any other non-nil value
2726means ignore all of the above restrictions for all windows.
9aab8e0d
MR
2727
2728Optional argument ADD non-nil means add DELTA to the new total
2729size of WINDOW.
2730
2731Optional arguments TRAIL and EDGE, when non-nil, refine the set
2732of windows that shall be resized. If TRAIL equals `before',
2733resize only windows on the left or above EDGE. If TRAIL equals
2734`after', resize only windows on the right or below EDGE. Also,
2735preferably only resize windows adjacent to EDGE.
2736
880e6158
MR
2737If the optional argument CHAR-SIZE is a positive integer, it specifies
2738the number of pixels by which windows are incrementally resized.
2739If CHAR-SIZE is nil, this means to use the value of
2740`frame-char-height' or `frame-char-width' of WINDOW's frame.
2741
be7f5545 2742This function recursively resizes WINDOW's child windows to fit the
2cffd681 2743new size. Make sure that WINDOW is `window--resizable' before
9aab8e0d
MR
2744calling this function. Note that this function does not resize
2745siblings of WINDOW or WINDOW's parent window. You have to
d615d6d2 2746eventually call `window-resize-apply' in order to make resizing
9aab8e0d
MR
2747actually take effect."
2748 (when add
2749 ;; Add DELTA to the new total size of WINDOW.
880e6158 2750 (set-window-new-pixel window delta t))
387522b2 2751
9aab8e0d
MR
2752 (let ((sub (window-child window)))
2753 (cond
2754 ((not sub))
3d8daefe 2755 ((window-combined-p sub horizontal)
be7f5545 2756 ;; In an iso-combination resize child windows according to their
9aab8e0d 2757 ;; normal sizes.
be7f5545 2758 (window--resize-child-windows
880e6158 2759 window delta horizontal nil ignore trail edge char-size))
be7f5545 2760 ;; In an ortho-combination resize each child window by DELTA.
9aab8e0d
MR
2761 (t
2762 (while sub
5386012d 2763 (window--resize-this-window
880e6158 2764 sub delta horizontal ignore t trail edge char-size)
9aab8e0d
MR
2765 (setq sub (window-right sub)))))))
2766
880e6158 2767(defun window--resize-root-window (window delta horizontal ignore pixelwise)
9aab8e0d
MR
2768 "Resize root window WINDOW vertically by DELTA lines.
2769HORIZONTAL non-nil means resize root window WINDOW horizontally
2770by DELTA columns.
2771
2772IGNORE non-nil means ignore any restrictions imposed by fixed
2773size windows, `window-min-height' or `window-min-width' settings.
2774
2775This function is only called by the frame resizing routines. It
2776resizes windows proportionally and never deletes any windows."
880e6158
MR
2777 (when (and (windowp window) (numberp delta))
2778 (let ((pixel-delta
2779 (if pixelwise
2780 delta
2781 (window--size-to-pixel window delta horizontal))))
2782 (when (window-sizable-p window pixel-delta horizontal ignore t)
2783 (window--resize-reset (window-frame window) horizontal)
2784 (window--resize-this-window
2785 window pixel-delta horizontal ignore t)))))
9aab8e0d 2786
880e6158 2787(defun window--resize-root-window-vertically (window delta pixelwise)
9aab8e0d
MR
2788 "Resize root window WINDOW vertically by DELTA lines.
2789If DELTA is less than zero and we can't shrink WINDOW by DELTA
2790lines, shrink it as much as possible. If DELTA is greater than
be7f5545 2791zero, this function can resize fixed-size windows in order to
880e6158
MR
2792recover the necessary lines. Return the number of lines that
2793were recovered.
9aab8e0d 2794
f224e500 2795Third argument PIXELWISE non-nil means to interpret DELTA as
880e6158 2796pixels and return the number of pixels that were recovered.
9aab8e0d 2797
880e6158
MR
2798This function is called by the minibuffer window resizing
2799routines."
2800 (let* ((frame (window-frame window))
2801 (pixel-delta
2802 (cond
2803 (pixelwise
2804 delta)
2805 ((numberp delta)
2806 (* (frame-char-height frame) delta))
2807 (t 0)))
2808 ignore)
9c52dd5a 2809 (cond
880e6158
MR
2810 ((zerop pixel-delta))
2811 ((< pixel-delta 0)
2812 (setq pixel-delta (window-sizable window pixel-delta nil nil pixelwise))
9c52dd5a
MR
2813 (window--resize-reset frame)
2814 ;; When shrinking the root window, emulate an edge drag in order
2815 ;; to not resize other windows if we can avoid it (Bug#12419).
2816 (window--resize-this-window
880e6158
MR
2817 window pixel-delta nil ignore t 'before
2818 (+ (window-pixel-top window) (window-pixel-height window)))
9c52dd5a
MR
2819 ;; Don't record new normal sizes to make sure that shrinking back
2820 ;; proportionally works as intended.
2821 (walk-window-tree
2822 (lambda (window) (set-window-new-normal window 'ignore)) frame t))
880e6158 2823 ((> pixel-delta 0)
9c52dd5a 2824 (window--resize-reset frame)
880e6158 2825 (unless (window-sizable window pixel-delta nil nil pixelwise)
9c52dd5a
MR
2826 (setq ignore t))
2827 ;; When growing the root window, resize proportionally. This
2828 ;; should give windows back their original sizes (hopefully).
880e6158
MR
2829 (window--resize-this-window
2830 window pixel-delta nil ignore t)))
9c52dd5a 2831 ;; Return the possibly adjusted DELTA.
880e6158
MR
2832 (if pixelwise
2833 pixel-delta
2834 (/ pixel-delta (frame-char-height frame)))))
9aab8e0d 2835
880e6158 2836(defun adjust-window-trailing-edge (window delta &optional horizontal pixelwise)
562dd5e9
MR
2837 "Move WINDOW's bottom edge by DELTA lines.
2838Optional argument HORIZONTAL non-nil means move WINDOW's right
85c2386b
MR
2839edge by DELTA columns. WINDOW must be a valid window and
2840defaults to the selected one.
562dd5e9 2841
880e6158
MR
2842Optional argument PIXELWISE non-nil means interpret DELTA as
2843number of pixels.
2844
2116e93c 2845If DELTA is greater than zero, move the edge downwards or to the
562dd5e9
MR
2846right. If DELTA is less than zero, move the edge upwards or to
2847the left. If the edge can't be moved by DELTA lines or columns,
2848move it as far as possible in the desired direction."
447f16b8 2849 (setq window (window-normalize-window window))
41cfe0cb
MR
2850 (let* ((frame (window-frame window))
2851 (minibuffer-window (minibuffer-window frame))
2852 (right window)
2853 left this-delta min-delta max-delta)
880e6158
MR
2854
2855 (unless pixelwise
2856 (setq pixelwise t)
2857 (setq delta (* delta (frame-char-size window horizontal))))
2858
562dd5e9 2859 ;; Find the edge we want to move.
3d8daefe 2860 (while (and (or (not (window-combined-p right horizontal))
562dd5e9
MR
2861 (not (window-right right)))
2862 (setq right (window-parent right))))
2863 (cond
41cfe0cb
MR
2864 ((and (not right) (not horizontal)
2865 ;; Resize the minibuffer window if it's on the same frame as
2866 ;; and immediately below WINDOW and it's either active or
2867 ;; `resize-mini-windows' is nil.
2868 (eq (window-frame minibuffer-window) frame)
880e6158
MR
2869 (= (nth 1 (window-pixel-edges minibuffer-window))
2870 (nth 3 (window-pixel-edges window)))
41cfe0cb
MR
2871 (or (not resize-mini-windows)
2872 (eq minibuffer-window (active-minibuffer-window))))
2873 (window--resize-mini-window minibuffer-window (- delta)))
562dd5e9
MR
2874 ((or (not (setq left right)) (not (setq right (window-right right))))
2875 (if horizontal
2876 (error "No window on the right of this one")
2877 (error "No window below this one")))
2878 (t
2879 ;; Set LEFT to the first resizable window on the left. This step is
2880 ;; needed to handle fixed-size windows.
2881 (while (and left (window-size-fixed-p left horizontal))
2882 (setq left
2883 (or (window-left left)
2884 (progn
2885 (while (and (setq left (window-parent left))
3d8daefe 2886 (not (window-combined-p left horizontal))))
562dd5e9
MR
2887 (window-left left)))))
2888 (unless left
2889 (if horizontal
2890 (error "No resizable window on the left of this one")
2891 (error "No resizable window above this one")))
2892
2893 ;; Set RIGHT to the first resizable window on the right. This step
2894 ;; is needed to handle fixed-size windows.
2895 (while (and right (window-size-fixed-p right horizontal))
2896 (setq right
2897 (or (window-right right)
2898 (progn
2899 (while (and (setq right (window-parent right))
3d8daefe 2900 (not (window-combined-p right horizontal))))
562dd5e9
MR
2901 (window-right right)))))
2902 (unless right
2903 (if horizontal
2904 (error "No resizable window on the right of this one")
2905 (error "No resizable window below this one")))
2906
2907 ;; LEFT and RIGHT (which might be both internal windows) are now the
2908 ;; two windows we want to resize.
2909 (cond
2910 ((> delta 0)
880e6158
MR
2911 (setq max-delta
2912 (window--max-delta-1
2913 left 0 horizontal nil 'after nil pixelwise))
2914 (setq min-delta
2915 (window--min-delta-1
2916 right (- delta) horizontal nil 'before nil pixelwise))
562dd5e9
MR
2917 (when (or (< max-delta delta) (> min-delta (- delta)))
2918 ;; We can't get the whole DELTA - move as far as possible.
2919 (setq delta (min max-delta (- min-delta))))
2920 (unless (zerop delta)
2921 ;; Start resizing.
5386012d 2922 (window--resize-reset frame horizontal)
562dd5e9 2923 ;; Try to enlarge LEFT first.
880e6158
MR
2924 (setq this-delta (window--resizable
2925 left delta horizontal nil nil nil nil pixelwise))
562dd5e9 2926 (unless (zerop this-delta)
5386012d 2927 (window--resize-this-window
562dd5e9
MR
2928 left this-delta horizontal nil t 'before
2929 (if horizontal
880e6158
MR
2930 (+ (window-pixel-left left) (window-pixel-width left))
2931 (+ (window-pixel-top left) (window-pixel-height left)))))
562dd5e9 2932 ;; Shrink windows on right of LEFT.
5386012d 2933 (window--resize-siblings
562dd5e9
MR
2934 left delta horizontal nil 'after
2935 (if horizontal
880e6158
MR
2936 (window-pixel-left right)
2937 (window-pixel-top right)))))
562dd5e9 2938 ((< delta 0)
880e6158
MR
2939 (setq max-delta
2940 (window--max-delta-1
2941 right 0 horizontal nil 'before nil pixelwise))
2942 (setq min-delta
2943 (window--min-delta-1
2944 left delta horizontal nil 'after nil pixelwise))
562dd5e9
MR
2945 (when (or (< max-delta (- delta)) (> min-delta delta))
2946 ;; We can't get the whole DELTA - move as far as possible.
2947 (setq delta (max (- max-delta) min-delta)))
2948 (unless (zerop delta)
2949 ;; Start resizing.
5386012d 2950 (window--resize-reset frame horizontal)
562dd5e9 2951 ;; Try to enlarge RIGHT.
880e6158
MR
2952 (setq this-delta
2953 (window--resizable
2954 right (- delta) horizontal nil nil nil nil pixelwise))
562dd5e9 2955 (unless (zerop this-delta)
5386012d 2956 (window--resize-this-window
562dd5e9
MR
2957 right this-delta horizontal nil t 'after
2958 (if horizontal
880e6158
MR
2959 (window-pixel-left right)
2960 (window-pixel-top right))))
562dd5e9 2961 ;; Shrink windows on left of RIGHT.
5386012d 2962 (window--resize-siblings
562dd5e9
MR
2963 right (- delta) horizontal nil 'before
2964 (if horizontal
880e6158
MR
2965 (+ (window-pixel-left left) (window-pixel-width left))
2966 (+ (window-pixel-top left) (window-pixel-height left)))))))
562dd5e9
MR
2967 (unless (zerop delta)
2968 ;; Don't report an error in the standard case.
880e6158
MR
2969 (when (window--resize-apply-p frame horizontal)
2970 (if (window-resize-apply frame horizontal)
2971 (progn
2972 (window--pixel-to-total frame horizontal)
2973 (run-window-configuration-change-hook frame))
2974 ;; But do report an error if applying the changes fails.
2975 (error "Failed adjusting window %s" window))))))))
562dd5e9
MR
2976
2977(defun enlarge-window (delta &optional horizontal)
2116e93c 2978 "Make the selected window DELTA lines taller.
562dd5e9
MR
2979Interactively, if no argument is given, make the selected window
2980one line taller. If optional argument HORIZONTAL is non-nil,
2981make selected window wider by DELTA columns. If DELTA is
0ff7851c 2982negative, shrink selected window by -DELTA lines or columns."
562dd5e9 2983 (interactive "p")
feeb6f53
MR
2984 (let ((minibuffer-window (minibuffer-window)))
2985 (cond
2986 ((zerop delta))
2987 ((window-size-fixed-p nil horizontal)
2988 (error "Selected window has fixed size"))
2989 ((window-minibuffer-p)
2990 (if horizontal
2991 (error "Cannot resize minibuffer window horizontally")
2992 (window--resize-mini-window (selected-window) delta)))
2993 ((and (not horizontal)
2994 (window-full-height-p)
2995 (eq (window-frame minibuffer-window) (selected-frame))
2996 (not resize-mini-windows))
2997 ;; If the selected window is full height and `resize-mini-windows'
2998 ;; is nil, resize the minibuffer window.
2999 (window--resize-mini-window minibuffer-window (- delta)))
880e6158 3000 ((window--resizable-p nil delta horizontal)
feeb6f53
MR
3001 (window-resize nil delta horizontal))
3002 (t
3003 (window-resize
3004 nil (if (> delta 0)
3005 (window-max-delta nil horizontal)
3006 (- (window-min-delta nil horizontal)))
3007 horizontal)))))
562dd5e9
MR
3008
3009(defun shrink-window (delta &optional horizontal)
2116e93c 3010 "Make the selected window DELTA lines smaller.
562dd5e9
MR
3011Interactively, if no argument is given, make the selected window
3012one line smaller. If optional argument HORIZONTAL is non-nil,
3013make selected window narrower by DELTA columns. If DELTA is
3014negative, enlarge selected window by -DELTA lines or columns.
0ff7851c 3015Also see the `window-min-height' variable."
562dd5e9 3016 (interactive "p")
feeb6f53
MR
3017 (let ((minibuffer-window (minibuffer-window)))
3018 (cond
3019 ((zerop delta))
3020 ((window-size-fixed-p nil horizontal)
3021 (error "Selected window has fixed size"))
3022 ((window-minibuffer-p)
3023 (if horizontal
3024 (error "Cannot resize minibuffer window horizontally")
3025 (window--resize-mini-window (selected-window) (- delta))))
3026 ((and (not horizontal)
3027 (window-full-height-p)
3028 (eq (window-frame minibuffer-window) (selected-frame))
3029 (not resize-mini-windows))
3030 ;; If the selected window is full height and `resize-mini-windows'
3031 ;; is nil, resize the minibuffer window.
3032 (window--resize-mini-window minibuffer-window delta))
880e6158 3033 ((window--resizable-p nil (- delta) horizontal)
feeb6f53
MR
3034 (window-resize nil (- delta) horizontal))
3035 (t
3036 (window-resize
3037 nil (if (> delta 0)
3038 (- (window-min-delta nil horizontal))
3039 (window-max-delta nil horizontal))
3040 horizontal)))))
562dd5e9 3041
2d6b6005 3042(defun maximize-window (&optional window)
562dd5e9
MR
3043 "Maximize WINDOW.
3044Make WINDOW as large as possible without deleting any windows.
880e6158
MR
3045WINDOW must be a valid window and defaults to the selected one.
3046
3047If the option `window-resize-pixelwise' is non-nil maximize
3048WINDOW pixelwise."
562dd5e9 3049 (interactive)
447f16b8 3050 (setq window (window-normalize-window window))
880e6158
MR
3051 (window-resize
3052 window (window-max-delta window nil nil nil nil nil window-resize-pixelwise)
3053 nil nil window-resize-pixelwise)
3054 (window-resize
3055 window (window-max-delta window t nil nil nil nil window-resize-pixelwise)
3056 t nil window-resize-pixelwise))
3057
2d6b6005 3058(defun minimize-window (&optional window)
562dd5e9
MR
3059 "Minimize WINDOW.
3060Make WINDOW as small as possible without deleting any windows.
880e6158
MR
3061WINDOW must be a valid window and defaults to the selected one.
3062
3063If the option `window-resize-pixelwise' is non-nil minimize
3064WINDOW pixelwise."
562dd5e9 3065 (interactive)
447f16b8 3066 (setq window (window-normalize-window window))
880e6158
MR
3067 (window-resize
3068 window
3069 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise))
3070 nil nil window-resize-pixelwise)
3071 (window-resize
3072 window
3073 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise))
3074 t nil window-resize-pixelwise))
562dd5e9 3075\f
4b0d61e3 3076(defun frame-root-window-p (window)
9aab8e0d
MR
3077 "Return non-nil if WINDOW is the root window of its frame."
3078 (eq window (frame-root-window window)))
6198ccd0 3079
5e92ca23
MR
3080(defun window--subtree (window &optional next)
3081 "Return window subtree rooted at WINDOW.
3082Optional argument NEXT non-nil means include WINDOW's right
6198ccd0
MR
3083siblings in the return value.
3084
3085See the documentation of `window-tree' for a description of the
3086return value."
3087 (let (list)
3088 (while window
3089 (setq list
3090 (cons
3091 (cond
d68443dc 3092 ((window-top-child window)
6198ccd0 3093 (cons t (cons (window-edges window)
5e92ca23 3094 (window--subtree (window-top-child window) t))))
d68443dc 3095 ((window-left-child window)
6198ccd0 3096 (cons nil (cons (window-edges window)
5e92ca23 3097 (window--subtree (window-left-child window) t))))
6198ccd0
MR
3098 (t window))
3099 list))
d68443dc 3100 (setq window (when next (window-next-sibling window))))
6198ccd0
MR
3101 (nreverse list)))
3102
3103(defun window-tree (&optional frame)
3104 "Return the window tree of frame FRAME.
3105FRAME must be a live frame and defaults to the selected frame.
3106The return value is a list of the form (ROOT MINI), where ROOT
3107represents the window tree of the frame's root window, and MINI
3108is the frame's minibuffer window.
3109
3110If the root window is not split, ROOT is the root window itself.
3111Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3112for a horizontal split, and t for a vertical split. EDGES gives
be7f5545
MR
3113the combined size and position of the child windows in the split,
3114and the rest of the elements are the child windows in the split.
3115Each of the child windows may again be a window or a list
382c953b 3116representing a window split, and so on. EDGES is a list (LEFT
6198ccd0 3117TOP RIGHT BOTTOM) as returned by `window-edges'."
5386012d 3118 (setq frame (window-normalize-frame frame))
5e92ca23 3119 (window--subtree (frame-root-window frame) t))
9aab8e0d 3120\f
9397e56f
MR
3121(defun other-window (count &optional all-frames)
3122 "Select another window in cyclic ordering of windows.
3123COUNT specifies the number of windows to skip, starting with the
3124selected window, before making the selection. If COUNT is
3125positive, skip COUNT windows forwards. If COUNT is negative,
3126skip -COUNT windows backwards. COUNT zero means do not skip any
3127window, so select the selected window. In an interactive call,
3128COUNT is the numeric prefix argument. Return nil.
3129
9a9e9ef0
MR
3130If the `other-window' parameter of the selected window is a
3131function and `ignore-window-parameters' is nil, call that
3132function with the arguments COUNT and ALL-FRAMES.
9397e56f
MR
3133
3134This function does not select a window whose `no-other-window'
3135window parameter is non-nil.
3136
3137This function uses `next-window' for finding the window to
3138select. The argument ALL-FRAMES has the same meaning as in
3139`next-window', but the MINIBUF argument of `next-window' is
3140always effectively nil."
3141 (interactive "p")
3142 (let* ((window (selected-window))
3143 (function (and (not ignore-window-parameters)
3144 (window-parameter window 'other-window)))
3145 old-window old-count)
3146 (if (functionp function)
3147 (funcall function count all-frames)
3148 ;; `next-window' and `previous-window' may return a window we are
3149 ;; not allowed to select. Hence we need an exit strategy in case
3150 ;; all windows are non-selectable.
3151 (catch 'exit
3152 (while (> count 0)
3153 (setq window (next-window window nil all-frames))
3154 (cond
3155 ((eq window old-window)
3156 (when (= count old-count)
3157 ;; Keep out of infinite loops. When COUNT has not changed
3158 ;; since we last looked at `window' we're probably in one.
3159 (throw 'exit nil)))
3160 ((window-parameter window 'no-other-window)
3161 (unless old-window
3162 ;; The first non-selectable window `next-window' got us:
3163 ;; Remember it and the current value of COUNT.
3164 (setq old-window window)
3165 (setq old-count count)))
3166 (t
3167 (setq count (1- count)))))
3168 (while (< count 0)
3169 (setq window (previous-window window nil all-frames))
3170 (cond
3171 ((eq window old-window)
3172 (when (= count old-count)
3173 ;; Keep out of infinite loops. When COUNT has not changed
3174 ;; since we last looked at `window' we're probably in one.
3175 (throw 'exit nil)))
3176 ((window-parameter window 'no-other-window)
3177 (unless old-window
3178 ;; The first non-selectable window `previous-window' got
3179 ;; us: Remember it and the current value of COUNT.
3180 (setq old-window window)
3181 (setq old-count count)))
3182 (t
3183 (setq count (1+ count)))))
3184
3185 (select-window window)
3186 ;; Always return nil.
3187 nil))))
3188
387522b2
MR
3189;; This should probably return non-nil when the selected window is part
3190;; of an atomic window whose root is the frame's root window.
3191(defun one-window-p (&optional nomini all-frames)
3192 "Return non-nil if the selected window is the only window.
3193Optional arg NOMINI non-nil means don't count the minibuffer
3194even if it is active. Otherwise, the minibuffer is counted
3195when it is active.
3196
3197Optional argument ALL-FRAMES specifies the set of frames to
3198consider, see also `next-window'. ALL-FRAMES nil or omitted
3199means consider windows on the selected frame only, plus the
3200minibuffer window if specified by the NOMINI argument. If the
3201minibuffer counts, consider all windows on all frames that share
3202that minibuffer too. The remaining non-nil values of ALL-FRAMES
3203with a special meaning are:
3204
3205- t means consider all windows on all existing frames.
3206
3207- `visible' means consider all windows on all visible frames on
3208 the current terminal.
3209
3210- 0 (the number zero) means consider all windows on all visible
3211 and iconified frames on the current terminal.
3212
3213- A frame means consider all windows on that frame only.
3214
3215Anything else means consider all windows on the selected frame
3216and no others."
3217 (let ((base-window (selected-window)))
3218 (if (and nomini (eq base-window (minibuffer-window)))
3219 (setq base-window (next-window base-window)))
3220 (eq base-window
3221 (next-window base-window (if nomini 'arg) all-frames))))
3c448ab6 3222\f
9aab8e0d 3223;;; Deleting windows.
1b36ed6a 3224(defun window-deletable-p (&optional window)
9aab8e0d 3225 "Return t if WINDOW can be safely deleted from its frame.
85c2386b 3226WINDOW must be a valid window and defaults to the selected one.
c660a885 3227Return 'frame if deleting WINDOW should also delete its frame."
447f16b8 3228 (setq window (window-normalize-window window))
8b0874b5 3229
c660a885 3230 (unless (or ignore-window-parameters
f224e500 3231 (eq (window-parameter window 'delete-window) t))
9aab8e0d
MR
3232 ;; Handle atomicity.
3233 (when (window-parameter window 'window-atom)
3234 (setq window (window-atom-root window))))
8b0874b5 3235
caceae25 3236 (let ((frame (window-frame window)))
9aab8e0d
MR
3237 (cond
3238 ((frame-root-window-p window)
85e9c04b 3239 ;; WINDOW's frame can be deleted only if there are other frames
46b7967e
TN
3240 ;; on the same terminal, and it does not contain the active
3241 ;; minibuffer.
3242 (unless (or (eq frame (next-frame frame 0))
c660a885
MR
3243 ;; We can delete our frame only if no other frame
3244 ;; currently uses our minibuffer window.
3245 (catch 'other
3246 (dolist (other (frame-list))
3247 (when (and (not (eq other frame))
3248 (eq (window-frame (minibuffer-window other))
3249 frame))
3250 (throw 'other t))))
46b7967e
TN
3251 (let ((minibuf (active-minibuffer-window)))
3252 (and minibuf (eq frame (window-frame minibuf)))))
85e9c04b 3253 'frame))
1b36ed6a 3254 ((or ignore-window-parameters
caceae25
MR
3255 (not (eq window (window--major-non-side-window frame))))
3256 ;; WINDOW can be deleted unless it is the major non-side window of
3257 ;; its frame.
1f3c99ca 3258 t))))
9aab8e0d 3259
be7f5545
MR
3260(defun window--in-subtree-p (window root)
3261 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
3262 (or (eq window root)
3263 (let ((parent (window-parent window)))
9aab8e0d
MR
3264 (catch 'done
3265 (while parent
be7f5545 3266 (if (eq parent root)
9aab8e0d
MR
3267 (throw 'done t)
3268 (setq parent (window-parent parent))))))))
3269
562dd5e9
MR
3270(defun delete-window (&optional window)
3271 "Delete WINDOW.
85c2386b
MR
3272WINDOW must be a valid window and defaults to the selected one.
3273Return nil.
562dd5e9
MR
3274
3275If the variable `ignore-window-parameters' is non-nil or the
3276`delete-window' parameter of WINDOW equals t, do not process any
3277parameters of WINDOW. Otherwise, if the `delete-window'
3278parameter of WINDOW specifies a function, call that function with
3279WINDOW as its sole argument and return the value returned by that
3280function.
3281
3282Otherwise, if WINDOW is part of an atomic window, call
3283`delete-window' with the root of the atomic window as its
85c2386b
MR
3284argument. Signal an error if WINDOW is either the only window on
3285its frame, the last non-side window, or part of an atomic window
3286that is its frame's root window."
562dd5e9 3287 (interactive)
447f16b8 3288 (setq window (window-normalize-window window))
562dd5e9
MR
3289 (let* ((frame (window-frame window))
3290 (function (window-parameter window 'delete-window))
3291 (parent (window-parent window))
3292 atom-root)
54f9154c 3293 (window--check frame)
562dd5e9
MR
3294 (catch 'done
3295 ;; Handle window parameters.
3296 (cond
3297 ;; Ignore window parameters if `ignore-window-parameters' tells
3298 ;; us so or `delete-window' equals t.
3299 ((or ignore-window-parameters (eq function t)))
3300 ((functionp function)
3301 ;; The `delete-window' parameter specifies the function to call.
3302 ;; If that function is `ignore' nothing is done. It's up to the
3303 ;; function called here to avoid infinite recursion.
3304 (throw 'done (funcall function window)))
3305 ((and (window-parameter window 'window-atom)
3306 (setq atom-root (window-atom-root window))
3307 (not (eq atom-root window)))
caceae25
MR
3308 (if (eq atom-root (frame-root-window frame))
3309 (error "Root of atomic window is root window of its frame")
3310 (throw 'done (delete-window atom-root))))
562dd5e9 3311 ((not parent)
caceae25
MR
3312 (error "Attempt to delete minibuffer or sole ordinary window"))
3313 ((eq window (window--major-non-side-window frame))
3314 (error "Attempt to delete last non-side window")))
562dd5e9 3315
d68443dc 3316 (let* ((horizontal (window-left-child parent))
880e6158 3317 (size (window-size window horizontal t))
562dd5e9 3318 (frame-selected
be7f5545 3319 (window--in-subtree-p (frame-selected-window frame) window))
562dd5e9
MR
3320 ;; Emacs 23 preferably gives WINDOW's space to its left
3321 ;; sibling.
3322 (sibling (or (window-left window) (window-right window))))
5386012d 3323 (window--resize-reset frame horizontal)
562dd5e9 3324 (cond
a0c2d0ae 3325 ((and (not window-combination-resize)
880e6158 3326 sibling (window-sizable-p sibling size horizontal nil t))
562dd5e9 3327 ;; Resize WINDOW's sibling.
5386012d 3328 (window--resize-this-window sibling size horizontal nil t)
562dd5e9
MR
3329 (set-window-new-normal
3330 sibling (+ (window-normal-size sibling horizontal)
3331 (window-normal-size window horizontal))))
880e6158 3332 ((window--resizable-p window (- size) horizontal nil nil nil t t)
562dd5e9 3333 ;; Can do without resizing fixed-size windows.
5386012d 3334 (window--resize-siblings window (- size) horizontal))
562dd5e9
MR
3335 (t
3336 ;; Can't do without resizing fixed-size windows.
5386012d 3337 (window--resize-siblings window (- size) horizontal t)))
562dd5e9
MR
3338 ;; Actually delete WINDOW.
3339 (delete-window-internal window)
880e6158 3340 (window--pixel-to-total frame horizontal)
562dd5e9
MR
3341 (when (and frame-selected
3342 (window-parameter
3343 (frame-selected-window frame) 'no-other-window))
3344 ;; `delete-window-internal' has selected a window that should
3345 ;; not be selected, fix this here.
3346 (other-window -1 frame))
3347 (run-window-configuration-change-hook frame)
54f9154c 3348 (window--check frame)
562dd5e9
MR
3349 ;; Always return nil.
3350 nil))))
3351
3352(defun delete-other-windows (&optional window)
3353 "Make WINDOW fill its frame.
85c2386b 3354WINDOW must be a valid window and defaults to the selected one.
562dd5e9
MR
3355Return nil.
3356
3357If the variable `ignore-window-parameters' is non-nil or the
3358`delete-other-windows' parameter of WINDOW equals t, do not
3359process any parameters of WINDOW. Otherwise, if the
3360`delete-other-windows' parameter of WINDOW specifies a function,
3361call that function with WINDOW as its sole argument and return
3362the value returned by that function.
3363
3364Otherwise, if WINDOW is part of an atomic window, call this
3365function with the root of the atomic window as its argument. If
3366WINDOW is a non-side window, make WINDOW the only non-side window
382c953b 3367on the frame. Side windows are not deleted. If WINDOW is a side
562dd5e9
MR
3368window signal an error."
3369 (interactive)
447f16b8 3370 (setq window (window-normalize-window window))
562dd5e9
MR
3371 (let* ((frame (window-frame window))
3372 (function (window-parameter window 'delete-other-windows))
3373 (window-side (window-parameter window 'window-side))
3374 atom-root side-main)
54f9154c 3375 (window--check frame)
562dd5e9
MR
3376 (catch 'done
3377 (cond
3378 ;; Ignore window parameters if `ignore-window-parameters' is t or
3379 ;; `delete-other-windows' is t.
3380 ((or ignore-window-parameters (eq function t)))
3381 ((functionp function)
3382 ;; The `delete-other-windows' parameter specifies the function
3383 ;; to call. If the function is `ignore' no windows are deleted.
3384 ;; It's up to the function called to avoid infinite recursion.
3385 (throw 'done (funcall function window)))
3386 ((and (window-parameter window 'window-atom)
3387 (setq atom-root (window-atom-root window))
3388 (not (eq atom-root window)))
caceae25
MR
3389 (if (eq atom-root (frame-root-window frame))
3390 (error "Root of atomic window is root window of its frame")
3391 (throw 'done (delete-other-windows atom-root))))
562dd5e9 3392 ((memq window-side window-sides)
caceae25
MR
3393 (error "Cannot make side window the only window"))
3394 ((and (window-minibuffer-p window)
3395 (not (eq window (frame-root-window window))))
3396 (error "Can't expand minibuffer to full frame")))
3397
3398 ;; If WINDOW is the major non-side window, do nothing.
3399 (if (window-with-parameter 'window-side)
3400 (setq side-main (window--major-non-side-window frame))
3401 (setq side-main (frame-root-window frame)))
562dd5e9
MR
3402 (unless (eq window side-main)
3403 (delete-other-windows-internal window side-main)
3404 (run-window-configuration-change-hook frame)
54f9154c 3405 (window--check frame))
562dd5e9
MR
3406 ;; Always return nil.
3407 nil)))
9397e56f
MR
3408
3409(defun delete-other-windows-vertically (&optional window)
3410 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
3411This may be a useful alternative binding for \\[delete-other-windows]
3412 if you often split windows horizontally."
3413 (interactive)
3414 (let* ((window (or window (selected-window)))
3415 (edges (window-edges window))
3416 (w window) delenda)
3417 (while (not (eq (setq w (next-window w 1)) window))
3418 (let ((e (window-edges w)))
3419 (when (and (= (car e) (car edges))
36cec983 3420 (= (nth 2 e) (nth 2 edges)))
9397e56f
MR
3421 (push w delenda))))
3422 (mapc 'delete-window delenda)))
3423
3424;;; Windows and buffers.
3425
3426;; `prev-buffers' and `next-buffers' are two reserved window slots used
3427;; for (1) determining which buffer to show in the window when its
3428;; buffer shall be buried or killed and (2) which buffer to show for
3429;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3430
3431;; `prev-buffers' consists of <buffer, window-start, window-point>
3432;; triples. The entries on this list are ordered by the time their
3433;; buffer has been removed from the window, the most recently removed
3434;; buffer's entry being first. The window-start and window-point
3435;; components are `window-start' and `window-point' at the time the
3436;; buffer was removed from the window which implies that the entry must
3437;; be added when `set-window-buffer' removes the buffer from the window.
3438
3439;; `next-buffers' is the list of buffers that have been replaced
3440;; recently by `switch-to-prev-buffer'. These buffers are the least
3441;; preferred candidates of `switch-to-prev-buffer' and the preferred
3442;; candidates of `switch-to-next-buffer' to switch to. This list is
3443;; reset to nil by any action changing the window's buffer with the
3444;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
3445;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
3446;; `switch-to-next-buffer' pops the last pushed buffer from it.
3447
3448;; Both `prev-buffers' and `next-buffers' may reference killed buffers
3449;; if such a buffer was killed while the window was hidden within a
3450;; window configuration. Such killed buffers get removed whenever
3451;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
3452
3453;; The following function is called by `set-window-buffer' _before_ it
3454;; replaces the buffer of the argument window with the new buffer.
3455(defun record-window-buffer (&optional window)
3456 "Record WINDOW's buffer.
3457WINDOW must be a live window and defaults to the selected one."
447f16b8 3458 (let* ((window (window-normalize-window window t))
9397e56f
MR
3459 (buffer (window-buffer window))
3460 (entry (assq buffer (window-prev-buffers window))))
3461 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
3462 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3463 (set-window-next-buffers window nil)
3464
3465 (when entry
3466 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
3467 (set-window-prev-buffers
3468 window (assq-delete-all buffer (window-prev-buffers window))))
3469
3470 ;; Don't record insignificant buffers.
3471 (unless (eq (aref (buffer-name buffer) 0) ?\s)
3472 ;; Add an entry for buffer to WINDOW's previous buffers.
3473 (with-current-buffer buffer
3474 (let ((start (window-start window))
5481664a 3475 (point (window-point window)))
9397e56f
MR
3476 (setq entry
3477 (cons buffer
3478 (if entry
3479 ;; We have an entry, update marker positions.
3480 (list (set-marker (nth 1 entry) start)
3481 (set-marker (nth 2 entry) point))
3482 ;; Make new markers.
3483 (list (copy-marker start)
92346275
MR
3484 (copy-marker
3485 ;; Preserve window-point-insertion-type
3486 ;; (Bug#12588).
3487 point window-point-insertion-type)))))
9397e56f 3488 (set-window-prev-buffers
c660a885
MR
3489 window (cons entry (window-prev-buffers window)))))
3490
3491 (run-hooks 'buffer-list-update-hook))))
9397e56f
MR
3492
3493(defun unrecord-window-buffer (&optional window buffer)
3494 "Unrecord BUFFER in WINDOW.
3495WINDOW must be a live window and defaults to the selected one.
3496BUFFER must be a live buffer and defaults to the buffer of
3497WINDOW."
447f16b8 3498 (let* ((window (window-normalize-window window t))
9397e56f
MR
3499 (buffer (or buffer (window-buffer window))))
3500 (set-window-prev-buffers
3501 window (assq-delete-all buffer (window-prev-buffers window)))
3502 (set-window-next-buffers
3503 window (delq buffer (window-next-buffers window)))))
3504
3505(defun set-window-buffer-start-and-point (window buffer &optional start point)
3506 "Set WINDOW's buffer to BUFFER.
85c2386b 3507WINDOW must be a live window and defaults to the selected one.
9397e56f
MR
3508Optional argument START non-nil means set WINDOW's start position
3509to START. Optional argument POINT non-nil means set WINDOW's
3510point to POINT. If WINDOW is selected this also sets BUFFER's
3511`point' to POINT. If WINDOW is selected and the buffer it showed
3512before was current this also makes BUFFER the current buffer."
85c2386b 3513 (setq window (window-normalize-window window t))
9397e56f
MR
3514 (let ((selected (eq window (selected-window)))
3515 (current (eq (window-buffer window) (current-buffer))))
3516 (set-window-buffer window buffer)
3517 (when (and selected current)
3518 (set-buffer buffer))
3519 (when start
cf4eacfd
MR
3520 ;; Don't force window-start here (even if POINT is nil).
3521 (set-window-start window start t))
9397e56f 3522 (when point
5481664a 3523 (set-window-point window point))))
9397e56f 3524
dcb6e7b3
MR
3525(defcustom switch-to-visible-buffer t
3526 "If non-nil, allow switching to an already visible buffer.
3527If this variable is non-nil, `switch-to-prev-buffer' and
3528`switch-to-next-buffer' may switch to an already visible buffer
43bcfda6
MR
3529provided the buffer was shown before in the window specified as
3530argument to those functions. If this variable is nil,
3531`switch-to-prev-buffer' and `switch-to-next-buffer' always try to
3532avoid switching to a buffer that is already visible in another
3533window on the same frame."
dcb6e7b3
MR
3534 :type 'boolean
3535 :version "24.1"
3536 :group 'windows)
3537
9397e56f
MR
3538(defun switch-to-prev-buffer (&optional window bury-or-kill)
3539 "In WINDOW switch to previous buffer.
3540WINDOW must be a live window and defaults to the selected one.
502e3f89
MR
3541Return the buffer switched to, nil if no suitable buffer could be
3542found.
9397e56f
MR
3543
3544Optional argument BURY-OR-KILL non-nil means the buffer currently
3545shown in WINDOW is about to be buried or killed and consequently
78dd6ab1
MR
3546shall not be switched to in future invocations of this command.
3547
3548As a special case, if BURY-OR-KILL equals `append', this means to
3549move the buffer to the end of WINDOW's previous buffers list so a
3550future invocation of `switch-to-prev-buffer' less likely switches
3551to it."
9397e56f 3552 (interactive)
447f16b8 3553 (let* ((window (window-normalize-window window t))
dcb6e7b3 3554 (frame (window-frame window))
9397e56f
MR
3555 (old-buffer (window-buffer window))
3556 ;; Save this since it's destroyed by `set-window-buffer'.
3557 (next-buffers (window-next-buffers window))
862382df 3558 (pred (frame-parameter frame 'buffer-predicate))
78dd6ab1
MR
3559 entry new-buffer killed-buffers visible)
3560 (when (window-minibuffer-p window)
3561 ;; Don't switch in minibuffer window.
3562 (unless (setq window (minibuffer-selected-window))
3563 (error "Window %s is a minibuffer window" window)))
3564
1b36ed6a 3565 (when (window-dedicated-p window)
78dd6ab1 3566 ;; Don't switch in dedicated window.
1b36ed6a
MR
3567 (error "Window %s is dedicated to buffer %s" window old-buffer))
3568
3569 (catch 'found
3570 ;; Scan WINDOW's previous buffers first, skipping entries of next
3571 ;; buffers.
3572 (dolist (entry (window-prev-buffers window))
78dd6ab1
MR
3573 (when (and (setq new-buffer (car entry))
3574 (or (buffer-live-p new-buffer)
1b36ed6a 3575 (not (setq killed-buffers
78dd6ab1
MR
3576 (cons new-buffer killed-buffers))))
3577 (not (eq new-buffer old-buffer))
3578 (or (null pred) (funcall pred new-buffer))
3579 ;; When BURY-OR-KILL is nil, avoid switching to a
3580 ;; buffer in WINDOW's next buffers list.
3581 (or bury-or-kill (not (memq new-buffer next-buffers))))
dcb6e7b3 3582 (if (and (not switch-to-visible-buffer)
78dd6ab1
MR
3583 (get-buffer-window new-buffer frame))
3584 ;; Try to avoid showing a buffer visible in some other
3585 ;; window.
3586 (setq visible new-buffer)
502e3f89
MR
3587 (set-window-buffer-start-and-point
3588 window new-buffer (nth 1 entry) (nth 2 entry))
3589 (throw 'found t))))
1b36ed6a
MR
3590 ;; Scan reverted buffer list of WINDOW's frame next, skipping
3591 ;; entries of next buffers. Note that when we bury or kill a
3592 ;; buffer we don't reverse the global buffer list to avoid showing
3593 ;; a buried buffer instead. Otherwise, we must reverse the global
3594 ;; buffer list in order to make sure that switching to the
3595 ;; previous/next buffer traverse it in opposite directions.
3596 (dolist (buffer (if bury-or-kill
dcb6e7b3
MR
3597 (buffer-list frame)
3598 (nreverse (buffer-list frame))))
1b36ed6a
MR
3599 (when (and (buffer-live-p buffer)
3600 (not (eq buffer old-buffer))
862382df 3601 (or (null pred) (funcall pred buffer))
78dd6ab1 3602 (not (eq (aref (buffer-name buffer) 0) ?\s))
1b36ed6a 3603 (or bury-or-kill (not (memq buffer next-buffers))))
dcb6e7b3 3604 (if (get-buffer-window buffer frame)
1b36ed6a 3605 ;; Try to avoid showing a buffer visible in some other window.
dcb6e7b3
MR
3606 (unless visible
3607 (setq visible buffer))
1b36ed6a
MR
3608 (setq new-buffer buffer)
3609 (set-window-buffer-start-and-point window new-buffer)
3610 (throw 'found t))))
3611 (unless bury-or-kill
3612 ;; Scan reverted next buffers last (must not use nreverse
3613 ;; here!).
3614 (dolist (buffer (reverse next-buffers))
3615 ;; Actually, buffer _must_ be live here since otherwise it
3616 ;; would have been caught in the scan of previous buffers.
3617 (when (and (or (buffer-live-p buffer)
9397e56f 3618 (not (setq killed-buffers
1b36ed6a 3619 (cons buffer killed-buffers))))
9397e56f 3620 (not (eq buffer old-buffer))
862382df 3621 (or (null pred) (funcall pred buffer))
1b36ed6a 3622 (setq entry (assq buffer (window-prev-buffers window))))
9397e56f 3623 (setq new-buffer buffer)
1b36ed6a
MR
3624 (set-window-buffer-start-and-point
3625 window new-buffer (nth 1 entry) (nth 2 entry))
9397e56f 3626 (throw 'found t))))
1b36ed6a
MR
3627
3628 ;; Show a buffer visible in another window.
3629 (when visible
3630 (setq new-buffer visible)
3631 (set-window-buffer-start-and-point window new-buffer)))
3632
3633 (if bury-or-kill
78dd6ab1
MR
3634 (let ((entry (and (eq bury-or-kill 'append)
3635 (assq old-buffer (window-prev-buffers window)))))
3636 ;; Remove `old-buffer' from WINDOW's previous and (restored list
3637 ;; of) next buffers.
1b36ed6a
MR
3638 (set-window-prev-buffers
3639 window (assq-delete-all old-buffer (window-prev-buffers window)))
78dd6ab1
MR
3640 (set-window-next-buffers window (delq old-buffer next-buffers))
3641 (when entry
3642 ;; Append old-buffer's entry to list of WINDOW's previous
3643 ;; buffers so it's less likely to get switched to soon but
3644 ;; `display-buffer-in-previous-window' can nevertheless find
3645 ;; it.
3646 (set-window-prev-buffers
3647 window (append (window-prev-buffers window) (list entry)))))
1b36ed6a
MR
3648 ;; Move `old-buffer' to head of WINDOW's restored list of next
3649 ;; buffers.
3650 (set-window-next-buffers
3651 window (cons old-buffer (delq old-buffer next-buffers))))
9397e56f
MR
3652
3653 ;; Remove killed buffers from WINDOW's previous and next buffers.
3654 (when killed-buffers
3655 (dolist (buffer killed-buffers)
3656 (set-window-prev-buffers
3657 window (assq-delete-all buffer (window-prev-buffers window)))
3658 (set-window-next-buffers
3659 window (delq buffer (window-next-buffers window)))))
3660
3661 ;; Return new-buffer.
3662 new-buffer))
3663
3664(defun switch-to-next-buffer (&optional window)
3665 "In WINDOW switch to next buffer.
502e3f89
MR
3666WINDOW must be a live window and defaults to the selected one.
3667Return the buffer switched to, nil if no suitable buffer could be
3668found."
9397e56f 3669 (interactive)
447f16b8 3670 (let* ((window (window-normalize-window window t))
dcb6e7b3 3671 (frame (window-frame window))
9397e56f
MR
3672 (old-buffer (window-buffer window))
3673 (next-buffers (window-next-buffers window))
862382df 3674 (pred (frame-parameter frame 'buffer-predicate))
78dd6ab1
MR
3675 new-buffer entry killed-buffers visible)
3676 (when (window-minibuffer-p window)
3677 ;; Don't switch in minibuffer window.
3678 (unless (setq window (minibuffer-selected-window))
3679 (error "Window %s is a minibuffer window" window)))
3680
9397e56f 3681 (when (window-dedicated-p window)
78dd6ab1 3682 ;; Don't switch in dedicated window.
9397e56f
MR
3683 (error "Window %s is dedicated to buffer %s" window old-buffer))
3684
3685 (catch 'found
3686 ;; Scan WINDOW's next buffers first.
3687 (dolist (buffer next-buffers)
3688 (when (and (or (buffer-live-p buffer)
3689 (not (setq killed-buffers
3690 (cons buffer killed-buffers))))
3691 (not (eq buffer old-buffer))
862382df 3692 (or (null pred) (funcall pred buffer))
9397e56f
MR
3693 (setq entry (assq buffer (window-prev-buffers window))))
3694 (setq new-buffer buffer)
3695 (set-window-buffer-start-and-point
3696 window new-buffer (nth 1 entry) (nth 2 entry))
3697 (throw 'found t)))
3698 ;; Scan the buffer list of WINDOW's frame next, skipping previous
3699 ;; buffers entries.
dcb6e7b3 3700 (dolist (buffer (buffer-list frame))
78dd6ab1
MR
3701 (when (and (buffer-live-p buffer)
3702 (not (eq buffer old-buffer))
862382df 3703 (or (null pred) (funcall pred buffer))
78dd6ab1 3704 (not (eq (aref (buffer-name buffer) 0) ?\s))
9397e56f 3705 (not (assq buffer (window-prev-buffers window))))
dcb6e7b3 3706 (if (get-buffer-window buffer frame)
9397e56f
MR
3707 ;; Try to avoid showing a buffer visible in some other window.
3708 (setq visible buffer)
3709 (setq new-buffer buffer)
3710 (set-window-buffer-start-and-point window new-buffer)
3711 (throw 'found t))))
3712 ;; Scan WINDOW's reverted previous buffers last (must not use
3713 ;; nreverse here!)
3714 (dolist (entry (reverse (window-prev-buffers window)))
78dd6ab1
MR
3715 (when (and (setq new-buffer (car entry))
3716 (or (buffer-live-p new-buffer)
9397e56f 3717 (not (setq killed-buffers
78dd6ab1
MR
3718 (cons new-buffer killed-buffers))))
3719 (not (eq new-buffer old-buffer))
3720 (or (null pred) (funcall pred new-buffer)))
dcb6e7b3 3721 (if (and (not switch-to-visible-buffer)
78dd6ab1 3722 (get-buffer-window new-buffer frame))
dcb6e7b3
MR
3723 ;; Try to avoid showing a buffer visible in some other window.
3724 (unless visible
78dd6ab1 3725 (setq visible new-buffer))
dcb6e7b3
MR
3726 (set-window-buffer-start-and-point
3727 window new-buffer (nth 1 entry) (nth 2 entry))
3728 (throw 'found t))))
9397e56f
MR
3729
3730 ;; Show a buffer visible in another window.
3731 (when visible
3732 (setq new-buffer visible)
3733 (set-window-buffer-start-and-point window new-buffer)))
3734
3735 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
3736 (set-window-next-buffers window (delq new-buffer next-buffers))
3737
3738 ;; Remove killed buffers from WINDOW's previous and next buffers.
3739 (when killed-buffers
3740 (dolist (buffer killed-buffers)
3741 (set-window-prev-buffers
3742 window (assq-delete-all buffer (window-prev-buffers window)))
3743 (set-window-next-buffers
3744 window (delq buffer (window-next-buffers window)))))
3745
3746 ;; Return new-buffer.
3747 new-buffer))
3748
3749(defun get-next-valid-buffer (list &optional buffer visible-ok frame)
3750 "Search LIST for a valid buffer to display in FRAME.
3751Return nil when all buffers in LIST are undesirable for display,
3752otherwise return the first suitable buffer in LIST.
3753
3754Buffers not visible in windows are preferred to visible buffers,
3755unless VISIBLE-OK is non-nil.
3756If the optional argument FRAME is nil, it defaults to the selected frame.
3757If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
3758 ;; This logic is more or less copied from other-buffer.
3759 (setq frame (or frame (selected-frame)))
3760 (let ((pred (frame-parameter frame 'buffer-predicate))
3761 found buf)
3762 (while (and (not found) list)
3763 (setq buf (car list))
3764 (if (and (not (eq buffer buf))
3765 (buffer-live-p buf)
3766 (or (null pred) (funcall pred buf))
3767 (not (eq (aref (buffer-name buf) 0) ?\s))
3768 (or visible-ok (null (get-buffer-window buf 'visible))))
3769 (setq found buf)
3770 (setq list (cdr list))))
3771 (car list)))
3772
3773(defun last-buffer (&optional buffer visible-ok frame)
3774 "Return the last buffer in FRAME's buffer list.
3775If BUFFER is the last buffer, return the preceding buffer
3776instead. Buffers not visible in windows are preferred to visible
3777buffers, unless optional argument VISIBLE-OK is non-nil.
3778Optional third argument FRAME nil or omitted means use the
3779selected frame's buffer list. If no such buffer exists, return
3780the buffer `*scratch*', creating it if necessary."
3781 (setq frame (or frame (selected-frame)))
3782 (or (get-next-valid-buffer (nreverse (buffer-list frame))
3783 buffer visible-ok frame)
3784 (get-buffer "*scratch*")
3785 (let ((scratch (get-buffer-create "*scratch*")))
3786 (set-buffer-major-mode scratch)
3787 scratch)))
3788
5a4cf282
MR
3789(defcustom frame-auto-hide-function #'iconify-frame
3790 "Function called to automatically hide frames.
3791The function is called with one argument - a frame.
3792
3793Functions affected by this option are those that bury a buffer
3794shown in a separate frame like `quit-window' and `bury-buffer'."
3795 :type '(choice (const :tag "Iconify" iconify-frame)
3796 (const :tag "Delete" delete-frame)
3797 (const :tag "Do nothing" ignore)
3798 function)
3799 :group 'windows
49677495
MR
3800 :group 'frames
3801 :version "24.1")
0e2070b5
MR
3802
3803(defun window--delete (&optional window dedicated-only kill)
3804 "Delete WINDOW if possible.
3805WINDOW must be a live window and defaults to the selected one.
3806Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
3807only if it's dedicated to its buffer. Optional argument KILL
3808means the buffer shown in window will be killed. Return non-nil
c557cd6b 3809if WINDOW gets deleted or its frame is auto-hidden."
447f16b8 3810 (setq window (window-normalize-window window t))
0e2070b5 3811 (unless (and dedicated-only (not (window-dedicated-p window)))
cb882333 3812 (let ((deletable (window-deletable-p window)))
0e2070b5
MR
3813 (cond
3814 ((eq deletable 'frame)
3815 (let ((frame (window-frame window)))
c557cd6b
MR
3816 (cond
3817 (kill
3818 (delete-frame frame))
3819 ((functionp frame-auto-hide-function)
3820 (funcall frame-auto-hide-function frame))))
0e2070b5
MR
3821 'frame)
3822 (deletable
3823 (delete-window window)
3824 t)))))
3825
9397e56f
MR
3826(defun bury-buffer (&optional buffer-or-name)
3827 "Put BUFFER-OR-NAME at the end of the list of all buffers.
3828There it is the least likely candidate for `other-buffer' to
3829return; thus, the least likely buffer for \\[switch-to-buffer] to
3830select by default.
3831
3832You can specify a buffer name as BUFFER-OR-NAME, or an actual
3833buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
3834current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
3835remove the current buffer from the selected window if it is
3836displayed there."
3837 (interactive)
5386012d 3838 (let* ((buffer (window-normalize-buffer buffer-or-name)))
9397e56f
MR
3839 ;; If `buffer-or-name' is not on the selected frame we unrecord it
3840 ;; although it's not "here" (call it a feature).
e4ed06f1 3841 (bury-buffer-internal buffer)
9397e56f
MR
3842 ;; Handle case where `buffer-or-name' is nil and the current buffer
3843 ;; is shown in the selected window.
3844 (cond
3845 ((or buffer-or-name (not (eq buffer (window-buffer)))))
0e2070b5
MR
3846 ((window--delete nil t))
3847 (t
3848 ;; Switch to another buffer in window.
3849 (set-window-dedicated-p nil nil)
1885e5b8 3850 (switch-to-prev-buffer nil 'bury)))
1b36ed6a 3851
9397e56f
MR
3852 ;; Always return nil.
3853 nil))
3854
3855(defun unbury-buffer ()
3856 "Switch to the last buffer in the buffer list."
3857 (interactive)
3858 (switch-to-buffer (last-buffer)))
3859
3860(defun next-buffer ()
3861 "In selected window switch to next buffer."
3862 (interactive)
85c2386b
MR
3863 (cond
3864 ((window-minibuffer-p)
3865 (error "Cannot switch buffers in minibuffer window"))
3866 ((eq (window-dedicated-p) t)
3867 (error "Window is strongly dedicated to its buffer"))
3868 (t
3869 (switch-to-next-buffer))))
9397e56f
MR
3870
3871(defun previous-buffer ()
3872 "In selected window switch to previous 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-prev-buffer))))
9397e56f
MR
3881
3882(defun delete-windows-on (&optional buffer-or-name frame)
3883 "Delete all windows showing BUFFER-OR-NAME.
3884BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3885and defaults to the current buffer.
3886
3887The following non-nil values of the optional argument FRAME
3888have special meanings:
3889
3890- t means consider all windows on the selected frame only.
3891
3892- `visible' means consider all windows on all visible frames on
3893 the current terminal.
3894
3895- 0 (the number zero) means consider all windows on all visible
3896 and iconified frames on the current terminal.
3897
3898- A frame means consider all windows on that frame only.
3899
3900Any other value of FRAME means consider all windows on all
3901frames.
3902
3903When a window showing BUFFER-OR-NAME is dedicated and the only
3904window of its frame, that frame is deleted when there are other
3905frames left."
3906 (interactive "BDelete windows on (buffer):\nP")
5386012d 3907 (let ((buffer (window-normalize-buffer buffer-or-name))
9397e56f
MR
3908 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3909 ;; `window-list-1' based function.
3910 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
3911 (dolist (window (window-list-1 nil nil all-frames))
3912 (if (eq (window-buffer window) buffer)
1b36ed6a 3913 (let ((deletable (window-deletable-p window)))
9397e56f 3914 (cond
1b36ed6a
MR
3915 ((and (eq deletable 'frame) (window-dedicated-p window))
3916 ;; Delete frame if and only if window is dedicated.
9397e56f 3917 (delete-frame (window-frame window)))
1b36ed6a
MR
3918 ((eq deletable t)
3919 ;; Delete window.
9397e56f
MR
3920 (delete-window window))
3921 (t
3922 ;; In window switch to previous buffer.
3923 (set-window-dedicated-p window nil)
3924 (switch-to-prev-buffer window 'bury))))
3925 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3926 (unrecord-window-buffer window buffer)))))
3927
3928(defun replace-buffer-in-windows (&optional buffer-or-name)
3929 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
3930BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3931and defaults to the current buffer.
3932
0e2070b5
MR
3933When a window showing BUFFER-OR-NAME is dedicated, that window is
3934deleted. If that window is the only window on its frame, the
3935frame is deleted too when there are other frames left. If there
3936are no other frames left, some other buffer is displayed in that
3937window.
9397e56f
MR
3938
3939This function removes the buffer denoted by BUFFER-OR-NAME from
3940all window-local buffer lists."
24901d61 3941 (interactive "bBuffer to replace: ")
5386012d 3942 (let ((buffer (window-normalize-buffer buffer-or-name)))
9397e56f
MR
3943 (dolist (window (window-list-1 nil nil t))
3944 (if (eq (window-buffer window) buffer)
0e2070b5
MR
3945 (unless (window--delete window t t)
3946 ;; Switch to another buffer in window.
3947 (set-window-dedicated-p window nil)
3948 (switch-to-prev-buffer window 'kill))
9397e56f
MR
3949 ;; Unrecord BUFFER in WINDOW.
3950 (unrecord-window-buffer window buffer)))))
3951
78dd6ab1
MR
3952(defun quit-restore-window (&optional window bury-or-kill)
3953 "Quit WINDOW and deal with its buffer.
cf4eacfd 3954WINDOW must be a live window and defaults to the selected one.
9397e56f
MR
3955
3956According to information stored in WINDOW's `quit-restore' window
382c953b
JB
3957parameter either (1) delete WINDOW and its frame, (2) delete
3958WINDOW, (3) restore the buffer previously displayed in WINDOW,
3959or (4) make WINDOW display some other buffer than the present
78dd6ab1
MR
3960one. If non-nil, reset `quit-restore' parameter to nil.
3961
3962Optional second argument BURY-OR-KILL tells how to proceed with
3963the buffer of WINDOW. The following values are handled:
3964
3965`nil' means to not handle the buffer in a particular way. This
3966 means that if WINDOW is not deleted by this function, invoking
3967 `switch-to-prev-buffer' will usually show the buffer again.
3968
3969`append' means that if WINDOW is not deleted, move its buffer to
3970 the end of WINDOW's previous buffers so it's less likely that a
3971 future invocation of `switch-to-prev-buffer' will switch to it.
3972 Also, move the buffer to the end of the frame's buffer list.
3973
3974`bury' means that if WINDOW is not deleted, remove its buffer
3975 from WINDOW'S list of previous buffers. Also, move the buffer
3976 to the end of the frame's buffer list. This value provides the
3977 most reliable remedy to not have `switch-to-prev-buffer' switch
3978 to this buffer again without killing the buffer.
3979
3980`kill' means to kill WINDOW's buffer."
447f16b8 3981 (setq window (window-normalize-window window t))
cf4eacfd
MR
3982 (let* ((buffer (window-buffer window))
3983 (quit-restore (window-parameter window 'quit-restore))
3984 (prev-buffer
3985 (let* ((prev-buffers (window-prev-buffers window))
3986 (prev-buffer (caar prev-buffers)))
3987 (and (or (not (eq prev-buffer buffer))
3988 (and (cdr prev-buffers)
3989 (not (eq (setq prev-buffer (cadr prev-buffers))
3990 buffer))))
3991 prev-buffer)))
78dd6ab1 3992 quad entry)
9397e56f 3993 (cond
cf4eacfd 3994 ((and (not prev-buffer)
218e997a
MR
3995 (or (eq (nth 1 quit-restore) 'frame)
3996 (and (eq (nth 1 quit-restore) 'window)
3997 ;; If the window has been created on an existing
3998 ;; frame and ended up as the sole window on that
3999 ;; frame, do not delete it (Bug#12764).
4000 (not (eq window (frame-root-window window)))))
0e2070b5
MR
4001 (eq (nth 3 quit-restore) buffer)
4002 ;; Delete WINDOW if possible.
78dd6ab1 4003 (window--delete window nil (eq bury-or-kill 'kill)))
9397e56f
MR
4004 ;; If the previously selected window is still alive, select it.
4005 (when (window-live-p (nth 2 quit-restore))
4006 (select-window (nth 2 quit-restore))))
cf4eacfd
MR
4007 ((and (listp (setq quad (nth 1 quit-restore)))
4008 (buffer-live-p (car quad))
4009 (eq (nth 3 quit-restore) buffer))
4010 ;; Show another buffer stored in quit-restore parameter.
78dd6ab1 4011 (when (and (integerp (nth 3 quad))
880e6158 4012 (/= (nth 3 quad) (window-total-height window)))
cf4eacfd
MR
4013 ;; Try to resize WINDOW to its old height but don't signal an
4014 ;; error.
4015 (condition-case nil
880e6158 4016 (window-resize window (- (nth 3 quad) (window-total-height window)))
cf4eacfd 4017 (error nil)))
78dd6ab1 4018 (set-window-dedicated-p window nil)
1885e5b8 4019 ;; Restore WINDOW's previous buffer, start and point position.
cf4eacfd
MR
4020 (set-window-buffer-start-and-point
4021 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
78dd6ab1
MR
4022 ;; Deal with the buffer we just removed from WINDOW.
4023 (setq entry (and (eq bury-or-kill 'append)
4024 (assq buffer (window-prev-buffers window))))
4025 (when bury-or-kill
4026 ;; Remove buffer from WINDOW's previous and next buffers.
4027 (set-window-prev-buffers
4028 window (assq-delete-all buffer (window-prev-buffers window)))
4029 (set-window-next-buffers
4030 window (delq buffer (window-next-buffers window))))
4031 (when entry
4032 ;; Append old buffer's entry to list of WINDOW's previous
4033 ;; buffers so it's less likely to get switched to soon but
4034 ;; `display-buffer-in-previous-window' can nevertheless find it.
4035 (set-window-prev-buffers
4036 window (append (window-prev-buffers window) (list entry))))
9397e56f
MR
4037 ;; Reset the quit-restore parameter.
4038 (set-window-parameter window 'quit-restore nil)
cf4eacfd
MR
4039 ;; Select old window.
4040 (when (window-live-p (nth 2 quit-restore))
4041 (select-window (nth 2 quit-restore))))
9397e56f 4042 (t
cf4eacfd
MR
4043 ;; Show some other buffer in WINDOW and reset the quit-restore
4044 ;; parameter.
9397e56f 4045 (set-window-parameter window 'quit-restore nil)
b4d72fcf
MR
4046 ;; Make sure that WINDOW is no more dedicated.
4047 (set-window-dedicated-p window nil)
78dd6ab1
MR
4048 (switch-to-prev-buffer window bury-or-kill)))
4049
4050 ;; Deal with the buffer.
4051 (cond
4052 ((not (buffer-live-p buffer)))
4053 ((eq bury-or-kill 'kill)
4054 (kill-buffer buffer))
4055 (bury-or-kill
4056 (bury-buffer-internal buffer)))))
9397e56f 4057
78dd6ab1
MR
4058(defun quit-window (&optional kill window)
4059 "Quit WINDOW and bury its buffer.
4060WINDOW must be a live window and defaults to the selected one.
4061With prefix argument KILL non-nil, kill the buffer instead of
4062burying it.
4063
4064According to information stored in WINDOW's `quit-restore' window
4065parameter either (1) delete WINDOW and its frame, (2) delete
4066WINDOW, (3) restore the buffer previously displayed in WINDOW,
4067or (4) make WINDOW display some other buffer than the present
4068one. If non-nil, reset `quit-restore' parameter to nil."
4069 (interactive "P")
4070 (quit-restore-window window (if kill 'kill 'bury)))
366ca7f3
MR
4071
4072(defun quit-windows-on (&optional buffer-or-name kill frame)
4073 "Quit all windows showing BUFFER-OR-NAME.
4074BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4075and defaults to the current buffer. Optional argument KILL
4076non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4077BUFFER-OR-NAME. Optional argument FRAME is handled as by
4078`delete-windows-on'.
4079
4080This function calls `quit-window' on all candidate windows
4081showing BUFFER-OR-NAME."
4082 (interactive "BQuit windows on (buffer):\nP")
4083 (let ((buffer (window-normalize-buffer buffer-or-name))
4084 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4085 ;; `window-list-1' based function.
4086 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4087 (dolist (window (window-list-1 nil nil all-frames))
4088 (if (eq (window-buffer window) buffer)
4089 (quit-window kill window)
4090 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4091 (unrecord-window-buffer window buffer)))))
562dd5e9
MR
4092\f
4093;;; Splitting windows.
880e6158 4094(defun window-split-min-size (&optional horizontal pixelwise)
562dd5e9
MR
4095 "Return minimum height of any window when splitting windows.
4096Optional argument HORIZONTAL non-nil means return minimum width."
880e6158
MR
4097 (cond
4098 (pixelwise
4099 (if horizontal
4100 (window-min-pixel-width)
4101 (window-min-pixel-height)))
4102 (horizontal
4103 (max window-min-width window-safe-min-width))
4104 (t
4105 (max window-min-height window-safe-min-height))))
562dd5e9 4106
880e6158 4107(defun split-window (&optional window size side pixelwise)
562dd5e9 4108 "Make a new window adjacent to WINDOW.
85c2386b 4109WINDOW must be a valid window and defaults to the selected one.
562dd5e9
MR
4110Return the new window which is always a live window.
4111
4112Optional argument SIZE a positive number means make WINDOW SIZE
4113lines or columns tall. If SIZE is negative, make the new window
4114-SIZE lines or columns tall. If and only if SIZE is non-nil, its
4115absolute value can be less than `window-min-height' or
4116`window-min-width'; so this command can make a new window as
4117small as one line or two columns. SIZE defaults to half of
0aa3616e 4118WINDOW's size.
562dd5e9
MR
4119
4120Optional third argument SIDE nil (or `below') specifies that the
4121new window shall be located below WINDOW. SIDE `above' means the
4122new window shall be located above WINDOW. In both cases SIZE
382c953b 4123specifies the new number of lines for WINDOW (or the new window
562dd5e9
MR
4124if SIZE is negative) including space reserved for the mode and/or
4125header line.
4126
4127SIDE t (or `right') specifies that the new window shall be
4128located on the right side of WINDOW. SIDE `left' means the new
4129window shall be located on the left of WINDOW. In both cases
382c953b 4130SIZE specifies the new number of columns for WINDOW (or the new
562dd5e9
MR
4131window provided SIZE is negative) including space reserved for
4132fringes and the scrollbar or a divider column. Any other non-nil
4133value for SIDE is currently handled like t (or `right').
4134
f224e500 4135PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
880e6158 4136
562dd5e9
MR
4137If the variable `ignore-window-parameters' is non-nil or the
4138`split-window' parameter of WINDOW equals t, do not process any
4139parameters of WINDOW. Otherwise, if the `split-window' parameter
4140of WINDOW specifies a function, call that function with all three
4141arguments and return the value returned by that function.
4142
4143Otherwise, if WINDOW is part of an atomic window, \"split\" the
4144root of that atomic window. The new window does not become a
4145member of that atomic window.
4146
4147If WINDOW is live, properties of the new window like margins and
4148scrollbars are inherited from WINDOW. If WINDOW is an internal
4149window, these properties as well as the buffer displayed in the
4150new window are inherited from the window selected on WINDOW's
4151frame. The selected window is not changed by this function."
447f16b8 4152 (setq window (window-normalize-window window))
130e3e11
MR
4153 (let* ((side (cond
4154 ((not side) 'below)
4155 ((memq side '(below above right left)) side)
4156 (t 'right)))
caceae25 4157 (horizontal (not (memq side '(below above))))
562dd5e9
MR
4158 (frame (window-frame window))
4159 (parent (window-parent window))
4160 (function (window-parameter window 'split-window))
4161 (window-side (window-parameter window 'window-side))
8e17c9ba
MR
4162 ;; Rebind the following two variables since in some cases we
4163 ;; have to override their value.
b6f67890 4164 (window-combination-limit window-combination-limit)
caceae25 4165 (window-combination-resize window-combination-resize)
880e6158
MR
4166 (char-size (frame-char-size window horizontal))
4167 (pixel-size
4168 (when (numberp size)
4169 (window--size-to-pixel window size horizontal pixelwise t)))
562dd5e9 4170 atom-root)
54f9154c 4171 (window--check frame)
562dd5e9
MR
4172 (catch 'done
4173 (cond
4174 ;; Ignore window parameters if either `ignore-window-parameters'
4175 ;; is t or the `split-window' parameter equals t.
4176 ((or ignore-window-parameters (eq function t)))
4177 ((functionp function)
4178 ;; The `split-window' parameter specifies the function to call.
4179 ;; If that function is `ignore', do nothing.
4180 (throw 'done (funcall function window size side)))
be7f5545
MR
4181 ;; If WINDOW is part of an atomic window, split the root window
4182 ;; of that atomic window instead.
562dd5e9
MR
4183 ((and (window-parameter window 'window-atom)
4184 (setq atom-root (window-atom-root window))
4185 (not (eq atom-root window)))
880e6158 4186 (throw 'done (split-window atom-root size side pixelwise)))
caceae25
MR
4187 ;; If WINDOW is a side window or its first or last child is a
4188 ;; side window, throw an error unless `window-combination-resize'
4189 ;; equals 'side.
4190 ((and (not (eq window-combination-resize 'side))
4191 (or (window-parameter window 'window-side)
4192 (and (window-child window)
4193 (or (window-parameter
4194 (window-child window) 'window-side)
4195 (window-parameter
4196 (window-last-child window) 'window-side)))))
4197 (error "Cannot split side window or parent of side window"))
4198 ;; If `window-combination-resize' is 'side and window has a side
4199 ;; window sibling, bind `window-combination-limit' to t.
4200 ((and (not (eq window-combination-resize 'side))
4201 (or (and (window-prev-sibling window)
4202 (window-parameter
4203 (window-prev-sibling window) 'window-side))
4204 (and (window-next-sibling window)
4205 (window-parameter
4206 (window-next-sibling window) 'window-side))))
4207 (setq window-combination-limit t)))
4208
4209 ;; If `window-combination-resize' is t and SIZE is non-negative,
4210 ;; bind `window-combination-limit' to t.
880e6158
MR
4211 (when (and (eq window-combination-resize t)
4212 pixel-size (> pixel-size 0))
b6f67890 4213 (setq window-combination-limit t))
562dd5e9 4214
880e6158
MR
4215 (let* ((parent-pixel-size
4216 ;; `parent-pixel-size' is the pixel size of WINDOW's
4217 ;; parent, provided it has one.
4218 (when parent (window-size parent horizontal t)))
562dd5e9
MR
4219 ;; `resize' non-nil means we are supposed to resize other
4220 ;; windows in WINDOW's combination.
4221 (resize
caceae25
MR
4222 (and window-combination-resize
4223 (or (window-parameter window 'window-side)
4224 (not (eq window-combination-resize 'side)))
8e17c9ba 4225 (not (eq window-combination-limit t))
562dd5e9 4226 ;; Resize makes sense in iso-combinations only.
3d8daefe 4227 (window-combined-p window horizontal)))
880e6158
MR
4228 ;; `old-pixel-size' is the current pixel size of WINDOW.
4229 (old-pixel-size (window-size window horizontal t))
562dd5e9
MR
4230 ;; `new-size' is the specified or calculated size of the
4231 ;; new window.
2d6b6005 4232 new-pixel-size new-parent new-normal)
880e6158
MR
4233 (cond
4234 ((not pixel-size)
4235 (setq new-pixel-size
4236 (if resize
4237 ;; When resizing try to give the new window the
4238 ;; average size of a window in its combination.
4239 (min (- parent-pixel-size
4240 (window-min-size parent horizontal nil t))
4241 (/ parent-pixel-size
4242 (1+ (window-combinations parent horizontal))))
4243 ;; Else try to give the new window half the size
4244 ;; of WINDOW (plus an eventual odd pixel).
4245 (/ old-pixel-size 2)))
4246 (unless window-resize-pixelwise
4247 ;; Round to nearest char-size multiple.
4248 (setq new-pixel-size
4249 (* char-size (round new-pixel-size char-size)))))
4250 ((>= pixel-size 0)
4251 ;; SIZE non-negative specifies the new size of WINDOW.
4252
4253 ;; Note: Specifying a non-negative SIZE is practically
4254 ;; always done as workaround for making the new window
4255 ;; appear above or on the left of the new window (the
4256 ;; ispell window is a typical example of that). In all
4257 ;; these cases the SIDE argument should be set to 'above
4258 ;; or 'left in order to support the 'resize option.
4259 ;; Here we have to nest the windows instead, see above.
4260 (setq new-pixel-size (- old-pixel-size pixel-size)))
4261 (t
4262 ;; SIZE negative specifies the size of the new window.
4263 (setq new-pixel-size (- pixel-size))))
562dd5e9
MR
4264
4265 ;; Check SIZE.
4266 (cond
880e6158 4267 ((not pixel-size)
562dd5e9
MR
4268 (cond
4269 (resize
4270 ;; SIZE unspecified, resizing.
880e6158
MR
4271 (when (and (not (window-sizable-p
4272 parent (- new-pixel-size) horizontal nil t))
562dd5e9 4273 ;; Try again with minimum split size.
880e6158
MR
4274 (setq new-pixel-size
4275 (max new-pixel-size
4276 (window-split-min-size horizontal t)))
4277 (not (window-sizable-p
4278 parent (- new-pixel-size) horizontal nil t)))
4279 (error "Window %s too small for splitting 1" parent)))
4280 ((> (+ new-pixel-size (window-min-size window horizontal nil t))
4281 old-pixel-size)
562dd5e9 4282 ;; SIZE unspecified, no resizing.
880e6158
MR
4283 (error "Window %s too small for splitting 2" window))))
4284 ((and (>= pixel-size 0)
4285 (or (>= pixel-size old-pixel-size)
4286 (< new-pixel-size
4287 (window-safe-min-pixel-size window horizontal))))
562dd5e9
MR
4288 ;; SIZE specified as new size of old window. If the new size
4289 ;; is larger than the old size or the size of the new window
4290 ;; would be less than the safe minimum, signal an error.
880e6158 4291 (error "Window %s too small for splitting 3" window))
562dd5e9
MR
4292 (resize
4293 ;; SIZE specified, resizing.
880e6158
MR
4294 (unless (window-sizable-p
4295 parent (- new-pixel-size) horizontal nil t)
562dd5e9 4296 ;; If we cannot resize the parent give up.
880e6158
MR
4297 (error "Window %s too small for splitting 4" parent)))
4298 ((or (< new-pixel-size
4299 (window-safe-min-pixel-size window horizontal))
4300 (< (- old-pixel-size new-pixel-size)
4301 (window-safe-min-pixel-size window horizontal)))
562dd5e9 4302 ;; SIZE specification violates minimum size restrictions.
880e6158 4303 (error "Window %s too small for splitting 5" window)))
562dd5e9 4304
5386012d 4305 (window--resize-reset frame horizontal)
562dd5e9
MR
4306
4307 (setq new-parent
4308 ;; Make new-parent non-nil if we need a new parent window;
4309 ;; either because we want to nest or because WINDOW is not
4310 ;; iso-combined.
8e17c9ba 4311 (or (eq window-combination-limit t)
b6f67890 4312 (not (window-combined-p window horizontal))))
562dd5e9
MR
4313 (setq new-normal
4314 ;; Make new-normal the normal size of the new window.
4315 (cond
880e6158
MR
4316 (pixel-size (/ (float new-pixel-size)
4317 (if new-parent old-pixel-size parent-pixel-size)))
562dd5e9 4318 (new-parent 0.5)
3d8daefe 4319 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
562dd5e9
MR
4320 (t (/ (window-normal-size window horizontal) 2.0))))
4321
4322 (if resize
4323 ;; Try to get space from OLD's siblings. We could go "up" and
4324 ;; try getting additional space from surrounding windows but
4325 ;; we won't be able to return space to those windows when we
4326 ;; delete the one we create here. Hence we do not go up.
4327 (progn
880e6158
MR
4328 (window--resize-child-windows
4329 parent (- new-pixel-size) horizontal)
562dd5e9
MR
4330 (let* ((normal (- 1.0 new-normal))
4331 (sub (window-child parent)))
4332 (while sub
4333 (set-window-new-normal
4334 sub (* (window-normal-size sub horizontal) normal))
4335 (setq sub (window-right sub)))))
4336 ;; Get entire space from WINDOW.
880e6158
MR
4337 (set-window-new-pixel
4338 window (- old-pixel-size new-pixel-size))
4339;; (set-window-new-pixel window (- old-pixel-size new-pixel-size))
4340;; (set-window-new-total
4341;; window (- old-size new-size))
4342 (window--resize-this-window window (- new-pixel-size) horizontal)
562dd5e9
MR
4343 (set-window-new-normal
4344 window (- (if new-parent 1.0 (window-normal-size window horizontal))
4345 new-normal)))
4346
880e6158
MR
4347 (let* ((new (split-window-internal window new-pixel-size side new-normal)))
4348 (window--pixel-to-total frame horizontal)
caceae25 4349 ;; Assign window-side parameters, if any.
c660a885
MR
4350 (cond
4351 ((eq window-combination-resize 'side)
caceae25
MR
4352 (let ((window-side
4353 (cond
4354 (window-side window-side)
4355 ((eq side 'above) 'top)
4356 ((eq side 'below) 'bottom)
4357 (t side))))
4358 ;; We made a new side window.
4359 (set-window-parameter new 'window-side window-side)
4360 (when (and new-parent (window-parameter window 'window-side))
4361 ;; We've been splitting a side root window. Give the
4362 ;; new parent the same window-side parameter.
4363 (set-window-parameter
4364 (window-parent new) 'window-side window-side))))
c660a885
MR
4365 ((eq window-combination-resize 'atom)
4366 ;; Make sure `window--check-frame' won't destroy an existing
4367 ;; atomic window in case the new window gets nested inside.
4368 (unless (window-parameter window 'window-atom)
4369 (set-window-parameter window 'window-atom t))
4370 (when new-parent
4371 (set-window-parameter (window-parent new) 'window-atom t))
4372 (set-window-parameter new 'window-atom t)))
562dd5e9
MR
4373
4374 (run-window-configuration-change-hook frame)
880e6158 4375 (run-window-scroll-functions new)
54f9154c 4376 (window--check frame)
562dd5e9
MR
4377 ;; Always return the new window.
4378 new)))))
4379
4380;; I think this should be the default; I think people will prefer it--rms.
4381(defcustom split-window-keep-point t
2d197ffb
CY
4382 "If non-nil, \\[split-window-below] preserves point in the new window.
4383If nil, adjust point in the two windows to minimize redisplay.
4384This option applies only to `split-window-below' and functions
4385that call it. The low-level `split-window' function always keeps
4386the original point in both windows."
562dd5e9
MR
4387 :type 'boolean
4388 :group 'windows)
4389
2d197ffb
CY
4390(defun split-window-below (&optional size)
4391 "Split the selected window into two windows, one above the other.
4392The selected window is above. The newly split-off window is
4393below, and displays the same buffer. Return the new window.
4394
4395If optional argument SIZE is omitted or nil, both windows get the
4396same height, or close to it. If SIZE is positive, the upper
4397\(selected) window gets SIZE lines. If SIZE is negative, the
4398lower (new) window gets -SIZE lines.
4399
4400If the variable `split-window-keep-point' is non-nil, both
4401windows get the same value of point as the selected window.
4402Otherwise, the window starts are chosen so as to minimize the
4403amount of redisplay; this is convenient on slow terminals."
562dd5e9
MR
4404 (interactive "P")
4405 (let ((old-window (selected-window))
5481664a 4406 (old-point (window-point))
562dd5e9
MR
4407 (size (and size (prefix-numeric-value size)))
4408 moved-by-window-height moved new-window bottom)
4409 (when (and size (< size 0) (< (- size) window-min-height))
4410 ;; `split-window' would not signal an error here.
4411 (error "Size of new window too small"))
4412 (setq new-window (split-window nil size))
4413 (unless split-window-keep-point
4414 (with-current-buffer (window-buffer)
c491fa41
MR
4415 ;; Use `save-excursion' around vertical movements below
4416 ;; (Bug#10971). Note: When the selected window's buffer has a
4417 ;; header line, up to two lines of the buffer may not show up
4418 ;; in the resulting configuration.
4419 (save-excursion
4420 (goto-char (window-start))
4421 (setq moved (vertical-motion (window-height)))
4422 (set-window-start new-window (point))
4423 (when (> (point) (window-point new-window))
4424 (set-window-point new-window (point)))
4425 (when (= moved (window-height))
4426 (setq moved-by-window-height t)
4427 (vertical-motion -1))
4428 (setq bottom (point)))
4429 (and moved-by-window-height
4430 (<= bottom (point))
5481664a 4431 (set-window-point old-window (1- bottom)))
c491fa41
MR
4432 (and moved-by-window-height
4433 (<= (window-start new-window) old-point)
4434 (set-window-point new-window old-point)
4435 (select-window new-window))))
9397e56f
MR
4436 ;; Always copy quit-restore parameter in interactive use.
4437 (let ((quit-restore (window-parameter old-window 'quit-restore)))
4438 (when quit-restore
4439 (set-window-parameter new-window 'quit-restore quit-restore)))
4440 new-window))
562dd5e9 4441
2d197ffb 4442(defalias 'split-window-vertically 'split-window-below)
562dd5e9 4443
2d197ffb
CY
4444(defun split-window-right (&optional size)
4445 "Split the selected window into two side-by-side windows.
4446The selected window is on the left. The newly split-off window
4447is on the right, and displays the same buffer. Return the new
4448window.
562dd5e9 4449
2d197ffb
CY
4450If optional argument SIZE is omitted or nil, both windows get the
4451same width, or close to it. If SIZE is positive, the left-hand
4452\(selected) window gets SIZE columns. If SIZE is negative, the
4453right-hand (new) window gets -SIZE columns. Here, SIZE includes
4454the width of the window's scroll bar; if there are no scroll
4455bars, it includes the width of the divider column to the window's
4456right, if any."
562dd5e9
MR
4457 (interactive "P")
4458 (let ((old-window (selected-window))
4459 (size (and size (prefix-numeric-value size)))
4460 new-window)
4461 (when (and size (< size 0) (< (- size) window-min-width))
4462 ;; `split-window' would not signal an error here.
4463 (error "Size of new window too small"))
9397e56f
MR
4464 (setq new-window (split-window nil size t))
4465 ;; Always copy quit-restore parameter in interactive use.
4466 (let ((quit-restore (window-parameter old-window 'quit-restore)))
4467 (when quit-restore
4468 (set-window-parameter new-window 'quit-restore quit-restore)))
4469 new-window))
562dd5e9 4470
2d197ffb 4471(defalias 'split-window-horizontally 'split-window-right)
562dd5e9 4472\f
6198ccd0
MR
4473;;; Balancing windows.
4474
4475;; The following routine uses the recycled code from an old version of
be7f5545
MR
4476;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
4477;; new `window--resize-child-windows' code does would hardly make it any shorter or
6198ccd0
MR
4478;; more readable (FWIW we'd need three loops - one to calculate the
4479;; minimum sizes per window, one to enlarge or shrink windows until the
4480;; new parent-size matches, and one where we shrink the largest/enlarge
4481;; the smallest window).
4482(defun balance-windows-2 (window horizontal)
4483 "Subroutine of `balance-windows-1'.
3d8daefe 4484WINDOW must be a vertical combination (horizontal if HORIZONTAL
85c2386b 4485is non-nil)."
880e6158
MR
4486 (let* ((char-size (if window-resize-pixelwise
4487 1
4488 (frame-char-size window horizontal)))
4489 (first (window-child window))
6198ccd0
MR
4490 (sub first)
4491 (number-of-children 0)
880e6158 4492 (parent-size (window-new-pixel window))
6198ccd0 4493 (total-sum parent-size)
cb882333 4494 failed size sub-total sub-delta sub-amount rest)
6198ccd0
MR
4495 (while sub
4496 (setq number-of-children (1+ number-of-children))
4497 (when (window-size-fixed-p sub horizontal)
4498 (setq total-sum
880e6158 4499 (- total-sum (window-size sub horizontal t)))
6198ccd0
MR
4500 (set-window-new-normal sub 'ignore))
4501 (setq sub (window-right sub)))
3c448ab6 4502
6198ccd0
MR
4503 (setq failed t)
4504 (while (and failed (> number-of-children 0))
4505 (setq size (/ total-sum number-of-children))
4506 (setq failed nil)
4507 (setq sub first)
4508 (while (and sub (not failed))
be7f5545
MR
4509 ;; Ignore child windows that should be ignored or are stuck.
4510 (unless (window--resize-child-windows-skip-p sub)
880e6158 4511 (setq sub-total (window-size sub horizontal t))
6198ccd0
MR
4512 (setq sub-delta (- size sub-total))
4513 (setq sub-amount
880e6158 4514 (window-sizable sub sub-delta horizontal nil t))
be7f5545 4515 ;; Register the new total size for this child window.
880e6158 4516 (set-window-new-pixel sub (+ sub-total sub-amount))
6198ccd0
MR
4517 (unless (= sub-amount sub-delta)
4518 (setq total-sum (- total-sum sub-total sub-amount))
4519 (setq number-of-children (1- number-of-children))
4520 ;; We failed and need a new round.
4521 (setq failed t)
4522 (set-window-new-normal sub 'skip)))
4523 (setq sub (window-right sub))))
3c448ab6 4524
880e6158 4525 ;; How can we be sure that `number-of-children' is NOT zero here ?
6198ccd0
MR
4526 (setq rest (% total-sum number-of-children))
4527 ;; Fix rounding by trying to enlarge non-stuck windows by one line
4528 ;; (column) until `rest' is zero.
4529 (setq sub first)
4530 (while (and sub (> rest 0))
be7f5545 4531 (unless (window--resize-child-windows-skip-p window)
880e6158
MR
4532 (set-window-new-pixel sub char-size t)
4533 (setq rest (- rest char-size)))
6198ccd0 4534 (setq sub (window-right sub)))
3c448ab6 4535
6198ccd0
MR
4536 ;; Fix rounding by trying to enlarge stuck windows by one line
4537 ;; (column) until `rest' equals zero.
4538 (setq sub first)
4539 (while (and sub (> rest 0))
4540 (unless (eq (window-new-normal sub) 'ignore)
880e6158
MR
4541 (set-window-new-pixel sub char-size t)
4542 (setq rest (- rest char-size)))
6198ccd0
MR
4543 (setq sub (window-right sub)))
4544
4545 (setq sub first)
4546 (while sub
4547 ;; Record new normal sizes.
4548 (set-window-new-normal
4549 sub (/ (if (eq (window-new-normal sub) 'ignore)
880e6158
MR
4550 (window-size sub horizontal t)
4551 (window-new-pixel sub))
6198ccd0 4552 (float parent-size)))
be7f5545 4553 ;; Recursively balance each window's child windows.
6198ccd0
MR
4554 (balance-windows-1 sub horizontal)
4555 (setq sub (window-right sub)))))
4556
4557(defun balance-windows-1 (window &optional horizontal)
4558 "Subroutine of `balance-windows'."
4559 (if (window-child window)
4560 (let ((sub (window-child window)))
3d8daefe 4561 (if (window-combined-p sub horizontal)
6198ccd0 4562 (balance-windows-2 window horizontal)
880e6158 4563 (let ((size (window-new-pixel window)))
6198ccd0 4564 (while sub
880e6158 4565 (set-window-new-pixel sub size)
6198ccd0
MR
4566 (balance-windows-1 sub horizontal)
4567 (setq sub (window-right sub))))))))
4568
4569(defun balance-windows (&optional window-or-frame)
be7f5545 4570 "Balance the sizes of windows of WINDOW-OR-FRAME.
6198ccd0
MR
4571WINDOW-OR-FRAME is optional and defaults to the selected frame.
4572If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
4c36be58 4573windows of that frame. If WINDOW-OR-FRAME denotes a window,
be7f5545 4574recursively balance the sizes of all child windows of that
6198ccd0
MR
4575window."
4576 (interactive)
4577 (let* ((window
4578 (cond
4579 ((or (not window-or-frame)
4580 (frame-live-p window-or-frame))
4581 (frame-root-window window-or-frame))
4582 ((or (window-live-p window-or-frame)
4583 (window-child window-or-frame))
4584 window-or-frame)
4585 (t
4586 (error "Not a window or frame %s" window-or-frame))))
4587 (frame (window-frame window)))
4588 ;; Balance vertically.
5386012d 4589 (window--resize-reset (window-frame window))
6198ccd0 4590 (balance-windows-1 window)
880e6158
MR
4591 (when (window--resize-apply-p frame)
4592 (window-resize-apply frame)
4593 (window--pixel-to-total frame)
4594 (run-window-configuration-change-hook frame))
6198ccd0 4595 ;; Balance horizontally.
5386012d 4596 (window--resize-reset (window-frame window) t)
6198ccd0 4597 (balance-windows-1 window t)
880e6158
MR
4598 (when (window--resize-apply-p frame t)
4599 (window-resize-apply frame)
4600 (window--pixel-to-total frame t)
4601 (run-window-configuration-change-hook frame))))
3c448ab6
MR
4602
4603(defun window-fixed-size-p (&optional window direction)
4604 "Return t if WINDOW cannot be resized in DIRECTION.
4605WINDOW defaults to the selected window. DIRECTION can be
4606nil (i.e. any), `height' or `width'."
4607 (with-current-buffer (window-buffer window)
4608 (when (and (boundp 'window-size-fixed) window-size-fixed)
4609 (not (and direction
4610 (member (cons direction window-size-fixed)
4611 '((height . width) (width . height))))))))
4612
4613;;; A different solution to balance-windows.
3c448ab6
MR
4614(defvar window-area-factor 1
4615 "Factor by which the window area should be over-estimated.
4616This is used by `balance-windows-area'.
4617Changing this globally has no effect.")
4618(make-variable-buffer-local 'window-area-factor)
4619
880e6158 4620(defun balance-windows-area-adjust (window delta horizontal pixelwise)
d615d6d2 4621 "Wrapper around `window-resize' with error checking.
6198ccd0 4622Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
d615d6d2 4623 ;; `window-resize' may fail if delta is too large.
6198ccd0
MR
4624 (while (>= (abs delta) 1)
4625 (condition-case nil
4626 (progn
880e6158
MR
4627 ;; It was wrong to use `window-resize' here. Somehow
4628 ;; `balance-windows-area' depends on resizing windows
4629 ;; asymmetrically.
4630 (adjust-window-trailing-edge window delta horizontal pixelwise)
6198ccd0
MR
4631 (setq delta 0))
4632 (error
4633 ;;(message "adjust: %s" (error-message-string err))
4634 (setq delta (/ delta 2))))))
4635
3c448ab6
MR
4636(defun balance-windows-area ()
4637 "Make all visible windows the same area (approximately).
4638See also `window-area-factor' to change the relative size of
4639specific buffers."
4640 (interactive)
4641 (let* ((unchanged 0) (carry 0) (round 0)
4642 ;; Remove fixed-size windows.
4643 (wins (delq nil (mapcar (lambda (win)
4644 (if (not (window-fixed-size-p win)) win))
4645 (window-list nil 'nomini))))
4646 (changelog nil)
880e6158
MR
4647 (pixelwise window-resize-pixelwise)
4648 next)
3c448ab6
MR
4649 ;; Resizing a window changes the size of surrounding windows in complex
4650 ;; ways, so it's difficult to balance them all. The introduction of
4651 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
4652 ;; very difficult to do. `balance-window' above takes an off-line
4653 ;; approach: get the whole window tree, then balance it, then try to
4654 ;; adjust the windows so they fit the result.
4655 ;; Here, instead, we take a "local optimization" approach, where we just
4656 ;; go through all the windows several times until nothing needs to be
4657 ;; changed. The main problem with this approach is that it's difficult
4658 ;; to make sure it terminates, so we use some heuristic to try and break
4659 ;; off infinite loops.
4660 ;; After a round without any change, we allow a second, to give a chance
4661 ;; to the carry to propagate a minor imbalance from the end back to
4662 ;; the beginning.
4663 (while (< unchanged 2)
4664 ;; (message "New round")
4665 (setq unchanged (1+ unchanged) round (1+ round))
4666 (dolist (win wins)
4667 (setq next win)
4668 (while (progn (setq next (next-window next))
4669 (window-fixed-size-p next)))
4670 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
4671 (let* ((horiz
880e6158
MR
4672 (< (car (window-pixel-edges win)) (car (window-pixel-edges next))))
4673 (areadiff (/ (- (* (window-height next pixelwise)
4674 (window-width next pixelwise)
3c448ab6
MR
4675 (buffer-local-value 'window-area-factor
4676 (window-buffer next)))
880e6158
MR
4677 (* (window-height win pixelwise)
4678 (window-width win pixelwise)
3c448ab6
MR
4679 (buffer-local-value 'window-area-factor
4680 (window-buffer win))))
4681 (max (buffer-local-value 'window-area-factor
4682 (window-buffer win))
4683 (buffer-local-value 'window-area-factor
4684 (window-buffer next)))))
4685 (edgesize (if horiz
880e6158
MR
4686 (+ (window-height win pixelwise)
4687 (window-height next pixelwise))
4688 (+ (window-width win pixelwise)
4689 (window-width next pixelwise))))
3c448ab6
MR
4690 (diff (/ areadiff edgesize)))
4691 (when (zerop diff)
4692 ;; Maybe diff is actually closer to 1 than to 0.
4693 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
4694 (when (and (zerop diff) (not (zerop areadiff)))
4695 (setq diff (/ (+ areadiff carry) edgesize))
4696 ;; Change things smoothly.
4697 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
4698 (if (zerop diff)
4699 ;; Make sure negligible differences don't accumulate to
4700 ;; become significant.
4701 (setq carry (+ carry areadiff))
6198ccd0 4702 ;; This used `adjust-window-trailing-edge' before and uses
d615d6d2 4703 ;; `window-resize' now. Error wrapping is still needed.
880e6158 4704 (balance-windows-area-adjust win diff horiz pixelwise)
3c448ab6 4705 ;; (sit-for 0.5)
880e6158 4706 (let ((change (cons win (window-pixel-edges win))))
3c448ab6
MR
4707 ;; If the same change has been seen already for this window,
4708 ;; we're most likely in an endless loop, so don't count it as
4709 ;; a change.
4710 (unless (member change changelog)
4711 (push change changelog)
4712 (setq unchanged 0 carry 0)))))))
4713 ;; We've now basically balanced all the windows.
4714 ;; But there may be some minor off-by-one imbalance left over,
4715 ;; so let's do some fine tuning.
4716 ;; (bw-finetune wins)
4717 ;; (message "Done in %d rounds" round)
4718 ))
9d89fec7
MR
4719
4720;;; Window states, how to get them and how to put them in a window.
34a02f46 4721(defun window--state-get-1 (window &optional writable)
9d89fec7
MR
4722 "Helper function for `window-state-get'."
4723 (let* ((type
4724 (cond
d68443dc
MR
4725 ((window-top-child window) 'vc)
4726 ((window-left-child window) 'hc)
9d89fec7
MR
4727 (t 'leaf)))
4728 (buffer (window-buffer window))
4729 (selected (eq window (selected-window)))
4730 (head
4b0d61e3
SM
4731 `(,type
4732 ,@(unless (window-next-sibling window) `((last . t)))
880e6158
MR
4733 (pixel-width . ,(window-pixel-width window))
4734 (pixel-height . ,(window-pixel-height window))
4735 (total-width . ,(window-total-width window))
4736 (total-height . ,(window-total-height window))
4b0d61e3
SM
4737 (normal-height . ,(window-normal-size window))
4738 (normal-width . ,(window-normal-size window t))
8bbdea0f 4739 ,@(unless (window-live-p window)
ab5340fe 4740 `((combination-limit . ,(window-combination-limit window))))
34a02f46
MR
4741 ,@(let ((parameters (window-parameters window))
4742 list)
4743 ;; Make copies of those window parameters whose
4744 ;; persistence property is `writable' if WRITABLE is
4745 ;; non-nil and non-nil if WRITABLE is nil.
4746 (dolist (par parameters)
4747 (let ((pers (cdr (assq (car par)
4748 window-persistent-parameters))))
4749 (when (and pers (or (not writable) (eq pers 'writable)))
4750 (setq list (cons (cons (car par) (cdr par)) list)))))
4751 ;; Add `clone-of' parameter if necessary.
4752 (let ((pers (cdr (assq 'clone-of
4753 window-persistent-parameters))))
4754 (when (and pers (or (not writable) (eq pers 'writable))
4755 (not (assq 'clone-of list)))
4756 (setq list (cons (cons 'clone-of window) list))))
4b0d61e3
SM
4757 (when list
4758 `((parameters . ,list))))
4759 ,@(when buffer
1edf595d 4760 ;; All buffer related things go in here.
5481664a 4761 (let ((point (window-point window))
1edf595d
MR
4762 (start (window-start window)))
4763 `((buffer
4764 ,(buffer-name buffer)
4765 (selected . ,selected)
4766 (hscroll . ,(window-hscroll window))
4767 (fringes . ,(window-fringes window))
4768 (margins . ,(window-margins window))
4769 (scroll-bars . ,(window-scroll-bars window))
4770 (vscroll . ,(window-vscroll window))
4771 (dedicated . ,(window-dedicated-p window))
de8c03dc
SM
4772 (point . ,(if writable point
4773 (copy-marker point
4774 (buffer-local-value
4775 'window-point-insertion-type
4776 buffer))))
1edf595d 4777 (start . ,(if writable start (copy-marker start)))))))))
9d89fec7
MR
4778 (tail
4779 (when (memq type '(vc hc))
4780 (let (list)
4781 (setq window (window-child window))
4782 (while window
34a02f46 4783 (setq list (cons (window--state-get-1 window writable) list))
9d89fec7
MR
4784 (setq window (window-right window)))
4785 (nreverse list)))))
4786 (append head tail)))
4787
34a02f46 4788(defun window-state-get (&optional window writable)
9d89fec7
MR
4789 "Return state of WINDOW as a Lisp object.
4790WINDOW can be any window and defaults to the root window of the
4791selected frame.
4792
34a02f46
MR
4793Optional argument WRITABLE non-nil means do not use markers for
4794sampling `window-point' and `window-start'. Together, WRITABLE
4795and the variable `window-persistent-parameters' specify which
4796window parameters are saved by this function. WRITABLE should be
4797non-nil when the return value shall be written to a file and read
4798back in another session. Otherwise, an application may run into
4799an `invalid-read-syntax' error while attempting to read back the
4800value from file.
9d89fec7
MR
4801
4802The return value can be used as argument for `window-state-put'
4803to put the state recorded here into an arbitrary window. The
4804value can be also stored on disk and read back in a new session."
4805 (setq window
4806 (if window
24300f5f 4807 (if (window-valid-p window)
9d89fec7
MR
4808 window
4809 (error "%s is not a live or internal window" window))
4810 (frame-root-window)))
4811 ;; The return value is a cons whose car specifies some constraints on
be7f5545
MR
4812 ;; the size of WINDOW. The cdr lists the states of the child windows
4813 ;; of WINDOW.
9d89fec7
MR
4814 (cons
4815 ;; Frame related things would go into a function, say `frame-state',
4816 ;; calling `window-state-get' to insert the frame's root window.
4b0d61e3
SM
4817 `((min-height . ,(window-min-size window))
4818 (min-width . ,(window-min-size window t))
4819 (min-height-ignore . ,(window-min-size window nil t))
4820 (min-width-ignore . ,(window-min-size window t t))
4821 (min-height-safe . ,(window-min-size window nil 'safe))
880e6158
MR
4822 (min-width-safe . ,(window-min-size window t 'safe))
4823 (min-pixel-height . ,(window-min-size window nil nil t))
4824 (min-pixel-width . ,(window-min-size window t nil t))
4825 (min-pixel-height-ignore . ,(window-min-size window nil t t))
4826 (min-pixel-width-ignore . ,(window-min-size window t t t))
4827 (min-pixel-height-safe . ,(window-min-size window nil 'safe t))
4828 (min-pixel-width-safe . ,(window-min-size window t 'safe t)))
34a02f46 4829 (window--state-get-1 window writable)))
9d89fec7
MR
4830
4831(defvar window-state-put-list nil
4832 "Helper variable for `window-state-put'.")
4833
34ada5f4
MR
4834(defvar window-state-put-stale-windows nil
4835 "Helper variable for `window-state-put'.")
4836
880e6158 4837(defun window--state-put-1 (state &optional window ignore totals pixelwise)
9d89fec7
MR
4838 "Helper function for `window-state-put'."
4839 (let ((type (car state)))
4840 (setq state (cdr state))
4841 (cond
4842 ((eq type 'leaf)
4843 ;; For a leaf window just add unprocessed entries to
4844 ;; `window-state-put-list'.
c7635a97 4845 (push (cons window state) window-state-put-list))
9d89fec7
MR
4846 ((memq type '(vc hc))
4847 (let* ((horizontal (eq type 'hc))
880e6158 4848 (total (window-size window horizontal pixelwise))
9d89fec7
MR
4849 (first t)
4850 size new)
4851 (dolist (item state)
4852 ;; Find the next child window. WINDOW always points to the
4853 ;; real window that we want to fill with what we find here.
4854 (when (memq (car item) '(leaf vc hc))
4855 (if (assq 'last item)
c56cad4a 4856 ;; The last child window. Below `window--state-put-1'
9d89fec7
MR
4857 ;; will put into it whatever ITEM has in store.
4858 (setq new nil)
4859 ;; Not the last child window, prepare for splitting
4860 ;; WINDOW. SIZE is the new (and final) size of the old
4861 ;; window.
4862 (setq size
4863 (if totals
4864 ;; Use total size.
880e6158
MR
4865 (if pixelwise
4866 (cdr (assq (if horizontal
4867 'pixel-width
4868 'pixel-height)
4869 item))
4870 (cdr (assq (if horizontal
4871 'total-width
4872 'total-height)
4873 item)))
9d89fec7 4874 ;; Use normalized size and round.
880e6158
MR
4875 (round
4876 (* total
4877 (cdr (assq (if horizontal 'normal-width 'normal-height)
4878 item))))))
9d89fec7
MR
4879
4880 ;; Use safe sizes, we try to resize later.
880e6158
MR
4881 (setq size (max size
4882 (if horizontal
4883 (* window-safe-min-width
4884 (if pixelwise
4885 (frame-char-width (window-frame window))
4886 1))
4887 (* window-safe-min-height
4888 (if pixelwise
4889 (frame-char-height (window-frame window))
4890 1)))))
4891 (if (window-sizable-p window (- size) horizontal 'safe pixelwise)
b6f67890
MR
4892 (let* ((window-combination-limit
4893 (assq 'combination-limit item)))
301b181a 4894 ;; We must inherit the combination limit, otherwise
b6f67890
MR
4895 ;; we might mess up handling of atomic and side
4896 ;; window.
880e6158 4897 (setq new (split-window window size horizontal pixelwise)))
9d89fec7
MR
4898 ;; Give up if we can't resize window down to safe sizes.
4899 (error "Cannot resize window %s" window))
4900
4901 (when first
4902 (setq first nil)
4903 ;; When creating the first child window add for parent
4904 ;; unprocessed entries to `window-state-put-list'.
4905 (setq window-state-put-list
4906 (cons (cons (window-parent window) state)
4907 window-state-put-list))))
4908
4909 ;; Now process the current window (either the one we've just
4910 ;; split or the last child of its parent).
c56cad4a 4911 (window--state-put-1 item window ignore totals)
9d89fec7
MR
4912 ;; Continue with the last window split off.
4913 (setq window new))))))))
4914
880e6158 4915(defun window--state-put-2 (ignore pixelwise)
9d89fec7
MR
4916 "Helper function for `window-state-put'."
4917 (dolist (item window-state-put-list)
4918 (let ((window (car item))
b6f67890 4919 (combination-limit (cdr (assq 'combination-limit item)))
9d89fec7
MR
4920 (parameters (cdr (assq 'parameters item)))
4921 (state (cdr (assq 'buffer item))))
b6f67890
MR
4922 (when combination-limit
4923 (set-window-combination-limit window combination-limit))
34a02f46
MR
4924 ;; Reset window's parameters and assign saved ones (we might want
4925 ;; a `remove-window-parameters' function here).
4926 (dolist (parameter (window-parameters window))
4927 (set-window-parameter window (car parameter) nil))
9d89fec7
MR
4928 (when parameters
4929 (dolist (parameter parameters)
34a02f46 4930 (set-window-parameter window (car parameter) (cdr parameter))))
9d89fec7
MR
4931 ;; Process buffer related state.
4932 (when state
34ada5f4
MR
4933 (let ((buffer (get-buffer (car state))))
4934 (if buffer
c089653d
MR
4935 (with-current-buffer buffer
4936 (set-window-buffer window buffer)
4937 (set-window-hscroll window (cdr (assq 'hscroll state)))
4938 (apply 'set-window-fringes
4939 (cons window (cdr (assq 'fringes state))))
4940 (let ((margins (cdr (assq 'margins state))))
4941 (set-window-margins window (car margins) (cdr margins)))
4942 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
4943 (set-window-scroll-bars
4944 window (car scroll-bars) (nth 2 scroll-bars)
4945 (nth 3 scroll-bars)))
4946 (set-window-vscroll window (cdr (assq 'vscroll state)))
4947 ;; Adjust vertically.
4948 (if (memq window-size-fixed '(t height))
4949 ;; A fixed height window, try to restore the
4950 ;; original size.
880e6158
MR
4951 (let ((delta
4952 (- (cdr (assq
4953 (if pixelwise 'pixel-height 'total-height)
4954 item))
4955 (window-size window nil pixelwise)))
c089653d 4956 window-size-fixed)
880e6158
MR
4957 (when (window--resizable-p
4958 window delta nil nil nil nil nil pixelwise)
4959 (window-resize window delta nil nil pixelwise)))
c089653d 4960 ;; Else check whether the window is not high enough.
880e6158
MR
4961 (let* ((min-size
4962 (window-min-size window nil ignore pixelwise))
4963 (delta
4964 (- min-size (window-size window nil pixelwise))))
c089653d 4965 (when (and (> delta 0)
880e6158
MR
4966 (window--resizable-p
4967 window delta nil ignore nil nil nil pixelwise))
4968 (window-resize window delta nil ignore pixelwise))))
c089653d
MR
4969 ;; Adjust horizontally.
4970 (if (memq window-size-fixed '(t width))
4971 ;; A fixed width window, try to restore the original
4972 ;; size.
880e6158
MR
4973 (let ((delta
4974 (- (cdr (assq
4975 (if pixelwise 'pixel-width 'total-width)
4976 item))
4977 (window-size window t pixelwise)))
c089653d 4978 window-size-fixed)
880e6158
MR
4979 (when (window--resizable-p
4980 window delta nil nil nil nil nil pixelwise)
4981 (window-resize window delta nil nil pixelwise)))
c089653d 4982 ;; Else check whether the window is not wide enough.
880e6158
MR
4983 (let* ((min-size (window-min-size window t ignore pixelwise))
4984 (delta (- min-size (window-size window t pixelwise))))
c089653d 4985 (when (and (> delta 0)
880e6158
MR
4986 (window--resizable-p
4987 window delta t ignore nil nil nil pixelwise))
4988 (window-resize window delta t ignore pixelwise))))
c089653d
MR
4989 ;; Set dedicated status.
4990 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
4991 ;; Install positions (maybe we should do this after all
4992 ;; windows have been created and sized).
4993 (ignore-errors
4994 (set-window-start window (cdr (assq 'start state)))
4995 (set-window-point window (cdr (assq 'point state))))
4996 ;; Select window if it's the selected one.
4997 (when (cdr (assq 'selected state))
4998 (select-window window)))
4999 ;; We don't want to raise an error in case the buffer does
5000 ;; not exist anymore, so we switch to a previous one and
5001 ;; save the window with the intention of deleting it later
5002 ;; if possible.
34ada5f4 5003 (switch-to-prev-buffer window)
c089653d 5004 (push window window-state-put-stale-windows)))))))
9d89fec7
MR
5005
5006(defun window-state-put (state &optional window ignore)
5007 "Put window state STATE into WINDOW.
5008STATE should be the state of a window returned by an earlier
5009invocation of `window-state-get'. Optional argument WINDOW must
5010specify a live window and defaults to the selected one.
5011
5012Optional argument IGNORE non-nil means ignore minimum window
5013sizes and fixed size restrictions. IGNORE equal `safe' means
be7f5545 5014windows can get as small as `window-safe-min-height' and
9d89fec7 5015`window-safe-min-width'."
34ada5f4 5016 (setq window-state-put-stale-windows nil)
447f16b8 5017 (setq window (window-normalize-window window t))
9d89fec7
MR
5018 (let* ((frame (window-frame window))
5019 (head (car state))
5020 ;; We check here (1) whether the total sizes of root window of
5021 ;; STATE and that of WINDOW are equal so we can avoid
5022 ;; calculating new sizes, and (2) if we do have to resize
5023 ;; whether we can do so without violating size restrictions.
880e6158
MR
5024 (pixelwise (and (cdr (assq 'pixel-width state))
5025 (cdr (assq 'pixel-height state))))
5026 (totals (or (and pixelwise
5027 (= (window-pixel-width window)
5028 (cdr (assq 'pixel-width state)))
5029 (= (window-pixel-height window)
5030 (cdr (assq 'pixel-height state))))
5031 (and (= (window-total-width window)
5032 (cdr (assq 'total-width state)))
5033 (= (window-total-height window)
5034 (cdr (assq 'total-height state))))))
5035 (min-height (cdr (assq
5036 (if pixelwise 'min-pixel-height 'min-height)
5037 head)))
5038 (min-width (cdr (assq
5039 (if pixelwise 'min-pixel-width 'min-weight)
5040 head))))
9d89fec7 5041 (if (and (not totals)
880e6158
MR
5042 (or (> min-height (window-size window nil pixelwise))
5043 (> min-width (window-size window t pixelwise)))
9d89fec7
MR
5044 (or (not ignore)
5045 (and (setq min-height
880e6158
MR
5046 (cdr (assq
5047 (if pixelwise
5048 'min-pixel-height-ignore
5049 'min-height-ignore)
5050 head)))
9d89fec7 5051 (setq min-width
880e6158
MR
5052 (cdr (assq
5053 (if pixelwise
5054 'min-pixel-width-ignore
5055 'min-width-ignore)
5056 head)))
5057 (or (> min-height
5058 (window-size window nil pixelwise))
5059 (> min-width
5060 (window-size window t pixelwise)))
9d89fec7
MR
5061 (or (not (eq ignore 'safe))
5062 (and (setq min-height
880e6158
MR
5063 (cdr (assq
5064 (if pixelwise
5065 'min-pixel-height-safe
5066 'min-height-safe)
5067 head)))
9d89fec7 5068 (setq min-width
880e6158
MR
5069 (cdr (assq
5070 (if pixelwise
5071 'min-pixel-width-safe
5072 'min-width-safe)
5073 head)))
9d89fec7 5074 (or (> min-height
880e6158 5075 (window-size window nil pixelwise))
9d89fec7 5076 (> min-width
880e6158 5077 (window-size window t pixelwise))))))))
9d89fec7
MR
5078 ;; The check above might not catch all errors due to rounding
5079 ;; issues - so IGNORE equal 'safe might not always produce the
5080 ;; minimum possible state. But such configurations hardly make
5081 ;; sense anyway.
a91adc7e 5082 (error "Window %s too small to accommodate state" window)
9d89fec7
MR
5083 (setq state (cdr state))
5084 (setq window-state-put-list nil)
5085 ;; Work on the windows of a temporary buffer to make sure that
5086 ;; splitting proceeds regardless of any buffer local values of
5087 ;; `window-size-fixed'. Release that buffer after the buffers of
c56cad4a 5088 ;; all live windows have been set by `window--state-put-2'.
9d89fec7
MR
5089 (with-temp-buffer
5090 (set-window-buffer window (current-buffer))
880e6158
MR
5091 (window--state-put-1 state window nil totals pixelwise)
5092 (window--state-put-2 ignore pixelwise))
34ada5f4
MR
5093 (while window-state-put-stale-windows
5094 (let ((window (pop window-state-put-stale-windows)))
5095 (when (eq (window-deletable-p window) t)
5096 (delete-window window))))
54f9154c 5097 (window--check frame))))
9481c002 5098\f
f818cd2a
MR
5099(defun display-buffer-record-window (type window buffer)
5100 "Record information for window used by `display-buffer'.
cf4eacfd 5101TYPE specifies the type of the calling operation and must be one
382c953b
JB
5102of the symbols 'reuse (when WINDOW existed already and was
5103reused for displaying BUFFER), 'window (when WINDOW was created
5104on an already existing frame), or 'frame (when WINDOW was
5105created on a new frame). WINDOW is the window used for or created
cf4eacfd
MR
5106by the `display-buffer' routines. BUFFER is the buffer that
5107shall be displayed.
5108
5109This function installs or updates the quit-restore parameter of
5110WINDOW. The quit-restore parameter is a list of four elements:
5111The first element is one of the symbols 'window, 'frame, 'same or
5112'other. The second element is either one of the symbols 'window
5113or 'frame or a list whose elements are the buffer previously
5114shown in the window, that buffer's window start and window point,
5115and the window's height. The third element is the window
5116selected at the time the parameter was created. The fourth
5117element is BUFFER."
f818cd2a 5118 (cond
cf4eacfd
MR
5119 ((eq type 'reuse)
5120 (if (eq (window-buffer window) buffer)
5121 ;; WINDOW shows BUFFER already.
5122 (when (consp (window-parameter window 'quit-restore))
5123 ;; If WINDOW has a quit-restore parameter, reset its car.
5124 (setcar (window-parameter window 'quit-restore) 'same))
5125 ;; WINDOW shows another buffer.
92346275
MR
5126 (with-current-buffer (window-buffer window)
5127 (set-window-parameter
5128 window 'quit-restore
5129 (list 'other
5130 ;; A quadruple of WINDOW's buffer, start, point and height.
5131 (list (current-buffer) (window-start window)
5132 ;; Preserve window-point-insertion-type (Bug#12588).
5133 (copy-marker
5134 (window-point window) window-point-insertion-type)
880e6158 5135 (window-total-height window))
92346275 5136 (selected-window) buffer)))))
cf4eacfd
MR
5137 ((eq type 'window)
5138 ;; WINDOW has been created on an existing frame.
f818cd2a 5139 (set-window-parameter
cf4eacfd
MR
5140 window 'quit-restore
5141 (list 'window 'window (selected-window) buffer)))
5142 ((eq type 'frame)
5143 ;; WINDOW has been created on a new frame.
f818cd2a 5144 (set-window-parameter
cf4eacfd
MR
5145 window 'quit-restore
5146 (list 'frame 'frame (selected-window) buffer)))))
9481c002 5147
f818cd2a
MR
5148(defcustom display-buffer-function nil
5149 "If non-nil, function to call to handle `display-buffer'.
5150It will receive two args, the buffer and a flag which if non-nil
5151means that the currently selected window is not acceptable. It
5152should choose or create a window, display the specified buffer in
5153it, and return the window.
5154
e1c2c6f2
MR
5155The specified function should call `display-buffer-record-window'
5156with corresponding arguments to set up the quit-restore parameter
5157of the window used."
f818cd2a
MR
5158 :type '(choice
5159 (const nil)
5160 (function :tag "function"))
3c448ab6 5161 :group 'windows)
9481c002 5162
71ce58e7
CY
5163(make-obsolete-variable 'display-buffer-function
5164 'display-buffer-alist "24.3")
5165
d97af5a0
CY
5166;; Eventually, we want to turn this into a defvar; instead of
5167;; customizing this, the user should use a `pop-up-frame-parameters'
5168;; alist entry in `display-buffer-base-action'.
f818cd2a
MR
5169(defcustom pop-up-frame-alist nil
5170 "Alist of parameters for automatically generated new frames.
f818cd2a
MR
5171If non-nil, the value you specify here is used by the default
5172`pop-up-frame-function' for the creation of new frames.
9481c002 5173
f818cd2a
MR
5174Since `pop-up-frame-function' is used by `display-buffer' for
5175making new frames, any value specified here by default affects
5176the automatic generation of new frames via `display-buffer' and
5177all functions based on it. The behavior of `make-frame' is not
5178affected by this variable."
9481c002 5179 :type '(repeat (cons :format "%v"
f818cd2a
MR
5180 (symbol :tag "Parameter")
5181 (sexp :tag "Value")))
9481c002 5182 :group 'frames)
9481c002 5183
f818cd2a
MR
5184(defcustom pop-up-frame-function
5185 (lambda () (make-frame pop-up-frame-alist))
5186 "Function used by `display-buffer' for creating a new frame.
5187This function is called with no arguments and should return a new
5188frame. The default value calls `make-frame' with the argument
5189`pop-up-frame-alist'."
9481c002 5190 :type 'function
9481c002 5191 :group 'frames)
3c448ab6 5192
56f31926
MR
5193(defcustom special-display-buffer-names nil
5194 "List of names of buffers that should be displayed specially.
5195Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5196its name is in this list, displays the buffer in a way specified
5197by `special-display-function'. `special-display-popup-frame'
5198\(the default for `special-display-function') usually displays
5199the buffer in a separate frame made with the parameters specified
5200by `special-display-frame-alist'. If `special-display-function'
5201has been set to some other function, that function is called with
5202the buffer as first, and nil as second argument.
5203
5204Alternatively, an element of this list can be specified as
5205\(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
382c953b 5206name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
56f31926
MR
5207`special-display-popup-frame' will interpret such pairs as frame
5208parameters when it creates a special frame, overriding the
5209corresponding values from `special-display-frame-alist'.
5210
5211As a special case, if FRAME-PARAMETERS contains (same-window . t)
5212`special-display-popup-frame' displays that buffer in the
5213selected window. If FRAME-PARAMETERS contains (same-frame . t),
5214it displays that buffer in a window on the selected frame.
5215
5216If `special-display-function' specifies some other function than
5217`special-display-popup-frame', that function is called with the
5218buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5219argument.
5220
5221Finally, an element of this list can be also specified as
5222\(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5223`special-display-popup-frame' will call FUNCTION with the buffer
5224named BUFFER-NAME as first argument, and OTHER-ARGS as the
0563dae9
MR
5225second.
5226
5227Any alternative function specified here is responsible for
5228setting up the quit-restore parameter of the window used.
56f31926
MR
5229
5230If this variable appears \"not to work\", because you added a
5231name to it but the corresponding buffer is displayed in the
5232selected window, look at the values of `same-window-buffer-names'
5233and `same-window-regexps'. Those variables take precedence over
5234this one.
5235
5236See also `special-display-regexps'."
5237 :type '(repeat
5238 (choice :tag "Buffer"
5239 :value ""
5240 (string :format "%v")
5241 (cons :tag "With parameters"
5242 :format "%v"
5243 :value ("" . nil)
5244 (string :format "%v")
5245 (repeat :tag "Parameters"
5246 (cons :format "%v"
5247 (symbol :tag "Parameter")
5248 (sexp :tag "Value"))))
5249 (list :tag "With function"
5250 :format "%v"
5251 :value ("" . nil)
5252 (string :format "%v")
5253 (function :tag "Function")
5254 (repeat :tag "Arguments" (sexp)))))
5255 :group 'windows
5256 :group 'frames)
77f1f99c 5257(make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3")
ac549fa5
GM
5258(put 'special-display-buffer-names 'risky-local-variable t)
5259
56f31926
MR
5260(defcustom special-display-regexps nil
5261 "List of regexps saying which buffers should be displayed specially.
5262Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5263any regexp in this list matches its name, displays it specially
5264using `special-display-function'. `special-display-popup-frame'
5265\(the default for `special-display-function') usually displays
5266the buffer in a separate frame made with the parameters specified
5267by `special-display-frame-alist'. If `special-display-function'
5268has been set to some other function, that function is called with
5269the buffer as first, and nil as second argument.
5270
5271Alternatively, an element of this list can be specified as
5272\(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
5273FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5274`special-display-popup-frame' will then interpret these pairs as
5275frame parameters when creating a special frame for a buffer whose
5276name matches REGEXP, overriding the corresponding values from
5277`special-display-frame-alist'.
5278
5279As a special case, if FRAME-PARAMETERS contains (same-window . t)
5280`special-display-popup-frame' displays buffers matching REGEXP in
382c953b 5281the selected window. (same-frame . t) in FRAME-PARAMETERS means
56f31926
MR
5282to display such buffers in a window on the selected frame.
5283
5284If `special-display-function' specifies some other function than
5285`special-display-popup-frame', that function is called with the
5286buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
5287as second argument.
5288
5289Finally, an element of this list can be also specified as
5290\(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
5291will then call FUNCTION with the buffer whose name matched
0563dae9
MR
5292REGEXP as first, and OTHER-ARGS as second argument.
5293
5294Any alternative function specified here is responsible for
5295setting up the quit-restore parameter of the window used.
56f31926
MR
5296
5297If this variable appears \"not to work\", because you added a
5298name to it but the corresponding buffer is displayed in the
5299selected window, look at the values of `same-window-buffer-names'
5300and `same-window-regexps'. Those variables take precedence over
5301this one.
5302
5303See also `special-display-buffer-names'."
5304 :type '(repeat
5305 (choice :tag "Buffer"
5306 :value ""
5307 (regexp :format "%v")
5308 (cons :tag "With parameters"
5309 :format "%v"
5310 :value ("" . nil)
5311 (regexp :format "%v")
5312 (repeat :tag "Parameters"
5313 (cons :format "%v"
5314 (symbol :tag "Parameter")
5315 (sexp :tag "Value"))))
5316 (list :tag "With function"
5317 :format "%v"
5318 :value ("" . nil)
5319 (regexp :format "%v")
5320 (function :tag "Function")
5321 (repeat :tag "Arguments" (sexp)))))
5322 :group 'windows
5323 :group 'frames)
77f1f99c
CY
5324(make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3")
5325(put 'special-display-regexps 'risky-local-variable t)
56f31926 5326
3c448ab6
MR
5327(defun special-display-p (buffer-name)
5328 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
56f31926
MR
5329More precisely, return t if `special-display-buffer-names' or
5330`special-display-regexps' contain a string entry equaling or
5331matching BUFFER-NAME. If `special-display-buffer-names' or
5332`special-display-regexps' contain a list entry whose car equals
5333or matches BUFFER-NAME, the return value is the cdr of that
5334entry."
f818cd2a 5335 (let (tmp)
98722073 5336 (cond
f818cd2a 5337 ((member buffer-name special-display-buffer-names)
98722073 5338 t)
f818cd2a 5339 ((setq tmp (assoc buffer-name special-display-buffer-names))
98722073
MR
5340 (cdr tmp))
5341 ((catch 'found
f818cd2a 5342 (dolist (regexp special-display-regexps)
98722073
MR
5343 (cond
5344 ((stringp regexp)
5345 (when (string-match-p regexp buffer-name)
5346 (throw 'found t)))
5347 ((and (consp regexp) (stringp (car regexp))
5348 (string-match-p (car regexp) buffer-name))
5349 (throw 'found (cdr regexp))))))))))
9481c002 5350
f818cd2a
MR
5351(defcustom special-display-frame-alist
5352 '((height . 14) (width . 80) (unsplittable . t))
5353 "Alist of parameters for special frames.
5354Special frames are used for buffers whose names are listed in
5355`special-display-buffer-names' and for buffers whose names match
5356one of the regular expressions in `special-display-regexps'.
9481c002 5357
f818cd2a 5358This variable can be set in your init file, like this:
9481c002 5359
f818cd2a
MR
5360 (setq special-display-frame-alist '((width . 80) (height . 20)))
5361
5362These supersede the values given in `default-frame-alist'."
5363 :type '(repeat (cons :format "%v"
5364 (symbol :tag "Parameter")
5365 (sexp :tag "Value")))
5366 :group 'frames)
77f1f99c 5367(make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3")
f818cd2a
MR
5368
5369(defun special-display-popup-frame (buffer &optional args)
31cd32c9 5370 "Pop up a frame displaying BUFFER and return its window.
f818cd2a
MR
5371If BUFFER is already displayed in a visible or iconified frame,
5372raise that frame. Otherwise, display BUFFER in a new frame.
5373
5374Optional argument ARGS is a list specifying additional
5375information.
5376
5377If ARGS is an alist, use it as a list of frame parameters. If
382c953b
JB
5378these parameters contain (same-window . t), display BUFFER in
5379the selected window. If they contain (same-frame . t), display
f818cd2a
MR
5380BUFFER in a window of the selected frame.
5381
5382If ARGS is a list whose car is a symbol, use (car ARGS) as a
fdc2806d
CY
5383function to do the work. Pass it BUFFER as first argument, and
5384pass the elements of (cdr ARGS) as the remaining arguments."
f818cd2a
MR
5385 (if (and args (symbolp (car args)))
5386 (apply (car args) buffer (cdr args))
5387 (let ((window (get-buffer-window buffer 0)))
5388 (or
5389 ;; If we have a window already, make it visible.
5390 (when window
5391 (let ((frame (window-frame window)))
5392 (make-frame-visible frame)
5393 (raise-frame frame)
cf4eacfd 5394 (display-buffer-record-window 'reuse window buffer)
f818cd2a
MR
5395 window))
5396 ;; Reuse the current window if the user requested it.
5397 (when (cdr (assq 'same-window args))
5398 (condition-case nil
5399 (progn (switch-to-buffer buffer nil t) (selected-window))
5400 (error nil)))
5401 ;; Stay on the same frame if requested.
5402 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
5403 (let* ((pop-up-windows t)
5404 pop-up-frames
5405 special-display-buffer-names special-display-regexps)
5406 (display-buffer buffer)))
5407 ;; If no window yet, make one in a new frame.
e75852fd
MR
5408 (let* ((frame
5409 (with-current-buffer buffer
5410 (make-frame (append args special-display-frame-alist))))
5411 (window (frame-selected-window frame)))
5412 (display-buffer-record-window 'frame window buffer)
c5e28e39
MR
5413 (unless (eq buffer (window-buffer window))
5414 (set-window-buffer window buffer)
5415 (set-window-prev-buffers window nil))
e75852fd
MR
5416 (set-window-dedicated-p window t)
5417 window)))))
f818cd2a
MR
5418
5419(defcustom special-display-function 'special-display-popup-frame
5420 "Function to call for displaying special buffers.
5421This function is called with two arguments - the buffer and,
5422optionally, a list - and should return a window displaying that
5423buffer. The default value usually makes a separate frame for the
5424buffer using `special-display-frame-alist' to specify the frame
5425parameters. See the definition of `special-display-popup-frame'
5426for how to specify such a function.
5427
5428A buffer is special when its name is either listed in
5429`special-display-buffer-names' or matches a regexp in
5430`special-display-regexps'.
5431
e1c2c6f2
MR
5432The specified function should call `display-buffer-record-window'
5433with corresponding arguments to set up the quit-restore parameter
5434of the window used."
f818cd2a
MR
5435 :type 'function
5436 :group 'frames)
77f1f99c 5437(make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3")
f818cd2a
MR
5438
5439(defcustom same-window-buffer-names nil
5440 "List of names of buffers that should appear in the \"same\" window.
5441`display-buffer' and `pop-to-buffer' show a buffer whose name is
5442on this list in the selected rather than some other window.
5443
5444An element of this list can be a cons cell instead of just a
5445string. In that case, the cell's car must be a string specifying
5446the buffer name. This is for compatibility with
5447`special-display-buffer-names'; the cdr of the cons cell is
5448ignored.
5449
5450See also `same-window-regexps'."
5451 :type '(repeat (string :format "%v"))
5452 :group 'windows)
5453
5454(defcustom same-window-regexps nil
5455 "List of regexps saying which buffers should appear in the \"same\" window.
5456`display-buffer' and `pop-to-buffer' show a buffer whose name
5457matches a regexp on this list in the selected rather than some
5458other window.
5459
5460An element of this list can be a cons cell instead of just a
5461string. In that case, the cell's car must be a regexp matching
5462the buffer name. This is for compatibility with
5463`special-display-regexps'; the cdr of the cons cell is ignored.
9481c002 5464
f818cd2a
MR
5465See also `same-window-buffer-names'."
5466 :type '(repeat (regexp :format "%v"))
5467 :group 'windows)
9481c002 5468
f818cd2a
MR
5469(defun same-window-p (buffer-name)
5470 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5471This function returns non-nil if `display-buffer' or
5472`pop-to-buffer' would show a buffer named BUFFER-NAME in the
382c953b 5473selected rather than (as usual) some other window. See
f818cd2a
MR
5474`same-window-buffer-names' and `same-window-regexps'."
5475 (cond
5476 ((not (stringp buffer-name)))
5477 ;; The elements of `same-window-buffer-names' can be buffer
5478 ;; names or cons cells whose cars are buffer names.
5479 ((member buffer-name same-window-buffer-names))
5480 ((assoc buffer-name same-window-buffer-names))
5481 ((catch 'found
5482 (dolist (regexp same-window-regexps)
5483 ;; The elements of `same-window-regexps' can be regexps
5484 ;; or cons cells whose cars are regexps.
5485 (when (or (and (stringp regexp)
cb882333 5486 (string-match-p regexp buffer-name))
f818cd2a
MR
5487 (and (consp regexp) (stringp (car regexp))
5488 (string-match-p (car regexp) buffer-name)))
5489 (throw 'found t)))))))
3c448ab6 5490
d1067961 5491(defcustom pop-up-frames nil
3c448ab6 5492 "Whether `display-buffer' should make a separate frame.
d1f18ec0 5493If nil, never make a separate frame.
3c448ab6
MR
5494If the value is `graphic-only', make a separate frame
5495on graphic displays only.
5496Any other non-nil value means always make a separate frame."
5497 :type '(choice
5498 (const :tag "Never" nil)
5499 (const :tag "On graphic displays only" graphic-only)
5500 (const :tag "Always" t))
f818cd2a 5501 :group 'windows)
3c448ab6 5502
d1067961 5503(defcustom display-buffer-reuse-frames nil
f818cd2a 5504 "Non-nil means `display-buffer' should reuse frames.
3c448ab6
MR
5505If the buffer in question is already displayed in a frame, raise
5506that frame."
5507 :type 'boolean
d1067961 5508 :version "21.1"
f818cd2a 5509 :group 'windows)
24777832
CY
5510
5511(make-obsolete-variable
5512 'display-buffer-reuse-frames
5513 "use a `reusable-frames' alist entry in `display-buffer-alist'."
5514 "24.3")
3c448ab6 5515
4dc2a129
MR
5516(defcustom pop-up-windows t
5517 "Non-nil means `display-buffer' should make a new window."
3c448ab6
MR
5518 :type 'boolean
5519 :group 'windows)
5520
8b10a2d1 5521(defcustom split-window-preferred-function 'split-window-sensibly
f818cd2a 5522 "Function called by `display-buffer' routines to split a window.
8b10a2d1
MR
5523This function is called with a window as single argument and is
5524supposed to split that window and return the new window. If the
5525window can (or shall) not be split, it is supposed to return nil.
5526The default is to call the function `split-window-sensibly' which
5527tries to split the window in a way which seems most suitable.
5528You can customize the options `split-height-threshold' and/or
5529`split-width-threshold' in order to have `split-window-sensibly'
5530prefer either vertical or horizontal splitting.
5531
f818cd2a
MR
5532If you set this to any other function, bear in mind that the
5533`display-buffer' routines may call this function two times. The
5534argument of the first call is the largest window on its frame.
5535If that call fails to return a live window, the function is
5536called again with the least recently used window as argument. If
5537that call fails too, `display-buffer' will use an existing window
5538to display its buffer.
8b10a2d1
MR
5539
5540The window selected at the time `display-buffer' was invoked is
5541still selected when this function is called. Hence you can
5542compare the window argument with the value of `selected-window'
5543if you intend to split the selected window instead or if you do
5544not want to split the selected window."
5545 :type 'function
3c448ab6
MR
5546 :version "23.1"
5547 :group 'windows)
5548
8b10a2d1 5549(defcustom split-height-threshold 80
f818cd2a
MR
5550 "Minimum height for splitting windows sensibly.
5551If this is an integer, `split-window-sensibly' may split a window
8b10a2d1 5552vertically only if it has at least this many lines. If this is
f818cd2a
MR
5553nil, `split-window-sensibly' is not allowed to split a window
5554vertically. If, however, a window is the only window on its
5555frame, `split-window-sensibly' may split it vertically
5556disregarding the value of this variable."
8b10a2d1 5557 :type '(choice (const nil) (integer :tag "lines"))
3c448ab6
MR
5558 :version "23.1"
5559 :group 'windows)
5560
8b10a2d1 5561(defcustom split-width-threshold 160
f818cd2a
MR
5562 "Minimum width for splitting windows sensibly.
5563If this is an integer, `split-window-sensibly' may split a window
8b10a2d1 5564horizontally only if it has at least this many columns. If this
f818cd2a
MR
5565is nil, `split-window-sensibly' is not allowed to split a window
5566horizontally."
8b10a2d1 5567 :type '(choice (const nil) (integer :tag "columns"))
3c448ab6
MR
5568 :version "23.1"
5569 :group 'windows)
9481c002 5570
8b10a2d1
MR
5571(defun window-splittable-p (window &optional horizontal)
5572 "Return non-nil if `split-window-sensibly' may split WINDOW.
5573Optional argument HORIZONTAL nil or omitted means check whether
5574`split-window-sensibly' may split WINDOW vertically. HORIZONTAL
5575non-nil means check whether WINDOW may be split horizontally.
3c448ab6 5576
8b10a2d1 5577WINDOW may be split vertically when the following conditions
3c448ab6 5578hold:
3c448ab6
MR
5579- `window-size-fixed' is either nil or equals `width' for the
5580 buffer of WINDOW.
8b10a2d1 5581- `split-height-threshold' is an integer and WINDOW is at least as
3c448ab6 5582 high as `split-height-threshold'.
3c448ab6
MR
5583- When WINDOW is split evenly, the emanating windows are at least
5584 `window-min-height' lines tall and can accommodate at least one
5585 line plus - if WINDOW has one - a mode line.
5586
8b10a2d1 5587WINDOW may be split horizontally when the following conditions
3c448ab6 5588hold:
3c448ab6
MR
5589- `window-size-fixed' is either nil or equals `height' for the
5590 buffer of WINDOW.
8b10a2d1 5591- `split-width-threshold' is an integer and WINDOW is at least as
3c448ab6 5592 wide as `split-width-threshold'.
3c448ab6
MR
5593- When WINDOW is split evenly, the emanating windows are at least
5594 `window-min-width' or two (whichever is larger) columns wide."
5595 (when (window-live-p window)
5596 (with-current-buffer (window-buffer window)
5597 (if horizontal
5598 ;; A window can be split horizontally when its width is not
5599 ;; fixed, it is at least `split-width-threshold' columns wide
5600 ;; and at least twice as wide as `window-min-width' and 2 (the
5601 ;; latter value is hardcoded).
5602 (and (memq window-size-fixed '(nil height))
5603 ;; Testing `window-full-width-p' here hardly makes any
5604 ;; sense nowadays. This can be done more intuitively by
5605 ;; setting up `split-width-threshold' appropriately.
5606 (numberp split-width-threshold)
5607 (>= (window-width window)
5608 (max split-width-threshold
5609 (* 2 (max window-min-width 2)))))
5610 ;; A window can be split vertically when its height is not
5611 ;; fixed, it is at least `split-height-threshold' lines high,
5612 ;; and it is at least twice as high as `window-min-height' and 2
37269466 5613 ;; if it has a mode line or 1.
3c448ab6
MR
5614 (and (memq window-size-fixed '(nil width))
5615 (numberp split-height-threshold)
5616 (>= (window-height window)
5617 (max split-height-threshold
5618 (* 2 (max window-min-height
5619 (if mode-line-format 2 1))))))))))
5620
2dc2a609 5621(defun split-window-sensibly (&optional window)
8b10a2d1 5622 "Split WINDOW in a way suitable for `display-buffer'.
2dc2a609 5623WINDOW defaults to the currently selected window.
8b10a2d1
MR
5624If `split-height-threshold' specifies an integer, WINDOW is at
5625least `split-height-threshold' lines tall and can be split
5626vertically, split WINDOW into two windows one above the other and
5627return the lower window. Otherwise, if `split-width-threshold'
5628specifies an integer, WINDOW is at least `split-width-threshold'
5629columns wide and can be split horizontally, split WINDOW into two
5630windows side by side and return the window on the right. If this
5631can't be done either and WINDOW is the only window on its frame,
5632try to split WINDOW vertically disregarding any value specified
5633by `split-height-threshold'. If that succeeds, return the lower
5634window. Return nil otherwise.
5635
5636By default `display-buffer' routines call this function to split
5637the largest or least recently used window. To change the default
5638customize the option `split-window-preferred-function'.
5639
5640You can enforce this function to not split WINDOW horizontally,
382c953b 5641by setting (or binding) the variable `split-width-threshold' to
8b10a2d1
MR
5642nil. If, in addition, you set `split-height-threshold' to zero,
5643chances increase that this function does split WINDOW vertically.
5644
382c953b 5645In order to not split WINDOW vertically, set (or bind) the
8b10a2d1
MR
5646variable `split-height-threshold' to nil. Additionally, you can
5647set `split-width-threshold' to zero to make a horizontal split
5648more likely to occur.
5649
5650Have a look at the function `window-splittable-p' if you want to
5651know how `split-window-sensibly' determines whether WINDOW can be
5652split."
2dc2a609
TH
5653 (let ((window (or window (selected-window))))
5654 (or (and (window-splittable-p window)
5655 ;; Split window vertically.
5656 (with-selected-window window
5657 (split-window-below)))
5658 (and (window-splittable-p window t)
5659 ;; Split window horizontally.
5660 (with-selected-window window
5661 (split-window-right)))
5662 (and (eq window (frame-root-window (window-frame window)))
5663 (not (window-minibuffer-p window))
5664 ;; If WINDOW is the only window on its frame and is not the
5665 ;; minibuffer window, try to split it vertically disregarding
5666 ;; the value of `split-height-threshold'.
5667 (let ((split-height-threshold 0))
5668 (when (window-splittable-p window)
5669 (with-selected-window window
5670 (split-window-below))))))))
9481c002 5671
5938d519 5672(defun window--try-to-split-window (window &optional alist)
f818cd2a
MR
5673 "Try to split WINDOW.
5674Return value returned by `split-window-preferred-function' if it
5675represents a live window, nil otherwise."
5676 (and (window-live-p window)
5677 (not (frame-parameter (window-frame window) 'unsplittable))
8e17c9ba
MR
5678 (let* ((window-combination-limit
5679 ;; When `window-combination-limit' equals
5938d519
MR
5680 ;; `display-buffer' or equals `resize-window' and a
5681 ;; `window-height' or `window-width' alist entry are
5682 ;; present, bind it to t so resizing steals space
5683 ;; preferably from the window that was split.
5684 (if (or (eq window-combination-limit 'display-buffer)
5685 (and (eq window-combination-limit 'window-size)
5686 (or (cdr (assq 'window-height alist))
5687 (cdr (assq 'window-width alist)))))
8e17c9ba
MR
5688 t
5689 window-combination-limit))
5690 (new-window
5691 ;; Since `split-window-preferred-function' might
5692 ;; throw an error use `condition-case'.
5693 (condition-case nil
5694 (funcall split-window-preferred-function window)
5695 (error nil))))
f818cd2a
MR
5696 (and (window-live-p new-window) new-window))))
5697
5698(defun window--frame-usable-p (frame)
5699 "Return FRAME if it can be used to display a buffer."
5700 (when (frame-live-p frame)
5701 (let ((window (frame-root-window frame)))
5702 ;; `frame-root-window' may be an internal window which is considered
5703 ;; "dead" by `window-live-p'. Hence if `window' is not live we
5704 ;; implicitly know that `frame' has a visible window we can use.
5705 (unless (and (window-live-p window)
5706 (or (window-minibuffer-p window)
5707 ;; If the window is soft-dedicated, the frame is usable.
5708 ;; Actually, even if the window is really dedicated,
5709 ;; the frame is still usable by splitting it.
5710 ;; At least Emacs-22 allowed it, and it is desirable
5711 ;; when displaying same-frame windows.
5712 nil ; (eq t (window-dedicated-p window))
5713 ))
5714 frame))))
5715
5716(defcustom even-window-heights t
5717 "If non-nil `display-buffer' will try to even window heights.
5718Otherwise `display-buffer' will leave the window configuration
5719alone. Heights are evened only when `display-buffer' chooses a
5720window that appears above or below the selected window."
5721 :type 'boolean
5722 :group 'windows)
5723
5724(defun window--even-window-heights (window)
5725 "Even heights of WINDOW and selected window.
5726Do this only if these windows are vertically adjacent to each
5727other, `even-window-heights' is non-nil, and the selected window
5728is higher than WINDOW."
5729 (when (and even-window-heights
9aba119d
MR
5730 ;; Even iff WINDOW forms a vertical combination with the
5731 ;; selected window, and WINDOW's height exceeds that of the
5732 ;; selected window, see also bug#11880.
5733 (window-combined-p window)
5734 (= (window-child-count (window-parent window)) 2)
5735 (eq (window-parent) (window-parent window))
5736 (> (window-total-height) (window-total-height window)))
5737 ;; Don't throw an error if we can't even window heights for
5738 ;; whatever reason.
5739 (condition-case nil
5740 (enlarge-window
5741 (/ (- (window-total-height window) (window-total-height)) 2))
5742 (error nil))))
f818cd2a 5743
5938d519 5744(defun window--display-buffer (buffer window type &optional alist dedicated)
0ff7851c 5745 "Display BUFFER in WINDOW.
51a5f9d8 5746TYPE must be one of the symbols `reuse', `window' or `frame' and
0ff7851c
MR
5747is passed unaltered to `display-buffer-record-window'. ALIST is
5748the alist argument of `display-buffer'. Set `window-dedicated-p'
5749to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
5750live."
f818cd2a 5751 (when (and (buffer-live-p buffer) (window-live-p window))
caceae25 5752 (display-buffer-record-window type window buffer)
90749b53
CY
5753 (unless (eq buffer (window-buffer window))
5754 (set-window-dedicated-p window nil)
90749b53
CY
5755 (set-window-buffer window buffer)
5756 (when dedicated
5757 (set-window-dedicated-p window dedicated))
5758 (when (memq type '(window frame))
5759 (set-window-prev-buffers window nil)))
2d6b6005 5760 (let ((parameter (window-parameter window 'quit-restore))
5938d519 5761 (height (cdr (assq 'window-height alist)))
880e6158
MR
5762 (width (cdr (assq 'window-width alist)))
5763 (size (cdr (assq 'window-size alist))))
5764 (cond
5765 ((or (eq type 'frame)
5766 (and (eq (car parameter) 'same)
5767 (eq (nth 1 parameter) 'frame)))
5768 ;; Adjust size of frame if asked for.
5769 (cond
5770 ((not size))
5771 ((consp size)
5772 (let ((width (car size))
5773 (height (cdr size))
2d6b6005 5774 (frame (window-frame window)))
880e6158
MR
5775 (when (and (numberp width) (numberp height))
5776 (set-frame-height
5777 frame (+ (frame-height frame)
5778 (- height (window-total-height window))))
5779 (set-frame-width
5780 frame (+ (frame-width frame)
5781 (- width (window-total-width window)))))))
5782 ((functionp size)
5783 (ignore-errors (funcall size window)))))
5784 ((or (eq type 'window)
5785 (and (eq (car parameter) 'same)
5786 (eq (nth 1 parameter) 'window)))
0ff7851c 5787 ;; Adjust height of window if asked for.
5938d519
MR
5788 (cond
5789 ((not height))
5790 ((numberp height)
5791 (let* ((new-height
5792 (if (integerp height)
5793 height
5794 (round
880e6158 5795 (* (window-total-height (frame-root-window window))
5938d519 5796 height))))
880e6158
MR
5797 (delta (- new-height (window-total-height window))))
5798 (when (and (window--resizable-p window delta nil 'safe)
0ff7851c
MR
5799 (window-combined-p window))
5800 (window-resize window delta nil 'safe))))
5938d519
MR
5801 ((functionp height)
5802 (ignore-errors (funcall height window))))
0ff7851c 5803 ;; Adjust width of window if asked for.
5938d519
MR
5804 (cond
5805 ((not width))
5806 ((numberp width)
5807 (let* ((new-width
5808 (if (integerp width)
5809 width
5810 (round
880e6158 5811 (* (window-total-width (frame-root-window window))
5938d519 5812 width))))
880e6158
MR
5813 (delta (- new-width (window-total-width window))))
5814 (when (and (window--resizable-p window delta t 'safe)
0ff7851c
MR
5815 (window-combined-p window t))
5816 (window-resize window delta t 'safe))))
5938d519 5817 ((functionp width)
880e6158 5818 (ignore-errors (funcall width window)))))))
0ff7851c 5819
90749b53
CY
5820 window))
5821
5822(defun window--maybe-raise-frame (frame)
5823 (let ((visible (frame-visible-p frame)))
5824 (unless (or (not visible)
5825 ;; Assume the selected frame is already visible enough.
5826 (eq frame (selected-frame))
5827 ;; Assume the frame from which we invoked the
5828 ;; minibuffer is visible.
5829 (and (minibuffer-window-active-p (selected-window))
5830 (eq frame (window-frame (minibuffer-selected-window)))))
5831 (raise-frame frame))))
f818cd2a 5832
24510c22
SM
5833;; FIXME: Not implemented.
5834;; FIXME: By the way, there could be more levels of dedication:
5835;; - `barely' dedicated doesn't prevent reuse of the window, only records that
5836;; the window hasn't been used for something else yet.
5837;; - `softly' dedicated only allows reuse when asked explicitly.
5838;; - `strongly' never allows reuse.
f818cd2a
MR
5839(defvar display-buffer-mark-dedicated nil
5840 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
5841The actual non-nil value of this variable will be copied to the
5842`window-dedicated-p' flag.")
5843
fa5660f9
CY
5844(defconst display-buffer--action-function-custom-type
5845 '(choice :tag "Function"
5846 (const :tag "--" ignore) ; default for insertion
fa5660f9 5847 (const display-buffer-reuse-window)
d9558cad 5848 (const display-buffer-pop-up-window)
fa5660f9
CY
5849 (const display-buffer-same-window)
5850 (const display-buffer-pop-up-frame)
880e6158 5851 (const display-buffer-in-previous-window)
fa5660f9
CY
5852 (const display-buffer-use-some-window)
5853 (function :tag "Other function"))
5854 "Custom type for `display-buffer' action functions.")
5855
5856(defconst display-buffer--action-custom-type
5857 `(cons :tag "Action"
5858 (choice :tag "Action functions"
5859 ,display-buffer--action-function-custom-type
5860 (repeat
5861 :tag "List of functions"
5862 ,display-buffer--action-function-custom-type))
5863 (alist :tag "Action arguments"
5864 :key-type symbol
5865 :value-type (sexp :tag "Value")))
5866 "Custom type for `display-buffer' actions.")
5867
cbb0f9ab
CY
5868(defvar display-buffer-overriding-action '(nil . nil)
5869 "Overriding action to perform to display a buffer.
5870It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
382c953b 5871function or a list of functions. Each function should accept two
cbb0f9ab
CY
5872arguments: a buffer to display and an alist similar to ALIST.
5873See `display-buffer' for details.")
5874(put 'display-buffer-overriding-action 'risky-local-variable t)
5875
fa5660f9 5876(defcustom display-buffer-alist nil
89894cd8
CY
5877 "Alist of conditional actions for `display-buffer'.
5878This is a list of elements (CONDITION . ACTION), where:
5879
0ff7851c
MR
5880 CONDITION is either a regexp matching buffer names, or a
5881 function that takes two arguments - a buffer name and the
5882 ACTION argument of `display-buffer' - and returns a boolean.
89894cd8 5883
8319e0bf
CY
5884 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
5885 function or a list of functions. Each such function should
382c953b 5886 accept two arguments: a buffer to display and an alist of the
f0cfa5fe 5887 same form as ALIST. See `display-buffer' for details.
0ff7851c
MR
5888
5889`display-buffer' scans this alist until it either finds a
5890matching regular expression or the function specified by a
9139632a
JL
5891condition returns non-nil. In any of these cases, it adds the
5892associated action to the list of actions it will try."
fa5660f9
CY
5893 :type `(alist :key-type
5894 (choice :tag "Condition"
5895 regexp
5896 (function :tag "Matcher function"))
5897 :value-type ,display-buffer--action-custom-type)
5898 :risky t
5899 :version "24.1"
5900 :group 'windows)
89894cd8 5901
cbb0f9ab
CY
5902(defcustom display-buffer-base-action '(nil . nil)
5903 "User-specified default action for `display-buffer'.
8319e0bf 5904It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
382c953b 5905function or a list of functions. Each function should accept two
fa5660f9
CY
5906arguments: a buffer to display and an alist similar to ALIST.
5907See `display-buffer' for details."
5908 :type display-buffer--action-custom-type
5909 :risky t
5910 :version "24.1"
5911 :group 'windows)
89894cd8 5912
cbb0f9ab 5913(defconst display-buffer-fallback-action
0a9f9ab5 5914 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
cbb0f9ab 5915 display-buffer-reuse-window
cbb0f9ab 5916 display-buffer--maybe-pop-up-frame-or-window
880e6158 5917 display-buffer-in-previous-window
cbb0f9ab
CY
5918 display-buffer-use-some-window
5919 ;; If all else fails, pop up a new frame.
5920 display-buffer-pop-up-frame))
5921 "Default fallback action for `display-buffer'.
5922This is the action used by `display-buffer' if no other actions
5923specified, e.g. by the user options `display-buffer-alist' or
5924`display-buffer-base-action'. See `display-buffer'.")
5925(put 'display-buffer-fallback-action 'risky-local-variable t)
f818cd2a 5926
0ff7851c
MR
5927(defun display-buffer-assq-regexp (buffer-name alist action)
5928 "Retrieve ALIST entry corresponding to BUFFER-NAME.
5929ACTION is the action argument passed to `display-buffer'."
f818cd2a
MR
5930 (catch 'match
5931 (dolist (entry alist)
cb882333 5932 (let ((key (car entry)))
f818cd2a
MR
5933 (when (or (and (stringp key)
5934 (string-match-p key buffer-name))
0ff7851c
MR
5935 (and (functionp key)
5936 (funcall key buffer-name action)))
f818cd2a
MR
5937 (throw 'match (cdr entry)))))))
5938
8319e0bf
CY
5939(defvar display-buffer--same-window-action
5940 '(display-buffer-same-window
5941 (inhibit-same-window . nil))
5942 "A `display-buffer' action for displaying in the same window.")
5943(put 'display-buffer--same-window-action 'risky-local-variable t)
5944
5945(defvar display-buffer--other-frame-action
5946 '((display-buffer-reuse-window
8319e0bf
CY
5947 display-buffer-pop-up-frame)
5948 (reusable-frames . 0)
5949 (inhibit-same-window . t))
5950 "A `display-buffer' action for displaying in another frame.")
5951(put 'display-buffer--other-frame-action 'risky-local-variable t)
5952
d45ba96b 5953(defun display-buffer (buffer-or-name &optional action frame)
3199b96f 5954 "Display BUFFER-OR-NAME in some window, without selecting it.
89894cd8
CY
5955BUFFER-OR-NAME must be a buffer or the name of an existing
5956buffer. Return the window chosen for displaying BUFFER-OR-NAME,
5957or nil if no such window is found.
5958
7b9abf24
EZ
5959Optional argument ACTION, if non-nil, should specify a display
5960action. Its form is described below.
5961
5962Optional argument FRAME, if non-nil, acts like an additional
5963ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
5964specifying the frame(s) to search for a window that is already
5965displaying the buffer. See `display-buffer-reuse-window'
5966
5967If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
5968where FUNCTION is either a function or a list of functions, and
d4bd55e7
CY
5969ALIST is an arbitrary association list (alist).
5970
5971Each such FUNCTION should accept two arguments: the buffer to
f130cb76
LL
5972display and an alist. Based on those arguments, it should
5973display the buffer and return the window. If the caller is
9139632a
JL
5974prepared to handle the case of not displaying the buffer
5975and returning nil from `display-buffer' it should pass
5976\(allow-no-window . t) as an element of the ALIST.
89894cd8 5977
cbb0f9ab 5978The `display-buffer' function builds a function list and an alist
d4bd55e7
CY
5979by combining the functions and alists specified in
5980`display-buffer-overriding-action', `display-buffer-alist', the
5981ACTION argument, `display-buffer-base-action', and
5982`display-buffer-fallback-action' (in order). Then it calls each
5983function in the combined function list in turn, passing the
cbb0f9ab
CY
5984buffer as the first argument and the combined alist as the second
5985argument, until one of the functions returns non-nil.
8319e0bf 5986
7b9abf24
EZ
5987If ACTION is nil, the function list and the alist are built using
5988only the other variables mentioned above.
5989
8319e0bf
CY
5990Available action functions include:
5991 `display-buffer-same-window'
8319e0bf
CY
5992 `display-buffer-reuse-window'
5993 `display-buffer-pop-up-frame'
5994 `display-buffer-pop-up-window'
880e6158 5995 `display-buffer-in-previous-window'
8319e0bf
CY
5996 `display-buffer-use-some-window'
5997
5998Recognized alist entries include:
5999
6000 `inhibit-same-window' -- A non-nil value prevents the same
6001 window from being used for display.
6002
90749b53
CY
6003 `inhibit-switch-frame' -- A non-nil value prevents any other
6004 frame from being raised or selected,
6005 even if the window is displayed there.
6006
8319e0bf
CY
6007 `reusable-frames' -- Value specifies frame(s) to search for a
6008 window that already displays the buffer.
6009 See `display-buffer-reuse-window'.
2a7bdc1a 6010
d97af5a0
CY
6011 `pop-up-frame-parameters' -- Value specifies an alist of frame
6012 parameters to give a new frame, if
6013 one is created.
6014
df171c23
MR
6015 `window-height' -- Value specifies either an integer (the number
6016 of lines of a new window), a floating point number (the
6017 fraction of a new window with respect to the height of the
6018 frame's root window) or a function to be called with one
6019 argument - a new window. The function is supposed to adjust
6020 the height of the window; its return value is ignored.
6021 Suitable functions are `shrink-window-if-larger-than-buffer'
6022 and `fit-window-to-buffer'.
6023
6024 `window-width' -- Value specifies either an integer (the number
6025 of columns of a new window), a floating point number (the
6026 fraction of a new window with respect to the width of the
6027 frame's root window) or a function to be called with one
6028 argument - a new window. The function is supposed to adjust
6029 the width of the window; its return value is ignored.
6030
9139632a
JL
6031 `allow-no-window' -- A non-nil value indicates readiness for the case
6032 of not displaying the buffer and FUNCTION can safely return
6033 a non-window value to suppress displaying.
6034
2a7bdc1a
CY
6035The ACTION argument to `display-buffer' can also have a non-nil
6036and non-list value. This means to display the buffer in a window
6037other than the selected one, even if it is already displayed in
6038the selected window. If called interactively with a prefix
7b9abf24 6039argument, ACTION is t."
c3313451
CY
6040 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6041 (if current-prefix-arg t)))
d45ba96b
MR
6042 (let ((buffer (if (bufferp buffer-or-name)
6043 buffer-or-name
6044 (get-buffer buffer-or-name)))
f47ad11b
MR
6045 ;; Make sure that when we split windows the old window keeps
6046 ;; point, bug#14829.
6047 (split-window-keep-point t)
89894cd8
CY
6048 ;; Handle the old form of the first argument.
6049 (inhibit-same-window (and action (not (listp action)))))
6050 (unless (listp action) (setq action nil))
6051 (if display-buffer-function
6052 ;; If `display-buffer-function' is defined, let it do the job.
6053 (funcall display-buffer-function buffer inhibit-same-window)
6054 ;; Otherwise, use the defined actions.
6055 (let* ((user-action
0ff7851c
MR
6056 (display-buffer-assq-regexp
6057 (buffer-name buffer) display-buffer-alist action))
0a9f9ab5 6058 (special-action (display-buffer--special-action buffer))
89894cd8
CY
6059 ;; Extra actions from the arguments to this function:
6060 (extra-action
6061 (cons nil (append (if inhibit-same-window
6062 '((inhibit-same-window . t)))
6063 (if frame
8319e0bf 6064 `((reusable-frames . ,frame))))))
89894cd8
CY
6065 ;; Construct action function list and action alist.
6066 (actions (list display-buffer-overriding-action
0a9f9ab5 6067 user-action special-action action extra-action
cbb0f9ab
CY
6068 display-buffer-base-action
6069 display-buffer-fallback-action))
89894cd8
CY
6070 (functions (apply 'append
6071 (mapcar (lambda (x)
6072 (setq x (car x))
c3313451 6073 (if (functionp x) (list x) x))
89894cd8
CY
6074 actions)))
6075 (alist (apply 'append (mapcar 'cdr actions)))
6076 window)
6077 (unless (buffer-live-p buffer)
6078 (error "Invalid buffer"))
6079 (while (and functions (not window))
6080 (setq window (funcall (car functions) buffer alist)
6081 functions (cdr functions)))
f130cb76 6082 (and (windowp window) window)))))
f818cd2a
MR
6083
6084(defun display-buffer-other-frame (buffer)
e6f759f9 6085 "Display buffer BUFFER preferably in another frame.
f818cd2a
MR
6086This uses the function `display-buffer' as a subroutine; see
6087its documentation for additional customization information."
6088 (interactive "BDisplay buffer in other frame: ")
8319e0bf 6089 (display-buffer buffer display-buffer--other-frame-action t))
f818cd2a 6090
89894cd8 6091;;; `display-buffer' action functions:
437014c8 6092
89894cd8 6093(defun display-buffer-same-window (buffer alist)
8319e0bf
CY
6094 "Display BUFFER in the selected window.
6095This fails if ALIST has a non-nil `inhibit-same-window' entry, or
6096if the selected window is a minibuffer window or is dedicated to
6097another buffer; in that case, return nil. Otherwise, return the
6098selected window."
89894cd8
CY
6099 (unless (or (cdr (assq 'inhibit-same-window alist))
6100 (window-minibuffer-p)
6101 (window-dedicated-p))
5938d519 6102 (window--display-buffer buffer (selected-window) 'reuse alist)))
89894cd8 6103
0d3ff375 6104(defun display-buffer--maybe-same-window (buffer alist)
8319e0bf
CY
6105 "Conditionally display BUFFER in the selected window.
6106If `same-window-p' returns non-nil for BUFFER's name, call
6107`display-buffer-same-window' and return its value. Otherwise,
6108return nil."
89894cd8
CY
6109 (and (same-window-p (buffer-name buffer))
6110 (display-buffer-same-window buffer alist)))
6111
6112(defun display-buffer-reuse-window (buffer alist)
6113 "Return a window that is already displaying BUFFER.
8319e0bf
CY
6114Return nil if no usable window is found.
6115
6116If ALIST has a non-nil `inhibit-same-window' entry, the selected
6117window is not eligible for reuse.
6118
6119If ALIST contains a `reusable-frames' entry, its value determines
6120which frames to search for a reusable window:
6121 nil -- the selected frame (actually the last non-minibuffer frame)
6122 A frame -- just that frame
6123 `visible' -- all visible frames
6124 0 -- all frames on the current terminal
6125 t -- all frames.
6126
6127If ALIST contains no `reusable-frames' entry, search just the
6128selected frame if `display-buffer-reuse-frames' and
6129`pop-up-frames' are both nil; search all frames on the current
90749b53
CY
6130terminal if either of those variables is non-nil.
6131
6132If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6133event that a window on another frame is chosen, avoid raising
6134that frame."
8319e0bf
CY
6135 (let* ((alist-entry (assq 'reusable-frames alist))
6136 (frames (cond (alist-entry (cdr alist-entry))
6137 ((if (eq pop-up-frames 'graphic-only)
6138 (display-graphic-p)
6139 pop-up-frames)
6140 0)
6141 (display-buffer-reuse-frames 0)
6142 (t (last-nonminibuffer-frame))))
6143 (window (if (and (eq buffer (window-buffer))
6144 (not (cdr (assq 'inhibit-same-window alist))))
6145 (selected-window)
6146 (car (delq (selected-window)
6147 (get-buffer-window-list buffer 'nomini
6148 frames))))))
90749b53 6149 (when (window-live-p window)
5938d519 6150 (prog1 (window--display-buffer buffer window 'reuse alist)
90749b53
CY
6151 (unless (cdr (assq 'inhibit-switch-frame alist))
6152 (window--maybe-raise-frame (window-frame window)))))))
89894cd8 6153
0a9f9ab5 6154(defun display-buffer--special-action (buffer)
4ad3bc2a
CY
6155 "Return special display action for BUFFER, if any.
6156If `special-display-p' returns non-nil for BUFFER, return an
6157appropriate display action involving `special-display-function'.
6158See `display-buffer' for the format of display actions."
8319e0bf
CY
6159 (and special-display-function
6160 ;; `special-display-p' returns either t or a list of frame
6161 ;; parameters to pass to `special-display-function'.
6162 (let ((pars (special-display-p (buffer-name buffer))))
6163 (when pars
0a9f9ab5
SM
6164 (list (list #'display-buffer-reuse-window
6165 `(lambda (buffer _alist)
6166 (funcall special-display-function
6167 buffer ',(if (listp pars) pars)))))))))
8319e0bf 6168
90749b53 6169(defun display-buffer-pop-up-frame (buffer alist)
89894cd8 6170 "Display BUFFER in a new frame.
8319e0bf 6171This works by calling `pop-up-frame-function'. If successful,
90749b53
CY
6172return the window used; otherwise return nil.
6173
6174If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
d97af5a0
CY
6175raising the new frame.
6176
6177If ALIST has a non-nil `pop-up-frame-parameters' entry, the
6178corresponding value is an alist of frame parameters to give the
6179new frame."
6180 (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
6181 (pop-up-frame-alist (append params pop-up-frame-alist))
6182 (fun pop-up-frame-function)
6183 frame window)
89894cd8 6184 (when (and fun
7fd5f65e
MR
6185 ;; Make BUFFER current so `make-frame' will use it as the
6186 ;; new frame's buffer (Bug#15133).
6187 (with-current-buffer buffer
6188 (setq frame (funcall fun)))
89894cd8 6189 (setq window (frame-selected-window frame)))
5938d519
MR
6190 (prog1 (window--display-buffer
6191 buffer window 'frame alist display-buffer-mark-dedicated)
90749b53
CY
6192 (unless (cdr (assq 'inhibit-switch-frame alist))
6193 (window--maybe-raise-frame frame))))))
89894cd8 6194
90749b53 6195(defun display-buffer-pop-up-window (buffer alist)
89894cd8
CY
6196 "Display BUFFER by popping up a new window.
6197The new window is created on the selected frame, or in
6198`last-nonminibuffer-frame' if no windows can be created there.
90749b53
CY
6199If successful, return the new window; otherwise return nil.
6200
6201If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6202event that the new window is created on another frame, avoid
6203raising the frame."
89894cd8
CY
6204 (let ((frame (or (window--frame-usable-p (selected-frame))
6205 (window--frame-usable-p (last-nonminibuffer-frame))))
6206 window)
6207 (when (and (or (not (frame-parameter frame 'unsplittable))
6208 ;; If the selected frame cannot be split, look at
6209 ;; `last-nonminibuffer-frame'.
6210 (and (eq frame (selected-frame))
6211 (setq frame (last-nonminibuffer-frame))
6212 (window--frame-usable-p frame)
6213 (not (frame-parameter frame 'unsplittable))))
6214 ;; Attempt to split largest or least recently used window.
6215 (setq window (or (window--try-to-split-window
5938d519 6216 (get-largest-window frame t) alist)
89894cd8 6217 (window--try-to-split-window
5938d519
MR
6218 (get-lru-window frame t) alist))))
6219 (prog1 (window--display-buffer
6220 buffer window 'window alist display-buffer-mark-dedicated)
90749b53
CY
6221 (unless (cdr (assq 'inhibit-switch-frame alist))
6222 (window--maybe-raise-frame (window-frame window)))))))
89894cd8 6223
8319e0bf
CY
6224(defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
6225 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
6226
6227If `pop-up-frames' is non-nil (and not `graphic-only' on a
6228text-only terminal), try with `display-buffer-pop-up-frame'.
6229
6230If that cannot be done, and `pop-up-windows' is non-nil, try
6231again with `display-buffer-pop-up-window'."
6232 (or (and (if (eq pop-up-frames 'graphic-only)
6233 (display-graphic-p)
6234 pop-up-frames)
6235 (display-buffer-pop-up-frame buffer alist))
6236 (and pop-up-windows
6237 (display-buffer-pop-up-window buffer alist))))
89894cd8 6238
5938d519 6239(defun display-buffer-below-selected (buffer alist)
78dd6ab1
MR
6240 "Try displaying BUFFER in a window below the selected window.
6241This either splits the selected window or reuses the window below
6242the selected one."
6243 (let (window)
6244 (or (and (not (frame-parameter nil 'unsplittable))
d144ef06
MR
6245 (let ((split-height-threshold 0)
6246 split-width-threshold)
f93cc74f 6247 (setq window (window--try-to-split-window (selected-window) alist)))
78dd6ab1 6248 (window--display-buffer
5938d519 6249 buffer window 'window alist display-buffer-mark-dedicated))
78dd6ab1
MR
6250 (and (setq window (window-in-direction 'below))
6251 (not (window-dedicated-p window))
6252 (window--display-buffer
5938d519 6253 buffer window 'reuse alist display-buffer-mark-dedicated)))))
8e17c9ba 6254
5938d519 6255(defun display-buffer-at-bottom (buffer alist)
735135f9 6256 "Try displaying BUFFER in a window at the bottom of the selected frame.
8e17c9ba
MR
6257This either splits the window at the bottom of the frame or the
6258frame's root window, or reuses an existing window at the bottom
6259of the selected frame."
6260 (let (bottom-window window)
c660a885
MR
6261 (walk-window-tree
6262 (lambda (window) (setq bottom-window window)) nil nil 'nomini)
8e17c9ba 6263 (or (and (not (frame-parameter nil 'unsplittable))
00139435
MR
6264 (let (split-width-threshold)
6265 (setq window (window--try-to-split-window bottom-window alist)))
8e17c9ba 6266 (window--display-buffer
5938d519 6267 buffer window 'window alist display-buffer-mark-dedicated))
8e17c9ba
MR
6268 (and (not (frame-parameter nil 'unsplittable))
6269 (setq window
6270 (condition-case nil
c660a885 6271 (split-window (window--major-non-side-window))
8e17c9ba
MR
6272 (error nil)))
6273 (window--display-buffer
5938d519 6274 buffer window 'window alist display-buffer-mark-dedicated))
8e17c9ba
MR
6275 (and (setq window bottom-window)
6276 (not (window-dedicated-p window))
6277 (window--display-buffer
5938d519 6278 buffer window 'reuse alist display-buffer-mark-dedicated)))))
78dd6ab1 6279
fa2bcf43
MR
6280(defun display-buffer-in-previous-window (buffer alist)
6281 "Display BUFFER in a window previously showing it.
6282If ALIST has a non-nil `inhibit-same-window' entry, the selected
6283window is not eligible for reuse.
6284
6285If ALIST contains a `reusable-frames' entry, its value determines
6286which frames to search for a reusable window:
6287 nil -- the selected frame (actually the last non-minibuffer frame)
6288 A frame -- just that frame
6289 `visible' -- all visible frames
6290 0 -- all frames on the current terminal
6291 t -- all frames.
6292
6293If ALIST contains no `reusable-frames' entry, search just the
6294selected frame if `display-buffer-reuse-frames' and
6295`pop-up-frames' are both nil; search all frames on the current
6296terminal if either of those variables is non-nil.
6297
6298If ALIST has a `previous-window' entry, the window specified by
6299that entry will override any other window found by the methods
6300above, even if that window never showed BUFFER before."
6301 (let* ((alist-entry (assq 'reusable-frames alist))
6302 (inhibit-same-window
6303 (cdr (assq 'inhibit-same-window alist)))
6304 (frames (cond
6305 (alist-entry (cdr alist-entry))
6306 ((if (eq pop-up-frames 'graphic-only)
6307 (display-graphic-p)
6308 pop-up-frames)
6309 0)
6310 (display-buffer-reuse-frames 0)
6311 (t (last-nonminibuffer-frame))))
9d3aa82c 6312 best-window second-best-window window)
fa2bcf43
MR
6313 ;; Scan windows whether they have shown the buffer recently.
6314 (catch 'best
6315 (dolist (window (window-list-1 (frame-first-window) 'nomini frames))
6316 (when (and (assq buffer (window-prev-buffers window))
6317 (not (window-dedicated-p window)))
6318 (if (eq window (selected-window))
6319 (unless inhibit-same-window
6320 (setq second-best-window window))
6321 (setq best-window window)
6322 (throw 'best t)))))
6323 ;; When ALIST has a `previous-window' entry, that entry may override
6324 ;; anything we found so far.
6325 (when (and (setq window (cdr (assq 'previous-window alist)))
6326 (window-live-p window)
6327 (not (window-dedicated-p window)))
6328 (if (eq window (selected-window))
6329 (unless inhibit-same-window
6330 (setq second-best-window window))
6331 (setq best-window window)))
6332 ;; Return best or second best window found.
6333 (when (setq window (or best-window second-best-window))
5938d519 6334 (window--display-buffer buffer window 'reuse alist))))
fa2bcf43 6335
89894cd8
CY
6336(defun display-buffer-use-some-window (buffer alist)
6337 "Display BUFFER in an existing window.
6338Search for a usable window, set that window to the buffer, and
90749b53
CY
6339return the window. If no suitable window is found, return nil.
6340
6341If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6342event that a window in another frame is chosen, avoid raising
6343that frame."
89894cd8 6344 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
89894cd8
CY
6345 (frame (or (window--frame-usable-p (selected-frame))
6346 (window--frame-usable-p (last-nonminibuffer-frame))))
51a5f9d8
MR
6347 (window
6348 ;; Reuse an existing window.
6349 (or (get-lru-window frame nil not-this-window)
6350 (let ((window (get-buffer-window buffer 'visible)))
6351 (unless (and not-this-window
6352 (eq window (selected-window)))
6353 window))
6354 (get-largest-window 'visible nil not-this-window)
6355 (let ((window (get-buffer-window buffer 0)))
6356 (unless (and not-this-window
6357 (eq window (selected-window)))
6358 window))
880e6158
MR
6359 (get-largest-window 0 nil not-this-window)))
6360 (quit-restore (and (window-live-p window)
6361 (window-parameter window 'quit-restore)))
6362 (quad (nth 1 quit-restore)))
90749b53 6363 (when (window-live-p window)
880e6158
MR
6364 ;; If the window was used by `display-buffer' before, try to
6365 ;; resize it to its old height but don't signal an error.
6366 (when (and (listp quad)
6367 (integerp (nth 3 quad))
6368 (/= (nth 3 quad) (window-total-height window)))
6369 (condition-case nil
6370 (window-resize window (- (nth 3 quad) (window-total-height window)))
6371 (error nil)))
6372
9aba119d 6373 (prog1
5938d519 6374 (window--display-buffer buffer window 'reuse alist)
9aba119d 6375 (window--even-window-heights window)
90749b53
CY
6376 (unless (cdr (assq 'inhibit-switch-frame alist))
6377 (window--maybe-raise-frame (window-frame window)))))))
437014c8 6378
2d6b6005 6379(defun display-buffer-no-window (_buffer alist)
9139632a
JL
6380 "Display BUFFER in no window.
6381If ALIST has a non-nil `allow-no-window' entry, then don't display
6382a window at all. This makes possible to override the default action
6383and avoid displaying the buffer. It is assumed that when the caller
6384specifies a non-nil `allow-no-window' then it can handle a nil value
6385returned from `display-buffer' in this case."
6386 (when (cdr (assq 'allow-no-window alist))
6387 'fail))
6388
437014c8 6389;;; Display + selection commands:
c3313451
CY
6390(defun pop-to-buffer (buffer &optional action norecord)
6391 "Select buffer BUFFER in some window, preferably a different one.
6392BUFFER may be a buffer, a string (a buffer name), or nil. If it
6393is a string not naming an existent buffer, create a buffer with
6394that name. If BUFFER is nil, choose some other buffer. Return
6395the buffer.
6396
6397This uses `display-buffer' as a subroutine. The optional ACTION
6398argument is passed to `display-buffer' as its ACTION argument.
6399See `display-buffer' for more information. ACTION is t if called
6400interactively with a prefix argument, which means to pop to a
6401window other than the selected one even if the buffer is already
6402displayed in the selected window.
6403
6404If the window to show BUFFER is not on the selected
f818cd2a
MR
6405frame, raise that window's frame and give it input focus.
6406
f818cd2a
MR
6407Optional third arg NORECORD non-nil means do not put this buffer
6408at the front of the list of recently selected ones."
c3313451
CY
6409 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
6410 (if current-prefix-arg t)))
8319e0bf 6411 (setq buffer (window-normalize-buffer-to-switch-to buffer))
c660a885
MR
6412 ;; This should be done by `select-window' below.
6413 ;; (set-buffer buffer)
cb882333 6414 (let* ((old-frame (selected-frame))
8319e0bf 6415 (window (display-buffer buffer action))
c3313451 6416 (frame (window-frame window)))
97adfb97
CY
6417 ;; If we chose another frame, make sure it gets input focus.
6418 (unless (eq frame old-frame)
c3313451 6419 (select-frame-set-input-focus frame norecord))
97adfb97
CY
6420 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
6421 (select-window window norecord)
c3313451 6422 buffer))
f818cd2a 6423
72258fe5
CY
6424(defun pop-to-buffer-same-window (buffer &optional norecord)
6425 "Select buffer BUFFER in some window, preferably the same one.
72258fe5
CY
6426BUFFER may be a buffer, a string (a buffer name), or nil. If it
6427is a string not naming an existent buffer, create a buffer with
6428that name. If BUFFER is nil, choose some other buffer. Return
6429the buffer.
6430
6c8413fc
MR
6431Optional argument NORECORD, if non-nil means do not put this
6432buffer at the front of the list of recently selected ones.
6433
6434Unlike `pop-to-buffer', this function prefers using the selected
6435window over popping up a new window or frame."
24510c22 6436 (pop-to-buffer buffer display-buffer--same-window-action norecord))
72258fe5 6437
f818cd2a
MR
6438(defun read-buffer-to-switch (prompt)
6439 "Read the name of a buffer to switch to, prompting with PROMPT.
e1dbe924 6440Return the name of the buffer as a string.
f818cd2a
MR
6441
6442This function is intended for the `switch-to-buffer' family of
6443commands since these need to omit the name of the current buffer
6444from the list of completions and default values."
6445 (let ((rbts-completion-table (internal-complete-buffer-except)))
6446 (minibuffer-with-setup-hook
6447 (lambda ()
6448 (setq minibuffer-completion-table rbts-completion-table)
6449 ;; Since rbts-completion-table is built dynamically, we
6450 ;; can't just add it to the default value of
6451 ;; icomplete-with-completion-tables, so we add it
6452 ;; here manually.
6453 (if (and (boundp 'icomplete-with-completion-tables)
6454 (listp icomplete-with-completion-tables))
6455 (set (make-local-variable 'icomplete-with-completion-tables)
6456 (cons rbts-completion-table
6457 icomplete-with-completion-tables))))
6458 (read-buffer prompt (other-buffer (current-buffer))
6459 (confirm-nonexistent-file-or-buffer)))))
6460
6461(defun window-normalize-buffer-to-switch-to (buffer-or-name)
6462 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
6463If BUFFER-OR-NAME is nil, return the buffer returned by
6464`other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
6465exists, return that buffer. If no such buffer exists, create a
6466buffer with the name BUFFER-OR-NAME and return that buffer."
6467 (if buffer-or-name
6468 (or (get-buffer buffer-or-name)
6469 (let ((buffer (get-buffer-create buffer-or-name)))
6470 (set-buffer-major-mode buffer)
6471 buffer))
6472 (other-buffer)))
6473
9d7f027b
MR
6474(defcustom switch-to-buffer-preserve-window-point nil
6475 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
6476If this is nil, `switch-to-buffer' displays the buffer at that
6477buffer's `point'. If this is `already-displayed', it tries to
791ef5f8 6478display the buffer at its previous position in the selected
9d7f027b
MR
6479window, provided the buffer is currently displayed in some other
6480window on any visible or iconified frame. If this is t, it
6481unconditionally tries to display the buffer at its previous
6482position in the selected window.
6483
43bcfda6
MR
6484This variable is ignored if the buffer is already displayed in
6485the selected window or never appeared in it before, or if
9d7f027b
MR
6486`switch-to-buffer' calls `pop-to-buffer' to display the buffer."
6487 :type '(choice
6488 (const :tag "Never" nil)
6489 (const :tag "If already displayed elsewhere" already-displayed)
6490 (const :tag "Always" t))
6491 :group 'windows
6492 :version "24.3")
6493
f818cd2a 6494(defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
d1c0cddf
SM
6495 "Display buffer BUFFER-OR-NAME in the selected window.
6496
6497WARNING: This is NOT the way to work on another buffer temporarily
6498within a Lisp program! Use `set-buffer' instead. That avoids
6499messing with the window-buffer correspondences.
6500
cee2e90d
CY
6501If the selected window cannot display the specified
6502buffer (e.g. if it is a minibuffer window or strongly dedicated
6503to another buffer), call `pop-to-buffer' to select the buffer in
6504another window.
6505
6506If called interactively, read the buffer name using the
f818cd2a
MR
6507minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6508determines whether to request confirmation before creating a new
6509buffer.
6510
cee2e90d
CY
6511BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
6512If BUFFER-OR-NAME is a string that does not identify an existing
6513buffer, create a buffer with that name. If BUFFER-OR-NAME is
6514nil, switch to the buffer returned by `other-buffer'.
6515
6516If optional argument NORECORD is non-nil, do not put the buffer
6517at the front of the buffer list, and do not make the window
6518displaying it the most recently selected one.
6519
6520If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
6521must be displayed in the selected window; if that is impossible,
6522signal an error rather than calling `pop-to-buffer'.
f818cd2a 6523
9d7f027b
MR
6524The option `switch-to-buffer-preserve-window-point' can be used
6525to make the buffer appear at its last position in the selected
6526window.
6527
f818cd2a
MR
6528Return the buffer switched to."
6529 (interactive
acc825c5 6530 (list (read-buffer-to-switch "Switch to buffer: ") nil 'force-same-window))
f818cd2a 6531 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
24510c22
SM
6532 (cond
6533 ;; Don't call set-window-buffer if it's not needed since it
6534 ;; might signal an error (e.g. if the window is dedicated).
6535 ((eq buffer (window-buffer)))
6536 ((window-minibuffer-p)
6537 (if force-same-window
71873e2b 6538 (user-error "Cannot switch buffers in minibuffer window")
24510c22
SM
6539 (pop-to-buffer buffer norecord)))
6540 ((eq (window-dedicated-p) t)
6541 (if force-same-window
71873e2b 6542 (user-error "Cannot switch buffers in a dedicated window")
24510c22 6543 (pop-to-buffer buffer norecord)))
9d7f027b
MR
6544 (t
6545 (let* ((entry (assq buffer (window-prev-buffers)))
6546 (displayed (and (eq switch-to-buffer-preserve-window-point
6547 'already-displayed)
6548 (get-buffer-window buffer 0))))
6549 (set-window-buffer nil buffer)
6550 (when (and entry
6551 (or (eq switch-to-buffer-preserve-window-point t)
6552 displayed))
6553 ;; Try to restore start and point of buffer in the selected
6554 ;; window (Bug#4041).
6555 (set-window-start (selected-window) (nth 1 entry) t)
6556 (set-window-point nil (nth 2 entry))))))
24510c22
SM
6557
6558 (unless norecord
6559 (select-window (selected-window)))
6560 (set-buffer buffer)))
f818cd2a
MR
6561
6562(defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
6563 "Select the buffer specified by BUFFER-OR-NAME in another window.
382c953b 6564BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
f818cd2a
MR
6565nil. Return the buffer switched to.
6566
6567If called interactively, prompt for the buffer name using the
6568minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6569determines whether to request confirmation before creating a new
6570buffer.
6571
6572If BUFFER-OR-NAME is a string and does not identify an existing
6573buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6574nil, switch to the buffer returned by `other-buffer'.
6575
6576Optional second argument NORECORD non-nil means do not put this
6577buffer at the front of the list of recently selected ones.
6578
6579This uses the function `display-buffer' as a subroutine; see its
6580documentation for additional customization information."
6581 (interactive
6582 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
8319e0bf
CY
6583 (let ((pop-up-windows t))
6584 (pop-to-buffer buffer-or-name t norecord)))
f818cd2a
MR
6585
6586(defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
6587 "Switch to buffer BUFFER-OR-NAME in another frame.
382c953b 6588BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
f818cd2a
MR
6589nil. Return the buffer switched to.
6590
6591If called interactively, prompt for the buffer name using the
6592minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6593determines whether to request confirmation before creating a new
6594buffer.
6595
6596If BUFFER-OR-NAME is a string and does not identify an existing
6597buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6598nil, switch to the buffer returned by `other-buffer'.
6599
6600Optional second arg NORECORD non-nil means do not put this
6601buffer at the front of the list of recently selected ones.
6602
6603This uses the function `display-buffer' as a subroutine; see its
6604documentation for additional customization information."
6605 (interactive
6606 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
8319e0bf 6607 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
3c448ab6
MR
6608\f
6609(defun set-window-text-height (window height)
9992ea0c 6610 "Set the height in lines of the text display area of WINDOW to HEIGHT.
85c2386b
MR
6611WINDOW must be a live window and defaults to the selected one.
6612HEIGHT doesn't include the mode line or header line, if any, or
6613any partial-height lines in the text display area.
3c448ab6
MR
6614
6615Note that the current implementation of this function cannot
6616always set the height exactly, but attempts to be conservative,
6617by allocating more lines than are actually needed in the case
6618where some error may be present."
447f16b8 6619 (setq window (window-normalize-window window t))
3c448ab6
MR
6620 (let ((delta (- height (window-text-height window))))
6621 (unless (zerop delta)
6622 ;; Setting window-min-height to a value like 1 can lead to very
6623 ;; bizarre displays because it also allows Emacs to make *other*
37269466
CY
6624 ;; windows one line tall, which means that there's no more space
6625 ;; for the mode line.
6626 (let ((window-min-height (min 2 height)))
d615d6d2 6627 (window-resize window delta)))))
3c448ab6 6628
6198ccd0
MR
6629(defun enlarge-window-horizontally (delta)
6630 "Make selected window DELTA columns wider.
3c448ab6
MR
6631Interactively, if no argument is given, make selected window one
6632column wider."
6633 (interactive "p")
6198ccd0 6634 (enlarge-window delta t))
3c448ab6 6635
6198ccd0
MR
6636(defun shrink-window-horizontally (delta)
6637 "Make selected window DELTA columns narrower.
3c448ab6
MR
6638Interactively, if no argument is given, make selected window one
6639column narrower."
6640 (interactive "p")
6198ccd0 6641 (shrink-window delta t))
3c448ab6
MR
6642
6643(defun count-screen-lines (&optional beg end count-final-newline window)
6644 "Return the number of screen lines in the region.
6645The number of screen lines may be different from the number of actual lines,
6646due to line breaking, display table, etc.
6647
6648Optional arguments BEG and END default to `point-min' and `point-max'
6649respectively.
6650
6651If region ends with a newline, ignore it unless optional third argument
6652COUNT-FINAL-NEWLINE is non-nil.
6653
6654The optional fourth argument WINDOW specifies the window used for obtaining
6655parameters such as width, horizontal scrolling, and so on. The default is
6656to use the selected window's parameters.
6657
6658Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6659regardless of which buffer is displayed in WINDOW. This makes possible to use
6660`count-screen-lines' in any buffer, whether or not it is currently displayed
6661in some window."
6662 (unless beg
6663 (setq beg (point-min)))
6664 (unless end
6665 (setq end (point-max)))
6666 (if (= beg end)
6667 0
6668 (save-excursion
6669 (save-restriction
6670 (widen)
6671 (narrow-to-region (min beg end)
6672 (if (and (not count-final-newline)
6673 (= ?\n (char-before (max beg end))))
6674 (1- (max beg end))
6675 (max beg end)))
6676 (goto-char (point-min))
6677 (1+ (vertical-motion (buffer-size) window))))))
6678
6198ccd0 6679(defun window-buffer-height (window)
85c2386b
MR
6680 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
6681WINDOW must be a live window and defaults to the selected one."
6682 (setq window (window-normalize-window window t))
6198ccd0
MR
6683 (with-current-buffer (window-buffer window)
6684 (max 1
6685 (count-screen-lines (point-min) (point-max)
6686 ;; If buffer ends with a newline, ignore it when
6687 ;; counting height unless point is after it.
6688 (eobp)
6689 window))))
6690
880e6158
MR
6691;;; Resizing windows and frames to fit their contents exactly.
6692(defcustom fit-window-to-buffer-horizontally nil
6693 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
6694If this is nil, `fit-window-to-buffer' never resizes windows
6695horizontally. If this is `only', it can resize windows
6696horizontally only. Any other value means `fit-window-to-buffer'
6697can resize windows in both dimensions."
6698 :type 'boolean
6699 :version "24.4"
6700 :group 'help)
6701
6702;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
6703;; counting title bar and outer borders.
5938d519 6704(defcustom fit-frame-to-buffer nil
880e6158
MR
6705 "Non-nil means `fit-frame-to-buffer' can fit a frame to its buffer.
6706A frame is fit if and only if its root window is a live window
6707and this option is non-nil. If this is `horizontally', frames
6708are resized horizontally only. If this is `vertically', frames
6709are resized vertically only. Any other non-nil value means
6710frames can be resized in both dimensions. See also
6711`fit-frame-to-buffer-margins' and `fit-frame-to-buffer-sizes'.
6712
6713If this is non-nil and a window is the only window of its frame,
6714`fit-window-to-buffer' will invoke `fit-frame-to-buffer' to fit
6715the frame to its buffer."
5938d519 6716 :type 'boolean
880e6158 6717 :version "24.4"
5938d519
MR
6718 :group 'help)
6719
880e6158
MR
6720(defcustom fit-frame-to-buffer-margins '(nil nil nil nil)
6721 "Margins around frame for `fit-frame-to-buffer'.
6722This list specifies the numbers of pixels to be left free on the
6723left, above, the right, and below a frame that shall be fit to
6724its buffer. The value specified here can be overridden for a
6725specific frame by that frame's `fit-frame-to-buffer-margins'
6726parameter, if present.
6727
6728This variable controls how fitting a frame to the size of its
6729buffer coordinates with the size of your display. If you don't
6730specify a value here, the size of the display's workarea is used.
6731
6732See also `fit-frame-to-buffer-sizes'."
6733 :version "24.4"
6734 :type '(list
6735 (choice
6736 :tag "Left"
6737 :value nil
6738 :format "%[LeftMargin%] %v "
6739 (const :tag "None" :format "%t" nil)
6740 (integer :tag "Pixels" :size 5))
6741 (choice
6742 :tag "Top"
6743 :value nil
6744 :format "%[TopMargin%] %v "
6745 (const :tag "None" :format "%t" nil)
6746 (integer :tag "Pixels" :size 5))
6747 (choice
6748 :tag "Right"
6749 :value nil
6750 :format "%[RightMargin%] %v "
6751 (const :tag "None" :format "%t" nil)
6752 (integer :tag "Pixels" :size 5))
6753 (choice
6754 :tag "Bottom"
6755 :value nil
6756 :format "%[BottomMargin%] %v "
6757 (const :tag "None" :format "%t" nil)
6758 (integer :tag "Pixels" :size 5)))
6759 :group 'help)
6760
6761(defcustom fit-frame-to-buffer-sizes '(nil nil nil nil)
6762 "Size boundaries of frame for `fit-frame-to-buffer'.
6763This list specifies the total maximum and minimum lines and
6764maximum and minimum columns of the root window of any frame that
6765shall be fit to its buffer. If any of these values is non-nil,
6766it overrides the corresponding argument of `fit-frame-to-buffer'.
6767
6768On window systems where the menubar can wrap, fitting a frame to
6769its buffer may swallow the last line(s). Specifying an
6770appropriate minimum width value here can avoid such wrapping.
6771
6772See also `fit-frame-to-buffer-margins'."
6773 :version "24.4"
6774 :type '(list
6775 (choice
6776 :tag "Maximum Height"
6777 :value nil
6778 :format "%[MaxHeight%] %v "
6779 (const :tag "None" :format "%t" nil)
6780 (integer :tag "Lines" :size 5))
6781 (choice
6782 :tag "Minimum Height"
6783 :value nil
6784 :format "%[MinHeight%] %v "
6785 (const :tag "None" :format "%t" nil)
6786 (integer :tag "Lines" :size 5))
6787 (choice
6788 :tag "Maximum Width"
6789 :value nil
6790 :format "%[MaxWidth%] %v "
6791 (const :tag "None" :format "%t" nil)
6792 (integer :tag "Columns" :size 5))
6793 (choice
6794 :tag "Minimum Width"
6795 :value nil
6796 :format "%[MinWidth%] %v\n"
6797 (const :tag "None" :format "%t" nil)
6798 (integer :tag "Columns" :size 5)))
6799 :group 'help)
5938d519 6800
e740f9d2
GM
6801(declare-function x-display-pixel-height "xfns.c" (&optional terminal))
6802(declare-function tool-bar-lines-needed "xdisp.c" (&optional frame))
6803
880e6158
MR
6804(defun window--sanitize-margin (margin left right)
6805 "Return MARGIN if it's a number between LEFT and RIGHT."
6806 (when (and (numberp margin)
6807 (<= left (- right margin)) (<= margin right))
6808 margin))
5938d519 6809
880e6158
MR
6810(defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width)
6811 "Adjust size of FRAME to display the contents of its buffer exactly.
6812FRAME can be any live frame and defaults to the selected one.
6813Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
6814MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
6815FRAME's root window.
6816
6817The option `fit-frame-to-buffer' controls whether this function
6818has any effect. New position and size of FRAME are additionally
6819determined by the options `fit-frame-to-buffer-sizes' and
6820`fit-frame-to-buffer-margins' or the corresponding parameters of
6821FRAME."
5938d519 6822 (interactive)
e740f9d2
GM
6823 (or (fboundp 'x-display-pixel-height)
6824 (user-error "Cannot resize frame in non-graphic Emacs"))
5938d519 6825 (setq frame (window-normalize-frame frame))
880e6158
MR
6826 (when (and (window-live-p (frame-root-window frame))
6827 fit-frame-to-buffer
6828 (or (not window-size-fixed)
6829 (and (eq window-size-fixed 'height)
6830 (not (eq fit-frame-to-buffer 'vertically)))
6831 (and (eq window-size-fixed 'width)
6832 (not (eq fit-frame-to-buffer 'horizontally)))))
6833 (with-selected-window (frame-root-window frame)
6834 (let* ((window (frame-root-window frame))
6835 (char-width (frame-char-width))
6836 (char-height (frame-char-height))
6837 (monitor-attributes (car (display-monitor-attributes-list
6838 (frame-parameter frame 'display))))
6839 (geometry (cdr (assq 'geometry monitor-attributes)))
6840 (display-width (- (nth 2 geometry) (nth 0 geometry)))
6841 (display-height (- (nth 3 geometry) (nth 1 geometry)))
6842 (workarea (cdr (assq 'workarea monitor-attributes)))
6843 ;; Handle margins.
6844 (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins)
6845 fit-frame-to-buffer-margins))
6846 (left-margin (or (window--sanitize-margin
6847 (nth 0 margins) 0 display-width)
6848 (nth 0 workarea)))
6849 (top-margin (or (window--sanitize-margin
6850 (nth 1 margins) 0 display-height)
6851 (nth 1 workarea)))
6852 (workarea-width (nth 2 workarea))
6853 (right-margin (or (window--sanitize-margin
6854 (nth 2 margins) left-margin display-width)
6855 (+ left-margin workarea-width)))
6856 (workarea-height (nth 3 workarea))
6857 (bottom-margin (or (window--sanitize-margin
6858 (nth 3 margins) top-margin display-height)
6859 (+ top-margin workarea-height)))
6860 ;; The pixel width of FRAME (which does not include the
6861 ;; window manager's decorations).
6862 (frame-width (frame-pixel-width))
6863 ;; The pixel width of the body of FRAME's root window.
6864 (window-body-width (window-body-width nil t))
6865 ;; The difference in pixels between total and body width of
6866 ;; FRAME's window.
6867 (window-extra-width (- (window-pixel-width) window-body-width))
6868 ;; The difference in pixels between the frame's pixel width
6869 ;; and the window's body width. This is the space we can't
6870 ;; use for fitting.
6871 (extra-width (- frame-width window-body-width))
6872 ;; The maximum width we can use for fitting.
6873 (fit-width (- workarea-width extra-width))
6874 ;; The pixel position of FRAME's left border. We usually
6875 ;; try to leave this alone.
6876 (left
6877 (let ((left (frame-parameter nil 'left)))
6878 (if (consp left)
6879 (funcall (car left) (cadr left))
6880 left)))
6881 ;; The pixel height of FRAME (which does not include title
6882 ;; line, decorations, and sometimes neither the menu nor
6883 ;; the toolbar).
6884 (frame-height (frame-pixel-height))
6885 ;; The pixel height of FRAME's root window (we don't care
6886 ;; about the window's body height since the return value of
6887 ;; `window-text-pixel-size' includes header and mode line).
6888 (window-height (window-pixel-height))
6889 ;; The difference in pixels between the frame's pixel
6890 ;; height and the window's height.
6891 (extra-height (- frame-height window-height))
6892 ;; When tool-bar-mode is enabled and we just created a new
6893 ;; frame, reserve lines for toolbar resizing. Needed
6894 ;; because for reasons unknown to me Emacs (1) reserves one
6895 ;; line for the toolbar when making the initial frame and
6896 ;; toolbars are enabled, and (2) later adds the remaining
6897 ;; lines needed. Our code runs IN BETWEEN (1) and (2).
6898 ;; YMMV when you're on a system that behaves differently.
6899 (toolbar-extra-height
6900 (let ((quit-restore (window-parameter window 'quit-restore))
6901 ;; This may have to change when we allow arbitrary
6902 ;; pixel height toolbars.
6903 (lines (tool-bar-height)))
6904 (* char-height
6905 (if (and quit-restore (eq (car quit-restore) 'frame)
6906 (not (zerop lines)))
6907 (1- lines)
6908 0))))
6909 ;; The maximum height we can use for fitting.
6910 (fit-height
6911 (- workarea-height extra-height toolbar-extra-height))
6912 ;; The pixel position of FRAME's top border. We usually
6913 ;; try to leave this alone.
6914 (top
6915 (let ((top (frame-parameter nil 'top)))
6916 (if (consp top)
6917 (funcall (car top) (cadr top))
6918 top)))
6919 ;; Sanitize minimum and maximum sizes.
6920 (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes)
6921 fit-frame-to-buffer-sizes))
6922 (max-height
6923 (cond
6924 ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height))
6925 ((numberp max-height) (* max-height char-height))))
6926 (min-height
6927 (cond
6928 ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height))
6929 ((numberp min-height) (* min-height char-height))))
6930 (max-width
6931 (cond
6932 ((numberp (nth 2 sizes))
6933 (- (* (nth 2 sizes) char-width) window-extra-width))
6934 ((numberp max-width)
6935 (- (* max-width char-width) window-extra-width))))
6936 (min-width
6937 (cond
6938 ((numberp (nth 3 sizes))
6939 (- (* (nth 3 sizes) char-width) window-extra-width))
6940 ((numberp min-width)
6941 (- (* min-width char-width) window-extra-width))))
6942 ;; Note: Currently, for a new frame the sizes of the header
6943 ;; and mode line may be estimated incorrectly
6944 (value (window-text-pixel-size
6945 nil t t workarea-width workarea-height t))
6946 (width (+ (car value) (window-right-divider-width)))
6947 (height (+ (cdr value) (window-bottom-divider-width)))
6948 remainder)
6949 (unless window-resize-pixelwise
6950 ;; Round sizes to character sizes.
6951 (setq remainder (% width char-width))
6952 (unless (zerop remainder)
6953 (setq width (+ width (- char-width remainder))))
6954 (setq remainder (% height char-height))
6955 (setq height (+ height (- char-height remainder))))
6956 ;; Now make sure that we don't get larger than our rounded
6957 ;; maximum lines and columns.
6958 (when (> width fit-width)
6959 (setq width (- fit-width (% fit-width char-width))))
6960 (when (> height fit-height)
6961 (setq height (- fit-height (% fit-height char-height))))
6962 ;; Don't change height or width when the window's size is fixed
6963 ;; in either direction.
5938d519 6964 (cond
880e6158
MR
6965 ((eq window-size-fixed 'height)
6966 (setq height nil))
6967 ((eq window-size-fixed 'width)
6968 (setq height nil)))
6969 (when width
6970 ;; Fit to maximum and minimum widths.
6971 (when max-width
6972 (setq width (min width max-width)))
6973 (when min-width
6974 (setq width (max width min-width)))
6975 ;; Add extra width.
6976 (setq width (+ width extra-width))
6977 ;; Preserve right margin.
6978 (let ((right (+ left width extra-width))
6979 (max-right (- workarea-width right-margin)))
6980 (cond
6981 ((> right max-right)
6982 ;; Move FRAME to left.
6983 (setq left (max 0 (- left (- right max-right)))))
6984 ((< left left-margin)
6985 ;; Move frame to right.
6986 (setq left left-margin)))))
6987 (when height
6988 ;; Fit to maximum and minimum heights.
6989 (when max-height
6990 (setq height (min height max-height)))
6991 (when min-height
6992 (setq height (max height min-height)))
6993 ;; Add extra height.
6994 (setq height (+ height extra-height))
6995 ;; Preserve bottom and top margins.
6996 (let ((bottom (+ top height extra-height))
6997 (max-bottom (- workarea-height bottom-margin)))
6998 (cond
6999 ((> bottom max-bottom)
7000 ;; Move FRAME to left.
7001 (setq top (max 0 (- top (- bottom max-bottom)))))
7002 ((< top top-margin)
7003 ;; Move frame down.
7004 (setq top top-margin)))))
7005 ;; Apply changes.
7006 (set-frame-position frame left top)
7007 ;; Clumsily try to translate our calculations to what
7008 ;; `set-frame-size' wants.
7009 (when width
7010 (setq width (- (+ (frame-text-width) width)
7011 extra-width window-body-width)))
7012 (when height
7013 (setq height (- (+ (frame-text-height) height)
7014 extra-height window-height)))
7015 (set-frame-size
7016 frame
7017 (if width
7018 (if window-resize-pixelwise
7019 width
7020 (/ width char-width))
7021 (frame-text-width))
7022 (if height
7023 (if window-resize-pixelwise
7024 height
7025 (/ height char-height))
7026 (frame-text-height))
7027 window-resize-pixelwise)))))
7028
7029(defun fit-window-to-buffer (&optional window max-height min-height max-width min-width)
7030 "Adjust size of WINDOW to display its buffer's contents exactly.
85c2386b 7031WINDOW must be a live window and defaults to the selected one.
6198ccd0 7032
880e6158
MR
7033If WINDOW is part of a vertical combination, adjust WINDOW's
7034height. The new height is calculated from the number of lines of
7035the accessible portion of its buffer. The optional argument
7036MAX-HEIGHT specifies a maximum height and defaults to the height
7037of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
7038minimum height and defaults to `window-min-height'. Both
7039MAX-HEIGHT and MIN-HEIGHT are specified in lines and include the
7040mode line and header line, if any.
7041
7042If WINDOW is part of a horizontal combination and the value of
7043the option `fit-window-to-buffer-horizontally' is non-nil, adjust
7044WINDOW's height. The new width of WINDOW is calculated from the
7045maximum length of its buffer's lines that follow the current
7046start position of WINDOW. The optional argument MAX-WIDTH
7047specifies a maximum width and defaults to the width of WINDOW's
7048frame. The optional argument MIN-WIDTH specifies a minimum width
7049and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
7050are specified in columns and include fringes, margins and
7051scrollbars, if any.
7052
7053Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
7054If WINDOW is its frame's root window, then if the option
7055`fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
7056adjust the frame's size.
6198ccd0
MR
7057
7058Note that even if this function makes WINDOW large enough to show
880e6158
MR
7059_all_ parts of its buffer you might not see the first part when
7060WINDOW was scrolled. If WINDOW is resized horizontally, you will
7061not see the top of its buffer unless WINDOW starts at its minimum
7062accessible position."
f7baca20 7063 (interactive)
447f16b8 7064 (setq window (window-normalize-window window t))
880e6158
MR
7065 (if (eq window (frame-root-window window))
7066 (when fit-frame-to-buffer
7067 ;; Fit WINDOW's frame to buffer.
7068 (fit-frame-to-buffer
7069 (window-frame window)
7070 max-height min-height max-width min-width))
6198ccd0 7071 (with-selected-window window
880e6158 7072 (let* ((pixelwise window-resize-pixelwise)
880e6158
MR
7073 (char-height (frame-char-height))
7074 (char-width (frame-char-width))
880e6158
MR
7075 (total-height (window-size window nil pixelwise))
7076 (body-height (window-body-height window pixelwise))
7077 (body-width (window-body-width window pixelwise))
6198ccd0 7078 (min-height
880e6158 7079 ;; Sanitize MIN-HEIGHT.
c5e28e39
MR
7080 (if (numberp min-height)
7081 ;; Can't get smaller than `window-safe-min-height'.
880e6158
MR
7082 (max (if pixelwise
7083 (* char-height min-height)
7084 min-height)
7085 (if pixelwise
7086 (window-safe-min-pixel-height window)
7087 window-safe-min-height))
c5e28e39 7088 ;; Preserve header and mode line if present.
880e6158 7089 (window-min-size nil nil t pixelwise)))
6198ccd0 7090 (max-height
880e6158 7091 ;; Sanitize MAX-HEIGHT.
c5e28e39 7092 (if (numberp max-height)
880e6158
MR
7093 (min
7094 (+ total-height
7095 (window-max-delta
7096 window nil nil nil nil nil pixelwise))
7097 (if pixelwise
7098 (* char-height max-height)
7099 max-height))
7100 (+ total-height (window-max-delta
7101 window nil nil nil nil nil pixelwise))))
7102 height)
7103 (cond
7104 ;; If WINDOW is vertically combined, try to resize it
7105 ;; vertically.
7106 ((and (not (eq fit-window-to-buffer-horizontally 'only))
7107 (not (window-size-fixed-p window))
7108 (window-combined-p))
7109 ;; Vertically we always want to fit the entire buffer.
7110 ;; WINDOW'S height can't get larger than its frame's pixel
7111 ;; height. Its width remains fixed.
7112 (setq height (+ (cdr (window-text-pixel-size
7113 nil nil t nil (frame-pixel-height) t))
7114 (window-bottom-divider-width)))
7115 ;; Round height.
7116 (unless pixelwise
7117 (setq height (+ (/ height char-height)
7118 (if (zerop (% height char-height)) 0 1))))
7119 (unless (= height total-height)
7120 (window-resize-no-error
7121 window
7122 (- (max min-height (min max-height height)) total-height)
7123 nil window pixelwise)))
7124 ;; If WINDOW is horizontally combined, try to resize it
7125 ;; horizontally.
7126 ((and fit-window-to-buffer-horizontally
7127 (not (window-size-fixed-p window t))
7128 (window-combined-p nil t))
2d6b6005 7129 (let* ((total-width (window-size window nil pixelwise))
880e6158
MR
7130 (min-width
7131 ;; Sanitize MIN-WIDTH.
7132 (if (numberp min-width)
7133 ;; Can't get smaller than `window-safe-min-width'.
7134 (max (if pixelwise
7135 (* char-width min-width)
7136 min-width)
7137 (if pixelwise
7138 (window-safe-min-pixel-width)
7139 window-safe-min-width))
f224e500 7140 ;; Preserve fringes, margins, scrollbars if present.
880e6158
MR
7141 (window-min-size nil nil t pixelwise)))
7142 (max-width
7143 ;; Sanitize MAX-WIDTH.
7144 (if (numberp max-width)
7145 (min (+ total-width
7146 (window-max-delta
7147 nil t nil nil nil nil pixelwise))
7148 (if pixelwise
7149 (* char-width max-width)
7150 max-width))
7151 (+ total-width (window-max-delta
7152 nil t nil nil nil nil pixelwise))))
7153 ;; When fitting vertically, assume that WINDOW's start
7154 ;; position remains unaltered. WINDOW can't get wider
7155 ;; than its frame's pixel width, its height remains
7156 ;; unaltered.
7157 (width (+ (car (window-text-pixel-size
7158 nil (window-start) (point-max)
7159 (frame-pixel-width)
7160 ;; Add one char-height to assure that
7161 ;; we're on the safe side. This
7162 ;; overshoots when the first line below
7163 ;; the bottom is wider than the window.
7164 (* body-height
7165 (if pixelwise char-height 1))))
7166 (window-right-divider-width))))
7167 (unless pixelwise
7168 (setq width (+ (/ width char-width)
7169 (if (zerop (% width char-width)) 0 1))))
7170 (unless (= width body-width)
7171 (window-resize-no-error
7172 window
7173 (- (max min-width
7174 (min max-width
7175 (+ total-width (- width body-width))))
7176 total-width)
7177 t window pixelwise)))))))))
ef654460 7178
39cffb44
MR
7179(defun window-safely-shrinkable-p (&optional window)
7180 "Return t if WINDOW can be shrunk without shrinking other windows.
7181WINDOW defaults to the selected window."
7182 (with-selected-window (or window (selected-window))
7183 (let ((edges (window-edges)))
39cffb44
MR
7184 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
7185 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
39cffb44 7186
3c448ab6
MR
7187(defun shrink-window-if-larger-than-buffer (&optional window)
7188 "Shrink height of WINDOW if its buffer doesn't need so many lines.
7189More precisely, shrink WINDOW vertically to be as small as
7190possible, while still showing the full contents of its buffer.
85c2386b 7191WINDOW must be a live window and defaults to the selected one.
3c448ab6 7192
6198ccd0
MR
7193Do not shrink WINDOW to less than `window-min-height' lines. Do
7194nothing if the buffer contains more lines than the present window
7195height, or if some of the window's contents are scrolled out of
7196view, or if shrinking this window would also shrink another
7197window, or if the window is the only window of its frame.
3c448ab6
MR
7198
7199Return non-nil if the window was shrunk, nil otherwise."
7200 (interactive)
447f16b8 7201 (setq window (window-normalize-window window t))
6198ccd0
MR
7202 ;; Make sure that WINDOW is vertically combined and `point-min' is
7203 ;; visible (for whatever reason that's needed). The remaining issues
7204 ;; should be taken care of by `fit-window-to-buffer'.
3d8daefe 7205 (when (and (window-combined-p window)
6198ccd0 7206 (pos-visible-in-window-p (point-min) window))
880e6158 7207 (fit-window-to-buffer window (window-total-height window))))
6198ccd0 7208\f
3c448ab6
MR
7209(defun kill-buffer-and-window ()
7210 "Kill the current buffer and delete the selected window."
7211 (interactive)
7212 (let ((window-to-delete (selected-window))
7213 (buffer-to-kill (current-buffer))
6198ccd0 7214 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
3c448ab6
MR
7215 (unwind-protect
7216 (progn
7217 (add-hook 'kill-buffer-hook delete-window-hook t t)
7218 (if (kill-buffer (current-buffer))
7219 ;; If `delete-window' failed before, we rerun it to regenerate
7220 ;; the error so it can be seen in the echo area.
7221 (when (eq (selected-window) window-to-delete)
7222 (delete-window))))
7223 ;; If the buffer is not dead for some reason (probably because
7224 ;; of a `quit' signal), remove the hook again.
6198ccd0
MR
7225 (ignore-errors
7226 (with-current-buffer buffer-to-kill
7227 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
3c448ab6 7228
74f806a1 7229\f
3c448ab6
MR
7230(defvar recenter-last-op nil
7231 "Indicates the last recenter operation performed.
0116abbd
JL
7232Possible values: `top', `middle', `bottom', integer or float numbers.")
7233
7234(defcustom recenter-positions '(middle top bottom)
7235 "Cycling order for `recenter-top-bottom'.
7236A list of elements with possible values `top', `middle', `bottom',
7237integer or float numbers that define the cycling order for
7238the command `recenter-top-bottom'.
7239
382c953b 7240Top and bottom destinations are `scroll-margin' lines from the true
0116abbd
JL
7241window top and bottom. Middle redraws the frame and centers point
7242vertically within the window. Integer number moves current line to
7243the specified absolute window-line. Float number between 0.0 and 1.0
7244means the percentage of the screen space from the top. The default
7245cycling order is middle -> top -> bottom."
7246 :type '(repeat (choice
7247 (const :tag "Top" top)
7248 (const :tag "Middle" middle)
7249 (const :tag "Bottom" bottom)
7250 (integer :tag "Line number")
7251 (float :tag "Percentage")))
7252 :version "23.2"
7253 :group 'windows)
3c448ab6
MR
7254
7255(defun recenter-top-bottom (&optional arg)
0116abbd
JL
7256 "Move current buffer line to the specified window line.
7257With no prefix argument, successive calls place point according
7258to the cycling order defined by `recenter-positions'.
3c448ab6
MR
7259
7260A prefix argument is handled like `recenter':
7261 With numeric prefix ARG, move current line to window-line ARG.
0116abbd 7262 With plain `C-u', move current line to window center."
3c448ab6
MR
7263 (interactive "P")
7264 (cond
0116abbd 7265 (arg (recenter arg)) ; Always respect ARG.
3c448ab6 7266 (t
0116abbd
JL
7267 (setq recenter-last-op
7268 (if (eq this-command last-command)
7269 (car (or (cdr (member recenter-last-op recenter-positions))
7270 recenter-positions))
7271 (car recenter-positions)))
3c448ab6
MR
7272 (let ((this-scroll-margin
7273 (min (max 0 scroll-margin)
7274 (truncate (/ (window-body-height) 4.0)))))
7275 (cond ((eq recenter-last-op 'middle)
0116abbd 7276 (recenter))
3c448ab6 7277 ((eq recenter-last-op 'top)
0116abbd
JL
7278 (recenter this-scroll-margin))
7279 ((eq recenter-last-op 'bottom)
7280 (recenter (- -1 this-scroll-margin)))
7281 ((integerp recenter-last-op)
7282 (recenter recenter-last-op))
7283 ((floatp recenter-last-op)
7284 (recenter (round (* recenter-last-op (window-height))))))))))
3c448ab6
MR
7285
7286(define-key global-map [?\C-l] 'recenter-top-bottom)
216349f8 7287
216349f8
SM
7288(defun move-to-window-line-top-bottom (&optional arg)
7289 "Position point relative to window.
7290
0f202d5d 7291With a prefix argument ARG, acts like `move-to-window-line'.
216349f8
SM
7292
7293With no argument, positions point at center of window.
0116abbd
JL
7294Successive calls position point at positions defined
7295by `recenter-positions'."
216349f8
SM
7296 (interactive "P")
7297 (cond
0116abbd 7298 (arg (move-to-window-line arg)) ; Always respect ARG.
216349f8 7299 (t
0116abbd
JL
7300 (setq recenter-last-op
7301 (if (eq this-command last-command)
7302 (car (or (cdr (member recenter-last-op recenter-positions))
7303 recenter-positions))
7304 (car recenter-positions)))
216349f8
SM
7305 (let ((this-scroll-margin
7306 (min (max 0 scroll-margin)
7307 (truncate (/ (window-body-height) 4.0)))))
0f202d5d 7308 (cond ((eq recenter-last-op 'middle)
0116abbd 7309 (call-interactively 'move-to-window-line))
0f202d5d 7310 ((eq recenter-last-op 'top)
0116abbd
JL
7311 (move-to-window-line this-scroll-margin))
7312 ((eq recenter-last-op 'bottom)
7313 (move-to-window-line (- -1 this-scroll-margin)))
7314 ((integerp recenter-last-op)
7315 (move-to-window-line recenter-last-op))
7316 ((floatp recenter-last-op)
7317 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
216349f8
SM
7318
7319(define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
3c448ab6 7320\f
74f806a1
JL
7321;;; Scrolling commands.
7322
0a9f9ab5
SM
7323;;; Scrolling commands which do not signal errors at top/bottom
7324;;; of buffer at first key-press (instead move to top/bottom
74f806a1
JL
7325;;; of buffer).
7326
7327(defcustom scroll-error-top-bottom nil
8350f087 7328 "Move point to top/bottom of buffer before signaling a scrolling error.
74f806a1
JL
7329A value of nil means just signal an error if no more scrolling possible.
7330A value of t means point moves to the beginning or the end of the buffer
7331\(depending on scrolling direction) when no more scrolling possible.
7332When point is already on that position, then signal an error."
7333 :type 'boolean
60efac0f 7334 :group 'windows
74f806a1
JL
7335 :version "24.1")
7336
7337(defun scroll-up-command (&optional arg)
7338 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
7339If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
7340scroll window further, move cursor to the bottom line.
7341When point is already on that position, then signal an error.
7342A near full screen is `next-screen-context-lines' less than a full screen.
7343Negative ARG means scroll downward.
7344If ARG is the atom `-', scroll downward by nearly full screen."
7345 (interactive "^P")
7346 (cond
7347 ((null scroll-error-top-bottom)
7348 (scroll-up arg))
7349 ((eq arg '-)
7350 (scroll-down-command nil))
7351 ((< (prefix-numeric-value arg) 0)
7352 (scroll-down-command (- (prefix-numeric-value arg))))
7353 ((eobp)
7354 (scroll-up arg)) ; signal error
7355 (t
7356 (condition-case nil
7357 (scroll-up arg)
7358 (end-of-buffer
7359 (if arg
7360 ;; When scrolling by ARG lines can't be done,
7361 ;; move by ARG lines instead.
7362 (forward-line arg)
7363 ;; When ARG is nil for full-screen scrolling,
7364 ;; move to the bottom of the buffer.
7365 (goto-char (point-max))))))))
7366
7367(put 'scroll-up-command 'scroll-command t)
7368
7369(defun scroll-down-command (&optional arg)
7370 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
7371If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
7372scroll window further, move cursor to the top line.
7373When point is already on that position, then signal an error.
7374A near full screen is `next-screen-context-lines' less than a full screen.
7375Negative ARG means scroll upward.
7376If ARG is the atom `-', scroll upward by nearly full screen."
7377 (interactive "^P")
7378 (cond
7379 ((null scroll-error-top-bottom)
7380 (scroll-down arg))
7381 ((eq arg '-)
7382 (scroll-up-command nil))
7383 ((< (prefix-numeric-value arg) 0)
7384 (scroll-up-command (- (prefix-numeric-value arg))))
7385 ((bobp)
7386 (scroll-down arg)) ; signal error
7387 (t
7388 (condition-case nil
7389 (scroll-down arg)
7390 (beginning-of-buffer
7391 (if arg
7392 ;; When scrolling by ARG lines can't be done,
7393 ;; move by ARG lines instead.
7394 (forward-line (- arg))
7395 ;; When ARG is nil for full-screen scrolling,
7396 ;; move to the top of the buffer.
7397 (goto-char (point-min))))))))
7398
7399(put 'scroll-down-command 'scroll-command t)
7400
7401;;; Scrolling commands which scroll a line instead of full screen.
7402
7403(defun scroll-up-line (&optional arg)
7404 "Scroll text of selected window upward ARG lines; or one line if no ARG.
7405If ARG is omitted or nil, scroll upward by one line.
7406This is different from `scroll-up-command' that scrolls a full screen."
7407 (interactive "p")
7408 (scroll-up (or arg 1)))
7409
7410(put 'scroll-up-line 'scroll-command t)
7411
7412(defun scroll-down-line (&optional arg)
7413 "Scroll text of selected window down ARG lines; or one line if no ARG.
7414If ARG is omitted or nil, scroll down by one line.
7415This is different from `scroll-down-command' that scrolls a full screen."
7416 (interactive "p")
7417 (scroll-down (or arg 1)))
7418
7419(put 'scroll-down-line 'scroll-command t)
7420
7421\f
011474aa 7422(defun scroll-other-window-down (&optional lines)
74f806a1
JL
7423 "Scroll the \"other window\" down.
7424For more details, see the documentation for `scroll-other-window'."
7425 (interactive "P")
7426 (scroll-other-window
7427 ;; Just invert the argument's meaning.
7428 ;; We can do that without knowing which window it will be.
7429 (if (eq lines '-) nil
7430 (if (null lines) '-
7431 (- (prefix-numeric-value lines))))))
7432
7433(defun beginning-of-buffer-other-window (arg)
7434 "Move point to the beginning of the buffer in the other window.
7435Leave mark at previous position.
7436With arg N, put point N/10 of the way from the true beginning."
7437 (interactive "P")
7438 (let ((orig-window (selected-window))
7439 (window (other-window-for-scrolling)))
7440 ;; We use unwind-protect rather than save-window-excursion
7441 ;; because the latter would preserve the things we want to change.
7442 (unwind-protect
7443 (progn
7444 (select-window window)
7445 ;; Set point and mark in that window's buffer.
7446 (with-no-warnings
7447 (beginning-of-buffer arg))
7448 ;; Set point accordingly.
7449 (recenter '(t)))
7450 (select-window orig-window))))
7451
7452(defun end-of-buffer-other-window (arg)
7453 "Move point to the end of the buffer in the other window.
7454Leave mark at previous position.
7455With arg N, put point N/10 of the way from the true end."
7456 (interactive "P")
7457 ;; See beginning-of-buffer-other-window for comments.
7458 (let ((orig-window (selected-window))
7459 (window (other-window-for-scrolling)))
7460 (unwind-protect
7461 (progn
7462 (select-window window)
7463 (with-no-warnings
7464 (end-of-buffer arg))
7465 (recenter '(t)))
7466 (select-window orig-window))))
74f806a1 7467\f
3c448ab6
MR
7468(defvar mouse-autoselect-window-timer nil
7469 "Timer used by delayed window autoselection.")
7470
7471(defvar mouse-autoselect-window-position nil
7472 "Last mouse position recorded by delayed window autoselection.")
7473
7474(defvar mouse-autoselect-window-window nil
7475 "Last window recorded by delayed window autoselection.")
7476
7477(defvar mouse-autoselect-window-state nil
7478 "When non-nil, special state of delayed window autoselection.
382c953b
JB
7479Possible values are `suspend' (suspend autoselection after a menu or
7480scrollbar interaction) and `select' (the next invocation of
7481`handle-select-window' shall select the window immediately).")
3c448ab6
MR
7482
7483(defun mouse-autoselect-window-cancel (&optional force)
7484 "Cancel delayed window autoselection.
7485Optional argument FORCE means cancel unconditionally."
7486 (unless (and (not force)
7487 ;; Don't cancel for select-window or select-frame events
7488 ;; or when the user drags a scroll bar.
7489 (or (memq this-command
7490 '(handle-select-window handle-switch-frame))
7491 (and (eq this-command 'scroll-bar-toolkit-scroll)
7492 (memq (nth 4 (event-end last-input-event))
7493 '(handle end-scroll)))))
7494 (setq mouse-autoselect-window-state nil)
7495 (when (timerp mouse-autoselect-window-timer)
7496 (cancel-timer mouse-autoselect-window-timer))
7497 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
7498
7499(defun mouse-autoselect-window-start (mouse-position &optional window suspend)
7500 "Start delayed window autoselection.
7501MOUSE-POSITION is the last position where the mouse was seen as returned
7502by `mouse-position'. Optional argument WINDOW non-nil denotes the
7503window where the mouse was seen. Optional argument SUSPEND non-nil
7504means suspend autoselection."
7505 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
7506 (setq mouse-autoselect-window-position mouse-position)
7507 (when window (setq mouse-autoselect-window-window window))
7508 (setq mouse-autoselect-window-state (when suspend 'suspend))
7509 ;; Install timer which runs `mouse-autoselect-window-select' after
7510 ;; `mouse-autoselect-window' seconds.
7511 (setq mouse-autoselect-window-timer
7512 (run-at-time
7513 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
7514
7515(defun mouse-autoselect-window-select ()
7516 "Select window with delayed window autoselection.
7517If the mouse position has stabilized in a non-selected window, select
382c953b
JB
7518that window. The minibuffer window is selected only if the minibuffer
7519is active. This function is run by `mouse-autoselect-window-timer'."
6198ccd0
MR
7520 (ignore-errors
7521 (let* ((mouse-position (mouse-position))
7522 (window
7523 (ignore-errors
7524 (window-at (cadr mouse-position) (cddr mouse-position)
7525 (car mouse-position)))))
7526 (cond
e740f9d2 7527 ((or (and (fboundp 'menu-or-popup-active-p) (menu-or-popup-active-p))
6198ccd0 7528 (and window
3dfc5cd6
MR
7529 (let ((coords (coordinates-in-window-p
7530 (cdr mouse-position) window)))
7531 (and (not (consp coords))
7532 (not (memq coords '(left-margin right-margin)))))))
c660a885
MR
7533 ;; A menu / popup dialog is active or the mouse is not on the
7534 ;; text region of WINDOW: Suspend autoselection temporarily.
6198ccd0
MR
7535 (mouse-autoselect-window-start mouse-position nil t))
7536 ((eq mouse-autoselect-window-state 'suspend)
7537 ;; Delayed autoselection was temporarily suspended, reenable it.
7538 (mouse-autoselect-window-start mouse-position))
7539 ((and window (not (eq window (selected-window)))
7540 (or (not (numberp mouse-autoselect-window))
7541 (and (> mouse-autoselect-window 0)
7542 ;; If `mouse-autoselect-window' is positive, select
7543 ;; window if the window is the same as before.
7544 (eq window mouse-autoselect-window-window))
7545 ;; Otherwise select window if the mouse is at the same
7546 ;; position as before. Observe that the first test after
7547 ;; starting autoselection usually fails since the value of
7548 ;; `mouse-autoselect-window-position' recorded there is the
7549 ;; position where the mouse has entered the new window and
7550 ;; not necessarily where the mouse has stopped moving.
7551 (equal mouse-position mouse-autoselect-window-position))
7552 ;; The minibuffer is a candidate window if it's active.
7553 (or (not (window-minibuffer-p window))
7554 (eq window (active-minibuffer-window))))
7555 ;; Mouse position has stabilized in non-selected window: Cancel
7556 ;; delayed autoselection and try to select that window.
7557 (mouse-autoselect-window-cancel t)
7558 ;; Select window where mouse appears unless the selected window is the
7559 ;; minibuffer. Use `unread-command-events' in order to execute pre-
7560 ;; and post-command hooks and trigger idle timers. To avoid delaying
7561 ;; autoselection again, set `mouse-autoselect-window-state'."
290d5b58 7562 (unless (window-minibuffer-p)
6198ccd0
MR
7563 (setq mouse-autoselect-window-state 'select)
7564 (setq unread-command-events
7565 (cons (list 'select-window (list window))
7566 unread-command-events))))
7567 ((or (and window (eq window (selected-window)))
7568 (not (numberp mouse-autoselect-window))
7569 (equal mouse-position mouse-autoselect-window-position))
7570 ;; Mouse position has either stabilized in the selected window or at
7571 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
7572 (mouse-autoselect-window-cancel t))
7573 (t
7574 ;; Mouse position has not stabilized yet, resume delayed
7575 ;; autoselection.
7576 (mouse-autoselect-window-start mouse-position window))))))
3c448ab6
MR
7577
7578(defun handle-select-window (event)
7579 "Handle select-window events."
7580 (interactive "e")
7581 (let ((window (posn-window (event-start event))))
7582 (unless (or (not (window-live-p window))
7583 ;; Don't switch if we're currently in the minibuffer.
7584 ;; This tries to work around problems where the
7585 ;; minibuffer gets unselected unexpectedly, and where
7586 ;; you then have to move your mouse all the way down to
7587 ;; the minibuffer to select it.
290d5b58 7588 (window-minibuffer-p)
3c448ab6
MR
7589 ;; Don't switch to minibuffer window unless it's active.
7590 (and (window-minibuffer-p window)
7591 (not (minibuffer-window-active-p window)))
7592 ;; Don't switch when autoselection shall be delayed.
7593 (and (numberp mouse-autoselect-window)
7594 (not (zerop mouse-autoselect-window))
7595 (not (eq mouse-autoselect-window-state 'select))
7596 (progn
7597 ;; Cancel any delayed autoselection.
7598 (mouse-autoselect-window-cancel t)
7599 ;; Start delayed autoselection from current mouse
7600 ;; position and window.
7601 (mouse-autoselect-window-start (mouse-position) window)
7602 ;; Executing a command cancels delayed autoselection.
7603 (add-hook
7604 'pre-command-hook 'mouse-autoselect-window-cancel))))
7605 (when mouse-autoselect-window
7606 ;; Reset state of delayed autoselection.
7607 (setq mouse-autoselect-window-state nil)
7608 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
7609 (run-hooks 'mouse-leave-buffer-hook))
b1bac16e
MR
7610 ;; Clear echo area.
7611 (message nil)
3c448ab6
MR
7612 (select-window window))))
7613
3c448ab6
MR
7614(defun truncated-partial-width-window-p (&optional window)
7615 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
85c2386b 7616WINDOW must be a live window and defaults to the selected one.
3c448ab6
MR
7617Return nil if WINDOW is not a partial-width window
7618 (regardless of the value of `truncate-lines').
7619Otherwise, consult the value of `truncate-partial-width-windows'
7620 for the buffer shown in WINDOW."
85c2386b 7621 (setq window (window-normalize-window window t))
3c448ab6
MR
7622 (unless (window-full-width-p window)
7623 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
7624 (window-buffer window))))
7625 (if (integerp t-p-w-w)
7626 (< (window-width window) t-p-w-w)
7627 t-p-w-w))))
562dd5e9 7628\f
59ee0542
GM
7629;; Some of these are in tutorial--default-keys, so update that if you
7630;; change these.
562dd5e9
MR
7631(define-key ctl-x-map "0" 'delete-window)
7632(define-key ctl-x-map "1" 'delete-other-windows)
2d197ffb
CY
7633(define-key ctl-x-map "2" 'split-window-below)
7634(define-key ctl-x-map "3" 'split-window-right)
9397e56f 7635(define-key ctl-x-map "o" 'other-window)
562dd5e9 7636(define-key ctl-x-map "^" 'enlarge-window)
3c448ab6
MR
7637(define-key ctl-x-map "}" 'enlarge-window-horizontally)
7638(define-key ctl-x-map "{" 'shrink-window-horizontally)
7639(define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
7640(define-key ctl-x-map "+" 'balance-windows)
7641(define-key ctl-x-4-map "0" 'kill-buffer-and-window)
7642
3c448ab6 7643;;; window.el ends here