1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2012
4 ;; Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Window tree functions.
31 (defmacro save-selected-window
(&rest body
)
32 "Execute BODY, then select the previously selected window.
33 The value returned is the value of the last form in BODY.
35 This macro saves and restores the selected window, as well as the
36 selected window in each frame. If the previously selected window
37 is no longer live, then whatever window is selected at the end of
38 BODY remains selected. If the previously selected window of some
39 frame is no longer live at the end of BODY, that frame's selected
42 This macro saves and restores the current buffer, since otherwise
43 its normal operation could make a different buffer current. The
44 order of recently selected windows and the buffer list ordering
45 are not altered by this macro (unless they are altered in BODY)."
46 (declare (indent 0) (debug t
))
47 `(let ((save-selected-window-window (selected-window))
48 ;; We save and restore all frames' selected windows, because
49 ;; `select-window' can change the frame-selected-window of
50 ;; whatever frame that window is in. Each text terminal's
51 ;; top-frame is preserved by putting it last in the list.
52 (save-selected-window-alist
54 (mapcar (lambda (terminal)
55 (let ((frames (frames-on-display-list terminal
))
56 (top-frame (tty-top-frame terminal
))
61 (delq top-frame frames
))))
63 (push (cons f
(frame-selected-window f
))
69 (dolist (elt save-selected-window-alist
)
70 (and (frame-live-p (car elt
))
71 (window-live-p (cdr elt
))
72 (set-frame-selected-window (car elt
) (cdr elt
) 'norecord
)))
73 (when (window-live-p save-selected-window-window
)
74 (select-window save-selected-window-window
'norecord
))))))
76 (defvar temp-buffer-window-setup-hook nil
77 "Normal hook run by `with-temp-buffer-window' before buffer display.
78 This hook is run by `with-temp-buffer-window' with the buffer to be
81 (defvar temp-buffer-window-show-hook nil
82 "Normal hook run by `with-temp-buffer-window' after buffer display.
83 This hook is run by `with-temp-buffer-window' with the buffer
84 displayed and current and its window selected.")
86 (defun temp-buffer-window-setup (buffer-or-name)
87 "Set up temporary buffer specified by BUFFER-OR-NAME
89 (let ((old-dir default-directory
)
90 (buffer (get-buffer-create buffer-or-name
)))
91 (with-current-buffer buffer
92 (kill-all-local-variables)
93 (setq default-directory old-dir
)
95 (setq buffer-read-only nil
)
96 (setq buffer-file-name nil
)
97 (setq buffer-undo-list t
)
98 (let ((inhibit-read-only t
)
99 (inhibit-modification-hooks t
))
101 (run-hooks 'temp-buffer-window-setup-hook
))
102 ;; Return the buffer.
105 (defun temp-buffer-window-show (&optional buffer action
)
106 "Show temporary buffer BUFFER in a window.
107 Return the window showing BUFFER. Pass ACTION as action argument
108 to `display-buffer'."
110 (with-current-buffer buffer
111 (set-buffer-modified-p nil
)
112 (setq buffer-read-only t
)
113 (goto-char (point-min))
114 (when (setq window
(display-buffer buffer action
))
115 (setq frame
(window-frame window
))
116 (unless (eq frame
(selected-frame))
118 (setq minibuffer-scroll-window window
)
119 (set-window-hscroll window
0)
120 (with-selected-window window
121 (run-hooks 'temp-buffer-window-show-hook
)
122 (when temp-buffer-resize-mode
123 (resize-temp-buffer-window window
)))
124 ;; Return the window.
127 (defmacro with-temp-buffer-window
(buffer-or-name action quit-function
&rest body
)
128 "Evaluate BODY and display buffer specified by BUFFER-OR-NAME.
129 BUFFER-OR-NAME must specify either a live buffer or the name of a
130 buffer. If no buffer with such a name exists, create one.
132 Make sure the specified buffer is empty before evaluating BODY.
133 Do not make that buffer current for BODY. Instead, bind
134 `standard-output' to that buffer, so that output generated with
135 `prin1' and similar functions in BODY goes into that buffer.
137 After evaluating BODY, mark the specified buffer unmodified and
138 read-only, and display it in a window via `display-buffer'. Pass
139 ACTION as action argument to `display-buffer'. Automatically
140 shrink the window used if `temp-buffer-resize-mode' is enabled.
142 Return the value returned by BODY unless QUIT-FUNCTION specifies
143 a function. In that case, run the function with two arguments -
144 the window showing the specified buffer and the value returned by
145 BODY - and return the value returned by that function.
147 If the buffer is displayed on a new frame, the window manager may
148 decide to select that frame. In that case, it's usually a good
149 strategy if the function specified by QUIT-FUNCTION selects the
150 window showing the buffer before reading a value from the
151 minibuffer, for example, when asking a `yes-or-no-p' question.
153 This construct is similar to `with-output-to-temp-buffer' but
154 does neither put the buffer in help mode nor does it call
155 `temp-buffer-show-function'. It also runs different hooks,
156 namely `temp-buffer-window-setup-hook' (with the specified buffer
157 current) and `temp-buffer-window-show-hook' (with the specified
158 buffer current and the window showing it selected).
160 Since this macro calls `display-buffer', the window displaying
161 the buffer is usually not selected and the specified buffer
162 usually not made current. QUIT-FUNCTION can override that."
164 (let ((buffer (make-symbol "buffer"))
165 (window (make-symbol "window"))
166 (value (make-symbol "value")))
167 `(let* ((,buffer
(temp-buffer-window-setup ,buffer-or-name
))
168 (standard-output ,buffer
)
170 (with-current-buffer ,buffer
171 (setq ,value
(progn ,@body
))
172 (setq ,window
(temp-buffer-window-show ,buffer
,action
)))
174 (if (functionp ,quit-function
)
175 (funcall ,quit-function
,window
,value
)
178 ;; The following two functions are like `window-next-sibling' and
179 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
180 ;; they don't substitute the selected window for nil), and they return
181 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
182 ;; a minibuffer window).
183 (defun window-right (window)
184 "Return WINDOW's right sibling.
185 Return nil if WINDOW is the root window of its frame. WINDOW can
187 (and window
(window-parent window
) (window-next-sibling window
)))
189 (defun window-left (window)
190 "Return WINDOW's left sibling.
191 Return nil if WINDOW is the root window of its frame. WINDOW can
193 (and window
(window-parent window
) (window-prev-sibling window
)))
195 (defun window-child (window)
196 "Return WINDOW's first child window.
197 WINDOW can be any window."
198 (or (window-top-child window
) (window-left-child window
)))
200 (defun window-child-count (window)
201 "Return number of WINDOW's child windows.
202 WINDOW can be any window."
204 (when (and (windowp window
) (setq window
(window-child window
)))
206 (setq count
(1+ count
))
207 (setq window
(window-next-sibling window
))))
210 (defun window-last-child (window)
211 "Return last child window of WINDOW.
212 WINDOW can be any window."
213 (when (and (windowp window
) (setq window
(window-child window
)))
214 (while (window-next-sibling window
)
215 (setq window
(window-next-sibling window
))))
218 (defun window-normalize-buffer (buffer-or-name)
219 "Return buffer specified by BUFFER-OR-NAME.
220 BUFFER-OR-NAME must be either a buffer or a string naming a live
221 buffer and defaults to the current buffer."
223 ((not buffer-or-name
)
225 ((bufferp buffer-or-name
)
226 (if (buffer-live-p buffer-or-name
)
228 (error "Buffer %s is not a live buffer" buffer-or-name
)))
229 ((get-buffer buffer-or-name
))
231 (error "No such buffer %s" buffer-or-name
))))
233 (defun window-normalize-frame (frame)
234 "Return frame specified by FRAME.
235 FRAME must be a live frame and defaults to the selected frame."
237 (if (frame-live-p frame
)
239 (error "%s is not a live frame" frame
))
242 (defun window-normalize-window (window &optional live-only
)
243 "Return the window specified by WINDOW.
244 If WINDOW is nil, return the selected window. Otherwise, if
245 WINDOW is a live or an internal window, return WINDOW; if
246 LIVE-ONLY is non-nil, return WINDOW for a live window only.
247 Otherwise, signal an error."
252 (if (window-live-p window
)
254 (error "%s is not a live window" window
)))
255 ((window-valid-p window
)
258 (error "%s is not a valid window" window
))))
260 (defvar ignore-window-parameters nil
261 "If non-nil, standard functions ignore window parameters.
262 The functions currently affected by this are `split-window',
263 `delete-window', `delete-other-windows' and `other-window'.
265 An application may bind this to a non-nil value around calls to
266 these functions to inhibit processing of window parameters.")
268 (defconst window-safe-min-height
1
269 "The absolute minimum number of lines of a window.
270 Anything less might crash Emacs.")
272 (defcustom window-min-height
4
273 "The minimum number of lines of any window.
274 The value has to accommodate a mode- or header-line if present.
275 A value less than `window-safe-min-height' is ignored. The value
276 of this variable is honored when windows are resized or split.
278 Applications should never rebind this variable. To resize a
279 window to a height less than the one specified here, an
280 application should instead call `window-resize' with a non-nil
281 IGNORE argument. In order to have `split-window' make a window
282 shorter, explicitly specify the SIZE argument of that function."
287 (defconst window-safe-min-width
2
288 "The absolute minimum number of columns of a window.
289 Anything less might crash Emacs.")
291 (defcustom window-min-width
10
292 "The minimum number of columns of any window.
293 The value has to accommodate margins, fringes, or scrollbars if
294 present. A value less than `window-safe-min-width' is ignored.
295 The value of this variable is honored when windows are resized or
298 Applications should never rebind this variable. To resize a
299 window to a width less than the one specified here, an
300 application should instead call `window-resize' with a non-nil
301 IGNORE argument. In order to have `split-window' make a window
302 narrower, explicitly specify the SIZE argument of that function."
307 (defun window-combined-p (&optional window horizontal
)
308 "Return non-nil if WINDOW has siblings in a given direction.
309 WINDOW must be a valid window and defaults to the selected one.
311 HORIZONTAL determines a direction for the window combination.
312 If HORIZONTAL is omitted or nil, return non-nil if WINDOW is part
313 of a vertical window combination.
314 If HORIZONTAL is non-nil, return non-nil if WINDOW is part of a
315 horizontal window combination."
316 (setq window
(window-normalize-window window
))
317 (let ((parent (window-parent window
)))
320 (window-left-child parent
)
321 (window-top-child parent
)))))
323 (defun window-combinations (window &optional horizontal
)
324 "Return largest number of windows vertically arranged within WINDOW.
325 WINDOW must be a valid window and defaults to the selected one.
326 If HORIZONTAL is non-nil, return the largest number of
327 windows horizontally arranged within WINDOW."
328 (setq window
(window-normalize-window window
))
330 ((window-live-p window
)
331 ;; If WINDOW is live, return 1.
334 (window-left-child window
)
335 (window-top-child window
))
336 ;; If WINDOW is iso-combined, return the sum of the values for all
337 ;; child windows of WINDOW.
338 (let ((child (window-child window
))
342 (+ (window-combinations child horizontal
)
344 (setq child
(window-right child
)))
347 ;; If WINDOW is not iso-combined, return the maximum value of any
348 ;; child window of WINDOW.
349 (let ((child (window-child window
))
353 (max (window-combinations child horizontal
)
355 (setq child
(window-right child
)))
358 (defun walk-window-tree-1 (fun walk-window-tree-window any
&optional sub-only
)
359 "Helper function for `walk-window-tree' and `walk-window-subtree'."
360 (let (walk-window-tree-buffer)
361 (while walk-window-tree-window
362 (setq walk-window-tree-buffer
363 (window-buffer walk-window-tree-window
))
364 (when (or walk-window-tree-buffer any
)
365 (funcall fun walk-window-tree-window
))
366 (unless walk-window-tree-buffer
368 fun
(window-left-child walk-window-tree-window
) any
)
370 fun
(window-top-child walk-window-tree-window
) any
))
372 (setq walk-window-tree-window nil
)
373 (setq walk-window-tree-window
374 (window-right walk-window-tree-window
))))))
376 (defun walk-window-tree (fun &optional frame any minibuf
)
377 "Run function FUN on each live window of FRAME.
378 FUN must be a function with one argument - a window. FRAME must
379 be a live frame and defaults to the selected one. ANY, if
380 non-nil, means to run FUN on all live and internal windows of
383 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
384 window even if it isn't active. MINIBUF nil or omitted means run
385 FUN on FRAME's minibuffer window only if it's active. In both
386 cases the minibuffer window must be part of FRAME. MINIBUF
387 neither nil nor t means never run FUN on the minibuffer window.
389 This function performs a pre-order, depth-first traversal of the
390 window tree. If FUN changes the window tree, the result is
392 (setq frame
(window-normalize-frame frame
))
393 (walk-window-tree-1 fun
(frame-root-window frame
) any
)
394 (when (memq minibuf
'(nil t
))
395 ;; Run FUN on FRAME's minibuffer window if requested.
396 (let ((minibuffer-window (minibuffer-window frame
)))
397 (when (and (window-live-p minibuffer-window
)
398 (eq (window-frame minibuffer-window
) frame
)
400 (minibuffer-window-active-p minibuffer-window
)))
401 (funcall fun minibuffer-window
)))))
403 (defun walk-window-subtree (fun &optional window any
)
404 "Run function FUN on the subtree of windows rooted at WINDOW.
405 WINDOW defaults to the selected window. FUN must be a function
406 with one argument - a window. By default, run FUN only on live
407 windows of the subtree. If the optional argument ANY is non-nil,
408 run FUN on all live and internal windows of the subtree. If
409 WINDOW is live, run FUN on WINDOW only.
411 This function performs a pre-order, depth-first traversal of the
412 subtree rooted at WINDOW. If FUN changes that tree, the result
414 (setq window
(window-normalize-window window
))
415 (walk-window-tree-1 fun window any t
))
417 (defun window-with-parameter (parameter &optional value frame any minibuf
)
418 "Return first window on FRAME with PARAMETER non-nil.
419 FRAME defaults to the selected frame. Optional argument VALUE
420 non-nil means only return a window whose window-parameter value
421 for PARAMETER equals VALUE (comparison is done with `equal').
422 Optional argument ANY non-nil means consider internal windows
425 Optional argument MINIBUF t means consider FRAME's minibuffer
426 window even if it isn't active. MINIBUF nil or omitted means
427 consider FRAME's minibuffer window only if it's active. In both
428 cases the minibuffer window must be part of FRAME. MINIBUF
429 neither nil nor t means never consider the minibuffer window."
434 (when (and (setq this-value
(window-parameter window parameter
))
435 (or (not value
) (equal value this-value
)))
436 (throw 'found window
)))
437 frame any minibuf
))))
440 (defun window-atom-root (&optional window
)
441 "Return root of atomic window WINDOW is a part of.
442 WINDOW must be a valid window and defaults to the selected one.
443 Return nil if WINDOW is not part of an atomic window."
444 (setq window
(window-normalize-window window
))
446 (while (and window
(window-parameter window
'window-atom
))
448 (setq window
(window-parent window
)))
451 (defun window-make-atom (window)
452 "Make WINDOW an atomic window.
453 WINDOW must be an internal window. Return WINDOW."
454 (if (not (window-child window
))
455 (error "Window %s is not an internal window" window
)
458 (set-window-parameter window
'window-atom t
))
462 (defun display-buffer-in-atom-window (buffer alist
)
463 "Display BUFFER in an atomic window.
464 This function displays BUFFER in a new window that will be
465 combined with an existing window to form an atomic window. If
466 the existing window is already part of an atomic window, add the
467 new window to that atomic window. Operations like `split-window'
468 or `delete-window', when applied to a constituent of an atomic
469 window, are applied atomically to the root of that atomic window.
471 ALIST is an association list of symbols and values. The
472 following symbols can be used.
474 `window' specifies the existing window the new window shall be
475 combined with. Use `window-atom-root' to make the new window a
476 sibling of an atomic window's root. If an internal window is
477 specified here, all children of that window become part of the
478 atomic window too. If no window is specified, the new window
479 becomes a sibling of the selected window.
481 `side' denotes the side of the existing window where the new
482 window shall be located. Valid values are `below', `right',
483 `above' and `left'. The default is `below'.
485 The return value is the new window, nil when creating that window
487 (let ((ignore-window-parameters t
)
488 (window-combination-limit t
)
489 (window (cdr (assq 'window alist
)))
490 (side (cdr (assq 'side alist
)))
492 (setq window
(window-normalize-window window
))
493 ;; Split off new window
494 (when (setq new
(split-window window nil side
))
495 ;; Make sure we have a valid atomic window.
496 (window-make-atom (window-parent window
))
497 ;; Display BUFFER in NEW and return NEW.
498 (window--display-buffer
499 buffer new
'window display-buffer-mark-dedicated
))))
501 (defun window--atom-check-1 (window)
502 "Subroutine of `window--atom-check'."
504 (if (window-parameter window
'window-atom
)
506 (when (or (catch 'reset
509 (if (window-parameter window
'window-atom
)
510 (setq count
(1+ count
))
513 ;; count >= 1 must hold here. If there's no other
514 ;; window around dissolve this atomic window.
516 ;; Dissolve atomic window.
519 (set-window-parameter window
'window-atom nil
))
522 (unless (window-buffer window
)
523 (window--atom-check-1 (window-left-child window
))
524 (window--atom-check-1 (window-top-child window
))))
525 ;; Check right sibling
526 (window--atom-check-1 (window-right window
))))
528 (defun window--atom-check (&optional frame
)
529 "Check atomicity of all windows on FRAME.
530 FRAME defaults to the selected frame. If an atomic window is
531 wrongly configured, reset the atomicity of all its windows on
532 FRAME to nil. An atomic window is wrongly configured if it has
533 no child windows or one of its child windows is not atomic."
534 (window--atom-check-1 (frame-root-window frame
)))
537 (defvar window-sides
'(left top right bottom
)
540 (defcustom window-sides-vertical nil
541 "If non-nil, left and right side windows are full height.
542 Otherwise, top and bottom side windows are full width."
547 (defcustom window-sides-slots
'(nil nil nil nil
)
548 "Maximum number of side window slots.
549 The value is a list of four elements specifying the number of
550 side window slots on (in this order) the left, top, right and
551 bottom side of each frame. If an element is a number, this means
552 to display at most that many side windows on the corresponding
553 side. If an element is nil, this means there's no bound on the
554 number of slots on that side."
559 :value
(nil nil nil nil
)
562 :help-echo
"Maximum slots of left side window."
564 :format
"%[Left%] %v\n"
565 (const :tag
"Unlimited" :format
"%t" nil
)
566 (integer :tag
"Number" :value
2 :size
5))
569 :help-echo
"Maximum slots of top side window."
571 :format
"%[Top%] %v\n"
572 (const :tag
"Unlimited" :format
"%t" nil
)
573 (integer :tag
"Number" :value
3 :size
5))
576 :help-echo
"Maximum slots of right side window."
578 :format
"%[Right%] %v\n"
579 (const :tag
"Unlimited" :format
"%t" nil
)
580 (integer :tag
"Number" :value
2 :size
5))
583 :help-echo
"Maximum slots of bottom side window."
585 :format
"%[Bottom%] %v\n"
586 (const :tag
"Unlimited" :format
"%t" nil
)
587 (integer :tag
"Number" :value
3 :size
5)))
590 (defun window--major-non-side-window (&optional frame
)
591 "Return the major non-side window of frame FRAME.
592 The optional argument FRAME must be a live frame and defaults to
595 If FRAME has at least one side window, the major non-side window
596 is either an internal non-side window such that all other
597 non-side windows on FRAME descend from it, or the single live
598 non-side window of FRAME. If FRAME has no side windows, return
600 (let ((frame (window-normalize-frame frame
))
602 ;; Set major to the _last_ window found by `walk-window-tree' that
603 ;; is not a side window but has a side window as its sibling.
606 (and (not (window-parameter window
'window-side
))
607 (or (and (setq sibling
(window-prev-sibling window
))
608 (window-parameter sibling
'window-side
))
609 (and (setq sibling
(window-next-sibling window
))
610 (window-parameter sibling
'window-side
)))
611 (setq major window
)))
613 (or major
(frame-root-window frame
))))
615 (defun window--major-side-window (side)
616 "Return major side window on SIDE.
617 SIDE must be one of the symbols `left', `top', `right' or
618 `bottom'. Return nil if no such window exists."
619 (let ((root (frame-root-window))
621 ;; (1) If a window on the opposite side exists, return that window's
623 ;; (2) If the new window shall span the entire side, return the
624 ;; frame's root window.
625 ;; (3) If a window on an orthogonal side exists, return that
627 ;; (4) Otherwise return the frame's root window.
629 ((or (and (eq side
'left
)
630 (setq window
(window-with-parameter 'window-side
'right nil t
)))
632 (setq window
(window-with-parameter 'window-side
'bottom nil t
))))
633 (window-prev-sibling window
))
634 ((or (and (eq side
'right
)
635 (setq window
(window-with-parameter 'window-side
'left nil t
)))
636 (and (eq side
'bottom
)
637 (setq window
(window-with-parameter 'window-side
'top nil t
))))
638 (window-next-sibling window
))
639 ((memq side
'(left right
))
641 (window-sides-vertical
643 ((setq window
(window-with-parameter 'window-side
'top nil t
))
644 (window-next-sibling window
))
645 ((setq window
(window-with-parameter 'window-side
'bottom nil t
))
646 (window-prev-sibling window
))
648 ((memq side
'(top bottom
))
650 ((not window-sides-vertical
)
652 ((setq window
(window-with-parameter 'window-side
'left nil t
))
653 (window-next-sibling window
))
654 ((setq window
(window-with-parameter 'window-side
'right nil t
))
655 (window-prev-sibling window
))
658 (defun display-buffer-in-major-side-window (buffer side slot
&optional alist
)
659 "Display BUFFER in a new window on SIDE of the selected frame.
660 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
661 specifies the slot to use. ALIST is an association list of
662 symbols and values as passed to `display-buffer-in-side-window'.
663 This function may be called only if no window on SIDE exists yet.
664 The new window automatically becomes the \"major\" side window on
665 SIDE. Return the new window, nil if its creation window failed."
666 (let* ((root (frame-root-window))
667 (left-or-right (memq side
'(left right
)))
668 (size (or (assq 'size alist
)
669 (/ (window-total-size (frame-root-window) left-or-right
)
670 ;; By default use a fourth of the size of the
671 ;; frame's root window. This has to be made
672 ;; customizable via ALIST.
674 (major (window--major-side-window side
))
675 (selected-window (selected-window))
677 ((eq side
'top
) 'above
)
678 ((eq side
'bottom
) 'below
)
680 ;; The following two bindings will tell `split-window' to take
681 ;; the space for the new window from `major' and not make a new
682 ;; parent window unless needed.
683 (window-combination-resize 'side
)
684 (window-combination-limit nil
)
685 (new (split-window major
(- size
) on-side
))
688 ;; Initialize `window-side' parameter of new window to SIDE.
689 (set-window-parameter new
'window-side side
)
690 ;; Install `window-slot' parameter of new window.
691 (set-window-parameter new
'window-slot slot
)
692 ;; Install `delete-window' parameter thus making sure that when
693 ;; the new window is deleted, a side window on the opposite side
694 ;; does not get resized.
695 (set-window-parameter new
'delete-window
'delete-side-window
)
696 ;; Install BUFFER in new window and return NEW.
697 (window--display-buffer buffer new
'window
'side
))))
699 (defun delete-side-window (window)
700 "Delete side window WINDOW."
701 (let ((window-combination-resize
702 (window-parameter (window-parent window
) 'window-side
))
703 (ignore-window-parameters t
))
704 (delete-window window
)))
706 (defun display-buffer-in-side-window (buffer alist
)
707 "Display BUFFER in a window on side SIDE of the selected frame.
708 ALIST is an association list of symbols and values. The
709 following symbols can be used:
711 `side' denotes the side of the existing window where the new
712 window shall be located. Valid values are `bottom', `right',
713 `top' and `left'. The default is `bottom'.
715 `slot' if non-nil, specifies the window slot where to display
716 BUFFER. A value of zero or nil means use the middle slot on
717 the specified side. A negative value means use a slot
718 preceding (that is, above or on the left of) the middle slot.
719 A positive value means use a slot following (that is, below or
720 on the right of) the middle slot. The default is zero."
721 (let ((side (or (cdr (assq 'side alist
)) 'bottom
))
722 (slot (or (cdr (assq 'slot alist
)) 0))
725 ((not (memq side
'(top bottom left right
)))
726 (error "Invalid side %s specified" side
))
727 ((not (numberp slot
))
728 (error "Invalid slot %s specified" slot
)))
730 (let* ((major (window-with-parameter 'window-side side nil t
))
731 ;; `major' is the major window on SIDE, `windows' the list of
732 ;; life windows on SIDE.
738 (when (eq (window-parameter window
'window-side
) side
)
739 (setq windows
(cons window windows
)))))
740 (nreverse windows
))))
741 (slots (when major
(max 1 (window-child-count major
))))
747 ((eq side
'bottom
) 3))
749 (selected-window (selected-window))
750 window this-window this-slot prev-window next-window
751 best-window best-slot abs-slot new-window
)
754 ((and (numberp max-slots
) (<= max-slots
0))
755 ;; No side-slots available on this side. Don't create an error,
759 ;; No major window exists on this side, make one.
760 (display-buffer-in-major-side-window buffer side slot alist
))
762 ;; Scan windows on SIDE.
764 (dolist (window windows
)
765 (setq this-slot
(window-parameter window
'window-slot
))
767 ;; The following should not happen and probably be checked
768 ;; by window--side-check.
769 ((not (numberp this-slot
)))
771 ;; A window with a matching slot has been found.
772 (setq this-window window
)
775 ;; Check if this window has a better slot value wrt the
776 ;; slot of the window we want.
778 (if (or (and (> this-slot
0) (> slot
0))
779 (and (< this-slot
0) (< slot
0)))
780 (abs (- slot this-slot
))
781 (+ (abs slot
) (abs this-slot
))))
782 (unless (and best-slot
(<= best-slot abs-slot
))
783 (setq best-window window
)
784 (setq best-slot abs-slot
))
787 (setq prev-window window
))
789 (setq next-window window
)))))))
791 ;; `this-window' is the first window with the same SLOT.
792 ;; `prev-window' is the window with the largest slot < SLOT. A new
793 ;; window will be created after it.
794 ;; `next-window' is the window with the smallest slot > SLOT. A new
795 ;; window will be created before it.
796 ;; `best-window' is the window with the smallest absolute difference
797 ;; of its slot and SLOT.
799 ;; Note: We dedicate the window used softly to its buffer to
800 ;; avoid that "other" (non-side) buffer display functions steal
801 ;; it from us. This must eventually become customizable via
802 ;; ALIST (or, better, avoided in the "other" functions).
804 ;; Reuse `this-window'.
805 (window--display-buffer buffer this-window
'reuse
'side
))
806 (and (or (not max-slots
) (< slots max-slots
))
808 ;; Make new window before `next-window'.
810 (if (memq side
'(left right
)) 'above
'left
))
811 (window-combination-resize 'side
))
812 (setq window
(split-window next-window nil next-side
))
813 ;; When the new window is deleted, its space
814 ;; is returned to other side windows.
815 (set-window-parameter
816 window
'delete-window
'delete-side-window
)
819 ;; Make new window after `prev-window'.
821 (if (memq side
'(left right
)) 'below
'right
))
822 (window-combination-resize 'side
))
823 (setq window
(split-window prev-window nil prev-side
))
824 ;; When the new window is deleted, its space
825 ;; is returned to other side windows.
826 (set-window-parameter
827 window
'delete-window
'delete-side-window
)
829 (set-window-parameter window
'window-slot slot
)
830 (window--display-buffer buffer window
'window
'side
))
832 ;; Reuse `best-window'.
834 ;; Give best-window the new slot value.
835 (set-window-parameter best-window
'window-slot slot
)
836 (window--display-buffer buffer best-window
'reuse
'side
)))))))))
838 (defun window--side-check (&optional frame
)
839 "Check the side window configuration of FRAME.
840 FRAME defaults to the selected frame.
842 A valid side window configuration preserves the following two
845 - If there exists a window whose window-side parameter is
846 non-nil, there must exist at least one live window whose
847 window-side parameter is nil.
849 - If a window W has a non-nil window-side parameter (i) it must
850 have a parent window and that parent's window-side parameter
851 must be either nil or the same as for W, and (ii) any child
852 window of W must have the same window-side parameter as W.
854 If the configuration is invalid, reset the window-side parameters
855 of all windows on FRAME to nil."
856 (let (left top right bottom none side parent parent-side
)
857 (when (or (catch 'reset
860 (setq side
(window-parameter window
'window-side
))
861 (setq parent
(window-parent window
))
863 (and parent
(window-parameter parent
'window-side
)))
864 ;; The following `cond' seems a bit tedious, but I'd
865 ;; rather stick to using just the stack.
868 (when (not (eq parent-side side
))
869 ;; A parent whose window-side is non-nil must
870 ;; have a child with the same window-side.
873 (when (window-buffer window
)
874 ;; Record that we have at least one non-side,
877 ((if (memq side
'(left top
))
878 (window-prev-sibling window
)
879 (window-next-sibling window
))
880 ;; Left and top major side windows must not have a
881 ;; previous sibling, right and bottom major side
882 ;; windows must not have a next sibling.
884 ;; Now check that there's no more than one major
885 ;; window for any of left, top, right and bottom.
887 (if left
(throw 'reset t
) (setq left t
)))
889 (if top
(throw 'reset t
) (setq top t
)))
891 (if right
(throw 'reset t
) (setq right t
)))
893 (if bottom
(throw 'reset t
) (setq bottom t
)))
897 ;; If there's a side window, there must be at least one
899 (and (or left top right bottom
) (not none
)))
902 (set-window-parameter window
'window-side nil
))
905 (defun window--check (&optional frame
)
906 "Check atomic and side windows on FRAME.
907 FRAME defaults to the selected frame."
908 (window--side-check frame
)
909 (window--atom-check frame
))
912 (defvar window-size-fixed nil
913 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
914 If the value is `height', then only the window's height is fixed.
915 If the value is `width', then only the window's width is fixed.
916 Any other non-nil value fixes both the width and the height.
918 Emacs won't change the size of any window displaying that buffer,
919 unless it has no other choice (like when deleting a neighboring
921 (make-variable-buffer-local 'window-size-fixed
)
923 (defun window--size-ignore-p (window ignore
)
924 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
925 (if (window-valid-p ignore
) (eq window ignore
) ignore
))
927 (defun window-min-size (&optional window horizontal ignore
)
928 "Return the minimum size of WINDOW.
929 WINDOW must be a valid window and defaults to the selected one.
930 Optional argument HORIZONTAL non-nil means return the minimum
931 number of columns of WINDOW; otherwise return the minimum number
934 Optional argument IGNORE, if non-nil, means ignore restrictions
935 imposed by fixed size windows, `window-min-height' or
936 `window-min-width' settings. If IGNORE equals `safe', live
937 windows may get as small as `window-safe-min-height' lines and
938 `window-safe-min-width' columns. If IGNORE is a window, ignore
939 restrictions for that window only. Any other non-nil value
940 means ignore all of the above restrictions for all windows."
942 (window-normalize-window window
) horizontal ignore
))
944 (defun window--min-size-1 (window horizontal ignore
)
945 "Internal function of `window-min-size'."
946 (let ((sub (window-child window
)))
949 ;; WINDOW is an internal window.
950 (if (window-combined-p sub horizontal
)
951 ;; The minimum size of an iso-combination is the sum of
952 ;; the minimum sizes of its child windows.
955 (window--min-size-1 sub horizontal ignore
)))
956 (setq sub
(window-right sub
)))
957 ;; The minimum size of an ortho-combination is the maximum of
958 ;; the minimum sizes of its child windows.
960 (setq value
(max value
961 (window--min-size-1 sub horizontal ignore
)))
962 (setq sub
(window-right sub
))))
964 (with-current-buffer (window-buffer window
)
966 ((and (not (window--size-ignore-p window ignore
))
967 (window-size-fixed-p window horizontal
))
968 ;; The minimum size of a fixed size window is its size.
969 (window-total-size window horizontal
))
970 ((or (eq ignore
'safe
) (eq ignore window
))
971 ;; If IGNORE equals `safe' or WINDOW return the safe values.
972 (if horizontal window-safe-min-width window-safe-min-height
))
974 ;; For the minimum width of a window take fringes and
975 ;; scroll-bars into account. This is questionable and should
976 ;; be removed as soon as we are able to split (and resize)
977 ;; windows such that the new (or resized) windows can get a
978 ;; size less than the user-specified `window-min-height' and
979 ;; `window-min-width'.
980 (let ((frame (window-frame window
))
981 (fringes (window-fringes window
))
982 (scroll-bars (window-scroll-bars window
)))
984 (+ window-safe-min-width
985 (ceiling (car fringes
) (frame-char-width frame
))
986 (ceiling (cadr fringes
) (frame-char-width frame
))
988 ((memq (nth 2 scroll-bars
) '(left right
))
990 ((memq (frame-parameter frame
'vertical-scroll-bars
)
992 (ceiling (or (frame-parameter frame
'scroll-bar-width
) 14)
995 (if (and (not (window--size-ignore-p window ignore
))
996 (numberp window-min-width
))
1000 ;; For the minimum height of a window take any mode- or
1001 ;; header-line into account.
1002 (max (+ window-safe-min-height
1003 (if header-line-format
1 0)
1004 (if mode-line-format
1 0))
1005 (if (and (not (window--size-ignore-p window ignore
))
1006 (numberp window-min-height
))
1010 (defun window-sizable (window delta
&optional horizontal ignore
)
1011 "Return DELTA if DELTA lines can be added to WINDOW.
1012 WINDOW must be a valid window and defaults to the selected one.
1013 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1014 columns can be added to WINDOW. A return value of zero means
1015 that no lines (or columns) can be added to WINDOW.
1017 This function looks only at WINDOW and, recursively, its child
1018 windows. The function `window-resizable' looks at other windows
1021 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1022 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1023 return the maximum value in the range 0..DELTA by which WINDOW
1026 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1027 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1028 return the minimum value in the range DELTA..0 by which WINDOW
1031 Optional argument IGNORE non-nil means ignore restrictions
1032 imposed by fixed size windows, `window-min-height' or
1033 `window-min-width' settings. If IGNORE equals `safe', live
1034 windows may get as small as `window-safe-min-height' lines and
1035 `window-safe-min-width' columns. If IGNORE is a window, ignore
1036 restrictions for that window only. Any other non-nil value means
1037 ignore all of the above restrictions for all windows."
1038 (setq window
(window-normalize-window window
))
1041 (max (- (window-min-size window horizontal ignore
)
1042 (window-total-size window horizontal
))
1044 ((window--size-ignore-p window ignore
)
1047 (if (window-size-fixed-p window horizontal
)
1052 (defun window-sizable-p (window delta
&optional horizontal ignore
)
1053 "Return t if WINDOW can be resized by DELTA lines.
1054 WINDOW must be a valid window and defaults to the selected one.
1055 For the meaning of the arguments of this function see the
1056 doc-string of `window-sizable'."
1057 (setq window
(window-normalize-window window
))
1059 (>= (window-sizable window delta horizontal ignore
) delta
)
1060 (<= (window-sizable window delta horizontal ignore
) delta
)))
1062 (defun window--size-fixed-1 (window horizontal
)
1063 "Internal function for `window-size-fixed-p'."
1064 (let ((sub (window-child window
)))
1067 ;; WINDOW is an internal window.
1068 (if (window-combined-p sub horizontal
)
1069 ;; An iso-combination is fixed size if all its child
1070 ;; windows are fixed-size.
1073 (unless (window--size-fixed-1 sub horizontal
)
1074 ;; We found a non-fixed-size child window, so
1075 ;; WINDOW's size is not fixed.
1077 (setq sub
(window-right sub
)))
1078 ;; All child windows are fixed-size, so WINDOW's size is
1081 ;; An ortho-combination is fixed-size if at least one of its
1082 ;; child windows is fixed-size.
1084 (when (window--size-fixed-1 sub horizontal
)
1085 ;; We found a fixed-size child window, so WINDOW's size
1088 (setq sub
(window-right sub
))))
1089 ;; WINDOW is a live window.
1090 (with-current-buffer (window-buffer window
)
1092 (memq window-size-fixed
'(width t
))
1093 (memq window-size-fixed
'(height t
))))))))
1095 (defun window-size-fixed-p (&optional window horizontal
)
1096 "Return non-nil if WINDOW's height is fixed.
1097 WINDOW must be a valid window and defaults to the selected one.
1098 Optional argument HORIZONTAL non-nil means return non-nil if
1099 WINDOW's width is fixed.
1101 If this function returns nil, this does not necessarily mean that
1102 WINDOW can be resized in the desired direction. The function
1103 `window-resizable' can tell that."
1104 (window--size-fixed-1
1105 (window-normalize-window window
) horizontal
))
1107 (defun window--min-delta-1 (window delta
&optional horizontal ignore trail noup
)
1108 "Internal function for `window-min-delta'."
1109 (if (not (window-parent window
))
1110 ;; If we can't go up, return zero.
1112 ;; Else try to find a non-fixed-size sibling of WINDOW.
1113 (let* ((parent (window-parent window
))
1114 (sub (window-child parent
)))
1116 (if (window-combined-p sub horizontal
)
1117 ;; In an iso-combination throw DELTA if we find at least one
1118 ;; child window and that window is either not fixed-size or
1119 ;; we can ignore fixed-sizeness.
1120 (let ((skip (eq trail
'after
)))
1124 (setq skip
(eq trail
'before
)))
1126 ((and (not (window--size-ignore-p window ignore
))
1127 (window-size-fixed-p sub horizontal
)))
1129 ;; We found a non-fixed-size child window.
1130 (throw 'done delta
)))
1131 (setq sub
(window-right sub
))))
1132 ;; In an ortho-combination set DELTA to the minimum value by
1133 ;; which other child windows can shrink.
1135 (unless (eq sub window
)
1138 (- (window-total-size sub horizontal
)
1139 (window-min-size sub horizontal ignore
)))))
1140 (setq sub
(window-right sub
))))
1143 (window--min-delta-1 parent delta horizontal ignore trail
))))))
1145 (defun window-min-delta (&optional window horizontal ignore trail noup nodown
)
1146 "Return number of lines by which WINDOW can be shrunk.
1147 WINDOW must be a valid window and defaults to the selected one.
1148 Return zero if WINDOW cannot be shrunk.
1150 Optional argument HORIZONTAL non-nil means return number of
1151 columns by which WINDOW can be shrunk.
1153 Optional argument IGNORE non-nil means ignore restrictions
1154 imposed by fixed size windows, `window-min-height' or
1155 `window-min-width' settings. If IGNORE is a window, ignore
1156 restrictions for that window only. If IGNORE equals `safe',
1157 live windows may get as small as `window-safe-min-height' lines
1158 and `window-safe-min-width' columns. Any other non-nil value
1159 means ignore all of the above restrictions for all windows.
1161 Optional argument TRAIL restricts the windows that can be enlarged.
1162 If its value is `before', only windows to the left of or above WINDOW
1163 can be enlarged. If it is `after', only windows to the right of or
1164 below WINDOW can be enlarged.
1166 Optional argument NOUP non-nil means don't go up in the window
1167 tree, but try to enlarge windows within WINDOW's combination only.
1169 Optional argument NODOWN non-nil means don't check whether WINDOW
1170 itself (and its child windows) can be shrunk; check only whether
1171 at least one other window can be enlarged appropriately."
1172 (setq window
(window-normalize-window window
))
1173 (let ((size (window-total-size window horizontal
))
1174 (minimum (window-min-size window horizontal ignore
)))
1177 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1178 (window--min-delta-1 window size horizontal ignore trail noup
))
1180 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1181 ;; there's nothing to recover.
1184 ;; Otherwise, try to recover whatever WINDOW is larger than its
1186 (window--min-delta-1
1187 window
(- size minimum
) horizontal ignore trail noup
)))))
1189 (defun window--max-delta-1 (window delta
&optional horizontal ignore trail noup
)
1190 "Internal function of `window-max-delta'."
1191 (if (not (window-parent window
))
1192 ;; Can't go up. Return DELTA.
1194 (let* ((parent (window-parent window
))
1195 (sub (window-child parent
)))
1197 (if (window-combined-p sub horizontal
)
1198 ;; For an iso-combination calculate how much we can get from
1199 ;; other child windows.
1200 (let ((skip (eq trail
'after
)))
1204 (setq skip
(eq trail
'before
)))
1209 (- (window-total-size sub horizontal
)
1210 (window-min-size sub horizontal ignore
))))))
1211 (setq sub
(window-right sub
))))
1212 ;; For an ortho-combination throw DELTA when at least one
1213 ;; child window is fixed-size.
1215 (when (and (not (eq sub window
))
1216 (not (window--size-ignore-p sub ignore
))
1217 (window-size-fixed-p sub horizontal
))
1218 (throw 'fixed delta
))
1219 (setq sub
(window-right sub
))))
1221 ;; When NOUP is nil, DELTA is all we can get.
1223 ;; Else try with parent of WINDOW, passing the DELTA we
1224 ;; recovered so far.
1225 (window--max-delta-1 parent delta horizontal ignore trail
))))))
1227 (defun window-max-delta (&optional window horizontal ignore trail noup nodown
)
1228 "Return maximum number of lines by which WINDOW can be enlarged.
1229 WINDOW must be a valid window and defaults to the selected one.
1230 The return value is zero if WINDOW cannot be enlarged.
1232 Optional argument HORIZONTAL non-nil means return maximum number
1233 of columns by which WINDOW can be enlarged.
1235 Optional argument IGNORE non-nil means ignore restrictions
1236 imposed by fixed size windows, `window-min-height' or
1237 `window-min-width' settings. If IGNORE is a window, ignore
1238 restrictions for that window only. If IGNORE equals `safe',
1239 live windows may get as small as `window-safe-min-height' lines
1240 and `window-safe-min-width' columns. Any other non-nil value means
1241 ignore all of the above restrictions for all windows.
1243 Optional argument TRAIL restricts the windows that can be enlarged.
1244 If its value is `before', only windows to the left of or above WINDOW
1245 can be enlarged. If it is `after', only windows to the right of or
1246 below WINDOW can be enlarged.
1248 Optional argument NOUP non-nil means don't go up in the window
1249 tree but try to obtain the entire space from windows within
1250 WINDOW's combination.
1252 Optional argument NODOWN non-nil means do not check whether
1253 WINDOW itself (and its child windows) can be enlarged; check
1254 only whether other windows can be shrunk appropriately."
1255 (setq window
(window-normalize-window window
))
1256 (if (and (not (window--size-ignore-p window ignore
))
1257 (not nodown
) (window-size-fixed-p window horizontal
))
1258 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1261 ;; WINDOW has no fixed size.
1262 (window--max-delta-1 window
0 horizontal ignore trail noup
)))
1264 ;; Make NOUP also inhibit the min-size check.
1265 (defun window--resizable (window delta
&optional horizontal ignore trail noup nodown
)
1266 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1267 WINDOW must be a valid window and defaults to the selected one.
1268 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1269 can be resized horizontally by DELTA columns. A return value of
1270 zero means that WINDOW is not resizable.
1272 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1273 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1274 return the maximum value in the range 0..DELTA by which WINDOW
1277 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1278 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1279 return the minimum value in the range DELTA..0 that can be used
1280 for shrinking WINDOW.
1282 Optional argument IGNORE non-nil means ignore restrictions
1283 imposed by fixed size windows, `window-min-height' or
1284 `window-min-width' settings. If IGNORE is a window, ignore
1285 restrictions for that window only. If IGNORE equals `safe',
1286 live windows may get as small as `window-safe-min-height' lines
1287 and `window-safe-min-width' columns. Any other non-nil value
1288 means ignore all of the above restrictions for all windows.
1290 Optional argument TRAIL `before' means only windows to the left
1291 of or below WINDOW can be shrunk. Optional argument TRAIL
1292 `after' means only windows to the right of or above WINDOW can be
1295 Optional argument NOUP non-nil means don't go up in the window
1296 tree but check only whether space can be obtained from (or given
1297 to) WINDOW's siblings.
1299 Optional argument NODOWN non-nil means don't go down in the
1300 window tree. This means do not check whether resizing would
1301 violate size restrictions of WINDOW or its child windows."
1302 (setq window
(window-normalize-window window
))
1305 (max (- (window-min-delta window horizontal ignore trail noup nodown
))
1308 (min (window-max-delta window horizontal ignore trail noup nodown
)
1312 (defun window--resizable-p (window delta
&optional horizontal ignore trail noup nodown
)
1313 "Return t if WINDOW can be resized vertically by DELTA lines.
1314 WINDOW must be a valid window and defaults to the selected one.
1315 For the meaning of the arguments of this function see the
1316 doc-string of `window--resizable'."
1317 (setq window
(window-normalize-window window
))
1319 (>= (window--resizable window delta horizontal ignore trail noup nodown
)
1321 (<= (window--resizable window delta horizontal ignore trail noup nodown
)
1324 (defun window-resizable (window delta
&optional horizontal ignore
)
1325 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1326 WINDOW must be a valid window and defaults to the selected one.
1327 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1328 can be resized horizontally by DELTA columns. A return value of
1329 zero means that WINDOW is not resizable.
1331 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1332 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1333 return the maximum value in the range 0..DELTA by which WINDOW
1336 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1337 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1338 return the minimum value in the range DELTA..0 that can be used
1339 for shrinking WINDOW.
1341 Optional argument IGNORE non-nil means ignore restrictions
1342 imposed by fixed size windows, `window-min-height' or
1343 `window-min-width' settings. If IGNORE is a window, ignore
1344 restrictions for that window only. If IGNORE equals `safe',
1345 live windows may get as small as `window-safe-min-height' lines
1346 and `window-safe-min-width' columns. Any other non-nil value
1347 means ignore all of the above restrictions for all windows."
1348 (setq window
(window-normalize-window window
))
1349 (window--resizable window delta horizontal ignore
))
1351 (defun window-total-size (&optional window horizontal
)
1352 "Return the total height or width of WINDOW.
1353 WINDOW must be a valid window and defaults to the selected one.
1355 If HORIZONTAL is omitted or nil, return the total height of
1356 WINDOW, in lines, like `window-total-height'. Otherwise return
1357 the total width, in columns, like `window-total-width'."
1359 (window-total-width window
)
1360 (window-total-height window
)))
1362 ;; Eventually we should make `window-height' obsolete.
1363 (defalias 'window-height
'window-total-height
)
1365 ;; See discussion in bug#4543.
1366 (defun window-full-height-p (&optional window
)
1367 "Return t if WINDOW is as high as its containing frame.
1368 More precisely, return t if and only if the total height of
1369 WINDOW equals the total height of the root window of WINDOW's
1370 frame. WINDOW must be a valid window and defaults to the
1372 (setq window
(window-normalize-window window
))
1373 (= (window-total-size window
)
1374 (window-total-size (frame-root-window window
))))
1376 (defun window-full-width-p (&optional window
)
1377 "Return t if WINDOW is as wide as its containing frame.
1378 More precisely, return t if and only if the total width of WINDOW
1379 equals the total width of the root window of WINDOW's frame.
1380 WINDOW must be a valid window and defaults to the selected one."
1381 (setq window
(window-normalize-window window
))
1382 (= (window-total-size window t
)
1383 (window-total-size (frame-root-window window
) t
)))
1385 (defun window-body-size (&optional window horizontal
)
1386 "Return the height or width of WINDOW's text area.
1387 WINDOW must be a live window and defaults to the selected one.
1389 If HORIZONTAL is omitted or nil, return the height of the text
1390 area, like `window-body-height'. Otherwise, return the width of
1391 the text area, like `window-body-width'."
1393 (window-body-width window
)
1394 (window-body-height window
)))
1396 ;; Eventually we should make `window-height' obsolete.
1397 (defalias 'window-width
'window-body-width
)
1399 (defun window-current-scroll-bars (&optional window
)
1400 "Return the current scroll bar settings for WINDOW.
1401 WINDOW must be a live window and defaults to the selected one.
1403 The return value is a cons cell (VERTICAL . HORIZONTAL) where
1404 VERTICAL specifies the current location of the vertical scroll
1405 bars (`left', `right', or nil), and HORIZONTAL specifies the
1406 current location of the horizontal scroll bars (`top', `bottom',
1409 Unlike `window-scroll-bars', this function reports the scroll bar
1410 type actually used, once frame defaults and `scroll-bar-mode' are
1411 taken into account."
1412 (setq window
(window-normalize-window window t
))
1413 (let ((vert (nth 2 (window-scroll-bars window
)))
1415 (when (or (eq vert t
) (eq hor t
))
1416 (let ((fcsb (frame-current-scroll-bars (window-frame window
))))
1418 (setq vert
(car fcsb
)))
1420 (setq hor
(cdr fcsb
)))))
1423 (defun walk-windows (fun &optional minibuf all-frames
)
1424 "Cycle through all live windows, calling FUN for each one.
1425 FUN must specify a function with a window as its sole argument.
1426 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1427 windows to include in the walk.
1429 MINIBUF t means include the minibuffer window even if the
1430 minibuffer is not active. MINIBUF nil or omitted means include
1431 the minibuffer window only if the minibuffer is active. Any
1432 other value means do not include the minibuffer window even if
1433 the minibuffer is active.
1435 ALL-FRAMES nil or omitted means consider all windows on the
1436 selected frame, plus the minibuffer window if specified by the
1437 MINIBUF argument. If the minibuffer counts, consider all windows
1438 on all frames that share that minibuffer too. The following
1439 non-nil values of ALL-FRAMES have special meanings:
1441 - t means consider all windows on all existing frames.
1443 - `visible' means consider all windows on all visible frames on
1444 the current terminal.
1446 - 0 (the number zero) means consider all windows on all visible
1447 and iconified frames on the current terminal.
1449 - A frame means consider all windows on that frame only.
1451 Anything else means consider all windows on the selected frame
1454 This function changes neither the order of recently selected
1455 windows nor the buffer list."
1456 ;; If we start from the minibuffer window, don't fail to come
1458 (when (window-minibuffer-p (selected-window))
1460 ;; Make sure to not mess up the order of recently selected
1461 ;; windows. Use `save-selected-window' and `select-window'
1462 ;; with second argument non-nil for this purpose.
1463 (save-selected-window
1464 (when (framep all-frames
)
1465 (select-window (frame-first-window all-frames
) 'norecord
))
1466 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames
))
1467 (funcall fun walk-windows-window
))))
1469 (defun window-at-side-p (&optional window side
)
1470 "Return t if WINDOW is at SIDE of its containing frame.
1471 WINDOW must be a valid window and defaults to the selected one.
1472 SIDE can be any of the symbols `left', `top', `right' or
1473 `bottom'. The default value nil is handled like `bottom'."
1474 (setq window
(window-normalize-window window
))
1479 ((eq side
'right
) 2)
1480 ((memq side
'(bottom nil
)) 3))))
1481 (= (nth edge
(window-edges window
))
1482 (nth edge
(window-edges (frame-root-window window
))))))
1484 (defun window-at-side-list (&optional frame side
)
1485 "Return list of all windows on SIDE of FRAME.
1486 FRAME must be a live frame and defaults to the selected frame.
1487 SIDE can be any of the symbols `left', `top', `right' or
1488 `bottom'. The default value nil is handled like `bottom'."
1489 (setq frame
(window-normalize-frame frame
))
1493 (when (window-at-side-p window side
)
1494 (setq windows
(cons window windows
))))
1496 (nreverse windows
)))
1498 (defun window--in-direction-2 (window posn
&optional horizontal
)
1499 "Support function for `window-in-direction'."
1501 (let ((top (window-top-line window
)))
1504 (- posn top
(window-total-height window
))))
1505 (let ((left (window-left-column window
)))
1508 (- posn left
(window-total-width window
))))))
1510 ;; Predecessors to the below have been devised by Julian Assange in
1511 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
1512 ;; Neither of these allow to selectively ignore specific windows
1513 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
1515 (defun window-in-direction (direction &optional window ignore
)
1516 "Return window in DIRECTION as seen from WINDOW.
1517 More precisely, return the nearest window in direction DIRECTION
1518 as seen from the position of `window-point' in window WINDOW.
1519 DIRECTION must be one of `above', `below', `left' or `right'.
1520 WINDOW must be a live window and defaults to the selected one.
1522 Do not return a window whose `no-other-window' parameter is
1523 non-nil. If the nearest window's `no-other-window' parameter is
1524 non-nil, try to find another window in the indicated direction.
1525 If, however, the optional argument IGNORE is non-nil, return that
1526 window even if its `no-other-window' parameter is non-nil.
1528 Return nil if no suitable window can be found."
1529 (setq window
(window-normalize-window window t
))
1530 (unless (memq direction
'(above below left right
))
1531 (error "Wrong direction %s" direction
))
1532 (let* ((frame (window-frame window
))
1533 (hor (memq direction
'(left right
)))
1535 (window-left-column window
)
1536 (window-top-line window
)))
1537 (last (+ first
(if hor
1538 (window-total-width window
)
1539 (window-total-height window
))))
1540 (posn-cons (nth 6 (posn-at-point (window-point window
) window
)))
1541 ;; The column / row value of `posn-at-point' can be nil for the
1542 ;; mini-window, guard against that.
1544 (+ (or (cdr posn-cons
) 1) (window-top-line window
))
1545 (+ (or (car posn-cons
) 1) (window-left-column window
))))
1548 ((eq direction
'below
) (frame-height frame
))
1549 ((eq direction
'right
) (frame-width frame
))
1551 (best-edge-2 best-edge
)
1552 (best-diff-2 (if hor
(frame-height frame
) (frame-width frame
)))
1553 best best-2 best-diff-2-new
)
1556 (let* ((w-top (window-top-line w
))
1557 (w-left (window-left-column w
)))
1560 ;; Ignore ourselves.
1561 (and (window-parameter w
'no-other-window
)
1562 ;; Ignore W unless IGNORE is non-nil.
1566 ((and (<= w-top posn
)
1567 (< posn
(+ w-top
(window-total-height w
))))
1568 ;; W is to the left or right of WINDOW and covers POSN.
1569 (when (or (and (eq direction
'left
)
1570 (<= w-left first
) (> w-left best-edge
))
1571 (and (eq direction
'right
)
1572 (>= w-left last
) (< w-left best-edge
)))
1573 (setq best-edge w-left
)
1575 ((and (or (and (eq direction
'left
)
1576 (<= (+ w-left
(window-total-width w
)) first
))
1577 (and (eq direction
'right
) (<= last w-left
)))
1578 ;; W is to the left or right of WINDOW but does not
1580 (setq best-diff-2-new
1581 (window--in-direction-2 w posn hor
))
1582 (or (< best-diff-2-new best-diff-2
)
1583 (and (= best-diff-2-new best-diff-2
)
1584 (if (eq direction
'left
)
1585 (> w-left best-edge-2
)
1586 (< w-left best-edge-2
)))))
1587 (setq best-edge-2 w-left
)
1588 (setq best-diff-2 best-diff-2-new
)
1592 ((and (<= w-left posn
)
1593 (< posn
(+ w-left
(window-total-width w
))))
1594 ;; W is above or below WINDOW and covers POSN.
1595 (when (or (and (eq direction
'above
)
1596 (<= w-top first
) (> w-top best-edge
))
1597 (and (eq direction
'below
)
1598 (>= w-top first
) (< w-top best-edge
)))
1599 (setq best-edge w-top
)
1601 ((and (or (and (eq direction
'above
)
1602 (<= (+ w-top
(window-total-height w
)) first
))
1603 (and (eq direction
'below
) (<= last w-top
)))
1604 ;; W is above or below WINDOW but does not cover POSN.
1605 (setq best-diff-2-new
1606 (window--in-direction-2 w posn hor
))
1607 (or (< best-diff-2-new best-diff-2
)
1608 (and (= best-diff-2-new best-diff-2
)
1609 (if (eq direction
'above
)
1610 (> w-top best-edge-2
)
1611 (< w-top best-edge-2
)))))
1612 (setq best-edge-2 w-top
)
1613 (setq best-diff-2 best-diff-2-new
)
1614 (setq best-2 w
)))))))
1618 (defun get-window-with-predicate (predicate &optional minibuf all-frames default
)
1619 "Return a live window satisfying PREDICATE.
1620 More precisely, cycle through all windows calling the function
1621 PREDICATE on each one of them with the window as its sole
1622 argument. Return the first window for which PREDICATE returns
1623 non-nil. Windows are scanned starting with the window following
1624 the selected window. If no window satisfies PREDICATE, return
1627 MINIBUF t means include the minibuffer window even if the
1628 minibuffer is not active. MINIBUF nil or omitted means include
1629 the minibuffer window only if the minibuffer is active. Any
1630 other value means do not include the minibuffer window even if
1631 the minibuffer is active.
1633 ALL-FRAMES nil or omitted means consider all windows on the selected
1634 frame, plus the minibuffer window if specified by the MINIBUF
1635 argument. If the minibuffer counts, consider all windows on all
1636 frames that share that minibuffer too. The following non-nil
1637 values of ALL-FRAMES have special meanings:
1639 - t means consider all windows on all existing frames.
1641 - `visible' means consider all windows on all visible frames on
1642 the current terminal.
1644 - 0 (the number zero) means consider all windows on all visible
1645 and iconified frames on the current terminal.
1647 - A frame means consider all windows on that frame only.
1649 Anything else means consider all windows on the selected frame
1652 (dolist (window (window-list-1
1653 (next-window nil minibuf all-frames
)
1654 minibuf all-frames
))
1655 (when (funcall predicate window
)
1656 (throw 'found window
)))
1659 (defalias 'some-window
'get-window-with-predicate
)
1661 (defun get-lru-window (&optional all-frames dedicated not-selected
)
1662 "Return the least recently used window on frames specified by ALL-FRAMES.
1663 Return a full-width window if possible. A minibuffer window is
1664 never a candidate. A dedicated window is never a candidate
1665 unless DEDICATED is non-nil, so if all windows are dedicated, the
1666 value is nil. Avoid returning the selected window if possible.
1667 Optional argument NOT-SELECTED non-nil means never return the
1670 The following non-nil values of the optional argument ALL-FRAMES
1671 have special meanings:
1673 - t means consider all windows on all existing frames.
1675 - `visible' means consider all windows on all visible frames on
1676 the current terminal.
1678 - 0 (the number zero) means consider all windows on all visible
1679 and iconified frames on the current terminal.
1681 - A frame means consider all windows on that frame only.
1683 Any other value of ALL-FRAMES means consider all windows on the
1684 selected frame and no others."
1685 (let (best-window best-time second-best-window second-best-time time
)
1686 (dolist (window (window-list-1 nil
'nomini all-frames
))
1687 (when (and (or dedicated
(not (window-dedicated-p window
)))
1688 (or (not not-selected
) (not (eq window
(selected-window)))))
1689 (setq time
(window-use-time window
))
1690 (if (or (eq window
(selected-window))
1691 (not (window-full-width-p window
)))
1692 (when (or (not second-best-time
) (< time second-best-time
))
1693 (setq second-best-time time
)
1694 (setq second-best-window window
))
1695 (when (or (not best-time
) (< time best-time
))
1696 (setq best-time time
)
1697 (setq best-window window
)))))
1698 (or best-window second-best-window
)))
1700 (defun get-mru-window (&optional all-frames dedicated not-selected
)
1701 "Return the most recently used window on frames specified by ALL-FRAMES.
1702 A minibuffer window is never a candidate. A dedicated window is
1703 never a candidate unless DEDICATED is non-nil, so if all windows
1704 are dedicated, the value is nil. Optional argument NOT-SELECTED
1705 non-nil means never return the selected window.
1707 The following non-nil values of the optional argument ALL-FRAMES
1708 have special meanings:
1710 - t means consider all windows on all existing frames.
1712 - `visible' means consider all windows on all visible frames on
1713 the current terminal.
1715 - 0 (the number zero) means consider all windows on all visible
1716 and iconified frames on the current terminal.
1718 - A frame means consider all windows on that frame only.
1720 Any other value of ALL-FRAMES means consider all windows on the
1721 selected frame and no others."
1722 (let (best-window best-time time
)
1723 (dolist (window (window-list-1 nil
'nomini all-frames
))
1724 (setq time
(window-use-time window
))
1725 (when (and (or dedicated
(not (window-dedicated-p window
)))
1726 (or (not not-selected
) (not (eq window
(selected-window))))
1727 (or (not best-time
) (> time best-time
)))
1728 (setq best-time time
)
1729 (setq best-window window
)))
1732 (defun get-largest-window (&optional all-frames dedicated not-selected
)
1733 "Return the largest window on frames specified by ALL-FRAMES.
1734 A minibuffer window is never a candidate. A dedicated window is
1735 never a candidate unless DEDICATED is non-nil, so if all windows
1736 are dedicated, the value is nil. Optional argument NOT-SELECTED
1737 non-nil means never return the selected window.
1739 The following non-nil values of the optional argument ALL-FRAMES
1740 have special meanings:
1742 - t means consider all windows on all existing frames.
1744 - `visible' means consider all windows on all visible frames on
1745 the current terminal.
1747 - 0 (the number zero) means consider all windows on all visible
1748 and iconified frames on the current terminal.
1750 - A frame means consider all windows on that frame only.
1752 Any other value of ALL-FRAMES means consider all windows on the
1753 selected frame and no others."
1756 (dolist (window (window-list-1 nil
'nomini all-frames
))
1757 (when (and (or dedicated
(not (window-dedicated-p window
)))
1758 (or (not not-selected
) (not (eq window
(selected-window)))))
1759 (setq size
(* (window-total-size window
)
1760 (window-total-size window t
)))
1761 (when (> size best-size
)
1762 (setq best-size size
)
1763 (setq best-window window
))))
1766 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames
)
1767 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
1768 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
1769 and defaults to the current buffer. Windows are scanned starting
1770 with the selected window.
1772 MINIBUF t means include the minibuffer window even if the
1773 minibuffer is not active. MINIBUF nil or omitted means include
1774 the minibuffer window only if the minibuffer is active. Any
1775 other value means do not include the minibuffer window even if
1776 the minibuffer is active.
1778 ALL-FRAMES nil or omitted means consider all windows on the
1779 selected frame, plus the minibuffer window if specified by the
1780 MINIBUF argument. If the minibuffer counts, consider all windows
1781 on all frames that share that minibuffer too. The following
1782 non-nil values of ALL-FRAMES have special meanings:
1784 - t means consider all windows on all existing frames.
1786 - `visible' means consider all windows on all visible frames on
1787 the current terminal.
1789 - 0 (the number zero) means consider all windows on all visible
1790 and iconified frames on the current terminal.
1792 - A frame means consider all windows on that frame only.
1794 Anything else means consider all windows on the selected frame
1796 (let ((buffer (window-normalize-buffer buffer-or-name
))
1798 (dolist (window (window-list-1 (selected-window) minibuf all-frames
))
1799 (when (eq (window-buffer window
) buffer
)
1800 (setq windows
(cons window windows
))))
1801 (nreverse windows
)))
1803 (defun minibuffer-window-active-p (window)
1804 "Return t if WINDOW is the currently active minibuffer window."
1805 (eq window
(active-minibuffer-window)))
1807 (defun count-windows (&optional minibuf
)
1808 "Return the number of live windows on the selected frame.
1809 The optional argument MINIBUF specifies whether the minibuffer
1810 window shall be counted. See `walk-windows' for the precise
1811 meaning of this argument."
1812 (length (window-list-1 nil minibuf
)))
1814 ;;; Resizing windows.
1815 (defun window--resize-reset (&optional frame horizontal
)
1816 "Reset resize values for all windows on FRAME.
1817 FRAME defaults to the selected frame.
1819 This function stores the current value of `window-total-size' applied
1820 with argument HORIZONTAL in the new total size of all windows on
1821 FRAME. It also resets the new normal size of each of these
1823 (window--resize-reset-1
1824 (frame-root-window (window-normalize-frame frame
)) horizontal
))
1826 (defun window--resize-reset-1 (window horizontal
)
1827 "Internal function of `window--resize-reset'."
1828 ;; Register old size in the new total size.
1829 (set-window-new-total window
(window-total-size window horizontal
))
1830 ;; Reset new normal size.
1831 (set-window-new-normal window
)
1832 (when (window-child window
)
1833 (window--resize-reset-1 (window-child window
) horizontal
))
1834 (when (window-right window
)
1835 (window--resize-reset-1 (window-right window
) horizontal
)))
1837 ;; The following routine is used to manually resize the minibuffer
1838 ;; window and is currently used, for example, by ispell.el.
1839 (defun window--resize-mini-window (window delta
)
1840 "Resize minibuffer window WINDOW by DELTA lines.
1841 If WINDOW cannot be resized by DELTA lines make it as large (or
1842 as small) as possible, but don't signal an error."
1843 (when (window-minibuffer-p window
)
1844 (let* ((frame (window-frame window
))
1845 (root (frame-root-window frame
))
1846 (height (window-total-size window
))
1848 (- (window-total-size root
)
1849 (window-min-size root
))))
1852 ((<= (+ height delta
) 0)
1853 (setq delta
(- (- height
1))))
1854 ((> delta min-delta
)
1855 (setq delta min-delta
)))
1858 (window--resize-reset frame
)
1859 ;; Ideally we should be able to resize just the last child of root
1860 ;; here. See the comment in `resize-root-window-vertically' for
1861 ;; why we do not do that.
1862 (window--resize-this-window root
(- delta
) nil nil t
)
1863 (set-window-new-total window
(+ height delta
))
1864 ;; The following routine catches the case where we want to resize
1865 ;; a minibuffer-only frame.
1866 (resize-mini-window-internal window
))))
1868 (defun window-resize (window delta
&optional horizontal ignore
)
1869 "Resize WINDOW vertically by DELTA lines.
1870 WINDOW can be an arbitrary window and defaults to the selected
1871 one. An attempt to resize the root window of a frame will raise
1874 DELTA a positive number means WINDOW shall be enlarged by DELTA
1875 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
1878 Optional argument HORIZONTAL non-nil means resize WINDOW
1879 horizontally by DELTA columns. In this case a positive DELTA
1880 means enlarge WINDOW by DELTA columns. DELTA negative means
1881 WINDOW shall be shrunk by -DELTA columns.
1883 Optional argument IGNORE non-nil means ignore restrictions
1884 imposed by fixed size windows, `window-min-height' or
1885 `window-min-width' settings. If IGNORE is a window, ignore
1886 restrictions for that window only. If IGNORE equals `safe',
1887 live windows may get as small as `window-safe-min-height' lines
1888 and `window-safe-min-width' columns. Any other non-nil value
1889 means ignore all of the above restrictions for all windows.
1891 This function resizes other windows proportionally and never
1892 deletes any windows. If you want to move only the low (right)
1893 edge of WINDOW consider using `adjust-window-trailing-edge'
1895 (setq window
(window-normalize-window window
))
1896 (let* ((frame (window-frame window
))
1897 (minibuffer-window (minibuffer-window frame
))
1900 ((eq window
(frame-root-window frame
))
1901 (error "Cannot resize the root window of a frame"))
1902 ((window-minibuffer-p window
)
1904 (error "Cannot resize minibuffer window horizontally")
1905 (window--resize-mini-window window delta
)))
1906 ((and (not horizontal
)
1907 (window-full-height-p window
)
1908 (eq (window-frame minibuffer-window
) frame
)
1909 (or (not resize-mini-windows
)
1910 (eq minibuffer-window
(active-minibuffer-window))))
1911 ;; If WINDOW is full height and either `resize-mini-windows' is
1912 ;; nil or the minibuffer window is active, resize the minibuffer
1914 (window--resize-mini-window minibuffer-window
(- delta
)))
1915 ((window--resizable-p window delta horizontal ignore
)
1916 (window--resize-reset frame horizontal
)
1917 (window--resize-this-window window delta horizontal ignore t
)
1918 (if (and (not window-combination-resize
)
1919 (window-combined-p window horizontal
)
1920 (setq sibling
(or (window-right window
) (window-left window
)))
1921 (window-sizable-p sibling
(- delta
) horizontal ignore
))
1922 ;; If window-combination-resize is nil, WINDOW is part of an
1923 ;; iso-combination, and WINDOW's neighboring right or left
1924 ;; sibling can be resized as requested, resize that sibling.
1927 (window-total-size (window-parent window
) horizontal
))))
1928 (window--resize-this-window sibling
(- delta
) horizontal nil t
)
1929 (set-window-new-normal
1930 window
(+ (window-normal-size window horizontal
)
1932 (set-window-new-normal
1933 sibling
(- (window-normal-size sibling horizontal
)
1935 ;; Otherwise, resize all other windows in the same combination.
1936 (window--resize-siblings window delta horizontal ignore
))
1937 (window-resize-apply frame horizontal
))
1939 (error "Cannot resize window %s" window
)))))
1941 (defun window--resize-child-windows-skip-p (window)
1942 "Return non-nil if WINDOW shall be skipped by resizing routines."
1943 (memq (window-new-normal window
) '(ignore stuck skip
)))
1945 (defun window--resize-child-windows-normal (parent horizontal window this-delta
&optional trail other-delta
)
1946 "Recursively set new normal height of child windows of window PARENT.
1947 HORIZONTAL non-nil means set the new normal width of these
1948 windows. WINDOW specifies a child window of PARENT that has been
1949 resized by THIS-DELTA lines (columns).
1951 Optional argument TRAIL either `before' or `after' means set values
1952 only for windows before or after WINDOW. Optional argument
1953 OTHER-DELTA, a number, specifies that this many lines (columns)
1954 have been obtained from (or returned to) an ancestor window of
1955 PARENT in order to resize WINDOW."
1956 (let* ((delta-normal
1957 (if (and (= (- this-delta
) (window-total-size window horizontal
))
1958 (zerop other-delta
))
1959 ;; When WINDOW gets deleted and we can return its entire
1960 ;; space to its siblings, use WINDOW's normal size as the
1962 (- (window-normal-size window horizontal
))
1963 ;; In any other case calculate the normal delta from the
1964 ;; relation of THIS-DELTA to the total size of PARENT.
1965 (/ (float this-delta
) (window-total-size parent horizontal
))))
1966 (sub (window-child parent
))
1968 (skip (eq trail
'after
)))
1970 ;; Set parent-normal to the sum of the normal sizes of all child
1971 ;; windows of PARENT that shall be resized, excluding only WINDOW
1972 ;; and any windows specified by the optional TRAIL argument.
1976 (setq skip
(eq trail
'before
)))
1980 (+ parent-normal
(window-normal-size sub horizontal
)))))
1981 (setq sub
(window-right sub
)))
1983 ;; Set the new normal size of all child windows of PARENT from what
1984 ;; they should have contributed for recovering THIS-DELTA lines
1986 (setq sub
(window-child parent
))
1987 (setq skip
(eq trail
'after
))
1991 (setq skip
(eq trail
'before
)))
1994 (let ((old-normal (window-normal-size sub horizontal
)))
1995 (set-window-new-normal
1996 sub
(min 1.0 ; Don't get larger than 1.
1998 (* (/ old-normal parent-normal
)
2000 ;; Don't drop below 0.
2002 (setq sub
(window-right sub
)))
2004 (when (numberp other-delta
)
2005 ;; Set the new normal size of windows from what they should have
2006 ;; contributed for recovering OTHER-DELTA lines (columns).
2007 (setq delta-normal
(/ (float (window-total-size parent horizontal
))
2008 (+ (window-total-size parent horizontal
)
2010 (setq sub
(window-child parent
))
2011 (setq skip
(eq trail
'after
))
2015 (setq skip
(eq trail
'before
)))
2018 (set-window-new-normal
2019 sub
(min 1.0 ; Don't get larger than 1.
2020 (max (* (window-new-normal sub
) delta-normal
)
2021 ;; Don't drop below 0.
2023 (setq sub
(window-right sub
))))
2025 ;; Set the new normal size of WINDOW to what is left by the sum of
2026 ;; the normal sizes of its siblings.
2027 (set-window-new-normal
2030 (setq sub
(window-child parent
))
2034 ((not (numberp (window-new-normal sub
)))
2035 (setq sum
(+ sum
(window-normal-size sub horizontal
))))
2037 (setq sum
(+ sum
(window-new-normal sub
)))))
2038 (setq sub
(window-right sub
)))
2039 ;; Don't get larger than 1 or smaller than 0.
2040 (min 1.0 (max (- 1.0 sum
) 0.0))))))
2042 (defun window--resize-child-windows (parent delta
&optional horizontal window ignore trail edge
)
2043 "Resize child windows of window PARENT vertically by DELTA lines.
2044 PARENT must be a vertically combined internal window.
2046 Optional argument HORIZONTAL non-nil means resize child windows of
2047 PARENT horizontally by DELTA columns. In this case PARENT must
2048 be a horizontally combined internal window.
2050 WINDOW, if specified, must denote a child window of PARENT that
2051 is resized by DELTA lines.
2053 Optional argument IGNORE non-nil means ignore restrictions
2054 imposed by fixed size windows, `window-min-height' or
2055 `window-min-width' settings. If IGNORE equals `safe', live
2056 windows may get as small as `window-safe-min-height' lines and
2057 `window-safe-min-width' columns. If IGNORE is a window, ignore
2058 restrictions for that window only. Any other non-nil value means
2059 ignore all of the above restrictions for all windows.
2061 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2062 of windows that shall be resized. If TRAIL equals `before',
2063 resize only windows on the left or above EDGE. If TRAIL equals
2064 `after', resize only windows on the right or below EDGE. Also,
2065 preferably only resize windows adjacent to EDGE.
2067 Return the symbol `normalized' if new normal sizes have been
2068 already set by this routine."
2069 (let* ((first (window-child parent
))
2071 (parent-total (+ (window-total-size parent horizontal
) delta
))
2072 best-window best-value
)
2074 (if (and edge
(memq trail
'(before after
))
2077 (while (and (window-right sub
)
2078 (or (and (eq trail
'before
)
2079 (not (window--resize-child-windows-skip-p
2080 (window-right sub
))))
2081 (and (eq trail
'after
)
2082 (window--resize-child-windows-skip-p sub
))))
2083 (setq sub
(window-right sub
)))
2086 (if (eq trail
'before
)
2087 (= (+ (window-left-column sub
)
2088 (window-total-size sub t
))
2090 (= (window-left-column sub
) edge
))
2091 (if (eq trail
'before
)
2092 (= (+ (window-top-line sub
)
2093 (window-total-size sub
))
2095 (= (window-top-line sub
) edge
)))
2096 (window-sizable-p sub delta horizontal ignore
))
2097 ;; Resize only windows adjacent to EDGE.
2099 (window--resize-this-window
2100 sub delta horizontal ignore t trail edge
)
2101 (if (and window
(eq (window-parent sub
) parent
))
2103 ;; Assign new normal sizes.
2104 (set-window-new-normal
2105 sub
(/ (float (window-new-total sub
)) parent-total
))
2106 (set-window-new-normal
2107 window
(- (window-normal-size window horizontal
)
2108 (- (window-new-normal sub
)
2109 (window-normal-size sub horizontal
)))))
2110 (window--resize-child-windows-normal
2111 parent horizontal sub
0 trail delta
))
2112 ;; Return 'normalized to notify `window--resize-siblings' that
2113 ;; normal sizes have been already set.
2115 ;; Resize all windows proportionally.
2119 ((or (window--resize-child-windows-skip-p sub
)
2120 ;; Ignore windows to skip and fixed-size child windows -
2121 ;; in the latter case make it a window to skip.
2123 (window-size-fixed-p sub horizontal
)
2124 (set-window-new-normal sub
'ignore
))))
2126 ;; When shrinking store the number of lines/cols we can get
2127 ;; from this window here together with the total/normal size
2129 (set-window-new-normal
2132 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
2133 (window-min-delta sub horizontal ignore trail t
) ; t)
2134 (- (/ (float (window-total-size sub horizontal
))
2136 (window-normal-size sub horizontal
)))))
2138 ;; When enlarging store the total/normal size factor only
2139 (set-window-new-normal
2141 (- (/ (float (window-total-size sub horizontal
))
2143 (window-normal-size sub horizontal
)))))
2145 (setq sub
(window-right sub
)))
2149 ;; Shrink windows by delta.
2150 (setq best-window t
)
2151 (while (and best-window
(not (zerop delta
)))
2153 (setq best-window nil
)
2154 (setq best-value most-negative-fixnum
)
2156 (when (and (consp (window-new-normal sub
))
2157 (not (zerop (car (window-new-normal sub
))))
2158 (> (cdr (window-new-normal sub
)) best-value
))
2159 (setq best-window sub
)
2160 (setq best-value
(cdr (window-new-normal sub
))))
2162 (setq sub
(window-right sub
)))
2165 (setq delta
(1+ delta
)))
2166 (set-window-new-total best-window -
1 t
)
2167 (set-window-new-normal
2169 (if (= (car (window-new-normal best-window
)) 1)
2170 'skip
; We can't shrink best-window any further.
2171 (cons (1- (car (window-new-normal best-window
)))
2172 (- (/ (float (window-new-total best-window
))
2174 (window-normal-size best-window horizontal
)))))))
2176 ;; Enlarge windows by delta.
2177 (setq best-window t
)
2178 (while (and best-window
(not (zerop delta
)))
2180 (setq best-window nil
)
2181 (setq best-value most-positive-fixnum
)
2183 (when (and (numberp (window-new-normal sub
))
2184 (< (window-new-normal sub
) best-value
))
2185 (setq best-window sub
)
2186 (setq best-value
(window-new-normal sub
)))
2188 (setq sub
(window-right sub
)))
2191 (setq delta
(1- delta
)))
2192 (set-window-new-total best-window
1 t
)
2193 (set-window-new-normal
2195 (- (/ (float (window-new-total best-window
))
2197 (window-normal-size best-window horizontal
))))))
2202 (when (or (consp (window-new-normal sub
))
2203 (numberp (window-new-normal sub
)))
2204 ;; Reset new normal size fields so `window-resize-apply'
2205 ;; won't use them to apply new sizes.
2206 (set-window-new-normal sub
))
2208 (unless (eq (window-new-normal sub
) 'ignore
)
2209 ;; Resize this window's child windows (back-engineering
2210 ;; delta from sub's old and new total sizes).
2211 (let ((delta (- (window-new-total sub
)
2212 (window-total-size sub horizontal
))))
2213 (unless (and (zerop delta
) (not trail
))
2214 ;; For the TRAIL non-nil case we have to resize SUB
2215 ;; recursively even if it's size does not change.
2216 (window--resize-this-window
2217 sub delta horizontal ignore nil trail edge
))))
2218 (setq sub
(window-right sub
)))))))
2220 (defun window--resize-siblings (window delta
&optional horizontal ignore trail edge
)
2221 "Resize other windows when WINDOW is resized vertically by DELTA lines.
2222 Optional argument HORIZONTAL non-nil means resize other windows
2223 when WINDOW is resized horizontally by DELTA columns. WINDOW
2224 itself is not resized by this function.
2226 Optional argument IGNORE non-nil means ignore restrictions
2227 imposed by fixed size windows, `window-min-height' or
2228 `window-min-width' settings. If IGNORE equals `safe', live
2229 windows may get as small as `window-safe-min-height' lines and
2230 `window-safe-min-width' columns. If IGNORE is a window, ignore
2231 restrictions for that window only. Any other non-nil value means
2232 ignore all of the above restrictions for all windows.
2234 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2235 of windows that shall be resized. If TRAIL equals `before',
2236 resize only windows on the left or above EDGE. If TRAIL equals
2237 `after', resize only windows on the right or below EDGE. Also,
2238 preferably only resize windows adjacent to EDGE."
2239 (when (window-parent window
)
2240 (let* ((parent (window-parent window
))
2241 (sub (window-child parent
)))
2242 (if (window-combined-p sub horizontal
)
2243 ;; In an iso-combination try to extract DELTA from WINDOW's
2245 (let ((skip (eq trail
'after
))
2246 this-delta other-delta
)
2247 ;; Decide which windows shall be left alone.
2251 ;; Make sure WINDOW is left alone when
2252 ;; resizing its siblings.
2253 (set-window-new-normal sub
'ignore
)
2254 (setq skip
(eq trail
'before
)))
2256 ;; Make sure this sibling is left alone when
2257 ;; resizing its siblings.
2258 (set-window-new-normal sub
'ignore
))
2259 ((or (window--size-ignore-p sub ignore
)
2260 (not (window-size-fixed-p sub horizontal
)))
2261 ;; Set this-delta to t to signal that we found a sibling
2262 ;; of WINDOW whose size is not fixed.
2263 (setq this-delta t
)))
2265 (setq sub
(window-right sub
)))
2267 ;; Set this-delta to what we can get from WINDOW's siblings.
2268 (if (= (- delta
) (window-total-size window horizontal
))
2269 ;; A deletion, presumably. We must handle this case
2270 ;; specially since `window--resizable' can't be used.
2272 ;; There's at least one resizable sibling we can
2273 ;; give WINDOW's size to.
2274 (setq this-delta delta
)
2275 ;; No resizable sibling exists.
2276 (setq this-delta
0))
2277 ;; Any other form of resizing.
2279 (window--resizable window delta horizontal ignore trail t
)))
2281 ;; Set other-delta to what we still have to get from
2282 ;; ancestor windows of parent.
2283 (setq other-delta
(- delta this-delta
))
2284 (unless (zerop other-delta
)
2285 ;; Unless we got everything from WINDOW's siblings, PARENT
2286 ;; must be resized by other-delta lines or columns.
2287 (set-window-new-total parent other-delta
'add
))
2289 (if (zerop this-delta
)
2290 ;; We haven't got anything from WINDOW's siblings but we
2291 ;; must update the normal sizes to respect other-delta.
2292 (window--resize-child-windows-normal
2293 parent horizontal window this-delta trail other-delta
)
2294 ;; We did get something from WINDOW's siblings which means
2295 ;; we have to resize their child windows.
2296 (unless (eq (window--resize-child-windows
2297 parent
(- this-delta
) horizontal
2298 window ignore trail edge
)
2299 ;; If `window--resize-child-windows' returns
2300 ;; 'normalized, this means it has set the
2301 ;; normal sizes already.
2303 ;; Set the normal sizes.
2304 (window--resize-child-windows-normal
2305 parent horizontal window this-delta trail other-delta
))
2306 ;; Set DELTA to what we still have to get from ancestor
2308 (setq delta other-delta
)))
2310 ;; In an ortho-combination all siblings of WINDOW must be
2311 ;; resized by DELTA.
2312 (set-window-new-total parent delta
'add
)
2314 (unless (eq sub window
)
2315 (window--resize-this-window sub delta horizontal ignore t
))
2316 (setq sub
(window-right sub
))))
2318 (unless (zerop delta
)
2320 (window--resize-siblings
2321 parent delta horizontal ignore trail edge
)))))
2323 (defun window--resize-this-window (window delta
&optional horizontal ignore add trail edge
)
2324 "Resize WINDOW vertically by DELTA lines.
2325 Optional argument HORIZONTAL non-nil means resize WINDOW
2326 horizontally by DELTA columns.
2328 Optional argument IGNORE non-nil means ignore restrictions
2329 imposed by fixed size windows, `window-min-height' or
2330 `window-min-width' settings. If IGNORE equals `safe', live
2331 windows may get as small as `window-safe-min-height' lines and
2332 `window-safe-min-width' columns. If IGNORE is a window, ignore
2333 restrictions for that window only. Any other non-nil value
2334 means ignore all of the above restrictions for all windows.
2336 Optional argument ADD non-nil means add DELTA to the new total
2339 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2340 of windows that shall be resized. If TRAIL equals `before',
2341 resize only windows on the left or above EDGE. If TRAIL equals
2342 `after', resize only windows on the right or below EDGE. Also,
2343 preferably only resize windows adjacent to EDGE.
2345 This function recursively resizes WINDOW's child windows to fit the
2346 new size. Make sure that WINDOW is `window--resizable' before
2347 calling this function. Note that this function does not resize
2348 siblings of WINDOW or WINDOW's parent window. You have to
2349 eventually call `window-resize-apply' in order to make resizing
2350 actually take effect."
2352 ;; Add DELTA to the new total size of WINDOW.
2353 (set-window-new-total window delta t
))
2355 (let ((sub (window-child window
)))
2358 ((window-combined-p sub horizontal
)
2359 ;; In an iso-combination resize child windows according to their
2361 (window--resize-child-windows
2362 window delta horizontal nil ignore trail edge
))
2363 ;; In an ortho-combination resize each child window by DELTA.
2366 (window--resize-this-window
2367 sub delta horizontal ignore t trail edge
)
2368 (setq sub
(window-right sub
)))))))
2370 (defun window--resize-root-window (window delta horizontal ignore
)
2371 "Resize root window WINDOW vertically by DELTA lines.
2372 HORIZONTAL non-nil means resize root window WINDOW horizontally
2375 IGNORE non-nil means ignore any restrictions imposed by fixed
2376 size windows, `window-min-height' or `window-min-width' settings.
2378 This function is only called by the frame resizing routines. It
2379 resizes windows proportionally and never deletes any windows."
2380 (when (and (windowp window
) (numberp delta
)
2381 (window-sizable-p window delta horizontal ignore
))
2382 (window--resize-reset (window-frame window
) horizontal
)
2383 (window--resize-this-window window delta horizontal ignore t
)))
2385 (defun window--resize-root-window-vertically (window delta
)
2386 "Resize root window WINDOW vertically by DELTA lines.
2387 If DELTA is less than zero and we can't shrink WINDOW by DELTA
2388 lines, shrink it as much as possible. If DELTA is greater than
2389 zero, this function can resize fixed-size windows in order to
2390 recover the necessary lines.
2392 Return the number of lines that were recovered.
2394 This function is only called by the minibuffer window resizing
2395 routines. It resizes windows proportionally and never deletes
2397 (when (numberp delta
)
2401 (setq delta
(window-sizable window delta
)))
2403 (unless (window-sizable window delta
)
2406 (window--resize-reset (window-frame window
))
2407 ;; Ideally, we would resize just the last window in a combination
2408 ;; but that's not feasible for the following reason: If we grow
2409 ;; the minibuffer window and the last window cannot be shrunk any
2410 ;; more, we shrink another window instead. But if we then shrink
2411 ;; the minibuffer window again, the last window might get enlarged
2412 ;; and the state after shrinking is not the state before growing.
2413 ;; So, in practice, we'd need a history variable to record how to
2414 ;; proceed. But I'm not sure how such a variable could work with
2415 ;; repeated minibuffer window growing steps.
2416 (window--resize-this-window window delta nil ignore t
)
2419 (defun adjust-window-trailing-edge (window delta
&optional horizontal
)
2420 "Move WINDOW's bottom edge by DELTA lines.
2421 Optional argument HORIZONTAL non-nil means move WINDOW's right
2422 edge by DELTA columns. WINDOW must be a valid window and
2423 defaults to the selected one.
2425 If DELTA is greater than zero, move the edge downwards or to the
2426 right. If DELTA is less than zero, move the edge upwards or to
2427 the left. If the edge can't be moved by DELTA lines or columns,
2428 move it as far as possible in the desired direction."
2429 (setq window
(window-normalize-window window
))
2430 (let* ((frame (window-frame window
))
2431 (minibuffer-window (minibuffer-window frame
))
2433 left this-delta min-delta max-delta
)
2434 ;; Find the edge we want to move.
2435 (while (and (or (not (window-combined-p right horizontal
))
2436 (not (window-right right
)))
2437 (setq right
(window-parent right
))))
2439 ((and (not right
) (not horizontal
)
2440 ;; Resize the minibuffer window if it's on the same frame as
2441 ;; and immediately below WINDOW and it's either active or
2442 ;; `resize-mini-windows' is nil.
2443 (eq (window-frame minibuffer-window
) frame
)
2444 (= (nth 1 (window-edges minibuffer-window
))
2445 (nth 3 (window-edges window
)))
2446 (or (not resize-mini-windows
)
2447 (eq minibuffer-window
(active-minibuffer-window))))
2448 (window--resize-mini-window minibuffer-window
(- delta
)))
2449 ((or (not (setq left right
)) (not (setq right
(window-right right
))))
2451 (error "No window on the right of this one")
2452 (error "No window below this one")))
2454 ;; Set LEFT to the first resizable window on the left. This step is
2455 ;; needed to handle fixed-size windows.
2456 (while (and left
(window-size-fixed-p left horizontal
))
2458 (or (window-left left
)
2460 (while (and (setq left
(window-parent left
))
2461 (not (window-combined-p left horizontal
))))
2462 (window-left left
)))))
2465 (error "No resizable window on the left of this one")
2466 (error "No resizable window above this one")))
2468 ;; Set RIGHT to the first resizable window on the right. This step
2469 ;; is needed to handle fixed-size windows.
2470 (while (and right
(window-size-fixed-p right horizontal
))
2472 (or (window-right right
)
2474 (while (and (setq right
(window-parent right
))
2475 (not (window-combined-p right horizontal
))))
2476 (window-right right
)))))
2479 (error "No resizable window on the right of this one")
2480 (error "No resizable window below this one")))
2482 ;; LEFT and RIGHT (which might be both internal windows) are now the
2483 ;; two windows we want to resize.
2486 (setq max-delta
(window--max-delta-1 left
0 horizontal nil
'after
))
2487 (setq min-delta
(window--min-delta-1 right
(- delta
) horizontal nil
'before
))
2488 (when (or (< max-delta delta
) (> min-delta
(- delta
)))
2489 ;; We can't get the whole DELTA - move as far as possible.
2490 (setq delta
(min max-delta
(- min-delta
))))
2491 (unless (zerop delta
)
2493 (window--resize-reset frame horizontal
)
2494 ;; Try to enlarge LEFT first.
2495 (setq this-delta
(window--resizable left delta horizontal
))
2496 (unless (zerop this-delta
)
2497 (window--resize-this-window
2498 left this-delta horizontal nil t
'before
2500 (+ (window-left-column left
) (window-total-size left t
))
2501 (+ (window-top-line left
) (window-total-size left
)))))
2502 ;; Shrink windows on right of LEFT.
2503 (window--resize-siblings
2504 left delta horizontal nil
'after
2506 (window-left-column right
)
2507 (window-top-line right
)))))
2509 (setq max-delta
(window--max-delta-1 right
0 horizontal nil
'before
))
2510 (setq min-delta
(window--min-delta-1 left delta horizontal nil
'after
))
2511 (when (or (< max-delta
(- delta
)) (> min-delta delta
))
2512 ;; We can't get the whole DELTA - move as far as possible.
2513 (setq delta
(max (- max-delta
) min-delta
)))
2514 (unless (zerop delta
)
2516 (window--resize-reset frame horizontal
)
2517 ;; Try to enlarge RIGHT.
2518 (setq this-delta
(window--resizable right
(- delta
) horizontal
))
2519 (unless (zerop this-delta
)
2520 (window--resize-this-window
2521 right this-delta horizontal nil t
'after
2523 (window-left-column right
)
2524 (window-top-line right
))))
2525 ;; Shrink windows on left of RIGHT.
2526 (window--resize-siblings
2527 right
(- delta
) horizontal nil
'before
2529 (+ (window-left-column left
) (window-total-size left t
))
2530 (+ (window-top-line left
) (window-total-size left
)))))))
2531 (unless (zerop delta
)
2532 ;; Don't report an error in the standard case.
2533 (unless (window-resize-apply frame horizontal
)
2534 ;; But do report an error if applying the changes fails.
2535 (error "Failed adjusting window %s" window
)))))))
2537 (defun enlarge-window (delta &optional horizontal
)
2538 "Make the selected window DELTA lines taller.
2539 Interactively, if no argument is given, make the selected window
2540 one line taller. If optional argument HORIZONTAL is non-nil,
2541 make selected window wider by DELTA columns. If DELTA is
2542 negative, shrink selected window by -DELTA lines or columns.
2545 (let ((minibuffer-window (minibuffer-window)))
2548 ((window-size-fixed-p nil horizontal
)
2549 (error "Selected window has fixed size"))
2550 ((window-minibuffer-p)
2552 (error "Cannot resize minibuffer window horizontally")
2553 (window--resize-mini-window (selected-window) delta
)))
2554 ((and (not horizontal
)
2555 (window-full-height-p)
2556 (eq (window-frame minibuffer-window
) (selected-frame))
2557 (not resize-mini-windows
))
2558 ;; If the selected window is full height and `resize-mini-windows'
2559 ;; is nil, resize the minibuffer window.
2560 (window--resize-mini-window minibuffer-window
(- delta
)))
2561 ((window--resizable-p nil delta horizontal
)
2562 (window-resize nil delta horizontal
))
2566 (window-max-delta nil horizontal
)
2567 (- (window-min-delta nil horizontal
)))
2570 (defun shrink-window (delta &optional horizontal
)
2571 "Make the selected window DELTA lines smaller.
2572 Interactively, if no argument is given, make the selected window
2573 one line smaller. If optional argument HORIZONTAL is non-nil,
2574 make selected window narrower by DELTA columns. If DELTA is
2575 negative, enlarge selected window by -DELTA lines or columns.
2576 Also see the `window-min-height' variable.
2579 (let ((minibuffer-window (minibuffer-window)))
2582 ((window-size-fixed-p nil horizontal
)
2583 (error "Selected window has fixed size"))
2584 ((window-minibuffer-p)
2586 (error "Cannot resize minibuffer window horizontally")
2587 (window--resize-mini-window (selected-window) (- delta
))))
2588 ((and (not horizontal
)
2589 (window-full-height-p)
2590 (eq (window-frame minibuffer-window
) (selected-frame))
2591 (not resize-mini-windows
))
2592 ;; If the selected window is full height and `resize-mini-windows'
2593 ;; is nil, resize the minibuffer window.
2594 (window--resize-mini-window minibuffer-window delta
))
2595 ((window--resizable-p nil
(- delta
) horizontal
)
2596 (window-resize nil
(- delta
) horizontal
))
2600 (- (window-min-delta nil horizontal
))
2601 (window-max-delta nil horizontal
))
2604 (defun maximize-window (&optional window
)
2606 Make WINDOW as large as possible without deleting any windows.
2607 WINDOW must be a valid window and defaults to the selected one."
2609 (setq window
(window-normalize-window window
))
2610 (window-resize window
(window-max-delta window
))
2611 (window-resize window
(window-max-delta window t
) t
))
2613 (defun minimize-window (&optional window
)
2615 Make WINDOW as small as possible without deleting any windows.
2616 WINDOW must be a valid window and defaults to the selected one."
2618 (setq window
(window-normalize-window window
))
2619 (window-resize window
(- (window-min-delta window
)))
2620 (window-resize window
(- (window-min-delta window t
)) t
))
2622 (defun frame-root-window-p (window)
2623 "Return non-nil if WINDOW is the root window of its frame."
2624 (eq window
(frame-root-window window
)))
2626 (defun window--subtree (window &optional next
)
2627 "Return window subtree rooted at WINDOW.
2628 Optional argument NEXT non-nil means include WINDOW's right
2629 siblings in the return value.
2631 See the documentation of `window-tree' for a description of the
2638 ((window-top-child window
)
2639 (cons t
(cons (window-edges window
)
2640 (window--subtree (window-top-child window
) t
))))
2641 ((window-left-child window
)
2642 (cons nil
(cons (window-edges window
)
2643 (window--subtree (window-left-child window
) t
))))
2646 (setq window
(when next
(window-next-sibling window
))))
2649 (defun window-tree (&optional frame
)
2650 "Return the window tree of frame FRAME.
2651 FRAME must be a live frame and defaults to the selected frame.
2652 The return value is a list of the form (ROOT MINI), where ROOT
2653 represents the window tree of the frame's root window, and MINI
2654 is the frame's minibuffer window.
2656 If the root window is not split, ROOT is the root window itself.
2657 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
2658 for a horizontal split, and t for a vertical split. EDGES gives
2659 the combined size and position of the child windows in the split,
2660 and the rest of the elements are the child windows in the split.
2661 Each of the child windows may again be a window or a list
2662 representing a window split, and so on. EDGES is a list (LEFT
2663 TOP RIGHT BOTTOM) as returned by `window-edges'."
2664 (setq frame
(window-normalize-frame frame
))
2665 (window--subtree (frame-root-window frame
) t
))
2667 (defun other-window (count &optional all-frames
)
2668 "Select another window in cyclic ordering of windows.
2669 COUNT specifies the number of windows to skip, starting with the
2670 selected window, before making the selection. If COUNT is
2671 positive, skip COUNT windows forwards. If COUNT is negative,
2672 skip -COUNT windows backwards. COUNT zero means do not skip any
2673 window, so select the selected window. In an interactive call,
2674 COUNT is the numeric prefix argument. Return nil.
2676 If the `other-window' parameter of the selected window is a
2677 function and `ignore-window-parameters' is nil, call that
2678 function with the arguments COUNT and ALL-FRAMES.
2680 This function does not select a window whose `no-other-window'
2681 window parameter is non-nil.
2683 This function uses `next-window' for finding the window to
2684 select. The argument ALL-FRAMES has the same meaning as in
2685 `next-window', but the MINIBUF argument of `next-window' is
2686 always effectively nil."
2688 (let* ((window (selected-window))
2689 (function (and (not ignore-window-parameters
)
2690 (window-parameter window
'other-window
)))
2691 old-window old-count
)
2692 (if (functionp function
)
2693 (funcall function count all-frames
)
2694 ;; `next-window' and `previous-window' may return a window we are
2695 ;; not allowed to select. Hence we need an exit strategy in case
2696 ;; all windows are non-selectable.
2699 (setq window
(next-window window nil all-frames
))
2701 ((eq window old-window
)
2702 (when (= count old-count
)
2703 ;; Keep out of infinite loops. When COUNT has not changed
2704 ;; since we last looked at `window' we're probably in one.
2706 ((window-parameter window
'no-other-window
)
2708 ;; The first non-selectable window `next-window' got us:
2709 ;; Remember it and the current value of COUNT.
2710 (setq old-window window
)
2711 (setq old-count count
)))
2713 (setq count
(1- count
)))))
2715 (setq window
(previous-window window nil all-frames
))
2717 ((eq window old-window
)
2718 (when (= count old-count
)
2719 ;; Keep out of infinite loops. When COUNT has not changed
2720 ;; since we last looked at `window' we're probably in one.
2722 ((window-parameter window
'no-other-window
)
2724 ;; The first non-selectable window `previous-window' got
2725 ;; us: Remember it and the current value of COUNT.
2726 (setq old-window window
)
2727 (setq old-count count
)))
2729 (setq count
(1+ count
)))))
2731 (select-window window
)
2732 ;; Always return nil.
2735 ;; This should probably return non-nil when the selected window is part
2736 ;; of an atomic window whose root is the frame's root window.
2737 (defun one-window-p (&optional nomini all-frames
)
2738 "Return non-nil if the selected window is the only window.
2739 Optional arg NOMINI non-nil means don't count the minibuffer
2740 even if it is active. Otherwise, the minibuffer is counted
2743 Optional argument ALL-FRAMES specifies the set of frames to
2744 consider, see also `next-window'. ALL-FRAMES nil or omitted
2745 means consider windows on the selected frame only, plus the
2746 minibuffer window if specified by the NOMINI argument. If the
2747 minibuffer counts, consider all windows on all frames that share
2748 that minibuffer too. The remaining non-nil values of ALL-FRAMES
2749 with a special meaning are:
2751 - t means consider all windows on all existing frames.
2753 - `visible' means consider all windows on all visible frames on
2754 the current terminal.
2756 - 0 (the number zero) means consider all windows on all visible
2757 and iconified frames on the current terminal.
2759 - A frame means consider all windows on that frame only.
2761 Anything else means consider all windows on the selected frame
2763 (let ((base-window (selected-window)))
2764 (if (and nomini
(eq base-window
(minibuffer-window)))
2765 (setq base-window
(next-window base-window
)))
2767 (next-window base-window
(if nomini
'arg
) all-frames
))))
2769 ;;; Deleting windows.
2770 (defun window-deletable-p (&optional window
)
2771 "Return t if WINDOW can be safely deleted from its frame.
2772 WINDOW must be a valid window and defaults to the selected one.
2773 Return `frame' if deleting WINDOW should also delete its frame."
2774 (setq window
(window-normalize-window window
))
2776 (unless ignore-window-parameters
2777 ;; Handle atomicity.
2778 (when (window-parameter window
'window-atom
)
2779 (setq window
(window-atom-root window
))))
2781 (let ((frame (window-frame window
)))
2783 ((frame-root-window-p window
)
2784 ;; WINDOW's frame can be deleted only if there are other frames
2785 ;; on the same terminal, and it does not contain the active
2787 (unless (or (eq frame
(next-frame frame
0))
2788 (let ((minibuf (active-minibuffer-window)))
2789 (and minibuf
(eq frame
(window-frame minibuf
)))))
2791 ((or ignore-window-parameters
2792 (not (eq window
(window--major-non-side-window frame
))))
2793 ;; WINDOW can be deleted unless it is the major non-side window of
2797 (defun window--in-subtree-p (window root
)
2798 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
2799 (or (eq window root
)
2800 (let ((parent (window-parent window
)))
2803 (if (eq parent root
)
2805 (setq parent
(window-parent parent
))))))))
2807 (defun delete-window (&optional window
)
2809 WINDOW must be a valid window and defaults to the selected one.
2812 If the variable `ignore-window-parameters' is non-nil or the
2813 `delete-window' parameter of WINDOW equals t, do not process any
2814 parameters of WINDOW. Otherwise, if the `delete-window'
2815 parameter of WINDOW specifies a function, call that function with
2816 WINDOW as its sole argument and return the value returned by that
2819 Otherwise, if WINDOW is part of an atomic window, call
2820 `delete-window' with the root of the atomic window as its
2821 argument. Signal an error if WINDOW is either the only window on
2822 its frame, the last non-side window, or part of an atomic window
2823 that is its frame's root window."
2825 (setq window
(window-normalize-window window
))
2826 (let* ((frame (window-frame window
))
2827 (function (window-parameter window
'delete-window
))
2828 (parent (window-parent window
))
2830 (window--check frame
)
2832 ;; Handle window parameters.
2834 ;; Ignore window parameters if `ignore-window-parameters' tells
2835 ;; us so or `delete-window' equals t.
2836 ((or ignore-window-parameters
(eq function t
)))
2837 ((functionp function
)
2838 ;; The `delete-window' parameter specifies the function to call.
2839 ;; If that function is `ignore' nothing is done. It's up to the
2840 ;; function called here to avoid infinite recursion.
2841 (throw 'done
(funcall function window
)))
2842 ((and (window-parameter window
'window-atom
)
2843 (setq atom-root
(window-atom-root window
))
2844 (not (eq atom-root window
)))
2845 (if (eq atom-root
(frame-root-window frame
))
2846 (error "Root of atomic window is root window of its frame")
2847 (throw 'done
(delete-window atom-root
))))
2849 (error "Attempt to delete minibuffer or sole ordinary window"))
2850 ((eq window
(window--major-non-side-window frame
))
2851 (error "Attempt to delete last non-side window")))
2853 (let* ((horizontal (window-left-child parent
))
2854 (size (window-total-size window horizontal
))
2856 (window--in-subtree-p (frame-selected-window frame
) window
))
2857 ;; Emacs 23 preferably gives WINDOW's space to its left
2859 (sibling (or (window-left window
) (window-right window
))))
2860 (window--resize-reset frame horizontal
)
2862 ((and (not window-combination-resize
)
2863 sibling
(window-sizable-p sibling size
))
2864 ;; Resize WINDOW's sibling.
2865 (window--resize-this-window sibling size horizontal nil t
)
2866 (set-window-new-normal
2867 sibling
(+ (window-normal-size sibling horizontal
)
2868 (window-normal-size window horizontal
))))
2869 ((window--resizable-p window
(- size
) horizontal nil nil nil t
)
2870 ;; Can do without resizing fixed-size windows.
2871 (window--resize-siblings window
(- size
) horizontal
))
2873 ;; Can't do without resizing fixed-size windows.
2874 (window--resize-siblings window
(- size
) horizontal t
)))
2875 ;; Actually delete WINDOW.
2876 (delete-window-internal window
)
2877 (when (and frame-selected
2879 (frame-selected-window frame
) 'no-other-window
))
2880 ;; `delete-window-internal' has selected a window that should
2881 ;; not be selected, fix this here.
2882 (other-window -
1 frame
))
2883 (run-window-configuration-change-hook frame
)
2884 (window--check frame
)
2885 ;; Always return nil.
2888 (defun delete-other-windows (&optional window
)
2889 "Make WINDOW fill its frame.
2890 WINDOW must be a valid window and defaults to the selected one.
2893 If the variable `ignore-window-parameters' is non-nil or the
2894 `delete-other-windows' parameter of WINDOW equals t, do not
2895 process any parameters of WINDOW. Otherwise, if the
2896 `delete-other-windows' parameter of WINDOW specifies a function,
2897 call that function with WINDOW as its sole argument and return
2898 the value returned by that function.
2900 Otherwise, if WINDOW is part of an atomic window, call this
2901 function with the root of the atomic window as its argument. If
2902 WINDOW is a non-side window, make WINDOW the only non-side window
2903 on the frame. Side windows are not deleted. If WINDOW is a side
2904 window signal an error."
2906 (setq window
(window-normalize-window window
))
2907 (let* ((frame (window-frame window
))
2908 (function (window-parameter window
'delete-other-windows
))
2909 (window-side (window-parameter window
'window-side
))
2910 atom-root side-main
)
2911 (window--check frame
)
2914 ;; Ignore window parameters if `ignore-window-parameters' is t or
2915 ;; `delete-other-windows' is t.
2916 ((or ignore-window-parameters
(eq function t
)))
2917 ((functionp function
)
2918 ;; The `delete-other-windows' parameter specifies the function
2919 ;; to call. If the function is `ignore' no windows are deleted.
2920 ;; It's up to the function called to avoid infinite recursion.
2921 (throw 'done
(funcall function window
)))
2922 ((and (window-parameter window
'window-atom
)
2923 (setq atom-root
(window-atom-root window
))
2924 (not (eq atom-root window
)))
2925 (if (eq atom-root
(frame-root-window frame
))
2926 (error "Root of atomic window is root window of its frame")
2927 (throw 'done
(delete-other-windows atom-root
))))
2928 ((memq window-side window-sides
)
2929 (error "Cannot make side window the only window"))
2930 ((and (window-minibuffer-p window
)
2931 (not (eq window
(frame-root-window window
))))
2932 (error "Can't expand minibuffer to full frame")))
2934 ;; If WINDOW is the major non-side window, do nothing.
2935 (if (window-with-parameter 'window-side
)
2936 (setq side-main
(window--major-non-side-window frame
))
2937 (setq side-main
(frame-root-window frame
)))
2938 (unless (eq window side-main
)
2939 (delete-other-windows-internal window side-main
)
2940 (run-window-configuration-change-hook frame
)
2941 (window--check frame
))
2942 ;; Always return nil.
2945 (defun delete-other-windows-vertically (&optional window
)
2946 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
2947 This may be a useful alternative binding for \\[delete-other-windows]
2948 if you often split windows horizontally."
2950 (let* ((window (or window
(selected-window)))
2951 (edges (window-edges window
))
2953 (while (not (eq (setq w
(next-window w
1)) window
))
2954 (let ((e (window-edges w
)))
2955 (when (and (= (car e
) (car edges
))
2956 (= (nth 2 e
) (nth 2 edges
)))
2958 (mapc 'delete-window delenda
)))
2960 ;;; Windows and buffers.
2962 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
2963 ;; for (1) determining which buffer to show in the window when its
2964 ;; buffer shall be buried or killed and (2) which buffer to show for
2965 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2967 ;; `prev-buffers' consists of <buffer, window-start, window-point>
2968 ;; triples. The entries on this list are ordered by the time their
2969 ;; buffer has been removed from the window, the most recently removed
2970 ;; buffer's entry being first. The window-start and window-point
2971 ;; components are `window-start' and `window-point' at the time the
2972 ;; buffer was removed from the window which implies that the entry must
2973 ;; be added when `set-window-buffer' removes the buffer from the window.
2975 ;; `next-buffers' is the list of buffers that have been replaced
2976 ;; recently by `switch-to-prev-buffer'. These buffers are the least
2977 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
2978 ;; candidates of `switch-to-next-buffer' to switch to. This list is
2979 ;; reset to nil by any action changing the window's buffer with the
2980 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
2981 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
2982 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
2984 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
2985 ;; if such a buffer was killed while the window was hidden within a
2986 ;; window configuration. Such killed buffers get removed whenever
2987 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
2989 ;; The following function is called by `set-window-buffer' _before_ it
2990 ;; replaces the buffer of the argument window with the new buffer.
2991 (defun record-window-buffer (&optional window
)
2992 "Record WINDOW's buffer.
2993 WINDOW must be a live window and defaults to the selected one."
2994 (let* ((window (window-normalize-window window t
))
2995 (buffer (window-buffer window
))
2996 (entry (assq buffer
(window-prev-buffers window
))))
2997 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
2998 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2999 (set-window-next-buffers window nil
)
3002 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
3003 (set-window-prev-buffers
3004 window
(assq-delete-all buffer
(window-prev-buffers window
))))
3006 ;; Don't record insignificant buffers.
3007 (unless (eq (aref (buffer-name buffer
) 0) ?\s
)
3008 ;; Add an entry for buffer to WINDOW's previous buffers.
3009 (with-current-buffer buffer
3010 (let ((start (window-start window
))
3011 (point (window-point window
)))
3015 ;; We have an entry, update marker positions.
3016 (list (set-marker (nth 1 entry
) start
)
3017 (set-marker (nth 2 entry
) point
))
3018 ;; Make new markers.
3019 (list (copy-marker start
)
3020 (copy-marker point
)))))
3022 (set-window-prev-buffers
3023 window
(cons entry
(window-prev-buffers window
))))))))
3025 (defun unrecord-window-buffer (&optional window buffer
)
3026 "Unrecord BUFFER in WINDOW.
3027 WINDOW must be a live window and defaults to the selected one.
3028 BUFFER must be a live buffer and defaults to the buffer of
3030 (let* ((window (window-normalize-window window t
))
3031 (buffer (or buffer
(window-buffer window
))))
3032 (set-window-prev-buffers
3033 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3034 (set-window-next-buffers
3035 window
(delq buffer
(window-next-buffers window
)))))
3037 (defun set-window-buffer-start-and-point (window buffer
&optional start point
)
3038 "Set WINDOW's buffer to BUFFER.
3039 WINDOW must be a live window and defaults to the selected one.
3040 Optional argument START non-nil means set WINDOW's start position
3041 to START. Optional argument POINT non-nil means set WINDOW's
3042 point to POINT. If WINDOW is selected this also sets BUFFER's
3043 `point' to POINT. If WINDOW is selected and the buffer it showed
3044 before was current this also makes BUFFER the current buffer."
3045 (setq window
(window-normalize-window window t
))
3046 (let ((selected (eq window
(selected-window)))
3047 (current (eq (window-buffer window
) (current-buffer))))
3048 (set-window-buffer window buffer
)
3049 (when (and selected current
)
3050 (set-buffer buffer
))
3052 ;; Don't force window-start here (even if POINT is nil).
3053 (set-window-start window start t
))
3055 (set-window-point window point
))))
3057 (defcustom switch-to-visible-buffer t
3058 "If non-nil, allow switching to an already visible buffer.
3059 If this variable is non-nil, `switch-to-prev-buffer' and
3060 `switch-to-next-buffer' may switch to an already visible buffer
3061 provided the buffer was shown in the argument window before. If
3062 this variable is nil, `switch-to-prev-buffer' and
3063 `switch-to-next-buffer' always try to avoid switching to a buffer
3064 that is already visible in another window on the same frame."
3069 (defun switch-to-prev-buffer (&optional window bury-or-kill
)
3070 "In WINDOW switch to previous buffer.
3071 WINDOW must be a live window and defaults to the selected one.
3072 Return the buffer switched to, nil if no suitable buffer could be
3075 Optional argument BURY-OR-KILL non-nil means the buffer currently
3076 shown in WINDOW is about to be buried or killed and consequently
3077 shall not be switched to in future invocations of this command.
3079 As a special case, if BURY-OR-KILL equals `append', this means to
3080 move the buffer to the end of WINDOW's previous buffers list so a
3081 future invocation of `switch-to-prev-buffer' less likely switches
3084 (let* ((window (window-normalize-window window t
))
3085 (frame (window-frame window
))
3086 (old-buffer (window-buffer window
))
3087 ;; Save this since it's destroyed by `set-window-buffer'.
3088 (next-buffers (window-next-buffers window
))
3089 (pred (frame-parameter frame
'buffer-predicate
))
3090 entry new-buffer killed-buffers visible
)
3091 (when (window-minibuffer-p window
)
3092 ;; Don't switch in minibuffer window.
3093 (unless (setq window
(minibuffer-selected-window))
3094 (error "Window %s is a minibuffer window" window
)))
3096 (when (window-dedicated-p window
)
3097 ;; Don't switch in dedicated window.
3098 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3101 ;; Scan WINDOW's previous buffers first, skipping entries of next
3103 (dolist (entry (window-prev-buffers window
))
3104 (when (and (setq new-buffer
(car entry
))
3105 (or (buffer-live-p new-buffer
)
3106 (not (setq killed-buffers
3107 (cons new-buffer killed-buffers
))))
3108 (not (eq new-buffer old-buffer
))
3109 (or (null pred
) (funcall pred new-buffer
))
3110 ;; When BURY-OR-KILL is nil, avoid switching to a
3111 ;; buffer in WINDOW's next buffers list.
3112 (or bury-or-kill
(not (memq new-buffer next-buffers
))))
3113 (if (and (not switch-to-visible-buffer
)
3114 (get-buffer-window new-buffer frame
))
3115 ;; Try to avoid showing a buffer visible in some other
3117 (setq visible new-buffer
)
3118 (set-window-buffer-start-and-point
3119 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3121 ;; Scan reverted buffer list of WINDOW's frame next, skipping
3122 ;; entries of next buffers. Note that when we bury or kill a
3123 ;; buffer we don't reverse the global buffer list to avoid showing
3124 ;; a buried buffer instead. Otherwise, we must reverse the global
3125 ;; buffer list in order to make sure that switching to the
3126 ;; previous/next buffer traverse it in opposite directions.
3127 (dolist (buffer (if bury-or-kill
3129 (nreverse (buffer-list frame
))))
3130 (when (and (buffer-live-p buffer
)
3131 (not (eq buffer old-buffer
))
3132 (or (null pred
) (funcall pred buffer
))
3133 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3134 (or bury-or-kill
(not (memq buffer next-buffers
))))
3135 (if (get-buffer-window buffer frame
)
3136 ;; Try to avoid showing a buffer visible in some other window.
3138 (setq visible buffer
))
3139 (setq new-buffer buffer
)
3140 (set-window-buffer-start-and-point window new-buffer
)
3142 (unless bury-or-kill
3143 ;; Scan reverted next buffers last (must not use nreverse
3145 (dolist (buffer (reverse next-buffers
))
3146 ;; Actually, buffer _must_ be live here since otherwise it
3147 ;; would have been caught in the scan of previous buffers.
3148 (when (and (or (buffer-live-p buffer
)
3149 (not (setq killed-buffers
3150 (cons buffer killed-buffers
))))
3151 (not (eq buffer old-buffer
))
3152 (or (null pred
) (funcall pred buffer
))
3153 (setq entry
(assq buffer
(window-prev-buffers window
))))
3154 (setq new-buffer buffer
)
3155 (set-window-buffer-start-and-point
3156 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3159 ;; Show a buffer visible in another window.
3161 (setq new-buffer visible
)
3162 (set-window-buffer-start-and-point window new-buffer
)))
3165 (let ((entry (and (eq bury-or-kill
'append
)
3166 (assq old-buffer
(window-prev-buffers window
)))))
3167 ;; Remove `old-buffer' from WINDOW's previous and (restored list
3168 ;; of) next buffers.
3169 (set-window-prev-buffers
3170 window
(assq-delete-all old-buffer
(window-prev-buffers window
)))
3171 (set-window-next-buffers window
(delq old-buffer next-buffers
))
3173 ;; Append old-buffer's entry to list of WINDOW's previous
3174 ;; buffers so it's less likely to get switched to soon but
3175 ;; `display-buffer-in-previous-window' can nevertheless find
3177 (set-window-prev-buffers
3178 window
(append (window-prev-buffers window
) (list entry
)))))
3179 ;; Move `old-buffer' to head of WINDOW's restored list of next
3181 (set-window-next-buffers
3182 window
(cons old-buffer
(delq old-buffer next-buffers
))))
3184 ;; Remove killed buffers from WINDOW's previous and next buffers.
3185 (when killed-buffers
3186 (dolist (buffer killed-buffers
)
3187 (set-window-prev-buffers
3188 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3189 (set-window-next-buffers
3190 window
(delq buffer
(window-next-buffers window
)))))
3192 ;; Return new-buffer.
3195 (defun switch-to-next-buffer (&optional window
)
3196 "In WINDOW switch to next buffer.
3197 WINDOW must be a live window and defaults to the selected one.
3198 Return the buffer switched to, nil if no suitable buffer could be
3201 (let* ((window (window-normalize-window window t
))
3202 (frame (window-frame window
))
3203 (old-buffer (window-buffer window
))
3204 (next-buffers (window-next-buffers window
))
3205 (pred (frame-parameter frame
'buffer-predicate
))
3206 new-buffer entry killed-buffers visible
)
3207 (when (window-minibuffer-p window
)
3208 ;; Don't switch in minibuffer window.
3209 (unless (setq window
(minibuffer-selected-window))
3210 (error "Window %s is a minibuffer window" window
)))
3212 (when (window-dedicated-p window
)
3213 ;; Don't switch in dedicated window.
3214 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3217 ;; Scan WINDOW's next buffers first.
3218 (dolist (buffer next-buffers
)
3219 (when (and (or (buffer-live-p buffer
)
3220 (not (setq killed-buffers
3221 (cons buffer killed-buffers
))))
3222 (not (eq buffer old-buffer
))
3223 (or (null pred
) (funcall pred buffer
))
3224 (setq entry
(assq buffer
(window-prev-buffers window
))))
3225 (setq new-buffer buffer
)
3226 (set-window-buffer-start-and-point
3227 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3229 ;; Scan the buffer list of WINDOW's frame next, skipping previous
3231 (dolist (buffer (buffer-list frame
))
3232 (when (and (buffer-live-p buffer
)
3233 (not (eq buffer old-buffer
))
3234 (or (null pred
) (funcall pred buffer
))
3235 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3236 (not (assq buffer
(window-prev-buffers window
))))
3237 (if (get-buffer-window buffer frame
)
3238 ;; Try to avoid showing a buffer visible in some other window.
3239 (setq visible buffer
)
3240 (setq new-buffer buffer
)
3241 (set-window-buffer-start-and-point window new-buffer
)
3243 ;; Scan WINDOW's reverted previous buffers last (must not use
3245 (dolist (entry (reverse (window-prev-buffers window
)))
3246 (when (and (setq new-buffer
(car entry
))
3247 (or (buffer-live-p new-buffer
)
3248 (not (setq killed-buffers
3249 (cons new-buffer killed-buffers
))))
3250 (not (eq new-buffer old-buffer
))
3251 (or (null pred
) (funcall pred new-buffer
)))
3252 (if (and (not switch-to-visible-buffer
)
3253 (get-buffer-window new-buffer frame
))
3254 ;; Try to avoid showing a buffer visible in some other window.
3256 (setq visible new-buffer
))
3257 (set-window-buffer-start-and-point
3258 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3261 ;; Show a buffer visible in another window.
3263 (setq new-buffer visible
)
3264 (set-window-buffer-start-and-point window new-buffer
)))
3266 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
3267 (set-window-next-buffers window
(delq new-buffer next-buffers
))
3269 ;; Remove killed buffers from WINDOW's previous and next buffers.
3270 (when killed-buffers
3271 (dolist (buffer killed-buffers
)
3272 (set-window-prev-buffers
3273 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3274 (set-window-next-buffers
3275 window
(delq buffer
(window-next-buffers window
)))))
3277 ;; Return new-buffer.
3280 (defun get-next-valid-buffer (list &optional buffer visible-ok frame
)
3281 "Search LIST for a valid buffer to display in FRAME.
3282 Return nil when all buffers in LIST are undesirable for display,
3283 otherwise return the first suitable buffer in LIST.
3285 Buffers not visible in windows are preferred to visible buffers,
3286 unless VISIBLE-OK is non-nil.
3287 If the optional argument FRAME is nil, it defaults to the selected frame.
3288 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
3289 ;; This logic is more or less copied from other-buffer.
3290 (setq frame
(or frame
(selected-frame)))
3291 (let ((pred (frame-parameter frame
'buffer-predicate
))
3293 (while (and (not found
) list
)
3294 (setq buf
(car list
))
3295 (if (and (not (eq buffer buf
))
3297 (or (null pred
) (funcall pred buf
))
3298 (not (eq (aref (buffer-name buf
) 0) ?\s
))
3299 (or visible-ok
(null (get-buffer-window buf
'visible
))))
3301 (setq list
(cdr list
))))
3304 (defun last-buffer (&optional buffer visible-ok frame
)
3305 "Return the last buffer in FRAME's buffer list.
3306 If BUFFER is the last buffer, return the preceding buffer
3307 instead. Buffers not visible in windows are preferred to visible
3308 buffers, unless optional argument VISIBLE-OK is non-nil.
3309 Optional third argument FRAME nil or omitted means use the
3310 selected frame's buffer list. If no such buffer exists, return
3311 the buffer `*scratch*', creating it if necessary."
3312 (setq frame
(or frame
(selected-frame)))
3313 (or (get-next-valid-buffer (nreverse (buffer-list frame
))
3314 buffer visible-ok frame
)
3315 (get-buffer "*scratch*")
3316 (let ((scratch (get-buffer-create "*scratch*")))
3317 (set-buffer-major-mode scratch
)
3320 (defcustom frame-auto-hide-function
#'iconify-frame
3321 "Function called to automatically hide frames.
3322 The function is called with one argument - a frame.
3324 Functions affected by this option are those that bury a buffer
3325 shown in a separate frame like `quit-window' and `bury-buffer'."
3326 :type
'(choice (const :tag
"Iconify" iconify-frame
)
3327 (const :tag
"Delete" delete-frame
)
3328 (const :tag
"Do nothing" ignore
)
3334 (defun window--delete (&optional window dedicated-only kill
)
3335 "Delete WINDOW if possible.
3336 WINDOW must be a live window and defaults to the selected one.
3337 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
3338 only if it's dedicated to its buffer. Optional argument KILL
3339 means the buffer shown in window will be killed. Return non-nil
3340 if WINDOW gets deleted or its frame is auto-hidden."
3341 (setq window
(window-normalize-window window t
))
3342 (unless (and dedicated-only
(not (window-dedicated-p window
)))
3343 (let ((deletable (window-deletable-p window
)))
3345 ((eq deletable
'frame
)
3346 (let ((frame (window-frame window
)))
3349 (delete-frame frame
))
3350 ((functionp frame-auto-hide-function
)
3351 (funcall frame-auto-hide-function frame
))))
3354 (delete-window window
)
3357 (defun bury-buffer (&optional buffer-or-name
)
3358 "Put BUFFER-OR-NAME at the end of the list of all buffers.
3359 There it is the least likely candidate for `other-buffer' to
3360 return; thus, the least likely buffer for \\[switch-to-buffer] to
3363 You can specify a buffer name as BUFFER-OR-NAME, or an actual
3364 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
3365 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
3366 remove the current buffer from the selected window if it is
3369 (let* ((buffer (window-normalize-buffer buffer-or-name
)))
3370 ;; If `buffer-or-name' is not on the selected frame we unrecord it
3371 ;; although it's not "here" (call it a feature).
3372 (bury-buffer-internal buffer
)
3373 ;; Handle case where `buffer-or-name' is nil and the current buffer
3374 ;; is shown in the selected window.
3376 ((or buffer-or-name
(not (eq buffer
(window-buffer)))))
3377 ((window--delete nil t
))
3379 ;; Switch to another buffer in window.
3380 (set-window-dedicated-p nil nil
)
3381 (switch-to-prev-buffer nil
'bury
)))
3383 ;; Always return nil.
3386 (defun unbury-buffer ()
3387 "Switch to the last buffer in the buffer list."
3389 (switch-to-buffer (last-buffer)))
3391 (defun next-buffer ()
3392 "In selected window switch to next buffer."
3395 ((window-minibuffer-p)
3396 (error "Cannot switch buffers in minibuffer window"))
3397 ((eq (window-dedicated-p) t
)
3398 (error "Window is strongly dedicated to its buffer"))
3400 (switch-to-next-buffer))))
3402 (defun previous-buffer ()
3403 "In selected window switch to previous buffer."
3406 ((window-minibuffer-p)
3407 (error "Cannot switch buffers in minibuffer window"))
3408 ((eq (window-dedicated-p) t
)
3409 (error "Window is strongly dedicated to its buffer"))
3411 (switch-to-prev-buffer))))
3413 (defun delete-windows-on (&optional buffer-or-name frame
)
3414 "Delete all windows showing BUFFER-OR-NAME.
3415 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3416 and defaults to the current buffer.
3418 The following non-nil values of the optional argument FRAME
3419 have special meanings:
3421 - t means consider all windows on the selected frame only.
3423 - `visible' means consider all windows on all visible frames on
3424 the current terminal.
3426 - 0 (the number zero) means consider all windows on all visible
3427 and iconified frames on the current terminal.
3429 - A frame means consider all windows on that frame only.
3431 Any other value of FRAME means consider all windows on all
3434 When a window showing BUFFER-OR-NAME is dedicated and the only
3435 window of its frame, that frame is deleted when there are other
3437 (interactive "BDelete windows on (buffer):\nP")
3438 (let ((buffer (window-normalize-buffer buffer-or-name
))
3439 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3440 ;; `window-list-1' based function.
3441 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
3442 (dolist (window (window-list-1 nil nil all-frames
))
3443 (if (eq (window-buffer window
) buffer
)
3444 (let ((deletable (window-deletable-p window
)))
3446 ((and (eq deletable
'frame
) (window-dedicated-p window
))
3447 ;; Delete frame if and only if window is dedicated.
3448 (delete-frame (window-frame window
)))
3451 (delete-window window
))
3453 ;; In window switch to previous buffer.
3454 (set-window-dedicated-p window nil
)
3455 (switch-to-prev-buffer window
'bury
))))
3456 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3457 (unrecord-window-buffer window buffer
)))))
3459 (defun replace-buffer-in-windows (&optional buffer-or-name
)
3460 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
3461 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3462 and defaults to the current buffer.
3464 When a window showing BUFFER-OR-NAME is dedicated, that window is
3465 deleted. If that window is the only window on its frame, the
3466 frame is deleted too when there are other frames left. If there
3467 are no other frames left, some other buffer is displayed in that
3470 This function removes the buffer denoted by BUFFER-OR-NAME from
3471 all window-local buffer lists."
3472 (interactive "bBuffer to replace: ")
3473 (let ((buffer (window-normalize-buffer buffer-or-name
)))
3474 (dolist (window (window-list-1 nil nil t
))
3475 (if (eq (window-buffer window
) buffer
)
3476 (unless (window--delete window t t
)
3477 ;; Switch to another buffer in window.
3478 (set-window-dedicated-p window nil
)
3479 (switch-to-prev-buffer window
'kill
))
3480 ;; Unrecord BUFFER in WINDOW.
3481 (unrecord-window-buffer window buffer
)))))
3483 (defun quit-restore-window (&optional window bury-or-kill
)
3484 "Quit WINDOW and deal with its buffer.
3485 WINDOW must be a live window and defaults to the selected one.
3487 According to information stored in WINDOW's `quit-restore' window
3488 parameter either (1) delete WINDOW and its frame, (2) delete
3489 WINDOW, (3) restore the buffer previously displayed in WINDOW,
3490 or (4) make WINDOW display some other buffer than the present
3491 one. If non-nil, reset `quit-restore' parameter to nil.
3493 Optional second argument BURY-OR-KILL tells how to proceed with
3494 the buffer of WINDOW. The following values are handled:
3496 `nil' means to not handle the buffer in a particular way. This
3497 means that if WINDOW is not deleted by this function, invoking
3498 `switch-to-prev-buffer' will usually show the buffer again.
3500 `append' means that if WINDOW is not deleted, move its buffer to
3501 the end of WINDOW's previous buffers so it's less likely that a
3502 future invocation of `switch-to-prev-buffer' will switch to it.
3503 Also, move the buffer to the end of the frame's buffer list.
3505 `bury' means that if WINDOW is not deleted, remove its buffer
3506 from WINDOW'S list of previous buffers. Also, move the buffer
3507 to the end of the frame's buffer list. This value provides the
3508 most reliable remedy to not have `switch-to-prev-buffer' switch
3509 to this buffer again without killing the buffer.
3511 `kill' means to kill WINDOW's buffer."
3512 (setq window
(window-normalize-window window t
))
3513 (let* ((buffer (window-buffer window
))
3514 (quit-restore (window-parameter window
'quit-restore
))
3516 (let* ((prev-buffers (window-prev-buffers window
))
3517 (prev-buffer (caar prev-buffers
)))
3518 (and (or (not (eq prev-buffer buffer
))
3519 (and (cdr prev-buffers
)
3520 (not (eq (setq prev-buffer
(cadr prev-buffers
))
3525 ((and (not prev-buffer
)
3526 (memq (nth 1 quit-restore
) '(window frame
))
3527 (eq (nth 3 quit-restore
) buffer
)
3528 ;; Delete WINDOW if possible.
3529 (window--delete window nil
(eq bury-or-kill
'kill
)))
3530 ;; If the previously selected window is still alive, select it.
3531 (when (window-live-p (nth 2 quit-restore
))
3532 (select-window (nth 2 quit-restore
))))
3533 ((and (listp (setq quad
(nth 1 quit-restore
)))
3534 (buffer-live-p (car quad
))
3535 (eq (nth 3 quit-restore
) buffer
))
3536 ;; Show another buffer stored in quit-restore parameter.
3537 (when (and (integerp (nth 3 quad
))
3538 (/= (nth 3 quad
) (window-total-size window
)))
3539 ;; Try to resize WINDOW to its old height but don't signal an
3542 (window-resize window
(- (nth 3 quad
) (window-total-size window
)))
3544 (set-window-dedicated-p window nil
)
3545 ;; Restore WINDOW's previous buffer, start and point position.
3546 (set-window-buffer-start-and-point
3547 window
(nth 0 quad
) (nth 1 quad
) (nth 2 quad
))
3548 ;; Deal with the buffer we just removed from WINDOW.
3549 (setq entry
(and (eq bury-or-kill
'append
)
3550 (assq buffer
(window-prev-buffers window
))))
3552 ;; Remove buffer from WINDOW's previous and next buffers.
3553 (set-window-prev-buffers
3554 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3555 (set-window-next-buffers
3556 window
(delq buffer
(window-next-buffers window
))))
3558 ;; Append old buffer's entry to list of WINDOW's previous
3559 ;; buffers so it's less likely to get switched to soon but
3560 ;; `display-buffer-in-previous-window' can nevertheless find it.
3561 (set-window-prev-buffers
3562 window
(append (window-prev-buffers window
) (list entry
))))
3563 ;; Reset the quit-restore parameter.
3564 (set-window-parameter window
'quit-restore nil
)
3565 ;; Select old window.
3566 (when (window-live-p (nth 2 quit-restore
))
3567 (select-window (nth 2 quit-restore
))))
3569 ;; Show some other buffer in WINDOW and reset the quit-restore
3571 (set-window-parameter window
'quit-restore nil
)
3572 ;; Make sure that WINDOW is no more dedicated.
3573 (set-window-dedicated-p window nil
)
3574 (switch-to-prev-buffer window bury-or-kill
)))
3576 ;; Deal with the buffer.
3578 ((not (buffer-live-p buffer
)))
3579 ((eq bury-or-kill
'kill
)
3580 (kill-buffer buffer
))
3582 (bury-buffer-internal buffer
)))))
3584 (defun quit-window (&optional kill window
)
3585 "Quit WINDOW and bury its buffer.
3586 WINDOW must be a live window and defaults to the selected one.
3587 With prefix argument KILL non-nil, kill the buffer instead of
3590 According to information stored in WINDOW's `quit-restore' window
3591 parameter either (1) delete WINDOW and its frame, (2) delete
3592 WINDOW, (3) restore the buffer previously displayed in WINDOW,
3593 or (4) make WINDOW display some other buffer than the present
3594 one. If non-nil, reset `quit-restore' parameter to nil."
3596 (quit-restore-window window
(if kill
'kill
'bury
)))
3598 (defun quit-windows-on (&optional buffer-or-name kill frame
)
3599 "Quit all windows showing BUFFER-OR-NAME.
3600 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3601 and defaults to the current buffer. Optional argument KILL
3602 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
3603 BUFFER-OR-NAME. Optional argument FRAME is handled as by
3604 `delete-windows-on'.
3606 This function calls `quit-window' on all candidate windows
3607 showing BUFFER-OR-NAME."
3608 (interactive "BQuit windows on (buffer):\nP")
3609 (let ((buffer (window-normalize-buffer buffer-or-name
))
3610 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3611 ;; `window-list-1' based function.
3612 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
3613 (dolist (window (window-list-1 nil nil all-frames
))
3614 (if (eq (window-buffer window
) buffer
)
3615 (quit-window kill window
)
3616 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3617 (unrecord-window-buffer window buffer
)))))
3619 ;;; Splitting windows.
3620 (defun window-split-min-size (&optional horizontal
)
3621 "Return minimum height of any window when splitting windows.
3622 Optional argument HORIZONTAL non-nil means return minimum width."
3624 (max window-min-width window-safe-min-width
)
3625 (max window-min-height window-safe-min-height
)))
3627 (defun split-window (&optional window size side
)
3628 "Make a new window adjacent to WINDOW.
3629 WINDOW must be a valid window and defaults to the selected one.
3630 Return the new window which is always a live window.
3632 Optional argument SIZE a positive number means make WINDOW SIZE
3633 lines or columns tall. If SIZE is negative, make the new window
3634 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
3635 absolute value can be less than `window-min-height' or
3636 `window-min-width'; so this command can make a new window as
3637 small as one line or two columns. SIZE defaults to half of
3638 WINDOW's size. Interactively, SIZE is the prefix argument.
3640 Optional third argument SIDE nil (or `below') specifies that the
3641 new window shall be located below WINDOW. SIDE `above' means the
3642 new window shall be located above WINDOW. In both cases SIZE
3643 specifies the new number of lines for WINDOW (or the new window
3644 if SIZE is negative) including space reserved for the mode and/or
3647 SIDE t (or `right') specifies that the new window shall be
3648 located on the right side of WINDOW. SIDE `left' means the new
3649 window shall be located on the left of WINDOW. In both cases
3650 SIZE specifies the new number of columns for WINDOW (or the new
3651 window provided SIZE is negative) including space reserved for
3652 fringes and the scrollbar or a divider column. Any other non-nil
3653 value for SIDE is currently handled like t (or `right').
3655 If the variable `ignore-window-parameters' is non-nil or the
3656 `split-window' parameter of WINDOW equals t, do not process any
3657 parameters of WINDOW. Otherwise, if the `split-window' parameter
3658 of WINDOW specifies a function, call that function with all three
3659 arguments and return the value returned by that function.
3661 Otherwise, if WINDOW is part of an atomic window, \"split\" the
3662 root of that atomic window. The new window does not become a
3663 member of that atomic window.
3665 If WINDOW is live, properties of the new window like margins and
3666 scrollbars are inherited from WINDOW. If WINDOW is an internal
3667 window, these properties as well as the buffer displayed in the
3668 new window are inherited from the window selected on WINDOW's
3669 frame. The selected window is not changed by this function."
3671 (setq window
(window-normalize-window window
))
3674 ((memq side
'(below above right left
)) side
)
3676 (horizontal (not (memq side
'(below above
))))
3677 (frame (window-frame window
))
3678 (parent (window-parent window
))
3679 (function (window-parameter window
'split-window
))
3680 (window-side (window-parameter window
'window-side
))
3681 ;; Rebind `window-combination-limit' and
3682 ;; `window-combination-resize' since in some cases we may have
3683 ;; to override their value.
3684 (window-combination-limit window-combination-limit
)
3685 (window-combination-resize window-combination-resize
)
3688 (window--check frame
)
3691 ;; Ignore window parameters if either `ignore-window-parameters'
3692 ;; is t or the `split-window' parameter equals t.
3693 ((or ignore-window-parameters
(eq function t
)))
3694 ((functionp function
)
3695 ;; The `split-window' parameter specifies the function to call.
3696 ;; If that function is `ignore', do nothing.
3697 (throw 'done
(funcall function window size side
)))
3698 ;; If WINDOW is part of an atomic window, split the root window
3699 ;; of that atomic window instead.
3700 ((and (window-parameter window
'window-atom
)
3701 (setq atom-root
(window-atom-root window
))
3702 (not (eq atom-root window
)))
3703 (throw 'done
(split-window atom-root size side
)))
3704 ;; If WINDOW is a side window or its first or last child is a
3705 ;; side window, throw an error unless `window-combination-resize'
3707 ((and (not (eq window-combination-resize
'side
))
3708 (or (window-parameter window
'window-side
)
3709 (and (window-child window
)
3710 (or (window-parameter
3711 (window-child window
) 'window-side
)
3713 (window-last-child window
) 'window-side
)))))
3714 (error "Cannot split side window or parent of side window"))
3715 ;; If `window-combination-resize' is 'side and window has a side
3716 ;; window sibling, bind `window-combination-limit' to t.
3717 ((and (not (eq window-combination-resize
'side
))
3718 (or (and (window-prev-sibling window
)
3720 (window-prev-sibling window
) 'window-side
))
3721 (and (window-next-sibling window
)
3723 (window-next-sibling window
) 'window-side
))))
3724 (setq window-combination-limit t
)))
3726 ;; If `window-combination-resize' is t and SIZE is non-negative,
3727 ;; bind `window-combination-limit' to t.
3728 (when (and (eq window-combination-resize t
) size
(> size
0))
3729 (setq window-combination-limit t
))
3732 ;; `parent-size' is the size of WINDOW's parent, provided
3734 (when parent
(window-total-size parent horizontal
)))
3735 ;; `resize' non-nil means we are supposed to resize other
3736 ;; windows in WINDOW's combination.
3738 (and window-combination-resize
3739 (or (window-parameter window
'window-side
)
3740 (not (eq window-combination-resize
'side
)))
3741 (not window-combination-limit
)
3742 ;; Resize makes sense in iso-combinations only.
3743 (window-combined-p window horizontal
)))
3744 ;; `old-size' is the current size of WINDOW.
3745 (old-size (window-total-size window horizontal
))
3746 ;; `new-size' is the specified or calculated size of the
3751 (max (window-split-min-size horizontal
)
3753 ;; When resizing try to give the new window the
3754 ;; average size of a window in its combination.
3756 (window-min-size parent horizontal
))
3758 (1+ (window-combinations
3759 parent horizontal
))))
3760 ;; Else try to give the new window half the size
3761 ;; of WINDOW (plus an eventual odd line).
3762 (+ (/ old-size
2) (% old-size
2)))))
3764 ;; SIZE non-negative specifies the new size of WINDOW.
3766 ;; Note: Specifying a non-negative SIZE is practically
3767 ;; always done as workaround for making the new window
3768 ;; appear above or on the left of the new window (the
3769 ;; ispell window is a typical example of that). In all
3770 ;; these cases the SIDE argument should be set to 'above
3771 ;; or 'left in order to support the 'resize option.
3772 ;; Here we have to nest the windows instead, see above.
3775 ;; SIZE negative specifies the size of the new window.
3777 new-parent new-normal
)
3784 ;; SIZE unspecified, resizing.
3785 (when (and (not (window-sizable-p parent
(- new-size
) horizontal
))
3786 ;; Try again with minimum split size.
3788 (max new-size
(window-split-min-size horizontal
)))
3789 (not (window-sizable-p parent
(- new-size
) horizontal
)))
3790 (error "Window %s too small for splitting" parent
)))
3791 ((> (+ new-size
(window-min-size window horizontal
)) old-size
)
3792 ;; SIZE unspecified, no resizing.
3793 (error "Window %s too small for splitting" window
))))
3795 (or (>= size old-size
)
3796 (< new-size
(if horizontal
3797 window-safe-min-width
3798 window-safe-min-width
))))
3799 ;; SIZE specified as new size of old window. If the new size
3800 ;; is larger than the old size or the size of the new window
3801 ;; would be less than the safe minimum, signal an error.
3802 (error "Window %s too small for splitting" window
))
3804 ;; SIZE specified, resizing.
3805 (unless (window-sizable-p parent
(- new-size
) horizontal
)
3806 ;; If we cannot resize the parent give up.
3807 (error "Window %s too small for splitting" parent
)))
3809 (if horizontal window-safe-min-width window-safe-min-height
))
3810 (< (- old-size new-size
)
3811 (if horizontal window-safe-min-width window-safe-min-height
)))
3812 ;; SIZE specification violates minimum size restrictions.
3813 (error "Window %s too small for splitting" window
)))
3815 (window--resize-reset frame horizontal
)
3818 ;; Make new-parent non-nil if we need a new parent window;
3819 ;; either because we want to nest or because WINDOW is not
3821 (or window-combination-limit
3822 (not (window-combined-p window horizontal
))))
3824 ;; Make new-normal the normal size of the new window.
3826 (size (/ (float new-size
) (if new-parent old-size parent-size
)))
3828 (resize (/ 1.0 (1+ (window-combinations parent horizontal
))))
3829 (t (/ (window-normal-size window horizontal
) 2.0))))
3832 ;; Try to get space from OLD's siblings. We could go "up" and
3833 ;; try getting additional space from surrounding windows but
3834 ;; we won't be able to return space to those windows when we
3835 ;; delete the one we create here. Hence we do not go up.
3837 (window--resize-child-windows parent
(- new-size
) horizontal
)
3838 (let* ((normal (- 1.0 new-normal
))
3839 (sub (window-child parent
)))
3841 (set-window-new-normal
3842 sub
(* (window-normal-size sub horizontal
) normal
))
3843 (setq sub
(window-right sub
)))))
3844 ;; Get entire space from WINDOW.
3845 (set-window-new-total window
(- old-size new-size
))
3846 (window--resize-this-window window
(- new-size
) horizontal
)
3847 (set-window-new-normal
3848 window
(- (if new-parent
1.0 (window-normal-size window horizontal
))
3851 (let* ((new (split-window-internal window new-size side new-normal
)))
3852 ;; Assign window-side parameters, if any.
3853 (when (eq window-combination-resize
'side
)
3856 (window-side window-side
)
3857 ((eq side
'above
) 'top
)
3858 ((eq side
'below
) 'bottom
)
3860 ;; We made a new side window.
3861 (set-window-parameter new
'window-side window-side
)
3862 (when (and new-parent
(window-parameter window
'window-side
))
3863 ;; We've been splitting a side root window. Give the
3864 ;; new parent the same window-side parameter.
3865 (set-window-parameter
3866 (window-parent new
) 'window-side window-side
))))
3868 (run-window-configuration-change-hook frame
)
3869 (window--check frame
)
3870 ;; Always return the new window.
3873 ;; I think this should be the default; I think people will prefer it--rms.
3874 (defcustom split-window-keep-point t
3875 "If non-nil, \\[split-window-below] preserves point in the new window.
3876 If nil, adjust point in the two windows to minimize redisplay.
3877 This option applies only to `split-window-below' and functions
3878 that call it. The low-level `split-window' function always keeps
3879 the original point in both windows."
3883 (defun split-window-below (&optional size
)
3884 "Split the selected window into two windows, one above the other.
3885 The selected window is above. The newly split-off window is
3886 below, and displays the same buffer. Return the new window.
3888 If optional argument SIZE is omitted or nil, both windows get the
3889 same height, or close to it. If SIZE is positive, the upper
3890 \(selected) window gets SIZE lines. If SIZE is negative, the
3891 lower (new) window gets -SIZE lines.
3893 If the variable `split-window-keep-point' is non-nil, both
3894 windows get the same value of point as the selected window.
3895 Otherwise, the window starts are chosen so as to minimize the
3896 amount of redisplay; this is convenient on slow terminals."
3898 (let ((old-window (selected-window))
3899 (old-point (window-point))
3900 (size (and size
(prefix-numeric-value size
)))
3901 moved-by-window-height moved new-window bottom
)
3902 (when (and size
(< size
0) (< (- size
) window-min-height
))
3903 ;; `split-window' would not signal an error here.
3904 (error "Size of new window too small"))
3905 (setq new-window
(split-window nil size
))
3906 (unless split-window-keep-point
3907 (with-current-buffer (window-buffer)
3908 ;; Use `save-excursion' around vertical movements below
3909 ;; (Bug#10971). Note: When the selected window's buffer has a
3910 ;; header line, up to two lines of the buffer may not show up
3911 ;; in the resulting configuration.
3913 (goto-char (window-start))
3914 (setq moved
(vertical-motion (window-height)))
3915 (set-window-start new-window
(point))
3916 (when (> (point) (window-point new-window
))
3917 (set-window-point new-window
(point)))
3918 (when (= moved
(window-height))
3919 (setq moved-by-window-height t
)
3920 (vertical-motion -
1))
3921 (setq bottom
(point)))
3922 (and moved-by-window-height
3924 (set-window-point old-window
(1- bottom
)))
3925 (and moved-by-window-height
3926 (<= (window-start new-window
) old-point
)
3927 (set-window-point new-window old-point
)
3928 (select-window new-window
))))
3929 ;; Always copy quit-restore parameter in interactive use.
3930 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
3932 (set-window-parameter new-window
'quit-restore quit-restore
)))
3935 (defalias 'split-window-vertically
'split-window-below
)
3937 (defun split-window-right (&optional size
)
3938 "Split the selected window into two side-by-side windows.
3939 The selected window is on the left. The newly split-off window
3940 is on the right, and displays the same buffer. Return the new
3943 If optional argument SIZE is omitted or nil, both windows get the
3944 same width, or close to it. If SIZE is positive, the left-hand
3945 \(selected) window gets SIZE columns. If SIZE is negative, the
3946 right-hand (new) window gets -SIZE columns. Here, SIZE includes
3947 the width of the window's scroll bar; if there are no scroll
3948 bars, it includes the width of the divider column to the window's
3951 (let ((old-window (selected-window))
3952 (size (and size
(prefix-numeric-value size
)))
3954 (when (and size
(< size
0) (< (- size
) window-min-width
))
3955 ;; `split-window' would not signal an error here.
3956 (error "Size of new window too small"))
3957 (setq new-window
(split-window nil size t
))
3958 ;; Always copy quit-restore parameter in interactive use.
3959 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
3961 (set-window-parameter new-window
'quit-restore quit-restore
)))
3964 (defalias 'split-window-horizontally
'split-window-right
)
3966 ;;; Balancing windows.
3968 ;; The following routine uses the recycled code from an old version of
3969 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
3970 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
3971 ;; more readable (FWIW we'd need three loops - one to calculate the
3972 ;; minimum sizes per window, one to enlarge or shrink windows until the
3973 ;; new parent-size matches, and one where we shrink the largest/enlarge
3974 ;; the smallest window).
3975 (defun balance-windows-2 (window horizontal
)
3976 "Subroutine of `balance-windows-1'.
3977 WINDOW must be a vertical combination (horizontal if HORIZONTAL
3979 (let* ((first (window-child window
))
3981 (number-of-children 0)
3982 (parent-size (window-new-total window
))
3983 (total-sum parent-size
)
3984 failed size sub-total sub-delta sub-amount rest
)
3986 (setq number-of-children
(1+ number-of-children
))
3987 (when (window-size-fixed-p sub horizontal
)
3989 (- total-sum
(window-total-size sub horizontal
)))
3990 (set-window-new-normal sub
'ignore
))
3991 (setq sub
(window-right sub
)))
3994 (while (and failed
(> number-of-children
0))
3995 (setq size
(/ total-sum number-of-children
))
3998 (while (and sub
(not failed
))
3999 ;; Ignore child windows that should be ignored or are stuck.
4000 (unless (window--resize-child-windows-skip-p sub
)
4001 (setq sub-total
(window-total-size sub horizontal
))
4002 (setq sub-delta
(- size sub-total
))
4004 (window-sizable sub sub-delta horizontal
))
4005 ;; Register the new total size for this child window.
4006 (set-window-new-total sub
(+ sub-total sub-amount
))
4007 (unless (= sub-amount sub-delta
)
4008 (setq total-sum
(- total-sum sub-total sub-amount
))
4009 (setq number-of-children
(1- number-of-children
))
4010 ;; We failed and need a new round.
4012 (set-window-new-normal sub
'skip
)))
4013 (setq sub
(window-right sub
))))
4015 (setq rest
(% total-sum number-of-children
))
4016 ;; Fix rounding by trying to enlarge non-stuck windows by one line
4017 ;; (column) until `rest' is zero.
4019 (while (and sub
(> rest
0))
4020 (unless (window--resize-child-windows-skip-p window
)
4021 (set-window-new-total sub
1 t
)
4022 (setq rest
(1- rest
)))
4023 (setq sub
(window-right sub
)))
4025 ;; Fix rounding by trying to enlarge stuck windows by one line
4026 ;; (column) until `rest' equals zero.
4028 (while (and sub
(> rest
0))
4029 (unless (eq (window-new-normal sub
) 'ignore
)
4030 (set-window-new-total sub
1 t
)
4031 (setq rest
(1- rest
)))
4032 (setq sub
(window-right sub
)))
4036 ;; Record new normal sizes.
4037 (set-window-new-normal
4038 sub
(/ (if (eq (window-new-normal sub
) 'ignore
)
4039 (window-total-size sub horizontal
)
4040 (window-new-total sub
))
4041 (float parent-size
)))
4042 ;; Recursively balance each window's child windows.
4043 (balance-windows-1 sub horizontal
)
4044 (setq sub
(window-right sub
)))))
4046 (defun balance-windows-1 (window &optional horizontal
)
4047 "Subroutine of `balance-windows'."
4048 (if (window-child window
)
4049 (let ((sub (window-child window
)))
4050 (if (window-combined-p sub horizontal
)
4051 (balance-windows-2 window horizontal
)
4052 (let ((size (window-new-total window
)))
4054 (set-window-new-total sub size
)
4055 (balance-windows-1 sub horizontal
)
4056 (setq sub
(window-right sub
))))))))
4058 (defun balance-windows (&optional window-or-frame
)
4059 "Balance the sizes of windows of WINDOW-OR-FRAME.
4060 WINDOW-OR-FRAME is optional and defaults to the selected frame.
4061 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
4062 windows of that frame. If WINDOW-OR-FRAME denotes a window,
4063 recursively balance the sizes of all child windows of that
4068 ((or (not window-or-frame
)
4069 (frame-live-p window-or-frame
))
4070 (frame-root-window window-or-frame
))
4071 ((or (window-live-p window-or-frame
)
4072 (window-child window-or-frame
))
4075 (error "Not a window or frame %s" window-or-frame
))))
4076 (frame (window-frame window
)))
4077 ;; Balance vertically.
4078 (window--resize-reset (window-frame window
))
4079 (balance-windows-1 window
)
4080 (window-resize-apply frame
)
4081 ;; Balance horizontally.
4082 (window--resize-reset (window-frame window
) t
)
4083 (balance-windows-1 window t
)
4084 (window-resize-apply frame t
)))
4086 (defun window-fixed-size-p (&optional window direction
)
4087 "Return t if WINDOW cannot be resized in DIRECTION.
4088 WINDOW defaults to the selected window. DIRECTION can be
4089 nil (i.e. any), `height' or `width'."
4090 (with-current-buffer (window-buffer window
)
4091 (when (and (boundp 'window-size-fixed
) window-size-fixed
)
4093 (member (cons direction window-size-fixed
)
4094 '((height . width
) (width . height
))))))))
4096 ;;; A different solution to balance-windows.
4097 (defvar window-area-factor
1
4098 "Factor by which the window area should be over-estimated.
4099 This is used by `balance-windows-area'.
4100 Changing this globally has no effect.")
4101 (make-variable-buffer-local 'window-area-factor
)
4103 (defun balance-windows-area-adjust (window delta horizontal
)
4104 "Wrapper around `window-resize' with error checking.
4105 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
4106 ;; `window-resize' may fail if delta is too large.
4107 (while (>= (abs delta
) 1)
4110 (window-resize window delta horizontal
)
4113 ;;(message "adjust: %s" (error-message-string err))
4114 (setq delta
(/ delta
2))))))
4116 (defun balance-windows-area ()
4117 "Make all visible windows the same area (approximately).
4118 See also `window-area-factor' to change the relative size of
4121 (let* ((unchanged 0) (carry 0) (round 0)
4122 ;; Remove fixed-size windows.
4123 (wins (delq nil
(mapcar (lambda (win)
4124 (if (not (window-fixed-size-p win
)) win
))
4125 (window-list nil
'nomini
))))
4128 ;; Resizing a window changes the size of surrounding windows in complex
4129 ;; ways, so it's difficult to balance them all. The introduction of
4130 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
4131 ;; very difficult to do. `balance-window' above takes an off-line
4132 ;; approach: get the whole window tree, then balance it, then try to
4133 ;; adjust the windows so they fit the result.
4134 ;; Here, instead, we take a "local optimization" approach, where we just
4135 ;; go through all the windows several times until nothing needs to be
4136 ;; changed. The main problem with this approach is that it's difficult
4137 ;; to make sure it terminates, so we use some heuristic to try and break
4138 ;; off infinite loops.
4139 ;; After a round without any change, we allow a second, to give a chance
4140 ;; to the carry to propagate a minor imbalance from the end back to
4142 (while (< unchanged
2)
4143 ;; (message "New round")
4144 (setq unchanged
(1+ unchanged
) round
(1+ round
))
4147 (while (progn (setq next
(next-window next
))
4148 (window-fixed-size-p next
)))
4149 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
4151 (< (car (window-edges win
)) (car (window-edges next
))))
4152 (areadiff (/ (- (* (window-height next
) (window-width next
)
4153 (buffer-local-value 'window-area-factor
4154 (window-buffer next
)))
4155 (* (window-height win
) (window-width win
)
4156 (buffer-local-value 'window-area-factor
4157 (window-buffer win
))))
4158 (max (buffer-local-value 'window-area-factor
4159 (window-buffer win
))
4160 (buffer-local-value 'window-area-factor
4161 (window-buffer next
)))))
4163 (+ (window-height win
) (window-height next
))
4164 (+ (window-width win
) (window-width next
))))
4165 (diff (/ areadiff edgesize
)))
4167 ;; Maybe diff is actually closer to 1 than to 0.
4168 (setq diff
(/ (* 3 areadiff
) (* 2 edgesize
))))
4169 (when (and (zerop diff
) (not (zerop areadiff
)))
4170 (setq diff
(/ (+ areadiff carry
) edgesize
))
4171 ;; Change things smoothly.
4172 (if (or (> diff
1) (< diff -
1)) (setq diff
(/ diff
2))))
4174 ;; Make sure negligible differences don't accumulate to
4175 ;; become significant.
4176 (setq carry
(+ carry areadiff
))
4177 ;; This used `adjust-window-trailing-edge' before and uses
4178 ;; `window-resize' now. Error wrapping is still needed.
4179 (balance-windows-area-adjust win diff horiz
)
4181 (let ((change (cons win
(window-edges win
))))
4182 ;; If the same change has been seen already for this window,
4183 ;; we're most likely in an endless loop, so don't count it as
4185 (unless (member change changelog
)
4186 (push change changelog
)
4187 (setq unchanged
0 carry
0)))))))
4188 ;; We've now basically balanced all the windows.
4189 ;; But there may be some minor off-by-one imbalance left over,
4190 ;; so let's do some fine tuning.
4191 ;; (bw-finetune wins)
4192 ;; (message "Done in %d rounds" round)
4195 ;;; Window states, how to get them and how to put them in a window.
4196 (defun window--state-get-1 (window &optional writable
)
4197 "Helper function for `window-state-get'."
4200 ((window-top-child window
) 'vc
)
4201 ((window-left-child window
) 'hc
)
4203 (buffer (window-buffer window
))
4204 (selected (eq window
(selected-window)))
4207 ,@(unless (window-next-sibling window
) `((last . t
)))
4208 (total-height .
,(window-total-size window
))
4209 (total-width .
,(window-total-size window t
))
4210 (normal-height .
,(window-normal-size window
))
4211 (normal-width .
,(window-normal-size window t
))
4212 (combination-limit .
,(window-combination-limit window
))
4213 ,@(let ((parameters (window-parameters window
))
4215 ;; Make copies of those window parameters whose
4216 ;; persistence property is `writable' if WRITABLE is
4217 ;; non-nil and non-nil if WRITABLE is nil.
4218 (dolist (par parameters
)
4219 (let ((pers (cdr (assq (car par
)
4220 window-persistent-parameters
))))
4221 (when (and pers
(or (not writable
) (eq pers
'writable
)))
4222 (setq list
(cons (cons (car par
) (cdr par
)) list
)))))
4223 ;; Add `clone-of' parameter if necessary.
4224 (let ((pers (cdr (assq 'clone-of
4225 window-persistent-parameters
))))
4226 (when (and pers
(or (not writable
) (eq pers
'writable
))
4227 (not (assq 'clone-of list
)))
4228 (setq list
(cons (cons 'clone-of window
) list
))))
4230 `((parameters .
,list
))))
4232 ;; All buffer related things go in here.
4233 (let ((point (window-point window
))
4234 (start (window-start window
)))
4236 ,(buffer-name buffer
)
4237 (selected .
,selected
)
4238 (hscroll .
,(window-hscroll window
))
4239 (fringes .
,(window-fringes window
))
4240 (margins .
,(window-margins window
))
4241 (scroll-bars .
,(window-scroll-bars window
))
4242 (vscroll .
,(window-vscroll window
))
4243 (dedicated .
,(window-dedicated-p window
))
4244 (point .
,(if writable point
4247 'window-point-insertion-type
4249 (start .
,(if writable start
(copy-marker start
)))))))))
4251 (when (memq type
'(vc hc
))
4253 (setq window
(window-child window
))
4255 (setq list
(cons (window--state-get-1 window writable
) list
))
4256 (setq window
(window-right window
)))
4258 (append head tail
)))
4260 (defun window-state-get (&optional window writable
)
4261 "Return state of WINDOW as a Lisp object.
4262 WINDOW can be any window and defaults to the root window of the
4265 Optional argument WRITABLE non-nil means do not use markers for
4266 sampling `window-point' and `window-start'. Together, WRITABLE
4267 and the variable `window-persistent-parameters' specify which
4268 window parameters are saved by this function. WRITABLE should be
4269 non-nil when the return value shall be written to a file and read
4270 back in another session. Otherwise, an application may run into
4271 an `invalid-read-syntax' error while attempting to read back the
4274 The return value can be used as argument for `window-state-put'
4275 to put the state recorded here into an arbitrary window. The
4276 value can be also stored on disk and read back in a new session."
4279 (if (window-valid-p window
)
4281 (error "%s is not a live or internal window" window
))
4282 (frame-root-window)))
4283 ;; The return value is a cons whose car specifies some constraints on
4284 ;; the size of WINDOW. The cdr lists the states of the child windows
4287 ;; Frame related things would go into a function, say `frame-state',
4288 ;; calling `window-state-get' to insert the frame's root window.
4289 `((min-height .
,(window-min-size window
))
4290 (min-width .
,(window-min-size window t
))
4291 (min-height-ignore .
,(window-min-size window nil t
))
4292 (min-width-ignore .
,(window-min-size window t t
))
4293 (min-height-safe .
,(window-min-size window nil
'safe
))
4294 (min-width-safe .
,(window-min-size window t
'safe
)))
4295 (window--state-get-1 window writable
)))
4297 (defvar window-state-put-list nil
4298 "Helper variable for `window-state-put'.")
4300 (defun window--state-put-1 (state &optional window ignore totals
)
4301 "Helper function for `window-state-put'."
4302 (let ((type (car state
)))
4303 (setq state
(cdr state
))
4306 ;; For a leaf window just add unprocessed entries to
4307 ;; `window-state-put-list'.
4308 (push (cons window state
) window-state-put-list
))
4309 ((memq type
'(vc hc
))
4310 (let* ((horizontal (eq type
'hc
))
4311 (total (window-total-size window horizontal
))
4314 (dolist (item state
)
4315 ;; Find the next child window. WINDOW always points to the
4316 ;; real window that we want to fill with what we find here.
4317 (when (memq (car item
) '(leaf vc hc
))
4318 (if (assq 'last item
)
4319 ;; The last child window. Below `window--state-put-1'
4320 ;; will put into it whatever ITEM has in store.
4322 ;; Not the last child window, prepare for splitting
4323 ;; WINDOW. SIZE is the new (and final) size of the old
4328 (cdr (assq (if horizontal
'total-width
'total-height
) item
))
4329 ;; Use normalized size and round.
4332 (if horizontal
'normal-width
'normal-height
)
4335 ;; Use safe sizes, we try to resize later.
4336 (setq size
(max size
(if horizontal
4337 window-safe-min-height
4338 window-safe-min-width
)))
4340 (if (window-sizable-p window
(- size
) horizontal
'safe
)
4341 (let* ((window-combination-limit
4342 (assq 'combination-limit item
)))
4343 ;; We must inherit the combination limit, otherwise
4344 ;; we might mess up handling of atomic and side
4346 (setq new
(split-window window size horizontal
)))
4347 ;; Give up if we can't resize window down to safe sizes.
4348 (error "Cannot resize window %s" window
))
4352 ;; When creating the first child window add for parent
4353 ;; unprocessed entries to `window-state-put-list'.
4354 (setq window-state-put-list
4355 (cons (cons (window-parent window
) state
)
4356 window-state-put-list
))))
4358 ;; Now process the current window (either the one we've just
4359 ;; split or the last child of its parent).
4360 (window--state-put-1 item window ignore totals
)
4361 ;; Continue with the last window split off.
4362 (setq window new
))))))))
4364 (defun window--state-put-2 (ignore)
4365 "Helper function for `window-state-put'."
4366 (dolist (item window-state-put-list
)
4367 (let ((window (car item
))
4368 (combination-limit (cdr (assq 'combination-limit item
)))
4369 (parameters (cdr (assq 'parameters item
)))
4370 (state (cdr (assq 'buffer item
))))
4371 (when combination-limit
4372 (set-window-combination-limit window combination-limit
))
4373 ;; Reset window's parameters and assign saved ones (we might want
4374 ;; a `remove-window-parameters' function here).
4375 (dolist (parameter (window-parameters window
))
4376 (set-window-parameter window
(car parameter
) nil
))
4378 (dolist (parameter parameters
)
4379 (set-window-parameter window
(car parameter
) (cdr parameter
))))
4380 ;; Process buffer related state.
4382 ;; We don't want to raise an error here so we create a buffer if
4384 (set-window-buffer window
(get-buffer-create (car state
)))
4385 (with-current-buffer (window-buffer window
)
4386 (set-window-hscroll window
(cdr (assq 'hscroll state
)))
4387 (apply 'set-window-fringes
4388 (cons window
(cdr (assq 'fringes state
))))
4389 (let ((margins (cdr (assq 'margins state
))))
4390 (set-window-margins window
(car margins
) (cdr margins
)))
4391 (let ((scroll-bars (cdr (assq 'scroll-bars state
))))
4392 (set-window-scroll-bars
4393 window
(car scroll-bars
) (nth 2 scroll-bars
) (nth 3 scroll-bars
)))
4394 (set-window-vscroll window
(cdr (assq 'vscroll state
)))
4395 ;; Adjust vertically.
4396 (if (memq window-size-fixed
'(t height
))
4397 ;; A fixed height window, try to restore the original size.
4398 (let ((delta (- (cdr (assq 'total-height item
))
4399 (window-total-height window
)))
4401 (when (window--resizable-p window delta
)
4402 (window-resize window delta
)))
4403 ;; Else check whether the window is not high enough.
4404 (let* ((min-size (window-min-size window nil ignore
))
4405 (delta (- min-size
(window-total-size window
))))
4406 (when (and (> delta
0)
4407 (window--resizable-p window delta nil ignore
))
4408 (window-resize window delta nil ignore
))))
4409 ;; Adjust horizontally.
4410 (if (memq window-size-fixed
'(t width
))
4411 ;; A fixed width window, try to restore the original size.
4412 (let ((delta (- (cdr (assq 'total-width item
))
4413 (window-total-width window
)))
4415 (when (window--resizable-p window delta
)
4416 (window-resize window delta
)))
4417 ;; Else check whether the window is not wide enough.
4418 (let* ((min-size (window-min-size window t ignore
))
4419 (delta (- min-size
(window-total-size window t
))))
4420 (when (and (> delta
0)
4421 (window--resizable-p window delta t ignore
))
4422 (window-resize window delta t ignore
))))
4423 ;; Set dedicated status.
4424 (set-window-dedicated-p window
(cdr (assq 'dedicated state
)))
4425 ;; Install positions (maybe we should do this after all windows
4426 ;; have been created and sized).
4428 (set-window-start window
(cdr (assq 'start state
)))
4429 (set-window-point window
(cdr (assq 'point state
))))
4430 ;; Select window if it's the selected one.
4431 (when (cdr (assq 'selected state
))
4432 (select-window window
)))))))
4434 (defun window-state-put (state &optional window ignore
)
4435 "Put window state STATE into WINDOW.
4436 STATE should be the state of a window returned by an earlier
4437 invocation of `window-state-get'. Optional argument WINDOW must
4438 specify a live window and defaults to the selected one.
4440 Optional argument IGNORE non-nil means ignore minimum window
4441 sizes and fixed size restrictions. IGNORE equal `safe' means
4442 windows can get as small as `window-safe-min-height' and
4443 `window-safe-min-width'."
4444 (setq window
(window-normalize-window window t
))
4445 (let* ((frame (window-frame window
))
4447 ;; We check here (1) whether the total sizes of root window of
4448 ;; STATE and that of WINDOW are equal so we can avoid
4449 ;; calculating new sizes, and (2) if we do have to resize
4450 ;; whether we can do so without violating size restrictions.
4452 (and (= (window-total-size window
)
4453 (cdr (assq 'total-height state
)))
4454 (= (window-total-size window t
)
4455 (cdr (assq 'total-width state
)))))
4456 (min-height (cdr (assq 'min-height head
)))
4457 (min-width (cdr (assq 'min-width head
))))
4458 (if (and (not totals
)
4459 (or (> min-height
(window-total-size window
))
4460 (> min-width
(window-total-size window t
)))
4462 (and (setq min-height
4463 (cdr (assq 'min-height-ignore head
)))
4465 (cdr (assq 'min-width-ignore head
)))
4466 (or (> min-height
(window-total-size window
))
4467 (> min-width
(window-total-size window t
)))
4468 (or (not (eq ignore
'safe
))
4469 (and (setq min-height
4470 (cdr (assq 'min-height-safe head
)))
4472 (cdr (assq 'min-width-safe head
)))
4474 (window-total-size window
))
4476 (window-total-size window t
))))))))
4477 ;; The check above might not catch all errors due to rounding
4478 ;; issues - so IGNORE equal 'safe might not always produce the
4479 ;; minimum possible state. But such configurations hardly make
4481 (error "Window %s too small to accommodate state" window
)
4482 (setq state
(cdr state
))
4483 (setq window-state-put-list nil
)
4484 ;; Work on the windows of a temporary buffer to make sure that
4485 ;; splitting proceeds regardless of any buffer local values of
4486 ;; `window-size-fixed'. Release that buffer after the buffers of
4487 ;; all live windows have been set by `window--state-put-2'.
4489 (set-window-buffer window
(current-buffer))
4490 (window--state-put-1 state window nil totals
)
4491 (window--state-put-2 ignore
))
4492 (window--check frame
))))
4494 (defun display-buffer-record-window (type window buffer
)
4495 "Record information for window used by `display-buffer'.
4496 TYPE specifies the type of the calling operation and must be one
4497 of the symbols 'reuse (when WINDOW existed already and was
4498 reused for displaying BUFFER), 'window (when WINDOW was created
4499 on an already existing frame), or 'frame (when WINDOW was
4500 created on a new frame). WINDOW is the window used for or created
4501 by the `display-buffer' routines. BUFFER is the buffer that
4504 This function installs or updates the quit-restore parameter of
4505 WINDOW. The quit-restore parameter is a list of four elements:
4506 The first element is one of the symbols 'window, 'frame, 'same or
4507 'other. The second element is either one of the symbols 'window
4508 or 'frame or a list whose elements are the buffer previously
4509 shown in the window, that buffer's window start and window point,
4510 and the window's height. The third element is the window
4511 selected at the time the parameter was created. The fourth
4515 (if (eq (window-buffer window
) buffer
)
4516 ;; WINDOW shows BUFFER already.
4517 (when (consp (window-parameter window
'quit-restore
))
4518 ;; If WINDOW has a quit-restore parameter, reset its car.
4519 (setcar (window-parameter window
'quit-restore
) 'same
))
4520 ;; WINDOW shows another buffer.
4521 (set-window-parameter
4522 window
'quit-restore
4524 ;; A quadruple of WINDOW's buffer, start, point and height.
4525 (list (window-buffer window
) (window-start window
)
4526 (window-point window
) (window-total-size window
))
4527 (selected-window) buffer
))))
4529 ;; WINDOW has been created on an existing frame.
4530 (set-window-parameter
4531 window
'quit-restore
4532 (list 'window
'window
(selected-window) buffer
)))
4534 ;; WINDOW has been created on a new frame.
4535 (set-window-parameter
4536 window
'quit-restore
4537 (list 'frame
'frame
(selected-window) buffer
)))))
4539 (defcustom display-buffer-function nil
4540 "If non-nil, function to call to handle `display-buffer'.
4541 It will receive two args, the buffer and a flag which if non-nil
4542 means that the currently selected window is not acceptable. It
4543 should choose or create a window, display the specified buffer in
4544 it, and return the window.
4546 The specified function should call `display-buffer-record-window'
4547 with corresponding arguments to set up the quit-restore parameter
4548 of the window used."
4551 (function :tag
"function"))
4554 ;; Eventually, we want to turn this into a defvar; instead of
4555 ;; customizing this, the user should use a `pop-up-frame-parameters'
4556 ;; alist entry in `display-buffer-base-action'.
4557 (defcustom pop-up-frame-alist nil
4558 "Alist of parameters for automatically generated new frames.
4559 If non-nil, the value you specify here is used by the default
4560 `pop-up-frame-function' for the creation of new frames.
4562 Since `pop-up-frame-function' is used by `display-buffer' for
4563 making new frames, any value specified here by default affects
4564 the automatic generation of new frames via `display-buffer' and
4565 all functions based on it. The behavior of `make-frame' is not
4566 affected by this variable."
4567 :type
'(repeat (cons :format
"%v"
4568 (symbol :tag
"Parameter")
4569 (sexp :tag
"Value")))
4572 (defcustom pop-up-frame-function
4573 (lambda () (make-frame pop-up-frame-alist
))
4574 "Function used by `display-buffer' for creating a new frame.
4575 This function is called with no arguments and should return a new
4576 frame. The default value calls `make-frame' with the argument
4577 `pop-up-frame-alist'."
4581 (defcustom special-display-buffer-names nil
4582 "List of names of buffers that should be displayed specially.
4583 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4584 its name is in this list, displays the buffer in a way specified
4585 by `special-display-function'. `special-display-popup-frame'
4586 \(the default for `special-display-function') usually displays
4587 the buffer in a separate frame made with the parameters specified
4588 by `special-display-frame-alist'. If `special-display-function'
4589 has been set to some other function, that function is called with
4590 the buffer as first, and nil as second argument.
4592 Alternatively, an element of this list can be specified as
4593 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
4594 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4595 `special-display-popup-frame' will interpret such pairs as frame
4596 parameters when it creates a special frame, overriding the
4597 corresponding values from `special-display-frame-alist'.
4599 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4600 `special-display-popup-frame' displays that buffer in the
4601 selected window. If FRAME-PARAMETERS contains (same-frame . t),
4602 it displays that buffer in a window on the selected frame.
4604 If `special-display-function' specifies some other function than
4605 `special-display-popup-frame', that function is called with the
4606 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
4609 Finally, an element of this list can be also specified as
4610 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
4611 `special-display-popup-frame' will call FUNCTION with the buffer
4612 named BUFFER-NAME as first argument, and OTHER-ARGS as the
4615 Any alternative function specified here is responsible for
4616 setting up the quit-restore parameter of the window used.
4618 If this variable appears \"not to work\", because you added a
4619 name to it but the corresponding buffer is displayed in the
4620 selected window, look at the values of `same-window-buffer-names'
4621 and `same-window-regexps'. Those variables take precedence over
4624 See also `special-display-regexps'."
4626 (choice :tag
"Buffer"
4628 (string :format
"%v")
4629 (cons :tag
"With parameters"
4632 (string :format
"%v")
4633 (repeat :tag
"Parameters"
4635 (symbol :tag
"Parameter")
4636 (sexp :tag
"Value"))))
4637 (list :tag
"With function"
4640 (string :format
"%v")
4641 (function :tag
"Function")
4642 (repeat :tag
"Arguments" (sexp)))))
4645 (make-obsolete-variable 'special-display-buffer-names
'display-buffer-alist
"24.3")
4646 (put 'special-display-buffer-names
'risky-local-variable t
)
4648 (defcustom special-display-regexps nil
4649 "List of regexps saying which buffers should be displayed specially.
4650 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4651 any regexp in this list matches its name, displays it specially
4652 using `special-display-function'. `special-display-popup-frame'
4653 \(the default for `special-display-function') usually displays
4654 the buffer in a separate frame made with the parameters specified
4655 by `special-display-frame-alist'. If `special-display-function'
4656 has been set to some other function, that function is called with
4657 the buffer as first, and nil as second argument.
4659 Alternatively, an element of this list can be specified as
4660 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
4661 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4662 `special-display-popup-frame' will then interpret these pairs as
4663 frame parameters when creating a special frame for a buffer whose
4664 name matches REGEXP, overriding the corresponding values from
4665 `special-display-frame-alist'.
4667 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4668 `special-display-popup-frame' displays buffers matching REGEXP in
4669 the selected window. (same-frame . t) in FRAME-PARAMETERS means
4670 to display such buffers in a window on the selected frame.
4672 If `special-display-function' specifies some other function than
4673 `special-display-popup-frame', that function is called with the
4674 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
4677 Finally, an element of this list can be also specified as
4678 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
4679 will then call FUNCTION with the buffer whose name matched
4680 REGEXP as first, and OTHER-ARGS as second argument.
4682 Any alternative function specified here is responsible for
4683 setting up the quit-restore parameter of the window used.
4685 If this variable appears \"not to work\", because you added a
4686 name to it but the corresponding buffer is displayed in the
4687 selected window, look at the values of `same-window-buffer-names'
4688 and `same-window-regexps'. Those variables take precedence over
4691 See also `special-display-buffer-names'."
4693 (choice :tag
"Buffer"
4695 (regexp :format
"%v")
4696 (cons :tag
"With parameters"
4699 (regexp :format
"%v")
4700 (repeat :tag
"Parameters"
4702 (symbol :tag
"Parameter")
4703 (sexp :tag
"Value"))))
4704 (list :tag
"With function"
4707 (regexp :format
"%v")
4708 (function :tag
"Function")
4709 (repeat :tag
"Arguments" (sexp)))))
4712 (make-obsolete-variable 'special-display-regexps
'display-buffer-alist
"24.3")
4713 (put 'special-display-regexps
'risky-local-variable t
)
4715 (defun special-display-p (buffer-name)
4716 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
4717 More precisely, return t if `special-display-buffer-names' or
4718 `special-display-regexps' contain a string entry equaling or
4719 matching BUFFER-NAME. If `special-display-buffer-names' or
4720 `special-display-regexps' contain a list entry whose car equals
4721 or matches BUFFER-NAME, the return value is the cdr of that
4725 ((member buffer-name special-display-buffer-names
)
4727 ((setq tmp
(assoc buffer-name special-display-buffer-names
))
4730 (dolist (regexp special-display-regexps
)
4733 (when (string-match-p regexp buffer-name
)
4735 ((and (consp regexp
) (stringp (car regexp
))
4736 (string-match-p (car regexp
) buffer-name
))
4737 (throw 'found
(cdr regexp
))))))))))
4739 (defcustom special-display-frame-alist
4740 '((height .
14) (width .
80) (unsplittable . t
))
4741 "Alist of parameters for special frames.
4742 Special frames are used for buffers whose names are listed in
4743 `special-display-buffer-names' and for buffers whose names match
4744 one of the regular expressions in `special-display-regexps'.
4746 This variable can be set in your init file, like this:
4748 (setq special-display-frame-alist '((width . 80) (height . 20)))
4750 These supersede the values given in `default-frame-alist'."
4751 :type
'(repeat (cons :format
"%v"
4752 (symbol :tag
"Parameter")
4753 (sexp :tag
"Value")))
4755 (make-obsolete-variable 'special-display-frame-alist
'display-buffer-alist
"24.3")
4757 (defun special-display-popup-frame (buffer &optional args
)
4758 "Pop up a frame displaying BUFFER and return its window.
4759 If BUFFER is already displayed in a visible or iconified frame,
4760 raise that frame. Otherwise, display BUFFER in a new frame.
4762 Optional argument ARGS is a list specifying additional
4765 If ARGS is an alist, use it as a list of frame parameters. If
4766 these parameters contain (same-window . t), display BUFFER in
4767 the selected window. If they contain (same-frame . t), display
4768 BUFFER in a window of the selected frame.
4770 If ARGS is a list whose car is a symbol, use (car ARGS) as a
4771 function to do the work. Pass it BUFFER as first argument,
4772 and (cdr ARGS) as second."
4773 (if (and args
(symbolp (car args
)))
4774 (apply (car args
) buffer
(cdr args
))
4775 (let ((window (get-buffer-window buffer
0)))
4777 ;; If we have a window already, make it visible.
4779 (let ((frame (window-frame window
)))
4780 (make-frame-visible frame
)
4782 (display-buffer-record-window 'reuse window buffer
)
4784 ;; Reuse the current window if the user requested it.
4785 (when (cdr (assq 'same-window args
))
4787 (progn (switch-to-buffer buffer nil t
) (selected-window))
4789 ;; Stay on the same frame if requested.
4790 (when (or (cdr (assq 'same-frame args
)) (cdr (assq 'same-window args
)))
4791 (let* ((pop-up-windows t
)
4793 special-display-buffer-names special-display-regexps
)
4794 (display-buffer buffer
)))
4795 ;; If no window yet, make one in a new frame.
4797 (with-current-buffer buffer
4798 (make-frame (append args special-display-frame-alist
))))
4799 (window (frame-selected-window frame
)))
4800 (display-buffer-record-window 'frame window buffer
)
4801 (unless (eq buffer
(window-buffer window
))
4802 (set-window-buffer window buffer
)
4803 (set-window-prev-buffers window nil
))
4804 (set-window-dedicated-p window t
)
4807 (defcustom special-display-function
'special-display-popup-frame
4808 "Function to call for displaying special buffers.
4809 This function is called with two arguments - the buffer and,
4810 optionally, a list - and should return a window displaying that
4811 buffer. The default value usually makes a separate frame for the
4812 buffer using `special-display-frame-alist' to specify the frame
4813 parameters. See the definition of `special-display-popup-frame'
4814 for how to specify such a function.
4816 A buffer is special when its name is either listed in
4817 `special-display-buffer-names' or matches a regexp in
4818 `special-display-regexps'.
4820 The specified function should call `display-buffer-record-window'
4821 with corresponding arguments to set up the quit-restore parameter
4822 of the window used."
4825 (make-obsolete-variable 'special-display-function
'display-buffer-alist
"24.3")
4827 (defcustom same-window-buffer-names nil
4828 "List of names of buffers that should appear in the \"same\" window.
4829 `display-buffer' and `pop-to-buffer' show a buffer whose name is
4830 on this list in the selected rather than some other window.
4832 An element of this list can be a cons cell instead of just a
4833 string. In that case, the cell's car must be a string specifying
4834 the buffer name. This is for compatibility with
4835 `special-display-buffer-names'; the cdr of the cons cell is
4838 See also `same-window-regexps'."
4839 :type
'(repeat (string :format
"%v"))
4842 (defcustom same-window-regexps nil
4843 "List of regexps saying which buffers should appear in the \"same\" window.
4844 `display-buffer' and `pop-to-buffer' show a buffer whose name
4845 matches a regexp on this list in the selected rather than some
4848 An element of this list can be a cons cell instead of just a
4849 string. In that case, the cell's car must be a regexp matching
4850 the buffer name. This is for compatibility with
4851 `special-display-regexps'; the cdr of the cons cell is ignored.
4853 See also `same-window-buffer-names'."
4854 :type
'(repeat (regexp :format
"%v"))
4857 (defun same-window-p (buffer-name)
4858 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
4859 This function returns non-nil if `display-buffer' or
4860 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
4861 selected rather than (as usual) some other window. See
4862 `same-window-buffer-names' and `same-window-regexps'."
4864 ((not (stringp buffer-name
)))
4865 ;; The elements of `same-window-buffer-names' can be buffer
4866 ;; names or cons cells whose cars are buffer names.
4867 ((member buffer-name same-window-buffer-names
))
4868 ((assoc buffer-name same-window-buffer-names
))
4870 (dolist (regexp same-window-regexps
)
4871 ;; The elements of `same-window-regexps' can be regexps
4872 ;; or cons cells whose cars are regexps.
4873 (when (or (and (stringp regexp
)
4874 (string-match-p regexp buffer-name
))
4875 (and (consp regexp
) (stringp (car regexp
))
4876 (string-match-p (car regexp
) buffer-name
)))
4877 (throw 'found t
)))))))
4879 (defcustom pop-up-frames nil
4880 "Whether `display-buffer' should make a separate frame.
4881 If nil, never make a separate frame.
4882 If the value is `graphic-only', make a separate frame
4883 on graphic displays only.
4884 Any other non-nil value means always make a separate frame."
4886 (const :tag
"Never" nil
)
4887 (const :tag
"On graphic displays only" graphic-only
)
4888 (const :tag
"Always" t
))
4891 (defcustom display-buffer-reuse-frames nil
4892 "Non-nil means `display-buffer' should reuse frames.
4893 If the buffer in question is already displayed in a frame, raise
4899 (make-obsolete-variable
4900 'display-buffer-reuse-frames
4901 "use a `reusable-frames' alist entry in `display-buffer-alist'."
4904 (defcustom pop-up-windows t
4905 "Non-nil means `display-buffer' should make a new window."
4909 (defcustom split-window-preferred-function
'split-window-sensibly
4910 "Function called by `display-buffer' routines to split a window.
4911 This function is called with a window as single argument and is
4912 supposed to split that window and return the new window. If the
4913 window can (or shall) not be split, it is supposed to return nil.
4914 The default is to call the function `split-window-sensibly' which
4915 tries to split the window in a way which seems most suitable.
4916 You can customize the options `split-height-threshold' and/or
4917 `split-width-threshold' in order to have `split-window-sensibly'
4918 prefer either vertical or horizontal splitting.
4920 If you set this to any other function, bear in mind that the
4921 `display-buffer' routines may call this function two times. The
4922 argument of the first call is the largest window on its frame.
4923 If that call fails to return a live window, the function is
4924 called again with the least recently used window as argument. If
4925 that call fails too, `display-buffer' will use an existing window
4926 to display its buffer.
4928 The window selected at the time `display-buffer' was invoked is
4929 still selected when this function is called. Hence you can
4930 compare the window argument with the value of `selected-window'
4931 if you intend to split the selected window instead or if you do
4932 not want to split the selected window."
4937 (defcustom split-height-threshold
80
4938 "Minimum height for splitting windows sensibly.
4939 If this is an integer, `split-window-sensibly' may split a window
4940 vertically only if it has at least this many lines. If this is
4941 nil, `split-window-sensibly' is not allowed to split a window
4942 vertically. If, however, a window is the only window on its
4943 frame, `split-window-sensibly' may split it vertically
4944 disregarding the value of this variable."
4945 :type
'(choice (const nil
) (integer :tag
"lines"))
4949 (defcustom split-width-threshold
160
4950 "Minimum width for splitting windows sensibly.
4951 If this is an integer, `split-window-sensibly' may split a window
4952 horizontally only if it has at least this many columns. If this
4953 is nil, `split-window-sensibly' is not allowed to split a window
4955 :type
'(choice (const nil
) (integer :tag
"columns"))
4959 (defun window-splittable-p (window &optional horizontal
)
4960 "Return non-nil if `split-window-sensibly' may split WINDOW.
4961 Optional argument HORIZONTAL nil or omitted means check whether
4962 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
4963 non-nil means check whether WINDOW may be split horizontally.
4965 WINDOW may be split vertically when the following conditions
4967 - `window-size-fixed' is either nil or equals `width' for the
4969 - `split-height-threshold' is an integer and WINDOW is at least as
4970 high as `split-height-threshold'.
4971 - When WINDOW is split evenly, the emanating windows are at least
4972 `window-min-height' lines tall and can accommodate at least one
4973 line plus - if WINDOW has one - a mode line.
4975 WINDOW may be split horizontally when the following conditions
4977 - `window-size-fixed' is either nil or equals `height' for the
4979 - `split-width-threshold' is an integer and WINDOW is at least as
4980 wide as `split-width-threshold'.
4981 - When WINDOW is split evenly, the emanating windows are at least
4982 `window-min-width' or two (whichever is larger) columns wide."
4983 (when (window-live-p window
)
4984 (with-current-buffer (window-buffer window
)
4986 ;; A window can be split horizontally when its width is not
4987 ;; fixed, it is at least `split-width-threshold' columns wide
4988 ;; and at least twice as wide as `window-min-width' and 2 (the
4989 ;; latter value is hardcoded).
4990 (and (memq window-size-fixed
'(nil height
))
4991 ;; Testing `window-full-width-p' here hardly makes any
4992 ;; sense nowadays. This can be done more intuitively by
4993 ;; setting up `split-width-threshold' appropriately.
4994 (numberp split-width-threshold
)
4995 (>= (window-width window
)
4996 (max split-width-threshold
4997 (* 2 (max window-min-width
2)))))
4998 ;; A window can be split vertically when its height is not
4999 ;; fixed, it is at least `split-height-threshold' lines high,
5000 ;; and it is at least twice as high as `window-min-height' and 2
5001 ;; if it has a mode line or 1.
5002 (and (memq window-size-fixed
'(nil width
))
5003 (numberp split-height-threshold
)
5004 (>= (window-height window
)
5005 (max split-height-threshold
5006 (* 2 (max window-min-height
5007 (if mode-line-format
2 1))))))))))
5009 (defun split-window-sensibly (&optional window
)
5010 "Split WINDOW in a way suitable for `display-buffer'.
5011 WINDOW defaults to the currently selected window.
5012 If `split-height-threshold' specifies an integer, WINDOW is at
5013 least `split-height-threshold' lines tall and can be split
5014 vertically, split WINDOW into two windows one above the other and
5015 return the lower window. Otherwise, if `split-width-threshold'
5016 specifies an integer, WINDOW is at least `split-width-threshold'
5017 columns wide and can be split horizontally, split WINDOW into two
5018 windows side by side and return the window on the right. If this
5019 can't be done either and WINDOW is the only window on its frame,
5020 try to split WINDOW vertically disregarding any value specified
5021 by `split-height-threshold'. If that succeeds, return the lower
5022 window. Return nil otherwise.
5024 By default `display-buffer' routines call this function to split
5025 the largest or least recently used window. To change the default
5026 customize the option `split-window-preferred-function'.
5028 You can enforce this function to not split WINDOW horizontally,
5029 by setting (or binding) the variable `split-width-threshold' to
5030 nil. If, in addition, you set `split-height-threshold' to zero,
5031 chances increase that this function does split WINDOW vertically.
5033 In order to not split WINDOW vertically, set (or bind) the
5034 variable `split-height-threshold' to nil. Additionally, you can
5035 set `split-width-threshold' to zero to make a horizontal split
5036 more likely to occur.
5038 Have a look at the function `window-splittable-p' if you want to
5039 know how `split-window-sensibly' determines whether WINDOW can be
5041 (let ((window (or window
(selected-window))))
5042 (or (and (window-splittable-p window
)
5043 ;; Split window vertically.
5044 (with-selected-window window
5045 (split-window-below)))
5046 (and (window-splittable-p window t
)
5047 ;; Split window horizontally.
5048 (with-selected-window window
5049 (split-window-right)))
5050 (and (eq window
(frame-root-window (window-frame window
)))
5051 (not (window-minibuffer-p window
))
5052 ;; If WINDOW is the only window on its frame and is not the
5053 ;; minibuffer window, try to split it vertically disregarding
5054 ;; the value of `split-height-threshold'.
5055 (let ((split-height-threshold 0))
5056 (when (window-splittable-p window
)
5057 (with-selected-window window
5058 (split-window-below))))))))
5060 (defun window--try-to-split-window (window)
5061 "Try to split WINDOW.
5062 Return value returned by `split-window-preferred-function' if it
5063 represents a live window, nil otherwise."
5064 (and (window-live-p window
)
5065 (not (frame-parameter (window-frame window
) 'unsplittable
))
5067 ;; Since `split-window-preferred-function' might
5068 ;; throw an error use `condition-case'.
5070 (funcall split-window-preferred-function window
)
5072 (and (window-live-p new-window
) new-window
))))
5074 (defun window--frame-usable-p (frame)
5075 "Return FRAME if it can be used to display a buffer."
5076 (when (frame-live-p frame
)
5077 (let ((window (frame-root-window frame
)))
5078 ;; `frame-root-window' may be an internal window which is considered
5079 ;; "dead" by `window-live-p'. Hence if `window' is not live we
5080 ;; implicitly know that `frame' has a visible window we can use.
5081 (unless (and (window-live-p window
)
5082 (or (window-minibuffer-p window
)
5083 ;; If the window is soft-dedicated, the frame is usable.
5084 ;; Actually, even if the window is really dedicated,
5085 ;; the frame is still usable by splitting it.
5086 ;; At least Emacs-22 allowed it, and it is desirable
5087 ;; when displaying same-frame windows.
5088 nil
; (eq t (window-dedicated-p window))
5092 (defcustom even-window-heights t
5093 "If non-nil `display-buffer' will try to even window heights.
5094 Otherwise `display-buffer' will leave the window configuration
5095 alone. Heights are evened only when `display-buffer' chooses a
5096 window that appears above or below the selected window."
5100 (defun window--even-window-heights (window)
5101 "Even heights of WINDOW and selected window.
5102 Do this only if these windows are vertically adjacent to each
5103 other, `even-window-heights' is non-nil, and the selected window
5104 is higher than WINDOW."
5105 (when (and even-window-heights
5106 ;; Even iff WINDOW forms a vertical combination with the
5107 ;; selected window, and WINDOW's height exceeds that of the
5108 ;; selected window, see also bug#11880.
5109 (window-combined-p window
)
5110 (= (window-child-count (window-parent window
)) 2)
5111 (eq (window-parent) (window-parent window
))
5112 (> (window-total-height) (window-total-height window
)))
5113 ;; Don't throw an error if we can't even window heights for
5117 (/ (- (window-total-height window
) (window-total-height)) 2))
5120 (defun window--display-buffer (buffer window type
&optional dedicated
)
5121 "Display BUFFER in WINDOW and make its frame visible.
5122 TYPE must be one of the symbols `reuse', `window' or `frame' and
5123 is passed unaltered to `display-buffer-record-window'. Set
5124 `window-dedicated-p' to DEDICATED if non-nil. Return WINDOW if
5125 BUFFER and WINDOW are live."
5126 (when (and (buffer-live-p buffer
) (window-live-p window
))
5127 (display-buffer-record-window type window buffer
)
5128 (unless (eq buffer
(window-buffer window
))
5129 (set-window-dedicated-p window nil
)
5130 (set-window-buffer window buffer
)
5132 (set-window-dedicated-p window dedicated
))
5133 (when (memq type
'(window frame
))
5134 (set-window-prev-buffers window nil
)))
5137 (defun window--maybe-raise-frame (frame)
5138 (let ((visible (frame-visible-p frame
)))
5139 (unless (or (not visible
)
5140 ;; Assume the selected frame is already visible enough.
5141 (eq frame
(selected-frame))
5142 ;; Assume the frame from which we invoked the
5143 ;; minibuffer is visible.
5144 (and (minibuffer-window-active-p (selected-window))
5145 (eq frame
(window-frame (minibuffer-selected-window)))))
5146 (raise-frame frame
))))
5148 ;; FIXME: Not implemented.
5149 ;; FIXME: By the way, there could be more levels of dedication:
5150 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
5151 ;; the window hasn't been used for something else yet.
5152 ;; - `softly' dedicated only allows reuse when asked explicitly.
5153 ;; - `strongly' never allows reuse.
5154 (defvar display-buffer-mark-dedicated nil
5155 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
5156 The actual non-nil value of this variable will be copied to the
5157 `window-dedicated-p' flag.")
5159 (defconst display-buffer--action-function-custom-type
5160 '(choice :tag
"Function"
5161 (const :tag
"--" ignore
) ; default for insertion
5162 (const display-buffer-reuse-window
)
5163 (const display-buffer-pop-up-window
)
5164 (const display-buffer-same-window
)
5165 (const display-buffer-pop-up-frame
)
5166 (const display-buffer-use-some-window
)
5167 (function :tag
"Other function"))
5168 "Custom type for `display-buffer' action functions.")
5170 (defconst display-buffer--action-custom-type
5171 `(cons :tag
"Action"
5172 (choice :tag
"Action functions"
5173 ,display-buffer--action-function-custom-type
5175 :tag
"List of functions"
5176 ,display-buffer--action-function-custom-type
))
5177 (alist :tag
"Action arguments"
5179 :value-type
(sexp :tag
"Value")))
5180 "Custom type for `display-buffer' actions.")
5182 (defvar display-buffer-overriding-action
'(nil . nil
)
5183 "Overriding action to perform to display a buffer.
5184 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5185 function or a list of functions. Each function should accept two
5186 arguments: a buffer to display and an alist similar to ALIST.
5187 See `display-buffer' for details.")
5188 (put 'display-buffer-overriding-action
'risky-local-variable t
)
5190 (defcustom display-buffer-alist nil
5191 "Alist of conditional actions for `display-buffer'.
5192 This is a list of elements (CONDITION . ACTION), where:
5194 CONDITION is either a regexp matching buffer names, or a function
5195 that takes a buffer and returns a boolean.
5197 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
5198 function or a list of functions. Each such function should
5199 accept two arguments: a buffer to display and an alist of the
5200 same form as ALIST. See `display-buffer' for details."
5201 :type
`(alist :key-type
5202 (choice :tag
"Condition"
5204 (function :tag
"Matcher function"))
5205 :value-type
,display-buffer--action-custom-type
)
5210 (defcustom display-buffer-base-action
'(nil . nil
)
5211 "User-specified default action for `display-buffer'.
5212 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5213 function or a list of functions. Each function should accept two
5214 arguments: a buffer to display and an alist similar to ALIST.
5215 See `display-buffer' for details."
5216 :type display-buffer--action-custom-type
5221 (defconst display-buffer-fallback-action
5222 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
5223 display-buffer-reuse-window
5224 display-buffer--maybe-pop-up-frame-or-window
5225 display-buffer-use-some-window
5226 ;; If all else fails, pop up a new frame.
5227 display-buffer-pop-up-frame
))
5228 "Default fallback action for `display-buffer'.
5229 This is the action used by `display-buffer' if no other actions
5230 specified, e.g. by the user options `display-buffer-alist' or
5231 `display-buffer-base-action'. See `display-buffer'.")
5232 (put 'display-buffer-fallback-action
'risky-local-variable t
)
5234 (defun display-buffer-assq-regexp (buffer-name alist
)
5235 "Retrieve ALIST entry corresponding to BUFFER-NAME."
5237 (dolist (entry alist
)
5238 (let ((key (car entry
)))
5239 (when (or (and (stringp key
)
5240 (string-match-p key buffer-name
))
5241 (and (symbolp key
) (functionp key
)
5242 (funcall key buffer-name alist
)))
5243 (throw 'match
(cdr entry
)))))))
5245 (defvar display-buffer--same-window-action
5246 '(display-buffer-same-window
5247 (inhibit-same-window . nil
))
5248 "A `display-buffer' action for displaying in the same window.")
5249 (put 'display-buffer--same-window-action
'risky-local-variable t
)
5251 (defvar display-buffer--other-frame-action
5252 '((display-buffer-reuse-window
5253 display-buffer-pop-up-frame
)
5254 (reusable-frames .
0)
5255 (inhibit-same-window . t
))
5256 "A `display-buffer' action for displaying in another frame.")
5257 (put 'display-buffer--other-frame-action
'risky-local-variable t
)
5259 (defun display-buffer (buffer-or-name &optional action frame
)
5260 "Display BUFFER-OR-NAME in some window, without selecting it.
5261 BUFFER-OR-NAME must be a buffer or the name of an existing
5262 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
5263 or nil if no such window is found.
5265 Optional argument ACTION should have the form (FUNCTION . ALIST).
5266 FUNCTION is either a function or a list of functions.
5267 ALIST is an arbitrary association list (alist).
5269 Each such FUNCTION should accept two arguments: the buffer to
5270 display and an alist. Based on those arguments, it should either
5271 display the buffer and return the window, or return nil if unable
5272 to display the buffer.
5274 The `display-buffer' function builds a function list and an alist
5275 by combining the functions and alists specified in
5276 `display-buffer-overriding-action', `display-buffer-alist', the
5277 ACTION argument, `display-buffer-base-action', and
5278 `display-buffer-fallback-action' (in order). Then it calls each
5279 function in the combined function list in turn, passing the
5280 buffer as the first argument and the combined alist as the second
5281 argument, until one of the functions returns non-nil.
5283 Available action functions include:
5284 `display-buffer-same-window'
5285 `display-buffer-reuse-window'
5286 `display-buffer-pop-up-frame'
5287 `display-buffer-pop-up-window'
5288 `display-buffer-use-some-window'
5290 Recognized alist entries include:
5292 `inhibit-same-window' -- A non-nil value prevents the same
5293 window from being used for display.
5295 `inhibit-switch-frame' -- A non-nil value prevents any other
5296 frame from being raised or selected,
5297 even if the window is displayed there.
5299 `reusable-frames' -- Value specifies frame(s) to search for a
5300 window that already displays the buffer.
5301 See `display-buffer-reuse-window'.
5303 `pop-up-frame-parameters' -- Value specifies an alist of frame
5304 parameters to give a new frame, if
5307 The ACTION argument to `display-buffer' can also have a non-nil
5308 and non-list value. This means to display the buffer in a window
5309 other than the selected one, even if it is already displayed in
5310 the selected window. If called interactively with a prefix
5311 argument, ACTION is t.
5313 Optional argument FRAME, if non-nil, acts like an additional
5314 ALIST entry (reusable-frames . FRAME), specifying the frame(s) to
5315 search for a window that is already displaying the buffer. See
5316 `display-buffer-reuse-window'."
5317 (interactive (list (read-buffer "Display buffer: " (other-buffer))
5318 (if current-prefix-arg t
)))
5319 (let ((buffer (if (bufferp buffer-or-name
)
5321 (get-buffer buffer-or-name
)))
5322 ;; Handle the old form of the first argument.
5323 (inhibit-same-window (and action
(not (listp action
)))))
5324 (unless (listp action
) (setq action nil
))
5325 (if display-buffer-function
5326 ;; If `display-buffer-function' is defined, let it do the job.
5327 (funcall display-buffer-function buffer inhibit-same-window
)
5328 ;; Otherwise, use the defined actions.
5330 (display-buffer-assq-regexp (buffer-name buffer
)
5331 display-buffer-alist
))
5332 (special-action (display-buffer--special-action buffer
))
5333 ;; Extra actions from the arguments to this function:
5335 (cons nil
(append (if inhibit-same-window
5336 '((inhibit-same-window . t
)))
5338 `((reusable-frames .
,frame
))))))
5339 ;; Construct action function list and action alist.
5340 (actions (list display-buffer-overriding-action
5341 user-action special-action action extra-action
5342 display-buffer-base-action
5343 display-buffer-fallback-action
))
5344 (functions (apply 'append
5347 (if (functionp x
) (list x
) x
))
5349 (alist (apply 'append
(mapcar 'cdr actions
)))
5351 (unless (buffer-live-p buffer
)
5352 (error "Invalid buffer"))
5353 (while (and functions
(not window
))
5354 (setq window
(funcall (car functions
) buffer alist
)
5355 functions
(cdr functions
)))
5358 (defun display-buffer-other-frame (buffer)
5359 "Display buffer BUFFER in another frame.
5360 This uses the function `display-buffer' as a subroutine; see
5361 its documentation for additional customization information."
5362 (interactive "BDisplay buffer in other frame: ")
5363 (display-buffer buffer display-buffer--other-frame-action t
))
5365 ;;; `display-buffer' action functions:
5367 (defun display-buffer-same-window (buffer alist
)
5368 "Display BUFFER in the selected window.
5369 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
5370 if the selected window is a minibuffer window or is dedicated to
5371 another buffer; in that case, return nil. Otherwise, return the
5373 (unless (or (cdr (assq 'inhibit-same-window alist
))
5374 (window-minibuffer-p)
5375 (window-dedicated-p))
5376 (window--display-buffer buffer
(selected-window) 'reuse
)))
5378 (defun display-buffer--maybe-same-window (buffer alist
)
5379 "Conditionally display BUFFER in the selected window.
5380 If `same-window-p' returns non-nil for BUFFER's name, call
5381 `display-buffer-same-window' and return its value. Otherwise,
5383 (and (same-window-p (buffer-name buffer
))
5384 (display-buffer-same-window buffer alist
)))
5386 (defun display-buffer-reuse-window (buffer alist
)
5387 "Return a window that is already displaying BUFFER.
5388 Return nil if no usable window is found.
5390 If ALIST has a non-nil `inhibit-same-window' entry, the selected
5391 window is not eligible for reuse.
5393 If ALIST contains a `reusable-frames' entry, its value determines
5394 which frames to search for a reusable window:
5395 nil -- the selected frame (actually the last non-minibuffer frame)
5396 A frame -- just that frame
5397 `visible' -- all visible frames
5398 0 -- all frames on the current terminal
5401 If ALIST contains no `reusable-frames' entry, search just the
5402 selected frame if `display-buffer-reuse-frames' and
5403 `pop-up-frames' are both nil; search all frames on the current
5404 terminal if either of those variables is non-nil.
5406 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5407 event that a window on another frame is chosen, avoid raising
5409 (let* ((alist-entry (assq 'reusable-frames alist
))
5410 (frames (cond (alist-entry (cdr alist-entry
))
5411 ((if (eq pop-up-frames
'graphic-only
)
5415 (display-buffer-reuse-frames 0)
5416 (t (last-nonminibuffer-frame))))
5417 (window (if (and (eq buffer
(window-buffer))
5418 (not (cdr (assq 'inhibit-same-window alist
))))
5420 (car (delq (selected-window)
5421 (get-buffer-window-list buffer
'nomini
5423 (when (window-live-p window
)
5424 (prog1 (window--display-buffer buffer window
'reuse
)
5425 (unless (cdr (assq 'inhibit-switch-frame alist
))
5426 (window--maybe-raise-frame (window-frame window
)))))))
5428 (defun display-buffer--special-action (buffer)
5429 "Return special display action for BUFFER, if any.
5430 If `special-display-p' returns non-nil for BUFFER, return an
5431 appropriate display action involving `special-display-function'.
5432 See `display-buffer' for the format of display actions."
5433 (and special-display-function
5434 ;; `special-display-p' returns either t or a list of frame
5435 ;; parameters to pass to `special-display-function'.
5436 (let ((pars (special-display-p (buffer-name buffer
))))
5438 (list (list #'display-buffer-reuse-window
5439 `(lambda (buffer _alist
)
5440 (funcall special-display-function
5441 buffer
',(if (listp pars
) pars
)))))))))
5443 (defun display-buffer-pop-up-frame (buffer alist
)
5444 "Display BUFFER in a new frame.
5445 This works by calling `pop-up-frame-function'. If successful,
5446 return the window used; otherwise return nil.
5448 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
5449 raising the new frame.
5451 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
5452 corresponding value is an alist of frame parameters to give the
5454 (let* ((params (cdr (assq 'pop-up-frame-parameters alist
)))
5455 (pop-up-frame-alist (append params pop-up-frame-alist
))
5456 (fun pop-up-frame-function
)
5459 (setq frame
(funcall fun
))
5460 (setq window
(frame-selected-window frame
)))
5461 (prog1 (window--display-buffer buffer window
5462 'frame display-buffer-mark-dedicated
)
5463 (unless (cdr (assq 'inhibit-switch-frame alist
))
5464 (window--maybe-raise-frame frame
))))))
5466 (defun display-buffer-pop-up-window (buffer alist
)
5467 "Display BUFFER by popping up a new window.
5468 The new window is created on the selected frame, or in
5469 `last-nonminibuffer-frame' if no windows can be created there.
5470 If successful, return the new window; otherwise return nil.
5472 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5473 event that the new window is created on another frame, avoid
5475 (let ((frame (or (window--frame-usable-p (selected-frame))
5476 (window--frame-usable-p (last-nonminibuffer-frame))))
5478 (when (and (or (not (frame-parameter frame
'unsplittable
))
5479 ;; If the selected frame cannot be split, look at
5480 ;; `last-nonminibuffer-frame'.
5481 (and (eq frame
(selected-frame))
5482 (setq frame
(last-nonminibuffer-frame))
5483 (window--frame-usable-p frame
)
5484 (not (frame-parameter frame
'unsplittable
))))
5485 ;; Attempt to split largest or least recently used window.
5486 (setq window
(or (window--try-to-split-window
5487 (get-largest-window frame t
))
5488 (window--try-to-split-window
5489 (get-lru-window frame t
)))))
5490 (prog1 (window--display-buffer buffer window
5491 'window display-buffer-mark-dedicated
)
5492 (unless (cdr (assq 'inhibit-switch-frame alist
))
5493 (window--maybe-raise-frame (window-frame window
)))))))
5495 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist
)
5496 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
5498 If `pop-up-frames' is non-nil (and not `graphic-only' on a
5499 text-only terminal), try with `display-buffer-pop-up-frame'.
5501 If that cannot be done, and `pop-up-windows' is non-nil, try
5502 again with `display-buffer-pop-up-window'."
5503 (or (and (if (eq pop-up-frames
'graphic-only
)
5506 (display-buffer-pop-up-frame buffer alist
))
5508 (display-buffer-pop-up-window buffer alist
))))
5510 (defun display-buffer-below-selected (buffer _alist
)
5511 "Try displaying BUFFER in a window below the selected window.
5512 This either splits the selected window or reuses the window below
5515 (or (and (not (frame-parameter nil
'unsplittable
))
5516 (setq window
(window--try-to-split-window (selected-window)))
5517 (window--display-buffer
5518 buffer window
'window display-buffer-mark-dedicated
))
5519 (and (setq window
(window-in-direction 'below
))
5520 (not (window-dedicated-p window
))
5521 (window--display-buffer
5522 buffer window
'reuse display-buffer-mark-dedicated
)))))
5524 (defun display-buffer-in-previous-window (buffer alist
)
5525 "Display BUFFER in a window previously showing it.
5526 If ALIST has a non-nil `inhibit-same-window' entry, the selected
5527 window is not eligible for reuse.
5529 If ALIST contains a `reusable-frames' entry, its value determines
5530 which frames to search for a reusable window:
5531 nil -- the selected frame (actually the last non-minibuffer frame)
5532 A frame -- just that frame
5533 `visible' -- all visible frames
5534 0 -- all frames on the current terminal
5537 If ALIST contains no `reusable-frames' entry, search just the
5538 selected frame if `display-buffer-reuse-frames' and
5539 `pop-up-frames' are both nil; search all frames on the current
5540 terminal if either of those variables is non-nil.
5542 If ALIST has a `previous-window' entry, the window specified by
5543 that entry will override any other window found by the methods
5544 above, even if that window never showed BUFFER before."
5545 (let* ((alist-entry (assq 'reusable-frames alist
))
5546 (inhibit-same-window
5547 (cdr (assq 'inhibit-same-window alist
)))
5549 (alist-entry (cdr alist-entry
))
5550 ((if (eq pop-up-frames
'graphic-only
)
5554 (display-buffer-reuse-frames 0)
5555 (t (last-nonminibuffer-frame))))
5556 entry best-window second-best-window window
)
5557 ;; Scan windows whether they have shown the buffer recently.
5559 (dolist (window (window-list-1 (frame-first-window) 'nomini frames
))
5560 (when (and (assq buffer
(window-prev-buffers window
))
5561 (not (window-dedicated-p window
)))
5562 (if (eq window
(selected-window))
5563 (unless inhibit-same-window
5564 (setq second-best-window window
))
5565 (setq best-window window
)
5567 ;; When ALIST has a `previous-window' entry, that entry may override
5568 ;; anything we found so far.
5569 (when (and (setq window
(cdr (assq 'previous-window alist
)))
5570 (window-live-p window
)
5571 (not (window-dedicated-p window
)))
5572 (if (eq window
(selected-window))
5573 (unless inhibit-same-window
5574 (setq second-best-window window
))
5575 (setq best-window window
)))
5576 ;; Return best or second best window found.
5577 (when (setq window
(or best-window second-best-window
))
5578 (window--display-buffer buffer window
'reuse
))))
5580 (defun display-buffer-use-some-window (buffer alist
)
5581 "Display BUFFER in an existing window.
5582 Search for a usable window, set that window to the buffer, and
5583 return the window. If no suitable window is found, return nil.
5585 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5586 event that a window in another frame is chosen, avoid raising
5588 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist
)))
5589 (frame (or (window--frame-usable-p (selected-frame))
5590 (window--frame-usable-p (last-nonminibuffer-frame))))
5592 ;; Reuse an existing window.
5593 (or (get-lru-window frame nil not-this-window
)
5594 (let ((window (get-buffer-window buffer
'visible
)))
5595 (unless (and not-this-window
5596 (eq window
(selected-window)))
5598 (get-largest-window 'visible nil not-this-window
)
5599 (let ((window (get-buffer-window buffer
0)))
5600 (unless (and not-this-window
5601 (eq window
(selected-window)))
5603 (get-largest-window 0 not-this-window
))))
5604 (when (window-live-p window
)
5606 (window--display-buffer buffer window
'reuse
)
5607 (window--even-window-heights window
)
5608 (unless (cdr (assq 'inhibit-switch-frame alist
))
5609 (window--maybe-raise-frame (window-frame window
)))))))
5611 ;;; Display + selection commands:
5612 (defun pop-to-buffer (buffer &optional action norecord
)
5613 "Select buffer BUFFER in some window, preferably a different one.
5614 BUFFER may be a buffer, a string (a buffer name), or nil. If it
5615 is a string not naming an existent buffer, create a buffer with
5616 that name. If BUFFER is nil, choose some other buffer. Return
5619 This uses `display-buffer' as a subroutine. The optional ACTION
5620 argument is passed to `display-buffer' as its ACTION argument.
5621 See `display-buffer' for more information. ACTION is t if called
5622 interactively with a prefix argument, which means to pop to a
5623 window other than the selected one even if the buffer is already
5624 displayed in the selected window.
5626 If the window to show BUFFER is not on the selected
5627 frame, raise that window's frame and give it input focus.
5629 Optional third arg NORECORD non-nil means do not put this buffer
5630 at the front of the list of recently selected ones."
5631 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
5632 (if current-prefix-arg t
)))
5633 (setq buffer
(window-normalize-buffer-to-switch-to buffer
))
5635 (let* ((old-frame (selected-frame))
5636 (window (display-buffer buffer action
))
5637 (frame (window-frame window
)))
5638 ;; If we chose another frame, make sure it gets input focus.
5639 (unless (eq frame old-frame
)
5640 (select-frame-set-input-focus frame norecord
))
5641 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
5642 (select-window window norecord
)
5645 (defun pop-to-buffer-same-window (buffer &optional norecord
)
5646 "Select buffer BUFFER in some window, preferably the same one.
5647 This function behaves much like `switch-to-buffer', except it
5648 displays with `special-display-function' if BUFFER has a match in
5649 `special-display-buffer-names' or `special-display-regexps'.
5651 Unlike `pop-to-buffer', this function prefers using the selected
5652 window over popping up a new window or frame.
5654 BUFFER may be a buffer, a string (a buffer name), or nil. If it
5655 is a string not naming an existent buffer, create a buffer with
5656 that name. If BUFFER is nil, choose some other buffer. Return
5659 NORECORD, if non-nil means do not put this buffer at the front of
5660 the list of recently selected ones."
5661 (pop-to-buffer buffer display-buffer--same-window-action norecord
))
5663 (defun read-buffer-to-switch (prompt)
5664 "Read the name of a buffer to switch to, prompting with PROMPT.
5665 Return the name of the buffer as a string.
5667 This function is intended for the `switch-to-buffer' family of
5668 commands since these need to omit the name of the current buffer
5669 from the list of completions and default values."
5670 (let ((rbts-completion-table (internal-complete-buffer-except)))
5671 (minibuffer-with-setup-hook
5673 (setq minibuffer-completion-table rbts-completion-table
)
5674 ;; Since rbts-completion-table is built dynamically, we
5675 ;; can't just add it to the default value of
5676 ;; icomplete-with-completion-tables, so we add it
5678 (if (and (boundp 'icomplete-with-completion-tables
)
5679 (listp icomplete-with-completion-tables
))
5680 (set (make-local-variable 'icomplete-with-completion-tables
)
5681 (cons rbts-completion-table
5682 icomplete-with-completion-tables
))))
5683 (read-buffer prompt
(other-buffer (current-buffer))
5684 (confirm-nonexistent-file-or-buffer)))))
5686 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
5687 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
5688 If BUFFER-OR-NAME is nil, return the buffer returned by
5689 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
5690 exists, return that buffer. If no such buffer exists, create a
5691 buffer with the name BUFFER-OR-NAME and return that buffer."
5693 (or (get-buffer buffer-or-name
)
5694 (let ((buffer (get-buffer-create buffer-or-name
)))
5695 (set-buffer-major-mode buffer
)
5699 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window
)
5700 "Switch to buffer BUFFER-OR-NAME in the selected window.
5701 If the selected window cannot display the specified
5702 buffer (e.g. if it is a minibuffer window or strongly dedicated
5703 to another buffer), call `pop-to-buffer' to select the buffer in
5706 If called interactively, read the buffer name using the
5707 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5708 determines whether to request confirmation before creating a new
5711 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
5712 If BUFFER-OR-NAME is a string that does not identify an existing
5713 buffer, create a buffer with that name. If BUFFER-OR-NAME is
5714 nil, switch to the buffer returned by `other-buffer'.
5716 If optional argument NORECORD is non-nil, do not put the buffer
5717 at the front of the buffer list, and do not make the window
5718 displaying it the most recently selected one.
5720 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
5721 must be displayed in the selected window; if that is impossible,
5722 signal an error rather than calling `pop-to-buffer'.
5724 Return the buffer switched to."
5726 (list (read-buffer-to-switch "Switch to buffer: ") nil
'force-same-window
))
5727 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name
)))
5729 ;; Don't call set-window-buffer if it's not needed since it
5730 ;; might signal an error (e.g. if the window is dedicated).
5731 ((eq buffer
(window-buffer)))
5732 ((window-minibuffer-p)
5733 (if force-same-window
5734 (user-error "Cannot switch buffers in minibuffer window")
5735 (pop-to-buffer buffer norecord
)))
5736 ((eq (window-dedicated-p) t
)
5737 (if force-same-window
5738 (user-error "Cannot switch buffers in a dedicated window")
5739 (pop-to-buffer buffer norecord
)))
5740 (t (set-window-buffer nil buffer
)))
5743 (select-window (selected-window)))
5744 (set-buffer buffer
)))
5746 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord
)
5747 "Select the buffer specified by BUFFER-OR-NAME in another window.
5748 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5749 nil. Return the buffer switched to.
5751 If called interactively, prompt for the buffer name using the
5752 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5753 determines whether to request confirmation before creating a new
5756 If BUFFER-OR-NAME is a string and does not identify an existing
5757 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5758 nil, switch to the buffer returned by `other-buffer'.
5760 Optional second argument NORECORD non-nil means do not put this
5761 buffer at the front of the list of recently selected ones.
5763 This uses the function `display-buffer' as a subroutine; see its
5764 documentation for additional customization information."
5766 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5767 (let ((pop-up-windows t
))
5768 (pop-to-buffer buffer-or-name t norecord
)))
5770 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord
)
5771 "Switch to buffer BUFFER-OR-NAME in another frame.
5772 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5773 nil. Return the buffer switched to.
5775 If called interactively, prompt for the buffer name using the
5776 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5777 determines whether to request confirmation before creating a new
5780 If BUFFER-OR-NAME is a string and does not identify an existing
5781 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5782 nil, switch to the buffer returned by `other-buffer'.
5784 Optional second arg NORECORD non-nil means do not put this
5785 buffer at the front of the list of recently selected ones.
5787 This uses the function `display-buffer' as a subroutine; see its
5788 documentation for additional customization information."
5790 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
5791 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord
))
5793 (defun set-window-text-height (window height
)
5794 "Set the height in lines of the text display area of WINDOW to HEIGHT.
5795 WINDOW must be a live window and defaults to the selected one.
5796 HEIGHT doesn't include the mode line or header line, if any, or
5797 any partial-height lines in the text display area.
5799 Note that the current implementation of this function cannot
5800 always set the height exactly, but attempts to be conservative,
5801 by allocating more lines than are actually needed in the case
5802 where some error may be present."
5803 (setq window
(window-normalize-window window t
))
5804 (let ((delta (- height
(window-text-height window
))))
5805 (unless (zerop delta
)
5806 ;; Setting window-min-height to a value like 1 can lead to very
5807 ;; bizarre displays because it also allows Emacs to make *other*
5808 ;; windows one line tall, which means that there's no more space
5809 ;; for the mode line.
5810 (let ((window-min-height (min 2 height
)))
5811 (window-resize window delta
)))))
5813 (defun enlarge-window-horizontally (delta)
5814 "Make selected window DELTA columns wider.
5815 Interactively, if no argument is given, make selected window one
5818 (enlarge-window delta t
))
5820 (defun shrink-window-horizontally (delta)
5821 "Make selected window DELTA columns narrower.
5822 Interactively, if no argument is given, make selected window one
5825 (shrink-window delta t
))
5827 (defun count-screen-lines (&optional beg end count-final-newline window
)
5828 "Return the number of screen lines in the region.
5829 The number of screen lines may be different from the number of actual lines,
5830 due to line breaking, display table, etc.
5832 Optional arguments BEG and END default to `point-min' and `point-max'
5835 If region ends with a newline, ignore it unless optional third argument
5836 COUNT-FINAL-NEWLINE is non-nil.
5838 The optional fourth argument WINDOW specifies the window used for obtaining
5839 parameters such as width, horizontal scrolling, and so on. The default is
5840 to use the selected window's parameters.
5842 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
5843 regardless of which buffer is displayed in WINDOW. This makes possible to use
5844 `count-screen-lines' in any buffer, whether or not it is currently displayed
5847 (setq beg
(point-min)))
5849 (setq end
(point-max)))
5855 (narrow-to-region (min beg end
)
5856 (if (and (not count-final-newline
)
5857 (= ?
\n (char-before (max beg end
))))
5860 (goto-char (point-min))
5861 (1+ (vertical-motion (buffer-size) window
))))))
5863 (defun window-buffer-height (window)
5864 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
5865 WINDOW must be a live window and defaults to the selected one."
5866 (setq window
(window-normalize-window window t
))
5867 (with-current-buffer (window-buffer window
)
5869 (count-screen-lines (point-min) (point-max)
5870 ;; If buffer ends with a newline, ignore it when
5871 ;; counting height unless point is after it.
5875 ;;; Resizing buffers to fit their contents exactly.
5876 (defun fit-window-to-buffer (&optional window max-height min-height
)
5877 "Adjust height of WINDOW to display its buffer's contents exactly.
5878 WINDOW must be a live window and defaults to the selected one.
5880 Optional argument MAX-HEIGHT specifies the maximum height of
5881 WINDOW and defaults to the height of WINDOW's frame. Optional
5882 argument MIN-HEIGHT specifies the minimum height of WINDOW and
5883 defaults to `window-min-height'. Both MAX-HEIGHT and MIN-HEIGHT
5884 are specified in lines and include the mode line and header line,
5887 Return the number of lines by which WINDOW was enlarged or
5888 shrunk. If an error occurs during resizing, return nil but don't
5891 Note that even if this function makes WINDOW large enough to show
5892 _all_ lines of its buffer you might not see the first lines when
5893 WINDOW was scrolled."
5895 (setq window
(window-normalize-window window t
))
5896 ;; Can't resize a full height or fixed-size window.
5897 (unless (or (window-size-fixed-p window
)
5898 (window-full-height-p window
))
5899 (with-selected-window window
5900 (let* ((height (window-total-size))
5902 ;; Adjust MIN-HEIGHT.
5903 (if (numberp min-height
)
5904 ;; Can't get smaller than `window-safe-min-height'.
5905 (max min-height window-safe-min-height
)
5906 ;; Preserve header and mode line if present.
5907 (window-min-size nil nil t
)))
5909 ;; Adjust MAX-HEIGHT.
5910 (if (numberp max-height
)
5911 ;; Can't get larger than height of frame.
5913 (window-total-size (frame-root-window window
)))
5914 ;, Don't delete other windows.
5915 (+ height
(window-max-delta nil nil window
))))
5916 ;; Make `desired-height' the height necessary to show
5917 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
5922 (+ (count-screen-lines)
5923 ;; For non-minibuffers count the mode line, if any.
5924 (if (and (not (window-minibuffer-p window
))
5928 ;; Count the header line, if any.
5929 (if header-line-format
1 0))
5933 (- desired-height
(window-total-size window
)))
5935 (if (> desired-delta
0)
5937 (window-max-delta window nil window
))
5939 (- (window-min-delta window nil window
))))))
5942 ;; Return zero if DELTA became zero in the process.
5944 ;; Don't try to redisplay with the cursor at the end on its
5945 ;; own line--that would force a scroll and spoil things.
5946 (when (and (eobp) (bolp) (not (bobp)))
5947 ;; It's silly to put `point' at the end of the previous
5948 ;; line and so maybe force horizontal scrolling.
5949 (set-window-point window
(line-beginning-position 0)))
5950 ;; Call `window-resize' with OVERRIDE argument equal WINDOW.
5951 (window-resize window delta nil window
)
5952 ;; Check if the last line is surely fully visible. If
5953 ;; not, enlarge the window.
5954 (let ((end (save-excursion
5955 (goto-char (point-max))
5956 (when (and (bolp) (not (bobp)))
5957 ;; Don't include final newline.
5959 (when truncate-lines
5960 ;; If line-wrapping is turned off, test the
5961 ;; beginning of the last line for
5962 ;; visibility instead of the end, as the
5963 ;; end of the line could be invisible by
5964 ;; virtue of extending past the edge of the
5968 (set-window-vscroll window
0)
5969 ;; This loop might in some rare pathological cases raise
5970 ;; an error - another reason for the `condition-case'.
5971 (while (and (< desired-height max-height
)
5972 (= desired-height
(window-total-size))
5973 (not (pos-visible-in-window-p end
)))
5974 (window-resize window
1 nil window
)
5975 (setq desired-height
(1+ desired-height
)))))
5976 (error (setq delta nil
)))
5979 (defcustom fit-frame-to-buffer-bottom-margin
4
5980 "Bottom margin for `fit-frame-to-buffer'.
5981 This is the number of lines `fit-frame-to-buffer' leaves free at the
5982 bottom of the display in order to not obscure the system task bar."
5987 (defun fit-frame-to-buffer (&optional frame max-height min-height
)
5988 "Adjust height of FRAME to display its buffer's contents exactly.
5989 FRAME can be any live frame and defaults to the selected one.
5991 Optional argument MAX-HEIGHT specifies the maximum height of
5992 FRAME and defaults to the height of the display below the current
5993 top line of FRAME minus FIT-FRAME-TO-BUFFER-BOTTOM-MARGIN.
5994 Optional argument MIN-HEIGHT specifies the minimum height of
5997 (setq frame
(window-normalize-frame frame
))
5998 (let* ((root (frame-root-window frame
))
6000 (+ (- (frame-height frame
) (window-total-size root
))
6002 (frame-top (frame-parameter frame
'top
))
6003 (top (if (consp frame-top
)
6004 (funcall (car frame-top
) (cadr frame-top
))
6007 (- (/ (- (x-display-pixel-height frame
) top
)
6008 (frame-char-height frame
))
6009 fit-frame-to-buffer-bottom-margin
))
6012 (when (and (window-live-p root
) (not (window-size-fixed-p root
)))
6013 (with-selected-window root
6016 (setq max-height frame-max-height
))
6017 ((numberp max-height
)
6018 (setq max-height
(min max-height frame-max-height
)))
6020 (error "%s is an invalid maximum height" max-height
)))
6023 (setq min-height frame-min-height
))
6024 ((numberp min-height
)
6025 (setq min-height
(min min-height frame-min-height
)))
6027 (error "%s is an invalid minimum height" min-height
)))
6028 ;; When tool-bar-mode is enabled and we have just created a new
6029 ;; frame, reserve lines for toolbar resizing. This is needed
6030 ;; because for reasons unknown to me Emacs (1) reserves one line
6031 ;; for the toolbar when making the initial frame and toolbars
6032 ;; are enabled, and (2) later adds the remaining lines needed.
6033 ;; Our code runs IN BETWEEN (1) and (2). YMMV when you're on a
6034 ;; system that behaves differently.
6035 (let ((quit-restore (window-parameter root
'quit-restore
))
6036 (lines (tool-bar-lines-needed frame
)))
6037 (when (and quit-restore
(eq (car quit-restore
) 'frame
)
6038 (not (zerop lines
)))
6039 (setq compensate
(1- lines
))))
6040 (message "%s" compensate
)
6042 ;; Always count a final newline - we don't do any
6043 ;; post-processing, so let's play safe.
6044 (+ (count-screen-lines nil nil t
)
6045 (- (window-body-size))
6047 ;; Move away from final newline.
6048 (when (and (eobp) (bolp) (not (bobp)))
6049 (set-window-point root
(line-beginning-position 0)))
6050 (set-window-start root
(point-min))
6051 (set-window-vscroll root
0)
6055 (min (max (+ (frame-height frame
) delta
)
6058 (error (setq delta nil
))))
6061 (defun window-safely-shrinkable-p (&optional window
)
6062 "Return t if WINDOW can be shrunk without shrinking other windows.
6063 WINDOW defaults to the selected window."
6064 (with-selected-window (or window
(selected-window))
6065 (let ((edges (window-edges)))
6066 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
6067 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
6069 (defun shrink-window-if-larger-than-buffer (&optional window
)
6070 "Shrink height of WINDOW if its buffer doesn't need so many lines.
6071 More precisely, shrink WINDOW vertically to be as small as
6072 possible, while still showing the full contents of its buffer.
6073 WINDOW must be a live window and defaults to the selected one.
6075 Do not shrink WINDOW to less than `window-min-height' lines. Do
6076 nothing if the buffer contains more lines than the present window
6077 height, or if some of the window's contents are scrolled out of
6078 view, or if shrinking this window would also shrink another
6079 window, or if the window is the only window of its frame.
6081 Return non-nil if the window was shrunk, nil otherwise."
6083 (setq window
(window-normalize-window window t
))
6084 ;; Make sure that WINDOW is vertically combined and `point-min' is
6085 ;; visible (for whatever reason that's needed). The remaining issues
6086 ;; should be taken care of by `fit-window-to-buffer'.
6087 (when (and (window-combined-p window
)
6088 (pos-visible-in-window-p (point-min) window
))
6089 (fit-window-to-buffer window
(window-total-size window
))))
6091 (defun kill-buffer-and-window ()
6092 "Kill the current buffer and delete the selected window."
6094 (let ((window-to-delete (selected-window))
6095 (buffer-to-kill (current-buffer))
6096 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
6099 (add-hook 'kill-buffer-hook delete-window-hook t t
)
6100 (if (kill-buffer (current-buffer))
6101 ;; If `delete-window' failed before, we rerun it to regenerate
6102 ;; the error so it can be seen in the echo area.
6103 (when (eq (selected-window) window-to-delete
)
6105 ;; If the buffer is not dead for some reason (probably because
6106 ;; of a `quit' signal), remove the hook again.
6108 (with-current-buffer buffer-to-kill
6109 (remove-hook 'kill-buffer-hook delete-window-hook t
))))))
6112 (defvar recenter-last-op nil
6113 "Indicates the last recenter operation performed.
6114 Possible values: `top', `middle', `bottom', integer or float numbers.")
6116 (defcustom recenter-positions
'(middle top bottom
)
6117 "Cycling order for `recenter-top-bottom'.
6118 A list of elements with possible values `top', `middle', `bottom',
6119 integer or float numbers that define the cycling order for
6120 the command `recenter-top-bottom'.
6122 Top and bottom destinations are `scroll-margin' lines from the true
6123 window top and bottom. Middle redraws the frame and centers point
6124 vertically within the window. Integer number moves current line to
6125 the specified absolute window-line. Float number between 0.0 and 1.0
6126 means the percentage of the screen space from the top. The default
6127 cycling order is middle -> top -> bottom."
6128 :type
'(repeat (choice
6129 (const :tag
"Top" top
)
6130 (const :tag
"Middle" middle
)
6131 (const :tag
"Bottom" bottom
)
6132 (integer :tag
"Line number")
6133 (float :tag
"Percentage")))
6137 (defun recenter-top-bottom (&optional arg
)
6138 "Move current buffer line to the specified window line.
6139 With no prefix argument, successive calls place point according
6140 to the cycling order defined by `recenter-positions'.
6142 A prefix argument is handled like `recenter':
6143 With numeric prefix ARG, move current line to window-line ARG.
6144 With plain `C-u', move current line to window center."
6147 (arg (recenter arg
)) ; Always respect ARG.
6149 (setq recenter-last-op
6150 (if (eq this-command last-command
)
6151 (car (or (cdr (member recenter-last-op recenter-positions
))
6152 recenter-positions
))
6153 (car recenter-positions
)))
6154 (let ((this-scroll-margin
6155 (min (max 0 scroll-margin
)
6156 (truncate (/ (window-body-height) 4.0)))))
6157 (cond ((eq recenter-last-op
'middle
)
6159 ((eq recenter-last-op
'top
)
6160 (recenter this-scroll-margin
))
6161 ((eq recenter-last-op
'bottom
)
6162 (recenter (- -
1 this-scroll-margin
)))
6163 ((integerp recenter-last-op
)
6164 (recenter recenter-last-op
))
6165 ((floatp recenter-last-op
)
6166 (recenter (round (* recenter-last-op
(window-height))))))))))
6168 (define-key global-map
[?\C-l
] 'recenter-top-bottom
)
6170 (defun move-to-window-line-top-bottom (&optional arg
)
6171 "Position point relative to window.
6173 With a prefix argument ARG, acts like `move-to-window-line'.
6175 With no argument, positions point at center of window.
6176 Successive calls position point at positions defined
6177 by `recenter-positions'."
6180 (arg (move-to-window-line arg
)) ; Always respect ARG.
6182 (setq recenter-last-op
6183 (if (eq this-command last-command
)
6184 (car (or (cdr (member recenter-last-op recenter-positions
))
6185 recenter-positions
))
6186 (car recenter-positions
)))
6187 (let ((this-scroll-margin
6188 (min (max 0 scroll-margin
)
6189 (truncate (/ (window-body-height) 4.0)))))
6190 (cond ((eq recenter-last-op
'middle
)
6191 (call-interactively 'move-to-window-line
))
6192 ((eq recenter-last-op
'top
)
6193 (move-to-window-line this-scroll-margin
))
6194 ((eq recenter-last-op
'bottom
)
6195 (move-to-window-line (- -
1 this-scroll-margin
)))
6196 ((integerp recenter-last-op
)
6197 (move-to-window-line recenter-last-op
))
6198 ((floatp recenter-last-op
)
6199 (move-to-window-line (round (* recenter-last-op
(window-height))))))))))
6201 (define-key global-map
[?\M-r
] 'move-to-window-line-top-bottom
)
6203 ;;; Scrolling commands.
6205 ;;; Scrolling commands which do not signal errors at top/bottom
6206 ;;; of buffer at first key-press (instead move to top/bottom
6209 (defcustom scroll-error-top-bottom nil
6210 "Move point to top/bottom of buffer before signaling a scrolling error.
6211 A value of nil means just signal an error if no more scrolling possible.
6212 A value of t means point moves to the beginning or the end of the buffer
6213 \(depending on scrolling direction) when no more scrolling possible.
6214 When point is already on that position, then signal an error."
6219 (defun scroll-up-command (&optional arg
)
6220 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
6221 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
6222 scroll window further, move cursor to the bottom line.
6223 When point is already on that position, then signal an error.
6224 A near full screen is `next-screen-context-lines' less than a full screen.
6225 Negative ARG means scroll downward.
6226 If ARG is the atom `-', scroll downward by nearly full screen."
6229 ((null scroll-error-top-bottom
)
6232 (scroll-down-command nil
))
6233 ((< (prefix-numeric-value arg
) 0)
6234 (scroll-down-command (- (prefix-numeric-value arg
))))
6236 (scroll-up arg
)) ; signal error
6242 ;; When scrolling by ARG lines can't be done,
6243 ;; move by ARG lines instead.
6245 ;; When ARG is nil for full-screen scrolling,
6246 ;; move to the bottom of the buffer.
6247 (goto-char (point-max))))))))
6249 (put 'scroll-up-command
'scroll-command t
)
6251 (defun scroll-down-command (&optional arg
)
6252 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
6253 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
6254 scroll window further, move cursor to the top line.
6255 When point is already on that position, then signal an error.
6256 A near full screen is `next-screen-context-lines' less than a full screen.
6257 Negative ARG means scroll upward.
6258 If ARG is the atom `-', scroll upward by nearly full screen."
6261 ((null scroll-error-top-bottom
)
6264 (scroll-up-command nil
))
6265 ((< (prefix-numeric-value arg
) 0)
6266 (scroll-up-command (- (prefix-numeric-value arg
))))
6268 (scroll-down arg
)) ; signal error
6272 (beginning-of-buffer
6274 ;; When scrolling by ARG lines can't be done,
6275 ;; move by ARG lines instead.
6276 (forward-line (- arg
))
6277 ;; When ARG is nil for full-screen scrolling,
6278 ;; move to the top of the buffer.
6279 (goto-char (point-min))))))))
6281 (put 'scroll-down-command
'scroll-command t
)
6283 ;;; Scrolling commands which scroll a line instead of full screen.
6285 (defun scroll-up-line (&optional arg
)
6286 "Scroll text of selected window upward ARG lines; or one line if no ARG.
6287 If ARG is omitted or nil, scroll upward by one line.
6288 This is different from `scroll-up-command' that scrolls a full screen."
6290 (scroll-up (or arg
1)))
6292 (put 'scroll-up-line
'scroll-command t
)
6294 (defun scroll-down-line (&optional arg
)
6295 "Scroll text of selected window down ARG lines; or one line if no ARG.
6296 If ARG is omitted or nil, scroll down by one line.
6297 This is different from `scroll-down-command' that scrolls a full screen."
6299 (scroll-down (or arg
1)))
6301 (put 'scroll-down-line
'scroll-command t
)
6304 (defun scroll-other-window-down (lines)
6305 "Scroll the \"other window\" down.
6306 For more details, see the documentation for `scroll-other-window'."
6308 (scroll-other-window
6309 ;; Just invert the argument's meaning.
6310 ;; We can do that without knowing which window it will be.
6311 (if (eq lines
'-
) nil
6313 (- (prefix-numeric-value lines
))))))
6315 (defun beginning-of-buffer-other-window (arg)
6316 "Move point to the beginning of the buffer in the other window.
6317 Leave mark at previous position.
6318 With arg N, put point N/10 of the way from the true beginning."
6320 (let ((orig-window (selected-window))
6321 (window (other-window-for-scrolling)))
6322 ;; We use unwind-protect rather than save-window-excursion
6323 ;; because the latter would preserve the things we want to change.
6326 (select-window window
)
6327 ;; Set point and mark in that window's buffer.
6329 (beginning-of-buffer arg
))
6330 ;; Set point accordingly.
6332 (select-window orig-window
))))
6334 (defun end-of-buffer-other-window (arg)
6335 "Move point to the end of the buffer in the other window.
6336 Leave mark at previous position.
6337 With arg N, put point N/10 of the way from the true end."
6339 ;; See beginning-of-buffer-other-window for comments.
6340 (let ((orig-window (selected-window))
6341 (window (other-window-for-scrolling)))
6344 (select-window window
)
6346 (end-of-buffer arg
))
6348 (select-window orig-window
))))
6350 (defvar mouse-autoselect-window-timer nil
6351 "Timer used by delayed window autoselection.")
6353 (defvar mouse-autoselect-window-position nil
6354 "Last mouse position recorded by delayed window autoselection.")
6356 (defvar mouse-autoselect-window-window nil
6357 "Last window recorded by delayed window autoselection.")
6359 (defvar mouse-autoselect-window-state nil
6360 "When non-nil, special state of delayed window autoselection.
6361 Possible values are `suspend' (suspend autoselection after a menu or
6362 scrollbar interaction) and `select' (the next invocation of
6363 `handle-select-window' shall select the window immediately).")
6365 (defun mouse-autoselect-window-cancel (&optional force
)
6366 "Cancel delayed window autoselection.
6367 Optional argument FORCE means cancel unconditionally."
6368 (unless (and (not force
)
6369 ;; Don't cancel for select-window or select-frame events
6370 ;; or when the user drags a scroll bar.
6371 (or (memq this-command
6372 '(handle-select-window handle-switch-frame
))
6373 (and (eq this-command
'scroll-bar-toolkit-scroll
)
6374 (memq (nth 4 (event-end last-input-event
))
6375 '(handle end-scroll
)))))
6376 (setq mouse-autoselect-window-state nil
)
6377 (when (timerp mouse-autoselect-window-timer
)
6378 (cancel-timer mouse-autoselect-window-timer
))
6379 (remove-hook 'pre-command-hook
'mouse-autoselect-window-cancel
)))
6381 (defun mouse-autoselect-window-start (mouse-position &optional window suspend
)
6382 "Start delayed window autoselection.
6383 MOUSE-POSITION is the last position where the mouse was seen as returned
6384 by `mouse-position'. Optional argument WINDOW non-nil denotes the
6385 window where the mouse was seen. Optional argument SUSPEND non-nil
6386 means suspend autoselection."
6387 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
6388 (setq mouse-autoselect-window-position mouse-position
)
6389 (when window
(setq mouse-autoselect-window-window window
))
6390 (setq mouse-autoselect-window-state
(when suspend
'suspend
))
6391 ;; Install timer which runs `mouse-autoselect-window-select' after
6392 ;; `mouse-autoselect-window' seconds.
6393 (setq mouse-autoselect-window-timer
6395 (abs mouse-autoselect-window
) nil
'mouse-autoselect-window-select
)))
6397 (defun mouse-autoselect-window-select ()
6398 "Select window with delayed window autoselection.
6399 If the mouse position has stabilized in a non-selected window, select
6400 that window. The minibuffer window is selected only if the minibuffer
6401 is active. This function is run by `mouse-autoselect-window-timer'."
6403 (let* ((mouse-position (mouse-position))
6406 (window-at (cadr mouse-position
) (cddr mouse-position
)
6407 (car mouse-position
)))))
6409 ((or (menu-or-popup-active-p)
6411 (not (coordinates-in-window-p (cdr mouse-position
) window
))))
6412 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
6413 ;; of WINDOW, temporarily suspend delayed autoselection.
6414 (mouse-autoselect-window-start mouse-position nil t
))
6415 ((eq mouse-autoselect-window-state
'suspend
)
6416 ;; Delayed autoselection was temporarily suspended, reenable it.
6417 (mouse-autoselect-window-start mouse-position
))
6418 ((and window
(not (eq window
(selected-window)))
6419 (or (not (numberp mouse-autoselect-window
))
6420 (and (> mouse-autoselect-window
0)
6421 ;; If `mouse-autoselect-window' is positive, select
6422 ;; window if the window is the same as before.
6423 (eq window mouse-autoselect-window-window
))
6424 ;; Otherwise select window if the mouse is at the same
6425 ;; position as before. Observe that the first test after
6426 ;; starting autoselection usually fails since the value of
6427 ;; `mouse-autoselect-window-position' recorded there is the
6428 ;; position where the mouse has entered the new window and
6429 ;; not necessarily where the mouse has stopped moving.
6430 (equal mouse-position mouse-autoselect-window-position
))
6431 ;; The minibuffer is a candidate window if it's active.
6432 (or (not (window-minibuffer-p window
))
6433 (eq window
(active-minibuffer-window))))
6434 ;; Mouse position has stabilized in non-selected window: Cancel
6435 ;; delayed autoselection and try to select that window.
6436 (mouse-autoselect-window-cancel t
)
6437 ;; Select window where mouse appears unless the selected window is the
6438 ;; minibuffer. Use `unread-command-events' in order to execute pre-
6439 ;; and post-command hooks and trigger idle timers. To avoid delaying
6440 ;; autoselection again, set `mouse-autoselect-window-state'."
6441 (unless (window-minibuffer-p (selected-window))
6442 (setq mouse-autoselect-window-state
'select
)
6443 (setq unread-command-events
6444 (cons (list 'select-window
(list window
))
6445 unread-command-events
))))
6446 ((or (and window
(eq window
(selected-window)))
6447 (not (numberp mouse-autoselect-window
))
6448 (equal mouse-position mouse-autoselect-window-position
))
6449 ;; Mouse position has either stabilized in the selected window or at
6450 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
6451 (mouse-autoselect-window-cancel t
))
6453 ;; Mouse position has not stabilized yet, resume delayed
6455 (mouse-autoselect-window-start mouse-position window
))))))
6457 (defun handle-select-window (event)
6458 "Handle select-window events."
6460 (let ((window (posn-window (event-start event
))))
6461 (unless (or (not (window-live-p window
))
6462 ;; Don't switch if we're currently in the minibuffer.
6463 ;; This tries to work around problems where the
6464 ;; minibuffer gets unselected unexpectedly, and where
6465 ;; you then have to move your mouse all the way down to
6466 ;; the minibuffer to select it.
6467 (window-minibuffer-p (selected-window))
6468 ;; Don't switch to minibuffer window unless it's active.
6469 (and (window-minibuffer-p window
)
6470 (not (minibuffer-window-active-p window
)))
6471 ;; Don't switch when autoselection shall be delayed.
6472 (and (numberp mouse-autoselect-window
)
6473 (not (zerop mouse-autoselect-window
))
6474 (not (eq mouse-autoselect-window-state
'select
))
6476 ;; Cancel any delayed autoselection.
6477 (mouse-autoselect-window-cancel t
)
6478 ;; Start delayed autoselection from current mouse
6479 ;; position and window.
6480 (mouse-autoselect-window-start (mouse-position) window
)
6481 ;; Executing a command cancels delayed autoselection.
6483 'pre-command-hook
'mouse-autoselect-window-cancel
))))
6484 (when mouse-autoselect-window
6485 ;; Reset state of delayed autoselection.
6486 (setq mouse-autoselect-window-state nil
)
6487 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
6488 (run-hooks 'mouse-leave-buffer-hook
))
6491 (select-window window
))))
6493 (defun truncated-partial-width-window-p (&optional window
)
6494 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
6495 WINDOW must be a live window and defaults to the selected one.
6496 Return nil if WINDOW is not a partial-width window
6497 (regardless of the value of `truncate-lines').
6498 Otherwise, consult the value of `truncate-partial-width-windows'
6499 for the buffer shown in WINDOW."
6500 (setq window
(window-normalize-window window t
))
6501 (unless (window-full-width-p window
)
6502 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
6503 (window-buffer window
))))
6504 (if (integerp t-p-w-w
)
6505 (< (window-width window
) t-p-w-w
)
6508 ;; Some of these are in tutorial--default-keys, so update that if you
6510 (define-key ctl-x-map
"0" 'delete-window
)
6511 (define-key ctl-x-map
"1" 'delete-other-windows
)
6512 (define-key ctl-x-map
"2" 'split-window-below
)
6513 (define-key ctl-x-map
"3" 'split-window-right
)
6514 (define-key ctl-x-map
"o" 'other-window
)
6515 (define-key ctl-x-map
"^" 'enlarge-window
)
6516 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
6517 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
6518 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
6519 (define-key ctl-x-map
"+" 'balance-windows
)
6520 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
6522 ;;; window.el ends here