Make ‘text-quoting-style’ a plain defvar
[emacs.git] / lisp / window.el
blob65b4ef08268e281ee98711df0a1c1e536e5332e9
1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2015 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 (vaction action)
190 (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 (vaction action)
213 (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 (vaction action)
235 (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
241 ,buffer
242 ;; Remove window-height when it's handled below.
243 (if (functionp (cdr (assq 'window-height (cdr ,vaction))))
244 (assq-delete-all 'window-height (copy-sequence ,vaction))
245 ,vaction))))
247 (let ((inhibit-read-only t)
248 (inhibit-modification-hooks t))
249 (setq ,value (progn ,@body)))
251 (set-window-point ,window (point-min))
253 (when (functionp (cdr (assq 'window-height (cdr ,vaction))))
254 (ignore-errors
255 (funcall (cdr (assq 'window-height (cdr ,vaction))) ,window)))
257 (when (consp (cdr (assq 'preserve-size (cdr ,vaction))))
258 (window-preserve-size
259 ,window t (cadr (assq 'preserve-size (cdr ,vaction))))
260 (window-preserve-size
261 ,window nil (cddr (assq 'preserve-size (cdr ,vaction)))))
263 (if (functionp ,vquit-function)
264 (funcall ,vquit-function ,window ,value)
265 ,value)))))
267 ;; The following two functions are like `window-next-sibling' and
268 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
269 ;; they don't substitute the selected window for nil), and they return
270 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
271 ;; a minibuffer window).
272 (defun window-right (window)
273 "Return WINDOW's right sibling.
274 Return nil if WINDOW is the root window of its frame. WINDOW can
275 be any window."
276 (and window (window-parent window) (window-next-sibling window)))
278 (defun window-left (window)
279 "Return WINDOW's left sibling.
280 Return nil if WINDOW is the root window of its frame. WINDOW can
281 be any window."
282 (and window (window-parent window) (window-prev-sibling window)))
284 (defun window-child (window)
285 "Return WINDOW's first child window.
286 WINDOW can be any window."
287 (or (window-top-child window) (window-left-child window)))
289 (defun window-child-count (window)
290 "Return number of WINDOW's child windows.
291 WINDOW can be any window."
292 (let ((count 0))
293 (when (and (windowp window) (setq window (window-child window)))
294 (while window
295 (setq count (1+ count))
296 (setq window (window-next-sibling window))))
297 count))
299 (defun window-last-child (window)
300 "Return last child window of WINDOW.
301 WINDOW can be any window."
302 (when (and (windowp window) (setq window (window-child window)))
303 (while (window-next-sibling window)
304 (setq window (window-next-sibling window))))
305 window)
307 (defun window-normalize-buffer (buffer-or-name)
308 "Return buffer specified by BUFFER-OR-NAME.
309 BUFFER-OR-NAME must be either a buffer or a string naming a live
310 buffer and defaults to the current buffer."
311 (cond
312 ((not buffer-or-name)
313 (current-buffer))
314 ((bufferp buffer-or-name)
315 (if (buffer-live-p buffer-or-name)
316 buffer-or-name
317 (error "Buffer %s is not a live buffer" buffer-or-name)))
318 ((get-buffer buffer-or-name))
320 (error "No such buffer %s" buffer-or-name))))
322 (defun window-normalize-frame (frame)
323 "Return frame specified by FRAME.
324 FRAME must be a live frame and defaults to the selected frame."
325 (if frame
326 (if (frame-live-p frame)
327 frame
328 (error "%s is not a live frame" frame))
329 (selected-frame)))
331 (defun window-normalize-window (window &optional live-only)
332 "Return the window specified by WINDOW.
333 If WINDOW is nil, return the selected window. Otherwise, if
334 WINDOW is a live or an internal window, return WINDOW; if
335 LIVE-ONLY is non-nil, return WINDOW for a live window only.
336 Otherwise, signal an error."
337 (cond
338 ((null window)
339 (selected-window))
340 (live-only
341 (if (window-live-p window)
342 window
343 (error "%s is not a live window" window)))
344 ((window-valid-p window)
345 window)
347 (error "%s is not a valid window" window))))
349 ;; Maybe this should go to frame.el.
350 (defun frame-char-size (&optional window-or-frame horizontal)
351 "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
352 If WINDOW-OR-FRAME is a live frame, return the value of
353 `frame-char-height' for that frame. If WINDOW-OR-FRAME is a
354 valid window, return the value of `frame-char-height' for that
355 window's frame. In any other case, return the value of
356 `frame-char-height' for the selected frame.
358 Optional argument HORIZONTAL non-nil means to return the value of
359 `frame-char-width' for WINDOW-OR-FRAME."
360 (let ((frame
361 (cond
362 ((window-valid-p window-or-frame)
363 (window-frame window-or-frame))
364 ((frame-live-p window-or-frame)
365 window-or-frame)
366 (t (selected-frame)))))
367 (if horizontal
368 (frame-char-width frame)
369 (frame-char-height frame))))
371 (defvar ignore-window-parameters nil
372 "If non-nil, standard functions ignore window parameters.
373 The functions currently affected by this are `split-window',
374 `delete-window', `delete-other-windows' and `other-window'.
376 An application may bind this to a non-nil value around calls to
377 these functions to inhibit processing of window parameters.")
379 ;; This must go to C, finally (or get removed).
380 (defconst window-safe-min-height 1
381 "The absolute minimum number of lines of any window.
382 Anything less might crash Emacs.")
384 (defun window-safe-min-pixel-height (&optional window)
385 "Return the absolute minimum pixel height of WINDOW."
386 (* window-safe-min-height
387 (frame-char-size (window-normalize-window window))))
389 (defcustom window-min-height 4
390 "The minimum total height, in lines, of any window.
391 The value has to accommodate one text line, a mode and header
392 line, a horizontal scroll bar and a bottom divider, if present.
393 A value less than `window-safe-min-height' is ignored. The value
394 of this variable is honored when windows are resized or split.
396 Applications should never rebind this variable. To resize a
397 window to a height less than the one specified here, an
398 application should instead call `window-resize' with a non-nil
399 IGNORE argument. In order to have `split-window' make a window
400 shorter, explicitly specify the SIZE argument of that function."
401 :type 'integer
402 :version "24.1"
403 :group 'windows)
405 (defun window-min-pixel-height (&optional window)
406 "Return the minimum pixel height of window WINDOW."
407 (* (max window-min-height window-safe-min-height)
408 (frame-char-size window)))
410 ;; This must go to C, finally (or get removed).
411 (defconst window-safe-min-width 2
412 "The absolute minimum number of columns of a window.
413 Anything less might crash Emacs.")
415 (defun window-safe-min-pixel-width (&optional window)
416 "Return the absolute minimum pixel width of WINDOW."
417 (* window-safe-min-width
418 (frame-char-size (window-normalize-window window) t)))
420 (defcustom window-min-width 10
421 "The minimum total width, in columns, of any window.
422 The value has to accommodate two text columns as well as margins,
423 fringes, a scroll bar and a right divider, if present. A value
424 less than `window-safe-min-width' is ignored. The value of this
425 variable is honored when windows are resized or split.
427 Applications should never rebind this variable. To resize a
428 window to a width less than the one specified here, an
429 application should instead call `window-resize' with a non-nil
430 IGNORE argument. In order to have `split-window' make a window
431 narrower, explicitly specify the SIZE argument of that function."
432 :type 'integer
433 :version "24.1"
434 :group 'windows)
436 (defun window-min-pixel-width (&optional window)
437 "Return the minimum pixel width of window WINDOW."
438 (* (max window-min-width window-safe-min-width)
439 (frame-char-size window t)))
441 (defun window-safe-min-pixel-size (&optional window horizontal)
442 "Return the absolute minimum pixel height of WINDOW.
443 Optional argument HORIZONTAL non-nil means return the absolute
444 minimum pixel width of WINDOW."
445 (if horizontal
446 (window-safe-min-pixel-width window)
447 (window-safe-min-pixel-height window)))
449 (defun window-min-pixel-size (&optional window horizontal)
450 "Return the minimum pixel height of WINDOW.
451 Optional argument HORIZONTAL non-nil means return the minimum
452 pixel width of WINDOW."
453 (if horizontal
454 (window-min-pixel-width window)
455 (window-min-pixel-height window)))
457 (defun window-combined-p (&optional window horizontal)
458 "Return non-nil if WINDOW has siblings in a given direction.
459 WINDOW must be a valid window and defaults to the selected one.
461 HORIZONTAL determines a direction for the window combination. If
462 HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
463 a vertical window combination. If HORIZONTAL is non-nil, return
464 non-nil if WINDOW is part of a horizontal window combination."
465 (setq window (window-normalize-window window))
466 (let ((parent (window-parent window)))
467 (and parent
468 (if horizontal
469 (window-left-child parent)
470 (window-top-child parent)))))
472 (defun window-combination-p (&optional window horizontal)
473 "Return WINDOW's first child if WINDOW is a vertical combination.
474 WINDOW can be any window and defaults to the selected one.
475 Optional argument HORIZONTAL non-nil means return WINDOW's first
476 child if WINDOW is a horizontal combination."
477 (setq window (window-normalize-window window))
478 (if horizontal
479 (window-left-child window)
480 (window-top-child window)))
482 (defun window-combinations (window &optional horizontal)
483 "Return largest number of windows vertically arranged within WINDOW.
484 WINDOW must be a valid window and defaults to the selected one.
485 If HORIZONTAL is non-nil, return the largest number of
486 windows horizontally arranged within WINDOW."
487 (setq window (window-normalize-window window))
488 (cond
489 ((window-live-p window)
490 ;; If WINDOW is live, return 1.
492 ((if horizontal
493 (window-left-child window)
494 (window-top-child window))
495 ;; If WINDOW is iso-combined, return the sum of the values for all
496 ;; child windows of WINDOW.
497 (let ((child (window-child window))
498 (count 0))
499 (while child
500 (setq count
501 (+ (window-combinations child horizontal)
502 count))
503 (setq child (window-right child)))
504 count))
506 ;; If WINDOW is not iso-combined, return the maximum value of any
507 ;; child window of WINDOW.
508 (let ((child (window-child window))
509 (count 1))
510 (while child
511 (setq count
512 (max (window-combinations child horizontal)
513 count))
514 (setq child (window-right child)))
515 count))))
517 (defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only)
518 "Helper function for `walk-window-tree' and `walk-window-subtree'."
519 (let (walk-window-tree-buffer)
520 (while walk-window-tree-window
521 (setq walk-window-tree-buffer
522 (window-buffer walk-window-tree-window))
523 (when (or walk-window-tree-buffer any)
524 (funcall fun walk-window-tree-window))
525 (unless walk-window-tree-buffer
526 (walk-window-tree-1
527 fun (window-left-child walk-window-tree-window) any)
528 (walk-window-tree-1
529 fun (window-top-child walk-window-tree-window) any))
530 (if sub-only
531 (setq walk-window-tree-window nil)
532 (setq walk-window-tree-window
533 (window-right walk-window-tree-window))))))
535 (defun walk-window-tree (fun &optional frame any minibuf)
536 "Run function FUN on each live window of FRAME.
537 FUN must be a function with one argument - a window. FRAME must
538 be a live frame and defaults to the selected one. ANY, if
539 non-nil, means to run FUN on all live and internal windows of
540 FRAME.
542 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
543 window even if it isn't active. MINIBUF nil or omitted means run
544 FUN on FRAME's minibuffer window only if it's active. In both
545 cases the minibuffer window must be part of FRAME. MINIBUF
546 neither nil nor t means never run FUN on the minibuffer window.
548 This function performs a pre-order, depth-first traversal of the
549 window tree. If FUN changes the window tree, the result is
550 unpredictable."
551 (setq frame (window-normalize-frame frame))
552 (walk-window-tree-1 fun (frame-root-window frame) any)
553 (when (memq minibuf '(nil t))
554 ;; Run FUN on FRAME's minibuffer window if requested.
555 (let ((minibuffer-window (minibuffer-window frame)))
556 (when (and (window-live-p minibuffer-window)
557 (eq (window-frame minibuffer-window) frame)
558 (or (eq minibuf t)
559 (minibuffer-window-active-p minibuffer-window)))
560 (funcall fun minibuffer-window)))))
562 (defun walk-window-subtree (fun &optional window any)
563 "Run function FUN on the subtree of windows rooted at WINDOW.
564 WINDOW defaults to the selected window. FUN must be a function
565 with one argument - a window. By default, run FUN only on live
566 windows of the subtree. If the optional argument ANY is non-nil,
567 run FUN on all live and internal windows of the subtree. If
568 WINDOW is live, run FUN on WINDOW only.
570 This function performs a pre-order, depth-first traversal of the
571 subtree rooted at WINDOW. If FUN changes that tree, the result
572 is unpredictable."
573 (setq window (window-normalize-window window))
574 (walk-window-tree-1 fun window any t))
576 (defun window-with-parameter (parameter &optional value frame any minibuf)
577 "Return first window on FRAME with PARAMETER non-nil.
578 FRAME defaults to the selected frame. Optional argument VALUE
579 non-nil means only return a window whose window-parameter value
580 for PARAMETER equals VALUE (comparison is done with `equal').
581 Optional argument ANY non-nil means consider internal windows
582 too.
584 Optional argument MINIBUF t means consider FRAME's minibuffer
585 window even if it isn't active. MINIBUF nil or omitted means
586 consider FRAME's minibuffer window only if it's active. In both
587 cases the minibuffer window must be part of FRAME. MINIBUF
588 neither nil nor t means never consider the minibuffer window."
589 (let (this-value)
590 (catch 'found
591 (walk-window-tree
592 (lambda (window)
593 (when (and (setq this-value (window-parameter window parameter))
594 (or (not value) (equal value this-value)))
595 (throw 'found window)))
596 frame any minibuf))))
598 ;;; Atomic windows.
599 (defun window-atom-root (&optional window)
600 "Return root of atomic window WINDOW is a part of.
601 WINDOW must be a valid window and defaults to the selected one.
602 Return nil if WINDOW is not part of an atomic window."
603 (setq window (window-normalize-window window))
604 (let (root)
605 (while (and window (window-parameter window 'window-atom))
606 (setq root window)
607 (setq window (window-parent window)))
608 root))
610 (defun window-make-atom (window)
611 "Make WINDOW an atomic window.
612 WINDOW must be an internal window. Return WINDOW."
613 (if (not (window-child window))
614 (error "Window %s is not an internal window" window)
615 (walk-window-subtree
616 (lambda (window)
617 (unless (window-parameter window 'window-atom)
618 (set-window-parameter window 'window-atom t)))
619 window t)
620 window))
622 (defun display-buffer-in-atom-window (buffer alist)
623 "Display BUFFER in an atomic window.
624 This function displays BUFFER in a new window that will be
625 combined with an existing window to form an atomic window. If
626 the existing window is already part of an atomic window, add the
627 new window to that atomic window. Operations like `split-window'
628 or `delete-window', when applied to a constituent of an atomic
629 window, are applied atomically to the root of that atomic window.
631 ALIST is an association list of symbols and values. The
632 following symbols can be used.
634 `window' specifies the existing window the new window shall be
635 combined with. Use `window-atom-root' to make the new window a
636 sibling of an atomic window's root. If an internal window is
637 specified here, all children of that window become part of the
638 atomic window too. If no window is specified, the new window
639 becomes a sibling of the selected window. By default, the
640 `window-atom' parameter of the existing window is set to `main'
641 provided it is live and was not set before.
643 `side' denotes the side of the existing window where the new
644 window shall be located. Valid values are `below', `right',
645 `above' and `left'. The default is `below'. By default, the
646 `window-atom' parameter of the new window is set to this value.
648 The return value is the new window, nil when creating that window
649 failed."
650 (let* ((ignore-window-parameters t)
651 (window-combination-limit t)
652 (window-combination-resize 'atom)
653 (window (cdr (assq 'window alist)))
654 (side (cdr (assq 'side alist)))
655 (atom (when window (window-parameter window 'window-atom)))
656 root new)
657 (setq window (window-normalize-window window))
658 (setq root (window-atom-root window))
659 ;; Split off new window.
660 (when (setq new (split-window window nil side))
661 (window-make-atom
662 (if (and root (not (eq root window)))
663 ;; When WINDOW was part of an atomic window and we did not
664 ;; split its root, root atomic window at old root.
665 root
666 ;; Otherwise, root atomic window at WINDOW's new parent.
667 (window-parent window)))
668 ;; Assign `window-atom' parameters, if needed.
669 (when (and (not atom) (window-live-p window))
670 (set-window-parameter window 'window-atom 'main))
671 (set-window-parameter new 'window-atom side)
672 ;; Display BUFFER in NEW and return NEW.
673 (window--display-buffer
674 buffer new 'window alist display-buffer-mark-dedicated))))
676 (defun window--atom-check-1 (window)
677 "Subroutine of `window--atom-check'."
678 (when window
679 (if (window-parameter window 'window-atom)
680 (let ((count 0))
681 (when (or (catch 'reset
682 (walk-window-subtree
683 (lambda (window)
684 (if (window-parameter window 'window-atom)
685 (setq count (1+ count))
686 (throw 'reset t)))
687 window t))
688 ;; count >= 1 must hold here. If there's no other
689 ;; window around dissolve this atomic window.
690 (= count 1))
691 ;; Dissolve atomic window.
692 (walk-window-subtree
693 (lambda (window)
694 (set-window-parameter window 'window-atom nil))
695 window t)))
696 ;; Check children.
697 (unless (window-buffer window)
698 (window--atom-check-1 (window-left-child window))
699 (window--atom-check-1 (window-top-child window))))
700 ;; Check right sibling
701 (window--atom-check-1 (window-right window))))
703 (defun window--atom-check (&optional frame)
704 "Check atomicity of all windows on FRAME.
705 FRAME defaults to the selected frame. If an atomic window is
706 wrongly configured, reset the atomicity of all its windows on
707 FRAME to nil. An atomic window is wrongly configured if it has
708 no child windows or one of its child windows is not atomic."
709 (window--atom-check-1 (frame-root-window frame)))
711 ;; Side windows.
712 (defvar window-sides '(left top right bottom)
713 "Window sides.")
715 (defcustom window-sides-vertical nil
716 "If non-nil, left and right side windows are full height.
717 Otherwise, top and bottom side windows are full width."
718 :type 'boolean
719 :group 'windows
720 :version "24.1")
722 (defcustom window-sides-slots '(nil nil nil nil)
723 "Maximum number of side window slots.
724 The value is a list of four elements specifying the number of
725 side window slots on (in this order) the left, top, right and
726 bottom side of each frame. If an element is a number, this means
727 to display at most that many side windows on the corresponding
728 side. If an element is nil, this means there's no bound on the
729 number of slots on that side."
730 :version "24.1"
731 :risky t
732 :type
733 '(list
734 :value (nil nil nil nil)
735 (choice
736 :tag "Left"
737 :help-echo "Maximum slots of left side window."
738 :value nil
739 :format "%[Left%] %v\n"
740 (const :tag "Unlimited" :format "%t" nil)
741 (integer :tag "Number" :value 2 :size 5))
742 (choice
743 :tag "Top"
744 :help-echo "Maximum slots of top side window."
745 :value nil
746 :format "%[Top%] %v\n"
747 (const :tag "Unlimited" :format "%t" nil)
748 (integer :tag "Number" :value 3 :size 5))
749 (choice
750 :tag "Right"
751 :help-echo "Maximum slots of right side window."
752 :value nil
753 :format "%[Right%] %v\n"
754 (const :tag "Unlimited" :format "%t" nil)
755 (integer :tag "Number" :value 2 :size 5))
756 (choice
757 :tag "Bottom"
758 :help-echo "Maximum slots of bottom side window."
759 :value nil
760 :format "%[Bottom%] %v\n"
761 (const :tag "Unlimited" :format "%t" nil)
762 (integer :tag "Number" :value 3 :size 5)))
763 :group 'windows)
765 (defun window--side-window-p (window)
766 "Return non-nil if WINDOW is a side window or the parent of one."
767 (or (window-parameter window 'window-side)
768 (and (window-child window)
769 (or (window-parameter
770 (window-child window) 'window-side)
771 (window-parameter
772 (window-last-child window) 'window-side)))))
774 (defun window--major-non-side-window (&optional frame)
775 "Return the major non-side window of frame FRAME.
776 The optional argument FRAME must be a live frame and defaults to
777 the selected one.
779 If FRAME has at least one side window, the major non-side window
780 is either an internal non-side window such that all other
781 non-side windows on FRAME descend from it, or the single live
782 non-side window of FRAME. If FRAME has no side windows, return
783 its root window."
784 (let ((frame (window-normalize-frame frame))
785 major sibling)
786 ;; Set major to the _last_ window found by `walk-window-tree' that
787 ;; is not a side window but has a side window as its sibling.
788 (walk-window-tree
789 (lambda (window)
790 (and (not (window-parameter window 'window-side))
791 (or (and (setq sibling (window-prev-sibling window))
792 (window-parameter sibling 'window-side))
793 (and (setq sibling (window-next-sibling window))
794 (window-parameter sibling 'window-side)))
795 (setq major window)))
796 frame t 'nomini)
797 (or major (frame-root-window frame))))
799 (defun window--major-side-window (side)
800 "Return major side window on SIDE.
801 SIDE must be one of the symbols `left', `top', `right' or
802 `bottom'. Return nil if no such window exists."
803 (let ((root (frame-root-window))
804 window)
805 ;; (1) If a window on the opposite side exists, return that window's
806 ;; sibling.
807 ;; (2) If the new window shall span the entire side, return the
808 ;; frame's root window.
809 ;; (3) If a window on an orthogonal side exists, return that
810 ;; window's sibling.
811 ;; (4) Otherwise return the frame's root window.
812 (cond
813 ((or (and (eq side 'left)
814 (setq window (window-with-parameter 'window-side 'right nil t)))
815 (and (eq side 'top)
816 (setq window (window-with-parameter 'window-side 'bottom nil t))))
817 (window-prev-sibling window))
818 ((or (and (eq side 'right)
819 (setq window (window-with-parameter 'window-side 'left nil t)))
820 (and (eq side 'bottom)
821 (setq window (window-with-parameter 'window-side 'top nil t))))
822 (window-next-sibling window))
823 ((memq side '(left right))
824 (cond
825 (window-sides-vertical
826 root)
827 ((setq window (window-with-parameter 'window-side 'top nil t))
828 (window-next-sibling window))
829 ((setq window (window-with-parameter 'window-side 'bottom nil t))
830 (window-prev-sibling window))
831 (t root)))
832 ((memq side '(top bottom))
833 (cond
834 ((not window-sides-vertical)
835 root)
836 ((setq window (window-with-parameter 'window-side 'left nil t))
837 (window-next-sibling window))
838 ((setq window (window-with-parameter 'window-side 'right nil t))
839 (window-prev-sibling window))
840 (t root))))))
842 (defun display-buffer-in-major-side-window (buffer side slot &optional alist)
843 "Display BUFFER in a new window on SIDE of the selected frame.
844 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
845 specifies the slot to use. ALIST is an association list of
846 symbols and values as passed to `display-buffer-in-side-window'.
847 This function may be called only if no window on SIDE exists yet.
848 The new window automatically becomes the \"major\" side window on
849 SIDE. Return the new window, nil if its creation window failed."
850 (let* ((left-or-right (memq side '(left right)))
851 (major (window--major-side-window side))
852 (on-side (cond
853 ((eq side 'top) 'above)
854 ((eq side 'bottom) 'below)
855 (t side)))
856 ;; The following two bindings will tell `split-window' to take
857 ;; the space for the new window from `major' and not make a new
858 ;; parent window unless needed.
859 (window-combination-resize 'side)
860 (window-combination-limit nil)
861 (new (split-window major nil on-side)))
862 (when new
863 ;; Initialize `window-side' parameter of new window to SIDE.
864 (set-window-parameter new 'window-side side)
865 ;; Install `window-slot' parameter of new window.
866 (set-window-parameter new 'window-slot slot)
867 ;; Install `delete-window' parameter thus making sure that when
868 ;; the new window is deleted, a side window on the opposite side
869 ;; does not get resized.
870 (set-window-parameter new 'delete-window 'delete-side-window)
871 ;; Auto-adjust height/width of new window unless a size has been
872 ;; explicitly requested.
873 (unless (if left-or-right
874 (cdr (assq 'window-width alist))
875 (cdr (assq 'window-height alist)))
876 (setq alist
877 (cons
878 (cons
879 (if left-or-right 'window-width 'window-height)
880 (/ (window-total-size (frame-root-window) left-or-right)
881 ;; By default use a fourth of the size of the frame's
882 ;; root window.
884 alist)))
885 ;; Install BUFFER in new window and return NEW.
886 (window--display-buffer buffer new 'window alist 'side))))
888 (defun delete-side-window (window)
889 "Delete side window WINDOW."
890 (let ((window-combination-resize
891 (window-parameter (window-parent window) 'window-side))
892 (ignore-window-parameters t))
893 (delete-window window)))
895 (defun display-buffer-in-side-window (buffer alist)
896 "Display BUFFER in a side window of the selected frame.
897 ALIST is an association list of symbols and values. The
898 following special symbols can be used in ALIST.
900 `side' denotes the side of the frame where the new window shall
901 be located. Valid values are `bottom', `right', `top' and
902 `left'. The default is `bottom'.
904 `slot' if non-nil, specifies the window slot where to display
905 BUFFER. A value of zero or nil means use the middle slot on
906 the specified side. A negative value means use a slot
907 preceding (that is, above or on the left of) the middle slot.
908 A positive value means use a slot following (that is, below or
909 on the right of) the middle slot. The default is zero."
910 (let ((side (or (cdr (assq 'side alist)) 'bottom))
911 (slot (or (cdr (assq 'slot alist)) 0)))
912 (cond
913 ((not (memq side '(top bottom left right)))
914 (error "Invalid side %s specified" side))
915 ((not (numberp slot))
916 (error "Invalid slot %s specified" slot)))
918 (let* ((major (window-with-parameter 'window-side side nil t))
919 ;; `major' is the major window on SIDE, `windows' the list of
920 ;; life windows on SIDE.
921 (windows
922 (when major
923 (let (windows)
924 (walk-window-tree
925 (lambda (window)
926 (when (eq (window-parameter window 'window-side) side)
927 (setq windows (cons window windows))))
928 nil nil 'nomini)
929 (nreverse windows))))
930 (slots (when major (max 1 (window-child-count major))))
931 (max-slots
932 (nth (cond
933 ((eq side 'left) 0)
934 ((eq side 'top) 1)
935 ((eq side 'right) 2)
936 ((eq side 'bottom) 3))
937 window-sides-slots))
938 window this-window this-slot prev-window next-window
939 best-window best-slot abs-slot)
941 (cond
942 ((and (numberp max-slots) (<= max-slots 0))
943 ;; No side-slots available on this side. Don't create an error,
944 ;; just return nil.
945 nil)
946 ((not windows)
947 ;; No major window exists on this side, make one.
948 (display-buffer-in-major-side-window buffer side slot alist))
950 ;; Scan windows on SIDE.
951 (catch 'found
952 (dolist (window windows)
953 (setq this-slot (window-parameter window 'window-slot))
954 (cond
955 ;; The following should not happen and probably be checked
956 ;; by window--side-check.
957 ((not (numberp this-slot)))
958 ((= this-slot slot)
959 ;; A window with a matching slot has been found.
960 (setq this-window window)
961 (throw 'found t))
963 ;; Check if this window has a better slot value wrt the
964 ;; slot of the window we want.
965 (setq abs-slot
966 (if (or (and (> this-slot 0) (> slot 0))
967 (and (< this-slot 0) (< slot 0)))
968 (abs (- slot this-slot))
969 (+ (abs slot) (abs this-slot))))
970 (unless (and best-slot (<= best-slot abs-slot))
971 (setq best-window window)
972 (setq best-slot abs-slot))
973 (cond
974 ((<= this-slot slot)
975 (setq prev-window window))
976 ((not next-window)
977 (setq next-window window)))))))
979 ;; `this-window' is the first window with the same SLOT.
980 ;; `prev-window' is the window with the largest slot < SLOT. A new
981 ;; window will be created after it.
982 ;; `next-window' is the window with the smallest slot > SLOT. A new
983 ;; window will be created before it.
984 ;; `best-window' is the window with the smallest absolute difference
985 ;; of its slot and SLOT.
987 ;; Note: We dedicate the window used softly to its buffer to
988 ;; avoid that "other" (non-side) buffer display functions steal
989 ;; it from us. This must eventually become customizable via
990 ;; ALIST (or, better, avoided in the "other" functions).
991 (or (and this-window
992 ;; Reuse `this-window'.
993 (window--display-buffer buffer this-window 'reuse alist 'side))
994 (and (or (not max-slots) (< slots max-slots))
995 (or (and next-window
996 ;; Make new window before `next-window'.
997 (let ((next-side
998 (if (memq side '(left right)) 'above 'left))
999 (window-combination-resize 'side))
1000 (setq window (split-window next-window nil next-side))
1001 ;; When the new window is deleted, its space
1002 ;; is returned to other side windows.
1003 (set-window-parameter
1004 window 'delete-window 'delete-side-window)
1005 window))
1006 (and prev-window
1007 ;; Make new window after `prev-window'.
1008 (let ((prev-side
1009 (if (memq side '(left right)) 'below 'right))
1010 (window-combination-resize 'side))
1011 (setq window (split-window prev-window nil prev-side))
1012 ;; When the new window is deleted, its space
1013 ;; is returned to other side windows.
1014 (set-window-parameter
1015 window 'delete-window 'delete-side-window)
1016 window)))
1017 (set-window-parameter window 'window-slot slot)
1018 (window--display-buffer buffer window 'window alist 'side))
1019 (and best-window
1020 ;; Reuse `best-window'.
1021 (progn
1022 ;; Give best-window the new slot value.
1023 (set-window-parameter best-window 'window-slot slot)
1024 (window--display-buffer
1025 buffer best-window 'reuse alist 'side)))))))))
1027 (defun window--side-check (&optional frame)
1028 "Check the side window configuration of FRAME.
1029 FRAME defaults to the selected frame.
1031 A valid side window configuration preserves the following two
1032 invariants:
1034 - If there exists a window whose window-side parameter is
1035 non-nil, there must exist at least one live window whose
1036 window-side parameter is nil.
1038 - If a window W has a non-nil window-side parameter (i) it must
1039 have a parent window and that parent's window-side parameter
1040 must be either nil or the same as for W, and (ii) any child
1041 window of W must have the same window-side parameter as W.
1043 If the configuration is invalid, reset the window-side parameters
1044 of all windows on FRAME to nil."
1045 (let (left top right bottom none side parent parent-side)
1046 (when (or (catch 'reset
1047 (walk-window-tree
1048 (lambda (window)
1049 (setq side (window-parameter window 'window-side))
1050 (setq parent (window-parent window))
1051 (setq parent-side
1052 (and parent (window-parameter parent 'window-side)))
1053 ;; The following `cond' seems a bit tedious, but I'd
1054 ;; rather stick to using just the stack.
1055 (cond
1056 (parent-side
1057 (when (not (eq parent-side side))
1058 ;; A parent whose window-side is non-nil must
1059 ;; have a child with the same window-side.
1060 (throw 'reset t)))
1061 ((not side)
1062 (when (window-buffer window)
1063 ;; Record that we have at least one non-side,
1064 ;; live window.
1065 (setq none t)))
1066 ((if (memq side '(left top))
1067 (window-prev-sibling window)
1068 (window-next-sibling window))
1069 ;; Left and top major side windows must not have a
1070 ;; previous sibling, right and bottom major side
1071 ;; windows must not have a next sibling.
1072 (throw 'reset t))
1073 ;; Now check that there's no more than one major
1074 ;; window for any of left, top, right and bottom.
1075 ((eq side 'left)
1076 (if left (throw 'reset t) (setq left t)))
1077 ((eq side 'top)
1078 (if top (throw 'reset t) (setq top t)))
1079 ((eq side 'right)
1080 (if right (throw 'reset t) (setq right t)))
1081 ((eq side 'bottom)
1082 (if bottom (throw 'reset t) (setq bottom t)))
1084 (throw 'reset t))))
1085 frame t 'nomini))
1086 ;; If there's a side window, there must be at least one
1087 ;; non-side window.
1088 (and (or left top right bottom) (not none)))
1089 (walk-window-tree
1090 (lambda (window)
1091 (set-window-parameter window 'window-side nil))
1092 frame t 'nomini))))
1094 (defun window--check (&optional frame)
1095 "Check atomic and side windows on FRAME.
1096 FRAME defaults to the selected frame."
1097 (window--side-check frame)
1098 (window--atom-check frame))
1100 ;; Dumping frame/window contents.
1101 (defun window--dump-window (&optional window erase)
1102 "Dump WINDOW to buffer *window-frame-dump*.
1103 WINDOW must be a valid window and defaults to the selected one.
1104 Optional argument ERASE non-nil means erase *window-frame-dump*
1105 before writing to it."
1106 (setq window (window-normalize-window window))
1107 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1108 (when erase (erase-buffer))
1109 (insert
1110 (format "%s parent: %s\n" window (window-parent window))
1111 (format "pixel left: %s top: %s size: %s x %s new: %s\n"
1112 (window-pixel-left window) (window-pixel-top window)
1113 (window-size window t t) (window-size window nil t)
1114 (window-new-pixel window))
1115 (format "char left: %s top: %s size: %s x %s new: %s\n"
1116 (window-left-column window) (window-top-line window)
1117 (window-total-size window t) (window-total-size window)
1118 (window-new-total window))
1119 (format "normal: %s x %s new: %s\n"
1120 (window-normal-size window t) (window-normal-size window)
1121 (window-new-normal window)))
1122 (when (window-live-p window)
1123 (let ((fringes (window-fringes window))
1124 (margins (window-margins window)))
1125 (insert
1126 (format "body pixel: %s x %s char: %s x %s\n"
1127 (window-body-width window t) (window-body-height window t)
1128 (window-body-width window) (window-body-height window))
1129 (format "width left fringe: %s left margin: %s right margin: %s\n"
1130 (car fringes) (or (car margins) 0) (or (cdr margins) 0))
1131 (format "width right fringe: %s scroll-bar: %s divider: %s\n"
1132 (cadr fringes)
1133 (window-scroll-bar-width window)
1134 (window-right-divider-width window))
1135 (format "height header-line: %s mode-line: %s divider: %s\n"
1136 (window-header-line-height window)
1137 (window-mode-line-height window)
1138 (window-bottom-divider-width window)))))
1139 (insert "\n")))
1141 (defun window--dump-frame (&optional window-or-frame)
1142 "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
1143 WINDOW-OR-FRAME can be a frame or a window and defaults to the
1144 selected frame. When WINDOW-OR-FRAME is a window, dump that
1145 window's frame. The buffer *window-frame-dump* is erased before
1146 dumping to it."
1147 (let* ((window
1148 (cond
1149 ((or (not window-or-frame)
1150 (frame-live-p window-or-frame))
1151 (frame-root-window window-or-frame))
1152 ((or (window-live-p window-or-frame)
1153 (window-child window-or-frame))
1154 window-or-frame)
1156 (frame-root-window))))
1157 (frame (window-frame window)))
1158 (with-current-buffer (get-buffer-create "*window-frame-dump*")
1159 (erase-buffer)
1160 (insert
1161 (format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
1162 (frame-pixel-width frame) (frame-pixel-height frame)
1163 (frame-total-cols frame) (frame-total-lines frame)
1164 (frame-char-width frame) (frame-char-height frame))
1165 (format "frame text pixel: %s x %s cols/lines: %s x %s\n"
1166 (frame-text-width frame) (frame-text-height frame)
1167 (frame-text-cols frame) (frame-text-lines frame))
1168 (format "tool: %s scroll: %s/%s fringe: %s border: %s right: %s bottom: %s\n\n"
1169 (if (fboundp 'tool-bar-height)
1170 (tool-bar-height frame t)
1171 "0")
1172 (frame-scroll-bar-width frame)
1173 (frame-scroll-bar-height frame)
1174 (frame-fringe-width frame)
1175 (frame-border-width frame)
1176 (frame-right-divider-width frame)
1177 (frame-bottom-divider-width frame)))
1178 (walk-window-tree 'window--dump-window frame t t))))
1180 ;;; Window sizes.
1181 (defun window-total-size (&optional window horizontal round)
1182 "Return the total height or width of WINDOW.
1183 WINDOW must be a valid window and defaults to the selected one.
1185 If HORIZONTAL is omitted or nil, return the total height of
1186 WINDOW, in lines. If WINDOW is live, its total height includes,
1187 in addition to the height of WINDOW's text, the heights of
1188 WINDOW's mode and header line and a bottom divider, if any.
1190 If HORIZONTAL is non-nil, return the total width of WINDOW, in
1191 columns. If WINDOW is live, its total width includes, in
1192 addition to the width of WINDOW's text, the widths of WINDOW's
1193 fringes, margins, scroll bars and its right divider, if any.
1195 If WINDOW is internal, return the respective size of the screen
1196 areas spanned by its children.
1198 Optional argument ROUND is handled as for `window-total-height'
1199 and `window-total-width'."
1200 (if horizontal
1201 (window-total-width window round)
1202 (window-total-height window round)))
1204 (defun window-size (&optional window horizontal pixelwise round)
1205 "Return the height or width of WINDOW.
1206 WINDOW must be a valid window and defaults to the selected one.
1208 If HORIZONTAL is omitted or nil, return the total height of
1209 WINDOW, in lines, like `window-total-height'. Otherwise return
1210 the total width, in columns, like `window-total-width'.
1212 Optional argument PIXELWISE means return the pixel size of WINDOW
1213 like `window-pixel-height' and `window-pixel-width'.
1215 Optional argument ROUND is ignored if PIXELWISE is non-nil and
1216 handled as for `window-total-height' and `window-total-width'
1217 otherwise."
1218 (if horizontal
1219 (if pixelwise
1220 (window-pixel-width window)
1221 (window-total-width window round))
1222 (if pixelwise
1223 (window-pixel-height window)
1224 (window-total-height window round))))
1226 (defvar window-size-fixed nil
1227 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
1228 If the value is `height', then only the window's height is fixed.
1229 If the value is `width', then only the window's width is fixed.
1230 Any other non-nil value fixes both the width and the height.
1232 Emacs won't change the size of any window displaying that buffer,
1233 unless it has no other choice (like when deleting a neighboring
1234 window).")
1235 (make-variable-buffer-local 'window-size-fixed)
1237 (defun window--preservable-size (window &optional horizontal)
1238 "Return height of WINDOW as `window-preserve-size' would preserve it.
1239 Optional argument HORIZONTAL non-nil means to return the width of
1240 WINDOW as `window-preserve-size' would preserve it."
1241 (if horizontal
1242 (window-body-width window t)
1243 (+ (window-body-height window t)
1244 (window-header-line-height window)
1245 (window-mode-line-height window))))
1247 (defun window-preserve-size (&optional window horizontal preserve)
1248 "Preserve height of window WINDOW.
1249 WINDOW must be a live window and defaults to the selected one.
1250 Optional argument HORIZONTAL non-nil means preserve the width of
1251 WINDOW.
1253 PRESERVE t means to preserve the current height/width of WINDOW's
1254 body in frame and window resizing operations whenever possible.
1255 The height/width of WINDOW will change only if Emacs has no other
1256 choice. Resizing a window whose height/width is preserved never
1257 throws an error.
1259 PRESERVE nil means to stop preserving the height/width of WINDOW,
1260 lifting the respective restraint induced by a previous call of
1261 `window-preserve-size' for WINDOW. Calling `enlarge-window',
1262 `shrink-window', `split-window' or `fit-window-to-buffer' with
1263 WINDOW as argument also removes the respective restraint.
1265 Other values of PRESERVE are reserved for future use."
1266 (setq window (window-normalize-window window t))
1267 (let* ((parameter (window-parameter window 'window-preserved-size))
1268 (width (nth 1 parameter))
1269 (height (nth 2 parameter)))
1270 (if horizontal
1271 (set-window-parameter
1272 window 'window-preserved-size
1273 (list
1274 (window-buffer window)
1275 (and preserve (window--preservable-size window t))
1276 height))
1277 (set-window-parameter
1278 window 'window-preserved-size
1279 (list
1280 (window-buffer window)
1281 width
1282 (and preserve (window--preservable-size window)))))))
1284 (defun window-preserved-size (&optional window horizontal)
1285 "Return preserved height of window WINDOW.
1286 WINDOW must be a live window and defaults to the selected one.
1287 Optional argument HORIZONTAL non-nil means to return preserved
1288 width of WINDOW."
1289 (setq window (window-normalize-window window t))
1290 (let* ((parameter (window-parameter window 'window-preserved-size))
1291 (buffer (nth 0 parameter))
1292 (width (nth 1 parameter))
1293 (height (nth 2 parameter)))
1294 (when (eq buffer (window-buffer window))
1295 (if horizontal width height))))
1297 (defun window--preserve-size (window horizontal)
1298 "Return non-nil when the height of WINDOW shall be preserved.
1299 Optional argument HORIZONTAL non-nil means to return non-nil when
1300 the width of WINDOW shall be preserved."
1301 (let ((size (window-preserved-size window horizontal)))
1302 (and (numberp size)
1303 (= size (window--preservable-size window horizontal)))))
1305 (defun window-safe-min-size (&optional window horizontal pixelwise)
1306 "Return safe minimum size of WINDOW.
1307 WINDOW must be a valid window and defaults to the selected one.
1308 Optional argument HORIZONTAL non-nil means return the minimum
1309 number of columns of WINDOW; otherwise return the minimum number
1310 of WINDOW's lines.
1312 Optional argument PIXELWISE non-nil means return the minimum pixel-size
1313 of WINDOW."
1314 (setq window (window-normalize-window window))
1315 (if pixelwise
1316 (if horizontal
1317 (* window-safe-min-width
1318 (frame-char-width (window-frame window)))
1319 (* window-safe-min-height
1320 (frame-char-height (window-frame window))))
1321 (if horizontal window-safe-min-width window-safe-min-height)))
1323 (defun window-min-size (&optional window horizontal ignore pixelwise)
1324 "Return the minimum size of WINDOW.
1325 WINDOW must be a valid window and defaults to the selected one.
1326 Optional argument HORIZONTAL non-nil means return the minimum
1327 number of columns of WINDOW; otherwise return the minimum number
1328 of WINDOW's lines.
1330 The optional argument IGNORE has the same meaning as for
1331 `window-resizable'. Optional argument PIXELWISE non-nil means
1332 return the minimum pixel-size of WINDOW."
1333 (window--min-size-1
1334 (window-normalize-window window) horizontal ignore pixelwise))
1336 (defun window--min-size-ignore-p (window horizontal ignore)
1337 "Return non-nil if IGNORE says to ignore height restrictions for WINDOW.
1338 HORIZONTAL non-nil means to return non-nil if IGNORE says to
1339 ignore width restrictions for WINDOW."
1340 (if (window-valid-p ignore)
1341 (eq window ignore)
1342 (not (memq ignore '(nil preserved)))))
1344 (defun window--min-size-1 (window horizontal ignore pixelwise)
1345 "Internal function of `window-min-size'."
1346 (let ((sub (window-child window)))
1347 (if sub
1348 (let ((value 0))
1349 ;; WINDOW is an internal window.
1350 (if (window-combined-p sub horizontal)
1351 ;; The minimum size of an iso-combination is the sum of
1352 ;; the minimum sizes of its child windows.
1353 (while sub
1354 (setq value (+ value
1355 (window--min-size-1
1356 sub horizontal ignore pixelwise)))
1357 (setq sub (window-right sub)))
1358 ;; The minimum size of an ortho-combination is the maximum
1359 ;; of the minimum sizes of its child windows.
1360 (while sub
1361 (setq value (max value
1362 (window--min-size-1
1363 sub horizontal ignore pixelwise)))
1364 (setq sub (window-right sub))))
1365 value)
1366 (with-current-buffer (window-buffer window)
1367 (cond
1368 ((window-minibuffer-p window)
1369 (if pixelwise (frame-char-height (window-frame window)) 1))
1370 ((window-size-fixed-p window horizontal ignore)
1371 ;; The minimum size of a fixed size window is its size.
1372 (window-size window horizontal pixelwise))
1373 ((eq ignore 'safe)
1374 ;; If IGNORE equals `safe' return the safe value.
1375 (window-safe-min-size window horizontal pixelwise))
1376 (horizontal
1377 ;; For the minimum width of a window take fringes and
1378 ;; scroll-bars into account. This is questionable and should
1379 ;; be removed as soon as we are able to split (and resize)
1380 ;; windows such that the new (or resized) windows can get a
1381 ;; size less than the user-specified `window-min-height' and
1382 ;; `window-min-width'.
1383 (let* ((char-size (frame-char-size window t))
1384 (fringes (window-fringes window))
1385 (margins (window-margins window))
1386 (pixel-width
1387 (+ (window-safe-min-size window t t)
1388 (* (or (car margins) 0) char-size)
1389 (* (or (cdr margins) 0) char-size)
1390 (car fringes) (cadr fringes)
1391 (window-scroll-bar-width window)
1392 (window-right-divider-width window))))
1393 (if pixelwise
1394 (max
1395 (if window-resize-pixelwise
1396 pixel-width
1397 ;; Round up to next integral of columns.
1398 (* (ceiling pixel-width char-size) char-size))
1399 (if (window--min-size-ignore-p window horizontal ignore)
1401 (window-min-pixel-width window)))
1402 (max
1403 (ceiling pixel-width char-size)
1404 (if (window--min-size-ignore-p window horizontal ignore)
1406 window-min-width)))))
1407 ((let ((char-size (frame-char-size window))
1408 (pixel-height
1409 (+ (window-safe-min-size window nil t)
1410 (window-header-line-height window)
1411 (window-scroll-bar-height window)
1412 (window-mode-line-height window)
1413 (window-bottom-divider-width window))))
1414 (if pixelwise
1415 (max
1416 (if window-resize-pixelwise
1417 pixel-height
1418 ;; Round up to next integral of lines.
1419 (* (ceiling pixel-height char-size) char-size))
1420 (if (window--min-size-ignore-p window horizontal ignore)
1422 (window-min-pixel-height window)))
1423 (max (ceiling pixel-height char-size)
1424 (if (window--min-size-ignore-p window horizontal ignore)
1426 window-min-height))))))))))
1428 (defun window-sizable (window delta &optional horizontal ignore pixelwise)
1429 "Return DELTA if DELTA lines can be added to WINDOW.
1430 WINDOW must be a valid window and defaults to the selected one.
1431 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1432 columns can be added to WINDOW. A return value of zero means
1433 that no lines (or columns) can be added to WINDOW.
1435 This function looks only at WINDOW and, recursively, its child
1436 windows. The function `window-resizable' looks at other windows
1437 as well.
1439 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1440 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1441 return the maximum value in the range 0..DELTA by which WINDOW
1442 can be enlarged.
1444 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1445 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1446 return the minimum value in the range DELTA..0 by which WINDOW
1447 can be shrunk.
1449 The optional argument IGNORE has the same meaning as for
1450 `window-resizable'. Optional argument PIXELWISE non-nil means
1451 interpret DELTA as pixels."
1452 (setq window (window-normalize-window window))
1453 (cond
1454 ((< delta 0)
1455 (max (- (window-min-size window horizontal ignore pixelwise)
1456 (window-size window horizontal pixelwise))
1457 delta))
1458 ((> delta 0)
1459 (if (window-size-fixed-p window horizontal ignore)
1461 delta))
1462 (t 0)))
1464 (defun window-sizable-p (window delta &optional horizontal ignore pixelwise)
1465 "Return t if WINDOW can be resized by DELTA lines.
1466 WINDOW must be a valid window and defaults to the selected one.
1467 For the meaning of the arguments of this function see the
1468 doc-string of `window-sizable'."
1469 (setq window (window-normalize-window window))
1470 (if (> delta 0)
1471 (>= (window-sizable window delta horizontal ignore pixelwise)
1472 delta)
1473 (<= (window-sizable window delta horizontal ignore pixelwise)
1474 delta)))
1476 (defun window--size-fixed-1 (window horizontal ignore)
1477 "Internal function for `window-size-fixed-p'."
1478 (let ((sub (window-child window)))
1479 (catch 'fixed
1480 (if sub
1481 ;; WINDOW is an internal window.
1482 (if (window-combined-p sub horizontal)
1483 ;; An iso-combination is fixed size if all its child
1484 ;; windows are fixed-size.
1485 (progn
1486 (while sub
1487 (unless (window--size-fixed-1 sub horizontal ignore)
1488 ;; We found a non-fixed-size child window, so
1489 ;; WINDOW's size is not fixed.
1490 (throw 'fixed nil))
1491 (setq sub (window-right sub)))
1492 ;; All child windows are fixed-size, so WINDOW's size is
1493 ;; fixed.
1494 (throw 'fixed t))
1495 ;; An ortho-combination is fixed-size if at least one of its
1496 ;; child windows is fixed-size.
1497 (while sub
1498 (when (window--size-fixed-1 sub horizontal ignore)
1499 ;; We found a fixed-size child window, so WINDOW's size
1500 ;; is fixed.
1501 (throw 'fixed t))
1502 (setq sub (window-right sub))))
1503 ;; WINDOW is a live window.
1504 (and (or (not (windowp ignore)) (not (eq window ignore)))
1505 (or (and (not (eq ignore 'preserved))
1506 (window--preserve-size window horizontal))
1507 (with-current-buffer (window-buffer window)
1508 (if horizontal
1509 (memq window-size-fixed '(width t))
1510 (memq window-size-fixed '(height t))))))))))
1512 (defun window-size-fixed-p (&optional window horizontal ignore)
1513 "Return non-nil if WINDOW's height is fixed.
1514 WINDOW must be a valid window and defaults to the selected one.
1515 Optional argument HORIZONTAL non-nil means return non-nil if
1516 WINDOW's width is fixed. The optional argument IGNORE has the
1517 same meaning as for `window-resizable'.
1519 If this function returns nil, this does not necessarily mean that
1520 WINDOW can be resized in the desired direction. The function
1521 `window-resizable' can tell that."
1522 (when (or (windowp ignore) (memq ignore '(nil preserved)))
1523 (window--size-fixed-1
1524 (window-normalize-window window) horizontal ignore)))
1526 (defun window--min-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
1527 "Internal function for `window-min-delta'."
1528 (if (not (window-parent window))
1529 ;; If we can't go up, return zero.
1531 ;; Else try to find a non-fixed-size sibling of WINDOW.
1532 (let* ((parent (window-parent window))
1533 (sub (window-child parent)))
1534 (catch 'done
1535 (if (window-combined-p sub horizontal)
1536 ;; In an iso-combination throw DELTA if we find at least one
1537 ;; child window and that window is either not fixed-size or
1538 ;; we can ignore fixed-sizeness.
1539 (let ((skip (eq trail 'after)))
1540 (while sub
1541 (cond
1542 ((eq sub window)
1543 (setq skip (eq trail 'before)))
1544 (skip)
1545 ((window-size-fixed-p sub horizontal ignore))
1547 ;; We found a non-fixed-size child window.
1548 (throw 'done delta)))
1549 (setq sub (window-right sub))))
1550 ;; In an ortho-combination set DELTA to the minimum value by
1551 ;; which other child windows can shrink.
1552 (while sub
1553 (unless (eq sub window)
1554 (setq delta
1555 (min delta
1556 (max (- (window-size sub horizontal pixelwise 'ceiling)
1557 (window-min-size
1558 sub horizontal ignore pixelwise))
1559 0))))
1560 (setq sub (window-right sub))))
1561 (if noup
1562 delta
1563 (window--min-delta-1
1564 parent delta horizontal ignore trail nil pixelwise))))))
1566 (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise)
1567 "Return number of lines by which WINDOW can be shrunk.
1568 WINDOW must be a valid window and defaults to the selected one.
1569 Return zero if WINDOW cannot be shrunk.
1571 Optional argument HORIZONTAL non-nil means return number of
1572 columns by which WINDOW can be shrunk.
1574 The optional argument IGNORE has the same meaning as for
1575 `window-resizable'. Optional argument TRAIL restricts the
1576 windows that can be enlarged. If its value is `before', only
1577 windows to the left of or above WINDOW can be enlarged. If it is
1578 `after', only windows to the right of or below WINDOW can be
1579 enlarged.
1581 Optional argument NOUP non-nil means don't go up in the window
1582 tree, but try to enlarge windows within WINDOW's combination
1583 only. Optional argument NODOWN non-nil means don't check whether
1584 WINDOW itself (and its child windows) can be shrunk; check only
1585 whether at least one other window can be enlarged appropriately.
1587 Optional argument PIXELWISE non-nil means return number of pixels
1588 by which WINDOW can be shrunk."
1589 (setq window (window-normalize-window window))
1590 (let ((size (window-size window horizontal pixelwise 'floor))
1591 (minimum (window-min-size window horizontal ignore pixelwise)))
1592 (cond
1593 (nodown
1594 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1595 (window--min-delta-1
1596 window size horizontal ignore trail noup pixelwise))
1597 ((<= size minimum)
1598 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1599 ;; there's nothing to recover.
1602 ;; Otherwise, try to recover whatever WINDOW is larger than its
1603 ;; minimum size.
1604 (window--min-delta-1
1605 window (- size minimum) horizontal ignore trail noup pixelwise)))))
1607 (defun frame-windows-min-size (&optional frame horizontal ignore pixelwise)
1608 "Return minimum number of lines of FRAME's windows.
1609 HORIZONTAL non-nil means return number of columns of FRAME's
1610 windows. The optional argument IGNORE has the same meaning as
1611 for `window-resizable'. PIXELWISE non-nil means return sizes in
1612 pixels."
1613 (setq frame (window-normalize-frame frame))
1614 (let* ((root (frame-root-window frame))
1615 (mini (window-next-sibling root)))
1616 (+ (window-min-size root horizontal ignore pixelwise)
1617 (if (and mini (not horizontal))
1618 (window-min-size mini horizontal nil pixelwise)
1619 0))))
1621 (defun window--max-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
1622 "Internal function of `window-max-delta'."
1623 (if (not (window-parent window))
1624 ;; Can't go up. Return DELTA.
1625 delta
1626 (let* ((parent (window-parent window))
1627 (sub (window-child parent)))
1628 (catch 'fixed
1629 (if (window-combined-p sub horizontal)
1630 ;; For an iso-combination calculate how much we can get from
1631 ;; other child windows.
1632 (let ((skip (eq trail 'after)))
1633 (while sub
1634 (cond
1635 ((eq sub window)
1636 (setq skip (eq trail 'before)))
1637 (skip)
1639 (setq delta
1640 (+ delta
1641 (max
1642 (- (window-size sub horizontal pixelwise 'floor)
1643 (window-min-size
1644 sub horizontal ignore pixelwise))
1645 0)))))
1646 (setq sub (window-right sub))))
1647 ;; For an ortho-combination throw DELTA when at least one
1648 ;; child window is fixed-size.
1649 (while sub
1650 (when (and (not (eq sub window))
1651 (window-size-fixed-p sub horizontal ignore))
1652 (throw 'fixed delta))
1653 (setq sub (window-right sub))))
1654 (if noup
1655 ;; When NOUP is nil, DELTA is all we can get.
1656 delta
1657 ;; Else try with parent of WINDOW, passing the DELTA we
1658 ;; recovered so far.
1659 (window--max-delta-1
1660 parent delta horizontal ignore trail nil pixelwise))))))
1662 (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise)
1663 "Return maximum number of lines by which WINDOW can be enlarged.
1664 WINDOW must be a valid window and defaults to the selected one.
1665 The return value is zero if WINDOW cannot be enlarged.
1667 Optional argument HORIZONTAL non-nil means return maximum number
1668 of columns by which WINDOW can be enlarged.
1670 The optional argument IGNORE has the same meaning as for
1671 `window-resizable'. Optional argument TRAIL restricts the
1672 windows that can be enlarged. If its value is `before', only
1673 windows to the left of or above WINDOW can be enlarged. If it is
1674 `after', only windows to the right of or below WINDOW can be
1675 enlarged.
1677 Optional argument NOUP non-nil means don't go up in the window
1678 tree but try to obtain the entire space from windows within
1679 WINDOW's combination. Optional argument NODOWN non-nil means do
1680 not check whether WINDOW itself (and its child windows) can be
1681 enlarged; check only whether other windows can be shrunk
1682 appropriately.
1684 Optional argument PIXELWISE non-nil means return number of
1685 pixels by which WINDOW can be enlarged."
1686 (setq window (window-normalize-window window))
1687 (if (and (not nodown) (window-size-fixed-p window horizontal ignore))
1688 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1689 ;; size.
1691 ;; WINDOW has no fixed size.
1692 (window--max-delta-1 window 0 horizontal ignore trail noup pixelwise)))
1694 ;; Make NOUP also inhibit the min-size check.
1695 (defun window--resizable (window delta &optional horizontal ignore trail noup nodown pixelwise)
1696 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1697 WINDOW must be a valid window and defaults to the selected one.
1698 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1699 can be resized horizontally by DELTA columns. A return value of
1700 zero means that WINDOW is not resizable.
1702 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1703 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1704 return the maximum value in the range 0..DELTA by which WINDOW
1705 can be enlarged.
1707 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1708 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1709 return the minimum value in the range DELTA..0 that can be used
1710 for shrinking WINDOW.
1712 The optional argument IGNORE has the same meaning as for
1713 `window-resizable'. Optional argument TRAIL `before' means only
1714 windows to the left of or below WINDOW can be shrunk. Optional
1715 argument TRAIL `after' means only windows to the right of or
1716 above WINDOW can be shrunk.
1718 Optional argument NOUP non-nil means don't go up in the window
1719 tree but check only whether space can be obtained from (or given
1720 to) WINDOW's siblings. Optional argument NODOWN non-nil means
1721 don't go down in the window tree. This means do not check
1722 whether resizing would violate size restrictions of WINDOW or its
1723 child windows.
1725 Optional argument PIXELWISE non-nil means interpret DELTA as
1726 number of pixels."
1727 (setq window (window-normalize-window window))
1728 (cond
1729 ((< delta 0)
1730 (max (- (window-min-delta
1731 window horizontal ignore trail noup nodown pixelwise))
1732 delta))
1733 ((> delta 0)
1734 (min (window-max-delta
1735 window horizontal ignore trail noup nodown pixelwise)
1736 delta))
1737 (t 0)))
1739 (defun window--resizable-p (window delta &optional horizontal ignore trail noup nodown pixelwise)
1740 "Return t if WINDOW can be resized vertically by DELTA lines.
1741 WINDOW must be a valid window and defaults to the selected one.
1742 For the meaning of the arguments of this function see the
1743 doc-string of `window--resizable'.
1745 Optional argument PIXELWISE non-nil means interpret DELTA as
1746 pixels."
1747 (setq window (window-normalize-window window))
1748 (if (> delta 0)
1749 (>= (window--resizable
1750 window delta horizontal ignore trail noup nodown pixelwise)
1751 delta)
1752 (<= (window--resizable
1753 window delta horizontal ignore trail noup nodown pixelwise)
1754 delta)))
1756 (defun window-resizable (window delta &optional horizontal ignore pixelwise)
1757 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1758 WINDOW must be a valid window and defaults to the selected one.
1759 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1760 can be resized horizontally by DELTA columns. A return value of
1761 zero means that WINDOW is not resizable.
1763 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1764 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1765 return the maximum value in the range 0..DELTA by which WINDOW
1766 can be enlarged.
1768 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1769 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1770 return the minimum value in the range DELTA..0 that can be used
1771 for shrinking WINDOW.
1773 Optional argument IGNORE, if non-nil, means to ignore restraints
1774 induced by fixed size windows or the values of the variables
1775 `window-min-height' and `window-min-width'. The following values
1776 have special meanings: `safe' means that in addition live windows
1777 are allowed to get as small as `window-safe-min-height' lines and
1778 `window-safe-min-width' columns. `preserved' means to ignore
1779 only restrictions induced by `window-preserve-size'. If IGNORE
1780 is a window, then ignore restrictions for that window only.
1782 Optional argument PIXELWISE non-nil means interpret DELTA as
1783 pixels."
1784 (setq window (window-normalize-window window))
1785 (window--resizable window delta horizontal ignore nil nil nil pixelwise))
1787 (defun window-resizable-p (window delta &optional horizontal ignore pixelwise)
1788 "Return t if WINDOW can be resized vertically by DELTA lines.
1789 WINDOW must be a valid window and defaults to the selected one.
1790 For the meaning of the arguments of this function see the
1791 doc-string of `window-resizable'."
1792 (setq window (window-normalize-window window))
1793 (if (> delta 0)
1794 (>= (window--resizable
1795 window delta horizontal ignore nil nil nil pixelwise)
1796 delta)
1797 (<= (window--resizable
1798 window delta horizontal ignore nil nil nil pixelwise)
1799 delta)))
1801 ;; Aliases of functions defined in window.c.
1802 (defalias 'window-height 'window-total-height)
1803 (defalias 'window-width 'window-body-width)
1805 (defun window-full-height-p (&optional window)
1806 "Return t if WINDOW is as high as its containing frame.
1807 More precisely, return t if and only if the total height of
1808 WINDOW equals the total height of the root window of WINDOW's
1809 frame. WINDOW must be a valid window and defaults to the
1810 selected one."
1811 (setq window (window-normalize-window window))
1812 (if (window-minibuffer-p window)
1813 (eq window (frame-root-window (window-frame window)))
1814 (= (window-pixel-height window)
1815 (window-pixel-height (frame-root-window window)))))
1817 (defun window-full-width-p (&optional window)
1818 "Return t if WINDOW is as wide as its containing frame.
1819 More precisely, return t if and only if the total width of WINDOW
1820 equals the total width of the root window of WINDOW's frame.
1821 WINDOW must be a valid window and defaults to the selected one."
1822 (setq window (window-normalize-window window))
1823 (= (window-pixel-width window)
1824 (window-pixel-width (frame-root-window window))))
1826 (defun window-body-size (&optional window horizontal pixelwise)
1827 "Return the height or width of WINDOW's text area.
1828 WINDOW must be a live window and defaults to the selected one.
1830 If HORIZONTAL is omitted or nil, return the height of the text
1831 area, like `window-body-height'. Otherwise, return the width of
1832 the text area, like `window-body-width'. In either case, the
1833 optional argument PIXELWISE is passed to the functions."
1834 (if horizontal
1835 (window-body-width window pixelwise)
1836 (window-body-height window pixelwise)))
1838 (declare-function font-info "font.c" (name &optional frame))
1840 (defun window-font-width (&optional window face)
1841 "Return average character width for the font of FACE used in WINDOW.
1842 WINDOW must be a live window and defaults to the selected one.
1844 If FACE is nil or omitted, the default face is used. If FACE is
1845 remapped (see `face-remapping-alist'), the function returns the
1846 information for the remapped face."
1847 (with-selected-window (window-normalize-window window t)
1848 (if (display-multi-font-p)
1849 (let* ((face (if face face 'default))
1850 (info (font-info (face-font face)))
1851 (width (aref info 11)))
1852 (if (> width 0)
1853 width
1854 (aref info 10)))
1855 (frame-char-width))))
1857 (defun window-font-height (&optional window face)
1858 "Return character height for the font of FACE used in WINDOW.
1859 WINDOW must be a live window and defaults to the selected one.
1861 If FACE is nil or omitted, the default face is used. If FACE is
1862 remapped (see `face-remapping-alist'), the function returns the
1863 information for the remapped face."
1864 (with-selected-window (window-normalize-window window t)
1865 (if (display-multi-font-p)
1866 (let* ((face (if face face 'default))
1867 (info (font-info (face-font face))))
1868 (aref info 3))
1869 (frame-char-height))))
1871 (defvar overflow-newline-into-fringe)
1873 (defun window-max-chars-per-line (&optional window face)
1874 "Return the number of characters that can be displayed on one line in WINDOW.
1875 WINDOW must be a live window and defaults to the selected one.
1877 The character width of FACE is used for the calculation. If FACE
1878 is nil or omitted, the default face is used. If FACE is
1879 remapped (see `face-remapping-alist'), the function uses the
1880 remapped face.
1882 This function is different from `window-body-width' in two
1883 ways. First, it accounts for the portions of the line reserved
1884 for the continuation glyph. Second, it accounts for the size of
1885 the font."
1886 (with-selected-window (window-normalize-window window t)
1887 (let* ((window-width (window-body-width window t))
1888 (font-width (window-font-width window face))
1889 (ncols (/ window-width font-width)))
1890 (if (and (display-graphic-p)
1891 overflow-newline-into-fringe
1892 (/= (frame-parameter nil 'left-fringe) 0)
1893 (/= (frame-parameter nil 'right-fringe) 0))
1894 ncols
1895 (1- ncols)))))
1897 (defun window-current-scroll-bars (&optional window)
1898 "Return the current scroll bar types for WINDOW.
1899 WINDOW must be a live window and defaults to the selected one.
1901 The return value is a cons cell (VERTICAL . HORIZONTAL) where
1902 VERTICAL specifies the current location of the vertical scroll
1903 bar (`left', `right' or nil), and HORIZONTAL specifies the
1904 current location of the horizontal scroll bar (`bottom' or nil).
1906 Unlike `window-scroll-bars', this function reports the scroll bar
1907 type actually used, once frame defaults and `scroll-bar-mode' are
1908 taken into account."
1909 (setq window (window-normalize-window window t))
1910 (let ((vertical (nth 2 (window-scroll-bars window)))
1911 (horizontal (nth 5 (window-scroll-bars window)))
1912 (inherited (frame-current-scroll-bars (window-frame window))))
1913 (when (eq vertical t)
1914 (setq vertical (car inherited)))
1915 (when (eq horizontal t)
1916 (setq horizontal (cdr inherited)))
1917 (cons vertical (and horizontal 'bottom))))
1919 (defun walk-windows (fun &optional minibuf all-frames)
1920 "Cycle through all live windows, calling FUN for each one.
1921 FUN must specify a function with a window as its sole argument.
1922 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1923 windows to include in the walk.
1925 MINIBUF t means include the minibuffer window even if the
1926 minibuffer is not active. MINIBUF nil or omitted means include
1927 the minibuffer window only if the minibuffer is active. Any
1928 other value means do not include the minibuffer window even if
1929 the minibuffer is active.
1931 ALL-FRAMES nil or omitted means consider all windows on the
1932 selected frame, plus the minibuffer window if specified by the
1933 MINIBUF argument. If the minibuffer counts, consider all windows
1934 on all frames that share that minibuffer too. The following
1935 non-nil values of ALL-FRAMES have special meanings:
1937 - t means consider all windows on all existing frames.
1939 - `visible' means consider all windows on all visible frames on
1940 the current terminal.
1942 - 0 (the number zero) means consider all windows on all visible
1943 and iconified frames on the current terminal.
1945 - A frame means consider all windows on that frame only.
1947 Anything else means consider all windows on the selected frame
1948 and no others.
1950 This function changes neither the order of recently selected
1951 windows nor the buffer list."
1952 ;; If we start from the minibuffer window, don't fail to come
1953 ;; back to it.
1954 (when (window-minibuffer-p)
1955 (setq minibuf t))
1956 ;; Make sure to not mess up the order of recently selected
1957 ;; windows. Use `save-selected-window' and `select-window'
1958 ;; with second argument non-nil for this purpose.
1959 (save-selected-window
1960 (when (framep all-frames)
1961 (select-window (frame-first-window all-frames) 'norecord))
1962 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
1963 (funcall fun walk-windows-window))))
1965 (defun window-at-side-p (&optional window side)
1966 "Return t if WINDOW is at SIDE of its containing frame.
1967 WINDOW must be a valid window and defaults to the selected one.
1968 SIDE can be any of the symbols `left', `top', `right' or
1969 `bottom'. The default value nil is handled like `bottom'."
1970 (setq window (window-normalize-window window))
1971 (let ((edge
1972 (cond
1973 ((eq side 'left) 0)
1974 ((eq side 'top) 1)
1975 ((eq side 'right) 2)
1976 ((memq side '(bottom nil)) 3))))
1977 (= (nth edge (window-pixel-edges window))
1978 (nth edge (window-pixel-edges (frame-root-window window))))))
1980 (defun window-at-side-list (&optional frame side)
1981 "Return list of all windows on SIDE of FRAME.
1982 FRAME must be a live frame and defaults to the selected frame.
1983 SIDE can be any of the symbols `left', `top', `right' or
1984 `bottom'. The default value nil is handled like `bottom'."
1985 (setq frame (window-normalize-frame frame))
1986 (let (windows)
1987 (walk-window-tree
1988 (lambda (window)
1989 (when (window-at-side-p window side)
1990 (setq windows (cons window windows))))
1991 frame nil 'nomini)
1992 (nreverse windows)))
1994 (defun window--in-direction-2 (window posn &optional horizontal)
1995 "Support function for `window-in-direction'."
1996 (if horizontal
1997 (let ((top (window-pixel-top window)))
1998 (if (> top posn)
1999 (- top posn)
2000 (- posn top (window-pixel-height window))))
2001 (let ((left (window-pixel-left window)))
2002 (if (> left posn)
2003 (- left posn)
2004 (- posn left (window-pixel-width window))))))
2006 ;; Predecessors to the below have been devised by Julian Assange in
2007 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
2008 ;; Neither of these allow to selectively ignore specific windows
2009 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
2010 ;; the movement.
2011 (defun window-in-direction (direction &optional window ignore sign wrap mini)
2012 "Return window in DIRECTION as seen from WINDOW.
2013 More precisely, return the nearest window in direction DIRECTION
2014 as seen from the position of `window-point' in window WINDOW.
2015 DIRECTION must be one of `above', `below', `left' or `right'.
2016 WINDOW must be a live window and defaults to the selected one.
2018 Do not return a window whose `no-other-window' parameter is
2019 non-nil. If the nearest window's `no-other-window' parameter is
2020 non-nil, try to find another window in the indicated direction.
2021 If, however, the optional argument IGNORE is non-nil, return that
2022 window even if its `no-other-window' parameter is non-nil.
2024 Optional argument SIGN a negative number means to use the right
2025 or bottom edge of WINDOW as reference position instead of
2026 `window-point'. SIGN a positive number means to use the left or
2027 top edge of WINDOW as reference position.
2029 Optional argument WRAP non-nil means to wrap DIRECTION around
2030 frame borders. This means to return for WINDOW at the top of the
2031 frame and DIRECTION `above' the minibuffer window if the frame
2032 has one, and a window at the bottom of the frame otherwise.
2034 Optional argument MINI nil means to return the minibuffer window
2035 if and only if it is currently active. MINI non-nil means to
2036 return the minibuffer window even when it's not active. However,
2037 if WRAP non-nil, always act as if MINI were nil.
2039 Return nil if no suitable window can be found."
2040 (setq window (window-normalize-window window t))
2041 (unless (memq direction '(above below left right))
2042 (error "Wrong direction %s" direction))
2043 (let* ((frame (window-frame window))
2044 (hor (memq direction '(left right)))
2045 (first (if hor
2046 (window-pixel-left window)
2047 (window-pixel-top window)))
2048 (last (+ first (window-size window hor t)))
2049 ;; The column / row value of `posn-at-point' can be nil for the
2050 ;; mini-window, guard against that.
2051 (posn
2052 (cond
2053 ((and (numberp sign) (< sign 0))
2054 (if hor
2055 (1- (+ (window-pixel-top window) (window-pixel-height window)))
2056 (1- (+ (window-pixel-left window) (window-pixel-width window)))))
2057 ((and (numberp sign) (> sign 0))
2058 (if hor
2059 (window-pixel-top window)
2060 (window-pixel-left window)))
2061 ((let ((posn-cons (nth 2 (posn-at-point (window-point window) window))))
2062 (if hor
2063 (+ (or (cdr posn-cons) 1) (window-pixel-top window))
2064 (+ (or (car posn-cons) 1) (window-pixel-left window)))))))
2065 (best-edge
2066 (cond
2067 ((eq direction 'below) (frame-pixel-height frame))
2068 ((eq direction 'right) (frame-pixel-width frame))
2069 (t -1)))
2070 (best-edge-2 best-edge)
2071 (best-diff-2 (if hor (frame-pixel-height frame) (frame-pixel-width frame)))
2072 best best-2 best-diff-2-new)
2073 (walk-window-tree
2074 (lambda (w)
2075 (let* ((w-top (window-pixel-top w))
2076 (w-left (window-pixel-left w)))
2077 (cond
2078 ((or (eq window w)
2079 ;; Ignore ourselves.
2080 (and (window-parameter w 'no-other-window)
2081 ;; Ignore W unless IGNORE is non-nil.
2082 (not ignore))))
2083 (hor
2084 (cond
2085 ((and (<= w-top posn)
2086 (< posn (+ w-top (window-pixel-height w))))
2087 ;; W is to the left or right of WINDOW and covers POSN.
2088 (when (or (and (eq direction 'left)
2089 (or (and (<= w-left first) (> w-left best-edge))
2090 (and wrap
2091 (window-at-side-p window 'left)
2092 (window-at-side-p w 'right))))
2093 (and (eq direction 'right)
2094 (or (and (>= w-left last) (< w-left best-edge))
2095 (and wrap
2096 (window-at-side-p window 'right)
2097 (window-at-side-p w 'left)))))
2098 (setq best-edge w-left)
2099 (setq best w)))
2100 ((and (or (and (eq direction 'left)
2101 (<= (+ w-left (window-pixel-width w)) first))
2102 (and (eq direction 'right) (<= last w-left)))
2103 ;; W is to the left or right of WINDOW but does not
2104 ;; cover POSN.
2105 (setq best-diff-2-new
2106 (window--in-direction-2 w posn hor))
2107 (or (< best-diff-2-new best-diff-2)
2108 (and (= best-diff-2-new best-diff-2)
2109 (if (eq direction 'left)
2110 (> w-left best-edge-2)
2111 (< w-left best-edge-2)))))
2112 (setq best-edge-2 w-left)
2113 (setq best-diff-2 best-diff-2-new)
2114 (setq best-2 w))))
2115 ((and (<= w-left posn)
2116 (< posn (+ w-left (window-pixel-width w))))
2117 ;; W is above or below WINDOW and covers POSN.
2118 (when (or (and (eq direction 'above)
2119 (or (and (<= w-top first) (> w-top best-edge))
2120 (and wrap
2121 (window-at-side-p window 'top)
2122 (if (active-minibuffer-window)
2123 (minibuffer-window-active-p w)
2124 (window-at-side-p w 'bottom)))))
2125 (and (eq direction 'below)
2126 (or (and (>= w-top first) (< w-top best-edge))
2127 (and wrap
2128 (if (active-minibuffer-window)
2129 (minibuffer-window-active-p window)
2130 (window-at-side-p window 'bottom))
2131 (window-at-side-p w 'top)))))
2132 (setq best-edge w-top)
2133 (setq best w)))
2134 ((and (or (and (eq direction 'above)
2135 (<= (+ w-top (window-pixel-height w)) first))
2136 (and (eq direction 'below) (<= last w-top)))
2137 ;; W is above or below WINDOW but does not cover POSN.
2138 (setq best-diff-2-new
2139 (window--in-direction-2 w posn hor))
2140 (or (< best-diff-2-new best-diff-2)
2141 (and (= best-diff-2-new best-diff-2)
2142 (if (eq direction 'above)
2143 (> w-top best-edge-2)
2144 (< w-top best-edge-2)))))
2145 (setq best-edge-2 w-top)
2146 (setq best-diff-2 best-diff-2-new)
2147 (setq best-2 w)))))
2148 frame nil (and mini t))
2149 (or best best-2)))
2151 (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
2152 "Return a live window satisfying PREDICATE.
2153 More precisely, cycle through all windows calling the function
2154 PREDICATE on each one of them with the window as its sole
2155 argument. Return the first window for which PREDICATE returns
2156 non-nil. Windows are scanned starting with the window following
2157 the selected window. If no window satisfies PREDICATE, return
2158 DEFAULT.
2160 MINIBUF t means include the minibuffer window even if the
2161 minibuffer is not active. MINIBUF nil or omitted means include
2162 the minibuffer window only if the minibuffer is active. Any
2163 other value means do not include the minibuffer window even if
2164 the minibuffer is active.
2166 ALL-FRAMES nil or omitted means consider all windows on the selected
2167 frame, plus the minibuffer window if specified by the MINIBUF
2168 argument. If the minibuffer counts, consider all windows on all
2169 frames that share that minibuffer too. The following non-nil
2170 values of ALL-FRAMES have special meanings:
2172 - t means consider all windows on all existing frames.
2174 - `visible' means consider all windows on all visible frames on
2175 the current terminal.
2177 - 0 (the number zero) means consider all windows on all visible
2178 and iconified frames on the current terminal.
2180 - A frame means consider all windows on that frame only.
2182 Anything else means consider all windows on the selected frame
2183 and no others."
2184 (catch 'found
2185 (dolist (window (window-list-1
2186 (next-window nil minibuf all-frames)
2187 minibuf all-frames))
2188 (when (funcall predicate window)
2189 (throw 'found window)))
2190 default))
2192 (defalias 'some-window 'get-window-with-predicate)
2194 (defun get-lru-window (&optional all-frames dedicated not-selected)
2195 "Return the least recently used window on frames specified by ALL-FRAMES.
2196 Return a full-width window if possible. A minibuffer window is
2197 never a candidate. A dedicated window is never a candidate
2198 unless DEDICATED is non-nil, so if all windows are dedicated, the
2199 value is nil. Avoid returning the selected window if possible.
2200 Optional argument NOT-SELECTED non-nil means never return the
2201 selected window.
2203 The following non-nil values of the optional argument ALL-FRAMES
2204 have special meanings:
2206 - t means consider all windows on all existing frames.
2208 - `visible' means consider all windows on all visible frames on
2209 the current terminal.
2211 - 0 (the number zero) means consider all windows on all visible
2212 and iconified frames on the current terminal.
2214 - A frame means consider all windows on that frame only.
2216 Any other value of ALL-FRAMES means consider all windows on the
2217 selected frame and no others."
2218 (let (best-window best-time second-best-window second-best-time time)
2219 (dolist (window (window-list-1 nil 'nomini all-frames))
2220 (when (and (or dedicated (not (window-dedicated-p window)))
2221 (or (not not-selected) (not (eq window (selected-window)))))
2222 (setq time (window-use-time window))
2223 (if (or (eq window (selected-window))
2224 (not (window-full-width-p window)))
2225 (when (or (not second-best-time) (< time second-best-time))
2226 (setq second-best-time time)
2227 (setq second-best-window window))
2228 (when (or (not best-time) (< time best-time))
2229 (setq best-time time)
2230 (setq best-window window)))))
2231 (or best-window second-best-window)))
2233 (defun get-mru-window (&optional all-frames dedicated not-selected)
2234 "Return the most recently used window on frames specified by ALL-FRAMES.
2235 A minibuffer window is never a candidate. A dedicated window is
2236 never a candidate unless DEDICATED is non-nil, so if all windows
2237 are dedicated, the value is nil. Optional argument NOT-SELECTED
2238 non-nil means never return the selected window.
2240 The following non-nil values of the optional argument ALL-FRAMES
2241 have special meanings:
2243 - t means consider all windows on all existing frames.
2245 - `visible' means consider all windows on all visible frames on
2246 the current terminal.
2248 - 0 (the number zero) means consider all windows on all visible
2249 and iconified frames on the current terminal.
2251 - A frame means consider all windows on that frame only.
2253 Any other value of ALL-FRAMES means consider all windows on the
2254 selected frame and no others."
2255 (let (best-window best-time time)
2256 (dolist (window (window-list-1 nil 'nomini all-frames))
2257 (setq time (window-use-time window))
2258 (when (and (or dedicated (not (window-dedicated-p window)))
2259 (or (not not-selected) (not (eq window (selected-window))))
2260 (or (not best-time) (> time best-time)))
2261 (setq best-time time)
2262 (setq best-window window)))
2263 best-window))
2265 (defun get-largest-window (&optional all-frames dedicated not-selected)
2266 "Return the largest window on frames specified by ALL-FRAMES.
2267 A minibuffer window is never a candidate. A dedicated window is
2268 never a candidate unless DEDICATED is non-nil, so if all windows
2269 are dedicated, the value is nil. Optional argument NOT-SELECTED
2270 non-nil means never return the selected window.
2272 The following non-nil values of the optional argument ALL-FRAMES
2273 have special meanings:
2275 - t means consider all windows on all existing frames.
2277 - `visible' means consider all windows on all visible frames on
2278 the current terminal.
2280 - 0 (the number zero) means consider all windows on all visible
2281 and iconified frames on the current terminal.
2283 - A frame means consider all windows on that frame only.
2285 Any other value of ALL-FRAMES means consider all windows on the
2286 selected frame and no others."
2287 (let ((best-size 0)
2288 best-window size)
2289 (dolist (window (window-list-1 nil 'nomini all-frames))
2290 (when (and (or dedicated (not (window-dedicated-p window)))
2291 (or (not not-selected) (not (eq window (selected-window)))))
2292 (setq size (* (window-pixel-height window)
2293 (window-pixel-width window)))
2294 (when (> size best-size)
2295 (setq best-size size)
2296 (setq best-window window))))
2297 best-window))
2299 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
2300 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2301 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2302 and defaults to the current buffer. If the selected window displays
2303 BUFFER-OR-NAME, it will be the first in the resulting list.
2305 MINIBUF t means include the minibuffer window even if the
2306 minibuffer is not active. MINIBUF nil or omitted means include
2307 the minibuffer window only if the minibuffer is active. Any
2308 other value means do not include the minibuffer window even if
2309 the minibuffer is active.
2311 ALL-FRAMES nil or omitted means consider all windows on the
2312 selected frame, plus the minibuffer window if specified by the
2313 MINIBUF argument. If the minibuffer counts, consider all windows
2314 on all frames that share that minibuffer too. The following
2315 non-nil values of ALL-FRAMES have special meanings:
2317 - t means consider all windows on all existing frames.
2319 - `visible' means consider all windows on all visible frames on
2320 the current terminal.
2322 - 0 (the number zero) means consider all windows on all visible
2323 and iconified frames on the current terminal.
2325 - A frame means consider all windows on that frame only.
2327 Anything else means consider all windows on the selected frame
2328 and no others."
2329 (let ((buffer (window-normalize-buffer buffer-or-name))
2330 windows)
2331 (dolist (window (window-list-1 (selected-window) minibuf all-frames))
2332 (when (eq (window-buffer window) buffer)
2333 (setq windows (cons window windows))))
2334 (nreverse windows)))
2336 (defun minibuffer-window-active-p (window)
2337 "Return t if WINDOW is the currently active minibuffer window."
2338 (eq window (active-minibuffer-window)))
2340 (defun count-windows (&optional minibuf)
2341 "Return the number of live windows on the selected frame.
2342 The optional argument MINIBUF specifies whether the minibuffer
2343 window shall be counted. See `walk-windows' for the precise
2344 meaning of this argument."
2345 (length (window-list-1 nil minibuf)))
2347 ;;; Resizing windows.
2348 (defun window--size-to-pixel (window size &optional horizontal pixelwise round-maybe)
2349 "For WINDOW convert SIZE lines to pixels.
2350 SIZE is supposed to specify a height of WINDOW in terms of text
2351 lines. The return value is the number of pixels specifying that
2352 height.
2354 WINDOW must be a valid window. Optional argument HORIZONTAL
2355 non-nil means convert SIZE columns to pixels.
2357 Optional argument PIXELWISE non-nil means SIZE already specifies
2358 pixels but may have to be adjusted to a multiple of the character
2359 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2360 means round to the nearest multiple of the character size of
2361 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2362 (setq window (window-normalize-window window))
2363 (let ((char-size (frame-char-size window horizontal)))
2364 (if pixelwise
2365 (if (and round-maybe (not window-resize-pixelwise))
2366 (* (round size char-size) char-size)
2367 size)
2368 (* size char-size))))
2370 (defun window--pixel-to-total-1 (window horizontal char-size)
2371 "Subroutine of `window--pixel-to-total'."
2372 (let ((child (window-child window)))
2373 (if (window-combination-p window horizontal)
2374 ;; In an iso-combination distribute sizes proportionally.
2375 (let ((remainder (window-new-total window))
2376 size best-child rem best-rem)
2377 ;; Initialize total sizes to each child's floor.
2378 (while child
2379 (setq size (max (/ (window-size child horizontal t) char-size) 1))
2380 (set-window-new-total child size)
2381 (setq remainder (- remainder size))
2382 (setq child (window-next-sibling child)))
2383 ;; Distribute remainder.
2384 (while (> remainder 0)
2385 (setq child (window-last-child window))
2386 (setq best-child nil)
2387 (setq best-rem 0)
2388 (while child
2389 (when (and (<= (window-new-total child)
2390 (/ (window-size child horizontal t) char-size))
2391 (> (setq rem (% (window-size child horizontal t)
2392 char-size))
2393 best-rem))
2394 (setq best-child child)
2395 (setq best-rem rem))
2396 (setq child (window-prev-sibling child)))
2397 ;; We MUST have a best-child here.
2398 (set-window-new-total best-child 1 t)
2399 (setq remainder (1- remainder)))
2400 ;; Recurse.
2401 (setq child (window-child window))
2402 (while child
2403 (window--pixel-to-total-1 child horizontal char-size)
2404 (setq child (window-next-sibling child))))
2405 ;; In an ortho-combination assign new sizes directly.
2406 (let ((size (window-new-total window)))
2407 (while child
2408 (set-window-new-total child size)
2409 (window--pixel-to-total-1 child horizontal char-size)
2410 (setq child (window-next-sibling child)))))))
2412 (defun window--pixel-to-total (&optional frame horizontal)
2413 "On FRAME assign new total window heights from pixel heights.
2414 FRAME must be a live frame and defaults to the selected frame.
2416 Optional argument HORIZONTAL non-nil means assign new total
2417 window widths from pixel widths."
2418 (setq frame (window-normalize-frame frame))
2419 (let* ((char-size (frame-char-size frame horizontal))
2420 (root (frame-root-window frame))
2421 (root-size (window-size root horizontal t))
2422 ;; We have to care about the minibuffer window only if it
2423 ;; appears together with the root window on this frame.
2424 (mini (let ((mini (minibuffer-window frame)))
2425 (and (eq (window-frame mini) frame)
2426 (not (eq mini root)) mini)))
2427 (mini-size (and mini (window-size mini horizontal t))))
2428 ;; We round the line/column sizes of windows here to the nearest
2429 ;; integer. In some cases this can make windows appear _larger_
2430 ;; than the containing frame (line/column-wise) because the latter's
2431 ;; sizes are not (yet) rounded. We might eventually fix that.
2432 (if (and mini (not horizontal))
2433 (let (lines)
2434 (set-window-new-total root (max (/ root-size char-size) 1))
2435 (set-window-new-total mini (max (/ mini-size char-size) 1))
2436 (setq lines (- (round (+ root-size mini-size) char-size)
2437 (+ (window-new-total root) (window-new-total mini))))
2438 (while (> lines 0)
2439 (if (>= (% root-size (window-new-total root))
2440 (% mini-size (window-new-total mini)))
2441 (set-window-new-total root 1 t)
2442 (set-window-new-total mini 1 t))
2443 (setq lines (1- lines))))
2444 (set-window-new-total root (round root-size char-size))
2445 (when mini
2446 ;; This is taken in the horizontal case only.
2447 (set-window-new-total mini (round mini-size char-size))))
2448 (unless (window-buffer root)
2449 (window--pixel-to-total-1 root horizontal char-size))
2450 ;; Apply the new sizes.
2451 (window-resize-apply-total frame horizontal)))
2453 (defun window--resize-reset (&optional frame horizontal)
2454 "Reset resize values for all windows on FRAME.
2455 FRAME defaults to the selected frame.
2457 This function stores the current value of `window-size' applied
2458 with argument HORIZONTAL in the new total size of all windows on
2459 FRAME. It also resets the new normal size of each of these
2460 windows."
2461 (window--resize-reset-1
2462 (frame-root-window (window-normalize-frame frame)) horizontal))
2464 (defun window--resize-reset-1 (window horizontal)
2465 "Internal function of `window--resize-reset'."
2466 ;; Register old size in the new total size.
2467 (set-window-new-pixel window (window-size window horizontal t))
2468 (set-window-new-total window (window-size window horizontal))
2469 ;; Reset new normal size.
2470 (set-window-new-normal window)
2471 (when (window-child window)
2472 (window--resize-reset-1 (window-child window) horizontal))
2473 (when (window-right window)
2474 (window--resize-reset-1 (window-right window) horizontal)))
2476 ;; The following routine is used to manually resize the minibuffer
2477 ;; window and is currently used, for example, by ispell.el.
2478 (defun window--resize-mini-window (window delta)
2479 "Resize minibuffer window WINDOW by DELTA pixels.
2480 If WINDOW cannot be resized by DELTA pixels make it as large (or
2481 as small) as possible, but don't signal an error."
2482 (when (window-minibuffer-p window)
2483 (let* ((frame (window-frame window))
2484 (root (frame-root-window frame))
2485 (height (window-pixel-height window))
2486 (min-delta
2487 (- (window-pixel-height root)
2488 (window-min-size root nil nil t))))
2489 ;; Sanitize DELTA.
2490 (cond
2491 ((<= (+ height delta) 0)
2492 (setq delta (- (frame-char-height (window-frame window)) height)))
2493 ((> delta min-delta)
2494 (setq delta min-delta)))
2496 (unless (zerop delta)
2497 ;; Resize now.
2498 (window--resize-reset frame)
2499 ;; Ideally we should be able to resize just the last child of root
2500 ;; here. See the comment in `resize-root-window-vertically' for
2501 ;; why we do not do that.
2502 (window--resize-this-window root (- delta) nil nil t)
2503 (set-window-new-pixel window (+ height delta))
2504 ;; The following routine catches the case where we want to resize
2505 ;; a minibuffer-only frame.
2506 (when (resize-mini-window-internal window)
2507 (window--pixel-to-total frame)
2508 (run-window-configuration-change-hook frame))))))
2510 (defun window--resize-apply-p (frame &optional horizontal)
2511 "Return t when a window on FRAME shall be resized vertically.
2512 Optional argument HORIZONTAL non-nil means return t when a window
2513 shall be resized horizontally."
2514 (catch 'apply
2515 (walk-window-tree
2516 (lambda (window)
2517 (unless (= (window-new-pixel window)
2518 (window-size window horizontal t))
2519 (throw 'apply t)))
2520 frame t)
2521 nil))
2523 (defun window-resize (window delta &optional horizontal ignore pixelwise)
2524 "Resize WINDOW vertically by DELTA lines.
2525 WINDOW can be an arbitrary window and defaults to the selected
2526 one. An attempt to resize the root window of a frame will raise
2527 an error though.
2529 DELTA a positive number means WINDOW shall be enlarged by DELTA
2530 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2531 lines.
2533 Optional argument HORIZONTAL non-nil means resize WINDOW
2534 horizontally by DELTA columns. In this case a positive DELTA
2535 means enlarge WINDOW by DELTA columns. DELTA negative means
2536 WINDOW shall be shrunk by -DELTA columns.
2538 Optional argument IGNORE, if non-nil, means to ignore restraints
2539 induced by fixed size windows or the values of the variables
2540 `window-min-height' and `window-min-width'. The following values
2541 have special meanings: `safe' means that in addition live windows
2542 are allowed to get as small as `window-safe-min-height' lines and
2543 `window-safe-min-width' columns. `preserved' means to ignore
2544 only restrictions induced by `window-preserve-size'. If IGNORE
2545 is a window, then ignore restrictions for that window only.
2547 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2548 pixels.
2550 This function resizes other windows proportionally and never
2551 deletes any windows. If you want to move only the low (right)
2552 edge of WINDOW consider using `adjust-window-trailing-edge'
2553 instead."
2554 (setq window (window-normalize-window window))
2555 (let* ((frame (window-frame window))
2556 (minibuffer-window (minibuffer-window frame))
2557 sibling)
2558 (setq delta (window--size-to-pixel
2559 window delta horizontal pixelwise t))
2560 (cond
2561 ((eq window (frame-root-window frame))
2562 (error "Cannot resize the root window of a frame"))
2563 ((window-minibuffer-p window)
2564 (if horizontal
2565 (error "Cannot resize minibuffer window horizontally")
2566 (window--resize-mini-window window delta)))
2567 ((and (not horizontal)
2568 (window-full-height-p window)
2569 (eq (window-frame minibuffer-window) frame)
2570 (or (not resize-mini-windows)
2571 (eq minibuffer-window (active-minibuffer-window))))
2572 ;; If WINDOW is full height and either `resize-mini-windows' is
2573 ;; nil or the minibuffer window is active, resize the minibuffer
2574 ;; window.
2575 (window--resize-mini-window minibuffer-window (- delta)))
2576 ((or (window--resizable-p
2577 window delta horizontal ignore nil nil nil t)
2578 (and (not ignore)
2579 (setq ignore 'preserved)
2580 (window--resizable-p
2581 window delta horizontal ignore nil nil nil t)))
2582 (window--resize-reset frame horizontal)
2583 (window--resize-this-window window delta horizontal ignore t)
2584 (if (and (not window-combination-resize)
2585 (window-combined-p window horizontal)
2586 (setq sibling (or (window-right window) (window-left window)))
2587 (window-sizable-p
2588 sibling (- delta) horizontal ignore t))
2589 ;; If window-combination-resize is nil, WINDOW is part of an
2590 ;; iso-combination, and WINDOW's neighboring right or left
2591 ;; sibling can be resized as requested, resize that sibling.
2592 (let ((normal-delta
2593 (/ (float delta)
2594 (window-size (window-parent window) horizontal t))))
2595 (window--resize-this-window sibling (- delta) horizontal nil t)
2596 (set-window-new-normal
2597 window (+ (window-normal-size window horizontal)
2598 normal-delta))
2599 (set-window-new-normal
2600 sibling (- (window-normal-size sibling horizontal)
2601 normal-delta)))
2602 ;; Otherwise, resize all other windows in the same combination.
2603 (window--resize-siblings window delta horizontal ignore))
2604 (when (window--resize-apply-p frame horizontal)
2605 (if (window-resize-apply frame horizontal)
2606 (progn
2607 (window--pixel-to-total frame horizontal)
2608 (run-window-configuration-change-hook frame))
2609 (error "Failed to apply resizing %s" window))))
2611 (error "Cannot resize window %s" window)))))
2613 (defun window-resize-no-error (window delta &optional horizontal ignore pixelwise)
2614 "Resize WINDOW vertically if it is resizable by DELTA lines.
2615 This function is like `window-resize' but does not signal an
2616 error when WINDOW cannot be resized. For the meaning of the
2617 optional arguments see the documentation of `window-resize'.
2619 Optional argument PIXELWISE non-nil means interpret DELTA as
2620 pixels."
2621 (when (window--resizable-p
2622 window delta horizontal ignore nil nil nil pixelwise)
2623 (window-resize window delta horizontal ignore pixelwise)))
2625 (defun window--resize-child-windows-skip-p (window)
2626 "Return non-nil if WINDOW shall be skipped by resizing routines."
2627 (memq (window-new-normal window) '(ignore stuck skip)))
2629 (defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
2630 "Recursively set new normal height of child windows of window PARENT.
2631 HORIZONTAL non-nil means set the new normal width of these
2632 windows. WINDOW specifies a child window of PARENT that has been
2633 resized by THIS-DELTA lines (columns).
2635 Optional argument TRAIL either `before' or `after' means set values
2636 only for windows before or after WINDOW. Optional argument
2637 OTHER-DELTA, a number, specifies that this many lines (columns)
2638 have been obtained from (or returned to) an ancestor window of
2639 PARENT in order to resize WINDOW."
2640 (let* ((delta-normal
2641 (if (and (= (- this-delta)
2642 (window-size window horizontal t))
2643 (zerop other-delta))
2644 ;; When WINDOW gets deleted and we can return its entire
2645 ;; space to its siblings, use WINDOW's normal size as the
2646 ;; normal delta.
2647 (- (window-normal-size window horizontal))
2648 ;; In any other case calculate the normal delta from the
2649 ;; relation of THIS-DELTA to the total size of PARENT.
2650 (/ (float this-delta)
2651 (window-size parent horizontal t))))
2652 (sub (window-child parent))
2653 (parent-normal 0.0)
2654 (skip (eq trail 'after)))
2656 ;; Set parent-normal to the sum of the normal sizes of all child
2657 ;; windows of PARENT that shall be resized, excluding only WINDOW
2658 ;; and any windows specified by the optional TRAIL argument.
2659 (while sub
2660 (cond
2661 ((eq sub window)
2662 (setq skip (eq trail 'before)))
2663 (skip)
2665 (setq parent-normal
2666 (+ parent-normal (window-normal-size sub horizontal)))))
2667 (setq sub (window-right sub)))
2669 ;; Set the new normal size of all child windows of PARENT from what
2670 ;; they should have contributed for recovering THIS-DELTA lines
2671 ;; (columns).
2672 (setq sub (window-child parent))
2673 (setq skip (eq trail 'after))
2674 (while sub
2675 (cond
2676 ((eq sub window)
2677 (setq skip (eq trail 'before)))
2678 (skip)
2680 (let ((old-normal (window-normal-size sub horizontal)))
2681 (set-window-new-normal
2682 sub (min 1.0 ; Don't get larger than 1.
2683 (max (- old-normal
2684 (* (/ old-normal parent-normal)
2685 delta-normal))
2686 ;; Don't drop below 0.
2687 0.0))))))
2688 (setq sub (window-right sub)))
2690 (when (numberp other-delta)
2691 ;; Set the new normal size of windows from what they should have
2692 ;; contributed for recovering OTHER-DELTA lines (columns).
2693 (setq delta-normal (/ (float (window-size parent horizontal t))
2694 (+ (window-size parent horizontal t)
2695 other-delta)))
2696 (setq sub (window-child parent))
2697 (setq skip (eq trail 'after))
2698 (while sub
2699 (cond
2700 ((eq sub window)
2701 (setq skip (eq trail 'before)))
2702 (skip)
2704 (set-window-new-normal
2705 sub (min 1.0 ; Don't get larger than 1.
2706 (max (* (window-new-normal sub) delta-normal)
2707 ;; Don't drop below 0.
2708 0.0)))))
2709 (setq sub (window-right sub))))
2711 ;; Set the new normal size of WINDOW to what is left by the sum of
2712 ;; the normal sizes of its siblings.
2713 (set-window-new-normal
2714 window
2715 (let ((sum 0))
2716 (setq sub (window-child parent))
2717 (while sub
2718 (cond
2719 ((eq sub window))
2720 ((not (numberp (window-new-normal sub)))
2721 (setq sum (+ sum (window-normal-size sub horizontal))))
2723 (setq sum (+ sum (window-new-normal sub)))))
2724 (setq sub (window-right sub)))
2725 ;; Don't get larger than 1 or smaller than 0.
2726 (min 1.0 (max (- 1.0 sum) 0.0))))))
2728 (defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge char-size)
2729 "Resize child windows of window PARENT vertically by DELTA pixels.
2730 PARENT must be a vertically combined internal window.
2732 Optional argument HORIZONTAL non-nil means resize child windows
2733 of PARENT horizontally by DELTA pixels. In this case PARENT must
2734 be a horizontally combined internal window.
2736 WINDOW, if specified, must denote a child window of PARENT that
2737 is resized by DELTA pixels.
2739 The optional argument IGNORE has the same meaning as for
2740 `window-resizable'.
2742 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2743 of windows that shall be resized. If TRAIL equals `before',
2744 resize only windows on the left or above EDGE. If TRAIL equals
2745 `after', resize only windows on the right or below EDGE. Also,
2746 preferably only resize windows adjacent to EDGE.
2748 If the optional argument CHAR-SIZE is a positive integer, it specifies
2749 the number of pixels by which windows are incrementally resized.
2750 If CHAR-SIZE is nil, this means to use the value of
2751 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2753 Return the symbol `normalized' if new normal sizes have been
2754 already set by this routine."
2755 (let* ((first (window-child parent))
2756 (last (window-last-child parent))
2757 (parent-total (+ (window-size parent horizontal t)
2758 delta))
2759 (char-size (or char-size
2760 (and window-resize-pixelwise 1)
2761 (frame-char-size window horizontal)))
2762 sub best-window best-value best-delta)
2764 (if (and edge (memq trail '(before after))
2765 (progn
2766 (setq sub first)
2767 (while (and (window-right sub)
2768 (or (and (eq trail 'before)
2769 (not (window--resize-child-windows-skip-p
2770 (window-right sub))))
2771 (and (eq trail 'after)
2772 (window--resize-child-windows-skip-p sub))))
2773 (setq sub (window-right sub)))
2774 sub)
2775 (if horizontal
2776 (if (eq trail 'before)
2777 (= (+ (window-pixel-left sub) (window-pixel-width sub))
2778 edge)
2779 (= (window-pixel-left sub) edge))
2780 (if (eq trail 'before)
2781 (= (+ (window-pixel-top sub) (window-pixel-height sub))
2782 edge)
2783 (= (window-pixel-top sub) edge)))
2784 (window-sizable-p sub delta horizontal ignore t))
2785 ;; Resize only windows adjacent to EDGE.
2786 (progn
2787 (window--resize-this-window
2788 sub delta horizontal ignore t trail edge)
2789 (if (and window (eq (window-parent sub) parent))
2790 (progn
2791 ;; Assign new normal sizes.
2792 (set-window-new-normal
2793 sub (/ (float (window-new-pixel sub)) parent-total))
2794 (set-window-new-normal
2795 window (- (window-normal-size window horizontal)
2796 (- (window-new-normal sub)
2797 (window-normal-size sub horizontal)))))
2798 (window--resize-child-windows-normal
2799 parent horizontal sub 0 trail delta))
2800 ;; Return 'normalized to notify `window--resize-siblings' that
2801 ;; normal sizes have been already set.
2802 'normalized)
2803 ;; Resize all windows proportionally.
2804 (setq sub last)
2805 (while sub
2806 (cond
2807 ((or (window--resize-child-windows-skip-p sub)
2808 ;; Ignore windows to skip and fixed-size child windows -
2809 ;; in the latter case make it a window to skip.
2810 (and (not ignore)
2811 (window-size-fixed-p sub horizontal ignore)
2812 (set-window-new-normal sub 'ignore))))
2813 ((< delta 0)
2814 ;; When shrinking store the number of lines/cols we can get
2815 ;; from this window here together with the total/normal size
2816 ;; factor.
2817 (set-window-new-normal
2819 (cons
2820 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
2821 (window-min-delta sub horizontal ignore trail t nil t)
2822 (- (/ (float (window-size sub horizontal t))
2823 parent-total)
2824 (window-normal-size sub horizontal)))))
2825 ((> delta 0)
2826 ;; When enlarging store the total/normal size factor only
2827 (set-window-new-normal
2829 (- (/ (float (window-size sub horizontal t))
2830 parent-total)
2831 (window-normal-size sub horizontal)))))
2833 (setq sub (window-left sub)))
2835 (cond
2836 ((< delta 0)
2837 ;; Shrink windows by delta.
2838 (setq best-window t)
2839 (while (and best-window (not (zerop delta)))
2840 (setq sub last)
2841 (setq best-window nil)
2842 (setq best-value most-negative-fixnum)
2843 (while sub
2844 (when (and (consp (window-new-normal sub))
2845 (not (<= (car (window-new-normal sub)) 0))
2846 (> (cdr (window-new-normal sub)) best-value))
2847 (setq best-window sub)
2848 (setq best-value (cdr (window-new-normal sub))))
2850 (setq sub (window-left sub)))
2852 (when best-window
2853 (setq best-delta (min (car (window-new-normal best-window))
2854 char-size (- delta)))
2855 (setq delta (+ delta best-delta))
2856 (set-window-new-pixel best-window (- best-delta) t)
2857 (set-window-new-normal
2858 best-window
2859 (if (= (car (window-new-normal best-window)) best-delta)
2860 'skip ; We can't shrink best-window any further.
2861 (cons (- (car (window-new-normal best-window)) best-delta)
2862 (- (/ (float (window-new-pixel best-window))
2863 parent-total)
2864 (window-normal-size best-window horizontal))))))))
2865 ((> delta 0)
2866 ;; Enlarge windows by delta.
2867 (setq best-window t)
2868 (while (and best-window (not (zerop delta)))
2869 (setq sub last)
2870 (setq best-window nil)
2871 (setq best-value most-positive-fixnum)
2872 (while sub
2873 (when (and (numberp (window-new-normal sub))
2874 (< (window-new-normal sub) best-value))
2875 (setq best-window sub)
2876 (setq best-value (window-new-normal sub)))
2878 (setq sub (window-left sub)))
2880 (when best-window
2881 (setq best-delta (min delta char-size))
2882 (setq delta (- delta best-delta))
2883 (set-window-new-pixel best-window best-delta t)
2884 (set-window-new-normal
2885 best-window
2886 (- (/ (float (window-new-pixel best-window))
2887 parent-total)
2888 (window-normal-size best-window horizontal)))))))
2890 (when best-window
2891 (setq sub last)
2892 (while sub
2893 (when (or (consp (window-new-normal sub))
2894 (numberp (window-new-normal sub)))
2895 ;; Reset new normal size fields so `window-resize-apply'
2896 ;; won't use them to apply new sizes.
2897 (set-window-new-normal sub))
2899 (unless (eq (window-new-normal sub) 'ignore)
2900 ;; Resize this window's child windows (back-engineering
2901 ;; delta from sub's old and new total sizes).
2902 (let ((delta (- (window-new-pixel sub)
2903 (window-size sub horizontal t))))
2904 (unless (and (zerop delta) (not trail))
2905 ;; For the TRAIL non-nil case we have to resize SUB
2906 ;; recursively even if it's size does not change.
2907 (window--resize-this-window
2908 sub delta horizontal ignore nil trail edge))))
2909 (setq sub (window-left sub)))))))
2911 (defun window--resize-siblings (window delta &optional horizontal ignore trail edge char-size)
2912 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
2913 Optional argument HORIZONTAL non-nil means resize other windows
2914 when WINDOW is resized horizontally by DELTA pixels. WINDOW
2915 itself is not resized by this function.
2917 The optional argument IGNORE has the same meaning as for
2918 `window-resizable'.
2920 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2921 of windows that shall be resized. If TRAIL equals `before',
2922 resize only windows on the left or above EDGE. If TRAIL equals
2923 `after', resize only windows on the right or below EDGE. Also,
2924 preferably only resize windows adjacent to EDGE."
2925 (when (window-parent window)
2926 (let* ((parent (window-parent window))
2927 (sub (window-child parent)))
2928 (if (window-combined-p sub horizontal)
2929 ;; In an iso-combination try to extract DELTA from WINDOW's
2930 ;; siblings.
2931 (let ((skip (eq trail 'after))
2932 this-delta other-delta)
2933 ;; Decide which windows shall be left alone.
2934 (while sub
2935 (cond
2936 ((eq sub window)
2937 ;; Make sure WINDOW is left alone when
2938 ;; resizing its siblings.
2939 (set-window-new-normal sub 'ignore)
2940 (setq skip (eq trail 'before)))
2941 (skip
2942 ;; Make sure this sibling is left alone when
2943 ;; resizing its siblings.
2944 (set-window-new-normal sub 'ignore))
2945 ((not (window-size-fixed-p sub horizontal ignore))
2946 ;; Set this-delta to t to signal that we found a sibling
2947 ;; of WINDOW whose size is not fixed.
2948 (setq this-delta t)))
2950 (setq sub (window-right sub)))
2952 ;; Set this-delta to what we can get from WINDOW's siblings.
2953 (if (= (- delta) (window-size window horizontal t))
2954 ;; A deletion, presumably. We must handle this case
2955 ;; specially since `window--resizable' can't be used.
2956 (if this-delta
2957 ;; There's at least one resizable sibling we can
2958 ;; give WINDOW's size to.
2959 (setq this-delta delta)
2960 ;; No resizable sibling exists.
2961 (setq this-delta 0))
2962 ;; Any other form of resizing.
2963 (setq this-delta
2964 (window--resizable
2965 window delta horizontal ignore trail t nil t)))
2967 ;; Set other-delta to what we still have to get from
2968 ;; ancestor windows of parent.
2969 (setq other-delta (- delta this-delta))
2970 (unless (zerop other-delta)
2971 ;; Unless we got everything from WINDOW's siblings, PARENT
2972 ;; must be resized by other-delta lines or columns.
2973 (set-window-new-pixel parent other-delta 'add))
2975 (if (zerop this-delta)
2976 ;; We haven't got anything from WINDOW's siblings but we
2977 ;; must update the normal sizes to respect other-delta.
2978 (window--resize-child-windows-normal
2979 parent horizontal window this-delta trail other-delta)
2980 ;; We did get something from WINDOW's siblings which means
2981 ;; we have to resize their child windows.
2982 (unless (eq (window--resize-child-windows
2983 parent (- this-delta) horizontal
2984 window ignore trail edge char-size)
2985 ;; If `window--resize-child-windows' returns
2986 ;; 'normalized, this means it has set the
2987 ;; normal sizes already.
2988 'normalized)
2989 ;; Set the normal sizes.
2990 (window--resize-child-windows-normal
2991 parent horizontal window this-delta trail other-delta))
2992 ;; Set DELTA to what we still have to get from ancestor
2993 ;; windows.
2994 (setq delta other-delta)))
2996 ;; In an ortho-combination all siblings of WINDOW must be
2997 ;; resized by DELTA.
2998 (set-window-new-pixel parent delta 'add)
2999 (while sub
3000 (unless (eq sub window)
3001 (window--resize-this-window
3002 sub delta horizontal ignore t))
3003 (setq sub (window-right sub))))
3005 (unless (zerop delta)
3006 ;; "Go up."
3007 (window--resize-siblings
3008 parent delta horizontal ignore trail edge char-size)))))
3010 (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge char-size)
3011 "Resize WINDOW vertically by DELTA pixels.
3012 Optional argument HORIZONTAL non-nil means resize WINDOW
3013 horizontally by DELTA pixels.
3015 The optional argument IGNORE has the same meaning as for
3016 `window-resizable'. Optional argument ADD non-nil means add
3017 DELTA to the new total size of WINDOW.
3019 Optional arguments TRAIL and EDGE, when non-nil, refine the set
3020 of windows that shall be resized. If TRAIL equals `before',
3021 resize only windows on the left or above EDGE. If TRAIL equals
3022 `after', resize only windows on the right or below EDGE. Also,
3023 preferably only resize windows adjacent to EDGE.
3025 If the optional argument CHAR-SIZE is a positive integer, it specifies
3026 the number of pixels by which windows are incrementally resized.
3027 If CHAR-SIZE is nil, this means to use the value of
3028 `frame-char-height' or `frame-char-width' of WINDOW's frame.
3030 This function recursively resizes WINDOW's child windows to fit the
3031 new size. Make sure that WINDOW is `window--resizable' before
3032 calling this function. Note that this function does not resize
3033 siblings of WINDOW or WINDOW's parent window. You have to
3034 eventually call `window-resize-apply' in order to make resizing
3035 actually take effect."
3036 (when add
3037 ;; Add DELTA to the new total size of WINDOW.
3038 (set-window-new-pixel window delta t))
3040 (let ((sub (window-child window)))
3041 (cond
3042 ((not sub))
3043 ((window-combined-p sub horizontal)
3044 ;; In an iso-combination resize child windows according to their
3045 ;; normal sizes.
3046 (window--resize-child-windows
3047 window delta horizontal nil ignore trail edge char-size))
3048 ;; In an ortho-combination resize each child window by DELTA.
3050 (while sub
3051 (window--resize-this-window
3052 sub delta horizontal ignore t trail edge char-size)
3053 (setq sub (window-right sub)))))))
3055 (defun window--resize-root-window (window delta horizontal ignore pixelwise)
3056 "Resize root window WINDOW vertically by DELTA lines.
3057 HORIZONTAL non-nil means resize root window WINDOW horizontally
3058 by DELTA columns.
3060 IGNORE non-nil means ignore any restrictions imposed by fixed
3061 size windows, `window-min-height' or `window-min-width' settings.
3063 This function is only called by the frame resizing routines. It
3064 resizes windows proportionally and never deletes any windows."
3065 (when (and (windowp window) (numberp delta))
3066 (let ((pixel-delta
3067 (if pixelwise
3068 delta
3069 (window--size-to-pixel window delta horizontal))))
3070 (when (window-sizable-p window pixel-delta horizontal ignore t)
3071 (window--resize-reset (window-frame window) horizontal)
3072 (window--resize-this-window
3073 window pixel-delta horizontal ignore t)))))
3075 (defun window--resize-root-window-vertically (window delta pixelwise)
3076 "Resize root window WINDOW vertically by DELTA lines.
3077 If DELTA is less than zero and we can't shrink WINDOW by DELTA
3078 lines, shrink it as much as possible. If DELTA is greater than
3079 zero, this function can resize fixed-size windows in order to
3080 recover the necessary lines. Return the number of lines that
3081 were recovered.
3083 Third argument PIXELWISE non-nil means to interpret DELTA as
3084 pixels and return the number of pixels that were recovered.
3086 This function is called by the minibuffer window resizing
3087 routines."
3088 (let* ((frame (window-frame window))
3089 (pixel-delta
3090 (cond
3091 (pixelwise
3092 delta)
3093 ((numberp delta)
3094 (* (frame-char-height frame) delta))
3095 (t 0)))
3096 ignore)
3097 (cond
3098 ((zerop pixel-delta))
3099 ((< pixel-delta 0)
3100 (setq pixel-delta (window-sizable window pixel-delta nil nil pixelwise))
3101 (window--resize-reset frame)
3102 ;; When shrinking the root window, emulate an edge drag in order
3103 ;; to not resize other windows if we can avoid it (Bug#12419).
3104 (window--resize-this-window
3105 window pixel-delta nil ignore t 'before
3106 (+ (window-pixel-top window) (window-pixel-height window)))
3107 ;; Don't record new normal sizes to make sure that shrinking back
3108 ;; proportionally works as intended.
3109 (walk-window-tree
3110 (lambda (window) (set-window-new-normal window 'ignore)) frame t))
3111 ((> pixel-delta 0)
3112 (window--resize-reset frame)
3113 (unless (window-sizable window pixel-delta nil nil pixelwise)
3114 (setq ignore t))
3115 ;; When growing the root window, resize proportionally. This
3116 ;; should give windows back their original sizes (hopefully).
3117 (window--resize-this-window
3118 window pixel-delta nil ignore t)))
3119 ;; Return the possibly adjusted DELTA.
3120 (if pixelwise
3121 pixel-delta
3122 (/ pixel-delta (frame-char-height frame)))))
3124 (defun window--sanitize-window-sizes (frame horizontal)
3125 "Assert that all windows on FRAME are large enough.
3126 If necessary and possible, make sure that every window on frame
3127 FRAME has its minimum height. Optional argument HORIZONTAL
3128 non-nil means to make sure that every window on frame FRAME has
3129 its minimum width. The minimum height/width of a window is the
3130 respective value returned by `window-min-size' for that window.
3132 Return t if all windows were resized appropriately. Return nil
3133 if at least one window could not be resized as requested, which
3134 may happen when the FRAME is not large enough to accommodate it."
3135 (let ((value t))
3136 (walk-window-tree
3137 (lambda (window)
3138 (let ((delta (- (window-min-size window horizontal nil t)
3139 (window-size window horizontal t))))
3140 (when (> delta 0)
3141 (if (window-resizable-p window delta horizontal nil t)
3142 (window-resize window delta horizontal nil t)
3143 (setq value nil))))))
3144 value))
3146 (defun adjust-window-trailing-edge (window delta &optional horizontal pixelwise)
3147 "Move WINDOW's bottom edge by DELTA lines.
3148 Optional argument HORIZONTAL non-nil means move WINDOW's right
3149 edge by DELTA columns. WINDOW must be a valid window and
3150 defaults to the selected one.
3152 Optional argument PIXELWISE non-nil means interpret DELTA as
3153 number of pixels.
3155 If DELTA is greater than zero, move the edge downwards or to the
3156 right. If DELTA is less than zero, move the edge upwards or to
3157 the left. If the edge can't be moved by DELTA lines or columns,
3158 move it as far as possible in the desired direction."
3159 (setq window (window-normalize-window window))
3160 (let* ((frame (window-frame window))
3161 (minibuffer-window (minibuffer-window frame))
3162 (right window)
3163 left first-left first-right this-delta min-delta max-delta ignore)
3165 (unless pixelwise
3166 (setq pixelwise t)
3167 (setq delta (* delta (frame-char-size window horizontal))))
3169 ;; Find the edge we want to move.
3170 (while (and (or (not (window-combined-p right horizontal))
3171 (not (window-right right)))
3172 (setq right (window-parent right))))
3173 (cond
3174 ((and (not right) (not horizontal)
3175 ;; Resize the minibuffer window if it's on the same frame as
3176 ;; and immediately below WINDOW and it's either active or
3177 ;; `resize-mini-windows' is nil.
3178 (eq (window-frame minibuffer-window) frame)
3179 (= (nth 1 (window-pixel-edges minibuffer-window))
3180 (nth 3 (window-pixel-edges window)))
3181 (or (not resize-mini-windows)
3182 (eq minibuffer-window (active-minibuffer-window))))
3183 (window--resize-mini-window minibuffer-window (- delta)))
3184 ((or (not (setq left right)) (not (setq right (window-right right))))
3185 (if horizontal
3186 (user-error "No window on the right of this one")
3187 (user-error "No window below this one")))
3189 ;; Set LEFT to the first resizable window on the left. This step is
3190 ;; needed to handle fixed-size windows.
3191 (setq first-left left)
3192 (while (and left
3193 (or (window-size-fixed-p left horizontal)
3194 (and (< delta 0)
3195 (<= (window-size left horizontal t)
3196 (window-min-size left horizontal nil t)))))
3197 (setq left
3198 (or (window-left left)
3199 (progn
3200 (while (and (setq left (window-parent left))
3201 (not (window-combined-p left horizontal))))
3202 (window-left left)))))
3203 (unless left
3204 ;; We have to resize a size-preserved window. Start again with
3205 ;; the window initially on the left.
3206 (setq ignore 'preserved)
3207 (setq left first-left)
3208 (while (and left
3209 (or (window-size-fixed-p left horizontal 'preserved)
3210 (<= (window-size left horizontal t)
3211 (window-min-size left horizontal 'preserved t))))
3212 (setq left
3213 (or (window-left left)
3214 (progn
3215 (while (and (setq left (window-parent left))
3216 (not (window-combined-p left horizontal))))
3217 (window-left left)))))
3219 (unless left
3220 (if horizontal
3221 (user-error "No resizable window on the left of this one")
3222 (user-error "No resizable window above this one"))))
3224 ;; Set RIGHT to the first resizable window on the right. This step
3225 ;; is needed to handle fixed-size windows.
3226 (setq first-right right)
3227 (while (and right
3228 (or (window-size-fixed-p right horizontal)
3229 (and (> delta 0)
3230 (<= (window-size right horizontal t)
3231 (window-min-size right horizontal 'preserved t)))))
3232 (setq right
3233 (or (window-right right)
3234 (progn
3235 (while (and (setq right (window-parent right))
3236 (not (window-combined-p right horizontal))))
3237 (window-right right)))))
3238 (unless right
3239 ;; We have to resize a size-preserved window. Start again with
3240 ;; the window initially on the right.
3241 (setq ignore 'preserved)
3242 (setq right first-right)
3243 (while (and right
3244 (or (window-size-fixed-p right horizontal 'preserved))
3245 (<= (window-size right horizontal t)
3246 (window-min-size right horizontal 'preserved t)))
3247 (setq right
3248 (or (window-right right)
3249 (progn
3250 (while (and (setq right (window-parent right))
3251 (not (window-combined-p right horizontal))))
3252 (window-right right)))))
3253 (unless right
3254 (if horizontal
3255 (user-error "No resizable window on the right of this one")
3256 (user-error "No resizable window below this one"))))
3258 ;; LEFT and RIGHT (which might be both internal windows) are now the
3259 ;; two windows we want to resize.
3260 (cond
3261 ((> delta 0)
3262 (setq max-delta
3263 (window--max-delta-1
3264 left 0 horizontal ignore 'after nil pixelwise))
3265 (setq min-delta
3266 (window--min-delta-1
3267 right (- delta) horizontal ignore 'before nil pixelwise))
3268 (when (or (< max-delta delta) (> min-delta (- delta)))
3269 ;; We can't get the whole DELTA - move as far as possible.
3270 (setq delta (min max-delta (- min-delta))))
3271 (unless (zerop delta)
3272 ;; Start resizing.
3273 (window--resize-reset frame horizontal)
3274 ;; Try to enlarge LEFT first.
3275 (setq this-delta (window--resizable
3276 left delta horizontal ignore 'after nil nil pixelwise))
3277 (unless (zerop this-delta)
3278 (window--resize-this-window
3279 left this-delta horizontal ignore t 'before
3280 (if horizontal
3281 (+ (window-pixel-left left) (window-pixel-width left))
3282 (+ (window-pixel-top left) (window-pixel-height left)))))
3283 ;; Shrink windows on right of LEFT.
3284 (window--resize-siblings
3285 left delta horizontal ignore 'after
3286 (if horizontal
3287 (window-pixel-left right)
3288 (window-pixel-top right)))))
3289 ((< delta 0)
3290 (setq max-delta
3291 (window--max-delta-1
3292 right 0 horizontal ignore 'before nil pixelwise))
3293 (setq min-delta
3294 (window--min-delta-1
3295 left delta horizontal ignore 'after nil pixelwise))
3296 (when (or (< max-delta (- delta)) (> min-delta delta))
3297 ;; We can't get the whole DELTA - move as far as possible.
3298 (setq delta (max (- max-delta) min-delta)))
3299 (unless (zerop delta)
3300 ;; Start resizing.
3301 (window--resize-reset frame horizontal)
3302 ;; Try to enlarge RIGHT.
3303 (setq this-delta
3304 (window--resizable
3305 right (- delta) horizontal ignore 'before nil nil pixelwise))
3306 (unless (zerop this-delta)
3307 (window--resize-this-window
3308 right this-delta horizontal ignore t 'after
3309 (if horizontal
3310 (window-pixel-left right)
3311 (window-pixel-top right))))
3312 ;; Shrink windows on left of RIGHT.
3313 (window--resize-siblings
3314 right (- delta) horizontal ignore 'before
3315 (if horizontal
3316 (+ (window-pixel-left left) (window-pixel-width left))
3317 (+ (window-pixel-top left) (window-pixel-height left)))))))
3318 (unless (zerop delta)
3319 ;; Don't report an error in the standard case.
3320 (when (window--resize-apply-p frame horizontal)
3321 (if (window-resize-apply frame horizontal)
3322 (progn
3323 (window--pixel-to-total frame horizontal)
3324 (run-window-configuration-change-hook frame))
3325 ;; But do report an error if applying the changes fails.
3326 (error "Failed adjusting window %s" window))))))))
3328 (defun enlarge-window (delta &optional horizontal)
3329 "Make the selected window DELTA lines taller.
3330 Interactively, if no argument is given, make the selected window
3331 one line taller. If optional argument HORIZONTAL is non-nil,
3332 make selected window wider by DELTA columns. If DELTA is
3333 negative, shrink selected window by -DELTA lines or columns."
3334 (interactive "p")
3335 (let ((minibuffer-window (minibuffer-window)))
3336 (when (window-preserved-size nil horizontal)
3337 (window-preserve-size nil horizontal))
3338 (cond
3339 ((zerop delta))
3340 ((window-size-fixed-p nil horizontal)
3341 (error "Selected window has fixed size"))
3342 ((window-minibuffer-p)
3343 (if horizontal
3344 (error "Cannot resize minibuffer window horizontally")
3345 (window--resize-mini-window (selected-window) delta)))
3346 ((and (not horizontal)
3347 (window-full-height-p)
3348 (eq (window-frame minibuffer-window) (selected-frame))
3349 (not resize-mini-windows))
3350 ;; If the selected window is full height and `resize-mini-windows'
3351 ;; is nil, resize the minibuffer window.
3352 (window--resize-mini-window minibuffer-window (- delta)))
3353 ((window--resizable-p nil delta horizontal)
3354 (window-resize nil delta horizontal))
3356 (window-resize
3357 nil (if (> delta 0)
3358 (window-max-delta nil horizontal)
3359 (- (window-min-delta nil horizontal)))
3360 horizontal)))))
3362 (defun shrink-window (delta &optional horizontal)
3363 "Make the selected window DELTA lines smaller.
3364 Interactively, if no argument is given, make the selected window
3365 one line smaller. If optional argument HORIZONTAL is non-nil,
3366 make selected window narrower by DELTA columns. If DELTA is
3367 negative, enlarge selected window by -DELTA lines or columns.
3368 Also see the `window-min-height' variable."
3369 (interactive "p")
3370 (let ((minibuffer-window (minibuffer-window)))
3371 (when (window-preserved-size nil horizontal)
3372 (window-preserve-size nil horizontal))
3373 (cond
3374 ((zerop delta))
3375 ((window-size-fixed-p nil horizontal)
3376 (error "Selected window has fixed size"))
3377 ((window-minibuffer-p)
3378 (if horizontal
3379 (error "Cannot resize minibuffer window horizontally")
3380 (window--resize-mini-window (selected-window) (- delta))))
3381 ((and (not horizontal)
3382 (window-full-height-p)
3383 (eq (window-frame minibuffer-window) (selected-frame))
3384 (not resize-mini-windows))
3385 ;; If the selected window is full height and `resize-mini-windows'
3386 ;; is nil, resize the minibuffer window.
3387 (window--resize-mini-window minibuffer-window delta))
3388 ((window--resizable-p nil (- delta) horizontal)
3389 (window-resize nil (- delta) horizontal))
3391 (window-resize
3392 nil (if (> delta 0)
3393 (- (window-min-delta nil horizontal))
3394 (window-max-delta nil horizontal))
3395 horizontal)))))
3397 (defun maximize-window (&optional window)
3398 "Maximize WINDOW.
3399 Make WINDOW as large as possible without deleting any windows.
3400 WINDOW must be a valid window and defaults to the selected one.
3402 If the option `window-resize-pixelwise' is non-nil maximize
3403 WINDOW pixelwise."
3404 (interactive)
3405 (setq window (window-normalize-window window))
3406 (window-resize
3407 window (window-max-delta window nil nil nil nil nil window-resize-pixelwise)
3408 nil nil window-resize-pixelwise)
3409 (window-resize
3410 window (window-max-delta window t nil nil nil nil window-resize-pixelwise)
3411 t nil window-resize-pixelwise))
3413 (defun minimize-window (&optional window)
3414 "Minimize WINDOW.
3415 Make WINDOW as small as possible without deleting any windows.
3416 WINDOW must be a valid window and defaults to the selected one.
3418 If the option `window-resize-pixelwise' is non-nil minimize
3419 WINDOW pixelwise."
3420 (interactive)
3421 (setq window (window-normalize-window window))
3422 (window-resize
3423 window
3424 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise))
3425 nil nil window-resize-pixelwise)
3426 (window-resize
3427 window
3428 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise))
3429 t nil window-resize-pixelwise))
3431 ;;; Window edges
3432 (defun window-edges (&optional window body absolute pixelwise)
3433 "Return a list of the edge distances of WINDOW.
3434 WINDOW must be a valid window and defaults to the selected one.
3435 The list returned has the form (LEFT TOP RIGHT BOTTOM).
3437 If the optional argument BODY is nil, this means to return the
3438 edges corresponding to the total size of WINDOW. BODY non-nil
3439 means to return the edges of WINDOW's body (aka text area). If
3440 BODY is non-nil, WINDOW must specify a live window.
3442 Optional argument ABSOLUTE nil means to return edges relative to
3443 the position of WINDOW's native frame. ABSOLUTE non-nil means to
3444 return coordinates relative to the origin - the position (0, 0) -
3445 of FRAME's display. On non-graphical systems this argument has
3446 no effect.
3448 Optional argument PIXELWISE nil means to return the coordinates
3449 in terms of the canonical character width and height of WINDOW's
3450 frame, rounded if necessary. PIXELWISE non-nil means to return
3451 the coordinates in pixels where the values for RIGHT and BOTTOM
3452 are one more than the actual value of these edges. Note that if
3453 ABSOLUTE is non-nil, PIXELWISE is implicitly non-nil too."
3454 (let* ((window (window-normalize-window window body))
3455 (frame (window-frame window))
3456 (border-width (frame-border-width frame))
3457 (char-width (frame-char-width frame))
3458 (char-height (frame-char-height frame))
3459 (left (if pixelwise
3460 (+ (window-pixel-left window) border-width)
3461 (+ (window-left-column window)
3462 (/ border-width char-width))))
3463 (left-body
3464 (when body
3465 (+ (window-pixel-left window) border-width
3466 (if (eq (car (window-current-scroll-bars window)) 'left)
3467 (window-scroll-bar-width window)
3469 (nth 0 (window-fringes window))
3470 (* (or (nth 0 (window-margins window)) 0) char-width))))
3471 (top (if pixelwise
3472 (+ (window-pixel-top window) border-width)
3473 (+ (window-top-line window)
3474 (/ border-width char-height))))
3475 (top-body
3476 (when body
3477 (+ (window-pixel-top window) border-width
3478 (window-header-line-height window))))
3479 (right (+ left (if pixelwise
3480 (window-pixel-width window)
3481 (window-total-width window))))
3482 (right-body (and body (+ left-body (window-body-width window t))))
3483 (bottom (+ top (if pixelwise
3484 (window-pixel-height window)
3485 (window-total-height window))))
3486 (bottom-body (and body (+ top-body (window-body-height window t))))
3487 left-off right-off)
3488 (if absolute
3489 (let* ((native-edges (frame-edges frame 'native-edges))
3490 (left-off (nth 0 native-edges))
3491 (top-off (nth 1 native-edges)))
3492 (if body
3493 (list (+ left-body left-off) (+ top-body top-off)
3494 (+ right-body left-off) (+ bottom-body top-off))
3495 (list (+ left left-off) (+ top top-off)
3496 (+ right left-off) (+ bottom top-off))))
3497 (if body
3498 (if pixelwise
3499 (list left-body top-body right-body bottom-body)
3500 (list (/ left-body char-width) (/ top-body char-height)
3501 ;; Round up.
3502 (/ (+ right-body char-width -1) char-width)
3503 (/ (+ bottom-body char-height -1) char-height)))
3504 (list left top right bottom)))))
3506 (defun window-body-edges (&optional window)
3507 "Return a list of the edge coordinates of WINDOW's body.
3508 The return value is that of `window-edges' called with argument
3509 BODY non-nil."
3510 (window-edges window t))
3511 (defalias 'window-inside-edges 'window-body-edges)
3513 (defun window-pixel-edges (&optional window)
3514 "Return a list of the edge pixel coordinates of WINDOW.
3515 The return value is that of `window-edges' called with argument
3516 PIXELWISE non-nil."
3517 (window-edges window nil nil t))
3519 (defun window-body-pixel-edges (&optional window)
3520 "Return a list of the edge pixel coordinates of WINDOW's body.
3521 The return value is that of `window-edges' called with arguments
3522 BODY and PIXELWISE non-nil."
3523 (window-edges window t nil t))
3524 (defalias 'window-inside-pixel-edges 'window-body-pixel-edges)
3526 (defun window-absolute-pixel-edges (&optional window)
3527 "Return a list of the edge pixel coordinates of WINDOW.
3528 The return value is that of `window-edges' called with argument
3529 ABSOLUTE non-nil."
3530 (window-edges window nil t t))
3532 (defun window-absolute-body-pixel-edges (&optional window)
3533 "Return a list of the edge pixel coordinates of WINDOW's text area.
3534 The return value is that of `window-edges' called with arguments
3535 BODY and ABSOLUTE non-nil."
3536 (window-edges window t t t))
3537 (defalias 'window-inside-absolute-pixel-edges 'window-absolute-body-pixel-edges)
3539 (defun window-absolute-pixel-position (&optional position window)
3540 "Return display coordinates of POSITION in WINDOW.
3541 If the buffer position POSITION is visible in window WINDOW,
3542 return the display coordinates of the upper/left corner of the
3543 glyph at POSITION. The return value is a cons of the X- and
3544 Y-coordinates of that corner, relative to an origin at (0, 0) of
3545 WINDOW's display. Return nil if POSITION is not visible in
3546 WINDOW.
3548 WINDOW must be a live window and defaults to the selected window.
3549 POSITION defaults to the value of `window-point' of WINDOW."
3550 (let* ((window (window-normalize-window window t))
3551 (pos-in-window
3552 (pos-visible-in-window-p
3553 (or position (window-point window)) window t)))
3554 (when pos-in-window
3555 (let ((edges (window-absolute-body-pixel-edges window)))
3556 (cons (+ (nth 0 edges) (nth 0 pos-in-window))
3557 (+ (nth 1 edges) (nth 1 pos-in-window)))))))
3559 (defun frame-root-window-p (window)
3560 "Return non-nil if WINDOW is the root window of its frame."
3561 (eq window (frame-root-window window)))
3563 (defun window--subtree (window &optional next)
3564 "Return window subtree rooted at WINDOW.
3565 Optional argument NEXT non-nil means include WINDOW's right
3566 siblings in the return value.
3568 See the documentation of `window-tree' for a description of the
3569 return value."
3570 (let (list)
3571 (while window
3572 (setq list
3573 (cons
3574 (cond
3575 ((window-top-child window)
3576 (cons t (cons (window-edges window)
3577 (window--subtree (window-top-child window) t))))
3578 ((window-left-child window)
3579 (cons nil (cons (window-edges window)
3580 (window--subtree (window-left-child window) t))))
3581 (t window))
3582 list))
3583 (setq window (when next (window-next-sibling window))))
3584 (nreverse list)))
3586 (defun window-tree (&optional frame)
3587 "Return the window tree of frame FRAME.
3588 FRAME must be a live frame and defaults to the selected frame.
3589 The return value is a list of the form (ROOT MINI), where ROOT
3590 represents the window tree of the frame's root window, and MINI
3591 is the frame's minibuffer window.
3593 If the root window is not split, ROOT is the root window itself.
3594 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3595 for a horizontal split, and t for a vertical split. EDGES gives
3596 the combined size and position of the child windows in the split,
3597 and the rest of the elements are the child windows in the split.
3598 Each of the child windows may again be a window or a list
3599 representing a window split, and so on. EDGES is a list (LEFT
3600 TOP RIGHT BOTTOM) as returned by `window-edges'."
3601 (setq frame (window-normalize-frame frame))
3602 (window--subtree (frame-root-window frame) t))
3604 (defun other-window (count &optional all-frames)
3605 "Select another window in cyclic ordering of windows.
3606 COUNT specifies the number of windows to skip, starting with the
3607 selected window, before making the selection. If COUNT is
3608 positive, skip COUNT windows forwards. If COUNT is negative,
3609 skip -COUNT windows backwards. COUNT zero means do not skip any
3610 window, so select the selected window. In an interactive call,
3611 COUNT is the numeric prefix argument. Return nil.
3613 If the `other-window' parameter of the selected window is a
3614 function and `ignore-window-parameters' is nil, call that
3615 function with the arguments COUNT and ALL-FRAMES.
3617 This function does not select a window whose `no-other-window'
3618 window parameter is non-nil.
3620 This function uses `next-window' for finding the window to
3621 select. The argument ALL-FRAMES has the same meaning as in
3622 `next-window', but the MINIBUF argument of `next-window' is
3623 always effectively nil."
3624 (interactive "p")
3625 (let* ((window (selected-window))
3626 (function (and (not ignore-window-parameters)
3627 (window-parameter window 'other-window)))
3628 old-window old-count)
3629 (if (functionp function)
3630 (funcall function count all-frames)
3631 ;; `next-window' and `previous-window' may return a window we are
3632 ;; not allowed to select. Hence we need an exit strategy in case
3633 ;; all windows are non-selectable.
3634 (catch 'exit
3635 (while (> count 0)
3636 (setq window (next-window window nil all-frames))
3637 (cond
3638 ((eq window old-window)
3639 (when (= count old-count)
3640 ;; Keep out of infinite loops. When COUNT has not changed
3641 ;; since we last looked at `window' we're probably in one.
3642 (throw 'exit nil)))
3643 ((window-parameter window 'no-other-window)
3644 (unless old-window
3645 ;; The first non-selectable window `next-window' got us:
3646 ;; Remember it and the current value of COUNT.
3647 (setq old-window window)
3648 (setq old-count count)))
3650 (setq count (1- count)))))
3651 (while (< count 0)
3652 (setq window (previous-window window nil all-frames))
3653 (cond
3654 ((eq window old-window)
3655 (when (= count old-count)
3656 ;; Keep out of infinite loops. When COUNT has not changed
3657 ;; since we last looked at `window' we're probably in one.
3658 (throw 'exit nil)))
3659 ((window-parameter window 'no-other-window)
3660 (unless old-window
3661 ;; The first non-selectable window `previous-window' got
3662 ;; us: Remember it and the current value of COUNT.
3663 (setq old-window window)
3664 (setq old-count count)))
3666 (setq count (1+ count)))))
3668 (select-window window)
3669 ;; Always return nil.
3670 nil))))
3672 ;; This should probably return non-nil when the selected window is part
3673 ;; of an atomic window whose root is the frame's root window.
3674 (defun one-window-p (&optional nomini all-frames)
3675 "Return non-nil if the selected window is the only window.
3676 Optional arg NOMINI non-nil means don't count the minibuffer
3677 even if it is active. Otherwise, the minibuffer is counted
3678 when it is active.
3680 Optional argument ALL-FRAMES specifies the set of frames to
3681 consider, see also `next-window'. ALL-FRAMES nil or omitted
3682 means consider windows on the selected frame only, plus the
3683 minibuffer window if specified by the NOMINI argument. If the
3684 minibuffer counts, consider all windows on all frames that share
3685 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3686 with a special meaning are:
3688 - t means consider all windows on all existing frames.
3690 - `visible' means consider all windows on all visible frames on
3691 the current terminal.
3693 - 0 (the number zero) means consider all windows on all visible
3694 and iconified frames on the current terminal.
3696 - A frame means consider all windows on that frame only.
3698 Anything else means consider all windows on the selected frame
3699 and no others."
3700 (let ((base-window (selected-window)))
3701 (if (and nomini (eq base-window (minibuffer-window)))
3702 (setq base-window (next-window base-window)))
3703 (eq base-window
3704 (next-window base-window (if nomini 'arg) all-frames))))
3706 ;;; Deleting windows.
3707 (defun window-deletable-p (&optional window)
3708 "Return t if WINDOW can be safely deleted from its frame.
3709 WINDOW must be a valid window and defaults to the selected one.
3710 Return 'frame if deleting WINDOW should also delete its frame."
3711 (setq window (window-normalize-window window))
3713 (unless (or ignore-window-parameters
3714 (eq (window-parameter window 'delete-window) t))
3715 ;; Handle atomicity.
3716 (when (window-parameter window 'window-atom)
3717 (setq window (window-atom-root window))))
3719 (let ((frame (window-frame window)))
3720 (cond
3721 ((frame-root-window-p window)
3722 ;; WINDOW's frame can be deleted only if there are other frames
3723 ;; on the same terminal, and it does not contain the active
3724 ;; minibuffer.
3725 (unless (or (eq frame (next-frame frame 0))
3726 ;; We can delete our frame only if no other frame
3727 ;; currently uses our minibuffer window.
3728 (catch 'other
3729 (dolist (other (frame-list))
3730 (when (and (not (eq other frame))
3731 (eq (window-frame (minibuffer-window other))
3732 frame))
3733 (throw 'other t))))
3734 (let ((minibuf (active-minibuffer-window)))
3735 (and minibuf (eq frame (window-frame minibuf)))))
3736 'frame))
3737 ((or ignore-window-parameters
3738 (not (eq window (window--major-non-side-window frame))))
3739 ;; WINDOW can be deleted unless it is the major non-side window of
3740 ;; its frame.
3741 t))))
3743 (defun window--in-subtree-p (window root)
3744 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
3745 (or (eq window root)
3746 (let ((parent (window-parent window)))
3747 (catch 'done
3748 (while parent
3749 (if (eq parent root)
3750 (throw 'done t)
3751 (setq parent (window-parent parent))))))))
3753 (defun delete-window (&optional window)
3754 "Delete WINDOW.
3755 WINDOW must be a valid window and defaults to the selected one.
3756 Return nil.
3758 If the variable `ignore-window-parameters' is non-nil or the
3759 `delete-window' parameter of WINDOW equals t, do not process any
3760 parameters of WINDOW. Otherwise, if the `delete-window'
3761 parameter of WINDOW specifies a function, call that function with
3762 WINDOW as its sole argument and return the value returned by that
3763 function.
3765 Otherwise, if WINDOW is part of an atomic window, call
3766 `delete-window' with the root of the atomic window as its
3767 argument. Signal an error if WINDOW is either the only window on
3768 its frame, the last non-side window, or part of an atomic window
3769 that is its frame's root window."
3770 (interactive)
3771 (setq window (window-normalize-window window))
3772 (let* ((frame (window-frame window))
3773 (function (window-parameter window 'delete-window))
3774 (parent (window-parent window))
3775 atom-root)
3776 (window--check frame)
3777 (catch 'done
3778 ;; Handle window parameters.
3779 (cond
3780 ;; Ignore window parameters if `ignore-window-parameters' tells
3781 ;; us so or `delete-window' equals t.
3782 ((or ignore-window-parameters (eq function t)))
3783 ((functionp function)
3784 ;; The `delete-window' parameter specifies the function to call.
3785 ;; If that function is `ignore' nothing is done. It's up to the
3786 ;; function called here to avoid infinite recursion.
3787 (throw 'done (funcall function window)))
3788 ((and (window-parameter window 'window-atom)
3789 (setq atom-root (window-atom-root window))
3790 (not (eq atom-root window)))
3791 (if (eq atom-root (frame-root-window frame))
3792 (error "Root of atomic window is root window of its frame")
3793 (throw 'done (delete-window atom-root))))
3794 ((not parent)
3795 (error "Attempt to delete minibuffer or sole ordinary window"))
3796 ((eq window (window--major-non-side-window frame))
3797 (error "Attempt to delete last non-side window")))
3799 (let* ((horizontal (window-left-child parent))
3800 (size (window-size window horizontal t))
3801 (frame-selected
3802 (window--in-subtree-p (frame-selected-window frame) window))
3803 ;; Emacs 23 preferably gives WINDOW's space to its left
3804 ;; sibling.
3805 (sibling (or (window-left window) (window-right window))))
3806 (window--resize-reset frame horizontal)
3807 (cond
3808 ((and (not window-combination-resize)
3809 sibling (window-sizable-p sibling size horizontal nil t))
3810 ;; Resize WINDOW's sibling.
3811 (window--resize-this-window sibling size horizontal nil t)
3812 (set-window-new-normal
3813 sibling (+ (window-normal-size sibling horizontal)
3814 (window-normal-size window horizontal))))
3815 ((window--resizable-p window (- size) horizontal nil nil nil t t)
3816 ;; Can do without resizing fixed-size windows.
3817 (window--resize-siblings window (- size) horizontal))
3819 ;; Can't do without resizing fixed-size windows.
3820 (window--resize-siblings window (- size) horizontal t)))
3821 ;; Actually delete WINDOW.
3822 (delete-window-internal window)
3823 (window--pixel-to-total frame horizontal)
3824 (when (and frame-selected
3825 (window-parameter
3826 (frame-selected-window frame) 'no-other-window))
3827 ;; `delete-window-internal' has selected a window that should
3828 ;; not be selected, fix this here.
3829 (other-window -1 frame))
3830 (run-window-configuration-change-hook frame)
3831 (window--check frame)
3832 ;; Always return nil.
3833 nil))))
3835 (defun delete-other-windows (&optional window)
3836 "Make WINDOW fill its frame.
3837 WINDOW must be a valid window and defaults to the selected one.
3838 Return nil.
3840 If the variable `ignore-window-parameters' is non-nil or the
3841 `delete-other-windows' parameter of WINDOW equals t, do not
3842 process any parameters of WINDOW. Otherwise, if the
3843 `delete-other-windows' parameter of WINDOW specifies a function,
3844 call that function with WINDOW as its sole argument and return
3845 the value returned by that function.
3847 Otherwise, if WINDOW is part of an atomic window, call this
3848 function with the root of the atomic window as its argument. If
3849 WINDOW is a non-side window, make WINDOW the only non-side window
3850 on the frame. Side windows are not deleted. If WINDOW is a side
3851 window signal an error."
3852 (interactive)
3853 (setq window (window-normalize-window window))
3854 (let* ((frame (window-frame window))
3855 (function (window-parameter window 'delete-other-windows))
3856 (window-side (window-parameter window 'window-side))
3857 atom-root side-main)
3858 (window--check frame)
3859 (catch 'done
3860 (cond
3861 ;; Ignore window parameters if `ignore-window-parameters' is t or
3862 ;; `delete-other-windows' is t.
3863 ((or ignore-window-parameters (eq function t)))
3864 ((functionp function)
3865 ;; The `delete-other-windows' parameter specifies the function
3866 ;; to call. If the function is `ignore' no windows are deleted.
3867 ;; It's up to the function called to avoid infinite recursion.
3868 (throw 'done (funcall function window)))
3869 ((and (window-parameter window 'window-atom)
3870 (setq atom-root (window-atom-root window))
3871 (not (eq atom-root window)))
3872 (if (eq atom-root (frame-root-window frame))
3873 (error "Root of atomic window is root window of its frame")
3874 (throw 'done (delete-other-windows atom-root))))
3875 ((memq window-side window-sides)
3876 (error "Cannot make side window the only window"))
3877 ((and (window-minibuffer-p window)
3878 (not (eq window (frame-root-window window))))
3879 (error "Can't expand minibuffer to full frame")))
3881 ;; If WINDOW is the major non-side window, do nothing.
3882 (if (window-with-parameter 'window-side)
3883 (setq side-main (window--major-non-side-window frame))
3884 (setq side-main (frame-root-window frame)))
3885 (unless (eq window side-main)
3886 (delete-other-windows-internal window side-main)
3887 (run-window-configuration-change-hook frame)
3888 (window--check frame))
3889 ;; Always return nil.
3890 nil)))
3892 (defun delete-other-windows-vertically (&optional window)
3893 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
3894 This may be a useful alternative binding for \\[delete-other-windows]
3895 if you often split windows horizontally."
3896 (interactive)
3897 (let* ((window (or window (selected-window)))
3898 (edges (window-edges window))
3899 (w window) delenda)
3900 (while (not (eq (setq w (next-window w 1)) window))
3901 (let ((e (window-edges w)))
3902 (when (and (= (car e) (car edges))
3903 (= (nth 2 e) (nth 2 edges)))
3904 (push w delenda))))
3905 (mapc 'delete-window delenda)))
3907 ;;; Windows and buffers.
3909 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
3910 ;; for (1) determining which buffer to show in the window when its
3911 ;; buffer shall be buried or killed and (2) which buffer to show for
3912 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3914 ;; `prev-buffers' consists of <buffer, window-start, window-point>
3915 ;; triples. The entries on this list are ordered by the time their
3916 ;; buffer has been removed from the window, the most recently removed
3917 ;; buffer's entry being first. The window-start and window-point
3918 ;; components are `window-start' and `window-point' at the time the
3919 ;; buffer was removed from the window which implies that the entry must
3920 ;; be added when `set-window-buffer' removes the buffer from the window.
3922 ;; `next-buffers' is the list of buffers that have been replaced
3923 ;; recently by `switch-to-prev-buffer'. These buffers are the least
3924 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
3925 ;; candidates of `switch-to-next-buffer' to switch to. This list is
3926 ;; reset to nil by any action changing the window's buffer with the
3927 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
3928 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
3929 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
3931 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
3932 ;; if such a buffer was killed while the window was hidden within a
3933 ;; window configuration. Such killed buffers get removed whenever
3934 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
3936 ;; The following function is called by `set-window-buffer' _before_ it
3937 ;; replaces the buffer of the argument window with the new buffer.
3938 (defun record-window-buffer (&optional window)
3939 "Record WINDOW's buffer.
3940 WINDOW must be a live window and defaults to the selected one."
3941 (let* ((window (window-normalize-window window t))
3942 (buffer (window-buffer window))
3943 (entry (assq buffer (window-prev-buffers window))))
3944 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
3945 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3946 (set-window-next-buffers window nil)
3948 (when entry
3949 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
3950 (set-window-prev-buffers
3951 window (assq-delete-all buffer (window-prev-buffers window))))
3953 ;; Don't record insignificant buffers.
3954 (unless (eq (aref (buffer-name buffer) 0) ?\s)
3955 ;; Add an entry for buffer to WINDOW's previous buffers.
3956 (with-current-buffer buffer
3957 (let ((start (window-start window))
3958 (point (window-point window)))
3959 (setq entry
3960 (cons buffer
3961 (if entry
3962 ;; We have an entry, update marker positions.
3963 (list (set-marker (nth 1 entry) start)
3964 (set-marker (nth 2 entry) point))
3965 ;; Make new markers.
3966 (list (copy-marker start)
3967 (copy-marker
3968 ;; Preserve window-point-insertion-type
3969 ;; (Bug#12588).
3970 point window-point-insertion-type)))))
3971 (set-window-prev-buffers
3972 window (cons entry (window-prev-buffers window)))))
3974 (run-hooks 'buffer-list-update-hook))))
3976 (defun unrecord-window-buffer (&optional window buffer)
3977 "Unrecord BUFFER in WINDOW.
3978 WINDOW must be a live window and defaults to the selected one.
3979 BUFFER must be a live buffer and defaults to the buffer of
3980 WINDOW."
3981 (let* ((window (window-normalize-window window t))
3982 (buffer (or buffer (window-buffer window))))
3983 (set-window-prev-buffers
3984 window (assq-delete-all buffer (window-prev-buffers window)))
3985 (set-window-next-buffers
3986 window (delq buffer (window-next-buffers window)))))
3988 (defun set-window-buffer-start-and-point (window buffer &optional start point)
3989 "Set WINDOW's buffer to BUFFER.
3990 WINDOW must be a live window and defaults to the selected one.
3991 Optional argument START non-nil means set WINDOW's start position
3992 to START. Optional argument POINT non-nil means set WINDOW's
3993 point to POINT. If WINDOW is selected this also sets BUFFER's
3994 `point' to POINT. If WINDOW is selected and the buffer it showed
3995 before was current this also makes BUFFER the current buffer."
3996 (setq window (window-normalize-window window t))
3997 (let ((selected (eq window (selected-window)))
3998 (current (eq (window-buffer window) (current-buffer))))
3999 (set-window-buffer window buffer)
4000 (when (and selected current)
4001 (set-buffer buffer))
4002 (when start
4003 ;; Don't force window-start here (even if POINT is nil).
4004 (set-window-start window start t))
4005 (when point
4006 (set-window-point window point))))
4008 (defcustom switch-to-visible-buffer t
4009 "If non-nil, allow switching to an already visible buffer.
4010 If this variable is non-nil, `switch-to-prev-buffer' and
4011 `switch-to-next-buffer' may switch to an already visible buffer.
4012 If this variable is nil, `switch-to-prev-buffer' and
4013 `switch-to-next-buffer' always try to avoid switching to a buffer
4014 that is already visible in another window on the same frame."
4015 :type 'boolean
4016 :version "24.1"
4017 :group 'windows)
4019 (defun switch-to-prev-buffer (&optional window bury-or-kill)
4020 "In WINDOW switch to previous buffer.
4021 WINDOW must be a live window and defaults to the selected one.
4022 Return the buffer switched to, nil if no suitable buffer could be
4023 found.
4025 Optional argument BURY-OR-KILL non-nil means the buffer currently
4026 shown in WINDOW is about to be buried or killed and consequently
4027 shall not be switched to in future invocations of this command.
4029 As a special case, if BURY-OR-KILL equals `append', this means to
4030 move the buffer to the end of WINDOW's previous buffers list so a
4031 future invocation of `switch-to-prev-buffer' less likely switches
4032 to it."
4033 (interactive)
4034 (let* ((window (window-normalize-window window t))
4035 (frame (window-frame window))
4036 (old-buffer (window-buffer window))
4037 ;; Save this since it's destroyed by `set-window-buffer'.
4038 (next-buffers (window-next-buffers window))
4039 (pred (frame-parameter frame 'buffer-predicate))
4040 entry new-buffer killed-buffers visible)
4041 (when (window-minibuffer-p window)
4042 ;; Don't switch in minibuffer window.
4043 (unless (setq window (minibuffer-selected-window))
4044 (error "Window %s is a minibuffer window" window)))
4046 (when (window-dedicated-p window)
4047 ;; Don't switch in dedicated window.
4048 (error "Window %s is dedicated to buffer %s" window old-buffer))
4050 (catch 'found
4051 ;; Scan WINDOW's previous buffers first, skipping entries of next
4052 ;; buffers.
4053 (dolist (entry (window-prev-buffers window))
4054 (when (and (setq new-buffer (car entry))
4055 (or (buffer-live-p new-buffer)
4056 (not (setq killed-buffers
4057 (cons new-buffer killed-buffers))))
4058 (not (eq new-buffer old-buffer))
4059 (or (null pred) (funcall pred new-buffer))
4060 ;; When BURY-OR-KILL is nil, avoid switching to a
4061 ;; buffer in WINDOW's next buffers list.
4062 (or bury-or-kill (not (memq new-buffer next-buffers))))
4063 (if (and (not switch-to-visible-buffer)
4064 (get-buffer-window new-buffer frame))
4065 ;; Try to avoid showing a buffer visible in some other
4066 ;; window.
4067 (setq visible new-buffer)
4068 (set-window-buffer-start-and-point
4069 window new-buffer (nth 1 entry) (nth 2 entry))
4070 (throw 'found t))))
4071 ;; Scan reverted buffer list of WINDOW's frame next, skipping
4072 ;; entries of next buffers. Note that when we bury or kill a
4073 ;; buffer we don't reverse the global buffer list to avoid showing
4074 ;; a buried buffer instead. Otherwise, we must reverse the global
4075 ;; buffer list in order to make sure that switching to the
4076 ;; previous/next buffer traverse it in opposite directions.
4077 (dolist (buffer (if bury-or-kill
4078 (buffer-list frame)
4079 (nreverse (buffer-list frame))))
4080 (when (and (buffer-live-p buffer)
4081 (not (eq buffer old-buffer))
4082 (or (null pred) (funcall pred buffer))
4083 (not (eq (aref (buffer-name buffer) 0) ?\s))
4084 (or bury-or-kill (not (memq buffer next-buffers))))
4085 (if (and (not switch-to-visible-buffer)
4086 (get-buffer-window buffer frame))
4087 ;; Try to avoid showing a buffer visible in some other window.
4088 (unless visible
4089 (setq visible buffer))
4090 (setq new-buffer buffer)
4091 (set-window-buffer-start-and-point window new-buffer)
4092 (throw 'found t))))
4093 (unless bury-or-kill
4094 ;; Scan reverted next buffers last (must not use nreverse
4095 ;; here!).
4096 (dolist (buffer (reverse next-buffers))
4097 ;; Actually, buffer _must_ be live here since otherwise it
4098 ;; would have been caught in the scan of previous buffers.
4099 (when (and (or (buffer-live-p buffer)
4100 (not (setq killed-buffers
4101 (cons buffer killed-buffers))))
4102 (not (eq buffer old-buffer))
4103 (or (null pred) (funcall pred buffer))
4104 (setq entry (assq buffer (window-prev-buffers window))))
4105 (setq new-buffer buffer)
4106 (set-window-buffer-start-and-point
4107 window new-buffer (nth 1 entry) (nth 2 entry))
4108 (throw 'found t))))
4110 ;; Show a buffer visible in another window.
4111 (when visible
4112 (setq new-buffer visible)
4113 (set-window-buffer-start-and-point window new-buffer)))
4115 (if bury-or-kill
4116 (let ((entry (and (eq bury-or-kill 'append)
4117 (assq old-buffer (window-prev-buffers window)))))
4118 ;; Remove `old-buffer' from WINDOW's previous and (restored list
4119 ;; of) next buffers.
4120 (set-window-prev-buffers
4121 window (assq-delete-all old-buffer (window-prev-buffers window)))
4122 (set-window-next-buffers window (delq old-buffer next-buffers))
4123 (when entry
4124 ;; Append old-buffer's entry to list of WINDOW's previous
4125 ;; buffers so it's less likely to get switched to soon but
4126 ;; `display-buffer-in-previous-window' can nevertheless find
4127 ;; it.
4128 (set-window-prev-buffers
4129 window (append (window-prev-buffers window) (list entry)))))
4130 ;; Move `old-buffer' to head of WINDOW's restored list of next
4131 ;; buffers.
4132 (set-window-next-buffers
4133 window (cons old-buffer (delq old-buffer next-buffers))))
4135 ;; Remove killed buffers from WINDOW's previous and next buffers.
4136 (when killed-buffers
4137 (dolist (buffer killed-buffers)
4138 (set-window-prev-buffers
4139 window (assq-delete-all buffer (window-prev-buffers window)))
4140 (set-window-next-buffers
4141 window (delq buffer (window-next-buffers window)))))
4143 ;; Return new-buffer.
4144 new-buffer))
4146 (defun switch-to-next-buffer (&optional window)
4147 "In WINDOW switch to next buffer.
4148 WINDOW must be a live window and defaults to the selected one.
4149 Return the buffer switched to, nil if no suitable buffer could be
4150 found."
4151 (interactive)
4152 (let* ((window (window-normalize-window window t))
4153 (frame (window-frame window))
4154 (old-buffer (window-buffer window))
4155 (next-buffers (window-next-buffers window))
4156 (pred (frame-parameter frame 'buffer-predicate))
4157 new-buffer entry killed-buffers visible)
4158 (when (window-minibuffer-p window)
4159 ;; Don't switch in minibuffer window.
4160 (unless (setq window (minibuffer-selected-window))
4161 (error "Window %s is a minibuffer window" window)))
4163 (when (window-dedicated-p window)
4164 ;; Don't switch in dedicated window.
4165 (error "Window %s is dedicated to buffer %s" window old-buffer))
4167 (catch 'found
4168 ;; Scan WINDOW's next buffers first.
4169 (dolist (buffer next-buffers)
4170 (when (and (or (buffer-live-p buffer)
4171 (not (setq killed-buffers
4172 (cons buffer killed-buffers))))
4173 (not (eq buffer old-buffer))
4174 (or (null pred) (funcall pred buffer))
4175 (setq entry (assq buffer (window-prev-buffers window))))
4176 (setq new-buffer buffer)
4177 (set-window-buffer-start-and-point
4178 window new-buffer (nth 1 entry) (nth 2 entry))
4179 (throw 'found t)))
4180 ;; Scan the buffer list of WINDOW's frame next, skipping previous
4181 ;; buffers entries.
4182 (dolist (buffer (buffer-list frame))
4183 (when (and (buffer-live-p buffer)
4184 (not (eq buffer old-buffer))
4185 (or (null pred) (funcall pred buffer))
4186 (not (eq (aref (buffer-name buffer) 0) ?\s))
4187 (not (assq buffer (window-prev-buffers window))))
4188 (if (and (not switch-to-visible-buffer)
4189 (get-buffer-window buffer frame))
4190 ;; Try to avoid showing a buffer visible in some other window.
4191 (setq visible buffer)
4192 (setq new-buffer buffer)
4193 (set-window-buffer-start-and-point window new-buffer)
4194 (throw 'found t))))
4195 ;; Scan WINDOW's reverted previous buffers last (must not use
4196 ;; nreverse here!)
4197 (dolist (entry (reverse (window-prev-buffers window)))
4198 (when (and (setq new-buffer (car entry))
4199 (or (buffer-live-p new-buffer)
4200 (not (setq killed-buffers
4201 (cons new-buffer killed-buffers))))
4202 (not (eq new-buffer old-buffer))
4203 (or (null pred) (funcall pred new-buffer)))
4204 (if (and (not switch-to-visible-buffer)
4205 (get-buffer-window new-buffer frame))
4206 ;; Try to avoid showing a buffer visible in some other window.
4207 (unless visible
4208 (setq visible new-buffer))
4209 (set-window-buffer-start-and-point
4210 window new-buffer (nth 1 entry) (nth 2 entry))
4211 (throw 'found t))))
4213 ;; Show a buffer visible in another window.
4214 (when visible
4215 (setq new-buffer visible)
4216 (set-window-buffer-start-and-point window new-buffer)))
4218 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
4219 (set-window-next-buffers window (delq new-buffer next-buffers))
4221 ;; Remove killed buffers from WINDOW's previous and next buffers.
4222 (when killed-buffers
4223 (dolist (buffer killed-buffers)
4224 (set-window-prev-buffers
4225 window (assq-delete-all buffer (window-prev-buffers window)))
4226 (set-window-next-buffers
4227 window (delq buffer (window-next-buffers window)))))
4229 ;; Return new-buffer.
4230 new-buffer))
4232 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
4233 "Search LIST for a valid buffer to display in FRAME.
4234 Return nil when all buffers in LIST are undesirable for display,
4235 otherwise return the first suitable buffer in LIST.
4237 Buffers not visible in windows are preferred to visible buffers,
4238 unless VISIBLE-OK is non-nil.
4239 If the optional argument FRAME is nil, it defaults to the selected frame.
4240 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
4241 ;; This logic is more or less copied from other-buffer.
4242 (setq frame (or frame (selected-frame)))
4243 (let ((pred (frame-parameter frame 'buffer-predicate))
4244 found buf)
4245 (while (and (not found) list)
4246 (setq buf (car list))
4247 (if (and (not (eq buffer buf))
4248 (buffer-live-p buf)
4249 (or (null pred) (funcall pred buf))
4250 (not (eq (aref (buffer-name buf) 0) ?\s))
4251 (or visible-ok (null (get-buffer-window buf 'visible))))
4252 (setq found buf)
4253 (setq list (cdr list))))
4254 (car list)))
4256 (defun last-buffer (&optional buffer visible-ok frame)
4257 "Return the last buffer in FRAME's buffer list.
4258 If BUFFER is the last buffer, return the preceding buffer
4259 instead. Buffers not visible in windows are preferred to visible
4260 buffers, unless optional argument VISIBLE-OK is non-nil.
4261 Optional third argument FRAME nil or omitted means use the
4262 selected frame's buffer list. If no such buffer exists, return
4263 the buffer `*scratch*', creating it if necessary."
4264 (setq frame (or frame (selected-frame)))
4265 (or (get-next-valid-buffer (nreverse (buffer-list frame))
4266 buffer visible-ok frame)
4267 (get-buffer "*scratch*")
4268 (let ((scratch (get-buffer-create "*scratch*")))
4269 (set-buffer-major-mode scratch)
4270 scratch)))
4272 (defcustom frame-auto-hide-function #'iconify-frame
4273 "Function called to automatically hide frames.
4274 The function is called with one argument - a frame.
4276 Functions affected by this option are those that bury a buffer
4277 shown in a separate frame like `quit-window' and `bury-buffer'."
4278 :type '(choice (const :tag "Iconify" iconify-frame)
4279 (const :tag "Delete" delete-frame)
4280 (const :tag "Do nothing" ignore)
4281 function)
4282 :group 'windows
4283 :group 'frames
4284 :version "24.1")
4286 (defun window--delete (&optional window dedicated-only kill)
4287 "Delete WINDOW if possible.
4288 WINDOW must be a live window and defaults to the selected one.
4289 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
4290 only if it's dedicated to its buffer. Optional argument KILL
4291 means the buffer shown in window will be killed. Return non-nil
4292 if WINDOW gets deleted or its frame is auto-hidden."
4293 (setq window (window-normalize-window window t))
4294 (unless (and dedicated-only (not (window-dedicated-p window)))
4295 (let ((deletable (window-deletable-p window)))
4296 (cond
4297 ((eq deletable 'frame)
4298 (let ((frame (window-frame window)))
4299 (cond
4300 (kill
4301 (delete-frame frame))
4302 ((functionp frame-auto-hide-function)
4303 (funcall frame-auto-hide-function frame))))
4304 'frame)
4305 (deletable
4306 (delete-window window)
4307 t)))))
4309 (defun bury-buffer (&optional buffer-or-name)
4310 "Put BUFFER-OR-NAME at the end of the list of all buffers.
4311 There it is the least likely candidate for `other-buffer' to
4312 return; thus, the least likely buffer for \\[switch-to-buffer] to
4313 select by default.
4315 You can specify a buffer name as BUFFER-OR-NAME, or an actual
4316 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
4317 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
4318 remove the current buffer from the selected window if it is
4319 displayed there."
4320 (interactive)
4321 (let* ((buffer (window-normalize-buffer buffer-or-name)))
4322 ;; If `buffer-or-name' is not on the selected frame we unrecord it
4323 ;; although it's not "here" (call it a feature).
4324 (bury-buffer-internal buffer)
4325 ;; Handle case where `buffer-or-name' is nil and the current buffer
4326 ;; is shown in the selected window.
4327 (cond
4328 ((or buffer-or-name (not (eq buffer (window-buffer)))))
4329 ((window--delete nil t))
4331 ;; Switch to another buffer in window.
4332 (set-window-dedicated-p nil nil)
4333 (switch-to-prev-buffer nil 'bury)))
4335 ;; Always return nil.
4336 nil))
4338 (defun unbury-buffer ()
4339 "Switch to the last buffer in the buffer list."
4340 (interactive)
4341 (switch-to-buffer (last-buffer)))
4343 (defun next-buffer ()
4344 "In selected window switch to next buffer."
4345 (interactive)
4346 (cond
4347 ((window-minibuffer-p)
4348 (error "Cannot switch buffers in minibuffer window"))
4349 ((eq (window-dedicated-p) t)
4350 (error "Window is strongly dedicated to its buffer"))
4352 (switch-to-next-buffer))))
4354 (defun previous-buffer ()
4355 "In selected window switch to previous buffer."
4356 (interactive)
4357 (cond
4358 ((window-minibuffer-p)
4359 (error "Cannot switch buffers in minibuffer window"))
4360 ((eq (window-dedicated-p) t)
4361 (error "Window is strongly dedicated to its buffer"))
4363 (switch-to-prev-buffer))))
4365 (defun delete-windows-on (&optional buffer-or-name frame)
4366 "Delete all windows showing BUFFER-OR-NAME.
4367 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4368 and defaults to the current buffer.
4370 The following non-nil values of the optional argument FRAME
4371 have special meanings:
4373 - t means consider all windows on the selected frame only.
4375 - `visible' means consider all windows on all visible frames on
4376 the current terminal.
4378 - 0 (the number zero) means consider all windows on all visible
4379 and iconified frames on the current terminal.
4381 - A frame means consider all windows on that frame only.
4383 Any other value of FRAME means consider all windows on all
4384 frames.
4386 When a window showing BUFFER-OR-NAME is dedicated and the only
4387 window of its frame, that frame is deleted when there are other
4388 frames left."
4389 (interactive "BDelete windows on (buffer):\nP")
4390 (let ((buffer (window-normalize-buffer buffer-or-name))
4391 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4392 ;; `window-list-1' based function.
4393 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4394 (dolist (window (window-list-1 nil nil all-frames))
4395 (if (eq (window-buffer window) buffer)
4396 (let ((deletable (window-deletable-p window)))
4397 (cond
4398 ((and (eq deletable 'frame) (window-dedicated-p window))
4399 ;; Delete frame if and only if window is dedicated.
4400 (delete-frame (window-frame window)))
4401 ((eq deletable t)
4402 ;; Delete window.
4403 (delete-window window))
4405 ;; In window switch to previous buffer.
4406 (set-window-dedicated-p window nil)
4407 (switch-to-prev-buffer window 'bury))))
4408 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4409 (unrecord-window-buffer window buffer)))))
4411 (defun replace-buffer-in-windows (&optional buffer-or-name)
4412 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
4413 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4414 and defaults to the current buffer.
4416 When a window showing BUFFER-OR-NAME is dedicated, that window is
4417 deleted. If that window is the only window on its frame, the
4418 frame is deleted too when there are other frames left. If there
4419 are no other frames left, some other buffer is displayed in that
4420 window.
4422 This function removes the buffer denoted by BUFFER-OR-NAME from
4423 all window-local buffer lists."
4424 (interactive "bBuffer to replace: ")
4425 (let ((buffer (window-normalize-buffer buffer-or-name)))
4426 (dolist (window (window-list-1 nil nil t))
4427 (if (eq (window-buffer window) buffer)
4428 (unless (window--delete window t t)
4429 ;; Switch to another buffer in window.
4430 (set-window-dedicated-p window nil)
4431 (switch-to-prev-buffer window 'kill))
4432 ;; Unrecord BUFFER in WINDOW.
4433 (unrecord-window-buffer window buffer)))))
4435 (defun quit-restore-window (&optional window bury-or-kill)
4436 "Quit WINDOW and deal with its buffer.
4437 WINDOW must be a live window and defaults to the selected one.
4439 According to information stored in WINDOW's `quit-restore' window
4440 parameter either (1) delete WINDOW and its frame, (2) delete
4441 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4442 or (4) make WINDOW display some other buffer than the present
4443 one. If non-nil, reset `quit-restore' parameter to nil.
4445 Optional second argument BURY-OR-KILL tells how to proceed with
4446 the buffer of WINDOW. The following values are handled:
4448 nil means to not handle the buffer in a particular way. This
4449 means that if WINDOW is not deleted by this function, invoking
4450 `switch-to-prev-buffer' will usually show the buffer again.
4452 `append' means that if WINDOW is not deleted, move its buffer to
4453 the end of WINDOW's previous buffers so it's less likely that a
4454 future invocation of `switch-to-prev-buffer' will switch to it.
4455 Also, move the buffer to the end of the frame's buffer list.
4457 `bury' means that if WINDOW is not deleted, remove its buffer
4458 from WINDOW'S list of previous buffers. Also, move the buffer
4459 to the end of the frame's buffer list. This value provides the
4460 most reliable remedy to not have `switch-to-prev-buffer' switch
4461 to this buffer again without killing the buffer.
4463 `kill' means to kill WINDOW's buffer."
4464 (setq window (window-normalize-window window t))
4465 (let* ((buffer (window-buffer window))
4466 (quit-restore (window-parameter window 'quit-restore))
4467 (prev-buffer
4468 (let* ((prev-buffers (window-prev-buffers window))
4469 (prev-buffer (caar prev-buffers)))
4470 (and (or (not (eq prev-buffer buffer))
4471 (and (cdr prev-buffers)
4472 (not (eq (setq prev-buffer (cadr prev-buffers))
4473 buffer))))
4474 prev-buffer)))
4475 quad entry)
4476 (cond
4477 ((and (not prev-buffer)
4478 (or (eq (nth 1 quit-restore) 'frame)
4479 (and (eq (nth 1 quit-restore) 'window)
4480 ;; If the window has been created on an existing
4481 ;; frame and ended up as the sole window on that
4482 ;; frame, do not delete it (Bug#12764).
4483 (not (eq window (frame-root-window window)))))
4484 (eq (nth 3 quit-restore) buffer)
4485 ;; Delete WINDOW if possible.
4486 (window--delete window nil (eq bury-or-kill 'kill)))
4487 ;; If the previously selected window is still alive, select it.
4488 (when (window-live-p (nth 2 quit-restore))
4489 (select-window (nth 2 quit-restore))))
4490 ((and (listp (setq quad (nth 1 quit-restore)))
4491 (buffer-live-p (car quad))
4492 (eq (nth 3 quit-restore) buffer))
4493 ;; Show another buffer stored in quit-restore parameter.
4494 (when (and (integerp (nth 3 quad))
4495 (if (window-combined-p window)
4496 (/= (nth 3 quad) (window-total-height window))
4497 (/= (nth 3 quad) (window-total-width window))))
4498 ;; Try to resize WINDOW to its old height but don't signal an
4499 ;; error.
4500 (condition-case nil
4501 (window-resize
4502 window
4503 (- (nth 3 quad) (if (window-combined-p window)
4504 (window-total-height window)
4505 (window-total-width window)))
4506 (window-combined-p window t))
4507 (error nil)))
4508 (set-window-dedicated-p window nil)
4509 ;; Restore WINDOW's previous buffer, start and point position.
4510 (set-window-buffer-start-and-point
4511 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
4512 ;; Deal with the buffer we just removed from WINDOW.
4513 (setq entry (and (eq bury-or-kill 'append)
4514 (assq buffer (window-prev-buffers window))))
4515 (when bury-or-kill
4516 ;; Remove buffer from WINDOW's previous and next buffers.
4517 (set-window-prev-buffers
4518 window (assq-delete-all buffer (window-prev-buffers window)))
4519 (set-window-next-buffers
4520 window (delq buffer (window-next-buffers window))))
4521 (when entry
4522 ;; Append old buffer's entry to list of WINDOW's previous
4523 ;; buffers so it's less likely to get switched to soon but
4524 ;; `display-buffer-in-previous-window' can nevertheless find it.
4525 (set-window-prev-buffers
4526 window (append (window-prev-buffers window) (list entry))))
4527 ;; Reset the quit-restore parameter.
4528 (set-window-parameter window 'quit-restore nil)
4529 ;; Select old window.
4530 (when (window-live-p (nth 2 quit-restore))
4531 (select-window (nth 2 quit-restore))))
4533 ;; Show some other buffer in WINDOW and reset the quit-restore
4534 ;; parameter.
4535 (set-window-parameter window 'quit-restore nil)
4536 ;; Make sure that WINDOW is no more dedicated.
4537 (set-window-dedicated-p window nil)
4538 (switch-to-prev-buffer window bury-or-kill)))
4540 ;; Deal with the buffer.
4541 (cond
4542 ((not (buffer-live-p buffer)))
4543 ((eq bury-or-kill 'kill)
4544 (kill-buffer buffer))
4545 (bury-or-kill
4546 (bury-buffer-internal buffer)))))
4548 (defun quit-window (&optional kill window)
4549 "Quit WINDOW and bury its buffer.
4550 WINDOW must be a live window and defaults to the selected one.
4551 With prefix argument KILL non-nil, kill the buffer instead of
4552 burying it.
4554 According to information stored in WINDOW's `quit-restore' window
4555 parameter either (1) delete WINDOW and its frame, (2) delete
4556 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4557 or (4) make WINDOW display some other buffer than the present
4558 one. If non-nil, reset `quit-restore' parameter to nil."
4559 (interactive "P")
4560 (quit-restore-window window (if kill 'kill 'bury)))
4562 (defun quit-windows-on (&optional buffer-or-name kill frame)
4563 "Quit all windows showing BUFFER-OR-NAME.
4564 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4565 and defaults to the current buffer. Optional argument KILL
4566 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4567 BUFFER-OR-NAME. Optional argument FRAME is handled as by
4568 `delete-windows-on'.
4570 This function calls `quit-window' on all candidate windows
4571 showing BUFFER-OR-NAME."
4572 (interactive "BQuit windows on (buffer):\nP")
4573 (let ((buffer (window-normalize-buffer buffer-or-name))
4574 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4575 ;; `window-list-1' based function.
4576 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4577 (dolist (window (window-list-1 nil nil all-frames))
4578 (if (eq (window-buffer window) buffer)
4579 (quit-window kill window)
4580 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4581 (unrecord-window-buffer window buffer)))))
4583 (defun split-window (&optional window size side pixelwise)
4584 "Make a new window adjacent to WINDOW.
4585 WINDOW must be a valid window and defaults to the selected one.
4586 Return the new window which is always a live window.
4588 Optional argument SIZE a positive number means make WINDOW SIZE
4589 lines or columns tall. If SIZE is negative, make the new window
4590 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
4591 absolute value can be less than `window-min-height' or
4592 `window-min-width'; so this command can make a new window as
4593 small as one line or two columns. SIZE defaults to half of
4594 WINDOW's size.
4596 Optional third argument SIDE nil (or `below') specifies that the
4597 new window shall be located below WINDOW. SIDE `above' means the
4598 new window shall be located above WINDOW. In both cases SIZE
4599 specifies the new number of lines for WINDOW (or the new window
4600 if SIZE is negative) including space reserved for the mode and/or
4601 header line.
4603 SIDE t (or `right') specifies that the new window shall be
4604 located on the right side of WINDOW. SIDE `left' means the new
4605 window shall be located on the left of WINDOW. In both cases
4606 SIZE specifies the new number of columns for WINDOW (or the new
4607 window provided SIZE is negative) including space reserved for
4608 fringes and the scrollbar or a divider column. Any other non-nil
4609 value for SIDE is currently handled like t (or `right').
4611 PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
4613 If the variable `ignore-window-parameters' is non-nil or the
4614 `split-window' parameter of WINDOW equals t, do not process any
4615 parameters of WINDOW. Otherwise, if the `split-window' parameter
4616 of WINDOW specifies a function, call that function with all three
4617 arguments and return the value returned by that function.
4619 Otherwise, if WINDOW is part of an atomic window, \"split\" the
4620 root of that atomic window. The new window does not become a
4621 member of that atomic window.
4623 If WINDOW is live, properties of the new window like margins and
4624 scrollbars are inherited from WINDOW. If WINDOW is an internal
4625 window, these properties as well as the buffer displayed in the
4626 new window are inherited from the window selected on WINDOW's
4627 frame. The selected window is not changed by this function."
4628 (setq window (window-normalize-window window))
4629 (let* ((side (cond
4630 ((not side) 'below)
4631 ((memq side '(below above right left)) side)
4632 (t 'right)))
4633 (horizontal (not (memq side '(below above))))
4634 (frame (window-frame window))
4635 (parent (window-parent window))
4636 (function (window-parameter window 'split-window))
4637 (window-side (window-parameter window 'window-side))
4638 ;; Rebind the following two variables since in some cases we
4639 ;; have to override their value.
4640 (window-combination-limit window-combination-limit)
4641 (window-combination-resize window-combination-resize)
4642 (char-size (frame-char-size window horizontal))
4643 (pixel-size
4644 (when (numberp size)
4645 (window--size-to-pixel window size horizontal pixelwise t)))
4646 (divider-width (if horizontal
4647 (frame-right-divider-width frame)
4648 (frame-bottom-divider-width frame)))
4649 atom-root ignore)
4650 (window--check frame)
4651 (catch 'done
4652 (cond
4653 ;; Ignore window parameters if either `ignore-window-parameters'
4654 ;; is t or the `split-window' parameter equals t.
4655 ((or ignore-window-parameters (eq function t)))
4656 ((functionp function)
4657 ;; The `split-window' parameter specifies the function to call.
4658 ;; If that function is `ignore', do nothing.
4659 (throw 'done (funcall function window size side)))
4660 ;; If WINDOW is part of an atomic window, split the root window
4661 ;; of that atomic window instead.
4662 ((and (window-parameter window 'window-atom)
4663 (setq atom-root (window-atom-root window))
4664 (not (eq atom-root window)))
4665 (throw 'done (split-window atom-root size side pixelwise)))
4666 ;; If WINDOW is a side window or its first or last child is a
4667 ;; side window, throw an error unless `window-combination-resize'
4668 ;; equals 'side.
4669 ((and (not (eq window-combination-resize 'side))
4670 (window--side-window-p window))
4671 (error "Cannot split side window or parent of side window"))
4672 ;; If `window-combination-resize' is 'side and window has a side
4673 ;; window sibling, bind `window-combination-limit' to t.
4674 ((and (not (eq window-combination-resize 'side))
4675 (or (and (window-prev-sibling window)
4676 (window-parameter
4677 (window-prev-sibling window) 'window-side))
4678 (and (window-next-sibling window)
4679 (window-parameter
4680 (window-next-sibling window) 'window-side))))
4681 (setq window-combination-limit t)))
4683 ;; If `window-combination-resize' is t and SIZE is non-negative,
4684 ;; bind `window-combination-limit' to t.
4685 (when (and (eq window-combination-resize t)
4686 pixel-size (> pixel-size 0))
4687 (setq window-combination-limit t))
4689 (let* ((parent-pixel-size
4690 ;; `parent-pixel-size' is the pixel size of WINDOW's
4691 ;; parent, provided it has one.
4692 (when parent (window-size parent horizontal t)))
4693 ;; `resize' non-nil means we are supposed to resize other
4694 ;; windows in WINDOW's combination.
4695 (resize
4696 (and window-combination-resize
4697 (or (window-parameter window 'window-side)
4698 (not (eq window-combination-resize 'side)))
4699 (not (eq window-combination-limit t))
4700 ;; Resize makes sense in iso-combinations only.
4701 (window-combined-p window horizontal)))
4702 ;; `old-pixel-size' is the current pixel size of WINDOW.
4703 (old-pixel-size (window-size window horizontal t))
4704 ;; `new-size' is the specified or calculated size of the
4705 ;; new window.
4706 new-pixel-size new-parent new-normal)
4707 (cond
4708 ((not pixel-size)
4709 (setq new-pixel-size
4710 (if resize
4711 ;; When resizing try to give the new window the
4712 ;; average size of a window in its combination.
4713 (max (min (- parent-pixel-size
4714 (window-min-size parent horizontal nil t))
4715 (/ parent-pixel-size
4716 (1+ (window-combinations parent horizontal))))
4717 (window-min-pixel-size))
4718 ;; Else try to give the new window half the size
4719 ;; of WINDOW (plus an eventual odd pixel).
4720 (/ old-pixel-size 2)))
4721 (unless window-resize-pixelwise
4722 ;; Round to nearest char-size multiple.
4723 (setq new-pixel-size
4724 (* char-size (round new-pixel-size char-size)))))
4725 ((>= pixel-size 0)
4726 ;; SIZE non-negative specifies the new size of WINDOW.
4728 ;; Note: Specifying a non-negative SIZE is practically
4729 ;; always done as workaround for making the new window
4730 ;; appear above or on the left of the new window (the
4731 ;; ispell window is a typical example of that). In all
4732 ;; these cases the SIDE argument should be set to 'above
4733 ;; or 'left in order to support the 'resize option.
4734 ;; Here we have to nest the windows instead, see above.
4735 (setq new-pixel-size (- old-pixel-size pixel-size)))
4737 ;; SIZE negative specifies the size of the new window.
4738 (setq new-pixel-size (- pixel-size))))
4740 ;; Check SIZE.
4741 (cond
4742 ((not pixel-size)
4743 (cond
4744 (resize
4745 ;; SIZE unspecified, resizing.
4746 (unless (or (window-sizable-p
4747 parent (- (+ new-pixel-size divider-width)) horizontal
4748 nil t)
4749 (window-sizable-p
4750 parent (- (+ new-pixel-size divider-width)) horizontal
4751 (setq ignore 'preserved) t))
4752 (error "Window %s too small for splitting (1)" parent)))
4753 ((and (> (+ new-pixel-size divider-width
4754 (window-min-size window horizontal nil t))
4755 old-pixel-size)
4756 (> (+ new-pixel-size divider-width
4757 (window-min-size
4758 window horizontal (setq ignore 'preserved) t))
4759 old-pixel-size))
4760 ;; SIZE unspecified, no resizing.
4761 (error "Window %s too small for splitting (2)" window))))
4762 ((and (>= pixel-size 0)
4763 (or (>= pixel-size old-pixel-size)
4764 (< new-pixel-size
4765 (window-safe-min-pixel-size window horizontal))))
4766 ;; SIZE specified as new size of old window. If the new size
4767 ;; is larger than the old size or the size of the new window
4768 ;; would be less than the safe minimum, signal an error.
4769 (error "Window %s too small for splitting (3)" window))
4770 (resize
4771 ;; SIZE specified, resizing.
4772 (unless (or (window-sizable-p
4773 parent (- (+ new-pixel-size divider-width)) horizontal
4774 nil t)
4775 (window-sizable-p
4776 parent (- (+ new-pixel-size divider-width)) horizontal
4777 (setq ignore 'preserved) t))
4778 ;; If we cannot resize the parent give up.
4779 (error "Window %s too small for splitting (4)" parent)))
4780 ((or (< new-pixel-size
4781 (window-safe-min-pixel-size window horizontal))
4782 (< (- old-pixel-size new-pixel-size)
4783 (window-safe-min-pixel-size window horizontal)))
4784 ;; SIZE specification violates minimum size restrictions.
4785 (error "Window %s too small for splitting (5)" window)))
4787 (window--resize-reset frame horizontal)
4789 (setq new-parent
4790 ;; Make new-parent non-nil if we need a new parent window;
4791 ;; either because we want to nest or because WINDOW is not
4792 ;; iso-combined.
4793 (or (eq window-combination-limit t)
4794 (not (window-combined-p window horizontal))))
4795 (setq new-normal
4796 ;; Make new-normal the normal size of the new window.
4797 (cond
4798 (pixel-size (/ (float new-pixel-size)
4799 (if new-parent old-pixel-size parent-pixel-size)))
4800 (new-parent 0.5)
4801 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
4802 (t (/ (window-normal-size window horizontal) 2.0))))
4804 (if resize
4805 ;; Try to get space from OLD's siblings. We could go "up" and
4806 ;; try getting additional space from surrounding windows but
4807 ;; we won't be able to return space to those windows when we
4808 ;; delete the one we create here. Hence we do not go up.
4809 (progn
4810 (window--resize-child-windows
4811 parent (- new-pixel-size) horizontal nil ignore)
4812 (let* ((normal (- 1.0 new-normal))
4813 (sub (window-child parent)))
4814 (while sub
4815 (set-window-new-normal
4816 sub (* (window-normal-size sub horizontal) normal))
4817 (setq sub (window-right sub)))))
4818 ;; Get entire space from WINDOW.
4819 (set-window-new-pixel
4820 window (- old-pixel-size new-pixel-size))
4821 (window--resize-this-window
4822 window (- new-pixel-size) horizontal ignore)
4823 (set-window-new-normal
4824 window (- (if new-parent 1.0 (window-normal-size window horizontal))
4825 new-normal)))
4827 (let* ((new (split-window-internal window new-pixel-size side new-normal)))
4828 (window--pixel-to-total frame horizontal)
4829 ;; Assign window-side parameters, if any.
4830 (cond
4831 ((eq window-combination-resize 'side)
4832 (let ((window-side
4833 (cond
4834 (window-side window-side)
4835 ((eq side 'above) 'top)
4836 ((eq side 'below) 'bottom)
4837 (t side))))
4838 ;; We made a new side window.
4839 (set-window-parameter new 'window-side window-side)
4840 (when (and new-parent (window-parameter window 'window-side))
4841 ;; We've been splitting a side root window. Give the
4842 ;; new parent the same window-side parameter.
4843 (set-window-parameter
4844 (window-parent new) 'window-side window-side))))
4845 ((eq window-combination-resize 'atom)
4846 ;; Make sure `window--check-frame' won't destroy an existing
4847 ;; atomic window in case the new window gets nested inside.
4848 (unless (window-parameter window 'window-atom)
4849 (set-window-parameter window 'window-atom t))
4850 (when new-parent
4851 (set-window-parameter (window-parent new) 'window-atom t))
4852 (set-window-parameter new 'window-atom t)))
4854 ;; Sanitize sizes.
4855 (window--sanitize-window-sizes frame horizontal)
4857 (run-window-configuration-change-hook frame)
4858 (run-window-scroll-functions new)
4859 (window--check frame)
4860 ;; Always return the new window.
4861 new)))))
4863 ;; I think this should be the default; I think people will prefer it--rms.
4864 (defcustom split-window-keep-point t
4865 "If non-nil, \\[split-window-below] preserves point in the new window.
4866 If nil, adjust point in the two windows to minimize redisplay.
4867 This option applies only to `split-window-below' and functions
4868 that call it. The low-level `split-window' function always keeps
4869 the original point in both windows."
4870 :type 'boolean
4871 :group 'windows)
4873 (defun split-window-below (&optional size)
4874 "Split the selected window into two windows, one above the other.
4875 The selected window is above. The newly split-off window is
4876 below and displays the same buffer. Return the new window.
4878 If optional argument SIZE is omitted or nil, both windows get the
4879 same height, or close to it. If SIZE is positive, the upper
4880 \(selected) window gets SIZE lines. If SIZE is negative, the
4881 lower (new) window gets -SIZE lines.
4883 If the variable `split-window-keep-point' is non-nil, both
4884 windows get the same value of point as the selected window.
4885 Otherwise, the window starts are chosen so as to minimize the
4886 amount of redisplay; this is convenient on slow terminals."
4887 (interactive "P")
4888 (let ((old-window (selected-window))
4889 (old-point (window-point))
4890 (size (and size (prefix-numeric-value size)))
4891 moved-by-window-height moved new-window bottom)
4892 (when (and size (< size 0) (< (- size) window-min-height))
4893 ;; `split-window' would not signal an error here.
4894 (error "Size of new window too small"))
4895 (setq new-window (split-window nil size))
4896 (unless split-window-keep-point
4897 (with-current-buffer (window-buffer)
4898 ;; Use `save-excursion' around vertical movements below
4899 ;; (Bug#10971). Note: When the selected window's buffer has a
4900 ;; header line, up to two lines of the buffer may not show up
4901 ;; in the resulting configuration.
4902 (save-excursion
4903 (goto-char (window-start))
4904 (setq moved (vertical-motion (window-height)))
4905 (set-window-start new-window (point))
4906 (when (> (point) (window-point new-window))
4907 (set-window-point new-window (point)))
4908 (when (= moved (window-height))
4909 (setq moved-by-window-height t)
4910 (vertical-motion -1))
4911 (setq bottom (point)))
4912 (and moved-by-window-height
4913 (<= bottom (point))
4914 (set-window-point old-window (1- bottom)))
4915 (and moved-by-window-height
4916 (<= (window-start new-window) old-point)
4917 (set-window-point new-window old-point)
4918 (select-window new-window))))
4919 ;; Always copy quit-restore parameter in interactive use.
4920 (let ((quit-restore (window-parameter old-window 'quit-restore)))
4921 (when quit-restore
4922 (set-window-parameter new-window 'quit-restore quit-restore)))
4923 new-window))
4925 (defalias 'split-window-vertically 'split-window-below)
4927 (defun split-window-right (&optional size)
4928 "Split the selected window into two side-by-side windows.
4929 The selected window is on the left. The newly split-off window
4930 is on the right and displays the same buffer. Return the new
4931 window.
4933 If optional argument SIZE is omitted or nil, both windows get the
4934 same width, or close to it. If SIZE is positive, the left-hand
4935 \(selected) window gets SIZE columns. If SIZE is negative, the
4936 right-hand (new) window gets -SIZE columns. Here, SIZE includes
4937 the width of the window's scroll bar; if there are no scroll
4938 bars, it includes the width of the divider column to the window's
4939 right, if any."
4940 (interactive "P")
4941 (let ((old-window (selected-window))
4942 (size (and size (prefix-numeric-value size)))
4943 new-window)
4944 (when (and size (< size 0) (< (- size) window-min-width))
4945 ;; `split-window' would not signal an error here.
4946 (error "Size of new window too small"))
4947 (setq new-window (split-window nil size t))
4948 ;; Always copy quit-restore parameter in interactive use.
4949 (let ((quit-restore (window-parameter old-window 'quit-restore)))
4950 (when quit-restore
4951 (set-window-parameter new-window 'quit-restore quit-restore)))
4952 new-window))
4954 (defalias 'split-window-horizontally 'split-window-right)
4956 ;;; Balancing windows.
4958 ;; The following routine uses the recycled code from an old version of
4959 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
4960 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
4961 ;; more readable (FWIW we'd need three loops - one to calculate the
4962 ;; minimum sizes per window, one to enlarge or shrink windows until the
4963 ;; new parent-size matches, and one where we shrink the largest/enlarge
4964 ;; the smallest window).
4965 (defun balance-windows-2 (window horizontal)
4966 "Subroutine of `balance-windows-1'.
4967 WINDOW must be a vertical combination (horizontal if HORIZONTAL
4968 is non-nil)."
4969 (let* ((char-size (if window-resize-pixelwise
4971 (frame-char-size window horizontal)))
4972 (first (window-child window))
4973 (sub first)
4974 (number-of-children 0)
4975 (parent-size (window-new-pixel window))
4976 (total-sum parent-size)
4977 failed size sub-total sub-delta sub-amount rest)
4978 (while sub
4979 (setq number-of-children (1+ number-of-children))
4980 (when (window-size-fixed-p sub horizontal)
4981 (setq total-sum
4982 (- total-sum (window-size sub horizontal t)))
4983 (set-window-new-normal sub 'ignore))
4984 (setq sub (window-right sub)))
4986 (setq failed t)
4987 (while (and failed (> number-of-children 0))
4988 (setq size (/ total-sum number-of-children))
4989 (setq failed nil)
4990 (setq sub first)
4991 (while (and sub (not failed))
4992 ;; Ignore child windows that should be ignored or are stuck.
4993 (unless (window--resize-child-windows-skip-p sub)
4994 (setq sub-total (window-size sub horizontal t))
4995 (setq sub-delta (- size sub-total))
4996 (setq sub-amount
4997 (window-sizable sub sub-delta horizontal nil t))
4998 ;; Register the new total size for this child window.
4999 (set-window-new-pixel sub (+ sub-total sub-amount))
5000 (unless (= sub-amount sub-delta)
5001 (setq total-sum (- total-sum sub-total sub-amount))
5002 (setq number-of-children (1- number-of-children))
5003 ;; We failed and need a new round.
5004 (setq failed t)
5005 (set-window-new-normal sub 'skip)))
5006 (setq sub (window-right sub))))
5008 ;; How can we be sure that `number-of-children' is NOT zero here ?
5009 (setq rest (% total-sum number-of-children))
5010 ;; Fix rounding by trying to enlarge non-stuck windows by one line
5011 ;; (column) until `rest' is zero.
5012 (setq sub first)
5013 (while (and sub (> rest 0))
5014 (unless (window--resize-child-windows-skip-p window)
5015 (set-window-new-pixel sub (min rest char-size) t)
5016 (setq rest (- rest char-size)))
5017 (setq sub (window-right sub)))
5019 ;; Fix rounding by trying to enlarge stuck windows by one line
5020 ;; (column) until `rest' equals zero.
5021 (setq sub first)
5022 (while (and sub (> rest 0))
5023 (unless (eq (window-new-normal sub) 'ignore)
5024 (set-window-new-pixel sub (min rest char-size) t)
5025 (setq rest (- rest char-size)))
5026 (setq sub (window-right sub)))
5028 (setq sub first)
5029 (while sub
5030 ;; Record new normal sizes.
5031 (set-window-new-normal
5032 sub (/ (if (eq (window-new-normal sub) 'ignore)
5033 (window-size sub horizontal t)
5034 (window-new-pixel sub))
5035 (float parent-size)))
5036 ;; Recursively balance each window's child windows.
5037 (balance-windows-1 sub horizontal)
5038 (setq sub (window-right sub)))))
5040 (defun balance-windows-1 (window &optional horizontal)
5041 "Subroutine of `balance-windows'."
5042 (if (window-child window)
5043 (let ((sub (window-child window)))
5044 (if (window-combined-p sub horizontal)
5045 (balance-windows-2 window horizontal)
5046 (let ((size (window-new-pixel window)))
5047 (while sub
5048 (set-window-new-pixel sub size)
5049 (balance-windows-1 sub horizontal)
5050 (setq sub (window-right sub))))))))
5052 (defun balance-windows (&optional window-or-frame)
5053 "Balance the sizes of windows of WINDOW-OR-FRAME.
5054 WINDOW-OR-FRAME is optional and defaults to the selected frame.
5055 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
5056 windows of that frame. If WINDOW-OR-FRAME denotes a window,
5057 recursively balance the sizes of all child windows of that
5058 window."
5059 (interactive)
5060 (let* ((window
5061 (cond
5062 ((or (not window-or-frame)
5063 (frame-live-p window-or-frame))
5064 (frame-root-window window-or-frame))
5065 ((or (window-live-p window-or-frame)
5066 (window-child window-or-frame))
5067 window-or-frame)
5069 (error "Not a window or frame %s" window-or-frame))))
5070 (frame (window-frame window)))
5071 ;; Balance vertically.
5072 (window--resize-reset (window-frame window))
5073 (balance-windows-1 window)
5074 (when (window--resize-apply-p frame)
5075 (window-resize-apply frame)
5076 (window--pixel-to-total frame)
5077 (run-window-configuration-change-hook frame))
5078 ;; Balance horizontally.
5079 (window--resize-reset (window-frame window) t)
5080 (balance-windows-1 window t)
5081 (when (window--resize-apply-p frame t)
5082 (window-resize-apply frame t)
5083 (window--pixel-to-total frame t)
5084 (run-window-configuration-change-hook frame))))
5086 (defun window-fixed-size-p (&optional window direction)
5087 "Return t if WINDOW cannot be resized in DIRECTION.
5088 WINDOW defaults to the selected window. DIRECTION can be
5089 nil (i.e. any), `height' or `width'."
5090 (with-current-buffer (window-buffer window)
5091 (when (and (boundp 'window-size-fixed) window-size-fixed)
5092 (not (and direction
5093 (member (cons direction window-size-fixed)
5094 '((height . width) (width . height))))))))
5096 ;;; A different solution to balance-windows.
5097 (defvar window-area-factor 1
5098 "Factor by which the window area should be over-estimated.
5099 This is used by `balance-windows-area'.
5100 Changing this globally has no effect.")
5101 (make-variable-buffer-local 'window-area-factor)
5103 (defun balance-windows-area-adjust (window delta horizontal pixelwise)
5104 "Wrapper around `window-resize' with error checking.
5105 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
5106 ;; `window-resize' may fail if delta is too large.
5107 (while (>= (abs delta) 1)
5108 (condition-case nil
5109 (progn
5110 ;; It was wrong to use `window-resize' here. Somehow
5111 ;; `balance-windows-area' depends on resizing windows
5112 ;; asymmetrically.
5113 (adjust-window-trailing-edge window delta horizontal pixelwise)
5114 (setq delta 0))
5115 (error
5116 ;;(message "adjust: %s" (error-message-string err))
5117 (setq delta (/ delta 2))))))
5119 (defun balance-windows-area ()
5120 "Make all visible windows the same area (approximately).
5121 See also `window-area-factor' to change the relative size of
5122 specific buffers."
5123 (interactive)
5124 (let* ((unchanged 0) (carry 0) (round 0)
5125 ;; Remove fixed-size windows.
5126 (wins (delq nil (mapcar (lambda (win)
5127 (if (not (window-fixed-size-p win)) win))
5128 (window-list nil 'nomini))))
5129 (changelog nil)
5130 (pixelwise window-resize-pixelwise)
5131 next)
5132 ;; Resizing a window changes the size of surrounding windows in complex
5133 ;; ways, so it's difficult to balance them all. The introduction of
5134 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
5135 ;; very difficult to do. `balance-window' above takes an off-line
5136 ;; approach: get the whole window tree, then balance it, then try to
5137 ;; adjust the windows so they fit the result.
5138 ;; Here, instead, we take a "local optimization" approach, where we just
5139 ;; go through all the windows several times until nothing needs to be
5140 ;; changed. The main problem with this approach is that it's difficult
5141 ;; to make sure it terminates, so we use some heuristic to try and break
5142 ;; off infinite loops.
5143 ;; After a round without any change, we allow a second, to give a chance
5144 ;; to the carry to propagate a minor imbalance from the end back to
5145 ;; the beginning.
5146 (while (< unchanged 2)
5147 ;; (message "New round")
5148 (setq unchanged (1+ unchanged) round (1+ round))
5149 (dolist (win wins)
5150 (setq next win)
5151 (while (progn (setq next (next-window next))
5152 (window-fixed-size-p next)))
5153 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
5154 (let* ((horiz
5155 (< (car (window-pixel-edges win)) (car (window-pixel-edges next))))
5156 (areadiff (/ (- (* (window-size next nil pixelwise)
5157 (window-size next t pixelwise)
5158 (buffer-local-value 'window-area-factor
5159 (window-buffer next)))
5160 (* (window-size win nil pixelwise)
5161 (window-size win t pixelwise)
5162 (buffer-local-value 'window-area-factor
5163 (window-buffer win))))
5164 (max (buffer-local-value 'window-area-factor
5165 (window-buffer win))
5166 (buffer-local-value 'window-area-factor
5167 (window-buffer next)))))
5168 (edgesize (if horiz
5169 (+ (window-size win nil pixelwise)
5170 (window-size next nil pixelwise))
5171 (+ (window-size win t pixelwise)
5172 (window-size next t pixelwise))))
5173 (diff (/ areadiff edgesize)))
5174 (when (zerop diff)
5175 ;; Maybe diff is actually closer to 1 than to 0.
5176 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
5177 (when (and (zerop diff) (not (zerop areadiff)))
5178 (setq diff (/ (+ areadiff carry) edgesize))
5179 ;; Change things smoothly.
5180 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
5181 (if (zerop diff)
5182 ;; Make sure negligible differences don't accumulate to
5183 ;; become significant.
5184 (setq carry (+ carry areadiff))
5185 ;; This used `adjust-window-trailing-edge' before and uses
5186 ;; `window-resize' now. Error wrapping is still needed.
5187 (balance-windows-area-adjust win diff horiz pixelwise)
5188 ;; (sit-for 0.5)
5189 (let ((change (cons win (window-pixel-edges win))))
5190 ;; If the same change has been seen already for this window,
5191 ;; we're most likely in an endless loop, so don't count it as
5192 ;; a change.
5193 (unless (member change changelog)
5194 (push change changelog)
5195 (setq unchanged 0 carry 0)))))))
5196 ;; We've now basically balanced all the windows.
5197 ;; But there may be some minor off-by-one imbalance left over,
5198 ;; so let's do some fine tuning.
5199 ;; (bw-finetune wins)
5200 ;; (message "Done in %d rounds" round)
5203 ;;; Window states, how to get them and how to put them in a window.
5204 (defun window--state-get-1 (window &optional writable)
5205 "Helper function for `window-state-get'."
5206 (let* ((type
5207 (cond
5208 ((window-top-child window) 'vc)
5209 ((window-left-child window) 'hc)
5210 (t 'leaf)))
5211 (buffer (window-buffer window))
5212 (selected (eq window (selected-window)))
5213 (head
5214 `(,type
5215 ,@(unless (window-next-sibling window) `((last . t)))
5216 (pixel-width . ,(window-pixel-width window))
5217 (pixel-height . ,(window-pixel-height window))
5218 (total-width . ,(window-total-width window))
5219 (total-height . ,(window-total-height window))
5220 (normal-height . ,(window-normal-size window))
5221 (normal-width . ,(window-normal-size window t))
5222 ,@(unless (window-live-p window)
5223 `((combination-limit . ,(window-combination-limit window))))
5224 ,@(let ((parameters (window-parameters window))
5225 list)
5226 ;; Make copies of those window parameters whose
5227 ;; persistence property is `writable' if WRITABLE is
5228 ;; non-nil and non-nil if WRITABLE is nil.
5229 (dolist (par parameters)
5230 (let ((pers (cdr (assq (car par)
5231 window-persistent-parameters))))
5232 (when (and pers (or (not writable) (eq pers 'writable)))
5233 (setq list (cons (cons (car par) (cdr par)) list)))))
5234 ;; Add `clone-of' parameter if necessary.
5235 (let ((pers (cdr (assq 'clone-of
5236 window-persistent-parameters))))
5237 (when (and pers (or (not writable) (eq pers 'writable))
5238 (not (assq 'clone-of list)))
5239 (setq list (cons (cons 'clone-of window) list))))
5240 (when list
5241 `((parameters . ,list))))
5242 ,@(when buffer
5243 ;; All buffer related things go in here.
5244 (let ((point (window-point window))
5245 (start (window-start window)))
5246 `((buffer
5247 ,(buffer-name buffer)
5248 (selected . ,selected)
5249 (hscroll . ,(window-hscroll window))
5250 (fringes . ,(window-fringes window))
5251 (margins . ,(window-margins window))
5252 (scroll-bars . ,(window-scroll-bars window))
5253 (vscroll . ,(window-vscroll window))
5254 (dedicated . ,(window-dedicated-p window))
5255 (point . ,(if writable point
5256 (copy-marker point
5257 (buffer-local-value
5258 'window-point-insertion-type
5259 buffer))))
5260 (start . ,(if writable start (copy-marker start)))))))))
5261 (tail
5262 (when (memq type '(vc hc))
5263 (let (list)
5264 (setq window (window-child window))
5265 (while window
5266 (setq list (cons (window--state-get-1 window writable) list))
5267 (setq window (window-right window)))
5268 (nreverse list)))))
5269 (append head tail)))
5271 (defun window-state-get (&optional window writable)
5272 "Return state of WINDOW as a Lisp object.
5273 WINDOW can be any window and defaults to the root window of the
5274 selected frame.
5276 Optional argument WRITABLE non-nil means do not use markers for
5277 sampling `window-point' and `window-start'. Together, WRITABLE
5278 and the variable `window-persistent-parameters' specify which
5279 window parameters are saved by this function. WRITABLE should be
5280 non-nil when the return value shall be written to a file and read
5281 back in another session. Otherwise, an application may run into
5282 an `invalid-read-syntax' error while attempting to read back the
5283 value from file.
5285 The return value can be used as argument for `window-state-put'
5286 to put the state recorded here into an arbitrary window. The
5287 value can be also stored on disk and read back in a new session."
5288 (setq window
5289 (if window
5290 (if (window-valid-p window)
5291 window
5292 (error "%s is not a live or internal window" window))
5293 (frame-root-window)))
5294 ;; The return value is a cons whose car specifies some constraints on
5295 ;; the size of WINDOW. The cdr lists the states of the child windows
5296 ;; of WINDOW.
5297 (cons
5298 ;; Frame related things would go into a function, say `frame-state',
5299 ;; calling `window-state-get' to insert the frame's root window.
5300 `((min-height . ,(window-min-size window))
5301 (min-width . ,(window-min-size window t))
5302 (min-height-ignore . ,(window-min-size window nil t))
5303 (min-width-ignore . ,(window-min-size window t t))
5304 (min-height-safe . ,(window-min-size window nil 'safe))
5305 (min-width-safe . ,(window-min-size window t 'safe))
5306 (min-pixel-height . ,(window-min-size window nil nil t))
5307 (min-pixel-width . ,(window-min-size window t nil t))
5308 (min-pixel-height-ignore . ,(window-min-size window nil t t))
5309 (min-pixel-width-ignore . ,(window-min-size window t t t))
5310 (min-pixel-height-safe . ,(window-min-size window nil 'safe t))
5311 (min-pixel-width-safe . ,(window-min-size window t 'safe t)))
5312 (window--state-get-1 window writable)))
5314 (defvar window-state-put-list nil
5315 "Helper variable for `window-state-put'.")
5317 (defvar window-state-put-stale-windows nil
5318 "Helper variable for `window-state-put'.")
5320 (defun window--state-put-1 (state &optional window ignore totals pixelwise)
5321 "Helper function for `window-state-put'."
5322 (let ((type (car state)))
5323 (setq state (cdr state))
5324 (cond
5325 ((eq type 'leaf)
5326 ;; For a leaf window just add unprocessed entries to
5327 ;; `window-state-put-list'.
5328 (push (cons window state) window-state-put-list))
5329 ((memq type '(vc hc))
5330 (let* ((horizontal (eq type 'hc))
5331 (total (window-size window horizontal pixelwise))
5332 (first t)
5333 size new)
5334 (dolist (item state)
5335 ;; Find the next child window. WINDOW always points to the
5336 ;; real window that we want to fill with what we find here.
5337 (when (memq (car item) '(leaf vc hc))
5338 (if (assq 'last item)
5339 ;; The last child window. Below `window--state-put-1'
5340 ;; will put into it whatever ITEM has in store.
5341 (setq new nil)
5342 ;; Not the last child window, prepare for splitting
5343 ;; WINDOW. SIZE is the new (and final) size of the old
5344 ;; window.
5345 (setq size
5346 (if totals
5347 ;; Use total size.
5348 (if pixelwise
5349 (cdr (assq (if horizontal
5350 'pixel-width
5351 'pixel-height)
5352 item))
5353 (cdr (assq (if horizontal
5354 'total-width
5355 'total-height)
5356 item)))
5357 ;; Use normalized size and round.
5358 (round
5359 (* total
5360 (cdr (assq (if horizontal 'normal-width 'normal-height)
5361 item))))))
5363 ;; Use safe sizes, we try to resize later.
5364 (setq size (max size
5365 (if horizontal
5366 (* window-safe-min-width
5367 (if pixelwise
5368 (frame-char-width (window-frame window))
5370 (* window-safe-min-height
5371 (if pixelwise
5372 (frame-char-height (window-frame window))
5373 1)))))
5374 (if (window-sizable-p window (- size) horizontal 'safe pixelwise)
5375 (let* ((window-combination-limit
5376 (assq 'combination-limit item)))
5377 ;; We must inherit the combination limit, otherwise
5378 ;; we might mess up handling of atomic and side
5379 ;; window.
5380 (setq new (split-window window size horizontal pixelwise)))
5381 ;; Give up if we can't resize window down to safe sizes.
5382 (error "Cannot resize window %s" window))
5384 (when first
5385 (setq first nil)
5386 ;; When creating the first child window add for parent
5387 ;; unprocessed entries to `window-state-put-list'.
5388 (setq window-state-put-list
5389 (cons (cons (window-parent window) state)
5390 window-state-put-list))))
5392 ;; Now process the current window (either the one we've just
5393 ;; split or the last child of its parent).
5394 (window--state-put-1 item window ignore totals)
5395 ;; Continue with the last window split off.
5396 (setq window new))))))))
5398 (defun window--state-put-2 (ignore pixelwise)
5399 "Helper function for `window-state-put'."
5400 (dolist (item window-state-put-list)
5401 (let ((window (car item))
5402 (combination-limit (cdr (assq 'combination-limit item)))
5403 (parameters (cdr (assq 'parameters item)))
5404 (state (cdr (assq 'buffer item))))
5405 (when combination-limit
5406 (set-window-combination-limit window combination-limit))
5407 ;; Reset window's parameters and assign saved ones (we might want
5408 ;; a `remove-window-parameters' function here).
5409 (dolist (parameter (window-parameters window))
5410 (set-window-parameter window (car parameter) nil))
5411 (when parameters
5412 (dolist (parameter parameters)
5413 (set-window-parameter window (car parameter) (cdr parameter))))
5414 ;; Process buffer related state.
5415 (when state
5416 (let ((buffer (get-buffer (car state))))
5417 (if buffer
5418 (with-current-buffer buffer
5419 (set-window-buffer window buffer)
5420 (set-window-hscroll window (cdr (assq 'hscroll state)))
5421 (apply 'set-window-fringes
5422 (cons window (cdr (assq 'fringes state))))
5423 (let ((margins (cdr (assq 'margins state))))
5424 (set-window-margins window (car margins) (cdr margins)))
5425 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
5426 (set-window-scroll-bars
5427 window (car scroll-bars) (nth 2 scroll-bars)
5428 (nth 3 scroll-bars) (nth 5 scroll-bars)))
5429 (set-window-vscroll window (cdr (assq 'vscroll state)))
5430 ;; Adjust vertically.
5431 (if (memq window-size-fixed '(t height))
5432 ;; A fixed height window, try to restore the
5433 ;; original size.
5434 (let ((delta
5435 (- (cdr (assq
5436 (if pixelwise 'pixel-height 'total-height)
5437 item))
5438 (window-size window nil pixelwise)))
5439 window-size-fixed)
5440 (when (window--resizable-p
5441 window delta nil nil nil nil nil pixelwise)
5442 (window-resize window delta nil nil pixelwise)))
5443 ;; Else check whether the window is not high enough.
5444 (let* ((min-size
5445 (window-min-size window nil ignore pixelwise))
5446 (delta
5447 (- min-size (window-size window nil pixelwise))))
5448 (when (and (> delta 0)
5449 (window--resizable-p
5450 window delta nil ignore nil nil nil pixelwise))
5451 (window-resize window delta nil ignore pixelwise))))
5452 ;; Adjust horizontally.
5453 (if (memq window-size-fixed '(t width))
5454 ;; A fixed width window, try to restore the original
5455 ;; size.
5456 (let ((delta
5457 (- (cdr (assq
5458 (if pixelwise 'pixel-width 'total-width)
5459 item))
5460 (window-size window t pixelwise)))
5461 window-size-fixed)
5462 (when (window--resizable-p
5463 window delta nil nil nil nil nil pixelwise)
5464 (window-resize window delta nil nil pixelwise)))
5465 ;; Else check whether the window is not wide enough.
5466 (let* ((min-size (window-min-size window t ignore pixelwise))
5467 (delta (- min-size (window-size window t pixelwise))))
5468 (when (and (> delta 0)
5469 (window--resizable-p
5470 window delta t ignore nil nil nil pixelwise))
5471 (window-resize window delta t ignore pixelwise))))
5472 ;; Set dedicated status.
5473 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
5474 ;; Install positions (maybe we should do this after all
5475 ;; windows have been created and sized).
5476 (ignore-errors
5477 (set-window-start window (cdr (assq 'start state)))
5478 (set-window-point window (cdr (assq 'point state))))
5479 ;; Select window if it's the selected one.
5480 (when (cdr (assq 'selected state))
5481 (select-window window)))
5482 ;; We don't want to raise an error in case the buffer does
5483 ;; not exist anymore, so we switch to a previous one and
5484 ;; save the window with the intention of deleting it later
5485 ;; if possible.
5486 (switch-to-prev-buffer window)
5487 (push window window-state-put-stale-windows)))))))
5489 (defun window-state-put (state &optional window ignore)
5490 "Put window state STATE into WINDOW.
5491 STATE should be the state of a window returned by an earlier
5492 invocation of `window-state-get'. Optional argument WINDOW must
5493 specify a valid window and defaults to the selected one. If
5494 WINDOW is not live, replace WINDOW by a live one before putting
5495 STATE into it.
5497 Optional argument IGNORE non-nil means ignore minimum window
5498 sizes and fixed size restrictions. IGNORE equal `safe' means
5499 windows can get as small as `window-safe-min-height' and
5500 `window-safe-min-width'."
5501 (setq window-state-put-stale-windows nil)
5502 (setq window (window-normalize-window window))
5504 ;; When WINDOW is internal, reduce it to a live one to put STATE into,
5505 ;; see Bug#16793.
5506 (unless (window-live-p window)
5507 (let ((root (frame-root-window window)))
5508 (if (eq window root)
5509 (setq window (frame-first-window root))
5510 (setq root window)
5511 (setq window (catch 'live
5512 (walk-window-subtree
5513 (lambda (window)
5514 (when (window-live-p window)
5515 (throw 'live window)))
5516 root))))
5517 (delete-other-windows-internal window root)))
5519 (set-window-dedicated-p window nil)
5521 (let* ((frame (window-frame window))
5522 (head (car state))
5523 ;; We check here (1) whether the total sizes of root window of
5524 ;; STATE and that of WINDOW are equal so we can avoid
5525 ;; calculating new sizes, and (2) if we do have to resize
5526 ;; whether we can do so without violating size restrictions.
5527 (pixelwise (and (cdr (assq 'pixel-width state))
5528 (cdr (assq 'pixel-height state))))
5529 (totals (or (and pixelwise
5530 (= (window-pixel-width window)
5531 (cdr (assq 'pixel-width state)))
5532 (= (window-pixel-height window)
5533 (cdr (assq 'pixel-height state))))
5534 (and (= (window-total-width window)
5535 (cdr (assq 'total-width state)))
5536 (= (window-total-height window)
5537 (cdr (assq 'total-height state))))))
5538 (min-height (cdr (assq
5539 (if pixelwise 'min-pixel-height 'min-height)
5540 head)))
5541 (min-width (cdr (assq
5542 (if pixelwise 'min-pixel-width 'min-weight)
5543 head))))
5544 (if (and (not totals)
5545 (or (> min-height (window-size window nil pixelwise))
5546 (> min-width (window-size window t pixelwise)))
5547 (or (not ignore)
5548 (and (setq min-height
5549 (cdr (assq
5550 (if pixelwise
5551 'min-pixel-height-ignore
5552 'min-height-ignore)
5553 head)))
5554 (setq min-width
5555 (cdr (assq
5556 (if pixelwise
5557 'min-pixel-width-ignore
5558 'min-width-ignore)
5559 head)))
5560 (or (> min-height
5561 (window-size window nil pixelwise))
5562 (> min-width
5563 (window-size window t pixelwise)))
5564 (or (not (eq ignore 'safe))
5565 (and (setq min-height
5566 (cdr (assq
5567 (if pixelwise
5568 'min-pixel-height-safe
5569 'min-height-safe)
5570 head)))
5571 (setq min-width
5572 (cdr (assq
5573 (if pixelwise
5574 'min-pixel-width-safe
5575 'min-width-safe)
5576 head)))
5577 (or (> min-height
5578 (window-size window nil pixelwise))
5579 (> min-width
5580 (window-size window t pixelwise))))))))
5581 ;; The check above might not catch all errors due to rounding
5582 ;; issues - so IGNORE equal 'safe might not always produce the
5583 ;; minimum possible state. But such configurations hardly make
5584 ;; sense anyway.
5585 (error "Window %s too small to accommodate state" window)
5586 (setq state (cdr state))
5587 (setq window-state-put-list nil)
5588 ;; Work on the windows of a temporary buffer to make sure that
5589 ;; splitting proceeds regardless of any buffer local values of
5590 ;; `window-size-fixed'. Release that buffer after the buffers of
5591 ;; all live windows have been set by `window--state-put-2'.
5592 (with-temp-buffer
5593 (set-window-buffer window (current-buffer))
5594 (window--state-put-1 state window nil totals pixelwise)
5595 (window--state-put-2 ignore pixelwise))
5596 (while window-state-put-stale-windows
5597 (let ((window (pop window-state-put-stale-windows)))
5598 (when (eq (window-deletable-p window) t)
5599 (delete-window window))))
5600 (window--check frame))))
5602 (defun display-buffer-record-window (type window buffer)
5603 "Record information for window used by `display-buffer'.
5604 TYPE specifies the type of the calling operation and must be one
5605 of the symbols `reuse' (when WINDOW existed already and was
5606 reused for displaying BUFFER), `window' (when WINDOW was created
5607 on an already existing frame), or `frame' (when WINDOW was
5608 created on a new frame). WINDOW is the window used for or created
5609 by the `display-buffer' routines. BUFFER is the buffer that
5610 shall be displayed.
5612 This function installs or updates the quit-restore parameter of
5613 WINDOW. The quit-restore parameter is a list of four elements:
5614 The first element is one of the symbols `window', `frame', `same' or
5615 `other'. The second element is either one of the symbols `window'
5616 or `frame' or a list whose elements are the buffer previously
5617 shown in the window, that buffer's window start and window point,
5618 and the window's height. The third element is the window
5619 selected at the time the parameter was created. The fourth
5620 element is BUFFER."
5621 (cond
5622 ((eq type 'reuse)
5623 (if (eq (window-buffer window) buffer)
5624 ;; WINDOW shows BUFFER already. Update WINDOW's quit-restore
5625 ;; parameter, if any.
5626 (let ((quit-restore (window-parameter window 'quit-restore)))
5627 (when (consp quit-restore)
5628 (setcar quit-restore 'same)
5629 ;; The selected-window might have changed in
5630 ;; between (Bug#20353).
5631 (unless (or (eq window (selected-window))
5632 (eq window (nth 2 quit-restore)))
5633 (setcar (cddr quit-restore) (selected-window)))))
5634 ;; WINDOW shows another buffer.
5635 (with-current-buffer (window-buffer window)
5636 (set-window-parameter
5637 window 'quit-restore
5638 (list 'other
5639 ;; A quadruple of WINDOW's buffer, start, point and height.
5640 (list (current-buffer) (window-start window)
5641 ;; Preserve window-point-insertion-type (Bug#12588).
5642 (copy-marker
5643 (window-point window) window-point-insertion-type)
5644 (if (window-combined-p window)
5645 (window-total-height window)
5646 (window-total-width window)))
5647 (selected-window) buffer)))))
5648 ((eq type 'window)
5649 ;; WINDOW has been created on an existing frame.
5650 (set-window-parameter
5651 window 'quit-restore
5652 (list 'window 'window (selected-window) buffer)))
5653 ((eq type 'frame)
5654 ;; WINDOW has been created on a new frame.
5655 (set-window-parameter
5656 window 'quit-restore
5657 (list 'frame 'frame (selected-window) buffer)))))
5659 (defcustom display-buffer-function nil
5660 "If non-nil, function to call to handle `display-buffer'.
5661 It will receive two args, the buffer and a flag which if non-nil
5662 means that the currently selected window is not acceptable. It
5663 should choose or create a window, display the specified buffer in
5664 it, and return the window.
5666 The specified function should call `display-buffer-record-window'
5667 with corresponding arguments to set up the quit-restore parameter
5668 of the window used."
5669 :type '(choice
5670 (const nil)
5671 (function :tag "function"))
5672 :group 'windows)
5674 (make-obsolete-variable 'display-buffer-function
5675 'display-buffer-alist "24.3")
5677 ;; Eventually, we want to turn this into a defvar; instead of
5678 ;; customizing this, the user should use a `pop-up-frame-parameters'
5679 ;; alist entry in `display-buffer-base-action'.
5680 (defcustom pop-up-frame-alist nil
5681 "Alist of parameters for automatically generated new frames.
5682 If non-nil, the value you specify here is used by the default
5683 `pop-up-frame-function' for the creation of new frames.
5685 Since `pop-up-frame-function' is used by `display-buffer' for
5686 making new frames, any value specified here by default affects
5687 the automatic generation of new frames via `display-buffer' and
5688 all functions based on it. The behavior of `make-frame' is not
5689 affected by this variable."
5690 :type '(repeat (cons :format "%v"
5691 (symbol :tag "Parameter")
5692 (sexp :tag "Value")))
5693 :group 'frames)
5695 (defcustom pop-up-frame-function
5696 (lambda () (make-frame pop-up-frame-alist))
5697 "Function used by `display-buffer' for creating a new frame.
5698 This function is called with no arguments and should return a new
5699 frame. The default value calls `make-frame' with the argument
5700 `pop-up-frame-alist'."
5701 :type 'function
5702 :group 'frames)
5704 (defcustom special-display-buffer-names nil
5705 "List of names of buffers that should be displayed specially.
5706 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5707 its name is in this list, displays the buffer in a way specified
5708 by `special-display-function'. `special-display-popup-frame'
5709 \(the default for `special-display-function') usually displays
5710 the buffer in a separate frame made with the parameters specified
5711 by `special-display-frame-alist'. If `special-display-function'
5712 has been set to some other function, that function is called with
5713 the buffer as first, and nil as second argument.
5715 Alternatively, an element of this list can be specified as
5716 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
5717 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5718 `special-display-popup-frame' will interpret such pairs as frame
5719 parameters when it creates a special frame, overriding the
5720 corresponding values from `special-display-frame-alist'.
5722 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5723 `special-display-popup-frame' displays that buffer in the
5724 selected window. If FRAME-PARAMETERS contains (same-frame . t),
5725 it displays that buffer in a window on the selected frame.
5727 If `special-display-function' specifies some other function than
5728 `special-display-popup-frame', that function is called with the
5729 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5730 argument.
5732 Finally, an element of this list can be also specified as
5733 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5734 `special-display-popup-frame' will call FUNCTION with the buffer
5735 named BUFFER-NAME as first argument, and OTHER-ARGS as the
5736 second.
5738 Any alternative function specified here is responsible for
5739 setting up the quit-restore parameter of the window used.
5741 If this variable appears \"not to work\", because you added a
5742 name to it but the corresponding buffer is displayed in the
5743 selected window, look at the values of `same-window-buffer-names'
5744 and `same-window-regexps'. Those variables take precedence over
5745 this one.
5747 See also `special-display-regexps'."
5748 :type '(repeat
5749 (choice :tag "Buffer"
5750 :value ""
5751 (string :format "%v")
5752 (cons :tag "With parameters"
5753 :format "%v"
5754 :value ("" . nil)
5755 (string :format "%v")
5756 (repeat :tag "Parameters"
5757 (cons :format "%v"
5758 (symbol :tag "Parameter")
5759 (sexp :tag "Value"))))
5760 (list :tag "With function"
5761 :format "%v"
5762 :value ("" . nil)
5763 (string :format "%v")
5764 (function :tag "Function")
5765 (repeat :tag "Arguments" (sexp)))))
5766 :group 'windows
5767 :group 'frames)
5768 (make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3")
5769 (put 'special-display-buffer-names 'risky-local-variable t)
5771 (defcustom special-display-regexps nil
5772 "List of regexps saying which buffers should be displayed specially.
5773 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5774 any regexp in this list matches its name, displays it specially
5775 using `special-display-function'. `special-display-popup-frame'
5776 \(the default for `special-display-function') usually displays
5777 the buffer in a separate frame made with the parameters specified
5778 by `special-display-frame-alist'. If `special-display-function'
5779 has been set to some other function, that function is called with
5780 the buffer as first, and nil as second argument.
5782 Alternatively, an element of this list can be specified as
5783 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
5784 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5785 `special-display-popup-frame' will then interpret these pairs as
5786 frame parameters when creating a special frame for a buffer whose
5787 name matches REGEXP, overriding the corresponding values from
5788 `special-display-frame-alist'.
5790 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5791 `special-display-popup-frame' displays buffers matching REGEXP in
5792 the selected window. (same-frame . t) in FRAME-PARAMETERS means
5793 to display such buffers in a window on the selected frame.
5795 If `special-display-function' specifies some other function than
5796 `special-display-popup-frame', that function is called with the
5797 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
5798 as second argument.
5800 Finally, an element of this list can be also specified as
5801 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
5802 will then call FUNCTION with the buffer whose name matched
5803 REGEXP as first, and OTHER-ARGS as second argument.
5805 Any alternative function specified here is responsible for
5806 setting up the quit-restore parameter of the window used.
5808 If this variable appears \"not to work\", because you added a
5809 name to it but the corresponding buffer is displayed in the
5810 selected window, look at the values of `same-window-buffer-names'
5811 and `same-window-regexps'. Those variables take precedence over
5812 this one.
5814 See also `special-display-buffer-names'."
5815 :type '(repeat
5816 (choice :tag "Buffer"
5817 :value ""
5818 (regexp :format "%v")
5819 (cons :tag "With parameters"
5820 :format "%v"
5821 :value ("" . nil)
5822 (regexp :format "%v")
5823 (repeat :tag "Parameters"
5824 (cons :format "%v"
5825 (symbol :tag "Parameter")
5826 (sexp :tag "Value"))))
5827 (list :tag "With function"
5828 :format "%v"
5829 :value ("" . nil)
5830 (regexp :format "%v")
5831 (function :tag "Function")
5832 (repeat :tag "Arguments" (sexp)))))
5833 :group 'windows
5834 :group 'frames)
5835 (make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3")
5836 (put 'special-display-regexps 'risky-local-variable t)
5838 (defun special-display-p (buffer-name)
5839 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
5840 More precisely, return t if `special-display-buffer-names' or
5841 `special-display-regexps' contain a string entry equaling or
5842 matching BUFFER-NAME. If `special-display-buffer-names' or
5843 `special-display-regexps' contain a list entry whose car equals
5844 or matches BUFFER-NAME, the return value is the cdr of that
5845 entry."
5846 (let (tmp)
5847 (cond
5848 ((member buffer-name special-display-buffer-names)
5850 ((setq tmp (assoc buffer-name special-display-buffer-names))
5851 (cdr tmp))
5852 ((catch 'found
5853 (dolist (regexp special-display-regexps)
5854 (cond
5855 ((stringp regexp)
5856 (when (string-match-p regexp buffer-name)
5857 (throw 'found t)))
5858 ((and (consp regexp) (stringp (car regexp))
5859 (string-match-p (car regexp) buffer-name))
5860 (throw 'found (cdr regexp))))))))))
5862 (defcustom special-display-frame-alist
5863 '((height . 14) (width . 80) (unsplittable . t))
5864 "Alist of parameters for special frames.
5865 Special frames are used for buffers whose names are listed in
5866 `special-display-buffer-names' and for buffers whose names match
5867 one of the regular expressions in `special-display-regexps'.
5869 This variable can be set in your init file, like this:
5871 (setq special-display-frame-alist '((width . 80) (height . 20)))
5873 These supersede the values given in `default-frame-alist'."
5874 :type '(repeat (cons :format "%v"
5875 (symbol :tag "Parameter")
5876 (sexp :tag "Value")))
5877 :group 'frames)
5878 (make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3")
5880 (defun special-display-popup-frame (buffer &optional args)
5881 "Pop up a frame displaying BUFFER and return its window.
5882 If BUFFER is already displayed in a visible or iconified frame,
5883 raise that frame. Otherwise, display BUFFER in a new frame.
5885 Optional argument ARGS is a list specifying additional
5886 information.
5888 If ARGS is an alist, use it as a list of frame parameters. If
5889 these parameters contain (same-window . t), display BUFFER in
5890 the selected window. If they contain (same-frame . t), display
5891 BUFFER in a window of the selected frame.
5893 If ARGS is a list whose car is a symbol, use (car ARGS) as a
5894 function to do the work. Pass it BUFFER as first argument, and
5895 pass the elements of (cdr ARGS) as the remaining arguments."
5896 (if (and args (symbolp (car args)))
5897 (apply (car args) buffer (cdr args))
5898 (let ((window (get-buffer-window buffer 0)))
5900 ;; If we have a window already, make it visible.
5901 (when window
5902 (let ((frame (window-frame window)))
5903 (make-frame-visible frame)
5904 (raise-frame frame)
5905 (display-buffer-record-window 'reuse window buffer)
5906 window))
5907 ;; Reuse the current window if the user requested it.
5908 (when (cdr (assq 'same-window args))
5909 (condition-case nil
5910 (progn (switch-to-buffer buffer nil t) (selected-window))
5911 (error nil)))
5912 ;; Stay on the same frame if requested.
5913 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
5914 (let* ((pop-up-windows t)
5915 pop-up-frames
5916 special-display-buffer-names special-display-regexps)
5917 (display-buffer buffer)))
5918 ;; If no window yet, make one in a new frame.
5919 (let* ((frame
5920 (with-current-buffer buffer
5921 (make-frame (append args special-display-frame-alist))))
5922 (window (frame-selected-window frame)))
5923 (display-buffer-record-window 'frame window buffer)
5924 (unless (eq buffer (window-buffer window))
5925 (set-window-buffer window buffer)
5926 (set-window-prev-buffers window nil))
5927 (set-window-dedicated-p window t)
5928 window)))))
5930 (defcustom special-display-function 'special-display-popup-frame
5931 "Function to call for displaying special buffers.
5932 This function is called with two arguments - the buffer and,
5933 optionally, a list - and should return a window displaying that
5934 buffer. The default value usually makes a separate frame for the
5935 buffer using `special-display-frame-alist' to specify the frame
5936 parameters. See the definition of `special-display-popup-frame'
5937 for how to specify such a function.
5939 A buffer is special when its name is either listed in
5940 `special-display-buffer-names' or matches a regexp in
5941 `special-display-regexps'.
5943 The specified function should call `display-buffer-record-window'
5944 with corresponding arguments to set up the quit-restore parameter
5945 of the window used."
5946 :type 'function
5947 :group 'frames)
5948 (make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3")
5950 (defcustom same-window-buffer-names nil
5951 "List of names of buffers that should appear in the \"same\" window.
5952 `display-buffer' and `pop-to-buffer' show a buffer whose name is
5953 on this list in the selected rather than some other window.
5955 An element of this list can be a cons cell instead of just a
5956 string. In that case, the cell's car must be a string specifying
5957 the buffer name. This is for compatibility with
5958 `special-display-buffer-names'; the cdr of the cons cell is
5959 ignored.
5961 See also `same-window-regexps'."
5962 :type '(repeat (string :format "%v"))
5963 :group 'windows)
5965 (defcustom same-window-regexps nil
5966 "List of regexps saying which buffers should appear in the \"same\" window.
5967 `display-buffer' and `pop-to-buffer' show a buffer whose name
5968 matches a regexp on this list in the selected rather than some
5969 other window.
5971 An element of this list can be a cons cell instead of just a
5972 string. In that case, the cell's car must be a regexp matching
5973 the buffer name. This is for compatibility with
5974 `special-display-regexps'; the cdr of the cons cell is ignored.
5976 See also `same-window-buffer-names'."
5977 :type '(repeat (regexp :format "%v"))
5978 :group 'windows)
5980 (defun same-window-p (buffer-name)
5981 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5982 This function returns non-nil if `display-buffer' or
5983 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
5984 selected rather than (as usual) some other window. See
5985 `same-window-buffer-names' and `same-window-regexps'."
5986 (cond
5987 ((not (stringp buffer-name)))
5988 ;; The elements of `same-window-buffer-names' can be buffer
5989 ;; names or cons cells whose cars are buffer names.
5990 ((member buffer-name same-window-buffer-names))
5991 ((assoc buffer-name same-window-buffer-names))
5992 ((catch 'found
5993 (dolist (regexp same-window-regexps)
5994 ;; The elements of `same-window-regexps' can be regexps
5995 ;; or cons cells whose cars are regexps.
5996 (when (or (and (stringp regexp)
5997 (string-match-p regexp buffer-name))
5998 (and (consp regexp) (stringp (car regexp))
5999 (string-match-p (car regexp) buffer-name)))
6000 (throw 'found t)))))))
6002 (defcustom pop-up-frames nil
6003 "Whether `display-buffer' should make a separate frame.
6004 If nil, never make a separate frame.
6005 If the value is `graphic-only', make a separate frame
6006 on graphic displays only.
6007 Any other non-nil value means always make a separate frame."
6008 :type '(choice
6009 (const :tag "Never" nil)
6010 (const :tag "On graphic displays only" graphic-only)
6011 (const :tag "Always" t))
6012 :group 'windows)
6014 (defcustom display-buffer-reuse-frames nil
6015 "Non-nil means `display-buffer' should reuse frames.
6016 If the buffer in question is already displayed in a frame, raise
6017 that frame."
6018 :type 'boolean
6019 :version "21.1"
6020 :group 'windows)
6022 (make-obsolete-variable
6023 'display-buffer-reuse-frames
6024 "use a `reusable-frames' alist entry in `display-buffer-alist'."
6025 "24.3")
6027 (defcustom pop-up-windows t
6028 "Non-nil means `display-buffer' should make a new window."
6029 :type 'boolean
6030 :group 'windows)
6032 (defcustom split-window-preferred-function 'split-window-sensibly
6033 "Function called by `display-buffer' routines to split a window.
6034 This function is called with a window as single argument and is
6035 supposed to split that window and return the new window. If the
6036 window can (or shall) not be split, it is supposed to return nil.
6037 The default is to call the function `split-window-sensibly' which
6038 tries to split the window in a way which seems most suitable.
6039 You can customize the options `split-height-threshold' and/or
6040 `split-width-threshold' in order to have `split-window-sensibly'
6041 prefer either vertical or horizontal splitting.
6043 If you set this to any other function, bear in mind that the
6044 `display-buffer' routines may call this function two times. The
6045 argument of the first call is the largest window on its frame.
6046 If that call fails to return a live window, the function is
6047 called again with the least recently used window as argument. If
6048 that call fails too, `display-buffer' will use an existing window
6049 to display its buffer.
6051 The window selected at the time `display-buffer' was invoked is
6052 still selected when this function is called. Hence you can
6053 compare the window argument with the value of `selected-window'
6054 if you intend to split the selected window instead or if you do
6055 not want to split the selected window."
6056 :type 'function
6057 :version "23.1"
6058 :group 'windows)
6060 (defcustom split-height-threshold 80
6061 "Minimum height for splitting windows sensibly.
6062 If this is an integer, `split-window-sensibly' may split a window
6063 vertically only if it has at least this many lines. If this is
6064 nil, `split-window-sensibly' is not allowed to split a window
6065 vertically. If, however, a window is the only window on its
6066 frame, `split-window-sensibly' may split it vertically
6067 disregarding the value of this variable."
6068 :type '(choice (const nil) (integer :tag "lines"))
6069 :version "23.1"
6070 :group 'windows)
6072 (defcustom split-width-threshold 160
6073 "Minimum width for splitting windows sensibly.
6074 If this is an integer, `split-window-sensibly' may split a window
6075 horizontally only if it has at least this many columns. If this
6076 is nil, `split-window-sensibly' is not allowed to split a window
6077 horizontally."
6078 :type '(choice (const nil) (integer :tag "columns"))
6079 :version "23.1"
6080 :group 'windows)
6082 (defun window-splittable-p (window &optional horizontal)
6083 "Return non-nil if `split-window-sensibly' may split WINDOW.
6084 Optional argument HORIZONTAL nil or omitted means check whether
6085 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
6086 non-nil means check whether WINDOW may be split horizontally.
6088 WINDOW may be split vertically when the following conditions
6089 hold:
6090 - `window-size-fixed' is either nil or equals `width' for the
6091 buffer of WINDOW.
6092 - `split-height-threshold' is an integer and WINDOW is at least as
6093 high as `split-height-threshold'.
6094 - When WINDOW is split evenly, the emanating windows are at least
6095 `window-min-height' lines tall and can accommodate at least one
6096 line plus - if WINDOW has one - a mode line.
6098 WINDOW may be split horizontally when the following conditions
6099 hold:
6100 - `window-size-fixed' is either nil or equals `height' for the
6101 buffer of WINDOW.
6102 - `split-width-threshold' is an integer and WINDOW is at least as
6103 wide as `split-width-threshold'.
6104 - When WINDOW is split evenly, the emanating windows are at least
6105 `window-min-width' or two (whichever is larger) columns wide."
6106 (when (and (window-live-p window) (not (window--side-window-p window)))
6107 (with-current-buffer (window-buffer window)
6108 (if horizontal
6109 ;; A window can be split horizontally when its width is not
6110 ;; fixed, it is at least `split-width-threshold' columns wide
6111 ;; and at least twice as wide as `window-min-width' and 2 (the
6112 ;; latter value is hardcoded).
6113 (and (memq window-size-fixed '(nil height))
6114 ;; Testing `window-full-width-p' here hardly makes any
6115 ;; sense nowadays. This can be done more intuitively by
6116 ;; setting up `split-width-threshold' appropriately.
6117 (numberp split-width-threshold)
6118 (>= (window-width window)
6119 (max split-width-threshold
6120 (* 2 (max window-min-width 2)))))
6121 ;; A window can be split vertically when its height is not
6122 ;; fixed, it is at least `split-height-threshold' lines high,
6123 ;; and it is at least twice as high as `window-min-height' and 2
6124 ;; if it has a mode line or 1.
6125 (and (memq window-size-fixed '(nil width))
6126 (numberp split-height-threshold)
6127 (>= (window-height window)
6128 (max split-height-threshold
6129 (* 2 (max window-min-height
6130 (if mode-line-format 2 1))))))))))
6132 (defun split-window-sensibly (&optional window)
6133 "Split WINDOW in a way suitable for `display-buffer'.
6134 WINDOW defaults to the currently selected window.
6135 If `split-height-threshold' specifies an integer, WINDOW is at
6136 least `split-height-threshold' lines tall and can be split
6137 vertically, split WINDOW into two windows one above the other and
6138 return the lower window. Otherwise, if `split-width-threshold'
6139 specifies an integer, WINDOW is at least `split-width-threshold'
6140 columns wide and can be split horizontally, split WINDOW into two
6141 windows side by side and return the window on the right. If this
6142 can't be done either and WINDOW is the only window on its frame,
6143 try to split WINDOW vertically disregarding any value specified
6144 by `split-height-threshold'. If that succeeds, return the lower
6145 window. Return nil otherwise.
6147 By default `display-buffer' routines call this function to split
6148 the largest or least recently used window. To change the default
6149 customize the option `split-window-preferred-function'.
6151 You can enforce this function to not split WINDOW horizontally,
6152 by setting (or binding) the variable `split-width-threshold' to
6153 nil. If, in addition, you set `split-height-threshold' to zero,
6154 chances increase that this function does split WINDOW vertically.
6156 In order to not split WINDOW vertically, set (or bind) the
6157 variable `split-height-threshold' to nil. Additionally, you can
6158 set `split-width-threshold' to zero to make a horizontal split
6159 more likely to occur.
6161 Have a look at the function `window-splittable-p' if you want to
6162 know how `split-window-sensibly' determines whether WINDOW can be
6163 split."
6164 (let ((window (or window (selected-window))))
6165 (or (and (window-splittable-p window)
6166 ;; Split window vertically.
6167 (with-selected-window window
6168 (split-window-below)))
6169 (and (window-splittable-p window t)
6170 ;; Split window horizontally.
6171 (with-selected-window window
6172 (split-window-right)))
6173 (and (eq window (frame-root-window (window-frame window)))
6174 (not (window-minibuffer-p window))
6175 ;; If WINDOW is the only window on its frame and is not the
6176 ;; minibuffer window, try to split it vertically disregarding
6177 ;; the value of `split-height-threshold'.
6178 (let ((split-height-threshold 0))
6179 (when (window-splittable-p window)
6180 (with-selected-window window
6181 (split-window-below))))))))
6183 (defun window--try-to-split-window (window &optional alist)
6184 "Try to split WINDOW.
6185 Return value returned by `split-window-preferred-function' if it
6186 represents a live window, nil otherwise."
6187 (and (window-live-p window)
6188 (not (frame-parameter (window-frame window) 'unsplittable))
6189 (let* ((window-combination-limit
6190 ;; When `window-combination-limit' equals
6191 ;; `display-buffer' or equals `resize-window' and a
6192 ;; `window-height' or `window-width' alist entry are
6193 ;; present, bind it to t so resizing steals space
6194 ;; preferably from the window that was split.
6195 (if (or (eq window-combination-limit 'display-buffer)
6196 (and (eq window-combination-limit 'window-size)
6197 (or (cdr (assq 'window-height alist))
6198 (cdr (assq 'window-width alist)))))
6200 window-combination-limit))
6201 (new-window
6202 ;; Since `split-window-preferred-function' might
6203 ;; throw an error use `condition-case'.
6204 (condition-case nil
6205 (funcall split-window-preferred-function window)
6206 (error nil))))
6207 (and (window-live-p new-window) new-window))))
6209 (defun window--frame-usable-p (frame)
6210 "Return FRAME if it can be used to display a buffer."
6211 (when (frame-live-p frame)
6212 (let ((window (frame-root-window frame)))
6213 ;; `frame-root-window' may be an internal window which is considered
6214 ;; "dead" by `window-live-p'. Hence if `window' is not live we
6215 ;; implicitly know that `frame' has a visible window we can use.
6216 (unless (and (window-live-p window)
6217 (or (window-minibuffer-p window)
6218 ;; If the window is soft-dedicated, the frame is usable.
6219 ;; Actually, even if the window is really dedicated,
6220 ;; the frame is still usable by splitting it.
6221 ;; At least Emacs-22 allowed it, and it is desirable
6222 ;; when displaying same-frame windows.
6223 nil ; (eq t (window-dedicated-p window))
6225 frame))))
6227 (defcustom even-window-sizes t
6228 "If non-nil `display-buffer' will try to even window sizes.
6229 Otherwise `display-buffer' will leave the window configuration
6230 alone. Special values are `height-only' to even heights only and
6231 `width-only' to even widths only. Any other value means to even
6232 any of them."
6233 :type '(choice
6234 (const :tag "Never" nil)
6235 (const :tag "Side-by-side windows only" width-only)
6236 (const :tag "Windows above or below only" height-only)
6237 (const :tag "Always" t))
6238 :version "25.1"
6239 :group 'windows)
6240 (defvaralias 'even-window-heights 'even-window-sizes)
6242 (defun window--even-window-sizes (window)
6243 "Even sizes of WINDOW and selected window.
6244 Even only if these windows are the only children of their parent,
6245 `even-window-sizes' has the appropriate value and the selected
6246 window is larger than WINDOW."
6247 (when (and (= (window-child-count (window-parent window)) 2)
6248 (eq (window-parent) (window-parent window)))
6249 (cond
6250 ((and (not (memq even-window-sizes '(nil height-only)))
6251 (window-combined-p window t)
6252 (> (window-total-width) (window-total-width window)))
6253 (condition-case nil
6254 (enlarge-window
6255 (/ (- (window-total-width window) (window-total-width)) 2) t)
6256 (error nil)))
6257 ((and (not (memq even-window-sizes '(nil width-only)))
6258 (window-combined-p window)
6259 (> (window-total-height) (window-total-height window)))
6260 (condition-case nil
6261 (enlarge-window
6262 (/ (- (window-total-height window) (window-total-height)) 2))
6263 (error nil))))))
6265 (defun window--display-buffer (buffer window type &optional alist dedicated)
6266 "Display BUFFER in WINDOW.
6267 TYPE must be one of the symbols `reuse', `window' or `frame' and
6268 is passed unaltered to `display-buffer-record-window'. ALIST is
6269 the alist argument of `display-buffer'. Set `window-dedicated-p'
6270 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
6271 live."
6272 (when (and (buffer-live-p buffer) (window-live-p window))
6273 (display-buffer-record-window type window buffer)
6274 (unless (eq buffer (window-buffer window))
6275 (set-window-dedicated-p window nil)
6276 (set-window-buffer window buffer)
6277 (when dedicated
6278 (set-window-dedicated-p window dedicated))
6279 (when (memq type '(window frame))
6280 (set-window-prev-buffers window nil)))
6281 (let ((parameter (window-parameter window 'quit-restore))
6282 (height (cdr (assq 'window-height alist)))
6283 (width (cdr (assq 'window-width alist)))
6284 (size (cdr (assq 'window-size alist)))
6285 (preserve-size (cdr (assq 'preserve-size alist))))
6286 (cond
6287 ((or (eq type 'frame)
6288 (and (eq (car parameter) 'same)
6289 (eq (nth 1 parameter) 'frame)))
6290 ;; Adjust size of frame if asked for.
6291 (cond
6292 ((not size))
6293 ((consp size)
6294 (let ((width (car size))
6295 (height (cdr size))
6296 (frame (window-frame window)))
6297 (when (and (numberp width) (numberp height))
6298 (set-frame-height
6299 frame (+ (frame-height frame)
6300 (- height (window-total-height window))))
6301 (set-frame-width
6302 frame (+ (frame-width frame)
6303 (- width (window-total-width window)))))))
6304 ((functionp size)
6305 (ignore-errors (funcall size window)))))
6306 ((or (eq type 'window)
6307 (and (eq (car parameter) 'same)
6308 (eq (nth 1 parameter) 'window)))
6309 ;; Adjust height of window if asked for.
6310 (cond
6311 ((not height))
6312 ((numberp height)
6313 (let* ((new-height
6314 (if (integerp height)
6315 height
6316 (round
6317 (* (window-total-height (frame-root-window window))
6318 height))))
6319 (delta (- new-height (window-total-height window))))
6320 (when (and (window--resizable-p window delta nil 'safe)
6321 (window-combined-p window))
6322 (window-resize window delta nil 'safe))))
6323 ((functionp height)
6324 (ignore-errors (funcall height window))))
6325 ;; Adjust width of window if asked for.
6326 (cond
6327 ((not width))
6328 ((numberp width)
6329 (let* ((new-width
6330 (if (integerp width)
6331 width
6332 (round
6333 (* (window-total-width (frame-root-window window))
6334 width))))
6335 (delta (- new-width (window-total-width window))))
6336 (when (and (window--resizable-p window delta t 'safe)
6337 (window-combined-p window t))
6338 (window-resize window delta t 'safe))))
6339 ((functionp width)
6340 (ignore-errors (funcall width window))))
6341 ;; Preserve window size if asked for.
6342 (when (consp preserve-size)
6343 (window-preserve-size window t (car preserve-size))
6344 (window-preserve-size window nil (cdr preserve-size))))))
6346 window))
6348 (defun window--maybe-raise-frame (frame)
6349 (let ((visible (frame-visible-p frame)))
6350 (unless (or (not visible)
6351 ;; Assume the selected frame is already visible enough.
6352 (eq frame (selected-frame))
6353 ;; Assume the frame from which we invoked the
6354 ;; minibuffer is visible.
6355 (and (minibuffer-window-active-p (selected-window))
6356 (eq frame (window-frame (minibuffer-selected-window)))))
6357 (raise-frame frame))))
6359 ;; FIXME: Not implemented.
6360 ;; FIXME: By the way, there could be more levels of dedication:
6361 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
6362 ;; the window hasn't been used for something else yet.
6363 ;; - `soft' (`softly') dedicated only allows reuse when asked explicitly.
6364 ;; - `strongly' never allows reuse.
6365 (defvar display-buffer-mark-dedicated nil
6366 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
6367 The actual non-nil value of this variable will be copied to the
6368 `window-dedicated-p' flag.")
6370 (defconst display-buffer--action-function-custom-type
6371 '(choice :tag "Function"
6372 (const :tag "--" ignore) ; default for insertion
6373 (const display-buffer-reuse-window)
6374 (const display-buffer-pop-up-window)
6375 (const display-buffer-same-window)
6376 (const display-buffer-pop-up-frame)
6377 (const display-buffer-below-selected)
6378 (const display-buffer-at-bottom)
6379 (const display-buffer-in-previous-window)
6380 (const display-buffer-use-some-window)
6381 (const display-buffer-use-some-frame)
6382 (function :tag "Other function"))
6383 "Custom type for `display-buffer' action functions.")
6385 (defconst display-buffer--action-custom-type
6386 `(cons :tag "Action"
6387 (choice :tag "Action functions"
6388 ,display-buffer--action-function-custom-type
6389 (repeat
6390 :tag "List of functions"
6391 ,display-buffer--action-function-custom-type))
6392 (alist :tag "Action arguments"
6393 :key-type symbol
6394 :value-type (sexp :tag "Value")))
6395 "Custom type for `display-buffer' actions.")
6397 (defvar display-buffer-overriding-action '(nil . nil)
6398 "Overriding action to perform to display a buffer.
6399 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6400 function or a list of functions. Each function should accept two
6401 arguments: a buffer to display and an alist similar to ALIST.
6402 See `display-buffer' for details.")
6403 (put 'display-buffer-overriding-action 'risky-local-variable t)
6405 (defcustom display-buffer-alist nil
6406 "Alist of conditional actions for `display-buffer'.
6407 This is a list of elements (CONDITION . ACTION), where:
6409 CONDITION is either a regexp matching buffer names, or a
6410 function that takes two arguments - a buffer name and the
6411 ACTION argument of `display-buffer' - and returns a boolean.
6413 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
6414 function or a list of functions. Each such function should
6415 accept two arguments: a buffer to display and an alist of the
6416 same form as ALIST. See `display-buffer' for details.
6418 `display-buffer' scans this alist until it either finds a
6419 matching regular expression or the function specified by a
6420 condition returns non-nil. In any of these cases, it adds the
6421 associated action to the list of actions it will try."
6422 :type `(alist :key-type
6423 (choice :tag "Condition"
6424 regexp
6425 (function :tag "Matcher function"))
6426 :value-type ,display-buffer--action-custom-type)
6427 :risky t
6428 :version "24.1"
6429 :group 'windows)
6431 (defcustom display-buffer-base-action '(nil . nil)
6432 "User-specified default action for `display-buffer'.
6433 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6434 function or a list of functions. Each function should accept two
6435 arguments: a buffer to display and an alist similar to ALIST.
6436 See `display-buffer' for details."
6437 :type display-buffer--action-custom-type
6438 :risky t
6439 :version "24.1"
6440 :group 'windows)
6442 (defconst display-buffer-fallback-action
6443 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
6444 display-buffer-reuse-window
6445 display-buffer--maybe-pop-up-frame-or-window
6446 display-buffer-in-previous-window
6447 display-buffer-use-some-window
6448 ;; If all else fails, pop up a new frame.
6449 display-buffer-pop-up-frame))
6450 "Default fallback action for `display-buffer'.
6451 This is the action used by `display-buffer' if no other actions
6452 specified, e.g. by the user options `display-buffer-alist' or
6453 `display-buffer-base-action'. See `display-buffer'.")
6454 (put 'display-buffer-fallback-action 'risky-local-variable t)
6456 (defun display-buffer-assq-regexp (buffer-name alist action)
6457 "Retrieve ALIST entry corresponding to BUFFER-NAME.
6458 ACTION is the action argument passed to `display-buffer'."
6459 (catch 'match
6460 (dolist (entry alist)
6461 (let ((key (car entry)))
6462 (when (or (and (stringp key)
6463 (string-match-p key buffer-name))
6464 (and (functionp key)
6465 (funcall key buffer-name action)))
6466 (throw 'match (cdr entry)))))))
6468 (defvar display-buffer--same-window-action
6469 '(display-buffer-same-window
6470 (inhibit-same-window . nil))
6471 "A `display-buffer' action for displaying in the same window.")
6472 (put 'display-buffer--same-window-action 'risky-local-variable t)
6474 (defvar display-buffer--other-frame-action
6475 '((display-buffer-reuse-window
6476 display-buffer-pop-up-frame)
6477 (reusable-frames . 0)
6478 (inhibit-same-window . t))
6479 "A `display-buffer' action for displaying in another frame.")
6480 (put 'display-buffer--other-frame-action 'risky-local-variable t)
6482 (defun display-buffer (buffer-or-name &optional action frame)
6483 "Display BUFFER-OR-NAME in some window, without selecting it.
6484 BUFFER-OR-NAME must be a buffer or the name of an existing
6485 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
6486 or nil if no such window is found.
6488 Optional argument ACTION, if non-nil, should specify a display
6489 action. Its form is described below.
6491 Optional argument FRAME, if non-nil, acts like an additional
6492 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
6493 specifying the frame(s) to search for a window that is already
6494 displaying the buffer. See `display-buffer-reuse-window'
6496 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
6497 where FUNCTION is either a function or a list of functions, and
6498 ALIST is an arbitrary association list (alist).
6500 Each such FUNCTION should accept two arguments: the buffer to
6501 display and an alist. Based on those arguments, it should
6502 display the buffer and return the window. If the caller is
6503 prepared to handle the case of not displaying the buffer
6504 and returning nil from `display-buffer' it should pass
6505 \(allow-no-window . t) as an element of the ALIST.
6507 The `display-buffer' function builds a function list and an alist
6508 by combining the functions and alists specified in
6509 `display-buffer-overriding-action', `display-buffer-alist', the
6510 ACTION argument, `display-buffer-base-action', and
6511 `display-buffer-fallback-action' (in order). Then it calls each
6512 function in the combined function list in turn, passing the
6513 buffer as the first argument and the combined alist as the second
6514 argument, until one of the functions returns non-nil.
6516 If ACTION is nil, the function list and the alist are built using
6517 only the other variables mentioned above.
6519 Available action functions include:
6520 `display-buffer-same-window'
6521 `display-buffer-reuse-window'
6522 `display-buffer-pop-up-frame'
6523 `display-buffer-pop-up-window'
6524 `display-buffer-in-previous-window'
6525 `display-buffer-use-some-window'
6526 `display-buffer-use-some-frame'
6528 Recognized alist entries include:
6530 `inhibit-same-window' -- A non-nil value prevents the same
6531 window from being used for display.
6533 `inhibit-switch-frame' -- A non-nil value prevents any other
6534 frame from being raised or selected,
6535 even if the window is displayed there.
6537 `reusable-frames' -- Value specifies frame(s) to search for a
6538 window that already displays the buffer.
6539 See `display-buffer-reuse-window'.
6541 `pop-up-frame-parameters' -- Value specifies an alist of frame
6542 parameters to give a new frame, if
6543 one is created.
6545 `window-height' -- Value specifies either an integer (the number
6546 of lines of a new window), a floating point number (the
6547 fraction of a new window with respect to the height of the
6548 frame's root window) or a function to be called with one
6549 argument - a new window. The function is supposed to adjust
6550 the height of the window; its return value is ignored.
6551 Suitable functions are `shrink-window-if-larger-than-buffer'
6552 and `fit-window-to-buffer'.
6554 `window-width' -- Value specifies either an integer (the number
6555 of columns of a new window), a floating point number (the
6556 fraction of a new window with respect to the width of the
6557 frame's root window) or a function to be called with one
6558 argument - a new window. The function is supposed to adjust
6559 the width of the window; its return value is ignored.
6561 `allow-no-window' -- A non-nil value indicates readiness for the case
6562 of not displaying the buffer and FUNCTION can safely return
6563 a non-window value to suppress displaying.
6565 `preserve-size' -- Value should be either '(t . nil)' to
6566 preserve the width of the window, '(nil . t)' to preserve its
6567 height or '(t . t)' to preserve both.
6569 The ACTION argument to `display-buffer' can also have a non-nil
6570 and non-list value. This means to display the buffer in a window
6571 other than the selected one, even if it is already displayed in
6572 the selected window. If called interactively with a prefix
6573 argument, ACTION is t."
6574 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6575 (if current-prefix-arg t)))
6576 (let ((buffer (if (bufferp buffer-or-name)
6577 buffer-or-name
6578 (get-buffer buffer-or-name)))
6579 ;; Make sure that when we split windows the old window keeps
6580 ;; point, bug#14829.
6581 (split-window-keep-point t)
6582 ;; Handle the old form of the first argument.
6583 (inhibit-same-window (and action (not (listp action)))))
6584 (unless (listp action) (setq action nil))
6585 (if display-buffer-function
6586 ;; If `display-buffer-function' is defined, let it do the job.
6587 (funcall display-buffer-function buffer inhibit-same-window)
6588 ;; Otherwise, use the defined actions.
6589 (let* ((user-action
6590 (display-buffer-assq-regexp
6591 (buffer-name buffer) display-buffer-alist action))
6592 (special-action (display-buffer--special-action buffer))
6593 ;; Extra actions from the arguments to this function:
6594 (extra-action
6595 (cons nil (append (if inhibit-same-window
6596 '((inhibit-same-window . t)))
6597 (if frame
6598 `((reusable-frames . ,frame))))))
6599 ;; Construct action function list and action alist.
6600 (actions (list display-buffer-overriding-action
6601 user-action special-action action extra-action
6602 display-buffer-base-action
6603 display-buffer-fallback-action))
6604 (functions (apply 'append
6605 (mapcar (lambda (x)
6606 (setq x (car x))
6607 (if (functionp x) (list x) x))
6608 actions)))
6609 (alist (apply 'append (mapcar 'cdr actions)))
6610 window)
6611 (unless (buffer-live-p buffer)
6612 (error "Invalid buffer"))
6613 (while (and functions (not window))
6614 (setq window (funcall (car functions) buffer alist)
6615 functions (cdr functions)))
6616 (and (windowp window) window)))))
6618 (defun display-buffer-other-frame (buffer)
6619 "Display buffer BUFFER preferably in another frame.
6620 This uses the function `display-buffer' as a subroutine; see
6621 its documentation for additional customization information."
6622 (interactive "BDisplay buffer in other frame: ")
6623 (display-buffer buffer display-buffer--other-frame-action t))
6625 ;;; `display-buffer' action functions:
6627 (defun display-buffer-use-some-frame (buffer alist)
6628 "Display BUFFER in an existing frame that meets a predicate
6629 \(by default any frame other than the current frame). If
6630 successful, return the window used; otherwise return nil.
6632 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
6633 raising the frame.
6635 If ALIST has a non-nil `frame-predicate' entry, its value is a
6636 function taking one argument (a frame), returning non-nil if the
6637 frame is a candidate; this function replaces the default
6638 predicate.
6640 If ALIST has a non-nil `inhibit-same-window' entry, avoid using
6641 the currently selected window (only useful with a frame-predicate
6642 that allows the selected frame)."
6643 (let* ((predicate (or (cdr (assq 'frame-predicate alist))
6644 (lambda (frame)
6645 (and
6646 (not (eq frame (selected-frame)))
6647 (not (window-dedicated-p
6649 (get-lru-window frame)
6650 (frame-first-window frame)))))
6652 (frame (car (filtered-frame-list predicate)))
6653 (window (and frame (get-lru-window frame nil (cdr (assq 'inhibit-same-window alist))))))
6654 (when window
6655 (prog1
6656 (window--display-buffer
6657 buffer window 'frame alist display-buffer-mark-dedicated)
6658 (unless (cdr (assq 'inhibit-switch-frame alist))
6659 (window--maybe-raise-frame frame))))
6662 (defun display-buffer-same-window (buffer alist)
6663 "Display BUFFER in the selected window.
6664 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
6665 if the selected window is a minibuffer window or is dedicated to
6666 another buffer; in that case, return nil. Otherwise, return the
6667 selected window."
6668 (unless (or (cdr (assq 'inhibit-same-window alist))
6669 (window-minibuffer-p)
6670 (window-dedicated-p))
6671 (window--display-buffer buffer (selected-window) 'reuse alist)))
6673 (defun display-buffer--maybe-same-window (buffer alist)
6674 "Conditionally display BUFFER in the selected window.
6675 If `same-window-p' returns non-nil for BUFFER's name, call
6676 `display-buffer-same-window' and return its value. Otherwise,
6677 return nil."
6678 (and (same-window-p (buffer-name buffer))
6679 (display-buffer-same-window buffer alist)))
6681 (defun display-buffer-reuse-window (buffer alist)
6682 "Return a window that is already displaying BUFFER.
6683 Return nil if no usable window is found.
6685 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6686 window is not eligible for reuse.
6688 If ALIST contains a `reusable-frames' entry, its value determines
6689 which frames to search for a reusable window:
6690 nil -- the selected frame (actually the last non-minibuffer frame)
6691 A frame -- just that frame
6692 `visible' -- all visible frames
6693 0 -- all frames on the current terminal
6694 t -- all frames.
6696 If ALIST contains no `reusable-frames' entry, search just the
6697 selected frame if `display-buffer-reuse-frames' and
6698 `pop-up-frames' are both nil; search all frames on the current
6699 terminal if either of those variables is non-nil.
6701 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6702 event that a window on another frame is chosen, avoid raising
6703 that frame."
6704 (let* ((alist-entry (assq 'reusable-frames alist))
6705 (frames (cond (alist-entry (cdr alist-entry))
6706 ((if (eq pop-up-frames 'graphic-only)
6707 (display-graphic-p)
6708 pop-up-frames)
6710 (display-buffer-reuse-frames 0)
6711 (t (last-nonminibuffer-frame))))
6712 (window (if (and (eq buffer (window-buffer))
6713 (not (cdr (assq 'inhibit-same-window alist))))
6714 (selected-window)
6715 (car (delq (selected-window)
6716 (get-buffer-window-list buffer 'nomini
6717 frames))))))
6718 (when (window-live-p window)
6719 (prog1 (window--display-buffer buffer window 'reuse alist)
6720 (unless (cdr (assq 'inhibit-switch-frame alist))
6721 (window--maybe-raise-frame (window-frame window)))))))
6723 (defun display-buffer--special-action (buffer)
6724 "Return special display action for BUFFER, if any.
6725 If `special-display-p' returns non-nil for BUFFER, return an
6726 appropriate display action involving `special-display-function'.
6727 See `display-buffer' for the format of display actions."
6728 (and special-display-function
6729 ;; `special-display-p' returns either t or a list of frame
6730 ;; parameters to pass to `special-display-function'.
6731 (let ((pars (special-display-p (buffer-name buffer))))
6732 (when pars
6733 (list (list #'display-buffer-reuse-window
6734 `(lambda (buffer _alist)
6735 (funcall special-display-function
6736 buffer ',(if (listp pars) pars)))))))))
6738 (defun display-buffer-pop-up-frame (buffer alist)
6739 "Display BUFFER in a new frame.
6740 This works by calling `pop-up-frame-function'. If successful,
6741 return the window used; otherwise return nil.
6743 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
6744 raising the new frame.
6746 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
6747 corresponding value is an alist of frame parameters to give the
6748 new frame."
6749 (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
6750 (pop-up-frame-alist (append params pop-up-frame-alist))
6751 (fun pop-up-frame-function)
6752 frame window)
6753 (when (and fun
6754 ;; Make BUFFER current so `make-frame' will use it as the
6755 ;; new frame's buffer (Bug#15133).
6756 (with-current-buffer buffer
6757 (setq frame (funcall fun)))
6758 (setq window (frame-selected-window frame)))
6759 (prog1 (window--display-buffer
6760 buffer window 'frame alist display-buffer-mark-dedicated)
6761 (unless (cdr (assq 'inhibit-switch-frame alist))
6762 (window--maybe-raise-frame frame))))))
6764 (defun display-buffer-pop-up-window (buffer alist)
6765 "Display BUFFER by popping up a new window.
6766 The new window is created on the selected frame, or in
6767 `last-nonminibuffer-frame' if no windows can be created there.
6768 If successful, return the new window; otherwise return nil.
6770 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6771 event that the new window is created on another frame, avoid
6772 raising the frame."
6773 (let ((frame (or (window--frame-usable-p (selected-frame))
6774 (window--frame-usable-p (last-nonminibuffer-frame))))
6775 window)
6776 (when (and (or (not (frame-parameter frame 'unsplittable))
6777 ;; If the selected frame cannot be split, look at
6778 ;; `last-nonminibuffer-frame'.
6779 (and (eq frame (selected-frame))
6780 (setq frame (last-nonminibuffer-frame))
6781 (window--frame-usable-p frame)
6782 (not (frame-parameter frame 'unsplittable))))
6783 ;; Attempt to split largest or least recently used window.
6784 (setq window (or (window--try-to-split-window
6785 (get-largest-window frame t) alist)
6786 (window--try-to-split-window
6787 (get-lru-window frame t) alist))))
6788 (prog1 (window--display-buffer
6789 buffer window 'window alist display-buffer-mark-dedicated)
6790 (unless (cdr (assq 'inhibit-switch-frame alist))
6791 (window--maybe-raise-frame (window-frame window)))))))
6793 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
6794 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
6796 If `pop-up-frames' is non-nil (and not `graphic-only' on a
6797 text-only terminal), try with `display-buffer-pop-up-frame'.
6799 If that cannot be done, and `pop-up-windows' is non-nil, try
6800 again with `display-buffer-pop-up-window'."
6801 (or (and (if (eq pop-up-frames 'graphic-only)
6802 (display-graphic-p)
6803 pop-up-frames)
6804 (display-buffer-pop-up-frame buffer alist))
6805 (and pop-up-windows
6806 (display-buffer-pop-up-window buffer alist))))
6808 (defun display-buffer-below-selected (buffer alist)
6809 "Try displaying BUFFER in a window below the selected window.
6810 This either splits the selected window or reuses the window below
6811 the selected one."
6812 (let (window)
6813 (or (and (setq window (window-in-direction 'below))
6814 (eq buffer (window-buffer window))
6815 (window--display-buffer buffer window 'reuse alist))
6816 (and (not (frame-parameter nil 'unsplittable))
6817 (let ((split-height-threshold 0)
6818 split-width-threshold)
6819 (setq window (window--try-to-split-window (selected-window) alist)))
6820 (window--display-buffer
6821 buffer window 'window alist display-buffer-mark-dedicated))
6822 (and (setq window (window-in-direction 'below))
6823 (not (window-dedicated-p window))
6824 (window--display-buffer
6825 buffer window 'reuse alist display-buffer-mark-dedicated)))))
6827 (defun display-buffer-at-bottom (buffer alist)
6828 "Try displaying BUFFER in a window at the bottom of the selected frame.
6829 This either reuses such a window provided it shows BUFFER
6830 already, splits a window at the bottom of the frame or the
6831 frame's root window, or reuses some window at the bottom of the
6832 selected frame."
6833 (let (bottom-window bottom-window-shows-buffer window)
6834 (walk-window-tree
6835 (lambda (window)
6836 (cond
6837 ((window-in-direction 'below window))
6838 ((and (not bottom-window-shows-buffer)
6839 (eq buffer (window-buffer window)))
6840 (setq bottom-window-shows-buffer t)
6841 (setq bottom-window window))
6842 ((not bottom-window)
6843 (setq bottom-window window)))
6844 nil nil 'nomini))
6845 (or (and bottom-window-shows-buffer
6846 (window--display-buffer
6847 buffer bottom-window 'reuse alist display-buffer-mark-dedicated))
6848 (and (not (frame-parameter nil 'unsplittable))
6849 (let (split-width-threshold)
6850 (setq window (window--try-to-split-window bottom-window alist)))
6851 (window--display-buffer
6852 buffer window 'window alist display-buffer-mark-dedicated))
6853 (and (not (frame-parameter nil 'unsplittable))
6854 (setq window
6855 (condition-case nil
6856 (split-window (window--major-non-side-window))
6857 (error nil)))
6858 (window--display-buffer
6859 buffer window 'window alist display-buffer-mark-dedicated))
6860 (and (setq window bottom-window)
6861 (not (window-dedicated-p window))
6862 (window--display-buffer
6863 buffer window 'reuse alist display-buffer-mark-dedicated)))))
6865 (defun display-buffer-in-previous-window (buffer alist)
6866 "Display BUFFER in a window previously showing it.
6867 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6868 window is not eligible for reuse.
6870 If ALIST contains a `reusable-frames' entry, its value determines
6871 which frames to search for a reusable window:
6872 nil -- the selected frame (actually the last non-minibuffer frame)
6873 A frame -- just that frame
6874 `visible' -- all visible frames
6875 0 -- all frames on the current terminal
6876 t -- all frames.
6878 If ALIST contains no `reusable-frames' entry, search just the
6879 selected frame if `display-buffer-reuse-frames' and
6880 `pop-up-frames' are both nil; search all frames on the current
6881 terminal if either of those variables is non-nil.
6883 If ALIST has a `previous-window' entry, the window specified by
6884 that entry will override any other window found by the methods
6885 above, even if that window never showed BUFFER before."
6886 (let* ((alist-entry (assq 'reusable-frames alist))
6887 (inhibit-same-window
6888 (cdr (assq 'inhibit-same-window alist)))
6889 (frames (cond
6890 (alist-entry (cdr alist-entry))
6891 ((if (eq pop-up-frames 'graphic-only)
6892 (display-graphic-p)
6893 pop-up-frames)
6895 (display-buffer-reuse-frames 0)
6896 (t (last-nonminibuffer-frame))))
6897 best-window second-best-window window)
6898 ;; Scan windows whether they have shown the buffer recently.
6899 (catch 'best
6900 (dolist (window (window-list-1 (frame-first-window) 'nomini frames))
6901 (when (and (assq buffer (window-prev-buffers window))
6902 (not (window-dedicated-p window)))
6903 (if (eq window (selected-window))
6904 (unless inhibit-same-window
6905 (setq second-best-window window))
6906 (setq best-window window)
6907 (throw 'best t)))))
6908 ;; When ALIST has a `previous-window' entry, that entry may override
6909 ;; anything we found so far.
6910 (when (and (setq window (cdr (assq 'previous-window alist)))
6911 (window-live-p window)
6912 (not (window-dedicated-p window)))
6913 (if (eq window (selected-window))
6914 (unless inhibit-same-window
6915 (setq second-best-window window))
6916 (setq best-window window)))
6917 ;; Return best or second best window found.
6918 (when (setq window (or best-window second-best-window))
6919 (window--display-buffer buffer window 'reuse alist))))
6921 (defun display-buffer-use-some-window (buffer alist)
6922 "Display BUFFER in an existing window.
6923 Search for a usable window, set that window to the buffer, and
6924 return the window. If no suitable window is found, return nil.
6926 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6927 event that a window in another frame is chosen, avoid raising
6928 that frame."
6929 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
6930 (frame (or (window--frame-usable-p (selected-frame))
6931 (window--frame-usable-p (last-nonminibuffer-frame))))
6932 (window
6933 ;; Reuse an existing window.
6934 (or (get-lru-window frame nil not-this-window)
6935 (let ((window (get-buffer-window buffer 'visible)))
6936 (unless (and not-this-window
6937 (eq window (selected-window)))
6938 window))
6939 (get-largest-window 'visible nil not-this-window)
6940 (let ((window (get-buffer-window buffer 0)))
6941 (unless (and not-this-window
6942 (eq window (selected-window)))
6943 window))
6944 (get-largest-window 0 nil not-this-window)))
6945 (quit-restore (and (window-live-p window)
6946 (window-parameter window 'quit-restore)))
6947 (quad (nth 1 quit-restore)))
6948 (when (window-live-p window)
6949 ;; If the window was used by `display-buffer' before, try to
6950 ;; resize it to its old height but don't signal an error.
6951 (when (and (listp quad)
6952 (integerp (nth 3 quad))
6953 (> (nth 3 quad) (window-total-height window)))
6954 (condition-case nil
6955 (window-resize window (- (nth 3 quad) (window-total-height window)))
6956 (error nil)))
6958 (prog1
6959 (window--display-buffer buffer window 'reuse alist)
6960 (window--even-window-sizes window)
6961 (unless (cdr (assq 'inhibit-switch-frame alist))
6962 (window--maybe-raise-frame (window-frame window)))))))
6964 (defun display-buffer-no-window (_buffer alist)
6965 "Display BUFFER in no window.
6966 If ALIST has a non-nil `allow-no-window' entry, then don't display
6967 a window at all. This makes possible to override the default action
6968 and avoid displaying the buffer. It is assumed that when the caller
6969 specifies a non-nil `allow-no-window' then it can handle a nil value
6970 returned from `display-buffer' in this case."
6971 (when (cdr (assq 'allow-no-window alist))
6972 'fail))
6974 ;;; Display + selection commands:
6975 (defun pop-to-buffer (buffer &optional action norecord)
6976 "Select buffer BUFFER in some window, preferably a different one.
6977 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6978 is a string not naming an existent buffer, create a buffer with
6979 that name. If BUFFER is nil, choose some other buffer. Return
6980 the buffer.
6982 This uses `display-buffer' as a subroutine. The optional ACTION
6983 argument is passed to `display-buffer' as its ACTION argument.
6984 See `display-buffer' for more information. ACTION is t if called
6985 interactively with a prefix argument, which means to pop to a
6986 window other than the selected one even if the buffer is already
6987 displayed in the selected window.
6989 If the window to show BUFFER is not on the selected
6990 frame, raise that window's frame and give it input focus.
6992 Optional third arg NORECORD non-nil means do not put this buffer
6993 at the front of the list of recently selected ones."
6994 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
6995 (if current-prefix-arg t)))
6996 (setq buffer (window-normalize-buffer-to-switch-to buffer))
6997 ;; This should be done by `select-window' below.
6998 ;; (set-buffer buffer)
6999 (let* ((old-frame (selected-frame))
7000 (window (display-buffer buffer action))
7001 (frame (window-frame window)))
7002 ;; If we chose another frame, make sure it gets input focus.
7003 (unless (eq frame old-frame)
7004 (select-frame-set-input-focus frame norecord))
7005 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
7006 (select-window window norecord)
7007 buffer))
7009 (defun pop-to-buffer-same-window (buffer &optional norecord)
7010 "Select buffer BUFFER in some window, preferably the same one.
7011 BUFFER may be a buffer, a string (a buffer name), or nil. If it
7012 is a string not naming an existent buffer, create a buffer with
7013 that name. If BUFFER is nil, choose some other buffer. Return
7014 the buffer.
7016 Optional argument NORECORD, if non-nil means do not put this
7017 buffer at the front of the list of recently selected ones.
7019 Unlike `pop-to-buffer', this function prefers using the selected
7020 window over popping up a new window or frame."
7021 (pop-to-buffer buffer display-buffer--same-window-action norecord))
7023 (defun read-buffer-to-switch (prompt)
7024 "Read the name of a buffer to switch to, prompting with PROMPT.
7025 Return the name of the buffer as a string.
7027 This function is intended for the `switch-to-buffer' family of
7028 commands since these need to omit the name of the current buffer
7029 from the list of completions and default values."
7030 (let ((rbts-completion-table (internal-complete-buffer-except)))
7031 (minibuffer-with-setup-hook
7032 (lambda ()
7033 (setq minibuffer-completion-table rbts-completion-table)
7034 ;; Since rbts-completion-table is built dynamically, we
7035 ;; can't just add it to the default value of
7036 ;; icomplete-with-completion-tables, so we add it
7037 ;; here manually.
7038 (if (and (boundp 'icomplete-with-completion-tables)
7039 (listp icomplete-with-completion-tables))
7040 (set (make-local-variable 'icomplete-with-completion-tables)
7041 (cons rbts-completion-table
7042 icomplete-with-completion-tables))))
7043 (read-buffer prompt (other-buffer (current-buffer))
7044 (confirm-nonexistent-file-or-buffer)))))
7046 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
7047 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
7048 If BUFFER-OR-NAME is nil, return the buffer returned by
7049 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
7050 exists, return that buffer. If no such buffer exists, create a
7051 buffer with the name BUFFER-OR-NAME and return that buffer."
7052 (if buffer-or-name
7053 (or (get-buffer buffer-or-name)
7054 (let ((buffer (get-buffer-create buffer-or-name)))
7055 (set-buffer-major-mode buffer)
7056 buffer))
7057 (other-buffer)))
7059 (defcustom switch-to-buffer-preserve-window-point nil
7060 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
7061 If this is nil, `switch-to-buffer' displays the buffer at that
7062 buffer's `point'. If this is `already-displayed', it tries to
7063 display the buffer at its previous position in the selected
7064 window, provided the buffer is currently displayed in some other
7065 window on any visible or iconified frame. If this is t, it
7066 unconditionally tries to display the buffer at its previous
7067 position in the selected window.
7069 This variable is ignored if the buffer is already displayed in
7070 the selected window or never appeared in it before, or if
7071 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
7072 :type '(choice
7073 (const :tag "Never" nil)
7074 (const :tag "If already displayed elsewhere" already-displayed)
7075 (const :tag "Always" t))
7076 :group 'windows
7077 :version "24.3")
7079 (defcustom switch-to-buffer-in-dedicated-window nil
7080 "Allow switching to buffer in strongly dedicated windows.
7081 If non-nil, allow `switch-to-buffer' to proceed when called
7082 interactively and the selected window is strongly dedicated to
7083 its buffer.
7085 The following values are recognized:
7087 nil - disallow switching; signal an error
7089 prompt - prompt user whether to allow switching
7091 pop - perform `pop-to-buffer' instead
7093 t - undedicate selected window and switch
7095 When called non-interactively, `switch-to-buffer' always signals
7096 an error when the selected window is dedicated to its buffer and
7097 FORCE-SAME-WINDOW is non-nil."
7098 :type '(choice
7099 (const :tag "Disallow" nil)
7100 (const :tag "Prompt" prompt)
7101 (const :tag "Pop" pop)
7102 (const :tag "Allow" t))
7103 :group 'windows
7104 :version "25.1")
7106 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
7107 "Display buffer BUFFER-OR-NAME in the selected window.
7109 WARNING: This is NOT the way to work on another buffer temporarily
7110 within a Lisp program! Use `set-buffer' instead. That avoids
7111 messing with the window-buffer correspondences.
7113 If the selected window cannot display the specified buffer
7114 because it is a minibuffer window or strongly dedicated to
7115 another buffer, call `pop-to-buffer' to select the buffer in
7116 another window. In interactive use, if the selected window is
7117 strongly dedicated to its buffer, the value of the option
7118 `switch-to-buffer-in-dedicated-window' specifies how to proceed.
7120 If called interactively, read the buffer name using the
7121 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7122 determines whether to request confirmation before creating a new
7123 buffer.
7125 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
7126 If BUFFER-OR-NAME is a string that does not identify an existing
7127 buffer, create a buffer with that name. If BUFFER-OR-NAME is
7128 nil, switch to the buffer returned by `other-buffer'.
7130 If optional argument NORECORD is non-nil, do not put the buffer
7131 at the front of the buffer list, and do not make the window
7132 displaying it the most recently selected one.
7134 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
7135 must be displayed in the selected window when called
7136 non-interactively; if that is impossible, signal an error rather
7137 than calling `pop-to-buffer'.
7139 The option `switch-to-buffer-preserve-window-point' can be used
7140 to make the buffer appear at its last position in the selected
7141 window.
7143 Return the buffer switched to."
7144 (interactive
7145 (let ((force-same-window
7146 (cond
7147 ((window-minibuffer-p) nil)
7148 ((not (eq (window-dedicated-p) t)) 'force-same-window)
7149 ((pcase switch-to-buffer-in-dedicated-window
7150 (`nil (user-error
7151 "Cannot switch buffers in a dedicated window"))
7152 (`prompt
7153 (if (y-or-n-p
7154 (format "Window is dedicated to %s; undedicate it"
7155 (window-buffer)))
7156 (progn
7157 (set-window-dedicated-p nil nil)
7158 'force-same-window)
7159 (user-error
7160 "Cannot switch buffers in a dedicated window")))
7161 (`pop nil)
7162 (_ (set-window-dedicated-p nil nil) 'force-same-window))))))
7163 (list (read-buffer-to-switch "Switch to buffer: ") nil force-same-window)))
7164 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
7165 (cond
7166 ;; Don't call set-window-buffer if it's not needed since it
7167 ;; might signal an error (e.g. if the window is dedicated).
7168 ((eq buffer (window-buffer)))
7169 ((window-minibuffer-p)
7170 (if force-same-window
7171 (user-error "Cannot switch buffers in minibuffer window")
7172 (pop-to-buffer buffer norecord)))
7173 ((eq (window-dedicated-p) t)
7174 (if force-same-window
7175 (user-error "Cannot switch buffers in a dedicated window")
7176 (pop-to-buffer buffer norecord)))
7178 (let* ((entry (assq buffer (window-prev-buffers)))
7179 (displayed (and (eq switch-to-buffer-preserve-window-point
7180 'already-displayed)
7181 (get-buffer-window buffer 0))))
7182 (set-window-buffer nil buffer)
7183 (when (and entry
7184 (or (eq switch-to-buffer-preserve-window-point t)
7185 displayed))
7186 ;; Try to restore start and point of buffer in the selected
7187 ;; window (Bug#4041).
7188 (set-window-start (selected-window) (nth 1 entry) t)
7189 (set-window-point nil (nth 2 entry))))))
7191 (unless norecord
7192 (select-window (selected-window)))
7193 (set-buffer buffer)))
7195 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
7196 "Select the buffer specified by BUFFER-OR-NAME in another window.
7197 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
7198 nil. Return the buffer switched to.
7200 If called interactively, prompt for the buffer name using the
7201 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7202 determines whether to request confirmation before creating a new
7203 buffer.
7205 If BUFFER-OR-NAME is a string and does not identify an existing
7206 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
7207 nil, switch to the buffer returned by `other-buffer'.
7209 Optional second argument NORECORD non-nil means do not put this
7210 buffer at the front of the list of recently selected ones.
7212 This uses the function `display-buffer' as a subroutine; see its
7213 documentation for additional customization information."
7214 (interactive
7215 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
7216 (let ((pop-up-windows t))
7217 (pop-to-buffer buffer-or-name t norecord)))
7219 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
7220 "Switch to buffer BUFFER-OR-NAME in another frame.
7221 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
7222 nil. Return the buffer switched to.
7224 If called interactively, prompt for the buffer name using the
7225 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
7226 determines whether to request confirmation before creating a new
7227 buffer.
7229 If BUFFER-OR-NAME is a string and does not identify an existing
7230 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
7231 nil, switch to the buffer returned by `other-buffer'.
7233 Optional second arg NORECORD non-nil means do not put this
7234 buffer at the front of the list of recently selected ones.
7236 This uses the function `display-buffer' as a subroutine; see its
7237 documentation for additional customization information."
7238 (interactive
7239 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
7240 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
7242 (defun set-window-text-height (window height)
7243 "Set the height in lines of the text display area of WINDOW to HEIGHT.
7244 WINDOW must be a live window and defaults to the selected one.
7245 HEIGHT doesn't include the mode line or header line, if any, or
7246 any partial-height lines in the text display area.
7248 Note that the current implementation of this function cannot
7249 always set the height exactly, but attempts to be conservative,
7250 by allocating more lines than are actually needed in the case
7251 where some error may be present."
7252 (setq window (window-normalize-window window t))
7253 (let ((delta (- height (window-text-height window))))
7254 (unless (zerop delta)
7255 ;; Setting window-min-height to a value like 1 can lead to very
7256 ;; bizarre displays because it also allows Emacs to make *other*
7257 ;; windows one line tall, which means that there's no more space
7258 ;; for the mode line.
7259 (let ((window-min-height (min 2 height)))
7260 (window-resize window delta)))))
7262 (defun enlarge-window-horizontally (delta)
7263 "Make selected window DELTA columns wider.
7264 Interactively, if no argument is given, make selected window one
7265 column wider."
7266 (interactive "p")
7267 (enlarge-window delta t))
7269 (defun shrink-window-horizontally (delta)
7270 "Make selected window DELTA columns narrower.
7271 Interactively, if no argument is given, make selected window one
7272 column narrower."
7273 (interactive "p")
7274 (shrink-window delta t))
7276 (defun count-screen-lines (&optional beg end count-final-newline window)
7277 "Return the number of screen lines in the region.
7278 The number of screen lines may be different from the number of actual lines,
7279 due to line breaking, display table, etc.
7281 Optional arguments BEG and END default to `point-min' and `point-max'
7282 respectively.
7284 If region ends with a newline, ignore it unless optional third argument
7285 COUNT-FINAL-NEWLINE is non-nil.
7287 The optional fourth argument WINDOW specifies the window used for obtaining
7288 parameters such as width, horizontal scrolling, and so on. The default is
7289 to use the selected window's parameters.
7291 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
7292 regardless of which buffer is displayed in WINDOW. This makes possible to use
7293 `count-screen-lines' in any buffer, whether or not it is currently displayed
7294 in some window."
7295 (unless beg
7296 (setq beg (point-min)))
7297 (unless end
7298 (setq end (point-max)))
7299 (if (= beg end)
7301 (save-excursion
7302 (save-restriction
7303 (widen)
7304 (narrow-to-region (min beg end)
7305 (if (and (not count-final-newline)
7306 (= ?\n (char-before (max beg end))))
7307 (1- (max beg end))
7308 (max beg end)))
7309 (goto-char (point-min))
7310 (1+ (vertical-motion (buffer-size) window))))))
7312 (defun window-buffer-height (window)
7313 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
7314 WINDOW must be a live window and defaults to the selected one."
7315 (setq window (window-normalize-window window t))
7316 (with-current-buffer (window-buffer window)
7317 (max 1
7318 (count-screen-lines (point-min) (point-max)
7319 ;; If buffer ends with a newline, ignore it when
7320 ;; counting height unless point is after it.
7321 (eobp)
7322 window))))
7324 ;;; Resizing windows and frames to fit their contents exactly.
7325 (defcustom fit-window-to-buffer-horizontally nil
7326 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
7327 If this is nil, `fit-window-to-buffer' never resizes windows
7328 horizontally. If this is `only', it can resize windows
7329 horizontally only. Any other value means `fit-window-to-buffer'
7330 can resize windows in both dimensions."
7331 :type 'boolean
7332 :version "24.4"
7333 :group 'help)
7335 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
7336 ;; counting title bar and outer borders.
7337 (defcustom fit-frame-to-buffer nil
7338 "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
7339 A frame is fit if and only if its root window is a live window
7340 and this option is non-nil. If this is `horizontally', frames
7341 are resized horizontally only. If this is `vertically', frames
7342 are resized vertically only. Any other non-nil value means
7343 frames can be resized in both dimensions."
7344 :type 'boolean
7345 :version "24.4"
7346 :group 'help)
7348 (defcustom fit-frame-to-buffer-margins '(nil nil nil nil)
7349 "Margins around frame for `fit-frame-to-buffer'.
7350 This specifies the numbers of pixels to be left free on the left,
7351 above, on the right, and below a frame fitted to its buffer. Set
7352 this to avoid obscuring other desktop objects like the taskbar.
7353 The default is nil for each side, which means to not add margins.
7355 The value specified here can be overridden for a specific frame
7356 by that frame's `fit-frame-to-buffer-margins' parameter, if
7357 present. See also `fit-frame-to-buffer-sizes'."
7358 :version "24.4"
7359 :type '(list
7360 (choice
7361 :tag "Left"
7362 :value nil
7363 :format "%[LeftMargin%] %v "
7364 (const :tag "None" :format "%t" nil)
7365 (integer :tag "Pixels" :size 5))
7366 (choice
7367 :tag "Top"
7368 :value nil
7369 :format "%[TopMargin%] %v "
7370 (const :tag "None" :format "%t" nil)
7371 (integer :tag "Pixels" :size 5))
7372 (choice
7373 :tag "Right"
7374 :value nil
7375 :format "%[RightMargin%] %v "
7376 (const :tag "None" :format "%t" nil)
7377 (integer :tag "Pixels" :size 5))
7378 (choice
7379 :tag "Bottom"
7380 :value nil
7381 :format "%[BottomMargin%] %v "
7382 (const :tag "None" :format "%t" nil)
7383 (integer :tag "Pixels" :size 5)))
7384 :group 'help)
7386 (defcustom fit-frame-to-buffer-sizes '(nil nil nil nil)
7387 "Size boundaries of frame for `fit-frame-to-buffer'.
7388 This list specifies the total maximum and minimum lines and
7389 maximum and minimum columns of the root window of any frame that
7390 shall be fit to its buffer. If any of these values is non-nil,
7391 it overrides the corresponding argument of `fit-frame-to-buffer'.
7393 On window systems where the menubar can wrap, fitting a frame to
7394 its buffer may swallow the last line(s). Specifying an
7395 appropriate minimum width value here can avoid such wrapping.
7397 See also `fit-frame-to-buffer-margins'."
7398 :version "24.4"
7399 :type '(list
7400 (choice
7401 :tag "Maximum Height"
7402 :value nil
7403 :format "%[MaxHeight%] %v "
7404 (const :tag "None" :format "%t" nil)
7405 (integer :tag "Lines" :size 5))
7406 (choice
7407 :tag "Minimum Height"
7408 :value nil
7409 :format "%[MinHeight%] %v "
7410 (const :tag "None" :format "%t" nil)
7411 (integer :tag "Lines" :size 5))
7412 (choice
7413 :tag "Maximum Width"
7414 :value nil
7415 :format "%[MaxWidth%] %v "
7416 (const :tag "None" :format "%t" nil)
7417 (integer :tag "Columns" :size 5))
7418 (choice
7419 :tag "Minimum Width"
7420 :value nil
7421 :format "%[MinWidth%] %v\n"
7422 (const :tag "None" :format "%t" nil)
7423 (integer :tag "Columns" :size 5)))
7424 :group 'help)
7426 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
7428 (defun window--sanitize-margin (margin left right)
7429 "Return MARGIN if it's a number between LEFT and RIGHT."
7430 (when (and (numberp margin)
7431 (<= left (- right margin)) (<= margin right))
7432 margin))
7434 (declare-function tool-bar-height "xdisp.c" (&optional frame pixelwise))
7436 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only)
7437 "Adjust size of FRAME to display the contents of its buffer exactly.
7438 FRAME can be any live frame and defaults to the selected one.
7439 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
7440 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
7441 FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
7442 `window-min-height' and `window-min-width' respectively.
7444 If the optional argument ONLY is `vertically', resize the frame
7445 vertically only. If ONLY is `horizontally', resize the frame
7446 horizontally only.
7448 The new position and size of FRAME can be additionally determined
7449 by customizing the options `fit-frame-to-buffer-sizes' and
7450 `fit-frame-to-buffer-margins' or the corresponding parameters of
7451 FRAME."
7452 (interactive)
7453 (unless (and (fboundp 'x-display-pixel-height)
7454 ;; We need the respective sizes now.
7455 (fboundp 'display-monitor-attributes-list))
7456 (user-error "Cannot resize frame in non-graphic Emacs"))
7457 (setq frame (window-normalize-frame frame))
7458 (when (window-live-p (frame-root-window frame))
7459 (with-selected-window (frame-root-window frame)
7460 (let* ((window (frame-root-window frame))
7461 (char-width (frame-char-width))
7462 (char-height (frame-char-height))
7463 (monitor-attributes (car (display-monitor-attributes-list
7464 (frame-parameter frame 'display))))
7465 (geometry (cdr (assq 'geometry monitor-attributes)))
7466 (display-width (- (nth 2 geometry) (nth 0 geometry)))
7467 (display-height (- (nth 3 geometry) (nth 1 geometry)))
7468 (workarea (cdr (assq 'workarea monitor-attributes)))
7469 ;; Handle margins.
7470 (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins)
7471 fit-frame-to-buffer-margins))
7472 (left-margin (if (nth 0 margins)
7473 (or (window--sanitize-margin
7474 (nth 0 margins) 0 display-width)
7476 (nth 0 workarea)))
7477 (top-margin (if (nth 1 margins)
7478 (or (window--sanitize-margin
7479 (nth 1 margins) 0 display-height)
7481 (nth 1 workarea)))
7482 (workarea-width (nth 2 workarea))
7483 (right-margin (if (nth 2 margins)
7484 (- display-width
7485 (or (window--sanitize-margin
7486 (nth 2 margins) left-margin display-width)
7488 (nth 2 workarea)))
7489 (workarea-height (nth 3 workarea))
7490 (bottom-margin (if (nth 3 margins)
7491 (- display-height
7492 (or (window--sanitize-margin
7493 (nth 3 margins) top-margin display-height)
7495 (nth 3 workarea)))
7496 ;; The pixel width of FRAME (which does not include the
7497 ;; window manager's decorations).
7498 (frame-width (frame-pixel-width))
7499 ;; The pixel width of the body of FRAME's root window.
7500 (window-body-width (window-body-width nil t))
7501 ;; The difference in pixels between total and body width of
7502 ;; FRAME's window.
7503 (window-extra-width (- (window-pixel-width) window-body-width))
7504 ;; The difference in pixels between the frame's pixel width
7505 ;; and the window's body width. This is the space we can't
7506 ;; use for fitting.
7507 (extra-width (- frame-width window-body-width))
7508 ;; The maximum width we can use for fitting.
7509 (fit-width (- workarea-width extra-width))
7510 ;; The pixel position of FRAME's left border. We usually
7511 ;; try to leave this alone.
7512 (left
7513 (let ((left (frame-parameter nil 'left)))
7514 (if (consp left)
7515 (funcall (car left) (cadr left))
7516 left)))
7517 ;; The pixel height of FRAME (which does not include title
7518 ;; line, decorations, and sometimes neither the menu nor
7519 ;; the toolbar).
7520 (frame-height (frame-pixel-height))
7521 ;; The pixel height of FRAME's root window (we don't care
7522 ;; about the window's body height since the return value of
7523 ;; `window-text-pixel-size' includes header and mode line).
7524 (window-height (window-pixel-height))
7525 ;; The difference in pixels between the frame's pixel
7526 ;; height and the window's height.
7527 (extra-height (- frame-height window-height))
7528 ;; When tool-bar-mode is enabled and we just created a new
7529 ;; frame, reserve lines for toolbar resizing. Needed
7530 ;; because for reasons unknown to me Emacs (1) reserves one
7531 ;; line for the toolbar when making the initial frame and
7532 ;; toolbars are enabled, and (2) later adds the remaining
7533 ;; lines needed. Our code runs IN BETWEEN (1) and (2).
7534 ;; YMMV when you're on a system that behaves differently.
7535 (toolbar-extra-height
7536 (let ((quit-restore (window-parameter window 'quit-restore))
7537 ;; This may have to change when we allow arbitrary
7538 ;; pixel height toolbars.
7539 (lines (tool-bar-height)))
7540 (* char-height
7541 (if (and quit-restore (eq (car quit-restore) 'frame)
7542 (not (zerop lines)))
7543 (1- lines)
7544 0))))
7545 ;; The pixel position of FRAME's top border.
7546 (top
7547 (let ((top (frame-parameter nil 'top)))
7548 (if (consp top)
7549 (funcall (car top) (cadr top))
7550 top)))
7551 ;; Sanitize minimum and maximum sizes.
7552 (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes)
7553 fit-frame-to-buffer-sizes))
7554 (max-height
7555 (cond
7556 ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height))
7557 ((numberp max-height) (* max-height char-height))
7558 (t display-height)))
7559 (min-height
7560 (cond
7561 ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height))
7562 ((numberp min-height) (* min-height char-height))
7563 (t (* window-min-height char-height))))
7564 (max-width
7565 (cond
7566 ((numberp (nth 2 sizes))
7567 (- (* (nth 2 sizes) char-width) window-extra-width))
7568 ((numberp max-width)
7569 (- (* max-width char-width) window-extra-width))
7570 (t display-width)))
7571 (min-width
7572 (cond
7573 ((numberp (nth 3 sizes))
7574 (- (* (nth 3 sizes) char-width) window-extra-width))
7575 ((numberp min-width)
7576 (- (* min-width char-width) window-extra-width))
7577 (t (* window-min-width char-width))))
7578 ;; Note: Currently, for a new frame the sizes of the header
7579 ;; and mode line may be estimated incorrectly
7580 (value (window-text-pixel-size
7581 nil t t workarea-width workarea-height t))
7582 (width (+ (car value) (window-right-divider-width)))
7583 (height
7584 (+ (cdr value)
7585 (window-bottom-divider-width)
7586 (window-scroll-bar-height))))
7587 ;; Don't change height or width when the window's size is fixed
7588 ;; in either direction or ONLY forbids it.
7589 (cond
7590 ((or (eq window-size-fixed 'width) (eq only 'vertically))
7591 (setq width nil))
7592 ((or (eq window-size-fixed 'height) (eq only 'horizontally))
7593 (setq height nil)))
7594 ;; Fit width to constraints.
7595 (when width
7596 (unless frame-resize-pixelwise
7597 ;; Round to character sizes.
7598 (setq width (* (/ (+ width char-width -1) char-width)
7599 char-width)))
7600 ;; Fit to maximum and minimum widths.
7601 (setq width (max (min width max-width) min-width))
7602 ;; Add extra width.
7603 (setq width (+ width extra-width))
7604 ;; Preserve margins.
7605 (let ((right (+ left width)))
7606 (cond
7607 ((> right right-margin)
7608 ;; Move frame to left (we don't know its real width).
7609 (setq left (max left-margin (- left (- right right-margin)))))
7610 ((< left left-margin)
7611 ;; Move frame to right.
7612 (setq left left-margin)))))
7613 ;; Fit height to constraints.
7614 (when height
7615 (unless frame-resize-pixelwise
7616 (setq height (* (/ (+ height char-height -1) char-height)
7617 char-height)))
7618 ;; Fit to maximum and minimum heights.
7619 (setq height (max (min height max-height) min-height))
7620 ;; Add extra height.
7621 (setq height (+ height extra-height))
7622 ;; Preserve margins.
7623 (let ((bottom (+ top height)))
7624 (cond
7625 ((> bottom bottom-margin)
7626 ;; Move frame up (we don't know its real height).
7627 (setq top (max top-margin (- top (- bottom bottom-margin)))))
7628 ((< top top-margin)
7629 ;; Move frame down.
7630 (setq top top-margin)))))
7631 ;; Apply changes.
7632 (set-frame-position frame left top)
7633 ;; Clumsily try to translate our calculations to what
7634 ;; `set-frame-size' wants.
7635 (when width
7636 (setq width (- (+ (frame-text-width) width)
7637 extra-width window-body-width)))
7638 (when height
7639 (setq height (- (+ (frame-text-height) height)
7640 extra-height window-height)))
7641 (set-frame-size
7642 frame
7643 (if width
7644 (if frame-resize-pixelwise
7645 width
7646 (/ width char-width))
7647 (frame-text-width))
7648 (if height
7649 (if frame-resize-pixelwise
7650 height
7651 (/ height char-height))
7652 (frame-text-height))
7653 frame-resize-pixelwise)))))
7655 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width preserve-size)
7656 "Adjust size of WINDOW to display its buffer's contents exactly.
7657 WINDOW must be a live window and defaults to the selected one.
7659 If WINDOW is part of a vertical combination, adjust WINDOW's
7660 height. The new height is calculated from the actual height of
7661 the accessible portion of its buffer. The optional argument
7662 MAX-HEIGHT specifies a maximum height and defaults to the height
7663 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
7664 minimum height and defaults to `window-min-height'. Both
7665 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
7666 and header line and a bottom divider, if any.
7668 If WINDOW is part of a horizontal combination and the value of
7669 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
7670 WINDOW's width. The new width of WINDOW is calculated from the
7671 maximum length of its buffer's lines that follow the current
7672 start position of WINDOW. The optional argument MAX-WIDTH
7673 specifies a maximum width and defaults to the width of WINDOW's
7674 frame. The optional argument MIN-WIDTH specifies a minimum width
7675 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
7676 are specified in columns and include fringes, margins, a
7677 scrollbar and a vertical divider, if any.
7679 If the optional argument `preserve-size' is non-nil, preserve the
7680 size of WINDOW (see `window-preserve-size').
7682 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
7683 If WINDOW is its frame's root window and the option
7684 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
7685 adjust the frame's size.
7687 Note that even if this function makes WINDOW large enough to show
7688 _all_ parts of its buffer you might not see the first part when
7689 WINDOW was scrolled. If WINDOW is resized horizontally, you will
7690 not see the top of its buffer unless WINDOW starts at its minimum
7691 accessible position."
7692 (interactive)
7693 (setq window (window-normalize-window window t))
7694 (if (eq window (frame-root-window window))
7695 (when fit-frame-to-buffer
7696 ;; Fit WINDOW's frame to buffer.
7697 (fit-frame-to-buffer
7698 (window-frame window)
7699 max-height min-height max-width min-width
7700 (and (memq fit-frame-to-buffer '(vertically horizontally))
7701 fit-frame-to-buffer)))
7702 (with-selected-window window
7703 (let* ((pixelwise window-resize-pixelwise)
7704 (char-height (frame-char-height))
7705 (char-width (frame-char-width))
7706 (total-height (window-size window nil pixelwise))
7707 (body-height (window-body-height window pixelwise))
7708 (body-width (window-body-width window pixelwise))
7709 (min-height
7710 ;; Sanitize MIN-HEIGHT.
7711 (if (numberp min-height)
7712 ;; Can't get smaller than `window-safe-min-height'.
7713 (max (if pixelwise
7714 (* char-height min-height)
7715 min-height)
7716 (if pixelwise
7717 (window-safe-min-pixel-height window)
7718 window-safe-min-height))
7719 ;; Preserve header and mode line if present.
7720 (max (if pixelwise
7721 (* char-height window-min-height)
7722 window-min-height)
7723 (window-min-size window nil window pixelwise))))
7724 (max-height
7725 ;; Sanitize MAX-HEIGHT.
7726 (if (numberp max-height)
7727 (min
7728 (+ total-height
7729 (window-max-delta
7730 window nil window nil nil nil pixelwise))
7731 (if pixelwise
7732 (* char-height max-height)
7733 max-height))
7734 (+ total-height (window-max-delta
7735 window nil window nil nil nil pixelwise))))
7736 height)
7737 (cond
7738 ;; If WINDOW is vertically combined, try to resize it
7739 ;; vertically.
7740 ((and (not (eq fit-window-to-buffer-horizontally 'only))
7741 (not (window-size-fixed-p window 'preserved))
7742 (window-combined-p))
7743 ;; Vertically we always want to fit the entire buffer.
7744 ;; WINDOW'S height can't get larger than its frame's pixel
7745 ;; height. Its width remains fixed.
7746 (setq height (+ (cdr (window-text-pixel-size
7747 nil nil t nil (frame-pixel-height) t))
7748 (window-scroll-bar-height window)
7749 (window-bottom-divider-width)))
7750 ;; Round height.
7751 (unless pixelwise
7752 (setq height (/ (+ height char-height -1) char-height)))
7753 (unless (= height total-height)
7754 (window-preserve-size window)
7755 (window-resize-no-error
7756 window
7757 (- (max min-height (min max-height height)) total-height)
7758 nil window pixelwise)
7759 (when preserve-size
7760 (window-preserve-size window nil t))))
7761 ;; If WINDOW is horizontally combined, try to resize it
7762 ;; horizontally.
7763 ((and fit-window-to-buffer-horizontally
7764 (not (window-size-fixed-p window t 'preserved))
7765 (window-combined-p nil t))
7766 (let* ((total-width (window-size window t pixelwise))
7767 (min-width
7768 ;; Sanitize MIN-WIDTH.
7769 (if (numberp min-width)
7770 ;; Can't get smaller than `window-safe-min-width'.
7771 (max (if pixelwise
7772 (* char-width min-width)
7773 min-width)
7774 (if pixelwise
7775 (window-safe-min-pixel-width)
7776 window-safe-min-width))
7777 ;; Preserve fringes, margins, scrollbars if present.
7778 (max (if pixelwise
7779 (* char-width window-min-width)
7780 window-min-width)
7781 (window-min-size nil nil window pixelwise))))
7782 (max-width
7783 ;; Sanitize MAX-WIDTH.
7784 (if (numberp max-width)
7785 (min (+ total-width
7786 (window-max-delta
7787 window t window nil nil nil pixelwise))
7788 (if pixelwise
7789 (* char-width max-width)
7790 max-width))
7791 (+ total-width (window-max-delta
7792 window t window nil nil nil pixelwise))))
7793 ;; When fitting horizontally, assume that WINDOW's
7794 ;; start position remains unaltered. WINDOW can't get
7795 ;; wider than its frame's pixel width, its height
7796 ;; remains unaltered.
7797 (width (+ (car (window-text-pixel-size
7798 nil (window-start) (point-max)
7799 (frame-pixel-width)
7800 ;; Add one char-height to assure that
7801 ;; we're on the safe side. This
7802 ;; overshoots when the first line below
7803 ;; the bottom is wider than the window.
7804 (* body-height
7805 (if pixelwise 1 char-height))))
7806 (window-right-divider-width))))
7807 (unless pixelwise
7808 (setq width (/ (+ width char-width -1) char-width)))
7809 (unless (= width body-width)
7810 (window-preserve-size window t)
7811 (window-resize-no-error
7812 window
7813 (- (max min-width
7814 (min max-width
7815 (+ total-width (- width body-width))))
7816 total-width)
7817 t window pixelwise)
7818 (when preserve-size
7819 (window-preserve-size window t t))))))))))
7821 (defun window-safely-shrinkable-p (&optional window)
7822 "Return t if WINDOW can be shrunk without shrinking other windows.
7823 WINDOW defaults to the selected window."
7824 (with-selected-window (or window (selected-window))
7825 (let ((edges (window-edges)))
7826 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
7827 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
7829 (defun shrink-window-if-larger-than-buffer (&optional window)
7830 "Shrink height of WINDOW if its buffer doesn't need so many lines.
7831 More precisely, shrink WINDOW vertically to be as small as
7832 possible, while still showing the full contents of its buffer.
7833 WINDOW must be a live window and defaults to the selected one.
7835 Do not shrink WINDOW to less than `window-min-height' lines. Do
7836 nothing if the buffer contains more lines than the present window
7837 height, or if some of the window's contents are scrolled out of
7838 view, or if shrinking this window would also shrink another
7839 window, or if the window is the only window of its frame.
7841 Return non-nil if the window was shrunk, nil otherwise."
7842 (interactive)
7843 (setq window (window-normalize-window window t))
7844 ;; Make sure that WINDOW is vertically combined and `point-min' is
7845 ;; visible (for whatever reason that's needed). The remaining issues
7846 ;; should be taken care of by `fit-window-to-buffer'.
7847 (when (and (window-combined-p window)
7848 (pos-visible-in-window-p (point-min) window))
7849 (fit-window-to-buffer window (window-total-height window))))
7851 (defun kill-buffer-and-window ()
7852 "Kill the current buffer and delete the selected window."
7853 (interactive)
7854 (let ((window-to-delete (selected-window))
7855 (buffer-to-kill (current-buffer))
7856 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
7857 (unwind-protect
7858 (progn
7859 (add-hook 'kill-buffer-hook delete-window-hook t t)
7860 (if (kill-buffer (current-buffer))
7861 ;; If `delete-window' failed before, we rerun it to regenerate
7862 ;; the error so it can be seen in the echo area.
7863 (when (eq (selected-window) window-to-delete)
7864 (delete-window))))
7865 ;; If the buffer is not dead for some reason (probably because
7866 ;; of a `quit' signal), remove the hook again.
7867 (ignore-errors
7868 (with-current-buffer buffer-to-kill
7869 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
7872 (defvar recenter-last-op nil
7873 "Indicates the last recenter operation performed.
7874 Possible values: `top', `middle', `bottom', integer or float numbers.
7875 It can also be nil, which means the first value in `recenter-positions'.")
7877 (defcustom recenter-positions '(middle top bottom)
7878 "Cycling order for `recenter-top-bottom'.
7879 A list of elements with possible values `top', `middle', `bottom',
7880 integer or float numbers that define the cycling order for
7881 the command `recenter-top-bottom'.
7883 Top and bottom destinations are `scroll-margin' lines from the true
7884 window top and bottom. Middle redraws the frame and centers point
7885 vertically within the window. Integer number moves current line to
7886 the specified absolute window-line. Float number between 0.0 and 1.0
7887 means the percentage of the screen space from the top. The default
7888 cycling order is middle -> top -> bottom."
7889 :type '(repeat (choice
7890 (const :tag "Top" top)
7891 (const :tag "Middle" middle)
7892 (const :tag "Bottom" bottom)
7893 (integer :tag "Line number")
7894 (float :tag "Percentage")))
7895 :version "23.2"
7896 :group 'windows)
7898 (defun recenter-top-bottom (&optional arg)
7899 "Move current buffer line to the specified window line.
7900 With no prefix argument, successive calls place point according
7901 to the cycling order defined by `recenter-positions'.
7903 A prefix argument is handled like `recenter':
7904 With numeric prefix ARG, move current line to window-line ARG.
7905 With plain `C-u', move current line to window center."
7906 (interactive "P")
7907 (cond
7908 (arg (recenter arg)) ; Always respect ARG.
7910 (setq recenter-last-op
7911 (if (eq this-command last-command)
7912 (car (or (cdr (member recenter-last-op recenter-positions))
7913 recenter-positions))
7914 (car recenter-positions)))
7915 (let ((this-scroll-margin
7916 (min (max 0 scroll-margin)
7917 (truncate (/ (window-body-height) 4.0)))))
7918 (cond ((eq recenter-last-op 'middle)
7919 (recenter))
7920 ((eq recenter-last-op 'top)
7921 (recenter this-scroll-margin))
7922 ((eq recenter-last-op 'bottom)
7923 (recenter (- -1 this-scroll-margin)))
7924 ((integerp recenter-last-op)
7925 (recenter recenter-last-op))
7926 ((floatp recenter-last-op)
7927 (recenter (round (* recenter-last-op (window-height))))))))))
7929 (define-key global-map [?\C-l] 'recenter-top-bottom)
7931 (defun move-to-window-line-top-bottom (&optional arg)
7932 "Position point relative to window.
7934 With a prefix argument ARG, acts like `move-to-window-line'.
7936 With no argument, positions point at center of window.
7937 Successive calls position point at positions defined
7938 by `recenter-positions'."
7939 (interactive "P")
7940 (cond
7941 (arg (move-to-window-line arg)) ; Always respect ARG.
7943 (setq recenter-last-op
7944 (if (eq this-command last-command)
7945 (car (or (cdr (member recenter-last-op recenter-positions))
7946 recenter-positions))
7947 (car recenter-positions)))
7948 (let ((this-scroll-margin
7949 (min (max 0 scroll-margin)
7950 (truncate (/ (window-body-height) 4.0)))))
7951 (cond ((eq recenter-last-op 'middle)
7952 (call-interactively 'move-to-window-line))
7953 ((eq recenter-last-op 'top)
7954 (move-to-window-line this-scroll-margin))
7955 ((eq recenter-last-op 'bottom)
7956 (move-to-window-line (- -1 this-scroll-margin)))
7957 ((integerp recenter-last-op)
7958 (move-to-window-line recenter-last-op))
7959 ((floatp recenter-last-op)
7960 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
7962 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
7964 ;;; Scrolling commands.
7966 ;;; Scrolling commands which do not signal errors at top/bottom
7967 ;;; of buffer at first key-press (instead move to top/bottom
7968 ;;; of buffer).
7970 (defcustom scroll-error-top-bottom nil
7971 "Move point to top/bottom of buffer before signaling a scrolling error.
7972 A value of nil means just signal an error if no more scrolling possible.
7973 A value of t means point moves to the beginning or the end of the buffer
7974 \(depending on scrolling direction) when no more scrolling possible.
7975 When point is already on that position, then signal an error."
7976 :type 'boolean
7977 :group 'windows
7978 :version "24.1")
7980 (defun scroll-up-command (&optional arg)
7981 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
7982 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
7983 scroll window further, move cursor to the bottom line.
7984 When point is already on that position, then signal an error.
7985 A near full screen is `next-screen-context-lines' less than a full screen.
7986 Negative ARG means scroll downward.
7987 If ARG is the atom `-', scroll downward by nearly full screen."
7988 (interactive "^P")
7989 (cond
7990 ((null scroll-error-top-bottom)
7991 (scroll-up arg))
7992 ((eq arg '-)
7993 (scroll-down-command nil))
7994 ((< (prefix-numeric-value arg) 0)
7995 (scroll-down-command (- (prefix-numeric-value arg))))
7996 ((eobp)
7997 (scroll-up arg)) ; signal error
7999 (condition-case nil
8000 (scroll-up arg)
8001 (end-of-buffer
8002 (if arg
8003 ;; When scrolling by ARG lines can't be done,
8004 ;; move by ARG lines instead.
8005 (forward-line arg)
8006 ;; When ARG is nil for full-screen scrolling,
8007 ;; move to the bottom of the buffer.
8008 (goto-char (point-max))))))))
8010 (put 'scroll-up-command 'scroll-command t)
8012 (defun scroll-down-command (&optional arg)
8013 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
8014 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
8015 scroll window further, move cursor to the top line.
8016 When point is already on that position, then signal an error.
8017 A near full screen is `next-screen-context-lines' less than a full screen.
8018 Negative ARG means scroll upward.
8019 If ARG is the atom `-', scroll upward by nearly full screen."
8020 (interactive "^P")
8021 (cond
8022 ((null scroll-error-top-bottom)
8023 (scroll-down arg))
8024 ((eq arg '-)
8025 (scroll-up-command nil))
8026 ((< (prefix-numeric-value arg) 0)
8027 (scroll-up-command (- (prefix-numeric-value arg))))
8028 ((bobp)
8029 (scroll-down arg)) ; signal error
8031 (condition-case nil
8032 (scroll-down arg)
8033 (beginning-of-buffer
8034 (if arg
8035 ;; When scrolling by ARG lines can't be done,
8036 ;; move by ARG lines instead.
8037 (forward-line (- arg))
8038 ;; When ARG is nil for full-screen scrolling,
8039 ;; move to the top of the buffer.
8040 (goto-char (point-min))))))))
8042 (put 'scroll-down-command 'scroll-command t)
8044 ;;; Scrolling commands which scroll a line instead of full screen.
8046 (defun scroll-up-line (&optional arg)
8047 "Scroll text of selected window upward ARG lines; or one line if no ARG.
8048 If ARG is omitted or nil, scroll upward by one line.
8049 This is different from `scroll-up-command' that scrolls a full screen."
8050 (interactive "p")
8051 (scroll-up (or arg 1)))
8053 (put 'scroll-up-line 'scroll-command t)
8055 (defun scroll-down-line (&optional arg)
8056 "Scroll text of selected window down ARG lines; or one line if no ARG.
8057 If ARG is omitted or nil, scroll down by one line.
8058 This is different from `scroll-down-command' that scrolls a full screen."
8059 (interactive "p")
8060 (scroll-down (or arg 1)))
8062 (put 'scroll-down-line 'scroll-command t)
8065 (defun scroll-other-window-down (&optional lines)
8066 "Scroll the \"other window\" down.
8067 For more details, see the documentation for `scroll-other-window'."
8068 (interactive "P")
8069 (scroll-other-window
8070 ;; Just invert the argument's meaning.
8071 ;; We can do that without knowing which window it will be.
8072 (if (eq lines '-) nil
8073 (if (null lines) '-
8074 (- (prefix-numeric-value lines))))))
8076 (defun beginning-of-buffer-other-window (arg)
8077 "Move point to the beginning of the buffer in the other window.
8078 Leave mark at previous position.
8079 With arg N, put point N/10 of the way from the true beginning."
8080 (interactive "P")
8081 (let ((orig-window (selected-window))
8082 (window (other-window-for-scrolling)))
8083 ;; We use unwind-protect rather than save-window-excursion
8084 ;; because the latter would preserve the things we want to change.
8085 (unwind-protect
8086 (progn
8087 (select-window window)
8088 ;; Set point and mark in that window's buffer.
8089 (with-no-warnings
8090 (beginning-of-buffer arg))
8091 ;; Set point accordingly.
8092 (recenter '(t)))
8093 (select-window orig-window))))
8095 (defun end-of-buffer-other-window (arg)
8096 "Move point to the end of the buffer in the other window.
8097 Leave mark at previous position.
8098 With arg N, put point N/10 of the way from the true end."
8099 (interactive "P")
8100 ;; See beginning-of-buffer-other-window for comments.
8101 (let ((orig-window (selected-window))
8102 (window (other-window-for-scrolling)))
8103 (unwind-protect
8104 (progn
8105 (select-window window)
8106 (with-no-warnings
8107 (end-of-buffer arg))
8108 (recenter '(t)))
8109 (select-window orig-window))))
8111 (defvar mouse-autoselect-window-timer nil
8112 "Timer used by delayed window autoselection.")
8114 (defvar mouse-autoselect-window-position-1 nil
8115 "First mouse position recorded by delayed window autoselection.")
8117 (defvar mouse-autoselect-window-position nil
8118 "Last mouse position recorded by delayed window autoselection.")
8120 (defvar mouse-autoselect-window-window nil
8121 "Last window recorded by delayed window autoselection.")
8123 (defvar mouse-autoselect-window-state nil
8124 "When non-nil, special state of delayed window autoselection.
8125 Possible values are `suspend' (suspend autoselection after a menu or
8126 scrollbar interaction) and `select' (the next invocation of
8127 `handle-select-window' shall select the window immediately).")
8129 (defun mouse-autoselect-window-cancel (&optional force)
8130 "Cancel delayed window autoselection.
8131 Optional argument FORCE means cancel unconditionally."
8132 (unless (and (not force)
8133 ;; Don't cancel for select-window or select-frame events
8134 ;; or when the user drags a scroll bar.
8135 (or (memq this-command
8136 '(handle-select-window handle-switch-frame))
8137 (and (eq this-command 'scroll-bar-toolkit-scroll)
8138 (memq (nth 4 (event-end last-input-event))
8139 '(handle end-scroll)))))
8140 (setq mouse-autoselect-window-state nil)
8141 (setq mouse-autoselect-window-position-1 nil)
8142 (when (timerp mouse-autoselect-window-timer)
8143 (cancel-timer mouse-autoselect-window-timer))
8144 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
8146 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
8147 "Start delayed window autoselection.
8148 MOUSE-POSITION is the last position where the mouse was seen as returned
8149 by `mouse-position'. Optional argument WINDOW non-nil denotes the
8150 window where the mouse was seen. Optional argument SUSPEND non-nil
8151 means suspend autoselection."
8152 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
8153 (setq mouse-autoselect-window-position mouse-position)
8154 (when window (setq mouse-autoselect-window-window window))
8155 (setq mouse-autoselect-window-state (when suspend 'suspend))
8156 ;; Install timer which runs `mouse-autoselect-window-select' after
8157 ;; `mouse-autoselect-window' seconds.
8158 (setq mouse-autoselect-window-timer
8159 (run-at-time
8160 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
8162 (defun mouse-autoselect-window-select ()
8163 "Select window with delayed window autoselection.
8164 If the mouse position has stabilized in a non-selected window, select
8165 that window. The minibuffer window is selected only if the minibuffer
8166 is active. This function is run by `mouse-autoselect-window-timer'."
8167 (ignore-errors
8168 (let* ((mouse-position (mouse-position))
8169 (window
8170 (ignore-errors
8171 (window-at (cadr mouse-position) (cddr mouse-position)
8172 (car mouse-position)))))
8173 (cond
8174 ((or (and (fboundp 'menu-or-popup-active-p) (menu-or-popup-active-p))
8175 (and window
8176 (let ((coords (coordinates-in-window-p
8177 (cdr mouse-position) window)))
8178 (and (not (consp coords))
8179 (not (memq coords '(left-margin right-margin)))))))
8180 ;; A menu / popup dialog is active or the mouse is not on the
8181 ;; text region of WINDOW: Suspend autoselection temporarily.
8182 (mouse-autoselect-window-start mouse-position nil t))
8183 ((or (eq mouse-autoselect-window-state 'suspend)
8184 ;; When the mouse is at its first recorded position, restart
8185 ;; delayed autoselection. This works around a scenario with
8186 ;; two two-window frames with identical dimensions: select the
8187 ;; first window of the first frame, switch to the second
8188 ;; frame, move the mouse to its second window, minimize the
8189 ;; second frame. Now the second window of the first frame
8190 ;; gets selected although the mouse never really "moved" into
8191 ;; that window.
8192 (and (numberp mouse-autoselect-window)
8193 (equal (mouse-position) mouse-autoselect-window-position-1)))
8194 ;; Delayed autoselection was temporarily suspended, reenable it.
8195 (mouse-autoselect-window-start mouse-position))
8196 ((and window (not (eq window (selected-window)))
8197 (or (not (numberp mouse-autoselect-window))
8198 (and (>= mouse-autoselect-window 0)
8199 ;; If `mouse-autoselect-window' is non-negative,
8200 ;; select window if it's the same as before.
8201 (eq window mouse-autoselect-window-window))
8202 ;; Otherwise select window iff the mouse is at the same
8203 ;; position as before. Observe that the first test
8204 ;; after starting autoselection usually fails since the
8205 ;; value of `mouse-autoselect-window-position' recorded
8206 ;; there is the position where the mouse has entered the
8207 ;; new window and not necessarily where the mouse has
8208 ;; stopped moving.
8209 (equal mouse-position mouse-autoselect-window-position))
8210 ;; The minibuffer is a candidate window if it's active.
8211 (or (not (window-minibuffer-p window))
8212 (eq window (active-minibuffer-window))))
8213 ;; Mouse position has stabilized in non-selected window: Cancel
8214 ;; delayed autoselection and try to select that window.
8215 (mouse-autoselect-window-cancel t)
8216 ;; Select window where mouse appears unless the selected window is the
8217 ;; minibuffer. Use `unread-command-events' in order to execute pre-
8218 ;; and post-command hooks and trigger idle timers. To avoid delaying
8219 ;; autoselection again, set `mouse-autoselect-window-state'."
8220 (unless (window-minibuffer-p)
8221 (setq mouse-autoselect-window-state 'select)
8222 (setq unread-command-events
8223 (cons (list 'select-window (list window))
8224 unread-command-events))))
8225 ((or (and window (eq window (selected-window)))
8226 (not (numberp mouse-autoselect-window))
8227 (equal mouse-position mouse-autoselect-window-position))
8228 ;; Mouse position has either stabilized in the selected window or at
8229 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
8230 (mouse-autoselect-window-cancel t))
8232 ;; Mouse position has not stabilized yet, resume delayed
8233 ;; autoselection.
8234 (mouse-autoselect-window-start mouse-position window))))))
8236 (defun handle-select-window (event)
8237 "Handle select-window events."
8238 (interactive "^e")
8239 (let ((window (posn-window (event-start event))))
8240 (unless (or (not (window-live-p window))
8241 ;; Don't switch if we're currently in the minibuffer.
8242 ;; This tries to work around problems where the
8243 ;; minibuffer gets unselected unexpectedly, and where
8244 ;; you then have to move your mouse all the way down to
8245 ;; the minibuffer to select it.
8246 (window-minibuffer-p)
8247 ;; Don't switch to minibuffer window unless it's active.
8248 (and (window-minibuffer-p window)
8249 (not (minibuffer-window-active-p window)))
8250 ;; Don't switch when autoselection shall be delayed.
8251 (and (numberp mouse-autoselect-window)
8252 (not (eq mouse-autoselect-window-state 'select))
8253 (let ((position (mouse-position)))
8254 ;; Cancel any delayed autoselection.
8255 (mouse-autoselect-window-cancel t)
8256 ;; Start delayed autoselection from current mouse
8257 ;; position and window.
8258 (setq mouse-autoselect-window-position-1 position)
8259 (mouse-autoselect-window-start position window)
8260 ;; Executing a command cancels delayed autoselection.
8261 (add-hook
8262 'pre-command-hook 'mouse-autoselect-window-cancel))))
8263 (when mouse-autoselect-window
8264 ;; Reset state of delayed autoselection.
8265 (setq mouse-autoselect-window-state nil)
8266 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
8267 (run-hooks 'mouse-leave-buffer-hook))
8268 ;; Clear echo area.
8269 (message nil)
8270 (select-window window))))
8272 (defun truncated-partial-width-window-p (&optional window)
8273 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
8274 WINDOW must be a live window and defaults to the selected one.
8275 Return nil if WINDOW is not a partial-width window
8276 (regardless of the value of `truncate-lines').
8277 Otherwise, consult the value of `truncate-partial-width-windows'
8278 for the buffer shown in WINDOW."
8279 (setq window (window-normalize-window window t))
8280 (unless (window-full-width-p window)
8281 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
8282 (window-buffer window))))
8283 (if (integerp t-p-w-w)
8284 (< (window-width window) t-p-w-w)
8285 t-p-w-w))))
8288 ;; Automatically inform subprocesses of changes to window size.
8290 (defcustom window-adjust-process-window-size-function
8291 'window-adjust-process-window-size-smallest
8292 "Control how Emacs chooses inferior process window sizes.
8293 Emacs uses this function to tell processes the space they have
8294 available for displaying their output. After each window
8295 configuration change, Emacs calls the value of
8296 `window-adjust-process-window-size-function' for each process
8297 with a buffer being displayed in at least one window.
8298 This function is responsible for combining the sizes of the
8299 displayed windows and returning a cons (WIDTH . HEIGHT)
8300 describing the width and height with which Emacs will call
8301 `set-process-window-size' for that process. If the function
8302 returns nil, Emacs does not call `set-process-window-size'.
8304 This function is called with the process buffer as the current
8305 buffer and with two arguments: the process and a list of windows
8306 displaying process. Modes can make this variable buffer-local;
8307 additionally, the `adjust-window-size-function' process property
8308 overrides the global or buffer-local value of
8309 `window-adjust-process-window-size-function'."
8310 :type '(choice
8311 (const :tag "Minimum area of any window"
8312 window-adjust-process-window-size-smallest)
8313 (const :tag "Maximum area of any window"
8314 window-adjust-process-window-size-largest)
8315 (const :tag "Do not adjust process window sizes" ignore)
8316 function)
8317 :group 'windows
8318 :version "25.1")
8320 (defun window-adjust-process-window-size (reducer process windows)
8321 "Adjust the process window size of PROCESS.
8322 WINDOWS is a list of windows associated with PROCESS. REDUCER is
8323 a two-argument function used to combine the widths and heights of
8324 the given windows."
8325 (when windows
8326 (let ((width (window-body-width (car windows)))
8327 (height (window-body-height (car windows))))
8328 (dolist (window (cdr windows))
8329 (setf width (funcall reducer width (window-body-width window)))
8330 (setf height (funcall reducer height (window-body-height window))))
8331 (cons width height))))
8333 (defun window-adjust-process-window-size-smallest (process windows)
8334 "Adjust the process window size of PROCESS.
8335 WINDOWS is a list of windows associated with PROCESS. Choose the
8336 smallest area available for displaying PROCESS's output."
8337 (window-adjust-process-window-size #'min process windows))
8339 (defun window-adjust-process-window-size-largest (process windows)
8340 "Adjust the process window size of PROCESS.
8341 WINDOWS is a list of windows associated with PROCESS. Choose the
8342 largest area available for displaying PROCESS's output."
8343 (window-adjust-process-window-size #'max process windows))
8345 (defun window--process-window-list ()
8346 "Return an alist mapping processes to associated windows.
8347 A window is associated with a process if that window is
8348 displaying that processes's buffer."
8349 (let ((processes (process-list))
8350 (process-windows nil))
8351 (walk-windows
8352 (lambda (window)
8353 (let ((buffer (window-buffer window))
8354 (iter processes))
8355 (while (let ((process (car iter)))
8356 (if (and (process-live-p process)
8357 (eq buffer (process-buffer process)))
8358 (let ((procwin (assq process process-windows)))
8359 ;; Add this window to the list of windows
8360 ;; displaying process.
8361 (if procwin
8362 (push window (cdr procwin))
8363 (push (list process window) process-windows))
8364 ;; We found our process for this window, so
8365 ;; stop iterating over the process list.
8366 nil)
8367 (setf iter (cdr iter)))))))
8368 1 t)
8369 process-windows))
8371 (defun window--adjust-process-windows ()
8372 "Update process window sizes to match the current window configuration."
8373 (dolist (procwin (window--process-window-list))
8374 (let ((process (car procwin)))
8375 (with-demoted-errors "Error adjusting window size: %S"
8376 (with-current-buffer (process-buffer process)
8377 (let ((size (funcall
8378 (or (process-get process 'adjust-window-size-function)
8379 window-adjust-process-window-size-function)
8380 process (cdr procwin))))
8381 (when size
8382 (set-process-window-size process (cdr size) (car size)))))))))
8384 (add-hook 'window-configuration-change-hook 'window--adjust-process-windows)
8387 ;; Some of these are in tutorial--default-keys, so update that if you
8388 ;; change these.
8389 (define-key ctl-x-map "0" 'delete-window)
8390 (define-key ctl-x-map "1" 'delete-other-windows)
8391 (define-key ctl-x-map "2" 'split-window-below)
8392 (define-key ctl-x-map "3" 'split-window-right)
8393 (define-key ctl-x-map "o" 'other-window)
8394 (define-key ctl-x-map "^" 'enlarge-window)
8395 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
8396 (define-key ctl-x-map "{" 'shrink-window-horizontally)
8397 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
8398 (define-key ctl-x-map "+" 'balance-windows)
8399 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
8401 ;;; window.el ends here