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