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 ;; Doc is very similar to with-output-to-temp-buffer.
146 (defmacro with-temp-buffer-window
(buffer-or-name action quit-function
&rest body
)
147 "Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
148 BUFFER-OR-NAME must specify either a live buffer, or the name of a
149 buffer (if it does not exist, this macro creates it).
151 This construct makes buffer BUFFER-OR-NAME empty before running BODY.
152 It does not make the buffer current for BODY.
153 Instead it binds `standard-output' to that buffer, so that output
154 generated with `prin1' and similar functions in BODY goes into
157 At the end of BODY, this marks the specified buffer unmodified and
158 read-only, and displays it in a window (but does not select it, or make
159 the buffer current). The display happens by calling `display-buffer'
160 with the ACTION argument. If `temp-buffer-resize-mode' is enabled,
161 the relevant window shrinks automatically.
163 This returns the value returned by BODY, unless QUIT-FUNCTION specifies
164 a function. In that case, it runs the function with two arguments -
165 the window showing the specified buffer and the value returned by
166 BODY - and returns 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
176 hook `temp-buffer-window-show-hook' after displaying the buffer,
177 with 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 runs different hooks. In particular, it does not run
182 `temp-buffer-setup-hook', which usually puts the buffer in Help mode.
183 Also, it does not call `temp-buffer-show-function' (the ACTION
184 argument replaces this)."
186 (let ((buffer (make-symbol "buffer"))
187 (window (make-symbol "window"))
188 (value (make-symbol "value")))
189 `(let* ((,buffer
(temp-buffer-window-setup ,buffer-or-name
))
190 (standard-output ,buffer
)
192 (setq ,value
(progn ,@body
))
193 (with-current-buffer ,buffer
194 (setq ,window
(temp-buffer-window-show ,buffer
,action
)))
196 (if (functionp ,quit-function
)
197 (funcall ,quit-function
,window
,value
)
200 ;; The following two functions are like `window-next-sibling' and
201 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
202 ;; they don't substitute the selected window for nil), and they return
203 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
204 ;; a minibuffer window).
205 (defun window-right (window)
206 "Return WINDOW's right sibling.
207 Return nil if WINDOW is the root window of its frame. WINDOW can
209 (and window
(window-parent window
) (window-next-sibling window
)))
211 (defun window-left (window)
212 "Return WINDOW's left sibling.
213 Return nil if WINDOW is the root window of its frame. WINDOW can
215 (and window
(window-parent window
) (window-prev-sibling window
)))
217 (defun window-child (window)
218 "Return WINDOW's first child window.
219 WINDOW can be any window."
220 (or (window-top-child window
) (window-left-child window
)))
222 (defun window-child-count (window)
223 "Return number of WINDOW's child windows.
224 WINDOW can be any window."
226 (when (and (windowp window
) (setq window
(window-child window
)))
228 (setq count
(1+ count
))
229 (setq window
(window-next-sibling window
))))
232 (defun window-last-child (window)
233 "Return last child window of WINDOW.
234 WINDOW can be any window."
235 (when (and (windowp window
) (setq window
(window-child window
)))
236 (while (window-next-sibling window
)
237 (setq window
(window-next-sibling window
))))
240 (defun window-normalize-buffer (buffer-or-name)
241 "Return buffer specified by BUFFER-OR-NAME.
242 BUFFER-OR-NAME must be either a buffer or a string naming a live
243 buffer and defaults to the current buffer."
245 ((not buffer-or-name
)
247 ((bufferp buffer-or-name
)
248 (if (buffer-live-p buffer-or-name
)
250 (error "Buffer %s is not a live buffer" buffer-or-name
)))
251 ((get-buffer buffer-or-name
))
253 (error "No such buffer %s" buffer-or-name
))))
255 (defun window-normalize-frame (frame)
256 "Return frame specified by FRAME.
257 FRAME must be a live frame and defaults to the selected frame."
259 (if (frame-live-p frame
)
261 (error "%s is not a live frame" frame
))
264 (defun window-normalize-window (window &optional live-only
)
265 "Return the window specified by WINDOW.
266 If WINDOW is nil, return the selected window. Otherwise, if
267 WINDOW is a live or an internal window, return WINDOW; if
268 LIVE-ONLY is non-nil, return WINDOW for a live window only.
269 Otherwise, signal an error."
274 (if (window-live-p window
)
276 (error "%s is not a live window" window
)))
277 ((window-valid-p window
)
280 (error "%s is not a valid window" window
))))
282 ;; Maybe this should go to frame.el.
283 (defun frame-char-size (&optional window-or-frame horizontal
)
284 "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
285 If WINDOW-OR-FRAME is a live frame, return the value of
286 `frame-char-height' for that frame. If WINDOW-OR-FRAME is a
287 valid window, return the value of `frame-char-height' for that
288 window's frame. In any other case, return the value of
289 `frame-char-height' for the selected frame.
291 Optional argument HORIZONTAL non-nil means to return the value of
292 `frame-char-width' for WINDOW-OR-FRAME."
295 ((window-valid-p window-or-frame
)
296 (window-frame window-or-frame
))
297 ((frame-live-p window-or-frame
)
299 (t (selected-frame)))))
301 (frame-char-width frame
)
302 (frame-char-height frame
))))
304 (defvar ignore-window-parameters nil
305 "If non-nil, standard functions ignore window parameters.
306 The functions currently affected by this are `split-window',
307 `delete-window', `delete-other-windows' and `other-window'.
309 An application may bind this to a non-nil value around calls to
310 these functions to inhibit processing of window parameters.")
312 ;; This must go to C, finally (or get removed).
313 (defconst window-safe-min-height
1
314 "The absolute minimum number of lines of any window.
315 Anything less might crash Emacs.")
317 (defun window-safe-min-pixel-height (&optional window
)
318 "Return the absolute minimum pixel height of WINDOW."
319 (* window-safe-min-height
320 (frame-char-size (window-normalize-window window
))))
322 (defcustom window-min-height
4
323 "The minimum number of lines of any window.
324 The value has to accommodate a mode- or header-line if present.
325 A value less than `window-safe-min-height' is ignored. The value
326 of this variable is honored when windows are resized or split.
328 Applications should never rebind this variable. To resize a
329 window to a height less than the one specified here, an
330 application should instead call `window-resize' with a non-nil
331 IGNORE argument. In order to have `split-window' make a window
332 shorter, explicitly specify the SIZE argument of that function."
337 (defun window-min-pixel-height (&optional window
)
338 "Return the minimum pixel height of window WINDOW."
339 (* (max window-min-height window-safe-min-height
)
340 (frame-char-size window
)))
342 ;; This must go to C, finally (or get removed).
343 (defconst window-safe-min-width
2
344 "The absolute minimum number of columns of a window.
345 Anything less might crash Emacs.")
347 (defun window-safe-min-pixel-width (&optional window
)
348 "Return the absolute minimum pixel width of WINDOW."
349 (* window-safe-min-width
350 (frame-char-size (window-normalize-window window
) t
)))
352 (defcustom window-min-width
10
353 "The minimum number of columns of any window.
354 The value has to accommodate margins, fringes, or scrollbars if
355 present. A value less than `window-safe-min-width' is ignored.
356 The value of this variable is honored when windows are resized or
359 Applications should never rebind this variable. To resize a
360 window to a width less than the one specified here, an
361 application should instead call `window-resize' with a non-nil
362 IGNORE argument. In order to have `split-window' make a window
363 narrower, explicitly specify the SIZE argument of that function."
368 (defun window-min-pixel-width (&optional window
)
369 "Return the minimum pixel width of window WINDOW."
370 (* (max window-min-width window-safe-min-width
)
371 (frame-char-size window t
)))
373 (defun window-safe-min-pixel-size (&optional window horizontal
)
374 "Return the absolute minimum pixel height of WINDOW.
375 Optional argument HORIZONTAL non-nil means return the absolute
376 minimum pixel width of WINDOW."
378 (window-safe-min-pixel-width window
)
379 (window-safe-min-pixel-height window
)))
381 (defun window-combined-p (&optional window horizontal
)
382 "Return non-nil if WINDOW has siblings in a given direction.
383 WINDOW must be a valid window and defaults to the selected one.
385 HORIZONTAL determines a direction for the window combination. If
386 HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
387 a vertical window combination. If HORIZONTAL is non-nil, return
388 non-nil if WINDOW is part of a horizontal window combination."
389 (setq window
(window-normalize-window window
))
390 (let ((parent (window-parent window
)))
393 (window-left-child parent
)
394 (window-top-child parent
)))))
396 (defun window-combination-p (&optional window horizontal
)
397 "Return WINDOW's first child if WINDOW is a vertical combination.
398 WINDOW can be any window and defaults to the selected one.
399 Optional argument HORIZONTAL non-nil means return WINDOW's first
400 child if WINDOW is a horizontal combination."
401 (setq window
(window-normalize-window window
))
403 (window-left-child window
)
404 (window-top-child window
)))
406 (defun window-combinations (window &optional horizontal
)
407 "Return largest number of windows vertically arranged within WINDOW.
408 WINDOW must be a valid window and defaults to the selected one.
409 If HORIZONTAL is non-nil, return the largest number of
410 windows horizontally arranged within WINDOW."
411 (setq window
(window-normalize-window window
))
413 ((window-live-p window
)
414 ;; If WINDOW is live, return 1.
417 (window-left-child window
)
418 (window-top-child window
))
419 ;; If WINDOW is iso-combined, return the sum of the values for all
420 ;; child windows of WINDOW.
421 (let ((child (window-child window
))
425 (+ (window-combinations child horizontal
)
427 (setq child
(window-right child
)))
430 ;; If WINDOW is not iso-combined, return the maximum value of any
431 ;; child window of WINDOW.
432 (let ((child (window-child window
))
436 (max (window-combinations child horizontal
)
438 (setq child
(window-right child
)))
441 (defun walk-window-tree-1 (fun walk-window-tree-window any
&optional sub-only
)
442 "Helper function for `walk-window-tree' and `walk-window-subtree'."
443 (let (walk-window-tree-buffer)
444 (while walk-window-tree-window
445 (setq walk-window-tree-buffer
446 (window-buffer walk-window-tree-window
))
447 (when (or walk-window-tree-buffer any
)
448 (funcall fun walk-window-tree-window
))
449 (unless walk-window-tree-buffer
451 fun
(window-left-child walk-window-tree-window
) any
)
453 fun
(window-top-child walk-window-tree-window
) any
))
455 (setq walk-window-tree-window nil
)
456 (setq walk-window-tree-window
457 (window-right walk-window-tree-window
))))))
459 (defun walk-window-tree (fun &optional frame any minibuf
)
460 "Run function FUN on each live window of FRAME.
461 FUN must be a function with one argument - a window. FRAME must
462 be a live frame and defaults to the selected one. ANY, if
463 non-nil, means to run FUN on all live and internal windows of
466 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
467 window even if it isn't active. MINIBUF nil or omitted means run
468 FUN on FRAME's minibuffer window only if it's active. In both
469 cases the minibuffer window must be part of FRAME. MINIBUF
470 neither nil nor t means never run FUN on the minibuffer window.
472 This function performs a pre-order, depth-first traversal of the
473 window tree. If FUN changes the window tree, the result is
475 (setq frame
(window-normalize-frame frame
))
476 (walk-window-tree-1 fun
(frame-root-window frame
) any
)
477 (when (memq minibuf
'(nil t
))
478 ;; Run FUN on FRAME's minibuffer window if requested.
479 (let ((minibuffer-window (minibuffer-window frame
)))
480 (when (and (window-live-p minibuffer-window
)
481 (eq (window-frame minibuffer-window
) frame
)
483 (minibuffer-window-active-p minibuffer-window
)))
484 (funcall fun minibuffer-window
)))))
486 (defun walk-window-subtree (fun &optional window any
)
487 "Run function FUN on the subtree of windows rooted at WINDOW.
488 WINDOW defaults to the selected window. FUN must be a function
489 with one argument - a window. By default, run FUN only on live
490 windows of the subtree. If the optional argument ANY is non-nil,
491 run FUN on all live and internal windows of the subtree. If
492 WINDOW is live, run FUN on WINDOW only.
494 This function performs a pre-order, depth-first traversal of the
495 subtree rooted at WINDOW. If FUN changes that tree, the result
497 (setq window
(window-normalize-window window
))
498 (walk-window-tree-1 fun window any t
))
500 (defun window-with-parameter (parameter &optional value frame any minibuf
)
501 "Return first window on FRAME with PARAMETER non-nil.
502 FRAME defaults to the selected frame. Optional argument VALUE
503 non-nil means only return a window whose window-parameter value
504 for PARAMETER equals VALUE (comparison is done with `equal').
505 Optional argument ANY non-nil means consider internal windows
508 Optional argument MINIBUF t means consider FRAME's minibuffer
509 window even if it isn't active. MINIBUF nil or omitted means
510 consider FRAME's minibuffer window only if it's active. In both
511 cases the minibuffer window must be part of FRAME. MINIBUF
512 neither nil nor t means never consider the minibuffer window."
517 (when (and (setq this-value
(window-parameter window parameter
))
518 (or (not value
) (equal value this-value
)))
519 (throw 'found window
)))
520 frame any minibuf
))))
523 (defun window-atom-root (&optional window
)
524 "Return root of atomic window WINDOW is a part of.
525 WINDOW must be a valid window and defaults to the selected one.
526 Return nil if WINDOW is not part of an atomic window."
527 (setq window
(window-normalize-window window
))
529 (while (and window
(window-parameter window
'window-atom
))
531 (setq window
(window-parent window
)))
534 (defun window-make-atom (window)
535 "Make WINDOW an atomic window.
536 WINDOW must be an internal window. Return WINDOW."
537 (if (not (window-child window
))
538 (error "Window %s is not an internal window" window
)
541 (unless (window-parameter window
'window-atom
)
542 (set-window-parameter window
'window-atom t
)))
546 (defun display-buffer-in-atom-window (buffer alist
)
547 "Display BUFFER in an atomic window.
548 This function displays BUFFER in a new window that will be
549 combined with an existing window to form an atomic window. If
550 the existing window is already part of an atomic window, add the
551 new window to that atomic window. Operations like `split-window'
552 or `delete-window', when applied to a constituent of an atomic
553 window, are applied atomically to the root of that atomic window.
555 ALIST is an association list of symbols and values. The
556 following symbols can be used.
558 `window' specifies the existing window the new window shall be
559 combined with. Use `window-atom-root' to make the new window a
560 sibling of an atomic window's root. If an internal window is
561 specified here, all children of that window become part of the
562 atomic window too. If no window is specified, the new window
563 becomes a sibling of the selected window. By default, the
564 `window-atom' parameter of the existing window is set to `main'
565 provided it is live and was not set before.
567 `side' denotes the side of the existing window where the new
568 window shall be located. Valid values are `below', `right',
569 `above' and `left'. The default is `below'. By default, the
570 `window-atom' parameter of the new window is set to this value.
572 The return value is the new window, nil when creating that window
574 (let* ((ignore-window-parameters t
)
575 (window-combination-limit t
)
576 (window-combination-resize 'atom
)
577 (window (cdr (assq 'window alist
)))
578 (side (cdr (assq 'side alist
)))
579 (atom (when window
(window-parameter window
'window-atom
)))
581 (setq window
(window-normalize-window window
))
582 (setq root
(window-atom-root window
))
583 ;; Split off new window.
584 (when (setq new
(split-window window nil side
))
586 (if (and root
(not (eq root window
)))
587 ;; When WINDOW was part of an atomic window and we did not
588 ;; split its root, root atomic window at old root.
590 ;; Otherwise, root atomic window at WINDOW's new parent.
591 (window-parent window
)))
592 ;; Assign `window-atom' parameters, if needed.
593 (when (and (not atom
) (window-live-p window
))
594 (set-window-parameter window
'window-atom
'main
))
595 (set-window-parameter new
'window-atom side
)
596 ;; Display BUFFER in NEW and return NEW.
597 (window--display-buffer
598 buffer new
'window alist display-buffer-mark-dedicated
))))
600 (defun window--atom-check-1 (window)
601 "Subroutine of `window--atom-check'."
603 (if (window-parameter window
'window-atom
)
605 (when (or (catch 'reset
608 (if (window-parameter window
'window-atom
)
609 (setq count
(1+ count
))
612 ;; count >= 1 must hold here. If there's no other
613 ;; window around dissolve this atomic window.
615 ;; Dissolve atomic window.
618 (set-window-parameter window
'window-atom nil
))
621 (unless (window-buffer window
)
622 (window--atom-check-1 (window-left-child window
))
623 (window--atom-check-1 (window-top-child window
))))
624 ;; Check right sibling
625 (window--atom-check-1 (window-right window
))))
627 (defun window--atom-check (&optional frame
)
628 "Check atomicity of all windows on FRAME.
629 FRAME defaults to the selected frame. If an atomic window is
630 wrongly configured, reset the atomicity of all its windows on
631 FRAME to nil. An atomic window is wrongly configured if it has
632 no child windows or one of its child windows is not atomic."
633 (window--atom-check-1 (frame-root-window frame
)))
636 (defvar window-sides
'(left top right bottom
)
639 (defcustom window-sides-vertical nil
640 "If non-nil, left and right side windows are full height.
641 Otherwise, top and bottom side windows are full width."
646 (defcustom window-sides-slots
'(nil nil nil nil
)
647 "Maximum number of side window slots.
648 The value is a list of four elements specifying the number of
649 side window slots on (in this order) the left, top, right and
650 bottom side of each frame. If an element is a number, this means
651 to display at most that many side windows on the corresponding
652 side. If an element is nil, this means there's no bound on the
653 number of slots on that side."
658 :value
(nil nil nil nil
)
661 :help-echo
"Maximum slots of left side window."
663 :format
"%[Left%] %v\n"
664 (const :tag
"Unlimited" :format
"%t" nil
)
665 (integer :tag
"Number" :value
2 :size
5))
668 :help-echo
"Maximum slots of top side window."
670 :format
"%[Top%] %v\n"
671 (const :tag
"Unlimited" :format
"%t" nil
)
672 (integer :tag
"Number" :value
3 :size
5))
675 :help-echo
"Maximum slots of right side window."
677 :format
"%[Right%] %v\n"
678 (const :tag
"Unlimited" :format
"%t" nil
)
679 (integer :tag
"Number" :value
2 :size
5))
682 :help-echo
"Maximum slots of bottom side window."
684 :format
"%[Bottom%] %v\n"
685 (const :tag
"Unlimited" :format
"%t" nil
)
686 (integer :tag
"Number" :value
3 :size
5)))
689 (defun window--major-non-side-window (&optional frame
)
690 "Return the major non-side window of frame FRAME.
691 The optional argument FRAME must be a live frame and defaults to
694 If FRAME has at least one side window, the major non-side window
695 is either an internal non-side window such that all other
696 non-side windows on FRAME descend from it, or the single live
697 non-side window of FRAME. If FRAME has no side windows, return
699 (let ((frame (window-normalize-frame frame
))
701 ;; Set major to the _last_ window found by `walk-window-tree' that
702 ;; is not a side window but has a side window as its sibling.
705 (and (not (window-parameter window
'window-side
))
706 (or (and (setq sibling
(window-prev-sibling window
))
707 (window-parameter sibling
'window-side
))
708 (and (setq sibling
(window-next-sibling window
))
709 (window-parameter sibling
'window-side
)))
710 (setq major window
)))
712 (or major
(frame-root-window frame
))))
714 (defun window--major-side-window (side)
715 "Return major side window on SIDE.
716 SIDE must be one of the symbols `left', `top', `right' or
717 `bottom'. Return nil if no such window exists."
718 (let ((root (frame-root-window))
720 ;; (1) If a window on the opposite side exists, return that window's
722 ;; (2) If the new window shall span the entire side, return the
723 ;; frame's root window.
724 ;; (3) If a window on an orthogonal side exists, return that
726 ;; (4) Otherwise return the frame's root window.
728 ((or (and (eq side
'left
)
729 (setq window
(window-with-parameter 'window-side
'right nil t
)))
731 (setq window
(window-with-parameter 'window-side
'bottom nil t
))))
732 (window-prev-sibling window
))
733 ((or (and (eq side
'right
)
734 (setq window
(window-with-parameter 'window-side
'left nil t
)))
735 (and (eq side
'bottom
)
736 (setq window
(window-with-parameter 'window-side
'top nil t
))))
737 (window-next-sibling window
))
738 ((memq side
'(left right
))
740 (window-sides-vertical
742 ((setq window
(window-with-parameter 'window-side
'top nil t
))
743 (window-next-sibling window
))
744 ((setq window
(window-with-parameter 'window-side
'bottom nil t
))
745 (window-prev-sibling window
))
747 ((memq side
'(top bottom
))
749 ((not window-sides-vertical
)
751 ((setq window
(window-with-parameter 'window-side
'left nil t
))
752 (window-next-sibling window
))
753 ((setq window
(window-with-parameter 'window-side
'right nil t
))
754 (window-prev-sibling window
))
757 (defun display-buffer-in-major-side-window (buffer side slot
&optional alist
)
758 "Display BUFFER in a new window on SIDE of the selected frame.
759 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
760 specifies the slot to use. ALIST is an association list of
761 symbols and values as passed to `display-buffer-in-side-window'.
762 This function may be called only if no window on SIDE exists yet.
763 The new window automatically becomes the \"major\" side window on
764 SIDE. Return the new window, nil if its creation window failed."
765 (let* ((left-or-right (memq side
'(left right
)))
766 (major (window--major-side-window side
))
768 ((eq side
'top
) 'above
)
769 ((eq side
'bottom
) 'below
)
771 ;; The following two bindings will tell `split-window' to take
772 ;; the space for the new window from `major' and not make a new
773 ;; parent window unless needed.
774 (window-combination-resize 'side
)
775 (window-combination-limit nil
)
776 (new (split-window major nil on-side
)))
778 ;; Initialize `window-side' parameter of new window to SIDE.
779 (set-window-parameter new
'window-side side
)
780 ;; Install `window-slot' parameter of new window.
781 (set-window-parameter new
'window-slot slot
)
782 ;; Install `delete-window' parameter thus making sure that when
783 ;; the new window is deleted, a side window on the opposite side
784 ;; does not get resized.
785 (set-window-parameter new
'delete-window
'delete-side-window
)
786 ;; Auto-adjust height/width of new window unless a size has been
787 ;; explicitly requested.
788 (unless (if left-or-right
789 (cdr (assq 'window-width alist
))
790 (cdr (assq 'window-height alist
)))
794 (if left-or-right
'window-width
'window-height
)
795 (/ (window-total-size (frame-root-window) left-or-right
)
796 ;; By default use a fourth of the size of the frame's
800 ;; Install BUFFER in new window and return NEW.
801 (window--display-buffer buffer new
'window alist
'side
))))
803 (defun delete-side-window (window)
804 "Delete side window WINDOW."
805 (let ((window-combination-resize
806 (window-parameter (window-parent window
) 'window-side
))
807 (ignore-window-parameters t
))
808 (delete-window window
)))
810 (defun display-buffer-in-side-window (buffer alist
)
811 "Display BUFFER in a side window of the selected frame.
812 ALIST is an association list of symbols and values. The
813 following special symbols can be used in ALIST.
815 `side' denotes the side of the frame where the new window shall
816 be located. Valid values are `bottom', `right', `top' and
817 `left'. The default is `bottom'.
819 `slot' if non-nil, specifies the window slot where to display
820 BUFFER. A value of zero or nil means use the middle slot on
821 the specified side. A negative value means use a slot
822 preceding (that is, above or on the left of) the middle slot.
823 A positive value means use a slot following (that is, below or
824 on the right of) the middle slot. The default is zero."
825 (let ((side (or (cdr (assq 'side alist
)) 'bottom
))
826 (slot (or (cdr (assq 'slot alist
)) 0)))
828 ((not (memq side
'(top bottom left right
)))
829 (error "Invalid side %s specified" side
))
830 ((not (numberp slot
))
831 (error "Invalid slot %s specified" slot
)))
833 (let* ((major (window-with-parameter 'window-side side nil t
))
834 ;; `major' is the major window on SIDE, `windows' the list of
835 ;; life windows on SIDE.
841 (when (eq (window-parameter window
'window-side
) side
)
842 (setq windows
(cons window windows
))))
844 (nreverse windows
))))
845 (slots (when major
(max 1 (window-child-count major
))))
851 ((eq side
'bottom
) 3))
853 window this-window this-slot prev-window next-window
854 best-window best-slot abs-slot
)
857 ((and (numberp max-slots
) (<= max-slots
0))
858 ;; No side-slots available on this side. Don't create an error,
862 ;; No major window exists on this side, make one.
863 (display-buffer-in-major-side-window buffer side slot alist
))
865 ;; Scan windows on SIDE.
867 (dolist (window windows
)
868 (setq this-slot
(window-parameter window
'window-slot
))
870 ;; The following should not happen and probably be checked
871 ;; by window--side-check.
872 ((not (numberp this-slot
)))
874 ;; A window with a matching slot has been found.
875 (setq this-window window
)
878 ;; Check if this window has a better slot value wrt the
879 ;; slot of the window we want.
881 (if (or (and (> this-slot
0) (> slot
0))
882 (and (< this-slot
0) (< slot
0)))
883 (abs (- slot this-slot
))
884 (+ (abs slot
) (abs this-slot
))))
885 (unless (and best-slot
(<= best-slot abs-slot
))
886 (setq best-window window
)
887 (setq best-slot abs-slot
))
890 (setq prev-window window
))
892 (setq next-window window
)))))))
894 ;; `this-window' is the first window with the same SLOT.
895 ;; `prev-window' is the window with the largest slot < SLOT. A new
896 ;; window will be created after it.
897 ;; `next-window' is the window with the smallest slot > SLOT. A new
898 ;; window will be created before it.
899 ;; `best-window' is the window with the smallest absolute difference
900 ;; of its slot and SLOT.
902 ;; Note: We dedicate the window used softly to its buffer to
903 ;; avoid that "other" (non-side) buffer display functions steal
904 ;; it from us. This must eventually become customizable via
905 ;; ALIST (or, better, avoided in the "other" functions).
907 ;; Reuse `this-window'.
908 (window--display-buffer buffer this-window
'reuse alist
'side
))
909 (and (or (not max-slots
) (< slots max-slots
))
911 ;; Make new window before `next-window'.
913 (if (memq side
'(left right
)) 'above
'left
))
914 (window-combination-resize 'side
))
915 (setq window
(split-window next-window nil next-side
))
916 ;; When the new window is deleted, its space
917 ;; is returned to other side windows.
918 (set-window-parameter
919 window
'delete-window
'delete-side-window
)
922 ;; Make new window after `prev-window'.
924 (if (memq side
'(left right
)) 'below
'right
))
925 (window-combination-resize 'side
))
926 (setq window
(split-window prev-window nil prev-side
))
927 ;; When the new window is deleted, its space
928 ;; is returned to other side windows.
929 (set-window-parameter
930 window
'delete-window
'delete-side-window
)
932 (set-window-parameter window
'window-slot slot
)
933 (window--display-buffer buffer window
'window alist
'side
))
935 ;; Reuse `best-window'.
937 ;; Give best-window the new slot value.
938 (set-window-parameter best-window
'window-slot slot
)
939 (window--display-buffer
940 buffer best-window
'reuse alist
'side
)))))))))
942 (defun window--side-check (&optional frame
)
943 "Check the side window configuration of FRAME.
944 FRAME defaults to the selected frame.
946 A valid side window configuration preserves the following two
949 - If there exists a window whose window-side parameter is
950 non-nil, there must exist at least one live window whose
951 window-side parameter is nil.
953 - If a window W has a non-nil window-side parameter (i) it must
954 have a parent window and that parent's window-side parameter
955 must be either nil or the same as for W, and (ii) any child
956 window of W must have the same window-side parameter as W.
958 If the configuration is invalid, reset the window-side parameters
959 of all windows on FRAME to nil."
960 (let (left top right bottom none side parent parent-side
)
961 (when (or (catch 'reset
964 (setq side
(window-parameter window
'window-side
))
965 (setq parent
(window-parent window
))
967 (and parent
(window-parameter parent
'window-side
)))
968 ;; The following `cond' seems a bit tedious, but I'd
969 ;; rather stick to using just the stack.
972 (when (not (eq parent-side side
))
973 ;; A parent whose window-side is non-nil must
974 ;; have a child with the same window-side.
977 (when (window-buffer window
)
978 ;; Record that we have at least one non-side,
981 ((if (memq side
'(left top
))
982 (window-prev-sibling window
)
983 (window-next-sibling window
))
984 ;; Left and top major side windows must not have a
985 ;; previous sibling, right and bottom major side
986 ;; windows must not have a next sibling.
988 ;; Now check that there's no more than one major
989 ;; window for any of left, top, right and bottom.
991 (if left
(throw 'reset t
) (setq left t
)))
993 (if top
(throw 'reset t
) (setq top t
)))
995 (if right
(throw 'reset t
) (setq right t
)))
997 (if bottom
(throw 'reset t
) (setq bottom t
)))
1001 ;; If there's a side window, there must be at least one
1003 (and (or left top right bottom
) (not none
)))
1006 (set-window-parameter window
'window-side nil
))
1009 (defun window--check (&optional frame
)
1010 "Check atomic and side windows on FRAME.
1011 FRAME defaults to the selected frame."
1012 (window--side-check frame
)
1013 (window--atom-check frame
))
1015 ;; Dumping frame/window contents.
1016 (defun window--dump-window (&optional window erase
)
1017 "Dump WINDOW to buffer *window-frame-dump*.
1018 WINDOW must be a valid window and defaults to the selected one.
1019 Optional argument ERASE non-nil means erase *window-frame-dump*
1020 before writing to it."
1021 (setq window
(window-normalize-window window
))
1022 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1023 (when erase
(erase-buffer))
1025 (format "%s parent: %s\n" window
(window-parent window
))
1026 (format "pixel left: %s top: %s size: %s x %s new: %s\n"
1027 (window-pixel-left window
) (window-pixel-top window
)
1028 (window-size window t t
) (window-size window nil t
)
1029 (window-new-pixel window
))
1030 (format "char left: %s top: %s size: %s x %s new: %s\n"
1031 (window-left-column window
) (window-top-line window
)
1032 (window-total-size window t
) (window-total-size window
)
1033 (window-new-total window
))
1034 (format "normal: %s x %s new: %s\n"
1035 (window-normal-size window t
) (window-normal-size window
)
1036 (window-new-normal window
)))
1037 (when (window-live-p window
)
1038 (let ((fringes (window-fringes window
))
1039 (margins (window-margins window
)))
1041 (format "body pixel: %s x %s char: %s x %s\n"
1042 (window-body-width window t
) (window-body-height window t
)
1043 (window-body-width window
) (window-body-height window
))
1044 (format "width left fringe: %s left margin: %s right margin: %s\n"
1045 (car fringes
) (or (car margins
) 0) (or (cdr margins
) 0))
1046 (format "width right fringe: %s scroll-bar: %s divider: %s\n"
1048 (window-scroll-bar-width window
)
1049 (window-right-divider-width window
))
1050 (format "height header-line: %s mode-line: %s divider: %s\n"
1051 (window-header-line-height window
)
1052 (window-mode-line-height window
)
1053 (window-bottom-divider-width window
)))))
1056 (defun window--dump-frame (&optional window-or-frame
)
1057 "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
1058 WINDOW-OR-FRAME can be a frame or a window and defaults to the
1059 selected frame. When WINDOW-OR-FRAME is a window, dump that
1060 window's frame. The buffer *window-frame-dump* is erased before
1065 ((or (not window-or-frame
)
1066 (frame-live-p window-or-frame
))
1067 (frame-root-window window-or-frame
))
1068 ((or (window-live-p window-or-frame
)
1069 (window-child window-or-frame
))
1072 (frame-root-window))))
1073 (frame (window-frame window
)))
1074 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1077 (format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
1078 (frame-pixel-width frame
) (frame-pixel-height frame
)
1079 (frame-total-cols frame
) (frame-text-lines frame
) ; (frame-total-lines frame)
1080 (frame-char-width frame
) (frame-char-height frame
))
1081 (format "frame text pixel: %s x %s cols/lines: %s x %s\n"
1082 (frame-text-width frame
) (frame-text-height frame
)
1083 (frame-text-cols frame
) (frame-text-lines frame
))
1084 (format "tool: %s scroll: %s fringe: %s border: %s right: %s bottom: %s\n\n"
1085 (tool-bar-height frame t
)
1086 (frame-scroll-bar-width frame
)
1087 (frame-fringe-width frame
)
1088 (frame-border-width frame
)
1089 (frame-right-divider-width frame
)
1090 (frame-bottom-divider-width frame
)))
1091 (walk-window-tree 'window--dump-window frame t t
))))
1094 (defun window-total-size (&optional window horizontal round
)
1095 "Return the total height or width of WINDOW.
1096 WINDOW must be a valid window and defaults to the selected one.
1098 If HORIZONTAL is omitted or nil, return the total height of
1099 WINDOW, in lines, like `window-total-height'. Otherwise return
1100 the total width, in columns, like `window-total-width'.
1102 Optional argument ROUND is handled as for `window-total-height'
1103 and `window-total-width'."
1105 (window-total-width window round
)
1106 (window-total-height window round
)))
1108 (defun window-size (&optional window horizontal pixelwise round
)
1109 "Return the height or width of WINDOW.
1110 WINDOW must be a valid window and defaults to the selected one.
1112 If HORIZONTAL is omitted or nil, return the total height of
1113 WINDOW, in lines, like `window-total-height'. Otherwise return
1114 the total width, in columns, like `window-total-width'.
1116 Optional argument PIXELWISE means return the pixel size of WINDOW
1117 like `window-pixel-height' and `window-pixel-width'.
1119 Optional argument ROUND is ignored if PIXELWISE is non-nil and
1120 handled as for `window-total-height' and `window-total-width'
1124 (window-pixel-width window
)
1125 (window-total-width window round
))
1127 (window-pixel-height window
)
1128 (window-total-height window round
))))
1130 (defvar window-size-fixed nil
1131 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
1132 If the value is `height', then only the window's height is fixed.
1133 If the value is `width', then only the window's width is fixed.
1134 Any other non-nil value fixes both the width and the height.
1136 Emacs won't change the size of any window displaying that buffer,
1137 unless it has no other choice (like when deleting a neighboring
1139 (make-variable-buffer-local 'window-size-fixed
)
1141 (defun window--size-ignore-p (window ignore
)
1142 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
1143 (if (window-valid-p ignore
) (eq window ignore
) ignore
))
1145 (defun window-safe-min-size (&optional window horizontal pixelwise
)
1146 "Return safe minimum size of WINDOW.
1147 WINDOW must be a valid window and defaults to the selected one.
1148 Optional argument HORIZONTAL non-nil means return the minimum
1149 number of columns of WINDOW; otherwise return the minimum number
1152 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1154 (setq window
(window-normalize-window window
))
1157 (* window-safe-min-width
1158 (frame-char-width (window-frame window
)))
1159 (* window-safe-min-height
1160 (frame-char-height (window-frame window
))))
1161 (if horizontal window-safe-min-width window-safe-min-height
)))
1163 (defun window-min-size (&optional window horizontal ignore pixelwise
)
1164 "Return the minimum size of WINDOW.
1165 WINDOW must be a valid window and defaults to the selected one.
1166 Optional argument HORIZONTAL non-nil means return the minimum
1167 number of columns of WINDOW; otherwise return the minimum number
1170 Optional argument IGNORE, if non-nil, means ignore restrictions
1171 imposed by fixed size windows, `window-min-height' or
1172 `window-min-width' settings. If IGNORE equals `safe', live
1173 windows may get as small as `window-safe-min-height' lines and
1174 `window-safe-min-width' columns. If IGNORE is a window, ignore
1175 restrictions for that window only. Any other non-nil value
1176 means ignore all of the above restrictions for all windows.
1178 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1181 (window-normalize-window window
) horizontal ignore pixelwise
))
1183 (defun window--min-size-1 (window horizontal ignore pixelwise
)
1184 "Internal function of `window-min-size'."
1185 (let ((sub (window-child window
)))
1188 ;; WINDOW is an internal window.
1189 (if (window-combined-p sub horizontal
)
1190 ;; The minimum size of an iso-combination is the sum of
1191 ;; the minimum sizes of its child windows.
1193 (setq value
(+ value
1195 sub horizontal ignore pixelwise
)))
1196 (setq sub
(window-right sub
)))
1197 ;; The minimum size of an ortho-combination is the maximum
1198 ;; of the minimum sizes of its child windows.
1200 (setq value
(max value
1202 sub horizontal ignore pixelwise
)))
1203 (setq sub
(window-right sub
))))
1205 (with-current-buffer (window-buffer window
)
1207 ((and (not (window--size-ignore-p window ignore
))
1208 (window-size-fixed-p window horizontal
))
1209 ;; The minimum size of a fixed size window is its size.
1210 (window-size window horizontal pixelwise
))
1211 ((or (eq ignore
'safe
) (eq ignore window
))
1212 ;; If IGNORE equals `safe' or WINDOW return the safe values.
1213 (window-safe-min-size window horizontal pixelwise
))
1215 ;; For the minimum width of a window take fringes and
1216 ;; scroll-bars into account. This is questionable and should
1217 ;; be removed as soon as we are able to split (and resize)
1218 ;; windows such that the new (or resized) windows can get a
1219 ;; size less than the user-specified `window-min-height' and
1220 ;; `window-min-width'.
1221 (let* ((char-size (frame-char-size window t
))
1222 (fringes (window-fringes window
))
1224 (+ (window-safe-min-size window t t
)
1225 (car fringes
) (cadr fringes
)
1226 (window-scroll-bar-width window
)
1227 (window-right-divider-width window
))))
1230 (if window-resize-pixelwise
1232 ;; Round up to next integral of columns.
1233 (* (ceiling pixel-width char-size
) char-size
))
1234 (if (window--size-ignore-p window ignore
)
1236 (window-min-pixel-width)))
1238 (ceiling pixel-width char-size
)
1239 (if (window--size-ignore-p window ignore
)
1241 window-min-width
)))))
1242 ((let ((char-size (frame-char-size window
))
1244 (+ (window-safe-min-size window nil t
)
1245 (window-header-line-height window
)
1246 (window-mode-line-height window
)
1247 (window-bottom-divider-width window
))))
1250 (if window-resize-pixelwise
1252 ;; Round up to next integral of lines.
1253 (* (ceiling pixel-height char-size
) char-size
))
1254 (if (window--size-ignore-p window ignore
)
1256 (window-min-pixel-height)))
1257 (max (ceiling pixel-height char-size
)
1258 (if (window--size-ignore-p window ignore
)
1260 window-min-height
))))))))))
1262 (defun window-sizable (window delta
&optional horizontal ignore pixelwise
)
1263 "Return DELTA if DELTA lines can be added to WINDOW.
1264 WINDOW must be a valid window and defaults to the selected one.
1265 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1266 columns can be added to WINDOW. A return value of zero means
1267 that no lines (or columns) can be added to WINDOW.
1269 This function looks only at WINDOW and, recursively, its child
1270 windows. The function `window-resizable' looks at other windows
1273 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1274 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1275 return the maximum value in the range 0..DELTA by which WINDOW
1278 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1279 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1280 return the minimum value in the range DELTA..0 by which WINDOW
1283 Optional argument IGNORE non-nil means ignore restrictions
1284 imposed by fixed size windows, `window-min-height' or
1285 `window-min-width' settings. If IGNORE equals `safe', live
1286 windows may get as small as `window-safe-min-height' lines and
1287 `window-safe-min-width' columns. If IGNORE is a window, ignore
1288 restrictions for that window only. Any other non-nil value means
1289 ignore all of the above restrictions for all windows.
1291 Optional argument PIXELWISE non-nil means interpret DELTA as
1293 (setq window
(window-normalize-window window
))
1296 (max (- (window-min-size window horizontal ignore pixelwise
)
1297 (window-size window horizontal pixelwise
))
1299 ((window--size-ignore-p window ignore
)
1302 (if (window-size-fixed-p window horizontal
)
1307 (defun window-sizable-p (window delta
&optional horizontal ignore pixelwise
)
1308 "Return t if WINDOW can be resized by DELTA lines.
1309 WINDOW must be a valid window and defaults to the selected one.
1310 For the meaning of the arguments of this function see the
1311 doc-string of `window-sizable'."
1312 (setq window
(window-normalize-window window
))
1314 (>= (window-sizable window delta horizontal ignore pixelwise
)
1316 (<= (window-sizable window delta horizontal ignore pixelwise
)
1319 (defun window--size-fixed-1 (window horizontal
)
1320 "Internal function for `window-size-fixed-p'."
1321 (let ((sub (window-child window
)))
1324 ;; WINDOW is an internal window.
1325 (if (window-combined-p sub horizontal
)
1326 ;; An iso-combination is fixed size if all its child
1327 ;; windows are fixed-size.
1330 (unless (window--size-fixed-1 sub horizontal
)
1331 ;; We found a non-fixed-size child window, so
1332 ;; WINDOW's size is not fixed.
1334 (setq sub
(window-right sub
)))
1335 ;; All child windows are fixed-size, so WINDOW's size is
1338 ;; An ortho-combination is fixed-size if at least one of its
1339 ;; child windows is fixed-size.
1341 (when (window--size-fixed-1 sub horizontal
)
1342 ;; We found a fixed-size child window, so WINDOW's size
1345 (setq sub
(window-right sub
))))
1346 ;; WINDOW is a live window.
1347 (with-current-buffer (window-buffer window
)
1349 (memq window-size-fixed
'(width t
))
1350 (memq window-size-fixed
'(height t
))))))))
1352 (defun window-size-fixed-p (&optional window horizontal
)
1353 "Return non-nil if WINDOW's height is fixed.
1354 WINDOW must be a valid window and defaults to the selected one.
1355 Optional argument HORIZONTAL non-nil means return non-nil if
1356 WINDOW's width is fixed.
1358 If this function returns nil, this does not necessarily mean that
1359 WINDOW can be resized in the desired direction. The function
1360 `window-resizable' can tell that."
1361 (window--size-fixed-1
1362 (window-normalize-window window
) horizontal
))
1364 (defun window--min-delta-1 (window delta
&optional horizontal ignore trail noup pixelwise
)
1365 "Internal function for `window-min-delta'."
1366 (if (not (window-parent window
))
1367 ;; If we can't go up, return zero.
1369 ;; Else try to find a non-fixed-size sibling of WINDOW.
1370 (let* ((parent (window-parent window
))
1371 (sub (window-child parent
)))
1373 (if (window-combined-p sub horizontal
)
1374 ;; In an iso-combination throw DELTA if we find at least one
1375 ;; child window and that window is either not fixed-size or
1376 ;; we can ignore fixed-sizeness.
1377 (let ((skip (eq trail
'after
)))
1381 (setq skip
(eq trail
'before
)))
1383 ((and (not (window--size-ignore-p window ignore
))
1384 (window-size-fixed-p sub horizontal
)))
1386 ;; We found a non-fixed-size child window.
1387 (throw 'done delta
)))
1388 (setq sub
(window-right sub
))))
1389 ;; In an ortho-combination set DELTA to the minimum value by
1390 ;; which other child windows can shrink.
1392 (unless (eq sub window
)
1395 (max (- (window-size sub horizontal pixelwise
'ceiling
)
1397 sub horizontal ignore pixelwise
))
1399 (setq sub
(window-right sub
))))
1402 (window--min-delta-1
1403 parent delta horizontal ignore trail nil pixelwise
))))))
1405 (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise
)
1406 "Return number of lines by which WINDOW can be shrunk.
1407 WINDOW must be a valid window and defaults to the selected one.
1408 Return zero if WINDOW cannot be shrunk.
1410 Optional argument HORIZONTAL non-nil means return number of
1411 columns by which WINDOW can be shrunk.
1413 Optional argument IGNORE non-nil means ignore restrictions
1414 imposed by fixed size windows, `window-min-height' or
1415 `window-min-width' settings. If IGNORE is a window, ignore
1416 restrictions for that window only. If IGNORE equals `safe',
1417 live windows may get as small as `window-safe-min-height' lines
1418 and `window-safe-min-width' columns. Any other non-nil value
1419 means ignore all of the above restrictions for all windows.
1421 Optional argument TRAIL restricts the windows that can be enlarged.
1422 If its value is `before', only windows to the left of or above WINDOW
1423 can be enlarged. If it is `after', only windows to the right of or
1424 below WINDOW can be enlarged.
1426 Optional argument NOUP non-nil means don't go up in the window
1427 tree, but try to enlarge windows within WINDOW's combination only.
1429 Optional argument NODOWN non-nil means don't check whether WINDOW
1430 itself (and its child windows) can be shrunk; check only whether
1431 at least one other window can be enlarged appropriately.
1433 Optional argument PIXELWISE non-nil means return number of pixels
1434 by which WINDOW can be shrunk."
1435 (setq window
(window-normalize-window window
))
1436 (let ((size (window-size window horizontal pixelwise
'floor
))
1437 (minimum (window-min-size window horizontal ignore pixelwise
)))
1440 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1441 (window--min-delta-1
1442 window size horizontal ignore trail noup pixelwise
))
1444 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1445 ;; there's nothing to recover.
1448 ;; Otherwise, try to recover whatever WINDOW is larger than its
1450 (window--min-delta-1
1451 window
(- size minimum
) horizontal ignore trail noup pixelwise
)))))
1453 (defun window--max-delta-1 (window delta
&optional horizontal ignore trail noup pixelwise
)
1454 "Internal function of `window-max-delta'."
1455 (if (not (window-parent window
))
1456 ;; Can't go up. Return DELTA.
1458 (let* ((parent (window-parent window
))
1459 (sub (window-child parent
)))
1461 (if (window-combined-p sub horizontal
)
1462 ;; For an iso-combination calculate how much we can get from
1463 ;; other child windows.
1464 (let ((skip (eq trail
'after
)))
1468 (setq skip
(eq trail
'before
)))
1474 (- (window-size sub horizontal pixelwise
'ceiling
)
1476 sub horizontal ignore pixelwise
))
1478 (setq sub
(window-right sub
))))
1479 ;; For an ortho-combination throw DELTA when at least one
1480 ;; child window is fixed-size.
1482 (when (and (not (eq sub window
))
1483 (not (window--size-ignore-p sub ignore
))
1484 (window-size-fixed-p sub horizontal
))
1485 (throw 'fixed delta
))
1486 (setq sub
(window-right sub
))))
1488 ;; When NOUP is nil, DELTA is all we can get.
1490 ;; Else try with parent of WINDOW, passing the DELTA we
1491 ;; recovered so far.
1492 (window--max-delta-1
1493 parent delta horizontal ignore trail nil pixelwise
))))))
1495 (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise
)
1496 "Return maximum number of lines by which WINDOW can be enlarged.
1497 WINDOW must be a valid window and defaults to the selected one.
1498 The return value is zero if WINDOW cannot be enlarged.
1500 Optional argument HORIZONTAL non-nil means return maximum number
1501 of columns by which WINDOW can be enlarged.
1503 Optional argument IGNORE non-nil means ignore restrictions
1504 imposed by fixed size windows, `window-min-height' or
1505 `window-min-width' settings. If IGNORE is a window, ignore
1506 restrictions for that window only. If IGNORE equals `safe',
1507 live windows may get as small as `window-safe-min-height' lines
1508 and `window-safe-min-width' columns. Any other non-nil value means
1509 ignore all of the above restrictions for all windows.
1511 Optional argument TRAIL restricts the windows that can be enlarged.
1512 If its value is `before', only windows to the left of or above WINDOW
1513 can be enlarged. If it is `after', only windows to the right of or
1514 below WINDOW can be enlarged.
1516 Optional argument NOUP non-nil means don't go up in the window
1517 tree but try to obtain the entire space from windows within
1518 WINDOW's combination.
1520 Optional argument NODOWN non-nil means do not check whether
1521 WINDOW itself (and its child windows) can be enlarged; check
1522 only whether other windows can be shrunk appropriately.
1524 Optional argument PIXELWISE non-nil means return number of
1525 pixels by which WINDOW can be enlarged."
1526 (setq window
(window-normalize-window window
))
1527 (if (and (not (window--size-ignore-p window ignore
))
1528 (not nodown
) (window-size-fixed-p window horizontal
))
1529 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1532 ;; WINDOW has no fixed size.
1533 (window--max-delta-1 window
0 horizontal ignore trail noup pixelwise
)))
1535 ;; Make NOUP also inhibit the min-size check.
1536 (defun window--resizable (window delta
&optional horizontal ignore trail noup nodown pixelwise
)
1537 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1538 WINDOW must be a valid window and defaults to the selected one.
1539 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1540 can be resized horizontally by DELTA columns. A return value of
1541 zero means that WINDOW is not resizable.
1543 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1544 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1545 return the maximum value in the range 0..DELTA by which WINDOW
1548 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1549 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1550 return the minimum value in the range DELTA..0 that can be used
1551 for shrinking WINDOW.
1553 Optional argument IGNORE non-nil means ignore restrictions
1554 imposed by fixed size windows, `window-min-height' or
1555 `window-min-width' settings. If IGNORE is a window, ignore
1556 restrictions for that window only. If IGNORE equals `safe',
1557 live windows may get as small as `window-safe-min-height' lines
1558 and `window-safe-min-width' columns. Any other non-nil value
1559 means ignore all of the above restrictions for all windows.
1561 Optional argument TRAIL `before' means only windows to the left
1562 of or below WINDOW can be shrunk. Optional argument TRAIL
1563 `after' means only windows to the right of or above WINDOW can be
1566 Optional argument NOUP non-nil means don't go up in the window
1567 tree but check only whether space can be obtained from (or given
1568 to) WINDOW's siblings.
1570 Optional argument NODOWN non-nil means don't go down in the
1571 window tree. This means do not check whether resizing would
1572 violate size restrictions of WINDOW or its child windows.
1574 Optional argument PIXELWISE non-nil means interpret DELTA as
1576 (setq window
(window-normalize-window window
))
1579 (max (- (window-min-delta
1580 window horizontal ignore trail noup nodown pixelwise
))
1583 (min (window-max-delta
1584 window horizontal ignore trail noup nodown pixelwise
)
1588 (defun window--resizable-p (window delta
&optional horizontal ignore trail noup nodown pixelwise
)
1589 "Return t if WINDOW can be resized vertically by DELTA lines.
1590 WINDOW must be a valid window and defaults to the selected one.
1591 For the meaning of the arguments of this function see the
1592 doc-string of `window--resizable'.
1594 Optional argument PIXELWISE non-nil means interpret DELTA as
1596 (setq window
(window-normalize-window window
))
1598 (>= (window--resizable
1599 window delta horizontal ignore trail noup nodown pixelwise
)
1601 (<= (window--resizable
1602 window delta horizontal ignore trail noup nodown pixelwise
)
1605 (defun window-resizable (window delta
&optional horizontal ignore pixelwise
)
1606 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1607 WINDOW must be a valid window and defaults to the selected one.
1608 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1609 can be resized horizontally by DELTA columns. A return value of
1610 zero means that WINDOW is not resizable.
1612 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1613 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1614 return the maximum value in the range 0..DELTA by which WINDOW
1617 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1618 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1619 return the minimum value in the range DELTA..0 that can be used
1620 for shrinking WINDOW.
1622 Optional argument IGNORE non-nil means ignore restrictions
1623 imposed by fixed size windows, `window-min-height' or
1624 `window-min-width' settings. If IGNORE is a window, ignore
1625 restrictions for that window only. If IGNORE equals `safe',
1626 live windows may get as small as `window-safe-min-height' lines
1627 and `window-safe-min-width' columns. Any other non-nil value
1628 means ignore all of the above restrictions for all windows.
1630 Optional argument PIXELWISE non-nil means interpret DELTA as
1632 (setq window
(window-normalize-window window
))
1633 (window--resizable window delta horizontal ignore nil nil nil pixelwise
))
1635 (defun window-resizable-p (window delta
&optional horizontal ignore pixelwise
)
1636 "Return t if WINDOW can be resized vertically by DELTA lines.
1637 WINDOW must be a valid window and defaults to the selected one.
1638 For the meaning of the arguments of this function see the
1639 doc-string of `window-resizable'."
1640 (setq window
(window-normalize-window window
))
1642 (>= (window--resizable
1643 window delta horizontal ignore nil nil nil pixelwise
)
1645 (<= (window--resizable
1646 window delta horizontal ignore nil nil nil pixelwise
)
1649 ;; Aliases of functions defined in window.c.
1650 (defalias 'window-height
'window-total-height
)
1651 (defalias 'window-width
'window-body-width
)
1653 ;; Eventually the following two should work pixelwise.
1655 ;; See discussion in bug#4543.
1656 (defun window-full-height-p (&optional window
)
1657 "Return t if WINDOW is as high as its containing frame.
1658 More precisely, return t if and only if the total height of
1659 WINDOW equals the total height of the root window of WINDOW's
1660 frame. WINDOW must be a valid window and defaults to the
1662 (setq window
(window-normalize-window window
))
1663 (= (window-pixel-height window
)
1664 (window-pixel-height (frame-root-window window
))))
1666 (defun window-full-width-p (&optional window
)
1667 "Return t if WINDOW is as wide as its containing frame.
1668 More precisely, return t if and only if the total width of WINDOW
1669 equals the total width of the root window of WINDOW's frame.
1670 WINDOW must be a valid window and defaults to the selected one."
1671 (setq window
(window-normalize-window window
))
1672 (= (window-pixel-width window
)
1673 (window-pixel-width (frame-root-window window
))))
1675 (defun window-body-size (&optional window horizontal
)
1676 "Return the height or width of WINDOW's text area.
1677 WINDOW must be a live window and defaults to the selected one.
1679 If HORIZONTAL is omitted or nil, return the height of the text
1680 area, like `window-body-height'. Otherwise, return the width of
1681 the text area, like `window-body-width'."
1683 (window-body-width window
)
1684 (window-body-height window
)))
1686 (defun window-current-scroll-bars (&optional window
)
1687 "Return the current scroll bar settings for WINDOW.
1688 WINDOW must be a live window and defaults to the selected one.
1690 The return value is a cons cell (VERTICAL . HORIZONTAL) where
1691 VERTICAL specifies the current location of the vertical scroll
1692 bars (`left', `right', or nil), and HORIZONTAL specifies the
1693 current location of the horizontal scroll bars (`top', `bottom',
1696 Unlike `window-scroll-bars', this function reports the scroll bar
1697 type actually used, once frame defaults and `scroll-bar-mode' are
1698 taken into account."
1699 (setq window
(window-normalize-window window t
))
1700 (let ((vert (nth 2 (window-scroll-bars window
)))
1702 (when (or (eq vert t
) (eq hor t
))
1703 (let ((fcsb (frame-current-scroll-bars (window-frame window
))))
1705 (setq vert
(car fcsb
)))
1707 (setq hor
(cdr fcsb
)))))
1710 (defun walk-windows (fun &optional minibuf all-frames
)
1711 "Cycle through all live windows, calling FUN for each one.
1712 FUN must specify a function with a window as its sole argument.
1713 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1714 windows to include in the walk.
1716 MINIBUF t means include the minibuffer window even if the
1717 minibuffer is not active. MINIBUF nil or omitted means include
1718 the minibuffer window only if the minibuffer is active. Any
1719 other value means do not include the minibuffer window even if
1720 the minibuffer is active.
1722 ALL-FRAMES nil or omitted means consider all windows on the
1723 selected frame, plus the minibuffer window if specified by the
1724 MINIBUF argument. If the minibuffer counts, consider all windows
1725 on all frames that share that minibuffer too. The following
1726 non-nil values of ALL-FRAMES have special meanings:
1728 - t means consider all windows on all existing frames.
1730 - `visible' means consider all windows on all visible frames on
1731 the current terminal.
1733 - 0 (the number zero) means consider all windows on all visible
1734 and iconified frames on the current terminal.
1736 - A frame means consider all windows on that frame only.
1738 Anything else means consider all windows on the selected frame
1741 This function changes neither the order of recently selected
1742 windows nor the buffer list."
1743 ;; If we start from the minibuffer window, don't fail to come
1745 (when (window-minibuffer-p)
1747 ;; Make sure to not mess up the order of recently selected
1748 ;; windows. Use `save-selected-window' and `select-window'
1749 ;; with second argument non-nil for this purpose.
1750 (save-selected-window
1751 (when (framep all-frames
)
1752 (select-window (frame-first-window all-frames
) 'norecord
))
1753 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames
))
1754 (funcall fun walk-windows-window
))))
1756 (defun window-at-side-p (&optional window side
)
1757 "Return t if WINDOW is at SIDE of its containing frame.
1758 WINDOW must be a valid window and defaults to the selected one.
1759 SIDE can be any of the symbols `left', `top', `right' or
1760 `bottom'. The default value nil is handled like `bottom'."
1761 (setq window
(window-normalize-window window
))
1766 ((eq side
'right
) 2)
1767 ((memq side
'(bottom nil
)) 3))))
1768 (= (nth edge
(window-pixel-edges window
))
1769 (nth edge
(window-pixel-edges (frame-root-window window
))))))
1771 (defun window-at-side-list (&optional frame side
)
1772 "Return list of all windows on SIDE of FRAME.
1773 FRAME must be a live frame and defaults to the selected frame.
1774 SIDE can be any of the symbols `left', `top', `right' or
1775 `bottom'. The default value nil is handled like `bottom'."
1776 (setq frame
(window-normalize-frame frame
))
1780 (when (window-at-side-p window side
)
1781 (setq windows
(cons window windows
))))
1783 (nreverse windows
)))
1785 (defun window--in-direction-2 (window posn
&optional horizontal
)
1786 "Support function for `window-in-direction'."
1788 (let ((top (window-pixel-top window
)))
1791 (- posn top
(window-pixel-height window
))))
1792 (let ((left (window-pixel-left window
)))
1795 (- posn left
(window-pixel-width window
))))))
1797 ;; Predecessors to the below have been devised by Julian Assange in
1798 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
1799 ;; Neither of these allow to selectively ignore specific windows
1800 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
1802 (defun window-in-direction (direction &optional window ignore sign wrap mini
)
1803 "Return window in DIRECTION as seen from WINDOW.
1804 More precisely, return the nearest window in direction DIRECTION
1805 as seen from the position of `window-point' in window WINDOW.
1806 DIRECTION must be one of `above', `below', `left' or `right'.
1807 WINDOW must be a live window and defaults to the selected one.
1809 Do not return a window whose `no-other-window' parameter is
1810 non-nil. If the nearest window's `no-other-window' parameter is
1811 non-nil, try to find another window in the indicated direction.
1812 If, however, the optional argument IGNORE is non-nil, return that
1813 window even if its `no-other-window' parameter is non-nil.
1815 Optional argument SIGN a negative number means to use the right
1816 or bottom edge of WINDOW as reference position instead of
1817 `window-point'. SIGN a positive number means to use the left or
1818 top edge of WINDOW as reference position.
1820 Optional argument WRAP non-nil means to wrap DIRECTION around
1821 frame borders. This means to return for a WINDOW a the top of
1822 the frame and DIRECTION `above' to return the minibuffer window
1823 if the frame has one, and a window at the bottom of the frame
1826 Optional argument MINI nil means to return the minibuffer window
1827 if and only if it is currently active. MINI non-nil means to
1828 return the minibuffer window even when it's not active. However,
1829 if WRAP non-nil, always act as if MINI were nil.
1831 Return nil if no suitable window can be found."
1832 (setq window
(window-normalize-window window t
))
1833 (unless (memq direction
'(above below left right
))
1834 (error "Wrong direction %s" direction
))
1835 (let* ((frame (window-frame window
))
1836 (hor (memq direction
'(left right
)))
1838 (window-pixel-left window
)
1839 (window-pixel-top window
)))
1840 (last (+ first
(window-size window hor t
)))
1841 ;; The column / row value of `posn-at-point' can be nil for the
1842 ;; mini-window, guard against that.
1845 ((and (numberp sign
) (< sign
0))
1847 (1- (+ (window-pixel-top window
) (window-pixel-height window
)))
1848 (1- (+ (window-pixel-left window
) (window-pixel-width window
)))))
1849 ((and (numberp sign
) (> sign
0))
1851 (window-pixel-top window
)
1852 (window-pixel-left window
)))
1853 ((let ((posn-cons (nth 2 (posn-at-point (window-point window
) window
))))
1855 (+ (or (cdr posn-cons
) 1) (window-pixel-top window
))
1856 (+ (or (car posn-cons
) 1) (window-pixel-left window
)))))))
1859 ((eq direction
'below
) (frame-pixel-height frame
))
1860 ((eq direction
'right
) (frame-pixel-width frame
))
1862 (best-edge-2 best-edge
)
1863 (best-diff-2 (if hor
(frame-pixel-height frame
) (frame-pixel-width frame
)))
1864 best best-2 best-diff-2-new
)
1867 (let* ((w-top (window-pixel-top w
))
1868 (w-left (window-pixel-left w
)))
1871 ;; Ignore ourselves.
1872 (and (window-parameter w
'no-other-window
)
1873 ;; Ignore W unless IGNORE is non-nil.
1877 ((and (<= w-top posn
)
1878 (< posn
(+ w-top
(window-pixel-height w
))))
1879 ;; W is to the left or right of WINDOW and covers POSN.
1880 (when (or (and (eq direction
'left
)
1881 (or (and (<= w-left first
) (> w-left best-edge
))
1883 (window-at-side-p window
'left
)
1884 (window-at-side-p w
'right
))))
1885 (and (eq direction
'right
)
1886 (or (and (>= w-left last
) (< w-left best-edge
))
1888 (window-at-side-p window
'right
)
1889 (window-at-side-p w
'left
)))))
1890 (setq best-edge w-left
)
1892 ((and (or (and (eq direction
'left
)
1893 (<= (+ w-left
(window-pixel-width w
)) first
))
1894 (and (eq direction
'right
) (<= last w-left
)))
1895 ;; W is to the left or right of WINDOW but does not
1897 (setq best-diff-2-new
1898 (window--in-direction-2 w posn hor
))
1899 (or (< best-diff-2-new best-diff-2
)
1900 (and (= best-diff-2-new best-diff-2
)
1901 (if (eq direction
'left
)
1902 (> w-left best-edge-2
)
1903 (< w-left best-edge-2
)))))
1904 (setq best-edge-2 w-left
)
1905 (setq best-diff-2 best-diff-2-new
)
1907 ((and (<= w-left posn
)
1908 (< posn
(+ w-left
(window-pixel-width w
))))
1909 ;; W is above or below WINDOW and covers POSN.
1910 (when (or (and (eq direction
'above
)
1911 (or (and (<= w-top first
) (> w-top best-edge
))
1913 (window-at-side-p window
'top
)
1914 (if (active-minibuffer-window)
1915 (minibuffer-window-active-p w
)
1916 (window-at-side-p w
'bottom
)))))
1917 (and (eq direction
'below
)
1918 (or (and (>= w-top first
) (< w-top best-edge
))
1920 (if (active-minibuffer-window)
1921 (minibuffer-window-active-p window
)
1922 (window-at-side-p window
'bottom
))
1923 (window-at-side-p w
'top
)))))
1924 (setq best-edge w-top
)
1926 ((and (or (and (eq direction
'above
)
1927 (<= (+ w-top
(window-pixel-height w
)) first
))
1928 (and (eq direction
'below
) (<= last w-top
)))
1929 ;; W is above or below WINDOW but does not cover POSN.
1930 (setq best-diff-2-new
1931 (window--in-direction-2 w posn hor
))
1932 (or (< best-diff-2-new best-diff-2
)
1933 (and (= best-diff-2-new best-diff-2
)
1934 (if (eq direction
'above
)
1935 (> w-top best-edge-2
)
1936 (< w-top best-edge-2
)))))
1937 (setq best-edge-2 w-top
)
1938 (setq best-diff-2 best-diff-2-new
)
1940 frame nil
(and mini t
))
1943 (defun get-window-with-predicate (predicate &optional minibuf all-frames default
)
1944 "Return a live window satisfying PREDICATE.
1945 More precisely, cycle through all windows calling the function
1946 PREDICATE on each one of them with the window as its sole
1947 argument. Return the first window for which PREDICATE returns
1948 non-nil. Windows are scanned starting with the window following
1949 the selected window. If no window satisfies PREDICATE, return
1952 MINIBUF t means include the minibuffer window even if the
1953 minibuffer is not active. MINIBUF nil or omitted means include
1954 the minibuffer window only if the minibuffer is active. Any
1955 other value means do not include the minibuffer window even if
1956 the minibuffer is active.
1958 ALL-FRAMES nil or omitted means consider all windows on the selected
1959 frame, plus the minibuffer window if specified by the MINIBUF
1960 argument. If the minibuffer counts, consider all windows on all
1961 frames that share that minibuffer too. The following non-nil
1962 values of ALL-FRAMES have special meanings:
1964 - t means consider all windows on all existing frames.
1966 - `visible' means consider all windows on all visible frames on
1967 the current terminal.
1969 - 0 (the number zero) means consider all windows on all visible
1970 and iconified frames on the current terminal.
1972 - A frame means consider all windows on that frame only.
1974 Anything else means consider all windows on the selected frame
1977 (dolist (window (window-list-1
1978 (next-window nil minibuf all-frames
)
1979 minibuf all-frames
))
1980 (when (funcall predicate window
)
1981 (throw 'found window
)))
1984 (defalias 'some-window
'get-window-with-predicate
)
1986 (defun get-lru-window (&optional all-frames dedicated not-selected
)
1987 "Return the least recently used window on frames specified by ALL-FRAMES.
1988 Return a full-width window if possible. A minibuffer window is
1989 never a candidate. A dedicated window is never a candidate
1990 unless DEDICATED is non-nil, so if all windows are dedicated, the
1991 value is nil. Avoid returning the selected window if possible.
1992 Optional argument NOT-SELECTED non-nil means never return the
1995 The following non-nil values of the optional argument ALL-FRAMES
1996 have special meanings:
1998 - t means consider all windows on all existing frames.
2000 - `visible' means consider all windows on all visible frames on
2001 the current terminal.
2003 - 0 (the number zero) means consider all windows on all visible
2004 and iconified frames on the current terminal.
2006 - A frame means consider all windows on that frame only.
2008 Any other value of ALL-FRAMES means consider all windows on the
2009 selected frame and no others."
2010 (let (best-window best-time second-best-window second-best-time time
)
2011 (dolist (window (window-list-1 nil
'nomini all-frames
))
2012 (when (and (or dedicated
(not (window-dedicated-p window
)))
2013 (or (not not-selected
) (not (eq window
(selected-window)))))
2014 (setq time
(window-use-time window
))
2015 (if (or (eq window
(selected-window))
2016 (not (window-full-width-p window
)))
2017 (when (or (not second-best-time
) (< time second-best-time
))
2018 (setq second-best-time time
)
2019 (setq second-best-window window
))
2020 (when (or (not best-time
) (< time best-time
))
2021 (setq best-time time
)
2022 (setq best-window window
)))))
2023 (or best-window second-best-window
)))
2025 (defun get-mru-window (&optional all-frames dedicated not-selected
)
2026 "Return the most recently used window on frames specified by ALL-FRAMES.
2027 A minibuffer window is never a candidate. A dedicated window is
2028 never a candidate unless DEDICATED is non-nil, so if all windows
2029 are dedicated, the value is nil. Optional argument NOT-SELECTED
2030 non-nil means never return the selected window.
2032 The following non-nil values of the optional argument ALL-FRAMES
2033 have special meanings:
2035 - t means consider all windows on all existing frames.
2037 - `visible' means consider all windows on all visible frames on
2038 the current terminal.
2040 - 0 (the number zero) means consider all windows on all visible
2041 and iconified frames on the current terminal.
2043 - A frame means consider all windows on that frame only.
2045 Any other value of ALL-FRAMES means consider all windows on the
2046 selected frame and no others."
2047 (let (best-window best-time time
)
2048 (dolist (window (window-list-1 nil
'nomini all-frames
))
2049 (setq time
(window-use-time window
))
2050 (when (and (or dedicated
(not (window-dedicated-p window
)))
2051 (or (not not-selected
) (not (eq window
(selected-window))))
2052 (or (not best-time
) (> time best-time
)))
2053 (setq best-time time
)
2054 (setq best-window window
)))
2057 (defun get-largest-window (&optional all-frames dedicated not-selected
)
2058 "Return the largest window on frames specified by ALL-FRAMES.
2059 A minibuffer window is never a candidate. A dedicated window is
2060 never a candidate unless DEDICATED is non-nil, so if all windows
2061 are dedicated, the value is nil. Optional argument NOT-SELECTED
2062 non-nil means never return the selected window.
2064 The following non-nil values of the optional argument ALL-FRAMES
2065 have special meanings:
2067 - t means consider all windows on all existing frames.
2069 - `visible' means consider all windows on all visible frames on
2070 the current terminal.
2072 - 0 (the number zero) means consider all windows on all visible
2073 and iconified frames on the current terminal.
2075 - A frame means consider all windows on that frame only.
2077 Any other value of ALL-FRAMES means consider all windows on the
2078 selected frame and no others."
2081 (dolist (window (window-list-1 nil
'nomini all-frames
))
2082 (when (and (or dedicated
(not (window-dedicated-p window
)))
2083 (or (not not-selected
) (not (eq window
(selected-window)))))
2084 (setq size
(* (window-pixel-height window
)
2085 (window-pixel-width window
)))
2086 (when (> size best-size
)
2087 (setq best-size size
)
2088 (setq best-window window
))))
2091 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames
)
2092 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2093 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2094 and defaults to the current buffer. Windows are scanned starting
2095 with the selected window.
2097 MINIBUF t means include the minibuffer window even if the
2098 minibuffer is not active. MINIBUF nil or omitted means include
2099 the minibuffer window only if the minibuffer is active. Any
2100 other value means do not include the minibuffer window even if
2101 the minibuffer is active.
2103 ALL-FRAMES nil or omitted means consider all windows on the
2104 selected frame, plus the minibuffer window if specified by the
2105 MINIBUF argument. If the minibuffer counts, consider all windows
2106 on all frames that share that minibuffer too. The following
2107 non-nil values of ALL-FRAMES have special meanings:
2109 - t means consider all windows on all existing frames.
2111 - `visible' means consider all windows on all visible frames on
2112 the current terminal.
2114 - 0 (the number zero) means consider all windows on all visible
2115 and iconified frames on the current terminal.
2117 - A frame means consider all windows on that frame only.
2119 Anything else means consider all windows on the selected frame
2121 (let ((buffer (window-normalize-buffer buffer-or-name
))
2123 (dolist (window (window-list-1 (selected-window) minibuf all-frames
))
2124 (when (eq (window-buffer window
) buffer
)
2125 (setq windows
(cons window windows
))))
2126 (nreverse windows
)))
2128 (defun minibuffer-window-active-p (window)
2129 "Return t if WINDOW is the currently active minibuffer window."
2130 (eq window
(active-minibuffer-window)))
2132 (defun count-windows (&optional minibuf
)
2133 "Return the number of live windows on the selected frame.
2134 The optional argument MINIBUF specifies whether the minibuffer
2135 window shall be counted. See `walk-windows' for the precise
2136 meaning of this argument."
2137 (length (window-list-1 nil minibuf
)))
2139 ;;; Resizing windows.
2140 (defun window--size-to-pixel (window size
&optional horizontal pixelwise round-maybe
)
2141 "For WINDOW convert SIZE lines to pixels.
2142 SIZE is supposed to specify a height of WINDOW in terms of text
2143 lines. The return value is the number of pixels specifying that
2146 WINDOW must be a valid window. Optional argument HORIZONTAL
2147 non-nil means convert SIZE columns to pixels.
2149 Optional argument PIXELWISE non-nil means SIZE already specifies
2150 pixels but may have to be adjusted to a multiple of the character
2151 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2152 means round to the nearest multiple of the character size of
2153 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2154 (setq window
(window-normalize-window window
))
2155 (let ((char-size (frame-char-size window horizontal
)))
2157 (if (and round-maybe
(not window-resize-pixelwise
))
2158 (* (round size char-size
) char-size
)
2160 (* size char-size
))))
2162 (defun window--pixel-to-total-1 (window horizontal char-size
)
2163 "Subroutine of `window--pixel-to-total'."
2164 (let ((child (window-child window
)))
2165 (if (window-combination-p window horizontal
)
2166 ;; In an iso-combination distribute sizes proportionally.
2167 (let ((remainder (window-new-total window
))
2168 size best-child rem best-rem
)
2169 ;; Initialize total sizes to each child's floor.
2171 (setq size
(max (/ (window-size child horizontal t
) char-size
) 1))
2172 (set-window-new-total child size
)
2173 (setq remainder
(- remainder size
))
2174 (setq child
(window-next-sibling child
)))
2175 ;; Distribute remainder.
2176 (while (> remainder
0)
2177 (setq child
(window-last-child window
))
2178 (setq best-child nil
)
2181 (when (and (<= (window-new-total child
)
2182 (/ (window-size child horizontal t
) char-size
))
2183 (> (setq rem
(%
(window-size child horizontal t
)
2186 (setq best-child child
)
2187 (setq best-rem rem
))
2188 (setq child
(window-prev-sibling child
)))
2189 ;; We MUST have a best-child here.
2190 (set-window-new-total best-child
1 t
)
2191 (setq remainder
(1- remainder
)))
2193 (setq child
(window-child window
))
2195 (window--pixel-to-total-1 child horizontal char-size
)
2196 (setq child
(window-next-sibling child
))))
2197 ;; In an ortho-combination assign new sizes directly.
2198 (let ((size (window-new-total window
)))
2200 (set-window-new-total child size
)
2201 (window--pixel-to-total-1 child horizontal char-size
)
2202 (setq child
(window-next-sibling child
)))))))
2204 (defun window--pixel-to-total (&optional frame horizontal
)
2205 "On FRAME assign new total window heights from pixel heights.
2206 FRAME must be a live frame and defaults to the selected frame.
2208 Optional argument HORIZONTAL non-nil means assign new total
2209 window widths from pixel widths."
2210 (setq frame
(window-normalize-frame frame
))
2211 (let* ((char-size (frame-char-size frame horizontal
))
2212 (root (frame-root-window))
2213 (root-size (window-size root horizontal t
))
2214 ;; We have to care about the minibuffer window only if it
2215 ;; appears together with the root window on this frame.
2216 (mini (let ((mini (minibuffer-window frame
)))
2217 (and (eq (window-frame mini
) frame
)
2218 (not (eq mini root
)) mini
)))
2219 (mini-size (and mini
(window-size mini horizontal t
))))
2220 ;; We round the line/column sizes of windows here to the nearest
2221 ;; integer. In some cases this can make windows appear _larger_
2222 ;; than the containing frame (line/column-wise) because the latter's
2223 ;; sizes are not (yet) rounded. We might eventually fix that.
2224 (if (and mini
(not horizontal
))
2226 (set-window-new-total root
(max (/ root-size char-size
) 1))
2227 (set-window-new-total mini
(max (/ mini-size char-size
) 1))
2228 (setq lines
(- (round (+ root-size mini-size
) char-size
)
2229 (+ (window-new-total root
) (window-new-total mini
))))
2231 (if (>= (% root-size
(window-new-total root
))
2232 (% mini-size
(window-new-total mini
)))
2233 (set-window-new-total root
1 t
)
2234 (set-window-new-total mini
1 t
))
2235 (setq lines
(1- lines
))))
2236 (set-window-new-total root
(round root-size char-size
))
2238 ;; This is taken in the horizontal case only.
2239 (set-window-new-total mini
(round mini-size char-size
))))
2240 (unless (window-buffer root
)
2241 (window--pixel-to-total-1 root horizontal char-size
))
2242 ;; Apply the new sizes.
2243 (window-resize-apply-total frame horizontal
)))
2245 (defun window--resize-reset (&optional frame horizontal
)
2246 "Reset resize values for all windows on FRAME.
2247 FRAME defaults to the selected frame.
2249 This function stores the current value of `window-size' applied
2250 with argument HORIZONTAL in the new total size of all windows on
2251 FRAME. It also resets the new normal size of each of these
2253 (window--resize-reset-1
2254 (frame-root-window (window-normalize-frame frame
)) horizontal
))
2256 (defun window--resize-reset-1 (window horizontal
)
2257 "Internal function of `window--resize-reset'."
2258 ;; Register old size in the new total size.
2259 (set-window-new-pixel window
(window-size window horizontal t
))
2260 (set-window-new-total window
(window-size window horizontal
))
2261 ;; Reset new normal size.
2262 (set-window-new-normal window
)
2263 (when (window-child window
)
2264 (window--resize-reset-1 (window-child window
) horizontal
))
2265 (when (window-right window
)
2266 (window--resize-reset-1 (window-right window
) horizontal
)))
2268 ;; The following routine is used to manually resize the minibuffer
2269 ;; window and is currently used, for example, by ispell.el.
2270 (defun window--resize-mini-window (window delta
)
2271 "Resize minibuffer window WINDOW by DELTA pixels.
2272 If WINDOW cannot be resized by DELTA pixels make it as large (or
2273 as small) as possible, but don't signal an error."
2274 (when (window-minibuffer-p window
)
2275 (let* ((frame (window-frame window
))
2276 (root (frame-root-window frame
))
2277 (height (window-pixel-height window
))
2279 (- (window-pixel-height root
)
2280 (window-min-size root nil nil t
))))
2283 ((<= (+ height delta
) 0)
2284 (setq delta
(- (frame-char-height (window-frame window
)) height
)))
2285 ((> delta min-delta
)
2286 (setq delta min-delta
)))
2288 (unless (zerop delta
)
2290 (window--resize-reset frame
)
2291 ;; Ideally we should be able to resize just the last child of root
2292 ;; here. See the comment in `resize-root-window-vertically' for
2293 ;; why we do not do that.
2294 (window--resize-this-window root
(- delta
) nil nil t
)
2295 (set-window-new-pixel window
(+ height delta
))
2296 ;; The following routine catches the case where we want to resize
2297 ;; a minibuffer-only frame.
2298 (when (resize-mini-window-internal window
)
2299 (window--pixel-to-total frame
)
2300 (run-window-configuration-change-hook frame
))))))
2302 (defun window--resize-apply-p (frame &optional horizontal
)
2303 "Return t when a window on FRAME shall be resized vertically.
2304 Optional argument HORIZONTAL non-nil means return t when a window
2305 shall be resized horizontally."
2309 (unless (= (window-new-pixel window
)
2310 (window-size window horizontal t
))
2315 (defun window-resize (window delta
&optional horizontal ignore pixelwise
)
2316 "Resize WINDOW vertically by DELTA lines.
2317 WINDOW can be an arbitrary window and defaults to the selected
2318 one. An attempt to resize the root window of a frame will raise
2321 DELTA a positive number means WINDOW shall be enlarged by DELTA
2322 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2325 Optional argument HORIZONTAL non-nil means resize WINDOW
2326 horizontally by DELTA columns. In this case a positive DELTA
2327 means enlarge WINDOW by DELTA columns. DELTA negative means
2328 WINDOW shall be shrunk by -DELTA columns.
2330 Optional argument IGNORE non-nil means ignore restrictions
2331 imposed by fixed size windows, `window-min-height' or
2332 `window-min-width' settings. If IGNORE is a window, ignore
2333 restrictions for that window only. If IGNORE equals `safe',
2334 live windows may get as small as `window-safe-min-height' lines
2335 and `window-safe-min-width' columns. Any other non-nil value
2336 means ignore all of the above restrictions for all windows.
2338 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2341 This function resizes other windows proportionally and never
2342 deletes any windows. If you want to move only the low (right)
2343 edge of WINDOW consider using `adjust-window-trailing-edge'
2345 (setq window
(window-normalize-window window
))
2346 (let* ((frame (window-frame window
))
2347 (minibuffer-window (minibuffer-window frame
))
2349 (setq delta
(window--size-to-pixel
2350 window delta horizontal pixelwise t
))
2352 ((eq window
(frame-root-window frame
))
2353 (error "Cannot resize the root window of a frame"))
2354 ((window-minibuffer-p window
)
2356 (error "Cannot resize minibuffer window horizontally")
2357 (window--resize-mini-window window delta
)))
2358 ((and (not horizontal
)
2359 (window-full-height-p window
)
2360 (eq (window-frame minibuffer-window
) frame
)
2361 (or (not resize-mini-windows
)
2362 (eq minibuffer-window
(active-minibuffer-window))))
2363 ;; If WINDOW is full height and either `resize-mini-windows' is
2364 ;; nil or the minibuffer window is active, resize the minibuffer
2366 (window--resize-mini-window minibuffer-window
(- delta
)))
2367 ((window--resizable-p
2368 window delta horizontal ignore nil nil nil t
)
2369 (window--resize-reset frame horizontal
)
2370 (window--resize-this-window window delta horizontal ignore t
)
2371 (if (and (not window-combination-resize
)
2372 (window-combined-p window horizontal
)
2373 (setq sibling
(or (window-right window
) (window-left window
)))
2375 sibling
(- delta
) horizontal ignore t
))
2376 ;; If window-combination-resize is nil, WINDOW is part of an
2377 ;; iso-combination, and WINDOW's neighboring right or left
2378 ;; sibling can be resized as requested, resize that sibling.
2381 (window-size (window-parent window
) horizontal t
))))
2382 (window--resize-this-window sibling
(- delta
) horizontal nil t
)
2383 (set-window-new-normal
2384 window
(+ (window-normal-size window horizontal
)
2386 (set-window-new-normal
2387 sibling
(- (window-normal-size sibling horizontal
)
2389 ;; Otherwise, resize all other windows in the same combination.
2390 (window--resize-siblings window delta horizontal ignore
))
2391 (when (window--resize-apply-p frame horizontal
)
2392 (if (window-resize-apply frame horizontal
)
2394 (window--pixel-to-total frame horizontal
)
2395 (run-window-configuration-change-hook frame
))
2396 (error "Failed to apply resizing %s" window
))))
2398 (error "Cannot resize window %s" window
)))))
2400 (defun window-resize-no-error (window delta
&optional horizontal ignore pixelwise
)
2401 "Resize WINDOW vertically if it is resizable by DELTA lines.
2402 This function is like `window-resize' but does not signal an
2403 error when WINDOW cannot be resized. For the meaning of the
2404 optional arguments see the documentation of `window-resize'.
2406 Optional argument PIXELWISE non-nil means interpret DELTA as
2408 (when (window--resizable-p
2409 window delta horizontal ignore nil nil nil pixelwise
)
2410 (window-resize window delta horizontal ignore pixelwise
)))
2412 (defun window--resize-child-windows-skip-p (window)
2413 "Return non-nil if WINDOW shall be skipped by resizing routines."
2414 (memq (window-new-normal window
) '(ignore stuck skip
)))
2416 (defun window--resize-child-windows-normal (parent horizontal window this-delta
&optional trail other-delta
)
2417 "Recursively set new normal height of child windows of window PARENT.
2418 HORIZONTAL non-nil means set the new normal width of these
2419 windows. WINDOW specifies a child window of PARENT that has been
2420 resized by THIS-DELTA lines (columns).
2422 Optional argument TRAIL either `before' or `after' means set values
2423 only for windows before or after WINDOW. Optional argument
2424 OTHER-DELTA, a number, specifies that this many lines (columns)
2425 have been obtained from (or returned to) an ancestor window of
2426 PARENT in order to resize WINDOW."
2427 (let* ((delta-normal
2428 (if (and (= (- this-delta
)
2429 (window-size window horizontal t
))
2430 (zerop other-delta
))
2431 ;; When WINDOW gets deleted and we can return its entire
2432 ;; space to its siblings, use WINDOW's normal size as the
2434 (- (window-normal-size window horizontal
))
2435 ;; In any other case calculate the normal delta from the
2436 ;; relation of THIS-DELTA to the total size of PARENT.
2437 (/ (float this-delta
)
2438 (window-size parent horizontal t
))))
2439 (sub (window-child parent
))
2441 (skip (eq trail
'after
)))
2443 ;; Set parent-normal to the sum of the normal sizes of all child
2444 ;; windows of PARENT that shall be resized, excluding only WINDOW
2445 ;; and any windows specified by the optional TRAIL argument.
2449 (setq skip
(eq trail
'before
)))
2453 (+ parent-normal
(window-normal-size sub horizontal
)))))
2454 (setq sub
(window-right sub
)))
2456 ;; Set the new normal size of all child windows of PARENT from what
2457 ;; they should have contributed for recovering THIS-DELTA lines
2459 (setq sub
(window-child parent
))
2460 (setq skip
(eq trail
'after
))
2464 (setq skip
(eq trail
'before
)))
2467 (let ((old-normal (window-normal-size sub horizontal
)))
2468 (set-window-new-normal
2469 sub
(min 1.0 ; Don't get larger than 1.
2471 (* (/ old-normal parent-normal
)
2473 ;; Don't drop below 0.
2475 (setq sub
(window-right sub
)))
2477 (when (numberp other-delta
)
2478 ;; Set the new normal size of windows from what they should have
2479 ;; contributed for recovering OTHER-DELTA lines (columns).
2480 (setq delta-normal
(/ (float (window-size parent horizontal t
))
2481 (+ (window-size parent horizontal t
)
2483 (setq sub
(window-child parent
))
2484 (setq skip
(eq trail
'after
))
2488 (setq skip
(eq trail
'before
)))
2491 (set-window-new-normal
2492 sub
(min 1.0 ; Don't get larger than 1.
2493 (max (* (window-new-normal sub
) delta-normal
)
2494 ;; Don't drop below 0.
2496 (setq sub
(window-right sub
))))
2498 ;; Set the new normal size of WINDOW to what is left by the sum of
2499 ;; the normal sizes of its siblings.
2500 (set-window-new-normal
2503 (setq sub
(window-child parent
))
2507 ((not (numberp (window-new-normal sub
)))
2508 (setq sum
(+ sum
(window-normal-size sub horizontal
))))
2510 (setq sum
(+ sum
(window-new-normal sub
)))))
2511 (setq sub
(window-right sub
)))
2512 ;; Don't get larger than 1 or smaller than 0.
2513 (min 1.0 (max (- 1.0 sum
) 0.0))))))
2515 (defun window--resize-child-windows (parent delta
&optional horizontal window ignore trail edge char-size
)
2516 "Resize child windows of window PARENT vertically by DELTA pixels.
2517 PARENT must be a vertically combined internal window.
2519 Optional argument HORIZONTAL non-nil means resize child windows
2520 of PARENT horizontally by DELTA pixels. In this case PARENT must
2521 be a horizontally combined internal window.
2523 WINDOW, if specified, must denote a child window of PARENT that
2524 is resized by DELTA pixels.
2526 Optional argument IGNORE non-nil means ignore restrictions
2527 imposed by fixed size windows, `window-min-height' or
2528 `window-min-width' settings. If IGNORE equals `safe', live
2529 windows may get as small as `window-safe-min-height' lines and
2530 `window-safe-min-width' columns. If IGNORE is a window, ignore
2531 restrictions for that window only. Any other non-nil value means
2532 ignore all of the above restrictions for all windows.
2534 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2535 of windows that shall be resized. If TRAIL equals `before',
2536 resize only windows on the left or above EDGE. If TRAIL equals
2537 `after', resize only windows on the right or below EDGE. Also,
2538 preferably only resize windows adjacent to EDGE.
2540 If the optional argument CHAR-SIZE is a positive integer, it specifies
2541 the number of pixels by which windows are incrementally resized.
2542 If CHAR-SIZE is nil, this means to use the value of
2543 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2545 Return the symbol `normalized' if new normal sizes have been
2546 already set by this routine."
2547 (let* ((first (window-child parent
))
2548 (last (window-last-child parent
))
2549 (parent-total (+ (window-size parent horizontal t
)
2551 (char-size (or char-size
2552 (and window-resize-pixelwise
1)
2553 (frame-char-size window horizontal
)))
2554 sub best-window best-value best-delta
)
2556 (if (and edge
(memq trail
'(before after
))
2559 (while (and (window-right sub
)
2560 (or (and (eq trail
'before
)
2561 (not (window--resize-child-windows-skip-p
2562 (window-right sub
))))
2563 (and (eq trail
'after
)
2564 (window--resize-child-windows-skip-p sub
))))
2565 (setq sub
(window-right sub
)))
2568 (if (eq trail
'before
)
2569 (= (+ (window-pixel-left sub
) (window-pixel-width sub
))
2571 (= (window-pixel-left sub
) edge
))
2572 (if (eq trail
'before
)
2573 (= (+ (window-pixel-top sub
) (window-pixel-height sub
))
2575 (= (window-pixel-top sub
) edge
)))
2576 (window-sizable-p sub delta horizontal ignore t
))
2577 ;; Resize only windows adjacent to EDGE.
2579 (window--resize-this-window
2580 sub delta horizontal ignore t trail edge
)
2581 (if (and window
(eq (window-parent sub
) parent
))
2583 ;; Assign new normal sizes.
2584 (set-window-new-normal
2585 sub
(/ (float (window-new-pixel sub
)) parent-total
))
2586 (set-window-new-normal
2587 window
(- (window-normal-size window horizontal
)
2588 (- (window-new-normal sub
)
2589 (window-normal-size sub horizontal
)))))
2590 (window--resize-child-windows-normal
2591 parent horizontal sub
0 trail delta
))
2592 ;; Return 'normalized to notify `window--resize-siblings' that
2593 ;; normal sizes have been already set.
2595 ;; Resize all windows proportionally.
2599 ((or (window--resize-child-windows-skip-p sub
)
2600 ;; Ignore windows to skip and fixed-size child windows -
2601 ;; in the latter case make it a window to skip.
2603 (window-size-fixed-p sub horizontal
)
2604 (set-window-new-normal sub
'ignore
))))
2606 ;; When shrinking store the number of lines/cols we can get
2607 ;; from this window here together with the total/normal size
2609 (set-window-new-normal
2612 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
2613 (window-min-delta sub horizontal ignore trail t nil t
)
2614 (- (/ (float (window-size sub horizontal t
))
2616 (window-normal-size sub horizontal
)))))
2618 ;; When enlarging store the total/normal size factor only
2619 (set-window-new-normal
2621 (- (/ (float (window-size sub horizontal t
))
2623 (window-normal-size sub horizontal
)))))
2625 (setq sub
(window-left sub
)))
2629 ;; Shrink windows by delta.
2630 (setq best-window t
)
2631 (while (and best-window
(not (zerop delta
)))
2633 (setq best-window nil
)
2634 (setq best-value most-negative-fixnum
)
2636 (when (and (consp (window-new-normal sub
))
2637 (not (<= (car (window-new-normal sub
)) 0))
2638 (> (cdr (window-new-normal sub
)) best-value
))
2639 (setq best-window sub
)
2640 (setq best-value
(cdr (window-new-normal sub
))))
2642 (setq sub
(window-left sub
)))
2645 (setq best-delta
(min (car (window-new-normal best-window
))
2646 char-size
(- delta
)))
2647 (setq delta
(+ delta best-delta
))
2648 (set-window-new-pixel best-window
(- best-delta
) t
)
2649 (set-window-new-normal
2651 (if (= (car (window-new-normal best-window
)) best-delta
)
2652 'skip
; We can't shrink best-window any further.
2653 (cons (- (car (window-new-normal best-window
)) best-delta
)
2654 (- (/ (float (window-new-pixel best-window
))
2656 (window-normal-size best-window horizontal
))))))))
2658 ;; Enlarge windows by delta.
2659 (setq best-window t
)
2660 (while (and best-window
(not (zerop delta
)))
2662 (setq best-window nil
)
2663 (setq best-value most-positive-fixnum
)
2665 (when (and (numberp (window-new-normal sub
))
2666 (< (window-new-normal sub
) best-value
))
2667 (setq best-window sub
)
2668 (setq best-value
(window-new-normal sub
)))
2670 (setq sub
(window-left sub
)))
2673 (setq best-delta
(min delta char-size
))
2674 (setq delta
(- delta best-delta
))
2675 (set-window-new-pixel best-window best-delta t
)
2676 (set-window-new-normal
2678 (- (/ (float (window-new-pixel best-window
))
2680 (window-normal-size best-window horizontal
)))))))
2685 (when (or (consp (window-new-normal sub
))
2686 (numberp (window-new-normal sub
)))
2687 ;; Reset new normal size fields so `window-resize-apply'
2688 ;; won't use them to apply new sizes.
2689 (set-window-new-normal sub
))
2691 (unless (eq (window-new-normal sub
) 'ignore
)
2692 ;; Resize this window's child windows (back-engineering
2693 ;; delta from sub's old and new total sizes).
2694 (let ((delta (- (window-new-pixel sub
)
2695 (window-size sub horizontal t
))))
2696 (unless (and (zerop delta
) (not trail
))
2697 ;; For the TRAIL non-nil case we have to resize SUB
2698 ;; recursively even if it's size does not change.
2699 (window--resize-this-window
2700 sub delta horizontal ignore nil trail edge
))))
2701 (setq sub
(window-left sub
)))))))
2703 (defun window--resize-siblings (window delta
&optional horizontal ignore trail edge char-size
)
2704 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
2705 Optional argument HORIZONTAL non-nil means resize other windows
2706 when WINDOW is resized horizontally by DELTA pixels. WINDOW
2707 itself is not resized by this function.
2709 Optional argument IGNORE non-nil means ignore restrictions
2710 imposed by fixed size windows, `window-min-height' or
2711 `window-min-width' settings. If IGNORE equals `safe', live
2712 windows may get as small as `window-safe-min-height' lines and
2713 `window-safe-min-width' columns. If IGNORE is a window, ignore
2714 restrictions for that window only. Any other non-nil value means
2715 ignore all of the above restrictions for all windows.
2717 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2718 of windows that shall be resized. If TRAIL equals `before',
2719 resize only windows on the left or above EDGE. If TRAIL equals
2720 `after', resize only windows on the right or below EDGE. Also,
2721 preferably only resize windows adjacent to EDGE."
2722 (when (window-parent window
)
2723 (let* ((parent (window-parent window
))
2724 (sub (window-child parent
)))
2725 (if (window-combined-p sub horizontal
)
2726 ;; In an iso-combination try to extract DELTA from WINDOW's
2728 (let ((skip (eq trail
'after
))
2729 this-delta other-delta
)
2730 ;; Decide which windows shall be left alone.
2734 ;; Make sure WINDOW is left alone when
2735 ;; resizing its siblings.
2736 (set-window-new-normal sub
'ignore
)
2737 (setq skip
(eq trail
'before
)))
2739 ;; Make sure this sibling is left alone when
2740 ;; resizing its siblings.
2741 (set-window-new-normal sub
'ignore
))
2742 ((or (window--size-ignore-p sub ignore
)
2743 (not (window-size-fixed-p sub horizontal
)))
2744 ;; Set this-delta to t to signal that we found a sibling
2745 ;; of WINDOW whose size is not fixed.
2746 (setq this-delta t
)))
2748 (setq sub
(window-right sub
)))
2750 ;; Set this-delta to what we can get from WINDOW's siblings.
2751 (if (= (- delta
) (window-size window horizontal t
))
2752 ;; A deletion, presumably. We must handle this case
2753 ;; specially since `window--resizable' can't be used.
2755 ;; There's at least one resizable sibling we can
2756 ;; give WINDOW's size to.
2757 (setq this-delta delta
)
2758 ;; No resizable sibling exists.
2759 (setq this-delta
0))
2760 ;; Any other form of resizing.
2763 window delta horizontal ignore trail t nil t
)))
2765 ;; Set other-delta to what we still have to get from
2766 ;; ancestor windows of parent.
2767 (setq other-delta
(- delta this-delta
))
2768 (unless (zerop other-delta
)
2769 ;; Unless we got everything from WINDOW's siblings, PARENT
2770 ;; must be resized by other-delta lines or columns.
2771 (set-window-new-pixel parent other-delta
'add
))
2773 (if (zerop this-delta
)
2774 ;; We haven't got anything from WINDOW's siblings but we
2775 ;; must update the normal sizes to respect other-delta.
2776 (window--resize-child-windows-normal
2777 parent horizontal window this-delta trail other-delta
)
2778 ;; We did get something from WINDOW's siblings which means
2779 ;; we have to resize their child windows.
2780 (unless (eq (window--resize-child-windows
2781 parent
(- this-delta
) horizontal
2782 window ignore trail edge char-size
)
2783 ;; If `window--resize-child-windows' returns
2784 ;; 'normalized, this means it has set the
2785 ;; normal sizes already.
2787 ;; Set the normal sizes.
2788 (window--resize-child-windows-normal
2789 parent horizontal window this-delta trail other-delta
))
2790 ;; Set DELTA to what we still have to get from ancestor
2792 (setq delta other-delta
)))
2794 ;; In an ortho-combination all siblings of WINDOW must be
2795 ;; resized by DELTA.
2796 (set-window-new-pixel parent delta
'add
)
2798 (unless (eq sub window
)
2799 (window--resize-this-window
2800 sub delta horizontal ignore t
))
2801 (setq sub
(window-right sub
))))
2803 (unless (zerop delta
)
2805 (window--resize-siblings
2806 parent delta horizontal ignore trail edge char-size
)))))
2808 (defun window--resize-this-window (window delta
&optional horizontal ignore add trail edge char-size
)
2809 "Resize WINDOW vertically by DELTA pixels.
2810 Optional argument HORIZONTAL non-nil means resize WINDOW
2811 horizontally by DELTA pixels.
2813 Optional argument IGNORE non-nil means ignore restrictions
2814 imposed by fixed size windows, `window-min-height' or
2815 `window-min-width' settings. If IGNORE equals `safe', live
2816 windows may get as small as `window-safe-min-height' lines and
2817 `window-safe-min-width' columns. If IGNORE is a window, ignore
2818 restrictions for that window only. Any other non-nil value
2819 means ignore all of the above restrictions for all windows.
2821 Optional argument ADD non-nil means add DELTA to the new total
2824 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2825 of windows that shall be resized. If TRAIL equals `before',
2826 resize only windows on the left or above EDGE. If TRAIL equals
2827 `after', resize only windows on the right or below EDGE. Also,
2828 preferably only resize windows adjacent to EDGE.
2830 If the optional argument CHAR-SIZE is a positive integer, it specifies
2831 the number of pixels by which windows are incrementally resized.
2832 If CHAR-SIZE is nil, this means to use the value of
2833 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2835 This function recursively resizes WINDOW's child windows to fit the
2836 new size. Make sure that WINDOW is `window--resizable' before
2837 calling this function. Note that this function does not resize
2838 siblings of WINDOW or WINDOW's parent window. You have to
2839 eventually call `window-resize-apply' in order to make resizing
2840 actually take effect."
2842 ;; Add DELTA to the new total size of WINDOW.
2843 (set-window-new-pixel window delta t
))
2845 (let ((sub (window-child window
)))
2848 ((window-combined-p sub horizontal
)
2849 ;; In an iso-combination resize child windows according to their
2851 (window--resize-child-windows
2852 window delta horizontal nil ignore trail edge char-size
))
2853 ;; In an ortho-combination resize each child window by DELTA.
2856 (window--resize-this-window
2857 sub delta horizontal ignore t trail edge char-size
)
2858 (setq sub
(window-right sub
)))))))
2860 (defun window--resize-root-window (window delta horizontal ignore pixelwise
)
2861 "Resize root window WINDOW vertically by DELTA lines.
2862 HORIZONTAL non-nil means resize root window WINDOW horizontally
2865 IGNORE non-nil means ignore any restrictions imposed by fixed
2866 size windows, `window-min-height' or `window-min-width' settings.
2868 This function is only called by the frame resizing routines. It
2869 resizes windows proportionally and never deletes any windows."
2870 (when (and (windowp window
) (numberp delta
))
2874 (window--size-to-pixel window delta horizontal
))))
2875 (when (window-sizable-p window pixel-delta horizontal ignore t
)
2876 (window--resize-reset (window-frame window
) horizontal
)
2877 (window--resize-this-window
2878 window pixel-delta horizontal ignore t
)))))
2880 (defun window--resize-root-window-vertically (window delta pixelwise
)
2881 "Resize root window WINDOW vertically by DELTA lines.
2882 If DELTA is less than zero and we can't shrink WINDOW by DELTA
2883 lines, shrink it as much as possible. If DELTA is greater than
2884 zero, this function can resize fixed-size windows in order to
2885 recover the necessary lines. Return the number of lines that
2888 Third argument PIXELWISE non-nil means to interpret DELTA as
2889 pixels and return the number of pixels that were recovered.
2891 This function is called by the minibuffer window resizing
2893 (let* ((frame (window-frame window
))
2899 (* (frame-char-height frame
) delta
))
2903 ((zerop pixel-delta
))
2905 (setq pixel-delta
(window-sizable window pixel-delta nil nil pixelwise
))
2906 (window--resize-reset frame
)
2907 ;; When shrinking the root window, emulate an edge drag in order
2908 ;; to not resize other windows if we can avoid it (Bug#12419).
2909 (window--resize-this-window
2910 window pixel-delta nil ignore t
'before
2911 (+ (window-pixel-top window
) (window-pixel-height window
)))
2912 ;; Don't record new normal sizes to make sure that shrinking back
2913 ;; proportionally works as intended.
2915 (lambda (window) (set-window-new-normal window
'ignore
)) frame t
))
2917 (window--resize-reset frame
)
2918 (unless (window-sizable window pixel-delta nil nil pixelwise
)
2920 ;; When growing the root window, resize proportionally. This
2921 ;; should give windows back their original sizes (hopefully).
2922 (window--resize-this-window
2923 window pixel-delta nil ignore t
)))
2924 ;; Return the possibly adjusted DELTA.
2927 (/ pixel-delta
(frame-char-height frame
)))))
2929 (defun adjust-window-trailing-edge (window delta
&optional horizontal pixelwise
)
2930 "Move WINDOW's bottom edge by DELTA lines.
2931 Optional argument HORIZONTAL non-nil means move WINDOW's right
2932 edge by DELTA columns. WINDOW must be a valid window and
2933 defaults to the selected one.
2935 Optional argument PIXELWISE non-nil means interpret DELTA as
2938 If DELTA is greater than zero, move the edge downwards or to the
2939 right. If DELTA is less than zero, move the edge upwards or to
2940 the left. If the edge can't be moved by DELTA lines or columns,
2941 move it as far as possible in the desired direction."
2942 (setq window
(window-normalize-window window
))
2943 (let* ((frame (window-frame window
))
2944 (minibuffer-window (minibuffer-window frame
))
2946 left this-delta min-delta max-delta
)
2950 (setq delta
(* delta
(frame-char-size window horizontal
))))
2952 ;; Find the edge we want to move.
2953 (while (and (or (not (window-combined-p right horizontal
))
2954 (not (window-right right
)))
2955 (setq right
(window-parent right
))))
2957 ((and (not right
) (not horizontal
)
2958 ;; Resize the minibuffer window if it's on the same frame as
2959 ;; and immediately below WINDOW and it's either active or
2960 ;; `resize-mini-windows' is nil.
2961 (eq (window-frame minibuffer-window
) frame
)
2962 (= (nth 1 (window-pixel-edges minibuffer-window
))
2963 (nth 3 (window-pixel-edges window
)))
2964 (or (not resize-mini-windows
)
2965 (eq minibuffer-window
(active-minibuffer-window))))
2966 (window--resize-mini-window minibuffer-window
(- delta
)))
2967 ((or (not (setq left right
)) (not (setq right
(window-right right
))))
2969 (error "No window on the right of this one")
2970 (error "No window below this one")))
2972 ;; Set LEFT to the first resizable window on the left. This step is
2973 ;; needed to handle fixed-size windows.
2974 (while (and left
(window-size-fixed-p left horizontal
))
2976 (or (window-left left
)
2978 (while (and (setq left
(window-parent left
))
2979 (not (window-combined-p left horizontal
))))
2980 (window-left left
)))))
2983 (error "No resizable window on the left of this one")
2984 (error "No resizable window above this one")))
2986 ;; Set RIGHT to the first resizable window on the right. This step
2987 ;; is needed to handle fixed-size windows.
2988 (while (and right
(window-size-fixed-p right horizontal
))
2990 (or (window-right right
)
2992 (while (and (setq right
(window-parent right
))
2993 (not (window-combined-p right horizontal
))))
2994 (window-right right
)))))
2997 (error "No resizable window on the right of this one")
2998 (error "No resizable window below this one")))
3000 ;; LEFT and RIGHT (which might be both internal windows) are now the
3001 ;; two windows we want to resize.
3005 (window--max-delta-1
3006 left
0 horizontal nil
'after nil pixelwise
))
3008 (window--min-delta-1
3009 right
(- delta
) horizontal nil
'before nil pixelwise
))
3010 (when (or (< max-delta delta
) (> min-delta
(- delta
)))
3011 ;; We can't get the whole DELTA - move as far as possible.
3012 (setq delta
(min max-delta
(- min-delta
))))
3013 (unless (zerop delta
)
3015 (window--resize-reset frame horizontal
)
3016 ;; Try to enlarge LEFT first.
3017 (setq this-delta
(window--resizable
3018 left delta horizontal nil
'after nil nil pixelwise
))
3019 (unless (zerop this-delta
)
3020 (window--resize-this-window
3021 left this-delta horizontal nil t
'before
3023 (+ (window-pixel-left left
) (window-pixel-width left
))
3024 (+ (window-pixel-top left
) (window-pixel-height left
)))))
3025 ;; Shrink windows on right of LEFT.
3026 (window--resize-siblings
3027 left delta horizontal nil
'after
3029 (window-pixel-left right
)
3030 (window-pixel-top right
)))))
3033 (window--max-delta-1
3034 right
0 horizontal nil
'before nil pixelwise
))
3036 (window--min-delta-1
3037 left delta horizontal nil
'after nil pixelwise
))
3038 (when (or (< max-delta
(- delta
)) (> min-delta delta
))
3039 ;; We can't get the whole DELTA - move as far as possible.
3040 (setq delta
(max (- max-delta
) min-delta
)))
3041 (unless (zerop delta
)
3043 (window--resize-reset frame horizontal
)
3044 ;; Try to enlarge RIGHT.
3047 right
(- delta
) horizontal nil
'before nil nil pixelwise
))
3048 (unless (zerop this-delta
)
3049 (window--resize-this-window
3050 right this-delta horizontal nil t
'after
3052 (window-pixel-left right
)
3053 (window-pixel-top right
))))
3054 ;; Shrink windows on left of RIGHT.
3055 (window--resize-siblings
3056 right
(- delta
) horizontal nil
'before
3058 (+ (window-pixel-left left
) (window-pixel-width left
))
3059 (+ (window-pixel-top left
) (window-pixel-height left
)))))))
3060 (unless (zerop delta
)
3061 ;; Don't report an error in the standard case.
3062 (when (window--resize-apply-p frame horizontal
)
3063 (if (window-resize-apply frame horizontal
)
3065 (window--pixel-to-total frame horizontal
)
3066 (run-window-configuration-change-hook frame
))
3067 ;; But do report an error if applying the changes fails.
3068 (error "Failed adjusting window %s" window
))))))))
3070 (defun enlarge-window (delta &optional horizontal
)
3071 "Make the selected window DELTA lines taller.
3072 Interactively, if no argument is given, make the selected window
3073 one line taller. If optional argument HORIZONTAL is non-nil,
3074 make selected window wider by DELTA columns. If DELTA is
3075 negative, shrink selected window by -DELTA lines or columns."
3077 (let ((minibuffer-window (minibuffer-window)))
3080 ((window-size-fixed-p nil horizontal
)
3081 (error "Selected window has fixed size"))
3082 ((window-minibuffer-p)
3084 (error "Cannot resize minibuffer window horizontally")
3085 (window--resize-mini-window (selected-window) delta
)))
3086 ((and (not horizontal
)
3087 (window-full-height-p)
3088 (eq (window-frame minibuffer-window
) (selected-frame))
3089 (not resize-mini-windows
))
3090 ;; If the selected window is full height and `resize-mini-windows'
3091 ;; is nil, resize the minibuffer window.
3092 (window--resize-mini-window minibuffer-window
(- delta
)))
3093 ((window--resizable-p nil delta horizontal
)
3094 (window-resize nil delta horizontal
))
3098 (window-max-delta nil horizontal
)
3099 (- (window-min-delta nil horizontal
)))
3102 (defun shrink-window (delta &optional horizontal
)
3103 "Make the selected window DELTA lines smaller.
3104 Interactively, if no argument is given, make the selected window
3105 one line smaller. If optional argument HORIZONTAL is non-nil,
3106 make selected window narrower by DELTA columns. If DELTA is
3107 negative, enlarge selected window by -DELTA lines or columns.
3108 Also see the `window-min-height' variable."
3110 (let ((minibuffer-window (minibuffer-window)))
3113 ((window-size-fixed-p nil horizontal
)
3114 (error "Selected window has fixed size"))
3115 ((window-minibuffer-p)
3117 (error "Cannot resize minibuffer window horizontally")
3118 (window--resize-mini-window (selected-window) (- delta
))))
3119 ((and (not horizontal
)
3120 (window-full-height-p)
3121 (eq (window-frame minibuffer-window
) (selected-frame))
3122 (not resize-mini-windows
))
3123 ;; If the selected window is full height and `resize-mini-windows'
3124 ;; is nil, resize the minibuffer window.
3125 (window--resize-mini-window minibuffer-window delta
))
3126 ((window--resizable-p nil
(- delta
) horizontal
)
3127 (window-resize nil
(- delta
) horizontal
))
3131 (- (window-min-delta nil horizontal
))
3132 (window-max-delta nil horizontal
))
3135 (defun maximize-window (&optional window
)
3137 Make WINDOW as large as possible without deleting any windows.
3138 WINDOW must be a valid window and defaults to the selected one.
3140 If the option `window-resize-pixelwise' is non-nil maximize
3143 (setq window
(window-normalize-window window
))
3145 window
(window-max-delta window nil nil nil nil nil window-resize-pixelwise
)
3146 nil nil window-resize-pixelwise
)
3148 window
(window-max-delta window t nil nil nil nil window-resize-pixelwise
)
3149 t nil window-resize-pixelwise
))
3151 (defun minimize-window (&optional window
)
3153 Make WINDOW as small as possible without deleting any windows.
3154 WINDOW must be a valid window and defaults to the selected one.
3156 If the option `window-resize-pixelwise' is non-nil minimize
3159 (setq window
(window-normalize-window window
))
3162 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise
))
3163 nil nil window-resize-pixelwise
)
3166 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise
))
3167 t nil window-resize-pixelwise
))
3169 (defun frame-root-window-p (window)
3170 "Return non-nil if WINDOW is the root window of its frame."
3171 (eq window
(frame-root-window window
)))
3173 (defun window--subtree (window &optional next
)
3174 "Return window subtree rooted at WINDOW.
3175 Optional argument NEXT non-nil means include WINDOW's right
3176 siblings in the return value.
3178 See the documentation of `window-tree' for a description of the
3185 ((window-top-child window
)
3186 (cons t
(cons (window-edges window
)
3187 (window--subtree (window-top-child window
) t
))))
3188 ((window-left-child window
)
3189 (cons nil
(cons (window-edges window
)
3190 (window--subtree (window-left-child window
) t
))))
3193 (setq window
(when next
(window-next-sibling window
))))
3196 (defun window-tree (&optional frame
)
3197 "Return the window tree of frame FRAME.
3198 FRAME must be a live frame and defaults to the selected frame.
3199 The return value is a list of the form (ROOT MINI), where ROOT
3200 represents the window tree of the frame's root window, and MINI
3201 is the frame's minibuffer window.
3203 If the root window is not split, ROOT is the root window itself.
3204 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3205 for a horizontal split, and t for a vertical split. EDGES gives
3206 the combined size and position of the child windows in the split,
3207 and the rest of the elements are the child windows in the split.
3208 Each of the child windows may again be a window or a list
3209 representing a window split, and so on. EDGES is a list (LEFT
3210 TOP RIGHT BOTTOM) as returned by `window-edges'."
3211 (setq frame
(window-normalize-frame frame
))
3212 (window--subtree (frame-root-window frame
) t
))
3214 (defun other-window (count &optional all-frames
)
3215 "Select another window in cyclic ordering of windows.
3216 COUNT specifies the number of windows to skip, starting with the
3217 selected window, before making the selection. If COUNT is
3218 positive, skip COUNT windows forwards. If COUNT is negative,
3219 skip -COUNT windows backwards. COUNT zero means do not skip any
3220 window, so select the selected window. In an interactive call,
3221 COUNT is the numeric prefix argument. Return nil.
3223 If the `other-window' parameter of the selected window is a
3224 function and `ignore-window-parameters' is nil, call that
3225 function with the arguments COUNT and ALL-FRAMES.
3227 This function does not select a window whose `no-other-window'
3228 window parameter is non-nil.
3230 This function uses `next-window' for finding the window to
3231 select. The argument ALL-FRAMES has the same meaning as in
3232 `next-window', but the MINIBUF argument of `next-window' is
3233 always effectively nil."
3235 (let* ((window (selected-window))
3236 (function (and (not ignore-window-parameters
)
3237 (window-parameter window
'other-window
)))
3238 old-window old-count
)
3239 (if (functionp function
)
3240 (funcall function count all-frames
)
3241 ;; `next-window' and `previous-window' may return a window we are
3242 ;; not allowed to select. Hence we need an exit strategy in case
3243 ;; all windows are non-selectable.
3246 (setq window
(next-window window nil all-frames
))
3248 ((eq window old-window
)
3249 (when (= count old-count
)
3250 ;; Keep out of infinite loops. When COUNT has not changed
3251 ;; since we last looked at `window' we're probably in one.
3253 ((window-parameter window
'no-other-window
)
3255 ;; The first non-selectable window `next-window' got us:
3256 ;; Remember it and the current value of COUNT.
3257 (setq old-window window
)
3258 (setq old-count count
)))
3260 (setq count
(1- count
)))))
3262 (setq window
(previous-window window nil all-frames
))
3264 ((eq window old-window
)
3265 (when (= count old-count
)
3266 ;; Keep out of infinite loops. When COUNT has not changed
3267 ;; since we last looked at `window' we're probably in one.
3269 ((window-parameter window
'no-other-window
)
3271 ;; The first non-selectable window `previous-window' got
3272 ;; us: Remember it and the current value of COUNT.
3273 (setq old-window window
)
3274 (setq old-count count
)))
3276 (setq count
(1+ count
)))))
3278 (select-window window
)
3279 ;; Always return nil.
3282 ;; This should probably return non-nil when the selected window is part
3283 ;; of an atomic window whose root is the frame's root window.
3284 (defun one-window-p (&optional nomini all-frames
)
3285 "Return non-nil if the selected window is the only window.
3286 Optional arg NOMINI non-nil means don't count the minibuffer
3287 even if it is active. Otherwise, the minibuffer is counted
3290 Optional argument ALL-FRAMES specifies the set of frames to
3291 consider, see also `next-window'. ALL-FRAMES nil or omitted
3292 means consider windows on the selected frame only, plus the
3293 minibuffer window if specified by the NOMINI argument. If the
3294 minibuffer counts, consider all windows on all frames that share
3295 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3296 with a special meaning are:
3298 - t means consider all windows on all existing frames.
3300 - `visible' means consider all windows on all visible frames on
3301 the current terminal.
3303 - 0 (the number zero) means consider all windows on all visible
3304 and iconified frames on the current terminal.
3306 - A frame means consider all windows on that frame only.
3308 Anything else means consider all windows on the selected frame
3310 (let ((base-window (selected-window)))
3311 (if (and nomini
(eq base-window
(minibuffer-window)))
3312 (setq base-window
(next-window base-window
)))
3314 (next-window base-window
(if nomini
'arg
) all-frames
))))
3316 ;;; Deleting windows.
3317 (defun window-deletable-p (&optional window
)
3318 "Return t if WINDOW can be safely deleted from its frame.
3319 WINDOW must be a valid window and defaults to the selected one.
3320 Return 'frame if deleting WINDOW should also delete its frame."
3321 (setq window
(window-normalize-window window
))
3323 (unless (or ignore-window-parameters
3324 (eq (window-parameter window
'delete-window
) t
))
3325 ;; Handle atomicity.
3326 (when (window-parameter window
'window-atom
)
3327 (setq window
(window-atom-root window
))))
3329 (let ((frame (window-frame window
)))
3331 ((frame-root-window-p window
)
3332 ;; WINDOW's frame can be deleted only if there are other frames
3333 ;; on the same terminal, and it does not contain the active
3335 (unless (or (eq frame
(next-frame frame
0))
3336 ;; We can delete our frame only if no other frame
3337 ;; currently uses our minibuffer window.
3339 (dolist (other (frame-list))
3340 (when (and (not (eq other frame
))
3341 (eq (window-frame (minibuffer-window other
))
3344 (let ((minibuf (active-minibuffer-window)))
3345 (and minibuf
(eq frame
(window-frame minibuf
)))))
3347 ((or ignore-window-parameters
3348 (not (eq window
(window--major-non-side-window frame
))))
3349 ;; WINDOW can be deleted unless it is the major non-side window of
3353 (defun window--in-subtree-p (window root
)
3354 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
3355 (or (eq window root
)
3356 (let ((parent (window-parent window
)))
3359 (if (eq parent root
)
3361 (setq parent
(window-parent parent
))))))))
3363 (defun delete-window (&optional window
)
3365 WINDOW must be a valid window and defaults to the selected one.
3368 If the variable `ignore-window-parameters' is non-nil or the
3369 `delete-window' parameter of WINDOW equals t, do not process any
3370 parameters of WINDOW. Otherwise, if the `delete-window'
3371 parameter of WINDOW specifies a function, call that function with
3372 WINDOW as its sole argument and return the value returned by that
3375 Otherwise, if WINDOW is part of an atomic window, call
3376 `delete-window' with the root of the atomic window as its
3377 argument. Signal an error if WINDOW is either the only window on
3378 its frame, the last non-side window, or part of an atomic window
3379 that is its frame's root window."
3381 (setq window
(window-normalize-window window
))
3382 (let* ((frame (window-frame window
))
3383 (function (window-parameter window
'delete-window
))
3384 (parent (window-parent window
))
3386 (window--check frame
)
3388 ;; Handle window parameters.
3390 ;; Ignore window parameters if `ignore-window-parameters' tells
3391 ;; us so or `delete-window' equals t.
3392 ((or ignore-window-parameters
(eq function t
)))
3393 ((functionp function
)
3394 ;; The `delete-window' parameter specifies the function to call.
3395 ;; If that function is `ignore' nothing is done. It's up to the
3396 ;; function called here to avoid infinite recursion.
3397 (throw 'done
(funcall function window
)))
3398 ((and (window-parameter window
'window-atom
)
3399 (setq atom-root
(window-atom-root window
))
3400 (not (eq atom-root window
)))
3401 (if (eq atom-root
(frame-root-window frame
))
3402 (error "Root of atomic window is root window of its frame")
3403 (throw 'done
(delete-window atom-root
))))
3405 (error "Attempt to delete minibuffer or sole ordinary window"))
3406 ((eq window
(window--major-non-side-window frame
))
3407 (error "Attempt to delete last non-side window")))
3409 (let* ((horizontal (window-left-child parent
))
3410 (size (window-size window horizontal t
))
3412 (window--in-subtree-p (frame-selected-window frame
) window
))
3413 ;; Emacs 23 preferably gives WINDOW's space to its left
3415 (sibling (or (window-left window
) (window-right window
))))
3416 (window--resize-reset frame horizontal
)
3418 ((and (not window-combination-resize
)
3419 sibling
(window-sizable-p sibling size horizontal nil t
))
3420 ;; Resize WINDOW's sibling.
3421 (window--resize-this-window sibling size horizontal nil t
)
3422 (set-window-new-normal
3423 sibling
(+ (window-normal-size sibling horizontal
)
3424 (window-normal-size window horizontal
))))
3425 ((window--resizable-p window
(- size
) horizontal nil nil nil t t
)
3426 ;; Can do without resizing fixed-size windows.
3427 (window--resize-siblings window
(- size
) horizontal
))
3429 ;; Can't do without resizing fixed-size windows.
3430 (window--resize-siblings window
(- size
) horizontal t
)))
3431 ;; Actually delete WINDOW.
3432 (delete-window-internal window
)
3433 (window--pixel-to-total frame horizontal
)
3434 (when (and frame-selected
3436 (frame-selected-window frame
) 'no-other-window
))
3437 ;; `delete-window-internal' has selected a window that should
3438 ;; not be selected, fix this here.
3439 (other-window -
1 frame
))
3440 (run-window-configuration-change-hook frame
)
3441 (window--check frame
)
3442 ;; Always return nil.
3445 (defun delete-other-windows (&optional window
)
3446 "Make WINDOW fill its frame.
3447 WINDOW must be a valid window and defaults to the selected one.
3450 If the variable `ignore-window-parameters' is non-nil or the
3451 `delete-other-windows' parameter of WINDOW equals t, do not
3452 process any parameters of WINDOW. Otherwise, if the
3453 `delete-other-windows' parameter of WINDOW specifies a function,
3454 call that function with WINDOW as its sole argument and return
3455 the value returned by that function.
3457 Otherwise, if WINDOW is part of an atomic window, call this
3458 function with the root of the atomic window as its argument. If
3459 WINDOW is a non-side window, make WINDOW the only non-side window
3460 on the frame. Side windows are not deleted. If WINDOW is a side
3461 window signal an error."
3463 (setq window
(window-normalize-window window
))
3464 (let* ((frame (window-frame window
))
3465 (function (window-parameter window
'delete-other-windows
))
3466 (window-side (window-parameter window
'window-side
))
3467 atom-root side-main
)
3468 (window--check frame
)
3471 ;; Ignore window parameters if `ignore-window-parameters' is t or
3472 ;; `delete-other-windows' is t.
3473 ((or ignore-window-parameters
(eq function t
)))
3474 ((functionp function
)
3475 ;; The `delete-other-windows' parameter specifies the function
3476 ;; to call. If the function is `ignore' no windows are deleted.
3477 ;; It's up to the function called to avoid infinite recursion.
3478 (throw 'done
(funcall function window
)))
3479 ((and (window-parameter window
'window-atom
)
3480 (setq atom-root
(window-atom-root window
))
3481 (not (eq atom-root window
)))
3482 (if (eq atom-root
(frame-root-window frame
))
3483 (error "Root of atomic window is root window of its frame")
3484 (throw 'done
(delete-other-windows atom-root
))))
3485 ((memq window-side window-sides
)
3486 (error "Cannot make side window the only window"))
3487 ((and (window-minibuffer-p window
)
3488 (not (eq window
(frame-root-window window
))))
3489 (error "Can't expand minibuffer to full frame")))
3491 ;; If WINDOW is the major non-side window, do nothing.
3492 (if (window-with-parameter 'window-side
)
3493 (setq side-main
(window--major-non-side-window frame
))
3494 (setq side-main
(frame-root-window frame
)))
3495 (unless (eq window side-main
)
3496 (delete-other-windows-internal window side-main
)
3497 (run-window-configuration-change-hook frame
)
3498 (window--check frame
))
3499 ;; Always return nil.
3502 (defun delete-other-windows-vertically (&optional window
)
3503 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
3504 This may be a useful alternative binding for \\[delete-other-windows]
3505 if you often split windows horizontally."
3507 (let* ((window (or window
(selected-window)))
3508 (edges (window-edges window
))
3510 (while (not (eq (setq w
(next-window w
1)) window
))
3511 (let ((e (window-edges w
)))
3512 (when (and (= (car e
) (car edges
))
3513 (= (nth 2 e
) (nth 2 edges
)))
3515 (mapc 'delete-window delenda
)))
3517 ;;; Windows and buffers.
3519 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
3520 ;; for (1) determining which buffer to show in the window when its
3521 ;; buffer shall be buried or killed and (2) which buffer to show for
3522 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3524 ;; `prev-buffers' consists of <buffer, window-start, window-point>
3525 ;; triples. The entries on this list are ordered by the time their
3526 ;; buffer has been removed from the window, the most recently removed
3527 ;; buffer's entry being first. The window-start and window-point
3528 ;; components are `window-start' and `window-point' at the time the
3529 ;; buffer was removed from the window which implies that the entry must
3530 ;; be added when `set-window-buffer' removes the buffer from the window.
3532 ;; `next-buffers' is the list of buffers that have been replaced
3533 ;; recently by `switch-to-prev-buffer'. These buffers are the least
3534 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
3535 ;; candidates of `switch-to-next-buffer' to switch to. This list is
3536 ;; reset to nil by any action changing the window's buffer with the
3537 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
3538 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
3539 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
3541 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
3542 ;; if such a buffer was killed while the window was hidden within a
3543 ;; window configuration. Such killed buffers get removed whenever
3544 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
3546 ;; The following function is called by `set-window-buffer' _before_ it
3547 ;; replaces the buffer of the argument window with the new buffer.
3548 (defun record-window-buffer (&optional window
)
3549 "Record WINDOW's buffer.
3550 WINDOW must be a live window and defaults to the selected one."
3551 (let* ((window (window-normalize-window window t
))
3552 (buffer (window-buffer window
))
3553 (entry (assq buffer
(window-prev-buffers window
))))
3554 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
3555 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3556 (set-window-next-buffers window nil
)
3559 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
3560 (set-window-prev-buffers
3561 window
(assq-delete-all buffer
(window-prev-buffers window
))))
3563 ;; Don't record insignificant buffers.
3564 (unless (eq (aref (buffer-name buffer
) 0) ?\s
)
3565 ;; Add an entry for buffer to WINDOW's previous buffers.
3566 (with-current-buffer buffer
3567 (let ((start (window-start window
))
3568 (point (window-point window
)))
3572 ;; We have an entry, update marker positions.
3573 (list (set-marker (nth 1 entry
) start
)
3574 (set-marker (nth 2 entry
) point
))
3575 ;; Make new markers.
3576 (list (copy-marker start
)
3578 ;; Preserve window-point-insertion-type
3580 point window-point-insertion-type
)))))
3581 (set-window-prev-buffers
3582 window
(cons entry
(window-prev-buffers window
)))))
3584 (run-hooks 'buffer-list-update-hook
))))
3586 (defun unrecord-window-buffer (&optional window buffer
)
3587 "Unrecord BUFFER in WINDOW.
3588 WINDOW must be a live window and defaults to the selected one.
3589 BUFFER must be a live buffer and defaults to the buffer of
3591 (let* ((window (window-normalize-window window t
))
3592 (buffer (or buffer
(window-buffer window
))))
3593 (set-window-prev-buffers
3594 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3595 (set-window-next-buffers
3596 window
(delq buffer
(window-next-buffers window
)))))
3598 (defun set-window-buffer-start-and-point (window buffer
&optional start point
)
3599 "Set WINDOW's buffer to BUFFER.
3600 WINDOW must be a live window and defaults to the selected one.
3601 Optional argument START non-nil means set WINDOW's start position
3602 to START. Optional argument POINT non-nil means set WINDOW's
3603 point to POINT. If WINDOW is selected this also sets BUFFER's
3604 `point' to POINT. If WINDOW is selected and the buffer it showed
3605 before was current this also makes BUFFER the current buffer."
3606 (setq window
(window-normalize-window window t
))
3607 (let ((selected (eq window
(selected-window)))
3608 (current (eq (window-buffer window
) (current-buffer))))
3609 (set-window-buffer window buffer
)
3610 (when (and selected current
)
3611 (set-buffer buffer
))
3613 ;; Don't force window-start here (even if POINT is nil).
3614 (set-window-start window start t
))
3616 (set-window-point window point
))))
3618 (defcustom switch-to-visible-buffer t
3619 "If non-nil, allow switching to an already visible buffer.
3620 If this variable is non-nil, `switch-to-prev-buffer' and
3621 `switch-to-next-buffer' may switch to an already visible buffer
3622 provided the buffer was shown before in the window specified as
3623 argument to those functions. If this variable is nil,
3624 `switch-to-prev-buffer' and `switch-to-next-buffer' always try to
3625 avoid switching to a buffer that is already visible in another
3626 window on the same frame."
3631 (defun switch-to-prev-buffer (&optional window bury-or-kill
)
3632 "In WINDOW switch to previous buffer.
3633 WINDOW must be a live window and defaults to the selected one.
3634 Return the buffer switched to, nil if no suitable buffer could be
3637 Optional argument BURY-OR-KILL non-nil means the buffer currently
3638 shown in WINDOW is about to be buried or killed and consequently
3639 shall not be switched to in future invocations of this command.
3641 As a special case, if BURY-OR-KILL equals `append', this means to
3642 move the buffer to the end of WINDOW's previous buffers list so a
3643 future invocation of `switch-to-prev-buffer' less likely switches
3646 (let* ((window (window-normalize-window window t
))
3647 (frame (window-frame window
))
3648 (old-buffer (window-buffer window
))
3649 ;; Save this since it's destroyed by `set-window-buffer'.
3650 (next-buffers (window-next-buffers window
))
3651 (pred (frame-parameter frame
'buffer-predicate
))
3652 entry new-buffer killed-buffers visible
)
3653 (when (window-minibuffer-p window
)
3654 ;; Don't switch in minibuffer window.
3655 (unless (setq window
(minibuffer-selected-window))
3656 (error "Window %s is a minibuffer window" window
)))
3658 (when (window-dedicated-p window
)
3659 ;; Don't switch in dedicated window.
3660 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3663 ;; Scan WINDOW's previous buffers first, skipping entries of next
3665 (dolist (entry (window-prev-buffers window
))
3666 (when (and (setq new-buffer
(car entry
))
3667 (or (buffer-live-p new-buffer
)
3668 (not (setq killed-buffers
3669 (cons new-buffer killed-buffers
))))
3670 (not (eq new-buffer old-buffer
))
3671 (or (null pred
) (funcall pred new-buffer
))
3672 ;; When BURY-OR-KILL is nil, avoid switching to a
3673 ;; buffer in WINDOW's next buffers list.
3674 (or bury-or-kill
(not (memq new-buffer next-buffers
))))
3675 (if (and (not switch-to-visible-buffer
)
3676 (get-buffer-window new-buffer frame
))
3677 ;; Try to avoid showing a buffer visible in some other
3679 (setq visible new-buffer
)
3680 (set-window-buffer-start-and-point
3681 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3683 ;; Scan reverted buffer list of WINDOW's frame next, skipping
3684 ;; entries of next buffers. Note that when we bury or kill a
3685 ;; buffer we don't reverse the global buffer list to avoid showing
3686 ;; a buried buffer instead. Otherwise, we must reverse the global
3687 ;; buffer list in order to make sure that switching to the
3688 ;; previous/next buffer traverse it in opposite directions.
3689 (dolist (buffer (if bury-or-kill
3691 (nreverse (buffer-list frame
))))
3692 (when (and (buffer-live-p buffer
)
3693 (not (eq buffer old-buffer
))
3694 (or (null pred
) (funcall pred buffer
))
3695 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3696 (or bury-or-kill
(not (memq buffer next-buffers
))))
3697 (if (get-buffer-window buffer frame
)
3698 ;; Try to avoid showing a buffer visible in some other window.
3700 (setq visible buffer
))
3701 (setq new-buffer buffer
)
3702 (set-window-buffer-start-and-point window new-buffer
)
3704 (unless bury-or-kill
3705 ;; Scan reverted next buffers last (must not use nreverse
3707 (dolist (buffer (reverse next-buffers
))
3708 ;; Actually, buffer _must_ be live here since otherwise it
3709 ;; would have been caught in the scan of previous buffers.
3710 (when (and (or (buffer-live-p buffer
)
3711 (not (setq killed-buffers
3712 (cons buffer killed-buffers
))))
3713 (not (eq buffer old-buffer
))
3714 (or (null pred
) (funcall pred buffer
))
3715 (setq entry
(assq buffer
(window-prev-buffers window
))))
3716 (setq new-buffer buffer
)
3717 (set-window-buffer-start-and-point
3718 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3721 ;; Show a buffer visible in another window.
3723 (setq new-buffer visible
)
3724 (set-window-buffer-start-and-point window new-buffer
)))
3727 (let ((entry (and (eq bury-or-kill
'append
)
3728 (assq old-buffer
(window-prev-buffers window
)))))
3729 ;; Remove `old-buffer' from WINDOW's previous and (restored list
3730 ;; of) next buffers.
3731 (set-window-prev-buffers
3732 window
(assq-delete-all old-buffer
(window-prev-buffers window
)))
3733 (set-window-next-buffers window
(delq old-buffer next-buffers
))
3735 ;; Append old-buffer's entry to list of WINDOW's previous
3736 ;; buffers so it's less likely to get switched to soon but
3737 ;; `display-buffer-in-previous-window' can nevertheless find
3739 (set-window-prev-buffers
3740 window
(append (window-prev-buffers window
) (list entry
)))))
3741 ;; Move `old-buffer' to head of WINDOW's restored list of next
3743 (set-window-next-buffers
3744 window
(cons old-buffer
(delq old-buffer next-buffers
))))
3746 ;; Remove killed buffers from WINDOW's previous and next buffers.
3747 (when killed-buffers
3748 (dolist (buffer killed-buffers
)
3749 (set-window-prev-buffers
3750 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3751 (set-window-next-buffers
3752 window
(delq buffer
(window-next-buffers window
)))))
3754 ;; Return new-buffer.
3757 (defun switch-to-next-buffer (&optional window
)
3758 "In WINDOW switch to next buffer.
3759 WINDOW must be a live window and defaults to the selected one.
3760 Return the buffer switched to, nil if no suitable buffer could be
3763 (let* ((window (window-normalize-window window t
))
3764 (frame (window-frame window
))
3765 (old-buffer (window-buffer window
))
3766 (next-buffers (window-next-buffers window
))
3767 (pred (frame-parameter frame
'buffer-predicate
))
3768 new-buffer entry killed-buffers visible
)
3769 (when (window-minibuffer-p window
)
3770 ;; Don't switch in minibuffer window.
3771 (unless (setq window
(minibuffer-selected-window))
3772 (error "Window %s is a minibuffer window" window
)))
3774 (when (window-dedicated-p window
)
3775 ;; Don't switch in dedicated window.
3776 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3779 ;; Scan WINDOW's next buffers first.
3780 (dolist (buffer next-buffers
)
3781 (when (and (or (buffer-live-p buffer
)
3782 (not (setq killed-buffers
3783 (cons buffer killed-buffers
))))
3784 (not (eq buffer old-buffer
))
3785 (or (null pred
) (funcall pred buffer
))
3786 (setq entry
(assq buffer
(window-prev-buffers window
))))
3787 (setq new-buffer buffer
)
3788 (set-window-buffer-start-and-point
3789 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3791 ;; Scan the buffer list of WINDOW's frame next, skipping previous
3793 (dolist (buffer (buffer-list frame
))
3794 (when (and (buffer-live-p buffer
)
3795 (not (eq buffer old-buffer
))
3796 (or (null pred
) (funcall pred buffer
))
3797 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3798 (not (assq buffer
(window-prev-buffers window
))))
3799 (if (get-buffer-window buffer frame
)
3800 ;; Try to avoid showing a buffer visible in some other window.
3801 (setq visible buffer
)
3802 (setq new-buffer buffer
)
3803 (set-window-buffer-start-and-point window new-buffer
)
3805 ;; Scan WINDOW's reverted previous buffers last (must not use
3807 (dolist (entry (reverse (window-prev-buffers window
)))
3808 (when (and (setq new-buffer
(car entry
))
3809 (or (buffer-live-p new-buffer
)
3810 (not (setq killed-buffers
3811 (cons new-buffer killed-buffers
))))
3812 (not (eq new-buffer old-buffer
))
3813 (or (null pred
) (funcall pred new-buffer
)))
3814 (if (and (not switch-to-visible-buffer
)
3815 (get-buffer-window new-buffer frame
))
3816 ;; Try to avoid showing a buffer visible in some other window.
3818 (setq visible new-buffer
))
3819 (set-window-buffer-start-and-point
3820 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3823 ;; Show a buffer visible in another window.
3825 (setq new-buffer visible
)
3826 (set-window-buffer-start-and-point window new-buffer
)))
3828 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
3829 (set-window-next-buffers window
(delq new-buffer next-buffers
))
3831 ;; Remove killed buffers from WINDOW's previous and next buffers.
3832 (when killed-buffers
3833 (dolist (buffer killed-buffers
)
3834 (set-window-prev-buffers
3835 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3836 (set-window-next-buffers
3837 window
(delq buffer
(window-next-buffers window
)))))
3839 ;; Return new-buffer.
3842 (defun get-next-valid-buffer (list &optional buffer visible-ok frame
)
3843 "Search LIST for a valid buffer to display in FRAME.
3844 Return nil when all buffers in LIST are undesirable for display,
3845 otherwise return the first suitable buffer in LIST.
3847 Buffers not visible in windows are preferred to visible buffers,
3848 unless VISIBLE-OK is non-nil.
3849 If the optional argument FRAME is nil, it defaults to the selected frame.
3850 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
3851 ;; This logic is more or less copied from other-buffer.
3852 (setq frame
(or frame
(selected-frame)))
3853 (let ((pred (frame-parameter frame
'buffer-predicate
))
3855 (while (and (not found
) list
)
3856 (setq buf
(car list
))
3857 (if (and (not (eq buffer buf
))
3859 (or (null pred
) (funcall pred buf
))
3860 (not (eq (aref (buffer-name buf
) 0) ?\s
))
3861 (or visible-ok
(null (get-buffer-window buf
'visible
))))
3863 (setq list
(cdr list
))))
3866 (defun last-buffer (&optional buffer visible-ok frame
)
3867 "Return the last buffer in FRAME's buffer list.
3868 If BUFFER is the last buffer, return the preceding buffer
3869 instead. Buffers not visible in windows are preferred to visible
3870 buffers, unless optional argument VISIBLE-OK is non-nil.
3871 Optional third argument FRAME nil or omitted means use the
3872 selected frame's buffer list. If no such buffer exists, return
3873 the buffer `*scratch*', creating it if necessary."
3874 (setq frame
(or frame
(selected-frame)))
3875 (or (get-next-valid-buffer (nreverse (buffer-list frame
))
3876 buffer visible-ok frame
)
3877 (get-buffer "*scratch*")
3878 (let ((scratch (get-buffer-create "*scratch*")))
3879 (set-buffer-major-mode scratch
)
3882 (defcustom frame-auto-hide-function
#'iconify-frame
3883 "Function called to automatically hide frames.
3884 The function is called with one argument - a frame.
3886 Functions affected by this option are those that bury a buffer
3887 shown in a separate frame like `quit-window' and `bury-buffer'."
3888 :type
'(choice (const :tag
"Iconify" iconify-frame
)
3889 (const :tag
"Delete" delete-frame
)
3890 (const :tag
"Do nothing" ignore
)
3896 (defun window--delete (&optional window dedicated-only kill
)
3897 "Delete WINDOW if possible.
3898 WINDOW must be a live window and defaults to the selected one.
3899 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
3900 only if it's dedicated to its buffer. Optional argument KILL
3901 means the buffer shown in window will be killed. Return non-nil
3902 if WINDOW gets deleted or its frame is auto-hidden."
3903 (setq window
(window-normalize-window window t
))
3904 (unless (and dedicated-only
(not (window-dedicated-p window
)))
3905 (let ((deletable (window-deletable-p window
)))
3907 ((eq deletable
'frame
)
3908 (let ((frame (window-frame window
)))
3911 (delete-frame frame
))
3912 ((functionp frame-auto-hide-function
)
3913 (funcall frame-auto-hide-function frame
))))
3916 (delete-window window
)
3919 (defun bury-buffer (&optional buffer-or-name
)
3920 "Put BUFFER-OR-NAME at the end of the list of all buffers.
3921 There it is the least likely candidate for `other-buffer' to
3922 return; thus, the least likely buffer for \\[switch-to-buffer] to
3925 You can specify a buffer name as BUFFER-OR-NAME, or an actual
3926 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
3927 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
3928 remove the current buffer from the selected window if it is
3931 (let* ((buffer (window-normalize-buffer buffer-or-name
)))
3932 ;; If `buffer-or-name' is not on the selected frame we unrecord it
3933 ;; although it's not "here" (call it a feature).
3934 (bury-buffer-internal buffer
)
3935 ;; Handle case where `buffer-or-name' is nil and the current buffer
3936 ;; is shown in the selected window.
3938 ((or buffer-or-name
(not (eq buffer
(window-buffer)))))
3939 ((window--delete nil t
))
3941 ;; Switch to another buffer in window.
3942 (set-window-dedicated-p nil nil
)
3943 (switch-to-prev-buffer nil
'bury
)))
3945 ;; Always return nil.
3948 (defun unbury-buffer ()
3949 "Switch to the last buffer in the buffer list."
3951 (switch-to-buffer (last-buffer)))
3953 (defun next-buffer ()
3954 "In selected window switch to next buffer."
3957 ((window-minibuffer-p)
3958 (error "Cannot switch buffers in minibuffer window"))
3959 ((eq (window-dedicated-p) t
)
3960 (error "Window is strongly dedicated to its buffer"))
3962 (switch-to-next-buffer))))
3964 (defun previous-buffer ()
3965 "In selected window switch to previous buffer."
3968 ((window-minibuffer-p)
3969 (error "Cannot switch buffers in minibuffer window"))
3970 ((eq (window-dedicated-p) t
)
3971 (error "Window is strongly dedicated to its buffer"))
3973 (switch-to-prev-buffer))))
3975 (defun delete-windows-on (&optional buffer-or-name frame
)
3976 "Delete all windows showing BUFFER-OR-NAME.
3977 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3978 and defaults to the current buffer.
3980 The following non-nil values of the optional argument FRAME
3981 have special meanings:
3983 - t means consider all windows on the selected frame only.
3985 - `visible' means consider all windows on all visible frames on
3986 the current terminal.
3988 - 0 (the number zero) means consider all windows on all visible
3989 and iconified frames on the current terminal.
3991 - A frame means consider all windows on that frame only.
3993 Any other value of FRAME means consider all windows on all
3996 When a window showing BUFFER-OR-NAME is dedicated and the only
3997 window of its frame, that frame is deleted when there are other
3999 (interactive "BDelete windows on (buffer):\nP")
4000 (let ((buffer (window-normalize-buffer buffer-or-name
))
4001 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4002 ;; `window-list-1' based function.
4003 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
4004 (dolist (window (window-list-1 nil nil all-frames
))
4005 (if (eq (window-buffer window
) buffer
)
4006 (let ((deletable (window-deletable-p window
)))
4008 ((and (eq deletable
'frame
) (window-dedicated-p window
))
4009 ;; Delete frame if and only if window is dedicated.
4010 (delete-frame (window-frame window
)))
4013 (delete-window window
))
4015 ;; In window switch to previous buffer.
4016 (set-window-dedicated-p window nil
)
4017 (switch-to-prev-buffer window
'bury
))))
4018 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4019 (unrecord-window-buffer window buffer
)))))
4021 (defun replace-buffer-in-windows (&optional buffer-or-name
)
4022 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
4023 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4024 and defaults to the current buffer.
4026 When a window showing BUFFER-OR-NAME is dedicated, that window is
4027 deleted. If that window is the only window on its frame, the
4028 frame is deleted too when there are other frames left. If there
4029 are no other frames left, some other buffer is displayed in that
4032 This function removes the buffer denoted by BUFFER-OR-NAME from
4033 all window-local buffer lists."
4034 (interactive "bBuffer to replace: ")
4035 (let ((buffer (window-normalize-buffer buffer-or-name
)))
4036 (dolist (window (window-list-1 nil nil t
))
4037 (if (eq (window-buffer window
) buffer
)
4038 (unless (window--delete window t t
)
4039 ;; Switch to another buffer in window.
4040 (set-window-dedicated-p window nil
)
4041 (switch-to-prev-buffer window
'kill
))
4042 ;; Unrecord BUFFER in WINDOW.
4043 (unrecord-window-buffer window buffer
)))))
4045 (defun quit-restore-window (&optional window bury-or-kill
)
4046 "Quit WINDOW and deal with its buffer.
4047 WINDOW must be a live window and defaults to the selected one.
4049 According to information stored in WINDOW's `quit-restore' window
4050 parameter either (1) delete WINDOW and its frame, (2) delete
4051 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4052 or (4) make WINDOW display some other buffer than the present
4053 one. If non-nil, reset `quit-restore' parameter to nil.
4055 Optional second argument BURY-OR-KILL tells how to proceed with
4056 the buffer of WINDOW. The following values are handled:
4058 `nil' means to not handle the buffer in a particular way. This
4059 means that if WINDOW is not deleted by this function, invoking
4060 `switch-to-prev-buffer' will usually show the buffer again.
4062 `append' means that if WINDOW is not deleted, move its buffer to
4063 the end of WINDOW's previous buffers so it's less likely that a
4064 future invocation of `switch-to-prev-buffer' will switch to it.
4065 Also, move the buffer to the end of the frame's buffer list.
4067 `bury' means that if WINDOW is not deleted, remove its buffer
4068 from WINDOW'S list of previous buffers. Also, move the buffer
4069 to the end of the frame's buffer list. This value provides the
4070 most reliable remedy to not have `switch-to-prev-buffer' switch
4071 to this buffer again without killing the buffer.
4073 `kill' means to kill WINDOW's buffer."
4074 (setq window
(window-normalize-window window t
))
4075 (let* ((buffer (window-buffer window
))
4076 (quit-restore (window-parameter window
'quit-restore
))
4078 (let* ((prev-buffers (window-prev-buffers window
))
4079 (prev-buffer (caar prev-buffers
)))
4080 (and (or (not (eq prev-buffer buffer
))
4081 (and (cdr prev-buffers
)
4082 (not (eq (setq prev-buffer
(cadr prev-buffers
))
4087 ((and (not prev-buffer
)
4088 (or (eq (nth 1 quit-restore
) 'frame
)
4089 (and (eq (nth 1 quit-restore
) 'window
)
4090 ;; If the window has been created on an existing
4091 ;; frame and ended up as the sole window on that
4092 ;; frame, do not delete it (Bug#12764).
4093 (not (eq window
(frame-root-window window
)))))
4094 (eq (nth 3 quit-restore
) buffer
)
4095 ;; Delete WINDOW if possible.
4096 (window--delete window nil
(eq bury-or-kill
'kill
)))
4097 ;; If the previously selected window is still alive, select it.
4098 (when (window-live-p (nth 2 quit-restore
))
4099 (select-window (nth 2 quit-restore
))))
4100 ((and (listp (setq quad
(nth 1 quit-restore
)))
4101 (buffer-live-p (car quad
))
4102 (eq (nth 3 quit-restore
) buffer
))
4103 ;; Show another buffer stored in quit-restore parameter.
4104 (when (and (integerp (nth 3 quad
))
4105 (/= (nth 3 quad
) (window-total-height window
)))
4106 ;; Try to resize WINDOW to its old height but don't signal an
4109 (window-resize window
(- (nth 3 quad
) (window-total-height window
)))
4111 (set-window-dedicated-p window nil
)
4112 ;; Restore WINDOW's previous buffer, start and point position.
4113 (set-window-buffer-start-and-point
4114 window
(nth 0 quad
) (nth 1 quad
) (nth 2 quad
))
4115 ;; Deal with the buffer we just removed from WINDOW.
4116 (setq entry
(and (eq bury-or-kill
'append
)
4117 (assq buffer
(window-prev-buffers window
))))
4119 ;; Remove buffer from WINDOW's previous and next buffers.
4120 (set-window-prev-buffers
4121 window
(assq-delete-all buffer
(window-prev-buffers window
)))
4122 (set-window-next-buffers
4123 window
(delq buffer
(window-next-buffers window
))))
4125 ;; Append old buffer's entry to list of WINDOW's previous
4126 ;; buffers so it's less likely to get switched to soon but
4127 ;; `display-buffer-in-previous-window' can nevertheless find it.
4128 (set-window-prev-buffers
4129 window
(append (window-prev-buffers window
) (list entry
))))
4130 ;; Reset the quit-restore parameter.
4131 (set-window-parameter window
'quit-restore nil
)
4132 ;; Select old window.
4133 (when (window-live-p (nth 2 quit-restore
))
4134 (select-window (nth 2 quit-restore
))))
4136 ;; Show some other buffer in WINDOW and reset the quit-restore
4138 (set-window-parameter window
'quit-restore nil
)
4139 ;; Make sure that WINDOW is no more dedicated.
4140 (set-window-dedicated-p window nil
)
4141 (switch-to-prev-buffer window bury-or-kill
)))
4143 ;; Deal with the buffer.
4145 ((not (buffer-live-p buffer
)))
4146 ((eq bury-or-kill
'kill
)
4147 (kill-buffer buffer
))
4149 (bury-buffer-internal buffer
)))))
4151 (defun quit-window (&optional kill window
)
4152 "Quit WINDOW and bury its buffer.
4153 WINDOW must be a live window and defaults to the selected one.
4154 With prefix argument KILL non-nil, kill the buffer instead of
4157 According to information stored in WINDOW's `quit-restore' window
4158 parameter either (1) delete WINDOW and its frame, (2) delete
4159 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4160 or (4) make WINDOW display some other buffer than the present
4161 one. If non-nil, reset `quit-restore' parameter to nil."
4163 (quit-restore-window window
(if kill
'kill
'bury
)))
4165 (defun quit-windows-on (&optional buffer-or-name kill frame
)
4166 "Quit all windows showing BUFFER-OR-NAME.
4167 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4168 and defaults to the current buffer. Optional argument KILL
4169 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4170 BUFFER-OR-NAME. Optional argument FRAME is handled as by
4171 `delete-windows-on'.
4173 This function calls `quit-window' on all candidate windows
4174 showing BUFFER-OR-NAME."
4175 (interactive "BQuit windows on (buffer):\nP")
4176 (let ((buffer (window-normalize-buffer buffer-or-name
))
4177 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4178 ;; `window-list-1' based function.
4179 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
4180 (dolist (window (window-list-1 nil nil all-frames
))
4181 (if (eq (window-buffer window
) buffer
)
4182 (quit-window kill window
)
4183 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4184 (unrecord-window-buffer window buffer
)))))
4186 ;;; Splitting windows.
4187 (defun window-split-min-size (&optional horizontal pixelwise
)
4188 "Return minimum height of any window when splitting windows.
4189 Optional argument HORIZONTAL non-nil means return minimum width."
4193 (window-min-pixel-width)
4194 (window-min-pixel-height)))
4196 (max window-min-width window-safe-min-width
))
4198 (max window-min-height window-safe-min-height
))))
4200 (defun split-window (&optional window size side pixelwise
)
4201 "Make a new window adjacent to WINDOW.
4202 WINDOW must be a valid window and defaults to the selected one.
4203 Return the new window which is always a live window.
4205 Optional argument SIZE a positive number means make WINDOW SIZE
4206 lines or columns tall. If SIZE is negative, make the new window
4207 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
4208 absolute value can be less than `window-min-height' or
4209 `window-min-width'; so this command can make a new window as
4210 small as one line or two columns. SIZE defaults to half of
4213 Optional third argument SIDE nil (or `below') specifies that the
4214 new window shall be located below WINDOW. SIDE `above' means the
4215 new window shall be located above WINDOW. In both cases SIZE
4216 specifies the new number of lines for WINDOW (or the new window
4217 if SIZE is negative) including space reserved for the mode and/or
4220 SIDE t (or `right') specifies that the new window shall be
4221 located on the right side of WINDOW. SIDE `left' means the new
4222 window shall be located on the left of WINDOW. In both cases
4223 SIZE specifies the new number of columns for WINDOW (or the new
4224 window provided SIZE is negative) including space reserved for
4225 fringes and the scrollbar or a divider column. Any other non-nil
4226 value for SIDE is currently handled like t (or `right').
4228 PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
4230 If the variable `ignore-window-parameters' is non-nil or the
4231 `split-window' parameter of WINDOW equals t, do not process any
4232 parameters of WINDOW. Otherwise, if the `split-window' parameter
4233 of WINDOW specifies a function, call that function with all three
4234 arguments and return the value returned by that function.
4236 Otherwise, if WINDOW is part of an atomic window, \"split\" the
4237 root of that atomic window. The new window does not become a
4238 member of that atomic window.
4240 If WINDOW is live, properties of the new window like margins and
4241 scrollbars are inherited from WINDOW. If WINDOW is an internal
4242 window, these properties as well as the buffer displayed in the
4243 new window are inherited from the window selected on WINDOW's
4244 frame. The selected window is not changed by this function."
4245 (setq window
(window-normalize-window window
))
4248 ((memq side
'(below above right left
)) side
)
4250 (horizontal (not (memq side
'(below above
))))
4251 (frame (window-frame window
))
4252 (parent (window-parent window
))
4253 (function (window-parameter window
'split-window
))
4254 (window-side (window-parameter window
'window-side
))
4255 ;; Rebind the following two variables since in some cases we
4256 ;; have to override their value.
4257 (window-combination-limit window-combination-limit
)
4258 (window-combination-resize window-combination-resize
)
4259 (char-size (frame-char-size window horizontal
))
4261 (when (numberp size
)
4262 (window--size-to-pixel window size horizontal pixelwise t
)))
4264 (window--check frame
)
4267 ;; Ignore window parameters if either `ignore-window-parameters'
4268 ;; is t or the `split-window' parameter equals t.
4269 ((or ignore-window-parameters
(eq function t
)))
4270 ((functionp function
)
4271 ;; The `split-window' parameter specifies the function to call.
4272 ;; If that function is `ignore', do nothing.
4273 (throw 'done
(funcall function window size side
)))
4274 ;; If WINDOW is part of an atomic window, split the root window
4275 ;; of that atomic window instead.
4276 ((and (window-parameter window
'window-atom
)
4277 (setq atom-root
(window-atom-root window
))
4278 (not (eq atom-root window
)))
4279 (throw 'done
(split-window atom-root size side pixelwise
)))
4280 ;; If WINDOW is a side window or its first or last child is a
4281 ;; side window, throw an error unless `window-combination-resize'
4283 ((and (not (eq window-combination-resize
'side
))
4284 (or (window-parameter window
'window-side
)
4285 (and (window-child window
)
4286 (or (window-parameter
4287 (window-child window
) 'window-side
)
4289 (window-last-child window
) 'window-side
)))))
4290 (error "Cannot split side window or parent of side window"))
4291 ;; If `window-combination-resize' is 'side and window has a side
4292 ;; window sibling, bind `window-combination-limit' to t.
4293 ((and (not (eq window-combination-resize
'side
))
4294 (or (and (window-prev-sibling window
)
4296 (window-prev-sibling window
) 'window-side
))
4297 (and (window-next-sibling window
)
4299 (window-next-sibling window
) 'window-side
))))
4300 (setq window-combination-limit t
)))
4302 ;; If `window-combination-resize' is t and SIZE is non-negative,
4303 ;; bind `window-combination-limit' to t.
4304 (when (and (eq window-combination-resize t
)
4305 pixel-size
(> pixel-size
0))
4306 (setq window-combination-limit t
))
4308 (let* ((parent-pixel-size
4309 ;; `parent-pixel-size' is the pixel size of WINDOW's
4310 ;; parent, provided it has one.
4311 (when parent
(window-size parent horizontal t
)))
4312 ;; `resize' non-nil means we are supposed to resize other
4313 ;; windows in WINDOW's combination.
4315 (and window-combination-resize
4316 (or (window-parameter window
'window-side
)
4317 (not (eq window-combination-resize
'side
)))
4318 (not (eq window-combination-limit t
))
4319 ;; Resize makes sense in iso-combinations only.
4320 (window-combined-p window horizontal
)))
4321 ;; `old-pixel-size' is the current pixel size of WINDOW.
4322 (old-pixel-size (window-size window horizontal t
))
4323 ;; `new-size' is the specified or calculated size of the
4325 new-pixel-size new-parent new-normal
)
4328 (setq new-pixel-size
4330 ;; When resizing try to give the new window the
4331 ;; average size of a window in its combination.
4332 (min (- parent-pixel-size
4333 (window-min-size parent horizontal nil t
))
4334 (/ parent-pixel-size
4335 (1+ (window-combinations parent horizontal
))))
4336 ;; Else try to give the new window half the size
4337 ;; of WINDOW (plus an eventual odd pixel).
4338 (/ old-pixel-size
2)))
4339 (unless window-resize-pixelwise
4340 ;; Round to nearest char-size multiple.
4341 (setq new-pixel-size
4342 (* char-size
(round new-pixel-size char-size
)))))
4344 ;; SIZE non-negative specifies the new size of WINDOW.
4346 ;; Note: Specifying a non-negative SIZE is practically
4347 ;; always done as workaround for making the new window
4348 ;; appear above or on the left of the new window (the
4349 ;; ispell window is a typical example of that). In all
4350 ;; these cases the SIDE argument should be set to 'above
4351 ;; or 'left in order to support the 'resize option.
4352 ;; Here we have to nest the windows instead, see above.
4353 (setq new-pixel-size
(- old-pixel-size pixel-size
)))
4355 ;; SIZE negative specifies the size of the new window.
4356 (setq new-pixel-size
(- pixel-size
))))
4363 ;; SIZE unspecified, resizing.
4364 (when (and (not (window-sizable-p
4365 parent
(- new-pixel-size
) horizontal nil t
))
4366 ;; Try again with minimum split size.
4367 (setq new-pixel-size
4369 (window-split-min-size horizontal t
)))
4370 (not (window-sizable-p
4371 parent
(- new-pixel-size
) horizontal nil t
)))
4372 (error "Window %s too small for splitting 1" parent
)))
4373 ((> (+ new-pixel-size
(window-min-size window horizontal nil t
))
4375 ;; SIZE unspecified, no resizing.
4376 (error "Window %s too small for splitting 2" window
))))
4377 ((and (>= pixel-size
0)
4378 (or (>= pixel-size old-pixel-size
)
4380 (window-safe-min-pixel-size window horizontal
))))
4381 ;; SIZE specified as new size of old window. If the new size
4382 ;; is larger than the old size or the size of the new window
4383 ;; would be less than the safe minimum, signal an error.
4384 (error "Window %s too small for splitting 3" window
))
4386 ;; SIZE specified, resizing.
4387 (unless (window-sizable-p
4388 parent
(- new-pixel-size
) horizontal nil t
)
4389 ;; If we cannot resize the parent give up.
4390 (error "Window %s too small for splitting 4" parent
)))
4391 ((or (< new-pixel-size
4392 (window-safe-min-pixel-size window horizontal
))
4393 (< (- old-pixel-size new-pixel-size
)
4394 (window-safe-min-pixel-size window horizontal
)))
4395 ;; SIZE specification violates minimum size restrictions.
4396 (error "Window %s too small for splitting 5" window
)))
4398 (window--resize-reset frame horizontal
)
4401 ;; Make new-parent non-nil if we need a new parent window;
4402 ;; either because we want to nest or because WINDOW is not
4404 (or (eq window-combination-limit t
)
4405 (not (window-combined-p window horizontal
))))
4407 ;; Make new-normal the normal size of the new window.
4409 (pixel-size (/ (float new-pixel-size
)
4410 (if new-parent old-pixel-size parent-pixel-size
)))
4412 (resize (/ 1.0 (1+ (window-combinations parent horizontal
))))
4413 (t (/ (window-normal-size window horizontal
) 2.0))))
4416 ;; Try to get space from OLD's siblings. We could go "up" and
4417 ;; try getting additional space from surrounding windows but
4418 ;; we won't be able to return space to those windows when we
4419 ;; delete the one we create here. Hence we do not go up.
4421 (window--resize-child-windows
4422 parent
(- new-pixel-size
) horizontal
)
4423 (let* ((normal (- 1.0 new-normal
))
4424 (sub (window-child parent
)))
4426 (set-window-new-normal
4427 sub
(* (window-normal-size sub horizontal
) normal
))
4428 (setq sub
(window-right sub
)))))
4429 ;; Get entire space from WINDOW.
4430 (set-window-new-pixel
4431 window
(- old-pixel-size new-pixel-size
))
4432 ;; (set-window-new-pixel window (- old-pixel-size new-pixel-size))
4433 ;; (set-window-new-total
4434 ;; window (- old-size new-size))
4435 (window--resize-this-window window
(- new-pixel-size
) horizontal
)
4436 (set-window-new-normal
4437 window
(- (if new-parent
1.0 (window-normal-size window horizontal
))
4440 (let* ((new (split-window-internal window new-pixel-size side new-normal
)))
4441 (window--pixel-to-total frame horizontal
)
4442 ;; Assign window-side parameters, if any.
4444 ((eq window-combination-resize
'side
)
4447 (window-side window-side
)
4448 ((eq side
'above
) 'top
)
4449 ((eq side
'below
) 'bottom
)
4451 ;; We made a new side window.
4452 (set-window-parameter new
'window-side window-side
)
4453 (when (and new-parent
(window-parameter window
'window-side
))
4454 ;; We've been splitting a side root window. Give the
4455 ;; new parent the same window-side parameter.
4456 (set-window-parameter
4457 (window-parent new
) 'window-side window-side
))))
4458 ((eq window-combination-resize
'atom
)
4459 ;; Make sure `window--check-frame' won't destroy an existing
4460 ;; atomic window in case the new window gets nested inside.
4461 (unless (window-parameter window
'window-atom
)
4462 (set-window-parameter window
'window-atom t
))
4464 (set-window-parameter (window-parent new
) 'window-atom t
))
4465 (set-window-parameter new
'window-atom t
)))
4467 (run-window-configuration-change-hook frame
)
4468 (run-window-scroll-functions new
)
4469 (window--check frame
)
4470 ;; Always return the new window.
4473 ;; I think this should be the default; I think people will prefer it--rms.
4474 (defcustom split-window-keep-point t
4475 "If non-nil, \\[split-window-below] preserves point in the new window.
4476 If nil, adjust point in the two windows to minimize redisplay.
4477 This option applies only to `split-window-below' and functions
4478 that call it. The low-level `split-window' function always keeps
4479 the original point in both windows."
4483 (defun split-window-below (&optional size
)
4484 "Split the selected window into two windows, one above the other.
4485 The selected window is above. The newly split-off window is
4486 below, and displays the same buffer. Return the new window.
4488 If optional argument SIZE is omitted or nil, both windows get the
4489 same height, or close to it. If SIZE is positive, the upper
4490 \(selected) window gets SIZE lines. If SIZE is negative, the
4491 lower (new) window gets -SIZE lines.
4493 If the variable `split-window-keep-point' is non-nil, both
4494 windows get the same value of point as the selected window.
4495 Otherwise, the window starts are chosen so as to minimize the
4496 amount of redisplay; this is convenient on slow terminals."
4498 (let ((old-window (selected-window))
4499 (old-point (window-point))
4500 (size (and size
(prefix-numeric-value size
)))
4501 moved-by-window-height moved new-window bottom
)
4502 (when (and size
(< size
0) (< (- size
) window-min-height
))
4503 ;; `split-window' would not signal an error here.
4504 (error "Size of new window too small"))
4505 (setq new-window
(split-window nil size
))
4506 (unless split-window-keep-point
4507 (with-current-buffer (window-buffer)
4508 ;; Use `save-excursion' around vertical movements below
4509 ;; (Bug#10971). Note: When the selected window's buffer has a
4510 ;; header line, up to two lines of the buffer may not show up
4511 ;; in the resulting configuration.
4513 (goto-char (window-start))
4514 (setq moved
(vertical-motion (window-height)))
4515 (set-window-start new-window
(point))
4516 (when (> (point) (window-point new-window
))
4517 (set-window-point new-window
(point)))
4518 (when (= moved
(window-height))
4519 (setq moved-by-window-height t
)
4520 (vertical-motion -
1))
4521 (setq bottom
(point)))
4522 (and moved-by-window-height
4524 (set-window-point old-window
(1- bottom
)))
4525 (and moved-by-window-height
4526 (<= (window-start new-window
) old-point
)
4527 (set-window-point new-window old-point
)
4528 (select-window new-window
))))
4529 ;; Always copy quit-restore parameter in interactive use.
4530 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
4532 (set-window-parameter new-window
'quit-restore quit-restore
)))
4535 (defalias 'split-window-vertically
'split-window-below
)
4537 (defun split-window-right (&optional size
)
4538 "Split the selected window into two side-by-side windows.
4539 The selected window is on the left. The newly split-off window
4540 is on the right, and displays the same buffer. Return the new
4543 If optional argument SIZE is omitted or nil, both windows get the
4544 same width, or close to it. If SIZE is positive, the left-hand
4545 \(selected) window gets SIZE columns. If SIZE is negative, the
4546 right-hand (new) window gets -SIZE columns. Here, SIZE includes
4547 the width of the window's scroll bar; if there are no scroll
4548 bars, it includes the width of the divider column to the window's
4551 (let ((old-window (selected-window))
4552 (size (and size
(prefix-numeric-value size
)))
4554 (when (and size
(< size
0) (< (- size
) window-min-width
))
4555 ;; `split-window' would not signal an error here.
4556 (error "Size of new window too small"))
4557 (setq new-window
(split-window nil size t
))
4558 ;; Always copy quit-restore parameter in interactive use.
4559 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
4561 (set-window-parameter new-window
'quit-restore quit-restore
)))
4564 (defalias 'split-window-horizontally
'split-window-right
)
4566 ;;; Balancing windows.
4568 ;; The following routine uses the recycled code from an old version of
4569 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
4570 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
4571 ;; more readable (FWIW we'd need three loops - one to calculate the
4572 ;; minimum sizes per window, one to enlarge or shrink windows until the
4573 ;; new parent-size matches, and one where we shrink the largest/enlarge
4574 ;; the smallest window).
4575 (defun balance-windows-2 (window horizontal
)
4576 "Subroutine of `balance-windows-1'.
4577 WINDOW must be a vertical combination (horizontal if HORIZONTAL
4579 (let* ((char-size (if window-resize-pixelwise
4581 (frame-char-size window horizontal
)))
4582 (first (window-child window
))
4584 (number-of-children 0)
4585 (parent-size (window-new-pixel window
))
4586 (total-sum parent-size
)
4587 failed size sub-total sub-delta sub-amount rest
)
4589 (setq number-of-children
(1+ number-of-children
))
4590 (when (window-size-fixed-p sub horizontal
)
4592 (- total-sum
(window-size sub horizontal t
)))
4593 (set-window-new-normal sub
'ignore
))
4594 (setq sub
(window-right sub
)))
4597 (while (and failed
(> number-of-children
0))
4598 (setq size
(/ total-sum number-of-children
))
4601 (while (and sub
(not failed
))
4602 ;; Ignore child windows that should be ignored or are stuck.
4603 (unless (window--resize-child-windows-skip-p sub
)
4604 (setq sub-total
(window-size sub horizontal t
))
4605 (setq sub-delta
(- size sub-total
))
4607 (window-sizable sub sub-delta horizontal nil t
))
4608 ;; Register the new total size for this child window.
4609 (set-window-new-pixel sub
(+ sub-total sub-amount
))
4610 (unless (= sub-amount sub-delta
)
4611 (setq total-sum
(- total-sum sub-total sub-amount
))
4612 (setq number-of-children
(1- number-of-children
))
4613 ;; We failed and need a new round.
4615 (set-window-new-normal sub
'skip
)))
4616 (setq sub
(window-right sub
))))
4618 ;; How can we be sure that `number-of-children' is NOT zero here ?
4619 (setq rest
(% total-sum number-of-children
))
4620 ;; Fix rounding by trying to enlarge non-stuck windows by one line
4621 ;; (column) until `rest' is zero.
4623 (while (and sub
(> rest
0))
4624 (unless (window--resize-child-windows-skip-p window
)
4625 (set-window-new-pixel sub
(min rest char-size
) t
)
4626 (setq rest
(- rest char-size
)))
4627 (setq sub
(window-right sub
)))
4629 ;; Fix rounding by trying to enlarge stuck windows by one line
4630 ;; (column) until `rest' equals zero.
4632 (while (and sub
(> rest
0))
4633 (unless (eq (window-new-normal sub
) 'ignore
)
4634 (set-window-new-pixel sub
(min rest char-size
) t
)
4635 (setq rest
(- rest char-size
)))
4636 (setq sub
(window-right sub
)))
4640 ;; Record new normal sizes.
4641 (set-window-new-normal
4642 sub
(/ (if (eq (window-new-normal sub
) 'ignore
)
4643 (window-size sub horizontal t
)
4644 (window-new-pixel sub
))
4645 (float parent-size
)))
4646 ;; Recursively balance each window's child windows.
4647 (balance-windows-1 sub horizontal
)
4648 (setq sub
(window-right sub
)))))
4650 (defun balance-windows-1 (window &optional horizontal
)
4651 "Subroutine of `balance-windows'."
4652 (if (window-child window
)
4653 (let ((sub (window-child window
)))
4654 (if (window-combined-p sub horizontal
)
4655 (balance-windows-2 window horizontal
)
4656 (let ((size (window-new-pixel window
)))
4658 (set-window-new-pixel sub size
)
4659 (balance-windows-1 sub horizontal
)
4660 (setq sub
(window-right sub
))))))))
4662 (defun balance-windows (&optional window-or-frame
)
4663 "Balance the sizes of windows of WINDOW-OR-FRAME.
4664 WINDOW-OR-FRAME is optional and defaults to the selected frame.
4665 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
4666 windows of that frame. If WINDOW-OR-FRAME denotes a window,
4667 recursively balance the sizes of all child windows of that
4672 ((or (not window-or-frame
)
4673 (frame-live-p window-or-frame
))
4674 (frame-root-window window-or-frame
))
4675 ((or (window-live-p window-or-frame
)
4676 (window-child window-or-frame
))
4679 (error "Not a window or frame %s" window-or-frame
))))
4680 (frame (window-frame window
)))
4681 ;; Balance vertically.
4682 (window--resize-reset (window-frame window
))
4683 (balance-windows-1 window
)
4684 (when (window--resize-apply-p frame
)
4685 (window-resize-apply frame
)
4686 (window--pixel-to-total frame
)
4687 (run-window-configuration-change-hook frame
))
4688 ;; Balance horizontally.
4689 (window--resize-reset (window-frame window
) t
)
4690 (balance-windows-1 window t
)
4691 (when (window--resize-apply-p frame t
)
4692 (window-resize-apply frame t
)
4693 (window--pixel-to-total frame t
)
4694 (run-window-configuration-change-hook frame
))))
4696 (defun window-fixed-size-p (&optional window direction
)
4697 "Return t if WINDOW cannot be resized in DIRECTION.
4698 WINDOW defaults to the selected window. DIRECTION can be
4699 nil (i.e. any), `height' or `width'."
4700 (with-current-buffer (window-buffer window
)
4701 (when (and (boundp 'window-size-fixed
) window-size-fixed
)
4703 (member (cons direction window-size-fixed
)
4704 '((height . width
) (width . height
))))))))
4706 ;;; A different solution to balance-windows.
4707 (defvar window-area-factor
1
4708 "Factor by which the window area should be over-estimated.
4709 This is used by `balance-windows-area'.
4710 Changing this globally has no effect.")
4711 (make-variable-buffer-local 'window-area-factor
)
4713 (defun balance-windows-area-adjust (window delta horizontal pixelwise
)
4714 "Wrapper around `window-resize' with error checking.
4715 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
4716 ;; `window-resize' may fail if delta is too large.
4717 (while (>= (abs delta
) 1)
4720 ;; It was wrong to use `window-resize' here. Somehow
4721 ;; `balance-windows-area' depends on resizing windows
4723 (adjust-window-trailing-edge window delta horizontal pixelwise
)
4726 ;;(message "adjust: %s" (error-message-string err))
4727 (setq delta
(/ delta
2))))))
4729 (defun balance-windows-area ()
4730 "Make all visible windows the same area (approximately).
4731 See also `window-area-factor' to change the relative size of
4734 (let* ((unchanged 0) (carry 0) (round 0)
4735 ;; Remove fixed-size windows.
4736 (wins (delq nil
(mapcar (lambda (win)
4737 (if (not (window-fixed-size-p win
)) win
))
4738 (window-list nil
'nomini
))))
4740 (pixelwise window-resize-pixelwise
)
4742 ;; Resizing a window changes the size of surrounding windows in complex
4743 ;; ways, so it's difficult to balance them all. The introduction of
4744 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
4745 ;; very difficult to do. `balance-window' above takes an off-line
4746 ;; approach: get the whole window tree, then balance it, then try to
4747 ;; adjust the windows so they fit the result.
4748 ;; Here, instead, we take a "local optimization" approach, where we just
4749 ;; go through all the windows several times until nothing needs to be
4750 ;; changed. The main problem with this approach is that it's difficult
4751 ;; to make sure it terminates, so we use some heuristic to try and break
4752 ;; off infinite loops.
4753 ;; After a round without any change, we allow a second, to give a chance
4754 ;; to the carry to propagate a minor imbalance from the end back to
4756 (while (< unchanged
2)
4757 ;; (message "New round")
4758 (setq unchanged
(1+ unchanged
) round
(1+ round
))
4761 (while (progn (setq next
(next-window next
))
4762 (window-fixed-size-p next
)))
4763 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
4765 (< (car (window-pixel-edges win
)) (car (window-pixel-edges next
))))
4766 (areadiff (/ (- (* (window-size next nil pixelwise
)
4767 (window-size next t pixelwise
)
4768 (buffer-local-value 'window-area-factor
4769 (window-buffer next
)))
4770 (* (window-size win nil pixelwise
)
4771 (window-size win t pixelwise
)
4772 (buffer-local-value 'window-area-factor
4773 (window-buffer win
))))
4774 (max (buffer-local-value 'window-area-factor
4775 (window-buffer win
))
4776 (buffer-local-value 'window-area-factor
4777 (window-buffer next
)))))
4779 (+ (window-size win nil pixelwise
)
4780 (window-size next nil pixelwise
))
4781 (+ (window-size win t pixelwise
)
4782 (window-size next t pixelwise
))))
4783 (diff (/ areadiff edgesize
)))
4785 ;; Maybe diff is actually closer to 1 than to 0.
4786 (setq diff
(/ (* 3 areadiff
) (* 2 edgesize
))))
4787 (when (and (zerop diff
) (not (zerop areadiff
)))
4788 (setq diff
(/ (+ areadiff carry
) edgesize
))
4789 ;; Change things smoothly.
4790 (if (or (> diff
1) (< diff -
1)) (setq diff
(/ diff
2))))
4792 ;; Make sure negligible differences don't accumulate to
4793 ;; become significant.
4794 (setq carry
(+ carry areadiff
))
4795 ;; This used `adjust-window-trailing-edge' before and uses
4796 ;; `window-resize' now. Error wrapping is still needed.
4797 (balance-windows-area-adjust win diff horiz pixelwise
)
4799 (let ((change (cons win
(window-pixel-edges win
))))
4800 ;; If the same change has been seen already for this window,
4801 ;; we're most likely in an endless loop, so don't count it as
4803 (unless (member change changelog
)
4804 (push change changelog
)
4805 (setq unchanged
0 carry
0)))))))
4806 ;; We've now basically balanced all the windows.
4807 ;; But there may be some minor off-by-one imbalance left over,
4808 ;; so let's do some fine tuning.
4809 ;; (bw-finetune wins)
4810 ;; (message "Done in %d rounds" round)
4813 ;;; Window states, how to get them and how to put them in a window.
4814 (defun window--state-get-1 (window &optional writable
)
4815 "Helper function for `window-state-get'."
4818 ((window-top-child window
) 'vc
)
4819 ((window-left-child window
) 'hc
)
4821 (buffer (window-buffer window
))
4822 (selected (eq window
(selected-window)))
4825 ,@(unless (window-next-sibling window
) `((last . t
)))
4826 (pixel-width .
,(window-pixel-width window
))
4827 (pixel-height .
,(window-pixel-height window
))
4828 (total-width .
,(window-total-width window
))
4829 (total-height .
,(window-total-height window
))
4830 (normal-height .
,(window-normal-size window
))
4831 (normal-width .
,(window-normal-size window t
))
4832 ,@(unless (window-live-p window
)
4833 `((combination-limit .
,(window-combination-limit window
))))
4834 ,@(let ((parameters (window-parameters window
))
4836 ;; Make copies of those window parameters whose
4837 ;; persistence property is `writable' if WRITABLE is
4838 ;; non-nil and non-nil if WRITABLE is nil.
4839 (dolist (par parameters
)
4840 (let ((pers (cdr (assq (car par
)
4841 window-persistent-parameters
))))
4842 (when (and pers
(or (not writable
) (eq pers
'writable
)))
4843 (setq list
(cons (cons (car par
) (cdr par
)) list
)))))
4844 ;; Add `clone-of' parameter if necessary.
4845 (let ((pers (cdr (assq 'clone-of
4846 window-persistent-parameters
))))
4847 (when (and pers
(or (not writable
) (eq pers
'writable
))
4848 (not (assq 'clone-of list
)))
4849 (setq list
(cons (cons 'clone-of window
) list
))))
4851 `((parameters .
,list
))))
4853 ;; All buffer related things go in here.
4854 (let ((point (window-point window
))
4855 (start (window-start window
)))
4857 ,(buffer-name buffer
)
4858 (selected .
,selected
)
4859 (hscroll .
,(window-hscroll window
))
4860 (fringes .
,(window-fringes window
))
4861 (margins .
,(window-margins window
))
4862 (scroll-bars .
,(window-scroll-bars window
))
4863 (vscroll .
,(window-vscroll window
))
4864 (dedicated .
,(window-dedicated-p window
))
4865 (point .
,(if writable point
4868 'window-point-insertion-type
4870 (start .
,(if writable start
(copy-marker start
)))))))))
4872 (when (memq type
'(vc hc
))
4874 (setq window
(window-child window
))
4876 (setq list
(cons (window--state-get-1 window writable
) list
))
4877 (setq window
(window-right window
)))
4879 (append head tail
)))
4881 (defun window-state-get (&optional window writable
)
4882 "Return state of WINDOW as a Lisp object.
4883 WINDOW can be any window and defaults to the root window of the
4886 Optional argument WRITABLE non-nil means do not use markers for
4887 sampling `window-point' and `window-start'. Together, WRITABLE
4888 and the variable `window-persistent-parameters' specify which
4889 window parameters are saved by this function. WRITABLE should be
4890 non-nil when the return value shall be written to a file and read
4891 back in another session. Otherwise, an application may run into
4892 an `invalid-read-syntax' error while attempting to read back the
4895 The return value can be used as argument for `window-state-put'
4896 to put the state recorded here into an arbitrary window. The
4897 value can be also stored on disk and read back in a new session."
4900 (if (window-valid-p window
)
4902 (error "%s is not a live or internal window" window
))
4903 (frame-root-window)))
4904 ;; The return value is a cons whose car specifies some constraints on
4905 ;; the size of WINDOW. The cdr lists the states of the child windows
4908 ;; Frame related things would go into a function, say `frame-state',
4909 ;; calling `window-state-get' to insert the frame's root window.
4910 `((min-height .
,(window-min-size window
))
4911 (min-width .
,(window-min-size window t
))
4912 (min-height-ignore .
,(window-min-size window nil t
))
4913 (min-width-ignore .
,(window-min-size window t t
))
4914 (min-height-safe .
,(window-min-size window nil
'safe
))
4915 (min-width-safe .
,(window-min-size window t
'safe
))
4916 (min-pixel-height .
,(window-min-size window nil nil t
))
4917 (min-pixel-width .
,(window-min-size window t nil t
))
4918 (min-pixel-height-ignore .
,(window-min-size window nil t t
))
4919 (min-pixel-width-ignore .
,(window-min-size window t t t
))
4920 (min-pixel-height-safe .
,(window-min-size window nil
'safe t
))
4921 (min-pixel-width-safe .
,(window-min-size window t
'safe t
)))
4922 (window--state-get-1 window writable
)))
4924 (defvar window-state-put-list nil
4925 "Helper variable for `window-state-put'.")
4927 (defvar window-state-put-stale-windows nil
4928 "Helper variable for `window-state-put'.")
4930 (defun window--state-put-1 (state &optional window ignore totals pixelwise
)
4931 "Helper function for `window-state-put'."
4932 (let ((type (car state
)))
4933 (setq state
(cdr state
))
4936 ;; For a leaf window just add unprocessed entries to
4937 ;; `window-state-put-list'.
4938 (push (cons window state
) window-state-put-list
))
4939 ((memq type
'(vc hc
))
4940 (let* ((horizontal (eq type
'hc
))
4941 (total (window-size window horizontal pixelwise
))
4944 (dolist (item state
)
4945 ;; Find the next child window. WINDOW always points to the
4946 ;; real window that we want to fill with what we find here.
4947 (when (memq (car item
) '(leaf vc hc
))
4948 (if (assq 'last item
)
4949 ;; The last child window. Below `window--state-put-1'
4950 ;; will put into it whatever ITEM has in store.
4952 ;; Not the last child window, prepare for splitting
4953 ;; WINDOW. SIZE is the new (and final) size of the old
4959 (cdr (assq (if horizontal
4963 (cdr (assq (if horizontal
4967 ;; Use normalized size and round.
4970 (cdr (assq (if horizontal
'normal-width
'normal-height
)
4973 ;; Use safe sizes, we try to resize later.
4974 (setq size
(max size
4976 (* window-safe-min-width
4978 (frame-char-width (window-frame window
))
4980 (* window-safe-min-height
4982 (frame-char-height (window-frame window
))
4984 (if (window-sizable-p window
(- size
) horizontal
'safe pixelwise
)
4985 (let* ((window-combination-limit
4986 (assq 'combination-limit item
)))
4987 ;; We must inherit the combination limit, otherwise
4988 ;; we might mess up handling of atomic and side
4990 (setq new
(split-window window size horizontal pixelwise
)))
4991 ;; Give up if we can't resize window down to safe sizes.
4992 (error "Cannot resize window %s" window
))
4996 ;; When creating the first child window add for parent
4997 ;; unprocessed entries to `window-state-put-list'.
4998 (setq window-state-put-list
4999 (cons (cons (window-parent window
) state
)
5000 window-state-put-list
))))
5002 ;; Now process the current window (either the one we've just
5003 ;; split or the last child of its parent).
5004 (window--state-put-1 item window ignore totals
)
5005 ;; Continue with the last window split off.
5006 (setq window new
))))))))
5008 (defun window--state-put-2 (ignore pixelwise
)
5009 "Helper function for `window-state-put'."
5010 (dolist (item window-state-put-list
)
5011 (let ((window (car item
))
5012 (combination-limit (cdr (assq 'combination-limit item
)))
5013 (parameters (cdr (assq 'parameters item
)))
5014 (state (cdr (assq 'buffer item
))))
5015 (when combination-limit
5016 (set-window-combination-limit window combination-limit
))
5017 ;; Reset window's parameters and assign saved ones (we might want
5018 ;; a `remove-window-parameters' function here).
5019 (dolist (parameter (window-parameters window
))
5020 (set-window-parameter window
(car parameter
) nil
))
5022 (dolist (parameter parameters
)
5023 (set-window-parameter window
(car parameter
) (cdr parameter
))))
5024 ;; Process buffer related state.
5026 (let ((buffer (get-buffer (car state
))))
5028 (with-current-buffer buffer
5029 (set-window-buffer window buffer
)
5030 (set-window-hscroll window
(cdr (assq 'hscroll state
)))
5031 (apply 'set-window-fringes
5032 (cons window
(cdr (assq 'fringes state
))))
5033 (let ((margins (cdr (assq 'margins state
))))
5034 (set-window-margins window
(car margins
) (cdr margins
)))
5035 (let ((scroll-bars (cdr (assq 'scroll-bars state
))))
5036 (set-window-scroll-bars
5037 window
(car scroll-bars
) (nth 2 scroll-bars
)
5038 (nth 3 scroll-bars
)))
5039 (set-window-vscroll window
(cdr (assq 'vscroll state
)))
5040 ;; Adjust vertically.
5041 (if (memq window-size-fixed
'(t height
))
5042 ;; A fixed height window, try to restore the
5046 (if pixelwise
'pixel-height
'total-height
)
5048 (window-size window nil pixelwise
)))
5050 (when (window--resizable-p
5051 window delta nil nil nil nil nil pixelwise
)
5052 (window-resize window delta nil nil pixelwise
)))
5053 ;; Else check whether the window is not high enough.
5055 (window-min-size window nil ignore pixelwise
))
5057 (- min-size
(window-size window nil pixelwise
))))
5058 (when (and (> delta
0)
5059 (window--resizable-p
5060 window delta nil ignore nil nil nil pixelwise
))
5061 (window-resize window delta nil ignore pixelwise
))))
5062 ;; Adjust horizontally.
5063 (if (memq window-size-fixed
'(t width
))
5064 ;; A fixed width window, try to restore the original
5068 (if pixelwise
'pixel-width
'total-width
)
5070 (window-size window t pixelwise
)))
5072 (when (window--resizable-p
5073 window delta nil nil nil nil nil pixelwise
)
5074 (window-resize window delta nil nil pixelwise
)))
5075 ;; Else check whether the window is not wide enough.
5076 (let* ((min-size (window-min-size window t ignore pixelwise
))
5077 (delta (- min-size
(window-size window t pixelwise
))))
5078 (when (and (> delta
0)
5079 (window--resizable-p
5080 window delta t ignore nil nil nil pixelwise
))
5081 (window-resize window delta t ignore pixelwise
))))
5082 ;; Set dedicated status.
5083 (set-window-dedicated-p window
(cdr (assq 'dedicated state
)))
5084 ;; Install positions (maybe we should do this after all
5085 ;; windows have been created and sized).
5087 (set-window-start window
(cdr (assq 'start state
)))
5088 (set-window-point window
(cdr (assq 'point state
))))
5089 ;; Select window if it's the selected one.
5090 (when (cdr (assq 'selected state
))
5091 (select-window window
)))
5092 ;; We don't want to raise an error in case the buffer does
5093 ;; not exist anymore, so we switch to a previous one and
5094 ;; save the window with the intention of deleting it later
5096 (switch-to-prev-buffer window
)
5097 (push window window-state-put-stale-windows
)))))))
5099 (defun window-state-put (state &optional window ignore
)
5100 "Put window state STATE into WINDOW.
5101 STATE should be the state of a window returned by an earlier
5102 invocation of `window-state-get'. Optional argument WINDOW must
5103 specify a valid window and defaults to the selected one. If
5104 WINDOW is not live, replace WINDOW by a live one before putting
5107 Optional argument IGNORE non-nil means ignore minimum window
5108 sizes and fixed size restrictions. IGNORE equal `safe' means
5109 windows can get as small as `window-safe-min-height' and
5110 `window-safe-min-width'."
5111 (setq window-state-put-stale-windows nil
)
5112 (setq window
(window-normalize-window window
))
5114 ;; When WINDOW is internal, reduce it to a live one to put STATE into,
5116 (unless (window-live-p window
)
5117 (let ((root (frame-root-window window
)))
5118 (if (eq window root
)
5119 (setq window
(frame-first-window root
))
5121 (setq window
(catch 'live
5122 (walk-window-subtree
5124 (when (window-live-p window
)
5125 (throw 'live window
)))
5127 (delete-other-windows-internal window root
)))
5129 (let* ((frame (window-frame window
))
5131 ;; We check here (1) whether the total sizes of root window of
5132 ;; STATE and that of WINDOW are equal so we can avoid
5133 ;; calculating new sizes, and (2) if we do have to resize
5134 ;; whether we can do so without violating size restrictions.
5135 (pixelwise (and (cdr (assq 'pixel-width state
))
5136 (cdr (assq 'pixel-height state
))))
5137 (totals (or (and pixelwise
5138 (= (window-pixel-width window
)
5139 (cdr (assq 'pixel-width state
)))
5140 (= (window-pixel-height window
)
5141 (cdr (assq 'pixel-height state
))))
5142 (and (= (window-total-width window
)
5143 (cdr (assq 'total-width state
)))
5144 (= (window-total-height window
)
5145 (cdr (assq 'total-height state
))))))
5146 (min-height (cdr (assq
5147 (if pixelwise
'min-pixel-height
'min-height
)
5149 (min-width (cdr (assq
5150 (if pixelwise
'min-pixel-width
'min-weight
)
5152 (if (and (not totals
)
5153 (or (> min-height
(window-size window nil pixelwise
))
5154 (> min-width
(window-size window t pixelwise
)))
5156 (and (setq min-height
5159 'min-pixel-height-ignore
5165 'min-pixel-width-ignore
5169 (window-size window nil pixelwise
))
5171 (window-size window t pixelwise
)))
5172 (or (not (eq ignore
'safe
))
5173 (and (setq min-height
5176 'min-pixel-height-safe
5182 'min-pixel-width-safe
5186 (window-size window nil pixelwise
))
5188 (window-size window t pixelwise
))))))))
5189 ;; The check above might not catch all errors due to rounding
5190 ;; issues - so IGNORE equal 'safe might not always produce the
5191 ;; minimum possible state. But such configurations hardly make
5193 (error "Window %s too small to accommodate state" window
)
5194 (setq state
(cdr state
))
5195 (setq window-state-put-list nil
)
5196 ;; Work on the windows of a temporary buffer to make sure that
5197 ;; splitting proceeds regardless of any buffer local values of
5198 ;; `window-size-fixed'. Release that buffer after the buffers of
5199 ;; all live windows have been set by `window--state-put-2'.
5201 (set-window-buffer window
(current-buffer))
5202 (window--state-put-1 state window nil totals pixelwise
)
5203 (window--state-put-2 ignore pixelwise
))
5204 (while window-state-put-stale-windows
5205 (let ((window (pop window-state-put-stale-windows
)))
5206 (when (eq (window-deletable-p window
) t
)
5207 (delete-window window
))))
5208 (window--check frame
))))
5210 (defun display-buffer-record-window (type window buffer
)
5211 "Record information for window used by `display-buffer'.
5212 TYPE specifies the type of the calling operation and must be one
5213 of the symbols 'reuse (when WINDOW existed already and was
5214 reused for displaying BUFFER), 'window (when WINDOW was created
5215 on an already existing frame), or 'frame (when WINDOW was
5216 created on a new frame). WINDOW is the window used for or created
5217 by the `display-buffer' routines. BUFFER is the buffer that
5220 This function installs or updates the quit-restore parameter of
5221 WINDOW. The quit-restore parameter is a list of four elements:
5222 The first element is one of the symbols 'window, 'frame, 'same or
5223 'other. The second element is either one of the symbols 'window
5224 or 'frame or a list whose elements are the buffer previously
5225 shown in the window, that buffer's window start and window point,
5226 and the window's height. The third element is the window
5227 selected at the time the parameter was created. The fourth
5231 (if (eq (window-buffer window
) buffer
)
5232 ;; WINDOW shows BUFFER already.
5233 (when (consp (window-parameter window
'quit-restore
))
5234 ;; If WINDOW has a quit-restore parameter, reset its car.
5235 (setcar (window-parameter window
'quit-restore
) 'same
))
5236 ;; WINDOW shows another buffer.
5237 (with-current-buffer (window-buffer window
)
5238 (set-window-parameter
5239 window
'quit-restore
5241 ;; A quadruple of WINDOW's buffer, start, point and height.
5242 (list (current-buffer) (window-start window
)
5243 ;; Preserve window-point-insertion-type (Bug#12588).
5245 (window-point window
) window-point-insertion-type
)
5246 (window-total-height window
))
5247 (selected-window) buffer
)))))
5249 ;; WINDOW has been created on an existing frame.
5250 (set-window-parameter
5251 window
'quit-restore
5252 (list 'window
'window
(selected-window) buffer
)))
5254 ;; WINDOW has been created on a new frame.
5255 (set-window-parameter
5256 window
'quit-restore
5257 (list 'frame
'frame
(selected-window) buffer
)))))
5259 (defcustom display-buffer-function nil
5260 "If non-nil, function to call to handle `display-buffer'.
5261 It will receive two args, the buffer and a flag which if non-nil
5262 means that the currently selected window is not acceptable. It
5263 should choose or create a window, display the specified buffer in
5264 it, and return the window.
5266 The specified function should call `display-buffer-record-window'
5267 with corresponding arguments to set up the quit-restore parameter
5268 of the window used."
5271 (function :tag
"function"))
5274 (make-obsolete-variable 'display-buffer-function
5275 'display-buffer-alist
"24.3")
5277 ;; Eventually, we want to turn this into a defvar; instead of
5278 ;; customizing this, the user should use a `pop-up-frame-parameters'
5279 ;; alist entry in `display-buffer-base-action'.
5280 (defcustom pop-up-frame-alist nil
5281 "Alist of parameters for automatically generated new frames.
5282 If non-nil, the value you specify here is used by the default
5283 `pop-up-frame-function' for the creation of new frames.
5285 Since `pop-up-frame-function' is used by `display-buffer' for
5286 making new frames, any value specified here by default affects
5287 the automatic generation of new frames via `display-buffer' and
5288 all functions based on it. The behavior of `make-frame' is not
5289 affected by this variable."
5290 :type
'(repeat (cons :format
"%v"
5291 (symbol :tag
"Parameter")
5292 (sexp :tag
"Value")))
5295 (defcustom pop-up-frame-function
5296 (lambda () (make-frame pop-up-frame-alist
))
5297 "Function used by `display-buffer' for creating a new frame.
5298 This function is called with no arguments and should return a new
5299 frame. The default value calls `make-frame' with the argument
5300 `pop-up-frame-alist'."
5304 (defcustom special-display-buffer-names nil
5305 "List of names of buffers that should be displayed specially.
5306 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5307 its name is in this list, displays the buffer in a way specified
5308 by `special-display-function'. `special-display-popup-frame'
5309 \(the default for `special-display-function') usually displays
5310 the buffer in a separate frame made with the parameters specified
5311 by `special-display-frame-alist'. If `special-display-function'
5312 has been set to some other function, that function is called with
5313 the buffer as first, and nil as second argument.
5315 Alternatively, an element of this list can be specified as
5316 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
5317 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5318 `special-display-popup-frame' will interpret such pairs as frame
5319 parameters when it creates a special frame, overriding the
5320 corresponding values from `special-display-frame-alist'.
5322 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5323 `special-display-popup-frame' displays that buffer in the
5324 selected window. If FRAME-PARAMETERS contains (same-frame . t),
5325 it displays that buffer in a window on the selected frame.
5327 If `special-display-function' specifies some other function than
5328 `special-display-popup-frame', that function is called with the
5329 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5332 Finally, an element of this list can be also specified as
5333 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5334 `special-display-popup-frame' will call FUNCTION with the buffer
5335 named BUFFER-NAME as first argument, and OTHER-ARGS as the
5338 Any alternative function specified here is responsible for
5339 setting up the quit-restore parameter of the window used.
5341 If this variable appears \"not to work\", because you added a
5342 name to it but the corresponding buffer is displayed in the
5343 selected window, look at the values of `same-window-buffer-names'
5344 and `same-window-regexps'. Those variables take precedence over
5347 See also `special-display-regexps'."
5349 (choice :tag
"Buffer"
5351 (string :format
"%v")
5352 (cons :tag
"With parameters"
5355 (string :format
"%v")
5356 (repeat :tag
"Parameters"
5358 (symbol :tag
"Parameter")
5359 (sexp :tag
"Value"))))
5360 (list :tag
"With function"
5363 (string :format
"%v")
5364 (function :tag
"Function")
5365 (repeat :tag
"Arguments" (sexp)))))
5368 (make-obsolete-variable 'special-display-buffer-names
'display-buffer-alist
"24.3")
5369 (put 'special-display-buffer-names
'risky-local-variable t
)
5371 (defcustom special-display-regexps nil
5372 "List of regexps saying which buffers should be displayed specially.
5373 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5374 any regexp in this list matches its name, displays it specially
5375 using `special-display-function'. `special-display-popup-frame'
5376 \(the default for `special-display-function') usually displays
5377 the buffer in a separate frame made with the parameters specified
5378 by `special-display-frame-alist'. If `special-display-function'
5379 has been set to some other function, that function is called with
5380 the buffer as first, and nil as second argument.
5382 Alternatively, an element of this list can be specified as
5383 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
5384 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5385 `special-display-popup-frame' will then interpret these pairs as
5386 frame parameters when creating a special frame for a buffer whose
5387 name matches REGEXP, overriding the corresponding values from
5388 `special-display-frame-alist'.
5390 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5391 `special-display-popup-frame' displays buffers matching REGEXP in
5392 the selected window. (same-frame . t) in FRAME-PARAMETERS means
5393 to display such buffers in a window on the selected frame.
5395 If `special-display-function' specifies some other function than
5396 `special-display-popup-frame', that function is called with the
5397 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
5400 Finally, an element of this list can be also specified as
5401 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
5402 will then call FUNCTION with the buffer whose name matched
5403 REGEXP as first, and OTHER-ARGS as second argument.
5405 Any alternative function specified here is responsible for
5406 setting up the quit-restore parameter of the window used.
5408 If this variable appears \"not to work\", because you added a
5409 name to it but the corresponding buffer is displayed in the
5410 selected window, look at the values of `same-window-buffer-names'
5411 and `same-window-regexps'. Those variables take precedence over
5414 See also `special-display-buffer-names'."
5416 (choice :tag
"Buffer"
5418 (regexp :format
"%v")
5419 (cons :tag
"With parameters"
5422 (regexp :format
"%v")
5423 (repeat :tag
"Parameters"
5425 (symbol :tag
"Parameter")
5426 (sexp :tag
"Value"))))
5427 (list :tag
"With function"
5430 (regexp :format
"%v")
5431 (function :tag
"Function")
5432 (repeat :tag
"Arguments" (sexp)))))
5435 (make-obsolete-variable 'special-display-regexps
'display-buffer-alist
"24.3")
5436 (put 'special-display-regexps
'risky-local-variable t
)
5438 (defun special-display-p (buffer-name)
5439 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
5440 More precisely, return t if `special-display-buffer-names' or
5441 `special-display-regexps' contain a string entry equaling or
5442 matching BUFFER-NAME. If `special-display-buffer-names' or
5443 `special-display-regexps' contain a list entry whose car equals
5444 or matches BUFFER-NAME, the return value is the cdr of that
5448 ((member buffer-name special-display-buffer-names
)
5450 ((setq tmp
(assoc buffer-name special-display-buffer-names
))
5453 (dolist (regexp special-display-regexps
)
5456 (when (string-match-p regexp buffer-name
)
5458 ((and (consp regexp
) (stringp (car regexp
))
5459 (string-match-p (car regexp
) buffer-name
))
5460 (throw 'found
(cdr regexp
))))))))))
5462 (defcustom special-display-frame-alist
5463 '((height .
14) (width .
80) (unsplittable . t
))
5464 "Alist of parameters for special frames.
5465 Special frames are used for buffers whose names are listed in
5466 `special-display-buffer-names' and for buffers whose names match
5467 one of the regular expressions in `special-display-regexps'.
5469 This variable can be set in your init file, like this:
5471 (setq special-display-frame-alist '((width . 80) (height . 20)))
5473 These supersede the values given in `default-frame-alist'."
5474 :type
'(repeat (cons :format
"%v"
5475 (symbol :tag
"Parameter")
5476 (sexp :tag
"Value")))
5478 (make-obsolete-variable 'special-display-frame-alist
'display-buffer-alist
"24.3")
5480 (defun special-display-popup-frame (buffer &optional args
)
5481 "Pop up a frame displaying BUFFER and return its window.
5482 If BUFFER is already displayed in a visible or iconified frame,
5483 raise that frame. Otherwise, display BUFFER in a new frame.
5485 Optional argument ARGS is a list specifying additional
5488 If ARGS is an alist, use it as a list of frame parameters. If
5489 these parameters contain (same-window . t), display BUFFER in
5490 the selected window. If they contain (same-frame . t), display
5491 BUFFER in a window of the selected frame.
5493 If ARGS is a list whose car is a symbol, use (car ARGS) as a
5494 function to do the work. Pass it BUFFER as first argument, and
5495 pass the elements of (cdr ARGS) as the remaining arguments."
5496 (if (and args
(symbolp (car args
)))
5497 (apply (car args
) buffer
(cdr args
))
5498 (let ((window (get-buffer-window buffer
0)))
5500 ;; If we have a window already, make it visible.
5502 (let ((frame (window-frame window
)))
5503 (make-frame-visible frame
)
5505 (display-buffer-record-window 'reuse window buffer
)
5507 ;; Reuse the current window if the user requested it.
5508 (when (cdr (assq 'same-window args
))
5510 (progn (switch-to-buffer buffer nil t
) (selected-window))
5512 ;; Stay on the same frame if requested.
5513 (when (or (cdr (assq 'same-frame args
)) (cdr (assq 'same-window args
)))
5514 (let* ((pop-up-windows t
)
5516 special-display-buffer-names special-display-regexps
)
5517 (display-buffer buffer
)))
5518 ;; If no window yet, make one in a new frame.
5520 (with-current-buffer buffer
5521 (make-frame (append args special-display-frame-alist
))))
5522 (window (frame-selected-window frame
)))
5523 (display-buffer-record-window 'frame window buffer
)
5524 (unless (eq buffer
(window-buffer window
))
5525 (set-window-buffer window buffer
)
5526 (set-window-prev-buffers window nil
))
5527 (set-window-dedicated-p window t
)
5530 (defcustom special-display-function
'special-display-popup-frame
5531 "Function to call for displaying special buffers.
5532 This function is called with two arguments - the buffer and,
5533 optionally, a list - and should return a window displaying that
5534 buffer. The default value usually makes a separate frame for the
5535 buffer using `special-display-frame-alist' to specify the frame
5536 parameters. See the definition of `special-display-popup-frame'
5537 for how to specify such a function.
5539 A buffer is special when its name is either listed in
5540 `special-display-buffer-names' or matches a regexp in
5541 `special-display-regexps'.
5543 The specified function should call `display-buffer-record-window'
5544 with corresponding arguments to set up the quit-restore parameter
5545 of the window used."
5548 (make-obsolete-variable 'special-display-function
'display-buffer-alist
"24.3")
5550 (defcustom same-window-buffer-names nil
5551 "List of names of buffers that should appear in the \"same\" window.
5552 `display-buffer' and `pop-to-buffer' show a buffer whose name is
5553 on this list in the selected rather than some other window.
5555 An element of this list can be a cons cell instead of just a
5556 string. In that case, the cell's car must be a string specifying
5557 the buffer name. This is for compatibility with
5558 `special-display-buffer-names'; the cdr of the cons cell is
5561 See also `same-window-regexps'."
5562 :type
'(repeat (string :format
"%v"))
5565 (defcustom same-window-regexps nil
5566 "List of regexps saying which buffers should appear in the \"same\" window.
5567 `display-buffer' and `pop-to-buffer' show a buffer whose name
5568 matches a regexp on this list in the selected rather than some
5571 An element of this list can be a cons cell instead of just a
5572 string. In that case, the cell's car must be a regexp matching
5573 the buffer name. This is for compatibility with
5574 `special-display-regexps'; the cdr of the cons cell is ignored.
5576 See also `same-window-buffer-names'."
5577 :type
'(repeat (regexp :format
"%v"))
5580 (defun same-window-p (buffer-name)
5581 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5582 This function returns non-nil if `display-buffer' or
5583 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
5584 selected rather than (as usual) some other window. See
5585 `same-window-buffer-names' and `same-window-regexps'."
5587 ((not (stringp buffer-name
)))
5588 ;; The elements of `same-window-buffer-names' can be buffer
5589 ;; names or cons cells whose cars are buffer names.
5590 ((member buffer-name same-window-buffer-names
))
5591 ((assoc buffer-name same-window-buffer-names
))
5593 (dolist (regexp same-window-regexps
)
5594 ;; The elements of `same-window-regexps' can be regexps
5595 ;; or cons cells whose cars are regexps.
5596 (when (or (and (stringp regexp
)
5597 (string-match-p regexp buffer-name
))
5598 (and (consp regexp
) (stringp (car regexp
))
5599 (string-match-p (car regexp
) buffer-name
)))
5600 (throw 'found t
)))))))
5602 (defcustom pop-up-frames nil
5603 "Whether `display-buffer' should make a separate frame.
5604 If nil, never make a separate frame.
5605 If the value is `graphic-only', make a separate frame
5606 on graphic displays only.
5607 Any other non-nil value means always make a separate frame."
5609 (const :tag
"Never" nil
)
5610 (const :tag
"On graphic displays only" graphic-only
)
5611 (const :tag
"Always" t
))
5614 (defcustom display-buffer-reuse-frames nil
5615 "Non-nil means `display-buffer' should reuse frames.
5616 If the buffer in question is already displayed in a frame, raise
5622 (make-obsolete-variable
5623 'display-buffer-reuse-frames
5624 "use a `reusable-frames' alist entry in `display-buffer-alist'."
5627 (defcustom pop-up-windows t
5628 "Non-nil means `display-buffer' should make a new window."
5632 (defcustom split-window-preferred-function
'split-window-sensibly
5633 "Function called by `display-buffer' routines to split a window.
5634 This function is called with a window as single argument and is
5635 supposed to split that window and return the new window. If the
5636 window can (or shall) not be split, it is supposed to return nil.
5637 The default is to call the function `split-window-sensibly' which
5638 tries to split the window in a way which seems most suitable.
5639 You can customize the options `split-height-threshold' and/or
5640 `split-width-threshold' in order to have `split-window-sensibly'
5641 prefer either vertical or horizontal splitting.
5643 If you set this to any other function, bear in mind that the
5644 `display-buffer' routines may call this function two times. The
5645 argument of the first call is the largest window on its frame.
5646 If that call fails to return a live window, the function is
5647 called again with the least recently used window as argument. If
5648 that call fails too, `display-buffer' will use an existing window
5649 to display its buffer.
5651 The window selected at the time `display-buffer' was invoked is
5652 still selected when this function is called. Hence you can
5653 compare the window argument with the value of `selected-window'
5654 if you intend to split the selected window instead or if you do
5655 not want to split the selected window."
5660 (defcustom split-height-threshold
80
5661 "Minimum height for splitting windows sensibly.
5662 If this is an integer, `split-window-sensibly' may split a window
5663 vertically only if it has at least this many lines. If this is
5664 nil, `split-window-sensibly' is not allowed to split a window
5665 vertically. If, however, a window is the only window on its
5666 frame, `split-window-sensibly' may split it vertically
5667 disregarding the value of this variable."
5668 :type
'(choice (const nil
) (integer :tag
"lines"))
5672 (defcustom split-width-threshold
160
5673 "Minimum width for splitting windows sensibly.
5674 If this is an integer, `split-window-sensibly' may split a window
5675 horizontally only if it has at least this many columns. If this
5676 is nil, `split-window-sensibly' is not allowed to split a window
5678 :type
'(choice (const nil
) (integer :tag
"columns"))
5682 (defun window-splittable-p (window &optional horizontal
)
5683 "Return non-nil if `split-window-sensibly' may split WINDOW.
5684 Optional argument HORIZONTAL nil or omitted means check whether
5685 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
5686 non-nil means check whether WINDOW may be split horizontally.
5688 WINDOW may be split vertically when the following conditions
5690 - `window-size-fixed' is either nil or equals `width' for the
5692 - `split-height-threshold' is an integer and WINDOW is at least as
5693 high as `split-height-threshold'.
5694 - When WINDOW is split evenly, the emanating windows are at least
5695 `window-min-height' lines tall and can accommodate at least one
5696 line plus - if WINDOW has one - a mode line.
5698 WINDOW may be split horizontally when the following conditions
5700 - `window-size-fixed' is either nil or equals `height' for the
5702 - `split-width-threshold' is an integer and WINDOW is at least as
5703 wide as `split-width-threshold'.
5704 - When WINDOW is split evenly, the emanating windows are at least
5705 `window-min-width' or two (whichever is larger) columns wide."
5706 (when (window-live-p window
)
5707 (with-current-buffer (window-buffer window
)
5709 ;; A window can be split horizontally when its width is not
5710 ;; fixed, it is at least `split-width-threshold' columns wide
5711 ;; and at least twice as wide as `window-min-width' and 2 (the
5712 ;; latter value is hardcoded).
5713 (and (memq window-size-fixed
'(nil height
))
5714 ;; Testing `window-full-width-p' here hardly makes any
5715 ;; sense nowadays. This can be done more intuitively by
5716 ;; setting up `split-width-threshold' appropriately.
5717 (numberp split-width-threshold
)
5718 (>= (window-width window
)
5719 (max split-width-threshold
5720 (* 2 (max window-min-width
2)))))
5721 ;; A window can be split vertically when its height is not
5722 ;; fixed, it is at least `split-height-threshold' lines high,
5723 ;; and it is at least twice as high as `window-min-height' and 2
5724 ;; if it has a mode line or 1.
5725 (and (memq window-size-fixed
'(nil width
))
5726 (numberp split-height-threshold
)
5727 (>= (window-height window
)
5728 (max split-height-threshold
5729 (* 2 (max window-min-height
5730 (if mode-line-format
2 1))))))))))
5732 (defun split-window-sensibly (&optional window
)
5733 "Split WINDOW in a way suitable for `display-buffer'.
5734 WINDOW defaults to the currently selected window.
5735 If `split-height-threshold' specifies an integer, WINDOW is at
5736 least `split-height-threshold' lines tall and can be split
5737 vertically, split WINDOW into two windows one above the other and
5738 return the lower window. Otherwise, if `split-width-threshold'
5739 specifies an integer, WINDOW is at least `split-width-threshold'
5740 columns wide and can be split horizontally, split WINDOW into two
5741 windows side by side and return the window on the right. If this
5742 can't be done either and WINDOW is the only window on its frame,
5743 try to split WINDOW vertically disregarding any value specified
5744 by `split-height-threshold'. If that succeeds, return the lower
5745 window. Return nil otherwise.
5747 By default `display-buffer' routines call this function to split
5748 the largest or least recently used window. To change the default
5749 customize the option `split-window-preferred-function'.
5751 You can enforce this function to not split WINDOW horizontally,
5752 by setting (or binding) the variable `split-width-threshold' to
5753 nil. If, in addition, you set `split-height-threshold' to zero,
5754 chances increase that this function does split WINDOW vertically.
5756 In order to not split WINDOW vertically, set (or bind) the
5757 variable `split-height-threshold' to nil. Additionally, you can
5758 set `split-width-threshold' to zero to make a horizontal split
5759 more likely to occur.
5761 Have a look at the function `window-splittable-p' if you want to
5762 know how `split-window-sensibly' determines whether WINDOW can be
5764 (let ((window (or window
(selected-window))))
5765 (or (and (window-splittable-p window
)
5766 ;; Split window vertically.
5767 (with-selected-window window
5768 (split-window-below)))
5769 (and (window-splittable-p window t
)
5770 ;; Split window horizontally.
5771 (with-selected-window window
5772 (split-window-right)))
5773 (and (eq window
(frame-root-window (window-frame window
)))
5774 (not (window-minibuffer-p window
))
5775 ;; If WINDOW is the only window on its frame and is not the
5776 ;; minibuffer window, try to split it vertically disregarding
5777 ;; the value of `split-height-threshold'.
5778 (let ((split-height-threshold 0))
5779 (when (window-splittable-p window
)
5780 (with-selected-window window
5781 (split-window-below))))))))
5783 (defun window--try-to-split-window (window &optional alist
)
5784 "Try to split WINDOW.
5785 Return value returned by `split-window-preferred-function' if it
5786 represents a live window, nil otherwise."
5787 (and (window-live-p window
)
5788 (not (frame-parameter (window-frame window
) 'unsplittable
))
5789 (let* ((window-combination-limit
5790 ;; When `window-combination-limit' equals
5791 ;; `display-buffer' or equals `resize-window' and a
5792 ;; `window-height' or `window-width' alist entry are
5793 ;; present, bind it to t so resizing steals space
5794 ;; preferably from the window that was split.
5795 (if (or (eq window-combination-limit
'display-buffer
)
5796 (and (eq window-combination-limit
'window-size
)
5797 (or (cdr (assq 'window-height alist
))
5798 (cdr (assq 'window-width alist
)))))
5800 window-combination-limit
))
5802 ;; Since `split-window-preferred-function' might
5803 ;; throw an error use `condition-case'.
5805 (funcall split-window-preferred-function window
)
5807 (and (window-live-p new-window
) new-window
))))
5809 (defun window--frame-usable-p (frame)
5810 "Return FRAME if it can be used to display a buffer."
5811 (when (frame-live-p frame
)
5812 (let ((window (frame-root-window frame
)))
5813 ;; `frame-root-window' may be an internal window which is considered
5814 ;; "dead" by `window-live-p'. Hence if `window' is not live we
5815 ;; implicitly know that `frame' has a visible window we can use.
5816 (unless (and (window-live-p window
)
5817 (or (window-minibuffer-p window
)
5818 ;; If the window is soft-dedicated, the frame is usable.
5819 ;; Actually, even if the window is really dedicated,
5820 ;; the frame is still usable by splitting it.
5821 ;; At least Emacs-22 allowed it, and it is desirable
5822 ;; when displaying same-frame windows.
5823 nil
; (eq t (window-dedicated-p window))
5827 (defcustom even-window-heights t
5828 "If non-nil `display-buffer' will try to even window heights.
5829 Otherwise `display-buffer' will leave the window configuration
5830 alone. Heights are evened only when `display-buffer' chooses a
5831 window that appears above or below the selected window."
5835 (defun window--even-window-heights (window)
5836 "Even heights of WINDOW and selected window.
5837 Do this only if these windows are vertically adjacent to each
5838 other, `even-window-heights' is non-nil, and the selected window
5839 is higher than WINDOW."
5840 (when (and even-window-heights
5841 ;; Even iff WINDOW forms a vertical combination with the
5842 ;; selected window, and WINDOW's height exceeds that of the
5843 ;; selected window, see also bug#11880.
5844 (window-combined-p window
)
5845 (= (window-child-count (window-parent window
)) 2)
5846 (eq (window-parent) (window-parent window
))
5847 (> (window-total-height) (window-total-height window
)))
5848 ;; Don't throw an error if we can't even window heights for
5852 (/ (- (window-total-height window
) (window-total-height)) 2))
5855 (defun window--display-buffer (buffer window type
&optional alist dedicated
)
5856 "Display BUFFER in WINDOW.
5857 TYPE must be one of the symbols `reuse', `window' or `frame' and
5858 is passed unaltered to `display-buffer-record-window'. ALIST is
5859 the alist argument of `display-buffer'. Set `window-dedicated-p'
5860 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
5862 (when (and (buffer-live-p buffer
) (window-live-p window
))
5863 (display-buffer-record-window type window buffer
)
5864 (unless (eq buffer
(window-buffer window
))
5865 (set-window-dedicated-p window nil
)
5866 (set-window-buffer window buffer
)
5868 (set-window-dedicated-p window dedicated
))
5869 (when (memq type
'(window frame
))
5870 (set-window-prev-buffers window nil
)))
5871 (let ((parameter (window-parameter window
'quit-restore
))
5872 (height (cdr (assq 'window-height alist
)))
5873 (width (cdr (assq 'window-width alist
)))
5874 (size (cdr (assq 'window-size alist
))))
5876 ((or (eq type
'frame
)
5877 (and (eq (car parameter
) 'same
)
5878 (eq (nth 1 parameter
) 'frame
)))
5879 ;; Adjust size of frame if asked for.
5883 (let ((width (car size
))
5885 (frame (window-frame window
)))
5886 (when (and (numberp width
) (numberp height
))
5888 frame
(+ (frame-height frame
)
5889 (- height
(window-total-height window
))))
5891 frame
(+ (frame-width frame
)
5892 (- width
(window-total-width window
)))))))
5894 (ignore-errors (funcall size window
)))))
5895 ((or (eq type
'window
)
5896 (and (eq (car parameter
) 'same
)
5897 (eq (nth 1 parameter
) 'window
)))
5898 ;; Adjust height of window if asked for.
5903 (if (integerp height
)
5906 (* (window-total-height (frame-root-window window
))
5908 (delta (- new-height
(window-total-height window
))))
5909 (when (and (window--resizable-p window delta nil
'safe
)
5910 (window-combined-p window
))
5911 (window-resize window delta nil
'safe
))))
5913 (ignore-errors (funcall height window
))))
5914 ;; Adjust width of window if asked for.
5919 (if (integerp width
)
5922 (* (window-total-width (frame-root-window window
))
5924 (delta (- new-width
(window-total-width window
))))
5925 (when (and (window--resizable-p window delta t
'safe
)
5926 (window-combined-p window t
))
5927 (window-resize window delta t
'safe
))))
5929 (ignore-errors (funcall width window
)))))))
5933 (defun window--maybe-raise-frame (frame)
5934 (let ((visible (frame-visible-p frame
)))
5935 (unless (or (not visible
)
5936 ;; Assume the selected frame is already visible enough.
5937 (eq frame
(selected-frame))
5938 ;; Assume the frame from which we invoked the
5939 ;; minibuffer is visible.
5940 (and (minibuffer-window-active-p (selected-window))
5941 (eq frame
(window-frame (minibuffer-selected-window)))))
5942 (raise-frame frame
))))
5944 ;; FIXME: Not implemented.
5945 ;; FIXME: By the way, there could be more levels of dedication:
5946 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
5947 ;; the window hasn't been used for something else yet.
5948 ;; - `softly' dedicated only allows reuse when asked explicitly.
5949 ;; - `strongly' never allows reuse.
5950 (defvar display-buffer-mark-dedicated nil
5951 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
5952 The actual non-nil value of this variable will be copied to the
5953 `window-dedicated-p' flag.")
5955 (defconst display-buffer--action-function-custom-type
5956 '(choice :tag
"Function"
5957 (const :tag
"--" ignore
) ; default for insertion
5958 (const display-buffer-reuse-window
)
5959 (const display-buffer-pop-up-window
)
5960 (const display-buffer-same-window
)
5961 (const display-buffer-pop-up-frame
)
5962 (const display-buffer-in-previous-window
)
5963 (const display-buffer-use-some-window
)
5964 (function :tag
"Other function"))
5965 "Custom type for `display-buffer' action functions.")
5967 (defconst display-buffer--action-custom-type
5968 `(cons :tag
"Action"
5969 (choice :tag
"Action functions"
5970 ,display-buffer--action-function-custom-type
5972 :tag
"List of functions"
5973 ,display-buffer--action-function-custom-type
))
5974 (alist :tag
"Action arguments"
5976 :value-type
(sexp :tag
"Value")))
5977 "Custom type for `display-buffer' actions.")
5979 (defvar display-buffer-overriding-action
'(nil . nil
)
5980 "Overriding action to perform to display a buffer.
5981 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5982 function or a list of functions. Each function should accept two
5983 arguments: a buffer to display and an alist similar to ALIST.
5984 See `display-buffer' for details.")
5985 (put 'display-buffer-overriding-action
'risky-local-variable t
)
5987 (defcustom display-buffer-alist nil
5988 "Alist of conditional actions for `display-buffer'.
5989 This is a list of elements (CONDITION . ACTION), where:
5991 CONDITION is either a regexp matching buffer names, or a
5992 function that takes two arguments - a buffer name and the
5993 ACTION argument of `display-buffer' - and returns a boolean.
5995 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
5996 function or a list of functions. Each such function should
5997 accept two arguments: a buffer to display and an alist of the
5998 same form as ALIST. See `display-buffer' for details.
6000 `display-buffer' scans this alist until it either finds a
6001 matching regular expression or the function specified by a
6002 condition returns non-nil. In any of these cases, it adds the
6003 associated action to the list of actions it will try."
6004 :type
`(alist :key-type
6005 (choice :tag
"Condition"
6007 (function :tag
"Matcher function"))
6008 :value-type
,display-buffer--action-custom-type
)
6013 (defcustom display-buffer-base-action
'(nil . nil
)
6014 "User-specified default action for `display-buffer'.
6015 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6016 function or a list of functions. Each function should accept two
6017 arguments: a buffer to display and an alist similar to ALIST.
6018 See `display-buffer' for details."
6019 :type display-buffer--action-custom-type
6024 (defconst display-buffer-fallback-action
6025 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
6026 display-buffer-reuse-window
6027 display-buffer--maybe-pop-up-frame-or-window
6028 display-buffer-in-previous-window
6029 display-buffer-use-some-window
6030 ;; If all else fails, pop up a new frame.
6031 display-buffer-pop-up-frame
))
6032 "Default fallback action for `display-buffer'.
6033 This is the action used by `display-buffer' if no other actions
6034 specified, e.g. by the user options `display-buffer-alist' or
6035 `display-buffer-base-action'. See `display-buffer'.")
6036 (put 'display-buffer-fallback-action
'risky-local-variable t
)
6038 (defun display-buffer-assq-regexp (buffer-name alist action
)
6039 "Retrieve ALIST entry corresponding to BUFFER-NAME.
6040 ACTION is the action argument passed to `display-buffer'."
6042 (dolist (entry alist
)
6043 (let ((key (car entry
)))
6044 (when (or (and (stringp key
)
6045 (string-match-p key buffer-name
))
6046 (and (functionp key
)
6047 (funcall key buffer-name action
)))
6048 (throw 'match
(cdr entry
)))))))
6050 (defvar display-buffer--same-window-action
6051 '(display-buffer-same-window
6052 (inhibit-same-window . nil
))
6053 "A `display-buffer' action for displaying in the same window.")
6054 (put 'display-buffer--same-window-action
'risky-local-variable t
)
6056 (defvar display-buffer--other-frame-action
6057 '((display-buffer-reuse-window
6058 display-buffer-pop-up-frame
)
6059 (reusable-frames .
0)
6060 (inhibit-same-window . t
))
6061 "A `display-buffer' action for displaying in another frame.")
6062 (put 'display-buffer--other-frame-action
'risky-local-variable t
)
6064 (defun display-buffer (buffer-or-name &optional action frame
)
6065 "Display BUFFER-OR-NAME in some window, without selecting it.
6066 BUFFER-OR-NAME must be a buffer or the name of an existing
6067 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
6068 or nil if no such window is found.
6070 Optional argument ACTION, if non-nil, should specify a display
6071 action. Its form is described below.
6073 Optional argument FRAME, if non-nil, acts like an additional
6074 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
6075 specifying the frame(s) to search for a window that is already
6076 displaying the buffer. See `display-buffer-reuse-window'
6078 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
6079 where FUNCTION is either a function or a list of functions, and
6080 ALIST is an arbitrary association list (alist).
6082 Each such FUNCTION should accept two arguments: the buffer to
6083 display and an alist. Based on those arguments, it should
6084 display the buffer and return the window. If the caller is
6085 prepared to handle the case of not displaying the buffer
6086 and returning nil from `display-buffer' it should pass
6087 \(allow-no-window . t) as an element of the ALIST.
6089 The `display-buffer' function builds a function list and an alist
6090 by combining the functions and alists specified in
6091 `display-buffer-overriding-action', `display-buffer-alist', the
6092 ACTION argument, `display-buffer-base-action', and
6093 `display-buffer-fallback-action' (in order). Then it calls each
6094 function in the combined function list in turn, passing the
6095 buffer as the first argument and the combined alist as the second
6096 argument, until one of the functions returns non-nil.
6098 If ACTION is nil, the function list and the alist are built using
6099 only the other variables mentioned above.
6101 Available action functions include:
6102 `display-buffer-same-window'
6103 `display-buffer-reuse-window'
6104 `display-buffer-pop-up-frame'
6105 `display-buffer-pop-up-window'
6106 `display-buffer-in-previous-window'
6107 `display-buffer-use-some-window'
6109 Recognized alist entries include:
6111 `inhibit-same-window' -- A non-nil value prevents the same
6112 window from being used for display.
6114 `inhibit-switch-frame' -- A non-nil value prevents any other
6115 frame from being raised or selected,
6116 even if the window is displayed there.
6118 `reusable-frames' -- Value specifies frame(s) to search for a
6119 window that already displays the buffer.
6120 See `display-buffer-reuse-window'.
6122 `pop-up-frame-parameters' -- Value specifies an alist of frame
6123 parameters to give a new frame, if
6126 `window-height' -- Value specifies either an integer (the number
6127 of lines of a new window), a floating point number (the
6128 fraction of a new window with respect to the height of the
6129 frame's root window) or a function to be called with one
6130 argument - a new window. The function is supposed to adjust
6131 the height of the window; its return value is ignored.
6132 Suitable functions are `shrink-window-if-larger-than-buffer'
6133 and `fit-window-to-buffer'.
6135 `window-width' -- Value specifies either an integer (the number
6136 of columns of a new window), a floating point number (the
6137 fraction of a new window with respect to the width of the
6138 frame's root window) or a function to be called with one
6139 argument - a new window. The function is supposed to adjust
6140 the width of the window; its return value is ignored.
6142 `allow-no-window' -- A non-nil value indicates readiness for the case
6143 of not displaying the buffer and FUNCTION can safely return
6144 a non-window value to suppress displaying.
6146 The ACTION argument to `display-buffer' can also have a non-nil
6147 and non-list value. This means to display the buffer in a window
6148 other than the selected one, even if it is already displayed in
6149 the selected window. If called interactively with a prefix
6150 argument, ACTION is t."
6151 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6152 (if current-prefix-arg t
)))
6153 (let ((buffer (if (bufferp buffer-or-name
)
6155 (get-buffer buffer-or-name
)))
6156 ;; Make sure that when we split windows the old window keeps
6157 ;; point, bug#14829.
6158 (split-window-keep-point t
)
6159 ;; Handle the old form of the first argument.
6160 (inhibit-same-window (and action
(not (listp action
)))))
6161 (unless (listp action
) (setq action nil
))
6162 (if display-buffer-function
6163 ;; If `display-buffer-function' is defined, let it do the job.
6164 (funcall display-buffer-function buffer inhibit-same-window
)
6165 ;; Otherwise, use the defined actions.
6167 (display-buffer-assq-regexp
6168 (buffer-name buffer
) display-buffer-alist action
))
6169 (special-action (display-buffer--special-action buffer
))
6170 ;; Extra actions from the arguments to this function:
6172 (cons nil
(append (if inhibit-same-window
6173 '((inhibit-same-window . t
)))
6175 `((reusable-frames .
,frame
))))))
6176 ;; Construct action function list and action alist.
6177 (actions (list display-buffer-overriding-action
6178 user-action special-action action extra-action
6179 display-buffer-base-action
6180 display-buffer-fallback-action
))
6181 (functions (apply 'append
6184 (if (functionp x
) (list x
) x
))
6186 (alist (apply 'append
(mapcar 'cdr actions
)))
6188 (unless (buffer-live-p buffer
)
6189 (error "Invalid buffer"))
6190 (while (and functions
(not window
))
6191 (setq window
(funcall (car functions
) buffer alist
)
6192 functions
(cdr functions
)))
6193 (and (windowp window
) window
)))))
6195 (defun display-buffer-other-frame (buffer)
6196 "Display buffer BUFFER preferably in another frame.
6197 This uses the function `display-buffer' as a subroutine; see
6198 its documentation for additional customization information."
6199 (interactive "BDisplay buffer in other frame: ")
6200 (display-buffer buffer display-buffer--other-frame-action t
))
6202 ;;; `display-buffer' action functions:
6204 (defun display-buffer-same-window (buffer alist
)
6205 "Display BUFFER in the selected window.
6206 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
6207 if the selected window is a minibuffer window or is dedicated to
6208 another buffer; in that case, return nil. Otherwise, return the
6210 (unless (or (cdr (assq 'inhibit-same-window alist
))
6211 (window-minibuffer-p)
6212 (window-dedicated-p))
6213 (window--display-buffer buffer
(selected-window) 'reuse alist
)))
6215 (defun display-buffer--maybe-same-window (buffer alist
)
6216 "Conditionally display BUFFER in the selected window.
6217 If `same-window-p' returns non-nil for BUFFER's name, call
6218 `display-buffer-same-window' and return its value. Otherwise,
6220 (and (same-window-p (buffer-name buffer
))
6221 (display-buffer-same-window buffer alist
)))
6223 (defun display-buffer-reuse-window (buffer alist
)
6224 "Return a window that is already displaying BUFFER.
6225 Return nil if no usable window is found.
6227 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6228 window is not eligible for reuse.
6230 If ALIST contains a `reusable-frames' entry, its value determines
6231 which frames to search for a reusable window:
6232 nil -- the selected frame (actually the last non-minibuffer frame)
6233 A frame -- just that frame
6234 `visible' -- all visible frames
6235 0 -- all frames on the current terminal
6238 If ALIST contains no `reusable-frames' entry, search just the
6239 selected frame if `display-buffer-reuse-frames' and
6240 `pop-up-frames' are both nil; search all frames on the current
6241 terminal if either of those variables is non-nil.
6243 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6244 event that a window on another frame is chosen, avoid raising
6246 (let* ((alist-entry (assq 'reusable-frames alist
))
6247 (frames (cond (alist-entry (cdr alist-entry
))
6248 ((if (eq pop-up-frames
'graphic-only
)
6252 (display-buffer-reuse-frames 0)
6253 (t (last-nonminibuffer-frame))))
6254 (window (if (and (eq buffer
(window-buffer))
6255 (not (cdr (assq 'inhibit-same-window alist
))))
6257 (car (delq (selected-window)
6258 (get-buffer-window-list buffer
'nomini
6260 (when (window-live-p window
)
6261 (prog1 (window--display-buffer buffer window
'reuse alist
)
6262 (unless (cdr (assq 'inhibit-switch-frame alist
))
6263 (window--maybe-raise-frame (window-frame window
)))))))
6265 (defun display-buffer--special-action (buffer)
6266 "Return special display action for BUFFER, if any.
6267 If `special-display-p' returns non-nil for BUFFER, return an
6268 appropriate display action involving `special-display-function'.
6269 See `display-buffer' for the format of display actions."
6270 (and special-display-function
6271 ;; `special-display-p' returns either t or a list of frame
6272 ;; parameters to pass to `special-display-function'.
6273 (let ((pars (special-display-p (buffer-name buffer
))))
6275 (list (list #'display-buffer-reuse-window
6276 `(lambda (buffer _alist
)
6277 (funcall special-display-function
6278 buffer
',(if (listp pars
) pars
)))))))))
6280 (defun display-buffer-pop-up-frame (buffer alist
)
6281 "Display BUFFER in a new frame.
6282 This works by calling `pop-up-frame-function'. If successful,
6283 return the window used; otherwise return nil.
6285 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
6286 raising the new frame.
6288 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
6289 corresponding value is an alist of frame parameters to give the
6291 (let* ((params (cdr (assq 'pop-up-frame-parameters alist
)))
6292 (pop-up-frame-alist (append params pop-up-frame-alist
))
6293 (fun pop-up-frame-function
)
6296 ;; Make BUFFER current so `make-frame' will use it as the
6297 ;; new frame's buffer (Bug#15133).
6298 (with-current-buffer buffer
6299 (setq frame
(funcall fun
)))
6300 (setq window
(frame-selected-window frame
)))
6301 (prog1 (window--display-buffer
6302 buffer window
'frame alist display-buffer-mark-dedicated
)
6303 (unless (cdr (assq 'inhibit-switch-frame alist
))
6304 (window--maybe-raise-frame frame
))))))
6306 (defun display-buffer-pop-up-window (buffer alist
)
6307 "Display BUFFER by popping up a new window.
6308 The new window is created on the selected frame, or in
6309 `last-nonminibuffer-frame' if no windows can be created there.
6310 If successful, return the new window; otherwise return nil.
6312 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6313 event that the new window is created on another frame, avoid
6315 (let ((frame (or (window--frame-usable-p (selected-frame))
6316 (window--frame-usable-p (last-nonminibuffer-frame))))
6318 (when (and (or (not (frame-parameter frame
'unsplittable
))
6319 ;; If the selected frame cannot be split, look at
6320 ;; `last-nonminibuffer-frame'.
6321 (and (eq frame
(selected-frame))
6322 (setq frame
(last-nonminibuffer-frame))
6323 (window--frame-usable-p frame
)
6324 (not (frame-parameter frame
'unsplittable
))))
6325 ;; Attempt to split largest or least recently used window.
6326 (setq window
(or (window--try-to-split-window
6327 (get-largest-window frame t
) alist
)
6328 (window--try-to-split-window
6329 (get-lru-window frame t
) alist
))))
6330 (prog1 (window--display-buffer
6331 buffer window
'window alist display-buffer-mark-dedicated
)
6332 (unless (cdr (assq 'inhibit-switch-frame alist
))
6333 (window--maybe-raise-frame (window-frame window
)))))))
6335 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist
)
6336 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
6338 If `pop-up-frames' is non-nil (and not `graphic-only' on a
6339 text-only terminal), try with `display-buffer-pop-up-frame'.
6341 If that cannot be done, and `pop-up-windows' is non-nil, try
6342 again with `display-buffer-pop-up-window'."
6343 (or (and (if (eq pop-up-frames
'graphic-only
)
6346 (display-buffer-pop-up-frame buffer alist
))
6348 (display-buffer-pop-up-window buffer alist
))))
6350 (defun display-buffer-below-selected (buffer alist
)
6351 "Try displaying BUFFER in a window below the selected window.
6352 This either splits the selected window or reuses the window below
6355 (or (and (not (frame-parameter nil
'unsplittable
))
6356 (let ((split-height-threshold 0)
6357 split-width-threshold
)
6358 (setq window
(window--try-to-split-window (selected-window) alist
)))
6359 (window--display-buffer
6360 buffer window
'window alist display-buffer-mark-dedicated
))
6361 (and (setq window
(window-in-direction 'below
))
6362 (not (window-dedicated-p window
))
6363 (window--display-buffer
6364 buffer window
'reuse alist display-buffer-mark-dedicated
)))))
6366 (defun display-buffer-at-bottom (buffer alist
)
6367 "Try displaying BUFFER in a window at the bottom of the selected frame.
6368 This either splits the window at the bottom of the frame or the
6369 frame's root window, or reuses an existing window at the bottom
6370 of the selected frame."
6371 (let (bottom-window window
)
6373 (lambda (window) (setq bottom-window window
)) nil nil
'nomini
)
6374 (or (and (not (frame-parameter nil
'unsplittable
))
6375 (let (split-width-threshold)
6376 (setq window
(window--try-to-split-window bottom-window alist
)))
6377 (window--display-buffer
6378 buffer window
'window alist display-buffer-mark-dedicated
))
6379 (and (not (frame-parameter nil
'unsplittable
))
6382 (split-window (window--major-non-side-window))
6384 (window--display-buffer
6385 buffer window
'window alist display-buffer-mark-dedicated
))
6386 (and (setq window bottom-window
)
6387 (not (window-dedicated-p window
))
6388 (window--display-buffer
6389 buffer window
'reuse alist display-buffer-mark-dedicated
)))))
6391 (defun display-buffer-in-previous-window (buffer alist
)
6392 "Display BUFFER in a window previously showing it.
6393 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6394 window is not eligible for reuse.
6396 If ALIST contains a `reusable-frames' entry, its value determines
6397 which frames to search for a reusable window:
6398 nil -- the selected frame (actually the last non-minibuffer frame)
6399 A frame -- just that frame
6400 `visible' -- all visible frames
6401 0 -- all frames on the current terminal
6404 If ALIST contains no `reusable-frames' entry, search just the
6405 selected frame if `display-buffer-reuse-frames' and
6406 `pop-up-frames' are both nil; search all frames on the current
6407 terminal if either of those variables is non-nil.
6409 If ALIST has a `previous-window' entry, the window specified by
6410 that entry will override any other window found by the methods
6411 above, even if that window never showed BUFFER before."
6412 (let* ((alist-entry (assq 'reusable-frames alist
))
6413 (inhibit-same-window
6414 (cdr (assq 'inhibit-same-window alist
)))
6416 (alist-entry (cdr alist-entry
))
6417 ((if (eq pop-up-frames
'graphic-only
)
6421 (display-buffer-reuse-frames 0)
6422 (t (last-nonminibuffer-frame))))
6423 best-window second-best-window window
)
6424 ;; Scan windows whether they have shown the buffer recently.
6426 (dolist (window (window-list-1 (frame-first-window) 'nomini frames
))
6427 (when (and (assq buffer
(window-prev-buffers window
))
6428 (not (window-dedicated-p window
)))
6429 (if (eq window
(selected-window))
6430 (unless inhibit-same-window
6431 (setq second-best-window window
))
6432 (setq best-window window
)
6434 ;; When ALIST has a `previous-window' entry, that entry may override
6435 ;; anything we found so far.
6436 (when (and (setq window
(cdr (assq 'previous-window alist
)))
6437 (window-live-p window
)
6438 (not (window-dedicated-p window
)))
6439 (if (eq window
(selected-window))
6440 (unless inhibit-same-window
6441 (setq second-best-window window
))
6442 (setq best-window window
)))
6443 ;; Return best or second best window found.
6444 (when (setq window
(or best-window second-best-window
))
6445 (window--display-buffer buffer window
'reuse alist
))))
6447 (defun display-buffer-use-some-window (buffer alist
)
6448 "Display BUFFER in an existing window.
6449 Search for a usable window, set that window to the buffer, and
6450 return the window. If no suitable window is found, return nil.
6452 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6453 event that a window in another frame is chosen, avoid raising
6455 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist
)))
6456 (frame (or (window--frame-usable-p (selected-frame))
6457 (window--frame-usable-p (last-nonminibuffer-frame))))
6459 ;; Reuse an existing window.
6460 (or (get-lru-window frame nil not-this-window
)
6461 (let ((window (get-buffer-window buffer
'visible
)))
6462 (unless (and not-this-window
6463 (eq window
(selected-window)))
6465 (get-largest-window 'visible nil not-this-window
)
6466 (let ((window (get-buffer-window buffer
0)))
6467 (unless (and not-this-window
6468 (eq window
(selected-window)))
6470 (get-largest-window 0 nil not-this-window
)))
6471 (quit-restore (and (window-live-p window
)
6472 (window-parameter window
'quit-restore
)))
6473 (quad (nth 1 quit-restore
)))
6474 (when (window-live-p window
)
6475 ;; If the window was used by `display-buffer' before, try to
6476 ;; resize it to its old height but don't signal an error.
6477 (when (and (listp quad
)
6478 (integerp (nth 3 quad
))
6479 (/= (nth 3 quad
) (window-total-height window
)))
6481 (window-resize window
(- (nth 3 quad
) (window-total-height window
)))
6485 (window--display-buffer buffer window
'reuse alist
)
6486 (window--even-window-heights window
)
6487 (unless (cdr (assq 'inhibit-switch-frame alist
))
6488 (window--maybe-raise-frame (window-frame window
)))))))
6490 (defun display-buffer-no-window (_buffer alist
)
6491 "Display BUFFER in no window.
6492 If ALIST has a non-nil `allow-no-window' entry, then don't display
6493 a window at all. This makes possible to override the default action
6494 and avoid displaying the buffer. It is assumed that when the caller
6495 specifies a non-nil `allow-no-window' then it can handle a nil value
6496 returned from `display-buffer' in this case."
6497 (when (cdr (assq 'allow-no-window alist
))
6500 ;;; Display + selection commands:
6501 (defun pop-to-buffer (buffer &optional action norecord
)
6502 "Select buffer BUFFER in some window, preferably a different one.
6503 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6504 is a string not naming an existent buffer, create a buffer with
6505 that name. If BUFFER is nil, choose some other buffer. Return
6508 This uses `display-buffer' as a subroutine. The optional ACTION
6509 argument is passed to `display-buffer' as its ACTION argument.
6510 See `display-buffer' for more information. ACTION is t if called
6511 interactively with a prefix argument, which means to pop to a
6512 window other than the selected one even if the buffer is already
6513 displayed in the selected window.
6515 If the window to show BUFFER is not on the selected
6516 frame, raise that window's frame and give it input focus.
6518 Optional third arg NORECORD non-nil means do not put this buffer
6519 at the front of the list of recently selected ones."
6520 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
6521 (if current-prefix-arg t
)))
6522 (setq buffer
(window-normalize-buffer-to-switch-to buffer
))
6523 ;; This should be done by `select-window' below.
6524 ;; (set-buffer buffer)
6525 (let* ((old-frame (selected-frame))
6526 (window (display-buffer buffer action
))
6527 (frame (window-frame window
)))
6528 ;; If we chose another frame, make sure it gets input focus.
6529 (unless (eq frame old-frame
)
6530 (select-frame-set-input-focus frame norecord
))
6531 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
6532 (select-window window norecord
)
6535 (defun pop-to-buffer-same-window (buffer &optional norecord
)
6536 "Select buffer BUFFER in some window, preferably the same one.
6537 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6538 is a string not naming an existent buffer, create a buffer with
6539 that name. If BUFFER is nil, choose some other buffer. Return
6542 Optional argument NORECORD, if non-nil means do not put this
6543 buffer at the front of the list of recently selected ones.
6545 Unlike `pop-to-buffer', this function prefers using the selected
6546 window over popping up a new window or frame."
6547 (pop-to-buffer buffer display-buffer--same-window-action norecord
))
6549 (defun read-buffer-to-switch (prompt)
6550 "Read the name of a buffer to switch to, prompting with PROMPT.
6551 Return the name of the buffer as a string.
6553 This function is intended for the `switch-to-buffer' family of
6554 commands since these need to omit the name of the current buffer
6555 from the list of completions and default values."
6556 (let ((rbts-completion-table (internal-complete-buffer-except)))
6557 (minibuffer-with-setup-hook
6559 (setq minibuffer-completion-table rbts-completion-table
)
6560 ;; Since rbts-completion-table is built dynamically, we
6561 ;; can't just add it to the default value of
6562 ;; icomplete-with-completion-tables, so we add it
6564 (if (and (boundp 'icomplete-with-completion-tables
)
6565 (listp icomplete-with-completion-tables
))
6566 (set (make-local-variable 'icomplete-with-completion-tables
)
6567 (cons rbts-completion-table
6568 icomplete-with-completion-tables
))))
6569 (read-buffer prompt
(other-buffer (current-buffer))
6570 (confirm-nonexistent-file-or-buffer)))))
6572 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
6573 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
6574 If BUFFER-OR-NAME is nil, return the buffer returned by
6575 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
6576 exists, return that buffer. If no such buffer exists, create a
6577 buffer with the name BUFFER-OR-NAME and return that buffer."
6579 (or (get-buffer buffer-or-name
)
6580 (let ((buffer (get-buffer-create buffer-or-name
)))
6581 (set-buffer-major-mode buffer
)
6585 (defcustom switch-to-buffer-preserve-window-point nil
6586 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
6587 If this is nil, `switch-to-buffer' displays the buffer at that
6588 buffer's `point'. If this is `already-displayed', it tries to
6589 display the buffer at its previous position in the selected
6590 window, provided the buffer is currently displayed in some other
6591 window on any visible or iconified frame. If this is t, it
6592 unconditionally tries to display the buffer at its previous
6593 position in the selected window.
6595 This variable is ignored if the buffer is already displayed in
6596 the selected window or never appeared in it before, or if
6597 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
6599 (const :tag
"Never" nil
)
6600 (const :tag
"If already displayed elsewhere" already-displayed
)
6601 (const :tag
"Always" t
))
6605 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window
)
6606 "Display buffer BUFFER-OR-NAME in the selected window.
6608 WARNING: This is NOT the way to work on another buffer temporarily
6609 within a Lisp program! Use `set-buffer' instead. That avoids
6610 messing with the window-buffer correspondences.
6612 If the selected window cannot display the specified
6613 buffer (e.g. if it is a minibuffer window or strongly dedicated
6614 to another buffer), call `pop-to-buffer' to select the buffer in
6617 If called interactively, read the buffer name using the
6618 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6619 determines whether to request confirmation before creating a new
6622 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
6623 If BUFFER-OR-NAME is a string that does not identify an existing
6624 buffer, create a buffer with that name. If BUFFER-OR-NAME is
6625 nil, switch to the buffer returned by `other-buffer'.
6627 If optional argument NORECORD is non-nil, do not put the buffer
6628 at the front of the buffer list, and do not make the window
6629 displaying it the most recently selected one.
6631 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
6632 must be displayed in the selected window; if that is impossible,
6633 signal an error rather than calling `pop-to-buffer'.
6635 The option `switch-to-buffer-preserve-window-point' can be used
6636 to make the buffer appear at its last position in the selected
6639 Return the buffer switched to."
6641 (list (read-buffer-to-switch "Switch to buffer: ") nil
'force-same-window
))
6642 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name
)))
6644 ;; Don't call set-window-buffer if it's not needed since it
6645 ;; might signal an error (e.g. if the window is dedicated).
6646 ((eq buffer
(window-buffer)))
6647 ((window-minibuffer-p)
6648 (if force-same-window
6649 (user-error "Cannot switch buffers in minibuffer window")
6650 (pop-to-buffer buffer norecord
)))
6651 ((eq (window-dedicated-p) t
)
6652 (if force-same-window
6653 (user-error "Cannot switch buffers in a dedicated window")
6654 (pop-to-buffer buffer norecord
)))
6656 (let* ((entry (assq buffer
(window-prev-buffers)))
6657 (displayed (and (eq switch-to-buffer-preserve-window-point
6659 (get-buffer-window buffer
0))))
6660 (set-window-buffer nil buffer
)
6662 (or (eq switch-to-buffer-preserve-window-point t
)
6664 ;; Try to restore start and point of buffer in the selected
6665 ;; window (Bug#4041).
6666 (set-window-start (selected-window) (nth 1 entry
) t
)
6667 (set-window-point nil
(nth 2 entry
))))))
6670 (select-window (selected-window)))
6671 (set-buffer buffer
)))
6673 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord
)
6674 "Select the buffer specified by BUFFER-OR-NAME in another window.
6675 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6676 nil. Return the buffer switched to.
6678 If called interactively, prompt for the buffer name using the
6679 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6680 determines whether to request confirmation before creating a new
6683 If BUFFER-OR-NAME is a string and does not identify an existing
6684 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6685 nil, switch to the buffer returned by `other-buffer'.
6687 Optional second argument NORECORD non-nil means do not put this
6688 buffer at the front of the list of recently selected ones.
6690 This uses the function `display-buffer' as a subroutine; see its
6691 documentation for additional customization information."
6693 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
6694 (let ((pop-up-windows t
))
6695 (pop-to-buffer buffer-or-name t norecord
)))
6697 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord
)
6698 "Switch to buffer BUFFER-OR-NAME in another frame.
6699 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6700 nil. Return the buffer switched to.
6702 If called interactively, prompt for the buffer name using the
6703 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6704 determines whether to request confirmation before creating a new
6707 If BUFFER-OR-NAME is a string and does not identify an existing
6708 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6709 nil, switch to the buffer returned by `other-buffer'.
6711 Optional second arg NORECORD non-nil means do not put this
6712 buffer at the front of the list of recently selected ones.
6714 This uses the function `display-buffer' as a subroutine; see its
6715 documentation for additional customization information."
6717 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
6718 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord
))
6720 (defun set-window-text-height (window height
)
6721 "Set the height in lines of the text display area of WINDOW to HEIGHT.
6722 WINDOW must be a live window and defaults to the selected one.
6723 HEIGHT doesn't include the mode line or header line, if any, or
6724 any partial-height lines in the text display area.
6726 Note that the current implementation of this function cannot
6727 always set the height exactly, but attempts to be conservative,
6728 by allocating more lines than are actually needed in the case
6729 where some error may be present."
6730 (setq window
(window-normalize-window window t
))
6731 (let ((delta (- height
(window-text-height window
))))
6732 (unless (zerop delta
)
6733 ;; Setting window-min-height to a value like 1 can lead to very
6734 ;; bizarre displays because it also allows Emacs to make *other*
6735 ;; windows one line tall, which means that there's no more space
6736 ;; for the mode line.
6737 (let ((window-min-height (min 2 height
)))
6738 (window-resize window delta
)))))
6740 (defun enlarge-window-horizontally (delta)
6741 "Make selected window DELTA columns wider.
6742 Interactively, if no argument is given, make selected window one
6745 (enlarge-window delta t
))
6747 (defun shrink-window-horizontally (delta)
6748 "Make selected window DELTA columns narrower.
6749 Interactively, if no argument is given, make selected window one
6752 (shrink-window delta t
))
6754 (defun count-screen-lines (&optional beg end count-final-newline window
)
6755 "Return the number of screen lines in the region.
6756 The number of screen lines may be different from the number of actual lines,
6757 due to line breaking, display table, etc.
6759 Optional arguments BEG and END default to `point-min' and `point-max'
6762 If region ends with a newline, ignore it unless optional third argument
6763 COUNT-FINAL-NEWLINE is non-nil.
6765 The optional fourth argument WINDOW specifies the window used for obtaining
6766 parameters such as width, horizontal scrolling, and so on. The default is
6767 to use the selected window's parameters.
6769 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6770 regardless of which buffer is displayed in WINDOW. This makes possible to use
6771 `count-screen-lines' in any buffer, whether or not it is currently displayed
6774 (setq beg
(point-min)))
6776 (setq end
(point-max)))
6782 (narrow-to-region (min beg end
)
6783 (if (and (not count-final-newline
)
6784 (= ?
\n (char-before (max beg end
))))
6787 (goto-char (point-min))
6788 (1+ (vertical-motion (buffer-size) window
))))))
6790 (defun window-buffer-height (window)
6791 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
6792 WINDOW must be a live window and defaults to the selected one."
6793 (setq window
(window-normalize-window window t
))
6794 (with-current-buffer (window-buffer window
)
6796 (count-screen-lines (point-min) (point-max)
6797 ;; If buffer ends with a newline, ignore it when
6798 ;; counting height unless point is after it.
6802 ;;; Resizing windows and frames to fit their contents exactly.
6803 (defcustom fit-window-to-buffer-horizontally nil
6804 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
6805 If this is nil, `fit-window-to-buffer' never resizes windows
6806 horizontally. If this is `only', it can resize windows
6807 horizontally only. Any other value means `fit-window-to-buffer'
6808 can resize windows in both dimensions."
6813 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
6814 ;; counting title bar and outer borders.
6815 (defcustom fit-frame-to-buffer nil
6816 "Non-nil means `fit-frame-to-buffer' can fit a frame to its buffer.
6817 A frame is fit if and only if its root window is a live window
6818 and this option is non-nil. If this is `horizontally', frames
6819 are resized horizontally only. If this is `vertically', frames
6820 are resized vertically only. Any other non-nil value means
6821 frames can be resized in both dimensions. See also
6822 `fit-frame-to-buffer-margins' and `fit-frame-to-buffer-sizes'.
6824 If this is non-nil and a window is the only window of its frame,
6825 `fit-window-to-buffer' will invoke `fit-frame-to-buffer' to fit
6826 the frame to its buffer."
6831 (defcustom fit-frame-to-buffer-margins
'(nil nil nil nil
)
6832 "Margins around frame for `fit-frame-to-buffer'.
6833 This list specifies the numbers of pixels to be left free on the
6834 left, above, the right, and below a frame that shall be fit to
6835 its buffer. The value specified here can be overridden for a
6836 specific frame by that frame's `fit-frame-to-buffer-margins'
6837 parameter, if present.
6839 This variable controls how fitting a frame to the size of its
6840 buffer coordinates with the size of your display. If you don't
6841 specify a value here, the size of the display's workarea is used.
6843 See also `fit-frame-to-buffer-sizes'."
6849 :format
"%[LeftMargin%] %v "
6850 (const :tag
"None" :format
"%t" nil
)
6851 (integer :tag
"Pixels" :size
5))
6855 :format
"%[TopMargin%] %v "
6856 (const :tag
"None" :format
"%t" nil
)
6857 (integer :tag
"Pixels" :size
5))
6861 :format
"%[RightMargin%] %v "
6862 (const :tag
"None" :format
"%t" nil
)
6863 (integer :tag
"Pixels" :size
5))
6867 :format
"%[BottomMargin%] %v "
6868 (const :tag
"None" :format
"%t" nil
)
6869 (integer :tag
"Pixels" :size
5)))
6872 (defcustom fit-frame-to-buffer-sizes
'(nil nil nil nil
)
6873 "Size boundaries of frame for `fit-frame-to-buffer'.
6874 This list specifies the total maximum and minimum lines and
6875 maximum and minimum columns of the root window of any frame that
6876 shall be fit to its buffer. If any of these values is non-nil,
6877 it overrides the corresponding argument of `fit-frame-to-buffer'.
6879 On window systems where the menubar can wrap, fitting a frame to
6880 its buffer may swallow the last line(s). Specifying an
6881 appropriate minimum width value here can avoid such wrapping.
6883 See also `fit-frame-to-buffer-margins'."
6887 :tag
"Maximum Height"
6889 :format
"%[MaxHeight%] %v "
6890 (const :tag
"None" :format
"%t" nil
)
6891 (integer :tag
"Lines" :size
5))
6893 :tag
"Minimum Height"
6895 :format
"%[MinHeight%] %v "
6896 (const :tag
"None" :format
"%t" nil
)
6897 (integer :tag
"Lines" :size
5))
6899 :tag
"Maximum Width"
6901 :format
"%[MaxWidth%] %v "
6902 (const :tag
"None" :format
"%t" nil
)
6903 (integer :tag
"Columns" :size
5))
6905 :tag
"Minimum Width"
6907 :format
"%[MinWidth%] %v\n"
6908 (const :tag
"None" :format
"%t" nil
)
6909 (integer :tag
"Columns" :size
5)))
6912 (declare-function x-display-pixel-height
"xfns.c" (&optional terminal
))
6914 (defun window--sanitize-margin (margin left right
)
6915 "Return MARGIN if it's a number between LEFT and RIGHT."
6916 (when (and (numberp margin
)
6917 (<= left
(- right margin
)) (<= margin right
))
6920 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width
)
6921 "Adjust size of FRAME to display the contents of its buffer exactly.
6922 FRAME can be any live frame and defaults to the selected one.
6923 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
6924 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
6925 FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
6926 `window-min-height' and `window-min-width' respectively.
6928 The option `fit-frame-to-buffer' controls whether this function
6929 has any effect. New position and size of FRAME are additionally
6930 determined by the options `fit-frame-to-buffer-sizes' and
6931 `fit-frame-to-buffer-margins' or the corresponding parameters of
6934 (unless (and (fboundp 'x-display-pixel-height
)
6935 ;; We need the respective sizes now.
6936 (fboundp 'display-monitor-attributes-list
))
6937 (user-error "Cannot resize frame in non-graphic Emacs"))
6938 (setq frame
(window-normalize-frame frame
))
6939 (when (and (window-live-p (frame-root-window frame
))
6941 (or (not window-size-fixed
)
6942 (and (eq window-size-fixed
'height
)
6943 (not (eq fit-frame-to-buffer
'vertically
)))
6944 (and (eq window-size-fixed
'width
)
6945 (not (eq fit-frame-to-buffer
'horizontally
)))))
6946 (with-selected-window (frame-root-window frame
)
6947 (let* ((window (frame-root-window frame
))
6948 (char-width (frame-char-width))
6949 (char-height (frame-char-height))
6950 (monitor-attributes (car (display-monitor-attributes-list
6951 (frame-parameter frame
'display
))))
6952 (geometry (cdr (assq 'geometry monitor-attributes
)))
6953 (display-width (- (nth 2 geometry
) (nth 0 geometry
)))
6954 (display-height (- (nth 3 geometry
) (nth 1 geometry
)))
6955 (workarea (cdr (assq 'workarea monitor-attributes
)))
6957 (margins (or (frame-parameter frame
'fit-frame-to-buffer-margins
)
6958 fit-frame-to-buffer-margins
))
6959 (left-margin (if (nth 0 margins
)
6960 (or (window--sanitize-margin
6961 (nth 0 margins
) 0 display-width
)
6964 (top-margin (if (nth 1 margins
)
6965 (or (window--sanitize-margin
6966 (nth 1 margins
) 0 display-height
)
6969 (workarea-width (nth 2 workarea
))
6970 (right-margin (if (nth 2 margins
)
6972 (or (window--sanitize-margin
6973 (nth 2 margins
) left-margin display-width
)
6976 (workarea-height (nth 3 workarea
))
6977 (bottom-margin (if (nth 3 margins
)
6979 (or (window--sanitize-margin
6980 (nth 3 margins
) top-margin display-height
)
6983 ;; The pixel width of FRAME (which does not include the
6984 ;; window manager's decorations).
6985 (frame-width (frame-pixel-width))
6986 ;; The pixel width of the body of FRAME's root window.
6987 (window-body-width (window-body-width nil t
))
6988 ;; The difference in pixels between total and body width of
6990 (window-extra-width (- (window-pixel-width) window-body-width
))
6991 ;; The difference in pixels between the frame's pixel width
6992 ;; and the window's body width. This is the space we can't
6994 (extra-width (- frame-width window-body-width
))
6995 ;; The maximum width we can use for fitting.
6996 (fit-width (- workarea-width extra-width
))
6997 ;; The pixel position of FRAME's left border. We usually
6998 ;; try to leave this alone.
7000 (let ((left (frame-parameter nil
'left
)))
7002 (funcall (car left
) (cadr left
))
7004 ;; The pixel height of FRAME (which does not include title
7005 ;; line, decorations, and sometimes neither the menu nor
7007 (frame-height (frame-pixel-height))
7008 ;; The pixel height of FRAME's root window (we don't care
7009 ;; about the window's body height since the return value of
7010 ;; `window-text-pixel-size' includes header and mode line).
7011 (window-height (window-pixel-height))
7012 ;; The difference in pixels between the frame's pixel
7013 ;; height and the window's height.
7014 (extra-height (- frame-height window-height
))
7015 ;; When tool-bar-mode is enabled and we just created a new
7016 ;; frame, reserve lines for toolbar resizing. Needed
7017 ;; because for reasons unknown to me Emacs (1) reserves one
7018 ;; line for the toolbar when making the initial frame and
7019 ;; toolbars are enabled, and (2) later adds the remaining
7020 ;; lines needed. Our code runs IN BETWEEN (1) and (2).
7021 ;; YMMV when you're on a system that behaves differently.
7022 (toolbar-extra-height
7023 (let ((quit-restore (window-parameter window
'quit-restore
))
7024 ;; This may have to change when we allow arbitrary
7025 ;; pixel height toolbars.
7026 (lines (tool-bar-height)))
7028 (if (and quit-restore
(eq (car quit-restore
) 'frame
)
7029 (not (zerop lines
)))
7032 ;; The pixel position of FRAME's top border.
7034 (let ((top (frame-parameter nil
'top
)))
7036 (funcall (car top
) (cadr top
))
7038 ;; Sanitize minimum and maximum sizes.
7039 (sizes (or (frame-parameter frame
'fit-frame-to-buffer-sizes
)
7040 fit-frame-to-buffer-sizes
))
7043 ((numberp (nth 0 sizes
)) (* (nth 0 sizes
) char-height
))
7044 ((numberp max-height
) (* max-height char-height
))
7045 (t display-height
)))
7048 ((numberp (nth 1 sizes
)) (* (nth 1 sizes
) char-height
))
7049 ((numberp min-height
) (* min-height char-height
))
7050 (t (* window-min-height char-height
))))
7053 ((numberp (nth 2 sizes
))
7054 (- (* (nth 2 sizes
) char-width
) window-extra-width
))
7055 ((numberp max-width
)
7056 (- (* max-width char-width
) window-extra-width
))
7057 (t display-height
)))
7060 ((numberp (nth 3 sizes
))
7061 (- (* (nth 3 sizes
) char-width
) window-extra-width
))
7062 ((numberp min-width
)
7063 (- (* min-width char-width
) window-extra-width
))
7064 (t (* window-min-width char-width
))))
7065 ;; Note: Currently, for a new frame the sizes of the header
7066 ;; and mode line may be estimated incorrectly
7067 (value (window-text-pixel-size
7068 nil t t workarea-width workarea-height t
))
7069 (width (+ (car value
) (window-right-divider-width)))
7070 (height (+ (cdr value
) (window-bottom-divider-width))))
7071 ;; Don't change height or width when the window's size is fixed
7072 ;; in either direction.
7074 ((eq window-size-fixed
'width
)
7076 ((eq window-size-fixed
'height
)
7078 ;; Fit width to constraints.
7080 (unless frame-resize-pixelwise
7081 ;; Round to character sizes.
7082 (setq width
(* (/ (+ width char-width -
1) char-width
)
7084 ;; Fit to maximum and minimum widths.
7085 (setq width
(max (min width max-width
) min-width
))
7087 (setq width
(+ width extra-width
))
7088 ;; Preserve margins.
7089 (let ((right (+ left width
)))
7091 ((> right right-margin
)
7092 ;; Move frame to left (we don't know its real width).
7093 (setq left
(max left-margin
(- left
(- right right-margin
)))))
7094 ((< left left-margin
)
7095 ;; Move frame to right.
7096 (setq left left-margin
)))))
7097 ;; Fit height to constraints.
7099 (unless frame-resize-pixelwise
7100 (setq height
(* (/ (+ height char-height -
1) char-height
)
7102 ;; Fit to maximum and minimum heights.
7103 (setq height
(max (min height max-height
) min-height
))
7104 ;; Add extra height.
7105 (setq height
(+ height extra-height
))
7106 ;; Preserve margins.
7107 (let ((bottom (+ top height
)))
7109 ((> bottom bottom-margin
)
7110 ;; Move frame up (we don't know its real height).
7111 (setq top
(max top-margin
(- top
(- bottom bottom-margin
)))))
7114 (setq top top-margin
)))))
7116 (set-frame-position frame left top
)
7117 ;; Clumsily try to translate our calculations to what
7118 ;; `set-frame-size' wants.
7120 (setq width
(- (+ (frame-text-width) width
)
7121 extra-width window-body-width
)))
7123 (setq height
(- (+ (frame-text-height) height
)
7124 extra-height window-height
)))
7128 (if frame-resize-pixelwise
7130 (/ width char-width
))
7133 (if frame-resize-pixelwise
7135 (/ height char-height
))
7136 (frame-text-height))
7137 frame-resize-pixelwise
)))))
7139 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width
)
7140 "Adjust size of WINDOW to display its buffer's contents exactly.
7141 WINDOW must be a live window and defaults to the selected one.
7143 If WINDOW is part of a vertical combination, adjust WINDOW's
7144 height. The new height is calculated from the number of lines of
7145 the accessible portion of its buffer. The optional argument
7146 MAX-HEIGHT specifies a maximum height and defaults to the height
7147 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
7148 minimum height and defaults to `window-min-height'. Both
7149 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include the
7150 mode line and header line, if any.
7152 If WINDOW is part of a horizontal combination and the value of
7153 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
7154 WINDOW's height. The new width of WINDOW is calculated from the
7155 maximum length of its buffer's lines that follow the current
7156 start position of WINDOW. The optional argument MAX-WIDTH
7157 specifies a maximum width and defaults to the width of WINDOW's
7158 frame. The optional argument MIN-WIDTH specifies a minimum width
7159 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
7160 are specified in columns and include fringes, margins and
7163 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
7164 If WINDOW is its frame's root window, then if the option
7165 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
7166 adjust the frame's size.
7168 Note that even if this function makes WINDOW large enough to show
7169 _all_ parts of its buffer you might not see the first part when
7170 WINDOW was scrolled. If WINDOW is resized horizontally, you will
7171 not see the top of its buffer unless WINDOW starts at its minimum
7172 accessible position."
7174 (setq window
(window-normalize-window window t
))
7175 (if (eq window
(frame-root-window window
))
7176 (when fit-frame-to-buffer
7177 ;; Fit WINDOW's frame to buffer.
7178 (fit-frame-to-buffer
7179 (window-frame window
)
7180 max-height min-height max-width min-width
))
7181 (with-selected-window window
7182 (let* ((pixelwise window-resize-pixelwise
)
7183 (char-height (frame-char-height))
7184 (char-width (frame-char-width))
7185 (total-height (window-size window nil pixelwise
))
7186 (body-height (window-body-height window pixelwise
))
7187 (body-width (window-body-width window pixelwise
))
7189 ;; Sanitize MIN-HEIGHT.
7190 (if (numberp min-height
)
7191 ;; Can't get smaller than `window-safe-min-height'.
7193 (* char-height min-height
)
7196 (window-safe-min-pixel-height window
)
7197 window-safe-min-height
))
7198 ;; Preserve header and mode line if present.
7200 (* char-height window-min-height
)
7202 (window-min-size nil nil t pixelwise
))))
7204 ;; Sanitize MAX-HEIGHT.
7205 (if (numberp max-height
)
7209 window nil nil nil nil nil pixelwise
))
7211 (* char-height max-height
)
7213 (+ total-height
(window-max-delta
7214 window nil nil nil nil nil pixelwise
))))
7217 ;; If WINDOW is vertically combined, try to resize it
7219 ((and (not (eq fit-window-to-buffer-horizontally
'only
))
7220 (not (window-size-fixed-p window
))
7221 (window-combined-p))
7222 ;; Vertically we always want to fit the entire buffer.
7223 ;; WINDOW'S height can't get larger than its frame's pixel
7224 ;; height. Its width remains fixed.
7225 (setq height
(+ (cdr (window-text-pixel-size
7226 nil nil t nil
(frame-pixel-height) t
))
7227 (window-bottom-divider-width)))
7230 (setq height
(/ (+ height char-height -
1) char-height
)))
7231 (unless (= height total-height
)
7232 (window-resize-no-error
7234 (- (max min-height
(min max-height height
)) total-height
)
7235 nil window pixelwise
)))
7236 ;; If WINDOW is horizontally combined, try to resize it
7238 ((and fit-window-to-buffer-horizontally
7239 (not (window-size-fixed-p window t
))
7240 (window-combined-p nil t
))
7241 (let* ((total-width (window-size window nil pixelwise
))
7243 ;; Sanitize MIN-WIDTH.
7244 (if (numberp min-width
)
7245 ;; Can't get smaller than `window-safe-min-width'.
7247 (* char-width min-width
)
7250 (window-safe-min-pixel-width)
7251 window-safe-min-width
))
7252 ;; Preserve fringes, margins, scrollbars if present.
7254 (* char-width window-min-width
)
7256 (window-min-size nil nil t pixelwise
))))
7258 ;; Sanitize MAX-WIDTH.
7259 (if (numberp max-width
)
7262 nil t nil nil nil nil pixelwise
))
7264 (* char-width max-width
)
7266 (+ total-width
(window-max-delta
7267 nil t nil nil nil nil pixelwise
))))
7268 ;; When fitting vertically, assume that WINDOW's start
7269 ;; position remains unaltered. WINDOW can't get wider
7270 ;; than its frame's pixel width, its height remains
7272 (width (+ (car (window-text-pixel-size
7273 nil
(window-start) (point-max)
7275 ;; Add one char-height to assure that
7276 ;; we're on the safe side. This
7277 ;; overshoots when the first line below
7278 ;; the bottom is wider than the window.
7280 (if pixelwise char-height
1))))
7281 (window-right-divider-width))))
7283 (setq width
(/ (+ width char-width -
1) char-width
)))
7284 (unless (= width body-width
)
7285 (window-resize-no-error
7289 (+ total-width
(- width body-width
))))
7291 t window pixelwise
)))))))))
7293 (defun window-safely-shrinkable-p (&optional window
)
7294 "Return t if WINDOW can be shrunk without shrinking other windows.
7295 WINDOW defaults to the selected window."
7296 (with-selected-window (or window
(selected-window))
7297 (let ((edges (window-edges)))
7298 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
7299 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
7301 (defun shrink-window-if-larger-than-buffer (&optional window
)
7302 "Shrink height of WINDOW if its buffer doesn't need so many lines.
7303 More precisely, shrink WINDOW vertically to be as small as
7304 possible, while still showing the full contents of its buffer.
7305 WINDOW must be a live window and defaults to the selected one.
7307 Do not shrink WINDOW to less than `window-min-height' lines. Do
7308 nothing if the buffer contains more lines than the present window
7309 height, or if some of the window's contents are scrolled out of
7310 view, or if shrinking this window would also shrink another
7311 window, or if the window is the only window of its frame.
7313 Return non-nil if the window was shrunk, nil otherwise."
7315 (setq window
(window-normalize-window window t
))
7316 ;; Make sure that WINDOW is vertically combined and `point-min' is
7317 ;; visible (for whatever reason that's needed). The remaining issues
7318 ;; should be taken care of by `fit-window-to-buffer'.
7319 (when (and (window-combined-p window
)
7320 (pos-visible-in-window-p (point-min) window
))
7321 (fit-window-to-buffer window
(window-total-height window
))))
7323 (defun kill-buffer-and-window ()
7324 "Kill the current buffer and delete the selected window."
7326 (let ((window-to-delete (selected-window))
7327 (buffer-to-kill (current-buffer))
7328 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
7331 (add-hook 'kill-buffer-hook delete-window-hook t t
)
7332 (if (kill-buffer (current-buffer))
7333 ;; If `delete-window' failed before, we rerun it to regenerate
7334 ;; the error so it can be seen in the echo area.
7335 (when (eq (selected-window) window-to-delete
)
7337 ;; If the buffer is not dead for some reason (probably because
7338 ;; of a `quit' signal), remove the hook again.
7340 (with-current-buffer buffer-to-kill
7341 (remove-hook 'kill-buffer-hook delete-window-hook t
))))))
7344 (defvar recenter-last-op nil
7345 "Indicates the last recenter operation performed.
7346 Possible values: `top', `middle', `bottom', integer or float numbers.")
7348 (defcustom recenter-positions
'(middle top bottom
)
7349 "Cycling order for `recenter-top-bottom'.
7350 A list of elements with possible values `top', `middle', `bottom',
7351 integer or float numbers that define the cycling order for
7352 the command `recenter-top-bottom'.
7354 Top and bottom destinations are `scroll-margin' lines from the true
7355 window top and bottom. Middle redraws the frame and centers point
7356 vertically within the window. Integer number moves current line to
7357 the specified absolute window-line. Float number between 0.0 and 1.0
7358 means the percentage of the screen space from the top. The default
7359 cycling order is middle -> top -> bottom."
7360 :type
'(repeat (choice
7361 (const :tag
"Top" top
)
7362 (const :tag
"Middle" middle
)
7363 (const :tag
"Bottom" bottom
)
7364 (integer :tag
"Line number")
7365 (float :tag
"Percentage")))
7369 (defun recenter-top-bottom (&optional arg
)
7370 "Move current buffer line to the specified window line.
7371 With no prefix argument, successive calls place point according
7372 to the cycling order defined by `recenter-positions'.
7374 A prefix argument is handled like `recenter':
7375 With numeric prefix ARG, move current line to window-line ARG.
7376 With plain `C-u', move current line to window center."
7379 (arg (recenter arg
)) ; Always respect ARG.
7381 (setq recenter-last-op
7382 (if (eq this-command last-command
)
7383 (car (or (cdr (member recenter-last-op recenter-positions
))
7384 recenter-positions
))
7385 (car recenter-positions
)))
7386 (let ((this-scroll-margin
7387 (min (max 0 scroll-margin
)
7388 (truncate (/ (window-body-height) 4.0)))))
7389 (cond ((eq recenter-last-op
'middle
)
7391 ((eq recenter-last-op
'top
)
7392 (recenter this-scroll-margin
))
7393 ((eq recenter-last-op
'bottom
)
7394 (recenter (- -
1 this-scroll-margin
)))
7395 ((integerp recenter-last-op
)
7396 (recenter recenter-last-op
))
7397 ((floatp recenter-last-op
)
7398 (recenter (round (* recenter-last-op
(window-height))))))))))
7400 (define-key global-map
[?\C-l
] 'recenter-top-bottom
)
7402 (defun move-to-window-line-top-bottom (&optional arg
)
7403 "Position point relative to window.
7405 With a prefix argument ARG, acts like `move-to-window-line'.
7407 With no argument, positions point at center of window.
7408 Successive calls position point at positions defined
7409 by `recenter-positions'."
7412 (arg (move-to-window-line arg
)) ; Always respect ARG.
7414 (setq recenter-last-op
7415 (if (eq this-command last-command
)
7416 (car (or (cdr (member recenter-last-op recenter-positions
))
7417 recenter-positions
))
7418 (car recenter-positions
)))
7419 (let ((this-scroll-margin
7420 (min (max 0 scroll-margin
)
7421 (truncate (/ (window-body-height) 4.0)))))
7422 (cond ((eq recenter-last-op
'middle
)
7423 (call-interactively 'move-to-window-line
))
7424 ((eq recenter-last-op
'top
)
7425 (move-to-window-line this-scroll-margin
))
7426 ((eq recenter-last-op
'bottom
)
7427 (move-to-window-line (- -
1 this-scroll-margin
)))
7428 ((integerp recenter-last-op
)
7429 (move-to-window-line recenter-last-op
))
7430 ((floatp recenter-last-op
)
7431 (move-to-window-line (round (* recenter-last-op
(window-height))))))))))
7433 (define-key global-map
[?\M-r
] 'move-to-window-line-top-bottom
)
7435 ;;; Scrolling commands.
7437 ;;; Scrolling commands which do not signal errors at top/bottom
7438 ;;; of buffer at first key-press (instead move to top/bottom
7441 (defcustom scroll-error-top-bottom nil
7442 "Move point to top/bottom of buffer before signaling a scrolling error.
7443 A value of nil means just signal an error if no more scrolling possible.
7444 A value of t means point moves to the beginning or the end of the buffer
7445 \(depending on scrolling direction) when no more scrolling possible.
7446 When point is already on that position, then signal an error."
7451 (defun scroll-up-command (&optional arg
)
7452 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
7453 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
7454 scroll window further, move cursor to the bottom line.
7455 When point is already on that position, then signal an error.
7456 A near full screen is `next-screen-context-lines' less than a full screen.
7457 Negative ARG means scroll downward.
7458 If ARG is the atom `-', scroll downward by nearly full screen."
7461 ((null scroll-error-top-bottom
)
7464 (scroll-down-command nil
))
7465 ((< (prefix-numeric-value arg
) 0)
7466 (scroll-down-command (- (prefix-numeric-value arg
))))
7468 (scroll-up arg
)) ; signal error
7474 ;; When scrolling by ARG lines can't be done,
7475 ;; move by ARG lines instead.
7477 ;; When ARG is nil for full-screen scrolling,
7478 ;; move to the bottom of the buffer.
7479 (goto-char (point-max))))))))
7481 (put 'scroll-up-command
'scroll-command t
)
7483 (defun scroll-down-command (&optional arg
)
7484 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
7485 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
7486 scroll window further, move cursor to the top line.
7487 When point is already on that position, then signal an error.
7488 A near full screen is `next-screen-context-lines' less than a full screen.
7489 Negative ARG means scroll upward.
7490 If ARG is the atom `-', scroll upward by nearly full screen."
7493 ((null scroll-error-top-bottom
)
7496 (scroll-up-command nil
))
7497 ((< (prefix-numeric-value arg
) 0)
7498 (scroll-up-command (- (prefix-numeric-value arg
))))
7500 (scroll-down arg
)) ; signal error
7504 (beginning-of-buffer
7506 ;; When scrolling by ARG lines can't be done,
7507 ;; move by ARG lines instead.
7508 (forward-line (- arg
))
7509 ;; When ARG is nil for full-screen scrolling,
7510 ;; move to the top of the buffer.
7511 (goto-char (point-min))))))))
7513 (put 'scroll-down-command
'scroll-command t
)
7515 ;;; Scrolling commands which scroll a line instead of full screen.
7517 (defun scroll-up-line (&optional arg
)
7518 "Scroll text of selected window upward ARG lines; or one line if no ARG.
7519 If ARG is omitted or nil, scroll upward by one line.
7520 This is different from `scroll-up-command' that scrolls a full screen."
7522 (scroll-up (or arg
1)))
7524 (put 'scroll-up-line
'scroll-command t
)
7526 (defun scroll-down-line (&optional arg
)
7527 "Scroll text of selected window down ARG lines; or one line if no ARG.
7528 If ARG is omitted or nil, scroll down by one line.
7529 This is different from `scroll-down-command' that scrolls a full screen."
7531 (scroll-down (or arg
1)))
7533 (put 'scroll-down-line
'scroll-command t
)
7536 (defun scroll-other-window-down (&optional lines
)
7537 "Scroll the \"other window\" down.
7538 For more details, see the documentation for `scroll-other-window'."
7540 (scroll-other-window
7541 ;; Just invert the argument's meaning.
7542 ;; We can do that without knowing which window it will be.
7543 (if (eq lines
'-
) nil
7545 (- (prefix-numeric-value lines
))))))
7547 (defun beginning-of-buffer-other-window (arg)
7548 "Move point to the beginning of the buffer in the other window.
7549 Leave mark at previous position.
7550 With arg N, put point N/10 of the way from the true beginning."
7552 (let ((orig-window (selected-window))
7553 (window (other-window-for-scrolling)))
7554 ;; We use unwind-protect rather than save-window-excursion
7555 ;; because the latter would preserve the things we want to change.
7558 (select-window window
)
7559 ;; Set point and mark in that window's buffer.
7561 (beginning-of-buffer arg
))
7562 ;; Set point accordingly.
7564 (select-window orig-window
))))
7566 (defun end-of-buffer-other-window (arg)
7567 "Move point to the end of the buffer in the other window.
7568 Leave mark at previous position.
7569 With arg N, put point N/10 of the way from the true end."
7571 ;; See beginning-of-buffer-other-window for comments.
7572 (let ((orig-window (selected-window))
7573 (window (other-window-for-scrolling)))
7576 (select-window window
)
7578 (end-of-buffer arg
))
7580 (select-window orig-window
))))
7582 (defvar mouse-autoselect-window-timer nil
7583 "Timer used by delayed window autoselection.")
7585 (defvar mouse-autoselect-window-position nil
7586 "Last mouse position recorded by delayed window autoselection.")
7588 (defvar mouse-autoselect-window-window nil
7589 "Last window recorded by delayed window autoselection.")
7591 (defvar mouse-autoselect-window-state nil
7592 "When non-nil, special state of delayed window autoselection.
7593 Possible values are `suspend' (suspend autoselection after a menu or
7594 scrollbar interaction) and `select' (the next invocation of
7595 `handle-select-window' shall select the window immediately).")
7597 (defun mouse-autoselect-window-cancel (&optional force
)
7598 "Cancel delayed window autoselection.
7599 Optional argument FORCE means cancel unconditionally."
7600 (unless (and (not force
)
7601 ;; Don't cancel for select-window or select-frame events
7602 ;; or when the user drags a scroll bar.
7603 (or (memq this-command
7604 '(handle-select-window handle-switch-frame
))
7605 (and (eq this-command
'scroll-bar-toolkit-scroll
)
7606 (memq (nth 4 (event-end last-input-event
))
7607 '(handle end-scroll
)))))
7608 (setq mouse-autoselect-window-state nil
)
7609 (when (timerp mouse-autoselect-window-timer
)
7610 (cancel-timer mouse-autoselect-window-timer
))
7611 (remove-hook 'pre-command-hook
'mouse-autoselect-window-cancel
)))
7613 (defun mouse-autoselect-window-start (mouse-position &optional window suspend
)
7614 "Start delayed window autoselection.
7615 MOUSE-POSITION is the last position where the mouse was seen as returned
7616 by `mouse-position'. Optional argument WINDOW non-nil denotes the
7617 window where the mouse was seen. Optional argument SUSPEND non-nil
7618 means suspend autoselection."
7619 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
7620 (setq mouse-autoselect-window-position mouse-position
)
7621 (when window
(setq mouse-autoselect-window-window window
))
7622 (setq mouse-autoselect-window-state
(when suspend
'suspend
))
7623 ;; Install timer which runs `mouse-autoselect-window-select' after
7624 ;; `mouse-autoselect-window' seconds.
7625 (setq mouse-autoselect-window-timer
7627 (abs mouse-autoselect-window
) nil
'mouse-autoselect-window-select
)))
7629 (defun mouse-autoselect-window-select ()
7630 "Select window with delayed window autoselection.
7631 If the mouse position has stabilized in a non-selected window, select
7632 that window. The minibuffer window is selected only if the minibuffer
7633 is active. This function is run by `mouse-autoselect-window-timer'."
7635 (let* ((mouse-position (mouse-position))
7638 (window-at (cadr mouse-position
) (cddr mouse-position
)
7639 (car mouse-position
)))))
7641 ((or (and (fboundp 'menu-or-popup-active-p
) (menu-or-popup-active-p))
7643 (let ((coords (coordinates-in-window-p
7644 (cdr mouse-position
) window
)))
7645 (and (not (consp coords
))
7646 (not (memq coords
'(left-margin right-margin
)))))))
7647 ;; A menu / popup dialog is active or the mouse is not on the
7648 ;; text region of WINDOW: Suspend autoselection temporarily.
7649 (mouse-autoselect-window-start mouse-position nil t
))
7650 ((eq mouse-autoselect-window-state
'suspend
)
7651 ;; Delayed autoselection was temporarily suspended, reenable it.
7652 (mouse-autoselect-window-start mouse-position
))
7653 ((and window
(not (eq window
(selected-window)))
7654 (or (not (numberp mouse-autoselect-window
))
7655 (and (> mouse-autoselect-window
0)
7656 ;; If `mouse-autoselect-window' is positive, select
7657 ;; window if the window is the same as before.
7658 (eq window mouse-autoselect-window-window
))
7659 ;; Otherwise select window if the mouse is at the same
7660 ;; position as before. Observe that the first test after
7661 ;; starting autoselection usually fails since the value of
7662 ;; `mouse-autoselect-window-position' recorded there is the
7663 ;; position where the mouse has entered the new window and
7664 ;; not necessarily where the mouse has stopped moving.
7665 (equal mouse-position mouse-autoselect-window-position
))
7666 ;; The minibuffer is a candidate window if it's active.
7667 (or (not (window-minibuffer-p window
))
7668 (eq window
(active-minibuffer-window))))
7669 ;; Mouse position has stabilized in non-selected window: Cancel
7670 ;; delayed autoselection and try to select that window.
7671 (mouse-autoselect-window-cancel t
)
7672 ;; Select window where mouse appears unless the selected window is the
7673 ;; minibuffer. Use `unread-command-events' in order to execute pre-
7674 ;; and post-command hooks and trigger idle timers. To avoid delaying
7675 ;; autoselection again, set `mouse-autoselect-window-state'."
7676 (unless (window-minibuffer-p)
7677 (setq mouse-autoselect-window-state
'select
)
7678 (setq unread-command-events
7679 (cons (list 'select-window
(list window
))
7680 unread-command-events
))))
7681 ((or (and window
(eq window
(selected-window)))
7682 (not (numberp mouse-autoselect-window
))
7683 (equal mouse-position mouse-autoselect-window-position
))
7684 ;; Mouse position has either stabilized in the selected window or at
7685 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
7686 (mouse-autoselect-window-cancel t
))
7688 ;; Mouse position has not stabilized yet, resume delayed
7690 (mouse-autoselect-window-start mouse-position window
))))))
7692 (defun handle-select-window (event)
7693 "Handle select-window events."
7695 (let ((window (posn-window (event-start event
))))
7696 (unless (or (not (window-live-p window
))
7697 ;; Don't switch if we're currently in the minibuffer.
7698 ;; This tries to work around problems where the
7699 ;; minibuffer gets unselected unexpectedly, and where
7700 ;; you then have to move your mouse all the way down to
7701 ;; the minibuffer to select it.
7702 (window-minibuffer-p)
7703 ;; Don't switch to minibuffer window unless it's active.
7704 (and (window-minibuffer-p window
)
7705 (not (minibuffer-window-active-p window
)))
7706 ;; Don't switch when autoselection shall be delayed.
7707 (and (numberp mouse-autoselect-window
)
7708 (not (zerop mouse-autoselect-window
))
7709 (not (eq mouse-autoselect-window-state
'select
))
7711 ;; Cancel any delayed autoselection.
7712 (mouse-autoselect-window-cancel t
)
7713 ;; Start delayed autoselection from current mouse
7714 ;; position and window.
7715 (mouse-autoselect-window-start (mouse-position) window
)
7716 ;; Executing a command cancels delayed autoselection.
7718 'pre-command-hook
'mouse-autoselect-window-cancel
))))
7719 (when mouse-autoselect-window
7720 ;; Reset state of delayed autoselection.
7721 (setq mouse-autoselect-window-state nil
)
7722 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
7723 (run-hooks 'mouse-leave-buffer-hook
))
7726 (select-window window
))))
7728 (defun truncated-partial-width-window-p (&optional window
)
7729 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
7730 WINDOW must be a live window and defaults to the selected one.
7731 Return nil if WINDOW is not a partial-width window
7732 (regardless of the value of `truncate-lines').
7733 Otherwise, consult the value of `truncate-partial-width-windows'
7734 for the buffer shown in WINDOW."
7735 (setq window
(window-normalize-window window t
))
7736 (unless (window-full-width-p window
)
7737 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
7738 (window-buffer window
))))
7739 (if (integerp t-p-w-w
)
7740 (< (window-width window
) t-p-w-w
)
7743 ;; Some of these are in tutorial--default-keys, so update that if you
7745 (define-key ctl-x-map
"0" 'delete-window
)
7746 (define-key ctl-x-map
"1" 'delete-other-windows
)
7747 (define-key ctl-x-map
"2" 'split-window-below
)
7748 (define-key ctl-x-map
"3" 'split-window-right
)
7749 (define-key ctl-x-map
"o" 'other-window
)
7750 (define-key ctl-x-map
"^" 'enlarge-window
)
7751 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
7752 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
7753 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
7754 (define-key ctl-x-map
"+" 'balance-windows
)
7755 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
7757 ;;; window.el ends here