1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2013 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Window tree functions.
30 (defun internal--before-save-selected-window ()
31 (cons (selected-window)
32 ;; We save and restore all frames' selected windows, because
33 ;; `select-window' can change the frame-selected-window of
34 ;; whatever frame that window is in. Each text terminal's
35 ;; top-frame is preserved by putting it last in the list.
37 (mapcar (lambda (terminal)
38 (let ((frames (frames-on-display-list terminal
))
39 (top-frame (tty-top-frame terminal
))
44 (delq top-frame frames
))))
46 (push (cons f
(frame-selected-window f
))
51 (defun internal--after-save-selected-window (state)
52 (dolist (elt (cdr state
))
53 (and (frame-live-p (car elt
))
54 (window-live-p (cdr elt
))
55 (set-frame-selected-window (car elt
) (cdr elt
) 'norecord
)))
56 (when (window-live-p (car state
))
57 (select-window (car state
) 'norecord
)))
59 (defmacro save-selected-window
(&rest body
)
60 "Execute BODY, then select the previously selected window.
61 The value returned is the value of the last form in BODY.
63 This macro saves and restores the selected window, as well as the
64 selected window in each frame. If the previously selected window
65 is no longer live, then whatever window is selected at the end of
66 BODY remains selected. If the previously selected window of some
67 frame is no longer live at the end of BODY, that frame's selected
70 This macro saves and restores the current buffer, since otherwise
71 its normal operation could make a different buffer current. The
72 order of recently selected windows and the buffer list ordering
73 are not altered by this macro (unless they are altered in BODY)."
74 (declare (indent 0) (debug t
))
75 `(let ((save-selected-window--state (internal--before-save-selected-window)))
79 (internal--after-save-selected-window save-selected-window--state
)))))
81 (defvar temp-buffer-window-setup-hook nil
82 "Normal hook run by `with-temp-buffer-window' before buffer display.
83 This hook is run by `with-temp-buffer-window' with the buffer to be
86 (defvar temp-buffer-window-show-hook nil
87 "Normal hook run by `with-temp-buffer-window' after buffer display.
88 This hook is run by `with-temp-buffer-window' with the buffer
89 displayed and current and its window selected.")
91 (defun temp-buffer-window-setup (buffer-or-name)
92 "Set up temporary buffer specified by BUFFER-OR-NAME.
94 (let ((old-dir default-directory
)
95 (buffer (get-buffer-create buffer-or-name
)))
96 (with-current-buffer buffer
97 (kill-all-local-variables)
98 (setq default-directory old-dir
)
100 (setq buffer-read-only nil
)
101 (setq buffer-file-name nil
)
102 (setq buffer-undo-list t
)
103 (let ((inhibit-read-only t
)
104 (inhibit-modification-hooks t
))
106 (run-hooks 'temp-buffer-window-setup-hook
))
107 ;; Return the buffer.
110 (defun temp-buffer-window-show (&optional buffer action
)
111 "Show temporary buffer BUFFER in a window.
112 Return the window showing BUFFER. Pass ACTION as action argument
113 to `display-buffer'."
115 (with-current-buffer buffer
116 (set-buffer-modified-p nil
)
117 (setq buffer-read-only t
)
118 (goto-char (point-min))
119 (when (let ((window-combination-limit
120 ;; When `window-combination-limit' equals
121 ;; `temp-buffer' or `temp-buffer-resize' and
122 ;; `temp-buffer-resize-mode' is enabled in this
123 ;; buffer bind it to t so resizing steals space
124 ;; preferably from the window that was split.
125 (if (or (eq window-combination-limit
'temp-buffer
)
126 (and (eq window-combination-limit
128 temp-buffer-resize-mode
))
130 window-combination-limit
)))
131 (setq window
(display-buffer buffer action
)))
132 (setq frame
(window-frame window
))
133 (unless (eq frame
(selected-frame))
135 (setq minibuffer-scroll-window window
)
136 (set-window-hscroll window
0)
137 (with-selected-window window
138 (run-hooks 'temp-buffer-window-show-hook
)
139 (when temp-buffer-resize-mode
140 (resize-temp-buffer-window window
)))
141 ;; Return the window.
144 ;; Doc is very similar to with-output-to-temp-buffer.
145 (defmacro with-temp-buffer-window
(buffer-or-name action quit-function
&rest body
)
146 "Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
147 BUFFER-OR-NAME must specify either a live buffer, or the name of a
148 buffer (if it does not exist, this macro creates it).
150 This construct makes buffer BUFFER-OR-NAME empty before running BODY.
151 It does not make the buffer current for BODY.
152 Instead it binds `standard-output' to that buffer, so that output
153 generated with `prin1' and similar functions in BODY goes into
156 At the end of BODY, this marks the specified buffer unmodified and
157 read-only, and displays it in a window (but does not select it, or make
158 the buffer current). The display happens by calling `display-buffer'
159 with the ACTION argument. If `temp-buffer-resize-mode' is enabled,
160 the relevant window shrinks automatically.
162 This returns the value returned by BODY, unless QUIT-FUNCTION specifies
163 a function. In that case, it runs the function with two arguments -
164 the window showing the specified buffer and the value returned by
165 BODY - and returns the value returned by that function.
167 If the buffer is displayed on a new frame, the window manager may
168 decide to select that frame. In that case, it's usually a good
169 strategy if QUIT-FUNCTION selects the window showing the buffer
170 before reading any value from the minibuffer; for example, when
171 asking a `yes-or-no-p' question.
173 This runs the hook `temp-buffer-window-setup-hook' before BODY,
174 with the specified buffer temporarily current. It runs the
175 hook `temp-buffer-window-show-hook' after displaying the buffer,
176 with that buffer temporarily current, and the window that was used to
177 display it temporarily selected.
179 This construct is similar to `with-output-to-temp-buffer', but
180 runs different hooks. In particular, it does not run
181 `temp-buffer-setup-hook', which usually puts the buffer in Help mode.
182 Also, it does not call `temp-buffer-show-function' (the ACTION
183 argument replaces this)."
185 (let ((buffer (make-symbol "buffer"))
186 (window (make-symbol "window"))
187 (value (make-symbol "value")))
188 `(let* ((,buffer
(temp-buffer-window-setup ,buffer-or-name
))
189 (standard-output ,buffer
)
191 (with-current-buffer ,buffer
192 (setq ,value
(progn ,@body
))
193 (setq ,window
(temp-buffer-window-show ,buffer
,action
)))
195 (if (functionp ,quit-function
)
196 (funcall ,quit-function
,window
,value
)
199 ;; The following two functions are like `window-next-sibling' and
200 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
201 ;; they don't substitute the selected window for nil), and they return
202 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
203 ;; a minibuffer window).
204 (defun window-right (window)
205 "Return WINDOW's right sibling.
206 Return nil if WINDOW is the root window of its frame. WINDOW can
208 (and window
(window-parent window
) (window-next-sibling window
)))
210 (defun window-left (window)
211 "Return WINDOW's left sibling.
212 Return nil if WINDOW is the root window of its frame. WINDOW can
214 (and window
(window-parent window
) (window-prev-sibling window
)))
216 (defun window-child (window)
217 "Return WINDOW's first child window.
218 WINDOW can be any window."
219 (or (window-top-child window
) (window-left-child window
)))
221 (defun window-child-count (window)
222 "Return number of WINDOW's child windows.
223 WINDOW can be any window."
225 (when (and (windowp window
) (setq window
(window-child window
)))
227 (setq count
(1+ count
))
228 (setq window
(window-next-sibling window
))))
231 (defun window-last-child (window)
232 "Return last child window of WINDOW.
233 WINDOW can be any window."
234 (when (and (windowp window
) (setq window
(window-child window
)))
235 (while (window-next-sibling window
)
236 (setq window
(window-next-sibling window
))))
239 (defun window-normalize-buffer (buffer-or-name)
240 "Return buffer specified by BUFFER-OR-NAME.
241 BUFFER-OR-NAME must be either a buffer or a string naming a live
242 buffer and defaults to the current buffer."
244 ((not buffer-or-name
)
246 ((bufferp buffer-or-name
)
247 (if (buffer-live-p buffer-or-name
)
249 (error "Buffer %s is not a live buffer" buffer-or-name
)))
250 ((get-buffer buffer-or-name
))
252 (error "No such buffer %s" buffer-or-name
))))
254 (defun window-normalize-frame (frame)
255 "Return frame specified by FRAME.
256 FRAME must be a live frame and defaults to the selected frame."
258 (if (frame-live-p frame
)
260 (error "%s is not a live frame" frame
))
263 (defun window-normalize-window (window &optional live-only
)
264 "Return the window specified by WINDOW.
265 If WINDOW is nil, return the selected window. Otherwise, if
266 WINDOW is a live or an internal window, return WINDOW; if
267 LIVE-ONLY is non-nil, return WINDOW for a live window only.
268 Otherwise, signal an error."
273 (if (window-live-p window
)
275 (error "%s is not a live window" window
)))
276 ((window-valid-p window
)
279 (error "%s is not a valid window" window
))))
281 ;; Maybe this should go to frame.el.
282 (defun frame-char-size (&optional window-or-frame horizontal
)
283 "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
284 If WINDOW-OR-FRAME is a live frame, return the value of
285 `frame-char-height' for that frame. If WINDOW-OR-FRAME is a
286 valid window, return the value of `frame-char-height' for that
287 window's frame. In any other case, return the value of
288 `frame-char-height' for the selected frame.
290 Optional argument HORIZONTAL non-nil means to return the value of
291 `frame-char-width' for WINDOW-OR-FRAME."
294 ((window-valid-p window-or-frame
)
295 (window-frame window-or-frame
))
296 ((frame-live-p window-or-frame
)
298 (t (selected-frame)))))
300 (frame-char-width frame
)
301 (frame-char-height frame
))))
303 (defvar ignore-window-parameters nil
304 "If non-nil, standard functions ignore window parameters.
305 The functions currently affected by this are `split-window',
306 `delete-window', `delete-other-windows' and `other-window'.
308 An application may bind this to a non-nil value around calls to
309 these functions to inhibit processing of window parameters.")
311 ;; This must go to C, finally (or get removed).
312 (defconst window-safe-min-height
1
313 "The absolute minimum number of lines of any window.
314 Anything less might crash Emacs.")
316 (defun window-safe-min-pixel-height (&optional window
)
317 "Return the absolute minimum pixel height of WINDOW."
318 (* window-safe-min-height
319 (frame-char-size (window-normalize-window window
))))
321 (defcustom window-min-height
4
322 "The minimum number of lines of any window.
323 The value has to accommodate a mode- or header-line if present.
324 A value less than `window-safe-min-height' is ignored. The value
325 of this variable is honored when windows are resized or split.
327 Applications should never rebind this variable. To resize a
328 window to a height less than the one specified here, an
329 application should instead call `window-resize' with a non-nil
330 IGNORE argument. In order to have `split-window' make a window
331 shorter, explicitly specify the SIZE argument of that function."
336 (defun window-min-pixel-height (&optional window
)
337 "Return the minimum pixel height of window WINDOW."
338 (* (max window-min-height window-safe-min-height
)
339 (frame-char-size window
)))
341 ;; This must go to C, finally (or get removed).
342 (defconst window-safe-min-width
2
343 "The absolute minimum number of columns of a window.
344 Anything less might crash Emacs.")
346 (defun window-safe-min-pixel-width (&optional window
)
347 "Return the absolute minimum pixel width of WINDOW."
348 (* window-safe-min-width
349 (frame-char-size (window-normalize-window window
) t
)))
351 (defcustom window-min-width
10
352 "The minimum number of columns of any window.
353 The value has to accommodate margins, fringes, or scrollbars if
354 present. A value less than `window-safe-min-width' is ignored.
355 The value of this variable is honored when windows are resized or
358 Applications should never rebind this variable. To resize a
359 window to a width less than the one specified here, an
360 application should instead call `window-resize' with a non-nil
361 IGNORE argument. In order to have `split-window' make a window
362 narrower, explicitly specify the SIZE argument of that function."
367 (defun window-min-pixel-width (&optional window
)
368 "Return the minimum pixel width of window WINDOW."
369 (* (max window-min-width window-safe-min-width
)
370 (frame-char-size window t
)))
372 (defun window-safe-min-pixel-size (&optional window horizontal
)
373 "Return the absolute minimum pixel height of WINDOW.
374 Optional argument HORIZONTAL non-nil means return the absolute
375 minimum pixel width of WINDOW."
377 (window-safe-min-pixel-width window
)
378 (window-safe-min-pixel-height window
)))
380 (defun window-combined-p (&optional window horizontal
)
381 "Return non-nil if WINDOW has siblings in a given direction.
382 WINDOW must be a valid window and defaults to the selected one.
384 HORIZONTAL determines a direction for the window combination. If
385 HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
386 a vertical window combination. If HORIZONTAL is non-nil, return
387 non-nil if WINDOW is part of a horizontal window combination."
388 (setq window
(window-normalize-window window
))
389 (let ((parent (window-parent window
)))
392 (window-left-child parent
)
393 (window-top-child parent
)))))
395 (defun window-combination-p (&optional window horizontal
)
396 "Return WINDOW's first child if WINDOW is a vertical combination.
397 WINDOW can be any window and defaults to the selected one.
398 Optional argument HORIZONTAL non-nil means return WINDOW's first
399 child if WINDOW is a horizontal combination."
400 (setq window
(window-normalize-window window
))
402 (window-left-child window
)
403 (window-top-child window
)))
405 (defun window-combinations (window &optional horizontal
)
406 "Return largest number of windows vertically arranged within WINDOW.
407 WINDOW must be a valid window and defaults to the selected one.
408 If HORIZONTAL is non-nil, return the largest number of
409 windows horizontally arranged within WINDOW."
410 (setq window
(window-normalize-window window
))
412 ((window-live-p window
)
413 ;; If WINDOW is live, return 1.
416 (window-left-child window
)
417 (window-top-child window
))
418 ;; If WINDOW is iso-combined, return the sum of the values for all
419 ;; child windows of WINDOW.
420 (let ((child (window-child window
))
424 (+ (window-combinations child horizontal
)
426 (setq child
(window-right child
)))
429 ;; If WINDOW is not iso-combined, return the maximum value of any
430 ;; child window of WINDOW.
431 (let ((child (window-child window
))
435 (max (window-combinations child horizontal
)
437 (setq child
(window-right child
)))
440 (defun walk-window-tree-1 (fun walk-window-tree-window any
&optional sub-only
)
441 "Helper function for `walk-window-tree' and `walk-window-subtree'."
442 (let (walk-window-tree-buffer)
443 (while walk-window-tree-window
444 (setq walk-window-tree-buffer
445 (window-buffer walk-window-tree-window
))
446 (when (or walk-window-tree-buffer any
)
447 (funcall fun walk-window-tree-window
))
448 (unless walk-window-tree-buffer
450 fun
(window-left-child walk-window-tree-window
) any
)
452 fun
(window-top-child walk-window-tree-window
) any
))
454 (setq walk-window-tree-window nil
)
455 (setq walk-window-tree-window
456 (window-right walk-window-tree-window
))))))
458 (defun walk-window-tree (fun &optional frame any minibuf
)
459 "Run function FUN on each live window of FRAME.
460 FUN must be a function with one argument - a window. FRAME must
461 be a live frame and defaults to the selected one. ANY, if
462 non-nil, means to run FUN on all live and internal windows of
465 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
466 window even if it isn't active. MINIBUF nil or omitted means run
467 FUN on FRAME's minibuffer window only if it's active. In both
468 cases the minibuffer window must be part of FRAME. MINIBUF
469 neither nil nor t means never run FUN on the minibuffer window.
471 This function performs a pre-order, depth-first traversal of the
472 window tree. If FUN changes the window tree, the result is
474 (setq frame
(window-normalize-frame frame
))
475 (walk-window-tree-1 fun
(frame-root-window frame
) any
)
476 (when (memq minibuf
'(nil t
))
477 ;; Run FUN on FRAME's minibuffer window if requested.
478 (let ((minibuffer-window (minibuffer-window frame
)))
479 (when (and (window-live-p minibuffer-window
)
480 (eq (window-frame minibuffer-window
) frame
)
482 (minibuffer-window-active-p minibuffer-window
)))
483 (funcall fun minibuffer-window
)))))
485 (defun walk-window-subtree (fun &optional window any
)
486 "Run function FUN on the subtree of windows rooted at WINDOW.
487 WINDOW defaults to the selected window. FUN must be a function
488 with one argument - a window. By default, run FUN only on live
489 windows of the subtree. If the optional argument ANY is non-nil,
490 run FUN on all live and internal windows of the subtree. If
491 WINDOW is live, run FUN on WINDOW only.
493 This function performs a pre-order, depth-first traversal of the
494 subtree rooted at WINDOW. If FUN changes that tree, the result
496 (setq window
(window-normalize-window window
))
497 (walk-window-tree-1 fun window any t
))
499 (defun window-with-parameter (parameter &optional value frame any minibuf
)
500 "Return first window on FRAME with PARAMETER non-nil.
501 FRAME defaults to the selected frame. Optional argument VALUE
502 non-nil means only return a window whose window-parameter value
503 for PARAMETER equals VALUE (comparison is done with `equal').
504 Optional argument ANY non-nil means consider internal windows
507 Optional argument MINIBUF t means consider FRAME's minibuffer
508 window even if it isn't active. MINIBUF nil or omitted means
509 consider FRAME's minibuffer window only if it's active. In both
510 cases the minibuffer window must be part of FRAME. MINIBUF
511 neither nil nor t means never consider the minibuffer window."
516 (when (and (setq this-value
(window-parameter window parameter
))
517 (or (not value
) (equal value this-value
)))
518 (throw 'found window
)))
519 frame any minibuf
))))
522 (defun window-atom-root (&optional window
)
523 "Return root of atomic window WINDOW is a part of.
524 WINDOW must be a valid window and defaults to the selected one.
525 Return nil if WINDOW is not part of an atomic window."
526 (setq window
(window-normalize-window window
))
528 (while (and window
(window-parameter window
'window-atom
))
530 (setq window
(window-parent window
)))
533 (defun window-make-atom (window)
534 "Make WINDOW an atomic window.
535 WINDOW must be an internal window. Return WINDOW."
536 (if (not (window-child window
))
537 (error "Window %s is not an internal window" window
)
540 (unless (window-parameter window
'window-atom
)
541 (set-window-parameter window
'window-atom t
)))
545 (defun display-buffer-in-atom-window (buffer alist
)
546 "Display BUFFER in an atomic window.
547 This function displays BUFFER in a new window that will be
548 combined with an existing window to form an atomic window. If
549 the existing window is already part of an atomic window, add the
550 new window to that atomic window. Operations like `split-window'
551 or `delete-window', when applied to a constituent of an atomic
552 window, are applied atomically to the root of that atomic window.
554 ALIST is an association list of symbols and values. The
555 following symbols can be used.
557 `window' specifies the existing window the new window shall be
558 combined with. Use `window-atom-root' to make the new window a
559 sibling of an atomic window's root. If an internal window is
560 specified here, all children of that window become part of the
561 atomic window too. If no window is specified, the new window
562 becomes a sibling of the selected window. By default, the
563 `window-atom' parameter of the existing window is set to `main'
564 provided it is live and was not set before.
566 `side' denotes the side of the existing window where the new
567 window shall be located. Valid values are `below', `right',
568 `above' and `left'. The default is `below'. By default, the
569 `window-atom' parameter of the new window is set to this value.
571 The return value is the new window, nil when creating that window
573 (let* ((ignore-window-parameters t
)
574 (window-combination-limit t
)
575 (window-combination-resize 'atom
)
576 (window (cdr (assq 'window alist
)))
577 (side (cdr (assq 'side alist
)))
578 (atom (when window
(window-parameter window
'window-atom
)))
580 (setq window
(window-normalize-window window
))
581 (setq root
(window-atom-root window
))
582 ;; Split off new window.
583 (when (setq new
(split-window window nil side
))
585 (if (and root
(not (eq root window
)))
586 ;; When WINDOW was part of an atomic window and we did not
587 ;; split its root, root atomic window at old root.
589 ;; Otherwise, root atomic window at WINDOW's new parent.
590 (window-parent window
)))
591 ;; Assign `window-atom' parameters, if needed.
592 (when (and (not atom
) (window-live-p window
))
593 (set-window-parameter window
'window-atom
'main
))
594 (set-window-parameter new
'window-atom side
)
595 ;; Display BUFFER in NEW and return NEW.
596 (window--display-buffer
597 buffer new
'window alist display-buffer-mark-dedicated
))))
599 (defun window--atom-check-1 (window)
600 "Subroutine of `window--atom-check'."
602 (if (window-parameter window
'window-atom
)
604 (when (or (catch 'reset
607 (if (window-parameter window
'window-atom
)
608 (setq count
(1+ count
))
611 ;; count >= 1 must hold here. If there's no other
612 ;; window around dissolve this atomic window.
614 ;; Dissolve atomic window.
617 (set-window-parameter window
'window-atom nil
))
620 (unless (window-buffer window
)
621 (window--atom-check-1 (window-left-child window
))
622 (window--atom-check-1 (window-top-child window
))))
623 ;; Check right sibling
624 (window--atom-check-1 (window-right window
))))
626 (defun window--atom-check (&optional frame
)
627 "Check atomicity of all windows on FRAME.
628 FRAME defaults to the selected frame. If an atomic window is
629 wrongly configured, reset the atomicity of all its windows on
630 FRAME to nil. An atomic window is wrongly configured if it has
631 no child windows or one of its child windows is not atomic."
632 (window--atom-check-1 (frame-root-window frame
)))
635 (defvar window-sides
'(left top right bottom
)
638 (defcustom window-sides-vertical nil
639 "If non-nil, left and right side windows are full height.
640 Otherwise, top and bottom side windows are full width."
645 (defcustom window-sides-slots
'(nil nil nil nil
)
646 "Maximum number of side window slots.
647 The value is a list of four elements specifying the number of
648 side window slots on (in this order) the left, top, right and
649 bottom side of each frame. If an element is a number, this means
650 to display at most that many side windows on the corresponding
651 side. If an element is nil, this means there's no bound on the
652 number of slots on that side."
657 :value
(nil nil nil nil
)
660 :help-echo
"Maximum slots of left side window."
662 :format
"%[Left%] %v\n"
663 (const :tag
"Unlimited" :format
"%t" nil
)
664 (integer :tag
"Number" :value
2 :size
5))
667 :help-echo
"Maximum slots of top side window."
669 :format
"%[Top%] %v\n"
670 (const :tag
"Unlimited" :format
"%t" nil
)
671 (integer :tag
"Number" :value
3 :size
5))
674 :help-echo
"Maximum slots of right side window."
676 :format
"%[Right%] %v\n"
677 (const :tag
"Unlimited" :format
"%t" nil
)
678 (integer :tag
"Number" :value
2 :size
5))
681 :help-echo
"Maximum slots of bottom side window."
683 :format
"%[Bottom%] %v\n"
684 (const :tag
"Unlimited" :format
"%t" nil
)
685 (integer :tag
"Number" :value
3 :size
5)))
688 (defun window--major-non-side-window (&optional frame
)
689 "Return the major non-side window of frame FRAME.
690 The optional argument FRAME must be a live frame and defaults to
693 If FRAME has at least one side window, the major non-side window
694 is either an internal non-side window such that all other
695 non-side windows on FRAME descend from it, or the single live
696 non-side window of FRAME. If FRAME has no side windows, return
698 (let ((frame (window-normalize-frame frame
))
700 ;; Set major to the _last_ window found by `walk-window-tree' that
701 ;; is not a side window but has a side window as its sibling.
704 (and (not (window-parameter window
'window-side
))
705 (or (and (setq sibling
(window-prev-sibling window
))
706 (window-parameter sibling
'window-side
))
707 (and (setq sibling
(window-next-sibling window
))
708 (window-parameter sibling
'window-side
)))
709 (setq major window
)))
711 (or major
(frame-root-window frame
))))
713 (defun window--major-side-window (side)
714 "Return major side window on SIDE.
715 SIDE must be one of the symbols `left', `top', `right' or
716 `bottom'. Return nil if no such window exists."
717 (let ((root (frame-root-window))
719 ;; (1) If a window on the opposite side exists, return that window's
721 ;; (2) If the new window shall span the entire side, return the
722 ;; frame's root window.
723 ;; (3) If a window on an orthogonal side exists, return that
725 ;; (4) Otherwise return the frame's root window.
727 ((or (and (eq side
'left
)
728 (setq window
(window-with-parameter 'window-side
'right nil t
)))
730 (setq window
(window-with-parameter 'window-side
'bottom nil t
))))
731 (window-prev-sibling window
))
732 ((or (and (eq side
'right
)
733 (setq window
(window-with-parameter 'window-side
'left nil t
)))
734 (and (eq side
'bottom
)
735 (setq window
(window-with-parameter 'window-side
'top nil t
))))
736 (window-next-sibling window
))
737 ((memq side
'(left right
))
739 (window-sides-vertical
741 ((setq window
(window-with-parameter 'window-side
'top nil t
))
742 (window-next-sibling window
))
743 ((setq window
(window-with-parameter 'window-side
'bottom nil t
))
744 (window-prev-sibling window
))
746 ((memq side
'(top bottom
))
748 ((not window-sides-vertical
)
750 ((setq window
(window-with-parameter 'window-side
'left nil t
))
751 (window-next-sibling window
))
752 ((setq window
(window-with-parameter 'window-side
'right nil t
))
753 (window-prev-sibling window
))
756 (defun display-buffer-in-major-side-window (buffer side slot
&optional alist
)
757 "Display BUFFER in a new window on SIDE of the selected frame.
758 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
759 specifies the slot to use. ALIST is an association list of
760 symbols and values as passed to `display-buffer-in-side-window'.
761 This function may be called only if no window on SIDE exists yet.
762 The new window automatically becomes the \"major\" side window on
763 SIDE. Return the new window, nil if its creation window failed."
764 (let* ((left-or-right (memq side
'(left right
)))
765 (major (window--major-side-window side
))
767 ((eq side
'top
) 'above
)
768 ((eq side
'bottom
) 'below
)
770 ;; The following two bindings will tell `split-window' to take
771 ;; the space for the new window from `major' and not make a new
772 ;; parent window unless needed.
773 (window-combination-resize 'side
)
774 (window-combination-limit nil
)
775 (new (split-window major nil on-side
)))
777 ;; Initialize `window-side' parameter of new window to SIDE.
778 (set-window-parameter new
'window-side side
)
779 ;; Install `window-slot' parameter of new window.
780 (set-window-parameter new
'window-slot slot
)
781 ;; Install `delete-window' parameter thus making sure that when
782 ;; the new window is deleted, a side window on the opposite side
783 ;; does not get resized.
784 (set-window-parameter new
'delete-window
'delete-side-window
)
785 ;; Auto-adjust height/width of new window unless a size has been
786 ;; explicitly requested.
787 (unless (if left-or-right
788 (cdr (assq 'window-width alist
))
789 (cdr (assq 'window-height alist
)))
793 (if left-or-right
'window-width
'window-height
)
794 (/ (window-total-size (frame-root-window) left-or-right
)
795 ;; By default use a fourth of the size of the frame's
799 ;; Install BUFFER in new window and return NEW.
800 (window--display-buffer buffer new
'window alist
'side
))))
802 (defun delete-side-window (window)
803 "Delete side window WINDOW."
804 (let ((window-combination-resize
805 (window-parameter (window-parent window
) 'window-side
))
806 (ignore-window-parameters t
))
807 (delete-window window
)))
809 (defun display-buffer-in-side-window (buffer alist
)
810 "Display BUFFER in a side window of the selected frame.
811 ALIST is an association list of symbols and values. The
812 following special symbols can be used in ALIST.
814 `side' denotes the side of the frame where the new window shall
815 be located. Valid values are `bottom', `right', `top' and
816 `left'. The default is `bottom'.
818 `slot' if non-nil, specifies the window slot where to display
819 BUFFER. A value of zero or nil means use the middle slot on
820 the specified side. A negative value means use a slot
821 preceding (that is, above or on the left of) the middle slot.
822 A positive value means use a slot following (that is, below or
823 on the right of) the middle slot. The default is zero."
824 (let ((side (or (cdr (assq 'side alist
)) 'bottom
))
825 (slot (or (cdr (assq 'slot alist
)) 0)))
827 ((not (memq side
'(top bottom left right
)))
828 (error "Invalid side %s specified" side
))
829 ((not (numberp slot
))
830 (error "Invalid slot %s specified" slot
)))
832 (let* ((major (window-with-parameter 'window-side side nil t
))
833 ;; `major' is the major window on SIDE, `windows' the list of
834 ;; life windows on SIDE.
840 (when (eq (window-parameter window
'window-side
) side
)
841 (setq windows
(cons window windows
))))
843 (nreverse windows
))))
844 (slots (when major
(max 1 (window-child-count major
))))
850 ((eq side
'bottom
) 3))
852 window this-window this-slot prev-window next-window
853 best-window best-slot abs-slot
)
856 ((and (numberp max-slots
) (<= max-slots
0))
857 ;; No side-slots available on this side. Don't create an error,
861 ;; No major window exists on this side, make one.
862 (display-buffer-in-major-side-window buffer side slot alist
))
864 ;; Scan windows on SIDE.
866 (dolist (window windows
)
867 (setq this-slot
(window-parameter window
'window-slot
))
869 ;; The following should not happen and probably be checked
870 ;; by window--side-check.
871 ((not (numberp this-slot
)))
873 ;; A window with a matching slot has been found.
874 (setq this-window window
)
877 ;; Check if this window has a better slot value wrt the
878 ;; slot of the window we want.
880 (if (or (and (> this-slot
0) (> slot
0))
881 (and (< this-slot
0) (< slot
0)))
882 (abs (- slot this-slot
))
883 (+ (abs slot
) (abs this-slot
))))
884 (unless (and best-slot
(<= best-slot abs-slot
))
885 (setq best-window window
)
886 (setq best-slot abs-slot
))
889 (setq prev-window window
))
891 (setq next-window window
)))))))
893 ;; `this-window' is the first window with the same SLOT.
894 ;; `prev-window' is the window with the largest slot < SLOT. A new
895 ;; window will be created after it.
896 ;; `next-window' is the window with the smallest slot > SLOT. A new
897 ;; window will be created before it.
898 ;; `best-window' is the window with the smallest absolute difference
899 ;; of its slot and SLOT.
901 ;; Note: We dedicate the window used softly to its buffer to
902 ;; avoid that "other" (non-side) buffer display functions steal
903 ;; it from us. This must eventually become customizable via
904 ;; ALIST (or, better, avoided in the "other" functions).
906 ;; Reuse `this-window'.
907 (window--display-buffer buffer this-window
'reuse alist
'side
))
908 (and (or (not max-slots
) (< slots max-slots
))
910 ;; Make new window before `next-window'.
912 (if (memq side
'(left right
)) 'above
'left
))
913 (window-combination-resize 'side
))
914 (setq window
(split-window next-window nil next-side
))
915 ;; When the new window is deleted, its space
916 ;; is returned to other side windows.
917 (set-window-parameter
918 window
'delete-window
'delete-side-window
)
921 ;; Make new window after `prev-window'.
923 (if (memq side
'(left right
)) 'below
'right
))
924 (window-combination-resize 'side
))
925 (setq window
(split-window prev-window nil prev-side
))
926 ;; When the new window is deleted, its space
927 ;; is returned to other side windows.
928 (set-window-parameter
929 window
'delete-window
'delete-side-window
)
931 (set-window-parameter window
'window-slot slot
)
932 (window--display-buffer buffer window
'window alist
'side
))
934 ;; Reuse `best-window'.
936 ;; Give best-window the new slot value.
937 (set-window-parameter best-window
'window-slot slot
)
938 (window--display-buffer
939 buffer best-window
'reuse alist
'side
)))))))))
941 (defun window--side-check (&optional frame
)
942 "Check the side window configuration of FRAME.
943 FRAME defaults to the selected frame.
945 A valid side window configuration preserves the following two
948 - If there exists a window whose window-side parameter is
949 non-nil, there must exist at least one live window whose
950 window-side parameter is nil.
952 - If a window W has a non-nil window-side parameter (i) it must
953 have a parent window and that parent's window-side parameter
954 must be either nil or the same as for W, and (ii) any child
955 window of W must have the same window-side parameter as W.
957 If the configuration is invalid, reset the window-side parameters
958 of all windows on FRAME to nil."
959 (let (left top right bottom none side parent parent-side
)
960 (when (or (catch 'reset
963 (setq side
(window-parameter window
'window-side
))
964 (setq parent
(window-parent window
))
966 (and parent
(window-parameter parent
'window-side
)))
967 ;; The following `cond' seems a bit tedious, but I'd
968 ;; rather stick to using just the stack.
971 (when (not (eq parent-side side
))
972 ;; A parent whose window-side is non-nil must
973 ;; have a child with the same window-side.
976 (when (window-buffer window
)
977 ;; Record that we have at least one non-side,
980 ((if (memq side
'(left top
))
981 (window-prev-sibling window
)
982 (window-next-sibling window
))
983 ;; Left and top major side windows must not have a
984 ;; previous sibling, right and bottom major side
985 ;; windows must not have a next sibling.
987 ;; Now check that there's no more than one major
988 ;; window for any of left, top, right and bottom.
990 (if left
(throw 'reset t
) (setq left t
)))
992 (if top
(throw 'reset t
) (setq top t
)))
994 (if right
(throw 'reset t
) (setq right t
)))
996 (if bottom
(throw 'reset t
) (setq bottom t
)))
1000 ;; If there's a side window, there must be at least one
1002 (and (or left top right bottom
) (not none
)))
1005 (set-window-parameter window
'window-side nil
))
1008 (defun window--check (&optional frame
)
1009 "Check atomic and side windows on FRAME.
1010 FRAME defaults to the selected frame."
1011 (window--side-check frame
)
1012 (window--atom-check frame
))
1015 (defun window-total-size (&optional window horizontal
)
1016 "Return the total height or width of WINDOW.
1017 WINDOW must be a valid window and defaults to the selected one.
1019 If HORIZONTAL is omitted or nil, return the total height of
1020 WINDOW, in lines, like `window-total-height'. Otherwise return
1021 the total width, in columns, like `window-total-width'."
1023 (window-total-width window
)
1024 (window-total-height window
)))
1026 (defun window-size (&optional window horizontal pixelwise
)
1027 "Return the height or width of WINDOW.
1028 WINDOW must be a valid window and defaults to the selected one.
1030 If HORIZONTAL is omitted or nil, return the total height of
1031 WINDOW, in lines, like `window-total-height'. Otherwise return
1032 the total width, in columns, like `window-total-width'.
1034 Optional argument PIXELWISE means return the pixel size of WINDOW
1035 like `window-pixel-height' and `window-pixel-width'."
1038 (window-pixel-width window
)
1039 (window-total-width window
))
1041 (window-pixel-height window
)
1042 (window-total-height window
))))
1044 (defvar window-size-fixed nil
1045 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
1046 If the value is `height', then only the window's height is fixed.
1047 If the value is `width', then only the window's width is fixed.
1048 Any other non-nil value fixes both the width and the height.
1050 Emacs won't change the size of any window displaying that buffer,
1051 unless it has no other choice (like when deleting a neighboring
1053 (make-variable-buffer-local 'window-size-fixed
)
1055 (defun window--size-ignore-p (window ignore
)
1056 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
1057 (if (window-valid-p ignore
) (eq window ignore
) ignore
))
1059 (defun window-safe-min-size (&optional window horizontal pixelwise
)
1060 "Return safe minimum size of WINDOW.
1061 WINDOW must be a valid window and defaults to the selected one.
1062 Optional argument HORIZONTAL non-nil means return the minimum
1063 number of columns of WINDOW; otherwise return the minimum number
1066 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1068 (setq window
(window-normalize-window window
))
1071 (* window-safe-min-width
1072 (frame-char-width (window-frame window
)))
1073 (* window-safe-min-height
1074 (frame-char-height (window-frame window
))))
1075 (if horizontal window-safe-min-width window-safe-min-height
)))
1077 (defun window-min-size (&optional window horizontal ignore pixelwise
)
1078 "Return the minimum size of WINDOW.
1079 WINDOW must be a valid window and defaults to the selected one.
1080 Optional argument HORIZONTAL non-nil means return the minimum
1081 number of columns of WINDOW; otherwise return the minimum number
1084 Optional argument IGNORE, if non-nil, means ignore restrictions
1085 imposed by fixed size windows, `window-min-height' or
1086 `window-min-width' settings. If IGNORE equals `safe', live
1087 windows may get as small as `window-safe-min-height' lines and
1088 `window-safe-min-width' columns. If IGNORE is a window, ignore
1089 restrictions for that window only. Any other non-nil value
1090 means ignore all of the above restrictions for all windows.
1092 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1095 (window-normalize-window window
) horizontal ignore pixelwise
))
1097 (defun window--min-size-1 (window horizontal ignore pixelwise
)
1098 "Internal function of `window-min-size'."
1099 (let ((sub (window-child window
)))
1102 ;; WINDOW is an internal window.
1103 (if (window-combined-p sub horizontal
)
1104 ;; The minimum size of an iso-combination is the sum of
1105 ;; the minimum sizes of its child windows.
1107 (setq value
(+ value
1109 sub horizontal ignore pixelwise
)))
1110 (setq sub
(window-right sub
)))
1111 ;; The minimum size of an ortho-combination is the maximum
1112 ;; of the minimum sizes of its child windows.
1114 (setq value
(max value
1116 sub horizontal ignore pixelwise
)))
1117 (setq sub
(window-right sub
))))
1119 (with-current-buffer (window-buffer window
)
1121 ((and (not (window--size-ignore-p window ignore
))
1122 (window-size-fixed-p window horizontal
))
1123 ;; The minimum size of a fixed size window is its size.
1124 (window-size window horizontal pixelwise
))
1125 ((or (eq ignore
'safe
) (eq ignore window
))
1126 ;; If IGNORE equals `safe' or WINDOW return the safe values.
1127 (window-safe-min-size window horizontal pixelwise
))
1129 ;; For the minimum width of a window take fringes and
1130 ;; scroll-bars into account. This is questionable and should
1131 ;; be removed as soon as we are able to split (and resize)
1132 ;; windows such that the new (or resized) windows can get a
1133 ;; size less than the user-specified `window-min-height' and
1134 ;; `window-min-width'.
1135 (let ((frame (window-frame window
))
1136 (fringes (window-fringes window
))
1137 (scroll-bars (window-scroll-bars window
)))
1140 (+ (window-safe-min-size window t t
)
1141 (car fringes
) (cadr fringes
)
1143 ((memq (nth 2 scroll-bars
) '(left right
))
1144 (nth 1 scroll-bars
))
1145 ((memq (frame-parameter frame
'vertical-scroll-bars
)
1147 (frame-parameter frame
'scroll-bar-width
))
1149 (if (window--size-ignore-p window ignore
)
1151 (window-min-pixel-width)))
1153 (+ window-safe-min-width
1154 (ceiling (car fringes
) (frame-char-width frame
))
1155 (ceiling (cadr fringes
) (frame-char-width frame
))
1157 ((memq (nth 2 scroll-bars
) '(left right
))
1158 (nth 1 scroll-bars
))
1159 ((memq (frame-parameter frame
'vertical-scroll-bars
)
1161 (ceiling (or (frame-parameter frame
'scroll-bar-width
) 14)
1162 (frame-char-width)))
1164 (if (window--size-ignore-p window ignore
)
1166 window-min-width
)))))
1169 (+ (window-safe-min-size window nil t
)
1170 (window-header-line-height window
)
1171 (window-mode-line-height window
))
1172 (if (window--size-ignore-p window ignore
)
1174 (window-min-pixel-height))))
1176 ;; For the minimum height of a window take any mode- or
1177 ;; header-line into account.
1178 (max (+ window-safe-min-height
1179 (if header-line-format
1 0)
1180 (if mode-line-format
1 0))
1181 (if (window--size-ignore-p window ignore
)
1183 window-min-height
))))))))
1185 (defun window-sizable (window delta
&optional horizontal ignore pixelwise
)
1186 "Return DELTA if DELTA lines can be added to WINDOW.
1187 WINDOW must be a valid window and defaults to the selected one.
1188 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1189 columns can be added to WINDOW. A return value of zero means
1190 that no lines (or columns) can be added to WINDOW.
1192 This function looks only at WINDOW and, recursively, its child
1193 windows. The function `window-resizable' looks at other windows
1196 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1197 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1198 return the maximum value in the range 0..DELTA by which WINDOW
1201 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1202 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1203 return the minimum value in the range DELTA..0 by which WINDOW
1206 Optional argument IGNORE non-nil means ignore restrictions
1207 imposed by fixed size windows, `window-min-height' or
1208 `window-min-width' settings. If IGNORE equals `safe', live
1209 windows may get as small as `window-safe-min-height' lines and
1210 `window-safe-min-width' columns. If IGNORE is a window, ignore
1211 restrictions for that window only. Any other non-nil value means
1212 ignore all of the above restrictions for all windows.
1214 Optional argument PIXELWISE non-nil means interpret DELTA as
1216 (setq window
(window-normalize-window window
))
1219 (max (- (window-min-size window horizontal ignore pixelwise
)
1220 (window-size window horizontal pixelwise
))
1222 ((window--size-ignore-p window ignore
)
1225 (if (window-size-fixed-p window horizontal
)
1230 (defun window-sizable-p (window delta
&optional horizontal ignore pixelwise
)
1231 "Return t if WINDOW can be resized by DELTA lines.
1232 WINDOW must be a valid window and defaults to the selected one.
1233 For the meaning of the arguments of this function see the
1234 doc-string of `window-sizable'."
1235 (setq window
(window-normalize-window window
))
1237 (>= (window-sizable window delta horizontal ignore pixelwise
)
1239 (<= (window-sizable window delta horizontal ignore pixelwise
)
1242 (defun window--size-fixed-1 (window horizontal
)
1243 "Internal function for `window-size-fixed-p'."
1244 (let ((sub (window-child window
)))
1247 ;; WINDOW is an internal window.
1248 (if (window-combined-p sub horizontal
)
1249 ;; An iso-combination is fixed size if all its child
1250 ;; windows are fixed-size.
1253 (unless (window--size-fixed-1 sub horizontal
)
1254 ;; We found a non-fixed-size child window, so
1255 ;; WINDOW's size is not fixed.
1257 (setq sub
(window-right sub
)))
1258 ;; All child windows are fixed-size, so WINDOW's size is
1261 ;; An ortho-combination is fixed-size if at least one of its
1262 ;; child windows is fixed-size.
1264 (when (window--size-fixed-1 sub horizontal
)
1265 ;; We found a fixed-size child window, so WINDOW's size
1268 (setq sub
(window-right sub
))))
1269 ;; WINDOW is a live window.
1270 (with-current-buffer (window-buffer window
)
1272 (memq window-size-fixed
'(width t
))
1273 (memq window-size-fixed
'(height t
))))))))
1275 (defun window-size-fixed-p (&optional window horizontal
)
1276 "Return non-nil if WINDOW's height is fixed.
1277 WINDOW must be a valid window and defaults to the selected one.
1278 Optional argument HORIZONTAL non-nil means return non-nil if
1279 WINDOW's width is fixed.
1281 If this function returns nil, this does not necessarily mean that
1282 WINDOW can be resized in the desired direction. The function
1283 `window-resizable' can tell that."
1284 (window--size-fixed-1
1285 (window-normalize-window window
) horizontal
))
1287 (defun window--min-delta-1 (window delta
&optional horizontal ignore trail noup pixelwise
)
1288 "Internal function for `window-min-delta'."
1289 (if (not (window-parent window
))
1290 ;; If we can't go up, return zero.
1292 ;; Else try to find a non-fixed-size sibling of WINDOW.
1293 (let* ((parent (window-parent window
))
1294 (sub (window-child parent
)))
1296 (if (window-combined-p sub horizontal
)
1297 ;; In an iso-combination throw DELTA if we find at least one
1298 ;; child window and that window is either not fixed-size or
1299 ;; we can ignore fixed-sizeness.
1300 (let ((skip (eq trail
'after
)))
1304 (setq skip
(eq trail
'before
)))
1306 ((and (not (window--size-ignore-p window ignore
))
1307 (window-size-fixed-p sub horizontal
)))
1309 ;; We found a non-fixed-size child window.
1310 (throw 'done delta
)))
1311 (setq sub
(window-right sub
))))
1312 ;; In an ortho-combination set DELTA to the minimum value by
1313 ;; which other child windows can shrink.
1315 (unless (eq sub window
)
1318 (- (window-size sub horizontal pixelwise
)
1320 sub horizontal ignore pixelwise
)))))
1321 (setq sub
(window-right sub
))))
1324 (window--min-delta-1
1325 parent delta horizontal ignore trail nil pixelwise
))))))
1327 (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise
)
1328 "Return number of lines by which WINDOW can be shrunk.
1329 WINDOW must be a valid window and defaults to the selected one.
1330 Return zero if WINDOW cannot be shrunk.
1332 Optional argument HORIZONTAL non-nil means return number of
1333 columns by which WINDOW can be shrunk.
1335 Optional argument IGNORE non-nil means ignore restrictions
1336 imposed by fixed size windows, `window-min-height' or
1337 `window-min-width' settings. If IGNORE is a window, ignore
1338 restrictions for that window only. If IGNORE equals `safe',
1339 live windows may get as small as `window-safe-min-height' lines
1340 and `window-safe-min-width' columns. Any other non-nil value
1341 means ignore all of the above restrictions for all windows.
1343 Optional argument TRAIL restricts the windows that can be enlarged.
1344 If its value is `before', only windows to the left of or above WINDOW
1345 can be enlarged. If it is `after', only windows to the right of or
1346 below WINDOW can be enlarged.
1348 Optional argument NOUP non-nil means don't go up in the window
1349 tree, but try to enlarge windows within WINDOW's combination only.
1351 Optional argument NODOWN non-nil means don't check whether WINDOW
1352 itself (and its child windows) can be shrunk; check only whether
1353 at least one other window can be enlarged appropriately.
1355 Optional argument PIXELWISE non-nil means return number of pixels
1356 by which WINDOW can be shrunk."
1357 (setq window
(window-normalize-window window
))
1358 (let ((size (window-size window horizontal pixelwise
))
1359 (minimum (window-min-size window horizontal ignore pixelwise
)))
1362 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1363 (window--min-delta-1
1364 window size horizontal ignore trail noup pixelwise
))
1366 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1367 ;; there's nothing to recover.
1370 ;; Otherwise, try to recover whatever WINDOW is larger than its
1372 (window--min-delta-1
1373 window
(- size minimum
) horizontal ignore trail noup pixelwise
)))))
1375 (defun window--max-delta-1 (window delta
&optional horizontal ignore trail noup pixelwise
)
1376 "Internal function of `window-max-delta'."
1377 (if (not (window-parent window
))
1378 ;; Can't go up. Return DELTA.
1380 (let* ((parent (window-parent window
))
1381 (sub (window-child parent
)))
1383 (if (window-combined-p sub horizontal
)
1384 ;; For an iso-combination calculate how much we can get from
1385 ;; other child windows.
1386 (let ((skip (eq trail
'after
)))
1390 (setq skip
(eq trail
'before
)))
1395 (- (window-size sub horizontal pixelwise
)
1397 sub horizontal ignore pixelwise
))))))
1398 (setq sub
(window-right sub
))))
1399 ;; For an ortho-combination throw DELTA when at least one
1400 ;; child window is fixed-size.
1402 (when (and (not (eq sub window
))
1403 (not (window--size-ignore-p sub ignore
))
1404 (window-size-fixed-p sub horizontal
))
1405 (throw 'fixed delta
))
1406 (setq sub
(window-right sub
))))
1408 ;; When NOUP is nil, DELTA is all we can get.
1410 ;; Else try with parent of WINDOW, passing the DELTA we
1411 ;; recovered so far.
1412 (window--max-delta-1
1413 parent delta horizontal ignore trail nil pixelwise
))))))
1415 (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise
)
1416 "Return maximum number of lines by which WINDOW can be enlarged.
1417 WINDOW must be a valid window and defaults to the selected one.
1418 The return value is zero if WINDOW cannot be enlarged.
1420 Optional argument HORIZONTAL non-nil means return maximum number
1421 of columns by which WINDOW can be enlarged.
1423 Optional argument IGNORE non-nil means ignore restrictions
1424 imposed by fixed size windows, `window-min-height' or
1425 `window-min-width' settings. If IGNORE is a window, ignore
1426 restrictions for that window only. If IGNORE equals `safe',
1427 live windows may get as small as `window-safe-min-height' lines
1428 and `window-safe-min-width' columns. Any other non-nil value means
1429 ignore all of the above restrictions for all windows.
1431 Optional argument TRAIL restricts the windows that can be enlarged.
1432 If its value is `before', only windows to the left of or above WINDOW
1433 can be enlarged. If it is `after', only windows to the right of or
1434 below WINDOW can be enlarged.
1436 Optional argument NOUP non-nil means don't go up in the window
1437 tree but try to obtain the entire space from windows within
1438 WINDOW's combination.
1440 Optional argument NODOWN non-nil means do not check whether
1441 WINDOW itself (and its child windows) can be enlarged; check
1442 only whether other windows can be shrunk appropriately.
1444 Optional argument PIXELWISE non-nil means return number of
1445 pixels by which WINDOW can be enlarged."
1446 (setq window
(window-normalize-window window
))
1447 (if (and (not (window--size-ignore-p window ignore
))
1448 (not nodown
) (window-size-fixed-p window horizontal
))
1449 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1452 ;; WINDOW has no fixed size.
1453 (window--max-delta-1 window
0 horizontal ignore trail noup pixelwise
)))
1455 ;; Make NOUP also inhibit the min-size check.
1456 (defun window--resizable (window delta
&optional horizontal ignore trail noup nodown pixelwise
)
1457 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1458 WINDOW must be a valid window and defaults to the selected one.
1459 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1460 can be resized horizontally by DELTA columns. A return value of
1461 zero means that WINDOW is not resizable.
1463 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1464 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1465 return the maximum value in the range 0..DELTA by which WINDOW
1468 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1469 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1470 return the minimum value in the range DELTA..0 that can be used
1471 for shrinking WINDOW.
1473 Optional argument IGNORE non-nil means ignore restrictions
1474 imposed by fixed size windows, `window-min-height' or
1475 `window-min-width' settings. If IGNORE is a window, ignore
1476 restrictions for that window only. If IGNORE equals `safe',
1477 live windows may get as small as `window-safe-min-height' lines
1478 and `window-safe-min-width' columns. Any other non-nil value
1479 means ignore all of the above restrictions for all windows.
1481 Optional argument TRAIL `before' means only windows to the left
1482 of or below WINDOW can be shrunk. Optional argument TRAIL
1483 `after' means only windows to the right of or above WINDOW can be
1486 Optional argument NOUP non-nil means don't go up in the window
1487 tree but check only whether space can be obtained from (or given
1488 to) WINDOW's siblings.
1490 Optional argument NODOWN non-nil means don't go down in the
1491 window tree. This means do not check whether resizing would
1492 violate size restrictions of WINDOW or its child windows.
1494 Optional argument PIXELWISE non-nil means interpret DELTA as
1496 (setq window
(window-normalize-window window
))
1499 (max (- (window-min-delta
1500 window horizontal ignore trail noup nodown pixelwise
))
1503 (min (window-max-delta
1504 window horizontal ignore trail noup nodown pixelwise
)
1508 (defun window--resizable-p (window delta
&optional horizontal ignore trail noup nodown pixelwise
)
1509 "Return t if WINDOW can be resized vertically by DELTA lines.
1510 WINDOW must be a valid window and defaults to the selected one.
1511 For the meaning of the arguments of this function see the
1512 doc-string of `window--resizable'.
1514 Optional argument PIXELWISE non-nil means interpret DELTA as
1516 (setq window
(window-normalize-window window
))
1518 (>= (window--resizable
1519 window delta horizontal ignore trail noup nodown pixelwise
)
1521 (<= (window--resizable
1522 window delta horizontal ignore trail noup nodown pixelwise
)
1525 (defun window-resizable (window delta
&optional horizontal ignore pixelwise
)
1526 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1527 WINDOW must be a valid window and defaults to the selected one.
1528 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1529 can be resized horizontally by DELTA columns. A return value of
1530 zero means that WINDOW is not resizable.
1532 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1533 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1534 return the maximum value in the range 0..DELTA by which WINDOW
1537 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1538 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1539 return the minimum value in the range DELTA..0 that can be used
1540 for shrinking WINDOW.
1542 Optional argument IGNORE non-nil means ignore restrictions
1543 imposed by fixed size windows, `window-min-height' or
1544 `window-min-width' settings. If IGNORE is a window, ignore
1545 restrictions for that window only. If IGNORE equals `safe',
1546 live windows may get as small as `window-safe-min-height' lines
1547 and `window-safe-min-width' columns. Any other non-nil value
1548 means ignore all of the above restrictions for all windows.
1550 Optional argument PIXELWISE non-nil means interpret DELTA as
1552 (setq window
(window-normalize-window window
))
1553 (window--resizable window delta horizontal ignore nil nil nil pixelwise
))
1555 (defun window-resizable-p (window delta
&optional horizontal ignore pixelwise
)
1556 "Return t if WINDOW can be resized vertically by DELTA lines.
1557 WINDOW must be a valid window and defaults to the selected one.
1558 For the meaning of the arguments of this function see the
1559 doc-string of `window-resizable'."
1560 (setq window
(window-normalize-window window
))
1562 (>= (window--resizable
1563 window delta horizontal ignore nil nil nil pixelwise
)
1565 (<= (window--resizable
1566 window delta horizontal ignore nil nil nil pixelwise
)
1569 ;; Aliases of functions defined in window.c.
1570 (defalias 'window-height
'window-total-height
)
1571 (defalias 'window-width
'window-body-width
)
1573 ;; Eventually the following two should work pixelwise.
1575 ;; See discussion in bug#4543.
1576 (defun window-full-height-p (&optional window
)
1577 "Return t if WINDOW is as high as its containing frame.
1578 More precisely, return t if and only if the total height of
1579 WINDOW equals the total height of the root window of WINDOW's
1580 frame. WINDOW must be a valid window and defaults to the
1582 (setq window
(window-normalize-window window
))
1583 (= (window-pixel-height window
)
1584 (window-pixel-height (frame-root-window window
))))
1586 (defun window-full-width-p (&optional window
)
1587 "Return t if WINDOW is as wide as its containing frame.
1588 More precisely, return t if and only if the total width of WINDOW
1589 equals the total width of the root window of WINDOW's frame.
1590 WINDOW must be a valid window and defaults to the selected one."
1591 (setq window
(window-normalize-window window
))
1592 (= (window-pixel-width window
)
1593 (window-pixel-width (frame-root-window window
))))
1595 (defun window-body-size (&optional window horizontal
)
1596 "Return the height or width of WINDOW's text area.
1597 WINDOW must be a live window and defaults to the selected one.
1599 If HORIZONTAL is omitted or nil, return the height of the text
1600 area, like `window-body-height'. Otherwise, return the width of
1601 the text area, like `window-body-width'."
1603 (window-body-width window
)
1604 (window-body-height window
)))
1606 (defun window-current-scroll-bars (&optional window
)
1607 "Return the current scroll bar settings for WINDOW.
1608 WINDOW must be a live window and defaults to the selected one.
1610 The return value is a cons cell (VERTICAL . HORIZONTAL) where
1611 VERTICAL specifies the current location of the vertical scroll
1612 bars (`left', `right', or nil), and HORIZONTAL specifies the
1613 current location of the horizontal scroll bars (`top', `bottom',
1616 Unlike `window-scroll-bars', this function reports the scroll bar
1617 type actually used, once frame defaults and `scroll-bar-mode' are
1618 taken into account."
1619 (setq window
(window-normalize-window window t
))
1620 (let ((vert (nth 2 (window-scroll-bars window
)))
1622 (when (or (eq vert t
) (eq hor t
))
1623 (let ((fcsb (frame-current-scroll-bars (window-frame window
))))
1625 (setq vert
(car fcsb
)))
1627 (setq hor
(cdr fcsb
)))))
1630 (defun walk-windows (fun &optional minibuf all-frames
)
1631 "Cycle through all live windows, calling FUN for each one.
1632 FUN must specify a function with a window as its sole argument.
1633 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1634 windows to include in the walk.
1636 MINIBUF t means include the minibuffer window even if the
1637 minibuffer is not active. MINIBUF nil or omitted means include
1638 the minibuffer window only if the minibuffer is active. Any
1639 other value means do not include the minibuffer window even if
1640 the minibuffer is active.
1642 ALL-FRAMES nil or omitted means consider all windows on the
1643 selected frame, plus the minibuffer window if specified by the
1644 MINIBUF argument. If the minibuffer counts, consider all windows
1645 on all frames that share that minibuffer too. The following
1646 non-nil values of ALL-FRAMES have special meanings:
1648 - t means consider all windows on all existing frames.
1650 - `visible' means consider all windows on all visible frames on
1651 the current terminal.
1653 - 0 (the number zero) means consider all windows on all visible
1654 and iconified frames on the current terminal.
1656 - A frame means consider all windows on that frame only.
1658 Anything else means consider all windows on the selected frame
1661 This function changes neither the order of recently selected
1662 windows nor the buffer list."
1663 ;; If we start from the minibuffer window, don't fail to come
1665 (when (window-minibuffer-p)
1667 ;; Make sure to not mess up the order of recently selected
1668 ;; windows. Use `save-selected-window' and `select-window'
1669 ;; with second argument non-nil for this purpose.
1670 (save-selected-window
1671 (when (framep all-frames
)
1672 (select-window (frame-first-window all-frames
) 'norecord
))
1673 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames
))
1674 (funcall fun walk-windows-window
))))
1676 (defun window-at-side-p (&optional window side
)
1677 "Return t if WINDOW is at SIDE of its containing frame.
1678 WINDOW must be a valid window and defaults to the selected one.
1679 SIDE can be any of the symbols `left', `top', `right' or
1680 `bottom'. The default value nil is handled like `bottom'."
1681 (setq window
(window-normalize-window window
))
1686 ((eq side
'right
) 2)
1687 ((memq side
'(bottom nil
)) 3))))
1688 (= (nth edge
(window-pixel-edges window
))
1689 (nth edge
(window-pixel-edges (frame-root-window window
))))))
1691 (defun window-at-side-list (&optional frame side
)
1692 "Return list of all windows on SIDE of FRAME.
1693 FRAME must be a live frame and defaults to the selected frame.
1694 SIDE can be any of the symbols `left', `top', `right' or
1695 `bottom'. The default value nil is handled like `bottom'."
1696 (setq frame
(window-normalize-frame frame
))
1700 (when (window-at-side-p window side
)
1701 (setq windows
(cons window windows
))))
1703 (nreverse windows
)))
1705 (defun window--in-direction-2 (window posn
&optional horizontal
)
1706 "Support function for `window-in-direction'."
1708 (let ((top (window-pixel-top window
)))
1711 (- posn top
(window-pixel-height window
))))
1712 (let ((left (window-pixel-left window
)))
1715 (- posn left
(window-pixel-width window
))))))
1717 ;; Predecessors to the below have been devised by Julian Assange in
1718 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
1719 ;; Neither of these allow to selectively ignore specific windows
1720 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
1722 (defun window-in-direction (direction &optional window ignore sign wrap mini
)
1723 "Return window in DIRECTION as seen from WINDOW.
1724 More precisely, return the nearest window in direction DIRECTION
1725 as seen from the position of `window-point' in window WINDOW.
1726 DIRECTION must be one of `above', `below', `left' or `right'.
1727 WINDOW must be a live window and defaults to the selected one.
1729 Do not return a window whose `no-other-window' parameter is
1730 non-nil. If the nearest window's `no-other-window' parameter is
1731 non-nil, try to find another window in the indicated direction.
1732 If, however, the optional argument IGNORE is non-nil, return that
1733 window even if its `no-other-window' parameter is non-nil.
1735 Optional argument SIGN a negative number means to use the right
1736 or bottom edge of WINDOW as reference position instead of
1737 `window-point'. SIGN a positive number means to use the left or
1738 top edge of WINDOW as reference position.
1740 Optional argument WRAP non-nil means to wrap DIRECTION around
1741 frame borders. This means to return for a WINDOW a the top of
1742 the frame and DIRECTION `above' to return the minibuffer window
1743 if the frame has one, and a window at the bottom of the frame
1746 Optional argument MINI nil means to return the minibuffer window
1747 if and only if it is currently active. MINI non-nil means to
1748 return the minibuffer window even when it's not active. However,
1749 if WRAP non-nil, always act as if MINI were nil.
1751 Return nil if no suitable window can be found."
1752 (setq window
(window-normalize-window window t
))
1753 (unless (memq direction
'(above below left right
))
1754 (error "Wrong direction %s" direction
))
1755 (let* ((frame (window-frame window
))
1756 (hor (memq direction
'(left right
)))
1758 (window-pixel-left window
)
1759 (window-pixel-top window
)))
1760 (last (+ first
(window-size window hor t
)))
1761 ;; The column / row value of `posn-at-point' can be nil for the
1762 ;; mini-window, guard against that.
1765 ((and (numberp sign
) (< sign
0))
1767 (1- (+ (window-pixel-top window
) (window-pixel-height window
)))
1768 (1- (+ (window-pixel-left window
) (window-pixel-width window
)))))
1769 ((and (numberp sign
) (> sign
0))
1771 (window-pixel-top window
)
1772 (window-pixel-left window
)))
1773 ((let ((posn-cons (nth 2 (posn-at-point (window-point window
) window
))))
1775 (+ (or (cdr posn-cons
) 1) (window-pixel-top window
))
1776 (+ (or (car posn-cons
) 1) (window-pixel-left window
)))))))
1779 ((eq direction
'below
) (frame-pixel-height frame
))
1780 ((eq direction
'right
) (frame-pixel-width frame
))
1782 (best-edge-2 best-edge
)
1783 (best-diff-2 (if hor
(frame-pixel-height frame
) (frame-pixel-width frame
)))
1784 best best-2 best-diff-2-new
)
1787 (let* ((w-top (window-pixel-top w
))
1788 (w-left (window-pixel-left w
)))
1791 ;; Ignore ourselves.
1792 (and (window-parameter w
'no-other-window
)
1793 ;; Ignore W unless IGNORE is non-nil.
1797 ((and (<= w-top posn
)
1798 (< posn
(+ w-top
(window-pixel-height w
))))
1799 ;; W is to the left or right of WINDOW and covers POSN.
1800 (when (or (and (eq direction
'left
)
1801 (or (and (<= w-left first
) (> w-left best-edge
))
1803 (window-at-side-p window
'left
)
1804 (window-at-side-p w
'right
))))
1805 (and (eq direction
'right
)
1806 (or (and (>= w-left last
) (< w-left best-edge
))
1808 (window-at-side-p window
'right
)
1809 (window-at-side-p w
'left
)))))
1810 (setq best-edge w-left
)
1812 ((and (or (and (eq direction
'left
)
1813 (<= (+ w-left
(window-pixel-width w
)) first
))
1814 (and (eq direction
'right
) (<= last w-left
)))
1815 ;; W is to the left or right of WINDOW but does not
1817 (setq best-diff-2-new
1818 (window--in-direction-2 w posn hor
))
1819 (or (< best-diff-2-new best-diff-2
)
1820 (and (= best-diff-2-new best-diff-2
)
1821 (if (eq direction
'left
)
1822 (> w-left best-edge-2
)
1823 (< w-left best-edge-2
)))))
1824 (setq best-edge-2 w-left
)
1825 (setq best-diff-2 best-diff-2-new
)
1827 ((and (<= w-left posn
)
1828 (< posn
(+ w-left
(window-pixel-width w
))))
1829 ;; W is above or below WINDOW and covers POSN.
1830 (when (or (and (eq direction
'above
)
1831 (or (and (<= w-top first
) (> w-top best-edge
))
1833 (window-at-side-p window
'top
)
1834 (if (active-minibuffer-window)
1835 (minibuffer-window-active-p w
)
1836 (window-at-side-p w
'bottom
)))))
1837 (and (eq direction
'below
)
1838 (or (and (>= w-top first
) (< w-top best-edge
))
1840 (if (active-minibuffer-window)
1841 (minibuffer-window-active-p window
)
1842 (window-at-side-p window
'bottom
))
1843 (window-at-side-p w
'top
)))))
1844 (setq best-edge w-top
)
1846 ((and (or (and (eq direction
'above
)
1847 (<= (+ w-top
(window-pixel-height w
)) first
))
1848 (and (eq direction
'below
) (<= last w-top
)))
1849 ;; W is above or below WINDOW but does not cover POSN.
1850 (setq best-diff-2-new
1851 (window--in-direction-2 w posn hor
))
1852 (or (< best-diff-2-new best-diff-2
)
1853 (and (= best-diff-2-new best-diff-2
)
1854 (if (eq direction
'above
)
1855 (> w-top best-edge-2
)
1856 (< w-top best-edge-2
)))))
1857 (setq best-edge-2 w-top
)
1858 (setq best-diff-2 best-diff-2-new
)
1860 frame nil
(and mini t
))
1863 (defun get-window-with-predicate (predicate &optional minibuf all-frames default
)
1864 "Return a live window satisfying PREDICATE.
1865 More precisely, cycle through all windows calling the function
1866 PREDICATE on each one of them with the window as its sole
1867 argument. Return the first window for which PREDICATE returns
1868 non-nil. Windows are scanned starting with the window following
1869 the selected window. If no window satisfies PREDICATE, return
1872 MINIBUF t means include the minibuffer window even if the
1873 minibuffer is not active. MINIBUF nil or omitted means include
1874 the minibuffer window only if the minibuffer is active. Any
1875 other value means do not include the minibuffer window even if
1876 the minibuffer is active.
1878 ALL-FRAMES nil or omitted means consider all windows on the selected
1879 frame, plus the minibuffer window if specified by the MINIBUF
1880 argument. If the minibuffer counts, consider all windows on all
1881 frames that share that minibuffer too. The following non-nil
1882 values of ALL-FRAMES have special meanings:
1884 - t means consider all windows on all existing frames.
1886 - `visible' means consider all windows on all visible frames on
1887 the current terminal.
1889 - 0 (the number zero) means consider all windows on all visible
1890 and iconified frames on the current terminal.
1892 - A frame means consider all windows on that frame only.
1894 Anything else means consider all windows on the selected frame
1897 (dolist (window (window-list-1
1898 (next-window nil minibuf all-frames
)
1899 minibuf all-frames
))
1900 (when (funcall predicate window
)
1901 (throw 'found window
)))
1904 (defalias 'some-window
'get-window-with-predicate
)
1906 (defun get-lru-window (&optional all-frames dedicated not-selected
)
1907 "Return the least recently used window on frames specified by ALL-FRAMES.
1908 Return a full-width window if possible. A minibuffer window is
1909 never a candidate. A dedicated window is never a candidate
1910 unless DEDICATED is non-nil, so if all windows are dedicated, the
1911 value is nil. Avoid returning the selected window if possible.
1912 Optional argument NOT-SELECTED non-nil means never return the
1915 The following non-nil values of the optional argument ALL-FRAMES
1916 have special meanings:
1918 - t means consider all windows on all existing frames.
1920 - `visible' means consider all windows on all visible frames on
1921 the current terminal.
1923 - 0 (the number zero) means consider all windows on all visible
1924 and iconified frames on the current terminal.
1926 - A frame means consider all windows on that frame only.
1928 Any other value of ALL-FRAMES means consider all windows on the
1929 selected frame and no others."
1930 (let (best-window best-time second-best-window second-best-time time
)
1931 (dolist (window (window-list-1 nil
'nomini all-frames
))
1932 (when (and (or dedicated
(not (window-dedicated-p window
)))
1933 (or (not not-selected
) (not (eq window
(selected-window)))))
1934 (setq time
(window-use-time window
))
1935 (if (or (eq window
(selected-window))
1936 (not (window-full-width-p window
)))
1937 (when (or (not second-best-time
) (< time second-best-time
))
1938 (setq second-best-time time
)
1939 (setq second-best-window window
))
1940 (when (or (not best-time
) (< time best-time
))
1941 (setq best-time time
)
1942 (setq best-window window
)))))
1943 (or best-window second-best-window
)))
1945 (defun get-mru-window (&optional all-frames dedicated not-selected
)
1946 "Return the most recently used window on frames specified by ALL-FRAMES.
1947 A minibuffer window is never a candidate. A dedicated window is
1948 never a candidate unless DEDICATED is non-nil, so if all windows
1949 are dedicated, the value is nil. Optional argument NOT-SELECTED
1950 non-nil means never return the selected window.
1952 The following non-nil values of the optional argument ALL-FRAMES
1953 have special meanings:
1955 - t means consider all windows on all existing frames.
1957 - `visible' means consider all windows on all visible frames on
1958 the current terminal.
1960 - 0 (the number zero) means consider all windows on all visible
1961 and iconified frames on the current terminal.
1963 - A frame means consider all windows on that frame only.
1965 Any other value of ALL-FRAMES means consider all windows on the
1966 selected frame and no others."
1967 (let (best-window best-time time
)
1968 (dolist (window (window-list-1 nil
'nomini all-frames
))
1969 (setq time
(window-use-time window
))
1970 (when (and (or dedicated
(not (window-dedicated-p window
)))
1971 (or (not not-selected
) (not (eq window
(selected-window))))
1972 (or (not best-time
) (> time best-time
)))
1973 (setq best-time time
)
1974 (setq best-window window
)))
1977 (defun get-largest-window (&optional all-frames dedicated not-selected
)
1978 "Return the largest window on frames specified by ALL-FRAMES.
1979 A minibuffer window is never a candidate. A dedicated window is
1980 never a candidate unless DEDICATED is non-nil, so if all windows
1981 are dedicated, the value is nil. Optional argument NOT-SELECTED
1982 non-nil means never return the selected window.
1984 The following non-nil values of the optional argument ALL-FRAMES
1985 have special meanings:
1987 - t means consider all windows on all existing frames.
1989 - `visible' means consider all windows on all visible frames on
1990 the current terminal.
1992 - 0 (the number zero) means consider all windows on all visible
1993 and iconified frames on the current terminal.
1995 - A frame means consider all windows on that frame only.
1997 Any other value of ALL-FRAMES means consider all windows on the
1998 selected frame and no others."
2001 (dolist (window (window-list-1 nil
'nomini all-frames
))
2002 (when (and (or dedicated
(not (window-dedicated-p window
)))
2003 (or (not not-selected
) (not (eq window
(selected-window)))))
2004 (setq size
(* (window-pixel-height window
)
2005 (window-pixel-width window
)))
2006 (when (> size best-size
)
2007 (setq best-size size
)
2008 (setq best-window window
))))
2011 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames
)
2012 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2013 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2014 and defaults to the current buffer. Windows are scanned starting
2015 with the selected window.
2017 MINIBUF t means include the minibuffer window even if the
2018 minibuffer is not active. MINIBUF nil or omitted means include
2019 the minibuffer window only if the minibuffer is active. Any
2020 other value means do not include the minibuffer window even if
2021 the minibuffer is active.
2023 ALL-FRAMES nil or omitted means consider all windows on the
2024 selected frame, plus the minibuffer window if specified by the
2025 MINIBUF argument. If the minibuffer counts, consider all windows
2026 on all frames that share that minibuffer too. The following
2027 non-nil values of ALL-FRAMES have special meanings:
2029 - t means consider all windows on all existing frames.
2031 - `visible' means consider all windows on all visible frames on
2032 the current terminal.
2034 - 0 (the number zero) means consider all windows on all visible
2035 and iconified frames on the current terminal.
2037 - A frame means consider all windows on that frame only.
2039 Anything else means consider all windows on the selected frame
2041 (let ((buffer (window-normalize-buffer buffer-or-name
))
2043 (dolist (window (window-list-1 (selected-window) minibuf all-frames
))
2044 (when (eq (window-buffer window
) buffer
)
2045 (setq windows
(cons window windows
))))
2046 (nreverse windows
)))
2048 (defun minibuffer-window-active-p (window)
2049 "Return t if WINDOW is the currently active minibuffer window."
2050 (eq window
(active-minibuffer-window)))
2052 (defun count-windows (&optional minibuf
)
2053 "Return the number of live windows on the selected frame.
2054 The optional argument MINIBUF specifies whether the minibuffer
2055 window shall be counted. See `walk-windows' for the precise
2056 meaning of this argument."
2057 (length (window-list-1 nil minibuf
)))
2059 ;;; Resizing windows.
2060 (defun window--size-to-pixel (window size
&optional horizontal pixelwise round-maybe
)
2061 "For WINDOW convert SIZE lines to pixels.
2062 SIZE is supposed to specify a height of WINDOW in terms of text
2063 lines. The return value is the number of pixels specifying that
2066 WINDOW must be a valid window. Optional argument HORIZONTAL
2067 non-nil means convert SIZE columns to pixels.
2069 Optional argument PIXELWISE non-nil means SIZE already specifies
2070 pixels but may have to be adjusted to a multiple of the character
2071 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2072 means round to the nearest multiple of the character size of
2073 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2074 (setq window
(window-normalize-window window
))
2075 (let ((char-size (frame-char-size window horizontal
)))
2077 (if (and round-maybe
(not window-resize-pixelwise
))
2078 (* (round size char-size
) char-size
)
2080 (* size char-size
))))
2082 (defun window--pixel-to-size (window size
&optional horizontal round-up
)
2083 "For WINDOW convert SIZE pixels to lines.
2084 WINDOW must be a valid window and defaults to the selected one.
2085 Optional argument HORIZONTAL non-nil means convert SIZE pixels to
2086 columns. Optional argument ROUND-UP means to round up the return
2088 (let ((char-size (frame-char-size
2089 (window-normalize-window window
) horizontal
)))
2091 (/ (+ size char-size -
1) char-size
)
2092 (/ size char-size
))))
2094 (defun window--pixel-to-total-1 (window horizontal char-size
)
2095 "Subroutine of `window--pixel-to-total'."
2096 (let ((child (window-child window
)))
2097 (if (window-combination-p window horizontal
)
2098 ;; In an iso-combination distribute sizes proportionally.
2099 (let ((remainder (window-new-total window
))
2100 size best-child best-size
)
2101 ;; Initialize total sizes to each child's floor.
2103 (setq size
(window--pixel-to-size
2104 child
(window-size child horizontal t
)
2106 (set-window-new-total child size
)
2107 (setq remainder
(- remainder size
))
2108 (setq child
(window-next-sibling child
)))
2109 ;; Distribute remainder.
2110 (while (> remainder
0)
2111 (setq child
(window-last-child window
))
2112 (setq best-child nil
)
2114 ;; We want those auxiliary fields in the window structure to
2117 (setq size
(- (/ (window-size child horizontal t
) char-size
)
2118 (window-new-total child
)))
2119 (when (> size best-size
)
2120 (setq best-child child
)
2121 (setq best-size size
))
2122 (setq child
(window-prev-sibling child
)))
2123 ;; We MUST have a best-child here.
2124 (set-window-new-total best-child
1 t
)
2125 (setq remainder
(1- remainder
)))
2127 (setq child
(window-child window
))
2129 (window--pixel-to-total-1 child horizontal char-size
)
2130 (setq child
(window-next-sibling child
))))
2131 ;; In an ortho-combination assign new sizes directly.
2132 (let ((size (window-new-total window
)))
2134 (set-window-new-total child size
)
2135 (window--pixel-to-total-1 child horizontal char-size
)
2136 (setq child
(window-next-sibling child
)))))))
2138 (defun window--pixel-to-total (&optional frame horizontal
)
2139 "On FRAME assign new total window heights from pixel heights.
2140 FRAME must be a live frame and defaults to the selected frame.
2142 Optional argument HORIZONTAL non-nil means assign new total
2143 window widths from pixel widths."
2144 (setq frame
(window-normalize-frame frame
))
2145 (let ((root (frame-root-window))
2146 (char-size (frame-char-size frame horizontal
))
2147 (mini (minibuffer-window frame
)))
2148 (set-window-new-total
2149 root
(window--pixel-to-size
2150 root
(window-size root horizontal t
) horizontal
))
2151 (unless (window-buffer root
)
2152 (window--pixel-to-total-1 root horizontal char-size
)))
2153 (window-resize-apply-total frame horizontal
))
2155 (defun window--resize-reset (&optional frame horizontal
)
2156 "Reset resize values for all windows on FRAME.
2157 FRAME defaults to the selected frame.
2159 This function stores the current value of `window-size' applied
2160 with argument HORIZONTAL in the new total size of all windows on
2161 FRAME. It also resets the new normal size of each of these
2163 (window--resize-reset-1
2164 (frame-root-window (window-normalize-frame frame
)) horizontal
))
2166 (defun window--resize-reset-1 (window horizontal
)
2167 "Internal function of `window--resize-reset'."
2168 ;; Register old size in the new total size.
2169 (set-window-new-pixel window
(window-size window horizontal t
))
2170 (set-window-new-total window
(window-size window horizontal
))
2171 ;; Reset new normal size.
2172 (set-window-new-normal window
)
2173 (when (window-child window
)
2174 (window--resize-reset-1 (window-child window
) horizontal
))
2175 (when (window-right window
)
2176 (window--resize-reset-1 (window-right window
) horizontal
)))
2178 ;; The following routine is used to manually resize the minibuffer
2179 ;; window and is currently used, for example, by ispell.el.
2180 (defun window--resize-mini-window (window delta
)
2181 "Resize minibuffer window WINDOW by DELTA pixels.
2182 If WINDOW cannot be resized by DELTA pixels make it as large (or
2183 as small) as possible, but don't signal an error."
2184 (when (window-minibuffer-p window
)
2185 (let* ((frame (window-frame window
))
2186 (root (frame-root-window frame
))
2187 (height (window-pixel-height window
))
2189 (- (window-pixel-height root
)
2190 (window-min-size root nil nil t
))))
2193 ((<= (+ height delta
) 0)
2194 (setq delta
(- (frame-char-height (window-frame window
)) height
)))
2195 ((> delta min-delta
)
2196 (setq delta min-delta
)))
2198 (unless (zerop delta
)
2200 (window--resize-reset frame
)
2201 ;; Ideally we should be able to resize just the last child of root
2202 ;; here. See the comment in `resize-root-window-vertically' for
2203 ;; why we do not do that.
2204 (window--resize-this-window root
(- delta
) nil nil t
)
2205 (set-window-new-pixel window
(+ height delta
))
2206 ;; The following routine catches the case where we want to resize
2207 ;; a minibuffer-only frame.
2208 (when (resize-mini-window-internal window
)
2209 (window--pixel-to-total frame
)
2210 (run-window-configuration-change-hook frame
))))))
2212 (defun window--resize-apply-p (frame &optional horizontal
)
2213 "Return t when a window on FRAME shall be resized vertically.
2214 Optional argument HORIZONTAL non-nil means return t when a window
2215 shall be resized horizontally."
2219 (unless (= (window-new-pixel window
)
2220 (window-size window horizontal t
))
2225 (defun window-resize (window delta
&optional horizontal ignore pixelwise
)
2226 "Resize WINDOW vertically by DELTA lines.
2227 WINDOW can be an arbitrary window and defaults to the selected
2228 one. An attempt to resize the root window of a frame will raise
2231 DELTA a positive number means WINDOW shall be enlarged by DELTA
2232 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2235 Optional argument HORIZONTAL non-nil means resize WINDOW
2236 horizontally by DELTA columns. In this case a positive DELTA
2237 means enlarge WINDOW by DELTA columns. DELTA negative means
2238 WINDOW shall be shrunk by -DELTA columns.
2240 Optional argument IGNORE non-nil means ignore restrictions
2241 imposed by fixed size windows, `window-min-height' or
2242 `window-min-width' settings. If IGNORE is a window, ignore
2243 restrictions for that window only. If IGNORE equals `safe',
2244 live windows may get as small as `window-safe-min-height' lines
2245 and `window-safe-min-width' columns. Any other non-nil value
2246 means ignore all of the above restrictions for all windows.
2248 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2251 This function resizes other windows proportionally and never
2252 deletes any windows. If you want to move only the low (right)
2253 edge of WINDOW consider using `adjust-window-trailing-edge'
2255 (setq window
(window-normalize-window window
))
2256 (let* ((frame (window-frame window
))
2257 (minibuffer-window (minibuffer-window frame
))
2259 (setq delta
(window--size-to-pixel
2260 window delta horizontal pixelwise t
))
2262 ((eq window
(frame-root-window frame
))
2263 (error "Cannot resize the root window of a frame"))
2264 ((window-minibuffer-p window
)
2266 (error "Cannot resize minibuffer window horizontally")
2267 (window--resize-mini-window window delta
)))
2268 ((and (not horizontal
)
2269 (window-full-height-p window
)
2270 (eq (window-frame minibuffer-window
) frame
)
2271 (or (not resize-mini-windows
)
2272 (eq minibuffer-window
(active-minibuffer-window))))
2273 ;; If WINDOW is full height and either `resize-mini-windows' is
2274 ;; nil or the minibuffer window is active, resize the minibuffer
2276 (window--resize-mini-window minibuffer-window
(- delta
)))
2277 ((window--resizable-p
2278 window delta horizontal ignore nil nil nil t
)
2279 (window--resize-reset frame horizontal
)
2280 (window--resize-this-window window delta horizontal ignore t
)
2281 (if (and (not window-combination-resize
)
2282 (window-combined-p window horizontal
)
2283 (setq sibling
(or (window-right window
) (window-left window
)))
2285 sibling
(- delta
) horizontal ignore t
))
2286 ;; If window-combination-resize is nil, WINDOW is part of an
2287 ;; iso-combination, and WINDOW's neighboring right or left
2288 ;; sibling can be resized as requested, resize that sibling.
2291 (window-size (window-parent window
) horizontal t
))))
2292 (window--resize-this-window sibling
(- delta
) horizontal nil t
)
2293 (set-window-new-normal
2294 window
(+ (window-normal-size window horizontal
)
2296 (set-window-new-normal
2297 sibling
(- (window-normal-size sibling horizontal
)
2299 ;; Otherwise, resize all other windows in the same combination.
2300 (window--resize-siblings window delta horizontal ignore
))
2301 (when (window--resize-apply-p frame horizontal
)
2302 (window-resize-apply frame horizontal
)
2303 (window--pixel-to-total frame horizontal
)
2304 (run-window-configuration-change-hook frame
)))
2306 (error "Cannot resize window %s" window
)))))
2308 (defun window-resize-no-error (window delta
&optional horizontal ignore pixelwise
)
2309 "Resize WINDOW vertically if it is resizable by DELTA lines.
2310 This function is like `window-resize' but does not signal an
2311 error when WINDOW cannot be resized. For the meaning of the
2312 optional arguments see the documentation of `window-resize'.
2314 Optional argument PIXELWISE non-nil means interpret DELTA as
2316 (when (window--resizable-p
2317 window delta horizontal ignore nil nil nil pixelwise
)
2318 (window-resize window delta horizontal ignore pixelwise
)))
2320 (defun window--resize-child-windows-skip-p (window)
2321 "Return non-nil if WINDOW shall be skipped by resizing routines."
2322 (memq (window-new-normal window
) '(ignore stuck skip
)))
2324 (defun window--resize-child-windows-normal (parent horizontal window this-delta
&optional trail other-delta
)
2325 "Recursively set new normal height of child windows of window PARENT.
2326 HORIZONTAL non-nil means set the new normal width of these
2327 windows. WINDOW specifies a child window of PARENT that has been
2328 resized by THIS-DELTA lines (columns).
2330 Optional argument TRAIL either `before' or `after' means set values
2331 only for windows before or after WINDOW. Optional argument
2332 OTHER-DELTA, a number, specifies that this many lines (columns)
2333 have been obtained from (or returned to) an ancestor window of
2334 PARENT in order to resize WINDOW."
2335 (let* ((delta-normal
2336 (if (and (= (- this-delta
)
2337 (window-size window horizontal t
))
2338 (zerop other-delta
))
2339 ;; When WINDOW gets deleted and we can return its entire
2340 ;; space to its siblings, use WINDOW's normal size as the
2342 (- (window-normal-size window horizontal
))
2343 ;; In any other case calculate the normal delta from the
2344 ;; relation of THIS-DELTA to the total size of PARENT.
2345 (/ (float this-delta
)
2346 (window-size parent horizontal t
))))
2347 (sub (window-child parent
))
2349 (skip (eq trail
'after
)))
2351 ;; Set parent-normal to the sum of the normal sizes of all child
2352 ;; windows of PARENT that shall be resized, excluding only WINDOW
2353 ;; and any windows specified by the optional TRAIL argument.
2357 (setq skip
(eq trail
'before
)))
2361 (+ parent-normal
(window-normal-size sub horizontal
)))))
2362 (setq sub
(window-right sub
)))
2364 ;; Set the new normal size of all child windows of PARENT from what
2365 ;; they should have contributed for recovering THIS-DELTA lines
2367 (setq sub
(window-child parent
))
2368 (setq skip
(eq trail
'after
))
2372 (setq skip
(eq trail
'before
)))
2375 (let ((old-normal (window-normal-size sub horizontal
)))
2376 (set-window-new-normal
2377 sub
(min 1.0 ; Don't get larger than 1.
2379 (* (/ old-normal parent-normal
)
2381 ;; Don't drop below 0.
2383 (setq sub
(window-right sub
)))
2385 (when (numberp other-delta
)
2386 ;; Set the new normal size of windows from what they should have
2387 ;; contributed for recovering OTHER-DELTA lines (columns).
2388 (setq delta-normal
(/ (float (window-size parent horizontal t
))
2389 (+ (window-size parent horizontal t
)
2391 (setq sub
(window-child parent
))
2392 (setq skip
(eq trail
'after
))
2396 (setq skip
(eq trail
'before
)))
2399 (set-window-new-normal
2400 sub
(min 1.0 ; Don't get larger than 1.
2401 (max (* (window-new-normal sub
) delta-normal
)
2402 ;; Don't drop below 0.
2404 (setq sub
(window-right sub
))))
2406 ;; Set the new normal size of WINDOW to what is left by the sum of
2407 ;; the normal sizes of its siblings.
2408 (set-window-new-normal
2411 (setq sub
(window-child parent
))
2415 ((not (numberp (window-new-normal sub
)))
2416 (setq sum
(+ sum
(window-normal-size sub horizontal
))))
2418 (setq sum
(+ sum
(window-new-normal sub
)))))
2419 (setq sub
(window-right sub
)))
2420 ;; Don't get larger than 1 or smaller than 0.
2421 (min 1.0 (max (- 1.0 sum
) 0.0))))))
2423 (defun window--resize-child-windows (parent delta
&optional horizontal window ignore trail edge char-size
)
2424 "Resize child windows of window PARENT vertically by DELTA pixels.
2425 PARENT must be a vertically combined internal window.
2427 Optional argument HORIZONTAL non-nil means resize child windows
2428 of PARENT horizontally by DELTA pixels. In this case PARENT must
2429 be a horizontally combined internal window.
2431 WINDOW, if specified, must denote a child window of PARENT that
2432 is resized by DELTA pixels.
2434 Optional argument IGNORE non-nil means ignore restrictions
2435 imposed by fixed size windows, `window-min-height' or
2436 `window-min-width' settings. If IGNORE equals `safe', live
2437 windows may get as small as `window-safe-min-height' lines and
2438 `window-safe-min-width' columns. If IGNORE is a window, ignore
2439 restrictions for that window only. Any other non-nil value means
2440 ignore all of the above restrictions for all windows.
2442 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2443 of windows that shall be resized. If TRAIL equals `before',
2444 resize only windows on the left or above EDGE. If TRAIL equals
2445 `after', resize only windows on the right or below EDGE. Also,
2446 preferably only resize windows adjacent to EDGE.
2448 If the optional argument CHAR-SIZE is a positive integer, it specifies
2449 the number of pixels by which windows are incrementally resized.
2450 If CHAR-SIZE is nil, this means to use the value of
2451 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2453 Return the symbol `normalized' if new normal sizes have been
2454 already set by this routine."
2455 (let* ((first (window-child parent
))
2456 (last (window-last-child parent
))
2457 (parent-total (+ (window-size parent horizontal t
)
2459 (char-size (or char-size
2460 (and window-resize-pixelwise
1)
2461 (frame-char-size window horizontal
)))
2462 sub best-window best-value best-delta
)
2464 (if (and edge
(memq trail
'(before after
))
2467 (while (and (window-right sub
)
2468 (or (and (eq trail
'before
)
2469 (not (window--resize-child-windows-skip-p
2470 (window-right sub
))))
2471 (and (eq trail
'after
)
2472 (window--resize-child-windows-skip-p sub
))))
2473 (setq sub
(window-right sub
)))
2476 (if (eq trail
'before
)
2477 (= (+ (window-pixel-left sub
) (window-pixel-width sub
))
2479 (= (window-pixel-left sub
) edge
))
2480 (if (eq trail
'before
)
2481 (= (+ (window-pixel-top sub
) (window-pixel-height sub
))
2483 (= (window-pixel-top sub
) edge
)))
2484 (window-sizable-p sub delta horizontal ignore t
))
2485 ;; Resize only windows adjacent to EDGE.
2487 (window--resize-this-window
2488 sub delta horizontal ignore t trail edge
)
2489 (if (and window
(eq (window-parent sub
) parent
))
2491 ;; Assign new normal sizes.
2492 (set-window-new-normal
2493 sub
(/ (float (window-new-pixel sub
)) parent-total
))
2494 (set-window-new-normal
2495 window
(- (window-normal-size window horizontal
)
2496 (- (window-new-normal sub
)
2497 (window-normal-size sub horizontal
)))))
2498 (window--resize-child-windows-normal
2499 parent horizontal sub
0 trail delta
))
2500 ;; Return 'normalized to notify `window--resize-siblings' that
2501 ;; normal sizes have been already set.
2503 ;; Resize all windows proportionally.
2507 ((or (window--resize-child-windows-skip-p sub
)
2508 ;; Ignore windows to skip and fixed-size child windows -
2509 ;; in the latter case make it a window to skip.
2511 (window-size-fixed-p sub horizontal
)
2512 (set-window-new-normal sub
'ignore
))))
2514 ;; When shrinking store the number of lines/cols we can get
2515 ;; from this window here together with the total/normal size
2517 (set-window-new-normal
2520 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
2521 (window-min-delta sub horizontal ignore trail t nil t
)
2522 (- (/ (float (window-size sub horizontal t
))
2524 (window-normal-size sub horizontal
)))))
2526 ;; When enlarging store the total/normal size factor only
2527 (set-window-new-normal
2529 (- (/ (float (window-size sub horizontal t
))
2531 (window-normal-size sub horizontal
)))))
2533 (setq sub
(window-left sub
)))
2537 ;; Shrink windows by delta.
2538 (setq best-window t
)
2539 (while (and best-window
(not (zerop delta
)))
2541 (setq best-window nil
)
2542 (setq best-value most-negative-fixnum
)
2544 (when (and (consp (window-new-normal sub
))
2545 (not (zerop (car (window-new-normal sub
))))
2546 (> (cdr (window-new-normal sub
)) best-value
))
2547 (setq best-window sub
)
2548 (setq best-value
(cdr (window-new-normal sub
))))
2550 (setq sub
(window-left sub
)))
2553 (setq best-delta
(min (car (window-new-normal best-window
))
2554 char-size
(- delta
)))
2555 (setq delta
(+ delta best-delta
))
2556 (set-window-new-pixel best-window
(- best-delta
) t
)
2557 (set-window-new-normal
2559 (if (= (car (window-new-normal best-window
)) best-delta
)
2560 'skip
; We can't shrink best-window any further.
2561 (cons (1- (car (window-new-normal best-window
)))
2562 (- (/ (float (window-new-pixel best-window
))
2564 (window-normal-size best-window horizontal
))))))))
2566 ;; Enlarge windows by delta.
2567 (setq best-window t
)
2568 (while (and best-window
(not (zerop delta
)))
2570 (setq best-window nil
)
2571 (setq best-value most-positive-fixnum
)
2573 (when (and (numberp (window-new-normal sub
))
2574 (< (window-new-normal sub
) best-value
))
2575 (setq best-window sub
)
2576 (setq best-value
(window-new-normal sub
)))
2578 (setq sub
(window-left sub
)))
2581 (setq best-delta
(min delta char-size
))
2582 (setq delta
(- delta best-delta
))
2583 (set-window-new-pixel best-window best-delta t
)
2584 (set-window-new-normal
2586 (- (/ (float (window-new-pixel best-window
))
2588 (window-normal-size best-window horizontal
)))))))
2593 (when (or (consp (window-new-normal sub
))
2594 (numberp (window-new-normal sub
)))
2595 ;; Reset new normal size fields so `window-resize-apply'
2596 ;; won't use them to apply new sizes.
2597 (set-window-new-normal sub
))
2599 (unless (eq (window-new-normal sub
) 'ignore
)
2600 ;; Resize this window's child windows (back-engineering
2601 ;; delta from sub's old and new total sizes).
2602 (let ((delta (- (window-new-pixel sub
)
2603 (window-size sub horizontal t
))))
2604 (unless (and (zerop delta
) (not trail
))
2605 ;; For the TRAIL non-nil case we have to resize SUB
2606 ;; recursively even if it's size does not change.
2607 (window--resize-this-window
2608 sub delta horizontal ignore nil trail edge
))))
2609 (setq sub
(window-left sub
)))))))
2611 (defun window--resize-siblings (window delta
&optional horizontal ignore trail edge char-size
)
2612 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
2613 Optional argument HORIZONTAL non-nil means resize other windows
2614 when WINDOW is resized horizontally by DELTA pixels. WINDOW
2615 itself is not resized by this function.
2617 Optional argument IGNORE non-nil means ignore restrictions
2618 imposed by fixed size windows, `window-min-height' or
2619 `window-min-width' settings. If IGNORE equals `safe', live
2620 windows may get as small as `window-safe-min-height' lines and
2621 `window-safe-min-width' columns. If IGNORE is a window, ignore
2622 restrictions for that window only. Any other non-nil value means
2623 ignore all of the above restrictions for all windows.
2625 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2626 of windows that shall be resized. If TRAIL equals `before',
2627 resize only windows on the left or above EDGE. If TRAIL equals
2628 `after', resize only windows on the right or below EDGE. Also,
2629 preferably only resize windows adjacent to EDGE."
2630 (when (window-parent window
)
2631 (let* ((parent (window-parent window
))
2632 (sub (window-child parent
)))
2633 (if (window-combined-p sub horizontal
)
2634 ;; In an iso-combination try to extract DELTA from WINDOW's
2636 (let ((skip (eq trail
'after
))
2637 this-delta other-delta
)
2638 ;; Decide which windows shall be left alone.
2642 ;; Make sure WINDOW is left alone when
2643 ;; resizing its siblings.
2644 (set-window-new-normal sub
'ignore
)
2645 (setq skip
(eq trail
'before
)))
2647 ;; Make sure this sibling is left alone when
2648 ;; resizing its siblings.
2649 (set-window-new-normal sub
'ignore
))
2650 ((or (window--size-ignore-p sub ignore
)
2651 (not (window-size-fixed-p sub horizontal
)))
2652 ;; Set this-delta to t to signal that we found a sibling
2653 ;; of WINDOW whose size is not fixed.
2654 (setq this-delta t
)))
2656 (setq sub
(window-right sub
)))
2658 ;; Set this-delta to what we can get from WINDOW's siblings.
2659 (if (= (- delta
) (window-size window horizontal t
))
2660 ;; A deletion, presumably. We must handle this case
2661 ;; specially since `window--resizable' can't be used.
2663 ;; There's at least one resizable sibling we can
2664 ;; give WINDOW's size to.
2665 (setq this-delta delta
)
2666 ;; No resizable sibling exists.
2667 (setq this-delta
0))
2668 ;; Any other form of resizing.
2671 window delta horizontal ignore trail t nil t
)))
2673 ;; Set other-delta to what we still have to get from
2674 ;; ancestor windows of parent.
2675 (setq other-delta
(- delta this-delta
))
2676 (unless (zerop other-delta
)
2677 ;; Unless we got everything from WINDOW's siblings, PARENT
2678 ;; must be resized by other-delta lines or columns.
2679 (set-window-new-pixel parent other-delta
'add
))
2681 (if (zerop this-delta
)
2682 ;; We haven't got anything from WINDOW's siblings but we
2683 ;; must update the normal sizes to respect other-delta.
2684 (window--resize-child-windows-normal
2685 parent horizontal window this-delta trail other-delta
)
2686 ;; We did get something from WINDOW's siblings which means
2687 ;; we have to resize their child windows.
2688 (unless (eq (window--resize-child-windows
2689 parent
(- this-delta
) horizontal
2690 window ignore trail edge char-size
)
2691 ;; If `window--resize-child-windows' returns
2692 ;; 'normalized, this means it has set the
2693 ;; normal sizes already.
2695 ;; Set the normal sizes.
2696 (window--resize-child-windows-normal
2697 parent horizontal window this-delta trail other-delta
))
2698 ;; Set DELTA to what we still have to get from ancestor
2700 (setq delta other-delta
)))
2702 ;; In an ortho-combination all siblings of WINDOW must be
2703 ;; resized by DELTA.
2704 (set-window-new-pixel parent delta
'add
)
2706 (unless (eq sub window
)
2707 (window--resize-this-window
2708 sub delta horizontal ignore t
))
2709 (setq sub
(window-right sub
))))
2711 (unless (zerop delta
)
2713 (window--resize-siblings
2714 parent delta horizontal ignore trail edge char-size
)))))
2716 (defun window--resize-this-window (window delta
&optional horizontal ignore add trail edge char-size
)
2717 "Resize WINDOW vertically by DELTA pixels.
2718 Optional argument HORIZONTAL non-nil means resize WINDOW
2719 horizontally by DELTA pixels.
2721 Optional argument IGNORE non-nil means ignore restrictions
2722 imposed by fixed size windows, `window-min-height' or
2723 `window-min-width' settings. If IGNORE equals `safe', live
2724 windows may get as small as `window-safe-min-height' lines and
2725 `window-safe-min-width' columns. If IGNORE is a window, ignore
2726 restrictions for that window only. Any other non-nil value
2727 means ignore all of the above restrictions for all windows.
2729 Optional argument ADD non-nil means add DELTA to the new total
2732 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2733 of windows that shall be resized. If TRAIL equals `before',
2734 resize only windows on the left or above EDGE. If TRAIL equals
2735 `after', resize only windows on the right or below EDGE. Also,
2736 preferably only resize windows adjacent to EDGE.
2738 If the optional argument CHAR-SIZE is a positive integer, it specifies
2739 the number of pixels by which windows are incrementally resized.
2740 If CHAR-SIZE is nil, this means to use the value of
2741 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2743 This function recursively resizes WINDOW's child windows to fit the
2744 new size. Make sure that WINDOW is `window--resizable' before
2745 calling this function. Note that this function does not resize
2746 siblings of WINDOW or WINDOW's parent window. You have to
2747 eventually call `window-resize-apply' in order to make resizing
2748 actually take effect."
2750 ;; Add DELTA to the new total size of WINDOW.
2751 (set-window-new-pixel window delta t
))
2753 (let ((sub (window-child window
)))
2756 ((window-combined-p sub horizontal
)
2757 ;; In an iso-combination resize child windows according to their
2759 (window--resize-child-windows
2760 window delta horizontal nil ignore trail edge char-size
))
2761 ;; In an ortho-combination resize each child window by DELTA.
2764 (window--resize-this-window
2765 sub delta horizontal ignore t trail edge char-size
)
2766 (setq sub
(window-right sub
)))))))
2768 (defun window--resize-root-window (window delta horizontal ignore pixelwise
)
2769 "Resize root window WINDOW vertically by DELTA lines.
2770 HORIZONTAL non-nil means resize root window WINDOW horizontally
2773 IGNORE non-nil means ignore any restrictions imposed by fixed
2774 size windows, `window-min-height' or `window-min-width' settings.
2776 This function is only called by the frame resizing routines. It
2777 resizes windows proportionally and never deletes any windows."
2778 (when (and (windowp window
) (numberp delta
))
2782 (window--size-to-pixel window delta horizontal
))))
2783 (when (window-sizable-p window pixel-delta horizontal ignore t
)
2784 (window--resize-reset (window-frame window
) horizontal
)
2785 (window--resize-this-window
2786 window pixel-delta horizontal ignore t
)))))
2788 (defun window--resize-root-window-vertically (window delta pixelwise
)
2789 "Resize root window WINDOW vertically by DELTA lines.
2790 If DELTA is less than zero and we can't shrink WINDOW by DELTA
2791 lines, shrink it as much as possible. If DELTA is greater than
2792 zero, this function can resize fixed-size windows in order to
2793 recover the necessary lines. Return the number of lines that
2796 Third argument PIXELWISE non-nil means to interpret DELTA as
2797 pixels and return the number of pixels that were recovered.
2799 This function is called by the minibuffer window resizing
2801 (let* ((frame (window-frame window
))
2807 (* (frame-char-height frame
) delta
))
2811 ((zerop pixel-delta
))
2813 (setq pixel-delta
(window-sizable window pixel-delta nil nil pixelwise
))
2814 (window--resize-reset frame
)
2815 ;; When shrinking the root window, emulate an edge drag in order
2816 ;; to not resize other windows if we can avoid it (Bug#12419).
2817 (window--resize-this-window
2818 window pixel-delta nil ignore t
'before
2819 (+ (window-pixel-top window
) (window-pixel-height window
)))
2820 ;; Don't record new normal sizes to make sure that shrinking back
2821 ;; proportionally works as intended.
2823 (lambda (window) (set-window-new-normal window
'ignore
)) frame t
))
2825 (window--resize-reset frame
)
2826 (unless (window-sizable window pixel-delta nil nil pixelwise
)
2828 ;; When growing the root window, resize proportionally. This
2829 ;; should give windows back their original sizes (hopefully).
2830 (window--resize-this-window
2831 window pixel-delta nil ignore t
)))
2832 ;; Return the possibly adjusted DELTA.
2835 (/ pixel-delta
(frame-char-height frame
)))))
2837 (defun adjust-window-trailing-edge (window delta
&optional horizontal pixelwise
)
2838 "Move WINDOW's bottom edge by DELTA lines.
2839 Optional argument HORIZONTAL non-nil means move WINDOW's right
2840 edge by DELTA columns. WINDOW must be a valid window and
2841 defaults to the selected one.
2843 Optional argument PIXELWISE non-nil means interpret DELTA as
2846 If DELTA is greater than zero, move the edge downwards or to the
2847 right. If DELTA is less than zero, move the edge upwards or to
2848 the left. If the edge can't be moved by DELTA lines or columns,
2849 move it as far as possible in the desired direction."
2850 (setq window
(window-normalize-window window
))
2851 (let* ((frame (window-frame window
))
2852 (minibuffer-window (minibuffer-window frame
))
2854 left this-delta min-delta max-delta
)
2858 (setq delta
(* delta
(frame-char-size window horizontal
))))
2860 ;; Find the edge we want to move.
2861 (while (and (or (not (window-combined-p right horizontal
))
2862 (not (window-right right
)))
2863 (setq right
(window-parent right
))))
2865 ((and (not right
) (not horizontal
)
2866 ;; Resize the minibuffer window if it's on the same frame as
2867 ;; and immediately below WINDOW and it's either active or
2868 ;; `resize-mini-windows' is nil.
2869 (eq (window-frame minibuffer-window
) frame
)
2870 (= (nth 1 (window-pixel-edges minibuffer-window
))
2871 (nth 3 (window-pixel-edges window
)))
2872 (or (not resize-mini-windows
)
2873 (eq minibuffer-window
(active-minibuffer-window))))
2874 (window--resize-mini-window minibuffer-window
(- delta
)))
2875 ((or (not (setq left right
)) (not (setq right
(window-right right
))))
2877 (error "No window on the right of this one")
2878 (error "No window below this one")))
2880 ;; Set LEFT to the first resizable window on the left. This step is
2881 ;; needed to handle fixed-size windows.
2882 (while (and left
(window-size-fixed-p left horizontal
))
2884 (or (window-left left
)
2886 (while (and (setq left
(window-parent left
))
2887 (not (window-combined-p left horizontal
))))
2888 (window-left left
)))))
2891 (error "No resizable window on the left of this one")
2892 (error "No resizable window above this one")))
2894 ;; Set RIGHT to the first resizable window on the right. This step
2895 ;; is needed to handle fixed-size windows.
2896 (while (and right
(window-size-fixed-p right horizontal
))
2898 (or (window-right right
)
2900 (while (and (setq right
(window-parent right
))
2901 (not (window-combined-p right horizontal
))))
2902 (window-right right
)))))
2905 (error "No resizable window on the right of this one")
2906 (error "No resizable window below this one")))
2908 ;; LEFT and RIGHT (which might be both internal windows) are now the
2909 ;; two windows we want to resize.
2913 (window--max-delta-1
2914 left
0 horizontal nil
'after nil pixelwise
))
2916 (window--min-delta-1
2917 right
(- delta
) horizontal nil
'before nil pixelwise
))
2918 (when (or (< max-delta delta
) (> min-delta
(- delta
)))
2919 ;; We can't get the whole DELTA - move as far as possible.
2920 (setq delta
(min max-delta
(- min-delta
))))
2921 (unless (zerop delta
)
2923 (window--resize-reset frame horizontal
)
2924 ;; Try to enlarge LEFT first.
2925 (setq this-delta
(window--resizable
2926 left delta horizontal nil nil nil nil pixelwise
))
2927 (unless (zerop this-delta
)
2928 (window--resize-this-window
2929 left this-delta horizontal nil t
'before
2931 (+ (window-pixel-left left
) (window-pixel-width left
))
2932 (+ (window-pixel-top left
) (window-pixel-height left
)))))
2933 ;; Shrink windows on right of LEFT.
2934 (window--resize-siblings
2935 left delta horizontal nil
'after
2937 (window-pixel-left right
)
2938 (window-pixel-top right
)))))
2941 (window--max-delta-1
2942 right
0 horizontal nil
'before nil pixelwise
))
2944 (window--min-delta-1
2945 left delta horizontal nil
'after nil pixelwise
))
2946 (when (or (< max-delta
(- delta
)) (> min-delta delta
))
2947 ;; We can't get the whole DELTA - move as far as possible.
2948 (setq delta
(max (- max-delta
) min-delta
)))
2949 (unless (zerop delta
)
2951 (window--resize-reset frame horizontal
)
2952 ;; Try to enlarge RIGHT.
2955 right
(- delta
) horizontal nil nil nil nil pixelwise
))
2956 (unless (zerop this-delta
)
2957 (window--resize-this-window
2958 right this-delta horizontal nil t
'after
2960 (window-pixel-left right
)
2961 (window-pixel-top right
))))
2962 ;; Shrink windows on left of RIGHT.
2963 (window--resize-siblings
2964 right
(- delta
) horizontal nil
'before
2966 (+ (window-pixel-left left
) (window-pixel-width left
))
2967 (+ (window-pixel-top left
) (window-pixel-height left
)))))))
2968 (unless (zerop delta
)
2969 ;; Don't report an error in the standard case.
2970 (when (window--resize-apply-p frame horizontal
)
2971 (if (window-resize-apply frame horizontal
)
2973 (window--pixel-to-total frame horizontal
)
2974 (run-window-configuration-change-hook frame
))
2975 ;; But do report an error if applying the changes fails.
2976 (error "Failed adjusting window %s" window
))))))))
2978 (defun enlarge-window (delta &optional horizontal
)
2979 "Make the selected window DELTA lines taller.
2980 Interactively, if no argument is given, make the selected window
2981 one line taller. If optional argument HORIZONTAL is non-nil,
2982 make selected window wider by DELTA columns. If DELTA is
2983 negative, shrink selected window by -DELTA lines or columns."
2985 (let ((minibuffer-window (minibuffer-window)))
2988 ((window-size-fixed-p nil horizontal
)
2989 (error "Selected window has fixed size"))
2990 ((window-minibuffer-p)
2992 (error "Cannot resize minibuffer window horizontally")
2993 (window--resize-mini-window (selected-window) delta
)))
2994 ((and (not horizontal
)
2995 (window-full-height-p)
2996 (eq (window-frame minibuffer-window
) (selected-frame))
2997 (not resize-mini-windows
))
2998 ;; If the selected window is full height and `resize-mini-windows'
2999 ;; is nil, resize the minibuffer window.
3000 (window--resize-mini-window minibuffer-window
(- delta
)))
3001 ((window--resizable-p nil delta horizontal
)
3002 (window-resize nil delta horizontal
))
3006 (window-max-delta nil horizontal
)
3007 (- (window-min-delta nil horizontal
)))
3010 (defun shrink-window (delta &optional horizontal
)
3011 "Make the selected window DELTA lines smaller.
3012 Interactively, if no argument is given, make the selected window
3013 one line smaller. If optional argument HORIZONTAL is non-nil,
3014 make selected window narrower by DELTA columns. If DELTA is
3015 negative, enlarge selected window by -DELTA lines or columns.
3016 Also see the `window-min-height' variable."
3018 (let ((minibuffer-window (minibuffer-window)))
3021 ((window-size-fixed-p nil horizontal
)
3022 (error "Selected window has fixed size"))
3023 ((window-minibuffer-p)
3025 (error "Cannot resize minibuffer window horizontally")
3026 (window--resize-mini-window (selected-window) (- delta
))))
3027 ((and (not horizontal
)
3028 (window-full-height-p)
3029 (eq (window-frame minibuffer-window
) (selected-frame))
3030 (not resize-mini-windows
))
3031 ;; If the selected window is full height and `resize-mini-windows'
3032 ;; is nil, resize the minibuffer window.
3033 (window--resize-mini-window minibuffer-window delta
))
3034 ((window--resizable-p nil
(- delta
) horizontal
)
3035 (window-resize nil
(- delta
) horizontal
))
3039 (- (window-min-delta nil horizontal
))
3040 (window-max-delta nil horizontal
))
3043 (defun maximize-window (&optional window pixelwise
)
3045 Make WINDOW as large as possible without deleting any windows.
3046 WINDOW must be a valid window and defaults to the selected one.
3048 If the option `window-resize-pixelwise' is non-nil maximize
3051 (setq window
(window-normalize-window window
))
3053 window
(window-max-delta window nil nil nil nil nil window-resize-pixelwise
)
3054 nil nil window-resize-pixelwise
)
3056 window
(window-max-delta window t nil nil nil nil window-resize-pixelwise
)
3057 t nil window-resize-pixelwise
))
3059 (defun minimize-window (&optional window pixelwise
)
3061 Make WINDOW as small as possible without deleting any windows.
3062 WINDOW must be a valid window and defaults to the selected one.
3064 If the option `window-resize-pixelwise' is non-nil minimize
3067 (setq window
(window-normalize-window window
))
3070 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise
))
3071 nil nil window-resize-pixelwise
)
3074 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise
))
3075 t nil window-resize-pixelwise
))
3077 (defun frame-root-window-p (window)
3078 "Return non-nil if WINDOW is the root window of its frame."
3079 (eq window
(frame-root-window window
)))
3081 (defun window--subtree (window &optional next
)
3082 "Return window subtree rooted at WINDOW.
3083 Optional argument NEXT non-nil means include WINDOW's right
3084 siblings in the return value.
3086 See the documentation of `window-tree' for a description of the
3093 ((window-top-child window
)
3094 (cons t
(cons (window-edges window
)
3095 (window--subtree (window-top-child window
) t
))))
3096 ((window-left-child window
)
3097 (cons nil
(cons (window-edges window
)
3098 (window--subtree (window-left-child window
) t
))))
3101 (setq window
(when next
(window-next-sibling window
))))
3104 (defun window-tree (&optional frame
)
3105 "Return the window tree of frame FRAME.
3106 FRAME must be a live frame and defaults to the selected frame.
3107 The return value is a list of the form (ROOT MINI), where ROOT
3108 represents the window tree of the frame's root window, and MINI
3109 is the frame's minibuffer window.
3111 If the root window is not split, ROOT is the root window itself.
3112 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3113 for a horizontal split, and t for a vertical split. EDGES gives
3114 the combined size and position of the child windows in the split,
3115 and the rest of the elements are the child windows in the split.
3116 Each of the child windows may again be a window or a list
3117 representing a window split, and so on. EDGES is a list (LEFT
3118 TOP RIGHT BOTTOM) as returned by `window-edges'."
3119 (setq frame
(window-normalize-frame frame
))
3120 (window--subtree (frame-root-window frame
) t
))
3122 (defun other-window (count &optional all-frames
)
3123 "Select another window in cyclic ordering of windows.
3124 COUNT specifies the number of windows to skip, starting with the
3125 selected window, before making the selection. If COUNT is
3126 positive, skip COUNT windows forwards. If COUNT is negative,
3127 skip -COUNT windows backwards. COUNT zero means do not skip any
3128 window, so select the selected window. In an interactive call,
3129 COUNT is the numeric prefix argument. Return nil.
3131 If the `other-window' parameter of the selected window is a
3132 function and `ignore-window-parameters' is nil, call that
3133 function with the arguments COUNT and ALL-FRAMES.
3135 This function does not select a window whose `no-other-window'
3136 window parameter is non-nil.
3138 This function uses `next-window' for finding the window to
3139 select. The argument ALL-FRAMES has the same meaning as in
3140 `next-window', but the MINIBUF argument of `next-window' is
3141 always effectively nil."
3143 (let* ((window (selected-window))
3144 (function (and (not ignore-window-parameters
)
3145 (window-parameter window
'other-window
)))
3146 old-window old-count
)
3147 (if (functionp function
)
3148 (funcall function count all-frames
)
3149 ;; `next-window' and `previous-window' may return a window we are
3150 ;; not allowed to select. Hence we need an exit strategy in case
3151 ;; all windows are non-selectable.
3154 (setq window
(next-window window nil all-frames
))
3156 ((eq window old-window
)
3157 (when (= count old-count
)
3158 ;; Keep out of infinite loops. When COUNT has not changed
3159 ;; since we last looked at `window' we're probably in one.
3161 ((window-parameter window
'no-other-window
)
3163 ;; The first non-selectable window `next-window' got us:
3164 ;; Remember it and the current value of COUNT.
3165 (setq old-window window
)
3166 (setq old-count count
)))
3168 (setq count
(1- count
)))))
3170 (setq window
(previous-window window nil all-frames
))
3172 ((eq window old-window
)
3173 (when (= count old-count
)
3174 ;; Keep out of infinite loops. When COUNT has not changed
3175 ;; since we last looked at `window' we're probably in one.
3177 ((window-parameter window
'no-other-window
)
3179 ;; The first non-selectable window `previous-window' got
3180 ;; us: Remember it and the current value of COUNT.
3181 (setq old-window window
)
3182 (setq old-count count
)))
3184 (setq count
(1+ count
)))))
3186 (select-window window
)
3187 ;; Always return nil.
3190 ;; This should probably return non-nil when the selected window is part
3191 ;; of an atomic window whose root is the frame's root window.
3192 (defun one-window-p (&optional nomini all-frames
)
3193 "Return non-nil if the selected window is the only window.
3194 Optional arg NOMINI non-nil means don't count the minibuffer
3195 even if it is active. Otherwise, the minibuffer is counted
3198 Optional argument ALL-FRAMES specifies the set of frames to
3199 consider, see also `next-window'. ALL-FRAMES nil or omitted
3200 means consider windows on the selected frame only, plus the
3201 minibuffer window if specified by the NOMINI argument. If the
3202 minibuffer counts, consider all windows on all frames that share
3203 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3204 with a special meaning are:
3206 - t means consider all windows on all existing frames.
3208 - `visible' means consider all windows on all visible frames on
3209 the current terminal.
3211 - 0 (the number zero) means consider all windows on all visible
3212 and iconified frames on the current terminal.
3214 - A frame means consider all windows on that frame only.
3216 Anything else means consider all windows on the selected frame
3218 (let ((base-window (selected-window)))
3219 (if (and nomini
(eq base-window
(minibuffer-window)))
3220 (setq base-window
(next-window base-window
)))
3222 (next-window base-window
(if nomini
'arg
) all-frames
))))
3224 ;;; Deleting windows.
3225 (defun window-deletable-p (&optional window
)
3226 "Return t if WINDOW can be safely deleted from its frame.
3227 WINDOW must be a valid window and defaults to the selected one.
3228 Return 'frame if deleting WINDOW should also delete its frame."
3229 (setq window
(window-normalize-window window
))
3231 (unless (or ignore-window-parameters
3232 (eq (window-parameter window
'delete-window
) t
))
3233 ;; Handle atomicity.
3234 (when (window-parameter window
'window-atom
)
3235 (setq window
(window-atom-root window
))))
3237 (let ((frame (window-frame window
)))
3239 ((frame-root-window-p window
)
3240 ;; WINDOW's frame can be deleted only if there are other frames
3241 ;; on the same terminal, and it does not contain the active
3243 (unless (or (eq frame
(next-frame frame
0))
3244 ;; We can delete our frame only if no other frame
3245 ;; currently uses our minibuffer window.
3247 (dolist (other (frame-list))
3248 (when (and (not (eq other frame
))
3249 (eq (window-frame (minibuffer-window other
))
3252 (let ((minibuf (active-minibuffer-window)))
3253 (and minibuf
(eq frame
(window-frame minibuf
)))))
3255 ((or ignore-window-parameters
3256 (not (eq window
(window--major-non-side-window frame
))))
3257 ;; WINDOW can be deleted unless it is the major non-side window of
3261 (defun window--in-subtree-p (window root
)
3262 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
3263 (or (eq window root
)
3264 (let ((parent (window-parent window
)))
3267 (if (eq parent root
)
3269 (setq parent
(window-parent parent
))))))))
3271 (defun delete-window (&optional window
)
3273 WINDOW must be a valid window and defaults to the selected one.
3276 If the variable `ignore-window-parameters' is non-nil or the
3277 `delete-window' parameter of WINDOW equals t, do not process any
3278 parameters of WINDOW. Otherwise, if the `delete-window'
3279 parameter of WINDOW specifies a function, call that function with
3280 WINDOW as its sole argument and return the value returned by that
3283 Otherwise, if WINDOW is part of an atomic window, call
3284 `delete-window' with the root of the atomic window as its
3285 argument. Signal an error if WINDOW is either the only window on
3286 its frame, the last non-side window, or part of an atomic window
3287 that is its frame's root window."
3289 (setq window
(window-normalize-window window
))
3290 (let* ((frame (window-frame window
))
3291 (function (window-parameter window
'delete-window
))
3292 (parent (window-parent window
))
3294 (window--check frame
)
3296 ;; Handle window parameters.
3298 ;; Ignore window parameters if `ignore-window-parameters' tells
3299 ;; us so or `delete-window' equals t.
3300 ((or ignore-window-parameters
(eq function t
)))
3301 ((functionp function
)
3302 ;; The `delete-window' parameter specifies the function to call.
3303 ;; If that function is `ignore' nothing is done. It's up to the
3304 ;; function called here to avoid infinite recursion.
3305 (throw 'done
(funcall function window
)))
3306 ((and (window-parameter window
'window-atom
)
3307 (setq atom-root
(window-atom-root window
))
3308 (not (eq atom-root window
)))
3309 (if (eq atom-root
(frame-root-window frame
))
3310 (error "Root of atomic window is root window of its frame")
3311 (throw 'done
(delete-window atom-root
))))
3313 (error "Attempt to delete minibuffer or sole ordinary window"))
3314 ((eq window
(window--major-non-side-window frame
))
3315 (error "Attempt to delete last non-side window")))
3317 (let* ((horizontal (window-left-child parent
))
3318 (size (window-size window horizontal t
))
3320 (window--in-subtree-p (frame-selected-window frame
) window
))
3321 ;; Emacs 23 preferably gives WINDOW's space to its left
3323 (sibling (or (window-left window
) (window-right window
))))
3324 (window--resize-reset frame horizontal
)
3326 ((and (not window-combination-resize
)
3327 sibling
(window-sizable-p sibling size horizontal nil t
))
3328 ;; Resize WINDOW's sibling.
3329 (window--resize-this-window sibling size horizontal nil t
)
3330 (set-window-new-normal
3331 sibling
(+ (window-normal-size sibling horizontal
)
3332 (window-normal-size window horizontal
))))
3333 ((window--resizable-p window
(- size
) horizontal nil nil nil t t
)
3334 ;; Can do without resizing fixed-size windows.
3335 (window--resize-siblings window
(- size
) horizontal
))
3337 ;; Can't do without resizing fixed-size windows.
3338 (window--resize-siblings window
(- size
) horizontal t
)))
3339 ;; Actually delete WINDOW.
3340 (delete-window-internal window
)
3341 (window--pixel-to-total frame horizontal
)
3342 (when (and frame-selected
3344 (frame-selected-window frame
) 'no-other-window
))
3345 ;; `delete-window-internal' has selected a window that should
3346 ;; not be selected, fix this here.
3347 (other-window -
1 frame
))
3348 (run-window-configuration-change-hook frame
)
3349 (window--check frame
)
3350 ;; Always return nil.
3353 (defun delete-other-windows (&optional window
)
3354 "Make WINDOW fill its frame.
3355 WINDOW must be a valid window and defaults to the selected one.
3358 If the variable `ignore-window-parameters' is non-nil or the
3359 `delete-other-windows' parameter of WINDOW equals t, do not
3360 process any parameters of WINDOW. Otherwise, if the
3361 `delete-other-windows' parameter of WINDOW specifies a function,
3362 call that function with WINDOW as its sole argument and return
3363 the value returned by that function.
3365 Otherwise, if WINDOW is part of an atomic window, call this
3366 function with the root of the atomic window as its argument. If
3367 WINDOW is a non-side window, make WINDOW the only non-side window
3368 on the frame. Side windows are not deleted. If WINDOW is a side
3369 window signal an error."
3371 (setq window
(window-normalize-window window
))
3372 (let* ((frame (window-frame window
))
3373 (function (window-parameter window
'delete-other-windows
))
3374 (window-side (window-parameter window
'window-side
))
3375 atom-root side-main
)
3376 (window--check frame
)
3379 ;; Ignore window parameters if `ignore-window-parameters' is t or
3380 ;; `delete-other-windows' is t.
3381 ((or ignore-window-parameters
(eq function t
)))
3382 ((functionp function
)
3383 ;; The `delete-other-windows' parameter specifies the function
3384 ;; to call. If the function is `ignore' no windows are deleted.
3385 ;; It's up to the function called to avoid infinite recursion.
3386 (throw 'done
(funcall function window
)))
3387 ((and (window-parameter window
'window-atom
)
3388 (setq atom-root
(window-atom-root window
))
3389 (not (eq atom-root window
)))
3390 (if (eq atom-root
(frame-root-window frame
))
3391 (error "Root of atomic window is root window of its frame")
3392 (throw 'done
(delete-other-windows atom-root
))))
3393 ((memq window-side window-sides
)
3394 (error "Cannot make side window the only window"))
3395 ((and (window-minibuffer-p window
)
3396 (not (eq window
(frame-root-window window
))))
3397 (error "Can't expand minibuffer to full frame")))
3399 ;; If WINDOW is the major non-side window, do nothing.
3400 (if (window-with-parameter 'window-side
)
3401 (setq side-main
(window--major-non-side-window frame
))
3402 (setq side-main
(frame-root-window frame
)))
3403 (unless (eq window side-main
)
3404 (delete-other-windows-internal window side-main
)
3405 (run-window-configuration-change-hook frame
)
3406 (window--check frame
))
3407 ;; Always return nil.
3410 (defun delete-other-windows-vertically (&optional window
)
3411 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
3412 This may be a useful alternative binding for \\[delete-other-windows]
3413 if you often split windows horizontally."
3415 (let* ((window (or window
(selected-window)))
3416 (edges (window-edges window
))
3418 (while (not (eq (setq w
(next-window w
1)) window
))
3419 (let ((e (window-edges w
)))
3420 (when (and (= (car e
) (car edges
))
3421 (= (nth 2 e
) (nth 2 edges
)))
3423 (mapc 'delete-window delenda
)))
3425 ;;; Windows and buffers.
3427 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
3428 ;; for (1) determining which buffer to show in the window when its
3429 ;; buffer shall be buried or killed and (2) which buffer to show for
3430 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3432 ;; `prev-buffers' consists of <buffer, window-start, window-point>
3433 ;; triples. The entries on this list are ordered by the time their
3434 ;; buffer has been removed from the window, the most recently removed
3435 ;; buffer's entry being first. The window-start and window-point
3436 ;; components are `window-start' and `window-point' at the time the
3437 ;; buffer was removed from the window which implies that the entry must
3438 ;; be added when `set-window-buffer' removes the buffer from the window.
3440 ;; `next-buffers' is the list of buffers that have been replaced
3441 ;; recently by `switch-to-prev-buffer'. These buffers are the least
3442 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
3443 ;; candidates of `switch-to-next-buffer' to switch to. This list is
3444 ;; reset to nil by any action changing the window's buffer with the
3445 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
3446 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
3447 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
3449 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
3450 ;; if such a buffer was killed while the window was hidden within a
3451 ;; window configuration. Such killed buffers get removed whenever
3452 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
3454 ;; The following function is called by `set-window-buffer' _before_ it
3455 ;; replaces the buffer of the argument window with the new buffer.
3456 (defun record-window-buffer (&optional window
)
3457 "Record WINDOW's buffer.
3458 WINDOW must be a live window and defaults to the selected one."
3459 (let* ((window (window-normalize-window window t
))
3460 (buffer (window-buffer window
))
3461 (entry (assq buffer
(window-prev-buffers window
))))
3462 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
3463 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3464 (set-window-next-buffers window nil
)
3467 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
3468 (set-window-prev-buffers
3469 window
(assq-delete-all buffer
(window-prev-buffers window
))))
3471 ;; Don't record insignificant buffers.
3472 (unless (eq (aref (buffer-name buffer
) 0) ?\s
)
3473 ;; Add an entry for buffer to WINDOW's previous buffers.
3474 (with-current-buffer buffer
3475 (let ((start (window-start window
))
3476 (point (window-point window
)))
3480 ;; We have an entry, update marker positions.
3481 (list (set-marker (nth 1 entry
) start
)
3482 (set-marker (nth 2 entry
) point
))
3483 ;; Make new markers.
3484 (list (copy-marker start
)
3486 ;; Preserve window-point-insertion-type
3488 point window-point-insertion-type
)))))
3489 (set-window-prev-buffers
3490 window
(cons entry
(window-prev-buffers window
)))))
3492 (run-hooks 'buffer-list-update-hook
))))
3494 (defun unrecord-window-buffer (&optional window buffer
)
3495 "Unrecord BUFFER in WINDOW.
3496 WINDOW must be a live window and defaults to the selected one.
3497 BUFFER must be a live buffer and defaults to the buffer of
3499 (let* ((window (window-normalize-window window t
))
3500 (buffer (or buffer
(window-buffer window
))))
3501 (set-window-prev-buffers
3502 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3503 (set-window-next-buffers
3504 window
(delq buffer
(window-next-buffers window
)))))
3506 (defun set-window-buffer-start-and-point (window buffer
&optional start point
)
3507 "Set WINDOW's buffer to BUFFER.
3508 WINDOW must be a live window and defaults to the selected one.
3509 Optional argument START non-nil means set WINDOW's start position
3510 to START. Optional argument POINT non-nil means set WINDOW's
3511 point to POINT. If WINDOW is selected this also sets BUFFER's
3512 `point' to POINT. If WINDOW is selected and the buffer it showed
3513 before was current this also makes BUFFER the current buffer."
3514 (setq window
(window-normalize-window window t
))
3515 (let ((selected (eq window
(selected-window)))
3516 (current (eq (window-buffer window
) (current-buffer))))
3517 (set-window-buffer window buffer
)
3518 (when (and selected current
)
3519 (set-buffer buffer
))
3521 ;; Don't force window-start here (even if POINT is nil).
3522 (set-window-start window start t
))
3524 (set-window-point window point
))))
3526 (defcustom switch-to-visible-buffer t
3527 "If non-nil, allow switching to an already visible buffer.
3528 If this variable is non-nil, `switch-to-prev-buffer' and
3529 `switch-to-next-buffer' may switch to an already visible buffer
3530 provided the buffer was shown before in the window specified as
3531 argument to those functions. If this variable is nil,
3532 `switch-to-prev-buffer' and `switch-to-next-buffer' always try to
3533 avoid switching to a buffer that is already visible in another
3534 window on the same frame."
3539 (defun switch-to-prev-buffer (&optional window bury-or-kill
)
3540 "In WINDOW switch to previous buffer.
3541 WINDOW must be a live window and defaults to the selected one.
3542 Return the buffer switched to, nil if no suitable buffer could be
3545 Optional argument BURY-OR-KILL non-nil means the buffer currently
3546 shown in WINDOW is about to be buried or killed and consequently
3547 shall not be switched to in future invocations of this command.
3549 As a special case, if BURY-OR-KILL equals `append', this means to
3550 move the buffer to the end of WINDOW's previous buffers list so a
3551 future invocation of `switch-to-prev-buffer' less likely switches
3554 (let* ((window (window-normalize-window window t
))
3555 (frame (window-frame window
))
3556 (old-buffer (window-buffer window
))
3557 ;; Save this since it's destroyed by `set-window-buffer'.
3558 (next-buffers (window-next-buffers window
))
3559 (pred (frame-parameter frame
'buffer-predicate
))
3560 entry new-buffer killed-buffers visible
)
3561 (when (window-minibuffer-p window
)
3562 ;; Don't switch in minibuffer window.
3563 (unless (setq window
(minibuffer-selected-window))
3564 (error "Window %s is a minibuffer window" window
)))
3566 (when (window-dedicated-p window
)
3567 ;; Don't switch in dedicated window.
3568 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3571 ;; Scan WINDOW's previous buffers first, skipping entries of next
3573 (dolist (entry (window-prev-buffers window
))
3574 (when (and (setq new-buffer
(car entry
))
3575 (or (buffer-live-p new-buffer
)
3576 (not (setq killed-buffers
3577 (cons new-buffer killed-buffers
))))
3578 (not (eq new-buffer old-buffer
))
3579 (or (null pred
) (funcall pred new-buffer
))
3580 ;; When BURY-OR-KILL is nil, avoid switching to a
3581 ;; buffer in WINDOW's next buffers list.
3582 (or bury-or-kill
(not (memq new-buffer next-buffers
))))
3583 (if (and (not switch-to-visible-buffer
)
3584 (get-buffer-window new-buffer frame
))
3585 ;; Try to avoid showing a buffer visible in some other
3587 (setq visible new-buffer
)
3588 (set-window-buffer-start-and-point
3589 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3591 ;; Scan reverted buffer list of WINDOW's frame next, skipping
3592 ;; entries of next buffers. Note that when we bury or kill a
3593 ;; buffer we don't reverse the global buffer list to avoid showing
3594 ;; a buried buffer instead. Otherwise, we must reverse the global
3595 ;; buffer list in order to make sure that switching to the
3596 ;; previous/next buffer traverse it in opposite directions.
3597 (dolist (buffer (if bury-or-kill
3599 (nreverse (buffer-list frame
))))
3600 (when (and (buffer-live-p buffer
)
3601 (not (eq buffer old-buffer
))
3602 (or (null pred
) (funcall pred buffer
))
3603 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3604 (or bury-or-kill
(not (memq buffer next-buffers
))))
3605 (if (get-buffer-window buffer frame
)
3606 ;; Try to avoid showing a buffer visible in some other window.
3608 (setq visible buffer
))
3609 (setq new-buffer buffer
)
3610 (set-window-buffer-start-and-point window new-buffer
)
3612 (unless bury-or-kill
3613 ;; Scan reverted next buffers last (must not use nreverse
3615 (dolist (buffer (reverse next-buffers
))
3616 ;; Actually, buffer _must_ be live here since otherwise it
3617 ;; would have been caught in the scan of previous buffers.
3618 (when (and (or (buffer-live-p buffer
)
3619 (not (setq killed-buffers
3620 (cons buffer killed-buffers
))))
3621 (not (eq buffer old-buffer
))
3622 (or (null pred
) (funcall pred buffer
))
3623 (setq entry
(assq buffer
(window-prev-buffers window
))))
3624 (setq new-buffer buffer
)
3625 (set-window-buffer-start-and-point
3626 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3629 ;; Show a buffer visible in another window.
3631 (setq new-buffer visible
)
3632 (set-window-buffer-start-and-point window new-buffer
)))
3635 (let ((entry (and (eq bury-or-kill
'append
)
3636 (assq old-buffer
(window-prev-buffers window
)))))
3637 ;; Remove `old-buffer' from WINDOW's previous and (restored list
3638 ;; of) next buffers.
3639 (set-window-prev-buffers
3640 window
(assq-delete-all old-buffer
(window-prev-buffers window
)))
3641 (set-window-next-buffers window
(delq old-buffer next-buffers
))
3643 ;; Append old-buffer's entry to list of WINDOW's previous
3644 ;; buffers so it's less likely to get switched to soon but
3645 ;; `display-buffer-in-previous-window' can nevertheless find
3647 (set-window-prev-buffers
3648 window
(append (window-prev-buffers window
) (list entry
)))))
3649 ;; Move `old-buffer' to head of WINDOW's restored list of next
3651 (set-window-next-buffers
3652 window
(cons old-buffer
(delq old-buffer next-buffers
))))
3654 ;; Remove killed buffers from WINDOW's previous and next buffers.
3655 (when killed-buffers
3656 (dolist (buffer killed-buffers
)
3657 (set-window-prev-buffers
3658 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3659 (set-window-next-buffers
3660 window
(delq buffer
(window-next-buffers window
)))))
3662 ;; Return new-buffer.
3665 (defun switch-to-next-buffer (&optional window
)
3666 "In WINDOW switch to next buffer.
3667 WINDOW must be a live window and defaults to the selected one.
3668 Return the buffer switched to, nil if no suitable buffer could be
3671 (let* ((window (window-normalize-window window t
))
3672 (frame (window-frame window
))
3673 (old-buffer (window-buffer window
))
3674 (next-buffers (window-next-buffers window
))
3675 (pred (frame-parameter frame
'buffer-predicate
))
3676 new-buffer entry killed-buffers visible
)
3677 (when (window-minibuffer-p window
)
3678 ;; Don't switch in minibuffer window.
3679 (unless (setq window
(minibuffer-selected-window))
3680 (error "Window %s is a minibuffer window" window
)))
3682 (when (window-dedicated-p window
)
3683 ;; Don't switch in dedicated window.
3684 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3687 ;; Scan WINDOW's next buffers first.
3688 (dolist (buffer next-buffers
)
3689 (when (and (or (buffer-live-p buffer
)
3690 (not (setq killed-buffers
3691 (cons buffer killed-buffers
))))
3692 (not (eq buffer old-buffer
))
3693 (or (null pred
) (funcall pred buffer
))
3694 (setq entry
(assq buffer
(window-prev-buffers window
))))
3695 (setq new-buffer buffer
)
3696 (set-window-buffer-start-and-point
3697 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3699 ;; Scan the buffer list of WINDOW's frame next, skipping previous
3701 (dolist (buffer (buffer-list frame
))
3702 (when (and (buffer-live-p buffer
)
3703 (not (eq buffer old-buffer
))
3704 (or (null pred
) (funcall pred buffer
))
3705 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3706 (not (assq buffer
(window-prev-buffers window
))))
3707 (if (get-buffer-window buffer frame
)
3708 ;; Try to avoid showing a buffer visible in some other window.
3709 (setq visible buffer
)
3710 (setq new-buffer buffer
)
3711 (set-window-buffer-start-and-point window new-buffer
)
3713 ;; Scan WINDOW's reverted previous buffers last (must not use
3715 (dolist (entry (reverse (window-prev-buffers window
)))
3716 (when (and (setq new-buffer
(car entry
))
3717 (or (buffer-live-p new-buffer
)
3718 (not (setq killed-buffers
3719 (cons new-buffer killed-buffers
))))
3720 (not (eq new-buffer old-buffer
))
3721 (or (null pred
) (funcall pred new-buffer
)))
3722 (if (and (not switch-to-visible-buffer
)
3723 (get-buffer-window new-buffer frame
))
3724 ;; Try to avoid showing a buffer visible in some other window.
3726 (setq visible new-buffer
))
3727 (set-window-buffer-start-and-point
3728 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3731 ;; Show a buffer visible in another window.
3733 (setq new-buffer visible
)
3734 (set-window-buffer-start-and-point window new-buffer
)))
3736 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
3737 (set-window-next-buffers window
(delq new-buffer next-buffers
))
3739 ;; Remove killed buffers from WINDOW's previous and next buffers.
3740 (when killed-buffers
3741 (dolist (buffer killed-buffers
)
3742 (set-window-prev-buffers
3743 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3744 (set-window-next-buffers
3745 window
(delq buffer
(window-next-buffers window
)))))
3747 ;; Return new-buffer.
3750 (defun get-next-valid-buffer (list &optional buffer visible-ok frame
)
3751 "Search LIST for a valid buffer to display in FRAME.
3752 Return nil when all buffers in LIST are undesirable for display,
3753 otherwise return the first suitable buffer in LIST.
3755 Buffers not visible in windows are preferred to visible buffers,
3756 unless VISIBLE-OK is non-nil.
3757 If the optional argument FRAME is nil, it defaults to the selected frame.
3758 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
3759 ;; This logic is more or less copied from other-buffer.
3760 (setq frame
(or frame
(selected-frame)))
3761 (let ((pred (frame-parameter frame
'buffer-predicate
))
3763 (while (and (not found
) list
)
3764 (setq buf
(car list
))
3765 (if (and (not (eq buffer buf
))
3767 (or (null pred
) (funcall pred buf
))
3768 (not (eq (aref (buffer-name buf
) 0) ?\s
))
3769 (or visible-ok
(null (get-buffer-window buf
'visible
))))
3771 (setq list
(cdr list
))))
3774 (defun last-buffer (&optional buffer visible-ok frame
)
3775 "Return the last buffer in FRAME's buffer list.
3776 If BUFFER is the last buffer, return the preceding buffer
3777 instead. Buffers not visible in windows are preferred to visible
3778 buffers, unless optional argument VISIBLE-OK is non-nil.
3779 Optional third argument FRAME nil or omitted means use the
3780 selected frame's buffer list. If no such buffer exists, return
3781 the buffer `*scratch*', creating it if necessary."
3782 (setq frame
(or frame
(selected-frame)))
3783 (or (get-next-valid-buffer (nreverse (buffer-list frame
))
3784 buffer visible-ok frame
)
3785 (get-buffer "*scratch*")
3786 (let ((scratch (get-buffer-create "*scratch*")))
3787 (set-buffer-major-mode scratch
)
3790 (defcustom frame-auto-hide-function
#'iconify-frame
3791 "Function called to automatically hide frames.
3792 The function is called with one argument - a frame.
3794 Functions affected by this option are those that bury a buffer
3795 shown in a separate frame like `quit-window' and `bury-buffer'."
3796 :type
'(choice (const :tag
"Iconify" iconify-frame
)
3797 (const :tag
"Delete" delete-frame
)
3798 (const :tag
"Do nothing" ignore
)
3804 (defun window--delete (&optional window dedicated-only kill
)
3805 "Delete WINDOW if possible.
3806 WINDOW must be a live window and defaults to the selected one.
3807 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
3808 only if it's dedicated to its buffer. Optional argument KILL
3809 means the buffer shown in window will be killed. Return non-nil
3810 if WINDOW gets deleted or its frame is auto-hidden."
3811 (setq window
(window-normalize-window window t
))
3812 (unless (and dedicated-only
(not (window-dedicated-p window
)))
3813 (let ((deletable (window-deletable-p window
)))
3815 ((eq deletable
'frame
)
3816 (let ((frame (window-frame window
)))
3819 (delete-frame frame
))
3820 ((functionp frame-auto-hide-function
)
3821 (funcall frame-auto-hide-function frame
))))
3824 (delete-window window
)
3827 (defun bury-buffer (&optional buffer-or-name
)
3828 "Put BUFFER-OR-NAME at the end of the list of all buffers.
3829 There it is the least likely candidate for `other-buffer' to
3830 return; thus, the least likely buffer for \\[switch-to-buffer] to
3833 You can specify a buffer name as BUFFER-OR-NAME, or an actual
3834 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
3835 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
3836 remove the current buffer from the selected window if it is
3839 (let* ((buffer (window-normalize-buffer buffer-or-name
)))
3840 ;; If `buffer-or-name' is not on the selected frame we unrecord it
3841 ;; although it's not "here" (call it a feature).
3842 (bury-buffer-internal buffer
)
3843 ;; Handle case where `buffer-or-name' is nil and the current buffer
3844 ;; is shown in the selected window.
3846 ((or buffer-or-name
(not (eq buffer
(window-buffer)))))
3847 ((window--delete nil t
))
3849 ;; Switch to another buffer in window.
3850 (set-window-dedicated-p nil nil
)
3851 (switch-to-prev-buffer nil
'bury
)))
3853 ;; Always return nil.
3856 (defun unbury-buffer ()
3857 "Switch to the last buffer in the buffer list."
3859 (switch-to-buffer (last-buffer)))
3861 (defun next-buffer ()
3862 "In selected window switch to next buffer."
3865 ((window-minibuffer-p)
3866 (error "Cannot switch buffers in minibuffer window"))
3867 ((eq (window-dedicated-p) t
)
3868 (error "Window is strongly dedicated to its buffer"))
3870 (switch-to-next-buffer))))
3872 (defun previous-buffer ()
3873 "In selected window switch to previous buffer."
3876 ((window-minibuffer-p)
3877 (error "Cannot switch buffers in minibuffer window"))
3878 ((eq (window-dedicated-p) t
)
3879 (error "Window is strongly dedicated to its buffer"))
3881 (switch-to-prev-buffer))))
3883 (defun delete-windows-on (&optional buffer-or-name frame
)
3884 "Delete all windows showing BUFFER-OR-NAME.
3885 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3886 and defaults to the current buffer.
3888 The following non-nil values of the optional argument FRAME
3889 have special meanings:
3891 - t means consider all windows on the selected frame only.
3893 - `visible' means consider all windows on all visible frames on
3894 the current terminal.
3896 - 0 (the number zero) means consider all windows on all visible
3897 and iconified frames on the current terminal.
3899 - A frame means consider all windows on that frame only.
3901 Any other value of FRAME means consider all windows on all
3904 When a window showing BUFFER-OR-NAME is dedicated and the only
3905 window of its frame, that frame is deleted when there are other
3907 (interactive "BDelete windows on (buffer):\nP")
3908 (let ((buffer (window-normalize-buffer buffer-or-name
))
3909 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3910 ;; `window-list-1' based function.
3911 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
3912 (dolist (window (window-list-1 nil nil all-frames
))
3913 (if (eq (window-buffer window
) buffer
)
3914 (let ((deletable (window-deletable-p window
)))
3916 ((and (eq deletable
'frame
) (window-dedicated-p window
))
3917 ;; Delete frame if and only if window is dedicated.
3918 (delete-frame (window-frame window
)))
3921 (delete-window window
))
3923 ;; In window switch to previous buffer.
3924 (set-window-dedicated-p window nil
)
3925 (switch-to-prev-buffer window
'bury
))))
3926 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3927 (unrecord-window-buffer window buffer
)))))
3929 (defun replace-buffer-in-windows (&optional buffer-or-name
)
3930 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
3931 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3932 and defaults to the current buffer.
3934 When a window showing BUFFER-OR-NAME is dedicated, that window is
3935 deleted. If that window is the only window on its frame, the
3936 frame is deleted too when there are other frames left. If there
3937 are no other frames left, some other buffer is displayed in that
3940 This function removes the buffer denoted by BUFFER-OR-NAME from
3941 all window-local buffer lists."
3942 (interactive "bBuffer to replace: ")
3943 (let ((buffer (window-normalize-buffer buffer-or-name
)))
3944 (dolist (window (window-list-1 nil nil t
))
3945 (if (eq (window-buffer window
) buffer
)
3946 (unless (window--delete window t t
)
3947 ;; Switch to another buffer in window.
3948 (set-window-dedicated-p window nil
)
3949 (switch-to-prev-buffer window
'kill
))
3950 ;; Unrecord BUFFER in WINDOW.
3951 (unrecord-window-buffer window buffer
)))))
3953 (defun quit-restore-window (&optional window bury-or-kill
)
3954 "Quit WINDOW and deal with its buffer.
3955 WINDOW must be a live window and defaults to the selected one.
3957 According to information stored in WINDOW's `quit-restore' window
3958 parameter either (1) delete WINDOW and its frame, (2) delete
3959 WINDOW, (3) restore the buffer previously displayed in WINDOW,
3960 or (4) make WINDOW display some other buffer than the present
3961 one. If non-nil, reset `quit-restore' parameter to nil.
3963 Optional second argument BURY-OR-KILL tells how to proceed with
3964 the buffer of WINDOW. The following values are handled:
3966 `nil' means to not handle the buffer in a particular way. This
3967 means that if WINDOW is not deleted by this function, invoking
3968 `switch-to-prev-buffer' will usually show the buffer again.
3970 `append' means that if WINDOW is not deleted, move its buffer to
3971 the end of WINDOW's previous buffers so it's less likely that a
3972 future invocation of `switch-to-prev-buffer' will switch to it.
3973 Also, move the buffer to the end of the frame's buffer list.
3975 `bury' means that if WINDOW is not deleted, remove its buffer
3976 from WINDOW'S list of previous buffers. Also, move the buffer
3977 to the end of the frame's buffer list. This value provides the
3978 most reliable remedy to not have `switch-to-prev-buffer' switch
3979 to this buffer again without killing the buffer.
3981 `kill' means to kill WINDOW's buffer."
3982 (setq window
(window-normalize-window window t
))
3983 (let* ((buffer (window-buffer window
))
3984 (quit-restore (window-parameter window
'quit-restore
))
3986 (let* ((prev-buffers (window-prev-buffers window
))
3987 (prev-buffer (caar prev-buffers
)))
3988 (and (or (not (eq prev-buffer buffer
))
3989 (and (cdr prev-buffers
)
3990 (not (eq (setq prev-buffer
(cadr prev-buffers
))
3995 ((and (not prev-buffer
)
3996 (or (eq (nth 1 quit-restore
) 'frame
)
3997 (and (eq (nth 1 quit-restore
) 'window
)
3998 ;; If the window has been created on an existing
3999 ;; frame and ended up as the sole window on that
4000 ;; frame, do not delete it (Bug#12764).
4001 (not (eq window
(frame-root-window window
)))))
4002 (eq (nth 3 quit-restore
) buffer
)
4003 ;; Delete WINDOW if possible.
4004 (window--delete window nil
(eq bury-or-kill
'kill
)))
4005 ;; If the previously selected window is still alive, select it.
4006 (when (window-live-p (nth 2 quit-restore
))
4007 (select-window (nth 2 quit-restore
))))
4008 ((and (listp (setq quad
(nth 1 quit-restore
)))
4009 (buffer-live-p (car quad
))
4010 (eq (nth 3 quit-restore
) buffer
))
4011 ;; Show another buffer stored in quit-restore parameter.
4012 (when (and (integerp (nth 3 quad
))
4013 (/= (nth 3 quad
) (window-total-height window
)))
4014 ;; Try to resize WINDOW to its old height but don't signal an
4017 (window-resize window
(- (nth 3 quad
) (window-total-height window
)))
4019 (set-window-dedicated-p window nil
)
4020 ;; Restore WINDOW's previous buffer, start and point position.
4021 (set-window-buffer-start-and-point
4022 window
(nth 0 quad
) (nth 1 quad
) (nth 2 quad
))
4023 ;; Deal with the buffer we just removed from WINDOW.
4024 (setq entry
(and (eq bury-or-kill
'append
)
4025 (assq buffer
(window-prev-buffers window
))))
4027 ;; Remove buffer from WINDOW's previous and next buffers.
4028 (set-window-prev-buffers
4029 window
(assq-delete-all buffer
(window-prev-buffers window
)))
4030 (set-window-next-buffers
4031 window
(delq buffer
(window-next-buffers window
))))
4033 ;; Append old buffer's entry to list of WINDOW's previous
4034 ;; buffers so it's less likely to get switched to soon but
4035 ;; `display-buffer-in-previous-window' can nevertheless find it.
4036 (set-window-prev-buffers
4037 window
(append (window-prev-buffers window
) (list entry
))))
4038 ;; Reset the quit-restore parameter.
4039 (set-window-parameter window
'quit-restore nil
)
4040 ;; Select old window.
4041 (when (window-live-p (nth 2 quit-restore
))
4042 (select-window (nth 2 quit-restore
))))
4044 ;; Show some other buffer in WINDOW and reset the quit-restore
4046 (set-window-parameter window
'quit-restore nil
)
4047 ;; Make sure that WINDOW is no more dedicated.
4048 (set-window-dedicated-p window nil
)
4049 (switch-to-prev-buffer window bury-or-kill
)))
4051 ;; Deal with the buffer.
4053 ((not (buffer-live-p buffer
)))
4054 ((eq bury-or-kill
'kill
)
4055 (kill-buffer buffer
))
4057 (bury-buffer-internal buffer
)))))
4059 (defun quit-window (&optional kill window
)
4060 "Quit WINDOW and bury its buffer.
4061 WINDOW must be a live window and defaults to the selected one.
4062 With prefix argument KILL non-nil, kill the buffer instead of
4065 According to information stored in WINDOW's `quit-restore' window
4066 parameter either (1) delete WINDOW and its frame, (2) delete
4067 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4068 or (4) make WINDOW display some other buffer than the present
4069 one. If non-nil, reset `quit-restore' parameter to nil."
4071 (quit-restore-window window
(if kill
'kill
'bury
)))
4073 (defun quit-windows-on (&optional buffer-or-name kill frame
)
4074 "Quit all windows showing BUFFER-OR-NAME.
4075 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4076 and defaults to the current buffer. Optional argument KILL
4077 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4078 BUFFER-OR-NAME. Optional argument FRAME is handled as by
4079 `delete-windows-on'.
4081 This function calls `quit-window' on all candidate windows
4082 showing BUFFER-OR-NAME."
4083 (interactive "BQuit windows on (buffer):\nP")
4084 (let ((buffer (window-normalize-buffer buffer-or-name
))
4085 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4086 ;; `window-list-1' based function.
4087 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
4088 (dolist (window (window-list-1 nil nil all-frames
))
4089 (if (eq (window-buffer window
) buffer
)
4090 (quit-window kill window
)
4091 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4092 (unrecord-window-buffer window buffer
)))))
4094 ;;; Splitting windows.
4095 (defun window-split-min-size (&optional horizontal pixelwise
)
4096 "Return minimum height of any window when splitting windows.
4097 Optional argument HORIZONTAL non-nil means return minimum width."
4101 (window-min-pixel-width)
4102 (window-min-pixel-height)))
4104 (max window-min-width window-safe-min-width
))
4106 (max window-min-height window-safe-min-height
))))
4108 (defun split-window (&optional window size side pixelwise
)
4109 "Make a new window adjacent to WINDOW.
4110 WINDOW must be a valid window and defaults to the selected one.
4111 Return the new window which is always a live window.
4113 Optional argument SIZE a positive number means make WINDOW SIZE
4114 lines or columns tall. If SIZE is negative, make the new window
4115 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
4116 absolute value can be less than `window-min-height' or
4117 `window-min-width'; so this command can make a new window as
4118 small as one line or two columns. SIZE defaults to half of
4121 Optional third argument SIDE nil (or `below') specifies that the
4122 new window shall be located below WINDOW. SIDE `above' means the
4123 new window shall be located above WINDOW. In both cases SIZE
4124 specifies the new number of lines for WINDOW (or the new window
4125 if SIZE is negative) including space reserved for the mode and/or
4128 SIDE t (or `right') specifies that the new window shall be
4129 located on the right side of WINDOW. SIDE `left' means the new
4130 window shall be located on the left of WINDOW. In both cases
4131 SIZE specifies the new number of columns for WINDOW (or the new
4132 window provided SIZE is negative) including space reserved for
4133 fringes and the scrollbar or a divider column. Any other non-nil
4134 value for SIDE is currently handled like t (or `right').
4136 PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
4138 If the variable `ignore-window-parameters' is non-nil or the
4139 `split-window' parameter of WINDOW equals t, do not process any
4140 parameters of WINDOW. Otherwise, if the `split-window' parameter
4141 of WINDOW specifies a function, call that function with all three
4142 arguments and return the value returned by that function.
4144 Otherwise, if WINDOW is part of an atomic window, \"split\" the
4145 root of that atomic window. The new window does not become a
4146 member of that atomic window.
4148 If WINDOW is live, properties of the new window like margins and
4149 scrollbars are inherited from WINDOW. If WINDOW is an internal
4150 window, these properties as well as the buffer displayed in the
4151 new window are inherited from the window selected on WINDOW's
4152 frame. The selected window is not changed by this function."
4153 (setq window
(window-normalize-window window
))
4156 ((memq side
'(below above right left
)) side
)
4158 (horizontal (not (memq side
'(below above
))))
4159 (frame (window-frame window
))
4160 (parent (window-parent window
))
4161 (function (window-parameter window
'split-window
))
4162 (window-side (window-parameter window
'window-side
))
4163 ;; Rebind the following two variables since in some cases we
4164 ;; have to override their value.
4165 (window-combination-limit window-combination-limit
)
4166 (window-combination-resize window-combination-resize
)
4167 (char-size (frame-char-size window horizontal
))
4169 (when (numberp size
)
4170 (window--size-to-pixel window size horizontal pixelwise t
)))
4172 (window--check frame
)
4175 ;; Ignore window parameters if either `ignore-window-parameters'
4176 ;; is t or the `split-window' parameter equals t.
4177 ((or ignore-window-parameters
(eq function t
)))
4178 ((functionp function
)
4179 ;; The `split-window' parameter specifies the function to call.
4180 ;; If that function is `ignore', do nothing.
4181 (throw 'done
(funcall function window size side
)))
4182 ;; If WINDOW is part of an atomic window, split the root window
4183 ;; of that atomic window instead.
4184 ((and (window-parameter window
'window-atom
)
4185 (setq atom-root
(window-atom-root window
))
4186 (not (eq atom-root window
)))
4187 (throw 'done
(split-window atom-root size side pixelwise
)))
4188 ;; If WINDOW is a side window or its first or last child is a
4189 ;; side window, throw an error unless `window-combination-resize'
4191 ((and (not (eq window-combination-resize
'side
))
4192 (or (window-parameter window
'window-side
)
4193 (and (window-child window
)
4194 (or (window-parameter
4195 (window-child window
) 'window-side
)
4197 (window-last-child window
) 'window-side
)))))
4198 (error "Cannot split side window or parent of side window"))
4199 ;; If `window-combination-resize' is 'side and window has a side
4200 ;; window sibling, bind `window-combination-limit' to t.
4201 ((and (not (eq window-combination-resize
'side
))
4202 (or (and (window-prev-sibling window
)
4204 (window-prev-sibling window
) 'window-side
))
4205 (and (window-next-sibling window
)
4207 (window-next-sibling window
) 'window-side
))))
4208 (setq window-combination-limit t
)))
4210 ;; If `window-combination-resize' is t and SIZE is non-negative,
4211 ;; bind `window-combination-limit' to t.
4212 (when (and (eq window-combination-resize t
)
4213 pixel-size
(> pixel-size
0))
4214 (setq window-combination-limit t
))
4216 (let* ((parent-pixel-size
4217 ;; `parent-pixel-size' is the pixel size of WINDOW's
4218 ;; parent, provided it has one.
4219 (when parent
(window-size parent horizontal t
)))
4220 ;; `resize' non-nil means we are supposed to resize other
4221 ;; windows in WINDOW's combination.
4223 (and window-combination-resize
4224 (or (window-parameter window
'window-side
)
4225 (not (eq window-combination-resize
'side
)))
4226 (not (eq window-combination-limit t
))
4227 ;; Resize makes sense in iso-combinations only.
4228 (window-combined-p window horizontal
)))
4229 ;; `old-pixel-size' is the current pixel size of WINDOW.
4230 (old-pixel-size (window-size window horizontal t
))
4231 ;; `new-size' is the specified or calculated size of the
4233 new-pixel-size new new-parent new-normal
)
4236 (setq new-pixel-size
4238 ;; When resizing try to give the new window the
4239 ;; average size of a window in its combination.
4240 (min (- parent-pixel-size
4241 (window-min-size parent horizontal nil t
))
4242 (/ parent-pixel-size
4243 (1+ (window-combinations parent horizontal
))))
4244 ;; Else try to give the new window half the size
4245 ;; of WINDOW (plus an eventual odd pixel).
4246 (/ old-pixel-size
2)))
4247 (unless window-resize-pixelwise
4248 ;; Round to nearest char-size multiple.
4249 (setq new-pixel-size
4250 (* char-size
(round new-pixel-size char-size
)))))
4252 ;; SIZE non-negative specifies the new size of WINDOW.
4254 ;; Note: Specifying a non-negative SIZE is practically
4255 ;; always done as workaround for making the new window
4256 ;; appear above or on the left of the new window (the
4257 ;; ispell window is a typical example of that). In all
4258 ;; these cases the SIDE argument should be set to 'above
4259 ;; or 'left in order to support the 'resize option.
4260 ;; Here we have to nest the windows instead, see above.
4261 (setq new-pixel-size
(- old-pixel-size pixel-size
)))
4263 ;; SIZE negative specifies the size of the new window.
4264 (setq new-pixel-size
(- pixel-size
))))
4271 ;; SIZE unspecified, resizing.
4272 (when (and (not (window-sizable-p
4273 parent
(- new-pixel-size
) horizontal nil t
))
4274 ;; Try again with minimum split size.
4275 (setq new-pixel-size
4277 (window-split-min-size horizontal t
)))
4278 (not (window-sizable-p
4279 parent
(- new-pixel-size
) horizontal nil t
)))
4280 (error "Window %s too small for splitting 1" parent
)))
4281 ((> (+ new-pixel-size
(window-min-size window horizontal nil t
))
4283 ;; SIZE unspecified, no resizing.
4284 (error "Window %s too small for splitting 2" window
))))
4285 ((and (>= pixel-size
0)
4286 (or (>= pixel-size old-pixel-size
)
4288 (window-safe-min-pixel-size window horizontal
))))
4289 ;; SIZE specified as new size of old window. If the new size
4290 ;; is larger than the old size or the size of the new window
4291 ;; would be less than the safe minimum, signal an error.
4292 (error "Window %s too small for splitting 3" window
))
4294 ;; SIZE specified, resizing.
4295 (unless (window-sizable-p
4296 parent
(- new-pixel-size
) horizontal nil t
)
4297 ;; If we cannot resize the parent give up.
4298 (error "Window %s too small for splitting 4" parent
)))
4299 ((or (< new-pixel-size
4300 (window-safe-min-pixel-size window horizontal
))
4301 (< (- old-pixel-size new-pixel-size
)
4302 (window-safe-min-pixel-size window horizontal
)))
4303 ;; SIZE specification violates minimum size restrictions.
4304 (error "Window %s too small for splitting 5" window
)))
4306 (window--resize-reset frame horizontal
)
4309 ;; Make new-parent non-nil if we need a new parent window;
4310 ;; either because we want to nest or because WINDOW is not
4312 (or (eq window-combination-limit t
)
4313 (not (window-combined-p window horizontal
))))
4315 ;; Make new-normal the normal size of the new window.
4317 (pixel-size (/ (float new-pixel-size
)
4318 (if new-parent old-pixel-size parent-pixel-size
)))
4320 (resize (/ 1.0 (1+ (window-combinations parent horizontal
))))
4321 (t (/ (window-normal-size window horizontal
) 2.0))))
4324 ;; Try to get space from OLD's siblings. We could go "up" and
4325 ;; try getting additional space from surrounding windows but
4326 ;; we won't be able to return space to those windows when we
4327 ;; delete the one we create here. Hence we do not go up.
4329 (window--resize-child-windows
4330 parent
(- new-pixel-size
) horizontal
)
4331 (let* ((normal (- 1.0 new-normal
))
4332 (sub (window-child parent
)))
4334 (set-window-new-normal
4335 sub
(* (window-normal-size sub horizontal
) normal
))
4336 (setq sub
(window-right sub
)))))
4337 ;; Get entire space from WINDOW.
4338 (set-window-new-pixel
4339 window
(- old-pixel-size new-pixel-size
))
4340 ;; (set-window-new-pixel window (- old-pixel-size new-pixel-size))
4341 ;; (set-window-new-total
4342 ;; window (- old-size new-size))
4343 (window--resize-this-window window
(- new-pixel-size
) horizontal
)
4344 (set-window-new-normal
4345 window
(- (if new-parent
1.0 (window-normal-size window horizontal
))
4348 (let* ((new (split-window-internal window new-pixel-size side new-normal
)))
4349 (window--pixel-to-total frame horizontal
)
4350 ;; Assign window-side parameters, if any.
4352 ((eq window-combination-resize
'side
)
4355 (window-side window-side
)
4356 ((eq side
'above
) 'top
)
4357 ((eq side
'below
) 'bottom
)
4359 ;; We made a new side window.
4360 (set-window-parameter new
'window-side window-side
)
4361 (when (and new-parent
(window-parameter window
'window-side
))
4362 ;; We've been splitting a side root window. Give the
4363 ;; new parent the same window-side parameter.
4364 (set-window-parameter
4365 (window-parent new
) 'window-side window-side
))))
4366 ((eq window-combination-resize
'atom
)
4367 ;; Make sure `window--check-frame' won't destroy an existing
4368 ;; atomic window in case the new window gets nested inside.
4369 (unless (window-parameter window
'window-atom
)
4370 (set-window-parameter window
'window-atom t
))
4372 (set-window-parameter (window-parent new
) 'window-atom t
))
4373 (set-window-parameter new
'window-atom t
)))
4375 (run-window-configuration-change-hook frame
)
4376 (run-window-scroll-functions new
)
4377 (window--check frame
)
4378 ;; Always return the new window.
4381 ;; I think this should be the default; I think people will prefer it--rms.
4382 (defcustom split-window-keep-point t
4383 "If non-nil, \\[split-window-below] preserves point in the new window.
4384 If nil, adjust point in the two windows to minimize redisplay.
4385 This option applies only to `split-window-below' and functions
4386 that call it. The low-level `split-window' function always keeps
4387 the original point in both windows."
4391 (defun split-window-below (&optional size
)
4392 "Split the selected window into two windows, one above the other.
4393 The selected window is above. The newly split-off window is
4394 below, and displays the same buffer. Return the new window.
4396 If optional argument SIZE is omitted or nil, both windows get the
4397 same height, or close to it. If SIZE is positive, the upper
4398 \(selected) window gets SIZE lines. If SIZE is negative, the
4399 lower (new) window gets -SIZE lines.
4401 If the variable `split-window-keep-point' is non-nil, both
4402 windows get the same value of point as the selected window.
4403 Otherwise, the window starts are chosen so as to minimize the
4404 amount of redisplay; this is convenient on slow terminals."
4406 (let ((old-window (selected-window))
4407 (old-point (window-point))
4408 (size (and size
(prefix-numeric-value size
)))
4409 moved-by-window-height moved new-window bottom
)
4410 (when (and size
(< size
0) (< (- size
) window-min-height
))
4411 ;; `split-window' would not signal an error here.
4412 (error "Size of new window too small"))
4413 (setq new-window
(split-window nil size
))
4414 (unless split-window-keep-point
4415 (with-current-buffer (window-buffer)
4416 ;; Use `save-excursion' around vertical movements below
4417 ;; (Bug#10971). Note: When the selected window's buffer has a
4418 ;; header line, up to two lines of the buffer may not show up
4419 ;; in the resulting configuration.
4421 (goto-char (window-start))
4422 (setq moved
(vertical-motion (window-height)))
4423 (set-window-start new-window
(point))
4424 (when (> (point) (window-point new-window
))
4425 (set-window-point new-window
(point)))
4426 (when (= moved
(window-height))
4427 (setq moved-by-window-height t
)
4428 (vertical-motion -
1))
4429 (setq bottom
(point)))
4430 (and moved-by-window-height
4432 (set-window-point old-window
(1- bottom
)))
4433 (and moved-by-window-height
4434 (<= (window-start new-window
) old-point
)
4435 (set-window-point new-window old-point
)
4436 (select-window new-window
))))
4437 ;; Always copy quit-restore parameter in interactive use.
4438 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
4440 (set-window-parameter new-window
'quit-restore quit-restore
)))
4443 (defalias 'split-window-vertically
'split-window-below
)
4445 (defun split-window-right (&optional size
)
4446 "Split the selected window into two side-by-side windows.
4447 The selected window is on the left. The newly split-off window
4448 is on the right, and displays the same buffer. Return the new
4451 If optional argument SIZE is omitted or nil, both windows get the
4452 same width, or close to it. If SIZE is positive, the left-hand
4453 \(selected) window gets SIZE columns. If SIZE is negative, the
4454 right-hand (new) window gets -SIZE columns. Here, SIZE includes
4455 the width of the window's scroll bar; if there are no scroll
4456 bars, it includes the width of the divider column to the window's
4459 (let ((old-window (selected-window))
4460 (size (and size
(prefix-numeric-value size
)))
4462 (when (and size
(< size
0) (< (- size
) window-min-width
))
4463 ;; `split-window' would not signal an error here.
4464 (error "Size of new window too small"))
4465 (setq new-window
(split-window nil size t
))
4466 ;; Always copy quit-restore parameter in interactive use.
4467 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
4469 (set-window-parameter new-window
'quit-restore quit-restore
)))
4472 (defalias 'split-window-horizontally
'split-window-right
)
4474 ;;; Balancing windows.
4476 ;; The following routine uses the recycled code from an old version of
4477 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
4478 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
4479 ;; more readable (FWIW we'd need three loops - one to calculate the
4480 ;; minimum sizes per window, one to enlarge or shrink windows until the
4481 ;; new parent-size matches, and one where we shrink the largest/enlarge
4482 ;; the smallest window).
4483 (defun balance-windows-2 (window horizontal
)
4484 "Subroutine of `balance-windows-1'.
4485 WINDOW must be a vertical combination (horizontal if HORIZONTAL
4487 (let* ((char-size (if window-resize-pixelwise
4489 (frame-char-size window horizontal
)))
4490 (first (window-child window
))
4492 (number-of-children 0)
4493 (parent-size (window-new-pixel window
))
4494 (total-sum parent-size
)
4495 failed size sub-total sub-delta sub-amount rest
)
4497 (setq number-of-children
(1+ number-of-children
))
4498 (when (window-size-fixed-p sub horizontal
)
4500 (- total-sum
(window-size sub horizontal t
)))
4501 (set-window-new-normal sub
'ignore
))
4502 (setq sub
(window-right sub
)))
4505 (while (and failed
(> number-of-children
0))
4506 (setq size
(/ total-sum number-of-children
))
4509 (while (and sub
(not failed
))
4510 ;; Ignore child windows that should be ignored or are stuck.
4511 (unless (window--resize-child-windows-skip-p sub
)
4512 (setq sub-total
(window-size sub horizontal t
))
4513 (setq sub-delta
(- size sub-total
))
4515 (window-sizable sub sub-delta horizontal nil t
))
4516 ;; Register the new total size for this child window.
4517 (set-window-new-pixel sub
(+ sub-total sub-amount
))
4518 (unless (= sub-amount sub-delta
)
4519 (setq total-sum
(- total-sum sub-total sub-amount
))
4520 (setq number-of-children
(1- number-of-children
))
4521 ;; We failed and need a new round.
4523 (set-window-new-normal sub
'skip
)))
4524 (setq sub
(window-right sub
))))
4526 ;; How can we be sure that `number-of-children' is NOT zero here ?
4527 (setq rest
(% total-sum number-of-children
))
4528 ;; Fix rounding by trying to enlarge non-stuck windows by one line
4529 ;; (column) until `rest' is zero.
4531 (while (and sub
(> rest
0))
4532 (unless (window--resize-child-windows-skip-p window
)
4533 (set-window-new-pixel sub char-size t
)
4534 (setq rest
(- rest char-size
)))
4535 (setq sub
(window-right sub
)))
4537 ;; Fix rounding by trying to enlarge stuck windows by one line
4538 ;; (column) until `rest' equals zero.
4540 (while (and sub
(> rest
0))
4541 (unless (eq (window-new-normal sub
) 'ignore
)
4542 (set-window-new-pixel sub char-size t
)
4543 (setq rest
(- rest char-size
)))
4544 (setq sub
(window-right sub
)))
4548 ;; Record new normal sizes.
4549 (set-window-new-normal
4550 sub
(/ (if (eq (window-new-normal sub
) 'ignore
)
4551 (window-size sub horizontal t
)
4552 (window-new-pixel sub
))
4553 (float parent-size
)))
4554 ;; Recursively balance each window's child windows.
4555 (balance-windows-1 sub horizontal
)
4556 (setq sub
(window-right sub
)))))
4558 (defun balance-windows-1 (window &optional horizontal
)
4559 "Subroutine of `balance-windows'."
4560 (if (window-child window
)
4561 (let ((sub (window-child window
)))
4562 (if (window-combined-p sub horizontal
)
4563 (balance-windows-2 window horizontal
)
4564 (let ((size (window-new-pixel window
)))
4566 (set-window-new-pixel sub size
)
4567 (balance-windows-1 sub horizontal
)
4568 (setq sub
(window-right sub
))))))))
4570 (defun balance-windows (&optional window-or-frame
)
4571 "Balance the sizes of windows of WINDOW-OR-FRAME.
4572 WINDOW-OR-FRAME is optional and defaults to the selected frame.
4573 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
4574 windows of that frame. If WINDOW-OR-FRAME denotes a window,
4575 recursively balance the sizes of all child windows of that
4580 ((or (not window-or-frame
)
4581 (frame-live-p window-or-frame
))
4582 (frame-root-window window-or-frame
))
4583 ((or (window-live-p window-or-frame
)
4584 (window-child window-or-frame
))
4587 (error "Not a window or frame %s" window-or-frame
))))
4588 (frame (window-frame window
)))
4589 ;; Balance vertically.
4590 (window--resize-reset (window-frame window
))
4591 (balance-windows-1 window
)
4592 (when (window--resize-apply-p frame
)
4593 (window-resize-apply frame
)
4594 (window--pixel-to-total frame
)
4595 (run-window-configuration-change-hook frame
))
4596 ;; Balance horizontally.
4597 (window--resize-reset (window-frame window
) t
)
4598 (balance-windows-1 window t
)
4599 (when (window--resize-apply-p frame t
)
4600 (window-resize-apply frame
)
4601 (window--pixel-to-total frame t
)
4602 (run-window-configuration-change-hook frame
))))
4604 (defun window-fixed-size-p (&optional window direction
)
4605 "Return t if WINDOW cannot be resized in DIRECTION.
4606 WINDOW defaults to the selected window. DIRECTION can be
4607 nil (i.e. any), `height' or `width'."
4608 (with-current-buffer (window-buffer window
)
4609 (when (and (boundp 'window-size-fixed
) window-size-fixed
)
4611 (member (cons direction window-size-fixed
)
4612 '((height . width
) (width . height
))))))))
4614 ;;; A different solution to balance-windows.
4615 (defvar window-area-factor
1
4616 "Factor by which the window area should be over-estimated.
4617 This is used by `balance-windows-area'.
4618 Changing this globally has no effect.")
4619 (make-variable-buffer-local 'window-area-factor
)
4621 (defun balance-windows-area-adjust (window delta horizontal pixelwise
)
4622 "Wrapper around `window-resize' with error checking.
4623 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
4624 ;; `window-resize' may fail if delta is too large.
4625 (while (>= (abs delta
) 1)
4628 ;; It was wrong to use `window-resize' here. Somehow
4629 ;; `balance-windows-area' depends on resizing windows
4631 (adjust-window-trailing-edge window delta horizontal pixelwise
)
4634 ;;(message "adjust: %s" (error-message-string err))
4635 (setq delta
(/ delta
2))))))
4637 (defun balance-windows-area ()
4638 "Make all visible windows the same area (approximately).
4639 See also `window-area-factor' to change the relative size of
4642 (let* ((unchanged 0) (carry 0) (round 0)
4643 ;; Remove fixed-size windows.
4644 (wins (delq nil
(mapcar (lambda (win)
4645 (if (not (window-fixed-size-p win
)) win
))
4646 (window-list nil
'nomini
))))
4648 (pixelwise window-resize-pixelwise
)
4650 ;; Resizing a window changes the size of surrounding windows in complex
4651 ;; ways, so it's difficult to balance them all. The introduction of
4652 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
4653 ;; very difficult to do. `balance-window' above takes an off-line
4654 ;; approach: get the whole window tree, then balance it, then try to
4655 ;; adjust the windows so they fit the result.
4656 ;; Here, instead, we take a "local optimization" approach, where we just
4657 ;; go through all the windows several times until nothing needs to be
4658 ;; changed. The main problem with this approach is that it's difficult
4659 ;; to make sure it terminates, so we use some heuristic to try and break
4660 ;; off infinite loops.
4661 ;; After a round without any change, we allow a second, to give a chance
4662 ;; to the carry to propagate a minor imbalance from the end back to
4664 (while (< unchanged
2)
4665 ;; (message "New round")
4666 (setq unchanged
(1+ unchanged
) round
(1+ round
))
4669 (while (progn (setq next
(next-window next
))
4670 (window-fixed-size-p next
)))
4671 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
4673 (< (car (window-pixel-edges win
)) (car (window-pixel-edges next
))))
4674 (areadiff (/ (- (* (window-height next pixelwise
)
4675 (window-width next pixelwise
)
4676 (buffer-local-value 'window-area-factor
4677 (window-buffer next
)))
4678 (* (window-height win pixelwise
)
4679 (window-width win pixelwise
)
4680 (buffer-local-value 'window-area-factor
4681 (window-buffer win
))))
4682 (max (buffer-local-value 'window-area-factor
4683 (window-buffer win
))
4684 (buffer-local-value 'window-area-factor
4685 (window-buffer next
)))))
4687 (+ (window-height win pixelwise
)
4688 (window-height next pixelwise
))
4689 (+ (window-width win pixelwise
)
4690 (window-width next pixelwise
))))
4691 (diff (/ areadiff edgesize
)))
4693 ;; Maybe diff is actually closer to 1 than to 0.
4694 (setq diff
(/ (* 3 areadiff
) (* 2 edgesize
))))
4695 (when (and (zerop diff
) (not (zerop areadiff
)))
4696 (setq diff
(/ (+ areadiff carry
) edgesize
))
4697 ;; Change things smoothly.
4698 (if (or (> diff
1) (< diff -
1)) (setq diff
(/ diff
2))))
4700 ;; Make sure negligible differences don't accumulate to
4701 ;; become significant.
4702 (setq carry
(+ carry areadiff
))
4703 ;; This used `adjust-window-trailing-edge' before and uses
4704 ;; `window-resize' now. Error wrapping is still needed.
4705 (balance-windows-area-adjust win diff horiz pixelwise
)
4707 (let ((change (cons win
(window-pixel-edges win
))))
4708 ;; If the same change has been seen already for this window,
4709 ;; we're most likely in an endless loop, so don't count it as
4711 (unless (member change changelog
)
4712 (push change changelog
)
4713 (setq unchanged
0 carry
0)))))))
4714 ;; We've now basically balanced all the windows.
4715 ;; But there may be some minor off-by-one imbalance left over,
4716 ;; so let's do some fine tuning.
4717 ;; (bw-finetune wins)
4718 ;; (message "Done in %d rounds" round)
4721 ;;; Window states, how to get them and how to put them in a window.
4722 (defun window--state-get-1 (window &optional writable
)
4723 "Helper function for `window-state-get'."
4726 ((window-top-child window
) 'vc
)
4727 ((window-left-child window
) 'hc
)
4729 (buffer (window-buffer window
))
4730 (selected (eq window
(selected-window)))
4733 ,@(unless (window-next-sibling window
) `((last . t
)))
4734 (pixel-width .
,(window-pixel-width window
))
4735 (pixel-height .
,(window-pixel-height window
))
4736 (total-width .
,(window-total-width window
))
4737 (total-height .
,(window-total-height window
))
4738 (normal-height .
,(window-normal-size window
))
4739 (normal-width .
,(window-normal-size window t
))
4740 ,@(unless (window-live-p window
)
4741 `((combination-limit .
,(window-combination-limit window
))))
4742 ,@(let ((parameters (window-parameters window
))
4744 ;; Make copies of those window parameters whose
4745 ;; persistence property is `writable' if WRITABLE is
4746 ;; non-nil and non-nil if WRITABLE is nil.
4747 (dolist (par parameters
)
4748 (let ((pers (cdr (assq (car par
)
4749 window-persistent-parameters
))))
4750 (when (and pers
(or (not writable
) (eq pers
'writable
)))
4751 (setq list
(cons (cons (car par
) (cdr par
)) list
)))))
4752 ;; Add `clone-of' parameter if necessary.
4753 (let ((pers (cdr (assq 'clone-of
4754 window-persistent-parameters
))))
4755 (when (and pers
(or (not writable
) (eq pers
'writable
))
4756 (not (assq 'clone-of list
)))
4757 (setq list
(cons (cons 'clone-of window
) list
))))
4759 `((parameters .
,list
))))
4761 ;; All buffer related things go in here.
4762 (let ((point (window-point window
))
4763 (start (window-start window
)))
4765 ,(buffer-name buffer
)
4766 (selected .
,selected
)
4767 (hscroll .
,(window-hscroll window
))
4768 (fringes .
,(window-fringes window
))
4769 (margins .
,(window-margins window
))
4770 (scroll-bars .
,(window-scroll-bars window
))
4771 (vscroll .
,(window-vscroll window
))
4772 (dedicated .
,(window-dedicated-p window
))
4773 (point .
,(if writable point
4776 'window-point-insertion-type
4778 (start .
,(if writable start
(copy-marker start
)))))))))
4780 (when (memq type
'(vc hc
))
4782 (setq window
(window-child window
))
4784 (setq list
(cons (window--state-get-1 window writable
) list
))
4785 (setq window
(window-right window
)))
4787 (append head tail
)))
4789 (defun window-state-get (&optional window writable
)
4790 "Return state of WINDOW as a Lisp object.
4791 WINDOW can be any window and defaults to the root window of the
4794 Optional argument WRITABLE non-nil means do not use markers for
4795 sampling `window-point' and `window-start'. Together, WRITABLE
4796 and the variable `window-persistent-parameters' specify which
4797 window parameters are saved by this function. WRITABLE should be
4798 non-nil when the return value shall be written to a file and read
4799 back in another session. Otherwise, an application may run into
4800 an `invalid-read-syntax' error while attempting to read back the
4803 The return value can be used as argument for `window-state-put'
4804 to put the state recorded here into an arbitrary window. The
4805 value can be also stored on disk and read back in a new session."
4808 (if (window-valid-p window
)
4810 (error "%s is not a live or internal window" window
))
4811 (frame-root-window)))
4812 ;; The return value is a cons whose car specifies some constraints on
4813 ;; the size of WINDOW. The cdr lists the states of the child windows
4816 ;; Frame related things would go into a function, say `frame-state',
4817 ;; calling `window-state-get' to insert the frame's root window.
4818 `((min-height .
,(window-min-size window
))
4819 (min-width .
,(window-min-size window t
))
4820 (min-height-ignore .
,(window-min-size window nil t
))
4821 (min-width-ignore .
,(window-min-size window t t
))
4822 (min-height-safe .
,(window-min-size window nil
'safe
))
4823 (min-width-safe .
,(window-min-size window t
'safe
))
4824 (min-pixel-height .
,(window-min-size window nil nil t
))
4825 (min-pixel-width .
,(window-min-size window t nil t
))
4826 (min-pixel-height-ignore .
,(window-min-size window nil t t
))
4827 (min-pixel-width-ignore .
,(window-min-size window t t t
))
4828 (min-pixel-height-safe .
,(window-min-size window nil
'safe t
))
4829 (min-pixel-width-safe .
,(window-min-size window t
'safe t
)))
4830 (window--state-get-1 window writable
)))
4832 (defvar window-state-put-list nil
4833 "Helper variable for `window-state-put'.")
4835 (defvar window-state-put-stale-windows nil
4836 "Helper variable for `window-state-put'.")
4838 (defun window--state-put-1 (state &optional window ignore totals pixelwise
)
4839 "Helper function for `window-state-put'."
4840 (let ((type (car state
)))
4841 (setq state
(cdr state
))
4844 ;; For a leaf window just add unprocessed entries to
4845 ;; `window-state-put-list'.
4846 (push (cons window state
) window-state-put-list
))
4847 ((memq type
'(vc hc
))
4848 (let* ((horizontal (eq type
'hc
))
4849 (total (window-size window horizontal pixelwise
))
4852 (dolist (item state
)
4853 ;; Find the next child window. WINDOW always points to the
4854 ;; real window that we want to fill with what we find here.
4855 (when (memq (car item
) '(leaf vc hc
))
4856 (if (assq 'last item
)
4857 ;; The last child window. Below `window--state-put-1'
4858 ;; will put into it whatever ITEM has in store.
4860 ;; Not the last child window, prepare for splitting
4861 ;; WINDOW. SIZE is the new (and final) size of the old
4867 (cdr (assq (if horizontal
4871 (cdr (assq (if horizontal
4875 ;; Use normalized size and round.
4878 (cdr (assq (if horizontal
'normal-width
'normal-height
)
4881 ;; Use safe sizes, we try to resize later.
4882 (setq size
(max size
4884 (* window-safe-min-width
4886 (frame-char-width (window-frame window
))
4888 (* window-safe-min-height
4890 (frame-char-height (window-frame window
))
4892 (if (window-sizable-p window
(- size
) horizontal
'safe pixelwise
)
4893 (let* ((window-combination-limit
4894 (assq 'combination-limit item
)))
4895 ;; We must inherit the combination limit, otherwise
4896 ;; we might mess up handling of atomic and side
4898 (setq new
(split-window window size horizontal pixelwise
)))
4899 ;; Give up if we can't resize window down to safe sizes.
4900 (error "Cannot resize window %s" window
))
4904 ;; When creating the first child window add for parent
4905 ;; unprocessed entries to `window-state-put-list'.
4906 (setq window-state-put-list
4907 (cons (cons (window-parent window
) state
)
4908 window-state-put-list
))))
4910 ;; Now process the current window (either the one we've just
4911 ;; split or the last child of its parent).
4912 (window--state-put-1 item window ignore totals
)
4913 ;; Continue with the last window split off.
4914 (setq window new
))))))))
4916 (defun window--state-put-2 (ignore pixelwise
)
4917 "Helper function for `window-state-put'."
4918 (dolist (item window-state-put-list
)
4919 (let ((window (car item
))
4920 (combination-limit (cdr (assq 'combination-limit item
)))
4921 (parameters (cdr (assq 'parameters item
)))
4922 (state (cdr (assq 'buffer item
))))
4923 (when combination-limit
4924 (set-window-combination-limit window combination-limit
))
4925 ;; Reset window's parameters and assign saved ones (we might want
4926 ;; a `remove-window-parameters' function here).
4927 (dolist (parameter (window-parameters window
))
4928 (set-window-parameter window
(car parameter
) nil
))
4930 (dolist (parameter parameters
)
4931 (set-window-parameter window
(car parameter
) (cdr parameter
))))
4932 ;; Process buffer related state.
4934 (let ((buffer (get-buffer (car state
))))
4936 (with-current-buffer buffer
4937 (set-window-buffer window buffer
)
4938 (set-window-hscroll window
(cdr (assq 'hscroll state
)))
4939 (apply 'set-window-fringes
4940 (cons window
(cdr (assq 'fringes state
))))
4941 (let ((margins (cdr (assq 'margins state
))))
4942 (set-window-margins window
(car margins
) (cdr margins
)))
4943 (let ((scroll-bars (cdr (assq 'scroll-bars state
))))
4944 (set-window-scroll-bars
4945 window
(car scroll-bars
) (nth 2 scroll-bars
)
4946 (nth 3 scroll-bars
)))
4947 (set-window-vscroll window
(cdr (assq 'vscroll state
)))
4948 ;; Adjust vertically.
4949 (if (memq window-size-fixed
'(t height
))
4950 ;; A fixed height window, try to restore the
4954 (if pixelwise
'pixel-height
'total-height
)
4956 (window-size window nil pixelwise
)))
4958 (when (window--resizable-p
4959 window delta nil nil nil nil nil pixelwise
)
4960 (window-resize window delta nil nil pixelwise
)))
4961 ;; Else check whether the window is not high enough.
4963 (window-min-size window nil ignore pixelwise
))
4965 (- min-size
(window-size window nil pixelwise
))))
4966 (when (and (> delta
0)
4967 (window--resizable-p
4968 window delta nil ignore nil nil nil pixelwise
))
4969 (window-resize window delta nil ignore pixelwise
))))
4970 ;; Adjust horizontally.
4971 (if (memq window-size-fixed
'(t width
))
4972 ;; A fixed width window, try to restore the original
4976 (if pixelwise
'pixel-width
'total-width
)
4978 (window-size window t pixelwise
)))
4980 (when (window--resizable-p
4981 window delta nil nil nil nil nil pixelwise
)
4982 (window-resize window delta nil nil pixelwise
)))
4983 ;; Else check whether the window is not wide enough.
4984 (let* ((min-size (window-min-size window t ignore pixelwise
))
4985 (delta (- min-size
(window-size window t pixelwise
))))
4986 (when (and (> delta
0)
4987 (window--resizable-p
4988 window delta t ignore nil nil nil pixelwise
))
4989 (window-resize window delta t ignore pixelwise
))))
4990 ;; Set dedicated status.
4991 (set-window-dedicated-p window
(cdr (assq 'dedicated state
)))
4992 ;; Install positions (maybe we should do this after all
4993 ;; windows have been created and sized).
4995 (set-window-start window
(cdr (assq 'start state
)))
4996 (set-window-point window
(cdr (assq 'point state
))))
4997 ;; Select window if it's the selected one.
4998 (when (cdr (assq 'selected state
))
4999 (select-window window
)))
5000 ;; We don't want to raise an error in case the buffer does
5001 ;; not exist anymore, so we switch to a previous one and
5002 ;; save the window with the intention of deleting it later
5004 (switch-to-prev-buffer window
)
5005 (push window window-state-put-stale-windows
)))))))
5007 (defun window-state-put (state &optional window ignore
)
5008 "Put window state STATE into WINDOW.
5009 STATE should be the state of a window returned by an earlier
5010 invocation of `window-state-get'. Optional argument WINDOW must
5011 specify a live window and defaults to the selected one.
5013 Optional argument IGNORE non-nil means ignore minimum window
5014 sizes and fixed size restrictions. IGNORE equal `safe' means
5015 windows can get as small as `window-safe-min-height' and
5016 `window-safe-min-width'."
5017 (setq window-state-put-stale-windows nil
)
5018 (setq window
(window-normalize-window window t
))
5019 (let* ((frame (window-frame window
))
5021 ;; We check here (1) whether the total sizes of root window of
5022 ;; STATE and that of WINDOW are equal so we can avoid
5023 ;; calculating new sizes, and (2) if we do have to resize
5024 ;; whether we can do so without violating size restrictions.
5025 (pixelwise (and (cdr (assq 'pixel-width state
))
5026 (cdr (assq 'pixel-height state
))))
5027 (totals (or (and pixelwise
5028 (= (window-pixel-width window
)
5029 (cdr (assq 'pixel-width state
)))
5030 (= (window-pixel-height window
)
5031 (cdr (assq 'pixel-height state
))))
5032 (and (= (window-total-width window
)
5033 (cdr (assq 'total-width state
)))
5034 (= (window-total-height window
)
5035 (cdr (assq 'total-height state
))))))
5036 (min-height (cdr (assq
5037 (if pixelwise
'min-pixel-height
'min-height
)
5039 (min-width (cdr (assq
5040 (if pixelwise
'min-pixel-width
'min-weight
)
5042 (if (and (not totals
)
5043 (or (> min-height
(window-size window nil pixelwise
))
5044 (> min-width
(window-size window t pixelwise
)))
5046 (and (setq min-height
5049 'min-pixel-height-ignore
5055 'min-pixel-width-ignore
5059 (window-size window nil pixelwise
))
5061 (window-size window t pixelwise
)))
5062 (or (not (eq ignore
'safe
))
5063 (and (setq min-height
5066 'min-pixel-height-safe
5072 'min-pixel-width-safe
5076 (window-size window nil pixelwise
))
5078 (window-size window t pixelwise
))))))))
5079 ;; The check above might not catch all errors due to rounding
5080 ;; issues - so IGNORE equal 'safe might not always produce the
5081 ;; minimum possible state. But such configurations hardly make
5083 (error "Window %s too small to accommodate state" window
)
5084 (setq state
(cdr state
))
5085 (setq window-state-put-list nil
)
5086 ;; Work on the windows of a temporary buffer to make sure that
5087 ;; splitting proceeds regardless of any buffer local values of
5088 ;; `window-size-fixed'. Release that buffer after the buffers of
5089 ;; all live windows have been set by `window--state-put-2'.
5091 (set-window-buffer window
(current-buffer))
5092 (window--state-put-1 state window nil totals pixelwise
)
5093 (window--state-put-2 ignore pixelwise
))
5094 (while window-state-put-stale-windows
5095 (let ((window (pop window-state-put-stale-windows
)))
5096 (when (eq (window-deletable-p window
) t
)
5097 (delete-window window
))))
5098 (window--check frame
))))
5100 (defun display-buffer-record-window (type window buffer
)
5101 "Record information for window used by `display-buffer'.
5102 TYPE specifies the type of the calling operation and must be one
5103 of the symbols 'reuse (when WINDOW existed already and was
5104 reused for displaying BUFFER), 'window (when WINDOW was created
5105 on an already existing frame), or 'frame (when WINDOW was
5106 created on a new frame). WINDOW is the window used for or created
5107 by the `display-buffer' routines. BUFFER is the buffer that
5110 This function installs or updates the quit-restore parameter of
5111 WINDOW. The quit-restore parameter is a list of four elements:
5112 The first element is one of the symbols 'window, 'frame, 'same or
5113 'other. The second element is either one of the symbols 'window
5114 or 'frame or a list whose elements are the buffer previously
5115 shown in the window, that buffer's window start and window point,
5116 and the window's height. The third element is the window
5117 selected at the time the parameter was created. The fourth
5121 (if (eq (window-buffer window
) buffer
)
5122 ;; WINDOW shows BUFFER already.
5123 (when (consp (window-parameter window
'quit-restore
))
5124 ;; If WINDOW has a quit-restore parameter, reset its car.
5125 (setcar (window-parameter window
'quit-restore
) 'same
))
5126 ;; WINDOW shows another buffer.
5127 (with-current-buffer (window-buffer window
)
5128 (set-window-parameter
5129 window
'quit-restore
5131 ;; A quadruple of WINDOW's buffer, start, point and height.
5132 (list (current-buffer) (window-start window
)
5133 ;; Preserve window-point-insertion-type (Bug#12588).
5135 (window-point window
) window-point-insertion-type
)
5136 (window-total-height window
))
5137 (selected-window) buffer
)))))
5139 ;; WINDOW has been created on an existing frame.
5140 (set-window-parameter
5141 window
'quit-restore
5142 (list 'window
'window
(selected-window) buffer
)))
5144 ;; WINDOW has been created on a new frame.
5145 (set-window-parameter
5146 window
'quit-restore
5147 (list 'frame
'frame
(selected-window) buffer
)))))
5149 (defcustom display-buffer-function nil
5150 "If non-nil, function to call to handle `display-buffer'.
5151 It will receive two args, the buffer and a flag which if non-nil
5152 means that the currently selected window is not acceptable. It
5153 should choose or create a window, display the specified buffer in
5154 it, and return the window.
5156 The specified function should call `display-buffer-record-window'
5157 with corresponding arguments to set up the quit-restore parameter
5158 of the window used."
5161 (function :tag
"function"))
5164 (make-obsolete-variable 'display-buffer-function
5165 'display-buffer-alist
"24.3")
5167 ;; Eventually, we want to turn this into a defvar; instead of
5168 ;; customizing this, the user should use a `pop-up-frame-parameters'
5169 ;; alist entry in `display-buffer-base-action'.
5170 (defcustom pop-up-frame-alist nil
5171 "Alist of parameters for automatically generated new frames.
5172 If non-nil, the value you specify here is used by the default
5173 `pop-up-frame-function' for the creation of new frames.
5175 Since `pop-up-frame-function' is used by `display-buffer' for
5176 making new frames, any value specified here by default affects
5177 the automatic generation of new frames via `display-buffer' and
5178 all functions based on it. The behavior of `make-frame' is not
5179 affected by this variable."
5180 :type
'(repeat (cons :format
"%v"
5181 (symbol :tag
"Parameter")
5182 (sexp :tag
"Value")))
5185 (defcustom pop-up-frame-function
5186 (lambda () (make-frame pop-up-frame-alist
))
5187 "Function used by `display-buffer' for creating a new frame.
5188 This function is called with no arguments and should return a new
5189 frame. The default value calls `make-frame' with the argument
5190 `pop-up-frame-alist'."
5194 (defcustom special-display-buffer-names nil
5195 "List of names of buffers that should be displayed specially.
5196 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5197 its name is in this list, displays the buffer in a way specified
5198 by `special-display-function'. `special-display-popup-frame'
5199 \(the default for `special-display-function') usually displays
5200 the buffer in a separate frame made with the parameters specified
5201 by `special-display-frame-alist'. If `special-display-function'
5202 has been set to some other function, that function is called with
5203 the buffer as first, and nil as second argument.
5205 Alternatively, an element of this list can be specified as
5206 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
5207 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5208 `special-display-popup-frame' will interpret such pairs as frame
5209 parameters when it creates a special frame, overriding the
5210 corresponding values from `special-display-frame-alist'.
5212 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5213 `special-display-popup-frame' displays that buffer in the
5214 selected window. If FRAME-PARAMETERS contains (same-frame . t),
5215 it displays that buffer in a window on the selected frame.
5217 If `special-display-function' specifies some other function than
5218 `special-display-popup-frame', that function is called with the
5219 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5222 Finally, an element of this list can be also specified as
5223 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5224 `special-display-popup-frame' will call FUNCTION with the buffer
5225 named BUFFER-NAME as first argument, and OTHER-ARGS as the
5228 Any alternative function specified here is responsible for
5229 setting up the quit-restore parameter of the window used.
5231 If this variable appears \"not to work\", because you added a
5232 name to it but the corresponding buffer is displayed in the
5233 selected window, look at the values of `same-window-buffer-names'
5234 and `same-window-regexps'. Those variables take precedence over
5237 See also `special-display-regexps'."
5239 (choice :tag
"Buffer"
5241 (string :format
"%v")
5242 (cons :tag
"With parameters"
5245 (string :format
"%v")
5246 (repeat :tag
"Parameters"
5248 (symbol :tag
"Parameter")
5249 (sexp :tag
"Value"))))
5250 (list :tag
"With function"
5253 (string :format
"%v")
5254 (function :tag
"Function")
5255 (repeat :tag
"Arguments" (sexp)))))
5258 (make-obsolete-variable 'special-display-buffer-names
'display-buffer-alist
"24.3")
5259 (put 'special-display-buffer-names
'risky-local-variable t
)
5261 (defcustom special-display-regexps nil
5262 "List of regexps saying which buffers should be displayed specially.
5263 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5264 any regexp in this list matches its name, displays it specially
5265 using `special-display-function'. `special-display-popup-frame'
5266 \(the default for `special-display-function') usually displays
5267 the buffer in a separate frame made with the parameters specified
5268 by `special-display-frame-alist'. If `special-display-function'
5269 has been set to some other function, that function is called with
5270 the buffer as first, and nil as second argument.
5272 Alternatively, an element of this list can be specified as
5273 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
5274 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5275 `special-display-popup-frame' will then interpret these pairs as
5276 frame parameters when creating a special frame for a buffer whose
5277 name matches REGEXP, overriding the corresponding values from
5278 `special-display-frame-alist'.
5280 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5281 `special-display-popup-frame' displays buffers matching REGEXP in
5282 the selected window. (same-frame . t) in FRAME-PARAMETERS means
5283 to display such buffers in a window on the selected frame.
5285 If `special-display-function' specifies some other function than
5286 `special-display-popup-frame', that function is called with the
5287 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
5290 Finally, an element of this list can be also specified as
5291 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
5292 will then call FUNCTION with the buffer whose name matched
5293 REGEXP as first, and OTHER-ARGS as second argument.
5295 Any alternative function specified here is responsible for
5296 setting up the quit-restore parameter of the window used.
5298 If this variable appears \"not to work\", because you added a
5299 name to it but the corresponding buffer is displayed in the
5300 selected window, look at the values of `same-window-buffer-names'
5301 and `same-window-regexps'. Those variables take precedence over
5304 See also `special-display-buffer-names'."
5306 (choice :tag
"Buffer"
5308 (regexp :format
"%v")
5309 (cons :tag
"With parameters"
5312 (regexp :format
"%v")
5313 (repeat :tag
"Parameters"
5315 (symbol :tag
"Parameter")
5316 (sexp :tag
"Value"))))
5317 (list :tag
"With function"
5320 (regexp :format
"%v")
5321 (function :tag
"Function")
5322 (repeat :tag
"Arguments" (sexp)))))
5325 (make-obsolete-variable 'special-display-regexps
'display-buffer-alist
"24.3")
5326 (put 'special-display-regexps
'risky-local-variable t
)
5328 (defun special-display-p (buffer-name)
5329 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
5330 More precisely, return t if `special-display-buffer-names' or
5331 `special-display-regexps' contain a string entry equaling or
5332 matching BUFFER-NAME. If `special-display-buffer-names' or
5333 `special-display-regexps' contain a list entry whose car equals
5334 or matches BUFFER-NAME, the return value is the cdr of that
5338 ((member buffer-name special-display-buffer-names
)
5340 ((setq tmp
(assoc buffer-name special-display-buffer-names
))
5343 (dolist (regexp special-display-regexps
)
5346 (when (string-match-p regexp buffer-name
)
5348 ((and (consp regexp
) (stringp (car regexp
))
5349 (string-match-p (car regexp
) buffer-name
))
5350 (throw 'found
(cdr regexp
))))))))))
5352 (defcustom special-display-frame-alist
5353 '((height .
14) (width .
80) (unsplittable . t
))
5354 "Alist of parameters for special frames.
5355 Special frames are used for buffers whose names are listed in
5356 `special-display-buffer-names' and for buffers whose names match
5357 one of the regular expressions in `special-display-regexps'.
5359 This variable can be set in your init file, like this:
5361 (setq special-display-frame-alist '((width . 80) (height . 20)))
5363 These supersede the values given in `default-frame-alist'."
5364 :type
'(repeat (cons :format
"%v"
5365 (symbol :tag
"Parameter")
5366 (sexp :tag
"Value")))
5368 (make-obsolete-variable 'special-display-frame-alist
'display-buffer-alist
"24.3")
5370 (defun special-display-popup-frame (buffer &optional args
)
5371 "Pop up a frame displaying BUFFER and return its window.
5372 If BUFFER is already displayed in a visible or iconified frame,
5373 raise that frame. Otherwise, display BUFFER in a new frame.
5375 Optional argument ARGS is a list specifying additional
5378 If ARGS is an alist, use it as a list of frame parameters. If
5379 these parameters contain (same-window . t), display BUFFER in
5380 the selected window. If they contain (same-frame . t), display
5381 BUFFER in a window of the selected frame.
5383 If ARGS is a list whose car is a symbol, use (car ARGS) as a
5384 function to do the work. Pass it BUFFER as first argument, and
5385 pass the elements of (cdr ARGS) as the remaining arguments."
5386 (if (and args
(symbolp (car args
)))
5387 (apply (car args
) buffer
(cdr args
))
5388 (let ((window (get-buffer-window buffer
0)))
5390 ;; If we have a window already, make it visible.
5392 (let ((frame (window-frame window
)))
5393 (make-frame-visible frame
)
5395 (display-buffer-record-window 'reuse window buffer
)
5397 ;; Reuse the current window if the user requested it.
5398 (when (cdr (assq 'same-window args
))
5400 (progn (switch-to-buffer buffer nil t
) (selected-window))
5402 ;; Stay on the same frame if requested.
5403 (when (or (cdr (assq 'same-frame args
)) (cdr (assq 'same-window args
)))
5404 (let* ((pop-up-windows t
)
5406 special-display-buffer-names special-display-regexps
)
5407 (display-buffer buffer
)))
5408 ;; If no window yet, make one in a new frame.
5410 (with-current-buffer buffer
5411 (make-frame (append args special-display-frame-alist
))))
5412 (window (frame-selected-window frame
)))
5413 (display-buffer-record-window 'frame window buffer
)
5414 (unless (eq buffer
(window-buffer window
))
5415 (set-window-buffer window buffer
)
5416 (set-window-prev-buffers window nil
))
5417 (set-window-dedicated-p window t
)
5420 (defcustom special-display-function
'special-display-popup-frame
5421 "Function to call for displaying special buffers.
5422 This function is called with two arguments - the buffer and,
5423 optionally, a list - and should return a window displaying that
5424 buffer. The default value usually makes a separate frame for the
5425 buffer using `special-display-frame-alist' to specify the frame
5426 parameters. See the definition of `special-display-popup-frame'
5427 for how to specify such a function.
5429 A buffer is special when its name is either listed in
5430 `special-display-buffer-names' or matches a regexp in
5431 `special-display-regexps'.
5433 The specified function should call `display-buffer-record-window'
5434 with corresponding arguments to set up the quit-restore parameter
5435 of the window used."
5438 (make-obsolete-variable 'special-display-function
'display-buffer-alist
"24.3")
5440 (defcustom same-window-buffer-names nil
5441 "List of names of buffers that should appear in the \"same\" window.
5442 `display-buffer' and `pop-to-buffer' show a buffer whose name is
5443 on this list in the selected rather than some other window.
5445 An element of this list can be a cons cell instead of just a
5446 string. In that case, the cell's car must be a string specifying
5447 the buffer name. This is for compatibility with
5448 `special-display-buffer-names'; the cdr of the cons cell is
5451 See also `same-window-regexps'."
5452 :type
'(repeat (string :format
"%v"))
5455 (defcustom same-window-regexps nil
5456 "List of regexps saying which buffers should appear in the \"same\" window.
5457 `display-buffer' and `pop-to-buffer' show a buffer whose name
5458 matches a regexp on this list in the selected rather than some
5461 An element of this list can be a cons cell instead of just a
5462 string. In that case, the cell's car must be a regexp matching
5463 the buffer name. This is for compatibility with
5464 `special-display-regexps'; the cdr of the cons cell is ignored.
5466 See also `same-window-buffer-names'."
5467 :type
'(repeat (regexp :format
"%v"))
5470 (defun same-window-p (buffer-name)
5471 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5472 This function returns non-nil if `display-buffer' or
5473 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
5474 selected rather than (as usual) some other window. See
5475 `same-window-buffer-names' and `same-window-regexps'."
5477 ((not (stringp buffer-name
)))
5478 ;; The elements of `same-window-buffer-names' can be buffer
5479 ;; names or cons cells whose cars are buffer names.
5480 ((member buffer-name same-window-buffer-names
))
5481 ((assoc buffer-name same-window-buffer-names
))
5483 (dolist (regexp same-window-regexps
)
5484 ;; The elements of `same-window-regexps' can be regexps
5485 ;; or cons cells whose cars are regexps.
5486 (when (or (and (stringp regexp
)
5487 (string-match-p regexp buffer-name
))
5488 (and (consp regexp
) (stringp (car regexp
))
5489 (string-match-p (car regexp
) buffer-name
)))
5490 (throw 'found t
)))))))
5492 (defcustom pop-up-frames nil
5493 "Whether `display-buffer' should make a separate frame.
5494 If nil, never make a separate frame.
5495 If the value is `graphic-only', make a separate frame
5496 on graphic displays only.
5497 Any other non-nil value means always make a separate frame."
5499 (const :tag
"Never" nil
)
5500 (const :tag
"On graphic displays only" graphic-only
)
5501 (const :tag
"Always" t
))
5504 (defcustom display-buffer-reuse-frames nil
5505 "Non-nil means `display-buffer' should reuse frames.
5506 If the buffer in question is already displayed in a frame, raise
5512 (make-obsolete-variable
5513 'display-buffer-reuse-frames
5514 "use a `reusable-frames' alist entry in `display-buffer-alist'."
5517 (defcustom pop-up-windows t
5518 "Non-nil means `display-buffer' should make a new window."
5522 (defcustom split-window-preferred-function
'split-window-sensibly
5523 "Function called by `display-buffer' routines to split a window.
5524 This function is called with a window as single argument and is
5525 supposed to split that window and return the new window. If the
5526 window can (or shall) not be split, it is supposed to return nil.
5527 The default is to call the function `split-window-sensibly' which
5528 tries to split the window in a way which seems most suitable.
5529 You can customize the options `split-height-threshold' and/or
5530 `split-width-threshold' in order to have `split-window-sensibly'
5531 prefer either vertical or horizontal splitting.
5533 If you set this to any other function, bear in mind that the
5534 `display-buffer' routines may call this function two times. The
5535 argument of the first call is the largest window on its frame.
5536 If that call fails to return a live window, the function is
5537 called again with the least recently used window as argument. If
5538 that call fails too, `display-buffer' will use an existing window
5539 to display its buffer.
5541 The window selected at the time `display-buffer' was invoked is
5542 still selected when this function is called. Hence you can
5543 compare the window argument with the value of `selected-window'
5544 if you intend to split the selected window instead or if you do
5545 not want to split the selected window."
5550 (defcustom split-height-threshold
80
5551 "Minimum height for splitting windows sensibly.
5552 If this is an integer, `split-window-sensibly' may split a window
5553 vertically only if it has at least this many lines. If this is
5554 nil, `split-window-sensibly' is not allowed to split a window
5555 vertically. If, however, a window is the only window on its
5556 frame, `split-window-sensibly' may split it vertically
5557 disregarding the value of this variable."
5558 :type
'(choice (const nil
) (integer :tag
"lines"))
5562 (defcustom split-width-threshold
160
5563 "Minimum width for splitting windows sensibly.
5564 If this is an integer, `split-window-sensibly' may split a window
5565 horizontally only if it has at least this many columns. If this
5566 is nil, `split-window-sensibly' is not allowed to split a window
5568 :type
'(choice (const nil
) (integer :tag
"columns"))
5572 (defun window-splittable-p (window &optional horizontal
)
5573 "Return non-nil if `split-window-sensibly' may split WINDOW.
5574 Optional argument HORIZONTAL nil or omitted means check whether
5575 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
5576 non-nil means check whether WINDOW may be split horizontally.
5578 WINDOW may be split vertically when the following conditions
5580 - `window-size-fixed' is either nil or equals `width' for the
5582 - `split-height-threshold' is an integer and WINDOW is at least as
5583 high as `split-height-threshold'.
5584 - When WINDOW is split evenly, the emanating windows are at least
5585 `window-min-height' lines tall and can accommodate at least one
5586 line plus - if WINDOW has one - a mode line.
5588 WINDOW may be split horizontally when the following conditions
5590 - `window-size-fixed' is either nil or equals `height' for the
5592 - `split-width-threshold' is an integer and WINDOW is at least as
5593 wide as `split-width-threshold'.
5594 - When WINDOW is split evenly, the emanating windows are at least
5595 `window-min-width' or two (whichever is larger) columns wide."
5596 (when (window-live-p window
)
5597 (with-current-buffer (window-buffer window
)
5599 ;; A window can be split horizontally when its width is not
5600 ;; fixed, it is at least `split-width-threshold' columns wide
5601 ;; and at least twice as wide as `window-min-width' and 2 (the
5602 ;; latter value is hardcoded).
5603 (and (memq window-size-fixed
'(nil height
))
5604 ;; Testing `window-full-width-p' here hardly makes any
5605 ;; sense nowadays. This can be done more intuitively by
5606 ;; setting up `split-width-threshold' appropriately.
5607 (numberp split-width-threshold
)
5608 (>= (window-width window
)
5609 (max split-width-threshold
5610 (* 2 (max window-min-width
2)))))
5611 ;; A window can be split vertically when its height is not
5612 ;; fixed, it is at least `split-height-threshold' lines high,
5613 ;; and it is at least twice as high as `window-min-height' and 2
5614 ;; if it has a mode line or 1.
5615 (and (memq window-size-fixed
'(nil width
))
5616 (numberp split-height-threshold
)
5617 (>= (window-height window
)
5618 (max split-height-threshold
5619 (* 2 (max window-min-height
5620 (if mode-line-format
2 1))))))))))
5622 (defun split-window-sensibly (&optional window
)
5623 "Split WINDOW in a way suitable for `display-buffer'.
5624 WINDOW defaults to the currently selected window.
5625 If `split-height-threshold' specifies an integer, WINDOW is at
5626 least `split-height-threshold' lines tall and can be split
5627 vertically, split WINDOW into two windows one above the other and
5628 return the lower window. Otherwise, if `split-width-threshold'
5629 specifies an integer, WINDOW is at least `split-width-threshold'
5630 columns wide and can be split horizontally, split WINDOW into two
5631 windows side by side and return the window on the right. If this
5632 can't be done either and WINDOW is the only window on its frame,
5633 try to split WINDOW vertically disregarding any value specified
5634 by `split-height-threshold'. If that succeeds, return the lower
5635 window. Return nil otherwise.
5637 By default `display-buffer' routines call this function to split
5638 the largest or least recently used window. To change the default
5639 customize the option `split-window-preferred-function'.
5641 You can enforce this function to not split WINDOW horizontally,
5642 by setting (or binding) the variable `split-width-threshold' to
5643 nil. If, in addition, you set `split-height-threshold' to zero,
5644 chances increase that this function does split WINDOW vertically.
5646 In order to not split WINDOW vertically, set (or bind) the
5647 variable `split-height-threshold' to nil. Additionally, you can
5648 set `split-width-threshold' to zero to make a horizontal split
5649 more likely to occur.
5651 Have a look at the function `window-splittable-p' if you want to
5652 know how `split-window-sensibly' determines whether WINDOW can be
5654 (let ((window (or window
(selected-window))))
5655 (or (and (window-splittable-p window
)
5656 ;; Split window vertically.
5657 (with-selected-window window
5658 (split-window-below)))
5659 (and (window-splittable-p window t
)
5660 ;; Split window horizontally.
5661 (with-selected-window window
5662 (split-window-right)))
5663 (and (eq window
(frame-root-window (window-frame window
)))
5664 (not (window-minibuffer-p window
))
5665 ;; If WINDOW is the only window on its frame and is not the
5666 ;; minibuffer window, try to split it vertically disregarding
5667 ;; the value of `split-height-threshold'.
5668 (let ((split-height-threshold 0))
5669 (when (window-splittable-p window
)
5670 (with-selected-window window
5671 (split-window-below))))))))
5673 (defun window--try-to-split-window (window &optional alist
)
5674 "Try to split WINDOW.
5675 Return value returned by `split-window-preferred-function' if it
5676 represents a live window, nil otherwise."
5677 (and (window-live-p window
)
5678 (not (frame-parameter (window-frame window
) 'unsplittable
))
5679 (let* ((window-combination-limit
5680 ;; When `window-combination-limit' equals
5681 ;; `display-buffer' or equals `resize-window' and a
5682 ;; `window-height' or `window-width' alist entry are
5683 ;; present, bind it to t so resizing steals space
5684 ;; preferably from the window that was split.
5685 (if (or (eq window-combination-limit
'display-buffer
)
5686 (and (eq window-combination-limit
'window-size
)
5687 (or (cdr (assq 'window-height alist
))
5688 (cdr (assq 'window-width alist
)))))
5690 window-combination-limit
))
5692 ;; Since `split-window-preferred-function' might
5693 ;; throw an error use `condition-case'.
5695 (funcall split-window-preferred-function window
)
5697 (and (window-live-p new-window
) new-window
))))
5699 (defun window--frame-usable-p (frame)
5700 "Return FRAME if it can be used to display a buffer."
5701 (when (frame-live-p frame
)
5702 (let ((window (frame-root-window frame
)))
5703 ;; `frame-root-window' may be an internal window which is considered
5704 ;; "dead" by `window-live-p'. Hence if `window' is not live we
5705 ;; implicitly know that `frame' has a visible window we can use.
5706 (unless (and (window-live-p window
)
5707 (or (window-minibuffer-p window
)
5708 ;; If the window is soft-dedicated, the frame is usable.
5709 ;; Actually, even if the window is really dedicated,
5710 ;; the frame is still usable by splitting it.
5711 ;; At least Emacs-22 allowed it, and it is desirable
5712 ;; when displaying same-frame windows.
5713 nil
; (eq t (window-dedicated-p window))
5717 (defcustom even-window-heights t
5718 "If non-nil `display-buffer' will try to even window heights.
5719 Otherwise `display-buffer' will leave the window configuration
5720 alone. Heights are evened only when `display-buffer' chooses a
5721 window that appears above or below the selected window."
5725 (defun window--even-window-heights (window)
5726 "Even heights of WINDOW and selected window.
5727 Do this only if these windows are vertically adjacent to each
5728 other, `even-window-heights' is non-nil, and the selected window
5729 is higher than WINDOW."
5730 (when (and even-window-heights
5731 ;; Even iff WINDOW forms a vertical combination with the
5732 ;; selected window, and WINDOW's height exceeds that of the
5733 ;; selected window, see also bug#11880.
5734 (window-combined-p window
)
5735 (= (window-child-count (window-parent window
)) 2)
5736 (eq (window-parent) (window-parent window
))
5737 (> (window-total-height) (window-total-height window
)))
5738 ;; Don't throw an error if we can't even window heights for
5742 (/ (- (window-total-height window
) (window-total-height)) 2))
5745 (defun window--display-buffer (buffer window type
&optional alist dedicated
)
5746 "Display BUFFER in WINDOW.
5747 TYPE must be one of the symbols `reuse', `window' or `frame' and
5748 is passed unaltered to `display-buffer-record-window'. ALIST is
5749 the alist argument of `display-buffer'. Set `window-dedicated-p'
5750 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
5752 (when (and (buffer-live-p buffer
) (window-live-p window
))
5753 (display-buffer-record-window type window buffer
)
5754 (unless (eq buffer
(window-buffer window
))
5755 (set-window-dedicated-p window nil
)
5756 (set-window-buffer window buffer
)
5758 (set-window-dedicated-p window dedicated
))
5759 (when (memq type
'(window frame
))
5760 (set-window-prev-buffers window nil
)))
5761 (let ((frame (window-frame window
))
5762 (parameter (window-parameter window
'quit-restore
))
5763 (height (cdr (assq 'window-height alist
)))
5764 (width (cdr (assq 'window-width alist
)))
5765 (size (cdr (assq 'window-size alist
))))
5767 ((or (eq type
'frame
)
5768 (and (eq (car parameter
) 'same
)
5769 (eq (nth 1 parameter
) 'frame
)))
5770 ;; Adjust size of frame if asked for.
5774 (let ((width (car size
))
5776 (frame (window-frame window
))
5778 (when (and (numberp width
) (numberp height
))
5780 frame
(+ (frame-height frame
)
5781 (- height
(window-total-height window
))))
5783 frame
(+ (frame-width frame
)
5784 (- width
(window-total-width window
)))))))
5786 (ignore-errors (funcall size window
)))))
5787 ((or (eq type
'window
)
5788 (and (eq (car parameter
) 'same
)
5789 (eq (nth 1 parameter
) 'window
)))
5790 ;; Adjust height of window if asked for.
5795 (if (integerp height
)
5798 (* (window-total-height (frame-root-window window
))
5800 (delta (- new-height
(window-total-height window
))))
5801 (when (and (window--resizable-p window delta nil
'safe
)
5802 (window-combined-p window
))
5803 (window-resize window delta nil
'safe
))))
5805 (ignore-errors (funcall height window
))))
5806 ;; Adjust width of window if asked for.
5811 (if (integerp width
)
5814 (* (window-total-width (frame-root-window window
))
5816 (delta (- new-width
(window-total-width window
))))
5817 (when (and (window--resizable-p window delta t
'safe
)
5818 (window-combined-p window t
))
5819 (window-resize window delta t
'safe
))))
5821 (ignore-errors (funcall width window
)))))))
5825 (defun window--maybe-raise-frame (frame)
5826 (let ((visible (frame-visible-p frame
)))
5827 (unless (or (not visible
)
5828 ;; Assume the selected frame is already visible enough.
5829 (eq frame
(selected-frame))
5830 ;; Assume the frame from which we invoked the
5831 ;; minibuffer is visible.
5832 (and (minibuffer-window-active-p (selected-window))
5833 (eq frame
(window-frame (minibuffer-selected-window)))))
5834 (raise-frame frame
))))
5836 ;; FIXME: Not implemented.
5837 ;; FIXME: By the way, there could be more levels of dedication:
5838 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
5839 ;; the window hasn't been used for something else yet.
5840 ;; - `softly' dedicated only allows reuse when asked explicitly.
5841 ;; - `strongly' never allows reuse.
5842 (defvar display-buffer-mark-dedicated nil
5843 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
5844 The actual non-nil value of this variable will be copied to the
5845 `window-dedicated-p' flag.")
5847 (defconst display-buffer--action-function-custom-type
5848 '(choice :tag
"Function"
5849 (const :tag
"--" ignore
) ; default for insertion
5850 (const display-buffer-reuse-window
)
5851 (const display-buffer-pop-up-window
)
5852 (const display-buffer-same-window
)
5853 (const display-buffer-pop-up-frame
)
5854 (const display-buffer-in-previous-window
)
5855 (const display-buffer-use-some-window
)
5856 (function :tag
"Other function"))
5857 "Custom type for `display-buffer' action functions.")
5859 (defconst display-buffer--action-custom-type
5860 `(cons :tag
"Action"
5861 (choice :tag
"Action functions"
5862 ,display-buffer--action-function-custom-type
5864 :tag
"List of functions"
5865 ,display-buffer--action-function-custom-type
))
5866 (alist :tag
"Action arguments"
5868 :value-type
(sexp :tag
"Value")))
5869 "Custom type for `display-buffer' actions.")
5871 (defvar display-buffer-overriding-action
'(nil . nil
)
5872 "Overriding action to perform to display a buffer.
5873 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5874 function or a list of functions. Each function should accept two
5875 arguments: a buffer to display and an alist similar to ALIST.
5876 See `display-buffer' for details.")
5877 (put 'display-buffer-overriding-action
'risky-local-variable t
)
5879 (defcustom display-buffer-alist nil
5880 "Alist of conditional actions for `display-buffer'.
5881 This is a list of elements (CONDITION . ACTION), where:
5883 CONDITION is either a regexp matching buffer names, or a
5884 function that takes two arguments - a buffer name and the
5885 ACTION argument of `display-buffer' - and returns a boolean.
5887 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
5888 function or a list of functions. Each such function should
5889 accept two arguments: a buffer to display and an alist of the
5890 same form as ALIST. See `display-buffer' for details.
5892 `display-buffer' scans this alist until it either finds a
5893 matching regular expression or the function specified by a
5894 condition returns non-nil. In any of these cases, it adds the
5895 associated action to the list of actions it will try."
5896 :type
`(alist :key-type
5897 (choice :tag
"Condition"
5899 (function :tag
"Matcher function"))
5900 :value-type
,display-buffer--action-custom-type
)
5905 (defcustom display-buffer-base-action
'(nil . nil
)
5906 "User-specified default action for `display-buffer'.
5907 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5908 function or a list of functions. Each function should accept two
5909 arguments: a buffer to display and an alist similar to ALIST.
5910 See `display-buffer' for details."
5911 :type display-buffer--action-custom-type
5916 (defconst display-buffer-fallback-action
5917 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
5918 display-buffer-reuse-window
5919 display-buffer--maybe-pop-up-frame-or-window
5920 display-buffer-in-previous-window
5921 display-buffer-use-some-window
5922 ;; If all else fails, pop up a new frame.
5923 display-buffer-pop-up-frame
))
5924 "Default fallback action for `display-buffer'.
5925 This is the action used by `display-buffer' if no other actions
5926 specified, e.g. by the user options `display-buffer-alist' or
5927 `display-buffer-base-action'. See `display-buffer'.")
5928 (put 'display-buffer-fallback-action
'risky-local-variable t
)
5930 (defun display-buffer-assq-regexp (buffer-name alist action
)
5931 "Retrieve ALIST entry corresponding to BUFFER-NAME.
5932 ACTION is the action argument passed to `display-buffer'."
5934 (dolist (entry alist
)
5935 (let ((key (car entry
)))
5936 (when (or (and (stringp key
)
5937 (string-match-p key buffer-name
))
5938 (and (functionp key
)
5939 (funcall key buffer-name action
)))
5940 (throw 'match
(cdr entry
)))))))
5942 (defvar display-buffer--same-window-action
5943 '(display-buffer-same-window
5944 (inhibit-same-window . nil
))
5945 "A `display-buffer' action for displaying in the same window.")
5946 (put 'display-buffer--same-window-action
'risky-local-variable t
)
5948 (defvar display-buffer--other-frame-action
5949 '((display-buffer-reuse-window
5950 display-buffer-pop-up-frame
)
5951 (reusable-frames .
0)
5952 (inhibit-same-window . t
))
5953 "A `display-buffer' action for displaying in another frame.")
5954 (put 'display-buffer--other-frame-action
'risky-local-variable t
)
5956 (defun display-buffer (buffer-or-name &optional action frame
)
5957 "Display BUFFER-OR-NAME in some window, without selecting it.
5958 BUFFER-OR-NAME must be a buffer or the name of an existing
5959 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
5960 or nil if no such window is found.
5962 Optional argument ACTION, if non-nil, should specify a display
5963 action. Its form is described below.
5965 Optional argument FRAME, if non-nil, acts like an additional
5966 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
5967 specifying the frame(s) to search for a window that is already
5968 displaying the buffer. See `display-buffer-reuse-window'
5970 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
5971 where FUNCTION is either a function or a list of functions, and
5972 ALIST is an arbitrary association list (alist).
5974 Each such FUNCTION should accept two arguments: the buffer to
5975 display and an alist. Based on those arguments, it should
5976 display the buffer and return the window. If the caller is
5977 prepared to handle the case of not displaying the buffer
5978 and returning nil from `display-buffer' it should pass
5979 \(allow-no-window . t) as an element of the ALIST.
5981 The `display-buffer' function builds a function list and an alist
5982 by combining the functions and alists specified in
5983 `display-buffer-overriding-action', `display-buffer-alist', the
5984 ACTION argument, `display-buffer-base-action', and
5985 `display-buffer-fallback-action' (in order). Then it calls each
5986 function in the combined function list in turn, passing the
5987 buffer as the first argument and the combined alist as the second
5988 argument, until one of the functions returns non-nil.
5990 If ACTION is nil, the function list and the alist are built using
5991 only the other variables mentioned above.
5993 Available action functions include:
5994 `display-buffer-same-window'
5995 `display-buffer-reuse-window'
5996 `display-buffer-pop-up-frame'
5997 `display-buffer-pop-up-window'
5998 `display-buffer-in-previous-window'
5999 `display-buffer-use-some-window'
6001 Recognized alist entries include:
6003 `inhibit-same-window' -- A non-nil value prevents the same
6004 window from being used for display.
6006 `inhibit-switch-frame' -- A non-nil value prevents any other
6007 frame from being raised or selected,
6008 even if the window is displayed there.
6010 `reusable-frames' -- Value specifies frame(s) to search for a
6011 window that already displays the buffer.
6012 See `display-buffer-reuse-window'.
6014 `pop-up-frame-parameters' -- Value specifies an alist of frame
6015 parameters to give a new frame, if
6018 `window-height' -- Value specifies either an integer (the number
6019 of lines of a new window), a floating point number (the
6020 fraction of a new window with respect to the height of the
6021 frame's root window) or a function to be called with one
6022 argument - a new window. The function is supposed to adjust
6023 the height of the window; its return value is ignored.
6024 Suitable functions are `shrink-window-if-larger-than-buffer'
6025 and `fit-window-to-buffer'.
6027 `window-width' -- Value specifies either an integer (the number
6028 of columns of a new window), a floating point number (the
6029 fraction of a new window with respect to the width of the
6030 frame's root window) or a function to be called with one
6031 argument - a new window. The function is supposed to adjust
6032 the width of the window; its return value is ignored.
6034 `allow-no-window' -- A non-nil value indicates readiness for the case
6035 of not displaying the buffer and FUNCTION can safely return
6036 a non-window value to suppress displaying.
6038 The ACTION argument to `display-buffer' can also have a non-nil
6039 and non-list value. This means to display the buffer in a window
6040 other than the selected one, even if it is already displayed in
6041 the selected window. If called interactively with a prefix
6042 argument, ACTION is t."
6043 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6044 (if current-prefix-arg t
)))
6045 (let ((buffer (if (bufferp buffer-or-name
)
6047 (get-buffer buffer-or-name
)))
6048 ;; Make sure that when we split windows the old window keeps
6049 ;; point, bug#14829.
6050 (split-window-keep-point t
)
6051 ;; Handle the old form of the first argument.
6052 (inhibit-same-window (and action
(not (listp action
)))))
6053 (unless (listp action
) (setq action nil
))
6054 (if display-buffer-function
6055 ;; If `display-buffer-function' is defined, let it do the job.
6056 (funcall display-buffer-function buffer inhibit-same-window
)
6057 ;; Otherwise, use the defined actions.
6059 (display-buffer-assq-regexp
6060 (buffer-name buffer
) display-buffer-alist action
))
6061 (special-action (display-buffer--special-action buffer
))
6062 ;; Extra actions from the arguments to this function:
6064 (cons nil
(append (if inhibit-same-window
6065 '((inhibit-same-window . t
)))
6067 `((reusable-frames .
,frame
))))))
6068 ;; Construct action function list and action alist.
6069 (actions (list display-buffer-overriding-action
6070 user-action special-action action extra-action
6071 display-buffer-base-action
6072 display-buffer-fallback-action
))
6073 (functions (apply 'append
6076 (if (functionp x
) (list x
) x
))
6078 (alist (apply 'append
(mapcar 'cdr actions
)))
6080 (unless (buffer-live-p buffer
)
6081 (error "Invalid buffer"))
6082 (while (and functions
(not window
))
6083 (setq window
(funcall (car functions
) buffer alist
)
6084 functions
(cdr functions
)))
6085 (and (windowp window
) window
)))))
6087 (defun display-buffer-other-frame (buffer)
6088 "Display buffer BUFFER preferably in another frame.
6089 This uses the function `display-buffer' as a subroutine; see
6090 its documentation for additional customization information."
6091 (interactive "BDisplay buffer in other frame: ")
6092 (display-buffer buffer display-buffer--other-frame-action t
))
6094 ;;; `display-buffer' action functions:
6096 (defun display-buffer-same-window (buffer alist
)
6097 "Display BUFFER in the selected window.
6098 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
6099 if the selected window is a minibuffer window or is dedicated to
6100 another buffer; in that case, return nil. Otherwise, return the
6102 (unless (or (cdr (assq 'inhibit-same-window alist
))
6103 (window-minibuffer-p)
6104 (window-dedicated-p))
6105 (window--display-buffer buffer
(selected-window) 'reuse alist
)))
6107 (defun display-buffer--maybe-same-window (buffer alist
)
6108 "Conditionally display BUFFER in the selected window.
6109 If `same-window-p' returns non-nil for BUFFER's name, call
6110 `display-buffer-same-window' and return its value. Otherwise,
6112 (and (same-window-p (buffer-name buffer
))
6113 (display-buffer-same-window buffer alist
)))
6115 (defun display-buffer-reuse-window (buffer alist
)
6116 "Return a window that is already displaying BUFFER.
6117 Return nil if no usable window is found.
6119 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6120 window is not eligible for reuse.
6122 If ALIST contains a `reusable-frames' entry, its value determines
6123 which frames to search for a reusable window:
6124 nil -- the selected frame (actually the last non-minibuffer frame)
6125 A frame -- just that frame
6126 `visible' -- all visible frames
6127 0 -- all frames on the current terminal
6130 If ALIST contains no `reusable-frames' entry, search just the
6131 selected frame if `display-buffer-reuse-frames' and
6132 `pop-up-frames' are both nil; search all frames on the current
6133 terminal if either of those variables is non-nil.
6135 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6136 event that a window on another frame is chosen, avoid raising
6138 (let* ((alist-entry (assq 'reusable-frames alist
))
6139 (frames (cond (alist-entry (cdr alist-entry
))
6140 ((if (eq pop-up-frames
'graphic-only
)
6144 (display-buffer-reuse-frames 0)
6145 (t (last-nonminibuffer-frame))))
6146 (window (if (and (eq buffer
(window-buffer))
6147 (not (cdr (assq 'inhibit-same-window alist
))))
6149 (car (delq (selected-window)
6150 (get-buffer-window-list buffer
'nomini
6152 (when (window-live-p window
)
6153 (prog1 (window--display-buffer buffer window
'reuse alist
)
6154 (unless (cdr (assq 'inhibit-switch-frame alist
))
6155 (window--maybe-raise-frame (window-frame window
)))))))
6157 (defun display-buffer--special-action (buffer)
6158 "Return special display action for BUFFER, if any.
6159 If `special-display-p' returns non-nil for BUFFER, return an
6160 appropriate display action involving `special-display-function'.
6161 See `display-buffer' for the format of display actions."
6162 (and special-display-function
6163 ;; `special-display-p' returns either t or a list of frame
6164 ;; parameters to pass to `special-display-function'.
6165 (let ((pars (special-display-p (buffer-name buffer
))))
6167 (list (list #'display-buffer-reuse-window
6168 `(lambda (buffer _alist
)
6169 (funcall special-display-function
6170 buffer
',(if (listp pars
) pars
)))))))))
6172 (defun display-buffer-pop-up-frame (buffer alist
)
6173 "Display BUFFER in a new frame.
6174 This works by calling `pop-up-frame-function'. If successful,
6175 return the window used; otherwise return nil.
6177 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
6178 raising the new frame.
6180 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
6181 corresponding value is an alist of frame parameters to give the
6183 (let* ((params (cdr (assq 'pop-up-frame-parameters alist
)))
6184 (pop-up-frame-alist (append params pop-up-frame-alist
))
6185 (fun pop-up-frame-function
)
6188 ;; Make BUFFER current so `make-frame' will use it as the
6189 ;; new frame's buffer (Bug#15133).
6190 (with-current-buffer buffer
6191 (setq frame
(funcall fun
)))
6192 (setq window
(frame-selected-window frame
)))
6193 (prog1 (window--display-buffer
6194 buffer window
'frame alist display-buffer-mark-dedicated
)
6195 (unless (cdr (assq 'inhibit-switch-frame alist
))
6196 (window--maybe-raise-frame frame
))))))
6198 (defun display-buffer-pop-up-window (buffer alist
)
6199 "Display BUFFER by popping up a new window.
6200 The new window is created on the selected frame, or in
6201 `last-nonminibuffer-frame' if no windows can be created there.
6202 If successful, return the new window; otherwise return nil.
6204 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6205 event that the new window is created on another frame, avoid
6207 (let ((frame (or (window--frame-usable-p (selected-frame))
6208 (window--frame-usable-p (last-nonminibuffer-frame))))
6210 (when (and (or (not (frame-parameter frame
'unsplittable
))
6211 ;; If the selected frame cannot be split, look at
6212 ;; `last-nonminibuffer-frame'.
6213 (and (eq frame
(selected-frame))
6214 (setq frame
(last-nonminibuffer-frame))
6215 (window--frame-usable-p frame
)
6216 (not (frame-parameter frame
'unsplittable
))))
6217 ;; Attempt to split largest or least recently used window.
6218 (setq window
(or (window--try-to-split-window
6219 (get-largest-window frame t
) alist
)
6220 (window--try-to-split-window
6221 (get-lru-window frame t
) alist
))))
6222 (prog1 (window--display-buffer
6223 buffer window
'window alist display-buffer-mark-dedicated
)
6224 (unless (cdr (assq 'inhibit-switch-frame alist
))
6225 (window--maybe-raise-frame (window-frame window
)))))))
6227 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist
)
6228 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
6230 If `pop-up-frames' is non-nil (and not `graphic-only' on a
6231 text-only terminal), try with `display-buffer-pop-up-frame'.
6233 If that cannot be done, and `pop-up-windows' is non-nil, try
6234 again with `display-buffer-pop-up-window'."
6235 (or (and (if (eq pop-up-frames
'graphic-only
)
6238 (display-buffer-pop-up-frame buffer alist
))
6240 (display-buffer-pop-up-window buffer alist
))))
6242 (defun display-buffer-below-selected (buffer alist
)
6243 "Try displaying BUFFER in a window below the selected window.
6244 This either splits the selected window or reuses the window below
6247 (or (and (not (frame-parameter nil
'unsplittable
))
6248 (let ((split-height-threshold 0)
6249 split-width-threshold
)
6250 (setq window
(window--try-to-split-window (selected-window) alist
)))
6251 (window--display-buffer
6252 buffer window
'window alist display-buffer-mark-dedicated
))
6253 (and (setq window
(window-in-direction 'below
))
6254 (not (window-dedicated-p window
))
6255 (window--display-buffer
6256 buffer window
'reuse alist display-buffer-mark-dedicated
)))))
6258 (defun display-buffer-at-bottom (buffer alist
)
6259 "Try displaying BUFFER in a window at the bottom of the selected frame.
6260 This either splits the window at the bottom of the frame or the
6261 frame's root window, or reuses an existing window at the bottom
6262 of the selected frame."
6263 (let (bottom-window window
)
6265 (lambda (window) (setq bottom-window window
)) nil nil
'nomini
)
6266 (or (and (not (frame-parameter nil
'unsplittable
))
6267 (let (split-width-threshold)
6268 (setq window
(window--try-to-split-window bottom-window alist
)))
6269 (window--display-buffer
6270 buffer window
'window alist display-buffer-mark-dedicated
))
6271 (and (not (frame-parameter nil
'unsplittable
))
6274 (split-window (window--major-non-side-window))
6276 (window--display-buffer
6277 buffer window
'window alist display-buffer-mark-dedicated
))
6278 (and (setq window bottom-window
)
6279 (not (window-dedicated-p window
))
6280 (window--display-buffer
6281 buffer window
'reuse alist display-buffer-mark-dedicated
)))))
6283 (defun display-buffer-in-previous-window (buffer alist
)
6284 "Display BUFFER in a window previously showing it.
6285 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6286 window is not eligible for reuse.
6288 If ALIST contains a `reusable-frames' entry, its value determines
6289 which frames to search for a reusable window:
6290 nil -- the selected frame (actually the last non-minibuffer frame)
6291 A frame -- just that frame
6292 `visible' -- all visible frames
6293 0 -- all frames on the current terminal
6296 If ALIST contains no `reusable-frames' entry, search just the
6297 selected frame if `display-buffer-reuse-frames' and
6298 `pop-up-frames' are both nil; search all frames on the current
6299 terminal if either of those variables is non-nil.
6301 If ALIST has a `previous-window' entry, the window specified by
6302 that entry will override any other window found by the methods
6303 above, even if that window never showed BUFFER before."
6304 (let* ((alist-entry (assq 'reusable-frames alist
))
6305 (inhibit-same-window
6306 (cdr (assq 'inhibit-same-window alist
)))
6308 (alist-entry (cdr alist-entry
))
6309 ((if (eq pop-up-frames
'graphic-only
)
6313 (display-buffer-reuse-frames 0)
6314 (t (last-nonminibuffer-frame))))
6315 best-window second-best-window window
)
6316 ;; Scan windows whether they have shown the buffer recently.
6318 (dolist (window (window-list-1 (frame-first-window) 'nomini frames
))
6319 (when (and (assq buffer
(window-prev-buffers window
))
6320 (not (window-dedicated-p window
)))
6321 (if (eq window
(selected-window))
6322 (unless inhibit-same-window
6323 (setq second-best-window window
))
6324 (setq best-window window
)
6326 ;; When ALIST has a `previous-window' entry, that entry may override
6327 ;; anything we found so far.
6328 (when (and (setq window
(cdr (assq 'previous-window alist
)))
6329 (window-live-p window
)
6330 (not (window-dedicated-p window
)))
6331 (if (eq window
(selected-window))
6332 (unless inhibit-same-window
6333 (setq second-best-window window
))
6334 (setq best-window window
)))
6335 ;; Return best or second best window found.
6336 (when (setq window
(or best-window second-best-window
))
6337 (window--display-buffer buffer window
'reuse alist
))))
6339 (defun display-buffer-use-some-window (buffer alist
)
6340 "Display BUFFER in an existing window.
6341 Search for a usable window, set that window to the buffer, and
6342 return the window. If no suitable window is found, return nil.
6344 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6345 event that a window in another frame is chosen, avoid raising
6347 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist
)))
6348 (frame (or (window--frame-usable-p (selected-frame))
6349 (window--frame-usable-p (last-nonminibuffer-frame))))
6351 ;; Reuse an existing window.
6352 (or (get-lru-window frame nil not-this-window
)
6353 (let ((window (get-buffer-window buffer
'visible
)))
6354 (unless (and not-this-window
6355 (eq window
(selected-window)))
6357 (get-largest-window 'visible nil not-this-window
)
6358 (let ((window (get-buffer-window buffer
0)))
6359 (unless (and not-this-window
6360 (eq window
(selected-window)))
6362 (get-largest-window 0 nil not-this-window
)))
6363 (quit-restore (and (window-live-p window
)
6364 (window-parameter window
'quit-restore
)))
6365 (quad (nth 1 quit-restore
)))
6366 (when (window-live-p window
)
6367 ;; If the window was used by `display-buffer' before, try to
6368 ;; resize it to its old height but don't signal an error.
6369 (when (and (listp quad
)
6370 (integerp (nth 3 quad
))
6371 (/= (nth 3 quad
) (window-total-height window
)))
6373 (window-resize window
(- (nth 3 quad
) (window-total-height window
)))
6377 (window--display-buffer buffer window
'reuse alist
)
6378 (window--even-window-heights window
)
6379 (unless (cdr (assq 'inhibit-switch-frame alist
))
6380 (window--maybe-raise-frame (window-frame window
)))))))
6382 (defun display-buffer-no-window (buffer alist
)
6383 "Display BUFFER in no window.
6384 If ALIST has a non-nil `allow-no-window' entry, then don't display
6385 a window at all. This makes possible to override the default action
6386 and avoid displaying the buffer. It is assumed that when the caller
6387 specifies a non-nil `allow-no-window' then it can handle a nil value
6388 returned from `display-buffer' in this case."
6389 (when (cdr (assq 'allow-no-window alist
))
6392 ;;; Display + selection commands:
6393 (defun pop-to-buffer (buffer &optional action norecord
)
6394 "Select buffer BUFFER in some window, preferably a different one.
6395 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6396 is a string not naming an existent buffer, create a buffer with
6397 that name. If BUFFER is nil, choose some other buffer. Return
6400 This uses `display-buffer' as a subroutine. The optional ACTION
6401 argument is passed to `display-buffer' as its ACTION argument.
6402 See `display-buffer' for more information. ACTION is t if called
6403 interactively with a prefix argument, which means to pop to a
6404 window other than the selected one even if the buffer is already
6405 displayed in the selected window.
6407 If the window to show BUFFER is not on the selected
6408 frame, raise that window's frame and give it input focus.
6410 Optional third arg NORECORD non-nil means do not put this buffer
6411 at the front of the list of recently selected ones."
6412 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
6413 (if current-prefix-arg t
)))
6414 (setq buffer
(window-normalize-buffer-to-switch-to buffer
))
6415 ;; This should be done by `select-window' below.
6416 ;; (set-buffer buffer)
6417 (let* ((old-frame (selected-frame))
6418 (window (display-buffer buffer action
))
6419 (frame (window-frame window
)))
6420 ;; If we chose another frame, make sure it gets input focus.
6421 (unless (eq frame old-frame
)
6422 (select-frame-set-input-focus frame norecord
))
6423 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
6424 (select-window window norecord
)
6427 (defun pop-to-buffer-same-window (buffer &optional norecord
)
6428 "Select buffer BUFFER in some window, preferably the same one.
6429 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6430 is a string not naming an existent buffer, create a buffer with
6431 that name. If BUFFER is nil, choose some other buffer. Return
6434 Optional argument NORECORD, if non-nil means do not put this
6435 buffer at the front of the list of recently selected ones.
6437 Unlike `pop-to-buffer', this function prefers using the selected
6438 window over popping up a new window or frame."
6439 (pop-to-buffer buffer display-buffer--same-window-action norecord
))
6441 (defun read-buffer-to-switch (prompt)
6442 "Read the name of a buffer to switch to, prompting with PROMPT.
6443 Return the name of the buffer as a string.
6445 This function is intended for the `switch-to-buffer' family of
6446 commands since these need to omit the name of the current buffer
6447 from the list of completions and default values."
6448 (let ((rbts-completion-table (internal-complete-buffer-except)))
6449 (minibuffer-with-setup-hook
6451 (setq minibuffer-completion-table rbts-completion-table
)
6452 ;; Since rbts-completion-table is built dynamically, we
6453 ;; can't just add it to the default value of
6454 ;; icomplete-with-completion-tables, so we add it
6456 (if (and (boundp 'icomplete-with-completion-tables
)
6457 (listp icomplete-with-completion-tables
))
6458 (set (make-local-variable 'icomplete-with-completion-tables
)
6459 (cons rbts-completion-table
6460 icomplete-with-completion-tables
))))
6461 (read-buffer prompt
(other-buffer (current-buffer))
6462 (confirm-nonexistent-file-or-buffer)))))
6464 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
6465 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
6466 If BUFFER-OR-NAME is nil, return the buffer returned by
6467 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
6468 exists, return that buffer. If no such buffer exists, create a
6469 buffer with the name BUFFER-OR-NAME and return that buffer."
6471 (or (get-buffer buffer-or-name
)
6472 (let ((buffer (get-buffer-create buffer-or-name
)))
6473 (set-buffer-major-mode buffer
)
6477 (defcustom switch-to-buffer-preserve-window-point nil
6478 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
6479 If this is nil, `switch-to-buffer' displays the buffer at that
6480 buffer's `point'. If this is `already-displayed', it tries to
6481 display the buffer at its previous position in the selected
6482 window, provided the buffer is currently displayed in some other
6483 window on any visible or iconified frame. If this is t, it
6484 unconditionally tries to display the buffer at its previous
6485 position in the selected window.
6487 This variable is ignored if the buffer is already displayed in
6488 the selected window or never appeared in it before, or if
6489 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
6491 (const :tag
"Never" nil
)
6492 (const :tag
"If already displayed elsewhere" already-displayed
)
6493 (const :tag
"Always" t
))
6497 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window
)
6498 "Display buffer BUFFER-OR-NAME in the selected window.
6500 WARNING: This is NOT the way to work on another buffer temporarily
6501 within a Lisp program! Use `set-buffer' instead. That avoids
6502 messing with the window-buffer correspondences.
6504 If the selected window cannot display the specified
6505 buffer (e.g. if it is a minibuffer window or strongly dedicated
6506 to another buffer), call `pop-to-buffer' to select the buffer in
6509 If called interactively, read the buffer name using the
6510 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6511 determines whether to request confirmation before creating a new
6514 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
6515 If BUFFER-OR-NAME is a string that does not identify an existing
6516 buffer, create a buffer with that name. If BUFFER-OR-NAME is
6517 nil, switch to the buffer returned by `other-buffer'.
6519 If optional argument NORECORD is non-nil, do not put the buffer
6520 at the front of the buffer list, and do not make the window
6521 displaying it the most recently selected one.
6523 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
6524 must be displayed in the selected window; if that is impossible,
6525 signal an error rather than calling `pop-to-buffer'.
6527 The option `switch-to-buffer-preserve-window-point' can be used
6528 to make the buffer appear at its last position in the selected
6531 Return the buffer switched to."
6533 (list (read-buffer-to-switch "Switch to buffer: ") nil
'force-same-window
))
6534 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name
)))
6536 ;; Don't call set-window-buffer if it's not needed since it
6537 ;; might signal an error (e.g. if the window is dedicated).
6538 ((eq buffer
(window-buffer)))
6539 ((window-minibuffer-p)
6540 (if force-same-window
6541 (user-error "Cannot switch buffers in minibuffer window")
6542 (pop-to-buffer buffer norecord
)))
6543 ((eq (window-dedicated-p) t
)
6544 (if force-same-window
6545 (user-error "Cannot switch buffers in a dedicated window")
6546 (pop-to-buffer buffer norecord
)))
6548 (let* ((entry (assq buffer
(window-prev-buffers)))
6549 (displayed (and (eq switch-to-buffer-preserve-window-point
6551 (get-buffer-window buffer
0))))
6552 (set-window-buffer nil buffer
)
6554 (or (eq switch-to-buffer-preserve-window-point t
)
6556 ;; Try to restore start and point of buffer in the selected
6557 ;; window (Bug#4041).
6558 (set-window-start (selected-window) (nth 1 entry
) t
)
6559 (set-window-point nil
(nth 2 entry
))))))
6562 (select-window (selected-window)))
6563 (set-buffer buffer
)))
6565 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord
)
6566 "Select the buffer specified by BUFFER-OR-NAME in another window.
6567 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6568 nil. Return the buffer switched to.
6570 If called interactively, prompt for the buffer name using the
6571 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6572 determines whether to request confirmation before creating a new
6575 If BUFFER-OR-NAME is a string and does not identify an existing
6576 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6577 nil, switch to the buffer returned by `other-buffer'.
6579 Optional second argument NORECORD non-nil means do not put this
6580 buffer at the front of the list of recently selected ones.
6582 This uses the function `display-buffer' as a subroutine; see its
6583 documentation for additional customization information."
6585 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
6586 (let ((pop-up-windows t
))
6587 (pop-to-buffer buffer-or-name t norecord
)))
6589 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord
)
6590 "Switch to buffer BUFFER-OR-NAME in another frame.
6591 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6592 nil. Return the buffer switched to.
6594 If called interactively, prompt for the buffer name using the
6595 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6596 determines whether to request confirmation before creating a new
6599 If BUFFER-OR-NAME is a string and does not identify an existing
6600 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6601 nil, switch to the buffer returned by `other-buffer'.
6603 Optional second arg NORECORD non-nil means do not put this
6604 buffer at the front of the list of recently selected ones.
6606 This uses the function `display-buffer' as a subroutine; see its
6607 documentation for additional customization information."
6609 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
6610 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord
))
6612 (defun set-window-text-height (window height
)
6613 "Set the height in lines of the text display area of WINDOW to HEIGHT.
6614 WINDOW must be a live window and defaults to the selected one.
6615 HEIGHT doesn't include the mode line or header line, if any, or
6616 any partial-height lines in the text display area.
6618 Note that the current implementation of this function cannot
6619 always set the height exactly, but attempts to be conservative,
6620 by allocating more lines than are actually needed in the case
6621 where some error may be present."
6622 (setq window
(window-normalize-window window t
))
6623 (let ((delta (- height
(window-text-height window
))))
6624 (unless (zerop delta
)
6625 ;; Setting window-min-height to a value like 1 can lead to very
6626 ;; bizarre displays because it also allows Emacs to make *other*
6627 ;; windows one line tall, which means that there's no more space
6628 ;; for the mode line.
6629 (let ((window-min-height (min 2 height
)))
6630 (window-resize window delta
)))))
6632 (defun enlarge-window-horizontally (delta)
6633 "Make selected window DELTA columns wider.
6634 Interactively, if no argument is given, make selected window one
6637 (enlarge-window delta t
))
6639 (defun shrink-window-horizontally (delta)
6640 "Make selected window DELTA columns narrower.
6641 Interactively, if no argument is given, make selected window one
6644 (shrink-window delta t
))
6646 (defun count-screen-lines (&optional beg end count-final-newline window
)
6647 "Return the number of screen lines in the region.
6648 The number of screen lines may be different from the number of actual lines,
6649 due to line breaking, display table, etc.
6651 Optional arguments BEG and END default to `point-min' and `point-max'
6654 If region ends with a newline, ignore it unless optional third argument
6655 COUNT-FINAL-NEWLINE is non-nil.
6657 The optional fourth argument WINDOW specifies the window used for obtaining
6658 parameters such as width, horizontal scrolling, and so on. The default is
6659 to use the selected window's parameters.
6661 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6662 regardless of which buffer is displayed in WINDOW. This makes possible to use
6663 `count-screen-lines' in any buffer, whether or not it is currently displayed
6666 (setq beg
(point-min)))
6668 (setq end
(point-max)))
6674 (narrow-to-region (min beg end
)
6675 (if (and (not count-final-newline
)
6676 (= ?
\n (char-before (max beg end
))))
6679 (goto-char (point-min))
6680 (1+ (vertical-motion (buffer-size) window
))))))
6682 (defun window-buffer-height (window)
6683 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
6684 WINDOW must be a live window and defaults to the selected one."
6685 (setq window
(window-normalize-window window t
))
6686 (with-current-buffer (window-buffer window
)
6688 (count-screen-lines (point-min) (point-max)
6689 ;; If buffer ends with a newline, ignore it when
6690 ;; counting height unless point is after it.
6694 ;;; Resizing windows and frames to fit their contents exactly.
6695 (defcustom fit-window-to-buffer-horizontally nil
6696 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
6697 If this is nil, `fit-window-to-buffer' never resizes windows
6698 horizontally. If this is `only', it can resize windows
6699 horizontally only. Any other value means `fit-window-to-buffer'
6700 can resize windows in both dimensions."
6705 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
6706 ;; counting title bar and outer borders.
6707 (defcustom fit-frame-to-buffer nil
6708 "Non-nil means `fit-frame-to-buffer' can fit a frame to its buffer.
6709 A frame is fit if and only if its root window is a live window
6710 and this option is non-nil. If this is `horizontally', frames
6711 are resized horizontally only. If this is `vertically', frames
6712 are resized vertically only. Any other non-nil value means
6713 frames can be resized in both dimensions. See also
6714 `fit-frame-to-buffer-margins' and `fit-frame-to-buffer-sizes'.
6716 If this is non-nil and a window is the only window of its frame,
6717 `fit-window-to-buffer' will invoke `fit-frame-to-buffer' to fit
6718 the frame to its buffer."
6723 (defcustom fit-frame-to-buffer-margins
'(nil nil nil nil
)
6724 "Margins around frame for `fit-frame-to-buffer'.
6725 This list specifies the numbers of pixels to be left free on the
6726 left, above, the right, and below a frame that shall be fit to
6727 its buffer. The value specified here can be overridden for a
6728 specific frame by that frame's `fit-frame-to-buffer-margins'
6729 parameter, if present.
6731 This variable controls how fitting a frame to the size of its
6732 buffer coordinates with the size of your display. If you don't
6733 specify a value here, the size of the display's workarea is used.
6735 See also `fit-frame-to-buffer-sizes'."
6741 :format
"%[LeftMargin%] %v "
6742 (const :tag
"None" :format
"%t" nil
)
6743 (integer :tag
"Pixels" :size
5))
6747 :format
"%[TopMargin%] %v "
6748 (const :tag
"None" :format
"%t" nil
)
6749 (integer :tag
"Pixels" :size
5))
6753 :format
"%[RightMargin%] %v "
6754 (const :tag
"None" :format
"%t" nil
)
6755 (integer :tag
"Pixels" :size
5))
6759 :format
"%[BottomMargin%] %v "
6760 (const :tag
"None" :format
"%t" nil
)
6761 (integer :tag
"Pixels" :size
5)))
6764 (defcustom fit-frame-to-buffer-sizes
'(nil nil nil nil
)
6765 "Size boundaries of frame for `fit-frame-to-buffer'.
6766 This list specifies the total maximum and minimum lines and
6767 maximum and minimum columns of the root window of any frame that
6768 shall be fit to its buffer. If any of these values is non-nil,
6769 it overrides the corresponding argument of `fit-frame-to-buffer'.
6771 On window systems where the menubar can wrap, fitting a frame to
6772 its buffer may swallow the last line(s). Specifying an
6773 appropriate minimum width value here can avoid such wrapping.
6775 See also `fit-frame-to-buffer-margins'."
6779 :tag
"Maximum Height"
6781 :format
"%[MaxHeight%] %v "
6782 (const :tag
"None" :format
"%t" nil
)
6783 (integer :tag
"Lines" :size
5))
6785 :tag
"Minimum Height"
6787 :format
"%[MinHeight%] %v "
6788 (const :tag
"None" :format
"%t" nil
)
6789 (integer :tag
"Lines" :size
5))
6791 :tag
"Maximum Width"
6793 :format
"%[MaxWidth%] %v "
6794 (const :tag
"None" :format
"%t" nil
)
6795 (integer :tag
"Columns" :size
5))
6797 :tag
"Minimum Width"
6799 :format
"%[MinWidth%] %v\n"
6800 (const :tag
"None" :format
"%t" nil
)
6801 (integer :tag
"Columns" :size
5)))
6804 (declare-function x-display-pixel-height
"xfns.c" (&optional terminal
))
6805 (declare-function tool-bar-lines-needed
"xdisp.c" (&optional frame
))
6807 (defun window--sanitize-margin (margin left right
)
6808 "Return MARGIN if it's a number between LEFT and RIGHT."
6809 (when (and (numberp margin
)
6810 (<= left
(- right margin
)) (<= margin right
))
6813 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width
)
6814 "Adjust size of FRAME to display the contents of its buffer exactly.
6815 FRAME can be any live frame and defaults to the selected one.
6816 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
6817 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
6818 FRAME's root window.
6820 The option `fit-frame-to-buffer' controls whether this function
6821 has any effect. New position and size of FRAME are additionally
6822 determined by the options `fit-frame-to-buffer-sizes' and
6823 `fit-frame-to-buffer-margins' or the corresponding parameters of
6826 (or (fboundp 'x-display-pixel-height
)
6827 (user-error "Cannot resize frame in non-graphic Emacs"))
6828 (setq frame
(window-normalize-frame frame
))
6829 (when (and (window-live-p (frame-root-window frame
))
6831 (or (not window-size-fixed
)
6832 (and (eq window-size-fixed
'height
)
6833 (not (eq fit-frame-to-buffer
'vertically
)))
6834 (and (eq window-size-fixed
'width
)
6835 (not (eq fit-frame-to-buffer
'horizontally
)))))
6836 (with-selected-window (frame-root-window frame
)
6837 (let* ((window (frame-root-window frame
))
6838 (char-width (frame-char-width))
6839 (char-height (frame-char-height))
6840 (monitor-attributes (car (display-monitor-attributes-list
6841 (frame-parameter frame
'display
))))
6842 (geometry (cdr (assq 'geometry monitor-attributes
)))
6843 (display-width (- (nth 2 geometry
) (nth 0 geometry
)))
6844 (display-height (- (nth 3 geometry
) (nth 1 geometry
)))
6845 (workarea (cdr (assq 'workarea monitor-attributes
)))
6847 (margins (or (frame-parameter frame
'fit-frame-to-buffer-margins
)
6848 fit-frame-to-buffer-margins
))
6849 (left-margin (or (window--sanitize-margin
6850 (nth 0 margins
) 0 display-width
)
6852 (top-margin (or (window--sanitize-margin
6853 (nth 1 margins
) 0 display-height
)
6855 (workarea-width (nth 2 workarea
))
6856 (right-margin (or (window--sanitize-margin
6857 (nth 2 margins
) left-margin display-width
)
6858 (+ left-margin workarea-width
)))
6859 (workarea-height (nth 3 workarea
))
6860 (bottom-margin (or (window--sanitize-margin
6861 (nth 3 margins
) top-margin display-height
)
6862 (+ top-margin workarea-height
)))
6863 ;; The pixel width of FRAME (which does not include the
6864 ;; window manager's decorations).
6865 (frame-width (frame-pixel-width))
6866 ;; The pixel width of the body of FRAME's root window.
6867 (window-body-width (window-body-width nil t
))
6868 ;; The difference in pixels between total and body width of
6870 (window-extra-width (- (window-pixel-width) window-body-width
))
6871 ;; The difference in pixels between the frame's pixel width
6872 ;; and the window's body width. This is the space we can't
6874 (extra-width (- frame-width window-body-width
))
6875 ;; The maximum width we can use for fitting.
6876 (fit-width (- workarea-width extra-width
))
6877 ;; The pixel position of FRAME's left border. We usually
6878 ;; try to leave this alone.
6880 (let ((left (frame-parameter nil
'left
)))
6882 (funcall (car left
) (cadr left
))
6884 ;; The pixel height of FRAME (which does not include title
6885 ;; line, decorations, and sometimes neither the menu nor
6887 (frame-height (frame-pixel-height))
6888 ;; The pixel height of FRAME's root window (we don't care
6889 ;; about the window's body height since the return value of
6890 ;; `window-text-pixel-size' includes header and mode line).
6891 (window-height (window-pixel-height))
6892 ;; The difference in pixels between the frame's pixel
6893 ;; height and the window's height.
6894 (extra-height (- frame-height window-height
))
6895 ;; When tool-bar-mode is enabled and we just created a new
6896 ;; frame, reserve lines for toolbar resizing. Needed
6897 ;; because for reasons unknown to me Emacs (1) reserves one
6898 ;; line for the toolbar when making the initial frame and
6899 ;; toolbars are enabled, and (2) later adds the remaining
6900 ;; lines needed. Our code runs IN BETWEEN (1) and (2).
6901 ;; YMMV when you're on a system that behaves differently.
6902 (toolbar-extra-height
6903 (let ((quit-restore (window-parameter window
'quit-restore
))
6904 ;; This may have to change when we allow arbitrary
6905 ;; pixel height toolbars.
6906 (lines (tool-bar-height)))
6908 (if (and quit-restore
(eq (car quit-restore
) 'frame
)
6909 (not (zerop lines
)))
6912 ;; The maximum height we can use for fitting.
6914 (- workarea-height extra-height toolbar-extra-height
))
6915 ;; The pixel position of FRAME's top border. We usually
6916 ;; try to leave this alone.
6918 (let ((top (frame-parameter nil
'top
)))
6920 (funcall (car top
) (cadr top
))
6922 ;; Sanitize minimum and maximum sizes.
6923 (sizes (or (frame-parameter frame
'fit-frame-to-buffer-sizes
)
6924 fit-frame-to-buffer-sizes
))
6927 ((numberp (nth 0 sizes
)) (* (nth 0 sizes
) char-height
))
6928 ((numberp max-height
) (* max-height char-height
))))
6931 ((numberp (nth 1 sizes
)) (* (nth 1 sizes
) char-height
))
6932 ((numberp min-height
) (* min-height char-height
))))
6935 ((numberp (nth 2 sizes
))
6936 (- (* (nth 2 sizes
) char-width
) window-extra-width
))
6937 ((numberp max-width
)
6938 (- (* max-width char-width
) window-extra-width
))))
6941 ((numberp (nth 3 sizes
))
6942 (- (* (nth 3 sizes
) char-width
) window-extra-width
))
6943 ((numberp min-width
)
6944 (- (* min-width char-width
) window-extra-width
))))
6945 ;; Note: Currently, for a new frame the sizes of the header
6946 ;; and mode line may be estimated incorrectly
6947 (value (window-text-pixel-size
6948 nil t t workarea-width workarea-height t
))
6949 (width (+ (car value
) (window-right-divider-width)))
6950 (height (+ (cdr value
) (window-bottom-divider-width)))
6952 (unless window-resize-pixelwise
6953 ;; Round sizes to character sizes.
6954 (setq remainder
(% width char-width
))
6955 (unless (zerop remainder
)
6956 (setq width
(+ width
(- char-width remainder
))))
6957 (setq remainder
(% height char-height
))
6958 (setq height
(+ height
(- char-height remainder
))))
6959 ;; Now make sure that we don't get larger than our rounded
6960 ;; maximum lines and columns.
6961 (when (> width fit-width
)
6962 (setq width
(- fit-width
(% fit-width char-width
))))
6963 (when (> height fit-height
)
6964 (setq height
(- fit-height
(% fit-height char-height
))))
6965 ;; Don't change height or width when the window's size is fixed
6966 ;; in either direction.
6968 ((eq window-size-fixed
'height
)
6970 ((eq window-size-fixed
'width
)
6973 ;; Fit to maximum and minimum widths.
6975 (setq width
(min width max-width
)))
6977 (setq width
(max width min-width
)))
6979 (setq width
(+ width extra-width
))
6980 ;; Preserve right margin.
6981 (let ((right (+ left width extra-width
))
6982 (max-right (- workarea-width right-margin
)))
6984 ((> right max-right
)
6985 ;; Move FRAME to left.
6986 (setq left
(max 0 (- left
(- right max-right
)))))
6987 ((< left left-margin
)
6988 ;; Move frame to right.
6989 (setq left left-margin
)))))
6991 ;; Fit to maximum and minimum heights.
6993 (setq height
(min height max-height
)))
6995 (setq height
(max height min-height
)))
6996 ;; Add extra height.
6997 (setq height
(+ height extra-height
))
6998 ;; Preserve bottom and top margins.
6999 (let ((bottom (+ top height extra-height
))
7000 (max-bottom (- workarea-height bottom-margin
)))
7002 ((> bottom max-bottom
)
7003 ;; Move FRAME to left.
7004 (setq top
(max 0 (- top
(- bottom max-bottom
)))))
7007 (setq top top-margin
)))))
7009 (set-frame-position frame left top
)
7010 ;; Clumsily try to translate our calculations to what
7011 ;; `set-frame-size' wants.
7013 (setq width
(- (+ (frame-text-width) width
)
7014 extra-width window-body-width
)))
7016 (setq height
(- (+ (frame-text-height) height
)
7017 extra-height window-height
)))
7021 (if window-resize-pixelwise
7023 (/ width char-width
))
7026 (if window-resize-pixelwise
7028 (/ height char-height
))
7029 (frame-text-height))
7030 window-resize-pixelwise
)))))
7032 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width
)
7033 "Adjust size of WINDOW to display its buffer's contents exactly.
7034 WINDOW must be a live window and defaults to the selected one.
7036 If WINDOW is part of a vertical combination, adjust WINDOW's
7037 height. The new height is calculated from the number of lines of
7038 the accessible portion of its buffer. The optional argument
7039 MAX-HEIGHT specifies a maximum height and defaults to the height
7040 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
7041 minimum height and defaults to `window-min-height'. Both
7042 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include the
7043 mode line and header line, if any.
7045 If WINDOW is part of a horizontal combination and the value of
7046 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
7047 WINDOW's height. The new width of WINDOW is calculated from the
7048 maximum length of its buffer's lines that follow the current
7049 start position of WINDOW. The optional argument MAX-WIDTH
7050 specifies a maximum width and defaults to the width of WINDOW's
7051 frame. The optional argument MIN-WIDTH specifies a minimum width
7052 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
7053 are specified in columns and include fringes, margins and
7056 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
7057 If WINDOW is its frame's root window, then if the option
7058 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
7059 adjust the frame's size.
7061 Note that even if this function makes WINDOW large enough to show
7062 _all_ parts of its buffer you might not see the first part when
7063 WINDOW was scrolled. If WINDOW is resized horizontally, you will
7064 not see the top of its buffer unless WINDOW starts at its minimum
7065 accessible position."
7067 (setq window
(window-normalize-window window t
))
7068 (if (eq window
(frame-root-window window
))
7069 (when fit-frame-to-buffer
7070 ;; Fit WINDOW's frame to buffer.
7071 (fit-frame-to-buffer
7072 (window-frame window
)
7073 max-height min-height max-width min-width
))
7074 (with-selected-window window
7075 (let* ((pixelwise window-resize-pixelwise
)
7076 (frame (window-frame))
7077 (char-height (frame-char-height))
7078 (char-width (frame-char-width))
7079 (display-height (display-pixel-height))
7080 (total-height (window-size window nil pixelwise
))
7081 (body-height (window-body-height window pixelwise
))
7082 (body-width (window-body-width window pixelwise
))
7084 ;; Sanitize MIN-HEIGHT.
7085 (if (numberp min-height
)
7086 ;; Can't get smaller than `window-safe-min-height'.
7088 (* char-height min-height
)
7091 (window-safe-min-pixel-height window
)
7092 window-safe-min-height
))
7093 ;; Preserve header and mode line if present.
7094 (window-min-size nil nil t pixelwise
)))
7096 ;; Sanitize MAX-HEIGHT.
7097 (if (numberp max-height
)
7101 window nil nil nil nil nil pixelwise
))
7103 (* char-height max-height
)
7105 (+ total-height
(window-max-delta
7106 window nil nil nil nil nil pixelwise
))))
7109 ;; If WINDOW is vertically combined, try to resize it
7111 ((and (not (eq fit-window-to-buffer-horizontally
'only
))
7112 (not (window-size-fixed-p window
))
7113 (window-combined-p))
7114 ;; Vertically we always want to fit the entire buffer.
7115 ;; WINDOW'S height can't get larger than its frame's pixel
7116 ;; height. Its width remains fixed.
7117 (setq height
(+ (cdr (window-text-pixel-size
7118 nil nil t nil
(frame-pixel-height) t
))
7119 (window-bottom-divider-width)))
7122 (setq height
(+ (/ height char-height
)
7123 (if (zerop (% height char-height
)) 0 1))))
7124 (unless (= height total-height
)
7125 (window-resize-no-error
7127 (- (max min-height
(min max-height height
)) total-height
)
7128 nil window pixelwise
)))
7129 ;; If WINDOW is horizontally combined, try to resize it
7131 ((and fit-window-to-buffer-horizontally
7132 (not (window-size-fixed-p window t
))
7133 (window-combined-p nil t
))
7134 (let* ((display-width (display-pixel-width))
7135 (total-width (window-size window nil pixelwise
))
7137 ;; Sanitize MIN-WIDTH.
7138 (if (numberp min-width
)
7139 ;; Can't get smaller than `window-safe-min-width'.
7141 (* char-width min-width
)
7144 (window-safe-min-pixel-width)
7145 window-safe-min-width
))
7146 ;; Preserve fringes, margins, scrollbars if present.
7147 (window-min-size nil nil t pixelwise
)))
7149 ;; Sanitize MAX-WIDTH.
7150 (if (numberp max-width
)
7153 nil t nil nil nil nil pixelwise
))
7155 (* char-width max-width
)
7157 (+ total-width
(window-max-delta
7158 nil t nil nil nil nil pixelwise
))))
7159 ;; When fitting vertically, assume that WINDOW's start
7160 ;; position remains unaltered. WINDOW can't get wider
7161 ;; than its frame's pixel width, its height remains
7163 (width (+ (car (window-text-pixel-size
7164 nil
(window-start) (point-max)
7166 ;; Add one char-height to assure that
7167 ;; we're on the safe side. This
7168 ;; overshoots when the first line below
7169 ;; the bottom is wider than the window.
7171 (if pixelwise char-height
1))))
7172 (window-right-divider-width))))
7174 (setq width
(+ (/ width char-width
)
7175 (if (zerop (% width char-width
)) 0 1))))
7176 (unless (= width body-width
)
7177 (window-resize-no-error
7181 (+ total-width
(- width body-width
))))
7183 t window pixelwise
)))))))))
7185 (defun window-safely-shrinkable-p (&optional window
)
7186 "Return t if WINDOW can be shrunk without shrinking other windows.
7187 WINDOW defaults to the selected window."
7188 (with-selected-window (or window
(selected-window))
7189 (let ((edges (window-edges)))
7190 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
7191 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
7193 (defun shrink-window-if-larger-than-buffer (&optional window
)
7194 "Shrink height of WINDOW if its buffer doesn't need so many lines.
7195 More precisely, shrink WINDOW vertically to be as small as
7196 possible, while still showing the full contents of its buffer.
7197 WINDOW must be a live window and defaults to the selected one.
7199 Do not shrink WINDOW to less than `window-min-height' lines. Do
7200 nothing if the buffer contains more lines than the present window
7201 height, or if some of the window's contents are scrolled out of
7202 view, or if shrinking this window would also shrink another
7203 window, or if the window is the only window of its frame.
7205 Return non-nil if the window was shrunk, nil otherwise."
7207 (setq window
(window-normalize-window window t
))
7208 ;; Make sure that WINDOW is vertically combined and `point-min' is
7209 ;; visible (for whatever reason that's needed). The remaining issues
7210 ;; should be taken care of by `fit-window-to-buffer'.
7211 (when (and (window-combined-p window
)
7212 (pos-visible-in-window-p (point-min) window
))
7213 (fit-window-to-buffer window
(window-total-height window
))))
7215 (defun kill-buffer-and-window ()
7216 "Kill the current buffer and delete the selected window."
7218 (let ((window-to-delete (selected-window))
7219 (buffer-to-kill (current-buffer))
7220 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
7223 (add-hook 'kill-buffer-hook delete-window-hook t t
)
7224 (if (kill-buffer (current-buffer))
7225 ;; If `delete-window' failed before, we rerun it to regenerate
7226 ;; the error so it can be seen in the echo area.
7227 (when (eq (selected-window) window-to-delete
)
7229 ;; If the buffer is not dead for some reason (probably because
7230 ;; of a `quit' signal), remove the hook again.
7232 (with-current-buffer buffer-to-kill
7233 (remove-hook 'kill-buffer-hook delete-window-hook t
))))))
7236 (defvar recenter-last-op nil
7237 "Indicates the last recenter operation performed.
7238 Possible values: `top', `middle', `bottom', integer or float numbers.")
7240 (defcustom recenter-positions
'(middle top bottom
)
7241 "Cycling order for `recenter-top-bottom'.
7242 A list of elements with possible values `top', `middle', `bottom',
7243 integer or float numbers that define the cycling order for
7244 the command `recenter-top-bottom'.
7246 Top and bottom destinations are `scroll-margin' lines from the true
7247 window top and bottom. Middle redraws the frame and centers point
7248 vertically within the window. Integer number moves current line to
7249 the specified absolute window-line. Float number between 0.0 and 1.0
7250 means the percentage of the screen space from the top. The default
7251 cycling order is middle -> top -> bottom."
7252 :type
'(repeat (choice
7253 (const :tag
"Top" top
)
7254 (const :tag
"Middle" middle
)
7255 (const :tag
"Bottom" bottom
)
7256 (integer :tag
"Line number")
7257 (float :tag
"Percentage")))
7261 (defun recenter-top-bottom (&optional arg
)
7262 "Move current buffer line to the specified window line.
7263 With no prefix argument, successive calls place point according
7264 to the cycling order defined by `recenter-positions'.
7266 A prefix argument is handled like `recenter':
7267 With numeric prefix ARG, move current line to window-line ARG.
7268 With plain `C-u', move current line to window center."
7271 (arg (recenter arg
)) ; Always respect ARG.
7273 (setq recenter-last-op
7274 (if (eq this-command last-command
)
7275 (car (or (cdr (member recenter-last-op recenter-positions
))
7276 recenter-positions
))
7277 (car recenter-positions
)))
7278 (let ((this-scroll-margin
7279 (min (max 0 scroll-margin
)
7280 (truncate (/ (window-body-height) 4.0)))))
7281 (cond ((eq recenter-last-op
'middle
)
7283 ((eq recenter-last-op
'top
)
7284 (recenter this-scroll-margin
))
7285 ((eq recenter-last-op
'bottom
)
7286 (recenter (- -
1 this-scroll-margin
)))
7287 ((integerp recenter-last-op
)
7288 (recenter recenter-last-op
))
7289 ((floatp recenter-last-op
)
7290 (recenter (round (* recenter-last-op
(window-height))))))))))
7292 (define-key global-map
[?\C-l
] 'recenter-top-bottom
)
7294 (defun move-to-window-line-top-bottom (&optional arg
)
7295 "Position point relative to window.
7297 With a prefix argument ARG, acts like `move-to-window-line'.
7299 With no argument, positions point at center of window.
7300 Successive calls position point at positions defined
7301 by `recenter-positions'."
7304 (arg (move-to-window-line arg
)) ; Always respect ARG.
7306 (setq recenter-last-op
7307 (if (eq this-command last-command
)
7308 (car (or (cdr (member recenter-last-op recenter-positions
))
7309 recenter-positions
))
7310 (car recenter-positions
)))
7311 (let ((this-scroll-margin
7312 (min (max 0 scroll-margin
)
7313 (truncate (/ (window-body-height) 4.0)))))
7314 (cond ((eq recenter-last-op
'middle
)
7315 (call-interactively 'move-to-window-line
))
7316 ((eq recenter-last-op
'top
)
7317 (move-to-window-line this-scroll-margin
))
7318 ((eq recenter-last-op
'bottom
)
7319 (move-to-window-line (- -
1 this-scroll-margin
)))
7320 ((integerp recenter-last-op
)
7321 (move-to-window-line recenter-last-op
))
7322 ((floatp recenter-last-op
)
7323 (move-to-window-line (round (* recenter-last-op
(window-height))))))))))
7325 (define-key global-map
[?\M-r
] 'move-to-window-line-top-bottom
)
7327 ;;; Scrolling commands.
7329 ;;; Scrolling commands which do not signal errors at top/bottom
7330 ;;; of buffer at first key-press (instead move to top/bottom
7333 (defcustom scroll-error-top-bottom nil
7334 "Move point to top/bottom of buffer before signaling a scrolling error.
7335 A value of nil means just signal an error if no more scrolling possible.
7336 A value of t means point moves to the beginning or the end of the buffer
7337 \(depending on scrolling direction) when no more scrolling possible.
7338 When point is already on that position, then signal an error."
7343 (defun scroll-up-command (&optional arg
)
7344 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
7345 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
7346 scroll window further, move cursor to the bottom line.
7347 When point is already on that position, then signal an error.
7348 A near full screen is `next-screen-context-lines' less than a full screen.
7349 Negative ARG means scroll downward.
7350 If ARG is the atom `-', scroll downward by nearly full screen."
7353 ((null scroll-error-top-bottom
)
7356 (scroll-down-command nil
))
7357 ((< (prefix-numeric-value arg
) 0)
7358 (scroll-down-command (- (prefix-numeric-value arg
))))
7360 (scroll-up arg
)) ; signal error
7366 ;; When scrolling by ARG lines can't be done,
7367 ;; move by ARG lines instead.
7369 ;; When ARG is nil for full-screen scrolling,
7370 ;; move to the bottom of the buffer.
7371 (goto-char (point-max))))))))
7373 (put 'scroll-up-command
'scroll-command t
)
7375 (defun scroll-down-command (&optional arg
)
7376 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
7377 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
7378 scroll window further, move cursor to the top line.
7379 When point is already on that position, then signal an error.
7380 A near full screen is `next-screen-context-lines' less than a full screen.
7381 Negative ARG means scroll upward.
7382 If ARG is the atom `-', scroll upward by nearly full screen."
7385 ((null scroll-error-top-bottom
)
7388 (scroll-up-command nil
))
7389 ((< (prefix-numeric-value arg
) 0)
7390 (scroll-up-command (- (prefix-numeric-value arg
))))
7392 (scroll-down arg
)) ; signal error
7396 (beginning-of-buffer
7398 ;; When scrolling by ARG lines can't be done,
7399 ;; move by ARG lines instead.
7400 (forward-line (- arg
))
7401 ;; When ARG is nil for full-screen scrolling,
7402 ;; move to the top of the buffer.
7403 (goto-char (point-min))))))))
7405 (put 'scroll-down-command
'scroll-command t
)
7407 ;;; Scrolling commands which scroll a line instead of full screen.
7409 (defun scroll-up-line (&optional arg
)
7410 "Scroll text of selected window upward ARG lines; or one line if no ARG.
7411 If ARG is omitted or nil, scroll upward by one line.
7412 This is different from `scroll-up-command' that scrolls a full screen."
7414 (scroll-up (or arg
1)))
7416 (put 'scroll-up-line
'scroll-command t
)
7418 (defun scroll-down-line (&optional arg
)
7419 "Scroll text of selected window down ARG lines; or one line if no ARG.
7420 If ARG is omitted or nil, scroll down by one line.
7421 This is different from `scroll-down-command' that scrolls a full screen."
7423 (scroll-down (or arg
1)))
7425 (put 'scroll-down-line
'scroll-command t
)
7428 (defun scroll-other-window-down (&optional lines
)
7429 "Scroll the \"other window\" down.
7430 For more details, see the documentation for `scroll-other-window'."
7432 (scroll-other-window
7433 ;; Just invert the argument's meaning.
7434 ;; We can do that without knowing which window it will be.
7435 (if (eq lines
'-
) nil
7437 (- (prefix-numeric-value lines
))))))
7439 (defun beginning-of-buffer-other-window (arg)
7440 "Move point to the beginning of the buffer in the other window.
7441 Leave mark at previous position.
7442 With arg N, put point N/10 of the way from the true beginning."
7444 (let ((orig-window (selected-window))
7445 (window (other-window-for-scrolling)))
7446 ;; We use unwind-protect rather than save-window-excursion
7447 ;; because the latter would preserve the things we want to change.
7450 (select-window window
)
7451 ;; Set point and mark in that window's buffer.
7453 (beginning-of-buffer arg
))
7454 ;; Set point accordingly.
7456 (select-window orig-window
))))
7458 (defun end-of-buffer-other-window (arg)
7459 "Move point to the end of the buffer in the other window.
7460 Leave mark at previous position.
7461 With arg N, put point N/10 of the way from the true end."
7463 ;; See beginning-of-buffer-other-window for comments.
7464 (let ((orig-window (selected-window))
7465 (window (other-window-for-scrolling)))
7468 (select-window window
)
7470 (end-of-buffer arg
))
7472 (select-window orig-window
))))
7474 (defvar mouse-autoselect-window-timer nil
7475 "Timer used by delayed window autoselection.")
7477 (defvar mouse-autoselect-window-position nil
7478 "Last mouse position recorded by delayed window autoselection.")
7480 (defvar mouse-autoselect-window-window nil
7481 "Last window recorded by delayed window autoselection.")
7483 (defvar mouse-autoselect-window-state nil
7484 "When non-nil, special state of delayed window autoselection.
7485 Possible values are `suspend' (suspend autoselection after a menu or
7486 scrollbar interaction) and `select' (the next invocation of
7487 `handle-select-window' shall select the window immediately).")
7489 (defun mouse-autoselect-window-cancel (&optional force
)
7490 "Cancel delayed window autoselection.
7491 Optional argument FORCE means cancel unconditionally."
7492 (unless (and (not force
)
7493 ;; Don't cancel for select-window or select-frame events
7494 ;; or when the user drags a scroll bar.
7495 (or (memq this-command
7496 '(handle-select-window handle-switch-frame
))
7497 (and (eq this-command
'scroll-bar-toolkit-scroll
)
7498 (memq (nth 4 (event-end last-input-event
))
7499 '(handle end-scroll
)))))
7500 (setq mouse-autoselect-window-state nil
)
7501 (when (timerp mouse-autoselect-window-timer
)
7502 (cancel-timer mouse-autoselect-window-timer
))
7503 (remove-hook 'pre-command-hook
'mouse-autoselect-window-cancel
)))
7505 (defun mouse-autoselect-window-start (mouse-position &optional window suspend
)
7506 "Start delayed window autoselection.
7507 MOUSE-POSITION is the last position where the mouse was seen as returned
7508 by `mouse-position'. Optional argument WINDOW non-nil denotes the
7509 window where the mouse was seen. Optional argument SUSPEND non-nil
7510 means suspend autoselection."
7511 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
7512 (setq mouse-autoselect-window-position mouse-position
)
7513 (when window
(setq mouse-autoselect-window-window window
))
7514 (setq mouse-autoselect-window-state
(when suspend
'suspend
))
7515 ;; Install timer which runs `mouse-autoselect-window-select' after
7516 ;; `mouse-autoselect-window' seconds.
7517 (setq mouse-autoselect-window-timer
7519 (abs mouse-autoselect-window
) nil
'mouse-autoselect-window-select
)))
7521 (defun mouse-autoselect-window-select ()
7522 "Select window with delayed window autoselection.
7523 If the mouse position has stabilized in a non-selected window, select
7524 that window. The minibuffer window is selected only if the minibuffer
7525 is active. This function is run by `mouse-autoselect-window-timer'."
7527 (let* ((mouse-position (mouse-position))
7530 (window-at (cadr mouse-position
) (cddr mouse-position
)
7531 (car mouse-position
)))))
7533 ((or (and (fboundp 'menu-or-popup-active-p
) (menu-or-popup-active-p))
7535 (let ((coords (coordinates-in-window-p
7536 (cdr mouse-position
) window
)))
7537 (and (not (consp coords
))
7538 (not (memq coords
'(left-margin right-margin
)))))))
7539 ;; A menu / popup dialog is active or the mouse is not on the
7540 ;; text region of WINDOW: Suspend autoselection temporarily.
7541 (mouse-autoselect-window-start mouse-position nil t
))
7542 ((eq mouse-autoselect-window-state
'suspend
)
7543 ;; Delayed autoselection was temporarily suspended, reenable it.
7544 (mouse-autoselect-window-start mouse-position
))
7545 ((and window
(not (eq window
(selected-window)))
7546 (or (not (numberp mouse-autoselect-window
))
7547 (and (> mouse-autoselect-window
0)
7548 ;; If `mouse-autoselect-window' is positive, select
7549 ;; window if the window is the same as before.
7550 (eq window mouse-autoselect-window-window
))
7551 ;; Otherwise select window if the mouse is at the same
7552 ;; position as before. Observe that the first test after
7553 ;; starting autoselection usually fails since the value of
7554 ;; `mouse-autoselect-window-position' recorded there is the
7555 ;; position where the mouse has entered the new window and
7556 ;; not necessarily where the mouse has stopped moving.
7557 (equal mouse-position mouse-autoselect-window-position
))
7558 ;; The minibuffer is a candidate window if it's active.
7559 (or (not (window-minibuffer-p window
))
7560 (eq window
(active-minibuffer-window))))
7561 ;; Mouse position has stabilized in non-selected window: Cancel
7562 ;; delayed autoselection and try to select that window.
7563 (mouse-autoselect-window-cancel t
)
7564 ;; Select window where mouse appears unless the selected window is the
7565 ;; minibuffer. Use `unread-command-events' in order to execute pre-
7566 ;; and post-command hooks and trigger idle timers. To avoid delaying
7567 ;; autoselection again, set `mouse-autoselect-window-state'."
7568 (unless (window-minibuffer-p)
7569 (setq mouse-autoselect-window-state
'select
)
7570 (setq unread-command-events
7571 (cons (list 'select-window
(list window
))
7572 unread-command-events
))))
7573 ((or (and window
(eq window
(selected-window)))
7574 (not (numberp mouse-autoselect-window
))
7575 (equal mouse-position mouse-autoselect-window-position
))
7576 ;; Mouse position has either stabilized in the selected window or at
7577 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
7578 (mouse-autoselect-window-cancel t
))
7580 ;; Mouse position has not stabilized yet, resume delayed
7582 (mouse-autoselect-window-start mouse-position window
))))))
7584 (defun handle-select-window (event)
7585 "Handle select-window events."
7587 (let ((window (posn-window (event-start event
))))
7588 (unless (or (not (window-live-p window
))
7589 ;; Don't switch if we're currently in the minibuffer.
7590 ;; This tries to work around problems where the
7591 ;; minibuffer gets unselected unexpectedly, and where
7592 ;; you then have to move your mouse all the way down to
7593 ;; the minibuffer to select it.
7594 (window-minibuffer-p)
7595 ;; Don't switch to minibuffer window unless it's active.
7596 (and (window-minibuffer-p window
)
7597 (not (minibuffer-window-active-p window
)))
7598 ;; Don't switch when autoselection shall be delayed.
7599 (and (numberp mouse-autoselect-window
)
7600 (not (zerop mouse-autoselect-window
))
7601 (not (eq mouse-autoselect-window-state
'select
))
7603 ;; Cancel any delayed autoselection.
7604 (mouse-autoselect-window-cancel t
)
7605 ;; Start delayed autoselection from current mouse
7606 ;; position and window.
7607 (mouse-autoselect-window-start (mouse-position) window
)
7608 ;; Executing a command cancels delayed autoselection.
7610 'pre-command-hook
'mouse-autoselect-window-cancel
))))
7611 (when mouse-autoselect-window
7612 ;; Reset state of delayed autoselection.
7613 (setq mouse-autoselect-window-state nil
)
7614 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
7615 (run-hooks 'mouse-leave-buffer-hook
))
7618 (select-window window
))))
7620 (defun truncated-partial-width-window-p (&optional window
)
7621 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
7622 WINDOW must be a live window and defaults to the selected one.
7623 Return nil if WINDOW is not a partial-width window
7624 (regardless of the value of `truncate-lines').
7625 Otherwise, consult the value of `truncate-partial-width-windows'
7626 for the buffer shown in WINDOW."
7627 (setq window
(window-normalize-window window t
))
7628 (unless (window-full-width-p window
)
7629 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
7630 (window-buffer window
))))
7631 (if (integerp t-p-w-w
)
7632 (< (window-width window
) t-p-w-w
)
7635 ;; Some of these are in tutorial--default-keys, so update that if you
7637 (define-key ctl-x-map
"0" 'delete-window
)
7638 (define-key ctl-x-map
"1" 'delete-other-windows
)
7639 (define-key ctl-x-map
"2" 'split-window-below
)
7640 (define-key ctl-x-map
"3" 'split-window-right
)
7641 (define-key ctl-x-map
"o" 'other-window
)
7642 (define-key ctl-x-map
"^" 'enlarge-window
)
7643 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
7644 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
7645 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
7646 (define-key ctl-x-map
"+" 'balance-windows
)
7647 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
7649 ;;; window.el ends here