1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2015 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 <http://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
)
239 (with-current-buffer ,buffer
240 (setq ,window
(temp-buffer-window-show
242 ;; Remove window-height when it's handled below.
243 (if (functionp (cdr (assq 'window-height
(cdr ,vaction
))))
244 (assq-delete-all 'window-height
(copy-sequence ,vaction
))
247 (let ((inhibit-read-only t
)
248 (inhibit-modification-hooks t
))
249 (setq ,value
(progn ,@body
)))
251 (set-window-point ,window
(point-min))
253 (when (functionp (cdr (assq 'window-height
(cdr ,vaction
))))
255 (funcall (cdr (assq 'window-height
(cdr ,vaction
))) ,window
)))
257 (when (consp (cdr (assq 'preserve-size
(cdr ,vaction
))))
258 (window-preserve-size
259 ,window t
(cadr (assq 'preserve-size
(cdr ,vaction
))))
260 (window-preserve-size
261 ,window nil
(cddr (assq 'preserve-size
(cdr ,vaction
)))))
263 (if (functionp ,vquit-function
)
264 (funcall ,vquit-function
,window
,value
)
267 ;; The following two functions are like `window-next-sibling' and
268 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
269 ;; they don't substitute the selected window for nil), and they return
270 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
271 ;; a minibuffer window).
272 (defun window-right (window)
273 "Return WINDOW's right sibling.
274 Return nil if WINDOW is the root window of its frame. WINDOW can
276 (and window
(window-parent window
) (window-next-sibling window
)))
278 (defun window-left (window)
279 "Return WINDOW's left sibling.
280 Return nil if WINDOW is the root window of its frame. WINDOW can
282 (and window
(window-parent window
) (window-prev-sibling window
)))
284 (defun window-child (window)
285 "Return WINDOW's first child window.
286 WINDOW can be any window."
287 (or (window-top-child window
) (window-left-child window
)))
289 (defun window-child-count (window)
290 "Return number of WINDOW's child windows.
291 WINDOW can be any window."
293 (when (and (windowp window
) (setq window
(window-child window
)))
295 (setq count
(1+ count
))
296 (setq window
(window-next-sibling window
))))
299 (defun window-last-child (window)
300 "Return last child window of WINDOW.
301 WINDOW can be any window."
302 (when (and (windowp window
) (setq window
(window-child window
)))
303 (while (window-next-sibling window
)
304 (setq window
(window-next-sibling window
))))
307 (defun window-normalize-buffer (buffer-or-name)
308 "Return buffer specified by BUFFER-OR-NAME.
309 BUFFER-OR-NAME must be either a buffer or a string naming a live
310 buffer and defaults to the current buffer."
312 ((not buffer-or-name
)
314 ((bufferp buffer-or-name
)
315 (if (buffer-live-p buffer-or-name
)
317 (error "Buffer %s is not a live buffer" buffer-or-name
)))
318 ((get-buffer buffer-or-name
))
320 (error "No such buffer %s" buffer-or-name
))))
322 (defun window-normalize-frame (frame)
323 "Return frame specified by FRAME.
324 FRAME must be a live frame and defaults to the selected frame."
326 (if (frame-live-p frame
)
328 (error "%s is not a live frame" frame
))
331 (defun window-normalize-window (window &optional live-only
)
332 "Return the window specified by WINDOW.
333 If WINDOW is nil, return the selected window. Otherwise, if
334 WINDOW is a live or an internal window, return WINDOW; if
335 LIVE-ONLY is non-nil, return WINDOW for a live window only.
336 Otherwise, signal an error."
341 (if (window-live-p window
)
343 (error "%s is not a live window" window
)))
344 ((window-valid-p window
)
347 (error "%s is not a valid window" window
))))
349 ;; Maybe this should go to frame.el.
350 (defun frame-char-size (&optional window-or-frame horizontal
)
351 "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
352 If WINDOW-OR-FRAME is a live frame, return the value of
353 `frame-char-height' for that frame. If WINDOW-OR-FRAME is a
354 valid window, return the value of `frame-char-height' for that
355 window's frame. In any other case, return the value of
356 `frame-char-height' for the selected frame.
358 Optional argument HORIZONTAL non-nil means to return the value of
359 `frame-char-width' for WINDOW-OR-FRAME."
362 ((window-valid-p window-or-frame
)
363 (window-frame window-or-frame
))
364 ((frame-live-p window-or-frame
)
366 (t (selected-frame)))))
368 (frame-char-width frame
)
369 (frame-char-height frame
))))
371 (defvar ignore-window-parameters nil
372 "If non-nil, standard functions ignore window parameters.
373 The functions currently affected by this are `split-window',
374 `delete-window', `delete-other-windows' and `other-window'.
376 An application may bind this to a non-nil value around calls to
377 these functions to inhibit processing of window parameters.")
379 ;; This must go to C, finally (or get removed).
380 (defconst window-safe-min-height
1
381 "The absolute minimum number of lines of any window.
382 Anything less might crash Emacs.")
384 (defun window-safe-min-pixel-height (&optional window
)
385 "Return the absolute minimum pixel height of WINDOW."
386 (* window-safe-min-height
387 (frame-char-size (window-normalize-window window
))))
389 (defcustom window-min-height
4
390 "The minimum total height, in lines, of any window.
391 The value has to accommodate one text line, a mode and header
392 line, a horizontal scroll bar and a bottom divider, if present.
393 A value less than `window-safe-min-height' is ignored. The value
394 of this variable is honored when windows are resized or split.
396 Applications should never rebind this variable. To resize a
397 window to a height less than the one specified here, an
398 application should instead call `window-resize' with a non-nil
399 IGNORE argument. In order to have `split-window' make a window
400 shorter, explicitly specify the SIZE argument of that function."
405 (defun window-min-pixel-height (&optional window
)
406 "Return the minimum pixel height of window WINDOW."
407 (* (max window-min-height window-safe-min-height
)
408 (frame-char-size window
)))
410 ;; This must go to C, finally (or get removed).
411 (defconst window-safe-min-width
2
412 "The absolute minimum number of columns of a window.
413 Anything less might crash Emacs.")
415 (defun window-safe-min-pixel-width (&optional window
)
416 "Return the absolute minimum pixel width of WINDOW."
417 (* window-safe-min-width
418 (frame-char-size (window-normalize-window window
) t
)))
420 (defcustom window-min-width
10
421 "The minimum total width, in columns, of any window.
422 The value has to accommodate two text columns as well as margins,
423 fringes, a scroll bar and a right divider, if present. A value
424 less than `window-safe-min-width' is ignored. The value of this
425 variable is honored when windows are resized or split.
427 Applications should never rebind this variable. To resize a
428 window to a width less than the one specified here, an
429 application should instead call `window-resize' with a non-nil
430 IGNORE argument. In order to have `split-window' make a window
431 narrower, explicitly specify the SIZE argument of that function."
436 (defun window-min-pixel-width (&optional window
)
437 "Return the minimum pixel width of window WINDOW."
438 (* (max window-min-width window-safe-min-width
)
439 (frame-char-size window t
)))
441 (defun window-safe-min-pixel-size (&optional window horizontal
)
442 "Return the absolute minimum pixel height of WINDOW.
443 Optional argument HORIZONTAL non-nil means return the absolute
444 minimum pixel width of WINDOW."
446 (window-safe-min-pixel-width window
)
447 (window-safe-min-pixel-height window
)))
449 (defun window-min-pixel-size (&optional window horizontal
)
450 "Return the minimum pixel height of WINDOW.
451 Optional argument HORIZONTAL non-nil means return the minimum
452 pixel width of WINDOW."
454 (window-min-pixel-width window
)
455 (window-min-pixel-height window
)))
457 (defun window-combined-p (&optional window horizontal
)
458 "Return non-nil if WINDOW has siblings in a given direction.
459 WINDOW must be a valid window and defaults to the selected one.
461 HORIZONTAL determines a direction for the window combination. If
462 HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
463 a vertical window combination. If HORIZONTAL is non-nil, return
464 non-nil if WINDOW is part of a horizontal window combination."
465 (setq window
(window-normalize-window window
))
466 (let ((parent (window-parent window
)))
469 (window-left-child parent
)
470 (window-top-child parent
)))))
472 (defun window-combination-p (&optional window horizontal
)
473 "Return WINDOW's first child if WINDOW is a vertical combination.
474 WINDOW can be any window and defaults to the selected one.
475 Optional argument HORIZONTAL non-nil means return WINDOW's first
476 child if WINDOW is a horizontal combination."
477 (setq window
(window-normalize-window window
))
479 (window-left-child window
)
480 (window-top-child window
)))
482 (defun window-combinations (window &optional horizontal
)
483 "Return largest number of windows vertically arranged within WINDOW.
484 WINDOW must be a valid window and defaults to the selected one.
485 If HORIZONTAL is non-nil, return the largest number of
486 windows horizontally arranged within WINDOW."
487 (setq window
(window-normalize-window window
))
489 ((window-live-p window
)
490 ;; If WINDOW is live, return 1.
493 (window-left-child window
)
494 (window-top-child window
))
495 ;; If WINDOW is iso-combined, return the sum of the values for all
496 ;; child windows of WINDOW.
497 (let ((child (window-child window
))
501 (+ (window-combinations child horizontal
)
503 (setq child
(window-right child
)))
506 ;; If WINDOW is not iso-combined, return the maximum value of any
507 ;; child window of WINDOW.
508 (let ((child (window-child window
))
512 (max (window-combinations child horizontal
)
514 (setq child
(window-right child
)))
517 (defun walk-window-tree-1 (fun walk-window-tree-window any
&optional sub-only
)
518 "Helper function for `walk-window-tree' and `walk-window-subtree'."
519 (let (walk-window-tree-buffer)
520 (while walk-window-tree-window
521 (setq walk-window-tree-buffer
522 (window-buffer walk-window-tree-window
))
523 (when (or walk-window-tree-buffer any
)
524 (funcall fun walk-window-tree-window
))
525 (unless walk-window-tree-buffer
527 fun
(window-left-child walk-window-tree-window
) any
)
529 fun
(window-top-child walk-window-tree-window
) any
))
531 (setq walk-window-tree-window nil
)
532 (setq walk-window-tree-window
533 (window-right walk-window-tree-window
))))))
535 (defun walk-window-tree (fun &optional frame any minibuf
)
536 "Run function FUN on each live window of FRAME.
537 FUN must be a function with one argument - a window. FRAME must
538 be a live frame and defaults to the selected one. ANY, if
539 non-nil, means to run FUN on all live and internal windows of
542 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
543 window even if it isn't active. MINIBUF nil or omitted means run
544 FUN on FRAME's minibuffer window only if it's active. In both
545 cases the minibuffer window must be part of FRAME. MINIBUF
546 neither nil nor t means never run FUN on the minibuffer window.
548 This function performs a pre-order, depth-first traversal of the
549 window tree. If FUN changes the window tree, the result is
551 (setq frame
(window-normalize-frame frame
))
552 (walk-window-tree-1 fun
(frame-root-window frame
) any
)
553 (when (memq minibuf
'(nil t
))
554 ;; Run FUN on FRAME's minibuffer window if requested.
555 (let ((minibuffer-window (minibuffer-window frame
)))
556 (when (and (window-live-p minibuffer-window
)
557 (eq (window-frame minibuffer-window
) frame
)
559 (minibuffer-window-active-p minibuffer-window
)))
560 (funcall fun minibuffer-window
)))))
562 (defun walk-window-subtree (fun &optional window any
)
563 "Run function FUN on the subtree of windows rooted at WINDOW.
564 WINDOW defaults to the selected window. FUN must be a function
565 with one argument - a window. By default, run FUN only on live
566 windows of the subtree. If the optional argument ANY is non-nil,
567 run FUN on all live and internal windows of the subtree. If
568 WINDOW is live, run FUN on WINDOW only.
570 This function performs a pre-order, depth-first traversal of the
571 subtree rooted at WINDOW. If FUN changes that tree, the result
573 (setq window
(window-normalize-window window
))
574 (walk-window-tree-1 fun window any t
))
576 (defun window-with-parameter (parameter &optional value frame any minibuf
)
577 "Return first window on FRAME with PARAMETER non-nil.
578 FRAME defaults to the selected frame. Optional argument VALUE
579 non-nil means only return a window whose window-parameter value
580 for PARAMETER equals VALUE (comparison is done with `equal').
581 Optional argument ANY non-nil means consider internal windows
584 Optional argument MINIBUF t means consider FRAME's minibuffer
585 window even if it isn't active. MINIBUF nil or omitted means
586 consider FRAME's minibuffer window only if it's active. In both
587 cases the minibuffer window must be part of FRAME. MINIBUF
588 neither nil nor t means never consider the minibuffer window."
593 (when (and (setq this-value
(window-parameter window parameter
))
594 (or (not value
) (equal value this-value
)))
595 (throw 'found window
)))
596 frame any minibuf
))))
599 (defun window-atom-root (&optional window
)
600 "Return root of atomic window WINDOW is a part of.
601 WINDOW must be a valid window and defaults to the selected one.
602 Return nil if WINDOW is not part of an atomic window."
603 (setq window
(window-normalize-window window
))
605 (while (and window
(window-parameter window
'window-atom
))
607 (setq window
(window-parent window
)))
610 (defun window-make-atom (window)
611 "Make WINDOW an atomic window.
612 WINDOW must be an internal window. Return WINDOW."
613 (if (not (window-child window
))
614 (error "Window %s is not an internal window" window
)
617 (unless (window-parameter window
'window-atom
)
618 (set-window-parameter window
'window-atom t
)))
622 (defun display-buffer-in-atom-window (buffer alist
)
623 "Display BUFFER in an atomic window.
624 This function displays BUFFER in a new window that will be
625 combined with an existing window to form an atomic window. If
626 the existing window is already part of an atomic window, add the
627 new window to that atomic window. Operations like `split-window'
628 or `delete-window', when applied to a constituent of an atomic
629 window, are applied atomically to the root of that atomic window.
631 ALIST is an association list of symbols and values. The
632 following symbols can be used.
634 `window' specifies the existing window the new window shall be
635 combined with. Use `window-atom-root' to make the new window a
636 sibling of an atomic window's root. If an internal window is
637 specified here, all children of that window become part of the
638 atomic window too. If no window is specified, the new window
639 becomes a sibling of the selected window. By default, the
640 `window-atom' parameter of the existing window is set to `main'
641 provided it is live and was not set before.
643 `side' denotes the side of the existing window where the new
644 window shall be located. Valid values are `below', `right',
645 `above' and `left'. The default is `below'. By default, the
646 `window-atom' parameter of the new window is set to this value.
648 The return value is the new window, nil when creating that window
650 (let* ((ignore-window-parameters t
)
651 (window-combination-limit t
)
652 (window-combination-resize 'atom
)
653 (window (cdr (assq 'window alist
)))
654 (side (cdr (assq 'side alist
)))
655 (atom (when window
(window-parameter window
'window-atom
)))
657 (setq window
(window-normalize-window window
))
658 (setq root
(window-atom-root window
))
659 ;; Split off new window.
660 (when (setq new
(split-window window nil side
))
662 (if (and root
(not (eq root window
)))
663 ;; When WINDOW was part of an atomic window and we did not
664 ;; split its root, root atomic window at old root.
666 ;; Otherwise, root atomic window at WINDOW's new parent.
667 (window-parent window
)))
668 ;; Assign `window-atom' parameters, if needed.
669 (when (and (not atom
) (window-live-p window
))
670 (set-window-parameter window
'window-atom
'main
))
671 (set-window-parameter new
'window-atom side
)
672 ;; Display BUFFER in NEW and return NEW.
673 (window--display-buffer
674 buffer new
'window alist display-buffer-mark-dedicated
))))
676 (defun window--atom-check-1 (window)
677 "Subroutine of `window--atom-check'."
679 (if (window-parameter window
'window-atom
)
681 (when (or (catch 'reset
684 (if (window-parameter window
'window-atom
)
685 (setq count
(1+ count
))
688 ;; count >= 1 must hold here. If there's no other
689 ;; window around dissolve this atomic window.
691 ;; Dissolve atomic window.
694 (set-window-parameter window
'window-atom nil
))
697 (unless (window-buffer window
)
698 (window--atom-check-1 (window-left-child window
))
699 (window--atom-check-1 (window-top-child window
))))
700 ;; Check right sibling
701 (window--atom-check-1 (window-right window
))))
703 (defun window--atom-check (&optional frame
)
704 "Check atomicity of all windows on FRAME.
705 FRAME defaults to the selected frame. If an atomic window is
706 wrongly configured, reset the atomicity of all its windows on
707 FRAME to nil. An atomic window is wrongly configured if it has
708 no child windows or one of its child windows is not atomic."
709 (window--atom-check-1 (frame-root-window frame
)))
712 (defvar window-sides
'(left top right bottom
)
715 (defcustom window-sides-vertical nil
716 "If non-nil, left and right side windows are full height.
717 Otherwise, top and bottom side windows are full width."
722 (defcustom window-sides-slots
'(nil nil nil nil
)
723 "Maximum number of side window slots.
724 The value is a list of four elements specifying the number of
725 side window slots on (in this order) the left, top, right and
726 bottom side of each frame. If an element is a number, this means
727 to display at most that many side windows on the corresponding
728 side. If an element is nil, this means there's no bound on the
729 number of slots on that side."
734 :value
(nil nil nil nil
)
737 :help-echo
"Maximum slots of left side window."
739 :format
"%[Left%] %v\n"
740 (const :tag
"Unlimited" :format
"%t" nil
)
741 (integer :tag
"Number" :value
2 :size
5))
744 :help-echo
"Maximum slots of top side window."
746 :format
"%[Top%] %v\n"
747 (const :tag
"Unlimited" :format
"%t" nil
)
748 (integer :tag
"Number" :value
3 :size
5))
751 :help-echo
"Maximum slots of right side window."
753 :format
"%[Right%] %v\n"
754 (const :tag
"Unlimited" :format
"%t" nil
)
755 (integer :tag
"Number" :value
2 :size
5))
758 :help-echo
"Maximum slots of bottom side window."
760 :format
"%[Bottom%] %v\n"
761 (const :tag
"Unlimited" :format
"%t" nil
)
762 (integer :tag
"Number" :value
3 :size
5)))
765 (defun window--side-window-p (window)
766 "Return non-nil if WINDOW is a side window or the parent of one."
767 (or (window-parameter window
'window-side
)
768 (and (window-child window
)
769 (or (window-parameter
770 (window-child window
) 'window-side
)
772 (window-last-child window
) 'window-side
)))))
774 (defun window--major-non-side-window (&optional frame
)
775 "Return the major non-side window of frame FRAME.
776 The optional argument FRAME must be a live frame and defaults to
779 If FRAME has at least one side window, the major non-side window
780 is either an internal non-side window such that all other
781 non-side windows on FRAME descend from it, or the single live
782 non-side window of FRAME. If FRAME has no side windows, return
784 (let ((frame (window-normalize-frame frame
))
786 ;; Set major to the _last_ window found by `walk-window-tree' that
787 ;; is not a side window but has a side window as its sibling.
790 (and (not (window-parameter window
'window-side
))
791 (or (and (setq sibling
(window-prev-sibling window
))
792 (window-parameter sibling
'window-side
))
793 (and (setq sibling
(window-next-sibling window
))
794 (window-parameter sibling
'window-side
)))
795 (setq major window
)))
797 (or major
(frame-root-window frame
))))
799 (defun window--major-side-window (side)
800 "Return major side window on SIDE.
801 SIDE must be one of the symbols `left', `top', `right' or
802 `bottom'. Return nil if no such window exists."
803 (let ((root (frame-root-window))
805 ;; (1) If a window on the opposite side exists, return that window's
807 ;; (2) If the new window shall span the entire side, return the
808 ;; frame's root window.
809 ;; (3) If a window on an orthogonal side exists, return that
811 ;; (4) Otherwise return the frame's root window.
813 ((or (and (eq side
'left
)
814 (setq window
(window-with-parameter 'window-side
'right nil t
)))
816 (setq window
(window-with-parameter 'window-side
'bottom nil t
))))
817 (window-prev-sibling window
))
818 ((or (and (eq side
'right
)
819 (setq window
(window-with-parameter 'window-side
'left nil t
)))
820 (and (eq side
'bottom
)
821 (setq window
(window-with-parameter 'window-side
'top nil t
))))
822 (window-next-sibling window
))
823 ((memq side
'(left right
))
825 (window-sides-vertical
827 ((setq window
(window-with-parameter 'window-side
'top nil t
))
828 (window-next-sibling window
))
829 ((setq window
(window-with-parameter 'window-side
'bottom nil t
))
830 (window-prev-sibling window
))
832 ((memq side
'(top bottom
))
834 ((not window-sides-vertical
)
836 ((setq window
(window-with-parameter 'window-side
'left nil t
))
837 (window-next-sibling window
))
838 ((setq window
(window-with-parameter 'window-side
'right nil t
))
839 (window-prev-sibling window
))
842 (defun display-buffer-in-major-side-window (buffer side slot
&optional alist
)
843 "Display BUFFER in a new window on SIDE of the selected frame.
844 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
845 specifies the slot to use. ALIST is an association list of
846 symbols and values as passed to `display-buffer-in-side-window'.
847 This function may be called only if no window on SIDE exists yet.
848 The new window automatically becomes the \"major\" side window on
849 SIDE. Return the new window, nil if its creation window failed."
850 (let* ((left-or-right (memq side
'(left right
)))
851 (major (window--major-side-window side
))
853 ((eq side
'top
) 'above
)
854 ((eq side
'bottom
) 'below
)
856 ;; The following two bindings will tell `split-window' to take
857 ;; the space for the new window from `major' and not make a new
858 ;; parent window unless needed.
859 (window-combination-resize 'side
)
860 (window-combination-limit nil
)
861 (new (split-window major nil on-side
)))
863 ;; Initialize `window-side' parameter of new window to SIDE.
864 (set-window-parameter new
'window-side side
)
865 ;; Install `window-slot' parameter of new window.
866 (set-window-parameter new
'window-slot slot
)
867 ;; Install `delete-window' parameter thus making sure that when
868 ;; the new window is deleted, a side window on the opposite side
869 ;; does not get resized.
870 (set-window-parameter new
'delete-window
'delete-side-window
)
871 ;; Auto-adjust height/width of new window unless a size has been
872 ;; explicitly requested.
873 (unless (if left-or-right
874 (cdr (assq 'window-width alist
))
875 (cdr (assq 'window-height alist
)))
879 (if left-or-right
'window-width
'window-height
)
880 (/ (window-total-size (frame-root-window) left-or-right
)
881 ;; By default use a fourth of the size of the frame's
885 ;; Install BUFFER in new window and return NEW.
886 (window--display-buffer buffer new
'window alist
'side
))))
888 (defun delete-side-window (window)
889 "Delete side window WINDOW."
890 (let ((window-combination-resize
891 (window-parameter (window-parent window
) 'window-side
))
892 (ignore-window-parameters t
))
893 (delete-window window
)))
895 (defun display-buffer-in-side-window (buffer alist
)
896 "Display BUFFER in a side window of the selected frame.
897 ALIST is an association list of symbols and values. The
898 following special symbols can be used in ALIST.
900 `side' denotes the side of the frame where the new window shall
901 be located. Valid values are `bottom', `right', `top' and
902 `left'. The default is `bottom'.
904 `slot' if non-nil, specifies the window slot where to display
905 BUFFER. A value of zero or nil means use the middle slot on
906 the specified side. A negative value means use a slot
907 preceding (that is, above or on the left of) the middle slot.
908 A positive value means use a slot following (that is, below or
909 on the right of) the middle slot. The default is zero."
910 (let ((side (or (cdr (assq 'side alist
)) 'bottom
))
911 (slot (or (cdr (assq 'slot alist
)) 0)))
913 ((not (memq side
'(top bottom left right
)))
914 (error "Invalid side %s specified" side
))
915 ((not (numberp slot
))
916 (error "Invalid slot %s specified" slot
)))
918 (let* ((major (window-with-parameter 'window-side side nil t
))
919 ;; `major' is the major window on SIDE, `windows' the list of
920 ;; life windows on SIDE.
926 (when (eq (window-parameter window
'window-side
) side
)
927 (setq windows
(cons window windows
))))
929 (nreverse windows
))))
930 (slots (when major
(max 1 (window-child-count major
))))
936 ((eq side
'bottom
) 3))
938 window this-window this-slot prev-window next-window
939 best-window best-slot abs-slot
)
942 ((and (numberp max-slots
) (<= max-slots
0))
943 ;; No side-slots available on this side. Don't create an error,
947 ;; No major window exists on this side, make one.
948 (display-buffer-in-major-side-window buffer side slot alist
))
950 ;; Scan windows on SIDE.
952 (dolist (window windows
)
953 (setq this-slot
(window-parameter window
'window-slot
))
955 ;; The following should not happen and probably be checked
956 ;; by window--side-check.
957 ((not (numberp this-slot
)))
959 ;; A window with a matching slot has been found.
960 (setq this-window window
)
963 ;; Check if this window has a better slot value wrt the
964 ;; slot of the window we want.
966 (if (or (and (> this-slot
0) (> slot
0))
967 (and (< this-slot
0) (< slot
0)))
968 (abs (- slot this-slot
))
969 (+ (abs slot
) (abs this-slot
))))
970 (unless (and best-slot
(<= best-slot abs-slot
))
971 (setq best-window window
)
972 (setq best-slot abs-slot
))
975 (setq prev-window window
))
977 (setq next-window window
)))))))
979 ;; `this-window' is the first window with the same SLOT.
980 ;; `prev-window' is the window with the largest slot < SLOT. A new
981 ;; window will be created after it.
982 ;; `next-window' is the window with the smallest slot > SLOT. A new
983 ;; window will be created before it.
984 ;; `best-window' is the window with the smallest absolute difference
985 ;; of its slot and SLOT.
987 ;; Note: We dedicate the window used softly to its buffer to
988 ;; avoid that "other" (non-side) buffer display functions steal
989 ;; it from us. This must eventually become customizable via
990 ;; ALIST (or, better, avoided in the "other" functions).
992 ;; Reuse `this-window'.
993 (window--display-buffer buffer this-window
'reuse alist
'side
))
994 (and (or (not max-slots
) (< slots max-slots
))
996 ;; Make new window before `next-window'.
998 (if (memq side
'(left right
)) 'above
'left
))
999 (window-combination-resize 'side
))
1000 (setq window
(split-window next-window nil next-side
))
1001 ;; When the new window is deleted, its space
1002 ;; is returned to other side windows.
1003 (set-window-parameter
1004 window
'delete-window
'delete-side-window
)
1007 ;; Make new window after `prev-window'.
1009 (if (memq side
'(left right
)) 'below
'right
))
1010 (window-combination-resize 'side
))
1011 (setq window
(split-window prev-window nil prev-side
))
1012 ;; When the new window is deleted, its space
1013 ;; is returned to other side windows.
1014 (set-window-parameter
1015 window
'delete-window
'delete-side-window
)
1017 (set-window-parameter window
'window-slot slot
)
1018 (window--display-buffer buffer window
'window alist
'side
))
1020 ;; Reuse `best-window'.
1022 ;; Give best-window the new slot value.
1023 (set-window-parameter best-window
'window-slot slot
)
1024 (window--display-buffer
1025 buffer best-window
'reuse alist
'side
)))))))))
1027 (defun window--side-check (&optional frame
)
1028 "Check the side window configuration of FRAME.
1029 FRAME defaults to the selected frame.
1031 A valid side window configuration preserves the following two
1034 - If there exists a window whose window-side parameter is
1035 non-nil, there must exist at least one live window whose
1036 window-side parameter is nil.
1038 - If a window W has a non-nil window-side parameter (i) it must
1039 have a parent window and that parent's window-side parameter
1040 must be either nil or the same as for W, and (ii) any child
1041 window of W must have the same window-side parameter as W.
1043 If the configuration is invalid, reset the window-side parameters
1044 of all windows on FRAME to nil."
1045 (let (left top right bottom none side parent parent-side
)
1046 (when (or (catch 'reset
1049 (setq side
(window-parameter window
'window-side
))
1050 (setq parent
(window-parent window
))
1052 (and parent
(window-parameter parent
'window-side
)))
1053 ;; The following `cond' seems a bit tedious, but I'd
1054 ;; rather stick to using just the stack.
1057 (when (not (eq parent-side side
))
1058 ;; A parent whose window-side is non-nil must
1059 ;; have a child with the same window-side.
1062 (when (window-buffer window
)
1063 ;; Record that we have at least one non-side,
1066 ((if (memq side
'(left top
))
1067 (window-prev-sibling window
)
1068 (window-next-sibling window
))
1069 ;; Left and top major side windows must not have a
1070 ;; previous sibling, right and bottom major side
1071 ;; windows must not have a next sibling.
1073 ;; Now check that there's no more than one major
1074 ;; window for any of left, top, right and bottom.
1076 (if left
(throw 'reset t
) (setq left t
)))
1078 (if top
(throw 'reset t
) (setq top t
)))
1080 (if right
(throw 'reset t
) (setq right t
)))
1082 (if bottom
(throw 'reset t
) (setq bottom t
)))
1086 ;; If there's a side window, there must be at least one
1088 (and (or left top right bottom
) (not none
)))
1091 (set-window-parameter window
'window-side nil
))
1094 (defun window--check (&optional frame
)
1095 "Check atomic and side windows on FRAME.
1096 FRAME defaults to the selected frame."
1097 (window--side-check frame
)
1098 (window--atom-check frame
))
1100 ;; Dumping frame/window contents.
1101 (defun window--dump-window (&optional window erase
)
1102 "Dump WINDOW to buffer *window-frame-dump*.
1103 WINDOW must be a valid window and defaults to the selected one.
1104 Optional argument ERASE non-nil means erase *window-frame-dump*
1105 before writing to it."
1106 (setq window
(window-normalize-window window
))
1107 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1108 (when erase
(erase-buffer))
1110 (format "%s parent: %s\n" window
(window-parent window
))
1111 (format "pixel left: %s top: %s size: %s x %s new: %s\n"
1112 (window-pixel-left window
) (window-pixel-top window
)
1113 (window-size window t t
) (window-size window nil t
)
1114 (window-new-pixel window
))
1115 (format "char left: %s top: %s size: %s x %s new: %s\n"
1116 (window-left-column window
) (window-top-line window
)
1117 (window-total-size window t
) (window-total-size window
)
1118 (window-new-total window
))
1119 (format "normal: %s x %s new: %s\n"
1120 (window-normal-size window t
) (window-normal-size window
)
1121 (window-new-normal window
)))
1122 (when (window-live-p window
)
1123 (let ((fringes (window-fringes window
))
1124 (margins (window-margins window
)))
1126 (format "body pixel: %s x %s char: %s x %s\n"
1127 (window-body-width window t
) (window-body-height window t
)
1128 (window-body-width window
) (window-body-height window
))
1129 (format "width left fringe: %s left margin: %s right margin: %s\n"
1130 (car fringes
) (or (car margins
) 0) (or (cdr margins
) 0))
1131 (format "width right fringe: %s scroll-bar: %s divider: %s\n"
1133 (window-scroll-bar-width window
)
1134 (window-right-divider-width window
))
1135 (format "height header-line: %s mode-line: %s divider: %s\n"
1136 (window-header-line-height window
)
1137 (window-mode-line-height window
)
1138 (window-bottom-divider-width window
)))))
1141 (defun window--dump-frame (&optional window-or-frame
)
1142 "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
1143 WINDOW-OR-FRAME can be a frame or a window and defaults to the
1144 selected frame. When WINDOW-OR-FRAME is a window, dump that
1145 window's frame. The buffer *window-frame-dump* is erased before
1149 ((or (not window-or-frame
)
1150 (frame-live-p window-or-frame
))
1151 (frame-root-window window-or-frame
))
1152 ((or (window-live-p window-or-frame
)
1153 (window-child window-or-frame
))
1156 (frame-root-window))))
1157 (frame (window-frame window
)))
1158 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1161 (format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
1162 (frame-pixel-width frame
) (frame-pixel-height frame
)
1163 (frame-total-cols frame
) (frame-total-lines frame
)
1164 (frame-char-width frame
) (frame-char-height frame
))
1165 (format "frame text pixel: %s x %s cols/lines: %s x %s\n"
1166 (frame-text-width frame
) (frame-text-height frame
)
1167 (frame-text-cols frame
) (frame-text-lines frame
))
1168 (format "tool: %s scroll: %s/%s fringe: %s border: %s right: %s bottom: %s\n\n"
1169 (if (fboundp 'tool-bar-height
)
1170 (tool-bar-height frame t
)
1172 (frame-scroll-bar-width frame
)
1173 (frame-scroll-bar-height frame
)
1174 (frame-fringe-width frame
)
1175 (frame-border-width frame
)
1176 (frame-right-divider-width frame
)
1177 (frame-bottom-divider-width frame
)))
1178 (walk-window-tree 'window--dump-window frame t t
))))
1181 (defun window-total-size (&optional window horizontal round
)
1182 "Return the total height or width of WINDOW.
1183 WINDOW must be a valid window and defaults to the selected one.
1185 If HORIZONTAL is omitted or nil, return the total height of
1186 WINDOW, in lines. If WINDOW is live, its total height includes,
1187 in addition to the height of WINDOW's text, the heights of
1188 WINDOW's mode and header line and a bottom divider, if any.
1190 If HORIZONTAL is non-nil, return the total width of WINDOW, in
1191 columns. If WINDOW is live, its total width includes, in
1192 addition to the width of WINDOW's text, the widths of WINDOW's
1193 fringes, margins, scroll bars and its right divider, if any.
1195 If WINDOW is internal, return the respective size of the screen
1196 areas spanned by its children.
1198 Optional argument ROUND is handled as for `window-total-height'
1199 and `window-total-width'."
1201 (window-total-width window round
)
1202 (window-total-height window round
)))
1204 (defun window-size (&optional window horizontal pixelwise round
)
1205 "Return the height or width of WINDOW.
1206 WINDOW must be a valid window and defaults to the selected one.
1208 If HORIZONTAL is omitted or nil, return the total height of
1209 WINDOW, in lines, like `window-total-height'. Otherwise return
1210 the total width, in columns, like `window-total-width'.
1212 Optional argument PIXELWISE means return the pixel size of WINDOW
1213 like `window-pixel-height' and `window-pixel-width'.
1215 Optional argument ROUND is ignored if PIXELWISE is non-nil and
1216 handled as for `window-total-height' and `window-total-width'
1220 (window-pixel-width window
)
1221 (window-total-width window round
))
1223 (window-pixel-height window
)
1224 (window-total-height window round
))))
1226 (defvar window-size-fixed nil
1227 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
1228 If the value is `height', then only the window's height is fixed.
1229 If the value is `width', then only the window's width is fixed.
1230 Any other non-nil value fixes both the width and the height.
1232 Emacs won't change the size of any window displaying that buffer,
1233 unless it has no other choice (like when deleting a neighboring
1235 (make-variable-buffer-local 'window-size-fixed
)
1237 (defun window--preservable-size (window &optional horizontal
)
1238 "Return height of WINDOW as `window-preserve-size' would preserve it.
1239 Optional argument HORIZONTAL non-nil means to return the width of
1240 WINDOW as `window-preserve-size' would preserve it."
1242 (window-body-width window t
)
1243 (+ (window-body-height window t
)
1244 (window-header-line-height window
)
1245 (window-mode-line-height window
))))
1247 (defun window-preserve-size (&optional window horizontal preserve
)
1248 "Preserve height of window WINDOW.
1249 WINDOW must be a live window and defaults to the selected one.
1250 Optional argument HORIZONTAL non-nil means preserve the width of
1253 PRESERVE t means to preserve the current height/width of WINDOW's
1254 body in frame and window resizing operations whenever possible.
1255 The height/width of WINDOW will change only if Emacs has no other
1256 choice. Resizing a window whose height/width is preserved never
1259 PRESERVE nil means to stop preserving the height/width of WINDOW,
1260 lifting the respective restraint induced by a previous call of
1261 `window-preserve-size' for WINDOW. Calling `enlarge-window',
1262 `shrink-window', `split-window' or `fit-window-to-buffer' with
1263 WINDOW as argument also removes the respective restraint.
1265 Other values of PRESERVE are reserved for future use."
1266 (setq window
(window-normalize-window window t
))
1267 (let* ((parameter (window-parameter window
'window-preserved-size
))
1268 (width (nth 1 parameter
))
1269 (height (nth 2 parameter
)))
1271 (set-window-parameter
1272 window
'window-preserved-size
1274 (window-buffer window
)
1275 (and preserve
(window--preservable-size window t
))
1277 (set-window-parameter
1278 window
'window-preserved-size
1280 (window-buffer window
)
1282 (and preserve
(window--preservable-size window
)))))))
1284 (defun window-preserved-size (&optional window horizontal
)
1285 "Return preserved height of window WINDOW.
1286 WINDOW must be a live window and defaults to the selected one.
1287 Optional argument HORIZONTAL non-nil means to return preserved
1289 (setq window
(window-normalize-window window t
))
1290 (let* ((parameter (window-parameter window
'window-preserved-size
))
1291 (buffer (nth 0 parameter
))
1292 (width (nth 1 parameter
))
1293 (height (nth 2 parameter
)))
1294 (when (eq buffer
(window-buffer window
))
1295 (if horizontal width height
))))
1297 (defun window--preserve-size (window horizontal
)
1298 "Return non-nil when the height of WINDOW shall be preserved.
1299 Optional argument HORIZONTAL non-nil means to return non-nil when
1300 the width of WINDOW shall be preserved."
1301 (let ((size (window-preserved-size window horizontal
)))
1303 (= size
(window--preservable-size window horizontal
)))))
1305 (defun window-safe-min-size (&optional window horizontal pixelwise
)
1306 "Return safe minimum size of WINDOW.
1307 WINDOW must be a valid window and defaults to the selected one.
1308 Optional argument HORIZONTAL non-nil means return the minimum
1309 number of columns of WINDOW; otherwise return the minimum number
1312 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1314 (setq window
(window-normalize-window window
))
1317 (* window-safe-min-width
1318 (frame-char-width (window-frame window
)))
1319 (* window-safe-min-height
1320 (frame-char-height (window-frame window
))))
1321 (if horizontal window-safe-min-width window-safe-min-height
)))
1323 (defun window-min-size (&optional window horizontal ignore pixelwise
)
1324 "Return the minimum size of WINDOW.
1325 WINDOW must be a valid window and defaults to the selected one.
1326 Optional argument HORIZONTAL non-nil means return the minimum
1327 number of columns of WINDOW; otherwise return the minimum number
1330 The optional argument IGNORE has the same meaning as for
1331 `window-resizable'. Optional argument PIXELWISE non-nil means
1332 return the minimum pixel-size of WINDOW."
1334 (window-normalize-window window
) horizontal ignore pixelwise
))
1336 (defun window--min-size-ignore-p (window horizontal ignore
)
1337 "Return non-nil if IGNORE says to ignore height restrictions for WINDOW.
1338 HORIZONTAL non-nil means to return non-nil if IGNORE says to
1339 ignore width restrictions for WINDOW."
1340 (if (window-valid-p ignore
)
1342 (not (memq ignore
'(nil preserved
)))))
1344 (defun window--min-size-1 (window horizontal ignore pixelwise
)
1345 "Internal function of `window-min-size'."
1346 (let ((sub (window-child window
)))
1349 ;; WINDOW is an internal window.
1350 (if (window-combined-p sub horizontal
)
1351 ;; The minimum size of an iso-combination is the sum of
1352 ;; the minimum sizes of its child windows.
1354 (setq value
(+ value
1356 sub horizontal ignore pixelwise
)))
1357 (setq sub
(window-right sub
)))
1358 ;; The minimum size of an ortho-combination is the maximum
1359 ;; of the minimum sizes of its child windows.
1361 (setq value
(max value
1363 sub horizontal ignore pixelwise
)))
1364 (setq sub
(window-right sub
))))
1366 (with-current-buffer (window-buffer window
)
1368 ((window-minibuffer-p window
)
1369 (if pixelwise
(frame-char-height (window-frame window
)) 1))
1370 ((window-size-fixed-p window horizontal ignore
)
1371 ;; The minimum size of a fixed size window is its size.
1372 (window-size window horizontal pixelwise
))
1374 ;; If IGNORE equals `safe' return the safe value.
1375 (window-safe-min-size window horizontal pixelwise
))
1377 ;; For the minimum width of a window take fringes and
1378 ;; scroll-bars into account. This is questionable and should
1379 ;; be removed as soon as we are able to split (and resize)
1380 ;; windows such that the new (or resized) windows can get a
1381 ;; size less than the user-specified `window-min-height' and
1382 ;; `window-min-width'.
1383 (let* ((char-size (frame-char-size window t
))
1384 (fringes (window-fringes window
))
1385 (margins (window-margins window
))
1387 (+ (window-safe-min-size window t t
)
1388 (* (or (car margins
) 0) char-size
)
1389 (* (or (cdr margins
) 0) char-size
)
1390 (car fringes
) (cadr fringes
)
1391 (window-scroll-bar-width window
)
1392 (window-right-divider-width window
))))
1395 (if window-resize-pixelwise
1397 ;; Round up to next integral of columns.
1398 (* (ceiling pixel-width char-size
) char-size
))
1399 (if (window--min-size-ignore-p window horizontal ignore
)
1401 (window-min-pixel-width window
)))
1403 (ceiling pixel-width char-size
)
1404 (if (window--min-size-ignore-p window horizontal ignore
)
1406 window-min-width
)))))
1407 ((let ((char-size (frame-char-size window
))
1409 (+ (window-safe-min-size window nil t
)
1410 (window-header-line-height window
)
1411 (window-scroll-bar-height window
)
1412 (window-mode-line-height window
)
1413 (window-bottom-divider-width window
))))
1416 (if window-resize-pixelwise
1418 ;; Round up to next integral of lines.
1419 (* (ceiling pixel-height char-size
) char-size
))
1420 (if (window--min-size-ignore-p window horizontal ignore
)
1422 (window-min-pixel-height window
)))
1423 (max (ceiling pixel-height char-size
)
1424 (if (window--min-size-ignore-p window horizontal ignore
)
1426 window-min-height
))))))))))
1428 (defun window-sizable (window delta
&optional horizontal ignore pixelwise
)
1429 "Return DELTA if DELTA lines can be added to WINDOW.
1430 WINDOW must be a valid window and defaults to the selected one.
1431 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1432 columns can be added to WINDOW. A return value of zero means
1433 that no lines (or columns) can be added to WINDOW.
1435 This function looks only at WINDOW and, recursively, its child
1436 windows. The function `window-resizable' looks at other windows
1439 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1440 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1441 return the maximum value in the range 0..DELTA by which WINDOW
1444 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1445 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1446 return the minimum value in the range DELTA..0 by which WINDOW
1449 The optional argument IGNORE has the same meaning as for
1450 `window-resizable'. Optional argument PIXELWISE non-nil means
1451 interpret DELTA as pixels."
1452 (setq window
(window-normalize-window window
))
1455 (max (- (window-min-size window horizontal ignore pixelwise
)
1456 (window-size window horizontal pixelwise
))
1459 (if (window-size-fixed-p window horizontal ignore
)
1464 (defun window-sizable-p (window delta
&optional horizontal ignore pixelwise
)
1465 "Return t if WINDOW can be resized by DELTA lines.
1466 WINDOW must be a valid window and defaults to the selected one.
1467 For the meaning of the arguments of this function see the
1468 doc-string of `window-sizable'."
1469 (setq window
(window-normalize-window window
))
1471 (>= (window-sizable window delta horizontal ignore pixelwise
)
1473 (<= (window-sizable window delta horizontal ignore pixelwise
)
1476 (defun window--size-fixed-1 (window horizontal ignore
)
1477 "Internal function for `window-size-fixed-p'."
1478 (let ((sub (window-child window
)))
1481 ;; WINDOW is an internal window.
1482 (if (window-combined-p sub horizontal
)
1483 ;; An iso-combination is fixed size if all its child
1484 ;; windows are fixed-size.
1487 (unless (window--size-fixed-1 sub horizontal ignore
)
1488 ;; We found a non-fixed-size child window, so
1489 ;; WINDOW's size is not fixed.
1491 (setq sub
(window-right sub
)))
1492 ;; All child windows are fixed-size, so WINDOW's size is
1495 ;; An ortho-combination is fixed-size if at least one of its
1496 ;; child windows is fixed-size.
1498 (when (window--size-fixed-1 sub horizontal ignore
)
1499 ;; We found a fixed-size child window, so WINDOW's size
1502 (setq sub
(window-right sub
))))
1503 ;; WINDOW is a live window.
1504 (and (or (not (windowp ignore
)) (not (eq window ignore
)))
1505 (or (and (not (eq ignore
'preserved
))
1506 (window--preserve-size window horizontal
))
1507 (with-current-buffer (window-buffer window
)
1509 (memq window-size-fixed
'(width t
))
1510 (memq window-size-fixed
'(height t
))))))))))
1512 (defun window-size-fixed-p (&optional window horizontal ignore
)
1513 "Return non-nil if WINDOW's height is fixed.
1514 WINDOW must be a valid window and defaults to the selected one.
1515 Optional argument HORIZONTAL non-nil means return non-nil if
1516 WINDOW's width is fixed. The optional argument IGNORE has the
1517 same meaning as for `window-resizable'.
1519 If this function returns nil, this does not necessarily mean that
1520 WINDOW can be resized in the desired direction. The function
1521 `window-resizable' can tell that."
1522 (when (or (windowp ignore
) (memq ignore
'(nil preserved
)))
1523 (window--size-fixed-1
1524 (window-normalize-window window
) horizontal ignore
)))
1526 (defun window--min-delta-1 (window delta
&optional horizontal ignore trail noup pixelwise
)
1527 "Internal function for `window-min-delta'."
1528 (if (not (window-parent window
))
1529 ;; If we can't go up, return zero.
1531 ;; Else try to find a non-fixed-size sibling of WINDOW.
1532 (let* ((parent (window-parent window
))
1533 (sub (window-child parent
)))
1535 (if (window-combined-p sub horizontal
)
1536 ;; In an iso-combination throw DELTA if we find at least one
1537 ;; child window and that window is either not fixed-size or
1538 ;; we can ignore fixed-sizeness.
1539 (let ((skip (eq trail
'after
)))
1543 (setq skip
(eq trail
'before
)))
1545 ((window-size-fixed-p sub horizontal ignore
))
1547 ;; We found a non-fixed-size child window.
1548 (throw 'done delta
)))
1549 (setq sub
(window-right sub
))))
1550 ;; In an ortho-combination set DELTA to the minimum value by
1551 ;; which other child windows can shrink.
1553 (unless (eq sub window
)
1556 (max (- (window-size sub horizontal pixelwise
'ceiling
)
1558 sub horizontal ignore pixelwise
))
1560 (setq sub
(window-right sub
))))
1563 (window--min-delta-1
1564 parent delta horizontal ignore trail nil pixelwise
))))))
1566 (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise
)
1567 "Return number of lines by which WINDOW can be shrunk.
1568 WINDOW must be a valid window and defaults to the selected one.
1569 Return zero if WINDOW cannot be shrunk.
1571 Optional argument HORIZONTAL non-nil means return number of
1572 columns by which WINDOW can be shrunk.
1574 The optional argument IGNORE has the same meaning as for
1575 `window-resizable'. Optional argument TRAIL restricts the
1576 windows that can be enlarged. If its value is `before', only
1577 windows to the left of or above WINDOW can be enlarged. If it is
1578 `after', only windows to the right of or below WINDOW can be
1581 Optional argument NOUP non-nil means don't go up in the window
1582 tree, but try to enlarge windows within WINDOW's combination
1583 only. Optional argument NODOWN non-nil means don't check whether
1584 WINDOW itself (and its child windows) can be shrunk; check only
1585 whether at least one other window can be enlarged appropriately.
1587 Optional argument PIXELWISE non-nil means return number of pixels
1588 by which WINDOW can be shrunk."
1589 (setq window
(window-normalize-window window
))
1590 (let ((size (window-size window horizontal pixelwise
'floor
))
1591 (minimum (window-min-size window horizontal ignore pixelwise
)))
1594 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1595 (window--min-delta-1
1596 window size horizontal ignore trail noup pixelwise
))
1598 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1599 ;; there's nothing to recover.
1602 ;; Otherwise, try to recover whatever WINDOW is larger than its
1604 (window--min-delta-1
1605 window
(- size minimum
) horizontal ignore trail noup pixelwise
)))))
1607 (defun frame-windows-min-size (&optional frame horizontal ignore pixelwise
)
1608 "Return minimum number of lines of FRAME's windows.
1609 HORIZONTAL non-nil means return number of columns of FRAME's
1610 windows. The optional argument IGNORE has the same meaning as
1611 for `window-resizable'. PIXELWISE non-nil means return sizes in
1613 (setq frame
(window-normalize-frame frame
))
1614 (let* ((root (frame-root-window frame
))
1615 (mini (window-next-sibling root
)))
1616 (+ (window-min-size root horizontal ignore pixelwise
)
1617 (if (and mini
(not horizontal
))
1618 (window-min-size mini horizontal nil pixelwise
)
1621 (defun window--max-delta-1 (window delta
&optional horizontal ignore trail noup pixelwise
)
1622 "Internal function of `window-max-delta'."
1623 (if (not (window-parent window
))
1624 ;; Can't go up. Return DELTA.
1626 (let* ((parent (window-parent window
))
1627 (sub (window-child parent
)))
1629 (if (window-combined-p sub horizontal
)
1630 ;; For an iso-combination calculate how much we can get from
1631 ;; other child windows.
1632 (let ((skip (eq trail
'after
)))
1636 (setq skip
(eq trail
'before
)))
1642 (- (window-size sub horizontal pixelwise
'floor
)
1644 sub horizontal ignore pixelwise
))
1646 (setq sub
(window-right sub
))))
1647 ;; For an ortho-combination throw DELTA when at least one
1648 ;; child window is fixed-size.
1650 (when (and (not (eq sub window
))
1651 (window-size-fixed-p sub horizontal ignore
))
1652 (throw 'fixed delta
))
1653 (setq sub
(window-right sub
))))
1655 ;; When NOUP is nil, DELTA is all we can get.
1657 ;; Else try with parent of WINDOW, passing the DELTA we
1658 ;; recovered so far.
1659 (window--max-delta-1
1660 parent delta horizontal ignore trail nil pixelwise
))))))
1662 (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise
)
1663 "Return maximum number of lines by which WINDOW can be enlarged.
1664 WINDOW must be a valid window and defaults to the selected one.
1665 The return value is zero if WINDOW cannot be enlarged.
1667 Optional argument HORIZONTAL non-nil means return maximum number
1668 of columns by which WINDOW can be enlarged.
1670 The optional argument IGNORE has the same meaning as for
1671 `window-resizable'. Optional argument TRAIL restricts the
1672 windows that can be enlarged. If its value is `before', only
1673 windows to the left of or above WINDOW can be enlarged. If it is
1674 `after', only windows to the right of or below WINDOW can be
1677 Optional argument NOUP non-nil means don't go up in the window
1678 tree but try to obtain the entire space from windows within
1679 WINDOW's combination. Optional argument NODOWN non-nil means do
1680 not check whether WINDOW itself (and its child windows) can be
1681 enlarged; check only whether other windows can be shrunk
1684 Optional argument PIXELWISE non-nil means return number of
1685 pixels by which WINDOW can be enlarged."
1686 (setq window
(window-normalize-window window
))
1687 (if (and (not nodown
) (window-size-fixed-p window horizontal ignore
))
1688 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1691 ;; WINDOW has no fixed size.
1692 (window--max-delta-1 window
0 horizontal ignore trail noup pixelwise
)))
1694 ;; Make NOUP also inhibit the min-size check.
1695 (defun window--resizable (window delta
&optional horizontal ignore trail noup nodown pixelwise
)
1696 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1697 WINDOW must be a valid window and defaults to the selected one.
1698 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1699 can be resized horizontally by DELTA columns. A return value of
1700 zero means that WINDOW is not resizable.
1702 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1703 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1704 return the maximum value in the range 0..DELTA by which WINDOW
1707 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1708 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1709 return the minimum value in the range DELTA..0 that can be used
1710 for shrinking WINDOW.
1712 The optional argument IGNORE has the same meaning as for
1713 `window-resizable'. Optional argument TRAIL `before' means only
1714 windows to the left of or below WINDOW can be shrunk. Optional
1715 argument TRAIL `after' means only windows to the right of or
1716 above WINDOW can be shrunk.
1718 Optional argument NOUP non-nil means don't go up in the window
1719 tree but check only whether space can be obtained from (or given
1720 to) WINDOW's siblings. Optional argument NODOWN non-nil means
1721 don't go down in the window tree. This means do not check
1722 whether resizing would violate size restrictions of WINDOW or its
1725 Optional argument PIXELWISE non-nil means interpret DELTA as
1727 (setq window
(window-normalize-window window
))
1730 (max (- (window-min-delta
1731 window horizontal ignore trail noup nodown pixelwise
))
1734 (min (window-max-delta
1735 window horizontal ignore trail noup nodown pixelwise
)
1739 (defun window--resizable-p (window delta
&optional horizontal ignore trail noup nodown pixelwise
)
1740 "Return t if WINDOW can be resized vertically by DELTA lines.
1741 WINDOW must be a valid window and defaults to the selected one.
1742 For the meaning of the arguments of this function see the
1743 doc-string of `window--resizable'.
1745 Optional argument PIXELWISE non-nil means interpret DELTA as
1747 (setq window
(window-normalize-window window
))
1749 (>= (window--resizable
1750 window delta horizontal ignore trail noup nodown pixelwise
)
1752 (<= (window--resizable
1753 window delta horizontal ignore trail noup nodown pixelwise
)
1756 (defun window-resizable (window delta
&optional horizontal ignore pixelwise
)
1757 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1758 WINDOW must be a valid window and defaults to the selected one.
1759 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1760 can be resized horizontally by DELTA columns. A return value of
1761 zero means that WINDOW is not resizable.
1763 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1764 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1765 return the maximum value in the range 0..DELTA by which WINDOW
1768 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1769 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1770 return the minimum value in the range DELTA..0 that can be used
1771 for shrinking WINDOW.
1773 Optional argument IGNORE, if non-nil, means to ignore restraints
1774 induced by fixed size windows or the values of the variables
1775 `window-min-height' and `window-min-width'. The following values
1776 have special meanings: `safe' means that in addition live windows
1777 are allowed to get as small as `window-safe-min-height' lines and
1778 `window-safe-min-width' columns. `preserved' means to ignore
1779 only restrictions induced by `window-preserve-size'. If IGNORE
1780 is a window, then ignore restrictions for that window only.
1782 Optional argument PIXELWISE non-nil means interpret DELTA as
1784 (setq window
(window-normalize-window window
))
1785 (window--resizable window delta horizontal ignore nil nil nil pixelwise
))
1787 (defun window-resizable-p (window delta
&optional horizontal ignore pixelwise
)
1788 "Return t if WINDOW can be resized vertically by DELTA lines.
1789 WINDOW must be a valid window and defaults to the selected one.
1790 For the meaning of the arguments of this function see the
1791 doc-string of `window-resizable'."
1792 (setq window
(window-normalize-window window
))
1794 (>= (window--resizable
1795 window delta horizontal ignore nil nil nil pixelwise
)
1797 (<= (window--resizable
1798 window delta horizontal ignore nil nil nil pixelwise
)
1801 ;; Aliases of functions defined in window.c.
1802 (defalias 'window-height
'window-total-height
)
1803 (defalias 'window-width
'window-body-width
)
1805 (defun window-full-height-p (&optional window
)
1806 "Return t if WINDOW is as high as its containing frame.
1807 More precisely, return t if and only if the total height of
1808 WINDOW equals the total height of the root window of WINDOW's
1809 frame. WINDOW must be a valid window and defaults to the
1811 (setq window
(window-normalize-window window
))
1812 (if (window-minibuffer-p window
)
1813 (eq window
(frame-root-window (window-frame window
)))
1814 (= (window-pixel-height window
)
1815 (window-pixel-height (frame-root-window window
)))))
1817 (defun window-full-width-p (&optional window
)
1818 "Return t if WINDOW is as wide as its containing frame.
1819 More precisely, return t if and only if the total width of WINDOW
1820 equals the total width of the root window of WINDOW's frame.
1821 WINDOW must be a valid window and defaults to the selected one."
1822 (setq window
(window-normalize-window window
))
1823 (= (window-pixel-width window
)
1824 (window-pixel-width (frame-root-window window
))))
1826 (defun window-body-size (&optional window horizontal pixelwise
)
1827 "Return the height or width of WINDOW's text area.
1828 WINDOW must be a live window and defaults to the selected one.
1830 If HORIZONTAL is omitted or nil, return the height of the text
1831 area, like `window-body-height'. Otherwise, return the width of
1832 the text area, like `window-body-width'. In either case, the
1833 optional argument PIXELWISE is passed to the functions."
1835 (window-body-width window pixelwise
)
1836 (window-body-height window pixelwise
)))
1838 (defun window-font-width (&optional window face
)
1839 "Return average character width for the font of FACE used in WINDOW.
1840 WINDOW must be a live window and defaults to the selected one.
1842 If FACE is nil or omitted, the default face is used. If FACE is
1843 remapped (see `face-remapping-alist'), the function returns the
1844 information for the remapped face."
1845 (with-selected-window (window-normalize-window window t
)
1846 (if (display-multi-font-p)
1847 (let* ((face (if face face
'default
))
1848 (info (font-info (face-font face
)))
1849 (width (aref info
11)))
1853 (frame-char-width))))
1855 (defun window-font-height (&optional window face
)
1856 "Return character height for the font of FACE used in WINDOW.
1857 WINDOW must be a live window and defaults to the selected one.
1859 If FACE is nil or omitted, the default face is used. If FACE is
1860 remapped (see `face-remapping-alist'), the function returns the
1861 information for the remapped face."
1862 (with-selected-window (window-normalize-window window t
)
1863 (if (display-multi-font-p)
1864 (let* ((face (if face face
'default
))
1865 (info (font-info (face-font face
))))
1867 (frame-char-height))))
1869 (defun window-max-chars-per-line (&optional window face
)
1870 "Return the number of characters that can be displayed on one line in WINDOW.
1871 WINDOW must be a live window and defaults to the selected one.
1873 The character width of FACE is used for the calculation. If FACE
1874 is nil or omitted, the default face is used. If FACE is
1875 remapped (see `face-remapping-alist'), the function uses the
1878 This function is different from `window-body-width' in two
1879 ways. First, it accounts for the portions of the line reserved
1880 for the continuation glyph. Second, it accounts for the size of
1882 (with-selected-window (window-normalize-window window t
)
1883 (let* ((window-width (window-body-width window t
))
1884 (font-width (window-font-width window face
))
1885 (ncols (/ window-width font-width
)))
1886 (if (and (display-graphic-p)
1887 overflow-newline-into-fringe
1888 (/= (frame-parameter nil
'left-fringe
) 0)
1889 (/= (frame-parameter nil
'right-fringe
) 0))
1893 (defun window-current-scroll-bars (&optional window
)
1894 "Return the current scroll bar types for WINDOW.
1895 WINDOW must be a live window and defaults to the selected one.
1897 The return value is a cons cell (VERTICAL . HORIZONTAL) where
1898 VERTICAL specifies the current location of the vertical scroll
1899 bar (`left', `right' or nil), and HORIZONTAL specifies the
1900 current location of the horizontal scroll bar (`bottom' or nil).
1902 Unlike `window-scroll-bars', this function reports the scroll bar
1903 type actually used, once frame defaults and `scroll-bar-mode' are
1904 taken into account."
1905 (setq window
(window-normalize-window window t
))
1906 (let ((vertical (nth 2 (window-scroll-bars window
)))
1907 (horizontal (nth 5 (window-scroll-bars window
)))
1908 (inherited (frame-current-scroll-bars (window-frame window
))))
1909 (when (eq vertical t
)
1910 (setq vertical
(car inherited
)))
1911 (when (eq horizontal t
)
1912 (setq horizontal
(cdr inherited
)))
1913 (cons vertical
(and horizontal
'bottom
))))
1915 (defun walk-windows (fun &optional minibuf all-frames
)
1916 "Cycle through all live windows, calling FUN for each one.
1917 FUN must specify a function with a window as its sole argument.
1918 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1919 windows to include in the walk.
1921 MINIBUF t means include the minibuffer window even if the
1922 minibuffer is not active. MINIBUF nil or omitted means include
1923 the minibuffer window only if the minibuffer is active. Any
1924 other value means do not include the minibuffer window even if
1925 the minibuffer is active.
1927 ALL-FRAMES nil or omitted means consider all windows on the
1928 selected frame, plus the minibuffer window if specified by the
1929 MINIBUF argument. If the minibuffer counts, consider all windows
1930 on all frames that share that minibuffer too. The following
1931 non-nil values of ALL-FRAMES have special meanings:
1933 - t means consider all windows on all existing frames.
1935 - `visible' means consider all windows on all visible frames on
1936 the current terminal.
1938 - 0 (the number zero) means consider all windows on all visible
1939 and iconified frames on the current terminal.
1941 - A frame means consider all windows on that frame only.
1943 Anything else means consider all windows on the selected frame
1946 This function changes neither the order of recently selected
1947 windows nor the buffer list."
1948 ;; If we start from the minibuffer window, don't fail to come
1950 (when (window-minibuffer-p)
1952 ;; Make sure to not mess up the order of recently selected
1953 ;; windows. Use `save-selected-window' and `select-window'
1954 ;; with second argument non-nil for this purpose.
1955 (save-selected-window
1956 (when (framep all-frames
)
1957 (select-window (frame-first-window all-frames
) 'norecord
))
1958 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames
))
1959 (funcall fun walk-windows-window
))))
1961 (defun window-at-side-p (&optional window side
)
1962 "Return t if WINDOW is at SIDE of its containing frame.
1963 WINDOW must be a valid window and defaults to the selected one.
1964 SIDE can be any of the symbols `left', `top', `right' or
1965 `bottom'. The default value nil is handled like `bottom'."
1966 (setq window
(window-normalize-window window
))
1971 ((eq side
'right
) 2)
1972 ((memq side
'(bottom nil
)) 3))))
1973 (= (nth edge
(window-pixel-edges window
))
1974 (nth edge
(window-pixel-edges (frame-root-window window
))))))
1976 (defun window-at-side-list (&optional frame side
)
1977 "Return list of all windows on SIDE of FRAME.
1978 FRAME must be a live frame and defaults to the selected frame.
1979 SIDE can be any of the symbols `left', `top', `right' or
1980 `bottom'. The default value nil is handled like `bottom'."
1981 (setq frame
(window-normalize-frame frame
))
1985 (when (window-at-side-p window side
)
1986 (setq windows
(cons window windows
))))
1988 (nreverse windows
)))
1990 (defun window--in-direction-2 (window posn
&optional horizontal
)
1991 "Support function for `window-in-direction'."
1993 (let ((top (window-pixel-top window
)))
1996 (- posn top
(window-pixel-height window
))))
1997 (let ((left (window-pixel-left window
)))
2000 (- posn left
(window-pixel-width window
))))))
2002 ;; Predecessors to the below have been devised by Julian Assange in
2003 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
2004 ;; Neither of these allow to selectively ignore specific windows
2005 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
2007 (defun window-in-direction (direction &optional window ignore sign wrap mini
)
2008 "Return window in DIRECTION as seen from WINDOW.
2009 More precisely, return the nearest window in direction DIRECTION
2010 as seen from the position of `window-point' in window WINDOW.
2011 DIRECTION must be one of `above', `below', `left' or `right'.
2012 WINDOW must be a live window and defaults to the selected one.
2014 Do not return a window whose `no-other-window' parameter is
2015 non-nil. If the nearest window's `no-other-window' parameter is
2016 non-nil, try to find another window in the indicated direction.
2017 If, however, the optional argument IGNORE is non-nil, return that
2018 window even if its `no-other-window' parameter is non-nil.
2020 Optional argument SIGN a negative number means to use the right
2021 or bottom edge of WINDOW as reference position instead of
2022 `window-point'. SIGN a positive number means to use the left or
2023 top edge of WINDOW as reference position.
2025 Optional argument WRAP non-nil means to wrap DIRECTION around
2026 frame borders. This means to return for WINDOW at the top of the
2027 frame and DIRECTION `above' the minibuffer window if the frame
2028 has one, and a window at the bottom of the frame otherwise.
2030 Optional argument MINI nil means to return the minibuffer window
2031 if and only if it is currently active. MINI non-nil means to
2032 return the minibuffer window even when it's not active. However,
2033 if WRAP non-nil, always act as if MINI were nil.
2035 Return nil if no suitable window can be found."
2036 (setq window
(window-normalize-window window t
))
2037 (unless (memq direction
'(above below left right
))
2038 (error "Wrong direction %s" direction
))
2039 (let* ((frame (window-frame window
))
2040 (hor (memq direction
'(left right
)))
2042 (window-pixel-left window
)
2043 (window-pixel-top window
)))
2044 (last (+ first
(window-size window hor t
)))
2045 ;; The column / row value of `posn-at-point' can be nil for the
2046 ;; mini-window, guard against that.
2049 ((and (numberp sign
) (< sign
0))
2051 (1- (+ (window-pixel-top window
) (window-pixel-height window
)))
2052 (1- (+ (window-pixel-left window
) (window-pixel-width window
)))))
2053 ((and (numberp sign
) (> sign
0))
2055 (window-pixel-top window
)
2056 (window-pixel-left window
)))
2057 ((let ((posn-cons (nth 2 (posn-at-point (window-point window
) window
))))
2059 (+ (or (cdr posn-cons
) 1) (window-pixel-top window
))
2060 (+ (or (car posn-cons
) 1) (window-pixel-left window
)))))))
2063 ((eq direction
'below
) (frame-pixel-height frame
))
2064 ((eq direction
'right
) (frame-pixel-width frame
))
2066 (best-edge-2 best-edge
)
2067 (best-diff-2 (if hor
(frame-pixel-height frame
) (frame-pixel-width frame
)))
2068 best best-2 best-diff-2-new
)
2071 (let* ((w-top (window-pixel-top w
))
2072 (w-left (window-pixel-left w
)))
2075 ;; Ignore ourselves.
2076 (and (window-parameter w
'no-other-window
)
2077 ;; Ignore W unless IGNORE is non-nil.
2081 ((and (<= w-top posn
)
2082 (< posn
(+ w-top
(window-pixel-height w
))))
2083 ;; W is to the left or right of WINDOW and covers POSN.
2084 (when (or (and (eq direction
'left
)
2085 (or (and (<= w-left first
) (> w-left best-edge
))
2087 (window-at-side-p window
'left
)
2088 (window-at-side-p w
'right
))))
2089 (and (eq direction
'right
)
2090 (or (and (>= w-left last
) (< w-left best-edge
))
2092 (window-at-side-p window
'right
)
2093 (window-at-side-p w
'left
)))))
2094 (setq best-edge w-left
)
2096 ((and (or (and (eq direction
'left
)
2097 (<= (+ w-left
(window-pixel-width w
)) first
))
2098 (and (eq direction
'right
) (<= last w-left
)))
2099 ;; W is to the left or right of WINDOW but does not
2101 (setq best-diff-2-new
2102 (window--in-direction-2 w posn hor
))
2103 (or (< best-diff-2-new best-diff-2
)
2104 (and (= best-diff-2-new best-diff-2
)
2105 (if (eq direction
'left
)
2106 (> w-left best-edge-2
)
2107 (< w-left best-edge-2
)))))
2108 (setq best-edge-2 w-left
)
2109 (setq best-diff-2 best-diff-2-new
)
2111 ((and (<= w-left posn
)
2112 (< posn
(+ w-left
(window-pixel-width w
))))
2113 ;; W is above or below WINDOW and covers POSN.
2114 (when (or (and (eq direction
'above
)
2115 (or (and (<= w-top first
) (> w-top best-edge
))
2117 (window-at-side-p window
'top
)
2118 (if (active-minibuffer-window)
2119 (minibuffer-window-active-p w
)
2120 (window-at-side-p w
'bottom
)))))
2121 (and (eq direction
'below
)
2122 (or (and (>= w-top first
) (< w-top best-edge
))
2124 (if (active-minibuffer-window)
2125 (minibuffer-window-active-p window
)
2126 (window-at-side-p window
'bottom
))
2127 (window-at-side-p w
'top
)))))
2128 (setq best-edge w-top
)
2130 ((and (or (and (eq direction
'above
)
2131 (<= (+ w-top
(window-pixel-height w
)) first
))
2132 (and (eq direction
'below
) (<= last w-top
)))
2133 ;; W is above or below WINDOW but does not cover POSN.
2134 (setq best-diff-2-new
2135 (window--in-direction-2 w posn hor
))
2136 (or (< best-diff-2-new best-diff-2
)
2137 (and (= best-diff-2-new best-diff-2
)
2138 (if (eq direction
'above
)
2139 (> w-top best-edge-2
)
2140 (< w-top best-edge-2
)))))
2141 (setq best-edge-2 w-top
)
2142 (setq best-diff-2 best-diff-2-new
)
2144 frame nil
(and mini t
))
2147 (defun get-window-with-predicate (predicate &optional minibuf all-frames default
)
2148 "Return a live window satisfying PREDICATE.
2149 More precisely, cycle through all windows calling the function
2150 PREDICATE on each one of them with the window as its sole
2151 argument. Return the first window for which PREDICATE returns
2152 non-nil. Windows are scanned starting with the window following
2153 the selected window. If no window satisfies PREDICATE, return
2156 MINIBUF t means include the minibuffer window even if the
2157 minibuffer is not active. MINIBUF nil or omitted means include
2158 the minibuffer window only if the minibuffer is active. Any
2159 other value means do not include the minibuffer window even if
2160 the minibuffer is active.
2162 ALL-FRAMES nil or omitted means consider all windows on the selected
2163 frame, plus the minibuffer window if specified by the MINIBUF
2164 argument. If the minibuffer counts, consider all windows on all
2165 frames that share that minibuffer too. The following non-nil
2166 values of ALL-FRAMES have special meanings:
2168 - t means consider all windows on all existing frames.
2170 - `visible' means consider all windows on all visible frames on
2171 the current terminal.
2173 - 0 (the number zero) means consider all windows on all visible
2174 and iconified frames on the current terminal.
2176 - A frame means consider all windows on that frame only.
2178 Anything else means consider all windows on the selected frame
2181 (dolist (window (window-list-1
2182 (next-window nil minibuf all-frames
)
2183 minibuf all-frames
))
2184 (when (funcall predicate window
)
2185 (throw 'found window
)))
2188 (defalias 'some-window
'get-window-with-predicate
)
2190 (defun get-lru-window (&optional all-frames dedicated not-selected
)
2191 "Return the least recently used window on frames specified by ALL-FRAMES.
2192 Return a full-width window if possible. A minibuffer window is
2193 never a candidate. A dedicated window is never a candidate
2194 unless DEDICATED is non-nil, so if all windows are dedicated, the
2195 value is nil. Avoid returning the selected window if possible.
2196 Optional argument NOT-SELECTED non-nil means never return the
2199 The following non-nil values of the optional argument ALL-FRAMES
2200 have special meanings:
2202 - t means consider all windows on all existing frames.
2204 - `visible' means consider all windows on all visible frames on
2205 the current terminal.
2207 - 0 (the number zero) means consider all windows on all visible
2208 and iconified frames on the current terminal.
2210 - A frame means consider all windows on that frame only.
2212 Any other value of ALL-FRAMES means consider all windows on the
2213 selected frame and no others."
2214 (let (best-window best-time second-best-window second-best-time time
)
2215 (dolist (window (window-list-1 nil
'nomini all-frames
))
2216 (when (and (or dedicated
(not (window-dedicated-p window
)))
2217 (or (not not-selected
) (not (eq window
(selected-window)))))
2218 (setq time
(window-use-time window
))
2219 (if (or (eq window
(selected-window))
2220 (not (window-full-width-p window
)))
2221 (when (or (not second-best-time
) (< time second-best-time
))
2222 (setq second-best-time time
)
2223 (setq second-best-window window
))
2224 (when (or (not best-time
) (< time best-time
))
2225 (setq best-time time
)
2226 (setq best-window window
)))))
2227 (or best-window second-best-window
)))
2229 (defun get-mru-window (&optional all-frames dedicated not-selected
)
2230 "Return the most recently used window on frames specified by ALL-FRAMES.
2231 A minibuffer window is never a candidate. A dedicated window is
2232 never a candidate unless DEDICATED is non-nil, so if all windows
2233 are dedicated, the value is nil. Optional argument NOT-SELECTED
2234 non-nil means never return the selected window.
2236 The following non-nil values of the optional argument ALL-FRAMES
2237 have special meanings:
2239 - t means consider all windows on all existing frames.
2241 - `visible' means consider all windows on all visible frames on
2242 the current terminal.
2244 - 0 (the number zero) means consider all windows on all visible
2245 and iconified frames on the current terminal.
2247 - A frame means consider all windows on that frame only.
2249 Any other value of ALL-FRAMES means consider all windows on the
2250 selected frame and no others."
2251 (let (best-window best-time time
)
2252 (dolist (window (window-list-1 nil
'nomini all-frames
))
2253 (setq time
(window-use-time window
))
2254 (when (and (or dedicated
(not (window-dedicated-p window
)))
2255 (or (not not-selected
) (not (eq window
(selected-window))))
2256 (or (not best-time
) (> time best-time
)))
2257 (setq best-time time
)
2258 (setq best-window window
)))
2261 (defun get-largest-window (&optional all-frames dedicated not-selected
)
2262 "Return the largest window on frames specified by ALL-FRAMES.
2263 A minibuffer window is never a candidate. A dedicated window is
2264 never a candidate unless DEDICATED is non-nil, so if all windows
2265 are dedicated, the value is nil. Optional argument NOT-SELECTED
2266 non-nil means never return the selected window.
2268 The following non-nil values of the optional argument ALL-FRAMES
2269 have special meanings:
2271 - t means consider all windows on all existing frames.
2273 - `visible' means consider all windows on all visible frames on
2274 the current terminal.
2276 - 0 (the number zero) means consider all windows on all visible
2277 and iconified frames on the current terminal.
2279 - A frame means consider all windows on that frame only.
2281 Any other value of ALL-FRAMES means consider all windows on the
2282 selected frame and no others."
2285 (dolist (window (window-list-1 nil
'nomini all-frames
))
2286 (when (and (or dedicated
(not (window-dedicated-p window
)))
2287 (or (not not-selected
) (not (eq window
(selected-window)))))
2288 (setq size
(* (window-pixel-height window
)
2289 (window-pixel-width window
)))
2290 (when (> size best-size
)
2291 (setq best-size size
)
2292 (setq best-window window
))))
2295 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames
)
2296 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2297 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2298 and defaults to the current buffer. Windows are scanned starting
2299 with the selected window.
2301 MINIBUF t means include the minibuffer window even if the
2302 minibuffer is not active. MINIBUF nil or omitted means include
2303 the minibuffer window only if the minibuffer is active. Any
2304 other value means do not include the minibuffer window even if
2305 the minibuffer is active.
2307 ALL-FRAMES nil or omitted means consider all windows on the
2308 selected frame, plus the minibuffer window if specified by the
2309 MINIBUF argument. If the minibuffer counts, consider all windows
2310 on all frames that share that minibuffer too. The following
2311 non-nil values of ALL-FRAMES have special meanings:
2313 - t means consider all windows on all existing frames.
2315 - `visible' means consider all windows on all visible frames on
2316 the current terminal.
2318 - 0 (the number zero) means consider all windows on all visible
2319 and iconified frames on the current terminal.
2321 - A frame means consider all windows on that frame only.
2323 Anything else means consider all windows on the selected frame
2325 (let ((buffer (window-normalize-buffer buffer-or-name
))
2327 (dolist (window (window-list-1 (selected-window) minibuf all-frames
))
2328 (when (eq (window-buffer window
) buffer
)
2329 (setq windows
(cons window windows
))))
2330 (nreverse windows
)))
2332 (defun minibuffer-window-active-p (window)
2333 "Return t if WINDOW is the currently active minibuffer window."
2334 (eq window
(active-minibuffer-window)))
2336 (defun count-windows (&optional minibuf
)
2337 "Return the number of live windows on the selected frame.
2338 The optional argument MINIBUF specifies whether the minibuffer
2339 window shall be counted. See `walk-windows' for the precise
2340 meaning of this argument."
2341 (length (window-list-1 nil minibuf
)))
2343 ;;; Resizing windows.
2344 (defun window--size-to-pixel (window size
&optional horizontal pixelwise round-maybe
)
2345 "For WINDOW convert SIZE lines to pixels.
2346 SIZE is supposed to specify a height of WINDOW in terms of text
2347 lines. The return value is the number of pixels specifying that
2350 WINDOW must be a valid window. Optional argument HORIZONTAL
2351 non-nil means convert SIZE columns to pixels.
2353 Optional argument PIXELWISE non-nil means SIZE already specifies
2354 pixels but may have to be adjusted to a multiple of the character
2355 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2356 means round to the nearest multiple of the character size of
2357 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2358 (setq window
(window-normalize-window window
))
2359 (let ((char-size (frame-char-size window horizontal
)))
2361 (if (and round-maybe
(not window-resize-pixelwise
))
2362 (* (round size char-size
) char-size
)
2364 (* size char-size
))))
2366 (defun window--pixel-to-total-1 (window horizontal char-size
)
2367 "Subroutine of `window--pixel-to-total'."
2368 (let ((child (window-child window
)))
2369 (if (window-combination-p window horizontal
)
2370 ;; In an iso-combination distribute sizes proportionally.
2371 (let ((remainder (window-new-total window
))
2372 size best-child rem best-rem
)
2373 ;; Initialize total sizes to each child's floor.
2375 (setq size
(max (/ (window-size child horizontal t
) char-size
) 1))
2376 (set-window-new-total child size
)
2377 (setq remainder
(- remainder size
))
2378 (setq child
(window-next-sibling child
)))
2379 ;; Distribute remainder.
2380 (while (> remainder
0)
2381 (setq child
(window-last-child window
))
2382 (setq best-child nil
)
2385 (when (and (<= (window-new-total child
)
2386 (/ (window-size child horizontal t
) char-size
))
2387 (> (setq rem
(%
(window-size child horizontal t
)
2390 (setq best-child child
)
2391 (setq best-rem rem
))
2392 (setq child
(window-prev-sibling child
)))
2393 ;; We MUST have a best-child here.
2394 (set-window-new-total best-child
1 t
)
2395 (setq remainder
(1- remainder
)))
2397 (setq child
(window-child window
))
2399 (window--pixel-to-total-1 child horizontal char-size
)
2400 (setq child
(window-next-sibling child
))))
2401 ;; In an ortho-combination assign new sizes directly.
2402 (let ((size (window-new-total window
)))
2404 (set-window-new-total child size
)
2405 (window--pixel-to-total-1 child horizontal char-size
)
2406 (setq child
(window-next-sibling child
)))))))
2408 (defun window--pixel-to-total (&optional frame horizontal
)
2409 "On FRAME assign new total window heights from pixel heights.
2410 FRAME must be a live frame and defaults to the selected frame.
2412 Optional argument HORIZONTAL non-nil means assign new total
2413 window widths from pixel widths."
2414 (setq frame
(window-normalize-frame frame
))
2415 (let* ((char-size (frame-char-size frame horizontal
))
2416 (root (frame-root-window frame
))
2417 (root-size (window-size root horizontal t
))
2418 ;; We have to care about the minibuffer window only if it
2419 ;; appears together with the root window on this frame.
2420 (mini (let ((mini (minibuffer-window frame
)))
2421 (and (eq (window-frame mini
) frame
)
2422 (not (eq mini root
)) mini
)))
2423 (mini-size (and mini
(window-size mini horizontal t
))))
2424 ;; We round the line/column sizes of windows here to the nearest
2425 ;; integer. In some cases this can make windows appear _larger_
2426 ;; than the containing frame (line/column-wise) because the latter's
2427 ;; sizes are not (yet) rounded. We might eventually fix that.
2428 (if (and mini
(not horizontal
))
2430 (set-window-new-total root
(max (/ root-size char-size
) 1))
2431 (set-window-new-total mini
(max (/ mini-size char-size
) 1))
2432 (setq lines
(- (round (+ root-size mini-size
) char-size
)
2433 (+ (window-new-total root
) (window-new-total mini
))))
2435 (if (>= (% root-size
(window-new-total root
))
2436 (% mini-size
(window-new-total mini
)))
2437 (set-window-new-total root
1 t
)
2438 (set-window-new-total mini
1 t
))
2439 (setq lines
(1- lines
))))
2440 (set-window-new-total root
(round root-size char-size
))
2442 ;; This is taken in the horizontal case only.
2443 (set-window-new-total mini
(round mini-size char-size
))))
2444 (unless (window-buffer root
)
2445 (window--pixel-to-total-1 root horizontal char-size
))
2446 ;; Apply the new sizes.
2447 (window-resize-apply-total frame horizontal
)))
2449 (defun window--resize-reset (&optional frame horizontal
)
2450 "Reset resize values for all windows on FRAME.
2451 FRAME defaults to the selected frame.
2453 This function stores the current value of `window-size' applied
2454 with argument HORIZONTAL in the new total size of all windows on
2455 FRAME. It also resets the new normal size of each of these
2457 (window--resize-reset-1
2458 (frame-root-window (window-normalize-frame frame
)) horizontal
))
2460 (defun window--resize-reset-1 (window horizontal
)
2461 "Internal function of `window--resize-reset'."
2462 ;; Register old size in the new total size.
2463 (set-window-new-pixel window
(window-size window horizontal t
))
2464 (set-window-new-total window
(window-size window horizontal
))
2465 ;; Reset new normal size.
2466 (set-window-new-normal window
)
2467 (when (window-child window
)
2468 (window--resize-reset-1 (window-child window
) horizontal
))
2469 (when (window-right window
)
2470 (window--resize-reset-1 (window-right window
) horizontal
)))
2472 ;; The following routine is used to manually resize the minibuffer
2473 ;; window and is currently used, for example, by ispell.el.
2474 (defun window--resize-mini-window (window delta
)
2475 "Resize minibuffer window WINDOW by DELTA pixels.
2476 If WINDOW cannot be resized by DELTA pixels make it as large (or
2477 as small) as possible, but don't signal an error."
2478 (when (window-minibuffer-p window
)
2479 (let* ((frame (window-frame window
))
2480 (root (frame-root-window frame
))
2481 (height (window-pixel-height window
))
2483 (- (window-pixel-height root
)
2484 (window-min-size root nil nil t
))))
2487 ((<= (+ height delta
) 0)
2488 (setq delta
(- (frame-char-height (window-frame window
)) height
)))
2489 ((> delta min-delta
)
2490 (setq delta min-delta
)))
2492 (unless (zerop delta
)
2494 (window--resize-reset frame
)
2495 ;; Ideally we should be able to resize just the last child of root
2496 ;; here. See the comment in `resize-root-window-vertically' for
2497 ;; why we do not do that.
2498 (window--resize-this-window root
(- delta
) nil nil t
)
2499 (set-window-new-pixel window
(+ height delta
))
2500 ;; The following routine catches the case where we want to resize
2501 ;; a minibuffer-only frame.
2502 (when (resize-mini-window-internal window
)
2503 (window--pixel-to-total frame
)
2504 (run-window-configuration-change-hook frame
))))))
2506 (defun window--resize-apply-p (frame &optional horizontal
)
2507 "Return t when a window on FRAME shall be resized vertically.
2508 Optional argument HORIZONTAL non-nil means return t when a window
2509 shall be resized horizontally."
2513 (unless (= (window-new-pixel window
)
2514 (window-size window horizontal t
))
2519 (defun window-resize (window delta
&optional horizontal ignore pixelwise
)
2520 "Resize WINDOW vertically by DELTA lines.
2521 WINDOW can be an arbitrary window and defaults to the selected
2522 one. An attempt to resize the root window of a frame will raise
2525 DELTA a positive number means WINDOW shall be enlarged by DELTA
2526 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2529 Optional argument HORIZONTAL non-nil means resize WINDOW
2530 horizontally by DELTA columns. In this case a positive DELTA
2531 means enlarge WINDOW by DELTA columns. DELTA negative means
2532 WINDOW shall be shrunk by -DELTA columns.
2534 Optional argument IGNORE, if non-nil, means to ignore restraints
2535 induced by fixed size windows or the values of the variables
2536 `window-min-height' and `window-min-width'. The following values
2537 have special meanings: `safe' means that in addition live windows
2538 are allowed to get as small as `window-safe-min-height' lines and
2539 `window-safe-min-width' columns. `preserved' means to ignore
2540 only restrictions induced by `window-preserve-size'. If IGNORE
2541 is a window, then ignore restrictions for that window only.
2543 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2546 This function resizes other windows proportionally and never
2547 deletes any windows. If you want to move only the low (right)
2548 edge of WINDOW consider using `adjust-window-trailing-edge'
2550 (setq window
(window-normalize-window window
))
2551 (let* ((frame (window-frame window
))
2552 (minibuffer-window (minibuffer-window frame
))
2554 (setq delta
(window--size-to-pixel
2555 window delta horizontal pixelwise t
))
2557 ((eq window
(frame-root-window frame
))
2558 (error "Cannot resize the root window of a frame"))
2559 ((window-minibuffer-p window
)
2561 (error "Cannot resize minibuffer window horizontally")
2562 (window--resize-mini-window window delta
)))
2563 ((and (not horizontal
)
2564 (window-full-height-p window
)
2565 (eq (window-frame minibuffer-window
) frame
)
2566 (or (not resize-mini-windows
)
2567 (eq minibuffer-window
(active-minibuffer-window))))
2568 ;; If WINDOW is full height and either `resize-mini-windows' is
2569 ;; nil or the minibuffer window is active, resize the minibuffer
2571 (window--resize-mini-window minibuffer-window
(- delta
)))
2572 ((or (window--resizable-p
2573 window delta horizontal ignore nil nil nil t
)
2575 (setq ignore
'preserved
)
2576 (window--resizable-p
2577 window delta horizontal ignore nil nil nil t
)))
2578 (window--resize-reset frame horizontal
)
2579 (window--resize-this-window window delta horizontal ignore t
)
2580 (if (and (not window-combination-resize
)
2581 (window-combined-p window horizontal
)
2582 (setq sibling
(or (window-right window
) (window-left window
)))
2584 sibling
(- delta
) horizontal ignore t
))
2585 ;; If window-combination-resize is nil, WINDOW is part of an
2586 ;; iso-combination, and WINDOW's neighboring right or left
2587 ;; sibling can be resized as requested, resize that sibling.
2590 (window-size (window-parent window
) horizontal t
))))
2591 (window--resize-this-window sibling
(- delta
) horizontal nil t
)
2592 (set-window-new-normal
2593 window
(+ (window-normal-size window horizontal
)
2595 (set-window-new-normal
2596 sibling
(- (window-normal-size sibling horizontal
)
2598 ;; Otherwise, resize all other windows in the same combination.
2599 (window--resize-siblings window delta horizontal ignore
))
2600 (when (window--resize-apply-p frame horizontal
)
2601 (if (window-resize-apply frame horizontal
)
2603 (window--pixel-to-total frame horizontal
)
2604 (run-window-configuration-change-hook frame
))
2605 (error "Failed to apply resizing %s" window
))))
2607 (error "Cannot resize window %s" window
)))))
2609 (defun window-resize-no-error (window delta
&optional horizontal ignore pixelwise
)
2610 "Resize WINDOW vertically if it is resizable by DELTA lines.
2611 This function is like `window-resize' but does not signal an
2612 error when WINDOW cannot be resized. For the meaning of the
2613 optional arguments see the documentation of `window-resize'.
2615 Optional argument PIXELWISE non-nil means interpret DELTA as
2617 (when (window--resizable-p
2618 window delta horizontal ignore nil nil nil pixelwise
)
2619 (window-resize window delta horizontal ignore pixelwise
)))
2621 (defun window--resize-child-windows-skip-p (window)
2622 "Return non-nil if WINDOW shall be skipped by resizing routines."
2623 (memq (window-new-normal window
) '(ignore stuck skip
)))
2625 (defun window--resize-child-windows-normal (parent horizontal window this-delta
&optional trail other-delta
)
2626 "Recursively set new normal height of child windows of window PARENT.
2627 HORIZONTAL non-nil means set the new normal width of these
2628 windows. WINDOW specifies a child window of PARENT that has been
2629 resized by THIS-DELTA lines (columns).
2631 Optional argument TRAIL either `before' or `after' means set values
2632 only for windows before or after WINDOW. Optional argument
2633 OTHER-DELTA, a number, specifies that this many lines (columns)
2634 have been obtained from (or returned to) an ancestor window of
2635 PARENT in order to resize WINDOW."
2636 (let* ((delta-normal
2637 (if (and (= (- this-delta
)
2638 (window-size window horizontal t
))
2639 (zerop other-delta
))
2640 ;; When WINDOW gets deleted and we can return its entire
2641 ;; space to its siblings, use WINDOW's normal size as the
2643 (- (window-normal-size window horizontal
))
2644 ;; In any other case calculate the normal delta from the
2645 ;; relation of THIS-DELTA to the total size of PARENT.
2646 (/ (float this-delta
)
2647 (window-size parent horizontal t
))))
2648 (sub (window-child parent
))
2650 (skip (eq trail
'after
)))
2652 ;; Set parent-normal to the sum of the normal sizes of all child
2653 ;; windows of PARENT that shall be resized, excluding only WINDOW
2654 ;; and any windows specified by the optional TRAIL argument.
2658 (setq skip
(eq trail
'before
)))
2662 (+ parent-normal
(window-normal-size sub horizontal
)))))
2663 (setq sub
(window-right sub
)))
2665 ;; Set the new normal size of all child windows of PARENT from what
2666 ;; they should have contributed for recovering THIS-DELTA lines
2668 (setq sub
(window-child parent
))
2669 (setq skip
(eq trail
'after
))
2673 (setq skip
(eq trail
'before
)))
2676 (let ((old-normal (window-normal-size sub horizontal
)))
2677 (set-window-new-normal
2678 sub
(min 1.0 ; Don't get larger than 1.
2680 (* (/ old-normal parent-normal
)
2682 ;; Don't drop below 0.
2684 (setq sub
(window-right sub
)))
2686 (when (numberp other-delta
)
2687 ;; Set the new normal size of windows from what they should have
2688 ;; contributed for recovering OTHER-DELTA lines (columns).
2689 (setq delta-normal
(/ (float (window-size parent horizontal t
))
2690 (+ (window-size parent horizontal t
)
2692 (setq sub
(window-child parent
))
2693 (setq skip
(eq trail
'after
))
2697 (setq skip
(eq trail
'before
)))
2700 (set-window-new-normal
2701 sub
(min 1.0 ; Don't get larger than 1.
2702 (max (* (window-new-normal sub
) delta-normal
)
2703 ;; Don't drop below 0.
2705 (setq sub
(window-right sub
))))
2707 ;; Set the new normal size of WINDOW to what is left by the sum of
2708 ;; the normal sizes of its siblings.
2709 (set-window-new-normal
2712 (setq sub
(window-child parent
))
2716 ((not (numberp (window-new-normal sub
)))
2717 (setq sum
(+ sum
(window-normal-size sub horizontal
))))
2719 (setq sum
(+ sum
(window-new-normal sub
)))))
2720 (setq sub
(window-right sub
)))
2721 ;; Don't get larger than 1 or smaller than 0.
2722 (min 1.0 (max (- 1.0 sum
) 0.0))))))
2724 (defun window--resize-child-windows (parent delta
&optional horizontal window ignore trail edge char-size
)
2725 "Resize child windows of window PARENT vertically by DELTA pixels.
2726 PARENT must be a vertically combined internal window.
2728 Optional argument HORIZONTAL non-nil means resize child windows
2729 of PARENT horizontally by DELTA pixels. In this case PARENT must
2730 be a horizontally combined internal window.
2732 WINDOW, if specified, must denote a child window of PARENT that
2733 is resized by DELTA pixels.
2735 The optional argument IGNORE has the same meaning as for
2738 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2739 of windows that shall be resized. If TRAIL equals `before',
2740 resize only windows on the left or above EDGE. If TRAIL equals
2741 `after', resize only windows on the right or below EDGE. Also,
2742 preferably only resize windows adjacent to EDGE.
2744 If the optional argument CHAR-SIZE is a positive integer, it specifies
2745 the number of pixels by which windows are incrementally resized.
2746 If CHAR-SIZE is nil, this means to use the value of
2747 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2749 Return the symbol `normalized' if new normal sizes have been
2750 already set by this routine."
2751 (let* ((first (window-child parent
))
2752 (last (window-last-child parent
))
2753 (parent-total (+ (window-size parent horizontal t
)
2755 (char-size (or char-size
2756 (and window-resize-pixelwise
1)
2757 (frame-char-size window horizontal
)))
2758 sub best-window best-value best-delta
)
2760 (if (and edge
(memq trail
'(before after
))
2763 (while (and (window-right sub
)
2764 (or (and (eq trail
'before
)
2765 (not (window--resize-child-windows-skip-p
2766 (window-right sub
))))
2767 (and (eq trail
'after
)
2768 (window--resize-child-windows-skip-p sub
))))
2769 (setq sub
(window-right sub
)))
2772 (if (eq trail
'before
)
2773 (= (+ (window-pixel-left sub
) (window-pixel-width sub
))
2775 (= (window-pixel-left sub
) edge
))
2776 (if (eq trail
'before
)
2777 (= (+ (window-pixel-top sub
) (window-pixel-height sub
))
2779 (= (window-pixel-top sub
) edge
)))
2780 (window-sizable-p sub delta horizontal ignore t
))
2781 ;; Resize only windows adjacent to EDGE.
2783 (window--resize-this-window
2784 sub delta horizontal ignore t trail edge
)
2785 (if (and window
(eq (window-parent sub
) parent
))
2787 ;; Assign new normal sizes.
2788 (set-window-new-normal
2789 sub
(/ (float (window-new-pixel sub
)) parent-total
))
2790 (set-window-new-normal
2791 window
(- (window-normal-size window horizontal
)
2792 (- (window-new-normal sub
)
2793 (window-normal-size sub horizontal
)))))
2794 (window--resize-child-windows-normal
2795 parent horizontal sub
0 trail delta
))
2796 ;; Return 'normalized to notify `window--resize-siblings' that
2797 ;; normal sizes have been already set.
2799 ;; Resize all windows proportionally.
2803 ((or (window--resize-child-windows-skip-p sub
)
2804 ;; Ignore windows to skip and fixed-size child windows -
2805 ;; in the latter case make it a window to skip.
2807 (window-size-fixed-p sub horizontal ignore
)
2808 (set-window-new-normal sub
'ignore
))))
2810 ;; When shrinking store the number of lines/cols we can get
2811 ;; from this window here together with the total/normal size
2813 (set-window-new-normal
2816 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
2817 (window-min-delta sub horizontal ignore trail t nil t
)
2818 (- (/ (float (window-size sub horizontal t
))
2820 (window-normal-size sub horizontal
)))))
2822 ;; When enlarging store the total/normal size factor only
2823 (set-window-new-normal
2825 (- (/ (float (window-size sub horizontal t
))
2827 (window-normal-size sub horizontal
)))))
2829 (setq sub
(window-left sub
)))
2833 ;; Shrink windows by delta.
2834 (setq best-window t
)
2835 (while (and best-window
(not (zerop delta
)))
2837 (setq best-window nil
)
2838 (setq best-value most-negative-fixnum
)
2840 (when (and (consp (window-new-normal sub
))
2841 (not (<= (car (window-new-normal sub
)) 0))
2842 (> (cdr (window-new-normal sub
)) best-value
))
2843 (setq best-window sub
)
2844 (setq best-value
(cdr (window-new-normal sub
))))
2846 (setq sub
(window-left sub
)))
2849 (setq best-delta
(min (car (window-new-normal best-window
))
2850 char-size
(- delta
)))
2851 (setq delta
(+ delta best-delta
))
2852 (set-window-new-pixel best-window
(- best-delta
) t
)
2853 (set-window-new-normal
2855 (if (= (car (window-new-normal best-window
)) best-delta
)
2856 'skip
; We can't shrink best-window any further.
2857 (cons (- (car (window-new-normal best-window
)) best-delta
)
2858 (- (/ (float (window-new-pixel best-window
))
2860 (window-normal-size best-window horizontal
))))))))
2862 ;; Enlarge windows by delta.
2863 (setq best-window t
)
2864 (while (and best-window
(not (zerop delta
)))
2866 (setq best-window nil
)
2867 (setq best-value most-positive-fixnum
)
2869 (when (and (numberp (window-new-normal sub
))
2870 (< (window-new-normal sub
) best-value
))
2871 (setq best-window sub
)
2872 (setq best-value
(window-new-normal sub
)))
2874 (setq sub
(window-left sub
)))
2877 (setq best-delta
(min delta char-size
))
2878 (setq delta
(- delta best-delta
))
2879 (set-window-new-pixel best-window best-delta t
)
2880 (set-window-new-normal
2882 (- (/ (float (window-new-pixel best-window
))
2884 (window-normal-size best-window horizontal
)))))))
2889 (when (or (consp (window-new-normal sub
))
2890 (numberp (window-new-normal sub
)))
2891 ;; Reset new normal size fields so `window-resize-apply'
2892 ;; won't use them to apply new sizes.
2893 (set-window-new-normal sub
))
2895 (unless (eq (window-new-normal sub
) 'ignore
)
2896 ;; Resize this window's child windows (back-engineering
2897 ;; delta from sub's old and new total sizes).
2898 (let ((delta (- (window-new-pixel sub
)
2899 (window-size sub horizontal t
))))
2900 (unless (and (zerop delta
) (not trail
))
2901 ;; For the TRAIL non-nil case we have to resize SUB
2902 ;; recursively even if it's size does not change.
2903 (window--resize-this-window
2904 sub delta horizontal ignore nil trail edge
))))
2905 (setq sub
(window-left sub
)))))))
2907 (defun window--resize-siblings (window delta
&optional horizontal ignore trail edge char-size
)
2908 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
2909 Optional argument HORIZONTAL non-nil means resize other windows
2910 when WINDOW is resized horizontally by DELTA pixels. WINDOW
2911 itself is not resized by this function.
2913 The optional argument IGNORE has the same meaning as for
2916 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2917 of windows that shall be resized. If TRAIL equals `before',
2918 resize only windows on the left or above EDGE. If TRAIL equals
2919 `after', resize only windows on the right or below EDGE. Also,
2920 preferably only resize windows adjacent to EDGE."
2921 (when (window-parent window
)
2922 (let* ((parent (window-parent window
))
2923 (sub (window-child parent
)))
2924 (if (window-combined-p sub horizontal
)
2925 ;; In an iso-combination try to extract DELTA from WINDOW's
2927 (let ((skip (eq trail
'after
))
2928 this-delta other-delta
)
2929 ;; Decide which windows shall be left alone.
2933 ;; Make sure WINDOW is left alone when
2934 ;; resizing its siblings.
2935 (set-window-new-normal sub
'ignore
)
2936 (setq skip
(eq trail
'before
)))
2938 ;; Make sure this sibling is left alone when
2939 ;; resizing its siblings.
2940 (set-window-new-normal sub
'ignore
))
2941 ((not (window-size-fixed-p sub horizontal ignore
))
2942 ;; Set this-delta to t to signal that we found a sibling
2943 ;; of WINDOW whose size is not fixed.
2944 (setq this-delta t
)))
2946 (setq sub
(window-right sub
)))
2948 ;; Set this-delta to what we can get from WINDOW's siblings.
2949 (if (= (- delta
) (window-size window horizontal t
))
2950 ;; A deletion, presumably. We must handle this case
2951 ;; specially since `window--resizable' can't be used.
2953 ;; There's at least one resizable sibling we can
2954 ;; give WINDOW's size to.
2955 (setq this-delta delta
)
2956 ;; No resizable sibling exists.
2957 (setq this-delta
0))
2958 ;; Any other form of resizing.
2961 window delta horizontal ignore trail t nil t
)))
2963 ;; Set other-delta to what we still have to get from
2964 ;; ancestor windows of parent.
2965 (setq other-delta
(- delta this-delta
))
2966 (unless (zerop other-delta
)
2967 ;; Unless we got everything from WINDOW's siblings, PARENT
2968 ;; must be resized by other-delta lines or columns.
2969 (set-window-new-pixel parent other-delta
'add
))
2971 (if (zerop this-delta
)
2972 ;; We haven't got anything from WINDOW's siblings but we
2973 ;; must update the normal sizes to respect other-delta.
2974 (window--resize-child-windows-normal
2975 parent horizontal window this-delta trail other-delta
)
2976 ;; We did get something from WINDOW's siblings which means
2977 ;; we have to resize their child windows.
2978 (unless (eq (window--resize-child-windows
2979 parent
(- this-delta
) horizontal
2980 window ignore trail edge char-size
)
2981 ;; If `window--resize-child-windows' returns
2982 ;; 'normalized, this means it has set the
2983 ;; normal sizes already.
2985 ;; Set the normal sizes.
2986 (window--resize-child-windows-normal
2987 parent horizontal window this-delta trail other-delta
))
2988 ;; Set DELTA to what we still have to get from ancestor
2990 (setq delta other-delta
)))
2992 ;; In an ortho-combination all siblings of WINDOW must be
2993 ;; resized by DELTA.
2994 (set-window-new-pixel parent delta
'add
)
2996 (unless (eq sub window
)
2997 (window--resize-this-window
2998 sub delta horizontal ignore t
))
2999 (setq sub
(window-right sub
))))
3001 (unless (zerop delta
)
3003 (window--resize-siblings
3004 parent delta horizontal ignore trail edge char-size
)))))
3006 (defun window--resize-this-window (window delta
&optional horizontal ignore add trail edge char-size
)
3007 "Resize WINDOW vertically by DELTA pixels.
3008 Optional argument HORIZONTAL non-nil means resize WINDOW
3009 horizontally by DELTA pixels.
3011 The optional argument IGNORE has the same meaning as for
3012 `window-resizable'. Optional argument ADD non-nil means add
3013 DELTA to the new total size of WINDOW.
3015 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3016 of windows that shall be resized. If TRAIL equals `before',
3017 resize only windows on the left or above EDGE. If TRAIL equals
3018 `after', resize only windows on the right or below EDGE. Also,
3019 preferably only resize windows adjacent to EDGE.
3021 If the optional argument CHAR-SIZE is a positive integer, it specifies
3022 the number of pixels by which windows are incrementally resized.
3023 If CHAR-SIZE is nil, this means to use the value of
3024 `frame-char-height' or `frame-char-width' of WINDOW's frame.
3026 This function recursively resizes WINDOW's child windows to fit the
3027 new size. Make sure that WINDOW is `window--resizable' before
3028 calling this function. Note that this function does not resize
3029 siblings of WINDOW or WINDOW's parent window. You have to
3030 eventually call `window-resize-apply' in order to make resizing
3031 actually take effect."
3033 ;; Add DELTA to the new total size of WINDOW.
3034 (set-window-new-pixel window delta t
))
3036 (let ((sub (window-child window
)))
3039 ((window-combined-p sub horizontal
)
3040 ;; In an iso-combination resize child windows according to their
3042 (window--resize-child-windows
3043 window delta horizontal nil ignore trail edge char-size
))
3044 ;; In an ortho-combination resize each child window by DELTA.
3047 (window--resize-this-window
3048 sub delta horizontal ignore t trail edge char-size
)
3049 (setq sub
(window-right sub
)))))))
3051 (defun window--resize-root-window (window delta horizontal ignore pixelwise
)
3052 "Resize root window WINDOW vertically by DELTA lines.
3053 HORIZONTAL non-nil means resize root window WINDOW horizontally
3056 IGNORE non-nil means ignore any restrictions imposed by fixed
3057 size windows, `window-min-height' or `window-min-width' settings.
3059 This function is only called by the frame resizing routines. It
3060 resizes windows proportionally and never deletes any windows."
3061 (when (and (windowp window
) (numberp delta
))
3065 (window--size-to-pixel window delta horizontal
))))
3066 (when (window-sizable-p window pixel-delta horizontal ignore t
)
3067 (window--resize-reset (window-frame window
) horizontal
)
3068 (window--resize-this-window
3069 window pixel-delta horizontal ignore t
)))))
3071 (defun window--resize-root-window-vertically (window delta pixelwise
)
3072 "Resize root window WINDOW vertically by DELTA lines.
3073 If DELTA is less than zero and we can't shrink WINDOW by DELTA
3074 lines, shrink it as much as possible. If DELTA is greater than
3075 zero, this function can resize fixed-size windows in order to
3076 recover the necessary lines. Return the number of lines that
3079 Third argument PIXELWISE non-nil means to interpret DELTA as
3080 pixels and return the number of pixels that were recovered.
3082 This function is called by the minibuffer window resizing
3084 (let* ((frame (window-frame window
))
3090 (* (frame-char-height frame
) delta
))
3094 ((zerop pixel-delta
))
3096 (setq pixel-delta
(window-sizable window pixel-delta nil nil pixelwise
))
3097 (window--resize-reset frame
)
3098 ;; When shrinking the root window, emulate an edge drag in order
3099 ;; to not resize other windows if we can avoid it (Bug#12419).
3100 (window--resize-this-window
3101 window pixel-delta nil ignore t
'before
3102 (+ (window-pixel-top window
) (window-pixel-height window
)))
3103 ;; Don't record new normal sizes to make sure that shrinking back
3104 ;; proportionally works as intended.
3106 (lambda (window) (set-window-new-normal window
'ignore
)) frame t
))
3108 (window--resize-reset frame
)
3109 (unless (window-sizable window pixel-delta nil nil pixelwise
)
3111 ;; When growing the root window, resize proportionally. This
3112 ;; should give windows back their original sizes (hopefully).
3113 (window--resize-this-window
3114 window pixel-delta nil ignore t
)))
3115 ;; Return the possibly adjusted DELTA.
3118 (/ pixel-delta
(frame-char-height frame
)))))
3120 (defun window--sanitize-window-sizes (frame horizontal
)
3121 "Assert that all windows on FRAME are large enough.
3122 If necessary and possible, make sure that every window on frame
3123 FRAME has its minimum height. Optional argument HORIZONTAL
3124 non-nil means to make sure that every window on frame FRAME has
3125 its minimum width. The minimum height/width of a window is the
3126 respective value returned by `window-min-size' for that window.
3128 Return t if all windows were resized appropriately. Return nil
3129 if at least one window could not be resized as requested, which
3130 may happen when the FRAME is not large enough to accommodate it."
3134 (let ((delta (- (window-min-size window horizontal nil t
)
3135 (window-size window horizontal t
))))
3137 (if (window-resizable-p window delta horizontal nil t
)
3138 (window-resize window delta horizontal nil t
)
3139 (setq value nil
))))))
3142 (defun adjust-window-trailing-edge (window delta
&optional horizontal pixelwise
)
3143 "Move WINDOW's bottom edge by DELTA lines.
3144 Optional argument HORIZONTAL non-nil means move WINDOW's right
3145 edge by DELTA columns. WINDOW must be a valid window and
3146 defaults to the selected one.
3148 Optional argument PIXELWISE non-nil means interpret DELTA as
3151 If DELTA is greater than zero, move the edge downwards or to the
3152 right. If DELTA is less than zero, move the edge upwards or to
3153 the left. If the edge can't be moved by DELTA lines or columns,
3154 move it as far as possible in the desired direction."
3155 (setq window
(window-normalize-window window
))
3156 (let* ((frame (window-frame window
))
3157 (minibuffer-window (minibuffer-window frame
))
3159 left this-delta min-delta max-delta ignore
)
3163 (setq delta
(* delta
(frame-char-size window horizontal
))))
3165 ;; Find the edge we want to move.
3166 (while (and (or (not (window-combined-p right horizontal
))
3167 (not (window-right right
)))
3168 (setq right
(window-parent right
))))
3170 ((and (not right
) (not horizontal
)
3171 ;; Resize the minibuffer window if it's on the same frame as
3172 ;; and immediately below WINDOW and it's either active or
3173 ;; `resize-mini-windows' is nil.
3174 (eq (window-frame minibuffer-window
) frame
)
3175 (= (nth 1 (window-pixel-edges minibuffer-window
))
3176 (nth 3 (window-pixel-edges window
)))
3177 (or (not resize-mini-windows
)
3178 (eq minibuffer-window
(active-minibuffer-window))))
3179 (window--resize-mini-window minibuffer-window
(- delta
)))
3180 ((or (not (setq left right
)) (not (setq right
(window-right right
))))
3182 (user-error "No window on the right of this one")
3183 (user-error "No window below this one")))
3185 ;; Set LEFT to the first resizable window on the left. This step is
3186 ;; needed to handle fixed-size windows.
3188 (or (window-size-fixed-p left horizontal
)
3190 (<= (window-size left horizontal t
)
3191 (window-min-size left horizontal nil t
)))))
3193 (or (window-left left
)
3195 (while (and (setq left
(window-parent left
))
3196 (not (window-combined-p left horizontal
))))
3197 (window-left left
)))))
3199 (setq ignore
'preserved
)
3202 (or (window-size-fixed-p left horizontal
'preserved
)
3203 (<= (window-size left horizontal t
)
3204 (window-min-size left horizontal
'preserved t
))))
3206 (or (window-left left
)
3208 (while (and (setq left
(window-parent left
))
3209 (not (window-combined-p left horizontal
))))
3210 (window-left left
)))))
3214 (user-error "No resizable window on the left of this one")
3215 (user-error "No resizable window above this one"))))
3217 ;; Set RIGHT to the first resizable window on the right. This step
3218 ;; is needed to handle fixed-size windows.
3220 (or (window-size-fixed-p right horizontal
)
3222 (<= (window-size right horizontal t
)
3223 (window-min-size right horizontal
'preserved t
)))))
3225 (or (window-right right
)
3227 (while (and (setq right
(window-parent right
))
3228 (not (window-combined-p right horizontal
))))
3229 (window-right right
)))))
3231 (setq ignore
'preserved
)
3232 (setq right
(window-right window
))
3234 (or (window-size-fixed-p right horizontal
'preserved
))
3235 (<= (window-size right horizontal t
)
3236 (window-min-size right horizontal nil t
)))
3238 (or (window-right right
)
3240 (while (and (setq right
(window-parent right
))
3241 (not (window-combined-p right horizontal
))))
3242 (window-right right
)))))
3245 (user-error "No resizable window on the right of this one")
3246 (user-error "No resizable window below this one"))))
3248 ;; LEFT and RIGHT (which might be both internal windows) are now the
3249 ;; two windows we want to resize.
3253 (window--max-delta-1
3254 left
0 horizontal ignore
'after nil pixelwise
))
3256 (window--min-delta-1
3257 right
(- delta
) horizontal ignore
'before nil pixelwise
))
3258 (when (or (< max-delta delta
) (> min-delta
(- delta
)))
3259 ;; We can't get the whole DELTA - move as far as possible.
3260 (setq delta
(min max-delta
(- min-delta
))))
3261 (unless (zerop delta
)
3263 (window--resize-reset frame horizontal
)
3264 ;; Try to enlarge LEFT first.
3265 (setq this-delta
(window--resizable
3266 left delta horizontal ignore
'after nil nil pixelwise
))
3267 (unless (zerop this-delta
)
3268 (window--resize-this-window
3269 left this-delta horizontal ignore t
'before
3271 (+ (window-pixel-left left
) (window-pixel-width left
))
3272 (+ (window-pixel-top left
) (window-pixel-height left
)))))
3273 ;; Shrink windows on right of LEFT.
3274 (window--resize-siblings
3275 left delta horizontal ignore
'after
3277 (window-pixel-left right
)
3278 (window-pixel-top right
)))))
3281 (window--max-delta-1
3282 right
0 horizontal ignore
'before nil pixelwise
))
3284 (window--min-delta-1
3285 left delta horizontal ignore
'after nil pixelwise
))
3286 (when (or (< max-delta
(- delta
)) (> min-delta delta
))
3287 ;; We can't get the whole DELTA - move as far as possible.
3288 (setq delta
(max (- max-delta
) min-delta
)))
3289 (unless (zerop delta
)
3291 (window--resize-reset frame horizontal
)
3292 ;; Try to enlarge RIGHT.
3295 right
(- delta
) horizontal ignore
'before nil nil pixelwise
))
3296 (unless (zerop this-delta
)
3297 (window--resize-this-window
3298 right this-delta horizontal ignore t
'after
3300 (window-pixel-left right
)
3301 (window-pixel-top right
))))
3302 ;; Shrink windows on left of RIGHT.
3303 (window--resize-siblings
3304 right
(- delta
) horizontal ignore
'before
3306 (+ (window-pixel-left left
) (window-pixel-width left
))
3307 (+ (window-pixel-top left
) (window-pixel-height left
)))))))
3308 (unless (zerop delta
)
3309 ;; Don't report an error in the standard case.
3310 (when (window--resize-apply-p frame horizontal
)
3311 (if (window-resize-apply frame horizontal
)
3313 (window--pixel-to-total frame horizontal
)
3314 (run-window-configuration-change-hook frame
))
3315 ;; But do report an error if applying the changes fails.
3316 (error "Failed adjusting window %s" window
))))))))
3318 (defun enlarge-window (delta &optional horizontal
)
3319 "Make the selected window DELTA lines taller.
3320 Interactively, if no argument is given, make the selected window
3321 one line taller. If optional argument HORIZONTAL is non-nil,
3322 make selected window wider by DELTA columns. If DELTA is
3323 negative, shrink selected window by -DELTA lines or columns."
3325 (let ((minibuffer-window (minibuffer-window)))
3326 (when (window-preserved-size nil horizontal
)
3327 (window-preserve-size nil horizontal
))
3330 ((window-size-fixed-p nil horizontal
)
3331 (error "Selected window has fixed size"))
3332 ((window-minibuffer-p)
3334 (error "Cannot resize minibuffer window horizontally")
3335 (window--resize-mini-window (selected-window) delta
)))
3336 ((and (not horizontal
)
3337 (window-full-height-p)
3338 (eq (window-frame minibuffer-window
) (selected-frame))
3339 (not resize-mini-windows
))
3340 ;; If the selected window is full height and `resize-mini-windows'
3341 ;; is nil, resize the minibuffer window.
3342 (window--resize-mini-window minibuffer-window
(- delta
)))
3343 ((window--resizable-p nil delta horizontal
)
3344 (window-resize nil delta horizontal
))
3348 (window-max-delta nil horizontal
)
3349 (- (window-min-delta nil horizontal
)))
3352 (defun shrink-window (delta &optional horizontal
)
3353 "Make the selected window DELTA lines smaller.
3354 Interactively, if no argument is given, make the selected window
3355 one line smaller. If optional argument HORIZONTAL is non-nil,
3356 make selected window narrower by DELTA columns. If DELTA is
3357 negative, enlarge selected window by -DELTA lines or columns.
3358 Also see the `window-min-height' variable."
3360 (let ((minibuffer-window (minibuffer-window)))
3361 (when (window-preserved-size nil horizontal
)
3362 (window-preserve-size nil horizontal
))
3365 ((window-size-fixed-p nil horizontal
)
3366 (error "Selected window has fixed size"))
3367 ((window-minibuffer-p)
3369 (error "Cannot resize minibuffer window horizontally")
3370 (window--resize-mini-window (selected-window) (- delta
))))
3371 ((and (not horizontal
)
3372 (window-full-height-p)
3373 (eq (window-frame minibuffer-window
) (selected-frame))
3374 (not resize-mini-windows
))
3375 ;; If the selected window is full height and `resize-mini-windows'
3376 ;; is nil, resize the minibuffer window.
3377 (window--resize-mini-window minibuffer-window delta
))
3378 ((window--resizable-p nil
(- delta
) horizontal
)
3379 (window-resize nil
(- delta
) horizontal
))
3383 (- (window-min-delta nil horizontal
))
3384 (window-max-delta nil horizontal
))
3387 (defun maximize-window (&optional window
)
3389 Make WINDOW as large as possible without deleting any windows.
3390 WINDOW must be a valid window and defaults to the selected one.
3392 If the option `window-resize-pixelwise' is non-nil maximize
3395 (setq window
(window-normalize-window window
))
3397 window
(window-max-delta window nil nil nil nil nil window-resize-pixelwise
)
3398 nil nil window-resize-pixelwise
)
3400 window
(window-max-delta window t nil nil nil nil window-resize-pixelwise
)
3401 t nil window-resize-pixelwise
))
3403 (defun minimize-window (&optional window
)
3405 Make WINDOW as small as possible without deleting any windows.
3406 WINDOW must be a valid window and defaults to the selected one.
3408 If the option `window-resize-pixelwise' is non-nil minimize
3411 (setq window
(window-normalize-window window
))
3414 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise
))
3415 nil nil window-resize-pixelwise
)
3418 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise
))
3419 t nil window-resize-pixelwise
))
3421 (defun frame-root-window-p (window)
3422 "Return non-nil if WINDOW is the root window of its frame."
3423 (eq window
(frame-root-window window
)))
3425 (defun window--subtree (window &optional next
)
3426 "Return window subtree rooted at WINDOW.
3427 Optional argument NEXT non-nil means include WINDOW's right
3428 siblings in the return value.
3430 See the documentation of `window-tree' for a description of the
3437 ((window-top-child window
)
3438 (cons t
(cons (window-edges window
)
3439 (window--subtree (window-top-child window
) t
))))
3440 ((window-left-child window
)
3441 (cons nil
(cons (window-edges window
)
3442 (window--subtree (window-left-child window
) t
))))
3445 (setq window
(when next
(window-next-sibling window
))))
3448 (defun window-tree (&optional frame
)
3449 "Return the window tree of frame FRAME.
3450 FRAME must be a live frame and defaults to the selected frame.
3451 The return value is a list of the form (ROOT MINI), where ROOT
3452 represents the window tree of the frame's root window, and MINI
3453 is the frame's minibuffer window.
3455 If the root window is not split, ROOT is the root window itself.
3456 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3457 for a horizontal split, and t for a vertical split. EDGES gives
3458 the combined size and position of the child windows in the split,
3459 and the rest of the elements are the child windows in the split.
3460 Each of the child windows may again be a window or a list
3461 representing a window split, and so on. EDGES is a list (LEFT
3462 TOP RIGHT BOTTOM) as returned by `window-edges'."
3463 (setq frame
(window-normalize-frame frame
))
3464 (window--subtree (frame-root-window frame
) t
))
3466 (defun other-window (count &optional all-frames
)
3467 "Select another window in cyclic ordering of windows.
3468 COUNT specifies the number of windows to skip, starting with the
3469 selected window, before making the selection. If COUNT is
3470 positive, skip COUNT windows forwards. If COUNT is negative,
3471 skip -COUNT windows backwards. COUNT zero means do not skip any
3472 window, so select the selected window. In an interactive call,
3473 COUNT is the numeric prefix argument. Return nil.
3475 If the `other-window' parameter of the selected window is a
3476 function and `ignore-window-parameters' is nil, call that
3477 function with the arguments COUNT and ALL-FRAMES.
3479 This function does not select a window whose `no-other-window'
3480 window parameter is non-nil.
3482 This function uses `next-window' for finding the window to
3483 select. The argument ALL-FRAMES has the same meaning as in
3484 `next-window', but the MINIBUF argument of `next-window' is
3485 always effectively nil."
3487 (let* ((window (selected-window))
3488 (function (and (not ignore-window-parameters
)
3489 (window-parameter window
'other-window
)))
3490 old-window old-count
)
3491 (if (functionp function
)
3492 (funcall function count all-frames
)
3493 ;; `next-window' and `previous-window' may return a window we are
3494 ;; not allowed to select. Hence we need an exit strategy in case
3495 ;; all windows are non-selectable.
3498 (setq window
(next-window window nil all-frames
))
3500 ((eq window old-window
)
3501 (when (= count old-count
)
3502 ;; Keep out of infinite loops. When COUNT has not changed
3503 ;; since we last looked at `window' we're probably in one.
3505 ((window-parameter window
'no-other-window
)
3507 ;; The first non-selectable window `next-window' got us:
3508 ;; Remember it and the current value of COUNT.
3509 (setq old-window window
)
3510 (setq old-count count
)))
3512 (setq count
(1- count
)))))
3514 (setq window
(previous-window window nil all-frames
))
3516 ((eq window old-window
)
3517 (when (= count old-count
)
3518 ;; Keep out of infinite loops. When COUNT has not changed
3519 ;; since we last looked at `window' we're probably in one.
3521 ((window-parameter window
'no-other-window
)
3523 ;; The first non-selectable window `previous-window' got
3524 ;; us: Remember it and the current value of COUNT.
3525 (setq old-window window
)
3526 (setq old-count count
)))
3528 (setq count
(1+ count
)))))
3530 (select-window window
)
3531 ;; Always return nil.
3534 ;; This should probably return non-nil when the selected window is part
3535 ;; of an atomic window whose root is the frame's root window.
3536 (defun one-window-p (&optional nomini all-frames
)
3537 "Return non-nil if the selected window is the only window.
3538 Optional arg NOMINI non-nil means don't count the minibuffer
3539 even if it is active. Otherwise, the minibuffer is counted
3542 Optional argument ALL-FRAMES specifies the set of frames to
3543 consider, see also `next-window'. ALL-FRAMES nil or omitted
3544 means consider windows on the selected frame only, plus the
3545 minibuffer window if specified by the NOMINI argument. If the
3546 minibuffer counts, consider all windows on all frames that share
3547 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3548 with a special meaning are:
3550 - t means consider all windows on all existing frames.
3552 - `visible' means consider all windows on all visible frames on
3553 the current terminal.
3555 - 0 (the number zero) means consider all windows on all visible
3556 and iconified frames on the current terminal.
3558 - A frame means consider all windows on that frame only.
3560 Anything else means consider all windows on the selected frame
3562 (let ((base-window (selected-window)))
3563 (if (and nomini
(eq base-window
(minibuffer-window)))
3564 (setq base-window
(next-window base-window
)))
3566 (next-window base-window
(if nomini
'arg
) all-frames
))))
3568 ;;; Deleting windows.
3569 (defun window-deletable-p (&optional window
)
3570 "Return t if WINDOW can be safely deleted from its frame.
3571 WINDOW must be a valid window and defaults to the selected one.
3572 Return 'frame if deleting WINDOW should also delete its frame."
3573 (setq window
(window-normalize-window window
))
3575 (unless (or ignore-window-parameters
3576 (eq (window-parameter window
'delete-window
) t
))
3577 ;; Handle atomicity.
3578 (when (window-parameter window
'window-atom
)
3579 (setq window
(window-atom-root window
))))
3581 (let ((frame (window-frame window
)))
3583 ((frame-root-window-p window
)
3584 ;; WINDOW's frame can be deleted only if there are other frames
3585 ;; on the same terminal, and it does not contain the active
3587 (unless (or (eq frame
(next-frame frame
0))
3588 ;; We can delete our frame only if no other frame
3589 ;; currently uses our minibuffer window.
3591 (dolist (other (frame-list))
3592 (when (and (not (eq other frame
))
3593 (eq (window-frame (minibuffer-window other
))
3596 (let ((minibuf (active-minibuffer-window)))
3597 (and minibuf
(eq frame
(window-frame minibuf
)))))
3599 ((or ignore-window-parameters
3600 (not (eq window
(window--major-non-side-window frame
))))
3601 ;; WINDOW can be deleted unless it is the major non-side window of
3605 (defun window--in-subtree-p (window root
)
3606 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
3607 (or (eq window root
)
3608 (let ((parent (window-parent window
)))
3611 (if (eq parent root
)
3613 (setq parent
(window-parent parent
))))))))
3615 (defun delete-window (&optional window
)
3617 WINDOW must be a valid window and defaults to the selected one.
3620 If the variable `ignore-window-parameters' is non-nil or the
3621 `delete-window' parameter of WINDOW equals t, do not process any
3622 parameters of WINDOW. Otherwise, if the `delete-window'
3623 parameter of WINDOW specifies a function, call that function with
3624 WINDOW as its sole argument and return the value returned by that
3627 Otherwise, if WINDOW is part of an atomic window, call
3628 `delete-window' with the root of the atomic window as its
3629 argument. Signal an error if WINDOW is either the only window on
3630 its frame, the last non-side window, or part of an atomic window
3631 that is its frame's root window."
3633 (setq window
(window-normalize-window window
))
3634 (let* ((frame (window-frame window
))
3635 (function (window-parameter window
'delete-window
))
3636 (parent (window-parent window
))
3638 (window--check frame
)
3640 ;; Handle window parameters.
3642 ;; Ignore window parameters if `ignore-window-parameters' tells
3643 ;; us so or `delete-window' equals t.
3644 ((or ignore-window-parameters
(eq function t
)))
3645 ((functionp function
)
3646 ;; The `delete-window' parameter specifies the function to call.
3647 ;; If that function is `ignore' nothing is done. It's up to the
3648 ;; function called here to avoid infinite recursion.
3649 (throw 'done
(funcall function window
)))
3650 ((and (window-parameter window
'window-atom
)
3651 (setq atom-root
(window-atom-root window
))
3652 (not (eq atom-root window
)))
3653 (if (eq atom-root
(frame-root-window frame
))
3654 (error "Root of atomic window is root window of its frame")
3655 (throw 'done
(delete-window atom-root
))))
3657 (error "Attempt to delete minibuffer or sole ordinary window"))
3658 ((eq window
(window--major-non-side-window frame
))
3659 (error "Attempt to delete last non-side window")))
3661 (let* ((horizontal (window-left-child parent
))
3662 (size (window-size window horizontal t
))
3664 (window--in-subtree-p (frame-selected-window frame
) window
))
3665 ;; Emacs 23 preferably gives WINDOW's space to its left
3667 (sibling (or (window-left window
) (window-right window
))))
3668 (window--resize-reset frame horizontal
)
3670 ((and (not window-combination-resize
)
3671 sibling
(window-sizable-p sibling size horizontal nil t
))
3672 ;; Resize WINDOW's sibling.
3673 (window--resize-this-window sibling size horizontal nil t
)
3674 (set-window-new-normal
3675 sibling
(+ (window-normal-size sibling horizontal
)
3676 (window-normal-size window horizontal
))))
3677 ((window--resizable-p window
(- size
) horizontal nil nil nil t t
)
3678 ;; Can do without resizing fixed-size windows.
3679 (window--resize-siblings window
(- size
) horizontal
))
3681 ;; Can't do without resizing fixed-size windows.
3682 (window--resize-siblings window
(- size
) horizontal t
)))
3683 ;; Actually delete WINDOW.
3684 (delete-window-internal window
)
3685 (window--pixel-to-total frame horizontal
)
3686 (when (and frame-selected
3688 (frame-selected-window frame
) 'no-other-window
))
3689 ;; `delete-window-internal' has selected a window that should
3690 ;; not be selected, fix this here.
3691 (other-window -
1 frame
))
3692 (run-window-configuration-change-hook frame
)
3693 (window--check frame
)
3694 ;; Always return nil.
3697 (defun delete-other-windows (&optional window
)
3698 "Make WINDOW fill its frame.
3699 WINDOW must be a valid window and defaults to the selected one.
3702 If the variable `ignore-window-parameters' is non-nil or the
3703 `delete-other-windows' parameter of WINDOW equals t, do not
3704 process any parameters of WINDOW. Otherwise, if the
3705 `delete-other-windows' parameter of WINDOW specifies a function,
3706 call that function with WINDOW as its sole argument and return
3707 the value returned by that function.
3709 Otherwise, if WINDOW is part of an atomic window, call this
3710 function with the root of the atomic window as its argument. If
3711 WINDOW is a non-side window, make WINDOW the only non-side window
3712 on the frame. Side windows are not deleted. If WINDOW is a side
3713 window signal an error."
3715 (setq window
(window-normalize-window window
))
3716 (let* ((frame (window-frame window
))
3717 (function (window-parameter window
'delete-other-windows
))
3718 (window-side (window-parameter window
'window-side
))
3719 atom-root side-main
)
3720 (window--check frame
)
3723 ;; Ignore window parameters if `ignore-window-parameters' is t or
3724 ;; `delete-other-windows' is t.
3725 ((or ignore-window-parameters
(eq function t
)))
3726 ((functionp function
)
3727 ;; The `delete-other-windows' parameter specifies the function
3728 ;; to call. If the function is `ignore' no windows are deleted.
3729 ;; It's up to the function called to avoid infinite recursion.
3730 (throw 'done
(funcall function window
)))
3731 ((and (window-parameter window
'window-atom
)
3732 (setq atom-root
(window-atom-root window
))
3733 (not (eq atom-root window
)))
3734 (if (eq atom-root
(frame-root-window frame
))
3735 (error "Root of atomic window is root window of its frame")
3736 (throw 'done
(delete-other-windows atom-root
))))
3737 ((memq window-side window-sides
)
3738 (error "Cannot make side window the only window"))
3739 ((and (window-minibuffer-p window
)
3740 (not (eq window
(frame-root-window window
))))
3741 (error "Can't expand minibuffer to full frame")))
3743 ;; If WINDOW is the major non-side window, do nothing.
3744 (if (window-with-parameter 'window-side
)
3745 (setq side-main
(window--major-non-side-window frame
))
3746 (setq side-main
(frame-root-window frame
)))
3747 (unless (eq window side-main
)
3748 (delete-other-windows-internal window side-main
)
3749 (run-window-configuration-change-hook frame
)
3750 (window--check frame
))
3751 ;; Always return nil.
3754 (defun delete-other-windows-vertically (&optional window
)
3755 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
3756 This may be a useful alternative binding for \\[delete-other-windows]
3757 if you often split windows horizontally."
3759 (let* ((window (or window
(selected-window)))
3760 (edges (window-edges window
))
3762 (while (not (eq (setq w
(next-window w
1)) window
))
3763 (let ((e (window-edges w
)))
3764 (when (and (= (car e
) (car edges
))
3765 (= (nth 2 e
) (nth 2 edges
)))
3767 (mapc 'delete-window delenda
)))
3769 ;;; Windows and buffers.
3771 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
3772 ;; for (1) determining which buffer to show in the window when its
3773 ;; buffer shall be buried or killed and (2) which buffer to show for
3774 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3776 ;; `prev-buffers' consists of <buffer, window-start, window-point>
3777 ;; triples. The entries on this list are ordered by the time their
3778 ;; buffer has been removed from the window, the most recently removed
3779 ;; buffer's entry being first. The window-start and window-point
3780 ;; components are `window-start' and `window-point' at the time the
3781 ;; buffer was removed from the window which implies that the entry must
3782 ;; be added when `set-window-buffer' removes the buffer from the window.
3784 ;; `next-buffers' is the list of buffers that have been replaced
3785 ;; recently by `switch-to-prev-buffer'. These buffers are the least
3786 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
3787 ;; candidates of `switch-to-next-buffer' to switch to. This list is
3788 ;; reset to nil by any action changing the window's buffer with the
3789 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
3790 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
3791 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
3793 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
3794 ;; if such a buffer was killed while the window was hidden within a
3795 ;; window configuration. Such killed buffers get removed whenever
3796 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
3798 ;; The following function is called by `set-window-buffer' _before_ it
3799 ;; replaces the buffer of the argument window with the new buffer.
3800 (defun record-window-buffer (&optional window
)
3801 "Record WINDOW's buffer.
3802 WINDOW must be a live window and defaults to the selected one."
3803 (let* ((window (window-normalize-window window t
))
3804 (buffer (window-buffer window
))
3805 (entry (assq buffer
(window-prev-buffers window
))))
3806 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
3807 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3808 (set-window-next-buffers window nil
)
3811 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
3812 (set-window-prev-buffers
3813 window
(assq-delete-all buffer
(window-prev-buffers window
))))
3815 ;; Don't record insignificant buffers.
3816 (unless (eq (aref (buffer-name buffer
) 0) ?\s
)
3817 ;; Add an entry for buffer to WINDOW's previous buffers.
3818 (with-current-buffer buffer
3819 (let ((start (window-start window
))
3820 (point (window-point window
)))
3824 ;; We have an entry, update marker positions.
3825 (list (set-marker (nth 1 entry
) start
)
3826 (set-marker (nth 2 entry
) point
))
3827 ;; Make new markers.
3828 (list (copy-marker start
)
3830 ;; Preserve window-point-insertion-type
3832 point window-point-insertion-type
)))))
3833 (set-window-prev-buffers
3834 window
(cons entry
(window-prev-buffers window
)))))
3836 (run-hooks 'buffer-list-update-hook
))))
3838 (defun unrecord-window-buffer (&optional window buffer
)
3839 "Unrecord BUFFER in WINDOW.
3840 WINDOW must be a live window and defaults to the selected one.
3841 BUFFER must be a live buffer and defaults to the buffer of
3843 (let* ((window (window-normalize-window window t
))
3844 (buffer (or buffer
(window-buffer window
))))
3845 (set-window-prev-buffers
3846 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3847 (set-window-next-buffers
3848 window
(delq buffer
(window-next-buffers window
)))))
3850 (defun set-window-buffer-start-and-point (window buffer
&optional start point
)
3851 "Set WINDOW's buffer to BUFFER.
3852 WINDOW must be a live window and defaults to the selected one.
3853 Optional argument START non-nil means set WINDOW's start position
3854 to START. Optional argument POINT non-nil means set WINDOW's
3855 point to POINT. If WINDOW is selected this also sets BUFFER's
3856 `point' to POINT. If WINDOW is selected and the buffer it showed
3857 before was current this also makes BUFFER the current buffer."
3858 (setq window
(window-normalize-window window t
))
3859 (let ((selected (eq window
(selected-window)))
3860 (current (eq (window-buffer window
) (current-buffer))))
3861 (set-window-buffer window buffer
)
3862 (when (and selected current
)
3863 (set-buffer buffer
))
3865 ;; Don't force window-start here (even if POINT is nil).
3866 (set-window-start window start t
))
3868 (set-window-point window point
))))
3870 (defcustom switch-to-visible-buffer t
3871 "If non-nil, allow switching to an already visible buffer.
3872 If this variable is non-nil, `switch-to-prev-buffer' and
3873 `switch-to-next-buffer' may switch to an already visible buffer
3874 provided the buffer was shown before in the window specified as
3875 argument to those functions. If this variable is nil,
3876 `switch-to-prev-buffer' and `switch-to-next-buffer' always try to
3877 avoid switching to a buffer that is already visible in another
3878 window on the same frame."
3883 (defun switch-to-prev-buffer (&optional window bury-or-kill
)
3884 "In WINDOW switch to previous buffer.
3885 WINDOW must be a live window and defaults to the selected one.
3886 Return the buffer switched to, nil if no suitable buffer could be
3889 Optional argument BURY-OR-KILL non-nil means the buffer currently
3890 shown in WINDOW is about to be buried or killed and consequently
3891 shall not be switched to in future invocations of this command.
3893 As a special case, if BURY-OR-KILL equals `append', this means to
3894 move the buffer to the end of WINDOW's previous buffers list so a
3895 future invocation of `switch-to-prev-buffer' less likely switches
3898 (let* ((window (window-normalize-window window t
))
3899 (frame (window-frame window
))
3900 (old-buffer (window-buffer window
))
3901 ;; Save this since it's destroyed by `set-window-buffer'.
3902 (next-buffers (window-next-buffers window
))
3903 (pred (frame-parameter frame
'buffer-predicate
))
3904 entry new-buffer killed-buffers visible
)
3905 (when (window-minibuffer-p window
)
3906 ;; Don't switch in minibuffer window.
3907 (unless (setq window
(minibuffer-selected-window))
3908 (error "Window %s is a minibuffer window" window
)))
3910 (when (window-dedicated-p window
)
3911 ;; Don't switch in dedicated window.
3912 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3915 ;; Scan WINDOW's previous buffers first, skipping entries of next
3917 (dolist (entry (window-prev-buffers window
))
3918 (when (and (setq new-buffer
(car entry
))
3919 (or (buffer-live-p new-buffer
)
3920 (not (setq killed-buffers
3921 (cons new-buffer killed-buffers
))))
3922 (not (eq new-buffer old-buffer
))
3923 (or (null pred
) (funcall pred new-buffer
))
3924 ;; When BURY-OR-KILL is nil, avoid switching to a
3925 ;; buffer in WINDOW's next buffers list.
3926 (or bury-or-kill
(not (memq new-buffer next-buffers
))))
3927 (if (and (not switch-to-visible-buffer
)
3928 (get-buffer-window new-buffer frame
))
3929 ;; Try to avoid showing a buffer visible in some other
3931 (setq visible new-buffer
)
3932 (set-window-buffer-start-and-point
3933 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3935 ;; Scan reverted buffer list of WINDOW's frame next, skipping
3936 ;; entries of next buffers. Note that when we bury or kill a
3937 ;; buffer we don't reverse the global buffer list to avoid showing
3938 ;; a buried buffer instead. Otherwise, we must reverse the global
3939 ;; buffer list in order to make sure that switching to the
3940 ;; previous/next buffer traverse it in opposite directions.
3941 (dolist (buffer (if bury-or-kill
3943 (nreverse (buffer-list frame
))))
3944 (when (and (buffer-live-p buffer
)
3945 (not (eq buffer old-buffer
))
3946 (or (null pred
) (funcall pred buffer
))
3947 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3948 (or bury-or-kill
(not (memq buffer next-buffers
))))
3949 (if (get-buffer-window buffer frame
)
3950 ;; Try to avoid showing a buffer visible in some other window.
3952 (setq visible buffer
))
3953 (setq new-buffer buffer
)
3954 (set-window-buffer-start-and-point window new-buffer
)
3956 (unless bury-or-kill
3957 ;; Scan reverted next buffers last (must not use nreverse
3959 (dolist (buffer (reverse next-buffers
))
3960 ;; Actually, buffer _must_ be live here since otherwise it
3961 ;; would have been caught in the scan of previous buffers.
3962 (when (and (or (buffer-live-p buffer
)
3963 (not (setq killed-buffers
3964 (cons buffer killed-buffers
))))
3965 (not (eq buffer old-buffer
))
3966 (or (null pred
) (funcall pred buffer
))
3967 (setq entry
(assq buffer
(window-prev-buffers window
))))
3968 (setq new-buffer buffer
)
3969 (set-window-buffer-start-and-point
3970 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3973 ;; Show a buffer visible in another window.
3975 (setq new-buffer visible
)
3976 (set-window-buffer-start-and-point window new-buffer
)))
3979 (let ((entry (and (eq bury-or-kill
'append
)
3980 (assq old-buffer
(window-prev-buffers window
)))))
3981 ;; Remove `old-buffer' from WINDOW's previous and (restored list
3982 ;; of) next buffers.
3983 (set-window-prev-buffers
3984 window
(assq-delete-all old-buffer
(window-prev-buffers window
)))
3985 (set-window-next-buffers window
(delq old-buffer next-buffers
))
3987 ;; Append old-buffer's entry to list of WINDOW's previous
3988 ;; buffers so it's less likely to get switched to soon but
3989 ;; `display-buffer-in-previous-window' can nevertheless find
3991 (set-window-prev-buffers
3992 window
(append (window-prev-buffers window
) (list entry
)))))
3993 ;; Move `old-buffer' to head of WINDOW's restored list of next
3995 (set-window-next-buffers
3996 window
(cons old-buffer
(delq old-buffer next-buffers
))))
3998 ;; Remove killed buffers from WINDOW's previous and next buffers.
3999 (when killed-buffers
4000 (dolist (buffer killed-buffers
)
4001 (set-window-prev-buffers
4002 window
(assq-delete-all buffer
(window-prev-buffers window
)))
4003 (set-window-next-buffers
4004 window
(delq buffer
(window-next-buffers window
)))))
4006 ;; Return new-buffer.
4009 (defun switch-to-next-buffer (&optional window
)
4010 "In WINDOW switch to next buffer.
4011 WINDOW must be a live window and defaults to the selected one.
4012 Return the buffer switched to, nil if no suitable buffer could be
4015 (let* ((window (window-normalize-window window t
))
4016 (frame (window-frame window
))
4017 (old-buffer (window-buffer window
))
4018 (next-buffers (window-next-buffers window
))
4019 (pred (frame-parameter frame
'buffer-predicate
))
4020 new-buffer entry killed-buffers visible
)
4021 (when (window-minibuffer-p window
)
4022 ;; Don't switch in minibuffer window.
4023 (unless (setq window
(minibuffer-selected-window))
4024 (error "Window %s is a minibuffer window" window
)))
4026 (when (window-dedicated-p window
)
4027 ;; Don't switch in dedicated window.
4028 (error "Window %s is dedicated to buffer %s" window old-buffer
))
4031 ;; Scan WINDOW's next buffers first.
4032 (dolist (buffer next-buffers
)
4033 (when (and (or (buffer-live-p buffer
)
4034 (not (setq killed-buffers
4035 (cons buffer killed-buffers
))))
4036 (not (eq buffer old-buffer
))
4037 (or (null pred
) (funcall pred buffer
))
4038 (setq entry
(assq buffer
(window-prev-buffers window
))))
4039 (setq new-buffer buffer
)
4040 (set-window-buffer-start-and-point
4041 window new-buffer
(nth 1 entry
) (nth 2 entry
))
4043 ;; Scan the buffer list of WINDOW's frame next, skipping previous
4045 (dolist (buffer (buffer-list frame
))
4046 (when (and (buffer-live-p buffer
)
4047 (not (eq buffer old-buffer
))
4048 (or (null pred
) (funcall pred buffer
))
4049 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
4050 (not (assq buffer
(window-prev-buffers window
))))
4051 (if (get-buffer-window buffer frame
)
4052 ;; Try to avoid showing a buffer visible in some other window.
4053 (setq visible buffer
)
4054 (setq new-buffer buffer
)
4055 (set-window-buffer-start-and-point window new-buffer
)
4057 ;; Scan WINDOW's reverted previous buffers last (must not use
4059 (dolist (entry (reverse (window-prev-buffers window
)))
4060 (when (and (setq new-buffer
(car entry
))
4061 (or (buffer-live-p new-buffer
)
4062 (not (setq killed-buffers
4063 (cons new-buffer killed-buffers
))))
4064 (not (eq new-buffer old-buffer
))
4065 (or (null pred
) (funcall pred new-buffer
)))
4066 (if (and (not switch-to-visible-buffer
)
4067 (get-buffer-window new-buffer frame
))
4068 ;; Try to avoid showing a buffer visible in some other window.
4070 (setq visible new-buffer
))
4071 (set-window-buffer-start-and-point
4072 window new-buffer
(nth 1 entry
) (nth 2 entry
))
4075 ;; Show a buffer visible in another window.
4077 (setq new-buffer visible
)
4078 (set-window-buffer-start-and-point window new-buffer
)))
4080 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
4081 (set-window-next-buffers window
(delq new-buffer next-buffers
))
4083 ;; Remove killed buffers from WINDOW's previous and next buffers.
4084 (when killed-buffers
4085 (dolist (buffer killed-buffers
)
4086 (set-window-prev-buffers
4087 window
(assq-delete-all buffer
(window-prev-buffers window
)))
4088 (set-window-next-buffers
4089 window
(delq buffer
(window-next-buffers window
)))))
4091 ;; Return new-buffer.
4094 (defun get-next-valid-buffer (list &optional buffer visible-ok frame
)
4095 "Search LIST for a valid buffer to display in FRAME.
4096 Return nil when all buffers in LIST are undesirable for display,
4097 otherwise return the first suitable buffer in LIST.
4099 Buffers not visible in windows are preferred to visible buffers,
4100 unless VISIBLE-OK is non-nil.
4101 If the optional argument FRAME is nil, it defaults to the selected frame.
4102 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
4103 ;; This logic is more or less copied from other-buffer.
4104 (setq frame
(or frame
(selected-frame)))
4105 (let ((pred (frame-parameter frame
'buffer-predicate
))
4107 (while (and (not found
) list
)
4108 (setq buf
(car list
))
4109 (if (and (not (eq buffer buf
))
4111 (or (null pred
) (funcall pred buf
))
4112 (not (eq (aref (buffer-name buf
) 0) ?\s
))
4113 (or visible-ok
(null (get-buffer-window buf
'visible
))))
4115 (setq list
(cdr list
))))
4118 (defun last-buffer (&optional buffer visible-ok frame
)
4119 "Return the last buffer in FRAME's buffer list.
4120 If BUFFER is the last buffer, return the preceding buffer
4121 instead. Buffers not visible in windows are preferred to visible
4122 buffers, unless optional argument VISIBLE-OK is non-nil.
4123 Optional third argument FRAME nil or omitted means use the
4124 selected frame's buffer list. If no such buffer exists, return
4125 the buffer `*scratch*', creating it if necessary."
4126 (setq frame
(or frame
(selected-frame)))
4127 (or (get-next-valid-buffer (nreverse (buffer-list frame
))
4128 buffer visible-ok frame
)
4129 (get-buffer "*scratch*")
4130 (let ((scratch (get-buffer-create "*scratch*")))
4131 (set-buffer-major-mode scratch
)
4134 (defcustom frame-auto-hide-function
#'iconify-frame
4135 "Function called to automatically hide frames.
4136 The function is called with one argument - a frame.
4138 Functions affected by this option are those that bury a buffer
4139 shown in a separate frame like `quit-window' and `bury-buffer'."
4140 :type
'(choice (const :tag
"Iconify" iconify-frame
)
4141 (const :tag
"Delete" delete-frame
)
4142 (const :tag
"Do nothing" ignore
)
4148 (defun window--delete (&optional window dedicated-only kill
)
4149 "Delete WINDOW if possible.
4150 WINDOW must be a live window and defaults to the selected one.
4151 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
4152 only if it's dedicated to its buffer. Optional argument KILL
4153 means the buffer shown in window will be killed. Return non-nil
4154 if WINDOW gets deleted or its frame is auto-hidden."
4155 (setq window
(window-normalize-window window t
))
4156 (unless (and dedicated-only
(not (window-dedicated-p window
)))
4157 (let ((deletable (window-deletable-p window
)))
4159 ((eq deletable
'frame
)
4160 (let ((frame (window-frame window
)))
4163 (delete-frame frame
))
4164 ((functionp frame-auto-hide-function
)
4165 (funcall frame-auto-hide-function frame
))))
4168 (delete-window window
)
4171 (defun bury-buffer (&optional buffer-or-name
)
4172 "Put BUFFER-OR-NAME at the end of the list of all buffers.
4173 There it is the least likely candidate for `other-buffer' to
4174 return; thus, the least likely buffer for \\[switch-to-buffer] to
4177 You can specify a buffer name as BUFFER-OR-NAME, or an actual
4178 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
4179 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
4180 remove the current buffer from the selected window if it is
4183 (let* ((buffer (window-normalize-buffer buffer-or-name
)))
4184 ;; If `buffer-or-name' is not on the selected frame we unrecord it
4185 ;; although it's not "here" (call it a feature).
4186 (bury-buffer-internal buffer
)
4187 ;; Handle case where `buffer-or-name' is nil and the current buffer
4188 ;; is shown in the selected window.
4190 ((or buffer-or-name
(not (eq buffer
(window-buffer)))))
4191 ((window--delete nil t
))
4193 ;; Switch to another buffer in window.
4194 (set-window-dedicated-p nil nil
)
4195 (switch-to-prev-buffer nil
'bury
)))
4197 ;; Always return nil.
4200 (defun unbury-buffer ()
4201 "Switch to the last buffer in the buffer list."
4203 (switch-to-buffer (last-buffer)))
4205 (defun next-buffer ()
4206 "In selected window switch to next buffer."
4209 ((window-minibuffer-p)
4210 (error "Cannot switch buffers in minibuffer window"))
4211 ((eq (window-dedicated-p) t
)
4212 (error "Window is strongly dedicated to its buffer"))
4214 (switch-to-next-buffer))))
4216 (defun previous-buffer ()
4217 "In selected window switch to previous buffer."
4220 ((window-minibuffer-p)
4221 (error "Cannot switch buffers in minibuffer window"))
4222 ((eq (window-dedicated-p) t
)
4223 (error "Window is strongly dedicated to its buffer"))
4225 (switch-to-prev-buffer))))
4227 (defun delete-windows-on (&optional buffer-or-name frame
)
4228 "Delete all windows showing BUFFER-OR-NAME.
4229 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4230 and defaults to the current buffer.
4232 The following non-nil values of the optional argument FRAME
4233 have special meanings:
4235 - t means consider all windows on the selected frame only.
4237 - `visible' means consider all windows on all visible frames on
4238 the current terminal.
4240 - 0 (the number zero) means consider all windows on all visible
4241 and iconified frames on the current terminal.
4243 - A frame means consider all windows on that frame only.
4245 Any other value of FRAME means consider all windows on all
4248 When a window showing BUFFER-OR-NAME is dedicated and the only
4249 window of its frame, that frame is deleted when there are other
4251 (interactive "BDelete windows on (buffer):\nP")
4252 (let ((buffer (window-normalize-buffer buffer-or-name
))
4253 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4254 ;; `window-list-1' based function.
4255 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
4256 (dolist (window (window-list-1 nil nil all-frames
))
4257 (if (eq (window-buffer window
) buffer
)
4258 (let ((deletable (window-deletable-p window
)))
4260 ((and (eq deletable
'frame
) (window-dedicated-p window
))
4261 ;; Delete frame if and only if window is dedicated.
4262 (delete-frame (window-frame window
)))
4265 (delete-window window
))
4267 ;; In window switch to previous buffer.
4268 (set-window-dedicated-p window nil
)
4269 (switch-to-prev-buffer window
'bury
))))
4270 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4271 (unrecord-window-buffer window buffer
)))))
4273 (defun replace-buffer-in-windows (&optional buffer-or-name
)
4274 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
4275 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4276 and defaults to the current buffer.
4278 When a window showing BUFFER-OR-NAME is dedicated, that window is
4279 deleted. If that window is the only window on its frame, the
4280 frame is deleted too when there are other frames left. If there
4281 are no other frames left, some other buffer is displayed in that
4284 This function removes the buffer denoted by BUFFER-OR-NAME from
4285 all window-local buffer lists."
4286 (interactive "bBuffer to replace: ")
4287 (let ((buffer (window-normalize-buffer buffer-or-name
)))
4288 (dolist (window (window-list-1 nil nil t
))
4289 (if (eq (window-buffer window
) buffer
)
4290 (unless (window--delete window t t
)
4291 ;; Switch to another buffer in window.
4292 (set-window-dedicated-p window nil
)
4293 (switch-to-prev-buffer window
'kill
))
4294 ;; Unrecord BUFFER in WINDOW.
4295 (unrecord-window-buffer window buffer
)))))
4297 (defun quit-restore-window (&optional window bury-or-kill
)
4298 "Quit WINDOW and deal with its buffer.
4299 WINDOW must be a live window and defaults to the selected one.
4301 According to information stored in WINDOW's `quit-restore' window
4302 parameter either (1) delete WINDOW and its frame, (2) delete
4303 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4304 or (4) make WINDOW display some other buffer than the present
4305 one. If non-nil, reset `quit-restore' parameter to nil.
4307 Optional second argument BURY-OR-KILL tells how to proceed with
4308 the buffer of WINDOW. The following values are handled:
4310 `nil' means to not handle the buffer in a particular way. This
4311 means that if WINDOW is not deleted by this function, invoking
4312 `switch-to-prev-buffer' will usually show the buffer again.
4314 `append' means that if WINDOW is not deleted, move its buffer to
4315 the end of WINDOW's previous buffers so it's less likely that a
4316 future invocation of `switch-to-prev-buffer' will switch to it.
4317 Also, move the buffer to the end of the frame's buffer list.
4319 `bury' means that if WINDOW is not deleted, remove its buffer
4320 from WINDOW'S list of previous buffers. Also, move the buffer
4321 to the end of the frame's buffer list. This value provides the
4322 most reliable remedy to not have `switch-to-prev-buffer' switch
4323 to this buffer again without killing the buffer.
4325 `kill' means to kill WINDOW's buffer."
4326 (setq window
(window-normalize-window window t
))
4327 (let* ((buffer (window-buffer window
))
4328 (quit-restore (window-parameter window
'quit-restore
))
4330 (let* ((prev-buffers (window-prev-buffers window
))
4331 (prev-buffer (caar prev-buffers
)))
4332 (and (or (not (eq prev-buffer buffer
))
4333 (and (cdr prev-buffers
)
4334 (not (eq (setq prev-buffer
(cadr prev-buffers
))
4339 ((and (not prev-buffer
)
4340 (or (eq (nth 1 quit-restore
) 'frame
)
4341 (and (eq (nth 1 quit-restore
) 'window
)
4342 ;; If the window has been created on an existing
4343 ;; frame and ended up as the sole window on that
4344 ;; frame, do not delete it (Bug#12764).
4345 (not (eq window
(frame-root-window window
)))))
4346 (eq (nth 3 quit-restore
) buffer
)
4347 ;; Delete WINDOW if possible.
4348 (window--delete window nil
(eq bury-or-kill
'kill
)))
4349 ;; If the previously selected window is still alive, select it.
4350 (when (window-live-p (nth 2 quit-restore
))
4351 (select-window (nth 2 quit-restore
))))
4352 ((and (listp (setq quad
(nth 1 quit-restore
)))
4353 (buffer-live-p (car quad
))
4354 (eq (nth 3 quit-restore
) buffer
))
4355 ;; Show another buffer stored in quit-restore parameter.
4356 (when (and (integerp (nth 3 quad
))
4357 (/= (nth 3 quad
) (window-total-height window
)))
4358 ;; Try to resize WINDOW to its old height but don't signal an
4361 (window-resize window
(- (nth 3 quad
) (window-total-height window
)))
4363 (set-window-dedicated-p window nil
)
4364 ;; Restore WINDOW's previous buffer, start and point position.
4365 (set-window-buffer-start-and-point
4366 window
(nth 0 quad
) (nth 1 quad
) (nth 2 quad
))
4367 ;; Deal with the buffer we just removed from WINDOW.
4368 (setq entry
(and (eq bury-or-kill
'append
)
4369 (assq buffer
(window-prev-buffers window
))))
4371 ;; Remove buffer from WINDOW's previous and next buffers.
4372 (set-window-prev-buffers
4373 window
(assq-delete-all buffer
(window-prev-buffers window
)))
4374 (set-window-next-buffers
4375 window
(delq buffer
(window-next-buffers window
))))
4377 ;; Append old buffer's entry to list of WINDOW's previous
4378 ;; buffers so it's less likely to get switched to soon but
4379 ;; `display-buffer-in-previous-window' can nevertheless find it.
4380 (set-window-prev-buffers
4381 window
(append (window-prev-buffers window
) (list entry
))))
4382 ;; Reset the quit-restore parameter.
4383 (set-window-parameter window
'quit-restore nil
)
4384 ;; Select old window.
4385 (when (window-live-p (nth 2 quit-restore
))
4386 (select-window (nth 2 quit-restore
))))
4388 ;; Show some other buffer in WINDOW and reset the quit-restore
4390 (set-window-parameter window
'quit-restore nil
)
4391 ;; Make sure that WINDOW is no more dedicated.
4392 (set-window-dedicated-p window nil
)
4393 (switch-to-prev-buffer window bury-or-kill
)))
4395 ;; Deal with the buffer.
4397 ((not (buffer-live-p buffer
)))
4398 ((eq bury-or-kill
'kill
)
4399 (kill-buffer buffer
))
4401 (bury-buffer-internal buffer
)))))
4403 (defun quit-window (&optional kill window
)
4404 "Quit WINDOW and bury its buffer.
4405 WINDOW must be a live window and defaults to the selected one.
4406 With prefix argument KILL non-nil, kill the buffer instead of
4409 According to information stored in WINDOW's `quit-restore' window
4410 parameter either (1) delete WINDOW and its frame, (2) delete
4411 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4412 or (4) make WINDOW display some other buffer than the present
4413 one. If non-nil, reset `quit-restore' parameter to nil."
4415 (quit-restore-window window
(if kill
'kill
'bury
)))
4417 (defun quit-windows-on (&optional buffer-or-name kill frame
)
4418 "Quit all windows showing BUFFER-OR-NAME.
4419 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4420 and defaults to the current buffer. Optional argument KILL
4421 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4422 BUFFER-OR-NAME. Optional argument FRAME is handled as by
4423 `delete-windows-on'.
4425 This function calls `quit-window' on all candidate windows
4426 showing BUFFER-OR-NAME."
4427 (interactive "BQuit windows on (buffer):\nP")
4428 (let ((buffer (window-normalize-buffer buffer-or-name
))
4429 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4430 ;; `window-list-1' based function.
4431 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
4432 (dolist (window (window-list-1 nil nil all-frames
))
4433 (if (eq (window-buffer window
) buffer
)
4434 (quit-window kill window
)
4435 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4436 (unrecord-window-buffer window buffer
)))))
4438 (defun split-window (&optional window size side pixelwise
)
4439 "Make a new window adjacent to WINDOW.
4440 WINDOW must be a valid window and defaults to the selected one.
4441 Return the new window which is always a live window.
4443 Optional argument SIZE a positive number means make WINDOW SIZE
4444 lines or columns tall. If SIZE is negative, make the new window
4445 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
4446 absolute value can be less than `window-min-height' or
4447 `window-min-width'; so this command can make a new window as
4448 small as one line or two columns. SIZE defaults to half of
4451 Optional third argument SIDE nil (or `below') specifies that the
4452 new window shall be located below WINDOW. SIDE `above' means the
4453 new window shall be located above WINDOW. In both cases SIZE
4454 specifies the new number of lines for WINDOW (or the new window
4455 if SIZE is negative) including space reserved for the mode and/or
4458 SIDE t (or `right') specifies that the new window shall be
4459 located on the right side of WINDOW. SIDE `left' means the new
4460 window shall be located on the left of WINDOW. In both cases
4461 SIZE specifies the new number of columns for WINDOW (or the new
4462 window provided SIZE is negative) including space reserved for
4463 fringes and the scrollbar or a divider column. Any other non-nil
4464 value for SIDE is currently handled like t (or `right').
4466 PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
4468 If the variable `ignore-window-parameters' is non-nil or the
4469 `split-window' parameter of WINDOW equals t, do not process any
4470 parameters of WINDOW. Otherwise, if the `split-window' parameter
4471 of WINDOW specifies a function, call that function with all three
4472 arguments and return the value returned by that function.
4474 Otherwise, if WINDOW is part of an atomic window, \"split\" the
4475 root of that atomic window. The new window does not become a
4476 member of that atomic window.
4478 If WINDOW is live, properties of the new window like margins and
4479 scrollbars are inherited from WINDOW. If WINDOW is an internal
4480 window, these properties as well as the buffer displayed in the
4481 new window are inherited from the window selected on WINDOW's
4482 frame. The selected window is not changed by this function."
4483 (setq window
(window-normalize-window window
))
4486 ((memq side
'(below above right left
)) side
)
4488 (horizontal (not (memq side
'(below above
))))
4489 (frame (window-frame window
))
4490 (parent (window-parent window
))
4491 (function (window-parameter window
'split-window
))
4492 (window-side (window-parameter window
'window-side
))
4493 ;; Rebind the following two variables since in some cases we
4494 ;; have to override their value.
4495 (window-combination-limit window-combination-limit
)
4496 (window-combination-resize window-combination-resize
)
4497 (char-size (frame-char-size window horizontal
))
4499 (when (numberp size
)
4500 (window--size-to-pixel window size horizontal pixelwise t
)))
4501 (divider-width (if horizontal
4502 (frame-right-divider-width frame
)
4503 (frame-bottom-divider-width frame
)))
4505 (window--check frame
)
4508 ;; Ignore window parameters if either `ignore-window-parameters'
4509 ;; is t or the `split-window' parameter equals t.
4510 ((or ignore-window-parameters
(eq function t
)))
4511 ((functionp function
)
4512 ;; The `split-window' parameter specifies the function to call.
4513 ;; If that function is `ignore', do nothing.
4514 (throw 'done
(funcall function window size side
)))
4515 ;; If WINDOW is part of an atomic window, split the root window
4516 ;; of that atomic window instead.
4517 ((and (window-parameter window
'window-atom
)
4518 (setq atom-root
(window-atom-root window
))
4519 (not (eq atom-root window
)))
4520 (throw 'done
(split-window atom-root size side pixelwise
)))
4521 ;; If WINDOW is a side window or its first or last child is a
4522 ;; side window, throw an error unless `window-combination-resize'
4524 ((and (not (eq window-combination-resize
'side
))
4525 (window--side-window-p window
))
4526 (error "Cannot split side window or parent of side window"))
4527 ;; If `window-combination-resize' is 'side and window has a side
4528 ;; window sibling, bind `window-combination-limit' to t.
4529 ((and (not (eq window-combination-resize
'side
))
4530 (or (and (window-prev-sibling window
)
4532 (window-prev-sibling window
) 'window-side
))
4533 (and (window-next-sibling window
)
4535 (window-next-sibling window
) 'window-side
))))
4536 (setq window-combination-limit t
)))
4538 ;; If `window-combination-resize' is t and SIZE is non-negative,
4539 ;; bind `window-combination-limit' to t.
4540 (when (and (eq window-combination-resize t
)
4541 pixel-size
(> pixel-size
0))
4542 (setq window-combination-limit t
))
4544 (let* ((parent-pixel-size
4545 ;; `parent-pixel-size' is the pixel size of WINDOW's
4546 ;; parent, provided it has one.
4547 (when parent
(window-size parent horizontal t
)))
4548 ;; `resize' non-nil means we are supposed to resize other
4549 ;; windows in WINDOW's combination.
4551 (and window-combination-resize
4552 (or (window-parameter window
'window-side
)
4553 (not (eq window-combination-resize
'side
)))
4554 (not (eq window-combination-limit t
))
4555 ;; Resize makes sense in iso-combinations only.
4556 (window-combined-p window horizontal
)))
4557 ;; `old-pixel-size' is the current pixel size of WINDOW.
4558 (old-pixel-size (window-size window horizontal t
))
4559 ;; `new-size' is the specified or calculated size of the
4561 new-pixel-size new-parent new-normal
)
4564 (setq new-pixel-size
4566 ;; When resizing try to give the new window the
4567 ;; average size of a window in its combination.
4568 (max (min (- parent-pixel-size
4569 (window-min-size parent horizontal nil t
))
4570 (/ parent-pixel-size
4571 (1+ (window-combinations parent horizontal
))))
4572 (window-min-pixel-size))
4573 ;; Else try to give the new window half the size
4574 ;; of WINDOW (plus an eventual odd pixel).
4575 (/ old-pixel-size
2)))
4576 (unless window-resize-pixelwise
4577 ;; Round to nearest char-size multiple.
4578 (setq new-pixel-size
4579 (* char-size
(round new-pixel-size char-size
)))))
4581 ;; SIZE non-negative specifies the new size of WINDOW.
4583 ;; Note: Specifying a non-negative SIZE is practically
4584 ;; always done as workaround for making the new window
4585 ;; appear above or on the left of the new window (the
4586 ;; ispell window is a typical example of that). In all
4587 ;; these cases the SIDE argument should be set to 'above
4588 ;; or 'left in order to support the 'resize option.
4589 ;; Here we have to nest the windows instead, see above.
4590 (setq new-pixel-size
(- old-pixel-size pixel-size
)))
4592 ;; SIZE negative specifies the size of the new window.
4593 (setq new-pixel-size
(- pixel-size
))))
4600 ;; SIZE unspecified, resizing.
4601 (unless (or (window-sizable-p
4602 parent
(- (+ new-pixel-size divider-width
)) horizontal
4605 parent
(- (+ new-pixel-size divider-width
)) horizontal
4606 (setq ignore
'preserved
) t
))
4607 (error "Window %s too small for splitting (1)" parent
)))
4608 ((and (> (+ new-pixel-size divider-width
4609 (window-min-size window horizontal nil t
))
4611 (> (+ new-pixel-size divider-width
4613 window horizontal
(setq ignore
'preserved
) t
))
4615 ;; SIZE unspecified, no resizing.
4616 (error "Window %s too small for splitting (2)" window
))))
4617 ((and (>= pixel-size
0)
4618 (or (>= pixel-size old-pixel-size
)
4620 (window-safe-min-pixel-size window horizontal
))))
4621 ;; SIZE specified as new size of old window. If the new size
4622 ;; is larger than the old size or the size of the new window
4623 ;; would be less than the safe minimum, signal an error.
4624 (error "Window %s too small for splitting (3)" window
))
4626 ;; SIZE specified, resizing.
4627 (unless (or (window-sizable-p
4628 parent
(- (+ new-pixel-size divider-width
)) horizontal
4631 parent
(- (+ new-pixel-size divider-width
)) horizontal
4632 (setq ignore
'preserved
) t
))
4633 ;; If we cannot resize the parent give up.
4634 (error "Window %s too small for splitting (4)" parent
)))
4635 ((or (< new-pixel-size
4636 (window-safe-min-pixel-size window horizontal
))
4637 (< (- old-pixel-size new-pixel-size
)
4638 (window-safe-min-pixel-size window horizontal
)))
4639 ;; SIZE specification violates minimum size restrictions.
4640 (error "Window %s too small for splitting (5)" window
)))
4642 (window--resize-reset frame horizontal
)
4645 ;; Make new-parent non-nil if we need a new parent window;
4646 ;; either because we want to nest or because WINDOW is not
4648 (or (eq window-combination-limit t
)
4649 (not (window-combined-p window horizontal
))))
4651 ;; Make new-normal the normal size of the new window.
4653 (pixel-size (/ (float new-pixel-size
)
4654 (if new-parent old-pixel-size parent-pixel-size
)))
4656 (resize (/ 1.0 (1+ (window-combinations parent horizontal
))))
4657 (t (/ (window-normal-size window horizontal
) 2.0))))
4660 ;; Try to get space from OLD's siblings. We could go "up" and
4661 ;; try getting additional space from surrounding windows but
4662 ;; we won't be able to return space to those windows when we
4663 ;; delete the one we create here. Hence we do not go up.
4665 (window--resize-child-windows
4666 parent
(- new-pixel-size
) horizontal nil ignore
)
4667 (let* ((normal (- 1.0 new-normal
))
4668 (sub (window-child parent
)))
4670 (set-window-new-normal
4671 sub
(* (window-normal-size sub horizontal
) normal
))
4672 (setq sub
(window-right sub
)))))
4673 ;; Get entire space from WINDOW.
4674 (set-window-new-pixel
4675 window
(- old-pixel-size new-pixel-size
))
4676 (window--resize-this-window
4677 window
(- new-pixel-size
) horizontal ignore
)
4678 (set-window-new-normal
4679 window
(- (if new-parent
1.0 (window-normal-size window horizontal
))
4682 (let* ((new (split-window-internal window new-pixel-size side new-normal
)))
4683 (window--pixel-to-total frame horizontal
)
4684 ;; Assign window-side parameters, if any.
4686 ((eq window-combination-resize
'side
)
4689 (window-side window-side
)
4690 ((eq side
'above
) 'top
)
4691 ((eq side
'below
) 'bottom
)
4693 ;; We made a new side window.
4694 (set-window-parameter new
'window-side window-side
)
4695 (when (and new-parent
(window-parameter window
'window-side
))
4696 ;; We've been splitting a side root window. Give the
4697 ;; new parent the same window-side parameter.
4698 (set-window-parameter
4699 (window-parent new
) 'window-side window-side
))))
4700 ((eq window-combination-resize
'atom
)
4701 ;; Make sure `window--check-frame' won't destroy an existing
4702 ;; atomic window in case the new window gets nested inside.
4703 (unless (window-parameter window
'window-atom
)
4704 (set-window-parameter window
'window-atom t
))
4706 (set-window-parameter (window-parent new
) 'window-atom t
))
4707 (set-window-parameter new
'window-atom t
)))
4710 (window--sanitize-window-sizes frame horizontal
)
4712 (run-window-configuration-change-hook frame
)
4713 (run-window-scroll-functions new
)
4714 (window--check frame
)
4715 ;; Always return the new window.
4718 ;; I think this should be the default; I think people will prefer it--rms.
4719 (defcustom split-window-keep-point t
4720 "If non-nil, \\[split-window-below] preserves point in the new window.
4721 If nil, adjust point in the two windows to minimize redisplay.
4722 This option applies only to `split-window-below' and functions
4723 that call it. The low-level `split-window' function always keeps
4724 the original point in both windows."
4728 (defun split-window-below (&optional size
)
4729 "Split the selected window into two windows, one above the other.
4730 The selected window is above. The newly split-off window is
4731 below and displays the same buffer. Return the new window.
4733 If optional argument SIZE is omitted or nil, both windows get the
4734 same height, or close to it. If SIZE is positive, the upper
4735 \(selected) window gets SIZE lines. If SIZE is negative, the
4736 lower (new) window gets -SIZE lines.
4738 If the variable `split-window-keep-point' is non-nil, both
4739 windows get the same value of point as the selected window.
4740 Otherwise, the window starts are chosen so as to minimize the
4741 amount of redisplay; this is convenient on slow terminals."
4743 (let ((old-window (selected-window))
4744 (old-point (window-point))
4745 (size (and size
(prefix-numeric-value size
)))
4746 moved-by-window-height moved new-window bottom
)
4747 (when (and size
(< size
0) (< (- size
) window-min-height
))
4748 ;; `split-window' would not signal an error here.
4749 (error "Size of new window too small"))
4750 (setq new-window
(split-window nil size
))
4751 (unless split-window-keep-point
4752 (with-current-buffer (window-buffer)
4753 ;; Use `save-excursion' around vertical movements below
4754 ;; (Bug#10971). Note: When the selected window's buffer has a
4755 ;; header line, up to two lines of the buffer may not show up
4756 ;; in the resulting configuration.
4758 (goto-char (window-start))
4759 (setq moved
(vertical-motion (window-height)))
4760 (set-window-start new-window
(point))
4761 (when (> (point) (window-point new-window
))
4762 (set-window-point new-window
(point)))
4763 (when (= moved
(window-height))
4764 (setq moved-by-window-height t
)
4765 (vertical-motion -
1))
4766 (setq bottom
(point)))
4767 (and moved-by-window-height
4769 (set-window-point old-window
(1- bottom
)))
4770 (and moved-by-window-height
4771 (<= (window-start new-window
) old-point
)
4772 (set-window-point new-window old-point
)
4773 (select-window new-window
))))
4774 ;; Always copy quit-restore parameter in interactive use.
4775 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
4777 (set-window-parameter new-window
'quit-restore quit-restore
)))
4780 (defalias 'split-window-vertically
'split-window-below
)
4782 (defun split-window-right (&optional size
)
4783 "Split the selected window into two side-by-side windows.
4784 The selected window is on the left. The newly split-off window
4785 is on the right and displays the same buffer. Return the new
4788 If optional argument SIZE is omitted or nil, both windows get the
4789 same width, or close to it. If SIZE is positive, the left-hand
4790 \(selected) window gets SIZE columns. If SIZE is negative, the
4791 right-hand (new) window gets -SIZE columns. Here, SIZE includes
4792 the width of the window's scroll bar; if there are no scroll
4793 bars, it includes the width of the divider column to the window's
4796 (let ((old-window (selected-window))
4797 (size (and size
(prefix-numeric-value size
)))
4799 (when (and size
(< size
0) (< (- size
) window-min-width
))
4800 ;; `split-window' would not signal an error here.
4801 (error "Size of new window too small"))
4802 (setq new-window
(split-window nil size t
))
4803 ;; Always copy quit-restore parameter in interactive use.
4804 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
4806 (set-window-parameter new-window
'quit-restore quit-restore
)))
4809 (defalias 'split-window-horizontally
'split-window-right
)
4811 ;;; Balancing windows.
4813 ;; The following routine uses the recycled code from an old version of
4814 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
4815 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
4816 ;; more readable (FWIW we'd need three loops - one to calculate the
4817 ;; minimum sizes per window, one to enlarge or shrink windows until the
4818 ;; new parent-size matches, and one where we shrink the largest/enlarge
4819 ;; the smallest window).
4820 (defun balance-windows-2 (window horizontal
)
4821 "Subroutine of `balance-windows-1'.
4822 WINDOW must be a vertical combination (horizontal if HORIZONTAL
4824 (let* ((char-size (if window-resize-pixelwise
4826 (frame-char-size window horizontal
)))
4827 (first (window-child window
))
4829 (number-of-children 0)
4830 (parent-size (window-new-pixel window
))
4831 (total-sum parent-size
)
4832 failed size sub-total sub-delta sub-amount rest
)
4834 (setq number-of-children
(1+ number-of-children
))
4835 (when (window-size-fixed-p sub horizontal
)
4837 (- total-sum
(window-size sub horizontal t
)))
4838 (set-window-new-normal sub
'ignore
))
4839 (setq sub
(window-right sub
)))
4842 (while (and failed
(> number-of-children
0))
4843 (setq size
(/ total-sum number-of-children
))
4846 (while (and sub
(not failed
))
4847 ;; Ignore child windows that should be ignored or are stuck.
4848 (unless (window--resize-child-windows-skip-p sub
)
4849 (setq sub-total
(window-size sub horizontal t
))
4850 (setq sub-delta
(- size sub-total
))
4852 (window-sizable sub sub-delta horizontal nil t
))
4853 ;; Register the new total size for this child window.
4854 (set-window-new-pixel sub
(+ sub-total sub-amount
))
4855 (unless (= sub-amount sub-delta
)
4856 (setq total-sum
(- total-sum sub-total sub-amount
))
4857 (setq number-of-children
(1- number-of-children
))
4858 ;; We failed and need a new round.
4860 (set-window-new-normal sub
'skip
)))
4861 (setq sub
(window-right sub
))))
4863 ;; How can we be sure that `number-of-children' is NOT zero here ?
4864 (setq rest
(% total-sum number-of-children
))
4865 ;; Fix rounding by trying to enlarge non-stuck windows by one line
4866 ;; (column) until `rest' is zero.
4868 (while (and sub
(> rest
0))
4869 (unless (window--resize-child-windows-skip-p window
)
4870 (set-window-new-pixel sub
(min rest char-size
) t
)
4871 (setq rest
(- rest char-size
)))
4872 (setq sub
(window-right sub
)))
4874 ;; Fix rounding by trying to enlarge stuck windows by one line
4875 ;; (column) until `rest' equals zero.
4877 (while (and sub
(> rest
0))
4878 (unless (eq (window-new-normal sub
) 'ignore
)
4879 (set-window-new-pixel sub
(min rest char-size
) t
)
4880 (setq rest
(- rest char-size
)))
4881 (setq sub
(window-right sub
)))
4885 ;; Record new normal sizes.
4886 (set-window-new-normal
4887 sub
(/ (if (eq (window-new-normal sub
) 'ignore
)
4888 (window-size sub horizontal t
)
4889 (window-new-pixel sub
))
4890 (float parent-size
)))
4891 ;; Recursively balance each window's child windows.
4892 (balance-windows-1 sub horizontal
)
4893 (setq sub
(window-right sub
)))))
4895 (defun balance-windows-1 (window &optional horizontal
)
4896 "Subroutine of `balance-windows'."
4897 (if (window-child window
)
4898 (let ((sub (window-child window
)))
4899 (if (window-combined-p sub horizontal
)
4900 (balance-windows-2 window horizontal
)
4901 (let ((size (window-new-pixel window
)))
4903 (set-window-new-pixel sub size
)
4904 (balance-windows-1 sub horizontal
)
4905 (setq sub
(window-right sub
))))))))
4907 (defun balance-windows (&optional window-or-frame
)
4908 "Balance the sizes of windows of WINDOW-OR-FRAME.
4909 WINDOW-OR-FRAME is optional and defaults to the selected frame.
4910 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
4911 windows of that frame. If WINDOW-OR-FRAME denotes a window,
4912 recursively balance the sizes of all child windows of that
4917 ((or (not window-or-frame
)
4918 (frame-live-p window-or-frame
))
4919 (frame-root-window window-or-frame
))
4920 ((or (window-live-p window-or-frame
)
4921 (window-child window-or-frame
))
4924 (error "Not a window or frame %s" window-or-frame
))))
4925 (frame (window-frame window
)))
4926 ;; Balance vertically.
4927 (window--resize-reset (window-frame window
))
4928 (balance-windows-1 window
)
4929 (when (window--resize-apply-p frame
)
4930 (window-resize-apply frame
)
4931 (window--pixel-to-total frame
)
4932 (run-window-configuration-change-hook frame
))
4933 ;; Balance horizontally.
4934 (window--resize-reset (window-frame window
) t
)
4935 (balance-windows-1 window t
)
4936 (when (window--resize-apply-p frame t
)
4937 (window-resize-apply frame t
)
4938 (window--pixel-to-total frame t
)
4939 (run-window-configuration-change-hook frame
))))
4941 (defun window-fixed-size-p (&optional window direction
)
4942 "Return t if WINDOW cannot be resized in DIRECTION.
4943 WINDOW defaults to the selected window. DIRECTION can be
4944 nil (i.e. any), `height' or `width'."
4945 (with-current-buffer (window-buffer window
)
4946 (when (and (boundp 'window-size-fixed
) window-size-fixed
)
4948 (member (cons direction window-size-fixed
)
4949 '((height . width
) (width . height
))))))))
4951 ;;; A different solution to balance-windows.
4952 (defvar window-area-factor
1
4953 "Factor by which the window area should be over-estimated.
4954 This is used by `balance-windows-area'.
4955 Changing this globally has no effect.")
4956 (make-variable-buffer-local 'window-area-factor
)
4958 (defun balance-windows-area-adjust (window delta horizontal pixelwise
)
4959 "Wrapper around `window-resize' with error checking.
4960 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
4961 ;; `window-resize' may fail if delta is too large.
4962 (while (>= (abs delta
) 1)
4965 ;; It was wrong to use `window-resize' here. Somehow
4966 ;; `balance-windows-area' depends on resizing windows
4968 (adjust-window-trailing-edge window delta horizontal pixelwise
)
4971 ;;(message "adjust: %s" (error-message-string err))
4972 (setq delta
(/ delta
2))))))
4974 (defun balance-windows-area ()
4975 "Make all visible windows the same area (approximately).
4976 See also `window-area-factor' to change the relative size of
4979 (let* ((unchanged 0) (carry 0) (round 0)
4980 ;; Remove fixed-size windows.
4981 (wins (delq nil
(mapcar (lambda (win)
4982 (if (not (window-fixed-size-p win
)) win
))
4983 (window-list nil
'nomini
))))
4985 (pixelwise window-resize-pixelwise
)
4987 ;; Resizing a window changes the size of surrounding windows in complex
4988 ;; ways, so it's difficult to balance them all. The introduction of
4989 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
4990 ;; very difficult to do. `balance-window' above takes an off-line
4991 ;; approach: get the whole window tree, then balance it, then try to
4992 ;; adjust the windows so they fit the result.
4993 ;; Here, instead, we take a "local optimization" approach, where we just
4994 ;; go through all the windows several times until nothing needs to be
4995 ;; changed. The main problem with this approach is that it's difficult
4996 ;; to make sure it terminates, so we use some heuristic to try and break
4997 ;; off infinite loops.
4998 ;; After a round without any change, we allow a second, to give a chance
4999 ;; to the carry to propagate a minor imbalance from the end back to
5001 (while (< unchanged
2)
5002 ;; (message "New round")
5003 (setq unchanged
(1+ unchanged
) round
(1+ round
))
5006 (while (progn (setq next
(next-window next
))
5007 (window-fixed-size-p next
)))
5008 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
5010 (< (car (window-pixel-edges win
)) (car (window-pixel-edges next
))))
5011 (areadiff (/ (- (* (window-size next nil pixelwise
)
5012 (window-size next t pixelwise
)
5013 (buffer-local-value 'window-area-factor
5014 (window-buffer next
)))
5015 (* (window-size win nil pixelwise
)
5016 (window-size win t pixelwise
)
5017 (buffer-local-value 'window-area-factor
5018 (window-buffer win
))))
5019 (max (buffer-local-value 'window-area-factor
5020 (window-buffer win
))
5021 (buffer-local-value 'window-area-factor
5022 (window-buffer next
)))))
5024 (+ (window-size win nil pixelwise
)
5025 (window-size next nil pixelwise
))
5026 (+ (window-size win t pixelwise
)
5027 (window-size next t pixelwise
))))
5028 (diff (/ areadiff edgesize
)))
5030 ;; Maybe diff is actually closer to 1 than to 0.
5031 (setq diff
(/ (* 3 areadiff
) (* 2 edgesize
))))
5032 (when (and (zerop diff
) (not (zerop areadiff
)))
5033 (setq diff
(/ (+ areadiff carry
) edgesize
))
5034 ;; Change things smoothly.
5035 (if (or (> diff
1) (< diff -
1)) (setq diff
(/ diff
2))))
5037 ;; Make sure negligible differences don't accumulate to
5038 ;; become significant.
5039 (setq carry
(+ carry areadiff
))
5040 ;; This used `adjust-window-trailing-edge' before and uses
5041 ;; `window-resize' now. Error wrapping is still needed.
5042 (balance-windows-area-adjust win diff horiz pixelwise
)
5044 (let ((change (cons win
(window-pixel-edges win
))))
5045 ;; If the same change has been seen already for this window,
5046 ;; we're most likely in an endless loop, so don't count it as
5048 (unless (member change changelog
)
5049 (push change changelog
)
5050 (setq unchanged
0 carry
0)))))))
5051 ;; We've now basically balanced all the windows.
5052 ;; But there may be some minor off-by-one imbalance left over,
5053 ;; so let's do some fine tuning.
5054 ;; (bw-finetune wins)
5055 ;; (message "Done in %d rounds" round)
5058 ;;; Window states, how to get them and how to put them in a window.
5059 (defun window--state-get-1 (window &optional writable
)
5060 "Helper function for `window-state-get'."
5063 ((window-top-child window
) 'vc
)
5064 ((window-left-child window
) 'hc
)
5066 (buffer (window-buffer window
))
5067 (selected (eq window
(selected-window)))
5070 ,@(unless (window-next-sibling window
) `((last . t
)))
5071 (pixel-width .
,(window-pixel-width window
))
5072 (pixel-height .
,(window-pixel-height window
))
5073 (total-width .
,(window-total-width window
))
5074 (total-height .
,(window-total-height window
))
5075 (normal-height .
,(window-normal-size window
))
5076 (normal-width .
,(window-normal-size window t
))
5077 ,@(unless (window-live-p window
)
5078 `((combination-limit .
,(window-combination-limit window
))))
5079 ,@(let ((parameters (window-parameters window
))
5081 ;; Make copies of those window parameters whose
5082 ;; persistence property is `writable' if WRITABLE is
5083 ;; non-nil and non-nil if WRITABLE is nil.
5084 (dolist (par parameters
)
5085 (let ((pers (cdr (assq (car par
)
5086 window-persistent-parameters
))))
5087 (when (and pers
(or (not writable
) (eq pers
'writable
)))
5088 (setq list
(cons (cons (car par
) (cdr par
)) list
)))))
5089 ;; Add `clone-of' parameter if necessary.
5090 (let ((pers (cdr (assq 'clone-of
5091 window-persistent-parameters
))))
5092 (when (and pers
(or (not writable
) (eq pers
'writable
))
5093 (not (assq 'clone-of list
)))
5094 (setq list
(cons (cons 'clone-of window
) list
))))
5096 `((parameters .
,list
))))
5098 ;; All buffer related things go in here.
5099 (let ((point (window-point window
))
5100 (start (window-start window
)))
5102 ,(buffer-name buffer
)
5103 (selected .
,selected
)
5104 (hscroll .
,(window-hscroll window
))
5105 (fringes .
,(window-fringes window
))
5106 (margins .
,(window-margins window
))
5107 (scroll-bars .
,(window-scroll-bars window
))
5108 (vscroll .
,(window-vscroll window
))
5109 (dedicated .
,(window-dedicated-p window
))
5110 (point .
,(if writable point
5113 'window-point-insertion-type
5115 (start .
,(if writable start
(copy-marker start
)))))))))
5117 (when (memq type
'(vc hc
))
5119 (setq window
(window-child window
))
5121 (setq list
(cons (window--state-get-1 window writable
) list
))
5122 (setq window
(window-right window
)))
5124 (append head tail
)))
5126 (defun window-state-get (&optional window writable
)
5127 "Return state of WINDOW as a Lisp object.
5128 WINDOW can be any window and defaults to the root window of the
5131 Optional argument WRITABLE non-nil means do not use markers for
5132 sampling `window-point' and `window-start'. Together, WRITABLE
5133 and the variable `window-persistent-parameters' specify which
5134 window parameters are saved by this function. WRITABLE should be
5135 non-nil when the return value shall be written to a file and read
5136 back in another session. Otherwise, an application may run into
5137 an `invalid-read-syntax' error while attempting to read back the
5140 The return value can be used as argument for `window-state-put'
5141 to put the state recorded here into an arbitrary window. The
5142 value can be also stored on disk and read back in a new session."
5145 (if (window-valid-p window
)
5147 (error "%s is not a live or internal window" window
))
5148 (frame-root-window)))
5149 ;; The return value is a cons whose car specifies some constraints on
5150 ;; the size of WINDOW. The cdr lists the states of the child windows
5153 ;; Frame related things would go into a function, say `frame-state',
5154 ;; calling `window-state-get' to insert the frame's root window.
5155 `((min-height .
,(window-min-size window
))
5156 (min-width .
,(window-min-size window t
))
5157 (min-height-ignore .
,(window-min-size window nil t
))
5158 (min-width-ignore .
,(window-min-size window t t
))
5159 (min-height-safe .
,(window-min-size window nil
'safe
))
5160 (min-width-safe .
,(window-min-size window t
'safe
))
5161 (min-pixel-height .
,(window-min-size window nil nil t
))
5162 (min-pixel-width .
,(window-min-size window t nil t
))
5163 (min-pixel-height-ignore .
,(window-min-size window nil t t
))
5164 (min-pixel-width-ignore .
,(window-min-size window t t t
))
5165 (min-pixel-height-safe .
,(window-min-size window nil
'safe t
))
5166 (min-pixel-width-safe .
,(window-min-size window t
'safe t
)))
5167 (window--state-get-1 window writable
)))
5169 (defvar window-state-put-list nil
5170 "Helper variable for `window-state-put'.")
5172 (defvar window-state-put-stale-windows nil
5173 "Helper variable for `window-state-put'.")
5175 (defun window--state-put-1 (state &optional window ignore totals pixelwise
)
5176 "Helper function for `window-state-put'."
5177 (let ((type (car state
)))
5178 (setq state
(cdr state
))
5181 ;; For a leaf window just add unprocessed entries to
5182 ;; `window-state-put-list'.
5183 (push (cons window state
) window-state-put-list
))
5184 ((memq type
'(vc hc
))
5185 (let* ((horizontal (eq type
'hc
))
5186 (total (window-size window horizontal pixelwise
))
5189 (dolist (item state
)
5190 ;; Find the next child window. WINDOW always points to the
5191 ;; real window that we want to fill with what we find here.
5192 (when (memq (car item
) '(leaf vc hc
))
5193 (if (assq 'last item
)
5194 ;; The last child window. Below `window--state-put-1'
5195 ;; will put into it whatever ITEM has in store.
5197 ;; Not the last child window, prepare for splitting
5198 ;; WINDOW. SIZE is the new (and final) size of the old
5204 (cdr (assq (if horizontal
5208 (cdr (assq (if horizontal
5212 ;; Use normalized size and round.
5215 (cdr (assq (if horizontal
'normal-width
'normal-height
)
5218 ;; Use safe sizes, we try to resize later.
5219 (setq size
(max size
5221 (* window-safe-min-width
5223 (frame-char-width (window-frame window
))
5225 (* window-safe-min-height
5227 (frame-char-height (window-frame window
))
5229 (if (window-sizable-p window
(- size
) horizontal
'safe pixelwise
)
5230 (let* ((window-combination-limit
5231 (assq 'combination-limit item
)))
5232 ;; We must inherit the combination limit, otherwise
5233 ;; we might mess up handling of atomic and side
5235 (setq new
(split-window window size horizontal pixelwise
)))
5236 ;; Give up if we can't resize window down to safe sizes.
5237 (error "Cannot resize window %s" window
))
5241 ;; When creating the first child window add for parent
5242 ;; unprocessed entries to `window-state-put-list'.
5243 (setq window-state-put-list
5244 (cons (cons (window-parent window
) state
)
5245 window-state-put-list
))))
5247 ;; Now process the current window (either the one we've just
5248 ;; split or the last child of its parent).
5249 (window--state-put-1 item window ignore totals
)
5250 ;; Continue with the last window split off.
5251 (setq window new
))))))))
5253 (defun window--state-put-2 (ignore pixelwise
)
5254 "Helper function for `window-state-put'."
5255 (dolist (item window-state-put-list
)
5256 (let ((window (car item
))
5257 (combination-limit (cdr (assq 'combination-limit item
)))
5258 (parameters (cdr (assq 'parameters item
)))
5259 (state (cdr (assq 'buffer item
))))
5260 (when combination-limit
5261 (set-window-combination-limit window combination-limit
))
5262 ;; Reset window's parameters and assign saved ones (we might want
5263 ;; a `remove-window-parameters' function here).
5264 (dolist (parameter (window-parameters window
))
5265 (set-window-parameter window
(car parameter
) nil
))
5267 (dolist (parameter parameters
)
5268 (set-window-parameter window
(car parameter
) (cdr parameter
))))
5269 ;; Process buffer related state.
5271 (let ((buffer (get-buffer (car state
))))
5273 (with-current-buffer buffer
5274 (set-window-buffer window buffer
)
5275 (set-window-hscroll window
(cdr (assq 'hscroll state
)))
5276 (apply 'set-window-fringes
5277 (cons window
(cdr (assq 'fringes state
))))
5278 (let ((margins (cdr (assq 'margins state
))))
5279 (set-window-margins window
(car margins
) (cdr margins
)))
5280 (let ((scroll-bars (cdr (assq 'scroll-bars state
))))
5281 (set-window-scroll-bars
5282 window
(car scroll-bars
) (nth 2 scroll-bars
)
5283 (nth 3 scroll-bars
) (nth 5 scroll-bars
)))
5284 (set-window-vscroll window
(cdr (assq 'vscroll state
)))
5285 ;; Adjust vertically.
5286 (if (memq window-size-fixed
'(t height
))
5287 ;; A fixed height window, try to restore the
5291 (if pixelwise
'pixel-height
'total-height
)
5293 (window-size window nil pixelwise
)))
5295 (when (window--resizable-p
5296 window delta nil nil nil nil nil pixelwise
)
5297 (window-resize window delta nil nil pixelwise
)))
5298 ;; Else check whether the window is not high enough.
5300 (window-min-size window nil ignore pixelwise
))
5302 (- min-size
(window-size window nil pixelwise
))))
5303 (when (and (> delta
0)
5304 (window--resizable-p
5305 window delta nil ignore nil nil nil pixelwise
))
5306 (window-resize window delta nil ignore pixelwise
))))
5307 ;; Adjust horizontally.
5308 (if (memq window-size-fixed
'(t width
))
5309 ;; A fixed width window, try to restore the original
5313 (if pixelwise
'pixel-width
'total-width
)
5315 (window-size window t pixelwise
)))
5317 (when (window--resizable-p
5318 window delta nil nil nil nil nil pixelwise
)
5319 (window-resize window delta nil nil pixelwise
)))
5320 ;; Else check whether the window is not wide enough.
5321 (let* ((min-size (window-min-size window t ignore pixelwise
))
5322 (delta (- min-size
(window-size window t pixelwise
))))
5323 (when (and (> delta
0)
5324 (window--resizable-p
5325 window delta t ignore nil nil nil pixelwise
))
5326 (window-resize window delta t ignore pixelwise
))))
5327 ;; Set dedicated status.
5328 (set-window-dedicated-p window
(cdr (assq 'dedicated state
)))
5329 ;; Install positions (maybe we should do this after all
5330 ;; windows have been created and sized).
5332 (set-window-start window
(cdr (assq 'start state
)))
5333 (set-window-point window
(cdr (assq 'point state
))))
5334 ;; Select window if it's the selected one.
5335 (when (cdr (assq 'selected state
))
5336 (select-window window
)))
5337 ;; We don't want to raise an error in case the buffer does
5338 ;; not exist anymore, so we switch to a previous one and
5339 ;; save the window with the intention of deleting it later
5341 (switch-to-prev-buffer window
)
5342 (push window window-state-put-stale-windows
)))))))
5344 (defun window-state-put (state &optional window ignore
)
5345 "Put window state STATE into WINDOW.
5346 STATE should be the state of a window returned by an earlier
5347 invocation of `window-state-get'. Optional argument WINDOW must
5348 specify a valid window and defaults to the selected one. If
5349 WINDOW is not live, replace WINDOW by a live one before putting
5352 Optional argument IGNORE non-nil means ignore minimum window
5353 sizes and fixed size restrictions. IGNORE equal `safe' means
5354 windows can get as small as `window-safe-min-height' and
5355 `window-safe-min-width'."
5356 (setq window-state-put-stale-windows nil
)
5357 (setq window
(window-normalize-window window
))
5359 ;; When WINDOW is internal, reduce it to a live one to put STATE into,
5361 (unless (window-live-p window
)
5362 (let ((root (frame-root-window window
)))
5363 (if (eq window root
)
5364 (setq window
(frame-first-window root
))
5366 (setq window
(catch 'live
5367 (walk-window-subtree
5369 (when (window-live-p window
)
5370 (throw 'live window
)))
5372 (delete-other-windows-internal window root
)))
5374 (let* ((frame (window-frame window
))
5376 ;; We check here (1) whether the total sizes of root window of
5377 ;; STATE and that of WINDOW are equal so we can avoid
5378 ;; calculating new sizes, and (2) if we do have to resize
5379 ;; whether we can do so without violating size restrictions.
5380 (pixelwise (and (cdr (assq 'pixel-width state
))
5381 (cdr (assq 'pixel-height state
))))
5382 (totals (or (and pixelwise
5383 (= (window-pixel-width window
)
5384 (cdr (assq 'pixel-width state
)))
5385 (= (window-pixel-height window
)
5386 (cdr (assq 'pixel-height state
))))
5387 (and (= (window-total-width window
)
5388 (cdr (assq 'total-width state
)))
5389 (= (window-total-height window
)
5390 (cdr (assq 'total-height state
))))))
5391 (min-height (cdr (assq
5392 (if pixelwise
'min-pixel-height
'min-height
)
5394 (min-width (cdr (assq
5395 (if pixelwise
'min-pixel-width
'min-weight
)
5397 (if (and (not totals
)
5398 (or (> min-height
(window-size window nil pixelwise
))
5399 (> min-width
(window-size window t pixelwise
)))
5401 (and (setq min-height
5404 'min-pixel-height-ignore
5410 'min-pixel-width-ignore
5414 (window-size window nil pixelwise
))
5416 (window-size window t pixelwise
)))
5417 (or (not (eq ignore
'safe
))
5418 (and (setq min-height
5421 'min-pixel-height-safe
5427 'min-pixel-width-safe
5431 (window-size window nil pixelwise
))
5433 (window-size window t pixelwise
))))))))
5434 ;; The check above might not catch all errors due to rounding
5435 ;; issues - so IGNORE equal 'safe might not always produce the
5436 ;; minimum possible state. But such configurations hardly make
5438 (error "Window %s too small to accommodate state" window
)
5439 (setq state
(cdr state
))
5440 (setq window-state-put-list nil
)
5441 ;; Work on the windows of a temporary buffer to make sure that
5442 ;; splitting proceeds regardless of any buffer local values of
5443 ;; `window-size-fixed'. Release that buffer after the buffers of
5444 ;; all live windows have been set by `window--state-put-2'.
5446 (set-window-buffer window
(current-buffer))
5447 (window--state-put-1 state window nil totals pixelwise
)
5448 (window--state-put-2 ignore pixelwise
))
5449 (while window-state-put-stale-windows
5450 (let ((window (pop window-state-put-stale-windows
)))
5451 (when (eq (window-deletable-p window
) t
)
5452 (delete-window window
))))
5453 (window--check frame
))))
5455 (defun display-buffer-record-window (type window buffer
)
5456 "Record information for window used by `display-buffer'.
5457 TYPE specifies the type of the calling operation and must be one
5458 of the symbols 'reuse (when WINDOW existed already and was
5459 reused for displaying BUFFER), 'window (when WINDOW was created
5460 on an already existing frame), or 'frame (when WINDOW was
5461 created on a new frame). WINDOW is the window used for or created
5462 by the `display-buffer' routines. BUFFER is the buffer that
5465 This function installs or updates the quit-restore parameter of
5466 WINDOW. The quit-restore parameter is a list of four elements:
5467 The first element is one of the symbols 'window, 'frame, 'same or
5468 'other. The second element is either one of the symbols 'window
5469 or 'frame or a list whose elements are the buffer previously
5470 shown in the window, that buffer's window start and window point,
5471 and the window's height. The third element is the window
5472 selected at the time the parameter was created. The fourth
5476 (if (eq (window-buffer window
) buffer
)
5477 ;; WINDOW shows BUFFER already. Update WINDOW's quit-restore
5478 ;; parameter, if any.
5479 (let ((quit-restore (window-parameter window
'quit-restore
)))
5480 (when (consp quit-restore
)
5481 (setcar quit-restore
'same
)
5482 ;; The selected-window might have changed in
5483 ;; between (Bug#20353).
5484 (unless (memq (selected-window) '(window (nth 2 quit-restore
)))
5485 (setcar (cddr quit-restore
) (selected-window)))))
5486 ;; WINDOW shows another buffer.
5487 (with-current-buffer (window-buffer window
)
5488 (set-window-parameter
5489 window
'quit-restore
5491 ;; A quadruple of WINDOW's buffer, start, point and height.
5492 (list (current-buffer) (window-start window
)
5493 ;; Preserve window-point-insertion-type (Bug#12588).
5495 (window-point window
) window-point-insertion-type
)
5496 (window-total-height window
))
5497 (selected-window) buffer
)))))
5499 ;; WINDOW has been created on an existing frame.
5500 (set-window-parameter
5501 window
'quit-restore
5502 (list 'window
'window
(selected-window) buffer
)))
5504 ;; WINDOW has been created on a new frame.
5505 (set-window-parameter
5506 window
'quit-restore
5507 (list 'frame
'frame
(selected-window) buffer
)))))
5509 (defcustom display-buffer-function nil
5510 "If non-nil, function to call to handle `display-buffer'.
5511 It will receive two args, the buffer and a flag which if non-nil
5512 means that the currently selected window is not acceptable. It
5513 should choose or create a window, display the specified buffer in
5514 it, and return the window.
5516 The specified function should call `display-buffer-record-window'
5517 with corresponding arguments to set up the quit-restore parameter
5518 of the window used."
5521 (function :tag
"function"))
5524 (make-obsolete-variable 'display-buffer-function
5525 'display-buffer-alist
"24.3")
5527 ;; Eventually, we want to turn this into a defvar; instead of
5528 ;; customizing this, the user should use a `pop-up-frame-parameters'
5529 ;; alist entry in `display-buffer-base-action'.
5530 (defcustom pop-up-frame-alist nil
5531 "Alist of parameters for automatically generated new frames.
5532 If non-nil, the value you specify here is used by the default
5533 `pop-up-frame-function' for the creation of new frames.
5535 Since `pop-up-frame-function' is used by `display-buffer' for
5536 making new frames, any value specified here by default affects
5537 the automatic generation of new frames via `display-buffer' and
5538 all functions based on it. The behavior of `make-frame' is not
5539 affected by this variable."
5540 :type
'(repeat (cons :format
"%v"
5541 (symbol :tag
"Parameter")
5542 (sexp :tag
"Value")))
5545 (defcustom pop-up-frame-function
5546 (lambda () (make-frame pop-up-frame-alist
))
5547 "Function used by `display-buffer' for creating a new frame.
5548 This function is called with no arguments and should return a new
5549 frame. The default value calls `make-frame' with the argument
5550 `pop-up-frame-alist'."
5554 (defcustom special-display-buffer-names nil
5555 "List of names of buffers that should be displayed specially.
5556 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5557 its name is in this list, displays the buffer in a way specified
5558 by `special-display-function'. `special-display-popup-frame'
5559 \(the default for `special-display-function') usually displays
5560 the buffer in a separate frame made with the parameters specified
5561 by `special-display-frame-alist'. If `special-display-function'
5562 has been set to some other function, that function is called with
5563 the buffer as first, and nil as second argument.
5565 Alternatively, an element of this list can be specified as
5566 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
5567 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5568 `special-display-popup-frame' will interpret such pairs as frame
5569 parameters when it creates a special frame, overriding the
5570 corresponding values from `special-display-frame-alist'.
5572 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5573 `special-display-popup-frame' displays that buffer in the
5574 selected window. If FRAME-PARAMETERS contains (same-frame . t),
5575 it displays that buffer in a window on the selected frame.
5577 If `special-display-function' specifies some other function than
5578 `special-display-popup-frame', that function is called with the
5579 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5582 Finally, an element of this list can be also specified as
5583 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5584 `special-display-popup-frame' will call FUNCTION with the buffer
5585 named BUFFER-NAME as first argument, and OTHER-ARGS as the
5588 Any alternative function specified here is responsible for
5589 setting up the quit-restore parameter of the window used.
5591 If this variable appears \"not to work\", because you added a
5592 name to it but the corresponding buffer is displayed in the
5593 selected window, look at the values of `same-window-buffer-names'
5594 and `same-window-regexps'. Those variables take precedence over
5597 See also `special-display-regexps'."
5599 (choice :tag
"Buffer"
5601 (string :format
"%v")
5602 (cons :tag
"With parameters"
5605 (string :format
"%v")
5606 (repeat :tag
"Parameters"
5608 (symbol :tag
"Parameter")
5609 (sexp :tag
"Value"))))
5610 (list :tag
"With function"
5613 (string :format
"%v")
5614 (function :tag
"Function")
5615 (repeat :tag
"Arguments" (sexp)))))
5618 (make-obsolete-variable 'special-display-buffer-names
'display-buffer-alist
"24.3")
5619 (put 'special-display-buffer-names
'risky-local-variable t
)
5621 (defcustom special-display-regexps nil
5622 "List of regexps saying which buffers should be displayed specially.
5623 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5624 any regexp in this list matches its name, displays it specially
5625 using `special-display-function'. `special-display-popup-frame'
5626 \(the default for `special-display-function') usually displays
5627 the buffer in a separate frame made with the parameters specified
5628 by `special-display-frame-alist'. If `special-display-function'
5629 has been set to some other function, that function is called with
5630 the buffer as first, and nil as second argument.
5632 Alternatively, an element of this list can be specified as
5633 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
5634 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5635 `special-display-popup-frame' will then interpret these pairs as
5636 frame parameters when creating a special frame for a buffer whose
5637 name matches REGEXP, overriding the corresponding values from
5638 `special-display-frame-alist'.
5640 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5641 `special-display-popup-frame' displays buffers matching REGEXP in
5642 the selected window. (same-frame . t) in FRAME-PARAMETERS means
5643 to display such buffers in a window on the selected frame.
5645 If `special-display-function' specifies some other function than
5646 `special-display-popup-frame', that function is called with the
5647 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
5650 Finally, an element of this list can be also specified as
5651 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
5652 will then call FUNCTION with the buffer whose name matched
5653 REGEXP as first, and OTHER-ARGS as second argument.
5655 Any alternative function specified here is responsible for
5656 setting up the quit-restore parameter of the window used.
5658 If this variable appears \"not to work\", because you added a
5659 name to it but the corresponding buffer is displayed in the
5660 selected window, look at the values of `same-window-buffer-names'
5661 and `same-window-regexps'. Those variables take precedence over
5664 See also `special-display-buffer-names'."
5666 (choice :tag
"Buffer"
5668 (regexp :format
"%v")
5669 (cons :tag
"With parameters"
5672 (regexp :format
"%v")
5673 (repeat :tag
"Parameters"
5675 (symbol :tag
"Parameter")
5676 (sexp :tag
"Value"))))
5677 (list :tag
"With function"
5680 (regexp :format
"%v")
5681 (function :tag
"Function")
5682 (repeat :tag
"Arguments" (sexp)))))
5685 (make-obsolete-variable 'special-display-regexps
'display-buffer-alist
"24.3")
5686 (put 'special-display-regexps
'risky-local-variable t
)
5688 (defun special-display-p (buffer-name)
5689 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
5690 More precisely, return t if `special-display-buffer-names' or
5691 `special-display-regexps' contain a string entry equaling or
5692 matching BUFFER-NAME. If `special-display-buffer-names' or
5693 `special-display-regexps' contain a list entry whose car equals
5694 or matches BUFFER-NAME, the return value is the cdr of that
5698 ((member buffer-name special-display-buffer-names
)
5700 ((setq tmp
(assoc buffer-name special-display-buffer-names
))
5703 (dolist (regexp special-display-regexps
)
5706 (when (string-match-p regexp buffer-name
)
5708 ((and (consp regexp
) (stringp (car regexp
))
5709 (string-match-p (car regexp
) buffer-name
))
5710 (throw 'found
(cdr regexp
))))))))))
5712 (defcustom special-display-frame-alist
5713 '((height .
14) (width .
80) (unsplittable . t
))
5714 "Alist of parameters for special frames.
5715 Special frames are used for buffers whose names are listed in
5716 `special-display-buffer-names' and for buffers whose names match
5717 one of the regular expressions in `special-display-regexps'.
5719 This variable can be set in your init file, like this:
5721 (setq special-display-frame-alist '((width . 80) (height . 20)))
5723 These supersede the values given in `default-frame-alist'."
5724 :type
'(repeat (cons :format
"%v"
5725 (symbol :tag
"Parameter")
5726 (sexp :tag
"Value")))
5728 (make-obsolete-variable 'special-display-frame-alist
'display-buffer-alist
"24.3")
5730 (defun special-display-popup-frame (buffer &optional args
)
5731 "Pop up a frame displaying BUFFER and return its window.
5732 If BUFFER is already displayed in a visible or iconified frame,
5733 raise that frame. Otherwise, display BUFFER in a new frame.
5735 Optional argument ARGS is a list specifying additional
5738 If ARGS is an alist, use it as a list of frame parameters. If
5739 these parameters contain (same-window . t), display BUFFER in
5740 the selected window. If they contain (same-frame . t), display
5741 BUFFER in a window of the selected frame.
5743 If ARGS is a list whose car is a symbol, use (car ARGS) as a
5744 function to do the work. Pass it BUFFER as first argument, and
5745 pass the elements of (cdr ARGS) as the remaining arguments."
5746 (if (and args
(symbolp (car args
)))
5747 (apply (car args
) buffer
(cdr args
))
5748 (let ((window (get-buffer-window buffer
0)))
5750 ;; If we have a window already, make it visible.
5752 (let ((frame (window-frame window
)))
5753 (make-frame-visible frame
)
5755 (display-buffer-record-window 'reuse window buffer
)
5757 ;; Reuse the current window if the user requested it.
5758 (when (cdr (assq 'same-window args
))
5760 (progn (switch-to-buffer buffer nil t
) (selected-window))
5762 ;; Stay on the same frame if requested.
5763 (when (or (cdr (assq 'same-frame args
)) (cdr (assq 'same-window args
)))
5764 (let* ((pop-up-windows t
)
5766 special-display-buffer-names special-display-regexps
)
5767 (display-buffer buffer
)))
5768 ;; If no window yet, make one in a new frame.
5770 (with-current-buffer buffer
5771 (make-frame (append args special-display-frame-alist
))))
5772 (window (frame-selected-window frame
)))
5773 (display-buffer-record-window 'frame window buffer
)
5774 (unless (eq buffer
(window-buffer window
))
5775 (set-window-buffer window buffer
)
5776 (set-window-prev-buffers window nil
))
5777 (set-window-dedicated-p window t
)
5780 (defcustom special-display-function
'special-display-popup-frame
5781 "Function to call for displaying special buffers.
5782 This function is called with two arguments - the buffer and,
5783 optionally, a list - and should return a window displaying that
5784 buffer. The default value usually makes a separate frame for the
5785 buffer using `special-display-frame-alist' to specify the frame
5786 parameters. See the definition of `special-display-popup-frame'
5787 for how to specify such a function.
5789 A buffer is special when its name is either listed in
5790 `special-display-buffer-names' or matches a regexp in
5791 `special-display-regexps'.
5793 The specified function should call `display-buffer-record-window'
5794 with corresponding arguments to set up the quit-restore parameter
5795 of the window used."
5798 (make-obsolete-variable 'special-display-function
'display-buffer-alist
"24.3")
5800 (defcustom same-window-buffer-names nil
5801 "List of names of buffers that should appear in the \"same\" window.
5802 `display-buffer' and `pop-to-buffer' show a buffer whose name is
5803 on this list in the selected rather than some other window.
5805 An element of this list can be a cons cell instead of just a
5806 string. In that case, the cell's car must be a string specifying
5807 the buffer name. This is for compatibility with
5808 `special-display-buffer-names'; the cdr of the cons cell is
5811 See also `same-window-regexps'."
5812 :type
'(repeat (string :format
"%v"))
5815 (defcustom same-window-regexps nil
5816 "List of regexps saying which buffers should appear in the \"same\" window.
5817 `display-buffer' and `pop-to-buffer' show a buffer whose name
5818 matches a regexp on this list in the selected rather than some
5821 An element of this list can be a cons cell instead of just a
5822 string. In that case, the cell's car must be a regexp matching
5823 the buffer name. This is for compatibility with
5824 `special-display-regexps'; the cdr of the cons cell is ignored.
5826 See also `same-window-buffer-names'."
5827 :type
'(repeat (regexp :format
"%v"))
5830 (defun same-window-p (buffer-name)
5831 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5832 This function returns non-nil if `display-buffer' or
5833 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
5834 selected rather than (as usual) some other window. See
5835 `same-window-buffer-names' and `same-window-regexps'."
5837 ((not (stringp buffer-name
)))
5838 ;; The elements of `same-window-buffer-names' can be buffer
5839 ;; names or cons cells whose cars are buffer names.
5840 ((member buffer-name same-window-buffer-names
))
5841 ((assoc buffer-name same-window-buffer-names
))
5843 (dolist (regexp same-window-regexps
)
5844 ;; The elements of `same-window-regexps' can be regexps
5845 ;; or cons cells whose cars are regexps.
5846 (when (or (and (stringp regexp
)
5847 (string-match-p regexp buffer-name
))
5848 (and (consp regexp
) (stringp (car regexp
))
5849 (string-match-p (car regexp
) buffer-name
)))
5850 (throw 'found t
)))))))
5852 (defcustom pop-up-frames nil
5853 "Whether `display-buffer' should make a separate frame.
5854 If nil, never make a separate frame.
5855 If the value is `graphic-only', make a separate frame
5856 on graphic displays only.
5857 Any other non-nil value means always make a separate frame."
5859 (const :tag
"Never" nil
)
5860 (const :tag
"On graphic displays only" graphic-only
)
5861 (const :tag
"Always" t
))
5864 (defcustom display-buffer-reuse-frames nil
5865 "Non-nil means `display-buffer' should reuse frames.
5866 If the buffer in question is already displayed in a frame, raise
5872 (make-obsolete-variable
5873 'display-buffer-reuse-frames
5874 "use a `reusable-frames' alist entry in `display-buffer-alist'."
5877 (defcustom pop-up-windows t
5878 "Non-nil means `display-buffer' should make a new window."
5882 (defcustom split-window-preferred-function
'split-window-sensibly
5883 "Function called by `display-buffer' routines to split a window.
5884 This function is called with a window as single argument and is
5885 supposed to split that window and return the new window. If the
5886 window can (or shall) not be split, it is supposed to return nil.
5887 The default is to call the function `split-window-sensibly' which
5888 tries to split the window in a way which seems most suitable.
5889 You can customize the options `split-height-threshold' and/or
5890 `split-width-threshold' in order to have `split-window-sensibly'
5891 prefer either vertical or horizontal splitting.
5893 If you set this to any other function, bear in mind that the
5894 `display-buffer' routines may call this function two times. The
5895 argument of the first call is the largest window on its frame.
5896 If that call fails to return a live window, the function is
5897 called again with the least recently used window as argument. If
5898 that call fails too, `display-buffer' will use an existing window
5899 to display its buffer.
5901 The window selected at the time `display-buffer' was invoked is
5902 still selected when this function is called. Hence you can
5903 compare the window argument with the value of `selected-window'
5904 if you intend to split the selected window instead or if you do
5905 not want to split the selected window."
5910 (defcustom split-height-threshold
80
5911 "Minimum height for splitting windows sensibly.
5912 If this is an integer, `split-window-sensibly' may split a window
5913 vertically only if it has at least this many lines. If this is
5914 nil, `split-window-sensibly' is not allowed to split a window
5915 vertically. If, however, a window is the only window on its
5916 frame, `split-window-sensibly' may split it vertically
5917 disregarding the value of this variable."
5918 :type
'(choice (const nil
) (integer :tag
"lines"))
5922 (defcustom split-width-threshold
160
5923 "Minimum width for splitting windows sensibly.
5924 If this is an integer, `split-window-sensibly' may split a window
5925 horizontally only if it has at least this many columns. If this
5926 is nil, `split-window-sensibly' is not allowed to split a window
5928 :type
'(choice (const nil
) (integer :tag
"columns"))
5932 (defun window-splittable-p (window &optional horizontal
)
5933 "Return non-nil if `split-window-sensibly' may split WINDOW.
5934 Optional argument HORIZONTAL nil or omitted means check whether
5935 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
5936 non-nil means check whether WINDOW may be split horizontally.
5938 WINDOW may be split vertically when the following conditions
5940 - `window-size-fixed' is either nil or equals `width' for the
5942 - `split-height-threshold' is an integer and WINDOW is at least as
5943 high as `split-height-threshold'.
5944 - When WINDOW is split evenly, the emanating windows are at least
5945 `window-min-height' lines tall and can accommodate at least one
5946 line plus - if WINDOW has one - a mode line.
5948 WINDOW may be split horizontally when the following conditions
5950 - `window-size-fixed' is either nil or equals `height' for the
5952 - `split-width-threshold' is an integer and WINDOW is at least as
5953 wide as `split-width-threshold'.
5954 - When WINDOW is split evenly, the emanating windows are at least
5955 `window-min-width' or two (whichever is larger) columns wide."
5956 (when (and (window-live-p window
) (not (window--side-window-p window
)))
5957 (with-current-buffer (window-buffer window
)
5959 ;; A window can be split horizontally when its width is not
5960 ;; fixed, it is at least `split-width-threshold' columns wide
5961 ;; and at least twice as wide as `window-min-width' and 2 (the
5962 ;; latter value is hardcoded).
5963 (and (memq window-size-fixed
'(nil height
))
5964 ;; Testing `window-full-width-p' here hardly makes any
5965 ;; sense nowadays. This can be done more intuitively by
5966 ;; setting up `split-width-threshold' appropriately.
5967 (numberp split-width-threshold
)
5968 (>= (window-width window
)
5969 (max split-width-threshold
5970 (* 2 (max window-min-width
2)))))
5971 ;; A window can be split vertically when its height is not
5972 ;; fixed, it is at least `split-height-threshold' lines high,
5973 ;; and it is at least twice as high as `window-min-height' and 2
5974 ;; if it has a mode line or 1.
5975 (and (memq window-size-fixed
'(nil width
))
5976 (numberp split-height-threshold
)
5977 (>= (window-height window
)
5978 (max split-height-threshold
5979 (* 2 (max window-min-height
5980 (if mode-line-format
2 1))))))))))
5982 (defun split-window-sensibly (&optional window
)
5983 "Split WINDOW in a way suitable for `display-buffer'.
5984 WINDOW defaults to the currently selected window.
5985 If `split-height-threshold' specifies an integer, WINDOW is at
5986 least `split-height-threshold' lines tall and can be split
5987 vertically, split WINDOW into two windows one above the other and
5988 return the lower window. Otherwise, if `split-width-threshold'
5989 specifies an integer, WINDOW is at least `split-width-threshold'
5990 columns wide and can be split horizontally, split WINDOW into two
5991 windows side by side and return the window on the right. If this
5992 can't be done either and WINDOW is the only window on its frame,
5993 try to split WINDOW vertically disregarding any value specified
5994 by `split-height-threshold'. If that succeeds, return the lower
5995 window. Return nil otherwise.
5997 By default `display-buffer' routines call this function to split
5998 the largest or least recently used window. To change the default
5999 customize the option `split-window-preferred-function'.
6001 You can enforce this function to not split WINDOW horizontally,
6002 by setting (or binding) the variable `split-width-threshold' to
6003 nil. If, in addition, you set `split-height-threshold' to zero,
6004 chances increase that this function does split WINDOW vertically.
6006 In order to not split WINDOW vertically, set (or bind) the
6007 variable `split-height-threshold' to nil. Additionally, you can
6008 set `split-width-threshold' to zero to make a horizontal split
6009 more likely to occur.
6011 Have a look at the function `window-splittable-p' if you want to
6012 know how `split-window-sensibly' determines whether WINDOW can be
6014 (let ((window (or window
(selected-window))))
6015 (or (and (window-splittable-p window
)
6016 ;; Split window vertically.
6017 (with-selected-window window
6018 (split-window-below)))
6019 (and (window-splittable-p window t
)
6020 ;; Split window horizontally.
6021 (with-selected-window window
6022 (split-window-right)))
6023 (and (eq window
(frame-root-window (window-frame window
)))
6024 (not (window-minibuffer-p window
))
6025 ;; If WINDOW is the only window on its frame and is not the
6026 ;; minibuffer window, try to split it vertically disregarding
6027 ;; the value of `split-height-threshold'.
6028 (let ((split-height-threshold 0))
6029 (when (window-splittable-p window
)
6030 (with-selected-window window
6031 (split-window-below))))))))
6033 (defun window--try-to-split-window (window &optional alist
)
6034 "Try to split WINDOW.
6035 Return value returned by `split-window-preferred-function' if it
6036 represents a live window, nil otherwise."
6037 (and (window-live-p window
)
6038 (not (frame-parameter (window-frame window
) 'unsplittable
))
6039 (let* ((window-combination-limit
6040 ;; When `window-combination-limit' equals
6041 ;; `display-buffer' or equals `resize-window' and a
6042 ;; `window-height' or `window-width' alist entry are
6043 ;; present, bind it to t so resizing steals space
6044 ;; preferably from the window that was split.
6045 (if (or (eq window-combination-limit
'display-buffer
)
6046 (and (eq window-combination-limit
'window-size
)
6047 (or (cdr (assq 'window-height alist
))
6048 (cdr (assq 'window-width alist
)))))
6050 window-combination-limit
))
6052 ;; Since `split-window-preferred-function' might
6053 ;; throw an error use `condition-case'.
6055 (funcall split-window-preferred-function window
)
6057 (and (window-live-p new-window
) new-window
))))
6059 (defun window--frame-usable-p (frame)
6060 "Return FRAME if it can be used to display a buffer."
6061 (when (frame-live-p frame
)
6062 (let ((window (frame-root-window frame
)))
6063 ;; `frame-root-window' may be an internal window which is considered
6064 ;; "dead" by `window-live-p'. Hence if `window' is not live we
6065 ;; implicitly know that `frame' has a visible window we can use.
6066 (unless (and (window-live-p window
)
6067 (or (window-minibuffer-p window
)
6068 ;; If the window is soft-dedicated, the frame is usable.
6069 ;; Actually, even if the window is really dedicated,
6070 ;; the frame is still usable by splitting it.
6071 ;; At least Emacs-22 allowed it, and it is desirable
6072 ;; when displaying same-frame windows.
6073 nil
; (eq t (window-dedicated-p window))
6077 (defcustom even-window-heights t
6078 "If non-nil `display-buffer' will try to even window heights.
6079 Otherwise `display-buffer' will leave the window configuration
6080 alone. Heights are evened only when `display-buffer' chooses a
6081 window that appears above or below the selected window."
6085 (defun window--even-window-heights (window)
6086 "Even heights of WINDOW and selected window.
6087 Do this only if these windows are vertically adjacent to each
6088 other, `even-window-heights' is non-nil, and the selected window
6089 is higher than WINDOW."
6090 (when (and even-window-heights
6091 ;; Even iff WINDOW forms a vertical combination with the
6092 ;; selected window, and WINDOW's height exceeds that of the
6093 ;; selected window, see also bug#11880.
6094 (window-combined-p window
)
6095 (= (window-child-count (window-parent window
)) 2)
6096 (eq (window-parent) (window-parent window
))
6097 (> (window-total-height) (window-total-height window
)))
6098 ;; Don't throw an error if we can't even window heights for
6102 (/ (- (window-total-height window
) (window-total-height)) 2))
6105 (defun window--display-buffer (buffer window type
&optional alist dedicated
)
6106 "Display BUFFER in WINDOW.
6107 TYPE must be one of the symbols `reuse', `window' or `frame' and
6108 is passed unaltered to `display-buffer-record-window'. ALIST is
6109 the alist argument of `display-buffer'. Set `window-dedicated-p'
6110 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
6112 (when (and (buffer-live-p buffer
) (window-live-p window
))
6113 (display-buffer-record-window type window buffer
)
6114 (unless (eq buffer
(window-buffer window
))
6115 (set-window-dedicated-p window nil
)
6116 (set-window-buffer window buffer
)
6118 (set-window-dedicated-p window dedicated
))
6119 (when (memq type
'(window frame
))
6120 (set-window-prev-buffers window nil
)))
6121 (let ((parameter (window-parameter window
'quit-restore
))
6122 (height (cdr (assq 'window-height alist
)))
6123 (width (cdr (assq 'window-width alist
)))
6124 (size (cdr (assq 'window-size alist
)))
6125 (preserve-size (cdr (assq 'preserve-size alist
))))
6127 ((or (eq type
'frame
)
6128 (and (eq (car parameter
) 'same
)
6129 (eq (nth 1 parameter
) 'frame
)))
6130 ;; Adjust size of frame if asked for.
6134 (let ((width (car size
))
6136 (frame (window-frame window
)))
6137 (when (and (numberp width
) (numberp height
))
6139 frame
(+ (frame-height frame
)
6140 (- height
(window-total-height window
))))
6142 frame
(+ (frame-width frame
)
6143 (- width
(window-total-width window
)))))))
6145 (ignore-errors (funcall size window
)))))
6146 ((or (eq type
'window
)
6147 (and (eq (car parameter
) 'same
)
6148 (eq (nth 1 parameter
) 'window
)))
6149 ;; Adjust height of window if asked for.
6154 (if (integerp height
)
6157 (* (window-total-height (frame-root-window window
))
6159 (delta (- new-height
(window-total-height window
))))
6160 (when (and (window--resizable-p window delta nil
'safe
)
6161 (window-combined-p window
))
6162 (window-resize window delta nil
'safe
))))
6164 (ignore-errors (funcall height window
))))
6165 ;; Adjust width of window if asked for.
6170 (if (integerp width
)
6173 (* (window-total-width (frame-root-window window
))
6175 (delta (- new-width
(window-total-width window
))))
6176 (when (and (window--resizable-p window delta t
'safe
)
6177 (window-combined-p window t
))
6178 (window-resize window delta t
'safe
))))
6180 (ignore-errors (funcall width window
))))
6181 ;; Preserve window size if asked for.
6182 (when (consp preserve-size
)
6183 (window-preserve-size window t
(car preserve-size
))
6184 (window-preserve-size window nil
(cdr preserve-size
))))))
6188 (defun window--maybe-raise-frame (frame)
6189 (let ((visible (frame-visible-p frame
)))
6190 (unless (or (not visible
)
6191 ;; Assume the selected frame is already visible enough.
6192 (eq frame
(selected-frame))
6193 ;; Assume the frame from which we invoked the
6194 ;; minibuffer is visible.
6195 (and (minibuffer-window-active-p (selected-window))
6196 (eq frame
(window-frame (minibuffer-selected-window)))))
6197 (raise-frame frame
))))
6199 ;; FIXME: Not implemented.
6200 ;; FIXME: By the way, there could be more levels of dedication:
6201 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
6202 ;; the window hasn't been used for something else yet.
6203 ;; - `soft' (`softly') dedicated only allows reuse when asked explicitly.
6204 ;; - `strongly' never allows reuse.
6205 (defvar display-buffer-mark-dedicated nil
6206 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
6207 The actual non-nil value of this variable will be copied to the
6208 `window-dedicated-p' flag.")
6210 (defconst display-buffer--action-function-custom-type
6211 '(choice :tag
"Function"
6212 (const :tag
"--" ignore
) ; default for insertion
6213 (const display-buffer-reuse-window
)
6214 (const display-buffer-pop-up-window
)
6215 (const display-buffer-same-window
)
6216 (const display-buffer-pop-up-frame
)
6217 (const display-buffer-below-selected
)
6218 (const display-buffer-at-bottom
)
6219 (const display-buffer-in-previous-window
)
6220 (const display-buffer-use-some-window
)
6221 (function :tag
"Other function"))
6222 "Custom type for `display-buffer' action functions.")
6224 (defconst display-buffer--action-custom-type
6225 `(cons :tag
"Action"
6226 (choice :tag
"Action functions"
6227 ,display-buffer--action-function-custom-type
6229 :tag
"List of functions"
6230 ,display-buffer--action-function-custom-type
))
6231 (alist :tag
"Action arguments"
6233 :value-type
(sexp :tag
"Value")))
6234 "Custom type for `display-buffer' actions.")
6236 (defvar display-buffer-overriding-action
'(nil . nil
)
6237 "Overriding action to perform to display a buffer.
6238 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6239 function or a list of functions. Each function should accept two
6240 arguments: a buffer to display and an alist similar to ALIST.
6241 See `display-buffer' for details.")
6242 (put 'display-buffer-overriding-action
'risky-local-variable t
)
6244 (defcustom display-buffer-alist nil
6245 "Alist of conditional actions for `display-buffer'.
6246 This is a list of elements (CONDITION . ACTION), where:
6248 CONDITION is either a regexp matching buffer names, or a
6249 function that takes two arguments - a buffer name and the
6250 ACTION argument of `display-buffer' - and returns a boolean.
6252 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
6253 function or a list of functions. Each such function should
6254 accept two arguments: a buffer to display and an alist of the
6255 same form as ALIST. See `display-buffer' for details.
6257 `display-buffer' scans this alist until it either finds a
6258 matching regular expression or the function specified by a
6259 condition returns non-nil. In any of these cases, it adds the
6260 associated action to the list of actions it will try."
6261 :type
`(alist :key-type
6262 (choice :tag
"Condition"
6264 (function :tag
"Matcher function"))
6265 :value-type
,display-buffer--action-custom-type
)
6270 (defcustom display-buffer-base-action
'(nil . nil
)
6271 "User-specified default action for `display-buffer'.
6272 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6273 function or a list of functions. Each function should accept two
6274 arguments: a buffer to display and an alist similar to ALIST.
6275 See `display-buffer' for details."
6276 :type display-buffer--action-custom-type
6281 (defconst display-buffer-fallback-action
6282 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
6283 display-buffer-reuse-window
6284 display-buffer--maybe-pop-up-frame-or-window
6285 display-buffer-in-previous-window
6286 display-buffer-use-some-window
6287 ;; If all else fails, pop up a new frame.
6288 display-buffer-pop-up-frame
))
6289 "Default fallback action for `display-buffer'.
6290 This is the action used by `display-buffer' if no other actions
6291 specified, e.g. by the user options `display-buffer-alist' or
6292 `display-buffer-base-action'. See `display-buffer'.")
6293 (put 'display-buffer-fallback-action
'risky-local-variable t
)
6295 (defun display-buffer-assq-regexp (buffer-name alist action
)
6296 "Retrieve ALIST entry corresponding to BUFFER-NAME.
6297 ACTION is the action argument passed to `display-buffer'."
6299 (dolist (entry alist
)
6300 (let ((key (car entry
)))
6301 (when (or (and (stringp key
)
6302 (string-match-p key buffer-name
))
6303 (and (functionp key
)
6304 (funcall key buffer-name action
)))
6305 (throw 'match
(cdr entry
)))))))
6307 (defvar display-buffer--same-window-action
6308 '(display-buffer-same-window
6309 (inhibit-same-window . nil
))
6310 "A `display-buffer' action for displaying in the same window.")
6311 (put 'display-buffer--same-window-action
'risky-local-variable t
)
6313 (defvar display-buffer--other-frame-action
6314 '((display-buffer-reuse-window
6315 display-buffer-pop-up-frame
)
6316 (reusable-frames .
0)
6317 (inhibit-same-window . t
))
6318 "A `display-buffer' action for displaying in another frame.")
6319 (put 'display-buffer--other-frame-action
'risky-local-variable t
)
6321 (defun display-buffer (buffer-or-name &optional action frame
)
6322 "Display BUFFER-OR-NAME in some window, without selecting it.
6323 BUFFER-OR-NAME must be a buffer or the name of an existing
6324 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
6325 or nil if no such window is found.
6327 Optional argument ACTION, if non-nil, should specify a display
6328 action. Its form is described below.
6330 Optional argument FRAME, if non-nil, acts like an additional
6331 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
6332 specifying the frame(s) to search for a window that is already
6333 displaying the buffer. See `display-buffer-reuse-window'
6335 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
6336 where FUNCTION is either a function or a list of functions, and
6337 ALIST is an arbitrary association list (alist).
6339 Each such FUNCTION should accept two arguments: the buffer to
6340 display and an alist. Based on those arguments, it should
6341 display the buffer and return the window. If the caller is
6342 prepared to handle the case of not displaying the buffer
6343 and returning nil from `display-buffer' it should pass
6344 \(allow-no-window . t) as an element of the ALIST.
6346 The `display-buffer' function builds a function list and an alist
6347 by combining the functions and alists specified in
6348 `display-buffer-overriding-action', `display-buffer-alist', the
6349 ACTION argument, `display-buffer-base-action', and
6350 `display-buffer-fallback-action' (in order). Then it calls each
6351 function in the combined function list in turn, passing the
6352 buffer as the first argument and the combined alist as the second
6353 argument, until one of the functions returns non-nil.
6355 If ACTION is nil, the function list and the alist are built using
6356 only the other variables mentioned above.
6358 Available action functions include:
6359 `display-buffer-same-window'
6360 `display-buffer-reuse-window'
6361 `display-buffer-pop-up-frame'
6362 `display-buffer-pop-up-window'
6363 `display-buffer-in-previous-window'
6364 `display-buffer-use-some-window'
6366 Recognized alist entries include:
6368 `inhibit-same-window' -- A non-nil value prevents the same
6369 window from being used for display.
6371 `inhibit-switch-frame' -- A non-nil value prevents any other
6372 frame from being raised or selected,
6373 even if the window is displayed there.
6375 `reusable-frames' -- Value specifies frame(s) to search for a
6376 window that already displays the buffer.
6377 See `display-buffer-reuse-window'.
6379 `pop-up-frame-parameters' -- Value specifies an alist of frame
6380 parameters to give a new frame, if
6383 `window-height' -- Value specifies either an integer (the number
6384 of lines of a new window), a floating point number (the
6385 fraction of a new window with respect to the height of the
6386 frame's root window) or a function to be called with one
6387 argument - a new window. The function is supposed to adjust
6388 the height of the window; its return value is ignored.
6389 Suitable functions are `shrink-window-if-larger-than-buffer'
6390 and `fit-window-to-buffer'.
6392 `window-width' -- Value specifies either an integer (the number
6393 of columns of a new window), a floating point number (the
6394 fraction of a new window with respect to the width of the
6395 frame's root window) or a function to be called with one
6396 argument - a new window. The function is supposed to adjust
6397 the width of the window; its return value is ignored.
6399 `allow-no-window' -- A non-nil value indicates readiness for the case
6400 of not displaying the buffer and FUNCTION can safely return
6401 a non-window value to suppress displaying.
6403 `preserve-size' -- Value should be either '(t . nil)' to
6404 preserve the width of the window, '(nil . t)' to preserve its
6405 height or '(t . t)' to preserve both.
6407 The ACTION argument to `display-buffer' can also have a non-nil
6408 and non-list value. This means to display the buffer in a window
6409 other than the selected one, even if it is already displayed in
6410 the selected window. If called interactively with a prefix
6411 argument, ACTION is t."
6412 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6413 (if current-prefix-arg t
)))
6414 (let ((buffer (if (bufferp buffer-or-name
)
6416 (get-buffer buffer-or-name
)))
6417 ;; Make sure that when we split windows the old window keeps
6418 ;; point, bug#14829.
6419 (split-window-keep-point t
)
6420 ;; Handle the old form of the first argument.
6421 (inhibit-same-window (and action
(not (listp action
)))))
6422 (unless (listp action
) (setq action nil
))
6423 (if display-buffer-function
6424 ;; If `display-buffer-function' is defined, let it do the job.
6425 (funcall display-buffer-function buffer inhibit-same-window
)
6426 ;; Otherwise, use the defined actions.
6428 (display-buffer-assq-regexp
6429 (buffer-name buffer
) display-buffer-alist action
))
6430 (special-action (display-buffer--special-action buffer
))
6431 ;; Extra actions from the arguments to this function:
6433 (cons nil
(append (if inhibit-same-window
6434 '((inhibit-same-window . t
)))
6436 `((reusable-frames .
,frame
))))))
6437 ;; Construct action function list and action alist.
6438 (actions (list display-buffer-overriding-action
6439 user-action special-action action extra-action
6440 display-buffer-base-action
6441 display-buffer-fallback-action
))
6442 (functions (apply 'append
6445 (if (functionp x
) (list x
) x
))
6447 (alist (apply 'append
(mapcar 'cdr actions
)))
6449 (unless (buffer-live-p buffer
)
6450 (error "Invalid buffer"))
6451 (while (and functions
(not window
))
6452 (setq window
(funcall (car functions
) buffer alist
)
6453 functions
(cdr functions
)))
6454 (and (windowp window
) window
)))))
6456 (defun display-buffer-other-frame (buffer)
6457 "Display buffer BUFFER preferably in another frame.
6458 This uses the function `display-buffer' as a subroutine; see
6459 its documentation for additional customization information."
6460 (interactive "BDisplay buffer in other frame: ")
6461 (display-buffer buffer display-buffer--other-frame-action t
))
6463 ;;; `display-buffer' action functions:
6465 (defun display-buffer-same-window (buffer alist
)
6466 "Display BUFFER in the selected window.
6467 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
6468 if the selected window is a minibuffer window or is dedicated to
6469 another buffer; in that case, return nil. Otherwise, return the
6471 (unless (or (cdr (assq 'inhibit-same-window alist
))
6472 (window-minibuffer-p)
6473 (window-dedicated-p))
6474 (window--display-buffer buffer
(selected-window) 'reuse alist
)))
6476 (defun display-buffer--maybe-same-window (buffer alist
)
6477 "Conditionally display BUFFER in the selected window.
6478 If `same-window-p' returns non-nil for BUFFER's name, call
6479 `display-buffer-same-window' and return its value. Otherwise,
6481 (and (same-window-p (buffer-name buffer
))
6482 (display-buffer-same-window buffer alist
)))
6484 (defun display-buffer-reuse-window (buffer alist
)
6485 "Return a window that is already displaying BUFFER.
6486 Return nil if no usable window is found.
6488 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6489 window is not eligible for reuse.
6491 If ALIST contains a `reusable-frames' entry, its value determines
6492 which frames to search for a reusable window:
6493 nil -- the selected frame (actually the last non-minibuffer frame)
6494 A frame -- just that frame
6495 `visible' -- all visible frames
6496 0 -- all frames on the current terminal
6499 If ALIST contains no `reusable-frames' entry, search just the
6500 selected frame if `display-buffer-reuse-frames' and
6501 `pop-up-frames' are both nil; search all frames on the current
6502 terminal if either of those variables is non-nil.
6504 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6505 event that a window on another frame is chosen, avoid raising
6507 (let* ((alist-entry (assq 'reusable-frames alist
))
6508 (frames (cond (alist-entry (cdr alist-entry
))
6509 ((if (eq pop-up-frames
'graphic-only
)
6513 (display-buffer-reuse-frames 0)
6514 (t (last-nonminibuffer-frame))))
6515 (window (if (and (eq buffer
(window-buffer))
6516 (not (cdr (assq 'inhibit-same-window alist
))))
6518 (car (delq (selected-window)
6519 (get-buffer-window-list buffer
'nomini
6521 (when (window-live-p window
)
6522 (prog1 (window--display-buffer buffer window
'reuse alist
)
6523 (unless (cdr (assq 'inhibit-switch-frame alist
))
6524 (window--maybe-raise-frame (window-frame window
)))))))
6526 (defun display-buffer--special-action (buffer)
6527 "Return special display action for BUFFER, if any.
6528 If `special-display-p' returns non-nil for BUFFER, return an
6529 appropriate display action involving `special-display-function'.
6530 See `display-buffer' for the format of display actions."
6531 (and special-display-function
6532 ;; `special-display-p' returns either t or a list of frame
6533 ;; parameters to pass to `special-display-function'.
6534 (let ((pars (special-display-p (buffer-name buffer
))))
6536 (list (list #'display-buffer-reuse-window
6537 `(lambda (buffer _alist
)
6538 (funcall special-display-function
6539 buffer
',(if (listp pars
) pars
)))))))))
6541 (defun display-buffer-pop-up-frame (buffer alist
)
6542 "Display BUFFER in a new frame.
6543 This works by calling `pop-up-frame-function'. If successful,
6544 return the window used; otherwise return nil.
6546 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
6547 raising the new frame.
6549 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
6550 corresponding value is an alist of frame parameters to give the
6552 (let* ((params (cdr (assq 'pop-up-frame-parameters alist
)))
6553 (pop-up-frame-alist (append params pop-up-frame-alist
))
6554 (fun pop-up-frame-function
)
6557 ;; Make BUFFER current so `make-frame' will use it as the
6558 ;; new frame's buffer (Bug#15133).
6559 (with-current-buffer buffer
6560 (setq frame
(funcall fun
)))
6561 (setq window
(frame-selected-window frame
)))
6562 (prog1 (window--display-buffer
6563 buffer window
'frame alist display-buffer-mark-dedicated
)
6564 (unless (cdr (assq 'inhibit-switch-frame alist
))
6565 (window--maybe-raise-frame frame
))))))
6567 (defun display-buffer-pop-up-window (buffer alist
)
6568 "Display BUFFER by popping up a new window.
6569 The new window is created on the selected frame, or in
6570 `last-nonminibuffer-frame' if no windows can be created there.
6571 If successful, return the new window; otherwise return nil.
6573 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6574 event that the new window is created on another frame, avoid
6576 (let ((frame (or (window--frame-usable-p (selected-frame))
6577 (window--frame-usable-p (last-nonminibuffer-frame))))
6579 (when (and (or (not (frame-parameter frame
'unsplittable
))
6580 ;; If the selected frame cannot be split, look at
6581 ;; `last-nonminibuffer-frame'.
6582 (and (eq frame
(selected-frame))
6583 (setq frame
(last-nonminibuffer-frame))
6584 (window--frame-usable-p frame
)
6585 (not (frame-parameter frame
'unsplittable
))))
6586 ;; Attempt to split largest or least recently used window.
6587 (setq window
(or (window--try-to-split-window
6588 (get-largest-window frame t
) alist
)
6589 (window--try-to-split-window
6590 (get-lru-window frame t
) alist
))))
6591 (prog1 (window--display-buffer
6592 buffer window
'window alist display-buffer-mark-dedicated
)
6593 (unless (cdr (assq 'inhibit-switch-frame alist
))
6594 (window--maybe-raise-frame (window-frame window
)))))))
6596 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist
)
6597 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
6599 If `pop-up-frames' is non-nil (and not `graphic-only' on a
6600 text-only terminal), try with `display-buffer-pop-up-frame'.
6602 If that cannot be done, and `pop-up-windows' is non-nil, try
6603 again with `display-buffer-pop-up-window'."
6604 (or (and (if (eq pop-up-frames
'graphic-only
)
6607 (display-buffer-pop-up-frame buffer alist
))
6609 (display-buffer-pop-up-window buffer alist
))))
6611 (defun display-buffer-below-selected (buffer alist
)
6612 "Try displaying BUFFER in a window below the selected window.
6613 This either splits the selected window or reuses the window below
6616 (or (and (setq window
(window-in-direction 'below
))
6617 (eq buffer
(window-buffer window
))
6618 (window--display-buffer buffer window
'reuse alist
))
6619 (and (not (frame-parameter nil
'unsplittable
))
6620 (let ((split-height-threshold 0)
6621 split-width-threshold
)
6622 (setq window
(window--try-to-split-window (selected-window) alist
)))
6623 (window--display-buffer
6624 buffer window
'window alist display-buffer-mark-dedicated
))
6625 (and (setq window
(window-in-direction 'below
))
6626 (not (window-dedicated-p window
))
6627 (window--display-buffer
6628 buffer window
'reuse alist display-buffer-mark-dedicated
)))))
6630 (defun display-buffer-at-bottom (buffer alist
)
6631 "Try displaying BUFFER in a window at the bottom of the selected frame.
6632 This either reuses such a window provided it shows BUFFER
6633 already, splits a window at the bottom of the frame or the
6634 frame's root window, or reuses some window at the bottom of the
6636 (let (bottom-window bottom-window-shows-buffer window
)
6640 ((window-in-direction 'below window
))
6641 ((and (not bottom-window-shows-buffer
)
6642 (eq buffer
(window-buffer window
)))
6643 (setq bottom-window-shows-buffer t
)
6644 (setq bottom-window window
))
6645 ((not bottom-window
)
6646 (setq bottom-window window
)))
6648 (or (and bottom-window-shows-buffer
6649 (window--display-buffer
6650 buffer bottom-window
'reuse alist display-buffer-mark-dedicated
))
6651 (and (not (frame-parameter nil
'unsplittable
))
6652 (let (split-width-threshold)
6653 (setq window
(window--try-to-split-window bottom-window alist
)))
6654 (window--display-buffer
6655 buffer window
'window alist display-buffer-mark-dedicated
))
6656 (and (not (frame-parameter nil
'unsplittable
))
6659 (split-window (window--major-non-side-window))
6661 (window--display-buffer
6662 buffer window
'window alist display-buffer-mark-dedicated
))
6663 (and (setq window bottom-window
)
6664 (not (window-dedicated-p window
))
6665 (window--display-buffer
6666 buffer window
'reuse alist display-buffer-mark-dedicated
)))))
6668 (defun display-buffer-in-previous-window (buffer alist
)
6669 "Display BUFFER in a window previously showing it.
6670 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6671 window is not eligible for reuse.
6673 If ALIST contains a `reusable-frames' entry, its value determines
6674 which frames to search for a reusable window:
6675 nil -- the selected frame (actually the last non-minibuffer frame)
6676 A frame -- just that frame
6677 `visible' -- all visible frames
6678 0 -- all frames on the current terminal
6681 If ALIST contains no `reusable-frames' entry, search just the
6682 selected frame if `display-buffer-reuse-frames' and
6683 `pop-up-frames' are both nil; search all frames on the current
6684 terminal if either of those variables is non-nil.
6686 If ALIST has a `previous-window' entry, the window specified by
6687 that entry will override any other window found by the methods
6688 above, even if that window never showed BUFFER before."
6689 (let* ((alist-entry (assq 'reusable-frames alist
))
6690 (inhibit-same-window
6691 (cdr (assq 'inhibit-same-window alist
)))
6693 (alist-entry (cdr alist-entry
))
6694 ((if (eq pop-up-frames
'graphic-only
)
6698 (display-buffer-reuse-frames 0)
6699 (t (last-nonminibuffer-frame))))
6700 best-window second-best-window window
)
6701 ;; Scan windows whether they have shown the buffer recently.
6703 (dolist (window (window-list-1 (frame-first-window) 'nomini frames
))
6704 (when (and (assq buffer
(window-prev-buffers window
))
6705 (not (window-dedicated-p window
)))
6706 (if (eq window
(selected-window))
6707 (unless inhibit-same-window
6708 (setq second-best-window window
))
6709 (setq best-window window
)
6711 ;; When ALIST has a `previous-window' entry, that entry may override
6712 ;; anything we found so far.
6713 (when (and (setq window
(cdr (assq 'previous-window alist
)))
6714 (window-live-p window
)
6715 (not (window-dedicated-p window
)))
6716 (if (eq window
(selected-window))
6717 (unless inhibit-same-window
6718 (setq second-best-window window
))
6719 (setq best-window window
)))
6720 ;; Return best or second best window found.
6721 (when (setq window
(or best-window second-best-window
))
6722 (window--display-buffer buffer window
'reuse alist
))))
6724 (defun display-buffer-use-some-window (buffer alist
)
6725 "Display BUFFER in an existing window.
6726 Search for a usable window, set that window to the buffer, and
6727 return the window. If no suitable window is found, return nil.
6729 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6730 event that a window in another frame is chosen, avoid raising
6732 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist
)))
6733 (frame (or (window--frame-usable-p (selected-frame))
6734 (window--frame-usable-p (last-nonminibuffer-frame))))
6736 ;; Reuse an existing window.
6737 (or (get-lru-window frame nil not-this-window
)
6738 (let ((window (get-buffer-window buffer
'visible
)))
6739 (unless (and not-this-window
6740 (eq window
(selected-window)))
6742 (get-largest-window 'visible nil not-this-window
)
6743 (let ((window (get-buffer-window buffer
0)))
6744 (unless (and not-this-window
6745 (eq window
(selected-window)))
6747 (get-largest-window 0 nil not-this-window
)))
6748 (quit-restore (and (window-live-p window
)
6749 (window-parameter window
'quit-restore
)))
6750 (quad (nth 1 quit-restore
)))
6751 (when (window-live-p window
)
6752 ;; If the window was used by `display-buffer' before, try to
6753 ;; resize it to its old height but don't signal an error.
6754 (when (and (listp quad
)
6755 (integerp (nth 3 quad
))
6756 (> (nth 3 quad
) (window-total-height window
)))
6758 (window-resize window
(- (nth 3 quad
) (window-total-height window
)))
6762 (window--display-buffer buffer window
'reuse alist
)
6763 (window--even-window-heights window
)
6764 (unless (cdr (assq 'inhibit-switch-frame alist
))
6765 (window--maybe-raise-frame (window-frame window
)))))))
6767 (defun display-buffer-no-window (_buffer alist
)
6768 "Display BUFFER in no window.
6769 If ALIST has a non-nil `allow-no-window' entry, then don't display
6770 a window at all. This makes possible to override the default action
6771 and avoid displaying the buffer. It is assumed that when the caller
6772 specifies a non-nil `allow-no-window' then it can handle a nil value
6773 returned from `display-buffer' in this case."
6774 (when (cdr (assq 'allow-no-window alist
))
6777 ;;; Display + selection commands:
6778 (defun pop-to-buffer (buffer &optional action norecord
)
6779 "Select buffer BUFFER in some window, preferably a different one.
6780 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6781 is a string not naming an existent buffer, create a buffer with
6782 that name. If BUFFER is nil, choose some other buffer. Return
6785 This uses `display-buffer' as a subroutine. The optional ACTION
6786 argument is passed to `display-buffer' as its ACTION argument.
6787 See `display-buffer' for more information. ACTION is t if called
6788 interactively with a prefix argument, which means to pop to a
6789 window other than the selected one even if the buffer is already
6790 displayed in the selected window.
6792 If the window to show BUFFER is not on the selected
6793 frame, raise that window's frame and give it input focus.
6795 Optional third arg NORECORD non-nil means do not put this buffer
6796 at the front of the list of recently selected ones."
6797 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
6798 (if current-prefix-arg t
)))
6799 (setq buffer
(window-normalize-buffer-to-switch-to buffer
))
6800 ;; This should be done by `select-window' below.
6801 ;; (set-buffer buffer)
6802 (let* ((old-frame (selected-frame))
6803 (window (display-buffer buffer action
))
6804 (frame (window-frame window
)))
6805 ;; If we chose another frame, make sure it gets input focus.
6806 (unless (eq frame old-frame
)
6807 (select-frame-set-input-focus frame norecord
))
6808 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
6809 (select-window window norecord
)
6812 (defun pop-to-buffer-same-window (buffer &optional norecord
)
6813 "Select buffer BUFFER in some window, preferably the same one.
6814 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6815 is a string not naming an existent buffer, create a buffer with
6816 that name. If BUFFER is nil, choose some other buffer. Return
6819 Optional argument NORECORD, if non-nil means do not put this
6820 buffer at the front of the list of recently selected ones.
6822 Unlike `pop-to-buffer', this function prefers using the selected
6823 window over popping up a new window or frame."
6824 (pop-to-buffer buffer display-buffer--same-window-action norecord
))
6826 (defun read-buffer-to-switch (prompt)
6827 "Read the name of a buffer to switch to, prompting with PROMPT.
6828 Return the name of the buffer as a string.
6830 This function is intended for the `switch-to-buffer' family of
6831 commands since these need to omit the name of the current buffer
6832 from the list of completions and default values."
6833 (let ((rbts-completion-table (internal-complete-buffer-except)))
6834 (minibuffer-with-setup-hook
6836 (setq minibuffer-completion-table rbts-completion-table
)
6837 ;; Since rbts-completion-table is built dynamically, we
6838 ;; can't just add it to the default value of
6839 ;; icomplete-with-completion-tables, so we add it
6841 (if (and (boundp 'icomplete-with-completion-tables
)
6842 (listp icomplete-with-completion-tables
))
6843 (set (make-local-variable 'icomplete-with-completion-tables
)
6844 (cons rbts-completion-table
6845 icomplete-with-completion-tables
))))
6846 (read-buffer prompt
(other-buffer (current-buffer))
6847 (confirm-nonexistent-file-or-buffer)))))
6849 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
6850 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
6851 If BUFFER-OR-NAME is nil, return the buffer returned by
6852 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
6853 exists, return that buffer. If no such buffer exists, create a
6854 buffer with the name BUFFER-OR-NAME and return that buffer."
6856 (or (get-buffer buffer-or-name
)
6857 (let ((buffer (get-buffer-create buffer-or-name
)))
6858 (set-buffer-major-mode buffer
)
6862 (defcustom switch-to-buffer-preserve-window-point nil
6863 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
6864 If this is nil, `switch-to-buffer' displays the buffer at that
6865 buffer's `point'. If this is `already-displayed', it tries to
6866 display the buffer at its previous position in the selected
6867 window, provided the buffer is currently displayed in some other
6868 window on any visible or iconified frame. If this is t, it
6869 unconditionally tries to display the buffer at its previous
6870 position in the selected window.
6872 This variable is ignored if the buffer is already displayed in
6873 the selected window or never appeared in it before, or if
6874 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
6876 (const :tag
"Never" nil
)
6877 (const :tag
"If already displayed elsewhere" already-displayed
)
6878 (const :tag
"Always" t
))
6882 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window
)
6883 "Display buffer BUFFER-OR-NAME in the selected window.
6885 WARNING: This is NOT the way to work on another buffer temporarily
6886 within a Lisp program! Use `set-buffer' instead. That avoids
6887 messing with the window-buffer correspondences.
6889 If the selected window cannot display the specified
6890 buffer (e.g. if it is a minibuffer window or strongly dedicated
6891 to another buffer), call `pop-to-buffer' to select the buffer in
6894 If called interactively, read the buffer name using the
6895 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6896 determines whether to request confirmation before creating a new
6899 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
6900 If BUFFER-OR-NAME is a string that does not identify an existing
6901 buffer, create a buffer with that name. If BUFFER-OR-NAME is
6902 nil, switch to the buffer returned by `other-buffer'.
6904 If optional argument NORECORD is non-nil, do not put the buffer
6905 at the front of the buffer list, and do not make the window
6906 displaying it the most recently selected one.
6908 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
6909 must be displayed in the selected window; if that is impossible,
6910 signal an error rather than calling `pop-to-buffer'.
6912 The option `switch-to-buffer-preserve-window-point' can be used
6913 to make the buffer appear at its last position in the selected
6916 Return the buffer switched to."
6918 (list (read-buffer-to-switch "Switch to buffer: ") nil
'force-same-window
))
6919 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name
)))
6921 ;; Don't call set-window-buffer if it's not needed since it
6922 ;; might signal an error (e.g. if the window is dedicated).
6923 ((eq buffer
(window-buffer)))
6924 ((window-minibuffer-p)
6925 (if force-same-window
6926 (user-error "Cannot switch buffers in minibuffer window")
6927 (pop-to-buffer buffer norecord
)))
6928 ((eq (window-dedicated-p) t
)
6929 (if force-same-window
6930 (user-error "Cannot switch buffers in a dedicated window")
6931 (pop-to-buffer buffer norecord
)))
6933 (let* ((entry (assq buffer
(window-prev-buffers)))
6934 (displayed (and (eq switch-to-buffer-preserve-window-point
6936 (get-buffer-window buffer
0))))
6937 (set-window-buffer nil buffer
)
6939 (or (eq switch-to-buffer-preserve-window-point t
)
6941 ;; Try to restore start and point of buffer in the selected
6942 ;; window (Bug#4041).
6943 (set-window-start (selected-window) (nth 1 entry
) t
)
6944 (set-window-point nil
(nth 2 entry
))))))
6947 (select-window (selected-window)))
6948 (set-buffer buffer
)))
6950 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord
)
6951 "Select the buffer specified by BUFFER-OR-NAME in another window.
6952 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6953 nil. Return the buffer switched to.
6955 If called interactively, prompt for the buffer name using the
6956 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6957 determines whether to request confirmation before creating a new
6960 If BUFFER-OR-NAME is a string and does not identify an existing
6961 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6962 nil, switch to the buffer returned by `other-buffer'.
6964 Optional second argument NORECORD non-nil means do not put this
6965 buffer at the front of the list of recently selected ones.
6967 This uses the function `display-buffer' as a subroutine; see its
6968 documentation for additional customization information."
6970 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
6971 (let ((pop-up-windows t
))
6972 (pop-to-buffer buffer-or-name t norecord
)))
6974 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord
)
6975 "Switch to buffer BUFFER-OR-NAME in another frame.
6976 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6977 nil. Return the buffer switched to.
6979 If called interactively, prompt for the buffer name using the
6980 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6981 determines whether to request confirmation before creating a new
6984 If BUFFER-OR-NAME is a string and does not identify an existing
6985 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6986 nil, switch to the buffer returned by `other-buffer'.
6988 Optional second arg NORECORD non-nil means do not put this
6989 buffer at the front of the list of recently selected ones.
6991 This uses the function `display-buffer' as a subroutine; see its
6992 documentation for additional customization information."
6994 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
6995 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord
))
6997 (defun set-window-text-height (window height
)
6998 "Set the height in lines of the text display area of WINDOW to HEIGHT.
6999 WINDOW must be a live window and defaults to the selected one.
7000 HEIGHT doesn't include the mode line or header line, if any, or
7001 any partial-height lines in the text display area.
7003 Note that the current implementation of this function cannot
7004 always set the height exactly, but attempts to be conservative,
7005 by allocating more lines than are actually needed in the case
7006 where some error may be present."
7007 (setq window
(window-normalize-window window t
))
7008 (let ((delta (- height
(window-text-height window
))))
7009 (unless (zerop delta
)
7010 ;; Setting window-min-height to a value like 1 can lead to very
7011 ;; bizarre displays because it also allows Emacs to make *other*
7012 ;; windows one line tall, which means that there's no more space
7013 ;; for the mode line.
7014 (let ((window-min-height (min 2 height
)))
7015 (window-resize window delta
)))))
7017 (defun enlarge-window-horizontally (delta)
7018 "Make selected window DELTA columns wider.
7019 Interactively, if no argument is given, make selected window one
7022 (enlarge-window delta t
))
7024 (defun shrink-window-horizontally (delta)
7025 "Make selected window DELTA columns narrower.
7026 Interactively, if no argument is given, make selected window one
7029 (shrink-window delta t
))
7031 (defun count-screen-lines (&optional beg end count-final-newline window
)
7032 "Return the number of screen lines in the region.
7033 The number of screen lines may be different from the number of actual lines,
7034 due to line breaking, display table, etc.
7036 Optional arguments BEG and END default to `point-min' and `point-max'
7039 If region ends with a newline, ignore it unless optional third argument
7040 COUNT-FINAL-NEWLINE is non-nil.
7042 The optional fourth argument WINDOW specifies the window used for obtaining
7043 parameters such as width, horizontal scrolling, and so on. The default is
7044 to use the selected window's parameters.
7046 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
7047 regardless of which buffer is displayed in WINDOW. This makes possible to use
7048 `count-screen-lines' in any buffer, whether or not it is currently displayed
7051 (setq beg
(point-min)))
7053 (setq end
(point-max)))
7059 (narrow-to-region (min beg end
)
7060 (if (and (not count-final-newline
)
7061 (= ?
\n (char-before (max beg end
))))
7064 (goto-char (point-min))
7065 (1+ (vertical-motion (buffer-size) window
))))))
7067 (defun window-buffer-height (window)
7068 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
7069 WINDOW must be a live window and defaults to the selected one."
7070 (setq window
(window-normalize-window window t
))
7071 (with-current-buffer (window-buffer window
)
7073 (count-screen-lines (point-min) (point-max)
7074 ;; If buffer ends with a newline, ignore it when
7075 ;; counting height unless point is after it.
7079 ;;; Resizing windows and frames to fit their contents exactly.
7080 (defcustom fit-window-to-buffer-horizontally nil
7081 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
7082 If this is nil, `fit-window-to-buffer' never resizes windows
7083 horizontally. If this is `only', it can resize windows
7084 horizontally only. Any other value means `fit-window-to-buffer'
7085 can resize windows in both dimensions."
7090 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
7091 ;; counting title bar and outer borders.
7092 (defcustom fit-frame-to-buffer nil
7093 "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
7094 A frame is fit if and only if its root window is a live window
7095 and this option is non-nil. If this is `horizontally', frames
7096 are resized horizontally only. If this is `vertically', frames
7097 are resized vertically only. Any other non-nil value means
7098 frames can be resized in both dimensions."
7103 (defcustom fit-frame-to-buffer-margins
'(nil nil nil nil
)
7104 "Margins around frame for `fit-frame-to-buffer'.
7105 This specifies the numbers of pixels to be left free on the left,
7106 above, on the right, and below a frame fitted to its buffer. Set
7107 this to avoid obscuring other desktop objects like the taskbar.
7108 The default is nil for each side, which means to not add margins.
7110 The value specified here can be overridden for a specific frame
7111 by that frame's `fit-frame-to-buffer-margins' parameter, if
7112 present. See also `fit-frame-to-buffer-sizes'."
7118 :format
"%[LeftMargin%] %v "
7119 (const :tag
"None" :format
"%t" nil
)
7120 (integer :tag
"Pixels" :size
5))
7124 :format
"%[TopMargin%] %v "
7125 (const :tag
"None" :format
"%t" nil
)
7126 (integer :tag
"Pixels" :size
5))
7130 :format
"%[RightMargin%] %v "
7131 (const :tag
"None" :format
"%t" nil
)
7132 (integer :tag
"Pixels" :size
5))
7136 :format
"%[BottomMargin%] %v "
7137 (const :tag
"None" :format
"%t" nil
)
7138 (integer :tag
"Pixels" :size
5)))
7141 (defcustom fit-frame-to-buffer-sizes
'(nil nil nil nil
)
7142 "Size boundaries of frame for `fit-frame-to-buffer'.
7143 This list specifies the total maximum and minimum lines and
7144 maximum and minimum columns of the root window of any frame that
7145 shall be fit to its buffer. If any of these values is non-nil,
7146 it overrides the corresponding argument of `fit-frame-to-buffer'.
7148 On window systems where the menubar can wrap, fitting a frame to
7149 its buffer may swallow the last line(s). Specifying an
7150 appropriate minimum width value here can avoid such wrapping.
7152 See also `fit-frame-to-buffer-margins'."
7156 :tag
"Maximum Height"
7158 :format
"%[MaxHeight%] %v "
7159 (const :tag
"None" :format
"%t" nil
)
7160 (integer :tag
"Lines" :size
5))
7162 :tag
"Minimum Height"
7164 :format
"%[MinHeight%] %v "
7165 (const :tag
"None" :format
"%t" nil
)
7166 (integer :tag
"Lines" :size
5))
7168 :tag
"Maximum Width"
7170 :format
"%[MaxWidth%] %v "
7171 (const :tag
"None" :format
"%t" nil
)
7172 (integer :tag
"Columns" :size
5))
7174 :tag
"Minimum Width"
7176 :format
"%[MinWidth%] %v\n"
7177 (const :tag
"None" :format
"%t" nil
)
7178 (integer :tag
"Columns" :size
5)))
7181 (declare-function x-display-pixel-height
"xfns.c" (&optional terminal
))
7183 (defun window--sanitize-margin (margin left right
)
7184 "Return MARGIN if it's a number between LEFT and RIGHT."
7185 (when (and (numberp margin
)
7186 (<= left
(- right margin
)) (<= margin right
))
7189 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only
)
7190 "Adjust size of FRAME to display the contents of its buffer exactly.
7191 FRAME can be any live frame and defaults to the selected one.
7192 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
7193 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
7194 FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
7195 `window-min-height' and `window-min-width' respectively.
7197 If the optional argument ONLY is `vertically', resize the frame
7198 vertically only. If ONLY is `horizontally', resize the frame
7201 The new position and size of FRAME can be additionally determined
7202 by customizing the options `fit-frame-to-buffer-sizes' and
7203 `fit-frame-to-buffer-margins' or the corresponding parameters of
7206 (unless (and (fboundp 'x-display-pixel-height
)
7207 ;; We need the respective sizes now.
7208 (fboundp 'display-monitor-attributes-list
))
7209 (user-error "Cannot resize frame in non-graphic Emacs"))
7210 (setq frame
(window-normalize-frame frame
))
7211 (when (window-live-p (frame-root-window frame
))
7212 (with-selected-window (frame-root-window frame
)
7213 (let* ((window (frame-root-window frame
))
7214 (char-width (frame-char-width))
7215 (char-height (frame-char-height))
7216 (monitor-attributes (car (display-monitor-attributes-list
7217 (frame-parameter frame
'display
))))
7218 (geometry (cdr (assq 'geometry monitor-attributes
)))
7219 (display-width (- (nth 2 geometry
) (nth 0 geometry
)))
7220 (display-height (- (nth 3 geometry
) (nth 1 geometry
)))
7221 (workarea (cdr (assq 'workarea monitor-attributes
)))
7223 (margins (or (frame-parameter frame
'fit-frame-to-buffer-margins
)
7224 fit-frame-to-buffer-margins
))
7225 (left-margin (if (nth 0 margins
)
7226 (or (window--sanitize-margin
7227 (nth 0 margins
) 0 display-width
)
7230 (top-margin (if (nth 1 margins
)
7231 (or (window--sanitize-margin
7232 (nth 1 margins
) 0 display-height
)
7235 (workarea-width (nth 2 workarea
))
7236 (right-margin (if (nth 2 margins
)
7238 (or (window--sanitize-margin
7239 (nth 2 margins
) left-margin display-width
)
7242 (workarea-height (nth 3 workarea
))
7243 (bottom-margin (if (nth 3 margins
)
7245 (or (window--sanitize-margin
7246 (nth 3 margins
) top-margin display-height
)
7249 ;; The pixel width of FRAME (which does not include the
7250 ;; window manager's decorations).
7251 (frame-width (frame-pixel-width))
7252 ;; The pixel width of the body of FRAME's root window.
7253 (window-body-width (window-body-width nil t
))
7254 ;; The difference in pixels between total and body width of
7256 (window-extra-width (- (window-pixel-width) window-body-width
))
7257 ;; The difference in pixels between the frame's pixel width
7258 ;; and the window's body width. This is the space we can't
7260 (extra-width (- frame-width window-body-width
))
7261 ;; The maximum width we can use for fitting.
7262 (fit-width (- workarea-width extra-width
))
7263 ;; The pixel position of FRAME's left border. We usually
7264 ;; try to leave this alone.
7266 (let ((left (frame-parameter nil
'left
)))
7268 (funcall (car left
) (cadr left
))
7270 ;; The pixel height of FRAME (which does not include title
7271 ;; line, decorations, and sometimes neither the menu nor
7273 (frame-height (frame-pixel-height))
7274 ;; The pixel height of FRAME's root window (we don't care
7275 ;; about the window's body height since the return value of
7276 ;; `window-text-pixel-size' includes header and mode line).
7277 (window-height (window-pixel-height))
7278 ;; The difference in pixels between the frame's pixel
7279 ;; height and the window's height.
7280 (extra-height (- frame-height window-height
))
7281 ;; When tool-bar-mode is enabled and we just created a new
7282 ;; frame, reserve lines for toolbar resizing. Needed
7283 ;; because for reasons unknown to me Emacs (1) reserves one
7284 ;; line for the toolbar when making the initial frame and
7285 ;; toolbars are enabled, and (2) later adds the remaining
7286 ;; lines needed. Our code runs IN BETWEEN (1) and (2).
7287 ;; YMMV when you're on a system that behaves differently.
7288 (toolbar-extra-height
7289 (let ((quit-restore (window-parameter window
'quit-restore
))
7290 ;; This may have to change when we allow arbitrary
7291 ;; pixel height toolbars.
7292 (lines (tool-bar-height)))
7294 (if (and quit-restore
(eq (car quit-restore
) 'frame
)
7295 (not (zerop lines
)))
7298 ;; The pixel position of FRAME's top border.
7300 (let ((top (frame-parameter nil
'top
)))
7302 (funcall (car top
) (cadr top
))
7304 ;; Sanitize minimum and maximum sizes.
7305 (sizes (or (frame-parameter frame
'fit-frame-to-buffer-sizes
)
7306 fit-frame-to-buffer-sizes
))
7309 ((numberp (nth 0 sizes
)) (* (nth 0 sizes
) char-height
))
7310 ((numberp max-height
) (* max-height char-height
))
7311 (t display-height
)))
7314 ((numberp (nth 1 sizes
)) (* (nth 1 sizes
) char-height
))
7315 ((numberp min-height
) (* min-height char-height
))
7316 (t (* window-min-height char-height
))))
7319 ((numberp (nth 2 sizes
))
7320 (- (* (nth 2 sizes
) char-width
) window-extra-width
))
7321 ((numberp max-width
)
7322 (- (* max-width char-width
) window-extra-width
))
7326 ((numberp (nth 3 sizes
))
7327 (- (* (nth 3 sizes
) char-width
) window-extra-width
))
7328 ((numberp min-width
)
7329 (- (* min-width char-width
) window-extra-width
))
7330 (t (* window-min-width char-width
))))
7331 ;; Note: Currently, for a new frame the sizes of the header
7332 ;; and mode line may be estimated incorrectly
7333 (value (window-text-pixel-size
7334 nil t t workarea-width workarea-height t
))
7335 (width (+ (car value
) (window-right-divider-width)))
7338 (window-bottom-divider-width)
7339 (window-scroll-bar-height))))
7340 ;; Don't change height or width when the window's size is fixed
7341 ;; in either direction or ONLY forbids it.
7343 ((or (eq window-size-fixed
'width
) (eq only
'vertically
))
7345 ((or (eq window-size-fixed
'height
) (eq only
'horizontally
))
7347 ;; Fit width to constraints.
7349 (unless frame-resize-pixelwise
7350 ;; Round to character sizes.
7351 (setq width
(* (/ (+ width char-width -
1) char-width
)
7353 ;; Fit to maximum and minimum widths.
7354 (setq width
(max (min width max-width
) min-width
))
7356 (setq width
(+ width extra-width
))
7357 ;; Preserve margins.
7358 (let ((right (+ left width
)))
7360 ((> right right-margin
)
7361 ;; Move frame to left (we don't know its real width).
7362 (setq left
(max left-margin
(- left
(- right right-margin
)))))
7363 ((< left left-margin
)
7364 ;; Move frame to right.
7365 (setq left left-margin
)))))
7366 ;; Fit height to constraints.
7368 (unless frame-resize-pixelwise
7369 (setq height
(* (/ (+ height char-height -
1) char-height
)
7371 ;; Fit to maximum and minimum heights.
7372 (setq height
(max (min height max-height
) min-height
))
7373 ;; Add extra height.
7374 (setq height
(+ height extra-height
))
7375 ;; Preserve margins.
7376 (let ((bottom (+ top height
)))
7378 ((> bottom bottom-margin
)
7379 ;; Move frame up (we don't know its real height).
7380 (setq top
(max top-margin
(- top
(- bottom bottom-margin
)))))
7383 (setq top top-margin
)))))
7385 (set-frame-position frame left top
)
7386 ;; Clumsily try to translate our calculations to what
7387 ;; `set-frame-size' wants.
7389 (setq width
(- (+ (frame-text-width) width
)
7390 extra-width window-body-width
)))
7392 (setq height
(- (+ (frame-text-height) height
)
7393 extra-height window-height
)))
7397 (if frame-resize-pixelwise
7399 (/ width char-width
))
7402 (if frame-resize-pixelwise
7404 (/ height char-height
))
7405 (frame-text-height))
7406 frame-resize-pixelwise
)))))
7408 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width preserve-size
)
7409 "Adjust size of WINDOW to display its buffer's contents exactly.
7410 WINDOW must be a live window and defaults to the selected one.
7412 If WINDOW is part of a vertical combination, adjust WINDOW's
7413 height. The new height is calculated from the actual height of
7414 the accessible portion of its buffer. The optional argument
7415 MAX-HEIGHT specifies a maximum height and defaults to the height
7416 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
7417 minimum height and defaults to `window-min-height'. Both
7418 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
7419 and header line and a bottom divider, if any.
7421 If WINDOW is part of a horizontal combination and the value of
7422 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
7423 WINDOW's width. The new width of WINDOW is calculated from the
7424 maximum length of its buffer's lines that follow the current
7425 start position of WINDOW. The optional argument MAX-WIDTH
7426 specifies a maximum width and defaults to the width of WINDOW's
7427 frame. The optional argument MIN-WIDTH specifies a minimum width
7428 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
7429 are specified in columns and include fringes, margins, a
7430 scrollbar and a vertical divider, if any.
7432 If the optional argument `preserve-size' is non-nil, preserve the
7433 size of WINDOW (see `window-preserve-size').
7435 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
7436 If WINDOW is its frame's root window and the option
7437 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
7438 adjust the frame's size.
7440 Note that even if this function makes WINDOW large enough to show
7441 _all_ parts of its buffer you might not see the first part when
7442 WINDOW was scrolled. If WINDOW is resized horizontally, you will
7443 not see the top of its buffer unless WINDOW starts at its minimum
7444 accessible position."
7446 (setq window
(window-normalize-window window t
))
7447 (if (eq window
(frame-root-window window
))
7448 (when fit-frame-to-buffer
7449 ;; Fit WINDOW's frame to buffer.
7450 (fit-frame-to-buffer
7451 (window-frame window
)
7452 max-height min-height max-width min-width
7453 (and (memq fit-frame-to-buffer
'(vertically horizontally
))
7454 fit-frame-to-buffer
)))
7455 (with-selected-window window
7456 (let* ((pixelwise window-resize-pixelwise
)
7457 (char-height (frame-char-height))
7458 (char-width (frame-char-width))
7459 (total-height (window-size window nil pixelwise
))
7460 (body-height (window-body-height window pixelwise
))
7461 (body-width (window-body-width window pixelwise
))
7463 ;; Sanitize MIN-HEIGHT.
7464 (if (numberp min-height
)
7465 ;; Can't get smaller than `window-safe-min-height'.
7467 (* char-height min-height
)
7470 (window-safe-min-pixel-height window
)
7471 window-safe-min-height
))
7472 ;; Preserve header and mode line if present.
7474 (* char-height window-min-height
)
7476 (window-min-size window nil window pixelwise
))))
7478 ;; Sanitize MAX-HEIGHT.
7479 (if (numberp max-height
)
7483 window nil window nil nil nil pixelwise
))
7485 (* char-height max-height
)
7487 (+ total-height
(window-max-delta
7488 window nil window nil nil nil pixelwise
))))
7491 ;; If WINDOW is vertically combined, try to resize it
7493 ((and (not (eq fit-window-to-buffer-horizontally
'only
))
7494 (not (window-size-fixed-p window
'preserved
))
7495 (window-combined-p))
7496 ;; Vertically we always want to fit the entire buffer.
7497 ;; WINDOW'S height can't get larger than its frame's pixel
7498 ;; height. Its width remains fixed.
7499 (setq height
(+ (cdr (window-text-pixel-size
7500 nil nil t nil
(frame-pixel-height) t
))
7501 (window-scroll-bar-height window
)
7502 (window-bottom-divider-width)))
7505 (setq height
(/ (+ height char-height -
1) char-height
)))
7506 (unless (= height total-height
)
7507 (window-preserve-size window
)
7508 (window-resize-no-error
7510 (- (max min-height
(min max-height height
)) total-height
)
7511 nil window pixelwise
)
7513 (window-preserve-size window nil t
))))
7514 ;; If WINDOW is horizontally combined, try to resize it
7516 ((and fit-window-to-buffer-horizontally
7517 (not (window-size-fixed-p window t
'preserved
))
7518 (window-combined-p nil t
))
7519 (let* ((total-width (window-size window t pixelwise
))
7521 ;; Sanitize MIN-WIDTH.
7522 (if (numberp min-width
)
7523 ;; Can't get smaller than `window-safe-min-width'.
7525 (* char-width min-width
)
7528 (window-safe-min-pixel-width)
7529 window-safe-min-width
))
7530 ;; Preserve fringes, margins, scrollbars if present.
7532 (* char-width window-min-width
)
7534 (window-min-size nil nil window pixelwise
))))
7536 ;; Sanitize MAX-WIDTH.
7537 (if (numberp max-width
)
7540 window t window nil nil nil pixelwise
))
7542 (* char-width max-width
)
7544 (+ total-width
(window-max-delta
7545 window t window nil nil nil pixelwise
))))
7546 ;; When fitting horizontally, assume that WINDOW's
7547 ;; start position remains unaltered. WINDOW can't get
7548 ;; wider than its frame's pixel width, its height
7549 ;; remains unaltered.
7550 (width (+ (car (window-text-pixel-size
7551 nil
(window-start) (point-max)
7553 ;; Add one char-height to assure that
7554 ;; we're on the safe side. This
7555 ;; overshoots when the first line below
7556 ;; the bottom is wider than the window.
7558 (if pixelwise
1 char-height
))))
7559 (window-right-divider-width))))
7561 (setq width
(/ (+ width char-width -
1) char-width
)))
7562 (unless (= width body-width
)
7563 (window-preserve-size window t
)
7564 (window-resize-no-error
7568 (+ total-width
(- width body-width
))))
7572 (window-preserve-size window t t
))))))))))
7574 (defun window-safely-shrinkable-p (&optional window
)
7575 "Return t if WINDOW can be shrunk without shrinking other windows.
7576 WINDOW defaults to the selected window."
7577 (with-selected-window (or window
(selected-window))
7578 (let ((edges (window-edges)))
7579 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
7580 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
7582 (defun shrink-window-if-larger-than-buffer (&optional window
)
7583 "Shrink height of WINDOW if its buffer doesn't need so many lines.
7584 More precisely, shrink WINDOW vertically to be as small as
7585 possible, while still showing the full contents of its buffer.
7586 WINDOW must be a live window and defaults to the selected one.
7588 Do not shrink WINDOW to less than `window-min-height' lines. Do
7589 nothing if the buffer contains more lines than the present window
7590 height, or if some of the window's contents are scrolled out of
7591 view, or if shrinking this window would also shrink another
7592 window, or if the window is the only window of its frame.
7594 Return non-nil if the window was shrunk, nil otherwise."
7596 (setq window
(window-normalize-window window t
))
7597 ;; Make sure that WINDOW is vertically combined and `point-min' is
7598 ;; visible (for whatever reason that's needed). The remaining issues
7599 ;; should be taken care of by `fit-window-to-buffer'.
7600 (when (and (window-combined-p window
)
7601 (pos-visible-in-window-p (point-min) window
))
7602 (fit-window-to-buffer window
(window-total-height window
))))
7604 (defun kill-buffer-and-window ()
7605 "Kill the current buffer and delete the selected window."
7607 (let ((window-to-delete (selected-window))
7608 (buffer-to-kill (current-buffer))
7609 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
7612 (add-hook 'kill-buffer-hook delete-window-hook t t
)
7613 (if (kill-buffer (current-buffer))
7614 ;; If `delete-window' failed before, we rerun it to regenerate
7615 ;; the error so it can be seen in the echo area.
7616 (when (eq (selected-window) window-to-delete
)
7618 ;; If the buffer is not dead for some reason (probably because
7619 ;; of a `quit' signal), remove the hook again.
7621 (with-current-buffer buffer-to-kill
7622 (remove-hook 'kill-buffer-hook delete-window-hook t
))))))
7625 (defvar recenter-last-op nil
7626 "Indicates the last recenter operation performed.
7627 Possible values: `top', `middle', `bottom', integer or float numbers.")
7629 (defcustom recenter-positions
'(middle top bottom
)
7630 "Cycling order for `recenter-top-bottom'.
7631 A list of elements with possible values `top', `middle', `bottom',
7632 integer or float numbers that define the cycling order for
7633 the command `recenter-top-bottom'.
7635 Top and bottom destinations are `scroll-margin' lines from the true
7636 window top and bottom. Middle redraws the frame and centers point
7637 vertically within the window. Integer number moves current line to
7638 the specified absolute window-line. Float number between 0.0 and 1.0
7639 means the percentage of the screen space from the top. The default
7640 cycling order is middle -> top -> bottom."
7641 :type
'(repeat (choice
7642 (const :tag
"Top" top
)
7643 (const :tag
"Middle" middle
)
7644 (const :tag
"Bottom" bottom
)
7645 (integer :tag
"Line number")
7646 (float :tag
"Percentage")))
7650 (defun recenter-top-bottom (&optional arg
)
7651 "Move current buffer line to the specified window line.
7652 With no prefix argument, successive calls place point according
7653 to the cycling order defined by `recenter-positions'.
7655 A prefix argument is handled like `recenter':
7656 With numeric prefix ARG, move current line to window-line ARG.
7657 With plain `C-u', move current line to window center."
7660 (arg (recenter arg
)) ; Always respect ARG.
7662 (setq recenter-last-op
7663 (if (eq this-command last-command
)
7664 (car (or (cdr (member recenter-last-op recenter-positions
))
7665 recenter-positions
))
7666 (car recenter-positions
)))
7667 (let ((this-scroll-margin
7668 (min (max 0 scroll-margin
)
7669 (truncate (/ (window-body-height) 4.0)))))
7670 (cond ((eq recenter-last-op
'middle
)
7672 ((eq recenter-last-op
'top
)
7673 (recenter this-scroll-margin
))
7674 ((eq recenter-last-op
'bottom
)
7675 (recenter (- -
1 this-scroll-margin
)))
7676 ((integerp recenter-last-op
)
7677 (recenter recenter-last-op
))
7678 ((floatp recenter-last-op
)
7679 (recenter (round (* recenter-last-op
(window-height))))))))))
7681 (define-key global-map
[?\C-l
] 'recenter-top-bottom
)
7683 (defun move-to-window-line-top-bottom (&optional arg
)
7684 "Position point relative to window.
7686 With a prefix argument ARG, acts like `move-to-window-line'.
7688 With no argument, positions point at center of window.
7689 Successive calls position point at positions defined
7690 by `recenter-positions'."
7693 (arg (move-to-window-line arg
)) ; Always respect ARG.
7695 (setq recenter-last-op
7696 (if (eq this-command last-command
)
7697 (car (or (cdr (member recenter-last-op recenter-positions
))
7698 recenter-positions
))
7699 (car recenter-positions
)))
7700 (let ((this-scroll-margin
7701 (min (max 0 scroll-margin
)
7702 (truncate (/ (window-body-height) 4.0)))))
7703 (cond ((eq recenter-last-op
'middle
)
7704 (call-interactively 'move-to-window-line
))
7705 ((eq recenter-last-op
'top
)
7706 (move-to-window-line this-scroll-margin
))
7707 ((eq recenter-last-op
'bottom
)
7708 (move-to-window-line (- -
1 this-scroll-margin
)))
7709 ((integerp recenter-last-op
)
7710 (move-to-window-line recenter-last-op
))
7711 ((floatp recenter-last-op
)
7712 (move-to-window-line (round (* recenter-last-op
(window-height))))))))))
7714 (define-key global-map
[?\M-r
] 'move-to-window-line-top-bottom
)
7716 ;;; Scrolling commands.
7718 ;;; Scrolling commands which do not signal errors at top/bottom
7719 ;;; of buffer at first key-press (instead move to top/bottom
7722 (defcustom scroll-error-top-bottom nil
7723 "Move point to top/bottom of buffer before signaling a scrolling error.
7724 A value of nil means just signal an error if no more scrolling possible.
7725 A value of t means point moves to the beginning or the end of the buffer
7726 \(depending on scrolling direction) when no more scrolling possible.
7727 When point is already on that position, then signal an error."
7732 (defun scroll-up-command (&optional arg
)
7733 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
7734 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
7735 scroll window further, move cursor to the bottom line.
7736 When point is already on that position, then signal an error.
7737 A near full screen is `next-screen-context-lines' less than a full screen.
7738 Negative ARG means scroll downward.
7739 If ARG is the atom `-', scroll downward by nearly full screen."
7742 ((null scroll-error-top-bottom
)
7745 (scroll-down-command nil
))
7746 ((< (prefix-numeric-value arg
) 0)
7747 (scroll-down-command (- (prefix-numeric-value arg
))))
7749 (scroll-up arg
)) ; signal error
7755 ;; When scrolling by ARG lines can't be done,
7756 ;; move by ARG lines instead.
7758 ;; When ARG is nil for full-screen scrolling,
7759 ;; move to the bottom of the buffer.
7760 (goto-char (point-max))))))))
7762 (put 'scroll-up-command
'scroll-command t
)
7764 (defun scroll-down-command (&optional arg
)
7765 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
7766 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
7767 scroll window further, move cursor to the top line.
7768 When point is already on that position, then signal an error.
7769 A near full screen is `next-screen-context-lines' less than a full screen.
7770 Negative ARG means scroll upward.
7771 If ARG is the atom `-', scroll upward by nearly full screen."
7774 ((null scroll-error-top-bottom
)
7777 (scroll-up-command nil
))
7778 ((< (prefix-numeric-value arg
) 0)
7779 (scroll-up-command (- (prefix-numeric-value arg
))))
7781 (scroll-down arg
)) ; signal error
7785 (beginning-of-buffer
7787 ;; When scrolling by ARG lines can't be done,
7788 ;; move by ARG lines instead.
7789 (forward-line (- arg
))
7790 ;; When ARG is nil for full-screen scrolling,
7791 ;; move to the top of the buffer.
7792 (goto-char (point-min))))))))
7794 (put 'scroll-down-command
'scroll-command t
)
7796 ;;; Scrolling commands which scroll a line instead of full screen.
7798 (defun scroll-up-line (&optional arg
)
7799 "Scroll text of selected window upward ARG lines; or one line if no ARG.
7800 If ARG is omitted or nil, scroll upward by one line.
7801 This is different from `scroll-up-command' that scrolls a full screen."
7803 (scroll-up (or arg
1)))
7805 (put 'scroll-up-line
'scroll-command t
)
7807 (defun scroll-down-line (&optional arg
)
7808 "Scroll text of selected window down ARG lines; or one line if no ARG.
7809 If ARG is omitted or nil, scroll down by one line.
7810 This is different from `scroll-down-command' that scrolls a full screen."
7812 (scroll-down (or arg
1)))
7814 (put 'scroll-down-line
'scroll-command t
)
7817 (defun scroll-other-window-down (&optional lines
)
7818 "Scroll the \"other window\" down.
7819 For more details, see the documentation for `scroll-other-window'."
7821 (scroll-other-window
7822 ;; Just invert the argument's meaning.
7823 ;; We can do that without knowing which window it will be.
7824 (if (eq lines
'-
) nil
7826 (- (prefix-numeric-value lines
))))))
7828 (defun beginning-of-buffer-other-window (arg)
7829 "Move point to the beginning of the buffer in the other window.
7830 Leave mark at previous position.
7831 With arg N, put point N/10 of the way from the true beginning."
7833 (let ((orig-window (selected-window))
7834 (window (other-window-for-scrolling)))
7835 ;; We use unwind-protect rather than save-window-excursion
7836 ;; because the latter would preserve the things we want to change.
7839 (select-window window
)
7840 ;; Set point and mark in that window's buffer.
7842 (beginning-of-buffer arg
))
7843 ;; Set point accordingly.
7845 (select-window orig-window
))))
7847 (defun end-of-buffer-other-window (arg)
7848 "Move point to the end of the buffer in the other window.
7849 Leave mark at previous position.
7850 With arg N, put point N/10 of the way from the true end."
7852 ;; See beginning-of-buffer-other-window for comments.
7853 (let ((orig-window (selected-window))
7854 (window (other-window-for-scrolling)))
7857 (select-window window
)
7859 (end-of-buffer arg
))
7861 (select-window orig-window
))))
7863 (defvar mouse-autoselect-window-timer nil
7864 "Timer used by delayed window autoselection.")
7866 (defvar mouse-autoselect-window-position-1 nil
7867 "First mouse position recorded by delayed window autoselection.")
7869 (defvar mouse-autoselect-window-position nil
7870 "Last mouse position recorded by delayed window autoselection.")
7872 (defvar mouse-autoselect-window-window nil
7873 "Last window recorded by delayed window autoselection.")
7875 (defvar mouse-autoselect-window-state nil
7876 "When non-nil, special state of delayed window autoselection.
7877 Possible values are `suspend' (suspend autoselection after a menu or
7878 scrollbar interaction) and `select' (the next invocation of
7879 `handle-select-window' shall select the window immediately).")
7881 (defun mouse-autoselect-window-cancel (&optional force
)
7882 "Cancel delayed window autoselection.
7883 Optional argument FORCE means cancel unconditionally."
7884 (unless (and (not force
)
7885 ;; Don't cancel for select-window or select-frame events
7886 ;; or when the user drags a scroll bar.
7887 (or (memq this-command
7888 '(handle-select-window handle-switch-frame
))
7889 (and (eq this-command
'scroll-bar-toolkit-scroll
)
7890 (memq (nth 4 (event-end last-input-event
))
7891 '(handle end-scroll
)))))
7892 (setq mouse-autoselect-window-state nil
)
7893 (setq mouse-autoselect-window-position-1 nil
)
7894 (when (timerp mouse-autoselect-window-timer
)
7895 (cancel-timer mouse-autoselect-window-timer
))
7896 (remove-hook 'pre-command-hook
'mouse-autoselect-window-cancel
)))
7898 (defun mouse-autoselect-window-start (mouse-position &optional window suspend
)
7899 "Start delayed window autoselection.
7900 MOUSE-POSITION is the last position where the mouse was seen as returned
7901 by `mouse-position'. Optional argument WINDOW non-nil denotes the
7902 window where the mouse was seen. Optional argument SUSPEND non-nil
7903 means suspend autoselection."
7904 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
7905 (setq mouse-autoselect-window-position mouse-position
)
7906 (when window
(setq mouse-autoselect-window-window window
))
7907 (setq mouse-autoselect-window-state
(when suspend
'suspend
))
7908 ;; Install timer which runs `mouse-autoselect-window-select' after
7909 ;; `mouse-autoselect-window' seconds.
7910 (setq mouse-autoselect-window-timer
7912 (abs mouse-autoselect-window
) nil
'mouse-autoselect-window-select
)))
7914 (defun mouse-autoselect-window-select ()
7915 "Select window with delayed window autoselection.
7916 If the mouse position has stabilized in a non-selected window, select
7917 that window. The minibuffer window is selected only if the minibuffer
7918 is active. This function is run by `mouse-autoselect-window-timer'."
7920 (let* ((mouse-position (mouse-position))
7923 (window-at (cadr mouse-position
) (cddr mouse-position
)
7924 (car mouse-position
)))))
7926 ((or (and (fboundp 'menu-or-popup-active-p
) (menu-or-popup-active-p))
7928 (let ((coords (coordinates-in-window-p
7929 (cdr mouse-position
) window
)))
7930 (and (not (consp coords
))
7931 (not (memq coords
'(left-margin right-margin
)))))))
7932 ;; A menu / popup dialog is active or the mouse is not on the
7933 ;; text region of WINDOW: Suspend autoselection temporarily.
7934 (mouse-autoselect-window-start mouse-position nil t
))
7935 ((or (eq mouse-autoselect-window-state
'suspend
)
7936 ;; When the mouse is at its first recorded position, restart
7937 ;; delayed autoselection. This works around a scenario with
7938 ;; two two-window frames with identical dimensions: select the
7939 ;; first window of the first frame, switch to the second
7940 ;; frame, move the mouse to its second window, minimize the
7941 ;; second frame. Now the second window of the first frame
7942 ;; gets selected although the mouse never really "moved" into
7944 (and (numberp mouse-autoselect-window
)
7945 (equal (mouse-position) mouse-autoselect-window-position-1
)))
7946 ;; Delayed autoselection was temporarily suspended, reenable it.
7947 (mouse-autoselect-window-start mouse-position
))
7948 ((and window
(not (eq window
(selected-window)))
7949 (or (not (numberp mouse-autoselect-window
))
7950 (and (>= mouse-autoselect-window
0)
7951 ;; If `mouse-autoselect-window' is non-negative,
7952 ;; select window if it's the same as before.
7953 (eq window mouse-autoselect-window-window
))
7954 ;; Otherwise select window iff the mouse is at the same
7955 ;; position as before. Observe that the first test
7956 ;; after starting autoselection usually fails since the
7957 ;; value of `mouse-autoselect-window-position' recorded
7958 ;; there is the position where the mouse has entered the
7959 ;; new window and not necessarily where the mouse has
7961 (equal mouse-position mouse-autoselect-window-position
))
7962 ;; The minibuffer is a candidate window if it's active.
7963 (or (not (window-minibuffer-p window
))
7964 (eq window
(active-minibuffer-window))))
7965 ;; Mouse position has stabilized in non-selected window: Cancel
7966 ;; delayed autoselection and try to select that window.
7967 (mouse-autoselect-window-cancel t
)
7968 ;; Select window where mouse appears unless the selected window is the
7969 ;; minibuffer. Use `unread-command-events' in order to execute pre-
7970 ;; and post-command hooks and trigger idle timers. To avoid delaying
7971 ;; autoselection again, set `mouse-autoselect-window-state'."
7972 (unless (window-minibuffer-p)
7973 (setq mouse-autoselect-window-state
'select
)
7974 (setq unread-command-events
7975 (cons (list 'select-window
(list window
))
7976 unread-command-events
))))
7977 ((or (and window
(eq window
(selected-window)))
7978 (not (numberp mouse-autoselect-window
))
7979 (equal mouse-position mouse-autoselect-window-position
))
7980 ;; Mouse position has either stabilized in the selected window or at
7981 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
7982 (mouse-autoselect-window-cancel t
))
7984 ;; Mouse position has not stabilized yet, resume delayed
7986 (mouse-autoselect-window-start mouse-position window
))))))
7988 (defun handle-select-window (event)
7989 "Handle select-window events."
7991 (let ((window (posn-window (event-start event
))))
7992 (unless (or (not (window-live-p window
))
7993 ;; Don't switch if we're currently in the minibuffer.
7994 ;; This tries to work around problems where the
7995 ;; minibuffer gets unselected unexpectedly, and where
7996 ;; you then have to move your mouse all the way down to
7997 ;; the minibuffer to select it.
7998 (window-minibuffer-p)
7999 ;; Don't switch to minibuffer window unless it's active.
8000 (and (window-minibuffer-p window
)
8001 (not (minibuffer-window-active-p window
)))
8002 ;; Don't switch when autoselection shall be delayed.
8003 (and (numberp mouse-autoselect-window
)
8004 (not (eq mouse-autoselect-window-state
'select
))
8005 (let ((position (mouse-position)))
8006 ;; Cancel any delayed autoselection.
8007 (mouse-autoselect-window-cancel t
)
8008 ;; Start delayed autoselection from current mouse
8009 ;; position and window.
8010 (setq mouse-autoselect-window-position-1 position
)
8011 (mouse-autoselect-window-start position window
)
8012 ;; Executing a command cancels delayed autoselection.
8014 'pre-command-hook
'mouse-autoselect-window-cancel
))))
8015 (when mouse-autoselect-window
8016 ;; Reset state of delayed autoselection.
8017 (setq mouse-autoselect-window-state nil
)
8018 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
8019 (run-hooks 'mouse-leave-buffer-hook
))
8022 (select-window window
))))
8024 (defun truncated-partial-width-window-p (&optional window
)
8025 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
8026 WINDOW must be a live window and defaults to the selected one.
8027 Return nil if WINDOW is not a partial-width window
8028 (regardless of the value of `truncate-lines').
8029 Otherwise, consult the value of `truncate-partial-width-windows'
8030 for the buffer shown in WINDOW."
8031 (setq window
(window-normalize-window window t
))
8032 (unless (window-full-width-p window
)
8033 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
8034 (window-buffer window
))))
8035 (if (integerp t-p-w-w
)
8036 (< (window-width window
) t-p-w-w
)
8040 ;; Automatically inform subprocesses of changes to window size.
8042 (defcustom window-adjust-process-window-size-function
8043 'window-adjust-process-window-size-smallest
8044 "Control how Emacs chooses inferior process window sizes.
8045 Emacs uses this function to tell processes the space they have
8046 available for displaying their output. After each window
8047 configuration change, Emacs calls the value of
8048 `window-adjust-process-window-size-function' for each process
8049 with a buffer being displayed in at least one window.
8050 This function is responsible for combining the sizes of the
8051 displayed windows and returning a cons (WIDTH . HEIGHT)
8052 describing the width and height with which Emacs will call
8053 `set-process-window-size' for that process. If the function
8054 returns `nil', Emacs does not call `set-process-window-size'.
8056 This function is called with the process buffer as the current
8057 buffer and with two arguments: the process and a list of windows
8058 displaying process. Modes can make this variable buffer-local;
8059 additionally, the `adjust-window-size-function' process property
8060 overrides the global or buffer-local value of
8061 `window-adjust-process-window-size-function'."
8063 (const :tag
"Minimum area of any window"
8064 window-adjust-process-window-size-smallest
)
8065 (const :tag
"Maximum area of any window"
8066 window-adjust-process-window-size-largest
)
8067 (const :tag
"Do not adjust process window sizes" ignore
)
8072 (defun window-adjust-process-window-size (reducer process windows
)
8073 "Adjust the process window size of PROCESS.
8074 WINDOWS is a list of windows associated with PROCESS. REDUCER is
8075 a two-argument function used to combine the widths and heights of
8078 (let ((width (window-body-width (car windows
)))
8079 (height (window-body-height (car windows
))))
8080 (dolist (window (cdr windows
))
8081 (setf width
(funcall reducer width
(window-body-width window
)))
8082 (setf height
(funcall reducer height
(window-body-height window
))))
8083 (cons width height
))))
8085 (defun window-adjust-process-window-size-smallest (process windows
)
8086 "Adjust the process window size of PROCESS.
8087 WINDOWS is a list of windows associated with PROCESS. Choose the
8088 smallest area available for displaying PROCESS's output."
8089 (window-adjust-process-window-size #'min process windows
))
8091 (defun window-adjust-process-window-size-largest (process windows
)
8092 "Adjust the process window size of PROCESS.
8093 WINDOWS is a list of windows associated with PROCESS. Choose the
8094 largest area available for displaying PROCESS's output."
8095 (window-adjust-process-window-size #'max process windows
))
8097 (defun window--process-window-list ()
8098 "Return an alist mapping processes to associated windows.
8099 A window is associated with a process if that window is
8100 displaying that processes's buffer."
8101 (let ((processes (process-list))
8102 (process-windows nil
))
8105 (let ((buffer (window-buffer window
))
8107 (while (let ((process (car iter
)))
8108 (if (and (process-live-p process
)
8109 (eq buffer
(process-buffer process
)))
8110 (let ((procwin (assq process process-windows
)))
8111 ;; Add this window to the list of windows
8112 ;; displaying process.
8114 (push window
(cdr procwin
))
8115 (push (list process window
) process-windows
))
8116 ;; We found our process for this window, so
8117 ;; stop iterating over the process list.
8119 (setf iter
(cdr iter
)))))))
8123 (defun window--adjust-process-windows ()
8124 "Update process window sizes to match the current window configuration."
8125 (dolist (procwin (window--process-window-list))
8126 (let ((process (car procwin
)))
8127 (with-demoted-errors "Error adjusting window size: %S"
8128 (with-current-buffer (process-buffer process
)
8129 (let ((size (funcall
8130 (or (process-get process
'adjust-window-size-function
)
8131 window-adjust-process-window-size-function
)
8132 process
(cdr procwin
))))
8134 (set-process-window-size process
(cdr size
) (car size
)))))))))
8136 (add-hook 'window-configuration-change-hook
'window--adjust-process-windows
)
8139 ;; Some of these are in tutorial--default-keys, so update that if you
8141 (define-key ctl-x-map
"0" 'delete-window
)
8142 (define-key ctl-x-map
"1" 'delete-other-windows
)
8143 (define-key ctl-x-map
"2" 'split-window-below
)
8144 (define-key ctl-x-map
"3" 'split-window-right
)
8145 (define-key ctl-x-map
"o" 'other-window
)
8146 (define-key ctl-x-map
"^" 'enlarge-window
)
8147 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
8148 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
8149 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
8150 (define-key ctl-x-map
"+" 'balance-windows
)
8151 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
8153 ;;; window.el ends here