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