1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2014 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 (&optional buffer 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
189 (macroexp-let2 nil vaction action
190 (macroexp-let2 nil 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
212 (macroexp-let2 nil vaction action
213 (macroexp-let2 nil 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
234 (macroexp-let2 nil vaction action
235 (macroexp-let2 nil 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 ,buffer
,vaction
)))
242 (let ((inhibit-read-only t
)
243 (inhibit-modification-hooks t
))
244 (setq ,value
(progn ,@body
)))
246 (set-window-point ,window
(point-min))
248 (when (functionp (cdr (assq 'window-height
(cdr ,vaction
))))
250 (funcall (cdr (assq 'window-height
(cdr ,vaction
))) ,window
)))
252 (if (functionp ,vquit-function
)
253 (funcall ,vquit-function
,window
,value
)
256 ;; The following two functions are like `window-next-sibling' and
257 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
258 ;; they don't substitute the selected window for nil), and they return
259 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
260 ;; a minibuffer window).
261 (defun window-right (window)
262 "Return WINDOW's right sibling.
263 Return nil if WINDOW is the root window of its frame. WINDOW can
265 (and window
(window-parent window
) (window-next-sibling window
)))
267 (defun window-left (window)
268 "Return WINDOW's left sibling.
269 Return nil if WINDOW is the root window of its frame. WINDOW can
271 (and window
(window-parent window
) (window-prev-sibling window
)))
273 (defun window-child (window)
274 "Return WINDOW's first child window.
275 WINDOW can be any window."
276 (or (window-top-child window
) (window-left-child window
)))
278 (defun window-child-count (window)
279 "Return number of WINDOW's child windows.
280 WINDOW can be any window."
282 (when (and (windowp window
) (setq window
(window-child window
)))
284 (setq count
(1+ count
))
285 (setq window
(window-next-sibling window
))))
288 (defun window-last-child (window)
289 "Return last child window of WINDOW.
290 WINDOW can be any window."
291 (when (and (windowp window
) (setq window
(window-child window
)))
292 (while (window-next-sibling window
)
293 (setq window
(window-next-sibling window
))))
296 (defun window-normalize-buffer (buffer-or-name)
297 "Return buffer specified by BUFFER-OR-NAME.
298 BUFFER-OR-NAME must be either a buffer or a string naming a live
299 buffer and defaults to the current buffer."
301 ((not buffer-or-name
)
303 ((bufferp buffer-or-name
)
304 (if (buffer-live-p buffer-or-name
)
306 (error "Buffer %s is not a live buffer" buffer-or-name
)))
307 ((get-buffer buffer-or-name
))
309 (error "No such buffer %s" buffer-or-name
))))
311 (defun window-normalize-frame (frame)
312 "Return frame specified by FRAME.
313 FRAME must be a live frame and defaults to the selected frame."
315 (if (frame-live-p frame
)
317 (error "%s is not a live frame" frame
))
320 (defun window-normalize-window (window &optional live-only
)
321 "Return the window specified by WINDOW.
322 If WINDOW is nil, return the selected window. Otherwise, if
323 WINDOW is a live or an internal window, return WINDOW; if
324 LIVE-ONLY is non-nil, return WINDOW for a live window only.
325 Otherwise, signal an error."
330 (if (window-live-p window
)
332 (error "%s is not a live window" window
)))
333 ((window-valid-p window
)
336 (error "%s is not a valid window" window
))))
338 ;; Maybe this should go to frame.el.
339 (defun frame-char-size (&optional window-or-frame horizontal
)
340 "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
341 If WINDOW-OR-FRAME is a live frame, return the value of
342 `frame-char-height' for that frame. If WINDOW-OR-FRAME is a
343 valid window, return the value of `frame-char-height' for that
344 window's frame. In any other case, return the value of
345 `frame-char-height' for the selected frame.
347 Optional argument HORIZONTAL non-nil means to return the value of
348 `frame-char-width' for WINDOW-OR-FRAME."
351 ((window-valid-p window-or-frame
)
352 (window-frame window-or-frame
))
353 ((frame-live-p window-or-frame
)
355 (t (selected-frame)))))
357 (frame-char-width frame
)
358 (frame-char-height frame
))))
360 (defvar ignore-window-parameters nil
361 "If non-nil, standard functions ignore window parameters.
362 The functions currently affected by this are `split-window',
363 `delete-window', `delete-other-windows' and `other-window'.
365 An application may bind this to a non-nil value around calls to
366 these functions to inhibit processing of window parameters.")
368 ;; This must go to C, finally (or get removed).
369 (defconst window-safe-min-height
1
370 "The absolute minimum number of lines of any window.
371 Anything less might crash Emacs.")
373 (defun window-safe-min-pixel-height (&optional window
)
374 "Return the absolute minimum pixel height of WINDOW."
375 (* window-safe-min-height
376 (frame-char-size (window-normalize-window window
))))
378 (defcustom window-min-height
4
379 "The minimum total height, in lines, of any window.
380 The value has to accommodate one text line, a mode and header
381 line, and a bottom divider, if present. A value less than
382 `window-safe-min-height' is ignored. The value of this variable
383 is honored when windows are resized or split.
385 Applications should never rebind this variable. To resize a
386 window to a height less than the one specified here, an
387 application should instead call `window-resize' with a non-nil
388 IGNORE argument. In order to have `split-window' make a window
389 shorter, explicitly specify the SIZE argument of that function."
394 (defun window-min-pixel-height (&optional window
)
395 "Return the minimum pixel height of window WINDOW."
396 (* (max window-min-height window-safe-min-height
)
397 (frame-char-size window
)))
399 ;; This must go to C, finally (or get removed).
400 (defconst window-safe-min-width
2
401 "The absolute minimum number of columns of a window.
402 Anything less might crash Emacs.")
404 (defun window-safe-min-pixel-width (&optional window
)
405 "Return the absolute minimum pixel width of WINDOW."
406 (* window-safe-min-width
407 (frame-char-size (window-normalize-window window
) t
)))
409 (defcustom window-min-width
10
410 "The minimum total width, in columns, of any window.
411 The value has to accommodate two text columns as well as margins,
412 fringes, a scroll bar and a right divider, if present. A value
413 less than `window-safe-min-width' is ignored. The value of this
414 variable is honored when windows are resized or split.
416 Applications should never rebind this variable. To resize a
417 window to a width less than the one specified here, an
418 application should instead call `window-resize' with a non-nil
419 IGNORE argument. In order to have `split-window' make a window
420 narrower, explicitly specify the SIZE argument of that function."
425 (defun window-min-pixel-width (&optional window
)
426 "Return the minimum pixel width of window WINDOW."
427 (* (max window-min-width window-safe-min-width
)
428 (frame-char-size window t
)))
430 (defun window-safe-min-pixel-size (&optional window horizontal
)
431 "Return the absolute minimum pixel height of WINDOW.
432 Optional argument HORIZONTAL non-nil means return the absolute
433 minimum pixel width of WINDOW."
435 (window-safe-min-pixel-width window
)
436 (window-safe-min-pixel-height window
)))
438 (defun window-combined-p (&optional window horizontal
)
439 "Return non-nil if WINDOW has siblings in a given direction.
440 WINDOW must be a valid window and defaults to the selected one.
442 HORIZONTAL determines a direction for the window combination. If
443 HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
444 a vertical window combination. If HORIZONTAL is non-nil, return
445 non-nil if WINDOW is part of a horizontal window combination."
446 (setq window
(window-normalize-window window
))
447 (let ((parent (window-parent window
)))
450 (window-left-child parent
)
451 (window-top-child parent
)))))
453 (defun window-combination-p (&optional window horizontal
)
454 "Return WINDOW's first child if WINDOW is a vertical combination.
455 WINDOW can be any window and defaults to the selected one.
456 Optional argument HORIZONTAL non-nil means return WINDOW's first
457 child if WINDOW is a horizontal combination."
458 (setq window
(window-normalize-window window
))
460 (window-left-child window
)
461 (window-top-child window
)))
463 (defun window-combinations (window &optional horizontal
)
464 "Return largest number of windows vertically arranged within WINDOW.
465 WINDOW must be a valid window and defaults to the selected one.
466 If HORIZONTAL is non-nil, return the largest number of
467 windows horizontally arranged within WINDOW."
468 (setq window
(window-normalize-window window
))
470 ((window-live-p window
)
471 ;; If WINDOW is live, return 1.
474 (window-left-child window
)
475 (window-top-child window
))
476 ;; If WINDOW is iso-combined, return the sum of the values for all
477 ;; child windows of WINDOW.
478 (let ((child (window-child window
))
482 (+ (window-combinations child horizontal
)
484 (setq child
(window-right child
)))
487 ;; If WINDOW is not iso-combined, return the maximum value of any
488 ;; child window of WINDOW.
489 (let ((child (window-child window
))
493 (max (window-combinations child horizontal
)
495 (setq child
(window-right child
)))
498 (defun walk-window-tree-1 (fun walk-window-tree-window any
&optional sub-only
)
499 "Helper function for `walk-window-tree' and `walk-window-subtree'."
500 (let (walk-window-tree-buffer)
501 (while walk-window-tree-window
502 (setq walk-window-tree-buffer
503 (window-buffer walk-window-tree-window
))
504 (when (or walk-window-tree-buffer any
)
505 (funcall fun walk-window-tree-window
))
506 (unless walk-window-tree-buffer
508 fun
(window-left-child walk-window-tree-window
) any
)
510 fun
(window-top-child walk-window-tree-window
) any
))
512 (setq walk-window-tree-window nil
)
513 (setq walk-window-tree-window
514 (window-right walk-window-tree-window
))))))
516 (defun walk-window-tree (fun &optional frame any minibuf
)
517 "Run function FUN on each live window of FRAME.
518 FUN must be a function with one argument - a window. FRAME must
519 be a live frame and defaults to the selected one. ANY, if
520 non-nil, means to run FUN on all live and internal windows of
523 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
524 window even if it isn't active. MINIBUF nil or omitted means run
525 FUN on FRAME's minibuffer window only if it's active. In both
526 cases the minibuffer window must be part of FRAME. MINIBUF
527 neither nil nor t means never run FUN on the minibuffer window.
529 This function performs a pre-order, depth-first traversal of the
530 window tree. If FUN changes the window tree, the result is
532 (setq frame
(window-normalize-frame frame
))
533 (walk-window-tree-1 fun
(frame-root-window frame
) any
)
534 (when (memq minibuf
'(nil t
))
535 ;; Run FUN on FRAME's minibuffer window if requested.
536 (let ((minibuffer-window (minibuffer-window frame
)))
537 (when (and (window-live-p minibuffer-window
)
538 (eq (window-frame minibuffer-window
) frame
)
540 (minibuffer-window-active-p minibuffer-window
)))
541 (funcall fun minibuffer-window
)))))
543 (defun walk-window-subtree (fun &optional window any
)
544 "Run function FUN on the subtree of windows rooted at WINDOW.
545 WINDOW defaults to the selected window. FUN must be a function
546 with one argument - a window. By default, run FUN only on live
547 windows of the subtree. If the optional argument ANY is non-nil,
548 run FUN on all live and internal windows of the subtree. If
549 WINDOW is live, run FUN on WINDOW only.
551 This function performs a pre-order, depth-first traversal of the
552 subtree rooted at WINDOW. If FUN changes that tree, the result
554 (setq window
(window-normalize-window window
))
555 (walk-window-tree-1 fun window any t
))
557 (defun window-with-parameter (parameter &optional value frame any minibuf
)
558 "Return first window on FRAME with PARAMETER non-nil.
559 FRAME defaults to the selected frame. Optional argument VALUE
560 non-nil means only return a window whose window-parameter value
561 for PARAMETER equals VALUE (comparison is done with `equal').
562 Optional argument ANY non-nil means consider internal windows
565 Optional argument MINIBUF t means consider FRAME's minibuffer
566 window even if it isn't active. MINIBUF nil or omitted means
567 consider FRAME's minibuffer window only if it's active. In both
568 cases the minibuffer window must be part of FRAME. MINIBUF
569 neither nil nor t means never consider the minibuffer window."
574 (when (and (setq this-value
(window-parameter window parameter
))
575 (or (not value
) (equal value this-value
)))
576 (throw 'found window
)))
577 frame any minibuf
))))
580 (defun window-atom-root (&optional window
)
581 "Return root of atomic window WINDOW is a part of.
582 WINDOW must be a valid window and defaults to the selected one.
583 Return nil if WINDOW is not part of an atomic window."
584 (setq window
(window-normalize-window window
))
586 (while (and window
(window-parameter window
'window-atom
))
588 (setq window
(window-parent window
)))
591 (defun window-make-atom (window)
592 "Make WINDOW an atomic window.
593 WINDOW must be an internal window. Return WINDOW."
594 (if (not (window-child window
))
595 (error "Window %s is not an internal window" window
)
598 (unless (window-parameter window
'window-atom
)
599 (set-window-parameter window
'window-atom t
)))
603 (defun display-buffer-in-atom-window (buffer alist
)
604 "Display BUFFER in an atomic window.
605 This function displays BUFFER in a new window that will be
606 combined with an existing window to form an atomic window. If
607 the existing window is already part of an atomic window, add the
608 new window to that atomic window. Operations like `split-window'
609 or `delete-window', when applied to a constituent of an atomic
610 window, are applied atomically to the root of that atomic window.
612 ALIST is an association list of symbols and values. The
613 following symbols can be used.
615 `window' specifies the existing window the new window shall be
616 combined with. Use `window-atom-root' to make the new window a
617 sibling of an atomic window's root. If an internal window is
618 specified here, all children of that window become part of the
619 atomic window too. If no window is specified, the new window
620 becomes a sibling of the selected window. By default, the
621 `window-atom' parameter of the existing window is set to `main'
622 provided it is live and was not set before.
624 `side' denotes the side of the existing window where the new
625 window shall be located. Valid values are `below', `right',
626 `above' and `left'. The default is `below'. By default, the
627 `window-atom' parameter of the new window is set to this value.
629 The return value is the new window, nil when creating that window
631 (let* ((ignore-window-parameters t
)
632 (window-combination-limit t
)
633 (window-combination-resize 'atom
)
634 (window (cdr (assq 'window alist
)))
635 (side (cdr (assq 'side alist
)))
636 (atom (when window
(window-parameter window
'window-atom
)))
638 (setq window
(window-normalize-window window
))
639 (setq root
(window-atom-root window
))
640 ;; Split off new window.
641 (when (setq new
(split-window window nil side
))
643 (if (and root
(not (eq root window
)))
644 ;; When WINDOW was part of an atomic window and we did not
645 ;; split its root, root atomic window at old root.
647 ;; Otherwise, root atomic window at WINDOW's new parent.
648 (window-parent window
)))
649 ;; Assign `window-atom' parameters, if needed.
650 (when (and (not atom
) (window-live-p window
))
651 (set-window-parameter window
'window-atom
'main
))
652 (set-window-parameter new
'window-atom side
)
653 ;; Display BUFFER in NEW and return NEW.
654 (window--display-buffer
655 buffer new
'window alist display-buffer-mark-dedicated
))))
657 (defun window--atom-check-1 (window)
658 "Subroutine of `window--atom-check'."
660 (if (window-parameter window
'window-atom
)
662 (when (or (catch 'reset
665 (if (window-parameter window
'window-atom
)
666 (setq count
(1+ count
))
669 ;; count >= 1 must hold here. If there's no other
670 ;; window around dissolve this atomic window.
672 ;; Dissolve atomic window.
675 (set-window-parameter window
'window-atom nil
))
678 (unless (window-buffer window
)
679 (window--atom-check-1 (window-left-child window
))
680 (window--atom-check-1 (window-top-child window
))))
681 ;; Check right sibling
682 (window--atom-check-1 (window-right window
))))
684 (defun window--atom-check (&optional frame
)
685 "Check atomicity of all windows on FRAME.
686 FRAME defaults to the selected frame. If an atomic window is
687 wrongly configured, reset the atomicity of all its windows on
688 FRAME to nil. An atomic window is wrongly configured if it has
689 no child windows or one of its child windows is not atomic."
690 (window--atom-check-1 (frame-root-window frame
)))
693 (defvar window-sides
'(left top right bottom
)
696 (defcustom window-sides-vertical nil
697 "If non-nil, left and right side windows are full height.
698 Otherwise, top and bottom side windows are full width."
703 (defcustom window-sides-slots
'(nil nil nil nil
)
704 "Maximum number of side window slots.
705 The value is a list of four elements specifying the number of
706 side window slots on (in this order) the left, top, right and
707 bottom side of each frame. If an element is a number, this means
708 to display at most that many side windows on the corresponding
709 side. If an element is nil, this means there's no bound on the
710 number of slots on that side."
715 :value
(nil nil nil nil
)
718 :help-echo
"Maximum slots of left side window."
720 :format
"%[Left%] %v\n"
721 (const :tag
"Unlimited" :format
"%t" nil
)
722 (integer :tag
"Number" :value
2 :size
5))
725 :help-echo
"Maximum slots of top side window."
727 :format
"%[Top%] %v\n"
728 (const :tag
"Unlimited" :format
"%t" nil
)
729 (integer :tag
"Number" :value
3 :size
5))
732 :help-echo
"Maximum slots of right side window."
734 :format
"%[Right%] %v\n"
735 (const :tag
"Unlimited" :format
"%t" nil
)
736 (integer :tag
"Number" :value
2 :size
5))
739 :help-echo
"Maximum slots of bottom side window."
741 :format
"%[Bottom%] %v\n"
742 (const :tag
"Unlimited" :format
"%t" nil
)
743 (integer :tag
"Number" :value
3 :size
5)))
746 (defun window--major-non-side-window (&optional frame
)
747 "Return the major non-side window of frame FRAME.
748 The optional argument FRAME must be a live frame and defaults to
751 If FRAME has at least one side window, the major non-side window
752 is either an internal non-side window such that all other
753 non-side windows on FRAME descend from it, or the single live
754 non-side window of FRAME. If FRAME has no side windows, return
756 (let ((frame (window-normalize-frame frame
))
758 ;; Set major to the _last_ window found by `walk-window-tree' that
759 ;; is not a side window but has a side window as its sibling.
762 (and (not (window-parameter window
'window-side
))
763 (or (and (setq sibling
(window-prev-sibling window
))
764 (window-parameter sibling
'window-side
))
765 (and (setq sibling
(window-next-sibling window
))
766 (window-parameter sibling
'window-side
)))
767 (setq major window
)))
769 (or major
(frame-root-window frame
))))
771 (defun window--major-side-window (side)
772 "Return major side window on SIDE.
773 SIDE must be one of the symbols `left', `top', `right' or
774 `bottom'. Return nil if no such window exists."
775 (let ((root (frame-root-window))
777 ;; (1) If a window on the opposite side exists, return that window's
779 ;; (2) If the new window shall span the entire side, return the
780 ;; frame's root window.
781 ;; (3) If a window on an orthogonal side exists, return that
783 ;; (4) Otherwise return the frame's root window.
785 ((or (and (eq side
'left
)
786 (setq window
(window-with-parameter 'window-side
'right nil t
)))
788 (setq window
(window-with-parameter 'window-side
'bottom nil t
))))
789 (window-prev-sibling window
))
790 ((or (and (eq side
'right
)
791 (setq window
(window-with-parameter 'window-side
'left nil t
)))
792 (and (eq side
'bottom
)
793 (setq window
(window-with-parameter 'window-side
'top nil t
))))
794 (window-next-sibling window
))
795 ((memq side
'(left right
))
797 (window-sides-vertical
799 ((setq window
(window-with-parameter 'window-side
'top nil t
))
800 (window-next-sibling window
))
801 ((setq window
(window-with-parameter 'window-side
'bottom nil t
))
802 (window-prev-sibling window
))
804 ((memq side
'(top bottom
))
806 ((not window-sides-vertical
)
808 ((setq window
(window-with-parameter 'window-side
'left nil t
))
809 (window-next-sibling window
))
810 ((setq window
(window-with-parameter 'window-side
'right nil t
))
811 (window-prev-sibling window
))
814 (defun display-buffer-in-major-side-window (buffer side slot
&optional alist
)
815 "Display BUFFER in a new window on SIDE of the selected frame.
816 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
817 specifies the slot to use. ALIST is an association list of
818 symbols and values as passed to `display-buffer-in-side-window'.
819 This function may be called only if no window on SIDE exists yet.
820 The new window automatically becomes the \"major\" side window on
821 SIDE. Return the new window, nil if its creation window failed."
822 (let* ((left-or-right (memq side
'(left right
)))
823 (major (window--major-side-window side
))
825 ((eq side
'top
) 'above
)
826 ((eq side
'bottom
) 'below
)
828 ;; The following two bindings will tell `split-window' to take
829 ;; the space for the new window from `major' and not make a new
830 ;; parent window unless needed.
831 (window-combination-resize 'side
)
832 (window-combination-limit nil
)
833 (new (split-window major nil on-side
)))
835 ;; Initialize `window-side' parameter of new window to SIDE.
836 (set-window-parameter new
'window-side side
)
837 ;; Install `window-slot' parameter of new window.
838 (set-window-parameter new
'window-slot slot
)
839 ;; Install `delete-window' parameter thus making sure that when
840 ;; the new window is deleted, a side window on the opposite side
841 ;; does not get resized.
842 (set-window-parameter new
'delete-window
'delete-side-window
)
843 ;; Auto-adjust height/width of new window unless a size has been
844 ;; explicitly requested.
845 (unless (if left-or-right
846 (cdr (assq 'window-width alist
))
847 (cdr (assq 'window-height alist
)))
851 (if left-or-right
'window-width
'window-height
)
852 (/ (window-total-size (frame-root-window) left-or-right
)
853 ;; By default use a fourth of the size of the frame's
857 ;; Install BUFFER in new window and return NEW.
858 (window--display-buffer buffer new
'window alist
'side
))))
860 (defun delete-side-window (window)
861 "Delete side window WINDOW."
862 (let ((window-combination-resize
863 (window-parameter (window-parent window
) 'window-side
))
864 (ignore-window-parameters t
))
865 (delete-window window
)))
867 (defun display-buffer-in-side-window (buffer alist
)
868 "Display BUFFER in a side window of the selected frame.
869 ALIST is an association list of symbols and values. The
870 following special symbols can be used in ALIST.
872 `side' denotes the side of the frame where the new window shall
873 be located. Valid values are `bottom', `right', `top' and
874 `left'. The default is `bottom'.
876 `slot' if non-nil, specifies the window slot where to display
877 BUFFER. A value of zero or nil means use the middle slot on
878 the specified side. A negative value means use a slot
879 preceding (that is, above or on the left of) the middle slot.
880 A positive value means use a slot following (that is, below or
881 on the right of) the middle slot. The default is zero."
882 (let ((side (or (cdr (assq 'side alist
)) 'bottom
))
883 (slot (or (cdr (assq 'slot alist
)) 0)))
885 ((not (memq side
'(top bottom left right
)))
886 (error "Invalid side %s specified" side
))
887 ((not (numberp slot
))
888 (error "Invalid slot %s specified" slot
)))
890 (let* ((major (window-with-parameter 'window-side side nil t
))
891 ;; `major' is the major window on SIDE, `windows' the list of
892 ;; life windows on SIDE.
898 (when (eq (window-parameter window
'window-side
) side
)
899 (setq windows
(cons window windows
))))
901 (nreverse windows
))))
902 (slots (when major
(max 1 (window-child-count major
))))
908 ((eq side
'bottom
) 3))
910 window this-window this-slot prev-window next-window
911 best-window best-slot abs-slot
)
914 ((and (numberp max-slots
) (<= max-slots
0))
915 ;; No side-slots available on this side. Don't create an error,
919 ;; No major window exists on this side, make one.
920 (display-buffer-in-major-side-window buffer side slot alist
))
922 ;; Scan windows on SIDE.
924 (dolist (window windows
)
925 (setq this-slot
(window-parameter window
'window-slot
))
927 ;; The following should not happen and probably be checked
928 ;; by window--side-check.
929 ((not (numberp this-slot
)))
931 ;; A window with a matching slot has been found.
932 (setq this-window window
)
935 ;; Check if this window has a better slot value wrt the
936 ;; slot of the window we want.
938 (if (or (and (> this-slot
0) (> slot
0))
939 (and (< this-slot
0) (< slot
0)))
940 (abs (- slot this-slot
))
941 (+ (abs slot
) (abs this-slot
))))
942 (unless (and best-slot
(<= best-slot abs-slot
))
943 (setq best-window window
)
944 (setq best-slot abs-slot
))
947 (setq prev-window window
))
949 (setq next-window window
)))))))
951 ;; `this-window' is the first window with the same SLOT.
952 ;; `prev-window' is the window with the largest slot < SLOT. A new
953 ;; window will be created after it.
954 ;; `next-window' is the window with the smallest slot > SLOT. A new
955 ;; window will be created before it.
956 ;; `best-window' is the window with the smallest absolute difference
957 ;; of its slot and SLOT.
959 ;; Note: We dedicate the window used softly to its buffer to
960 ;; avoid that "other" (non-side) buffer display functions steal
961 ;; it from us. This must eventually become customizable via
962 ;; ALIST (or, better, avoided in the "other" functions).
964 ;; Reuse `this-window'.
965 (window--display-buffer buffer this-window
'reuse alist
'side
))
966 (and (or (not max-slots
) (< slots max-slots
))
968 ;; Make new window before `next-window'.
970 (if (memq side
'(left right
)) 'above
'left
))
971 (window-combination-resize 'side
))
972 (setq window
(split-window next-window nil next-side
))
973 ;; When the new window is deleted, its space
974 ;; is returned to other side windows.
975 (set-window-parameter
976 window
'delete-window
'delete-side-window
)
979 ;; Make new window after `prev-window'.
981 (if (memq side
'(left right
)) 'below
'right
))
982 (window-combination-resize 'side
))
983 (setq window
(split-window prev-window nil prev-side
))
984 ;; When the new window is deleted, its space
985 ;; is returned to other side windows.
986 (set-window-parameter
987 window
'delete-window
'delete-side-window
)
989 (set-window-parameter window
'window-slot slot
)
990 (window--display-buffer buffer window
'window alist
'side
))
992 ;; Reuse `best-window'.
994 ;; Give best-window the new slot value.
995 (set-window-parameter best-window
'window-slot slot
)
996 (window--display-buffer
997 buffer best-window
'reuse alist
'side
)))))))))
999 (defun window--side-check (&optional frame
)
1000 "Check the side window configuration of FRAME.
1001 FRAME defaults to the selected frame.
1003 A valid side window configuration preserves the following two
1006 - If there exists a window whose window-side parameter is
1007 non-nil, there must exist at least one live window whose
1008 window-side parameter is nil.
1010 - If a window W has a non-nil window-side parameter (i) it must
1011 have a parent window and that parent's window-side parameter
1012 must be either nil or the same as for W, and (ii) any child
1013 window of W must have the same window-side parameter as W.
1015 If the configuration is invalid, reset the window-side parameters
1016 of all windows on FRAME to nil."
1017 (let (left top right bottom none side parent parent-side
)
1018 (when (or (catch 'reset
1021 (setq side
(window-parameter window
'window-side
))
1022 (setq parent
(window-parent window
))
1024 (and parent
(window-parameter parent
'window-side
)))
1025 ;; The following `cond' seems a bit tedious, but I'd
1026 ;; rather stick to using just the stack.
1029 (when (not (eq parent-side side
))
1030 ;; A parent whose window-side is non-nil must
1031 ;; have a child with the same window-side.
1034 (when (window-buffer window
)
1035 ;; Record that we have at least one non-side,
1038 ((if (memq side
'(left top
))
1039 (window-prev-sibling window
)
1040 (window-next-sibling window
))
1041 ;; Left and top major side windows must not have a
1042 ;; previous sibling, right and bottom major side
1043 ;; windows must not have a next sibling.
1045 ;; Now check that there's no more than one major
1046 ;; window for any of left, top, right and bottom.
1048 (if left
(throw 'reset t
) (setq left t
)))
1050 (if top
(throw 'reset t
) (setq top t
)))
1052 (if right
(throw 'reset t
) (setq right t
)))
1054 (if bottom
(throw 'reset t
) (setq bottom t
)))
1058 ;; If there's a side window, there must be at least one
1060 (and (or left top right bottom
) (not none
)))
1063 (set-window-parameter window
'window-side nil
))
1066 (defun window--check (&optional frame
)
1067 "Check atomic and side windows on FRAME.
1068 FRAME defaults to the selected frame."
1069 (window--side-check frame
)
1070 (window--atom-check frame
))
1072 ;; Dumping frame/window contents.
1073 (defun window--dump-window (&optional window erase
)
1074 "Dump WINDOW to buffer *window-frame-dump*.
1075 WINDOW must be a valid window and defaults to the selected one.
1076 Optional argument ERASE non-nil means erase *window-frame-dump*
1077 before writing to it."
1078 (setq window
(window-normalize-window window
))
1079 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1080 (when erase
(erase-buffer))
1082 (format "%s parent: %s\n" window
(window-parent window
))
1083 (format "pixel left: %s top: %s size: %s x %s new: %s\n"
1084 (window-pixel-left window
) (window-pixel-top window
)
1085 (window-size window t t
) (window-size window nil t
)
1086 (window-new-pixel window
))
1087 (format "char left: %s top: %s size: %s x %s new: %s\n"
1088 (window-left-column window
) (window-top-line window
)
1089 (window-total-size window t
) (window-total-size window
)
1090 (window-new-total window
))
1091 (format "normal: %s x %s new: %s\n"
1092 (window-normal-size window t
) (window-normal-size window
)
1093 (window-new-normal window
)))
1094 (when (window-live-p window
)
1095 (let ((fringes (window-fringes window
))
1096 (margins (window-margins window
)))
1098 (format "body pixel: %s x %s char: %s x %s\n"
1099 (window-body-width window t
) (window-body-height window t
)
1100 (window-body-width window
) (window-body-height window
))
1101 (format "width left fringe: %s left margin: %s right margin: %s\n"
1102 (car fringes
) (or (car margins
) 0) (or (cdr margins
) 0))
1103 (format "width right fringe: %s scroll-bar: %s divider: %s\n"
1105 (window-scroll-bar-width window
)
1106 (window-right-divider-width window
))
1107 (format "height header-line: %s mode-line: %s divider: %s\n"
1108 (window-header-line-height window
)
1109 (window-mode-line-height window
)
1110 (window-bottom-divider-width window
)))))
1113 (defun window--dump-frame (&optional window-or-frame
)
1114 "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
1115 WINDOW-OR-FRAME can be a frame or a window and defaults to the
1116 selected frame. When WINDOW-OR-FRAME is a window, dump that
1117 window's frame. The buffer *window-frame-dump* is erased before
1121 ((or (not window-or-frame
)
1122 (frame-live-p window-or-frame
))
1123 (frame-root-window window-or-frame
))
1124 ((or (window-live-p window-or-frame
)
1125 (window-child window-or-frame
))
1128 (frame-root-window))))
1129 (frame (window-frame window
)))
1130 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1133 (format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
1134 (frame-pixel-width frame
) (frame-pixel-height frame
)
1135 (frame-total-cols frame
) (frame-text-lines frame
) ; (frame-total-lines frame)
1136 (frame-char-width frame
) (frame-char-height frame
))
1137 (format "frame text pixel: %s x %s cols/lines: %s x %s\n"
1138 (frame-text-width frame
) (frame-text-height frame
)
1139 (frame-text-cols frame
) (frame-text-lines frame
))
1140 (format "tool: %s scroll: %s fringe: %s border: %s right: %s bottom: %s\n\n"
1141 (if (fboundp 'tool-bar-height
)
1142 (tool-bar-height frame t
)
1144 (frame-scroll-bar-width frame
)
1145 (frame-fringe-width frame
)
1146 (frame-border-width frame
)
1147 (frame-right-divider-width frame
)
1148 (frame-bottom-divider-width frame
)))
1149 (walk-window-tree 'window--dump-window frame t t
))))
1152 (defun window-total-size (&optional window horizontal round
)
1153 "Return the total height or width of WINDOW.
1154 WINDOW must be a valid window and defaults to the selected one.
1156 If HORIZONTAL is omitted or nil, return the total height of
1157 WINDOW, in lines, like `window-total-height'. Otherwise return
1158 the total width, in columns, like `window-total-width'.
1160 Optional argument ROUND is handled as for `window-total-height'
1161 and `window-total-width'."
1163 (window-total-width window round
)
1164 (window-total-height window round
)))
1166 (defun window-size (&optional window horizontal pixelwise round
)
1167 "Return the height or width of WINDOW.
1168 WINDOW must be a valid window and defaults to the selected one.
1170 If HORIZONTAL is omitted or nil, return the total height of
1171 WINDOW, in lines, like `window-total-height'. Otherwise return
1172 the total width, in columns, like `window-total-width'.
1174 Optional argument PIXELWISE means return the pixel size of WINDOW
1175 like `window-pixel-height' and `window-pixel-width'.
1177 Optional argument ROUND is ignored if PIXELWISE is non-nil and
1178 handled as for `window-total-height' and `window-total-width'
1182 (window-pixel-width window
)
1183 (window-total-width window round
))
1185 (window-pixel-height window
)
1186 (window-total-height window round
))))
1188 (defvar window-size-fixed nil
1189 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
1190 If the value is `height', then only the window's height is fixed.
1191 If the value is `width', then only the window's width is fixed.
1192 Any other non-nil value fixes both the width and the height.
1194 Emacs won't change the size of any window displaying that buffer,
1195 unless it has no other choice (like when deleting a neighboring
1197 (make-variable-buffer-local 'window-size-fixed
)
1199 (defun window--size-ignore-p (window ignore
)
1200 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
1201 (if (window-valid-p ignore
) (eq window ignore
) ignore
))
1203 (defun window-safe-min-size (&optional window horizontal pixelwise
)
1204 "Return safe minimum size of WINDOW.
1205 WINDOW must be a valid window and defaults to the selected one.
1206 Optional argument HORIZONTAL non-nil means return the minimum
1207 number of columns of WINDOW; otherwise return the minimum number
1210 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1212 (setq window
(window-normalize-window window
))
1215 (* window-safe-min-width
1216 (frame-char-width (window-frame window
)))
1217 (* window-safe-min-height
1218 (frame-char-height (window-frame window
))))
1219 (if horizontal window-safe-min-width window-safe-min-height
)))
1221 (defun window-min-size (&optional window horizontal ignore pixelwise
)
1222 "Return the minimum size of WINDOW.
1223 WINDOW must be a valid window and defaults to the selected one.
1224 Optional argument HORIZONTAL non-nil means return the minimum
1225 number of columns of WINDOW; otherwise return the minimum number
1228 Optional argument IGNORE, if non-nil, means ignore restrictions
1229 imposed by fixed size windows, `window-min-height' or
1230 `window-min-width' settings. If IGNORE equals `safe', live
1231 windows may get as small as `window-safe-min-height' lines and
1232 `window-safe-min-width' columns. If IGNORE is a window, ignore
1233 restrictions for that window only. Any other non-nil value
1234 means ignore all of the above restrictions for all windows.
1236 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1239 (window-normalize-window window
) horizontal ignore pixelwise
))
1241 (defun window--min-size-1 (window horizontal ignore pixelwise
)
1242 "Internal function of `window-min-size'."
1243 (let ((sub (window-child window
)))
1246 ;; WINDOW is an internal window.
1247 (if (window-combined-p sub horizontal
)
1248 ;; The minimum size of an iso-combination is the sum of
1249 ;; the minimum sizes of its child windows.
1251 (setq value
(+ value
1253 sub horizontal ignore pixelwise
)))
1254 (setq sub
(window-right sub
)))
1255 ;; The minimum size of an ortho-combination is the maximum
1256 ;; of the minimum sizes of its child windows.
1258 (setq value
(max value
1260 sub horizontal ignore pixelwise
)))
1261 (setq sub
(window-right sub
))))
1263 (with-current-buffer (window-buffer window
)
1265 ((and (not (window--size-ignore-p window ignore
))
1266 (window-size-fixed-p window horizontal
))
1267 ;; The minimum size of a fixed size window is its size.
1268 (window-size window horizontal pixelwise
))
1269 ((or (eq ignore
'safe
) (eq ignore window
))
1270 ;; If IGNORE equals `safe' or WINDOW return the safe values.
1271 (window-safe-min-size window horizontal pixelwise
))
1273 ;; For the minimum width of a window take fringes and
1274 ;; scroll-bars into account. This is questionable and should
1275 ;; be removed as soon as we are able to split (and resize)
1276 ;; windows such that the new (or resized) windows can get a
1277 ;; size less than the user-specified `window-min-height' and
1278 ;; `window-min-width'.
1279 (let* ((char-size (frame-char-size window t
))
1280 (fringes (window-fringes window
))
1282 (+ (window-safe-min-size window t t
)
1283 (car fringes
) (cadr fringes
)
1284 (window-scroll-bar-width window
)
1285 (window-right-divider-width window
))))
1288 (if window-resize-pixelwise
1290 ;; Round up to next integral of columns.
1291 (* (ceiling pixel-width char-size
) char-size
))
1292 (if (window--size-ignore-p window ignore
)
1294 (window-min-pixel-width)))
1296 (ceiling pixel-width char-size
)
1297 (if (window--size-ignore-p window ignore
)
1299 window-min-width
)))))
1300 ((let ((char-size (frame-char-size window
))
1302 (+ (window-safe-min-size window nil t
)
1303 (window-header-line-height window
)
1304 (window-mode-line-height window
)
1305 (window-bottom-divider-width window
))))
1308 (if window-resize-pixelwise
1310 ;; Round up to next integral of lines.
1311 (* (ceiling pixel-height char-size
) char-size
))
1312 (if (window--size-ignore-p window ignore
)
1314 (window-min-pixel-height)))
1315 (max (ceiling pixel-height char-size
)
1316 (if (window--size-ignore-p window ignore
)
1318 window-min-height
))))))))))
1320 (defun window-sizable (window delta
&optional horizontal ignore pixelwise
)
1321 "Return DELTA if DELTA lines can be added to WINDOW.
1322 WINDOW must be a valid window and defaults to the selected one.
1323 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1324 columns can be added to WINDOW. A return value of zero means
1325 that no lines (or columns) can be added to WINDOW.
1327 This function looks only at WINDOW and, recursively, its child
1328 windows. The function `window-resizable' looks at other windows
1331 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1332 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1333 return the maximum value in the range 0..DELTA by which WINDOW
1336 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1337 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1338 return the minimum value in the range DELTA..0 by which WINDOW
1341 Optional argument IGNORE non-nil means ignore restrictions
1342 imposed by fixed size windows, `window-min-height' or
1343 `window-min-width' settings. If IGNORE equals `safe', live
1344 windows may get as small as `window-safe-min-height' lines and
1345 `window-safe-min-width' columns. If IGNORE is a window, ignore
1346 restrictions for that window only. Any other non-nil value means
1347 ignore all of the above restrictions for all windows.
1349 Optional argument PIXELWISE non-nil means interpret DELTA as
1351 (setq window
(window-normalize-window window
))
1354 (max (- (window-min-size window horizontal ignore pixelwise
)
1355 (window-size window horizontal pixelwise
))
1357 ((window--size-ignore-p window ignore
)
1360 (if (window-size-fixed-p window horizontal
)
1365 (defun window-sizable-p (window delta
&optional horizontal ignore pixelwise
)
1366 "Return t if WINDOW can be resized by DELTA lines.
1367 WINDOW must be a valid window and defaults to the selected one.
1368 For the meaning of the arguments of this function see the
1369 doc-string of `window-sizable'."
1370 (setq window
(window-normalize-window window
))
1372 (>= (window-sizable window delta horizontal ignore pixelwise
)
1374 (<= (window-sizable window delta horizontal ignore pixelwise
)
1377 (defun window--size-fixed-1 (window horizontal
)
1378 "Internal function for `window-size-fixed-p'."
1379 (let ((sub (window-child window
)))
1382 ;; WINDOW is an internal window.
1383 (if (window-combined-p sub horizontal
)
1384 ;; An iso-combination is fixed size if all its child
1385 ;; windows are fixed-size.
1388 (unless (window--size-fixed-1 sub horizontal
)
1389 ;; We found a non-fixed-size child window, so
1390 ;; WINDOW's size is not fixed.
1392 (setq sub
(window-right sub
)))
1393 ;; All child windows are fixed-size, so WINDOW's size is
1396 ;; An ortho-combination is fixed-size if at least one of its
1397 ;; child windows is fixed-size.
1399 (when (window--size-fixed-1 sub horizontal
)
1400 ;; We found a fixed-size child window, so WINDOW's size
1403 (setq sub
(window-right sub
))))
1404 ;; WINDOW is a live window.
1405 (with-current-buffer (window-buffer window
)
1407 (memq window-size-fixed
'(width t
))
1408 (memq window-size-fixed
'(height t
))))))))
1410 (defun window-size-fixed-p (&optional window horizontal
)
1411 "Return non-nil if WINDOW's height is fixed.
1412 WINDOW must be a valid window and defaults to the selected one.
1413 Optional argument HORIZONTAL non-nil means return non-nil if
1414 WINDOW's width is fixed.
1416 If this function returns nil, this does not necessarily mean that
1417 WINDOW can be resized in the desired direction. The function
1418 `window-resizable' can tell that."
1419 (window--size-fixed-1
1420 (window-normalize-window window
) horizontal
))
1422 (defun window--min-delta-1 (window delta
&optional horizontal ignore trail noup pixelwise
)
1423 "Internal function for `window-min-delta'."
1424 (if (not (window-parent window
))
1425 ;; If we can't go up, return zero.
1427 ;; Else try to find a non-fixed-size sibling of WINDOW.
1428 (let* ((parent (window-parent window
))
1429 (sub (window-child parent
)))
1431 (if (window-combined-p sub horizontal
)
1432 ;; In an iso-combination throw DELTA if we find at least one
1433 ;; child window and that window is either not fixed-size or
1434 ;; we can ignore fixed-sizeness.
1435 (let ((skip (eq trail
'after
)))
1439 (setq skip
(eq trail
'before
)))
1441 ((and (not (window--size-ignore-p window ignore
))
1442 (window-size-fixed-p sub horizontal
)))
1444 ;; We found a non-fixed-size child window.
1445 (throw 'done delta
)))
1446 (setq sub
(window-right sub
))))
1447 ;; In an ortho-combination set DELTA to the minimum value by
1448 ;; which other child windows can shrink.
1450 (unless (eq sub window
)
1453 (max (- (window-size sub horizontal pixelwise
'ceiling
)
1455 sub horizontal ignore pixelwise
))
1457 (setq sub
(window-right sub
))))
1460 (window--min-delta-1
1461 parent delta horizontal ignore trail nil pixelwise
))))))
1463 (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise
)
1464 "Return number of lines by which WINDOW can be shrunk.
1465 WINDOW must be a valid window and defaults to the selected one.
1466 Return zero if WINDOW cannot be shrunk.
1468 Optional argument HORIZONTAL non-nil means return number of
1469 columns by which WINDOW can be shrunk.
1471 Optional argument IGNORE non-nil means ignore restrictions
1472 imposed by fixed size windows, `window-min-height' or
1473 `window-min-width' settings. If IGNORE is a window, ignore
1474 restrictions for that window only. If IGNORE equals `safe',
1475 live windows may get as small as `window-safe-min-height' lines
1476 and `window-safe-min-width' columns. Any other non-nil value
1477 means ignore all of the above restrictions for all windows.
1479 Optional argument TRAIL restricts the windows that can be enlarged.
1480 If its value is `before', only windows to the left of or above WINDOW
1481 can be enlarged. If it is `after', only windows to the right of or
1482 below WINDOW can be enlarged.
1484 Optional argument NOUP non-nil means don't go up in the window
1485 tree, but try to enlarge windows within WINDOW's combination only.
1487 Optional argument NODOWN non-nil means don't check whether WINDOW
1488 itself (and its child windows) can be shrunk; check only whether
1489 at least one other window can be enlarged appropriately.
1491 Optional argument PIXELWISE non-nil means return number of pixels
1492 by which WINDOW can be shrunk."
1493 (setq window
(window-normalize-window window
))
1494 (let ((size (window-size window horizontal pixelwise
'floor
))
1495 (minimum (window-min-size window horizontal ignore pixelwise
)))
1498 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1499 (window--min-delta-1
1500 window size horizontal ignore trail noup pixelwise
))
1502 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1503 ;; there's nothing to recover.
1506 ;; Otherwise, try to recover whatever WINDOW is larger than its
1508 (window--min-delta-1
1509 window
(- size minimum
) horizontal ignore trail noup pixelwise
)))))
1511 (defun window--max-delta-1 (window delta
&optional horizontal ignore trail noup pixelwise
)
1512 "Internal function of `window-max-delta'."
1513 (if (not (window-parent window
))
1514 ;; Can't go up. Return DELTA.
1516 (let* ((parent (window-parent window
))
1517 (sub (window-child parent
)))
1519 (if (window-combined-p sub horizontal
)
1520 ;; For an iso-combination calculate how much we can get from
1521 ;; other child windows.
1522 (let ((skip (eq trail
'after
)))
1526 (setq skip
(eq trail
'before
)))
1532 (- (window-size sub horizontal pixelwise
'floor
)
1534 sub horizontal ignore pixelwise
))
1536 (setq sub
(window-right sub
))))
1537 ;; For an ortho-combination throw DELTA when at least one
1538 ;; child window is fixed-size.
1540 (when (and (not (eq sub window
))
1541 (not (window--size-ignore-p sub ignore
))
1542 (window-size-fixed-p sub horizontal
))
1543 (throw 'fixed delta
))
1544 (setq sub
(window-right sub
))))
1546 ;; When NOUP is nil, DELTA is all we can get.
1548 ;; Else try with parent of WINDOW, passing the DELTA we
1549 ;; recovered so far.
1550 (window--max-delta-1
1551 parent delta horizontal ignore trail nil pixelwise
))))))
1553 (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise
)
1554 "Return maximum number of lines by which WINDOW can be enlarged.
1555 WINDOW must be a valid window and defaults to the selected one.
1556 The return value is zero if WINDOW cannot be enlarged.
1558 Optional argument HORIZONTAL non-nil means return maximum number
1559 of columns by which WINDOW can be enlarged.
1561 Optional argument IGNORE non-nil means ignore restrictions
1562 imposed by fixed size windows, `window-min-height' or
1563 `window-min-width' settings. If IGNORE is a window, ignore
1564 restrictions for that window only. If IGNORE equals `safe',
1565 live windows may get as small as `window-safe-min-height' lines
1566 and `window-safe-min-width' columns. Any other non-nil value means
1567 ignore all of the above restrictions for all windows.
1569 Optional argument TRAIL restricts the windows that can be enlarged.
1570 If its value is `before', only windows to the left of or above WINDOW
1571 can be enlarged. If it is `after', only windows to the right of or
1572 below WINDOW can be enlarged.
1574 Optional argument NOUP non-nil means don't go up in the window
1575 tree but try to obtain the entire space from windows within
1576 WINDOW's combination.
1578 Optional argument NODOWN non-nil means do not check whether
1579 WINDOW itself (and its child windows) can be enlarged; check
1580 only whether other windows can be shrunk appropriately.
1582 Optional argument PIXELWISE non-nil means return number of
1583 pixels by which WINDOW can be enlarged."
1584 (setq window
(window-normalize-window window
))
1585 (if (and (not (window--size-ignore-p window ignore
))
1586 (not nodown
) (window-size-fixed-p window horizontal
))
1587 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1590 ;; WINDOW has no fixed size.
1591 (window--max-delta-1 window
0 horizontal ignore trail noup pixelwise
)))
1593 ;; Make NOUP also inhibit the min-size check.
1594 (defun window--resizable (window delta
&optional horizontal ignore trail noup nodown pixelwise
)
1595 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1596 WINDOW must be a valid window and defaults to the selected one.
1597 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1598 can be resized horizontally by DELTA columns. A return value of
1599 zero means that WINDOW is not resizable.
1601 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1602 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1603 return the maximum value in the range 0..DELTA by which WINDOW
1606 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1607 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1608 return the minimum value in the range DELTA..0 that can be used
1609 for shrinking WINDOW.
1611 Optional argument IGNORE non-nil means ignore restrictions
1612 imposed by fixed size windows, `window-min-height' or
1613 `window-min-width' settings. If IGNORE is a window, ignore
1614 restrictions for that window only. If IGNORE equals `safe',
1615 live windows may get as small as `window-safe-min-height' lines
1616 and `window-safe-min-width' columns. Any other non-nil value
1617 means ignore all of the above restrictions for all windows.
1619 Optional argument TRAIL `before' means only windows to the left
1620 of or below WINDOW can be shrunk. Optional argument TRAIL
1621 `after' means only windows to the right of or above WINDOW can be
1624 Optional argument NOUP non-nil means don't go up in the window
1625 tree but check only whether space can be obtained from (or given
1626 to) WINDOW's siblings.
1628 Optional argument NODOWN non-nil means don't go down in the
1629 window tree. This means do not check whether resizing would
1630 violate size restrictions of WINDOW or its child windows.
1632 Optional argument PIXELWISE non-nil means interpret DELTA as
1634 (setq window
(window-normalize-window window
))
1637 (max (- (window-min-delta
1638 window horizontal ignore trail noup nodown pixelwise
))
1641 (min (window-max-delta
1642 window horizontal ignore trail noup nodown pixelwise
)
1646 (defun window--resizable-p (window delta
&optional horizontal ignore trail noup nodown pixelwise
)
1647 "Return t if WINDOW can be resized vertically by DELTA lines.
1648 WINDOW must be a valid window and defaults to the selected one.
1649 For the meaning of the arguments of this function see the
1650 doc-string of `window--resizable'.
1652 Optional argument PIXELWISE non-nil means interpret DELTA as
1654 (setq window
(window-normalize-window window
))
1656 (>= (window--resizable
1657 window delta horizontal ignore trail noup nodown pixelwise
)
1659 (<= (window--resizable
1660 window delta horizontal ignore trail noup nodown pixelwise
)
1663 (defun window-resizable (window delta
&optional horizontal ignore pixelwise
)
1664 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1665 WINDOW must be a valid window and defaults to the selected one.
1666 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1667 can be resized horizontally by DELTA columns. A return value of
1668 zero means that WINDOW is not resizable.
1670 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1671 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1672 return the maximum value in the range 0..DELTA by which WINDOW
1675 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1676 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1677 return the minimum value in the range DELTA..0 that can be used
1678 for shrinking WINDOW.
1680 Optional argument IGNORE non-nil means ignore restrictions
1681 imposed by fixed size windows, `window-min-height' or
1682 `window-min-width' settings. If IGNORE is a window, ignore
1683 restrictions for that window only. If IGNORE equals `safe',
1684 live windows may get as small as `window-safe-min-height' lines
1685 and `window-safe-min-width' columns. Any other non-nil value
1686 means ignore all of the above restrictions for all windows.
1688 Optional argument PIXELWISE non-nil means interpret DELTA as
1690 (setq window
(window-normalize-window window
))
1691 (window--resizable window delta horizontal ignore nil nil nil pixelwise
))
1693 (defun window-resizable-p (window delta
&optional horizontal ignore pixelwise
)
1694 "Return t if WINDOW can be resized vertically by DELTA lines.
1695 WINDOW must be a valid window and defaults to the selected one.
1696 For the meaning of the arguments of this function see the
1697 doc-string of `window-resizable'."
1698 (setq window
(window-normalize-window window
))
1700 (>= (window--resizable
1701 window delta horizontal ignore nil nil nil pixelwise
)
1703 (<= (window--resizable
1704 window delta horizontal ignore nil nil nil pixelwise
)
1707 ;; Aliases of functions defined in window.c.
1708 (defalias 'window-height
'window-total-height
)
1709 (defalias 'window-width
'window-body-width
)
1711 ;; Eventually the following two should work pixelwise.
1713 ;; See discussion in bug#4543.
1714 (defun window-full-height-p (&optional window
)
1715 "Return t if WINDOW is as high as its containing frame.
1716 More precisely, return t if and only if the total height of
1717 WINDOW equals the total height of the root window of WINDOW's
1718 frame. WINDOW must be a valid window and defaults to the
1720 (setq window
(window-normalize-window window
))
1721 (= (window-pixel-height window
)
1722 (window-pixel-height (frame-root-window window
))))
1724 (defun window-full-width-p (&optional window
)
1725 "Return t if WINDOW is as wide as its containing frame.
1726 More precisely, return t if and only if the total width of WINDOW
1727 equals the total width of the root window of WINDOW's frame.
1728 WINDOW must be a valid window and defaults to the selected one."
1729 (setq window
(window-normalize-window window
))
1730 (= (window-pixel-width window
)
1731 (window-pixel-width (frame-root-window window
))))
1733 (defun window-body-size (&optional window horizontal pixelwise
)
1734 "Return the height or width of WINDOW's text area.
1735 WINDOW must be a live window and defaults to the selected one.
1737 If HORIZONTAL is omitted or nil, return the height of the text
1738 area, like `window-body-height'. Otherwise, return the width of
1739 the text area, like `window-body-width'. In either case, the
1740 optional argument PIXELWISE is passed to the functions."
1742 (window-body-width window pixelwise
)
1743 (window-body-height window pixelwise
)))
1745 (defun window-current-scroll-bars (&optional window
)
1746 "Return the current scroll bar settings for WINDOW.
1747 WINDOW must be a live window and defaults to the selected one.
1749 The return value is a cons cell (VERTICAL . HORIZONTAL) where
1750 VERTICAL specifies the current location of the vertical scroll
1751 bars (`left', `right', or nil), and HORIZONTAL specifies the
1752 current location of the horizontal scroll bars (`top', `bottom',
1755 Unlike `window-scroll-bars', this function reports the scroll bar
1756 type actually used, once frame defaults and `scroll-bar-mode' are
1757 taken into account."
1758 (setq window
(window-normalize-window window t
))
1759 (let ((vert (nth 2 (window-scroll-bars window
)))
1761 (when (or (eq vert t
) (eq hor t
))
1762 (let ((fcsb (frame-current-scroll-bars (window-frame window
))))
1764 (setq vert
(car fcsb
)))
1766 (setq hor
(cdr fcsb
)))))
1769 (defun walk-windows (fun &optional minibuf all-frames
)
1770 "Cycle through all live windows, calling FUN for each one.
1771 FUN must specify a function with a window as its sole argument.
1772 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1773 windows to include in the walk.
1775 MINIBUF t means include the minibuffer window even if the
1776 minibuffer is not active. MINIBUF nil or omitted means include
1777 the minibuffer window only if the minibuffer is active. Any
1778 other value means do not include the minibuffer window even if
1779 the minibuffer is active.
1781 ALL-FRAMES nil or omitted means consider all windows on the
1782 selected frame, plus the minibuffer window if specified by the
1783 MINIBUF argument. If the minibuffer counts, consider all windows
1784 on all frames that share that minibuffer too. The following
1785 non-nil values of ALL-FRAMES have special meanings:
1787 - t means consider all windows on all existing frames.
1789 - `visible' means consider all windows on all visible frames on
1790 the current terminal.
1792 - 0 (the number zero) means consider all windows on all visible
1793 and iconified frames on the current terminal.
1795 - A frame means consider all windows on that frame only.
1797 Anything else means consider all windows on the selected frame
1800 This function changes neither the order of recently selected
1801 windows nor the buffer list."
1802 ;; If we start from the minibuffer window, don't fail to come
1804 (when (window-minibuffer-p)
1806 ;; Make sure to not mess up the order of recently selected
1807 ;; windows. Use `save-selected-window' and `select-window'
1808 ;; with second argument non-nil for this purpose.
1809 (save-selected-window
1810 (when (framep all-frames
)
1811 (select-window (frame-first-window all-frames
) 'norecord
))
1812 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames
))
1813 (funcall fun walk-windows-window
))))
1815 (defun window-at-side-p (&optional window side
)
1816 "Return t if WINDOW is at SIDE of its containing frame.
1817 WINDOW must be a valid window and defaults to the selected one.
1818 SIDE can be any of the symbols `left', `top', `right' or
1819 `bottom'. The default value nil is handled like `bottom'."
1820 (setq window
(window-normalize-window window
))
1825 ((eq side
'right
) 2)
1826 ((memq side
'(bottom nil
)) 3))))
1827 (= (nth edge
(window-pixel-edges window
))
1828 (nth edge
(window-pixel-edges (frame-root-window window
))))))
1830 (defun window-at-side-list (&optional frame side
)
1831 "Return list of all windows on SIDE of FRAME.
1832 FRAME must be a live frame and defaults to the selected frame.
1833 SIDE can be any of the symbols `left', `top', `right' or
1834 `bottom'. The default value nil is handled like `bottom'."
1835 (setq frame
(window-normalize-frame frame
))
1839 (when (window-at-side-p window side
)
1840 (setq windows
(cons window windows
))))
1842 (nreverse windows
)))
1844 (defun window--in-direction-2 (window posn
&optional horizontal
)
1845 "Support function for `window-in-direction'."
1847 (let ((top (window-pixel-top window
)))
1850 (- posn top
(window-pixel-height window
))))
1851 (let ((left (window-pixel-left window
)))
1854 (- posn left
(window-pixel-width window
))))))
1856 ;; Predecessors to the below have been devised by Julian Assange in
1857 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
1858 ;; Neither of these allow to selectively ignore specific windows
1859 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
1861 (defun window-in-direction (direction &optional window ignore sign wrap mini
)
1862 "Return window in DIRECTION as seen from WINDOW.
1863 More precisely, return the nearest window in direction DIRECTION
1864 as seen from the position of `window-point' in window WINDOW.
1865 DIRECTION must be one of `above', `below', `left' or `right'.
1866 WINDOW must be a live window and defaults to the selected one.
1868 Do not return a window whose `no-other-window' parameter is
1869 non-nil. If the nearest window's `no-other-window' parameter is
1870 non-nil, try to find another window in the indicated direction.
1871 If, however, the optional argument IGNORE is non-nil, return that
1872 window even if its `no-other-window' parameter is non-nil.
1874 Optional argument SIGN a negative number means to use the right
1875 or bottom edge of WINDOW as reference position instead of
1876 `window-point'. SIGN a positive number means to use the left or
1877 top edge of WINDOW as reference position.
1879 Optional argument WRAP non-nil means to wrap DIRECTION around
1880 frame borders. This means to return for WINDOW at the top of the
1881 frame and DIRECTION `above' the minibuffer window if the frame
1882 has one, and a window at the bottom of the frame otherwise.
1884 Optional argument MINI nil means to return the minibuffer window
1885 if and only if it is currently active. MINI non-nil means to
1886 return the minibuffer window even when it's not active. However,
1887 if WRAP non-nil, always act as if MINI were nil.
1889 Return nil if no suitable window can be found."
1890 (setq window
(window-normalize-window window t
))
1891 (unless (memq direction
'(above below left right
))
1892 (error "Wrong direction %s" direction
))
1893 (let* ((frame (window-frame window
))
1894 (hor (memq direction
'(left right
)))
1896 (window-pixel-left window
)
1897 (window-pixel-top window
)))
1898 (last (+ first
(window-size window hor t
)))
1899 ;; The column / row value of `posn-at-point' can be nil for the
1900 ;; mini-window, guard against that.
1903 ((and (numberp sign
) (< sign
0))
1905 (1- (+ (window-pixel-top window
) (window-pixel-height window
)))
1906 (1- (+ (window-pixel-left window
) (window-pixel-width window
)))))
1907 ((and (numberp sign
) (> sign
0))
1909 (window-pixel-top window
)
1910 (window-pixel-left window
)))
1911 ((let ((posn-cons (nth 2 (posn-at-point (window-point window
) window
))))
1913 (+ (or (cdr posn-cons
) 1) (window-pixel-top window
))
1914 (+ (or (car posn-cons
) 1) (window-pixel-left window
)))))))
1917 ((eq direction
'below
) (frame-pixel-height frame
))
1918 ((eq direction
'right
) (frame-pixel-width frame
))
1920 (best-edge-2 best-edge
)
1921 (best-diff-2 (if hor
(frame-pixel-height frame
) (frame-pixel-width frame
)))
1922 best best-2 best-diff-2-new
)
1925 (let* ((w-top (window-pixel-top w
))
1926 (w-left (window-pixel-left w
)))
1929 ;; Ignore ourselves.
1930 (and (window-parameter w
'no-other-window
)
1931 ;; Ignore W unless IGNORE is non-nil.
1935 ((and (<= w-top posn
)
1936 (< posn
(+ w-top
(window-pixel-height w
))))
1937 ;; W is to the left or right of WINDOW and covers POSN.
1938 (when (or (and (eq direction
'left
)
1939 (or (and (<= w-left first
) (> w-left best-edge
))
1941 (window-at-side-p window
'left
)
1942 (window-at-side-p w
'right
))))
1943 (and (eq direction
'right
)
1944 (or (and (>= w-left last
) (< w-left best-edge
))
1946 (window-at-side-p window
'right
)
1947 (window-at-side-p w
'left
)))))
1948 (setq best-edge w-left
)
1950 ((and (or (and (eq direction
'left
)
1951 (<= (+ w-left
(window-pixel-width w
)) first
))
1952 (and (eq direction
'right
) (<= last w-left
)))
1953 ;; W is to the left or right of WINDOW but does not
1955 (setq best-diff-2-new
1956 (window--in-direction-2 w posn hor
))
1957 (or (< best-diff-2-new best-diff-2
)
1958 (and (= best-diff-2-new best-diff-2
)
1959 (if (eq direction
'left
)
1960 (> w-left best-edge-2
)
1961 (< w-left best-edge-2
)))))
1962 (setq best-edge-2 w-left
)
1963 (setq best-diff-2 best-diff-2-new
)
1965 ((and (<= w-left posn
)
1966 (< posn
(+ w-left
(window-pixel-width w
))))
1967 ;; W is above or below WINDOW and covers POSN.
1968 (when (or (and (eq direction
'above
)
1969 (or (and (<= w-top first
) (> w-top best-edge
))
1971 (window-at-side-p window
'top
)
1972 (if (active-minibuffer-window)
1973 (minibuffer-window-active-p w
)
1974 (window-at-side-p w
'bottom
)))))
1975 (and (eq direction
'below
)
1976 (or (and (>= w-top first
) (< w-top best-edge
))
1978 (if (active-minibuffer-window)
1979 (minibuffer-window-active-p window
)
1980 (window-at-side-p window
'bottom
))
1981 (window-at-side-p w
'top
)))))
1982 (setq best-edge w-top
)
1984 ((and (or (and (eq direction
'above
)
1985 (<= (+ w-top
(window-pixel-height w
)) first
))
1986 (and (eq direction
'below
) (<= last w-top
)))
1987 ;; W is above or below WINDOW but does not cover POSN.
1988 (setq best-diff-2-new
1989 (window--in-direction-2 w posn hor
))
1990 (or (< best-diff-2-new best-diff-2
)
1991 (and (= best-diff-2-new best-diff-2
)
1992 (if (eq direction
'above
)
1993 (> w-top best-edge-2
)
1994 (< w-top best-edge-2
)))))
1995 (setq best-edge-2 w-top
)
1996 (setq best-diff-2 best-diff-2-new
)
1998 frame nil
(and mini t
))
2001 (defun get-window-with-predicate (predicate &optional minibuf all-frames default
)
2002 "Return a live window satisfying PREDICATE.
2003 More precisely, cycle through all windows calling the function
2004 PREDICATE on each one of them with the window as its sole
2005 argument. Return the first window for which PREDICATE returns
2006 non-nil. Windows are scanned starting with the window following
2007 the selected window. If no window satisfies PREDICATE, return
2010 MINIBUF t means include the minibuffer window even if the
2011 minibuffer is not active. MINIBUF nil or omitted means include
2012 the minibuffer window only if the minibuffer is active. Any
2013 other value means do not include the minibuffer window even if
2014 the minibuffer is active.
2016 ALL-FRAMES nil or omitted means consider all windows on the selected
2017 frame, plus the minibuffer window if specified by the MINIBUF
2018 argument. If the minibuffer counts, consider all windows on all
2019 frames that share that minibuffer too. The following non-nil
2020 values of ALL-FRAMES have special meanings:
2022 - t means consider all windows on all existing frames.
2024 - `visible' means consider all windows on all visible frames on
2025 the current terminal.
2027 - 0 (the number zero) means consider all windows on all visible
2028 and iconified frames on the current terminal.
2030 - A frame means consider all windows on that frame only.
2032 Anything else means consider all windows on the selected frame
2035 (dolist (window (window-list-1
2036 (next-window nil minibuf all-frames
)
2037 minibuf all-frames
))
2038 (when (funcall predicate window
)
2039 (throw 'found window
)))
2042 (defalias 'some-window
'get-window-with-predicate
)
2044 (defun get-lru-window (&optional all-frames dedicated not-selected
)
2045 "Return the least recently used window on frames specified by ALL-FRAMES.
2046 Return a full-width window if possible. A minibuffer window is
2047 never a candidate. A dedicated window is never a candidate
2048 unless DEDICATED is non-nil, so if all windows are dedicated, the
2049 value is nil. Avoid returning the selected window if possible.
2050 Optional argument NOT-SELECTED non-nil means never return the
2053 The following non-nil values of the optional argument ALL-FRAMES
2054 have special meanings:
2056 - t means consider all windows on all existing frames.
2058 - `visible' means consider all windows on all visible frames on
2059 the current terminal.
2061 - 0 (the number zero) means consider all windows on all visible
2062 and iconified frames on the current terminal.
2064 - A frame means consider all windows on that frame only.
2066 Any other value of ALL-FRAMES means consider all windows on the
2067 selected frame and no others."
2068 (let (best-window best-time second-best-window second-best-time time
)
2069 (dolist (window (window-list-1 nil
'nomini all-frames
))
2070 (when (and (or dedicated
(not (window-dedicated-p window
)))
2071 (or (not not-selected
) (not (eq window
(selected-window)))))
2072 (setq time
(window-use-time window
))
2073 (if (or (eq window
(selected-window))
2074 (not (window-full-width-p window
)))
2075 (when (or (not second-best-time
) (< time second-best-time
))
2076 (setq second-best-time time
)
2077 (setq second-best-window window
))
2078 (when (or (not best-time
) (< time best-time
))
2079 (setq best-time time
)
2080 (setq best-window window
)))))
2081 (or best-window second-best-window
)))
2083 (defun get-mru-window (&optional all-frames dedicated not-selected
)
2084 "Return the most recently used window on frames specified by ALL-FRAMES.
2085 A minibuffer window is never a candidate. A dedicated window is
2086 never a candidate unless DEDICATED is non-nil, so if all windows
2087 are dedicated, the value is nil. Optional argument NOT-SELECTED
2088 non-nil means never return the selected window.
2090 The following non-nil values of the optional argument ALL-FRAMES
2091 have special meanings:
2093 - t means consider all windows on all existing frames.
2095 - `visible' means consider all windows on all visible frames on
2096 the current terminal.
2098 - 0 (the number zero) means consider all windows on all visible
2099 and iconified frames on the current terminal.
2101 - A frame means consider all windows on that frame only.
2103 Any other value of ALL-FRAMES means consider all windows on the
2104 selected frame and no others."
2105 (let (best-window best-time time
)
2106 (dolist (window (window-list-1 nil
'nomini all-frames
))
2107 (setq time
(window-use-time window
))
2108 (when (and (or dedicated
(not (window-dedicated-p window
)))
2109 (or (not not-selected
) (not (eq window
(selected-window))))
2110 (or (not best-time
) (> time best-time
)))
2111 (setq best-time time
)
2112 (setq best-window window
)))
2115 (defun get-largest-window (&optional all-frames dedicated not-selected
)
2116 "Return the largest window on frames specified by ALL-FRAMES.
2117 A minibuffer window is never a candidate. A dedicated window is
2118 never a candidate unless DEDICATED is non-nil, so if all windows
2119 are dedicated, the value is nil. Optional argument NOT-SELECTED
2120 non-nil means never return the selected window.
2122 The following non-nil values of the optional argument ALL-FRAMES
2123 have special meanings:
2125 - t means consider all windows on all existing frames.
2127 - `visible' means consider all windows on all visible frames on
2128 the current terminal.
2130 - 0 (the number zero) means consider all windows on all visible
2131 and iconified frames on the current terminal.
2133 - A frame means consider all windows on that frame only.
2135 Any other value of ALL-FRAMES means consider all windows on the
2136 selected frame and no others."
2139 (dolist (window (window-list-1 nil
'nomini all-frames
))
2140 (when (and (or dedicated
(not (window-dedicated-p window
)))
2141 (or (not not-selected
) (not (eq window
(selected-window)))))
2142 (setq size
(* (window-pixel-height window
)
2143 (window-pixel-width window
)))
2144 (when (> size best-size
)
2145 (setq best-size size
)
2146 (setq best-window window
))))
2149 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames
)
2150 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2151 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2152 and defaults to the current buffer. Windows are scanned starting
2153 with the selected window.
2155 MINIBUF t means include the minibuffer window even if the
2156 minibuffer is not active. MINIBUF nil or omitted means include
2157 the minibuffer window only if the minibuffer is active. Any
2158 other value means do not include the minibuffer window even if
2159 the minibuffer is active.
2161 ALL-FRAMES nil or omitted means consider all windows on the
2162 selected frame, plus the minibuffer window if specified by the
2163 MINIBUF argument. If the minibuffer counts, consider all windows
2164 on all frames that share that minibuffer too. The following
2165 non-nil values of ALL-FRAMES have special meanings:
2167 - t means consider all windows on all existing frames.
2169 - `visible' means consider all windows on all visible frames on
2170 the current terminal.
2172 - 0 (the number zero) means consider all windows on all visible
2173 and iconified frames on the current terminal.
2175 - A frame means consider all windows on that frame only.
2177 Anything else means consider all windows on the selected frame
2179 (let ((buffer (window-normalize-buffer buffer-or-name
))
2181 (dolist (window (window-list-1 (selected-window) minibuf all-frames
))
2182 (when (eq (window-buffer window
) buffer
)
2183 (setq windows
(cons window windows
))))
2184 (nreverse windows
)))
2186 (defun minibuffer-window-active-p (window)
2187 "Return t if WINDOW is the currently active minibuffer window."
2188 (eq window
(active-minibuffer-window)))
2190 (defun count-windows (&optional minibuf
)
2191 "Return the number of live windows on the selected frame.
2192 The optional argument MINIBUF specifies whether the minibuffer
2193 window shall be counted. See `walk-windows' for the precise
2194 meaning of this argument."
2195 (length (window-list-1 nil minibuf
)))
2197 ;;; Resizing windows.
2198 (defun window--size-to-pixel (window size
&optional horizontal pixelwise round-maybe
)
2199 "For WINDOW convert SIZE lines to pixels.
2200 SIZE is supposed to specify a height of WINDOW in terms of text
2201 lines. The return value is the number of pixels specifying that
2204 WINDOW must be a valid window. Optional argument HORIZONTAL
2205 non-nil means convert SIZE columns to pixels.
2207 Optional argument PIXELWISE non-nil means SIZE already specifies
2208 pixels but may have to be adjusted to a multiple of the character
2209 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2210 means round to the nearest multiple of the character size of
2211 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2212 (setq window
(window-normalize-window window
))
2213 (let ((char-size (frame-char-size window horizontal
)))
2215 (if (and round-maybe
(not window-resize-pixelwise
))
2216 (* (round size char-size
) char-size
)
2218 (* size char-size
))))
2220 (defun window--pixel-to-total-1 (window horizontal char-size
)
2221 "Subroutine of `window--pixel-to-total'."
2222 (let ((child (window-child window
)))
2223 (if (window-combination-p window horizontal
)
2224 ;; In an iso-combination distribute sizes proportionally.
2225 (let ((remainder (window-new-total window
))
2226 size best-child rem best-rem
)
2227 ;; Initialize total sizes to each child's floor.
2229 (setq size
(max (/ (window-size child horizontal t
) char-size
) 1))
2230 (set-window-new-total child size
)
2231 (setq remainder
(- remainder size
))
2232 (setq child
(window-next-sibling child
)))
2233 ;; Distribute remainder.
2234 (while (> remainder
0)
2235 (setq child
(window-last-child window
))
2236 (setq best-child nil
)
2239 (when (and (<= (window-new-total child
)
2240 (/ (window-size child horizontal t
) char-size
))
2241 (> (setq rem
(%
(window-size child horizontal t
)
2244 (setq best-child child
)
2245 (setq best-rem rem
))
2246 (setq child
(window-prev-sibling child
)))
2247 ;; We MUST have a best-child here.
2248 (set-window-new-total best-child
1 t
)
2249 (setq remainder
(1- remainder
)))
2251 (setq child
(window-child window
))
2253 (window--pixel-to-total-1 child horizontal char-size
)
2254 (setq child
(window-next-sibling child
))))
2255 ;; In an ortho-combination assign new sizes directly.
2256 (let ((size (window-new-total window
)))
2258 (set-window-new-total child size
)
2259 (window--pixel-to-total-1 child horizontal char-size
)
2260 (setq child
(window-next-sibling child
)))))))
2262 (defun window--pixel-to-total (&optional frame horizontal
)
2263 "On FRAME assign new total window heights from pixel heights.
2264 FRAME must be a live frame and defaults to the selected frame.
2266 Optional argument HORIZONTAL non-nil means assign new total
2267 window widths from pixel widths."
2268 (setq frame
(window-normalize-frame frame
))
2269 (let* ((char-size (frame-char-size frame horizontal
))
2270 (root (frame-root-window))
2271 (root-size (window-size root horizontal t
))
2272 ;; We have to care about the minibuffer window only if it
2273 ;; appears together with the root window on this frame.
2274 (mini (let ((mini (minibuffer-window frame
)))
2275 (and (eq (window-frame mini
) frame
)
2276 (not (eq mini root
)) mini
)))
2277 (mini-size (and mini
(window-size mini horizontal t
))))
2278 ;; We round the line/column sizes of windows here to the nearest
2279 ;; integer. In some cases this can make windows appear _larger_
2280 ;; than the containing frame (line/column-wise) because the latter's
2281 ;; sizes are not (yet) rounded. We might eventually fix that.
2282 (if (and mini
(not horizontal
))
2284 (set-window-new-total root
(max (/ root-size char-size
) 1))
2285 (set-window-new-total mini
(max (/ mini-size char-size
) 1))
2286 (setq lines
(- (round (+ root-size mini-size
) char-size
)
2287 (+ (window-new-total root
) (window-new-total mini
))))
2289 (if (>= (% root-size
(window-new-total root
))
2290 (% mini-size
(window-new-total mini
)))
2291 (set-window-new-total root
1 t
)
2292 (set-window-new-total mini
1 t
))
2293 (setq lines
(1- lines
))))
2294 (set-window-new-total root
(round root-size char-size
))
2296 ;; This is taken in the horizontal case only.
2297 (set-window-new-total mini
(round mini-size char-size
))))
2298 (unless (window-buffer root
)
2299 (window--pixel-to-total-1 root horizontal char-size
))
2300 ;; Apply the new sizes.
2301 (window-resize-apply-total frame horizontal
)))
2303 (defun window--resize-reset (&optional frame horizontal
)
2304 "Reset resize values for all windows on FRAME.
2305 FRAME defaults to the selected frame.
2307 This function stores the current value of `window-size' applied
2308 with argument HORIZONTAL in the new total size of all windows on
2309 FRAME. It also resets the new normal size of each of these
2311 (window--resize-reset-1
2312 (frame-root-window (window-normalize-frame frame
)) horizontal
))
2314 (defun window--resize-reset-1 (window horizontal
)
2315 "Internal function of `window--resize-reset'."
2316 ;; Register old size in the new total size.
2317 (set-window-new-pixel window
(window-size window horizontal t
))
2318 (set-window-new-total window
(window-size window horizontal
))
2319 ;; Reset new normal size.
2320 (set-window-new-normal window
)
2321 (when (window-child window
)
2322 (window--resize-reset-1 (window-child window
) horizontal
))
2323 (when (window-right window
)
2324 (window--resize-reset-1 (window-right window
) horizontal
)))
2326 ;; The following routine is used to manually resize the minibuffer
2327 ;; window and is currently used, for example, by ispell.el.
2328 (defun window--resize-mini-window (window delta
)
2329 "Resize minibuffer window WINDOW by DELTA pixels.
2330 If WINDOW cannot be resized by DELTA pixels make it as large (or
2331 as small) as possible, but don't signal an error."
2332 (when (window-minibuffer-p window
)
2333 (let* ((frame (window-frame window
))
2334 (root (frame-root-window frame
))
2335 (height (window-pixel-height window
))
2337 (- (window-pixel-height root
)
2338 (window-min-size root nil nil t
))))
2341 ((<= (+ height delta
) 0)
2342 (setq delta
(- (frame-char-height (window-frame window
)) height
)))
2343 ((> delta min-delta
)
2344 (setq delta min-delta
)))
2346 (unless (zerop delta
)
2348 (window--resize-reset frame
)
2349 ;; Ideally we should be able to resize just the last child of root
2350 ;; here. See the comment in `resize-root-window-vertically' for
2351 ;; why we do not do that.
2352 (window--resize-this-window root
(- delta
) nil nil t
)
2353 (set-window-new-pixel window
(+ height delta
))
2354 ;; The following routine catches the case where we want to resize
2355 ;; a minibuffer-only frame.
2356 (when (resize-mini-window-internal window
)
2357 (window--pixel-to-total frame
)
2358 (run-window-configuration-change-hook frame
))))))
2360 (defun window--resize-apply-p (frame &optional horizontal
)
2361 "Return t when a window on FRAME shall be resized vertically.
2362 Optional argument HORIZONTAL non-nil means return t when a window
2363 shall be resized horizontally."
2367 (unless (= (window-new-pixel window
)
2368 (window-size window horizontal t
))
2373 (defun window-resize (window delta
&optional horizontal ignore pixelwise
)
2374 "Resize WINDOW vertically by DELTA lines.
2375 WINDOW can be an arbitrary window and defaults to the selected
2376 one. An attempt to resize the root window of a frame will raise
2379 DELTA a positive number means WINDOW shall be enlarged by DELTA
2380 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2383 Optional argument HORIZONTAL non-nil means resize WINDOW
2384 horizontally by DELTA columns. In this case a positive DELTA
2385 means enlarge WINDOW by DELTA columns. DELTA negative means
2386 WINDOW shall be shrunk by -DELTA columns.
2388 Optional argument IGNORE non-nil means ignore restrictions
2389 imposed by fixed size windows, `window-min-height' or
2390 `window-min-width' settings. If IGNORE is a window, ignore
2391 restrictions for that window only. If IGNORE equals `safe',
2392 live windows may get as small as `window-safe-min-height' lines
2393 and `window-safe-min-width' columns. Any other non-nil value
2394 means ignore all of the above restrictions for all windows.
2396 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2399 This function resizes other windows proportionally and never
2400 deletes any windows. If you want to move only the low (right)
2401 edge of WINDOW consider using `adjust-window-trailing-edge'
2403 (setq window
(window-normalize-window window
))
2404 (let* ((frame (window-frame window
))
2405 (minibuffer-window (minibuffer-window frame
))
2407 (setq delta
(window--size-to-pixel
2408 window delta horizontal pixelwise t
))
2410 ((eq window
(frame-root-window frame
))
2411 (error "Cannot resize the root window of a frame"))
2412 ((window-minibuffer-p window
)
2414 (error "Cannot resize minibuffer window horizontally")
2415 (window--resize-mini-window window delta
)))
2416 ((and (not horizontal
)
2417 (window-full-height-p window
)
2418 (eq (window-frame minibuffer-window
) frame
)
2419 (or (not resize-mini-windows
)
2420 (eq minibuffer-window
(active-minibuffer-window))))
2421 ;; If WINDOW is full height and either `resize-mini-windows' is
2422 ;; nil or the minibuffer window is active, resize the minibuffer
2424 (window--resize-mini-window minibuffer-window
(- delta
)))
2425 ((window--resizable-p
2426 window delta horizontal ignore nil nil nil t
)
2427 (window--resize-reset frame horizontal
)
2428 (window--resize-this-window window delta horizontal ignore t
)
2429 (if (and (not window-combination-resize
)
2430 (window-combined-p window horizontal
)
2431 (setq sibling
(or (window-right window
) (window-left window
)))
2433 sibling
(- delta
) horizontal ignore t
))
2434 ;; If window-combination-resize is nil, WINDOW is part of an
2435 ;; iso-combination, and WINDOW's neighboring right or left
2436 ;; sibling can be resized as requested, resize that sibling.
2439 (window-size (window-parent window
) horizontal t
))))
2440 (window--resize-this-window sibling
(- delta
) horizontal nil t
)
2441 (set-window-new-normal
2442 window
(+ (window-normal-size window horizontal
)
2444 (set-window-new-normal
2445 sibling
(- (window-normal-size sibling horizontal
)
2447 ;; Otherwise, resize all other windows in the same combination.
2448 (window--resize-siblings window delta horizontal ignore
))
2449 (when (window--resize-apply-p frame horizontal
)
2450 (if (window-resize-apply frame horizontal
)
2452 (window--pixel-to-total frame horizontal
)
2453 (run-window-configuration-change-hook frame
))
2454 (error "Failed to apply resizing %s" window
))))
2456 (error "Cannot resize window %s" window
)))))
2458 (defun window-resize-no-error (window delta
&optional horizontal ignore pixelwise
)
2459 "Resize WINDOW vertically if it is resizable by DELTA lines.
2460 This function is like `window-resize' but does not signal an
2461 error when WINDOW cannot be resized. For the meaning of the
2462 optional arguments see the documentation of `window-resize'.
2464 Optional argument PIXELWISE non-nil means interpret DELTA as
2466 (when (window--resizable-p
2467 window delta horizontal ignore nil nil nil pixelwise
)
2468 (window-resize window delta horizontal ignore pixelwise
)))
2470 (defun window--resize-child-windows-skip-p (window)
2471 "Return non-nil if WINDOW shall be skipped by resizing routines."
2472 (memq (window-new-normal window
) '(ignore stuck skip
)))
2474 (defun window--resize-child-windows-normal (parent horizontal window this-delta
&optional trail other-delta
)
2475 "Recursively set new normal height of child windows of window PARENT.
2476 HORIZONTAL non-nil means set the new normal width of these
2477 windows. WINDOW specifies a child window of PARENT that has been
2478 resized by THIS-DELTA lines (columns).
2480 Optional argument TRAIL either `before' or `after' means set values
2481 only for windows before or after WINDOW. Optional argument
2482 OTHER-DELTA, a number, specifies that this many lines (columns)
2483 have been obtained from (or returned to) an ancestor window of
2484 PARENT in order to resize WINDOW."
2485 (let* ((delta-normal
2486 (if (and (= (- this-delta
)
2487 (window-size window horizontal t
))
2488 (zerop other-delta
))
2489 ;; When WINDOW gets deleted and we can return its entire
2490 ;; space to its siblings, use WINDOW's normal size as the
2492 (- (window-normal-size window horizontal
))
2493 ;; In any other case calculate the normal delta from the
2494 ;; relation of THIS-DELTA to the total size of PARENT.
2495 (/ (float this-delta
)
2496 (window-size parent horizontal t
))))
2497 (sub (window-child parent
))
2499 (skip (eq trail
'after
)))
2501 ;; Set parent-normal to the sum of the normal sizes of all child
2502 ;; windows of PARENT that shall be resized, excluding only WINDOW
2503 ;; and any windows specified by the optional TRAIL argument.
2507 (setq skip
(eq trail
'before
)))
2511 (+ parent-normal
(window-normal-size sub horizontal
)))))
2512 (setq sub
(window-right sub
)))
2514 ;; Set the new normal size of all child windows of PARENT from what
2515 ;; they should have contributed for recovering THIS-DELTA lines
2517 (setq sub
(window-child parent
))
2518 (setq skip
(eq trail
'after
))
2522 (setq skip
(eq trail
'before
)))
2525 (let ((old-normal (window-normal-size sub horizontal
)))
2526 (set-window-new-normal
2527 sub
(min 1.0 ; Don't get larger than 1.
2529 (* (/ old-normal parent-normal
)
2531 ;; Don't drop below 0.
2533 (setq sub
(window-right sub
)))
2535 (when (numberp other-delta
)
2536 ;; Set the new normal size of windows from what they should have
2537 ;; contributed for recovering OTHER-DELTA lines (columns).
2538 (setq delta-normal
(/ (float (window-size parent horizontal t
))
2539 (+ (window-size parent horizontal t
)
2541 (setq sub
(window-child parent
))
2542 (setq skip
(eq trail
'after
))
2546 (setq skip
(eq trail
'before
)))
2549 (set-window-new-normal
2550 sub
(min 1.0 ; Don't get larger than 1.
2551 (max (* (window-new-normal sub
) delta-normal
)
2552 ;; Don't drop below 0.
2554 (setq sub
(window-right sub
))))
2556 ;; Set the new normal size of WINDOW to what is left by the sum of
2557 ;; the normal sizes of its siblings.
2558 (set-window-new-normal
2561 (setq sub
(window-child parent
))
2565 ((not (numberp (window-new-normal sub
)))
2566 (setq sum
(+ sum
(window-normal-size sub horizontal
))))
2568 (setq sum
(+ sum
(window-new-normal sub
)))))
2569 (setq sub
(window-right sub
)))
2570 ;; Don't get larger than 1 or smaller than 0.
2571 (min 1.0 (max (- 1.0 sum
) 0.0))))))
2573 (defun window--resize-child-windows (parent delta
&optional horizontal window ignore trail edge char-size
)
2574 "Resize child windows of window PARENT vertically by DELTA pixels.
2575 PARENT must be a vertically combined internal window.
2577 Optional argument HORIZONTAL non-nil means resize child windows
2578 of PARENT horizontally by DELTA pixels. In this case PARENT must
2579 be a horizontally combined internal window.
2581 WINDOW, if specified, must denote a child window of PARENT that
2582 is resized by DELTA pixels.
2584 Optional argument IGNORE non-nil means ignore restrictions
2585 imposed by fixed size windows, `window-min-height' or
2586 `window-min-width' settings. If IGNORE equals `safe', live
2587 windows may get as small as `window-safe-min-height' lines and
2588 `window-safe-min-width' columns. If IGNORE is a window, ignore
2589 restrictions for that window only. Any other non-nil value means
2590 ignore all of the above restrictions for all windows.
2592 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2593 of windows that shall be resized. If TRAIL equals `before',
2594 resize only windows on the left or above EDGE. If TRAIL equals
2595 `after', resize only windows on the right or below EDGE. Also,
2596 preferably only resize windows adjacent to EDGE.
2598 If the optional argument CHAR-SIZE is a positive integer, it specifies
2599 the number of pixels by which windows are incrementally resized.
2600 If CHAR-SIZE is nil, this means to use the value of
2601 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2603 Return the symbol `normalized' if new normal sizes have been
2604 already set by this routine."
2605 (let* ((first (window-child parent
))
2606 (last (window-last-child parent
))
2607 (parent-total (+ (window-size parent horizontal t
)
2609 (char-size (or char-size
2610 (and window-resize-pixelwise
1)
2611 (frame-char-size window horizontal
)))
2612 sub best-window best-value best-delta
)
2614 (if (and edge
(memq trail
'(before after
))
2617 (while (and (window-right sub
)
2618 (or (and (eq trail
'before
)
2619 (not (window--resize-child-windows-skip-p
2620 (window-right sub
))))
2621 (and (eq trail
'after
)
2622 (window--resize-child-windows-skip-p sub
))))
2623 (setq sub
(window-right sub
)))
2626 (if (eq trail
'before
)
2627 (= (+ (window-pixel-left sub
) (window-pixel-width sub
))
2629 (= (window-pixel-left sub
) edge
))
2630 (if (eq trail
'before
)
2631 (= (+ (window-pixel-top sub
) (window-pixel-height sub
))
2633 (= (window-pixel-top sub
) edge
)))
2634 (window-sizable-p sub delta horizontal ignore t
))
2635 ;; Resize only windows adjacent to EDGE.
2637 (window--resize-this-window
2638 sub delta horizontal ignore t trail edge
)
2639 (if (and window
(eq (window-parent sub
) parent
))
2641 ;; Assign new normal sizes.
2642 (set-window-new-normal
2643 sub
(/ (float (window-new-pixel sub
)) parent-total
))
2644 (set-window-new-normal
2645 window
(- (window-normal-size window horizontal
)
2646 (- (window-new-normal sub
)
2647 (window-normal-size sub horizontal
)))))
2648 (window--resize-child-windows-normal
2649 parent horizontal sub
0 trail delta
))
2650 ;; Return 'normalized to notify `window--resize-siblings' that
2651 ;; normal sizes have been already set.
2653 ;; Resize all windows proportionally.
2657 ((or (window--resize-child-windows-skip-p sub
)
2658 ;; Ignore windows to skip and fixed-size child windows -
2659 ;; in the latter case make it a window to skip.
2661 (window-size-fixed-p sub horizontal
)
2662 (set-window-new-normal sub
'ignore
))))
2664 ;; When shrinking store the number of lines/cols we can get
2665 ;; from this window here together with the total/normal size
2667 (set-window-new-normal
2670 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
2671 (window-min-delta sub horizontal ignore trail t nil t
)
2672 (- (/ (float (window-size sub horizontal t
))
2674 (window-normal-size sub horizontal
)))))
2676 ;; When enlarging store the total/normal size factor only
2677 (set-window-new-normal
2679 (- (/ (float (window-size sub horizontal t
))
2681 (window-normal-size sub horizontal
)))))
2683 (setq sub
(window-left sub
)))
2687 ;; Shrink windows by delta.
2688 (setq best-window t
)
2689 (while (and best-window
(not (zerop delta
)))
2691 (setq best-window nil
)
2692 (setq best-value most-negative-fixnum
)
2694 (when (and (consp (window-new-normal sub
))
2695 (not (<= (car (window-new-normal sub
)) 0))
2696 (> (cdr (window-new-normal sub
)) best-value
))
2697 (setq best-window sub
)
2698 (setq best-value
(cdr (window-new-normal sub
))))
2700 (setq sub
(window-left sub
)))
2703 (setq best-delta
(min (car (window-new-normal best-window
))
2704 char-size
(- delta
)))
2705 (setq delta
(+ delta best-delta
))
2706 (set-window-new-pixel best-window
(- best-delta
) t
)
2707 (set-window-new-normal
2709 (if (= (car (window-new-normal best-window
)) best-delta
)
2710 'skip
; We can't shrink best-window any further.
2711 (cons (- (car (window-new-normal best-window
)) best-delta
)
2712 (- (/ (float (window-new-pixel best-window
))
2714 (window-normal-size best-window horizontal
))))))))
2716 ;; Enlarge windows by delta.
2717 (setq best-window t
)
2718 (while (and best-window
(not (zerop delta
)))
2720 (setq best-window nil
)
2721 (setq best-value most-positive-fixnum
)
2723 (when (and (numberp (window-new-normal sub
))
2724 (< (window-new-normal sub
) best-value
))
2725 (setq best-window sub
)
2726 (setq best-value
(window-new-normal sub
)))
2728 (setq sub
(window-left sub
)))
2731 (setq best-delta
(min delta char-size
))
2732 (setq delta
(- delta best-delta
))
2733 (set-window-new-pixel best-window best-delta t
)
2734 (set-window-new-normal
2736 (- (/ (float (window-new-pixel best-window
))
2738 (window-normal-size best-window horizontal
)))))))
2743 (when (or (consp (window-new-normal sub
))
2744 (numberp (window-new-normal sub
)))
2745 ;; Reset new normal size fields so `window-resize-apply'
2746 ;; won't use them to apply new sizes.
2747 (set-window-new-normal sub
))
2749 (unless (eq (window-new-normal sub
) 'ignore
)
2750 ;; Resize this window's child windows (back-engineering
2751 ;; delta from sub's old and new total sizes).
2752 (let ((delta (- (window-new-pixel sub
)
2753 (window-size sub horizontal t
))))
2754 (unless (and (zerop delta
) (not trail
))
2755 ;; For the TRAIL non-nil case we have to resize SUB
2756 ;; recursively even if it's size does not change.
2757 (window--resize-this-window
2758 sub delta horizontal ignore nil trail edge
))))
2759 (setq sub
(window-left sub
)))))))
2761 (defun window--resize-siblings (window delta
&optional horizontal ignore trail edge char-size
)
2762 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
2763 Optional argument HORIZONTAL non-nil means resize other windows
2764 when WINDOW is resized horizontally by DELTA pixels. WINDOW
2765 itself is not resized by this function.
2767 Optional argument IGNORE non-nil means ignore restrictions
2768 imposed by fixed size windows, `window-min-height' or
2769 `window-min-width' settings. If IGNORE equals `safe', live
2770 windows may get as small as `window-safe-min-height' lines and
2771 `window-safe-min-width' columns. If IGNORE is a window, ignore
2772 restrictions for that window only. Any other non-nil value means
2773 ignore all of the above restrictions for all windows.
2775 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2776 of windows that shall be resized. If TRAIL equals `before',
2777 resize only windows on the left or above EDGE. If TRAIL equals
2778 `after', resize only windows on the right or below EDGE. Also,
2779 preferably only resize windows adjacent to EDGE."
2780 (when (window-parent window
)
2781 (let* ((parent (window-parent window
))
2782 (sub (window-child parent
)))
2783 (if (window-combined-p sub horizontal
)
2784 ;; In an iso-combination try to extract DELTA from WINDOW's
2786 (let ((skip (eq trail
'after
))
2787 this-delta other-delta
)
2788 ;; Decide which windows shall be left alone.
2792 ;; Make sure WINDOW is left alone when
2793 ;; resizing its siblings.
2794 (set-window-new-normal sub
'ignore
)
2795 (setq skip
(eq trail
'before
)))
2797 ;; Make sure this sibling is left alone when
2798 ;; resizing its siblings.
2799 (set-window-new-normal sub
'ignore
))
2800 ((or (window--size-ignore-p sub ignore
)
2801 (not (window-size-fixed-p sub horizontal
)))
2802 ;; Set this-delta to t to signal that we found a sibling
2803 ;; of WINDOW whose size is not fixed.
2804 (setq this-delta t
)))
2806 (setq sub
(window-right sub
)))
2808 ;; Set this-delta to what we can get from WINDOW's siblings.
2809 (if (= (- delta
) (window-size window horizontal t
))
2810 ;; A deletion, presumably. We must handle this case
2811 ;; specially since `window--resizable' can't be used.
2813 ;; There's at least one resizable sibling we can
2814 ;; give WINDOW's size to.
2815 (setq this-delta delta
)
2816 ;; No resizable sibling exists.
2817 (setq this-delta
0))
2818 ;; Any other form of resizing.
2821 window delta horizontal ignore trail t nil t
)))
2823 ;; Set other-delta to what we still have to get from
2824 ;; ancestor windows of parent.
2825 (setq other-delta
(- delta this-delta
))
2826 (unless (zerop other-delta
)
2827 ;; Unless we got everything from WINDOW's siblings, PARENT
2828 ;; must be resized by other-delta lines or columns.
2829 (set-window-new-pixel parent other-delta
'add
))
2831 (if (zerop this-delta
)
2832 ;; We haven't got anything from WINDOW's siblings but we
2833 ;; must update the normal sizes to respect other-delta.
2834 (window--resize-child-windows-normal
2835 parent horizontal window this-delta trail other-delta
)
2836 ;; We did get something from WINDOW's siblings which means
2837 ;; we have to resize their child windows.
2838 (unless (eq (window--resize-child-windows
2839 parent
(- this-delta
) horizontal
2840 window ignore trail edge char-size
)
2841 ;; If `window--resize-child-windows' returns
2842 ;; 'normalized, this means it has set the
2843 ;; normal sizes already.
2845 ;; Set the normal sizes.
2846 (window--resize-child-windows-normal
2847 parent horizontal window this-delta trail other-delta
))
2848 ;; Set DELTA to what we still have to get from ancestor
2850 (setq delta other-delta
)))
2852 ;; In an ortho-combination all siblings of WINDOW must be
2853 ;; resized by DELTA.
2854 (set-window-new-pixel parent delta
'add
)
2856 (unless (eq sub window
)
2857 (window--resize-this-window
2858 sub delta horizontal ignore t
))
2859 (setq sub
(window-right sub
))))
2861 (unless (zerop delta
)
2863 (window--resize-siblings
2864 parent delta horizontal ignore trail edge char-size
)))))
2866 (defun window--resize-this-window (window delta
&optional horizontal ignore add trail edge char-size
)
2867 "Resize WINDOW vertically by DELTA pixels.
2868 Optional argument HORIZONTAL non-nil means resize WINDOW
2869 horizontally by DELTA pixels.
2871 Optional argument IGNORE non-nil means ignore restrictions
2872 imposed by fixed size windows, `window-min-height' or
2873 `window-min-width' settings. If IGNORE equals `safe', live
2874 windows may get as small as `window-safe-min-height' lines and
2875 `window-safe-min-width' columns. If IGNORE is a window, ignore
2876 restrictions for that window only. Any other non-nil value
2877 means ignore all of the above restrictions for all windows.
2879 Optional argument ADD non-nil means add DELTA to the new total
2882 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2883 of windows that shall be resized. If TRAIL equals `before',
2884 resize only windows on the left or above EDGE. If TRAIL equals
2885 `after', resize only windows on the right or below EDGE. Also,
2886 preferably only resize windows adjacent to EDGE.
2888 If the optional argument CHAR-SIZE is a positive integer, it specifies
2889 the number of pixels by which windows are incrementally resized.
2890 If CHAR-SIZE is nil, this means to use the value of
2891 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2893 This function recursively resizes WINDOW's child windows to fit the
2894 new size. Make sure that WINDOW is `window--resizable' before
2895 calling this function. Note that this function does not resize
2896 siblings of WINDOW or WINDOW's parent window. You have to
2897 eventually call `window-resize-apply' in order to make resizing
2898 actually take effect."
2900 ;; Add DELTA to the new total size of WINDOW.
2901 (set-window-new-pixel window delta t
))
2903 (let ((sub (window-child window
)))
2906 ((window-combined-p sub horizontal
)
2907 ;; In an iso-combination resize child windows according to their
2909 (window--resize-child-windows
2910 window delta horizontal nil ignore trail edge char-size
))
2911 ;; In an ortho-combination resize each child window by DELTA.
2914 (window--resize-this-window
2915 sub delta horizontal ignore t trail edge char-size
)
2916 (setq sub
(window-right sub
)))))))
2918 (defun window--resize-root-window (window delta horizontal ignore pixelwise
)
2919 "Resize root window WINDOW vertically by DELTA lines.
2920 HORIZONTAL non-nil means resize root window WINDOW horizontally
2923 IGNORE non-nil means ignore any restrictions imposed by fixed
2924 size windows, `window-min-height' or `window-min-width' settings.
2926 This function is only called by the frame resizing routines. It
2927 resizes windows proportionally and never deletes any windows."
2928 (when (and (windowp window
) (numberp delta
))
2932 (window--size-to-pixel window delta horizontal
))))
2933 (when (window-sizable-p window pixel-delta horizontal ignore t
)
2934 (window--resize-reset (window-frame window
) horizontal
)
2935 (window--resize-this-window
2936 window pixel-delta horizontal ignore t
)))))
2938 (defun window--resize-root-window-vertically (window delta pixelwise
)
2939 "Resize root window WINDOW vertically by DELTA lines.
2940 If DELTA is less than zero and we can't shrink WINDOW by DELTA
2941 lines, shrink it as much as possible. If DELTA is greater than
2942 zero, this function can resize fixed-size windows in order to
2943 recover the necessary lines. Return the number of lines that
2946 Third argument PIXELWISE non-nil means to interpret DELTA as
2947 pixels and return the number of pixels that were recovered.
2949 This function is called by the minibuffer window resizing
2951 (let* ((frame (window-frame window
))
2957 (* (frame-char-height frame
) delta
))
2961 ((zerop pixel-delta
))
2963 (setq pixel-delta
(window-sizable window pixel-delta nil nil pixelwise
))
2964 (window--resize-reset frame
)
2965 ;; When shrinking the root window, emulate an edge drag in order
2966 ;; to not resize other windows if we can avoid it (Bug#12419).
2967 (window--resize-this-window
2968 window pixel-delta nil ignore t
'before
2969 (+ (window-pixel-top window
) (window-pixel-height window
)))
2970 ;; Don't record new normal sizes to make sure that shrinking back
2971 ;; proportionally works as intended.
2973 (lambda (window) (set-window-new-normal window
'ignore
)) frame t
))
2975 (window--resize-reset frame
)
2976 (unless (window-sizable window pixel-delta nil nil pixelwise
)
2978 ;; When growing the root window, resize proportionally. This
2979 ;; should give windows back their original sizes (hopefully).
2980 (window--resize-this-window
2981 window pixel-delta nil ignore t
)))
2982 ;; Return the possibly adjusted DELTA.
2985 (/ pixel-delta
(frame-char-height frame
)))))
2987 (defun adjust-window-trailing-edge (window delta
&optional horizontal pixelwise
)
2988 "Move WINDOW's bottom edge by DELTA lines.
2989 Optional argument HORIZONTAL non-nil means move WINDOW's right
2990 edge by DELTA columns. WINDOW must be a valid window and
2991 defaults to the selected one.
2993 Optional argument PIXELWISE non-nil means interpret DELTA as
2996 If DELTA is greater than zero, move the edge downwards or to the
2997 right. If DELTA is less than zero, move the edge upwards or to
2998 the left. If the edge can't be moved by DELTA lines or columns,
2999 move it as far as possible in the desired direction."
3000 (setq window
(window-normalize-window window
))
3001 (let* ((frame (window-frame window
))
3002 (minibuffer-window (minibuffer-window frame
))
3004 left this-delta min-delta max-delta
)
3008 (setq delta
(* delta
(frame-char-size window horizontal
))))
3010 ;; Find the edge we want to move.
3011 (while (and (or (not (window-combined-p right horizontal
))
3012 (not (window-right right
)))
3013 (setq right
(window-parent right
))))
3015 ((and (not right
) (not horizontal
)
3016 ;; Resize the minibuffer window if it's on the same frame as
3017 ;; and immediately below WINDOW and it's either active or
3018 ;; `resize-mini-windows' is nil.
3019 (eq (window-frame minibuffer-window
) frame
)
3020 (= (nth 1 (window-pixel-edges minibuffer-window
))
3021 (nth 3 (window-pixel-edges window
)))
3022 (or (not resize-mini-windows
)
3023 (eq minibuffer-window
(active-minibuffer-window))))
3024 (window--resize-mini-window minibuffer-window
(- delta
)))
3025 ((or (not (setq left right
)) (not (setq right
(window-right right
))))
3027 (error "No window on the right of this one")
3028 (error "No window below this one")))
3030 ;; Set LEFT to the first resizable window on the left. This step is
3031 ;; needed to handle fixed-size windows.
3032 (while (and left
(window-size-fixed-p left horizontal
))
3034 (or (window-left left
)
3036 (while (and (setq left
(window-parent left
))
3037 (not (window-combined-p left horizontal
))))
3038 (window-left left
)))))
3041 (error "No resizable window on the left of this one")
3042 (error "No resizable window above this one")))
3044 ;; Set RIGHT to the first resizable window on the right. This step
3045 ;; is needed to handle fixed-size windows.
3046 (while (and right
(window-size-fixed-p right horizontal
))
3048 (or (window-right right
)
3050 (while (and (setq right
(window-parent right
))
3051 (not (window-combined-p right horizontal
))))
3052 (window-right right
)))))
3055 (error "No resizable window on the right of this one")
3056 (error "No resizable window below this one")))
3058 ;; LEFT and RIGHT (which might be both internal windows) are now the
3059 ;; two windows we want to resize.
3063 (window--max-delta-1
3064 left
0 horizontal nil
'after nil pixelwise
))
3066 (window--min-delta-1
3067 right
(- delta
) horizontal nil
'before nil pixelwise
))
3068 (when (or (< max-delta delta
) (> min-delta
(- delta
)))
3069 ;; We can't get the whole DELTA - move as far as possible.
3070 (setq delta
(min max-delta
(- min-delta
))))
3071 (unless (zerop delta
)
3073 (window--resize-reset frame horizontal
)
3074 ;; Try to enlarge LEFT first.
3075 (setq this-delta
(window--resizable
3076 left delta horizontal nil
'after nil nil pixelwise
))
3077 (unless (zerop this-delta
)
3078 (window--resize-this-window
3079 left this-delta horizontal nil t
'before
3081 (+ (window-pixel-left left
) (window-pixel-width left
))
3082 (+ (window-pixel-top left
) (window-pixel-height left
)))))
3083 ;; Shrink windows on right of LEFT.
3084 (window--resize-siblings
3085 left delta horizontal nil
'after
3087 (window-pixel-left right
)
3088 (window-pixel-top right
)))))
3091 (window--max-delta-1
3092 right
0 horizontal nil
'before nil pixelwise
))
3094 (window--min-delta-1
3095 left delta horizontal nil
'after nil pixelwise
))
3096 (when (or (< max-delta
(- delta
)) (> min-delta delta
))
3097 ;; We can't get the whole DELTA - move as far as possible.
3098 (setq delta
(max (- max-delta
) min-delta
)))
3099 (unless (zerop delta
)
3101 (window--resize-reset frame horizontal
)
3102 ;; Try to enlarge RIGHT.
3105 right
(- delta
) horizontal nil
'before nil nil pixelwise
))
3106 (unless (zerop this-delta
)
3107 (window--resize-this-window
3108 right this-delta horizontal nil t
'after
3110 (window-pixel-left right
)
3111 (window-pixel-top right
))))
3112 ;; Shrink windows on left of RIGHT.
3113 (window--resize-siblings
3114 right
(- delta
) horizontal nil
'before
3116 (+ (window-pixel-left left
) (window-pixel-width left
))
3117 (+ (window-pixel-top left
) (window-pixel-height left
)))))))
3118 (unless (zerop delta
)
3119 ;; Don't report an error in the standard case.
3120 (when (window--resize-apply-p frame horizontal
)
3121 (if (window-resize-apply frame horizontal
)
3123 (window--pixel-to-total frame horizontal
)
3124 (run-window-configuration-change-hook frame
))
3125 ;; But do report an error if applying the changes fails.
3126 (error "Failed adjusting window %s" window
))))))))
3128 (defun enlarge-window (delta &optional horizontal
)
3129 "Make the selected window DELTA lines taller.
3130 Interactively, if no argument is given, make the selected window
3131 one line taller. If optional argument HORIZONTAL is non-nil,
3132 make selected window wider by DELTA columns. If DELTA is
3133 negative, shrink selected window by -DELTA lines or columns."
3135 (let ((minibuffer-window (minibuffer-window)))
3138 ((window-size-fixed-p nil horizontal
)
3139 (error "Selected window has fixed size"))
3140 ((window-minibuffer-p)
3142 (error "Cannot resize minibuffer window horizontally")
3143 (window--resize-mini-window (selected-window) delta
)))
3144 ((and (not horizontal
)
3145 (window-full-height-p)
3146 (eq (window-frame minibuffer-window
) (selected-frame))
3147 (not resize-mini-windows
))
3148 ;; If the selected window is full height and `resize-mini-windows'
3149 ;; is nil, resize the minibuffer window.
3150 (window--resize-mini-window minibuffer-window
(- delta
)))
3151 ((window--resizable-p nil delta horizontal
)
3152 (window-resize nil delta horizontal
))
3156 (window-max-delta nil horizontal
)
3157 (- (window-min-delta nil horizontal
)))
3160 (defun shrink-window (delta &optional horizontal
)
3161 "Make the selected window DELTA lines smaller.
3162 Interactively, if no argument is given, make the selected window
3163 one line smaller. If optional argument HORIZONTAL is non-nil,
3164 make selected window narrower by DELTA columns. If DELTA is
3165 negative, enlarge selected window by -DELTA lines or columns.
3166 Also see the `window-min-height' variable."
3168 (let ((minibuffer-window (minibuffer-window)))
3171 ((window-size-fixed-p nil horizontal
)
3172 (error "Selected window has fixed size"))
3173 ((window-minibuffer-p)
3175 (error "Cannot resize minibuffer window horizontally")
3176 (window--resize-mini-window (selected-window) (- delta
))))
3177 ((and (not horizontal
)
3178 (window-full-height-p)
3179 (eq (window-frame minibuffer-window
) (selected-frame))
3180 (not resize-mini-windows
))
3181 ;; If the selected window is full height and `resize-mini-windows'
3182 ;; is nil, resize the minibuffer window.
3183 (window--resize-mini-window minibuffer-window delta
))
3184 ((window--resizable-p nil
(- delta
) horizontal
)
3185 (window-resize nil
(- delta
) horizontal
))
3189 (- (window-min-delta nil horizontal
))
3190 (window-max-delta nil horizontal
))
3193 (defun maximize-window (&optional window
)
3195 Make WINDOW as large as possible without deleting any windows.
3196 WINDOW must be a valid window and defaults to the selected one.
3198 If the option `window-resize-pixelwise' is non-nil maximize
3201 (setq window
(window-normalize-window window
))
3203 window
(window-max-delta window nil nil nil nil nil window-resize-pixelwise
)
3204 nil nil window-resize-pixelwise
)
3206 window
(window-max-delta window t nil nil nil nil window-resize-pixelwise
)
3207 t nil window-resize-pixelwise
))
3209 (defun minimize-window (&optional window
)
3211 Make WINDOW as small as possible without deleting any windows.
3212 WINDOW must be a valid window and defaults to the selected one.
3214 If the option `window-resize-pixelwise' is non-nil minimize
3217 (setq window
(window-normalize-window window
))
3220 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise
))
3221 nil nil window-resize-pixelwise
)
3224 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise
))
3225 t nil window-resize-pixelwise
))
3227 (defun frame-root-window-p (window)
3228 "Return non-nil if WINDOW is the root window of its frame."
3229 (eq window
(frame-root-window window
)))
3231 (defun window--subtree (window &optional next
)
3232 "Return window subtree rooted at WINDOW.
3233 Optional argument NEXT non-nil means include WINDOW's right
3234 siblings in the return value.
3236 See the documentation of `window-tree' for a description of the
3243 ((window-top-child window
)
3244 (cons t
(cons (window-edges window
)
3245 (window--subtree (window-top-child window
) t
))))
3246 ((window-left-child window
)
3247 (cons nil
(cons (window-edges window
)
3248 (window--subtree (window-left-child window
) t
))))
3251 (setq window
(when next
(window-next-sibling window
))))
3254 (defun window-tree (&optional frame
)
3255 "Return the window tree of frame FRAME.
3256 FRAME must be a live frame and defaults to the selected frame.
3257 The return value is a list of the form (ROOT MINI), where ROOT
3258 represents the window tree of the frame's root window, and MINI
3259 is the frame's minibuffer window.
3261 If the root window is not split, ROOT is the root window itself.
3262 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3263 for a horizontal split, and t for a vertical split. EDGES gives
3264 the combined size and position of the child windows in the split,
3265 and the rest of the elements are the child windows in the split.
3266 Each of the child windows may again be a window or a list
3267 representing a window split, and so on. EDGES is a list (LEFT
3268 TOP RIGHT BOTTOM) as returned by `window-edges'."
3269 (setq frame
(window-normalize-frame frame
))
3270 (window--subtree (frame-root-window frame
) t
))
3272 (defun other-window (count &optional all-frames
)
3273 "Select another window in cyclic ordering of windows.
3274 COUNT specifies the number of windows to skip, starting with the
3275 selected window, before making the selection. If COUNT is
3276 positive, skip COUNT windows forwards. If COUNT is negative,
3277 skip -COUNT windows backwards. COUNT zero means do not skip any
3278 window, so select the selected window. In an interactive call,
3279 COUNT is the numeric prefix argument. Return nil.
3281 If the `other-window' parameter of the selected window is a
3282 function and `ignore-window-parameters' is nil, call that
3283 function with the arguments COUNT and ALL-FRAMES.
3285 This function does not select a window whose `no-other-window'
3286 window parameter is non-nil.
3288 This function uses `next-window' for finding the window to
3289 select. The argument ALL-FRAMES has the same meaning as in
3290 `next-window', but the MINIBUF argument of `next-window' is
3291 always effectively nil."
3293 (let* ((window (selected-window))
3294 (function (and (not ignore-window-parameters
)
3295 (window-parameter window
'other-window
)))
3296 old-window old-count
)
3297 (if (functionp function
)
3298 (funcall function count all-frames
)
3299 ;; `next-window' and `previous-window' may return a window we are
3300 ;; not allowed to select. Hence we need an exit strategy in case
3301 ;; all windows are non-selectable.
3304 (setq window
(next-window window nil all-frames
))
3306 ((eq window old-window
)
3307 (when (= count old-count
)
3308 ;; Keep out of infinite loops. When COUNT has not changed
3309 ;; since we last looked at `window' we're probably in one.
3311 ((window-parameter window
'no-other-window
)
3313 ;; The first non-selectable window `next-window' got us:
3314 ;; Remember it and the current value of COUNT.
3315 (setq old-window window
)
3316 (setq old-count count
)))
3318 (setq count
(1- count
)))))
3320 (setq window
(previous-window window nil all-frames
))
3322 ((eq window old-window
)
3323 (when (= count old-count
)
3324 ;; Keep out of infinite loops. When COUNT has not changed
3325 ;; since we last looked at `window' we're probably in one.
3327 ((window-parameter window
'no-other-window
)
3329 ;; The first non-selectable window `previous-window' got
3330 ;; us: Remember it and the current value of COUNT.
3331 (setq old-window window
)
3332 (setq old-count count
)))
3334 (setq count
(1+ count
)))))
3336 (select-window window
)
3337 ;; Always return nil.
3340 ;; This should probably return non-nil when the selected window is part
3341 ;; of an atomic window whose root is the frame's root window.
3342 (defun one-window-p (&optional nomini all-frames
)
3343 "Return non-nil if the selected window is the only window.
3344 Optional arg NOMINI non-nil means don't count the minibuffer
3345 even if it is active. Otherwise, the minibuffer is counted
3348 Optional argument ALL-FRAMES specifies the set of frames to
3349 consider, see also `next-window'. ALL-FRAMES nil or omitted
3350 means consider windows on the selected frame only, plus the
3351 minibuffer window if specified by the NOMINI argument. If the
3352 minibuffer counts, consider all windows on all frames that share
3353 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3354 with a special meaning are:
3356 - t means consider all windows on all existing frames.
3358 - `visible' means consider all windows on all visible frames on
3359 the current terminal.
3361 - 0 (the number zero) means consider all windows on all visible
3362 and iconified frames on the current terminal.
3364 - A frame means consider all windows on that frame only.
3366 Anything else means consider all windows on the selected frame
3368 (let ((base-window (selected-window)))
3369 (if (and nomini
(eq base-window
(minibuffer-window)))
3370 (setq base-window
(next-window base-window
)))
3372 (next-window base-window
(if nomini
'arg
) all-frames
))))
3374 ;;; Deleting windows.
3375 (defun window-deletable-p (&optional window
)
3376 "Return t if WINDOW can be safely deleted from its frame.
3377 WINDOW must be a valid window and defaults to the selected one.
3378 Return 'frame if deleting WINDOW should also delete its frame."
3379 (setq window
(window-normalize-window window
))
3381 (unless (or ignore-window-parameters
3382 (eq (window-parameter window
'delete-window
) t
))
3383 ;; Handle atomicity.
3384 (when (window-parameter window
'window-atom
)
3385 (setq window
(window-atom-root window
))))
3387 (let ((frame (window-frame window
)))
3389 ((frame-root-window-p window
)
3390 ;; WINDOW's frame can be deleted only if there are other frames
3391 ;; on the same terminal, and it does not contain the active
3393 (unless (or (eq frame
(next-frame frame
0))
3394 ;; We can delete our frame only if no other frame
3395 ;; currently uses our minibuffer window.
3397 (dolist (other (frame-list))
3398 (when (and (not (eq other frame
))
3399 (eq (window-frame (minibuffer-window other
))
3402 (let ((minibuf (active-minibuffer-window)))
3403 (and minibuf
(eq frame
(window-frame minibuf
)))))
3405 ((or ignore-window-parameters
3406 (not (eq window
(window--major-non-side-window frame
))))
3407 ;; WINDOW can be deleted unless it is the major non-side window of
3411 (defun window--in-subtree-p (window root
)
3412 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
3413 (or (eq window root
)
3414 (let ((parent (window-parent window
)))
3417 (if (eq parent root
)
3419 (setq parent
(window-parent parent
))))))))
3421 (defun delete-window (&optional window
)
3423 WINDOW must be a valid window and defaults to the selected one.
3426 If the variable `ignore-window-parameters' is non-nil or the
3427 `delete-window' parameter of WINDOW equals t, do not process any
3428 parameters of WINDOW. Otherwise, if the `delete-window'
3429 parameter of WINDOW specifies a function, call that function with
3430 WINDOW as its sole argument and return the value returned by that
3433 Otherwise, if WINDOW is part of an atomic window, call
3434 `delete-window' with the root of the atomic window as its
3435 argument. Signal an error if WINDOW is either the only window on
3436 its frame, the last non-side window, or part of an atomic window
3437 that is its frame's root window."
3439 (setq window
(window-normalize-window window
))
3440 (let* ((frame (window-frame window
))
3441 (function (window-parameter window
'delete-window
))
3442 (parent (window-parent window
))
3444 (window--check frame
)
3446 ;; Handle window parameters.
3448 ;; Ignore window parameters if `ignore-window-parameters' tells
3449 ;; us so or `delete-window' equals t.
3450 ((or ignore-window-parameters
(eq function t
)))
3451 ((functionp function
)
3452 ;; The `delete-window' parameter specifies the function to call.
3453 ;; If that function is `ignore' nothing is done. It's up to the
3454 ;; function called here to avoid infinite recursion.
3455 (throw 'done
(funcall function window
)))
3456 ((and (window-parameter window
'window-atom
)
3457 (setq atom-root
(window-atom-root window
))
3458 (not (eq atom-root window
)))
3459 (if (eq atom-root
(frame-root-window frame
))
3460 (error "Root of atomic window is root window of its frame")
3461 (throw 'done
(delete-window atom-root
))))
3463 (error "Attempt to delete minibuffer or sole ordinary window"))
3464 ((eq window
(window--major-non-side-window frame
))
3465 (error "Attempt to delete last non-side window")))
3467 (let* ((horizontal (window-left-child parent
))
3468 (size (window-size window horizontal t
))
3470 (window--in-subtree-p (frame-selected-window frame
) window
))
3471 ;; Emacs 23 preferably gives WINDOW's space to its left
3473 (sibling (or (window-left window
) (window-right window
))))
3474 (window--resize-reset frame horizontal
)
3476 ((and (not window-combination-resize
)
3477 sibling
(window-sizable-p sibling size horizontal nil t
))
3478 ;; Resize WINDOW's sibling.
3479 (window--resize-this-window sibling size horizontal nil t
)
3480 (set-window-new-normal
3481 sibling
(+ (window-normal-size sibling horizontal
)
3482 (window-normal-size window horizontal
))))
3483 ((window--resizable-p window
(- size
) horizontal nil nil nil t t
)
3484 ;; Can do without resizing fixed-size windows.
3485 (window--resize-siblings window
(- size
) horizontal
))
3487 ;; Can't do without resizing fixed-size windows.
3488 (window--resize-siblings window
(- size
) horizontal t
)))
3489 ;; Actually delete WINDOW.
3490 (delete-window-internal window
)
3491 (window--pixel-to-total frame horizontal
)
3492 (when (and frame-selected
3494 (frame-selected-window frame
) 'no-other-window
))
3495 ;; `delete-window-internal' has selected a window that should
3496 ;; not be selected, fix this here.
3497 (other-window -
1 frame
))
3498 (run-window-configuration-change-hook frame
)
3499 (window--check frame
)
3500 ;; Always return nil.
3503 (defun delete-other-windows (&optional window
)
3504 "Make WINDOW fill its frame.
3505 WINDOW must be a valid window and defaults to the selected one.
3508 If the variable `ignore-window-parameters' is non-nil or the
3509 `delete-other-windows' parameter of WINDOW equals t, do not
3510 process any parameters of WINDOW. Otherwise, if the
3511 `delete-other-windows' parameter of WINDOW specifies a function,
3512 call that function with WINDOW as its sole argument and return
3513 the value returned by that function.
3515 Otherwise, if WINDOW is part of an atomic window, call this
3516 function with the root of the atomic window as its argument. If
3517 WINDOW is a non-side window, make WINDOW the only non-side window
3518 on the frame. Side windows are not deleted. If WINDOW is a side
3519 window signal an error."
3521 (setq window
(window-normalize-window window
))
3522 (let* ((frame (window-frame window
))
3523 (function (window-parameter window
'delete-other-windows
))
3524 (window-side (window-parameter window
'window-side
))
3525 atom-root side-main
)
3526 (window--check frame
)
3529 ;; Ignore window parameters if `ignore-window-parameters' is t or
3530 ;; `delete-other-windows' is t.
3531 ((or ignore-window-parameters
(eq function t
)))
3532 ((functionp function
)
3533 ;; The `delete-other-windows' parameter specifies the function
3534 ;; to call. If the function is `ignore' no windows are deleted.
3535 ;; It's up to the function called to avoid infinite recursion.
3536 (throw 'done
(funcall function window
)))
3537 ((and (window-parameter window
'window-atom
)
3538 (setq atom-root
(window-atom-root window
))
3539 (not (eq atom-root window
)))
3540 (if (eq atom-root
(frame-root-window frame
))
3541 (error "Root of atomic window is root window of its frame")
3542 (throw 'done
(delete-other-windows atom-root
))))
3543 ((memq window-side window-sides
)
3544 (error "Cannot make side window the only window"))
3545 ((and (window-minibuffer-p window
)
3546 (not (eq window
(frame-root-window window
))))
3547 (error "Can't expand minibuffer to full frame")))
3549 ;; If WINDOW is the major non-side window, do nothing.
3550 (if (window-with-parameter 'window-side
)
3551 (setq side-main
(window--major-non-side-window frame
))
3552 (setq side-main
(frame-root-window frame
)))
3553 (unless (eq window side-main
)
3554 (delete-other-windows-internal window side-main
)
3555 (run-window-configuration-change-hook frame
)
3556 (window--check frame
))
3557 ;; Always return nil.
3560 (defun delete-other-windows-vertically (&optional window
)
3561 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
3562 This may be a useful alternative binding for \\[delete-other-windows]
3563 if you often split windows horizontally."
3565 (let* ((window (or window
(selected-window)))
3566 (edges (window-edges window
))
3568 (while (not (eq (setq w
(next-window w
1)) window
))
3569 (let ((e (window-edges w
)))
3570 (when (and (= (car e
) (car edges
))
3571 (= (nth 2 e
) (nth 2 edges
)))
3573 (mapc 'delete-window delenda
)))
3575 ;;; Windows and buffers.
3577 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
3578 ;; for (1) determining which buffer to show in the window when its
3579 ;; buffer shall be buried or killed and (2) which buffer to show for
3580 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3582 ;; `prev-buffers' consists of <buffer, window-start, window-point>
3583 ;; triples. The entries on this list are ordered by the time their
3584 ;; buffer has been removed from the window, the most recently removed
3585 ;; buffer's entry being first. The window-start and window-point
3586 ;; components are `window-start' and `window-point' at the time the
3587 ;; buffer was removed from the window which implies that the entry must
3588 ;; be added when `set-window-buffer' removes the buffer from the window.
3590 ;; `next-buffers' is the list of buffers that have been replaced
3591 ;; recently by `switch-to-prev-buffer'. These buffers are the least
3592 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
3593 ;; candidates of `switch-to-next-buffer' to switch to. This list is
3594 ;; reset to nil by any action changing the window's buffer with the
3595 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
3596 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
3597 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
3599 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
3600 ;; if such a buffer was killed while the window was hidden within a
3601 ;; window configuration. Such killed buffers get removed whenever
3602 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
3604 ;; The following function is called by `set-window-buffer' _before_ it
3605 ;; replaces the buffer of the argument window with the new buffer.
3606 (defun record-window-buffer (&optional window
)
3607 "Record WINDOW's buffer.
3608 WINDOW must be a live window and defaults to the selected one."
3609 (let* ((window (window-normalize-window window t
))
3610 (buffer (window-buffer window
))
3611 (entry (assq buffer
(window-prev-buffers window
))))
3612 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
3613 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3614 (set-window-next-buffers window nil
)
3617 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
3618 (set-window-prev-buffers
3619 window
(assq-delete-all buffer
(window-prev-buffers window
))))
3621 ;; Don't record insignificant buffers.
3622 (unless (eq (aref (buffer-name buffer
) 0) ?\s
)
3623 ;; Add an entry for buffer to WINDOW's previous buffers.
3624 (with-current-buffer buffer
3625 (let ((start (window-start window
))
3626 (point (window-point window
)))
3630 ;; We have an entry, update marker positions.
3631 (list (set-marker (nth 1 entry
) start
)
3632 (set-marker (nth 2 entry
) point
))
3633 ;; Make new markers.
3634 (list (copy-marker start
)
3636 ;; Preserve window-point-insertion-type
3638 point window-point-insertion-type
)))))
3639 (set-window-prev-buffers
3640 window
(cons entry
(window-prev-buffers window
)))))
3642 (run-hooks 'buffer-list-update-hook
))))
3644 (defun unrecord-window-buffer (&optional window buffer
)
3645 "Unrecord BUFFER in WINDOW.
3646 WINDOW must be a live window and defaults to the selected one.
3647 BUFFER must be a live buffer and defaults to the buffer of
3649 (let* ((window (window-normalize-window window t
))
3650 (buffer (or buffer
(window-buffer window
))))
3651 (set-window-prev-buffers
3652 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3653 (set-window-next-buffers
3654 window
(delq buffer
(window-next-buffers window
)))))
3656 (defun set-window-buffer-start-and-point (window buffer
&optional start point
)
3657 "Set WINDOW's buffer to BUFFER.
3658 WINDOW must be a live window and defaults to the selected one.
3659 Optional argument START non-nil means set WINDOW's start position
3660 to START. Optional argument POINT non-nil means set WINDOW's
3661 point to POINT. If WINDOW is selected this also sets BUFFER's
3662 `point' to POINT. If WINDOW is selected and the buffer it showed
3663 before was current this also makes BUFFER the current buffer."
3664 (setq window
(window-normalize-window window t
))
3665 (let ((selected (eq window
(selected-window)))
3666 (current (eq (window-buffer window
) (current-buffer))))
3667 (set-window-buffer window buffer
)
3668 (when (and selected current
)
3669 (set-buffer buffer
))
3671 ;; Don't force window-start here (even if POINT is nil).
3672 (set-window-start window start t
))
3674 (set-window-point window point
))))
3676 (defcustom switch-to-visible-buffer t
3677 "If non-nil, allow switching to an already visible buffer.
3678 If this variable is non-nil, `switch-to-prev-buffer' and
3679 `switch-to-next-buffer' may switch to an already visible buffer
3680 provided the buffer was shown before in the window specified as
3681 argument to those functions. If this variable is nil,
3682 `switch-to-prev-buffer' and `switch-to-next-buffer' always try to
3683 avoid switching to a buffer that is already visible in another
3684 window on the same frame."
3689 (defun switch-to-prev-buffer (&optional window bury-or-kill
)
3690 "In WINDOW switch to previous buffer.
3691 WINDOW must be a live window and defaults to the selected one.
3692 Return the buffer switched to, nil if no suitable buffer could be
3695 Optional argument BURY-OR-KILL non-nil means the buffer currently
3696 shown in WINDOW is about to be buried or killed and consequently
3697 shall not be switched to in future invocations of this command.
3699 As a special case, if BURY-OR-KILL equals `append', this means to
3700 move the buffer to the end of WINDOW's previous buffers list so a
3701 future invocation of `switch-to-prev-buffer' less likely switches
3704 (let* ((window (window-normalize-window window t
))
3705 (frame (window-frame window
))
3706 (old-buffer (window-buffer window
))
3707 ;; Save this since it's destroyed by `set-window-buffer'.
3708 (next-buffers (window-next-buffers window
))
3709 (pred (frame-parameter frame
'buffer-predicate
))
3710 entry new-buffer killed-buffers visible
)
3711 (when (window-minibuffer-p window
)
3712 ;; Don't switch in minibuffer window.
3713 (unless (setq window
(minibuffer-selected-window))
3714 (error "Window %s is a minibuffer window" window
)))
3716 (when (window-dedicated-p window
)
3717 ;; Don't switch in dedicated window.
3718 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3721 ;; Scan WINDOW's previous buffers first, skipping entries of next
3723 (dolist (entry (window-prev-buffers window
))
3724 (when (and (setq new-buffer
(car entry
))
3725 (or (buffer-live-p new-buffer
)
3726 (not (setq killed-buffers
3727 (cons new-buffer killed-buffers
))))
3728 (not (eq new-buffer old-buffer
))
3729 (or (null pred
) (funcall pred new-buffer
))
3730 ;; When BURY-OR-KILL is nil, avoid switching to a
3731 ;; buffer in WINDOW's next buffers list.
3732 (or bury-or-kill
(not (memq new-buffer next-buffers
))))
3733 (if (and (not switch-to-visible-buffer
)
3734 (get-buffer-window new-buffer frame
))
3735 ;; Try to avoid showing a buffer visible in some other
3737 (setq visible new-buffer
)
3738 (set-window-buffer-start-and-point
3739 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3741 ;; Scan reverted buffer list of WINDOW's frame next, skipping
3742 ;; entries of next buffers. Note that when we bury or kill a
3743 ;; buffer we don't reverse the global buffer list to avoid showing
3744 ;; a buried buffer instead. Otherwise, we must reverse the global
3745 ;; buffer list in order to make sure that switching to the
3746 ;; previous/next buffer traverse it in opposite directions.
3747 (dolist (buffer (if bury-or-kill
3749 (nreverse (buffer-list frame
))))
3750 (when (and (buffer-live-p buffer
)
3751 (not (eq buffer old-buffer
))
3752 (or (null pred
) (funcall pred buffer
))
3753 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3754 (or bury-or-kill
(not (memq buffer next-buffers
))))
3755 (if (get-buffer-window buffer frame
)
3756 ;; Try to avoid showing a buffer visible in some other window.
3758 (setq visible buffer
))
3759 (setq new-buffer buffer
)
3760 (set-window-buffer-start-and-point window new-buffer
)
3762 (unless bury-or-kill
3763 ;; Scan reverted next buffers last (must not use nreverse
3765 (dolist (buffer (reverse next-buffers
))
3766 ;; Actually, buffer _must_ be live here since otherwise it
3767 ;; would have been caught in the scan of previous buffers.
3768 (when (and (or (buffer-live-p buffer
)
3769 (not (setq killed-buffers
3770 (cons buffer killed-buffers
))))
3771 (not (eq buffer old-buffer
))
3772 (or (null pred
) (funcall pred buffer
))
3773 (setq entry
(assq buffer
(window-prev-buffers window
))))
3774 (setq new-buffer buffer
)
3775 (set-window-buffer-start-and-point
3776 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3779 ;; Show a buffer visible in another window.
3781 (setq new-buffer visible
)
3782 (set-window-buffer-start-and-point window new-buffer
)))
3785 (let ((entry (and (eq bury-or-kill
'append
)
3786 (assq old-buffer
(window-prev-buffers window
)))))
3787 ;; Remove `old-buffer' from WINDOW's previous and (restored list
3788 ;; of) next buffers.
3789 (set-window-prev-buffers
3790 window
(assq-delete-all old-buffer
(window-prev-buffers window
)))
3791 (set-window-next-buffers window
(delq old-buffer next-buffers
))
3793 ;; Append old-buffer's entry to list of WINDOW's previous
3794 ;; buffers so it's less likely to get switched to soon but
3795 ;; `display-buffer-in-previous-window' can nevertheless find
3797 (set-window-prev-buffers
3798 window
(append (window-prev-buffers window
) (list entry
)))))
3799 ;; Move `old-buffer' to head of WINDOW's restored list of next
3801 (set-window-next-buffers
3802 window
(cons old-buffer
(delq old-buffer next-buffers
))))
3804 ;; Remove killed buffers from WINDOW's previous and next buffers.
3805 (when killed-buffers
3806 (dolist (buffer killed-buffers
)
3807 (set-window-prev-buffers
3808 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3809 (set-window-next-buffers
3810 window
(delq buffer
(window-next-buffers window
)))))
3812 ;; Return new-buffer.
3815 (defun switch-to-next-buffer (&optional window
)
3816 "In WINDOW switch to next buffer.
3817 WINDOW must be a live window and defaults to the selected one.
3818 Return the buffer switched to, nil if no suitable buffer could be
3821 (let* ((window (window-normalize-window window t
))
3822 (frame (window-frame window
))
3823 (old-buffer (window-buffer window
))
3824 (next-buffers (window-next-buffers window
))
3825 (pred (frame-parameter frame
'buffer-predicate
))
3826 new-buffer entry killed-buffers visible
)
3827 (when (window-minibuffer-p window
)
3828 ;; Don't switch in minibuffer window.
3829 (unless (setq window
(minibuffer-selected-window))
3830 (error "Window %s is a minibuffer window" window
)))
3832 (when (window-dedicated-p window
)
3833 ;; Don't switch in dedicated window.
3834 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3837 ;; Scan WINDOW's next buffers first.
3838 (dolist (buffer next-buffers
)
3839 (when (and (or (buffer-live-p buffer
)
3840 (not (setq killed-buffers
3841 (cons buffer killed-buffers
))))
3842 (not (eq buffer old-buffer
))
3843 (or (null pred
) (funcall pred buffer
))
3844 (setq entry
(assq buffer
(window-prev-buffers window
))))
3845 (setq new-buffer buffer
)
3846 (set-window-buffer-start-and-point
3847 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3849 ;; Scan the buffer list of WINDOW's frame next, skipping previous
3851 (dolist (buffer (buffer-list frame
))
3852 (when (and (buffer-live-p buffer
)
3853 (not (eq buffer old-buffer
))
3854 (or (null pred
) (funcall pred buffer
))
3855 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3856 (not (assq buffer
(window-prev-buffers window
))))
3857 (if (get-buffer-window buffer frame
)
3858 ;; Try to avoid showing a buffer visible in some other window.
3859 (setq visible buffer
)
3860 (setq new-buffer buffer
)
3861 (set-window-buffer-start-and-point window new-buffer
)
3863 ;; Scan WINDOW's reverted previous buffers last (must not use
3865 (dolist (entry (reverse (window-prev-buffers window
)))
3866 (when (and (setq new-buffer
(car entry
))
3867 (or (buffer-live-p new-buffer
)
3868 (not (setq killed-buffers
3869 (cons new-buffer killed-buffers
))))
3870 (not (eq new-buffer old-buffer
))
3871 (or (null pred
) (funcall pred new-buffer
)))
3872 (if (and (not switch-to-visible-buffer
)
3873 (get-buffer-window new-buffer frame
))
3874 ;; Try to avoid showing a buffer visible in some other window.
3876 (setq visible new-buffer
))
3877 (set-window-buffer-start-and-point
3878 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3881 ;; Show a buffer visible in another window.
3883 (setq new-buffer visible
)
3884 (set-window-buffer-start-and-point window new-buffer
)))
3886 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
3887 (set-window-next-buffers window
(delq new-buffer next-buffers
))
3889 ;; Remove killed buffers from WINDOW's previous and next buffers.
3890 (when killed-buffers
3891 (dolist (buffer killed-buffers
)
3892 (set-window-prev-buffers
3893 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3894 (set-window-next-buffers
3895 window
(delq buffer
(window-next-buffers window
)))))
3897 ;; Return new-buffer.
3900 (defun get-next-valid-buffer (list &optional buffer visible-ok frame
)
3901 "Search LIST for a valid buffer to display in FRAME.
3902 Return nil when all buffers in LIST are undesirable for display,
3903 otherwise return the first suitable buffer in LIST.
3905 Buffers not visible in windows are preferred to visible buffers,
3906 unless VISIBLE-OK is non-nil.
3907 If the optional argument FRAME is nil, it defaults to the selected frame.
3908 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
3909 ;; This logic is more or less copied from other-buffer.
3910 (setq frame
(or frame
(selected-frame)))
3911 (let ((pred (frame-parameter frame
'buffer-predicate
))
3913 (while (and (not found
) list
)
3914 (setq buf
(car list
))
3915 (if (and (not (eq buffer buf
))
3917 (or (null pred
) (funcall pred buf
))
3918 (not (eq (aref (buffer-name buf
) 0) ?\s
))
3919 (or visible-ok
(null (get-buffer-window buf
'visible
))))
3921 (setq list
(cdr list
))))
3924 (defun last-buffer (&optional buffer visible-ok frame
)
3925 "Return the last buffer in FRAME's buffer list.
3926 If BUFFER is the last buffer, return the preceding buffer
3927 instead. Buffers not visible in windows are preferred to visible
3928 buffers, unless optional argument VISIBLE-OK is non-nil.
3929 Optional third argument FRAME nil or omitted means use the
3930 selected frame's buffer list. If no such buffer exists, return
3931 the buffer `*scratch*', creating it if necessary."
3932 (setq frame
(or frame
(selected-frame)))
3933 (or (get-next-valid-buffer (nreverse (buffer-list frame
))
3934 buffer visible-ok frame
)
3935 (get-buffer "*scratch*")
3936 (let ((scratch (get-buffer-create "*scratch*")))
3937 (set-buffer-major-mode scratch
)
3940 (defcustom frame-auto-hide-function
#'iconify-frame
3941 "Function called to automatically hide frames.
3942 The function is called with one argument - a frame.
3944 Functions affected by this option are those that bury a buffer
3945 shown in a separate frame like `quit-window' and `bury-buffer'."
3946 :type
'(choice (const :tag
"Iconify" iconify-frame
)
3947 (const :tag
"Delete" delete-frame
)
3948 (const :tag
"Do nothing" ignore
)
3954 (defun window--delete (&optional window dedicated-only kill
)
3955 "Delete WINDOW if possible.
3956 WINDOW must be a live window and defaults to the selected one.
3957 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
3958 only if it's dedicated to its buffer. Optional argument KILL
3959 means the buffer shown in window will be killed. Return non-nil
3960 if WINDOW gets deleted or its frame is auto-hidden."
3961 (setq window
(window-normalize-window window t
))
3962 (unless (and dedicated-only
(not (window-dedicated-p window
)))
3963 (let ((deletable (window-deletable-p window
)))
3965 ((eq deletable
'frame
)
3966 (let ((frame (window-frame window
)))
3969 (delete-frame frame
))
3970 ((functionp frame-auto-hide-function
)
3971 (funcall frame-auto-hide-function frame
))))
3974 (delete-window window
)
3977 (defun bury-buffer (&optional buffer-or-name
)
3978 "Put BUFFER-OR-NAME at the end of the list of all buffers.
3979 There it is the least likely candidate for `other-buffer' to
3980 return; thus, the least likely buffer for \\[switch-to-buffer] to
3983 You can specify a buffer name as BUFFER-OR-NAME, or an actual
3984 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
3985 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
3986 remove the current buffer from the selected window if it is
3989 (let* ((buffer (window-normalize-buffer buffer-or-name
)))
3990 ;; If `buffer-or-name' is not on the selected frame we unrecord it
3991 ;; although it's not "here" (call it a feature).
3992 (bury-buffer-internal buffer
)
3993 ;; Handle case where `buffer-or-name' is nil and the current buffer
3994 ;; is shown in the selected window.
3996 ((or buffer-or-name
(not (eq buffer
(window-buffer)))))
3997 ((window--delete nil t
))
3999 ;; Switch to another buffer in window.
4000 (set-window-dedicated-p nil nil
)
4001 (switch-to-prev-buffer nil
'bury
)))
4003 ;; Always return nil.
4006 (defun unbury-buffer ()
4007 "Switch to the last buffer in the buffer list."
4009 (switch-to-buffer (last-buffer)))
4011 (defun next-buffer ()
4012 "In selected window switch to next buffer."
4015 ((window-minibuffer-p)
4016 (error "Cannot switch buffers in minibuffer window"))
4017 ((eq (window-dedicated-p) t
)
4018 (error "Window is strongly dedicated to its buffer"))
4020 (switch-to-next-buffer))))
4022 (defun previous-buffer ()
4023 "In selected window switch to previous buffer."
4026 ((window-minibuffer-p)
4027 (error "Cannot switch buffers in minibuffer window"))
4028 ((eq (window-dedicated-p) t
)
4029 (error "Window is strongly dedicated to its buffer"))
4031 (switch-to-prev-buffer))))
4033 (defun delete-windows-on (&optional buffer-or-name frame
)
4034 "Delete all windows showing BUFFER-OR-NAME.
4035 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4036 and defaults to the current buffer.
4038 The following non-nil values of the optional argument FRAME
4039 have special meanings:
4041 - t means consider all windows on the selected frame only.
4043 - `visible' means consider all windows on all visible frames on
4044 the current terminal.
4046 - 0 (the number zero) means consider all windows on all visible
4047 and iconified frames on the current terminal.
4049 - A frame means consider all windows on that frame only.
4051 Any other value of FRAME means consider all windows on all
4054 When a window showing BUFFER-OR-NAME is dedicated and the only
4055 window of its frame, that frame is deleted when there are other
4057 (interactive "BDelete windows on (buffer):\nP")
4058 (let ((buffer (window-normalize-buffer buffer-or-name
))
4059 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4060 ;; `window-list-1' based function.
4061 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
4062 (dolist (window (window-list-1 nil nil all-frames
))
4063 (if (eq (window-buffer window
) buffer
)
4064 (let ((deletable (window-deletable-p window
)))
4066 ((and (eq deletable
'frame
) (window-dedicated-p window
))
4067 ;; Delete frame if and only if window is dedicated.
4068 (delete-frame (window-frame window
)))
4071 (delete-window window
))
4073 ;; In window switch to previous buffer.
4074 (set-window-dedicated-p window nil
)
4075 (switch-to-prev-buffer window
'bury
))))
4076 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4077 (unrecord-window-buffer window buffer
)))))
4079 (defun replace-buffer-in-windows (&optional buffer-or-name
)
4080 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
4081 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4082 and defaults to the current buffer.
4084 When a window showing BUFFER-OR-NAME is dedicated, that window is
4085 deleted. If that window is the only window on its frame, the
4086 frame is deleted too when there are other frames left. If there
4087 are no other frames left, some other buffer is displayed in that
4090 This function removes the buffer denoted by BUFFER-OR-NAME from
4091 all window-local buffer lists."
4092 (interactive "bBuffer to replace: ")
4093 (let ((buffer (window-normalize-buffer buffer-or-name
)))
4094 (dolist (window (window-list-1 nil nil t
))
4095 (if (eq (window-buffer window
) buffer
)
4096 (unless (window--delete window t t
)
4097 ;; Switch to another buffer in window.
4098 (set-window-dedicated-p window nil
)
4099 (switch-to-prev-buffer window
'kill
))
4100 ;; Unrecord BUFFER in WINDOW.
4101 (unrecord-window-buffer window buffer
)))))
4103 (defun quit-restore-window (&optional window bury-or-kill
)
4104 "Quit WINDOW and deal with its buffer.
4105 WINDOW must be a live window and defaults to the selected one.
4107 According to information stored in WINDOW's `quit-restore' window
4108 parameter either (1) delete WINDOW and its frame, (2) delete
4109 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4110 or (4) make WINDOW display some other buffer than the present
4111 one. If non-nil, reset `quit-restore' parameter to nil.
4113 Optional second argument BURY-OR-KILL tells how to proceed with
4114 the buffer of WINDOW. The following values are handled:
4116 `nil' means to not handle the buffer in a particular way. This
4117 means that if WINDOW is not deleted by this function, invoking
4118 `switch-to-prev-buffer' will usually show the buffer again.
4120 `append' means that if WINDOW is not deleted, move its buffer to
4121 the end of WINDOW's previous buffers so it's less likely that a
4122 future invocation of `switch-to-prev-buffer' will switch to it.
4123 Also, move the buffer to the end of the frame's buffer list.
4125 `bury' means that if WINDOW is not deleted, remove its buffer
4126 from WINDOW'S list of previous buffers. Also, move the buffer
4127 to the end of the frame's buffer list. This value provides the
4128 most reliable remedy to not have `switch-to-prev-buffer' switch
4129 to this buffer again without killing the buffer.
4131 `kill' means to kill WINDOW's buffer."
4132 (setq window
(window-normalize-window window t
))
4133 (let* ((buffer (window-buffer window
))
4134 (quit-restore (window-parameter window
'quit-restore
))
4136 (let* ((prev-buffers (window-prev-buffers window
))
4137 (prev-buffer (caar prev-buffers
)))
4138 (and (or (not (eq prev-buffer buffer
))
4139 (and (cdr prev-buffers
)
4140 (not (eq (setq prev-buffer
(cadr prev-buffers
))
4145 ((and (not prev-buffer
)
4146 (or (eq (nth 1 quit-restore
) 'frame
)
4147 (and (eq (nth 1 quit-restore
) 'window
)
4148 ;; If the window has been created on an existing
4149 ;; frame and ended up as the sole window on that
4150 ;; frame, do not delete it (Bug#12764).
4151 (not (eq window
(frame-root-window window
)))))
4152 (eq (nth 3 quit-restore
) buffer
)
4153 ;; Delete WINDOW if possible.
4154 (window--delete window nil
(eq bury-or-kill
'kill
)))
4155 ;; If the previously selected window is still alive, select it.
4156 (when (window-live-p (nth 2 quit-restore
))
4157 (select-window (nth 2 quit-restore
))))
4158 ((and (listp (setq quad
(nth 1 quit-restore
)))
4159 (buffer-live-p (car quad
))
4160 (eq (nth 3 quit-restore
) buffer
))
4161 ;; Show another buffer stored in quit-restore parameter.
4162 (when (and (integerp (nth 3 quad
))
4163 (/= (nth 3 quad
) (window-total-height window
)))
4164 ;; Try to resize WINDOW to its old height but don't signal an
4167 (window-resize window
(- (nth 3 quad
) (window-total-height window
)))
4169 (set-window-dedicated-p window nil
)
4170 ;; Restore WINDOW's previous buffer, start and point position.
4171 (set-window-buffer-start-and-point
4172 window
(nth 0 quad
) (nth 1 quad
) (nth 2 quad
))
4173 ;; Deal with the buffer we just removed from WINDOW.
4174 (setq entry
(and (eq bury-or-kill
'append
)
4175 (assq buffer
(window-prev-buffers window
))))
4177 ;; Remove buffer from WINDOW's previous and next buffers.
4178 (set-window-prev-buffers
4179 window
(assq-delete-all buffer
(window-prev-buffers window
)))
4180 (set-window-next-buffers
4181 window
(delq buffer
(window-next-buffers window
))))
4183 ;; Append old buffer's entry to list of WINDOW's previous
4184 ;; buffers so it's less likely to get switched to soon but
4185 ;; `display-buffer-in-previous-window' can nevertheless find it.
4186 (set-window-prev-buffers
4187 window
(append (window-prev-buffers window
) (list entry
))))
4188 ;; Reset the quit-restore parameter.
4189 (set-window-parameter window
'quit-restore nil
)
4190 ;; Select old window.
4191 (when (window-live-p (nth 2 quit-restore
))
4192 (select-window (nth 2 quit-restore
))))
4194 ;; Show some other buffer in WINDOW and reset the quit-restore
4196 (set-window-parameter window
'quit-restore nil
)
4197 ;; Make sure that WINDOW is no more dedicated.
4198 (set-window-dedicated-p window nil
)
4199 (switch-to-prev-buffer window bury-or-kill
)))
4201 ;; Deal with the buffer.
4203 ((not (buffer-live-p buffer
)))
4204 ((eq bury-or-kill
'kill
)
4205 (kill-buffer buffer
))
4207 (bury-buffer-internal buffer
)))))
4209 (defun quit-window (&optional kill window
)
4210 "Quit WINDOW and bury its buffer.
4211 WINDOW must be a live window and defaults to the selected one.
4212 With prefix argument KILL non-nil, kill the buffer instead of
4215 According to information stored in WINDOW's `quit-restore' window
4216 parameter either (1) delete WINDOW and its frame, (2) delete
4217 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4218 or (4) make WINDOW display some other buffer than the present
4219 one. If non-nil, reset `quit-restore' parameter to nil."
4221 (quit-restore-window window
(if kill
'kill
'bury
)))
4223 (defun quit-windows-on (&optional buffer-or-name kill frame
)
4224 "Quit all windows showing BUFFER-OR-NAME.
4225 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4226 and defaults to the current buffer. Optional argument KILL
4227 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4228 BUFFER-OR-NAME. Optional argument FRAME is handled as by
4229 `delete-windows-on'.
4231 This function calls `quit-window' on all candidate windows
4232 showing BUFFER-OR-NAME."
4233 (interactive "BQuit windows on (buffer):\nP")
4234 (let ((buffer (window-normalize-buffer buffer-or-name
))
4235 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4236 ;; `window-list-1' based function.
4237 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
4238 (dolist (window (window-list-1 nil nil all-frames
))
4239 (if (eq (window-buffer window
) buffer
)
4240 (quit-window kill window
)
4241 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4242 (unrecord-window-buffer window buffer
)))))
4244 ;;; Splitting windows.
4245 (defun window-split-min-size (&optional horizontal pixelwise
)
4246 "Return minimum height of any window when splitting windows.
4247 Optional argument HORIZONTAL non-nil means return minimum width."
4251 (window-min-pixel-width)
4252 (window-min-pixel-height)))
4254 (max window-min-width window-safe-min-width
))
4256 (max window-min-height window-safe-min-height
))))
4258 (defun split-window (&optional window size side pixelwise
)
4259 "Make a new window adjacent to WINDOW.
4260 WINDOW must be a valid window and defaults to the selected one.
4261 Return the new window which is always a live window.
4263 Optional argument SIZE a positive number means make WINDOW SIZE
4264 lines or columns tall. If SIZE is negative, make the new window
4265 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
4266 absolute value can be less than `window-min-height' or
4267 `window-min-width'; so this command can make a new window as
4268 small as one line or two columns. SIZE defaults to half of
4271 Optional third argument SIDE nil (or `below') specifies that the
4272 new window shall be located below WINDOW. SIDE `above' means the
4273 new window shall be located above WINDOW. In both cases SIZE
4274 specifies the new number of lines for WINDOW (or the new window
4275 if SIZE is negative) including space reserved for the mode and/or
4278 SIDE t (or `right') specifies that the new window shall be
4279 located on the right side of WINDOW. SIDE `left' means the new
4280 window shall be located on the left of WINDOW. In both cases
4281 SIZE specifies the new number of columns for WINDOW (or the new
4282 window provided SIZE is negative) including space reserved for
4283 fringes and the scrollbar or a divider column. Any other non-nil
4284 value for SIDE is currently handled like t (or `right').
4286 PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
4288 If the variable `ignore-window-parameters' is non-nil or the
4289 `split-window' parameter of WINDOW equals t, do not process any
4290 parameters of WINDOW. Otherwise, if the `split-window' parameter
4291 of WINDOW specifies a function, call that function with all three
4292 arguments and return the value returned by that function.
4294 Otherwise, if WINDOW is part of an atomic window, \"split\" the
4295 root of that atomic window. The new window does not become a
4296 member of that atomic window.
4298 If WINDOW is live, properties of the new window like margins and
4299 scrollbars are inherited from WINDOW. If WINDOW is an internal
4300 window, these properties as well as the buffer displayed in the
4301 new window are inherited from the window selected on WINDOW's
4302 frame. The selected window is not changed by this function."
4303 (setq window
(window-normalize-window window
))
4306 ((memq side
'(below above right left
)) side
)
4308 (horizontal (not (memq side
'(below above
))))
4309 (frame (window-frame window
))
4310 (parent (window-parent window
))
4311 (function (window-parameter window
'split-window
))
4312 (window-side (window-parameter window
'window-side
))
4313 ;; Rebind the following two variables since in some cases we
4314 ;; have to override their value.
4315 (window-combination-limit window-combination-limit
)
4316 (window-combination-resize window-combination-resize
)
4317 (char-size (frame-char-size window horizontal
))
4319 (when (numberp size
)
4320 (window--size-to-pixel window size horizontal pixelwise t
)))
4322 (window--check frame
)
4325 ;; Ignore window parameters if either `ignore-window-parameters'
4326 ;; is t or the `split-window' parameter equals t.
4327 ((or ignore-window-parameters
(eq function t
)))
4328 ((functionp function
)
4329 ;; The `split-window' parameter specifies the function to call.
4330 ;; If that function is `ignore', do nothing.
4331 (throw 'done
(funcall function window size side
)))
4332 ;; If WINDOW is part of an atomic window, split the root window
4333 ;; of that atomic window instead.
4334 ((and (window-parameter window
'window-atom
)
4335 (setq atom-root
(window-atom-root window
))
4336 (not (eq atom-root window
)))
4337 (throw 'done
(split-window atom-root size side pixelwise
)))
4338 ;; If WINDOW is a side window or its first or last child is a
4339 ;; side window, throw an error unless `window-combination-resize'
4341 ((and (not (eq window-combination-resize
'side
))
4342 (or (window-parameter window
'window-side
)
4343 (and (window-child window
)
4344 (or (window-parameter
4345 (window-child window
) 'window-side
)
4347 (window-last-child window
) 'window-side
)))))
4348 (error "Cannot split side window or parent of side window"))
4349 ;; If `window-combination-resize' is 'side and window has a side
4350 ;; window sibling, bind `window-combination-limit' to t.
4351 ((and (not (eq window-combination-resize
'side
))
4352 (or (and (window-prev-sibling window
)
4354 (window-prev-sibling window
) 'window-side
))
4355 (and (window-next-sibling window
)
4357 (window-next-sibling window
) 'window-side
))))
4358 (setq window-combination-limit t
)))
4360 ;; If `window-combination-resize' is t and SIZE is non-negative,
4361 ;; bind `window-combination-limit' to t.
4362 (when (and (eq window-combination-resize t
)
4363 pixel-size
(> pixel-size
0))
4364 (setq window-combination-limit t
))
4366 (let* ((parent-pixel-size
4367 ;; `parent-pixel-size' is the pixel size of WINDOW's
4368 ;; parent, provided it has one.
4369 (when parent
(window-size parent horizontal t
)))
4370 ;; `resize' non-nil means we are supposed to resize other
4371 ;; windows in WINDOW's combination.
4373 (and window-combination-resize
4374 (or (window-parameter window
'window-side
)
4375 (not (eq window-combination-resize
'side
)))
4376 (not (eq window-combination-limit t
))
4377 ;; Resize makes sense in iso-combinations only.
4378 (window-combined-p window horizontal
)))
4379 ;; `old-pixel-size' is the current pixel size of WINDOW.
4380 (old-pixel-size (window-size window horizontal t
))
4381 ;; `new-size' is the specified or calculated size of the
4383 new-pixel-size new-parent new-normal
)
4386 (setq new-pixel-size
4388 ;; When resizing try to give the new window the
4389 ;; average size of a window in its combination.
4390 (min (- parent-pixel-size
4391 (window-min-size parent horizontal nil t
))
4392 (/ parent-pixel-size
4393 (1+ (window-combinations parent horizontal
))))
4394 ;; Else try to give the new window half the size
4395 ;; of WINDOW (plus an eventual odd pixel).
4396 (/ old-pixel-size
2)))
4397 (unless window-resize-pixelwise
4398 ;; Round to nearest char-size multiple.
4399 (setq new-pixel-size
4400 (* char-size
(round new-pixel-size char-size
)))))
4402 ;; SIZE non-negative specifies the new size of WINDOW.
4404 ;; Note: Specifying a non-negative SIZE is practically
4405 ;; always done as workaround for making the new window
4406 ;; appear above or on the left of the new window (the
4407 ;; ispell window is a typical example of that). In all
4408 ;; these cases the SIDE argument should be set to 'above
4409 ;; or 'left in order to support the 'resize option.
4410 ;; Here we have to nest the windows instead, see above.
4411 (setq new-pixel-size
(- old-pixel-size pixel-size
)))
4413 ;; SIZE negative specifies the size of the new window.
4414 (setq new-pixel-size
(- pixel-size
))))
4421 ;; SIZE unspecified, resizing.
4422 (when (and (not (window-sizable-p
4423 parent
(- new-pixel-size
) horizontal nil t
))
4424 ;; Try again with minimum split size.
4425 (setq new-pixel-size
4427 (window-split-min-size horizontal t
)))
4428 (not (window-sizable-p
4429 parent
(- new-pixel-size
) horizontal nil t
)))
4430 (error "Window %s too small for splitting 1" parent
)))
4431 ((> (+ new-pixel-size
(window-min-size window horizontal nil t
))
4433 ;; SIZE unspecified, no resizing.
4434 (error "Window %s too small for splitting 2" window
))))
4435 ((and (>= pixel-size
0)
4436 (or (>= pixel-size old-pixel-size
)
4438 (window-safe-min-pixel-size window horizontal
))))
4439 ;; SIZE specified as new size of old window. If the new size
4440 ;; is larger than the old size or the size of the new window
4441 ;; would be less than the safe minimum, signal an error.
4442 (error "Window %s too small for splitting 3" window
))
4444 ;; SIZE specified, resizing.
4445 (unless (window-sizable-p
4446 parent
(- new-pixel-size
) horizontal nil t
)
4447 ;; If we cannot resize the parent give up.
4448 (error "Window %s too small for splitting 4" parent
)))
4449 ((or (< new-pixel-size
4450 (window-safe-min-pixel-size window horizontal
))
4451 (< (- old-pixel-size new-pixel-size
)
4452 (window-safe-min-pixel-size window horizontal
)))
4453 ;; SIZE specification violates minimum size restrictions.
4454 (error "Window %s too small for splitting 5" window
)))
4456 (window--resize-reset frame horizontal
)
4459 ;; Make new-parent non-nil if we need a new parent window;
4460 ;; either because we want to nest or because WINDOW is not
4462 (or (eq window-combination-limit t
)
4463 (not (window-combined-p window horizontal
))))
4465 ;; Make new-normal the normal size of the new window.
4467 (pixel-size (/ (float new-pixel-size
)
4468 (if new-parent old-pixel-size parent-pixel-size
)))
4470 (resize (/ 1.0 (1+ (window-combinations parent horizontal
))))
4471 (t (/ (window-normal-size window horizontal
) 2.0))))
4474 ;; Try to get space from OLD's siblings. We could go "up" and
4475 ;; try getting additional space from surrounding windows but
4476 ;; we won't be able to return space to those windows when we
4477 ;; delete the one we create here. Hence we do not go up.
4479 (window--resize-child-windows
4480 parent
(- new-pixel-size
) horizontal
)
4481 (let* ((normal (- 1.0 new-normal
))
4482 (sub (window-child parent
)))
4484 (set-window-new-normal
4485 sub
(* (window-normal-size sub horizontal
) normal
))
4486 (setq sub
(window-right sub
)))))
4487 ;; Get entire space from WINDOW.
4488 (set-window-new-pixel
4489 window
(- old-pixel-size new-pixel-size
))
4490 ;; (set-window-new-pixel window (- old-pixel-size new-pixel-size))
4491 ;; (set-window-new-total
4492 ;; window (- old-size new-size))
4493 (window--resize-this-window window
(- new-pixel-size
) horizontal
)
4494 (set-window-new-normal
4495 window
(- (if new-parent
1.0 (window-normal-size window horizontal
))
4498 (let* ((new (split-window-internal window new-pixel-size side new-normal
)))
4499 (window--pixel-to-total frame horizontal
)
4500 ;; Assign window-side parameters, if any.
4502 ((eq window-combination-resize
'side
)
4505 (window-side window-side
)
4506 ((eq side
'above
) 'top
)
4507 ((eq side
'below
) 'bottom
)
4509 ;; We made a new side window.
4510 (set-window-parameter new
'window-side window-side
)
4511 (when (and new-parent
(window-parameter window
'window-side
))
4512 ;; We've been splitting a side root window. Give the
4513 ;; new parent the same window-side parameter.
4514 (set-window-parameter
4515 (window-parent new
) 'window-side window-side
))))
4516 ((eq window-combination-resize
'atom
)
4517 ;; Make sure `window--check-frame' won't destroy an existing
4518 ;; atomic window in case the new window gets nested inside.
4519 (unless (window-parameter window
'window-atom
)
4520 (set-window-parameter window
'window-atom t
))
4522 (set-window-parameter (window-parent new
) 'window-atom t
))
4523 (set-window-parameter new
'window-atom t
)))
4525 (run-window-configuration-change-hook frame
)
4526 (run-window-scroll-functions new
)
4527 (window--check frame
)
4528 ;; Always return the new window.
4531 ;; I think this should be the default; I think people will prefer it--rms.
4532 (defcustom split-window-keep-point t
4533 "If non-nil, \\[split-window-below] preserves point in the new window.
4534 If nil, adjust point in the two windows to minimize redisplay.
4535 This option applies only to `split-window-below' and functions
4536 that call it. The low-level `split-window' function always keeps
4537 the original point in both windows."
4541 (defun split-window-below (&optional size
)
4542 "Split the selected window into two windows, one above the other.
4543 The selected window is above. The newly split-off window is
4544 below, and displays the same buffer. Return the new window.
4546 If optional argument SIZE is omitted or nil, both windows get the
4547 same height, or close to it. If SIZE is positive, the upper
4548 \(selected) window gets SIZE lines. If SIZE is negative, the
4549 lower (new) window gets -SIZE lines.
4551 If the variable `split-window-keep-point' is non-nil, both
4552 windows get the same value of point as the selected window.
4553 Otherwise, the window starts are chosen so as to minimize the
4554 amount of redisplay; this is convenient on slow terminals."
4556 (let ((old-window (selected-window))
4557 (old-point (window-point))
4558 (size (and size
(prefix-numeric-value size
)))
4559 moved-by-window-height moved new-window bottom
)
4560 (when (and size
(< size
0) (< (- size
) window-min-height
))
4561 ;; `split-window' would not signal an error here.
4562 (error "Size of new window too small"))
4563 (setq new-window
(split-window nil size
))
4564 (unless split-window-keep-point
4565 (with-current-buffer (window-buffer)
4566 ;; Use `save-excursion' around vertical movements below
4567 ;; (Bug#10971). Note: When the selected window's buffer has a
4568 ;; header line, up to two lines of the buffer may not show up
4569 ;; in the resulting configuration.
4571 (goto-char (window-start))
4572 (setq moved
(vertical-motion (window-height)))
4573 (set-window-start new-window
(point))
4574 (when (> (point) (window-point new-window
))
4575 (set-window-point new-window
(point)))
4576 (when (= moved
(window-height))
4577 (setq moved-by-window-height t
)
4578 (vertical-motion -
1))
4579 (setq bottom
(point)))
4580 (and moved-by-window-height
4582 (set-window-point old-window
(1- bottom
)))
4583 (and moved-by-window-height
4584 (<= (window-start new-window
) old-point
)
4585 (set-window-point new-window old-point
)
4586 (select-window new-window
))))
4587 ;; Always copy quit-restore parameter in interactive use.
4588 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
4590 (set-window-parameter new-window
'quit-restore quit-restore
)))
4593 (defalias 'split-window-vertically
'split-window-below
)
4595 (defun split-window-right (&optional size
)
4596 "Split the selected window into two side-by-side windows.
4597 The selected window is on the left. The newly split-off window
4598 is on the right, and displays the same buffer. Return the new
4601 If optional argument SIZE is omitted or nil, both windows get the
4602 same width, or close to it. If SIZE is positive, the left-hand
4603 \(selected) window gets SIZE columns. If SIZE is negative, the
4604 right-hand (new) window gets -SIZE columns. Here, SIZE includes
4605 the width of the window's scroll bar; if there are no scroll
4606 bars, it includes the width of the divider column to the window's
4609 (let ((old-window (selected-window))
4610 (size (and size
(prefix-numeric-value size
)))
4612 (when (and size
(< size
0) (< (- size
) window-min-width
))
4613 ;; `split-window' would not signal an error here.
4614 (error "Size of new window too small"))
4615 (setq new-window
(split-window nil size t
))
4616 ;; Always copy quit-restore parameter in interactive use.
4617 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
4619 (set-window-parameter new-window
'quit-restore quit-restore
)))
4622 (defalias 'split-window-horizontally
'split-window-right
)
4624 ;;; Balancing windows.
4626 ;; The following routine uses the recycled code from an old version of
4627 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
4628 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
4629 ;; more readable (FWIW we'd need three loops - one to calculate the
4630 ;; minimum sizes per window, one to enlarge or shrink windows until the
4631 ;; new parent-size matches, and one where we shrink the largest/enlarge
4632 ;; the smallest window).
4633 (defun balance-windows-2 (window horizontal
)
4634 "Subroutine of `balance-windows-1'.
4635 WINDOW must be a vertical combination (horizontal if HORIZONTAL
4637 (let* ((char-size (if window-resize-pixelwise
4639 (frame-char-size window horizontal
)))
4640 (first (window-child window
))
4642 (number-of-children 0)
4643 (parent-size (window-new-pixel window
))
4644 (total-sum parent-size
)
4645 failed size sub-total sub-delta sub-amount rest
)
4647 (setq number-of-children
(1+ number-of-children
))
4648 (when (window-size-fixed-p sub horizontal
)
4650 (- total-sum
(window-size sub horizontal t
)))
4651 (set-window-new-normal sub
'ignore
))
4652 (setq sub
(window-right sub
)))
4655 (while (and failed
(> number-of-children
0))
4656 (setq size
(/ total-sum number-of-children
))
4659 (while (and sub
(not failed
))
4660 ;; Ignore child windows that should be ignored or are stuck.
4661 (unless (window--resize-child-windows-skip-p sub
)
4662 (setq sub-total
(window-size sub horizontal t
))
4663 (setq sub-delta
(- size sub-total
))
4665 (window-sizable sub sub-delta horizontal nil t
))
4666 ;; Register the new total size for this child window.
4667 (set-window-new-pixel sub
(+ sub-total sub-amount
))
4668 (unless (= sub-amount sub-delta
)
4669 (setq total-sum
(- total-sum sub-total sub-amount
))
4670 (setq number-of-children
(1- number-of-children
))
4671 ;; We failed and need a new round.
4673 (set-window-new-normal sub
'skip
)))
4674 (setq sub
(window-right sub
))))
4676 ;; How can we be sure that `number-of-children' is NOT zero here ?
4677 (setq rest
(% total-sum number-of-children
))
4678 ;; Fix rounding by trying to enlarge non-stuck windows by one line
4679 ;; (column) until `rest' is zero.
4681 (while (and sub
(> rest
0))
4682 (unless (window--resize-child-windows-skip-p window
)
4683 (set-window-new-pixel sub
(min rest char-size
) t
)
4684 (setq rest
(- rest char-size
)))
4685 (setq sub
(window-right sub
)))
4687 ;; Fix rounding by trying to enlarge stuck windows by one line
4688 ;; (column) until `rest' equals zero.
4690 (while (and sub
(> rest
0))
4691 (unless (eq (window-new-normal sub
) 'ignore
)
4692 (set-window-new-pixel sub
(min rest char-size
) t
)
4693 (setq rest
(- rest char-size
)))
4694 (setq sub
(window-right sub
)))
4698 ;; Record new normal sizes.
4699 (set-window-new-normal
4700 sub
(/ (if (eq (window-new-normal sub
) 'ignore
)
4701 (window-size sub horizontal t
)
4702 (window-new-pixel sub
))
4703 (float parent-size
)))
4704 ;; Recursively balance each window's child windows.
4705 (balance-windows-1 sub horizontal
)
4706 (setq sub
(window-right sub
)))))
4708 (defun balance-windows-1 (window &optional horizontal
)
4709 "Subroutine of `balance-windows'."
4710 (if (window-child window
)
4711 (let ((sub (window-child window
)))
4712 (if (window-combined-p sub horizontal
)
4713 (balance-windows-2 window horizontal
)
4714 (let ((size (window-new-pixel window
)))
4716 (set-window-new-pixel sub size
)
4717 (balance-windows-1 sub horizontal
)
4718 (setq sub
(window-right sub
))))))))
4720 (defun balance-windows (&optional window-or-frame
)
4721 "Balance the sizes of windows of WINDOW-OR-FRAME.
4722 WINDOW-OR-FRAME is optional and defaults to the selected frame.
4723 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
4724 windows of that frame. If WINDOW-OR-FRAME denotes a window,
4725 recursively balance the sizes of all child windows of that
4730 ((or (not window-or-frame
)
4731 (frame-live-p window-or-frame
))
4732 (frame-root-window window-or-frame
))
4733 ((or (window-live-p window-or-frame
)
4734 (window-child window-or-frame
))
4737 (error "Not a window or frame %s" window-or-frame
))))
4738 (frame (window-frame window
)))
4739 ;; Balance vertically.
4740 (window--resize-reset (window-frame window
))
4741 (balance-windows-1 window
)
4742 (when (window--resize-apply-p frame
)
4743 (window-resize-apply frame
)
4744 (window--pixel-to-total frame
)
4745 (run-window-configuration-change-hook frame
))
4746 ;; Balance horizontally.
4747 (window--resize-reset (window-frame window
) t
)
4748 (balance-windows-1 window t
)
4749 (when (window--resize-apply-p frame t
)
4750 (window-resize-apply frame t
)
4751 (window--pixel-to-total frame t
)
4752 (run-window-configuration-change-hook frame
))))
4754 (defun window-fixed-size-p (&optional window direction
)
4755 "Return t if WINDOW cannot be resized in DIRECTION.
4756 WINDOW defaults to the selected window. DIRECTION can be
4757 nil (i.e. any), `height' or `width'."
4758 (with-current-buffer (window-buffer window
)
4759 (when (and (boundp 'window-size-fixed
) window-size-fixed
)
4761 (member (cons direction window-size-fixed
)
4762 '((height . width
) (width . height
))))))))
4764 ;;; A different solution to balance-windows.
4765 (defvar window-area-factor
1
4766 "Factor by which the window area should be over-estimated.
4767 This is used by `balance-windows-area'.
4768 Changing this globally has no effect.")
4769 (make-variable-buffer-local 'window-area-factor
)
4771 (defun balance-windows-area-adjust (window delta horizontal pixelwise
)
4772 "Wrapper around `window-resize' with error checking.
4773 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
4774 ;; `window-resize' may fail if delta is too large.
4775 (while (>= (abs delta
) 1)
4778 ;; It was wrong to use `window-resize' here. Somehow
4779 ;; `balance-windows-area' depends on resizing windows
4781 (adjust-window-trailing-edge window delta horizontal pixelwise
)
4784 ;;(message "adjust: %s" (error-message-string err))
4785 (setq delta
(/ delta
2))))))
4787 (defun balance-windows-area ()
4788 "Make all visible windows the same area (approximately).
4789 See also `window-area-factor' to change the relative size of
4792 (let* ((unchanged 0) (carry 0) (round 0)
4793 ;; Remove fixed-size windows.
4794 (wins (delq nil
(mapcar (lambda (win)
4795 (if (not (window-fixed-size-p win
)) win
))
4796 (window-list nil
'nomini
))))
4798 (pixelwise window-resize-pixelwise
)
4800 ;; Resizing a window changes the size of surrounding windows in complex
4801 ;; ways, so it's difficult to balance them all. The introduction of
4802 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
4803 ;; very difficult to do. `balance-window' above takes an off-line
4804 ;; approach: get the whole window tree, then balance it, then try to
4805 ;; adjust the windows so they fit the result.
4806 ;; Here, instead, we take a "local optimization" approach, where we just
4807 ;; go through all the windows several times until nothing needs to be
4808 ;; changed. The main problem with this approach is that it's difficult
4809 ;; to make sure it terminates, so we use some heuristic to try and break
4810 ;; off infinite loops.
4811 ;; After a round without any change, we allow a second, to give a chance
4812 ;; to the carry to propagate a minor imbalance from the end back to
4814 (while (< unchanged
2)
4815 ;; (message "New round")
4816 (setq unchanged
(1+ unchanged
) round
(1+ round
))
4819 (while (progn (setq next
(next-window next
))
4820 (window-fixed-size-p next
)))
4821 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
4823 (< (car (window-pixel-edges win
)) (car (window-pixel-edges next
))))
4824 (areadiff (/ (- (* (window-size next nil pixelwise
)
4825 (window-size next t pixelwise
)
4826 (buffer-local-value 'window-area-factor
4827 (window-buffer next
)))
4828 (* (window-size win nil pixelwise
)
4829 (window-size win t pixelwise
)
4830 (buffer-local-value 'window-area-factor
4831 (window-buffer win
))))
4832 (max (buffer-local-value 'window-area-factor
4833 (window-buffer win
))
4834 (buffer-local-value 'window-area-factor
4835 (window-buffer next
)))))
4837 (+ (window-size win nil pixelwise
)
4838 (window-size next nil pixelwise
))
4839 (+ (window-size win t pixelwise
)
4840 (window-size next t pixelwise
))))
4841 (diff (/ areadiff edgesize
)))
4843 ;; Maybe diff is actually closer to 1 than to 0.
4844 (setq diff
(/ (* 3 areadiff
) (* 2 edgesize
))))
4845 (when (and (zerop diff
) (not (zerop areadiff
)))
4846 (setq diff
(/ (+ areadiff carry
) edgesize
))
4847 ;; Change things smoothly.
4848 (if (or (> diff
1) (< diff -
1)) (setq diff
(/ diff
2))))
4850 ;; Make sure negligible differences don't accumulate to
4851 ;; become significant.
4852 (setq carry
(+ carry areadiff
))
4853 ;; This used `adjust-window-trailing-edge' before and uses
4854 ;; `window-resize' now. Error wrapping is still needed.
4855 (balance-windows-area-adjust win diff horiz pixelwise
)
4857 (let ((change (cons win
(window-pixel-edges win
))))
4858 ;; If the same change has been seen already for this window,
4859 ;; we're most likely in an endless loop, so don't count it as
4861 (unless (member change changelog
)
4862 (push change changelog
)
4863 (setq unchanged
0 carry
0)))))))
4864 ;; We've now basically balanced all the windows.
4865 ;; But there may be some minor off-by-one imbalance left over,
4866 ;; so let's do some fine tuning.
4867 ;; (bw-finetune wins)
4868 ;; (message "Done in %d rounds" round)
4871 ;;; Window states, how to get them and how to put them in a window.
4872 (defun window--state-get-1 (window &optional writable
)
4873 "Helper function for `window-state-get'."
4876 ((window-top-child window
) 'vc
)
4877 ((window-left-child window
) 'hc
)
4879 (buffer (window-buffer window
))
4880 (selected (eq window
(selected-window)))
4883 ,@(unless (window-next-sibling window
) `((last . t
)))
4884 (pixel-width .
,(window-pixel-width window
))
4885 (pixel-height .
,(window-pixel-height window
))
4886 (total-width .
,(window-total-width window
))
4887 (total-height .
,(window-total-height window
))
4888 (normal-height .
,(window-normal-size window
))
4889 (normal-width .
,(window-normal-size window t
))
4890 ,@(unless (window-live-p window
)
4891 `((combination-limit .
,(window-combination-limit window
))))
4892 ,@(let ((parameters (window-parameters window
))
4894 ;; Make copies of those window parameters whose
4895 ;; persistence property is `writable' if WRITABLE is
4896 ;; non-nil and non-nil if WRITABLE is nil.
4897 (dolist (par parameters
)
4898 (let ((pers (cdr (assq (car par
)
4899 window-persistent-parameters
))))
4900 (when (and pers
(or (not writable
) (eq pers
'writable
)))
4901 (setq list
(cons (cons (car par
) (cdr par
)) list
)))))
4902 ;; Add `clone-of' parameter if necessary.
4903 (let ((pers (cdr (assq 'clone-of
4904 window-persistent-parameters
))))
4905 (when (and pers
(or (not writable
) (eq pers
'writable
))
4906 (not (assq 'clone-of list
)))
4907 (setq list
(cons (cons 'clone-of window
) list
))))
4909 `((parameters .
,list
))))
4911 ;; All buffer related things go in here.
4912 (let ((point (window-point window
))
4913 (start (window-start window
)))
4915 ,(buffer-name buffer
)
4916 (selected .
,selected
)
4917 (hscroll .
,(window-hscroll window
))
4918 (fringes .
,(window-fringes window
))
4919 (margins .
,(window-margins window
))
4920 (scroll-bars .
,(window-scroll-bars window
))
4921 (vscroll .
,(window-vscroll window
))
4922 (dedicated .
,(window-dedicated-p window
))
4923 (point .
,(if writable point
4926 'window-point-insertion-type
4928 (start .
,(if writable start
(copy-marker start
)))))))))
4930 (when (memq type
'(vc hc
))
4932 (setq window
(window-child window
))
4934 (setq list
(cons (window--state-get-1 window writable
) list
))
4935 (setq window
(window-right window
)))
4937 (append head tail
)))
4939 (defun window-state-get (&optional window writable
)
4940 "Return state of WINDOW as a Lisp object.
4941 WINDOW can be any window and defaults to the root window of the
4944 Optional argument WRITABLE non-nil means do not use markers for
4945 sampling `window-point' and `window-start'. Together, WRITABLE
4946 and the variable `window-persistent-parameters' specify which
4947 window parameters are saved by this function. WRITABLE should be
4948 non-nil when the return value shall be written to a file and read
4949 back in another session. Otherwise, an application may run into
4950 an `invalid-read-syntax' error while attempting to read back the
4953 The return value can be used as argument for `window-state-put'
4954 to put the state recorded here into an arbitrary window. The
4955 value can be also stored on disk and read back in a new session."
4958 (if (window-valid-p window
)
4960 (error "%s is not a live or internal window" window
))
4961 (frame-root-window)))
4962 ;; The return value is a cons whose car specifies some constraints on
4963 ;; the size of WINDOW. The cdr lists the states of the child windows
4966 ;; Frame related things would go into a function, say `frame-state',
4967 ;; calling `window-state-get' to insert the frame's root window.
4968 `((min-height .
,(window-min-size window
))
4969 (min-width .
,(window-min-size window t
))
4970 (min-height-ignore .
,(window-min-size window nil t
))
4971 (min-width-ignore .
,(window-min-size window t t
))
4972 (min-height-safe .
,(window-min-size window nil
'safe
))
4973 (min-width-safe .
,(window-min-size window t
'safe
))
4974 (min-pixel-height .
,(window-min-size window nil nil t
))
4975 (min-pixel-width .
,(window-min-size window t nil t
))
4976 (min-pixel-height-ignore .
,(window-min-size window nil t t
))
4977 (min-pixel-width-ignore .
,(window-min-size window t t t
))
4978 (min-pixel-height-safe .
,(window-min-size window nil
'safe t
))
4979 (min-pixel-width-safe .
,(window-min-size window t
'safe t
)))
4980 (window--state-get-1 window writable
)))
4982 (defvar window-state-put-list nil
4983 "Helper variable for `window-state-put'.")
4985 (defvar window-state-put-stale-windows nil
4986 "Helper variable for `window-state-put'.")
4988 (defun window--state-put-1 (state &optional window ignore totals pixelwise
)
4989 "Helper function for `window-state-put'."
4990 (let ((type (car state
)))
4991 (setq state
(cdr state
))
4994 ;; For a leaf window just add unprocessed entries to
4995 ;; `window-state-put-list'.
4996 (push (cons window state
) window-state-put-list
))
4997 ((memq type
'(vc hc
))
4998 (let* ((horizontal (eq type
'hc
))
4999 (total (window-size window horizontal pixelwise
))
5002 (dolist (item state
)
5003 ;; Find the next child window. WINDOW always points to the
5004 ;; real window that we want to fill with what we find here.
5005 (when (memq (car item
) '(leaf vc hc
))
5006 (if (assq 'last item
)
5007 ;; The last child window. Below `window--state-put-1'
5008 ;; will put into it whatever ITEM has in store.
5010 ;; Not the last child window, prepare for splitting
5011 ;; WINDOW. SIZE is the new (and final) size of the old
5017 (cdr (assq (if horizontal
5021 (cdr (assq (if horizontal
5025 ;; Use normalized size and round.
5028 (cdr (assq (if horizontal
'normal-width
'normal-height
)
5031 ;; Use safe sizes, we try to resize later.
5032 (setq size
(max size
5034 (* window-safe-min-width
5036 (frame-char-width (window-frame window
))
5038 (* window-safe-min-height
5040 (frame-char-height (window-frame window
))
5042 (if (window-sizable-p window
(- size
) horizontal
'safe pixelwise
)
5043 (let* ((window-combination-limit
5044 (assq 'combination-limit item
)))
5045 ;; We must inherit the combination limit, otherwise
5046 ;; we might mess up handling of atomic and side
5048 (setq new
(split-window window size horizontal pixelwise
)))
5049 ;; Give up if we can't resize window down to safe sizes.
5050 (error "Cannot resize window %s" window
))
5054 ;; When creating the first child window add for parent
5055 ;; unprocessed entries to `window-state-put-list'.
5056 (setq window-state-put-list
5057 (cons (cons (window-parent window
) state
)
5058 window-state-put-list
))))
5060 ;; Now process the current window (either the one we've just
5061 ;; split or the last child of its parent).
5062 (window--state-put-1 item window ignore totals
)
5063 ;; Continue with the last window split off.
5064 (setq window new
))))))))
5066 (defun window--state-put-2 (ignore pixelwise
)
5067 "Helper function for `window-state-put'."
5068 (dolist (item window-state-put-list
)
5069 (let ((window (car item
))
5070 (combination-limit (cdr (assq 'combination-limit item
)))
5071 (parameters (cdr (assq 'parameters item
)))
5072 (state (cdr (assq 'buffer item
))))
5073 (when combination-limit
5074 (set-window-combination-limit window combination-limit
))
5075 ;; Reset window's parameters and assign saved ones (we might want
5076 ;; a `remove-window-parameters' function here).
5077 (dolist (parameter (window-parameters window
))
5078 (set-window-parameter window
(car parameter
) nil
))
5080 (dolist (parameter parameters
)
5081 (set-window-parameter window
(car parameter
) (cdr parameter
))))
5082 ;; Process buffer related state.
5084 (let ((buffer (get-buffer (car state
))))
5086 (with-current-buffer buffer
5087 (set-window-buffer window buffer
)
5088 (set-window-hscroll window
(cdr (assq 'hscroll state
)))
5089 (apply 'set-window-fringes
5090 (cons window
(cdr (assq 'fringes state
))))
5091 (let ((margins (cdr (assq 'margins state
))))
5092 (set-window-margins window
(car margins
) (cdr margins
)))
5093 (let ((scroll-bars (cdr (assq 'scroll-bars state
))))
5094 (set-window-scroll-bars
5095 window
(car scroll-bars
) (nth 2 scroll-bars
)
5096 (nth 3 scroll-bars
)))
5097 (set-window-vscroll window
(cdr (assq 'vscroll state
)))
5098 ;; Adjust vertically.
5099 (if (memq window-size-fixed
'(t height
))
5100 ;; A fixed height window, try to restore the
5104 (if pixelwise
'pixel-height
'total-height
)
5106 (window-size window nil pixelwise
)))
5108 (when (window--resizable-p
5109 window delta nil nil nil nil nil pixelwise
)
5110 (window-resize window delta nil nil pixelwise
)))
5111 ;; Else check whether the window is not high enough.
5113 (window-min-size window nil ignore pixelwise
))
5115 (- min-size
(window-size window nil pixelwise
))))
5116 (when (and (> delta
0)
5117 (window--resizable-p
5118 window delta nil ignore nil nil nil pixelwise
))
5119 (window-resize window delta nil ignore pixelwise
))))
5120 ;; Adjust horizontally.
5121 (if (memq window-size-fixed
'(t width
))
5122 ;; A fixed width window, try to restore the original
5126 (if pixelwise
'pixel-width
'total-width
)
5128 (window-size window t pixelwise
)))
5130 (when (window--resizable-p
5131 window delta nil nil nil nil nil pixelwise
)
5132 (window-resize window delta nil nil pixelwise
)))
5133 ;; Else check whether the window is not wide enough.
5134 (let* ((min-size (window-min-size window t ignore pixelwise
))
5135 (delta (- min-size
(window-size window t pixelwise
))))
5136 (when (and (> delta
0)
5137 (window--resizable-p
5138 window delta t ignore nil nil nil pixelwise
))
5139 (window-resize window delta t ignore pixelwise
))))
5140 ;; Set dedicated status.
5141 (set-window-dedicated-p window
(cdr (assq 'dedicated state
)))
5142 ;; Install positions (maybe we should do this after all
5143 ;; windows have been created and sized).
5145 (set-window-start window
(cdr (assq 'start state
)))
5146 (set-window-point window
(cdr (assq 'point state
))))
5147 ;; Select window if it's the selected one.
5148 (when (cdr (assq 'selected state
))
5149 (select-window window
)))
5150 ;; We don't want to raise an error in case the buffer does
5151 ;; not exist anymore, so we switch to a previous one and
5152 ;; save the window with the intention of deleting it later
5154 (switch-to-prev-buffer window
)
5155 (push window window-state-put-stale-windows
)))))))
5157 (defun window-state-put (state &optional window ignore
)
5158 "Put window state STATE into WINDOW.
5159 STATE should be the state of a window returned by an earlier
5160 invocation of `window-state-get'. Optional argument WINDOW must
5161 specify a valid window and defaults to the selected one. If
5162 WINDOW is not live, replace WINDOW by a live one before putting
5165 Optional argument IGNORE non-nil means ignore minimum window
5166 sizes and fixed size restrictions. IGNORE equal `safe' means
5167 windows can get as small as `window-safe-min-height' and
5168 `window-safe-min-width'."
5169 (setq window-state-put-stale-windows nil
)
5170 (setq window
(window-normalize-window window
))
5172 ;; When WINDOW is internal, reduce it to a live one to put STATE into,
5174 (unless (window-live-p window
)
5175 (let ((root (frame-root-window window
)))
5176 (if (eq window root
)
5177 (setq window
(frame-first-window root
))
5179 (setq window
(catch 'live
5180 (walk-window-subtree
5182 (when (window-live-p window
)
5183 (throw 'live window
)))
5185 (delete-other-windows-internal window root
)))
5187 (let* ((frame (window-frame window
))
5189 ;; We check here (1) whether the total sizes of root window of
5190 ;; STATE and that of WINDOW are equal so we can avoid
5191 ;; calculating new sizes, and (2) if we do have to resize
5192 ;; whether we can do so without violating size restrictions.
5193 (pixelwise (and (cdr (assq 'pixel-width state
))
5194 (cdr (assq 'pixel-height state
))))
5195 (totals (or (and pixelwise
5196 (= (window-pixel-width window
)
5197 (cdr (assq 'pixel-width state
)))
5198 (= (window-pixel-height window
)
5199 (cdr (assq 'pixel-height state
))))
5200 (and (= (window-total-width window
)
5201 (cdr (assq 'total-width state
)))
5202 (= (window-total-height window
)
5203 (cdr (assq 'total-height state
))))))
5204 (min-height (cdr (assq
5205 (if pixelwise
'min-pixel-height
'min-height
)
5207 (min-width (cdr (assq
5208 (if pixelwise
'min-pixel-width
'min-weight
)
5210 (if (and (not totals
)
5211 (or (> min-height
(window-size window nil pixelwise
))
5212 (> min-width
(window-size window t pixelwise
)))
5214 (and (setq min-height
5217 'min-pixel-height-ignore
5223 'min-pixel-width-ignore
5227 (window-size window nil pixelwise
))
5229 (window-size window t pixelwise
)))
5230 (or (not (eq ignore
'safe
))
5231 (and (setq min-height
5234 'min-pixel-height-safe
5240 'min-pixel-width-safe
5244 (window-size window nil pixelwise
))
5246 (window-size window t pixelwise
))))))))
5247 ;; The check above might not catch all errors due to rounding
5248 ;; issues - so IGNORE equal 'safe might not always produce the
5249 ;; minimum possible state. But such configurations hardly make
5251 (error "Window %s too small to accommodate state" window
)
5252 (setq state
(cdr state
))
5253 (setq window-state-put-list nil
)
5254 ;; Work on the windows of a temporary buffer to make sure that
5255 ;; splitting proceeds regardless of any buffer local values of
5256 ;; `window-size-fixed'. Release that buffer after the buffers of
5257 ;; all live windows have been set by `window--state-put-2'.
5259 (set-window-buffer window
(current-buffer))
5260 (window--state-put-1 state window nil totals pixelwise
)
5261 (window--state-put-2 ignore pixelwise
))
5262 (while window-state-put-stale-windows
5263 (let ((window (pop window-state-put-stale-windows
)))
5264 (when (eq (window-deletable-p window
) t
)
5265 (delete-window window
))))
5266 (window--check frame
))))
5268 (defun display-buffer-record-window (type window buffer
)
5269 "Record information for window used by `display-buffer'.
5270 TYPE specifies the type of the calling operation and must be one
5271 of the symbols 'reuse (when WINDOW existed already and was
5272 reused for displaying BUFFER), 'window (when WINDOW was created
5273 on an already existing frame), or 'frame (when WINDOW was
5274 created on a new frame). WINDOW is the window used for or created
5275 by the `display-buffer' routines. BUFFER is the buffer that
5278 This function installs or updates the quit-restore parameter of
5279 WINDOW. The quit-restore parameter is a list of four elements:
5280 The first element is one of the symbols 'window, 'frame, 'same or
5281 'other. The second element is either one of the symbols 'window
5282 or 'frame or a list whose elements are the buffer previously
5283 shown in the window, that buffer's window start and window point,
5284 and the window's height. The third element is the window
5285 selected at the time the parameter was created. The fourth
5289 (if (eq (window-buffer window
) buffer
)
5290 ;; WINDOW shows BUFFER already.
5291 (when (consp (window-parameter window
'quit-restore
))
5292 ;; If WINDOW has a quit-restore parameter, reset its car.
5293 (setcar (window-parameter window
'quit-restore
) 'same
))
5294 ;; WINDOW shows another buffer.
5295 (with-current-buffer (window-buffer window
)
5296 (set-window-parameter
5297 window
'quit-restore
5299 ;; A quadruple of WINDOW's buffer, start, point and height.
5300 (list (current-buffer) (window-start window
)
5301 ;; Preserve window-point-insertion-type (Bug#12588).
5303 (window-point window
) window-point-insertion-type
)
5304 (window-total-height window
))
5305 (selected-window) buffer
)))))
5307 ;; WINDOW has been created on an existing frame.
5308 (set-window-parameter
5309 window
'quit-restore
5310 (list 'window
'window
(selected-window) buffer
)))
5312 ;; WINDOW has been created on a new frame.
5313 (set-window-parameter
5314 window
'quit-restore
5315 (list 'frame
'frame
(selected-window) buffer
)))))
5317 (defcustom display-buffer-function nil
5318 "If non-nil, function to call to handle `display-buffer'.
5319 It will receive two args, the buffer and a flag which if non-nil
5320 means that the currently selected window is not acceptable. It
5321 should choose or create a window, display the specified buffer in
5322 it, and return the window.
5324 The specified function should call `display-buffer-record-window'
5325 with corresponding arguments to set up the quit-restore parameter
5326 of the window used."
5329 (function :tag
"function"))
5332 (make-obsolete-variable 'display-buffer-function
5333 'display-buffer-alist
"24.3")
5335 ;; Eventually, we want to turn this into a defvar; instead of
5336 ;; customizing this, the user should use a `pop-up-frame-parameters'
5337 ;; alist entry in `display-buffer-base-action'.
5338 (defcustom pop-up-frame-alist nil
5339 "Alist of parameters for automatically generated new frames.
5340 If non-nil, the value you specify here is used by the default
5341 `pop-up-frame-function' for the creation of new frames.
5343 Since `pop-up-frame-function' is used by `display-buffer' for
5344 making new frames, any value specified here by default affects
5345 the automatic generation of new frames via `display-buffer' and
5346 all functions based on it. The behavior of `make-frame' is not
5347 affected by this variable."
5348 :type
'(repeat (cons :format
"%v"
5349 (symbol :tag
"Parameter")
5350 (sexp :tag
"Value")))
5353 (defcustom pop-up-frame-function
5354 (lambda () (make-frame pop-up-frame-alist
))
5355 "Function used by `display-buffer' for creating a new frame.
5356 This function is called with no arguments and should return a new
5357 frame. The default value calls `make-frame' with the argument
5358 `pop-up-frame-alist'."
5362 (defcustom special-display-buffer-names nil
5363 "List of names of buffers that should be displayed specially.
5364 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5365 its name is in this list, displays the buffer in a way specified
5366 by `special-display-function'. `special-display-popup-frame'
5367 \(the default for `special-display-function') usually displays
5368 the buffer in a separate frame made with the parameters specified
5369 by `special-display-frame-alist'. If `special-display-function'
5370 has been set to some other function, that function is called with
5371 the buffer as first, and nil as second argument.
5373 Alternatively, an element of this list can be specified as
5374 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
5375 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5376 `special-display-popup-frame' will interpret such pairs as frame
5377 parameters when it creates a special frame, overriding the
5378 corresponding values from `special-display-frame-alist'.
5380 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5381 `special-display-popup-frame' displays that buffer in the
5382 selected window. If FRAME-PARAMETERS contains (same-frame . t),
5383 it displays that buffer in a window on the selected frame.
5385 If `special-display-function' specifies some other function than
5386 `special-display-popup-frame', that function is called with the
5387 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5390 Finally, an element of this list can be also specified as
5391 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5392 `special-display-popup-frame' will call FUNCTION with the buffer
5393 named BUFFER-NAME as first argument, and OTHER-ARGS as the
5396 Any alternative function specified here is responsible for
5397 setting up the quit-restore parameter of the window used.
5399 If this variable appears \"not to work\", because you added a
5400 name to it but the corresponding buffer is displayed in the
5401 selected window, look at the values of `same-window-buffer-names'
5402 and `same-window-regexps'. Those variables take precedence over
5405 See also `special-display-regexps'."
5407 (choice :tag
"Buffer"
5409 (string :format
"%v")
5410 (cons :tag
"With parameters"
5413 (string :format
"%v")
5414 (repeat :tag
"Parameters"
5416 (symbol :tag
"Parameter")
5417 (sexp :tag
"Value"))))
5418 (list :tag
"With function"
5421 (string :format
"%v")
5422 (function :tag
"Function")
5423 (repeat :tag
"Arguments" (sexp)))))
5426 (make-obsolete-variable 'special-display-buffer-names
'display-buffer-alist
"24.3")
5427 (put 'special-display-buffer-names
'risky-local-variable t
)
5429 (defcustom special-display-regexps nil
5430 "List of regexps saying which buffers should be displayed specially.
5431 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5432 any regexp in this list matches its name, displays it specially
5433 using `special-display-function'. `special-display-popup-frame'
5434 \(the default for `special-display-function') usually displays
5435 the buffer in a separate frame made with the parameters specified
5436 by `special-display-frame-alist'. If `special-display-function'
5437 has been set to some other function, that function is called with
5438 the buffer as first, and nil as second argument.
5440 Alternatively, an element of this list can be specified as
5441 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
5442 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5443 `special-display-popup-frame' will then interpret these pairs as
5444 frame parameters when creating a special frame for a buffer whose
5445 name matches REGEXP, overriding the corresponding values from
5446 `special-display-frame-alist'.
5448 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5449 `special-display-popup-frame' displays buffers matching REGEXP in
5450 the selected window. (same-frame . t) in FRAME-PARAMETERS means
5451 to display such buffers in a window on the selected frame.
5453 If `special-display-function' specifies some other function than
5454 `special-display-popup-frame', that function is called with the
5455 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
5458 Finally, an element of this list can be also specified as
5459 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
5460 will then call FUNCTION with the buffer whose name matched
5461 REGEXP as first, and OTHER-ARGS as second argument.
5463 Any alternative function specified here is responsible for
5464 setting up the quit-restore parameter of the window used.
5466 If this variable appears \"not to work\", because you added a
5467 name to it but the corresponding buffer is displayed in the
5468 selected window, look at the values of `same-window-buffer-names'
5469 and `same-window-regexps'. Those variables take precedence over
5472 See also `special-display-buffer-names'."
5474 (choice :tag
"Buffer"
5476 (regexp :format
"%v")
5477 (cons :tag
"With parameters"
5480 (regexp :format
"%v")
5481 (repeat :tag
"Parameters"
5483 (symbol :tag
"Parameter")
5484 (sexp :tag
"Value"))))
5485 (list :tag
"With function"
5488 (regexp :format
"%v")
5489 (function :tag
"Function")
5490 (repeat :tag
"Arguments" (sexp)))))
5493 (make-obsolete-variable 'special-display-regexps
'display-buffer-alist
"24.3")
5494 (put 'special-display-regexps
'risky-local-variable t
)
5496 (defun special-display-p (buffer-name)
5497 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
5498 More precisely, return t if `special-display-buffer-names' or
5499 `special-display-regexps' contain a string entry equaling or
5500 matching BUFFER-NAME. If `special-display-buffer-names' or
5501 `special-display-regexps' contain a list entry whose car equals
5502 or matches BUFFER-NAME, the return value is the cdr of that
5506 ((member buffer-name special-display-buffer-names
)
5508 ((setq tmp
(assoc buffer-name special-display-buffer-names
))
5511 (dolist (regexp special-display-regexps
)
5514 (when (string-match-p regexp buffer-name
)
5516 ((and (consp regexp
) (stringp (car regexp
))
5517 (string-match-p (car regexp
) buffer-name
))
5518 (throw 'found
(cdr regexp
))))))))))
5520 (defcustom special-display-frame-alist
5521 '((height .
14) (width .
80) (unsplittable . t
))
5522 "Alist of parameters for special frames.
5523 Special frames are used for buffers whose names are listed in
5524 `special-display-buffer-names' and for buffers whose names match
5525 one of the regular expressions in `special-display-regexps'.
5527 This variable can be set in your init file, like this:
5529 (setq special-display-frame-alist '((width . 80) (height . 20)))
5531 These supersede the values given in `default-frame-alist'."
5532 :type
'(repeat (cons :format
"%v"
5533 (symbol :tag
"Parameter")
5534 (sexp :tag
"Value")))
5536 (make-obsolete-variable 'special-display-frame-alist
'display-buffer-alist
"24.3")
5538 (defun special-display-popup-frame (buffer &optional args
)
5539 "Pop up a frame displaying BUFFER and return its window.
5540 If BUFFER is already displayed in a visible or iconified frame,
5541 raise that frame. Otherwise, display BUFFER in a new frame.
5543 Optional argument ARGS is a list specifying additional
5546 If ARGS is an alist, use it as a list of frame parameters. If
5547 these parameters contain (same-window . t), display BUFFER in
5548 the selected window. If they contain (same-frame . t), display
5549 BUFFER in a window of the selected frame.
5551 If ARGS is a list whose car is a symbol, use (car ARGS) as a
5552 function to do the work. Pass it BUFFER as first argument, and
5553 pass the elements of (cdr ARGS) as the remaining arguments."
5554 (if (and args
(symbolp (car args
)))
5555 (apply (car args
) buffer
(cdr args
))
5556 (let ((window (get-buffer-window buffer
0)))
5558 ;; If we have a window already, make it visible.
5560 (let ((frame (window-frame window
)))
5561 (make-frame-visible frame
)
5563 (display-buffer-record-window 'reuse window buffer
)
5565 ;; Reuse the current window if the user requested it.
5566 (when (cdr (assq 'same-window args
))
5568 (progn (switch-to-buffer buffer nil t
) (selected-window))
5570 ;; Stay on the same frame if requested.
5571 (when (or (cdr (assq 'same-frame args
)) (cdr (assq 'same-window args
)))
5572 (let* ((pop-up-windows t
)
5574 special-display-buffer-names special-display-regexps
)
5575 (display-buffer buffer
)))
5576 ;; If no window yet, make one in a new frame.
5578 (with-current-buffer buffer
5579 (make-frame (append args special-display-frame-alist
))))
5580 (window (frame-selected-window frame
)))
5581 (display-buffer-record-window 'frame window buffer
)
5582 (unless (eq buffer
(window-buffer window
))
5583 (set-window-buffer window buffer
)
5584 (set-window-prev-buffers window nil
))
5585 (set-window-dedicated-p window t
)
5588 (defcustom special-display-function
'special-display-popup-frame
5589 "Function to call for displaying special buffers.
5590 This function is called with two arguments - the buffer and,
5591 optionally, a list - and should return a window displaying that
5592 buffer. The default value usually makes a separate frame for the
5593 buffer using `special-display-frame-alist' to specify the frame
5594 parameters. See the definition of `special-display-popup-frame'
5595 for how to specify such a function.
5597 A buffer is special when its name is either listed in
5598 `special-display-buffer-names' or matches a regexp in
5599 `special-display-regexps'.
5601 The specified function should call `display-buffer-record-window'
5602 with corresponding arguments to set up the quit-restore parameter
5603 of the window used."
5606 (make-obsolete-variable 'special-display-function
'display-buffer-alist
"24.3")
5608 (defcustom same-window-buffer-names nil
5609 "List of names of buffers that should appear in the \"same\" window.
5610 `display-buffer' and `pop-to-buffer' show a buffer whose name is
5611 on this list in the selected rather than some other window.
5613 An element of this list can be a cons cell instead of just a
5614 string. In that case, the cell's car must be a string specifying
5615 the buffer name. This is for compatibility with
5616 `special-display-buffer-names'; the cdr of the cons cell is
5619 See also `same-window-regexps'."
5620 :type
'(repeat (string :format
"%v"))
5623 (defcustom same-window-regexps nil
5624 "List of regexps saying which buffers should appear in the \"same\" window.
5625 `display-buffer' and `pop-to-buffer' show a buffer whose name
5626 matches a regexp on this list in the selected rather than some
5629 An element of this list can be a cons cell instead of just a
5630 string. In that case, the cell's car must be a regexp matching
5631 the buffer name. This is for compatibility with
5632 `special-display-regexps'; the cdr of the cons cell is ignored.
5634 See also `same-window-buffer-names'."
5635 :type
'(repeat (regexp :format
"%v"))
5638 (defun same-window-p (buffer-name)
5639 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5640 This function returns non-nil if `display-buffer' or
5641 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
5642 selected rather than (as usual) some other window. See
5643 `same-window-buffer-names' and `same-window-regexps'."
5645 ((not (stringp buffer-name
)))
5646 ;; The elements of `same-window-buffer-names' can be buffer
5647 ;; names or cons cells whose cars are buffer names.
5648 ((member buffer-name same-window-buffer-names
))
5649 ((assoc buffer-name same-window-buffer-names
))
5651 (dolist (regexp same-window-regexps
)
5652 ;; The elements of `same-window-regexps' can be regexps
5653 ;; or cons cells whose cars are regexps.
5654 (when (or (and (stringp regexp
)
5655 (string-match-p regexp buffer-name
))
5656 (and (consp regexp
) (stringp (car regexp
))
5657 (string-match-p (car regexp
) buffer-name
)))
5658 (throw 'found t
)))))))
5660 (defcustom pop-up-frames nil
5661 "Whether `display-buffer' should make a separate frame.
5662 If nil, never make a separate frame.
5663 If the value is `graphic-only', make a separate frame
5664 on graphic displays only.
5665 Any other non-nil value means always make a separate frame."
5667 (const :tag
"Never" nil
)
5668 (const :tag
"On graphic displays only" graphic-only
)
5669 (const :tag
"Always" t
))
5672 (defcustom display-buffer-reuse-frames nil
5673 "Non-nil means `display-buffer' should reuse frames.
5674 If the buffer in question is already displayed in a frame, raise
5680 (make-obsolete-variable
5681 'display-buffer-reuse-frames
5682 "use a `reusable-frames' alist entry in `display-buffer-alist'."
5685 (defcustom pop-up-windows t
5686 "Non-nil means `display-buffer' should make a new window."
5690 (defcustom split-window-preferred-function
'split-window-sensibly
5691 "Function called by `display-buffer' routines to split a window.
5692 This function is called with a window as single argument and is
5693 supposed to split that window and return the new window. If the
5694 window can (or shall) not be split, it is supposed to return nil.
5695 The default is to call the function `split-window-sensibly' which
5696 tries to split the window in a way which seems most suitable.
5697 You can customize the options `split-height-threshold' and/or
5698 `split-width-threshold' in order to have `split-window-sensibly'
5699 prefer either vertical or horizontal splitting.
5701 If you set this to any other function, bear in mind that the
5702 `display-buffer' routines may call this function two times. The
5703 argument of the first call is the largest window on its frame.
5704 If that call fails to return a live window, the function is
5705 called again with the least recently used window as argument. If
5706 that call fails too, `display-buffer' will use an existing window
5707 to display its buffer.
5709 The window selected at the time `display-buffer' was invoked is
5710 still selected when this function is called. Hence you can
5711 compare the window argument with the value of `selected-window'
5712 if you intend to split the selected window instead or if you do
5713 not want to split the selected window."
5718 (defcustom split-height-threshold
80
5719 "Minimum height for splitting windows sensibly.
5720 If this is an integer, `split-window-sensibly' may split a window
5721 vertically only if it has at least this many lines. If this is
5722 nil, `split-window-sensibly' is not allowed to split a window
5723 vertically. If, however, a window is the only window on its
5724 frame, `split-window-sensibly' may split it vertically
5725 disregarding the value of this variable."
5726 :type
'(choice (const nil
) (integer :tag
"lines"))
5730 (defcustom split-width-threshold
160
5731 "Minimum width for splitting windows sensibly.
5732 If this is an integer, `split-window-sensibly' may split a window
5733 horizontally only if it has at least this many columns. If this
5734 is nil, `split-window-sensibly' is not allowed to split a window
5736 :type
'(choice (const nil
) (integer :tag
"columns"))
5740 (defun window-splittable-p (window &optional horizontal
)
5741 "Return non-nil if `split-window-sensibly' may split WINDOW.
5742 Optional argument HORIZONTAL nil or omitted means check whether
5743 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
5744 non-nil means check whether WINDOW may be split horizontally.
5746 WINDOW may be split vertically when the following conditions
5748 - `window-size-fixed' is either nil or equals `width' for the
5750 - `split-height-threshold' is an integer and WINDOW is at least as
5751 high as `split-height-threshold'.
5752 - When WINDOW is split evenly, the emanating windows are at least
5753 `window-min-height' lines tall and can accommodate at least one
5754 line plus - if WINDOW has one - a mode line.
5756 WINDOW may be split horizontally when the following conditions
5758 - `window-size-fixed' is either nil or equals `height' for the
5760 - `split-width-threshold' is an integer and WINDOW is at least as
5761 wide as `split-width-threshold'.
5762 - When WINDOW is split evenly, the emanating windows are at least
5763 `window-min-width' or two (whichever is larger) columns wide."
5764 (when (window-live-p window
)
5765 (with-current-buffer (window-buffer window
)
5767 ;; A window can be split horizontally when its width is not
5768 ;; fixed, it is at least `split-width-threshold' columns wide
5769 ;; and at least twice as wide as `window-min-width' and 2 (the
5770 ;; latter value is hardcoded).
5771 (and (memq window-size-fixed
'(nil height
))
5772 ;; Testing `window-full-width-p' here hardly makes any
5773 ;; sense nowadays. This can be done more intuitively by
5774 ;; setting up `split-width-threshold' appropriately.
5775 (numberp split-width-threshold
)
5776 (>= (window-width window
)
5777 (max split-width-threshold
5778 (* 2 (max window-min-width
2)))))
5779 ;; A window can be split vertically when its height is not
5780 ;; fixed, it is at least `split-height-threshold' lines high,
5781 ;; and it is at least twice as high as `window-min-height' and 2
5782 ;; if it has a mode line or 1.
5783 (and (memq window-size-fixed
'(nil width
))
5784 (numberp split-height-threshold
)
5785 (>= (window-height window
)
5786 (max split-height-threshold
5787 (* 2 (max window-min-height
5788 (if mode-line-format
2 1))))))))))
5790 (defun split-window-sensibly (&optional window
)
5791 "Split WINDOW in a way suitable for `display-buffer'.
5792 WINDOW defaults to the currently selected window.
5793 If `split-height-threshold' specifies an integer, WINDOW is at
5794 least `split-height-threshold' lines tall and can be split
5795 vertically, split WINDOW into two windows one above the other and
5796 return the lower window. Otherwise, if `split-width-threshold'
5797 specifies an integer, WINDOW is at least `split-width-threshold'
5798 columns wide and can be split horizontally, split WINDOW into two
5799 windows side by side and return the window on the right. If this
5800 can't be done either and WINDOW is the only window on its frame,
5801 try to split WINDOW vertically disregarding any value specified
5802 by `split-height-threshold'. If that succeeds, return the lower
5803 window. Return nil otherwise.
5805 By default `display-buffer' routines call this function to split
5806 the largest or least recently used window. To change the default
5807 customize the option `split-window-preferred-function'.
5809 You can enforce this function to not split WINDOW horizontally,
5810 by setting (or binding) the variable `split-width-threshold' to
5811 nil. If, in addition, you set `split-height-threshold' to zero,
5812 chances increase that this function does split WINDOW vertically.
5814 In order to not split WINDOW vertically, set (or bind) the
5815 variable `split-height-threshold' to nil. Additionally, you can
5816 set `split-width-threshold' to zero to make a horizontal split
5817 more likely to occur.
5819 Have a look at the function `window-splittable-p' if you want to
5820 know how `split-window-sensibly' determines whether WINDOW can be
5822 (let ((window (or window
(selected-window))))
5823 (or (and (window-splittable-p window
)
5824 ;; Split window vertically.
5825 (with-selected-window window
5826 (split-window-below)))
5827 (and (window-splittable-p window t
)
5828 ;; Split window horizontally.
5829 (with-selected-window window
5830 (split-window-right)))
5831 (and (eq window
(frame-root-window (window-frame window
)))
5832 (not (window-minibuffer-p window
))
5833 ;; If WINDOW is the only window on its frame and is not the
5834 ;; minibuffer window, try to split it vertically disregarding
5835 ;; the value of `split-height-threshold'.
5836 (let ((split-height-threshold 0))
5837 (when (window-splittable-p window
)
5838 (with-selected-window window
5839 (split-window-below))))))))
5841 (defun window--try-to-split-window (window &optional alist
)
5842 "Try to split WINDOW.
5843 Return value returned by `split-window-preferred-function' if it
5844 represents a live window, nil otherwise."
5845 (and (window-live-p window
)
5846 (not (frame-parameter (window-frame window
) 'unsplittable
))
5847 (let* ((window-combination-limit
5848 ;; When `window-combination-limit' equals
5849 ;; `display-buffer' or equals `resize-window' and a
5850 ;; `window-height' or `window-width' alist entry are
5851 ;; present, bind it to t so resizing steals space
5852 ;; preferably from the window that was split.
5853 (if (or (eq window-combination-limit
'display-buffer
)
5854 (and (eq window-combination-limit
'window-size
)
5855 (or (cdr (assq 'window-height alist
))
5856 (cdr (assq 'window-width alist
)))))
5858 window-combination-limit
))
5860 ;; Since `split-window-preferred-function' might
5861 ;; throw an error use `condition-case'.
5863 (funcall split-window-preferred-function window
)
5865 (and (window-live-p new-window
) new-window
))))
5867 (defun window--frame-usable-p (frame)
5868 "Return FRAME if it can be used to display a buffer."
5869 (when (frame-live-p frame
)
5870 (let ((window (frame-root-window frame
)))
5871 ;; `frame-root-window' may be an internal window which is considered
5872 ;; "dead" by `window-live-p'. Hence if `window' is not live we
5873 ;; implicitly know that `frame' has a visible window we can use.
5874 (unless (and (window-live-p window
)
5875 (or (window-minibuffer-p window
)
5876 ;; If the window is soft-dedicated, the frame is usable.
5877 ;; Actually, even if the window is really dedicated,
5878 ;; the frame is still usable by splitting it.
5879 ;; At least Emacs-22 allowed it, and it is desirable
5880 ;; when displaying same-frame windows.
5881 nil
; (eq t (window-dedicated-p window))
5885 (defcustom even-window-heights t
5886 "If non-nil `display-buffer' will try to even window heights.
5887 Otherwise `display-buffer' will leave the window configuration
5888 alone. Heights are evened only when `display-buffer' chooses a
5889 window that appears above or below the selected window."
5893 (defun window--even-window-heights (window)
5894 "Even heights of WINDOW and selected window.
5895 Do this only if these windows are vertically adjacent to each
5896 other, `even-window-heights' is non-nil, and the selected window
5897 is higher than WINDOW."
5898 (when (and even-window-heights
5899 ;; Even iff WINDOW forms a vertical combination with the
5900 ;; selected window, and WINDOW's height exceeds that of the
5901 ;; selected window, see also bug#11880.
5902 (window-combined-p window
)
5903 (= (window-child-count (window-parent window
)) 2)
5904 (eq (window-parent) (window-parent window
))
5905 (> (window-total-height) (window-total-height window
)))
5906 ;; Don't throw an error if we can't even window heights for
5910 (/ (- (window-total-height window
) (window-total-height)) 2))
5913 (defun window--display-buffer (buffer window type
&optional alist dedicated
)
5914 "Display BUFFER in WINDOW.
5915 TYPE must be one of the symbols `reuse', `window' or `frame' and
5916 is passed unaltered to `display-buffer-record-window'. ALIST is
5917 the alist argument of `display-buffer'. Set `window-dedicated-p'
5918 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
5920 (when (and (buffer-live-p buffer
) (window-live-p window
))
5921 (display-buffer-record-window type window buffer
)
5922 (unless (eq buffer
(window-buffer window
))
5923 (set-window-dedicated-p window nil
)
5924 (set-window-buffer window buffer
)
5926 (set-window-dedicated-p window dedicated
))
5927 (when (memq type
'(window frame
))
5928 (set-window-prev-buffers window nil
)))
5929 (let ((parameter (window-parameter window
'quit-restore
))
5930 (height (cdr (assq 'window-height alist
)))
5931 (width (cdr (assq 'window-width alist
)))
5932 (size (cdr (assq 'window-size alist
))))
5934 ((or (eq type
'frame
)
5935 (and (eq (car parameter
) 'same
)
5936 (eq (nth 1 parameter
) 'frame
)))
5937 ;; Adjust size of frame if asked for.
5941 (let ((width (car size
))
5943 (frame (window-frame window
)))
5944 (when (and (numberp width
) (numberp height
))
5946 frame
(+ (frame-height frame
)
5947 (- height
(window-total-height window
))))
5949 frame
(+ (frame-width frame
)
5950 (- width
(window-total-width window
)))))))
5952 (ignore-errors (funcall size window
)))))
5953 ((or (eq type
'window
)
5954 (and (eq (car parameter
) 'same
)
5955 (eq (nth 1 parameter
) 'window
)))
5956 ;; Adjust height of window if asked for.
5961 (if (integerp height
)
5964 (* (window-total-height (frame-root-window window
))
5966 (delta (- new-height
(window-total-height window
))))
5967 (when (and (window--resizable-p window delta nil
'safe
)
5968 (window-combined-p window
))
5969 (window-resize window delta nil
'safe
))))
5971 (ignore-errors (funcall height window
))))
5972 ;; Adjust width of window if asked for.
5977 (if (integerp width
)
5980 (* (window-total-width (frame-root-window window
))
5982 (delta (- new-width
(window-total-width window
))))
5983 (when (and (window--resizable-p window delta t
'safe
)
5984 (window-combined-p window t
))
5985 (window-resize window delta t
'safe
))))
5987 (ignore-errors (funcall width window
)))))))
5991 (defun window--maybe-raise-frame (frame)
5992 (let ((visible (frame-visible-p frame
)))
5993 (unless (or (not visible
)
5994 ;; Assume the selected frame is already visible enough.
5995 (eq frame
(selected-frame))
5996 ;; Assume the frame from which we invoked the
5997 ;; minibuffer is visible.
5998 (and (minibuffer-window-active-p (selected-window))
5999 (eq frame
(window-frame (minibuffer-selected-window)))))
6000 (raise-frame frame
))))
6002 ;; FIXME: Not implemented.
6003 ;; FIXME: By the way, there could be more levels of dedication:
6004 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
6005 ;; the window hasn't been used for something else yet.
6006 ;; - `soft' (`softly') dedicated only allows reuse when asked explicitly.
6007 ;; - `strongly' never allows reuse.
6008 (defvar display-buffer-mark-dedicated nil
6009 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
6010 The actual non-nil value of this variable will be copied to the
6011 `window-dedicated-p' flag.")
6013 (defconst display-buffer--action-function-custom-type
6014 '(choice :tag
"Function"
6015 (const :tag
"--" ignore
) ; default for insertion
6016 (const display-buffer-reuse-window
)
6017 (const display-buffer-pop-up-window
)
6018 (const display-buffer-same-window
)
6019 (const display-buffer-pop-up-frame
)
6020 (const display-buffer-below-selected
)
6021 (const display-buffer-at-bottom
)
6022 (const display-buffer-in-previous-window
)
6023 (const display-buffer-use-some-window
)
6024 (function :tag
"Other function"))
6025 "Custom type for `display-buffer' action functions.")
6027 (defconst display-buffer--action-custom-type
6028 `(cons :tag
"Action"
6029 (choice :tag
"Action functions"
6030 ,display-buffer--action-function-custom-type
6032 :tag
"List of functions"
6033 ,display-buffer--action-function-custom-type
))
6034 (alist :tag
"Action arguments"
6036 :value-type
(sexp :tag
"Value")))
6037 "Custom type for `display-buffer' actions.")
6039 (defvar display-buffer-overriding-action
'(nil . nil
)
6040 "Overriding action to perform to display a buffer.
6041 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6042 function or a list of functions. Each function should accept two
6043 arguments: a buffer to display and an alist similar to ALIST.
6044 See `display-buffer' for details.")
6045 (put 'display-buffer-overriding-action
'risky-local-variable t
)
6047 (defcustom display-buffer-alist nil
6048 "Alist of conditional actions for `display-buffer'.
6049 This is a list of elements (CONDITION . ACTION), where:
6051 CONDITION is either a regexp matching buffer names, or a
6052 function that takes two arguments - a buffer name and the
6053 ACTION argument of `display-buffer' - and returns a boolean.
6055 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
6056 function or a list of functions. Each such function should
6057 accept two arguments: a buffer to display and an alist of the
6058 same form as ALIST. See `display-buffer' for details.
6060 `display-buffer' scans this alist until it either finds a
6061 matching regular expression or the function specified by a
6062 condition returns non-nil. In any of these cases, it adds the
6063 associated action to the list of actions it will try."
6064 :type
`(alist :key-type
6065 (choice :tag
"Condition"
6067 (function :tag
"Matcher function"))
6068 :value-type
,display-buffer--action-custom-type
)
6073 (defcustom display-buffer-base-action
'(nil . nil
)
6074 "User-specified default action for `display-buffer'.
6075 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6076 function or a list of functions. Each function should accept two
6077 arguments: a buffer to display and an alist similar to ALIST.
6078 See `display-buffer' for details."
6079 :type display-buffer--action-custom-type
6084 (defconst display-buffer-fallback-action
6085 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
6086 display-buffer-reuse-window
6087 display-buffer--maybe-pop-up-frame-or-window
6088 display-buffer-in-previous-window
6089 display-buffer-use-some-window
6090 ;; If all else fails, pop up a new frame.
6091 display-buffer-pop-up-frame
))
6092 "Default fallback action for `display-buffer'.
6093 This is the action used by `display-buffer' if no other actions
6094 specified, e.g. by the user options `display-buffer-alist' or
6095 `display-buffer-base-action'. See `display-buffer'.")
6096 (put 'display-buffer-fallback-action
'risky-local-variable t
)
6098 (defun display-buffer-assq-regexp (buffer-name alist action
)
6099 "Retrieve ALIST entry corresponding to BUFFER-NAME.
6100 ACTION is the action argument passed to `display-buffer'."
6102 (dolist (entry alist
)
6103 (let ((key (car entry
)))
6104 (when (or (and (stringp key
)
6105 (string-match-p key buffer-name
))
6106 (and (functionp key
)
6107 (funcall key buffer-name action
)))
6108 (throw 'match
(cdr entry
)))))))
6110 (defvar display-buffer--same-window-action
6111 '(display-buffer-same-window
6112 (inhibit-same-window . nil
))
6113 "A `display-buffer' action for displaying in the same window.")
6114 (put 'display-buffer--same-window-action
'risky-local-variable t
)
6116 (defvar display-buffer--other-frame-action
6117 '((display-buffer-reuse-window
6118 display-buffer-pop-up-frame
)
6119 (reusable-frames .
0)
6120 (inhibit-same-window . t
))
6121 "A `display-buffer' action for displaying in another frame.")
6122 (put 'display-buffer--other-frame-action
'risky-local-variable t
)
6124 (defun display-buffer (buffer-or-name &optional action frame
)
6125 "Display BUFFER-OR-NAME in some window, without selecting it.
6126 BUFFER-OR-NAME must be a buffer or the name of an existing
6127 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
6128 or nil if no such window is found.
6130 Optional argument ACTION, if non-nil, should specify a display
6131 action. Its form is described below.
6133 Optional argument FRAME, if non-nil, acts like an additional
6134 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
6135 specifying the frame(s) to search for a window that is already
6136 displaying the buffer. See `display-buffer-reuse-window'
6138 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
6139 where FUNCTION is either a function or a list of functions, and
6140 ALIST is an arbitrary association list (alist).
6142 Each such FUNCTION should accept two arguments: the buffer to
6143 display and an alist. Based on those arguments, it should
6144 display the buffer and return the window. If the caller is
6145 prepared to handle the case of not displaying the buffer
6146 and returning nil from `display-buffer' it should pass
6147 \(allow-no-window . t) as an element of the ALIST.
6149 The `display-buffer' function builds a function list and an alist
6150 by combining the functions and alists specified in
6151 `display-buffer-overriding-action', `display-buffer-alist', the
6152 ACTION argument, `display-buffer-base-action', and
6153 `display-buffer-fallback-action' (in order). Then it calls each
6154 function in the combined function list in turn, passing the
6155 buffer as the first argument and the combined alist as the second
6156 argument, until one of the functions returns non-nil.
6158 If ACTION is nil, the function list and the alist are built using
6159 only the other variables mentioned above.
6161 Available action functions include:
6162 `display-buffer-same-window'
6163 `display-buffer-reuse-window'
6164 `display-buffer-pop-up-frame'
6165 `display-buffer-pop-up-window'
6166 `display-buffer-in-previous-window'
6167 `display-buffer-use-some-window'
6169 Recognized alist entries include:
6171 `inhibit-same-window' -- A non-nil value prevents the same
6172 window from being used for display.
6174 `inhibit-switch-frame' -- A non-nil value prevents any other
6175 frame from being raised or selected,
6176 even if the window is displayed there.
6178 `reusable-frames' -- Value specifies frame(s) to search for a
6179 window that already displays the buffer.
6180 See `display-buffer-reuse-window'.
6182 `pop-up-frame-parameters' -- Value specifies an alist of frame
6183 parameters to give a new frame, if
6186 `window-height' -- Value specifies either an integer (the number
6187 of lines of a new window), a floating point number (the
6188 fraction of a new window with respect to the height of the
6189 frame's root window) or a function to be called with one
6190 argument - a new window. The function is supposed to adjust
6191 the height of the window; its return value is ignored.
6192 Suitable functions are `shrink-window-if-larger-than-buffer'
6193 and `fit-window-to-buffer'.
6195 `window-width' -- Value specifies either an integer (the number
6196 of columns of a new window), a floating point number (the
6197 fraction of a new window with respect to the width of the
6198 frame's root window) or a function to be called with one
6199 argument - a new window. The function is supposed to adjust
6200 the width of the window; its return value is ignored.
6202 `allow-no-window' -- A non-nil value indicates readiness for the case
6203 of not displaying the buffer and FUNCTION can safely return
6204 a non-window value to suppress displaying.
6206 The ACTION argument to `display-buffer' can also have a non-nil
6207 and non-list value. This means to display the buffer in a window
6208 other than the selected one, even if it is already displayed in
6209 the selected window. If called interactively with a prefix
6210 argument, ACTION is t."
6211 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6212 (if current-prefix-arg t
)))
6213 (let ((buffer (if (bufferp buffer-or-name
)
6215 (get-buffer buffer-or-name
)))
6216 ;; Make sure that when we split windows the old window keeps
6217 ;; point, bug#14829.
6218 (split-window-keep-point t
)
6219 ;; Handle the old form of the first argument.
6220 (inhibit-same-window (and action
(not (listp action
)))))
6221 (unless (listp action
) (setq action nil
))
6222 (if display-buffer-function
6223 ;; If `display-buffer-function' is defined, let it do the job.
6224 (funcall display-buffer-function buffer inhibit-same-window
)
6225 ;; Otherwise, use the defined actions.
6227 (display-buffer-assq-regexp
6228 (buffer-name buffer
) display-buffer-alist action
))
6229 (special-action (display-buffer--special-action buffer
))
6230 ;; Extra actions from the arguments to this function:
6232 (cons nil
(append (if inhibit-same-window
6233 '((inhibit-same-window . t
)))
6235 `((reusable-frames .
,frame
))))))
6236 ;; Construct action function list and action alist.
6237 (actions (list display-buffer-overriding-action
6238 user-action special-action action extra-action
6239 display-buffer-base-action
6240 display-buffer-fallback-action
))
6241 (functions (apply 'append
6244 (if (functionp x
) (list x
) x
))
6246 (alist (apply 'append
(mapcar 'cdr actions
)))
6248 (unless (buffer-live-p buffer
)
6249 (error "Invalid buffer"))
6250 (while (and functions
(not window
))
6251 (setq window
(funcall (car functions
) buffer alist
)
6252 functions
(cdr functions
)))
6253 (and (windowp window
) window
)))))
6255 (defun display-buffer-other-frame (buffer)
6256 "Display buffer BUFFER preferably in another frame.
6257 This uses the function `display-buffer' as a subroutine; see
6258 its documentation for additional customization information."
6259 (interactive "BDisplay buffer in other frame: ")
6260 (display-buffer buffer display-buffer--other-frame-action t
))
6262 ;;; `display-buffer' action functions:
6264 (defun display-buffer-same-window (buffer alist
)
6265 "Display BUFFER in the selected window.
6266 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
6267 if the selected window is a minibuffer window or is dedicated to
6268 another buffer; in that case, return nil. Otherwise, return the
6270 (unless (or (cdr (assq 'inhibit-same-window alist
))
6271 (window-minibuffer-p)
6272 (window-dedicated-p))
6273 (window--display-buffer buffer
(selected-window) 'reuse alist
)))
6275 (defun display-buffer--maybe-same-window (buffer alist
)
6276 "Conditionally display BUFFER in the selected window.
6277 If `same-window-p' returns non-nil for BUFFER's name, call
6278 `display-buffer-same-window' and return its value. Otherwise,
6280 (and (same-window-p (buffer-name buffer
))
6281 (display-buffer-same-window buffer alist
)))
6283 (defun display-buffer-reuse-window (buffer alist
)
6284 "Return a window that is already displaying BUFFER.
6285 Return nil if no usable window is found.
6287 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6288 window is not eligible for reuse.
6290 If ALIST contains a `reusable-frames' entry, its value determines
6291 which frames to search for a reusable window:
6292 nil -- the selected frame (actually the last non-minibuffer frame)
6293 A frame -- just that frame
6294 `visible' -- all visible frames
6295 0 -- all frames on the current terminal
6298 If ALIST contains no `reusable-frames' entry, search just the
6299 selected frame if `display-buffer-reuse-frames' and
6300 `pop-up-frames' are both nil; search all frames on the current
6301 terminal if either of those variables is non-nil.
6303 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6304 event that a window on another frame is chosen, avoid raising
6306 (let* ((alist-entry (assq 'reusable-frames alist
))
6307 (frames (cond (alist-entry (cdr alist-entry
))
6308 ((if (eq pop-up-frames
'graphic-only
)
6312 (display-buffer-reuse-frames 0)
6313 (t (last-nonminibuffer-frame))))
6314 (window (if (and (eq buffer
(window-buffer))
6315 (not (cdr (assq 'inhibit-same-window alist
))))
6317 (car (delq (selected-window)
6318 (get-buffer-window-list buffer
'nomini
6320 (when (window-live-p window
)
6321 (prog1 (window--display-buffer buffer window
'reuse alist
)
6322 (unless (cdr (assq 'inhibit-switch-frame alist
))
6323 (window--maybe-raise-frame (window-frame window
)))))))
6325 (defun display-buffer--special-action (buffer)
6326 "Return special display action for BUFFER, if any.
6327 If `special-display-p' returns non-nil for BUFFER, return an
6328 appropriate display action involving `special-display-function'.
6329 See `display-buffer' for the format of display actions."
6330 (and special-display-function
6331 ;; `special-display-p' returns either t or a list of frame
6332 ;; parameters to pass to `special-display-function'.
6333 (let ((pars (special-display-p (buffer-name buffer
))))
6335 (list (list #'display-buffer-reuse-window
6336 `(lambda (buffer _alist
)
6337 (funcall special-display-function
6338 buffer
',(if (listp pars
) pars
)))))))))
6340 (defun display-buffer-pop-up-frame (buffer alist
)
6341 "Display BUFFER in a new frame.
6342 This works by calling `pop-up-frame-function'. If successful,
6343 return the window used; otherwise return nil.
6345 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
6346 raising the new frame.
6348 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
6349 corresponding value is an alist of frame parameters to give the
6351 (let* ((params (cdr (assq 'pop-up-frame-parameters alist
)))
6352 (pop-up-frame-alist (append params pop-up-frame-alist
))
6353 (fun pop-up-frame-function
)
6356 ;; Make BUFFER current so `make-frame' will use it as the
6357 ;; new frame's buffer (Bug#15133).
6358 (with-current-buffer buffer
6359 (setq frame
(funcall fun
)))
6360 (setq window
(frame-selected-window frame
)))
6361 (prog1 (window--display-buffer
6362 buffer window
'frame alist display-buffer-mark-dedicated
)
6363 (unless (cdr (assq 'inhibit-switch-frame alist
))
6364 (window--maybe-raise-frame frame
))))))
6366 (defun display-buffer-pop-up-window (buffer alist
)
6367 "Display BUFFER by popping up a new window.
6368 The new window is created on the selected frame, or in
6369 `last-nonminibuffer-frame' if no windows can be created there.
6370 If successful, return the new window; otherwise return nil.
6372 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6373 event that the new window is created on another frame, avoid
6375 (let ((frame (or (window--frame-usable-p (selected-frame))
6376 (window--frame-usable-p (last-nonminibuffer-frame))))
6378 (when (and (or (not (frame-parameter frame
'unsplittable
))
6379 ;; If the selected frame cannot be split, look at
6380 ;; `last-nonminibuffer-frame'.
6381 (and (eq frame
(selected-frame))
6382 (setq frame
(last-nonminibuffer-frame))
6383 (window--frame-usable-p frame
)
6384 (not (frame-parameter frame
'unsplittable
))))
6385 ;; Attempt to split largest or least recently used window.
6386 (setq window
(or (window--try-to-split-window
6387 (get-largest-window frame t
) alist
)
6388 (window--try-to-split-window
6389 (get-lru-window frame t
) alist
))))
6390 (prog1 (window--display-buffer
6391 buffer window
'window alist display-buffer-mark-dedicated
)
6392 (unless (cdr (assq 'inhibit-switch-frame alist
))
6393 (window--maybe-raise-frame (window-frame window
)))))))
6395 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist
)
6396 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
6398 If `pop-up-frames' is non-nil (and not `graphic-only' on a
6399 text-only terminal), try with `display-buffer-pop-up-frame'.
6401 If that cannot be done, and `pop-up-windows' is non-nil, try
6402 again with `display-buffer-pop-up-window'."
6403 (or (and (if (eq pop-up-frames
'graphic-only
)
6406 (display-buffer-pop-up-frame buffer alist
))
6408 (display-buffer-pop-up-window buffer alist
))))
6410 (defun display-buffer-below-selected (buffer alist
)
6411 "Try displaying BUFFER in a window below the selected window.
6412 This either splits the selected window or reuses the window below
6415 (or (and (not (frame-parameter nil
'unsplittable
))
6416 (let ((split-height-threshold 0)
6417 split-width-threshold
)
6418 (setq window
(window--try-to-split-window (selected-window) alist
)))
6419 (window--display-buffer
6420 buffer window
'window alist display-buffer-mark-dedicated
))
6421 (and (setq window
(window-in-direction 'below
))
6422 (not (window-dedicated-p window
))
6423 (window--display-buffer
6424 buffer window
'reuse alist display-buffer-mark-dedicated
)))))
6426 (defun display-buffer-at-bottom (buffer alist
)
6427 "Try displaying BUFFER in a window at the bottom of the selected frame.
6428 This either splits the window at the bottom of the frame or the
6429 frame's root window, or reuses an existing window at the bottom
6430 of the selected frame."
6431 (let (bottom-window window
)
6433 (lambda (window) (setq bottom-window window
)) nil nil
'nomini
)
6434 (or (and (not (frame-parameter nil
'unsplittable
))
6435 (let (split-width-threshold)
6436 (setq window
(window--try-to-split-window bottom-window alist
)))
6437 (window--display-buffer
6438 buffer window
'window alist display-buffer-mark-dedicated
))
6439 (and (not (frame-parameter nil
'unsplittable
))
6442 (split-window (window--major-non-side-window))
6444 (window--display-buffer
6445 buffer window
'window alist display-buffer-mark-dedicated
))
6446 (and (setq window bottom-window
)
6447 (not (window-dedicated-p window
))
6448 (window--display-buffer
6449 buffer window
'reuse alist display-buffer-mark-dedicated
)))))
6451 (defun display-buffer-in-previous-window (buffer alist
)
6452 "Display BUFFER in a window previously showing it.
6453 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6454 window is not eligible for reuse.
6456 If ALIST contains a `reusable-frames' entry, its value determines
6457 which frames to search for a reusable window:
6458 nil -- the selected frame (actually the last non-minibuffer frame)
6459 A frame -- just that frame
6460 `visible' -- all visible frames
6461 0 -- all frames on the current terminal
6464 If ALIST contains no `reusable-frames' entry, search just the
6465 selected frame if `display-buffer-reuse-frames' and
6466 `pop-up-frames' are both nil; search all frames on the current
6467 terminal if either of those variables is non-nil.
6469 If ALIST has a `previous-window' entry, the window specified by
6470 that entry will override any other window found by the methods
6471 above, even if that window never showed BUFFER before."
6472 (let* ((alist-entry (assq 'reusable-frames alist
))
6473 (inhibit-same-window
6474 (cdr (assq 'inhibit-same-window alist
)))
6476 (alist-entry (cdr alist-entry
))
6477 ((if (eq pop-up-frames
'graphic-only
)
6481 (display-buffer-reuse-frames 0)
6482 (t (last-nonminibuffer-frame))))
6483 best-window second-best-window window
)
6484 ;; Scan windows whether they have shown the buffer recently.
6486 (dolist (window (window-list-1 (frame-first-window) 'nomini frames
))
6487 (when (and (assq buffer
(window-prev-buffers window
))
6488 (not (window-dedicated-p window
)))
6489 (if (eq window
(selected-window))
6490 (unless inhibit-same-window
6491 (setq second-best-window window
))
6492 (setq best-window window
)
6494 ;; When ALIST has a `previous-window' entry, that entry may override
6495 ;; anything we found so far.
6496 (when (and (setq window
(cdr (assq 'previous-window alist
)))
6497 (window-live-p window
)
6498 (not (window-dedicated-p window
)))
6499 (if (eq window
(selected-window))
6500 (unless inhibit-same-window
6501 (setq second-best-window window
))
6502 (setq best-window window
)))
6503 ;; Return best or second best window found.
6504 (when (setq window
(or best-window second-best-window
))
6505 (window--display-buffer buffer window
'reuse alist
))))
6507 (defun display-buffer-use-some-window (buffer alist
)
6508 "Display BUFFER in an existing window.
6509 Search for a usable window, set that window to the buffer, and
6510 return the window. If no suitable window is found, return nil.
6512 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6513 event that a window in another frame is chosen, avoid raising
6515 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist
)))
6516 (frame (or (window--frame-usable-p (selected-frame))
6517 (window--frame-usable-p (last-nonminibuffer-frame))))
6519 ;; Reuse an existing window.
6520 (or (get-lru-window frame nil not-this-window
)
6521 (let ((window (get-buffer-window buffer
'visible
)))
6522 (unless (and not-this-window
6523 (eq window
(selected-window)))
6525 (get-largest-window 'visible nil not-this-window
)
6526 (let ((window (get-buffer-window buffer
0)))
6527 (unless (and not-this-window
6528 (eq window
(selected-window)))
6530 (get-largest-window 0 nil not-this-window
)))
6531 (quit-restore (and (window-live-p window
)
6532 (window-parameter window
'quit-restore
)))
6533 (quad (nth 1 quit-restore
)))
6534 (when (window-live-p window
)
6535 ;; If the window was used by `display-buffer' before, try to
6536 ;; resize it to its old height but don't signal an error.
6537 (when (and (listp quad
)
6538 (integerp (nth 3 quad
))
6539 (> (nth 3 quad
) (window-total-height window
)))
6541 (window-resize window
(- (nth 3 quad
) (window-total-height window
)))
6545 (window--display-buffer buffer window
'reuse alist
)
6546 (window--even-window-heights window
)
6547 (unless (cdr (assq 'inhibit-switch-frame alist
))
6548 (window--maybe-raise-frame (window-frame window
)))))))
6550 (defun display-buffer-no-window (_buffer alist
)
6551 "Display BUFFER in no window.
6552 If ALIST has a non-nil `allow-no-window' entry, then don't display
6553 a window at all. This makes possible to override the default action
6554 and avoid displaying the buffer. It is assumed that when the caller
6555 specifies a non-nil `allow-no-window' then it can handle a nil value
6556 returned from `display-buffer' in this case."
6557 (when (cdr (assq 'allow-no-window alist
))
6560 ;;; Display + selection commands:
6561 (defun pop-to-buffer (buffer &optional action norecord
)
6562 "Select buffer BUFFER in some window, preferably a different one.
6563 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6564 is a string not naming an existent buffer, create a buffer with
6565 that name. If BUFFER is nil, choose some other buffer. Return
6568 This uses `display-buffer' as a subroutine. The optional ACTION
6569 argument is passed to `display-buffer' as its ACTION argument.
6570 See `display-buffer' for more information. ACTION is t if called
6571 interactively with a prefix argument, which means to pop to a
6572 window other than the selected one even if the buffer is already
6573 displayed in the selected window.
6575 If the window to show BUFFER is not on the selected
6576 frame, raise that window's frame and give it input focus.
6578 Optional third arg NORECORD non-nil means do not put this buffer
6579 at the front of the list of recently selected ones."
6580 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
6581 (if current-prefix-arg t
)))
6582 (setq buffer
(window-normalize-buffer-to-switch-to buffer
))
6583 ;; This should be done by `select-window' below.
6584 ;; (set-buffer buffer)
6585 (let* ((old-frame (selected-frame))
6586 (window (display-buffer buffer action
))
6587 (frame (window-frame window
)))
6588 ;; If we chose another frame, make sure it gets input focus.
6589 (unless (eq frame old-frame
)
6590 (select-frame-set-input-focus frame norecord
))
6591 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
6592 (select-window window norecord
)
6595 (defun pop-to-buffer-same-window (buffer &optional norecord
)
6596 "Select buffer BUFFER in some window, preferably the same one.
6597 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6598 is a string not naming an existent buffer, create a buffer with
6599 that name. If BUFFER is nil, choose some other buffer. Return
6602 Optional argument NORECORD, if non-nil means do not put this
6603 buffer at the front of the list of recently selected ones.
6605 Unlike `pop-to-buffer', this function prefers using the selected
6606 window over popping up a new window or frame."
6607 (pop-to-buffer buffer display-buffer--same-window-action norecord
))
6609 (defun read-buffer-to-switch (prompt)
6610 "Read the name of a buffer to switch to, prompting with PROMPT.
6611 Return the name of the buffer as a string.
6613 This function is intended for the `switch-to-buffer' family of
6614 commands since these need to omit the name of the current buffer
6615 from the list of completions and default values."
6616 (let ((rbts-completion-table (internal-complete-buffer-except)))
6617 (minibuffer-with-setup-hook
6619 (setq minibuffer-completion-table rbts-completion-table
)
6620 ;; Since rbts-completion-table is built dynamically, we
6621 ;; can't just add it to the default value of
6622 ;; icomplete-with-completion-tables, so we add it
6624 (if (and (boundp 'icomplete-with-completion-tables
)
6625 (listp icomplete-with-completion-tables
))
6626 (set (make-local-variable 'icomplete-with-completion-tables
)
6627 (cons rbts-completion-table
6628 icomplete-with-completion-tables
))))
6629 (read-buffer prompt
(other-buffer (current-buffer))
6630 (confirm-nonexistent-file-or-buffer)))))
6632 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
6633 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
6634 If BUFFER-OR-NAME is nil, return the buffer returned by
6635 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
6636 exists, return that buffer. If no such buffer exists, create a
6637 buffer with the name BUFFER-OR-NAME and return that buffer."
6639 (or (get-buffer buffer-or-name
)
6640 (let ((buffer (get-buffer-create buffer-or-name
)))
6641 (set-buffer-major-mode buffer
)
6645 (defcustom switch-to-buffer-preserve-window-point nil
6646 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
6647 If this is nil, `switch-to-buffer' displays the buffer at that
6648 buffer's `point'. If this is `already-displayed', it tries to
6649 display the buffer at its previous position in the selected
6650 window, provided the buffer is currently displayed in some other
6651 window on any visible or iconified frame. If this is t, it
6652 unconditionally tries to display the buffer at its previous
6653 position in the selected window.
6655 This variable is ignored if the buffer is already displayed in
6656 the selected window or never appeared in it before, or if
6657 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
6659 (const :tag
"Never" nil
)
6660 (const :tag
"If already displayed elsewhere" already-displayed
)
6661 (const :tag
"Always" t
))
6665 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window
)
6666 "Display buffer BUFFER-OR-NAME in the selected window.
6668 WARNING: This is NOT the way to work on another buffer temporarily
6669 within a Lisp program! Use `set-buffer' instead. That avoids
6670 messing with the window-buffer correspondences.
6672 If the selected window cannot display the specified
6673 buffer (e.g. if it is a minibuffer window or strongly dedicated
6674 to another buffer), call `pop-to-buffer' to select the buffer in
6677 If called interactively, read the buffer name using the
6678 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6679 determines whether to request confirmation before creating a new
6682 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
6683 If BUFFER-OR-NAME is a string that does not identify an existing
6684 buffer, create a buffer with that name. If BUFFER-OR-NAME is
6685 nil, switch to the buffer returned by `other-buffer'.
6687 If optional argument NORECORD is non-nil, do not put the buffer
6688 at the front of the buffer list, and do not make the window
6689 displaying it the most recently selected one.
6691 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
6692 must be displayed in the selected window; if that is impossible,
6693 signal an error rather than calling `pop-to-buffer'.
6695 The option `switch-to-buffer-preserve-window-point' can be used
6696 to make the buffer appear at its last position in the selected
6699 Return the buffer switched to."
6701 (list (read-buffer-to-switch "Switch to buffer: ") nil
'force-same-window
))
6702 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name
)))
6704 ;; Don't call set-window-buffer if it's not needed since it
6705 ;; might signal an error (e.g. if the window is dedicated).
6706 ((eq buffer
(window-buffer)))
6707 ((window-minibuffer-p)
6708 (if force-same-window
6709 (user-error "Cannot switch buffers in minibuffer window")
6710 (pop-to-buffer buffer norecord
)))
6711 ((eq (window-dedicated-p) t
)
6712 (if force-same-window
6713 (user-error "Cannot switch buffers in a dedicated window")
6714 (pop-to-buffer buffer norecord
)))
6716 (let* ((entry (assq buffer
(window-prev-buffers)))
6717 (displayed (and (eq switch-to-buffer-preserve-window-point
6719 (get-buffer-window buffer
0))))
6720 (set-window-buffer nil buffer
)
6722 (or (eq switch-to-buffer-preserve-window-point t
)
6724 ;; Try to restore start and point of buffer in the selected
6725 ;; window (Bug#4041).
6726 (set-window-start (selected-window) (nth 1 entry
) t
)
6727 (set-window-point nil
(nth 2 entry
))))))
6730 (select-window (selected-window)))
6731 (set-buffer buffer
)))
6733 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord
)
6734 "Select the buffer specified by BUFFER-OR-NAME in another window.
6735 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6736 nil. Return the buffer switched to.
6738 If called interactively, prompt for the buffer name using the
6739 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6740 determines whether to request confirmation before creating a new
6743 If BUFFER-OR-NAME is a string and does not identify an existing
6744 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6745 nil, switch to the buffer returned by `other-buffer'.
6747 Optional second argument NORECORD non-nil means do not put this
6748 buffer at the front of the list of recently selected ones.
6750 This uses the function `display-buffer' as a subroutine; see its
6751 documentation for additional customization information."
6753 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
6754 (let ((pop-up-windows t
))
6755 (pop-to-buffer buffer-or-name t norecord
)))
6757 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord
)
6758 "Switch to buffer BUFFER-OR-NAME in another frame.
6759 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6760 nil. Return the buffer switched to.
6762 If called interactively, prompt for the buffer name using the
6763 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6764 determines whether to request confirmation before creating a new
6767 If BUFFER-OR-NAME is a string and does not identify an existing
6768 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6769 nil, switch to the buffer returned by `other-buffer'.
6771 Optional second arg NORECORD non-nil means do not put this
6772 buffer at the front of the list of recently selected ones.
6774 This uses the function `display-buffer' as a subroutine; see its
6775 documentation for additional customization information."
6777 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
6778 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord
))
6780 (defun set-window-text-height (window height
)
6781 "Set the height in lines of the text display area of WINDOW to HEIGHT.
6782 WINDOW must be a live window and defaults to the selected one.
6783 HEIGHT doesn't include the mode line or header line, if any, or
6784 any partial-height lines in the text display area.
6786 Note that the current implementation of this function cannot
6787 always set the height exactly, but attempts to be conservative,
6788 by allocating more lines than are actually needed in the case
6789 where some error may be present."
6790 (setq window
(window-normalize-window window t
))
6791 (let ((delta (- height
(window-text-height window
))))
6792 (unless (zerop delta
)
6793 ;; Setting window-min-height to a value like 1 can lead to very
6794 ;; bizarre displays because it also allows Emacs to make *other*
6795 ;; windows one line tall, which means that there's no more space
6796 ;; for the mode line.
6797 (let ((window-min-height (min 2 height
)))
6798 (window-resize window delta
)))))
6800 (defun enlarge-window-horizontally (delta)
6801 "Make selected window DELTA columns wider.
6802 Interactively, if no argument is given, make selected window one
6805 (enlarge-window delta t
))
6807 (defun shrink-window-horizontally (delta)
6808 "Make selected window DELTA columns narrower.
6809 Interactively, if no argument is given, make selected window one
6812 (shrink-window delta t
))
6814 (defun count-screen-lines (&optional beg end count-final-newline window
)
6815 "Return the number of screen lines in the region.
6816 The number of screen lines may be different from the number of actual lines,
6817 due to line breaking, display table, etc.
6819 Optional arguments BEG and END default to `point-min' and `point-max'
6822 If region ends with a newline, ignore it unless optional third argument
6823 COUNT-FINAL-NEWLINE is non-nil.
6825 The optional fourth argument WINDOW specifies the window used for obtaining
6826 parameters such as width, horizontal scrolling, and so on. The default is
6827 to use the selected window's parameters.
6829 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6830 regardless of which buffer is displayed in WINDOW. This makes possible to use
6831 `count-screen-lines' in any buffer, whether or not it is currently displayed
6834 (setq beg
(point-min)))
6836 (setq end
(point-max)))
6842 (narrow-to-region (min beg end
)
6843 (if (and (not count-final-newline
)
6844 (= ?
\n (char-before (max beg end
))))
6847 (goto-char (point-min))
6848 (1+ (vertical-motion (buffer-size) window
))))))
6850 (defun window-buffer-height (window)
6851 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
6852 WINDOW must be a live window and defaults to the selected one."
6853 (setq window
(window-normalize-window window t
))
6854 (with-current-buffer (window-buffer window
)
6856 (count-screen-lines (point-min) (point-max)
6857 ;; If buffer ends with a newline, ignore it when
6858 ;; counting height unless point is after it.
6862 ;;; Resizing windows and frames to fit their contents exactly.
6863 (defcustom fit-window-to-buffer-horizontally nil
6864 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
6865 If this is nil, `fit-window-to-buffer' never resizes windows
6866 horizontally. If this is `only', it can resize windows
6867 horizontally only. Any other value means `fit-window-to-buffer'
6868 can resize windows in both dimensions."
6873 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
6874 ;; counting title bar and outer borders.
6875 (defcustom fit-frame-to-buffer nil
6876 "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
6877 A frame is fit if and only if its root window is a live window
6878 and this option is non-nil. If this is `horizontally', frames
6879 are resized horizontally only. If this is `vertically', frames
6880 are resized vertically only. Any other non-nil value means
6881 frames can be resized in both dimensions."
6886 (defcustom fit-frame-to-buffer-margins
'(nil nil nil nil
)
6887 "Margins around frame for `fit-frame-to-buffer'.
6888 This specifies the numbers of pixels to be left free on the left,
6889 above, on the right, and below a frame fitted to its buffer. Set
6890 this to avoid obscuring other desktop objects like the taskbar.
6891 The default is nil for each side, which means to not add margins.
6893 The value specified here can be overridden for a specific frame
6894 by that frame's `fit-frame-to-buffer-margins' parameter, if
6895 present. See also `fit-frame-to-buffer-sizes'."
6901 :format
"%[LeftMargin%] %v "
6902 (const :tag
"None" :format
"%t" nil
)
6903 (integer :tag
"Pixels" :size
5))
6907 :format
"%[TopMargin%] %v "
6908 (const :tag
"None" :format
"%t" nil
)
6909 (integer :tag
"Pixels" :size
5))
6913 :format
"%[RightMargin%] %v "
6914 (const :tag
"None" :format
"%t" nil
)
6915 (integer :tag
"Pixels" :size
5))
6919 :format
"%[BottomMargin%] %v "
6920 (const :tag
"None" :format
"%t" nil
)
6921 (integer :tag
"Pixels" :size
5)))
6924 (defcustom fit-frame-to-buffer-sizes
'(nil nil nil nil
)
6925 "Size boundaries of frame for `fit-frame-to-buffer'.
6926 This list specifies the total maximum and minimum lines and
6927 maximum and minimum columns of the root window of any frame that
6928 shall be fit to its buffer. If any of these values is non-nil,
6929 it overrides the corresponding argument of `fit-frame-to-buffer'.
6931 On window systems where the menubar can wrap, fitting a frame to
6932 its buffer may swallow the last line(s). Specifying an
6933 appropriate minimum width value here can avoid such wrapping.
6935 See also `fit-frame-to-buffer-margins'."
6939 :tag
"Maximum Height"
6941 :format
"%[MaxHeight%] %v "
6942 (const :tag
"None" :format
"%t" nil
)
6943 (integer :tag
"Lines" :size
5))
6945 :tag
"Minimum Height"
6947 :format
"%[MinHeight%] %v "
6948 (const :tag
"None" :format
"%t" nil
)
6949 (integer :tag
"Lines" :size
5))
6951 :tag
"Maximum Width"
6953 :format
"%[MaxWidth%] %v "
6954 (const :tag
"None" :format
"%t" nil
)
6955 (integer :tag
"Columns" :size
5))
6957 :tag
"Minimum Width"
6959 :format
"%[MinWidth%] %v\n"
6960 (const :tag
"None" :format
"%t" nil
)
6961 (integer :tag
"Columns" :size
5)))
6964 (declare-function x-display-pixel-height
"xfns.c" (&optional terminal
))
6966 (defun window--sanitize-margin (margin left right
)
6967 "Return MARGIN if it's a number between LEFT and RIGHT."
6968 (when (and (numberp margin
)
6969 (<= left
(- right margin
)) (<= margin right
))
6972 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only
)
6973 "Adjust size of FRAME to display the contents of its buffer exactly.
6974 FRAME can be any live frame and defaults to the selected one.
6975 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
6976 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
6977 FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
6978 `window-min-height' and `window-min-width' respectively.
6980 If the optional argument ONLY is `vertically', resize the frame
6981 vertically only. If ONLY is `horizontally', resize the frame
6984 The new position and size of FRAME can be additionally determined
6985 by customizing the options `fit-frame-to-buffer-sizes' and
6986 `fit-frame-to-buffer-margins' or the corresponding parameters of
6989 (unless (and (fboundp 'x-display-pixel-height
)
6990 ;; We need the respective sizes now.
6991 (fboundp 'display-monitor-attributes-list
))
6992 (user-error "Cannot resize frame in non-graphic Emacs"))
6993 (setq frame
(window-normalize-frame frame
))
6994 (when (window-live-p (frame-root-window frame
))
6995 (with-selected-window (frame-root-window frame
)
6996 (let* ((window (frame-root-window frame
))
6997 (char-width (frame-char-width))
6998 (char-height (frame-char-height))
6999 (monitor-attributes (car (display-monitor-attributes-list
7000 (frame-parameter frame
'display
))))
7001 (geometry (cdr (assq 'geometry monitor-attributes
)))
7002 (display-width (- (nth 2 geometry
) (nth 0 geometry
)))
7003 (display-height (- (nth 3 geometry
) (nth 1 geometry
)))
7004 (workarea (cdr (assq 'workarea monitor-attributes
)))
7006 (margins (or (frame-parameter frame
'fit-frame-to-buffer-margins
)
7007 fit-frame-to-buffer-margins
))
7008 (left-margin (if (nth 0 margins
)
7009 (or (window--sanitize-margin
7010 (nth 0 margins
) 0 display-width
)
7013 (top-margin (if (nth 1 margins
)
7014 (or (window--sanitize-margin
7015 (nth 1 margins
) 0 display-height
)
7018 (workarea-width (nth 2 workarea
))
7019 (right-margin (if (nth 2 margins
)
7021 (or (window--sanitize-margin
7022 (nth 2 margins
) left-margin display-width
)
7025 (workarea-height (nth 3 workarea
))
7026 (bottom-margin (if (nth 3 margins
)
7028 (or (window--sanitize-margin
7029 (nth 3 margins
) top-margin display-height
)
7032 ;; The pixel width of FRAME (which does not include the
7033 ;; window manager's decorations).
7034 (frame-width (frame-pixel-width))
7035 ;; The pixel width of the body of FRAME's root window.
7036 (window-body-width (window-body-width nil t
))
7037 ;; The difference in pixels between total and body width of
7039 (window-extra-width (- (window-pixel-width) window-body-width
))
7040 ;; The difference in pixels between the frame's pixel width
7041 ;; and the window's body width. This is the space we can't
7043 (extra-width (- frame-width window-body-width
))
7044 ;; The maximum width we can use for fitting.
7045 (fit-width (- workarea-width extra-width
))
7046 ;; The pixel position of FRAME's left border. We usually
7047 ;; try to leave this alone.
7049 (let ((left (frame-parameter nil
'left
)))
7051 (funcall (car left
) (cadr left
))
7053 ;; The pixel height of FRAME (which does not include title
7054 ;; line, decorations, and sometimes neither the menu nor
7056 (frame-height (frame-pixel-height))
7057 ;; The pixel height of FRAME's root window (we don't care
7058 ;; about the window's body height since the return value of
7059 ;; `window-text-pixel-size' includes header and mode line).
7060 (window-height (window-pixel-height))
7061 ;; The difference in pixels between the frame's pixel
7062 ;; height and the window's height.
7063 (extra-height (- frame-height window-height
))
7064 ;; When tool-bar-mode is enabled and we just created a new
7065 ;; frame, reserve lines for toolbar resizing. Needed
7066 ;; because for reasons unknown to me Emacs (1) reserves one
7067 ;; line for the toolbar when making the initial frame and
7068 ;; toolbars are enabled, and (2) later adds the remaining
7069 ;; lines needed. Our code runs IN BETWEEN (1) and (2).
7070 ;; YMMV when you're on a system that behaves differently.
7071 (toolbar-extra-height
7072 (let ((quit-restore (window-parameter window
'quit-restore
))
7073 ;; This may have to change when we allow arbitrary
7074 ;; pixel height toolbars.
7075 (lines (tool-bar-height)))
7077 (if (and quit-restore
(eq (car quit-restore
) 'frame
)
7078 (not (zerop lines
)))
7081 ;; The pixel position of FRAME's top border.
7083 (let ((top (frame-parameter nil
'top
)))
7085 (funcall (car top
) (cadr top
))
7087 ;; Sanitize minimum and maximum sizes.
7088 (sizes (or (frame-parameter frame
'fit-frame-to-buffer-sizes
)
7089 fit-frame-to-buffer-sizes
))
7092 ((numberp (nth 0 sizes
)) (* (nth 0 sizes
) char-height
))
7093 ((numberp max-height
) (* max-height char-height
))
7094 (t display-height
)))
7097 ((numberp (nth 1 sizes
)) (* (nth 1 sizes
) char-height
))
7098 ((numberp min-height
) (* min-height char-height
))
7099 (t (* window-min-height char-height
))))
7102 ((numberp (nth 2 sizes
))
7103 (- (* (nth 2 sizes
) char-width
) window-extra-width
))
7104 ((numberp max-width
)
7105 (- (* max-width char-width
) window-extra-width
))
7109 ((numberp (nth 3 sizes
))
7110 (- (* (nth 3 sizes
) char-width
) window-extra-width
))
7111 ((numberp min-width
)
7112 (- (* min-width char-width
) window-extra-width
))
7113 (t (* window-min-width char-width
))))
7114 ;; Note: Currently, for a new frame the sizes of the header
7115 ;; and mode line may be estimated incorrectly
7116 (value (window-text-pixel-size
7117 nil t t workarea-width workarea-height t
))
7118 (width (+ (car value
) (window-right-divider-width)))
7119 (height (+ (cdr value
) (window-bottom-divider-width))))
7120 ;; Don't change height or width when the window's size is fixed
7121 ;; in either direction or ONLY forbids it.
7123 ((or (eq window-size-fixed
'width
) (eq only
'vertically
))
7125 ((or (eq window-size-fixed
'height
) (eq only
'horizontally
))
7127 ;; Fit width to constraints.
7129 (unless frame-resize-pixelwise
7130 ;; Round to character sizes.
7131 (setq width
(* (/ (+ width char-width -
1) char-width
)
7133 ;; Fit to maximum and minimum widths.
7134 (setq width
(max (min width max-width
) min-width
))
7136 (setq width
(+ width extra-width
))
7137 ;; Preserve margins.
7138 (let ((right (+ left width
)))
7140 ((> right right-margin
)
7141 ;; Move frame to left (we don't know its real width).
7142 (setq left
(max left-margin
(- left
(- right right-margin
)))))
7143 ((< left left-margin
)
7144 ;; Move frame to right.
7145 (setq left left-margin
)))))
7146 ;; Fit height to constraints.
7148 (unless frame-resize-pixelwise
7149 (setq height
(* (/ (+ height char-height -
1) char-height
)
7151 ;; Fit to maximum and minimum heights.
7152 (setq height
(max (min height max-height
) min-height
))
7153 ;; Add extra height.
7154 (setq height
(+ height extra-height
))
7155 ;; Preserve margins.
7156 (let ((bottom (+ top height
)))
7158 ((> bottom bottom-margin
)
7159 ;; Move frame up (we don't know its real height).
7160 (setq top
(max top-margin
(- top
(- bottom bottom-margin
)))))
7163 (setq top top-margin
)))))
7165 (set-frame-position frame left top
)
7166 ;; Clumsily try to translate our calculations to what
7167 ;; `set-frame-size' wants.
7169 (setq width
(- (+ (frame-text-width) width
)
7170 extra-width window-body-width
)))
7172 (setq height
(- (+ (frame-text-height) height
)
7173 extra-height window-height
)))
7177 (if frame-resize-pixelwise
7179 (/ width char-width
))
7182 (if frame-resize-pixelwise
7184 (/ height char-height
))
7185 (frame-text-height))
7186 frame-resize-pixelwise
)))))
7188 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width
)
7189 "Adjust size of WINDOW to display its buffer's contents exactly.
7190 WINDOW must be a live window and defaults to the selected one.
7192 If WINDOW is part of a vertical combination, adjust WINDOW's
7193 height. The new height is calculated from the actual height of
7194 the accessible portion of its buffer. The optional argument
7195 MAX-HEIGHT specifies a maximum height and defaults to the height
7196 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
7197 minimum height and defaults to `window-min-height'. Both
7198 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
7199 and header line and a bottom divider, if any.
7201 If WINDOW is part of a horizontal combination and the value of
7202 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
7203 WINDOW's height. The new width of WINDOW is calculated from the
7204 maximum length of its buffer's lines that follow the current
7205 start position of WINDOW. The optional argument MAX-WIDTH
7206 specifies a maximum width and defaults to the width of WINDOW's
7207 frame. The optional argument MIN-WIDTH specifies a minimum width
7208 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
7209 are specified in columns and include fringes, margins, a
7210 scrollbar and a vertical divider, if any.
7212 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
7213 If WINDOW is its frame's root window and the option
7214 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
7215 adjust the frame's size.
7217 Note that even if this function makes WINDOW large enough to show
7218 _all_ parts of its buffer you might not see the first part when
7219 WINDOW was scrolled. If WINDOW is resized horizontally, you will
7220 not see the top of its buffer unless WINDOW starts at its minimum
7221 accessible position."
7223 (setq window
(window-normalize-window window t
))
7224 (if (eq window
(frame-root-window window
))
7225 (when fit-frame-to-buffer
7226 ;; Fit WINDOW's frame to buffer.
7227 (fit-frame-to-buffer
7228 (window-frame window
)
7229 max-height min-height max-width min-width
7230 (and (memq fit-frame-to-buffer
'(vertically horizontally
))
7231 fit-frame-to-buffer
)))
7232 (with-selected-window window
7233 (let* ((pixelwise window-resize-pixelwise
)
7234 (char-height (frame-char-height))
7235 (char-width (frame-char-width))
7236 (total-height (window-size window nil pixelwise
))
7237 (body-height (window-body-height window pixelwise
))
7238 (body-width (window-body-width window pixelwise
))
7240 ;; Sanitize MIN-HEIGHT.
7241 (if (numberp min-height
)
7242 ;; Can't get smaller than `window-safe-min-height'.
7244 (* char-height min-height
)
7247 (window-safe-min-pixel-height window
)
7248 window-safe-min-height
))
7249 ;; Preserve header and mode line if present.
7251 (* char-height window-min-height
)
7253 (window-min-size nil nil t pixelwise
))))
7255 ;; Sanitize MAX-HEIGHT.
7256 (if (numberp max-height
)
7260 window nil nil nil nil nil pixelwise
))
7262 (* char-height max-height
)
7264 (+ total-height
(window-max-delta
7265 window nil nil nil nil nil pixelwise
))))
7268 ;; If WINDOW is vertically combined, try to resize it
7270 ((and (not (eq fit-window-to-buffer-horizontally
'only
))
7271 (not (window-size-fixed-p window
))
7272 (window-combined-p))
7273 ;; Vertically we always want to fit the entire buffer.
7274 ;; WINDOW'S height can't get larger than its frame's pixel
7275 ;; height. Its width remains fixed.
7276 (setq height
(+ (cdr (window-text-pixel-size
7277 nil nil t nil
(frame-pixel-height) t
))
7278 (window-bottom-divider-width)))
7281 (setq height
(/ (+ height char-height -
1) char-height
)))
7282 (unless (= height total-height
)
7283 (window-resize-no-error
7285 (- (max min-height
(min max-height height
)) total-height
)
7286 nil window pixelwise
)))
7287 ;; If WINDOW is horizontally combined, try to resize it
7289 ((and fit-window-to-buffer-horizontally
7290 (not (window-size-fixed-p window t
))
7291 (window-combined-p nil t
))
7292 (let* ((total-width (window-size window t pixelwise
))
7294 ;; Sanitize MIN-WIDTH.
7295 (if (numberp min-width
)
7296 ;; Can't get smaller than `window-safe-min-width'.
7298 (* char-width min-width
)
7301 (window-safe-min-pixel-width)
7302 window-safe-min-width
))
7303 ;; Preserve fringes, margins, scrollbars if present.
7305 (* char-width window-min-width
)
7307 (window-min-size nil nil t pixelwise
))))
7309 ;; Sanitize MAX-WIDTH.
7310 (if (numberp max-width
)
7313 nil t nil nil nil nil pixelwise
))
7315 (* char-width max-width
)
7317 (+ total-width
(window-max-delta
7318 nil t nil nil nil nil pixelwise
))))
7319 ;; When fitting vertically, assume that WINDOW's start
7320 ;; position remains unaltered. WINDOW can't get wider
7321 ;; than its frame's pixel width, its height remains
7323 (width (+ (car (window-text-pixel-size
7324 nil
(window-start) (point-max)
7326 ;; Add one char-height to assure that
7327 ;; we're on the safe side. This
7328 ;; overshoots when the first line below
7329 ;; the bottom is wider than the window.
7331 (if pixelwise char-height
1))))
7332 (window-right-divider-width))))
7334 (setq width
(/ (+ width char-width -
1) char-width
)))
7335 (unless (= width body-width
)
7336 (window-resize-no-error
7340 (+ total-width
(- width body-width
))))
7342 t window pixelwise
)))))))))
7344 (defun window-safely-shrinkable-p (&optional window
)
7345 "Return t if WINDOW can be shrunk without shrinking other windows.
7346 WINDOW defaults to the selected window."
7347 (with-selected-window (or window
(selected-window))
7348 (let ((edges (window-edges)))
7349 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
7350 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
7352 (defun shrink-window-if-larger-than-buffer (&optional window
)
7353 "Shrink height of WINDOW if its buffer doesn't need so many lines.
7354 More precisely, shrink WINDOW vertically to be as small as
7355 possible, while still showing the full contents of its buffer.
7356 WINDOW must be a live window and defaults to the selected one.
7358 Do not shrink WINDOW to less than `window-min-height' lines. Do
7359 nothing if the buffer contains more lines than the present window
7360 height, or if some of the window's contents are scrolled out of
7361 view, or if shrinking this window would also shrink another
7362 window, or if the window is the only window of its frame.
7364 Return non-nil if the window was shrunk, nil otherwise."
7366 (setq window
(window-normalize-window window t
))
7367 ;; Make sure that WINDOW is vertically combined and `point-min' is
7368 ;; visible (for whatever reason that's needed). The remaining issues
7369 ;; should be taken care of by `fit-window-to-buffer'.
7370 (when (and (window-combined-p window
)
7371 (pos-visible-in-window-p (point-min) window
))
7372 (fit-window-to-buffer window
(window-total-height window
))))
7374 (defun kill-buffer-and-window ()
7375 "Kill the current buffer and delete the selected window."
7377 (let ((window-to-delete (selected-window))
7378 (buffer-to-kill (current-buffer))
7379 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
7382 (add-hook 'kill-buffer-hook delete-window-hook t t
)
7383 (if (kill-buffer (current-buffer))
7384 ;; If `delete-window' failed before, we rerun it to regenerate
7385 ;; the error so it can be seen in the echo area.
7386 (when (eq (selected-window) window-to-delete
)
7388 ;; If the buffer is not dead for some reason (probably because
7389 ;; of a `quit' signal), remove the hook again.
7391 (with-current-buffer buffer-to-kill
7392 (remove-hook 'kill-buffer-hook delete-window-hook t
))))))
7395 (defvar recenter-last-op nil
7396 "Indicates the last recenter operation performed.
7397 Possible values: `top', `middle', `bottom', integer or float numbers.")
7399 (defcustom recenter-positions
'(middle top bottom
)
7400 "Cycling order for `recenter-top-bottom'.
7401 A list of elements with possible values `top', `middle', `bottom',
7402 integer or float numbers that define the cycling order for
7403 the command `recenter-top-bottom'.
7405 Top and bottom destinations are `scroll-margin' lines from the true
7406 window top and bottom. Middle redraws the frame and centers point
7407 vertically within the window. Integer number moves current line to
7408 the specified absolute window-line. Float number between 0.0 and 1.0
7409 means the percentage of the screen space from the top. The default
7410 cycling order is middle -> top -> bottom."
7411 :type
'(repeat (choice
7412 (const :tag
"Top" top
)
7413 (const :tag
"Middle" middle
)
7414 (const :tag
"Bottom" bottom
)
7415 (integer :tag
"Line number")
7416 (float :tag
"Percentage")))
7420 (defun recenter-top-bottom (&optional arg
)
7421 "Move current buffer line to the specified window line.
7422 With no prefix argument, successive calls place point according
7423 to the cycling order defined by `recenter-positions'.
7425 A prefix argument is handled like `recenter':
7426 With numeric prefix ARG, move current line to window-line ARG.
7427 With plain `C-u', move current line to window center."
7430 (arg (recenter arg
)) ; Always respect ARG.
7432 (setq recenter-last-op
7433 (if (eq this-command last-command
)
7434 (car (or (cdr (member recenter-last-op recenter-positions
))
7435 recenter-positions
))
7436 (car recenter-positions
)))
7437 (let ((this-scroll-margin
7438 (min (max 0 scroll-margin
)
7439 (truncate (/ (window-body-height) 4.0)))))
7440 (cond ((eq recenter-last-op
'middle
)
7442 ((eq recenter-last-op
'top
)
7443 (recenter this-scroll-margin
))
7444 ((eq recenter-last-op
'bottom
)
7445 (recenter (- -
1 this-scroll-margin
)))
7446 ((integerp recenter-last-op
)
7447 (recenter recenter-last-op
))
7448 ((floatp recenter-last-op
)
7449 (recenter (round (* recenter-last-op
(window-height))))))))))
7451 (define-key global-map
[?\C-l
] 'recenter-top-bottom
)
7453 (defun move-to-window-line-top-bottom (&optional arg
)
7454 "Position point relative to window.
7456 With a prefix argument ARG, acts like `move-to-window-line'.
7458 With no argument, positions point at center of window.
7459 Successive calls position point at positions defined
7460 by `recenter-positions'."
7463 (arg (move-to-window-line arg
)) ; Always respect ARG.
7465 (setq recenter-last-op
7466 (if (eq this-command last-command
)
7467 (car (or (cdr (member recenter-last-op recenter-positions
))
7468 recenter-positions
))
7469 (car recenter-positions
)))
7470 (let ((this-scroll-margin
7471 (min (max 0 scroll-margin
)
7472 (truncate (/ (window-body-height) 4.0)))))
7473 (cond ((eq recenter-last-op
'middle
)
7474 (call-interactively 'move-to-window-line
))
7475 ((eq recenter-last-op
'top
)
7476 (move-to-window-line this-scroll-margin
))
7477 ((eq recenter-last-op
'bottom
)
7478 (move-to-window-line (- -
1 this-scroll-margin
)))
7479 ((integerp recenter-last-op
)
7480 (move-to-window-line recenter-last-op
))
7481 ((floatp recenter-last-op
)
7482 (move-to-window-line (round (* recenter-last-op
(window-height))))))))))
7484 (define-key global-map
[?\M-r
] 'move-to-window-line-top-bottom
)
7486 ;;; Scrolling commands.
7488 ;;; Scrolling commands which do not signal errors at top/bottom
7489 ;;; of buffer at first key-press (instead move to top/bottom
7492 (defcustom scroll-error-top-bottom nil
7493 "Move point to top/bottom of buffer before signaling a scrolling error.
7494 A value of nil means just signal an error if no more scrolling possible.
7495 A value of t means point moves to the beginning or the end of the buffer
7496 \(depending on scrolling direction) when no more scrolling possible.
7497 When point is already on that position, then signal an error."
7502 (defun scroll-up-command (&optional arg
)
7503 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
7504 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
7505 scroll window further, move cursor to the bottom line.
7506 When point is already on that position, then signal an error.
7507 A near full screen is `next-screen-context-lines' less than a full screen.
7508 Negative ARG means scroll downward.
7509 If ARG is the atom `-', scroll downward by nearly full screen."
7512 ((null scroll-error-top-bottom
)
7515 (scroll-down-command nil
))
7516 ((< (prefix-numeric-value arg
) 0)
7517 (scroll-down-command (- (prefix-numeric-value arg
))))
7519 (scroll-up arg
)) ; signal error
7525 ;; When scrolling by ARG lines can't be done,
7526 ;; move by ARG lines instead.
7528 ;; When ARG is nil for full-screen scrolling,
7529 ;; move to the bottom of the buffer.
7530 (goto-char (point-max))))))))
7532 (put 'scroll-up-command
'scroll-command t
)
7534 (defun scroll-down-command (&optional arg
)
7535 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
7536 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
7537 scroll window further, move cursor to the top line.
7538 When point is already on that position, then signal an error.
7539 A near full screen is `next-screen-context-lines' less than a full screen.
7540 Negative ARG means scroll upward.
7541 If ARG is the atom `-', scroll upward by nearly full screen."
7544 ((null scroll-error-top-bottom
)
7547 (scroll-up-command nil
))
7548 ((< (prefix-numeric-value arg
) 0)
7549 (scroll-up-command (- (prefix-numeric-value arg
))))
7551 (scroll-down arg
)) ; signal error
7555 (beginning-of-buffer
7557 ;; When scrolling by ARG lines can't be done,
7558 ;; move by ARG lines instead.
7559 (forward-line (- arg
))
7560 ;; When ARG is nil for full-screen scrolling,
7561 ;; move to the top of the buffer.
7562 (goto-char (point-min))))))))
7564 (put 'scroll-down-command
'scroll-command t
)
7566 ;;; Scrolling commands which scroll a line instead of full screen.
7568 (defun scroll-up-line (&optional arg
)
7569 "Scroll text of selected window upward ARG lines; or one line if no ARG.
7570 If ARG is omitted or nil, scroll upward by one line.
7571 This is different from `scroll-up-command' that scrolls a full screen."
7573 (scroll-up (or arg
1)))
7575 (put 'scroll-up-line
'scroll-command t
)
7577 (defun scroll-down-line (&optional arg
)
7578 "Scroll text of selected window down ARG lines; or one line if no ARG.
7579 If ARG is omitted or nil, scroll down by one line.
7580 This is different from `scroll-down-command' that scrolls a full screen."
7582 (scroll-down (or arg
1)))
7584 (put 'scroll-down-line
'scroll-command t
)
7587 (defun scroll-other-window-down (&optional lines
)
7588 "Scroll the \"other window\" down.
7589 For more details, see the documentation for `scroll-other-window'."
7591 (scroll-other-window
7592 ;; Just invert the argument's meaning.
7593 ;; We can do that without knowing which window it will be.
7594 (if (eq lines
'-
) nil
7596 (- (prefix-numeric-value lines
))))))
7598 (defun beginning-of-buffer-other-window (arg)
7599 "Move point to the beginning of the buffer in the other window.
7600 Leave mark at previous position.
7601 With arg N, put point N/10 of the way from the true beginning."
7603 (let ((orig-window (selected-window))
7604 (window (other-window-for-scrolling)))
7605 ;; We use unwind-protect rather than save-window-excursion
7606 ;; because the latter would preserve the things we want to change.
7609 (select-window window
)
7610 ;; Set point and mark in that window's buffer.
7612 (beginning-of-buffer arg
))
7613 ;; Set point accordingly.
7615 (select-window orig-window
))))
7617 (defun end-of-buffer-other-window (arg)
7618 "Move point to the end of the buffer in the other window.
7619 Leave mark at previous position.
7620 With arg N, put point N/10 of the way from the true end."
7622 ;; See beginning-of-buffer-other-window for comments.
7623 (let ((orig-window (selected-window))
7624 (window (other-window-for-scrolling)))
7627 (select-window window
)
7629 (end-of-buffer arg
))
7631 (select-window orig-window
))))
7633 (defvar mouse-autoselect-window-timer nil
7634 "Timer used by delayed window autoselection.")
7636 (defvar mouse-autoselect-window-position nil
7637 "Last mouse position recorded by delayed window autoselection.")
7639 (defvar mouse-autoselect-window-window nil
7640 "Last window recorded by delayed window autoselection.")
7642 (defvar mouse-autoselect-window-state nil
7643 "When non-nil, special state of delayed window autoselection.
7644 Possible values are `suspend' (suspend autoselection after a menu or
7645 scrollbar interaction) and `select' (the next invocation of
7646 `handle-select-window' shall select the window immediately).")
7648 (defun mouse-autoselect-window-cancel (&optional force
)
7649 "Cancel delayed window autoselection.
7650 Optional argument FORCE means cancel unconditionally."
7651 (unless (and (not force
)
7652 ;; Don't cancel for select-window or select-frame events
7653 ;; or when the user drags a scroll bar.
7654 (or (memq this-command
7655 '(handle-select-window handle-switch-frame
))
7656 (and (eq this-command
'scroll-bar-toolkit-scroll
)
7657 (memq (nth 4 (event-end last-input-event
))
7658 '(handle end-scroll
)))))
7659 (setq mouse-autoselect-window-state nil
)
7660 (when (timerp mouse-autoselect-window-timer
)
7661 (cancel-timer mouse-autoselect-window-timer
))
7662 (remove-hook 'pre-command-hook
'mouse-autoselect-window-cancel
)))
7664 (defun mouse-autoselect-window-start (mouse-position &optional window suspend
)
7665 "Start delayed window autoselection.
7666 MOUSE-POSITION is the last position where the mouse was seen as returned
7667 by `mouse-position'. Optional argument WINDOW non-nil denotes the
7668 window where the mouse was seen. Optional argument SUSPEND non-nil
7669 means suspend autoselection."
7670 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
7671 (setq mouse-autoselect-window-position mouse-position
)
7672 (when window
(setq mouse-autoselect-window-window window
))
7673 (setq mouse-autoselect-window-state
(when suspend
'suspend
))
7674 ;; Install timer which runs `mouse-autoselect-window-select' after
7675 ;; `mouse-autoselect-window' seconds.
7676 (setq mouse-autoselect-window-timer
7678 (abs mouse-autoselect-window
) nil
'mouse-autoselect-window-select
)))
7680 (defun mouse-autoselect-window-select ()
7681 "Select window with delayed window autoselection.
7682 If the mouse position has stabilized in a non-selected window, select
7683 that window. The minibuffer window is selected only if the minibuffer
7684 is active. This function is run by `mouse-autoselect-window-timer'."
7686 (let* ((mouse-position (mouse-position))
7689 (window-at (cadr mouse-position
) (cddr mouse-position
)
7690 (car mouse-position
)))))
7692 ((or (and (fboundp 'menu-or-popup-active-p
) (menu-or-popup-active-p))
7694 (let ((coords (coordinates-in-window-p
7695 (cdr mouse-position
) window
)))
7696 (and (not (consp coords
))
7697 (not (memq coords
'(left-margin right-margin
)))))))
7698 ;; A menu / popup dialog is active or the mouse is not on the
7699 ;; text region of WINDOW: Suspend autoselection temporarily.
7700 (mouse-autoselect-window-start mouse-position nil t
))
7701 ((eq mouse-autoselect-window-state
'suspend
)
7702 ;; Delayed autoselection was temporarily suspended, reenable it.
7703 (mouse-autoselect-window-start mouse-position
))
7704 ((and window
(not (eq window
(selected-window)))
7705 (or (not (numberp mouse-autoselect-window
))
7706 (and (> mouse-autoselect-window
0)
7707 ;; If `mouse-autoselect-window' is positive, select
7708 ;; window if the window is the same as before.
7709 (eq window mouse-autoselect-window-window
))
7710 ;; Otherwise select window if the mouse is at the same
7711 ;; position as before. Observe that the first test after
7712 ;; starting autoselection usually fails since the value of
7713 ;; `mouse-autoselect-window-position' recorded there is the
7714 ;; position where the mouse has entered the new window and
7715 ;; not necessarily where the mouse has stopped moving.
7716 (equal mouse-position mouse-autoselect-window-position
))
7717 ;; The minibuffer is a candidate window if it's active.
7718 (or (not (window-minibuffer-p window
))
7719 (eq window
(active-minibuffer-window))))
7720 ;; Mouse position has stabilized in non-selected window: Cancel
7721 ;; delayed autoselection and try to select that window.
7722 (mouse-autoselect-window-cancel t
)
7723 ;; Select window where mouse appears unless the selected window is the
7724 ;; minibuffer. Use `unread-command-events' in order to execute pre-
7725 ;; and post-command hooks and trigger idle timers. To avoid delaying
7726 ;; autoselection again, set `mouse-autoselect-window-state'."
7727 (unless (window-minibuffer-p)
7728 (setq mouse-autoselect-window-state
'select
)
7729 (setq unread-command-events
7730 (cons (list 'select-window
(list window
))
7731 unread-command-events
))))
7732 ((or (and window
(eq window
(selected-window)))
7733 (not (numberp mouse-autoselect-window
))
7734 (equal mouse-position mouse-autoselect-window-position
))
7735 ;; Mouse position has either stabilized in the selected window or at
7736 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
7737 (mouse-autoselect-window-cancel t
))
7739 ;; Mouse position has not stabilized yet, resume delayed
7741 (mouse-autoselect-window-start mouse-position window
))))))
7743 (defun handle-select-window (event)
7744 "Handle select-window events."
7746 (let ((window (posn-window (event-start event
))))
7747 (unless (or (not (window-live-p window
))
7748 ;; Don't switch if we're currently in the minibuffer.
7749 ;; This tries to work around problems where the
7750 ;; minibuffer gets unselected unexpectedly, and where
7751 ;; you then have to move your mouse all the way down to
7752 ;; the minibuffer to select it.
7753 (window-minibuffer-p)
7754 ;; Don't switch to minibuffer window unless it's active.
7755 (and (window-minibuffer-p window
)
7756 (not (minibuffer-window-active-p window
)))
7757 ;; Don't switch when autoselection shall be delayed.
7758 (and (numberp mouse-autoselect-window
)
7759 (not (zerop mouse-autoselect-window
))
7760 (not (eq mouse-autoselect-window-state
'select
))
7762 ;; Cancel any delayed autoselection.
7763 (mouse-autoselect-window-cancel t
)
7764 ;; Start delayed autoselection from current mouse
7765 ;; position and window.
7766 (mouse-autoselect-window-start (mouse-position) window
)
7767 ;; Executing a command cancels delayed autoselection.
7769 'pre-command-hook
'mouse-autoselect-window-cancel
))))
7770 (when mouse-autoselect-window
7771 ;; Reset state of delayed autoselection.
7772 (setq mouse-autoselect-window-state nil
)
7773 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
7774 (run-hooks 'mouse-leave-buffer-hook
))
7777 (select-window window
))))
7779 (defun truncated-partial-width-window-p (&optional window
)
7780 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
7781 WINDOW must be a live window and defaults to the selected one.
7782 Return nil if WINDOW is not a partial-width window
7783 (regardless of the value of `truncate-lines').
7784 Otherwise, consult the value of `truncate-partial-width-windows'
7785 for the buffer shown in WINDOW."
7786 (setq window
(window-normalize-window window t
))
7787 (unless (window-full-width-p window
)
7788 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
7789 (window-buffer window
))))
7790 (if (integerp t-p-w-w
)
7791 (< (window-width window
) t-p-w-w
)
7794 ;; Some of these are in tutorial--default-keys, so update that if you
7796 (define-key ctl-x-map
"0" 'delete-window
)
7797 (define-key ctl-x-map
"1" 'delete-other-windows
)
7798 (define-key ctl-x-map
"2" 'split-window-below
)
7799 (define-key ctl-x-map
"3" 'split-window-right
)
7800 (define-key ctl-x-map
"o" 'other-window
)
7801 (define-key ctl-x-map
"^" 'enlarge-window
)
7802 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
7803 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
7804 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
7805 (define-key ctl-x-map
"+" 'balance-windows
)
7806 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
7808 ;;; window.el ends here