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 (let ((window-combination-limit
115 ;; When `window-combination-limit' equals
116 ;; `temp-buffer' or `temp-buffer-resize' and
117 ;; `temp-buffer-resize-mode' is enabled in this
118 ;; buffer bind it to t so resizing steals space
119 ;; preferably from the window that was split.
120 (if (or (eq window-combination-limit
'temp-buffer
)
121 (and (eq window-combination-limit
123 temp-buffer-resize-mode
))
125 window-combination-limit
)))
126 (setq window
(display-buffer buffer action
)))
127 (setq frame
(window-frame window
))
128 (unless (eq frame
(selected-frame))
130 (setq minibuffer-scroll-window window
)
131 (set-window-hscroll window
0)
132 (with-selected-window window
133 (run-hooks 'temp-buffer-window-show-hook
)
134 (when temp-buffer-resize-mode
135 (resize-temp-buffer-window window
)))
136 ;; Return the window.
139 (defmacro with-temp-buffer-window
(buffer-or-name action quit-function
&rest body
)
140 "Evaluate BODY and display buffer specified by BUFFER-OR-NAME.
141 BUFFER-OR-NAME must specify either a live buffer or the name of a
142 buffer. If no buffer with such a name exists, create one.
144 Make sure the specified buffer is empty before evaluating BODY.
145 Do not make that buffer current for BODY. Instead, bind
146 `standard-output' to that buffer, so that output generated with
147 `prin1' and similar functions in BODY goes into that buffer.
149 After evaluating BODY, mark the specified buffer unmodified and
150 read-only, and display it in a window via `display-buffer'. Pass
151 ACTION as action argument to `display-buffer'. Automatically
152 shrink the window used if `temp-buffer-resize-mode' is enabled.
154 Return the value returned by BODY unless QUIT-FUNCTION specifies
155 a function. In that case, run the function with two arguments -
156 the window showing the specified buffer and the value returned by
157 BODY - and return the value returned by that function.
159 If the buffer is displayed on a new frame, the window manager may
160 decide to select that frame. In that case, it's usually a good
161 strategy if the function specified by QUIT-FUNCTION selects the
162 window showing the buffer before reading a value from the
163 minibuffer, for example, when asking a `yes-or-no-p' question.
165 This construct is similar to `with-output-to-temp-buffer' but
166 does neither put the buffer in help mode nor does it call
167 `temp-buffer-show-function'. It also runs different hooks,
168 namely `temp-buffer-window-setup-hook' (with the specified buffer
169 current) and `temp-buffer-window-show-hook' (with the specified
170 buffer current and the window showing it selected).
172 Since this macro calls `display-buffer', the window displaying
173 the buffer is usually not selected and the specified buffer
174 usually not made current. QUIT-FUNCTION can override that."
176 (let ((buffer (make-symbol "buffer"))
177 (window (make-symbol "window"))
178 (value (make-symbol "value")))
179 `(let* ((,buffer
(temp-buffer-window-setup ,buffer-or-name
))
180 (standard-output ,buffer
)
182 (with-current-buffer ,buffer
183 (setq ,value
(progn ,@body
))
184 (setq ,window
(temp-buffer-window-show ,buffer
,action
)))
186 (if (functionp ,quit-function
)
187 (funcall ,quit-function
,window
,value
)
190 ;; The following two functions are like `window-next-sibling' and
191 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
192 ;; they don't substitute the selected window for nil), and they return
193 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
194 ;; a minibuffer window).
195 (defun window-right (window)
196 "Return WINDOW's right sibling.
197 Return nil if WINDOW is the root window of its frame. WINDOW can
199 (and window
(window-parent window
) (window-next-sibling window
)))
201 (defun window-left (window)
202 "Return WINDOW's left sibling.
203 Return nil if WINDOW is the root window of its frame. WINDOW can
205 (and window
(window-parent window
) (window-prev-sibling window
)))
207 (defun window-child (window)
208 "Return WINDOW's first child window.
209 WINDOW can be any window."
210 (or (window-top-child window
) (window-left-child window
)))
212 (defun window-child-count (window)
213 "Return number of WINDOW's child windows.
214 WINDOW can be any window."
216 (when (and (windowp window
) (setq window
(window-child window
)))
218 (setq count
(1+ count
))
219 (setq window
(window-next-sibling window
))))
222 (defun window-last-child (window)
223 "Return last child window of WINDOW.
224 WINDOW can be any window."
225 (when (and (windowp window
) (setq window
(window-child window
)))
226 (while (window-next-sibling window
)
227 (setq window
(window-next-sibling window
))))
230 (defun window-normalize-buffer (buffer-or-name)
231 "Return buffer specified by BUFFER-OR-NAME.
232 BUFFER-OR-NAME must be either a buffer or a string naming a live
233 buffer and defaults to the current buffer."
235 ((not buffer-or-name
)
237 ((bufferp buffer-or-name
)
238 (if (buffer-live-p buffer-or-name
)
240 (error "Buffer %s is not a live buffer" buffer-or-name
)))
241 ((get-buffer buffer-or-name
))
243 (error "No such buffer %s" buffer-or-name
))))
245 (defun window-normalize-frame (frame)
246 "Return frame specified by FRAME.
247 FRAME must be a live frame and defaults to the selected frame."
249 (if (frame-live-p frame
)
251 (error "%s is not a live frame" frame
))
254 (defun window-normalize-window (window &optional live-only
)
255 "Return the window specified by WINDOW.
256 If WINDOW is nil, return the selected window. Otherwise, if
257 WINDOW is a live or an internal window, return WINDOW; if
258 LIVE-ONLY is non-nil, return WINDOW for a live window only.
259 Otherwise, signal an error."
264 (if (window-live-p window
)
266 (error "%s is not a live window" window
)))
267 ((window-valid-p window
)
270 (error "%s is not a valid window" window
))))
272 (defvar ignore-window-parameters nil
273 "If non-nil, standard functions ignore window parameters.
274 The functions currently affected by this are `split-window',
275 `delete-window', `delete-other-windows' and `other-window'.
277 An application may bind this to a non-nil value around calls to
278 these functions to inhibit processing of window parameters.")
280 (defconst window-safe-min-height
1
281 "The absolute minimum number of lines of a window.
282 Anything less might crash Emacs.")
284 (defcustom window-min-height
4
285 "The minimum number of lines of any window.
286 The value has to accommodate a mode- or header-line if present.
287 A value less than `window-safe-min-height' is ignored. The value
288 of this variable is honored when windows are resized or split.
290 Applications should never rebind this variable. To resize a
291 window to a height less than the one specified here, an
292 application should instead call `window-resize' with a non-nil
293 IGNORE argument. In order to have `split-window' make a window
294 shorter, explicitly specify the SIZE argument of that function."
299 (defconst window-safe-min-width
2
300 "The absolute minimum number of columns of a window.
301 Anything less might crash Emacs.")
303 (defcustom window-min-width
10
304 "The minimum number of columns of any window.
305 The value has to accommodate margins, fringes, or scrollbars if
306 present. A value less than `window-safe-min-width' is ignored.
307 The value of this variable is honored when windows are resized or
310 Applications should never rebind this variable. To resize a
311 window to a width less than the one specified here, an
312 application should instead call `window-resize' with a non-nil
313 IGNORE argument. In order to have `split-window' make a window
314 narrower, explicitly specify the SIZE argument of that function."
319 (defun window-combined-p (&optional window horizontal
)
320 "Return non-nil if WINDOW has siblings in a given direction.
321 WINDOW must be a valid window and defaults to the selected one.
323 HORIZONTAL determines a direction for the window combination.
324 If HORIZONTAL is omitted or nil, return non-nil if WINDOW is part
325 of a vertical window combination.
326 If HORIZONTAL is non-nil, return non-nil if WINDOW is part of a
327 horizontal window combination."
328 (setq window
(window-normalize-window window
))
329 (let ((parent (window-parent window
)))
332 (window-left-child parent
)
333 (window-top-child parent
)))))
335 (defun window-combinations (window &optional horizontal
)
336 "Return largest number of windows vertically arranged within WINDOW.
337 WINDOW must be a valid window and defaults to the selected one.
338 If HORIZONTAL is non-nil, return the largest number of
339 windows horizontally arranged within WINDOW."
340 (setq window
(window-normalize-window window
))
342 ((window-live-p window
)
343 ;; If WINDOW is live, return 1.
346 (window-left-child window
)
347 (window-top-child window
))
348 ;; If WINDOW is iso-combined, return the sum of the values for all
349 ;; child windows of WINDOW.
350 (let ((child (window-child window
))
354 (+ (window-combinations child horizontal
)
356 (setq child
(window-right child
)))
359 ;; If WINDOW is not iso-combined, return the maximum value of any
360 ;; child window of WINDOW.
361 (let ((child (window-child window
))
365 (max (window-combinations child horizontal
)
367 (setq child
(window-right child
)))
370 (defun walk-window-tree-1 (fun walk-window-tree-window any
&optional sub-only
)
371 "Helper function for `walk-window-tree' and `walk-window-subtree'."
372 (let (walk-window-tree-buffer)
373 (while walk-window-tree-window
374 (setq walk-window-tree-buffer
375 (window-buffer walk-window-tree-window
))
376 (when (or walk-window-tree-buffer any
)
377 (funcall fun walk-window-tree-window
))
378 (unless walk-window-tree-buffer
380 fun
(window-left-child walk-window-tree-window
) any
)
382 fun
(window-top-child walk-window-tree-window
) any
))
384 (setq walk-window-tree-window nil
)
385 (setq walk-window-tree-window
386 (window-right walk-window-tree-window
))))))
388 (defun walk-window-tree (fun &optional frame any minibuf
)
389 "Run function FUN on each live window of FRAME.
390 FUN must be a function with one argument - a window. FRAME must
391 be a live frame and defaults to the selected one. ANY, if
392 non-nil, means to run FUN on all live and internal windows of
395 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
396 window even if it isn't active. MINIBUF nil or omitted means run
397 FUN on FRAME's minibuffer window only if it's active. In both
398 cases the minibuffer window must be part of FRAME. MINIBUF
399 neither nil nor t means never run FUN on the minibuffer window.
401 This function performs a pre-order, depth-first traversal of the
402 window tree. If FUN changes the window tree, the result is
404 (setq frame
(window-normalize-frame frame
))
405 (walk-window-tree-1 fun
(frame-root-window frame
) any
)
406 (when (memq minibuf
'(nil t
))
407 ;; Run FUN on FRAME's minibuffer window if requested.
408 (let ((minibuffer-window (minibuffer-window frame
)))
409 (when (and (window-live-p minibuffer-window
)
410 (eq (window-frame minibuffer-window
) frame
)
412 (minibuffer-window-active-p minibuffer-window
)))
413 (funcall fun minibuffer-window
)))))
415 (defun walk-window-subtree (fun &optional window any
)
416 "Run function FUN on the subtree of windows rooted at WINDOW.
417 WINDOW defaults to the selected window. FUN must be a function
418 with one argument - a window. By default, run FUN only on live
419 windows of the subtree. If the optional argument ANY is non-nil,
420 run FUN on all live and internal windows of the subtree. If
421 WINDOW is live, run FUN on WINDOW only.
423 This function performs a pre-order, depth-first traversal of the
424 subtree rooted at WINDOW. If FUN changes that tree, the result
426 (setq window
(window-normalize-window window
))
427 (walk-window-tree-1 fun window any t
))
429 (defun window-with-parameter (parameter &optional value frame any minibuf
)
430 "Return first window on FRAME with PARAMETER non-nil.
431 FRAME defaults to the selected frame. Optional argument VALUE
432 non-nil means only return a window whose window-parameter value
433 for PARAMETER equals VALUE (comparison is done with `equal').
434 Optional argument ANY non-nil means consider internal windows
437 Optional argument MINIBUF t means consider FRAME's minibuffer
438 window even if it isn't active. MINIBUF nil or omitted means
439 consider FRAME's minibuffer window only if it's active. In both
440 cases the minibuffer window must be part of FRAME. MINIBUF
441 neither nil nor t means never consider the minibuffer window."
446 (when (and (setq this-value
(window-parameter window parameter
))
447 (or (not value
) (equal value this-value
)))
448 (throw 'found window
)))
449 frame any minibuf
))))
452 (defun window-atom-root (&optional window
)
453 "Return root of atomic window WINDOW is a part of.
454 WINDOW must be a valid window and defaults to the selected one.
455 Return nil if WINDOW is not part of an atomic window."
456 (setq window
(window-normalize-window window
))
458 (while (and window
(window-parameter window
'window-atom
))
460 (setq window
(window-parent window
)))
463 (defun window-make-atom (window)
464 "Make WINDOW an atomic window.
465 WINDOW must be an internal window. Return WINDOW."
466 (if (not (window-child window
))
467 (error "Window %s is not an internal window" window
)
470 (set-window-parameter window
'window-atom t
))
474 (defun display-buffer-in-atom-window (buffer alist
)
475 "Display BUFFER in an atomic window.
476 This function displays BUFFER in a new window that will be
477 combined with an existing window to form an atomic window. If
478 the existing window is already part of an atomic window, add the
479 new window to that atomic window. Operations like `split-window'
480 or `delete-window', when applied to a constituent of an atomic
481 window, are applied atomically to the root of that atomic window.
483 ALIST is an association list of symbols and values. The
484 following symbols can be used.
486 `window' specifies the existing window the new window shall be
487 combined with. Use `window-atom-root' to make the new window a
488 sibling of an atomic window's root. If an internal window is
489 specified here, all children of that window become part of the
490 atomic window too. If no window is specified, the new window
491 becomes a sibling of the selected window.
493 `side' denotes the side of the existing window where the new
494 window shall be located. Valid values are `below', `right',
495 `above' and `left'. The default is `below'.
497 The return value is the new window, nil when creating that window
499 (let ((ignore-window-parameters t
)
500 (window-combination-limit t
)
501 (window (cdr (assq 'window alist
)))
502 (side (cdr (assq 'side alist
)))
504 (setq window
(window-normalize-window window
))
505 ;; Split off new window
506 (when (setq new
(split-window window nil side
))
507 ;; Make sure we have a valid atomic window.
508 (window-make-atom (window-parent window
))
509 ;; Display BUFFER in NEW and return NEW.
510 (window--display-buffer
511 buffer new
'window display-buffer-mark-dedicated
))))
513 (defun window--atom-check-1 (window)
514 "Subroutine of `window--atom-check'."
516 (if (window-parameter window
'window-atom
)
518 (when (or (catch 'reset
521 (if (window-parameter window
'window-atom
)
522 (setq count
(1+ count
))
525 ;; count >= 1 must hold here. If there's no other
526 ;; window around dissolve this atomic window.
528 ;; Dissolve atomic window.
531 (set-window-parameter window
'window-atom nil
))
534 (unless (window-buffer window
)
535 (window--atom-check-1 (window-left-child window
))
536 (window--atom-check-1 (window-top-child window
))))
537 ;; Check right sibling
538 (window--atom-check-1 (window-right window
))))
540 (defun window--atom-check (&optional frame
)
541 "Check atomicity of all windows on FRAME.
542 FRAME defaults to the selected frame. If an atomic window is
543 wrongly configured, reset the atomicity of all its windows on
544 FRAME to nil. An atomic window is wrongly configured if it has
545 no child windows or one of its child windows is not atomic."
546 (window--atom-check-1 (frame-root-window frame
)))
549 (defvar window-sides
'(left top right bottom
)
552 (defcustom window-sides-vertical nil
553 "If non-nil, left and right side windows are full height.
554 Otherwise, top and bottom side windows are full width."
559 (defcustom window-sides-slots
'(nil nil nil nil
)
560 "Maximum number of side window slots.
561 The value is a list of four elements specifying the number of
562 side window slots on (in this order) the left, top, right and
563 bottom side of each frame. If an element is a number, this means
564 to display at most that many side windows on the corresponding
565 side. If an element is nil, this means there's no bound on the
566 number of slots on that side."
571 :value
(nil nil nil nil
)
574 :help-echo
"Maximum slots of left side window."
576 :format
"%[Left%] %v\n"
577 (const :tag
"Unlimited" :format
"%t" nil
)
578 (integer :tag
"Number" :value
2 :size
5))
581 :help-echo
"Maximum slots of top side window."
583 :format
"%[Top%] %v\n"
584 (const :tag
"Unlimited" :format
"%t" nil
)
585 (integer :tag
"Number" :value
3 :size
5))
588 :help-echo
"Maximum slots of right side window."
590 :format
"%[Right%] %v\n"
591 (const :tag
"Unlimited" :format
"%t" nil
)
592 (integer :tag
"Number" :value
2 :size
5))
595 :help-echo
"Maximum slots of bottom side window."
597 :format
"%[Bottom%] %v\n"
598 (const :tag
"Unlimited" :format
"%t" nil
)
599 (integer :tag
"Number" :value
3 :size
5)))
602 (defun window--major-non-side-window (&optional frame
)
603 "Return the major non-side window of frame FRAME.
604 The optional argument FRAME must be a live frame and defaults to
607 If FRAME has at least one side window, the major non-side window
608 is either an internal non-side window such that all other
609 non-side windows on FRAME descend from it, or the single live
610 non-side window of FRAME. If FRAME has no side windows, return
612 (let ((frame (window-normalize-frame frame
))
614 ;; Set major to the _last_ window found by `walk-window-tree' that
615 ;; is not a side window but has a side window as its sibling.
618 (and (not (window-parameter window
'window-side
))
619 (or (and (setq sibling
(window-prev-sibling window
))
620 (window-parameter sibling
'window-side
))
621 (and (setq sibling
(window-next-sibling window
))
622 (window-parameter sibling
'window-side
)))
623 (setq major window
)))
625 (or major
(frame-root-window frame
))))
627 (defun window--major-side-window (side)
628 "Return major side window on SIDE.
629 SIDE must be one of the symbols `left', `top', `right' or
630 `bottom'. Return nil if no such window exists."
631 (let ((root (frame-root-window))
633 ;; (1) If a window on the opposite side exists, return that window's
635 ;; (2) If the new window shall span the entire side, return the
636 ;; frame's root window.
637 ;; (3) If a window on an orthogonal side exists, return that
639 ;; (4) Otherwise return the frame's root window.
641 ((or (and (eq side
'left
)
642 (setq window
(window-with-parameter 'window-side
'right nil t
)))
644 (setq window
(window-with-parameter 'window-side
'bottom nil t
))))
645 (window-prev-sibling window
))
646 ((or (and (eq side
'right
)
647 (setq window
(window-with-parameter 'window-side
'left nil t
)))
648 (and (eq side
'bottom
)
649 (setq window
(window-with-parameter 'window-side
'top nil t
))))
650 (window-next-sibling window
))
651 ((memq side
'(left right
))
653 (window-sides-vertical
655 ((setq window
(window-with-parameter 'window-side
'top nil t
))
656 (window-next-sibling window
))
657 ((setq window
(window-with-parameter 'window-side
'bottom nil t
))
658 (window-prev-sibling window
))
660 ((memq side
'(top bottom
))
662 ((not window-sides-vertical
)
664 ((setq window
(window-with-parameter 'window-side
'left nil t
))
665 (window-next-sibling window
))
666 ((setq window
(window-with-parameter 'window-side
'right nil t
))
667 (window-prev-sibling window
))
670 (defun display-buffer-in-major-side-window (buffer side slot
&optional alist
)
671 "Display BUFFER in a new window on SIDE of the selected frame.
672 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
673 specifies the slot to use. ALIST is an association list of
674 symbols and values as passed to `display-buffer-in-side-window'.
675 This function may be called only if no window on SIDE exists yet.
676 The new window automatically becomes the \"major\" side window on
677 SIDE. Return the new window, nil if its creation window failed."
678 (let* ((root (frame-root-window))
679 (left-or-right (memq side
'(left right
)))
680 (size (or (assq 'size alist
)
681 (/ (window-total-size (frame-root-window) left-or-right
)
682 ;; By default use a fourth of the size of the
683 ;; frame's root window. This has to be made
684 ;; customizable via ALIST.
686 (major (window--major-side-window side
))
687 (selected-window (selected-window))
689 ((eq side
'top
) 'above
)
690 ((eq side
'bottom
) 'below
)
692 ;; The following two bindings will tell `split-window' to take
693 ;; the space for the new window from `major' and not make a new
694 ;; parent window unless needed.
695 (window-combination-resize 'side
)
696 (window-combination-limit nil
)
697 (new (split-window major
(- size
) on-side
))
700 ;; Initialize `window-side' parameter of new window to SIDE.
701 (set-window-parameter new
'window-side side
)
702 ;; Install `window-slot' parameter of new window.
703 (set-window-parameter new
'window-slot slot
)
704 ;; Install `delete-window' parameter thus making sure that when
705 ;; the new window is deleted, a side window on the opposite side
706 ;; does not get resized.
707 (set-window-parameter new
'delete-window
'delete-side-window
)
708 ;; Install BUFFER in new window and return NEW.
709 (window--display-buffer buffer new
'window
'side
))))
711 (defun delete-side-window (window)
712 "Delete side window WINDOW."
713 (let ((window-combination-resize
714 (window-parameter (window-parent window
) 'window-side
))
715 (ignore-window-parameters t
))
716 (delete-window window
)))
718 (defun display-buffer-in-side-window (buffer alist
)
719 "Display BUFFER in a window on side SIDE of the selected frame.
720 ALIST is an association list of symbols and values. The
721 following symbols can be used:
723 `side' denotes the side of the existing window where the new
724 window shall be located. Valid values are `bottom', `right',
725 `top' and `left'. The default is `bottom'.
727 `slot' if non-nil, specifies the window slot where to display
728 BUFFER. A value of zero or nil means use the middle slot on
729 the specified side. A negative value means use a slot
730 preceding (that is, above or on the left of) the middle slot.
731 A positive value means use a slot following (that is, below or
732 on the right of) the middle slot. The default is zero."
733 (let ((side (or (cdr (assq 'side alist
)) 'bottom
))
734 (slot (or (cdr (assq 'slot alist
)) 0))
737 ((not (memq side
'(top bottom left right
)))
738 (error "Invalid side %s specified" side
))
739 ((not (numberp slot
))
740 (error "Invalid slot %s specified" slot
)))
742 (let* ((major (window-with-parameter 'window-side side nil t
))
743 ;; `major' is the major window on SIDE, `windows' the list of
744 ;; life windows on SIDE.
750 (when (eq (window-parameter window
'window-side
) side
)
751 (setq windows
(cons window windows
)))))
752 (nreverse windows
))))
753 (slots (when major
(max 1 (window-child-count major
))))
759 ((eq side
'bottom
) 3))
761 (selected-window (selected-window))
762 window this-window this-slot prev-window next-window
763 best-window best-slot abs-slot new-window
)
766 ((and (numberp max-slots
) (<= max-slots
0))
767 ;; No side-slots available on this side. Don't create an error,
771 ;; No major window exists on this side, make one.
772 (display-buffer-in-major-side-window buffer side slot alist
))
774 ;; Scan windows on SIDE.
776 (dolist (window windows
)
777 (setq this-slot
(window-parameter window
'window-slot
))
779 ;; The following should not happen and probably be checked
780 ;; by window--side-check.
781 ((not (numberp this-slot
)))
783 ;; A window with a matching slot has been found.
784 (setq this-window window
)
787 ;; Check if this window has a better slot value wrt the
788 ;; slot of the window we want.
790 (if (or (and (> this-slot
0) (> slot
0))
791 (and (< this-slot
0) (< slot
0)))
792 (abs (- slot this-slot
))
793 (+ (abs slot
) (abs this-slot
))))
794 (unless (and best-slot
(<= best-slot abs-slot
))
795 (setq best-window window
)
796 (setq best-slot abs-slot
))
799 (setq prev-window window
))
801 (setq next-window window
)))))))
803 ;; `this-window' is the first window with the same SLOT.
804 ;; `prev-window' is the window with the largest slot < SLOT. A new
805 ;; window will be created after it.
806 ;; `next-window' is the window with the smallest slot > SLOT. A new
807 ;; window will be created before it.
808 ;; `best-window' is the window with the smallest absolute difference
809 ;; of its slot and SLOT.
811 ;; Note: We dedicate the window used softly to its buffer to
812 ;; avoid that "other" (non-side) buffer display functions steal
813 ;; it from us. This must eventually become customizable via
814 ;; ALIST (or, better, avoided in the "other" functions).
816 ;; Reuse `this-window'.
817 (window--display-buffer buffer this-window
'reuse
'side
))
818 (and (or (not max-slots
) (< slots max-slots
))
820 ;; Make new window before `next-window'.
822 (if (memq side
'(left right
)) 'above
'left
))
823 (window-combination-resize 'side
))
824 (setq window
(split-window next-window nil next-side
))
825 ;; When the new window is deleted, its space
826 ;; is returned to other side windows.
827 (set-window-parameter
828 window
'delete-window
'delete-side-window
)
831 ;; Make new window after `prev-window'.
833 (if (memq side
'(left right
)) 'below
'right
))
834 (window-combination-resize 'side
))
835 (setq window
(split-window prev-window nil prev-side
))
836 ;; When the new window is deleted, its space
837 ;; is returned to other side windows.
838 (set-window-parameter
839 window
'delete-window
'delete-side-window
)
841 (set-window-parameter window
'window-slot slot
)
842 (window--display-buffer buffer window
'window
'side
))
844 ;; Reuse `best-window'.
846 ;; Give best-window the new slot value.
847 (set-window-parameter best-window
'window-slot slot
)
848 (window--display-buffer buffer best-window
'reuse
'side
)))))))))
850 (defun window--side-check (&optional frame
)
851 "Check the side window configuration of FRAME.
852 FRAME defaults to the selected frame.
854 A valid side window configuration preserves the following two
857 - If there exists a window whose window-side parameter is
858 non-nil, there must exist at least one live window whose
859 window-side parameter is nil.
861 - If a window W has a non-nil window-side parameter (i) it must
862 have a parent window and that parent's window-side parameter
863 must be either nil or the same as for W, and (ii) any child
864 window of W must have the same window-side parameter as W.
866 If the configuration is invalid, reset the window-side parameters
867 of all windows on FRAME to nil."
868 (let (left top right bottom none side parent parent-side
)
869 (when (or (catch 'reset
872 (setq side
(window-parameter window
'window-side
))
873 (setq parent
(window-parent window
))
875 (and parent
(window-parameter parent
'window-side
)))
876 ;; The following `cond' seems a bit tedious, but I'd
877 ;; rather stick to using just the stack.
880 (when (not (eq parent-side side
))
881 ;; A parent whose window-side is non-nil must
882 ;; have a child with the same window-side.
885 (when (window-buffer window
)
886 ;; Record that we have at least one non-side,
889 ((if (memq side
'(left top
))
890 (window-prev-sibling window
)
891 (window-next-sibling window
))
892 ;; Left and top major side windows must not have a
893 ;; previous sibling, right and bottom major side
894 ;; windows must not have a next sibling.
896 ;; Now check that there's no more than one major
897 ;; window for any of left, top, right and bottom.
899 (if left
(throw 'reset t
) (setq left t
)))
901 (if top
(throw 'reset t
) (setq top t
)))
903 (if right
(throw 'reset t
) (setq right t
)))
905 (if bottom
(throw 'reset t
) (setq bottom t
)))
909 ;; If there's a side window, there must be at least one
911 (and (or left top right bottom
) (not none
)))
914 (set-window-parameter window
'window-side nil
))
917 (defun window--check (&optional frame
)
918 "Check atomic and side windows on FRAME.
919 FRAME defaults to the selected frame."
920 (window--side-check frame
)
921 (window--atom-check frame
))
924 (defvar window-size-fixed nil
925 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
926 If the value is `height', then only the window's height is fixed.
927 If the value is `width', then only the window's width is fixed.
928 Any other non-nil value fixes both the width and the height.
930 Emacs won't change the size of any window displaying that buffer,
931 unless it has no other choice (like when deleting a neighboring
933 (make-variable-buffer-local 'window-size-fixed
)
935 (defun window--size-ignore-p (window ignore
)
936 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
937 (if (window-valid-p ignore
) (eq window ignore
) ignore
))
939 (defun window-min-size (&optional window horizontal ignore
)
940 "Return the minimum size of WINDOW.
941 WINDOW must be a valid window and defaults to the selected one.
942 Optional argument HORIZONTAL non-nil means return the minimum
943 number of columns of WINDOW; otherwise return the minimum number
946 Optional argument IGNORE, if non-nil, means ignore restrictions
947 imposed by fixed size windows, `window-min-height' or
948 `window-min-width' settings. If IGNORE equals `safe', live
949 windows may get as small as `window-safe-min-height' lines and
950 `window-safe-min-width' columns. If IGNORE is a window, ignore
951 restrictions for that window only. Any other non-nil value
952 means ignore all of the above restrictions for all windows."
954 (window-normalize-window window
) horizontal ignore
))
956 (defun window--min-size-1 (window horizontal ignore
)
957 "Internal function of `window-min-size'."
958 (let ((sub (window-child window
)))
961 ;; WINDOW is an internal window.
962 (if (window-combined-p sub horizontal
)
963 ;; The minimum size of an iso-combination is the sum of
964 ;; the minimum sizes of its child windows.
967 (window--min-size-1 sub horizontal ignore
)))
968 (setq sub
(window-right sub
)))
969 ;; The minimum size of an ortho-combination is the maximum of
970 ;; the minimum sizes of its child windows.
972 (setq value
(max value
973 (window--min-size-1 sub horizontal ignore
)))
974 (setq sub
(window-right sub
))))
976 (with-current-buffer (window-buffer window
)
978 ((and (not (window--size-ignore-p window ignore
))
979 (window-size-fixed-p window horizontal
))
980 ;; The minimum size of a fixed size window is its size.
981 (window-total-size window horizontal
))
982 ((or (eq ignore
'safe
) (eq ignore window
))
983 ;; If IGNORE equals `safe' or WINDOW return the safe values.
984 (if horizontal window-safe-min-width window-safe-min-height
))
986 ;; For the minimum width of a window take fringes and
987 ;; scroll-bars into account. This is questionable and should
988 ;; be removed as soon as we are able to split (and resize)
989 ;; windows such that the new (or resized) windows can get a
990 ;; size less than the user-specified `window-min-height' and
991 ;; `window-min-width'.
992 (let ((frame (window-frame window
))
993 (fringes (window-fringes window
))
994 (scroll-bars (window-scroll-bars window
)))
996 (+ window-safe-min-width
997 (ceiling (car fringes
) (frame-char-width frame
))
998 (ceiling (cadr fringes
) (frame-char-width frame
))
1000 ((memq (nth 2 scroll-bars
) '(left right
))
1001 (nth 1 scroll-bars
))
1002 ((memq (frame-parameter frame
'vertical-scroll-bars
)
1004 (ceiling (or (frame-parameter frame
'scroll-bar-width
) 14)
1005 (frame-char-width)))
1007 (if (and (not (window--size-ignore-p window ignore
))
1008 (numberp window-min-width
))
1012 ;; For the minimum height of a window take any mode- or
1013 ;; header-line into account.
1014 (max (+ window-safe-min-height
1015 (if header-line-format
1 0)
1016 (if mode-line-format
1 0))
1017 (if (and (not (window--size-ignore-p window ignore
))
1018 (numberp window-min-height
))
1022 (defun window-sizable (window delta
&optional horizontal ignore
)
1023 "Return DELTA if DELTA lines can be added to WINDOW.
1024 WINDOW must be a valid window and defaults to the selected one.
1025 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1026 columns can be added to WINDOW. A return value of zero means
1027 that no lines (or columns) can be added to WINDOW.
1029 This function looks only at WINDOW and, recursively, its child
1030 windows. The function `window-resizable' looks at other windows
1033 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1034 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1035 return the maximum value in the range 0..DELTA by which WINDOW
1038 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1039 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1040 return the minimum value in the range DELTA..0 by which WINDOW
1043 Optional argument IGNORE non-nil means ignore restrictions
1044 imposed by fixed size windows, `window-min-height' or
1045 `window-min-width' settings. If IGNORE equals `safe', live
1046 windows may get as small as `window-safe-min-height' lines and
1047 `window-safe-min-width' columns. If IGNORE is a window, ignore
1048 restrictions for that window only. Any other non-nil value means
1049 ignore all of the above restrictions for all windows."
1050 (setq window
(window-normalize-window window
))
1053 (max (- (window-min-size window horizontal ignore
)
1054 (window-total-size window horizontal
))
1056 ((window--size-ignore-p window ignore
)
1059 (if (window-size-fixed-p window horizontal
)
1064 (defun window-sizable-p (window delta
&optional horizontal ignore
)
1065 "Return t if WINDOW can be resized by DELTA lines.
1066 WINDOW must be a valid window and defaults to the selected one.
1067 For the meaning of the arguments of this function see the
1068 doc-string of `window-sizable'."
1069 (setq window
(window-normalize-window window
))
1071 (>= (window-sizable window delta horizontal ignore
) delta
)
1072 (<= (window-sizable window delta horizontal ignore
) delta
)))
1074 (defun window--size-fixed-1 (window horizontal
)
1075 "Internal function for `window-size-fixed-p'."
1076 (let ((sub (window-child window
)))
1079 ;; WINDOW is an internal window.
1080 (if (window-combined-p sub horizontal
)
1081 ;; An iso-combination is fixed size if all its child
1082 ;; windows are fixed-size.
1085 (unless (window--size-fixed-1 sub horizontal
)
1086 ;; We found a non-fixed-size child window, so
1087 ;; WINDOW's size is not fixed.
1089 (setq sub
(window-right sub
)))
1090 ;; All child windows are fixed-size, so WINDOW's size is
1093 ;; An ortho-combination is fixed-size if at least one of its
1094 ;; child windows is fixed-size.
1096 (when (window--size-fixed-1 sub horizontal
)
1097 ;; We found a fixed-size child window, so WINDOW's size
1100 (setq sub
(window-right sub
))))
1101 ;; WINDOW is a live window.
1102 (with-current-buffer (window-buffer window
)
1104 (memq window-size-fixed
'(width t
))
1105 (memq window-size-fixed
'(height t
))))))))
1107 (defun window-size-fixed-p (&optional window horizontal
)
1108 "Return non-nil if WINDOW's height is fixed.
1109 WINDOW must be a valid window and defaults to the selected one.
1110 Optional argument HORIZONTAL non-nil means return non-nil if
1111 WINDOW's width is fixed.
1113 If this function returns nil, this does not necessarily mean that
1114 WINDOW can be resized in the desired direction. The function
1115 `window-resizable' can tell that."
1116 (window--size-fixed-1
1117 (window-normalize-window window
) horizontal
))
1119 (defun window--min-delta-1 (window delta
&optional horizontal ignore trail noup
)
1120 "Internal function for `window-min-delta'."
1121 (if (not (window-parent window
))
1122 ;; If we can't go up, return zero.
1124 ;; Else try to find a non-fixed-size sibling of WINDOW.
1125 (let* ((parent (window-parent window
))
1126 (sub (window-child parent
)))
1128 (if (window-combined-p sub horizontal
)
1129 ;; In an iso-combination throw DELTA if we find at least one
1130 ;; child window and that window is either not fixed-size or
1131 ;; we can ignore fixed-sizeness.
1132 (let ((skip (eq trail
'after
)))
1136 (setq skip
(eq trail
'before
)))
1138 ((and (not (window--size-ignore-p window ignore
))
1139 (window-size-fixed-p sub horizontal
)))
1141 ;; We found a non-fixed-size child window.
1142 (throw 'done delta
)))
1143 (setq sub
(window-right sub
))))
1144 ;; In an ortho-combination set DELTA to the minimum value by
1145 ;; which other child windows can shrink.
1147 (unless (eq sub window
)
1150 (- (window-total-size sub horizontal
)
1151 (window-min-size sub horizontal ignore
)))))
1152 (setq sub
(window-right sub
))))
1155 (window--min-delta-1 parent delta horizontal ignore trail
))))))
1157 (defun window-min-delta (&optional window horizontal ignore trail noup nodown
)
1158 "Return number of lines by which WINDOW can be shrunk.
1159 WINDOW must be a valid window and defaults to the selected one.
1160 Return zero if WINDOW cannot be shrunk.
1162 Optional argument HORIZONTAL non-nil means return number of
1163 columns by which WINDOW can be shrunk.
1165 Optional argument IGNORE non-nil means ignore restrictions
1166 imposed by fixed size windows, `window-min-height' or
1167 `window-min-width' settings. If IGNORE is a window, ignore
1168 restrictions for that window only. If IGNORE equals `safe',
1169 live windows may get as small as `window-safe-min-height' lines
1170 and `window-safe-min-width' columns. Any other non-nil value
1171 means ignore all of the above restrictions for all windows.
1173 Optional argument TRAIL restricts the windows that can be enlarged.
1174 If its value is `before', only windows to the left of or above WINDOW
1175 can be enlarged. If it is `after', only windows to the right of or
1176 below WINDOW can be enlarged.
1178 Optional argument NOUP non-nil means don't go up in the window
1179 tree, but try to enlarge windows within WINDOW's combination only.
1181 Optional argument NODOWN non-nil means don't check whether WINDOW
1182 itself (and its child windows) can be shrunk; check only whether
1183 at least one other window can be enlarged appropriately."
1184 (setq window
(window-normalize-window window
))
1185 (let ((size (window-total-size window horizontal
))
1186 (minimum (window-min-size window horizontal ignore
)))
1189 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1190 (window--min-delta-1 window size horizontal ignore trail noup
))
1192 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1193 ;; there's nothing to recover.
1196 ;; Otherwise, try to recover whatever WINDOW is larger than its
1198 (window--min-delta-1
1199 window
(- size minimum
) horizontal ignore trail noup
)))))
1201 (defun window--max-delta-1 (window delta
&optional horizontal ignore trail noup
)
1202 "Internal function of `window-max-delta'."
1203 (if (not (window-parent window
))
1204 ;; Can't go up. Return DELTA.
1206 (let* ((parent (window-parent window
))
1207 (sub (window-child parent
)))
1209 (if (window-combined-p sub horizontal
)
1210 ;; For an iso-combination calculate how much we can get from
1211 ;; other child windows.
1212 (let ((skip (eq trail
'after
)))
1216 (setq skip
(eq trail
'before
)))
1221 (- (window-total-size sub horizontal
)
1222 (window-min-size sub horizontal ignore
))))))
1223 (setq sub
(window-right sub
))))
1224 ;; For an ortho-combination throw DELTA when at least one
1225 ;; child window is fixed-size.
1227 (when (and (not (eq sub window
))
1228 (not (window--size-ignore-p sub ignore
))
1229 (window-size-fixed-p sub horizontal
))
1230 (throw 'fixed delta
))
1231 (setq sub
(window-right sub
))))
1233 ;; When NOUP is nil, DELTA is all we can get.
1235 ;; Else try with parent of WINDOW, passing the DELTA we
1236 ;; recovered so far.
1237 (window--max-delta-1 parent delta horizontal ignore trail
))))))
1239 (defun window-max-delta (&optional window horizontal ignore trail noup nodown
)
1240 "Return maximum number of lines by which WINDOW can be enlarged.
1241 WINDOW must be a valid window and defaults to the selected one.
1242 The return value is zero if WINDOW cannot be enlarged.
1244 Optional argument HORIZONTAL non-nil means return maximum number
1245 of columns by which WINDOW can be enlarged.
1247 Optional argument IGNORE non-nil means ignore restrictions
1248 imposed by fixed size windows, `window-min-height' or
1249 `window-min-width' settings. If IGNORE is a window, ignore
1250 restrictions for that window only. If IGNORE equals `safe',
1251 live windows may get as small as `window-safe-min-height' lines
1252 and `window-safe-min-width' columns. Any other non-nil value means
1253 ignore all of the above restrictions for all windows.
1255 Optional argument TRAIL restricts the windows that can be enlarged.
1256 If its value is `before', only windows to the left of or above WINDOW
1257 can be enlarged. If it is `after', only windows to the right of or
1258 below WINDOW can be enlarged.
1260 Optional argument NOUP non-nil means don't go up in the window
1261 tree but try to obtain the entire space from windows within
1262 WINDOW's combination.
1264 Optional argument NODOWN non-nil means do not check whether
1265 WINDOW itself (and its child windows) can be enlarged; check
1266 only whether other windows can be shrunk appropriately."
1267 (setq window
(window-normalize-window window
))
1268 (if (and (not (window--size-ignore-p window ignore
))
1269 (not nodown
) (window-size-fixed-p window horizontal
))
1270 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1273 ;; WINDOW has no fixed size.
1274 (window--max-delta-1 window
0 horizontal ignore trail noup
)))
1276 ;; Make NOUP also inhibit the min-size check.
1277 (defun window--resizable (window delta
&optional horizontal ignore trail noup nodown
)
1278 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1279 WINDOW must be a valid window and defaults to the selected one.
1280 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1281 can be resized horizontally by DELTA columns. A return value of
1282 zero means that WINDOW is not resizable.
1284 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1285 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1286 return the maximum value in the range 0..DELTA by which WINDOW
1289 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1290 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1291 return the minimum value in the range DELTA..0 that can be used
1292 for shrinking WINDOW.
1294 Optional argument IGNORE non-nil means ignore restrictions
1295 imposed by fixed size windows, `window-min-height' or
1296 `window-min-width' settings. If IGNORE is a window, ignore
1297 restrictions for that window only. If IGNORE equals `safe',
1298 live windows may get as small as `window-safe-min-height' lines
1299 and `window-safe-min-width' columns. Any other non-nil value
1300 means ignore all of the above restrictions for all windows.
1302 Optional argument TRAIL `before' means only windows to the left
1303 of or below WINDOW can be shrunk. Optional argument TRAIL
1304 `after' means only windows to the right of or above WINDOW can be
1307 Optional argument NOUP non-nil means don't go up in the window
1308 tree but check only whether space can be obtained from (or given
1309 to) WINDOW's siblings.
1311 Optional argument NODOWN non-nil means don't go down in the
1312 window tree. This means do not check whether resizing would
1313 violate size restrictions of WINDOW or its child windows."
1314 (setq window
(window-normalize-window window
))
1317 (max (- (window-min-delta window horizontal ignore trail noup nodown
))
1320 (min (window-max-delta window horizontal ignore trail noup nodown
)
1324 (defun window--resizable-p (window delta
&optional horizontal ignore trail noup nodown
)
1325 "Return t if WINDOW can be resized vertically by DELTA lines.
1326 WINDOW must be a valid window and defaults to the selected one.
1327 For the meaning of the arguments of this function see the
1328 doc-string of `window--resizable'."
1329 (setq window
(window-normalize-window window
))
1331 (>= (window--resizable window delta horizontal ignore trail noup nodown
)
1333 (<= (window--resizable window delta horizontal ignore trail noup nodown
)
1336 (defun window-resizable (window delta
&optional horizontal ignore
)
1337 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1338 WINDOW must be a valid window and defaults to the selected one.
1339 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1340 can be resized horizontally by DELTA columns. A return value of
1341 zero means that WINDOW is not resizable.
1343 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1344 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1345 return the maximum value in the range 0..DELTA by which WINDOW
1348 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1349 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1350 return the minimum value in the range DELTA..0 that can be used
1351 for shrinking WINDOW.
1353 Optional argument IGNORE non-nil means ignore restrictions
1354 imposed by fixed size windows, `window-min-height' or
1355 `window-min-width' settings. If IGNORE is a window, ignore
1356 restrictions for that window only. If IGNORE equals `safe',
1357 live windows may get as small as `window-safe-min-height' lines
1358 and `window-safe-min-width' columns. Any other non-nil value
1359 means ignore all of the above restrictions for all windows."
1360 (setq window
(window-normalize-window window
))
1361 (window--resizable window delta horizontal ignore
))
1363 (defun window-total-size (&optional window horizontal
)
1364 "Return the total height or width of WINDOW.
1365 WINDOW must be a valid window and defaults to the selected one.
1367 If HORIZONTAL is omitted or nil, return the total height of
1368 WINDOW, in lines, like `window-total-height'. Otherwise return
1369 the total width, in columns, like `window-total-width'."
1371 (window-total-width window
)
1372 (window-total-height window
)))
1374 ;; Eventually we should make `window-height' obsolete.
1375 (defalias 'window-height
'window-total-height
)
1377 ;; See discussion in bug#4543.
1378 (defun window-full-height-p (&optional window
)
1379 "Return t if WINDOW is as high as its containing frame.
1380 More precisely, return t if and only if the total height of
1381 WINDOW equals the total height of the root window of WINDOW's
1382 frame. WINDOW must be a valid window and defaults to the
1384 (setq window
(window-normalize-window window
))
1385 (= (window-total-size window
)
1386 (window-total-size (frame-root-window window
))))
1388 (defun window-full-width-p (&optional window
)
1389 "Return t if WINDOW is as wide as its containing frame.
1390 More precisely, return t if and only if the total width of WINDOW
1391 equals the total width of the root window of WINDOW's frame.
1392 WINDOW must be a valid window and defaults to the selected one."
1393 (setq window
(window-normalize-window window
))
1394 (= (window-total-size window t
)
1395 (window-total-size (frame-root-window window
) t
)))
1397 (defun window-body-size (&optional window horizontal
)
1398 "Return the height or width of WINDOW's text area.
1399 WINDOW must be a live window and defaults to the selected one.
1401 If HORIZONTAL is omitted or nil, return the height of the text
1402 area, like `window-body-height'. Otherwise, return the width of
1403 the text area, like `window-body-width'."
1405 (window-body-width window
)
1406 (window-body-height window
)))
1408 ;; Eventually we should make `window-height' obsolete.
1409 (defalias 'window-width
'window-body-width
)
1411 (defun window-current-scroll-bars (&optional window
)
1412 "Return the current scroll bar settings for WINDOW.
1413 WINDOW must be a live window and defaults to the selected one.
1415 The return value is a cons cell (VERTICAL . HORIZONTAL) where
1416 VERTICAL specifies the current location of the vertical scroll
1417 bars (`left', `right', or nil), and HORIZONTAL specifies the
1418 current location of the horizontal scroll bars (`top', `bottom',
1421 Unlike `window-scroll-bars', this function reports the scroll bar
1422 type actually used, once frame defaults and `scroll-bar-mode' are
1423 taken into account."
1424 (setq window
(window-normalize-window window t
))
1425 (let ((vert (nth 2 (window-scroll-bars window
)))
1427 (when (or (eq vert t
) (eq hor t
))
1428 (let ((fcsb (frame-current-scroll-bars (window-frame window
))))
1430 (setq vert
(car fcsb
)))
1432 (setq hor
(cdr fcsb
)))))
1435 (defun walk-windows (fun &optional minibuf all-frames
)
1436 "Cycle through all live windows, calling FUN for each one.
1437 FUN must specify a function with a window as its sole argument.
1438 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1439 windows to include in the walk.
1441 MINIBUF t means include the minibuffer window even if the
1442 minibuffer is not active. MINIBUF nil or omitted means include
1443 the minibuffer window only if the minibuffer is active. Any
1444 other value means do not include the minibuffer window even if
1445 the minibuffer is active.
1447 ALL-FRAMES nil or omitted means consider all windows on the
1448 selected frame, plus the minibuffer window if specified by the
1449 MINIBUF argument. If the minibuffer counts, consider all windows
1450 on all frames that share that minibuffer too. The following
1451 non-nil values of ALL-FRAMES have special meanings:
1453 - t means consider all windows on all existing frames.
1455 - `visible' means consider all windows on all visible frames on
1456 the current terminal.
1458 - 0 (the number zero) means consider all windows on all visible
1459 and iconified frames on the current terminal.
1461 - A frame means consider all windows on that frame only.
1463 Anything else means consider all windows on the selected frame
1466 This function changes neither the order of recently selected
1467 windows nor the buffer list."
1468 ;; If we start from the minibuffer window, don't fail to come
1470 (when (window-minibuffer-p (selected-window))
1472 ;; Make sure to not mess up the order of recently selected
1473 ;; windows. Use `save-selected-window' and `select-window'
1474 ;; with second argument non-nil for this purpose.
1475 (save-selected-window
1476 (when (framep all-frames
)
1477 (select-window (frame-first-window all-frames
) 'norecord
))
1478 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames
))
1479 (funcall fun walk-windows-window
))))
1481 (defun window-at-side-p (&optional window side
)
1482 "Return t if WINDOW is at SIDE of its containing frame.
1483 WINDOW must be a valid window and defaults to the selected one.
1484 SIDE can be any of the symbols `left', `top', `right' or
1485 `bottom'. The default value nil is handled like `bottom'."
1486 (setq window
(window-normalize-window window
))
1491 ((eq side
'right
) 2)
1492 ((memq side
'(bottom nil
)) 3))))
1493 (= (nth edge
(window-edges window
))
1494 (nth edge
(window-edges (frame-root-window window
))))))
1496 (defun window-at-side-list (&optional frame side
)
1497 "Return list of all windows on SIDE of FRAME.
1498 FRAME must be a live frame and defaults to the selected frame.
1499 SIDE can be any of the symbols `left', `top', `right' or
1500 `bottom'. The default value nil is handled like `bottom'."
1501 (setq frame
(window-normalize-frame frame
))
1505 (when (window-at-side-p window side
)
1506 (setq windows
(cons window windows
))))
1508 (nreverse windows
)))
1510 (defun window--in-direction-2 (window posn
&optional horizontal
)
1511 "Support function for `window-in-direction'."
1513 (let ((top (window-top-line window
)))
1516 (- posn top
(window-total-height window
))))
1517 (let ((left (window-left-column window
)))
1520 (- posn left
(window-total-width window
))))))
1522 ;; Predecessors to the below have been devised by Julian Assange in
1523 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
1524 ;; Neither of these allow to selectively ignore specific windows
1525 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
1527 (defun window-in-direction (direction &optional window ignore
)
1528 "Return window in DIRECTION as seen from WINDOW.
1529 More precisely, return the nearest window in direction DIRECTION
1530 as seen from the position of `window-point' in window WINDOW.
1531 DIRECTION must be one of `above', `below', `left' or `right'.
1532 WINDOW must be a live window and defaults to the selected one.
1534 Do not return a window whose `no-other-window' parameter is
1535 non-nil. If the nearest window's `no-other-window' parameter is
1536 non-nil, try to find another window in the indicated direction.
1537 If, however, the optional argument IGNORE is non-nil, return that
1538 window even if its `no-other-window' parameter is non-nil.
1540 Return nil if no suitable window can be found."
1541 (setq window
(window-normalize-window window t
))
1542 (unless (memq direction
'(above below left right
))
1543 (error "Wrong direction %s" direction
))
1544 (let* ((frame (window-frame window
))
1545 (hor (memq direction
'(left right
)))
1547 (window-left-column window
)
1548 (window-top-line window
)))
1549 (last (+ first
(if hor
1550 (window-total-width window
)
1551 (window-total-height window
))))
1552 (posn-cons (nth 6 (posn-at-point (window-point window
) window
)))
1553 ;; The column / row value of `posn-at-point' can be nil for the
1554 ;; mini-window, guard against that.
1556 (+ (or (cdr posn-cons
) 1) (window-top-line window
))
1557 (+ (or (car posn-cons
) 1) (window-left-column window
))))
1560 ((eq direction
'below
) (frame-height frame
))
1561 ((eq direction
'right
) (frame-width frame
))
1563 (best-edge-2 best-edge
)
1564 (best-diff-2 (if hor
(frame-height frame
) (frame-width frame
)))
1565 best best-2 best-diff-2-new
)
1568 (let* ((w-top (window-top-line w
))
1569 (w-left (window-left-column w
)))
1572 ;; Ignore ourselves.
1573 (and (window-parameter w
'no-other-window
)
1574 ;; Ignore W unless IGNORE is non-nil.
1578 ((and (<= w-top posn
)
1579 (< posn
(+ w-top
(window-total-height w
))))
1580 ;; W is to the left or right of WINDOW and covers POSN.
1581 (when (or (and (eq direction
'left
)
1582 (<= w-left first
) (> w-left best-edge
))
1583 (and (eq direction
'right
)
1584 (>= w-left last
) (< w-left best-edge
)))
1585 (setq best-edge w-left
)
1587 ((and (or (and (eq direction
'left
)
1588 (<= (+ w-left
(window-total-width w
)) first
))
1589 (and (eq direction
'right
) (<= last w-left
)))
1590 ;; W is to the left or right of WINDOW but does not
1592 (setq best-diff-2-new
1593 (window--in-direction-2 w posn hor
))
1594 (or (< best-diff-2-new best-diff-2
)
1595 (and (= best-diff-2-new best-diff-2
)
1596 (if (eq direction
'left
)
1597 (> w-left best-edge-2
)
1598 (< w-left best-edge-2
)))))
1599 (setq best-edge-2 w-left
)
1600 (setq best-diff-2 best-diff-2-new
)
1604 ((and (<= w-left posn
)
1605 (< posn
(+ w-left
(window-total-width w
))))
1606 ;; W is above or below WINDOW and covers POSN.
1607 (when (or (and (eq direction
'above
)
1608 (<= w-top first
) (> w-top best-edge
))
1609 (and (eq direction
'below
)
1610 (>= w-top first
) (< w-top best-edge
)))
1611 (setq best-edge w-top
)
1613 ((and (or (and (eq direction
'above
)
1614 (<= (+ w-top
(window-total-height w
)) first
))
1615 (and (eq direction
'below
) (<= last w-top
)))
1616 ;; W is above or below WINDOW but does not cover POSN.
1617 (setq best-diff-2-new
1618 (window--in-direction-2 w posn hor
))
1619 (or (< best-diff-2-new best-diff-2
)
1620 (and (= best-diff-2-new best-diff-2
)
1621 (if (eq direction
'above
)
1622 (> w-top best-edge-2
)
1623 (< w-top best-edge-2
)))))
1624 (setq best-edge-2 w-top
)
1625 (setq best-diff-2 best-diff-2-new
)
1626 (setq best-2 w
)))))))
1630 (defun get-window-with-predicate (predicate &optional minibuf all-frames default
)
1631 "Return a live window satisfying PREDICATE.
1632 More precisely, cycle through all windows calling the function
1633 PREDICATE on each one of them with the window as its sole
1634 argument. Return the first window for which PREDICATE returns
1635 non-nil. Windows are scanned starting with the window following
1636 the selected window. If no window satisfies PREDICATE, return
1639 MINIBUF t means include the minibuffer window even if the
1640 minibuffer is not active. MINIBUF nil or omitted means include
1641 the minibuffer window only if the minibuffer is active. Any
1642 other value means do not include the minibuffer window even if
1643 the minibuffer is active.
1645 ALL-FRAMES nil or omitted means consider all windows on the selected
1646 frame, plus the minibuffer window if specified by the MINIBUF
1647 argument. If the minibuffer counts, consider all windows on all
1648 frames that share that minibuffer too. The following non-nil
1649 values of ALL-FRAMES have special meanings:
1651 - t means consider all windows on all existing frames.
1653 - `visible' means consider all windows on all visible frames on
1654 the current terminal.
1656 - 0 (the number zero) means consider all windows on all visible
1657 and iconified frames on the current terminal.
1659 - A frame means consider all windows on that frame only.
1661 Anything else means consider all windows on the selected frame
1664 (dolist (window (window-list-1
1665 (next-window nil minibuf all-frames
)
1666 minibuf all-frames
))
1667 (when (funcall predicate window
)
1668 (throw 'found window
)))
1671 (defalias 'some-window
'get-window-with-predicate
)
1673 (defun get-lru-window (&optional all-frames dedicated not-selected
)
1674 "Return the least recently used window on frames specified by ALL-FRAMES.
1675 Return a full-width window if possible. A minibuffer window is
1676 never a candidate. A dedicated window is never a candidate
1677 unless DEDICATED is non-nil, so if all windows are dedicated, the
1678 value is nil. Avoid returning the selected window if possible.
1679 Optional argument NOT-SELECTED non-nil means never return the
1682 The following non-nil values of the optional argument ALL-FRAMES
1683 have special meanings:
1685 - t means consider all windows on all existing frames.
1687 - `visible' means consider all windows on all visible frames on
1688 the current terminal.
1690 - 0 (the number zero) means consider all windows on all visible
1691 and iconified frames on the current terminal.
1693 - A frame means consider all windows on that frame only.
1695 Any other value of ALL-FRAMES means consider all windows on the
1696 selected frame and no others."
1697 (let (best-window best-time second-best-window second-best-time time
)
1698 (dolist (window (window-list-1 nil
'nomini all-frames
))
1699 (when (and (or dedicated
(not (window-dedicated-p window
)))
1700 (or (not not-selected
) (not (eq window
(selected-window)))))
1701 (setq time
(window-use-time window
))
1702 (if (or (eq window
(selected-window))
1703 (not (window-full-width-p window
)))
1704 (when (or (not second-best-time
) (< time second-best-time
))
1705 (setq second-best-time time
)
1706 (setq second-best-window window
))
1707 (when (or (not best-time
) (< time best-time
))
1708 (setq best-time time
)
1709 (setq best-window window
)))))
1710 (or best-window second-best-window
)))
1712 (defun get-mru-window (&optional all-frames dedicated not-selected
)
1713 "Return the most recently used window on frames specified by ALL-FRAMES.
1714 A minibuffer window is never a candidate. A dedicated window is
1715 never a candidate unless DEDICATED is non-nil, so if all windows
1716 are dedicated, the value is nil. Optional argument NOT-SELECTED
1717 non-nil means never return the selected window.
1719 The following non-nil values of the optional argument ALL-FRAMES
1720 have special meanings:
1722 - t means consider all windows on all existing frames.
1724 - `visible' means consider all windows on all visible frames on
1725 the current terminal.
1727 - 0 (the number zero) means consider all windows on all visible
1728 and iconified frames on the current terminal.
1730 - A frame means consider all windows on that frame only.
1732 Any other value of ALL-FRAMES means consider all windows on the
1733 selected frame and no others."
1734 (let (best-window best-time time
)
1735 (dolist (window (window-list-1 nil
'nomini all-frames
))
1736 (setq time
(window-use-time window
))
1737 (when (and (or dedicated
(not (window-dedicated-p window
)))
1738 (or (not not-selected
) (not (eq window
(selected-window))))
1739 (or (not best-time
) (> time best-time
)))
1740 (setq best-time time
)
1741 (setq best-window window
)))
1744 (defun get-largest-window (&optional all-frames dedicated not-selected
)
1745 "Return the largest window on frames specified by ALL-FRAMES.
1746 A minibuffer window is never a candidate. A dedicated window is
1747 never a candidate unless DEDICATED is non-nil, so if all windows
1748 are dedicated, the value is nil. Optional argument NOT-SELECTED
1749 non-nil means never return the selected window.
1751 The following non-nil values of the optional argument ALL-FRAMES
1752 have special meanings:
1754 - t means consider all windows on all existing frames.
1756 - `visible' means consider all windows on all visible frames on
1757 the current terminal.
1759 - 0 (the number zero) means consider all windows on all visible
1760 and iconified frames on the current terminal.
1762 - A frame means consider all windows on that frame only.
1764 Any other value of ALL-FRAMES means consider all windows on the
1765 selected frame and no others."
1768 (dolist (window (window-list-1 nil
'nomini all-frames
))
1769 (when (and (or dedicated
(not (window-dedicated-p window
)))
1770 (or (not not-selected
) (not (eq window
(selected-window)))))
1771 (setq size
(* (window-total-size window
)
1772 (window-total-size window t
)))
1773 (when (> size best-size
)
1774 (setq best-size size
)
1775 (setq best-window window
))))
1778 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames
)
1779 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
1780 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
1781 and defaults to the current buffer. Windows are scanned starting
1782 with the selected window.
1784 MINIBUF t means include the minibuffer window even if the
1785 minibuffer is not active. MINIBUF nil or omitted means include
1786 the minibuffer window only if the minibuffer is active. Any
1787 other value means do not include the minibuffer window even if
1788 the minibuffer is active.
1790 ALL-FRAMES nil or omitted means consider all windows on the
1791 selected frame, plus the minibuffer window if specified by the
1792 MINIBUF argument. If the minibuffer counts, consider all windows
1793 on all frames that share that minibuffer too. The following
1794 non-nil values of ALL-FRAMES have special meanings:
1796 - t means consider all windows on all existing frames.
1798 - `visible' means consider all windows on all visible frames on
1799 the current terminal.
1801 - 0 (the number zero) means consider all windows on all visible
1802 and iconified frames on the current terminal.
1804 - A frame means consider all windows on that frame only.
1806 Anything else means consider all windows on the selected frame
1808 (let ((buffer (window-normalize-buffer buffer-or-name
))
1810 (dolist (window (window-list-1 (selected-window) minibuf all-frames
))
1811 (when (eq (window-buffer window
) buffer
)
1812 (setq windows
(cons window windows
))))
1813 (nreverse windows
)))
1815 (defun minibuffer-window-active-p (window)
1816 "Return t if WINDOW is the currently active minibuffer window."
1817 (eq window
(active-minibuffer-window)))
1819 (defun count-windows (&optional minibuf
)
1820 "Return the number of live windows on the selected frame.
1821 The optional argument MINIBUF specifies whether the minibuffer
1822 window shall be counted. See `walk-windows' for the precise
1823 meaning of this argument."
1824 (length (window-list-1 nil minibuf
)))
1826 ;;; Resizing windows.
1827 (defun window--resize-reset (&optional frame horizontal
)
1828 "Reset resize values for all windows on FRAME.
1829 FRAME defaults to the selected frame.
1831 This function stores the current value of `window-total-size' applied
1832 with argument HORIZONTAL in the new total size of all windows on
1833 FRAME. It also resets the new normal size of each of these
1835 (window--resize-reset-1
1836 (frame-root-window (window-normalize-frame frame
)) horizontal
))
1838 (defun window--resize-reset-1 (window horizontal
)
1839 "Internal function of `window--resize-reset'."
1840 ;; Register old size in the new total size.
1841 (set-window-new-total window
(window-total-size window horizontal
))
1842 ;; Reset new normal size.
1843 (set-window-new-normal window
)
1844 (when (window-child window
)
1845 (window--resize-reset-1 (window-child window
) horizontal
))
1846 (when (window-right window
)
1847 (window--resize-reset-1 (window-right window
) horizontal
)))
1849 ;; The following routine is used to manually resize the minibuffer
1850 ;; window and is currently used, for example, by ispell.el.
1851 (defun window--resize-mini-window (window delta
)
1852 "Resize minibuffer window WINDOW by DELTA lines.
1853 If WINDOW cannot be resized by DELTA lines make it as large (or
1854 as small) as possible, but don't signal an error."
1855 (when (window-minibuffer-p window
)
1856 (let* ((frame (window-frame window
))
1857 (root (frame-root-window frame
))
1858 (height (window-total-size window
))
1860 (- (window-total-size root
)
1861 (window-min-size root
))))
1864 ((<= (+ height delta
) 0)
1865 (setq delta
(- (- height
1))))
1866 ((> delta min-delta
)
1867 (setq delta min-delta
)))
1870 (window--resize-reset frame
)
1871 ;; Ideally we should be able to resize just the last child of root
1872 ;; here. See the comment in `resize-root-window-vertically' for
1873 ;; why we do not do that.
1874 (window--resize-this-window root
(- delta
) nil nil t
)
1875 (set-window-new-total window
(+ height delta
))
1876 ;; The following routine catches the case where we want to resize
1877 ;; a minibuffer-only frame.
1878 (resize-mini-window-internal window
))))
1880 (defun window-resize (window delta
&optional horizontal ignore
)
1881 "Resize WINDOW vertically by DELTA lines.
1882 WINDOW can be an arbitrary window and defaults to the selected
1883 one. An attempt to resize the root window of a frame will raise
1886 DELTA a positive number means WINDOW shall be enlarged by DELTA
1887 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
1890 Optional argument HORIZONTAL non-nil means resize WINDOW
1891 horizontally by DELTA columns. In this case a positive DELTA
1892 means enlarge WINDOW by DELTA columns. DELTA negative means
1893 WINDOW shall be shrunk by -DELTA columns.
1895 Optional argument IGNORE non-nil means ignore restrictions
1896 imposed by fixed size windows, `window-min-height' or
1897 `window-min-width' settings. If IGNORE is a window, ignore
1898 restrictions for that window only. If IGNORE equals `safe',
1899 live windows may get as small as `window-safe-min-height' lines
1900 and `window-safe-min-width' columns. Any other non-nil value
1901 means ignore all of the above restrictions for all windows.
1903 This function resizes other windows proportionally and never
1904 deletes any windows. If you want to move only the low (right)
1905 edge of WINDOW consider using `adjust-window-trailing-edge'
1907 (setq window
(window-normalize-window window
))
1908 (let* ((frame (window-frame window
))
1909 (minibuffer-window (minibuffer-window frame
))
1912 ((eq window
(frame-root-window frame
))
1913 (error "Cannot resize the root window of a frame"))
1914 ((window-minibuffer-p window
)
1916 (error "Cannot resize minibuffer window horizontally")
1917 (window--resize-mini-window window delta
)))
1918 ((and (not horizontal
)
1919 (window-full-height-p window
)
1920 (eq (window-frame minibuffer-window
) frame
)
1921 (or (not resize-mini-windows
)
1922 (eq minibuffer-window
(active-minibuffer-window))))
1923 ;; If WINDOW is full height and either `resize-mini-windows' is
1924 ;; nil or the minibuffer window is active, resize the minibuffer
1926 (window--resize-mini-window minibuffer-window
(- delta
)))
1927 ((window--resizable-p window delta horizontal ignore
)
1928 (window--resize-reset frame horizontal
)
1929 (window--resize-this-window window delta horizontal ignore t
)
1930 (if (and (not window-combination-resize
)
1931 (window-combined-p window horizontal
)
1932 (setq sibling
(or (window-right window
) (window-left window
)))
1933 (window-sizable-p sibling
(- delta
) horizontal ignore
))
1934 ;; If window-combination-resize is nil, WINDOW is part of an
1935 ;; iso-combination, and WINDOW's neighboring right or left
1936 ;; sibling can be resized as requested, resize that sibling.
1939 (window-total-size (window-parent window
) horizontal
))))
1940 (window--resize-this-window sibling
(- delta
) horizontal nil t
)
1941 (set-window-new-normal
1942 window
(+ (window-normal-size window horizontal
)
1944 (set-window-new-normal
1945 sibling
(- (window-normal-size sibling horizontal
)
1947 ;; Otherwise, resize all other windows in the same combination.
1948 (window--resize-siblings window delta horizontal ignore
))
1949 (window-resize-apply frame horizontal
))
1951 (error "Cannot resize window %s" window
)))))
1953 (defun window--resize-child-windows-skip-p (window)
1954 "Return non-nil if WINDOW shall be skipped by resizing routines."
1955 (memq (window-new-normal window
) '(ignore stuck skip
)))
1957 (defun window--resize-child-windows-normal (parent horizontal window this-delta
&optional trail other-delta
)
1958 "Recursively set new normal height of child windows of window PARENT.
1959 HORIZONTAL non-nil means set the new normal width of these
1960 windows. WINDOW specifies a child window of PARENT that has been
1961 resized by THIS-DELTA lines (columns).
1963 Optional argument TRAIL either `before' or `after' means set values
1964 only for windows before or after WINDOW. Optional argument
1965 OTHER-DELTA, a number, specifies that this many lines (columns)
1966 have been obtained from (or returned to) an ancestor window of
1967 PARENT in order to resize WINDOW."
1968 (let* ((delta-normal
1969 (if (and (= (- this-delta
) (window-total-size window horizontal
))
1970 (zerop other-delta
))
1971 ;; When WINDOW gets deleted and we can return its entire
1972 ;; space to its siblings, use WINDOW's normal size as the
1974 (- (window-normal-size window horizontal
))
1975 ;; In any other case calculate the normal delta from the
1976 ;; relation of THIS-DELTA to the total size of PARENT.
1977 (/ (float this-delta
) (window-total-size parent horizontal
))))
1978 (sub (window-child parent
))
1980 (skip (eq trail
'after
)))
1982 ;; Set parent-normal to the sum of the normal sizes of all child
1983 ;; windows of PARENT that shall be resized, excluding only WINDOW
1984 ;; and any windows specified by the optional TRAIL argument.
1988 (setq skip
(eq trail
'before
)))
1992 (+ parent-normal
(window-normal-size sub horizontal
)))))
1993 (setq sub
(window-right sub
)))
1995 ;; Set the new normal size of all child windows of PARENT from what
1996 ;; they should have contributed for recovering THIS-DELTA lines
1998 (setq sub
(window-child parent
))
1999 (setq skip
(eq trail
'after
))
2003 (setq skip
(eq trail
'before
)))
2006 (let ((old-normal (window-normal-size sub horizontal
)))
2007 (set-window-new-normal
2008 sub
(min 1.0 ; Don't get larger than 1.
2010 (* (/ old-normal parent-normal
)
2012 ;; Don't drop below 0.
2014 (setq sub
(window-right sub
)))
2016 (when (numberp other-delta
)
2017 ;; Set the new normal size of windows from what they should have
2018 ;; contributed for recovering OTHER-DELTA lines (columns).
2019 (setq delta-normal
(/ (float (window-total-size parent horizontal
))
2020 (+ (window-total-size parent horizontal
)
2022 (setq sub
(window-child parent
))
2023 (setq skip
(eq trail
'after
))
2027 (setq skip
(eq trail
'before
)))
2030 (set-window-new-normal
2031 sub
(min 1.0 ; Don't get larger than 1.
2032 (max (* (window-new-normal sub
) delta-normal
)
2033 ;; Don't drop below 0.
2035 (setq sub
(window-right sub
))))
2037 ;; Set the new normal size of WINDOW to what is left by the sum of
2038 ;; the normal sizes of its siblings.
2039 (set-window-new-normal
2042 (setq sub
(window-child parent
))
2046 ((not (numberp (window-new-normal sub
)))
2047 (setq sum
(+ sum
(window-normal-size sub horizontal
))))
2049 (setq sum
(+ sum
(window-new-normal sub
)))))
2050 (setq sub
(window-right sub
)))
2051 ;; Don't get larger than 1 or smaller than 0.
2052 (min 1.0 (max (- 1.0 sum
) 0.0))))))
2054 (defun window--resize-child-windows (parent delta
&optional horizontal window ignore trail edge
)
2055 "Resize child windows of window PARENT vertically by DELTA lines.
2056 PARENT must be a vertically combined internal window.
2058 Optional argument HORIZONTAL non-nil means resize child windows of
2059 PARENT horizontally by DELTA columns. In this case PARENT must
2060 be a horizontally combined internal window.
2062 WINDOW, if specified, must denote a child window of PARENT that
2063 is resized by DELTA lines.
2065 Optional argument IGNORE non-nil means ignore restrictions
2066 imposed by fixed size windows, `window-min-height' or
2067 `window-min-width' settings. If IGNORE equals `safe', live
2068 windows may get as small as `window-safe-min-height' lines and
2069 `window-safe-min-width' columns. If IGNORE is a window, ignore
2070 restrictions for that window only. Any other non-nil value means
2071 ignore all of the above restrictions for all windows.
2073 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2074 of windows that shall be resized. If TRAIL equals `before',
2075 resize only windows on the left or above EDGE. If TRAIL equals
2076 `after', resize only windows on the right or below EDGE. Also,
2077 preferably only resize windows adjacent to EDGE.
2079 Return the symbol `normalized' if new normal sizes have been
2080 already set by this routine."
2081 (let* ((first (window-child parent
))
2083 (parent-total (+ (window-total-size parent horizontal
) delta
))
2084 best-window best-value
)
2086 (if (and edge
(memq trail
'(before after
))
2089 (while (and (window-right sub
)
2090 (or (and (eq trail
'before
)
2091 (not (window--resize-child-windows-skip-p
2092 (window-right sub
))))
2093 (and (eq trail
'after
)
2094 (window--resize-child-windows-skip-p sub
))))
2095 (setq sub
(window-right sub
)))
2098 (if (eq trail
'before
)
2099 (= (+ (window-left-column sub
)
2100 (window-total-size sub t
))
2102 (= (window-left-column sub
) edge
))
2103 (if (eq trail
'before
)
2104 (= (+ (window-top-line sub
)
2105 (window-total-size sub
))
2107 (= (window-top-line sub
) edge
)))
2108 (window-sizable-p sub delta horizontal ignore
))
2109 ;; Resize only windows adjacent to EDGE.
2111 (window--resize-this-window
2112 sub delta horizontal ignore t trail edge
)
2113 (if (and window
(eq (window-parent sub
) parent
))
2115 ;; Assign new normal sizes.
2116 (set-window-new-normal
2117 sub
(/ (float (window-new-total sub
)) parent-total
))
2118 (set-window-new-normal
2119 window
(- (window-normal-size window horizontal
)
2120 (- (window-new-normal sub
)
2121 (window-normal-size sub horizontal
)))))
2122 (window--resize-child-windows-normal
2123 parent horizontal sub
0 trail delta
))
2124 ;; Return 'normalized to notify `window--resize-siblings' that
2125 ;; normal sizes have been already set.
2127 ;; Resize all windows proportionally.
2131 ((or (window--resize-child-windows-skip-p sub
)
2132 ;; Ignore windows to skip and fixed-size child windows -
2133 ;; in the latter case make it a window to skip.
2135 (window-size-fixed-p sub horizontal
)
2136 (set-window-new-normal sub
'ignore
))))
2138 ;; When shrinking store the number of lines/cols we can get
2139 ;; from this window here together with the total/normal size
2141 (set-window-new-normal
2144 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
2145 (window-min-delta sub horizontal ignore trail t
) ; t)
2146 (- (/ (float (window-total-size sub horizontal
))
2148 (window-normal-size sub horizontal
)))))
2150 ;; When enlarging store the total/normal size factor only
2151 (set-window-new-normal
2153 (- (/ (float (window-total-size sub horizontal
))
2155 (window-normal-size sub horizontal
)))))
2157 (setq sub
(window-right sub
)))
2161 ;; Shrink windows by delta.
2162 (setq best-window t
)
2163 (while (and best-window
(not (zerop delta
)))
2165 (setq best-window nil
)
2166 (setq best-value most-negative-fixnum
)
2168 (when (and (consp (window-new-normal sub
))
2169 (not (zerop (car (window-new-normal sub
))))
2170 (> (cdr (window-new-normal sub
)) best-value
))
2171 (setq best-window sub
)
2172 (setq best-value
(cdr (window-new-normal sub
))))
2174 (setq sub
(window-right sub
)))
2177 (setq delta
(1+ delta
)))
2178 (set-window-new-total best-window -
1 t
)
2179 (set-window-new-normal
2181 (if (= (car (window-new-normal best-window
)) 1)
2182 'skip
; We can't shrink best-window any further.
2183 (cons (1- (car (window-new-normal best-window
)))
2184 (- (/ (float (window-new-total best-window
))
2186 (window-normal-size best-window horizontal
)))))))
2188 ;; Enlarge windows by delta.
2189 (setq best-window t
)
2190 (while (and best-window
(not (zerop delta
)))
2192 (setq best-window nil
)
2193 (setq best-value most-positive-fixnum
)
2195 (when (and (numberp (window-new-normal sub
))
2196 (< (window-new-normal sub
) best-value
))
2197 (setq best-window sub
)
2198 (setq best-value
(window-new-normal sub
)))
2200 (setq sub
(window-right sub
)))
2203 (setq delta
(1- delta
)))
2204 (set-window-new-total best-window
1 t
)
2205 (set-window-new-normal
2207 (- (/ (float (window-new-total best-window
))
2209 (window-normal-size best-window horizontal
))))))
2214 (when (or (consp (window-new-normal sub
))
2215 (numberp (window-new-normal sub
)))
2216 ;; Reset new normal size fields so `window-resize-apply'
2217 ;; won't use them to apply new sizes.
2218 (set-window-new-normal sub
))
2220 (unless (eq (window-new-normal sub
) 'ignore
)
2221 ;; Resize this window's child windows (back-engineering
2222 ;; delta from sub's old and new total sizes).
2223 (let ((delta (- (window-new-total sub
)
2224 (window-total-size sub horizontal
))))
2225 (unless (and (zerop delta
) (not trail
))
2226 ;; For the TRAIL non-nil case we have to resize SUB
2227 ;; recursively even if it's size does not change.
2228 (window--resize-this-window
2229 sub delta horizontal ignore nil trail edge
))))
2230 (setq sub
(window-right sub
)))))))
2232 (defun window--resize-siblings (window delta
&optional horizontal ignore trail edge
)
2233 "Resize other windows when WINDOW is resized vertically by DELTA lines.
2234 Optional argument HORIZONTAL non-nil means resize other windows
2235 when WINDOW is resized horizontally by DELTA columns. WINDOW
2236 itself is not resized by this function.
2238 Optional argument IGNORE non-nil means ignore restrictions
2239 imposed by fixed size windows, `window-min-height' or
2240 `window-min-width' settings. If IGNORE equals `safe', live
2241 windows may get as small as `window-safe-min-height' lines and
2242 `window-safe-min-width' columns. If IGNORE is a window, ignore
2243 restrictions for that window only. Any other non-nil value means
2244 ignore all of the above restrictions for all windows.
2246 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2247 of windows that shall be resized. If TRAIL equals `before',
2248 resize only windows on the left or above EDGE. If TRAIL equals
2249 `after', resize only windows on the right or below EDGE. Also,
2250 preferably only resize windows adjacent to EDGE."
2251 (when (window-parent window
)
2252 (let* ((parent (window-parent window
))
2253 (sub (window-child parent
)))
2254 (if (window-combined-p sub horizontal
)
2255 ;; In an iso-combination try to extract DELTA from WINDOW's
2257 (let ((skip (eq trail
'after
))
2258 this-delta other-delta
)
2259 ;; Decide which windows shall be left alone.
2263 ;; Make sure WINDOW is left alone when
2264 ;; resizing its siblings.
2265 (set-window-new-normal sub
'ignore
)
2266 (setq skip
(eq trail
'before
)))
2268 ;; Make sure this sibling is left alone when
2269 ;; resizing its siblings.
2270 (set-window-new-normal sub
'ignore
))
2271 ((or (window--size-ignore-p sub ignore
)
2272 (not (window-size-fixed-p sub horizontal
)))
2273 ;; Set this-delta to t to signal that we found a sibling
2274 ;; of WINDOW whose size is not fixed.
2275 (setq this-delta t
)))
2277 (setq sub
(window-right sub
)))
2279 ;; Set this-delta to what we can get from WINDOW's siblings.
2280 (if (= (- delta
) (window-total-size window horizontal
))
2281 ;; A deletion, presumably. We must handle this case
2282 ;; specially since `window--resizable' can't be used.
2284 ;; There's at least one resizable sibling we can
2285 ;; give WINDOW's size to.
2286 (setq this-delta delta
)
2287 ;; No resizable sibling exists.
2288 (setq this-delta
0))
2289 ;; Any other form of resizing.
2291 (window--resizable window delta horizontal ignore trail t
)))
2293 ;; Set other-delta to what we still have to get from
2294 ;; ancestor windows of parent.
2295 (setq other-delta
(- delta this-delta
))
2296 (unless (zerop other-delta
)
2297 ;; Unless we got everything from WINDOW's siblings, PARENT
2298 ;; must be resized by other-delta lines or columns.
2299 (set-window-new-total parent other-delta
'add
))
2301 (if (zerop this-delta
)
2302 ;; We haven't got anything from WINDOW's siblings but we
2303 ;; must update the normal sizes to respect other-delta.
2304 (window--resize-child-windows-normal
2305 parent horizontal window this-delta trail other-delta
)
2306 ;; We did get something from WINDOW's siblings which means
2307 ;; we have to resize their child windows.
2308 (unless (eq (window--resize-child-windows
2309 parent
(- this-delta
) horizontal
2310 window ignore trail edge
)
2311 ;; If `window--resize-child-windows' returns
2312 ;; 'normalized, this means it has set the
2313 ;; normal sizes already.
2315 ;; Set the normal sizes.
2316 (window--resize-child-windows-normal
2317 parent horizontal window this-delta trail other-delta
))
2318 ;; Set DELTA to what we still have to get from ancestor
2320 (setq delta other-delta
)))
2322 ;; In an ortho-combination all siblings of WINDOW must be
2323 ;; resized by DELTA.
2324 (set-window-new-total parent delta
'add
)
2326 (unless (eq sub window
)
2327 (window--resize-this-window sub delta horizontal ignore t
))
2328 (setq sub
(window-right sub
))))
2330 (unless (zerop delta
)
2332 (window--resize-siblings
2333 parent delta horizontal ignore trail edge
)))))
2335 (defun window--resize-this-window (window delta
&optional horizontal ignore add trail edge
)
2336 "Resize WINDOW vertically by DELTA lines.
2337 Optional argument HORIZONTAL non-nil means resize WINDOW
2338 horizontally by DELTA columns.
2340 Optional argument IGNORE non-nil means ignore restrictions
2341 imposed by fixed size windows, `window-min-height' or
2342 `window-min-width' settings. If IGNORE equals `safe', live
2343 windows may get as small as `window-safe-min-height' lines and
2344 `window-safe-min-width' columns. If IGNORE is a window, ignore
2345 restrictions for that window only. Any other non-nil value
2346 means ignore all of the above restrictions for all windows.
2348 Optional argument ADD non-nil means add DELTA to the new total
2351 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2352 of windows that shall be resized. If TRAIL equals `before',
2353 resize only windows on the left or above EDGE. If TRAIL equals
2354 `after', resize only windows on the right or below EDGE. Also,
2355 preferably only resize windows adjacent to EDGE.
2357 This function recursively resizes WINDOW's child windows to fit the
2358 new size. Make sure that WINDOW is `window--resizable' before
2359 calling this function. Note that this function does not resize
2360 siblings of WINDOW or WINDOW's parent window. You have to
2361 eventually call `window-resize-apply' in order to make resizing
2362 actually take effect."
2364 ;; Add DELTA to the new total size of WINDOW.
2365 (set-window-new-total window delta t
))
2367 (let ((sub (window-child window
)))
2370 ((window-combined-p sub horizontal
)
2371 ;; In an iso-combination resize child windows according to their
2373 (window--resize-child-windows
2374 window delta horizontal nil ignore trail edge
))
2375 ;; In an ortho-combination resize each child window by DELTA.
2378 (window--resize-this-window
2379 sub delta horizontal ignore t trail edge
)
2380 (setq sub
(window-right sub
)))))))
2382 (defun window--resize-root-window (window delta horizontal ignore
)
2383 "Resize root window WINDOW vertically by DELTA lines.
2384 HORIZONTAL non-nil means resize root window WINDOW horizontally
2387 IGNORE non-nil means ignore any restrictions imposed by fixed
2388 size windows, `window-min-height' or `window-min-width' settings.
2390 This function is only called by the frame resizing routines. It
2391 resizes windows proportionally and never deletes any windows."
2392 (when (and (windowp window
) (numberp delta
)
2393 (window-sizable-p window delta horizontal ignore
))
2394 (window--resize-reset (window-frame window
) horizontal
)
2395 (window--resize-this-window window delta horizontal ignore t
)))
2397 (defun window--resize-root-window-vertically (window delta
)
2398 "Resize root window WINDOW vertically by DELTA lines.
2399 If DELTA is less than zero and we can't shrink WINDOW by DELTA
2400 lines, shrink it as much as possible. If DELTA is greater than
2401 zero, this function can resize fixed-size windows in order to
2402 recover the necessary lines.
2404 Return the number of lines that were recovered.
2406 This function is only called by the minibuffer window resizing
2407 routines. It resizes windows proportionally and never deletes
2409 (when (numberp delta
)
2413 (setq delta
(window-sizable window delta
)))
2415 (unless (window-sizable window delta
)
2418 (window--resize-reset (window-frame window
))
2419 ;; Ideally, we would resize just the last window in a combination
2420 ;; but that's not feasible for the following reason: If we grow
2421 ;; the minibuffer window and the last window cannot be shrunk any
2422 ;; more, we shrink another window instead. But if we then shrink
2423 ;; the minibuffer window again, the last window might get enlarged
2424 ;; and the state after shrinking is not the state before growing.
2425 ;; So, in practice, we'd need a history variable to record how to
2426 ;; proceed. But I'm not sure how such a variable could work with
2427 ;; repeated minibuffer window growing steps.
2428 (window--resize-this-window window delta nil ignore t
)
2431 (defun adjust-window-trailing-edge (window delta
&optional horizontal
)
2432 "Move WINDOW's bottom edge by DELTA lines.
2433 Optional argument HORIZONTAL non-nil means move WINDOW's right
2434 edge by DELTA columns. WINDOW must be a valid window and
2435 defaults to the selected one.
2437 If DELTA is greater than zero, move the edge downwards or to the
2438 right. If DELTA is less than zero, move the edge upwards or to
2439 the left. If the edge can't be moved by DELTA lines or columns,
2440 move it as far as possible in the desired direction."
2441 (setq window
(window-normalize-window window
))
2442 (let* ((frame (window-frame window
))
2443 (minibuffer-window (minibuffer-window frame
))
2445 left this-delta min-delta max-delta
)
2446 ;; Find the edge we want to move.
2447 (while (and (or (not (window-combined-p right horizontal
))
2448 (not (window-right right
)))
2449 (setq right
(window-parent right
))))
2451 ((and (not right
) (not horizontal
)
2452 ;; Resize the minibuffer window if it's on the same frame as
2453 ;; and immediately below WINDOW and it's either active or
2454 ;; `resize-mini-windows' is nil.
2455 (eq (window-frame minibuffer-window
) frame
)
2456 (= (nth 1 (window-edges minibuffer-window
))
2457 (nth 3 (window-edges window
)))
2458 (or (not resize-mini-windows
)
2459 (eq minibuffer-window
(active-minibuffer-window))))
2460 (window--resize-mini-window minibuffer-window
(- delta
)))
2461 ((or (not (setq left right
)) (not (setq right
(window-right right
))))
2463 (error "No window on the right of this one")
2464 (error "No window below this one")))
2466 ;; Set LEFT to the first resizable window on the left. This step is
2467 ;; needed to handle fixed-size windows.
2468 (while (and left
(window-size-fixed-p left horizontal
))
2470 (or (window-left left
)
2472 (while (and (setq left
(window-parent left
))
2473 (not (window-combined-p left horizontal
))))
2474 (window-left left
)))))
2477 (error "No resizable window on the left of this one")
2478 (error "No resizable window above this one")))
2480 ;; Set RIGHT to the first resizable window on the right. This step
2481 ;; is needed to handle fixed-size windows.
2482 (while (and right
(window-size-fixed-p right horizontal
))
2484 (or (window-right right
)
2486 (while (and (setq right
(window-parent right
))
2487 (not (window-combined-p right horizontal
))))
2488 (window-right right
)))))
2491 (error "No resizable window on the right of this one")
2492 (error "No resizable window below this one")))
2494 ;; LEFT and RIGHT (which might be both internal windows) are now the
2495 ;; two windows we want to resize.
2498 (setq max-delta
(window--max-delta-1 left
0 horizontal nil
'after
))
2499 (setq min-delta
(window--min-delta-1 right
(- delta
) horizontal nil
'before
))
2500 (when (or (< max-delta delta
) (> min-delta
(- delta
)))
2501 ;; We can't get the whole DELTA - move as far as possible.
2502 (setq delta
(min max-delta
(- min-delta
))))
2503 (unless (zerop delta
)
2505 (window--resize-reset frame horizontal
)
2506 ;; Try to enlarge LEFT first.
2507 (setq this-delta
(window--resizable left delta horizontal
))
2508 (unless (zerop this-delta
)
2509 (window--resize-this-window
2510 left this-delta horizontal nil t
'before
2512 (+ (window-left-column left
) (window-total-size left t
))
2513 (+ (window-top-line left
) (window-total-size left
)))))
2514 ;; Shrink windows on right of LEFT.
2515 (window--resize-siblings
2516 left delta horizontal nil
'after
2518 (window-left-column right
)
2519 (window-top-line right
)))))
2521 (setq max-delta
(window--max-delta-1 right
0 horizontal nil
'before
))
2522 (setq min-delta
(window--min-delta-1 left delta horizontal nil
'after
))
2523 (when (or (< max-delta
(- delta
)) (> min-delta delta
))
2524 ;; We can't get the whole DELTA - move as far as possible.
2525 (setq delta
(max (- max-delta
) min-delta
)))
2526 (unless (zerop delta
)
2528 (window--resize-reset frame horizontal
)
2529 ;; Try to enlarge RIGHT.
2530 (setq this-delta
(window--resizable right
(- delta
) horizontal
))
2531 (unless (zerop this-delta
)
2532 (window--resize-this-window
2533 right this-delta horizontal nil t
'after
2535 (window-left-column right
)
2536 (window-top-line right
))))
2537 ;; Shrink windows on left of RIGHT.
2538 (window--resize-siblings
2539 right
(- delta
) horizontal nil
'before
2541 (+ (window-left-column left
) (window-total-size left t
))
2542 (+ (window-top-line left
) (window-total-size left
)))))))
2543 (unless (zerop delta
)
2544 ;; Don't report an error in the standard case.
2545 (unless (window-resize-apply frame horizontal
)
2546 ;; But do report an error if applying the changes fails.
2547 (error "Failed adjusting window %s" window
)))))))
2549 (defun enlarge-window (delta &optional horizontal
)
2550 "Make the selected window DELTA lines taller.
2551 Interactively, if no argument is given, make the selected window
2552 one line taller. If optional argument HORIZONTAL is non-nil,
2553 make selected window wider by DELTA columns. If DELTA is
2554 negative, shrink selected window by -DELTA lines or columns.
2557 (let ((minibuffer-window (minibuffer-window)))
2560 ((window-size-fixed-p nil horizontal
)
2561 (error "Selected window has fixed size"))
2562 ((window-minibuffer-p)
2564 (error "Cannot resize minibuffer window horizontally")
2565 (window--resize-mini-window (selected-window) delta
)))
2566 ((and (not horizontal
)
2567 (window-full-height-p)
2568 (eq (window-frame minibuffer-window
) (selected-frame))
2569 (not resize-mini-windows
))
2570 ;; If the selected window is full height and `resize-mini-windows'
2571 ;; is nil, resize the minibuffer window.
2572 (window--resize-mini-window minibuffer-window
(- delta
)))
2573 ((window--resizable-p nil delta horizontal
)
2574 (window-resize nil delta horizontal
))
2578 (window-max-delta nil horizontal
)
2579 (- (window-min-delta nil horizontal
)))
2582 (defun shrink-window (delta &optional horizontal
)
2583 "Make the selected window DELTA lines smaller.
2584 Interactively, if no argument is given, make the selected window
2585 one line smaller. If optional argument HORIZONTAL is non-nil,
2586 make selected window narrower by DELTA columns. If DELTA is
2587 negative, enlarge selected window by -DELTA lines or columns.
2588 Also see the `window-min-height' variable.
2591 (let ((minibuffer-window (minibuffer-window)))
2594 ((window-size-fixed-p nil horizontal
)
2595 (error "Selected window has fixed size"))
2596 ((window-minibuffer-p)
2598 (error "Cannot resize minibuffer window horizontally")
2599 (window--resize-mini-window (selected-window) (- delta
))))
2600 ((and (not horizontal
)
2601 (window-full-height-p)
2602 (eq (window-frame minibuffer-window
) (selected-frame))
2603 (not resize-mini-windows
))
2604 ;; If the selected window is full height and `resize-mini-windows'
2605 ;; is nil, resize the minibuffer window.
2606 (window--resize-mini-window minibuffer-window delta
))
2607 ((window--resizable-p nil
(- delta
) horizontal
)
2608 (window-resize nil
(- delta
) horizontal
))
2612 (- (window-min-delta nil horizontal
))
2613 (window-max-delta nil horizontal
))
2616 (defun maximize-window (&optional window
)
2618 Make WINDOW as large as possible without deleting any windows.
2619 WINDOW must be a valid window and defaults to the selected one."
2621 (setq window
(window-normalize-window window
))
2622 (window-resize window
(window-max-delta window
))
2623 (window-resize window
(window-max-delta window t
) t
))
2625 (defun minimize-window (&optional window
)
2627 Make WINDOW as small as possible without deleting any windows.
2628 WINDOW must be a valid window and defaults to the selected one."
2630 (setq window
(window-normalize-window window
))
2631 (window-resize window
(- (window-min-delta window
)))
2632 (window-resize window
(- (window-min-delta window t
)) t
))
2634 (defun frame-root-window-p (window)
2635 "Return non-nil if WINDOW is the root window of its frame."
2636 (eq window
(frame-root-window window
)))
2638 (defun window--subtree (window &optional next
)
2639 "Return window subtree rooted at WINDOW.
2640 Optional argument NEXT non-nil means include WINDOW's right
2641 siblings in the return value.
2643 See the documentation of `window-tree' for a description of the
2650 ((window-top-child window
)
2651 (cons t
(cons (window-edges window
)
2652 (window--subtree (window-top-child window
) t
))))
2653 ((window-left-child window
)
2654 (cons nil
(cons (window-edges window
)
2655 (window--subtree (window-left-child window
) t
))))
2658 (setq window
(when next
(window-next-sibling window
))))
2661 (defun window-tree (&optional frame
)
2662 "Return the window tree of frame FRAME.
2663 FRAME must be a live frame and defaults to the selected frame.
2664 The return value is a list of the form (ROOT MINI), where ROOT
2665 represents the window tree of the frame's root window, and MINI
2666 is the frame's minibuffer window.
2668 If the root window is not split, ROOT is the root window itself.
2669 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
2670 for a horizontal split, and t for a vertical split. EDGES gives
2671 the combined size and position of the child windows in the split,
2672 and the rest of the elements are the child windows in the split.
2673 Each of the child windows may again be a window or a list
2674 representing a window split, and so on. EDGES is a list (LEFT
2675 TOP RIGHT BOTTOM) as returned by `window-edges'."
2676 (setq frame
(window-normalize-frame frame
))
2677 (window--subtree (frame-root-window frame
) t
))
2679 (defun other-window (count &optional all-frames
)
2680 "Select another window in cyclic ordering of windows.
2681 COUNT specifies the number of windows to skip, starting with the
2682 selected window, before making the selection. If COUNT is
2683 positive, skip COUNT windows forwards. If COUNT is negative,
2684 skip -COUNT windows backwards. COUNT zero means do not skip any
2685 window, so select the selected window. In an interactive call,
2686 COUNT is the numeric prefix argument. Return nil.
2688 If the `other-window' parameter of the selected window is a
2689 function and `ignore-window-parameters' is nil, call that
2690 function with the arguments COUNT and ALL-FRAMES.
2692 This function does not select a window whose `no-other-window'
2693 window parameter is non-nil.
2695 This function uses `next-window' for finding the window to
2696 select. The argument ALL-FRAMES has the same meaning as in
2697 `next-window', but the MINIBUF argument of `next-window' is
2698 always effectively nil."
2700 (let* ((window (selected-window))
2701 (function (and (not ignore-window-parameters
)
2702 (window-parameter window
'other-window
)))
2703 old-window old-count
)
2704 (if (functionp function
)
2705 (funcall function count all-frames
)
2706 ;; `next-window' and `previous-window' may return a window we are
2707 ;; not allowed to select. Hence we need an exit strategy in case
2708 ;; all windows are non-selectable.
2711 (setq window
(next-window window nil all-frames
))
2713 ((eq window old-window
)
2714 (when (= count old-count
)
2715 ;; Keep out of infinite loops. When COUNT has not changed
2716 ;; since we last looked at `window' we're probably in one.
2718 ((window-parameter window
'no-other-window
)
2720 ;; The first non-selectable window `next-window' got us:
2721 ;; Remember it and the current value of COUNT.
2722 (setq old-window window
)
2723 (setq old-count count
)))
2725 (setq count
(1- count
)))))
2727 (setq window
(previous-window window nil all-frames
))
2729 ((eq window old-window
)
2730 (when (= count old-count
)
2731 ;; Keep out of infinite loops. When COUNT has not changed
2732 ;; since we last looked at `window' we're probably in one.
2734 ((window-parameter window
'no-other-window
)
2736 ;; The first non-selectable window `previous-window' got
2737 ;; us: Remember it and the current value of COUNT.
2738 (setq old-window window
)
2739 (setq old-count count
)))
2741 (setq count
(1+ count
)))))
2743 (select-window window
)
2744 ;; Always return nil.
2747 ;; This should probably return non-nil when the selected window is part
2748 ;; of an atomic window whose root is the frame's root window.
2749 (defun one-window-p (&optional nomini all-frames
)
2750 "Return non-nil if the selected window is the only window.
2751 Optional arg NOMINI non-nil means don't count the minibuffer
2752 even if it is active. Otherwise, the minibuffer is counted
2755 Optional argument ALL-FRAMES specifies the set of frames to
2756 consider, see also `next-window'. ALL-FRAMES nil or omitted
2757 means consider windows on the selected frame only, plus the
2758 minibuffer window if specified by the NOMINI argument. If the
2759 minibuffer counts, consider all windows on all frames that share
2760 that minibuffer too. The remaining non-nil values of ALL-FRAMES
2761 with a special meaning are:
2763 - t means consider all windows on all existing frames.
2765 - `visible' means consider all windows on all visible frames on
2766 the current terminal.
2768 - 0 (the number zero) means consider all windows on all visible
2769 and iconified frames on the current terminal.
2771 - A frame means consider all windows on that frame only.
2773 Anything else means consider all windows on the selected frame
2775 (let ((base-window (selected-window)))
2776 (if (and nomini
(eq base-window
(minibuffer-window)))
2777 (setq base-window
(next-window base-window
)))
2779 (next-window base-window
(if nomini
'arg
) all-frames
))))
2781 ;;; Deleting windows.
2782 (defun window-deletable-p (&optional window
)
2783 "Return t if WINDOW can be safely deleted from its frame.
2784 WINDOW must be a valid window and defaults to the selected one.
2785 Return `frame' if deleting WINDOW should also delete its frame."
2786 (setq window
(window-normalize-window window
))
2788 (unless ignore-window-parameters
2789 ;; Handle atomicity.
2790 (when (window-parameter window
'window-atom
)
2791 (setq window
(window-atom-root window
))))
2793 (let ((frame (window-frame window
)))
2795 ((frame-root-window-p window
)
2796 ;; WINDOW's frame can be deleted only if there are other frames
2797 ;; on the same terminal, and it does not contain the active
2799 (unless (or (eq frame
(next-frame frame
0))
2800 (let ((minibuf (active-minibuffer-window)))
2801 (and minibuf
(eq frame
(window-frame minibuf
)))))
2803 ((or ignore-window-parameters
2804 (not (eq window
(window--major-non-side-window frame
))))
2805 ;; WINDOW can be deleted unless it is the major non-side window of
2809 (defun window--in-subtree-p (window root
)
2810 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
2811 (or (eq window root
)
2812 (let ((parent (window-parent window
)))
2815 (if (eq parent root
)
2817 (setq parent
(window-parent parent
))))))))
2819 (defun delete-window (&optional window
)
2821 WINDOW must be a valid window and defaults to the selected one.
2824 If the variable `ignore-window-parameters' is non-nil or the
2825 `delete-window' parameter of WINDOW equals t, do not process any
2826 parameters of WINDOW. Otherwise, if the `delete-window'
2827 parameter of WINDOW specifies a function, call that function with
2828 WINDOW as its sole argument and return the value returned by that
2831 Otherwise, if WINDOW is part of an atomic window, call
2832 `delete-window' with the root of the atomic window as its
2833 argument. Signal an error if WINDOW is either the only window on
2834 its frame, the last non-side window, or part of an atomic window
2835 that is its frame's root window."
2837 (setq window
(window-normalize-window window
))
2838 (let* ((frame (window-frame window
))
2839 (function (window-parameter window
'delete-window
))
2840 (parent (window-parent window
))
2842 (window--check frame
)
2844 ;; Handle window parameters.
2846 ;; Ignore window parameters if `ignore-window-parameters' tells
2847 ;; us so or `delete-window' equals t.
2848 ((or ignore-window-parameters
(eq function t
)))
2849 ((functionp function
)
2850 ;; The `delete-window' parameter specifies the function to call.
2851 ;; If that function is `ignore' nothing is done. It's up to the
2852 ;; function called here to avoid infinite recursion.
2853 (throw 'done
(funcall function window
)))
2854 ((and (window-parameter window
'window-atom
)
2855 (setq atom-root
(window-atom-root window
))
2856 (not (eq atom-root window
)))
2857 (if (eq atom-root
(frame-root-window frame
))
2858 (error "Root of atomic window is root window of its frame")
2859 (throw 'done
(delete-window atom-root
))))
2861 (error "Attempt to delete minibuffer or sole ordinary window"))
2862 ((eq window
(window--major-non-side-window frame
))
2863 (error "Attempt to delete last non-side window")))
2865 (let* ((horizontal (window-left-child parent
))
2866 (size (window-total-size window horizontal
))
2868 (window--in-subtree-p (frame-selected-window frame
) window
))
2869 ;; Emacs 23 preferably gives WINDOW's space to its left
2871 (sibling (or (window-left window
) (window-right window
))))
2872 (window--resize-reset frame horizontal
)
2874 ((and (not window-combination-resize
)
2875 sibling
(window-sizable-p sibling size
))
2876 ;; Resize WINDOW's sibling.
2877 (window--resize-this-window sibling size horizontal nil t
)
2878 (set-window-new-normal
2879 sibling
(+ (window-normal-size sibling horizontal
)
2880 (window-normal-size window horizontal
))))
2881 ((window--resizable-p window
(- size
) horizontal nil nil nil t
)
2882 ;; Can do without resizing fixed-size windows.
2883 (window--resize-siblings window
(- size
) horizontal
))
2885 ;; Can't do without resizing fixed-size windows.
2886 (window--resize-siblings window
(- size
) horizontal t
)))
2887 ;; Actually delete WINDOW.
2888 (delete-window-internal window
)
2889 (when (and frame-selected
2891 (frame-selected-window frame
) 'no-other-window
))
2892 ;; `delete-window-internal' has selected a window that should
2893 ;; not be selected, fix this here.
2894 (other-window -
1 frame
))
2895 (run-window-configuration-change-hook frame
)
2896 (window--check frame
)
2897 ;; Always return nil.
2900 (defun delete-other-windows (&optional window
)
2901 "Make WINDOW fill its frame.
2902 WINDOW must be a valid window and defaults to the selected one.
2905 If the variable `ignore-window-parameters' is non-nil or the
2906 `delete-other-windows' parameter of WINDOW equals t, do not
2907 process any parameters of WINDOW. Otherwise, if the
2908 `delete-other-windows' parameter of WINDOW specifies a function,
2909 call that function with WINDOW as its sole argument and return
2910 the value returned by that function.
2912 Otherwise, if WINDOW is part of an atomic window, call this
2913 function with the root of the atomic window as its argument. If
2914 WINDOW is a non-side window, make WINDOW the only non-side window
2915 on the frame. Side windows are not deleted. If WINDOW is a side
2916 window signal an error."
2918 (setq window
(window-normalize-window window
))
2919 (let* ((frame (window-frame window
))
2920 (function (window-parameter window
'delete-other-windows
))
2921 (window-side (window-parameter window
'window-side
))
2922 atom-root side-main
)
2923 (window--check frame
)
2926 ;; Ignore window parameters if `ignore-window-parameters' is t or
2927 ;; `delete-other-windows' is t.
2928 ((or ignore-window-parameters
(eq function t
)))
2929 ((functionp function
)
2930 ;; The `delete-other-windows' parameter specifies the function
2931 ;; to call. If the function is `ignore' no windows are deleted.
2932 ;; It's up to the function called to avoid infinite recursion.
2933 (throw 'done
(funcall function window
)))
2934 ((and (window-parameter window
'window-atom
)
2935 (setq atom-root
(window-atom-root window
))
2936 (not (eq atom-root window
)))
2937 (if (eq atom-root
(frame-root-window frame
))
2938 (error "Root of atomic window is root window of its frame")
2939 (throw 'done
(delete-other-windows atom-root
))))
2940 ((memq window-side window-sides
)
2941 (error "Cannot make side window the only window"))
2942 ((and (window-minibuffer-p window
)
2943 (not (eq window
(frame-root-window window
))))
2944 (error "Can't expand minibuffer to full frame")))
2946 ;; If WINDOW is the major non-side window, do nothing.
2947 (if (window-with-parameter 'window-side
)
2948 (setq side-main
(window--major-non-side-window frame
))
2949 (setq side-main
(frame-root-window frame
)))
2950 (unless (eq window side-main
)
2951 (delete-other-windows-internal window side-main
)
2952 (run-window-configuration-change-hook frame
)
2953 (window--check frame
))
2954 ;; Always return nil.
2957 (defun delete-other-windows-vertically (&optional window
)
2958 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
2959 This may be a useful alternative binding for \\[delete-other-windows]
2960 if you often split windows horizontally."
2962 (let* ((window (or window
(selected-window)))
2963 (edges (window-edges window
))
2965 (while (not (eq (setq w
(next-window w
1)) window
))
2966 (let ((e (window-edges w
)))
2967 (when (and (= (car e
) (car edges
))
2968 (= (nth 2 e
) (nth 2 edges
)))
2970 (mapc 'delete-window delenda
)))
2972 ;;; Windows and buffers.
2974 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
2975 ;; for (1) determining which buffer to show in the window when its
2976 ;; buffer shall be buried or killed and (2) which buffer to show for
2977 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2979 ;; `prev-buffers' consists of <buffer, window-start, window-point>
2980 ;; triples. The entries on this list are ordered by the time their
2981 ;; buffer has been removed from the window, the most recently removed
2982 ;; buffer's entry being first. The window-start and window-point
2983 ;; components are `window-start' and `window-point' at the time the
2984 ;; buffer was removed from the window which implies that the entry must
2985 ;; be added when `set-window-buffer' removes the buffer from the window.
2987 ;; `next-buffers' is the list of buffers that have been replaced
2988 ;; recently by `switch-to-prev-buffer'. These buffers are the least
2989 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
2990 ;; candidates of `switch-to-next-buffer' to switch to. This list is
2991 ;; reset to nil by any action changing the window's buffer with the
2992 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
2993 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
2994 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
2996 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
2997 ;; if such a buffer was killed while the window was hidden within a
2998 ;; window configuration. Such killed buffers get removed whenever
2999 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
3001 ;; The following function is called by `set-window-buffer' _before_ it
3002 ;; replaces the buffer of the argument window with the new buffer.
3003 (defun record-window-buffer (&optional window
)
3004 "Record WINDOW's buffer.
3005 WINDOW must be a live window and defaults to the selected one."
3006 (let* ((window (window-normalize-window window t
))
3007 (buffer (window-buffer window
))
3008 (entry (assq buffer
(window-prev-buffers window
))))
3009 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
3010 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3011 (set-window-next-buffers window nil
)
3014 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
3015 (set-window-prev-buffers
3016 window
(assq-delete-all buffer
(window-prev-buffers window
))))
3018 ;; Don't record insignificant buffers.
3019 (unless (eq (aref (buffer-name buffer
) 0) ?\s
)
3020 ;; Add an entry for buffer to WINDOW's previous buffers.
3021 (with-current-buffer buffer
3022 (let ((start (window-start window
))
3023 (point (window-point window
)))
3027 ;; We have an entry, update marker positions.
3028 (list (set-marker (nth 1 entry
) start
)
3029 (set-marker (nth 2 entry
) point
))
3030 ;; Make new markers.
3031 (list (copy-marker start
)
3032 (copy-marker point
)))))
3034 (set-window-prev-buffers
3035 window
(cons entry
(window-prev-buffers window
))))))))
3037 (defun unrecord-window-buffer (&optional window buffer
)
3038 "Unrecord BUFFER in WINDOW.
3039 WINDOW must be a live window and defaults to the selected one.
3040 BUFFER must be a live buffer and defaults to the buffer of
3042 (let* ((window (window-normalize-window window t
))
3043 (buffer (or buffer
(window-buffer window
))))
3044 (set-window-prev-buffers
3045 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3046 (set-window-next-buffers
3047 window
(delq buffer
(window-next-buffers window
)))))
3049 (defun set-window-buffer-start-and-point (window buffer
&optional start point
)
3050 "Set WINDOW's buffer to BUFFER.
3051 WINDOW must be a live window and defaults to the selected one.
3052 Optional argument START non-nil means set WINDOW's start position
3053 to START. Optional argument POINT non-nil means set WINDOW's
3054 point to POINT. If WINDOW is selected this also sets BUFFER's
3055 `point' to POINT. If WINDOW is selected and the buffer it showed
3056 before was current this also makes BUFFER the current buffer."
3057 (setq window
(window-normalize-window window t
))
3058 (let ((selected (eq window
(selected-window)))
3059 (current (eq (window-buffer window
) (current-buffer))))
3060 (set-window-buffer window buffer
)
3061 (when (and selected current
)
3062 (set-buffer buffer
))
3064 ;; Don't force window-start here (even if POINT is nil).
3065 (set-window-start window start t
))
3067 (set-window-point window point
))))
3069 (defcustom switch-to-visible-buffer t
3070 "If non-nil, allow switching to an already visible buffer.
3071 If this variable is non-nil, `switch-to-prev-buffer' and
3072 `switch-to-next-buffer' may switch to an already visible buffer
3073 provided the buffer was shown in the argument window before. If
3074 this variable is nil, `switch-to-prev-buffer' and
3075 `switch-to-next-buffer' always try to avoid switching to a buffer
3076 that is already visible in another window on the same frame."
3081 (defun switch-to-prev-buffer (&optional window bury-or-kill
)
3082 "In WINDOW switch to previous buffer.
3083 WINDOW must be a live window and defaults to the selected one.
3084 Return the buffer switched to, nil if no suitable buffer could be
3087 Optional argument BURY-OR-KILL non-nil means the buffer currently
3088 shown in WINDOW is about to be buried or killed and consequently
3089 shall not be switched to in future invocations of this command.
3091 As a special case, if BURY-OR-KILL equals `append', this means to
3092 move the buffer to the end of WINDOW's previous buffers list so a
3093 future invocation of `switch-to-prev-buffer' less likely switches
3096 (let* ((window (window-normalize-window window t
))
3097 (frame (window-frame window
))
3098 (old-buffer (window-buffer window
))
3099 ;; Save this since it's destroyed by `set-window-buffer'.
3100 (next-buffers (window-next-buffers window
))
3101 (pred (frame-parameter frame
'buffer-predicate
))
3102 entry new-buffer killed-buffers visible
)
3103 (when (window-minibuffer-p window
)
3104 ;; Don't switch in minibuffer window.
3105 (unless (setq window
(minibuffer-selected-window))
3106 (error "Window %s is a minibuffer window" window
)))
3108 (when (window-dedicated-p window
)
3109 ;; Don't switch in dedicated window.
3110 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3113 ;; Scan WINDOW's previous buffers first, skipping entries of next
3115 (dolist (entry (window-prev-buffers window
))
3116 (when (and (setq new-buffer
(car entry
))
3117 (or (buffer-live-p new-buffer
)
3118 (not (setq killed-buffers
3119 (cons new-buffer killed-buffers
))))
3120 (not (eq new-buffer old-buffer
))
3121 (or (null pred
) (funcall pred new-buffer
))
3122 ;; When BURY-OR-KILL is nil, avoid switching to a
3123 ;; buffer in WINDOW's next buffers list.
3124 (or bury-or-kill
(not (memq new-buffer next-buffers
))))
3125 (if (and (not switch-to-visible-buffer
)
3126 (get-buffer-window new-buffer frame
))
3127 ;; Try to avoid showing a buffer visible in some other
3129 (setq visible new-buffer
)
3130 (set-window-buffer-start-and-point
3131 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3133 ;; Scan reverted buffer list of WINDOW's frame next, skipping
3134 ;; entries of next buffers. Note that when we bury or kill a
3135 ;; buffer we don't reverse the global buffer list to avoid showing
3136 ;; a buried buffer instead. Otherwise, we must reverse the global
3137 ;; buffer list in order to make sure that switching to the
3138 ;; previous/next buffer traverse it in opposite directions.
3139 (dolist (buffer (if bury-or-kill
3141 (nreverse (buffer-list frame
))))
3142 (when (and (buffer-live-p buffer
)
3143 (not (eq buffer old-buffer
))
3144 (or (null pred
) (funcall pred buffer
))
3145 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3146 (or bury-or-kill
(not (memq buffer next-buffers
))))
3147 (if (get-buffer-window buffer frame
)
3148 ;; Try to avoid showing a buffer visible in some other window.
3150 (setq visible buffer
))
3151 (setq new-buffer buffer
)
3152 (set-window-buffer-start-and-point window new-buffer
)
3154 (unless bury-or-kill
3155 ;; Scan reverted next buffers last (must not use nreverse
3157 (dolist (buffer (reverse next-buffers
))
3158 ;; Actually, buffer _must_ be live here since otherwise it
3159 ;; would have been caught in the scan of previous buffers.
3160 (when (and (or (buffer-live-p buffer
)
3161 (not (setq killed-buffers
3162 (cons buffer killed-buffers
))))
3163 (not (eq buffer old-buffer
))
3164 (or (null pred
) (funcall pred buffer
))
3165 (setq entry
(assq buffer
(window-prev-buffers window
))))
3166 (setq new-buffer buffer
)
3167 (set-window-buffer-start-and-point
3168 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3171 ;; Show a buffer visible in another window.
3173 (setq new-buffer visible
)
3174 (set-window-buffer-start-and-point window new-buffer
)))
3177 (let ((entry (and (eq bury-or-kill
'append
)
3178 (assq old-buffer
(window-prev-buffers window
)))))
3179 ;; Remove `old-buffer' from WINDOW's previous and (restored list
3180 ;; of) next buffers.
3181 (set-window-prev-buffers
3182 window
(assq-delete-all old-buffer
(window-prev-buffers window
)))
3183 (set-window-next-buffers window
(delq old-buffer next-buffers
))
3185 ;; Append old-buffer's entry to list of WINDOW's previous
3186 ;; buffers so it's less likely to get switched to soon but
3187 ;; `display-buffer-in-previous-window' can nevertheless find
3189 (set-window-prev-buffers
3190 window
(append (window-prev-buffers window
) (list entry
)))))
3191 ;; Move `old-buffer' to head of WINDOW's restored list of next
3193 (set-window-next-buffers
3194 window
(cons old-buffer
(delq old-buffer next-buffers
))))
3196 ;; Remove killed buffers from WINDOW's previous and next buffers.
3197 (when killed-buffers
3198 (dolist (buffer killed-buffers
)
3199 (set-window-prev-buffers
3200 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3201 (set-window-next-buffers
3202 window
(delq buffer
(window-next-buffers window
)))))
3204 ;; Return new-buffer.
3207 (defun switch-to-next-buffer (&optional window
)
3208 "In WINDOW switch to next buffer.
3209 WINDOW must be a live window and defaults to the selected one.
3210 Return the buffer switched to, nil if no suitable buffer could be
3213 (let* ((window (window-normalize-window window t
))
3214 (frame (window-frame window
))
3215 (old-buffer (window-buffer window
))
3216 (next-buffers (window-next-buffers window
))
3217 (pred (frame-parameter frame
'buffer-predicate
))
3218 new-buffer entry killed-buffers visible
)
3219 (when (window-minibuffer-p window
)
3220 ;; Don't switch in minibuffer window.
3221 (unless (setq window
(minibuffer-selected-window))
3222 (error "Window %s is a minibuffer window" window
)))
3224 (when (window-dedicated-p window
)
3225 ;; Don't switch in dedicated window.
3226 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3229 ;; Scan WINDOW's next buffers first.
3230 (dolist (buffer next-buffers
)
3231 (when (and (or (buffer-live-p buffer
)
3232 (not (setq killed-buffers
3233 (cons buffer killed-buffers
))))
3234 (not (eq buffer old-buffer
))
3235 (or (null pred
) (funcall pred buffer
))
3236 (setq entry
(assq buffer
(window-prev-buffers window
))))
3237 (setq new-buffer buffer
)
3238 (set-window-buffer-start-and-point
3239 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3241 ;; Scan the buffer list of WINDOW's frame next, skipping previous
3243 (dolist (buffer (buffer-list frame
))
3244 (when (and (buffer-live-p buffer
)
3245 (not (eq buffer old-buffer
))
3246 (or (null pred
) (funcall pred buffer
))
3247 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3248 (not (assq buffer
(window-prev-buffers window
))))
3249 (if (get-buffer-window buffer frame
)
3250 ;; Try to avoid showing a buffer visible in some other window.
3251 (setq visible buffer
)
3252 (setq new-buffer buffer
)
3253 (set-window-buffer-start-and-point window new-buffer
)
3255 ;; Scan WINDOW's reverted previous buffers last (must not use
3257 (dolist (entry (reverse (window-prev-buffers window
)))
3258 (when (and (setq new-buffer
(car entry
))
3259 (or (buffer-live-p new-buffer
)
3260 (not (setq killed-buffers
3261 (cons new-buffer killed-buffers
))))
3262 (not (eq new-buffer old-buffer
))
3263 (or (null pred
) (funcall pred new-buffer
)))
3264 (if (and (not switch-to-visible-buffer
)
3265 (get-buffer-window new-buffer frame
))
3266 ;; Try to avoid showing a buffer visible in some other window.
3268 (setq visible new-buffer
))
3269 (set-window-buffer-start-and-point
3270 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3273 ;; Show a buffer visible in another window.
3275 (setq new-buffer visible
)
3276 (set-window-buffer-start-and-point window new-buffer
)))
3278 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
3279 (set-window-next-buffers window
(delq new-buffer next-buffers
))
3281 ;; Remove killed buffers from WINDOW's previous and next buffers.
3282 (when killed-buffers
3283 (dolist (buffer killed-buffers
)
3284 (set-window-prev-buffers
3285 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3286 (set-window-next-buffers
3287 window
(delq buffer
(window-next-buffers window
)))))
3289 ;; Return new-buffer.
3292 (defun get-next-valid-buffer (list &optional buffer visible-ok frame
)
3293 "Search LIST for a valid buffer to display in FRAME.
3294 Return nil when all buffers in LIST are undesirable for display,
3295 otherwise return the first suitable buffer in LIST.
3297 Buffers not visible in windows are preferred to visible buffers,
3298 unless VISIBLE-OK is non-nil.
3299 If the optional argument FRAME is nil, it defaults to the selected frame.
3300 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
3301 ;; This logic is more or less copied from other-buffer.
3302 (setq frame
(or frame
(selected-frame)))
3303 (let ((pred (frame-parameter frame
'buffer-predicate
))
3305 (while (and (not found
) list
)
3306 (setq buf
(car list
))
3307 (if (and (not (eq buffer buf
))
3309 (or (null pred
) (funcall pred buf
))
3310 (not (eq (aref (buffer-name buf
) 0) ?\s
))
3311 (or visible-ok
(null (get-buffer-window buf
'visible
))))
3313 (setq list
(cdr list
))))
3316 (defun last-buffer (&optional buffer visible-ok frame
)
3317 "Return the last buffer in FRAME's buffer list.
3318 If BUFFER is the last buffer, return the preceding buffer
3319 instead. Buffers not visible in windows are preferred to visible
3320 buffers, unless optional argument VISIBLE-OK is non-nil.
3321 Optional third argument FRAME nil or omitted means use the
3322 selected frame's buffer list. If no such buffer exists, return
3323 the buffer `*scratch*', creating it if necessary."
3324 (setq frame
(or frame
(selected-frame)))
3325 (or (get-next-valid-buffer (nreverse (buffer-list frame
))
3326 buffer visible-ok frame
)
3327 (get-buffer "*scratch*")
3328 (let ((scratch (get-buffer-create "*scratch*")))
3329 (set-buffer-major-mode scratch
)
3332 (defcustom frame-auto-hide-function
#'iconify-frame
3333 "Function called to automatically hide frames.
3334 The function is called with one argument - a frame.
3336 Functions affected by this option are those that bury a buffer
3337 shown in a separate frame like `quit-window' and `bury-buffer'."
3338 :type
'(choice (const :tag
"Iconify" iconify-frame
)
3339 (const :tag
"Delete" delete-frame
)
3340 (const :tag
"Do nothing" ignore
)
3346 (defun window--delete (&optional window dedicated-only kill
)
3347 "Delete WINDOW if possible.
3348 WINDOW must be a live window and defaults to the selected one.
3349 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
3350 only if it's dedicated to its buffer. Optional argument KILL
3351 means the buffer shown in window will be killed. Return non-nil
3352 if WINDOW gets deleted or its frame is auto-hidden."
3353 (setq window
(window-normalize-window window t
))
3354 (unless (and dedicated-only
(not (window-dedicated-p window
)))
3355 (let ((deletable (window-deletable-p window
)))
3357 ((eq deletable
'frame
)
3358 (let ((frame (window-frame window
)))
3361 (delete-frame frame
))
3362 ((functionp frame-auto-hide-function
)
3363 (funcall frame-auto-hide-function frame
))))
3366 (delete-window window
)
3369 (defun bury-buffer (&optional buffer-or-name
)
3370 "Put BUFFER-OR-NAME at the end of the list of all buffers.
3371 There it is the least likely candidate for `other-buffer' to
3372 return; thus, the least likely buffer for \\[switch-to-buffer] to
3375 You can specify a buffer name as BUFFER-OR-NAME, or an actual
3376 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
3377 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
3378 remove the current buffer from the selected window if it is
3381 (let* ((buffer (window-normalize-buffer buffer-or-name
)))
3382 ;; If `buffer-or-name' is not on the selected frame we unrecord it
3383 ;; although it's not "here" (call it a feature).
3384 (bury-buffer-internal buffer
)
3385 ;; Handle case where `buffer-or-name' is nil and the current buffer
3386 ;; is shown in the selected window.
3388 ((or buffer-or-name
(not (eq buffer
(window-buffer)))))
3389 ((window--delete nil t
))
3391 ;; Switch to another buffer in window.
3392 (set-window-dedicated-p nil nil
)
3393 (switch-to-prev-buffer nil
'bury
)))
3395 ;; Always return nil.
3398 (defun unbury-buffer ()
3399 "Switch to the last buffer in the buffer list."
3401 (switch-to-buffer (last-buffer)))
3403 (defun next-buffer ()
3404 "In selected window switch to next buffer."
3407 ((window-minibuffer-p)
3408 (error "Cannot switch buffers in minibuffer window"))
3409 ((eq (window-dedicated-p) t
)
3410 (error "Window is strongly dedicated to its buffer"))
3412 (switch-to-next-buffer))))
3414 (defun previous-buffer ()
3415 "In selected window switch to previous buffer."
3418 ((window-minibuffer-p)
3419 (error "Cannot switch buffers in minibuffer window"))
3420 ((eq (window-dedicated-p) t
)
3421 (error "Window is strongly dedicated to its buffer"))
3423 (switch-to-prev-buffer))))
3425 (defun delete-windows-on (&optional buffer-or-name frame
)
3426 "Delete all windows showing BUFFER-OR-NAME.
3427 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3428 and defaults to the current buffer.
3430 The following non-nil values of the optional argument FRAME
3431 have special meanings:
3433 - t means consider all windows on the selected frame only.
3435 - `visible' means consider all windows on all visible frames on
3436 the current terminal.
3438 - 0 (the number zero) means consider all windows on all visible
3439 and iconified frames on the current terminal.
3441 - A frame means consider all windows on that frame only.
3443 Any other value of FRAME means consider all windows on all
3446 When a window showing BUFFER-OR-NAME is dedicated and the only
3447 window of its frame, that frame is deleted when there are other
3449 (interactive "BDelete windows on (buffer):\nP")
3450 (let ((buffer (window-normalize-buffer buffer-or-name
))
3451 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3452 ;; `window-list-1' based function.
3453 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
3454 (dolist (window (window-list-1 nil nil all-frames
))
3455 (if (eq (window-buffer window
) buffer
)
3456 (let ((deletable (window-deletable-p window
)))
3458 ((and (eq deletable
'frame
) (window-dedicated-p window
))
3459 ;; Delete frame if and only if window is dedicated.
3460 (delete-frame (window-frame window
)))
3463 (delete-window window
))
3465 ;; In window switch to previous buffer.
3466 (set-window-dedicated-p window nil
)
3467 (switch-to-prev-buffer window
'bury
))))
3468 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3469 (unrecord-window-buffer window buffer
)))))
3471 (defun replace-buffer-in-windows (&optional buffer-or-name
)
3472 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
3473 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3474 and defaults to the current buffer.
3476 When a window showing BUFFER-OR-NAME is dedicated, that window is
3477 deleted. If that window is the only window on its frame, the
3478 frame is deleted too when there are other frames left. If there
3479 are no other frames left, some other buffer is displayed in that
3482 This function removes the buffer denoted by BUFFER-OR-NAME from
3483 all window-local buffer lists."
3484 (interactive "bBuffer to replace: ")
3485 (let ((buffer (window-normalize-buffer buffer-or-name
)))
3486 (dolist (window (window-list-1 nil nil t
))
3487 (if (eq (window-buffer window
) buffer
)
3488 (unless (window--delete window t t
)
3489 ;; Switch to another buffer in window.
3490 (set-window-dedicated-p window nil
)
3491 (switch-to-prev-buffer window
'kill
))
3492 ;; Unrecord BUFFER in WINDOW.
3493 (unrecord-window-buffer window buffer
)))))
3495 (defun quit-restore-window (&optional window bury-or-kill
)
3496 "Quit WINDOW and deal with its buffer.
3497 WINDOW must be a live window and defaults to the selected one.
3499 According to information stored in WINDOW's `quit-restore' window
3500 parameter either (1) delete WINDOW and its frame, (2) delete
3501 WINDOW, (3) restore the buffer previously displayed in WINDOW,
3502 or (4) make WINDOW display some other buffer than the present
3503 one. If non-nil, reset `quit-restore' parameter to nil.
3505 Optional second argument BURY-OR-KILL tells how to proceed with
3506 the buffer of WINDOW. The following values are handled:
3508 `nil' means to not handle the buffer in a particular way. This
3509 means that if WINDOW is not deleted by this function, invoking
3510 `switch-to-prev-buffer' will usually show the buffer again.
3512 `append' means that if WINDOW is not deleted, move its buffer to
3513 the end of WINDOW's previous buffers so it's less likely that a
3514 future invocation of `switch-to-prev-buffer' will switch to it.
3515 Also, move the buffer to the end of the frame's buffer list.
3517 `bury' means that if WINDOW is not deleted, remove its buffer
3518 from WINDOW'S list of previous buffers. Also, move the buffer
3519 to the end of the frame's buffer list. This value provides the
3520 most reliable remedy to not have `switch-to-prev-buffer' switch
3521 to this buffer again without killing the buffer.
3523 `kill' means to kill WINDOW's buffer."
3524 (setq window
(window-normalize-window window t
))
3525 (let* ((buffer (window-buffer window
))
3526 (quit-restore (window-parameter window
'quit-restore
))
3528 (let* ((prev-buffers (window-prev-buffers window
))
3529 (prev-buffer (caar prev-buffers
)))
3530 (and (or (not (eq prev-buffer buffer
))
3531 (and (cdr prev-buffers
)
3532 (not (eq (setq prev-buffer
(cadr prev-buffers
))
3537 ((and (not prev-buffer
)
3538 (memq (nth 1 quit-restore
) '(window frame
))
3539 (eq (nth 3 quit-restore
) buffer
)
3540 ;; Delete WINDOW if possible.
3541 (window--delete window nil
(eq bury-or-kill
'kill
)))
3542 ;; If the previously selected window is still alive, select it.
3543 (when (window-live-p (nth 2 quit-restore
))
3544 (select-window (nth 2 quit-restore
))))
3545 ((and (listp (setq quad
(nth 1 quit-restore
)))
3546 (buffer-live-p (car quad
))
3547 (eq (nth 3 quit-restore
) buffer
))
3548 ;; Show another buffer stored in quit-restore parameter.
3549 (when (and (integerp (nth 3 quad
))
3550 (/= (nth 3 quad
) (window-total-size window
)))
3551 ;; Try to resize WINDOW to its old height but don't signal an
3554 (window-resize window
(- (nth 3 quad
) (window-total-size window
)))
3556 (set-window-dedicated-p window nil
)
3557 ;; Restore WINDOW's previous buffer, start and point position.
3558 (set-window-buffer-start-and-point
3559 window
(nth 0 quad
) (nth 1 quad
) (nth 2 quad
))
3560 ;; Deal with the buffer we just removed from WINDOW.
3561 (setq entry
(and (eq bury-or-kill
'append
)
3562 (assq buffer
(window-prev-buffers window
))))
3564 ;; Remove buffer from WINDOW's previous and next buffers.
3565 (set-window-prev-buffers
3566 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3567 (set-window-next-buffers
3568 window
(delq buffer
(window-next-buffers window
))))
3570 ;; Append old buffer's entry to list of WINDOW's previous
3571 ;; buffers so it's less likely to get switched to soon but
3572 ;; `display-buffer-in-previous-window' can nevertheless find it.
3573 (set-window-prev-buffers
3574 window
(append (window-prev-buffers window
) (list entry
))))
3575 ;; Reset the quit-restore parameter.
3576 (set-window-parameter window
'quit-restore nil
)
3577 ;; Select old window.
3578 (when (window-live-p (nth 2 quit-restore
))
3579 (select-window (nth 2 quit-restore
))))
3581 ;; Show some other buffer in WINDOW and reset the quit-restore
3583 (set-window-parameter window
'quit-restore nil
)
3584 ;; Make sure that WINDOW is no more dedicated.
3585 (set-window-dedicated-p window nil
)
3586 (switch-to-prev-buffer window bury-or-kill
)))
3588 ;; Deal with the buffer.
3590 ((not (buffer-live-p buffer
)))
3591 ((eq bury-or-kill
'kill
)
3592 (kill-buffer buffer
))
3594 (bury-buffer-internal buffer
)))))
3596 (defun quit-window (&optional kill window
)
3597 "Quit WINDOW and bury its buffer.
3598 WINDOW must be a live window and defaults to the selected one.
3599 With prefix argument KILL non-nil, kill the buffer instead of
3602 According to information stored in WINDOW's `quit-restore' window
3603 parameter either (1) delete WINDOW and its frame, (2) delete
3604 WINDOW, (3) restore the buffer previously displayed in WINDOW,
3605 or (4) make WINDOW display some other buffer than the present
3606 one. If non-nil, reset `quit-restore' parameter to nil."
3608 (quit-restore-window window
(if kill
'kill
'bury
)))
3610 (defun quit-windows-on (&optional buffer-or-name kill frame
)
3611 "Quit all windows showing BUFFER-OR-NAME.
3612 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3613 and defaults to the current buffer. Optional argument KILL
3614 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
3615 BUFFER-OR-NAME. Optional argument FRAME is handled as by
3616 `delete-windows-on'.
3618 This function calls `quit-window' on all candidate windows
3619 showing BUFFER-OR-NAME."
3620 (interactive "BQuit windows on (buffer):\nP")
3621 (let ((buffer (window-normalize-buffer buffer-or-name
))
3622 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3623 ;; `window-list-1' based function.
3624 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
3625 (dolist (window (window-list-1 nil nil all-frames
))
3626 (if (eq (window-buffer window
) buffer
)
3627 (quit-window kill window
)
3628 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3629 (unrecord-window-buffer window buffer
)))))
3631 ;;; Splitting windows.
3632 (defun window-split-min-size (&optional horizontal
)
3633 "Return minimum height of any window when splitting windows.
3634 Optional argument HORIZONTAL non-nil means return minimum width."
3636 (max window-min-width window-safe-min-width
)
3637 (max window-min-height window-safe-min-height
)))
3639 (defun split-window (&optional window size side
)
3640 "Make a new window adjacent to WINDOW.
3641 WINDOW must be a valid window and defaults to the selected one.
3642 Return the new window which is always a live window.
3644 Optional argument SIZE a positive number means make WINDOW SIZE
3645 lines or columns tall. If SIZE is negative, make the new window
3646 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
3647 absolute value can be less than `window-min-height' or
3648 `window-min-width'; so this command can make a new window as
3649 small as one line or two columns. SIZE defaults to half of
3650 WINDOW's size. Interactively, SIZE is the prefix argument.
3652 Optional third argument SIDE nil (or `below') specifies that the
3653 new window shall be located below WINDOW. SIDE `above' means the
3654 new window shall be located above WINDOW. In both cases SIZE
3655 specifies the new number of lines for WINDOW (or the new window
3656 if SIZE is negative) including space reserved for the mode and/or
3659 SIDE t (or `right') specifies that the new window shall be
3660 located on the right side of WINDOW. SIDE `left' means the new
3661 window shall be located on the left of WINDOW. In both cases
3662 SIZE specifies the new number of columns for WINDOW (or the new
3663 window provided SIZE is negative) including space reserved for
3664 fringes and the scrollbar or a divider column. Any other non-nil
3665 value for SIDE is currently handled like t (or `right').
3667 If the variable `ignore-window-parameters' is non-nil or the
3668 `split-window' parameter of WINDOW equals t, do not process any
3669 parameters of WINDOW. Otherwise, if the `split-window' parameter
3670 of WINDOW specifies a function, call that function with all three
3671 arguments and return the value returned by that function.
3673 Otherwise, if WINDOW is part of an atomic window, \"split\" the
3674 root of that atomic window. The new window does not become a
3675 member of that atomic window.
3677 If WINDOW is live, properties of the new window like margins and
3678 scrollbars are inherited from WINDOW. If WINDOW is an internal
3679 window, these properties as well as the buffer displayed in the
3680 new window are inherited from the window selected on WINDOW's
3681 frame. The selected window is not changed by this function."
3683 (setq window
(window-normalize-window window
))
3686 ((memq side
'(below above right left
)) side
)
3688 (horizontal (not (memq side
'(below above
))))
3689 (frame (window-frame window
))
3690 (parent (window-parent window
))
3691 (function (window-parameter window
'split-window
))
3692 (window-side (window-parameter window
'window-side
))
3693 ;; Rebind the following two variables since in some cases we
3694 ;; have to override their value.
3695 (window-combination-limit window-combination-limit
)
3696 (window-combination-resize window-combination-resize
)
3699 (window--check frame
)
3702 ;; Ignore window parameters if either `ignore-window-parameters'
3703 ;; is t or the `split-window' parameter equals t.
3704 ((or ignore-window-parameters
(eq function t
)))
3705 ((functionp function
)
3706 ;; The `split-window' parameter specifies the function to call.
3707 ;; If that function is `ignore', do nothing.
3708 (throw 'done
(funcall function window size side
)))
3709 ;; If WINDOW is part of an atomic window, split the root window
3710 ;; of that atomic window instead.
3711 ((and (window-parameter window
'window-atom
)
3712 (setq atom-root
(window-atom-root window
))
3713 (not (eq atom-root window
)))
3714 (throw 'done
(split-window atom-root size side
)))
3715 ;; If WINDOW is a side window or its first or last child is a
3716 ;; side window, throw an error unless `window-combination-resize'
3718 ((and (not (eq window-combination-resize
'side
))
3719 (or (window-parameter window
'window-side
)
3720 (and (window-child window
)
3721 (or (window-parameter
3722 (window-child window
) 'window-side
)
3724 (window-last-child window
) 'window-side
)))))
3725 (error "Cannot split side window or parent of side window"))
3726 ;; If `window-combination-resize' is 'side and window has a side
3727 ;; window sibling, bind `window-combination-limit' to t.
3728 ((and (not (eq window-combination-resize
'side
))
3729 (or (and (window-prev-sibling window
)
3731 (window-prev-sibling window
) 'window-side
))
3732 (and (window-next-sibling window
)
3734 (window-next-sibling window
) 'window-side
))))
3735 (setq window-combination-limit t
)))
3737 ;; If `window-combination-resize' is t and SIZE is non-negative,
3738 ;; bind `window-combination-limit' to t.
3739 (when (and (eq window-combination-resize t
) size
(> size
0))
3740 (setq window-combination-limit t
))
3743 ;; `parent-size' is the size of WINDOW's parent, provided
3745 (when parent
(window-total-size parent horizontal
)))
3746 ;; `resize' non-nil means we are supposed to resize other
3747 ;; windows in WINDOW's combination.
3749 (and window-combination-resize
3750 (or (window-parameter window
'window-side
)
3751 (not (eq window-combination-resize
'side
)))
3752 (not (eq window-combination-limit t
))
3753 ;; Resize makes sense in iso-combinations only.
3754 (window-combined-p window horizontal
)))
3755 ;; `old-size' is the current size of WINDOW.
3756 (old-size (window-total-size window horizontal
))
3757 ;; `new-size' is the specified or calculated size of the
3762 (max (window-split-min-size horizontal
)
3764 ;; When resizing try to give the new window the
3765 ;; average size of a window in its combination.
3767 (window-min-size parent horizontal
))
3769 (1+ (window-combinations
3770 parent horizontal
))))
3771 ;; Else try to give the new window half the size
3772 ;; of WINDOW (plus an eventual odd line).
3773 (+ (/ old-size
2) (% old-size
2)))))
3775 ;; SIZE non-negative specifies the new size of WINDOW.
3777 ;; Note: Specifying a non-negative SIZE is practically
3778 ;; always done as workaround for making the new window
3779 ;; appear above or on the left of the new window (the
3780 ;; ispell window is a typical example of that). In all
3781 ;; these cases the SIDE argument should be set to 'above
3782 ;; or 'left in order to support the 'resize option.
3783 ;; Here we have to nest the windows instead, see above.
3786 ;; SIZE negative specifies the size of the new window.
3788 new-parent new-normal
)
3795 ;; SIZE unspecified, resizing.
3796 (when (and (not (window-sizable-p parent
(- new-size
) horizontal
))
3797 ;; Try again with minimum split size.
3799 (max new-size
(window-split-min-size horizontal
)))
3800 (not (window-sizable-p parent
(- new-size
) horizontal
)))
3801 (error "Window %s too small for splitting" parent
)))
3802 ((> (+ new-size
(window-min-size window horizontal
)) old-size
)
3803 ;; SIZE unspecified, no resizing.
3804 (error "Window %s too small for splitting" window
))))
3806 (or (>= size old-size
)
3807 (< new-size
(if horizontal
3808 window-safe-min-width
3809 window-safe-min-width
))))
3810 ;; SIZE specified as new size of old window. If the new size
3811 ;; is larger than the old size or the size of the new window
3812 ;; would be less than the safe minimum, signal an error.
3813 (error "Window %s too small for splitting" window
))
3815 ;; SIZE specified, resizing.
3816 (unless (window-sizable-p parent
(- new-size
) horizontal
)
3817 ;; If we cannot resize the parent give up.
3818 (error "Window %s too small for splitting" parent
)))
3820 (if horizontal window-safe-min-width window-safe-min-height
))
3821 (< (- old-size new-size
)
3822 (if horizontal window-safe-min-width window-safe-min-height
)))
3823 ;; SIZE specification violates minimum size restrictions.
3824 (error "Window %s too small for splitting" window
)))
3826 (window--resize-reset frame horizontal
)
3829 ;; Make new-parent non-nil if we need a new parent window;
3830 ;; either because we want to nest or because WINDOW is not
3832 (or (eq window-combination-limit t
)
3833 (not (window-combined-p window horizontal
))))
3835 ;; Make new-normal the normal size of the new window.
3837 (size (/ (float new-size
) (if new-parent old-size parent-size
)))
3839 (resize (/ 1.0 (1+ (window-combinations parent horizontal
))))
3840 (t (/ (window-normal-size window horizontal
) 2.0))))
3843 ;; Try to get space from OLD's siblings. We could go "up" and
3844 ;; try getting additional space from surrounding windows but
3845 ;; we won't be able to return space to those windows when we
3846 ;; delete the one we create here. Hence we do not go up.
3848 (window--resize-child-windows parent
(- new-size
) horizontal
)
3849 (let* ((normal (- 1.0 new-normal
))
3850 (sub (window-child parent
)))
3852 (set-window-new-normal
3853 sub
(* (window-normal-size sub horizontal
) normal
))
3854 (setq sub
(window-right sub
)))))
3855 ;; Get entire space from WINDOW.
3856 (set-window-new-total window
(- old-size new-size
))
3857 (window--resize-this-window window
(- new-size
) horizontal
)
3858 (set-window-new-normal
3859 window
(- (if new-parent
1.0 (window-normal-size window horizontal
))
3862 (let* ((new (split-window-internal window new-size side new-normal
)))
3863 ;; Assign window-side parameters, if any.
3864 (when (eq window-combination-resize
'side
)
3867 (window-side window-side
)
3868 ((eq side
'above
) 'top
)
3869 ((eq side
'below
) 'bottom
)
3871 ;; We made a new side window.
3872 (set-window-parameter new
'window-side window-side
)
3873 (when (and new-parent
(window-parameter window
'window-side
))
3874 ;; We've been splitting a side root window. Give the
3875 ;; new parent the same window-side parameter.
3876 (set-window-parameter
3877 (window-parent new
) 'window-side window-side
))))
3879 (run-window-configuration-change-hook frame
)
3880 (window--check frame
)
3881 ;; Always return the new window.
3884 ;; I think this should be the default; I think people will prefer it--rms.
3885 (defcustom split-window-keep-point t
3886 "If non-nil, \\[split-window-below] preserves point in the new window.
3887 If nil, adjust point in the two windows to minimize redisplay.
3888 This option applies only to `split-window-below' and functions
3889 that call it. The low-level `split-window' function always keeps
3890 the original point in both windows."
3894 (defun split-window-below (&optional size
)
3895 "Split the selected window into two windows, one above the other.
3896 The selected window is above. The newly split-off window is
3897 below, and displays the same buffer. Return the new window.
3899 If optional argument SIZE is omitted or nil, both windows get the
3900 same height, or close to it. If SIZE is positive, the upper
3901 \(selected) window gets SIZE lines. If SIZE is negative, the
3902 lower (new) window gets -SIZE lines.
3904 If the variable `split-window-keep-point' is non-nil, both
3905 windows get the same value of point as the selected window.
3906 Otherwise, the window starts are chosen so as to minimize the
3907 amount of redisplay; this is convenient on slow terminals."
3909 (let ((old-window (selected-window))
3910 (old-point (window-point))
3911 (size (and size
(prefix-numeric-value size
)))
3912 moved-by-window-height moved new-window bottom
)
3913 (when (and size
(< size
0) (< (- size
) window-min-height
))
3914 ;; `split-window' would not signal an error here.
3915 (error "Size of new window too small"))
3916 (setq new-window
(split-window nil size
))
3917 (unless split-window-keep-point
3918 (with-current-buffer (window-buffer)
3919 ;; Use `save-excursion' around vertical movements below
3920 ;; (Bug#10971). Note: When the selected window's buffer has a
3921 ;; header line, up to two lines of the buffer may not show up
3922 ;; in the resulting configuration.
3924 (goto-char (window-start))
3925 (setq moved
(vertical-motion (window-height)))
3926 (set-window-start new-window
(point))
3927 (when (> (point) (window-point new-window
))
3928 (set-window-point new-window
(point)))
3929 (when (= moved
(window-height))
3930 (setq moved-by-window-height t
)
3931 (vertical-motion -
1))
3932 (setq bottom
(point)))
3933 (and moved-by-window-height
3935 (set-window-point old-window
(1- bottom
)))
3936 (and moved-by-window-height
3937 (<= (window-start new-window
) old-point
)
3938 (set-window-point new-window old-point
)
3939 (select-window new-window
))))
3940 ;; Always copy quit-restore parameter in interactive use.
3941 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
3943 (set-window-parameter new-window
'quit-restore quit-restore
)))
3946 (defalias 'split-window-vertically
'split-window-below
)
3948 (defun split-window-right (&optional size
)
3949 "Split the selected window into two side-by-side windows.
3950 The selected window is on the left. The newly split-off window
3951 is on the right, and displays the same buffer. Return the new
3954 If optional argument SIZE is omitted or nil, both windows get the
3955 same width, or close to it. If SIZE is positive, the left-hand
3956 \(selected) window gets SIZE columns. If SIZE is negative, the
3957 right-hand (new) window gets -SIZE columns. Here, SIZE includes
3958 the width of the window's scroll bar; if there are no scroll
3959 bars, it includes the width of the divider column to the window's
3962 (let ((old-window (selected-window))
3963 (size (and size
(prefix-numeric-value size
)))
3965 (when (and size
(< size
0) (< (- size
) window-min-width
))
3966 ;; `split-window' would not signal an error here.
3967 (error "Size of new window too small"))
3968 (setq new-window
(split-window nil size t
))
3969 ;; Always copy quit-restore parameter in interactive use.
3970 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
3972 (set-window-parameter new-window
'quit-restore quit-restore
)))
3975 (defalias 'split-window-horizontally
'split-window-right
)
3977 ;;; Balancing windows.
3979 ;; The following routine uses the recycled code from an old version of
3980 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
3981 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
3982 ;; more readable (FWIW we'd need three loops - one to calculate the
3983 ;; minimum sizes per window, one to enlarge or shrink windows until the
3984 ;; new parent-size matches, and one where we shrink the largest/enlarge
3985 ;; the smallest window).
3986 (defun balance-windows-2 (window horizontal
)
3987 "Subroutine of `balance-windows-1'.
3988 WINDOW must be a vertical combination (horizontal if HORIZONTAL
3990 (let* ((first (window-child window
))
3992 (number-of-children 0)
3993 (parent-size (window-new-total window
))
3994 (total-sum parent-size
)
3995 failed size sub-total sub-delta sub-amount rest
)
3997 (setq number-of-children
(1+ number-of-children
))
3998 (when (window-size-fixed-p sub horizontal
)
4000 (- total-sum
(window-total-size sub horizontal
)))
4001 (set-window-new-normal sub
'ignore
))
4002 (setq sub
(window-right sub
)))
4005 (while (and failed
(> number-of-children
0))
4006 (setq size
(/ total-sum number-of-children
))
4009 (while (and sub
(not failed
))
4010 ;; Ignore child windows that should be ignored or are stuck.
4011 (unless (window--resize-child-windows-skip-p sub
)
4012 (setq sub-total
(window-total-size sub horizontal
))
4013 (setq sub-delta
(- size sub-total
))
4015 (window-sizable sub sub-delta horizontal
))
4016 ;; Register the new total size for this child window.
4017 (set-window-new-total sub
(+ sub-total sub-amount
))
4018 (unless (= sub-amount sub-delta
)
4019 (setq total-sum
(- total-sum sub-total sub-amount
))
4020 (setq number-of-children
(1- number-of-children
))
4021 ;; We failed and need a new round.
4023 (set-window-new-normal sub
'skip
)))
4024 (setq sub
(window-right sub
))))
4026 (setq rest
(% total-sum number-of-children
))
4027 ;; Fix rounding by trying to enlarge non-stuck windows by one line
4028 ;; (column) until `rest' is zero.
4030 (while (and sub
(> rest
0))
4031 (unless (window--resize-child-windows-skip-p window
)
4032 (set-window-new-total sub
1 t
)
4033 (setq rest
(1- rest
)))
4034 (setq sub
(window-right sub
)))
4036 ;; Fix rounding by trying to enlarge stuck windows by one line
4037 ;; (column) until `rest' equals zero.
4039 (while (and sub
(> rest
0))
4040 (unless (eq (window-new-normal sub
) 'ignore
)
4041 (set-window-new-total sub
1 t
)
4042 (setq rest
(1- rest
)))
4043 (setq sub
(window-right sub
)))
4047 ;; Record new normal sizes.
4048 (set-window-new-normal
4049 sub
(/ (if (eq (window-new-normal sub
) 'ignore
)
4050 (window-total-size sub horizontal
)
4051 (window-new-total sub
))
4052 (float parent-size
)))
4053 ;; Recursively balance each window's child windows.
4054 (balance-windows-1 sub horizontal
)
4055 (setq sub
(window-right sub
)))))
4057 (defun balance-windows-1 (window &optional horizontal
)
4058 "Subroutine of `balance-windows'."
4059 (if (window-child window
)
4060 (let ((sub (window-child window
)))
4061 (if (window-combined-p sub horizontal
)
4062 (balance-windows-2 window horizontal
)
4063 (let ((size (window-new-total window
)))
4065 (set-window-new-total sub size
)
4066 (balance-windows-1 sub horizontal
)
4067 (setq sub
(window-right sub
))))))))
4069 (defun balance-windows (&optional window-or-frame
)
4070 "Balance the sizes of windows of WINDOW-OR-FRAME.
4071 WINDOW-OR-FRAME is optional and defaults to the selected frame.
4072 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
4073 windows of that frame. If WINDOW-OR-FRAME denotes a window,
4074 recursively balance the sizes of all child windows of that
4079 ((or (not window-or-frame
)
4080 (frame-live-p window-or-frame
))
4081 (frame-root-window window-or-frame
))
4082 ((or (window-live-p window-or-frame
)
4083 (window-child window-or-frame
))
4086 (error "Not a window or frame %s" window-or-frame
))))
4087 (frame (window-frame window
)))
4088 ;; Balance vertically.
4089 (window--resize-reset (window-frame window
))
4090 (balance-windows-1 window
)
4091 (window-resize-apply frame
)
4092 ;; Balance horizontally.
4093 (window--resize-reset (window-frame window
) t
)
4094 (balance-windows-1 window t
)
4095 (window-resize-apply frame t
)))
4097 (defun window-fixed-size-p (&optional window direction
)
4098 "Return t if WINDOW cannot be resized in DIRECTION.
4099 WINDOW defaults to the selected window. DIRECTION can be
4100 nil (i.e. any), `height' or `width'."
4101 (with-current-buffer (window-buffer window
)
4102 (when (and (boundp 'window-size-fixed
) window-size-fixed
)
4104 (member (cons direction window-size-fixed
)
4105 '((height . width
) (width . height
))))))))
4107 ;;; A different solution to balance-windows.
4108 (defvar window-area-factor
1
4109 "Factor by which the window area should be over-estimated.
4110 This is used by `balance-windows-area'.
4111 Changing this globally has no effect.")
4112 (make-variable-buffer-local 'window-area-factor
)
4114 (defun balance-windows-area-adjust (window delta horizontal
)
4115 "Wrapper around `window-resize' with error checking.
4116 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
4117 ;; `window-resize' may fail if delta is too large.
4118 (while (>= (abs delta
) 1)
4121 (window-resize window delta horizontal
)
4124 ;;(message "adjust: %s" (error-message-string err))
4125 (setq delta
(/ delta
2))))))
4127 (defun balance-windows-area ()
4128 "Make all visible windows the same area (approximately).
4129 See also `window-area-factor' to change the relative size of
4132 (let* ((unchanged 0) (carry 0) (round 0)
4133 ;; Remove fixed-size windows.
4134 (wins (delq nil
(mapcar (lambda (win)
4135 (if (not (window-fixed-size-p win
)) win
))
4136 (window-list nil
'nomini
))))
4139 ;; Resizing a window changes the size of surrounding windows in complex
4140 ;; ways, so it's difficult to balance them all. The introduction of
4141 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
4142 ;; very difficult to do. `balance-window' above takes an off-line
4143 ;; approach: get the whole window tree, then balance it, then try to
4144 ;; adjust the windows so they fit the result.
4145 ;; Here, instead, we take a "local optimization" approach, where we just
4146 ;; go through all the windows several times until nothing needs to be
4147 ;; changed. The main problem with this approach is that it's difficult
4148 ;; to make sure it terminates, so we use some heuristic to try and break
4149 ;; off infinite loops.
4150 ;; After a round without any change, we allow a second, to give a chance
4151 ;; to the carry to propagate a minor imbalance from the end back to
4153 (while (< unchanged
2)
4154 ;; (message "New round")
4155 (setq unchanged
(1+ unchanged
) round
(1+ round
))
4158 (while (progn (setq next
(next-window next
))
4159 (window-fixed-size-p next
)))
4160 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
4162 (< (car (window-edges win
)) (car (window-edges next
))))
4163 (areadiff (/ (- (* (window-height next
) (window-width next
)
4164 (buffer-local-value 'window-area-factor
4165 (window-buffer next
)))
4166 (* (window-height win
) (window-width win
)
4167 (buffer-local-value 'window-area-factor
4168 (window-buffer win
))))
4169 (max (buffer-local-value 'window-area-factor
4170 (window-buffer win
))
4171 (buffer-local-value 'window-area-factor
4172 (window-buffer next
)))))
4174 (+ (window-height win
) (window-height next
))
4175 (+ (window-width win
) (window-width next
))))
4176 (diff (/ areadiff edgesize
)))
4178 ;; Maybe diff is actually closer to 1 than to 0.
4179 (setq diff
(/ (* 3 areadiff
) (* 2 edgesize
))))
4180 (when (and (zerop diff
) (not (zerop areadiff
)))
4181 (setq diff
(/ (+ areadiff carry
) edgesize
))
4182 ;; Change things smoothly.
4183 (if (or (> diff
1) (< diff -
1)) (setq diff
(/ diff
2))))
4185 ;; Make sure negligible differences don't accumulate to
4186 ;; become significant.
4187 (setq carry
(+ carry areadiff
))
4188 ;; This used `adjust-window-trailing-edge' before and uses
4189 ;; `window-resize' now. Error wrapping is still needed.
4190 (balance-windows-area-adjust win diff horiz
)
4192 (let ((change (cons win
(window-edges win
))))
4193 ;; If the same change has been seen already for this window,
4194 ;; we're most likely in an endless loop, so don't count it as
4196 (unless (member change changelog
)
4197 (push change changelog
)
4198 (setq unchanged
0 carry
0)))))))
4199 ;; We've now basically balanced all the windows.
4200 ;; But there may be some minor off-by-one imbalance left over,
4201 ;; so let's do some fine tuning.
4202 ;; (bw-finetune wins)
4203 ;; (message "Done in %d rounds" round)
4206 ;;; Window states, how to get them and how to put them in a window.
4207 (defun window--state-get-1 (window &optional writable
)
4208 "Helper function for `window-state-get'."
4211 ((window-top-child window
) 'vc
)
4212 ((window-left-child window
) 'hc
)
4214 (buffer (window-buffer window
))
4215 (selected (eq window
(selected-window)))
4218 ,@(unless (window-next-sibling window
) `((last . t
)))
4219 (total-height .
,(window-total-size window
))
4220 (total-width .
,(window-total-size window t
))
4221 (normal-height .
,(window-normal-size window
))
4222 (normal-width .
,(window-normal-size window t
))
4223 (combination-limit .
,(window-combination-limit window
))
4224 ,@(let ((parameters (window-parameters window
))
4226 ;; Make copies of those window parameters whose
4227 ;; persistence property is `writable' if WRITABLE is
4228 ;; non-nil and non-nil if WRITABLE is nil.
4229 (dolist (par parameters
)
4230 (let ((pers (cdr (assq (car par
)
4231 window-persistent-parameters
))))
4232 (when (and pers
(or (not writable
) (eq pers
'writable
)))
4233 (setq list
(cons (cons (car par
) (cdr par
)) list
)))))
4234 ;; Add `clone-of' parameter if necessary.
4235 (let ((pers (cdr (assq 'clone-of
4236 window-persistent-parameters
))))
4237 (when (and pers
(or (not writable
) (eq pers
'writable
))
4238 (not (assq 'clone-of list
)))
4239 (setq list
(cons (cons 'clone-of window
) list
))))
4241 `((parameters .
,list
))))
4243 ;; All buffer related things go in here.
4244 (let ((point (window-point window
))
4245 (start (window-start window
)))
4247 ,(buffer-name buffer
)
4248 (selected .
,selected
)
4249 (hscroll .
,(window-hscroll window
))
4250 (fringes .
,(window-fringes window
))
4251 (margins .
,(window-margins window
))
4252 (scroll-bars .
,(window-scroll-bars window
))
4253 (vscroll .
,(window-vscroll window
))
4254 (dedicated .
,(window-dedicated-p window
))
4255 (point .
,(if writable point
4258 'window-point-insertion-type
4260 (start .
,(if writable start
(copy-marker start
)))))))))
4262 (when (memq type
'(vc hc
))
4264 (setq window
(window-child window
))
4266 (setq list
(cons (window--state-get-1 window writable
) list
))
4267 (setq window
(window-right window
)))
4269 (append head tail
)))
4271 (defun window-state-get (&optional window writable
)
4272 "Return state of WINDOW as a Lisp object.
4273 WINDOW can be any window and defaults to the root window of the
4276 Optional argument WRITABLE non-nil means do not use markers for
4277 sampling `window-point' and `window-start'. Together, WRITABLE
4278 and the variable `window-persistent-parameters' specify which
4279 window parameters are saved by this function. WRITABLE should be
4280 non-nil when the return value shall be written to a file and read
4281 back in another session. Otherwise, an application may run into
4282 an `invalid-read-syntax' error while attempting to read back the
4285 The return value can be used as argument for `window-state-put'
4286 to put the state recorded here into an arbitrary window. The
4287 value can be also stored on disk and read back in a new session."
4290 (if (window-valid-p window
)
4292 (error "%s is not a live or internal window" window
))
4293 (frame-root-window)))
4294 ;; The return value is a cons whose car specifies some constraints on
4295 ;; the size of WINDOW. The cdr lists the states of the child windows
4298 ;; Frame related things would go into a function, say `frame-state',
4299 ;; calling `window-state-get' to insert the frame's root window.
4300 `((min-height .
,(window-min-size window
))
4301 (min-width .
,(window-min-size window t
))
4302 (min-height-ignore .
,(window-min-size window nil t
))
4303 (min-width-ignore .
,(window-min-size window t t
))
4304 (min-height-safe .
,(window-min-size window nil
'safe
))
4305 (min-width-safe .
,(window-min-size window t
'safe
)))
4306 (window--state-get-1 window writable
)))
4308 (defvar window-state-put-list nil
4309 "Helper variable for `window-state-put'.")
4311 (defun window--state-put-1 (state &optional window ignore totals
)
4312 "Helper function for `window-state-put'."
4313 (let ((type (car state
)))
4314 (setq state
(cdr state
))
4317 ;; For a leaf window just add unprocessed entries to
4318 ;; `window-state-put-list'.
4319 (push (cons window state
) window-state-put-list
))
4320 ((memq type
'(vc hc
))
4321 (let* ((horizontal (eq type
'hc
))
4322 (total (window-total-size window horizontal
))
4325 (dolist (item state
)
4326 ;; Find the next child window. WINDOW always points to the
4327 ;; real window that we want to fill with what we find here.
4328 (when (memq (car item
) '(leaf vc hc
))
4329 (if (assq 'last item
)
4330 ;; The last child window. Below `window--state-put-1'
4331 ;; will put into it whatever ITEM has in store.
4333 ;; Not the last child window, prepare for splitting
4334 ;; WINDOW. SIZE is the new (and final) size of the old
4339 (cdr (assq (if horizontal
'total-width
'total-height
) item
))
4340 ;; Use normalized size and round.
4343 (if horizontal
'normal-width
'normal-height
)
4346 ;; Use safe sizes, we try to resize later.
4347 (setq size
(max size
(if horizontal
4348 window-safe-min-height
4349 window-safe-min-width
)))
4351 (if (window-sizable-p window
(- size
) horizontal
'safe
)
4352 (let* ((window-combination-limit
4353 (assq 'combination-limit item
)))
4354 ;; We must inherit the combination limit, otherwise
4355 ;; we might mess up handling of atomic and side
4357 (setq new
(split-window window size horizontal
)))
4358 ;; Give up if we can't resize window down to safe sizes.
4359 (error "Cannot resize window %s" window
))
4363 ;; When creating the first child window add for parent
4364 ;; unprocessed entries to `window-state-put-list'.
4365 (setq window-state-put-list
4366 (cons (cons (window-parent window
) state
)
4367 window-state-put-list
))))
4369 ;; Now process the current window (either the one we've just
4370 ;; split or the last child of its parent).
4371 (window--state-put-1 item window ignore totals
)
4372 ;; Continue with the last window split off.
4373 (setq window new
))))))))
4375 (defun window--state-put-2 (ignore)
4376 "Helper function for `window-state-put'."
4377 (dolist (item window-state-put-list
)
4378 (let ((window (car item
))
4379 (combination-limit (cdr (assq 'combination-limit item
)))
4380 (parameters (cdr (assq 'parameters item
)))
4381 (state (cdr (assq 'buffer item
))))
4382 (when combination-limit
4383 (set-window-combination-limit window combination-limit
))
4384 ;; Reset window's parameters and assign saved ones (we might want
4385 ;; a `remove-window-parameters' function here).
4386 (dolist (parameter (window-parameters window
))
4387 (set-window-parameter window
(car parameter
) nil
))
4389 (dolist (parameter parameters
)
4390 (set-window-parameter window
(car parameter
) (cdr parameter
))))
4391 ;; Process buffer related state.
4393 ;; We don't want to raise an error here so we create a buffer if
4395 (set-window-buffer window
(get-buffer-create (car state
)))
4396 (with-current-buffer (window-buffer window
)
4397 (set-window-hscroll window
(cdr (assq 'hscroll state
)))
4398 (apply 'set-window-fringes
4399 (cons window
(cdr (assq 'fringes state
))))
4400 (let ((margins (cdr (assq 'margins state
))))
4401 (set-window-margins window
(car margins
) (cdr margins
)))
4402 (let ((scroll-bars (cdr (assq 'scroll-bars state
))))
4403 (set-window-scroll-bars
4404 window
(car scroll-bars
) (nth 2 scroll-bars
) (nth 3 scroll-bars
)))
4405 (set-window-vscroll window
(cdr (assq 'vscroll state
)))
4406 ;; Adjust vertically.
4407 (if (memq window-size-fixed
'(t height
))
4408 ;; A fixed height window, try to restore the original size.
4409 (let ((delta (- (cdr (assq 'total-height item
))
4410 (window-total-height window
)))
4412 (when (window--resizable-p window delta
)
4413 (window-resize window delta
)))
4414 ;; Else check whether the window is not high enough.
4415 (let* ((min-size (window-min-size window nil ignore
))
4416 (delta (- min-size
(window-total-size window
))))
4417 (when (and (> delta
0)
4418 (window--resizable-p window delta nil ignore
))
4419 (window-resize window delta nil ignore
))))
4420 ;; Adjust horizontally.
4421 (if (memq window-size-fixed
'(t width
))
4422 ;; A fixed width window, try to restore the original size.
4423 (let ((delta (- (cdr (assq 'total-width item
))
4424 (window-total-width window
)))
4426 (when (window--resizable-p window delta
)
4427 (window-resize window delta
)))
4428 ;; Else check whether the window is not wide enough.
4429 (let* ((min-size (window-min-size window t ignore
))
4430 (delta (- min-size
(window-total-size window t
))))
4431 (when (and (> delta
0)
4432 (window--resizable-p window delta t ignore
))
4433 (window-resize window delta t ignore
))))
4434 ;; Set dedicated status.
4435 (set-window-dedicated-p window
(cdr (assq 'dedicated state
)))
4436 ;; Install positions (maybe we should do this after all windows
4437 ;; have been created and sized).
4439 (set-window-start window
(cdr (assq 'start state
)))
4440 (set-window-point window
(cdr (assq 'point state
))))
4441 ;; Select window if it's the selected one.
4442 (when (cdr (assq 'selected state
))
4443 (select-window window
)))))))
4445 (defun window-state-put (state &optional window ignore
)
4446 "Put window state STATE into WINDOW.
4447 STATE should be the state of a window returned by an earlier
4448 invocation of `window-state-get'. Optional argument WINDOW must
4449 specify a live window and defaults to the selected one.
4451 Optional argument IGNORE non-nil means ignore minimum window
4452 sizes and fixed size restrictions. IGNORE equal `safe' means
4453 windows can get as small as `window-safe-min-height' and
4454 `window-safe-min-width'."
4455 (setq window
(window-normalize-window window t
))
4456 (let* ((frame (window-frame window
))
4458 ;; We check here (1) whether the total sizes of root window of
4459 ;; STATE and that of WINDOW are equal so we can avoid
4460 ;; calculating new sizes, and (2) if we do have to resize
4461 ;; whether we can do so without violating size restrictions.
4463 (and (= (window-total-size window
)
4464 (cdr (assq 'total-height state
)))
4465 (= (window-total-size window t
)
4466 (cdr (assq 'total-width state
)))))
4467 (min-height (cdr (assq 'min-height head
)))
4468 (min-width (cdr (assq 'min-width head
))))
4469 (if (and (not totals
)
4470 (or (> min-height
(window-total-size window
))
4471 (> min-width
(window-total-size window t
)))
4473 (and (setq min-height
4474 (cdr (assq 'min-height-ignore head
)))
4476 (cdr (assq 'min-width-ignore head
)))
4477 (or (> min-height
(window-total-size window
))
4478 (> min-width
(window-total-size window t
)))
4479 (or (not (eq ignore
'safe
))
4480 (and (setq min-height
4481 (cdr (assq 'min-height-safe head
)))
4483 (cdr (assq 'min-width-safe head
)))
4485 (window-total-size window
))
4487 (window-total-size window t
))))))))
4488 ;; The check above might not catch all errors due to rounding
4489 ;; issues - so IGNORE equal 'safe might not always produce the
4490 ;; minimum possible state. But such configurations hardly make
4492 (error "Window %s too small to accommodate state" window
)
4493 (setq state
(cdr state
))
4494 (setq window-state-put-list nil
)
4495 ;; Work on the windows of a temporary buffer to make sure that
4496 ;; splitting proceeds regardless of any buffer local values of
4497 ;; `window-size-fixed'. Release that buffer after the buffers of
4498 ;; all live windows have been set by `window--state-put-2'.
4500 (set-window-buffer window
(current-buffer))
4501 (window--state-put-1 state window nil totals
)
4502 (window--state-put-2 ignore
))
4503 (window--check frame
))))
4505 (defun display-buffer-record-window (type window buffer
)
4506 "Record information for window used by `display-buffer'.
4507 TYPE specifies the type of the calling operation and must be one
4508 of the symbols 'reuse (when WINDOW existed already and was
4509 reused for displaying BUFFER), 'window (when WINDOW was created
4510 on an already existing frame), or 'frame (when WINDOW was
4511 created on a new frame). WINDOW is the window used for or created
4512 by the `display-buffer' routines. BUFFER is the buffer that
4515 This function installs or updates the quit-restore parameter of
4516 WINDOW. The quit-restore parameter is a list of four elements:
4517 The first element is one of the symbols 'window, 'frame, 'same or
4518 'other. The second element is either one of the symbols 'window
4519 or 'frame or a list whose elements are the buffer previously
4520 shown in the window, that buffer's window start and window point,
4521 and the window's height. The third element is the window
4522 selected at the time the parameter was created. The fourth
4526 (if (eq (window-buffer window
) buffer
)
4527 ;; WINDOW shows BUFFER already.
4528 (when (consp (window-parameter window
'quit-restore
))
4529 ;; If WINDOW has a quit-restore parameter, reset its car.
4530 (setcar (window-parameter window
'quit-restore
) 'same
))
4531 ;; WINDOW shows another buffer.
4532 (set-window-parameter
4533 window
'quit-restore
4535 ;; A quadruple of WINDOW's buffer, start, point and height.
4536 (list (window-buffer window
) (window-start window
)
4537 (window-point window
) (window-total-size window
))
4538 (selected-window) buffer
))))
4540 ;; WINDOW has been created on an existing frame.
4541 (set-window-parameter
4542 window
'quit-restore
4543 (list 'window
'window
(selected-window) buffer
)))
4545 ;; WINDOW has been created on a new frame.
4546 (set-window-parameter
4547 window
'quit-restore
4548 (list 'frame
'frame
(selected-window) buffer
)))))
4550 (defcustom display-buffer-function nil
4551 "If non-nil, function to call to handle `display-buffer'.
4552 It will receive two args, the buffer and a flag which if non-nil
4553 means that the currently selected window is not acceptable. It
4554 should choose or create a window, display the specified buffer in
4555 it, and return the window.
4557 The specified function should call `display-buffer-record-window'
4558 with corresponding arguments to set up the quit-restore parameter
4559 of the window used."
4562 (function :tag
"function"))
4565 (make-obsolete-variable 'display-buffer-function
4566 'display-buffer-alist
"24.3")
4568 ;; Eventually, we want to turn this into a defvar; instead of
4569 ;; customizing this, the user should use a `pop-up-frame-parameters'
4570 ;; alist entry in `display-buffer-base-action'.
4571 (defcustom pop-up-frame-alist nil
4572 "Alist of parameters for automatically generated new frames.
4573 If non-nil, the value you specify here is used by the default
4574 `pop-up-frame-function' for the creation of new frames.
4576 Since `pop-up-frame-function' is used by `display-buffer' for
4577 making new frames, any value specified here by default affects
4578 the automatic generation of new frames via `display-buffer' and
4579 all functions based on it. The behavior of `make-frame' is not
4580 affected by this variable."
4581 :type
'(repeat (cons :format
"%v"
4582 (symbol :tag
"Parameter")
4583 (sexp :tag
"Value")))
4586 (defcustom pop-up-frame-function
4587 (lambda () (make-frame pop-up-frame-alist
))
4588 "Function used by `display-buffer' for creating a new frame.
4589 This function is called with no arguments and should return a new
4590 frame. The default value calls `make-frame' with the argument
4591 `pop-up-frame-alist'."
4595 (defcustom special-display-buffer-names nil
4596 "List of names of buffers that should be displayed specially.
4597 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4598 its name is in this list, displays the buffer in a way specified
4599 by `special-display-function'. `special-display-popup-frame'
4600 \(the default for `special-display-function') usually displays
4601 the buffer in a separate frame made with the parameters specified
4602 by `special-display-frame-alist'. If `special-display-function'
4603 has been set to some other function, that function is called with
4604 the buffer as first, and nil as second argument.
4606 Alternatively, an element of this list can be specified as
4607 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
4608 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4609 `special-display-popup-frame' will interpret such pairs as frame
4610 parameters when it creates a special frame, overriding the
4611 corresponding values from `special-display-frame-alist'.
4613 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4614 `special-display-popup-frame' displays that buffer in the
4615 selected window. If FRAME-PARAMETERS contains (same-frame . t),
4616 it displays that buffer in a window on the selected frame.
4618 If `special-display-function' specifies some other function than
4619 `special-display-popup-frame', that function is called with the
4620 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
4623 Finally, an element of this list can be also specified as
4624 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
4625 `special-display-popup-frame' will call FUNCTION with the buffer
4626 named BUFFER-NAME as first argument, and OTHER-ARGS as the
4629 Any alternative function specified here is responsible for
4630 setting up the quit-restore parameter of the window used.
4632 If this variable appears \"not to work\", because you added a
4633 name to it but the corresponding buffer is displayed in the
4634 selected window, look at the values of `same-window-buffer-names'
4635 and `same-window-regexps'. Those variables take precedence over
4638 See also `special-display-regexps'."
4640 (choice :tag
"Buffer"
4642 (string :format
"%v")
4643 (cons :tag
"With parameters"
4646 (string :format
"%v")
4647 (repeat :tag
"Parameters"
4649 (symbol :tag
"Parameter")
4650 (sexp :tag
"Value"))))
4651 (list :tag
"With function"
4654 (string :format
"%v")
4655 (function :tag
"Function")
4656 (repeat :tag
"Arguments" (sexp)))))
4659 (make-obsolete-variable 'special-display-buffer-names
'display-buffer-alist
"24.3")
4660 (put 'special-display-buffer-names
'risky-local-variable t
)
4662 (defcustom special-display-regexps nil
4663 "List of regexps saying which buffers should be displayed specially.
4664 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4665 any regexp in this list matches its name, displays it specially
4666 using `special-display-function'. `special-display-popup-frame'
4667 \(the default for `special-display-function') usually displays
4668 the buffer in a separate frame made with the parameters specified
4669 by `special-display-frame-alist'. If `special-display-function'
4670 has been set to some other function, that function is called with
4671 the buffer as first, and nil as second argument.
4673 Alternatively, an element of this list can be specified as
4674 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
4675 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4676 `special-display-popup-frame' will then interpret these pairs as
4677 frame parameters when creating a special frame for a buffer whose
4678 name matches REGEXP, overriding the corresponding values from
4679 `special-display-frame-alist'.
4681 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4682 `special-display-popup-frame' displays buffers matching REGEXP in
4683 the selected window. (same-frame . t) in FRAME-PARAMETERS means
4684 to display such buffers in a window on the selected frame.
4686 If `special-display-function' specifies some other function than
4687 `special-display-popup-frame', that function is called with the
4688 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
4691 Finally, an element of this list can be also specified as
4692 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
4693 will then call FUNCTION with the buffer whose name matched
4694 REGEXP as first, and OTHER-ARGS as second argument.
4696 Any alternative function specified here is responsible for
4697 setting up the quit-restore parameter of the window used.
4699 If this variable appears \"not to work\", because you added a
4700 name to it but the corresponding buffer is displayed in the
4701 selected window, look at the values of `same-window-buffer-names'
4702 and `same-window-regexps'. Those variables take precedence over
4705 See also `special-display-buffer-names'."
4707 (choice :tag
"Buffer"
4709 (regexp :format
"%v")
4710 (cons :tag
"With parameters"
4713 (regexp :format
"%v")
4714 (repeat :tag
"Parameters"
4716 (symbol :tag
"Parameter")
4717 (sexp :tag
"Value"))))
4718 (list :tag
"With function"
4721 (regexp :format
"%v")
4722 (function :tag
"Function")
4723 (repeat :tag
"Arguments" (sexp)))))
4726 (make-obsolete-variable 'special-display-regexps
'display-buffer-alist
"24.3")
4727 (put 'special-display-regexps
'risky-local-variable t
)
4729 (defun special-display-p (buffer-name)
4730 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
4731 More precisely, return t if `special-display-buffer-names' or
4732 `special-display-regexps' contain a string entry equaling or
4733 matching BUFFER-NAME. If `special-display-buffer-names' or
4734 `special-display-regexps' contain a list entry whose car equals
4735 or matches BUFFER-NAME, the return value is the cdr of that
4739 ((member buffer-name special-display-buffer-names
)
4741 ((setq tmp
(assoc buffer-name special-display-buffer-names
))
4744 (dolist (regexp special-display-regexps
)
4747 (when (string-match-p regexp buffer-name
)
4749 ((and (consp regexp
) (stringp (car regexp
))
4750 (string-match-p (car regexp
) buffer-name
))
4751 (throw 'found
(cdr regexp
))))))))))
4753 (defcustom special-display-frame-alist
4754 '((height .
14) (width .
80) (unsplittable . t
))
4755 "Alist of parameters for special frames.
4756 Special frames are used for buffers whose names are listed in
4757 `special-display-buffer-names' and for buffers whose names match
4758 one of the regular expressions in `special-display-regexps'.
4760 This variable can be set in your init file, like this:
4762 (setq special-display-frame-alist '((width . 80) (height . 20)))
4764 These supersede the values given in `default-frame-alist'."
4765 :type
'(repeat (cons :format
"%v"
4766 (symbol :tag
"Parameter")
4767 (sexp :tag
"Value")))
4769 (make-obsolete-variable 'special-display-frame-alist
'display-buffer-alist
"24.3")
4771 (defun special-display-popup-frame (buffer &optional args
)
4772 "Pop up a frame displaying BUFFER and return its window.
4773 If BUFFER is already displayed in a visible or iconified frame,
4774 raise that frame. Otherwise, display BUFFER in a new frame.
4776 Optional argument ARGS is a list specifying additional
4779 If ARGS is an alist, use it as a list of frame parameters. If
4780 these parameters contain (same-window . t), display BUFFER in
4781 the selected window. If they contain (same-frame . t), display
4782 BUFFER in a window of the selected frame.
4784 If ARGS is a list whose car is a symbol, use (car ARGS) as a
4785 function to do the work. Pass it BUFFER as first argument, and
4786 pass the elements of (cdr ARGS) as the remaining arguments."
4787 (if (and args
(symbolp (car args
)))
4788 (apply (car args
) buffer
(cdr args
))
4789 (let ((window (get-buffer-window buffer
0)))
4791 ;; If we have a window already, make it visible.
4793 (let ((frame (window-frame window
)))
4794 (make-frame-visible frame
)
4796 (display-buffer-record-window 'reuse window buffer
)
4798 ;; Reuse the current window if the user requested it.
4799 (when (cdr (assq 'same-window args
))
4801 (progn (switch-to-buffer buffer nil t
) (selected-window))
4803 ;; Stay on the same frame if requested.
4804 (when (or (cdr (assq 'same-frame args
)) (cdr (assq 'same-window args
)))
4805 (let* ((pop-up-windows t
)
4807 special-display-buffer-names special-display-regexps
)
4808 (display-buffer buffer
)))
4809 ;; If no window yet, make one in a new frame.
4811 (with-current-buffer buffer
4812 (make-frame (append args special-display-frame-alist
))))
4813 (window (frame-selected-window frame
)))
4814 (display-buffer-record-window 'frame window buffer
)
4815 (unless (eq buffer
(window-buffer window
))
4816 (set-window-buffer window buffer
)
4817 (set-window-prev-buffers window nil
))
4818 (set-window-dedicated-p window t
)
4821 (defcustom special-display-function
'special-display-popup-frame
4822 "Function to call for displaying special buffers.
4823 This function is called with two arguments - the buffer and,
4824 optionally, a list - and should return a window displaying that
4825 buffer. The default value usually makes a separate frame for the
4826 buffer using `special-display-frame-alist' to specify the frame
4827 parameters. See the definition of `special-display-popup-frame'
4828 for how to specify such a function.
4830 A buffer is special when its name is either listed in
4831 `special-display-buffer-names' or matches a regexp in
4832 `special-display-regexps'.
4834 The specified function should call `display-buffer-record-window'
4835 with corresponding arguments to set up the quit-restore parameter
4836 of the window used."
4839 (make-obsolete-variable 'special-display-function
'display-buffer-alist
"24.3")
4841 (defcustom same-window-buffer-names nil
4842 "List of names of buffers that should appear in the \"same\" window.
4843 `display-buffer' and `pop-to-buffer' show a buffer whose name is
4844 on this list in the selected rather than some other window.
4846 An element of this list can be a cons cell instead of just a
4847 string. In that case, the cell's car must be a string specifying
4848 the buffer name. This is for compatibility with
4849 `special-display-buffer-names'; the cdr of the cons cell is
4852 See also `same-window-regexps'."
4853 :type
'(repeat (string :format
"%v"))
4856 (defcustom same-window-regexps nil
4857 "List of regexps saying which buffers should appear in the \"same\" window.
4858 `display-buffer' and `pop-to-buffer' show a buffer whose name
4859 matches a regexp on this list in the selected rather than some
4862 An element of this list can be a cons cell instead of just a
4863 string. In that case, the cell's car must be a regexp matching
4864 the buffer name. This is for compatibility with
4865 `special-display-regexps'; the cdr of the cons cell is ignored.
4867 See also `same-window-buffer-names'."
4868 :type
'(repeat (regexp :format
"%v"))
4871 (defun same-window-p (buffer-name)
4872 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
4873 This function returns non-nil if `display-buffer' or
4874 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
4875 selected rather than (as usual) some other window. See
4876 `same-window-buffer-names' and `same-window-regexps'."
4878 ((not (stringp buffer-name
)))
4879 ;; The elements of `same-window-buffer-names' can be buffer
4880 ;; names or cons cells whose cars are buffer names.
4881 ((member buffer-name same-window-buffer-names
))
4882 ((assoc buffer-name same-window-buffer-names
))
4884 (dolist (regexp same-window-regexps
)
4885 ;; The elements of `same-window-regexps' can be regexps
4886 ;; or cons cells whose cars are regexps.
4887 (when (or (and (stringp regexp
)
4888 (string-match-p regexp buffer-name
))
4889 (and (consp regexp
) (stringp (car regexp
))
4890 (string-match-p (car regexp
) buffer-name
)))
4891 (throw 'found t
)))))))
4893 (defcustom pop-up-frames nil
4894 "Whether `display-buffer' should make a separate frame.
4895 If nil, never make a separate frame.
4896 If the value is `graphic-only', make a separate frame
4897 on graphic displays only.
4898 Any other non-nil value means always make a separate frame."
4900 (const :tag
"Never" nil
)
4901 (const :tag
"On graphic displays only" graphic-only
)
4902 (const :tag
"Always" t
))
4905 (defcustom display-buffer-reuse-frames nil
4906 "Non-nil means `display-buffer' should reuse frames.
4907 If the buffer in question is already displayed in a frame, raise
4913 (make-obsolete-variable
4914 'display-buffer-reuse-frames
4915 "use a `reusable-frames' alist entry in `display-buffer-alist'."
4918 (defcustom pop-up-windows t
4919 "Non-nil means `display-buffer' should make a new window."
4923 (defcustom split-window-preferred-function
'split-window-sensibly
4924 "Function called by `display-buffer' routines to split a window.
4925 This function is called with a window as single argument and is
4926 supposed to split that window and return the new window. If the
4927 window can (or shall) not be split, it is supposed to return nil.
4928 The default is to call the function `split-window-sensibly' which
4929 tries to split the window in a way which seems most suitable.
4930 You can customize the options `split-height-threshold' and/or
4931 `split-width-threshold' in order to have `split-window-sensibly'
4932 prefer either vertical or horizontal splitting.
4934 If you set this to any other function, bear in mind that the
4935 `display-buffer' routines may call this function two times. The
4936 argument of the first call is the largest window on its frame.
4937 If that call fails to return a live window, the function is
4938 called again with the least recently used window as argument. If
4939 that call fails too, `display-buffer' will use an existing window
4940 to display its buffer.
4942 The window selected at the time `display-buffer' was invoked is
4943 still selected when this function is called. Hence you can
4944 compare the window argument with the value of `selected-window'
4945 if you intend to split the selected window instead or if you do
4946 not want to split the selected window."
4951 (defcustom split-height-threshold
80
4952 "Minimum height for splitting windows sensibly.
4953 If this is an integer, `split-window-sensibly' may split a window
4954 vertically only if it has at least this many lines. If this is
4955 nil, `split-window-sensibly' is not allowed to split a window
4956 vertically. If, however, a window is the only window on its
4957 frame, `split-window-sensibly' may split it vertically
4958 disregarding the value of this variable."
4959 :type
'(choice (const nil
) (integer :tag
"lines"))
4963 (defcustom split-width-threshold
160
4964 "Minimum width for splitting windows sensibly.
4965 If this is an integer, `split-window-sensibly' may split a window
4966 horizontally only if it has at least this many columns. If this
4967 is nil, `split-window-sensibly' is not allowed to split a window
4969 :type
'(choice (const nil
) (integer :tag
"columns"))
4973 (defun window-splittable-p (window &optional horizontal
)
4974 "Return non-nil if `split-window-sensibly' may split WINDOW.
4975 Optional argument HORIZONTAL nil or omitted means check whether
4976 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
4977 non-nil means check whether WINDOW may be split horizontally.
4979 WINDOW may be split vertically when the following conditions
4981 - `window-size-fixed' is either nil or equals `width' for the
4983 - `split-height-threshold' is an integer and WINDOW is at least as
4984 high as `split-height-threshold'.
4985 - When WINDOW is split evenly, the emanating windows are at least
4986 `window-min-height' lines tall and can accommodate at least one
4987 line plus - if WINDOW has one - a mode line.
4989 WINDOW may be split horizontally when the following conditions
4991 - `window-size-fixed' is either nil or equals `height' for the
4993 - `split-width-threshold' is an integer and WINDOW is at least as
4994 wide as `split-width-threshold'.
4995 - When WINDOW is split evenly, the emanating windows are at least
4996 `window-min-width' or two (whichever is larger) columns wide."
4997 (when (window-live-p window
)
4998 (with-current-buffer (window-buffer window
)
5000 ;; A window can be split horizontally when its width is not
5001 ;; fixed, it is at least `split-width-threshold' columns wide
5002 ;; and at least twice as wide as `window-min-width' and 2 (the
5003 ;; latter value is hardcoded).
5004 (and (memq window-size-fixed
'(nil height
))
5005 ;; Testing `window-full-width-p' here hardly makes any
5006 ;; sense nowadays. This can be done more intuitively by
5007 ;; setting up `split-width-threshold' appropriately.
5008 (numberp split-width-threshold
)
5009 (>= (window-width window
)
5010 (max split-width-threshold
5011 (* 2 (max window-min-width
2)))))
5012 ;; A window can be split vertically when its height is not
5013 ;; fixed, it is at least `split-height-threshold' lines high,
5014 ;; and it is at least twice as high as `window-min-height' and 2
5015 ;; if it has a mode line or 1.
5016 (and (memq window-size-fixed
'(nil width
))
5017 (numberp split-height-threshold
)
5018 (>= (window-height window
)
5019 (max split-height-threshold
5020 (* 2 (max window-min-height
5021 (if mode-line-format
2 1))))))))))
5023 (defun split-window-sensibly (&optional window
)
5024 "Split WINDOW in a way suitable for `display-buffer'.
5025 WINDOW defaults to the currently selected window.
5026 If `split-height-threshold' specifies an integer, WINDOW is at
5027 least `split-height-threshold' lines tall and can be split
5028 vertically, split WINDOW into two windows one above the other and
5029 return the lower window. Otherwise, if `split-width-threshold'
5030 specifies an integer, WINDOW is at least `split-width-threshold'
5031 columns wide and can be split horizontally, split WINDOW into two
5032 windows side by side and return the window on the right. If this
5033 can't be done either and WINDOW is the only window on its frame,
5034 try to split WINDOW vertically disregarding any value specified
5035 by `split-height-threshold'. If that succeeds, return the lower
5036 window. Return nil otherwise.
5038 By default `display-buffer' routines call this function to split
5039 the largest or least recently used window. To change the default
5040 customize the option `split-window-preferred-function'.
5042 You can enforce this function to not split WINDOW horizontally,
5043 by setting (or binding) the variable `split-width-threshold' to
5044 nil. If, in addition, you set `split-height-threshold' to zero,
5045 chances increase that this function does split WINDOW vertically.
5047 In order to not split WINDOW vertically, set (or bind) the
5048 variable `split-height-threshold' to nil. Additionally, you can
5049 set `split-width-threshold' to zero to make a horizontal split
5050 more likely to occur.
5052 Have a look at the function `window-splittable-p' if you want to
5053 know how `split-window-sensibly' determines whether WINDOW can be
5055 (let ((window (or window
(selected-window))))
5056 (or (and (window-splittable-p window
)
5057 ;; Split window vertically.
5058 (with-selected-window window
5059 (split-window-below)))
5060 (and (window-splittable-p window t
)
5061 ;; Split window horizontally.
5062 (with-selected-window window
5063 (split-window-right)))
5064 (and (eq window
(frame-root-window (window-frame window
)))
5065 (not (window-minibuffer-p window
))
5066 ;; If WINDOW is the only window on its frame and is not the
5067 ;; minibuffer window, try to split it vertically disregarding
5068 ;; the value of `split-height-threshold'.
5069 (let ((split-height-threshold 0))
5070 (when (window-splittable-p window
)
5071 (with-selected-window window
5072 (split-window-below))))))))
5074 (defun window--try-to-split-window (window)
5075 "Try to split WINDOW.
5076 Return value returned by `split-window-preferred-function' if it
5077 represents a live window, nil otherwise."
5078 (and (window-live-p window
)
5079 (not (frame-parameter (window-frame window
) 'unsplittable
))
5080 (let* ((window-combination-limit
5081 ;; When `window-combination-limit' equals
5082 ;; `display-buffer' bind it to t so resizing steals
5083 ;; space preferably from the window that was split.
5084 (if (eq window-combination-limit
'display-buffer
)
5086 window-combination-limit
))
5088 ;; Since `split-window-preferred-function' might
5089 ;; throw an error use `condition-case'.
5091 (funcall split-window-preferred-function window
)
5093 (and (window-live-p new-window
) new-window
))))
5095 (defun window--frame-usable-p (frame)
5096 "Return FRAME if it can be used to display a buffer."
5097 (when (frame-live-p frame
)
5098 (let ((window (frame-root-window frame
)))
5099 ;; `frame-root-window' may be an internal window which is considered
5100 ;; "dead" by `window-live-p'. Hence if `window' is not live we
5101 ;; implicitly know that `frame' has a visible window we can use.
5102 (unless (and (window-live-p window
)
5103 (or (window-minibuffer-p window
)
5104 ;; If the window is soft-dedicated, the frame is usable.
5105 ;; Actually, even if the window is really dedicated,
5106 ;; the frame is still usable by splitting it.
5107 ;; At least Emacs-22 allowed it, and it is desirable
5108 ;; when displaying same-frame windows.
5109 nil
; (eq t (window-dedicated-p window))
5113 (defcustom even-window-heights t
5114 "If non-nil `display-buffer' will try to even window heights.
5115 Otherwise `display-buffer' will leave the window configuration
5116 alone. Heights are evened only when `display-buffer' chooses a
5117 window that appears above or below the selected window."
5121 (defun window--even-window-heights (window)
5122 "Even heights of WINDOW and selected window.
5123 Do this only if these windows are vertically adjacent to each
5124 other, `even-window-heights' is non-nil, and the selected window
5125 is higher than WINDOW."
5126 (when (and even-window-heights
5127 ;; Even iff WINDOW forms a vertical combination with the
5128 ;; selected window, and WINDOW's height exceeds that of the
5129 ;; selected window, see also bug#11880.
5130 (window-combined-p window
)
5131 (= (window-child-count (window-parent window
)) 2)
5132 (eq (window-parent) (window-parent window
))
5133 (> (window-total-height) (window-total-height window
)))
5134 ;; Don't throw an error if we can't even window heights for
5138 (/ (- (window-total-height window
) (window-total-height)) 2))
5141 (defun window--display-buffer (buffer window type
&optional dedicated
)
5142 "Display BUFFER in WINDOW and make its frame visible.
5143 TYPE must be one of the symbols `reuse', `window' or `frame' and
5144 is passed unaltered to `display-buffer-record-window'. Set
5145 `window-dedicated-p' to DEDICATED if non-nil. Return WINDOW if
5146 BUFFER and WINDOW are live."
5147 (when (and (buffer-live-p buffer
) (window-live-p window
))
5148 (display-buffer-record-window type window buffer
)
5149 (unless (eq buffer
(window-buffer window
))
5150 (set-window-dedicated-p window nil
)
5151 (set-window-buffer window buffer
)
5153 (set-window-dedicated-p window dedicated
))
5154 (when (memq type
'(window frame
))
5155 (set-window-prev-buffers window nil
)))
5158 (defun window--maybe-raise-frame (frame)
5159 (let ((visible (frame-visible-p frame
)))
5160 (unless (or (not visible
)
5161 ;; Assume the selected frame is already visible enough.
5162 (eq frame
(selected-frame))
5163 ;; Assume the frame from which we invoked the
5164 ;; minibuffer is visible.
5165 (and (minibuffer-window-active-p (selected-window))
5166 (eq frame
(window-frame (minibuffer-selected-window)))))
5167 (raise-frame frame
))))
5169 ;; FIXME: Not implemented.
5170 ;; FIXME: By the way, there could be more levels of dedication:
5171 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
5172 ;; the window hasn't been used for something else yet.
5173 ;; - `softly' dedicated only allows reuse when asked explicitly.
5174 ;; - `strongly' never allows reuse.
5175 (defvar display-buffer-mark-dedicated nil
5176 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
5177 The actual non-nil value of this variable will be copied to the
5178 `window-dedicated-p' flag.")
5180 (defconst display-buffer--action-function-custom-type
5181 '(choice :tag
"Function"
5182 (const :tag
"--" ignore
) ; default for insertion
5183 (const display-buffer-reuse-window
)
5184 (const display-buffer-pop-up-window
)
5185 (const display-buffer-same-window
)
5186 (const display-buffer-pop-up-frame
)
5187 (const display-buffer-use-some-window
)
5188 (function :tag
"Other function"))
5189 "Custom type for `display-buffer' action functions.")
5191 (defconst display-buffer--action-custom-type
5192 `(cons :tag
"Action"
5193 (choice :tag
"Action functions"
5194 ,display-buffer--action-function-custom-type
5196 :tag
"List of functions"
5197 ,display-buffer--action-function-custom-type
))
5198 (alist :tag
"Action arguments"
5200 :value-type
(sexp :tag
"Value")))
5201 "Custom type for `display-buffer' actions.")
5203 (defvar display-buffer-overriding-action
'(nil . nil
)
5204 "Overriding action to perform to display a buffer.
5205 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5206 function or a list of functions. Each function should accept two
5207 arguments: a buffer to display and an alist similar to ALIST.
5208 See `display-buffer' for details.")
5209 (put 'display-buffer-overriding-action
'risky-local-variable t
)
5211 (defcustom display-buffer-alist nil
5212 "Alist of conditional actions for `display-buffer'.
5213 This is a list of elements (CONDITION . ACTION), where:
5215 CONDITION is either a regexp matching buffer names, or a function
5216 that takes a buffer and returns a boolean.
5218 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
5219 function or a list of functions. Each such function should
5220 accept two arguments: a buffer to display and an alist of the
5221 same form as ALIST. See `display-buffer' for details."
5222 :type
`(alist :key-type
5223 (choice :tag
"Condition"
5225 (function :tag
"Matcher function"))
5226 :value-type
,display-buffer--action-custom-type
)
5231 (defcustom display-buffer-base-action
'(nil . nil
)
5232 "User-specified default action for `display-buffer'.
5233 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5234 function or a list of functions. Each function should accept two
5235 arguments: a buffer to display and an alist similar to ALIST.
5236 See `display-buffer' for details."
5237 :type display-buffer--action-custom-type
5242 (defconst display-buffer-fallback-action
5243 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
5244 display-buffer-reuse-window
5245 display-buffer--maybe-pop-up-frame-or-window
5246 display-buffer-use-some-window
5247 ;; If all else fails, pop up a new frame.
5248 display-buffer-pop-up-frame
))
5249 "Default fallback action for `display-buffer'.
5250 This is the action used by `display-buffer' if no other actions
5251 specified, e.g. by the user options `display-buffer-alist' or
5252 `display-buffer-base-action'. See `display-buffer'.")
5253 (put 'display-buffer-fallback-action
'risky-local-variable t
)
5255 (defun display-buffer-assq-regexp (buffer-name alist
)
5256 "Retrieve ALIST entry corresponding to BUFFER-NAME."
5258 (dolist (entry alist
)
5259 (let ((key (car entry
)))
5260 (when (or (and (stringp key
)
5261 (string-match-p key buffer-name
))
5262 (and (symbolp key
) (functionp key
)
5263 (funcall key buffer-name alist
)))
5264 (throw 'match
(cdr entry
)))))))
5266 (defvar display-buffer--same-window-action
5267 '(display-buffer-same-window
5268 (inhibit-same-window . nil
))
5269 "A `display-buffer' action for displaying in the same window.")
5270 (put 'display-buffer--same-window-action
'risky-local-variable t
)
5272 (defvar display-buffer--other-frame-action
5273 '((display-buffer-reuse-window
5274 display-buffer-pop-up-frame
)
5275 (reusable-frames .
0)
5276 (inhibit-same-window . t
))
5277 "A `display-buffer' action for displaying in another frame.")
5278 (put 'display-buffer--other-frame-action
'risky-local-variable t
)
5280 (defun display-buffer (buffer-or-name &optional action frame
)
5281 "Display BUFFER-OR-NAME in some window, without selecting it.
5282 BUFFER-OR-NAME must be a buffer or the name of an existing
5283 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
5284 or nil if no such window is found.
5286 Optional argument ACTION should have the form (FUNCTION . ALIST).
5287 FUNCTION is either a function or a list of functions.
5288 ALIST is an arbitrary association list (alist).
5290 Each such FUNCTION should accept two arguments: the buffer to
5291 display and an alist. Based on those arguments, it should either
5292 display the buffer and return the window, or return nil if unable
5293 to display the buffer.
5295 The `display-buffer' function builds a function list and an alist
5296 by combining the functions and alists specified in
5297 `display-buffer-overriding-action', `display-buffer-alist', the
5298 ACTION argument, `display-buffer-base-action', and
5299 `display-buffer-fallback-action' (in order). Then it calls each
5300 function in the combined function list in turn, passing the
5301 buffer as the first argument and the combined alist as the second
5302 argument, until one of the functions returns non-nil.
5304 Available action functions include:
5305 `display-buffer-same-window'
5306 `display-buffer-reuse-window'
5307 `display-buffer-pop-up-frame'
5308 `display-buffer-pop-up-window'
5309 `display-buffer-use-some-window'
5311 Recognized alist entries include:
5313 `inhibit-same-window' -- A non-nil value prevents the same
5314 window from being used for display.
5316 `inhibit-switch-frame' -- A non-nil value prevents any other
5317 frame from being raised or selected,
5318 even if the window is displayed there.
5320 `reusable-frames' -- Value specifies frame(s) to search for a
5321 window that already displays the buffer.
5322 See `display-buffer-reuse-window'.
5324 `pop-up-frame-parameters' -- Value specifies an alist of frame
5325 parameters to give a new frame, if
5328 The ACTION argument to `display-buffer' can also have a non-nil
5329 and non-list value. This means to display the buffer in a window
5330 other than the selected one, even if it is already displayed in
5331 the selected window. If called interactively with a prefix
5332 argument, ACTION is t.
5334 Optional argument FRAME, if non-nil, acts like an additional
5335 ALIST entry (reusable-frames . FRAME), specifying the frame(s) to
5336 search for a window that is already displaying the buffer. See
5337 `display-buffer-reuse-window'."
5338 (interactive (list (read-buffer "Display buffer: " (other-buffer))
5339 (if current-prefix-arg t
)))
5340 (let ((buffer (if (bufferp buffer-or-name
)
5342 (get-buffer buffer-or-name
)))
5343 ;; Handle the old form of the first argument.
5344 (inhibit-same-window (and action
(not (listp action
)))))
5345 (unless (listp action
) (setq action nil
))
5346 (if display-buffer-function
5347 ;; If `display-buffer-function' is defined, let it do the job.
5348 (funcall display-buffer-function buffer inhibit-same-window
)
5349 ;; Otherwise, use the defined actions.
5351 (display-buffer-assq-regexp (buffer-name buffer
)
5352 display-buffer-alist
))
5353 (special-action (display-buffer--special-action buffer
))
5354 ;; Extra actions from the arguments to this function:
5356 (cons nil
(append (if inhibit-same-window
5357 '((inhibit-same-window . t
)))
5359 `((reusable-frames .
,frame
))))))
5360 ;; Construct action function list and action alist.
5361 (actions (list display-buffer-overriding-action
5362 user-action special-action action extra-action
5363 display-buffer-base-action
5364 display-buffer-fallback-action
))
5365 (functions (apply 'append
5368 (if (functionp x
) (list x
) x
))
5370 (alist (apply 'append
(mapcar 'cdr actions
)))
5372 (unless (buffer-live-p buffer
)
5373 (error "Invalid buffer"))
5374 (while (and functions
(not window
))
5375 (setq window
(funcall (car functions
) buffer alist
)
5376 functions
(cdr functions
)))
5379 (defun display-buffer-other-frame (buffer)
5380 "Display buffer BUFFER in another frame.
5381 This uses the function `display-buffer' as a subroutine; see
5382 its documentation for additional customization information."
5383 (interactive "BDisplay buffer in other frame: ")
5384 (display-buffer buffer display-buffer--other-frame-action t
))
5386 ;;; `display-buffer' action functions:
5388 (defun display-buffer-same-window (buffer alist
)
5389 "Display BUFFER in the selected window.
5390 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
5391 if the selected window is a minibuffer window or is dedicated to
5392 another buffer; in that case, return nil. Otherwise, return the
5394 (unless (or (cdr (assq 'inhibit-same-window alist
))
5395 (window-minibuffer-p)
5396 (window-dedicated-p))
5397 (window--display-buffer buffer
(selected-window) 'reuse
)))
5399 (defun display-buffer--maybe-same-window (buffer alist
)
5400 "Conditionally display BUFFER in the selected window.
5401 If `same-window-p' returns non-nil for BUFFER's name, call
5402 `display-buffer-same-window' and return its value. Otherwise,
5404 (and (same-window-p (buffer-name buffer
))
5405 (display-buffer-same-window buffer alist
)))
5407 (defun display-buffer-reuse-window (buffer alist
)
5408 "Return a window that is already displaying BUFFER.
5409 Return nil if no usable window is found.
5411 If ALIST has a non-nil `inhibit-same-window' entry, the selected
5412 window is not eligible for reuse.
5414 If ALIST contains a `reusable-frames' entry, its value determines
5415 which frames to search for a reusable window:
5416 nil -- the selected frame (actually the last non-minibuffer frame)
5417 A frame -- just that frame
5418 `visible' -- all visible frames
5419 0 -- all frames on the current terminal
5422 If ALIST contains no `reusable-frames' entry, search just the
5423 selected frame if `display-buffer-reuse-frames' and
5424 `pop-up-frames' are both nil; search all frames on the current
5425 terminal if either of those variables is non-nil.
5427 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5428 event that a window on another frame is chosen, avoid raising
5430 (let* ((alist-entry (assq 'reusable-frames alist
))
5431 (frames (cond (alist-entry (cdr alist-entry
))
5432 ((if (eq pop-up-frames
'graphic-only
)
5436 (display-buffer-reuse-frames 0)
5437 (t (last-nonminibuffer-frame))))
5438 (window (if (and (eq buffer
(window-buffer))
5439 (not (cdr (assq 'inhibit-same-window alist
))))
5441 (car (delq (selected-window)
5442 (get-buffer-window-list buffer
'nomini
5444 (when (window-live-p window
)
5445 (prog1 (window--display-buffer buffer window
'reuse
)
5446 (unless (cdr (assq 'inhibit-switch-frame alist
))
5447 (window--maybe-raise-frame (window-frame window
)))))))
5449 (defun display-buffer--special-action (buffer)
5450 "Return special display action for BUFFER, if any.
5451 If `special-display-p' returns non-nil for BUFFER, return an
5452 appropriate display action involving `special-display-function'.
5453 See `display-buffer' for the format of display actions."
5454 (and special-display-function
5455 ;; `special-display-p' returns either t or a list of frame
5456 ;; parameters to pass to `special-display-function'.
5457 (let ((pars (special-display-p (buffer-name buffer
))))
5459 (list (list #'display-buffer-reuse-window
5460 `(lambda (buffer _alist
)
5461 (funcall special-display-function
5462 buffer
',(if (listp pars
) pars
)))))))))
5464 (defun display-buffer-pop-up-frame (buffer alist
)
5465 "Display BUFFER in a new frame.
5466 This works by calling `pop-up-frame-function'. If successful,
5467 return the window used; otherwise return nil.
5469 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
5470 raising the new frame.
5472 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
5473 corresponding value is an alist of frame parameters to give the
5475 (let* ((params (cdr (assq 'pop-up-frame-parameters alist
)))
5476 (pop-up-frame-alist (append params pop-up-frame-alist
))
5477 (fun pop-up-frame-function
)
5480 (setq frame
(funcall fun
))
5481 (setq window
(frame-selected-window frame
)))
5482 (prog1 (window--display-buffer buffer window
5483 'frame display-buffer-mark-dedicated
)
5484 (unless (cdr (assq 'inhibit-switch-frame alist
))
5485 (window--maybe-raise-frame frame
))))))
5487 (defun display-buffer-pop-up-window (buffer alist
)
5488 "Display BUFFER by popping up a new window.
5489 The new window is created on the selected frame, or in
5490 `last-nonminibuffer-frame' if no windows can be created there.
5491 If successful, return the new window; otherwise return nil.
5493 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5494 event that the new window is created on another frame, avoid
5496 (let ((frame (or (window--frame-usable-p (selected-frame))
5497 (window--frame-usable-p (last-nonminibuffer-frame))))
5499 (when (and (or (not (frame-parameter frame
'unsplittable
))
5500 ;; If the selected frame cannot be split, look at
5501 ;; `last-nonminibuffer-frame'.
5502 (and (eq frame
(selected-frame))
5503 (setq frame
(last-nonminibuffer-frame))
5504 (window--frame-usable-p frame
)
5505 (not (frame-parameter frame
'unsplittable
))))
5506 ;; Attempt to split largest or least recently used window.
5507 (setq window
(or (window--try-to-split-window
5508 (get-largest-window frame t
))
5509 (window--try-to-split-window
5510 (get-lru-window frame t
)))))
5511 (prog1 (window--display-buffer buffer window
5512 'window display-buffer-mark-dedicated
)
5513 (unless (cdr (assq 'inhibit-switch-frame alist
))
5514 (window--maybe-raise-frame (window-frame window
)))))))
5516 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist
)
5517 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
5519 If `pop-up-frames' is non-nil (and not `graphic-only' on a
5520 text-only terminal), try with `display-buffer-pop-up-frame'.
5522 If that cannot be done, and `pop-up-windows' is non-nil, try
5523 again with `display-buffer-pop-up-window'."
5524 (or (and (if (eq pop-up-frames
'graphic-only
)
5527 (display-buffer-pop-up-frame buffer alist
))
5529 (display-buffer-pop-up-window buffer alist
))))
5531 (defun display-buffer-below-selected (buffer _alist
)
5532 "Try displaying BUFFER in a window below the selected window.
5533 This either splits the selected window or reuses the window below
5536 (or (and (not (frame-parameter nil
'unsplittable
))
5537 (setq window
(window--try-to-split-window (selected-window)))
5538 (window--display-buffer
5539 buffer window
'window display-buffer-mark-dedicated
))
5540 (and (setq window
(window-in-direction 'below
))
5541 (not (window-dedicated-p window
))
5542 (window--display-buffer
5543 buffer window
'reuse display-buffer-mark-dedicated
)))))
5545 (defun display-buffer-at-bottom (buffer _alist
)
5546 "Try displaying BUFFER in a window at the botom of the selected frame.
5547 This either splits the window at the bottom of the frame or the
5548 frame's root window, or reuses an existing window at the bottom
5549 of the selected frame."
5550 (let (bottom-window window
)
5551 (walk-window-tree (lambda (window) (setq bottom-window window
)))
5552 (or (and (not (frame-parameter nil
'unsplittable
))
5553 (setq window
(window--try-to-split-window bottom-window
))
5554 (window--display-buffer
5555 buffer window
'window display-buffer-mark-dedicated
))
5556 (and (not (frame-parameter nil
'unsplittable
))
5559 (split-window (frame-root-window))
5561 (window--display-buffer
5562 buffer window
'window display-buffer-mark-dedicated
))
5563 (and (setq window bottom-window
)
5564 (not (window-dedicated-p window
))
5565 (window--display-buffer
5566 buffer window
'reuse display-buffer-mark-dedicated
)))))
5568 (defun display-buffer-in-previous-window (buffer alist
)
5569 "Display BUFFER in a window previously showing it.
5570 If ALIST has a non-nil `inhibit-same-window' entry, the selected
5571 window is not eligible for reuse.
5573 If ALIST contains a `reusable-frames' entry, its value determines
5574 which frames to search for a reusable window:
5575 nil -- the selected frame (actually the last non-minibuffer frame)
5576 A frame -- just that frame
5577 `visible' -- all visible frames
5578 0 -- all frames on the current terminal
5581 If ALIST contains no `reusable-frames' entry, search just the
5582 selected frame if `display-buffer-reuse-frames' and
5583 `pop-up-frames' are both nil; search all frames on the current
5584 terminal if either of those variables is non-nil.
5586 If ALIST has a `previous-window' entry, the window specified by
5587 that entry will override any other window found by the methods
5588 above, even if that window never showed BUFFER before."
5589 (let* ((alist-entry (assq 'reusable-frames alist
))
5590 (inhibit-same-window
5591 (cdr (assq 'inhibit-same-window alist
)))
5593 (alist-entry (cdr alist-entry
))
5594 ((if (eq pop-up-frames
'graphic-only
)
5598 (display-buffer-reuse-frames 0)
5599 (t (last-nonminibuffer-frame))))
5600 entry best-window second-best-window window
)
5601 ;; Scan windows whether they have shown the buffer recently.
5603 (dolist (window (window-list-1 (frame-first-window) 'nomini frames
))
5604 (when (and (assq buffer
(window-prev-buffers window
))
5605 (not (window-dedicated-p window
)))
5606 (if (eq window
(selected-window))
5607 (unless inhibit-same-window
5608 (setq second-best-window window
))
5609 (setq best-window window
)
5611 ;; When ALIST has a `previous-window' entry, that entry may override
5612 ;; anything we found so far.
5613 (when (and (setq window
(cdr (assq 'previous-window alist
)))
5614 (window-live-p window
)
5615 (not (window-dedicated-p window
)))
5616 (if (eq window
(selected-window))
5617 (unless inhibit-same-window
5618 (setq second-best-window window
))
5619 (setq best-window window
)))
5620 ;; Return best or second best window found.
5621 (when (setq window
(or best-window second-best-window
))
5622 (window--display-buffer buffer window
'reuse
))))
5624 (defun display-buffer-use-some-window (buffer alist
)
5625 "Display BUFFER in an existing window.
5626 Search for a usable window, set that window to the buffer, and
5627 return the window. If no suitable window is found, return nil.
5629 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5630 event that a window in another frame is chosen, avoid raising
5632 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist
)))
5633 (frame (or (window--frame-usable-p (selected-frame))
5634 (window--frame-usable-p (last-nonminibuffer-frame))))
5636 ;; Reuse an existing window.
5637 (or (get-lru-window frame nil not-this-window
)
5638 (let ((window (get-buffer-window buffer
'visible
)))
5639 (unless (and not-this-window
5640 (eq window
(selected-window)))
5642 (get-largest-window 'visible nil not-this-window
)
5643 (let ((window (get-buffer-window buffer
0)))
5644 (unless (and not-this-window
5645 (eq window
(selected-window)))
5647 (get-largest-window 0 not-this-window
))))
5648 (when (window-live-p window
)
5650 (window--display-buffer buffer window
'reuse
)
5651 (window--even-window-heights window
)
5652 (unless (cdr (assq 'inhibit-switch-frame alist
))
5653 (window--maybe-raise-frame (window-frame window
)))))))
5655 ;;; Display + selection commands:
5656 (defun pop-to-buffer (buffer &optional action norecord
)
5657 "Select buffer BUFFER in some window, preferably a different one.
5658 BUFFER may be a buffer, a string (a buffer name), or nil. If it
5659 is a string not naming an existent buffer, create a buffer with
5660 that name. If BUFFER is nil, choose some other buffer. Return
5663 This uses `display-buffer' as a subroutine. The optional ACTION
5664 argument is passed to `display-buffer' as its ACTION argument.
5665 See `display-buffer' for more information. ACTION is t if called
5666 interactively with a prefix argument, which means to pop to a
5667 window other than the selected one even if the buffer is already
5668 displayed in the selected window.
5670 If the window to show BUFFER is not on the selected
5671 frame, raise that window's frame and give it input focus.
5673 Optional third arg NORECORD non-nil means do not put this buffer
5674 at the front of the list of recently selected ones."
5675 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
5676 (if current-prefix-arg t
)))
5677 (setq buffer
(window-normalize-buffer-to-switch-to buffer
))
5679 (let* ((old-frame (selected-frame))
5680 (window (display-buffer buffer action
))
5681 (frame (window-frame window
)))
5682 ;; If we chose another frame, make sure it gets input focus.
5683 (unless (eq frame old-frame
)
5684 (select-frame-set-input-focus frame norecord
))
5685 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
5686 (select-window window norecord
)
5689 (defun pop-to-buffer-same-window (buffer &optional norecord
)
5690 "Select buffer BUFFER in some window, preferably the same one.
5691 This function behaves much like `switch-to-buffer', except it
5692 displays with `special-display-function' if BUFFER has a match in
5693 `special-display-buffer-names' or `special-display-regexps'.
5695 Unlike `pop-to-buffer', this function prefers using the selected
5696 window over popping up a new window or frame.
5698 BUFFER may be a buffer, a string (a buffer name), or nil. If it
5699 is a string not naming an existent buffer, create a buffer with
5700 that name. If BUFFER is nil, choose some other buffer. Return
5703 NORECORD, if non-nil means do not put this buffer at the front of
5704 the list of recently selected ones."
5705 (pop-to-buffer buffer display-buffer--same-window-action norecord
))
5707 (defun read-buffer-to-switch (prompt)
5708 "Read the name of a buffer to switch to, prompting with PROMPT.
5709 Return the name of the buffer as a string.
5711 This function is intended for the `switch-to-buffer' family of
5712 commands since these need to omit the name of the current buffer
5713 from the list of completions and default values."
5714 (let ((rbts-completion-table (internal-complete-buffer-except)))
5715 (minibuffer-with-setup-hook
5717 (setq minibuffer-completion-table rbts-completion-table
)
5718 ;; Since rbts-completion-table is built dynamically, we
5719 ;; can't just add it to the default value of
5720 ;; icomplete-with-completion-tables, so we add it
5722 (if (and (boundp 'icomplete-with-completion-tables
)
5723 (listp icomplete-with-completion-tables
))
5724 (set (make-local-variable 'icomplete-with-completion-tables
)
5725 (cons rbts-completion-table
5726 icomplete-with-completion-tables
))))
5727 (read-buffer prompt
(other-buffer (current-buffer))
5728 (confirm-nonexistent-file-or-buffer)))))
5730 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
5731 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
5732 If BUFFER-OR-NAME is nil, return the buffer returned by
5733 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
5734 exists, return that buffer. If no such buffer exists, create a
5735 buffer with the name BUFFER-OR-NAME and return that buffer."
5737 (or (get-buffer buffer-or-name
)
5738 (let ((buffer (get-buffer-create buffer-or-name
)))
5739 (set-buffer-major-mode buffer
)
5743 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window
)
5744 "Switch to buffer BUFFER-OR-NAME in the selected window.
5745 If the selected window cannot display the specified
5746 buffer (e.g. if it is a minibuffer window or strongly dedicated
5747 to another buffer), call `pop-to-buffer' to select the buffer in
5750 If called interactively, read the buffer name using the
5751 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5752 determines whether to request confirmation before creating a new
5755 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
5756 If BUFFER-OR-NAME is a string that does not identify an existing
5757 buffer, create a buffer with that name. If BUFFER-OR-NAME is
5758 nil, switch to the buffer returned by `other-buffer'.
5760 If optional argument NORECORD is non-nil, do not put the buffer
5761 at the front of the buffer list, and do not make the window
5762 displaying it the most recently selected one.
5764 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
5765 must be displayed in the selected window; if that is impossible,
5766 signal an error rather than calling `pop-to-buffer'.
5768 Return the buffer switched to."
5770 (list (read-buffer-to-switch "Switch to buffer: ") nil
'force-same-window
))
5771 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name
)))
5773 ;; Don't call set-window-buffer if it's not needed since it
5774 ;; might signal an error (e.g. if the window is dedicated).
5775 ((eq buffer
(window-buffer)))
5776 ((window-minibuffer-p)
5777 (if force-same-window
5778 (user-error "Cannot switch buffers in minibuffer window")
5779 (pop-to-buffer buffer norecord
)))
5780 ((eq (window-dedicated-p) t
)
5781 (if force-same-window
5782 (user-error "Cannot switch buffers in a dedicated window")
5783 (pop-to-buffer buffer norecord
)))
5784 (t (set-window-buffer nil buffer
)))
5787 (select-window (selected-window)))
5788 (set-buffer buffer
)))
5790 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord
)
5791 "Select the buffer specified by BUFFER-OR-NAME in another window.
5792 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5793 nil. Return the buffer switched to.
5795 If called interactively, prompt for the buffer name using the
5796 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5797 determines whether to request confirmation before creating a new
5800 If BUFFER-OR-NAME is a string and does not identify an existing
5801 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5802 nil, switch to the buffer returned by `other-buffer'.
5804 Optional second argument NORECORD non-nil means do not put this
5805 buffer at the front of the list of recently selected ones.
5807 This uses the function `display-buffer' as a subroutine; see its
5808 documentation for additional customization information."
5810 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5811 (let ((pop-up-windows t
))
5812 (pop-to-buffer buffer-or-name t norecord
)))
5814 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord
)
5815 "Switch to buffer BUFFER-OR-NAME in another frame.
5816 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5817 nil. Return the buffer switched to.
5819 If called interactively, prompt for the buffer name using the
5820 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5821 determines whether to request confirmation before creating a new
5824 If BUFFER-OR-NAME is a string and does not identify an existing
5825 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5826 nil, switch to the buffer returned by `other-buffer'.
5828 Optional second arg NORECORD non-nil means do not put this
5829 buffer at the front of the list of recently selected ones.
5831 This uses the function `display-buffer' as a subroutine; see its
5832 documentation for additional customization information."
5834 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
5835 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord
))
5837 (defun set-window-text-height (window height
)
5838 "Set the height in lines of the text display area of WINDOW to HEIGHT.
5839 WINDOW must be a live window and defaults to the selected one.
5840 HEIGHT doesn't include the mode line or header line, if any, or
5841 any partial-height lines in the text display area.
5843 Note that the current implementation of this function cannot
5844 always set the height exactly, but attempts to be conservative,
5845 by allocating more lines than are actually needed in the case
5846 where some error may be present."
5847 (setq window
(window-normalize-window window t
))
5848 (let ((delta (- height
(window-text-height window
))))
5849 (unless (zerop delta
)
5850 ;; Setting window-min-height to a value like 1 can lead to very
5851 ;; bizarre displays because it also allows Emacs to make *other*
5852 ;; windows one line tall, which means that there's no more space
5853 ;; for the mode line.
5854 (let ((window-min-height (min 2 height
)))
5855 (window-resize window delta
)))))
5857 (defun enlarge-window-horizontally (delta)
5858 "Make selected window DELTA columns wider.
5859 Interactively, if no argument is given, make selected window one
5862 (enlarge-window delta t
))
5864 (defun shrink-window-horizontally (delta)
5865 "Make selected window DELTA columns narrower.
5866 Interactively, if no argument is given, make selected window one
5869 (shrink-window delta t
))
5871 (defun count-screen-lines (&optional beg end count-final-newline window
)
5872 "Return the number of screen lines in the region.
5873 The number of screen lines may be different from the number of actual lines,
5874 due to line breaking, display table, etc.
5876 Optional arguments BEG and END default to `point-min' and `point-max'
5879 If region ends with a newline, ignore it unless optional third argument
5880 COUNT-FINAL-NEWLINE is non-nil.
5882 The optional fourth argument WINDOW specifies the window used for obtaining
5883 parameters such as width, horizontal scrolling, and so on. The default is
5884 to use the selected window's parameters.
5886 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
5887 regardless of which buffer is displayed in WINDOW. This makes possible to use
5888 `count-screen-lines' in any buffer, whether or not it is currently displayed
5891 (setq beg
(point-min)))
5893 (setq end
(point-max)))
5899 (narrow-to-region (min beg end
)
5900 (if (and (not count-final-newline
)
5901 (= ?
\n (char-before (max beg end
))))
5904 (goto-char (point-min))
5905 (1+ (vertical-motion (buffer-size) window
))))))
5907 (defun window-buffer-height (window)
5908 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
5909 WINDOW must be a live window and defaults to the selected one."
5910 (setq window
(window-normalize-window window t
))
5911 (with-current-buffer (window-buffer window
)
5913 (count-screen-lines (point-min) (point-max)
5914 ;; If buffer ends with a newline, ignore it when
5915 ;; counting height unless point is after it.
5919 ;;; Resizing buffers to fit their contents exactly.
5920 (defun fit-window-to-buffer (&optional window max-height min-height
)
5921 "Adjust height of WINDOW to display its buffer's contents exactly.
5922 WINDOW must be a live window and defaults to the selected one.
5924 Optional argument MAX-HEIGHT specifies the maximum height of
5925 WINDOW and defaults to the height of WINDOW's frame. Optional
5926 argument MIN-HEIGHT specifies the minimum height of WINDOW and
5927 defaults to `window-min-height'. Both MAX-HEIGHT and MIN-HEIGHT
5928 are specified in lines and include the mode line and header line,
5931 Return the number of lines by which WINDOW was enlarged or
5932 shrunk. If an error occurs during resizing, return nil but don't
5935 Note that even if this function makes WINDOW large enough to show
5936 _all_ lines of its buffer you might not see the first lines when
5937 WINDOW was scrolled."
5939 (setq window
(window-normalize-window window t
))
5940 ;; Can't resize a full height or fixed-size window.
5941 (unless (or (window-size-fixed-p window
)
5942 (window-full-height-p window
))
5943 (with-selected-window window
5944 (let* ((height (window-total-size))
5946 ;; Adjust MIN-HEIGHT.
5947 (if (numberp min-height
)
5948 ;; Can't get smaller than `window-safe-min-height'.
5949 (max min-height window-safe-min-height
)
5950 ;; Preserve header and mode line if present.
5951 (window-min-size nil nil t
)))
5953 ;; Adjust MAX-HEIGHT.
5954 (if (numberp max-height
)
5955 ;; Can't get larger than height of frame.
5957 (window-total-size (frame-root-window window
)))
5958 ;, Don't delete other windows.
5959 (+ height
(window-max-delta nil nil window
))))
5960 ;; Make `desired-height' the height necessary to show
5961 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
5966 (+ (count-screen-lines)
5967 ;; For non-minibuffers count the mode line, if any.
5968 (if (and (not (window-minibuffer-p window
))
5972 ;; Count the header line, if any.
5973 (if header-line-format
1 0))
5977 (- desired-height
(window-total-size window
)))
5979 (if (> desired-delta
0)
5981 (window-max-delta window nil window
))
5983 (- (window-min-delta window nil window
))))))
5986 ;; Return zero if DELTA became zero in the process.
5988 ;; Don't try to redisplay with the cursor at the end on its
5989 ;; own line--that would force a scroll and spoil things.
5990 (when (and (eobp) (bolp) (not (bobp)))
5991 ;; It's silly to put `point' at the end of the previous
5992 ;; line and so maybe force horizontal scrolling.
5993 (set-window-point window
(line-beginning-position 0)))
5994 ;; Call `window-resize' with OVERRIDE argument equal WINDOW.
5995 (window-resize window delta nil window
)
5996 ;; Check if the last line is surely fully visible. If
5997 ;; not, enlarge the window.
5998 (let ((end (save-excursion
5999 (goto-char (point-max))
6000 (when (and (bolp) (not (bobp)))
6001 ;; Don't include final newline.
6003 (when truncate-lines
6004 ;; If line-wrapping is turned off, test the
6005 ;; beginning of the last line for
6006 ;; visibility instead of the end, as the
6007 ;; end of the line could be invisible by
6008 ;; virtue of extending past the edge of the
6012 (set-window-vscroll window
0)
6013 ;; This loop might in some rare pathological cases raise
6014 ;; an error - another reason for the `condition-case'.
6015 (while (and (< desired-height max-height
)
6016 (= desired-height
(window-total-size))
6017 (not (pos-visible-in-window-p end
)))
6018 (window-resize window
1 nil window
)
6019 (setq desired-height
(1+ desired-height
)))))
6020 (error (setq delta nil
)))
6023 (defcustom fit-frame-to-buffer-bottom-margin
4
6024 "Bottom margin for `fit-frame-to-buffer'.
6025 This is the number of lines `fit-frame-to-buffer' leaves free at the
6026 bottom of the display in order to not obscure the system task bar."
6031 (defun fit-frame-to-buffer (&optional frame max-height min-height
)
6032 "Adjust height of FRAME to display its buffer's contents exactly.
6033 FRAME can be any live frame and defaults to the selected one.
6035 Optional argument MAX-HEIGHT specifies the maximum height of
6036 FRAME and defaults to the height of the display below the current
6037 top line of FRAME minus FIT-FRAME-TO-BUFFER-BOTTOM-MARGIN.
6038 Optional argument MIN-HEIGHT specifies the minimum height of
6041 (setq frame
(window-normalize-frame frame
))
6042 (let* ((root (frame-root-window frame
))
6044 (+ (- (frame-height frame
) (window-total-size root
))
6046 (frame-top (frame-parameter frame
'top
))
6047 (top (if (consp frame-top
)
6048 (funcall (car frame-top
) (cadr frame-top
))
6051 (- (/ (- (x-display-pixel-height frame
) top
)
6052 (frame-char-height frame
))
6053 fit-frame-to-buffer-bottom-margin
))
6056 (when (and (window-live-p root
) (not (window-size-fixed-p root
)))
6057 (with-selected-window root
6060 (setq max-height frame-max-height
))
6061 ((numberp max-height
)
6062 (setq max-height
(min max-height frame-max-height
)))
6064 (error "%s is an invalid maximum height" max-height
)))
6067 (setq min-height frame-min-height
))
6068 ((numberp min-height
)
6069 (setq min-height
(min min-height frame-min-height
)))
6071 (error "%s is an invalid minimum height" min-height
)))
6072 ;; When tool-bar-mode is enabled and we have just created a new
6073 ;; frame, reserve lines for toolbar resizing. This is needed
6074 ;; because for reasons unknown to me Emacs (1) reserves one line
6075 ;; for the toolbar when making the initial frame and toolbars
6076 ;; are enabled, and (2) later adds the remaining lines needed.
6077 ;; Our code runs IN BETWEEN (1) and (2). YMMV when you're on a
6078 ;; system that behaves differently.
6079 (let ((quit-restore (window-parameter root
'quit-restore
))
6080 (lines (tool-bar-lines-needed frame
)))
6081 (when (and quit-restore
(eq (car quit-restore
) 'frame
)
6082 (not (zerop lines
)))
6083 (setq compensate
(1- lines
))))
6084 (message "%s" compensate
)
6086 ;; Always count a final newline - we don't do any
6087 ;; post-processing, so let's play safe.
6088 (+ (count-screen-lines nil nil t
)
6089 (- (window-body-size))
6091 ;; Move away from final newline.
6092 (when (and (eobp) (bolp) (not (bobp)))
6093 (set-window-point root
(line-beginning-position 0)))
6094 (set-window-start root
(point-min))
6095 (set-window-vscroll root
0)
6099 (min (max (+ (frame-height frame
) delta
)
6102 (error (setq delta nil
))))
6105 (defun window-safely-shrinkable-p (&optional window
)
6106 "Return t if WINDOW can be shrunk without shrinking other windows.
6107 WINDOW defaults to the selected window."
6108 (with-selected-window (or window
(selected-window))
6109 (let ((edges (window-edges)))
6110 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
6111 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
6113 (defun shrink-window-if-larger-than-buffer (&optional window
)
6114 "Shrink height of WINDOW if its buffer doesn't need so many lines.
6115 More precisely, shrink WINDOW vertically to be as small as
6116 possible, while still showing the full contents of its buffer.
6117 WINDOW must be a live window and defaults to the selected one.
6119 Do not shrink WINDOW to less than `window-min-height' lines. Do
6120 nothing if the buffer contains more lines than the present window
6121 height, or if some of the window's contents are scrolled out of
6122 view, or if shrinking this window would also shrink another
6123 window, or if the window is the only window of its frame.
6125 Return non-nil if the window was shrunk, nil otherwise."
6127 (setq window
(window-normalize-window window t
))
6128 ;; Make sure that WINDOW is vertically combined and `point-min' is
6129 ;; visible (for whatever reason that's needed). The remaining issues
6130 ;; should be taken care of by `fit-window-to-buffer'.
6131 (when (and (window-combined-p window
)
6132 (pos-visible-in-window-p (point-min) window
))
6133 (fit-window-to-buffer window
(window-total-size window
))))
6135 (defun kill-buffer-and-window ()
6136 "Kill the current buffer and delete the selected window."
6138 (let ((window-to-delete (selected-window))
6139 (buffer-to-kill (current-buffer))
6140 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
6143 (add-hook 'kill-buffer-hook delete-window-hook t t
)
6144 (if (kill-buffer (current-buffer))
6145 ;; If `delete-window' failed before, we rerun it to regenerate
6146 ;; the error so it can be seen in the echo area.
6147 (when (eq (selected-window) window-to-delete
)
6149 ;; If the buffer is not dead for some reason (probably because
6150 ;; of a `quit' signal), remove the hook again.
6152 (with-current-buffer buffer-to-kill
6153 (remove-hook 'kill-buffer-hook delete-window-hook t
))))))
6156 (defvar recenter-last-op nil
6157 "Indicates the last recenter operation performed.
6158 Possible values: `top', `middle', `bottom', integer or float numbers.")
6160 (defcustom recenter-positions
'(middle top bottom
)
6161 "Cycling order for `recenter-top-bottom'.
6162 A list of elements with possible values `top', `middle', `bottom',
6163 integer or float numbers that define the cycling order for
6164 the command `recenter-top-bottom'.
6166 Top and bottom destinations are `scroll-margin' lines from the true
6167 window top and bottom. Middle redraws the frame and centers point
6168 vertically within the window. Integer number moves current line to
6169 the specified absolute window-line. Float number between 0.0 and 1.0
6170 means the percentage of the screen space from the top. The default
6171 cycling order is middle -> top -> bottom."
6172 :type
'(repeat (choice
6173 (const :tag
"Top" top
)
6174 (const :tag
"Middle" middle
)
6175 (const :tag
"Bottom" bottom
)
6176 (integer :tag
"Line number")
6177 (float :tag
"Percentage")))
6181 (defun recenter-top-bottom (&optional arg
)
6182 "Move current buffer line to the specified window line.
6183 With no prefix argument, successive calls place point according
6184 to the cycling order defined by `recenter-positions'.
6186 A prefix argument is handled like `recenter':
6187 With numeric prefix ARG, move current line to window-line ARG.
6188 With plain `C-u', move current line to window center."
6191 (arg (recenter arg
)) ; Always respect ARG.
6193 (setq recenter-last-op
6194 (if (eq this-command last-command
)
6195 (car (or (cdr (member recenter-last-op recenter-positions
))
6196 recenter-positions
))
6197 (car recenter-positions
)))
6198 (let ((this-scroll-margin
6199 (min (max 0 scroll-margin
)
6200 (truncate (/ (window-body-height) 4.0)))))
6201 (cond ((eq recenter-last-op
'middle
)
6203 ((eq recenter-last-op
'top
)
6204 (recenter this-scroll-margin
))
6205 ((eq recenter-last-op
'bottom
)
6206 (recenter (- -
1 this-scroll-margin
)))
6207 ((integerp recenter-last-op
)
6208 (recenter recenter-last-op
))
6209 ((floatp recenter-last-op
)
6210 (recenter (round (* recenter-last-op
(window-height))))))))))
6212 (define-key global-map
[?\C-l
] 'recenter-top-bottom
)
6214 (defun move-to-window-line-top-bottom (&optional arg
)
6215 "Position point relative to window.
6217 With a prefix argument ARG, acts like `move-to-window-line'.
6219 With no argument, positions point at center of window.
6220 Successive calls position point at positions defined
6221 by `recenter-positions'."
6224 (arg (move-to-window-line arg
)) ; Always respect ARG.
6226 (setq recenter-last-op
6227 (if (eq this-command last-command
)
6228 (car (or (cdr (member recenter-last-op recenter-positions
))
6229 recenter-positions
))
6230 (car recenter-positions
)))
6231 (let ((this-scroll-margin
6232 (min (max 0 scroll-margin
)
6233 (truncate (/ (window-body-height) 4.0)))))
6234 (cond ((eq recenter-last-op
'middle
)
6235 (call-interactively 'move-to-window-line
))
6236 ((eq recenter-last-op
'top
)
6237 (move-to-window-line this-scroll-margin
))
6238 ((eq recenter-last-op
'bottom
)
6239 (move-to-window-line (- -
1 this-scroll-margin
)))
6240 ((integerp recenter-last-op
)
6241 (move-to-window-line recenter-last-op
))
6242 ((floatp recenter-last-op
)
6243 (move-to-window-line (round (* recenter-last-op
(window-height))))))))))
6245 (define-key global-map
[?\M-r
] 'move-to-window-line-top-bottom
)
6247 ;;; Scrolling commands.
6249 ;;; Scrolling commands which do not signal errors at top/bottom
6250 ;;; of buffer at first key-press (instead move to top/bottom
6253 (defcustom scroll-error-top-bottom nil
6254 "Move point to top/bottom of buffer before signaling a scrolling error.
6255 A value of nil means just signal an error if no more scrolling possible.
6256 A value of t means point moves to the beginning or the end of the buffer
6257 \(depending on scrolling direction) when no more scrolling possible.
6258 When point is already on that position, then signal an error."
6263 (defun scroll-up-command (&optional arg
)
6264 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
6265 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
6266 scroll window further, move cursor to the bottom line.
6267 When point is already on that position, then signal an error.
6268 A near full screen is `next-screen-context-lines' less than a full screen.
6269 Negative ARG means scroll downward.
6270 If ARG is the atom `-', scroll downward by nearly full screen."
6273 ((null scroll-error-top-bottom
)
6276 (scroll-down-command nil
))
6277 ((< (prefix-numeric-value arg
) 0)
6278 (scroll-down-command (- (prefix-numeric-value arg
))))
6280 (scroll-up arg
)) ; signal error
6286 ;; When scrolling by ARG lines can't be done,
6287 ;; move by ARG lines instead.
6289 ;; When ARG is nil for full-screen scrolling,
6290 ;; move to the bottom of the buffer.
6291 (goto-char (point-max))))))))
6293 (put 'scroll-up-command
'scroll-command t
)
6295 (defun scroll-down-command (&optional arg
)
6296 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
6297 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
6298 scroll window further, move cursor to the top line.
6299 When point is already on that position, then signal an error.
6300 A near full screen is `next-screen-context-lines' less than a full screen.
6301 Negative ARG means scroll upward.
6302 If ARG is the atom `-', scroll upward by nearly full screen."
6305 ((null scroll-error-top-bottom
)
6308 (scroll-up-command nil
))
6309 ((< (prefix-numeric-value arg
) 0)
6310 (scroll-up-command (- (prefix-numeric-value arg
))))
6312 (scroll-down arg
)) ; signal error
6316 (beginning-of-buffer
6318 ;; When scrolling by ARG lines can't be done,
6319 ;; move by ARG lines instead.
6320 (forward-line (- arg
))
6321 ;; When ARG is nil for full-screen scrolling,
6322 ;; move to the top of the buffer.
6323 (goto-char (point-min))))))))
6325 (put 'scroll-down-command
'scroll-command t
)
6327 ;;; Scrolling commands which scroll a line instead of full screen.
6329 (defun scroll-up-line (&optional arg
)
6330 "Scroll text of selected window upward ARG lines; or one line if no ARG.
6331 If ARG is omitted or nil, scroll upward by one line.
6332 This is different from `scroll-up-command' that scrolls a full screen."
6334 (scroll-up (or arg
1)))
6336 (put 'scroll-up-line
'scroll-command t
)
6338 (defun scroll-down-line (&optional arg
)
6339 "Scroll text of selected window down ARG lines; or one line if no ARG.
6340 If ARG is omitted or nil, scroll down by one line.
6341 This is different from `scroll-down-command' that scrolls a full screen."
6343 (scroll-down (or arg
1)))
6345 (put 'scroll-down-line
'scroll-command t
)
6348 (defun scroll-other-window-down (&optional lines
)
6349 "Scroll the \"other window\" down.
6350 For more details, see the documentation for `scroll-other-window'."
6352 (scroll-other-window
6353 ;; Just invert the argument's meaning.
6354 ;; We can do that without knowing which window it will be.
6355 (if (eq lines
'-
) nil
6357 (- (prefix-numeric-value lines
))))))
6359 (defun beginning-of-buffer-other-window (arg)
6360 "Move point to the beginning of the buffer in the other window.
6361 Leave mark at previous position.
6362 With arg N, put point N/10 of the way from the true beginning."
6364 (let ((orig-window (selected-window))
6365 (window (other-window-for-scrolling)))
6366 ;; We use unwind-protect rather than save-window-excursion
6367 ;; because the latter would preserve the things we want to change.
6370 (select-window window
)
6371 ;; Set point and mark in that window's buffer.
6373 (beginning-of-buffer arg
))
6374 ;; Set point accordingly.
6376 (select-window orig-window
))))
6378 (defun end-of-buffer-other-window (arg)
6379 "Move point to the end of the buffer in the other window.
6380 Leave mark at previous position.
6381 With arg N, put point N/10 of the way from the true end."
6383 ;; See beginning-of-buffer-other-window for comments.
6384 (let ((orig-window (selected-window))
6385 (window (other-window-for-scrolling)))
6388 (select-window window
)
6390 (end-of-buffer arg
))
6392 (select-window orig-window
))))
6394 (defvar mouse-autoselect-window-timer nil
6395 "Timer used by delayed window autoselection.")
6397 (defvar mouse-autoselect-window-position nil
6398 "Last mouse position recorded by delayed window autoselection.")
6400 (defvar mouse-autoselect-window-window nil
6401 "Last window recorded by delayed window autoselection.")
6403 (defvar mouse-autoselect-window-state nil
6404 "When non-nil, special state of delayed window autoselection.
6405 Possible values are `suspend' (suspend autoselection after a menu or
6406 scrollbar interaction) and `select' (the next invocation of
6407 `handle-select-window' shall select the window immediately).")
6409 (defun mouse-autoselect-window-cancel (&optional force
)
6410 "Cancel delayed window autoselection.
6411 Optional argument FORCE means cancel unconditionally."
6412 (unless (and (not force
)
6413 ;; Don't cancel for select-window or select-frame events
6414 ;; or when the user drags a scroll bar.
6415 (or (memq this-command
6416 '(handle-select-window handle-switch-frame
))
6417 (and (eq this-command
'scroll-bar-toolkit-scroll
)
6418 (memq (nth 4 (event-end last-input-event
))
6419 '(handle end-scroll
)))))
6420 (setq mouse-autoselect-window-state nil
)
6421 (when (timerp mouse-autoselect-window-timer
)
6422 (cancel-timer mouse-autoselect-window-timer
))
6423 (remove-hook 'pre-command-hook
'mouse-autoselect-window-cancel
)))
6425 (defun mouse-autoselect-window-start (mouse-position &optional window suspend
)
6426 "Start delayed window autoselection.
6427 MOUSE-POSITION is the last position where the mouse was seen as returned
6428 by `mouse-position'. Optional argument WINDOW non-nil denotes the
6429 window where the mouse was seen. Optional argument SUSPEND non-nil
6430 means suspend autoselection."
6431 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
6432 (setq mouse-autoselect-window-position mouse-position
)
6433 (when window
(setq mouse-autoselect-window-window window
))
6434 (setq mouse-autoselect-window-state
(when suspend
'suspend
))
6435 ;; Install timer which runs `mouse-autoselect-window-select' after
6436 ;; `mouse-autoselect-window' seconds.
6437 (setq mouse-autoselect-window-timer
6439 (abs mouse-autoselect-window
) nil
'mouse-autoselect-window-select
)))
6441 (defun mouse-autoselect-window-select ()
6442 "Select window with delayed window autoselection.
6443 If the mouse position has stabilized in a non-selected window, select
6444 that window. The minibuffer window is selected only if the minibuffer
6445 is active. This function is run by `mouse-autoselect-window-timer'."
6447 (let* ((mouse-position (mouse-position))
6450 (window-at (cadr mouse-position
) (cddr mouse-position
)
6451 (car mouse-position
)))))
6453 ((or (menu-or-popup-active-p)
6455 (not (coordinates-in-window-p (cdr mouse-position
) window
))))
6456 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
6457 ;; of WINDOW, temporarily suspend delayed autoselection.
6458 (mouse-autoselect-window-start mouse-position nil t
))
6459 ((eq mouse-autoselect-window-state
'suspend
)
6460 ;; Delayed autoselection was temporarily suspended, reenable it.
6461 (mouse-autoselect-window-start mouse-position
))
6462 ((and window
(not (eq window
(selected-window)))
6463 (or (not (numberp mouse-autoselect-window
))
6464 (and (> mouse-autoselect-window
0)
6465 ;; If `mouse-autoselect-window' is positive, select
6466 ;; window if the window is the same as before.
6467 (eq window mouse-autoselect-window-window
))
6468 ;; Otherwise select window if the mouse is at the same
6469 ;; position as before. Observe that the first test after
6470 ;; starting autoselection usually fails since the value of
6471 ;; `mouse-autoselect-window-position' recorded there is the
6472 ;; position where the mouse has entered the new window and
6473 ;; not necessarily where the mouse has stopped moving.
6474 (equal mouse-position mouse-autoselect-window-position
))
6475 ;; The minibuffer is a candidate window if it's active.
6476 (or (not (window-minibuffer-p window
))
6477 (eq window
(active-minibuffer-window))))
6478 ;; Mouse position has stabilized in non-selected window: Cancel
6479 ;; delayed autoselection and try to select that window.
6480 (mouse-autoselect-window-cancel t
)
6481 ;; Select window where mouse appears unless the selected window is the
6482 ;; minibuffer. Use `unread-command-events' in order to execute pre-
6483 ;; and post-command hooks and trigger idle timers. To avoid delaying
6484 ;; autoselection again, set `mouse-autoselect-window-state'."
6485 (unless (window-minibuffer-p (selected-window))
6486 (setq mouse-autoselect-window-state
'select
)
6487 (setq unread-command-events
6488 (cons (list 'select-window
(list window
))
6489 unread-command-events
))))
6490 ((or (and window
(eq window
(selected-window)))
6491 (not (numberp mouse-autoselect-window
))
6492 (equal mouse-position mouse-autoselect-window-position
))
6493 ;; Mouse position has either stabilized in the selected window or at
6494 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
6495 (mouse-autoselect-window-cancel t
))
6497 ;; Mouse position has not stabilized yet, resume delayed
6499 (mouse-autoselect-window-start mouse-position window
))))))
6501 (defun handle-select-window (event)
6502 "Handle select-window events."
6504 (let ((window (posn-window (event-start event
))))
6505 (unless (or (not (window-live-p window
))
6506 ;; Don't switch if we're currently in the minibuffer.
6507 ;; This tries to work around problems where the
6508 ;; minibuffer gets unselected unexpectedly, and where
6509 ;; you then have to move your mouse all the way down to
6510 ;; the minibuffer to select it.
6511 (window-minibuffer-p (selected-window))
6512 ;; Don't switch to minibuffer window unless it's active.
6513 (and (window-minibuffer-p window
)
6514 (not (minibuffer-window-active-p window
)))
6515 ;; Don't switch when autoselection shall be delayed.
6516 (and (numberp mouse-autoselect-window
)
6517 (not (zerop mouse-autoselect-window
))
6518 (not (eq mouse-autoselect-window-state
'select
))
6520 ;; Cancel any delayed autoselection.
6521 (mouse-autoselect-window-cancel t
)
6522 ;; Start delayed autoselection from current mouse
6523 ;; position and window.
6524 (mouse-autoselect-window-start (mouse-position) window
)
6525 ;; Executing a command cancels delayed autoselection.
6527 'pre-command-hook
'mouse-autoselect-window-cancel
))))
6528 (when mouse-autoselect-window
6529 ;; Reset state of delayed autoselection.
6530 (setq mouse-autoselect-window-state nil
)
6531 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
6532 (run-hooks 'mouse-leave-buffer-hook
))
6535 (select-window window
))))
6537 (defun truncated-partial-width-window-p (&optional window
)
6538 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
6539 WINDOW must be a live window and defaults to the selected one.
6540 Return nil if WINDOW is not a partial-width window
6541 (regardless of the value of `truncate-lines').
6542 Otherwise, consult the value of `truncate-partial-width-windows'
6543 for the buffer shown in WINDOW."
6544 (setq window
(window-normalize-window window t
))
6545 (unless (window-full-width-p window
)
6546 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
6547 (window-buffer window
))))
6548 (if (integerp t-p-w-w
)
6549 (< (window-width window
) t-p-w-w
)
6552 ;; Some of these are in tutorial--default-keys, so update that if you
6554 (define-key ctl-x-map
"0" 'delete-window
)
6555 (define-key ctl-x-map
"1" 'delete-other-windows
)
6556 (define-key ctl-x-map
"2" 'split-window-below
)
6557 (define-key ctl-x-map
"3" 'split-window-right
)
6558 (define-key ctl-x-map
"o" 'other-window
)
6559 (define-key ctl-x-map
"^" 'enlarge-window
)
6560 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
6561 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
6562 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
6563 (define-key ctl-x-map
"+" 'balance-windows
)
6564 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
6566 ;;; window.el ends here