1 ;;; window.el --- GNU Emacs window commands aside from those written in C -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2017 Free Software
6 ;; Maintainer: emacs-devel@gnu.org
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 <https://www.gnu.org/licenses/>.
27 ;; Window tree functions.
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.
38 (mapcar (lambda (terminal)
39 (let ((frames (frames-on-display-list terminal
))
40 (top-frame (tty-top-frame terminal
))
45 (delq top-frame frames
))))
47 (push (cons f
(frame-selected-window f
))
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
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)))
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
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.
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
))
107 (run-hooks 'temp-buffer-window-setup-hook
))
108 ;; Return the 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'."
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
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))
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.
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)."
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
)
190 (vquit-function quit-function
))
191 `(let* ((,buffer
(temp-buffer-window-setup ,vbuffer-or-name
))
192 (standard-output ,buffer
)
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
)
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
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
)
213 (vquit-function quit-function
))
214 `(let* ((,buffer
(temp-buffer-window-setup ,vbuffer-or-name
))
215 (standard-output ,buffer
)
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
)
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."
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
)
235 (vquit-function quit-function
))
236 `(let* ((,buffer
(temp-buffer-window-setup ,vbuffer-or-name
))
237 (standard-output ,buffer
)
238 ;; If a 'window-height' entry specifies a function,
239 ;; remember it here in order to call it below but replace
240 ;; the entry so `window--try-to-split-window' will bind
241 ;; `window-combination-limit' to t and the function does
242 ;; not resize any other window but the one we split this
243 ;; one off (Bug#25055, Bug#25179).
245 (let ((window-height (assq 'window-height
(cdr ,vaction
))))
246 (when (functionp (cdr window-height
))
247 (cdr window-height
))))
249 (when vheight-function
250 (cons (car , vaction
)
254 'window-height
(cdr (copy-sequence ,vaction
)))))))
256 (with-current-buffer ,buffer
257 (setq ,window
(temp-buffer-window-show
258 ,buffer
(or vaction-copied
,vaction
))))
260 (let ((inhibit-read-only t
)
261 (inhibit-modification-hooks t
))
262 (setq ,value
(progn ,@body
)))
264 (set-window-point ,window
(point-min))
266 (when vheight-function
268 (set-window-parameter ,window
'preserve-size nil
)
269 (funcall vheight-function
,window
)))
271 (when (consp (cdr (assq 'preserve-size
(cdr ,vaction
))))
272 (window-preserve-size
273 ,window t
(cadr (assq 'preserve-size
(cdr ,vaction
))))
274 (window-preserve-size
275 ,window nil
(cddr (assq 'preserve-size
(cdr ,vaction
)))))
277 (if (functionp ,vquit-function
)
278 (funcall ,vquit-function
,window
,value
)
281 ;; The following two functions are like `window-next-sibling' and
282 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
283 ;; they don't substitute the selected window for nil), and they return
284 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
285 ;; a minibuffer window).
286 (defun window-right (window)
287 "Return WINDOW's right sibling.
288 Return nil if WINDOW is the root window of its frame. WINDOW can
290 (and window
(window-parent window
) (window-next-sibling window
)))
292 (defun window-left (window)
293 "Return WINDOW's left sibling.
294 Return nil if WINDOW is the root window of its frame. WINDOW can
296 (and window
(window-parent window
) (window-prev-sibling window
)))
298 (defun window-child (window)
299 "Return WINDOW's first child window.
300 WINDOW can be any window."
301 (or (window-top-child window
) (window-left-child window
)))
303 (defun window-child-count (window)
304 "Return number of WINDOW's child windows.
305 WINDOW can be any window."
307 (when (and (windowp window
) (setq window
(window-child window
)))
309 (setq count
(1+ count
))
310 (setq window
(window-next-sibling window
))))
313 (defun window-last-child (window)
314 "Return last child window of WINDOW.
315 WINDOW can be any window."
316 (when (and (windowp window
) (setq window
(window-child window
)))
317 (while (window-next-sibling window
)
318 (setq window
(window-next-sibling window
))))
321 (defun window-normalize-buffer (buffer-or-name)
322 "Return buffer specified by BUFFER-OR-NAME.
323 BUFFER-OR-NAME must be a live buffer, a string naming a live
324 buffer or nil which means to return the current buffer.
326 This function is commonly used to process the (usually optional)
327 \"BUFFER-OR-NAME\" argument of window related functions where nil
328 stands for the current buffer."
331 ((not buffer-or-name
)
333 ((bufferp buffer-or-name
)
335 ((stringp buffer-or-name
)
336 (get-buffer buffer-or-name
))
338 (error "No such buffer %s" buffer-or-name
)))))
339 (if (buffer-live-p buffer
)
341 (error "No such live buffer %s" buffer-or-name
))))
343 (defun window-normalize-frame (frame)
344 "Return frame specified by FRAME.
345 FRAME must be a live frame or nil which means to return the
348 This function is commonly used to process the (usually optional)
349 \"FRAME\" argument of window and frame related functions where
350 nil stands for the selected frame."
352 (if (frame-live-p frame
)
354 (error "%s is not a live frame" frame
))
357 (defun window-normalize-window (window &optional live-only
)
358 "Return window specified by WINDOW.
359 If WINDOW is nil, return the selected window. Otherwise, if
360 WINDOW is a live or an internal window, return WINDOW; if
361 LIVE-ONLY is non-nil, return WINDOW for a live window only.
362 Otherwise, signal an error.
364 This function is commonly used to process the (usually optional)
365 \"WINDOW\" argument of window related functions where nil stands
366 for the selected window."
371 (if (window-live-p window
)
373 (error "%s is not a live window" window
)))
374 ((window-valid-p window
)
377 (error "%s is not a valid window" window
))))
379 ;; Maybe this should go to frame.el.
380 (defun frame-char-size (&optional window-or-frame horizontal
)
381 "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
382 If WINDOW-OR-FRAME is a live frame, return the value of
383 `frame-char-height' for that frame. If WINDOW-OR-FRAME is a
384 valid window, return the value of `frame-char-height' for that
385 window's frame. In any other case, return the value of
386 `frame-char-height' for the selected frame.
388 Optional argument HORIZONTAL non-nil means to return the value of
389 `frame-char-width' for WINDOW-OR-FRAME."
392 ((window-valid-p window-or-frame
)
393 (window-frame window-or-frame
))
394 ((frame-live-p window-or-frame
)
396 (t (selected-frame)))))
398 (frame-char-width frame
)
399 (frame-char-height frame
))))
401 (defvar ignore-window-parameters nil
402 "If non-nil, standard functions ignore window parameters.
403 The functions currently affected by this are `split-window',
404 `delete-window', `delete-other-windows' and `other-window'.
406 An application may bind this to a non-nil value around calls to
407 these functions to inhibit processing of window parameters.")
409 ;; This must go to C, finally (or get removed).
410 (defconst window-safe-min-height
1
411 "The absolute minimum number of lines of any window.
412 Anything less might crash Emacs.")
414 (defun window-safe-min-pixel-height (&optional window
)
415 "Return the absolute minimum pixel height of WINDOW."
416 (* window-safe-min-height
417 (frame-char-size (window-normalize-window window
))))
419 (defcustom window-min-height
4
420 "The minimum total height, in lines, of any window.
421 The value has to accommodate one text line, a mode and header
422 line, a horizontal scroll bar and a bottom divider, if present.
423 A value less than `window-safe-min-height' is ignored. The value
424 of this variable is honored when windows are resized or split.
426 Applications should never rebind this variable. To resize a
427 window to a height less than the one specified here, an
428 application should instead call `window-resize' with a non-nil
429 IGNORE argument. In order to have `split-window' make a window
430 shorter, explicitly specify the SIZE argument of that function."
435 (defun window-min-pixel-height (&optional window
)
436 "Return the minimum pixel height of window WINDOW."
437 (* (max window-min-height window-safe-min-height
)
438 (frame-char-size window
)))
440 ;; This must go to C, finally (or get removed).
441 (defconst window-safe-min-width
2
442 "The absolute minimum number of columns of a window.
443 Anything less might crash Emacs.")
445 (defun window-safe-min-pixel-width (&optional window
)
446 "Return the absolute minimum pixel width of WINDOW."
447 (* window-safe-min-width
448 (frame-char-size (window-normalize-window window
) t
)))
450 (defcustom window-min-width
10
451 "The minimum total width, in columns, of any window.
452 The value has to accommodate two text columns as well as margins,
453 fringes, a scroll bar and a right divider, if present. A value
454 less than `window-safe-min-width' is ignored. The value of this
455 variable is honored when windows are resized or split.
457 Applications should never rebind this variable. To resize a
458 window to a width less than the one specified here, an
459 application should instead call `window-resize' with a non-nil
460 IGNORE argument. In order to have `split-window' make a window
461 narrower, explicitly specify the SIZE argument of that function."
466 (defun window-min-pixel-width (&optional window
)
467 "Return the minimum pixel width of window WINDOW."
468 (* (max window-min-width window-safe-min-width
)
469 (frame-char-size window t
)))
471 (defun window-safe-min-pixel-size (&optional window horizontal
)
472 "Return the absolute minimum pixel height of WINDOW.
473 Optional argument HORIZONTAL non-nil means return the absolute
474 minimum pixel width of WINDOW."
476 (window-safe-min-pixel-width window
)
477 (window-safe-min-pixel-height window
)))
479 (defun window-min-pixel-size (&optional window horizontal
)
480 "Return the minimum pixel height of WINDOW.
481 Optional argument HORIZONTAL non-nil means return the minimum
482 pixel width of WINDOW."
484 (window-min-pixel-width window
)
485 (window-min-pixel-height window
)))
487 (defun window-combined-p (&optional window horizontal
)
488 "Return non-nil if WINDOW has siblings in a given direction.
489 WINDOW must be a valid window and defaults to the selected one.
491 HORIZONTAL determines a direction for the window combination. If
492 HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
493 a vertical window combination. If HORIZONTAL is non-nil, return
494 non-nil if WINDOW is part of a horizontal window combination."
495 (setq window
(window-normalize-window window
))
496 (let ((parent (window-parent window
)))
499 (window-left-child parent
)
500 (window-top-child parent
)))))
502 (defun window-combination-p (&optional window horizontal
)
503 "Return WINDOW's first child if WINDOW is a vertical combination.
504 WINDOW can be any window and defaults to the selected one.
505 Optional argument HORIZONTAL non-nil means return WINDOW's first
506 child if WINDOW is a horizontal combination."
507 (setq window
(window-normalize-window window
))
509 (window-left-child window
)
510 (window-top-child window
)))
512 (defun window-combinations (window &optional horizontal
)
513 "Return largest number of windows vertically arranged within WINDOW.
514 WINDOW must be a valid window and defaults to the selected one.
515 If HORIZONTAL is non-nil, return the largest number of
516 windows horizontally arranged within WINDOW."
517 (setq window
(window-normalize-window window
))
519 ((window-live-p window
)
520 ;; If WINDOW is live, return 1.
523 (window-left-child window
)
524 (window-top-child window
))
525 ;; If WINDOW is iso-combined, return the sum of the values for all
526 ;; child windows of WINDOW.
527 (let ((child (window-child window
))
531 (+ (window-combinations child horizontal
)
533 (setq child
(window-right child
)))
536 ;; If WINDOW is not iso-combined, return the maximum value of any
537 ;; child window of WINDOW.
538 (let ((child (window-child window
))
542 (max (window-combinations child horizontal
)
544 (setq child
(window-right child
)))
547 (defun walk-window-tree-1 (fun walk-window-tree-window any
&optional sub-only
)
548 "Helper function for `walk-window-tree' and `walk-window-subtree'."
549 (let (walk-window-tree-buffer)
550 (while walk-window-tree-window
551 (setq walk-window-tree-buffer
552 (window-buffer walk-window-tree-window
))
553 (when (or walk-window-tree-buffer any
)
554 (funcall fun walk-window-tree-window
))
555 (unless walk-window-tree-buffer
557 fun
(window-left-child walk-window-tree-window
) any
)
559 fun
(window-top-child walk-window-tree-window
) any
))
561 (setq walk-window-tree-window nil
)
562 (setq walk-window-tree-window
563 (window-right walk-window-tree-window
))))))
565 (defun walk-window-tree (fun &optional frame any minibuf
)
566 "Run function FUN on each live window of FRAME.
567 FUN must be a function with one argument - a window. FRAME must
568 be a live frame and defaults to the selected one. ANY, if
569 non-nil, means to run FUN on all live and internal windows of
572 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
573 window even if it isn't active. MINIBUF nil or omitted means run
574 FUN on FRAME's minibuffer window only if it's active. In both
575 cases the minibuffer window must be part of FRAME. MINIBUF
576 neither nil nor t means never run FUN on the minibuffer window.
578 This function performs a pre-order, depth-first traversal of the
579 window tree. If FUN changes the window tree, the result is
581 (setq frame
(window-normalize-frame frame
))
582 (walk-window-tree-1 fun
(frame-root-window frame
) any
)
583 (when (memq minibuf
'(nil t
))
584 ;; Run FUN on FRAME's minibuffer window if requested.
585 (let ((minibuffer-window (minibuffer-window frame
)))
586 (when (and (window-live-p minibuffer-window
)
587 (eq (window-frame minibuffer-window
) frame
)
589 (minibuffer-window-active-p minibuffer-window
)))
590 (funcall fun minibuffer-window
)))))
592 (defun walk-window-subtree (fun &optional window any
)
593 "Run function FUN on the subtree of windows rooted at WINDOW.
594 WINDOW defaults to the selected window. FUN must be a function
595 with one argument - a window. By default, run FUN only on live
596 windows of the subtree. If the optional argument ANY is non-nil,
597 run FUN on all live and internal windows of the subtree. If
598 WINDOW is live, run FUN on WINDOW only.
600 This function performs a pre-order, depth-first traversal of the
601 subtree rooted at WINDOW. If FUN changes that tree, the result
603 (setq window
(window-normalize-window window
))
604 (walk-window-tree-1 fun window any t
))
606 (defun window-with-parameter (parameter &optional value frame any minibuf
)
607 "Return first window on FRAME with PARAMETER non-nil.
608 FRAME defaults to the selected frame. Optional argument VALUE
609 non-nil means only return a window whose window-parameter value
610 for PARAMETER equals VALUE (comparison is done with `equal').
611 Optional argument ANY non-nil means consider internal windows
614 Optional argument MINIBUF t means consider FRAME's minibuffer
615 window even if it isn't active. MINIBUF nil or omitted means
616 consider FRAME's minibuffer window only if it's active. In both
617 cases the minibuffer window must be part of FRAME. MINIBUF
618 neither nil nor t means never consider the minibuffer window."
623 (when (and (setq this-value
(window-parameter window parameter
))
624 (or (not value
) (equal value this-value
)))
625 (throw 'found window
)))
626 frame any minibuf
))))
629 (defun window-atom-root (&optional window
)
630 "Return root of atomic window WINDOW is a part of.
631 WINDOW must be a valid window and defaults to the selected one.
632 Return nil if WINDOW is not part of an atomic window."
633 (setq window
(window-normalize-window window
))
635 (while (and window
(window-parameter window
'window-atom
))
637 (setq window
(window-parent window
)))
640 (defun window-make-atom (window)
641 "Make WINDOW an atomic window.
642 WINDOW must be an internal window. Return WINDOW."
643 (if (not (window-child window
))
644 (error "Window %s is not an internal window" window
)
647 (unless (window-parameter window
'window-atom
)
648 (set-window-parameter window
'window-atom t
)))
652 (defun display-buffer-in-atom-window (buffer alist
)
653 "Display BUFFER in an atomic window.
654 This function displays BUFFER in a new window that will be
655 combined with an existing window to form an atomic window. If
656 the existing window is already part of an atomic window, add the
657 new window to that atomic window. Operations like `split-window'
658 or `delete-window', when applied to a constituent of an atomic
659 window, are applied atomically to the root of that atomic window.
661 ALIST is an association list of symbols and values. The
662 following symbols can be used.
664 `window' specifies the existing window the new window shall be
665 combined with. Use `window-atom-root' to make the new window a
666 sibling of an atomic window's root. If an internal window is
667 specified here, all children of that window become part of the
668 atomic window too. If no window is specified, the new window
669 becomes a sibling of the selected window. By default, the
670 `window-atom' parameter of the existing window is set to `main'
671 provided it is live and was not set before.
673 `side' denotes the side of the existing window where the new
674 window shall be located. Valid values are `below', `right',
675 `above' and `left'. The default is `below'. By default, the
676 `window-atom' parameter of the new window is set to this value.
678 The return value is the new window, nil when creating that window
680 (let* ((ignore-window-parameters t
)
681 (window-combination-limit t
)
682 (window-combination-resize 'atom
)
683 (window (cdr (assq 'window alist
)))
684 (side (or (cdr (assq 'side alist
)) 'below
))
685 (atom (when window
(window-parameter window
'window-atom
)))
687 (setq window
(window-normalize-window window
))
688 (setq root
(window-atom-root window
))
689 ;; Split off new window.
690 (when (setq new
(split-window-no-error window nil side
))
692 (if (and root
(not (eq root window
)))
693 ;; When WINDOW was part of an atomic window and we did not
694 ;; split its root, root atomic window at old root.
696 ;; Otherwise, root atomic window at WINDOW's new parent.
697 (window-parent window
)))
698 ;; Assign `window-atom' parameters, if needed.
699 (when (and (not atom
) (window-live-p window
))
700 (set-window-parameter window
'window-atom
'main
))
701 (set-window-parameter new
'window-atom side
)
702 ;; Display BUFFER in NEW and return NEW.
703 (window--display-buffer
704 buffer new
'window alist display-buffer-mark-dedicated
))))
706 (defun window--atom-check-1 (window)
707 "Subroutine of `window--atom-check'."
709 (if (window-parameter window
'window-atom
)
711 (when (or (catch 'reset
714 (if (window-parameter window
'window-atom
)
715 (setq count
(1+ count
))
718 ;; count >= 1 must hold here. If there's no other
719 ;; window around dissolve this atomic window.
721 ;; Dissolve atomic window.
724 (set-window-parameter window
'window-atom nil
))
727 (unless (window-buffer window
)
728 (window--atom-check-1 (window-left-child window
))
729 (window--atom-check-1 (window-top-child window
))))
730 ;; Check right sibling
731 (window--atom-check-1 (window-right window
))))
733 (defun window--atom-check (&optional frame
)
734 "Check atomicity of all windows on FRAME.
735 FRAME defaults to the selected frame. If an atomic window is
736 wrongly configured, reset the atomicity of all its windows on
737 FRAME to nil. An atomic window is wrongly configured if it has
738 no child windows or one of its child windows is not atomic."
739 (window--atom-check-1 (frame-root-window frame
)))
742 (defcustom window-sides-vertical nil
743 "If non-nil, left and right side windows occupy full frame height.
744 If nil, top and bottom side windows occupy full frame width."
746 :initialize
'custom-initialize-default
747 :set
'window--sides-verticalize
751 (defcustom window-sides-reversed nil
752 "Whether top/bottom side windows appear in reverse order.
753 When this is nil, side windows on the top and bottom of a frame
754 are always drawn from left to right with increasing slot values.
755 When this is t, side windows on the top and bottom of a frame are
756 always drawn from right to left with increasing slot values.
758 When this is `bidi', the drawing order is like that for the value
759 t if the value of `bidi-paragraph-direction' is `right-to-left'
760 in the buffer most recently shown in the window selected within
761 the main window area of this frame.
763 The layout of side windows on the left or right of a frame is not
764 affected by the value of this variable."
766 '(choice (const :tag
"Never" nil
)
767 (const :tag
"Bidi" bidi
)
768 (const :tag
"Always" t
))
769 :initialize
'custom-initialize-default
770 :set
'window--sides-reverse
774 (defcustom window-sides-slots
'(nil nil nil nil
)
775 "Number of available side window slots on each side of a frame.
776 The value is a list of four elements specifying the maximum
777 number of side windows that may be created on the left, top,
778 right and bottom side of any frame.
780 If an element is a number, `display-buffer-in-side-window' will
781 refrain from making a new side window if the number of windows on
782 that side is equal to or exceeds that number. Rather, it will
783 reuse the window whose `window-slot' value is nearest to the slot
784 specified via its ALIST argument. If an element is nil, this
785 means there's no bound on the number of windows on that side."
790 :value
(nil nil nil nil
)
793 :help-echo
"Maximum number of left side windows."
795 :format
"%[Left%] %v\n"
796 (const :tag
"Unlimited" :format
"%t" nil
)
797 (integer :tag
"Number" :value
2 :size
5))
800 :help-echo
"Maximum number of top side windows."
802 :format
"%[Top%] %v\n"
803 (const :tag
"Unlimited" :format
"%t" nil
)
804 (integer :tag
"Number" :value
3 :size
5))
807 :help-echo
"Maximum number of right side windows."
809 :format
"%[Right%] %v\n"
810 (const :tag
"Unlimited" :format
"%t" nil
)
811 (integer :tag
"Number" :value
2 :size
5))
814 :help-echo
"Maximum number of bottom side windows."
816 :format
"%[Bottom%] %v\n"
817 (const :tag
"Unlimited" :format
"%t" nil
)
818 (integer :tag
"Number" :value
3 :size
5)))
821 (defvar-local window--sides-shown nil
822 "Non-nil if this buffer was shown in a side window once.
823 If this variable is non-nil in a buffer, `switch-to-prev-buffer'
824 and `switch-to-next-buffer' will refrain from showing this buffer
825 within the main window area. `display-buffer-in-side-window'
826 sets this variable automatically.
828 Killing buffer local variables after showing the buffer in a side
829 window annihilates any effect provided by this variable.")
831 (defvar window--sides-inhibit-check nil
832 "Non-nil means inhibit any checks on side windows.")
834 (defun window--sides-reverse-on-frame-p (frame)
835 "Return non-nil when side windows should appear reversed on FRAME.
836 This uses some heuristics to guess the user's intentions when the
837 selected window of FRAME is a side window ."
839 ;; Reverse when `window-sides-reversed' is t. Do not reverse when
840 ;; `window-sides-reversed' is nil.
841 ((memq window-sides-reversed
'(nil t
))
842 window-sides-reversed
)
843 ;; Reverse when FRAME's selected window shows a right-to-left buffer.
844 ((let ((window (frame-selected-window frame
)))
845 (when (and (not (window-parameter window
'window-side
))
846 (or (not (window-minibuffer-p window
))
847 (setq window
(minibuffer-selected-window))))
848 (with-current-buffer (window-buffer window
)
849 (eq bidi-paragraph-direction
'right-to-left
)))))
850 ;; Reverse when FRAME's `window-sides-main-selected-window' parameter
851 ;; specifies a live window showing a right-to-left buffer.
852 ((let ((window (frame-parameter
853 frame
'window-sides-main-selected-window
)))
854 (when (window-live-p window
)
855 (with-current-buffer (window-buffer window
)
856 (eq bidi-paragraph-direction
'right-to-left
)))))
857 ;; Reverse when all windows in FRAME's main window show right-to-left
863 (with-current-buffer (window-buffer window
)
864 (when (eq bidi-paragraph-direction
'left-to-right
)
865 (throw 'found nil
))))
866 (window-main-window frame
))
869 (defun window-main-window (&optional frame
)
870 "Return the main window of specified FRAME.
871 The optional argument FRAME must be a live frame and defaults to
874 If FRAME has no side windows, return FRAME's root window.
875 Otherwise, return either an internal non-side window such that
876 all other non-side windows on FRAME descend from it, or the
877 single live non-side window of FRAME."
878 (let ((frame (window-normalize-frame frame
))
880 ;; Set main to the _last_ window found by `walk-window-tree' that
881 ;; is not a side window but has a side window as its sibling.
884 (and (not (window-parameter window
'window-side
))
885 (or (and (setq sibling
(window-prev-sibling window
))
886 (window-parameter sibling
'window-side
))
887 (and (setq sibling
(window-next-sibling window
))
888 (window-parameter sibling
'window-side
)))
891 (or main
(frame-root-window frame
))))
893 (defun window--make-major-side-window-next-to (side)
894 "Return window to split for making a major side window.
895 SIDE must be one of the symbols `left', `top', `right' or
898 This is an auxiliary function of `window--make-major-side-window'
899 and must not be called when a window on SIDE exists already."
900 (let ((root (frame-root-window))
901 (window--sides-inhibit-check t
)
903 ;; (1) If a window on the opposite side exists, return that window's
905 ;; (2) If the new window shall span the entire side, return the
906 ;; frame's root window.
907 ;; (3) If a window on an orthogonal side exists, return that
909 ;; (4) Otherwise return the frame's root window.
911 ((or (and (eq side
'left
)
912 (setq window
(window-with-parameter 'window-side
'right nil t
)))
914 (setq window
(window-with-parameter 'window-side
'bottom nil t
))))
915 (window-prev-sibling window
))
916 ((or (and (eq side
'right
)
917 (setq window
(window-with-parameter 'window-side
'left nil t
)))
918 (and (eq side
'bottom
)
919 (setq window
(window-with-parameter 'window-side
'top nil t
))))
920 (window-next-sibling window
))
921 ((memq side
'(left right
))
923 (window-sides-vertical
925 ((setq window
(window-with-parameter 'window-side
'top nil t
))
926 (window-next-sibling window
))
927 ((setq window
(window-with-parameter 'window-side
'bottom nil t
))
928 (window-prev-sibling window
))
930 ((memq side
'(top bottom
))
932 ((not window-sides-vertical
)
934 ((setq window
(window-with-parameter 'window-side
'left nil t
))
935 (window-next-sibling window
))
936 ((setq window
(window-with-parameter 'window-side
'right nil t
))
937 (window-prev-sibling window
))
940 (defun window--make-major-side-window (buffer side slot
&optional alist
)
941 "Display BUFFER in a new major side window on the selected frame.
942 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
943 specifies the slot to use. ALIST is an association list of
944 symbols and values as passed to `display-buffer-in-side-window'.
945 Return the new window, nil if its creation failed.
947 This is an auxiliary function of `display-buffer-in-side-window'
948 and may be called only if no window on SIDE exists yet."
949 (let* ((left-or-right (memq side
'(left right
)))
950 (next-to (window--make-major-side-window-next-to side
))
952 ((eq side
'top
) 'above
)
953 ((eq side
'bottom
) 'below
)
955 (window--sides-inhibit-check t
)
956 ;; The following two bindings will tell `split-window' to take
957 ;; the space for the new window from the selected frame's main
958 ;; window and not make a new parent window unless needed.
959 (window-combination-resize 'side
)
960 (window-combination-limit nil
)
961 (window (split-window-no-error next-to nil on-side
)))
963 ;; Initialize `window-side' parameter of new window to SIDE and
964 ;; make that parameter persistent.
965 (set-window-parameter window
'window-side side
)
966 (add-to-list 'window-persistent-parameters
'(window-side . writable
))
967 ;; Install `window-slot' parameter of new window and make that
968 ;; parameter persistent.
969 (set-window-parameter window
'window-slot slot
)
970 (add-to-list 'window-persistent-parameters
'(window-slot . writable
))
971 ;; Auto-adjust height/width of new window unless a size has been
972 ;; explicitly requested.
973 (unless (if left-or-right
974 (cdr (assq 'window-width alist
))
975 (cdr (assq 'window-height alist
)))
979 (if left-or-right
'window-width
'window-height
)
980 (/ (window-total-size (frame-root-window) left-or-right
)
981 ;; By default use a fourth of the size of the frame's
985 (with-current-buffer buffer
986 (setq window--sides-shown t
))
987 ;; Install BUFFER in new window and return WINDOW.
988 (window--display-buffer buffer window
'window alist
'side
))))
990 (defun display-buffer-in-side-window (buffer alist
)
991 "Display BUFFER in a side window of the selected frame.
992 ALIST is an association list of symbols and values. The
993 following special symbols can be used in ALIST.
995 `side' denotes the side of the frame where the new window shall
996 be located. Valid values are `bottom', `right', `top' and
997 `left'. The default is `bottom'.
999 `slot' if non-nil, specifies the window slot where to display
1000 BUFFER. A value of zero or nil means use the middle slot on
1001 the specified side. A negative value means use a slot
1002 preceding (that is, above or on the left of) the middle slot.
1003 A positive value means use a slot following (that is, below or
1004 on the right of) the middle slot. The default is zero.
1006 If the current frame size or the settings of `window-sides-slots'
1007 do not permit making a new window, a suitable existing window may
1008 be reused and have its `window-slot' parameter value accordingly
1011 Unless `display-buffer-mark-dedicated' is non-nil, dedicate the
1012 side window used to BUFFER so that it does not get reused by
1013 other `display-buffer' action functions. Return the window used
1014 for displaying BUFFER, nil if no suitable window can be found.
1016 This function installs the `window-side' and `window-slot'
1017 parameters and makes them persistent. It neither modifies ALIST
1018 nor installs any other window parameters unless they have been
1019 explicitly provided via a `window-parameters' entry in ALIST."
1020 (let* ((side (or (cdr (assq 'side alist
)) 'bottom
))
1021 (slot (or (cdr (assq 'slot alist
)) 0))
1022 (left-or-right (memq side
'(left right
)))
1023 ;; Softly dedicate window to BUFFER unless
1024 ;; `display-buffer-mark-dedicated' already asks for it.
1025 (dedicated (or display-buffer-mark-dedicated
'side
)))
1027 ((not (memq side
'(top bottom left right
)))
1028 (error "Invalid side %s specified" side
))
1029 ((not (numberp slot
))
1030 (error "Invalid slot %s specified" slot
)))
1032 (let* ((major (window-with-parameter 'window-side side nil t
))
1033 ;; `major' is the major window on SIDE, `windows' the list of
1034 ;; life windows on SIDE.
1035 (reversed (window--sides-reverse-on-frame-p (selected-frame)))
1038 ((window-live-p major
)
1040 ((window-valid-p major
)
1041 (let* ((first (window-child major
))
1042 (next (window-next-sibling first
))
1043 (windows (list next first
)))
1044 (setq reversed
(> (window-parameter first
'window-slot
)
1045 (window-parameter next
'window-slot
)))
1046 (while (setq next
(window-next-sibling next
))
1047 (setq windows
(cons next windows
)))
1048 (if reversed windows
(nreverse windows
))))))
1049 (slots (when major
(max 1 (window-child-count major
))))
1054 ((eq side
'right
) 2)
1055 ((eq side
'bottom
) 3))
1056 window-sides-slots
))
1057 (window--sides-inhibit-check t
)
1058 window this-window this-slot prev-window next-window
1059 best-window best-slot abs-slot
)
1062 ((and (numberp max-slots
) (<= max-slots
0))
1063 ;; No side-slots available on this side. Don't raise an error,
1067 ;; No major side window exists on this side, make one.
1068 (window--make-major-side-window buffer side slot alist
))
1070 ;; Scan windows on SIDE.
1072 (dolist (window windows
)
1073 (setq this-slot
(window-parameter window
'window-slot
))
1075 ;; The following should not happen and probably be checked
1076 ;; by window--sides-check.
1077 ((not (numberp this-slot
)))
1079 ;; A window with a matching slot has been found.
1080 (setq this-window window
)
1083 ;; Check if this window has a better slot value wrt the
1084 ;; slot of the window we want.
1086 (if (or (and (> this-slot
0) (> slot
0))
1087 (and (< this-slot
0) (< slot
0)))
1088 (abs (- slot this-slot
))
1089 (+ (abs slot
) (abs this-slot
))))
1090 (unless (and best-slot
(<= best-slot abs-slot
))
1091 (setq best-window window
)
1092 (setq best-slot abs-slot
))
1095 ((<= this-slot slot
)
1096 (setq next-window window
))
1098 (setq prev-window window
)))
1100 ((<= this-slot slot
)
1101 (setq prev-window window
))
1103 (setq next-window window
))))))))
1105 ;; `this-window' is the first window with the same SLOT.
1106 ;; `prev-window' is the window with the largest slot < SLOT. A new
1107 ;; window will be created after it.
1108 ;; `next-window' is the window with the smallest slot > SLOT. A new
1109 ;; window will be created before it.
1110 ;; `best-window' is the window with the smallest absolute difference
1111 ;; of its slot and SLOT.
1112 (or (and this-window
1113 ;; Reuse `this-window'.
1114 (with-current-buffer buffer
1115 (setq window--sides-shown t
))
1116 (window--display-buffer
1117 buffer this-window
'reuse alist dedicated
))
1118 (and (or (not max-slots
) (< slots max-slots
))
1119 (or (and next-window
1120 ;; Make new window before `next-window'.
1121 (let ((next-side (if left-or-right
'above
'left
))
1122 (window-combination-resize 'side
))
1123 (setq window
(split-window-no-error
1124 next-window nil next-side
))))
1126 ;; Make new window after `prev-window'.
1127 (let ((prev-side (if left-or-right
'below
'right
))
1128 (window-combination-resize 'side
))
1129 (setq window
(split-window-no-error
1130 prev-window nil prev-side
)))))
1131 (set-window-parameter window
'window-slot slot
)
1132 (with-current-buffer buffer
1133 (setq window--sides-shown t
))
1134 (window--display-buffer
1135 buffer window
'window alist dedicated
))
1137 ;; Reuse `best-window'.
1139 ;; Give best-window the new slot value.
1140 (set-window-parameter best-window
'window-slot slot
)
1141 (with-current-buffer buffer
1142 (setq window--sides-shown t
))
1143 (window--display-buffer
1144 buffer best-window
'reuse alist dedicated
)))))))))
1146 (defun window-toggle-side-windows (&optional frame
)
1147 "Toggle side windows on specified FRAME.
1148 FRAME must be a live frame and defaults to the selected one.
1150 If FRAME has at least one side window, save FRAME's state in the
1151 FRAME's `window-state' frame parameter and delete all side
1152 windows on FRAME afterwards. Otherwise, if FRAME has a
1153 `window-state' parameter, use that to restore any side windows on
1154 FRAME leaving FRAME's main window alone. Signal an error if
1155 FRAME has no side window and no saved state is found."
1157 (let* ((frame (window-normalize-frame frame
))
1158 (window--sides-inhibit-check t
)
1161 ((window-with-parameter 'window-side nil frame
)
1162 ;; At least one side window exists. Remove all side windows after
1163 ;; saving FRAME's state in its `window-state' parameter.
1164 (set-frame-parameter
1165 frame
'window-state
(window-state-get (frame-root-window frame
)))
1166 (let ((ignore-window-parameters t
))
1167 (delete-other-windows (window-main-window frame
))))
1168 ((setq state
(frame-parameter frame
'window-state
))
1169 ;; A window state was saved for FRAME. Restore it and put the
1170 ;; current root window into its main window.
1171 (let ((main-state (window-state-get (frame-root-window frame
))))
1172 (window-state-put state
(frame-root-window frame
) t
)
1173 (window-state-put main-state
(window-main-window frame
)))
1174 (window--sides-reverse-frame frame
))
1176 (error "No side windows state found")))))
1178 (defun window--sides-reverse-all ()
1179 "Maybe reverse side windows on all frames."
1180 (unless window--sides-inhibit-check
1181 (dolist (frame (frame-list))
1182 (window--sides-reverse-frame frame
))))
1184 (defun window--sides-reverse-frame (frame)
1185 "Maybe reverse side windows on FRAME."
1186 (when (eq window-sides-reversed
'bidi
)
1187 (let ((window (frame-selected-window frame
)))
1188 (unless (or (window-parameter window
'window-side
)
1189 (window-minibuffer-p window
))
1190 (set-frame-parameter
1191 frame
'window-sides-main-selected-window window
))))
1192 (window--sides-reverse-side frame
'top
)
1193 (window--sides-reverse-side frame
'bottom
))
1195 (defun window--sides-reverse-side (frame side
)
1196 "Maybe reverse windows on SIDE of FRAME."
1197 (let ((major (window-with-parameter 'window-side side frame t
))
1198 (window--sides-inhibit-check t
))
1199 (when (and major
(not (window-live-p major
)))
1200 (let* ((first (window-child major
))
1201 (reversed (> (window-parameter first
'window-slot
)
1203 (window-next-sibling first
) 'window-slot
)))
1204 (reverse (window--sides-reverse-on-frame-p frame
)))
1205 (unless (eq reversed reverse
)
1206 ;; We have to reverse.
1207 (let ((last (window-last-child major
)))
1208 (while (and (not (eq first last
))
1209 (not (eq first
(window-next-sibling last
))))
1210 (window-swap-states first last t
)
1211 (setq first
(window-next-sibling first
))
1212 (setq last
(window-prev-sibling last
)))))))))
1214 (defun window--sides-reverse (symbol value
)
1215 "Helper function for customizing `window-sides-reversed'."
1216 (set-default symbol value
)
1217 (remove-hook 'buffer-list-update-hook
'window--sides-reverse-all
)
1218 (remove-hook 'window-configuration-change-hook
'window--sides-reverse-all
)
1219 (dolist (frame (frame-list))
1220 (set-frame-parameter frame
'window-sides-main-selected-window nil
))
1221 (when (eq value
'bidi
)
1222 (add-hook 'buffer-list-update-hook
'window--sides-reverse-all
)
1223 (add-hook 'window-configuration-change-hook
'window--sides-reverse-all
))
1224 (window--sides-reverse-all))
1226 (defun window--sides-verticalize-frame (&optional frame
)
1227 "Maybe change side windows layout on specified FRAME."
1228 (setq frame
(window-normalize-frame frame
))
1229 (let ((window--sides-inhibit-check t
)
1230 (root (frame-root-window frame
))
1231 (main (window-main-window frame
)))
1232 (when (and (not (eq main root
))
1233 (not (eq (window-parent main
) root
))
1234 (window-combined-p main window-sides-vertical
))
1235 (let* ((window--sides-inhibit-check t
)
1236 (ignore-window-parameters t
)
1237 (first (window-child root
))
1239 (and first
(window-parameter first
'window-side
)
1240 (window-state-get first
)))
1241 (last (window-last-child root
))
1243 (and last
(window-parameter last
'window-side
)
1244 (window-state-get last
)))
1245 (dummy (get-buffer-create " *dummy*"))
1249 (when first-state
(delete-window first
))
1250 (when last-state
(delete-window last
))
1252 (setq major
(window--make-major-side-window
1253 dummy
(if window-sides-vertical
'top
'left
) 0))
1254 (window-state-put first-state major t
))
1256 (setq major
(window--make-major-side-window
1257 dummy
(if window-sides-vertical
'bottom
'right
) 0))
1258 (window-state-put last-state major t
)))
1259 (kill-buffer " *dummy*"))))))
1261 (defun window--sides-verticalize (symbol value
)
1262 "Helper function for customizing `window-sides-vertical'."
1263 (set-default symbol value
)
1264 (dolist (frame (frame-list))
1265 (window--sides-verticalize-frame frame
)))
1267 (defun window--sides-check-failed (frame)
1268 "Helper function for `window--sides-check'."
1270 ;; FRAME must have a main window.
1271 (unless (window-main-window frame
)
1272 (error "Frame %s has no main window" frame
)
1274 ;; Now check the side windows.
1275 (dolist (side '(left top right bottom
))
1276 (let ((window (window-with-parameter 'window-side side frame t
)))
1278 ;; If WINDOW is live there must be no other window on this frame
1279 ;; with the same `window-side' parameter.
1280 (if (window-live-p window
)
1283 (when (and (eq (window-parameter this
'window-side
) side
)
1284 (not (eq this window
)))
1285 (error "Window %s has same side %s as window %s but no common parent"
1291 (if (eq (window-parent this
) window
)
1292 (unless (eq (window-parameter this
'window-side
) side
)
1293 (error "Window %s has not same side %s as its parent %s"
1296 (when (and (eq (window-parameter this
'window-side
) side
)
1297 (not (eq this window
)))
1298 (error "Window %s has same side %s as major side window %s but its parent is %s"
1299 this side window
(window-parent this
))
1300 (throw 'failed t
))))
1301 frame t
'nomini
)))))))
1303 (defun window--sides-check (frame)
1304 "Check side windows configuration of FRAME.
1305 In a valid side windows configuration there can be at most one
1306 internal side window on each side and all its children must be
1307 live and have the same `window-side' parameter and no other
1308 window with the same `window-side' parameter exists on FRAME. If
1309 there is no such internal window, there may be at most one window
1310 with this side's `window-side' parameter on FRAME.
1312 If the configuration is invalid, reset the `window-side'
1313 parameters of all windows on FRAME."
1314 (when (and (not window--sides-inhibit-check
)
1315 (window-with-parameter 'window-side nil frame t
)
1316 (window--sides-check-failed frame
))
1317 ;; Reset all `window-side' parameters.
1320 (set-window-parameter window
'window-side nil
))
1322 (message "Side windows configuration reset for frame %s" frame
)))
1324 (defun window--check (&optional frame
)
1325 "Check atomic and side windows on FRAME.
1326 FRAME defaults to the selected frame."
1327 (window--sides-check frame
)
1328 (window--atom-check frame
))
1330 ;; Dumping frame/window contents.
1331 (defun window--dump-window (&optional window erase
)
1332 "Dump WINDOW to buffer *window-frame-dump*.
1333 WINDOW must be a valid window and defaults to the selected one.
1334 Optional argument ERASE non-nil means erase *window-frame-dump*
1335 before writing to it."
1336 (setq window
(window-normalize-window window
))
1337 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1338 (when erase
(erase-buffer))
1340 (format "%s parent: %s\n" window
(window-parent window
))
1341 (format "pixel left: %s top: %s size: %s x %s new: %s\n"
1342 (window-pixel-left window
) (window-pixel-top window
)
1343 (window-size window t t
) (window-size window nil t
)
1344 (window-new-pixel window
))
1345 (format "char left: %s top: %s size: %s x %s new: %s\n"
1346 (window-left-column window
) (window-top-line window
)
1347 (window-total-size window t
) (window-total-size window
)
1348 (window-new-total window
))
1349 (format "normal: %s x %s new: %s\n"
1350 (window-normal-size window t
) (window-normal-size window
)
1351 (window-new-normal window
)))
1352 (when (window-live-p window
)
1353 (let ((fringes (window-fringes window
))
1354 (margins (window-margins window
)))
1356 (format "body pixel: %s x %s char: %s x %s\n"
1357 (window-body-width window t
) (window-body-height window t
)
1358 (window-body-width window
) (window-body-height window
))
1359 (format "width left fringe: %s left margin: %s right margin: %s\n"
1360 (car fringes
) (or (car margins
) 0) (or (cdr margins
) 0))
1361 (format "width right fringe: %s scroll-bar: %s divider: %s\n"
1363 (window-scroll-bar-width window
)
1364 (window-right-divider-width window
))
1365 (format "height header-line: %s mode-line: %s divider: %s\n"
1366 (window-header-line-height window
)
1367 (window-mode-line-height window
)
1368 (window-bottom-divider-width window
)))))
1371 (defun window--dump-frame (&optional window-or-frame
)
1372 "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
1373 WINDOW-OR-FRAME can be a frame or a window and defaults to the
1374 selected frame. When WINDOW-OR-FRAME is a window, dump that
1375 window's frame. The buffer *window-frame-dump* is erased before
1379 ((or (not window-or-frame
)
1380 (frame-live-p window-or-frame
))
1381 (frame-root-window window-or-frame
))
1382 ((or (window-live-p window-or-frame
)
1383 (window-child window-or-frame
))
1386 (frame-root-window))))
1387 (frame (window-frame window
)))
1388 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1391 (format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
1392 (frame-pixel-width frame
) (frame-pixel-height frame
)
1393 (frame-total-cols frame
) (frame-total-lines frame
)
1394 (frame-char-width frame
) (frame-char-height frame
))
1395 (format "frame text pixel: %s x %s cols/lines: %s x %s\n"
1396 (frame-text-width frame
) (frame-text-height frame
)
1397 (frame-text-cols frame
) (frame-text-lines frame
))
1398 (format "tool: %s scroll: %s/%s fringe: %s border: %s right: %s bottom: %s\n\n"
1399 (if (fboundp 'tool-bar-height
)
1400 (tool-bar-height frame t
)
1402 (frame-scroll-bar-width frame
)
1403 (frame-scroll-bar-height frame
)
1404 (frame-fringe-width frame
)
1405 (frame-border-width frame
)
1406 (frame-right-divider-width frame
)
1407 (frame-bottom-divider-width frame
)))
1408 (walk-window-tree 'window--dump-window frame t t
))))
1411 (defun window-total-size (&optional window horizontal round
)
1412 "Return the total height or width of WINDOW.
1413 WINDOW must be a valid window and defaults to the selected one.
1415 If HORIZONTAL is omitted or nil, return the total height of
1416 WINDOW, in lines. If WINDOW is live, its total height includes,
1417 in addition to the height of WINDOW's text, the heights of
1418 WINDOW's mode and header line and a bottom divider, if any.
1420 If HORIZONTAL is non-nil, return the total width of WINDOW, in
1421 columns. If WINDOW is live, its total width includes, in
1422 addition to the width of WINDOW's text, the widths of WINDOW's
1423 fringes, margins, scroll bars and its right divider, if any.
1425 If WINDOW is internal, return the respective size of the screen
1426 areas spanned by its children.
1428 Optional argument ROUND is handled as for `window-total-height'
1429 and `window-total-width'."
1431 (window-total-width window round
)
1432 (window-total-height window round
)))
1434 (defun window-size (&optional window horizontal pixelwise round
)
1435 "Return the height or width of WINDOW.
1436 WINDOW must be a valid window and defaults to the selected one.
1438 If HORIZONTAL is omitted or nil, return the total height of
1439 WINDOW, in lines, like `window-total-height'. Otherwise return
1440 the total width, in columns, like `window-total-width'.
1442 Optional argument PIXELWISE means return the pixel size of WINDOW
1443 like `window-pixel-height' and `window-pixel-width'.
1445 Optional argument ROUND is ignored if PIXELWISE is non-nil and
1446 handled as for `window-total-height' and `window-total-width'
1450 (window-pixel-width window
)
1451 (window-total-width window round
))
1453 (window-pixel-height window
)
1454 (window-total-height window round
))))
1456 (defvar window-size-fixed nil
1457 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
1458 If the value is `height', then only the window's height is fixed.
1459 If the value is `width', then only the window's width is fixed.
1460 Any other non-nil value fixes both the width and the height.
1462 Emacs won't change the size of any window displaying that buffer,
1463 unless it has no other choice (like when deleting a neighboring
1465 (make-variable-buffer-local 'window-size-fixed
)
1467 (defun window--preservable-size (window &optional horizontal
)
1468 "Return height of WINDOW as `window-preserve-size' would preserve it.
1469 Optional argument HORIZONTAL non-nil means to return the width of
1470 WINDOW as `window-preserve-size' would preserve it."
1472 (window-body-width window t
)
1473 (+ (window-body-height window t
)
1474 (window-header-line-height window
)
1475 (window-mode-line-height window
))))
1477 (defun window-preserve-size (&optional window horizontal preserve
)
1478 "Preserve height of window WINDOW.
1479 WINDOW must be a live window and defaults to the selected one.
1480 Optional argument HORIZONTAL non-nil means preserve the width of
1483 PRESERVE t means to preserve the current height/width of WINDOW's
1484 body in frame and window resizing operations whenever possible.
1485 The height/width of WINDOW will change only if Emacs has no other
1486 choice. Resizing a window whose height/width is preserved never
1489 PRESERVE nil means to stop preserving the height/width of WINDOW,
1490 lifting the respective restraint induced by a previous call of
1491 `window-preserve-size' for WINDOW. Calling `enlarge-window',
1492 `shrink-window', `split-window' or `fit-window-to-buffer' with
1493 WINDOW as argument also removes the respective restraint.
1495 Other values of PRESERVE are reserved for future use."
1496 (setq window
(window-normalize-window window t
))
1497 (let* ((parameter (window-parameter window
'window-preserved-size
))
1498 (width (nth 1 parameter
))
1499 (height (nth 2 parameter
)))
1501 (set-window-parameter
1502 window
'window-preserved-size
1504 (window-buffer window
)
1505 (and preserve
(window--preservable-size window t
))
1507 (set-window-parameter
1508 window
'window-preserved-size
1510 (window-buffer window
)
1512 (and preserve
(window--preservable-size window
)))))))
1514 (defun window-preserved-size (&optional window horizontal
)
1515 "Return preserved height of window WINDOW.
1516 WINDOW must be a live window and defaults to the selected one.
1517 Optional argument HORIZONTAL non-nil means to return preserved
1519 (setq window
(window-normalize-window window t
))
1520 (let* ((parameter (window-parameter window
'window-preserved-size
))
1521 (buffer (nth 0 parameter
))
1522 (width (nth 1 parameter
))
1523 (height (nth 2 parameter
)))
1524 (when (eq buffer
(window-buffer window
))
1525 (if horizontal width height
))))
1527 (defun window--preserve-size (window horizontal
)
1528 "Return non-nil when the height of WINDOW shall be preserved.
1529 Optional argument HORIZONTAL non-nil means to return non-nil when
1530 the width of WINDOW shall be preserved."
1531 (let ((size (window-preserved-size window horizontal
)))
1533 (= size
(window--preservable-size window horizontal
)))))
1535 (defun window-safe-min-size (&optional window horizontal pixelwise
)
1536 "Return safe minimum size of WINDOW.
1537 WINDOW must be a valid window and defaults to the selected one.
1538 Optional argument HORIZONTAL non-nil means return the minimum
1539 number of columns of WINDOW; otherwise return the minimum number
1542 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1544 (setq window
(window-normalize-window window
))
1547 (* window-safe-min-width
1548 (frame-char-width (window-frame window
)))
1549 (* window-safe-min-height
1550 (frame-char-height (window-frame window
))))
1551 (if horizontal window-safe-min-width window-safe-min-height
)))
1553 (defun window-min-size (&optional window horizontal ignore pixelwise
)
1554 "Return the minimum size of WINDOW.
1555 WINDOW must be a valid window and defaults to the selected one.
1556 Optional argument HORIZONTAL non-nil means return the minimum
1557 number of columns of WINDOW; otherwise return the minimum number
1560 The optional argument IGNORE has the same meaning as for
1561 `window-resizable'. Optional argument PIXELWISE non-nil means
1562 return the minimum pixel-size of WINDOW."
1564 (window-normalize-window window
) horizontal ignore pixelwise
))
1566 (defun window--min-size-ignore-p (window ignore
)
1567 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
1568 (if (window-valid-p ignore
)
1570 (not (memq ignore
'(nil preserved
)))))
1572 (defun window--min-size-1 (window horizontal ignore pixelwise
)
1573 "Internal function of `window-min-size'."
1574 (let ((sub (window-child window
)))
1577 ;; WINDOW is an internal window.
1578 (if (window-combined-p sub horizontal
)
1579 ;; The minimum size of an iso-combination is the sum of
1580 ;; the minimum sizes of its child windows.
1582 (setq value
(+ value
1584 sub horizontal ignore pixelwise
)))
1585 (setq sub
(window-right sub
)))
1586 ;; The minimum size of an ortho-combination is the maximum
1587 ;; of the minimum sizes of its child windows.
1589 (setq value
(max value
1591 sub horizontal ignore pixelwise
)))
1592 (setq sub
(window-right sub
))))
1594 (with-current-buffer (window-buffer window
)
1596 ((window-minibuffer-p window
)
1597 (if pixelwise
(frame-char-height (window-frame window
)) 1))
1598 ((window-size-fixed-p window horizontal ignore
)
1599 ;; The minimum size of a fixed size window is its size.
1600 (window-size window horizontal pixelwise
))
1602 ;; If IGNORE equals `safe' return the safe value.
1603 (window-safe-min-size window horizontal pixelwise
))
1605 ;; For the minimum width of a window take fringes and
1606 ;; scroll-bars into account. This is questionable and should
1607 ;; be removed as soon as we are able to split (and resize)
1608 ;; windows such that the new (or resized) windows can get a
1609 ;; size less than the user-specified `window-min-height' and
1610 ;; `window-min-width'.
1611 (let* ((char-size (frame-char-size window t
))
1612 (fringes (window-fringes window
))
1613 (margins (window-margins window
))
1614 ;; Let the 'min-margins' parameter override the actual
1615 ;; widths of the margins. We allow any number to
1616 ;; replace the values specified by `window-margins'.
1617 ;; See bug#24193 for the rationale of this parameter.
1618 (min-margins (window-parameter window
'min-margins
))
1619 (left-min-margin (and min-margins
1620 (numberp (car min-margins
))
1622 (right-min-margin (and min-margins
1623 (numberp (cdr min-margins
))
1626 (+ (window-safe-min-size window t t
)
1627 (* (or left-min-margin
(car margins
) 0) char-size
)
1628 (* (or right-min-margin
(cdr margins
) 0) char-size
)
1629 (car fringes
) (cadr fringes
)
1630 (window-scroll-bar-width window
)
1631 (window-right-divider-width window
))))
1634 (if window-resize-pixelwise
1636 ;; Round up to next integral of columns.
1637 (* (ceiling pixel-width char-size
) char-size
))
1638 (if (window--min-size-ignore-p window ignore
)
1640 (window-min-pixel-width window
)))
1642 (ceiling pixel-width char-size
)
1643 (if (window--min-size-ignore-p window ignore
)
1645 window-min-width
)))))
1646 ((let ((char-size (frame-char-size window
))
1648 (+ (window-safe-min-size window nil t
)
1649 (window-header-line-height window
)
1650 (window-scroll-bar-height window
)
1651 (window-mode-line-height window
)
1652 (window-bottom-divider-width window
))))
1655 (if window-resize-pixelwise
1657 ;; Round up to next integral of lines.
1658 (* (ceiling pixel-height char-size
) char-size
))
1659 (if (window--min-size-ignore-p window ignore
)
1661 (window-min-pixel-height window
)))
1662 (max (ceiling pixel-height char-size
)
1663 (if (window--min-size-ignore-p window ignore
)
1665 window-min-height
))))))))))
1667 (defun window-sizable (window delta
&optional horizontal ignore pixelwise
)
1668 "Return DELTA if DELTA lines can be added to WINDOW.
1669 WINDOW must be a valid window and defaults to the selected one.
1670 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1671 columns can be added to WINDOW. A return value of zero means
1672 that no lines (or columns) can be added to WINDOW.
1674 This function looks only at WINDOW and, recursively, its child
1675 windows. The function `window-resizable' looks at other windows
1678 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1679 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1680 return the maximum value in the range 0..DELTA by which WINDOW
1683 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1684 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1685 return the minimum value in the range DELTA..0 by which WINDOW
1688 The optional argument IGNORE has the same meaning as for
1689 `window-resizable'. Optional argument PIXELWISE non-nil means
1690 interpret DELTA as pixels."
1691 (setq window
(window-normalize-window window
))
1694 (max (- (window-min-size window horizontal ignore pixelwise
)
1695 (window-size window horizontal pixelwise
))
1698 (if (window-size-fixed-p window horizontal ignore
)
1703 (defun window-sizable-p (window delta
&optional horizontal ignore pixelwise
)
1704 "Return t if WINDOW can be resized by DELTA lines.
1705 WINDOW must be a valid window and defaults to the selected one.
1706 For the meaning of the arguments of this function see the
1707 doc-string of `window-sizable'."
1708 (setq window
(window-normalize-window window
))
1710 (>= (window-sizable window delta horizontal ignore pixelwise
)
1712 (<= (window-sizable window delta horizontal ignore pixelwise
)
1715 (defun window--size-fixed-1 (window horizontal ignore
)
1716 "Internal function for `window-size-fixed-p'."
1717 (let ((sub (window-child window
)))
1720 ;; WINDOW is an internal window.
1721 (if (window-combined-p sub horizontal
)
1722 ;; An iso-combination is fixed size if all its child
1723 ;; windows are fixed-size.
1726 (unless (window--size-fixed-1 sub horizontal ignore
)
1727 ;; We found a non-fixed-size child window, so
1728 ;; WINDOW's size is not fixed.
1730 (setq sub
(window-right sub
)))
1731 ;; All child windows are fixed-size, so WINDOW's size is
1734 ;; An ortho-combination is fixed-size if at least one of its
1735 ;; child windows is fixed-size.
1737 (when (window--size-fixed-1 sub horizontal ignore
)
1738 ;; We found a fixed-size child window, so WINDOW's size
1741 (setq sub
(window-right sub
))))
1742 ;; WINDOW is a live window.
1743 (and (or (not (windowp ignore
)) (not (eq window ignore
)))
1744 (or (and (not (eq ignore
'preserved
))
1745 (window--preserve-size window horizontal
))
1746 (with-current-buffer (window-buffer window
)
1748 (memq window-size-fixed
'(width t
))
1749 (memq window-size-fixed
'(height t
))))))))))
1751 (defun window-size-fixed-p (&optional window horizontal ignore
)
1752 "Return non-nil if WINDOW's height is fixed.
1753 WINDOW must be a valid window and defaults to the selected one.
1754 Optional argument HORIZONTAL non-nil means return non-nil if
1755 WINDOW's width is fixed. The optional argument IGNORE has the
1756 same meaning as for `window-resizable'.
1758 If this function returns nil, this does not necessarily mean that
1759 WINDOW can be resized in the desired direction. The function
1760 `window-resizable' can tell that."
1761 (when (or (windowp ignore
) (memq ignore
'(nil preserved
)))
1762 (window--size-fixed-1
1763 (window-normalize-window window
) horizontal ignore
)))
1765 (defun window--min-delta-1 (window delta
&optional horizontal ignore trail noup pixelwise
)
1766 "Internal function for `window-min-delta'."
1767 (if (not (window-parent window
))
1768 ;; If we can't go up, return zero.
1770 ;; Else try to find a non-fixed-size sibling of WINDOW.
1771 (let* ((parent (window-parent window
))
1772 (sub (window-child parent
)))
1774 (if (window-combined-p sub horizontal
)
1775 ;; In an iso-combination throw DELTA if we find at least one
1776 ;; child window and that window is either not fixed-size or
1777 ;; we can ignore fixed-sizeness.
1778 (let ((skip (eq trail
'after
)))
1782 (setq skip
(eq trail
'before
)))
1784 ((window-size-fixed-p sub horizontal ignore
))
1786 ;; We found a non-fixed-size child window.
1787 (throw 'done delta
)))
1788 (setq sub
(window-right sub
))))
1789 ;; In an ortho-combination set DELTA to the minimum value by
1790 ;; which other child windows can shrink.
1792 (unless (eq sub window
)
1795 (max (- (window-size sub horizontal pixelwise
'ceiling
)
1797 sub horizontal ignore pixelwise
))
1799 (setq sub
(window-right sub
))))
1802 (window--min-delta-1
1803 parent delta horizontal ignore trail nil pixelwise
))))))
1805 (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise
)
1806 "Return number of lines by which WINDOW can be shrunk.
1807 WINDOW must be a valid window and defaults to the selected one.
1808 Return zero if WINDOW cannot be shrunk.
1810 Optional argument HORIZONTAL non-nil means return number of
1811 columns by which WINDOW can be shrunk.
1813 The optional argument IGNORE has the same meaning as for
1814 `window-resizable'. Optional argument TRAIL restricts the
1815 windows that can be enlarged. If its value is `before', only
1816 windows to the left of or above WINDOW can be enlarged. If it is
1817 `after', only windows to the right of or below WINDOW can be
1820 Optional argument NOUP non-nil means don't go up in the window
1821 tree, but try to enlarge windows within WINDOW's combination
1822 only. Optional argument NODOWN non-nil means don't check whether
1823 WINDOW itself (and its child windows) can be shrunk; check only
1824 whether at least one other window can be enlarged appropriately.
1826 Optional argument PIXELWISE non-nil means return number of pixels
1827 by which WINDOW can be shrunk."
1828 (setq window
(window-normalize-window window
))
1829 (let ((size (window-size window horizontal pixelwise
'floor
))
1830 (minimum (window-min-size window horizontal ignore pixelwise
)))
1833 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1834 (window--min-delta-1
1835 window size horizontal ignore trail noup pixelwise
))
1837 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1838 ;; there's nothing to recover.
1841 ;; Otherwise, try to recover whatever WINDOW is larger than its
1843 (window--min-delta-1
1844 window
(- size minimum
) horizontal ignore trail noup pixelwise
)))))
1846 (defun frame-windows-min-size (&optional frame horizontal ignore pixelwise
)
1847 "Return minimum number of lines of FRAME's windows.
1848 HORIZONTAL non-nil means return number of columns of FRAME's
1849 windows. The optional argument IGNORE has the same meaning as
1850 for `window-resizable'. PIXELWISE non-nil means return sizes in
1852 (setq frame
(window-normalize-frame frame
))
1853 (let* ((root (frame-root-window frame
))
1854 (mini (window-next-sibling root
)))
1855 (+ (window-min-size root horizontal ignore pixelwise
)
1856 (if (and mini
(not horizontal
))
1857 (window-min-size mini horizontal nil pixelwise
)
1860 (defun window--max-delta-1 (window delta
&optional horizontal ignore trail noup pixelwise
)
1861 "Internal function of `window-max-delta'."
1862 (if (not (window-parent window
))
1863 ;; Can't go up. Return DELTA.
1865 (let* ((parent (window-parent window
))
1866 (sub (window-child parent
)))
1868 (if (window-combined-p sub horizontal
)
1869 ;; For an iso-combination calculate how much we can get from
1870 ;; other child windows.
1871 (let ((skip (eq trail
'after
)))
1875 (setq skip
(eq trail
'before
)))
1881 (- (window-size sub horizontal pixelwise
'floor
)
1883 sub horizontal ignore pixelwise
))
1885 (setq sub
(window-right sub
))))
1886 ;; For an ortho-combination throw DELTA when at least one
1887 ;; child window is fixed-size.
1889 (when (and (not (eq sub window
))
1890 (window-size-fixed-p sub horizontal ignore
))
1891 (throw 'fixed delta
))
1892 (setq sub
(window-right sub
))))
1894 ;; When NOUP is nil, DELTA is all we can get.
1896 ;; Else try with parent of WINDOW, passing the DELTA we
1897 ;; recovered so far.
1898 (window--max-delta-1
1899 parent delta horizontal ignore trail nil pixelwise
))))))
1901 (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise
)
1902 "Return maximum number of lines by which WINDOW can be enlarged.
1903 WINDOW must be a valid window and defaults to the selected one.
1904 The return value is zero if WINDOW cannot be enlarged.
1906 Optional argument HORIZONTAL non-nil means return maximum number
1907 of columns by which WINDOW can be enlarged.
1909 The optional argument IGNORE has the same meaning as for
1910 `window-resizable'. Optional argument TRAIL restricts the
1911 windows that can be enlarged. If its value is `before', only
1912 windows to the left of or above WINDOW can be enlarged. If it is
1913 `after', only windows to the right of or below WINDOW can be
1916 Optional argument NOUP non-nil means don't go up in the window
1917 tree but try to obtain the entire space from windows within
1918 WINDOW's combination. Optional argument NODOWN non-nil means do
1919 not check whether WINDOW itself (and its child windows) can be
1920 enlarged; check only whether other windows can be shrunk
1923 Optional argument PIXELWISE non-nil means return number of
1924 pixels by which WINDOW can be enlarged."
1925 (setq window
(window-normalize-window window
))
1926 (if (and (not nodown
) (window-size-fixed-p window horizontal ignore
))
1927 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1930 ;; WINDOW has no fixed size.
1931 (window--max-delta-1 window
0 horizontal ignore trail noup pixelwise
)))
1933 ;; Make NOUP also inhibit the min-size check.
1934 (defun window--resizable (window delta
&optional horizontal ignore trail noup nodown pixelwise
)
1935 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1936 WINDOW must be a valid window and defaults to the selected one.
1937 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1938 can be resized horizontally by DELTA columns. A return value of
1939 zero means that WINDOW is not resizable.
1941 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1942 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1943 return the maximum value in the range 0..DELTA by which WINDOW
1946 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1947 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1948 return the minimum value in the range DELTA..0 that can be used
1949 for shrinking WINDOW.
1951 The optional argument IGNORE has the same meaning as for
1952 `window-resizable'. Optional argument TRAIL `before' means only
1953 windows to the left of or below WINDOW can be shrunk. Optional
1954 argument TRAIL `after' means only windows to the right of or
1955 above WINDOW can be shrunk.
1957 Optional argument NOUP non-nil means don't go up in the window
1958 tree but check only whether space can be obtained from (or given
1959 to) WINDOW's siblings. Optional argument NODOWN non-nil means
1960 don't go down in the window tree. This means do not check
1961 whether resizing would violate size restrictions of WINDOW or its
1964 Optional argument PIXELWISE non-nil means interpret DELTA as
1966 (setq window
(window-normalize-window window
))
1969 (max (- (window-min-delta
1970 window horizontal ignore trail noup nodown pixelwise
))
1973 (min (window-max-delta
1974 window horizontal ignore trail noup nodown pixelwise
)
1978 (defun window--resizable-p (window delta
&optional horizontal ignore trail noup nodown pixelwise
)
1979 "Return t if WINDOW can be resized vertically by DELTA lines.
1980 WINDOW must be a valid window and defaults to the selected one.
1981 For the meaning of the arguments of this function see the
1982 doc-string of `window--resizable'.
1984 Optional argument PIXELWISE non-nil means interpret DELTA as
1986 (setq window
(window-normalize-window window
))
1988 (>= (window--resizable
1989 window delta horizontal ignore trail noup nodown pixelwise
)
1991 (<= (window--resizable
1992 window delta horizontal ignore trail noup nodown pixelwise
)
1995 (defun window-resizable (window delta
&optional horizontal ignore pixelwise
)
1996 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1997 WINDOW must be a valid window and defaults to the selected one.
1998 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1999 can be resized horizontally by DELTA columns. A return value of
2000 zero means that WINDOW is not resizable.
2002 DELTA positive means WINDOW shall be enlarged by DELTA lines or
2003 columns. If WINDOW cannot be enlarged by DELTA lines or columns
2004 return the maximum value in the range 0..DELTA by which WINDOW
2007 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
2008 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
2009 return the minimum value in the range DELTA..0 that can be used
2010 for shrinking WINDOW.
2012 Optional argument IGNORE, if non-nil, means to ignore restraints
2013 induced by fixed size windows or the values of the variables
2014 `window-min-height' and `window-min-width'. The following values
2015 have special meanings: `safe' means that in addition live windows
2016 are allowed to get as small as `window-safe-min-height' lines and
2017 `window-safe-min-width' columns. `preserved' means to ignore
2018 only restrictions induced by `window-preserve-size'. If IGNORE
2019 is a window, then ignore restrictions for that window only.
2021 Optional argument PIXELWISE non-nil means interpret DELTA as
2023 (setq window
(window-normalize-window window
))
2024 (window--resizable window delta horizontal ignore nil nil nil pixelwise
))
2026 (defun window-resizable-p (window delta
&optional horizontal ignore pixelwise
)
2027 "Return t if WINDOW can be resized vertically by DELTA lines.
2028 WINDOW must be a valid window and defaults to the selected one.
2029 For the meaning of the arguments of this function see the
2030 doc-string of `window-resizable'."
2031 (setq window
(window-normalize-window window
))
2033 (>= (window--resizable
2034 window delta horizontal ignore nil nil nil pixelwise
)
2036 (<= (window--resizable
2037 window delta horizontal ignore nil nil nil pixelwise
)
2040 ;; Aliases of functions defined in window.c.
2041 (defalias 'window-height
'window-total-height
)
2042 (defalias 'window-width
'window-body-width
)
2044 (defun window-full-height-p (&optional window
)
2045 "Return t if WINDOW is as high as its containing frame.
2046 More precisely, return t if and only if the total height of
2047 WINDOW equals the total height of the root window of WINDOW's
2048 frame. WINDOW must be a valid window and defaults to the
2050 (setq window
(window-normalize-window window
))
2051 (if (window-minibuffer-p window
)
2052 (eq window
(frame-root-window (window-frame window
)))
2053 (= (window-pixel-height window
)
2054 (window-pixel-height (frame-root-window window
)))))
2056 (defun window-full-width-p (&optional window
)
2057 "Return t if WINDOW is as wide as its containing frame.
2058 More precisely, return t if and only if the total width of WINDOW
2059 equals the total width of the root window of WINDOW's frame.
2060 WINDOW must be a valid window and defaults to the selected one."
2061 (setq window
(window-normalize-window window
))
2062 (= (window-pixel-width window
)
2063 (window-pixel-width (frame-root-window window
))))
2065 (defun window-body-size (&optional window horizontal pixelwise
)
2066 "Return the height or width of WINDOW's text area.
2067 WINDOW must be a live window and defaults to the selected one.
2069 If HORIZONTAL is omitted or nil, return the height of the text
2070 area, like `window-body-height'. Otherwise, return the width of
2071 the text area, like `window-body-width'. In either case, the
2072 optional argument PIXELWISE is passed to the functions."
2074 (window-body-width window pixelwise
)
2075 (window-body-height window pixelwise
)))
2077 (declare-function font-info
"font.c" (name &optional frame
))
2079 (defun window-font-width (&optional window face
)
2080 "Return average character width for the font of FACE used in WINDOW.
2081 WINDOW must be a live window and defaults to the selected one.
2083 If FACE is nil or omitted, the default face is used. If FACE is
2084 remapped (see `face-remapping-alist'), the function returns the
2085 information for the remapped face."
2086 (with-selected-window (window-normalize-window window t
)
2087 (if (display-multi-font-p)
2088 (let* ((face (if face face
'default
))
2089 (info (font-info (face-font face
)))
2090 (width (aref info
11)))
2094 (frame-char-width))))
2096 (defun window-font-height (&optional window face
)
2097 "Return character height for the font of FACE used in WINDOW.
2098 WINDOW must be a live window and defaults to the selected one.
2100 If FACE is nil or omitted, the default face is used. If FACE is
2101 remapped (see `face-remapping-alist'), the function returns the
2102 information for the remapped face."
2103 (with-selected-window (window-normalize-window window t
)
2104 (if (display-multi-font-p)
2105 (let* ((face (if face face
'default
))
2106 (info (font-info (face-font face
))))
2108 (frame-char-height))))
2110 (defvar overflow-newline-into-fringe
)
2112 (defun window-max-chars-per-line (&optional window face
)
2113 "Return the number of characters that can be displayed on one line in WINDOW.
2114 WINDOW must be a live window and defaults to the selected one.
2116 The character width of FACE is used for the calculation. If FACE
2117 is nil or omitted, the default face is used. If FACE is
2118 remapped (see `face-remapping-alist'), the function uses the
2121 This function is different from `window-body-width' in two
2122 ways. First, it accounts for the portions of the line reserved
2123 for the continuation glyph. Second, it accounts for the size of
2125 (with-selected-window (window-normalize-window window t
)
2126 (let* ((window-width (window-body-width window t
))
2127 (font-width (window-font-width window face
))
2128 (ncols (/ window-width font-width
)))
2129 (if (and (display-graphic-p)
2130 overflow-newline-into-fringe
2132 (or (eq left-fringe-width
0)
2133 (and (null left-fringe-width
)
2134 (= (frame-parameter nil
'left-fringe
) 0))))
2136 (or (eq right-fringe-width
0)
2137 (and (null right-fringe-width
)
2138 (= (frame-parameter nil
'right-fringe
) 0)))))
2140 ;; FIXME: This should remove 1 more column when there are no
2141 ;; fringes, lines are truncated, and the window is hscrolled,
2142 ;; but EOL is not in the view, because then there are 2
2143 ;; truncation glyphs, not one.
2146 (defun window-current-scroll-bars (&optional window
)
2147 "Return the current scroll bar types for WINDOW.
2148 WINDOW must be a live window and defaults to the selected one.
2150 The return value is a cons cell (VERTICAL . HORIZONTAL) where
2151 VERTICAL specifies the current location of the vertical scroll
2152 bar (`left', `right' or nil), and HORIZONTAL specifies the
2153 current location of the horizontal scroll bar (`bottom' or nil).
2155 Unlike `window-scroll-bars', this function reports the scroll bar
2156 type actually used, once frame defaults and `scroll-bar-mode' are
2157 taken into account."
2158 (setq window
(window-normalize-window window t
))
2159 (let ((vertical (nth 2 (window-scroll-bars window
)))
2160 (horizontal (nth 5 (window-scroll-bars window
)))
2161 (inherited (frame-current-scroll-bars (window-frame window
))))
2162 (when (eq vertical t
)
2163 (setq vertical
(car inherited
)))
2164 (when (eq horizontal t
)
2165 (setq horizontal
(cdr inherited
)))
2166 (cons vertical
(and horizontal
'bottom
))))
2168 (defun walk-windows (fun &optional minibuf all-frames
)
2169 "Cycle through all live windows, calling FUN for each one.
2170 FUN must specify a function with a window as its sole argument.
2171 The optional arguments MINIBUF and ALL-FRAMES specify the set of
2172 windows to include in the walk.
2174 MINIBUF t means include the minibuffer window even if the
2175 minibuffer is not active. MINIBUF nil or omitted means include
2176 the minibuffer window only if the minibuffer is active. Any
2177 other value means do not include the minibuffer window even if
2178 the minibuffer is active.
2180 ALL-FRAMES nil or omitted means consider all windows on the
2181 selected frame, plus the minibuffer window if specified by the
2182 MINIBUF argument. If the minibuffer counts, consider all windows
2183 on all frames that share that minibuffer too. The following
2184 non-nil values of ALL-FRAMES have special meanings:
2186 - t means consider all windows on all existing frames.
2188 - `visible' means consider all windows on all visible frames on
2189 the current terminal.
2191 - 0 (the number zero) means consider all windows on all visible
2192 and iconified frames on the current terminal.
2194 - A frame means consider all windows on that frame only.
2196 Anything else means consider all windows on the selected frame
2199 This function changes neither the order of recently selected
2200 windows nor the buffer list."
2201 ;; If we start from the minibuffer window, don't fail to come
2203 (when (window-minibuffer-p)
2205 ;; Make sure to not mess up the order of recently selected
2206 ;; windows. Use `save-selected-window' and `select-window'
2207 ;; with second argument non-nil for this purpose.
2208 (save-selected-window
2209 (when (framep all-frames
)
2210 (select-window (frame-first-window all-frames
) 'norecord
))
2211 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames
))
2212 (funcall fun walk-windows-window
))))
2214 (defun window-at-side-p (&optional window side
)
2215 "Return t if WINDOW is at SIDE of its containing frame.
2216 WINDOW must be a valid window and defaults to the selected one.
2217 SIDE can be any of the symbols `left', `top', `right' or
2218 `bottom'. The default value nil is handled like `bottom'."
2219 (setq window
(window-normalize-window window
))
2224 ((eq side
'right
) 2)
2225 ((memq side
'(bottom nil
)) 3))))
2226 (= (nth edge
(window-pixel-edges window
))
2227 (nth edge
(window-pixel-edges (frame-root-window window
))))))
2229 (defun window-at-side-list (&optional frame side
)
2230 "Return list of all windows on SIDE of FRAME.
2231 FRAME must be a live frame and defaults to the selected frame.
2232 SIDE can be any of the symbols `left', `top', `right' or
2233 `bottom'. The default value nil is handled like `bottom'."
2234 (setq frame
(window-normalize-frame frame
))
2238 (when (window-at-side-p window side
)
2239 (setq windows
(cons window windows
))))
2241 (nreverse windows
)))
2243 (defun window--in-direction-2 (window posn
&optional horizontal
)
2244 "Support function for `window-in-direction'."
2246 (let ((top (window-pixel-top window
)))
2249 (- posn top
(window-pixel-height window
))))
2250 (let ((left (window-pixel-left window
)))
2253 (- posn left
(window-pixel-width window
))))))
2255 ;; Predecessors to the below have been devised by Julian Assange in
2256 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
2257 ;; Neither of these allow one to selectively ignore specific windows
2258 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
2260 (defun window-in-direction (direction &optional window ignore sign wrap mini
)
2261 "Return window in DIRECTION as seen from WINDOW.
2262 More precisely, return the nearest window in direction DIRECTION
2263 as seen from the position of `window-point' in window WINDOW.
2264 DIRECTION must be one of `above', `below', `left' or `right'.
2265 WINDOW must be a live window and defaults to the selected one.
2267 Do not return a window whose `no-other-window' parameter is
2268 non-nil. If the nearest window's `no-other-window' parameter is
2269 non-nil, try to find another window in the indicated direction.
2270 If, however, the optional argument IGNORE is non-nil, return that
2271 window even if its `no-other-window' parameter is non-nil.
2273 Optional argument SIGN a negative number means to use the right
2274 or bottom edge of WINDOW as reference position instead of
2275 `window-point'. SIGN a positive number means to use the left or
2276 top edge of WINDOW as reference position.
2278 Optional argument WRAP non-nil means to wrap DIRECTION around
2279 frame borders. This means to return for WINDOW at the top of the
2280 frame and DIRECTION `above' the minibuffer window if the frame
2281 has one, and a window at the bottom of the frame otherwise.
2283 Optional argument MINI nil means to return the minibuffer window
2284 if and only if it is currently active. MINI non-nil means to
2285 return the minibuffer window even when it's not active. However,
2286 if WRAP is non-nil, always act as if MINI were nil.
2288 Return nil if no suitable window can be found."
2289 (setq window
(window-normalize-window window t
))
2290 (unless (memq direction
'(above below left right
))
2291 (error "Wrong direction %s" direction
))
2292 (let* ((frame (window-frame window
))
2293 (hor (memq direction
'(left right
)))
2295 (window-pixel-left window
)
2296 (window-pixel-top window
)))
2297 (last (+ first
(window-size window hor t
)))
2298 ;; The column / row value of `posn-at-point' can be nil for the
2299 ;; mini-window, guard against that.
2302 ((and (numberp sign
) (< sign
0))
2304 (1- (+ (window-pixel-top window
) (window-pixel-height window
)))
2305 (1- (+ (window-pixel-left window
) (window-pixel-width window
)))))
2306 ((and (numberp sign
) (> sign
0))
2308 (window-pixel-top window
)
2309 (window-pixel-left window
)))
2310 ((let ((posn-cons (nth 2 (posn-at-point (window-point window
) window
))))
2312 (+ (or (cdr posn-cons
) 1) (window-pixel-top window
))
2313 (+ (or (car posn-cons
) 1) (window-pixel-left window
)))))))
2316 ((eq direction
'below
) (frame-pixel-height frame
))
2317 ((eq direction
'right
) (frame-pixel-width frame
))
2319 (best-edge-2 best-edge
)
2320 (best-diff-2 (if hor
(frame-pixel-height frame
) (frame-pixel-width frame
)))
2321 best best-2 best-diff-2-new
)
2324 (let* ((w-top (window-pixel-top w
))
2325 (w-left (window-pixel-left w
)))
2328 ;; Ignore ourselves.
2329 (and (window-parameter w
'no-other-window
)
2330 ;; Ignore W unless IGNORE is non-nil.
2334 ((and (<= w-top posn
)
2335 (< posn
(+ w-top
(window-pixel-height w
))))
2336 ;; W is to the left or right of WINDOW and covers POSN.
2337 (when (or (and (eq direction
'left
)
2338 (or (and (<= w-left first
) (> w-left best-edge
))
2340 (window-at-side-p window
'left
)
2341 (window-at-side-p w
'right
))))
2342 (and (eq direction
'right
)
2343 (or (and (>= w-left last
) (< w-left best-edge
))
2345 (window-at-side-p window
'right
)
2346 (window-at-side-p w
'left
)))))
2347 (setq best-edge w-left
)
2349 ((and (or (and (eq direction
'left
)
2350 (<= (+ w-left
(window-pixel-width w
)) first
))
2351 (and (eq direction
'right
) (<= last w-left
)))
2352 ;; W is to the left or right of WINDOW but does not
2354 (setq best-diff-2-new
2355 (window--in-direction-2 w posn hor
))
2356 (or (< best-diff-2-new best-diff-2
)
2357 (and (= best-diff-2-new best-diff-2
)
2358 (if (eq direction
'left
)
2359 (> w-left best-edge-2
)
2360 (< w-left best-edge-2
)))))
2361 (setq best-edge-2 w-left
)
2362 (setq best-diff-2 best-diff-2-new
)
2364 ((and (<= w-left posn
)
2365 (< posn
(+ w-left
(window-pixel-width w
))))
2366 ;; W is above or below WINDOW and covers POSN.
2367 (when (or (and (eq direction
'above
)
2368 (or (and (<= w-top first
) (> w-top best-edge
))
2370 (window-at-side-p window
'top
)
2371 (if (active-minibuffer-window)
2372 (minibuffer-window-active-p w
)
2373 (window-at-side-p w
'bottom
)))))
2374 (and (eq direction
'below
)
2375 (or (and (>= w-top first
) (< w-top best-edge
))
2377 (if (active-minibuffer-window)
2378 (minibuffer-window-active-p window
)
2379 (window-at-side-p window
'bottom
))
2380 (window-at-side-p w
'top
)))))
2381 (setq best-edge w-top
)
2383 ((and (or (and (eq direction
'above
)
2384 (<= (+ w-top
(window-pixel-height w
)) first
))
2385 (and (eq direction
'below
) (<= last w-top
)))
2386 ;; W is above or below WINDOW but does not cover POSN.
2387 (setq best-diff-2-new
2388 (window--in-direction-2 w posn hor
))
2389 (or (< best-diff-2-new best-diff-2
)
2390 (and (= best-diff-2-new best-diff-2
)
2391 (if (eq direction
'above
)
2392 (> w-top best-edge-2
)
2393 (< w-top best-edge-2
)))))
2394 (setq best-edge-2 w-top
)
2395 (setq best-diff-2 best-diff-2-new
)
2397 frame nil
(and mini t
))
2400 (defun get-window-with-predicate (predicate &optional minibuf all-frames default
)
2401 "Return a live window satisfying PREDICATE.
2402 More precisely, cycle through all windows calling the function
2403 PREDICATE on each one of them with the window as its sole
2404 argument. Return the first window for which PREDICATE returns
2405 non-nil. Windows are scanned starting with the window following
2406 the selected window. If no window satisfies PREDICATE, return
2409 MINIBUF t means include the minibuffer window even if the
2410 minibuffer is not active. MINIBUF nil or omitted means include
2411 the minibuffer window only if the minibuffer is active. Any
2412 other value means do not include the minibuffer window even if
2413 the minibuffer is active.
2415 ALL-FRAMES nil or omitted means consider all windows on the selected
2416 frame, plus the minibuffer window if specified by the MINIBUF
2417 argument. If the minibuffer counts, consider all windows on all
2418 frames that share that minibuffer too. The following non-nil
2419 values of ALL-FRAMES have special meanings:
2421 - t means consider all windows on all existing frames.
2423 - `visible' means consider all windows on all visible frames on
2424 the current terminal.
2426 - 0 (the number zero) means consider all windows on all visible
2427 and iconified frames on the current terminal.
2429 - A frame means consider all windows on that frame only.
2431 Anything else means consider all windows on the selected frame
2434 (dolist (window (window-list-1
2435 (next-window nil minibuf all-frames
)
2436 minibuf all-frames
))
2437 (when (funcall predicate window
)
2438 (throw 'found window
)))
2441 (defalias 'some-window
'get-window-with-predicate
)
2443 (defun get-lru-window (&optional all-frames dedicated not-selected
)
2444 "Return the least recently used window on frames specified by ALL-FRAMES.
2445 Return a full-width window if possible. A minibuffer window is
2446 never a candidate. A dedicated window is never a candidate
2447 unless DEDICATED is non-nil, so if all windows are dedicated, the
2448 value is nil. Avoid returning the selected window if possible.
2449 Optional argument NOT-SELECTED non-nil means never return the
2452 The following non-nil values of the optional argument ALL-FRAMES
2453 have special meanings:
2455 - t means consider all windows on all existing frames.
2457 - `visible' means consider all windows on all visible frames on
2458 the current terminal.
2460 - 0 (the number zero) means consider all windows on all visible
2461 and iconified frames on the current terminal.
2463 - A frame means consider all windows on that frame only.
2465 Any other value of ALL-FRAMES means consider all windows on the
2466 selected frame and no others."
2467 (let (best-window best-time second-best-window second-best-time time
)
2468 (dolist (window (window-list-1 nil
'nomini all-frames
))
2469 (when (and (or dedicated
(not (window-dedicated-p window
)))
2470 (or (not not-selected
) (not (eq window
(selected-window)))))
2471 (setq time
(window-use-time window
))
2472 (if (or (eq window
(selected-window))
2473 (not (window-full-width-p window
)))
2474 (when (or (not second-best-time
) (< time second-best-time
))
2475 (setq second-best-time time
)
2476 (setq second-best-window window
))
2477 (when (or (not best-time
) (< time best-time
))
2478 (setq best-time time
)
2479 (setq best-window window
)))))
2480 (or best-window second-best-window
)))
2482 (defun get-mru-window (&optional all-frames dedicated not-selected
)
2483 "Return the most recently used window on frames specified by ALL-FRAMES.
2484 A minibuffer window is never a candidate. A dedicated window is
2485 never a candidate unless DEDICATED is non-nil, so if all windows
2486 are dedicated, the value is nil. Optional argument NOT-SELECTED
2487 non-nil means never return the selected window.
2489 The following non-nil values of the optional argument ALL-FRAMES
2490 have special meanings:
2492 - t means consider all windows on all existing frames.
2494 - `visible' means consider all windows on all visible frames on
2495 the current terminal.
2497 - 0 (the number zero) means consider all windows on all visible
2498 and iconified frames on the current terminal.
2500 - A frame means consider all windows on that frame only.
2502 Any other value of ALL-FRAMES means consider all windows on the
2503 selected frame and no others."
2504 (let (best-window best-time time
)
2505 (dolist (window (window-list-1 nil
'nomini all-frames
))
2506 (setq time
(window-use-time window
))
2507 (when (and (or dedicated
(not (window-dedicated-p window
)))
2508 (or (not not-selected
) (not (eq window
(selected-window))))
2509 (or (not best-time
) (> time best-time
)))
2510 (setq best-time time
)
2511 (setq best-window window
)))
2514 (defun get-largest-window (&optional all-frames dedicated not-selected
)
2515 "Return the largest window on frames specified by ALL-FRAMES.
2516 A minibuffer window is never a candidate. A dedicated window is
2517 never a candidate unless DEDICATED is non-nil, so if all windows
2518 are dedicated, the value is nil. Optional argument NOT-SELECTED
2519 non-nil means never return the selected window.
2521 The following non-nil values of the optional argument ALL-FRAMES
2522 have special meanings:
2524 - t means consider all windows on all existing frames.
2526 - `visible' means consider all windows on all visible frames on
2527 the current terminal.
2529 - 0 (the number zero) means consider all windows on all visible
2530 and iconified frames on the current terminal.
2532 - A frame means consider all windows on that frame only.
2534 Any other value of ALL-FRAMES means consider all windows on the
2535 selected frame and no others."
2538 (dolist (window (window-list-1 nil
'nomini all-frames
))
2539 (when (and (or dedicated
(not (window-dedicated-p window
)))
2540 (or (not not-selected
) (not (eq window
(selected-window)))))
2541 (setq size
(* (window-pixel-height window
)
2542 (window-pixel-width window
)))
2543 (when (> size best-size
)
2544 (setq best-size size
)
2545 (setq best-window window
))))
2548 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames
)
2549 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2550 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2551 and defaults to the current buffer. If the selected window displays
2552 BUFFER-OR-NAME, it will be the first in the resulting list.
2554 MINIBUF t means include the minibuffer window even if the
2555 minibuffer is not active. MINIBUF nil or omitted means include
2556 the minibuffer window only if the minibuffer is active. Any
2557 other value means do not include the minibuffer window even if
2558 the minibuffer is active.
2560 ALL-FRAMES nil or omitted means consider all windows on the
2561 selected frame, plus the minibuffer window if specified by the
2562 MINIBUF argument. If the minibuffer counts, consider all windows
2563 on all frames that share that minibuffer too. The following
2564 non-nil values of ALL-FRAMES have special meanings:
2566 - t means consider all windows on all existing frames.
2568 - `visible' means consider all windows on all visible frames on
2569 the current terminal.
2571 - 0 (the number zero) means consider all windows on all visible
2572 and iconified frames on the current terminal.
2574 - A frame means consider all windows on that frame only.
2576 Anything else means consider all windows on the selected frame
2578 (let ((buffer (window-normalize-buffer buffer-or-name
))
2580 (dolist (window (window-list-1 (selected-window) minibuf all-frames
))
2581 (when (eq (window-buffer window
) buffer
)
2582 (setq windows
(cons window windows
))))
2583 (nreverse windows
)))
2585 (defun minibuffer-window-active-p (window)
2586 "Return t if WINDOW is the currently active minibuffer window."
2587 (and (window-live-p window
) (eq window
(active-minibuffer-window))))
2589 (defun count-windows (&optional minibuf
)
2590 "Return the number of live windows on the selected frame.
2591 The optional argument MINIBUF specifies whether the minibuffer
2592 window shall be counted. See `walk-windows' for the precise
2593 meaning of this argument."
2594 (length (window-list-1 nil minibuf
)))
2596 ;;; Resizing windows.
2597 (defun window--size-to-pixel (window size
&optional horizontal pixelwise round-maybe
)
2598 "For WINDOW convert SIZE lines to pixels.
2599 SIZE is supposed to specify a height of WINDOW in terms of text
2600 lines. The return value is the number of pixels specifying that
2603 WINDOW must be a valid window. Optional argument HORIZONTAL
2604 non-nil means convert SIZE columns to pixels.
2606 Optional argument PIXELWISE non-nil means SIZE already specifies
2607 pixels but may have to be adjusted to a multiple of the character
2608 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2609 means round to the nearest multiple of the character size of
2610 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2611 (setq window
(window-normalize-window window
))
2612 (let ((char-size (frame-char-size window horizontal
)))
2614 (if (and round-maybe
(not window-resize-pixelwise
))
2615 (* (round size char-size
) char-size
)
2617 (* size char-size
))))
2619 (defun window--pixel-to-total-1 (window horizontal char-size
)
2620 "Subroutine of `window--pixel-to-total'."
2621 (let ((child (window-child window
)))
2622 (if (window-combination-p window horizontal
)
2623 ;; In an iso-combination distribute sizes proportionally.
2624 (let ((remainder (window-new-total window
))
2625 size best-child rem best-rem
)
2626 ;; Initialize total sizes to each child's floor.
2628 (setq size
(max (/ (window-size child horizontal t
) char-size
) 1))
2629 (set-window-new-total child size
)
2630 (setq remainder
(- remainder size
))
2631 (setq child
(window-next-sibling child
)))
2632 ;; Distribute remainder.
2633 (while (> remainder
0)
2634 (setq child
(window-last-child window
))
2635 (setq best-child nil
)
2638 (when (and (<= (window-new-total child
)
2639 (/ (window-size child horizontal t
) char-size
))
2640 (> (setq rem
(%
(window-size child horizontal t
)
2643 (setq best-child child
)
2644 (setq best-rem rem
))
2645 (setq child
(window-prev-sibling child
)))
2646 ;; We MUST have a best-child here.
2647 (set-window-new-total best-child
1 t
)
2648 (setq remainder
(1- remainder
)))
2650 (setq child
(window-child window
))
2652 (window--pixel-to-total-1 child horizontal char-size
)
2653 (setq child
(window-next-sibling child
))))
2654 ;; In an ortho-combination assign new sizes directly.
2655 (let ((size (window-new-total window
)))
2657 (set-window-new-total child size
)
2658 (window--pixel-to-total-1 child horizontal char-size
)
2659 (setq child
(window-next-sibling child
)))))))
2661 (defun window--pixel-to-total (&optional frame horizontal
)
2662 "On FRAME assign new total window heights from pixel heights.
2663 FRAME must be a live frame and defaults to the selected frame.
2665 Optional argument HORIZONTAL non-nil means assign new total
2666 window widths from pixel widths."
2667 (setq frame
(window-normalize-frame frame
))
2668 (let* ((char-size (frame-char-size frame horizontal
))
2669 (root (frame-root-window frame
))
2670 (root-size (window-size root horizontal t
))
2671 ;; We have to care about the minibuffer window only if it
2672 ;; appears together with the root window on this frame.
2673 (mini (let ((mini (minibuffer-window frame
)))
2674 (and (eq (window-frame mini
) frame
)
2675 (not (eq mini root
)) mini
)))
2676 (mini-size (and mini
(window-size mini horizontal t
))))
2677 ;; We round the line/column sizes of windows here to the nearest
2678 ;; integer. In some cases this can make windows appear _larger_
2679 ;; than the containing frame (line/column-wise) because the latter's
2680 ;; sizes are not (yet) rounded. We might eventually fix that.
2681 (if (and mini
(not horizontal
))
2683 (set-window-new-total root
(max (/ root-size char-size
) 1))
2684 (set-window-new-total mini
(max (/ mini-size char-size
) 1))
2685 (setq lines
(- (round (+ root-size mini-size
) char-size
)
2686 (+ (window-new-total root
) (window-new-total mini
))))
2688 (if (>= (% root-size
(window-new-total root
))
2689 (% mini-size
(window-new-total mini
)))
2690 (set-window-new-total root
1 t
)
2691 (set-window-new-total mini
1 t
))
2692 (setq lines
(1- lines
))))
2693 (set-window-new-total root
(round root-size char-size
))
2695 ;; This is taken in the horizontal case only.
2696 (set-window-new-total mini
(round mini-size char-size
))))
2697 (unless (window-buffer root
)
2698 (window--pixel-to-total-1 root horizontal char-size
))
2699 ;; Apply the new sizes.
2700 (window-resize-apply-total frame horizontal
)))
2702 (defun window--resize-reset (&optional frame horizontal
)
2703 "Reset resize values for all windows on FRAME.
2704 FRAME defaults to the selected frame.
2706 This function stores the current value of `window-size' applied
2707 with argument HORIZONTAL in the new total size of all windows on
2708 FRAME. It also resets the new normal size of each of these
2710 (window--resize-reset-1
2711 (frame-root-window (window-normalize-frame frame
)) horizontal
))
2713 (defun window--resize-reset-1 (window horizontal
)
2714 "Internal function of `window--resize-reset'."
2715 ;; Register old size in the new total size.
2716 (set-window-new-pixel window
(window-size window horizontal t
))
2717 (set-window-new-total window
(window-size window horizontal
))
2718 ;; Reset new normal size.
2719 (set-window-new-normal window
)
2720 (when (window-child window
)
2721 (window--resize-reset-1 (window-child window
) horizontal
))
2722 (when (window-right window
)
2723 (window--resize-reset-1 (window-right window
) horizontal
)))
2725 (defun window--resize-mini-window (window delta
)
2726 "Resize minibuffer window WINDOW by DELTA pixels.
2727 If WINDOW cannot be resized by DELTA pixels make it as large (or
2728 as small) as possible, but don't signal an error."
2729 (when (window-minibuffer-p window
)
2730 (let* ((frame (window-frame window
))
2731 (root (frame-root-window frame
))
2732 (height (window-pixel-height window
))
2734 (- (window-pixel-height root
)
2735 (window-min-size root nil nil t
))))
2738 ((<= (+ height delta
) 0)
2739 (setq delta
(- (frame-char-height (window-frame window
)) height
)))
2740 ((> delta min-delta
)
2741 (setq delta min-delta
)))
2743 (unless (zerop delta
)
2745 (window--resize-reset frame
)
2746 ;; Ideally we should be able to resize just the last child of root
2747 ;; here. See the comment in `resize-root-window-vertically' for
2748 ;; why we do not do that.
2749 (window--resize-this-window root
(- delta
) nil nil t
)
2750 (set-window-new-pixel window
(+ height delta
))
2751 ;; The following routine catches the case where we want to resize
2752 ;; a minibuffer-only frame.
2753 (when (resize-mini-window-internal window
)
2754 (window--pixel-to-total frame
)
2755 (run-window-configuration-change-hook frame
))))))
2757 (defun window--resize-apply-p (frame &optional horizontal
)
2758 "Return t when a window on FRAME shall be resized vertically.
2759 Optional argument HORIZONTAL non-nil means return t when a window
2760 shall be resized horizontally."
2764 (unless (= (window-new-pixel window
)
2765 (window-size window horizontal t
))
2770 (defun window-resize (window delta
&optional horizontal ignore pixelwise
)
2771 "Resize WINDOW vertically by DELTA lines.
2772 WINDOW can be an arbitrary window and defaults to the selected
2773 one. An attempt to resize the root window of a frame will raise
2776 DELTA a positive number means WINDOW shall be enlarged by DELTA
2777 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2780 Optional argument HORIZONTAL non-nil means resize WINDOW
2781 horizontally by DELTA columns. In this case a positive DELTA
2782 means enlarge WINDOW by DELTA columns. DELTA negative means
2783 WINDOW shall be shrunk by -DELTA columns.
2785 Optional argument IGNORE, if non-nil, means to ignore restraints
2786 induced by fixed size windows or the values of the variables
2787 `window-min-height' and `window-min-width'. The following values
2788 have special meanings: `safe' means that in addition live windows
2789 are allowed to get as small as `window-safe-min-height' lines and
2790 `window-safe-min-width' columns. `preserved' means to ignore
2791 only restrictions induced by `window-preserve-size'. If IGNORE
2792 is a window, then ignore restrictions for that window only.
2794 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2797 This function resizes other windows proportionally and never
2798 deletes any windows. If you want to move only the low (right)
2799 edge of WINDOW consider using `adjust-window-trailing-edge'
2801 (setq window
(window-normalize-window window
))
2802 (let* ((frame (window-frame window
))
2803 (minibuffer-window (minibuffer-window frame
))
2805 (setq delta
(window--size-to-pixel
2806 window delta horizontal pixelwise t
))
2808 ((eq window
(frame-root-window frame
))
2809 (error "Cannot resize the root window of a frame"))
2810 ((window-minibuffer-p window
)
2812 (error "Cannot resize minibuffer window horizontally")
2813 (window--resize-mini-window window delta
)))
2814 ((and (not horizontal
)
2815 (window-full-height-p window
)
2816 (eq (window-frame minibuffer-window
) frame
)
2817 (or (not resize-mini-windows
)
2818 (eq minibuffer-window
(active-minibuffer-window))))
2819 ;; If WINDOW is full height and either `resize-mini-windows' is
2820 ;; nil or the minibuffer window is active, resize the minibuffer
2822 (window--resize-mini-window minibuffer-window
(- delta
)))
2823 ((or (window--resizable-p
2824 window delta horizontal ignore nil nil nil t
)
2826 (setq ignore
'preserved
)
2827 (window--resizable-p
2828 window delta horizontal ignore nil nil nil t
)))
2829 (window--resize-reset frame horizontal
)
2830 (window--resize-this-window window delta horizontal ignore t
)
2831 (if (and (not (eq window-combination-resize t
))
2832 (window-combined-p window horizontal
)
2833 (setq sibling
(or (window-right window
) (window-left window
)))
2835 sibling
(- delta
) horizontal ignore t
))
2836 ;; If window-combination-resize is nil, WINDOW is part of an
2837 ;; iso-combination, and WINDOW's neighboring right or left
2838 ;; sibling can be resized as requested, resize that sibling.
2841 (window-size (window-parent window
) horizontal t
))))
2842 (window--resize-this-window sibling
(- delta
) horizontal nil t
)
2843 (set-window-new-normal
2844 window
(+ (window-normal-size window horizontal
)
2846 (set-window-new-normal
2847 sibling
(- (window-normal-size sibling horizontal
)
2849 ;; Otherwise, resize all other windows in the same combination.
2850 (window--resize-siblings window delta horizontal ignore
))
2851 (when (window--resize-apply-p frame horizontal
)
2852 (if (window-resize-apply frame horizontal
)
2854 (window--pixel-to-total frame horizontal
)
2855 (run-window-configuration-change-hook frame
))
2856 (error "Failed to apply resizing %s" window
))))
2858 (error "Cannot resize window %s" window
)))))
2860 (defun window-resize-no-error (window delta
&optional horizontal ignore pixelwise
)
2861 "Resize WINDOW vertically if it is resizable by DELTA lines.
2862 This function is like `window-resize' but does not signal an
2863 error when WINDOW cannot be resized. For the meaning of the
2864 optional arguments see the documentation of `window-resize'."
2865 (when (window--resizable-p
2866 window delta horizontal ignore nil nil nil pixelwise
)
2867 (window-resize window delta horizontal ignore pixelwise
)))
2869 (defun window--resize-child-windows-skip-p (window)
2870 "Return non-nil if WINDOW shall be skipped by resizing routines."
2871 (memq (window-new-normal window
) '(ignore stuck skip
)))
2873 (defun window--resize-child-windows-normal (parent horizontal window this-delta
&optional trail other-delta
)
2874 "Recursively set new normal height of child windows of window PARENT.
2875 HORIZONTAL non-nil means set the new normal width of these
2876 windows. WINDOW specifies a child window of PARENT that has been
2877 resized by THIS-DELTA lines (columns).
2879 Optional argument TRAIL either `before' or `after' means set values
2880 only for windows before or after WINDOW. Optional argument
2881 OTHER-DELTA, a number, specifies that this many lines (columns)
2882 have been obtained from (or returned to) an ancestor window of
2883 PARENT in order to resize WINDOW."
2884 (let* ((delta-normal
2885 (if (and (= (- this-delta
)
2886 (window-size window horizontal t
))
2887 (zerop other-delta
))
2888 ;; When WINDOW gets deleted and we can return its entire
2889 ;; space to its siblings, use WINDOW's normal size as the
2891 (- (window-normal-size window horizontal
))
2892 ;; In any other case calculate the normal delta from the
2893 ;; relation of THIS-DELTA to the total size of PARENT.
2894 (/ (float this-delta
)
2895 (window-size parent horizontal t
))))
2896 (sub (window-child parent
))
2898 (skip (eq trail
'after
)))
2900 ;; Set parent-normal to the sum of the normal sizes of all child
2901 ;; windows of PARENT that shall be resized, excluding only WINDOW
2902 ;; and any windows specified by the optional TRAIL argument.
2906 (setq skip
(eq trail
'before
)))
2910 (+ parent-normal
(window-normal-size sub horizontal
)))))
2911 (setq sub
(window-right sub
)))
2913 ;; Set the new normal size of all child windows of PARENT from what
2914 ;; they should have contributed for recovering THIS-DELTA lines
2916 (setq sub
(window-child parent
))
2917 (setq skip
(eq trail
'after
))
2921 (setq skip
(eq trail
'before
)))
2924 (let ((old-normal (window-normal-size sub horizontal
)))
2925 (set-window-new-normal
2926 sub
(min 1.0 ; Don't get larger than 1.
2928 (* (/ old-normal parent-normal
)
2930 ;; Don't drop below 0.
2932 (setq sub
(window-right sub
)))
2934 (when (numberp other-delta
)
2935 ;; Set the new normal size of windows from what they should have
2936 ;; contributed for recovering OTHER-DELTA lines (columns).
2937 (setq delta-normal
(/ (float (window-size parent horizontal t
))
2938 (+ (window-size parent horizontal t
)
2940 (setq sub
(window-child parent
))
2941 (setq skip
(eq trail
'after
))
2945 (setq skip
(eq trail
'before
)))
2948 (set-window-new-normal
2949 sub
(min 1.0 ; Don't get larger than 1.
2950 (max (* (window-new-normal sub
) delta-normal
)
2951 ;; Don't drop below 0.
2953 (setq sub
(window-right sub
))))
2955 ;; Set the new normal size of WINDOW to what is left by the sum of
2956 ;; the normal sizes of its siblings.
2957 (set-window-new-normal
2960 (setq sub
(window-child parent
))
2964 ((not (numberp (window-new-normal sub
)))
2965 (setq sum
(+ sum
(window-normal-size sub horizontal
))))
2967 (setq sum
(+ sum
(window-new-normal sub
)))))
2968 (setq sub
(window-right sub
)))
2969 ;; Don't get larger than 1 or smaller than 0.
2970 (min 1.0 (max (- 1.0 sum
) 0.0))))))
2972 (defun window--resize-child-windows (parent delta
&optional horizontal window ignore trail edge char-size
)
2973 "Resize child windows of window PARENT vertically by DELTA pixels.
2974 PARENT must be a vertically combined internal window.
2976 Optional argument HORIZONTAL non-nil means resize child windows
2977 of PARENT horizontally by DELTA pixels. In this case PARENT must
2978 be a horizontally combined internal window.
2980 WINDOW, if specified, must denote a child window of PARENT that
2981 is resized by DELTA pixels.
2983 The optional argument IGNORE has the same meaning as for
2986 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2987 of windows that shall be resized. If TRAIL equals `before',
2988 resize only windows on the left or above EDGE. If TRAIL equals
2989 `after', resize only windows on the right or below EDGE. Also,
2990 preferably only resize windows adjacent to EDGE.
2992 If the optional argument CHAR-SIZE is a positive integer, it specifies
2993 the number of pixels by which windows are incrementally resized.
2994 If CHAR-SIZE is nil, this means to use the value of
2995 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2997 Return the symbol `normalized' if new normal sizes have been
2998 already set by this routine."
2999 (let* ((first (window-child parent
))
3000 (last (window-last-child parent
))
3001 (parent-total (+ (window-size parent horizontal t
)
3003 (char-size (or char-size
3004 (and window-resize-pixelwise
1)
3005 (frame-char-size window horizontal
)))
3006 sub best-window best-value best-delta
)
3008 (if (and edge
(memq trail
'(before after
))
3011 (while (and (window-right sub
)
3012 (or (and (eq trail
'before
)
3013 (not (window--resize-child-windows-skip-p
3014 (window-right sub
))))
3015 (and (eq trail
'after
)
3016 (window--resize-child-windows-skip-p sub
))))
3017 (setq sub
(window-right sub
)))
3020 (if (eq trail
'before
)
3021 (= (+ (window-pixel-left sub
) (window-pixel-width sub
))
3023 (= (window-pixel-left sub
) edge
))
3024 (if (eq trail
'before
)
3025 (= (+ (window-pixel-top sub
) (window-pixel-height sub
))
3027 (= (window-pixel-top sub
) edge
)))
3028 (window-sizable-p sub delta horizontal ignore t
))
3029 ;; Resize only windows adjacent to EDGE.
3031 (window--resize-this-window
3032 sub delta horizontal ignore t trail edge
)
3033 (if (and window
(eq (window-parent sub
) parent
))
3035 ;; Assign new normal sizes.
3036 (set-window-new-normal
3037 sub
(/ (float (window-new-pixel sub
)) parent-total
))
3038 (set-window-new-normal
3039 window
(- (window-normal-size window horizontal
)
3040 (- (window-new-normal sub
)
3041 (window-normal-size sub horizontal
)))))
3042 (window--resize-child-windows-normal
3043 parent horizontal sub
0 trail delta
))
3044 ;; Return 'normalized to notify `window--resize-siblings' that
3045 ;; normal sizes have been already set.
3047 ;; Resize all windows proportionally.
3051 ((or (window--resize-child-windows-skip-p sub
)
3052 ;; Ignore windows to skip and fixed-size child windows -
3053 ;; in the latter case make it a window to skip.
3055 (window-size-fixed-p sub horizontal ignore
)
3056 (set-window-new-normal sub
'ignore
))))
3058 ;; When shrinking store the number of lines/cols we can get
3059 ;; from this window here together with the total/normal size
3061 (set-window-new-normal
3064 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
3065 (window-min-delta sub horizontal ignore trail t nil t
)
3066 (- (/ (float (window-size sub horizontal t
))
3068 (window-normal-size sub horizontal
)))))
3070 ;; When enlarging store the total/normal size factor only
3071 (set-window-new-normal
3073 (- (/ (float (window-size sub horizontal t
))
3075 (window-normal-size sub horizontal
)))))
3077 (setq sub
(window-left sub
)))
3081 ;; Shrink windows by delta.
3082 (setq best-window t
)
3083 (while (and best-window
(not (zerop delta
)))
3085 (setq best-window nil
)
3086 (setq best-value most-negative-fixnum
)
3088 (when (and (consp (window-new-normal sub
))
3089 (not (<= (car (window-new-normal sub
)) 0))
3090 (> (cdr (window-new-normal sub
)) best-value
))
3091 (setq best-window sub
)
3092 (setq best-value
(cdr (window-new-normal sub
))))
3094 (setq sub
(window-left sub
)))
3097 (setq best-delta
(min (car (window-new-normal best-window
))
3098 char-size
(- delta
)))
3099 (setq delta
(+ delta best-delta
))
3100 (set-window-new-pixel best-window
(- best-delta
) t
)
3101 (set-window-new-normal
3103 (if (= (car (window-new-normal best-window
)) best-delta
)
3104 'skip
; We can't shrink best-window any further.
3105 (cons (- (car (window-new-normal best-window
)) best-delta
)
3106 (- (/ (float (window-new-pixel best-window
))
3108 (window-normal-size best-window horizontal
))))))))
3110 ;; Enlarge windows by delta.
3111 (setq best-window t
)
3112 (while (and best-window
(not (zerop delta
)))
3114 (setq best-window nil
)
3115 (setq best-value most-positive-fixnum
)
3117 (when (and (numberp (window-new-normal sub
))
3118 (< (window-new-normal sub
) best-value
))
3119 (setq best-window sub
)
3120 (setq best-value
(window-new-normal sub
)))
3122 (setq sub
(window-left sub
)))
3125 (setq best-delta
(min delta char-size
))
3126 (setq delta
(- delta best-delta
))
3127 (set-window-new-pixel best-window best-delta t
)
3128 (set-window-new-normal
3130 (- (/ (float (window-new-pixel best-window
))
3132 (window-normal-size best-window horizontal
)))))))
3137 (when (or (consp (window-new-normal sub
))
3138 (numberp (window-new-normal sub
)))
3139 ;; Reset new normal size fields so `window-resize-apply'
3140 ;; won't use them to apply new sizes.
3141 (set-window-new-normal sub
))
3143 (unless (eq (window-new-normal sub
) 'ignore
)
3144 ;; Resize this window's child windows (back-engineering
3145 ;; delta from sub's old and new total sizes).
3146 (let ((delta (- (window-new-pixel sub
)
3147 (window-size sub horizontal t
))))
3148 (unless (and (zerop delta
) (not trail
))
3149 ;; For the TRAIL non-nil case we have to resize SUB
3150 ;; recursively even if it's size does not change.
3151 (window--resize-this-window
3152 sub delta horizontal ignore nil trail edge
))))
3153 (setq sub
(window-left sub
)))))))
3155 (defun window--resize-siblings (window delta
&optional horizontal ignore trail edge char-size
)
3156 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
3157 Optional argument HORIZONTAL non-nil means resize other windows
3158 when WINDOW is resized horizontally by DELTA pixels. WINDOW
3159 itself is not resized by this function.
3161 The optional argument IGNORE has the same meaning as for
3164 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3165 of windows that shall be resized. If TRAIL equals `before',
3166 resize only windows on the left or above EDGE. If TRAIL equals
3167 `after', resize only windows on the right or below EDGE. Also,
3168 preferably only resize windows adjacent to EDGE."
3169 (when (window-parent window
)
3170 (let* ((parent (window-parent window
))
3171 (sub (window-child parent
)))
3172 (if (window-combined-p sub horizontal
)
3173 ;; In an iso-combination try to extract DELTA from WINDOW's
3175 (let ((skip (eq trail
'after
))
3176 this-delta other-delta
)
3177 ;; Decide which windows shall be left alone.
3181 ;; Make sure WINDOW is left alone when
3182 ;; resizing its siblings.
3183 (set-window-new-normal sub
'ignore
)
3184 (setq skip
(eq trail
'before
)))
3186 ;; Make sure this sibling is left alone when
3187 ;; resizing its siblings.
3188 (set-window-new-normal sub
'ignore
))
3189 ((not (window-size-fixed-p sub horizontal ignore
))
3190 ;; Set this-delta to t to signal that we found a sibling
3191 ;; of WINDOW whose size is not fixed.
3192 (setq this-delta t
)))
3194 (setq sub
(window-right sub
)))
3196 ;; Set this-delta to what we can get from WINDOW's siblings.
3197 (if (= (- delta
) (window-size window horizontal t
))
3198 ;; A deletion, presumably. We must handle this case
3199 ;; specially since `window--resizable' can't be used.
3201 ;; There's at least one resizable sibling we can
3202 ;; give WINDOW's size to.
3203 (setq this-delta delta
)
3204 ;; No resizable sibling exists.
3205 (setq this-delta
0))
3206 ;; Any other form of resizing.
3209 window delta horizontal ignore trail t nil t
)))
3211 ;; Set other-delta to what we still have to get from
3212 ;; ancestor windows of parent.
3213 (setq other-delta
(- delta this-delta
))
3214 (unless (zerop other-delta
)
3215 ;; Unless we got everything from WINDOW's siblings, PARENT
3216 ;; must be resized by other-delta lines or columns.
3217 (set-window-new-pixel parent other-delta
'add
))
3219 (if (zerop this-delta
)
3220 ;; We haven't got anything from WINDOW's siblings but we
3221 ;; must update the normal sizes to respect other-delta.
3222 (window--resize-child-windows-normal
3223 parent horizontal window this-delta trail other-delta
)
3224 ;; We did get something from WINDOW's siblings which means
3225 ;; we have to resize their child windows.
3226 (unless (eq (window--resize-child-windows
3227 parent
(- this-delta
) horizontal
3228 window ignore trail edge char-size
)
3229 ;; If `window--resize-child-windows' returns
3230 ;; 'normalized, this means it has set the
3231 ;; normal sizes already.
3233 ;; Set the normal sizes.
3234 (window--resize-child-windows-normal
3235 parent horizontal window this-delta trail other-delta
))
3236 ;; Set DELTA to what we still have to get from ancestor
3238 (setq delta other-delta
)))
3240 ;; In an ortho-combination all siblings of WINDOW must be
3241 ;; resized by DELTA.
3242 (set-window-new-pixel parent delta
'add
)
3244 (unless (eq sub window
)
3245 (window--resize-this-window
3246 sub delta horizontal ignore t
))
3247 (setq sub
(window-right sub
))))
3249 (unless (zerop delta
)
3251 (window--resize-siblings
3252 parent delta horizontal ignore trail edge char-size
)))))
3254 (defun window--resize-this-window (window delta
&optional horizontal ignore add trail edge char-size
)
3255 "Resize WINDOW vertically by DELTA pixels.
3256 Optional argument HORIZONTAL non-nil means resize WINDOW
3257 horizontally by DELTA pixels.
3259 The optional argument IGNORE has the same meaning as for
3260 `window-resizable'. Optional argument ADD non-nil means add
3261 DELTA to the new total size of WINDOW.
3263 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3264 of windows that shall be resized. If TRAIL equals `before',
3265 resize only windows on the left or above EDGE. If TRAIL equals
3266 `after', resize only windows on the right or below EDGE. Also,
3267 preferably only resize windows adjacent to EDGE.
3269 If the optional argument CHAR-SIZE is a positive integer, it specifies
3270 the number of pixels by which windows are incrementally resized.
3271 If CHAR-SIZE is nil, this means to use the value of
3272 `frame-char-height' or `frame-char-width' of WINDOW's frame.
3274 This function recursively resizes WINDOW's child windows to fit the
3275 new size. Make sure that WINDOW is `window--resizable' before
3276 calling this function. Note that this function does not resize
3277 siblings of WINDOW or WINDOW's parent window. You have to
3278 eventually call `window-resize-apply' in order to make resizing
3279 actually take effect."
3281 ;; Add DELTA to the new total size of WINDOW.
3282 (set-window-new-pixel window delta t
))
3284 (let ((sub (window-child window
)))
3287 ((window-combined-p sub horizontal
)
3288 ;; In an iso-combination resize child windows according to their
3290 (window--resize-child-windows
3291 window delta horizontal nil ignore trail edge char-size
))
3292 ;; In an ortho-combination resize each child window by DELTA.
3295 (window--resize-this-window
3296 sub delta horizontal ignore t trail edge char-size
)
3297 (setq sub
(window-right sub
)))))))
3299 (defun window--resize-root-window (window delta horizontal ignore pixelwise
)
3300 "Resize root window WINDOW vertically by DELTA lines.
3301 HORIZONTAL non-nil means resize root window WINDOW horizontally
3304 IGNORE non-nil means ignore any restrictions imposed by fixed
3305 size windows, `window-min-height' or `window-min-width' settings.
3307 This function is only called by the frame resizing routines. It
3308 resizes windows proportionally and never deletes any windows."
3309 (when (and (windowp window
) (numberp delta
))
3313 (window--size-to-pixel window delta horizontal
))))
3314 (when (window-sizable-p window pixel-delta horizontal ignore t
)
3315 (window--resize-reset (window-frame window
) horizontal
)
3316 (window--resize-this-window
3317 window pixel-delta horizontal ignore t
)))))
3319 (defun window--resize-root-window-vertically (window delta pixelwise
)
3320 "Resize root window WINDOW vertically by DELTA lines.
3321 If DELTA is less than zero and we can't shrink WINDOW by DELTA
3322 lines, shrink it as much as possible. If DELTA is greater than
3323 zero, this function can resize fixed-size windows in order to
3324 recover the necessary lines. Return the number of lines that
3327 Third argument PIXELWISE non-nil means to interpret DELTA as
3328 pixels and return the number of pixels that were recovered.
3330 This function is called by the minibuffer window resizing
3332 (let* ((frame (window-frame window
))
3338 (* (frame-char-height frame
) delta
))
3342 ((zerop pixel-delta
))
3344 (setq pixel-delta
(window-sizable window pixel-delta nil nil pixelwise
))
3345 (window--resize-reset frame
)
3346 ;; When shrinking the root window, emulate an edge drag in order
3347 ;; to not resize other windows if we can avoid it (Bug#12419).
3348 (window--resize-this-window
3349 window pixel-delta nil ignore t
'before
3350 (+ (window-pixel-top window
) (window-pixel-height window
)))
3351 ;; Don't record new normal sizes to make sure that shrinking back
3352 ;; proportionally works as intended.
3354 (lambda (window) (set-window-new-normal window
'ignore
)) frame t
))
3356 (window--resize-reset frame
)
3357 (unless (window-sizable window pixel-delta nil nil pixelwise
)
3359 ;; When growing the root window, resize proportionally. This
3360 ;; should give windows back their original sizes (hopefully).
3361 (window--resize-this-window
3362 window pixel-delta nil ignore t
)))
3363 ;; Return the possibly adjusted DELTA.
3366 (/ pixel-delta
(frame-char-height frame
)))))
3368 (defun window--sanitize-window-sizes (horizontal)
3369 "Assert that all windows on selected frame are large enough.
3370 If necessary and possible, make sure that every window on frame
3371 FRAME has its minimum height. Optional argument HORIZONTAL
3372 non-nil means to make sure that every window on frame FRAME has
3373 its minimum width. The minimum height/width of a window is the
3374 respective value returned by `window-min-size' for that window.
3376 Return t if all windows were resized appropriately. Return nil
3377 if at least one window could not be resized as requested, which
3378 may happen when the FRAME is not large enough to accommodate it."
3382 (let ((delta (- (window-min-size window horizontal nil t
)
3383 (window-size window horizontal t
))))
3385 (if (window-resizable-p window delta horizontal nil t
)
3386 (window-resize window delta horizontal nil t
)
3387 (setq value nil
))))))
3390 (defun adjust-window-trailing-edge (window delta
&optional horizontal pixelwise
)
3391 "Move WINDOW's bottom edge by DELTA lines.
3392 Optional argument HORIZONTAL non-nil means move WINDOW's right
3393 edge by DELTA columns. WINDOW must be a valid window and
3394 defaults to the selected one.
3396 Optional argument PIXELWISE non-nil means interpret DELTA as
3399 If DELTA is greater than zero, move the edge downwards or to the
3400 right. If DELTA is less than zero, move the edge upwards or to
3401 the left. If the edge can't be moved by DELTA lines or columns,
3402 move it as far as possible in the desired direction."
3403 (setq window
(window-normalize-window window
))
3404 (let* ((frame (window-frame window
))
3405 (minibuffer-window (minibuffer-window frame
))
3407 left first-left first-right this-delta min-delta max-delta ignore
)
3411 (setq delta
(* delta
(frame-char-size window horizontal
))))
3413 ;; Find the edge we want to move.
3414 (while (and (or (not (window-combined-p right horizontal
))
3415 (not (window-right right
)))
3416 (setq right
(window-parent right
))))
3418 ((and (not right
) (not horizontal
)
3419 ;; Resize the minibuffer window if it's on the same frame as
3420 ;; and immediately below WINDOW and it's either active or
3421 ;; `resize-mini-windows' is nil.
3422 (eq (window-frame minibuffer-window
) frame
)
3423 (= (nth 1 (window-pixel-edges minibuffer-window
))
3424 (nth 3 (window-pixel-edges window
)))
3425 (or (not resize-mini-windows
)
3426 (eq minibuffer-window
(active-minibuffer-window))))
3427 (window--resize-mini-window minibuffer-window
(- delta
)))
3428 ((or (not (setq left right
)) (not (setq right
(window-right right
))))
3430 (user-error "No window on the right of this one")
3431 (user-error "No window below this one")))
3433 ;; Set LEFT to the first resizable window on the left. This step is
3434 ;; needed to handle fixed-size windows.
3435 (setq first-left left
)
3437 (or (window-size-fixed-p left horizontal
)
3439 (<= (window-size left horizontal t
)
3440 (window-min-size left horizontal nil t
)))))
3442 (or (window-left left
)
3444 (while (and (setq left
(window-parent left
))
3445 (not (window-combined-p left horizontal
))))
3446 (window-left left
)))))
3448 ;; We have to resize a size-preserved window. Start again with
3449 ;; the window initially on the left.
3450 (setq ignore
'preserved
)
3451 (setq left first-left
)
3453 (or (window-size-fixed-p left horizontal
'preserved
)
3455 (<= (window-size left horizontal t
)
3457 left horizontal
'preserved t
)))))
3459 (or (window-left left
)
3461 (while (and (setq left
(window-parent left
))
3462 (not (window-combined-p left horizontal
))))
3463 (window-left left
)))))
3467 (user-error "No resizable window on the left of this one")
3468 (user-error "No resizable window above this one"))))
3470 ;; Set RIGHT to the first resizable window on the right. This step
3471 ;; is needed to handle fixed-size windows.
3472 (setq first-right right
)
3474 (or (window-size-fixed-p right horizontal
)
3476 (<= (window-size right horizontal t
)
3478 right horizontal
'preserved t
)))))
3480 (or (window-right right
)
3482 (while (and (setq right
(window-parent right
))
3483 (not (window-combined-p right horizontal
))))
3484 (window-right right
)))))
3486 ;; We have to resize a size-preserved window. Start again with
3487 ;; the window initially on the right.
3488 (setq ignore
'preserved
)
3489 (setq right first-right
)
3491 (or (window-size-fixed-p right horizontal
'preserved
)
3493 (<= (window-size right horizontal t
)
3495 right horizontal
'preserved t
)))))
3497 (or (window-right right
)
3499 (while (and (setq right
(window-parent right
))
3500 (not (window-combined-p right horizontal
))))
3501 (window-right right
)))))
3504 (user-error "No resizable window on the right of this one")
3505 (user-error "No resizable window below this one"))))
3507 ;; LEFT and RIGHT (which might be both internal windows) are now the
3508 ;; two windows we want to resize.
3512 (window--max-delta-1
3513 left
0 horizontal ignore
'after nil pixelwise
))
3515 (window--min-delta-1
3516 right
(- delta
) horizontal ignore
'before nil pixelwise
))
3517 (when (or (< max-delta delta
) (> min-delta
(- delta
)))
3518 ;; We can't get the whole DELTA - move as far as possible.
3519 (setq delta
(min max-delta
(- min-delta
))))
3520 (unless (zerop delta
)
3522 (window--resize-reset frame horizontal
)
3523 ;; Try to enlarge LEFT first.
3526 left delta horizontal ignore
'after nil nil pixelwise
))
3527 (unless (zerop this-delta
)
3528 (window--resize-this-window
3529 left this-delta horizontal ignore t
'before
3531 (+ (window-pixel-left left
) (window-pixel-width left
))
3532 (+ (window-pixel-top left
) (window-pixel-height left
)))))
3533 ;; Shrink windows on right of LEFT.
3534 (window--resize-siblings
3535 left delta horizontal ignore
'after
3537 (window-pixel-left right
)
3538 (window-pixel-top right
)))))
3541 (window--max-delta-1
3542 right
0 horizontal ignore
'before nil pixelwise
))
3544 (window--min-delta-1
3545 left delta horizontal ignore
'after nil pixelwise
))
3546 (when (or (< max-delta
(- delta
)) (> min-delta delta
))
3547 ;; We can't get the whole DELTA - move as far as possible.
3548 (setq delta
(max (- max-delta
) min-delta
)))
3549 (unless (zerop delta
)
3551 (window--resize-reset frame horizontal
)
3552 ;; Try to enlarge RIGHT.
3555 right
(- delta
) horizontal ignore
'before nil nil pixelwise
))
3556 (unless (zerop this-delta
)
3557 (window--resize-this-window
3558 right this-delta horizontal ignore t
'after
3560 (window-pixel-left right
)
3561 (window-pixel-top right
))))
3562 ;; Shrink windows on left of RIGHT.
3563 (window--resize-siblings
3564 right
(- delta
) horizontal ignore
'before
3566 (+ (window-pixel-left left
) (window-pixel-width left
))
3567 (+ (window-pixel-top left
) (window-pixel-height left
)))))))
3568 (unless (zerop delta
)
3569 ;; Don't report an error in the standard case.
3570 (when (window--resize-apply-p frame horizontal
)
3571 (if (window-resize-apply frame horizontal
)
3573 (window--pixel-to-total frame horizontal
)
3574 (run-window-configuration-change-hook frame
))
3575 ;; But do report an error if applying the changes fails.
3576 (error "Failed adjusting window %s" window
))))))))
3578 (defun enlarge-window (delta &optional horizontal
)
3579 "Make the selected window DELTA lines taller.
3580 Interactively, if no argument is given, make the selected window
3581 one line taller. If optional argument HORIZONTAL is non-nil,
3582 make selected window wider by DELTA columns. If DELTA is
3583 negative, shrink selected window by -DELTA lines or columns."
3585 (let ((minibuffer-window (minibuffer-window)))
3586 (when (window-preserved-size nil horizontal
)
3587 (window-preserve-size nil horizontal
))
3590 ((window-size-fixed-p nil horizontal
)
3591 (user-error "Selected window has fixed size"))
3592 ((window-minibuffer-p)
3594 (user-error "Cannot resize minibuffer window horizontally")
3595 (window--resize-mini-window
3596 (selected-window) (* delta
(frame-char-height)))))
3597 ((and (not horizontal
)
3598 (window-full-height-p)
3599 (eq (window-frame minibuffer-window
) (selected-frame))
3600 (not resize-mini-windows
))
3601 ;; If the selected window is full height and `resize-mini-windows'
3602 ;; is nil, resize the minibuffer window.
3603 (window--resize-mini-window
3604 minibuffer-window
(* (- delta
) (frame-char-height))))
3605 ((window--resizable-p nil delta horizontal
)
3606 (window-resize nil delta horizontal
))
3607 ((window--resizable-p nil delta horizontal
'preserved
)
3608 (window-resize nil delta horizontal
'preserved
))
3610 (if horizontal
'enlarge-window-horizontally
'enlarge-window
))
3611 ;; For backward compatibility don't signal an error unless this
3612 ;; command is `enlarge-window(-horizontally)'.
3613 (user-error "Cannot enlarge selected window"))
3617 (window-max-delta nil horizontal
)
3618 (- (window-min-delta nil horizontal
)))
3621 (defun shrink-window (delta &optional horizontal
)
3622 "Make the selected window DELTA lines smaller.
3623 Interactively, if no argument is given, make the selected window
3624 one line smaller. If optional argument HORIZONTAL is non-nil,
3625 make selected window narrower by DELTA columns. If DELTA is
3626 negative, enlarge selected window by -DELTA lines or columns."
3628 (let ((minibuffer-window (minibuffer-window)))
3629 (when (window-preserved-size nil horizontal
)
3630 (window-preserve-size nil horizontal
))
3633 ((window-size-fixed-p nil horizontal
)
3634 (user-error "Selected window has fixed size"))
3635 ((window-minibuffer-p)
3637 (user-error "Cannot resize minibuffer window horizontally")
3638 (window--resize-mini-window
3639 (selected-window) (* (- delta
) (frame-char-height)))))
3640 ((and (not horizontal
)
3641 (window-full-height-p)
3642 (eq (window-frame minibuffer-window
) (selected-frame))
3643 (not resize-mini-windows
))
3644 ;; If the selected window is full height and `resize-mini-windows'
3645 ;; is nil, resize the minibuffer window.
3646 (window--resize-mini-window
3647 minibuffer-window
(* delta
(frame-char-height))))
3648 ((window--resizable-p nil
(- delta
) horizontal
)
3649 (window-resize nil
(- delta
) horizontal
))
3650 ((window--resizable-p nil
(- delta
) horizontal
'preserved
)
3651 (window-resize nil
(- delta
) horizontal
'preserved
))
3653 (if horizontal
'shrink-window-horizontally
'shrink-window
))
3654 ;; For backward compatibility don't signal an error unless this
3655 ;; command is `shrink-window(-horizontally)'.
3656 (user-error "Cannot shrink selected window"))
3660 (- (window-min-delta nil horizontal
))
3661 (window-max-delta nil horizontal
))
3664 (defun maximize-window (&optional window
)
3666 Make WINDOW as large as possible without deleting any windows.
3667 WINDOW must be a valid window and defaults to the selected one.
3669 If the option `window-resize-pixelwise' is non-nil maximize
3672 (setq window
(window-normalize-window window
))
3674 window
(window-max-delta window nil nil nil nil nil window-resize-pixelwise
)
3675 nil nil window-resize-pixelwise
)
3677 window
(window-max-delta window t nil nil nil nil window-resize-pixelwise
)
3678 t nil window-resize-pixelwise
))
3680 (defun minimize-window (&optional window
)
3682 Make WINDOW as small as possible without deleting any windows.
3683 WINDOW must be a valid window and defaults to the selected one.
3685 If the option `window-resize-pixelwise' is non-nil minimize
3688 (setq window
(window-normalize-window window
))
3691 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise
))
3692 nil nil window-resize-pixelwise
)
3695 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise
))
3696 t nil window-resize-pixelwise
))
3699 (defun window-edges (&optional window body absolute pixelwise
)
3700 "Return a list of the edge distances of WINDOW.
3701 WINDOW must be a valid window and defaults to the selected one.
3702 The list returned has the form (LEFT TOP RIGHT BOTTOM).
3704 If the optional argument BODY is nil, this means to return the
3705 edges corresponding to the total size of WINDOW. BODY non-nil
3706 means to return the edges of WINDOW's body (aka text area). If
3707 BODY is non-nil, WINDOW must specify a live window.
3709 Optional argument ABSOLUTE nil means to return edges relative to
3710 the position of WINDOW's native frame. ABSOLUTE non-nil means to
3711 return coordinates relative to the origin - the position (0, 0) -
3712 of FRAME's display. On non-graphical systems this argument has
3715 Optional argument PIXELWISE nil means to return the coordinates
3716 in terms of the canonical character width and height of WINDOW's
3717 frame, rounded if necessary. PIXELWISE non-nil means to return
3718 the coordinates in pixels where the values for RIGHT and BOTTOM
3719 are one more than the actual value of these edges. Note that if
3720 ABSOLUTE is non-nil, PIXELWISE is implicitly non-nil too."
3721 (let* ((window (window-normalize-window window body
))
3722 (frame (window-frame window
))
3723 (border-width (frame-internal-border-width frame
))
3724 (char-width (frame-char-width frame
))
3725 (char-height (frame-char-height frame
))
3727 (+ (window-pixel-left window
) border-width
)
3728 (+ (window-left-column window
)
3729 (/ border-width char-width
))))
3732 (+ (window-pixel-left window
) border-width
3733 (if (eq (car (window-current-scroll-bars window
)) 'left
)
3734 (window-scroll-bar-width window
)
3736 (nth 0 (window-fringes window
))
3737 (* (or (nth 0 (window-margins window
)) 0) char-width
))))
3739 (+ (window-pixel-top window
) border-width
)
3740 (+ (window-top-line window
)
3741 (/ border-width char-height
))))
3744 (+ (window-pixel-top window
) border-width
3745 (window-header-line-height window
))))
3746 (right (+ left
(if pixelwise
3747 (window-pixel-width window
)
3748 (window-total-width window
))))
3749 (right-body (and body
(+ left-body
(window-body-width window t
))))
3750 (bottom (+ top
(if pixelwise
3751 (window-pixel-height window
)
3752 (window-total-height window
))))
3753 (bottom-body (and body
(+ top-body
(window-body-height window t
)))))
3755 (let* ((native-edges (frame-edges frame
'native-edges
))
3756 (left-off (nth 0 native-edges
))
3757 (top-off (nth 1 native-edges
)))
3759 (list (+ left-body left-off
) (+ top-body top-off
)
3760 (+ right-body left-off
) (+ bottom-body top-off
))
3761 (list (+ left left-off
) (+ top top-off
)
3762 (+ right left-off
) (+ bottom top-off
))))
3765 (list left-body top-body right-body bottom-body
)
3766 (list (/ left-body char-width
) (/ top-body char-height
)
3768 (/ (+ right-body char-width -
1) char-width
)
3769 (/ (+ bottom-body char-height -
1) char-height
)))
3770 (list left top right bottom
)))))
3772 (defun window-body-edges (&optional window
)
3773 "Return a list of the edge coordinates of WINDOW's body.
3774 The return value is that of `window-edges' called with argument
3776 (window-edges window t
))
3777 (defalias 'window-inside-edges
'window-body-edges
)
3779 (defun window-pixel-edges (&optional window
)
3780 "Return a list of the edge pixel coordinates of WINDOW.
3781 The return value is that of `window-edges' called with argument
3783 (window-edges window nil nil t
))
3785 (defun window-body-pixel-edges (&optional window
)
3786 "Return a list of the edge pixel coordinates of WINDOW's body.
3787 The return value is that of `window-edges' called with arguments
3788 BODY and PIXELWISE non-nil."
3789 (window-edges window t nil t
))
3790 (defalias 'window-inside-pixel-edges
'window-body-pixel-edges
)
3792 (defun window-absolute-pixel-edges (&optional window
)
3793 "Return a list of the edge pixel coordinates of WINDOW.
3794 The return value is that of `window-edges' called with argument
3796 (window-edges window nil t t
))
3798 (defun window-absolute-body-pixel-edges (&optional window
)
3799 "Return a list of the edge pixel coordinates of WINDOW's text area.
3800 The return value is that of `window-edges' called with arguments
3801 BODY and ABSOLUTE non-nil."
3802 (window-edges window t t t
))
3803 (defalias 'window-inside-absolute-pixel-edges
'window-absolute-body-pixel-edges
)
3805 (defun window-absolute-pixel-position (&optional position window
)
3806 "Return display coordinates of POSITION in WINDOW.
3807 If the buffer position POSITION is visible in window WINDOW,
3808 return the display coordinates of the upper/left corner of the
3809 glyph at POSITION. The return value is a cons of the X- and
3810 Y-coordinates of that corner, relative to an origin at (0, 0) of
3811 WINDOW's display. Return nil if POSITION is not visible in
3814 WINDOW must be a live window and defaults to the selected window.
3815 POSITION defaults to the value of `window-point' of WINDOW."
3816 (let* ((window (window-normalize-window window t
))
3818 (pos-visible-in-window-p
3819 (or position
(window-point window
)) window t
)))
3821 (let ((edges (window-absolute-body-pixel-edges window
)))
3822 (cons (+ (nth 0 edges
) (nth 0 pos-in-window
))
3823 (+ (nth 1 edges
) (nth 1 pos-in-window
)))))))
3825 (defun frame-root-window-p (window)
3826 "Return non-nil if WINDOW is the root window of its frame."
3827 (eq window
(frame-root-window window
)))
3829 (defun window--subtree (window &optional next
)
3830 "Return window subtree rooted at WINDOW.
3831 Optional argument NEXT non-nil means include WINDOW's right
3832 siblings in the return value.
3834 See the documentation of `window-tree' for a description of the
3841 ((window-top-child window
)
3842 (cons t
(cons (window-edges window
)
3843 (window--subtree (window-top-child window
) t
))))
3844 ((window-left-child window
)
3845 (cons nil
(cons (window-edges window
)
3846 (window--subtree (window-left-child window
) t
))))
3849 (setq window
(when next
(window-next-sibling window
))))
3852 (defun window-tree (&optional frame
)
3853 "Return the window tree of frame FRAME.
3854 FRAME must be a live frame and defaults to the selected frame.
3855 The return value is a list of the form (ROOT MINI), where ROOT
3856 represents the window tree of the frame's root window, and MINI
3857 is the frame's minibuffer window.
3859 If the root window is not split, ROOT is the root window itself.
3860 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3861 for a horizontal split, and t for a vertical split. EDGES gives
3862 the combined size and position of the child windows in the split,
3863 and the rest of the elements are the child windows in the split.
3864 Each of the child windows may again be a window or a list
3865 representing a window split, and so on. EDGES is a list (LEFT
3866 TOP RIGHT BOTTOM) as returned by `window-edges'."
3867 (setq frame
(window-normalize-frame frame
))
3868 (window--subtree (frame-root-window frame
) t
))
3870 (defun other-window (count &optional all-frames
)
3871 "Select another window in cyclic ordering of windows.
3872 COUNT specifies the number of windows to skip, starting with the
3873 selected window, before making the selection. If COUNT is
3874 positive, skip COUNT windows forwards. If COUNT is negative,
3875 skip -COUNT windows backwards. COUNT zero means do not skip any
3876 window, so select the selected window. In an interactive call,
3877 COUNT is the numeric prefix argument. Return nil.
3879 If the `other-window' parameter of the selected window is a
3880 function and `ignore-window-parameters' is nil, call that
3881 function with the arguments COUNT and ALL-FRAMES.
3883 This function does not select a window whose `no-other-window'
3884 window parameter is non-nil.
3886 This function uses `next-window' for finding the window to
3887 select. The argument ALL-FRAMES has the same meaning as in
3888 `next-window', but the MINIBUF argument of `next-window' is
3889 always effectively nil."
3891 (let* ((window (selected-window))
3892 (function (and (not ignore-window-parameters
)
3893 (window-parameter window
'other-window
)))
3894 old-window old-count
)
3895 (if (functionp function
)
3896 (funcall function count all-frames
)
3897 ;; `next-window' and `previous-window' may return a window we are
3898 ;; not allowed to select. Hence we need an exit strategy in case
3899 ;; all windows are non-selectable.
3902 (setq window
(next-window window nil all-frames
))
3904 ((eq window old-window
)
3905 (when (= count old-count
)
3906 ;; Keep out of infinite loops. When COUNT has not changed
3907 ;; since we last looked at `window' we're probably in one.
3909 ((window-parameter window
'no-other-window
)
3911 ;; The first non-selectable window `next-window' got us:
3912 ;; Remember it and the current value of COUNT.
3913 (setq old-window window
)
3914 (setq old-count count
)))
3916 (setq count
(1- count
)))))
3918 (setq window
(previous-window window nil all-frames
))
3920 ((eq window old-window
)
3921 (when (= count old-count
)
3922 ;; Keep out of infinite loops. When COUNT has not changed
3923 ;; since we last looked at `window' we're probably in one.
3925 ((window-parameter window
'no-other-window
)
3927 ;; The first non-selectable window `previous-window' got
3928 ;; us: Remember it and the current value of COUNT.
3929 (setq old-window window
)
3930 (setq old-count count
)))
3932 (setq count
(1+ count
)))))
3934 (select-window window
)
3935 ;; Always return nil.
3938 ;; This should probably return non-nil when the selected window is part
3939 ;; of an atomic window whose root is the frame's root window.
3940 (defun one-window-p (&optional nomini all-frames
)
3941 "Return non-nil if the selected window is the only window.
3942 Optional arg NOMINI non-nil means don't count the minibuffer
3943 even if it is active. Otherwise, the minibuffer is counted
3946 Optional argument ALL-FRAMES specifies the set of frames to
3947 consider, see also `next-window'. ALL-FRAMES nil or omitted
3948 means consider windows on the selected frame only, plus the
3949 minibuffer window if specified by the NOMINI argument. If the
3950 minibuffer counts, consider all windows on all frames that share
3951 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3952 with a special meaning are:
3954 - t means consider all windows on all existing frames.
3956 - `visible' means consider all windows on all visible frames on
3957 the current terminal.
3959 - 0 (the number zero) means consider all windows on all visible
3960 and iconified frames on the current terminal.
3962 - A frame means consider all windows on that frame only.
3964 Anything else means consider all windows on the selected frame
3966 (let ((base-window (selected-window)))
3967 (if (and nomini
(eq base-window
(minibuffer-window)))
3968 (setq base-window
(next-window base-window
)))
3970 (next-window base-window
(if nomini
'arg
) all-frames
))))
3972 ;;; Deleting windows.
3973 (defun window-deletable-p (&optional window
)
3974 "Return t if WINDOW can be safely deleted from its frame.
3975 WINDOW must be a valid window and defaults to the selected one.
3977 Return `frame' if WINDOW is the root window of its frame and that
3978 frame can be safely deleted."
3979 (setq window
(window-normalize-window window
))
3981 (unless (or ignore-window-parameters
3982 (eq (window-parameter window
'delete-window
) t
))
3983 ;; Handle atomicity.
3984 (when (window-parameter window
'window-atom
)
3985 (setq window
(window-atom-root window
))))
3987 (let ((frame (window-frame window
)))
3989 ((frame-root-window-p window
)
3990 ;; WINDOW's frame can be deleted only if there are other frames
3991 ;; on the same terminal, and it does not contain the active
3993 (unless (or (eq frame
(next-frame frame
0))
3994 ;; We can delete our frame only if no other frame
3995 ;; currently uses our minibuffer window.
3997 (dolist (other (frame-list))
3998 (when (and (not (eq other frame
))
3999 (eq (window-frame (minibuffer-window other
))
4002 (let ((minibuf (active-minibuffer-window)))
4003 (and minibuf
(eq frame
(window-frame minibuf
)))))
4005 ((window-minibuffer-p window
)
4006 ;; If WINDOW is the minibuffer window of a non-minibuffer-only
4007 ;; frame, it cannot be deleted separately.
4009 ((or ignore-window-parameters
4010 (not (eq window
(window-main-window frame
))))
4011 ;; Otherwise, WINDOW can be deleted unless it is the main window
4015 (defun window--in-subtree-p (window root
)
4016 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
4017 (or (eq window root
)
4018 (let ((parent (window-parent window
)))
4021 (if (eq parent root
)
4023 (setq parent
(window-parent parent
))))))))
4025 (defun delete-window (&optional window
)
4027 WINDOW must be a valid window and defaults to the selected one.
4030 If the variable `ignore-window-parameters' is non-nil or the
4031 `delete-window' parameter of WINDOW equals t, do not process any
4032 parameters of WINDOW. Otherwise, if the `delete-window'
4033 parameter of WINDOW specifies a function, call that function with
4034 WINDOW as its sole argument and return the value returned by that
4037 Otherwise, if WINDOW is part of an atomic window, call
4038 `delete-window' with the root of the atomic window as its
4039 argument. Signal an error if WINDOW is either the only window on
4040 its frame, the last non-side window, or part of an atomic window
4041 that is its frame's root window."
4043 (setq window
(window-normalize-window window
))
4044 (let* ((frame (window-frame window
))
4045 (function (window-parameter window
'delete-window
))
4046 (parent (window-parent window
))
4048 (window--check frame
)
4050 ;; Handle window parameters.
4052 ;; Ignore window parameters if `ignore-window-parameters' tells
4053 ;; us so or `delete-window' equals t.
4054 ((or ignore-window-parameters
(eq function t
)))
4055 ((functionp function
)
4056 ;; The `delete-window' parameter specifies the function to call.
4057 ;; If that function is `ignore' nothing is done. It's up to the
4058 ;; function called here to avoid infinite recursion.
4059 (throw 'done
(funcall function window
)))
4060 ((and (window-parameter window
'window-atom
)
4061 (setq atom-root
(window-atom-root window
))
4062 (not (eq atom-root window
)))
4063 (if (eq atom-root
(frame-root-window frame
))
4064 (error "Root of atomic window is root window of its frame")
4065 (throw 'done
(delete-window atom-root
))))
4067 (error "Attempt to delete minibuffer or sole ordinary window"))
4068 ((eq window
(window-main-window frame
))
4069 (error "Attempt to delete main window of frame %s" frame
)))
4071 (let* ((horizontal (window-left-child parent
))
4072 (size (window-size window horizontal t
))
4073 (window-combination-resize
4074 (or window-combination-resize
4075 (window-parameter parent
'window-side
)))
4077 (window--in-subtree-p (frame-selected-window frame
) window
))
4078 ;; Emacs 23 preferably gives WINDOW's space to its left
4080 (sibling (or (window-left window
) (window-right window
))))
4081 (window--resize-reset frame horizontal
)
4083 ((and (not (eq window-combination-resize t
))
4084 sibling
(window-sizable-p sibling size horizontal nil t
))
4085 ;; Resize WINDOW's sibling.
4086 (window--resize-this-window sibling size horizontal nil t
)
4087 (set-window-new-normal
4088 sibling
(+ (window-normal-size sibling horizontal
)
4089 (window-normal-size window horizontal
))))
4090 ((window--resizable-p window
(- size
) horizontal nil nil nil t t
)
4091 ;; Can do without resizing fixed-size windows.
4092 (window--resize-siblings window
(- size
) horizontal
))
4094 ;; Can't do without resizing fixed-size windows.
4095 (window--resize-siblings window
(- size
) horizontal t
)))
4096 ;; Actually delete WINDOW.
4097 (delete-window-internal window
)
4098 (window--pixel-to-total frame horizontal
)
4099 (when (and frame-selected
4101 (frame-selected-window frame
) 'no-other-window
))
4102 ;; `delete-window-internal' has selected a window that should
4103 ;; not be selected, fix this here.
4104 (other-window -
1 frame
))
4105 (run-window-configuration-change-hook frame
)
4106 (window--check frame
)
4107 ;; Always return nil.
4110 (defun delete-other-windows (&optional window
)
4111 "Make WINDOW fill its frame.
4112 WINDOW must be a valid window and defaults to the selected one.
4115 If the variable `ignore-window-parameters' is non-nil or the
4116 `delete-other-windows' parameter of WINDOW equals t, do not pay
4117 attention to any other parameters of WINDOW. Otherwise, if the
4118 `delete-other-windows' parameter of WINDOW specifies a function,
4119 call that function with WINDOW as its sole argument and return
4120 the value returned by that function.
4122 Else, if WINDOW is part of an atomic window, call this function
4123 with the root of the atomic window as its argument. Signal an
4124 error if that root window is the root window of WINDOW's frame.
4125 Also signal an error if WINDOW is a side window. Do not delete
4126 any window whose `no-delete-other-windows' parameter is non-nil."
4128 (setq window
(window-normalize-window window
))
4129 (let* ((frame (window-frame window
))
4130 (function (window-parameter window
'delete-other-windows
))
4132 (window--check frame
)
4135 ;; Ignore window parameters if `ignore-window-parameters' is t or
4136 ;; `delete-other-windows' is t.
4137 ((or ignore-window-parameters
(eq function t
)))
4138 ((functionp function
)
4139 ;; The `delete-other-windows' parameter specifies the function
4140 ;; to call. If the function is `ignore' no windows are deleted.
4141 ;; It's up to the function called to avoid infinite recursion.
4142 (throw 'done
(funcall function window
)))
4143 ((and (window-parameter window
'window-atom
)
4144 (setq atom-root
(window-atom-root window
))
4145 (not (eq atom-root window
)))
4146 (if (eq atom-root
(frame-root-window frame
))
4147 (error "Root of atomic window is root window of its frame")
4148 (throw 'done
(delete-other-windows atom-root
))))
4149 ((window-parameter window
'window-side
)
4150 (error "Cannot make side window the only window"))
4151 ((and (window-minibuffer-p window
)
4152 (not (eq window
(frame-root-window window
))))
4153 (error "Can't expand minibuffer to full frame")))
4156 ((or ignore-window-parameters
4157 (not (window-with-parameter 'no-delete-other-windows nil frame
)))
4158 (setq main
(frame-root-window frame
)))
4162 (when (or (and (window-parameter other
'window-side
)
4163 (not (window-parameter
4164 other
'no-delete-other-windows
)))
4165 (and (not (window-parameter other
'window-side
))
4167 other
'no-delete-other-windows
)))
4170 (setq main
(window-main-window frame
)))
4172 ;; Delete windows via `delete-window' because we found either a
4173 ;; deletable side window or a non-deletable non-side-window.
4174 (dolist (other (window-list frame
))
4175 (when (and (window-live-p other
)
4176 (not (eq other window
))
4177 (not (window-parameter
4178 other
'no-delete-other-windows
))
4179 ;; When WINDOW and the other window are part of the
4180 ;; same atomic window, don't delete the other.
4182 (not (eq (window-atom-root other
) atom-root
))))
4184 (delete-window other
)
4188 ;; If WINDOW is the main window of its frame do nothing.
4189 (unless (eq window main
)
4190 (delete-other-windows-internal window main
)
4191 (run-window-configuration-change-hook frame
)
4192 (window--check frame
))
4193 ;; Always return nil.
4196 (defun delete-other-windows-vertically (&optional window
)
4197 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
4198 This may be a useful alternative binding for \\[delete-other-windows]
4199 if you often split windows horizontally."
4201 (let* ((window (or window
(selected-window)))
4202 (edges (window-edges window
))
4204 (while (not (eq (setq w
(next-window w
1)) window
))
4205 (let ((e (window-edges w
)))
4206 (when (and (= (car e
) (car edges
))
4207 (= (nth 2 e
) (nth 2 edges
)))
4209 (mapc 'delete-window delenda
)))
4211 ;;; Windows and buffers.
4213 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
4214 ;; for (1) determining which buffer to show in the window when its
4215 ;; buffer shall be buried or killed and (2) which buffer to show for
4216 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
4218 ;; `prev-buffers' consists of <buffer, window-start, window-point>
4219 ;; triples. The entries on this list are ordered by the time their
4220 ;; buffer has been removed from the window, the most recently removed
4221 ;; buffer's entry being first. The window-start and window-point
4222 ;; components are `window-start' and `window-point' at the time the
4223 ;; buffer was removed from the window which implies that the entry must
4224 ;; be added when `set-window-buffer' removes the buffer from the window.
4226 ;; `next-buffers' is the list of buffers that have been replaced
4227 ;; recently by `switch-to-prev-buffer'. These buffers are the least
4228 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
4229 ;; candidates of `switch-to-next-buffer' to switch to. This list is
4230 ;; reset to nil by any action changing the window's buffer with the
4231 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
4232 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
4233 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
4235 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
4236 ;; if such a buffer was killed while the window was hidden within a
4237 ;; window configuration. Such killed buffers get removed whenever
4238 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
4240 ;; The following function is called by `set-window-buffer' _before_ it
4241 ;; replaces the buffer of the argument window with the new buffer.
4242 (defun record-window-buffer (&optional window
)
4243 "Record WINDOW's buffer.
4244 WINDOW must be a live window and defaults to the selected one."
4245 (let* ((window (window-normalize-window window t
))
4246 (buffer (window-buffer window
))
4247 (entry (assq buffer
(window-prev-buffers window
))))
4248 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
4249 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
4250 (set-window-next-buffers window nil
)
4253 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
4254 (set-window-prev-buffers
4255 window
(assq-delete-all buffer
(window-prev-buffers window
))))
4257 ;; Don't record insignificant buffers.
4258 (unless (eq (aref (buffer-name buffer
) 0) ?\s
)
4259 ;; Add an entry for buffer to WINDOW's previous buffers.
4260 (with-current-buffer buffer
4261 (let ((start (window-start window
))
4262 (point (window-point window
)))
4266 ;; We have an entry, update marker positions.
4267 (list (set-marker (nth 1 entry
) start
)
4268 (set-marker (nth 2 entry
) point
))
4269 ;; Make new markers.
4270 (list (copy-marker start
)
4272 ;; Preserve window-point-insertion-type
4274 point window-point-insertion-type
)))))
4275 (set-window-prev-buffers
4276 window
(cons entry
(window-prev-buffers window
)))))
4278 (run-hooks 'buffer-list-update-hook
))))
4280 (defun unrecord-window-buffer (&optional window buffer
)
4281 "Unrecord BUFFER in WINDOW.
4282 WINDOW must be a live window and defaults to the selected one.
4283 BUFFER must be a live buffer and defaults to the buffer of
4285 (let* ((window (window-normalize-window window t
))
4286 (buffer (or buffer
(window-buffer window
))))
4287 (set-window-prev-buffers
4288 window
(assq-delete-all buffer
(window-prev-buffers window
)))
4289 (set-window-next-buffers
4290 window
(delq buffer
(window-next-buffers window
)))))
4292 (defun set-window-buffer-start-and-point (window buffer
&optional start point
)
4293 "Set WINDOW's buffer to BUFFER.
4294 WINDOW must be a live window and defaults to the selected one.
4295 Optional argument START non-nil means set WINDOW's start position
4296 to START. Optional argument POINT non-nil means set WINDOW's
4297 point to POINT. If WINDOW is selected this also sets BUFFER's
4298 `point' to POINT. If WINDOW is selected and the buffer it showed
4299 before was current this also makes BUFFER the current buffer."
4300 (setq window
(window-normalize-window window t
))
4301 (let ((selected (eq window
(selected-window)))
4302 (current (eq (window-buffer window
) (current-buffer))))
4303 (set-window-buffer window buffer
)
4304 (when (and selected current
)
4305 (set-buffer buffer
))
4307 ;; Don't force window-start here (even if POINT is nil).
4308 (set-window-start window start t
))
4310 (set-window-point window point
))))
4312 (defcustom switch-to-visible-buffer t
4313 "If non-nil, allow switching to an already visible buffer.
4314 If this variable is non-nil, `switch-to-prev-buffer' and
4315 `switch-to-next-buffer' may switch to an already visible buffer.
4316 If this variable is nil, `switch-to-prev-buffer' and
4317 `switch-to-next-buffer' always try to avoid switching to a buffer
4318 that is already visible in another window on the same frame."
4323 (defun switch-to-prev-buffer (&optional window bury-or-kill
)
4324 "In WINDOW switch to previous buffer.
4325 WINDOW must be a live window and defaults to the selected one.
4326 Return the buffer switched to, nil if no suitable buffer could be
4329 Optional argument BURY-OR-KILL non-nil means the buffer currently
4330 shown in WINDOW is about to be buried or killed and consequently
4331 shall not be switched to in future invocations of this command.
4333 As a special case, if BURY-OR-KILL equals `append', this means to
4334 move the buffer to the end of WINDOW's previous buffers list so a
4335 future invocation of `switch-to-prev-buffer' less likely switches
4338 (let* ((window (window-normalize-window window t
))
4339 (frame (window-frame window
))
4340 (window-side (window-parameter window
'window-side
))
4341 (old-buffer (window-buffer window
))
4342 ;; Save this since it's destroyed by `set-window-buffer'.
4343 (next-buffers (window-next-buffers window
))
4344 (pred (frame-parameter frame
'buffer-predicate
))
4345 entry new-buffer killed-buffers visible
)
4346 (when (window-minibuffer-p window
)
4347 ;; Don't switch in minibuffer window.
4348 (unless (setq window
(minibuffer-selected-window))
4349 (error "Window %s is a minibuffer window" window
)))
4351 (unless (memq (window-dedicated-p window
) '(nil side
))
4352 ;; Don't switch in dedicated window.
4353 (error "Window %s is dedicated to buffer %s" window old-buffer
))
4356 ;; Scan WINDOW's previous buffers first, skipping entries of next
4358 (dolist (entry (window-prev-buffers window
))
4359 (when (and (setq new-buffer
(car entry
))
4360 (or (buffer-live-p new-buffer
)
4361 (not (setq killed-buffers
4362 (cons new-buffer killed-buffers
))))
4363 (not (eq new-buffer old-buffer
))
4364 (or (null pred
) (funcall pred new-buffer
))
4365 ;; When BURY-OR-KILL is nil, avoid switching to a
4366 ;; buffer in WINDOW's next buffers list.
4367 (or bury-or-kill
(not (memq new-buffer next-buffers
))))
4368 (if (and (not switch-to-visible-buffer
)
4369 (get-buffer-window new-buffer frame
))
4370 ;; Try to avoid showing a buffer visible in some other
4372 (setq visible new-buffer
)
4373 (set-window-buffer-start-and-point
4374 window new-buffer
(nth 1 entry
) (nth 2 entry
))
4376 ;; Scan reverted buffer list of WINDOW's frame next, skipping
4377 ;; entries of next buffers. Note that when we bury or kill a
4378 ;; buffer we don't reverse the global buffer list to avoid showing
4379 ;; a buried buffer instead. Otherwise, we must reverse the global
4380 ;; buffer list in order to make sure that switching to the
4381 ;; previous/next buffer traverse it in opposite directions. Skip
4382 ;; this step for side windows.
4384 (dolist (buffer (if bury-or-kill
4386 (nreverse (buffer-list frame
))))
4387 (when (and (buffer-live-p buffer
)
4388 (not (eq buffer old-buffer
))
4389 (or (null pred
) (funcall pred buffer
))
4390 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
4391 ;; Don't show a buffer shown in a side window before.
4392 (not (buffer-local-value 'window--sides-shown buffer
))
4393 (or bury-or-kill
(not (memq buffer next-buffers
))))
4394 (if (and (not switch-to-visible-buffer
)
4395 (get-buffer-window buffer frame
))
4396 ;; Try to avoid showing a buffer visible in some other window.
4398 (setq visible buffer
))
4399 (setq new-buffer buffer
)
4400 (set-window-buffer-start-and-point window new-buffer
)
4401 (throw 'found t
)))))
4402 (unless bury-or-kill
4403 ;; Scan reverted next buffers last (must not use nreverse
4405 (dolist (buffer (reverse next-buffers
))
4406 ;; Actually, buffer _must_ be live here since otherwise it
4407 ;; would have been caught in the scan of previous buffers.
4408 (when (and (or (buffer-live-p buffer
)
4409 (not (setq killed-buffers
4410 (cons buffer killed-buffers
))))
4411 (not (eq buffer old-buffer
))
4412 (or (null pred
) (funcall pred buffer
))
4413 (setq entry
(assq buffer
(window-prev-buffers window
))))
4414 (setq new-buffer buffer
)
4415 (set-window-buffer-start-and-point
4416 window new-buffer
(nth 1 entry
) (nth 2 entry
))
4419 ;; Show a buffer visible in another window.
4421 (setq new-buffer visible
)
4422 (set-window-buffer-start-and-point window new-buffer
)))
4425 (let ((entry (and (eq bury-or-kill
'append
)
4426 (assq old-buffer
(window-prev-buffers window
)))))
4427 ;; Remove `old-buffer' from WINDOW's previous and (restored list
4428 ;; of) next buffers.
4429 (set-window-prev-buffers
4430 window
(assq-delete-all old-buffer
(window-prev-buffers window
)))
4431 (set-window-next-buffers window
(delq old-buffer next-buffers
))
4433 ;; Append old-buffer's entry to list of WINDOW's previous
4434 ;; buffers so it's less likely to get switched to soon but
4435 ;; `display-buffer-in-previous-window' can nevertheless find
4437 (set-window-prev-buffers
4438 window
(append (window-prev-buffers window
) (list entry
)))))
4439 ;; Move `old-buffer' to head of WINDOW's restored list of next
4441 (set-window-next-buffers
4442 window
(cons old-buffer
(delq old-buffer next-buffers
))))
4444 ;; Remove killed buffers from WINDOW's previous and next buffers.
4445 (when killed-buffers
4446 (dolist (buffer killed-buffers
)
4447 (set-window-prev-buffers
4448 window
(assq-delete-all buffer
(window-prev-buffers window
)))
4449 (set-window-next-buffers
4450 window
(delq buffer
(window-next-buffers window
)))))
4452 ;; Return new-buffer.
4455 (defun switch-to-next-buffer (&optional window
)
4456 "In WINDOW switch to next buffer.
4457 WINDOW must be a live window and defaults to the selected one.
4458 Return the buffer switched to, nil if no suitable buffer could be
4461 (let* ((window (window-normalize-window window t
))
4462 (frame (window-frame window
))
4463 (window-side (window-parameter window
'window-side
))
4464 (old-buffer (window-buffer window
))
4465 (next-buffers (window-next-buffers window
))
4466 (pred (frame-parameter frame
'buffer-predicate
))
4467 new-buffer entry killed-buffers visible
)
4468 (when (window-minibuffer-p window
)
4469 ;; Don't switch in minibuffer window.
4470 (unless (setq window
(minibuffer-selected-window))
4471 (error "Window %s is a minibuffer window" window
)))
4473 (unless (memq (window-dedicated-p window
) '(nil side
))
4474 ;; Don't switch in dedicated window.
4475 (error "Window %s is dedicated to buffer %s" window old-buffer
))
4478 ;; Scan WINDOW's next buffers first.
4479 (dolist (buffer next-buffers
)
4480 (when (and (or (buffer-live-p buffer
)
4481 (not (setq killed-buffers
4482 (cons buffer killed-buffers
))))
4483 (not (eq buffer old-buffer
))
4484 (or (null pred
) (funcall pred buffer
))
4485 (setq entry
(assq buffer
(window-prev-buffers window
))))
4486 (setq new-buffer buffer
)
4487 (set-window-buffer-start-and-point
4488 window new-buffer
(nth 1 entry
) (nth 2 entry
))
4490 ;; Scan the buffer list of WINDOW's frame next, skipping previous
4491 ;; buffers entries. Skip this step for side windows.
4493 (dolist (buffer (buffer-list frame
))
4494 (when (and (buffer-live-p buffer
)
4495 (not (eq buffer old-buffer
))
4496 (or (null pred
) (funcall pred buffer
))
4497 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
4498 ;; Don't show a buffer shown in a side window before.
4499 (not (buffer-local-value 'window--sides-shown buffer
))
4500 (not (assq buffer
(window-prev-buffers window
))))
4501 (if (and (not switch-to-visible-buffer
)
4502 (get-buffer-window buffer frame
))
4503 ;; Try to avoid showing a buffer visible in some other window.
4504 (setq visible buffer
)
4505 (setq new-buffer buffer
)
4506 (set-window-buffer-start-and-point window new-buffer
)
4507 (throw 'found t
)))))
4508 ;; Scan WINDOW's reverted previous buffers last (must not use
4510 (dolist (entry (reverse (window-prev-buffers window
)))
4511 (when (and (setq new-buffer
(car entry
))
4512 (or (buffer-live-p new-buffer
)
4513 (not (setq killed-buffers
4514 (cons new-buffer killed-buffers
))))
4515 (not (eq new-buffer old-buffer
))
4516 (or (null pred
) (funcall pred new-buffer
)))
4517 (if (and (not switch-to-visible-buffer
)
4518 (get-buffer-window new-buffer frame
))
4519 ;; Try to avoid showing a buffer visible in some other window.
4521 (setq visible new-buffer
))
4522 (set-window-buffer-start-and-point
4523 window new-buffer
(nth 1 entry
) (nth 2 entry
))
4526 ;; Show a buffer visible in another window.
4528 (setq new-buffer visible
)
4529 (set-window-buffer-start-and-point window new-buffer
)))
4531 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
4532 (set-window-next-buffers window
(delq new-buffer next-buffers
))
4534 ;; Remove killed buffers from WINDOW's previous and next buffers.
4535 (when killed-buffers
4536 (dolist (buffer killed-buffers
)
4537 (set-window-prev-buffers
4538 window
(assq-delete-all buffer
(window-prev-buffers window
)))
4539 (set-window-next-buffers
4540 window
(delq buffer
(window-next-buffers window
)))))
4542 ;; Return new-buffer.
4545 (defun get-next-valid-buffer (list &optional buffer visible-ok frame
)
4546 "Search LIST for a valid buffer to display in FRAME.
4547 Return nil when all buffers in LIST are undesirable for display,
4548 otherwise return the first suitable buffer in LIST.
4550 Buffers not visible in windows are preferred to visible buffers,
4551 unless VISIBLE-OK is non-nil.
4552 If the optional argument FRAME is nil, it defaults to the selected frame.
4553 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
4554 ;; This logic is more or less copied from other-buffer.
4555 (setq frame
(or frame
(selected-frame)))
4556 (let ((pred (frame-parameter frame
'buffer-predicate
))
4558 (while (and (not found
) list
)
4559 (setq buf
(car list
))
4560 (if (and (not (eq buffer buf
))
4562 (or (null pred
) (funcall pred buf
))
4563 (not (eq (aref (buffer-name buf
) 0) ?\s
))
4564 (or visible-ok
(null (get-buffer-window buf
'visible
))))
4566 (setq list
(cdr list
))))
4569 (defun last-buffer (&optional buffer visible-ok frame
)
4570 "Return the last buffer in FRAME's buffer list.
4571 If BUFFER is the last buffer, return the preceding buffer
4572 instead. Buffers not visible in windows are preferred to visible
4573 buffers, unless optional argument VISIBLE-OK is non-nil.
4574 Optional third argument FRAME nil or omitted means use the
4575 selected frame's buffer list. If no such buffer exists, return
4576 the buffer `*scratch*', creating it if necessary."
4577 (setq frame
(or frame
(selected-frame)))
4578 (or (get-next-valid-buffer (nreverse (buffer-list frame
))
4579 buffer visible-ok frame
)
4580 (get-buffer "*scratch*")
4581 (let ((scratch (get-buffer-create "*scratch*")))
4582 (set-buffer-major-mode scratch
)
4585 (defcustom frame-auto-hide-function
#'iconify-frame
4586 "Function called to automatically hide frames.
4587 The function is called with one argument - a frame.
4589 Functions affected by this option are those that bury a buffer
4590 shown in a separate frame like `quit-window' and `bury-buffer'."
4591 :type
'(choice (const :tag
"Iconify" iconify-frame
)
4592 (const :tag
"Make invisible" make-frame-invisible
)
4593 (const :tag
"Delete" delete-frame
)
4594 (const :tag
"Do nothing" ignore
)
4600 (defun window--delete (&optional window dedicated-only kill
)
4601 "Delete WINDOW if possible.
4602 WINDOW must be a live window and defaults to the selected one.
4603 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
4604 only if it's dedicated to its buffer. Optional argument KILL
4605 means the buffer shown in window will be killed. Return non-nil
4606 if WINDOW gets deleted or its frame is auto-hidden."
4607 (setq window
(window-normalize-window window t
))
4608 (unless (and dedicated-only
(not (window-dedicated-p window
)))
4609 (let ((deletable (window-deletable-p window
)))
4611 ((eq deletable
'frame
)
4612 (let ((frame (window-frame window
)))
4615 (delete-frame frame
))
4616 ((functionp (frame-parameter frame
'auto-hide-function
))
4617 (funcall (frame-parameter frame
'auto-hide-function
)))
4618 ((functionp frame-auto-hide-function
)
4619 (funcall frame-auto-hide-function frame
))))
4622 (delete-window window
)
4625 (defun bury-buffer (&optional buffer-or-name
)
4626 "Put BUFFER-OR-NAME at the end of the list of all buffers.
4627 There it is the least likely candidate for `other-buffer' to
4628 return; thus, the least likely buffer for \\[switch-to-buffer] to
4631 You can specify a buffer name as BUFFER-OR-NAME, or an actual
4632 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
4633 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
4634 remove the current buffer from the selected window if it is
4637 (let* ((buffer (window-normalize-buffer buffer-or-name
)))
4638 ;; If `buffer-or-name' is not on the selected frame we unrecord it
4639 ;; although it's not "here" (call it a feature).
4640 (bury-buffer-internal buffer
)
4641 ;; Handle case where `buffer-or-name' is nil and the current buffer
4642 ;; is shown in the selected window.
4644 ((or buffer-or-name
(not (eq buffer
(window-buffer)))))
4645 ((window--delete nil t
))
4647 ;; Switch to another buffer in window.
4648 (set-window-dedicated-p nil nil
)
4649 (switch-to-prev-buffer nil
'bury
)))
4651 ;; Always return nil.
4654 (defun unbury-buffer ()
4655 "Switch to the last buffer in the buffer list."
4657 (switch-to-buffer (last-buffer)))
4659 (defun next-buffer ()
4660 "In selected window switch to next buffer."
4663 ((window-minibuffer-p)
4664 (error "Cannot switch buffers in minibuffer window"))
4665 ((eq (window-dedicated-p) t
)
4666 (error "Window is strongly dedicated to its buffer"))
4668 (switch-to-next-buffer))))
4670 (defun previous-buffer ()
4671 "In selected window switch to previous buffer."
4674 ((window-minibuffer-p)
4675 (error "Cannot switch buffers in minibuffer window"))
4676 ((eq (window-dedicated-p) t
)
4677 (error "Window is strongly dedicated to its buffer"))
4679 (switch-to-prev-buffer))))
4681 (defun delete-windows-on (&optional buffer-or-name frame
)
4682 "Delete all windows showing BUFFER-OR-NAME.
4683 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4684 and defaults to the current buffer.
4686 The following non-nil values of the optional argument FRAME
4687 have special meanings:
4689 - t means consider all windows on the selected frame only.
4691 - `visible' means consider all windows on all visible frames on
4692 the current terminal.
4694 - 0 (the number zero) means consider all windows on all visible
4695 and iconified frames on the current terminal.
4697 - A frame means consider all windows on that frame only.
4699 Any other value of FRAME means consider all windows on all
4702 When a window showing BUFFER-OR-NAME is dedicated and the only
4703 window of its frame, that frame is deleted when there are other
4705 (interactive "BDelete windows on (buffer):\nP")
4706 (let ((buffer (window-normalize-buffer buffer-or-name
))
4707 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4708 ;; `window-list-1' based function.
4709 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
4710 (dolist (window (window-list-1 nil nil all-frames
))
4711 (if (eq (window-buffer window
) buffer
)
4712 (let ((deletable (window-deletable-p window
)))
4714 ((and (eq deletable
'frame
) (window-dedicated-p window
))
4715 ;; Delete frame if and only if window is dedicated.
4716 (delete-frame (window-frame window
)))
4719 (delete-window window
))
4721 ;; In window switch to previous buffer.
4722 (set-window-dedicated-p window nil
)
4723 (switch-to-prev-buffer window
'bury
))))
4724 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4725 (unrecord-window-buffer window buffer
)))))
4727 (defun replace-buffer-in-windows (&optional buffer-or-name
)
4728 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
4729 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4730 and defaults to the current buffer.
4732 When a window showing BUFFER-OR-NAME is dedicated, that window is
4733 deleted. If that window is the only window on its frame, the
4734 frame is deleted too when there are other frames left. If there
4735 are no other frames left, some other buffer is displayed in that
4738 This function removes the buffer denoted by BUFFER-OR-NAME from
4739 all window-local buffer lists."
4740 (interactive "bBuffer to replace: ")
4741 (let ((buffer (window-normalize-buffer buffer-or-name
)))
4742 (dolist (window (window-list-1 nil nil t
))
4743 (if (eq (window-buffer window
) buffer
)
4744 (unless (window--delete window t t
)
4745 ;; Switch to another buffer in window.
4746 (set-window-dedicated-p window nil
)
4747 (switch-to-prev-buffer window
'kill
))
4748 ;; Unrecord BUFFER in WINDOW.
4749 (unrecord-window-buffer window buffer
)))))
4751 (defun quit-restore-window (&optional window bury-or-kill
)
4752 "Quit WINDOW and deal with its buffer.
4753 WINDOW must be a live window and defaults to the selected one.
4755 According to information stored in WINDOW's `quit-restore' window
4756 parameter either (1) delete WINDOW and its frame, (2) delete
4757 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4758 or (4) make WINDOW display some other buffer than the present
4759 one. If non-nil, reset `quit-restore' parameter to nil.
4761 Optional second argument BURY-OR-KILL tells how to proceed with
4762 the buffer of WINDOW. The following values are handled:
4764 nil means to not handle the buffer in a particular way. This
4765 means that if WINDOW is not deleted by this function, invoking
4766 `switch-to-prev-buffer' will usually show the buffer again.
4768 `append' means that if WINDOW is not deleted, move its buffer to
4769 the end of WINDOW's previous buffers so it's less likely that a
4770 future invocation of `switch-to-prev-buffer' will switch to it.
4771 Also, move the buffer to the end of the frame's buffer list.
4773 `bury' means that if WINDOW is not deleted, remove its buffer
4774 from WINDOW'S list of previous buffers. Also, move the buffer
4775 to the end of the frame's buffer list. This value provides the
4776 most reliable remedy to not have `switch-to-prev-buffer' switch
4777 to this buffer again without killing the buffer.
4779 `kill' means to kill WINDOW's buffer."
4780 (setq window
(window-normalize-window window t
))
4781 (let* ((buffer (window-buffer window
))
4782 (quit-restore (window-parameter window
'quit-restore
))
4784 (let* ((prev-buffers (window-prev-buffers window
))
4785 (prev-buffer (caar prev-buffers
)))
4786 (and (or (not (eq prev-buffer buffer
))
4787 (and (cdr prev-buffers
)
4788 (not (eq (setq prev-buffer
(cadr prev-buffers
))
4793 ((and (not prev-buffer
)
4794 (or (eq (nth 1 quit-restore
) 'frame
)
4795 (and (eq (nth 1 quit-restore
) 'window
)
4796 ;; If the window has been created on an existing
4797 ;; frame and ended up as the sole window on that
4798 ;; frame, do not delete it (Bug#12764).
4799 (not (eq window
(frame-root-window window
)))))
4800 (eq (nth 3 quit-restore
) buffer
)
4801 ;; Delete WINDOW if possible.
4802 (window--delete window nil
(eq bury-or-kill
'kill
)))
4803 ;; If the previously selected window is still alive, select it.
4804 (when (window-live-p (nth 2 quit-restore
))
4805 (select-window (nth 2 quit-restore
))))
4806 ((and (listp (setq quad
(nth 1 quit-restore
)))
4807 (buffer-live-p (car quad
))
4808 (eq (nth 3 quit-restore
) buffer
))
4809 ;; Show another buffer stored in quit-restore parameter.
4810 (when (and (integerp (nth 3 quad
))
4811 (if (window-combined-p window
)
4812 (/= (nth 3 quad
) (window-total-height window
))
4813 (/= (nth 3 quad
) (window-total-width window
))))
4814 ;; Try to resize WINDOW to its old height but don't signal an
4819 (- (nth 3 quad
) (if (window-combined-p window
)
4820 (window-total-height window
)
4821 (window-total-width window
)))
4822 (window-combined-p window t
))
4824 (set-window-dedicated-p window nil
)
4825 ;; Restore WINDOW's previous buffer, start and point position.
4826 (set-window-buffer-start-and-point
4827 window
(nth 0 quad
) (nth 1 quad
) (nth 2 quad
))
4828 ;; Deal with the buffer we just removed from WINDOW.
4829 (setq entry
(and (eq bury-or-kill
'append
)
4830 (assq buffer
(window-prev-buffers window
))))
4832 ;; Remove buffer from WINDOW's previous and next buffers.
4833 (set-window-prev-buffers
4834 window
(assq-delete-all buffer
(window-prev-buffers window
)))
4835 (set-window-next-buffers
4836 window
(delq buffer
(window-next-buffers window
))))
4838 ;; Append old buffer's entry to list of WINDOW's previous
4839 ;; buffers so it's less likely to get switched to soon but
4840 ;; `display-buffer-in-previous-window' can nevertheless find it.
4841 (set-window-prev-buffers
4842 window
(append (window-prev-buffers window
) (list entry
))))
4843 ;; Reset the quit-restore parameter.
4844 (set-window-parameter window
'quit-restore nil
)
4845 ;; Select old window.
4846 (when (window-live-p (nth 2 quit-restore
))
4847 (select-window (nth 2 quit-restore
))))
4849 ;; Show some other buffer in WINDOW and reset the quit-restore
4851 (set-window-parameter window
'quit-restore nil
)
4852 ;; Make sure that WINDOW is no more dedicated.
4853 (set-window-dedicated-p window nil
)
4854 (switch-to-prev-buffer window bury-or-kill
)))
4856 ;; Deal with the buffer.
4858 ((not (buffer-live-p buffer
)))
4859 ((eq bury-or-kill
'kill
)
4860 (kill-buffer buffer
))
4862 (bury-buffer-internal buffer
)))))
4864 (defun quit-window (&optional kill window
)
4865 "Quit WINDOW and bury its buffer.
4866 WINDOW must be a live window and defaults to the selected one.
4867 With prefix argument KILL non-nil, kill the buffer instead of
4870 According to information stored in WINDOW's `quit-restore' window
4871 parameter either (1) delete WINDOW and its frame, (2) delete
4872 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4873 or (4) make WINDOW display some other buffer than the present
4874 one. If non-nil, reset `quit-restore' parameter to nil."
4876 (quit-restore-window window
(if kill
'kill
'bury
)))
4878 (defun quit-windows-on (&optional buffer-or-name kill frame
)
4879 "Quit all windows showing BUFFER-OR-NAME.
4880 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4881 and defaults to the current buffer. Optional argument KILL
4882 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4883 BUFFER-OR-NAME. Optional argument FRAME is handled as by
4884 `delete-windows-on'.
4886 This function calls `quit-window' on all candidate windows
4887 showing BUFFER-OR-NAME."
4888 (interactive "BQuit windows on (buffer):\nP")
4889 (let ((buffer (window-normalize-buffer buffer-or-name
))
4890 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4891 ;; `window-list-1' based function.
4892 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
4893 (dolist (window (window-list-1 nil nil all-frames
))
4894 (if (eq (window-buffer window
) buffer
)
4895 (quit-window kill window
)
4896 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4897 (unrecord-window-buffer window buffer
)))))
4899 (defun split-window (&optional window size side pixelwise
)
4900 "Make a new window adjacent to WINDOW.
4901 WINDOW must be a valid window and defaults to the selected one.
4902 Return the new window which is always a live window.
4904 Optional argument SIZE a positive number means make WINDOW SIZE
4905 lines or columns tall. If SIZE is negative, make the new window
4906 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
4907 absolute value can be less than `window-min-height' or
4908 `window-min-width'; so this command can make a new window as
4909 small as one line or two columns. SIZE defaults to half of
4912 Optional third argument SIDE nil (or `below') specifies that the
4913 new window shall be located below WINDOW. SIDE `above' means the
4914 new window shall be located above WINDOW. In both cases SIZE
4915 specifies the new number of lines for WINDOW (or the new window
4916 if SIZE is negative) including space reserved for the mode and/or
4919 SIDE t (or `right') specifies that the new window shall be
4920 located on the right side of WINDOW. SIDE `left' means the new
4921 window shall be located on the left of WINDOW. In both cases
4922 SIZE specifies the new number of columns for WINDOW (or the new
4923 window provided SIZE is negative) including space reserved for
4924 fringes and the scrollbar or a divider column. Any other non-nil
4925 value for SIDE is currently handled like t (or `right').
4927 PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
4929 If the variable `ignore-window-parameters' is non-nil or the
4930 `split-window' parameter of WINDOW equals t, do not process any
4931 parameters of WINDOW. Otherwise, if the `split-window' parameter
4932 of WINDOW specifies a function, call that function with all three
4933 arguments and return the value returned by that function.
4935 Otherwise, if WINDOW is part of an atomic window, \"split\" the
4936 root of that atomic window. The new window does not become a
4937 member of that atomic window.
4939 If WINDOW is live, properties of the new window like margins and
4940 scrollbars are inherited from WINDOW. If WINDOW is an internal
4941 window, these properties as well as the buffer displayed in the
4942 new window are inherited from the window selected on WINDOW's
4943 frame. The selected window is not changed by this function."
4944 (setq window
(window-normalize-window window
))
4947 ((memq side
'(below above right left
)) side
)
4949 (horizontal (not (memq side
'(below above
))))
4950 (frame (window-frame window
))
4951 (parent (window-parent window
))
4952 (function (window-parameter window
'split-window
))
4953 (window-side (window-parameter window
'window-side
))
4954 ;; Rebind the following two variables since in some cases we
4955 ;; have to override their value.
4956 (window-combination-limit window-combination-limit
)
4957 (window-combination-resize window-combination-resize
)
4958 (char-size (frame-char-size window horizontal
))
4960 (when (numberp size
)
4961 (window--size-to-pixel window size horizontal pixelwise t
)))
4962 (divider-width (if horizontal
4963 (frame-right-divider-width frame
)
4964 (frame-bottom-divider-width frame
)))
4966 (window--check frame
)
4969 ;; Ignore window parameters if either `ignore-window-parameters'
4970 ;; is t or the `split-window' parameter equals t.
4971 ((or ignore-window-parameters
(eq function t
)))
4972 ((functionp function
)
4973 ;; The `split-window' parameter specifies the function to call.
4974 ;; If that function is `ignore', do nothing.
4975 (throw 'done
(funcall function window size side
)))
4976 ;; If WINDOW is part of an atomic window, split the root window
4977 ;; of that atomic window instead.
4978 ((and (window-parameter window
'window-atom
)
4979 (setq atom-root
(window-atom-root window
))
4980 (not (eq atom-root window
)))
4981 (throw 'done
(split-window atom-root size side pixelwise
)))
4982 ;; If WINDOW is a side window or its first or last child is a
4983 ;; side window, throw an error unless `window-combination-resize'
4985 ((and (not (eq window-combination-resize
'side
))
4986 (window-parameter window
'window-side
))
4987 (error "Cannot split side window or parent of side window"))
4988 ;; If `window-combination-resize' is 'side and window has a side
4989 ;; window sibling, bind `window-combination-limit' to t.
4990 ((and (not (eq window-combination-resize
'side
))
4991 (or (and (window-prev-sibling window
)
4993 (window-prev-sibling window
) 'window-side
))
4994 (and (window-next-sibling window
)
4996 (window-next-sibling window
) 'window-side
))))
4997 (setq window-combination-limit t
)))
4999 ;; If `window-combination-resize' is t and SIZE is non-negative,
5000 ;; bind `window-combination-limit' to t.
5001 (when (and (eq window-combination-resize t
)
5002 pixel-size
(> pixel-size
0))
5003 (setq window-combination-limit t
))
5005 (let* ((parent-pixel-size
5006 ;; `parent-pixel-size' is the pixel size of WINDOW's
5007 ;; parent, provided it has one.
5008 (when parent
(window-size parent horizontal t
)))
5009 ;; `resize' non-nil means we are supposed to resize other
5010 ;; windows in WINDOW's combination.
5012 (and window-combination-resize
5013 (or (window-parameter window
'window-side
)
5014 (not (eq window-combination-resize
'side
)))
5015 (not (eq window-combination-limit t
))
5016 ;; Resize makes sense in iso-combinations only.
5017 (window-combined-p window horizontal
)))
5018 ;; `old-pixel-size' is the current pixel size of WINDOW.
5019 (old-pixel-size (window-size window horizontal t
))
5020 ;; `new-size' is the specified or calculated size of the
5022 new-pixel-size new-parent new-normal
)
5025 (setq new-pixel-size
5027 ;; When resizing try to give the new window the
5028 ;; average size of a window in its combination.
5029 (max (min (- parent-pixel-size
5030 (window-min-size parent horizontal nil t
))
5031 (/ parent-pixel-size
5032 (1+ (window-combinations parent horizontal
))))
5033 (window-min-pixel-size))
5034 ;; Else try to give the new window half the size
5035 ;; of WINDOW (plus an eventual odd pixel).
5036 (/ old-pixel-size
2)))
5037 (unless window-resize-pixelwise
5038 ;; Round to nearest char-size multiple.
5039 (setq new-pixel-size
5040 (* char-size
(round new-pixel-size char-size
)))))
5042 ;; SIZE non-negative specifies the new size of WINDOW.
5044 ;; Note: Specifying a non-negative SIZE is practically
5045 ;; always done as workaround for making the new window
5046 ;; appear above or on the left of the new window (the
5047 ;; ispell window is a typical example of that). In all
5048 ;; these cases the SIDE argument should be set to 'above
5049 ;; or 'left in order to support the 'resize option.
5050 ;; Here we have to nest the windows instead, see above.
5051 (setq new-pixel-size
(- old-pixel-size pixel-size
)))
5053 ;; SIZE negative specifies the size of the new window.
5054 (setq new-pixel-size
(- pixel-size
))))
5061 ;; SIZE unspecified, resizing.
5062 (unless (or (window-sizable-p
5063 parent
(- (+ new-pixel-size divider-width
)) horizontal
5066 parent
(- (+ new-pixel-size divider-width
)) horizontal
5067 (setq ignore
'preserved
) t
))
5068 (error "Window %s too small for splitting" parent
)))
5069 ((and (> (+ new-pixel-size divider-width
5070 (window-min-size window horizontal nil t
))
5072 (> (+ new-pixel-size divider-width
5074 window horizontal
(setq ignore
'preserved
) t
))
5076 ;; SIZE unspecified, no resizing.
5077 (error "Window %s too small for splitting" window
))))
5078 ((and (>= pixel-size
0)
5079 (or (>= pixel-size old-pixel-size
)
5081 (window-safe-min-pixel-size window horizontal
))))
5082 ;; SIZE specified as new size of old window. If the new size
5083 ;; is larger than the old size or the size of the new window
5084 ;; would be less than the safe minimum, signal an error.
5085 (error "Window %s too small for splitting" window
))
5087 ;; SIZE specified, resizing.
5088 (unless (or (window-sizable-p
5089 parent
(- (+ new-pixel-size divider-width
)) horizontal
5092 parent
(- (+ new-pixel-size divider-width
)) horizontal
5093 (setq ignore
'preserved
) t
))
5094 ;; If we cannot resize the parent give up.
5095 (error "Window %s too small for splitting" parent
)))
5096 ((or (< new-pixel-size
5097 (window-safe-min-pixel-size window horizontal
))
5098 (< (- old-pixel-size new-pixel-size
)
5099 (window-safe-min-pixel-size window horizontal
)))
5100 ;; SIZE specification violates minimum size restrictions.
5101 (error "Window %s too small for splitting" window
)))
5103 (window--resize-reset frame horizontal
)
5106 ;; Make new-parent non-nil if we need a new parent window;
5107 ;; either because we want to nest or because WINDOW is not
5109 (or (eq window-combination-limit t
)
5110 (not (window-combined-p window horizontal
))))
5112 ;; Make new-normal the normal size of the new window.
5114 (pixel-size (/ (float new-pixel-size
)
5115 (if new-parent old-pixel-size parent-pixel-size
)))
5117 (resize (/ 1.0 (1+ (window-combinations parent horizontal
))))
5118 (t (/ (window-normal-size window horizontal
) 2.0))))
5121 ;; Try to get space from OLD's siblings. We could go "up" and
5122 ;; try getting additional space from surrounding windows but
5123 ;; we won't be able to return space to those windows when we
5124 ;; delete the one we create here. Hence we do not go up.
5126 (window--resize-child-windows
5127 parent
(- new-pixel-size
) horizontal nil ignore
)
5128 (let* ((normal (- 1.0 new-normal
))
5129 (sub (window-child parent
)))
5131 (set-window-new-normal
5132 sub
(* (window-normal-size sub horizontal
) normal
))
5133 (setq sub
(window-right sub
)))))
5134 ;; Get entire space from WINDOW.
5135 (set-window-new-pixel
5136 window
(- old-pixel-size new-pixel-size
))
5137 (window--resize-this-window
5138 window
(- new-pixel-size
) horizontal ignore
)
5139 (set-window-new-normal
5140 window
(- (if new-parent
1.0 (window-normal-size window horizontal
))
5143 (let* ((new (split-window-internal window new-pixel-size side new-normal
)))
5144 (window--pixel-to-total frame horizontal
)
5145 ;; Assign window-side parameters, if any.
5147 ((eq window-combination-resize
'side
)
5150 (window-side window-side
)
5151 ((eq side
'above
) 'top
)
5152 ((eq side
'below
) 'bottom
)
5154 ;; We made a new side window.
5155 (set-window-parameter new
'window-side window-side
)
5156 (when (and new-parent
(window-parameter window
'window-side
))
5157 ;; We've been splitting a side root window. Give the
5158 ;; new parent the same window-side parameter.
5159 (set-window-parameter
5160 (window-parent new
) 'window-side window-side
))))
5161 ((eq window-combination-resize
'atom
)
5162 ;; Make sure `window--check-frame' won't destroy an existing
5163 ;; atomic window in case the new window gets nested inside.
5164 (unless (window-parameter window
'window-atom
)
5165 (set-window-parameter window
'window-atom t
))
5167 (set-window-parameter (window-parent new
) 'window-atom t
))
5168 (set-window-parameter new
'window-atom t
)))
5170 ;; Sanitize sizes unless SIZE was specified.
5172 (window--sanitize-window-sizes horizontal
))
5174 (run-window-configuration-change-hook frame
)
5175 (run-window-scroll-functions new
)
5176 (window--check frame
)
5177 ;; Always return the new window.
5180 (defun split-window-no-error (&optional window size side pixelwise
)
5181 "Make a new window adjacent to WINDOW.
5182 This function is like `split-window' but does not signal an error
5183 when WINDOW cannot be split.
5185 For the meaning of all arguments see the documentation of
5188 (split-window window size side pixelwise
)
5191 ;; I think this should be the default; I think people will prefer it--rms.
5192 (defcustom split-window-keep-point t
5193 "If non-nil, \\[split-window-below] preserves point in the new window.
5194 If nil, adjust point in the two windows to minimize redisplay.
5195 This option applies only to `split-window-below' and functions
5196 that call it. The low-level `split-window' function always keeps
5197 the original point in both windows."
5201 (defun split-window-below (&optional size
)
5202 "Split the selected window into two windows, one above the other.
5203 The selected window is above. The newly split-off window is
5204 below and displays the same buffer. Return the new window.
5206 If optional argument SIZE is omitted or nil, both windows get the
5207 same height, or close to it. If SIZE is positive, the upper
5208 \(selected) window gets SIZE lines. If SIZE is negative, the
5209 lower (new) window gets -SIZE lines.
5211 If the variable `split-window-keep-point' is non-nil, both
5212 windows get the same value of point as the selected window.
5213 Otherwise, the window starts are chosen so as to minimize the
5214 amount of redisplay; this is convenient on slow terminals."
5216 (let ((old-window (selected-window))
5217 (old-point (window-point))
5218 (size (and size
(prefix-numeric-value size
)))
5219 moved-by-window-height moved new-window bottom
)
5220 (when (and size
(< size
0) (< (- size
) window-min-height
))
5221 ;; `split-window' would not signal an error here.
5222 (error "Size of new window too small"))
5223 (setq new-window
(split-window nil size
))
5224 (unless split-window-keep-point
5225 (with-current-buffer (window-buffer)
5226 ;; Use `save-excursion' around vertical movements below
5227 ;; (Bug#10971). Note: When the selected window's buffer has a
5228 ;; header line, up to two lines of the buffer may not show up
5229 ;; in the resulting configuration.
5231 (goto-char (window-start))
5232 (setq moved
(vertical-motion (window-height)))
5233 (set-window-start new-window
(point))
5234 (when (> (point) (window-point new-window
))
5235 (set-window-point new-window
(point)))
5236 (when (= moved
(window-height))
5237 (setq moved-by-window-height t
)
5238 (vertical-motion -
1))
5239 (setq bottom
(point)))
5240 (and moved-by-window-height
5242 (set-window-point old-window
(1- bottom
)))
5243 (and moved-by-window-height
5244 (<= (window-start new-window
) old-point
)
5245 (set-window-point new-window old-point
)
5246 (select-window new-window
))))
5247 ;; Always copy quit-restore parameter in interactive use.
5248 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
5250 (set-window-parameter new-window
'quit-restore quit-restore
)))
5253 (defalias 'split-window-vertically
'split-window-below
)
5255 (defun split-window-right (&optional size
)
5256 "Split the selected window into two side-by-side windows.
5257 The selected window is on the left. The newly split-off window
5258 is on the right and displays the same buffer. Return the new
5261 If optional argument SIZE is omitted or nil, both windows get the
5262 same width, or close to it. If SIZE is positive, the left-hand
5263 \(selected) window gets SIZE columns. If SIZE is negative, the
5264 right-hand (new) window gets -SIZE columns. Here, SIZE includes
5265 the width of the window's scroll bar; if there are no scroll
5266 bars, it includes the width of the divider column to the window's
5269 (let ((old-window (selected-window))
5270 (size (and size
(prefix-numeric-value size
)))
5272 (when (and size
(< size
0) (< (- size
) window-min-width
))
5273 ;; `split-window' would not signal an error here.
5274 (error "Size of new window too small"))
5275 (setq new-window
(split-window nil size t
))
5276 ;; Always copy quit-restore parameter in interactive use.
5277 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
5279 (set-window-parameter new-window
'quit-restore quit-restore
)))
5282 (defalias 'split-window-horizontally
'split-window-right
)
5284 ;;; Balancing windows.
5286 ;; The following routine uses the recycled code from an old version of
5287 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
5288 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
5289 ;; more readable (FWIW we'd need three loops - one to calculate the
5290 ;; minimum sizes per window, one to enlarge or shrink windows until the
5291 ;; new parent-size matches, and one where we shrink the largest/enlarge
5292 ;; the smallest window).
5293 (defun balance-windows-2 (window horizontal
)
5294 "Subroutine of `balance-windows-1'.
5295 WINDOW must be a vertical combination (horizontal if HORIZONTAL
5297 (let* ((char-size (if window-resize-pixelwise
5299 (frame-char-size window horizontal
)))
5300 (first (window-child window
))
5302 (number-of-children 0)
5303 (parent-size (window-new-pixel window
))
5304 (total-sum parent-size
)
5305 failed size sub-total sub-delta sub-amount rest
)
5307 (setq number-of-children
(1+ number-of-children
))
5308 (when (window-size-fixed-p sub horizontal
)
5310 (- total-sum
(window-size sub horizontal t
)))
5311 (set-window-new-normal sub
'ignore
))
5312 (setq sub
(window-right sub
)))
5315 (while (and failed
(> number-of-children
0))
5316 (setq size
(/ total-sum number-of-children
))
5319 (while (and sub
(not failed
))
5320 ;; Ignore child windows that should be ignored or are stuck.
5321 (unless (window--resize-child-windows-skip-p sub
)
5322 (setq sub-total
(window-size sub horizontal t
))
5323 (setq sub-delta
(- size sub-total
))
5325 (window-sizable sub sub-delta horizontal nil t
))
5326 ;; Register the new total size for this child window.
5327 (set-window-new-pixel sub
(+ sub-total sub-amount
))
5328 (unless (= sub-amount sub-delta
)
5329 (setq total-sum
(- total-sum sub-total sub-amount
))
5330 (setq number-of-children
(1- number-of-children
))
5331 ;; We failed and need a new round.
5333 (set-window-new-normal sub
'skip
)))
5334 (setq sub
(window-right sub
))))
5336 ;; How can we be sure that `number-of-children' is NOT zero here ?
5337 (setq rest
(% total-sum number-of-children
))
5338 ;; Fix rounding by trying to enlarge non-stuck windows by one line
5339 ;; (column) until `rest' is zero.
5341 (while (and sub
(> rest
0))
5342 (unless (window--resize-child-windows-skip-p window
)
5343 (set-window-new-pixel sub
(min rest char-size
) t
)
5344 (setq rest
(- rest char-size
)))
5345 (setq sub
(window-right sub
)))
5347 ;; Fix rounding by trying to enlarge stuck windows by one line
5348 ;; (column) until `rest' equals zero.
5350 (while (and sub
(> rest
0))
5351 (unless (eq (window-new-normal sub
) 'ignore
)
5352 (set-window-new-pixel sub
(min rest char-size
) t
)
5353 (setq rest
(- rest char-size
)))
5354 (setq sub
(window-right sub
)))
5358 ;; Record new normal sizes.
5359 (set-window-new-normal
5360 sub
(/ (if (eq (window-new-normal sub
) 'ignore
)
5361 (window-size sub horizontal t
)
5362 (window-new-pixel sub
))
5363 (float parent-size
)))
5364 ;; Recursively balance each window's child windows.
5365 (balance-windows-1 sub horizontal
)
5366 (setq sub
(window-right sub
)))))
5368 (defun balance-windows-1 (window &optional horizontal
)
5369 "Subroutine of `balance-windows'."
5370 (if (window-child window
)
5371 (let ((sub (window-child window
)))
5372 (if (window-combined-p sub horizontal
)
5373 (balance-windows-2 window horizontal
)
5374 (let ((size (window-new-pixel window
)))
5376 (set-window-new-pixel sub size
)
5377 (balance-windows-1 sub horizontal
)
5378 (setq sub
(window-right sub
))))))))
5380 (defun balance-windows (&optional window-or-frame
)
5381 "Balance the sizes of windows of WINDOW-OR-FRAME.
5382 WINDOW-OR-FRAME is optional and defaults to the selected frame.
5383 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
5384 windows of that frame. If WINDOW-OR-FRAME denotes a window,
5385 recursively balance the sizes of all child windows of that
5390 ((or (not window-or-frame
)
5391 (frame-live-p window-or-frame
))
5392 (frame-root-window window-or-frame
))
5393 ((or (window-live-p window-or-frame
)
5394 (window-child window-or-frame
))
5397 (error "Not a window or frame %s" window-or-frame
))))
5398 (frame (window-frame window
)))
5399 ;; Balance vertically.
5400 (window--resize-reset (window-frame window
))
5401 (balance-windows-1 window
)
5402 (when (window--resize-apply-p frame
)
5403 (window-resize-apply frame
)
5404 (window--pixel-to-total frame
)
5405 (run-window-configuration-change-hook frame
))
5406 ;; Balance horizontally.
5407 (window--resize-reset (window-frame window
) t
)
5408 (balance-windows-1 window t
)
5409 (when (window--resize-apply-p frame t
)
5410 (window-resize-apply frame t
)
5411 (window--pixel-to-total frame t
)
5412 (run-window-configuration-change-hook frame
))))
5414 (defun window-fixed-size-p (&optional window direction
)
5415 "Return t if WINDOW cannot be resized in DIRECTION.
5416 WINDOW defaults to the selected window. DIRECTION can be
5417 nil (i.e. any), `height' or `width'."
5418 (with-current-buffer (window-buffer window
)
5419 (when (and (boundp 'window-size-fixed
) window-size-fixed
)
5421 (member (cons direction window-size-fixed
)
5422 '((height . width
) (width . height
))))))))
5424 ;;; A different solution to balance-windows.
5425 (defvar window-area-factor
1
5426 "Factor by which the window area should be over-estimated.
5427 This is used by `balance-windows-area'.
5428 Changing this globally has no effect.")
5429 (make-variable-buffer-local 'window-area-factor
)
5431 (defun balance-windows-area-adjust (window delta horizontal pixelwise
)
5432 "Wrapper around `window-resize' with error checking.
5433 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
5434 ;; `window-resize' may fail if delta is too large.
5435 (while (>= (abs delta
) 1)
5438 ;; It was wrong to use `window-resize' here. Somehow
5439 ;; `balance-windows-area' depends on resizing windows
5441 (adjust-window-trailing-edge window delta horizontal pixelwise
)
5444 ;;(message "adjust: %s" (error-message-string err))
5445 (setq delta
(/ delta
2))))))
5447 (defun balance-windows-area ()
5448 "Make all visible windows the same area (approximately).
5449 See also `window-area-factor' to change the relative size of
5452 (let* ((unchanged 0) (carry 0) (round 0)
5453 ;; Remove fixed-size windows.
5454 (wins (delq nil
(mapcar (lambda (win)
5455 (if (not (window-fixed-size-p win
)) win
))
5456 (window-list nil
'nomini
))))
5458 (pixelwise window-resize-pixelwise
)
5460 ;; Resizing a window changes the size of surrounding windows in complex
5461 ;; ways, so it's difficult to balance them all. The introduction of
5462 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
5463 ;; very difficult to do. `balance-window' above takes an off-line
5464 ;; approach: get the whole window tree, then balance it, then try to
5465 ;; adjust the windows so they fit the result.
5466 ;; Here, instead, we take a "local optimization" approach, where we just
5467 ;; go through all the windows several times until nothing needs to be
5468 ;; changed. The main problem with this approach is that it's difficult
5469 ;; to make sure it terminates, so we use some heuristic to try and break
5470 ;; off infinite loops.
5471 ;; After a round without any change, we allow a second, to give a chance
5472 ;; to the carry to propagate a minor imbalance from the end back to
5474 (while (< unchanged
2)
5475 ;; (message "New round")
5476 (setq unchanged
(1+ unchanged
) round
(1+ round
))
5479 (while (progn (setq next
(next-window next
))
5480 (window-fixed-size-p next
)))
5481 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
5483 (< (car (window-pixel-edges win
)) (car (window-pixel-edges next
))))
5484 (areadiff (/ (- (* (window-size next nil pixelwise
)
5485 (window-size next t pixelwise
)
5486 (buffer-local-value 'window-area-factor
5487 (window-buffer next
)))
5488 (* (window-size win nil pixelwise
)
5489 (window-size win t pixelwise
)
5490 (buffer-local-value 'window-area-factor
5491 (window-buffer win
))))
5492 (max (buffer-local-value 'window-area-factor
5493 (window-buffer win
))
5494 (buffer-local-value 'window-area-factor
5495 (window-buffer next
)))))
5497 (+ (window-size win nil pixelwise
)
5498 (window-size next nil pixelwise
))
5499 (+ (window-size win t pixelwise
)
5500 (window-size next t pixelwise
))))
5501 (diff (/ areadiff edgesize
)))
5503 ;; Maybe diff is actually closer to 1 than to 0.
5504 (setq diff
(/ (* 3 areadiff
) (* 2 edgesize
))))
5505 (when (and (zerop diff
) (not (zerop areadiff
)))
5506 (setq diff
(/ (+ areadiff carry
) edgesize
))
5507 ;; Change things smoothly.
5508 (if (or (> diff
1) (< diff -
1)) (setq diff
(/ diff
2))))
5510 ;; Make sure negligible differences don't accumulate to
5511 ;; become significant.
5512 (setq carry
(+ carry areadiff
))
5513 ;; This used `adjust-window-trailing-edge' before and uses
5514 ;; `window-resize' now. Error wrapping is still needed.
5515 (balance-windows-area-adjust win diff horiz pixelwise
)
5517 (let ((change (cons win
(window-pixel-edges win
))))
5518 ;; If the same change has been seen already for this window,
5519 ;; we're most likely in an endless loop, so don't count it as
5521 (unless (member change changelog
)
5522 (push change changelog
)
5523 (setq unchanged
0 carry
0)))))))
5524 ;; We've now basically balanced all the windows.
5525 ;; But there may be some minor off-by-one imbalance left over,
5526 ;; so let's do some fine tuning.
5527 ;; (bw-finetune wins)
5528 ;; (message "Done in %d rounds" round)
5531 ;;; Window states, how to get them and how to put them in a window.
5532 (defun window--state-get-1 (window &optional writable
)
5533 "Helper function for `window-state-get'."
5536 ((window-top-child window
) 'vc
)
5537 ((window-left-child window
) 'hc
)
5539 (buffer (window-buffer window
))
5540 (selected (eq window
(selected-window)))
5543 ,@(unless (window-next-sibling window
) `((last . t
)))
5544 (pixel-width .
,(window-pixel-width window
))
5545 (pixel-height .
,(window-pixel-height window
))
5546 (total-width .
,(window-total-width window
))
5547 (total-height .
,(window-total-height window
))
5548 (normal-height .
,(window-normal-size window
))
5549 (normal-width .
,(window-normal-size window t
))
5550 ,@(unless (window-live-p window
)
5551 `((combination-limit .
,(window-combination-limit window
))))
5552 ,@(let ((parameters (window-parameters window
))
5554 ;; Make copies of those window parameters whose
5555 ;; persistence property is `writable' if WRITABLE is
5556 ;; non-nil and non-nil if WRITABLE is nil.
5557 (dolist (par parameters
)
5558 (let ((pers (cdr (assq (car par
)
5559 window-persistent-parameters
))))
5560 (when (and pers
(or (not writable
) (eq pers
'writable
)))
5561 (setq list
(cons (cons (car par
) (cdr par
)) list
)))))
5562 ;; Add `clone-of' parameter if necessary.
5563 (let ((pers (cdr (assq 'clone-of
5564 window-persistent-parameters
))))
5565 (when (and pers
(or (not writable
) (eq pers
'writable
))
5566 (not (assq 'clone-of list
)))
5567 (setq list
(cons (cons 'clone-of window
) list
))))
5569 `((parameters .
,list
))))
5571 ;; All buffer related things go in here.
5572 (let ((point (window-point window
))
5573 (start (window-start window
)))
5575 ,(buffer-name buffer
)
5576 (selected .
,selected
)
5577 (hscroll .
,(window-hscroll window
))
5578 (fringes .
,(window-fringes window
))
5579 (margins .
,(window-margins window
))
5580 (scroll-bars .
,(window-scroll-bars window
))
5581 (vscroll .
,(window-vscroll window
))
5582 (dedicated .
,(window-dedicated-p window
))
5583 (point .
,(if writable
5585 (with-current-buffer buffer
5588 'window-point-insertion-type
5590 (start .
,(if writable
5592 (with-current-buffer buffer
5593 (copy-marker start
))))))))))
5595 (when (memq type
'(vc hc
))
5597 (setq window
(window-child window
))
5599 (setq list
(cons (window--state-get-1 window writable
) list
))
5600 (setq window
(window-right window
)))
5602 (append head tail
)))
5604 (defun window-state-get (&optional window writable
)
5605 "Return state of WINDOW as a Lisp object.
5606 WINDOW can be any window and defaults to the root window of the
5609 Optional argument WRITABLE non-nil means do not use markers for
5610 sampling `window-point' and `window-start'. Together, WRITABLE
5611 and the variable `window-persistent-parameters' specify which
5612 window parameters are saved by this function. WRITABLE should be
5613 non-nil when the return value shall be written to a file and read
5614 back in another session. Otherwise, an application may run into
5615 an `invalid-read-syntax' error while attempting to read back the
5618 The return value can be used as argument for `window-state-put'
5619 to put the state recorded here into an arbitrary window. The
5620 value can be also stored on disk and read back in a new session."
5623 (if (window-valid-p window
)
5625 (error "%s is not a live or internal window" window
))
5626 (frame-root-window)))
5627 ;; The return value is a cons whose car specifies some constraints on
5628 ;; the size of WINDOW. The cdr lists the states of the child windows
5631 ;; Frame related things would go into a function, say `frame-state',
5632 ;; calling `window-state-get' to insert the frame's root window.
5633 `((min-height .
,(window-min-size window
))
5634 (min-width .
,(window-min-size window t
))
5635 (min-height-ignore .
,(window-min-size window nil t
))
5636 (min-width-ignore .
,(window-min-size window t t
))
5637 (min-height-safe .
,(window-min-size window nil
'safe
))
5638 (min-width-safe .
,(window-min-size window t
'safe
))
5639 (min-pixel-height .
,(window-min-size window nil nil t
))
5640 (min-pixel-width .
,(window-min-size window t nil t
))
5641 (min-pixel-height-ignore .
,(window-min-size window nil t t
))
5642 (min-pixel-width-ignore .
,(window-min-size window t t t
))
5643 (min-pixel-height-safe .
,(window-min-size window nil
'safe t
))
5644 (min-pixel-width-safe .
,(window-min-size window t
'safe t
)))
5645 (window--state-get-1 window writable
)))
5647 (defvar window-state-put-list nil
5648 "Helper variable for `window-state-put'.")
5650 (defvar window-state-put-stale-windows nil
5651 "Helper variable for `window-state-put'.")
5653 (defun window--state-put-1 (state &optional window ignore totals pixelwise
)
5654 "Helper function for `window-state-put'."
5655 (let ((type (car state
)))
5656 (setq state
(cdr state
))
5659 ;; For a leaf window just add unprocessed entries to
5660 ;; `window-state-put-list'.
5661 (push (cons window state
) window-state-put-list
))
5662 ((memq type
'(vc hc
))
5663 (let* ((horizontal (eq type
'hc
))
5664 (total (window-size window horizontal pixelwise
))
5666 (window-combination-limit (cdr (assq 'combination-limit state
)))
5668 (dolist (item state
)
5669 ;; Find the next child window. WINDOW always points to the
5670 ;; real window that we want to fill with what we find here.
5671 (when (memq (car item
) '(leaf vc hc
))
5672 (if (assq 'last item
)
5673 ;; The last child window. Below `window--state-put-1'
5674 ;; will put into it whatever ITEM has in store.
5676 ;; Not the last child window, prepare for splitting
5677 ;; WINDOW. SIZE is the new (and final) size of the old
5683 (cdr (assq (if horizontal
5687 (cdr (assq (if horizontal
5691 ;; Use normalized size and round.
5694 (cdr (assq (if horizontal
'normal-width
'normal-height
)
5697 ;; Use safe sizes, we try to resize later.
5698 (setq size
(max size
5700 (* window-safe-min-width
5702 (frame-char-width (window-frame window
))
5704 (* window-safe-min-height
5706 (frame-char-height (window-frame window
))
5708 (if (window-sizable-p window
(- size
) horizontal
'safe pixelwise
)
5710 (setq new
(split-window-no-error
5711 window size horizontal pixelwise
))
5712 (setq window-combination-limit nil
))
5713 ;; Give up if we can't resize window down to safe sizes.
5714 (error "Cannot resize window %s" window
))
5718 ;; When creating the first child window add for parent
5719 ;; unprocessed entries to `window-state-put-list'.
5720 (setq window-state-put-list
5721 (cons (cons (window-parent window
) state
)
5722 window-state-put-list
))))
5724 ;; Now process the current window (either the one we've just
5725 ;; split or the last child of its parent).
5726 (window--state-put-1 item window ignore totals
)
5727 ;; Continue with the last window split off.
5728 (setq window new
))))))))
5730 (defun window--state-put-2 (ignore pixelwise
)
5731 "Helper function for `window-state-put'."
5732 (dolist (item window-state-put-list
)
5733 (let ((window (car item
))
5734 (combination-limit (cdr (assq 'combination-limit item
)))
5735 (parameters (cdr (assq 'parameters item
)))
5736 (state (cdr (assq 'buffer item
))))
5737 (when combination-limit
5738 (set-window-combination-limit window combination-limit
))
5739 ;; Reset window's parameters and assign saved ones (we might want
5740 ;; a `remove-window-parameters' function here).
5741 (dolist (parameter (window-parameters window
))
5742 (set-window-parameter window
(car parameter
) nil
))
5744 (dolist (parameter parameters
)
5745 (set-window-parameter window
(car parameter
) (cdr parameter
))))
5746 ;; Process buffer related state.
5748 (let ((buffer (get-buffer (car state
))))
5750 (with-current-buffer buffer
5751 (set-window-buffer window buffer
)
5752 (set-window-hscroll window
(cdr (assq 'hscroll state
)))
5753 (apply 'set-window-fringes
5754 (cons window
(cdr (assq 'fringes state
))))
5755 (let ((margins (cdr (assq 'margins state
))))
5756 (set-window-margins window
(car margins
) (cdr margins
)))
5757 (let ((scroll-bars (cdr (assq 'scroll-bars state
))))
5758 (set-window-scroll-bars
5759 window
(car scroll-bars
) (nth 2 scroll-bars
)
5760 (nth 3 scroll-bars
) (nth 5 scroll-bars
)))
5761 (set-window-vscroll window
(cdr (assq 'vscroll state
)))
5762 ;; Adjust vertically.
5763 (if (or (memq window-size-fixed
'(t height
))
5764 (window-preserved-size window
))
5765 ;; A fixed height window, try to restore the
5769 (if pixelwise
'pixel-height
'total-height
)
5771 (window-size window nil pixelwise
)))
5773 (when (window--resizable-p
5774 window delta nil nil nil nil nil pixelwise
)
5775 (window-resize window delta nil nil pixelwise
)))
5776 ;; Else check whether the window is not high enough.
5778 (window-min-size window nil ignore pixelwise
))
5780 (- min-size
(window-size window nil pixelwise
))))
5781 (when (and (> delta
0)
5782 (window--resizable-p
5783 window delta nil ignore nil nil nil pixelwise
))
5784 (window-resize window delta nil ignore pixelwise
))))
5785 ;; Adjust horizontally.
5786 (if (or (memq window-size-fixed
'(t width
))
5787 (window-preserved-size window t
))
5788 ;; A fixed width window, try to restore the original
5792 (if pixelwise
'pixel-width
'total-width
)
5794 (window-size window t pixelwise
)))
5796 (when (window--resizable-p
5797 window delta t nil nil nil nil pixelwise
)
5798 (window-resize window delta t nil pixelwise
)))
5799 ;; Else check whether the window is not wide enough.
5800 (let* ((min-size (window-min-size window t ignore pixelwise
))
5801 (delta (- min-size
(window-size window t pixelwise
))))
5802 (when (and (> delta
0)
5803 (window--resizable-p
5804 window delta t ignore nil nil nil pixelwise
))
5805 (window-resize window delta t ignore pixelwise
))))
5806 ;; Set dedicated status.
5807 (set-window-dedicated-p window
(cdr (assq 'dedicated state
)))
5808 ;; Install positions (maybe we should do this after all
5809 ;; windows have been created and sized).
5811 ;; Set 'noforce argument to avoid that window start
5812 ;; overrides window point set below (Bug#24240).
5813 (set-window-start window
(cdr (assq 'start state
)) 'noforce
)
5814 (set-window-point window
(cdr (assq 'point state
))))
5815 ;; Select window if it's the selected one.
5816 (when (cdr (assq 'selected state
))
5817 (select-window window
)))
5818 ;; We don't want to raise an error in case the buffer does
5819 ;; not exist anymore, so we switch to a previous one and
5820 ;; save the window with the intention of deleting it later
5822 (switch-to-prev-buffer window
)
5823 (push window window-state-put-stale-windows
)))))))
5825 (defun window-state-put (state &optional window ignore
)
5826 "Put window state STATE into WINDOW.
5827 STATE should be the state of a window returned by an earlier
5828 invocation of `window-state-get'. Optional argument WINDOW must
5829 specify a valid window and defaults to the selected one. If
5830 WINDOW is not live, replace WINDOW by a live one before putting
5833 Optional argument IGNORE non-nil means ignore minimum window
5834 sizes and fixed size restrictions. IGNORE equal `safe' means
5835 windows can get as small as `window-safe-min-height' and
5836 `window-safe-min-width'."
5837 (setq window-state-put-stale-windows nil
)
5838 (setq window
(window-normalize-window window
))
5840 ;; When WINDOW is internal, reduce it to a live one to put STATE into,
5842 (unless (window-live-p window
)
5843 (let ((root window
))
5844 (setq window
(catch 'live
5845 (walk-window-subtree
5847 (when (and (window-live-p window
)
5848 (not (window-parameter window
'window-side
)))
5849 (throw 'live window
)))
5851 (delete-other-windows-internal window root
)))
5853 (set-window-dedicated-p window nil
)
5855 (let* ((frame (window-frame window
))
5857 ;; We check here (1) whether the total sizes of root window of
5858 ;; STATE and that of WINDOW are equal so we can avoid
5859 ;; calculating new sizes, and (2) if we do have to resize
5860 ;; whether we can do so without violating size restrictions.
5861 (pixelwise (and (cdr (assq 'pixel-width state
))
5862 (cdr (assq 'pixel-height state
))))
5863 (totals (or (and pixelwise
5864 (= (window-pixel-width window
)
5865 (cdr (assq 'pixel-width state
)))
5866 (= (window-pixel-height window
)
5867 (cdr (assq 'pixel-height state
))))
5868 (and (= (window-total-width window
)
5869 (cdr (assq 'total-width state
)))
5870 (= (window-total-height window
)
5871 (cdr (assq 'total-height state
))))))
5872 (min-height (cdr (assq
5873 (if pixelwise
'min-pixel-height
'min-height
)
5875 (min-width (cdr (assq
5876 (if pixelwise
'min-pixel-width
'min-weight
)
5878 (if (and (not totals
)
5879 (or (> min-height
(window-size window nil pixelwise
))
5880 (> min-width
(window-size window t pixelwise
)))
5882 (and (setq min-height
5885 'min-pixel-height-ignore
5891 'min-pixel-width-ignore
5895 (window-size window nil pixelwise
))
5897 (window-size window t pixelwise
)))
5898 (or (not (eq ignore
'safe
))
5899 (and (setq min-height
5902 'min-pixel-height-safe
5908 'min-pixel-width-safe
5912 (window-size window nil pixelwise
))
5914 (window-size window t pixelwise
))))))))
5915 ;; The check above might not catch all errors due to rounding
5916 ;; issues - so IGNORE equal 'safe might not always produce the
5917 ;; minimum possible state. But such configurations hardly make
5919 (error "Window %s too small to accommodate state" window
)
5920 (setq state
(cdr state
))
5921 (setq window-state-put-list nil
)
5922 ;; Work on the windows of a temporary buffer to make sure that
5923 ;; splitting proceeds regardless of any buffer local values of
5924 ;; `window-size-fixed'. Release that buffer after the buffers of
5925 ;; all live windows have been set by `window--state-put-2'.
5927 (set-window-buffer window
(current-buffer))
5928 (window--state-put-1 state window nil totals pixelwise
)
5929 (window--state-put-2 ignore pixelwise
))
5930 (while window-state-put-stale-windows
5931 (let ((window (pop window-state-put-stale-windows
)))
5932 (when (eq (window-deletable-p window
) t
)
5933 (delete-window window
))))
5934 (window--check frame
))))
5936 (defun window-swap-states (&optional window-1 window-2 size
)
5937 "Swap the states of live windows WINDOW-1 and WINDOW-2.
5938 WINDOW-1 must specify a live window and defaults to the selected
5939 one. WINDOW-2 must specify a live window and defaults to the
5940 window following WINDOW-1 in the cyclic ordering of windows,
5941 excluding minibuffer windows and including live windows on all
5944 Optional argument SIZE non-nil means to try swapping the sizes of
5945 WINDOW-1 and WINDOW-2 as well. A value of `height' means to swap
5946 heights only, a value of `width' means to swap widths only, while
5947 t means to swap both widths and heights, if possible. Frames are
5948 not resized by this function."
5950 (setq window-1
(window-normalize-window window-1 t
))
5952 (unless (window-live-p window-2
)
5953 (error "%s is not a live window" window-2
))
5954 (setq window-2
(next-window window-1
'nomini
'visible
)))
5955 (unless (eq window-1 window-2
)
5956 (let* ((height (memq size
'(t height
)))
5957 (width (memq size
'(t width
)))
5958 (state-1 (window-state-get window-1
))
5959 (width-1 (and width
(window-text-width window-1 t
)))
5960 (height-1 (and height
(window-text-height window-1 t
)))
5961 (state-2 (window-state-get window-2
))
5962 (width-2 (and width
(window-text-width window-2 t
)))
5963 (height-2 (and height
(window-text-height window-2 t
)))
5965 ;; Swap basic states.
5966 (window-state-put state-1 window-2 t
)
5967 (window-state-put state-2 window-1 t
)
5968 ;; Swap overlays with `window' property.
5969 (with-current-buffer (window-buffer window-1
)
5970 (dolist (overlay (overlays-in (point-min) (point-max)))
5971 (let ((window (overlay-get overlay
'window
)))
5974 ((eq window window-1
)
5975 (overlay-put overlay
'window window-2
))
5976 ((eq window window-2
)
5977 (overlay-put overlay
'window window-1
))))))
5978 (unless (eq (window-buffer window-1
) (window-buffer window-2
))
5979 (with-current-buffer (window-buffer window-2
)
5980 (dolist (overlay (overlays-in (point-min) (point-max)))
5981 (let ((window (overlay-get overlay
'window
)))
5984 ((eq window window-1
)
5985 (overlay-put overlay
'window window-2
))
5986 ((eq window window-2
)
5987 (overlay-put overlay
'window window-1
)))))))
5988 ;; Try to swap window sizes.
5990 (unless (= (setq old
(window-text-width window-1 t
)) width-2
)
5991 (window-resize-no-error window-1
(- width-2 old
) t t t
))
5992 (unless (= (setq old
(window-text-width window-2 t
)) width-1
)
5993 (setq preserved
(window-preserved-size window-1 t
))
5994 (window-preserve-size window-1 t t
)
5995 (window-resize-no-error window-2
(- width-1 old
) t t t
)
5996 (window-preserve-size window-1 t preserved
))
5997 (unless (= (setq old
(window-text-height window-1 t
)) height-2
)
5998 (window-resize-no-error window-1
(- height-2 old
) nil t t
))
5999 (unless (= (setq old
(window-text-height window-2 t
)) height-1
)
6000 (setq preserved
(window-preserved-size window-1
))
6001 (window-preserve-size window-1 nil t
)
6002 (window-resize-no-error window-2
(- height-1 old
) nil t t
)
6003 (window-preserve-size window-1 nil preserved
))))))
6005 (defun display-buffer-record-window (type window buffer
)
6006 "Record information for window used by `display-buffer'.
6007 TYPE specifies the type of the calling operation and must be one
6008 of the symbols `reuse' (when WINDOW existed already and was
6009 reused for displaying BUFFER), `window' (when WINDOW was created
6010 on an already existing frame), or `frame' (when WINDOW was
6011 created on a new frame). WINDOW is the window used for or created
6012 by the `display-buffer' routines. BUFFER is the buffer that
6015 This function installs or updates the quit-restore parameter of
6016 WINDOW. The quit-restore parameter is a list of four elements:
6017 The first element is one of the symbols `window', `frame', `same' or
6018 `other'. The second element is either one of the symbols `window'
6019 or `frame' or a list whose elements are the buffer previously
6020 shown in the window, that buffer's window start and window point,
6021 and the window's height. The third element is the window
6022 selected at the time the parameter was created. The fourth
6026 (if (eq (window-buffer window
) buffer
)
6027 ;; WINDOW shows BUFFER already. Update WINDOW's quit-restore
6028 ;; parameter, if any.
6029 (let ((quit-restore (window-parameter window
'quit-restore
)))
6030 (when (consp quit-restore
)
6031 (setcar quit-restore
'same
)
6032 ;; The selected-window might have changed in
6033 ;; between (Bug#20353).
6034 (unless (or (eq window
(selected-window))
6035 (eq window
(nth 2 quit-restore
)))
6036 (setcar (cddr quit-restore
) (selected-window)))))
6037 ;; WINDOW shows another buffer.
6038 (with-current-buffer (window-buffer window
)
6039 (set-window-parameter
6040 window
'quit-restore
6042 ;; A quadruple of WINDOW's buffer, start, point and height.
6043 (list (current-buffer) (window-start window
)
6044 ;; Preserve window-point-insertion-type (Bug#12588).
6046 (window-point window
) window-point-insertion-type
)
6047 (if (window-combined-p window
)
6048 (window-total-height window
)
6049 (window-total-width window
)))
6050 (selected-window) buffer
)))))
6052 ;; WINDOW has been created on an existing frame.
6053 (set-window-parameter
6054 window
'quit-restore
6055 (list 'window
'window
(selected-window) buffer
)))
6057 ;; WINDOW has been created on a new frame.
6058 (set-window-parameter
6059 window
'quit-restore
6060 (list 'frame
'frame
(selected-window) buffer
)))))
6062 (defcustom display-buffer-function nil
6063 "If non-nil, function to call to handle `display-buffer'.
6064 It will receive two args, the buffer and a flag which if non-nil
6065 means that the currently selected window is not acceptable. It
6066 should choose or create a window, display the specified buffer in
6067 it, and return the window.
6069 The specified function should call `display-buffer-record-window'
6070 with corresponding arguments to set up the quit-restore parameter
6071 of the window used."
6074 (function :tag
"function"))
6077 (make-obsolete-variable 'display-buffer-function
6078 'display-buffer-alist
"24.3")
6080 ;; Eventually, we want to turn this into a defvar; instead of
6081 ;; customizing this, the user should use a `pop-up-frame-parameters'
6082 ;; alist entry in `display-buffer-base-action'.
6083 (defcustom pop-up-frame-alist nil
6084 "Alist of parameters for automatically generated new frames.
6085 If non-nil, the value you specify here is used by the default
6086 `pop-up-frame-function' for the creation of new frames.
6088 Since `pop-up-frame-function' is used by `display-buffer' for
6089 making new frames, any value specified here by default affects
6090 the automatic generation of new frames via `display-buffer' and
6091 all functions based on it. The behavior of `make-frame' is not
6092 affected by this variable."
6093 :type
'(repeat (cons :format
"%v"
6094 (symbol :tag
"Parameter")
6095 (sexp :tag
"Value")))
6098 (defcustom pop-up-frame-function
6099 (lambda () (make-frame pop-up-frame-alist
))
6100 "Function used by `display-buffer' for creating a new frame.
6101 This function is called with no arguments and should return a new
6102 frame. The default value calls `make-frame' with the argument
6103 `pop-up-frame-alist'."
6107 (defcustom special-display-buffer-names nil
6108 "List of names of buffers that should be displayed specially.
6109 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
6110 its name is in this list, displays the buffer in a way specified
6111 by `special-display-function'. `special-display-popup-frame'
6112 \(the default for `special-display-function') usually displays
6113 the buffer in a separate frame made with the parameters specified
6114 by `special-display-frame-alist'. If `special-display-function'
6115 has been set to some other function, that function is called with
6116 the buffer as first, and nil as second argument.
6118 Alternatively, an element of this list can be specified as
6119 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
6120 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
6121 `special-display-popup-frame' will interpret such pairs as frame
6122 parameters when it creates a special frame, overriding the
6123 corresponding values from `special-display-frame-alist'.
6125 As a special case, if FRAME-PARAMETERS contains (same-window . t)
6126 `special-display-popup-frame' displays that buffer in the
6127 selected window. If FRAME-PARAMETERS contains (same-frame . t),
6128 it displays that buffer in a window on the selected frame.
6130 If `special-display-function' specifies some other function than
6131 `special-display-popup-frame', that function is called with the
6132 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
6135 Finally, an element of this list can be also specified as
6136 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
6137 `special-display-popup-frame' will call FUNCTION with the buffer
6138 named BUFFER-NAME as first argument, and OTHER-ARGS as the
6141 Any alternative function specified here is responsible for
6142 setting up the quit-restore parameter of the window used.
6144 If this variable appears \"not to work\", because you added a
6145 name to it but the corresponding buffer is displayed in the
6146 selected window, look at the values of `same-window-buffer-names'
6147 and `same-window-regexps'. Those variables take precedence over
6150 See also `special-display-regexps'."
6152 (choice :tag
"Buffer"
6154 (string :format
"%v")
6155 (cons :tag
"With parameters"
6158 (string :format
"%v")
6159 (repeat :tag
"Parameters"
6161 (symbol :tag
"Parameter")
6162 (sexp :tag
"Value"))))
6163 (list :tag
"With function"
6166 (string :format
"%v")
6167 (function :tag
"Function")
6168 (repeat :tag
"Arguments" (sexp)))))
6171 (make-obsolete-variable 'special-display-buffer-names
'display-buffer-alist
"24.3")
6172 (put 'special-display-buffer-names
'risky-local-variable t
)
6174 (defcustom special-display-regexps nil
6175 "List of regexps saying which buffers should be displayed specially.
6176 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
6177 any regexp in this list matches its name, displays it specially
6178 using `special-display-function'. `special-display-popup-frame'
6179 \(the default for `special-display-function') usually displays
6180 the buffer in a separate frame made with the parameters specified
6181 by `special-display-frame-alist'. If `special-display-function'
6182 has been set to some other function, that function is called with
6183 the buffer as first, and nil as second argument.
6185 Alternatively, an element of this list can be specified as
6186 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
6187 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
6188 `special-display-popup-frame' will then interpret these pairs as
6189 frame parameters when creating a special frame for a buffer whose
6190 name matches REGEXP, overriding the corresponding values from
6191 `special-display-frame-alist'.
6193 As a special case, if FRAME-PARAMETERS contains (same-window . t)
6194 `special-display-popup-frame' displays buffers matching REGEXP in
6195 the selected window. (same-frame . t) in FRAME-PARAMETERS means
6196 to display such buffers in a window on the selected frame.
6198 If `special-display-function' specifies some other function than
6199 `special-display-popup-frame', that function is called with the
6200 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
6203 Finally, an element of this list can be also specified as
6204 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
6205 will then call FUNCTION with the buffer whose name matched
6206 REGEXP as first, and OTHER-ARGS as second argument.
6208 Any alternative function specified here is responsible for
6209 setting up the quit-restore parameter of the window used.
6211 If this variable appears \"not to work\", because you added a
6212 name to it but the corresponding buffer is displayed in the
6213 selected window, look at the values of `same-window-buffer-names'
6214 and `same-window-regexps'. Those variables take precedence over
6217 See also `special-display-buffer-names'."
6219 (choice :tag
"Buffer"
6221 (regexp :format
"%v")
6222 (cons :tag
"With parameters"
6225 (regexp :format
"%v")
6226 (repeat :tag
"Parameters"
6228 (symbol :tag
"Parameter")
6229 (sexp :tag
"Value"))))
6230 (list :tag
"With function"
6233 (regexp :format
"%v")
6234 (function :tag
"Function")
6235 (repeat :tag
"Arguments" (sexp)))))
6238 (make-obsolete-variable 'special-display-regexps
'display-buffer-alist
"24.3")
6239 (put 'special-display-regexps
'risky-local-variable t
)
6241 (defun special-display-p (buffer-name)
6242 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
6243 More precisely, return t if `special-display-buffer-names' or
6244 `special-display-regexps' contain a string entry equaling or
6245 matching BUFFER-NAME. If `special-display-buffer-names' or
6246 `special-display-regexps' contain a list entry whose car equals
6247 or matches BUFFER-NAME, the return value is the cdr of that
6251 ((member buffer-name special-display-buffer-names
)
6253 ((setq tmp
(assoc buffer-name special-display-buffer-names
))
6256 (dolist (regexp special-display-regexps
)
6259 (when (string-match-p regexp buffer-name
)
6261 ((and (consp regexp
) (stringp (car regexp
))
6262 (string-match-p (car regexp
) buffer-name
))
6263 (throw 'found
(cdr regexp
))))))))))
6265 (defcustom special-display-frame-alist
6266 '((height .
14) (width .
80) (unsplittable . t
))
6267 "Alist of parameters for special frames.
6268 Special frames are used for buffers whose names are listed in
6269 `special-display-buffer-names' and for buffers whose names match
6270 one of the regular expressions in `special-display-regexps'.
6272 This variable can be set in your init file, like this:
6274 (setq special-display-frame-alist \\='((width . 80) (height . 20)))
6276 These supersede the values given in `default-frame-alist'."
6277 :type
'(repeat (cons :format
"%v"
6278 (symbol :tag
"Parameter")
6279 (sexp :tag
"Value")))
6281 (make-obsolete-variable 'special-display-frame-alist
'display-buffer-alist
"24.3")
6283 (defun special-display-popup-frame (buffer &optional args
)
6284 "Pop up a frame displaying BUFFER and return its window.
6285 If BUFFER is already displayed in a visible or iconified frame,
6286 raise that frame. Otherwise, display BUFFER in a new frame.
6288 Optional argument ARGS is a list specifying additional
6291 If ARGS is an alist, use it as a list of frame parameters. If
6292 these parameters contain (same-window . t), display BUFFER in
6293 the selected window. If they contain (same-frame . t), display
6294 BUFFER in a window of the selected frame.
6296 If ARGS is a list whose car is a symbol, use (car ARGS) as a
6297 function to do the work. Pass it BUFFER as first argument, and
6298 pass the elements of (cdr ARGS) as the remaining arguments."
6299 (if (and args
(symbolp (car args
)))
6300 (apply (car args
) buffer
(cdr args
))
6301 (let ((window (get-buffer-window buffer
0)))
6303 ;; If we have a window already, make it visible.
6305 (let ((frame (window-frame window
)))
6306 (make-frame-visible frame
)
6308 (display-buffer-record-window 'reuse window buffer
)
6310 ;; Reuse the current window if the user requested it.
6311 (when (cdr (assq 'same-window args
))
6313 (progn (switch-to-buffer buffer nil t
) (selected-window))
6315 ;; Stay on the same frame if requested.
6316 (when (or (cdr (assq 'same-frame args
)) (cdr (assq 'same-window args
)))
6317 (let* ((pop-up-windows t
)
6319 special-display-buffer-names special-display-regexps
)
6320 (display-buffer buffer
)))
6321 ;; If no window yet, make one in a new frame.
6323 (with-current-buffer buffer
6324 (make-frame (append args special-display-frame-alist
))))
6325 (window (frame-selected-window frame
)))
6326 (display-buffer-record-window 'frame window buffer
)
6327 (unless (eq buffer
(window-buffer window
))
6328 (set-window-buffer window buffer
)
6329 (set-window-prev-buffers window nil
))
6330 (set-window-dedicated-p window t
)
6333 (defcustom special-display-function
'special-display-popup-frame
6334 "Function to call for displaying special buffers.
6335 This function is called with two arguments - the buffer and,
6336 optionally, a list - and should return a window displaying that
6337 buffer. The default value usually makes a separate frame for the
6338 buffer using `special-display-frame-alist' to specify the frame
6339 parameters. See the definition of `special-display-popup-frame'
6340 for how to specify such a function.
6342 A buffer is special when its name is either listed in
6343 `special-display-buffer-names' or matches a regexp in
6344 `special-display-regexps'.
6346 The specified function should call `display-buffer-record-window'
6347 with corresponding arguments to set up the quit-restore parameter
6348 of the window used."
6351 (make-obsolete-variable 'special-display-function
'display-buffer-alist
"24.3")
6353 (defcustom same-window-buffer-names nil
6354 "List of names of buffers that should appear in the \"same\" window.
6355 `display-buffer' and `pop-to-buffer' show a buffer whose name is
6356 on this list in the selected rather than some other window.
6358 An element of this list can be a cons cell instead of just a
6359 string. In that case, the cell's car must be a string specifying
6360 the buffer name. This is for compatibility with
6361 `special-display-buffer-names'; the cdr of the cons cell is
6364 See also `same-window-regexps'."
6365 :type
'(repeat (string :format
"%v"))
6368 (defcustom same-window-regexps nil
6369 "List of regexps saying which buffers should appear in the \"same\" window.
6370 `display-buffer' and `pop-to-buffer' show a buffer whose name
6371 matches a regexp on this list in the selected rather than some
6374 An element of this list can be a cons cell instead of just a
6375 string. In that case, the cell's car must be a regexp matching
6376 the buffer name. This is for compatibility with
6377 `special-display-regexps'; the cdr of the cons cell is ignored.
6379 See also `same-window-buffer-names'."
6380 :type
'(repeat (regexp :format
"%v"))
6383 (defun same-window-p (buffer-name)
6384 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
6385 This function returns non-nil if `display-buffer' or
6386 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
6387 selected rather than (as usual) some other window. See
6388 `same-window-buffer-names' and `same-window-regexps'."
6390 ((not (stringp buffer-name
)))
6391 ;; The elements of `same-window-buffer-names' can be buffer
6392 ;; names or cons cells whose cars are buffer names.
6393 ((member buffer-name same-window-buffer-names
))
6394 ((assoc buffer-name same-window-buffer-names
))
6396 (dolist (regexp same-window-regexps
)
6397 ;; The elements of `same-window-regexps' can be regexps
6398 ;; or cons cells whose cars are regexps.
6399 (when (or (and (stringp regexp
)
6400 (string-match-p regexp buffer-name
))
6401 (and (consp regexp
) (stringp (car regexp
))
6402 (string-match-p (car regexp
) buffer-name
)))
6403 (throw 'found t
)))))))
6405 (defcustom pop-up-frames nil
6406 "Whether `display-buffer' should make a separate frame.
6407 If nil, never make a separate frame.
6408 If the value is `graphic-only', make a separate frame
6409 on graphic displays only.
6410 Any other non-nil value means always make a separate frame."
6412 (const :tag
"Never" nil
)
6413 (const :tag
"On graphic displays only" graphic-only
)
6414 (const :tag
"Always" t
))
6417 (defcustom display-buffer-reuse-frames nil
6418 "Non-nil means `display-buffer' should reuse frames.
6419 If the buffer in question is already displayed in a frame, raise
6425 (make-obsolete-variable
6426 'display-buffer-reuse-frames
6427 "use a `reusable-frames' alist entry in `display-buffer-alist'."
6430 (defcustom pop-up-windows t
6431 "Non-nil means `display-buffer' should make a new window."
6435 (defcustom split-window-preferred-function
'split-window-sensibly
6436 "Function called by `display-buffer' routines to split a window.
6437 This function is called with a window as single argument and is
6438 supposed to split that window and return the new window. If the
6439 window can (or shall) not be split, it is supposed to return nil.
6440 The default is to call the function `split-window-sensibly' which
6441 tries to split the window in a way which seems most suitable.
6442 You can customize the options `split-height-threshold' and/or
6443 `split-width-threshold' in order to have `split-window-sensibly'
6444 prefer either vertical or horizontal splitting.
6446 If you set this to any other function, bear in mind that the
6447 `display-buffer' routines may call this function two times. The
6448 argument of the first call is the largest window on its frame.
6449 If that call fails to return a live window, the function is
6450 called again with the least recently used window as argument. If
6451 that call fails too, `display-buffer' will use an existing window
6452 to display its buffer.
6454 The window selected at the time `display-buffer' was invoked is
6455 still selected when this function is called. Hence you can
6456 compare the window argument with the value of `selected-window'
6457 if you intend to split the selected window instead or if you do
6458 not want to split the selected window."
6463 (defcustom split-height-threshold
80
6464 "Minimum height for splitting windows sensibly.
6465 If this is an integer, `split-window-sensibly' may split a window
6466 vertically only if it has at least this many lines. If this is
6467 nil, `split-window-sensibly' is not allowed to split a window
6468 vertically. If, however, a window is the only window on its
6469 frame, or all the other ones are dedicated,
6470 `split-window-sensibly' may split it vertically disregarding the
6471 value of this variable."
6472 :type
'(choice (const nil
) (integer :tag
"lines"))
6476 (defcustom split-width-threshold
160
6477 "Minimum width for splitting windows sensibly.
6478 If this is an integer, `split-window-sensibly' may split a window
6479 horizontally only if it has at least this many columns. If this
6480 is nil, `split-window-sensibly' is not allowed to split a window
6482 :type
'(choice (const nil
) (integer :tag
"columns"))
6486 (defun window-splittable-p (window &optional horizontal
)
6487 "Return non-nil if `split-window-sensibly' may split WINDOW.
6488 Optional argument HORIZONTAL nil or omitted means check whether
6489 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
6490 non-nil means check whether WINDOW may be split horizontally.
6492 WINDOW may be split vertically when the following conditions
6494 - `window-size-fixed' is either nil or equals `width' for the
6496 - `split-height-threshold' is an integer and WINDOW is at least as
6497 high as `split-height-threshold'.
6498 - When WINDOW is split evenly, the emanating windows are at least
6499 `window-min-height' lines tall and can accommodate at least one
6500 line plus - if WINDOW has one - a mode line.
6502 WINDOW may be split horizontally when the following conditions
6504 - `window-size-fixed' is either nil or equals `height' for the
6506 - `split-width-threshold' is an integer and WINDOW is at least as
6507 wide as `split-width-threshold'.
6508 - When WINDOW is split evenly, the emanating windows are at least
6509 `window-min-width' or two (whichever is larger) columns wide."
6510 (when (and (window-live-p window
)
6511 (not (window-parameter window
'window-side
)))
6512 (with-current-buffer (window-buffer window
)
6514 ;; A window can be split horizontally when its width is not
6515 ;; fixed, it is at least `split-width-threshold' columns wide
6516 ;; and at least twice as wide as `window-min-width' and 2 (the
6517 ;; latter value is hardcoded).
6518 (and (memq window-size-fixed
'(nil height
))
6519 ;; Testing `window-full-width-p' here hardly makes any
6520 ;; sense nowadays. This can be done more intuitively by
6521 ;; setting up `split-width-threshold' appropriately.
6522 (numberp split-width-threshold
)
6523 (>= (window-width window
)
6524 (max split-width-threshold
6525 (* 2 (max window-min-width
2)))))
6526 ;; A window can be split vertically when its height is not
6527 ;; fixed, it is at least `split-height-threshold' lines high,
6528 ;; and it is at least twice as high as `window-min-height' and 2
6529 ;; if it has a mode line or 1.
6530 (and (memq window-size-fixed
'(nil width
))
6531 (numberp split-height-threshold
)
6532 (>= (window-height window
)
6533 (max split-height-threshold
6534 (* 2 (max window-min-height
6535 (if mode-line-format
2 1))))))))))
6537 (defun split-window-sensibly (&optional window
)
6538 "Split WINDOW in a way suitable for `display-buffer'.
6539 WINDOW defaults to the currently selected window.
6540 If `split-height-threshold' specifies an integer, WINDOW is at
6541 least `split-height-threshold' lines tall and can be split
6542 vertically, split WINDOW into two windows one above the other and
6543 return the lower window. Otherwise, if `split-width-threshold'
6544 specifies an integer, WINDOW is at least `split-width-threshold'
6545 columns wide and can be split horizontally, split WINDOW into two
6546 windows side by side and return the window on the right. If this
6547 can't be done either and WINDOW is the only window on its frame,
6548 try to split WINDOW vertically disregarding any value specified
6549 by `split-height-threshold'. If that succeeds, return the lower
6550 window. Return nil otherwise.
6552 By default `display-buffer' routines call this function to split
6553 the largest or least recently used window. To change the default
6554 customize the option `split-window-preferred-function'.
6556 You can enforce this function to not split WINDOW horizontally,
6557 by setting (or binding) the variable `split-width-threshold' to
6558 nil. If, in addition, you set `split-height-threshold' to zero,
6559 chances increase that this function does split WINDOW vertically.
6561 In order to not split WINDOW vertically, set (or bind) the
6562 variable `split-height-threshold' to nil. Additionally, you can
6563 set `split-width-threshold' to zero to make a horizontal split
6564 more likely to occur.
6566 Have a look at the function `window-splittable-p' if you want to
6567 know how `split-window-sensibly' determines whether WINDOW can be
6569 (let ((window (or window
(selected-window))))
6570 (or (and (window-splittable-p window
)
6571 ;; Split window vertically.
6572 (with-selected-window window
6573 (split-window-below)))
6574 (and (window-splittable-p window t
)
6575 ;; Split window horizontally.
6576 (with-selected-window window
6577 (split-window-right)))
6579 ;; If WINDOW is the only usable window on its frame (it is
6580 ;; the only one or, not being the only one, all the other
6581 ;; ones are dedicated) and is not the minibuffer window, try
6582 ;; to split it vertically disregarding the value of
6583 ;; `split-height-threshold'.
6584 (let ((frame (window-frame window
)))
6586 (eq window
(frame-root-window frame
))
6588 (walk-window-tree (lambda (w)
6589 (unless (or (eq w window
)
6590 (window-dedicated-p w
))
6594 (not (window-minibuffer-p window
))
6595 (let ((split-height-threshold 0))
6596 (when (window-splittable-p window
)
6597 (with-selected-window window
6598 (split-window-below))))))))
6600 (defun window--try-to-split-window (window &optional alist
)
6601 "Try to split WINDOW.
6602 Return value returned by `split-window-preferred-function' if it
6603 represents a live window, nil otherwise."
6604 (and (window-live-p window
)
6605 (not (frame-parameter (window-frame window
) 'unsplittable
))
6606 (let* ((window-combination-limit
6607 ;; When `window-combination-limit' equals
6608 ;; `display-buffer' or equals `resize-window' and a
6609 ;; `window-height' or `window-width' alist entry are
6610 ;; present, bind it to t so resizing steals space
6611 ;; preferably from the window that was split.
6612 (if (or (eq window-combination-limit
'display-buffer
)
6613 (and (eq window-combination-limit
'window-size
)
6614 (or (cdr (assq 'window-height alist
))
6615 (cdr (assq 'window-width alist
)))))
6617 window-combination-limit
))
6619 ;; Since `split-window-preferred-function' might
6620 ;; throw an error use `condition-case'.
6622 (funcall split-window-preferred-function window
)
6624 (and (window-live-p new-window
) new-window
))))
6626 (defun window--frame-usable-p (frame)
6627 "Return FRAME if it can be used to display a buffer."
6628 (when (frame-live-p frame
)
6629 (let ((window (frame-root-window frame
)))
6630 ;; `frame-root-window' may be an internal window which is considered
6631 ;; "dead" by `window-live-p'. Hence if `window' is not live we
6632 ;; implicitly know that `frame' has a visible window we can use.
6633 (unless (and (window-live-p window
)
6634 (or (window-minibuffer-p window
)
6635 ;; If the window is soft-dedicated, the frame is usable.
6636 ;; Actually, even if the window is really dedicated,
6637 ;; the frame is still usable by splitting it.
6638 ;; At least Emacs-22 allowed it, and it is desirable
6639 ;; when displaying same-frame windows.
6640 nil
; (eq t (window-dedicated-p window))
6644 (defcustom even-window-sizes t
6645 "If non-nil `display-buffer' will try to even window sizes.
6646 Otherwise `display-buffer' will leave the window configuration
6647 alone. Special values are `height-only' to even heights only and
6648 `width-only' to even widths only. Any other value means to even
6651 (const :tag
"Never" nil
)
6652 (const :tag
"Side-by-side windows only" width-only
)
6653 (const :tag
"Windows above or below only" height-only
)
6654 (const :tag
"Always" t
))
6657 (defvaralias 'even-window-heights
'even-window-sizes
)
6659 (defun window--even-window-sizes (window)
6660 "Even sizes of WINDOW and selected window.
6661 Even only if these windows are the only children of their parent,
6662 `even-window-sizes' has the appropriate value and the selected
6663 window is larger than WINDOW."
6664 (when (and (= (window-child-count (window-parent window
)) 2)
6665 (eq (window-parent) (window-parent window
)))
6667 ((and (not (memq even-window-sizes
'(nil height-only
)))
6668 (window-combined-p window t
)
6669 (> (window-total-width) (window-total-width window
)))
6672 (/ (- (window-total-width window
) (window-total-width)) 2) t
)
6674 ((and (not (memq even-window-sizes
'(nil width-only
)))
6675 (window-combined-p window
)
6676 (> (window-total-height) (window-total-height window
)))
6679 (/ (- (window-total-height window
) (window-total-height)) 2))
6682 (defun window--display-buffer (buffer window type
&optional alist dedicated
)
6683 "Display BUFFER in WINDOW.
6684 TYPE must be one of the symbols `reuse', `window' or `frame' and
6685 is passed unaltered to `display-buffer-record-window'. ALIST is
6686 the alist argument of `display-buffer'. Set `window-dedicated-p'
6687 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
6689 (when (and (buffer-live-p buffer
) (window-live-p window
))
6690 (display-buffer-record-window type window buffer
)
6691 (unless (eq buffer
(window-buffer window
))
6692 (set-window-dedicated-p window nil
)
6693 (set-window-buffer window buffer
))
6695 (set-window-dedicated-p window dedicated
))
6696 (when (memq type
'(window frame
))
6697 (set-window-prev-buffers window nil
))
6698 (let ((quit-restore (window-parameter window
'quit-restore
))
6699 (height (cdr (assq 'window-height alist
)))
6700 (width (cdr (assq 'window-width alist
)))
6701 (size (cdr (assq 'window-size alist
)))
6702 (preserve-size (cdr (assq 'preserve-size alist
))))
6704 ((or (eq type
'frame
)
6705 (and (eq (car quit-restore
) 'same
)
6706 (eq (nth 1 quit-restore
) 'frame
)))
6707 ;; Adjust size of frame if asked for.
6711 (let ((width (car size
))
6713 (frame (window-frame window
)))
6714 (when (and (numberp width
) (numberp height
))
6716 frame
(+ (frame-height frame
)
6717 (- height
(window-total-height window
))))
6719 frame
(+ (frame-width frame
)
6720 (- width
(window-total-width window
)))))))
6722 (ignore-errors (funcall size window
)))))
6723 ((or (eq type
'window
)
6724 (and (eq (car quit-restore
) 'same
)
6725 (eq (nth 1 quit-restore
) 'window
)))
6726 ;; Adjust height of window if asked for.
6731 (if (integerp height
)
6734 (* (window-total-height (frame-root-window window
))
6736 (delta (- new-height
(window-total-height window
))))
6737 (when (and (window--resizable-p window delta nil
'safe
)
6738 (window-combined-p window
))
6739 (window-resize window delta nil
'safe
))))
6741 (ignore-errors (funcall height window
))))
6742 ;; Adjust width of window if asked for.
6747 (if (integerp width
)
6750 (* (window-total-width (frame-root-window window
))
6752 (delta (- new-width
(window-total-width window
))))
6753 (when (and (window--resizable-p window delta t
'safe
)
6754 (window-combined-p window t
))
6755 (window-resize window delta t
'safe
))))
6757 (ignore-errors (funcall width window
))))
6758 ;; Preserve window size if asked for.
6759 (when (consp preserve-size
)
6760 (window-preserve-size window t
(car preserve-size
))
6761 (window-preserve-size window nil
(cdr preserve-size
)))))
6762 ;; Assign any window parameters specified.
6763 (let ((parameters (cdr (assq 'window-parameters alist
))))
6764 (dolist (parameter parameters
)
6765 (set-window-parameter
6766 window
(car parameter
) (cdr parameter
)))))
6769 (defun window--maybe-raise-frame (frame)
6770 (make-frame-visible frame
)
6771 (unless (or (frame-parameter frame
'no-focus-on-map
)
6772 ;; Don't raise frames that should not get focus.
6773 (frame-parameter frame
'no-accept-focus
)
6774 ;; Assume the selected frame is already visible enough.
6775 (eq frame
(selected-frame))
6776 ;; Assume the frame from which we invoked the
6777 ;; minibuffer is visible.
6778 (and (minibuffer-window-active-p (selected-window))
6779 (eq frame
(window-frame (minibuffer-selected-window)))))
6780 (raise-frame frame
)))
6782 ;; FIXME: Not implemented.
6783 ;; FIXME: By the way, there could be more levels of dedication:
6784 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
6785 ;; the window hasn't been used for something else yet.
6786 ;; - `soft' (`softly') dedicated only allows reuse when asked explicitly.
6787 ;; - `strongly' never allows reuse.
6788 (defvar display-buffer-mark-dedicated nil
6789 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
6790 The actual non-nil value of this variable will be copied to the
6791 `window-dedicated-p' flag.")
6793 (defconst display-buffer--action-function-custom-type
6794 '(choice :tag
"Function"
6795 (const :tag
"--" ignore
) ; default for insertion
6796 (const display-buffer-reuse-window
)
6797 (const display-buffer-pop-up-window
)
6798 (const display-buffer-same-window
)
6799 (const display-buffer-pop-up-frame
)
6800 (const display-buffer-in-child-frame
)
6801 (const display-buffer-below-selected
)
6802 (const display-buffer-at-bottom
)
6803 (const display-buffer-in-previous-window
)
6804 (const display-buffer-use-some-window
)
6805 (const display-buffer-use-some-frame
)
6806 (function :tag
"Other function"))
6807 "Custom type for `display-buffer' action functions.")
6809 (defconst display-buffer--action-custom-type
6810 `(cons :tag
"Action"
6811 (choice :tag
"Action functions"
6812 ,display-buffer--action-function-custom-type
6814 :tag
"List of functions"
6815 ,display-buffer--action-function-custom-type
))
6816 (alist :tag
"Action arguments"
6818 :value-type
(sexp :tag
"Value")))
6819 "Custom type for `display-buffer' actions.")
6821 (defvar display-buffer-overriding-action
'(nil . nil
)
6822 "Overriding action to perform to display a buffer.
6823 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6824 function or a list of functions. Each function should accept two
6825 arguments: a buffer to display and an alist similar to ALIST.
6826 See `display-buffer' for details.")
6827 (put 'display-buffer-overriding-action
'risky-local-variable t
)
6829 (defcustom display-buffer-alist nil
6830 "Alist of conditional actions for `display-buffer'.
6831 This is a list of elements (CONDITION . ACTION), where:
6833 CONDITION is either a regexp matching buffer names, or a
6834 function that takes two arguments - a buffer name and the
6835 ACTION argument of `display-buffer' - and returns a boolean.
6837 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
6838 function or a list of functions. Each such function should
6839 accept two arguments: a buffer to display and an alist of the
6840 same form as ALIST. See `display-buffer' for details.
6842 `display-buffer' scans this alist until it either finds a
6843 matching regular expression or the function specified by a
6844 condition returns non-nil. In any of these cases, it adds the
6845 associated action to the list of actions it will try."
6846 :type
`(alist :key-type
6847 (choice :tag
"Condition"
6849 (function :tag
"Matcher function"))
6850 :value-type
,display-buffer--action-custom-type
)
6855 (defcustom display-buffer-base-action
'(nil . nil
)
6856 "User-specified default action for `display-buffer'.
6857 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6858 function or a list of functions. Each function should accept two
6859 arguments: a buffer to display and an alist similar to ALIST.
6860 See `display-buffer' for details."
6861 :type display-buffer--action-custom-type
6866 (defconst display-buffer-fallback-action
6867 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
6868 display-buffer-reuse-window
6869 display-buffer--maybe-pop-up-frame-or-window
6870 display-buffer-in-previous-window
6871 display-buffer-use-some-window
6872 ;; If all else fails, pop up a new frame.
6873 display-buffer-pop-up-frame
))
6874 "Default fallback action for `display-buffer'.
6875 This is the action used by `display-buffer' if no other actions
6876 specified, e.g. by the user options `display-buffer-alist' or
6877 `display-buffer-base-action'. See `display-buffer'.")
6878 (put 'display-buffer-fallback-action
'risky-local-variable t
)
6880 (defun display-buffer-assq-regexp (buffer-name alist action
)
6881 "Retrieve ALIST entry corresponding to BUFFER-NAME.
6882 ACTION is the action argument passed to `display-buffer'."
6884 (dolist (entry alist
)
6885 (let ((key (car entry
)))
6886 (when (or (and (stringp key
)
6887 (string-match-p key buffer-name
))
6888 (and (functionp key
)
6889 (funcall key buffer-name action
)))
6890 (throw 'match
(cdr entry
)))))))
6892 (defvar display-buffer--same-window-action
6893 '(display-buffer-same-window
6894 (inhibit-same-window . nil
))
6895 "A `display-buffer' action for displaying in the same window.")
6896 (put 'display-buffer--same-window-action
'risky-local-variable t
)
6898 (defvar display-buffer--other-frame-action
6899 '((display-buffer-reuse-window
6900 display-buffer-pop-up-frame
)
6901 (reusable-frames .
0)
6902 (inhibit-same-window . t
))
6903 "A `display-buffer' action for displaying in another frame.")
6904 (put 'display-buffer--other-frame-action
'risky-local-variable t
)
6906 (defun display-buffer (buffer-or-name &optional action frame
)
6907 "Display BUFFER-OR-NAME in some window, without selecting it.
6908 BUFFER-OR-NAME must be a buffer or the name of an existing
6909 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
6910 or nil if no such window is found.
6912 Optional argument ACTION, if non-nil, should specify a display
6913 action. Its form is described below.
6915 Optional argument FRAME, if non-nil, acts like an additional
6916 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
6917 specifying the frame(s) to search for a window that is already
6918 displaying the buffer. See `display-buffer-reuse-window'.
6920 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
6921 where FUNCTION is either a function or a list of functions, and
6922 ALIST is an arbitrary association list (alist).
6924 Each such FUNCTION should accept two arguments: the buffer to
6925 display and an alist. Based on those arguments, it should
6926 display the buffer and return the window. If the caller is
6927 prepared to handle the case of not displaying the buffer
6928 and returning nil from `display-buffer' it should pass
6929 \(allow-no-window . t) as an element of the ALIST.
6931 The `display-buffer' function builds a function list and an alist
6932 by combining the functions and alists specified in
6933 `display-buffer-overriding-action', `display-buffer-alist', the
6934 ACTION argument, `display-buffer-base-action', and
6935 `display-buffer-fallback-action' (in order). Then it calls each
6936 function in the combined function list in turn, passing the
6937 buffer as the first argument and the combined alist as the second
6938 argument, until one of the functions returns non-nil.
6940 If ACTION is nil, the function list and the alist are built using
6941 only the other variables mentioned above.
6943 Available action functions include:
6944 `display-buffer-same-window'
6945 `display-buffer-reuse-window'
6946 `display-buffer-pop-up-frame'
6947 `display-buffer-in-child-frame'
6948 `display-buffer-pop-up-window'
6949 `display-buffer-in-previous-window'
6950 `display-buffer-use-some-window'
6951 `display-buffer-use-some-frame'
6953 Recognized alist entries include:
6955 `inhibit-same-window' -- A non-nil value prevents the same
6956 window from being used for display.
6958 `inhibit-switch-frame' -- A non-nil value prevents any other
6959 frame from being raised or selected,
6960 even if the window is displayed there.
6962 `reusable-frames' -- Value specifies frame(s) to search for a
6963 window that already displays the buffer.
6964 See `display-buffer-reuse-window'.
6966 `pop-up-frame-parameters' -- Value specifies an alist of frame
6967 parameters to give a new frame, if
6970 `window-height' -- Value specifies either an integer (the number
6971 of lines of a new window), a floating point number (the
6972 fraction of a new window with respect to the height of the
6973 frame's root window) or a function to be called with one
6974 argument - a new window. The function is supposed to adjust
6975 the height of the window; its return value is ignored.
6976 Suitable functions are `shrink-window-if-larger-than-buffer'
6977 and `fit-window-to-buffer'.
6979 `window-width' -- Value specifies either an integer (the number
6980 of columns of a new window), a floating point number (the
6981 fraction of a new window with respect to the width of the
6982 frame's root window) or a function to be called with one
6983 argument - a new window. The function is supposed to adjust
6984 the width of the window; its return value is ignored.
6986 `allow-no-window' -- A non-nil value indicates readiness for the case
6987 of not displaying the buffer and FUNCTION can safely return
6988 a non-window value to suppress displaying.
6990 `preserve-size' -- Value should be either (t . nil) to
6991 preserve the width of the window, (nil . t) to preserve its
6992 height or (t . t) to preserve both.
6994 `window-parameters' -- Value specifies an alist of window
6995 parameters to give the chosen window.
6997 The ACTION argument to `display-buffer' can also have a non-nil
6998 and non-list value. This means to display the buffer in a window
6999 other than the selected one, even if it is already displayed in
7000 the selected window. If called interactively with a prefix
7001 argument, ACTION is t."
7002 (interactive (list (read-buffer "Display buffer: " (other-buffer))
7003 (if current-prefix-arg t
)))
7004 (let ((buffer (if (bufferp buffer-or-name
)
7006 (get-buffer buffer-or-name
)))
7007 ;; Make sure that when we split windows the old window keeps
7008 ;; point, bug#14829.
7009 (split-window-keep-point t
)
7010 ;; Handle the old form of the first argument.
7011 (inhibit-same-window (and action
(not (listp action
)))))
7012 (unless (listp action
) (setq action nil
))
7013 (if display-buffer-function
7014 ;; If `display-buffer-function' is defined, let it do the job.
7015 (funcall display-buffer-function buffer inhibit-same-window
)
7016 ;; Otherwise, use the defined actions.
7018 (display-buffer-assq-regexp
7019 (buffer-name buffer
) display-buffer-alist action
))
7020 (special-action (display-buffer--special-action buffer
))
7021 ;; Extra actions from the arguments to this function:
7023 (cons nil
(append (if inhibit-same-window
7024 '((inhibit-same-window . t
)))
7026 `((reusable-frames .
,frame
))))))
7027 ;; Construct action function list and action alist.
7028 (actions (list display-buffer-overriding-action
7029 user-action special-action action extra-action
7030 display-buffer-base-action
7031 display-buffer-fallback-action
))
7032 (functions (apply 'append
7035 (if (functionp x
) (list x
) x
))
7037 (alist (apply 'append
(mapcar 'cdr actions
)))
7039 (unless (buffer-live-p buffer
)
7040 (error "Invalid buffer"))
7041 (while (and functions
(not window
))
7042 (setq window
(funcall (car functions
) buffer alist
)
7043 functions
(cdr functions
)))
7044 (and (windowp window
) window
)))))
7046 (defun display-buffer-other-frame (buffer)
7047 "Display buffer BUFFER preferably in another frame.
7048 This uses the function `display-buffer' as a subroutine; see
7049 its documentation for additional customization information."
7050 (interactive "BDisplay buffer in other frame: ")
7051 (display-buffer buffer display-buffer--other-frame-action t
))
7053 ;;; `display-buffer' action functions:
7055 (defun display-buffer-use-some-frame (buffer alist
)
7056 "Display BUFFER in an existing frame that meets a predicate
7057 \(by default any frame other than the current frame). If
7058 successful, return the window used; otherwise return nil.
7060 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
7063 If ALIST has a non-nil `frame-predicate' entry, its value is a
7064 function taking one argument (a frame), returning non-nil if the
7065 frame is a candidate; this function replaces the default
7068 If ALIST has a non-nil `inhibit-same-window' entry, avoid using
7069 the currently selected window (only useful with a frame-predicate
7070 that allows the selected frame)."
7071 (let* ((predicate (or (cdr (assq 'frame-predicate alist
))
7074 (not (eq frame
(selected-frame)))
7075 (not (window-dedicated-p
7077 (get-lru-window frame
)
7078 (frame-first-window frame
)))))
7080 (frame (car (filtered-frame-list predicate
)))
7081 (window (and frame
(get-lru-window frame nil
(cdr (assq 'inhibit-same-window alist
))))))
7084 (window--display-buffer
7085 buffer window
'frame alist display-buffer-mark-dedicated
)
7086 (unless (cdr (assq 'inhibit-switch-frame alist
))
7087 (window--maybe-raise-frame frame
))))))
7089 (defun display-buffer-same-window (buffer alist
)
7090 "Display BUFFER in the selected window.
7091 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
7092 if the selected window is a minibuffer window or is dedicated to
7093 another buffer; in that case, return nil. Otherwise, return the
7095 (unless (or (cdr (assq 'inhibit-same-window alist
))
7096 (window-minibuffer-p)
7097 (window-dedicated-p))
7098 (window--display-buffer buffer
(selected-window) 'reuse alist
)))
7100 (defun display-buffer--maybe-same-window (buffer alist
)
7101 "Conditionally display BUFFER in the selected window.
7102 If `same-window-p' returns non-nil for BUFFER's name, call
7103 `display-buffer-same-window' and return its value. Otherwise,
7105 (and (same-window-p (buffer-name buffer
))
7106 (display-buffer-same-window buffer alist
)))
7108 (defun display-buffer-reuse-window (buffer alist
)
7109 "Return a window that is already displaying BUFFER.
7110 Return nil if no usable window is found.
7112 If ALIST has a non-nil `inhibit-same-window' entry, the selected
7113 window is not eligible for reuse.
7115 If ALIST contains a `reusable-frames' entry, its value determines
7116 which frames to search for a reusable window:
7117 nil -- the selected frame (actually the last non-minibuffer frame)
7118 A frame -- just that frame
7119 `visible' -- all visible frames
7120 0 -- all frames on the current terminal
7123 If ALIST contains no `reusable-frames' entry, search just the
7124 selected frame if `display-buffer-reuse-frames' and
7125 `pop-up-frames' are both nil; search all frames on the current
7126 terminal if either of those variables is non-nil.
7128 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7129 event that a window on another frame is chosen, avoid raising
7131 (let* ((alist-entry (assq 'reusable-frames alist
))
7132 (frames (cond (alist-entry (cdr alist-entry
))
7133 ((if (eq pop-up-frames
'graphic-only
)
7137 (display-buffer-reuse-frames 0)
7138 (t (last-nonminibuffer-frame))))
7139 (window (if (and (eq buffer
(window-buffer))
7140 (not (cdr (assq 'inhibit-same-window alist
))))
7142 (car (delq (selected-window)
7143 (get-buffer-window-list buffer
'nomini
7145 (when (window-live-p window
)
7146 (prog1 (window--display-buffer buffer window
'reuse alist
)
7147 (unless (cdr (assq 'inhibit-switch-frame alist
))
7148 (window--maybe-raise-frame (window-frame window
)))))))
7150 (defun display-buffer-reuse-mode-window (buffer alist
)
7151 "Return a window based on the mode of the buffer it displays.
7152 Display BUFFER in the returned window. Return nil if no usable
7155 If ALIST contains a `mode' entry, its value is a major mode (a
7156 symbol) or a list of modes. A window is a candidate if it
7157 displays a buffer that derives from one of the given modes. When
7158 ALIST contains no `mode' entry, the current major mode of BUFFER
7161 The behavior is also controlled by entries for
7162 `inhibit-same-window', `reusable-frames' and
7163 `inhibit-switch-frame' as is done in the function
7164 `display-buffer-reuse-window'."
7165 (let* ((alist-entry (assq 'reusable-frames alist
))
7166 (alist-mode-entry (assq 'mode alist
))
7167 (frames (cond (alist-entry (cdr alist-entry
))
7168 ((if (eq pop-up-frames
'graphic-only
)
7172 (display-buffer-reuse-frames 0)
7173 (t (last-nonminibuffer-frame))))
7174 (inhibit-same-window-p (cdr (assq 'inhibit-same-window alist
)))
7175 (windows (window-list-1 nil
'nomini frames
))
7176 (buffer-mode (with-current-buffer buffer major-mode
))
7177 (allowed-modes (if alist-mode-entry
7178 (cdr alist-mode-entry
)
7180 (curwin (selected-window))
7181 (curframe (selected-frame)))
7182 (unless (listp allowed-modes
)
7183 (setq allowed-modes
(list allowed-modes
)))
7184 (let (same-mode-same-frame
7185 same-mode-other-frame
7186 derived-mode-same-frame
7187 derived-mode-other-frame
)
7188 (dolist (window windows
)
7190 (with-current-buffer (window-buffer window
)
7191 (cond ((memq major-mode allowed-modes
)
7193 ((derived-mode-p allowed-modes
)
7196 (not (and inhibit-same-window-p
7197 (eq window curwin
))))
7198 (push window
(if (eq curframe
(window-frame window
))
7199 (if (eq mode?
'same
)
7200 same-mode-same-frame
7201 derived-mode-same-frame
)
7202 (if (eq mode?
'same
)
7203 same-mode-other-frame
7204 derived-mode-other-frame
))))))
7205 (let ((window (car (nconc same-mode-same-frame
7206 same-mode-other-frame
7207 derived-mode-same-frame
7208 derived-mode-other-frame
))))
7209 (when (window-live-p window
)
7210 (prog1 (window--display-buffer buffer window
'reuse alist
)
7211 (unless (cdr (assq 'inhibit-switch-frame alist
))
7212 (window--maybe-raise-frame (window-frame window
)))))))))
7214 (defun display-buffer--special-action (buffer)
7215 "Return special display action for BUFFER, if any.
7216 If `special-display-p' returns non-nil for BUFFER, return an
7217 appropriate display action involving `special-display-function'.
7218 See `display-buffer' for the format of display actions."
7219 (and special-display-function
7220 ;; `special-display-p' returns either t or a list of frame
7221 ;; parameters to pass to `special-display-function'.
7222 (let ((pars (special-display-p (buffer-name buffer
))))
7224 (list (list #'display-buffer-reuse-window
7225 (lambda (buffer _alist
)
7226 (funcall special-display-function
7227 buffer
(if (listp pars
) pars
)))))))))
7229 (defun display-buffer-pop-up-frame (buffer alist
)
7230 "Display BUFFER in a new frame.
7231 This works by calling `pop-up-frame-function'. If successful,
7232 return the window used; otherwise return nil.
7234 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
7235 raising the new frame.
7237 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
7238 corresponding value is an alist of frame parameters to give the
7240 (let* ((params (cdr (assq 'pop-up-frame-parameters alist
)))
7241 (pop-up-frame-alist (append params pop-up-frame-alist
))
7242 (fun pop-up-frame-function
)
7245 ;; Make BUFFER current so `make-frame' will use it as the
7246 ;; new frame's buffer (Bug#15133).
7247 (with-current-buffer buffer
7248 (setq frame
(funcall fun
)))
7249 (setq window
(frame-selected-window frame
)))
7250 (prog1 (window--display-buffer
7251 buffer window
'frame alist display-buffer-mark-dedicated
)
7252 (unless (cdr (assq 'inhibit-switch-frame alist
))
7253 (window--maybe-raise-frame frame
))))))
7255 (defun display-buffer-pop-up-window (buffer alist
)
7256 "Display BUFFER by popping up a new window.
7257 The new window is created on the selected frame, or in
7258 `last-nonminibuffer-frame' if no windows can be created there.
7259 If successful, return the new window; otherwise return nil.
7261 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7262 event that the new window is created on another frame, avoid
7264 (let ((frame (or (window--frame-usable-p (selected-frame))
7265 (window--frame-usable-p (last-nonminibuffer-frame))))
7267 (when (and (or (not (frame-parameter frame
'unsplittable
))
7268 ;; If the selected frame cannot be split, look at
7269 ;; `last-nonminibuffer-frame'.
7270 (and (eq frame
(selected-frame))
7271 (setq frame
(last-nonminibuffer-frame))
7272 (window--frame-usable-p frame
)
7273 (not (frame-parameter frame
'unsplittable
))))
7274 ;; Attempt to split largest or least recently used window.
7275 (setq window
(or (window--try-to-split-window
7276 (get-largest-window frame t
) alist
)
7277 (window--try-to-split-window
7278 (get-lru-window frame t
) alist
))))
7280 (prog1 (window--display-buffer
7281 buffer window
'window alist display-buffer-mark-dedicated
)
7282 (unless (cdr (assq 'inhibit-switch-frame alist
))
7283 (window--maybe-raise-frame (window-frame window
)))))))
7285 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist
)
7286 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
7287 If `pop-up-frames' is non-nil (and not `graphic-only' on a
7288 text-only terminal), try with `display-buffer-pop-up-frame'.
7290 If that cannot be done, and `pop-up-windows' is non-nil, try
7291 again with `display-buffer-pop-up-window'."
7292 (or (and (if (eq pop-up-frames
'graphic-only
)
7295 (display-buffer-pop-up-frame buffer alist
))
7297 (display-buffer-pop-up-window buffer alist
))))
7299 (defun display-buffer-in-child-frame (buffer alist
)
7300 "Display BUFFER in a child frame.
7301 By default, this either reuses a child frame of the selected
7302 frame or makes a new child frame of the selected frame. If
7303 successful, return the window used; otherwise return nil.
7305 If ALIST has a non-nil 'child-frame-parameters' entry, the
7306 corresponding value is an alist of frame parameters to give the
7307 new frame. A 'parent-frame' parameter specifying the selected
7308 frame is provided by default. If the child frame should be or
7309 become the child of any other frame, a corresponding entry must
7313 (cdr (assq 'child-frame-parameters alist
))
7314 `((parent-frame .
,(selected-frame)))))
7315 (parent (or (assq 'parent-frame parameters
)
7317 (share (assq 'share-child-frame parameters
))
7318 share1 frame window
)
7319 (with-current-buffer buffer
7320 (when (frame-live-p parent
)
7322 (dolist (frame1 (frame-list))
7323 (when (eq (frame-parent frame1
) parent
)
7324 (setq share1
(assq 'share-child-frame
7325 (frame-parameters frame1
)))
7326 (when (eq share share1
)
7328 (throw 'frame t
))))))
7331 (setq window
(frame-selected-window frame
))
7332 (setq frame
(make-frame parameters
))
7333 (setq window
(frame-selected-window frame
))))
7335 (prog1 (window--display-buffer
7336 buffer window
'frame alist display-buffer-mark-dedicated
)
7337 (unless (cdr (assq 'inhibit-switch-frame alist
))
7338 (window--maybe-raise-frame frame
)))))
7340 (defun display-buffer-below-selected (buffer alist
)
7341 "Try displaying BUFFER in a window below the selected window.
7342 If there is a window below the selected one and that window
7343 already displays BUFFER, use that window. Otherwise, try to
7344 create a new window below the selected one and show BUFFER there.
7345 If that attempt fails as well and there is a non-dedicated window
7346 below the selected one, use that window."
7348 (or (and (setq window
(window-in-direction 'below
))
7349 (eq buffer
(window-buffer window
))
7350 (window--display-buffer buffer window
'reuse alist
))
7351 (and (not (frame-parameter nil
'unsplittable
))
7352 (let ((split-height-threshold 0)
7353 split-width-threshold
)
7354 (setq window
(window--try-to-split-window
7355 (selected-window) alist
)))
7356 (window--display-buffer
7357 buffer window
'window alist display-buffer-mark-dedicated
))
7358 (and (setq window
(window-in-direction 'below
))
7359 (not (window-dedicated-p window
))
7360 (window--display-buffer
7361 buffer window
'reuse alist display-buffer-mark-dedicated
)))))
7363 (defun display-buffer-at-bottom (buffer alist
)
7364 "Try displaying BUFFER in a window at the bottom of the selected frame.
7365 This either reuses such a window provided it shows BUFFER
7366 already, splits a window at the bottom of the frame or the
7367 frame's root window, or reuses some window at the bottom of the
7369 (let (bottom-window bottom-window-shows-buffer window
)
7373 ((window-in-direction 'below window
))
7374 ((and (not bottom-window-shows-buffer
)
7375 (eq buffer
(window-buffer window
)))
7376 (setq bottom-window-shows-buffer t
)
7377 (setq bottom-window window
))
7378 ((not bottom-window
)
7379 (setq bottom-window window
)))
7381 (or (and bottom-window-shows-buffer
7382 (window--display-buffer
7383 buffer bottom-window
'reuse alist display-buffer-mark-dedicated
))
7384 (and (not (frame-parameter nil
'unsplittable
))
7385 (let (split-width-threshold)
7386 (setq window
(window--try-to-split-window bottom-window alist
)))
7387 (window--display-buffer
7388 buffer window
'window alist display-buffer-mark-dedicated
))
7389 (and (not (frame-parameter nil
'unsplittable
))
7390 (setq window
(split-window-no-error (window-main-window)))
7391 (window--display-buffer
7392 buffer window
'window alist display-buffer-mark-dedicated
))
7393 (and (setq window bottom-window
)
7394 (not (window-dedicated-p window
))
7395 (window--display-buffer
7396 buffer window
'reuse alist display-buffer-mark-dedicated
)))))
7398 (defun display-buffer-in-previous-window (buffer alist
)
7399 "Display BUFFER in a window previously showing it.
7400 If ALIST has a non-nil `inhibit-same-window' entry, the selected
7401 window is not eligible for reuse.
7403 If ALIST contains a `reusable-frames' entry, its value determines
7404 which frames to search for a reusable window:
7405 nil -- the selected frame (actually the last non-minibuffer frame)
7406 A frame -- just that frame
7407 `visible' -- all visible frames
7408 0 -- all frames on the current terminal
7411 If ALIST contains no `reusable-frames' entry, search just the
7412 selected frame if `display-buffer-reuse-frames' and
7413 `pop-up-frames' are both nil; search all frames on the current
7414 terminal if either of those variables is non-nil.
7416 If ALIST has a `previous-window' entry, the window specified by
7417 that entry will override any other window found by the methods
7418 above, even if that window never showed BUFFER before."
7419 (let* ((alist-entry (assq 'reusable-frames alist
))
7420 (inhibit-same-window
7421 (cdr (assq 'inhibit-same-window alist
)))
7423 (alist-entry (cdr alist-entry
))
7424 ((if (eq pop-up-frames
'graphic-only
)
7428 (display-buffer-reuse-frames 0)
7429 (t (last-nonminibuffer-frame))))
7430 best-window second-best-window window
)
7431 ;; Scan windows whether they have shown the buffer recently.
7433 (dolist (window (window-list-1 (frame-first-window) 'nomini frames
))
7434 (when (and (assq buffer
(window-prev-buffers window
))
7435 (not (window-dedicated-p window
)))
7436 (if (eq window
(selected-window))
7437 (unless inhibit-same-window
7438 (setq second-best-window window
))
7439 (setq best-window window
)
7441 ;; When ALIST has a `previous-window' entry, that entry may override
7442 ;; anything we found so far.
7443 (when (and (setq window
(cdr (assq 'previous-window alist
)))
7444 (window-live-p window
)
7445 (not (window-dedicated-p window
)))
7446 (if (eq window
(selected-window))
7447 (unless inhibit-same-window
7448 (setq second-best-window window
))
7449 (setq best-window window
)))
7450 ;; Return best or second best window found.
7451 (when (setq window
(or best-window second-best-window
))
7452 (window--display-buffer buffer window
'reuse alist
))))
7454 (defun display-buffer-use-some-window (buffer alist
)
7455 "Display BUFFER in an existing window.
7456 Search for a usable window, set that window to the buffer, and
7457 return the window. If no suitable window is found, return nil.
7459 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
7460 event that a window in another frame is chosen, avoid raising
7462 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist
)))
7463 (frame (or (window--frame-usable-p (selected-frame))
7464 (window--frame-usable-p (last-nonminibuffer-frame))))
7466 ;; Reuse an existing window.
7467 (or (get-lru-window frame nil not-this-window
)
7468 (let ((window (get-buffer-window buffer
'visible
)))
7469 (unless (and not-this-window
7470 (eq window
(selected-window)))
7472 (get-largest-window 'visible nil not-this-window
)
7473 (let ((window (get-buffer-window buffer
0)))
7474 (unless (and not-this-window
7475 (eq window
(selected-window)))
7477 (get-largest-window 0 nil not-this-window
)))
7478 (quit-restore (and (window-live-p window
)
7479 (window-parameter window
'quit-restore
)))
7480 (quad (nth 1 quit-restore
)))
7481 (when (window-live-p window
)
7482 ;; If the window was used by `display-buffer' before, try to
7483 ;; resize it to its old height but don't signal an error.
7484 (when (and (listp quad
)
7485 (integerp (nth 3 quad
))
7486 (> (nth 3 quad
) (window-total-height window
)))
7488 (window-resize window
(- (nth 3 quad
) (window-total-height window
)))
7492 (window--display-buffer buffer window
'reuse alist
)
7493 (window--even-window-sizes window
)
7494 (unless (cdr (assq 'inhibit-switch-frame alist
))
7495 (window--maybe-raise-frame (window-frame window
)))))))
7497 (defun display-buffer-no-window (_buffer alist
)
7498 "Display BUFFER in no window.
7499 If ALIST has a non-nil `allow-no-window' entry, then don't display
7500 a window at all. This makes possible to override the default action
7501 and avoid displaying the buffer. It is assumed that when the caller
7502 specifies a non-nil `allow-no-window' then it can handle a nil value
7503 returned from `display-buffer' in this case."
7504 (when (cdr (assq 'allow-no-window alist
))
7507 ;;; Display + selection commands:
7508 (defun pop-to-buffer (buffer-or-name &optional action norecord
)
7509 "Display buffer specified by BUFFER-OR-NAME and select its window.
7510 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
7511 If it is a string not naming an existent buffer, create a buffer
7512 with that name. If BUFFER-OR-NAME is nil, choose some other
7513 buffer. In either case, make that buffer current and return it.
7515 This uses `display-buffer' as a subroutine. The optional ACTION
7516 argument is passed to `display-buffer' as its ACTION argument.
7517 See `display-buffer' for more information. ACTION is t if called
7518 interactively with a prefix argument, which means to pop to a
7519 window other than the selected one even if the buffer is already
7520 displayed in the selected window.
7522 If a suitable window is found, select that window. If it is not
7523 on the selected frame, raise that window's frame and give it
7526 Optional third arg NORECORD non-nil means do not put this buffer
7527 at the front of the list of recently selected ones."
7528 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
7529 (if current-prefix-arg t
)))
7530 (let* ((buffer (window-normalize-buffer-to-switch-to buffer-or-name
))
7531 (old-frame (selected-frame))
7532 (window (display-buffer buffer action
)))
7533 ;; Don't assume that `display-buffer' has supplied us with a window
7536 (let ((frame (window-frame window
)))
7537 ;; If we chose another frame, make sure it gets input focus.
7538 (unless (eq frame old-frame
)
7539 (select-frame-set-input-focus frame norecord
))
7540 ;; Make sure the window is selected (Bug#8615), (Bug#6954)
7541 (select-window window norecord
))
7542 ;; If `display-buffer' failed to supply a window, just make the
7544 (set-buffer buffer
))
7545 ;; Return BUFFER even when we got no window.
7548 (defun pop-to-buffer-same-window (buffer &optional norecord
)
7549 "Select buffer BUFFER in some window, preferably the same one.
7550 BUFFER may be a buffer, a string (a buffer name), or nil. If it
7551 is a string not naming an existent buffer, create a buffer with
7552 that name. If BUFFER is nil, choose some other buffer. Return
7555 Optional argument NORECORD, if non-nil means do not put this
7556 buffer at the front of the list of recently selected ones.
7558 Unlike `pop-to-buffer', this function prefers using the selected
7559 window over popping up a new window or frame."
7560 (pop-to-buffer buffer display-buffer--same-window-action norecord
))
7562 (defun read-buffer-to-switch (prompt)
7563 "Read the name of a buffer to switch to, prompting with PROMPT.
7564 Return the name of the buffer as a string.
7566 This function is intended for the `switch-to-buffer' family of
7567 commands since these need to omit the name of the current buffer
7568 from the list of completions and default values."
7569 (let ((rbts-completion-table (internal-complete-buffer-except)))
7570 (minibuffer-with-setup-hook
7572 (setq minibuffer-completion-table rbts-completion-table
)
7573 ;; Since rbts-completion-table is built dynamically, we
7574 ;; can't just add it to the default value of
7575 ;; icomplete-with-completion-tables, so we add it
7577 (if (and (boundp 'icomplete-with-completion-tables
)
7578 (listp icomplete-with-completion-tables
))
7579 (set (make-local-variable 'icomplete-with-completion-tables
)
7580 (cons rbts-completion-table
7581 icomplete-with-completion-tables
))))
7582 (read-buffer prompt
(other-buffer (current-buffer))
7583 (confirm-nonexistent-file-or-buffer)))))
7585 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
7586 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
7587 If BUFFER-OR-NAME is nil, return the buffer returned by
7588 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
7589 exists, return that buffer. If no such buffer exists, create a
7590 buffer with the name BUFFER-OR-NAME and return that buffer."
7592 (or (get-buffer buffer-or-name
)
7593 (let ((buffer (get-buffer-create buffer-or-name
)))
7594 (set-buffer-major-mode buffer
)
7598 (defcustom switch-to-buffer-preserve-window-point t
7599 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
7600 If this is nil, `switch-to-buffer' displays the buffer at that
7601 buffer's `point'. If this is `already-displayed', it tries to
7602 display the buffer at its previous position in the selected
7603 window, provided the buffer is currently displayed in some other
7604 window on any visible or iconified frame. If this is t, it
7605 unconditionally tries to display the buffer at its previous
7606 position in the selected window.
7608 This variable is ignored if the buffer is already displayed in
7609 the selected window or never appeared in it before, or if
7610 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
7612 (const :tag
"Never" nil
)
7613 (const :tag
"If already displayed elsewhere" already-displayed
)
7614 (const :tag
"Always" t
))
7618 (defcustom switch-to-buffer-in-dedicated-window nil
7619 "Allow switching to buffer in strongly dedicated windows.
7620 If non-nil, allow `switch-to-buffer' to proceed when called
7621 interactively and the selected window is strongly dedicated to
7624 The following values are recognized:
7626 nil - disallow switching; signal an error
7628 prompt - prompt user whether to allow switching
7630 pop - perform `pop-to-buffer' instead
7632 t - undedicate selected window and switch
7634 When called non-interactively, `switch-to-buffer' always signals
7635 an error when the selected window is dedicated to its buffer and
7636 FORCE-SAME-WINDOW is non-nil."
7638 (const :tag
"Disallow" nil
)
7639 (const :tag
"Prompt" prompt
)
7640 (const :tag
"Pop" pop
)
7641 (const :tag
"Allow" t
))
7645 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window
)
7646 "Display buffer BUFFER-OR-NAME in the selected window.
7648 WARNING: This is NOT the way to work on another buffer temporarily
7649 within a Lisp program! Use `set-buffer' instead. That avoids
7650 messing with the window-buffer correspondences.
7652 If the selected window cannot display the specified buffer
7653 because it is a minibuffer window or strongly dedicated to
7654 another buffer, call `pop-to-buffer' to select the buffer in
7655 another window. In interactive use, if the selected window is
7656 strongly dedicated to its buffer, the value of the option
7657 `switch-to-buffer-in-dedicated-window' specifies how to proceed.
7659 If called interactively, read the buffer name using `read-buffer'.
7660 The variable `confirm-nonexistent-file-or-buffer' determines
7661 whether to request confirmation before creating a new buffer.
7662 See `read-buffer' for features related to input and completion
7665 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
7666 If BUFFER-OR-NAME is a string that does not identify an existing
7667 buffer, create a buffer with that name. If BUFFER-OR-NAME is
7668 nil, switch to the buffer returned by `other-buffer'.
7670 If optional argument NORECORD is non-nil, do not put the buffer
7671 at the front of the buffer list, and do not make the window
7672 displaying it the most recently selected one.
7674 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
7675 must be displayed in the selected window when called
7676 non-interactively; if that is impossible, signal an error rather
7677 than calling `pop-to-buffer'.
7679 The option `switch-to-buffer-preserve-window-point' can be used
7680 to make the buffer appear at its last position in the selected
7683 Return the buffer switched to."
7685 (let ((force-same-window
7687 ((window-minibuffer-p) nil
)
7688 ((not (eq (window-dedicated-p) t
)) 'force-same-window
)
7689 ((pcase switch-to-buffer-in-dedicated-window
7691 "Cannot switch buffers in a dedicated window"))
7694 (format "Window is dedicated to %s; undedicate it"
7697 (set-window-dedicated-p nil nil
)
7700 "Cannot switch buffers in a dedicated window")))
7702 (_ (set-window-dedicated-p nil nil
) 'force-same-window
))))))
7703 (list (read-buffer-to-switch "Switch to buffer: ") nil force-same-window
)))
7704 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name
)))
7706 ;; Don't call set-window-buffer if it's not needed since it
7707 ;; might signal an error (e.g. if the window is dedicated).
7708 ((eq buffer
(window-buffer)))
7709 ((window-minibuffer-p)
7710 (if force-same-window
7711 (user-error "Cannot switch buffers in minibuffer window")
7712 (pop-to-buffer buffer norecord
)))
7713 ((eq (window-dedicated-p) t
)
7714 (if force-same-window
7715 (user-error "Cannot switch buffers in a dedicated window")
7716 (pop-to-buffer buffer norecord
)))
7718 (let* ((entry (assq buffer
(window-prev-buffers)))
7719 (displayed (and (eq switch-to-buffer-preserve-window-point
7721 (get-buffer-window buffer
0))))
7722 (set-window-buffer nil buffer
)
7724 (or (eq switch-to-buffer-preserve-window-point t
)
7726 ;; Try to restore start and point of buffer in the selected
7727 ;; window (Bug#4041).
7728 (set-window-start (selected-window) (nth 1 entry
) t
)
7729 (set-window-point nil
(nth 2 entry
))))))
7732 (select-window (selected-window)))
7733 (set-buffer buffer
)))
7735 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord
)
7736 "Select the buffer specified by BUFFER-OR-NAME in another window.
7737 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
7738 nil. Return the buffer switched to.
7740 If called interactively, read the buffer name using `read-buffer'.
7741 The variable `confirm-nonexistent-file-or-buffer' determines
7742 whether to request confirmation before creating a new buffer.
7743 See `read-buffer' for features related to input and completion
7746 If BUFFER-OR-NAME is a string and does not identify an existing
7747 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
7748 nil, switch to the buffer returned by `other-buffer'.
7750 Optional second argument NORECORD non-nil means do not put this
7751 buffer at the front of the list of recently selected ones.
7753 This uses the function `display-buffer' as a subroutine; see its
7754 documentation for additional customization information."
7756 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
7757 (let ((pop-up-windows t
))
7758 (pop-to-buffer buffer-or-name t norecord
)))
7760 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord
)
7761 "Switch to buffer BUFFER-OR-NAME in another frame.
7762 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
7763 nil. Return the buffer switched to.
7765 If called interactively, read the buffer name using `read-buffer'.
7766 The variable `confirm-nonexistent-file-or-buffer' determines
7767 whether to request confirmation before creating a new buffer.
7768 See `read-buffer' for features related to input and completion
7771 If BUFFER-OR-NAME is a string and does not identify an existing
7772 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
7773 nil, switch to the buffer returned by `other-buffer'.
7775 Optional second arg NORECORD non-nil means do not put this
7776 buffer at the front of the list of recently selected ones.
7778 This uses the function `display-buffer' as a subroutine; see its
7779 documentation for additional customization information."
7781 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
7782 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord
))
7784 (defun set-window-text-height (window height
)
7785 "Set the height in lines of the text display area of WINDOW to HEIGHT.
7786 WINDOW must be a live window and defaults to the selected one.
7787 HEIGHT doesn't include the mode line or header line, if any, or
7788 any partial-height lines in the text display area.
7790 Note that the current implementation of this function cannot
7791 always set the height exactly, but attempts to be conservative,
7792 by allocating more lines than are actually needed in the case
7793 where some error may be present."
7794 (setq window
(window-normalize-window window t
))
7795 (let ((delta (- height
(window-text-height window
))))
7796 (unless (zerop delta
)
7797 ;; Setting window-min-height to a value like 1 can lead to very
7798 ;; bizarre displays because it also allows Emacs to make *other*
7799 ;; windows one line tall, which means that there's no more space
7800 ;; for the mode line.
7801 (let ((window-min-height (min 2 height
)))
7802 (window-resize window delta
)))))
7804 (defun enlarge-window-horizontally (delta)
7805 "Make selected window DELTA columns wider.
7806 Interactively, if no argument is given, make selected window one
7809 (enlarge-window delta t
))
7811 (defun shrink-window-horizontally (delta)
7812 "Make selected window DELTA columns narrower.
7813 Interactively, if no argument is given, make selected window one
7816 (shrink-window delta t
))
7818 (defun count-screen-lines (&optional beg end count-final-newline window
)
7819 "Return the number of screen lines in the region.
7820 The number of screen lines may be different from the number of actual lines,
7821 due to line breaking, display table, etc.
7823 Optional arguments BEG and END default to `point-min' and `point-max'
7826 If region ends with a newline, ignore it unless optional third argument
7827 COUNT-FINAL-NEWLINE is non-nil.
7829 The optional fourth argument WINDOW specifies the window used for obtaining
7830 parameters such as width, horizontal scrolling, and so on. The default is
7831 to use the selected window's parameters.
7833 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
7834 regardless of which buffer is displayed in WINDOW. This makes possible to use
7835 `count-screen-lines' in any buffer, whether or not it is currently displayed
7838 (setq beg
(point-min)))
7840 (setq end
(point-max)))
7846 (narrow-to-region (min beg end
)
7847 (if (and (not count-final-newline
)
7848 (= ?
\n (char-before (max beg end
))))
7851 (goto-char (point-min))
7852 (1+ (vertical-motion (buffer-size) window
))))))
7854 (defun window-buffer-height (window)
7855 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
7856 WINDOW must be a live window and defaults to the selected one."
7857 (setq window
(window-normalize-window window t
))
7858 (with-current-buffer (window-buffer window
)
7860 (count-screen-lines (point-min) (point-max)
7861 ;; If buffer ends with a newline, ignore it when
7862 ;; counting height unless point is after it.
7866 ;;; Resizing windows and frames to fit their contents exactly.
7867 (defcustom fit-window-to-buffer-horizontally nil
7868 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
7869 If this is nil, `fit-window-to-buffer' never resizes windows
7870 horizontally. If this is `only', it can resize windows
7871 horizontally only. Any other value means `fit-window-to-buffer'
7872 can resize windows in both dimensions."
7877 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
7878 ;; counting title bar and outer borders.
7879 (defcustom fit-frame-to-buffer nil
7880 "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
7881 A frame is fit if and only if its root window is a live window
7882 and this option is non-nil. If this is `horizontally', frames
7883 are resized horizontally only. If this is `vertically', frames
7884 are resized vertically only. Any other non-nil value means
7885 frames can be resized in both dimensions."
7890 (defcustom fit-frame-to-buffer-margins
'(nil nil nil nil
)
7891 "Margins around frame for `fit-frame-to-buffer'.
7892 This specifies the numbers of pixels to be left free on the left,
7893 above, on the right, and below a frame fitted to its buffer. Set
7894 this to avoid obscuring other desktop objects like the taskbar.
7895 The default is nil for each side, which means to not add margins.
7897 The value specified here can be overridden for a specific frame
7898 by that frame's `fit-frame-to-buffer-margins' parameter, if
7899 present. See also `fit-frame-to-buffer-sizes'."
7905 :format
"%[LeftMargin%] %v "
7906 (const :tag
"None" :format
"%t" nil
)
7907 (integer :tag
"Pixels" :size
5))
7911 :format
"%[TopMargin%] %v "
7912 (const :tag
"None" :format
"%t" nil
)
7913 (integer :tag
"Pixels" :size
5))
7917 :format
"%[RightMargin%] %v "
7918 (const :tag
"None" :format
"%t" nil
)
7919 (integer :tag
"Pixels" :size
5))
7923 :format
"%[BottomMargin%] %v "
7924 (const :tag
"None" :format
"%t" nil
)
7925 (integer :tag
"Pixels" :size
5)))
7928 (defcustom fit-frame-to-buffer-sizes
'(nil nil nil nil
)
7929 "Size boundaries of frame for `fit-frame-to-buffer'.
7930 This list specifies the total maximum and minimum lines and
7931 maximum and minimum columns of the root window of any frame that
7932 shall be fit to its buffer. If any of these values is non-nil,
7933 it overrides the corresponding argument of `fit-frame-to-buffer'.
7935 On window systems where the menubar can wrap, fitting a frame to
7936 its buffer may swallow the last line(s). Specifying an
7937 appropriate minimum width value here can avoid such wrapping.
7939 See also `fit-frame-to-buffer-margins'."
7943 :tag
"Maximum Height"
7945 :format
"%[MaxHeight%] %v "
7946 (const :tag
"None" :format
"%t" nil
)
7947 (integer :tag
"Lines" :size
5))
7949 :tag
"Minimum Height"
7951 :format
"%[MinHeight%] %v "
7952 (const :tag
"None" :format
"%t" nil
)
7953 (integer :tag
"Lines" :size
5))
7955 :tag
"Maximum Width"
7957 :format
"%[MaxWidth%] %v "
7958 (const :tag
"None" :format
"%t" nil
)
7959 (integer :tag
"Columns" :size
5))
7961 :tag
"Minimum Width"
7963 :format
"%[MinWidth%] %v\n"
7964 (const :tag
"None" :format
"%t" nil
)
7965 (integer :tag
"Columns" :size
5)))
7968 (declare-function x-display-pixel-height
"xfns.c" (&optional terminal
))
7970 (defun window--sanitize-margin (margin left right
)
7971 "Return MARGIN if it's a number between LEFT and RIGHT.
7972 Return 0 otherwise."
7973 (if (and (numberp margin
)
7974 (<= left
(- right margin
)) (<= margin right
))
7978 (declare-function tool-bar-height
"xdisp.c" (&optional frame pixelwise
))
7980 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only
)
7981 "Adjust size of FRAME to display the contents of its buffer exactly.
7982 FRAME can be any live frame and defaults to the selected one.
7983 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
7984 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
7985 FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
7986 `window-min-height' and `window-min-width' respectively.
7988 If the optional argument ONLY is `vertically', resize the frame
7989 vertically only. If ONLY is `horizontally', resize the frame
7992 The new position and size of FRAME can be additionally determined
7993 by customizing the options `fit-frame-to-buffer-sizes' and
7994 `fit-frame-to-buffer-margins' or setting the corresponding
7995 parameters of FRAME."
7997 (unless (fboundp 'display-monitor-attributes-list
)
7998 (user-error "Cannot resize frame in non-graphic Emacs"))
7999 (setq frame
(window-normalize-frame frame
))
8000 (when (window-live-p (frame-root-window frame
))
8001 (let* ((char-width (frame-char-width frame
))
8002 (char-height (frame-char-height frame
))
8003 ;; WINDOW is FRAME's root window.
8004 (window (frame-root-window frame
))
8005 (parent (frame-parent frame
))
8008 (car (display-monitor-attributes-list
8009 (frame-parameter frame
'display
)))))
8010 ;; FRAME'S parent or display sizes. Used in connection
8014 (cdr (assq 'geometry monitor-attributes
))))
8015 (parent-or-display-width
8017 (frame-native-width parent
)
8018 (- (nth 2 geometry
) (nth 0 geometry
))))
8019 (parent-or-display-height
8021 (frame-native-height parent
)
8022 (- (nth 3 geometry
) (nth 1 geometry
))))
8023 ;; FRAME'S parent or workarea sizes. Used when no margins
8027 `(0 0 ,parent-or-display-width
,parent-or-display-height
)
8028 (cdr (assq 'workarea monitor-attributes
))))
8029 ;; The outer size of FRAME. Needed to calculate the
8030 ;; margins around the root window's body that have to
8031 ;; remain untouched by fitting.
8032 (outer-edges (frame-edges frame
'outer-edges
))
8033 (outer-width (if outer-edges
8034 (- (nth 2 outer-edges
) (nth 0 outer-edges
))
8036 (frame-pixel-width frame
)))
8037 (outer-height (if outer-edges
8038 (- (nth 3 outer-edges
) (nth 1 outer-edges
))
8039 ;; Another poor guess.
8040 (frame-pixel-height frame
)))
8041 ;; The text size of FRAME. Needed to specify FRAME's
8042 ;; text size after the root window's body's new sizes have
8044 (text-width (frame-text-width frame
))
8045 (text-height (frame-text-height frame
))
8046 ;; WINDOW's body size.
8047 (body-width (window-body-width window t
))
8048 (body-height (window-body-height window t
))
8049 ;; The difference between FRAME's outer size and WINDOW's
8051 (outer-minus-body-width (- outer-width body-width
))
8052 (outer-minus-body-height (- outer-height body-height
))
8053 ;; The difference between FRAME's text size and WINDOW's
8054 ;; body size (these values "should" be positive).
8055 (text-minus-body-width (- text-width body-width
))
8056 (text-minus-body-height (- text-height body-height
))
8057 ;; The current position of FRAME.
8058 (position (frame-position frame
))
8059 (left (car position
))
8060 (top (cdr position
))
8061 ;; The margins specified for FRAME. These represent pixel
8062 ;; offsets from the left, top, right and bottom edge of the
8063 ;; display or FRAME's parent's native rectangle and have to
8064 ;; take care of the display's taskbar and other obstacles.
8065 ;; If they are unspecified, constrain the resulting frame
8066 ;; to its workarea or the parent frame's native rectangle.
8067 (margins (or (frame-parameter frame
'fit-frame-to-buffer-margins
)
8068 fit-frame-to-buffer-margins
))
8069 ;; Convert margins into pixel offsets from the left-top
8070 ;; corner of FRAME's display or parent.
8071 (left-margin (if (nth 0 margins
)
8072 (window--sanitize-margin
8073 (nth 0 margins
) 0 parent-or-display-width
)
8074 (nth 0 parent-or-workarea
)))
8075 (top-margin (if (nth 1 margins
)
8076 (window--sanitize-margin
8077 (nth 1 margins
) 0 parent-or-display-height
)
8078 (nth 1 parent-or-workarea
)))
8079 (right-margin (if (nth 2 margins
)
8080 (- parent-or-display-width
8081 (window--sanitize-margin
8082 (nth 2 margins
) left-margin
8083 parent-or-display-width
))
8084 (nth 2 parent-or-workarea
)))
8085 (bottom-margin (if (nth 3 margins
)
8086 (- parent-or-display-height
8087 (window--sanitize-margin
8088 (nth 3 margins
) top-margin
8089 parent-or-display-height
))
8090 (nth 3 parent-or-workarea
)))
8091 ;; Minimum and maximum sizes specified for FRAME.
8092 (sizes (or (frame-parameter frame
'fit-frame-to-buffer-sizes
)
8093 fit-frame-to-buffer-sizes
))
8094 ;; Calculate the minimum and maximum pixel sizes of FRAME
8095 ;; from the values provided by the MAX-HEIGHT, MIN-HEIGHT,
8096 ;; MAX-WIDTH and MIN-WIDTH arguments or, if these are nil,
8097 ;; from those provided by `fit-frame-to-buffer-sizes'.
8101 ((numberp max-height
) (* max-height char-height
))
8102 ((numberp (nth 0 sizes
)) (* (nth 0 sizes
) char-height
))
8103 (t parent-or-display-height
))
8104 ;; The following is the maximum height that fits into the
8105 ;; top and bottom margins.
8106 (max (- bottom-margin top-margin outer-minus-body-height
))))
8109 ((numberp min-height
) (* min-height char-height
))
8110 ((numberp (nth 1 sizes
)) (* (nth 1 sizes
) char-height
))
8111 (t (window-min-size window nil nil t
))))
8115 ((numberp max-width
) (* max-width char-width
))
8116 ((numberp (nth 2 sizes
)) (* (nth 2 sizes
) char-width
))
8117 (t parent-or-display-width
))
8118 ;; The following is the maximum width that fits into the
8119 ;; left and right margins.
8120 (max (- right-margin left-margin outer-minus-body-width
))))
8123 ((numberp min-width
) (* min-width char-width
))
8124 ((numberp (nth 3 sizes
)) (nth 3 sizes
))
8125 (t (window-min-size window t nil t
))))
8126 ;; Note: Currently, for a new frame the sizes of the header
8127 ;; and mode line may be estimated incorrectly
8129 (window-text-pixel-size window t t max-width max-height
))
8130 (width (max (car size
) min-width
))
8131 (height (max (cdr size
) min-height
)))
8132 ;; Don't change height or width when the window's size is fixed
8133 ;; in either direction or ONLY forbids it.
8135 ((or (eq window-size-fixed
'width
) (eq only
'vertically
))
8137 ((or (eq window-size-fixed
'height
) (eq only
'horizontally
))
8139 ;; Fit width to constraints.
8141 (unless frame-resize-pixelwise
8142 ;; Round to character sizes.
8143 (setq width
(* (/ (+ width char-width -
1) char-width
)
8145 ;; The new outer width (in pixels).
8146 (setq outer-width
(+ width outer-minus-body-width
))
8147 ;; Maybe move FRAME to preserve margins.
8148 (let ((right (+ left outer-width
)))
8150 ((> right right-margin
)
8151 ;; Move frame to left.
8152 (setq left
(max left-margin
(- left
(- right right-margin
)))))
8153 ((< left left-margin
)
8154 ;; Move frame to right.
8155 (setq left left-margin
)))))
8156 ;; Fit height to constraints.
8158 (unless frame-resize-pixelwise
8159 (setq height
(* (/ (+ height char-height -
1) char-height
)
8161 ;; The new outer height.
8162 (setq outer-height
(+ height outer-minus-body-height
))
8163 ;; Preserve margins.
8164 (let ((bottom (+ top outer-height
)))
8166 ((> bottom bottom-margin
)
8168 (setq top
(max top-margin
(- top
(- bottom bottom-margin
)))))
8171 (setq top top-margin
)))))
8172 ;; Apply our changes.
8175 (+ width text-minus-body-width
)
8176 (frame-text-width frame
)))
8179 (+ height text-minus-body-height
)
8180 (frame-text-height frame
)))
8181 (modify-frame-parameters
8182 frame
`((left .
,left
) (top .
,top
)
8183 (width .
(text-pixels .
,text-width
))
8184 (height .
(text-pixels .
,text-height
)))))))
8186 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width preserve-size
)
8187 "Adjust size of WINDOW to display its buffer's contents exactly.
8188 WINDOW must be a live window and defaults to the selected one.
8190 If WINDOW is part of a vertical combination, adjust WINDOW's
8191 height. The new height is calculated from the actual height of
8192 the accessible portion of its buffer. The optional argument
8193 MAX-HEIGHT specifies a maximum height and defaults to the height
8194 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
8195 minimum height and defaults to `window-min-height'. Both
8196 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
8197 and header line and a bottom divider, if any.
8199 If WINDOW is part of a horizontal combination and the value of
8200 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
8201 WINDOW's width. The new width of WINDOW is calculated from the
8202 maximum length of its buffer's lines that follow the current
8203 start position of WINDOW. The optional argument MAX-WIDTH
8204 specifies a maximum width and defaults to the width of WINDOW's
8205 frame. The optional argument MIN-WIDTH specifies a minimum width
8206 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
8207 are specified in columns and include fringes, margins, a
8208 scrollbar and a vertical divider, if any.
8210 If the optional argument `preserve-size' is non-nil, preserve the
8211 size of WINDOW (see `window-preserve-size').
8213 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
8214 If WINDOW is its frame's root window and the option
8215 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
8216 adjust the frame's size.
8218 Note that even if this function makes WINDOW large enough to show
8219 _all_ parts of its buffer you might not see the first part when
8220 WINDOW was scrolled. If WINDOW is resized horizontally, you will
8221 not see the top of its buffer unless WINDOW starts at its minimum
8222 accessible position."
8224 (setq window
(window-normalize-window window t
))
8225 (if (eq window
(frame-root-window window
))
8226 (when fit-frame-to-buffer
8227 ;; Fit WINDOW's frame to buffer.
8228 (fit-frame-to-buffer
8229 (window-frame window
)
8230 max-height min-height max-width min-width
8231 (and (memq fit-frame-to-buffer
'(vertically horizontally
))
8232 fit-frame-to-buffer
)))
8233 (with-selected-window window
8234 (let* ((pixelwise window-resize-pixelwise
)
8235 (char-height (frame-char-height))
8236 (char-width (frame-char-width))
8237 (total-height (window-size window nil pixelwise
))
8238 (body-height (window-body-height window pixelwise
))
8239 (body-width (window-body-width window pixelwise
))
8241 ;; Sanitize MIN-HEIGHT.
8242 (if (numberp min-height
)
8243 ;; Can't get smaller than `window-safe-min-height'.
8245 (* char-height min-height
)
8248 (window-safe-min-pixel-height window
)
8249 window-safe-min-height
))
8250 ;; Preserve header and mode line if present.
8252 (* char-height window-min-height
)
8254 (window-min-size window nil window pixelwise
))))
8256 ;; Sanitize MAX-HEIGHT.
8257 (if (numberp max-height
)
8261 window nil window nil t nil pixelwise
))
8263 (* char-height max-height
)
8265 (+ total-height
(window-max-delta
8266 window nil window nil t nil pixelwise
))))
8269 ;; If WINDOW is vertically combined, try to resize it
8271 ((and (not (eq fit-window-to-buffer-horizontally
'only
))
8272 (not (window-size-fixed-p window
'preserved
))
8273 (window-combined-p))
8274 ;; Vertically we always want to fit the entire buffer.
8275 ;; WINDOW'S height can't get larger than its frame's pixel
8276 ;; height. Its width remains fixed.
8277 (setq height
(+ (cdr (window-text-pixel-size
8278 nil nil t nil
(frame-pixel-height) t
))
8279 (window-scroll-bar-height window
)
8280 (window-bottom-divider-width)))
8283 (setq height
(/ (+ height char-height -
1) char-height
)))
8284 (unless (= height total-height
)
8285 (window-preserve-size window
)
8286 (window-resize-no-error
8288 (- (max min-height
(min max-height height
)) total-height
)
8289 nil window pixelwise
)
8291 (window-preserve-size window nil t
))))
8292 ;; If WINDOW is horizontally combined, try to resize it
8294 ((and fit-window-to-buffer-horizontally
8295 (not (window-size-fixed-p window t
'preserved
))
8296 (window-combined-p nil t
))
8297 (let* ((total-width (window-size window t pixelwise
))
8299 ;; Sanitize MIN-WIDTH.
8300 (if (numberp min-width
)
8301 ;; Can't get smaller than `window-safe-min-width'.
8303 (* char-width min-width
)
8306 (window-safe-min-pixel-width)
8307 window-safe-min-width
))
8308 ;; Preserve fringes, margins, scrollbars if present.
8310 (* char-width window-min-width
)
8312 (window-min-size nil nil window pixelwise
))))
8314 ;; Sanitize MAX-WIDTH.
8315 (if (numberp max-width
)
8318 window t window nil t nil pixelwise
))
8320 (* char-width max-width
)
8322 (+ total-width
(window-max-delta
8323 window t window nil t nil pixelwise
))))
8324 ;; When fitting horizontally, assume that WINDOW's
8325 ;; start position remains unaltered. WINDOW can't get
8326 ;; wider than its frame's pixel width, its height
8327 ;; remains unaltered.
8328 (width (+ (car (window-text-pixel-size
8329 nil
(window-start) (point-max)
8331 ;; Add one char-height to assure that
8332 ;; we're on the safe side. This
8333 ;; overshoots when the first line below
8334 ;; the bottom is wider than the window.
8336 (if pixelwise
1 char-height
))))
8337 (window-right-divider-width))))
8339 (setq width
(/ (+ width char-width -
1) char-width
)))
8340 (unless (= width body-width
)
8341 (window-preserve-size window t
)
8342 (window-resize-no-error
8346 (+ total-width
(- width body-width
))))
8350 (window-preserve-size window t t
))))))))))
8352 (defun window-safely-shrinkable-p (&optional window
)
8353 "Return t if WINDOW can be shrunk without shrinking other windows.
8354 WINDOW defaults to the selected window."
8355 (with-selected-window (or window
(selected-window))
8356 (let ((edges (window-edges)))
8357 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
8358 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
8360 (defun shrink-window-if-larger-than-buffer (&optional window
)
8361 "Shrink height of WINDOW if its buffer doesn't need so many lines.
8362 More precisely, shrink WINDOW vertically to be as small as
8363 possible, while still showing the full contents of its buffer.
8364 WINDOW must be a live window and defaults to the selected one.
8366 Do not shrink WINDOW to less than `window-min-height' lines. Do
8367 nothing if the buffer contains more lines than the present window
8368 height, or if some of the window's contents are scrolled out of
8369 view, or if shrinking this window would also shrink another
8370 window, or if the window is the only window of its frame.
8372 Return non-nil if the window was shrunk, nil otherwise."
8374 (setq window
(window-normalize-window window t
))
8375 ;; Make sure that WINDOW is vertically combined and `point-min' is
8376 ;; visible (for whatever reason that's needed). The remaining issues
8377 ;; should be taken care of by `fit-window-to-buffer'.
8378 (when (and (window-combined-p window
)
8379 (pos-visible-in-window-p (point-min) window
))
8380 (fit-window-to-buffer window
(window-total-height window
))))
8382 (defun window-largest-empty-rectangle--maximums-1 (quad maximums
)
8383 "Support function for `window-largest-empty-rectangle'."
8387 ((> (car quad
) (caar maximums
))
8388 (cons quad maximums
))
8390 (cons (car maximums
)
8391 (window-largest-empty-rectangle--maximums-1 quad
(cdr maximums
))))))
8393 (defun window-largest-empty-rectangle--maximums (quad maximums count
)
8394 "Support function for `window-largest-empty-rectangle'."
8395 (setq maximums
(window-largest-empty-rectangle--maximums-1 quad maximums
))
8396 (if (> (length maximums
) count
)
8400 (defun window-largest-empty-rectangle--disjoint-maximums (maximums count
)
8401 "Support function for `window-largest-empty-rectangle'."
8402 (setq maximums
(sort maximums
(lambda (x y
) (> (car x
) (car y
)))))
8403 (let ((new-length 0)
8405 (while (and maximums
(< new-length count
))
8406 (let* ((maximum (car maximums
))
8407 (at (nth 2 maximum
))
8408 (to (nth 3 maximum
)))
8410 (dolist (new-maximum new-maximums
)
8411 (let ((new-at (nth 2 new-maximum
))
8412 (new-to (nth 3 new-maximum
)))
8413 (when (if (< at new-at
) (> to new-at
) (< at new-to
))
8414 ;; Intersection -> drop.
8415 (throw 'drop nil
))))
8416 (setq new-maximums
(cons maximum new-maximums
))
8417 (setq new-length
(1+ new-length
)))
8418 (setq maximums
(cdr maximums
))))
8420 (nreverse new-maximums
)))
8422 (defun window-largest-empty-rectangle (&optional window count min-width min-height positions left
)
8423 "Return dimensions of largest empty rectangle in WINDOW.
8424 WINDOW must be a live window and defaults to the selected one.
8426 The return value is a triple of the width and the start and end
8427 Y-coordinates of the largest rectangle that can be inscribed into
8428 the empty space (the space not displaying any text) of WINDOW's
8429 text area. The return value is nil if the current glyph matrix
8430 of WINDOW is not up-to-date.
8432 Optional argument COUNT, if non-nil, specifies the maximum number
8433 of rectangles to return. This means that the return value is a
8434 list of triples specifying rectangles with the largest rectangle
8435 first. COUNT can be also a cons cell whose car specifies the
8436 number of rectangles to return and whose cdr, if non-nil, states
8437 that all rectangles returned must be disjoint.
8439 Note that the right edge of any rectangle returned by this
8440 function is the right edge of WINDOW (the left edge if its buffer
8443 Optional arguments MIN-WIDTH and MIN-HEIGHT, if non-nil, specify
8444 the minimum width and height of any rectangle returned.
8446 Optional argument POSITIONS, if non-nil, is a cons cell whose car
8447 specifies the uppermost and whose cdr specifies the lowermost
8448 pixel position that must be covered by any rectangle returned.
8449 Note that positions are counted from the start of the text area
8452 Optional argument LEFT, if non-nil, means to return values suitable for
8453 buffers displaying right to left text."
8454 ;; Process lines as returned by ‘window-lines-pixel-dimensions’.
8455 ;; STACK is a stack that contains rows that have to be processed yet.
8456 (let* ((window (window-normalize-window window t
))
8457 (disjoint (and (consp count
) (cdr count
)))
8458 (count (or (and (numberp count
) count
)
8459 (and (consp count
) (numberp (car count
)) (car count
))))
8460 (rows (window-lines-pixel-dimensions window nil nil t t left
))
8463 row stack stack-at stack-to
8464 top top-width top-at top-to top-size
8465 max-width max-at max-to maximums
)
8466 ;; ROWS-AT is the position where the first element of ROWS starts.
8467 ;; STACK-AT is the position where the first element of STACK starts.
8469 (setq row
(car rows
))
8470 (if (or (not stack
) (>= (car row
) (caar stack
)))
8473 (setq stack-at rows-at
))
8474 (setq stack
(cons row stack
))
8475 ;; Set ROWS-AT to where the first element of ROWS ends
8476 ;; which, after popping ROW, makes it the start position of
8478 (setq rows-at
(cdr row
))
8479 (setq rows
(cdr rows
)))
8480 (setq top
(car stack
))
8481 (setq stack
(cdr stack
))
8482 (setq top-width
(car top
))
8483 (setq top-at
(if stack
(cdar stack
) stack-at
))
8484 (setq top-to
(cdr top
))
8485 (setq top-size
(* top-width
(- top-to top-at
)))
8486 (unless (or (and min-width
(< top-width min-width
))
8487 (and min-height
(< (- top-to top-at
) min-height
))
8489 (or (> top-at
(car positions
))
8490 (< top-to
(cdr positions
)))))
8493 (setq maximums
(cons (list top-size top-width top-at top-to
)
8495 (setq maximums
(window-largest-empty-rectangle--maximums
8496 (list top-size top-width top-at top-to
)
8498 (when (> top-size max-size
)
8499 (setq max-size top-size
)
8500 (setq max-width top-width
)
8501 (setq max-at top-at
)
8502 (setq max-to top-to
))))
8503 (if (and stack
(> (caar stack
) (car row
)))
8504 ;; Have new top element of stack include old top.
8505 (setq stack
(cons (cons (caar stack
) (cdr top
)) (cdr stack
)))
8506 ;; Move rows-at backwards to top-at.
8507 (setq rows-at top-at
))))
8510 ;; STACK-TO is the position where the stack ends.
8511 (setq stack-to
(cdar stack
))
8513 (setq top
(car stack
))
8514 (setq stack
(cdr stack
))
8515 (setq top-width
(car top
))
8516 (setq top-at
(if stack
(cdar stack
) stack-at
))
8517 (setq top-size
(* top-width
(- stack-to top-at
)))
8518 (unless (or (and min-width
(< top-width min-width
))
8519 (and min-height
(< (- stack-to top-at
) min-height
))
8521 (or (> top-at
(car positions
))
8522 (< stack-to
(cdr positions
)))))
8525 (setq maximums
(cons (list top-size top-width top-at stack-to
)
8527 (setq maximums
(window-largest-empty-rectangle--maximums
8528 (list top-size top-width top-at stack-to
)
8530 (when (> top-size max-size
)
8531 (setq max-size top-size
)
8532 (setq max-width top-width
)
8533 (setq max-at top-at
)
8534 (setq max-to stack-to
))))))
8539 (window-largest-empty-rectangle--disjoint-maximums maximums count
)
8542 (list max-width max-at max-to
)))))
8544 (defun kill-buffer-and-window ()
8545 "Kill the current buffer and delete the selected window."
8547 (let ((window-to-delete (selected-window))
8548 (buffer-to-kill (current-buffer))
8549 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
8552 (add-hook 'kill-buffer-hook delete-window-hook t t
)
8553 (if (kill-buffer (current-buffer))
8554 ;; If `delete-window' failed before, we rerun it to regenerate
8555 ;; the error so it can be seen in the echo area.
8556 (when (eq (selected-window) window-to-delete
)
8558 ;; If the buffer is not dead for some reason (probably because
8559 ;; of a `quit' signal), remove the hook again.
8561 (with-current-buffer buffer-to-kill
8562 (remove-hook 'kill-buffer-hook delete-window-hook t
))))))
8566 ;; Groups of windows (Follow Mode).
8568 ;; This section of functions extends the functionality of some window
8569 ;; manipulating commands to groups of windows cooperatively
8570 ;; displaying a buffer, typically with Follow Mode.
8572 ;; The xxx-function variables are permanent locals so that their local
8573 ;; status is undone only when explicitly programmed, not when a buffer
8574 ;; is reverted or a mode function is called.
8576 (defvar window-group-start-function nil
)
8577 (make-variable-buffer-local 'window-group-start-function
)
8578 (put 'window-group-start-function
'permanent-local t
)
8579 (defun window-group-start (&optional window
)
8580 "Return position at which display currently starts in the group of
8581 windows containing WINDOW. When a grouping mode (such as Follow Mode)
8582 is not active, this function is identical to `window-start'.
8584 WINDOW must be a live window and defaults to the selected one.
8585 This is updated by redisplay or by calling `set-window*-start'."
8586 (if (functionp window-group-start-function
)
8587 (funcall window-group-start-function window
)
8588 (window-start window
)))
8590 (defvar window-group-end-function nil
)
8591 (make-variable-buffer-local 'window-group-end-function
)
8592 (put 'window-group-end-function
'permanent-local t
)
8593 (defun window-group-end (&optional window update
)
8594 "Return position at which display currently ends in the group of
8595 windows containing WINDOW. When a grouping mode (such as Follow Mode)
8596 is not active, this function is identical to `window-end'.
8598 WINDOW must be a live window and defaults to the selected one.
8599 This is updated by redisplay, when it runs to completion.
8600 Simply changing the buffer text or setting `window-group-start'
8601 does not update this value.
8602 Return nil if there is no recorded value. (This can happen if the
8603 last redisplay of WINDOW was preempted, and did not finish.)
8604 If UPDATE is non-nil, compute the up-to-date position
8605 if it isn't already recorded."
8606 (if (functionp window-group-end-function
)
8607 (funcall window-group-end-function window update
)
8608 (window-end window update
)))
8610 (defvar set-window-group-start-function nil
)
8611 (make-variable-buffer-local 'set-window-group-start-function
)
8612 (put 'set-window-group-start-function
'permanent-local t
)
8613 (defun set-window-group-start (window pos
&optional noforce
)
8614 "Make display in the group of windows containing WINDOW start at
8615 position POS in WINDOW's buffer. When a grouping mode (such as Follow
8616 Mode) is not active, this function is identical to `set-window-start'.
8618 WINDOW must be a live window and defaults to the selected one. Return
8619 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
8620 overriding motion of point in order to display at this exact start."
8621 (if (functionp set-window-group-start-function
)
8622 (funcall set-window-group-start-function window pos noforce
)
8623 (set-window-start window pos noforce
)))
8625 (defvar recenter-window-group-function nil
)
8626 (make-variable-buffer-local 'recenter-window-group-function
)
8627 (put 'recenter-window-group-function
'permanent-local t
)
8628 (defun recenter-window-group (&optional arg
)
8629 "Center point in the group of windows containing the selected window
8630 and maybe redisplay frame. When a grouping mode (such as Follow Mode)
8631 is not active, this function is identical to `recenter'.
8633 With a numeric prefix argument ARG, recenter putting point on screen line ARG
8634 relative to the first window in the selected window group. If ARG is
8635 negative, it counts up from the bottom of the last window in the
8636 group. (ARG should be less than the total height of the window group.)
8638 If ARG is omitted or nil, then recenter with point on the middle line of
8639 the selected window group; if the variable `recenter-redisplay' is
8640 non-nil, also erase the entire frame and redraw it (when
8641 `auto-resize-tool-bars' is set to `grow-only', this resets the
8642 tool-bar's height to the minimum height needed); if
8643 `recenter-redisplay' has the special value `tty', then only tty frames
8646 Just C-u as prefix means put point in the center of the window
8647 and redisplay normally--don't erase and redraw the frame."
8648 (if (functionp recenter-window-group-function
)
8649 (funcall recenter-window-group-function arg
)
8652 (defvar pos-visible-in-window-group-p-function nil
)
8653 (make-variable-buffer-local 'pos-visible-in-window-group-p-function
)
8654 (put 'pos-visible-in-window-group-p-function
'permanent-local t
)
8655 (defun pos-visible-in-window-group-p (&optional pos window partially
)
8656 "Return non-nil if position POS is currently on the frame in the
8657 window group containing WINDOW. When a grouping mode (such as Follow
8658 Mode) is not active, this function is identical to
8659 `pos-visible-in-window-p'.
8661 WINDOW must be a live window and defaults to the selected one.
8663 Return nil if that position is scrolled vertically out of view. If a
8664 character is only partially visible, nil is returned, unless the
8665 optional argument PARTIALLY is non-nil. If POS is only out of view
8666 because of horizontal scrolling, return non-nil. If POS is t, it
8667 specifies the position of the last visible glyph in the window group.
8668 POS defaults to point in WINDOW; WINDOW defaults to the selected
8671 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
8672 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
8673 where X and Y are the pixel coordinates relative to the top left corner
8674 of the window. The remaining elements are omitted if the character after
8675 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
8676 off-window at the top and bottom of the screen line (\"row\") containing
8677 POS, ROWH is the visible height of that row, and VPOS is the row number
8679 (if (functionp pos-visible-in-window-group-p-function
)
8680 (funcall pos-visible-in-window-group-p-function pos window partially
)
8681 (pos-visible-in-window-p pos window partially
)))
8683 (defvar selected-window-group-function nil
)
8684 (make-variable-buffer-local 'selected-window-group-function
)
8685 (put 'selected-window-group-function
'permanent-local t
)
8686 (defun selected-window-group ()
8687 "Return the list of windows in the group containing the selected window.
8688 When a grouping mode (such as Follow Mode) is not active, the
8689 result is a list containing only the selected window."
8690 (if (functionp selected-window-group-function
)
8691 (funcall selected-window-group-function
)
8692 (list (selected-window))))
8694 (defvar move-to-window-group-line-function nil
)
8695 (make-variable-buffer-local 'move-to-window-group-line-function
)
8696 (put 'move-to-window-group-line-function
'permanent-local t
)
8697 (defun move-to-window-group-line (arg)
8698 "Position point relative to the current group of windows.
8699 When a grouping mode (such as Follow Mode) is not active, this
8700 function is identical to `move-to-window-line'.
8702 ARG nil means position point at center of the window group.
8703 Else, ARG specifies the vertical position within the window
8704 group; zero means top of first window in the group, negative
8705 means relative to the bottom of the last window in the group."
8706 (if (functionp move-to-window-group-line-function
)
8707 (funcall move-to-window-group-line-function arg
)
8708 (move-to-window-line arg
)))
8711 (defvar recenter-last-op nil
8712 "Indicates the last recenter operation performed.
8713 Possible values: `top', `middle', `bottom', integer or float numbers.
8714 It can also be nil, which means the first value in `recenter-positions'.")
8716 (defcustom recenter-positions
'(middle top bottom
)
8717 "Cycling order for `recenter-top-bottom'.
8718 A list of elements with possible values `top', `middle', `bottom',
8719 integer or float numbers that define the cycling order for
8720 the command `recenter-top-bottom'.
8722 Top and bottom destinations are `scroll-margin' lines from the true
8723 window top and bottom. Middle redraws the frame and centers point
8724 vertically within the window. Integer number moves current line to
8725 the specified absolute window-line. Float number between 0.0 and 1.0
8726 means the percentage of the screen space from the top. The default
8727 cycling order is middle -> top -> bottom."
8728 :type
'(repeat (choice
8729 (const :tag
"Top" top
)
8730 (const :tag
"Middle" middle
)
8731 (const :tag
"Bottom" bottom
)
8732 (integer :tag
"Line number")
8733 (float :tag
"Percentage")))
8737 (defun recenter-top-bottom (&optional arg
)
8738 "Move current buffer line to the specified window line.
8739 With no prefix argument, successive calls place point according
8740 to the cycling order defined by `recenter-positions'.
8742 A prefix argument is handled like `recenter':
8743 With numeric prefix ARG, move current line to window-line ARG.
8744 With plain `C-u', move current line to window center."
8747 (arg (recenter arg
)) ; Always respect ARG.
8749 (setq recenter-last-op
8750 (if (eq this-command last-command
)
8751 (car (or (cdr (member recenter-last-op recenter-positions
))
8752 recenter-positions
))
8753 (car recenter-positions
)))
8754 (let ((this-scroll-margin
8755 (min (max 0 scroll-margin
)
8756 (truncate (/ (window-body-height) 4.0)))))
8757 (cond ((eq recenter-last-op
'middle
)
8759 ((eq recenter-last-op
'top
)
8760 (recenter this-scroll-margin
))
8761 ((eq recenter-last-op
'bottom
)
8762 (recenter (- -
1 this-scroll-margin
)))
8763 ((integerp recenter-last-op
)
8764 (recenter recenter-last-op
))
8765 ((floatp recenter-last-op
)
8766 (recenter (round (* recenter-last-op
(window-height))))))))))
8768 (define-key global-map
[?\C-l
] 'recenter-top-bottom
)
8770 (defun move-to-window-line-top-bottom (&optional arg
)
8771 "Position point relative to window.
8773 With a prefix argument ARG, acts like `move-to-window-line'.
8775 With no argument, positions point at center of window.
8776 Successive calls position point at positions defined
8777 by `recenter-positions'."
8780 (arg (move-to-window-line arg
)) ; Always respect ARG.
8782 (setq recenter-last-op
8783 (if (eq this-command last-command
)
8784 (car (or (cdr (member recenter-last-op recenter-positions
))
8785 recenter-positions
))
8786 (car recenter-positions
)))
8787 (let ((this-scroll-margin
8788 (min (max 0 scroll-margin
)
8789 (truncate (/ (window-body-height) 4.0)))))
8790 (cond ((eq recenter-last-op
'middle
)
8791 (call-interactively 'move-to-window-line
))
8792 ((eq recenter-last-op
'top
)
8793 (move-to-window-line this-scroll-margin
))
8794 ((eq recenter-last-op
'bottom
)
8795 (move-to-window-line (- -
1 this-scroll-margin
)))
8796 ((integerp recenter-last-op
)
8797 (move-to-window-line recenter-last-op
))
8798 ((floatp recenter-last-op
)
8799 (move-to-window-line (round (* recenter-last-op
(window-height))))))))))
8801 (define-key global-map
[?\M-r
] 'move-to-window-line-top-bottom
)
8803 ;;; Scrolling commands.
8805 ;;; Scrolling commands which do not signal errors at top/bottom
8806 ;;; of buffer at first key-press (instead move to top/bottom
8809 (defcustom scroll-error-top-bottom nil
8810 "Move point to top/bottom of buffer before signaling a scrolling error.
8811 A value of nil means just signal an error if no more scrolling possible.
8812 A value of t means point moves to the beginning or the end of the buffer
8813 \(depending on scrolling direction) when no more scrolling possible.
8814 When point is already on that position, then signal an error."
8819 (defun scroll-up-command (&optional arg
)
8820 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
8821 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
8822 scroll window further, move cursor to the bottom line.
8823 When point is already on that position, then signal an error.
8824 A near full screen is `next-screen-context-lines' less than a full screen.
8825 Negative ARG means scroll downward.
8826 If ARG is the atom `-', scroll downward by nearly full screen."
8829 ((null scroll-error-top-bottom
)
8832 (scroll-down-command nil
))
8833 ((< (prefix-numeric-value arg
) 0)
8834 (scroll-down-command (- (prefix-numeric-value arg
))))
8836 (scroll-up arg
)) ; signal error
8842 ;; When scrolling by ARG lines can't be done,
8843 ;; move by ARG lines instead.
8845 ;; When ARG is nil for full-screen scrolling,
8846 ;; move to the bottom of the buffer.
8847 (goto-char (point-max))))))))
8849 (put 'scroll-up-command
'scroll-command t
)
8851 (defun scroll-down-command (&optional arg
)
8852 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
8853 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
8854 scroll window further, move cursor to the top line.
8855 When point is already on that position, then signal an error.
8856 A near full screen is `next-screen-context-lines' less than a full screen.
8857 Negative ARG means scroll upward.
8858 If ARG is the atom `-', scroll upward by nearly full screen."
8861 ((null scroll-error-top-bottom
)
8864 (scroll-up-command nil
))
8865 ((< (prefix-numeric-value arg
) 0)
8866 (scroll-up-command (- (prefix-numeric-value arg
))))
8868 (scroll-down arg
)) ; signal error
8872 (beginning-of-buffer
8874 ;; When scrolling by ARG lines can't be done,
8875 ;; move by ARG lines instead.
8876 (forward-line (- arg
))
8877 ;; When ARG is nil for full-screen scrolling,
8878 ;; move to the top of the buffer.
8879 (goto-char (point-min))))))))
8881 (put 'scroll-down-command
'scroll-command t
)
8883 ;;; Scrolling commands which scroll a line instead of full screen.
8885 (defun scroll-up-line (&optional arg
)
8886 "Scroll text of selected window upward ARG lines; or one line if no ARG.
8887 If ARG is omitted or nil, scroll upward by one line.
8888 This is different from `scroll-up-command' that scrolls a full screen."
8890 (scroll-up (or arg
1)))
8892 (put 'scroll-up-line
'scroll-command t
)
8894 (defun scroll-down-line (&optional arg
)
8895 "Scroll text of selected window down ARG lines; or one line if no ARG.
8896 If ARG is omitted or nil, scroll down by one line.
8897 This is different from `scroll-down-command' that scrolls a full screen."
8899 (scroll-down (or arg
1)))
8901 (put 'scroll-down-line
'scroll-command t
)
8904 (defun scroll-other-window-down (&optional lines
)
8905 "Scroll the \"other window\" down.
8906 For more details, see the documentation for `scroll-other-window'."
8908 (scroll-other-window
8909 ;; Just invert the argument's meaning.
8910 ;; We can do that without knowing which window it will be.
8911 (if (eq lines
'-
) nil
8913 (- (prefix-numeric-value lines
))))))
8915 (defun beginning-of-buffer-other-window (arg)
8916 "Move point to the beginning of the buffer in the other window.
8917 Leave mark at previous position.
8918 With arg N, put point N/10 of the way from the true beginning."
8920 (let ((orig-window (selected-window))
8921 (window (other-window-for-scrolling)))
8922 ;; We use unwind-protect rather than save-window-excursion
8923 ;; because the latter would preserve the things we want to change.
8926 (select-window window
)
8927 ;; Set point and mark in that window's buffer.
8929 (beginning-of-buffer arg
))
8930 ;; Set point accordingly.
8932 (select-window orig-window
))))
8934 (defun end-of-buffer-other-window (arg)
8935 "Move point to the end of the buffer in the other window.
8936 Leave mark at previous position.
8937 With arg N, put point N/10 of the way from the true end."
8939 ;; See beginning-of-buffer-other-window for comments.
8940 (let ((orig-window (selected-window))
8941 (window (other-window-for-scrolling)))
8944 (select-window window
)
8946 (end-of-buffer arg
))
8948 (select-window orig-window
))))
8950 (defvar mouse-autoselect-window-timer nil
8951 "Timer used by delayed window autoselection.")
8953 (defvar mouse-autoselect-window-position-1 nil
8954 "First mouse position recorded by delayed window autoselection.")
8956 (defvar mouse-autoselect-window-position nil
8957 "Last mouse position recorded by delayed window autoselection.")
8959 (defvar mouse-autoselect-window-window nil
8960 "Last window recorded by delayed window autoselection.")
8962 (defvar mouse-autoselect-window-state nil
8963 "When non-nil, special state of delayed window autoselection.
8964 Possible values are `suspend' (suspend autoselection after a menu or
8965 scrollbar interaction) and `select' (the next invocation of
8966 `handle-select-window' shall select the window immediately).")
8968 (defun mouse-autoselect-window-cancel (&optional force
)
8969 "Cancel delayed window autoselection.
8970 Optional argument FORCE means cancel unconditionally."
8971 (unless (and (not force
)
8972 ;; Don't cancel for select-window or select-frame events
8973 ;; or when the user drags a scroll bar.
8974 (or (memq this-command
8975 '(handle-select-window handle-switch-frame
))
8976 (and (eq this-command
'scroll-bar-toolkit-scroll
)
8977 (memq (nth 4 (event-end last-input-event
))
8978 '(handle end-scroll
)))))
8979 (setq mouse-autoselect-window-state nil
)
8980 (setq mouse-autoselect-window-position-1 nil
)
8981 (when (timerp mouse-autoselect-window-timer
)
8982 (cancel-timer mouse-autoselect-window-timer
))
8983 (remove-hook 'pre-command-hook
'mouse-autoselect-window-cancel
)))
8985 (defun mouse-autoselect-window-start (mouse-position &optional window suspend
)
8986 "Start delayed window autoselection.
8987 MOUSE-POSITION is the last position where the mouse was seen as returned
8988 by `mouse-position'. Optional argument WINDOW non-nil denotes the
8989 window where the mouse was seen. Optional argument SUSPEND non-nil
8990 means suspend autoselection."
8991 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
8992 (setq mouse-autoselect-window-position mouse-position
)
8993 (when window
(setq mouse-autoselect-window-window window
))
8994 (setq mouse-autoselect-window-state
(when suspend
'suspend
))
8995 ;; Install timer which runs `mouse-autoselect-window-select' after
8996 ;; `mouse-autoselect-window' seconds.
8997 (setq mouse-autoselect-window-timer
8999 (abs mouse-autoselect-window
) nil
'mouse-autoselect-window-select
)))
9001 (defun mouse-autoselect-window-select ()
9002 "Select window with delayed window autoselection.
9003 If the mouse position has stabilized in a non-selected window, select
9004 that window. The minibuffer window is selected only if the minibuffer
9005 is active. This function is run by `mouse-autoselect-window-timer'."
9006 (let* ((mouse-position (mouse-position))
9007 (mouse-x (and (numberp (cadr mouse-position
))
9008 (cadr mouse-position
)))
9009 (mouse-y (and (numberp (cddr mouse-position
))
9010 (cddr mouse-position
)))
9011 (frame (and mouse-x mouse-y
(car mouse-position
)))
9012 (window (and frame
(window-at mouse-x mouse-y frame
))))
9014 ((or (and (fboundp 'menu-or-popup-active-p
) (menu-or-popup-active-p))
9016 (let ((coords (coordinates-in-window-p
9017 (cdr mouse-position
) window
)))
9018 (and (not (consp coords
))
9019 (not (memq coords
'(left-margin right-margin
)))))))
9020 ;; A menu / popup dialog is active or the mouse is not on the
9021 ;; text region of WINDOW: Suspend autoselection temporarily.
9022 (mouse-autoselect-window-start mouse-position nil t
))
9023 ((or (eq mouse-autoselect-window-state
'suspend
)
9024 ;; When the mouse is at its first recorded position, restart
9025 ;; delayed autoselection. This works around a scenario with
9026 ;; two two-window frames with identical dimensions: select the
9027 ;; first window of the first frame, switch to the second
9028 ;; frame, move the mouse to its second window, minimize the
9029 ;; second frame. Now the second window of the first frame
9030 ;; gets selected although the mouse never really "moved" into
9032 (and (numberp mouse-autoselect-window
)
9033 (equal (mouse-position) mouse-autoselect-window-position-1
)))
9034 ;; Delayed autoselection was temporarily suspended, reenable it.
9035 (mouse-autoselect-window-start mouse-position
))
9037 (or (not (numberp mouse-autoselect-window
))
9038 (and (>= mouse-autoselect-window
0)
9039 ;; If `mouse-autoselect-window' is non-negative,
9040 ;; select window if it's the same as before.
9041 (eq window mouse-autoselect-window-window
))
9042 ;; Otherwise select window iff the mouse is at the same
9043 ;; position as before. Observe that the first test
9044 ;; after starting autoselection usually fails since the
9045 ;; value of `mouse-autoselect-window-position' recorded
9046 ;; there is the position where the mouse has entered the
9047 ;; new window and not necessarily where the mouse has
9049 (equal mouse-position mouse-autoselect-window-position
))
9050 ;; The minibuffer is a candidate window if it's active.
9051 (or (not (window-minibuffer-p window
))
9052 (eq window
(active-minibuffer-window))))
9053 ;; Mouse position has stabilized in non-selected window: Cancel
9054 ;; delayed autoselection and try to select that window.
9055 (mouse-autoselect-window-cancel t
)
9056 ;; Use `unread-command-events' in order to execute pre- and
9057 ;; post-command hooks and trigger idle timers. To avoid delaying
9058 ;; autoselection again, set `mouse-autoselect-window-state'."
9059 (setq mouse-autoselect-window-state
'select
)
9060 (setq unread-command-events
9061 (cons (list 'select-window
(list window
))
9062 unread-command-events
)))
9063 ((or (not (numberp mouse-autoselect-window
))
9064 (equal mouse-position mouse-autoselect-window-position
))
9065 ;; Mouse position has stabilized at
9066 ;; `mouse-autoselect-window-position': Cancel delayed
9068 (mouse-autoselect-window-cancel t
))
9070 ;; Mouse position has not stabilized yet, resume delayed
9072 (mouse-autoselect-window-start mouse-position window
)))))
9074 (defun handle-select-window (event)
9075 "Handle select-window events."
9077 (let* ((window (posn-window (event-start event
)))
9078 (frame (and (window-live-p window
) (window-frame window
)))
9079 (old-frame (selected-frame)))
9080 (unless (or (not (window-live-p window
))
9081 ;; Don't switch when autoselection shall be delayed.
9082 (and (numberp mouse-autoselect-window
)
9083 (not (eq mouse-autoselect-window-state
'select
))
9084 (let ((position (mouse-position)))
9085 ;; Cancel any delayed autoselection.
9086 (mouse-autoselect-window-cancel t
)
9087 ;; Start delayed autoselection from current mouse
9088 ;; position and window.
9089 (setq mouse-autoselect-window-position-1 position
)
9090 (mouse-autoselect-window-start position window
)
9091 ;; Executing a command cancels delayed autoselection.
9093 'pre-command-hook
'mouse-autoselect-window-cancel
)))
9094 ;; Don't switch to a `no-accept-focus' frame unless it's
9095 ;; already selected.
9096 (and (not (eq frame
(selected-frame)))
9097 (frame-parameter frame
'no-accept-focus
))
9098 ;; Don't switch to minibuffer window unless it's active.
9099 (and (window-minibuffer-p window
)
9100 (not (minibuffer-window-active-p window
))))
9101 ;; Reset state of delayed autoselection.
9102 (setq mouse-autoselect-window-state nil
)
9103 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
9104 (run-hooks 'mouse-leave-buffer-hook
)
9107 ;; Select the window before giving the frame focus since otherwise
9108 ;; we might get two windows with an active cursor.
9109 (select-window window
)
9111 ((or (not (memq (window-system frame
) '(x w32 ns
)))
9112 (not focus-follows-mouse
)
9113 ;; Focus FRAME if it's either a child frame or an ancestor
9114 ;; of the frame switched from.
9115 (and (not (frame-parameter frame
'parent-frame
))
9116 (not (frame-ancestor-p frame old-frame
)))))
9117 ((eq focus-follows-mouse
'auto-raise
)
9118 ;; Focus and auto-raise frame.
9119 (x-focus-frame frame
)
9120 ;; This doesn't seem to work when we move from a normal frame
9121 ;; right into the child frame of another frame - we should raise
9122 ;; that child frame's ancestor frame first ...
9123 (raise-frame frame
))
9125 ;; Just focus frame.
9126 (x-focus-frame frame t
))))))
9128 (defun truncated-partial-width-window-p (&optional window
)
9129 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
9130 WINDOW must be a live window and defaults to the selected one.
9131 Return nil if WINDOW is not a partial-width window
9132 (regardless of the value of `truncate-lines').
9133 Otherwise, consult the value of `truncate-partial-width-windows'
9134 for the buffer shown in WINDOW."
9135 (setq window
(window-normalize-window window t
))
9136 (unless (window-full-width-p window
)
9137 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
9138 (window-buffer window
))))
9139 (if (integerp t-p-w-w
)
9140 (< (window-width window
) t-p-w-w
)
9144 ;; Automatically inform subprocesses of changes to window size.
9146 (defcustom window-adjust-process-window-size-function
9147 'window-adjust-process-window-size-smallest
9148 "Control how Emacs chooses inferior process window sizes.
9149 Emacs uses this function to tell processes the space they have
9150 available for displaying their output. After each window
9151 configuration change, Emacs calls the value of
9152 `window-adjust-process-window-size-function' for each process
9153 with a buffer being displayed in at least one window.
9154 This function is responsible for combining the sizes of the
9155 displayed windows and returning a cons (WIDTH . HEIGHT)
9156 describing the width and height with which Emacs will call
9157 `set-process-window-size' for that process. If the function
9158 returns nil, Emacs does not call `set-process-window-size'.
9160 This function is called with the process buffer as the current
9161 buffer and with two arguments: the process and a list of windows
9162 displaying process. Modes can make this variable buffer-local;
9163 additionally, the `adjust-window-size-function' process property
9164 overrides the global or buffer-local value of
9165 `window-adjust-process-window-size-function'."
9167 (const :tag
"Minimum area of any window"
9168 window-adjust-process-window-size-smallest
)
9169 (const :tag
"Maximum area of any window"
9170 window-adjust-process-window-size-largest
)
9171 (const :tag
"Do not adjust process window sizes" ignore
)
9176 (defun window-adjust-process-window-size (reducer windows
)
9177 "Adjust the window sizes of a process.
9178 WINDOWS is a list of windows associated with that process. REDUCER is
9179 a two-argument function used to combine the widths and heights of
9182 (let ((width (window-max-chars-per-line (car windows
)))
9183 (height (window-body-height (car windows
))))
9184 (dolist (window (cdr windows
))
9185 (setf width
(funcall reducer width
(window-max-chars-per-line window
)))
9186 (setf height
(funcall reducer height
(window-body-height window
))))
9187 (cons width height
))))
9189 (defun window-adjust-process-window-size-smallest (_process windows
)
9190 "Adjust the process window size of PROCESS.
9191 WINDOWS is a list of windows associated with PROCESS. Choose the
9192 smallest area available for displaying PROCESS's output."
9193 (window-adjust-process-window-size #'min windows
))
9195 (defun window-adjust-process-window-size-largest (_process windows
)
9196 "Adjust the process window size of PROCESS.
9197 WINDOWS is a list of windows associated with PROCESS. Choose the
9198 largest area available for displaying PROCESS's output."
9199 (window-adjust-process-window-size #'max windows
))
9201 (defun window--process-window-list ()
9202 "Return an alist mapping processes to associated windows.
9203 A window is associated with a process if that window is
9204 displaying that processes's buffer."
9205 (let ((processes (process-list))
9206 (process-windows nil
))
9210 (let ((buffer (window-buffer window
))
9212 (while (let ((process (car iter
)))
9213 (if (and (process-live-p process
)
9214 (eq buffer
(process-buffer process
)))
9215 (let ((procwin (assq process process-windows
)))
9216 ;; Add this window to the list of windows
9217 ;; displaying process.
9219 (push window
(cdr procwin
))
9220 (push (list process window
) process-windows
))
9221 ;; We found our process for this window, so
9222 ;; stop iterating over the process list.
9224 (setf iter
(cdr iter
)))))))
9228 (defun window--adjust-process-windows ()
9229 "Update process window sizes to match the current window configuration."
9230 (when (fboundp 'process-list
)
9231 (dolist (procwin (window--process-window-list))
9232 (let ((process (car procwin
)))
9233 (with-demoted-errors "Error adjusting window size: %S"
9234 (with-current-buffer (process-buffer process
)
9235 (let ((size (funcall
9236 (or (process-get process
'adjust-window-size-function
)
9237 window-adjust-process-window-size-function
)
9238 process
(cdr procwin
))))
9240 (set-process-window-size process
(cdr size
) (car size
))))))))))
9242 (add-hook 'window-configuration-change-hook
'window--adjust-process-windows
)
9245 ;; Some of these are in tutorial--default-keys, so update that if you
9247 (define-key ctl-x-map
"0" 'delete-window
)
9248 (define-key ctl-x-map
"1" 'delete-other-windows
)
9249 (define-key ctl-x-map
"2" 'split-window-below
)
9250 (define-key ctl-x-map
"3" 'split-window-right
)
9251 (define-key ctl-x-map
"o" 'other-window
)
9252 (define-key ctl-x-map
"^" 'enlarge-window
)
9253 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
9254 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
9255 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
9256 (define-key ctl-x-map
"+" 'balance-windows
)
9257 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
9259 ;;; window.el ends here