1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2013 Free Software
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Window tree functions.
31 (defun internal--before-save-selected-window ()
32 (cons (selected-window)
33 ;; We save and restore all frames' selected windows, because
34 ;; `select-window' can change the frame-selected-window of
35 ;; whatever frame that window is in. Each text terminal's
36 ;; top-frame is preserved by putting it last in the list.
38 (mapcar (lambda (terminal)
39 (let ((frames (frames-on-display-list terminal
))
40 (top-frame (tty-top-frame terminal
))
45 (delq top-frame frames
))))
47 (push (cons f
(frame-selected-window f
))
52 (defun internal--after-save-selected-window (state)
53 (dolist (elt (cdr state
))
54 (and (frame-live-p (car elt
))
55 (window-live-p (cdr elt
))
56 (set-frame-selected-window (car elt
) (cdr elt
) 'norecord
)))
57 (when (window-live-p (car state
))
58 (select-window (car state
) 'norecord
)))
60 (defmacro save-selected-window
(&rest body
)
61 "Execute BODY, then select the previously selected window.
62 The value returned is the value of the last form in BODY.
64 This macro saves and restores the selected window, as well as the
65 selected window in each frame. If the previously selected window
66 is no longer live, then whatever window is selected at the end of
67 BODY remains selected. If the previously selected window of some
68 frame is no longer live at the end of BODY, that frame's selected
71 This macro saves and restores the current buffer, since otherwise
72 its normal operation could make a different buffer current. The
73 order of recently selected windows and the buffer list ordering
74 are not altered by this macro (unless they are altered in BODY)."
75 (declare (indent 0) (debug t
))
76 `(let ((save-selected-window--state (internal--before-save-selected-window)))
80 (internal--after-save-selected-window save-selected-window--state
)))))
82 (defvar temp-buffer-window-setup-hook nil
83 "Normal hook run by `with-temp-buffer-window' before buffer display.
84 This hook is run by `with-temp-buffer-window' with the buffer to be
87 (defvar temp-buffer-window-show-hook nil
88 "Normal hook run by `with-temp-buffer-window' after buffer display.
89 This hook is run by `with-temp-buffer-window' with the buffer
90 displayed and current and its window selected.")
92 (defun temp-buffer-window-setup (buffer-or-name)
93 "Set up temporary buffer specified by BUFFER-OR-NAME.
95 (let ((old-dir default-directory
)
96 (buffer (get-buffer-create buffer-or-name
)))
97 (with-current-buffer buffer
98 (kill-all-local-variables)
99 (setq default-directory old-dir
)
100 (delete-all-overlays)
101 (setq buffer-read-only nil
)
102 (setq buffer-file-name nil
)
103 (setq buffer-undo-list t
)
104 (let ((inhibit-read-only t
)
105 (inhibit-modification-hooks t
))
107 (run-hooks 'temp-buffer-window-setup-hook
))
108 ;; Return the buffer.
111 (defun temp-buffer-window-show (&optional buffer action
)
112 "Show temporary buffer BUFFER in a window.
113 Return the window showing BUFFER. Pass ACTION as action argument
114 to `display-buffer'."
116 (with-current-buffer buffer
117 (set-buffer-modified-p nil
)
118 (setq buffer-read-only t
)
119 (goto-char (point-min))
120 (when (let ((window-combination-limit
121 ;; When `window-combination-limit' equals
122 ;; `temp-buffer' or `temp-buffer-resize' and
123 ;; `temp-buffer-resize-mode' is enabled in this
124 ;; buffer bind it to t so resizing steals space
125 ;; preferably from the window that was split.
126 (if (or (eq window-combination-limit
'temp-buffer
)
127 (and (eq window-combination-limit
129 temp-buffer-resize-mode
))
131 window-combination-limit
)))
132 (setq window
(display-buffer buffer action
)))
133 (setq frame
(window-frame window
))
134 (unless (eq frame
(selected-frame))
136 (setq minibuffer-scroll-window window
)
137 (set-window-hscroll window
0)
138 (with-selected-window window
139 (run-hooks 'temp-buffer-window-show-hook
)
140 (when temp-buffer-resize-mode
141 (resize-temp-buffer-window window
)))
142 ;; Return the window.
145 ;; Doc is very similar to with-output-to-temp-buffer.
146 (defmacro with-temp-buffer-window
(buffer-or-name action quit-function
&rest body
)
147 "Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
148 BUFFER-OR-NAME must specify either a live buffer, or the name of a
149 buffer (if it does not exist, this macro creates it).
151 This construct makes buffer BUFFER-OR-NAME empty before running BODY.
152 It does not make the buffer current for BODY.
153 Instead it binds `standard-output' to that buffer, so that output
154 generated with `prin1' and similar functions in BODY goes into
157 At the end of BODY, this marks the specified buffer unmodified and
158 read-only, and displays it in a window (but does not select it, or make
159 the buffer current). The display happens by calling `display-buffer'
160 with the ACTION argument. If `temp-buffer-resize-mode' is enabled,
161 the relevant window shrinks automatically.
163 This returns the value returned by BODY, unless QUIT-FUNCTION specifies
164 a function. In that case, it runs the function with two arguments -
165 the window showing the specified buffer and the value returned by
166 BODY - and returns the value returned by that function.
168 If the buffer is displayed on a new frame, the window manager may
169 decide to select that frame. In that case, it's usually a good
170 strategy if QUIT-FUNCTION selects the window showing the buffer
171 before reading any value from the minibuffer; for example, when
172 asking a `yes-or-no-p' question.
174 This runs the hook `temp-buffer-window-setup-hook' before BODY,
175 with the specified buffer temporarily current. It runs the
176 hook `temp-buffer-window-show-hook' after displaying the buffer,
177 with that buffer temporarily current, and the window that was used to
178 display it temporarily selected.
180 This construct is similar to `with-output-to-temp-buffer', but
181 runs different hooks. In particular, it does not run
182 `temp-buffer-setup-hook', which usually puts the buffer in Help mode.
183 Also, it does not call `temp-buffer-show-function' (the ACTION
184 argument replaces this)."
186 (let ((buffer (make-symbol "buffer"))
187 (window (make-symbol "window"))
188 (value (make-symbol "value")))
189 `(let* ((,buffer
(temp-buffer-window-setup ,buffer-or-name
))
190 (standard-output ,buffer
)
192 (with-current-buffer ,buffer
193 (setq ,value
(progn ,@body
))
194 (setq ,window
(temp-buffer-window-show ,buffer
,action
)))
196 (if (functionp ,quit-function
)
197 (funcall ,quit-function
,window
,value
)
200 ;; The following two functions are like `window-next-sibling' and
201 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
202 ;; they don't substitute the selected window for nil), and they return
203 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
204 ;; a minibuffer window).
205 (defun window-right (window)
206 "Return WINDOW's right sibling.
207 Return nil if WINDOW is the root window of its frame. WINDOW can
209 (and window
(window-parent window
) (window-next-sibling window
)))
211 (defun window-left (window)
212 "Return WINDOW's left sibling.
213 Return nil if WINDOW is the root window of its frame. WINDOW can
215 (and window
(window-parent window
) (window-prev-sibling window
)))
217 (defun window-child (window)
218 "Return WINDOW's first child window.
219 WINDOW can be any window."
220 (or (window-top-child window
) (window-left-child window
)))
222 (defun window-child-count (window)
223 "Return number of WINDOW's child windows.
224 WINDOW can be any window."
226 (when (and (windowp window
) (setq window
(window-child window
)))
228 (setq count
(1+ count
))
229 (setq window
(window-next-sibling window
))))
232 (defun window-last-child (window)
233 "Return last child window of WINDOW.
234 WINDOW can be any window."
235 (when (and (windowp window
) (setq window
(window-child window
)))
236 (while (window-next-sibling window
)
237 (setq window
(window-next-sibling window
))))
240 (defun window-normalize-buffer (buffer-or-name)
241 "Return buffer specified by BUFFER-OR-NAME.
242 BUFFER-OR-NAME must be either a buffer or a string naming a live
243 buffer and defaults to the current buffer."
245 ((not buffer-or-name
)
247 ((bufferp buffer-or-name
)
248 (if (buffer-live-p buffer-or-name
)
250 (error "Buffer %s is not a live buffer" buffer-or-name
)))
251 ((get-buffer buffer-or-name
))
253 (error "No such buffer %s" buffer-or-name
))))
255 (defun window-normalize-frame (frame)
256 "Return frame specified by FRAME.
257 FRAME must be a live frame and defaults to the selected frame."
259 (if (frame-live-p frame
)
261 (error "%s is not a live frame" frame
))
264 (defun window-normalize-window (window &optional live-only
)
265 "Return the window specified by WINDOW.
266 If WINDOW is nil, return the selected window. Otherwise, if
267 WINDOW is a live or an internal window, return WINDOW; if
268 LIVE-ONLY is non-nil, return WINDOW for a live window only.
269 Otherwise, signal an error."
274 (if (window-live-p window
)
276 (error "%s is not a live window" window
)))
277 ((window-valid-p window
)
280 (error "%s is not a valid window" window
))))
282 (defvar ignore-window-parameters nil
283 "If non-nil, standard functions ignore window parameters.
284 The functions currently affected by this are `split-window',
285 `delete-window', `delete-other-windows' and `other-window'.
287 An application may bind this to a non-nil value around calls to
288 these functions to inhibit processing of window parameters.")
290 (defconst window-safe-min-height
1
291 "The absolute minimum number of lines of a window.
292 Anything less might crash Emacs.")
294 (defcustom window-min-height
4
295 "The minimum number of lines of any window.
296 The value has to accommodate a mode- or header-line if present.
297 A value less than `window-safe-min-height' is ignored. The value
298 of this variable is honored when windows are resized or split.
300 Applications should never rebind this variable. To resize a
301 window to a height less than the one specified here, an
302 application should instead call `window-resize' with a non-nil
303 IGNORE argument. In order to have `split-window' make a window
304 shorter, explicitly specify the SIZE argument of that function."
309 (defconst window-safe-min-width
2
310 "The absolute minimum number of columns of a window.
311 Anything less might crash Emacs.")
313 (defcustom window-min-width
10
314 "The minimum number of columns of any window.
315 The value has to accommodate margins, fringes, or scrollbars if
316 present. A value less than `window-safe-min-width' is ignored.
317 The value of this variable is honored when windows are resized or
320 Applications should never rebind this variable. To resize a
321 window to a width less than the one specified here, an
322 application should instead call `window-resize' with a non-nil
323 IGNORE argument. In order to have `split-window' make a window
324 narrower, explicitly specify the SIZE argument of that function."
329 (defun window-combined-p (&optional window horizontal
)
330 "Return non-nil if WINDOW has siblings in a given direction.
331 WINDOW must be a valid window and defaults to the selected one.
333 HORIZONTAL determines a direction for the window combination.
334 If HORIZONTAL is omitted or nil, return non-nil if WINDOW is part
335 of a vertical window combination.
336 If HORIZONTAL is non-nil, return non-nil if WINDOW is part of a
337 horizontal window combination."
338 (setq window
(window-normalize-window window
))
339 (let ((parent (window-parent window
)))
342 (window-left-child parent
)
343 (window-top-child parent
)))))
345 (defun window-combinations (window &optional horizontal
)
346 "Return largest number of windows vertically arranged within WINDOW.
347 WINDOW must be a valid window and defaults to the selected one.
348 If HORIZONTAL is non-nil, return the largest number of
349 windows horizontally arranged within WINDOW."
350 (setq window
(window-normalize-window window
))
352 ((window-live-p window
)
353 ;; If WINDOW is live, return 1.
356 (window-left-child window
)
357 (window-top-child window
))
358 ;; If WINDOW is iso-combined, return the sum of the values for all
359 ;; child windows of WINDOW.
360 (let ((child (window-child window
))
364 (+ (window-combinations child horizontal
)
366 (setq child
(window-right child
)))
369 ;; If WINDOW is not iso-combined, return the maximum value of any
370 ;; child window of WINDOW.
371 (let ((child (window-child window
))
375 (max (window-combinations child horizontal
)
377 (setq child
(window-right child
)))
380 (defun walk-window-tree-1 (fun walk-window-tree-window any
&optional sub-only
)
381 "Helper function for `walk-window-tree' and `walk-window-subtree'."
382 (let (walk-window-tree-buffer)
383 (while walk-window-tree-window
384 (setq walk-window-tree-buffer
385 (window-buffer walk-window-tree-window
))
386 (when (or walk-window-tree-buffer any
)
387 (funcall fun walk-window-tree-window
))
388 (unless walk-window-tree-buffer
390 fun
(window-left-child walk-window-tree-window
) any
)
392 fun
(window-top-child walk-window-tree-window
) any
))
394 (setq walk-window-tree-window nil
)
395 (setq walk-window-tree-window
396 (window-right walk-window-tree-window
))))))
398 (defun walk-window-tree (fun &optional frame any minibuf
)
399 "Run function FUN on each live window of FRAME.
400 FUN must be a function with one argument - a window. FRAME must
401 be a live frame and defaults to the selected one. ANY, if
402 non-nil, means to run FUN on all live and internal windows of
405 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
406 window even if it isn't active. MINIBUF nil or omitted means run
407 FUN on FRAME's minibuffer window only if it's active. In both
408 cases the minibuffer window must be part of FRAME. MINIBUF
409 neither nil nor t means never run FUN on the minibuffer window.
411 This function performs a pre-order, depth-first traversal of the
412 window tree. If FUN changes the window tree, the result is
414 (setq frame
(window-normalize-frame frame
))
415 (walk-window-tree-1 fun
(frame-root-window frame
) any
)
416 (when (memq minibuf
'(nil t
))
417 ;; Run FUN on FRAME's minibuffer window if requested.
418 (let ((minibuffer-window (minibuffer-window frame
)))
419 (when (and (window-live-p minibuffer-window
)
420 (eq (window-frame minibuffer-window
) frame
)
422 (minibuffer-window-active-p minibuffer-window
)))
423 (funcall fun minibuffer-window
)))))
425 (defun walk-window-subtree (fun &optional window any
)
426 "Run function FUN on the subtree of windows rooted at WINDOW.
427 WINDOW defaults to the selected window. FUN must be a function
428 with one argument - a window. By default, run FUN only on live
429 windows of the subtree. If the optional argument ANY is non-nil,
430 run FUN on all live and internal windows of the subtree. If
431 WINDOW is live, run FUN on WINDOW only.
433 This function performs a pre-order, depth-first traversal of the
434 subtree rooted at WINDOW. If FUN changes that tree, the result
436 (setq window
(window-normalize-window window
))
437 (walk-window-tree-1 fun window any t
))
439 (defun window-with-parameter (parameter &optional value frame any minibuf
)
440 "Return first window on FRAME with PARAMETER non-nil.
441 FRAME defaults to the selected frame. Optional argument VALUE
442 non-nil means only return a window whose window-parameter value
443 for PARAMETER equals VALUE (comparison is done with `equal').
444 Optional argument ANY non-nil means consider internal windows
447 Optional argument MINIBUF t means consider FRAME's minibuffer
448 window even if it isn't active. MINIBUF nil or omitted means
449 consider FRAME's minibuffer window only if it's active. In both
450 cases the minibuffer window must be part of FRAME. MINIBUF
451 neither nil nor t means never consider the minibuffer window."
456 (when (and (setq this-value
(window-parameter window parameter
))
457 (or (not value
) (equal value this-value
)))
458 (throw 'found window
)))
459 frame any minibuf
))))
462 (defun window-atom-root (&optional window
)
463 "Return root of atomic window WINDOW is a part of.
464 WINDOW must be a valid window and defaults to the selected one.
465 Return nil if WINDOW is not part of an atomic window."
466 (setq window
(window-normalize-window window
))
468 (while (and window
(window-parameter window
'window-atom
))
470 (setq window
(window-parent window
)))
473 (defun window-make-atom (window)
474 "Make WINDOW an atomic window.
475 WINDOW must be an internal window. Return WINDOW."
476 (if (not (window-child window
))
477 (error "Window %s is not an internal window" window
)
480 (set-window-parameter window
'window-atom t
))
484 (defun display-buffer-in-atom-window (buffer alist
)
485 "Display BUFFER in an atomic window.
486 This function displays BUFFER in a new window that will be
487 combined with an existing window to form an atomic window. If
488 the existing window is already part of an atomic window, add the
489 new window to that atomic window. Operations like `split-window'
490 or `delete-window', when applied to a constituent of an atomic
491 window, are applied atomically to the root of that atomic window.
493 ALIST is an association list of symbols and values. The
494 following symbols can be used.
496 `window' specifies the existing window the new window shall be
497 combined with. Use `window-atom-root' to make the new window a
498 sibling of an atomic window's root. If an internal window is
499 specified here, all children of that window become part of the
500 atomic window too. If no window is specified, the new window
501 becomes a sibling of the selected window.
503 `side' denotes the side of the existing window where the new
504 window shall be located. Valid values are `below', `right',
505 `above' and `left'. The default is `below'.
507 The return value is the new window, nil when creating that window
509 (let ((ignore-window-parameters t
)
510 (window-combination-limit t
)
511 (window (cdr (assq 'window alist
)))
512 (side (cdr (assq 'side alist
)))
514 (setq window
(window-normalize-window window
))
515 ;; Split off new window
516 (when (setq new
(split-window window nil side
))
517 ;; Make sure we have a valid atomic window.
518 (window-make-atom (window-parent window
))
519 ;; Display BUFFER in NEW and return NEW.
520 (window--display-buffer
521 buffer new
'window alist display-buffer-mark-dedicated
))))
523 (defun window--atom-check-1 (window)
524 "Subroutine of `window--atom-check'."
526 (if (window-parameter window
'window-atom
)
528 (when (or (catch 'reset
531 (if (window-parameter window
'window-atom
)
532 (setq count
(1+ count
))
535 ;; count >= 1 must hold here. If there's no other
536 ;; window around dissolve this atomic window.
538 ;; Dissolve atomic window.
541 (set-window-parameter window
'window-atom nil
))
544 (unless (window-buffer window
)
545 (window--atom-check-1 (window-left-child window
))
546 (window--atom-check-1 (window-top-child window
))))
547 ;; Check right sibling
548 (window--atom-check-1 (window-right window
))))
550 (defun window--atom-check (&optional frame
)
551 "Check atomicity of all windows on FRAME.
552 FRAME defaults to the selected frame. If an atomic window is
553 wrongly configured, reset the atomicity of all its windows on
554 FRAME to nil. An atomic window is wrongly configured if it has
555 no child windows or one of its child windows is not atomic."
556 (window--atom-check-1 (frame-root-window frame
)))
559 (defvar window-sides
'(left top right bottom
)
562 (defcustom window-sides-vertical nil
563 "If non-nil, left and right side windows are full height.
564 Otherwise, top and bottom side windows are full width."
569 (defcustom window-sides-slots
'(nil nil nil nil
)
570 "Maximum number of side window slots.
571 The value is a list of four elements specifying the number of
572 side window slots on (in this order) the left, top, right and
573 bottom side of each frame. If an element is a number, this means
574 to display at most that many side windows on the corresponding
575 side. If an element is nil, this means there's no bound on the
576 number of slots on that side."
581 :value
(nil nil nil nil
)
584 :help-echo
"Maximum slots of left side window."
586 :format
"%[Left%] %v\n"
587 (const :tag
"Unlimited" :format
"%t" nil
)
588 (integer :tag
"Number" :value
2 :size
5))
591 :help-echo
"Maximum slots of top side window."
593 :format
"%[Top%] %v\n"
594 (const :tag
"Unlimited" :format
"%t" nil
)
595 (integer :tag
"Number" :value
3 :size
5))
598 :help-echo
"Maximum slots of right side window."
600 :format
"%[Right%] %v\n"
601 (const :tag
"Unlimited" :format
"%t" nil
)
602 (integer :tag
"Number" :value
2 :size
5))
605 :help-echo
"Maximum slots of bottom side window."
607 :format
"%[Bottom%] %v\n"
608 (const :tag
"Unlimited" :format
"%t" nil
)
609 (integer :tag
"Number" :value
3 :size
5)))
612 (defun window--major-non-side-window (&optional frame
)
613 "Return the major non-side window of frame FRAME.
614 The optional argument FRAME must be a live frame and defaults to
617 If FRAME has at least one side window, the major non-side window
618 is either an internal non-side window such that all other
619 non-side windows on FRAME descend from it, or the single live
620 non-side window of FRAME. If FRAME has no side windows, return
622 (let ((frame (window-normalize-frame frame
))
624 ;; Set major to the _last_ window found by `walk-window-tree' that
625 ;; is not a side window but has a side window as its sibling.
628 (and (not (window-parameter window
'window-side
))
629 (or (and (setq sibling
(window-prev-sibling window
))
630 (window-parameter sibling
'window-side
))
631 (and (setq sibling
(window-next-sibling window
))
632 (window-parameter sibling
'window-side
)))
633 (setq major window
)))
635 (or major
(frame-root-window frame
))))
637 (defun window--major-side-window (side)
638 "Return major side window on SIDE.
639 SIDE must be one of the symbols `left', `top', `right' or
640 `bottom'. Return nil if no such window exists."
641 (let ((root (frame-root-window))
643 ;; (1) If a window on the opposite side exists, return that window's
645 ;; (2) If the new window shall span the entire side, return the
646 ;; frame's root window.
647 ;; (3) If a window on an orthogonal side exists, return that
649 ;; (4) Otherwise return the frame's root window.
651 ((or (and (eq side
'left
)
652 (setq window
(window-with-parameter 'window-side
'right nil t
)))
654 (setq window
(window-with-parameter 'window-side
'bottom nil t
))))
655 (window-prev-sibling window
))
656 ((or (and (eq side
'right
)
657 (setq window
(window-with-parameter 'window-side
'left nil t
)))
658 (and (eq side
'bottom
)
659 (setq window
(window-with-parameter 'window-side
'top nil t
))))
660 (window-next-sibling window
))
661 ((memq side
'(left right
))
663 (window-sides-vertical
665 ((setq window
(window-with-parameter 'window-side
'top nil t
))
666 (window-next-sibling window
))
667 ((setq window
(window-with-parameter 'window-side
'bottom nil t
))
668 (window-prev-sibling window
))
670 ((memq side
'(top bottom
))
672 ((not window-sides-vertical
)
674 ((setq window
(window-with-parameter 'window-side
'left nil t
))
675 (window-next-sibling window
))
676 ((setq window
(window-with-parameter 'window-side
'right nil t
))
677 (window-prev-sibling window
))
680 (defun display-buffer-in-major-side-window (buffer side slot
&optional alist
)
681 "Display BUFFER in a new window on SIDE of the selected frame.
682 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
683 specifies the slot to use. ALIST is an association list of
684 symbols and values as passed to `display-buffer-in-side-window'.
685 This function may be called only if no window on SIDE exists yet.
686 The new window automatically becomes the \"major\" side window on
687 SIDE. Return the new window, nil if its creation window failed."
688 (let* ((left-or-right (memq side
'(left right
)))
689 (major (window--major-side-window side
))
691 ((eq side
'top
) 'above
)
692 ((eq side
'bottom
) 'below
)
694 ;; The following two bindings will tell `split-window' to take
695 ;; the space for the new window from `major' and not make a new
696 ;; parent window unless needed.
697 (window-combination-resize 'side
)
698 (window-combination-limit nil
)
699 (new (split-window major nil on-side
)))
701 ;; Initialize `window-side' parameter of new window to SIDE.
702 (set-window-parameter new
'window-side side
)
703 ;; Install `window-slot' parameter of new window.
704 (set-window-parameter new
'window-slot slot
)
705 ;; Install `delete-window' parameter thus making sure that when
706 ;; the new window is deleted, a side window on the opposite side
707 ;; does not get resized.
708 (set-window-parameter new
'delete-window
'delete-side-window
)
709 ;; Auto-adjust height/width of new window unless a size has been
710 ;; explicitly requested.
711 (unless (if left-or-right
712 (cdr (assq 'window-width alist
))
713 (cdr (assq 'window-height alist
)))
717 (if left-or-right
'window-width
'window-height
)
718 (/ (window-total-size (frame-root-window) left-or-right
)
719 ;; By default use a fourth of the size of the
720 ;; frame's root window.
723 ;; Install BUFFER in new window and return NEW.
724 (window--display-buffer buffer new
'window alist
'side
))))
726 (defun delete-side-window (window)
727 "Delete side window WINDOW."
728 (let ((window-combination-resize
729 (window-parameter (window-parent window
) 'window-side
))
730 (ignore-window-parameters t
))
731 (delete-window window
)))
733 (defun display-buffer-in-side-window (buffer alist
)
734 "Display BUFFER in a window on side SIDE of the selected frame.
735 ALIST is an association list of symbols and values. The
736 following symbols can be used:
738 `side' denotes the side of the existing window where the new
739 window shall be located. Valid values are `bottom', `right',
740 `top' and `left'. The default is `bottom'.
742 `slot' if non-nil, specifies the window slot where to display
743 BUFFER. A value of zero or nil means use the middle slot on
744 the specified side. A negative value means use a slot
745 preceding (that is, above or on the left of) the middle slot.
746 A positive value means use a slot following (that is, below or
747 on the right of) the middle slot. The default is zero."
748 (let ((side (or (cdr (assq 'side alist
)) 'bottom
))
749 (slot (or (cdr (assq 'slot alist
)) 0)))
751 ((not (memq side
'(top bottom left right
)))
752 (error "Invalid side %s specified" side
))
753 ((not (numberp slot
))
754 (error "Invalid slot %s specified" slot
)))
756 (let* ((major (window-with-parameter 'window-side side nil t
))
757 ;; `major' is the major window on SIDE, `windows' the list of
758 ;; life windows on SIDE.
764 (when (eq (window-parameter window
'window-side
) side
)
765 (setq windows
(cons window windows
)))))
766 (nreverse windows
))))
767 (slots (when major
(max 1 (window-child-count major
))))
773 ((eq side
'bottom
) 3))
775 window this-window this-slot prev-window next-window
776 best-window best-slot abs-slot
)
779 ((and (numberp max-slots
) (<= max-slots
0))
780 ;; No side-slots available on this side. Don't create an error,
784 ;; No major window exists on this side, make one.
785 (display-buffer-in-major-side-window buffer side slot alist
))
787 ;; Scan windows on SIDE.
789 (dolist (window windows
)
790 (setq this-slot
(window-parameter window
'window-slot
))
792 ;; The following should not happen and probably be checked
793 ;; by window--side-check.
794 ((not (numberp this-slot
)))
796 ;; A window with a matching slot has been found.
797 (setq this-window window
)
800 ;; Check if this window has a better slot value wrt the
801 ;; slot of the window we want.
803 (if (or (and (> this-slot
0) (> slot
0))
804 (and (< this-slot
0) (< slot
0)))
805 (abs (- slot this-slot
))
806 (+ (abs slot
) (abs this-slot
))))
807 (unless (and best-slot
(<= best-slot abs-slot
))
808 (setq best-window window
)
809 (setq best-slot abs-slot
))
812 (setq prev-window window
))
814 (setq next-window window
)))))))
816 ;; `this-window' is the first window with the same SLOT.
817 ;; `prev-window' is the window with the largest slot < SLOT. A new
818 ;; window will be created after it.
819 ;; `next-window' is the window with the smallest slot > SLOT. A new
820 ;; window will be created before it.
821 ;; `best-window' is the window with the smallest absolute difference
822 ;; of its slot and SLOT.
824 ;; Note: We dedicate the window used softly to its buffer to
825 ;; avoid that "other" (non-side) buffer display functions steal
826 ;; it from us. This must eventually become customizable via
827 ;; ALIST (or, better, avoided in the "other" functions).
829 ;; Reuse `this-window'.
830 (window--display-buffer buffer this-window
'reuse alist
'side
))
831 (and (or (not max-slots
) (< slots max-slots
))
833 ;; Make new window before `next-window'.
835 (if (memq side
'(left right
)) 'above
'left
))
836 (window-combination-resize 'side
))
837 (setq window
(split-window next-window nil next-side
))
838 ;; When the new window is deleted, its space
839 ;; is returned to other side windows.
840 (set-window-parameter
841 window
'delete-window
'delete-side-window
)
844 ;; Make new window after `prev-window'.
846 (if (memq side
'(left right
)) 'below
'right
))
847 (window-combination-resize 'side
))
848 (setq window
(split-window prev-window nil prev-side
))
849 ;; When the new window is deleted, its space
850 ;; is returned to other side windows.
851 (set-window-parameter
852 window
'delete-window
'delete-side-window
)
854 (set-window-parameter window
'window-slot slot
)
855 (window--display-buffer buffer window
'window alist
'side
))
857 ;; Reuse `best-window'.
859 ;; Give best-window the new slot value.
860 (set-window-parameter best-window
'window-slot slot
)
861 (window--display-buffer
862 buffer best-window
'reuse alist
'side
)))))))))
864 (defun window--side-check (&optional frame
)
865 "Check the side window configuration of FRAME.
866 FRAME defaults to the selected frame.
868 A valid side window configuration preserves the following two
871 - If there exists a window whose window-side parameter is
872 non-nil, there must exist at least one live window whose
873 window-side parameter is nil.
875 - If a window W has a non-nil window-side parameter (i) it must
876 have a parent window and that parent's window-side parameter
877 must be either nil or the same as for W, and (ii) any child
878 window of W must have the same window-side parameter as W.
880 If the configuration is invalid, reset the window-side parameters
881 of all windows on FRAME to nil."
882 (let (left top right bottom none side parent parent-side
)
883 (when (or (catch 'reset
886 (setq side
(window-parameter window
'window-side
))
887 (setq parent
(window-parent window
))
889 (and parent
(window-parameter parent
'window-side
)))
890 ;; The following `cond' seems a bit tedious, but I'd
891 ;; rather stick to using just the stack.
894 (when (not (eq parent-side side
))
895 ;; A parent whose window-side is non-nil must
896 ;; have a child with the same window-side.
899 (when (window-buffer window
)
900 ;; Record that we have at least one non-side,
903 ((if (memq side
'(left top
))
904 (window-prev-sibling window
)
905 (window-next-sibling window
))
906 ;; Left and top major side windows must not have a
907 ;; previous sibling, right and bottom major side
908 ;; windows must not have a next sibling.
910 ;; Now check that there's no more than one major
911 ;; window for any of left, top, right and bottom.
913 (if left
(throw 'reset t
) (setq left t
)))
915 (if top
(throw 'reset t
) (setq top t
)))
917 (if right
(throw 'reset t
) (setq right t
)))
919 (if bottom
(throw 'reset t
) (setq bottom t
)))
923 ;; If there's a side window, there must be at least one
925 (and (or left top right bottom
) (not none
)))
928 (set-window-parameter window
'window-side nil
))
931 (defun window--check (&optional frame
)
932 "Check atomic and side windows on FRAME.
933 FRAME defaults to the selected frame."
934 (window--side-check frame
)
935 (window--atom-check frame
))
938 (defvar window-size-fixed nil
939 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
940 If the value is `height', then only the window's height is fixed.
941 If the value is `width', then only the window's width is fixed.
942 Any other non-nil value fixes both the width and the height.
944 Emacs won't change the size of any window displaying that buffer,
945 unless it has no other choice (like when deleting a neighboring
947 (make-variable-buffer-local 'window-size-fixed
)
949 (defun window--size-ignore-p (window ignore
)
950 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
951 (if (window-valid-p ignore
) (eq window ignore
) ignore
))
953 (defun window-min-size (&optional window horizontal ignore
)
954 "Return the minimum size of WINDOW.
955 WINDOW must be a valid window and defaults to the selected one.
956 Optional argument HORIZONTAL non-nil means return the minimum
957 number of columns of WINDOW; otherwise return the minimum number
960 Optional argument IGNORE, if non-nil, means ignore restrictions
961 imposed by fixed size windows, `window-min-height' or
962 `window-min-width' settings. If IGNORE equals `safe', live
963 windows may get as small as `window-safe-min-height' lines and
964 `window-safe-min-width' columns. If IGNORE is a window, ignore
965 restrictions for that window only. Any other non-nil value
966 means ignore all of the above restrictions for all windows."
968 (window-normalize-window window
) horizontal ignore
))
970 (defun window--min-size-1 (window horizontal ignore
)
971 "Internal function of `window-min-size'."
972 (let ((sub (window-child window
)))
975 ;; WINDOW is an internal window.
976 (if (window-combined-p sub horizontal
)
977 ;; The minimum size of an iso-combination is the sum of
978 ;; the minimum sizes of its child windows.
981 (window--min-size-1 sub horizontal ignore
)))
982 (setq sub
(window-right sub
)))
983 ;; The minimum size of an ortho-combination is the maximum of
984 ;; the minimum sizes of its child windows.
986 (setq value
(max value
987 (window--min-size-1 sub horizontal ignore
)))
988 (setq sub
(window-right sub
))))
990 (with-current-buffer (window-buffer window
)
992 ((and (not (window--size-ignore-p window ignore
))
993 (window-size-fixed-p window horizontal
))
994 ;; The minimum size of a fixed size window is its size.
995 (window-total-size window horizontal
))
996 ((or (eq ignore
'safe
) (eq ignore window
))
997 ;; If IGNORE equals `safe' or WINDOW return the safe values.
998 (if horizontal window-safe-min-width window-safe-min-height
))
1000 ;; For the minimum width of a window take fringes and
1001 ;; scroll-bars into account. This is questionable and should
1002 ;; be removed as soon as we are able to split (and resize)
1003 ;; windows such that the new (or resized) windows can get a
1004 ;; size less than the user-specified `window-min-height' and
1005 ;; `window-min-width'.
1006 (let ((frame (window-frame window
))
1007 (fringes (window-fringes window
))
1008 (scroll-bars (window-scroll-bars window
)))
1010 (+ window-safe-min-width
1011 (ceiling (car fringes
) (frame-char-width frame
))
1012 (ceiling (cadr fringes
) (frame-char-width frame
))
1014 ((memq (nth 2 scroll-bars
) '(left right
))
1015 (nth 1 scroll-bars
))
1016 ((memq (frame-parameter frame
'vertical-scroll-bars
)
1018 (ceiling (or (frame-parameter frame
'scroll-bar-width
) 14)
1019 (frame-char-width)))
1021 (if (and (not (window--size-ignore-p window ignore
))
1022 (numberp window-min-width
))
1026 ;; For the minimum height of a window take any mode- or
1027 ;; header-line into account.
1028 (max (+ window-safe-min-height
1029 (if header-line-format
1 0)
1030 (if mode-line-format
1 0))
1031 (if (and (not (window--size-ignore-p window ignore
))
1032 (numberp window-min-height
))
1036 (defun window-sizable (window delta
&optional horizontal ignore
)
1037 "Return DELTA if DELTA lines can be added to WINDOW.
1038 WINDOW must be a valid window and defaults to the selected one.
1039 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1040 columns can be added to WINDOW. A return value of zero means
1041 that no lines (or columns) can be added to WINDOW.
1043 This function looks only at WINDOW and, recursively, its child
1044 windows. The function `window-resizable' looks at other windows
1047 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1048 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1049 return the maximum value in the range 0..DELTA by which WINDOW
1052 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1053 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1054 return the minimum value in the range DELTA..0 by which WINDOW
1057 Optional argument IGNORE non-nil means ignore restrictions
1058 imposed by fixed size windows, `window-min-height' or
1059 `window-min-width' settings. If IGNORE equals `safe', live
1060 windows may get as small as `window-safe-min-height' lines and
1061 `window-safe-min-width' columns. If IGNORE is a window, ignore
1062 restrictions for that window only. Any other non-nil value means
1063 ignore all of the above restrictions for all windows."
1064 (setq window
(window-normalize-window window
))
1067 (max (- (window-min-size window horizontal ignore
)
1068 (window-total-size window horizontal
))
1070 ((window--size-ignore-p window ignore
)
1073 (if (window-size-fixed-p window horizontal
)
1078 (defun window-sizable-p (window delta
&optional horizontal ignore
)
1079 "Return t if WINDOW can be resized by DELTA lines.
1080 WINDOW must be a valid window and defaults to the selected one.
1081 For the meaning of the arguments of this function see the
1082 doc-string of `window-sizable'."
1083 (setq window
(window-normalize-window window
))
1085 (>= (window-sizable window delta horizontal ignore
) delta
)
1086 (<= (window-sizable window delta horizontal ignore
) delta
)))
1088 (defun window--size-fixed-1 (window horizontal
)
1089 "Internal function for `window-size-fixed-p'."
1090 (let ((sub (window-child window
)))
1093 ;; WINDOW is an internal window.
1094 (if (window-combined-p sub horizontal
)
1095 ;; An iso-combination is fixed size if all its child
1096 ;; windows are fixed-size.
1099 (unless (window--size-fixed-1 sub horizontal
)
1100 ;; We found a non-fixed-size child window, so
1101 ;; WINDOW's size is not fixed.
1103 (setq sub
(window-right sub
)))
1104 ;; All child windows are fixed-size, so WINDOW's size is
1107 ;; An ortho-combination is fixed-size if at least one of its
1108 ;; child windows is fixed-size.
1110 (when (window--size-fixed-1 sub horizontal
)
1111 ;; We found a fixed-size child window, so WINDOW's size
1114 (setq sub
(window-right sub
))))
1115 ;; WINDOW is a live window.
1116 (with-current-buffer (window-buffer window
)
1118 (memq window-size-fixed
'(width t
))
1119 (memq window-size-fixed
'(height t
))))))))
1121 (defun window-size-fixed-p (&optional window horizontal
)
1122 "Return non-nil if WINDOW's height is fixed.
1123 WINDOW must be a valid window and defaults to the selected one.
1124 Optional argument HORIZONTAL non-nil means return non-nil if
1125 WINDOW's width is fixed.
1127 If this function returns nil, this does not necessarily mean that
1128 WINDOW can be resized in the desired direction. The function
1129 `window-resizable' can tell that."
1130 (window--size-fixed-1
1131 (window-normalize-window window
) horizontal
))
1133 (defun window--min-delta-1 (window delta
&optional horizontal ignore trail noup
)
1134 "Internal function for `window-min-delta'."
1135 (if (not (window-parent window
))
1136 ;; If we can't go up, return zero.
1138 ;; Else try to find a non-fixed-size sibling of WINDOW.
1139 (let* ((parent (window-parent window
))
1140 (sub (window-child parent
)))
1142 (if (window-combined-p sub horizontal
)
1143 ;; In an iso-combination throw DELTA if we find at least one
1144 ;; child window and that window is either not fixed-size or
1145 ;; we can ignore fixed-sizeness.
1146 (let ((skip (eq trail
'after
)))
1150 (setq skip
(eq trail
'before
)))
1152 ((and (not (window--size-ignore-p window ignore
))
1153 (window-size-fixed-p sub horizontal
)))
1155 ;; We found a non-fixed-size child window.
1156 (throw 'done delta
)))
1157 (setq sub
(window-right sub
))))
1158 ;; In an ortho-combination set DELTA to the minimum value by
1159 ;; which other child windows can shrink.
1161 (unless (eq sub window
)
1164 (- (window-total-size sub horizontal
)
1165 (window-min-size sub horizontal ignore
)))))
1166 (setq sub
(window-right sub
))))
1169 (window--min-delta-1 parent delta horizontal ignore trail
))))))
1171 (defun window-min-delta (&optional window horizontal ignore trail noup nodown
)
1172 "Return number of lines by which WINDOW can be shrunk.
1173 WINDOW must be a valid window and defaults to the selected one.
1174 Return zero if WINDOW cannot be shrunk.
1176 Optional argument HORIZONTAL non-nil means return number of
1177 columns by which WINDOW can be shrunk.
1179 Optional argument IGNORE non-nil means ignore restrictions
1180 imposed by fixed size windows, `window-min-height' or
1181 `window-min-width' settings. If IGNORE is a window, ignore
1182 restrictions for that window only. If IGNORE equals `safe',
1183 live windows may get as small as `window-safe-min-height' lines
1184 and `window-safe-min-width' columns. Any other non-nil value
1185 means ignore all of the above restrictions for all windows.
1187 Optional argument TRAIL restricts the windows that can be enlarged.
1188 If its value is `before', only windows to the left of or above WINDOW
1189 can be enlarged. If it is `after', only windows to the right of or
1190 below WINDOW can be enlarged.
1192 Optional argument NOUP non-nil means don't go up in the window
1193 tree, but try to enlarge windows within WINDOW's combination only.
1195 Optional argument NODOWN non-nil means don't check whether WINDOW
1196 itself (and its child windows) can be shrunk; check only whether
1197 at least one other window can be enlarged appropriately."
1198 (setq window
(window-normalize-window window
))
1199 (let ((size (window-total-size window horizontal
))
1200 (minimum (window-min-size window horizontal ignore
)))
1203 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1204 (window--min-delta-1 window size horizontal ignore trail noup
))
1206 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1207 ;; there's nothing to recover.
1210 ;; Otherwise, try to recover whatever WINDOW is larger than its
1212 (window--min-delta-1
1213 window
(- size minimum
) horizontal ignore trail noup
)))))
1215 (defun window--max-delta-1 (window delta
&optional horizontal ignore trail noup
)
1216 "Internal function of `window-max-delta'."
1217 (if (not (window-parent window
))
1218 ;; Can't go up. Return DELTA.
1220 (let* ((parent (window-parent window
))
1221 (sub (window-child parent
)))
1223 (if (window-combined-p sub horizontal
)
1224 ;; For an iso-combination calculate how much we can get from
1225 ;; other child windows.
1226 (let ((skip (eq trail
'after
)))
1230 (setq skip
(eq trail
'before
)))
1235 (- (window-total-size sub horizontal
)
1236 (window-min-size sub horizontal ignore
))))))
1237 (setq sub
(window-right sub
))))
1238 ;; For an ortho-combination throw DELTA when at least one
1239 ;; child window is fixed-size.
1241 (when (and (not (eq sub window
))
1242 (not (window--size-ignore-p sub ignore
))
1243 (window-size-fixed-p sub horizontal
))
1244 (throw 'fixed delta
))
1245 (setq sub
(window-right sub
))))
1247 ;; When NOUP is nil, DELTA is all we can get.
1249 ;; Else try with parent of WINDOW, passing the DELTA we
1250 ;; recovered so far.
1251 (window--max-delta-1 parent delta horizontal ignore trail
))))))
1253 (defun window-max-delta (&optional window horizontal ignore trail noup nodown
)
1254 "Return maximum number of lines by which WINDOW can be enlarged.
1255 WINDOW must be a valid window and defaults to the selected one.
1256 The return value is zero if WINDOW cannot be enlarged.
1258 Optional argument HORIZONTAL non-nil means return maximum number
1259 of columns by which WINDOW can be enlarged.
1261 Optional argument IGNORE non-nil means ignore restrictions
1262 imposed by fixed size windows, `window-min-height' or
1263 `window-min-width' settings. If IGNORE is a window, ignore
1264 restrictions for that window only. If IGNORE equals `safe',
1265 live windows may get as small as `window-safe-min-height' lines
1266 and `window-safe-min-width' columns. Any other non-nil value means
1267 ignore all of the above restrictions for all windows.
1269 Optional argument TRAIL restricts the windows that can be enlarged.
1270 If its value is `before', only windows to the left of or above WINDOW
1271 can be enlarged. If it is `after', only windows to the right of or
1272 below WINDOW can be enlarged.
1274 Optional argument NOUP non-nil means don't go up in the window
1275 tree but try to obtain the entire space from windows within
1276 WINDOW's combination.
1278 Optional argument NODOWN non-nil means do not check whether
1279 WINDOW itself (and its child windows) can be enlarged; check
1280 only whether other windows can be shrunk appropriately."
1281 (setq window
(window-normalize-window window
))
1282 (if (and (not (window--size-ignore-p window ignore
))
1283 (not nodown
) (window-size-fixed-p window horizontal
))
1284 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1287 ;; WINDOW has no fixed size.
1288 (window--max-delta-1 window
0 horizontal ignore trail noup
)))
1290 ;; Make NOUP also inhibit the min-size check.
1291 (defun window--resizable (window delta
&optional horizontal ignore trail noup nodown
)
1292 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1293 WINDOW must be a valid window and defaults to the selected one.
1294 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1295 can be resized horizontally by DELTA columns. A return value of
1296 zero means that WINDOW is not resizable.
1298 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1299 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1300 return the maximum value in the range 0..DELTA by which WINDOW
1303 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1304 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1305 return the minimum value in the range DELTA..0 that can be used
1306 for shrinking WINDOW.
1308 Optional argument IGNORE non-nil means ignore restrictions
1309 imposed by fixed size windows, `window-min-height' or
1310 `window-min-width' settings. If IGNORE is a window, ignore
1311 restrictions for that window only. If IGNORE equals `safe',
1312 live windows may get as small as `window-safe-min-height' lines
1313 and `window-safe-min-width' columns. Any other non-nil value
1314 means ignore all of the above restrictions for all windows.
1316 Optional argument TRAIL `before' means only windows to the left
1317 of or below WINDOW can be shrunk. Optional argument TRAIL
1318 `after' means only windows to the right of or above WINDOW can be
1321 Optional argument NOUP non-nil means don't go up in the window
1322 tree but check only whether space can be obtained from (or given
1323 to) WINDOW's siblings.
1325 Optional argument NODOWN non-nil means don't go down in the
1326 window tree. This means do not check whether resizing would
1327 violate size restrictions of WINDOW or its child windows."
1328 (setq window
(window-normalize-window window
))
1331 (max (- (window-min-delta window horizontal ignore trail noup nodown
))
1334 (min (window-max-delta window horizontal ignore trail noup nodown
)
1338 (defun window-resizable-p (window delta
&optional horizontal ignore trail noup nodown
)
1339 "Return t if WINDOW can be resized vertically by DELTA lines.
1340 WINDOW must be a valid window and defaults to the selected one.
1341 For the meaning of the arguments of this function see the
1342 doc-string of `window--resizable'."
1343 (setq window
(window-normalize-window window
))
1345 (>= (window--resizable window delta horizontal ignore trail noup nodown
)
1347 (<= (window--resizable window delta horizontal ignore trail noup nodown
)
1350 (defun window-resizable (window delta
&optional horizontal ignore
)
1351 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1352 WINDOW must be a valid window and defaults to the selected one.
1353 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1354 can be resized horizontally by DELTA columns. A return value of
1355 zero means that WINDOW is not resizable.
1357 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1358 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1359 return the maximum value in the range 0..DELTA by which WINDOW
1362 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1363 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1364 return the minimum value in the range DELTA..0 that can be used
1365 for shrinking WINDOW.
1367 Optional argument IGNORE non-nil means ignore restrictions
1368 imposed by fixed size windows, `window-min-height' or
1369 `window-min-width' settings. If IGNORE is a window, ignore
1370 restrictions for that window only. If IGNORE equals `safe',
1371 live windows may get as small as `window-safe-min-height' lines
1372 and `window-safe-min-width' columns. Any other non-nil value
1373 means ignore all of the above restrictions for all windows."
1374 (setq window
(window-normalize-window window
))
1375 (window--resizable window delta horizontal ignore
))
1377 (defun window-total-size (&optional window horizontal
)
1378 "Return the total height or width of WINDOW.
1379 WINDOW must be a valid window and defaults to the selected one.
1381 If HORIZONTAL is omitted or nil, return the total height of
1382 WINDOW, in lines, like `window-total-height'. Otherwise return
1383 the total width, in columns, like `window-total-width'."
1385 (window-total-width window
)
1386 (window-total-height window
)))
1388 ;; Eventually we should make `window-height' obsolete.
1389 (defalias 'window-height
'window-total-height
)
1391 ;; See discussion in bug#4543.
1392 (defun window-full-height-p (&optional window
)
1393 "Return t if WINDOW is as high as its containing frame.
1394 More precisely, return t if and only if the total height of
1395 WINDOW equals the total height of the root window of WINDOW's
1396 frame. WINDOW must be a valid window and defaults to the
1398 (setq window
(window-normalize-window window
))
1399 (= (window-total-size window
)
1400 (window-total-size (frame-root-window window
))))
1402 (defun window-full-width-p (&optional window
)
1403 "Return t if WINDOW is as wide as its containing frame.
1404 More precisely, return t if and only if the total width of WINDOW
1405 equals the total width of the root window of WINDOW's frame.
1406 WINDOW must be a valid window and defaults to the selected one."
1407 (setq window
(window-normalize-window window
))
1408 (= (window-total-size window t
)
1409 (window-total-size (frame-root-window window
) t
)))
1411 (defun window-body-size (&optional window horizontal
)
1412 "Return the height or width of WINDOW's text area.
1413 WINDOW must be a live window and defaults to the selected one.
1415 If HORIZONTAL is omitted or nil, return the height of the text
1416 area, like `window-body-height'. Otherwise, return the width of
1417 the text area, like `window-body-width'."
1419 (window-body-width window
)
1420 (window-body-height window
)))
1422 ;; Eventually we should make `window-height' obsolete.
1423 (defalias 'window-width
'window-body-width
)
1425 (defun window-current-scroll-bars (&optional window
)
1426 "Return the current scroll bar settings for WINDOW.
1427 WINDOW must be a live window and defaults to the selected one.
1429 The return value is a cons cell (VERTICAL . HORIZONTAL) where
1430 VERTICAL specifies the current location of the vertical scroll
1431 bars (`left', `right', or nil), and HORIZONTAL specifies the
1432 current location of the horizontal scroll bars (`top', `bottom',
1435 Unlike `window-scroll-bars', this function reports the scroll bar
1436 type actually used, once frame defaults and `scroll-bar-mode' are
1437 taken into account."
1438 (setq window
(window-normalize-window window t
))
1439 (let ((vert (nth 2 (window-scroll-bars window
)))
1441 (when (or (eq vert t
) (eq hor t
))
1442 (let ((fcsb (frame-current-scroll-bars (window-frame window
))))
1444 (setq vert
(car fcsb
)))
1446 (setq hor
(cdr fcsb
)))))
1449 (defun walk-windows (fun &optional minibuf all-frames
)
1450 "Cycle through all live windows, calling FUN for each one.
1451 FUN must specify a function with a window as its sole argument.
1452 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1453 windows to include in the walk.
1455 MINIBUF t means include the minibuffer window even if the
1456 minibuffer is not active. MINIBUF nil or omitted means include
1457 the minibuffer window only if the minibuffer is active. Any
1458 other value means do not include the minibuffer window even if
1459 the minibuffer is active.
1461 ALL-FRAMES nil or omitted means consider all windows on the
1462 selected frame, plus the minibuffer window if specified by the
1463 MINIBUF argument. If the minibuffer counts, consider all windows
1464 on all frames that share that minibuffer too. The following
1465 non-nil values of ALL-FRAMES have special meanings:
1467 - t means consider all windows on all existing frames.
1469 - `visible' means consider all windows on all visible frames on
1470 the current terminal.
1472 - 0 (the number zero) means consider all windows on all visible
1473 and iconified frames on the current terminal.
1475 - A frame means consider all windows on that frame only.
1477 Anything else means consider all windows on the selected frame
1480 This function changes neither the order of recently selected
1481 windows nor the buffer list."
1482 ;; If we start from the minibuffer window, don't fail to come
1484 (when (window-minibuffer-p)
1486 ;; Make sure to not mess up the order of recently selected
1487 ;; windows. Use `save-selected-window' and `select-window'
1488 ;; with second argument non-nil for this purpose.
1489 (save-selected-window
1490 (when (framep all-frames
)
1491 (select-window (frame-first-window all-frames
) 'norecord
))
1492 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames
))
1493 (funcall fun walk-windows-window
))))
1495 (defun window-at-side-p (&optional window side
)
1496 "Return t if WINDOW is at SIDE of its containing frame.
1497 WINDOW must be a valid window and defaults to the selected one.
1498 SIDE can be any of the symbols `left', `top', `right' or
1499 `bottom'. The default value nil is handled like `bottom'."
1500 (setq window
(window-normalize-window window
))
1505 ((eq side
'right
) 2)
1506 ((memq side
'(bottom nil
)) 3))))
1507 (= (nth edge
(window-edges window
))
1508 (nth edge
(window-edges (frame-root-window window
))))))
1510 (defun window-at-side-list (&optional frame side
)
1511 "Return list of all windows on SIDE of FRAME.
1512 FRAME must be a live frame and defaults to the selected frame.
1513 SIDE can be any of the symbols `left', `top', `right' or
1514 `bottom'. The default value nil is handled like `bottom'."
1515 (setq frame
(window-normalize-frame frame
))
1519 (when (window-at-side-p window side
)
1520 (setq windows
(cons window windows
))))
1522 (nreverse windows
)))
1524 (defun window--in-direction-2 (window posn
&optional horizontal
)
1525 "Support function for `window-in-direction'."
1527 (let ((top (window-top-line window
)))
1530 (- posn top
(window-total-height window
))))
1531 (let ((left (window-left-column window
)))
1534 (- posn left
(window-total-width window
))))))
1536 ;; Predecessors to the below have been devised by Julian Assange in
1537 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
1538 ;; Neither of these allow to selectively ignore specific windows
1539 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
1541 (defun window-in-direction (direction &optional window ignore
)
1542 "Return window in DIRECTION as seen from WINDOW.
1543 More precisely, return the nearest window in direction DIRECTION
1544 as seen from the position of `window-point' in window WINDOW.
1545 DIRECTION must be one of `above', `below', `left' or `right'.
1546 WINDOW must be a live window and defaults to the selected one.
1548 Do not return a window whose `no-other-window' parameter is
1549 non-nil. If the nearest window's `no-other-window' parameter is
1550 non-nil, try to find another window in the indicated direction.
1551 If, however, the optional argument IGNORE is non-nil, return that
1552 window even if its `no-other-window' parameter is non-nil.
1554 Return nil if no suitable window can be found."
1555 (setq window
(window-normalize-window window t
))
1556 (unless (memq direction
'(above below left right
))
1557 (error "Wrong direction %s" direction
))
1558 (let* ((frame (window-frame window
))
1559 (hor (memq direction
'(left right
)))
1561 (window-left-column window
)
1562 (window-top-line window
)))
1563 (last (+ first
(if hor
1564 (window-total-width window
)
1565 (window-total-height window
))))
1566 (posn-cons (nth 6 (posn-at-point (window-point window
) window
)))
1567 ;; The column / row value of `posn-at-point' can be nil for the
1568 ;; mini-window, guard against that.
1570 (+ (or (cdr posn-cons
) 1) (window-top-line window
))
1571 (+ (or (car posn-cons
) 1) (window-left-column window
))))
1574 ((eq direction
'below
) (frame-height frame
))
1575 ((eq direction
'right
) (frame-width frame
))
1577 (best-edge-2 best-edge
)
1578 (best-diff-2 (if hor
(frame-height frame
) (frame-width frame
)))
1579 best best-2 best-diff-2-new
)
1582 (let* ((w-top (window-top-line w
))
1583 (w-left (window-left-column w
)))
1586 ;; Ignore ourselves.
1587 (and (window-parameter w
'no-other-window
)
1588 ;; Ignore W unless IGNORE is non-nil.
1592 ((and (<= w-top posn
)
1593 (< posn
(+ w-top
(window-total-height w
))))
1594 ;; W is to the left or right of WINDOW and covers POSN.
1595 (when (or (and (eq direction
'left
)
1596 (<= w-left first
) (> w-left best-edge
))
1597 (and (eq direction
'right
)
1598 (>= w-left last
) (< w-left best-edge
)))
1599 (setq best-edge w-left
)
1601 ((and (or (and (eq direction
'left
)
1602 (<= (+ w-left
(window-total-width w
)) first
))
1603 (and (eq direction
'right
) (<= last w-left
)))
1604 ;; W is to the left or right of WINDOW but does not
1606 (setq best-diff-2-new
1607 (window--in-direction-2 w posn hor
))
1608 (or (< best-diff-2-new best-diff-2
)
1609 (and (= best-diff-2-new best-diff-2
)
1610 (if (eq direction
'left
)
1611 (> w-left best-edge-2
)
1612 (< w-left best-edge-2
)))))
1613 (setq best-edge-2 w-left
)
1614 (setq best-diff-2 best-diff-2-new
)
1618 ((and (<= w-left posn
)
1619 (< posn
(+ w-left
(window-total-width w
))))
1620 ;; W is above or below WINDOW and covers POSN.
1621 (when (or (and (eq direction
'above
)
1622 (<= w-top first
) (> w-top best-edge
))
1623 (and (eq direction
'below
)
1624 (>= w-top first
) (< w-top best-edge
)))
1625 (setq best-edge w-top
)
1627 ((and (or (and (eq direction
'above
)
1628 (<= (+ w-top
(window-total-height w
)) first
))
1629 (and (eq direction
'below
) (<= last w-top
)))
1630 ;; W is above or below WINDOW but does not cover POSN.
1631 (setq best-diff-2-new
1632 (window--in-direction-2 w posn hor
))
1633 (or (< best-diff-2-new best-diff-2
)
1634 (and (= best-diff-2-new best-diff-2
)
1635 (if (eq direction
'above
)
1636 (> w-top best-edge-2
)
1637 (< w-top best-edge-2
)))))
1638 (setq best-edge-2 w-top
)
1639 (setq best-diff-2 best-diff-2-new
)
1640 (setq best-2 w
)))))))
1644 (defun get-window-with-predicate (predicate &optional minibuf all-frames default
)
1645 "Return a live window satisfying PREDICATE.
1646 More precisely, cycle through all windows calling the function
1647 PREDICATE on each one of them with the window as its sole
1648 argument. Return the first window for which PREDICATE returns
1649 non-nil. Windows are scanned starting with the window following
1650 the selected window. If no window satisfies PREDICATE, return
1653 MINIBUF t means include the minibuffer window even if the
1654 minibuffer is not active. MINIBUF nil or omitted means include
1655 the minibuffer window only if the minibuffer is active. Any
1656 other value means do not include the minibuffer window even if
1657 the minibuffer is active.
1659 ALL-FRAMES nil or omitted means consider all windows on the selected
1660 frame, plus the minibuffer window if specified by the MINIBUF
1661 argument. If the minibuffer counts, consider all windows on all
1662 frames that share that minibuffer too. The following non-nil
1663 values of ALL-FRAMES have special meanings:
1665 - t means consider all windows on all existing frames.
1667 - `visible' means consider all windows on all visible frames on
1668 the current terminal.
1670 - 0 (the number zero) means consider all windows on all visible
1671 and iconified frames on the current terminal.
1673 - A frame means consider all windows on that frame only.
1675 Anything else means consider all windows on the selected frame
1678 (dolist (window (window-list-1
1679 (next-window nil minibuf all-frames
)
1680 minibuf all-frames
))
1681 (when (funcall predicate window
)
1682 (throw 'found window
)))
1685 (defalias 'some-window
'get-window-with-predicate
)
1687 (defun get-lru-window (&optional all-frames dedicated not-selected
)
1688 "Return the least recently used window on frames specified by ALL-FRAMES.
1689 Return a full-width window if possible. A minibuffer window is
1690 never a candidate. A dedicated window is never a candidate
1691 unless DEDICATED is non-nil, so if all windows are dedicated, the
1692 value is nil. Avoid returning the selected window if possible.
1693 Optional argument NOT-SELECTED non-nil means never return the
1696 The following non-nil values of the optional argument ALL-FRAMES
1697 have special meanings:
1699 - t means consider all windows on all existing frames.
1701 - `visible' means consider all windows on all visible frames on
1702 the current terminal.
1704 - 0 (the number zero) means consider all windows on all visible
1705 and iconified frames on the current terminal.
1707 - A frame means consider all windows on that frame only.
1709 Any other value of ALL-FRAMES means consider all windows on the
1710 selected frame and no others."
1711 (let (best-window best-time second-best-window second-best-time time
)
1712 (dolist (window (window-list-1 nil
'nomini all-frames
))
1713 (when (and (or dedicated
(not (window-dedicated-p window
)))
1714 (or (not not-selected
) (not (eq window
(selected-window)))))
1715 (setq time
(window-use-time window
))
1716 (if (or (eq window
(selected-window))
1717 (not (window-full-width-p window
)))
1718 (when (or (not second-best-time
) (< time second-best-time
))
1719 (setq second-best-time time
)
1720 (setq second-best-window window
))
1721 (when (or (not best-time
) (< time best-time
))
1722 (setq best-time time
)
1723 (setq best-window window
)))))
1724 (or best-window second-best-window
)))
1726 (defun get-mru-window (&optional all-frames dedicated not-selected
)
1727 "Return the most recently used window on frames specified by ALL-FRAMES.
1728 A minibuffer window is never a candidate. A dedicated window is
1729 never a candidate unless DEDICATED is non-nil, so if all windows
1730 are dedicated, the value is nil. Optional argument NOT-SELECTED
1731 non-nil means never return the selected window.
1733 The following non-nil values of the optional argument ALL-FRAMES
1734 have special meanings:
1736 - t means consider all windows on all existing frames.
1738 - `visible' means consider all windows on all visible frames on
1739 the current terminal.
1741 - 0 (the number zero) means consider all windows on all visible
1742 and iconified frames on the current terminal.
1744 - A frame means consider all windows on that frame only.
1746 Any other value of ALL-FRAMES means consider all windows on the
1747 selected frame and no others."
1748 (let (best-window best-time time
)
1749 (dolist (window (window-list-1 nil
'nomini all-frames
))
1750 (setq time
(window-use-time window
))
1751 (when (and (or dedicated
(not (window-dedicated-p window
)))
1752 (or (not not-selected
) (not (eq window
(selected-window))))
1753 (or (not best-time
) (> time best-time
)))
1754 (setq best-time time
)
1755 (setq best-window window
)))
1758 (defun get-largest-window (&optional all-frames dedicated not-selected
)
1759 "Return the largest window on frames specified by ALL-FRAMES.
1760 A minibuffer window is never a candidate. A dedicated window is
1761 never a candidate unless DEDICATED is non-nil, so if all windows
1762 are dedicated, the value is nil. Optional argument NOT-SELECTED
1763 non-nil means never return the selected window.
1765 The following non-nil values of the optional argument ALL-FRAMES
1766 have special meanings:
1768 - t means consider all windows on all existing frames.
1770 - `visible' means consider all windows on all visible frames on
1771 the current terminal.
1773 - 0 (the number zero) means consider all windows on all visible
1774 and iconified frames on the current terminal.
1776 - A frame means consider all windows on that frame only.
1778 Any other value of ALL-FRAMES means consider all windows on the
1779 selected frame and no others."
1782 (dolist (window (window-list-1 nil
'nomini all-frames
))
1783 (when (and (or dedicated
(not (window-dedicated-p window
)))
1784 (or (not not-selected
) (not (eq window
(selected-window)))))
1785 (setq size
(* (window-total-size window
)
1786 (window-total-size window t
)))
1787 (when (> size best-size
)
1788 (setq best-size size
)
1789 (setq best-window window
))))
1792 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames
)
1793 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
1794 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
1795 and defaults to the current buffer. Windows are scanned starting
1796 with the selected window.
1798 MINIBUF t means include the minibuffer window even if the
1799 minibuffer is not active. MINIBUF nil or omitted means include
1800 the minibuffer window only if the minibuffer is active. Any
1801 other value means do not include the minibuffer window even if
1802 the minibuffer is active.
1804 ALL-FRAMES nil or omitted means consider all windows on the
1805 selected frame, plus the minibuffer window if specified by the
1806 MINIBUF argument. If the minibuffer counts, consider all windows
1807 on all frames that share that minibuffer too. The following
1808 non-nil values of ALL-FRAMES have special meanings:
1810 - t means consider all windows on all existing frames.
1812 - `visible' means consider all windows on all visible frames on
1813 the current terminal.
1815 - 0 (the number zero) means consider all windows on all visible
1816 and iconified frames on the current terminal.
1818 - A frame means consider all windows on that frame only.
1820 Anything else means consider all windows on the selected frame
1822 (let ((buffer (window-normalize-buffer buffer-or-name
))
1824 (dolist (window (window-list-1 (selected-window) minibuf all-frames
))
1825 (when (eq (window-buffer window
) buffer
)
1826 (setq windows
(cons window windows
))))
1827 (nreverse windows
)))
1829 (defun minibuffer-window-active-p (window)
1830 "Return t if WINDOW is the currently active minibuffer window."
1831 (eq window
(active-minibuffer-window)))
1833 (defun count-windows (&optional minibuf
)
1834 "Return the number of live windows on the selected frame.
1835 The optional argument MINIBUF specifies whether the minibuffer
1836 window shall be counted. See `walk-windows' for the precise
1837 meaning of this argument."
1838 (length (window-list-1 nil minibuf
)))
1840 ;;; Resizing windows.
1841 (defun window--resize-reset (&optional frame horizontal
)
1842 "Reset resize values for all windows on FRAME.
1843 FRAME defaults to the selected frame.
1845 This function stores the current value of `window-total-size' applied
1846 with argument HORIZONTAL in the new total size of all windows on
1847 FRAME. It also resets the new normal size of each of these
1849 (window--resize-reset-1
1850 (frame-root-window (window-normalize-frame frame
)) horizontal
))
1852 (defun window--resize-reset-1 (window horizontal
)
1853 "Internal function of `window--resize-reset'."
1854 ;; Register old size in the new total size.
1855 (set-window-new-total window
(window-total-size window horizontal
))
1856 ;; Reset new normal size.
1857 (set-window-new-normal window
)
1858 (when (window-child window
)
1859 (window--resize-reset-1 (window-child window
) horizontal
))
1860 (when (window-right window
)
1861 (window--resize-reset-1 (window-right window
) horizontal
)))
1863 ;; The following routine is used to manually resize the minibuffer
1864 ;; window and is currently used, for example, by ispell.el.
1865 (defun window--resize-mini-window (window delta
)
1866 "Resize minibuffer window WINDOW by DELTA lines.
1867 If WINDOW cannot be resized by DELTA lines make it as large (or
1868 as small) as possible, but don't signal an error."
1869 (when (window-minibuffer-p window
)
1870 (let* ((frame (window-frame window
))
1871 (root (frame-root-window frame
))
1872 (height (window-total-size window
))
1874 (- (window-total-size root
)
1875 (window-min-size root
))))
1878 ((<= (+ height delta
) 0)
1879 (setq delta
(- (- height
1))))
1880 ((> delta min-delta
)
1881 (setq delta min-delta
)))
1884 (window--resize-reset frame
)
1885 ;; Ideally we should be able to resize just the last child of root
1886 ;; here. See the comment in `resize-root-window-vertically' for
1887 ;; why we do not do that.
1888 (window--resize-this-window root
(- delta
) nil nil t
)
1889 (set-window-new-total window
(+ height delta
))
1890 ;; The following routine catches the case where we want to resize
1891 ;; a minibuffer-only frame.
1892 (resize-mini-window-internal window
))))
1894 (defun window-resize (window delta
&optional horizontal ignore
)
1895 "Resize WINDOW vertically by DELTA lines.
1896 WINDOW can be an arbitrary window and defaults to the selected
1897 one. An attempt to resize the root window of a frame will raise
1900 DELTA a positive number means WINDOW shall be enlarged by DELTA
1901 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
1904 Optional argument HORIZONTAL non-nil means resize WINDOW
1905 horizontally by DELTA columns. In this case a positive DELTA
1906 means enlarge WINDOW by DELTA columns. DELTA negative means
1907 WINDOW shall be shrunk by -DELTA columns.
1909 Optional argument IGNORE non-nil means ignore restrictions
1910 imposed by fixed size windows, `window-min-height' or
1911 `window-min-width' settings. If IGNORE is a window, ignore
1912 restrictions for that window only. If IGNORE equals `safe',
1913 live windows may get as small as `window-safe-min-height' lines
1914 and `window-safe-min-width' columns. Any other non-nil value
1915 means ignore all of the above restrictions for all windows.
1917 This function resizes other windows proportionally and never
1918 deletes any windows. If you want to move only the low (right)
1919 edge of WINDOW consider using `adjust-window-trailing-edge'
1921 (setq window
(window-normalize-window window
))
1922 (let* ((frame (window-frame window
))
1923 (minibuffer-window (minibuffer-window frame
))
1926 ((eq window
(frame-root-window frame
))
1927 (error "Cannot resize the root window of a frame"))
1928 ((window-minibuffer-p window
)
1930 (error "Cannot resize minibuffer window horizontally")
1931 (window--resize-mini-window window delta
)))
1932 ((and (not horizontal
)
1933 (window-full-height-p window
)
1934 (eq (window-frame minibuffer-window
) frame
)
1935 (or (not resize-mini-windows
)
1936 (eq minibuffer-window
(active-minibuffer-window))))
1937 ;; If WINDOW is full height and either `resize-mini-windows' is
1938 ;; nil or the minibuffer window is active, resize the minibuffer
1940 (window--resize-mini-window minibuffer-window
(- delta
)))
1941 ((window-resizable-p window delta horizontal ignore
)
1942 (window--resize-reset frame horizontal
)
1943 (window--resize-this-window window delta horizontal ignore t
)
1944 (if (and (not window-combination-resize
)
1945 (window-combined-p window horizontal
)
1946 (setq sibling
(or (window-right window
) (window-left window
)))
1947 (window-sizable-p sibling
(- delta
) horizontal ignore
))
1948 ;; If window-combination-resize is nil, WINDOW is part of an
1949 ;; iso-combination, and WINDOW's neighboring right or left
1950 ;; sibling can be resized as requested, resize that sibling.
1953 (window-total-size (window-parent window
) horizontal
))))
1954 (window--resize-this-window sibling
(- delta
) horizontal nil t
)
1955 (set-window-new-normal
1956 window
(+ (window-normal-size window horizontal
)
1958 (set-window-new-normal
1959 sibling
(- (window-normal-size sibling horizontal
)
1961 ;; Otherwise, resize all other windows in the same combination.
1962 (window--resize-siblings window delta horizontal ignore
))
1963 (window-resize-apply frame horizontal
))
1965 (error "Cannot resize window %s" window
)))))
1967 (defun window-resize-no-error (window delta
&optional horizontal ignore
)
1968 "Resize WINDOW vertically if it is resizable by DELTA lines.
1969 This function is like `window-resize' but does not signal an
1970 error when WINDOW cannot be resized. For the meaning of the
1971 optional arguments see the documentation of `window-resize'."
1972 (when (window-resizable-p window delta horizontal ignore
)
1973 (window-resize window delta horizontal ignore
)))
1975 (defun window--resize-child-windows-skip-p (window)
1976 "Return non-nil if WINDOW shall be skipped by resizing routines."
1977 (memq (window-new-normal window
) '(ignore stuck skip
)))
1979 (defun window--resize-child-windows-normal (parent horizontal window this-delta
&optional trail other-delta
)
1980 "Recursively set new normal height of child windows of window PARENT.
1981 HORIZONTAL non-nil means set the new normal width of these
1982 windows. WINDOW specifies a child window of PARENT that has been
1983 resized by THIS-DELTA lines (columns).
1985 Optional argument TRAIL either `before' or `after' means set values
1986 only for windows before or after WINDOW. Optional argument
1987 OTHER-DELTA, a number, specifies that this many lines (columns)
1988 have been obtained from (or returned to) an ancestor window of
1989 PARENT in order to resize WINDOW."
1990 (let* ((delta-normal
1991 (if (and (= (- this-delta
) (window-total-size window horizontal
))
1992 (zerop other-delta
))
1993 ;; When WINDOW gets deleted and we can return its entire
1994 ;; space to its siblings, use WINDOW's normal size as the
1996 (- (window-normal-size window horizontal
))
1997 ;; In any other case calculate the normal delta from the
1998 ;; relation of THIS-DELTA to the total size of PARENT.
1999 (/ (float this-delta
) (window-total-size parent horizontal
))))
2000 (sub (window-child parent
))
2002 (skip (eq trail
'after
)))
2004 ;; Set parent-normal to the sum of the normal sizes of all child
2005 ;; windows of PARENT that shall be resized, excluding only WINDOW
2006 ;; and any windows specified by the optional TRAIL argument.
2010 (setq skip
(eq trail
'before
)))
2014 (+ parent-normal
(window-normal-size sub horizontal
)))))
2015 (setq sub
(window-right sub
)))
2017 ;; Set the new normal size of all child windows of PARENT from what
2018 ;; they should have contributed for recovering THIS-DELTA lines
2020 (setq sub
(window-child parent
))
2021 (setq skip
(eq trail
'after
))
2025 (setq skip
(eq trail
'before
)))
2028 (let ((old-normal (window-normal-size sub horizontal
)))
2029 (set-window-new-normal
2030 sub
(min 1.0 ; Don't get larger than 1.
2032 (* (/ old-normal parent-normal
)
2034 ;; Don't drop below 0.
2036 (setq sub
(window-right sub
)))
2038 (when (numberp other-delta
)
2039 ;; Set the new normal size of windows from what they should have
2040 ;; contributed for recovering OTHER-DELTA lines (columns).
2041 (setq delta-normal
(/ (float (window-total-size parent horizontal
))
2042 (+ (window-total-size parent horizontal
)
2044 (setq sub
(window-child parent
))
2045 (setq skip
(eq trail
'after
))
2049 (setq skip
(eq trail
'before
)))
2052 (set-window-new-normal
2053 sub
(min 1.0 ; Don't get larger than 1.
2054 (max (* (window-new-normal sub
) delta-normal
)
2055 ;; Don't drop below 0.
2057 (setq sub
(window-right sub
))))
2059 ;; Set the new normal size of WINDOW to what is left by the sum of
2060 ;; the normal sizes of its siblings.
2061 (set-window-new-normal
2064 (setq sub
(window-child parent
))
2068 ((not (numberp (window-new-normal sub
)))
2069 (setq sum
(+ sum
(window-normal-size sub horizontal
))))
2071 (setq sum
(+ sum
(window-new-normal sub
)))))
2072 (setq sub
(window-right sub
)))
2073 ;; Don't get larger than 1 or smaller than 0.
2074 (min 1.0 (max (- 1.0 sum
) 0.0))))))
2076 (defun window--resize-child-windows (parent delta
&optional horizontal window ignore trail edge
)
2077 "Resize child windows of window PARENT vertically by DELTA lines.
2078 PARENT must be a vertically combined internal window.
2080 Optional argument HORIZONTAL non-nil means resize child windows of
2081 PARENT horizontally by DELTA columns. In this case PARENT must
2082 be a horizontally combined internal window.
2084 WINDOW, if specified, must denote a child window of PARENT that
2085 is resized by DELTA lines.
2087 Optional argument IGNORE non-nil means ignore restrictions
2088 imposed by fixed size windows, `window-min-height' or
2089 `window-min-width' settings. If IGNORE equals `safe', live
2090 windows may get as small as `window-safe-min-height' lines and
2091 `window-safe-min-width' columns. If IGNORE is a window, ignore
2092 restrictions for that window only. Any other non-nil value means
2093 ignore all of the above restrictions for all windows.
2095 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2096 of windows that shall be resized. If TRAIL equals `before',
2097 resize only windows on the left or above EDGE. If TRAIL equals
2098 `after', resize only windows on the right or below EDGE. Also,
2099 preferably only resize windows adjacent to EDGE.
2101 Return the symbol `normalized' if new normal sizes have been
2102 already set by this routine."
2103 (let* ((first (window-child parent
))
2104 (last (window-last-child parent
))
2105 (parent-total (+ (window-total-size parent horizontal
) delta
))
2106 sub best-window best-value
)
2108 (if (and edge
(memq trail
'(before after
))
2111 (while (and (window-right sub
)
2112 (or (and (eq trail
'before
)
2113 (not (window--resize-child-windows-skip-p
2114 (window-right sub
))))
2115 (and (eq trail
'after
)
2116 (window--resize-child-windows-skip-p sub
))))
2117 (setq sub
(window-right sub
)))
2120 (if (eq trail
'before
)
2121 (= (+ (window-left-column sub
)
2122 (window-total-size sub t
))
2124 (= (window-left-column sub
) edge
))
2125 (if (eq trail
'before
)
2126 (= (+ (window-top-line sub
)
2127 (window-total-size sub
))
2129 (= (window-top-line sub
) edge
)))
2130 (window-sizable-p sub delta horizontal ignore
))
2131 ;; Resize only windows adjacent to EDGE.
2133 (window--resize-this-window
2134 sub delta horizontal ignore t trail edge
)
2135 (if (and window
(eq (window-parent sub
) parent
))
2137 ;; Assign new normal sizes.
2138 (set-window-new-normal
2139 sub
(/ (float (window-new-total sub
)) parent-total
))
2140 (set-window-new-normal
2141 window
(- (window-normal-size window horizontal
)
2142 (- (window-new-normal sub
)
2143 (window-normal-size sub horizontal
)))))
2144 (window--resize-child-windows-normal
2145 parent horizontal sub
0 trail delta
))
2146 ;; Return 'normalized to notify `window--resize-siblings' that
2147 ;; normal sizes have been already set.
2149 ;; Resize all windows proportionally.
2153 ((or (window--resize-child-windows-skip-p sub
)
2154 ;; Ignore windows to skip and fixed-size child windows -
2155 ;; in the latter case make it a window to skip.
2157 (window-size-fixed-p sub horizontal
)
2158 (set-window-new-normal sub
'ignore
))))
2160 ;; When shrinking store the number of lines/cols we can get
2161 ;; from this window here together with the total/normal size
2163 (set-window-new-normal
2166 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
2167 (window-min-delta sub horizontal ignore trail t
) ; t)
2168 (- (/ (float (window-total-size sub horizontal
))
2170 (window-normal-size sub horizontal
)))))
2172 ;; When enlarging store the total/normal size factor only
2173 (set-window-new-normal
2175 (- (/ (float (window-total-size sub horizontal
))
2177 (window-normal-size sub horizontal
)))))
2179 (setq sub
(window-left sub
)))
2183 ;; Shrink windows by delta.
2184 (setq best-window t
)
2185 (while (and best-window
(not (zerop delta
)))
2187 (setq best-window nil
)
2188 (setq best-value most-negative-fixnum
)
2190 (when (and (consp (window-new-normal sub
))
2191 (not (zerop (car (window-new-normal sub
))))
2192 (> (cdr (window-new-normal sub
)) best-value
))
2193 (setq best-window sub
)
2194 (setq best-value
(cdr (window-new-normal sub
))))
2196 (setq sub
(window-left sub
)))
2199 (setq delta
(1+ delta
)))
2200 (set-window-new-total best-window -
1 t
)
2201 (set-window-new-normal
2203 (if (= (car (window-new-normal best-window
)) 1)
2204 'skip
; We can't shrink best-window any further.
2205 (cons (1- (car (window-new-normal best-window
)))
2206 (- (/ (float (window-new-total best-window
))
2208 (window-normal-size best-window horizontal
)))))))
2210 ;; Enlarge windows by delta.
2211 (setq best-window t
)
2212 (while (and best-window
(not (zerop delta
)))
2214 (setq best-window nil
)
2215 (setq best-value most-positive-fixnum
)
2217 (when (and (numberp (window-new-normal sub
))
2218 (< (window-new-normal sub
) best-value
))
2219 (setq best-window sub
)
2220 (setq best-value
(window-new-normal sub
)))
2222 (setq sub
(window-left sub
)))
2225 (setq delta
(1- delta
)))
2226 (set-window-new-total best-window
1 t
)
2227 (set-window-new-normal
2229 (- (/ (float (window-new-total best-window
))
2231 (window-normal-size best-window horizontal
))))))
2236 (when (or (consp (window-new-normal sub
))
2237 (numberp (window-new-normal sub
)))
2238 ;; Reset new normal size fields so `window-resize-apply'
2239 ;; won't use them to apply new sizes.
2240 (set-window-new-normal sub
))
2242 (unless (eq (window-new-normal sub
) 'ignore
)
2243 ;; Resize this window's child windows (back-engineering
2244 ;; delta from sub's old and new total sizes).
2245 (let ((delta (- (window-new-total sub
)
2246 (window-total-size sub horizontal
))))
2247 (unless (and (zerop delta
) (not trail
))
2248 ;; For the TRAIL non-nil case we have to resize SUB
2249 ;; recursively even if it's size does not change.
2250 (window--resize-this-window
2251 sub delta horizontal ignore nil trail edge
))))
2252 (setq sub
(window-left sub
)))))))
2254 (defun window--resize-siblings (window delta
&optional horizontal ignore trail edge
)
2255 "Resize other windows when WINDOW is resized vertically by DELTA lines.
2256 Optional argument HORIZONTAL non-nil means resize other windows
2257 when WINDOW is resized horizontally by DELTA columns. WINDOW
2258 itself is not resized by this function.
2260 Optional argument IGNORE non-nil means ignore restrictions
2261 imposed by fixed size windows, `window-min-height' or
2262 `window-min-width' settings. If IGNORE equals `safe', live
2263 windows may get as small as `window-safe-min-height' lines and
2264 `window-safe-min-width' columns. If IGNORE is a window, ignore
2265 restrictions for that window only. Any other non-nil value means
2266 ignore all of the above restrictions for all windows.
2268 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2269 of windows that shall be resized. If TRAIL equals `before',
2270 resize only windows on the left or above EDGE. If TRAIL equals
2271 `after', resize only windows on the right or below EDGE. Also,
2272 preferably only resize windows adjacent to EDGE."
2273 (when (window-parent window
)
2274 (let* ((parent (window-parent window
))
2275 (sub (window-child parent
)))
2276 (if (window-combined-p sub horizontal
)
2277 ;; In an iso-combination try to extract DELTA from WINDOW's
2279 (let ((skip (eq trail
'after
))
2280 this-delta other-delta
)
2281 ;; Decide which windows shall be left alone.
2285 ;; Make sure WINDOW is left alone when
2286 ;; resizing its siblings.
2287 (set-window-new-normal sub
'ignore
)
2288 (setq skip
(eq trail
'before
)))
2290 ;; Make sure this sibling is left alone when
2291 ;; resizing its siblings.
2292 (set-window-new-normal sub
'ignore
))
2293 ((or (window--size-ignore-p sub ignore
)
2294 (not (window-size-fixed-p sub horizontal
)))
2295 ;; Set this-delta to t to signal that we found a sibling
2296 ;; of WINDOW whose size is not fixed.
2297 (setq this-delta t
)))
2299 (setq sub
(window-right sub
)))
2301 ;; Set this-delta to what we can get from WINDOW's siblings.
2302 (if (= (- delta
) (window-total-size window horizontal
))
2303 ;; A deletion, presumably. We must handle this case
2304 ;; specially since `window--resizable' can't be used.
2306 ;; There's at least one resizable sibling we can
2307 ;; give WINDOW's size to.
2308 (setq this-delta delta
)
2309 ;; No resizable sibling exists.
2310 (setq this-delta
0))
2311 ;; Any other form of resizing.
2313 (window--resizable window delta horizontal ignore trail t
)))
2315 ;; Set other-delta to what we still have to get from
2316 ;; ancestor windows of parent.
2317 (setq other-delta
(- delta this-delta
))
2318 (unless (zerop other-delta
)
2319 ;; Unless we got everything from WINDOW's siblings, PARENT
2320 ;; must be resized by other-delta lines or columns.
2321 (set-window-new-total parent other-delta
'add
))
2323 (if (zerop this-delta
)
2324 ;; We haven't got anything from WINDOW's siblings but we
2325 ;; must update the normal sizes to respect other-delta.
2326 (window--resize-child-windows-normal
2327 parent horizontal window this-delta trail other-delta
)
2328 ;; We did get something from WINDOW's siblings which means
2329 ;; we have to resize their child windows.
2330 (unless (eq (window--resize-child-windows
2331 parent
(- this-delta
) horizontal
2332 window ignore trail edge
)
2333 ;; If `window--resize-child-windows' returns
2334 ;; 'normalized, this means it has set the
2335 ;; normal sizes already.
2337 ;; Set the normal sizes.
2338 (window--resize-child-windows-normal
2339 parent horizontal window this-delta trail other-delta
))
2340 ;; Set DELTA to what we still have to get from ancestor
2342 (setq delta other-delta
)))
2344 ;; In an ortho-combination all siblings of WINDOW must be
2345 ;; resized by DELTA.
2346 (set-window-new-total parent delta
'add
)
2348 (unless (eq sub window
)
2349 (window--resize-this-window sub delta horizontal ignore t
))
2350 (setq sub
(window-right sub
))))
2352 (unless (zerop delta
)
2354 (window--resize-siblings
2355 parent delta horizontal ignore trail edge
)))))
2357 (defun window--resize-this-window (window delta
&optional horizontal ignore add trail edge
)
2358 "Resize WINDOW vertically by DELTA lines.
2359 Optional argument HORIZONTAL non-nil means resize WINDOW
2360 horizontally by DELTA columns.
2362 Optional argument IGNORE non-nil means ignore restrictions
2363 imposed by fixed size windows, `window-min-height' or
2364 `window-min-width' settings. If IGNORE equals `safe', live
2365 windows may get as small as `window-safe-min-height' lines and
2366 `window-safe-min-width' columns. If IGNORE is a window, ignore
2367 restrictions for that window only. Any other non-nil value
2368 means ignore all of the above restrictions for all windows.
2370 Optional argument ADD non-nil means add DELTA to the new total
2373 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2374 of windows that shall be resized. If TRAIL equals `before',
2375 resize only windows on the left or above EDGE. If TRAIL equals
2376 `after', resize only windows on the right or below EDGE. Also,
2377 preferably only resize windows adjacent to EDGE.
2379 This function recursively resizes WINDOW's child windows to fit the
2380 new size. Make sure that WINDOW is `window--resizable' before
2381 calling this function. Note that this function does not resize
2382 siblings of WINDOW or WINDOW's parent window. You have to
2383 eventually call `window-resize-apply' in order to make resizing
2384 actually take effect."
2386 ;; Add DELTA to the new total size of WINDOW.
2387 (set-window-new-total window delta t
))
2389 (let ((sub (window-child window
)))
2392 ((window-combined-p sub horizontal
)
2393 ;; In an iso-combination resize child windows according to their
2395 (window--resize-child-windows
2396 window delta horizontal nil ignore trail edge
))
2397 ;; In an ortho-combination resize each child window by DELTA.
2400 (window--resize-this-window
2401 sub delta horizontal ignore t trail edge
)
2402 (setq sub
(window-right sub
)))))))
2404 (defun window--resize-root-window (window delta horizontal ignore
)
2405 "Resize root window WINDOW vertically by DELTA lines.
2406 HORIZONTAL non-nil means resize root window WINDOW horizontally
2409 IGNORE non-nil means ignore any restrictions imposed by fixed
2410 size windows, `window-min-height' or `window-min-width' settings.
2412 This function is only called by the frame resizing routines. It
2413 resizes windows proportionally and never deletes any windows."
2414 (when (and (windowp window
) (numberp delta
)
2415 (window-sizable-p window delta horizontal ignore
))
2416 (window--resize-reset (window-frame window
) horizontal
)
2417 (window--resize-this-window window delta horizontal ignore t
)))
2419 (defun window--resize-root-window-vertically (window delta
)
2420 "Resize root window WINDOW vertically by DELTA lines.
2421 If DELTA is less than zero and we can't shrink WINDOW by DELTA
2422 lines, shrink it as much as possible. If DELTA is greater than
2423 zero, this function can resize fixed-size windows in order to
2424 recover the necessary lines.
2426 Return the number of lines that were recovered.
2428 This function is only called by the minibuffer window resizing
2429 routines. It resizes windows proportionally and never deletes
2431 (let ((frame (window-frame window
))
2434 ((not (numberp delta
))
2438 (setq delta
(window-sizable window delta
))
2439 (window--resize-reset frame
)
2440 ;; When shrinking the root window, emulate an edge drag in order
2441 ;; to not resize other windows if we can avoid it (Bug#12419).
2442 (window--resize-this-window
2443 window delta nil ignore t
'before
2444 (+ (window-top-line window
) (window-total-size window
)))
2445 ;; Don't record new normal sizes to make sure that shrinking back
2446 ;; proportionally works as intended.
2448 (lambda (window) (set-window-new-normal window
'ignore
)) frame t
))
2450 (window--resize-reset frame
)
2451 (unless (window-sizable window delta
)
2453 ;; When growing the root window, resize proportionally. This
2454 ;; should give windows back their original sizes (hopefully).
2455 (window--resize-this-window window delta nil ignore t
)))
2456 ;; Return the possibly adjusted DELTA.
2459 (defun adjust-window-trailing-edge (window delta
&optional horizontal
)
2460 "Move WINDOW's bottom edge by DELTA lines.
2461 Optional argument HORIZONTAL non-nil means move WINDOW's right
2462 edge by DELTA columns. WINDOW must be a valid window and
2463 defaults to the selected one.
2465 If DELTA is greater than zero, move the edge downwards or to the
2466 right. If DELTA is less than zero, move the edge upwards or to
2467 the left. If the edge can't be moved by DELTA lines or columns,
2468 move it as far as possible in the desired direction."
2469 (setq window
(window-normalize-window window
))
2470 (let* ((frame (window-frame window
))
2471 (minibuffer-window (minibuffer-window frame
))
2473 left this-delta min-delta max-delta
)
2474 ;; Find the edge we want to move.
2475 (while (and (or (not (window-combined-p right horizontal
))
2476 (not (window-right right
)))
2477 (setq right
(window-parent right
))))
2479 ((and (not right
) (not horizontal
)
2480 ;; Resize the minibuffer window if it's on the same frame as
2481 ;; and immediately below WINDOW and it's either active or
2482 ;; `resize-mini-windows' is nil.
2483 (eq (window-frame minibuffer-window
) frame
)
2484 (= (nth 1 (window-edges minibuffer-window
))
2485 (nth 3 (window-edges window
)))
2486 (or (not resize-mini-windows
)
2487 (eq minibuffer-window
(active-minibuffer-window))))
2488 (window--resize-mini-window minibuffer-window
(- delta
)))
2489 ((or (not (setq left right
)) (not (setq right
(window-right right
))))
2491 (error "No window on the right of this one")
2492 (error "No window below this one")))
2494 ;; Set LEFT to the first resizable window on the left. This step is
2495 ;; needed to handle fixed-size windows.
2496 (while (and left
(window-size-fixed-p left horizontal
))
2498 (or (window-left left
)
2500 (while (and (setq left
(window-parent left
))
2501 (not (window-combined-p left horizontal
))))
2502 (window-left left
)))))
2505 (error "No resizable window on the left of this one")
2506 (error "No resizable window above this one")))
2508 ;; Set RIGHT to the first resizable window on the right. This step
2509 ;; is needed to handle fixed-size windows.
2510 (while (and right
(window-size-fixed-p right horizontal
))
2512 (or (window-right right
)
2514 (while (and (setq right
(window-parent right
))
2515 (not (window-combined-p right horizontal
))))
2516 (window-right right
)))))
2519 (error "No resizable window on the right of this one")
2520 (error "No resizable window below this one")))
2522 ;; LEFT and RIGHT (which might be both internal windows) are now the
2523 ;; two windows we want to resize.
2526 (setq max-delta
(window--max-delta-1 left
0 horizontal nil
'after
))
2527 (setq min-delta
(window--min-delta-1 right
(- delta
) horizontal nil
'before
))
2528 (when (or (< max-delta delta
) (> min-delta
(- delta
)))
2529 ;; We can't get the whole DELTA - move as far as possible.
2530 (setq delta
(min max-delta
(- min-delta
))))
2531 (unless (zerop delta
)
2533 (window--resize-reset frame horizontal
)
2534 ;; Try to enlarge LEFT first.
2535 (setq this-delta
(window--resizable left delta horizontal
))
2536 (unless (zerop this-delta
)
2537 (window--resize-this-window
2538 left this-delta horizontal nil t
'before
2540 (+ (window-left-column left
) (window-total-size left t
))
2541 (+ (window-top-line left
) (window-total-size left
)))))
2542 ;; Shrink windows on right of LEFT.
2543 (window--resize-siblings
2544 left delta horizontal nil
'after
2546 (window-left-column right
)
2547 (window-top-line right
)))))
2549 (setq max-delta
(window--max-delta-1 right
0 horizontal nil
'before
))
2550 (setq min-delta
(window--min-delta-1 left delta horizontal nil
'after
))
2551 (when (or (< max-delta
(- delta
)) (> min-delta delta
))
2552 ;; We can't get the whole DELTA - move as far as possible.
2553 (setq delta
(max (- max-delta
) min-delta
)))
2554 (unless (zerop delta
)
2556 (window--resize-reset frame horizontal
)
2557 ;; Try to enlarge RIGHT.
2558 (setq this-delta
(window--resizable right
(- delta
) horizontal
))
2559 (unless (zerop this-delta
)
2560 (window--resize-this-window
2561 right this-delta horizontal nil t
'after
2563 (window-left-column right
)
2564 (window-top-line right
))))
2565 ;; Shrink windows on left of RIGHT.
2566 (window--resize-siblings
2567 right
(- delta
) horizontal nil
'before
2569 (+ (window-left-column left
) (window-total-size left t
))
2570 (+ (window-top-line left
) (window-total-size left
)))))))
2571 (unless (zerop delta
)
2572 ;; Don't report an error in the standard case.
2573 (unless (window-resize-apply frame horizontal
)
2574 ;; But do report an error if applying the changes fails.
2575 (error "Failed adjusting window %s" window
)))))))
2577 (defun enlarge-window (delta &optional horizontal
)
2578 "Make the selected window DELTA lines taller.
2579 Interactively, if no argument is given, make the selected window
2580 one line taller. If optional argument HORIZONTAL is non-nil,
2581 make selected window wider by DELTA columns. If DELTA is
2582 negative, shrink selected window by -DELTA lines or columns."
2584 (let ((minibuffer-window (minibuffer-window)))
2587 ((window-size-fixed-p nil horizontal
)
2588 (error "Selected window has fixed size"))
2589 ((window-minibuffer-p)
2591 (error "Cannot resize minibuffer window horizontally")
2592 (window--resize-mini-window (selected-window) delta
)))
2593 ((and (not horizontal
)
2594 (window-full-height-p)
2595 (eq (window-frame minibuffer-window
) (selected-frame))
2596 (not resize-mini-windows
))
2597 ;; If the selected window is full height and `resize-mini-windows'
2598 ;; is nil, resize the minibuffer window.
2599 (window--resize-mini-window minibuffer-window
(- delta
)))
2600 ((window-resizable-p nil delta horizontal
)
2601 (window-resize nil delta horizontal
))
2605 (window-max-delta nil horizontal
)
2606 (- (window-min-delta nil horizontal
)))
2609 (defun shrink-window (delta &optional horizontal
)
2610 "Make the selected window DELTA lines smaller.
2611 Interactively, if no argument is given, make the selected window
2612 one line smaller. If optional argument HORIZONTAL is non-nil,
2613 make selected window narrower by DELTA columns. If DELTA is
2614 negative, enlarge selected window by -DELTA lines or columns.
2615 Also see the `window-min-height' variable."
2617 (let ((minibuffer-window (minibuffer-window)))
2620 ((window-size-fixed-p nil horizontal
)
2621 (error "Selected window has fixed size"))
2622 ((window-minibuffer-p)
2624 (error "Cannot resize minibuffer window horizontally")
2625 (window--resize-mini-window (selected-window) (- delta
))))
2626 ((and (not horizontal
)
2627 (window-full-height-p)
2628 (eq (window-frame minibuffer-window
) (selected-frame))
2629 (not resize-mini-windows
))
2630 ;; If the selected window is full height and `resize-mini-windows'
2631 ;; is nil, resize the minibuffer window.
2632 (window--resize-mini-window minibuffer-window delta
))
2633 ((window-resizable-p nil
(- delta
) horizontal
)
2634 (window-resize nil
(- delta
) horizontal
))
2638 (- (window-min-delta nil horizontal
))
2639 (window-max-delta nil horizontal
))
2642 (defun maximize-window (&optional window
)
2644 Make WINDOW as large as possible without deleting any windows.
2645 WINDOW must be a valid window and defaults to the selected one."
2647 (setq window
(window-normalize-window window
))
2648 (window-resize window
(window-max-delta window
))
2649 (window-resize window
(window-max-delta window t
) t
))
2651 (defun minimize-window (&optional window
)
2653 Make WINDOW as small as possible without deleting any windows.
2654 WINDOW must be a valid window and defaults to the selected one."
2656 (setq window
(window-normalize-window window
))
2657 (window-resize window
(- (window-min-delta window
)))
2658 (window-resize window
(- (window-min-delta window t
)) t
))
2660 (defun frame-root-window-p (window)
2661 "Return non-nil if WINDOW is the root window of its frame."
2662 (eq window
(frame-root-window window
)))
2664 (defun window--subtree (window &optional next
)
2665 "Return window subtree rooted at WINDOW.
2666 Optional argument NEXT non-nil means include WINDOW's right
2667 siblings in the return value.
2669 See the documentation of `window-tree' for a description of the
2676 ((window-top-child window
)
2677 (cons t
(cons (window-edges window
)
2678 (window--subtree (window-top-child window
) t
))))
2679 ((window-left-child window
)
2680 (cons nil
(cons (window-edges window
)
2681 (window--subtree (window-left-child window
) t
))))
2684 (setq window
(when next
(window-next-sibling window
))))
2687 (defun window-tree (&optional frame
)
2688 "Return the window tree of frame FRAME.
2689 FRAME must be a live frame and defaults to the selected frame.
2690 The return value is a list of the form (ROOT MINI), where ROOT
2691 represents the window tree of the frame's root window, and MINI
2692 is the frame's minibuffer window.
2694 If the root window is not split, ROOT is the root window itself.
2695 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
2696 for a horizontal split, and t for a vertical split. EDGES gives
2697 the combined size and position of the child windows in the split,
2698 and the rest of the elements are the child windows in the split.
2699 Each of the child windows may again be a window or a list
2700 representing a window split, and so on. EDGES is a list (LEFT
2701 TOP RIGHT BOTTOM) as returned by `window-edges'."
2702 (setq frame
(window-normalize-frame frame
))
2703 (window--subtree (frame-root-window frame
) t
))
2705 (defun other-window (count &optional all-frames
)
2706 "Select another window in cyclic ordering of windows.
2707 COUNT specifies the number of windows to skip, starting with the
2708 selected window, before making the selection. If COUNT is
2709 positive, skip COUNT windows forwards. If COUNT is negative,
2710 skip -COUNT windows backwards. COUNT zero means do not skip any
2711 window, so select the selected window. In an interactive call,
2712 COUNT is the numeric prefix argument. Return nil.
2714 If the `other-window' parameter of the selected window is a
2715 function and `ignore-window-parameters' is nil, call that
2716 function with the arguments COUNT and ALL-FRAMES.
2718 This function does not select a window whose `no-other-window'
2719 window parameter is non-nil.
2721 This function uses `next-window' for finding the window to
2722 select. The argument ALL-FRAMES has the same meaning as in
2723 `next-window', but the MINIBUF argument of `next-window' is
2724 always effectively nil."
2726 (let* ((window (selected-window))
2727 (function (and (not ignore-window-parameters
)
2728 (window-parameter window
'other-window
)))
2729 old-window old-count
)
2730 (if (functionp function
)
2731 (funcall function count all-frames
)
2732 ;; `next-window' and `previous-window' may return a window we are
2733 ;; not allowed to select. Hence we need an exit strategy in case
2734 ;; all windows are non-selectable.
2737 (setq window
(next-window window nil all-frames
))
2739 ((eq window old-window
)
2740 (when (= count old-count
)
2741 ;; Keep out of infinite loops. When COUNT has not changed
2742 ;; since we last looked at `window' we're probably in one.
2744 ((window-parameter window
'no-other-window
)
2746 ;; The first non-selectable window `next-window' got us:
2747 ;; Remember it and the current value of COUNT.
2748 (setq old-window window
)
2749 (setq old-count count
)))
2751 (setq count
(1- count
)))))
2753 (setq window
(previous-window window nil all-frames
))
2755 ((eq window old-window
)
2756 (when (= count old-count
)
2757 ;; Keep out of infinite loops. When COUNT has not changed
2758 ;; since we last looked at `window' we're probably in one.
2760 ((window-parameter window
'no-other-window
)
2762 ;; The first non-selectable window `previous-window' got
2763 ;; us: Remember it and the current value of COUNT.
2764 (setq old-window window
)
2765 (setq old-count count
)))
2767 (setq count
(1+ count
)))))
2769 (select-window window
)
2770 ;; Always return nil.
2773 ;; This should probably return non-nil when the selected window is part
2774 ;; of an atomic window whose root is the frame's root window.
2775 (defun one-window-p (&optional nomini all-frames
)
2776 "Return non-nil if the selected window is the only window.
2777 Optional arg NOMINI non-nil means don't count the minibuffer
2778 even if it is active. Otherwise, the minibuffer is counted
2781 Optional argument ALL-FRAMES specifies the set of frames to
2782 consider, see also `next-window'. ALL-FRAMES nil or omitted
2783 means consider windows on the selected frame only, plus the
2784 minibuffer window if specified by the NOMINI argument. If the
2785 minibuffer counts, consider all windows on all frames that share
2786 that minibuffer too. The remaining non-nil values of ALL-FRAMES
2787 with a special meaning are:
2789 - t means consider all windows on all existing frames.
2791 - `visible' means consider all windows on all visible frames on
2792 the current terminal.
2794 - 0 (the number zero) means consider all windows on all visible
2795 and iconified frames on the current terminal.
2797 - A frame means consider all windows on that frame only.
2799 Anything else means consider all windows on the selected frame
2801 (let ((base-window (selected-window)))
2802 (if (and nomini
(eq base-window
(minibuffer-window)))
2803 (setq base-window
(next-window base-window
)))
2805 (next-window base-window
(if nomini
'arg
) all-frames
))))
2807 ;;; Deleting windows.
2808 (defun window-deletable-p (&optional window
)
2809 "Return t if WINDOW can be safely deleted from its frame.
2810 WINDOW must be a valid window and defaults to the selected one.
2811 Return `frame' if deleting WINDOW should also delete its frame."
2812 (setq window
(window-normalize-window window
))
2814 (unless ignore-window-parameters
2815 ;; Handle atomicity.
2816 (when (window-parameter window
'window-atom
)
2817 (setq window
(window-atom-root window
))))
2819 (let ((frame (window-frame window
)))
2821 ((frame-root-window-p window
)
2822 ;; WINDOW's frame can be deleted only if there are other frames
2823 ;; on the same terminal, and it does not contain the active
2825 (unless (or (eq frame
(next-frame frame
0))
2826 (let ((minibuf (active-minibuffer-window)))
2827 (and minibuf
(eq frame
(window-frame minibuf
)))))
2829 ((or ignore-window-parameters
2830 (not (eq window
(window--major-non-side-window frame
))))
2831 ;; WINDOW can be deleted unless it is the major non-side window of
2835 (defun window--in-subtree-p (window root
)
2836 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
2837 (or (eq window root
)
2838 (let ((parent (window-parent window
)))
2841 (if (eq parent root
)
2843 (setq parent
(window-parent parent
))))))))
2845 (defun delete-window (&optional window
)
2847 WINDOW must be a valid window and defaults to the selected one.
2850 If the variable `ignore-window-parameters' is non-nil or the
2851 `delete-window' parameter of WINDOW equals t, do not process any
2852 parameters of WINDOW. Otherwise, if the `delete-window'
2853 parameter of WINDOW specifies a function, call that function with
2854 WINDOW as its sole argument and return the value returned by that
2857 Otherwise, if WINDOW is part of an atomic window, call
2858 `delete-window' with the root of the atomic window as its
2859 argument. Signal an error if WINDOW is either the only window on
2860 its frame, the last non-side window, or part of an atomic window
2861 that is its frame's root window."
2863 (setq window
(window-normalize-window window
))
2864 (let* ((frame (window-frame window
))
2865 (function (window-parameter window
'delete-window
))
2866 (parent (window-parent window
))
2868 (window--check frame
)
2870 ;; Handle window parameters.
2872 ;; Ignore window parameters if `ignore-window-parameters' tells
2873 ;; us so or `delete-window' equals t.
2874 ((or ignore-window-parameters
(eq function t
)))
2875 ((functionp function
)
2876 ;; The `delete-window' parameter specifies the function to call.
2877 ;; If that function is `ignore' nothing is done. It's up to the
2878 ;; function called here to avoid infinite recursion.
2879 (throw 'done
(funcall function window
)))
2880 ((and (window-parameter window
'window-atom
)
2881 (setq atom-root
(window-atom-root window
))
2882 (not (eq atom-root window
)))
2883 (if (eq atom-root
(frame-root-window frame
))
2884 (error "Root of atomic window is root window of its frame")
2885 (throw 'done
(delete-window atom-root
))))
2887 (error "Attempt to delete minibuffer or sole ordinary window"))
2888 ((eq window
(window--major-non-side-window frame
))
2889 (error "Attempt to delete last non-side window")))
2891 (let* ((horizontal (window-left-child parent
))
2892 (size (window-total-size window horizontal
))
2894 (window--in-subtree-p (frame-selected-window frame
) window
))
2895 ;; Emacs 23 preferably gives WINDOW's space to its left
2897 (sibling (or (window-left window
) (window-right window
))))
2898 (window--resize-reset frame horizontal
)
2900 ((and (not window-combination-resize
)
2901 sibling
(window-sizable-p sibling size
))
2902 ;; Resize WINDOW's sibling.
2903 (window--resize-this-window sibling size horizontal nil t
)
2904 (set-window-new-normal
2905 sibling
(+ (window-normal-size sibling horizontal
)
2906 (window-normal-size window horizontal
))))
2907 ((window-resizable-p window
(- size
) horizontal nil nil nil t
)
2908 ;; Can do without resizing fixed-size windows.
2909 (window--resize-siblings window
(- size
) horizontal
))
2911 ;; Can't do without resizing fixed-size windows.
2912 (window--resize-siblings window
(- size
) horizontal t
)))
2913 ;; Actually delete WINDOW.
2914 (delete-window-internal window
)
2915 (when (and frame-selected
2917 (frame-selected-window frame
) 'no-other-window
))
2918 ;; `delete-window-internal' has selected a window that should
2919 ;; not be selected, fix this here.
2920 (other-window -
1 frame
))
2921 (run-window-configuration-change-hook frame
)
2922 (window--check frame
)
2923 ;; Always return nil.
2926 (defun delete-other-windows (&optional window
)
2927 "Make WINDOW fill its frame.
2928 WINDOW must be a valid window and defaults to the selected one.
2931 If the variable `ignore-window-parameters' is non-nil or the
2932 `delete-other-windows' parameter of WINDOW equals t, do not
2933 process any parameters of WINDOW. Otherwise, if the
2934 `delete-other-windows' parameter of WINDOW specifies a function,
2935 call that function with WINDOW as its sole argument and return
2936 the value returned by that function.
2938 Otherwise, if WINDOW is part of an atomic window, call this
2939 function with the root of the atomic window as its argument. If
2940 WINDOW is a non-side window, make WINDOW the only non-side window
2941 on the frame. Side windows are not deleted. If WINDOW is a side
2942 window signal an error."
2944 (setq window
(window-normalize-window window
))
2945 (let* ((frame (window-frame window
))
2946 (function (window-parameter window
'delete-other-windows
))
2947 (window-side (window-parameter window
'window-side
))
2948 atom-root side-main
)
2949 (window--check frame
)
2952 ;; Ignore window parameters if `ignore-window-parameters' is t or
2953 ;; `delete-other-windows' is t.
2954 ((or ignore-window-parameters
(eq function t
)))
2955 ((functionp function
)
2956 ;; The `delete-other-windows' parameter specifies the function
2957 ;; to call. If the function is `ignore' no windows are deleted.
2958 ;; It's up to the function called to avoid infinite recursion.
2959 (throw 'done
(funcall function window
)))
2960 ((and (window-parameter window
'window-atom
)
2961 (setq atom-root
(window-atom-root window
))
2962 (not (eq atom-root window
)))
2963 (if (eq atom-root
(frame-root-window frame
))
2964 (error "Root of atomic window is root window of its frame")
2965 (throw 'done
(delete-other-windows atom-root
))))
2966 ((memq window-side window-sides
)
2967 (error "Cannot make side window the only window"))
2968 ((and (window-minibuffer-p window
)
2969 (not (eq window
(frame-root-window window
))))
2970 (error "Can't expand minibuffer to full frame")))
2972 ;; If WINDOW is the major non-side window, do nothing.
2973 (if (window-with-parameter 'window-side
)
2974 (setq side-main
(window--major-non-side-window frame
))
2975 (setq side-main
(frame-root-window frame
)))
2976 (unless (eq window side-main
)
2977 (delete-other-windows-internal window side-main
)
2978 (run-window-configuration-change-hook frame
)
2979 (window--check frame
))
2980 ;; Always return nil.
2983 (defun delete-other-windows-vertically (&optional window
)
2984 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
2985 This may be a useful alternative binding for \\[delete-other-windows]
2986 if you often split windows horizontally."
2988 (let* ((window (or window
(selected-window)))
2989 (edges (window-edges window
))
2991 (while (not (eq (setq w
(next-window w
1)) window
))
2992 (let ((e (window-edges w
)))
2993 (when (and (= (car e
) (car edges
))
2994 (= (nth 2 e
) (nth 2 edges
)))
2996 (mapc 'delete-window delenda
)))
2998 ;;; Windows and buffers.
3000 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
3001 ;; for (1) determining which buffer to show in the window when its
3002 ;; buffer shall be buried or killed and (2) which buffer to show for
3003 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3005 ;; `prev-buffers' consists of <buffer, window-start, window-point>
3006 ;; triples. The entries on this list are ordered by the time their
3007 ;; buffer has been removed from the window, the most recently removed
3008 ;; buffer's entry being first. The window-start and window-point
3009 ;; components are `window-start' and `window-point' at the time the
3010 ;; buffer was removed from the window which implies that the entry must
3011 ;; be added when `set-window-buffer' removes the buffer from the window.
3013 ;; `next-buffers' is the list of buffers that have been replaced
3014 ;; recently by `switch-to-prev-buffer'. These buffers are the least
3015 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
3016 ;; candidates of `switch-to-next-buffer' to switch to. This list is
3017 ;; reset to nil by any action changing the window's buffer with the
3018 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
3019 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
3020 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
3022 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
3023 ;; if such a buffer was killed while the window was hidden within a
3024 ;; window configuration. Such killed buffers get removed whenever
3025 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
3027 ;; The following function is called by `set-window-buffer' _before_ it
3028 ;; replaces the buffer of the argument window with the new buffer.
3029 (defun record-window-buffer (&optional window
)
3030 "Record WINDOW's buffer.
3031 WINDOW must be a live window and defaults to the selected one."
3032 (let* ((window (window-normalize-window window t
))
3033 (buffer (window-buffer window
))
3034 (entry (assq buffer
(window-prev-buffers window
))))
3035 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
3036 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3037 (set-window-next-buffers window nil
)
3040 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
3041 (set-window-prev-buffers
3042 window
(assq-delete-all buffer
(window-prev-buffers window
))))
3044 ;; Don't record insignificant buffers.
3045 (unless (eq (aref (buffer-name buffer
) 0) ?\s
)
3046 ;; Add an entry for buffer to WINDOW's previous buffers.
3047 (with-current-buffer buffer
3048 (let ((start (window-start window
))
3049 (point (window-point window
)))
3053 ;; We have an entry, update marker positions.
3054 (list (set-marker (nth 1 entry
) start
)
3055 (set-marker (nth 2 entry
) point
))
3056 ;; Make new markers.
3057 (list (copy-marker start
)
3059 ;; Preserve window-point-insertion-type
3061 point window-point-insertion-type
)))))
3062 (set-window-prev-buffers
3063 window
(cons entry
(window-prev-buffers window
))))))))
3065 (defun unrecord-window-buffer (&optional window buffer
)
3066 "Unrecord BUFFER in WINDOW.
3067 WINDOW must be a live window and defaults to the selected one.
3068 BUFFER must be a live buffer and defaults to the buffer of
3070 (let* ((window (window-normalize-window window t
))
3071 (buffer (or buffer
(window-buffer window
))))
3072 (set-window-prev-buffers
3073 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3074 (set-window-next-buffers
3075 window
(delq buffer
(window-next-buffers window
)))))
3077 (defun set-window-buffer-start-and-point (window buffer
&optional start point
)
3078 "Set WINDOW's buffer to BUFFER.
3079 WINDOW must be a live window and defaults to the selected one.
3080 Optional argument START non-nil means set WINDOW's start position
3081 to START. Optional argument POINT non-nil means set WINDOW's
3082 point to POINT. If WINDOW is selected this also sets BUFFER's
3083 `point' to POINT. If WINDOW is selected and the buffer it showed
3084 before was current this also makes BUFFER the current buffer."
3085 (setq window
(window-normalize-window window t
))
3086 (let ((selected (eq window
(selected-window)))
3087 (current (eq (window-buffer window
) (current-buffer))))
3088 (set-window-buffer window buffer
)
3089 (when (and selected current
)
3090 (set-buffer buffer
))
3092 ;; Don't force window-start here (even if POINT is nil).
3093 (set-window-start window start t
))
3095 (set-window-point window point
))))
3097 (defcustom switch-to-visible-buffer t
3098 "If non-nil, allow switching to an already visible buffer.
3099 If this variable is non-nil, `switch-to-prev-buffer' and
3100 `switch-to-next-buffer' may switch to an already visible buffer
3101 provided the buffer was shown before in the window specified as
3102 argument to those functions. If this variable is nil,
3103 `switch-to-prev-buffer' and `switch-to-next-buffer' always try to
3104 avoid switching to a buffer that is already visible in another
3105 window on the same frame."
3110 (defun switch-to-prev-buffer (&optional window bury-or-kill
)
3111 "In WINDOW switch to previous buffer.
3112 WINDOW must be a live window and defaults to the selected one.
3113 Return the buffer switched to, nil if no suitable buffer could be
3116 Optional argument BURY-OR-KILL non-nil means the buffer currently
3117 shown in WINDOW is about to be buried or killed and consequently
3118 shall not be switched to in future invocations of this command.
3120 As a special case, if BURY-OR-KILL equals `append', this means to
3121 move the buffer to the end of WINDOW's previous buffers list so a
3122 future invocation of `switch-to-prev-buffer' less likely switches
3125 (let* ((window (window-normalize-window window t
))
3126 (frame (window-frame window
))
3127 (old-buffer (window-buffer window
))
3128 ;; Save this since it's destroyed by `set-window-buffer'.
3129 (next-buffers (window-next-buffers window
))
3130 (pred (frame-parameter frame
'buffer-predicate
))
3131 entry new-buffer killed-buffers visible
)
3132 (when (window-minibuffer-p window
)
3133 ;; Don't switch in minibuffer window.
3134 (unless (setq window
(minibuffer-selected-window))
3135 (error "Window %s is a minibuffer window" window
)))
3137 (when (window-dedicated-p window
)
3138 ;; Don't switch in dedicated window.
3139 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3142 ;; Scan WINDOW's previous buffers first, skipping entries of next
3144 (dolist (entry (window-prev-buffers window
))
3145 (when (and (setq new-buffer
(car entry
))
3146 (or (buffer-live-p new-buffer
)
3147 (not (setq killed-buffers
3148 (cons new-buffer killed-buffers
))))
3149 (not (eq new-buffer old-buffer
))
3150 (or (null pred
) (funcall pred new-buffer
))
3151 ;; When BURY-OR-KILL is nil, avoid switching to a
3152 ;; buffer in WINDOW's next buffers list.
3153 (or bury-or-kill
(not (memq new-buffer next-buffers
))))
3154 (if (and (not switch-to-visible-buffer
)
3155 (get-buffer-window new-buffer frame
))
3156 ;; Try to avoid showing a buffer visible in some other
3158 (setq visible new-buffer
)
3159 (set-window-buffer-start-and-point
3160 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3162 ;; Scan reverted buffer list of WINDOW's frame next, skipping
3163 ;; entries of next buffers. Note that when we bury or kill a
3164 ;; buffer we don't reverse the global buffer list to avoid showing
3165 ;; a buried buffer instead. Otherwise, we must reverse the global
3166 ;; buffer list in order to make sure that switching to the
3167 ;; previous/next buffer traverse it in opposite directions.
3168 (dolist (buffer (if bury-or-kill
3170 (nreverse (buffer-list frame
))))
3171 (when (and (buffer-live-p buffer
)
3172 (not (eq buffer old-buffer
))
3173 (or (null pred
) (funcall pred buffer
))
3174 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3175 (or bury-or-kill
(not (memq buffer next-buffers
))))
3176 (if (get-buffer-window buffer frame
)
3177 ;; Try to avoid showing a buffer visible in some other window.
3179 (setq visible buffer
))
3180 (setq new-buffer buffer
)
3181 (set-window-buffer-start-and-point window new-buffer
)
3183 (unless bury-or-kill
3184 ;; Scan reverted next buffers last (must not use nreverse
3186 (dolist (buffer (reverse next-buffers
))
3187 ;; Actually, buffer _must_ be live here since otherwise it
3188 ;; would have been caught in the scan of previous buffers.
3189 (when (and (or (buffer-live-p buffer
)
3190 (not (setq killed-buffers
3191 (cons buffer killed-buffers
))))
3192 (not (eq buffer old-buffer
))
3193 (or (null pred
) (funcall pred buffer
))
3194 (setq entry
(assq buffer
(window-prev-buffers window
))))
3195 (setq new-buffer buffer
)
3196 (set-window-buffer-start-and-point
3197 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3200 ;; Show a buffer visible in another window.
3202 (setq new-buffer visible
)
3203 (set-window-buffer-start-and-point window new-buffer
)))
3206 (let ((entry (and (eq bury-or-kill
'append
)
3207 (assq old-buffer
(window-prev-buffers window
)))))
3208 ;; Remove `old-buffer' from WINDOW's previous and (restored list
3209 ;; of) next buffers.
3210 (set-window-prev-buffers
3211 window
(assq-delete-all old-buffer
(window-prev-buffers window
)))
3212 (set-window-next-buffers window
(delq old-buffer next-buffers
))
3214 ;; Append old-buffer's entry to list of WINDOW's previous
3215 ;; buffers so it's less likely to get switched to soon but
3216 ;; `display-buffer-in-previous-window' can nevertheless find
3218 (set-window-prev-buffers
3219 window
(append (window-prev-buffers window
) (list entry
)))))
3220 ;; Move `old-buffer' to head of WINDOW's restored list of next
3222 (set-window-next-buffers
3223 window
(cons old-buffer
(delq old-buffer next-buffers
))))
3225 ;; Remove killed buffers from WINDOW's previous and next buffers.
3226 (when killed-buffers
3227 (dolist (buffer killed-buffers
)
3228 (set-window-prev-buffers
3229 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3230 (set-window-next-buffers
3231 window
(delq buffer
(window-next-buffers window
)))))
3233 ;; Return new-buffer.
3236 (defun switch-to-next-buffer (&optional window
)
3237 "In WINDOW switch to next buffer.
3238 WINDOW must be a live window and defaults to the selected one.
3239 Return the buffer switched to, nil if no suitable buffer could be
3242 (let* ((window (window-normalize-window window t
))
3243 (frame (window-frame window
))
3244 (old-buffer (window-buffer window
))
3245 (next-buffers (window-next-buffers window
))
3246 (pred (frame-parameter frame
'buffer-predicate
))
3247 new-buffer entry killed-buffers visible
)
3248 (when (window-minibuffer-p window
)
3249 ;; Don't switch in minibuffer window.
3250 (unless (setq window
(minibuffer-selected-window))
3251 (error "Window %s is a minibuffer window" window
)))
3253 (when (window-dedicated-p window
)
3254 ;; Don't switch in dedicated window.
3255 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3258 ;; Scan WINDOW's next buffers first.
3259 (dolist (buffer next-buffers
)
3260 (when (and (or (buffer-live-p buffer
)
3261 (not (setq killed-buffers
3262 (cons buffer killed-buffers
))))
3263 (not (eq buffer old-buffer
))
3264 (or (null pred
) (funcall pred buffer
))
3265 (setq entry
(assq buffer
(window-prev-buffers window
))))
3266 (setq new-buffer buffer
)
3267 (set-window-buffer-start-and-point
3268 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3270 ;; Scan the buffer list of WINDOW's frame next, skipping previous
3272 (dolist (buffer (buffer-list frame
))
3273 (when (and (buffer-live-p buffer
)
3274 (not (eq buffer old-buffer
))
3275 (or (null pred
) (funcall pred buffer
))
3276 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3277 (not (assq buffer
(window-prev-buffers window
))))
3278 (if (get-buffer-window buffer frame
)
3279 ;; Try to avoid showing a buffer visible in some other window.
3280 (setq visible buffer
)
3281 (setq new-buffer buffer
)
3282 (set-window-buffer-start-and-point window new-buffer
)
3284 ;; Scan WINDOW's reverted previous buffers last (must not use
3286 (dolist (entry (reverse (window-prev-buffers window
)))
3287 (when (and (setq new-buffer
(car entry
))
3288 (or (buffer-live-p new-buffer
)
3289 (not (setq killed-buffers
3290 (cons new-buffer killed-buffers
))))
3291 (not (eq new-buffer old-buffer
))
3292 (or (null pred
) (funcall pred new-buffer
)))
3293 (if (and (not switch-to-visible-buffer
)
3294 (get-buffer-window new-buffer frame
))
3295 ;; Try to avoid showing a buffer visible in some other window.
3297 (setq visible new-buffer
))
3298 (set-window-buffer-start-and-point
3299 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3302 ;; Show a buffer visible in another window.
3304 (setq new-buffer visible
)
3305 (set-window-buffer-start-and-point window new-buffer
)))
3307 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
3308 (set-window-next-buffers window
(delq new-buffer next-buffers
))
3310 ;; Remove killed buffers from WINDOW's previous and next buffers.
3311 (when killed-buffers
3312 (dolist (buffer killed-buffers
)
3313 (set-window-prev-buffers
3314 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3315 (set-window-next-buffers
3316 window
(delq buffer
(window-next-buffers window
)))))
3318 ;; Return new-buffer.
3321 (defun get-next-valid-buffer (list &optional buffer visible-ok frame
)
3322 "Search LIST for a valid buffer to display in FRAME.
3323 Return nil when all buffers in LIST are undesirable for display,
3324 otherwise return the first suitable buffer in LIST.
3326 Buffers not visible in windows are preferred to visible buffers,
3327 unless VISIBLE-OK is non-nil.
3328 If the optional argument FRAME is nil, it defaults to the selected frame.
3329 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
3330 ;; This logic is more or less copied from other-buffer.
3331 (setq frame
(or frame
(selected-frame)))
3332 (let ((pred (frame-parameter frame
'buffer-predicate
))
3334 (while (and (not found
) list
)
3335 (setq buf
(car list
))
3336 (if (and (not (eq buffer buf
))
3338 (or (null pred
) (funcall pred buf
))
3339 (not (eq (aref (buffer-name buf
) 0) ?\s
))
3340 (or visible-ok
(null (get-buffer-window buf
'visible
))))
3342 (setq list
(cdr list
))))
3345 (defun last-buffer (&optional buffer visible-ok frame
)
3346 "Return the last buffer in FRAME's buffer list.
3347 If BUFFER is the last buffer, return the preceding buffer
3348 instead. Buffers not visible in windows are preferred to visible
3349 buffers, unless optional argument VISIBLE-OK is non-nil.
3350 Optional third argument FRAME nil or omitted means use the
3351 selected frame's buffer list. If no such buffer exists, return
3352 the buffer `*scratch*', creating it if necessary."
3353 (setq frame
(or frame
(selected-frame)))
3354 (or (get-next-valid-buffer (nreverse (buffer-list frame
))
3355 buffer visible-ok frame
)
3356 (get-buffer "*scratch*")
3357 (let ((scratch (get-buffer-create "*scratch*")))
3358 (set-buffer-major-mode scratch
)
3361 (defcustom frame-auto-hide-function
#'iconify-frame
3362 "Function called to automatically hide frames.
3363 The function is called with one argument - a frame.
3365 Functions affected by this option are those that bury a buffer
3366 shown in a separate frame like `quit-window' and `bury-buffer'."
3367 :type
'(choice (const :tag
"Iconify" iconify-frame
)
3368 (const :tag
"Delete" delete-frame
)
3369 (const :tag
"Do nothing" ignore
)
3375 (defun window--delete (&optional window dedicated-only kill
)
3376 "Delete WINDOW if possible.
3377 WINDOW must be a live window and defaults to the selected one.
3378 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
3379 only if it's dedicated to its buffer. Optional argument KILL
3380 means the buffer shown in window will be killed. Return non-nil
3381 if WINDOW gets deleted or its frame is auto-hidden."
3382 (setq window
(window-normalize-window window t
))
3383 (unless (and dedicated-only
(not (window-dedicated-p window
)))
3384 (let ((deletable (window-deletable-p window
)))
3386 ((eq deletable
'frame
)
3387 (let ((frame (window-frame window
)))
3390 (delete-frame frame
))
3391 ((functionp frame-auto-hide-function
)
3392 (funcall frame-auto-hide-function frame
))))
3395 (delete-window window
)
3398 (defun bury-buffer (&optional buffer-or-name
)
3399 "Put BUFFER-OR-NAME at the end of the list of all buffers.
3400 There it is the least likely candidate for `other-buffer' to
3401 return; thus, the least likely buffer for \\[switch-to-buffer] to
3404 You can specify a buffer name as BUFFER-OR-NAME, or an actual
3405 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
3406 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
3407 remove the current buffer from the selected window if it is
3410 (let* ((buffer (window-normalize-buffer buffer-or-name
)))
3411 ;; If `buffer-or-name' is not on the selected frame we unrecord it
3412 ;; although it's not "here" (call it a feature).
3413 (bury-buffer-internal buffer
)
3414 ;; Handle case where `buffer-or-name' is nil and the current buffer
3415 ;; is shown in the selected window.
3417 ((or buffer-or-name
(not (eq buffer
(window-buffer)))))
3418 ((window--delete nil t
))
3420 ;; Switch to another buffer in window.
3421 (set-window-dedicated-p nil nil
)
3422 (switch-to-prev-buffer nil
'bury
)))
3424 ;; Always return nil.
3427 (defun unbury-buffer ()
3428 "Switch to the last buffer in the buffer list."
3430 (switch-to-buffer (last-buffer)))
3432 (defun next-buffer ()
3433 "In selected window switch to next buffer."
3436 ((window-minibuffer-p)
3437 (error "Cannot switch buffers in minibuffer window"))
3438 ((eq (window-dedicated-p) t
)
3439 (error "Window is strongly dedicated to its buffer"))
3441 (switch-to-next-buffer))))
3443 (defun previous-buffer ()
3444 "In selected window switch to previous buffer."
3447 ((window-minibuffer-p)
3448 (error "Cannot switch buffers in minibuffer window"))
3449 ((eq (window-dedicated-p) t
)
3450 (error "Window is strongly dedicated to its buffer"))
3452 (switch-to-prev-buffer))))
3454 (defun delete-windows-on (&optional buffer-or-name frame
)
3455 "Delete all windows showing BUFFER-OR-NAME.
3456 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3457 and defaults to the current buffer.
3459 The following non-nil values of the optional argument FRAME
3460 have special meanings:
3462 - t means consider all windows on the selected frame only.
3464 - `visible' means consider all windows on all visible frames on
3465 the current terminal.
3467 - 0 (the number zero) means consider all windows on all visible
3468 and iconified frames on the current terminal.
3470 - A frame means consider all windows on that frame only.
3472 Any other value of FRAME means consider all windows on all
3475 When a window showing BUFFER-OR-NAME is dedicated and the only
3476 window of its frame, that frame is deleted when there are other
3478 (interactive "BDelete windows on (buffer):\nP")
3479 (let ((buffer (window-normalize-buffer buffer-or-name
))
3480 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3481 ;; `window-list-1' based function.
3482 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
3483 (dolist (window (window-list-1 nil nil all-frames
))
3484 (if (eq (window-buffer window
) buffer
)
3485 (let ((deletable (window-deletable-p window
)))
3487 ((and (eq deletable
'frame
) (window-dedicated-p window
))
3488 ;; Delete frame if and only if window is dedicated.
3489 (delete-frame (window-frame window
)))
3492 (delete-window window
))
3494 ;; In window switch to previous buffer.
3495 (set-window-dedicated-p window nil
)
3496 (switch-to-prev-buffer window
'bury
))))
3497 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3498 (unrecord-window-buffer window buffer
)))))
3500 (defun replace-buffer-in-windows (&optional buffer-or-name
)
3501 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
3502 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3503 and defaults to the current buffer.
3505 When a window showing BUFFER-OR-NAME is dedicated, that window is
3506 deleted. If that window is the only window on its frame, the
3507 frame is deleted too when there are other frames left. If there
3508 are no other frames left, some other buffer is displayed in that
3511 This function removes the buffer denoted by BUFFER-OR-NAME from
3512 all window-local buffer lists."
3513 (interactive "bBuffer to replace: ")
3514 (let ((buffer (window-normalize-buffer buffer-or-name
)))
3515 (dolist (window (window-list-1 nil nil t
))
3516 (if (eq (window-buffer window
) buffer
)
3517 (unless (window--delete window t t
)
3518 ;; Switch to another buffer in window.
3519 (set-window-dedicated-p window nil
)
3520 (switch-to-prev-buffer window
'kill
))
3521 ;; Unrecord BUFFER in WINDOW.
3522 (unrecord-window-buffer window buffer
)))))
3524 (defun quit-restore-window (&optional window bury-or-kill
)
3525 "Quit WINDOW and deal with its buffer.
3526 WINDOW must be a live window and defaults to the selected one.
3528 According to information stored in WINDOW's `quit-restore' window
3529 parameter either (1) delete WINDOW and its frame, (2) delete
3530 WINDOW, (3) restore the buffer previously displayed in WINDOW,
3531 or (4) make WINDOW display some other buffer than the present
3532 one. If non-nil, reset `quit-restore' parameter to nil.
3534 Optional second argument BURY-OR-KILL tells how to proceed with
3535 the buffer of WINDOW. The following values are handled:
3537 `nil' means to not handle the buffer in a particular way. This
3538 means that if WINDOW is not deleted by this function, invoking
3539 `switch-to-prev-buffer' will usually show the buffer again.
3541 `append' means that if WINDOW is not deleted, move its buffer to
3542 the end of WINDOW's previous buffers so it's less likely that a
3543 future invocation of `switch-to-prev-buffer' will switch to it.
3544 Also, move the buffer to the end of the frame's buffer list.
3546 `bury' means that if WINDOW is not deleted, remove its buffer
3547 from WINDOW'S list of previous buffers. Also, move the buffer
3548 to the end of the frame's buffer list. This value provides the
3549 most reliable remedy to not have `switch-to-prev-buffer' switch
3550 to this buffer again without killing the buffer.
3552 `kill' means to kill WINDOW's buffer."
3553 (setq window
(window-normalize-window window t
))
3554 (let* ((buffer (window-buffer window
))
3555 (quit-restore (window-parameter window
'quit-restore
))
3557 (let* ((prev-buffers (window-prev-buffers window
))
3558 (prev-buffer (caar prev-buffers
)))
3559 (and (or (not (eq prev-buffer buffer
))
3560 (and (cdr prev-buffers
)
3561 (not (eq (setq prev-buffer
(cadr prev-buffers
))
3566 ((and (not prev-buffer
)
3567 (or (eq (nth 1 quit-restore
) 'frame
)
3568 (and (eq (nth 1 quit-restore
) 'window
)
3569 ;; If the window has been created on an existing
3570 ;; frame and ended up as the sole window on that
3571 ;; frame, do not delete it (Bug#12764).
3572 (not (eq window
(frame-root-window window
)))))
3573 (eq (nth 3 quit-restore
) buffer
)
3574 ;; Delete WINDOW if possible.
3575 (window--delete window nil
(eq bury-or-kill
'kill
)))
3576 ;; If the previously selected window is still alive, select it.
3577 (when (window-live-p (nth 2 quit-restore
))
3578 (select-window (nth 2 quit-restore
))))
3579 ((and (listp (setq quad
(nth 1 quit-restore
)))
3580 (buffer-live-p (car quad
))
3581 (eq (nth 3 quit-restore
) buffer
))
3582 ;; Show another buffer stored in quit-restore parameter.
3583 (when (and (integerp (nth 3 quad
))
3584 (/= (nth 3 quad
) (window-total-size window
)))
3585 ;; Try to resize WINDOW to its old height but don't signal an
3588 (window-resize window
(- (nth 3 quad
) (window-total-size window
)))
3590 (set-window-dedicated-p window nil
)
3591 ;; Restore WINDOW's previous buffer, start and point position.
3592 (set-window-buffer-start-and-point
3593 window
(nth 0 quad
) (nth 1 quad
) (nth 2 quad
))
3594 ;; Deal with the buffer we just removed from WINDOW.
3595 (setq entry
(and (eq bury-or-kill
'append
)
3596 (assq buffer
(window-prev-buffers window
))))
3598 ;; Remove buffer from WINDOW's previous and next buffers.
3599 (set-window-prev-buffers
3600 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3601 (set-window-next-buffers
3602 window
(delq buffer
(window-next-buffers window
))))
3604 ;; Append old buffer's entry to list of WINDOW's previous
3605 ;; buffers so it's less likely to get switched to soon but
3606 ;; `display-buffer-in-previous-window' can nevertheless find it.
3607 (set-window-prev-buffers
3608 window
(append (window-prev-buffers window
) (list entry
))))
3609 ;; Reset the quit-restore parameter.
3610 (set-window-parameter window
'quit-restore nil
)
3611 ;; Select old window.
3612 (when (window-live-p (nth 2 quit-restore
))
3613 (select-window (nth 2 quit-restore
))))
3615 ;; Show some other buffer in WINDOW and reset the quit-restore
3617 (set-window-parameter window
'quit-restore nil
)
3618 ;; Make sure that WINDOW is no more dedicated.
3619 (set-window-dedicated-p window nil
)
3620 (switch-to-prev-buffer window bury-or-kill
)))
3622 ;; Deal with the buffer.
3624 ((not (buffer-live-p buffer
)))
3625 ((eq bury-or-kill
'kill
)
3626 (kill-buffer buffer
))
3628 (bury-buffer-internal buffer
)))))
3630 (defun quit-window (&optional kill window
)
3631 "Quit WINDOW and bury its buffer.
3632 WINDOW must be a live window and defaults to the selected one.
3633 With prefix argument KILL non-nil, kill the buffer instead of
3636 According to information stored in WINDOW's `quit-restore' window
3637 parameter either (1) delete WINDOW and its frame, (2) delete
3638 WINDOW, (3) restore the buffer previously displayed in WINDOW,
3639 or (4) make WINDOW display some other buffer than the present
3640 one. If non-nil, reset `quit-restore' parameter to nil."
3642 (quit-restore-window window
(if kill
'kill
'bury
)))
3644 (defun quit-windows-on (&optional buffer-or-name kill frame
)
3645 "Quit all windows showing BUFFER-OR-NAME.
3646 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3647 and defaults to the current buffer. Optional argument KILL
3648 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
3649 BUFFER-OR-NAME. Optional argument FRAME is handled as by
3650 `delete-windows-on'.
3652 This function calls `quit-window' on all candidate windows
3653 showing BUFFER-OR-NAME."
3654 (interactive "BQuit windows on (buffer):\nP")
3655 (let ((buffer (window-normalize-buffer buffer-or-name
))
3656 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3657 ;; `window-list-1' based function.
3658 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
3659 (dolist (window (window-list-1 nil nil all-frames
))
3660 (if (eq (window-buffer window
) buffer
)
3661 (quit-window kill window
)
3662 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3663 (unrecord-window-buffer window buffer
)))))
3665 ;;; Splitting windows.
3666 (defun window-split-min-size (&optional horizontal
)
3667 "Return minimum height of any window when splitting windows.
3668 Optional argument HORIZONTAL non-nil means return minimum width."
3670 (max window-min-width window-safe-min-width
)
3671 (max window-min-height window-safe-min-height
)))
3673 (defun split-window (&optional window size side
)
3674 "Make a new window adjacent to WINDOW.
3675 WINDOW must be a valid window and defaults to the selected one.
3676 Return the new window which is always a live window.
3678 Optional argument SIZE a positive number means make WINDOW SIZE
3679 lines or columns tall. If SIZE is negative, make the new window
3680 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
3681 absolute value can be less than `window-min-height' or
3682 `window-min-width'; so this command can make a new window as
3683 small as one line or two columns. SIZE defaults to half of
3686 Optional third argument SIDE nil (or `below') specifies that the
3687 new window shall be located below WINDOW. SIDE `above' means the
3688 new window shall be located above WINDOW. In both cases SIZE
3689 specifies the new number of lines for WINDOW (or the new window
3690 if SIZE is negative) including space reserved for the mode and/or
3693 SIDE t (or `right') specifies that the new window shall be
3694 located on the right side of WINDOW. SIDE `left' means the new
3695 window shall be located on the left of WINDOW. In both cases
3696 SIZE specifies the new number of columns for WINDOW (or the new
3697 window provided SIZE is negative) including space reserved for
3698 fringes and the scrollbar or a divider column. Any other non-nil
3699 value for SIDE is currently handled like t (or `right').
3701 If the variable `ignore-window-parameters' is non-nil or the
3702 `split-window' parameter of WINDOW equals t, do not process any
3703 parameters of WINDOW. Otherwise, if the `split-window' parameter
3704 of WINDOW specifies a function, call that function with all three
3705 arguments and return the value returned by that function.
3707 Otherwise, if WINDOW is part of an atomic window, \"split\" the
3708 root of that atomic window. The new window does not become a
3709 member of that atomic window.
3711 If WINDOW is live, properties of the new window like margins and
3712 scrollbars are inherited from WINDOW. If WINDOW is an internal
3713 window, these properties as well as the buffer displayed in the
3714 new window are inherited from the window selected on WINDOW's
3715 frame. The selected window is not changed by this function."
3716 (setq window
(window-normalize-window window
))
3719 ((memq side
'(below above right left
)) side
)
3721 (horizontal (not (memq side
'(below above
))))
3722 (frame (window-frame window
))
3723 (parent (window-parent window
))
3724 (function (window-parameter window
'split-window
))
3725 (window-side (window-parameter window
'window-side
))
3726 ;; Rebind the following two variables since in some cases we
3727 ;; have to override their value.
3728 (window-combination-limit window-combination-limit
)
3729 (window-combination-resize window-combination-resize
)
3732 (window--check frame
)
3735 ;; Ignore window parameters if either `ignore-window-parameters'
3736 ;; is t or the `split-window' parameter equals t.
3737 ((or ignore-window-parameters
(eq function t
)))
3738 ((functionp function
)
3739 ;; The `split-window' parameter specifies the function to call.
3740 ;; If that function is `ignore', do nothing.
3741 (throw 'done
(funcall function window size side
)))
3742 ;; If WINDOW is part of an atomic window, split the root window
3743 ;; of that atomic window instead.
3744 ((and (window-parameter window
'window-atom
)
3745 (setq atom-root
(window-atom-root window
))
3746 (not (eq atom-root window
)))
3747 (throw 'done
(split-window atom-root size side
)))
3748 ;; If WINDOW is a side window or its first or last child is a
3749 ;; side window, throw an error unless `window-combination-resize'
3751 ((and (not (eq window-combination-resize
'side
))
3752 (or (window-parameter window
'window-side
)
3753 (and (window-child window
)
3754 (or (window-parameter
3755 (window-child window
) 'window-side
)
3757 (window-last-child window
) 'window-side
)))))
3758 (error "Cannot split side window or parent of side window"))
3759 ;; If `window-combination-resize' is 'side and window has a side
3760 ;; window sibling, bind `window-combination-limit' to t.
3761 ((and (not (eq window-combination-resize
'side
))
3762 (or (and (window-prev-sibling window
)
3764 (window-prev-sibling window
) 'window-side
))
3765 (and (window-next-sibling window
)
3767 (window-next-sibling window
) 'window-side
))))
3768 (setq window-combination-limit t
)))
3770 ;; If `window-combination-resize' is t and SIZE is non-negative,
3771 ;; bind `window-combination-limit' to t.
3772 (when (and (eq window-combination-resize t
) size
(> size
0))
3773 (setq window-combination-limit t
))
3776 ;; `parent-size' is the size of WINDOW's parent, provided
3778 (when parent
(window-total-size parent horizontal
)))
3779 ;; `resize' non-nil means we are supposed to resize other
3780 ;; windows in WINDOW's combination.
3782 (and window-combination-resize
3783 (or (window-parameter window
'window-side
)
3784 (not (eq window-combination-resize
'side
)))
3785 (not (eq window-combination-limit t
))
3786 ;; Resize makes sense in iso-combinations only.
3787 (window-combined-p window horizontal
)))
3788 ;; `old-size' is the current size of WINDOW.
3789 (old-size (window-total-size window horizontal
))
3790 ;; `new-size' is the specified or calculated size of the
3795 (max (window-split-min-size horizontal
)
3797 ;; When resizing try to give the new window the
3798 ;; average size of a window in its combination.
3800 (window-min-size parent horizontal
))
3802 (1+ (window-combinations
3803 parent horizontal
))))
3804 ;; Else try to give the new window half the size
3805 ;; of WINDOW (plus an eventual odd line).
3806 (+ (/ old-size
2) (% old-size
2)))))
3808 ;; SIZE non-negative specifies the new size of WINDOW.
3810 ;; Note: Specifying a non-negative SIZE is practically
3811 ;; always done as workaround for making the new window
3812 ;; appear above or on the left of the new window (the
3813 ;; ispell window is a typical example of that). In all
3814 ;; these cases the SIDE argument should be set to 'above
3815 ;; or 'left in order to support the 'resize option.
3816 ;; Here we have to nest the windows instead, see above.
3819 ;; SIZE negative specifies the size of the new window.
3821 new-parent new-normal
)
3828 ;; SIZE unspecified, resizing.
3829 (when (and (not (window-sizable-p parent
(- new-size
) horizontal
))
3830 ;; Try again with minimum split size.
3832 (max new-size
(window-split-min-size horizontal
)))
3833 (not (window-sizable-p parent
(- new-size
) horizontal
)))
3834 (error "Window %s too small for splitting" parent
)))
3835 ((> (+ new-size
(window-min-size window horizontal
)) old-size
)
3836 ;; SIZE unspecified, no resizing.
3837 (error "Window %s too small for splitting" window
))))
3839 (or (>= size old-size
)
3840 (< new-size
(if horizontal
3841 window-safe-min-width
3842 window-safe-min-width
))))
3843 ;; SIZE specified as new size of old window. If the new size
3844 ;; is larger than the old size or the size of the new window
3845 ;; would be less than the safe minimum, signal an error.
3846 (error "Window %s too small for splitting" window
))
3848 ;; SIZE specified, resizing.
3849 (unless (window-sizable-p parent
(- new-size
) horizontal
)
3850 ;; If we cannot resize the parent give up.
3851 (error "Window %s too small for splitting" parent
)))
3853 (if horizontal window-safe-min-width window-safe-min-height
))
3854 (< (- old-size new-size
)
3855 (if horizontal window-safe-min-width window-safe-min-height
)))
3856 ;; SIZE specification violates minimum size restrictions.
3857 (error "Window %s too small for splitting" window
)))
3859 (window--resize-reset frame horizontal
)
3862 ;; Make new-parent non-nil if we need a new parent window;
3863 ;; either because we want to nest or because WINDOW is not
3865 (or (eq window-combination-limit t
)
3866 (not (window-combined-p window horizontal
))))
3868 ;; Make new-normal the normal size of the new window.
3870 (size (/ (float new-size
) (if new-parent old-size parent-size
)))
3872 (resize (/ 1.0 (1+ (window-combinations parent horizontal
))))
3873 (t (/ (window-normal-size window horizontal
) 2.0))))
3876 ;; Try to get space from OLD's siblings. We could go "up" and
3877 ;; try getting additional space from surrounding windows but
3878 ;; we won't be able to return space to those windows when we
3879 ;; delete the one we create here. Hence we do not go up.
3881 (window--resize-child-windows parent
(- new-size
) horizontal
)
3882 (let* ((normal (- 1.0 new-normal
))
3883 (sub (window-child parent
)))
3885 (set-window-new-normal
3886 sub
(* (window-normal-size sub horizontal
) normal
))
3887 (setq sub
(window-right sub
)))))
3888 ;; Get entire space from WINDOW.
3889 (set-window-new-total window
(- old-size new-size
))
3890 (window--resize-this-window window
(- new-size
) horizontal
)
3891 (set-window-new-normal
3892 window
(- (if new-parent
1.0 (window-normal-size window horizontal
))
3895 (let* ((new (split-window-internal window new-size side new-normal
)))
3896 ;; Assign window-side parameters, if any.
3897 (when (eq window-combination-resize
'side
)
3900 (window-side window-side
)
3901 ((eq side
'above
) 'top
)
3902 ((eq side
'below
) 'bottom
)
3904 ;; We made a new side window.
3905 (set-window-parameter new
'window-side window-side
)
3906 (when (and new-parent
(window-parameter window
'window-side
))
3907 ;; We've been splitting a side root window. Give the
3908 ;; new parent the same window-side parameter.
3909 (set-window-parameter
3910 (window-parent new
) 'window-side window-side
))))
3912 (run-window-configuration-change-hook frame
)
3913 (window--check frame
)
3914 ;; Always return the new window.
3917 ;; I think this should be the default; I think people will prefer it--rms.
3918 (defcustom split-window-keep-point t
3919 "If non-nil, \\[split-window-below] preserves point in the new window.
3920 If nil, adjust point in the two windows to minimize redisplay.
3921 This option applies only to `split-window-below' and functions
3922 that call it. The low-level `split-window' function always keeps
3923 the original point in both windows."
3927 (defun split-window-below (&optional size
)
3928 "Split the selected window into two windows, one above the other.
3929 The selected window is above. The newly split-off window is
3930 below, and displays the same buffer. Return the new window.
3932 If optional argument SIZE is omitted or nil, both windows get the
3933 same height, or close to it. If SIZE is positive, the upper
3934 \(selected) window gets SIZE lines. If SIZE is negative, the
3935 lower (new) window gets -SIZE lines.
3937 If the variable `split-window-keep-point' is non-nil, both
3938 windows get the same value of point as the selected window.
3939 Otherwise, the window starts are chosen so as to minimize the
3940 amount of redisplay; this is convenient on slow terminals."
3942 (let ((old-window (selected-window))
3943 (old-point (window-point))
3944 (size (and size
(prefix-numeric-value size
)))
3945 moved-by-window-height moved new-window bottom
)
3946 (when (and size
(< size
0) (< (- size
) window-min-height
))
3947 ;; `split-window' would not signal an error here.
3948 (error "Size of new window too small"))
3949 (setq new-window
(split-window nil size
))
3950 (unless split-window-keep-point
3951 (with-current-buffer (window-buffer)
3952 ;; Use `save-excursion' around vertical movements below
3953 ;; (Bug#10971). Note: When the selected window's buffer has a
3954 ;; header line, up to two lines of the buffer may not show up
3955 ;; in the resulting configuration.
3957 (goto-char (window-start))
3958 (setq moved
(vertical-motion (window-height)))
3959 (set-window-start new-window
(point))
3960 (when (> (point) (window-point new-window
))
3961 (set-window-point new-window
(point)))
3962 (when (= moved
(window-height))
3963 (setq moved-by-window-height t
)
3964 (vertical-motion -
1))
3965 (setq bottom
(point)))
3966 (and moved-by-window-height
3968 (set-window-point old-window
(1- bottom
)))
3969 (and moved-by-window-height
3970 (<= (window-start new-window
) old-point
)
3971 (set-window-point new-window old-point
)
3972 (select-window new-window
))))
3973 ;; Always copy quit-restore parameter in interactive use.
3974 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
3976 (set-window-parameter new-window
'quit-restore quit-restore
)))
3979 (defalias 'split-window-vertically
'split-window-below
)
3981 (defun split-window-right (&optional size
)
3982 "Split the selected window into two side-by-side windows.
3983 The selected window is on the left. The newly split-off window
3984 is on the right, and displays the same buffer. Return the new
3987 If optional argument SIZE is omitted or nil, both windows get the
3988 same width, or close to it. If SIZE is positive, the left-hand
3989 \(selected) window gets SIZE columns. If SIZE is negative, the
3990 right-hand (new) window gets -SIZE columns. Here, SIZE includes
3991 the width of the window's scroll bar; if there are no scroll
3992 bars, it includes the width of the divider column to the window's
3995 (let ((old-window (selected-window))
3996 (size (and size
(prefix-numeric-value size
)))
3998 (when (and size
(< size
0) (< (- size
) window-min-width
))
3999 ;; `split-window' would not signal an error here.
4000 (error "Size of new window too small"))
4001 (setq new-window
(split-window nil size t
))
4002 ;; Always copy quit-restore parameter in interactive use.
4003 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
4005 (set-window-parameter new-window
'quit-restore quit-restore
)))
4008 (defalias 'split-window-horizontally
'split-window-right
)
4010 ;;; Balancing windows.
4012 ;; The following routine uses the recycled code from an old version of
4013 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
4014 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
4015 ;; more readable (FWIW we'd need three loops - one to calculate the
4016 ;; minimum sizes per window, one to enlarge or shrink windows until the
4017 ;; new parent-size matches, and one where we shrink the largest/enlarge
4018 ;; the smallest window).
4019 (defun balance-windows-2 (window horizontal
)
4020 "Subroutine of `balance-windows-1'.
4021 WINDOW must be a vertical combination (horizontal if HORIZONTAL
4023 (let* ((first (window-child window
))
4025 (number-of-children 0)
4026 (parent-size (window-new-total window
))
4027 (total-sum parent-size
)
4028 failed size sub-total sub-delta sub-amount rest
)
4030 (setq number-of-children
(1+ number-of-children
))
4031 (when (window-size-fixed-p sub horizontal
)
4033 (- total-sum
(window-total-size sub horizontal
)))
4034 (set-window-new-normal sub
'ignore
))
4035 (setq sub
(window-right sub
)))
4038 (while (and failed
(> number-of-children
0))
4039 (setq size
(/ total-sum number-of-children
))
4042 (while (and sub
(not failed
))
4043 ;; Ignore child windows that should be ignored or are stuck.
4044 (unless (window--resize-child-windows-skip-p sub
)
4045 (setq sub-total
(window-total-size sub horizontal
))
4046 (setq sub-delta
(- size sub-total
))
4048 (window-sizable sub sub-delta horizontal
))
4049 ;; Register the new total size for this child window.
4050 (set-window-new-total sub
(+ sub-total sub-amount
))
4051 (unless (= sub-amount sub-delta
)
4052 (setq total-sum
(- total-sum sub-total sub-amount
))
4053 (setq number-of-children
(1- number-of-children
))
4054 ;; We failed and need a new round.
4056 (set-window-new-normal sub
'skip
)))
4057 (setq sub
(window-right sub
))))
4059 (setq rest
(% total-sum number-of-children
))
4060 ;; Fix rounding by trying to enlarge non-stuck windows by one line
4061 ;; (column) until `rest' is zero.
4063 (while (and sub
(> rest
0))
4064 (unless (window--resize-child-windows-skip-p window
)
4065 (set-window-new-total sub
1 t
)
4066 (setq rest
(1- rest
)))
4067 (setq sub
(window-right sub
)))
4069 ;; Fix rounding by trying to enlarge stuck windows by one line
4070 ;; (column) until `rest' equals zero.
4072 (while (and sub
(> rest
0))
4073 (unless (eq (window-new-normal sub
) 'ignore
)
4074 (set-window-new-total sub
1 t
)
4075 (setq rest
(1- rest
)))
4076 (setq sub
(window-right sub
)))
4080 ;; Record new normal sizes.
4081 (set-window-new-normal
4082 sub
(/ (if (eq (window-new-normal sub
) 'ignore
)
4083 (window-total-size sub horizontal
)
4084 (window-new-total sub
))
4085 (float parent-size
)))
4086 ;; Recursively balance each window's child windows.
4087 (balance-windows-1 sub horizontal
)
4088 (setq sub
(window-right sub
)))))
4090 (defun balance-windows-1 (window &optional horizontal
)
4091 "Subroutine of `balance-windows'."
4092 (if (window-child window
)
4093 (let ((sub (window-child window
)))
4094 (if (window-combined-p sub horizontal
)
4095 (balance-windows-2 window horizontal
)
4096 (let ((size (window-new-total window
)))
4098 (set-window-new-total sub size
)
4099 (balance-windows-1 sub horizontal
)
4100 (setq sub
(window-right sub
))))))))
4102 (defun balance-windows (&optional window-or-frame
)
4103 "Balance the sizes of windows of WINDOW-OR-FRAME.
4104 WINDOW-OR-FRAME is optional and defaults to the selected frame.
4105 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
4106 windows of that frame. If WINDOW-OR-FRAME denotes a window,
4107 recursively balance the sizes of all child windows of that
4112 ((or (not window-or-frame
)
4113 (frame-live-p window-or-frame
))
4114 (frame-root-window window-or-frame
))
4115 ((or (window-live-p window-or-frame
)
4116 (window-child window-or-frame
))
4119 (error "Not a window or frame %s" window-or-frame
))))
4120 (frame (window-frame window
)))
4121 ;; Balance vertically.
4122 (window--resize-reset (window-frame window
))
4123 (balance-windows-1 window
)
4124 (window-resize-apply frame
)
4125 ;; Balance horizontally.
4126 (window--resize-reset (window-frame window
) t
)
4127 (balance-windows-1 window t
)
4128 (window-resize-apply frame t
)))
4130 (defun window-fixed-size-p (&optional window direction
)
4131 "Return t if WINDOW cannot be resized in DIRECTION.
4132 WINDOW defaults to the selected window. DIRECTION can be
4133 nil (i.e. any), `height' or `width'."
4134 (with-current-buffer (window-buffer window
)
4135 (when (and (boundp 'window-size-fixed
) window-size-fixed
)
4137 (member (cons direction window-size-fixed
)
4138 '((height . width
) (width . height
))))))))
4140 ;;; A different solution to balance-windows.
4141 (defvar window-area-factor
1
4142 "Factor by which the window area should be over-estimated.
4143 This is used by `balance-windows-area'.
4144 Changing this globally has no effect.")
4145 (make-variable-buffer-local 'window-area-factor
)
4147 (defun balance-windows-area-adjust (window delta horizontal
)
4148 "Wrapper around `window-resize' with error checking.
4149 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
4150 ;; `window-resize' may fail if delta is too large.
4151 (while (>= (abs delta
) 1)
4154 (window-resize window delta horizontal
)
4157 ;;(message "adjust: %s" (error-message-string err))
4158 (setq delta
(/ delta
2))))))
4160 (defun balance-windows-area ()
4161 "Make all visible windows the same area (approximately).
4162 See also `window-area-factor' to change the relative size of
4165 (let* ((unchanged 0) (carry 0) (round 0)
4166 ;; Remove fixed-size windows.
4167 (wins (delq nil
(mapcar (lambda (win)
4168 (if (not (window-fixed-size-p win
)) win
))
4169 (window-list nil
'nomini
))))
4172 ;; Resizing a window changes the size of surrounding windows in complex
4173 ;; ways, so it's difficult to balance them all. The introduction of
4174 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
4175 ;; very difficult to do. `balance-window' above takes an off-line
4176 ;; approach: get the whole window tree, then balance it, then try to
4177 ;; adjust the windows so they fit the result.
4178 ;; Here, instead, we take a "local optimization" approach, where we just
4179 ;; go through all the windows several times until nothing needs to be
4180 ;; changed. The main problem with this approach is that it's difficult
4181 ;; to make sure it terminates, so we use some heuristic to try and break
4182 ;; off infinite loops.
4183 ;; After a round without any change, we allow a second, to give a chance
4184 ;; to the carry to propagate a minor imbalance from the end back to
4186 (while (< unchanged
2)
4187 ;; (message "New round")
4188 (setq unchanged
(1+ unchanged
) round
(1+ round
))
4191 (while (progn (setq next
(next-window next
))
4192 (window-fixed-size-p next
)))
4193 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
4195 (< (car (window-edges win
)) (car (window-edges next
))))
4196 (areadiff (/ (- (* (window-height next
) (window-width next
)
4197 (buffer-local-value 'window-area-factor
4198 (window-buffer next
)))
4199 (* (window-height win
) (window-width win
)
4200 (buffer-local-value 'window-area-factor
4201 (window-buffer win
))))
4202 (max (buffer-local-value 'window-area-factor
4203 (window-buffer win
))
4204 (buffer-local-value 'window-area-factor
4205 (window-buffer next
)))))
4207 (+ (window-height win
) (window-height next
))
4208 (+ (window-width win
) (window-width next
))))
4209 (diff (/ areadiff edgesize
)))
4211 ;; Maybe diff is actually closer to 1 than to 0.
4212 (setq diff
(/ (* 3 areadiff
) (* 2 edgesize
))))
4213 (when (and (zerop diff
) (not (zerop areadiff
)))
4214 (setq diff
(/ (+ areadiff carry
) edgesize
))
4215 ;; Change things smoothly.
4216 (if (or (> diff
1) (< diff -
1)) (setq diff
(/ diff
2))))
4218 ;; Make sure negligible differences don't accumulate to
4219 ;; become significant.
4220 (setq carry
(+ carry areadiff
))
4221 ;; This used `adjust-window-trailing-edge' before and uses
4222 ;; `window-resize' now. Error wrapping is still needed.
4223 (balance-windows-area-adjust win diff horiz
)
4225 (let ((change (cons win
(window-edges win
))))
4226 ;; If the same change has been seen already for this window,
4227 ;; we're most likely in an endless loop, so don't count it as
4229 (unless (member change changelog
)
4230 (push change changelog
)
4231 (setq unchanged
0 carry
0)))))))
4232 ;; We've now basically balanced all the windows.
4233 ;; But there may be some minor off-by-one imbalance left over,
4234 ;; so let's do some fine tuning.
4235 ;; (bw-finetune wins)
4236 ;; (message "Done in %d rounds" round)
4239 ;;; Window states, how to get them and how to put them in a window.
4240 (defun window--state-get-1 (window &optional writable
)
4241 "Helper function for `window-state-get'."
4244 ((window-top-child window
) 'vc
)
4245 ((window-left-child window
) 'hc
)
4247 (buffer (window-buffer window
))
4248 (selected (eq window
(selected-window)))
4251 ,@(unless (window-next-sibling window
) `((last . t
)))
4252 (total-height .
,(window-total-size window
))
4253 (total-width .
,(window-total-size window t
))
4254 (normal-height .
,(window-normal-size window
))
4255 (normal-width .
,(window-normal-size window t
))
4256 ,@(unless (window-live-p window
)
4257 `((combination-limit .
,(window-combination-limit window
))))
4258 ,@(let ((parameters (window-parameters window
))
4260 ;; Make copies of those window parameters whose
4261 ;; persistence property is `writable' if WRITABLE is
4262 ;; non-nil and non-nil if WRITABLE is nil.
4263 (dolist (par parameters
)
4264 (let ((pers (cdr (assq (car par
)
4265 window-persistent-parameters
))))
4266 (when (and pers
(or (not writable
) (eq pers
'writable
)))
4267 (setq list
(cons (cons (car par
) (cdr par
)) list
)))))
4268 ;; Add `clone-of' parameter if necessary.
4269 (let ((pers (cdr (assq 'clone-of
4270 window-persistent-parameters
))))
4271 (when (and pers
(or (not writable
) (eq pers
'writable
))
4272 (not (assq 'clone-of list
)))
4273 (setq list
(cons (cons 'clone-of window
) list
))))
4275 `((parameters .
,list
))))
4277 ;; All buffer related things go in here.
4278 (let ((point (window-point window
))
4279 (start (window-start window
)))
4281 ,(buffer-name buffer
)
4282 (selected .
,selected
)
4283 (hscroll .
,(window-hscroll window
))
4284 (fringes .
,(window-fringes window
))
4285 (margins .
,(window-margins window
))
4286 (scroll-bars .
,(window-scroll-bars window
))
4287 (vscroll .
,(window-vscroll window
))
4288 (dedicated .
,(window-dedicated-p window
))
4289 (point .
,(if writable point
4292 'window-point-insertion-type
4294 (start .
,(if writable start
(copy-marker start
)))))))))
4296 (when (memq type
'(vc hc
))
4298 (setq window
(window-child window
))
4300 (setq list
(cons (window--state-get-1 window writable
) list
))
4301 (setq window
(window-right window
)))
4303 (append head tail
)))
4305 (defun window-state-get (&optional window writable
)
4306 "Return state of WINDOW as a Lisp object.
4307 WINDOW can be any window and defaults to the root window of the
4310 Optional argument WRITABLE non-nil means do not use markers for
4311 sampling `window-point' and `window-start'. Together, WRITABLE
4312 and the variable `window-persistent-parameters' specify which
4313 window parameters are saved by this function. WRITABLE should be
4314 non-nil when the return value shall be written to a file and read
4315 back in another session. Otherwise, an application may run into
4316 an `invalid-read-syntax' error while attempting to read back the
4319 The return value can be used as argument for `window-state-put'
4320 to put the state recorded here into an arbitrary window. The
4321 value can be also stored on disk and read back in a new session."
4324 (if (window-valid-p window
)
4326 (error "%s is not a live or internal window" window
))
4327 (frame-root-window)))
4328 ;; The return value is a cons whose car specifies some constraints on
4329 ;; the size of WINDOW. The cdr lists the states of the child windows
4332 ;; Frame related things would go into a function, say `frame-state',
4333 ;; calling `window-state-get' to insert the frame's root window.
4334 `((min-height .
,(window-min-size window
))
4335 (min-width .
,(window-min-size window t
))
4336 (min-height-ignore .
,(window-min-size window nil t
))
4337 (min-width-ignore .
,(window-min-size window t t
))
4338 (min-height-safe .
,(window-min-size window nil
'safe
))
4339 (min-width-safe .
,(window-min-size window t
'safe
)))
4340 (window--state-get-1 window writable
)))
4342 (defvar window-state-put-list nil
4343 "Helper variable for `window-state-put'.")
4345 (defvar window-state-put-stale-windows nil
4346 "Helper variable for `window-state-put'.")
4348 (defun window--state-put-1 (state &optional window ignore totals
)
4349 "Helper function for `window-state-put'."
4350 (let ((type (car state
)))
4351 (setq state
(cdr state
))
4354 ;; For a leaf window just add unprocessed entries to
4355 ;; `window-state-put-list'.
4356 (push (cons window state
) window-state-put-list
))
4357 ((memq type
'(vc hc
))
4358 (let* ((horizontal (eq type
'hc
))
4359 (total (window-total-size window horizontal
))
4362 (dolist (item state
)
4363 ;; Find the next child window. WINDOW always points to the
4364 ;; real window that we want to fill with what we find here.
4365 (when (memq (car item
) '(leaf vc hc
))
4366 (if (assq 'last item
)
4367 ;; The last child window. Below `window--state-put-1'
4368 ;; will put into it whatever ITEM has in store.
4370 ;; Not the last child window, prepare for splitting
4371 ;; WINDOW. SIZE is the new (and final) size of the old
4376 (cdr (assq (if horizontal
'total-width
'total-height
) item
))
4377 ;; Use normalized size and round.
4380 (if horizontal
'normal-width
'normal-height
)
4383 ;; Use safe sizes, we try to resize later.
4384 (setq size
(max size
(if horizontal
4385 window-safe-min-height
4386 window-safe-min-width
)))
4388 (if (window-sizable-p window
(- size
) horizontal
'safe
)
4389 (let* ((window-combination-limit
4390 (assq 'combination-limit item
)))
4391 ;; We must inherit the combination limit, otherwise
4392 ;; we might mess up handling of atomic and side
4394 (setq new
(split-window window size horizontal
)))
4395 ;; Give up if we can't resize window down to safe sizes.
4396 (error "Cannot resize window %s" window
))
4400 ;; When creating the first child window add for parent
4401 ;; unprocessed entries to `window-state-put-list'.
4402 (setq window-state-put-list
4403 (cons (cons (window-parent window
) state
)
4404 window-state-put-list
))))
4406 ;; Now process the current window (either the one we've just
4407 ;; split or the last child of its parent).
4408 (window--state-put-1 item window ignore totals
)
4409 ;; Continue with the last window split off.
4410 (setq window new
))))))))
4412 (defun window--state-put-2 (ignore)
4413 "Helper function for `window-state-put'."
4414 (dolist (item window-state-put-list
)
4415 (let ((window (car item
))
4416 (combination-limit (cdr (assq 'combination-limit item
)))
4417 (parameters (cdr (assq 'parameters item
)))
4418 (state (cdr (assq 'buffer item
))))
4419 (when combination-limit
4420 (set-window-combination-limit window combination-limit
))
4421 ;; Reset window's parameters and assign saved ones (we might want
4422 ;; a `remove-window-parameters' function here).
4423 (dolist (parameter (window-parameters window
))
4424 (set-window-parameter window
(car parameter
) nil
))
4426 (dolist (parameter parameters
)
4427 (set-window-parameter window
(car parameter
) (cdr parameter
))))
4428 ;; Process buffer related state.
4430 ;; We don't want to raise an error in case the buffer does not
4431 ;; exist anymore, so we switch to a previous one and save the
4432 ;; window with the intention of deleting it later if possible.
4433 (let ((buffer (get-buffer (car state
))))
4435 (set-window-buffer window buffer
)
4436 (switch-to-prev-buffer window
)
4437 (push window window-state-put-stale-windows
)))
4438 (with-current-buffer (window-buffer window
)
4439 (set-window-hscroll window
(cdr (assq 'hscroll state
)))
4440 (apply 'set-window-fringes
4441 (cons window
(cdr (assq 'fringes state
))))
4442 (let ((margins (cdr (assq 'margins state
))))
4443 (set-window-margins window
(car margins
) (cdr margins
)))
4444 (let ((scroll-bars (cdr (assq 'scroll-bars state
))))
4445 (set-window-scroll-bars
4446 window
(car scroll-bars
) (nth 2 scroll-bars
) (nth 3 scroll-bars
)))
4447 (set-window-vscroll window
(cdr (assq 'vscroll state
)))
4448 ;; Adjust vertically.
4449 (if (memq window-size-fixed
'(t height
))
4450 ;; A fixed height window, try to restore the original size.
4451 (let ((delta (- (cdr (assq 'total-height item
))
4452 (window-total-height window
)))
4454 (when (window-resizable-p window delta
)
4455 (window-resize window delta
)))
4456 ;; Else check whether the window is not high enough.
4457 (let* ((min-size (window-min-size window nil ignore
))
4458 (delta (- min-size
(window-total-size window
))))
4459 (when (and (> delta
0)
4460 (window-resizable-p window delta nil ignore
))
4461 (window-resize window delta nil ignore
))))
4462 ;; Adjust horizontally.
4463 (if (memq window-size-fixed
'(t width
))
4464 ;; A fixed width window, try to restore the original size.
4465 (let ((delta (- (cdr (assq 'total-width item
))
4466 (window-total-width window
)))
4468 (when (window-resizable-p window delta
)
4469 (window-resize window delta
)))
4470 ;; Else check whether the window is not wide enough.
4471 (let* ((min-size (window-min-size window t ignore
))
4472 (delta (- min-size
(window-total-size window t
))))
4473 (when (and (> delta
0)
4474 (window-resizable-p window delta t ignore
))
4475 (window-resize window delta t ignore
))))
4476 ;; Set dedicated status.
4477 (set-window-dedicated-p window
(cdr (assq 'dedicated state
)))
4478 ;; Install positions (maybe we should do this after all windows
4479 ;; have been created and sized).
4481 (set-window-start window
(cdr (assq 'start state
)))
4482 (set-window-point window
(cdr (assq 'point state
))))
4483 ;; Select window if it's the selected one.
4484 (when (cdr (assq 'selected state
))
4485 (select-window window
)))))))
4487 (defun window-state-put (state &optional window ignore
)
4488 "Put window state STATE into WINDOW.
4489 STATE should be the state of a window returned by an earlier
4490 invocation of `window-state-get'. Optional argument WINDOW must
4491 specify a live window and defaults to the selected one.
4493 Optional argument IGNORE non-nil means ignore minimum window
4494 sizes and fixed size restrictions. IGNORE equal `safe' means
4495 windows can get as small as `window-safe-min-height' and
4496 `window-safe-min-width'."
4497 (setq window-state-put-stale-windows nil
)
4498 (setq window
(window-normalize-window window t
))
4499 (let* ((frame (window-frame window
))
4501 ;; We check here (1) whether the total sizes of root window of
4502 ;; STATE and that of WINDOW are equal so we can avoid
4503 ;; calculating new sizes, and (2) if we do have to resize
4504 ;; whether we can do so without violating size restrictions.
4506 (and (= (window-total-size window
)
4507 (cdr (assq 'total-height state
)))
4508 (= (window-total-size window t
)
4509 (cdr (assq 'total-width state
)))))
4510 (min-height (cdr (assq 'min-height head
)))
4511 (min-width (cdr (assq 'min-width head
))))
4512 (if (and (not totals
)
4513 (or (> min-height
(window-total-size window
))
4514 (> min-width
(window-total-size window t
)))
4516 (and (setq min-height
4517 (cdr (assq 'min-height-ignore head
)))
4519 (cdr (assq 'min-width-ignore head
)))
4520 (or (> min-height
(window-total-size window
))
4521 (> min-width
(window-total-size window t
)))
4522 (or (not (eq ignore
'safe
))
4523 (and (setq min-height
4524 (cdr (assq 'min-height-safe head
)))
4526 (cdr (assq 'min-width-safe head
)))
4528 (window-total-size window
))
4530 (window-total-size window t
))))))))
4531 ;; The check above might not catch all errors due to rounding
4532 ;; issues - so IGNORE equal 'safe might not always produce the
4533 ;; minimum possible state. But such configurations hardly make
4535 (error "Window %s too small to accommodate state" window
)
4536 (setq state
(cdr state
))
4537 (setq window-state-put-list nil
)
4538 ;; Work on the windows of a temporary buffer to make sure that
4539 ;; splitting proceeds regardless of any buffer local values of
4540 ;; `window-size-fixed'. Release that buffer after the buffers of
4541 ;; all live windows have been set by `window--state-put-2'.
4543 (set-window-buffer window
(current-buffer))
4544 (window--state-put-1 state window nil totals
)
4545 (window--state-put-2 ignore
))
4546 (while window-state-put-stale-windows
4547 (let ((window (pop window-state-put-stale-windows
)))
4548 (when (eq (window-deletable-p window
) t
)
4549 (delete-window window
))))
4550 (window--check frame
))))
4552 (defun display-buffer-record-window (type window buffer
)
4553 "Record information for window used by `display-buffer'.
4554 TYPE specifies the type of the calling operation and must be one
4555 of the symbols 'reuse (when WINDOW existed already and was
4556 reused for displaying BUFFER), 'window (when WINDOW was created
4557 on an already existing frame), or 'frame (when WINDOW was
4558 created on a new frame). WINDOW is the window used for or created
4559 by the `display-buffer' routines. BUFFER is the buffer that
4562 This function installs or updates the quit-restore parameter of
4563 WINDOW. The quit-restore parameter is a list of four elements:
4564 The first element is one of the symbols 'window, 'frame, 'same or
4565 'other. The second element is either one of the symbols 'window
4566 or 'frame or a list whose elements are the buffer previously
4567 shown in the window, that buffer's window start and window point,
4568 and the window's height. The third element is the window
4569 selected at the time the parameter was created. The fourth
4573 (if (eq (window-buffer window
) buffer
)
4574 ;; WINDOW shows BUFFER already.
4575 (when (consp (window-parameter window
'quit-restore
))
4576 ;; If WINDOW has a quit-restore parameter, reset its car.
4577 (setcar (window-parameter window
'quit-restore
) 'same
))
4578 ;; WINDOW shows another buffer.
4579 (with-current-buffer (window-buffer window
)
4580 (set-window-parameter
4581 window
'quit-restore
4583 ;; A quadruple of WINDOW's buffer, start, point and height.
4584 (list (current-buffer) (window-start window
)
4585 ;; Preserve window-point-insertion-type (Bug#12588).
4587 (window-point window
) window-point-insertion-type
)
4588 (window-total-size window
))
4589 (selected-window) buffer
)))))
4591 ;; WINDOW has been created on an existing frame.
4592 (set-window-parameter
4593 window
'quit-restore
4594 (list 'window
'window
(selected-window) buffer
)))
4596 ;; WINDOW has been created on a new frame.
4597 (set-window-parameter
4598 window
'quit-restore
4599 (list 'frame
'frame
(selected-window) buffer
)))))
4601 (defcustom display-buffer-function nil
4602 "If non-nil, function to call to handle `display-buffer'.
4603 It will receive two args, the buffer and a flag which if non-nil
4604 means that the currently selected window is not acceptable. It
4605 should choose or create a window, display the specified buffer in
4606 it, and return the window.
4608 The specified function should call `display-buffer-record-window'
4609 with corresponding arguments to set up the quit-restore parameter
4610 of the window used."
4613 (function :tag
"function"))
4616 (make-obsolete-variable 'display-buffer-function
4617 'display-buffer-alist
"24.3")
4619 ;; Eventually, we want to turn this into a defvar; instead of
4620 ;; customizing this, the user should use a `pop-up-frame-parameters'
4621 ;; alist entry in `display-buffer-base-action'.
4622 (defcustom pop-up-frame-alist nil
4623 "Alist of parameters for automatically generated new frames.
4624 If non-nil, the value you specify here is used by the default
4625 `pop-up-frame-function' for the creation of new frames.
4627 Since `pop-up-frame-function' is used by `display-buffer' for
4628 making new frames, any value specified here by default affects
4629 the automatic generation of new frames via `display-buffer' and
4630 all functions based on it. The behavior of `make-frame' is not
4631 affected by this variable."
4632 :type
'(repeat (cons :format
"%v"
4633 (symbol :tag
"Parameter")
4634 (sexp :tag
"Value")))
4637 (defcustom pop-up-frame-function
4638 (lambda () (make-frame pop-up-frame-alist
))
4639 "Function used by `display-buffer' for creating a new frame.
4640 This function is called with no arguments and should return a new
4641 frame. The default value calls `make-frame' with the argument
4642 `pop-up-frame-alist'."
4646 (defcustom special-display-buffer-names nil
4647 "List of names of buffers that should be displayed specially.
4648 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4649 its name is in this list, displays the buffer in a way specified
4650 by `special-display-function'. `special-display-popup-frame'
4651 \(the default for `special-display-function') usually displays
4652 the buffer in a separate frame made with the parameters specified
4653 by `special-display-frame-alist'. If `special-display-function'
4654 has been set to some other function, that function is called with
4655 the buffer as first, and nil as second argument.
4657 Alternatively, an element of this list can be specified as
4658 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
4659 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4660 `special-display-popup-frame' will interpret such pairs as frame
4661 parameters when it creates a special frame, overriding the
4662 corresponding values from `special-display-frame-alist'.
4664 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4665 `special-display-popup-frame' displays that buffer in the
4666 selected window. If FRAME-PARAMETERS contains (same-frame . t),
4667 it displays that buffer in a window on the selected frame.
4669 If `special-display-function' specifies some other function than
4670 `special-display-popup-frame', that function is called with the
4671 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
4674 Finally, an element of this list can be also specified as
4675 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
4676 `special-display-popup-frame' will call FUNCTION with the buffer
4677 named BUFFER-NAME as first argument, and OTHER-ARGS as the
4680 Any alternative function specified here is responsible for
4681 setting up the quit-restore parameter of the window used.
4683 If this variable appears \"not to work\", because you added a
4684 name to it but the corresponding buffer is displayed in the
4685 selected window, look at the values of `same-window-buffer-names'
4686 and `same-window-regexps'. Those variables take precedence over
4689 See also `special-display-regexps'."
4691 (choice :tag
"Buffer"
4693 (string :format
"%v")
4694 (cons :tag
"With parameters"
4697 (string :format
"%v")
4698 (repeat :tag
"Parameters"
4700 (symbol :tag
"Parameter")
4701 (sexp :tag
"Value"))))
4702 (list :tag
"With function"
4705 (string :format
"%v")
4706 (function :tag
"Function")
4707 (repeat :tag
"Arguments" (sexp)))))
4710 (make-obsolete-variable 'special-display-buffer-names
'display-buffer-alist
"24.3")
4711 (put 'special-display-buffer-names
'risky-local-variable t
)
4713 (defcustom special-display-regexps nil
4714 "List of regexps saying which buffers should be displayed specially.
4715 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4716 any regexp in this list matches its name, displays it specially
4717 using `special-display-function'. `special-display-popup-frame'
4718 \(the default for `special-display-function') usually displays
4719 the buffer in a separate frame made with the parameters specified
4720 by `special-display-frame-alist'. If `special-display-function'
4721 has been set to some other function, that function is called with
4722 the buffer as first, and nil as second argument.
4724 Alternatively, an element of this list can be specified as
4725 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
4726 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4727 `special-display-popup-frame' will then interpret these pairs as
4728 frame parameters when creating a special frame for a buffer whose
4729 name matches REGEXP, overriding the corresponding values from
4730 `special-display-frame-alist'.
4732 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4733 `special-display-popup-frame' displays buffers matching REGEXP in
4734 the selected window. (same-frame . t) in FRAME-PARAMETERS means
4735 to display such buffers in a window on the selected frame.
4737 If `special-display-function' specifies some other function than
4738 `special-display-popup-frame', that function is called with the
4739 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
4742 Finally, an element of this list can be also specified as
4743 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
4744 will then call FUNCTION with the buffer whose name matched
4745 REGEXP as first, and OTHER-ARGS as second argument.
4747 Any alternative function specified here is responsible for
4748 setting up the quit-restore parameter of the window used.
4750 If this variable appears \"not to work\", because you added a
4751 name to it but the corresponding buffer is displayed in the
4752 selected window, look at the values of `same-window-buffer-names'
4753 and `same-window-regexps'. Those variables take precedence over
4756 See also `special-display-buffer-names'."
4758 (choice :tag
"Buffer"
4760 (regexp :format
"%v")
4761 (cons :tag
"With parameters"
4764 (regexp :format
"%v")
4765 (repeat :tag
"Parameters"
4767 (symbol :tag
"Parameter")
4768 (sexp :tag
"Value"))))
4769 (list :tag
"With function"
4772 (regexp :format
"%v")
4773 (function :tag
"Function")
4774 (repeat :tag
"Arguments" (sexp)))))
4777 (make-obsolete-variable 'special-display-regexps
'display-buffer-alist
"24.3")
4778 (put 'special-display-regexps
'risky-local-variable t
)
4780 (defun special-display-p (buffer-name)
4781 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
4782 More precisely, return t if `special-display-buffer-names' or
4783 `special-display-regexps' contain a string entry equaling or
4784 matching BUFFER-NAME. If `special-display-buffer-names' or
4785 `special-display-regexps' contain a list entry whose car equals
4786 or matches BUFFER-NAME, the return value is the cdr of that
4790 ((member buffer-name special-display-buffer-names
)
4792 ((setq tmp
(assoc buffer-name special-display-buffer-names
))
4795 (dolist (regexp special-display-regexps
)
4798 (when (string-match-p regexp buffer-name
)
4800 ((and (consp regexp
) (stringp (car regexp
))
4801 (string-match-p (car regexp
) buffer-name
))
4802 (throw 'found
(cdr regexp
))))))))))
4804 (defcustom special-display-frame-alist
4805 '((height .
14) (width .
80) (unsplittable . t
))
4806 "Alist of parameters for special frames.
4807 Special frames are used for buffers whose names are listed in
4808 `special-display-buffer-names' and for buffers whose names match
4809 one of the regular expressions in `special-display-regexps'.
4811 This variable can be set in your init file, like this:
4813 (setq special-display-frame-alist '((width . 80) (height . 20)))
4815 These supersede the values given in `default-frame-alist'."
4816 :type
'(repeat (cons :format
"%v"
4817 (symbol :tag
"Parameter")
4818 (sexp :tag
"Value")))
4820 (make-obsolete-variable 'special-display-frame-alist
'display-buffer-alist
"24.3")
4822 (defun special-display-popup-frame (buffer &optional args
)
4823 "Pop up a frame displaying BUFFER and return its window.
4824 If BUFFER is already displayed in a visible or iconified frame,
4825 raise that frame. Otherwise, display BUFFER in a new frame.
4827 Optional argument ARGS is a list specifying additional
4830 If ARGS is an alist, use it as a list of frame parameters. If
4831 these parameters contain (same-window . t), display BUFFER in
4832 the selected window. If they contain (same-frame . t), display
4833 BUFFER in a window of the selected frame.
4835 If ARGS is a list whose car is a symbol, use (car ARGS) as a
4836 function to do the work. Pass it BUFFER as first argument, and
4837 pass the elements of (cdr ARGS) as the remaining arguments."
4838 (if (and args
(symbolp (car args
)))
4839 (apply (car args
) buffer
(cdr args
))
4840 (let ((window (get-buffer-window buffer
0)))
4842 ;; If we have a window already, make it visible.
4844 (let ((frame (window-frame window
)))
4845 (make-frame-visible frame
)
4847 (display-buffer-record-window 'reuse window buffer
)
4849 ;; Reuse the current window if the user requested it.
4850 (when (cdr (assq 'same-window args
))
4852 (progn (switch-to-buffer buffer nil t
) (selected-window))
4854 ;; Stay on the same frame if requested.
4855 (when (or (cdr (assq 'same-frame args
)) (cdr (assq 'same-window args
)))
4856 (let* ((pop-up-windows t
)
4858 special-display-buffer-names special-display-regexps
)
4859 (display-buffer buffer
)))
4860 ;; If no window yet, make one in a new frame.
4862 (with-current-buffer buffer
4863 (make-frame (append args special-display-frame-alist
))))
4864 (window (frame-selected-window frame
)))
4865 (display-buffer-record-window 'frame window buffer
)
4866 (unless (eq buffer
(window-buffer window
))
4867 (set-window-buffer window buffer
)
4868 (set-window-prev-buffers window nil
))
4869 (set-window-dedicated-p window t
)
4872 (defcustom special-display-function
'special-display-popup-frame
4873 "Function to call for displaying special buffers.
4874 This function is called with two arguments - the buffer and,
4875 optionally, a list - and should return a window displaying that
4876 buffer. The default value usually makes a separate frame for the
4877 buffer using `special-display-frame-alist' to specify the frame
4878 parameters. See the definition of `special-display-popup-frame'
4879 for how to specify such a function.
4881 A buffer is special when its name is either listed in
4882 `special-display-buffer-names' or matches a regexp in
4883 `special-display-regexps'.
4885 The specified function should call `display-buffer-record-window'
4886 with corresponding arguments to set up the quit-restore parameter
4887 of the window used."
4890 (make-obsolete-variable 'special-display-function
'display-buffer-alist
"24.3")
4892 (defcustom same-window-buffer-names nil
4893 "List of names of buffers that should appear in the \"same\" window.
4894 `display-buffer' and `pop-to-buffer' show a buffer whose name is
4895 on this list in the selected rather than some other window.
4897 An element of this list can be a cons cell instead of just a
4898 string. In that case, the cell's car must be a string specifying
4899 the buffer name. This is for compatibility with
4900 `special-display-buffer-names'; the cdr of the cons cell is
4903 See also `same-window-regexps'."
4904 :type
'(repeat (string :format
"%v"))
4907 (defcustom same-window-regexps nil
4908 "List of regexps saying which buffers should appear in the \"same\" window.
4909 `display-buffer' and `pop-to-buffer' show a buffer whose name
4910 matches a regexp on this list in the selected rather than some
4913 An element of this list can be a cons cell instead of just a
4914 string. In that case, the cell's car must be a regexp matching
4915 the buffer name. This is for compatibility with
4916 `special-display-regexps'; the cdr of the cons cell is ignored.
4918 See also `same-window-buffer-names'."
4919 :type
'(repeat (regexp :format
"%v"))
4922 (defun same-window-p (buffer-name)
4923 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
4924 This function returns non-nil if `display-buffer' or
4925 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
4926 selected rather than (as usual) some other window. See
4927 `same-window-buffer-names' and `same-window-regexps'."
4929 ((not (stringp buffer-name
)))
4930 ;; The elements of `same-window-buffer-names' can be buffer
4931 ;; names or cons cells whose cars are buffer names.
4932 ((member buffer-name same-window-buffer-names
))
4933 ((assoc buffer-name same-window-buffer-names
))
4935 (dolist (regexp same-window-regexps
)
4936 ;; The elements of `same-window-regexps' can be regexps
4937 ;; or cons cells whose cars are regexps.
4938 (when (or (and (stringp regexp
)
4939 (string-match-p regexp buffer-name
))
4940 (and (consp regexp
) (stringp (car regexp
))
4941 (string-match-p (car regexp
) buffer-name
)))
4942 (throw 'found t
)))))))
4944 (defcustom pop-up-frames nil
4945 "Whether `display-buffer' should make a separate frame.
4946 If nil, never make a separate frame.
4947 If the value is `graphic-only', make a separate frame
4948 on graphic displays only.
4949 Any other non-nil value means always make a separate frame."
4951 (const :tag
"Never" nil
)
4952 (const :tag
"On graphic displays only" graphic-only
)
4953 (const :tag
"Always" t
))
4956 (defcustom display-buffer-reuse-frames nil
4957 "Non-nil means `display-buffer' should reuse frames.
4958 If the buffer in question is already displayed in a frame, raise
4964 (make-obsolete-variable
4965 'display-buffer-reuse-frames
4966 "use a `reusable-frames' alist entry in `display-buffer-alist'."
4969 (defcustom pop-up-windows t
4970 "Non-nil means `display-buffer' should make a new window."
4974 (defcustom split-window-preferred-function
'split-window-sensibly
4975 "Function called by `display-buffer' routines to split a window.
4976 This function is called with a window as single argument and is
4977 supposed to split that window and return the new window. If the
4978 window can (or shall) not be split, it is supposed to return nil.
4979 The default is to call the function `split-window-sensibly' which
4980 tries to split the window in a way which seems most suitable.
4981 You can customize the options `split-height-threshold' and/or
4982 `split-width-threshold' in order to have `split-window-sensibly'
4983 prefer either vertical or horizontal splitting.
4985 If you set this to any other function, bear in mind that the
4986 `display-buffer' routines may call this function two times. The
4987 argument of the first call is the largest window on its frame.
4988 If that call fails to return a live window, the function is
4989 called again with the least recently used window as argument. If
4990 that call fails too, `display-buffer' will use an existing window
4991 to display its buffer.
4993 The window selected at the time `display-buffer' was invoked is
4994 still selected when this function is called. Hence you can
4995 compare the window argument with the value of `selected-window'
4996 if you intend to split the selected window instead or if you do
4997 not want to split the selected window."
5002 (defcustom split-height-threshold
80
5003 "Minimum height for splitting windows sensibly.
5004 If this is an integer, `split-window-sensibly' may split a window
5005 vertically only if it has at least this many lines. If this is
5006 nil, `split-window-sensibly' is not allowed to split a window
5007 vertically. If, however, a window is the only window on its
5008 frame, `split-window-sensibly' may split it vertically
5009 disregarding the value of this variable."
5010 :type
'(choice (const nil
) (integer :tag
"lines"))
5014 (defcustom split-width-threshold
160
5015 "Minimum width for splitting windows sensibly.
5016 If this is an integer, `split-window-sensibly' may split a window
5017 horizontally only if it has at least this many columns. If this
5018 is nil, `split-window-sensibly' is not allowed to split a window
5020 :type
'(choice (const nil
) (integer :tag
"columns"))
5024 (defun window-splittable-p (window &optional horizontal
)
5025 "Return non-nil if `split-window-sensibly' may split WINDOW.
5026 Optional argument HORIZONTAL nil or omitted means check whether
5027 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
5028 non-nil means check whether WINDOW may be split horizontally.
5030 WINDOW may be split vertically when the following conditions
5032 - `window-size-fixed' is either nil or equals `width' for the
5034 - `split-height-threshold' is an integer and WINDOW is at least as
5035 high as `split-height-threshold'.
5036 - When WINDOW is split evenly, the emanating windows are at least
5037 `window-min-height' lines tall and can accommodate at least one
5038 line plus - if WINDOW has one - a mode line.
5040 WINDOW may be split horizontally when the following conditions
5042 - `window-size-fixed' is either nil or equals `height' for the
5044 - `split-width-threshold' is an integer and WINDOW is at least as
5045 wide as `split-width-threshold'.
5046 - When WINDOW is split evenly, the emanating windows are at least
5047 `window-min-width' or two (whichever is larger) columns wide."
5048 (when (window-live-p window
)
5049 (with-current-buffer (window-buffer window
)
5051 ;; A window can be split horizontally when its width is not
5052 ;; fixed, it is at least `split-width-threshold' columns wide
5053 ;; and at least twice as wide as `window-min-width' and 2 (the
5054 ;; latter value is hardcoded).
5055 (and (memq window-size-fixed
'(nil height
))
5056 ;; Testing `window-full-width-p' here hardly makes any
5057 ;; sense nowadays. This can be done more intuitively by
5058 ;; setting up `split-width-threshold' appropriately.
5059 (numberp split-width-threshold
)
5060 (>= (window-width window
)
5061 (max split-width-threshold
5062 (* 2 (max window-min-width
2)))))
5063 ;; A window can be split vertically when its height is not
5064 ;; fixed, it is at least `split-height-threshold' lines high,
5065 ;; and it is at least twice as high as `window-min-height' and 2
5066 ;; if it has a mode line or 1.
5067 (and (memq window-size-fixed
'(nil width
))
5068 (numberp split-height-threshold
)
5069 (>= (window-height window
)
5070 (max split-height-threshold
5071 (* 2 (max window-min-height
5072 (if mode-line-format
2 1))))))))))
5074 (defun split-window-sensibly (&optional window
)
5075 "Split WINDOW in a way suitable for `display-buffer'.
5076 WINDOW defaults to the currently selected window.
5077 If `split-height-threshold' specifies an integer, WINDOW is at
5078 least `split-height-threshold' lines tall and can be split
5079 vertically, split WINDOW into two windows one above the other and
5080 return the lower window. Otherwise, if `split-width-threshold'
5081 specifies an integer, WINDOW is at least `split-width-threshold'
5082 columns wide and can be split horizontally, split WINDOW into two
5083 windows side by side and return the window on the right. If this
5084 can't be done either and WINDOW is the only window on its frame,
5085 try to split WINDOW vertically disregarding any value specified
5086 by `split-height-threshold'. If that succeeds, return the lower
5087 window. Return nil otherwise.
5089 By default `display-buffer' routines call this function to split
5090 the largest or least recently used window. To change the default
5091 customize the option `split-window-preferred-function'.
5093 You can enforce this function to not split WINDOW horizontally,
5094 by setting (or binding) the variable `split-width-threshold' to
5095 nil. If, in addition, you set `split-height-threshold' to zero,
5096 chances increase that this function does split WINDOW vertically.
5098 In order to not split WINDOW vertically, set (or bind) the
5099 variable `split-height-threshold' to nil. Additionally, you can
5100 set `split-width-threshold' to zero to make a horizontal split
5101 more likely to occur.
5103 Have a look at the function `window-splittable-p' if you want to
5104 know how `split-window-sensibly' determines whether WINDOW can be
5106 (let ((window (or window
(selected-window))))
5107 (or (and (window-splittable-p window
)
5108 ;; Split window vertically.
5109 (with-selected-window window
5110 (split-window-below)))
5111 (and (window-splittable-p window t
)
5112 ;; Split window horizontally.
5113 (with-selected-window window
5114 (split-window-right)))
5115 (and (eq window
(frame-root-window (window-frame window
)))
5116 (not (window-minibuffer-p window
))
5117 ;; If WINDOW is the only window on its frame and is not the
5118 ;; minibuffer window, try to split it vertically disregarding
5119 ;; the value of `split-height-threshold'.
5120 (let ((split-height-threshold 0))
5121 (when (window-splittable-p window
)
5122 (with-selected-window window
5123 (split-window-below))))))))
5125 (defun window--try-to-split-window (window &optional alist
)
5126 "Try to split WINDOW.
5127 Return value returned by `split-window-preferred-function' if it
5128 represents a live window, nil otherwise."
5129 (and (window-live-p window
)
5130 (not (frame-parameter (window-frame window
) 'unsplittable
))
5131 (let* ((window-combination-limit
5132 ;; When `window-combination-limit' equals
5133 ;; `display-buffer' or equals `resize-window' and a
5134 ;; `window-height' or `window-width' alist entry are
5135 ;; present, bind it to t so resizing steals space
5136 ;; preferably from the window that was split.
5137 (if (or (eq window-combination-limit
'display-buffer
)
5138 (and (eq window-combination-limit
'window-size
)
5139 (or (cdr (assq 'window-height alist
))
5140 (cdr (assq 'window-width alist
)))))
5142 window-combination-limit
))
5144 ;; Since `split-window-preferred-function' might
5145 ;; throw an error use `condition-case'.
5147 (funcall split-window-preferred-function window
)
5149 (and (window-live-p new-window
) new-window
))))
5151 (defun window--frame-usable-p (frame)
5152 "Return FRAME if it can be used to display a buffer."
5153 (when (frame-live-p frame
)
5154 (let ((window (frame-root-window frame
)))
5155 ;; `frame-root-window' may be an internal window which is considered
5156 ;; "dead" by `window-live-p'. Hence if `window' is not live we
5157 ;; implicitly know that `frame' has a visible window we can use.
5158 (unless (and (window-live-p window
)
5159 (or (window-minibuffer-p window
)
5160 ;; If the window is soft-dedicated, the frame is usable.
5161 ;; Actually, even if the window is really dedicated,
5162 ;; the frame is still usable by splitting it.
5163 ;; At least Emacs-22 allowed it, and it is desirable
5164 ;; when displaying same-frame windows.
5165 nil
; (eq t (window-dedicated-p window))
5169 (defcustom even-window-heights t
5170 "If non-nil `display-buffer' will try to even window heights.
5171 Otherwise `display-buffer' will leave the window configuration
5172 alone. Heights are evened only when `display-buffer' chooses a
5173 window that appears above or below the selected window."
5177 (defun window--even-window-heights (window)
5178 "Even heights of WINDOW and selected window.
5179 Do this only if these windows are vertically adjacent to each
5180 other, `even-window-heights' is non-nil, and the selected window
5181 is higher than WINDOW."
5182 (when (and even-window-heights
5183 ;; Even iff WINDOW forms a vertical combination with the
5184 ;; selected window, and WINDOW's height exceeds that of the
5185 ;; selected window, see also bug#11880.
5186 (window-combined-p window
)
5187 (= (window-child-count (window-parent window
)) 2)
5188 (eq (window-parent) (window-parent window
))
5189 (> (window-total-height) (window-total-height window
)))
5190 ;; Don't throw an error if we can't even window heights for
5194 (/ (- (window-total-height window
) (window-total-height)) 2))
5197 (defun window--display-buffer (buffer window type
&optional alist dedicated
)
5198 "Display BUFFER in WINDOW.
5199 TYPE must be one of the symbols `reuse', `window' or `frame' and
5200 is passed unaltered to `display-buffer-record-window'. ALIST is
5201 the alist argument of `display-buffer'. Set `window-dedicated-p'
5202 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
5204 (when (and (buffer-live-p buffer
) (window-live-p window
))
5205 (display-buffer-record-window type window buffer
)
5206 (unless (eq buffer
(window-buffer window
))
5207 (set-window-dedicated-p window nil
)
5208 (set-window-buffer window buffer
)
5210 (set-window-dedicated-p window dedicated
))
5211 (when (memq type
'(window frame
))
5212 (set-window-prev-buffers window nil
)))
5213 (let ((parameter (window-parameter window
'quit-restore
))
5214 (height (cdr (assq 'window-height alist
)))
5215 (width (cdr (assq 'window-width alist
))))
5216 (when (or (eq type
'window
)
5217 (and (eq (car parameter
) 'same
)
5218 (eq (nth 1 parameter
) 'window
)))
5219 ;; Adjust height of window if asked for.
5224 (if (integerp height
)
5227 (* (window-total-size (frame-root-window window
))
5229 (delta (- new-height
(window-total-size window
))))
5230 (when (and (window-resizable-p window delta nil
'safe
)
5231 (window-combined-p window
))
5232 (window-resize window delta nil
'safe
))))
5234 (ignore-errors (funcall height window
))))
5235 ;; Adjust width of window if asked for.
5240 (if (integerp width
)
5243 (* (window-total-size (frame-root-window window
) t
)
5245 (delta (- new-width
(window-total-size window t
))))
5246 (when (and (window-resizable-p window delta t
'safe
)
5247 (window-combined-p window t
))
5248 (window-resize window delta t
'safe
))))
5250 (ignore-errors (funcall width window
))))))
5254 (defun window--maybe-raise-frame (frame)
5255 (let ((visible (frame-visible-p frame
)))
5256 (unless (or (not visible
)
5257 ;; Assume the selected frame is already visible enough.
5258 (eq frame
(selected-frame))
5259 ;; Assume the frame from which we invoked the
5260 ;; minibuffer is visible.
5261 (and (minibuffer-window-active-p (selected-window))
5262 (eq frame
(window-frame (minibuffer-selected-window)))))
5263 (raise-frame frame
))))
5265 ;; FIXME: Not implemented.
5266 ;; FIXME: By the way, there could be more levels of dedication:
5267 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
5268 ;; the window hasn't been used for something else yet.
5269 ;; - `softly' dedicated only allows reuse when asked explicitly.
5270 ;; - `strongly' never allows reuse.
5271 (defvar display-buffer-mark-dedicated nil
5272 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
5273 The actual non-nil value of this variable will be copied to the
5274 `window-dedicated-p' flag.")
5276 (defconst display-buffer--action-function-custom-type
5277 '(choice :tag
"Function"
5278 (const :tag
"--" ignore
) ; default for insertion
5279 (const display-buffer-reuse-window
)
5280 (const display-buffer-pop-up-window
)
5281 (const display-buffer-same-window
)
5282 (const display-buffer-pop-up-frame
)
5283 (const display-buffer-use-some-window
)
5284 (function :tag
"Other function"))
5285 "Custom type for `display-buffer' action functions.")
5287 (defconst display-buffer--action-custom-type
5288 `(cons :tag
"Action"
5289 (choice :tag
"Action functions"
5290 ,display-buffer--action-function-custom-type
5292 :tag
"List of functions"
5293 ,display-buffer--action-function-custom-type
))
5294 (alist :tag
"Action arguments"
5296 :value-type
(sexp :tag
"Value")))
5297 "Custom type for `display-buffer' actions.")
5299 (defvar display-buffer-overriding-action
'(nil . nil
)
5300 "Overriding action to perform to display a buffer.
5301 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5302 function or a list of functions. Each function should accept two
5303 arguments: a buffer to display and an alist similar to ALIST.
5304 See `display-buffer' for details.")
5305 (put 'display-buffer-overriding-action
'risky-local-variable t
)
5307 (defcustom display-buffer-alist nil
5308 "Alist of conditional actions for `display-buffer'.
5309 This is a list of elements (CONDITION . ACTION), where:
5311 CONDITION is either a regexp matching buffer names, or a
5312 function that takes two arguments - a buffer name and the
5313 ACTION argument of `display-buffer' - and returns a boolean.
5315 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
5316 function or a list of functions. Each such function should
5317 accept two arguments: a buffer to display and an alist of the
5318 same form as ALIST. See `display-buffer' for details.
5320 `display-buffer' scans this alist until it either finds a
5321 matching regular expression or the function specified by a
5322 condition returns non-nil. In any of these cases, it adds the
5323 associated action to the list of actions it will try."
5324 :type
`(alist :key-type
5325 (choice :tag
"Condition"
5327 (function :tag
"Matcher function"))
5328 :value-type
,display-buffer--action-custom-type
)
5333 (defcustom display-buffer-base-action
'(nil . nil
)
5334 "User-specified default action for `display-buffer'.
5335 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5336 function or a list of functions. Each function should accept two
5337 arguments: a buffer to display and an alist similar to ALIST.
5338 See `display-buffer' for details."
5339 :type display-buffer--action-custom-type
5344 (defconst display-buffer-fallback-action
5345 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
5346 display-buffer-reuse-window
5347 display-buffer--maybe-pop-up-frame-or-window
5348 display-buffer-use-some-window
5349 ;; If all else fails, pop up a new frame.
5350 display-buffer-pop-up-frame
))
5351 "Default fallback action for `display-buffer'.
5352 This is the action used by `display-buffer' if no other actions
5353 specified, e.g. by the user options `display-buffer-alist' or
5354 `display-buffer-base-action'. See `display-buffer'.")
5355 (put 'display-buffer-fallback-action
'risky-local-variable t
)
5357 (defun display-buffer-assq-regexp (buffer-name alist action
)
5358 "Retrieve ALIST entry corresponding to BUFFER-NAME.
5359 ACTION is the action argument passed to `display-buffer'."
5361 (dolist (entry alist
)
5362 (let ((key (car entry
)))
5363 (when (or (and (stringp key
)
5364 (string-match-p key buffer-name
))
5365 (and (functionp key
)
5366 (funcall key buffer-name action
)))
5367 (throw 'match
(cdr entry
)))))))
5369 (defvar display-buffer--same-window-action
5370 '(display-buffer-same-window
5371 (inhibit-same-window . nil
))
5372 "A `display-buffer' action for displaying in the same window.")
5373 (put 'display-buffer--same-window-action
'risky-local-variable t
)
5375 (defvar display-buffer--other-frame-action
5376 '((display-buffer-reuse-window
5377 display-buffer-pop-up-frame
)
5378 (reusable-frames .
0)
5379 (inhibit-same-window . t
))
5380 "A `display-buffer' action for displaying in another frame.")
5381 (put 'display-buffer--other-frame-action
'risky-local-variable t
)
5383 (defun display-buffer (buffer-or-name &optional action frame
)
5384 "Display BUFFER-OR-NAME in some window, without selecting it.
5385 BUFFER-OR-NAME must be a buffer or the name of an existing
5386 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
5387 or nil if no such window is found.
5389 Optional argument ACTION, if non-nil, should specify a display
5390 action. Its form is described below.
5392 Optional argument FRAME, if non-nil, acts like an additional
5393 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
5394 specifying the frame(s) to search for a window that is already
5395 displaying the buffer. See `display-buffer-reuse-window'
5397 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
5398 where FUNCTION is either a function or a list of functions, and
5399 ALIST is an arbitrary association list (alist).
5401 Each such FUNCTION should accept two arguments: the buffer to
5402 display and an alist. Based on those arguments, it should either
5403 display the buffer and return the window, or return nil if unable
5404 to display the buffer.
5406 The `display-buffer' function builds a function list and an alist
5407 by combining the functions and alists specified in
5408 `display-buffer-overriding-action', `display-buffer-alist', the
5409 ACTION argument, `display-buffer-base-action', and
5410 `display-buffer-fallback-action' (in order). Then it calls each
5411 function in the combined function list in turn, passing the
5412 buffer as the first argument and the combined alist as the second
5413 argument, until one of the functions returns non-nil.
5415 If ACTION is nil, the function list and the alist are built using
5416 only the other variables mentioned above.
5418 Available action functions include:
5419 `display-buffer-same-window'
5420 `display-buffer-reuse-window'
5421 `display-buffer-pop-up-frame'
5422 `display-buffer-pop-up-window'
5423 `display-buffer-use-some-window'
5425 Recognized alist entries include:
5427 `inhibit-same-window' -- A non-nil value prevents the same
5428 window from being used for display.
5430 `inhibit-switch-frame' -- A non-nil value prevents any other
5431 frame from being raised or selected,
5432 even if the window is displayed there.
5434 `reusable-frames' -- Value specifies frame(s) to search for a
5435 window that already displays the buffer.
5436 See `display-buffer-reuse-window'.
5438 `pop-up-frame-parameters' -- Value specifies an alist of frame
5439 parameters to give a new frame, if
5442 `window-height' -- Value specifies either an integer (the number
5443 of lines of a new window), a floating point number (the
5444 fraction of a new window with respect to the height of the
5445 frame's root window) or a function to be called with one
5446 argument - a new window. The function is supposed to adjust
5447 the height of the window; its return value is ignored.
5448 Suitable functions are `shrink-window-if-larger-than-buffer'
5449 and `fit-window-to-buffer'.
5451 `window-width' -- Value specifies either an integer (the number
5452 of columns of a new window), a floating point number (the
5453 fraction of a new window with respect to the width of the
5454 frame's root window) or a function to be called with one
5455 argument - a new window. The function is supposed to adjust
5456 the width of the window; its return value is ignored.
5458 The ACTION argument to `display-buffer' can also have a non-nil
5459 and non-list value. This means to display the buffer in a window
5460 other than the selected one, even if it is already displayed in
5461 the selected window. If called interactively with a prefix
5462 argument, ACTION is t."
5463 (interactive (list (read-buffer "Display buffer: " (other-buffer))
5464 (if current-prefix-arg t
)))
5465 (let ((buffer (if (bufferp buffer-or-name
)
5467 (get-buffer buffer-or-name
)))
5468 ;; Make sure that when we split windows the old window keeps
5469 ;; point, bug#14829.
5470 (split-window-keep-point t
)
5471 ;; Handle the old form of the first argument.
5472 (inhibit-same-window (and action
(not (listp action
)))))
5473 (unless (listp action
) (setq action nil
))
5474 (if display-buffer-function
5475 ;; If `display-buffer-function' is defined, let it do the job.
5476 (funcall display-buffer-function buffer inhibit-same-window
)
5477 ;; Otherwise, use the defined actions.
5479 (display-buffer-assq-regexp
5480 (buffer-name buffer
) display-buffer-alist action
))
5481 (special-action (display-buffer--special-action buffer
))
5482 ;; Extra actions from the arguments to this function:
5484 (cons nil
(append (if inhibit-same-window
5485 '((inhibit-same-window . t
)))
5487 `((reusable-frames .
,frame
))))))
5488 ;; Construct action function list and action alist.
5489 (actions (list display-buffer-overriding-action
5490 user-action special-action action extra-action
5491 display-buffer-base-action
5492 display-buffer-fallback-action
))
5493 (functions (apply 'append
5496 (if (functionp x
) (list x
) x
))
5498 (alist (apply 'append
(mapcar 'cdr actions
)))
5500 (unless (buffer-live-p buffer
)
5501 (error "Invalid buffer"))
5502 (while (and functions
(not window
))
5503 (setq window
(funcall (car functions
) buffer alist
)
5504 functions
(cdr functions
)))
5507 (defun display-buffer-other-frame (buffer)
5508 "Display buffer BUFFER in another frame.
5509 This uses the function `display-buffer' as a subroutine; see
5510 its documentation for additional customization information."
5511 (interactive "BDisplay buffer in other frame: ")
5512 (display-buffer buffer display-buffer--other-frame-action t
))
5514 ;;; `display-buffer' action functions:
5516 (defun display-buffer-same-window (buffer alist
)
5517 "Display BUFFER in the selected window.
5518 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
5519 if the selected window is a minibuffer window or is dedicated to
5520 another buffer; in that case, return nil. Otherwise, return the
5522 (unless (or (cdr (assq 'inhibit-same-window alist
))
5523 (window-minibuffer-p)
5524 (window-dedicated-p))
5525 (window--display-buffer buffer
(selected-window) 'reuse alist
)))
5527 (defun display-buffer--maybe-same-window (buffer alist
)
5528 "Conditionally display BUFFER in the selected window.
5529 If `same-window-p' returns non-nil for BUFFER's name, call
5530 `display-buffer-same-window' and return its value. Otherwise,
5532 (and (same-window-p (buffer-name buffer
))
5533 (display-buffer-same-window buffer alist
)))
5535 (defun display-buffer-reuse-window (buffer alist
)
5536 "Return a window that is already displaying BUFFER.
5537 Return nil if no usable window is found.
5539 If ALIST has a non-nil `inhibit-same-window' entry, the selected
5540 window is not eligible for reuse.
5542 If ALIST contains a `reusable-frames' entry, its value determines
5543 which frames to search for a reusable window:
5544 nil -- the selected frame (actually the last non-minibuffer frame)
5545 A frame -- just that frame
5546 `visible' -- all visible frames
5547 0 -- all frames on the current terminal
5550 If ALIST contains no `reusable-frames' entry, search just the
5551 selected frame if `display-buffer-reuse-frames' and
5552 `pop-up-frames' are both nil; search all frames on the current
5553 terminal if either of those variables is non-nil.
5555 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5556 event that a window on another frame is chosen, avoid raising
5558 (let* ((alist-entry (assq 'reusable-frames alist
))
5559 (frames (cond (alist-entry (cdr alist-entry
))
5560 ((if (eq pop-up-frames
'graphic-only
)
5564 (display-buffer-reuse-frames 0)
5565 (t (last-nonminibuffer-frame))))
5566 (window (if (and (eq buffer
(window-buffer))
5567 (not (cdr (assq 'inhibit-same-window alist
))))
5569 (car (delq (selected-window)
5570 (get-buffer-window-list buffer
'nomini
5572 (when (window-live-p window
)
5573 (prog1 (window--display-buffer buffer window
'reuse alist
)
5574 (unless (cdr (assq 'inhibit-switch-frame alist
))
5575 (window--maybe-raise-frame (window-frame window
)))))))
5577 (defun display-buffer--special-action (buffer)
5578 "Return special display action for BUFFER, if any.
5579 If `special-display-p' returns non-nil for BUFFER, return an
5580 appropriate display action involving `special-display-function'.
5581 See `display-buffer' for the format of display actions."
5582 (and special-display-function
5583 ;; `special-display-p' returns either t or a list of frame
5584 ;; parameters to pass to `special-display-function'.
5585 (let ((pars (special-display-p (buffer-name buffer
))))
5587 (list (list #'display-buffer-reuse-window
5588 `(lambda (buffer _alist
)
5589 (funcall special-display-function
5590 buffer
',(if (listp pars
) pars
)))))))))
5592 (defun display-buffer-pop-up-frame (buffer alist
)
5593 "Display BUFFER in a new frame.
5594 This works by calling `pop-up-frame-function'. If successful,
5595 return the window used; otherwise return nil.
5597 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
5598 raising the new frame.
5600 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
5601 corresponding value is an alist of frame parameters to give the
5603 (let* ((params (cdr (assq 'pop-up-frame-parameters alist
)))
5604 (pop-up-frame-alist (append params pop-up-frame-alist
))
5605 (fun pop-up-frame-function
)
5608 (setq frame
(funcall fun
))
5609 (setq window
(frame-selected-window frame
)))
5610 (prog1 (window--display-buffer
5611 buffer window
'frame alist display-buffer-mark-dedicated
)
5612 (unless (cdr (assq 'inhibit-switch-frame alist
))
5613 (window--maybe-raise-frame frame
))))))
5615 (defun display-buffer-pop-up-window (buffer alist
)
5616 "Display BUFFER by popping up a new window.
5617 The new window is created on the selected frame, or in
5618 `last-nonminibuffer-frame' if no windows can be created there.
5619 If successful, return the new window; otherwise return nil.
5621 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5622 event that the new window is created on another frame, avoid
5624 (let ((frame (or (window--frame-usable-p (selected-frame))
5625 (window--frame-usable-p (last-nonminibuffer-frame))))
5627 (when (and (or (not (frame-parameter frame
'unsplittable
))
5628 ;; If the selected frame cannot be split, look at
5629 ;; `last-nonminibuffer-frame'.
5630 (and (eq frame
(selected-frame))
5631 (setq frame
(last-nonminibuffer-frame))
5632 (window--frame-usable-p frame
)
5633 (not (frame-parameter frame
'unsplittable
))))
5634 ;; Attempt to split largest or least recently used window.
5635 (setq window
(or (window--try-to-split-window
5636 (get-largest-window frame t
) alist
)
5637 (window--try-to-split-window
5638 (get-lru-window frame t
) alist
))))
5639 (prog1 (window--display-buffer
5640 buffer window
'window alist display-buffer-mark-dedicated
)
5641 (unless (cdr (assq 'inhibit-switch-frame alist
))
5642 (window--maybe-raise-frame (window-frame window
)))))))
5644 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist
)
5645 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
5647 If `pop-up-frames' is non-nil (and not `graphic-only' on a
5648 text-only terminal), try with `display-buffer-pop-up-frame'.
5650 If that cannot be done, and `pop-up-windows' is non-nil, try
5651 again with `display-buffer-pop-up-window'."
5652 (or (and (if (eq pop-up-frames
'graphic-only
)
5655 (display-buffer-pop-up-frame buffer alist
))
5657 (display-buffer-pop-up-window buffer alist
))))
5659 (defun display-buffer-below-selected (buffer alist
)
5660 "Try displaying BUFFER in a window below the selected window.
5661 This either splits the selected window or reuses the window below
5664 (or (and (not (frame-parameter nil
'unsplittable
))
5665 (setq window
(window--try-to-split-window (selected-window) alist
))
5666 (window--display-buffer
5667 buffer window
'window alist display-buffer-mark-dedicated
))
5668 (and (setq window
(window-in-direction 'below
))
5669 (not (window-dedicated-p window
))
5670 (window--display-buffer
5671 buffer window
'reuse alist display-buffer-mark-dedicated
)))))
5673 (defun display-buffer-at-bottom (buffer alist
)
5674 "Try displaying BUFFER in a window at the bottom of the selected frame.
5675 This either splits the window at the bottom of the frame or the
5676 frame's root window, or reuses an existing window at the bottom
5677 of the selected frame."
5678 (let (bottom-window window
)
5679 (walk-window-tree (lambda (window) (setq bottom-window window
)))
5680 (or (and (not (frame-parameter nil
'unsplittable
))
5681 (setq window
(window--try-to-split-window bottom-window alist
))
5682 (window--display-buffer
5683 buffer window
'window alist display-buffer-mark-dedicated
))
5684 (and (not (frame-parameter nil
'unsplittable
))
5687 (split-window (frame-root-window))
5689 (window--display-buffer
5690 buffer window
'window alist display-buffer-mark-dedicated
))
5691 (and (setq window bottom-window
)
5692 (not (window-dedicated-p window
))
5693 (window--display-buffer
5694 buffer window
'reuse alist display-buffer-mark-dedicated
)))))
5696 (defun display-buffer-in-previous-window (buffer alist
)
5697 "Display BUFFER in a window previously showing it.
5698 If ALIST has a non-nil `inhibit-same-window' entry, the selected
5699 window is not eligible for reuse.
5701 If ALIST contains a `reusable-frames' entry, its value determines
5702 which frames to search for a reusable window:
5703 nil -- the selected frame (actually the last non-minibuffer frame)
5704 A frame -- just that frame
5705 `visible' -- all visible frames
5706 0 -- all frames on the current terminal
5709 If ALIST contains no `reusable-frames' entry, search just the
5710 selected frame if `display-buffer-reuse-frames' and
5711 `pop-up-frames' are both nil; search all frames on the current
5712 terminal if either of those variables is non-nil.
5714 If ALIST has a `previous-window' entry, the window specified by
5715 that entry will override any other window found by the methods
5716 above, even if that window never showed BUFFER before."
5717 (let* ((alist-entry (assq 'reusable-frames alist
))
5718 (inhibit-same-window
5719 (cdr (assq 'inhibit-same-window alist
)))
5721 (alist-entry (cdr alist-entry
))
5722 ((if (eq pop-up-frames
'graphic-only
)
5726 (display-buffer-reuse-frames 0)
5727 (t (last-nonminibuffer-frame))))
5728 best-window second-best-window window
)
5729 ;; Scan windows whether they have shown the buffer recently.
5731 (dolist (window (window-list-1 (frame-first-window) 'nomini frames
))
5732 (when (and (assq buffer
(window-prev-buffers window
))
5733 (not (window-dedicated-p window
)))
5734 (if (eq window
(selected-window))
5735 (unless inhibit-same-window
5736 (setq second-best-window window
))
5737 (setq best-window window
)
5739 ;; When ALIST has a `previous-window' entry, that entry may override
5740 ;; anything we found so far.
5741 (when (and (setq window
(cdr (assq 'previous-window alist
)))
5742 (window-live-p window
)
5743 (not (window-dedicated-p window
)))
5744 (if (eq window
(selected-window))
5745 (unless inhibit-same-window
5746 (setq second-best-window window
))
5747 (setq best-window window
)))
5748 ;; Return best or second best window found.
5749 (when (setq window
(or best-window second-best-window
))
5750 (window--display-buffer buffer window
'reuse alist
))))
5752 (defun display-buffer-use-some-window (buffer alist
)
5753 "Display BUFFER in an existing window.
5754 Search for a usable window, set that window to the buffer, and
5755 return the window. If no suitable window is found, return nil.
5757 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5758 event that a window in another frame is chosen, avoid raising
5760 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist
)))
5761 (frame (or (window--frame-usable-p (selected-frame))
5762 (window--frame-usable-p (last-nonminibuffer-frame))))
5764 ;; Reuse an existing window.
5765 (or (get-lru-window frame nil not-this-window
)
5766 (let ((window (get-buffer-window buffer
'visible
)))
5767 (unless (and not-this-window
5768 (eq window
(selected-window)))
5770 (get-largest-window 'visible nil not-this-window
)
5771 (let ((window (get-buffer-window buffer
0)))
5772 (unless (and not-this-window
5773 (eq window
(selected-window)))
5775 (get-largest-window 0 not-this-window
))))
5776 (when (window-live-p window
)
5778 (window--display-buffer buffer window
'reuse alist
)
5779 (window--even-window-heights window
)
5780 (unless (cdr (assq 'inhibit-switch-frame alist
))
5781 (window--maybe-raise-frame (window-frame window
)))))))
5783 ;;; Display + selection commands:
5784 (defun pop-to-buffer (buffer &optional action norecord
)
5785 "Select buffer BUFFER in some window, preferably a different one.
5786 BUFFER may be a buffer, a string (a buffer name), or nil. If it
5787 is a string not naming an existent buffer, create a buffer with
5788 that name. If BUFFER is nil, choose some other buffer. Return
5791 This uses `display-buffer' as a subroutine. The optional ACTION
5792 argument is passed to `display-buffer' as its ACTION argument.
5793 See `display-buffer' for more information. ACTION is t if called
5794 interactively with a prefix argument, which means to pop to a
5795 window other than the selected one even if the buffer is already
5796 displayed in the selected window.
5798 If the window to show BUFFER is not on the selected
5799 frame, raise that window's frame and give it input focus.
5801 Optional third arg NORECORD non-nil means do not put this buffer
5802 at the front of the list of recently selected ones."
5803 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
5804 (if current-prefix-arg t
)))
5805 (setq buffer
(window-normalize-buffer-to-switch-to buffer
))
5807 (let* ((old-frame (selected-frame))
5808 (window (display-buffer buffer action
))
5809 (frame (window-frame window
)))
5810 ;; If we chose another frame, make sure it gets input focus.
5811 (unless (eq frame old-frame
)
5812 (select-frame-set-input-focus frame norecord
))
5813 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
5814 (select-window window norecord
)
5817 (defun pop-to-buffer-same-window (buffer &optional norecord
)
5818 "Select buffer BUFFER in some window, preferably the same one.
5819 This function behaves much like `switch-to-buffer', except it
5820 displays with `special-display-function' if BUFFER has a match in
5821 `special-display-buffer-names' or `special-display-regexps'.
5823 Unlike `pop-to-buffer', this function prefers using the selected
5824 window over popping up a new window or frame.
5826 BUFFER may be a buffer, a string (a buffer name), or nil. If it
5827 is a string not naming an existent buffer, create a buffer with
5828 that name. If BUFFER is nil, choose some other buffer. Return
5831 NORECORD, if non-nil means do not put this buffer at the front of
5832 the list of recently selected ones."
5833 (pop-to-buffer buffer display-buffer--same-window-action norecord
))
5835 (defun read-buffer-to-switch (prompt)
5836 "Read the name of a buffer to switch to, prompting with PROMPT.
5837 Return the name of the buffer as a string.
5839 This function is intended for the `switch-to-buffer' family of
5840 commands since these need to omit the name of the current buffer
5841 from the list of completions and default values."
5842 (let ((rbts-completion-table (internal-complete-buffer-except)))
5843 (minibuffer-with-setup-hook
5845 (setq minibuffer-completion-table rbts-completion-table
)
5846 ;; Since rbts-completion-table is built dynamically, we
5847 ;; can't just add it to the default value of
5848 ;; icomplete-with-completion-tables, so we add it
5850 (if (and (boundp 'icomplete-with-completion-tables
)
5851 (listp icomplete-with-completion-tables
))
5852 (set (make-local-variable 'icomplete-with-completion-tables
)
5853 (cons rbts-completion-table
5854 icomplete-with-completion-tables
))))
5855 (read-buffer prompt
(other-buffer (current-buffer))
5856 (confirm-nonexistent-file-or-buffer)))))
5858 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
5859 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
5860 If BUFFER-OR-NAME is nil, return the buffer returned by
5861 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
5862 exists, return that buffer. If no such buffer exists, create a
5863 buffer with the name BUFFER-OR-NAME and return that buffer."
5865 (or (get-buffer buffer-or-name
)
5866 (let ((buffer (get-buffer-create buffer-or-name
)))
5867 (set-buffer-major-mode buffer
)
5871 (defcustom switch-to-buffer-preserve-window-point nil
5872 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
5873 If this is nil, `switch-to-buffer' displays the buffer at that
5874 buffer's `point'. If this is `already-displayed', it tries to
5875 display the buffer at its previous position in the selected
5876 window, provided the buffer is currently displayed in some other
5877 window on any visible or iconified frame. If this is t, it
5878 unconditionally tries to display the buffer at its previous
5879 position in the selected window.
5881 This variable is ignored if the buffer is already displayed in
5882 the selected window or never appeared in it before, or if
5883 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
5885 (const :tag
"Never" nil
)
5886 (const :tag
"If already displayed elsewhere" already-displayed
)
5887 (const :tag
"Always" t
))
5891 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window
)
5892 "Display buffer BUFFER-OR-NAME in the selected window.
5894 WARNING: This is NOT the way to work on another buffer temporarily
5895 within a Lisp program! Use `set-buffer' instead. That avoids
5896 messing with the window-buffer correspondences.
5898 If the selected window cannot display the specified
5899 buffer (e.g. if it is a minibuffer window or strongly dedicated
5900 to another buffer), call `pop-to-buffer' to select the buffer in
5903 If called interactively, read the buffer name using the
5904 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5905 determines whether to request confirmation before creating a new
5908 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
5909 If BUFFER-OR-NAME is a string that does not identify an existing
5910 buffer, create a buffer with that name. If BUFFER-OR-NAME is
5911 nil, switch to the buffer returned by `other-buffer'.
5913 If optional argument NORECORD is non-nil, do not put the buffer
5914 at the front of the buffer list, and do not make the window
5915 displaying it the most recently selected one.
5917 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
5918 must be displayed in the selected window; if that is impossible,
5919 signal an error rather than calling `pop-to-buffer'.
5921 The option `switch-to-buffer-preserve-window-point' can be used
5922 to make the buffer appear at its last position in the selected
5925 Return the buffer switched to."
5927 (list (read-buffer-to-switch "Switch to buffer: ") nil
'force-same-window
))
5928 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name
)))
5930 ;; Don't call set-window-buffer if it's not needed since it
5931 ;; might signal an error (e.g. if the window is dedicated).
5932 ((eq buffer
(window-buffer)))
5933 ((window-minibuffer-p)
5934 (if force-same-window
5935 (user-error "Cannot switch buffers in minibuffer window")
5936 (pop-to-buffer buffer norecord
)))
5937 ((eq (window-dedicated-p) t
)
5938 (if force-same-window
5939 (user-error "Cannot switch buffers in a dedicated window")
5940 (pop-to-buffer buffer norecord
)))
5942 (let* ((entry (assq buffer
(window-prev-buffers)))
5943 (displayed (and (eq switch-to-buffer-preserve-window-point
5945 (get-buffer-window buffer
0))))
5946 (set-window-buffer nil buffer
)
5948 (or (eq switch-to-buffer-preserve-window-point t
)
5950 ;; Try to restore start and point of buffer in the selected
5951 ;; window (Bug#4041).
5952 (set-window-start (selected-window) (nth 1 entry
) t
)
5953 (set-window-point nil
(nth 2 entry
))))))
5956 (select-window (selected-window)))
5957 (set-buffer buffer
)))
5959 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord
)
5960 "Select the buffer specified by BUFFER-OR-NAME in another window.
5961 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5962 nil. Return the buffer switched to.
5964 If called interactively, prompt for the buffer name using the
5965 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5966 determines whether to request confirmation before creating a new
5969 If BUFFER-OR-NAME is a string and does not identify an existing
5970 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5971 nil, switch to the buffer returned by `other-buffer'.
5973 Optional second argument NORECORD non-nil means do not put this
5974 buffer at the front of the list of recently selected ones.
5976 This uses the function `display-buffer' as a subroutine; see its
5977 documentation for additional customization information."
5979 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5980 (let ((pop-up-windows t
))
5981 (pop-to-buffer buffer-or-name t norecord
)))
5983 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord
)
5984 "Switch to buffer BUFFER-OR-NAME in another frame.
5985 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5986 nil. Return the buffer switched to.
5988 If called interactively, prompt for the buffer name using the
5989 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5990 determines whether to request confirmation before creating a new
5993 If BUFFER-OR-NAME is a string and does not identify an existing
5994 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5995 nil, switch to the buffer returned by `other-buffer'.
5997 Optional second arg NORECORD non-nil means do not put this
5998 buffer at the front of the list of recently selected ones.
6000 This uses the function `display-buffer' as a subroutine; see its
6001 documentation for additional customization information."
6003 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
6004 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord
))
6006 (defun set-window-text-height (window height
)
6007 "Set the height in lines of the text display area of WINDOW to HEIGHT.
6008 WINDOW must be a live window and defaults to the selected one.
6009 HEIGHT doesn't include the mode line or header line, if any, or
6010 any partial-height lines in the text display area.
6012 Note that the current implementation of this function cannot
6013 always set the height exactly, but attempts to be conservative,
6014 by allocating more lines than are actually needed in the case
6015 where some error may be present."
6016 (setq window
(window-normalize-window window t
))
6017 (let ((delta (- height
(window-text-height window
))))
6018 (unless (zerop delta
)
6019 ;; Setting window-min-height to a value like 1 can lead to very
6020 ;; bizarre displays because it also allows Emacs to make *other*
6021 ;; windows one line tall, which means that there's no more space
6022 ;; for the mode line.
6023 (let ((window-min-height (min 2 height
)))
6024 (window-resize window delta
)))))
6026 (defun enlarge-window-horizontally (delta)
6027 "Make selected window DELTA columns wider.
6028 Interactively, if no argument is given, make selected window one
6031 (enlarge-window delta t
))
6033 (defun shrink-window-horizontally (delta)
6034 "Make selected window DELTA columns narrower.
6035 Interactively, if no argument is given, make selected window one
6038 (shrink-window delta t
))
6040 (defun count-screen-lines (&optional beg end count-final-newline window
)
6041 "Return the number of screen lines in the region.
6042 The number of screen lines may be different from the number of actual lines,
6043 due to line breaking, display table, etc.
6045 Optional arguments BEG and END default to `point-min' and `point-max'
6048 If region ends with a newline, ignore it unless optional third argument
6049 COUNT-FINAL-NEWLINE is non-nil.
6051 The optional fourth argument WINDOW specifies the window used for obtaining
6052 parameters such as width, horizontal scrolling, and so on. The default is
6053 to use the selected window's parameters.
6055 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6056 regardless of which buffer is displayed in WINDOW. This makes possible to use
6057 `count-screen-lines' in any buffer, whether or not it is currently displayed
6060 (setq beg
(point-min)))
6062 (setq end
(point-max)))
6068 (narrow-to-region (min beg end
)
6069 (if (and (not count-final-newline
)
6070 (= ?
\n (char-before (max beg end
))))
6073 (goto-char (point-min))
6074 (1+ (vertical-motion (buffer-size) window
))))))
6076 (defun window-buffer-height (window)
6077 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
6078 WINDOW must be a live window and defaults to the selected one."
6079 (setq window
(window-normalize-window window t
))
6080 (with-current-buffer (window-buffer window
)
6082 (count-screen-lines (point-min) (point-max)
6083 ;; If buffer ends with a newline, ignore it when
6084 ;; counting height unless point is after it.
6088 ;;; Resizing buffers to fit their contents exactly.
6089 (defcustom fit-frame-to-buffer nil
6090 "Non-nil means `fit-window-to-buffer' can resize frames.
6091 A frame can be resized if and only if its root window is a live
6092 window. The height of the root window is subject to the values
6093 of `fit-frame-to-buffer-max-height' and `window-min-height'."
6098 (defcustom fit-frame-to-buffer-bottom-margin
4
6099 "Bottom margin for the command `fit-frame-to-buffer'.
6100 This is the number of lines that function leaves free at the bottom of
6101 the display, in order to not obscure any system task bar or panel.
6102 If you do not have one (or if it is vertical) you might want to
6103 reduce this. If it is thicker, you might want to increase this."
6104 ;; If you set this too small, fit-frame-to-buffer can shift the
6105 ;; frame up to avoid the panel.
6110 (defun fit-frame-to-buffer (&optional frame max-height min-height
)
6111 "Adjust height of FRAME to display its buffer contents exactly.
6112 FRAME can be any live frame and defaults to the selected one.
6114 Optional argument MAX-HEIGHT specifies the maximum height of FRAME.
6115 It defaults to the height of the display below the current
6116 top line of FRAME, minus `fit-frame-to-buffer-bottom-margin'.
6117 Optional argument MIN-HEIGHT specifies the minimum height of FRAME.
6118 The default corresponds to `window-min-height'."
6120 (setq frame
(window-normalize-frame frame
))
6121 (let* ((root (frame-root-window frame
))
6123 (+ (- (frame-height frame
) (window-total-size root
))
6125 (frame-top (frame-parameter frame
'top
))
6126 (top (if (consp frame-top
)
6127 (funcall (car frame-top
) (cadr frame-top
))
6130 (- (/ (- (x-display-pixel-height frame
) top
)
6131 (frame-char-height frame
))
6132 fit-frame-to-buffer-bottom-margin
))
6135 (when (and (window-live-p root
) (not (window-size-fixed-p root
)))
6136 (with-selected-window root
6139 (setq max-height frame-max-height
))
6140 ((numberp max-height
)
6141 (setq max-height
(min max-height frame-max-height
)))
6143 (error "%s is an invalid maximum height" max-height
)))
6146 (setq min-height frame-min-height
))
6147 ((numberp min-height
)
6148 (setq min-height
(min min-height frame-min-height
)))
6150 (error "%s is an invalid minimum height" min-height
)))
6151 ;; When tool-bar-mode is enabled and we have just created a new
6152 ;; frame, reserve lines for toolbar resizing. This is needed
6153 ;; because for reasons unknown to me Emacs (1) reserves one line
6154 ;; for the toolbar when making the initial frame and toolbars
6155 ;; are enabled, and (2) later adds the remaining lines needed.
6156 ;; Our code runs IN BETWEEN (1) and (2). YMMV when you're on a
6157 ;; system that behaves differently.
6158 (let ((quit-restore (window-parameter root
'quit-restore
))
6159 (lines (tool-bar-lines-needed frame
)))
6160 (when (and quit-restore
(eq (car quit-restore
) 'frame
)
6161 (not (zerop lines
)))
6162 (setq compensate
(1- lines
))))
6163 (message "%s" compensate
)
6165 ;; Always count a final newline - we don't do any
6166 ;; post-processing, so let's play safe.
6167 (+ (count-screen-lines nil nil t
)
6168 (- (window-body-size))
6170 ;; Move away from final newline.
6171 (when (and (eobp) (bolp) (not (bobp)))
6172 (set-window-point root
(line-beginning-position 0)))
6173 (set-window-start root
(point-min))
6174 (set-window-vscroll root
0)
6178 (min (max (+ (frame-height frame
) delta
)
6181 (error (setq delta nil
))))
6184 (defun fit-window-to-buffer (&optional window max-height min-height
)
6185 "Adjust height of WINDOW to display its buffer's contents exactly.
6186 WINDOW must be a live window and defaults to the selected one.
6188 Optional argument MAX-HEIGHT specifies the maximum height of
6189 WINDOW and defaults to the height of WINDOW's frame. Optional
6190 argument MIN-HEIGHT specifies the minimum height of WINDOW and
6191 defaults to `window-min-height'. Both MAX-HEIGHT and MIN-HEIGHT
6192 are specified in lines and include the mode line and header line,
6195 If WINDOW is a full height window, then if the option
6196 `fit-frame-to-buffer' is non-nil, this calls the function
6197 `fit-frame-to-buffer' to adjust the frame height.
6199 Return the number of lines by which WINDOW was enlarged or
6200 shrunk. If an error occurs during resizing, return nil but don't
6203 Note that even if this function makes WINDOW large enough to show
6204 _all_ lines of its buffer you might not see the first lines when
6205 WINDOW was scrolled."
6207 (setq window
(window-normalize-window window t
))
6209 ((window-size-fixed-p window
))
6210 ((window-full-height-p window
)
6211 (when fit-frame-to-buffer
6212 (fit-frame-to-buffer (window-frame window
))))
6214 (with-selected-window window
6215 (let* ((height (window-total-size))
6217 ;; Adjust MIN-HEIGHT.
6218 (if (numberp min-height
)
6219 ;; Can't get smaller than `window-safe-min-height'.
6220 (max min-height window-safe-min-height
)
6221 ;; Preserve header and mode line if present.
6222 (window-min-size nil nil t
)))
6224 ;; Adjust MAX-HEIGHT.
6225 (if (numberp max-height
)
6226 ;; Can't get larger than height of frame.
6228 (window-total-size (frame-root-window window
)))
6229 ;; Don't delete other windows.
6230 (+ height
(window-max-delta nil nil window
))))
6231 ;; Make `desired-height' the height necessary to show
6232 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
6237 (+ (count-screen-lines)
6238 ;; For non-minibuffers count the mode line, if any.
6239 (if (and (not (window-minibuffer-p window
))
6243 ;; Count the header line, if any.
6244 (if header-line-format
1 0))
6248 (- desired-height
(window-total-size window
)))
6250 (if (> desired-delta
0)
6252 (window-max-delta window nil window
))
6254 (- (window-min-delta window nil window
))))))
6257 ;; Return zero if DELTA became zero in the process.
6259 ;; Don't try to redisplay with the cursor at the end on its
6260 ;; own line--that would force a scroll and spoil things.
6261 (when (and (eobp) (bolp) (not (bobp)))
6262 ;; It's silly to put `point' at the end of the previous
6263 ;; line and so maybe force horizontal scrolling.
6264 (set-window-point window
(line-beginning-position 0)))
6265 ;; Call `window-resize' with OVERRIDE argument equal WINDOW.
6266 (window-resize window delta nil window
)
6267 ;; Check if the last line is surely fully visible. If
6268 ;; not, enlarge the window.
6269 (let ((end (save-excursion
6270 (goto-char (point-max))
6271 (when (and (bolp) (not (bobp)))
6272 ;; Don't include final newline.
6274 (when truncate-lines
6275 ;; If line-wrapping is turned off, test the
6276 ;; beginning of the last line for
6277 ;; visibility instead of the end, as the
6278 ;; end of the line could be invisible by
6279 ;; virtue of extending past the edge of the
6283 (set-window-vscroll window
0)
6284 ;; This loop might in some rare pathological cases raise
6285 ;; an error - another reason for the `condition-case'.
6286 (while (and (< desired-height max-height
)
6287 (= desired-height
(window-total-size))
6288 (not (pos-visible-in-window-p end
)))
6289 (window-resize window
1 nil window
)
6290 (setq desired-height
(1+ desired-height
)))))
6291 (error (setq delta nil
)))
6294 (defun window-safely-shrinkable-p (&optional window
)
6295 "Return t if WINDOW can be shrunk without shrinking other windows.
6296 WINDOW defaults to the selected window."
6297 (with-selected-window (or window
(selected-window))
6298 (let ((edges (window-edges)))
6299 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
6300 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
6302 (defun shrink-window-if-larger-than-buffer (&optional window
)
6303 "Shrink height of WINDOW if its buffer doesn't need so many lines.
6304 More precisely, shrink WINDOW vertically to be as small as
6305 possible, while still showing the full contents of its buffer.
6306 WINDOW must be a live window and defaults to the selected one.
6308 Do not shrink WINDOW to less than `window-min-height' lines. Do
6309 nothing if the buffer contains more lines than the present window
6310 height, or if some of the window's contents are scrolled out of
6311 view, or if shrinking this window would also shrink another
6312 window, or if the window is the only window of its frame.
6314 Return non-nil if the window was shrunk, nil otherwise."
6316 (setq window
(window-normalize-window window t
))
6317 ;; Make sure that WINDOW is vertically combined and `point-min' is
6318 ;; visible (for whatever reason that's needed). The remaining issues
6319 ;; should be taken care of by `fit-window-to-buffer'.
6320 (when (and (window-combined-p window
)
6321 (pos-visible-in-window-p (point-min) window
))
6322 (fit-window-to-buffer window
(window-total-size window
))))
6324 (defun kill-buffer-and-window ()
6325 "Kill the current buffer and delete the selected window."
6327 (let ((window-to-delete (selected-window))
6328 (buffer-to-kill (current-buffer))
6329 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
6332 (add-hook 'kill-buffer-hook delete-window-hook t t
)
6333 (if (kill-buffer (current-buffer))
6334 ;; If `delete-window' failed before, we rerun it to regenerate
6335 ;; the error so it can be seen in the echo area.
6336 (when (eq (selected-window) window-to-delete
)
6338 ;; If the buffer is not dead for some reason (probably because
6339 ;; of a `quit' signal), remove the hook again.
6341 (with-current-buffer buffer-to-kill
6342 (remove-hook 'kill-buffer-hook delete-window-hook t
))))))
6345 (defvar recenter-last-op nil
6346 "Indicates the last recenter operation performed.
6347 Possible values: `top', `middle', `bottom', integer or float numbers.")
6349 (defcustom recenter-positions
'(middle top bottom
)
6350 "Cycling order for `recenter-top-bottom'.
6351 A list of elements with possible values `top', `middle', `bottom',
6352 integer or float numbers that define the cycling order for
6353 the command `recenter-top-bottom'.
6355 Top and bottom destinations are `scroll-margin' lines from the true
6356 window top and bottom. Middle redraws the frame and centers point
6357 vertically within the window. Integer number moves current line to
6358 the specified absolute window-line. Float number between 0.0 and 1.0
6359 means the percentage of the screen space from the top. The default
6360 cycling order is middle -> top -> bottom."
6361 :type
'(repeat (choice
6362 (const :tag
"Top" top
)
6363 (const :tag
"Middle" middle
)
6364 (const :tag
"Bottom" bottom
)
6365 (integer :tag
"Line number")
6366 (float :tag
"Percentage")))
6370 (defun recenter-top-bottom (&optional arg
)
6371 "Move current buffer line to the specified window line.
6372 With no prefix argument, successive calls place point according
6373 to the cycling order defined by `recenter-positions'.
6375 A prefix argument is handled like `recenter':
6376 With numeric prefix ARG, move current line to window-line ARG.
6377 With plain `C-u', move current line to window center."
6380 (arg (recenter arg
)) ; Always respect ARG.
6382 (setq recenter-last-op
6383 (if (eq this-command last-command
)
6384 (car (or (cdr (member recenter-last-op recenter-positions
))
6385 recenter-positions
))
6386 (car recenter-positions
)))
6387 (let ((this-scroll-margin
6388 (min (max 0 scroll-margin
)
6389 (truncate (/ (window-body-height) 4.0)))))
6390 (cond ((eq recenter-last-op
'middle
)
6392 ((eq recenter-last-op
'top
)
6393 (recenter this-scroll-margin
))
6394 ((eq recenter-last-op
'bottom
)
6395 (recenter (- -
1 this-scroll-margin
)))
6396 ((integerp recenter-last-op
)
6397 (recenter recenter-last-op
))
6398 ((floatp recenter-last-op
)
6399 (recenter (round (* recenter-last-op
(window-height))))))))))
6401 (define-key global-map
[?\C-l
] 'recenter-top-bottom
)
6403 (defun move-to-window-line-top-bottom (&optional arg
)
6404 "Position point relative to window.
6406 With a prefix argument ARG, acts like `move-to-window-line'.
6408 With no argument, positions point at center of window.
6409 Successive calls position point at positions defined
6410 by `recenter-positions'."
6413 (arg (move-to-window-line arg
)) ; Always respect ARG.
6415 (setq recenter-last-op
6416 (if (eq this-command last-command
)
6417 (car (or (cdr (member recenter-last-op recenter-positions
))
6418 recenter-positions
))
6419 (car recenter-positions
)))
6420 (let ((this-scroll-margin
6421 (min (max 0 scroll-margin
)
6422 (truncate (/ (window-body-height) 4.0)))))
6423 (cond ((eq recenter-last-op
'middle
)
6424 (call-interactively 'move-to-window-line
))
6425 ((eq recenter-last-op
'top
)
6426 (move-to-window-line this-scroll-margin
))
6427 ((eq recenter-last-op
'bottom
)
6428 (move-to-window-line (- -
1 this-scroll-margin
)))
6429 ((integerp recenter-last-op
)
6430 (move-to-window-line recenter-last-op
))
6431 ((floatp recenter-last-op
)
6432 (move-to-window-line (round (* recenter-last-op
(window-height))))))))))
6434 (define-key global-map
[?\M-r
] 'move-to-window-line-top-bottom
)
6436 ;;; Scrolling commands.
6438 ;;; Scrolling commands which do not signal errors at top/bottom
6439 ;;; of buffer at first key-press (instead move to top/bottom
6442 (defcustom scroll-error-top-bottom nil
6443 "Move point to top/bottom of buffer before signaling a scrolling error.
6444 A value of nil means just signal an error if no more scrolling possible.
6445 A value of t means point moves to the beginning or the end of the buffer
6446 \(depending on scrolling direction) when no more scrolling possible.
6447 When point is already on that position, then signal an error."
6452 (defun scroll-up-command (&optional arg
)
6453 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
6454 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
6455 scroll window further, move cursor to the bottom line.
6456 When point is already on that position, then signal an error.
6457 A near full screen is `next-screen-context-lines' less than a full screen.
6458 Negative ARG means scroll downward.
6459 If ARG is the atom `-', scroll downward by nearly full screen."
6462 ((null scroll-error-top-bottom
)
6465 (scroll-down-command nil
))
6466 ((< (prefix-numeric-value arg
) 0)
6467 (scroll-down-command (- (prefix-numeric-value arg
))))
6469 (scroll-up arg
)) ; signal error
6475 ;; When scrolling by ARG lines can't be done,
6476 ;; move by ARG lines instead.
6478 ;; When ARG is nil for full-screen scrolling,
6479 ;; move to the bottom of the buffer.
6480 (goto-char (point-max))))))))
6482 (put 'scroll-up-command
'scroll-command t
)
6484 (defun scroll-down-command (&optional arg
)
6485 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
6486 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
6487 scroll window further, move cursor to the top line.
6488 When point is already on that position, then signal an error.
6489 A near full screen is `next-screen-context-lines' less than a full screen.
6490 Negative ARG means scroll upward.
6491 If ARG is the atom `-', scroll upward by nearly full screen."
6494 ((null scroll-error-top-bottom
)
6497 (scroll-up-command nil
))
6498 ((< (prefix-numeric-value arg
) 0)
6499 (scroll-up-command (- (prefix-numeric-value arg
))))
6501 (scroll-down arg
)) ; signal error
6505 (beginning-of-buffer
6507 ;; When scrolling by ARG lines can't be done,
6508 ;; move by ARG lines instead.
6509 (forward-line (- arg
))
6510 ;; When ARG is nil for full-screen scrolling,
6511 ;; move to the top of the buffer.
6512 (goto-char (point-min))))))))
6514 (put 'scroll-down-command
'scroll-command t
)
6516 ;;; Scrolling commands which scroll a line instead of full screen.
6518 (defun scroll-up-line (&optional arg
)
6519 "Scroll text of selected window upward ARG lines; or one line if no ARG.
6520 If ARG is omitted or nil, scroll upward by one line.
6521 This is different from `scroll-up-command' that scrolls a full screen."
6523 (scroll-up (or arg
1)))
6525 (put 'scroll-up-line
'scroll-command t
)
6527 (defun scroll-down-line (&optional arg
)
6528 "Scroll text of selected window down ARG lines; or one line if no ARG.
6529 If ARG is omitted or nil, scroll down by one line.
6530 This is different from `scroll-down-command' that scrolls a full screen."
6532 (scroll-down (or arg
1)))
6534 (put 'scroll-down-line
'scroll-command t
)
6537 (defun scroll-other-window-down (&optional lines
)
6538 "Scroll the \"other window\" down.
6539 For more details, see the documentation for `scroll-other-window'."
6541 (scroll-other-window
6542 ;; Just invert the argument's meaning.
6543 ;; We can do that without knowing which window it will be.
6544 (if (eq lines
'-
) nil
6546 (- (prefix-numeric-value lines
))))))
6548 (defun beginning-of-buffer-other-window (arg)
6549 "Move point to the beginning of the buffer in the other window.
6550 Leave mark at previous position.
6551 With arg N, put point N/10 of the way from the true beginning."
6553 (let ((orig-window (selected-window))
6554 (window (other-window-for-scrolling)))
6555 ;; We use unwind-protect rather than save-window-excursion
6556 ;; because the latter would preserve the things we want to change.
6559 (select-window window
)
6560 ;; Set point and mark in that window's buffer.
6562 (beginning-of-buffer arg
))
6563 ;; Set point accordingly.
6565 (select-window orig-window
))))
6567 (defun end-of-buffer-other-window (arg)
6568 "Move point to the end of the buffer in the other window.
6569 Leave mark at previous position.
6570 With arg N, put point N/10 of the way from the true end."
6572 ;; See beginning-of-buffer-other-window for comments.
6573 (let ((orig-window (selected-window))
6574 (window (other-window-for-scrolling)))
6577 (select-window window
)
6579 (end-of-buffer arg
))
6581 (select-window orig-window
))))
6583 (defvar mouse-autoselect-window-timer nil
6584 "Timer used by delayed window autoselection.")
6586 (defvar mouse-autoselect-window-position nil
6587 "Last mouse position recorded by delayed window autoselection.")
6589 (defvar mouse-autoselect-window-window nil
6590 "Last window recorded by delayed window autoselection.")
6592 (defvar mouse-autoselect-window-state nil
6593 "When non-nil, special state of delayed window autoselection.
6594 Possible values are `suspend' (suspend autoselection after a menu or
6595 scrollbar interaction) and `select' (the next invocation of
6596 `handle-select-window' shall select the window immediately).")
6598 (defun mouse-autoselect-window-cancel (&optional force
)
6599 "Cancel delayed window autoselection.
6600 Optional argument FORCE means cancel unconditionally."
6601 (unless (and (not force
)
6602 ;; Don't cancel for select-window or select-frame events
6603 ;; or when the user drags a scroll bar.
6604 (or (memq this-command
6605 '(handle-select-window handle-switch-frame
))
6606 (and (eq this-command
'scroll-bar-toolkit-scroll
)
6607 (memq (nth 4 (event-end last-input-event
))
6608 '(handle end-scroll
)))))
6609 (setq mouse-autoselect-window-state nil
)
6610 (when (timerp mouse-autoselect-window-timer
)
6611 (cancel-timer mouse-autoselect-window-timer
))
6612 (remove-hook 'pre-command-hook
'mouse-autoselect-window-cancel
)))
6614 (defun mouse-autoselect-window-start (mouse-position &optional window suspend
)
6615 "Start delayed window autoselection.
6616 MOUSE-POSITION is the last position where the mouse was seen as returned
6617 by `mouse-position'. Optional argument WINDOW non-nil denotes the
6618 window where the mouse was seen. Optional argument SUSPEND non-nil
6619 means suspend autoselection."
6620 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
6621 (setq mouse-autoselect-window-position mouse-position
)
6622 (when window
(setq mouse-autoselect-window-window window
))
6623 (setq mouse-autoselect-window-state
(when suspend
'suspend
))
6624 ;; Install timer which runs `mouse-autoselect-window-select' after
6625 ;; `mouse-autoselect-window' seconds.
6626 (setq mouse-autoselect-window-timer
6628 (abs mouse-autoselect-window
) nil
'mouse-autoselect-window-select
)))
6630 (defun mouse-autoselect-window-select ()
6631 "Select window with delayed window autoselection.
6632 If the mouse position has stabilized in a non-selected window, select
6633 that window. The minibuffer window is selected only if the minibuffer
6634 is active. This function is run by `mouse-autoselect-window-timer'."
6636 (let* ((mouse-position (mouse-position))
6639 (window-at (cadr mouse-position
) (cddr mouse-position
)
6640 (car mouse-position
)))))
6642 ((or (menu-or-popup-active-p)
6644 (not (coordinates-in-window-p (cdr mouse-position
) window
))))
6645 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
6646 ;; of WINDOW, temporarily suspend delayed autoselection.
6647 (mouse-autoselect-window-start mouse-position nil t
))
6648 ((eq mouse-autoselect-window-state
'suspend
)
6649 ;; Delayed autoselection was temporarily suspended, reenable it.
6650 (mouse-autoselect-window-start mouse-position
))
6651 ((and window
(not (eq window
(selected-window)))
6652 (or (not (numberp mouse-autoselect-window
))
6653 (and (> mouse-autoselect-window
0)
6654 ;; If `mouse-autoselect-window' is positive, select
6655 ;; window if the window is the same as before.
6656 (eq window mouse-autoselect-window-window
))
6657 ;; Otherwise select window if the mouse is at the same
6658 ;; position as before. Observe that the first test after
6659 ;; starting autoselection usually fails since the value of
6660 ;; `mouse-autoselect-window-position' recorded there is the
6661 ;; position where the mouse has entered the new window and
6662 ;; not necessarily where the mouse has stopped moving.
6663 (equal mouse-position mouse-autoselect-window-position
))
6664 ;; The minibuffer is a candidate window if it's active.
6665 (or (not (window-minibuffer-p window
))
6666 (eq window
(active-minibuffer-window))))
6667 ;; Mouse position has stabilized in non-selected window: Cancel
6668 ;; delayed autoselection and try to select that window.
6669 (mouse-autoselect-window-cancel t
)
6670 ;; Select window where mouse appears unless the selected window is the
6671 ;; minibuffer. Use `unread-command-events' in order to execute pre-
6672 ;; and post-command hooks and trigger idle timers. To avoid delaying
6673 ;; autoselection again, set `mouse-autoselect-window-state'."
6674 (unless (window-minibuffer-p)
6675 (setq mouse-autoselect-window-state
'select
)
6676 (setq unread-command-events
6677 (cons (list 'select-window
(list window
))
6678 unread-command-events
))))
6679 ((or (and window
(eq window
(selected-window)))
6680 (not (numberp mouse-autoselect-window
))
6681 (equal mouse-position mouse-autoselect-window-position
))
6682 ;; Mouse position has either stabilized in the selected window or at
6683 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
6684 (mouse-autoselect-window-cancel t
))
6686 ;; Mouse position has not stabilized yet, resume delayed
6688 (mouse-autoselect-window-start mouse-position window
))))))
6690 (defun handle-select-window (event)
6691 "Handle select-window events."
6693 (let ((window (posn-window (event-start event
))))
6694 (unless (or (not (window-live-p window
))
6695 ;; Don't switch if we're currently in the minibuffer.
6696 ;; This tries to work around problems where the
6697 ;; minibuffer gets unselected unexpectedly, and where
6698 ;; you then have to move your mouse all the way down to
6699 ;; the minibuffer to select it.
6700 (window-minibuffer-p)
6701 ;; Don't switch to minibuffer window unless it's active.
6702 (and (window-minibuffer-p window
)
6703 (not (minibuffer-window-active-p window
)))
6704 ;; Don't switch when autoselection shall be delayed.
6705 (and (numberp mouse-autoselect-window
)
6706 (not (zerop mouse-autoselect-window
))
6707 (not (eq mouse-autoselect-window-state
'select
))
6709 ;; Cancel any delayed autoselection.
6710 (mouse-autoselect-window-cancel t
)
6711 ;; Start delayed autoselection from current mouse
6712 ;; position and window.
6713 (mouse-autoselect-window-start (mouse-position) window
)
6714 ;; Executing a command cancels delayed autoselection.
6716 'pre-command-hook
'mouse-autoselect-window-cancel
))))
6717 (when mouse-autoselect-window
6718 ;; Reset state of delayed autoselection.
6719 (setq mouse-autoselect-window-state nil
)
6720 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
6721 (run-hooks 'mouse-leave-buffer-hook
))
6724 (select-window window
))))
6726 (defun truncated-partial-width-window-p (&optional window
)
6727 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
6728 WINDOW must be a live window and defaults to the selected one.
6729 Return nil if WINDOW is not a partial-width window
6730 (regardless of the value of `truncate-lines').
6731 Otherwise, consult the value of `truncate-partial-width-windows'
6732 for the buffer shown in WINDOW."
6733 (setq window
(window-normalize-window window t
))
6734 (unless (window-full-width-p window
)
6735 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
6736 (window-buffer window
))))
6737 (if (integerp t-p-w-w
)
6738 (< (window-width window
) t-p-w-w
)
6741 ;; Some of these are in tutorial--default-keys, so update that if you
6743 (define-key ctl-x-map
"0" 'delete-window
)
6744 (define-key ctl-x-map
"1" 'delete-other-windows
)
6745 (define-key ctl-x-map
"2" 'split-window-below
)
6746 (define-key ctl-x-map
"3" 'split-window-right
)
6747 (define-key ctl-x-map
"o" 'other-window
)
6748 (define-key ctl-x-map
"^" 'enlarge-window
)
6749 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
6750 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
6751 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
6752 (define-key ctl-x-map
"+" 'balance-windows
)
6753 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
6755 ;;; window.el ends here