Fix bug#18964
[emacs.git] / lisp / window.el
blob91a0e159a8442e2f2be936d98a555eb4130fe484
1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2014 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: internal
8 ;; Package: emacs
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/>.
25 ;;; Commentary:
27 ;; Window tree functions.
29 ;;; Code:
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.
37 (apply #'append
38 (mapcar (lambda (terminal)
39 (let ((frames (frames-on-display-list terminal))
40 (top-frame (tty-top-frame terminal))
41 alist)
42 (if top-frame
43 (setq frames
44 (cons top-frame
45 (delq top-frame frames))))
46 (dolist (f frames)
47 (push (cons f (frame-selected-window f))
48 alist))
49 alist))
50 (terminal-list)))))
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
69 window is left alone.
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)))
77 (save-current-buffer
78 (unwind-protect
79 (progn ,@body)
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
85 displayed current.")
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.
94 Return the buffer."
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))
106 (erase-buffer)
107 (run-hooks 'temp-buffer-window-setup-hook))
108 ;; Return the buffer.
109 buffer)))
111 (defun temp-buffer-window-show (buffer &optional action)
112 "Show temporary buffer BUFFER in a window.
113 Return the window showing BUFFER. Pass ACTION as action argument
114 to `display-buffer'."
115 (let (window frame)
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
128 'temp-buffer-resize)
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))
135 (raise-frame 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.
143 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)."
184 (declare (debug t))
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)
193 ,window ,value)
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)
200 ,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
206 BODY."
207 (declare (debug t))
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)
216 ,window ,value)
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)
223 ,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."
229 (declare (debug t))
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)
238 ,window ,value)
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))))
249 (ignore-errors
250 (funcall (cdr (assq 'window-height (cdr ,vaction))) ,window)))
252 (if (functionp ,vquit-function)
253 (funcall ,vquit-function ,window ,value)
254 ,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
264 be any window."
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
270 be any window."
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."
281 (let ((count 0))
282 (when (and (windowp window) (setq window (window-child window)))
283 (while window
284 (setq count (1+ count))
285 (setq window (window-next-sibling window))))
286 count))
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))))
294 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."
300 (cond
301 ((not buffer-or-name)
302 (current-buffer))
303 ((bufferp buffer-or-name)
304 (if (buffer-live-p buffer-or-name)
305 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."
314 (if frame
315 (if (frame-live-p frame)
316 frame
317 (error "%s is not a live frame" frame))
318 (selected-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."
326 (cond
327 ((null window)
328 (selected-window))
329 (live-only
330 (if (window-live-p window)
331 window
332 (error "%s is not a live window" window)))
333 ((window-valid-p window)
334 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."
349 (let ((frame
350 (cond
351 ((window-valid-p window-or-frame)
352 (window-frame window-or-frame))
353 ((frame-live-p window-or-frame)
354 window-or-frame)
355 (t (selected-frame)))))
356 (if horizontal
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."
390 :type 'integer
391 :version "24.1"
392 :group 'windows)
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."
421 :type 'integer
422 :version "24.1"
423 :group 'windows)
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."
434 (if horizontal
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)))
448 (and parent
449 (if horizontal
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))
459 (if horizontal
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))
469 (cond
470 ((window-live-p window)
471 ;; If WINDOW is live, return 1.
473 ((if horizontal
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))
479 (count 0))
480 (while child
481 (setq count
482 (+ (window-combinations child horizontal)
483 count))
484 (setq child (window-right child)))
485 count))
487 ;; If WINDOW is not iso-combined, return the maximum value of any
488 ;; child window of WINDOW.
489 (let ((child (window-child window))
490 (count 1))
491 (while child
492 (setq count
493 (max (window-combinations child horizontal)
494 count))
495 (setq child (window-right child)))
496 count))))
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
507 (walk-window-tree-1
508 fun (window-left-child walk-window-tree-window) any)
509 (walk-window-tree-1
510 fun (window-top-child walk-window-tree-window) any))
511 (if sub-only
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
521 FRAME.
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
531 unpredictable."
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)
539 (or (eq minibuf t)
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
553 is unpredictable."
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
563 too.
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."
570 (let (this-value)
571 (catch 'found
572 (walk-window-tree
573 (lambda (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))))
579 ;;; Atomic windows.
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))
585 (let (root)
586 (while (and window (window-parameter window 'window-atom))
587 (setq root window)
588 (setq window (window-parent window)))
589 root))
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)
596 (walk-window-subtree
597 (lambda (window)
598 (unless (window-parameter window 'window-atom)
599 (set-window-parameter window 'window-atom t)))
600 window t)
601 window))
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
630 failed."
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)))
637 root new)
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))
642 (window-make-atom
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.
646 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'."
659 (when window
660 (if (window-parameter window 'window-atom)
661 (let ((count 0))
662 (when (or (catch 'reset
663 (walk-window-subtree
664 (lambda (window)
665 (if (window-parameter window 'window-atom)
666 (setq count (1+ count))
667 (throw 'reset t)))
668 window t))
669 ;; count >= 1 must hold here. If there's no other
670 ;; window around dissolve this atomic window.
671 (= count 1))
672 ;; Dissolve atomic window.
673 (walk-window-subtree
674 (lambda (window)
675 (set-window-parameter window 'window-atom nil))
676 window t)))
677 ;; Check children.
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)))
692 ;; Side windows.
693 (defvar window-sides '(left top right bottom)
694 "Window sides.")
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."
699 :type 'boolean
700 :group 'windows
701 :version "24.1")
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."
711 :version "24.1"
712 :risky t
713 :type
714 '(list
715 :value (nil nil nil nil)
716 (choice
717 :tag "Left"
718 :help-echo "Maximum slots of left side window."
719 :value nil
720 :format "%[Left%] %v\n"
721 (const :tag "Unlimited" :format "%t" nil)
722 (integer :tag "Number" :value 2 :size 5))
723 (choice
724 :tag "Top"
725 :help-echo "Maximum slots of top side window."
726 :value nil
727 :format "%[Top%] %v\n"
728 (const :tag "Unlimited" :format "%t" nil)
729 (integer :tag "Number" :value 3 :size 5))
730 (choice
731 :tag "Right"
732 :help-echo "Maximum slots of right side window."
733 :value nil
734 :format "%[Right%] %v\n"
735 (const :tag "Unlimited" :format "%t" nil)
736 (integer :tag "Number" :value 2 :size 5))
737 (choice
738 :tag "Bottom"
739 :help-echo "Maximum slots of bottom side window."
740 :value nil
741 :format "%[Bottom%] %v\n"
742 (const :tag "Unlimited" :format "%t" nil)
743 (integer :tag "Number" :value 3 :size 5)))
744 :group 'windows)
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)
752 (window-parameter
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
758 the selected one.
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
764 its root window."
765 (let ((frame (window-normalize-frame frame))
766 major sibling)
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.
769 (walk-window-tree
770 (lambda (window)
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)))
777 frame t 'nomini)
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))
785 window)
786 ;; (1) If a window on the opposite side exists, return that window's
787 ;; sibling.
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
791 ;; window's sibling.
792 ;; (4) Otherwise return the frame's root window.
793 (cond
794 ((or (and (eq side 'left)
795 (setq window (window-with-parameter 'window-side 'right nil t)))
796 (and (eq side 'top)
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))
805 (cond
806 (window-sides-vertical
807 root)
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))
812 (t root)))
813 ((memq side '(top bottom))
814 (cond
815 ((not window-sides-vertical)
816 root)
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))
821 (t root))))))
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))
833 (on-side (cond
834 ((eq side 'top) 'above)
835 ((eq side 'bottom) 'below)
836 (t side)))
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)))
843 (when new
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)))
857 (setq alist
858 (cons
859 (cons
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
863 ;; root window.
865 alist)))
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)))
893 (cond
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.
902 (windows
903 (when major
904 (let (windows)
905 (walk-window-tree
906 (lambda (window)
907 (when (eq (window-parameter window 'window-side) side)
908 (setq windows (cons window windows))))
909 nil nil 'nomini)
910 (nreverse windows))))
911 (slots (when major (max 1 (window-child-count major))))
912 (max-slots
913 (nth (cond
914 ((eq side 'left) 0)
915 ((eq side 'top) 1)
916 ((eq side 'right) 2)
917 ((eq side 'bottom) 3))
918 window-sides-slots))
919 window this-window this-slot prev-window next-window
920 best-window best-slot abs-slot)
922 (cond
923 ((and (numberp max-slots) (<= max-slots 0))
924 ;; No side-slots available on this side. Don't create an error,
925 ;; just return nil.
926 nil)
927 ((not windows)
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.
932 (catch 'found
933 (dolist (window windows)
934 (setq this-slot (window-parameter window 'window-slot))
935 (cond
936 ;; The following should not happen and probably be checked
937 ;; by window--side-check.
938 ((not (numberp this-slot)))
939 ((= this-slot slot)
940 ;; A window with a matching slot has been found.
941 (setq this-window window)
942 (throw 'found t))
944 ;; Check if this window has a better slot value wrt the
945 ;; slot of the window we want.
946 (setq abs-slot
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))
954 (cond
955 ((<= this-slot slot)
956 (setq prev-window window))
957 ((not next-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).
972 (or (and this-window
973 ;; Reuse `this-window'.
974 (window--display-buffer buffer this-window 'reuse alist 'side))
975 (and (or (not max-slots) (< slots max-slots))
976 (or (and next-window
977 ;; Make new window before `next-window'.
978 (let ((next-side
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)
986 window))
987 (and prev-window
988 ;; Make new window after `prev-window'.
989 (let ((prev-side
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)
997 window)))
998 (set-window-parameter window 'window-slot slot)
999 (window--display-buffer buffer window 'window alist 'side))
1000 (and best-window
1001 ;; Reuse `best-window'.
1002 (progn
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
1013 invariants:
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
1028 (walk-window-tree
1029 (lambda (window)
1030 (setq side (window-parameter window 'window-side))
1031 (setq parent (window-parent window))
1032 (setq parent-side
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.
1036 (cond
1037 (parent-side
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.
1041 (throw 'reset t)))
1042 ((not side)
1043 (when (window-buffer window)
1044 ;; Record that we have at least one non-side,
1045 ;; live window.
1046 (setq none t)))
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.
1053 (throw 'reset t))
1054 ;; Now check that there's no more than one major
1055 ;; window for any of left, top, right and bottom.
1056 ((eq side 'left)
1057 (if left (throw 'reset t) (setq left t)))
1058 ((eq side 'top)
1059 (if top (throw 'reset t) (setq top t)))
1060 ((eq side 'right)
1061 (if right (throw 'reset t) (setq right t)))
1062 ((eq side 'bottom)
1063 (if bottom (throw 'reset t) (setq bottom t)))
1065 (throw 'reset t))))
1066 frame t 'nomini))
1067 ;; If there's a side window, there must be at least one
1068 ;; non-side window.
1069 (and (or left top right bottom) (not none)))
1070 (walk-window-tree
1071 (lambda (window)
1072 (set-window-parameter window 'window-side nil))
1073 frame t 'nomini))))
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))
1090 (insert
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)))
1106 (insert
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"
1113 (cadr fringes)
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)))))
1120 (insert "\n")))
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
1127 dumping to it."
1128 (let* ((window
1129 (cond
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))
1135 window-or-frame)
1137 (frame-root-window))))
1138 (frame (window-frame window)))
1139 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1140 (erase-buffer)
1141 (insert
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)
1152 "0")
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))))
1161 ;;; Window sizes.
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'."
1181 (if horizontal
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'
1198 otherwise."
1199 (if horizontal
1200 (if pixelwise
1201 (window-pixel-width window)
1202 (window-total-width window round))
1203 (if pixelwise
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
1215 window).")
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
1227 of WINDOW's lines.
1229 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1230 of WINDOW."
1231 (setq window (window-normalize-window window))
1232 (if pixelwise
1233 (if horizontal
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
1245 of WINDOW's lines.
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
1256 of WINDOW."
1257 (window--min-size-1
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)))
1263 (if sub
1264 (let ((value 0))
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.
1269 (while sub
1270 (setq value (+ value
1271 (window--min-size-1
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.
1276 (while sub
1277 (setq value (max value
1278 (window--min-size-1
1279 sub horizontal ignore pixelwise)))
1280 (setq sub (window-right sub))))
1281 value)
1282 (with-current-buffer (window-buffer window)
1283 (cond
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))
1290 ((eq ignore 'safe)
1291 ;; If IGNORE equals `safe' return the safe value.
1292 (window-safe-min-size window horizontal pixelwise))
1293 (horizontal
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))
1303 (pixel-width
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))))
1310 (if pixelwise
1311 (max
1312 (if window-resize-pixelwise
1313 pixel-width
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)))
1319 (max
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))
1325 (pixel-height
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))))
1331 (if pixelwise
1332 (max
1333 (if window-resize-pixelwise
1334 pixel-height
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
1354 as well.
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
1359 can be enlarged.
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
1364 can be shrunk.
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
1375 pixels."
1376 (setq window (window-normalize-window window))
1377 (cond
1378 ((< delta 0)
1379 (max (- (window-min-size window horizontal ignore pixelwise)
1380 (window-size window horizontal pixelwise))
1381 delta))
1382 ((window--size-ignore-p window ignore)
1383 delta)
1384 ((> delta 0)
1385 (if (window-size-fixed-p window horizontal)
1387 delta))
1388 (t 0)))
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))
1396 (if (> delta 0)
1397 (>= (window-sizable window delta horizontal ignore pixelwise)
1398 delta)
1399 (<= (window-sizable window delta horizontal ignore pixelwise)
1400 delta)))
1402 (defun window--size-fixed-1 (window horizontal)
1403 "Internal function for `window-size-fixed-p'."
1404 (let ((sub (window-child window)))
1405 (catch 'fixed
1406 (if sub
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.
1411 (progn
1412 (while sub
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.
1416 (throw 'fixed nil))
1417 (setq sub (window-right sub)))
1418 ;; All child windows are fixed-size, so WINDOW's size is
1419 ;; fixed.
1420 (throw 'fixed t))
1421 ;; An ortho-combination is fixed-size if at least one of its
1422 ;; child windows is fixed-size.
1423 (while sub
1424 (when (window--size-fixed-1 sub horizontal)
1425 ;; We found a fixed-size child window, so WINDOW's size
1426 ;; is fixed.
1427 (throw 'fixed t))
1428 (setq sub (window-right sub))))
1429 ;; WINDOW is a live window.
1430 (with-current-buffer (window-buffer window)
1431 (if horizontal
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)))
1455 (catch 'done
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)))
1461 (while sub
1462 (cond
1463 ((eq sub window)
1464 (setq skip (eq trail 'before)))
1465 (skip)
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.
1474 (while sub
1475 (unless (eq sub window)
1476 (setq delta
1477 (min delta
1478 (max (- (window-size sub horizontal pixelwise 'ceiling)
1479 (window-min-size
1480 sub horizontal ignore pixelwise))
1481 0))))
1482 (setq sub (window-right sub))))
1483 (if noup
1484 delta
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)))
1521 (cond
1522 (nodown
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))
1526 ((<= size minimum)
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
1532 ;; minimum size.
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)
1546 0))))
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.
1552 delta
1553 (let* ((parent (window-parent window))
1554 (sub (window-child parent)))
1555 (catch 'fixed
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)))
1560 (while sub
1561 (cond
1562 ((eq sub window)
1563 (setq skip (eq trail 'before)))
1564 (skip)
1566 (setq delta
1567 (+ delta
1568 (max
1569 (- (window-size sub horizontal pixelwise 'floor)
1570 (window-min-size
1571 sub horizontal ignore pixelwise))
1572 0)))))
1573 (setq sub (window-right sub))))
1574 ;; For an ortho-combination throw DELTA when at least one
1575 ;; child window is fixed-size.
1576 (while sub
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))))
1582 (if noup
1583 ;; When NOUP is nil, DELTA is all we can get.
1584 delta
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
1625 ;; size.
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
1641 can be enlarged.
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
1659 shrunk.
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
1670 number of pixels."
1671 (setq window (window-normalize-window window))
1672 (cond
1673 ((< delta 0)
1674 (max (- (window-min-delta
1675 window horizontal ignore trail noup nodown pixelwise))
1676 delta))
1677 ((> delta 0)
1678 (min (window-max-delta
1679 window horizontal ignore trail noup nodown pixelwise)
1680 delta))
1681 (t 0)))
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
1690 pixels."
1691 (setq window (window-normalize-window window))
1692 (if (> delta 0)
1693 (>= (window--resizable
1694 window delta horizontal ignore trail noup nodown pixelwise)
1695 delta)
1696 (<= (window--resizable
1697 window delta horizontal ignore trail noup nodown pixelwise)
1698 delta)))
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
1710 can be enlarged.
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
1726 pixels."
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))
1736 (if (> delta 0)
1737 (>= (window--resizable
1738 window delta horizontal ignore nil nil nil pixelwise)
1739 delta)
1740 (<= (window--resizable
1741 window delta horizontal ignore nil nil nil pixelwise)
1742 delta)))
1744 ;; Aliases of functions defined in window.c.
1745 (defalias 'window-height 'window-total-height)
1746 (defalias 'window-width 'window-body-width)
1748 (defun window-full-height-p (&optional window)
1749 "Return t if WINDOW is as high as its containing frame.
1750 More precisely, return t if and only if the total height of
1751 WINDOW equals the total height of the root window of WINDOW's
1752 frame. WINDOW must be a valid window and defaults to the
1753 selected one."
1754 (setq window (window-normalize-window window))
1755 (if (window-minibuffer-p window)
1756 (eq window (frame-root-window (window-frame window)))
1757 (= (window-pixel-height window)
1758 (window-pixel-height (frame-root-window window)))))
1760 (defun window-full-width-p (&optional window)
1761 "Return t if WINDOW is as wide as its containing frame.
1762 More precisely, return t if and only if the total width of WINDOW
1763 equals the total width of the root window of WINDOW's frame.
1764 WINDOW must be a valid window and defaults to the selected one."
1765 (setq window (window-normalize-window window))
1766 (= (window-pixel-width window)
1767 (window-pixel-width (frame-root-window window))))
1769 (defun window-body-size (&optional window horizontal pixelwise)
1770 "Return the height or width of WINDOW's text area.
1771 WINDOW must be a live window and defaults to the selected one.
1773 If HORIZONTAL is omitted or nil, return the height of the text
1774 area, like `window-body-height'. Otherwise, return the width of
1775 the text area, like `window-body-width'. In either case, the
1776 optional argument PIXELWISE is passed to the functions."
1777 (if horizontal
1778 (window-body-width window pixelwise)
1779 (window-body-height window pixelwise)))
1781 (defun window-current-scroll-bars (&optional window)
1782 "Return the current scroll bar types for WINDOW.
1783 WINDOW must be a live window and defaults to the selected one.
1785 The return value is a cons cell (VERTICAL . HORIZONTAL) where
1786 VERTICAL specifies the current location of the vertical scroll
1787 bar (`left', `right' or nil), and HORIZONTAL specifies the
1788 current location of the horizontal scroll bar (`bottom' or nil).
1790 Unlike `window-scroll-bars', this function reports the scroll bar
1791 type actually used, once frame defaults and `scroll-bar-mode' are
1792 taken into account."
1793 (setq window (window-normalize-window window t))
1794 (let ((vertical (nth 2 (window-scroll-bars window)))
1795 (horizontal (nth 5 (window-scroll-bars window)))
1796 (inherited (frame-current-scroll-bars (window-frame window))))
1797 (when (eq vertical t)
1798 (setq vertical (car inherited)))
1799 (when (eq horizontal t)
1800 (setq horizontal (cdr inherited)))
1801 (cons vertical (and horizontal 'bottom))))
1803 (defun walk-windows (fun &optional minibuf all-frames)
1804 "Cycle through all live windows, calling FUN for each one.
1805 FUN must specify a function with a window as its sole argument.
1806 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1807 windows to include in the walk.
1809 MINIBUF t means include the minibuffer window even if the
1810 minibuffer is not active. MINIBUF nil or omitted means include
1811 the minibuffer window only if the minibuffer is active. Any
1812 other value means do not include the minibuffer window even if
1813 the minibuffer is active.
1815 ALL-FRAMES nil or omitted means consider all windows on the
1816 selected frame, plus the minibuffer window if specified by the
1817 MINIBUF argument. If the minibuffer counts, consider all windows
1818 on all frames that share that minibuffer too. The following
1819 non-nil values of ALL-FRAMES have special meanings:
1821 - t means consider all windows on all existing frames.
1823 - `visible' means consider all windows on all visible frames on
1824 the current terminal.
1826 - 0 (the number zero) means consider all windows on all visible
1827 and iconified frames on the current terminal.
1829 - A frame means consider all windows on that frame only.
1831 Anything else means consider all windows on the selected frame
1832 and no others.
1834 This function changes neither the order of recently selected
1835 windows nor the buffer list."
1836 ;; If we start from the minibuffer window, don't fail to come
1837 ;; back to it.
1838 (when (window-minibuffer-p)
1839 (setq minibuf t))
1840 ;; Make sure to not mess up the order of recently selected
1841 ;; windows. Use `save-selected-window' and `select-window'
1842 ;; with second argument non-nil for this purpose.
1843 (save-selected-window
1844 (when (framep all-frames)
1845 (select-window (frame-first-window all-frames) 'norecord))
1846 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
1847 (funcall fun walk-windows-window))))
1849 (defun window-at-side-p (&optional window side)
1850 "Return t if WINDOW is at SIDE of its containing frame.
1851 WINDOW must be a valid window and defaults to the selected one.
1852 SIDE can be any of the symbols `left', `top', `right' or
1853 `bottom'. The default value nil is handled like `bottom'."
1854 (setq window (window-normalize-window window))
1855 (let ((edge
1856 (cond
1857 ((eq side 'left) 0)
1858 ((eq side 'top) 1)
1859 ((eq side 'right) 2)
1860 ((memq side '(bottom nil)) 3))))
1861 (= (nth edge (window-pixel-edges window))
1862 (nth edge (window-pixel-edges (frame-root-window window))))))
1864 (defun window-at-side-list (&optional frame side)
1865 "Return list of all windows on SIDE of FRAME.
1866 FRAME must be a live frame and defaults to the selected frame.
1867 SIDE can be any of the symbols `left', `top', `right' or
1868 `bottom'. The default value nil is handled like `bottom'."
1869 (setq frame (window-normalize-frame frame))
1870 (let (windows)
1871 (walk-window-tree
1872 (lambda (window)
1873 (when (window-at-side-p window side)
1874 (setq windows (cons window windows))))
1875 frame nil 'nomini)
1876 (nreverse windows)))
1878 (defun window--in-direction-2 (window posn &optional horizontal)
1879 "Support function for `window-in-direction'."
1880 (if horizontal
1881 (let ((top (window-pixel-top window)))
1882 (if (> top posn)
1883 (- top posn)
1884 (- posn top (window-pixel-height window))))
1885 (let ((left (window-pixel-left window)))
1886 (if (> left posn)
1887 (- left posn)
1888 (- posn left (window-pixel-width window))))))
1890 ;; Predecessors to the below have been devised by Julian Assange in
1891 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
1892 ;; Neither of these allow to selectively ignore specific windows
1893 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
1894 ;; the movement.
1895 (defun window-in-direction (direction &optional window ignore sign wrap mini)
1896 "Return window in DIRECTION as seen from WINDOW.
1897 More precisely, return the nearest window in direction DIRECTION
1898 as seen from the position of `window-point' in window WINDOW.
1899 DIRECTION must be one of `above', `below', `left' or `right'.
1900 WINDOW must be a live window and defaults to the selected one.
1902 Do not return a window whose `no-other-window' parameter is
1903 non-nil. If the nearest window's `no-other-window' parameter is
1904 non-nil, try to find another window in the indicated direction.
1905 If, however, the optional argument IGNORE is non-nil, return that
1906 window even if its `no-other-window' parameter is non-nil.
1908 Optional argument SIGN a negative number means to use the right
1909 or bottom edge of WINDOW as reference position instead of
1910 `window-point'. SIGN a positive number means to use the left or
1911 top edge of WINDOW as reference position.
1913 Optional argument WRAP non-nil means to wrap DIRECTION around
1914 frame borders. This means to return for WINDOW at the top of the
1915 frame and DIRECTION `above' the minibuffer window if the frame
1916 has one, and a window at the bottom of the frame otherwise.
1918 Optional argument MINI nil means to return the minibuffer window
1919 if and only if it is currently active. MINI non-nil means to
1920 return the minibuffer window even when it's not active. However,
1921 if WRAP non-nil, always act as if MINI were nil.
1923 Return nil if no suitable window can be found."
1924 (setq window (window-normalize-window window t))
1925 (unless (memq direction '(above below left right))
1926 (error "Wrong direction %s" direction))
1927 (let* ((frame (window-frame window))
1928 (hor (memq direction '(left right)))
1929 (first (if hor
1930 (window-pixel-left window)
1931 (window-pixel-top window)))
1932 (last (+ first (window-size window hor t)))
1933 ;; The column / row value of `posn-at-point' can be nil for the
1934 ;; mini-window, guard against that.
1935 (posn
1936 (cond
1937 ((and (numberp sign) (< sign 0))
1938 (if hor
1939 (1- (+ (window-pixel-top window) (window-pixel-height window)))
1940 (1- (+ (window-pixel-left window) (window-pixel-width window)))))
1941 ((and (numberp sign) (> sign 0))
1942 (if hor
1943 (window-pixel-top window)
1944 (window-pixel-left window)))
1945 ((let ((posn-cons (nth 2 (posn-at-point (window-point window) window))))
1946 (if hor
1947 (+ (or (cdr posn-cons) 1) (window-pixel-top window))
1948 (+ (or (car posn-cons) 1) (window-pixel-left window)))))))
1949 (best-edge
1950 (cond
1951 ((eq direction 'below) (frame-pixel-height frame))
1952 ((eq direction 'right) (frame-pixel-width frame))
1953 (t -1)))
1954 (best-edge-2 best-edge)
1955 (best-diff-2 (if hor (frame-pixel-height frame) (frame-pixel-width frame)))
1956 best best-2 best-diff-2-new)
1957 (walk-window-tree
1958 (lambda (w)
1959 (let* ((w-top (window-pixel-top w))
1960 (w-left (window-pixel-left w)))
1961 (cond
1962 ((or (eq window w)
1963 ;; Ignore ourselves.
1964 (and (window-parameter w 'no-other-window)
1965 ;; Ignore W unless IGNORE is non-nil.
1966 (not ignore))))
1967 (hor
1968 (cond
1969 ((and (<= w-top posn)
1970 (< posn (+ w-top (window-pixel-height w))))
1971 ;; W is to the left or right of WINDOW and covers POSN.
1972 (when (or (and (eq direction 'left)
1973 (or (and (<= w-left first) (> w-left best-edge))
1974 (and wrap
1975 (window-at-side-p window 'left)
1976 (window-at-side-p w 'right))))
1977 (and (eq direction 'right)
1978 (or (and (>= w-left last) (< w-left best-edge))
1979 (and wrap
1980 (window-at-side-p window 'right)
1981 (window-at-side-p w 'left)))))
1982 (setq best-edge w-left)
1983 (setq best w)))
1984 ((and (or (and (eq direction 'left)
1985 (<= (+ w-left (window-pixel-width w)) first))
1986 (and (eq direction 'right) (<= last w-left)))
1987 ;; W is to the left or right of WINDOW but does not
1988 ;; cover POSN.
1989 (setq best-diff-2-new
1990 (window--in-direction-2 w posn hor))
1991 (or (< best-diff-2-new best-diff-2)
1992 (and (= best-diff-2-new best-diff-2)
1993 (if (eq direction 'left)
1994 (> w-left best-edge-2)
1995 (< w-left best-edge-2)))))
1996 (setq best-edge-2 w-left)
1997 (setq best-diff-2 best-diff-2-new)
1998 (setq best-2 w))))
1999 ((and (<= w-left posn)
2000 (< posn (+ w-left (window-pixel-width w))))
2001 ;; W is above or below WINDOW and covers POSN.
2002 (when (or (and (eq direction 'above)
2003 (or (and (<= w-top first) (> w-top best-edge))
2004 (and wrap
2005 (window-at-side-p window 'top)
2006 (if (active-minibuffer-window)
2007 (minibuffer-window-active-p w)
2008 (window-at-side-p w 'bottom)))))
2009 (and (eq direction 'below)
2010 (or (and (>= w-top first) (< w-top best-edge))
2011 (and wrap
2012 (if (active-minibuffer-window)
2013 (minibuffer-window-active-p window)
2014 (window-at-side-p window 'bottom))
2015 (window-at-side-p w 'top)))))
2016 (setq best-edge w-top)
2017 (setq best w)))
2018 ((and (or (and (eq direction 'above)
2019 (<= (+ w-top (window-pixel-height w)) first))
2020 (and (eq direction 'below) (<= last w-top)))
2021 ;; W is above or below WINDOW but does not cover POSN.
2022 (setq best-diff-2-new
2023 (window--in-direction-2 w posn hor))
2024 (or (< best-diff-2-new best-diff-2)
2025 (and (= best-diff-2-new best-diff-2)
2026 (if (eq direction 'above)
2027 (> w-top best-edge-2)
2028 (< w-top best-edge-2)))))
2029 (setq best-edge-2 w-top)
2030 (setq best-diff-2 best-diff-2-new)
2031 (setq best-2 w)))))
2032 frame nil (and mini t))
2033 (or best best-2)))
2035 (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
2036 "Return a live window satisfying PREDICATE.
2037 More precisely, cycle through all windows calling the function
2038 PREDICATE on each one of them with the window as its sole
2039 argument. Return the first window for which PREDICATE returns
2040 non-nil. Windows are scanned starting with the window following
2041 the selected window. If no window satisfies PREDICATE, return
2042 DEFAULT.
2044 MINIBUF t means include the minibuffer window even if the
2045 minibuffer is not active. MINIBUF nil or omitted means include
2046 the minibuffer window only if the minibuffer is active. Any
2047 other value means do not include the minibuffer window even if
2048 the minibuffer is active.
2050 ALL-FRAMES nil or omitted means consider all windows on the selected
2051 frame, plus the minibuffer window if specified by the MINIBUF
2052 argument. If the minibuffer counts, consider all windows on all
2053 frames that share that minibuffer too. The following non-nil
2054 values of ALL-FRAMES have special meanings:
2056 - t means consider all windows on all existing frames.
2058 - `visible' means consider all windows on all visible frames on
2059 the current terminal.
2061 - 0 (the number zero) means consider all windows on all visible
2062 and iconified frames on the current terminal.
2064 - A frame means consider all windows on that frame only.
2066 Anything else means consider all windows on the selected frame
2067 and no others."
2068 (catch 'found
2069 (dolist (window (window-list-1
2070 (next-window nil minibuf all-frames)
2071 minibuf all-frames))
2072 (when (funcall predicate window)
2073 (throw 'found window)))
2074 default))
2076 (defalias 'some-window 'get-window-with-predicate)
2078 (defun get-lru-window (&optional all-frames dedicated not-selected)
2079 "Return the least recently used window on frames specified by ALL-FRAMES.
2080 Return a full-width window if possible. A minibuffer window is
2081 never a candidate. A dedicated window is never a candidate
2082 unless DEDICATED is non-nil, so if all windows are dedicated, the
2083 value is nil. Avoid returning the selected window if possible.
2084 Optional argument NOT-SELECTED non-nil means never return the
2085 selected window.
2087 The following non-nil values of the optional argument ALL-FRAMES
2088 have special meanings:
2090 - t means consider all windows on all existing frames.
2092 - `visible' means consider all windows on all visible frames on
2093 the current terminal.
2095 - 0 (the number zero) means consider all windows on all visible
2096 and iconified frames on the current terminal.
2098 - A frame means consider all windows on that frame only.
2100 Any other value of ALL-FRAMES means consider all windows on the
2101 selected frame and no others."
2102 (let (best-window best-time second-best-window second-best-time time)
2103 (dolist (window (window-list-1 nil 'nomini all-frames))
2104 (when (and (or dedicated (not (window-dedicated-p window)))
2105 (or (not not-selected) (not (eq window (selected-window)))))
2106 (setq time (window-use-time window))
2107 (if (or (eq window (selected-window))
2108 (not (window-full-width-p window)))
2109 (when (or (not second-best-time) (< time second-best-time))
2110 (setq second-best-time time)
2111 (setq second-best-window window))
2112 (when (or (not best-time) (< time best-time))
2113 (setq best-time time)
2114 (setq best-window window)))))
2115 (or best-window second-best-window)))
2117 (defun get-mru-window (&optional all-frames dedicated not-selected)
2118 "Return the most recently used window on frames specified by ALL-FRAMES.
2119 A minibuffer window is never a candidate. A dedicated window is
2120 never a candidate unless DEDICATED is non-nil, so if all windows
2121 are dedicated, the value is nil. Optional argument NOT-SELECTED
2122 non-nil means never return the selected window.
2124 The following non-nil values of the optional argument ALL-FRAMES
2125 have special meanings:
2127 - t means consider all windows on all existing frames.
2129 - `visible' means consider all windows on all visible frames on
2130 the current terminal.
2132 - 0 (the number zero) means consider all windows on all visible
2133 and iconified frames on the current terminal.
2135 - A frame means consider all windows on that frame only.
2137 Any other value of ALL-FRAMES means consider all windows on the
2138 selected frame and no others."
2139 (let (best-window best-time time)
2140 (dolist (window (window-list-1 nil 'nomini all-frames))
2141 (setq time (window-use-time window))
2142 (when (and (or dedicated (not (window-dedicated-p window)))
2143 (or (not not-selected) (not (eq window (selected-window))))
2144 (or (not best-time) (> time best-time)))
2145 (setq best-time time)
2146 (setq best-window window)))
2147 best-window))
2149 (defun get-largest-window (&optional all-frames dedicated not-selected)
2150 "Return the largest window on frames specified by ALL-FRAMES.
2151 A minibuffer window is never a candidate. A dedicated window is
2152 never a candidate unless DEDICATED is non-nil, so if all windows
2153 are dedicated, the value is nil. Optional argument NOT-SELECTED
2154 non-nil means never return the selected window.
2156 The following non-nil values of the optional argument ALL-FRAMES
2157 have special meanings:
2159 - t means consider all windows on all existing frames.
2161 - `visible' means consider all windows on all visible frames on
2162 the current terminal.
2164 - 0 (the number zero) means consider all windows on all visible
2165 and iconified frames on the current terminal.
2167 - A frame means consider all windows on that frame only.
2169 Any other value of ALL-FRAMES means consider all windows on the
2170 selected frame and no others."
2171 (let ((best-size 0)
2172 best-window size)
2173 (dolist (window (window-list-1 nil 'nomini all-frames))
2174 (when (and (or dedicated (not (window-dedicated-p window)))
2175 (or (not not-selected) (not (eq window (selected-window)))))
2176 (setq size (* (window-pixel-height window)
2177 (window-pixel-width window)))
2178 (when (> size best-size)
2179 (setq best-size size)
2180 (setq best-window window))))
2181 best-window))
2183 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
2184 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2185 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2186 and defaults to the current buffer. Windows are scanned starting
2187 with the selected window.
2189 MINIBUF t means include the minibuffer window even if the
2190 minibuffer is not active. MINIBUF nil or omitted means include
2191 the minibuffer window only if the minibuffer is active. Any
2192 other value means do not include the minibuffer window even if
2193 the minibuffer is active.
2195 ALL-FRAMES nil or omitted means consider all windows on the
2196 selected frame, plus the minibuffer window if specified by the
2197 MINIBUF argument. If the minibuffer counts, consider all windows
2198 on all frames that share that minibuffer too. The following
2199 non-nil values of ALL-FRAMES have special meanings:
2201 - t means consider all windows on all existing frames.
2203 - `visible' means consider all windows on all visible frames on
2204 the current terminal.
2206 - 0 (the number zero) means consider all windows on all visible
2207 and iconified frames on the current terminal.
2209 - A frame means consider all windows on that frame only.
2211 Anything else means consider all windows on the selected frame
2212 and no others."
2213 (let ((buffer (window-normalize-buffer buffer-or-name))
2214 windows)
2215 (dolist (window (window-list-1 (selected-window) minibuf all-frames))
2216 (when (eq (window-buffer window) buffer)
2217 (setq windows (cons window windows))))
2218 (nreverse windows)))
2220 (defun minibuffer-window-active-p (window)
2221 "Return t if WINDOW is the currently active minibuffer window."
2222 (eq window (active-minibuffer-window)))
2224 (defun count-windows (&optional minibuf)
2225 "Return the number of live windows on the selected frame.
2226 The optional argument MINIBUF specifies whether the minibuffer
2227 window shall be counted. See `walk-windows' for the precise
2228 meaning of this argument."
2229 (length (window-list-1 nil minibuf)))
2231 ;;; Resizing windows.
2232 (defun window--size-to-pixel (window size &optional horizontal pixelwise round-maybe)
2233 "For WINDOW convert SIZE lines to pixels.
2234 SIZE is supposed to specify a height of WINDOW in terms of text
2235 lines. The return value is the number of pixels specifying that
2236 height.
2238 WINDOW must be a valid window. Optional argument HORIZONTAL
2239 non-nil means convert SIZE columns to pixels.
2241 Optional argument PIXELWISE non-nil means SIZE already specifies
2242 pixels but may have to be adjusted to a multiple of the character
2243 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2244 means round to the nearest multiple of the character size of
2245 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2246 (setq window (window-normalize-window window))
2247 (let ((char-size (frame-char-size window horizontal)))
2248 (if pixelwise
2249 (if (and round-maybe (not window-resize-pixelwise))
2250 (* (round size char-size) char-size)
2251 size)
2252 (* size char-size))))
2254 (defun window--pixel-to-total-1 (window horizontal char-size)
2255 "Subroutine of `window--pixel-to-total'."
2256 (let ((child (window-child window)))
2257 (if (window-combination-p window horizontal)
2258 ;; In an iso-combination distribute sizes proportionally.
2259 (let ((remainder (window-new-total window))
2260 size best-child rem best-rem)
2261 ;; Initialize total sizes to each child's floor.
2262 (while child
2263 (setq size (max (/ (window-size child horizontal t) char-size) 1))
2264 (set-window-new-total child size)
2265 (setq remainder (- remainder size))
2266 (setq child (window-next-sibling child)))
2267 ;; Distribute remainder.
2268 (while (> remainder 0)
2269 (setq child (window-last-child window))
2270 (setq best-child nil)
2271 (setq best-rem 0)
2272 (while child
2273 (when (and (<= (window-new-total child)
2274 (/ (window-size child horizontal t) char-size))
2275 (> (setq rem (% (window-size child horizontal t)
2276 char-size))
2277 best-rem))
2278 (setq best-child child)
2279 (setq best-rem rem))
2280 (setq child (window-prev-sibling child)))
2281 ;; We MUST have a best-child here.
2282 (set-window-new-total best-child 1 t)
2283 (setq remainder (1- remainder)))
2284 ;; Recurse.
2285 (setq child (window-child window))
2286 (while child
2287 (window--pixel-to-total-1 child horizontal char-size)
2288 (setq child (window-next-sibling child))))
2289 ;; In an ortho-combination assign new sizes directly.
2290 (let ((size (window-new-total window)))
2291 (while child
2292 (set-window-new-total child size)
2293 (window--pixel-to-total-1 child horizontal char-size)
2294 (setq child (window-next-sibling child)))))))
2296 (defun window--pixel-to-total (&optional frame horizontal)
2297 "On FRAME assign new total window heights from pixel heights.
2298 FRAME must be a live frame and defaults to the selected frame.
2300 Optional argument HORIZONTAL non-nil means assign new total
2301 window widths from pixel widths."
2302 (setq frame (window-normalize-frame frame))
2303 (let* ((char-size (frame-char-size frame horizontal))
2304 (root (frame-root-window frame))
2305 (root-size (window-size root horizontal t))
2306 ;; We have to care about the minibuffer window only if it
2307 ;; appears together with the root window on this frame.
2308 (mini (let ((mini (minibuffer-window frame)))
2309 (and (eq (window-frame mini) frame)
2310 (not (eq mini root)) mini)))
2311 (mini-size (and mini (window-size mini horizontal t))))
2312 ;; We round the line/column sizes of windows here to the nearest
2313 ;; integer. In some cases this can make windows appear _larger_
2314 ;; than the containing frame (line/column-wise) because the latter's
2315 ;; sizes are not (yet) rounded. We might eventually fix that.
2316 (if (and mini (not horizontal))
2317 (let (lines)
2318 (set-window-new-total root (max (/ root-size char-size) 1))
2319 (set-window-new-total mini (max (/ mini-size char-size) 1))
2320 (setq lines (- (round (+ root-size mini-size) char-size)
2321 (+ (window-new-total root) (window-new-total mini))))
2322 (while (> lines 0)
2323 (if (>= (% root-size (window-new-total root))
2324 (% mini-size (window-new-total mini)))
2325 (set-window-new-total root 1 t)
2326 (set-window-new-total mini 1 t))
2327 (setq lines (1- lines))))
2328 (set-window-new-total root (round root-size char-size))
2329 (when mini
2330 ;; This is taken in the horizontal case only.
2331 (set-window-new-total mini (round mini-size char-size))))
2332 (unless (window-buffer root)
2333 (window--pixel-to-total-1 root horizontal char-size))
2334 ;; Apply the new sizes.
2335 (window-resize-apply-total frame horizontal)))
2337 (defun window--resize-reset (&optional frame horizontal)
2338 "Reset resize values for all windows on FRAME.
2339 FRAME defaults to the selected frame.
2341 This function stores the current value of `window-size' applied
2342 with argument HORIZONTAL in the new total size of all windows on
2343 FRAME. It also resets the new normal size of each of these
2344 windows."
2345 (window--resize-reset-1
2346 (frame-root-window (window-normalize-frame frame)) horizontal))
2348 (defun window--resize-reset-1 (window horizontal)
2349 "Internal function of `window--resize-reset'."
2350 ;; Register old size in the new total size.
2351 (set-window-new-pixel window (window-size window horizontal t))
2352 (set-window-new-total window (window-size window horizontal))
2353 ;; Reset new normal size.
2354 (set-window-new-normal window)
2355 (when (window-child window)
2356 (window--resize-reset-1 (window-child window) horizontal))
2357 (when (window-right window)
2358 (window--resize-reset-1 (window-right window) horizontal)))
2360 ;; The following routine is used to manually resize the minibuffer
2361 ;; window and is currently used, for example, by ispell.el.
2362 (defun window--resize-mini-window (window delta)
2363 "Resize minibuffer window WINDOW by DELTA pixels.
2364 If WINDOW cannot be resized by DELTA pixels make it as large (or
2365 as small) as possible, but don't signal an error."
2366 (when (window-minibuffer-p window)
2367 (let* ((frame (window-frame window))
2368 (root (frame-root-window frame))
2369 (height (window-pixel-height window))
2370 (min-delta
2371 (- (window-pixel-height root)
2372 (window-min-size root nil nil t))))
2373 ;; Sanitize DELTA.
2374 (cond
2375 ((<= (+ height delta) 0)
2376 (setq delta (- (frame-char-height (window-frame window)) height)))
2377 ((> delta min-delta)
2378 (setq delta min-delta)))
2380 (unless (zerop delta)
2381 ;; Resize now.
2382 (window--resize-reset frame)
2383 ;; Ideally we should be able to resize just the last child of root
2384 ;; here. See the comment in `resize-root-window-vertically' for
2385 ;; why we do not do that.
2386 (window--resize-this-window root (- delta) nil nil t)
2387 (set-window-new-pixel window (+ height delta))
2388 ;; The following routine catches the case where we want to resize
2389 ;; a minibuffer-only frame.
2390 (when (resize-mini-window-internal window)
2391 (window--pixel-to-total frame)
2392 (run-window-configuration-change-hook frame))))))
2394 (defun window--resize-apply-p (frame &optional horizontal)
2395 "Return t when a window on FRAME shall be resized vertically.
2396 Optional argument HORIZONTAL non-nil means return t when a window
2397 shall be resized horizontally."
2398 (catch 'apply
2399 (walk-window-tree
2400 (lambda (window)
2401 (unless (= (window-new-pixel window)
2402 (window-size window horizontal t))
2403 (throw 'apply t)))
2404 frame t)
2405 nil))
2407 (defun window-resize (window delta &optional horizontal ignore pixelwise)
2408 "Resize WINDOW vertically by DELTA lines.
2409 WINDOW can be an arbitrary window and defaults to the selected
2410 one. An attempt to resize the root window of a frame will raise
2411 an error though.
2413 DELTA a positive number means WINDOW shall be enlarged by DELTA
2414 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2415 lines.
2417 Optional argument HORIZONTAL non-nil means resize WINDOW
2418 horizontally by DELTA columns. In this case a positive DELTA
2419 means enlarge WINDOW by DELTA columns. DELTA negative means
2420 WINDOW shall be shrunk by -DELTA columns.
2422 Optional argument IGNORE non-nil means ignore restrictions
2423 imposed by fixed size windows, `window-min-height' or
2424 `window-min-width' settings. If IGNORE is a window, ignore
2425 restrictions for that window only. If IGNORE equals `safe',
2426 live windows may get as small as `window-safe-min-height' lines
2427 and `window-safe-min-width' columns. Any other non-nil value
2428 means ignore all of the above restrictions for all windows.
2430 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2431 pixels.
2433 This function resizes other windows proportionally and never
2434 deletes any windows. If you want to move only the low (right)
2435 edge of WINDOW consider using `adjust-window-trailing-edge'
2436 instead."
2437 (setq window (window-normalize-window window))
2438 (let* ((frame (window-frame window))
2439 (minibuffer-window (minibuffer-window frame))
2440 sibling)
2441 (setq delta (window--size-to-pixel
2442 window delta horizontal pixelwise t))
2443 (cond
2444 ((eq window (frame-root-window frame))
2445 (error "Cannot resize the root window of a frame"))
2446 ((window-minibuffer-p window)
2447 (if horizontal
2448 (error "Cannot resize minibuffer window horizontally")
2449 (window--resize-mini-window window delta)))
2450 ((and (not horizontal)
2451 (window-full-height-p window)
2452 (eq (window-frame minibuffer-window) frame)
2453 (or (not resize-mini-windows)
2454 (eq minibuffer-window (active-minibuffer-window))))
2455 ;; If WINDOW is full height and either `resize-mini-windows' is
2456 ;; nil or the minibuffer window is active, resize the minibuffer
2457 ;; window.
2458 (window--resize-mini-window minibuffer-window (- delta)))
2459 ((window--resizable-p
2460 window delta horizontal ignore nil nil nil t)
2461 (window--resize-reset frame horizontal)
2462 (window--resize-this-window window delta horizontal ignore t)
2463 (if (and (not window-combination-resize)
2464 (window-combined-p window horizontal)
2465 (setq sibling (or (window-right window) (window-left window)))
2466 (window-sizable-p
2467 sibling (- delta) horizontal ignore t))
2468 ;; If window-combination-resize is nil, WINDOW is part of an
2469 ;; iso-combination, and WINDOW's neighboring right or left
2470 ;; sibling can be resized as requested, resize that sibling.
2471 (let ((normal-delta
2472 (/ (float delta)
2473 (window-size (window-parent window) horizontal t))))
2474 (window--resize-this-window sibling (- delta) horizontal nil t)
2475 (set-window-new-normal
2476 window (+ (window-normal-size window horizontal)
2477 normal-delta))
2478 (set-window-new-normal
2479 sibling (- (window-normal-size sibling horizontal)
2480 normal-delta)))
2481 ;; Otherwise, resize all other windows in the same combination.
2482 (window--resize-siblings window delta horizontal ignore))
2483 (when (window--resize-apply-p frame horizontal)
2484 (if (window-resize-apply frame horizontal)
2485 (progn
2486 (window--pixel-to-total frame horizontal)
2487 (run-window-configuration-change-hook frame))
2488 (error "Failed to apply resizing %s" window))))
2490 (error "Cannot resize window %s" window)))))
2492 (defun window-resize-no-error (window delta &optional horizontal ignore pixelwise)
2493 "Resize WINDOW vertically if it is resizable by DELTA lines.
2494 This function is like `window-resize' but does not signal an
2495 error when WINDOW cannot be resized. For the meaning of the
2496 optional arguments see the documentation of `window-resize'.
2498 Optional argument PIXELWISE non-nil means interpret DELTA as
2499 pixels."
2500 (when (window--resizable-p
2501 window delta horizontal ignore nil nil nil pixelwise)
2502 (window-resize window delta horizontal ignore pixelwise)))
2504 (defun window--resize-child-windows-skip-p (window)
2505 "Return non-nil if WINDOW shall be skipped by resizing routines."
2506 (memq (window-new-normal window) '(ignore stuck skip)))
2508 (defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
2509 "Recursively set new normal height of child windows of window PARENT.
2510 HORIZONTAL non-nil means set the new normal width of these
2511 windows. WINDOW specifies a child window of PARENT that has been
2512 resized by THIS-DELTA lines (columns).
2514 Optional argument TRAIL either `before' or `after' means set values
2515 only for windows before or after WINDOW. Optional argument
2516 OTHER-DELTA, a number, specifies that this many lines (columns)
2517 have been obtained from (or returned to) an ancestor window of
2518 PARENT in order to resize WINDOW."
2519 (let* ((delta-normal
2520 (if (and (= (- this-delta)
2521 (window-size window horizontal t))
2522 (zerop other-delta))
2523 ;; When WINDOW gets deleted and we can return its entire
2524 ;; space to its siblings, use WINDOW's normal size as the
2525 ;; normal delta.
2526 (- (window-normal-size window horizontal))
2527 ;; In any other case calculate the normal delta from the
2528 ;; relation of THIS-DELTA to the total size of PARENT.
2529 (/ (float this-delta)
2530 (window-size parent horizontal t))))
2531 (sub (window-child parent))
2532 (parent-normal 0.0)
2533 (skip (eq trail 'after)))
2535 ;; Set parent-normal to the sum of the normal sizes of all child
2536 ;; windows of PARENT that shall be resized, excluding only WINDOW
2537 ;; and any windows specified by the optional TRAIL argument.
2538 (while sub
2539 (cond
2540 ((eq sub window)
2541 (setq skip (eq trail 'before)))
2542 (skip)
2544 (setq parent-normal
2545 (+ parent-normal (window-normal-size sub horizontal)))))
2546 (setq sub (window-right sub)))
2548 ;; Set the new normal size of all child windows of PARENT from what
2549 ;; they should have contributed for recovering THIS-DELTA lines
2550 ;; (columns).
2551 (setq sub (window-child parent))
2552 (setq skip (eq trail 'after))
2553 (while sub
2554 (cond
2555 ((eq sub window)
2556 (setq skip (eq trail 'before)))
2557 (skip)
2559 (let ((old-normal (window-normal-size sub horizontal)))
2560 (set-window-new-normal
2561 sub (min 1.0 ; Don't get larger than 1.
2562 (max (- old-normal
2563 (* (/ old-normal parent-normal)
2564 delta-normal))
2565 ;; Don't drop below 0.
2566 0.0))))))
2567 (setq sub (window-right sub)))
2569 (when (numberp other-delta)
2570 ;; Set the new normal size of windows from what they should have
2571 ;; contributed for recovering OTHER-DELTA lines (columns).
2572 (setq delta-normal (/ (float (window-size parent horizontal t))
2573 (+ (window-size parent horizontal t)
2574 other-delta)))
2575 (setq sub (window-child parent))
2576 (setq skip (eq trail 'after))
2577 (while sub
2578 (cond
2579 ((eq sub window)
2580 (setq skip (eq trail 'before)))
2581 (skip)
2583 (set-window-new-normal
2584 sub (min 1.0 ; Don't get larger than 1.
2585 (max (* (window-new-normal sub) delta-normal)
2586 ;; Don't drop below 0.
2587 0.0)))))
2588 (setq sub (window-right sub))))
2590 ;; Set the new normal size of WINDOW to what is left by the sum of
2591 ;; the normal sizes of its siblings.
2592 (set-window-new-normal
2593 window
2594 (let ((sum 0))
2595 (setq sub (window-child parent))
2596 (while sub
2597 (cond
2598 ((eq sub window))
2599 ((not (numberp (window-new-normal sub)))
2600 (setq sum (+ sum (window-normal-size sub horizontal))))
2602 (setq sum (+ sum (window-new-normal sub)))))
2603 (setq sub (window-right sub)))
2604 ;; Don't get larger than 1 or smaller than 0.
2605 (min 1.0 (max (- 1.0 sum) 0.0))))))
2607 (defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge char-size)
2608 "Resize child windows of window PARENT vertically by DELTA pixels.
2609 PARENT must be a vertically combined internal window.
2611 Optional argument HORIZONTAL non-nil means resize child windows
2612 of PARENT horizontally by DELTA pixels. In this case PARENT must
2613 be a horizontally combined internal window.
2615 WINDOW, if specified, must denote a child window of PARENT that
2616 is resized by DELTA pixels.
2618 Optional argument IGNORE non-nil means ignore restrictions
2619 imposed by fixed size windows, `window-min-height' or
2620 `window-min-width' settings. If IGNORE equals `safe', live
2621 windows may get as small as `window-safe-min-height' lines and
2622 `window-safe-min-width' columns. If IGNORE is a window, ignore
2623 restrictions for that window only. Any other non-nil value means
2624 ignore all of the above restrictions for all windows.
2626 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2627 of windows that shall be resized. If TRAIL equals `before',
2628 resize only windows on the left or above EDGE. If TRAIL equals
2629 `after', resize only windows on the right or below EDGE. Also,
2630 preferably only resize windows adjacent to EDGE.
2632 If the optional argument CHAR-SIZE is a positive integer, it specifies
2633 the number of pixels by which windows are incrementally resized.
2634 If CHAR-SIZE is nil, this means to use the value of
2635 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2637 Return the symbol `normalized' if new normal sizes have been
2638 already set by this routine."
2639 (let* ((first (window-child parent))
2640 (last (window-last-child parent))
2641 (parent-total (+ (window-size parent horizontal t)
2642 delta))
2643 (char-size (or char-size
2644 (and window-resize-pixelwise 1)
2645 (frame-char-size window horizontal)))
2646 sub best-window best-value best-delta)
2648 (if (and edge (memq trail '(before after))
2649 (progn
2650 (setq sub first)
2651 (while (and (window-right sub)
2652 (or (and (eq trail 'before)
2653 (not (window--resize-child-windows-skip-p
2654 (window-right sub))))
2655 (and (eq trail 'after)
2656 (window--resize-child-windows-skip-p sub))))
2657 (setq sub (window-right sub)))
2658 sub)
2659 (if horizontal
2660 (if (eq trail 'before)
2661 (= (+ (window-pixel-left sub) (window-pixel-width sub))
2662 edge)
2663 (= (window-pixel-left sub) edge))
2664 (if (eq trail 'before)
2665 (= (+ (window-pixel-top sub) (window-pixel-height sub))
2666 edge)
2667 (= (window-pixel-top sub) edge)))
2668 (window-sizable-p sub delta horizontal ignore t))
2669 ;; Resize only windows adjacent to EDGE.
2670 (progn
2671 (window--resize-this-window
2672 sub delta horizontal ignore t trail edge)
2673 (if (and window (eq (window-parent sub) parent))
2674 (progn
2675 ;; Assign new normal sizes.
2676 (set-window-new-normal
2677 sub (/ (float (window-new-pixel sub)) parent-total))
2678 (set-window-new-normal
2679 window (- (window-normal-size window horizontal)
2680 (- (window-new-normal sub)
2681 (window-normal-size sub horizontal)))))
2682 (window--resize-child-windows-normal
2683 parent horizontal sub 0 trail delta))
2684 ;; Return 'normalized to notify `window--resize-siblings' that
2685 ;; normal sizes have been already set.
2686 'normalized)
2687 ;; Resize all windows proportionally.
2688 (setq sub last)
2689 (while sub
2690 (cond
2691 ((or (window--resize-child-windows-skip-p sub)
2692 ;; Ignore windows to skip and fixed-size child windows -
2693 ;; in the latter case make it a window to skip.
2694 (and (not ignore)
2695 (window-size-fixed-p sub horizontal)
2696 (set-window-new-normal sub 'ignore))))
2697 ((< delta 0)
2698 ;; When shrinking store the number of lines/cols we can get
2699 ;; from this window here together with the total/normal size
2700 ;; factor.
2701 (set-window-new-normal
2703 (cons
2704 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
2705 (window-min-delta sub horizontal ignore trail t nil t)
2706 (- (/ (float (window-size sub horizontal t))
2707 parent-total)
2708 (window-normal-size sub horizontal)))))
2709 ((> delta 0)
2710 ;; When enlarging store the total/normal size factor only
2711 (set-window-new-normal
2713 (- (/ (float (window-size sub horizontal t))
2714 parent-total)
2715 (window-normal-size sub horizontal)))))
2717 (setq sub (window-left sub)))
2719 (cond
2720 ((< delta 0)
2721 ;; Shrink windows by delta.
2722 (setq best-window t)
2723 (while (and best-window (not (zerop delta)))
2724 (setq sub last)
2725 (setq best-window nil)
2726 (setq best-value most-negative-fixnum)
2727 (while sub
2728 (when (and (consp (window-new-normal sub))
2729 (not (<= (car (window-new-normal sub)) 0))
2730 (> (cdr (window-new-normal sub)) best-value))
2731 (setq best-window sub)
2732 (setq best-value (cdr (window-new-normal sub))))
2734 (setq sub (window-left sub)))
2736 (when best-window
2737 (setq best-delta (min (car (window-new-normal best-window))
2738 char-size (- delta)))
2739 (setq delta (+ delta best-delta))
2740 (set-window-new-pixel best-window (- best-delta) t)
2741 (set-window-new-normal
2742 best-window
2743 (if (= (car (window-new-normal best-window)) best-delta)
2744 'skip ; We can't shrink best-window any further.
2745 (cons (- (car (window-new-normal best-window)) best-delta)
2746 (- (/ (float (window-new-pixel best-window))
2747 parent-total)
2748 (window-normal-size best-window horizontal))))))))
2749 ((> delta 0)
2750 ;; Enlarge windows by delta.
2751 (setq best-window t)
2752 (while (and best-window (not (zerop delta)))
2753 (setq sub last)
2754 (setq best-window nil)
2755 (setq best-value most-positive-fixnum)
2756 (while sub
2757 (when (and (numberp (window-new-normal sub))
2758 (< (window-new-normal sub) best-value))
2759 (setq best-window sub)
2760 (setq best-value (window-new-normal sub)))
2762 (setq sub (window-left sub)))
2764 (when best-window
2765 (setq best-delta (min delta char-size))
2766 (setq delta (- delta best-delta))
2767 (set-window-new-pixel best-window best-delta t)
2768 (set-window-new-normal
2769 best-window
2770 (- (/ (float (window-new-pixel best-window))
2771 parent-total)
2772 (window-normal-size best-window horizontal)))))))
2774 (when best-window
2775 (setq sub last)
2776 (while sub
2777 (when (or (consp (window-new-normal sub))
2778 (numberp (window-new-normal sub)))
2779 ;; Reset new normal size fields so `window-resize-apply'
2780 ;; won't use them to apply new sizes.
2781 (set-window-new-normal sub))
2783 (unless (eq (window-new-normal sub) 'ignore)
2784 ;; Resize this window's child windows (back-engineering
2785 ;; delta from sub's old and new total sizes).
2786 (let ((delta (- (window-new-pixel sub)
2787 (window-size sub horizontal t))))
2788 (unless (and (zerop delta) (not trail))
2789 ;; For the TRAIL non-nil case we have to resize SUB
2790 ;; recursively even if it's size does not change.
2791 (window--resize-this-window
2792 sub delta horizontal ignore nil trail edge))))
2793 (setq sub (window-left sub)))))))
2795 (defun window--resize-siblings (window delta &optional horizontal ignore trail edge char-size)
2796 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
2797 Optional argument HORIZONTAL non-nil means resize other windows
2798 when WINDOW is resized horizontally by DELTA pixels. WINDOW
2799 itself is not resized by this function.
2801 Optional argument IGNORE non-nil means ignore restrictions
2802 imposed by fixed size windows, `window-min-height' or
2803 `window-min-width' settings. If IGNORE equals `safe', live
2804 windows may get as small as `window-safe-min-height' lines and
2805 `window-safe-min-width' columns. If IGNORE is a window, ignore
2806 restrictions for that window only. Any other non-nil value means
2807 ignore all of the above restrictions for all windows.
2809 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2810 of windows that shall be resized. If TRAIL equals `before',
2811 resize only windows on the left or above EDGE. If TRAIL equals
2812 `after', resize only windows on the right or below EDGE. Also,
2813 preferably only resize windows adjacent to EDGE."
2814 (when (window-parent window)
2815 (let* ((parent (window-parent window))
2816 (sub (window-child parent)))
2817 (if (window-combined-p sub horizontal)
2818 ;; In an iso-combination try to extract DELTA from WINDOW's
2819 ;; siblings.
2820 (let ((skip (eq trail 'after))
2821 this-delta other-delta)
2822 ;; Decide which windows shall be left alone.
2823 (while sub
2824 (cond
2825 ((eq sub window)
2826 ;; Make sure WINDOW is left alone when
2827 ;; resizing its siblings.
2828 (set-window-new-normal sub 'ignore)
2829 (setq skip (eq trail 'before)))
2830 (skip
2831 ;; Make sure this sibling is left alone when
2832 ;; resizing its siblings.
2833 (set-window-new-normal sub 'ignore))
2834 ((or (window--size-ignore-p sub ignore)
2835 (not (window-size-fixed-p sub horizontal)))
2836 ;; Set this-delta to t to signal that we found a sibling
2837 ;; of WINDOW whose size is not fixed.
2838 (setq this-delta t)))
2840 (setq sub (window-right sub)))
2842 ;; Set this-delta to what we can get from WINDOW's siblings.
2843 (if (= (- delta) (window-size window horizontal t))
2844 ;; A deletion, presumably. We must handle this case
2845 ;; specially since `window--resizable' can't be used.
2846 (if this-delta
2847 ;; There's at least one resizable sibling we can
2848 ;; give WINDOW's size to.
2849 (setq this-delta delta)
2850 ;; No resizable sibling exists.
2851 (setq this-delta 0))
2852 ;; Any other form of resizing.
2853 (setq this-delta
2854 (window--resizable
2855 window delta horizontal ignore trail t nil t)))
2857 ;; Set other-delta to what we still have to get from
2858 ;; ancestor windows of parent.
2859 (setq other-delta (- delta this-delta))
2860 (unless (zerop other-delta)
2861 ;; Unless we got everything from WINDOW's siblings, PARENT
2862 ;; must be resized by other-delta lines or columns.
2863 (set-window-new-pixel parent other-delta 'add))
2865 (if (zerop this-delta)
2866 ;; We haven't got anything from WINDOW's siblings but we
2867 ;; must update the normal sizes to respect other-delta.
2868 (window--resize-child-windows-normal
2869 parent horizontal window this-delta trail other-delta)
2870 ;; We did get something from WINDOW's siblings which means
2871 ;; we have to resize their child windows.
2872 (unless (eq (window--resize-child-windows
2873 parent (- this-delta) horizontal
2874 window ignore trail edge char-size)
2875 ;; If `window--resize-child-windows' returns
2876 ;; 'normalized, this means it has set the
2877 ;; normal sizes already.
2878 'normalized)
2879 ;; Set the normal sizes.
2880 (window--resize-child-windows-normal
2881 parent horizontal window this-delta trail other-delta))
2882 ;; Set DELTA to what we still have to get from ancestor
2883 ;; windows.
2884 (setq delta other-delta)))
2886 ;; In an ortho-combination all siblings of WINDOW must be
2887 ;; resized by DELTA.
2888 (set-window-new-pixel parent delta 'add)
2889 (while sub
2890 (unless (eq sub window)
2891 (window--resize-this-window
2892 sub delta horizontal ignore t))
2893 (setq sub (window-right sub))))
2895 (unless (zerop delta)
2896 ;; "Go up."
2897 (window--resize-siblings
2898 parent delta horizontal ignore trail edge char-size)))))
2900 (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge char-size)
2901 "Resize WINDOW vertically by DELTA pixels.
2902 Optional argument HORIZONTAL non-nil means resize WINDOW
2903 horizontally by DELTA pixels.
2905 Optional argument IGNORE non-nil means ignore restrictions
2906 imposed by fixed size windows, `window-min-height' or
2907 `window-min-width' settings. If IGNORE equals `safe', live
2908 windows may get as small as `window-safe-min-height' lines and
2909 `window-safe-min-width' columns. If IGNORE is a window, ignore
2910 restrictions for that window only. Any other non-nil value
2911 means ignore all of the above restrictions for all windows.
2913 Optional argument ADD non-nil means add DELTA to the new total
2914 size of WINDOW.
2916 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2917 of windows that shall be resized. If TRAIL equals `before',
2918 resize only windows on the left or above EDGE. If TRAIL equals
2919 `after', resize only windows on the right or below EDGE. Also,
2920 preferably only resize windows adjacent to EDGE.
2922 If the optional argument CHAR-SIZE is a positive integer, it specifies
2923 the number of pixels by which windows are incrementally resized.
2924 If CHAR-SIZE is nil, this means to use the value of
2925 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2927 This function recursively resizes WINDOW's child windows to fit the
2928 new size. Make sure that WINDOW is `window--resizable' before
2929 calling this function. Note that this function does not resize
2930 siblings of WINDOW or WINDOW's parent window. You have to
2931 eventually call `window-resize-apply' in order to make resizing
2932 actually take effect."
2933 (when add
2934 ;; Add DELTA to the new total size of WINDOW.
2935 (set-window-new-pixel window delta t))
2937 (let ((sub (window-child window)))
2938 (cond
2939 ((not sub))
2940 ((window-combined-p sub horizontal)
2941 ;; In an iso-combination resize child windows according to their
2942 ;; normal sizes.
2943 (window--resize-child-windows
2944 window delta horizontal nil ignore trail edge char-size))
2945 ;; In an ortho-combination resize each child window by DELTA.
2947 (while sub
2948 (window--resize-this-window
2949 sub delta horizontal ignore t trail edge char-size)
2950 (setq sub (window-right sub)))))))
2952 (defun window--resize-root-window (window delta horizontal ignore pixelwise)
2953 "Resize root window WINDOW vertically by DELTA lines.
2954 HORIZONTAL non-nil means resize root window WINDOW horizontally
2955 by DELTA columns.
2957 IGNORE non-nil means ignore any restrictions imposed by fixed
2958 size windows, `window-min-height' or `window-min-width' settings.
2960 This function is only called by the frame resizing routines. It
2961 resizes windows proportionally and never deletes any windows."
2962 (when (and (windowp window) (numberp delta))
2963 (let ((pixel-delta
2964 (if pixelwise
2965 delta
2966 (window--size-to-pixel window delta horizontal))))
2967 (when (window-sizable-p window pixel-delta horizontal ignore t)
2968 (window--resize-reset (window-frame window) horizontal)
2969 (window--resize-this-window
2970 window pixel-delta horizontal ignore t)))))
2972 (defun window--resize-root-window-vertically (window delta pixelwise)
2973 "Resize root window WINDOW vertically by DELTA lines.
2974 If DELTA is less than zero and we can't shrink WINDOW by DELTA
2975 lines, shrink it as much as possible. If DELTA is greater than
2976 zero, this function can resize fixed-size windows in order to
2977 recover the necessary lines. Return the number of lines that
2978 were recovered.
2980 Third argument PIXELWISE non-nil means to interpret DELTA as
2981 pixels and return the number of pixels that were recovered.
2983 This function is called by the minibuffer window resizing
2984 routines."
2985 (let* ((frame (window-frame window))
2986 (pixel-delta
2987 (cond
2988 (pixelwise
2989 delta)
2990 ((numberp delta)
2991 (* (frame-char-height frame) delta))
2992 (t 0)))
2993 ignore)
2994 (cond
2995 ((zerop pixel-delta))
2996 ((< pixel-delta 0)
2997 (setq pixel-delta (window-sizable window pixel-delta nil nil pixelwise))
2998 (window--resize-reset frame)
2999 ;; When shrinking the root window, emulate an edge drag in order
3000 ;; to not resize other windows if we can avoid it (Bug#12419).
3001 (window--resize-this-window
3002 window pixel-delta nil ignore t 'before
3003 (+ (window-pixel-top window) (window-pixel-height window)))
3004 ;; Don't record new normal sizes to make sure that shrinking back
3005 ;; proportionally works as intended.
3006 (walk-window-tree
3007 (lambda (window) (set-window-new-normal window 'ignore)) frame t))
3008 ((> pixel-delta 0)
3009 (window--resize-reset frame)
3010 (unless (window-sizable window pixel-delta nil nil pixelwise)
3011 (setq ignore t))
3012 ;; When growing the root window, resize proportionally. This
3013 ;; should give windows back their original sizes (hopefully).
3014 (window--resize-this-window
3015 window pixel-delta nil ignore t)))
3016 ;; Return the possibly adjusted DELTA.
3017 (if pixelwise
3018 pixel-delta
3019 (/ pixel-delta (frame-char-height frame)))))
3021 (defun window--sanitize-window-sizes (frame horizontal)
3022 "Assert that all windows on FRAME are large enough.
3023 If necessary and possible, make sure that every window on frame
3024 FRAME has its minimum height. Optional argument HORIZONTAL
3025 non-nil means to make sure that every window on frame FRAME has
3026 its minimum width. The minimum height/width of a window is the
3027 respective value returned by `window-min-size' for that window.
3029 Return t if all windows were resized appropriately. Return nil
3030 if at least one window could not be resized as requested, which
3031 may happen when the FRAME is not large enough to accommodate it."
3032 (let ((value t))
3033 (walk-window-tree
3034 (lambda (window)
3035 (let ((delta (- (window-min-size window horizontal nil t)
3036 (window-size window horizontal t))))
3037 (when (> delta 0)
3038 (if (window-resizable-p window delta horizontal nil t)
3039 (window-resize window delta horizontal nil t)
3040 (setq value nil))))))
3041 value))
3043 (defun adjust-window-trailing-edge (window delta &optional horizontal pixelwise)
3044 "Move WINDOW's bottom edge by DELTA lines.
3045 Optional argument HORIZONTAL non-nil means move WINDOW's right
3046 edge by DELTA columns. WINDOW must be a valid window and
3047 defaults to the selected one.
3049 Optional argument PIXELWISE non-nil means interpret DELTA as
3050 number of pixels.
3052 If DELTA is greater than zero, move the edge downwards or to the
3053 right. If DELTA is less than zero, move the edge upwards or to
3054 the left. If the edge can't be moved by DELTA lines or columns,
3055 move it as far as possible in the desired direction."
3056 (setq window (window-normalize-window window))
3057 (let* ((frame (window-frame window))
3058 (minibuffer-window (minibuffer-window frame))
3059 (right window)
3060 left this-delta min-delta max-delta)
3062 (unless pixelwise
3063 (setq pixelwise t)
3064 (setq delta (* delta (frame-char-size window horizontal))))
3066 ;; Find the edge we want to move.
3067 (while (and (or (not (window-combined-p right horizontal))
3068 (not (window-right right)))
3069 (setq right (window-parent right))))
3070 (cond
3071 ((and (not right) (not horizontal)
3072 ;; Resize the minibuffer window if it's on the same frame as
3073 ;; and immediately below WINDOW and it's either active or
3074 ;; `resize-mini-windows' is nil.
3075 (eq (window-frame minibuffer-window) frame)
3076 (= (nth 1 (window-pixel-edges minibuffer-window))
3077 (nth 3 (window-pixel-edges window)))
3078 (or (not resize-mini-windows)
3079 (eq minibuffer-window (active-minibuffer-window))))
3080 (window--resize-mini-window minibuffer-window (- delta)))
3081 ((or (not (setq left right)) (not (setq right (window-right right))))
3082 (if horizontal
3083 (error "No window on the right of this one")
3084 (error "No window below this one")))
3086 ;; Set LEFT to the first resizable window on the left. This step is
3087 ;; needed to handle fixed-size windows.
3088 (while (and left (window-size-fixed-p left horizontal))
3089 (setq left
3090 (or (window-left left)
3091 (progn
3092 (while (and (setq left (window-parent left))
3093 (not (window-combined-p left horizontal))))
3094 (window-left left)))))
3095 (unless left
3096 (if horizontal
3097 (error "No resizable window on the left of this one")
3098 (error "No resizable window above this one")))
3100 ;; Set RIGHT to the first resizable window on the right. This step
3101 ;; is needed to handle fixed-size windows.
3102 (while (and right (window-size-fixed-p right horizontal))
3103 (setq right
3104 (or (window-right right)
3105 (progn
3106 (while (and (setq right (window-parent right))
3107 (not (window-combined-p right horizontal))))
3108 (window-right right)))))
3109 (unless right
3110 (if horizontal
3111 (error "No resizable window on the right of this one")
3112 (error "No resizable window below this one")))
3114 ;; LEFT and RIGHT (which might be both internal windows) are now the
3115 ;; two windows we want to resize.
3116 (cond
3117 ((> delta 0)
3118 (setq max-delta
3119 (window--max-delta-1
3120 left 0 horizontal nil 'after nil pixelwise))
3121 (setq min-delta
3122 (window--min-delta-1
3123 right (- delta) horizontal nil 'before nil pixelwise))
3124 (when (or (< max-delta delta) (> min-delta (- delta)))
3125 ;; We can't get the whole DELTA - move as far as possible.
3126 (setq delta (min max-delta (- min-delta))))
3127 (unless (zerop delta)
3128 ;; Start resizing.
3129 (window--resize-reset frame horizontal)
3130 ;; Try to enlarge LEFT first.
3131 (setq this-delta (window--resizable
3132 left delta horizontal nil 'after nil nil pixelwise))
3133 (unless (zerop this-delta)
3134 (window--resize-this-window
3135 left this-delta horizontal nil t 'before
3136 (if horizontal
3137 (+ (window-pixel-left left) (window-pixel-width left))
3138 (+ (window-pixel-top left) (window-pixel-height left)))))
3139 ;; Shrink windows on right of LEFT.
3140 (window--resize-siblings
3141 left delta horizontal nil 'after
3142 (if horizontal
3143 (window-pixel-left right)
3144 (window-pixel-top right)))))
3145 ((< delta 0)
3146 (setq max-delta
3147 (window--max-delta-1
3148 right 0 horizontal nil 'before nil pixelwise))
3149 (setq min-delta
3150 (window--min-delta-1
3151 left delta horizontal nil 'after nil pixelwise))
3152 (when (or (< max-delta (- delta)) (> min-delta delta))
3153 ;; We can't get the whole DELTA - move as far as possible.
3154 (setq delta (max (- max-delta) min-delta)))
3155 (unless (zerop delta)
3156 ;; Start resizing.
3157 (window--resize-reset frame horizontal)
3158 ;; Try to enlarge RIGHT.
3159 (setq this-delta
3160 (window--resizable
3161 right (- delta) horizontal nil 'before nil nil pixelwise))
3162 (unless (zerop this-delta)
3163 (window--resize-this-window
3164 right this-delta horizontal nil t 'after
3165 (if horizontal
3166 (window-pixel-left right)
3167 (window-pixel-top right))))
3168 ;; Shrink windows on left of RIGHT.
3169 (window--resize-siblings
3170 right (- delta) horizontal nil 'before
3171 (if horizontal
3172 (+ (window-pixel-left left) (window-pixel-width left))
3173 (+ (window-pixel-top left) (window-pixel-height left)))))))
3174 (unless (zerop delta)
3175 ;; Don't report an error in the standard case.
3176 (when (window--resize-apply-p frame horizontal)
3177 (if (window-resize-apply frame horizontal)
3178 (progn
3179 (window--pixel-to-total frame horizontal)
3180 (run-window-configuration-change-hook frame))
3181 ;; But do report an error if applying the changes fails.
3182 (error "Failed adjusting window %s" window))))))))
3184 (defun enlarge-window (delta &optional horizontal)
3185 "Make the selected window DELTA lines taller.
3186 Interactively, if no argument is given, make the selected window
3187 one line taller. If optional argument HORIZONTAL is non-nil,
3188 make selected window wider by DELTA columns. If DELTA is
3189 negative, shrink selected window by -DELTA lines or columns."
3190 (interactive "p")
3191 (let ((minibuffer-window (minibuffer-window)))
3192 (cond
3193 ((zerop delta))
3194 ((window-size-fixed-p nil horizontal)
3195 (error "Selected window has fixed size"))
3196 ((window-minibuffer-p)
3197 (if horizontal
3198 (error "Cannot resize minibuffer window horizontally")
3199 (window--resize-mini-window (selected-window) delta)))
3200 ((and (not horizontal)
3201 (window-full-height-p)
3202 (eq (window-frame minibuffer-window) (selected-frame))
3203 (not resize-mini-windows))
3204 ;; If the selected window is full height and `resize-mini-windows'
3205 ;; is nil, resize the minibuffer window.
3206 (window--resize-mini-window minibuffer-window (- delta)))
3207 ((window--resizable-p nil delta horizontal)
3208 (window-resize nil delta horizontal))
3210 (window-resize
3211 nil (if (> delta 0)
3212 (window-max-delta nil horizontal)
3213 (- (window-min-delta nil horizontal)))
3214 horizontal)))))
3216 (defun shrink-window (delta &optional horizontal)
3217 "Make the selected window DELTA lines smaller.
3218 Interactively, if no argument is given, make the selected window
3219 one line smaller. If optional argument HORIZONTAL is non-nil,
3220 make selected window narrower by DELTA columns. If DELTA is
3221 negative, enlarge selected window by -DELTA lines or columns.
3222 Also see the `window-min-height' variable."
3223 (interactive "p")
3224 (let ((minibuffer-window (minibuffer-window)))
3225 (cond
3226 ((zerop delta))
3227 ((window-size-fixed-p nil horizontal)
3228 (error "Selected window has fixed size"))
3229 ((window-minibuffer-p)
3230 (if horizontal
3231 (error "Cannot resize minibuffer window horizontally")
3232 (window--resize-mini-window (selected-window) (- delta))))
3233 ((and (not horizontal)
3234 (window-full-height-p)
3235 (eq (window-frame minibuffer-window) (selected-frame))
3236 (not resize-mini-windows))
3237 ;; If the selected window is full height and `resize-mini-windows'
3238 ;; is nil, resize the minibuffer window.
3239 (window--resize-mini-window minibuffer-window delta))
3240 ((window--resizable-p nil (- delta) horizontal)
3241 (window-resize nil (- delta) horizontal))
3243 (window-resize
3244 nil (if (> delta 0)
3245 (- (window-min-delta nil horizontal))
3246 (window-max-delta nil horizontal))
3247 horizontal)))))
3249 (defun maximize-window (&optional window)
3250 "Maximize WINDOW.
3251 Make WINDOW as large as possible without deleting any windows.
3252 WINDOW must be a valid window and defaults to the selected one.
3254 If the option `window-resize-pixelwise' is non-nil maximize
3255 WINDOW pixelwise."
3256 (interactive)
3257 (setq window (window-normalize-window window))
3258 (window-resize
3259 window (window-max-delta window nil nil nil nil nil window-resize-pixelwise)
3260 nil nil window-resize-pixelwise)
3261 (window-resize
3262 window (window-max-delta window t nil nil nil nil window-resize-pixelwise)
3263 t nil window-resize-pixelwise))
3265 (defun minimize-window (&optional window)
3266 "Minimize WINDOW.
3267 Make WINDOW as small as possible without deleting any windows.
3268 WINDOW must be a valid window and defaults to the selected one.
3270 If the option `window-resize-pixelwise' is non-nil minimize
3271 WINDOW pixelwise."
3272 (interactive)
3273 (setq window (window-normalize-window window))
3274 (window-resize
3275 window
3276 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise))
3277 nil nil window-resize-pixelwise)
3278 (window-resize
3279 window
3280 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise))
3281 t nil window-resize-pixelwise))
3283 (defun frame-root-window-p (window)
3284 "Return non-nil if WINDOW is the root window of its frame."
3285 (eq window (frame-root-window window)))
3287 (defun window--subtree (window &optional next)
3288 "Return window subtree rooted at WINDOW.
3289 Optional argument NEXT non-nil means include WINDOW's right
3290 siblings in the return value.
3292 See the documentation of `window-tree' for a description of the
3293 return value."
3294 (let (list)
3295 (while window
3296 (setq list
3297 (cons
3298 (cond
3299 ((window-top-child window)
3300 (cons t (cons (window-edges window)
3301 (window--subtree (window-top-child window) t))))
3302 ((window-left-child window)
3303 (cons nil (cons (window-edges window)
3304 (window--subtree (window-left-child window) t))))
3305 (t window))
3306 list))
3307 (setq window (when next (window-next-sibling window))))
3308 (nreverse list)))
3310 (defun window-tree (&optional frame)
3311 "Return the window tree of frame FRAME.
3312 FRAME must be a live frame and defaults to the selected frame.
3313 The return value is a list of the form (ROOT MINI), where ROOT
3314 represents the window tree of the frame's root window, and MINI
3315 is the frame's minibuffer window.
3317 If the root window is not split, ROOT is the root window itself.
3318 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3319 for a horizontal split, and t for a vertical split. EDGES gives
3320 the combined size and position of the child windows in the split,
3321 and the rest of the elements are the child windows in the split.
3322 Each of the child windows may again be a window or a list
3323 representing a window split, and so on. EDGES is a list (LEFT
3324 TOP RIGHT BOTTOM) as returned by `window-edges'."
3325 (setq frame (window-normalize-frame frame))
3326 (window--subtree (frame-root-window frame) t))
3328 (defun other-window (count &optional all-frames)
3329 "Select another window in cyclic ordering of windows.
3330 COUNT specifies the number of windows to skip, starting with the
3331 selected window, before making the selection. If COUNT is
3332 positive, skip COUNT windows forwards. If COUNT is negative,
3333 skip -COUNT windows backwards. COUNT zero means do not skip any
3334 window, so select the selected window. In an interactive call,
3335 COUNT is the numeric prefix argument. Return nil.
3337 If the `other-window' parameter of the selected window is a
3338 function and `ignore-window-parameters' is nil, call that
3339 function with the arguments COUNT and ALL-FRAMES.
3341 This function does not select a window whose `no-other-window'
3342 window parameter is non-nil.
3344 This function uses `next-window' for finding the window to
3345 select. The argument ALL-FRAMES has the same meaning as in
3346 `next-window', but the MINIBUF argument of `next-window' is
3347 always effectively nil."
3348 (interactive "p")
3349 (let* ((window (selected-window))
3350 (function (and (not ignore-window-parameters)
3351 (window-parameter window 'other-window)))
3352 old-window old-count)
3353 (if (functionp function)
3354 (funcall function count all-frames)
3355 ;; `next-window' and `previous-window' may return a window we are
3356 ;; not allowed to select. Hence we need an exit strategy in case
3357 ;; all windows are non-selectable.
3358 (catch 'exit
3359 (while (> count 0)
3360 (setq window (next-window window nil all-frames))
3361 (cond
3362 ((eq window old-window)
3363 (when (= count old-count)
3364 ;; Keep out of infinite loops. When COUNT has not changed
3365 ;; since we last looked at `window' we're probably in one.
3366 (throw 'exit nil)))
3367 ((window-parameter window 'no-other-window)
3368 (unless old-window
3369 ;; The first non-selectable window `next-window' got us:
3370 ;; Remember it and the current value of COUNT.
3371 (setq old-window window)
3372 (setq old-count count)))
3374 (setq count (1- count)))))
3375 (while (< count 0)
3376 (setq window (previous-window window nil all-frames))
3377 (cond
3378 ((eq window old-window)
3379 (when (= count old-count)
3380 ;; Keep out of infinite loops. When COUNT has not changed
3381 ;; since we last looked at `window' we're probably in one.
3382 (throw 'exit nil)))
3383 ((window-parameter window 'no-other-window)
3384 (unless old-window
3385 ;; The first non-selectable window `previous-window' got
3386 ;; us: Remember it and the current value of COUNT.
3387 (setq old-window window)
3388 (setq old-count count)))
3390 (setq count (1+ count)))))
3392 (select-window window)
3393 ;; Always return nil.
3394 nil))))
3396 ;; This should probably return non-nil when the selected window is part
3397 ;; of an atomic window whose root is the frame's root window.
3398 (defun one-window-p (&optional nomini all-frames)
3399 "Return non-nil if the selected window is the only window.
3400 Optional arg NOMINI non-nil means don't count the minibuffer
3401 even if it is active. Otherwise, the minibuffer is counted
3402 when it is active.
3404 Optional argument ALL-FRAMES specifies the set of frames to
3405 consider, see also `next-window'. ALL-FRAMES nil or omitted
3406 means consider windows on the selected frame only, plus the
3407 minibuffer window if specified by the NOMINI argument. If the
3408 minibuffer counts, consider all windows on all frames that share
3409 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3410 with a special meaning are:
3412 - t means consider all windows on all existing frames.
3414 - `visible' means consider all windows on all visible frames on
3415 the current terminal.
3417 - 0 (the number zero) means consider all windows on all visible
3418 and iconified frames on the current terminal.
3420 - A frame means consider all windows on that frame only.
3422 Anything else means consider all windows on the selected frame
3423 and no others."
3424 (let ((base-window (selected-window)))
3425 (if (and nomini (eq base-window (minibuffer-window)))
3426 (setq base-window (next-window base-window)))
3427 (eq base-window
3428 (next-window base-window (if nomini 'arg) all-frames))))
3430 ;;; Deleting windows.
3431 (defun window-deletable-p (&optional window)
3432 "Return t if WINDOW can be safely deleted from its frame.
3433 WINDOW must be a valid window and defaults to the selected one.
3434 Return 'frame if deleting WINDOW should also delete its frame."
3435 (setq window (window-normalize-window window))
3437 (unless (or ignore-window-parameters
3438 (eq (window-parameter window 'delete-window) t))
3439 ;; Handle atomicity.
3440 (when (window-parameter window 'window-atom)
3441 (setq window (window-atom-root window))))
3443 (let ((frame (window-frame window)))
3444 (cond
3445 ((frame-root-window-p window)
3446 ;; WINDOW's frame can be deleted only if there are other frames
3447 ;; on the same terminal, and it does not contain the active
3448 ;; minibuffer.
3449 (unless (or (eq frame (next-frame frame 0))
3450 ;; We can delete our frame only if no other frame
3451 ;; currently uses our minibuffer window.
3452 (catch 'other
3453 (dolist (other (frame-list))
3454 (when (and (not (eq other frame))
3455 (eq (window-frame (minibuffer-window other))
3456 frame))
3457 (throw 'other t))))
3458 (let ((minibuf (active-minibuffer-window)))
3459 (and minibuf (eq frame (window-frame minibuf)))))
3460 'frame))
3461 ((or ignore-window-parameters
3462 (not (eq window (window--major-non-side-window frame))))
3463 ;; WINDOW can be deleted unless it is the major non-side window of
3464 ;; its frame.
3465 t))))
3467 (defun window--in-subtree-p (window root)
3468 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
3469 (or (eq window root)
3470 (let ((parent (window-parent window)))
3471 (catch 'done
3472 (while parent
3473 (if (eq parent root)
3474 (throw 'done t)
3475 (setq parent (window-parent parent))))))))
3477 (defun delete-window (&optional window)
3478 "Delete WINDOW.
3479 WINDOW must be a valid window and defaults to the selected one.
3480 Return nil.
3482 If the variable `ignore-window-parameters' is non-nil or the
3483 `delete-window' parameter of WINDOW equals t, do not process any
3484 parameters of WINDOW. Otherwise, if the `delete-window'
3485 parameter of WINDOW specifies a function, call that function with
3486 WINDOW as its sole argument and return the value returned by that
3487 function.
3489 Otherwise, if WINDOW is part of an atomic window, call
3490 `delete-window' with the root of the atomic window as its
3491 argument. Signal an error if WINDOW is either the only window on
3492 its frame, the last non-side window, or part of an atomic window
3493 that is its frame's root window."
3494 (interactive)
3495 (setq window (window-normalize-window window))
3496 (let* ((frame (window-frame window))
3497 (function (window-parameter window 'delete-window))
3498 (parent (window-parent window))
3499 atom-root)
3500 (window--check frame)
3501 (catch 'done
3502 ;; Handle window parameters.
3503 (cond
3504 ;; Ignore window parameters if `ignore-window-parameters' tells
3505 ;; us so or `delete-window' equals t.
3506 ((or ignore-window-parameters (eq function t)))
3507 ((functionp function)
3508 ;; The `delete-window' parameter specifies the function to call.
3509 ;; If that function is `ignore' nothing is done. It's up to the
3510 ;; function called here to avoid infinite recursion.
3511 (throw 'done (funcall function window)))
3512 ((and (window-parameter window 'window-atom)
3513 (setq atom-root (window-atom-root window))
3514 (not (eq atom-root window)))
3515 (if (eq atom-root (frame-root-window frame))
3516 (error "Root of atomic window is root window of its frame")
3517 (throw 'done (delete-window atom-root))))
3518 ((not parent)
3519 (error "Attempt to delete minibuffer or sole ordinary window"))
3520 ((eq window (window--major-non-side-window frame))
3521 (error "Attempt to delete last non-side window")))
3523 (let* ((horizontal (window-left-child parent))
3524 (size (window-size window horizontal t))
3525 (frame-selected
3526 (window--in-subtree-p (frame-selected-window frame) window))
3527 ;; Emacs 23 preferably gives WINDOW's space to its left
3528 ;; sibling.
3529 (sibling (or (window-left window) (window-right window))))
3530 (window--resize-reset frame horizontal)
3531 (cond
3532 ((and (not window-combination-resize)
3533 sibling (window-sizable-p sibling size horizontal nil t))
3534 ;; Resize WINDOW's sibling.
3535 (window--resize-this-window sibling size horizontal nil t)
3536 (set-window-new-normal
3537 sibling (+ (window-normal-size sibling horizontal)
3538 (window-normal-size window horizontal))))
3539 ((window--resizable-p window (- size) horizontal nil nil nil t t)
3540 ;; Can do without resizing fixed-size windows.
3541 (window--resize-siblings window (- size) horizontal))
3543 ;; Can't do without resizing fixed-size windows.
3544 (window--resize-siblings window (- size) horizontal t)))
3545 ;; Actually delete WINDOW.
3546 (delete-window-internal window)
3547 (window--pixel-to-total frame horizontal)
3548 (when (and frame-selected
3549 (window-parameter
3550 (frame-selected-window frame) 'no-other-window))
3551 ;; `delete-window-internal' has selected a window that should
3552 ;; not be selected, fix this here.
3553 (other-window -1 frame))
3554 (run-window-configuration-change-hook frame)
3555 (window--check frame)
3556 ;; Always return nil.
3557 nil))))
3559 (defun delete-other-windows (&optional window)
3560 "Make WINDOW fill its frame.
3561 WINDOW must be a valid window and defaults to the selected one.
3562 Return nil.
3564 If the variable `ignore-window-parameters' is non-nil or the
3565 `delete-other-windows' parameter of WINDOW equals t, do not
3566 process any parameters of WINDOW. Otherwise, if the
3567 `delete-other-windows' parameter of WINDOW specifies a function,
3568 call that function with WINDOW as its sole argument and return
3569 the value returned by that function.
3571 Otherwise, if WINDOW is part of an atomic window, call this
3572 function with the root of the atomic window as its argument. If
3573 WINDOW is a non-side window, make WINDOW the only non-side window
3574 on the frame. Side windows are not deleted. If WINDOW is a side
3575 window signal an error."
3576 (interactive)
3577 (setq window (window-normalize-window window))
3578 (let* ((frame (window-frame window))
3579 (function (window-parameter window 'delete-other-windows))
3580 (window-side (window-parameter window 'window-side))
3581 atom-root side-main)
3582 (window--check frame)
3583 (catch 'done
3584 (cond
3585 ;; Ignore window parameters if `ignore-window-parameters' is t or
3586 ;; `delete-other-windows' is t.
3587 ((or ignore-window-parameters (eq function t)))
3588 ((functionp function)
3589 ;; The `delete-other-windows' parameter specifies the function
3590 ;; to call. If the function is `ignore' no windows are deleted.
3591 ;; It's up to the function called to avoid infinite recursion.
3592 (throw 'done (funcall function window)))
3593 ((and (window-parameter window 'window-atom)
3594 (setq atom-root (window-atom-root window))
3595 (not (eq atom-root window)))
3596 (if (eq atom-root (frame-root-window frame))
3597 (error "Root of atomic window is root window of its frame")
3598 (throw 'done (delete-other-windows atom-root))))
3599 ((memq window-side window-sides)
3600 (error "Cannot make side window the only window"))
3601 ((and (window-minibuffer-p window)
3602 (not (eq window (frame-root-window window))))
3603 (error "Can't expand minibuffer to full frame")))
3605 ;; If WINDOW is the major non-side window, do nothing.
3606 (if (window-with-parameter 'window-side)
3607 (setq side-main (window--major-non-side-window frame))
3608 (setq side-main (frame-root-window frame)))
3609 (unless (eq window side-main)
3610 (delete-other-windows-internal window side-main)
3611 (run-window-configuration-change-hook frame)
3612 (window--check frame))
3613 ;; Always return nil.
3614 nil)))
3616 (defun delete-other-windows-vertically (&optional window)
3617 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
3618 This may be a useful alternative binding for \\[delete-other-windows]
3619 if you often split windows horizontally."
3620 (interactive)
3621 (let* ((window (or window (selected-window)))
3622 (edges (window-edges window))
3623 (w window) delenda)
3624 (while (not (eq (setq w (next-window w 1)) window))
3625 (let ((e (window-edges w)))
3626 (when (and (= (car e) (car edges))
3627 (= (nth 2 e) (nth 2 edges)))
3628 (push w delenda))))
3629 (mapc 'delete-window delenda)))
3631 ;;; Windows and buffers.
3633 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
3634 ;; for (1) determining which buffer to show in the window when its
3635 ;; buffer shall be buried or killed and (2) which buffer to show for
3636 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3638 ;; `prev-buffers' consists of <buffer, window-start, window-point>
3639 ;; triples. The entries on this list are ordered by the time their
3640 ;; buffer has been removed from the window, the most recently removed
3641 ;; buffer's entry being first. The window-start and window-point
3642 ;; components are `window-start' and `window-point' at the time the
3643 ;; buffer was removed from the window which implies that the entry must
3644 ;; be added when `set-window-buffer' removes the buffer from the window.
3646 ;; `next-buffers' is the list of buffers that have been replaced
3647 ;; recently by `switch-to-prev-buffer'. These buffers are the least
3648 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
3649 ;; candidates of `switch-to-next-buffer' to switch to. This list is
3650 ;; reset to nil by any action changing the window's buffer with the
3651 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
3652 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
3653 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
3655 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
3656 ;; if such a buffer was killed while the window was hidden within a
3657 ;; window configuration. Such killed buffers get removed whenever
3658 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
3660 ;; The following function is called by `set-window-buffer' _before_ it
3661 ;; replaces the buffer of the argument window with the new buffer.
3662 (defun record-window-buffer (&optional window)
3663 "Record WINDOW's buffer.
3664 WINDOW must be a live window and defaults to the selected one."
3665 (let* ((window (window-normalize-window window t))
3666 (buffer (window-buffer window))
3667 (entry (assq buffer (window-prev-buffers window))))
3668 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
3669 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3670 (set-window-next-buffers window nil)
3672 (when entry
3673 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
3674 (set-window-prev-buffers
3675 window (assq-delete-all buffer (window-prev-buffers window))))
3677 ;; Don't record insignificant buffers.
3678 (unless (eq (aref (buffer-name buffer) 0) ?\s)
3679 ;; Add an entry for buffer to WINDOW's previous buffers.
3680 (with-current-buffer buffer
3681 (let ((start (window-start window))
3682 (point (window-point window)))
3683 (setq entry
3684 (cons buffer
3685 (if entry
3686 ;; We have an entry, update marker positions.
3687 (list (set-marker (nth 1 entry) start)
3688 (set-marker (nth 2 entry) point))
3689 ;; Make new markers.
3690 (list (copy-marker start)
3691 (copy-marker
3692 ;; Preserve window-point-insertion-type
3693 ;; (Bug#12588).
3694 point window-point-insertion-type)))))
3695 (set-window-prev-buffers
3696 window (cons entry (window-prev-buffers window)))))
3698 (run-hooks 'buffer-list-update-hook))))
3700 (defun unrecord-window-buffer (&optional window buffer)
3701 "Unrecord BUFFER in WINDOW.
3702 WINDOW must be a live window and defaults to the selected one.
3703 BUFFER must be a live buffer and defaults to the buffer of
3704 WINDOW."
3705 (let* ((window (window-normalize-window window t))
3706 (buffer (or buffer (window-buffer window))))
3707 (set-window-prev-buffers
3708 window (assq-delete-all buffer (window-prev-buffers window)))
3709 (set-window-next-buffers
3710 window (delq buffer (window-next-buffers window)))))
3712 (defun set-window-buffer-start-and-point (window buffer &optional start point)
3713 "Set WINDOW's buffer to BUFFER.
3714 WINDOW must be a live window and defaults to the selected one.
3715 Optional argument START non-nil means set WINDOW's start position
3716 to START. Optional argument POINT non-nil means set WINDOW's
3717 point to POINT. If WINDOW is selected this also sets BUFFER's
3718 `point' to POINT. If WINDOW is selected and the buffer it showed
3719 before was current this also makes BUFFER the current buffer."
3720 (setq window (window-normalize-window window t))
3721 (let ((selected (eq window (selected-window)))
3722 (current (eq (window-buffer window) (current-buffer))))
3723 (set-window-buffer window buffer)
3724 (when (and selected current)
3725 (set-buffer buffer))
3726 (when start
3727 ;; Don't force window-start here (even if POINT is nil).
3728 (set-window-start window start t))
3729 (when point
3730 (set-window-point window point))))
3732 (defcustom switch-to-visible-buffer t
3733 "If non-nil, allow switching to an already visible buffer.
3734 If this variable is non-nil, `switch-to-prev-buffer' and
3735 `switch-to-next-buffer' may switch to an already visible buffer
3736 provided the buffer was shown before in the window specified as
3737 argument to those functions. If this variable is nil,
3738 `switch-to-prev-buffer' and `switch-to-next-buffer' always try to
3739 avoid switching to a buffer that is already visible in another
3740 window on the same frame."
3741 :type 'boolean
3742 :version "24.1"
3743 :group 'windows)
3745 (defun switch-to-prev-buffer (&optional window bury-or-kill)
3746 "In WINDOW switch to previous buffer.
3747 WINDOW must be a live window and defaults to the selected one.
3748 Return the buffer switched to, nil if no suitable buffer could be
3749 found.
3751 Optional argument BURY-OR-KILL non-nil means the buffer currently
3752 shown in WINDOW is about to be buried or killed and consequently
3753 shall not be switched to in future invocations of this command.
3755 As a special case, if BURY-OR-KILL equals `append', this means to
3756 move the buffer to the end of WINDOW's previous buffers list so a
3757 future invocation of `switch-to-prev-buffer' less likely switches
3758 to it."
3759 (interactive)
3760 (let* ((window (window-normalize-window window t))
3761 (frame (window-frame window))
3762 (old-buffer (window-buffer window))
3763 ;; Save this since it's destroyed by `set-window-buffer'.
3764 (next-buffers (window-next-buffers window))
3765 (pred (frame-parameter frame 'buffer-predicate))
3766 entry new-buffer killed-buffers visible)
3767 (when (window-minibuffer-p window)
3768 ;; Don't switch in minibuffer window.
3769 (unless (setq window (minibuffer-selected-window))
3770 (error "Window %s is a minibuffer window" window)))
3772 (when (window-dedicated-p window)
3773 ;; Don't switch in dedicated window.
3774 (error "Window %s is dedicated to buffer %s" window old-buffer))
3776 (catch 'found
3777 ;; Scan WINDOW's previous buffers first, skipping entries of next
3778 ;; buffers.
3779 (dolist (entry (window-prev-buffers window))
3780 (when (and (setq new-buffer (car entry))
3781 (or (buffer-live-p new-buffer)
3782 (not (setq killed-buffers
3783 (cons new-buffer killed-buffers))))
3784 (not (eq new-buffer old-buffer))
3785 (or (null pred) (funcall pred new-buffer))
3786 ;; When BURY-OR-KILL is nil, avoid switching to a
3787 ;; buffer in WINDOW's next buffers list.
3788 (or bury-or-kill (not (memq new-buffer next-buffers))))
3789 (if (and (not switch-to-visible-buffer)
3790 (get-buffer-window new-buffer frame))
3791 ;; Try to avoid showing a buffer visible in some other
3792 ;; window.
3793 (setq visible new-buffer)
3794 (set-window-buffer-start-and-point
3795 window new-buffer (nth 1 entry) (nth 2 entry))
3796 (throw 'found t))))
3797 ;; Scan reverted buffer list of WINDOW's frame next, skipping
3798 ;; entries of next buffers. Note that when we bury or kill a
3799 ;; buffer we don't reverse the global buffer list to avoid showing
3800 ;; a buried buffer instead. Otherwise, we must reverse the global
3801 ;; buffer list in order to make sure that switching to the
3802 ;; previous/next buffer traverse it in opposite directions.
3803 (dolist (buffer (if bury-or-kill
3804 (buffer-list frame)
3805 (nreverse (buffer-list frame))))
3806 (when (and (buffer-live-p buffer)
3807 (not (eq buffer old-buffer))
3808 (or (null pred) (funcall pred buffer))
3809 (not (eq (aref (buffer-name buffer) 0) ?\s))
3810 (or bury-or-kill (not (memq buffer next-buffers))))
3811 (if (get-buffer-window buffer frame)
3812 ;; Try to avoid showing a buffer visible in some other window.
3813 (unless visible
3814 (setq visible buffer))
3815 (setq new-buffer buffer)
3816 (set-window-buffer-start-and-point window new-buffer)
3817 (throw 'found t))))
3818 (unless bury-or-kill
3819 ;; Scan reverted next buffers last (must not use nreverse
3820 ;; here!).
3821 (dolist (buffer (reverse next-buffers))
3822 ;; Actually, buffer _must_ be live here since otherwise it
3823 ;; would have been caught in the scan of previous buffers.
3824 (when (and (or (buffer-live-p buffer)
3825 (not (setq killed-buffers
3826 (cons buffer killed-buffers))))
3827 (not (eq buffer old-buffer))
3828 (or (null pred) (funcall pred buffer))
3829 (setq entry (assq buffer (window-prev-buffers window))))
3830 (setq new-buffer buffer)
3831 (set-window-buffer-start-and-point
3832 window new-buffer (nth 1 entry) (nth 2 entry))
3833 (throw 'found t))))
3835 ;; Show a buffer visible in another window.
3836 (when visible
3837 (setq new-buffer visible)
3838 (set-window-buffer-start-and-point window new-buffer)))
3840 (if bury-or-kill
3841 (let ((entry (and (eq bury-or-kill 'append)
3842 (assq old-buffer (window-prev-buffers window)))))
3843 ;; Remove `old-buffer' from WINDOW's previous and (restored list
3844 ;; of) next buffers.
3845 (set-window-prev-buffers
3846 window (assq-delete-all old-buffer (window-prev-buffers window)))
3847 (set-window-next-buffers window (delq old-buffer next-buffers))
3848 (when entry
3849 ;; Append old-buffer's entry to list of WINDOW's previous
3850 ;; buffers so it's less likely to get switched to soon but
3851 ;; `display-buffer-in-previous-window' can nevertheless find
3852 ;; it.
3853 (set-window-prev-buffers
3854 window (append (window-prev-buffers window) (list entry)))))
3855 ;; Move `old-buffer' to head of WINDOW's restored list of next
3856 ;; buffers.
3857 (set-window-next-buffers
3858 window (cons old-buffer (delq old-buffer next-buffers))))
3860 ;; Remove killed buffers from WINDOW's previous and next buffers.
3861 (when killed-buffers
3862 (dolist (buffer killed-buffers)
3863 (set-window-prev-buffers
3864 window (assq-delete-all buffer (window-prev-buffers window)))
3865 (set-window-next-buffers
3866 window (delq buffer (window-next-buffers window)))))
3868 ;; Return new-buffer.
3869 new-buffer))
3871 (defun switch-to-next-buffer (&optional window)
3872 "In WINDOW switch to next buffer.
3873 WINDOW must be a live window and defaults to the selected one.
3874 Return the buffer switched to, nil if no suitable buffer could be
3875 found."
3876 (interactive)
3877 (let* ((window (window-normalize-window window t))
3878 (frame (window-frame window))
3879 (old-buffer (window-buffer window))
3880 (next-buffers (window-next-buffers window))
3881 (pred (frame-parameter frame 'buffer-predicate))
3882 new-buffer entry killed-buffers visible)
3883 (when (window-minibuffer-p window)
3884 ;; Don't switch in minibuffer window.
3885 (unless (setq window (minibuffer-selected-window))
3886 (error "Window %s is a minibuffer window" window)))
3888 (when (window-dedicated-p window)
3889 ;; Don't switch in dedicated window.
3890 (error "Window %s is dedicated to buffer %s" window old-buffer))
3892 (catch 'found
3893 ;; Scan WINDOW's next buffers first.
3894 (dolist (buffer next-buffers)
3895 (when (and (or (buffer-live-p buffer)
3896 (not (setq killed-buffers
3897 (cons buffer killed-buffers))))
3898 (not (eq buffer old-buffer))
3899 (or (null pred) (funcall pred buffer))
3900 (setq entry (assq buffer (window-prev-buffers window))))
3901 (setq new-buffer buffer)
3902 (set-window-buffer-start-and-point
3903 window new-buffer (nth 1 entry) (nth 2 entry))
3904 (throw 'found t)))
3905 ;; Scan the buffer list of WINDOW's frame next, skipping previous
3906 ;; buffers entries.
3907 (dolist (buffer (buffer-list frame))
3908 (when (and (buffer-live-p buffer)
3909 (not (eq buffer old-buffer))
3910 (or (null pred) (funcall pred buffer))
3911 (not (eq (aref (buffer-name buffer) 0) ?\s))
3912 (not (assq buffer (window-prev-buffers window))))
3913 (if (get-buffer-window buffer frame)
3914 ;; Try to avoid showing a buffer visible in some other window.
3915 (setq visible buffer)
3916 (setq new-buffer buffer)
3917 (set-window-buffer-start-and-point window new-buffer)
3918 (throw 'found t))))
3919 ;; Scan WINDOW's reverted previous buffers last (must not use
3920 ;; nreverse here!)
3921 (dolist (entry (reverse (window-prev-buffers window)))
3922 (when (and (setq new-buffer (car entry))
3923 (or (buffer-live-p new-buffer)
3924 (not (setq killed-buffers
3925 (cons new-buffer killed-buffers))))
3926 (not (eq new-buffer old-buffer))
3927 (or (null pred) (funcall pred new-buffer)))
3928 (if (and (not switch-to-visible-buffer)
3929 (get-buffer-window new-buffer frame))
3930 ;; Try to avoid showing a buffer visible in some other window.
3931 (unless visible
3932 (setq visible new-buffer))
3933 (set-window-buffer-start-and-point
3934 window new-buffer (nth 1 entry) (nth 2 entry))
3935 (throw 'found t))))
3937 ;; Show a buffer visible in another window.
3938 (when visible
3939 (setq new-buffer visible)
3940 (set-window-buffer-start-and-point window new-buffer)))
3942 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
3943 (set-window-next-buffers window (delq new-buffer next-buffers))
3945 ;; Remove killed buffers from WINDOW's previous and next buffers.
3946 (when killed-buffers
3947 (dolist (buffer killed-buffers)
3948 (set-window-prev-buffers
3949 window (assq-delete-all buffer (window-prev-buffers window)))
3950 (set-window-next-buffers
3951 window (delq buffer (window-next-buffers window)))))
3953 ;; Return new-buffer.
3954 new-buffer))
3956 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
3957 "Search LIST for a valid buffer to display in FRAME.
3958 Return nil when all buffers in LIST are undesirable for display,
3959 otherwise return the first suitable buffer in LIST.
3961 Buffers not visible in windows are preferred to visible buffers,
3962 unless VISIBLE-OK is non-nil.
3963 If the optional argument FRAME is nil, it defaults to the selected frame.
3964 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
3965 ;; This logic is more or less copied from other-buffer.
3966 (setq frame (or frame (selected-frame)))
3967 (let ((pred (frame-parameter frame 'buffer-predicate))
3968 found buf)
3969 (while (and (not found) list)
3970 (setq buf (car list))
3971 (if (and (not (eq buffer buf))
3972 (buffer-live-p buf)
3973 (or (null pred) (funcall pred buf))
3974 (not (eq (aref (buffer-name buf) 0) ?\s))
3975 (or visible-ok (null (get-buffer-window buf 'visible))))
3976 (setq found buf)
3977 (setq list (cdr list))))
3978 (car list)))
3980 (defun last-buffer (&optional buffer visible-ok frame)
3981 "Return the last buffer in FRAME's buffer list.
3982 If BUFFER is the last buffer, return the preceding buffer
3983 instead. Buffers not visible in windows are preferred to visible
3984 buffers, unless optional argument VISIBLE-OK is non-nil.
3985 Optional third argument FRAME nil or omitted means use the
3986 selected frame's buffer list. If no such buffer exists, return
3987 the buffer `*scratch*', creating it if necessary."
3988 (setq frame (or frame (selected-frame)))
3989 (or (get-next-valid-buffer (nreverse (buffer-list frame))
3990 buffer visible-ok frame)
3991 (get-buffer "*scratch*")
3992 (let ((scratch (get-buffer-create "*scratch*")))
3993 (set-buffer-major-mode scratch)
3994 scratch)))
3996 (defcustom frame-auto-hide-function #'iconify-frame
3997 "Function called to automatically hide frames.
3998 The function is called with one argument - a frame.
4000 Functions affected by this option are those that bury a buffer
4001 shown in a separate frame like `quit-window' and `bury-buffer'."
4002 :type '(choice (const :tag "Iconify" iconify-frame)
4003 (const :tag "Delete" delete-frame)
4004 (const :tag "Do nothing" ignore)
4005 function)
4006 :group 'windows
4007 :group 'frames
4008 :version "24.1")
4010 (defun window--delete (&optional window dedicated-only kill)
4011 "Delete WINDOW if possible.
4012 WINDOW must be a live window and defaults to the selected one.
4013 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
4014 only if it's dedicated to its buffer. Optional argument KILL
4015 means the buffer shown in window will be killed. Return non-nil
4016 if WINDOW gets deleted or its frame is auto-hidden."
4017 (setq window (window-normalize-window window t))
4018 (unless (and dedicated-only (not (window-dedicated-p window)))
4019 (let ((deletable (window-deletable-p window)))
4020 (cond
4021 ((eq deletable 'frame)
4022 (let ((frame (window-frame window)))
4023 (cond
4024 (kill
4025 (delete-frame frame))
4026 ((functionp frame-auto-hide-function)
4027 (funcall frame-auto-hide-function frame))))
4028 'frame)
4029 (deletable
4030 (delete-window window)
4031 t)))))
4033 (defun bury-buffer (&optional buffer-or-name)
4034 "Put BUFFER-OR-NAME at the end of the list of all buffers.
4035 There it is the least likely candidate for `other-buffer' to
4036 return; thus, the least likely buffer for \\[switch-to-buffer] to
4037 select by default.
4039 You can specify a buffer name as BUFFER-OR-NAME, or an actual
4040 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
4041 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
4042 remove the current buffer from the selected window if it is
4043 displayed there."
4044 (interactive)
4045 (let* ((buffer (window-normalize-buffer buffer-or-name)))
4046 ;; If `buffer-or-name' is not on the selected frame we unrecord it
4047 ;; although it's not "here" (call it a feature).
4048 (bury-buffer-internal buffer)
4049 ;; Handle case where `buffer-or-name' is nil and the current buffer
4050 ;; is shown in the selected window.
4051 (cond
4052 ((or buffer-or-name (not (eq buffer (window-buffer)))))
4053 ((window--delete nil t))
4055 ;; Switch to another buffer in window.
4056 (set-window-dedicated-p nil nil)
4057 (switch-to-prev-buffer nil 'bury)))
4059 ;; Always return nil.
4060 nil))
4062 (defun unbury-buffer ()
4063 "Switch to the last buffer in the buffer list."
4064 (interactive)
4065 (switch-to-buffer (last-buffer)))
4067 (defun next-buffer ()
4068 "In selected window switch to next buffer."
4069 (interactive)
4070 (cond
4071 ((window-minibuffer-p)
4072 (error "Cannot switch buffers in minibuffer window"))
4073 ((eq (window-dedicated-p) t)
4074 (error "Window is strongly dedicated to its buffer"))
4076 (switch-to-next-buffer))))
4078 (defun previous-buffer ()
4079 "In selected window switch to previous buffer."
4080 (interactive)
4081 (cond
4082 ((window-minibuffer-p)
4083 (error "Cannot switch buffers in minibuffer window"))
4084 ((eq (window-dedicated-p) t)
4085 (error "Window is strongly dedicated to its buffer"))
4087 (switch-to-prev-buffer))))
4089 (defun delete-windows-on (&optional buffer-or-name frame)
4090 "Delete all windows showing BUFFER-OR-NAME.
4091 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4092 and defaults to the current buffer.
4094 The following non-nil values of the optional argument FRAME
4095 have special meanings:
4097 - t means consider all windows on the selected frame only.
4099 - `visible' means consider all windows on all visible frames on
4100 the current terminal.
4102 - 0 (the number zero) means consider all windows on all visible
4103 and iconified frames on the current terminal.
4105 - A frame means consider all windows on that frame only.
4107 Any other value of FRAME means consider all windows on all
4108 frames.
4110 When a window showing BUFFER-OR-NAME is dedicated and the only
4111 window of its frame, that frame is deleted when there are other
4112 frames left."
4113 (interactive "BDelete windows on (buffer):\nP")
4114 (let ((buffer (window-normalize-buffer buffer-or-name))
4115 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4116 ;; `window-list-1' based function.
4117 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4118 (dolist (window (window-list-1 nil nil all-frames))
4119 (if (eq (window-buffer window) buffer)
4120 (let ((deletable (window-deletable-p window)))
4121 (cond
4122 ((and (eq deletable 'frame) (window-dedicated-p window))
4123 ;; Delete frame if and only if window is dedicated.
4124 (delete-frame (window-frame window)))
4125 ((eq deletable t)
4126 ;; Delete window.
4127 (delete-window window))
4129 ;; In window switch to previous buffer.
4130 (set-window-dedicated-p window nil)
4131 (switch-to-prev-buffer window 'bury))))
4132 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4133 (unrecord-window-buffer window buffer)))))
4135 (defun replace-buffer-in-windows (&optional buffer-or-name)
4136 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
4137 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4138 and defaults to the current buffer.
4140 When a window showing BUFFER-OR-NAME is dedicated, that window is
4141 deleted. If that window is the only window on its frame, the
4142 frame is deleted too when there are other frames left. If there
4143 are no other frames left, some other buffer is displayed in that
4144 window.
4146 This function removes the buffer denoted by BUFFER-OR-NAME from
4147 all window-local buffer lists."
4148 (interactive "bBuffer to replace: ")
4149 (let ((buffer (window-normalize-buffer buffer-or-name)))
4150 (dolist (window (window-list-1 nil nil t))
4151 (if (eq (window-buffer window) buffer)
4152 (unless (window--delete window t t)
4153 ;; Switch to another buffer in window.
4154 (set-window-dedicated-p window nil)
4155 (switch-to-prev-buffer window 'kill))
4156 ;; Unrecord BUFFER in WINDOW.
4157 (unrecord-window-buffer window buffer)))))
4159 (defun quit-restore-window (&optional window bury-or-kill)
4160 "Quit WINDOW and deal with its buffer.
4161 WINDOW must be a live window and defaults to the selected one.
4163 According to information stored in WINDOW's `quit-restore' window
4164 parameter either (1) delete WINDOW and its frame, (2) delete
4165 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4166 or (4) make WINDOW display some other buffer than the present
4167 one. If non-nil, reset `quit-restore' parameter to nil.
4169 Optional second argument BURY-OR-KILL tells how to proceed with
4170 the buffer of WINDOW. The following values are handled:
4172 `nil' means to not handle the buffer in a particular way. This
4173 means that if WINDOW is not deleted by this function, invoking
4174 `switch-to-prev-buffer' will usually show the buffer again.
4176 `append' means that if WINDOW is not deleted, move its buffer to
4177 the end of WINDOW's previous buffers so it's less likely that a
4178 future invocation of `switch-to-prev-buffer' will switch to it.
4179 Also, move the buffer to the end of the frame's buffer list.
4181 `bury' means that if WINDOW is not deleted, remove its buffer
4182 from WINDOW'S list of previous buffers. Also, move the buffer
4183 to the end of the frame's buffer list. This value provides the
4184 most reliable remedy to not have `switch-to-prev-buffer' switch
4185 to this buffer again without killing the buffer.
4187 `kill' means to kill WINDOW's buffer."
4188 (setq window (window-normalize-window window t))
4189 (let* ((buffer (window-buffer window))
4190 (quit-restore (window-parameter window 'quit-restore))
4191 (prev-buffer
4192 (let* ((prev-buffers (window-prev-buffers window))
4193 (prev-buffer (caar prev-buffers)))
4194 (and (or (not (eq prev-buffer buffer))
4195 (and (cdr prev-buffers)
4196 (not (eq (setq prev-buffer (cadr prev-buffers))
4197 buffer))))
4198 prev-buffer)))
4199 quad entry)
4200 (cond
4201 ((and (not prev-buffer)
4202 (or (eq (nth 1 quit-restore) 'frame)
4203 (and (eq (nth 1 quit-restore) 'window)
4204 ;; If the window has been created on an existing
4205 ;; frame and ended up as the sole window on that
4206 ;; frame, do not delete it (Bug#12764).
4207 (not (eq window (frame-root-window window)))))
4208 (eq (nth 3 quit-restore) buffer)
4209 ;; Delete WINDOW if possible.
4210 (window--delete window nil (eq bury-or-kill 'kill)))
4211 ;; If the previously selected window is still alive, select it.
4212 (when (window-live-p (nth 2 quit-restore))
4213 (select-window (nth 2 quit-restore))))
4214 ((and (listp (setq quad (nth 1 quit-restore)))
4215 (buffer-live-p (car quad))
4216 (eq (nth 3 quit-restore) buffer))
4217 ;; Show another buffer stored in quit-restore parameter.
4218 (when (and (integerp (nth 3 quad))
4219 (/= (nth 3 quad) (window-total-height window)))
4220 ;; Try to resize WINDOW to its old height but don't signal an
4221 ;; error.
4222 (condition-case nil
4223 (window-resize window (- (nth 3 quad) (window-total-height window)))
4224 (error nil)))
4225 (set-window-dedicated-p window nil)
4226 ;; Restore WINDOW's previous buffer, start and point position.
4227 (set-window-buffer-start-and-point
4228 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
4229 ;; Deal with the buffer we just removed from WINDOW.
4230 (setq entry (and (eq bury-or-kill 'append)
4231 (assq buffer (window-prev-buffers window))))
4232 (when bury-or-kill
4233 ;; Remove buffer from WINDOW's previous and next buffers.
4234 (set-window-prev-buffers
4235 window (assq-delete-all buffer (window-prev-buffers window)))
4236 (set-window-next-buffers
4237 window (delq buffer (window-next-buffers window))))
4238 (when entry
4239 ;; Append old buffer's entry to list of WINDOW's previous
4240 ;; buffers so it's less likely to get switched to soon but
4241 ;; `display-buffer-in-previous-window' can nevertheless find it.
4242 (set-window-prev-buffers
4243 window (append (window-prev-buffers window) (list entry))))
4244 ;; Reset the quit-restore parameter.
4245 (set-window-parameter window 'quit-restore nil)
4246 ;; Select old window.
4247 (when (window-live-p (nth 2 quit-restore))
4248 (select-window (nth 2 quit-restore))))
4250 ;; Show some other buffer in WINDOW and reset the quit-restore
4251 ;; parameter.
4252 (set-window-parameter window 'quit-restore nil)
4253 ;; Make sure that WINDOW is no more dedicated.
4254 (set-window-dedicated-p window nil)
4255 (switch-to-prev-buffer window bury-or-kill)))
4257 ;; Deal with the buffer.
4258 (cond
4259 ((not (buffer-live-p buffer)))
4260 ((eq bury-or-kill 'kill)
4261 (kill-buffer buffer))
4262 (bury-or-kill
4263 (bury-buffer-internal buffer)))))
4265 (defun quit-window (&optional kill window)
4266 "Quit WINDOW and bury its buffer.
4267 WINDOW must be a live window and defaults to the selected one.
4268 With prefix argument KILL non-nil, kill the buffer instead of
4269 burying it.
4271 According to information stored in WINDOW's `quit-restore' window
4272 parameter either (1) delete WINDOW and its frame, (2) delete
4273 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4274 or (4) make WINDOW display some other buffer than the present
4275 one. If non-nil, reset `quit-restore' parameter to nil."
4276 (interactive "P")
4277 (quit-restore-window window (if kill 'kill 'bury)))
4279 (defun quit-windows-on (&optional buffer-or-name kill frame)
4280 "Quit all windows showing BUFFER-OR-NAME.
4281 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4282 and defaults to the current buffer. Optional argument KILL
4283 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4284 BUFFER-OR-NAME. Optional argument FRAME is handled as by
4285 `delete-windows-on'.
4287 This function calls `quit-window' on all candidate windows
4288 showing BUFFER-OR-NAME."
4289 (interactive "BQuit windows on (buffer):\nP")
4290 (let ((buffer (window-normalize-buffer buffer-or-name))
4291 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4292 ;; `window-list-1' based function.
4293 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4294 (dolist (window (window-list-1 nil nil all-frames))
4295 (if (eq (window-buffer window) buffer)
4296 (quit-window kill window)
4297 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4298 (unrecord-window-buffer window buffer)))))
4300 (defun split-window (&optional window size side pixelwise)
4301 "Make a new window adjacent to WINDOW.
4302 WINDOW must be a valid window and defaults to the selected one.
4303 Return the new window which is always a live window.
4305 Optional argument SIZE a positive number means make WINDOW SIZE
4306 lines or columns tall. If SIZE is negative, make the new window
4307 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
4308 absolute value can be less than `window-min-height' or
4309 `window-min-width'; so this command can make a new window as
4310 small as one line or two columns. SIZE defaults to half of
4311 WINDOW's size.
4313 Optional third argument SIDE nil (or `below') specifies that the
4314 new window shall be located below WINDOW. SIDE `above' means the
4315 new window shall be located above WINDOW. In both cases SIZE
4316 specifies the new number of lines for WINDOW (or the new window
4317 if SIZE is negative) including space reserved for the mode and/or
4318 header line.
4320 SIDE t (or `right') specifies that the new window shall be
4321 located on the right side of WINDOW. SIDE `left' means the new
4322 window shall be located on the left of WINDOW. In both cases
4323 SIZE specifies the new number of columns for WINDOW (or the new
4324 window provided SIZE is negative) including space reserved for
4325 fringes and the scrollbar or a divider column. Any other non-nil
4326 value for SIDE is currently handled like t (or `right').
4328 PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
4330 If the variable `ignore-window-parameters' is non-nil or the
4331 `split-window' parameter of WINDOW equals t, do not process any
4332 parameters of WINDOW. Otherwise, if the `split-window' parameter
4333 of WINDOW specifies a function, call that function with all three
4334 arguments and return the value returned by that function.
4336 Otherwise, if WINDOW is part of an atomic window, \"split\" the
4337 root of that atomic window. The new window does not become a
4338 member of that atomic window.
4340 If WINDOW is live, properties of the new window like margins and
4341 scrollbars are inherited from WINDOW. If WINDOW is an internal
4342 window, these properties as well as the buffer displayed in the
4343 new window are inherited from the window selected on WINDOW's
4344 frame. The selected window is not changed by this function."
4345 (setq window (window-normalize-window window))
4346 (let* ((side (cond
4347 ((not side) 'below)
4348 ((memq side '(below above right left)) side)
4349 (t 'right)))
4350 (horizontal (not (memq side '(below above))))
4351 (frame (window-frame window))
4352 (parent (window-parent window))
4353 (function (window-parameter window 'split-window))
4354 (window-side (window-parameter window 'window-side))
4355 ;; Rebind the following two variables since in some cases we
4356 ;; have to override their value.
4357 (window-combination-limit window-combination-limit)
4358 (window-combination-resize window-combination-resize)
4359 (char-size (frame-char-size window horizontal))
4360 (pixel-size
4361 (when (numberp size)
4362 (window--size-to-pixel window size horizontal pixelwise t)))
4363 (divider-width (if horizontal
4364 (frame-right-divider-width frame)
4365 (frame-bottom-divider-width frame)))
4366 atom-root)
4367 (window--check frame)
4368 (catch 'done
4369 (cond
4370 ;; Ignore window parameters if either `ignore-window-parameters'
4371 ;; is t or the `split-window' parameter equals t.
4372 ((or ignore-window-parameters (eq function t)))
4373 ((functionp function)
4374 ;; The `split-window' parameter specifies the function to call.
4375 ;; If that function is `ignore', do nothing.
4376 (throw 'done (funcall function window size side)))
4377 ;; If WINDOW is part of an atomic window, split the root window
4378 ;; of that atomic window instead.
4379 ((and (window-parameter window 'window-atom)
4380 (setq atom-root (window-atom-root window))
4381 (not (eq atom-root window)))
4382 (throw 'done (split-window atom-root size side pixelwise)))
4383 ;; If WINDOW is a side window or its first or last child is a
4384 ;; side window, throw an error unless `window-combination-resize'
4385 ;; equals 'side.
4386 ((and (not (eq window-combination-resize 'side))
4387 (window--side-window-p window))
4388 (error "Cannot split side window or parent of side window"))
4389 ;; If `window-combination-resize' is 'side and window has a side
4390 ;; window sibling, bind `window-combination-limit' to t.
4391 ((and (not (eq window-combination-resize 'side))
4392 (or (and (window-prev-sibling window)
4393 (window-parameter
4394 (window-prev-sibling window) 'window-side))
4395 (and (window-next-sibling window)
4396 (window-parameter
4397 (window-next-sibling window) 'window-side))))
4398 (setq window-combination-limit t)))
4400 ;; If `window-combination-resize' is t and SIZE is non-negative,
4401 ;; bind `window-combination-limit' to t.
4402 (when (and (eq window-combination-resize t)
4403 pixel-size (> pixel-size 0))
4404 (setq window-combination-limit t))
4406 (let* ((parent-pixel-size
4407 ;; `parent-pixel-size' is the pixel size of WINDOW's
4408 ;; parent, provided it has one.
4409 (when parent (window-size parent horizontal t)))
4410 ;; `resize' non-nil means we are supposed to resize other
4411 ;; windows in WINDOW's combination.
4412 (resize
4413 (and window-combination-resize
4414 (or (window-parameter window 'window-side)
4415 (not (eq window-combination-resize 'side)))
4416 (not (eq window-combination-limit t))
4417 ;; Resize makes sense in iso-combinations only.
4418 (window-combined-p window horizontal)))
4419 ;; `old-pixel-size' is the current pixel size of WINDOW.
4420 (old-pixel-size (window-size window horizontal t))
4421 ;; `new-size' is the specified or calculated size of the
4422 ;; new window.
4423 new-pixel-size new-parent new-normal)
4424 (cond
4425 ((not pixel-size)
4426 (setq new-pixel-size
4427 (if resize
4428 ;; When resizing try to give the new window the
4429 ;; average size of a window in its combination.
4430 (min (- parent-pixel-size
4431 (window-min-size parent horizontal nil t))
4432 (/ parent-pixel-size
4433 (1+ (window-combinations parent horizontal))))
4434 ;; Else try to give the new window half the size
4435 ;; of WINDOW (plus an eventual odd pixel).
4436 (/ old-pixel-size 2)))
4437 (unless window-resize-pixelwise
4438 ;; Round to nearest char-size multiple.
4439 (setq new-pixel-size
4440 (* char-size (round new-pixel-size char-size)))))
4441 ((>= pixel-size 0)
4442 ;; SIZE non-negative specifies the new size of WINDOW.
4444 ;; Note: Specifying a non-negative SIZE is practically
4445 ;; always done as workaround for making the new window
4446 ;; appear above or on the left of the new window (the
4447 ;; ispell window is a typical example of that). In all
4448 ;; these cases the SIDE argument should be set to 'above
4449 ;; or 'left in order to support the 'resize option.
4450 ;; Here we have to nest the windows instead, see above.
4451 (setq new-pixel-size (- old-pixel-size pixel-size)))
4453 ;; SIZE negative specifies the size of the new window.
4454 (setq new-pixel-size (- pixel-size))))
4456 ;; Check SIZE.
4457 (cond
4458 ((not pixel-size)
4459 (cond
4460 (resize
4461 ;; SIZE unspecified, resizing.
4462 (unless (window-sizable-p
4463 parent (- new-pixel-size divider-width) horizontal nil t)
4464 (error "Window %s too small for splitting (1)" parent)))
4465 ((> (+ new-pixel-size divider-width
4466 (window-min-size window horizontal nil t))
4467 old-pixel-size)
4468 ;; SIZE unspecified, no resizing.
4469 (error "Window %s too small for splitting (2)" window))))
4470 ((and (>= pixel-size 0)
4471 (or (>= pixel-size old-pixel-size)
4472 (< new-pixel-size
4473 (window-safe-min-pixel-size window horizontal))))
4474 ;; SIZE specified as new size of old window. If the new size
4475 ;; is larger than the old size or the size of the new window
4476 ;; would be less than the safe minimum, signal an error.
4477 (error "Window %s too small for splitting (3)" window))
4478 (resize
4479 ;; SIZE specified, resizing.
4480 (unless (window-sizable-p
4481 parent (- new-pixel-size divider-width) horizontal nil t)
4482 ;; If we cannot resize the parent give up.
4483 (error "Window %s too small for splitting (4)" parent)))
4484 ((or (< new-pixel-size
4485 (window-safe-min-pixel-size window horizontal))
4486 (< (- old-pixel-size new-pixel-size)
4487 (window-safe-min-pixel-size window horizontal)))
4488 ;; SIZE specification violates minimum size restrictions.
4489 (error "Window %s too small for splitting (5)" window)))
4491 (window--resize-reset frame horizontal)
4493 (setq new-parent
4494 ;; Make new-parent non-nil if we need a new parent window;
4495 ;; either because we want to nest or because WINDOW is not
4496 ;; iso-combined.
4497 (or (eq window-combination-limit t)
4498 (not (window-combined-p window horizontal))))
4499 (setq new-normal
4500 ;; Make new-normal the normal size of the new window.
4501 (cond
4502 (pixel-size (/ (float new-pixel-size)
4503 (if new-parent old-pixel-size parent-pixel-size)))
4504 (new-parent 0.5)
4505 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
4506 (t (/ (window-normal-size window horizontal) 2.0))))
4508 (if resize
4509 ;; Try to get space from OLD's siblings. We could go "up" and
4510 ;; try getting additional space from surrounding windows but
4511 ;; we won't be able to return space to those windows when we
4512 ;; delete the one we create here. Hence we do not go up.
4513 (progn
4514 (window--resize-child-windows
4515 parent (- new-pixel-size) horizontal)
4516 (let* ((normal (- 1.0 new-normal))
4517 (sub (window-child parent)))
4518 (while sub
4519 (set-window-new-normal
4520 sub (* (window-normal-size sub horizontal) normal))
4521 (setq sub (window-right sub)))))
4522 ;; Get entire space from WINDOW.
4523 (set-window-new-pixel
4524 window (- old-pixel-size new-pixel-size))
4525 ;; (set-window-new-pixel window (- old-pixel-size new-pixel-size))
4526 ;; (set-window-new-total
4527 ;; window (- old-size new-size))
4528 (window--resize-this-window window (- new-pixel-size) horizontal)
4529 (set-window-new-normal
4530 window (- (if new-parent 1.0 (window-normal-size window horizontal))
4531 new-normal)))
4533 (let* ((new (split-window-internal window new-pixel-size side new-normal)))
4534 (window--pixel-to-total frame horizontal)
4535 ;; Assign window-side parameters, if any.
4536 (cond
4537 ((eq window-combination-resize 'side)
4538 (let ((window-side
4539 (cond
4540 (window-side window-side)
4541 ((eq side 'above) 'top)
4542 ((eq side 'below) 'bottom)
4543 (t side))))
4544 ;; We made a new side window.
4545 (set-window-parameter new 'window-side window-side)
4546 (when (and new-parent (window-parameter window 'window-side))
4547 ;; We've been splitting a side root window. Give the
4548 ;; new parent the same window-side parameter.
4549 (set-window-parameter
4550 (window-parent new) 'window-side window-side))))
4551 ((eq window-combination-resize 'atom)
4552 ;; Make sure `window--check-frame' won't destroy an existing
4553 ;; atomic window in case the new window gets nested inside.
4554 (unless (window-parameter window 'window-atom)
4555 (set-window-parameter window 'window-atom t))
4556 (when new-parent
4557 (set-window-parameter (window-parent new) 'window-atom t))
4558 (set-window-parameter new 'window-atom t)))
4560 ;; Sanitize sizes.
4561 (window--sanitize-window-sizes frame horizontal)
4563 (run-window-configuration-change-hook frame)
4564 (run-window-scroll-functions new)
4565 (window--check frame)
4566 ;; Always return the new window.
4567 new)))))
4569 ;; I think this should be the default; I think people will prefer it--rms.
4570 (defcustom split-window-keep-point t
4571 "If non-nil, \\[split-window-below] preserves point in the new window.
4572 If nil, adjust point in the two windows to minimize redisplay.
4573 This option applies only to `split-window-below' and functions
4574 that call it. The low-level `split-window' function always keeps
4575 the original point in both windows."
4576 :type 'boolean
4577 :group 'windows)
4579 (defun split-window-below (&optional size)
4580 "Split the selected window into two windows, one above the other.
4581 The selected window is above. The newly split-off window is
4582 below, and displays the same buffer. Return the new window.
4584 If optional argument SIZE is omitted or nil, both windows get the
4585 same height, or close to it. If SIZE is positive, the upper
4586 \(selected) window gets SIZE lines. If SIZE is negative, the
4587 lower (new) window gets -SIZE lines.
4589 If the variable `split-window-keep-point' is non-nil, both
4590 windows get the same value of point as the selected window.
4591 Otherwise, the window starts are chosen so as to minimize the
4592 amount of redisplay; this is convenient on slow terminals."
4593 (interactive "P")
4594 (let ((old-window (selected-window))
4595 (old-point (window-point))
4596 (size (and size (prefix-numeric-value size)))
4597 moved-by-window-height moved new-window bottom)
4598 (when (and size (< size 0) (< (- size) window-min-height))
4599 ;; `split-window' would not signal an error here.
4600 (error "Size of new window too small"))
4601 (setq new-window (split-window nil size))
4602 (unless split-window-keep-point
4603 (with-current-buffer (window-buffer)
4604 ;; Use `save-excursion' around vertical movements below
4605 ;; (Bug#10971). Note: When the selected window's buffer has a
4606 ;; header line, up to two lines of the buffer may not show up
4607 ;; in the resulting configuration.
4608 (save-excursion
4609 (goto-char (window-start))
4610 (setq moved (vertical-motion (window-height)))
4611 (set-window-start new-window (point))
4612 (when (> (point) (window-point new-window))
4613 (set-window-point new-window (point)))
4614 (when (= moved (window-height))
4615 (setq moved-by-window-height t)
4616 (vertical-motion -1))
4617 (setq bottom (point)))
4618 (and moved-by-window-height
4619 (<= bottom (point))
4620 (set-window-point old-window (1- bottom)))
4621 (and moved-by-window-height
4622 (<= (window-start new-window) old-point)
4623 (set-window-point new-window old-point)
4624 (select-window new-window))))
4625 ;; Always copy quit-restore parameter in interactive use.
4626 (let ((quit-restore (window-parameter old-window 'quit-restore)))
4627 (when quit-restore
4628 (set-window-parameter new-window 'quit-restore quit-restore)))
4629 new-window))
4631 (defalias 'split-window-vertically 'split-window-below)
4633 (defun split-window-right (&optional size)
4634 "Split the selected window into two side-by-side windows.
4635 The selected window is on the left. The newly split-off window
4636 is on the right, and displays the same buffer. Return the new
4637 window.
4639 If optional argument SIZE is omitted or nil, both windows get the
4640 same width, or close to it. If SIZE is positive, the left-hand
4641 \(selected) window gets SIZE columns. If SIZE is negative, the
4642 right-hand (new) window gets -SIZE columns. Here, SIZE includes
4643 the width of the window's scroll bar; if there are no scroll
4644 bars, it includes the width of the divider column to the window's
4645 right, if any."
4646 (interactive "P")
4647 (let ((old-window (selected-window))
4648 (size (and size (prefix-numeric-value size)))
4649 new-window)
4650 (when (and size (< size 0) (< (- size) window-min-width))
4651 ;; `split-window' would not signal an error here.
4652 (error "Size of new window too small"))
4653 (setq new-window (split-window nil size t))
4654 ;; Always copy quit-restore parameter in interactive use.
4655 (let ((quit-restore (window-parameter old-window 'quit-restore)))
4656 (when quit-restore
4657 (set-window-parameter new-window 'quit-restore quit-restore)))
4658 new-window))
4660 (defalias 'split-window-horizontally 'split-window-right)
4662 ;;; Balancing windows.
4664 ;; The following routine uses the recycled code from an old version of
4665 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
4666 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
4667 ;; more readable (FWIW we'd need three loops - one to calculate the
4668 ;; minimum sizes per window, one to enlarge or shrink windows until the
4669 ;; new parent-size matches, and one where we shrink the largest/enlarge
4670 ;; the smallest window).
4671 (defun balance-windows-2 (window horizontal)
4672 "Subroutine of `balance-windows-1'.
4673 WINDOW must be a vertical combination (horizontal if HORIZONTAL
4674 is non-nil)."
4675 (let* ((char-size (if window-resize-pixelwise
4677 (frame-char-size window horizontal)))
4678 (first (window-child window))
4679 (sub first)
4680 (number-of-children 0)
4681 (parent-size (window-new-pixel window))
4682 (total-sum parent-size)
4683 failed size sub-total sub-delta sub-amount rest)
4684 (while sub
4685 (setq number-of-children (1+ number-of-children))
4686 (when (window-size-fixed-p sub horizontal)
4687 (setq total-sum
4688 (- total-sum (window-size sub horizontal t)))
4689 (set-window-new-normal sub 'ignore))
4690 (setq sub (window-right sub)))
4692 (setq failed t)
4693 (while (and failed (> number-of-children 0))
4694 (setq size (/ total-sum number-of-children))
4695 (setq failed nil)
4696 (setq sub first)
4697 (while (and sub (not failed))
4698 ;; Ignore child windows that should be ignored or are stuck.
4699 (unless (window--resize-child-windows-skip-p sub)
4700 (setq sub-total (window-size sub horizontal t))
4701 (setq sub-delta (- size sub-total))
4702 (setq sub-amount
4703 (window-sizable sub sub-delta horizontal nil t))
4704 ;; Register the new total size for this child window.
4705 (set-window-new-pixel sub (+ sub-total sub-amount))
4706 (unless (= sub-amount sub-delta)
4707 (setq total-sum (- total-sum sub-total sub-amount))
4708 (setq number-of-children (1- number-of-children))
4709 ;; We failed and need a new round.
4710 (setq failed t)
4711 (set-window-new-normal sub 'skip)))
4712 (setq sub (window-right sub))))
4714 ;; How can we be sure that `number-of-children' is NOT zero here ?
4715 (setq rest (% total-sum number-of-children))
4716 ;; Fix rounding by trying to enlarge non-stuck windows by one line
4717 ;; (column) until `rest' is zero.
4718 (setq sub first)
4719 (while (and sub (> rest 0))
4720 (unless (window--resize-child-windows-skip-p window)
4721 (set-window-new-pixel sub (min rest char-size) t)
4722 (setq rest (- rest char-size)))
4723 (setq sub (window-right sub)))
4725 ;; Fix rounding by trying to enlarge stuck windows by one line
4726 ;; (column) until `rest' equals zero.
4727 (setq sub first)
4728 (while (and sub (> rest 0))
4729 (unless (eq (window-new-normal sub) 'ignore)
4730 (set-window-new-pixel sub (min rest char-size) t)
4731 (setq rest (- rest char-size)))
4732 (setq sub (window-right sub)))
4734 (setq sub first)
4735 (while sub
4736 ;; Record new normal sizes.
4737 (set-window-new-normal
4738 sub (/ (if (eq (window-new-normal sub) 'ignore)
4739 (window-size sub horizontal t)
4740 (window-new-pixel sub))
4741 (float parent-size)))
4742 ;; Recursively balance each window's child windows.
4743 (balance-windows-1 sub horizontal)
4744 (setq sub (window-right sub)))))
4746 (defun balance-windows-1 (window &optional horizontal)
4747 "Subroutine of `balance-windows'."
4748 (if (window-child window)
4749 (let ((sub (window-child window)))
4750 (if (window-combined-p sub horizontal)
4751 (balance-windows-2 window horizontal)
4752 (let ((size (window-new-pixel window)))
4753 (while sub
4754 (set-window-new-pixel sub size)
4755 (balance-windows-1 sub horizontal)
4756 (setq sub (window-right sub))))))))
4758 (defun balance-windows (&optional window-or-frame)
4759 "Balance the sizes of windows of WINDOW-OR-FRAME.
4760 WINDOW-OR-FRAME is optional and defaults to the selected frame.
4761 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
4762 windows of that frame. If WINDOW-OR-FRAME denotes a window,
4763 recursively balance the sizes of all child windows of that
4764 window."
4765 (interactive)
4766 (let* ((window
4767 (cond
4768 ((or (not window-or-frame)
4769 (frame-live-p window-or-frame))
4770 (frame-root-window window-or-frame))
4771 ((or (window-live-p window-or-frame)
4772 (window-child window-or-frame))
4773 window-or-frame)
4775 (error "Not a window or frame %s" window-or-frame))))
4776 (frame (window-frame window)))
4777 ;; Balance vertically.
4778 (window--resize-reset (window-frame window))
4779 (balance-windows-1 window)
4780 (when (window--resize-apply-p frame)
4781 (window-resize-apply frame)
4782 (window--pixel-to-total frame)
4783 (run-window-configuration-change-hook frame))
4784 ;; Balance horizontally.
4785 (window--resize-reset (window-frame window) t)
4786 (balance-windows-1 window t)
4787 (when (window--resize-apply-p frame t)
4788 (window-resize-apply frame t)
4789 (window--pixel-to-total frame t)
4790 (run-window-configuration-change-hook frame))))
4792 (defun window-fixed-size-p (&optional window direction)
4793 "Return t if WINDOW cannot be resized in DIRECTION.
4794 WINDOW defaults to the selected window. DIRECTION can be
4795 nil (i.e. any), `height' or `width'."
4796 (with-current-buffer (window-buffer window)
4797 (when (and (boundp 'window-size-fixed) window-size-fixed)
4798 (not (and direction
4799 (member (cons direction window-size-fixed)
4800 '((height . width) (width . height))))))))
4802 ;;; A different solution to balance-windows.
4803 (defvar window-area-factor 1
4804 "Factor by which the window area should be over-estimated.
4805 This is used by `balance-windows-area'.
4806 Changing this globally has no effect.")
4807 (make-variable-buffer-local 'window-area-factor)
4809 (defun balance-windows-area-adjust (window delta horizontal pixelwise)
4810 "Wrapper around `window-resize' with error checking.
4811 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
4812 ;; `window-resize' may fail if delta is too large.
4813 (while (>= (abs delta) 1)
4814 (condition-case nil
4815 (progn
4816 ;; It was wrong to use `window-resize' here. Somehow
4817 ;; `balance-windows-area' depends on resizing windows
4818 ;; asymmetrically.
4819 (adjust-window-trailing-edge window delta horizontal pixelwise)
4820 (setq delta 0))
4821 (error
4822 ;;(message "adjust: %s" (error-message-string err))
4823 (setq delta (/ delta 2))))))
4825 (defun balance-windows-area ()
4826 "Make all visible windows the same area (approximately).
4827 See also `window-area-factor' to change the relative size of
4828 specific buffers."
4829 (interactive)
4830 (let* ((unchanged 0) (carry 0) (round 0)
4831 ;; Remove fixed-size windows.
4832 (wins (delq nil (mapcar (lambda (win)
4833 (if (not (window-fixed-size-p win)) win))
4834 (window-list nil 'nomini))))
4835 (changelog nil)
4836 (pixelwise window-resize-pixelwise)
4837 next)
4838 ;; Resizing a window changes the size of surrounding windows in complex
4839 ;; ways, so it's difficult to balance them all. The introduction of
4840 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
4841 ;; very difficult to do. `balance-window' above takes an off-line
4842 ;; approach: get the whole window tree, then balance it, then try to
4843 ;; adjust the windows so they fit the result.
4844 ;; Here, instead, we take a "local optimization" approach, where we just
4845 ;; go through all the windows several times until nothing needs to be
4846 ;; changed. The main problem with this approach is that it's difficult
4847 ;; to make sure it terminates, so we use some heuristic to try and break
4848 ;; off infinite loops.
4849 ;; After a round without any change, we allow a second, to give a chance
4850 ;; to the carry to propagate a minor imbalance from the end back to
4851 ;; the beginning.
4852 (while (< unchanged 2)
4853 ;; (message "New round")
4854 (setq unchanged (1+ unchanged) round (1+ round))
4855 (dolist (win wins)
4856 (setq next win)
4857 (while (progn (setq next (next-window next))
4858 (window-fixed-size-p next)))
4859 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
4860 (let* ((horiz
4861 (< (car (window-pixel-edges win)) (car (window-pixel-edges next))))
4862 (areadiff (/ (- (* (window-size next nil pixelwise)
4863 (window-size next t pixelwise)
4864 (buffer-local-value 'window-area-factor
4865 (window-buffer next)))
4866 (* (window-size win nil pixelwise)
4867 (window-size win t pixelwise)
4868 (buffer-local-value 'window-area-factor
4869 (window-buffer win))))
4870 (max (buffer-local-value 'window-area-factor
4871 (window-buffer win))
4872 (buffer-local-value 'window-area-factor
4873 (window-buffer next)))))
4874 (edgesize (if horiz
4875 (+ (window-size win nil pixelwise)
4876 (window-size next nil pixelwise))
4877 (+ (window-size win t pixelwise)
4878 (window-size next t pixelwise))))
4879 (diff (/ areadiff edgesize)))
4880 (when (zerop diff)
4881 ;; Maybe diff is actually closer to 1 than to 0.
4882 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
4883 (when (and (zerop diff) (not (zerop areadiff)))
4884 (setq diff (/ (+ areadiff carry) edgesize))
4885 ;; Change things smoothly.
4886 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
4887 (if (zerop diff)
4888 ;; Make sure negligible differences don't accumulate to
4889 ;; become significant.
4890 (setq carry (+ carry areadiff))
4891 ;; This used `adjust-window-trailing-edge' before and uses
4892 ;; `window-resize' now. Error wrapping is still needed.
4893 (balance-windows-area-adjust win diff horiz pixelwise)
4894 ;; (sit-for 0.5)
4895 (let ((change (cons win (window-pixel-edges win))))
4896 ;; If the same change has been seen already for this window,
4897 ;; we're most likely in an endless loop, so don't count it as
4898 ;; a change.
4899 (unless (member change changelog)
4900 (push change changelog)
4901 (setq unchanged 0 carry 0)))))))
4902 ;; We've now basically balanced all the windows.
4903 ;; But there may be some minor off-by-one imbalance left over,
4904 ;; so let's do some fine tuning.
4905 ;; (bw-finetune wins)
4906 ;; (message "Done in %d rounds" round)
4909 ;;; Window states, how to get them and how to put them in a window.
4910 (defun window--state-get-1 (window &optional writable)
4911 "Helper function for `window-state-get'."
4912 (let* ((type
4913 (cond
4914 ((window-top-child window) 'vc)
4915 ((window-left-child window) 'hc)
4916 (t 'leaf)))
4917 (buffer (window-buffer window))
4918 (selected (eq window (selected-window)))
4919 (head
4920 `(,type
4921 ,@(unless (window-next-sibling window) `((last . t)))
4922 (pixel-width . ,(window-pixel-width window))
4923 (pixel-height . ,(window-pixel-height window))
4924 (total-width . ,(window-total-width window))
4925 (total-height . ,(window-total-height window))
4926 (normal-height . ,(window-normal-size window))
4927 (normal-width . ,(window-normal-size window t))
4928 ,@(unless (window-live-p window)
4929 `((combination-limit . ,(window-combination-limit window))))
4930 ,@(let ((parameters (window-parameters window))
4931 list)
4932 ;; Make copies of those window parameters whose
4933 ;; persistence property is `writable' if WRITABLE is
4934 ;; non-nil and non-nil if WRITABLE is nil.
4935 (dolist (par parameters)
4936 (let ((pers (cdr (assq (car par)
4937 window-persistent-parameters))))
4938 (when (and pers (or (not writable) (eq pers 'writable)))
4939 (setq list (cons (cons (car par) (cdr par)) list)))))
4940 ;; Add `clone-of' parameter if necessary.
4941 (let ((pers (cdr (assq 'clone-of
4942 window-persistent-parameters))))
4943 (when (and pers (or (not writable) (eq pers 'writable))
4944 (not (assq 'clone-of list)))
4945 (setq list (cons (cons 'clone-of window) list))))
4946 (when list
4947 `((parameters . ,list))))
4948 ,@(when buffer
4949 ;; All buffer related things go in here.
4950 (let ((point (window-point window))
4951 (start (window-start window)))
4952 `((buffer
4953 ,(buffer-name buffer)
4954 (selected . ,selected)
4955 (hscroll . ,(window-hscroll window))
4956 (fringes . ,(window-fringes window))
4957 (margins . ,(window-margins window))
4958 (scroll-bars . ,(window-scroll-bars window))
4959 (vscroll . ,(window-vscroll window))
4960 (dedicated . ,(window-dedicated-p window))
4961 (point . ,(if writable point
4962 (copy-marker point
4963 (buffer-local-value
4964 'window-point-insertion-type
4965 buffer))))
4966 (start . ,(if writable start (copy-marker start)))))))))
4967 (tail
4968 (when (memq type '(vc hc))
4969 (let (list)
4970 (setq window (window-child window))
4971 (while window
4972 (setq list (cons (window--state-get-1 window writable) list))
4973 (setq window (window-right window)))
4974 (nreverse list)))))
4975 (append head tail)))
4977 (defun window-state-get (&optional window writable)
4978 "Return state of WINDOW as a Lisp object.
4979 WINDOW can be any window and defaults to the root window of the
4980 selected frame.
4982 Optional argument WRITABLE non-nil means do not use markers for
4983 sampling `window-point' and `window-start'. Together, WRITABLE
4984 and the variable `window-persistent-parameters' specify which
4985 window parameters are saved by this function. WRITABLE should be
4986 non-nil when the return value shall be written to a file and read
4987 back in another session. Otherwise, an application may run into
4988 an `invalid-read-syntax' error while attempting to read back the
4989 value from file.
4991 The return value can be used as argument for `window-state-put'
4992 to put the state recorded here into an arbitrary window. The
4993 value can be also stored on disk and read back in a new session."
4994 (setq window
4995 (if window
4996 (if (window-valid-p window)
4997 window
4998 (error "%s is not a live or internal window" window))
4999 (frame-root-window)))
5000 ;; The return value is a cons whose car specifies some constraints on
5001 ;; the size of WINDOW. The cdr lists the states of the child windows
5002 ;; of WINDOW.
5003 (cons
5004 ;; Frame related things would go into a function, say `frame-state',
5005 ;; calling `window-state-get' to insert the frame's root window.
5006 `((min-height . ,(window-min-size window))
5007 (min-width . ,(window-min-size window t))
5008 (min-height-ignore . ,(window-min-size window nil t))
5009 (min-width-ignore . ,(window-min-size window t t))
5010 (min-height-safe . ,(window-min-size window nil 'safe))
5011 (min-width-safe . ,(window-min-size window t 'safe))
5012 (min-pixel-height . ,(window-min-size window nil nil t))
5013 (min-pixel-width . ,(window-min-size window t nil t))
5014 (min-pixel-height-ignore . ,(window-min-size window nil t t))
5015 (min-pixel-width-ignore . ,(window-min-size window t t t))
5016 (min-pixel-height-safe . ,(window-min-size window nil 'safe t))
5017 (min-pixel-width-safe . ,(window-min-size window t 'safe t)))
5018 (window--state-get-1 window writable)))
5020 (defvar window-state-put-list nil
5021 "Helper variable for `window-state-put'.")
5023 (defvar window-state-put-stale-windows nil
5024 "Helper variable for `window-state-put'.")
5026 (defun window--state-put-1 (state &optional window ignore totals pixelwise)
5027 "Helper function for `window-state-put'."
5028 (let ((type (car state)))
5029 (setq state (cdr state))
5030 (cond
5031 ((eq type 'leaf)
5032 ;; For a leaf window just add unprocessed entries to
5033 ;; `window-state-put-list'.
5034 (push (cons window state) window-state-put-list))
5035 ((memq type '(vc hc))
5036 (let* ((horizontal (eq type 'hc))
5037 (total (window-size window horizontal pixelwise))
5038 (first t)
5039 size new)
5040 (dolist (item state)
5041 ;; Find the next child window. WINDOW always points to the
5042 ;; real window that we want to fill with what we find here.
5043 (when (memq (car item) '(leaf vc hc))
5044 (if (assq 'last item)
5045 ;; The last child window. Below `window--state-put-1'
5046 ;; will put into it whatever ITEM has in store.
5047 (setq new nil)
5048 ;; Not the last child window, prepare for splitting
5049 ;; WINDOW. SIZE is the new (and final) size of the old
5050 ;; window.
5051 (setq size
5052 (if totals
5053 ;; Use total size.
5054 (if pixelwise
5055 (cdr (assq (if horizontal
5056 'pixel-width
5057 'pixel-height)
5058 item))
5059 (cdr (assq (if horizontal
5060 'total-width
5061 'total-height)
5062 item)))
5063 ;; Use normalized size and round.
5064 (round
5065 (* total
5066 (cdr (assq (if horizontal 'normal-width 'normal-height)
5067 item))))))
5069 ;; Use safe sizes, we try to resize later.
5070 (setq size (max size
5071 (if horizontal
5072 (* window-safe-min-width
5073 (if pixelwise
5074 (frame-char-width (window-frame window))
5076 (* window-safe-min-height
5077 (if pixelwise
5078 (frame-char-height (window-frame window))
5079 1)))))
5080 (if (window-sizable-p window (- size) horizontal 'safe pixelwise)
5081 (let* ((window-combination-limit
5082 (assq 'combination-limit item)))
5083 ;; We must inherit the combination limit, otherwise
5084 ;; we might mess up handling of atomic and side
5085 ;; window.
5086 (setq new (split-window window size horizontal pixelwise)))
5087 ;; Give up if we can't resize window down to safe sizes.
5088 (error "Cannot resize window %s" window))
5090 (when first
5091 (setq first nil)
5092 ;; When creating the first child window add for parent
5093 ;; unprocessed entries to `window-state-put-list'.
5094 (setq window-state-put-list
5095 (cons (cons (window-parent window) state)
5096 window-state-put-list))))
5098 ;; Now process the current window (either the one we've just
5099 ;; split or the last child of its parent).
5100 (window--state-put-1 item window ignore totals)
5101 ;; Continue with the last window split off.
5102 (setq window new))))))))
5104 (defun window--state-put-2 (ignore pixelwise)
5105 "Helper function for `window-state-put'."
5106 (dolist (item window-state-put-list)
5107 (let ((window (car item))
5108 (combination-limit (cdr (assq 'combination-limit item)))
5109 (parameters (cdr (assq 'parameters item)))
5110 (state (cdr (assq 'buffer item))))
5111 (when combination-limit
5112 (set-window-combination-limit window combination-limit))
5113 ;; Reset window's parameters and assign saved ones (we might want
5114 ;; a `remove-window-parameters' function here).
5115 (dolist (parameter (window-parameters window))
5116 (set-window-parameter window (car parameter) nil))
5117 (when parameters
5118 (dolist (parameter parameters)
5119 (set-window-parameter window (car parameter) (cdr parameter))))
5120 ;; Process buffer related state.
5121 (when state
5122 (let ((buffer (get-buffer (car state))))
5123 (if buffer
5124 (with-current-buffer buffer
5125 (set-window-buffer window buffer)
5126 (set-window-hscroll window (cdr (assq 'hscroll state)))
5127 (apply 'set-window-fringes
5128 (cons window (cdr (assq 'fringes state))))
5129 (let ((margins (cdr (assq 'margins state))))
5130 (set-window-margins window (car margins) (cdr margins)))
5131 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
5132 (set-window-scroll-bars
5133 window (car scroll-bars) (nth 2 scroll-bars)
5134 (or (nth 3 scroll-bars) 0) (nth 5 scroll-bars)))
5135 (set-window-vscroll window (cdr (assq 'vscroll state)))
5136 ;; Adjust vertically.
5137 (if (memq window-size-fixed '(t height))
5138 ;; A fixed height window, try to restore the
5139 ;; original size.
5140 (let ((delta
5141 (- (cdr (assq
5142 (if pixelwise 'pixel-height 'total-height)
5143 item))
5144 (window-size window nil pixelwise)))
5145 window-size-fixed)
5146 (when (window--resizable-p
5147 window delta nil nil nil nil nil pixelwise)
5148 (window-resize window delta nil nil pixelwise)))
5149 ;; Else check whether the window is not high enough.
5150 (let* ((min-size
5151 (window-min-size window nil ignore pixelwise))
5152 (delta
5153 (- min-size (window-size window nil pixelwise))))
5154 (when (and (> delta 0)
5155 (window--resizable-p
5156 window delta nil ignore nil nil nil pixelwise))
5157 (window-resize window delta nil ignore pixelwise))))
5158 ;; Adjust horizontally.
5159 (if (memq window-size-fixed '(t width))
5160 ;; A fixed width window, try to restore the original
5161 ;; size.
5162 (let ((delta
5163 (- (cdr (assq
5164 (if pixelwise 'pixel-width 'total-width)
5165 item))
5166 (window-size window t pixelwise)))
5167 window-size-fixed)
5168 (when (window--resizable-p
5169 window delta nil nil nil nil nil pixelwise)
5170 (window-resize window delta nil nil pixelwise)))
5171 ;; Else check whether the window is not wide enough.
5172 (let* ((min-size (window-min-size window t ignore pixelwise))
5173 (delta (- min-size (window-size window t pixelwise))))
5174 (when (and (> delta 0)
5175 (window--resizable-p
5176 window delta t ignore nil nil nil pixelwise))
5177 (window-resize window delta t ignore pixelwise))))
5178 ;; Set dedicated status.
5179 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
5180 ;; Install positions (maybe we should do this after all
5181 ;; windows have been created and sized).
5182 (ignore-errors
5183 (set-window-start window (cdr (assq 'start state)))
5184 (set-window-point window (cdr (assq 'point state))))
5185 ;; Select window if it's the selected one.
5186 (when (cdr (assq 'selected state))
5187 (select-window window)))
5188 ;; We don't want to raise an error in case the buffer does
5189 ;; not exist anymore, so we switch to a previous one and
5190 ;; save the window with the intention of deleting it later
5191 ;; if possible.
5192 (switch-to-prev-buffer window)
5193 (push window window-state-put-stale-windows)))))))
5195 (defun window-state-put (state &optional window ignore)
5196 "Put window state STATE into WINDOW.
5197 STATE should be the state of a window returned by an earlier
5198 invocation of `window-state-get'. Optional argument WINDOW must
5199 specify a valid window and defaults to the selected one. If
5200 WINDOW is not live, replace WINDOW by a live one before putting
5201 STATE into it.
5203 Optional argument IGNORE non-nil means ignore minimum window
5204 sizes and fixed size restrictions. IGNORE equal `safe' means
5205 windows can get as small as `window-safe-min-height' and
5206 `window-safe-min-width'."
5207 (setq window-state-put-stale-windows nil)
5208 (setq window (window-normalize-window window))
5210 ;; When WINDOW is internal, reduce it to a live one to put STATE into,
5211 ;; see Bug#16793.
5212 (unless (window-live-p window)
5213 (let ((root (frame-root-window window)))
5214 (if (eq window root)
5215 (setq window (frame-first-window root))
5216 (setq root window)
5217 (setq window (catch 'live
5218 (walk-window-subtree
5219 (lambda (window)
5220 (when (window-live-p window)
5221 (throw 'live window)))
5222 root))))
5223 (delete-other-windows-internal window root)))
5225 (let* ((frame (window-frame window))
5226 (head (car state))
5227 ;; We check here (1) whether the total sizes of root window of
5228 ;; STATE and that of WINDOW are equal so we can avoid
5229 ;; calculating new sizes, and (2) if we do have to resize
5230 ;; whether we can do so without violating size restrictions.
5231 (pixelwise (and (cdr (assq 'pixel-width state))
5232 (cdr (assq 'pixel-height state))))
5233 (totals (or (and pixelwise
5234 (= (window-pixel-width window)
5235 (cdr (assq 'pixel-width state)))
5236 (= (window-pixel-height window)
5237 (cdr (assq 'pixel-height state))))
5238 (and (= (window-total-width window)
5239 (cdr (assq 'total-width state)))
5240 (= (window-total-height window)
5241 (cdr (assq 'total-height state))))))
5242 (min-height (cdr (assq
5243 (if pixelwise 'min-pixel-height 'min-height)
5244 head)))
5245 (min-width (cdr (assq
5246 (if pixelwise 'min-pixel-width 'min-weight)
5247 head))))
5248 (if (and (not totals)
5249 (or (> min-height (window-size window nil pixelwise))
5250 (> min-width (window-size window t pixelwise)))
5251 (or (not ignore)
5252 (and (setq min-height
5253 (cdr (assq
5254 (if pixelwise
5255 'min-pixel-height-ignore
5256 'min-height-ignore)
5257 head)))
5258 (setq min-width
5259 (cdr (assq
5260 (if pixelwise
5261 'min-pixel-width-ignore
5262 'min-width-ignore)
5263 head)))
5264 (or (> min-height
5265 (window-size window nil pixelwise))
5266 (> min-width
5267 (window-size window t pixelwise)))
5268 (or (not (eq ignore 'safe))
5269 (and (setq min-height
5270 (cdr (assq
5271 (if pixelwise
5272 'min-pixel-height-safe
5273 'min-height-safe)
5274 head)))
5275 (setq min-width
5276 (cdr (assq
5277 (if pixelwise
5278 'min-pixel-width-safe
5279 'min-width-safe)
5280 head)))
5281 (or (> min-height
5282 (window-size window nil pixelwise))
5283 (> min-width
5284 (window-size window t pixelwise))))))))
5285 ;; The check above might not catch all errors due to rounding
5286 ;; issues - so IGNORE equal 'safe might not always produce the
5287 ;; minimum possible state. But such configurations hardly make
5288 ;; sense anyway.
5289 (error "Window %s too small to accommodate state" window)
5290 (setq state (cdr state))
5291 (setq window-state-put-list nil)
5292 ;; Work on the windows of a temporary buffer to make sure that
5293 ;; splitting proceeds regardless of any buffer local values of
5294 ;; `window-size-fixed'. Release that buffer after the buffers of
5295 ;; all live windows have been set by `window--state-put-2'.
5296 (with-temp-buffer
5297 (set-window-buffer window (current-buffer))
5298 (window--state-put-1 state window nil totals pixelwise)
5299 (window--state-put-2 ignore pixelwise))
5300 (while window-state-put-stale-windows
5301 (let ((window (pop window-state-put-stale-windows)))
5302 (when (eq (window-deletable-p window) t)
5303 (delete-window window))))
5304 (window--check frame))))
5306 (defun display-buffer-record-window (type window buffer)
5307 "Record information for window used by `display-buffer'.
5308 TYPE specifies the type of the calling operation and must be one
5309 of the symbols 'reuse (when WINDOW existed already and was
5310 reused for displaying BUFFER), 'window (when WINDOW was created
5311 on an already existing frame), or 'frame (when WINDOW was
5312 created on a new frame). WINDOW is the window used for or created
5313 by the `display-buffer' routines. BUFFER is the buffer that
5314 shall be displayed.
5316 This function installs or updates the quit-restore parameter of
5317 WINDOW. The quit-restore parameter is a list of four elements:
5318 The first element is one of the symbols 'window, 'frame, 'same or
5319 'other. The second element is either one of the symbols 'window
5320 or 'frame or a list whose elements are the buffer previously
5321 shown in the window, that buffer's window start and window point,
5322 and the window's height. The third element is the window
5323 selected at the time the parameter was created. The fourth
5324 element is BUFFER."
5325 (cond
5326 ((eq type 'reuse)
5327 (if (eq (window-buffer window) buffer)
5328 ;; WINDOW shows BUFFER already.
5329 (when (consp (window-parameter window 'quit-restore))
5330 ;; If WINDOW has a quit-restore parameter, reset its car.
5331 (setcar (window-parameter window 'quit-restore) 'same))
5332 ;; WINDOW shows another buffer.
5333 (with-current-buffer (window-buffer window)
5334 (set-window-parameter
5335 window 'quit-restore
5336 (list 'other
5337 ;; A quadruple of WINDOW's buffer, start, point and height.
5338 (list (current-buffer) (window-start window)
5339 ;; Preserve window-point-insertion-type (Bug#12588).
5340 (copy-marker
5341 (window-point window) window-point-insertion-type)
5342 (window-total-height window))
5343 (selected-window) buffer)))))
5344 ((eq type 'window)
5345 ;; WINDOW has been created on an existing frame.
5346 (set-window-parameter
5347 window 'quit-restore
5348 (list 'window 'window (selected-window) buffer)))
5349 ((eq type 'frame)
5350 ;; WINDOW has been created on a new frame.
5351 (set-window-parameter
5352 window 'quit-restore
5353 (list 'frame 'frame (selected-window) buffer)))))
5355 (defcustom display-buffer-function nil
5356 "If non-nil, function to call to handle `display-buffer'.
5357 It will receive two args, the buffer and a flag which if non-nil
5358 means that the currently selected window is not acceptable. It
5359 should choose or create a window, display the specified buffer in
5360 it, and return the window.
5362 The specified function should call `display-buffer-record-window'
5363 with corresponding arguments to set up the quit-restore parameter
5364 of the window used."
5365 :type '(choice
5366 (const nil)
5367 (function :tag "function"))
5368 :group 'windows)
5370 (make-obsolete-variable 'display-buffer-function
5371 'display-buffer-alist "24.3")
5373 ;; Eventually, we want to turn this into a defvar; instead of
5374 ;; customizing this, the user should use a `pop-up-frame-parameters'
5375 ;; alist entry in `display-buffer-base-action'.
5376 (defcustom pop-up-frame-alist nil
5377 "Alist of parameters for automatically generated new frames.
5378 If non-nil, the value you specify here is used by the default
5379 `pop-up-frame-function' for the creation of new frames.
5381 Since `pop-up-frame-function' is used by `display-buffer' for
5382 making new frames, any value specified here by default affects
5383 the automatic generation of new frames via `display-buffer' and
5384 all functions based on it. The behavior of `make-frame' is not
5385 affected by this variable."
5386 :type '(repeat (cons :format "%v"
5387 (symbol :tag "Parameter")
5388 (sexp :tag "Value")))
5389 :group 'frames)
5391 (defcustom pop-up-frame-function
5392 (lambda () (make-frame pop-up-frame-alist))
5393 "Function used by `display-buffer' for creating a new frame.
5394 This function is called with no arguments and should return a new
5395 frame. The default value calls `make-frame' with the argument
5396 `pop-up-frame-alist'."
5397 :type 'function
5398 :group 'frames)
5400 (defcustom special-display-buffer-names nil
5401 "List of names of buffers that should be displayed specially.
5402 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5403 its name is in this list, displays the buffer in a way specified
5404 by `special-display-function'. `special-display-popup-frame'
5405 \(the default for `special-display-function') usually displays
5406 the buffer in a separate frame made with the parameters specified
5407 by `special-display-frame-alist'. If `special-display-function'
5408 has been set to some other function, that function is called with
5409 the buffer as first, and nil as second argument.
5411 Alternatively, an element of this list can be specified as
5412 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
5413 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5414 `special-display-popup-frame' will interpret such pairs as frame
5415 parameters when it creates a special frame, overriding the
5416 corresponding values from `special-display-frame-alist'.
5418 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5419 `special-display-popup-frame' displays that buffer in the
5420 selected window. If FRAME-PARAMETERS contains (same-frame . t),
5421 it displays that buffer in a window on the selected frame.
5423 If `special-display-function' specifies some other function than
5424 `special-display-popup-frame', that function is called with the
5425 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5426 argument.
5428 Finally, an element of this list can be also specified as
5429 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5430 `special-display-popup-frame' will call FUNCTION with the buffer
5431 named BUFFER-NAME as first argument, and OTHER-ARGS as the
5432 second.
5434 Any alternative function specified here is responsible for
5435 setting up the quit-restore parameter of the window used.
5437 If this variable appears \"not to work\", because you added a
5438 name to it but the corresponding buffer is displayed in the
5439 selected window, look at the values of `same-window-buffer-names'
5440 and `same-window-regexps'. Those variables take precedence over
5441 this one.
5443 See also `special-display-regexps'."
5444 :type '(repeat
5445 (choice :tag "Buffer"
5446 :value ""
5447 (string :format "%v")
5448 (cons :tag "With parameters"
5449 :format "%v"
5450 :value ("" . nil)
5451 (string :format "%v")
5452 (repeat :tag "Parameters"
5453 (cons :format "%v"
5454 (symbol :tag "Parameter")
5455 (sexp :tag "Value"))))
5456 (list :tag "With function"
5457 :format "%v"
5458 :value ("" . nil)
5459 (string :format "%v")
5460 (function :tag "Function")
5461 (repeat :tag "Arguments" (sexp)))))
5462 :group 'windows
5463 :group 'frames)
5464 (make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3")
5465 (put 'special-display-buffer-names 'risky-local-variable t)
5467 (defcustom special-display-regexps nil
5468 "List of regexps saying which buffers should be displayed specially.
5469 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5470 any regexp in this list matches its name, displays it specially
5471 using `special-display-function'. `special-display-popup-frame'
5472 \(the default for `special-display-function') usually displays
5473 the buffer in a separate frame made with the parameters specified
5474 by `special-display-frame-alist'. If `special-display-function'
5475 has been set to some other function, that function is called with
5476 the buffer as first, and nil as second argument.
5478 Alternatively, an element of this list can be specified as
5479 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
5480 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5481 `special-display-popup-frame' will then interpret these pairs as
5482 frame parameters when creating a special frame for a buffer whose
5483 name matches REGEXP, overriding the corresponding values from
5484 `special-display-frame-alist'.
5486 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5487 `special-display-popup-frame' displays buffers matching REGEXP in
5488 the selected window. (same-frame . t) in FRAME-PARAMETERS means
5489 to display such buffers in a window on the selected frame.
5491 If `special-display-function' specifies some other function than
5492 `special-display-popup-frame', that function is called with the
5493 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
5494 as second argument.
5496 Finally, an element of this list can be also specified as
5497 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
5498 will then call FUNCTION with the buffer whose name matched
5499 REGEXP as first, and OTHER-ARGS as second argument.
5501 Any alternative function specified here is responsible for
5502 setting up the quit-restore parameter of the window used.
5504 If this variable appears \"not to work\", because you added a
5505 name to it but the corresponding buffer is displayed in the
5506 selected window, look at the values of `same-window-buffer-names'
5507 and `same-window-regexps'. Those variables take precedence over
5508 this one.
5510 See also `special-display-buffer-names'."
5511 :type '(repeat
5512 (choice :tag "Buffer"
5513 :value ""
5514 (regexp :format "%v")
5515 (cons :tag "With parameters"
5516 :format "%v"
5517 :value ("" . nil)
5518 (regexp :format "%v")
5519 (repeat :tag "Parameters"
5520 (cons :format "%v"
5521 (symbol :tag "Parameter")
5522 (sexp :tag "Value"))))
5523 (list :tag "With function"
5524 :format "%v"
5525 :value ("" . nil)
5526 (regexp :format "%v")
5527 (function :tag "Function")
5528 (repeat :tag "Arguments" (sexp)))))
5529 :group 'windows
5530 :group 'frames)
5531 (make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3")
5532 (put 'special-display-regexps 'risky-local-variable t)
5534 (defun special-display-p (buffer-name)
5535 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
5536 More precisely, return t if `special-display-buffer-names' or
5537 `special-display-regexps' contain a string entry equaling or
5538 matching BUFFER-NAME. If `special-display-buffer-names' or
5539 `special-display-regexps' contain a list entry whose car equals
5540 or matches BUFFER-NAME, the return value is the cdr of that
5541 entry."
5542 (let (tmp)
5543 (cond
5544 ((member buffer-name special-display-buffer-names)
5546 ((setq tmp (assoc buffer-name special-display-buffer-names))
5547 (cdr tmp))
5548 ((catch 'found
5549 (dolist (regexp special-display-regexps)
5550 (cond
5551 ((stringp regexp)
5552 (when (string-match-p regexp buffer-name)
5553 (throw 'found t)))
5554 ((and (consp regexp) (stringp (car regexp))
5555 (string-match-p (car regexp) buffer-name))
5556 (throw 'found (cdr regexp))))))))))
5558 (defcustom special-display-frame-alist
5559 '((height . 14) (width . 80) (unsplittable . t))
5560 "Alist of parameters for special frames.
5561 Special frames are used for buffers whose names are listed in
5562 `special-display-buffer-names' and for buffers whose names match
5563 one of the regular expressions in `special-display-regexps'.
5565 This variable can be set in your init file, like this:
5567 (setq special-display-frame-alist '((width . 80) (height . 20)))
5569 These supersede the values given in `default-frame-alist'."
5570 :type '(repeat (cons :format "%v"
5571 (symbol :tag "Parameter")
5572 (sexp :tag "Value")))
5573 :group 'frames)
5574 (make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3")
5576 (defun special-display-popup-frame (buffer &optional args)
5577 "Pop up a frame displaying BUFFER and return its window.
5578 If BUFFER is already displayed in a visible or iconified frame,
5579 raise that frame. Otherwise, display BUFFER in a new frame.
5581 Optional argument ARGS is a list specifying additional
5582 information.
5584 If ARGS is an alist, use it as a list of frame parameters. If
5585 these parameters contain (same-window . t), display BUFFER in
5586 the selected window. If they contain (same-frame . t), display
5587 BUFFER in a window of the selected frame.
5589 If ARGS is a list whose car is a symbol, use (car ARGS) as a
5590 function to do the work. Pass it BUFFER as first argument, and
5591 pass the elements of (cdr ARGS) as the remaining arguments."
5592 (if (and args (symbolp (car args)))
5593 (apply (car args) buffer (cdr args))
5594 (let ((window (get-buffer-window buffer 0)))
5596 ;; If we have a window already, make it visible.
5597 (when window
5598 (let ((frame (window-frame window)))
5599 (make-frame-visible frame)
5600 (raise-frame frame)
5601 (display-buffer-record-window 'reuse window buffer)
5602 window))
5603 ;; Reuse the current window if the user requested it.
5604 (when (cdr (assq 'same-window args))
5605 (condition-case nil
5606 (progn (switch-to-buffer buffer nil t) (selected-window))
5607 (error nil)))
5608 ;; Stay on the same frame if requested.
5609 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
5610 (let* ((pop-up-windows t)
5611 pop-up-frames
5612 special-display-buffer-names special-display-regexps)
5613 (display-buffer buffer)))
5614 ;; If no window yet, make one in a new frame.
5615 (let* ((frame
5616 (with-current-buffer buffer
5617 (make-frame (append args special-display-frame-alist))))
5618 (window (frame-selected-window frame)))
5619 (display-buffer-record-window 'frame window buffer)
5620 (unless (eq buffer (window-buffer window))
5621 (set-window-buffer window buffer)
5622 (set-window-prev-buffers window nil))
5623 (set-window-dedicated-p window t)
5624 window)))))
5626 (defcustom special-display-function 'special-display-popup-frame
5627 "Function to call for displaying special buffers.
5628 This function is called with two arguments - the buffer and,
5629 optionally, a list - and should return a window displaying that
5630 buffer. The default value usually makes a separate frame for the
5631 buffer using `special-display-frame-alist' to specify the frame
5632 parameters. See the definition of `special-display-popup-frame'
5633 for how to specify such a function.
5635 A buffer is special when its name is either listed in
5636 `special-display-buffer-names' or matches a regexp in
5637 `special-display-regexps'.
5639 The specified function should call `display-buffer-record-window'
5640 with corresponding arguments to set up the quit-restore parameter
5641 of the window used."
5642 :type 'function
5643 :group 'frames)
5644 (make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3")
5646 (defcustom same-window-buffer-names nil
5647 "List of names of buffers that should appear in the \"same\" window.
5648 `display-buffer' and `pop-to-buffer' show a buffer whose name is
5649 on this list in the selected rather than some other window.
5651 An element of this list can be a cons cell instead of just a
5652 string. In that case, the cell's car must be a string specifying
5653 the buffer name. This is for compatibility with
5654 `special-display-buffer-names'; the cdr of the cons cell is
5655 ignored.
5657 See also `same-window-regexps'."
5658 :type '(repeat (string :format "%v"))
5659 :group 'windows)
5661 (defcustom same-window-regexps nil
5662 "List of regexps saying which buffers should appear in the \"same\" window.
5663 `display-buffer' and `pop-to-buffer' show a buffer whose name
5664 matches a regexp on this list in the selected rather than some
5665 other window.
5667 An element of this list can be a cons cell instead of just a
5668 string. In that case, the cell's car must be a regexp matching
5669 the buffer name. This is for compatibility with
5670 `special-display-regexps'; the cdr of the cons cell is ignored.
5672 See also `same-window-buffer-names'."
5673 :type '(repeat (regexp :format "%v"))
5674 :group 'windows)
5676 (defun same-window-p (buffer-name)
5677 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5678 This function returns non-nil if `display-buffer' or
5679 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
5680 selected rather than (as usual) some other window. See
5681 `same-window-buffer-names' and `same-window-regexps'."
5682 (cond
5683 ((not (stringp buffer-name)))
5684 ;; The elements of `same-window-buffer-names' can be buffer
5685 ;; names or cons cells whose cars are buffer names.
5686 ((member buffer-name same-window-buffer-names))
5687 ((assoc buffer-name same-window-buffer-names))
5688 ((catch 'found
5689 (dolist (regexp same-window-regexps)
5690 ;; The elements of `same-window-regexps' can be regexps
5691 ;; or cons cells whose cars are regexps.
5692 (when (or (and (stringp regexp)
5693 (string-match-p regexp buffer-name))
5694 (and (consp regexp) (stringp (car regexp))
5695 (string-match-p (car regexp) buffer-name)))
5696 (throw 'found t)))))))
5698 (defcustom pop-up-frames nil
5699 "Whether `display-buffer' should make a separate frame.
5700 If nil, never make a separate frame.
5701 If the value is `graphic-only', make a separate frame
5702 on graphic displays only.
5703 Any other non-nil value means always make a separate frame."
5704 :type '(choice
5705 (const :tag "Never" nil)
5706 (const :tag "On graphic displays only" graphic-only)
5707 (const :tag "Always" t))
5708 :group 'windows)
5710 (defcustom display-buffer-reuse-frames nil
5711 "Non-nil means `display-buffer' should reuse frames.
5712 If the buffer in question is already displayed in a frame, raise
5713 that frame."
5714 :type 'boolean
5715 :version "21.1"
5716 :group 'windows)
5718 (make-obsolete-variable
5719 'display-buffer-reuse-frames
5720 "use a `reusable-frames' alist entry in `display-buffer-alist'."
5721 "24.3")
5723 (defcustom pop-up-windows t
5724 "Non-nil means `display-buffer' should make a new window."
5725 :type 'boolean
5726 :group 'windows)
5728 (defcustom split-window-preferred-function 'split-window-sensibly
5729 "Function called by `display-buffer' routines to split a window.
5730 This function is called with a window as single argument and is
5731 supposed to split that window and return the new window. If the
5732 window can (or shall) not be split, it is supposed to return nil.
5733 The default is to call the function `split-window-sensibly' which
5734 tries to split the window in a way which seems most suitable.
5735 You can customize the options `split-height-threshold' and/or
5736 `split-width-threshold' in order to have `split-window-sensibly'
5737 prefer either vertical or horizontal splitting.
5739 If you set this to any other function, bear in mind that the
5740 `display-buffer' routines may call this function two times. The
5741 argument of the first call is the largest window on its frame.
5742 If that call fails to return a live window, the function is
5743 called again with the least recently used window as argument. If
5744 that call fails too, `display-buffer' will use an existing window
5745 to display its buffer.
5747 The window selected at the time `display-buffer' was invoked is
5748 still selected when this function is called. Hence you can
5749 compare the window argument with the value of `selected-window'
5750 if you intend to split the selected window instead or if you do
5751 not want to split the selected window."
5752 :type 'function
5753 :version "23.1"
5754 :group 'windows)
5756 (defcustom split-height-threshold 80
5757 "Minimum height for splitting windows sensibly.
5758 If this is an integer, `split-window-sensibly' may split a window
5759 vertically only if it has at least this many lines. If this is
5760 nil, `split-window-sensibly' is not allowed to split a window
5761 vertically. If, however, a window is the only window on its
5762 frame, `split-window-sensibly' may split it vertically
5763 disregarding the value of this variable."
5764 :type '(choice (const nil) (integer :tag "lines"))
5765 :version "23.1"
5766 :group 'windows)
5768 (defcustom split-width-threshold 160
5769 "Minimum width for splitting windows sensibly.
5770 If this is an integer, `split-window-sensibly' may split a window
5771 horizontally only if it has at least this many columns. If this
5772 is nil, `split-window-sensibly' is not allowed to split a window
5773 horizontally."
5774 :type '(choice (const nil) (integer :tag "columns"))
5775 :version "23.1"
5776 :group 'windows)
5778 (defun window-splittable-p (window &optional horizontal)
5779 "Return non-nil if `split-window-sensibly' may split WINDOW.
5780 Optional argument HORIZONTAL nil or omitted means check whether
5781 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
5782 non-nil means check whether WINDOW may be split horizontally.
5784 WINDOW may be split vertically when the following conditions
5785 hold:
5786 - `window-size-fixed' is either nil or equals `width' for the
5787 buffer of WINDOW.
5788 - `split-height-threshold' is an integer and WINDOW is at least as
5789 high as `split-height-threshold'.
5790 - When WINDOW is split evenly, the emanating windows are at least
5791 `window-min-height' lines tall and can accommodate at least one
5792 line plus - if WINDOW has one - a mode line.
5794 WINDOW may be split horizontally when the following conditions
5795 hold:
5796 - `window-size-fixed' is either nil or equals `height' for the
5797 buffer of WINDOW.
5798 - `split-width-threshold' is an integer and WINDOW is at least as
5799 wide as `split-width-threshold'.
5800 - When WINDOW is split evenly, the emanating windows are at least
5801 `window-min-width' or two (whichever is larger) columns wide."
5802 (when (and (window-live-p window) (not (window--side-window-p window)))
5803 (with-current-buffer (window-buffer window)
5804 (if horizontal
5805 ;; A window can be split horizontally when its width is not
5806 ;; fixed, it is at least `split-width-threshold' columns wide
5807 ;; and at least twice as wide as `window-min-width' and 2 (the
5808 ;; latter value is hardcoded).
5809 (and (memq window-size-fixed '(nil height))
5810 ;; Testing `window-full-width-p' here hardly makes any
5811 ;; sense nowadays. This can be done more intuitively by
5812 ;; setting up `split-width-threshold' appropriately.
5813 (numberp split-width-threshold)
5814 (>= (window-width window)
5815 (max split-width-threshold
5816 (* 2 (max window-min-width 2)))))
5817 ;; A window can be split vertically when its height is not
5818 ;; fixed, it is at least `split-height-threshold' lines high,
5819 ;; and it is at least twice as high as `window-min-height' and 2
5820 ;; if it has a mode line or 1.
5821 (and (memq window-size-fixed '(nil width))
5822 (numberp split-height-threshold)
5823 (>= (window-height window)
5824 (max split-height-threshold
5825 (* 2 (max window-min-height
5826 (if mode-line-format 2 1))))))))))
5828 (defun split-window-sensibly (&optional window)
5829 "Split WINDOW in a way suitable for `display-buffer'.
5830 WINDOW defaults to the currently selected window.
5831 If `split-height-threshold' specifies an integer, WINDOW is at
5832 least `split-height-threshold' lines tall and can be split
5833 vertically, split WINDOW into two windows one above the other and
5834 return the lower window. Otherwise, if `split-width-threshold'
5835 specifies an integer, WINDOW is at least `split-width-threshold'
5836 columns wide and can be split horizontally, split WINDOW into two
5837 windows side by side and return the window on the right. If this
5838 can't be done either and WINDOW is the only window on its frame,
5839 try to split WINDOW vertically disregarding any value specified
5840 by `split-height-threshold'. If that succeeds, return the lower
5841 window. Return nil otherwise.
5843 By default `display-buffer' routines call this function to split
5844 the largest or least recently used window. To change the default
5845 customize the option `split-window-preferred-function'.
5847 You can enforce this function to not split WINDOW horizontally,
5848 by setting (or binding) the variable `split-width-threshold' to
5849 nil. If, in addition, you set `split-height-threshold' to zero,
5850 chances increase that this function does split WINDOW vertically.
5852 In order to not split WINDOW vertically, set (or bind) the
5853 variable `split-height-threshold' to nil. Additionally, you can
5854 set `split-width-threshold' to zero to make a horizontal split
5855 more likely to occur.
5857 Have a look at the function `window-splittable-p' if you want to
5858 know how `split-window-sensibly' determines whether WINDOW can be
5859 split."
5860 (let ((window (or window (selected-window))))
5861 (or (and (window-splittable-p window)
5862 ;; Split window vertically.
5863 (with-selected-window window
5864 (split-window-below)))
5865 (and (window-splittable-p window t)
5866 ;; Split window horizontally.
5867 (with-selected-window window
5868 (split-window-right)))
5869 (and (eq window (frame-root-window (window-frame window)))
5870 (not (window-minibuffer-p window))
5871 ;; If WINDOW is the only window on its frame and is not the
5872 ;; minibuffer window, try to split it vertically disregarding
5873 ;; the value of `split-height-threshold'.
5874 (let ((split-height-threshold 0))
5875 (when (window-splittable-p window)
5876 (with-selected-window window
5877 (split-window-below))))))))
5879 (defun window--try-to-split-window (window &optional alist)
5880 "Try to split WINDOW.
5881 Return value returned by `split-window-preferred-function' if it
5882 represents a live window, nil otherwise."
5883 (and (window-live-p window)
5884 (not (frame-parameter (window-frame window) 'unsplittable))
5885 (let* ((window-combination-limit
5886 ;; When `window-combination-limit' equals
5887 ;; `display-buffer' or equals `resize-window' and a
5888 ;; `window-height' or `window-width' alist entry are
5889 ;; present, bind it to t so resizing steals space
5890 ;; preferably from the window that was split.
5891 (if (or (eq window-combination-limit 'display-buffer)
5892 (and (eq window-combination-limit 'window-size)
5893 (or (cdr (assq 'window-height alist))
5894 (cdr (assq 'window-width alist)))))
5896 window-combination-limit))
5897 (new-window
5898 ;; Since `split-window-preferred-function' might
5899 ;; throw an error use `condition-case'.
5900 (condition-case nil
5901 (funcall split-window-preferred-function window)
5902 (error nil))))
5903 (and (window-live-p new-window) new-window))))
5905 (defun window--frame-usable-p (frame)
5906 "Return FRAME if it can be used to display a buffer."
5907 (when (frame-live-p frame)
5908 (let ((window (frame-root-window frame)))
5909 ;; `frame-root-window' may be an internal window which is considered
5910 ;; "dead" by `window-live-p'. Hence if `window' is not live we
5911 ;; implicitly know that `frame' has a visible window we can use.
5912 (unless (and (window-live-p window)
5913 (or (window-minibuffer-p window)
5914 ;; If the window is soft-dedicated, the frame is usable.
5915 ;; Actually, even if the window is really dedicated,
5916 ;; the frame is still usable by splitting it.
5917 ;; At least Emacs-22 allowed it, and it is desirable
5918 ;; when displaying same-frame windows.
5919 nil ; (eq t (window-dedicated-p window))
5921 frame))))
5923 (defcustom even-window-heights t
5924 "If non-nil `display-buffer' will try to even window heights.
5925 Otherwise `display-buffer' will leave the window configuration
5926 alone. Heights are evened only when `display-buffer' chooses a
5927 window that appears above or below the selected window."
5928 :type 'boolean
5929 :group 'windows)
5931 (defun window--even-window-heights (window)
5932 "Even heights of WINDOW and selected window.
5933 Do this only if these windows are vertically adjacent to each
5934 other, `even-window-heights' is non-nil, and the selected window
5935 is higher than WINDOW."
5936 (when (and even-window-heights
5937 ;; Even iff WINDOW forms a vertical combination with the
5938 ;; selected window, and WINDOW's height exceeds that of the
5939 ;; selected window, see also bug#11880.
5940 (window-combined-p window)
5941 (= (window-child-count (window-parent window)) 2)
5942 (eq (window-parent) (window-parent window))
5943 (> (window-total-height) (window-total-height window)))
5944 ;; Don't throw an error if we can't even window heights for
5945 ;; whatever reason.
5946 (condition-case nil
5947 (enlarge-window
5948 (/ (- (window-total-height window) (window-total-height)) 2))
5949 (error nil))))
5951 (defun window--display-buffer (buffer window type &optional alist dedicated)
5952 "Display BUFFER in WINDOW.
5953 TYPE must be one of the symbols `reuse', `window' or `frame' and
5954 is passed unaltered to `display-buffer-record-window'. ALIST is
5955 the alist argument of `display-buffer'. Set `window-dedicated-p'
5956 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
5957 live."
5958 (when (and (buffer-live-p buffer) (window-live-p window))
5959 (display-buffer-record-window type window buffer)
5960 (unless (eq buffer (window-buffer window))
5961 (set-window-dedicated-p window nil)
5962 (set-window-buffer window buffer)
5963 (when dedicated
5964 (set-window-dedicated-p window dedicated))
5965 (when (memq type '(window frame))
5966 (set-window-prev-buffers window nil)))
5967 (let ((parameter (window-parameter window 'quit-restore))
5968 (height (cdr (assq 'window-height alist)))
5969 (width (cdr (assq 'window-width alist)))
5970 (size (cdr (assq 'window-size alist))))
5971 (cond
5972 ((or (eq type 'frame)
5973 (and (eq (car parameter) 'same)
5974 (eq (nth 1 parameter) 'frame)))
5975 ;; Adjust size of frame if asked for.
5976 (cond
5977 ((not size))
5978 ((consp size)
5979 (let ((width (car size))
5980 (height (cdr size))
5981 (frame (window-frame window)))
5982 (when (and (numberp width) (numberp height))
5983 (set-frame-height
5984 frame (+ (frame-height frame)
5985 (- height (window-total-height window))))
5986 (set-frame-width
5987 frame (+ (frame-width frame)
5988 (- width (window-total-width window)))))))
5989 ((functionp size)
5990 (ignore-errors (funcall size window)))))
5991 ((or (eq type 'window)
5992 (and (eq (car parameter) 'same)
5993 (eq (nth 1 parameter) 'window)))
5994 ;; Adjust height of window if asked for.
5995 (cond
5996 ((not height))
5997 ((numberp height)
5998 (let* ((new-height
5999 (if (integerp height)
6000 height
6001 (round
6002 (* (window-total-height (frame-root-window window))
6003 height))))
6004 (delta (- new-height (window-total-height window))))
6005 (when (and (window--resizable-p window delta nil 'safe)
6006 (window-combined-p window))
6007 (window-resize window delta nil 'safe))))
6008 ((functionp height)
6009 (ignore-errors (funcall height window))))
6010 ;; Adjust width of window if asked for.
6011 (cond
6012 ((not width))
6013 ((numberp width)
6014 (let* ((new-width
6015 (if (integerp width)
6016 width
6017 (round
6018 (* (window-total-width (frame-root-window window))
6019 width))))
6020 (delta (- new-width (window-total-width window))))
6021 (when (and (window--resizable-p window delta t 'safe)
6022 (window-combined-p window t))
6023 (window-resize window delta t 'safe))))
6024 ((functionp width)
6025 (ignore-errors (funcall width window)))))))
6027 window))
6029 (defun window--maybe-raise-frame (frame)
6030 (let ((visible (frame-visible-p frame)))
6031 (unless (or (not visible)
6032 ;; Assume the selected frame is already visible enough.
6033 (eq frame (selected-frame))
6034 ;; Assume the frame from which we invoked the
6035 ;; minibuffer is visible.
6036 (and (minibuffer-window-active-p (selected-window))
6037 (eq frame (window-frame (minibuffer-selected-window)))))
6038 (raise-frame frame))))
6040 ;; FIXME: Not implemented.
6041 ;; FIXME: By the way, there could be more levels of dedication:
6042 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
6043 ;; the window hasn't been used for something else yet.
6044 ;; - `soft' (`softly') dedicated only allows reuse when asked explicitly.
6045 ;; - `strongly' never allows reuse.
6046 (defvar display-buffer-mark-dedicated nil
6047 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
6048 The actual non-nil value of this variable will be copied to the
6049 `window-dedicated-p' flag.")
6051 (defconst display-buffer--action-function-custom-type
6052 '(choice :tag "Function"
6053 (const :tag "--" ignore) ; default for insertion
6054 (const display-buffer-reuse-window)
6055 (const display-buffer-pop-up-window)
6056 (const display-buffer-same-window)
6057 (const display-buffer-pop-up-frame)
6058 (const display-buffer-below-selected)
6059 (const display-buffer-at-bottom)
6060 (const display-buffer-in-previous-window)
6061 (const display-buffer-use-some-window)
6062 (function :tag "Other function"))
6063 "Custom type for `display-buffer' action functions.")
6065 (defconst display-buffer--action-custom-type
6066 `(cons :tag "Action"
6067 (choice :tag "Action functions"
6068 ,display-buffer--action-function-custom-type
6069 (repeat
6070 :tag "List of functions"
6071 ,display-buffer--action-function-custom-type))
6072 (alist :tag "Action arguments"
6073 :key-type symbol
6074 :value-type (sexp :tag "Value")))
6075 "Custom type for `display-buffer' actions.")
6077 (defvar display-buffer-overriding-action '(nil . nil)
6078 "Overriding action to perform to display a buffer.
6079 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6080 function or a list of functions. Each function should accept two
6081 arguments: a buffer to display and an alist similar to ALIST.
6082 See `display-buffer' for details.")
6083 (put 'display-buffer-overriding-action 'risky-local-variable t)
6085 (defcustom display-buffer-alist nil
6086 "Alist of conditional actions for `display-buffer'.
6087 This is a list of elements (CONDITION . ACTION), where:
6089 CONDITION is either a regexp matching buffer names, or a
6090 function that takes two arguments - a buffer name and the
6091 ACTION argument of `display-buffer' - and returns a boolean.
6093 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
6094 function or a list of functions. Each such function should
6095 accept two arguments: a buffer to display and an alist of the
6096 same form as ALIST. See `display-buffer' for details.
6098 `display-buffer' scans this alist until it either finds a
6099 matching regular expression or the function specified by a
6100 condition returns non-nil. In any of these cases, it adds the
6101 associated action to the list of actions it will try."
6102 :type `(alist :key-type
6103 (choice :tag "Condition"
6104 regexp
6105 (function :tag "Matcher function"))
6106 :value-type ,display-buffer--action-custom-type)
6107 :risky t
6108 :version "24.1"
6109 :group 'windows)
6111 (defcustom display-buffer-base-action '(nil . nil)
6112 "User-specified default action for `display-buffer'.
6113 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6114 function or a list of functions. Each function should accept two
6115 arguments: a buffer to display and an alist similar to ALIST.
6116 See `display-buffer' for details."
6117 :type display-buffer--action-custom-type
6118 :risky t
6119 :version "24.1"
6120 :group 'windows)
6122 (defconst display-buffer-fallback-action
6123 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
6124 display-buffer-reuse-window
6125 display-buffer--maybe-pop-up-frame-or-window
6126 display-buffer-in-previous-window
6127 display-buffer-use-some-window
6128 ;; If all else fails, pop up a new frame.
6129 display-buffer-pop-up-frame))
6130 "Default fallback action for `display-buffer'.
6131 This is the action used by `display-buffer' if no other actions
6132 specified, e.g. by the user options `display-buffer-alist' or
6133 `display-buffer-base-action'. See `display-buffer'.")
6134 (put 'display-buffer-fallback-action 'risky-local-variable t)
6136 (defun display-buffer-assq-regexp (buffer-name alist action)
6137 "Retrieve ALIST entry corresponding to BUFFER-NAME.
6138 ACTION is the action argument passed to `display-buffer'."
6139 (catch 'match
6140 (dolist (entry alist)
6141 (let ((key (car entry)))
6142 (when (or (and (stringp key)
6143 (string-match-p key buffer-name))
6144 (and (functionp key)
6145 (funcall key buffer-name action)))
6146 (throw 'match (cdr entry)))))))
6148 (defvar display-buffer--same-window-action
6149 '(display-buffer-same-window
6150 (inhibit-same-window . nil))
6151 "A `display-buffer' action for displaying in the same window.")
6152 (put 'display-buffer--same-window-action 'risky-local-variable t)
6154 (defvar display-buffer--other-frame-action
6155 '((display-buffer-reuse-window
6156 display-buffer-pop-up-frame)
6157 (reusable-frames . 0)
6158 (inhibit-same-window . t))
6159 "A `display-buffer' action for displaying in another frame.")
6160 (put 'display-buffer--other-frame-action 'risky-local-variable t)
6162 (defun display-buffer (buffer-or-name &optional action frame)
6163 "Display BUFFER-OR-NAME in some window, without selecting it.
6164 BUFFER-OR-NAME must be a buffer or the name of an existing
6165 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
6166 or nil if no such window is found.
6168 Optional argument ACTION, if non-nil, should specify a display
6169 action. Its form is described below.
6171 Optional argument FRAME, if non-nil, acts like an additional
6172 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
6173 specifying the frame(s) to search for a window that is already
6174 displaying the buffer. See `display-buffer-reuse-window'
6176 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
6177 where FUNCTION is either a function or a list of functions, and
6178 ALIST is an arbitrary association list (alist).
6180 Each such FUNCTION should accept two arguments: the buffer to
6181 display and an alist. Based on those arguments, it should
6182 display the buffer and return the window. If the caller is
6183 prepared to handle the case of not displaying the buffer
6184 and returning nil from `display-buffer' it should pass
6185 \(allow-no-window . t) as an element of the ALIST.
6187 The `display-buffer' function builds a function list and an alist
6188 by combining the functions and alists specified in
6189 `display-buffer-overriding-action', `display-buffer-alist', the
6190 ACTION argument, `display-buffer-base-action', and
6191 `display-buffer-fallback-action' (in order). Then it calls each
6192 function in the combined function list in turn, passing the
6193 buffer as the first argument and the combined alist as the second
6194 argument, until one of the functions returns non-nil.
6196 If ACTION is nil, the function list and the alist are built using
6197 only the other variables mentioned above.
6199 Available action functions include:
6200 `display-buffer-same-window'
6201 `display-buffer-reuse-window'
6202 `display-buffer-pop-up-frame'
6203 `display-buffer-pop-up-window'
6204 `display-buffer-in-previous-window'
6205 `display-buffer-use-some-window'
6207 Recognized alist entries include:
6209 `inhibit-same-window' -- A non-nil value prevents the same
6210 window from being used for display.
6212 `inhibit-switch-frame' -- A non-nil value prevents any other
6213 frame from being raised or selected,
6214 even if the window is displayed there.
6216 `reusable-frames' -- Value specifies frame(s) to search for a
6217 window that already displays the buffer.
6218 See `display-buffer-reuse-window'.
6220 `pop-up-frame-parameters' -- Value specifies an alist of frame
6221 parameters to give a new frame, if
6222 one is created.
6224 `window-height' -- Value specifies either an integer (the number
6225 of lines of a new window), a floating point number (the
6226 fraction of a new window with respect to the height of the
6227 frame's root window) or a function to be called with one
6228 argument - a new window. The function is supposed to adjust
6229 the height of the window; its return value is ignored.
6230 Suitable functions are `shrink-window-if-larger-than-buffer'
6231 and `fit-window-to-buffer'.
6233 `window-width' -- Value specifies either an integer (the number
6234 of columns of a new window), a floating point number (the
6235 fraction of a new window with respect to the width of the
6236 frame's root window) or a function to be called with one
6237 argument - a new window. The function is supposed to adjust
6238 the width of the window; its return value is ignored.
6240 `allow-no-window' -- A non-nil value indicates readiness for the case
6241 of not displaying the buffer and FUNCTION can safely return
6242 a non-window value to suppress displaying.
6244 The ACTION argument to `display-buffer' can also have a non-nil
6245 and non-list value. This means to display the buffer in a window
6246 other than the selected one, even if it is already displayed in
6247 the selected window. If called interactively with a prefix
6248 argument, ACTION is t."
6249 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6250 (if current-prefix-arg t)))
6251 (let ((buffer (if (bufferp buffer-or-name)
6252 buffer-or-name
6253 (get-buffer buffer-or-name)))
6254 ;; Make sure that when we split windows the old window keeps
6255 ;; point, bug#14829.
6256 (split-window-keep-point t)
6257 ;; Handle the old form of the first argument.
6258 (inhibit-same-window (and action (not (listp action)))))
6259 (unless (listp action) (setq action nil))
6260 (if display-buffer-function
6261 ;; If `display-buffer-function' is defined, let it do the job.
6262 (funcall display-buffer-function buffer inhibit-same-window)
6263 ;; Otherwise, use the defined actions.
6264 (let* ((user-action
6265 (display-buffer-assq-regexp
6266 (buffer-name buffer) display-buffer-alist action))
6267 (special-action (display-buffer--special-action buffer))
6268 ;; Extra actions from the arguments to this function:
6269 (extra-action
6270 (cons nil (append (if inhibit-same-window
6271 '((inhibit-same-window . t)))
6272 (if frame
6273 `((reusable-frames . ,frame))))))
6274 ;; Construct action function list and action alist.
6275 (actions (list display-buffer-overriding-action
6276 user-action special-action action extra-action
6277 display-buffer-base-action
6278 display-buffer-fallback-action))
6279 (functions (apply 'append
6280 (mapcar (lambda (x)
6281 (setq x (car x))
6282 (if (functionp x) (list x) x))
6283 actions)))
6284 (alist (apply 'append (mapcar 'cdr actions)))
6285 window)
6286 (unless (buffer-live-p buffer)
6287 (error "Invalid buffer"))
6288 (while (and functions (not window))
6289 (setq window (funcall (car functions) buffer alist)
6290 functions (cdr functions)))
6291 (and (windowp window) window)))))
6293 (defun display-buffer-other-frame (buffer)
6294 "Display buffer BUFFER preferably in another frame.
6295 This uses the function `display-buffer' as a subroutine; see
6296 its documentation for additional customization information."
6297 (interactive "BDisplay buffer in other frame: ")
6298 (display-buffer buffer display-buffer--other-frame-action t))
6300 ;;; `display-buffer' action functions:
6302 (defun display-buffer-same-window (buffer alist)
6303 "Display BUFFER in the selected window.
6304 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
6305 if the selected window is a minibuffer window or is dedicated to
6306 another buffer; in that case, return nil. Otherwise, return the
6307 selected window."
6308 (unless (or (cdr (assq 'inhibit-same-window alist))
6309 (window-minibuffer-p)
6310 (window-dedicated-p))
6311 (window--display-buffer buffer (selected-window) 'reuse alist)))
6313 (defun display-buffer--maybe-same-window (buffer alist)
6314 "Conditionally display BUFFER in the selected window.
6315 If `same-window-p' returns non-nil for BUFFER's name, call
6316 `display-buffer-same-window' and return its value. Otherwise,
6317 return nil."
6318 (and (same-window-p (buffer-name buffer))
6319 (display-buffer-same-window buffer alist)))
6321 (defun display-buffer-reuse-window (buffer alist)
6322 "Return a window that is already displaying BUFFER.
6323 Return nil if no usable window is found.
6325 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6326 window is not eligible for reuse.
6328 If ALIST contains a `reusable-frames' entry, its value determines
6329 which frames to search for a reusable window:
6330 nil -- the selected frame (actually the last non-minibuffer frame)
6331 A frame -- just that frame
6332 `visible' -- all visible frames
6333 0 -- all frames on the current terminal
6334 t -- all frames.
6336 If ALIST contains no `reusable-frames' entry, search just the
6337 selected frame if `display-buffer-reuse-frames' and
6338 `pop-up-frames' are both nil; search all frames on the current
6339 terminal if either of those variables is non-nil.
6341 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6342 event that a window on another frame is chosen, avoid raising
6343 that frame."
6344 (let* ((alist-entry (assq 'reusable-frames alist))
6345 (frames (cond (alist-entry (cdr alist-entry))
6346 ((if (eq pop-up-frames 'graphic-only)
6347 (display-graphic-p)
6348 pop-up-frames)
6350 (display-buffer-reuse-frames 0)
6351 (t (last-nonminibuffer-frame))))
6352 (window (if (and (eq buffer (window-buffer))
6353 (not (cdr (assq 'inhibit-same-window alist))))
6354 (selected-window)
6355 (car (delq (selected-window)
6356 (get-buffer-window-list buffer 'nomini
6357 frames))))))
6358 (when (window-live-p window)
6359 (prog1 (window--display-buffer buffer window 'reuse alist)
6360 (unless (cdr (assq 'inhibit-switch-frame alist))
6361 (window--maybe-raise-frame (window-frame window)))))))
6363 (defun display-buffer--special-action (buffer)
6364 "Return special display action for BUFFER, if any.
6365 If `special-display-p' returns non-nil for BUFFER, return an
6366 appropriate display action involving `special-display-function'.
6367 See `display-buffer' for the format of display actions."
6368 (and special-display-function
6369 ;; `special-display-p' returns either t or a list of frame
6370 ;; parameters to pass to `special-display-function'.
6371 (let ((pars (special-display-p (buffer-name buffer))))
6372 (when pars
6373 (list (list #'display-buffer-reuse-window
6374 `(lambda (buffer _alist)
6375 (funcall special-display-function
6376 buffer ',(if (listp pars) pars)))))))))
6378 (defun display-buffer-pop-up-frame (buffer alist)
6379 "Display BUFFER in a new frame.
6380 This works by calling `pop-up-frame-function'. If successful,
6381 return the window used; otherwise return nil.
6383 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
6384 raising the new frame.
6386 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
6387 corresponding value is an alist of frame parameters to give the
6388 new frame."
6389 (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
6390 (pop-up-frame-alist (append params pop-up-frame-alist))
6391 (fun pop-up-frame-function)
6392 frame window)
6393 (when (and fun
6394 ;; Make BUFFER current so `make-frame' will use it as the
6395 ;; new frame's buffer (Bug#15133).
6396 (with-current-buffer buffer
6397 (setq frame (funcall fun)))
6398 (setq window (frame-selected-window frame)))
6399 (prog1 (window--display-buffer
6400 buffer window 'frame alist display-buffer-mark-dedicated)
6401 (unless (cdr (assq 'inhibit-switch-frame alist))
6402 (window--maybe-raise-frame frame))))))
6404 (defun display-buffer-pop-up-window (buffer alist)
6405 "Display BUFFER by popping up a new window.
6406 The new window is created on the selected frame, or in
6407 `last-nonminibuffer-frame' if no windows can be created there.
6408 If successful, return the new window; otherwise return nil.
6410 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6411 event that the new window is created on another frame, avoid
6412 raising the frame."
6413 (let ((frame (or (window--frame-usable-p (selected-frame))
6414 (window--frame-usable-p (last-nonminibuffer-frame))))
6415 window)
6416 (when (and (or (not (frame-parameter frame 'unsplittable))
6417 ;; If the selected frame cannot be split, look at
6418 ;; `last-nonminibuffer-frame'.
6419 (and (eq frame (selected-frame))
6420 (setq frame (last-nonminibuffer-frame))
6421 (window--frame-usable-p frame)
6422 (not (frame-parameter frame 'unsplittable))))
6423 ;; Attempt to split largest or least recently used window.
6424 (setq window (or (window--try-to-split-window
6425 (get-largest-window frame t) alist)
6426 (window--try-to-split-window
6427 (get-lru-window frame t) alist))))
6428 (prog1 (window--display-buffer
6429 buffer window 'window alist display-buffer-mark-dedicated)
6430 (unless (cdr (assq 'inhibit-switch-frame alist))
6431 (window--maybe-raise-frame (window-frame window)))))))
6433 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
6434 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
6436 If `pop-up-frames' is non-nil (and not `graphic-only' on a
6437 text-only terminal), try with `display-buffer-pop-up-frame'.
6439 If that cannot be done, and `pop-up-windows' is non-nil, try
6440 again with `display-buffer-pop-up-window'."
6441 (or (and (if (eq pop-up-frames 'graphic-only)
6442 (display-graphic-p)
6443 pop-up-frames)
6444 (display-buffer-pop-up-frame buffer alist))
6445 (and pop-up-windows
6446 (display-buffer-pop-up-window buffer alist))))
6448 (defun display-buffer-below-selected (buffer alist)
6449 "Try displaying BUFFER in a window below the selected window.
6450 This either splits the selected window or reuses the window below
6451 the selected one."
6452 (let (window)
6453 (or (and (setq window (window-in-direction 'below))
6454 (eq buffer (window-buffer window))
6455 (window--display-buffer buffer window 'reuse alist))
6456 (and (not (frame-parameter nil 'unsplittable))
6457 (let ((split-height-threshold 0)
6458 split-width-threshold)
6459 (setq window (window--try-to-split-window (selected-window) alist)))
6460 (window--display-buffer
6461 buffer window 'window alist display-buffer-mark-dedicated))
6462 (and (setq window (window-in-direction 'below))
6463 (not (window-dedicated-p window))
6464 (window--display-buffer
6465 buffer window 'reuse alist display-buffer-mark-dedicated)))))
6467 (defun display-buffer-at-bottom (buffer alist)
6468 "Try displaying BUFFER in a window at the bottom of the selected frame.
6469 This either reuses such a window provided it shows BUFFER
6470 already, splits a window at the bottom of the frame or the
6471 frame's root window, or reuses some window at the bottom of the
6472 selected frame."
6473 (let (bottom-window bottom-window-shows-buffer window)
6474 (walk-window-tree
6475 (lambda (window)
6476 (cond
6477 ((window-in-direction 'below window))
6478 ((and (not bottom-window-shows-buffer)
6479 (eq buffer (window-buffer window)))
6480 (setq bottom-window-shows-buffer t)
6481 (setq bottom-window window))
6482 ((not bottom-window)
6483 (setq bottom-window window)))
6484 nil nil 'nomini))
6485 (or (and bottom-window-shows-buffer
6486 (window--display-buffer
6487 buffer bottom-window 'reuse alist display-buffer-mark-dedicated))
6488 (and (not (frame-parameter nil 'unsplittable))
6489 (let (split-width-threshold)
6490 (setq window (window--try-to-split-window bottom-window alist)))
6491 (window--display-buffer
6492 buffer window 'window alist display-buffer-mark-dedicated))
6493 (and (not (frame-parameter nil 'unsplittable))
6494 (setq window
6495 (condition-case nil
6496 (split-window (window--major-non-side-window))
6497 (error nil)))
6498 (window--display-buffer
6499 buffer window 'window alist display-buffer-mark-dedicated))
6500 (and (setq window bottom-window)
6501 (not (window-dedicated-p window))
6502 (window--display-buffer
6503 buffer window 'reuse alist display-buffer-mark-dedicated)))))
6505 (defun display-buffer-in-previous-window (buffer alist)
6506 "Display BUFFER in a window previously showing it.
6507 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6508 window is not eligible for reuse.
6510 If ALIST contains a `reusable-frames' entry, its value determines
6511 which frames to search for a reusable window:
6512 nil -- the selected frame (actually the last non-minibuffer frame)
6513 A frame -- just that frame
6514 `visible' -- all visible frames
6515 0 -- all frames on the current terminal
6516 t -- all frames.
6518 If ALIST contains no `reusable-frames' entry, search just the
6519 selected frame if `display-buffer-reuse-frames' and
6520 `pop-up-frames' are both nil; search all frames on the current
6521 terminal if either of those variables is non-nil.
6523 If ALIST has a `previous-window' entry, the window specified by
6524 that entry will override any other window found by the methods
6525 above, even if that window never showed BUFFER before."
6526 (let* ((alist-entry (assq 'reusable-frames alist))
6527 (inhibit-same-window
6528 (cdr (assq 'inhibit-same-window alist)))
6529 (frames (cond
6530 (alist-entry (cdr alist-entry))
6531 ((if (eq pop-up-frames 'graphic-only)
6532 (display-graphic-p)
6533 pop-up-frames)
6535 (display-buffer-reuse-frames 0)
6536 (t (last-nonminibuffer-frame))))
6537 best-window second-best-window window)
6538 ;; Scan windows whether they have shown the buffer recently.
6539 (catch 'best
6540 (dolist (window (window-list-1 (frame-first-window) 'nomini frames))
6541 (when (and (assq buffer (window-prev-buffers window))
6542 (not (window-dedicated-p window)))
6543 (if (eq window (selected-window))
6544 (unless inhibit-same-window
6545 (setq second-best-window window))
6546 (setq best-window window)
6547 (throw 'best t)))))
6548 ;; When ALIST has a `previous-window' entry, that entry may override
6549 ;; anything we found so far.
6550 (when (and (setq window (cdr (assq 'previous-window alist)))
6551 (window-live-p window)
6552 (not (window-dedicated-p window)))
6553 (if (eq window (selected-window))
6554 (unless inhibit-same-window
6555 (setq second-best-window window))
6556 (setq best-window window)))
6557 ;; Return best or second best window found.
6558 (when (setq window (or best-window second-best-window))
6559 (window--display-buffer buffer window 'reuse alist))))
6561 (defun display-buffer-use-some-window (buffer alist)
6562 "Display BUFFER in an existing window.
6563 Search for a usable window, set that window to the buffer, and
6564 return the window. If no suitable window is found, return nil.
6566 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6567 event that a window in another frame is chosen, avoid raising
6568 that frame."
6569 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
6570 (frame (or (window--frame-usable-p (selected-frame))
6571 (window--frame-usable-p (last-nonminibuffer-frame))))
6572 (window
6573 ;; Reuse an existing window.
6574 (or (get-lru-window frame nil not-this-window)
6575 (let ((window (get-buffer-window buffer 'visible)))
6576 (unless (and not-this-window
6577 (eq window (selected-window)))
6578 window))
6579 (get-largest-window 'visible nil not-this-window)
6580 (let ((window (get-buffer-window buffer 0)))
6581 (unless (and not-this-window
6582 (eq window (selected-window)))
6583 window))
6584 (get-largest-window 0 nil not-this-window)))
6585 (quit-restore (and (window-live-p window)
6586 (window-parameter window 'quit-restore)))
6587 (quad (nth 1 quit-restore)))
6588 (when (window-live-p window)
6589 ;; If the window was used by `display-buffer' before, try to
6590 ;; resize it to its old height but don't signal an error.
6591 (when (and (listp quad)
6592 (integerp (nth 3 quad))
6593 (> (nth 3 quad) (window-total-height window)))
6594 (condition-case nil
6595 (window-resize window (- (nth 3 quad) (window-total-height window)))
6596 (error nil)))
6598 (prog1
6599 (window--display-buffer buffer window 'reuse alist)
6600 (window--even-window-heights window)
6601 (unless (cdr (assq 'inhibit-switch-frame alist))
6602 (window--maybe-raise-frame (window-frame window)))))))
6604 (defun display-buffer-no-window (_buffer alist)
6605 "Display BUFFER in no window.
6606 If ALIST has a non-nil `allow-no-window' entry, then don't display
6607 a window at all. This makes possible to override the default action
6608 and avoid displaying the buffer. It is assumed that when the caller
6609 specifies a non-nil `allow-no-window' then it can handle a nil value
6610 returned from `display-buffer' in this case."
6611 (when (cdr (assq 'allow-no-window alist))
6612 'fail))
6614 ;;; Display + selection commands:
6615 (defun pop-to-buffer (buffer &optional action norecord)
6616 "Select buffer BUFFER in some window, preferably a different one.
6617 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6618 is a string not naming an existent buffer, create a buffer with
6619 that name. If BUFFER is nil, choose some other buffer. Return
6620 the buffer.
6622 This uses `display-buffer' as a subroutine. The optional ACTION
6623 argument is passed to `display-buffer' as its ACTION argument.
6624 See `display-buffer' for more information. ACTION is t if called
6625 interactively with a prefix argument, which means to pop to a
6626 window other than the selected one even if the buffer is already
6627 displayed in the selected window.
6629 If the window to show BUFFER is not on the selected
6630 frame, raise that window's frame and give it input focus.
6632 Optional third arg NORECORD non-nil means do not put this buffer
6633 at the front of the list of recently selected ones."
6634 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
6635 (if current-prefix-arg t)))
6636 (setq buffer (window-normalize-buffer-to-switch-to buffer))
6637 ;; This should be done by `select-window' below.
6638 ;; (set-buffer buffer)
6639 (let* ((old-frame (selected-frame))
6640 (window (display-buffer buffer action))
6641 (frame (window-frame window)))
6642 ;; If we chose another frame, make sure it gets input focus.
6643 (unless (eq frame old-frame)
6644 (select-frame-set-input-focus frame norecord))
6645 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
6646 (select-window window norecord)
6647 buffer))
6649 (defun pop-to-buffer-same-window (buffer &optional norecord)
6650 "Select buffer BUFFER in some window, preferably the same one.
6651 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6652 is a string not naming an existent buffer, create a buffer with
6653 that name. If BUFFER is nil, choose some other buffer. Return
6654 the buffer.
6656 Optional argument NORECORD, if non-nil means do not put this
6657 buffer at the front of the list of recently selected ones.
6659 Unlike `pop-to-buffer', this function prefers using the selected
6660 window over popping up a new window or frame."
6661 (pop-to-buffer buffer display-buffer--same-window-action norecord))
6663 (defun read-buffer-to-switch (prompt)
6664 "Read the name of a buffer to switch to, prompting with PROMPT.
6665 Return the name of the buffer as a string.
6667 This function is intended for the `switch-to-buffer' family of
6668 commands since these need to omit the name of the current buffer
6669 from the list of completions and default values."
6670 (let ((rbts-completion-table (internal-complete-buffer-except)))
6671 (minibuffer-with-setup-hook
6672 (lambda ()
6673 (setq minibuffer-completion-table rbts-completion-table)
6674 ;; Since rbts-completion-table is built dynamically, we
6675 ;; can't just add it to the default value of
6676 ;; icomplete-with-completion-tables, so we add it
6677 ;; here manually.
6678 (if (and (boundp 'icomplete-with-completion-tables)
6679 (listp icomplete-with-completion-tables))
6680 (set (make-local-variable 'icomplete-with-completion-tables)
6681 (cons rbts-completion-table
6682 icomplete-with-completion-tables))))
6683 (read-buffer prompt (other-buffer (current-buffer))
6684 (confirm-nonexistent-file-or-buffer)))))
6686 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
6687 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
6688 If BUFFER-OR-NAME is nil, return the buffer returned by
6689 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
6690 exists, return that buffer. If no such buffer exists, create a
6691 buffer with the name BUFFER-OR-NAME and return that buffer."
6692 (if buffer-or-name
6693 (or (get-buffer buffer-or-name)
6694 (let ((buffer (get-buffer-create buffer-or-name)))
6695 (set-buffer-major-mode buffer)
6696 buffer))
6697 (other-buffer)))
6699 (defcustom switch-to-buffer-preserve-window-point nil
6700 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
6701 If this is nil, `switch-to-buffer' displays the buffer at that
6702 buffer's `point'. If this is `already-displayed', it tries to
6703 display the buffer at its previous position in the selected
6704 window, provided the buffer is currently displayed in some other
6705 window on any visible or iconified frame. If this is t, it
6706 unconditionally tries to display the buffer at its previous
6707 position in the selected window.
6709 This variable is ignored if the buffer is already displayed in
6710 the selected window or never appeared in it before, or if
6711 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
6712 :type '(choice
6713 (const :tag "Never" nil)
6714 (const :tag "If already displayed elsewhere" already-displayed)
6715 (const :tag "Always" t))
6716 :group 'windows
6717 :version "24.3")
6719 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
6720 "Display buffer BUFFER-OR-NAME in the selected window.
6722 WARNING: This is NOT the way to work on another buffer temporarily
6723 within a Lisp program! Use `set-buffer' instead. That avoids
6724 messing with the window-buffer correspondences.
6726 If the selected window cannot display the specified
6727 buffer (e.g. if it is a minibuffer window or strongly dedicated
6728 to another buffer), call `pop-to-buffer' to select the buffer in
6729 another window.
6731 If called interactively, read the buffer name using the
6732 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6733 determines whether to request confirmation before creating a new
6734 buffer.
6736 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
6737 If BUFFER-OR-NAME is a string that does not identify an existing
6738 buffer, create a buffer with that name. If BUFFER-OR-NAME is
6739 nil, switch to the buffer returned by `other-buffer'.
6741 If optional argument NORECORD is non-nil, do not put the buffer
6742 at the front of the buffer list, and do not make the window
6743 displaying it the most recently selected one.
6745 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
6746 must be displayed in the selected window; if that is impossible,
6747 signal an error rather than calling `pop-to-buffer'.
6749 The option `switch-to-buffer-preserve-window-point' can be used
6750 to make the buffer appear at its last position in the selected
6751 window.
6753 Return the buffer switched to."
6754 (interactive
6755 (list (read-buffer-to-switch "Switch to buffer: ") nil 'force-same-window))
6756 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
6757 (cond
6758 ;; Don't call set-window-buffer if it's not needed since it
6759 ;; might signal an error (e.g. if the window is dedicated).
6760 ((eq buffer (window-buffer)))
6761 ((window-minibuffer-p)
6762 (if force-same-window
6763 (user-error "Cannot switch buffers in minibuffer window")
6764 (pop-to-buffer buffer norecord)))
6765 ((eq (window-dedicated-p) t)
6766 (if force-same-window
6767 (user-error "Cannot switch buffers in a dedicated window")
6768 (pop-to-buffer buffer norecord)))
6770 (let* ((entry (assq buffer (window-prev-buffers)))
6771 (displayed (and (eq switch-to-buffer-preserve-window-point
6772 'already-displayed)
6773 (get-buffer-window buffer 0))))
6774 (set-window-buffer nil buffer)
6775 (when (and entry
6776 (or (eq switch-to-buffer-preserve-window-point t)
6777 displayed))
6778 ;; Try to restore start and point of buffer in the selected
6779 ;; window (Bug#4041).
6780 (set-window-start (selected-window) (nth 1 entry) t)
6781 (set-window-point nil (nth 2 entry))))))
6783 (unless norecord
6784 (select-window (selected-window)))
6785 (set-buffer buffer)))
6787 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
6788 "Select the buffer specified by BUFFER-OR-NAME in another window.
6789 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6790 nil. Return the buffer switched to.
6792 If called interactively, prompt for the buffer name using the
6793 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6794 determines whether to request confirmation before creating a new
6795 buffer.
6797 If BUFFER-OR-NAME is a string and does not identify an existing
6798 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6799 nil, switch to the buffer returned by `other-buffer'.
6801 Optional second argument NORECORD non-nil means do not put this
6802 buffer at the front of the list of recently selected ones.
6804 This uses the function `display-buffer' as a subroutine; see its
6805 documentation for additional customization information."
6806 (interactive
6807 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
6808 (let ((pop-up-windows t))
6809 (pop-to-buffer buffer-or-name t norecord)))
6811 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
6812 "Switch to buffer BUFFER-OR-NAME in another frame.
6813 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6814 nil. Return the buffer switched to.
6816 If called interactively, prompt for the buffer name using the
6817 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6818 determines whether to request confirmation before creating a new
6819 buffer.
6821 If BUFFER-OR-NAME is a string and does not identify an existing
6822 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6823 nil, switch to the buffer returned by `other-buffer'.
6825 Optional second arg NORECORD non-nil means do not put this
6826 buffer at the front of the list of recently selected ones.
6828 This uses the function `display-buffer' as a subroutine; see its
6829 documentation for additional customization information."
6830 (interactive
6831 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
6832 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
6834 (defun set-window-text-height (window height)
6835 "Set the height in lines of the text display area of WINDOW to HEIGHT.
6836 WINDOW must be a live window and defaults to the selected one.
6837 HEIGHT doesn't include the mode line or header line, if any, or
6838 any partial-height lines in the text display area.
6840 Note that the current implementation of this function cannot
6841 always set the height exactly, but attempts to be conservative,
6842 by allocating more lines than are actually needed in the case
6843 where some error may be present."
6844 (setq window (window-normalize-window window t))
6845 (let ((delta (- height (window-text-height window))))
6846 (unless (zerop delta)
6847 ;; Setting window-min-height to a value like 1 can lead to very
6848 ;; bizarre displays because it also allows Emacs to make *other*
6849 ;; windows one line tall, which means that there's no more space
6850 ;; for the mode line.
6851 (let ((window-min-height (min 2 height)))
6852 (window-resize window delta)))))
6854 (defun enlarge-window-horizontally (delta)
6855 "Make selected window DELTA columns wider.
6856 Interactively, if no argument is given, make selected window one
6857 column wider."
6858 (interactive "p")
6859 (enlarge-window delta t))
6861 (defun shrink-window-horizontally (delta)
6862 "Make selected window DELTA columns narrower.
6863 Interactively, if no argument is given, make selected window one
6864 column narrower."
6865 (interactive "p")
6866 (shrink-window delta t))
6868 (defun count-screen-lines (&optional beg end count-final-newline window)
6869 "Return the number of screen lines in the region.
6870 The number of screen lines may be different from the number of actual lines,
6871 due to line breaking, display table, etc.
6873 Optional arguments BEG and END default to `point-min' and `point-max'
6874 respectively.
6876 If region ends with a newline, ignore it unless optional third argument
6877 COUNT-FINAL-NEWLINE is non-nil.
6879 The optional fourth argument WINDOW specifies the window used for obtaining
6880 parameters such as width, horizontal scrolling, and so on. The default is
6881 to use the selected window's parameters.
6883 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6884 regardless of which buffer is displayed in WINDOW. This makes possible to use
6885 `count-screen-lines' in any buffer, whether or not it is currently displayed
6886 in some window."
6887 (unless beg
6888 (setq beg (point-min)))
6889 (unless end
6890 (setq end (point-max)))
6891 (if (= beg end)
6893 (save-excursion
6894 (save-restriction
6895 (widen)
6896 (narrow-to-region (min beg end)
6897 (if (and (not count-final-newline)
6898 (= ?\n (char-before (max beg end))))
6899 (1- (max beg end))
6900 (max beg end)))
6901 (goto-char (point-min))
6902 (1+ (vertical-motion (buffer-size) window))))))
6904 (defun window-buffer-height (window)
6905 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
6906 WINDOW must be a live window and defaults to the selected one."
6907 (setq window (window-normalize-window window t))
6908 (with-current-buffer (window-buffer window)
6909 (max 1
6910 (count-screen-lines (point-min) (point-max)
6911 ;; If buffer ends with a newline, ignore it when
6912 ;; counting height unless point is after it.
6913 (eobp)
6914 window))))
6916 ;;; Resizing windows and frames to fit their contents exactly.
6917 (defcustom fit-window-to-buffer-horizontally nil
6918 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
6919 If this is nil, `fit-window-to-buffer' never resizes windows
6920 horizontally. If this is `only', it can resize windows
6921 horizontally only. Any other value means `fit-window-to-buffer'
6922 can resize windows in both dimensions."
6923 :type 'boolean
6924 :version "24.4"
6925 :group 'help)
6927 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
6928 ;; counting title bar and outer borders.
6929 (defcustom fit-frame-to-buffer nil
6930 "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
6931 A frame is fit if and only if its root window is a live window
6932 and this option is non-nil. If this is `horizontally', frames
6933 are resized horizontally only. If this is `vertically', frames
6934 are resized vertically only. Any other non-nil value means
6935 frames can be resized in both dimensions."
6936 :type 'boolean
6937 :version "24.4"
6938 :group 'help)
6940 (defcustom fit-frame-to-buffer-margins '(nil nil nil nil)
6941 "Margins around frame for `fit-frame-to-buffer'.
6942 This specifies the numbers of pixels to be left free on the left,
6943 above, on the right, and below a frame fitted to its buffer. Set
6944 this to avoid obscuring other desktop objects like the taskbar.
6945 The default is nil for each side, which means to not add margins.
6947 The value specified here can be overridden for a specific frame
6948 by that frame's `fit-frame-to-buffer-margins' parameter, if
6949 present. See also `fit-frame-to-buffer-sizes'."
6950 :version "24.4"
6951 :type '(list
6952 (choice
6953 :tag "Left"
6954 :value nil
6955 :format "%[LeftMargin%] %v "
6956 (const :tag "None" :format "%t" nil)
6957 (integer :tag "Pixels" :size 5))
6958 (choice
6959 :tag "Top"
6960 :value nil
6961 :format "%[TopMargin%] %v "
6962 (const :tag "None" :format "%t" nil)
6963 (integer :tag "Pixels" :size 5))
6964 (choice
6965 :tag "Right"
6966 :value nil
6967 :format "%[RightMargin%] %v "
6968 (const :tag "None" :format "%t" nil)
6969 (integer :tag "Pixels" :size 5))
6970 (choice
6971 :tag "Bottom"
6972 :value nil
6973 :format "%[BottomMargin%] %v "
6974 (const :tag "None" :format "%t" nil)
6975 (integer :tag "Pixels" :size 5)))
6976 :group 'help)
6978 (defcustom fit-frame-to-buffer-sizes '(nil nil nil nil)
6979 "Size boundaries of frame for `fit-frame-to-buffer'.
6980 This list specifies the total maximum and minimum lines and
6981 maximum and minimum columns of the root window of any frame that
6982 shall be fit to its buffer. If any of these values is non-nil,
6983 it overrides the corresponding argument of `fit-frame-to-buffer'.
6985 On window systems where the menubar can wrap, fitting a frame to
6986 its buffer may swallow the last line(s). Specifying an
6987 appropriate minimum width value here can avoid such wrapping.
6989 See also `fit-frame-to-buffer-margins'."
6990 :version "24.4"
6991 :type '(list
6992 (choice
6993 :tag "Maximum Height"
6994 :value nil
6995 :format "%[MaxHeight%] %v "
6996 (const :tag "None" :format "%t" nil)
6997 (integer :tag "Lines" :size 5))
6998 (choice
6999 :tag "Minimum Height"
7000 :value nil
7001 :format "%[MinHeight%] %v "
7002 (const :tag "None" :format "%t" nil)
7003 (integer :tag "Lines" :size 5))
7004 (choice
7005 :tag "Maximum Width"
7006 :value nil
7007 :format "%[MaxWidth%] %v "
7008 (const :tag "None" :format "%t" nil)
7009 (integer :tag "Columns" :size 5))
7010 (choice
7011 :tag "Minimum Width"
7012 :value nil
7013 :format "%[MinWidth%] %v\n"
7014 (const :tag "None" :format "%t" nil)
7015 (integer :tag "Columns" :size 5)))
7016 :group 'help)
7018 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
7020 (defun window--sanitize-margin (margin left right)
7021 "Return MARGIN if it's a number between LEFT and RIGHT."
7022 (when (and (numberp margin)
7023 (<= left (- right margin)) (<= margin right))
7024 margin))
7026 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only)
7027 "Adjust size of FRAME to display the contents of its buffer exactly.
7028 FRAME can be any live frame and defaults to the selected one.
7029 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
7030 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
7031 FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
7032 `window-min-height' and `window-min-width' respectively.
7034 If the optional argument ONLY is `vertically', resize the frame
7035 vertically only. If ONLY is `horizontally', resize the frame
7036 horizontally only.
7038 The new position and size of FRAME can be additionally determined
7039 by customizing the options `fit-frame-to-buffer-sizes' and
7040 `fit-frame-to-buffer-margins' or the corresponding parameters of
7041 FRAME."
7042 (interactive)
7043 (unless (and (fboundp 'x-display-pixel-height)
7044 ;; We need the respective sizes now.
7045 (fboundp 'display-monitor-attributes-list))
7046 (user-error "Cannot resize frame in non-graphic Emacs"))
7047 (setq frame (window-normalize-frame frame))
7048 (when (window-live-p (frame-root-window frame))
7049 (with-selected-window (frame-root-window frame)
7050 (let* ((window (frame-root-window frame))
7051 (char-width (frame-char-width))
7052 (char-height (frame-char-height))
7053 (monitor-attributes (car (display-monitor-attributes-list
7054 (frame-parameter frame 'display))))
7055 (geometry (cdr (assq 'geometry monitor-attributes)))
7056 (display-width (- (nth 2 geometry) (nth 0 geometry)))
7057 (display-height (- (nth 3 geometry) (nth 1 geometry)))
7058 (workarea (cdr (assq 'workarea monitor-attributes)))
7059 ;; Handle margins.
7060 (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins)
7061 fit-frame-to-buffer-margins))
7062 (left-margin (if (nth 0 margins)
7063 (or (window--sanitize-margin
7064 (nth 0 margins) 0 display-width)
7066 (nth 0 workarea)))
7067 (top-margin (if (nth 1 margins)
7068 (or (window--sanitize-margin
7069 (nth 1 margins) 0 display-height)
7071 (nth 1 workarea)))
7072 (workarea-width (nth 2 workarea))
7073 (right-margin (if (nth 2 margins)
7074 (- display-width
7075 (or (window--sanitize-margin
7076 (nth 2 margins) left-margin display-width)
7078 (nth 2 workarea)))
7079 (workarea-height (nth 3 workarea))
7080 (bottom-margin (if (nth 3 margins)
7081 (- display-height
7082 (or (window--sanitize-margin
7083 (nth 3 margins) top-margin display-height)
7085 (nth 3 workarea)))
7086 ;; The pixel width of FRAME (which does not include the
7087 ;; window manager's decorations).
7088 (frame-width (frame-pixel-width))
7089 ;; The pixel width of the body of FRAME's root window.
7090 (window-body-width (window-body-width nil t))
7091 ;; The difference in pixels between total and body width of
7092 ;; FRAME's window.
7093 (window-extra-width (- (window-pixel-width) window-body-width))
7094 ;; The difference in pixels between the frame's pixel width
7095 ;; and the window's body width. This is the space we can't
7096 ;; use for fitting.
7097 (extra-width (- frame-width window-body-width))
7098 ;; The maximum width we can use for fitting.
7099 (fit-width (- workarea-width extra-width))
7100 ;; The pixel position of FRAME's left border. We usually
7101 ;; try to leave this alone.
7102 (left
7103 (let ((left (frame-parameter nil 'left)))
7104 (if (consp left)
7105 (funcall (car left) (cadr left))
7106 left)))
7107 ;; The pixel height of FRAME (which does not include title
7108 ;; line, decorations, and sometimes neither the menu nor
7109 ;; the toolbar).
7110 (frame-height (frame-pixel-height))
7111 ;; The pixel height of FRAME's root window (we don't care
7112 ;; about the window's body height since the return value of
7113 ;; `window-text-pixel-size' includes header and mode line).
7114 (window-height (window-pixel-height))
7115 ;; The difference in pixels between the frame's pixel
7116 ;; height and the window's height.
7117 (extra-height (- frame-height window-height))
7118 ;; When tool-bar-mode is enabled and we just created a new
7119 ;; frame, reserve lines for toolbar resizing. Needed
7120 ;; because for reasons unknown to me Emacs (1) reserves one
7121 ;; line for the toolbar when making the initial frame and
7122 ;; toolbars are enabled, and (2) later adds the remaining
7123 ;; lines needed. Our code runs IN BETWEEN (1) and (2).
7124 ;; YMMV when you're on a system that behaves differently.
7125 (toolbar-extra-height
7126 (let ((quit-restore (window-parameter window 'quit-restore))
7127 ;; This may have to change when we allow arbitrary
7128 ;; pixel height toolbars.
7129 (lines (tool-bar-height)))
7130 (* char-height
7131 (if (and quit-restore (eq (car quit-restore) 'frame)
7132 (not (zerop lines)))
7133 (1- lines)
7134 0))))
7135 ;; The pixel position of FRAME's top border.
7136 (top
7137 (let ((top (frame-parameter nil 'top)))
7138 (if (consp top)
7139 (funcall (car top) (cadr top))
7140 top)))
7141 ;; Sanitize minimum and maximum sizes.
7142 (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes)
7143 fit-frame-to-buffer-sizes))
7144 (max-height
7145 (cond
7146 ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height))
7147 ((numberp max-height) (* max-height char-height))
7148 (t display-height)))
7149 (min-height
7150 (cond
7151 ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height))
7152 ((numberp min-height) (* min-height char-height))
7153 (t (* window-min-height char-height))))
7154 (max-width
7155 (cond
7156 ((numberp (nth 2 sizes))
7157 (- (* (nth 2 sizes) char-width) window-extra-width))
7158 ((numberp max-width)
7159 (- (* max-width char-width) window-extra-width))
7160 (t display-width)))
7161 (min-width
7162 (cond
7163 ((numberp (nth 3 sizes))
7164 (- (* (nth 3 sizes) char-width) window-extra-width))
7165 ((numberp min-width)
7166 (- (* min-width char-width) window-extra-width))
7167 (t (* window-min-width char-width))))
7168 ;; Note: Currently, for a new frame the sizes of the header
7169 ;; and mode line may be estimated incorrectly
7170 (value (window-text-pixel-size
7171 nil t t workarea-width workarea-height t))
7172 (width (+ (car value) (window-right-divider-width)))
7173 (height
7174 (+ (cdr value)
7175 (window-bottom-divider-width)
7176 (window-scroll-bar-height))))
7177 ;; Don't change height or width when the window's size is fixed
7178 ;; in either direction or ONLY forbids it.
7179 (cond
7180 ((or (eq window-size-fixed 'width) (eq only 'vertically))
7181 (setq width nil))
7182 ((or (eq window-size-fixed 'height) (eq only 'horizontally))
7183 (setq height nil)))
7184 ;; Fit width to constraints.
7185 (when width
7186 (unless frame-resize-pixelwise
7187 ;; Round to character sizes.
7188 (setq width (* (/ (+ width char-width -1) char-width)
7189 char-width)))
7190 ;; Fit to maximum and minimum widths.
7191 (setq width (max (min width max-width) min-width))
7192 ;; Add extra width.
7193 (setq width (+ width extra-width))
7194 ;; Preserve margins.
7195 (let ((right (+ left width)))
7196 (cond
7197 ((> right right-margin)
7198 ;; Move frame to left (we don't know its real width).
7199 (setq left (max left-margin (- left (- right right-margin)))))
7200 ((< left left-margin)
7201 ;; Move frame to right.
7202 (setq left left-margin)))))
7203 ;; Fit height to constraints.
7204 (when height
7205 (unless frame-resize-pixelwise
7206 (setq height (* (/ (+ height char-height -1) char-height)
7207 char-height)))
7208 ;; Fit to maximum and minimum heights.
7209 (setq height (max (min height max-height) min-height))
7210 ;; Add extra height.
7211 (setq height (+ height extra-height))
7212 ;; Preserve margins.
7213 (let ((bottom (+ top height)))
7214 (cond
7215 ((> bottom bottom-margin)
7216 ;; Move frame up (we don't know its real height).
7217 (setq top (max top-margin (- top (- bottom bottom-margin)))))
7218 ((< top top-margin)
7219 ;; Move frame down.
7220 (setq top top-margin)))))
7221 ;; Apply changes.
7222 (set-frame-position frame left top)
7223 ;; Clumsily try to translate our calculations to what
7224 ;; `set-frame-size' wants.
7225 (when width
7226 (setq width (- (+ (frame-text-width) width)
7227 extra-width window-body-width)))
7228 (when height
7229 (setq height (- (+ (frame-text-height) height)
7230 extra-height window-height)))
7231 (set-frame-size
7232 frame
7233 (if width
7234 (if frame-resize-pixelwise
7235 width
7236 (/ width char-width))
7237 (frame-text-width))
7238 (if height
7239 (if frame-resize-pixelwise
7240 height
7241 (/ height char-height))
7242 (frame-text-height))
7243 frame-resize-pixelwise)))))
7245 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width)
7246 "Adjust size of WINDOW to display its buffer's contents exactly.
7247 WINDOW must be a live window and defaults to the selected one.
7249 If WINDOW is part of a vertical combination, adjust WINDOW's
7250 height. The new height is calculated from the actual height of
7251 the accessible portion of its buffer. The optional argument
7252 MAX-HEIGHT specifies a maximum height and defaults to the height
7253 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
7254 minimum height and defaults to `window-min-height'. Both
7255 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
7256 and header line and a bottom divider, if any.
7258 If WINDOW is part of a horizontal combination and the value of
7259 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
7260 WINDOW's width. The new width of WINDOW is calculated from the
7261 maximum length of its buffer's lines that follow the current
7262 start position of WINDOW. The optional argument MAX-WIDTH
7263 specifies a maximum width and defaults to the width of WINDOW's
7264 frame. The optional argument MIN-WIDTH specifies a minimum width
7265 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
7266 are specified in columns and include fringes, margins, a
7267 scrollbar and a vertical divider, if any.
7269 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
7270 If WINDOW is its frame's root window and the option
7271 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
7272 adjust the frame's size.
7274 Note that even if this function makes WINDOW large enough to show
7275 _all_ parts of its buffer you might not see the first part when
7276 WINDOW was scrolled. If WINDOW is resized horizontally, you will
7277 not see the top of its buffer unless WINDOW starts at its minimum
7278 accessible position."
7279 (interactive)
7280 (setq window (window-normalize-window window t))
7281 (if (eq window (frame-root-window window))
7282 (when fit-frame-to-buffer
7283 ;; Fit WINDOW's frame to buffer.
7284 (fit-frame-to-buffer
7285 (window-frame window)
7286 max-height min-height max-width min-width
7287 (and (memq fit-frame-to-buffer '(vertically horizontally))
7288 fit-frame-to-buffer)))
7289 (with-selected-window window
7290 (let* ((pixelwise window-resize-pixelwise)
7291 (char-height (frame-char-height))
7292 (char-width (frame-char-width))
7293 (total-height (window-size window nil pixelwise))
7294 (body-height (window-body-height window pixelwise))
7295 (body-width (window-body-width window pixelwise))
7296 (min-height
7297 ;; Sanitize MIN-HEIGHT.
7298 (if (numberp min-height)
7299 ;; Can't get smaller than `window-safe-min-height'.
7300 (max (if pixelwise
7301 (* char-height min-height)
7302 min-height)
7303 (if pixelwise
7304 (window-safe-min-pixel-height window)
7305 window-safe-min-height))
7306 ;; Preserve header and mode line if present.
7307 (max (if pixelwise
7308 (* char-height window-min-height)
7309 window-min-height)
7310 (window-min-size nil nil t pixelwise))))
7311 (max-height
7312 ;; Sanitize MAX-HEIGHT.
7313 (if (numberp max-height)
7314 (min
7315 (+ total-height
7316 (window-max-delta
7317 window nil nil nil nil nil pixelwise))
7318 (if pixelwise
7319 (* char-height max-height)
7320 max-height))
7321 (+ total-height (window-max-delta
7322 window nil nil nil nil nil pixelwise))))
7323 height)
7324 (cond
7325 ;; If WINDOW is vertically combined, try to resize it
7326 ;; vertically.
7327 ((and (not (eq fit-window-to-buffer-horizontally 'only))
7328 (not (window-size-fixed-p window))
7329 (window-combined-p))
7330 ;; Vertically we always want to fit the entire buffer.
7331 ;; WINDOW'S height can't get larger than its frame's pixel
7332 ;; height. Its width remains fixed.
7333 (setq height (+ (cdr (window-text-pixel-size
7334 nil nil t nil (frame-pixel-height) t))
7335 (window-scroll-bar-height window)
7336 (window-bottom-divider-width)))
7337 ;; Round height.
7338 (unless pixelwise
7339 (setq height (/ (+ height char-height -1) char-height)))
7340 (unless (= height total-height)
7341 (window-resize-no-error
7342 window
7343 (- (max min-height (min max-height height)) total-height)
7344 nil window pixelwise)))
7345 ;; If WINDOW is horizontally combined, try to resize it
7346 ;; horizontally.
7347 ((and fit-window-to-buffer-horizontally
7348 (not (window-size-fixed-p window t))
7349 (window-combined-p nil t))
7350 (let* ((total-width (window-size window t pixelwise))
7351 (min-width
7352 ;; Sanitize MIN-WIDTH.
7353 (if (numberp min-width)
7354 ;; Can't get smaller than `window-safe-min-width'.
7355 (max (if pixelwise
7356 (* char-width min-width)
7357 min-width)
7358 (if pixelwise
7359 (window-safe-min-pixel-width)
7360 window-safe-min-width))
7361 ;; Preserve fringes, margins, scrollbars if present.
7362 (max (if pixelwise
7363 (* char-width window-min-width)
7364 window-min-width)
7365 (window-min-size nil nil t pixelwise))))
7366 (max-width
7367 ;; Sanitize MAX-WIDTH.
7368 (if (numberp max-width)
7369 (min (+ total-width
7370 (window-max-delta
7371 nil t nil nil nil nil pixelwise))
7372 (if pixelwise
7373 (* char-width max-width)
7374 max-width))
7375 (+ total-width (window-max-delta
7376 nil t nil nil nil nil pixelwise))))
7377 ;; When fitting horizontally, assume that WINDOW's
7378 ;; start position remains unaltered. WINDOW can't get
7379 ;; wider than its frame's pixel width, its height
7380 ;; remains unaltered.
7381 (width (+ (car (window-text-pixel-size
7382 nil (window-start) (point-max)
7383 (frame-pixel-width)
7384 ;; Add one char-height to assure that
7385 ;; we're on the safe side. This
7386 ;; overshoots when the first line below
7387 ;; the bottom is wider than the window.
7388 (* body-height
7389 (if pixelwise 1 char-height))))
7390 (window-right-divider-width))))
7391 (unless pixelwise
7392 (setq width (/ (+ width char-width -1) char-width)))
7393 (unless (= width body-width)
7394 (window-resize-no-error
7395 window
7396 (- (max min-width
7397 (min max-width
7398 (+ total-width (- width body-width))))
7399 total-width)
7400 t window pixelwise)))))))))
7402 (defun window-safely-shrinkable-p (&optional window)
7403 "Return t if WINDOW can be shrunk without shrinking other windows.
7404 WINDOW defaults to the selected window."
7405 (with-selected-window (or window (selected-window))
7406 (let ((edges (window-edges)))
7407 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
7408 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
7410 (defun shrink-window-if-larger-than-buffer (&optional window)
7411 "Shrink height of WINDOW if its buffer doesn't need so many lines.
7412 More precisely, shrink WINDOW vertically to be as small as
7413 possible, while still showing the full contents of its buffer.
7414 WINDOW must be a live window and defaults to the selected one.
7416 Do not shrink WINDOW to less than `window-min-height' lines. Do
7417 nothing if the buffer contains more lines than the present window
7418 height, or if some of the window's contents are scrolled out of
7419 view, or if shrinking this window would also shrink another
7420 window, or if the window is the only window of its frame.
7422 Return non-nil if the window was shrunk, nil otherwise."
7423 (interactive)
7424 (setq window (window-normalize-window window t))
7425 ;; Make sure that WINDOW is vertically combined and `point-min' is
7426 ;; visible (for whatever reason that's needed). The remaining issues
7427 ;; should be taken care of by `fit-window-to-buffer'.
7428 (when (and (window-combined-p window)
7429 (pos-visible-in-window-p (point-min) window))
7430 (fit-window-to-buffer window (window-total-height window))))
7432 (defun kill-buffer-and-window ()
7433 "Kill the current buffer and delete the selected window."
7434 (interactive)
7435 (let ((window-to-delete (selected-window))
7436 (buffer-to-kill (current-buffer))
7437 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
7438 (unwind-protect
7439 (progn
7440 (add-hook 'kill-buffer-hook delete-window-hook t t)
7441 (if (kill-buffer (current-buffer))
7442 ;; If `delete-window' failed before, we rerun it to regenerate
7443 ;; the error so it can be seen in the echo area.
7444 (when (eq (selected-window) window-to-delete)
7445 (delete-window))))
7446 ;; If the buffer is not dead for some reason (probably because
7447 ;; of a `quit' signal), remove the hook again.
7448 (ignore-errors
7449 (with-current-buffer buffer-to-kill
7450 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
7453 (defvar recenter-last-op nil
7454 "Indicates the last recenter operation performed.
7455 Possible values: `top', `middle', `bottom', integer or float numbers.")
7457 (defcustom recenter-positions '(middle top bottom)
7458 "Cycling order for `recenter-top-bottom'.
7459 A list of elements with possible values `top', `middle', `bottom',
7460 integer or float numbers that define the cycling order for
7461 the command `recenter-top-bottom'.
7463 Top and bottom destinations are `scroll-margin' lines from the true
7464 window top and bottom. Middle redraws the frame and centers point
7465 vertically within the window. Integer number moves current line to
7466 the specified absolute window-line. Float number between 0.0 and 1.0
7467 means the percentage of the screen space from the top. The default
7468 cycling order is middle -> top -> bottom."
7469 :type '(repeat (choice
7470 (const :tag "Top" top)
7471 (const :tag "Middle" middle)
7472 (const :tag "Bottom" bottom)
7473 (integer :tag "Line number")
7474 (float :tag "Percentage")))
7475 :version "23.2"
7476 :group 'windows)
7478 (defun recenter-top-bottom (&optional arg)
7479 "Move current buffer line to the specified window line.
7480 With no prefix argument, successive calls place point according
7481 to the cycling order defined by `recenter-positions'.
7483 A prefix argument is handled like `recenter':
7484 With numeric prefix ARG, move current line to window-line ARG.
7485 With plain `C-u', move current line to window center."
7486 (interactive "P")
7487 (cond
7488 (arg (recenter arg)) ; Always respect ARG.
7490 (setq recenter-last-op
7491 (if (eq this-command last-command)
7492 (car (or (cdr (member recenter-last-op recenter-positions))
7493 recenter-positions))
7494 (car recenter-positions)))
7495 (let ((this-scroll-margin
7496 (min (max 0 scroll-margin)
7497 (truncate (/ (window-body-height) 4.0)))))
7498 (cond ((eq recenter-last-op 'middle)
7499 (recenter))
7500 ((eq recenter-last-op 'top)
7501 (recenter this-scroll-margin))
7502 ((eq recenter-last-op 'bottom)
7503 (recenter (- -1 this-scroll-margin)))
7504 ((integerp recenter-last-op)
7505 (recenter recenter-last-op))
7506 ((floatp recenter-last-op)
7507 (recenter (round (* recenter-last-op (window-height))))))))))
7509 (define-key global-map [?\C-l] 'recenter-top-bottom)
7511 (defun move-to-window-line-top-bottom (&optional arg)
7512 "Position point relative to window.
7514 With a prefix argument ARG, acts like `move-to-window-line'.
7516 With no argument, positions point at center of window.
7517 Successive calls position point at positions defined
7518 by `recenter-positions'."
7519 (interactive "P")
7520 (cond
7521 (arg (move-to-window-line arg)) ; Always respect ARG.
7523 (setq recenter-last-op
7524 (if (eq this-command last-command)
7525 (car (or (cdr (member recenter-last-op recenter-positions))
7526 recenter-positions))
7527 (car recenter-positions)))
7528 (let ((this-scroll-margin
7529 (min (max 0 scroll-margin)
7530 (truncate (/ (window-body-height) 4.0)))))
7531 (cond ((eq recenter-last-op 'middle)
7532 (call-interactively 'move-to-window-line))
7533 ((eq recenter-last-op 'top)
7534 (move-to-window-line this-scroll-margin))
7535 ((eq recenter-last-op 'bottom)
7536 (move-to-window-line (- -1 this-scroll-margin)))
7537 ((integerp recenter-last-op)
7538 (move-to-window-line recenter-last-op))
7539 ((floatp recenter-last-op)
7540 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
7542 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
7544 ;;; Scrolling commands.
7546 ;;; Scrolling commands which do not signal errors at top/bottom
7547 ;;; of buffer at first key-press (instead move to top/bottom
7548 ;;; of buffer).
7550 (defcustom scroll-error-top-bottom nil
7551 "Move point to top/bottom of buffer before signaling a scrolling error.
7552 A value of nil means just signal an error if no more scrolling possible.
7553 A value of t means point moves to the beginning or the end of the buffer
7554 \(depending on scrolling direction) when no more scrolling possible.
7555 When point is already on that position, then signal an error."
7556 :type 'boolean
7557 :group 'windows
7558 :version "24.1")
7560 (defun scroll-up-command (&optional arg)
7561 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
7562 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
7563 scroll window further, move cursor to the bottom line.
7564 When point is already on that position, then signal an error.
7565 A near full screen is `next-screen-context-lines' less than a full screen.
7566 Negative ARG means scroll downward.
7567 If ARG is the atom `-', scroll downward by nearly full screen."
7568 (interactive "^P")
7569 (cond
7570 ((null scroll-error-top-bottom)
7571 (scroll-up arg))
7572 ((eq arg '-)
7573 (scroll-down-command nil))
7574 ((< (prefix-numeric-value arg) 0)
7575 (scroll-down-command (- (prefix-numeric-value arg))))
7576 ((eobp)
7577 (scroll-up arg)) ; signal error
7579 (condition-case nil
7580 (scroll-up arg)
7581 (end-of-buffer
7582 (if arg
7583 ;; When scrolling by ARG lines can't be done,
7584 ;; move by ARG lines instead.
7585 (forward-line arg)
7586 ;; When ARG is nil for full-screen scrolling,
7587 ;; move to the bottom of the buffer.
7588 (goto-char (point-max))))))))
7590 (put 'scroll-up-command 'scroll-command t)
7592 (defun scroll-down-command (&optional arg)
7593 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
7594 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
7595 scroll window further, move cursor to the top line.
7596 When point is already on that position, then signal an error.
7597 A near full screen is `next-screen-context-lines' less than a full screen.
7598 Negative ARG means scroll upward.
7599 If ARG is the atom `-', scroll upward by nearly full screen."
7600 (interactive "^P")
7601 (cond
7602 ((null scroll-error-top-bottom)
7603 (scroll-down arg))
7604 ((eq arg '-)
7605 (scroll-up-command nil))
7606 ((< (prefix-numeric-value arg) 0)
7607 (scroll-up-command (- (prefix-numeric-value arg))))
7608 ((bobp)
7609 (scroll-down arg)) ; signal error
7611 (condition-case nil
7612 (scroll-down arg)
7613 (beginning-of-buffer
7614 (if arg
7615 ;; When scrolling by ARG lines can't be done,
7616 ;; move by ARG lines instead.
7617 (forward-line (- arg))
7618 ;; When ARG is nil for full-screen scrolling,
7619 ;; move to the top of the buffer.
7620 (goto-char (point-min))))))))
7622 (put 'scroll-down-command 'scroll-command t)
7624 ;;; Scrolling commands which scroll a line instead of full screen.
7626 (defun scroll-up-line (&optional arg)
7627 "Scroll text of selected window upward ARG lines; or one line if no ARG.
7628 If ARG is omitted or nil, scroll upward by one line.
7629 This is different from `scroll-up-command' that scrolls a full screen."
7630 (interactive "p")
7631 (scroll-up (or arg 1)))
7633 (put 'scroll-up-line 'scroll-command t)
7635 (defun scroll-down-line (&optional arg)
7636 "Scroll text of selected window down ARG lines; or one line if no ARG.
7637 If ARG is omitted or nil, scroll down by one line.
7638 This is different from `scroll-down-command' that scrolls a full screen."
7639 (interactive "p")
7640 (scroll-down (or arg 1)))
7642 (put 'scroll-down-line 'scroll-command t)
7645 (defun scroll-other-window-down (&optional lines)
7646 "Scroll the \"other window\" down.
7647 For more details, see the documentation for `scroll-other-window'."
7648 (interactive "P")
7649 (scroll-other-window
7650 ;; Just invert the argument's meaning.
7651 ;; We can do that without knowing which window it will be.
7652 (if (eq lines '-) nil
7653 (if (null lines) '-
7654 (- (prefix-numeric-value lines))))))
7656 (defun beginning-of-buffer-other-window (arg)
7657 "Move point to the beginning of the buffer in the other window.
7658 Leave mark at previous position.
7659 With arg N, put point N/10 of the way from the true beginning."
7660 (interactive "P")
7661 (let ((orig-window (selected-window))
7662 (window (other-window-for-scrolling)))
7663 ;; We use unwind-protect rather than save-window-excursion
7664 ;; because the latter would preserve the things we want to change.
7665 (unwind-protect
7666 (progn
7667 (select-window window)
7668 ;; Set point and mark in that window's buffer.
7669 (with-no-warnings
7670 (beginning-of-buffer arg))
7671 ;; Set point accordingly.
7672 (recenter '(t)))
7673 (select-window orig-window))))
7675 (defun end-of-buffer-other-window (arg)
7676 "Move point to the end of the buffer in the other window.
7677 Leave mark at previous position.
7678 With arg N, put point N/10 of the way from the true end."
7679 (interactive "P")
7680 ;; See beginning-of-buffer-other-window for comments.
7681 (let ((orig-window (selected-window))
7682 (window (other-window-for-scrolling)))
7683 (unwind-protect
7684 (progn
7685 (select-window window)
7686 (with-no-warnings
7687 (end-of-buffer arg))
7688 (recenter '(t)))
7689 (select-window orig-window))))
7691 (defvar mouse-autoselect-window-timer nil
7692 "Timer used by delayed window autoselection.")
7694 (defvar mouse-autoselect-window-position nil
7695 "Last mouse position recorded by delayed window autoselection.")
7697 (defvar mouse-autoselect-window-window nil
7698 "Last window recorded by delayed window autoselection.")
7700 (defvar mouse-autoselect-window-state nil
7701 "When non-nil, special state of delayed window autoselection.
7702 Possible values are `suspend' (suspend autoselection after a menu or
7703 scrollbar interaction) and `select' (the next invocation of
7704 `handle-select-window' shall select the window immediately).")
7706 (defun mouse-autoselect-window-cancel (&optional force)
7707 "Cancel delayed window autoselection.
7708 Optional argument FORCE means cancel unconditionally."
7709 (unless (and (not force)
7710 ;; Don't cancel for select-window or select-frame events
7711 ;; or when the user drags a scroll bar.
7712 (or (memq this-command
7713 '(handle-select-window handle-switch-frame))
7714 (and (eq this-command 'scroll-bar-toolkit-scroll)
7715 (memq (nth 4 (event-end last-input-event))
7716 '(handle end-scroll)))))
7717 (setq mouse-autoselect-window-state nil)
7718 (when (timerp mouse-autoselect-window-timer)
7719 (cancel-timer mouse-autoselect-window-timer))
7720 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
7722 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
7723 "Start delayed window autoselection.
7724 MOUSE-POSITION is the last position where the mouse was seen as returned
7725 by `mouse-position'. Optional argument WINDOW non-nil denotes the
7726 window where the mouse was seen. Optional argument SUSPEND non-nil
7727 means suspend autoselection."
7728 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
7729 (setq mouse-autoselect-window-position mouse-position)
7730 (when window (setq mouse-autoselect-window-window window))
7731 (setq mouse-autoselect-window-state (when suspend 'suspend))
7732 ;; Install timer which runs `mouse-autoselect-window-select' after
7733 ;; `mouse-autoselect-window' seconds.
7734 (setq mouse-autoselect-window-timer
7735 (run-at-time
7736 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
7738 (defun mouse-autoselect-window-select ()
7739 "Select window with delayed window autoselection.
7740 If the mouse position has stabilized in a non-selected window, select
7741 that window. The minibuffer window is selected only if the minibuffer
7742 is active. This function is run by `mouse-autoselect-window-timer'."
7743 (ignore-errors
7744 (let* ((mouse-position (mouse-position))
7745 (window
7746 (ignore-errors
7747 (window-at (cadr mouse-position) (cddr mouse-position)
7748 (car mouse-position)))))
7749 (cond
7750 ((or (and (fboundp 'menu-or-popup-active-p) (menu-or-popup-active-p))
7751 (and window
7752 (let ((coords (coordinates-in-window-p
7753 (cdr mouse-position) window)))
7754 (and (not (consp coords))
7755 (not (memq coords '(left-margin right-margin)))))))
7756 ;; A menu / popup dialog is active or the mouse is not on the
7757 ;; text region of WINDOW: Suspend autoselection temporarily.
7758 (mouse-autoselect-window-start mouse-position nil t))
7759 ((eq mouse-autoselect-window-state 'suspend)
7760 ;; Delayed autoselection was temporarily suspended, reenable it.
7761 (mouse-autoselect-window-start mouse-position))
7762 ((and window (not (eq window (selected-window)))
7763 (or (not (numberp mouse-autoselect-window))
7764 (and (> mouse-autoselect-window 0)
7765 ;; If `mouse-autoselect-window' is positive, select
7766 ;; window if the window is the same as before.
7767 (eq window mouse-autoselect-window-window))
7768 ;; Otherwise select window if the mouse is at the same
7769 ;; position as before. Observe that the first test after
7770 ;; starting autoselection usually fails since the value of
7771 ;; `mouse-autoselect-window-position' recorded there is the
7772 ;; position where the mouse has entered the new window and
7773 ;; not necessarily where the mouse has stopped moving.
7774 (equal mouse-position mouse-autoselect-window-position))
7775 ;; The minibuffer is a candidate window if it's active.
7776 (or (not (window-minibuffer-p window))
7777 (eq window (active-minibuffer-window))))
7778 ;; Mouse position has stabilized in non-selected window: Cancel
7779 ;; delayed autoselection and try to select that window.
7780 (mouse-autoselect-window-cancel t)
7781 ;; Select window where mouse appears unless the selected window is the
7782 ;; minibuffer. Use `unread-command-events' in order to execute pre-
7783 ;; and post-command hooks and trigger idle timers. To avoid delaying
7784 ;; autoselection again, set `mouse-autoselect-window-state'."
7785 (unless (window-minibuffer-p)
7786 (setq mouse-autoselect-window-state 'select)
7787 (setq unread-command-events
7788 (cons (list 'select-window (list window))
7789 unread-command-events))))
7790 ((or (and window (eq window (selected-window)))
7791 (not (numberp mouse-autoselect-window))
7792 (equal mouse-position mouse-autoselect-window-position))
7793 ;; Mouse position has either stabilized in the selected window or at
7794 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
7795 (mouse-autoselect-window-cancel t))
7797 ;; Mouse position has not stabilized yet, resume delayed
7798 ;; autoselection.
7799 (mouse-autoselect-window-start mouse-position window))))))
7801 (defun handle-select-window (event)
7802 "Handle select-window events."
7803 (interactive "e")
7804 (let ((window (posn-window (event-start event))))
7805 (unless (or (not (window-live-p window))
7806 ;; Don't switch if we're currently in the minibuffer.
7807 ;; This tries to work around problems where the
7808 ;; minibuffer gets unselected unexpectedly, and where
7809 ;; you then have to move your mouse all the way down to
7810 ;; the minibuffer to select it.
7811 (window-minibuffer-p)
7812 ;; Don't switch to minibuffer window unless it's active.
7813 (and (window-minibuffer-p window)
7814 (not (minibuffer-window-active-p window)))
7815 ;; Don't switch when autoselection shall be delayed.
7816 (and (numberp mouse-autoselect-window)
7817 (not (zerop mouse-autoselect-window))
7818 (not (eq mouse-autoselect-window-state 'select))
7819 (progn
7820 ;; Cancel any delayed autoselection.
7821 (mouse-autoselect-window-cancel t)
7822 ;; Start delayed autoselection from current mouse
7823 ;; position and window.
7824 (mouse-autoselect-window-start (mouse-position) window)
7825 ;; Executing a command cancels delayed autoselection.
7826 (add-hook
7827 'pre-command-hook 'mouse-autoselect-window-cancel))))
7828 (when mouse-autoselect-window
7829 ;; Reset state of delayed autoselection.
7830 (setq mouse-autoselect-window-state nil)
7831 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
7832 (run-hooks 'mouse-leave-buffer-hook))
7833 ;; Clear echo area.
7834 (message nil)
7835 (select-window window))))
7837 (defun truncated-partial-width-window-p (&optional window)
7838 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
7839 WINDOW must be a live window and defaults to the selected one.
7840 Return nil if WINDOW is not a partial-width window
7841 (regardless of the value of `truncate-lines').
7842 Otherwise, consult the value of `truncate-partial-width-windows'
7843 for the buffer shown in WINDOW."
7844 (setq window (window-normalize-window window t))
7845 (unless (window-full-width-p window)
7846 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
7847 (window-buffer window))))
7848 (if (integerp t-p-w-w)
7849 (< (window-width window) t-p-w-w)
7850 t-p-w-w))))
7852 ;; Some of these are in tutorial--default-keys, so update that if you
7853 ;; change these.
7854 (define-key ctl-x-map "0" 'delete-window)
7855 (define-key ctl-x-map "1" 'delete-other-windows)
7856 (define-key ctl-x-map "2" 'split-window-below)
7857 (define-key ctl-x-map "3" 'split-window-right)
7858 (define-key ctl-x-map "o" 'other-window)
7859 (define-key ctl-x-map "^" 'enlarge-window)
7860 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
7861 (define-key ctl-x-map "{" 'shrink-window-horizontally)
7862 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
7863 (define-key ctl-x-map "+" 'balance-windows)
7864 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
7866 ;;; window.el ends here