1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2014 Free Software
6 ;; Maintainer: emacs-devel@gnu.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Window tree functions.
31 (defun internal--before-save-selected-window ()
32 (cons (selected-window)
33 ;; We save and restore all frames' selected windows, because
34 ;; `select-window' can change the frame-selected-window of
35 ;; whatever frame that window is in. Each text terminal's
36 ;; top-frame is preserved by putting it last in the list.
38 (mapcar (lambda (terminal)
39 (let ((frames (frames-on-display-list terminal
))
40 (top-frame (tty-top-frame terminal
))
45 (delq top-frame frames
))))
47 (push (cons f
(frame-selected-window f
))
52 (defun internal--after-save-selected-window (state)
53 (dolist (elt (cdr state
))
54 (and (frame-live-p (car elt
))
55 (window-live-p (cdr elt
))
56 (set-frame-selected-window (car elt
) (cdr elt
) 'norecord
)))
57 (when (window-live-p (car state
))
58 (select-window (car state
) 'norecord
)))
60 (defmacro save-selected-window
(&rest body
)
61 "Execute BODY, then select the previously selected window.
62 The value returned is the value of the last form in BODY.
64 This macro saves and restores the selected window, as well as the
65 selected window in each frame. If the previously selected window
66 is no longer live, then whatever window is selected at the end of
67 BODY remains selected. If the previously selected window of some
68 frame is no longer live at the end of BODY, that frame's selected
71 This macro saves and restores the current buffer, since otherwise
72 its normal operation could make a different buffer current. The
73 order of recently selected windows and the buffer list ordering
74 are not altered by this macro (unless they are altered in BODY)."
75 (declare (indent 0) (debug t
))
76 `(let ((save-selected-window--state (internal--before-save-selected-window)))
80 (internal--after-save-selected-window save-selected-window--state
)))))
82 (defvar temp-buffer-window-setup-hook nil
83 "Normal hook run by `with-temp-buffer-window' before buffer display.
84 This hook is run by `with-temp-buffer-window' with the buffer to be
87 (defvar temp-buffer-window-show-hook nil
88 "Normal hook run by `with-temp-buffer-window' after buffer display.
89 This hook is run by `with-temp-buffer-window' with the buffer
90 displayed and current and its window selected.")
92 (defun temp-buffer-window-setup (buffer-or-name)
93 "Set up temporary buffer specified by BUFFER-OR-NAME.
95 (let ((old-dir default-directory
)
96 (buffer (get-buffer-create buffer-or-name
)))
97 (with-current-buffer buffer
98 (kill-all-local-variables)
99 (setq default-directory old-dir
)
100 (delete-all-overlays)
101 (setq buffer-read-only nil
)
102 (setq buffer-file-name nil
)
103 (setq buffer-undo-list t
)
104 (let ((inhibit-read-only t
)
105 (inhibit-modification-hooks t
))
107 (run-hooks 'temp-buffer-window-setup-hook
))
108 ;; Return the buffer.
111 (defun temp-buffer-window-show (&optional buffer action
)
112 "Show temporary buffer BUFFER in a window.
113 Return the window showing BUFFER. Pass ACTION as action argument
114 to `display-buffer'."
116 (with-current-buffer buffer
117 (set-buffer-modified-p nil
)
118 (setq buffer-read-only t
)
119 (goto-char (point-min))
120 (when (let ((window-combination-limit
121 ;; When `window-combination-limit' equals
122 ;; `temp-buffer' or `temp-buffer-resize' and
123 ;; `temp-buffer-resize-mode' is enabled in this
124 ;; buffer bind it to t so resizing steals space
125 ;; preferably from the window that was split.
126 (if (or (eq window-combination-limit
'temp-buffer
)
127 (and (eq window-combination-limit
129 temp-buffer-resize-mode
))
131 window-combination-limit
)))
132 (setq window
(display-buffer buffer action
)))
133 (setq frame
(window-frame window
))
134 (unless (eq frame
(selected-frame))
136 (setq minibuffer-scroll-window window
)
137 (set-window-hscroll window
0)
138 (with-selected-window window
139 (run-hooks 'temp-buffer-window-show-hook
)
140 (when temp-buffer-resize-mode
141 (resize-temp-buffer-window window
)))
142 ;; Return the window.
145 (defmacro with-temp-buffer-window
(buffer-or-name action quit-function
&rest body
)
146 "Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
147 BUFFER-OR-NAME must specify either a live buffer, or the name of
148 a buffer (if it does not exist, this macro creates it).
150 Make the buffer specified by BUFFER-OR-NAME empty before running
151 BODY and bind `standard-output' to that buffer, so that output
152 generated with `prin1' and similar functions in BODY goes into
153 that buffer. Do not make that buffer current for running the
154 forms in BODY. Use `with-current-buffer-window' instead if you
155 need to run BODY with that buffer current.
157 At the end of BODY, mark the specified buffer unmodified and
158 read-only, and display it in a window (but do not select it).
159 The display happens by calling `display-buffer' passing it the
160 ACTION argument. If `temp-buffer-resize-mode' is enabled, the
161 corresponding window may be resized automatically.
163 Return the value returned by BODY, unless QUIT-FUNCTION specifies
164 a function. In that case, run that function with two arguments -
165 the window showing the specified buffer and the value returned by
166 BODY - and return 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 hook
176 `temp-buffer-window-show-hook' after displaying the buffer, with
177 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 neither runs `temp-buffer-setup-hook' which usually puts the
182 buffer in Help mode, nor `temp-buffer-show-function' (the ACTION
183 argument replaces this)."
185 (let ((buffer (make-symbol "buffer"))
186 (window (make-symbol "window"))
187 (value (make-symbol "value")))
188 (macroexp-let2 nil vbuffer-or-name buffer-or-name
189 (macroexp-let2 nil vaction action
190 (macroexp-let2 nil vquit-function quit-function
191 `(let* ((,buffer
(temp-buffer-window-setup ,vbuffer-or-name
))
192 (standard-output ,buffer
)
194 (setq ,value
(progn ,@body
))
195 (with-current-buffer ,buffer
196 (setq ,window
(temp-buffer-window-show ,buffer
,vaction
)))
198 (if (functionp ,vquit-function
)
199 (funcall ,vquit-function
,window
,value
)
202 (defmacro with-current-buffer-window
(buffer-or-name action quit-function
&rest body
)
203 "Evaluate BODY with a buffer BUFFER-OR-NAME current and show that buffer.
204 This construct is like `with-temp-buffer-window' but unlike that
205 makes the buffer specified by BUFFER-OR-NAME current for running
208 (let ((buffer (make-symbol "buffer"))
209 (window (make-symbol "window"))
210 (value (make-symbol "value")))
211 (macroexp-let2 nil vbuffer-or-name buffer-or-name
212 (macroexp-let2 nil vaction action
213 (macroexp-let2 nil vquit-function quit-function
214 `(let* ((,buffer
(temp-buffer-window-setup ,vbuffer-or-name
))
215 (standard-output ,buffer
)
217 (with-current-buffer ,buffer
218 (setq ,value
(progn ,@body
))
219 (setq ,window
(temp-buffer-window-show ,buffer
,vaction
)))
221 (if (functionp ,vquit-function
)
222 (funcall ,vquit-function
,window
,value
)
225 (defmacro with-displayed-buffer-window
(buffer-or-name action quit-function
&rest body
)
226 "Show a buffer BUFFER-OR-NAME and evaluate BODY in that buffer.
227 This construct is like `with-current-buffer-window' but unlike that
228 displays the buffer specified by BUFFER-OR-NAME before running BODY."
230 (let ((buffer (make-symbol "buffer"))
231 (window (make-symbol "window"))
232 (value (make-symbol "value")))
233 (macroexp-let2 nil vbuffer-or-name buffer-or-name
234 (macroexp-let2 nil vaction action
235 (macroexp-let2 nil vquit-function quit-function
236 `(let* ((,buffer
(temp-buffer-window-setup ,vbuffer-or-name
))
237 (standard-output ,buffer
)
239 (with-current-buffer ,buffer
240 (setq ,window
(temp-buffer-window-show ,buffer
,vaction
)))
242 (let ((inhibit-read-only t
)
243 (inhibit-modification-hooks t
))
244 (setq ,value
(progn ,@body
)))
246 (set-window-point ,window
(point-min))
248 (when (functionp (cdr (assq 'window-height
(cdr ,vaction
))))
250 (funcall (cdr (assq 'window-height
(cdr ,vaction
))) ,window
)))
252 (if (functionp ,vquit-function
)
253 (funcall ,vquit-function
,window
,value
)
256 ;; The following two functions are like `window-next-sibling' and
257 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
258 ;; they don't substitute the selected window for nil), and they return
259 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
260 ;; a minibuffer window).
261 (defun window-right (window)
262 "Return WINDOW's right sibling.
263 Return nil if WINDOW is the root window of its frame. WINDOW can
265 (and window
(window-parent window
) (window-next-sibling window
)))
267 (defun window-left (window)
268 "Return WINDOW's left sibling.
269 Return nil if WINDOW is the root window of its frame. WINDOW can
271 (and window
(window-parent window
) (window-prev-sibling window
)))
273 (defun window-child (window)
274 "Return WINDOW's first child window.
275 WINDOW can be any window."
276 (or (window-top-child window
) (window-left-child window
)))
278 (defun window-child-count (window)
279 "Return number of WINDOW's child windows.
280 WINDOW can be any window."
282 (when (and (windowp window
) (setq window
(window-child window
)))
284 (setq count
(1+ count
))
285 (setq window
(window-next-sibling window
))))
288 (defun window-last-child (window)
289 "Return last child window of WINDOW.
290 WINDOW can be any window."
291 (when (and (windowp window
) (setq window
(window-child window
)))
292 (while (window-next-sibling window
)
293 (setq window
(window-next-sibling window
))))
296 (defun window-normalize-buffer (buffer-or-name)
297 "Return buffer specified by BUFFER-OR-NAME.
298 BUFFER-OR-NAME must be either a buffer or a string naming a live
299 buffer and defaults to the current buffer."
301 ((not buffer-or-name
)
303 ((bufferp buffer-or-name
)
304 (if (buffer-live-p buffer-or-name
)
306 (error "Buffer %s is not a live buffer" buffer-or-name
)))
307 ((get-buffer buffer-or-name
))
309 (error "No such buffer %s" buffer-or-name
))))
311 (defun window-normalize-frame (frame)
312 "Return frame specified by FRAME.
313 FRAME must be a live frame and defaults to the selected frame."
315 (if (frame-live-p frame
)
317 (error "%s is not a live frame" frame
))
320 (defun window-normalize-window (window &optional live-only
)
321 "Return the window specified by WINDOW.
322 If WINDOW is nil, return the selected window. Otherwise, if
323 WINDOW is a live or an internal window, return WINDOW; if
324 LIVE-ONLY is non-nil, return WINDOW for a live window only.
325 Otherwise, signal an error."
330 (if (window-live-p window
)
332 (error "%s is not a live window" window
)))
333 ((window-valid-p window
)
336 (error "%s is not a valid window" window
))))
338 ;; Maybe this should go to frame.el.
339 (defun frame-char-size (&optional window-or-frame horizontal
)
340 "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
341 If WINDOW-OR-FRAME is a live frame, return the value of
342 `frame-char-height' for that frame. If WINDOW-OR-FRAME is a
343 valid window, return the value of `frame-char-height' for that
344 window's frame. In any other case, return the value of
345 `frame-char-height' for the selected frame.
347 Optional argument HORIZONTAL non-nil means to return the value of
348 `frame-char-width' for WINDOW-OR-FRAME."
351 ((window-valid-p window-or-frame
)
352 (window-frame window-or-frame
))
353 ((frame-live-p window-or-frame
)
355 (t (selected-frame)))))
357 (frame-char-width frame
)
358 (frame-char-height frame
))))
360 (defvar ignore-window-parameters nil
361 "If non-nil, standard functions ignore window parameters.
362 The functions currently affected by this are `split-window',
363 `delete-window', `delete-other-windows' and `other-window'.
365 An application may bind this to a non-nil value around calls to
366 these functions to inhibit processing of window parameters.")
368 ;; This must go to C, finally (or get removed).
369 (defconst window-safe-min-height
1
370 "The absolute minimum number of lines of any window.
371 Anything less might crash Emacs.")
373 (defun window-safe-min-pixel-height (&optional window
)
374 "Return the absolute minimum pixel height of WINDOW."
375 (* window-safe-min-height
376 (frame-char-size (window-normalize-window window
))))
378 (defcustom window-min-height
4
379 "The minimum total height, in lines, of any window.
380 The value has to accommodate one text line, a mode and header
381 line, a horizontal scroll bar and a bottom divider, if present.
382 A value less than `window-safe-min-height' is ignored. The value
383 of this variable is honored when windows are resized or split.
385 Applications should never rebind this variable. To resize a
386 window to a height less than the one specified here, an
387 application should instead call `window-resize' with a non-nil
388 IGNORE argument. In order to have `split-window' make a window
389 shorter, explicitly specify the SIZE argument of that function."
394 (defun window-min-pixel-height (&optional window
)
395 "Return the minimum pixel height of window WINDOW."
396 (* (max window-min-height window-safe-min-height
)
397 (frame-char-size window
)))
399 ;; This must go to C, finally (or get removed).
400 (defconst window-safe-min-width
2
401 "The absolute minimum number of columns of a window.
402 Anything less might crash Emacs.")
404 (defun window-safe-min-pixel-width (&optional window
)
405 "Return the absolute minimum pixel width of WINDOW."
406 (* window-safe-min-width
407 (frame-char-size (window-normalize-window window
) t
)))
409 (defcustom window-min-width
10
410 "The minimum total width, in columns, of any window.
411 The value has to accommodate two text columns as well as margins,
412 fringes, a scroll bar and a right divider, if present. A value
413 less than `window-safe-min-width' is ignored. The value of this
414 variable is honored when windows are resized or split.
416 Applications should never rebind this variable. To resize a
417 window to a width less than the one specified here, an
418 application should instead call `window-resize' with a non-nil
419 IGNORE argument. In order to have `split-window' make a window
420 narrower, explicitly specify the SIZE argument of that function."
425 (defun window-min-pixel-width (&optional window
)
426 "Return the minimum pixel width of window WINDOW."
427 (* (max window-min-width window-safe-min-width
)
428 (frame-char-size window t
)))
430 (defun window-safe-min-pixel-size (&optional window horizontal
)
431 "Return the absolute minimum pixel height of WINDOW.
432 Optional argument HORIZONTAL non-nil means return the absolute
433 minimum pixel width of WINDOW."
435 (window-safe-min-pixel-width window
)
436 (window-safe-min-pixel-height window
)))
438 (defun window-combined-p (&optional window horizontal
)
439 "Return non-nil if WINDOW has siblings in a given direction.
440 WINDOW must be a valid window and defaults to the selected one.
442 HORIZONTAL determines a direction for the window combination. If
443 HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
444 a vertical window combination. If HORIZONTAL is non-nil, return
445 non-nil if WINDOW is part of a horizontal window combination."
446 (setq window
(window-normalize-window window
))
447 (let ((parent (window-parent window
)))
450 (window-left-child parent
)
451 (window-top-child parent
)))))
453 (defun window-combination-p (&optional window horizontal
)
454 "Return WINDOW's first child if WINDOW is a vertical combination.
455 WINDOW can be any window and defaults to the selected one.
456 Optional argument HORIZONTAL non-nil means return WINDOW's first
457 child if WINDOW is a horizontal combination."
458 (setq window
(window-normalize-window window
))
460 (window-left-child window
)
461 (window-top-child window
)))
463 (defun window-combinations (window &optional horizontal
)
464 "Return largest number of windows vertically arranged within WINDOW.
465 WINDOW must be a valid window and defaults to the selected one.
466 If HORIZONTAL is non-nil, return the largest number of
467 windows horizontally arranged within WINDOW."
468 (setq window
(window-normalize-window window
))
470 ((window-live-p window
)
471 ;; If WINDOW is live, return 1.
474 (window-left-child window
)
475 (window-top-child window
))
476 ;; If WINDOW is iso-combined, return the sum of the values for all
477 ;; child windows of WINDOW.
478 (let ((child (window-child window
))
482 (+ (window-combinations child horizontal
)
484 (setq child
(window-right child
)))
487 ;; If WINDOW is not iso-combined, return the maximum value of any
488 ;; child window of WINDOW.
489 (let ((child (window-child window
))
493 (max (window-combinations child horizontal
)
495 (setq child
(window-right child
)))
498 (defun walk-window-tree-1 (fun walk-window-tree-window any
&optional sub-only
)
499 "Helper function for `walk-window-tree' and `walk-window-subtree'."
500 (let (walk-window-tree-buffer)
501 (while walk-window-tree-window
502 (setq walk-window-tree-buffer
503 (window-buffer walk-window-tree-window
))
504 (when (or walk-window-tree-buffer any
)
505 (funcall fun walk-window-tree-window
))
506 (unless walk-window-tree-buffer
508 fun
(window-left-child walk-window-tree-window
) any
)
510 fun
(window-top-child walk-window-tree-window
) any
))
512 (setq walk-window-tree-window nil
)
513 (setq walk-window-tree-window
514 (window-right walk-window-tree-window
))))))
516 (defun walk-window-tree (fun &optional frame any minibuf
)
517 "Run function FUN on each live window of FRAME.
518 FUN must be a function with one argument - a window. FRAME must
519 be a live frame and defaults to the selected one. ANY, if
520 non-nil, means to run FUN on all live and internal windows of
523 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
524 window even if it isn't active. MINIBUF nil or omitted means run
525 FUN on FRAME's minibuffer window only if it's active. In both
526 cases the minibuffer window must be part of FRAME. MINIBUF
527 neither nil nor t means never run FUN on the minibuffer window.
529 This function performs a pre-order, depth-first traversal of the
530 window tree. If FUN changes the window tree, the result is
532 (setq frame
(window-normalize-frame frame
))
533 (walk-window-tree-1 fun
(frame-root-window frame
) any
)
534 (when (memq minibuf
'(nil t
))
535 ;; Run FUN on FRAME's minibuffer window if requested.
536 (let ((minibuffer-window (minibuffer-window frame
)))
537 (when (and (window-live-p minibuffer-window
)
538 (eq (window-frame minibuffer-window
) frame
)
540 (minibuffer-window-active-p minibuffer-window
)))
541 (funcall fun minibuffer-window
)))))
543 (defun walk-window-subtree (fun &optional window any
)
544 "Run function FUN on the subtree of windows rooted at WINDOW.
545 WINDOW defaults to the selected window. FUN must be a function
546 with one argument - a window. By default, run FUN only on live
547 windows of the subtree. If the optional argument ANY is non-nil,
548 run FUN on all live and internal windows of the subtree. If
549 WINDOW is live, run FUN on WINDOW only.
551 This function performs a pre-order, depth-first traversal of the
552 subtree rooted at WINDOW. If FUN changes that tree, the result
554 (setq window
(window-normalize-window window
))
555 (walk-window-tree-1 fun window any t
))
557 (defun window-with-parameter (parameter &optional value frame any minibuf
)
558 "Return first window on FRAME with PARAMETER non-nil.
559 FRAME defaults to the selected frame. Optional argument VALUE
560 non-nil means only return a window whose window-parameter value
561 for PARAMETER equals VALUE (comparison is done with `equal').
562 Optional argument ANY non-nil means consider internal windows
565 Optional argument MINIBUF t means consider FRAME's minibuffer
566 window even if it isn't active. MINIBUF nil or omitted means
567 consider FRAME's minibuffer window only if it's active. In both
568 cases the minibuffer window must be part of FRAME. MINIBUF
569 neither nil nor t means never consider the minibuffer window."
574 (when (and (setq this-value
(window-parameter window parameter
))
575 (or (not value
) (equal value this-value
)))
576 (throw 'found window
)))
577 frame any minibuf
))))
580 (defun window-atom-root (&optional window
)
581 "Return root of atomic window WINDOW is a part of.
582 WINDOW must be a valid window and defaults to the selected one.
583 Return nil if WINDOW is not part of an atomic window."
584 (setq window
(window-normalize-window window
))
586 (while (and window
(window-parameter window
'window-atom
))
588 (setq window
(window-parent window
)))
591 (defun window-make-atom (window)
592 "Make WINDOW an atomic window.
593 WINDOW must be an internal window. Return WINDOW."
594 (if (not (window-child window
))
595 (error "Window %s is not an internal window" window
)
598 (unless (window-parameter window
'window-atom
)
599 (set-window-parameter window
'window-atom t
)))
603 (defun display-buffer-in-atom-window (buffer alist
)
604 "Display BUFFER in an atomic window.
605 This function displays BUFFER in a new window that will be
606 combined with an existing window to form an atomic window. If
607 the existing window is already part of an atomic window, add the
608 new window to that atomic window. Operations like `split-window'
609 or `delete-window', when applied to a constituent of an atomic
610 window, are applied atomically to the root of that atomic window.
612 ALIST is an association list of symbols and values. The
613 following symbols can be used.
615 `window' specifies the existing window the new window shall be
616 combined with. Use `window-atom-root' to make the new window a
617 sibling of an atomic window's root. If an internal window is
618 specified here, all children of that window become part of the
619 atomic window too. If no window is specified, the new window
620 becomes a sibling of the selected window. By default, the
621 `window-atom' parameter of the existing window is set to `main'
622 provided it is live and was not set before.
624 `side' denotes the side of the existing window where the new
625 window shall be located. Valid values are `below', `right',
626 `above' and `left'. The default is `below'. By default, the
627 `window-atom' parameter of the new window is set to this value.
629 The return value is the new window, nil when creating that window
631 (let* ((ignore-window-parameters t
)
632 (window-combination-limit t
)
633 (window-combination-resize 'atom
)
634 (window (cdr (assq 'window alist
)))
635 (side (cdr (assq 'side alist
)))
636 (atom (when window
(window-parameter window
'window-atom
)))
638 (setq window
(window-normalize-window window
))
639 (setq root
(window-atom-root window
))
640 ;; Split off new window.
641 (when (setq new
(split-window window nil side
))
643 (if (and root
(not (eq root window
)))
644 ;; When WINDOW was part of an atomic window and we did not
645 ;; split its root, root atomic window at old root.
647 ;; Otherwise, root atomic window at WINDOW's new parent.
648 (window-parent window
)))
649 ;; Assign `window-atom' parameters, if needed.
650 (when (and (not atom
) (window-live-p window
))
651 (set-window-parameter window
'window-atom
'main
))
652 (set-window-parameter new
'window-atom side
)
653 ;; Display BUFFER in NEW and return NEW.
654 (window--display-buffer
655 buffer new
'window alist display-buffer-mark-dedicated
))))
657 (defun window--atom-check-1 (window)
658 "Subroutine of `window--atom-check'."
660 (if (window-parameter window
'window-atom
)
662 (when (or (catch 'reset
665 (if (window-parameter window
'window-atom
)
666 (setq count
(1+ count
))
669 ;; count >= 1 must hold here. If there's no other
670 ;; window around dissolve this atomic window.
672 ;; Dissolve atomic window.
675 (set-window-parameter window
'window-atom nil
))
678 (unless (window-buffer window
)
679 (window--atom-check-1 (window-left-child window
))
680 (window--atom-check-1 (window-top-child window
))))
681 ;; Check right sibling
682 (window--atom-check-1 (window-right window
))))
684 (defun window--atom-check (&optional frame
)
685 "Check atomicity of all windows on FRAME.
686 FRAME defaults to the selected frame. If an atomic window is
687 wrongly configured, reset the atomicity of all its windows on
688 FRAME to nil. An atomic window is wrongly configured if it has
689 no child windows or one of its child windows is not atomic."
690 (window--atom-check-1 (frame-root-window frame
)))
693 (defvar window-sides
'(left top right bottom
)
696 (defcustom window-sides-vertical nil
697 "If non-nil, left and right side windows are full height.
698 Otherwise, top and bottom side windows are full width."
703 (defcustom window-sides-slots
'(nil nil nil nil
)
704 "Maximum number of side window slots.
705 The value is a list of four elements specifying the number of
706 side window slots on (in this order) the left, top, right and
707 bottom side of each frame. If an element is a number, this means
708 to display at most that many side windows on the corresponding
709 side. If an element is nil, this means there's no bound on the
710 number of slots on that side."
715 :value
(nil nil nil nil
)
718 :help-echo
"Maximum slots of left side window."
720 :format
"%[Left%] %v\n"
721 (const :tag
"Unlimited" :format
"%t" nil
)
722 (integer :tag
"Number" :value
2 :size
5))
725 :help-echo
"Maximum slots of top side window."
727 :format
"%[Top%] %v\n"
728 (const :tag
"Unlimited" :format
"%t" nil
)
729 (integer :tag
"Number" :value
3 :size
5))
732 :help-echo
"Maximum slots of right side window."
734 :format
"%[Right%] %v\n"
735 (const :tag
"Unlimited" :format
"%t" nil
)
736 (integer :tag
"Number" :value
2 :size
5))
739 :help-echo
"Maximum slots of bottom side window."
741 :format
"%[Bottom%] %v\n"
742 (const :tag
"Unlimited" :format
"%t" nil
)
743 (integer :tag
"Number" :value
3 :size
5)))
746 (defun window--side-window-p (window)
747 "Return non-nil if WINDOW is a side window or the parent of one."
748 (or (window-parameter window
'window-side
)
749 (and (window-child window
)
750 (or (window-parameter
751 (window-child window
) 'window-side
)
753 (window-last-child window
) 'window-side
)))))
755 (defun window--major-non-side-window (&optional frame
)
756 "Return the major non-side window of frame FRAME.
757 The optional argument FRAME must be a live frame and defaults to
760 If FRAME has at least one side window, the major non-side window
761 is either an internal non-side window such that all other
762 non-side windows on FRAME descend from it, or the single live
763 non-side window of FRAME. If FRAME has no side windows, return
765 (let ((frame (window-normalize-frame frame
))
767 ;; Set major to the _last_ window found by `walk-window-tree' that
768 ;; is not a side window but has a side window as its sibling.
771 (and (not (window-parameter window
'window-side
))
772 (or (and (setq sibling
(window-prev-sibling window
))
773 (window-parameter sibling
'window-side
))
774 (and (setq sibling
(window-next-sibling window
))
775 (window-parameter sibling
'window-side
)))
776 (setq major window
)))
778 (or major
(frame-root-window frame
))))
780 (defun window--major-side-window (side)
781 "Return major side window on SIDE.
782 SIDE must be one of the symbols `left', `top', `right' or
783 `bottom'. Return nil if no such window exists."
784 (let ((root (frame-root-window))
786 ;; (1) If a window on the opposite side exists, return that window's
788 ;; (2) If the new window shall span the entire side, return the
789 ;; frame's root window.
790 ;; (3) If a window on an orthogonal side exists, return that
792 ;; (4) Otherwise return the frame's root window.
794 ((or (and (eq side
'left
)
795 (setq window
(window-with-parameter 'window-side
'right nil t
)))
797 (setq window
(window-with-parameter 'window-side
'bottom nil t
))))
798 (window-prev-sibling window
))
799 ((or (and (eq side
'right
)
800 (setq window
(window-with-parameter 'window-side
'left nil t
)))
801 (and (eq side
'bottom
)
802 (setq window
(window-with-parameter 'window-side
'top nil t
))))
803 (window-next-sibling window
))
804 ((memq side
'(left right
))
806 (window-sides-vertical
808 ((setq window
(window-with-parameter 'window-side
'top nil t
))
809 (window-next-sibling window
))
810 ((setq window
(window-with-parameter 'window-side
'bottom nil t
))
811 (window-prev-sibling window
))
813 ((memq side
'(top bottom
))
815 ((not window-sides-vertical
)
817 ((setq window
(window-with-parameter 'window-side
'left nil t
))
818 (window-next-sibling window
))
819 ((setq window
(window-with-parameter 'window-side
'right nil t
))
820 (window-prev-sibling window
))
823 (defun display-buffer-in-major-side-window (buffer side slot
&optional alist
)
824 "Display BUFFER in a new window on SIDE of the selected frame.
825 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
826 specifies the slot to use. ALIST is an association list of
827 symbols and values as passed to `display-buffer-in-side-window'.
828 This function may be called only if no window on SIDE exists yet.
829 The new window automatically becomes the \"major\" side window on
830 SIDE. Return the new window, nil if its creation window failed."
831 (let* ((left-or-right (memq side
'(left right
)))
832 (major (window--major-side-window side
))
834 ((eq side
'top
) 'above
)
835 ((eq side
'bottom
) 'below
)
837 ;; The following two bindings will tell `split-window' to take
838 ;; the space for the new window from `major' and not make a new
839 ;; parent window unless needed.
840 (window-combination-resize 'side
)
841 (window-combination-limit nil
)
842 (new (split-window major nil on-side
)))
844 ;; Initialize `window-side' parameter of new window to SIDE.
845 (set-window-parameter new
'window-side side
)
846 ;; Install `window-slot' parameter of new window.
847 (set-window-parameter new
'window-slot slot
)
848 ;; Install `delete-window' parameter thus making sure that when
849 ;; the new window is deleted, a side window on the opposite side
850 ;; does not get resized.
851 (set-window-parameter new
'delete-window
'delete-side-window
)
852 ;; Auto-adjust height/width of new window unless a size has been
853 ;; explicitly requested.
854 (unless (if left-or-right
855 (cdr (assq 'window-width alist
))
856 (cdr (assq 'window-height alist
)))
860 (if left-or-right
'window-width
'window-height
)
861 (/ (window-total-size (frame-root-window) left-or-right
)
862 ;; By default use a fourth of the size of the frame's
866 ;; Install BUFFER in new window and return NEW.
867 (window--display-buffer buffer new
'window alist
'side
))))
869 (defun delete-side-window (window)
870 "Delete side window WINDOW."
871 (let ((window-combination-resize
872 (window-parameter (window-parent window
) 'window-side
))
873 (ignore-window-parameters t
))
874 (delete-window window
)))
876 (defun display-buffer-in-side-window (buffer alist
)
877 "Display BUFFER in a side window of the selected frame.
878 ALIST is an association list of symbols and values. The
879 following special symbols can be used in ALIST.
881 `side' denotes the side of the frame where the new window shall
882 be located. Valid values are `bottom', `right', `top' and
883 `left'. The default is `bottom'.
885 `slot' if non-nil, specifies the window slot where to display
886 BUFFER. A value of zero or nil means use the middle slot on
887 the specified side. A negative value means use a slot
888 preceding (that is, above or on the left of) the middle slot.
889 A positive value means use a slot following (that is, below or
890 on the right of) the middle slot. The default is zero."
891 (let ((side (or (cdr (assq 'side alist
)) 'bottom
))
892 (slot (or (cdr (assq 'slot alist
)) 0)))
894 ((not (memq side
'(top bottom left right
)))
895 (error "Invalid side %s specified" side
))
896 ((not (numberp slot
))
897 (error "Invalid slot %s specified" slot
)))
899 (let* ((major (window-with-parameter 'window-side side nil t
))
900 ;; `major' is the major window on SIDE, `windows' the list of
901 ;; life windows on SIDE.
907 (when (eq (window-parameter window
'window-side
) side
)
908 (setq windows
(cons window windows
))))
910 (nreverse windows
))))
911 (slots (when major
(max 1 (window-child-count major
))))
917 ((eq side
'bottom
) 3))
919 window this-window this-slot prev-window next-window
920 best-window best-slot abs-slot
)
923 ((and (numberp max-slots
) (<= max-slots
0))
924 ;; No side-slots available on this side. Don't create an error,
928 ;; No major window exists on this side, make one.
929 (display-buffer-in-major-side-window buffer side slot alist
))
931 ;; Scan windows on SIDE.
933 (dolist (window windows
)
934 (setq this-slot
(window-parameter window
'window-slot
))
936 ;; The following should not happen and probably be checked
937 ;; by window--side-check.
938 ((not (numberp this-slot
)))
940 ;; A window with a matching slot has been found.
941 (setq this-window window
)
944 ;; Check if this window has a better slot value wrt the
945 ;; slot of the window we want.
947 (if (or (and (> this-slot
0) (> slot
0))
948 (and (< this-slot
0) (< slot
0)))
949 (abs (- slot this-slot
))
950 (+ (abs slot
) (abs this-slot
))))
951 (unless (and best-slot
(<= best-slot abs-slot
))
952 (setq best-window window
)
953 (setq best-slot abs-slot
))
956 (setq prev-window window
))
958 (setq next-window window
)))))))
960 ;; `this-window' is the first window with the same SLOT.
961 ;; `prev-window' is the window with the largest slot < SLOT. A new
962 ;; window will be created after it.
963 ;; `next-window' is the window with the smallest slot > SLOT. A new
964 ;; window will be created before it.
965 ;; `best-window' is the window with the smallest absolute difference
966 ;; of its slot and SLOT.
968 ;; Note: We dedicate the window used softly to its buffer to
969 ;; avoid that "other" (non-side) buffer display functions steal
970 ;; it from us. This must eventually become customizable via
971 ;; ALIST (or, better, avoided in the "other" functions).
973 ;; Reuse `this-window'.
974 (window--display-buffer buffer this-window
'reuse alist
'side
))
975 (and (or (not max-slots
) (< slots max-slots
))
977 ;; Make new window before `next-window'.
979 (if (memq side
'(left right
)) 'above
'left
))
980 (window-combination-resize 'side
))
981 (setq window
(split-window next-window nil next-side
))
982 ;; When the new window is deleted, its space
983 ;; is returned to other side windows.
984 (set-window-parameter
985 window
'delete-window
'delete-side-window
)
988 ;; Make new window after `prev-window'.
990 (if (memq side
'(left right
)) 'below
'right
))
991 (window-combination-resize 'side
))
992 (setq window
(split-window prev-window nil prev-side
))
993 ;; When the new window is deleted, its space
994 ;; is returned to other side windows.
995 (set-window-parameter
996 window
'delete-window
'delete-side-window
)
998 (set-window-parameter window
'window-slot slot
)
999 (window--display-buffer buffer window
'window alist
'side
))
1001 ;; Reuse `best-window'.
1003 ;; Give best-window the new slot value.
1004 (set-window-parameter best-window
'window-slot slot
)
1005 (window--display-buffer
1006 buffer best-window
'reuse alist
'side
)))))))))
1008 (defun window--side-check (&optional frame
)
1009 "Check the side window configuration of FRAME.
1010 FRAME defaults to the selected frame.
1012 A valid side window configuration preserves the following two
1015 - If there exists a window whose window-side parameter is
1016 non-nil, there must exist at least one live window whose
1017 window-side parameter is nil.
1019 - If a window W has a non-nil window-side parameter (i) it must
1020 have a parent window and that parent's window-side parameter
1021 must be either nil or the same as for W, and (ii) any child
1022 window of W must have the same window-side parameter as W.
1024 If the configuration is invalid, reset the window-side parameters
1025 of all windows on FRAME to nil."
1026 (let (left top right bottom none side parent parent-side
)
1027 (when (or (catch 'reset
1030 (setq side
(window-parameter window
'window-side
))
1031 (setq parent
(window-parent window
))
1033 (and parent
(window-parameter parent
'window-side
)))
1034 ;; The following `cond' seems a bit tedious, but I'd
1035 ;; rather stick to using just the stack.
1038 (when (not (eq parent-side side
))
1039 ;; A parent whose window-side is non-nil must
1040 ;; have a child with the same window-side.
1043 (when (window-buffer window
)
1044 ;; Record that we have at least one non-side,
1047 ((if (memq side
'(left top
))
1048 (window-prev-sibling window
)
1049 (window-next-sibling window
))
1050 ;; Left and top major side windows must not have a
1051 ;; previous sibling, right and bottom major side
1052 ;; windows must not have a next sibling.
1054 ;; Now check that there's no more than one major
1055 ;; window for any of left, top, right and bottom.
1057 (if left
(throw 'reset t
) (setq left t
)))
1059 (if top
(throw 'reset t
) (setq top t
)))
1061 (if right
(throw 'reset t
) (setq right t
)))
1063 (if bottom
(throw 'reset t
) (setq bottom t
)))
1067 ;; If there's a side window, there must be at least one
1069 (and (or left top right bottom
) (not none
)))
1072 (set-window-parameter window
'window-side nil
))
1075 (defun window--check (&optional frame
)
1076 "Check atomic and side windows on FRAME.
1077 FRAME defaults to the selected frame."
1078 (window--side-check frame
)
1079 (window--atom-check frame
))
1081 ;; Dumping frame/window contents.
1082 (defun window--dump-window (&optional window erase
)
1083 "Dump WINDOW to buffer *window-frame-dump*.
1084 WINDOW must be a valid window and defaults to the selected one.
1085 Optional argument ERASE non-nil means erase *window-frame-dump*
1086 before writing to it."
1087 (setq window
(window-normalize-window window
))
1088 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1089 (when erase
(erase-buffer))
1091 (format "%s parent: %s\n" window
(window-parent window
))
1092 (format "pixel left: %s top: %s size: %s x %s new: %s\n"
1093 (window-pixel-left window
) (window-pixel-top window
)
1094 (window-size window t t
) (window-size window nil t
)
1095 (window-new-pixel window
))
1096 (format "char left: %s top: %s size: %s x %s new: %s\n"
1097 (window-left-column window
) (window-top-line window
)
1098 (window-total-size window t
) (window-total-size window
)
1099 (window-new-total window
))
1100 (format "normal: %s x %s new: %s\n"
1101 (window-normal-size window t
) (window-normal-size window
)
1102 (window-new-normal window
)))
1103 (when (window-live-p window
)
1104 (let ((fringes (window-fringes window
))
1105 (margins (window-margins window
)))
1107 (format "body pixel: %s x %s char: %s x %s\n"
1108 (window-body-width window t
) (window-body-height window t
)
1109 (window-body-width window
) (window-body-height window
))
1110 (format "width left fringe: %s left margin: %s right margin: %s\n"
1111 (car fringes
) (or (car margins
) 0) (or (cdr margins
) 0))
1112 (format "width right fringe: %s scroll-bar: %s divider: %s\n"
1114 (window-scroll-bar-width window
)
1115 (window-right-divider-width window
))
1116 (format "height header-line: %s mode-line: %s divider: %s\n"
1117 (window-header-line-height window
)
1118 (window-mode-line-height window
)
1119 (window-bottom-divider-width window
)))))
1122 (defun window--dump-frame (&optional window-or-frame
)
1123 "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
1124 WINDOW-OR-FRAME can be a frame or a window and defaults to the
1125 selected frame. When WINDOW-OR-FRAME is a window, dump that
1126 window's frame. The buffer *window-frame-dump* is erased before
1130 ((or (not window-or-frame
)
1131 (frame-live-p window-or-frame
))
1132 (frame-root-window window-or-frame
))
1133 ((or (window-live-p window-or-frame
)
1134 (window-child window-or-frame
))
1137 (frame-root-window))))
1138 (frame (window-frame window
)))
1139 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1142 (format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
1143 (frame-pixel-width frame
) (frame-pixel-height frame
)
1144 (frame-total-cols frame
) (frame-text-lines frame
) ; (frame-total-lines frame)
1145 (frame-char-width frame
) (frame-char-height frame
))
1146 (format "frame text pixel: %s x %s cols/lines: %s x %s\n"
1147 (frame-text-width frame
) (frame-text-height frame
)
1148 (frame-text-cols frame
) (frame-text-lines frame
))
1149 (format "tool: %s scroll: %s/%s fringe: %s border: %s right: %s bottom: %s\n\n"
1150 (if (fboundp 'tool-bar-height
)
1151 (tool-bar-height frame t
)
1153 (frame-scroll-bar-width frame
)
1154 (frame-scroll-bar-height frame
)
1155 (frame-fringe-width frame
)
1156 (frame-border-width frame
)
1157 (frame-right-divider-width frame
)
1158 (frame-bottom-divider-width frame
)))
1159 (walk-window-tree 'window--dump-window frame t t
))))
1162 (defun window-total-size (&optional window horizontal round
)
1163 "Return the total height or width of WINDOW.
1164 WINDOW must be a valid window and defaults to the selected one.
1166 If HORIZONTAL is omitted or nil, return the total height of
1167 WINDOW, in lines. If WINDOW is live, its total height includes,
1168 in addition to the height of WINDOW's text, the heights of
1169 WINDOW's mode and header line and a bottom divider, if any.
1171 If HORIZONTAL is non-nil, return the total width of WINDOW, in
1172 columns. If WINDOW is live, its total width includes, in
1173 addition to the width of WINDOW's text, the widths of WINDOW's
1174 fringes, margins, scroll bars and its right divider, if any.
1176 If WINDOW is internal, return the respective size of the screen
1177 areas spanned by its children.
1179 Optional argument ROUND is handled as for `window-total-height'
1180 and `window-total-width'."
1182 (window-total-width window round
)
1183 (window-total-height window round
)))
1185 (defun window-size (&optional window horizontal pixelwise round
)
1186 "Return the height or width of WINDOW.
1187 WINDOW must be a valid window and defaults to the selected one.
1189 If HORIZONTAL is omitted or nil, return the total height of
1190 WINDOW, in lines, like `window-total-height'. Otherwise return
1191 the total width, in columns, like `window-total-width'.
1193 Optional argument PIXELWISE means return the pixel size of WINDOW
1194 like `window-pixel-height' and `window-pixel-width'.
1196 Optional argument ROUND is ignored if PIXELWISE is non-nil and
1197 handled as for `window-total-height' and `window-total-width'
1201 (window-pixel-width window
)
1202 (window-total-width window round
))
1204 (window-pixel-height window
)
1205 (window-total-height window round
))))
1207 (defvar window-size-fixed nil
1208 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
1209 If the value is `height', then only the window's height is fixed.
1210 If the value is `width', then only the window's width is fixed.
1211 Any other non-nil value fixes both the width and the height.
1213 Emacs won't change the size of any window displaying that buffer,
1214 unless it has no other choice (like when deleting a neighboring
1216 (make-variable-buffer-local 'window-size-fixed
)
1218 (defun window--size-ignore-p (window ignore
)
1219 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
1220 (if (window-valid-p ignore
) (eq window ignore
) ignore
))
1222 (defun window-safe-min-size (&optional window horizontal pixelwise
)
1223 "Return safe minimum size of WINDOW.
1224 WINDOW must be a valid window and defaults to the selected one.
1225 Optional argument HORIZONTAL non-nil means return the minimum
1226 number of columns of WINDOW; otherwise return the minimum number
1229 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1231 (setq window
(window-normalize-window window
))
1234 (* window-safe-min-width
1235 (frame-char-width (window-frame window
)))
1236 (* window-safe-min-height
1237 (frame-char-height (window-frame window
))))
1238 (if horizontal window-safe-min-width window-safe-min-height
)))
1240 (defun window-min-size (&optional window horizontal ignore pixelwise
)
1241 "Return the minimum size of WINDOW.
1242 WINDOW must be a valid window and defaults to the selected one.
1243 Optional argument HORIZONTAL non-nil means return the minimum
1244 number of columns of WINDOW; otherwise return the minimum number
1247 Optional argument IGNORE, if non-nil, means ignore restrictions
1248 imposed by fixed size windows, `window-min-height' or
1249 `window-min-width' settings. If IGNORE equals `safe', live
1250 windows may get as small as `window-safe-min-height' lines and
1251 `window-safe-min-width' columns. If IGNORE is a window, ignore
1252 restrictions for that window only. Any other non-nil value
1253 means ignore all of the above restrictions for all windows.
1255 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1258 (window-normalize-window window
) horizontal ignore pixelwise
))
1260 (defun window--min-size-1 (window horizontal ignore pixelwise
)
1261 "Internal function of `window-min-size'."
1262 (let ((sub (window-child window
)))
1265 ;; WINDOW is an internal window.
1266 (if (window-combined-p sub horizontal
)
1267 ;; The minimum size of an iso-combination is the sum of
1268 ;; the minimum sizes of its child windows.
1270 (setq value
(+ value
1272 sub horizontal ignore pixelwise
)))
1273 (setq sub
(window-right sub
)))
1274 ;; The minimum size of an ortho-combination is the maximum
1275 ;; of the minimum sizes of its child windows.
1277 (setq value
(max value
1279 sub horizontal ignore pixelwise
)))
1280 (setq sub
(window-right sub
))))
1282 (with-current-buffer (window-buffer window
)
1284 ((window-minibuffer-p window
)
1285 (if pixelwise
(frame-char-height (window-frame window
)) 1))
1286 ((and (not (window--size-ignore-p window ignore
))
1287 (window-size-fixed-p window horizontal
))
1288 ;; The minimum size of a fixed size window is its size.
1289 (window-size window horizontal pixelwise
))
1291 ;; If IGNORE equals `safe' return the safe value.
1292 (window-safe-min-size window horizontal pixelwise
))
1294 ;; For the minimum width of a window take fringes and
1295 ;; scroll-bars into account. This is questionable and should
1296 ;; be removed as soon as we are able to split (and resize)
1297 ;; windows such that the new (or resized) windows can get a
1298 ;; size less than the user-specified `window-min-height' and
1299 ;; `window-min-width'.
1300 (let* ((char-size (frame-char-size window t
))
1301 (fringes (window-fringes window
))
1302 (margins (window-margins window
))
1304 (+ (window-safe-min-size window t t
)
1305 (* (or (car margins
) 0) char-size
)
1306 (* (or (cdr margins
) 0) char-size
)
1307 (car fringes
) (cadr fringes
)
1308 (window-scroll-bar-width window
)
1309 (window-right-divider-width window
))))
1312 (if window-resize-pixelwise
1314 ;; Round up to next integral of columns.
1315 (* (ceiling pixel-width char-size
) char-size
))
1316 (if (window--size-ignore-p window ignore
)
1318 (window-min-pixel-width window
)))
1320 (ceiling pixel-width char-size
)
1321 (if (window--size-ignore-p window ignore
)
1323 window-min-width
)))))
1324 ((let ((char-size (frame-char-size window
))
1326 (+ (window-safe-min-size window nil t
)
1327 (window-header-line-height window
)
1328 (window-scroll-bar-height window
)
1329 (window-mode-line-height window
)
1330 (window-bottom-divider-width window
))))
1333 (if window-resize-pixelwise
1335 ;; Round up to next integral of lines.
1336 (* (ceiling pixel-height char-size
) char-size
))
1337 (if (window--size-ignore-p window ignore
)
1339 (window-min-pixel-height window
)))
1340 (max (ceiling pixel-height char-size
)
1341 (if (window--size-ignore-p window ignore
)
1343 window-min-height
))))))))))
1345 (defun window-sizable (window delta
&optional horizontal ignore pixelwise
)
1346 "Return DELTA if DELTA lines can be added to WINDOW.
1347 WINDOW must be a valid window and defaults to the selected one.
1348 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1349 columns can be added to WINDOW. A return value of zero means
1350 that no lines (or columns) can be added to WINDOW.
1352 This function looks only at WINDOW and, recursively, its child
1353 windows. The function `window-resizable' looks at other windows
1356 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1357 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1358 return the maximum value in the range 0..DELTA by which WINDOW
1361 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1362 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1363 return the minimum value in the range DELTA..0 by which WINDOW
1366 Optional argument IGNORE non-nil means ignore restrictions
1367 imposed by fixed size windows, `window-min-height' or
1368 `window-min-width' settings. If IGNORE equals `safe', live
1369 windows may get as small as `window-safe-min-height' lines and
1370 `window-safe-min-width' columns. If IGNORE is a window, ignore
1371 restrictions for that window only. Any other non-nil value means
1372 ignore all of the above restrictions for all windows.
1374 Optional argument PIXELWISE non-nil means interpret DELTA as
1376 (setq window
(window-normalize-window window
))
1379 (max (- (window-min-size window horizontal ignore pixelwise
)
1380 (window-size window horizontal pixelwise
))
1382 ((window--size-ignore-p window ignore
)
1385 (if (window-size-fixed-p window horizontal
)
1390 (defun window-sizable-p (window delta
&optional horizontal ignore pixelwise
)
1391 "Return t if WINDOW can be resized by DELTA lines.
1392 WINDOW must be a valid window and defaults to the selected one.
1393 For the meaning of the arguments of this function see the
1394 doc-string of `window-sizable'."
1395 (setq window
(window-normalize-window window
))
1397 (>= (window-sizable window delta horizontal ignore pixelwise
)
1399 (<= (window-sizable window delta horizontal ignore pixelwise
)
1402 (defun window--size-fixed-1 (window horizontal
)
1403 "Internal function for `window-size-fixed-p'."
1404 (let ((sub (window-child window
)))
1407 ;; WINDOW is an internal window.
1408 (if (window-combined-p sub horizontal
)
1409 ;; An iso-combination is fixed size if all its child
1410 ;; windows are fixed-size.
1413 (unless (window--size-fixed-1 sub horizontal
)
1414 ;; We found a non-fixed-size child window, so
1415 ;; WINDOW's size is not fixed.
1417 (setq sub
(window-right sub
)))
1418 ;; All child windows are fixed-size, so WINDOW's size is
1421 ;; An ortho-combination is fixed-size if at least one of its
1422 ;; child windows is fixed-size.
1424 (when (window--size-fixed-1 sub horizontal
)
1425 ;; We found a fixed-size child window, so WINDOW's size
1428 (setq sub
(window-right sub
))))
1429 ;; WINDOW is a live window.
1430 (with-current-buffer (window-buffer window
)
1432 (memq window-size-fixed
'(width t
))
1433 (memq window-size-fixed
'(height t
))))))))
1435 (defun window-size-fixed-p (&optional window horizontal
)
1436 "Return non-nil if WINDOW's height is fixed.
1437 WINDOW must be a valid window and defaults to the selected one.
1438 Optional argument HORIZONTAL non-nil means return non-nil if
1439 WINDOW's width is fixed.
1441 If this function returns nil, this does not necessarily mean that
1442 WINDOW can be resized in the desired direction. The function
1443 `window-resizable' can tell that."
1444 (window--size-fixed-1
1445 (window-normalize-window window
) horizontal
))
1447 (defun window--min-delta-1 (window delta
&optional horizontal ignore trail noup pixelwise
)
1448 "Internal function for `window-min-delta'."
1449 (if (not (window-parent window
))
1450 ;; If we can't go up, return zero.
1452 ;; Else try to find a non-fixed-size sibling of WINDOW.
1453 (let* ((parent (window-parent window
))
1454 (sub (window-child parent
)))
1456 (if (window-combined-p sub horizontal
)
1457 ;; In an iso-combination throw DELTA if we find at least one
1458 ;; child window and that window is either not fixed-size or
1459 ;; we can ignore fixed-sizeness.
1460 (let ((skip (eq trail
'after
)))
1464 (setq skip
(eq trail
'before
)))
1466 ((and (not (window--size-ignore-p window ignore
))
1467 (window-size-fixed-p sub horizontal
)))
1469 ;; We found a non-fixed-size child window.
1470 (throw 'done delta
)))
1471 (setq sub
(window-right sub
))))
1472 ;; In an ortho-combination set DELTA to the minimum value by
1473 ;; which other child windows can shrink.
1475 (unless (eq sub window
)
1478 (max (- (window-size sub horizontal pixelwise
'ceiling
)
1480 sub horizontal ignore pixelwise
))
1482 (setq sub
(window-right sub
))))
1485 (window--min-delta-1
1486 parent delta horizontal ignore trail nil pixelwise
))))))
1488 (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise
)
1489 "Return number of lines by which WINDOW can be shrunk.
1490 WINDOW must be a valid window and defaults to the selected one.
1491 Return zero if WINDOW cannot be shrunk.
1493 Optional argument HORIZONTAL non-nil means return number of
1494 columns by which WINDOW can be shrunk.
1496 Optional argument IGNORE non-nil means ignore restrictions
1497 imposed by fixed size windows, `window-min-height' or
1498 `window-min-width' settings. If IGNORE is a window, ignore
1499 restrictions for that window only. If IGNORE equals `safe',
1500 live windows may get as small as `window-safe-min-height' lines
1501 and `window-safe-min-width' columns. Any other non-nil value
1502 means ignore all of the above restrictions for all windows.
1504 Optional argument TRAIL restricts the windows that can be enlarged.
1505 If its value is `before', only windows to the left of or above WINDOW
1506 can be enlarged. If it is `after', only windows to the right of or
1507 below WINDOW can be enlarged.
1509 Optional argument NOUP non-nil means don't go up in the window
1510 tree, but try to enlarge windows within WINDOW's combination only.
1512 Optional argument NODOWN non-nil means don't check whether WINDOW
1513 itself (and its child windows) can be shrunk; check only whether
1514 at least one other window can be enlarged appropriately.
1516 Optional argument PIXELWISE non-nil means return number of pixels
1517 by which WINDOW can be shrunk."
1518 (setq window
(window-normalize-window window
))
1519 (let ((size (window-size window horizontal pixelwise
'floor
))
1520 (minimum (window-min-size window horizontal ignore pixelwise
)))
1523 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1524 (window--min-delta-1
1525 window size horizontal ignore trail noup pixelwise
))
1527 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1528 ;; there's nothing to recover.
1531 ;; Otherwise, try to recover whatever WINDOW is larger than its
1533 (window--min-delta-1
1534 window
(- size minimum
) horizontal ignore trail noup pixelwise
)))))
1536 (defun frame-windows-min-size (&optional frame horizontal pixelwise
)
1537 "Return minimum number of lines of FRAME's windows.
1538 HORIZONTAL non-nil means return number of columns of FRAME's
1539 windows. PIXELWISE non-nil means return sizes in pixels."
1540 (setq frame
(window-normalize-frame frame
))
1541 (let* ((root (frame-root-window frame
))
1542 (mini (window-next-sibling root
)))
1543 (+ (window-min-size root horizontal nil pixelwise
)
1544 (if (and mini
(not horizontal
))
1545 (window-min-size mini horizontal nil pixelwise
)
1548 (defun window--max-delta-1 (window delta
&optional horizontal ignore trail noup pixelwise
)
1549 "Internal function of `window-max-delta'."
1550 (if (not (window-parent window
))
1551 ;; Can't go up. Return DELTA.
1553 (let* ((parent (window-parent window
))
1554 (sub (window-child parent
)))
1556 (if (window-combined-p sub horizontal
)
1557 ;; For an iso-combination calculate how much we can get from
1558 ;; other child windows.
1559 (let ((skip (eq trail
'after
)))
1563 (setq skip
(eq trail
'before
)))
1569 (- (window-size sub horizontal pixelwise
'floor
)
1571 sub horizontal ignore pixelwise
))
1573 (setq sub
(window-right sub
))))
1574 ;; For an ortho-combination throw DELTA when at least one
1575 ;; child window is fixed-size.
1577 (when (and (not (eq sub window
))
1578 (not (window--size-ignore-p sub ignore
))
1579 (window-size-fixed-p sub horizontal
))
1580 (throw 'fixed delta
))
1581 (setq sub
(window-right sub
))))
1583 ;; When NOUP is nil, DELTA is all we can get.
1585 ;; Else try with parent of WINDOW, passing the DELTA we
1586 ;; recovered so far.
1587 (window--max-delta-1
1588 parent delta horizontal ignore trail nil pixelwise
))))))
1590 (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise
)
1591 "Return maximum number of lines by which WINDOW can be enlarged.
1592 WINDOW must be a valid window and defaults to the selected one.
1593 The return value is zero if WINDOW cannot be enlarged.
1595 Optional argument HORIZONTAL non-nil means return maximum number
1596 of columns by which WINDOW can be enlarged.
1598 Optional argument IGNORE non-nil means ignore restrictions
1599 imposed by fixed size windows, `window-min-height' or
1600 `window-min-width' settings. If IGNORE is a window, ignore
1601 restrictions for that window only. If IGNORE equals `safe',
1602 live windows may get as small as `window-safe-min-height' lines
1603 and `window-safe-min-width' columns. Any other non-nil value means
1604 ignore all of the above restrictions for all windows.
1606 Optional argument TRAIL restricts the windows that can be enlarged.
1607 If its value is `before', only windows to the left of or above WINDOW
1608 can be enlarged. If it is `after', only windows to the right of or
1609 below WINDOW can be enlarged.
1611 Optional argument NOUP non-nil means don't go up in the window
1612 tree but try to obtain the entire space from windows within
1613 WINDOW's combination.
1615 Optional argument NODOWN non-nil means do not check whether
1616 WINDOW itself (and its child windows) can be enlarged; check
1617 only whether other windows can be shrunk appropriately.
1619 Optional argument PIXELWISE non-nil means return number of
1620 pixels by which WINDOW can be enlarged."
1621 (setq window
(window-normalize-window window
))
1622 (if (and (not (window--size-ignore-p window ignore
))
1623 (not nodown
) (window-size-fixed-p window horizontal
))
1624 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1627 ;; WINDOW has no fixed size.
1628 (window--max-delta-1 window
0 horizontal ignore trail noup pixelwise
)))
1630 ;; Make NOUP also inhibit the min-size check.
1631 (defun window--resizable (window delta
&optional horizontal ignore trail noup nodown pixelwise
)
1632 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1633 WINDOW must be a valid window and defaults to the selected one.
1634 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1635 can be resized horizontally by DELTA columns. A return value of
1636 zero means that WINDOW is not resizable.
1638 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1639 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1640 return the maximum value in the range 0..DELTA by which WINDOW
1643 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1644 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1645 return the minimum value in the range DELTA..0 that can be used
1646 for shrinking WINDOW.
1648 Optional argument IGNORE non-nil means ignore restrictions
1649 imposed by fixed size windows, `window-min-height' or
1650 `window-min-width' settings. If IGNORE is a window, ignore
1651 restrictions for that window only. If IGNORE equals `safe',
1652 live windows may get as small as `window-safe-min-height' lines
1653 and `window-safe-min-width' columns. Any other non-nil value
1654 means ignore all of the above restrictions for all windows.
1656 Optional argument TRAIL `before' means only windows to the left
1657 of or below WINDOW can be shrunk. Optional argument TRAIL
1658 `after' means only windows to the right of or above WINDOW can be
1661 Optional argument NOUP non-nil means don't go up in the window
1662 tree but check only whether space can be obtained from (or given
1663 to) WINDOW's siblings.
1665 Optional argument NODOWN non-nil means don't go down in the
1666 window tree. This means do not check whether resizing would
1667 violate size restrictions of WINDOW or its child windows.
1669 Optional argument PIXELWISE non-nil means interpret DELTA as
1671 (setq window
(window-normalize-window window
))
1674 (max (- (window-min-delta
1675 window horizontal ignore trail noup nodown pixelwise
))
1678 (min (window-max-delta
1679 window horizontal ignore trail noup nodown pixelwise
)
1683 (defun window--resizable-p (window delta
&optional horizontal ignore trail noup nodown pixelwise
)
1684 "Return t if WINDOW can be resized vertically by DELTA lines.
1685 WINDOW must be a valid window and defaults to the selected one.
1686 For the meaning of the arguments of this function see the
1687 doc-string of `window--resizable'.
1689 Optional argument PIXELWISE non-nil means interpret DELTA as
1691 (setq window
(window-normalize-window window
))
1693 (>= (window--resizable
1694 window delta horizontal ignore trail noup nodown pixelwise
)
1696 (<= (window--resizable
1697 window delta horizontal ignore trail noup nodown pixelwise
)
1700 (defun window-resizable (window delta
&optional horizontal ignore pixelwise
)
1701 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1702 WINDOW must be a valid window and defaults to the selected one.
1703 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1704 can be resized horizontally by DELTA columns. A return value of
1705 zero means that WINDOW is not resizable.
1707 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1708 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1709 return the maximum value in the range 0..DELTA by which WINDOW
1712 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1713 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1714 return the minimum value in the range DELTA..0 that can be used
1715 for shrinking WINDOW.
1717 Optional argument IGNORE non-nil means ignore restrictions
1718 imposed by fixed size windows, `window-min-height' or
1719 `window-min-width' settings. If IGNORE is a window, ignore
1720 restrictions for that window only. If IGNORE equals `safe',
1721 live windows may get as small as `window-safe-min-height' lines
1722 and `window-safe-min-width' columns. Any other non-nil value
1723 means ignore all of the above restrictions for all windows.
1725 Optional argument PIXELWISE non-nil means interpret DELTA as
1727 (setq window
(window-normalize-window window
))
1728 (window--resizable window delta horizontal ignore nil nil nil pixelwise
))
1730 (defun window-resizable-p (window delta
&optional horizontal ignore pixelwise
)
1731 "Return t if WINDOW can be resized vertically by DELTA lines.
1732 WINDOW must be a valid window and defaults to the selected one.
1733 For the meaning of the arguments of this function see the
1734 doc-string of `window-resizable'."
1735 (setq window
(window-normalize-window window
))
1737 (>= (window--resizable
1738 window delta horizontal ignore nil nil nil pixelwise
)
1740 (<= (window--resizable
1741 window delta horizontal ignore nil nil nil pixelwise
)
1744 ;; Aliases of functions defined in window.c.
1745 (defalias 'window-height
'window-total-height
)
1746 (defalias 'window-width
'window-body-width
)
1748 ;; Eventually the following two should work pixelwise.
1750 ;; See discussion in bug#4543.
1751 (defun window-full-height-p (&optional window
)
1752 "Return t if WINDOW is as high as its containing frame.
1753 More precisely, return t if and only if the total height of
1754 WINDOW equals the total height of the root window of WINDOW's
1755 frame. WINDOW must be a valid window and defaults to the
1757 (setq window
(window-normalize-window window
))
1758 (= (window-pixel-height window
)
1759 (window-pixel-height (frame-root-window window
))))
1761 (defun window-full-width-p (&optional window
)
1762 "Return t if WINDOW is as wide as its containing frame.
1763 More precisely, return t if and only if the total width of WINDOW
1764 equals the total width of the root window of WINDOW's frame.
1765 WINDOW must be a valid window and defaults to the selected one."
1766 (setq window
(window-normalize-window window
))
1767 (= (window-pixel-width window
)
1768 (window-pixel-width (frame-root-window window
))))
1770 (defun window-body-size (&optional window horizontal pixelwise
)
1771 "Return the height or width of WINDOW's text area.
1772 WINDOW must be a live window and defaults to the selected one.
1774 If HORIZONTAL is omitted or nil, return the height of the text
1775 area, like `window-body-height'. Otherwise, return the width of
1776 the text area, like `window-body-width'. In either case, the
1777 optional argument PIXELWISE is passed to the functions."
1779 (window-body-width window pixelwise
)
1780 (window-body-height window pixelwise
)))
1782 (defun window-current-scroll-bars (&optional window
)
1783 "Return the current scroll bar settings for WINDOW.
1784 WINDOW must be a live window and defaults to the selected one.
1786 The return value is a cons cell (VERTICAL . HORIZONTAL) where
1787 VERTICAL specifies the current location of the vertical scroll
1788 bars (`left', `right', or nil), and HORIZONTAL specifies the
1789 current location of the horizontal scroll bars (`top', `bottom',
1792 Unlike `window-scroll-bars', this function reports the scroll bar
1793 type actually used, once frame defaults and `scroll-bar-mode' are
1794 taken into account."
1795 (setq window
(window-normalize-window window t
))
1796 (let ((vert (nth 2 (window-scroll-bars window
)))
1798 (when (or (eq vert t
) (eq hor t
))
1799 (let ((fcsb (frame-current-scroll-bars (window-frame window
))))
1801 (setq vert
(car fcsb
)))
1803 (setq hor
(cdr fcsb
)))))
1806 (defun walk-windows (fun &optional minibuf all-frames
)
1807 "Cycle through all live windows, calling FUN for each one.
1808 FUN must specify a function with a window as its sole argument.
1809 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1810 windows to include in the walk.
1812 MINIBUF t means include the minibuffer window even if the
1813 minibuffer is not active. MINIBUF nil or omitted means include
1814 the minibuffer window only if the minibuffer is active. Any
1815 other value means do not include the minibuffer window even if
1816 the minibuffer is active.
1818 ALL-FRAMES nil or omitted means consider all windows on the
1819 selected frame, plus the minibuffer window if specified by the
1820 MINIBUF argument. If the minibuffer counts, consider all windows
1821 on all frames that share that minibuffer too. The following
1822 non-nil values of ALL-FRAMES have special meanings:
1824 - t means consider all windows on all existing frames.
1826 - `visible' means consider all windows on all visible frames on
1827 the current terminal.
1829 - 0 (the number zero) means consider all windows on all visible
1830 and iconified frames on the current terminal.
1832 - A frame means consider all windows on that frame only.
1834 Anything else means consider all windows on the selected frame
1837 This function changes neither the order of recently selected
1838 windows nor the buffer list."
1839 ;; If we start from the minibuffer window, don't fail to come
1841 (when (window-minibuffer-p)
1843 ;; Make sure to not mess up the order of recently selected
1844 ;; windows. Use `save-selected-window' and `select-window'
1845 ;; with second argument non-nil for this purpose.
1846 (save-selected-window
1847 (when (framep all-frames
)
1848 (select-window (frame-first-window all-frames
) 'norecord
))
1849 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames
))
1850 (funcall fun walk-windows-window
))))
1852 (defun window-at-side-p (&optional window side
)
1853 "Return t if WINDOW is at SIDE of its containing frame.
1854 WINDOW must be a valid window and defaults to the selected one.
1855 SIDE can be any of the symbols `left', `top', `right' or
1856 `bottom'. The default value nil is handled like `bottom'."
1857 (setq window
(window-normalize-window window
))
1862 ((eq side
'right
) 2)
1863 ((memq side
'(bottom nil
)) 3))))
1864 (= (nth edge
(window-pixel-edges window
))
1865 (nth edge
(window-pixel-edges (frame-root-window window
))))))
1867 (defun window-at-side-list (&optional frame side
)
1868 "Return list of all windows on SIDE of FRAME.
1869 FRAME must be a live frame and defaults to the selected frame.
1870 SIDE can be any of the symbols `left', `top', `right' or
1871 `bottom'. The default value nil is handled like `bottom'."
1872 (setq frame
(window-normalize-frame frame
))
1876 (when (window-at-side-p window side
)
1877 (setq windows
(cons window windows
))))
1879 (nreverse windows
)))
1881 (defun window--in-direction-2 (window posn
&optional horizontal
)
1882 "Support function for `window-in-direction'."
1884 (let ((top (window-pixel-top window
)))
1887 (- posn top
(window-pixel-height window
))))
1888 (let ((left (window-pixel-left window
)))
1891 (- posn left
(window-pixel-width window
))))))
1893 ;; Predecessors to the below have been devised by Julian Assange in
1894 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
1895 ;; Neither of these allow to selectively ignore specific windows
1896 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
1898 (defun window-in-direction (direction &optional window ignore sign wrap mini
)
1899 "Return window in DIRECTION as seen from WINDOW.
1900 More precisely, return the nearest window in direction DIRECTION
1901 as seen from the position of `window-point' in window WINDOW.
1902 DIRECTION must be one of `above', `below', `left' or `right'.
1903 WINDOW must be a live window and defaults to the selected one.
1905 Do not return a window whose `no-other-window' parameter is
1906 non-nil. If the nearest window's `no-other-window' parameter is
1907 non-nil, try to find another window in the indicated direction.
1908 If, however, the optional argument IGNORE is non-nil, return that
1909 window even if its `no-other-window' parameter is non-nil.
1911 Optional argument SIGN a negative number means to use the right
1912 or bottom edge of WINDOW as reference position instead of
1913 `window-point'. SIGN a positive number means to use the left or
1914 top edge of WINDOW as reference position.
1916 Optional argument WRAP non-nil means to wrap DIRECTION around
1917 frame borders. This means to return for WINDOW at the top of the
1918 frame and DIRECTION `above' the minibuffer window if the frame
1919 has one, and a window at the bottom of the frame otherwise.
1921 Optional argument MINI nil means to return the minibuffer window
1922 if and only if it is currently active. MINI non-nil means to
1923 return the minibuffer window even when it's not active. However,
1924 if WRAP non-nil, always act as if MINI were nil.
1926 Return nil if no suitable window can be found."
1927 (setq window
(window-normalize-window window t
))
1928 (unless (memq direction
'(above below left right
))
1929 (error "Wrong direction %s" direction
))
1930 (let* ((frame (window-frame window
))
1931 (hor (memq direction
'(left right
)))
1933 (window-pixel-left window
)
1934 (window-pixel-top window
)))
1935 (last (+ first
(window-size window hor t
)))
1936 ;; The column / row value of `posn-at-point' can be nil for the
1937 ;; mini-window, guard against that.
1940 ((and (numberp sign
) (< sign
0))
1942 (1- (+ (window-pixel-top window
) (window-pixel-height window
)))
1943 (1- (+ (window-pixel-left window
) (window-pixel-width window
)))))
1944 ((and (numberp sign
) (> sign
0))
1946 (window-pixel-top window
)
1947 (window-pixel-left window
)))
1948 ((let ((posn-cons (nth 2 (posn-at-point (window-point window
) window
))))
1950 (+ (or (cdr posn-cons
) 1) (window-pixel-top window
))
1951 (+ (or (car posn-cons
) 1) (window-pixel-left window
)))))))
1954 ((eq direction
'below
) (frame-pixel-height frame
))
1955 ((eq direction
'right
) (frame-pixel-width frame
))
1957 (best-edge-2 best-edge
)
1958 (best-diff-2 (if hor
(frame-pixel-height frame
) (frame-pixel-width frame
)))
1959 best best-2 best-diff-2-new
)
1962 (let* ((w-top (window-pixel-top w
))
1963 (w-left (window-pixel-left w
)))
1966 ;; Ignore ourselves.
1967 (and (window-parameter w
'no-other-window
)
1968 ;; Ignore W unless IGNORE is non-nil.
1972 ((and (<= w-top posn
)
1973 (< posn
(+ w-top
(window-pixel-height w
))))
1974 ;; W is to the left or right of WINDOW and covers POSN.
1975 (when (or (and (eq direction
'left
)
1976 (or (and (<= w-left first
) (> w-left best-edge
))
1978 (window-at-side-p window
'left
)
1979 (window-at-side-p w
'right
))))
1980 (and (eq direction
'right
)
1981 (or (and (>= w-left last
) (< w-left best-edge
))
1983 (window-at-side-p window
'right
)
1984 (window-at-side-p w
'left
)))))
1985 (setq best-edge w-left
)
1987 ((and (or (and (eq direction
'left
)
1988 (<= (+ w-left
(window-pixel-width w
)) first
))
1989 (and (eq direction
'right
) (<= last w-left
)))
1990 ;; W is to the left or right of WINDOW but does not
1992 (setq best-diff-2-new
1993 (window--in-direction-2 w posn hor
))
1994 (or (< best-diff-2-new best-diff-2
)
1995 (and (= best-diff-2-new best-diff-2
)
1996 (if (eq direction
'left
)
1997 (> w-left best-edge-2
)
1998 (< w-left best-edge-2
)))))
1999 (setq best-edge-2 w-left
)
2000 (setq best-diff-2 best-diff-2-new
)
2002 ((and (<= w-left posn
)
2003 (< posn
(+ w-left
(window-pixel-width w
))))
2004 ;; W is above or below WINDOW and covers POSN.
2005 (when (or (and (eq direction
'above
)
2006 (or (and (<= w-top first
) (> w-top best-edge
))
2008 (window-at-side-p window
'top
)
2009 (if (active-minibuffer-window)
2010 (minibuffer-window-active-p w
)
2011 (window-at-side-p w
'bottom
)))))
2012 (and (eq direction
'below
)
2013 (or (and (>= w-top first
) (< w-top best-edge
))
2015 (if (active-minibuffer-window)
2016 (minibuffer-window-active-p window
)
2017 (window-at-side-p window
'bottom
))
2018 (window-at-side-p w
'top
)))))
2019 (setq best-edge w-top
)
2021 ((and (or (and (eq direction
'above
)
2022 (<= (+ w-top
(window-pixel-height w
)) first
))
2023 (and (eq direction
'below
) (<= last w-top
)))
2024 ;; W is above or below WINDOW but does not cover POSN.
2025 (setq best-diff-2-new
2026 (window--in-direction-2 w posn hor
))
2027 (or (< best-diff-2-new best-diff-2
)
2028 (and (= best-diff-2-new best-diff-2
)
2029 (if (eq direction
'above
)
2030 (> w-top best-edge-2
)
2031 (< w-top best-edge-2
)))))
2032 (setq best-edge-2 w-top
)
2033 (setq best-diff-2 best-diff-2-new
)
2035 frame nil
(and mini t
))
2038 (defun get-window-with-predicate (predicate &optional minibuf all-frames default
)
2039 "Return a live window satisfying PREDICATE.
2040 More precisely, cycle through all windows calling the function
2041 PREDICATE on each one of them with the window as its sole
2042 argument. Return the first window for which PREDICATE returns
2043 non-nil. Windows are scanned starting with the window following
2044 the selected window. If no window satisfies PREDICATE, return
2047 MINIBUF t means include the minibuffer window even if the
2048 minibuffer is not active. MINIBUF nil or omitted means include
2049 the minibuffer window only if the minibuffer is active. Any
2050 other value means do not include the minibuffer window even if
2051 the minibuffer is active.
2053 ALL-FRAMES nil or omitted means consider all windows on the selected
2054 frame, plus the minibuffer window if specified by the MINIBUF
2055 argument. If the minibuffer counts, consider all windows on all
2056 frames that share that minibuffer too. The following non-nil
2057 values of ALL-FRAMES have special meanings:
2059 - t means consider all windows on all existing frames.
2061 - `visible' means consider all windows on all visible frames on
2062 the current terminal.
2064 - 0 (the number zero) means consider all windows on all visible
2065 and iconified frames on the current terminal.
2067 - A frame means consider all windows on that frame only.
2069 Anything else means consider all windows on the selected frame
2072 (dolist (window (window-list-1
2073 (next-window nil minibuf all-frames
)
2074 minibuf all-frames
))
2075 (when (funcall predicate window
)
2076 (throw 'found window
)))
2079 (defalias 'some-window
'get-window-with-predicate
)
2081 (defun get-lru-window (&optional all-frames dedicated not-selected
)
2082 "Return the least recently used window on frames specified by ALL-FRAMES.
2083 Return a full-width window if possible. A minibuffer window is
2084 never a candidate. A dedicated window is never a candidate
2085 unless DEDICATED is non-nil, so if all windows are dedicated, the
2086 value is nil. Avoid returning the selected window if possible.
2087 Optional argument NOT-SELECTED non-nil means never return the
2090 The following non-nil values of the optional argument ALL-FRAMES
2091 have special meanings:
2093 - t means consider all windows on all existing frames.
2095 - `visible' means consider all windows on all visible frames on
2096 the current terminal.
2098 - 0 (the number zero) means consider all windows on all visible
2099 and iconified frames on the current terminal.
2101 - A frame means consider all windows on that frame only.
2103 Any other value of ALL-FRAMES means consider all windows on the
2104 selected frame and no others."
2105 (let (best-window best-time second-best-window second-best-time time
)
2106 (dolist (window (window-list-1 nil
'nomini all-frames
))
2107 (when (and (or dedicated
(not (window-dedicated-p window
)))
2108 (or (not not-selected
) (not (eq window
(selected-window)))))
2109 (setq time
(window-use-time window
))
2110 (if (or (eq window
(selected-window))
2111 (not (window-full-width-p window
)))
2112 (when (or (not second-best-time
) (< time second-best-time
))
2113 (setq second-best-time time
)
2114 (setq second-best-window window
))
2115 (when (or (not best-time
) (< time best-time
))
2116 (setq best-time time
)
2117 (setq best-window window
)))))
2118 (or best-window second-best-window
)))
2120 (defun get-mru-window (&optional all-frames dedicated not-selected
)
2121 "Return the most recently used window on frames specified by ALL-FRAMES.
2122 A minibuffer window is never a candidate. A dedicated window is
2123 never a candidate unless DEDICATED is non-nil, so if all windows
2124 are dedicated, the value is nil. Optional argument NOT-SELECTED
2125 non-nil means never return the selected window.
2127 The following non-nil values of the optional argument ALL-FRAMES
2128 have special meanings:
2130 - t means consider all windows on all existing frames.
2132 - `visible' means consider all windows on all visible frames on
2133 the current terminal.
2135 - 0 (the number zero) means consider all windows on all visible
2136 and iconified frames on the current terminal.
2138 - A frame means consider all windows on that frame only.
2140 Any other value of ALL-FRAMES means consider all windows on the
2141 selected frame and no others."
2142 (let (best-window best-time time
)
2143 (dolist (window (window-list-1 nil
'nomini all-frames
))
2144 (setq time
(window-use-time window
))
2145 (when (and (or dedicated
(not (window-dedicated-p window
)))
2146 (or (not not-selected
) (not (eq window
(selected-window))))
2147 (or (not best-time
) (> time best-time
)))
2148 (setq best-time time
)
2149 (setq best-window window
)))
2152 (defun get-largest-window (&optional all-frames dedicated not-selected
)
2153 "Return the largest window on frames specified by ALL-FRAMES.
2154 A minibuffer window is never a candidate. A dedicated window is
2155 never a candidate unless DEDICATED is non-nil, so if all windows
2156 are dedicated, the value is nil. Optional argument NOT-SELECTED
2157 non-nil means never return the selected window.
2159 The following non-nil values of the optional argument ALL-FRAMES
2160 have special meanings:
2162 - t means consider all windows on all existing frames.
2164 - `visible' means consider all windows on all visible frames on
2165 the current terminal.
2167 - 0 (the number zero) means consider all windows on all visible
2168 and iconified frames on the current terminal.
2170 - A frame means consider all windows on that frame only.
2172 Any other value of ALL-FRAMES means consider all windows on the
2173 selected frame and no others."
2176 (dolist (window (window-list-1 nil
'nomini all-frames
))
2177 (when (and (or dedicated
(not (window-dedicated-p window
)))
2178 (or (not not-selected
) (not (eq window
(selected-window)))))
2179 (setq size
(* (window-pixel-height window
)
2180 (window-pixel-width window
)))
2181 (when (> size best-size
)
2182 (setq best-size size
)
2183 (setq best-window window
))))
2186 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames
)
2187 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2188 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2189 and defaults to the current buffer. Windows are scanned starting
2190 with the selected window.
2192 MINIBUF t means include the minibuffer window even if the
2193 minibuffer is not active. MINIBUF nil or omitted means include
2194 the minibuffer window only if the minibuffer is active. Any
2195 other value means do not include the minibuffer window even if
2196 the minibuffer is active.
2198 ALL-FRAMES nil or omitted means consider all windows on the
2199 selected frame, plus the minibuffer window if specified by the
2200 MINIBUF argument. If the minibuffer counts, consider all windows
2201 on all frames that share that minibuffer too. The following
2202 non-nil values of ALL-FRAMES have special meanings:
2204 - t means consider all windows on all existing frames.
2206 - `visible' means consider all windows on all visible frames on
2207 the current terminal.
2209 - 0 (the number zero) means consider all windows on all visible
2210 and iconified frames on the current terminal.
2212 - A frame means consider all windows on that frame only.
2214 Anything else means consider all windows on the selected frame
2216 (let ((buffer (window-normalize-buffer buffer-or-name
))
2218 (dolist (window (window-list-1 (selected-window) minibuf all-frames
))
2219 (when (eq (window-buffer window
) buffer
)
2220 (setq windows
(cons window windows
))))
2221 (nreverse windows
)))
2223 (defun minibuffer-window-active-p (window)
2224 "Return t if WINDOW is the currently active minibuffer window."
2225 (eq window
(active-minibuffer-window)))
2227 (defun count-windows (&optional minibuf
)
2228 "Return the number of live windows on the selected frame.
2229 The optional argument MINIBUF specifies whether the minibuffer
2230 window shall be counted. See `walk-windows' for the precise
2231 meaning of this argument."
2232 (length (window-list-1 nil minibuf
)))
2234 ;;; Resizing windows.
2235 (defun window--size-to-pixel (window size
&optional horizontal pixelwise round-maybe
)
2236 "For WINDOW convert SIZE lines to pixels.
2237 SIZE is supposed to specify a height of WINDOW in terms of text
2238 lines. The return value is the number of pixels specifying that
2241 WINDOW must be a valid window. Optional argument HORIZONTAL
2242 non-nil means convert SIZE columns to pixels.
2244 Optional argument PIXELWISE non-nil means SIZE already specifies
2245 pixels but may have to be adjusted to a multiple of the character
2246 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2247 means round to the nearest multiple of the character size of
2248 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2249 (setq window
(window-normalize-window window
))
2250 (let ((char-size (frame-char-size window horizontal
)))
2252 (if (and round-maybe
(not window-resize-pixelwise
))
2253 (* (round size char-size
) char-size
)
2255 (* size char-size
))))
2257 (defun window--pixel-to-total-1 (window horizontal char-size
)
2258 "Subroutine of `window--pixel-to-total'."
2259 (let ((child (window-child window
)))
2260 (if (window-combination-p window horizontal
)
2261 ;; In an iso-combination distribute sizes proportionally.
2262 (let ((remainder (window-new-total window
))
2263 size best-child rem best-rem
)
2264 ;; Initialize total sizes to each child's floor.
2266 (setq size
(max (/ (window-size child horizontal t
) char-size
) 1))
2267 (set-window-new-total child size
)
2268 (setq remainder
(- remainder size
))
2269 (setq child
(window-next-sibling child
)))
2270 ;; Distribute remainder.
2271 (while (> remainder
0)
2272 (setq child
(window-last-child window
))
2273 (setq best-child nil
)
2276 (when (and (<= (window-new-total child
)
2277 (/ (window-size child horizontal t
) char-size
))
2278 (> (setq rem
(%
(window-size child horizontal t
)
2281 (setq best-child child
)
2282 (setq best-rem rem
))
2283 (setq child
(window-prev-sibling child
)))
2284 ;; We MUST have a best-child here.
2285 (set-window-new-total best-child
1 t
)
2286 (setq remainder
(1- remainder
)))
2288 (setq child
(window-child window
))
2290 (window--pixel-to-total-1 child horizontal char-size
)
2291 (setq child
(window-next-sibling child
))))
2292 ;; In an ortho-combination assign new sizes directly.
2293 (let ((size (window-new-total window
)))
2295 (set-window-new-total child size
)
2296 (window--pixel-to-total-1 child horizontal char-size
)
2297 (setq child
(window-next-sibling child
)))))))
2299 (defun window--pixel-to-total (&optional frame horizontal
)
2300 "On FRAME assign new total window heights from pixel heights.
2301 FRAME must be a live frame and defaults to the selected frame.
2303 Optional argument HORIZONTAL non-nil means assign new total
2304 window widths from pixel widths."
2305 (setq frame
(window-normalize-frame frame
))
2306 (let* ((char-size (frame-char-size frame horizontal
))
2307 (root (frame-root-window frame
))
2308 (root-size (window-size root horizontal t
))
2309 ;; We have to care about the minibuffer window only if it
2310 ;; appears together with the root window on this frame.
2311 (mini (let ((mini (minibuffer-window frame
)))
2312 (and (eq (window-frame mini
) frame
)
2313 (not (eq mini root
)) mini
)))
2314 (mini-size (and mini
(window-size mini horizontal t
))))
2315 ;; We round the line/column sizes of windows here to the nearest
2316 ;; integer. In some cases this can make windows appear _larger_
2317 ;; than the containing frame (line/column-wise) because the latter's
2318 ;; sizes are not (yet) rounded. We might eventually fix that.
2319 (if (and mini
(not horizontal
))
2321 (set-window-new-total root
(max (/ root-size char-size
) 1))
2322 (set-window-new-total mini
(max (/ mini-size char-size
) 1))
2323 (setq lines
(- (round (+ root-size mini-size
) char-size
)
2324 (+ (window-new-total root
) (window-new-total mini
))))
2326 (if (>= (% root-size
(window-new-total root
))
2327 (% mini-size
(window-new-total mini
)))
2328 (set-window-new-total root
1 t
)
2329 (set-window-new-total mini
1 t
))
2330 (setq lines
(1- lines
))))
2331 (set-window-new-total root
(round root-size char-size
))
2333 ;; This is taken in the horizontal case only.
2334 (set-window-new-total mini
(round mini-size char-size
))))
2335 (unless (window-buffer root
)
2336 (window--pixel-to-total-1 root horizontal char-size
))
2337 ;; Apply the new sizes.
2338 (window-resize-apply-total frame horizontal
)))
2340 (defun window--resize-reset (&optional frame horizontal
)
2341 "Reset resize values for all windows on FRAME.
2342 FRAME defaults to the selected frame.
2344 This function stores the current value of `window-size' applied
2345 with argument HORIZONTAL in the new total size of all windows on
2346 FRAME. It also resets the new normal size of each of these
2348 (window--resize-reset-1
2349 (frame-root-window (window-normalize-frame frame
)) horizontal
))
2351 (defun window--resize-reset-1 (window horizontal
)
2352 "Internal function of `window--resize-reset'."
2353 ;; Register old size in the new total size.
2354 (set-window-new-pixel window
(window-size window horizontal t
))
2355 (set-window-new-total window
(window-size window horizontal
))
2356 ;; Reset new normal size.
2357 (set-window-new-normal window
)
2358 (when (window-child window
)
2359 (window--resize-reset-1 (window-child window
) horizontal
))
2360 (when (window-right window
)
2361 (window--resize-reset-1 (window-right window
) horizontal
)))
2363 ;; The following routine is used to manually resize the minibuffer
2364 ;; window and is currently used, for example, by ispell.el.
2365 (defun window--resize-mini-window (window delta
)
2366 "Resize minibuffer window WINDOW by DELTA pixels.
2367 If WINDOW cannot be resized by DELTA pixels make it as large (or
2368 as small) as possible, but don't signal an error."
2369 (when (window-minibuffer-p window
)
2370 (let* ((frame (window-frame window
))
2371 (root (frame-root-window frame
))
2372 (height (window-pixel-height window
))
2374 (- (window-pixel-height root
)
2375 (window-min-size root nil nil t
))))
2378 ((<= (+ height delta
) 0)
2379 (setq delta
(- (frame-char-height (window-frame window
)) height
)))
2380 ((> delta min-delta
)
2381 (setq delta min-delta
)))
2383 (unless (zerop delta
)
2385 (window--resize-reset frame
)
2386 ;; Ideally we should be able to resize just the last child of root
2387 ;; here. See the comment in `resize-root-window-vertically' for
2388 ;; why we do not do that.
2389 (window--resize-this-window root
(- delta
) nil nil t
)
2390 (set-window-new-pixel window
(+ height delta
))
2391 ;; The following routine catches the case where we want to resize
2392 ;; a minibuffer-only frame.
2393 (when (resize-mini-window-internal window
)
2394 (window--pixel-to-total frame
)
2395 (run-window-configuration-change-hook frame
))))))
2397 (defun window--resize-apply-p (frame &optional horizontal
)
2398 "Return t when a window on FRAME shall be resized vertically.
2399 Optional argument HORIZONTAL non-nil means return t when a window
2400 shall be resized horizontally."
2404 (unless (= (window-new-pixel window
)
2405 (window-size window horizontal t
))
2410 (defun window-resize (window delta
&optional horizontal ignore pixelwise
)
2411 "Resize WINDOW vertically by DELTA lines.
2412 WINDOW can be an arbitrary window and defaults to the selected
2413 one. An attempt to resize the root window of a frame will raise
2416 DELTA a positive number means WINDOW shall be enlarged by DELTA
2417 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2420 Optional argument HORIZONTAL non-nil means resize WINDOW
2421 horizontally by DELTA columns. In this case a positive DELTA
2422 means enlarge WINDOW by DELTA columns. DELTA negative means
2423 WINDOW shall be shrunk by -DELTA columns.
2425 Optional argument IGNORE non-nil means ignore restrictions
2426 imposed by fixed size windows, `window-min-height' or
2427 `window-min-width' settings. If IGNORE is a window, ignore
2428 restrictions for that window only. If IGNORE equals `safe',
2429 live windows may get as small as `window-safe-min-height' lines
2430 and `window-safe-min-width' columns. Any other non-nil value
2431 means ignore all of the above restrictions for all windows.
2433 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2436 This function resizes other windows proportionally and never
2437 deletes any windows. If you want to move only the low (right)
2438 edge of WINDOW consider using `adjust-window-trailing-edge'
2440 (setq window
(window-normalize-window window
))
2441 (let* ((frame (window-frame window
))
2442 (minibuffer-window (minibuffer-window frame
))
2444 (setq delta
(window--size-to-pixel
2445 window delta horizontal pixelwise t
))
2447 ((eq window
(frame-root-window frame
))
2448 (error "Cannot resize the root window of a frame"))
2449 ((window-minibuffer-p window
)
2451 (error "Cannot resize minibuffer window horizontally")
2452 (window--resize-mini-window window delta
)))
2453 ((and (not horizontal
)
2454 (window-full-height-p window
)
2455 (eq (window-frame minibuffer-window
) frame
)
2456 (or (not resize-mini-windows
)
2457 (eq minibuffer-window
(active-minibuffer-window))))
2458 ;; If WINDOW is full height and either `resize-mini-windows' is
2459 ;; nil or the minibuffer window is active, resize the minibuffer
2461 (window--resize-mini-window minibuffer-window
(- delta
)))
2462 ((window--resizable-p
2463 window delta horizontal ignore nil nil nil t
)
2464 (window--resize-reset frame horizontal
)
2465 (window--resize-this-window window delta horizontal ignore t
)
2466 (if (and (not window-combination-resize
)
2467 (window-combined-p window horizontal
)
2468 (setq sibling
(or (window-right window
) (window-left window
)))
2470 sibling
(- delta
) horizontal ignore t
))
2471 ;; If window-combination-resize is nil, WINDOW is part of an
2472 ;; iso-combination, and WINDOW's neighboring right or left
2473 ;; sibling can be resized as requested, resize that sibling.
2476 (window-size (window-parent window
) horizontal t
))))
2477 (window--resize-this-window sibling
(- delta
) horizontal nil t
)
2478 (set-window-new-normal
2479 window
(+ (window-normal-size window horizontal
)
2481 (set-window-new-normal
2482 sibling
(- (window-normal-size sibling horizontal
)
2484 ;; Otherwise, resize all other windows in the same combination.
2485 (window--resize-siblings window delta horizontal ignore
))
2486 (when (window--resize-apply-p frame horizontal
)
2487 (if (window-resize-apply frame horizontal
)
2489 (window--pixel-to-total frame horizontal
)
2490 (run-window-configuration-change-hook frame
))
2491 (error "Failed to apply resizing %s" window
))))
2493 (error "Cannot resize window %s" window
)))))
2495 (defun window-resize-no-error (window delta
&optional horizontal ignore pixelwise
)
2496 "Resize WINDOW vertically if it is resizable by DELTA lines.
2497 This function is like `window-resize' but does not signal an
2498 error when WINDOW cannot be resized. For the meaning of the
2499 optional arguments see the documentation of `window-resize'.
2501 Optional argument PIXELWISE non-nil means interpret DELTA as
2503 (when (window--resizable-p
2504 window delta horizontal ignore nil nil nil pixelwise
)
2505 (window-resize window delta horizontal ignore pixelwise
)))
2507 (defun window--resize-child-windows-skip-p (window)
2508 "Return non-nil if WINDOW shall be skipped by resizing routines."
2509 (memq (window-new-normal window
) '(ignore stuck skip
)))
2511 (defun window--resize-child-windows-normal (parent horizontal window this-delta
&optional trail other-delta
)
2512 "Recursively set new normal height of child windows of window PARENT.
2513 HORIZONTAL non-nil means set the new normal width of these
2514 windows. WINDOW specifies a child window of PARENT that has been
2515 resized by THIS-DELTA lines (columns).
2517 Optional argument TRAIL either `before' or `after' means set values
2518 only for windows before or after WINDOW. Optional argument
2519 OTHER-DELTA, a number, specifies that this many lines (columns)
2520 have been obtained from (or returned to) an ancestor window of
2521 PARENT in order to resize WINDOW."
2522 (let* ((delta-normal
2523 (if (and (= (- this-delta
)
2524 (window-size window horizontal t
))
2525 (zerop other-delta
))
2526 ;; When WINDOW gets deleted and we can return its entire
2527 ;; space to its siblings, use WINDOW's normal size as the
2529 (- (window-normal-size window horizontal
))
2530 ;; In any other case calculate the normal delta from the
2531 ;; relation of THIS-DELTA to the total size of PARENT.
2532 (/ (float this-delta
)
2533 (window-size parent horizontal t
))))
2534 (sub (window-child parent
))
2536 (skip (eq trail
'after
)))
2538 ;; Set parent-normal to the sum of the normal sizes of all child
2539 ;; windows of PARENT that shall be resized, excluding only WINDOW
2540 ;; and any windows specified by the optional TRAIL argument.
2544 (setq skip
(eq trail
'before
)))
2548 (+ parent-normal
(window-normal-size sub horizontal
)))))
2549 (setq sub
(window-right sub
)))
2551 ;; Set the new normal size of all child windows of PARENT from what
2552 ;; they should have contributed for recovering THIS-DELTA lines
2554 (setq sub
(window-child parent
))
2555 (setq skip
(eq trail
'after
))
2559 (setq skip
(eq trail
'before
)))
2562 (let ((old-normal (window-normal-size sub horizontal
)))
2563 (set-window-new-normal
2564 sub
(min 1.0 ; Don't get larger than 1.
2566 (* (/ old-normal parent-normal
)
2568 ;; Don't drop below 0.
2570 (setq sub
(window-right sub
)))
2572 (when (numberp other-delta
)
2573 ;; Set the new normal size of windows from what they should have
2574 ;; contributed for recovering OTHER-DELTA lines (columns).
2575 (setq delta-normal
(/ (float (window-size parent horizontal t
))
2576 (+ (window-size parent horizontal t
)
2578 (setq sub
(window-child parent
))
2579 (setq skip
(eq trail
'after
))
2583 (setq skip
(eq trail
'before
)))
2586 (set-window-new-normal
2587 sub
(min 1.0 ; Don't get larger than 1.
2588 (max (* (window-new-normal sub
) delta-normal
)
2589 ;; Don't drop below 0.
2591 (setq sub
(window-right sub
))))
2593 ;; Set the new normal size of WINDOW to what is left by the sum of
2594 ;; the normal sizes of its siblings.
2595 (set-window-new-normal
2598 (setq sub
(window-child parent
))
2602 ((not (numberp (window-new-normal sub
)))
2603 (setq sum
(+ sum
(window-normal-size sub horizontal
))))
2605 (setq sum
(+ sum
(window-new-normal sub
)))))
2606 (setq sub
(window-right sub
)))
2607 ;; Don't get larger than 1 or smaller than 0.
2608 (min 1.0 (max (- 1.0 sum
) 0.0))))))
2610 (defun window--resize-child-windows (parent delta
&optional horizontal window ignore trail edge char-size
)
2611 "Resize child windows of window PARENT vertically by DELTA pixels.
2612 PARENT must be a vertically combined internal window.
2614 Optional argument HORIZONTAL non-nil means resize child windows
2615 of PARENT horizontally by DELTA pixels. In this case PARENT must
2616 be a horizontally combined internal window.
2618 WINDOW, if specified, must denote a child window of PARENT that
2619 is resized by DELTA pixels.
2621 Optional argument IGNORE non-nil means ignore restrictions
2622 imposed by fixed size windows, `window-min-height' or
2623 `window-min-width' settings. If IGNORE equals `safe', live
2624 windows may get as small as `window-safe-min-height' lines and
2625 `window-safe-min-width' columns. If IGNORE is a window, ignore
2626 restrictions for that window only. Any other non-nil value means
2627 ignore all of the above restrictions for all windows.
2629 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2630 of windows that shall be resized. If TRAIL equals `before',
2631 resize only windows on the left or above EDGE. If TRAIL equals
2632 `after', resize only windows on the right or below EDGE. Also,
2633 preferably only resize windows adjacent to EDGE.
2635 If the optional argument CHAR-SIZE is a positive integer, it specifies
2636 the number of pixels by which windows are incrementally resized.
2637 If CHAR-SIZE is nil, this means to use the value of
2638 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2640 Return the symbol `normalized' if new normal sizes have been
2641 already set by this routine."
2642 (let* ((first (window-child parent
))
2643 (last (window-last-child parent
))
2644 (parent-total (+ (window-size parent horizontal t
)
2646 (char-size (or char-size
2647 (and window-resize-pixelwise
1)
2648 (frame-char-size window horizontal
)))
2649 sub best-window best-value best-delta
)
2651 (if (and edge
(memq trail
'(before after
))
2654 (while (and (window-right sub
)
2655 (or (and (eq trail
'before
)
2656 (not (window--resize-child-windows-skip-p
2657 (window-right sub
))))
2658 (and (eq trail
'after
)
2659 (window--resize-child-windows-skip-p sub
))))
2660 (setq sub
(window-right sub
)))
2663 (if (eq trail
'before
)
2664 (= (+ (window-pixel-left sub
) (window-pixel-width sub
))
2666 (= (window-pixel-left sub
) edge
))
2667 (if (eq trail
'before
)
2668 (= (+ (window-pixel-top sub
) (window-pixel-height sub
))
2670 (= (window-pixel-top sub
) edge
)))
2671 (window-sizable-p sub delta horizontal ignore t
))
2672 ;; Resize only windows adjacent to EDGE.
2674 (window--resize-this-window
2675 sub delta horizontal ignore t trail edge
)
2676 (if (and window
(eq (window-parent sub
) parent
))
2678 ;; Assign new normal sizes.
2679 (set-window-new-normal
2680 sub
(/ (float (window-new-pixel sub
)) parent-total
))
2681 (set-window-new-normal
2682 window
(- (window-normal-size window horizontal
)
2683 (- (window-new-normal sub
)
2684 (window-normal-size sub horizontal
)))))
2685 (window--resize-child-windows-normal
2686 parent horizontal sub
0 trail delta
))
2687 ;; Return 'normalized to notify `window--resize-siblings' that
2688 ;; normal sizes have been already set.
2690 ;; Resize all windows proportionally.
2694 ((or (window--resize-child-windows-skip-p sub
)
2695 ;; Ignore windows to skip and fixed-size child windows -
2696 ;; in the latter case make it a window to skip.
2698 (window-size-fixed-p sub horizontal
)
2699 (set-window-new-normal sub
'ignore
))))
2701 ;; When shrinking store the number of lines/cols we can get
2702 ;; from this window here together with the total/normal size
2704 (set-window-new-normal
2707 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
2708 (window-min-delta sub horizontal ignore trail t nil t
)
2709 (- (/ (float (window-size sub horizontal t
))
2711 (window-normal-size sub horizontal
)))))
2713 ;; When enlarging store the total/normal size factor only
2714 (set-window-new-normal
2716 (- (/ (float (window-size sub horizontal t
))
2718 (window-normal-size sub horizontal
)))))
2720 (setq sub
(window-left sub
)))
2724 ;; Shrink windows by delta.
2725 (setq best-window t
)
2726 (while (and best-window
(not (zerop delta
)))
2728 (setq best-window nil
)
2729 (setq best-value most-negative-fixnum
)
2731 (when (and (consp (window-new-normal sub
))
2732 (not (<= (car (window-new-normal sub
)) 0))
2733 (> (cdr (window-new-normal sub
)) best-value
))
2734 (setq best-window sub
)
2735 (setq best-value
(cdr (window-new-normal sub
))))
2737 (setq sub
(window-left sub
)))
2740 (setq best-delta
(min (car (window-new-normal best-window
))
2741 char-size
(- delta
)))
2742 (setq delta
(+ delta best-delta
))
2743 (set-window-new-pixel best-window
(- best-delta
) t
)
2744 (set-window-new-normal
2746 (if (= (car (window-new-normal best-window
)) best-delta
)
2747 'skip
; We can't shrink best-window any further.
2748 (cons (- (car (window-new-normal best-window
)) best-delta
)
2749 (- (/ (float (window-new-pixel best-window
))
2751 (window-normal-size best-window horizontal
))))))))
2753 ;; Enlarge windows by delta.
2754 (setq best-window t
)
2755 (while (and best-window
(not (zerop delta
)))
2757 (setq best-window nil
)
2758 (setq best-value most-positive-fixnum
)
2760 (when (and (numberp (window-new-normal sub
))
2761 (< (window-new-normal sub
) best-value
))
2762 (setq best-window sub
)
2763 (setq best-value
(window-new-normal sub
)))
2765 (setq sub
(window-left sub
)))
2768 (setq best-delta
(min delta char-size
))
2769 (setq delta
(- delta best-delta
))
2770 (set-window-new-pixel best-window best-delta t
)
2771 (set-window-new-normal
2773 (- (/ (float (window-new-pixel best-window
))
2775 (window-normal-size best-window horizontal
)))))))
2780 (when (or (consp (window-new-normal sub
))
2781 (numberp (window-new-normal sub
)))
2782 ;; Reset new normal size fields so `window-resize-apply'
2783 ;; won't use them to apply new sizes.
2784 (set-window-new-normal sub
))
2786 (unless (eq (window-new-normal sub
) 'ignore
)
2787 ;; Resize this window's child windows (back-engineering
2788 ;; delta from sub's old and new total sizes).
2789 (let ((delta (- (window-new-pixel sub
)
2790 (window-size sub horizontal t
))))
2791 (unless (and (zerop delta
) (not trail
))
2792 ;; For the TRAIL non-nil case we have to resize SUB
2793 ;; recursively even if it's size does not change.
2794 (window--resize-this-window
2795 sub delta horizontal ignore nil trail edge
))))
2796 (setq sub
(window-left sub
)))))))
2798 (defun window--resize-siblings (window delta
&optional horizontal ignore trail edge char-size
)
2799 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
2800 Optional argument HORIZONTAL non-nil means resize other windows
2801 when WINDOW is resized horizontally by DELTA pixels. WINDOW
2802 itself is not resized by this function.
2804 Optional argument IGNORE non-nil means ignore restrictions
2805 imposed by fixed size windows, `window-min-height' or
2806 `window-min-width' settings. If IGNORE equals `safe', live
2807 windows may get as small as `window-safe-min-height' lines and
2808 `window-safe-min-width' columns. If IGNORE is a window, ignore
2809 restrictions for that window only. Any other non-nil value means
2810 ignore all of the above restrictions for all windows.
2812 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2813 of windows that shall be resized. If TRAIL equals `before',
2814 resize only windows on the left or above EDGE. If TRAIL equals
2815 `after', resize only windows on the right or below EDGE. Also,
2816 preferably only resize windows adjacent to EDGE."
2817 (when (window-parent window
)
2818 (let* ((parent (window-parent window
))
2819 (sub (window-child parent
)))
2820 (if (window-combined-p sub horizontal
)
2821 ;; In an iso-combination try to extract DELTA from WINDOW's
2823 (let ((skip (eq trail
'after
))
2824 this-delta other-delta
)
2825 ;; Decide which windows shall be left alone.
2829 ;; Make sure WINDOW is left alone when
2830 ;; resizing its siblings.
2831 (set-window-new-normal sub
'ignore
)
2832 (setq skip
(eq trail
'before
)))
2834 ;; Make sure this sibling is left alone when
2835 ;; resizing its siblings.
2836 (set-window-new-normal sub
'ignore
))
2837 ((or (window--size-ignore-p sub ignore
)
2838 (not (window-size-fixed-p sub horizontal
)))
2839 ;; Set this-delta to t to signal that we found a sibling
2840 ;; of WINDOW whose size is not fixed.
2841 (setq this-delta t
)))
2843 (setq sub
(window-right sub
)))
2845 ;; Set this-delta to what we can get from WINDOW's siblings.
2846 (if (= (- delta
) (window-size window horizontal t
))
2847 ;; A deletion, presumably. We must handle this case
2848 ;; specially since `window--resizable' can't be used.
2850 ;; There's at least one resizable sibling we can
2851 ;; give WINDOW's size to.
2852 (setq this-delta delta
)
2853 ;; No resizable sibling exists.
2854 (setq this-delta
0))
2855 ;; Any other form of resizing.
2858 window delta horizontal ignore trail t nil t
)))
2860 ;; Set other-delta to what we still have to get from
2861 ;; ancestor windows of parent.
2862 (setq other-delta
(- delta this-delta
))
2863 (unless (zerop other-delta
)
2864 ;; Unless we got everything from WINDOW's siblings, PARENT
2865 ;; must be resized by other-delta lines or columns.
2866 (set-window-new-pixel parent other-delta
'add
))
2868 (if (zerop this-delta
)
2869 ;; We haven't got anything from WINDOW's siblings but we
2870 ;; must update the normal sizes to respect other-delta.
2871 (window--resize-child-windows-normal
2872 parent horizontal window this-delta trail other-delta
)
2873 ;; We did get something from WINDOW's siblings which means
2874 ;; we have to resize their child windows.
2875 (unless (eq (window--resize-child-windows
2876 parent
(- this-delta
) horizontal
2877 window ignore trail edge char-size
)
2878 ;; If `window--resize-child-windows' returns
2879 ;; 'normalized, this means it has set the
2880 ;; normal sizes already.
2882 ;; Set the normal sizes.
2883 (window--resize-child-windows-normal
2884 parent horizontal window this-delta trail other-delta
))
2885 ;; Set DELTA to what we still have to get from ancestor
2887 (setq delta other-delta
)))
2889 ;; In an ortho-combination all siblings of WINDOW must be
2890 ;; resized by DELTA.
2891 (set-window-new-pixel parent delta
'add
)
2893 (unless (eq sub window
)
2894 (window--resize-this-window
2895 sub delta horizontal ignore t
))
2896 (setq sub
(window-right sub
))))
2898 (unless (zerop delta
)
2900 (window--resize-siblings
2901 parent delta horizontal ignore trail edge char-size
)))))
2903 (defun window--resize-this-window (window delta
&optional horizontal ignore add trail edge char-size
)
2904 "Resize WINDOW vertically by DELTA pixels.
2905 Optional argument HORIZONTAL non-nil means resize WINDOW
2906 horizontally by DELTA pixels.
2908 Optional argument IGNORE non-nil means ignore restrictions
2909 imposed by fixed size windows, `window-min-height' or
2910 `window-min-width' settings. If IGNORE equals `safe', live
2911 windows may get as small as `window-safe-min-height' lines and
2912 `window-safe-min-width' columns. If IGNORE is a window, ignore
2913 restrictions for that window only. Any other non-nil value
2914 means ignore all of the above restrictions for all windows.
2916 Optional argument ADD non-nil means add DELTA to the new total
2919 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2920 of windows that shall be resized. If TRAIL equals `before',
2921 resize only windows on the left or above EDGE. If TRAIL equals
2922 `after', resize only windows on the right or below EDGE. Also,
2923 preferably only resize windows adjacent to EDGE.
2925 If the optional argument CHAR-SIZE is a positive integer, it specifies
2926 the number of pixels by which windows are incrementally resized.
2927 If CHAR-SIZE is nil, this means to use the value of
2928 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2930 This function recursively resizes WINDOW's child windows to fit the
2931 new size. Make sure that WINDOW is `window--resizable' before
2932 calling this function. Note that this function does not resize
2933 siblings of WINDOW or WINDOW's parent window. You have to
2934 eventually call `window-resize-apply' in order to make resizing
2935 actually take effect."
2937 ;; Add DELTA to the new total size of WINDOW.
2938 (set-window-new-pixel window delta t
))
2940 (let ((sub (window-child window
)))
2943 ((window-combined-p sub horizontal
)
2944 ;; In an iso-combination resize child windows according to their
2946 (window--resize-child-windows
2947 window delta horizontal nil ignore trail edge char-size
))
2948 ;; In an ortho-combination resize each child window by DELTA.
2951 (window--resize-this-window
2952 sub delta horizontal ignore t trail edge char-size
)
2953 (setq sub
(window-right sub
)))))))
2955 (defun window--resize-root-window (window delta horizontal ignore pixelwise
)
2956 "Resize root window WINDOW vertically by DELTA lines.
2957 HORIZONTAL non-nil means resize root window WINDOW horizontally
2960 IGNORE non-nil means ignore any restrictions imposed by fixed
2961 size windows, `window-min-height' or `window-min-width' settings.
2963 This function is only called by the frame resizing routines. It
2964 resizes windows proportionally and never deletes any windows."
2965 (when (and (windowp window
) (numberp delta
))
2969 (window--size-to-pixel window delta horizontal
))))
2970 (when (window-sizable-p window pixel-delta horizontal ignore t
)
2971 (window--resize-reset (window-frame window
) horizontal
)
2972 (window--resize-this-window
2973 window pixel-delta horizontal ignore t
)))))
2975 (defun window--resize-root-window-vertically (window delta pixelwise
)
2976 "Resize root window WINDOW vertically by DELTA lines.
2977 If DELTA is less than zero and we can't shrink WINDOW by DELTA
2978 lines, shrink it as much as possible. If DELTA is greater than
2979 zero, this function can resize fixed-size windows in order to
2980 recover the necessary lines. Return the number of lines that
2983 Third argument PIXELWISE non-nil means to interpret DELTA as
2984 pixels and return the number of pixels that were recovered.
2986 This function is called by the minibuffer window resizing
2988 (let* ((frame (window-frame window
))
2994 (* (frame-char-height frame
) delta
))
2998 ((zerop pixel-delta
))
3000 (setq pixel-delta
(window-sizable window pixel-delta nil nil pixelwise
))
3001 (window--resize-reset frame
)
3002 ;; When shrinking the root window, emulate an edge drag in order
3003 ;; to not resize other windows if we can avoid it (Bug#12419).
3004 (window--resize-this-window
3005 window pixel-delta nil ignore t
'before
3006 (+ (window-pixel-top window
) (window-pixel-height window
)))
3007 ;; Don't record new normal sizes to make sure that shrinking back
3008 ;; proportionally works as intended.
3010 (lambda (window) (set-window-new-normal window
'ignore
)) frame t
))
3012 (window--resize-reset frame
)
3013 (unless (window-sizable window pixel-delta nil nil pixelwise
)
3015 ;; When growing the root window, resize proportionally. This
3016 ;; should give windows back their original sizes (hopefully).
3017 (window--resize-this-window
3018 window pixel-delta nil ignore t
)))
3019 ;; Return the possibly adjusted DELTA.
3022 (/ pixel-delta
(frame-char-height frame
)))))
3024 (defun window--sanitize-window-sizes (frame horizontal
)
3025 "Assert that all windows on FRAME are large enough.
3026 If necessary and possible, make sure that every window on frame
3027 FRAME has its minimum height. Optional argument HORIZONTAL
3028 non-nil means to make sure that every window on frame FRAME has
3029 its minimum width. The minimumm height/width of a window is the
3030 respective value returned by `window-min-size' for that window.
3032 Return t if all windows were resized appropriately. Return nil
3033 if at least one window could not be resized as requested, which
3034 may happen when the FRAME is not large enough to accomodate it."
3038 (let ((delta (- (window-min-size window horizontal nil t
)
3039 (window-size window horizontal t
))))
3041 (if (window-resizable-p window delta horizontal nil t
)
3042 (window-resize window delta horizontal nil t
)
3043 (setq value nil
))))))
3046 (defun adjust-window-trailing-edge (window delta
&optional horizontal pixelwise
)
3047 "Move WINDOW's bottom edge by DELTA lines.
3048 Optional argument HORIZONTAL non-nil means move WINDOW's right
3049 edge by DELTA columns. WINDOW must be a valid window and
3050 defaults to the selected one.
3052 Optional argument PIXELWISE non-nil means interpret DELTA as
3055 If DELTA is greater than zero, move the edge downwards or to the
3056 right. If DELTA is less than zero, move the edge upwards or to
3057 the left. If the edge can't be moved by DELTA lines or columns,
3058 move it as far as possible in the desired direction."
3059 (setq window
(window-normalize-window window
))
3060 (let* ((frame (window-frame window
))
3061 (minibuffer-window (minibuffer-window frame
))
3063 left this-delta min-delta max-delta
)
3067 (setq delta
(* delta
(frame-char-size window horizontal
))))
3069 ;; Find the edge we want to move.
3070 (while (and (or (not (window-combined-p right horizontal
))
3071 (not (window-right right
)))
3072 (setq right
(window-parent right
))))
3074 ((and (not right
) (not horizontal
)
3075 ;; Resize the minibuffer window if it's on the same frame as
3076 ;; and immediately below WINDOW and it's either active or
3077 ;; `resize-mini-windows' is nil.
3078 (eq (window-frame minibuffer-window
) frame
)
3079 (= (nth 1 (window-pixel-edges minibuffer-window
))
3080 (nth 3 (window-pixel-edges window
)))
3081 (or (not resize-mini-windows
)
3082 (eq minibuffer-window
(active-minibuffer-window))))
3083 (window--resize-mini-window minibuffer-window
(- delta
)))
3084 ((or (not (setq left right
)) (not (setq right
(window-right right
))))
3086 (error "No window on the right of this one")
3087 (error "No window below this one")))
3089 ;; Set LEFT to the first resizable window on the left. This step is
3090 ;; needed to handle fixed-size windows.
3091 (while (and left
(window-size-fixed-p left horizontal
))
3093 (or (window-left left
)
3095 (while (and (setq left
(window-parent left
))
3096 (not (window-combined-p left horizontal
))))
3097 (window-left left
)))))
3100 (error "No resizable window on the left of this one")
3101 (error "No resizable window above this one")))
3103 ;; Set RIGHT to the first resizable window on the right. This step
3104 ;; is needed to handle fixed-size windows.
3105 (while (and right
(window-size-fixed-p right horizontal
))
3107 (or (window-right right
)
3109 (while (and (setq right
(window-parent right
))
3110 (not (window-combined-p right horizontal
))))
3111 (window-right right
)))))
3114 (error "No resizable window on the right of this one")
3115 (error "No resizable window below this one")))
3117 ;; LEFT and RIGHT (which might be both internal windows) are now the
3118 ;; two windows we want to resize.
3122 (window--max-delta-1
3123 left
0 horizontal nil
'after nil pixelwise
))
3125 (window--min-delta-1
3126 right
(- delta
) horizontal nil
'before nil pixelwise
))
3127 (when (or (< max-delta delta
) (> min-delta
(- delta
)))
3128 ;; We can't get the whole DELTA - move as far as possible.
3129 (setq delta
(min max-delta
(- min-delta
))))
3130 (unless (zerop delta
)
3132 (window--resize-reset frame horizontal
)
3133 ;; Try to enlarge LEFT first.
3134 (setq this-delta
(window--resizable
3135 left delta horizontal nil
'after nil nil pixelwise
))
3136 (unless (zerop this-delta
)
3137 (window--resize-this-window
3138 left this-delta horizontal nil t
'before
3140 (+ (window-pixel-left left
) (window-pixel-width left
))
3141 (+ (window-pixel-top left
) (window-pixel-height left
)))))
3142 ;; Shrink windows on right of LEFT.
3143 (window--resize-siblings
3144 left delta horizontal nil
'after
3146 (window-pixel-left right
)
3147 (window-pixel-top right
)))))
3150 (window--max-delta-1
3151 right
0 horizontal nil
'before nil pixelwise
))
3153 (window--min-delta-1
3154 left delta horizontal nil
'after nil pixelwise
))
3155 (when (or (< max-delta
(- delta
)) (> min-delta delta
))
3156 ;; We can't get the whole DELTA - move as far as possible.
3157 (setq delta
(max (- max-delta
) min-delta
)))
3158 (unless (zerop delta
)
3160 (window--resize-reset frame horizontal
)
3161 ;; Try to enlarge RIGHT.
3164 right
(- delta
) horizontal nil
'before nil nil pixelwise
))
3165 (unless (zerop this-delta
)
3166 (window--resize-this-window
3167 right this-delta horizontal nil t
'after
3169 (window-pixel-left right
)
3170 (window-pixel-top right
))))
3171 ;; Shrink windows on left of RIGHT.
3172 (window--resize-siblings
3173 right
(- delta
) horizontal nil
'before
3175 (+ (window-pixel-left left
) (window-pixel-width left
))
3176 (+ (window-pixel-top left
) (window-pixel-height left
)))))))
3177 (unless (zerop delta
)
3178 ;; Don't report an error in the standard case.
3179 (when (window--resize-apply-p frame horizontal
)
3180 (if (window-resize-apply frame horizontal
)
3182 (window--pixel-to-total frame horizontal
)
3183 (run-window-configuration-change-hook frame
))
3184 ;; But do report an error if applying the changes fails.
3185 (error "Failed adjusting window %s" window
))))))))
3187 (defun enlarge-window (delta &optional horizontal
)
3188 "Make the selected window DELTA lines taller.
3189 Interactively, if no argument is given, make the selected window
3190 one line taller. If optional argument HORIZONTAL is non-nil,
3191 make selected window wider by DELTA columns. If DELTA is
3192 negative, shrink selected window by -DELTA lines or columns."
3194 (let ((minibuffer-window (minibuffer-window)))
3197 ((window-size-fixed-p nil horizontal
)
3198 (error "Selected window has fixed size"))
3199 ((window-minibuffer-p)
3201 (error "Cannot resize minibuffer window horizontally")
3202 (window--resize-mini-window (selected-window) delta
)))
3203 ((and (not horizontal
)
3204 (window-full-height-p)
3205 (eq (window-frame minibuffer-window
) (selected-frame))
3206 (not resize-mini-windows
))
3207 ;; If the selected window is full height and `resize-mini-windows'
3208 ;; is nil, resize the minibuffer window.
3209 (window--resize-mini-window minibuffer-window
(- delta
)))
3210 ((window--resizable-p nil delta horizontal
)
3211 (window-resize nil delta horizontal
))
3215 (window-max-delta nil horizontal
)
3216 (- (window-min-delta nil horizontal
)))
3219 (defun shrink-window (delta &optional horizontal
)
3220 "Make the selected window DELTA lines smaller.
3221 Interactively, if no argument is given, make the selected window
3222 one line smaller. If optional argument HORIZONTAL is non-nil,
3223 make selected window narrower by DELTA columns. If DELTA is
3224 negative, enlarge selected window by -DELTA lines or columns.
3225 Also see the `window-min-height' variable."
3227 (let ((minibuffer-window (minibuffer-window)))
3230 ((window-size-fixed-p nil horizontal
)
3231 (error "Selected window has fixed size"))
3232 ((window-minibuffer-p)
3234 (error "Cannot resize minibuffer window horizontally")
3235 (window--resize-mini-window (selected-window) (- delta
))))
3236 ((and (not horizontal
)
3237 (window-full-height-p)
3238 (eq (window-frame minibuffer-window
) (selected-frame))
3239 (not resize-mini-windows
))
3240 ;; If the selected window is full height and `resize-mini-windows'
3241 ;; is nil, resize the minibuffer window.
3242 (window--resize-mini-window minibuffer-window delta
))
3243 ((window--resizable-p nil
(- delta
) horizontal
)
3244 (window-resize nil
(- delta
) horizontal
))
3248 (- (window-min-delta nil horizontal
))
3249 (window-max-delta nil horizontal
))
3252 (defun maximize-window (&optional window
)
3254 Make WINDOW as large as possible without deleting any windows.
3255 WINDOW must be a valid window and defaults to the selected one.
3257 If the option `window-resize-pixelwise' is non-nil maximize
3260 (setq window
(window-normalize-window window
))
3262 window
(window-max-delta window nil nil nil nil nil window-resize-pixelwise
)
3263 nil nil window-resize-pixelwise
)
3265 window
(window-max-delta window t nil nil nil nil window-resize-pixelwise
)
3266 t nil window-resize-pixelwise
))
3268 (defun minimize-window (&optional window
)
3270 Make WINDOW as small as possible without deleting any windows.
3271 WINDOW must be a valid window and defaults to the selected one.
3273 If the option `window-resize-pixelwise' is non-nil minimize
3276 (setq window
(window-normalize-window window
))
3279 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise
))
3280 nil nil window-resize-pixelwise
)
3283 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise
))
3284 t nil window-resize-pixelwise
))
3286 (defun frame-root-window-p (window)
3287 "Return non-nil if WINDOW is the root window of its frame."
3288 (eq window
(frame-root-window window
)))
3290 (defun window--subtree (window &optional next
)
3291 "Return window subtree rooted at WINDOW.
3292 Optional argument NEXT non-nil means include WINDOW's right
3293 siblings in the return value.
3295 See the documentation of `window-tree' for a description of the
3302 ((window-top-child window
)
3303 (cons t
(cons (window-edges window
)
3304 (window--subtree (window-top-child window
) t
))))
3305 ((window-left-child window
)
3306 (cons nil
(cons (window-edges window
)
3307 (window--subtree (window-left-child window
) t
))))
3310 (setq window
(when next
(window-next-sibling window
))))
3313 (defun window-tree (&optional frame
)
3314 "Return the window tree of frame FRAME.
3315 FRAME must be a live frame and defaults to the selected frame.
3316 The return value is a list of the form (ROOT MINI), where ROOT
3317 represents the window tree of the frame's root window, and MINI
3318 is the frame's minibuffer window.
3320 If the root window is not split, ROOT is the root window itself.
3321 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3322 for a horizontal split, and t for a vertical split. EDGES gives
3323 the combined size and position of the child windows in the split,
3324 and the rest of the elements are the child windows in the split.
3325 Each of the child windows may again be a window or a list
3326 representing a window split, and so on. EDGES is a list (LEFT
3327 TOP RIGHT BOTTOM) as returned by `window-edges'."
3328 (setq frame
(window-normalize-frame frame
))
3329 (window--subtree (frame-root-window frame
) t
))
3331 (defun other-window (count &optional all-frames
)
3332 "Select another window in cyclic ordering of windows.
3333 COUNT specifies the number of windows to skip, starting with the
3334 selected window, before making the selection. If COUNT is
3335 positive, skip COUNT windows forwards. If COUNT is negative,
3336 skip -COUNT windows backwards. COUNT zero means do not skip any
3337 window, so select the selected window. In an interactive call,
3338 COUNT is the numeric prefix argument. Return nil.
3340 If the `other-window' parameter of the selected window is a
3341 function and `ignore-window-parameters' is nil, call that
3342 function with the arguments COUNT and ALL-FRAMES.
3344 This function does not select a window whose `no-other-window'
3345 window parameter is non-nil.
3347 This function uses `next-window' for finding the window to
3348 select. The argument ALL-FRAMES has the same meaning as in
3349 `next-window', but the MINIBUF argument of `next-window' is
3350 always effectively nil."
3352 (let* ((window (selected-window))
3353 (function (and (not ignore-window-parameters
)
3354 (window-parameter window
'other-window
)))
3355 old-window old-count
)
3356 (if (functionp function
)
3357 (funcall function count all-frames
)
3358 ;; `next-window' and `previous-window' may return a window we are
3359 ;; not allowed to select. Hence we need an exit strategy in case
3360 ;; all windows are non-selectable.
3363 (setq window
(next-window window nil all-frames
))
3365 ((eq window old-window
)
3366 (when (= count old-count
)
3367 ;; Keep out of infinite loops. When COUNT has not changed
3368 ;; since we last looked at `window' we're probably in one.
3370 ((window-parameter window
'no-other-window
)
3372 ;; The first non-selectable window `next-window' got us:
3373 ;; Remember it and the current value of COUNT.
3374 (setq old-window window
)
3375 (setq old-count count
)))
3377 (setq count
(1- count
)))))
3379 (setq window
(previous-window window nil all-frames
))
3381 ((eq window old-window
)
3382 (when (= count old-count
)
3383 ;; Keep out of infinite loops. When COUNT has not changed
3384 ;; since we last looked at `window' we're probably in one.
3386 ((window-parameter window
'no-other-window
)
3388 ;; The first non-selectable window `previous-window' got
3389 ;; us: Remember it and the current value of COUNT.
3390 (setq old-window window
)
3391 (setq old-count count
)))
3393 (setq count
(1+ count
)))))
3395 (select-window window
)
3396 ;; Always return nil.
3399 ;; This should probably return non-nil when the selected window is part
3400 ;; of an atomic window whose root is the frame's root window.
3401 (defun one-window-p (&optional nomini all-frames
)
3402 "Return non-nil if the selected window is the only window.
3403 Optional arg NOMINI non-nil means don't count the minibuffer
3404 even if it is active. Otherwise, the minibuffer is counted
3407 Optional argument ALL-FRAMES specifies the set of frames to
3408 consider, see also `next-window'. ALL-FRAMES nil or omitted
3409 means consider windows on the selected frame only, plus the
3410 minibuffer window if specified by the NOMINI argument. If the
3411 minibuffer counts, consider all windows on all frames that share
3412 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3413 with a special meaning are:
3415 - t means consider all windows on all existing frames.
3417 - `visible' means consider all windows on all visible frames on
3418 the current terminal.
3420 - 0 (the number zero) means consider all windows on all visible
3421 and iconified frames on the current terminal.
3423 - A frame means consider all windows on that frame only.
3425 Anything else means consider all windows on the selected frame
3427 (let ((base-window (selected-window)))
3428 (if (and nomini
(eq base-window
(minibuffer-window)))
3429 (setq base-window
(next-window base-window
)))
3431 (next-window base-window
(if nomini
'arg
) all-frames
))))
3433 ;;; Deleting windows.
3434 (defun window-deletable-p (&optional window
)
3435 "Return t if WINDOW can be safely deleted from its frame.
3436 WINDOW must be a valid window and defaults to the selected one.
3437 Return 'frame if deleting WINDOW should also delete its frame."
3438 (setq window
(window-normalize-window window
))
3440 (unless (or ignore-window-parameters
3441 (eq (window-parameter window
'delete-window
) t
))
3442 ;; Handle atomicity.
3443 (when (window-parameter window
'window-atom
)
3444 (setq window
(window-atom-root window
))))
3446 (let ((frame (window-frame window
)))
3448 ((frame-root-window-p window
)
3449 ;; WINDOW's frame can be deleted only if there are other frames
3450 ;; on the same terminal, and it does not contain the active
3452 (unless (or (eq frame
(next-frame frame
0))
3453 ;; We can delete our frame only if no other frame
3454 ;; currently uses our minibuffer window.
3456 (dolist (other (frame-list))
3457 (when (and (not (eq other frame
))
3458 (eq (window-frame (minibuffer-window other
))
3461 (let ((minibuf (active-minibuffer-window)))
3462 (and minibuf
(eq frame
(window-frame minibuf
)))))
3464 ((or ignore-window-parameters
3465 (not (eq window
(window--major-non-side-window frame
))))
3466 ;; WINDOW can be deleted unless it is the major non-side window of
3470 (defun window--in-subtree-p (window root
)
3471 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
3472 (or (eq window root
)
3473 (let ((parent (window-parent window
)))
3476 (if (eq parent root
)
3478 (setq parent
(window-parent parent
))))))))
3480 (defun delete-window (&optional window
)
3482 WINDOW must be a valid window and defaults to the selected one.
3485 If the variable `ignore-window-parameters' is non-nil or the
3486 `delete-window' parameter of WINDOW equals t, do not process any
3487 parameters of WINDOW. Otherwise, if the `delete-window'
3488 parameter of WINDOW specifies a function, call that function with
3489 WINDOW as its sole argument and return the value returned by that
3492 Otherwise, if WINDOW is part of an atomic window, call
3493 `delete-window' with the root of the atomic window as its
3494 argument. Signal an error if WINDOW is either the only window on
3495 its frame, the last non-side window, or part of an atomic window
3496 that is its frame's root window."
3498 (setq window
(window-normalize-window window
))
3499 (let* ((frame (window-frame window
))
3500 (function (window-parameter window
'delete-window
))
3501 (parent (window-parent window
))
3503 (window--check frame
)
3505 ;; Handle window parameters.
3507 ;; Ignore window parameters if `ignore-window-parameters' tells
3508 ;; us so or `delete-window' equals t.
3509 ((or ignore-window-parameters
(eq function t
)))
3510 ((functionp function
)
3511 ;; The `delete-window' parameter specifies the function to call.
3512 ;; If that function is `ignore' nothing is done. It's up to the
3513 ;; function called here to avoid infinite recursion.
3514 (throw 'done
(funcall function window
)))
3515 ((and (window-parameter window
'window-atom
)
3516 (setq atom-root
(window-atom-root window
))
3517 (not (eq atom-root window
)))
3518 (if (eq atom-root
(frame-root-window frame
))
3519 (error "Root of atomic window is root window of its frame")
3520 (throw 'done
(delete-window atom-root
))))
3522 (error "Attempt to delete minibuffer or sole ordinary window"))
3523 ((eq window
(window--major-non-side-window frame
))
3524 (error "Attempt to delete last non-side window")))
3526 (let* ((horizontal (window-left-child parent
))
3527 (size (window-size window horizontal t
))
3529 (window--in-subtree-p (frame-selected-window frame
) window
))
3530 ;; Emacs 23 preferably gives WINDOW's space to its left
3532 (sibling (or (window-left window
) (window-right window
))))
3533 (window--resize-reset frame horizontal
)
3535 ((and (not window-combination-resize
)
3536 sibling
(window-sizable-p sibling size horizontal nil t
))
3537 ;; Resize WINDOW's sibling.
3538 (window--resize-this-window sibling size horizontal nil t
)
3539 (set-window-new-normal
3540 sibling
(+ (window-normal-size sibling horizontal
)
3541 (window-normal-size window horizontal
))))
3542 ((window--resizable-p window
(- size
) horizontal nil nil nil t t
)
3543 ;; Can do without resizing fixed-size windows.
3544 (window--resize-siblings window
(- size
) horizontal
))
3546 ;; Can't do without resizing fixed-size windows.
3547 (window--resize-siblings window
(- size
) horizontal t
)))
3548 ;; Actually delete WINDOW.
3549 (delete-window-internal window
)
3550 (window--pixel-to-total frame horizontal
)
3551 (when (and frame-selected
3553 (frame-selected-window frame
) 'no-other-window
))
3554 ;; `delete-window-internal' has selected a window that should
3555 ;; not be selected, fix this here.
3556 (other-window -
1 frame
))
3557 (run-window-configuration-change-hook frame
)
3558 (window--check frame
)
3559 ;; Always return nil.
3562 (defun delete-other-windows (&optional window
)
3563 "Make WINDOW fill its frame.
3564 WINDOW must be a valid window and defaults to the selected one.
3567 If the variable `ignore-window-parameters' is non-nil or the
3568 `delete-other-windows' parameter of WINDOW equals t, do not
3569 process any parameters of WINDOW. Otherwise, if the
3570 `delete-other-windows' parameter of WINDOW specifies a function,
3571 call that function with WINDOW as its sole argument and return
3572 the value returned by that function.
3574 Otherwise, if WINDOW is part of an atomic window, call this
3575 function with the root of the atomic window as its argument. If
3576 WINDOW is a non-side window, make WINDOW the only non-side window
3577 on the frame. Side windows are not deleted. If WINDOW is a side
3578 window signal an error."
3580 (setq window
(window-normalize-window window
))
3581 (let* ((frame (window-frame window
))
3582 (function (window-parameter window
'delete-other-windows
))
3583 (window-side (window-parameter window
'window-side
))
3584 atom-root side-main
)
3585 (window--check frame
)
3588 ;; Ignore window parameters if `ignore-window-parameters' is t or
3589 ;; `delete-other-windows' is t.
3590 ((or ignore-window-parameters
(eq function t
)))
3591 ((functionp function
)
3592 ;; The `delete-other-windows' parameter specifies the function
3593 ;; to call. If the function is `ignore' no windows are deleted.
3594 ;; It's up to the function called to avoid infinite recursion.
3595 (throw 'done
(funcall function window
)))
3596 ((and (window-parameter window
'window-atom
)
3597 (setq atom-root
(window-atom-root window
))
3598 (not (eq atom-root window
)))
3599 (if (eq atom-root
(frame-root-window frame
))
3600 (error "Root of atomic window is root window of its frame")
3601 (throw 'done
(delete-other-windows atom-root
))))
3602 ((memq window-side window-sides
)
3603 (error "Cannot make side window the only window"))
3604 ((and (window-minibuffer-p window
)
3605 (not (eq window
(frame-root-window window
))))
3606 (error "Can't expand minibuffer to full frame")))
3608 ;; If WINDOW is the major non-side window, do nothing.
3609 (if (window-with-parameter 'window-side
)
3610 (setq side-main
(window--major-non-side-window frame
))
3611 (setq side-main
(frame-root-window frame
)))
3612 (unless (eq window side-main
)
3613 (delete-other-windows-internal window side-main
)
3614 (run-window-configuration-change-hook frame
)
3615 (window--check frame
))
3616 ;; Always return nil.
3619 (defun delete-other-windows-vertically (&optional window
)
3620 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
3621 This may be a useful alternative binding for \\[delete-other-windows]
3622 if you often split windows horizontally."
3624 (let* ((window (or window
(selected-window)))
3625 (edges (window-edges window
))
3627 (while (not (eq (setq w
(next-window w
1)) window
))
3628 (let ((e (window-edges w
)))
3629 (when (and (= (car e
) (car edges
))
3630 (= (nth 2 e
) (nth 2 edges
)))
3632 (mapc 'delete-window delenda
)))
3634 ;;; Windows and buffers.
3636 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
3637 ;; for (1) determining which buffer to show in the window when its
3638 ;; buffer shall be buried or killed and (2) which buffer to show for
3639 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3641 ;; `prev-buffers' consists of <buffer, window-start, window-point>
3642 ;; triples. The entries on this list are ordered by the time their
3643 ;; buffer has been removed from the window, the most recently removed
3644 ;; buffer's entry being first. The window-start and window-point
3645 ;; components are `window-start' and `window-point' at the time the
3646 ;; buffer was removed from the window which implies that the entry must
3647 ;; be added when `set-window-buffer' removes the buffer from the window.
3649 ;; `next-buffers' is the list of buffers that have been replaced
3650 ;; recently by `switch-to-prev-buffer'. These buffers are the least
3651 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
3652 ;; candidates of `switch-to-next-buffer' to switch to. This list is
3653 ;; reset to nil by any action changing the window's buffer with the
3654 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
3655 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
3656 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
3658 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
3659 ;; if such a buffer was killed while the window was hidden within a
3660 ;; window configuration. Such killed buffers get removed whenever
3661 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
3663 ;; The following function is called by `set-window-buffer' _before_ it
3664 ;; replaces the buffer of the argument window with the new buffer.
3665 (defun record-window-buffer (&optional window
)
3666 "Record WINDOW's buffer.
3667 WINDOW must be a live window and defaults to the selected one."
3668 (let* ((window (window-normalize-window window t
))
3669 (buffer (window-buffer window
))
3670 (entry (assq buffer
(window-prev-buffers window
))))
3671 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
3672 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3673 (set-window-next-buffers window nil
)
3676 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
3677 (set-window-prev-buffers
3678 window
(assq-delete-all buffer
(window-prev-buffers window
))))
3680 ;; Don't record insignificant buffers.
3681 (unless (eq (aref (buffer-name buffer
) 0) ?\s
)
3682 ;; Add an entry for buffer to WINDOW's previous buffers.
3683 (with-current-buffer buffer
3684 (let ((start (window-start window
))
3685 (point (window-point window
)))
3689 ;; We have an entry, update marker positions.
3690 (list (set-marker (nth 1 entry
) start
)
3691 (set-marker (nth 2 entry
) point
))
3692 ;; Make new markers.
3693 (list (copy-marker start
)
3695 ;; Preserve window-point-insertion-type
3697 point window-point-insertion-type
)))))
3698 (set-window-prev-buffers
3699 window
(cons entry
(window-prev-buffers window
)))))
3701 (run-hooks 'buffer-list-update-hook
))))
3703 (defun unrecord-window-buffer (&optional window buffer
)
3704 "Unrecord BUFFER in WINDOW.
3705 WINDOW must be a live window and defaults to the selected one.
3706 BUFFER must be a live buffer and defaults to the buffer of
3708 (let* ((window (window-normalize-window window t
))
3709 (buffer (or buffer
(window-buffer window
))))
3710 (set-window-prev-buffers
3711 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3712 (set-window-next-buffers
3713 window
(delq buffer
(window-next-buffers window
)))))
3715 (defun set-window-buffer-start-and-point (window buffer
&optional start point
)
3716 "Set WINDOW's buffer to BUFFER.
3717 WINDOW must be a live window and defaults to the selected one.
3718 Optional argument START non-nil means set WINDOW's start position
3719 to START. Optional argument POINT non-nil means set WINDOW's
3720 point to POINT. If WINDOW is selected this also sets BUFFER's
3721 `point' to POINT. If WINDOW is selected and the buffer it showed
3722 before was current this also makes BUFFER the current buffer."
3723 (setq window
(window-normalize-window window t
))
3724 (let ((selected (eq window
(selected-window)))
3725 (current (eq (window-buffer window
) (current-buffer))))
3726 (set-window-buffer window buffer
)
3727 (when (and selected current
)
3728 (set-buffer buffer
))
3730 ;; Don't force window-start here (even if POINT is nil).
3731 (set-window-start window start t
))
3733 (set-window-point window point
))))
3735 (defcustom switch-to-visible-buffer t
3736 "If non-nil, allow switching to an already visible buffer.
3737 If this variable is non-nil, `switch-to-prev-buffer' and
3738 `switch-to-next-buffer' may switch to an already visible buffer
3739 provided the buffer was shown before in the window specified as
3740 argument to those functions. If this variable is nil,
3741 `switch-to-prev-buffer' and `switch-to-next-buffer' always try to
3742 avoid switching to a buffer that is already visible in another
3743 window on the same frame."
3748 (defun switch-to-prev-buffer (&optional window bury-or-kill
)
3749 "In WINDOW switch to previous buffer.
3750 WINDOW must be a live window and defaults to the selected one.
3751 Return the buffer switched to, nil if no suitable buffer could be
3754 Optional argument BURY-OR-KILL non-nil means the buffer currently
3755 shown in WINDOW is about to be buried or killed and consequently
3756 shall not be switched to in future invocations of this command.
3758 As a special case, if BURY-OR-KILL equals `append', this means to
3759 move the buffer to the end of WINDOW's previous buffers list so a
3760 future invocation of `switch-to-prev-buffer' less likely switches
3763 (let* ((window (window-normalize-window window t
))
3764 (frame (window-frame window
))
3765 (old-buffer (window-buffer window
))
3766 ;; Save this since it's destroyed by `set-window-buffer'.
3767 (next-buffers (window-next-buffers window
))
3768 (pred (frame-parameter frame
'buffer-predicate
))
3769 entry new-buffer killed-buffers visible
)
3770 (when (window-minibuffer-p window
)
3771 ;; Don't switch in minibuffer window.
3772 (unless (setq window
(minibuffer-selected-window))
3773 (error "Window %s is a minibuffer window" window
)))
3775 (when (window-dedicated-p window
)
3776 ;; Don't switch in dedicated window.
3777 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3780 ;; Scan WINDOW's previous buffers first, skipping entries of next
3782 (dolist (entry (window-prev-buffers window
))
3783 (when (and (setq new-buffer
(car entry
))
3784 (or (buffer-live-p new-buffer
)
3785 (not (setq killed-buffers
3786 (cons new-buffer killed-buffers
))))
3787 (not (eq new-buffer old-buffer
))
3788 (or (null pred
) (funcall pred new-buffer
))
3789 ;; When BURY-OR-KILL is nil, avoid switching to a
3790 ;; buffer in WINDOW's next buffers list.
3791 (or bury-or-kill
(not (memq new-buffer next-buffers
))))
3792 (if (and (not switch-to-visible-buffer
)
3793 (get-buffer-window new-buffer frame
))
3794 ;; Try to avoid showing a buffer visible in some other
3796 (setq visible new-buffer
)
3797 (set-window-buffer-start-and-point
3798 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3800 ;; Scan reverted buffer list of WINDOW's frame next, skipping
3801 ;; entries of next buffers. Note that when we bury or kill a
3802 ;; buffer we don't reverse the global buffer list to avoid showing
3803 ;; a buried buffer instead. Otherwise, we must reverse the global
3804 ;; buffer list in order to make sure that switching to the
3805 ;; previous/next buffer traverse it in opposite directions.
3806 (dolist (buffer (if bury-or-kill
3808 (nreverse (buffer-list frame
))))
3809 (when (and (buffer-live-p buffer
)
3810 (not (eq buffer old-buffer
))
3811 (or (null pred
) (funcall pred buffer
))
3812 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3813 (or bury-or-kill
(not (memq buffer next-buffers
))))
3814 (if (get-buffer-window buffer frame
)
3815 ;; Try to avoid showing a buffer visible in some other window.
3817 (setq visible buffer
))
3818 (setq new-buffer buffer
)
3819 (set-window-buffer-start-and-point window new-buffer
)
3821 (unless bury-or-kill
3822 ;; Scan reverted next buffers last (must not use nreverse
3824 (dolist (buffer (reverse next-buffers
))
3825 ;; Actually, buffer _must_ be live here since otherwise it
3826 ;; would have been caught in the scan of previous buffers.
3827 (when (and (or (buffer-live-p buffer
)
3828 (not (setq killed-buffers
3829 (cons buffer killed-buffers
))))
3830 (not (eq buffer old-buffer
))
3831 (or (null pred
) (funcall pred buffer
))
3832 (setq entry
(assq buffer
(window-prev-buffers window
))))
3833 (setq new-buffer buffer
)
3834 (set-window-buffer-start-and-point
3835 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3838 ;; Show a buffer visible in another window.
3840 (setq new-buffer visible
)
3841 (set-window-buffer-start-and-point window new-buffer
)))
3844 (let ((entry (and (eq bury-or-kill
'append
)
3845 (assq old-buffer
(window-prev-buffers window
)))))
3846 ;; Remove `old-buffer' from WINDOW's previous and (restored list
3847 ;; of) next buffers.
3848 (set-window-prev-buffers
3849 window
(assq-delete-all old-buffer
(window-prev-buffers window
)))
3850 (set-window-next-buffers window
(delq old-buffer next-buffers
))
3852 ;; Append old-buffer's entry to list of WINDOW's previous
3853 ;; buffers so it's less likely to get switched to soon but
3854 ;; `display-buffer-in-previous-window' can nevertheless find
3856 (set-window-prev-buffers
3857 window
(append (window-prev-buffers window
) (list entry
)))))
3858 ;; Move `old-buffer' to head of WINDOW's restored list of next
3860 (set-window-next-buffers
3861 window
(cons old-buffer
(delq old-buffer next-buffers
))))
3863 ;; Remove killed buffers from WINDOW's previous and next buffers.
3864 (when killed-buffers
3865 (dolist (buffer killed-buffers
)
3866 (set-window-prev-buffers
3867 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3868 (set-window-next-buffers
3869 window
(delq buffer
(window-next-buffers window
)))))
3871 ;; Return new-buffer.
3874 (defun switch-to-next-buffer (&optional window
)
3875 "In WINDOW switch to next buffer.
3876 WINDOW must be a live window and defaults to the selected one.
3877 Return the buffer switched to, nil if no suitable buffer could be
3880 (let* ((window (window-normalize-window window t
))
3881 (frame (window-frame window
))
3882 (old-buffer (window-buffer window
))
3883 (next-buffers (window-next-buffers window
))
3884 (pred (frame-parameter frame
'buffer-predicate
))
3885 new-buffer entry killed-buffers visible
)
3886 (when (window-minibuffer-p window
)
3887 ;; Don't switch in minibuffer window.
3888 (unless (setq window
(minibuffer-selected-window))
3889 (error "Window %s is a minibuffer window" window
)))
3891 (when (window-dedicated-p window
)
3892 ;; Don't switch in dedicated window.
3893 (error "Window %s is dedicated to buffer %s" window old-buffer
))
3896 ;; Scan WINDOW's next buffers first.
3897 (dolist (buffer next-buffers
)
3898 (when (and (or (buffer-live-p buffer
)
3899 (not (setq killed-buffers
3900 (cons buffer killed-buffers
))))
3901 (not (eq buffer old-buffer
))
3902 (or (null pred
) (funcall pred buffer
))
3903 (setq entry
(assq buffer
(window-prev-buffers window
))))
3904 (setq new-buffer buffer
)
3905 (set-window-buffer-start-and-point
3906 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3908 ;; Scan the buffer list of WINDOW's frame next, skipping previous
3910 (dolist (buffer (buffer-list frame
))
3911 (when (and (buffer-live-p buffer
)
3912 (not (eq buffer old-buffer
))
3913 (or (null pred
) (funcall pred buffer
))
3914 (not (eq (aref (buffer-name buffer
) 0) ?\s
))
3915 (not (assq buffer
(window-prev-buffers window
))))
3916 (if (get-buffer-window buffer frame
)
3917 ;; Try to avoid showing a buffer visible in some other window.
3918 (setq visible buffer
)
3919 (setq new-buffer buffer
)
3920 (set-window-buffer-start-and-point window new-buffer
)
3922 ;; Scan WINDOW's reverted previous buffers last (must not use
3924 (dolist (entry (reverse (window-prev-buffers window
)))
3925 (when (and (setq new-buffer
(car entry
))
3926 (or (buffer-live-p new-buffer
)
3927 (not (setq killed-buffers
3928 (cons new-buffer killed-buffers
))))
3929 (not (eq new-buffer old-buffer
))
3930 (or (null pred
) (funcall pred new-buffer
)))
3931 (if (and (not switch-to-visible-buffer
)
3932 (get-buffer-window new-buffer frame
))
3933 ;; Try to avoid showing a buffer visible in some other window.
3935 (setq visible new-buffer
))
3936 (set-window-buffer-start-and-point
3937 window new-buffer
(nth 1 entry
) (nth 2 entry
))
3940 ;; Show a buffer visible in another window.
3942 (setq new-buffer visible
)
3943 (set-window-buffer-start-and-point window new-buffer
)))
3945 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
3946 (set-window-next-buffers window
(delq new-buffer next-buffers
))
3948 ;; Remove killed buffers from WINDOW's previous and next buffers.
3949 (when killed-buffers
3950 (dolist (buffer killed-buffers
)
3951 (set-window-prev-buffers
3952 window
(assq-delete-all buffer
(window-prev-buffers window
)))
3953 (set-window-next-buffers
3954 window
(delq buffer
(window-next-buffers window
)))))
3956 ;; Return new-buffer.
3959 (defun get-next-valid-buffer (list &optional buffer visible-ok frame
)
3960 "Search LIST for a valid buffer to display in FRAME.
3961 Return nil when all buffers in LIST are undesirable for display,
3962 otherwise return the first suitable buffer in LIST.
3964 Buffers not visible in windows are preferred to visible buffers,
3965 unless VISIBLE-OK is non-nil.
3966 If the optional argument FRAME is nil, it defaults to the selected frame.
3967 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
3968 ;; This logic is more or less copied from other-buffer.
3969 (setq frame
(or frame
(selected-frame)))
3970 (let ((pred (frame-parameter frame
'buffer-predicate
))
3972 (while (and (not found
) list
)
3973 (setq buf
(car list
))
3974 (if (and (not (eq buffer buf
))
3976 (or (null pred
) (funcall pred buf
))
3977 (not (eq (aref (buffer-name buf
) 0) ?\s
))
3978 (or visible-ok
(null (get-buffer-window buf
'visible
))))
3980 (setq list
(cdr list
))))
3983 (defun last-buffer (&optional buffer visible-ok frame
)
3984 "Return the last buffer in FRAME's buffer list.
3985 If BUFFER is the last buffer, return the preceding buffer
3986 instead. Buffers not visible in windows are preferred to visible
3987 buffers, unless optional argument VISIBLE-OK is non-nil.
3988 Optional third argument FRAME nil or omitted means use the
3989 selected frame's buffer list. If no such buffer exists, return
3990 the buffer `*scratch*', creating it if necessary."
3991 (setq frame
(or frame
(selected-frame)))
3992 (or (get-next-valid-buffer (nreverse (buffer-list frame
))
3993 buffer visible-ok frame
)
3994 (get-buffer "*scratch*")
3995 (let ((scratch (get-buffer-create "*scratch*")))
3996 (set-buffer-major-mode scratch
)
3999 (defcustom frame-auto-hide-function
#'iconify-frame
4000 "Function called to automatically hide frames.
4001 The function is called with one argument - a frame.
4003 Functions affected by this option are those that bury a buffer
4004 shown in a separate frame like `quit-window' and `bury-buffer'."
4005 :type
'(choice (const :tag
"Iconify" iconify-frame
)
4006 (const :tag
"Delete" delete-frame
)
4007 (const :tag
"Do nothing" ignore
)
4013 (defun window--delete (&optional window dedicated-only kill
)
4014 "Delete WINDOW if possible.
4015 WINDOW must be a live window and defaults to the selected one.
4016 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
4017 only if it's dedicated to its buffer. Optional argument KILL
4018 means the buffer shown in window will be killed. Return non-nil
4019 if WINDOW gets deleted or its frame is auto-hidden."
4020 (setq window
(window-normalize-window window t
))
4021 (unless (and dedicated-only
(not (window-dedicated-p window
)))
4022 (let ((deletable (window-deletable-p window
)))
4024 ((eq deletable
'frame
)
4025 (let ((frame (window-frame window
)))
4028 (delete-frame frame
))
4029 ((functionp frame-auto-hide-function
)
4030 (funcall frame-auto-hide-function frame
))))
4033 (delete-window window
)
4036 (defun bury-buffer (&optional buffer-or-name
)
4037 "Put BUFFER-OR-NAME at the end of the list of all buffers.
4038 There it is the least likely candidate for `other-buffer' to
4039 return; thus, the least likely buffer for \\[switch-to-buffer] to
4042 You can specify a buffer name as BUFFER-OR-NAME, or an actual
4043 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
4044 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
4045 remove the current buffer from the selected window if it is
4048 (let* ((buffer (window-normalize-buffer buffer-or-name
)))
4049 ;; If `buffer-or-name' is not on the selected frame we unrecord it
4050 ;; although it's not "here" (call it a feature).
4051 (bury-buffer-internal buffer
)
4052 ;; Handle case where `buffer-or-name' is nil and the current buffer
4053 ;; is shown in the selected window.
4055 ((or buffer-or-name
(not (eq buffer
(window-buffer)))))
4056 ((window--delete nil t
))
4058 ;; Switch to another buffer in window.
4059 (set-window-dedicated-p nil nil
)
4060 (switch-to-prev-buffer nil
'bury
)))
4062 ;; Always return nil.
4065 (defun unbury-buffer ()
4066 "Switch to the last buffer in the buffer list."
4068 (switch-to-buffer (last-buffer)))
4070 (defun next-buffer ()
4071 "In selected window switch to next buffer."
4074 ((window-minibuffer-p)
4075 (error "Cannot switch buffers in minibuffer window"))
4076 ((eq (window-dedicated-p) t
)
4077 (error "Window is strongly dedicated to its buffer"))
4079 (switch-to-next-buffer))))
4081 (defun previous-buffer ()
4082 "In selected window switch to previous buffer."
4085 ((window-minibuffer-p)
4086 (error "Cannot switch buffers in minibuffer window"))
4087 ((eq (window-dedicated-p) t
)
4088 (error "Window is strongly dedicated to its buffer"))
4090 (switch-to-prev-buffer))))
4092 (defun delete-windows-on (&optional buffer-or-name frame
)
4093 "Delete all windows showing BUFFER-OR-NAME.
4094 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4095 and defaults to the current buffer.
4097 The following non-nil values of the optional argument FRAME
4098 have special meanings:
4100 - t means consider all windows on the selected frame only.
4102 - `visible' means consider all windows on all visible frames on
4103 the current terminal.
4105 - 0 (the number zero) means consider all windows on all visible
4106 and iconified frames on the current terminal.
4108 - A frame means consider all windows on that frame only.
4110 Any other value of FRAME means consider all windows on all
4113 When a window showing BUFFER-OR-NAME is dedicated and the only
4114 window of its frame, that frame is deleted when there are other
4116 (interactive "BDelete windows on (buffer):\nP")
4117 (let ((buffer (window-normalize-buffer buffer-or-name
))
4118 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4119 ;; `window-list-1' based function.
4120 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
4121 (dolist (window (window-list-1 nil nil all-frames
))
4122 (if (eq (window-buffer window
) buffer
)
4123 (let ((deletable (window-deletable-p window
)))
4125 ((and (eq deletable
'frame
) (window-dedicated-p window
))
4126 ;; Delete frame if and only if window is dedicated.
4127 (delete-frame (window-frame window
)))
4130 (delete-window window
))
4132 ;; In window switch to previous buffer.
4133 (set-window-dedicated-p window nil
)
4134 (switch-to-prev-buffer window
'bury
))))
4135 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4136 (unrecord-window-buffer window buffer
)))))
4138 (defun replace-buffer-in-windows (&optional buffer-or-name
)
4139 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
4140 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4141 and defaults to the current buffer.
4143 When a window showing BUFFER-OR-NAME is dedicated, that window is
4144 deleted. If that window is the only window on its frame, the
4145 frame is deleted too when there are other frames left. If there
4146 are no other frames left, some other buffer is displayed in that
4149 This function removes the buffer denoted by BUFFER-OR-NAME from
4150 all window-local buffer lists."
4151 (interactive "bBuffer to replace: ")
4152 (let ((buffer (window-normalize-buffer buffer-or-name
)))
4153 (dolist (window (window-list-1 nil nil t
))
4154 (if (eq (window-buffer window
) buffer
)
4155 (unless (window--delete window t t
)
4156 ;; Switch to another buffer in window.
4157 (set-window-dedicated-p window nil
)
4158 (switch-to-prev-buffer window
'kill
))
4159 ;; Unrecord BUFFER in WINDOW.
4160 (unrecord-window-buffer window buffer
)))))
4162 (defun quit-restore-window (&optional window bury-or-kill
)
4163 "Quit WINDOW and deal with its buffer.
4164 WINDOW must be a live window and defaults to the selected one.
4166 According to information stored in WINDOW's `quit-restore' window
4167 parameter either (1) delete WINDOW and its frame, (2) delete
4168 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4169 or (4) make WINDOW display some other buffer than the present
4170 one. If non-nil, reset `quit-restore' parameter to nil.
4172 Optional second argument BURY-OR-KILL tells how to proceed with
4173 the buffer of WINDOW. The following values are handled:
4175 `nil' means to not handle the buffer in a particular way. This
4176 means that if WINDOW is not deleted by this function, invoking
4177 `switch-to-prev-buffer' will usually show the buffer again.
4179 `append' means that if WINDOW is not deleted, move its buffer to
4180 the end of WINDOW's previous buffers so it's less likely that a
4181 future invocation of `switch-to-prev-buffer' will switch to it.
4182 Also, move the buffer to the end of the frame's buffer list.
4184 `bury' means that if WINDOW is not deleted, remove its buffer
4185 from WINDOW'S list of previous buffers. Also, move the buffer
4186 to the end of the frame's buffer list. This value provides the
4187 most reliable remedy to not have `switch-to-prev-buffer' switch
4188 to this buffer again without killing the buffer.
4190 `kill' means to kill WINDOW's buffer."
4191 (setq window
(window-normalize-window window t
))
4192 (let* ((buffer (window-buffer window
))
4193 (quit-restore (window-parameter window
'quit-restore
))
4195 (let* ((prev-buffers (window-prev-buffers window
))
4196 (prev-buffer (caar prev-buffers
)))
4197 (and (or (not (eq prev-buffer buffer
))
4198 (and (cdr prev-buffers
)
4199 (not (eq (setq prev-buffer
(cadr prev-buffers
))
4204 ((and (not prev-buffer
)
4205 (or (eq (nth 1 quit-restore
) 'frame
)
4206 (and (eq (nth 1 quit-restore
) 'window
)
4207 ;; If the window has been created on an existing
4208 ;; frame and ended up as the sole window on that
4209 ;; frame, do not delete it (Bug#12764).
4210 (not (eq window
(frame-root-window window
)))))
4211 (eq (nth 3 quit-restore
) buffer
)
4212 ;; Delete WINDOW if possible.
4213 (window--delete window nil
(eq bury-or-kill
'kill
)))
4214 ;; If the previously selected window is still alive, select it.
4215 (when (window-live-p (nth 2 quit-restore
))
4216 (select-window (nth 2 quit-restore
))))
4217 ((and (listp (setq quad
(nth 1 quit-restore
)))
4218 (buffer-live-p (car quad
))
4219 (eq (nth 3 quit-restore
) buffer
))
4220 ;; Show another buffer stored in quit-restore parameter.
4221 (when (and (integerp (nth 3 quad
))
4222 (/= (nth 3 quad
) (window-total-height window
)))
4223 ;; Try to resize WINDOW to its old height but don't signal an
4226 (window-resize window
(- (nth 3 quad
) (window-total-height window
)))
4228 (set-window-dedicated-p window nil
)
4229 ;; Restore WINDOW's previous buffer, start and point position.
4230 (set-window-buffer-start-and-point
4231 window
(nth 0 quad
) (nth 1 quad
) (nth 2 quad
))
4232 ;; Deal with the buffer we just removed from WINDOW.
4233 (setq entry
(and (eq bury-or-kill
'append
)
4234 (assq buffer
(window-prev-buffers window
))))
4236 ;; Remove buffer from WINDOW's previous and next buffers.
4237 (set-window-prev-buffers
4238 window
(assq-delete-all buffer
(window-prev-buffers window
)))
4239 (set-window-next-buffers
4240 window
(delq buffer
(window-next-buffers window
))))
4242 ;; Append old buffer's entry to list of WINDOW's previous
4243 ;; buffers so it's less likely to get switched to soon but
4244 ;; `display-buffer-in-previous-window' can nevertheless find it.
4245 (set-window-prev-buffers
4246 window
(append (window-prev-buffers window
) (list entry
))))
4247 ;; Reset the quit-restore parameter.
4248 (set-window-parameter window
'quit-restore nil
)
4249 ;; Select old window.
4250 (when (window-live-p (nth 2 quit-restore
))
4251 (select-window (nth 2 quit-restore
))))
4253 ;; Show some other buffer in WINDOW and reset the quit-restore
4255 (set-window-parameter window
'quit-restore nil
)
4256 ;; Make sure that WINDOW is no more dedicated.
4257 (set-window-dedicated-p window nil
)
4258 (switch-to-prev-buffer window bury-or-kill
)))
4260 ;; Deal with the buffer.
4262 ((not (buffer-live-p buffer
)))
4263 ((eq bury-or-kill
'kill
)
4264 (kill-buffer buffer
))
4266 (bury-buffer-internal buffer
)))))
4268 (defun quit-window (&optional kill window
)
4269 "Quit WINDOW and bury its buffer.
4270 WINDOW must be a live window and defaults to the selected one.
4271 With prefix argument KILL non-nil, kill the buffer instead of
4274 According to information stored in WINDOW's `quit-restore' window
4275 parameter either (1) delete WINDOW and its frame, (2) delete
4276 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4277 or (4) make WINDOW display some other buffer than the present
4278 one. If non-nil, reset `quit-restore' parameter to nil."
4280 (quit-restore-window window
(if kill
'kill
'bury
)))
4282 (defun quit-windows-on (&optional buffer-or-name kill frame
)
4283 "Quit all windows showing BUFFER-OR-NAME.
4284 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4285 and defaults to the current buffer. Optional argument KILL
4286 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4287 BUFFER-OR-NAME. Optional argument FRAME is handled as by
4288 `delete-windows-on'.
4290 This function calls `quit-window' on all candidate windows
4291 showing BUFFER-OR-NAME."
4292 (interactive "BQuit windows on (buffer):\nP")
4293 (let ((buffer (window-normalize-buffer buffer-or-name
))
4294 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4295 ;; `window-list-1' based function.
4296 (all-frames (cond ((not frame
) t
) ((eq frame t
) nil
) (t frame
))))
4297 (dolist (window (window-list-1 nil nil all-frames
))
4298 (if (eq (window-buffer window
) buffer
)
4299 (quit-window kill window
)
4300 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4301 (unrecord-window-buffer window buffer
)))))
4303 (defun split-window (&optional window size side pixelwise
)
4304 "Make a new window adjacent to WINDOW.
4305 WINDOW must be a valid window and defaults to the selected one.
4306 Return the new window which is always a live window.
4308 Optional argument SIZE a positive number means make WINDOW SIZE
4309 lines or columns tall. If SIZE is negative, make the new window
4310 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
4311 absolute value can be less than `window-min-height' or
4312 `window-min-width'; so this command can make a new window as
4313 small as one line or two columns. SIZE defaults to half of
4316 Optional third argument SIDE nil (or `below') specifies that the
4317 new window shall be located below WINDOW. SIDE `above' means the
4318 new window shall be located above WINDOW. In both cases SIZE
4319 specifies the new number of lines for WINDOW (or the new window
4320 if SIZE is negative) including space reserved for the mode and/or
4323 SIDE t (or `right') specifies that the new window shall be
4324 located on the right side of WINDOW. SIDE `left' means the new
4325 window shall be located on the left of WINDOW. In both cases
4326 SIZE specifies the new number of columns for WINDOW (or the new
4327 window provided SIZE is negative) including space reserved for
4328 fringes and the scrollbar or a divider column. Any other non-nil
4329 value for SIDE is currently handled like t (or `right').
4331 PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
4333 If the variable `ignore-window-parameters' is non-nil or the
4334 `split-window' parameter of WINDOW equals t, do not process any
4335 parameters of WINDOW. Otherwise, if the `split-window' parameter
4336 of WINDOW specifies a function, call that function with all three
4337 arguments and return the value returned by that function.
4339 Otherwise, if WINDOW is part of an atomic window, \"split\" the
4340 root of that atomic window. The new window does not become a
4341 member of that atomic window.
4343 If WINDOW is live, properties of the new window like margins and
4344 scrollbars are inherited from WINDOW. If WINDOW is an internal
4345 window, these properties as well as the buffer displayed in the
4346 new window are inherited from the window selected on WINDOW's
4347 frame. The selected window is not changed by this function."
4348 (setq window
(window-normalize-window window
))
4351 ((memq side
'(below above right left
)) side
)
4353 (horizontal (not (memq side
'(below above
))))
4354 (frame (window-frame window
))
4355 (parent (window-parent window
))
4356 (function (window-parameter window
'split-window
))
4357 (window-side (window-parameter window
'window-side
))
4358 ;; Rebind the following two variables since in some cases we
4359 ;; have to override their value.
4360 (window-combination-limit window-combination-limit
)
4361 (window-combination-resize window-combination-resize
)
4362 (char-size (frame-char-size window horizontal
))
4364 (when (numberp size
)
4365 (window--size-to-pixel window size horizontal pixelwise t
)))
4366 (divider-width (if horizontal
4367 (frame-right-divider-width frame
)
4368 (frame-bottom-divider-width frame
)))
4370 (window--check frame
)
4373 ;; Ignore window parameters if either `ignore-window-parameters'
4374 ;; is t or the `split-window' parameter equals t.
4375 ((or ignore-window-parameters
(eq function t
)))
4376 ((functionp function
)
4377 ;; The `split-window' parameter specifies the function to call.
4378 ;; If that function is `ignore', do nothing.
4379 (throw 'done
(funcall function window size side
)))
4380 ;; If WINDOW is part of an atomic window, split the root window
4381 ;; of that atomic window instead.
4382 ((and (window-parameter window
'window-atom
)
4383 (setq atom-root
(window-atom-root window
))
4384 (not (eq atom-root window
)))
4385 (throw 'done
(split-window atom-root size side pixelwise
)))
4386 ;; If WINDOW is a side window or its first or last child is a
4387 ;; side window, throw an error unless `window-combination-resize'
4389 ((and (not (eq window-combination-resize
'side
))
4390 (window--side-window-p window
))
4391 (error "Cannot split side window or parent of side window"))
4392 ;; If `window-combination-resize' is 'side and window has a side
4393 ;; window sibling, bind `window-combination-limit' to t.
4394 ((and (not (eq window-combination-resize
'side
))
4395 (or (and (window-prev-sibling window
)
4397 (window-prev-sibling window
) 'window-side
))
4398 (and (window-next-sibling window
)
4400 (window-next-sibling window
) 'window-side
))))
4401 (setq window-combination-limit t
)))
4403 ;; If `window-combination-resize' is t and SIZE is non-negative,
4404 ;; bind `window-combination-limit' to t.
4405 (when (and (eq window-combination-resize t
)
4406 pixel-size
(> pixel-size
0))
4407 (setq window-combination-limit t
))
4409 (let* ((parent-pixel-size
4410 ;; `parent-pixel-size' is the pixel size of WINDOW's
4411 ;; parent, provided it has one.
4412 (when parent
(window-size parent horizontal t
)))
4413 ;; `resize' non-nil means we are supposed to resize other
4414 ;; windows in WINDOW's combination.
4416 (and window-combination-resize
4417 (or (window-parameter window
'window-side
)
4418 (not (eq window-combination-resize
'side
)))
4419 (not (eq window-combination-limit t
))
4420 ;; Resize makes sense in iso-combinations only.
4421 (window-combined-p window horizontal
)))
4422 ;; `old-pixel-size' is the current pixel size of WINDOW.
4423 (old-pixel-size (window-size window horizontal t
))
4424 ;; `new-size' is the specified or calculated size of the
4426 new-pixel-size new-parent new-normal
)
4429 (setq new-pixel-size
4431 ;; When resizing try to give the new window the
4432 ;; average size of a window in its combination.
4433 (min (- parent-pixel-size
4434 (window-min-size parent horizontal nil t
))
4435 (/ parent-pixel-size
4436 (1+ (window-combinations parent horizontal
))))
4437 ;; Else try to give the new window half the size
4438 ;; of WINDOW (plus an eventual odd pixel).
4439 (/ old-pixel-size
2)))
4440 (unless window-resize-pixelwise
4441 ;; Round to nearest char-size multiple.
4442 (setq new-pixel-size
4443 (* char-size
(round new-pixel-size char-size
)))))
4445 ;; SIZE non-negative specifies the new size of WINDOW.
4447 ;; Note: Specifying a non-negative SIZE is practically
4448 ;; always done as workaround for making the new window
4449 ;; appear above or on the left of the new window (the
4450 ;; ispell window is a typical example of that). In all
4451 ;; these cases the SIDE argument should be set to 'above
4452 ;; or 'left in order to support the 'resize option.
4453 ;; Here we have to nest the windows instead, see above.
4454 (setq new-pixel-size
(- old-pixel-size pixel-size
)))
4456 ;; SIZE negative specifies the size of the new window.
4457 (setq new-pixel-size
(- pixel-size
))))
4464 ;; SIZE unspecified, resizing.
4465 (unless (window-sizable-p
4466 parent
(- new-pixel-size divider-width
) horizontal nil t
)
4467 (error "Window %s too small for splitting (1)" parent
)))
4468 ((> (+ new-pixel-size divider-width
4469 (window-min-size window horizontal nil t
))
4471 ;; SIZE unspecified, no resizing.
4472 (error "Window %s too small for splitting (2)" window
))))
4473 ((and (>= pixel-size
0)
4474 (or (>= pixel-size old-pixel-size
)
4476 (window-safe-min-pixel-size window horizontal
))))
4477 ;; SIZE specified as new size of old window. If the new size
4478 ;; is larger than the old size or the size of the new window
4479 ;; would be less than the safe minimum, signal an error.
4480 (error "Window %s too small for splitting (3)" window
))
4482 ;; SIZE specified, resizing.
4483 (unless (window-sizable-p
4484 parent
(- new-pixel-size divider-width
) horizontal nil t
)
4485 ;; If we cannot resize the parent give up.
4486 (error "Window %s too small for splitting (4)" parent
)))
4487 ((or (< new-pixel-size
4488 (window-safe-min-pixel-size window horizontal
))
4489 (< (- old-pixel-size new-pixel-size
)
4490 (window-safe-min-pixel-size window horizontal
)))
4491 ;; SIZE specification violates minimum size restrictions.
4492 (error "Window %s too small for splitting (5)" window
)))
4494 (window--resize-reset frame horizontal
)
4497 ;; Make new-parent non-nil if we need a new parent window;
4498 ;; either because we want to nest or because WINDOW is not
4500 (or (eq window-combination-limit t
)
4501 (not (window-combined-p window horizontal
))))
4503 ;; Make new-normal the normal size of the new window.
4505 (pixel-size (/ (float new-pixel-size
)
4506 (if new-parent old-pixel-size parent-pixel-size
)))
4508 (resize (/ 1.0 (1+ (window-combinations parent horizontal
))))
4509 (t (/ (window-normal-size window horizontal
) 2.0))))
4512 ;; Try to get space from OLD's siblings. We could go "up" and
4513 ;; try getting additional space from surrounding windows but
4514 ;; we won't be able to return space to those windows when we
4515 ;; delete the one we create here. Hence we do not go up.
4517 (window--resize-child-windows
4518 parent
(- new-pixel-size
) horizontal
)
4519 (let* ((normal (- 1.0 new-normal
))
4520 (sub (window-child parent
)))
4522 (set-window-new-normal
4523 sub
(* (window-normal-size sub horizontal
) normal
))
4524 (setq sub
(window-right sub
)))))
4525 ;; Get entire space from WINDOW.
4526 (set-window-new-pixel
4527 window
(- old-pixel-size new-pixel-size
))
4528 ;; (set-window-new-pixel window (- old-pixel-size new-pixel-size))
4529 ;; (set-window-new-total
4530 ;; window (- old-size new-size))
4531 (window--resize-this-window window
(- new-pixel-size
) horizontal
)
4532 (set-window-new-normal
4533 window
(- (if new-parent
1.0 (window-normal-size window horizontal
))
4536 (let* ((new (split-window-internal window new-pixel-size side new-normal
)))
4537 (window--pixel-to-total frame horizontal
)
4538 ;; Assign window-side parameters, if any.
4540 ((eq window-combination-resize
'side
)
4543 (window-side window-side
)
4544 ((eq side
'above
) 'top
)
4545 ((eq side
'below
) 'bottom
)
4547 ;; We made a new side window.
4548 (set-window-parameter new
'window-side window-side
)
4549 (when (and new-parent
(window-parameter window
'window-side
))
4550 ;; We've been splitting a side root window. Give the
4551 ;; new parent the same window-side parameter.
4552 (set-window-parameter
4553 (window-parent new
) 'window-side window-side
))))
4554 ((eq window-combination-resize
'atom
)
4555 ;; Make sure `window--check-frame' won't destroy an existing
4556 ;; atomic window in case the new window gets nested inside.
4557 (unless (window-parameter window
'window-atom
)
4558 (set-window-parameter window
'window-atom t
))
4560 (set-window-parameter (window-parent new
) 'window-atom t
))
4561 (set-window-parameter new
'window-atom t
)))
4564 (window--sanitize-window-sizes frame horizontal
)
4566 (run-window-configuration-change-hook frame
)
4567 (run-window-scroll-functions new
)
4568 (window--check frame
)
4569 ;; Always return the new window.
4572 ;; I think this should be the default; I think people will prefer it--rms.
4573 (defcustom split-window-keep-point t
4574 "If non-nil, \\[split-window-below] preserves point in the new window.
4575 If nil, adjust point in the two windows to minimize redisplay.
4576 This option applies only to `split-window-below' and functions
4577 that call it. The low-level `split-window' function always keeps
4578 the original point in both windows."
4582 (defun split-window-below (&optional size
)
4583 "Split the selected window into two windows, one above the other.
4584 The selected window is above. The newly split-off window is
4585 below, and displays the same buffer. Return the new window.
4587 If optional argument SIZE is omitted or nil, both windows get the
4588 same height, or close to it. If SIZE is positive, the upper
4589 \(selected) window gets SIZE lines. If SIZE is negative, the
4590 lower (new) window gets -SIZE lines.
4592 If the variable `split-window-keep-point' is non-nil, both
4593 windows get the same value of point as the selected window.
4594 Otherwise, the window starts are chosen so as to minimize the
4595 amount of redisplay; this is convenient on slow terminals."
4597 (let ((old-window (selected-window))
4598 (old-point (window-point))
4599 (size (and size
(prefix-numeric-value size
)))
4600 moved-by-window-height moved new-window bottom
)
4601 (when (and size
(< size
0) (< (- size
) window-min-height
))
4602 ;; `split-window' would not signal an error here.
4603 (error "Size of new window too small"))
4604 (setq new-window
(split-window nil size
))
4605 (unless split-window-keep-point
4606 (with-current-buffer (window-buffer)
4607 ;; Use `save-excursion' around vertical movements below
4608 ;; (Bug#10971). Note: When the selected window's buffer has a
4609 ;; header line, up to two lines of the buffer may not show up
4610 ;; in the resulting configuration.
4612 (goto-char (window-start))
4613 (setq moved
(vertical-motion (window-height)))
4614 (set-window-start new-window
(point))
4615 (when (> (point) (window-point new-window
))
4616 (set-window-point new-window
(point)))
4617 (when (= moved
(window-height))
4618 (setq moved-by-window-height t
)
4619 (vertical-motion -
1))
4620 (setq bottom
(point)))
4621 (and moved-by-window-height
4623 (set-window-point old-window
(1- bottom
)))
4624 (and moved-by-window-height
4625 (<= (window-start new-window
) old-point
)
4626 (set-window-point new-window old-point
)
4627 (select-window new-window
))))
4628 ;; Always copy quit-restore parameter in interactive use.
4629 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
4631 (set-window-parameter new-window
'quit-restore quit-restore
)))
4634 (defalias 'split-window-vertically
'split-window-below
)
4636 (defun split-window-right (&optional size
)
4637 "Split the selected window into two side-by-side windows.
4638 The selected window is on the left. The newly split-off window
4639 is on the right, and displays the same buffer. Return the new
4642 If optional argument SIZE is omitted or nil, both windows get the
4643 same width, or close to it. If SIZE is positive, the left-hand
4644 \(selected) window gets SIZE columns. If SIZE is negative, the
4645 right-hand (new) window gets -SIZE columns. Here, SIZE includes
4646 the width of the window's scroll bar; if there are no scroll
4647 bars, it includes the width of the divider column to the window's
4650 (let ((old-window (selected-window))
4651 (size (and size
(prefix-numeric-value size
)))
4653 (when (and size
(< size
0) (< (- size
) window-min-width
))
4654 ;; `split-window' would not signal an error here.
4655 (error "Size of new window too small"))
4656 (setq new-window
(split-window nil size t
))
4657 ;; Always copy quit-restore parameter in interactive use.
4658 (let ((quit-restore (window-parameter old-window
'quit-restore
)))
4660 (set-window-parameter new-window
'quit-restore quit-restore
)))
4663 (defalias 'split-window-horizontally
'split-window-right
)
4665 ;;; Balancing windows.
4667 ;; The following routine uses the recycled code from an old version of
4668 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
4669 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
4670 ;; more readable (FWIW we'd need three loops - one to calculate the
4671 ;; minimum sizes per window, one to enlarge or shrink windows until the
4672 ;; new parent-size matches, and one where we shrink the largest/enlarge
4673 ;; the smallest window).
4674 (defun balance-windows-2 (window horizontal
)
4675 "Subroutine of `balance-windows-1'.
4676 WINDOW must be a vertical combination (horizontal if HORIZONTAL
4678 (let* ((char-size (if window-resize-pixelwise
4680 (frame-char-size window horizontal
)))
4681 (first (window-child window
))
4683 (number-of-children 0)
4684 (parent-size (window-new-pixel window
))
4685 (total-sum parent-size
)
4686 failed size sub-total sub-delta sub-amount rest
)
4688 (setq number-of-children
(1+ number-of-children
))
4689 (when (window-size-fixed-p sub horizontal
)
4691 (- total-sum
(window-size sub horizontal t
)))
4692 (set-window-new-normal sub
'ignore
))
4693 (setq sub
(window-right sub
)))
4696 (while (and failed
(> number-of-children
0))
4697 (setq size
(/ total-sum number-of-children
))
4700 (while (and sub
(not failed
))
4701 ;; Ignore child windows that should be ignored or are stuck.
4702 (unless (window--resize-child-windows-skip-p sub
)
4703 (setq sub-total
(window-size sub horizontal t
))
4704 (setq sub-delta
(- size sub-total
))
4706 (window-sizable sub sub-delta horizontal nil t
))
4707 ;; Register the new total size for this child window.
4708 (set-window-new-pixel sub
(+ sub-total sub-amount
))
4709 (unless (= sub-amount sub-delta
)
4710 (setq total-sum
(- total-sum sub-total sub-amount
))
4711 (setq number-of-children
(1- number-of-children
))
4712 ;; We failed and need a new round.
4714 (set-window-new-normal sub
'skip
)))
4715 (setq sub
(window-right sub
))))
4717 ;; How can we be sure that `number-of-children' is NOT zero here ?
4718 (setq rest
(% total-sum number-of-children
))
4719 ;; Fix rounding by trying to enlarge non-stuck windows by one line
4720 ;; (column) until `rest' is zero.
4722 (while (and sub
(> rest
0))
4723 (unless (window--resize-child-windows-skip-p window
)
4724 (set-window-new-pixel sub
(min rest char-size
) t
)
4725 (setq rest
(- rest char-size
)))
4726 (setq sub
(window-right sub
)))
4728 ;; Fix rounding by trying to enlarge stuck windows by one line
4729 ;; (column) until `rest' equals zero.
4731 (while (and sub
(> rest
0))
4732 (unless (eq (window-new-normal sub
) 'ignore
)
4733 (set-window-new-pixel sub
(min rest char-size
) t
)
4734 (setq rest
(- rest char-size
)))
4735 (setq sub
(window-right sub
)))
4739 ;; Record new normal sizes.
4740 (set-window-new-normal
4741 sub
(/ (if (eq (window-new-normal sub
) 'ignore
)
4742 (window-size sub horizontal t
)
4743 (window-new-pixel sub
))
4744 (float parent-size
)))
4745 ;; Recursively balance each window's child windows.
4746 (balance-windows-1 sub horizontal
)
4747 (setq sub
(window-right sub
)))))
4749 (defun balance-windows-1 (window &optional horizontal
)
4750 "Subroutine of `balance-windows'."
4751 (if (window-child window
)
4752 (let ((sub (window-child window
)))
4753 (if (window-combined-p sub horizontal
)
4754 (balance-windows-2 window horizontal
)
4755 (let ((size (window-new-pixel window
)))
4757 (set-window-new-pixel sub size
)
4758 (balance-windows-1 sub horizontal
)
4759 (setq sub
(window-right sub
))))))))
4761 (defun balance-windows (&optional window-or-frame
)
4762 "Balance the sizes of windows of WINDOW-OR-FRAME.
4763 WINDOW-OR-FRAME is optional and defaults to the selected frame.
4764 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
4765 windows of that frame. If WINDOW-OR-FRAME denotes a window,
4766 recursively balance the sizes of all child windows of that
4771 ((or (not window-or-frame
)
4772 (frame-live-p window-or-frame
))
4773 (frame-root-window window-or-frame
))
4774 ((or (window-live-p window-or-frame
)
4775 (window-child window-or-frame
))
4778 (error "Not a window or frame %s" window-or-frame
))))
4779 (frame (window-frame window
)))
4780 ;; Balance vertically.
4781 (window--resize-reset (window-frame window
))
4782 (balance-windows-1 window
)
4783 (when (window--resize-apply-p frame
)
4784 (window-resize-apply frame
)
4785 (window--pixel-to-total frame
)
4786 (run-window-configuration-change-hook frame
))
4787 ;; Balance horizontally.
4788 (window--resize-reset (window-frame window
) t
)
4789 (balance-windows-1 window t
)
4790 (when (window--resize-apply-p frame t
)
4791 (window-resize-apply frame t
)
4792 (window--pixel-to-total frame t
)
4793 (run-window-configuration-change-hook frame
))))
4795 (defun window-fixed-size-p (&optional window direction
)
4796 "Return t if WINDOW cannot be resized in DIRECTION.
4797 WINDOW defaults to the selected window. DIRECTION can be
4798 nil (i.e. any), `height' or `width'."
4799 (with-current-buffer (window-buffer window
)
4800 (when (and (boundp 'window-size-fixed
) window-size-fixed
)
4802 (member (cons direction window-size-fixed
)
4803 '((height . width
) (width . height
))))))))
4805 ;;; A different solution to balance-windows.
4806 (defvar window-area-factor
1
4807 "Factor by which the window area should be over-estimated.
4808 This is used by `balance-windows-area'.
4809 Changing this globally has no effect.")
4810 (make-variable-buffer-local 'window-area-factor
)
4812 (defun balance-windows-area-adjust (window delta horizontal pixelwise
)
4813 "Wrapper around `window-resize' with error checking.
4814 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
4815 ;; `window-resize' may fail if delta is too large.
4816 (while (>= (abs delta
) 1)
4819 ;; It was wrong to use `window-resize' here. Somehow
4820 ;; `balance-windows-area' depends on resizing windows
4822 (adjust-window-trailing-edge window delta horizontal pixelwise
)
4825 ;;(message "adjust: %s" (error-message-string err))
4826 (setq delta
(/ delta
2))))))
4828 (defun balance-windows-area ()
4829 "Make all visible windows the same area (approximately).
4830 See also `window-area-factor' to change the relative size of
4833 (let* ((unchanged 0) (carry 0) (round 0)
4834 ;; Remove fixed-size windows.
4835 (wins (delq nil
(mapcar (lambda (win)
4836 (if (not (window-fixed-size-p win
)) win
))
4837 (window-list nil
'nomini
))))
4839 (pixelwise window-resize-pixelwise
)
4841 ;; Resizing a window changes the size of surrounding windows in complex
4842 ;; ways, so it's difficult to balance them all. The introduction of
4843 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
4844 ;; very difficult to do. `balance-window' above takes an off-line
4845 ;; approach: get the whole window tree, then balance it, then try to
4846 ;; adjust the windows so they fit the result.
4847 ;; Here, instead, we take a "local optimization" approach, where we just
4848 ;; go through all the windows several times until nothing needs to be
4849 ;; changed. The main problem with this approach is that it's difficult
4850 ;; to make sure it terminates, so we use some heuristic to try and break
4851 ;; off infinite loops.
4852 ;; After a round without any change, we allow a second, to give a chance
4853 ;; to the carry to propagate a minor imbalance from the end back to
4855 (while (< unchanged
2)
4856 ;; (message "New round")
4857 (setq unchanged
(1+ unchanged
) round
(1+ round
))
4860 (while (progn (setq next
(next-window next
))
4861 (window-fixed-size-p next
)))
4862 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
4864 (< (car (window-pixel-edges win
)) (car (window-pixel-edges next
))))
4865 (areadiff (/ (- (* (window-size next nil pixelwise
)
4866 (window-size next t pixelwise
)
4867 (buffer-local-value 'window-area-factor
4868 (window-buffer next
)))
4869 (* (window-size win nil pixelwise
)
4870 (window-size win t pixelwise
)
4871 (buffer-local-value 'window-area-factor
4872 (window-buffer win
))))
4873 (max (buffer-local-value 'window-area-factor
4874 (window-buffer win
))
4875 (buffer-local-value 'window-area-factor
4876 (window-buffer next
)))))
4878 (+ (window-size win nil pixelwise
)
4879 (window-size next nil pixelwise
))
4880 (+ (window-size win t pixelwise
)
4881 (window-size next t pixelwise
))))
4882 (diff (/ areadiff edgesize
)))
4884 ;; Maybe diff is actually closer to 1 than to 0.
4885 (setq diff
(/ (* 3 areadiff
) (* 2 edgesize
))))
4886 (when (and (zerop diff
) (not (zerop areadiff
)))
4887 (setq diff
(/ (+ areadiff carry
) edgesize
))
4888 ;; Change things smoothly.
4889 (if (or (> diff
1) (< diff -
1)) (setq diff
(/ diff
2))))
4891 ;; Make sure negligible differences don't accumulate to
4892 ;; become significant.
4893 (setq carry
(+ carry areadiff
))
4894 ;; This used `adjust-window-trailing-edge' before and uses
4895 ;; `window-resize' now. Error wrapping is still needed.
4896 (balance-windows-area-adjust win diff horiz pixelwise
)
4898 (let ((change (cons win
(window-pixel-edges win
))))
4899 ;; If the same change has been seen already for this window,
4900 ;; we're most likely in an endless loop, so don't count it as
4902 (unless (member change changelog
)
4903 (push change changelog
)
4904 (setq unchanged
0 carry
0)))))))
4905 ;; We've now basically balanced all the windows.
4906 ;; But there may be some minor off-by-one imbalance left over,
4907 ;; so let's do some fine tuning.
4908 ;; (bw-finetune wins)
4909 ;; (message "Done in %d rounds" round)
4912 ;;; Window states, how to get them and how to put them in a window.
4913 (defun window--state-get-1 (window &optional writable
)
4914 "Helper function for `window-state-get'."
4917 ((window-top-child window
) 'vc
)
4918 ((window-left-child window
) 'hc
)
4920 (buffer (window-buffer window
))
4921 (selected (eq window
(selected-window)))
4924 ,@(unless (window-next-sibling window
) `((last . t
)))
4925 (pixel-width .
,(window-pixel-width window
))
4926 (pixel-height .
,(window-pixel-height window
))
4927 (total-width .
,(window-total-width window
))
4928 (total-height .
,(window-total-height window
))
4929 (normal-height .
,(window-normal-size window
))
4930 (normal-width .
,(window-normal-size window t
))
4931 ,@(unless (window-live-p window
)
4932 `((combination-limit .
,(window-combination-limit window
))))
4933 ,@(let ((parameters (window-parameters window
))
4935 ;; Make copies of those window parameters whose
4936 ;; persistence property is `writable' if WRITABLE is
4937 ;; non-nil and non-nil if WRITABLE is nil.
4938 (dolist (par parameters
)
4939 (let ((pers (cdr (assq (car par
)
4940 window-persistent-parameters
))))
4941 (when (and pers
(or (not writable
) (eq pers
'writable
)))
4942 (setq list
(cons (cons (car par
) (cdr par
)) list
)))))
4943 ;; Add `clone-of' parameter if necessary.
4944 (let ((pers (cdr (assq 'clone-of
4945 window-persistent-parameters
))))
4946 (when (and pers
(or (not writable
) (eq pers
'writable
))
4947 (not (assq 'clone-of list
)))
4948 (setq list
(cons (cons 'clone-of window
) list
))))
4950 `((parameters .
,list
))))
4952 ;; All buffer related things go in here.
4953 (let ((point (window-point window
))
4954 (start (window-start window
)))
4956 ,(buffer-name buffer
)
4957 (selected .
,selected
)
4958 (hscroll .
,(window-hscroll window
))
4959 (fringes .
,(window-fringes window
))
4960 (margins .
,(window-margins window
))
4961 (scroll-bars .
,(window-scroll-bars window
))
4962 (vscroll .
,(window-vscroll window
))
4963 (dedicated .
,(window-dedicated-p window
))
4964 (point .
,(if writable point
4967 'window-point-insertion-type
4969 (start .
,(if writable start
(copy-marker start
)))))))))
4971 (when (memq type
'(vc hc
))
4973 (setq window
(window-child window
))
4975 (setq list
(cons (window--state-get-1 window writable
) list
))
4976 (setq window
(window-right window
)))
4978 (append head tail
)))
4980 (defun window-state-get (&optional window writable
)
4981 "Return state of WINDOW as a Lisp object.
4982 WINDOW can be any window and defaults to the root window of the
4985 Optional argument WRITABLE non-nil means do not use markers for
4986 sampling `window-point' and `window-start'. Together, WRITABLE
4987 and the variable `window-persistent-parameters' specify which
4988 window parameters are saved by this function. WRITABLE should be
4989 non-nil when the return value shall be written to a file and read
4990 back in another session. Otherwise, an application may run into
4991 an `invalid-read-syntax' error while attempting to read back the
4994 The return value can be used as argument for `window-state-put'
4995 to put the state recorded here into an arbitrary window. The
4996 value can be also stored on disk and read back in a new session."
4999 (if (window-valid-p window
)
5001 (error "%s is not a live or internal window" window
))
5002 (frame-root-window)))
5003 ;; The return value is a cons whose car specifies some constraints on
5004 ;; the size of WINDOW. The cdr lists the states of the child windows
5007 ;; Frame related things would go into a function, say `frame-state',
5008 ;; calling `window-state-get' to insert the frame's root window.
5009 `((min-height .
,(window-min-size window
))
5010 (min-width .
,(window-min-size window t
))
5011 (min-height-ignore .
,(window-min-size window nil t
))
5012 (min-width-ignore .
,(window-min-size window t t
))
5013 (min-height-safe .
,(window-min-size window nil
'safe
))
5014 (min-width-safe .
,(window-min-size window t
'safe
))
5015 (min-pixel-height .
,(window-min-size window nil nil t
))
5016 (min-pixel-width .
,(window-min-size window t nil t
))
5017 (min-pixel-height-ignore .
,(window-min-size window nil t t
))
5018 (min-pixel-width-ignore .
,(window-min-size window t t t
))
5019 (min-pixel-height-safe .
,(window-min-size window nil
'safe t
))
5020 (min-pixel-width-safe .
,(window-min-size window t
'safe t
)))
5021 (window--state-get-1 window writable
)))
5023 (defvar window-state-put-list nil
5024 "Helper variable for `window-state-put'.")
5026 (defvar window-state-put-stale-windows nil
5027 "Helper variable for `window-state-put'.")
5029 (defun window--state-put-1 (state &optional window ignore totals pixelwise
)
5030 "Helper function for `window-state-put'."
5031 (let ((type (car state
)))
5032 (setq state
(cdr state
))
5035 ;; For a leaf window just add unprocessed entries to
5036 ;; `window-state-put-list'.
5037 (push (cons window state
) window-state-put-list
))
5038 ((memq type
'(vc hc
))
5039 (let* ((horizontal (eq type
'hc
))
5040 (total (window-size window horizontal pixelwise
))
5043 (dolist (item state
)
5044 ;; Find the next child window. WINDOW always points to the
5045 ;; real window that we want to fill with what we find here.
5046 (when (memq (car item
) '(leaf vc hc
))
5047 (if (assq 'last item
)
5048 ;; The last child window. Below `window--state-put-1'
5049 ;; will put into it whatever ITEM has in store.
5051 ;; Not the last child window, prepare for splitting
5052 ;; WINDOW. SIZE is the new (and final) size of the old
5058 (cdr (assq (if horizontal
5062 (cdr (assq (if horizontal
5066 ;; Use normalized size and round.
5069 (cdr (assq (if horizontal
'normal-width
'normal-height
)
5072 ;; Use safe sizes, we try to resize later.
5073 (setq size
(max size
5075 (* window-safe-min-width
5077 (frame-char-width (window-frame window
))
5079 (* window-safe-min-height
5081 (frame-char-height (window-frame window
))
5083 (if (window-sizable-p window
(- size
) horizontal
'safe pixelwise
)
5084 (let* ((window-combination-limit
5085 (assq 'combination-limit item
)))
5086 ;; We must inherit the combination limit, otherwise
5087 ;; we might mess up handling of atomic and side
5089 (setq new
(split-window window size horizontal pixelwise
)))
5090 ;; Give up if we can't resize window down to safe sizes.
5091 (error "Cannot resize window %s" window
))
5095 ;; When creating the first child window add for parent
5096 ;; unprocessed entries to `window-state-put-list'.
5097 (setq window-state-put-list
5098 (cons (cons (window-parent window
) state
)
5099 window-state-put-list
))))
5101 ;; Now process the current window (either the one we've just
5102 ;; split or the last child of its parent).
5103 (window--state-put-1 item window ignore totals
)
5104 ;; Continue with the last window split off.
5105 (setq window new
))))))))
5107 (defun window--state-put-2 (ignore pixelwise
)
5108 "Helper function for `window-state-put'."
5109 (dolist (item window-state-put-list
)
5110 (let ((window (car item
))
5111 (combination-limit (cdr (assq 'combination-limit item
)))
5112 (parameters (cdr (assq 'parameters item
)))
5113 (state (cdr (assq 'buffer item
))))
5114 (when combination-limit
5115 (set-window-combination-limit window combination-limit
))
5116 ;; Reset window's parameters and assign saved ones (we might want
5117 ;; a `remove-window-parameters' function here).
5118 (dolist (parameter (window-parameters window
))
5119 (set-window-parameter window
(car parameter
) nil
))
5121 (dolist (parameter parameters
)
5122 (set-window-parameter window
(car parameter
) (cdr parameter
))))
5123 ;; Process buffer related state.
5125 (let ((buffer (get-buffer (car state
))))
5127 (with-current-buffer buffer
5128 (set-window-buffer window buffer
)
5129 (set-window-hscroll window
(cdr (assq 'hscroll state
)))
5130 (apply 'set-window-fringes
5131 (cons window
(cdr (assq 'fringes state
))))
5132 (let ((margins (cdr (assq 'margins state
))))
5133 (set-window-margins window
(car margins
) (cdr margins
)))
5134 (let ((scroll-bars (cdr (assq 'scroll-bars state
))))
5135 (set-window-scroll-bars
5136 window
(car scroll-bars
) (nth 2 scroll-bars
)
5137 (or (nth 3 scroll-bars
) 0) (nth 5 scroll-bars
)))
5138 (set-window-vscroll window
(cdr (assq 'vscroll state
)))
5139 ;; Adjust vertically.
5140 (if (memq window-size-fixed
'(t height
))
5141 ;; A fixed height window, try to restore the
5145 (if pixelwise
'pixel-height
'total-height
)
5147 (window-size window nil pixelwise
)))
5149 (when (window--resizable-p
5150 window delta nil nil nil nil nil pixelwise
)
5151 (window-resize window delta nil nil pixelwise
)))
5152 ;; Else check whether the window is not high enough.
5154 (window-min-size window nil ignore pixelwise
))
5156 (- min-size
(window-size window nil pixelwise
))))
5157 (when (and (> delta
0)
5158 (window--resizable-p
5159 window delta nil ignore nil nil nil pixelwise
))
5160 (window-resize window delta nil ignore pixelwise
))))
5161 ;; Adjust horizontally.
5162 (if (memq window-size-fixed
'(t width
))
5163 ;; A fixed width window, try to restore the original
5167 (if pixelwise
'pixel-width
'total-width
)
5169 (window-size window t pixelwise
)))
5171 (when (window--resizable-p
5172 window delta nil nil nil nil nil pixelwise
)
5173 (window-resize window delta nil nil pixelwise
)))
5174 ;; Else check whether the window is not wide enough.
5175 (let* ((min-size (window-min-size window t ignore pixelwise
))
5176 (delta (- min-size
(window-size window t pixelwise
))))
5177 (when (and (> delta
0)
5178 (window--resizable-p
5179 window delta t ignore nil nil nil pixelwise
))
5180 (window-resize window delta t ignore pixelwise
))))
5181 ;; Set dedicated status.
5182 (set-window-dedicated-p window
(cdr (assq 'dedicated state
)))
5183 ;; Install positions (maybe we should do this after all
5184 ;; windows have been created and sized).
5186 (set-window-start window
(cdr (assq 'start state
)))
5187 (set-window-point window
(cdr (assq 'point state
))))
5188 ;; Select window if it's the selected one.
5189 (when (cdr (assq 'selected state
))
5190 (select-window window
)))
5191 ;; We don't want to raise an error in case the buffer does
5192 ;; not exist anymore, so we switch to a previous one and
5193 ;; save the window with the intention of deleting it later
5195 (switch-to-prev-buffer window
)
5196 (push window window-state-put-stale-windows
)))))))
5198 (defun window-state-put (state &optional window ignore
)
5199 "Put window state STATE into WINDOW.
5200 STATE should be the state of a window returned by an earlier
5201 invocation of `window-state-get'. Optional argument WINDOW must
5202 specify a valid window and defaults to the selected one. If
5203 WINDOW is not live, replace WINDOW by a live one before putting
5206 Optional argument IGNORE non-nil means ignore minimum window
5207 sizes and fixed size restrictions. IGNORE equal `safe' means
5208 windows can get as small as `window-safe-min-height' and
5209 `window-safe-min-width'."
5210 (setq window-state-put-stale-windows nil
)
5211 (setq window
(window-normalize-window window
))
5213 ;; When WINDOW is internal, reduce it to a live one to put STATE into,
5215 (unless (window-live-p window
)
5216 (let ((root (frame-root-window window
)))
5217 (if (eq window root
)
5218 (setq window
(frame-first-window root
))
5220 (setq window
(catch 'live
5221 (walk-window-subtree
5223 (when (window-live-p window
)
5224 (throw 'live window
)))
5226 (delete-other-windows-internal window root
)))
5228 (let* ((frame (window-frame window
))
5230 ;; We check here (1) whether the total sizes of root window of
5231 ;; STATE and that of WINDOW are equal so we can avoid
5232 ;; calculating new sizes, and (2) if we do have to resize
5233 ;; whether we can do so without violating size restrictions.
5234 (pixelwise (and (cdr (assq 'pixel-width state
))
5235 (cdr (assq 'pixel-height state
))))
5236 (totals (or (and pixelwise
5237 (= (window-pixel-width window
)
5238 (cdr (assq 'pixel-width state
)))
5239 (= (window-pixel-height window
)
5240 (cdr (assq 'pixel-height state
))))
5241 (and (= (window-total-width window
)
5242 (cdr (assq 'total-width state
)))
5243 (= (window-total-height window
)
5244 (cdr (assq 'total-height state
))))))
5245 (min-height (cdr (assq
5246 (if pixelwise
'min-pixel-height
'min-height
)
5248 (min-width (cdr (assq
5249 (if pixelwise
'min-pixel-width
'min-weight
)
5251 (if (and (not totals
)
5252 (or (> min-height
(window-size window nil pixelwise
))
5253 (> min-width
(window-size window t pixelwise
)))
5255 (and (setq min-height
5258 'min-pixel-height-ignore
5264 'min-pixel-width-ignore
5268 (window-size window nil pixelwise
))
5270 (window-size window t pixelwise
)))
5271 (or (not (eq ignore
'safe
))
5272 (and (setq min-height
5275 'min-pixel-height-safe
5281 'min-pixel-width-safe
5285 (window-size window nil pixelwise
))
5287 (window-size window t pixelwise
))))))))
5288 ;; The check above might not catch all errors due to rounding
5289 ;; issues - so IGNORE equal 'safe might not always produce the
5290 ;; minimum possible state. But such configurations hardly make
5292 (error "Window %s too small to accommodate state" window
)
5293 (setq state
(cdr state
))
5294 (setq window-state-put-list nil
)
5295 ;; Work on the windows of a temporary buffer to make sure that
5296 ;; splitting proceeds regardless of any buffer local values of
5297 ;; `window-size-fixed'. Release that buffer after the buffers of
5298 ;; all live windows have been set by `window--state-put-2'.
5300 (set-window-buffer window
(current-buffer))
5301 (window--state-put-1 state window nil totals pixelwise
)
5302 (window--state-put-2 ignore pixelwise
))
5303 (while window-state-put-stale-windows
5304 (let ((window (pop window-state-put-stale-windows
)))
5305 (when (eq (window-deletable-p window
) t
)
5306 (delete-window window
))))
5307 (window--check frame
))))
5309 (defun display-buffer-record-window (type window buffer
)
5310 "Record information for window used by `display-buffer'.
5311 TYPE specifies the type of the calling operation and must be one
5312 of the symbols 'reuse (when WINDOW existed already and was
5313 reused for displaying BUFFER), 'window (when WINDOW was created
5314 on an already existing frame), or 'frame (when WINDOW was
5315 created on a new frame). WINDOW is the window used for or created
5316 by the `display-buffer' routines. BUFFER is the buffer that
5319 This function installs or updates the quit-restore parameter of
5320 WINDOW. The quit-restore parameter is a list of four elements:
5321 The first element is one of the symbols 'window, 'frame, 'same or
5322 'other. The second element is either one of the symbols 'window
5323 or 'frame or a list whose elements are the buffer previously
5324 shown in the window, that buffer's window start and window point,
5325 and the window's height. The third element is the window
5326 selected at the time the parameter was created. The fourth
5330 (if (eq (window-buffer window
) buffer
)
5331 ;; WINDOW shows BUFFER already.
5332 (when (consp (window-parameter window
'quit-restore
))
5333 ;; If WINDOW has a quit-restore parameter, reset its car.
5334 (setcar (window-parameter window
'quit-restore
) 'same
))
5335 ;; WINDOW shows another buffer.
5336 (with-current-buffer (window-buffer window
)
5337 (set-window-parameter
5338 window
'quit-restore
5340 ;; A quadruple of WINDOW's buffer, start, point and height.
5341 (list (current-buffer) (window-start window
)
5342 ;; Preserve window-point-insertion-type (Bug#12588).
5344 (window-point window
) window-point-insertion-type
)
5345 (window-total-height window
))
5346 (selected-window) buffer
)))))
5348 ;; WINDOW has been created on an existing frame.
5349 (set-window-parameter
5350 window
'quit-restore
5351 (list 'window
'window
(selected-window) buffer
)))
5353 ;; WINDOW has been created on a new frame.
5354 (set-window-parameter
5355 window
'quit-restore
5356 (list 'frame
'frame
(selected-window) buffer
)))))
5358 (defcustom display-buffer-function nil
5359 "If non-nil, function to call to handle `display-buffer'.
5360 It will receive two args, the buffer and a flag which if non-nil
5361 means that the currently selected window is not acceptable. It
5362 should choose or create a window, display the specified buffer in
5363 it, and return the window.
5365 The specified function should call `display-buffer-record-window'
5366 with corresponding arguments to set up the quit-restore parameter
5367 of the window used."
5370 (function :tag
"function"))
5373 (make-obsolete-variable 'display-buffer-function
5374 'display-buffer-alist
"24.3")
5376 ;; Eventually, we want to turn this into a defvar; instead of
5377 ;; customizing this, the user should use a `pop-up-frame-parameters'
5378 ;; alist entry in `display-buffer-base-action'.
5379 (defcustom pop-up-frame-alist nil
5380 "Alist of parameters for automatically generated new frames.
5381 If non-nil, the value you specify here is used by the default
5382 `pop-up-frame-function' for the creation of new frames.
5384 Since `pop-up-frame-function' is used by `display-buffer' for
5385 making new frames, any value specified here by default affects
5386 the automatic generation of new frames via `display-buffer' and
5387 all functions based on it. The behavior of `make-frame' is not
5388 affected by this variable."
5389 :type
'(repeat (cons :format
"%v"
5390 (symbol :tag
"Parameter")
5391 (sexp :tag
"Value")))
5394 (defcustom pop-up-frame-function
5395 (lambda () (make-frame pop-up-frame-alist
))
5396 "Function used by `display-buffer' for creating a new frame.
5397 This function is called with no arguments and should return a new
5398 frame. The default value calls `make-frame' with the argument
5399 `pop-up-frame-alist'."
5403 (defcustom special-display-buffer-names nil
5404 "List of names of buffers that should be displayed specially.
5405 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5406 its name is in this list, displays the buffer in a way specified
5407 by `special-display-function'. `special-display-popup-frame'
5408 \(the default for `special-display-function') usually displays
5409 the buffer in a separate frame made with the parameters specified
5410 by `special-display-frame-alist'. If `special-display-function'
5411 has been set to some other function, that function is called with
5412 the buffer as first, and nil as second argument.
5414 Alternatively, an element of this list can be specified as
5415 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
5416 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5417 `special-display-popup-frame' will interpret such pairs as frame
5418 parameters when it creates a special frame, overriding the
5419 corresponding values from `special-display-frame-alist'.
5421 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5422 `special-display-popup-frame' displays that buffer in the
5423 selected window. If FRAME-PARAMETERS contains (same-frame . t),
5424 it displays that buffer in a window on the selected frame.
5426 If `special-display-function' specifies some other function than
5427 `special-display-popup-frame', that function is called with the
5428 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5431 Finally, an element of this list can be also specified as
5432 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5433 `special-display-popup-frame' will call FUNCTION with the buffer
5434 named BUFFER-NAME as first argument, and OTHER-ARGS as the
5437 Any alternative function specified here is responsible for
5438 setting up the quit-restore parameter of the window used.
5440 If this variable appears \"not to work\", because you added a
5441 name to it but the corresponding buffer is displayed in the
5442 selected window, look at the values of `same-window-buffer-names'
5443 and `same-window-regexps'. Those variables take precedence over
5446 See also `special-display-regexps'."
5448 (choice :tag
"Buffer"
5450 (string :format
"%v")
5451 (cons :tag
"With parameters"
5454 (string :format
"%v")
5455 (repeat :tag
"Parameters"
5457 (symbol :tag
"Parameter")
5458 (sexp :tag
"Value"))))
5459 (list :tag
"With function"
5462 (string :format
"%v")
5463 (function :tag
"Function")
5464 (repeat :tag
"Arguments" (sexp)))))
5467 (make-obsolete-variable 'special-display-buffer-names
'display-buffer-alist
"24.3")
5468 (put 'special-display-buffer-names
'risky-local-variable t
)
5470 (defcustom special-display-regexps nil
5471 "List of regexps saying which buffers should be displayed specially.
5472 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5473 any regexp in this list matches its name, displays it specially
5474 using `special-display-function'. `special-display-popup-frame'
5475 \(the default for `special-display-function') usually displays
5476 the buffer in a separate frame made with the parameters specified
5477 by `special-display-frame-alist'. If `special-display-function'
5478 has been set to some other function, that function is called with
5479 the buffer as first, and nil as second argument.
5481 Alternatively, an element of this list can be specified as
5482 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
5483 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5484 `special-display-popup-frame' will then interpret these pairs as
5485 frame parameters when creating a special frame for a buffer whose
5486 name matches REGEXP, overriding the corresponding values from
5487 `special-display-frame-alist'.
5489 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5490 `special-display-popup-frame' displays buffers matching REGEXP in
5491 the selected window. (same-frame . t) in FRAME-PARAMETERS means
5492 to display such buffers in a window on the selected frame.
5494 If `special-display-function' specifies some other function than
5495 `special-display-popup-frame', that function is called with the
5496 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
5499 Finally, an element of this list can be also specified as
5500 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
5501 will then call FUNCTION with the buffer whose name matched
5502 REGEXP as first, and OTHER-ARGS as second argument.
5504 Any alternative function specified here is responsible for
5505 setting up the quit-restore parameter of the window used.
5507 If this variable appears \"not to work\", because you added a
5508 name to it but the corresponding buffer is displayed in the
5509 selected window, look at the values of `same-window-buffer-names'
5510 and `same-window-regexps'. Those variables take precedence over
5513 See also `special-display-buffer-names'."
5515 (choice :tag
"Buffer"
5517 (regexp :format
"%v")
5518 (cons :tag
"With parameters"
5521 (regexp :format
"%v")
5522 (repeat :tag
"Parameters"
5524 (symbol :tag
"Parameter")
5525 (sexp :tag
"Value"))))
5526 (list :tag
"With function"
5529 (regexp :format
"%v")
5530 (function :tag
"Function")
5531 (repeat :tag
"Arguments" (sexp)))))
5534 (make-obsolete-variable 'special-display-regexps
'display-buffer-alist
"24.3")
5535 (put 'special-display-regexps
'risky-local-variable t
)
5537 (defun special-display-p (buffer-name)
5538 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
5539 More precisely, return t if `special-display-buffer-names' or
5540 `special-display-regexps' contain a string entry equaling or
5541 matching BUFFER-NAME. If `special-display-buffer-names' or
5542 `special-display-regexps' contain a list entry whose car equals
5543 or matches BUFFER-NAME, the return value is the cdr of that
5547 ((member buffer-name special-display-buffer-names
)
5549 ((setq tmp
(assoc buffer-name special-display-buffer-names
))
5552 (dolist (regexp special-display-regexps
)
5555 (when (string-match-p regexp buffer-name
)
5557 ((and (consp regexp
) (stringp (car regexp
))
5558 (string-match-p (car regexp
) buffer-name
))
5559 (throw 'found
(cdr regexp
))))))))))
5561 (defcustom special-display-frame-alist
5562 '((height .
14) (width .
80) (unsplittable . t
))
5563 "Alist of parameters for special frames.
5564 Special frames are used for buffers whose names are listed in
5565 `special-display-buffer-names' and for buffers whose names match
5566 one of the regular expressions in `special-display-regexps'.
5568 This variable can be set in your init file, like this:
5570 (setq special-display-frame-alist '((width . 80) (height . 20)))
5572 These supersede the values given in `default-frame-alist'."
5573 :type
'(repeat (cons :format
"%v"
5574 (symbol :tag
"Parameter")
5575 (sexp :tag
"Value")))
5577 (make-obsolete-variable 'special-display-frame-alist
'display-buffer-alist
"24.3")
5579 (defun special-display-popup-frame (buffer &optional args
)
5580 "Pop up a frame displaying BUFFER and return its window.
5581 If BUFFER is already displayed in a visible or iconified frame,
5582 raise that frame. Otherwise, display BUFFER in a new frame.
5584 Optional argument ARGS is a list specifying additional
5587 If ARGS is an alist, use it as a list of frame parameters. If
5588 these parameters contain (same-window . t), display BUFFER in
5589 the selected window. If they contain (same-frame . t), display
5590 BUFFER in a window of the selected frame.
5592 If ARGS is a list whose car is a symbol, use (car ARGS) as a
5593 function to do the work. Pass it BUFFER as first argument, and
5594 pass the elements of (cdr ARGS) as the remaining arguments."
5595 (if (and args
(symbolp (car args
)))
5596 (apply (car args
) buffer
(cdr args
))
5597 (let ((window (get-buffer-window buffer
0)))
5599 ;; If we have a window already, make it visible.
5601 (let ((frame (window-frame window
)))
5602 (make-frame-visible frame
)
5604 (display-buffer-record-window 'reuse window buffer
)
5606 ;; Reuse the current window if the user requested it.
5607 (when (cdr (assq 'same-window args
))
5609 (progn (switch-to-buffer buffer nil t
) (selected-window))
5611 ;; Stay on the same frame if requested.
5612 (when (or (cdr (assq 'same-frame args
)) (cdr (assq 'same-window args
)))
5613 (let* ((pop-up-windows t
)
5615 special-display-buffer-names special-display-regexps
)
5616 (display-buffer buffer
)))
5617 ;; If no window yet, make one in a new frame.
5619 (with-current-buffer buffer
5620 (make-frame (append args special-display-frame-alist
))))
5621 (window (frame-selected-window frame
)))
5622 (display-buffer-record-window 'frame window buffer
)
5623 (unless (eq buffer
(window-buffer window
))
5624 (set-window-buffer window buffer
)
5625 (set-window-prev-buffers window nil
))
5626 (set-window-dedicated-p window t
)
5629 (defcustom special-display-function
'special-display-popup-frame
5630 "Function to call for displaying special buffers.
5631 This function is called with two arguments - the buffer and,
5632 optionally, a list - and should return a window displaying that
5633 buffer. The default value usually makes a separate frame for the
5634 buffer using `special-display-frame-alist' to specify the frame
5635 parameters. See the definition of `special-display-popup-frame'
5636 for how to specify such a function.
5638 A buffer is special when its name is either listed in
5639 `special-display-buffer-names' or matches a regexp in
5640 `special-display-regexps'.
5642 The specified function should call `display-buffer-record-window'
5643 with corresponding arguments to set up the quit-restore parameter
5644 of the window used."
5647 (make-obsolete-variable 'special-display-function
'display-buffer-alist
"24.3")
5649 (defcustom same-window-buffer-names nil
5650 "List of names of buffers that should appear in the \"same\" window.
5651 `display-buffer' and `pop-to-buffer' show a buffer whose name is
5652 on this list in the selected rather than some other window.
5654 An element of this list can be a cons cell instead of just a
5655 string. In that case, the cell's car must be a string specifying
5656 the buffer name. This is for compatibility with
5657 `special-display-buffer-names'; the cdr of the cons cell is
5660 See also `same-window-regexps'."
5661 :type
'(repeat (string :format
"%v"))
5664 (defcustom same-window-regexps nil
5665 "List of regexps saying which buffers should appear in the \"same\" window.
5666 `display-buffer' and `pop-to-buffer' show a buffer whose name
5667 matches a regexp on this list in the selected rather than some
5670 An element of this list can be a cons cell instead of just a
5671 string. In that case, the cell's car must be a regexp matching
5672 the buffer name. This is for compatibility with
5673 `special-display-regexps'; the cdr of the cons cell is ignored.
5675 See also `same-window-buffer-names'."
5676 :type
'(repeat (regexp :format
"%v"))
5679 (defun same-window-p (buffer-name)
5680 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5681 This function returns non-nil if `display-buffer' or
5682 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
5683 selected rather than (as usual) some other window. See
5684 `same-window-buffer-names' and `same-window-regexps'."
5686 ((not (stringp buffer-name
)))
5687 ;; The elements of `same-window-buffer-names' can be buffer
5688 ;; names or cons cells whose cars are buffer names.
5689 ((member buffer-name same-window-buffer-names
))
5690 ((assoc buffer-name same-window-buffer-names
))
5692 (dolist (regexp same-window-regexps
)
5693 ;; The elements of `same-window-regexps' can be regexps
5694 ;; or cons cells whose cars are regexps.
5695 (when (or (and (stringp regexp
)
5696 (string-match-p regexp buffer-name
))
5697 (and (consp regexp
) (stringp (car regexp
))
5698 (string-match-p (car regexp
) buffer-name
)))
5699 (throw 'found t
)))))))
5701 (defcustom pop-up-frames nil
5702 "Whether `display-buffer' should make a separate frame.
5703 If nil, never make a separate frame.
5704 If the value is `graphic-only', make a separate frame
5705 on graphic displays only.
5706 Any other non-nil value means always make a separate frame."
5708 (const :tag
"Never" nil
)
5709 (const :tag
"On graphic displays only" graphic-only
)
5710 (const :tag
"Always" t
))
5713 (defcustom display-buffer-reuse-frames nil
5714 "Non-nil means `display-buffer' should reuse frames.
5715 If the buffer in question is already displayed in a frame, raise
5721 (make-obsolete-variable
5722 'display-buffer-reuse-frames
5723 "use a `reusable-frames' alist entry in `display-buffer-alist'."
5726 (defcustom pop-up-windows t
5727 "Non-nil means `display-buffer' should make a new window."
5731 (defcustom split-window-preferred-function
'split-window-sensibly
5732 "Function called by `display-buffer' routines to split a window.
5733 This function is called with a window as single argument and is
5734 supposed to split that window and return the new window. If the
5735 window can (or shall) not be split, it is supposed to return nil.
5736 The default is to call the function `split-window-sensibly' which
5737 tries to split the window in a way which seems most suitable.
5738 You can customize the options `split-height-threshold' and/or
5739 `split-width-threshold' in order to have `split-window-sensibly'
5740 prefer either vertical or horizontal splitting.
5742 If you set this to any other function, bear in mind that the
5743 `display-buffer' routines may call this function two times. The
5744 argument of the first call is the largest window on its frame.
5745 If that call fails to return a live window, the function is
5746 called again with the least recently used window as argument. If
5747 that call fails too, `display-buffer' will use an existing window
5748 to display its buffer.
5750 The window selected at the time `display-buffer' was invoked is
5751 still selected when this function is called. Hence you can
5752 compare the window argument with the value of `selected-window'
5753 if you intend to split the selected window instead or if you do
5754 not want to split the selected window."
5759 (defcustom split-height-threshold
80
5760 "Minimum height for splitting windows sensibly.
5761 If this is an integer, `split-window-sensibly' may split a window
5762 vertically only if it has at least this many lines. If this is
5763 nil, `split-window-sensibly' is not allowed to split a window
5764 vertically. If, however, a window is the only window on its
5765 frame, `split-window-sensibly' may split it vertically
5766 disregarding the value of this variable."
5767 :type
'(choice (const nil
) (integer :tag
"lines"))
5771 (defcustom split-width-threshold
160
5772 "Minimum width for splitting windows sensibly.
5773 If this is an integer, `split-window-sensibly' may split a window
5774 horizontally only if it has at least this many columns. If this
5775 is nil, `split-window-sensibly' is not allowed to split a window
5777 :type
'(choice (const nil
) (integer :tag
"columns"))
5781 (defun window-splittable-p (window &optional horizontal
)
5782 "Return non-nil if `split-window-sensibly' may split WINDOW.
5783 Optional argument HORIZONTAL nil or omitted means check whether
5784 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
5785 non-nil means check whether WINDOW may be split horizontally.
5787 WINDOW may be split vertically when the following conditions
5789 - `window-size-fixed' is either nil or equals `width' for the
5791 - `split-height-threshold' is an integer and WINDOW is at least as
5792 high as `split-height-threshold'.
5793 - When WINDOW is split evenly, the emanating windows are at least
5794 `window-min-height' lines tall and can accommodate at least one
5795 line plus - if WINDOW has one - a mode line.
5797 WINDOW may be split horizontally when the following conditions
5799 - `window-size-fixed' is either nil or equals `height' for the
5801 - `split-width-threshold' is an integer and WINDOW is at least as
5802 wide as `split-width-threshold'.
5803 - When WINDOW is split evenly, the emanating windows are at least
5804 `window-min-width' or two (whichever is larger) columns wide."
5805 (when (and (window-live-p window
) (not (window--side-window-p window
)))
5806 (with-current-buffer (window-buffer window
)
5808 ;; A window can be split horizontally when its width is not
5809 ;; fixed, it is at least `split-width-threshold' columns wide
5810 ;; and at least twice as wide as `window-min-width' and 2 (the
5811 ;; latter value is hardcoded).
5812 (and (memq window-size-fixed
'(nil height
))
5813 ;; Testing `window-full-width-p' here hardly makes any
5814 ;; sense nowadays. This can be done more intuitively by
5815 ;; setting up `split-width-threshold' appropriately.
5816 (numberp split-width-threshold
)
5817 (>= (window-width window
)
5818 (max split-width-threshold
5819 (* 2 (max window-min-width
2)))))
5820 ;; A window can be split vertically when its height is not
5821 ;; fixed, it is at least `split-height-threshold' lines high,
5822 ;; and it is at least twice as high as `window-min-height' and 2
5823 ;; if it has a mode line or 1.
5824 (and (memq window-size-fixed
'(nil width
))
5825 (numberp split-height-threshold
)
5826 (>= (window-height window
)
5827 (max split-height-threshold
5828 (* 2 (max window-min-height
5829 (if mode-line-format
2 1))))))))))
5831 (defun split-window-sensibly (&optional window
)
5832 "Split WINDOW in a way suitable for `display-buffer'.
5833 WINDOW defaults to the currently selected window.
5834 If `split-height-threshold' specifies an integer, WINDOW is at
5835 least `split-height-threshold' lines tall and can be split
5836 vertically, split WINDOW into two windows one above the other and
5837 return the lower window. Otherwise, if `split-width-threshold'
5838 specifies an integer, WINDOW is at least `split-width-threshold'
5839 columns wide and can be split horizontally, split WINDOW into two
5840 windows side by side and return the window on the right. If this
5841 can't be done either and WINDOW is the only window on its frame,
5842 try to split WINDOW vertically disregarding any value specified
5843 by `split-height-threshold'. If that succeeds, return the lower
5844 window. Return nil otherwise.
5846 By default `display-buffer' routines call this function to split
5847 the largest or least recently used window. To change the default
5848 customize the option `split-window-preferred-function'.
5850 You can enforce this function to not split WINDOW horizontally,
5851 by setting (or binding) the variable `split-width-threshold' to
5852 nil. If, in addition, you set `split-height-threshold' to zero,
5853 chances increase that this function does split WINDOW vertically.
5855 In order to not split WINDOW vertically, set (or bind) the
5856 variable `split-height-threshold' to nil. Additionally, you can
5857 set `split-width-threshold' to zero to make a horizontal split
5858 more likely to occur.
5860 Have a look at the function `window-splittable-p' if you want to
5861 know how `split-window-sensibly' determines whether WINDOW can be
5863 (let ((window (or window
(selected-window))))
5864 (or (and (window-splittable-p window
)
5865 ;; Split window vertically.
5866 (with-selected-window window
5867 (split-window-below)))
5868 (and (window-splittable-p window t
)
5869 ;; Split window horizontally.
5870 (with-selected-window window
5871 (split-window-right)))
5872 (and (eq window
(frame-root-window (window-frame window
)))
5873 (not (window-minibuffer-p window
))
5874 ;; If WINDOW is the only window on its frame and is not the
5875 ;; minibuffer window, try to split it vertically disregarding
5876 ;; the value of `split-height-threshold'.
5877 (let ((split-height-threshold 0))
5878 (when (window-splittable-p window
)
5879 (with-selected-window window
5880 (split-window-below))))))))
5882 (defun window--try-to-split-window (window &optional alist
)
5883 "Try to split WINDOW.
5884 Return value returned by `split-window-preferred-function' if it
5885 represents a live window, nil otherwise."
5886 (and (window-live-p window
)
5887 (not (frame-parameter (window-frame window
) 'unsplittable
))
5888 (let* ((window-combination-limit
5889 ;; When `window-combination-limit' equals
5890 ;; `display-buffer' or equals `resize-window' and a
5891 ;; `window-height' or `window-width' alist entry are
5892 ;; present, bind it to t so resizing steals space
5893 ;; preferably from the window that was split.
5894 (if (or (eq window-combination-limit
'display-buffer
)
5895 (and (eq window-combination-limit
'window-size
)
5896 (or (cdr (assq 'window-height alist
))
5897 (cdr (assq 'window-width alist
)))))
5899 window-combination-limit
))
5901 ;; Since `split-window-preferred-function' might
5902 ;; throw an error use `condition-case'.
5904 (funcall split-window-preferred-function window
)
5906 (and (window-live-p new-window
) new-window
))))
5908 (defun window--frame-usable-p (frame)
5909 "Return FRAME if it can be used to display a buffer."
5910 (when (frame-live-p frame
)
5911 (let ((window (frame-root-window frame
)))
5912 ;; `frame-root-window' may be an internal window which is considered
5913 ;; "dead" by `window-live-p'. Hence if `window' is not live we
5914 ;; implicitly know that `frame' has a visible window we can use.
5915 (unless (and (window-live-p window
)
5916 (or (window-minibuffer-p window
)
5917 ;; If the window is soft-dedicated, the frame is usable.
5918 ;; Actually, even if the window is really dedicated,
5919 ;; the frame is still usable by splitting it.
5920 ;; At least Emacs-22 allowed it, and it is desirable
5921 ;; when displaying same-frame windows.
5922 nil
; (eq t (window-dedicated-p window))
5926 (defcustom even-window-heights t
5927 "If non-nil `display-buffer' will try to even window heights.
5928 Otherwise `display-buffer' will leave the window configuration
5929 alone. Heights are evened only when `display-buffer' chooses a
5930 window that appears above or below the selected window."
5934 (defun window--even-window-heights (window)
5935 "Even heights of WINDOW and selected window.
5936 Do this only if these windows are vertically adjacent to each
5937 other, `even-window-heights' is non-nil, and the selected window
5938 is higher than WINDOW."
5939 (when (and even-window-heights
5940 ;; Even iff WINDOW forms a vertical combination with the
5941 ;; selected window, and WINDOW's height exceeds that of the
5942 ;; selected window, see also bug#11880.
5943 (window-combined-p window
)
5944 (= (window-child-count (window-parent window
)) 2)
5945 (eq (window-parent) (window-parent window
))
5946 (> (window-total-height) (window-total-height window
)))
5947 ;; Don't throw an error if we can't even window heights for
5951 (/ (- (window-total-height window
) (window-total-height)) 2))
5954 (defun window--display-buffer (buffer window type
&optional alist dedicated
)
5955 "Display BUFFER in WINDOW.
5956 TYPE must be one of the symbols `reuse', `window' or `frame' and
5957 is passed unaltered to `display-buffer-record-window'. ALIST is
5958 the alist argument of `display-buffer'. Set `window-dedicated-p'
5959 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
5961 (when (and (buffer-live-p buffer
) (window-live-p window
))
5962 (display-buffer-record-window type window buffer
)
5963 (unless (eq buffer
(window-buffer window
))
5964 (set-window-dedicated-p window nil
)
5965 (set-window-buffer window buffer
)
5967 (set-window-dedicated-p window dedicated
))
5968 (when (memq type
'(window frame
))
5969 (set-window-prev-buffers window nil
)))
5970 (let ((parameter (window-parameter window
'quit-restore
))
5971 (height (cdr (assq 'window-height alist
)))
5972 (width (cdr (assq 'window-width alist
)))
5973 (size (cdr (assq 'window-size alist
))))
5975 ((or (eq type
'frame
)
5976 (and (eq (car parameter
) 'same
)
5977 (eq (nth 1 parameter
) 'frame
)))
5978 ;; Adjust size of frame if asked for.
5982 (let ((width (car size
))
5984 (frame (window-frame window
)))
5985 (when (and (numberp width
) (numberp height
))
5987 frame
(+ (frame-height frame
)
5988 (- height
(window-total-height window
))))
5990 frame
(+ (frame-width frame
)
5991 (- width
(window-total-width window
)))))))
5993 (ignore-errors (funcall size window
)))))
5994 ((or (eq type
'window
)
5995 (and (eq (car parameter
) 'same
)
5996 (eq (nth 1 parameter
) 'window
)))
5997 ;; Adjust height of window if asked for.
6002 (if (integerp height
)
6005 (* (window-total-height (frame-root-window window
))
6007 (delta (- new-height
(window-total-height window
))))
6008 (when (and (window--resizable-p window delta nil
'safe
)
6009 (window-combined-p window
))
6010 (window-resize window delta nil
'safe
))))
6012 (ignore-errors (funcall height window
))))
6013 ;; Adjust width of window if asked for.
6018 (if (integerp width
)
6021 (* (window-total-width (frame-root-window window
))
6023 (delta (- new-width
(window-total-width window
))))
6024 (when (and (window--resizable-p window delta t
'safe
)
6025 (window-combined-p window t
))
6026 (window-resize window delta t
'safe
))))
6028 (ignore-errors (funcall width window
)))))))
6032 (defun window--maybe-raise-frame (frame)
6033 (let ((visible (frame-visible-p frame
)))
6034 (unless (or (not visible
)
6035 ;; Assume the selected frame is already visible enough.
6036 (eq frame
(selected-frame))
6037 ;; Assume the frame from which we invoked the
6038 ;; minibuffer is visible.
6039 (and (minibuffer-window-active-p (selected-window))
6040 (eq frame
(window-frame (minibuffer-selected-window)))))
6041 (raise-frame frame
))))
6043 ;; FIXME: Not implemented.
6044 ;; FIXME: By the way, there could be more levels of dedication:
6045 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
6046 ;; the window hasn't been used for something else yet.
6047 ;; - `soft' (`softly') dedicated only allows reuse when asked explicitly.
6048 ;; - `strongly' never allows reuse.
6049 (defvar display-buffer-mark-dedicated nil
6050 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
6051 The actual non-nil value of this variable will be copied to the
6052 `window-dedicated-p' flag.")
6054 (defconst display-buffer--action-function-custom-type
6055 '(choice :tag
"Function"
6056 (const :tag
"--" ignore
) ; default for insertion
6057 (const display-buffer-reuse-window
)
6058 (const display-buffer-pop-up-window
)
6059 (const display-buffer-same-window
)
6060 (const display-buffer-pop-up-frame
)
6061 (const display-buffer-below-selected
)
6062 (const display-buffer-at-bottom
)
6063 (const display-buffer-in-previous-window
)
6064 (const display-buffer-use-some-window
)
6065 (function :tag
"Other function"))
6066 "Custom type for `display-buffer' action functions.")
6068 (defconst display-buffer--action-custom-type
6069 `(cons :tag
"Action"
6070 (choice :tag
"Action functions"
6071 ,display-buffer--action-function-custom-type
6073 :tag
"List of functions"
6074 ,display-buffer--action-function-custom-type
))
6075 (alist :tag
"Action arguments"
6077 :value-type
(sexp :tag
"Value")))
6078 "Custom type for `display-buffer' actions.")
6080 (defvar display-buffer-overriding-action
'(nil . nil
)
6081 "Overriding action to perform to display a buffer.
6082 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6083 function or a list of functions. Each function should accept two
6084 arguments: a buffer to display and an alist similar to ALIST.
6085 See `display-buffer' for details.")
6086 (put 'display-buffer-overriding-action
'risky-local-variable t
)
6088 (defcustom display-buffer-alist nil
6089 "Alist of conditional actions for `display-buffer'.
6090 This is a list of elements (CONDITION . ACTION), where:
6092 CONDITION is either a regexp matching buffer names, or a
6093 function that takes two arguments - a buffer name and the
6094 ACTION argument of `display-buffer' - and returns a boolean.
6096 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
6097 function or a list of functions. Each such function should
6098 accept two arguments: a buffer to display and an alist of the
6099 same form as ALIST. See `display-buffer' for details.
6101 `display-buffer' scans this alist until it either finds a
6102 matching regular expression or the function specified by a
6103 condition returns non-nil. In any of these cases, it adds the
6104 associated action to the list of actions it will try."
6105 :type
`(alist :key-type
6106 (choice :tag
"Condition"
6108 (function :tag
"Matcher function"))
6109 :value-type
,display-buffer--action-custom-type
)
6114 (defcustom display-buffer-base-action
'(nil . nil
)
6115 "User-specified default action for `display-buffer'.
6116 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6117 function or a list of functions. Each function should accept two
6118 arguments: a buffer to display and an alist similar to ALIST.
6119 See `display-buffer' for details."
6120 :type display-buffer--action-custom-type
6125 (defconst display-buffer-fallback-action
6126 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
6127 display-buffer-reuse-window
6128 display-buffer--maybe-pop-up-frame-or-window
6129 display-buffer-in-previous-window
6130 display-buffer-use-some-window
6131 ;; If all else fails, pop up a new frame.
6132 display-buffer-pop-up-frame
))
6133 "Default fallback action for `display-buffer'.
6134 This is the action used by `display-buffer' if no other actions
6135 specified, e.g. by the user options `display-buffer-alist' or
6136 `display-buffer-base-action'. See `display-buffer'.")
6137 (put 'display-buffer-fallback-action
'risky-local-variable t
)
6139 (defun display-buffer-assq-regexp (buffer-name alist action
)
6140 "Retrieve ALIST entry corresponding to BUFFER-NAME.
6141 ACTION is the action argument passed to `display-buffer'."
6143 (dolist (entry alist
)
6144 (let ((key (car entry
)))
6145 (when (or (and (stringp key
)
6146 (string-match-p key buffer-name
))
6147 (and (functionp key
)
6148 (funcall key buffer-name action
)))
6149 (throw 'match
(cdr entry
)))))))
6151 (defvar display-buffer--same-window-action
6152 '(display-buffer-same-window
6153 (inhibit-same-window . nil
))
6154 "A `display-buffer' action for displaying in the same window.")
6155 (put 'display-buffer--same-window-action
'risky-local-variable t
)
6157 (defvar display-buffer--other-frame-action
6158 '((display-buffer-reuse-window
6159 display-buffer-pop-up-frame
)
6160 (reusable-frames .
0)
6161 (inhibit-same-window . t
))
6162 "A `display-buffer' action for displaying in another frame.")
6163 (put 'display-buffer--other-frame-action
'risky-local-variable t
)
6165 (defun display-buffer (buffer-or-name &optional action frame
)
6166 "Display BUFFER-OR-NAME in some window, without selecting it.
6167 BUFFER-OR-NAME must be a buffer or the name of an existing
6168 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
6169 or nil if no such window is found.
6171 Optional argument ACTION, if non-nil, should specify a display
6172 action. Its form is described below.
6174 Optional argument FRAME, if non-nil, acts like an additional
6175 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
6176 specifying the frame(s) to search for a window that is already
6177 displaying the buffer. See `display-buffer-reuse-window'
6179 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
6180 where FUNCTION is either a function or a list of functions, and
6181 ALIST is an arbitrary association list (alist).
6183 Each such FUNCTION should accept two arguments: the buffer to
6184 display and an alist. Based on those arguments, it should
6185 display the buffer and return the window. If the caller is
6186 prepared to handle the case of not displaying the buffer
6187 and returning nil from `display-buffer' it should pass
6188 \(allow-no-window . t) as an element of the ALIST.
6190 The `display-buffer' function builds a function list and an alist
6191 by combining the functions and alists specified in
6192 `display-buffer-overriding-action', `display-buffer-alist', the
6193 ACTION argument, `display-buffer-base-action', and
6194 `display-buffer-fallback-action' (in order). Then it calls each
6195 function in the combined function list in turn, passing the
6196 buffer as the first argument and the combined alist as the second
6197 argument, until one of the functions returns non-nil.
6199 If ACTION is nil, the function list and the alist are built using
6200 only the other variables mentioned above.
6202 Available action functions include:
6203 `display-buffer-same-window'
6204 `display-buffer-reuse-window'
6205 `display-buffer-pop-up-frame'
6206 `display-buffer-pop-up-window'
6207 `display-buffer-in-previous-window'
6208 `display-buffer-use-some-window'
6210 Recognized alist entries include:
6212 `inhibit-same-window' -- A non-nil value prevents the same
6213 window from being used for display.
6215 `inhibit-switch-frame' -- A non-nil value prevents any other
6216 frame from being raised or selected,
6217 even if the window is displayed there.
6219 `reusable-frames' -- Value specifies frame(s) to search for a
6220 window that already displays the buffer.
6221 See `display-buffer-reuse-window'.
6223 `pop-up-frame-parameters' -- Value specifies an alist of frame
6224 parameters to give a new frame, if
6227 `window-height' -- Value specifies either an integer (the number
6228 of lines of a new window), a floating point number (the
6229 fraction of a new window with respect to the height of the
6230 frame's root window) or a function to be called with one
6231 argument - a new window. The function is supposed to adjust
6232 the height of the window; its return value is ignored.
6233 Suitable functions are `shrink-window-if-larger-than-buffer'
6234 and `fit-window-to-buffer'.
6236 `window-width' -- Value specifies either an integer (the number
6237 of columns of a new window), a floating point number (the
6238 fraction of a new window with respect to the width of the
6239 frame's root window) or a function to be called with one
6240 argument - a new window. The function is supposed to adjust
6241 the width of the window; its return value is ignored.
6243 `allow-no-window' -- A non-nil value indicates readiness for the case
6244 of not displaying the buffer and FUNCTION can safely return
6245 a non-window value to suppress displaying.
6247 The ACTION argument to `display-buffer' can also have a non-nil
6248 and non-list value. This means to display the buffer in a window
6249 other than the selected one, even if it is already displayed in
6250 the selected window. If called interactively with a prefix
6251 argument, ACTION is t."
6252 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6253 (if current-prefix-arg t
)))
6254 (let ((buffer (if (bufferp buffer-or-name
)
6256 (get-buffer buffer-or-name
)))
6257 ;; Make sure that when we split windows the old window keeps
6258 ;; point, bug#14829.
6259 (split-window-keep-point t
)
6260 ;; Handle the old form of the first argument.
6261 (inhibit-same-window (and action
(not (listp action
)))))
6262 (unless (listp action
) (setq action nil
))
6263 (if display-buffer-function
6264 ;; If `display-buffer-function' is defined, let it do the job.
6265 (funcall display-buffer-function buffer inhibit-same-window
)
6266 ;; Otherwise, use the defined actions.
6268 (display-buffer-assq-regexp
6269 (buffer-name buffer
) display-buffer-alist action
))
6270 (special-action (display-buffer--special-action buffer
))
6271 ;; Extra actions from the arguments to this function:
6273 (cons nil
(append (if inhibit-same-window
6274 '((inhibit-same-window . t
)))
6276 `((reusable-frames .
,frame
))))))
6277 ;; Construct action function list and action alist.
6278 (actions (list display-buffer-overriding-action
6279 user-action special-action action extra-action
6280 display-buffer-base-action
6281 display-buffer-fallback-action
))
6282 (functions (apply 'append
6285 (if (functionp x
) (list x
) x
))
6287 (alist (apply 'append
(mapcar 'cdr actions
)))
6289 (unless (buffer-live-p buffer
)
6290 (error "Invalid buffer"))
6291 (while (and functions
(not window
))
6292 (setq window
(funcall (car functions
) buffer alist
)
6293 functions
(cdr functions
)))
6294 (and (windowp window
) window
)))))
6296 (defun display-buffer-other-frame (buffer)
6297 "Display buffer BUFFER preferably in another frame.
6298 This uses the function `display-buffer' as a subroutine; see
6299 its documentation for additional customization information."
6300 (interactive "BDisplay buffer in other frame: ")
6301 (display-buffer buffer display-buffer--other-frame-action t
))
6303 ;;; `display-buffer' action functions:
6305 (defun display-buffer-same-window (buffer alist
)
6306 "Display BUFFER in the selected window.
6307 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
6308 if the selected window is a minibuffer window or is dedicated to
6309 another buffer; in that case, return nil. Otherwise, return the
6311 (unless (or (cdr (assq 'inhibit-same-window alist
))
6312 (window-minibuffer-p)
6313 (window-dedicated-p))
6314 (window--display-buffer buffer
(selected-window) 'reuse alist
)))
6316 (defun display-buffer--maybe-same-window (buffer alist
)
6317 "Conditionally display BUFFER in the selected window.
6318 If `same-window-p' returns non-nil for BUFFER's name, call
6319 `display-buffer-same-window' and return its value. Otherwise,
6321 (and (same-window-p (buffer-name buffer
))
6322 (display-buffer-same-window buffer alist
)))
6324 (defun display-buffer-reuse-window (buffer alist
)
6325 "Return a window that is already displaying BUFFER.
6326 Return nil if no usable window is found.
6328 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6329 window is not eligible for reuse.
6331 If ALIST contains a `reusable-frames' entry, its value determines
6332 which frames to search for a reusable window:
6333 nil -- the selected frame (actually the last non-minibuffer frame)
6334 A frame -- just that frame
6335 `visible' -- all visible frames
6336 0 -- all frames on the current terminal
6339 If ALIST contains no `reusable-frames' entry, search just the
6340 selected frame if `display-buffer-reuse-frames' and
6341 `pop-up-frames' are both nil; search all frames on the current
6342 terminal if either of those variables is non-nil.
6344 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6345 event that a window on another frame is chosen, avoid raising
6347 (let* ((alist-entry (assq 'reusable-frames alist
))
6348 (frames (cond (alist-entry (cdr alist-entry
))
6349 ((if (eq pop-up-frames
'graphic-only
)
6353 (display-buffer-reuse-frames 0)
6354 (t (last-nonminibuffer-frame))))
6355 (window (if (and (eq buffer
(window-buffer))
6356 (not (cdr (assq 'inhibit-same-window alist
))))
6358 (car (delq (selected-window)
6359 (get-buffer-window-list buffer
'nomini
6361 (when (window-live-p window
)
6362 (prog1 (window--display-buffer buffer window
'reuse alist
)
6363 (unless (cdr (assq 'inhibit-switch-frame alist
))
6364 (window--maybe-raise-frame (window-frame window
)))))))
6366 (defun display-buffer--special-action (buffer)
6367 "Return special display action for BUFFER, if any.
6368 If `special-display-p' returns non-nil for BUFFER, return an
6369 appropriate display action involving `special-display-function'.
6370 See `display-buffer' for the format of display actions."
6371 (and special-display-function
6372 ;; `special-display-p' returns either t or a list of frame
6373 ;; parameters to pass to `special-display-function'.
6374 (let ((pars (special-display-p (buffer-name buffer
))))
6376 (list (list #'display-buffer-reuse-window
6377 `(lambda (buffer _alist
)
6378 (funcall special-display-function
6379 buffer
',(if (listp pars
) pars
)))))))))
6381 (defun display-buffer-pop-up-frame (buffer alist
)
6382 "Display BUFFER in a new frame.
6383 This works by calling `pop-up-frame-function'. If successful,
6384 return the window used; otherwise return nil.
6386 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
6387 raising the new frame.
6389 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
6390 corresponding value is an alist of frame parameters to give the
6392 (let* ((params (cdr (assq 'pop-up-frame-parameters alist
)))
6393 (pop-up-frame-alist (append params pop-up-frame-alist
))
6394 (fun pop-up-frame-function
)
6397 ;; Make BUFFER current so `make-frame' will use it as the
6398 ;; new frame's buffer (Bug#15133).
6399 (with-current-buffer buffer
6400 (setq frame
(funcall fun
)))
6401 (setq window
(frame-selected-window frame
)))
6402 (prog1 (window--display-buffer
6403 buffer window
'frame alist display-buffer-mark-dedicated
)
6404 (unless (cdr (assq 'inhibit-switch-frame alist
))
6405 (window--maybe-raise-frame frame
))))))
6407 (defun display-buffer-pop-up-window (buffer alist
)
6408 "Display BUFFER by popping up a new window.
6409 The new window is created on the selected frame, or in
6410 `last-nonminibuffer-frame' if no windows can be created there.
6411 If successful, return the new window; otherwise return nil.
6413 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6414 event that the new window is created on another frame, avoid
6416 (let ((frame (or (window--frame-usable-p (selected-frame))
6417 (window--frame-usable-p (last-nonminibuffer-frame))))
6419 (when (and (or (not (frame-parameter frame
'unsplittable
))
6420 ;; If the selected frame cannot be split, look at
6421 ;; `last-nonminibuffer-frame'.
6422 (and (eq frame
(selected-frame))
6423 (setq frame
(last-nonminibuffer-frame))
6424 (window--frame-usable-p frame
)
6425 (not (frame-parameter frame
'unsplittable
))))
6426 ;; Attempt to split largest or least recently used window.
6427 (setq window
(or (window--try-to-split-window
6428 (get-largest-window frame t
) alist
)
6429 (window--try-to-split-window
6430 (get-lru-window frame t
) alist
))))
6431 (prog1 (window--display-buffer
6432 buffer window
'window alist display-buffer-mark-dedicated
)
6433 (unless (cdr (assq 'inhibit-switch-frame alist
))
6434 (window--maybe-raise-frame (window-frame window
)))))))
6436 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist
)
6437 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
6439 If `pop-up-frames' is non-nil (and not `graphic-only' on a
6440 text-only terminal), try with `display-buffer-pop-up-frame'.
6442 If that cannot be done, and `pop-up-windows' is non-nil, try
6443 again with `display-buffer-pop-up-window'."
6444 (or (and (if (eq pop-up-frames
'graphic-only
)
6447 (display-buffer-pop-up-frame buffer alist
))
6449 (display-buffer-pop-up-window buffer alist
))))
6451 (defun display-buffer-below-selected (buffer alist
)
6452 "Try displaying BUFFER in a window below the selected window.
6453 This either splits the selected window or reuses the window below
6456 (or (and (setq window
(window-in-direction 'below
))
6457 (eq buffer
(window-buffer window
))
6458 (window--display-buffer buffer window
'reuse alist
))
6459 (and (not (frame-parameter nil
'unsplittable
))
6460 (let ((split-height-threshold 0)
6461 split-width-threshold
)
6462 (setq window
(window--try-to-split-window (selected-window) alist
)))
6463 (window--display-buffer
6464 buffer window
'window alist display-buffer-mark-dedicated
))
6465 (and (setq window
(window-in-direction 'below
))
6466 (not (window-dedicated-p window
))
6467 (window--display-buffer
6468 buffer window
'reuse alist display-buffer-mark-dedicated
)))))
6470 (defun display-buffer-at-bottom (buffer alist
)
6471 "Try displaying BUFFER in a window at the bottom of the selected frame.
6472 This either reuses such a window provided it shows BUFFER
6473 already, splits a window at the bottom of the frame or the
6474 frame's root window, or reuses some window at the bottom of the
6476 (let (bottom-window bottom-window-shows-buffer window
)
6480 ((window-in-direction 'below window
))
6481 ((and (not bottom-window-shows-buffer
)
6482 (eq buffer
(window-buffer window
)))
6483 (setq bottom-window-shows-buffer t
)
6484 (setq bottom-window window
))
6485 ((not bottom-window
)
6486 (setq bottom-window window
)))
6488 (or (and bottom-window-shows-buffer
6489 (window--display-buffer
6490 buffer bottom-window
'reuse alist display-buffer-mark-dedicated
))
6491 (and (not (frame-parameter nil
'unsplittable
))
6492 (let (split-width-threshold)
6493 (setq window
(window--try-to-split-window bottom-window alist
)))
6494 (window--display-buffer
6495 buffer window
'window alist display-buffer-mark-dedicated
))
6496 (and (not (frame-parameter nil
'unsplittable
))
6499 (split-window (window--major-non-side-window))
6501 (window--display-buffer
6502 buffer window
'window alist display-buffer-mark-dedicated
))
6503 (and (setq window bottom-window
)
6504 (not (window-dedicated-p window
))
6505 (window--display-buffer
6506 buffer window
'reuse alist display-buffer-mark-dedicated
)))))
6508 (defun display-buffer-in-previous-window (buffer alist
)
6509 "Display BUFFER in a window previously showing it.
6510 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6511 window is not eligible for reuse.
6513 If ALIST contains a `reusable-frames' entry, its value determines
6514 which frames to search for a reusable window:
6515 nil -- the selected frame (actually the last non-minibuffer frame)
6516 A frame -- just that frame
6517 `visible' -- all visible frames
6518 0 -- all frames on the current terminal
6521 If ALIST contains no `reusable-frames' entry, search just the
6522 selected frame if `display-buffer-reuse-frames' and
6523 `pop-up-frames' are both nil; search all frames on the current
6524 terminal if either of those variables is non-nil.
6526 If ALIST has a `previous-window' entry, the window specified by
6527 that entry will override any other window found by the methods
6528 above, even if that window never showed BUFFER before."
6529 (let* ((alist-entry (assq 'reusable-frames alist
))
6530 (inhibit-same-window
6531 (cdr (assq 'inhibit-same-window alist
)))
6533 (alist-entry (cdr alist-entry
))
6534 ((if (eq pop-up-frames
'graphic-only
)
6538 (display-buffer-reuse-frames 0)
6539 (t (last-nonminibuffer-frame))))
6540 best-window second-best-window window
)
6541 ;; Scan windows whether they have shown the buffer recently.
6543 (dolist (window (window-list-1 (frame-first-window) 'nomini frames
))
6544 (when (and (assq buffer
(window-prev-buffers window
))
6545 (not (window-dedicated-p window
)))
6546 (if (eq window
(selected-window))
6547 (unless inhibit-same-window
6548 (setq second-best-window window
))
6549 (setq best-window window
)
6551 ;; When ALIST has a `previous-window' entry, that entry may override
6552 ;; anything we found so far.
6553 (when (and (setq window
(cdr (assq 'previous-window alist
)))
6554 (window-live-p window
)
6555 (not (window-dedicated-p window
)))
6556 (if (eq window
(selected-window))
6557 (unless inhibit-same-window
6558 (setq second-best-window window
))
6559 (setq best-window window
)))
6560 ;; Return best or second best window found.
6561 (when (setq window
(or best-window second-best-window
))
6562 (window--display-buffer buffer window
'reuse alist
))))
6564 (defun display-buffer-use-some-window (buffer alist
)
6565 "Display BUFFER in an existing window.
6566 Search for a usable window, set that window to the buffer, and
6567 return the window. If no suitable window is found, return nil.
6569 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6570 event that a window in another frame is chosen, avoid raising
6572 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist
)))
6573 (frame (or (window--frame-usable-p (selected-frame))
6574 (window--frame-usable-p (last-nonminibuffer-frame))))
6576 ;; Reuse an existing window.
6577 (or (get-lru-window frame nil not-this-window
)
6578 (let ((window (get-buffer-window buffer
'visible
)))
6579 (unless (and not-this-window
6580 (eq window
(selected-window)))
6582 (get-largest-window 'visible nil not-this-window
)
6583 (let ((window (get-buffer-window buffer
0)))
6584 (unless (and not-this-window
6585 (eq window
(selected-window)))
6587 (get-largest-window 0 nil not-this-window
)))
6588 (quit-restore (and (window-live-p window
)
6589 (window-parameter window
'quit-restore
)))
6590 (quad (nth 1 quit-restore
)))
6591 (when (window-live-p window
)
6592 ;; If the window was used by `display-buffer' before, try to
6593 ;; resize it to its old height but don't signal an error.
6594 (when (and (listp quad
)
6595 (integerp (nth 3 quad
))
6596 (> (nth 3 quad
) (window-total-height window
)))
6598 (window-resize window
(- (nth 3 quad
) (window-total-height window
)))
6602 (window--display-buffer buffer window
'reuse alist
)
6603 (window--even-window-heights window
)
6604 (unless (cdr (assq 'inhibit-switch-frame alist
))
6605 (window--maybe-raise-frame (window-frame window
)))))))
6607 (defun display-buffer-no-window (_buffer alist
)
6608 "Display BUFFER in no window.
6609 If ALIST has a non-nil `allow-no-window' entry, then don't display
6610 a window at all. This makes possible to override the default action
6611 and avoid displaying the buffer. It is assumed that when the caller
6612 specifies a non-nil `allow-no-window' then it can handle a nil value
6613 returned from `display-buffer' in this case."
6614 (when (cdr (assq 'allow-no-window alist
))
6617 ;;; Display + selection commands:
6618 (defun pop-to-buffer (buffer &optional action norecord
)
6619 "Select buffer BUFFER in some window, preferably a different one.
6620 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6621 is a string not naming an existent buffer, create a buffer with
6622 that name. If BUFFER is nil, choose some other buffer. Return
6625 This uses `display-buffer' as a subroutine. The optional ACTION
6626 argument is passed to `display-buffer' as its ACTION argument.
6627 See `display-buffer' for more information. ACTION is t if called
6628 interactively with a prefix argument, which means to pop to a
6629 window other than the selected one even if the buffer is already
6630 displayed in the selected window.
6632 If the window to show BUFFER is not on the selected
6633 frame, raise that window's frame and give it input focus.
6635 Optional third arg NORECORD non-nil means do not put this buffer
6636 at the front of the list of recently selected ones."
6637 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
6638 (if current-prefix-arg t
)))
6639 (setq buffer
(window-normalize-buffer-to-switch-to buffer
))
6640 ;; This should be done by `select-window' below.
6641 ;; (set-buffer buffer)
6642 (let* ((old-frame (selected-frame))
6643 (window (display-buffer buffer action
))
6644 (frame (window-frame window
)))
6645 ;; If we chose another frame, make sure it gets input focus.
6646 (unless (eq frame old-frame
)
6647 (select-frame-set-input-focus frame norecord
))
6648 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
6649 (select-window window norecord
)
6652 (defun pop-to-buffer-same-window (buffer &optional norecord
)
6653 "Select buffer BUFFER in some window, preferably the same one.
6654 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6655 is a string not naming an existent buffer, create a buffer with
6656 that name. If BUFFER is nil, choose some other buffer. Return
6659 Optional argument NORECORD, if non-nil means do not put this
6660 buffer at the front of the list of recently selected ones.
6662 Unlike `pop-to-buffer', this function prefers using the selected
6663 window over popping up a new window or frame."
6664 (pop-to-buffer buffer display-buffer--same-window-action norecord
))
6666 (defun read-buffer-to-switch (prompt)
6667 "Read the name of a buffer to switch to, prompting with PROMPT.
6668 Return the name of the buffer as a string.
6670 This function is intended for the `switch-to-buffer' family of
6671 commands since these need to omit the name of the current buffer
6672 from the list of completions and default values."
6673 (let ((rbts-completion-table (internal-complete-buffer-except)))
6674 (minibuffer-with-setup-hook
6676 (setq minibuffer-completion-table rbts-completion-table
)
6677 ;; Since rbts-completion-table is built dynamically, we
6678 ;; can't just add it to the default value of
6679 ;; icomplete-with-completion-tables, so we add it
6681 (if (and (boundp 'icomplete-with-completion-tables
)
6682 (listp icomplete-with-completion-tables
))
6683 (set (make-local-variable 'icomplete-with-completion-tables
)
6684 (cons rbts-completion-table
6685 icomplete-with-completion-tables
))))
6686 (read-buffer prompt
(other-buffer (current-buffer))
6687 (confirm-nonexistent-file-or-buffer)))))
6689 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
6690 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
6691 If BUFFER-OR-NAME is nil, return the buffer returned by
6692 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
6693 exists, return that buffer. If no such buffer exists, create a
6694 buffer with the name BUFFER-OR-NAME and return that buffer."
6696 (or (get-buffer buffer-or-name
)
6697 (let ((buffer (get-buffer-create buffer-or-name
)))
6698 (set-buffer-major-mode buffer
)
6702 (defcustom switch-to-buffer-preserve-window-point nil
6703 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
6704 If this is nil, `switch-to-buffer' displays the buffer at that
6705 buffer's `point'. If this is `already-displayed', it tries to
6706 display the buffer at its previous position in the selected
6707 window, provided the buffer is currently displayed in some other
6708 window on any visible or iconified frame. If this is t, it
6709 unconditionally tries to display the buffer at its previous
6710 position in the selected window.
6712 This variable is ignored if the buffer is already displayed in
6713 the selected window or never appeared in it before, or if
6714 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
6716 (const :tag
"Never" nil
)
6717 (const :tag
"If already displayed elsewhere" already-displayed
)
6718 (const :tag
"Always" t
))
6722 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window
)
6723 "Display buffer BUFFER-OR-NAME in the selected window.
6725 WARNING: This is NOT the way to work on another buffer temporarily
6726 within a Lisp program! Use `set-buffer' instead. That avoids
6727 messing with the window-buffer correspondences.
6729 If the selected window cannot display the specified
6730 buffer (e.g. if it is a minibuffer window or strongly dedicated
6731 to another buffer), call `pop-to-buffer' to select the buffer in
6734 If called interactively, read the buffer name using the
6735 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6736 determines whether to request confirmation before creating a new
6739 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
6740 If BUFFER-OR-NAME is a string that does not identify an existing
6741 buffer, create a buffer with that name. If BUFFER-OR-NAME is
6742 nil, switch to the buffer returned by `other-buffer'.
6744 If optional argument NORECORD is non-nil, do not put the buffer
6745 at the front of the buffer list, and do not make the window
6746 displaying it the most recently selected one.
6748 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
6749 must be displayed in the selected window; if that is impossible,
6750 signal an error rather than calling `pop-to-buffer'.
6752 The option `switch-to-buffer-preserve-window-point' can be used
6753 to make the buffer appear at its last position in the selected
6756 Return the buffer switched to."
6758 (list (read-buffer-to-switch "Switch to buffer: ") nil
'force-same-window
))
6759 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name
)))
6761 ;; Don't call set-window-buffer if it's not needed since it
6762 ;; might signal an error (e.g. if the window is dedicated).
6763 ((eq buffer
(window-buffer)))
6764 ((window-minibuffer-p)
6765 (if force-same-window
6766 (user-error "Cannot switch buffers in minibuffer window")
6767 (pop-to-buffer buffer norecord
)))
6768 ((eq (window-dedicated-p) t
)
6769 (if force-same-window
6770 (user-error "Cannot switch buffers in a dedicated window")
6771 (pop-to-buffer buffer norecord
)))
6773 (let* ((entry (assq buffer
(window-prev-buffers)))
6774 (displayed (and (eq switch-to-buffer-preserve-window-point
6776 (get-buffer-window buffer
0))))
6777 (set-window-buffer nil buffer
)
6779 (or (eq switch-to-buffer-preserve-window-point t
)
6781 ;; Try to restore start and point of buffer in the selected
6782 ;; window (Bug#4041).
6783 (set-window-start (selected-window) (nth 1 entry
) t
)
6784 (set-window-point nil
(nth 2 entry
))))))
6787 (select-window (selected-window)))
6788 (set-buffer buffer
)))
6790 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord
)
6791 "Select the buffer specified by BUFFER-OR-NAME in another window.
6792 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6793 nil. Return the buffer switched to.
6795 If called interactively, prompt for the buffer name using the
6796 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6797 determines whether to request confirmation before creating a new
6800 If BUFFER-OR-NAME is a string and does not identify an existing
6801 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6802 nil, switch to the buffer returned by `other-buffer'.
6804 Optional second argument NORECORD non-nil means do not put this
6805 buffer at the front of the list of recently selected ones.
6807 This uses the function `display-buffer' as a subroutine; see its
6808 documentation for additional customization information."
6810 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
6811 (let ((pop-up-windows t
))
6812 (pop-to-buffer buffer-or-name t norecord
)))
6814 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord
)
6815 "Switch to buffer BUFFER-OR-NAME in another frame.
6816 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6817 nil. Return the buffer switched to.
6819 If called interactively, prompt for the buffer name using the
6820 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6821 determines whether to request confirmation before creating a new
6824 If BUFFER-OR-NAME is a string and does not identify an existing
6825 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6826 nil, switch to the buffer returned by `other-buffer'.
6828 Optional second arg NORECORD non-nil means do not put this
6829 buffer at the front of the list of recently selected ones.
6831 This uses the function `display-buffer' as a subroutine; see its
6832 documentation for additional customization information."
6834 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
6835 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord
))
6837 (defun set-window-text-height (window height
)
6838 "Set the height in lines of the text display area of WINDOW to HEIGHT.
6839 WINDOW must be a live window and defaults to the selected one.
6840 HEIGHT doesn't include the mode line or header line, if any, or
6841 any partial-height lines in the text display area.
6843 Note that the current implementation of this function cannot
6844 always set the height exactly, but attempts to be conservative,
6845 by allocating more lines than are actually needed in the case
6846 where some error may be present."
6847 (setq window
(window-normalize-window window t
))
6848 (let ((delta (- height
(window-text-height window
))))
6849 (unless (zerop delta
)
6850 ;; Setting window-min-height to a value like 1 can lead to very
6851 ;; bizarre displays because it also allows Emacs to make *other*
6852 ;; windows one line tall, which means that there's no more space
6853 ;; for the mode line.
6854 (let ((window-min-height (min 2 height
)))
6855 (window-resize window delta
)))))
6857 (defun enlarge-window-horizontally (delta)
6858 "Make selected window DELTA columns wider.
6859 Interactively, if no argument is given, make selected window one
6862 (enlarge-window delta t
))
6864 (defun shrink-window-horizontally (delta)
6865 "Make selected window DELTA columns narrower.
6866 Interactively, if no argument is given, make selected window one
6869 (shrink-window delta t
))
6871 (defun count-screen-lines (&optional beg end count-final-newline window
)
6872 "Return the number of screen lines in the region.
6873 The number of screen lines may be different from the number of actual lines,
6874 due to line breaking, display table, etc.
6876 Optional arguments BEG and END default to `point-min' and `point-max'
6879 If region ends with a newline, ignore it unless optional third argument
6880 COUNT-FINAL-NEWLINE is non-nil.
6882 The optional fourth argument WINDOW specifies the window used for obtaining
6883 parameters such as width, horizontal scrolling, and so on. The default is
6884 to use the selected window's parameters.
6886 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6887 regardless of which buffer is displayed in WINDOW. This makes possible to use
6888 `count-screen-lines' in any buffer, whether or not it is currently displayed
6891 (setq beg
(point-min)))
6893 (setq end
(point-max)))
6899 (narrow-to-region (min beg end
)
6900 (if (and (not count-final-newline
)
6901 (= ?
\n (char-before (max beg end
))))
6904 (goto-char (point-min))
6905 (1+ (vertical-motion (buffer-size) window
))))))
6907 (defun window-buffer-height (window)
6908 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
6909 WINDOW must be a live window and defaults to the selected one."
6910 (setq window
(window-normalize-window window t
))
6911 (with-current-buffer (window-buffer window
)
6913 (count-screen-lines (point-min) (point-max)
6914 ;; If buffer ends with a newline, ignore it when
6915 ;; counting height unless point is after it.
6919 ;;; Resizing windows and frames to fit their contents exactly.
6920 (defcustom fit-window-to-buffer-horizontally nil
6921 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
6922 If this is nil, `fit-window-to-buffer' never resizes windows
6923 horizontally. If this is `only', it can resize windows
6924 horizontally only. Any other value means `fit-window-to-buffer'
6925 can resize windows in both dimensions."
6930 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
6931 ;; counting title bar and outer borders.
6932 (defcustom fit-frame-to-buffer nil
6933 "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
6934 A frame is fit if and only if its root window is a live window
6935 and this option is non-nil. If this is `horizontally', frames
6936 are resized horizontally only. If this is `vertically', frames
6937 are resized vertically only. Any other non-nil value means
6938 frames can be resized in both dimensions."
6943 (defcustom fit-frame-to-buffer-margins
'(nil nil nil nil
)
6944 "Margins around frame for `fit-frame-to-buffer'.
6945 This specifies the numbers of pixels to be left free on the left,
6946 above, on the right, and below a frame fitted to its buffer. Set
6947 this to avoid obscuring other desktop objects like the taskbar.
6948 The default is nil for each side, which means to not add margins.
6950 The value specified here can be overridden for a specific frame
6951 by that frame's `fit-frame-to-buffer-margins' parameter, if
6952 present. See also `fit-frame-to-buffer-sizes'."
6958 :format
"%[LeftMargin%] %v "
6959 (const :tag
"None" :format
"%t" nil
)
6960 (integer :tag
"Pixels" :size
5))
6964 :format
"%[TopMargin%] %v "
6965 (const :tag
"None" :format
"%t" nil
)
6966 (integer :tag
"Pixels" :size
5))
6970 :format
"%[RightMargin%] %v "
6971 (const :tag
"None" :format
"%t" nil
)
6972 (integer :tag
"Pixels" :size
5))
6976 :format
"%[BottomMargin%] %v "
6977 (const :tag
"None" :format
"%t" nil
)
6978 (integer :tag
"Pixels" :size
5)))
6981 (defcustom fit-frame-to-buffer-sizes
'(nil nil nil nil
)
6982 "Size boundaries of frame for `fit-frame-to-buffer'.
6983 This list specifies the total maximum and minimum lines and
6984 maximum and minimum columns of the root window of any frame that
6985 shall be fit to its buffer. If any of these values is non-nil,
6986 it overrides the corresponding argument of `fit-frame-to-buffer'.
6988 On window systems where the menubar can wrap, fitting a frame to
6989 its buffer may swallow the last line(s). Specifying an
6990 appropriate minimum width value here can avoid such wrapping.
6992 See also `fit-frame-to-buffer-margins'."
6996 :tag
"Maximum Height"
6998 :format
"%[MaxHeight%] %v "
6999 (const :tag
"None" :format
"%t" nil
)
7000 (integer :tag
"Lines" :size
5))
7002 :tag
"Minimum Height"
7004 :format
"%[MinHeight%] %v "
7005 (const :tag
"None" :format
"%t" nil
)
7006 (integer :tag
"Lines" :size
5))
7008 :tag
"Maximum Width"
7010 :format
"%[MaxWidth%] %v "
7011 (const :tag
"None" :format
"%t" nil
)
7012 (integer :tag
"Columns" :size
5))
7014 :tag
"Minimum Width"
7016 :format
"%[MinWidth%] %v\n"
7017 (const :tag
"None" :format
"%t" nil
)
7018 (integer :tag
"Columns" :size
5)))
7021 (declare-function x-display-pixel-height
"xfns.c" (&optional terminal
))
7023 (defun window--sanitize-margin (margin left right
)
7024 "Return MARGIN if it's a number between LEFT and RIGHT."
7025 (when (and (numberp margin
)
7026 (<= left
(- right margin
)) (<= margin right
))
7029 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only
)
7030 "Adjust size of FRAME to display the contents of its buffer exactly.
7031 FRAME can be any live frame and defaults to the selected one.
7032 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
7033 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
7034 FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
7035 `window-min-height' and `window-min-width' respectively.
7037 If the optional argument ONLY is `vertically', resize the frame
7038 vertically only. If ONLY is `horizontally', resize the frame
7041 The new position and size of FRAME can be additionally determined
7042 by customizing the options `fit-frame-to-buffer-sizes' and
7043 `fit-frame-to-buffer-margins' or the corresponding parameters of
7046 (unless (and (fboundp 'x-display-pixel-height
)
7047 ;; We need the respective sizes now.
7048 (fboundp 'display-monitor-attributes-list
))
7049 (user-error "Cannot resize frame in non-graphic Emacs"))
7050 (setq frame
(window-normalize-frame frame
))
7051 (when (window-live-p (frame-root-window frame
))
7052 (with-selected-window (frame-root-window frame
)
7053 (let* ((window (frame-root-window frame
))
7054 (char-width (frame-char-width))
7055 (char-height (frame-char-height))
7056 (monitor-attributes (car (display-monitor-attributes-list
7057 (frame-parameter frame
'display
))))
7058 (geometry (cdr (assq 'geometry monitor-attributes
)))
7059 (display-width (- (nth 2 geometry
) (nth 0 geometry
)))
7060 (display-height (- (nth 3 geometry
) (nth 1 geometry
)))
7061 (workarea (cdr (assq 'workarea monitor-attributes
)))
7063 (margins (or (frame-parameter frame
'fit-frame-to-buffer-margins
)
7064 fit-frame-to-buffer-margins
))
7065 (left-margin (if (nth 0 margins
)
7066 (or (window--sanitize-margin
7067 (nth 0 margins
) 0 display-width
)
7070 (top-margin (if (nth 1 margins
)
7071 (or (window--sanitize-margin
7072 (nth 1 margins
) 0 display-height
)
7075 (workarea-width (nth 2 workarea
))
7076 (right-margin (if (nth 2 margins
)
7078 (or (window--sanitize-margin
7079 (nth 2 margins
) left-margin display-width
)
7082 (workarea-height (nth 3 workarea
))
7083 (bottom-margin (if (nth 3 margins
)
7085 (or (window--sanitize-margin
7086 (nth 3 margins
) top-margin display-height
)
7089 ;; The pixel width of FRAME (which does not include the
7090 ;; window manager's decorations).
7091 (frame-width (frame-pixel-width))
7092 ;; The pixel width of the body of FRAME's root window.
7093 (window-body-width (window-body-width nil t
))
7094 ;; The difference in pixels between total and body width of
7096 (window-extra-width (- (window-pixel-width) window-body-width
))
7097 ;; The difference in pixels between the frame's pixel width
7098 ;; and the window's body width. This is the space we can't
7100 (extra-width (- frame-width window-body-width
))
7101 ;; The maximum width we can use for fitting.
7102 (fit-width (- workarea-width extra-width
))
7103 ;; The pixel position of FRAME's left border. We usually
7104 ;; try to leave this alone.
7106 (let ((left (frame-parameter nil
'left
)))
7108 (funcall (car left
) (cadr left
))
7110 ;; The pixel height of FRAME (which does not include title
7111 ;; line, decorations, and sometimes neither the menu nor
7113 (frame-height (frame-pixel-height))
7114 ;; The pixel height of FRAME's root window (we don't care
7115 ;; about the window's body height since the return value of
7116 ;; `window-text-pixel-size' includes header and mode line).
7117 (window-height (window-pixel-height))
7118 ;; The difference in pixels between the frame's pixel
7119 ;; height and the window's height.
7120 (extra-height (- frame-height window-height
))
7121 ;; When tool-bar-mode is enabled and we just created a new
7122 ;; frame, reserve lines for toolbar resizing. Needed
7123 ;; because for reasons unknown to me Emacs (1) reserves one
7124 ;; line for the toolbar when making the initial frame and
7125 ;; toolbars are enabled, and (2) later adds the remaining
7126 ;; lines needed. Our code runs IN BETWEEN (1) and (2).
7127 ;; YMMV when you're on a system that behaves differently.
7128 (toolbar-extra-height
7129 (let ((quit-restore (window-parameter window
'quit-restore
))
7130 ;; This may have to change when we allow arbitrary
7131 ;; pixel height toolbars.
7132 (lines (tool-bar-height)))
7134 (if (and quit-restore
(eq (car quit-restore
) 'frame
)
7135 (not (zerop lines
)))
7138 ;; The pixel position of FRAME's top border.
7140 (let ((top (frame-parameter nil
'top
)))
7142 (funcall (car top
) (cadr top
))
7144 ;; Sanitize minimum and maximum sizes.
7145 (sizes (or (frame-parameter frame
'fit-frame-to-buffer-sizes
)
7146 fit-frame-to-buffer-sizes
))
7149 ((numberp (nth 0 sizes
)) (* (nth 0 sizes
) char-height
))
7150 ((numberp max-height
) (* max-height char-height
))
7151 (t display-height
)))
7154 ((numberp (nth 1 sizes
)) (* (nth 1 sizes
) char-height
))
7155 ((numberp min-height
) (* min-height char-height
))
7156 (t (* window-min-height char-height
))))
7159 ((numberp (nth 2 sizes
))
7160 (- (* (nth 2 sizes
) char-width
) window-extra-width
))
7161 ((numberp max-width
)
7162 (- (* max-width char-width
) window-extra-width
))
7166 ((numberp (nth 3 sizes
))
7167 (- (* (nth 3 sizes
) char-width
) window-extra-width
))
7168 ((numberp min-width
)
7169 (- (* min-width char-width
) window-extra-width
))
7170 (t (* window-min-width char-width
))))
7171 ;; Note: Currently, for a new frame the sizes of the header
7172 ;; and mode line may be estimated incorrectly
7173 (value (window-text-pixel-size
7174 nil t t workarea-width workarea-height t
))
7175 (width (+ (car value
) (window-right-divider-width)))
7178 (window-bottom-divider-width)
7179 (nth 3 (window-scroll-bars)))))
7180 ;; Don't change height or width when the window's size is fixed
7181 ;; in either direction or ONLY forbids it.
7183 ((or (eq window-size-fixed
'width
) (eq only
'vertically
))
7185 ((or (eq window-size-fixed
'height
) (eq only
'horizontally
))
7187 ;; Fit width to constraints.
7189 (unless frame-resize-pixelwise
7190 ;; Round to character sizes.
7191 (setq width
(* (/ (+ width char-width -
1) char-width
)
7193 ;; Fit to maximum and minimum widths.
7194 (setq width
(max (min width max-width
) min-width
))
7196 (setq width
(+ width extra-width
))
7197 ;; Preserve margins.
7198 (let ((right (+ left width
)))
7200 ((> right right-margin
)
7201 ;; Move frame to left (we don't know its real width).
7202 (setq left
(max left-margin
(- left
(- right right-margin
)))))
7203 ((< left left-margin
)
7204 ;; Move frame to right.
7205 (setq left left-margin
)))))
7206 ;; Fit height to constraints.
7208 (unless frame-resize-pixelwise
7209 (setq height
(* (/ (+ height char-height -
1) char-height
)
7211 ;; Fit to maximum and minimum heights.
7212 (setq height
(max (min height max-height
) min-height
))
7213 ;; Add extra height.
7214 (setq height
(+ height extra-height
))
7215 ;; Preserve margins.
7216 (let ((bottom (+ top height
)))
7218 ((> bottom bottom-margin
)
7219 ;; Move frame up (we don't know its real height).
7220 (setq top
(max top-margin
(- top
(- bottom bottom-margin
)))))
7223 (setq top top-margin
)))))
7225 (set-frame-position frame left top
)
7226 ;; Clumsily try to translate our calculations to what
7227 ;; `set-frame-size' wants.
7229 (setq width
(- (+ (frame-text-width) width
)
7230 extra-width window-body-width
)))
7232 (setq height
(- (+ (frame-text-height) height
)
7233 extra-height window-height
)))
7237 (if frame-resize-pixelwise
7239 (/ width char-width
))
7242 (if frame-resize-pixelwise
7244 (/ height char-height
))
7245 (frame-text-height))
7246 frame-resize-pixelwise
)))))
7248 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width
)
7249 "Adjust size of WINDOW to display its buffer's contents exactly.
7250 WINDOW must be a live window and defaults to the selected one.
7252 If WINDOW is part of a vertical combination, adjust WINDOW's
7253 height. The new height is calculated from the actual height of
7254 the accessible portion of its buffer. The optional argument
7255 MAX-HEIGHT specifies a maximum height and defaults to the height
7256 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
7257 minimum height and defaults to `window-min-height'. Both
7258 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
7259 and header line and a bottom divider, if any.
7261 If WINDOW is part of a horizontal combination and the value of
7262 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
7263 WINDOW's height. The new width of WINDOW is calculated from the
7264 maximum length of its buffer's lines that follow the current
7265 start position of WINDOW. The optional argument MAX-WIDTH
7266 specifies a maximum width and defaults to the width of WINDOW's
7267 frame. The optional argument MIN-WIDTH specifies a minimum width
7268 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
7269 are specified in columns and include fringes, margins, a
7270 scrollbar and a vertical divider, if any.
7272 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
7273 If WINDOW is its frame's root window and the option
7274 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
7275 adjust the frame's size.
7277 Note that even if this function makes WINDOW large enough to show
7278 _all_ parts of its buffer you might not see the first part when
7279 WINDOW was scrolled. If WINDOW is resized horizontally, you will
7280 not see the top of its buffer unless WINDOW starts at its minimum
7281 accessible position."
7283 (setq window
(window-normalize-window window t
))
7284 (if (eq window
(frame-root-window window
))
7285 (when fit-frame-to-buffer
7286 ;; Fit WINDOW's frame to buffer.
7287 (fit-frame-to-buffer
7288 (window-frame window
)
7289 max-height min-height max-width min-width
7290 (and (memq fit-frame-to-buffer
'(vertically horizontally
))
7291 fit-frame-to-buffer
)))
7292 (with-selected-window window
7293 (let* ((pixelwise window-resize-pixelwise
)
7294 (char-height (frame-char-height))
7295 (char-width (frame-char-width))
7296 (total-height (window-size window nil pixelwise
))
7297 (body-height (window-body-height window pixelwise
))
7298 (body-width (window-body-width window pixelwise
))
7300 ;; Sanitize MIN-HEIGHT.
7301 (if (numberp min-height
)
7302 ;; Can't get smaller than `window-safe-min-height'.
7304 (* char-height min-height
)
7307 (window-safe-min-pixel-height window
)
7308 window-safe-min-height
))
7309 ;; Preserve header and mode line if present.
7311 (* char-height window-min-height
)
7313 (window-min-size nil nil t pixelwise
))))
7315 ;; Sanitize MAX-HEIGHT.
7316 (if (numberp max-height
)
7320 window nil nil nil nil nil pixelwise
))
7322 (* char-height max-height
)
7324 (+ total-height
(window-max-delta
7325 window nil nil nil nil nil pixelwise
))))
7328 ;; If WINDOW is vertically combined, try to resize it
7330 ((and (not (eq fit-window-to-buffer-horizontally
'only
))
7331 (not (window-size-fixed-p window
))
7332 (window-combined-p))
7333 ;; Vertically we always want to fit the entire buffer.
7334 ;; WINDOW'S height can't get larger than its frame's pixel
7335 ;; height. Its width remains fixed.
7336 (setq height
(+ (cdr (window-text-pixel-size
7337 nil nil t nil
(frame-pixel-height) t
))
7338 (nth 3 (window-scroll-bars window
))
7339 (window-bottom-divider-width)))
7342 (setq height
(/ (+ height char-height -
1) char-height
)))
7343 (unless (= height total-height
)
7344 (window-resize-no-error
7346 (- (max min-height
(min max-height height
)) total-height
)
7347 nil window pixelwise
)))
7348 ;; If WINDOW is horizontally combined, try to resize it
7350 ((and fit-window-to-buffer-horizontally
7351 (not (window-size-fixed-p window t
))
7352 (window-combined-p nil t
))
7353 (let* ((total-width (window-size window t pixelwise
))
7355 ;; Sanitize MIN-WIDTH.
7356 (if (numberp min-width
)
7357 ;; Can't get smaller than `window-safe-min-width'.
7359 (* char-width min-width
)
7362 (window-safe-min-pixel-width)
7363 window-safe-min-width
))
7364 ;; Preserve fringes, margins, scrollbars if present.
7366 (* char-width window-min-width
)
7368 (window-min-size nil nil t pixelwise
))))
7370 ;; Sanitize MAX-WIDTH.
7371 (if (numberp max-width
)
7374 nil t nil nil nil nil pixelwise
))
7376 (* char-width max-width
)
7378 (+ total-width
(window-max-delta
7379 nil t nil nil nil nil pixelwise
))))
7380 ;; When fitting vertically, assume that WINDOW's start
7381 ;; position remains unaltered. WINDOW can't get wider
7382 ;; than its frame's pixel width, its height remains
7384 (width (+ (car (window-text-pixel-size
7385 nil
(window-start) (point-max)
7387 ;; Add one char-height to assure that
7388 ;; we're on the safe side. This
7389 ;; overshoots when the first line below
7390 ;; the bottom is wider than the window.
7392 (if pixelwise char-height
1))))
7393 (window-right-divider-width))))
7395 (setq width
(/ (+ width char-width -
1) char-width
)))
7396 (unless (= width body-width
)
7397 (window-resize-no-error
7401 (+ total-width
(- width body-width
))))
7403 t window pixelwise
)))))))))
7405 (defun window-safely-shrinkable-p (&optional window
)
7406 "Return t if WINDOW can be shrunk without shrinking other windows.
7407 WINDOW defaults to the selected window."
7408 (with-selected-window (or window
(selected-window))
7409 (let ((edges (window-edges)))
7410 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
7411 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
7413 (defun shrink-window-if-larger-than-buffer (&optional window
)
7414 "Shrink height of WINDOW if its buffer doesn't need so many lines.
7415 More precisely, shrink WINDOW vertically to be as small as
7416 possible, while still showing the full contents of its buffer.
7417 WINDOW must be a live window and defaults to the selected one.
7419 Do not shrink WINDOW to less than `window-min-height' lines. Do
7420 nothing if the buffer contains more lines than the present window
7421 height, or if some of the window's contents are scrolled out of
7422 view, or if shrinking this window would also shrink another
7423 window, or if the window is the only window of its frame.
7425 Return non-nil if the window was shrunk, nil otherwise."
7427 (setq window
(window-normalize-window window t
))
7428 ;; Make sure that WINDOW is vertically combined and `point-min' is
7429 ;; visible (for whatever reason that's needed). The remaining issues
7430 ;; should be taken care of by `fit-window-to-buffer'.
7431 (when (and (window-combined-p window
)
7432 (pos-visible-in-window-p (point-min) window
))
7433 (fit-window-to-buffer window
(window-total-height window
))))
7435 (defun kill-buffer-and-window ()
7436 "Kill the current buffer and delete the selected window."
7438 (let ((window-to-delete (selected-window))
7439 (buffer-to-kill (current-buffer))
7440 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
7443 (add-hook 'kill-buffer-hook delete-window-hook t t
)
7444 (if (kill-buffer (current-buffer))
7445 ;; If `delete-window' failed before, we rerun it to regenerate
7446 ;; the error so it can be seen in the echo area.
7447 (when (eq (selected-window) window-to-delete
)
7449 ;; If the buffer is not dead for some reason (probably because
7450 ;; of a `quit' signal), remove the hook again.
7452 (with-current-buffer buffer-to-kill
7453 (remove-hook 'kill-buffer-hook delete-window-hook t
))))))
7456 (defvar recenter-last-op nil
7457 "Indicates the last recenter operation performed.
7458 Possible values: `top', `middle', `bottom', integer or float numbers.")
7460 (defcustom recenter-positions
'(middle top bottom
)
7461 "Cycling order for `recenter-top-bottom'.
7462 A list of elements with possible values `top', `middle', `bottom',
7463 integer or float numbers that define the cycling order for
7464 the command `recenter-top-bottom'.
7466 Top and bottom destinations are `scroll-margin' lines from the true
7467 window top and bottom. Middle redraws the frame and centers point
7468 vertically within the window. Integer number moves current line to
7469 the specified absolute window-line. Float number between 0.0 and 1.0
7470 means the percentage of the screen space from the top. The default
7471 cycling order is middle -> top -> bottom."
7472 :type
'(repeat (choice
7473 (const :tag
"Top" top
)
7474 (const :tag
"Middle" middle
)
7475 (const :tag
"Bottom" bottom
)
7476 (integer :tag
"Line number")
7477 (float :tag
"Percentage")))
7481 (defun recenter-top-bottom (&optional arg
)
7482 "Move current buffer line to the specified window line.
7483 With no prefix argument, successive calls place point according
7484 to the cycling order defined by `recenter-positions'.
7486 A prefix argument is handled like `recenter':
7487 With numeric prefix ARG, move current line to window-line ARG.
7488 With plain `C-u', move current line to window center."
7491 (arg (recenter arg
)) ; Always respect ARG.
7493 (setq recenter-last-op
7494 (if (eq this-command last-command
)
7495 (car (or (cdr (member recenter-last-op recenter-positions
))
7496 recenter-positions
))
7497 (car recenter-positions
)))
7498 (let ((this-scroll-margin
7499 (min (max 0 scroll-margin
)
7500 (truncate (/ (window-body-height) 4.0)))))
7501 (cond ((eq recenter-last-op
'middle
)
7503 ((eq recenter-last-op
'top
)
7504 (recenter this-scroll-margin
))
7505 ((eq recenter-last-op
'bottom
)
7506 (recenter (- -
1 this-scroll-margin
)))
7507 ((integerp recenter-last-op
)
7508 (recenter recenter-last-op
))
7509 ((floatp recenter-last-op
)
7510 (recenter (round (* recenter-last-op
(window-height))))))))))
7512 (define-key global-map
[?\C-l
] 'recenter-top-bottom
)
7514 (defun move-to-window-line-top-bottom (&optional arg
)
7515 "Position point relative to window.
7517 With a prefix argument ARG, acts like `move-to-window-line'.
7519 With no argument, positions point at center of window.
7520 Successive calls position point at positions defined
7521 by `recenter-positions'."
7524 (arg (move-to-window-line arg
)) ; Always respect ARG.
7526 (setq recenter-last-op
7527 (if (eq this-command last-command
)
7528 (car (or (cdr (member recenter-last-op recenter-positions
))
7529 recenter-positions
))
7530 (car recenter-positions
)))
7531 (let ((this-scroll-margin
7532 (min (max 0 scroll-margin
)
7533 (truncate (/ (window-body-height) 4.0)))))
7534 (cond ((eq recenter-last-op
'middle
)
7535 (call-interactively 'move-to-window-line
))
7536 ((eq recenter-last-op
'top
)
7537 (move-to-window-line this-scroll-margin
))
7538 ((eq recenter-last-op
'bottom
)
7539 (move-to-window-line (- -
1 this-scroll-margin
)))
7540 ((integerp recenter-last-op
)
7541 (move-to-window-line recenter-last-op
))
7542 ((floatp recenter-last-op
)
7543 (move-to-window-line (round (* recenter-last-op
(window-height))))))))))
7545 (define-key global-map
[?\M-r
] 'move-to-window-line-top-bottom
)
7547 ;;; Scrolling commands.
7549 ;;; Scrolling commands which do not signal errors at top/bottom
7550 ;;; of buffer at first key-press (instead move to top/bottom
7553 (defcustom scroll-error-top-bottom nil
7554 "Move point to top/bottom of buffer before signaling a scrolling error.
7555 A value of nil means just signal an error if no more scrolling possible.
7556 A value of t means point moves to the beginning or the end of the buffer
7557 \(depending on scrolling direction) when no more scrolling possible.
7558 When point is already on that position, then signal an error."
7563 (defun scroll-up-command (&optional arg
)
7564 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
7565 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
7566 scroll window further, move cursor to the bottom line.
7567 When point is already on that position, then signal an error.
7568 A near full screen is `next-screen-context-lines' less than a full screen.
7569 Negative ARG means scroll downward.
7570 If ARG is the atom `-', scroll downward by nearly full screen."
7573 ((null scroll-error-top-bottom
)
7576 (scroll-down-command nil
))
7577 ((< (prefix-numeric-value arg
) 0)
7578 (scroll-down-command (- (prefix-numeric-value arg
))))
7580 (scroll-up arg
)) ; signal error
7586 ;; When scrolling by ARG lines can't be done,
7587 ;; move by ARG lines instead.
7589 ;; When ARG is nil for full-screen scrolling,
7590 ;; move to the bottom of the buffer.
7591 (goto-char (point-max))))))))
7593 (put 'scroll-up-command
'scroll-command t
)
7595 (defun scroll-down-command (&optional arg
)
7596 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
7597 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
7598 scroll window further, move cursor to the top line.
7599 When point is already on that position, then signal an error.
7600 A near full screen is `next-screen-context-lines' less than a full screen.
7601 Negative ARG means scroll upward.
7602 If ARG is the atom `-', scroll upward by nearly full screen."
7605 ((null scroll-error-top-bottom
)
7608 (scroll-up-command nil
))
7609 ((< (prefix-numeric-value arg
) 0)
7610 (scroll-up-command (- (prefix-numeric-value arg
))))
7612 (scroll-down arg
)) ; signal error
7616 (beginning-of-buffer
7618 ;; When scrolling by ARG lines can't be done,
7619 ;; move by ARG lines instead.
7620 (forward-line (- arg
))
7621 ;; When ARG is nil for full-screen scrolling,
7622 ;; move to the top of the buffer.
7623 (goto-char (point-min))))))))
7625 (put 'scroll-down-command
'scroll-command t
)
7627 ;;; Scrolling commands which scroll a line instead of full screen.
7629 (defun scroll-up-line (&optional arg
)
7630 "Scroll text of selected window upward ARG lines; or one line if no ARG.
7631 If ARG is omitted or nil, scroll upward by one line.
7632 This is different from `scroll-up-command' that scrolls a full screen."
7634 (scroll-up (or arg
1)))
7636 (put 'scroll-up-line
'scroll-command t
)
7638 (defun scroll-down-line (&optional arg
)
7639 "Scroll text of selected window down ARG lines; or one line if no ARG.
7640 If ARG is omitted or nil, scroll down by one line.
7641 This is different from `scroll-down-command' that scrolls a full screen."
7643 (scroll-down (or arg
1)))
7645 (put 'scroll-down-line
'scroll-command t
)
7648 (defun scroll-other-window-down (&optional lines
)
7649 "Scroll the \"other window\" down.
7650 For more details, see the documentation for `scroll-other-window'."
7652 (scroll-other-window
7653 ;; Just invert the argument's meaning.
7654 ;; We can do that without knowing which window it will be.
7655 (if (eq lines
'-
) nil
7657 (- (prefix-numeric-value lines
))))))
7659 (defun beginning-of-buffer-other-window (arg)
7660 "Move point to the beginning of the buffer in the other window.
7661 Leave mark at previous position.
7662 With arg N, put point N/10 of the way from the true beginning."
7664 (let ((orig-window (selected-window))
7665 (window (other-window-for-scrolling)))
7666 ;; We use unwind-protect rather than save-window-excursion
7667 ;; because the latter would preserve the things we want to change.
7670 (select-window window
)
7671 ;; Set point and mark in that window's buffer.
7673 (beginning-of-buffer arg
))
7674 ;; Set point accordingly.
7676 (select-window orig-window
))))
7678 (defun end-of-buffer-other-window (arg)
7679 "Move point to the end of the buffer in the other window.
7680 Leave mark at previous position.
7681 With arg N, put point N/10 of the way from the true end."
7683 ;; See beginning-of-buffer-other-window for comments.
7684 (let ((orig-window (selected-window))
7685 (window (other-window-for-scrolling)))
7688 (select-window window
)
7690 (end-of-buffer arg
))
7692 (select-window orig-window
))))
7694 (defvar mouse-autoselect-window-timer nil
7695 "Timer used by delayed window autoselection.")
7697 (defvar mouse-autoselect-window-position nil
7698 "Last mouse position recorded by delayed window autoselection.")
7700 (defvar mouse-autoselect-window-window nil
7701 "Last window recorded by delayed window autoselection.")
7703 (defvar mouse-autoselect-window-state nil
7704 "When non-nil, special state of delayed window autoselection.
7705 Possible values are `suspend' (suspend autoselection after a menu or
7706 scrollbar interaction) and `select' (the next invocation of
7707 `handle-select-window' shall select the window immediately).")
7709 (defun mouse-autoselect-window-cancel (&optional force
)
7710 "Cancel delayed window autoselection.
7711 Optional argument FORCE means cancel unconditionally."
7712 (unless (and (not force
)
7713 ;; Don't cancel for select-window or select-frame events
7714 ;; or when the user drags a scroll bar.
7715 (or (memq this-command
7716 '(handle-select-window handle-switch-frame
))
7717 (and (eq this-command
'scroll-bar-toolkit-scroll
)
7718 (memq (nth 4 (event-end last-input-event
))
7719 '(handle end-scroll
)))))
7720 (setq mouse-autoselect-window-state nil
)
7721 (when (timerp mouse-autoselect-window-timer
)
7722 (cancel-timer mouse-autoselect-window-timer
))
7723 (remove-hook 'pre-command-hook
'mouse-autoselect-window-cancel
)))
7725 (defun mouse-autoselect-window-start (mouse-position &optional window suspend
)
7726 "Start delayed window autoselection.
7727 MOUSE-POSITION is the last position where the mouse was seen as returned
7728 by `mouse-position'. Optional argument WINDOW non-nil denotes the
7729 window where the mouse was seen. Optional argument SUSPEND non-nil
7730 means suspend autoselection."
7731 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
7732 (setq mouse-autoselect-window-position mouse-position
)
7733 (when window
(setq mouse-autoselect-window-window window
))
7734 (setq mouse-autoselect-window-state
(when suspend
'suspend
))
7735 ;; Install timer which runs `mouse-autoselect-window-select' after
7736 ;; `mouse-autoselect-window' seconds.
7737 (setq mouse-autoselect-window-timer
7739 (abs mouse-autoselect-window
) nil
'mouse-autoselect-window-select
)))
7741 (defun mouse-autoselect-window-select ()
7742 "Select window with delayed window autoselection.
7743 If the mouse position has stabilized in a non-selected window, select
7744 that window. The minibuffer window is selected only if the minibuffer
7745 is active. This function is run by `mouse-autoselect-window-timer'."
7747 (let* ((mouse-position (mouse-position))
7750 (window-at (cadr mouse-position
) (cddr mouse-position
)
7751 (car mouse-position
)))))
7753 ((or (and (fboundp 'menu-or-popup-active-p
) (menu-or-popup-active-p))
7755 (let ((coords (coordinates-in-window-p
7756 (cdr mouse-position
) window
)))
7757 (and (not (consp coords
))
7758 (not (memq coords
'(left-margin right-margin
)))))))
7759 ;; A menu / popup dialog is active or the mouse is not on the
7760 ;; text region of WINDOW: Suspend autoselection temporarily.
7761 (mouse-autoselect-window-start mouse-position nil t
))
7762 ((eq mouse-autoselect-window-state
'suspend
)
7763 ;; Delayed autoselection was temporarily suspended, reenable it.
7764 (mouse-autoselect-window-start mouse-position
))
7765 ((and window
(not (eq window
(selected-window)))
7766 (or (not (numberp mouse-autoselect-window
))
7767 (and (> mouse-autoselect-window
0)
7768 ;; If `mouse-autoselect-window' is positive, select
7769 ;; window if the window is the same as before.
7770 (eq window mouse-autoselect-window-window
))
7771 ;; Otherwise select window if the mouse is at the same
7772 ;; position as before. Observe that the first test after
7773 ;; starting autoselection usually fails since the value of
7774 ;; `mouse-autoselect-window-position' recorded there is the
7775 ;; position where the mouse has entered the new window and
7776 ;; not necessarily where the mouse has stopped moving.
7777 (equal mouse-position mouse-autoselect-window-position
))
7778 ;; The minibuffer is a candidate window if it's active.
7779 (or (not (window-minibuffer-p window
))
7780 (eq window
(active-minibuffer-window))))
7781 ;; Mouse position has stabilized in non-selected window: Cancel
7782 ;; delayed autoselection and try to select that window.
7783 (mouse-autoselect-window-cancel t
)
7784 ;; Select window where mouse appears unless the selected window is the
7785 ;; minibuffer. Use `unread-command-events' in order to execute pre-
7786 ;; and post-command hooks and trigger idle timers. To avoid delaying
7787 ;; autoselection again, set `mouse-autoselect-window-state'."
7788 (unless (window-minibuffer-p)
7789 (setq mouse-autoselect-window-state
'select
)
7790 (setq unread-command-events
7791 (cons (list 'select-window
(list window
))
7792 unread-command-events
))))
7793 ((or (and window
(eq window
(selected-window)))
7794 (not (numberp mouse-autoselect-window
))
7795 (equal mouse-position mouse-autoselect-window-position
))
7796 ;; Mouse position has either stabilized in the selected window or at
7797 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
7798 (mouse-autoselect-window-cancel t
))
7800 ;; Mouse position has not stabilized yet, resume delayed
7802 (mouse-autoselect-window-start mouse-position window
))))))
7804 (defun handle-select-window (event)
7805 "Handle select-window events."
7807 (let ((window (posn-window (event-start event
))))
7808 (unless (or (not (window-live-p window
))
7809 ;; Don't switch if we're currently in the minibuffer.
7810 ;; This tries to work around problems where the
7811 ;; minibuffer gets unselected unexpectedly, and where
7812 ;; you then have to move your mouse all the way down to
7813 ;; the minibuffer to select it.
7814 (window-minibuffer-p)
7815 ;; Don't switch to minibuffer window unless it's active.
7816 (and (window-minibuffer-p window
)
7817 (not (minibuffer-window-active-p window
)))
7818 ;; Don't switch when autoselection shall be delayed.
7819 (and (numberp mouse-autoselect-window
)
7820 (not (zerop mouse-autoselect-window
))
7821 (not (eq mouse-autoselect-window-state
'select
))
7823 ;; Cancel any delayed autoselection.
7824 (mouse-autoselect-window-cancel t
)
7825 ;; Start delayed autoselection from current mouse
7826 ;; position and window.
7827 (mouse-autoselect-window-start (mouse-position) window
)
7828 ;; Executing a command cancels delayed autoselection.
7830 'pre-command-hook
'mouse-autoselect-window-cancel
))))
7831 (when mouse-autoselect-window
7832 ;; Reset state of delayed autoselection.
7833 (setq mouse-autoselect-window-state nil
)
7834 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
7835 (run-hooks 'mouse-leave-buffer-hook
))
7838 (select-window window
))))
7840 (defun truncated-partial-width-window-p (&optional window
)
7841 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
7842 WINDOW must be a live window and defaults to the selected one.
7843 Return nil if WINDOW is not a partial-width window
7844 (regardless of the value of `truncate-lines').
7845 Otherwise, consult the value of `truncate-partial-width-windows'
7846 for the buffer shown in WINDOW."
7847 (setq window
(window-normalize-window window t
))
7848 (unless (window-full-width-p window
)
7849 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
7850 (window-buffer window
))))
7851 (if (integerp t-p-w-w
)
7852 (< (window-width window
) t-p-w-w
)
7855 ;; Some of these are in tutorial--default-keys, so update that if you
7857 (define-key ctl-x-map
"0" 'delete-window
)
7858 (define-key ctl-x-map
"1" 'delete-other-windows
)
7859 (define-key ctl-x-map
"2" 'split-window-below
)
7860 (define-key ctl-x-map
"3" 'split-window-right
)
7861 (define-key ctl-x-map
"o" 'other-window
)
7862 (define-key ctl-x-map
"^" 'enlarge-window
)
7863 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
7864 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
7865 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
7866 (define-key ctl-x-map
"+" 'balance-windows
)
7867 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
7869 ;;; window.el ends here