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