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