Update copyright year to 2015
[emacs.git] / lisp / window.el
blobabc60063ab910bcdb08421fc0ea2c69dd1b77a87
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-text-lines 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 (defun window-current-scroll-bars (&optional window)
1839 "Return the current scroll bar types for WINDOW.
1840 WINDOW must be a live window and defaults to the selected one.
1842 The return value is a cons cell (VERTICAL . HORIZONTAL) where
1843 VERTICAL specifies the current location of the vertical scroll
1844 bar (`left', `right' or nil), and HORIZONTAL specifies the
1845 current location of the horizontal scroll bar (`bottom' or nil).
1847 Unlike `window-scroll-bars', this function reports the scroll bar
1848 type actually used, once frame defaults and `scroll-bar-mode' are
1849 taken into account."
1850 (setq window (window-normalize-window window t))
1851 (let ((vertical (nth 2 (window-scroll-bars window)))
1852 (horizontal (nth 5 (window-scroll-bars window)))
1853 (inherited (frame-current-scroll-bars (window-frame window))))
1854 (when (eq vertical t)
1855 (setq vertical (car inherited)))
1856 (when (eq horizontal t)
1857 (setq horizontal (cdr inherited)))
1858 (cons vertical (and horizontal 'bottom))))
1860 (defun walk-windows (fun &optional minibuf all-frames)
1861 "Cycle through all live windows, calling FUN for each one.
1862 FUN must specify a function with a window as its sole argument.
1863 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1864 windows to include in the walk.
1866 MINIBUF t means include the minibuffer window even if the
1867 minibuffer is not active. MINIBUF nil or omitted means include
1868 the minibuffer window only if the minibuffer is active. Any
1869 other value means do not include the minibuffer window even if
1870 the minibuffer is active.
1872 ALL-FRAMES nil or omitted means consider all windows on the
1873 selected frame, plus the minibuffer window if specified by the
1874 MINIBUF argument. If the minibuffer counts, consider all windows
1875 on all frames that share that minibuffer too. The following
1876 non-nil values of ALL-FRAMES have special meanings:
1878 - t means consider all windows on all existing frames.
1880 - `visible' means consider all windows on all visible frames on
1881 the current terminal.
1883 - 0 (the number zero) means consider all windows on all visible
1884 and iconified frames on the current terminal.
1886 - A frame means consider all windows on that frame only.
1888 Anything else means consider all windows on the selected frame
1889 and no others.
1891 This function changes neither the order of recently selected
1892 windows nor the buffer list."
1893 ;; If we start from the minibuffer window, don't fail to come
1894 ;; back to it.
1895 (when (window-minibuffer-p)
1896 (setq minibuf t))
1897 ;; Make sure to not mess up the order of recently selected
1898 ;; windows. Use `save-selected-window' and `select-window'
1899 ;; with second argument non-nil for this purpose.
1900 (save-selected-window
1901 (when (framep all-frames)
1902 (select-window (frame-first-window all-frames) 'norecord))
1903 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
1904 (funcall fun walk-windows-window))))
1906 (defun window-at-side-p (&optional window side)
1907 "Return t if WINDOW is at SIDE of its containing frame.
1908 WINDOW must be a valid window and defaults to the selected one.
1909 SIDE can be any of the symbols `left', `top', `right' or
1910 `bottom'. The default value nil is handled like `bottom'."
1911 (setq window (window-normalize-window window))
1912 (let ((edge
1913 (cond
1914 ((eq side 'left) 0)
1915 ((eq side 'top) 1)
1916 ((eq side 'right) 2)
1917 ((memq side '(bottom nil)) 3))))
1918 (= (nth edge (window-pixel-edges window))
1919 (nth edge (window-pixel-edges (frame-root-window window))))))
1921 (defun window-at-side-list (&optional frame side)
1922 "Return list of all windows on SIDE of FRAME.
1923 FRAME must be a live frame and defaults to the selected frame.
1924 SIDE can be any of the symbols `left', `top', `right' or
1925 `bottom'. The default value nil is handled like `bottom'."
1926 (setq frame (window-normalize-frame frame))
1927 (let (windows)
1928 (walk-window-tree
1929 (lambda (window)
1930 (when (window-at-side-p window side)
1931 (setq windows (cons window windows))))
1932 frame nil 'nomini)
1933 (nreverse windows)))
1935 (defun window--in-direction-2 (window posn &optional horizontal)
1936 "Support function for `window-in-direction'."
1937 (if horizontal
1938 (let ((top (window-pixel-top window)))
1939 (if (> top posn)
1940 (- top posn)
1941 (- posn top (window-pixel-height window))))
1942 (let ((left (window-pixel-left window)))
1943 (if (> left posn)
1944 (- left posn)
1945 (- posn left (window-pixel-width window))))))
1947 ;; Predecessors to the below have been devised by Julian Assange in
1948 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
1949 ;; Neither of these allow to selectively ignore specific windows
1950 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
1951 ;; the movement.
1952 (defun window-in-direction (direction &optional window ignore sign wrap mini)
1953 "Return window in DIRECTION as seen from WINDOW.
1954 More precisely, return the nearest window in direction DIRECTION
1955 as seen from the position of `window-point' in window WINDOW.
1956 DIRECTION must be one of `above', `below', `left' or `right'.
1957 WINDOW must be a live window and defaults to the selected one.
1959 Do not return a window whose `no-other-window' parameter is
1960 non-nil. If the nearest window's `no-other-window' parameter is
1961 non-nil, try to find another window in the indicated direction.
1962 If, however, the optional argument IGNORE is non-nil, return that
1963 window even if its `no-other-window' parameter is non-nil.
1965 Optional argument SIGN a negative number means to use the right
1966 or bottom edge of WINDOW as reference position instead of
1967 `window-point'. SIGN a positive number means to use the left or
1968 top edge of WINDOW as reference position.
1970 Optional argument WRAP non-nil means to wrap DIRECTION around
1971 frame borders. This means to return for WINDOW at the top of the
1972 frame and DIRECTION `above' the minibuffer window if the frame
1973 has one, and a window at the bottom of the frame otherwise.
1975 Optional argument MINI nil means to return the minibuffer window
1976 if and only if it is currently active. MINI non-nil means to
1977 return the minibuffer window even when it's not active. However,
1978 if WRAP non-nil, always act as if MINI were nil.
1980 Return nil if no suitable window can be found."
1981 (setq window (window-normalize-window window t))
1982 (unless (memq direction '(above below left right))
1983 (error "Wrong direction %s" direction))
1984 (let* ((frame (window-frame window))
1985 (hor (memq direction '(left right)))
1986 (first (if hor
1987 (window-pixel-left window)
1988 (window-pixel-top window)))
1989 (last (+ first (window-size window hor t)))
1990 ;; The column / row value of `posn-at-point' can be nil for the
1991 ;; mini-window, guard against that.
1992 (posn
1993 (cond
1994 ((and (numberp sign) (< sign 0))
1995 (if hor
1996 (1- (+ (window-pixel-top window) (window-pixel-height window)))
1997 (1- (+ (window-pixel-left window) (window-pixel-width window)))))
1998 ((and (numberp sign) (> sign 0))
1999 (if hor
2000 (window-pixel-top window)
2001 (window-pixel-left window)))
2002 ((let ((posn-cons (nth 2 (posn-at-point (window-point window) window))))
2003 (if hor
2004 (+ (or (cdr posn-cons) 1) (window-pixel-top window))
2005 (+ (or (car posn-cons) 1) (window-pixel-left window)))))))
2006 (best-edge
2007 (cond
2008 ((eq direction 'below) (frame-pixel-height frame))
2009 ((eq direction 'right) (frame-pixel-width frame))
2010 (t -1)))
2011 (best-edge-2 best-edge)
2012 (best-diff-2 (if hor (frame-pixel-height frame) (frame-pixel-width frame)))
2013 best best-2 best-diff-2-new)
2014 (walk-window-tree
2015 (lambda (w)
2016 (let* ((w-top (window-pixel-top w))
2017 (w-left (window-pixel-left w)))
2018 (cond
2019 ((or (eq window w)
2020 ;; Ignore ourselves.
2021 (and (window-parameter w 'no-other-window)
2022 ;; Ignore W unless IGNORE is non-nil.
2023 (not ignore))))
2024 (hor
2025 (cond
2026 ((and (<= w-top posn)
2027 (< posn (+ w-top (window-pixel-height w))))
2028 ;; W is to the left or right of WINDOW and covers POSN.
2029 (when (or (and (eq direction 'left)
2030 (or (and (<= w-left first) (> w-left best-edge))
2031 (and wrap
2032 (window-at-side-p window 'left)
2033 (window-at-side-p w 'right))))
2034 (and (eq direction 'right)
2035 (or (and (>= w-left last) (< w-left best-edge))
2036 (and wrap
2037 (window-at-side-p window 'right)
2038 (window-at-side-p w 'left)))))
2039 (setq best-edge w-left)
2040 (setq best w)))
2041 ((and (or (and (eq direction 'left)
2042 (<= (+ w-left (window-pixel-width w)) first))
2043 (and (eq direction 'right) (<= last w-left)))
2044 ;; W is to the left or right of WINDOW but does not
2045 ;; cover POSN.
2046 (setq best-diff-2-new
2047 (window--in-direction-2 w posn hor))
2048 (or (< best-diff-2-new best-diff-2)
2049 (and (= best-diff-2-new best-diff-2)
2050 (if (eq direction 'left)
2051 (> w-left best-edge-2)
2052 (< w-left best-edge-2)))))
2053 (setq best-edge-2 w-left)
2054 (setq best-diff-2 best-diff-2-new)
2055 (setq best-2 w))))
2056 ((and (<= w-left posn)
2057 (< posn (+ w-left (window-pixel-width w))))
2058 ;; W is above or below WINDOW and covers POSN.
2059 (when (or (and (eq direction 'above)
2060 (or (and (<= w-top first) (> w-top best-edge))
2061 (and wrap
2062 (window-at-side-p window 'top)
2063 (if (active-minibuffer-window)
2064 (minibuffer-window-active-p w)
2065 (window-at-side-p w 'bottom)))))
2066 (and (eq direction 'below)
2067 (or (and (>= w-top first) (< w-top best-edge))
2068 (and wrap
2069 (if (active-minibuffer-window)
2070 (minibuffer-window-active-p window)
2071 (window-at-side-p window 'bottom))
2072 (window-at-side-p w 'top)))))
2073 (setq best-edge w-top)
2074 (setq best w)))
2075 ((and (or (and (eq direction 'above)
2076 (<= (+ w-top (window-pixel-height w)) first))
2077 (and (eq direction 'below) (<= last w-top)))
2078 ;; W is above or below WINDOW but does not cover POSN.
2079 (setq best-diff-2-new
2080 (window--in-direction-2 w posn hor))
2081 (or (< best-diff-2-new best-diff-2)
2082 (and (= best-diff-2-new best-diff-2)
2083 (if (eq direction 'above)
2084 (> w-top best-edge-2)
2085 (< w-top best-edge-2)))))
2086 (setq best-edge-2 w-top)
2087 (setq best-diff-2 best-diff-2-new)
2088 (setq best-2 w)))))
2089 frame nil (and mini t))
2090 (or best best-2)))
2092 (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
2093 "Return a live window satisfying PREDICATE.
2094 More precisely, cycle through all windows calling the function
2095 PREDICATE on each one of them with the window as its sole
2096 argument. Return the first window for which PREDICATE returns
2097 non-nil. Windows are scanned starting with the window following
2098 the selected window. If no window satisfies PREDICATE, return
2099 DEFAULT.
2101 MINIBUF t means include the minibuffer window even if the
2102 minibuffer is not active. MINIBUF nil or omitted means include
2103 the minibuffer window only if the minibuffer is active. Any
2104 other value means do not include the minibuffer window even if
2105 the minibuffer is active.
2107 ALL-FRAMES nil or omitted means consider all windows on the selected
2108 frame, plus the minibuffer window if specified by the MINIBUF
2109 argument. If the minibuffer counts, consider all windows on all
2110 frames that share that minibuffer too. The following non-nil
2111 values of ALL-FRAMES have special meanings:
2113 - t means consider all windows on all existing frames.
2115 - `visible' means consider all windows on all visible frames on
2116 the current terminal.
2118 - 0 (the number zero) means consider all windows on all visible
2119 and iconified frames on the current terminal.
2121 - A frame means consider all windows on that frame only.
2123 Anything else means consider all windows on the selected frame
2124 and no others."
2125 (catch 'found
2126 (dolist (window (window-list-1
2127 (next-window nil minibuf all-frames)
2128 minibuf all-frames))
2129 (when (funcall predicate window)
2130 (throw 'found window)))
2131 default))
2133 (defalias 'some-window 'get-window-with-predicate)
2135 (defun get-lru-window (&optional all-frames dedicated not-selected)
2136 "Return the least recently used window on frames specified by ALL-FRAMES.
2137 Return a full-width window if possible. A minibuffer window is
2138 never a candidate. A dedicated window is never a candidate
2139 unless DEDICATED is non-nil, so if all windows are dedicated, the
2140 value is nil. Avoid returning the selected window if possible.
2141 Optional argument NOT-SELECTED non-nil means never return the
2142 selected window.
2144 The following non-nil values of the optional argument ALL-FRAMES
2145 have special meanings:
2147 - t means consider all windows on all existing frames.
2149 - `visible' means consider all windows on all visible frames on
2150 the current terminal.
2152 - 0 (the number zero) means consider all windows on all visible
2153 and iconified frames on the current terminal.
2155 - A frame means consider all windows on that frame only.
2157 Any other value of ALL-FRAMES means consider all windows on the
2158 selected frame and no others."
2159 (let (best-window best-time second-best-window second-best-time time)
2160 (dolist (window (window-list-1 nil 'nomini all-frames))
2161 (when (and (or dedicated (not (window-dedicated-p window)))
2162 (or (not not-selected) (not (eq window (selected-window)))))
2163 (setq time (window-use-time window))
2164 (if (or (eq window (selected-window))
2165 (not (window-full-width-p window)))
2166 (when (or (not second-best-time) (< time second-best-time))
2167 (setq second-best-time time)
2168 (setq second-best-window window))
2169 (when (or (not best-time) (< time best-time))
2170 (setq best-time time)
2171 (setq best-window window)))))
2172 (or best-window second-best-window)))
2174 (defun get-mru-window (&optional all-frames dedicated not-selected)
2175 "Return the most recently used window on frames specified by ALL-FRAMES.
2176 A minibuffer window is never a candidate. A dedicated window is
2177 never a candidate unless DEDICATED is non-nil, so if all windows
2178 are dedicated, the value is nil. Optional argument NOT-SELECTED
2179 non-nil means never return the selected window.
2181 The following non-nil values of the optional argument ALL-FRAMES
2182 have special meanings:
2184 - t means consider all windows on all existing frames.
2186 - `visible' means consider all windows on all visible frames on
2187 the current terminal.
2189 - 0 (the number zero) means consider all windows on all visible
2190 and iconified frames on the current terminal.
2192 - A frame means consider all windows on that frame only.
2194 Any other value of ALL-FRAMES means consider all windows on the
2195 selected frame and no others."
2196 (let (best-window best-time time)
2197 (dolist (window (window-list-1 nil 'nomini all-frames))
2198 (setq time (window-use-time window))
2199 (when (and (or dedicated (not (window-dedicated-p window)))
2200 (or (not not-selected) (not (eq window (selected-window))))
2201 (or (not best-time) (> time best-time)))
2202 (setq best-time time)
2203 (setq best-window window)))
2204 best-window))
2206 (defun get-largest-window (&optional all-frames dedicated not-selected)
2207 "Return the largest window on frames specified by ALL-FRAMES.
2208 A minibuffer window is never a candidate. A dedicated window is
2209 never a candidate unless DEDICATED is non-nil, so if all windows
2210 are dedicated, the value is nil. Optional argument NOT-SELECTED
2211 non-nil means never return the selected window.
2213 The following non-nil values of the optional argument ALL-FRAMES
2214 have special meanings:
2216 - t means consider all windows on all existing frames.
2218 - `visible' means consider all windows on all visible frames on
2219 the current terminal.
2221 - 0 (the number zero) means consider all windows on all visible
2222 and iconified frames on the current terminal.
2224 - A frame means consider all windows on that frame only.
2226 Any other value of ALL-FRAMES means consider all windows on the
2227 selected frame and no others."
2228 (let ((best-size 0)
2229 best-window size)
2230 (dolist (window (window-list-1 nil 'nomini all-frames))
2231 (when (and (or dedicated (not (window-dedicated-p window)))
2232 (or (not not-selected) (not (eq window (selected-window)))))
2233 (setq size (* (window-pixel-height window)
2234 (window-pixel-width window)))
2235 (when (> size best-size)
2236 (setq best-size size)
2237 (setq best-window window))))
2238 best-window))
2240 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
2241 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
2242 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2243 and defaults to the current buffer. Windows are scanned starting
2244 with the selected window.
2246 MINIBUF t means include the minibuffer window even if the
2247 minibuffer is not active. MINIBUF nil or omitted means include
2248 the minibuffer window only if the minibuffer is active. Any
2249 other value means do not include the minibuffer window even if
2250 the minibuffer is active.
2252 ALL-FRAMES nil or omitted means consider all windows on the
2253 selected frame, plus the minibuffer window if specified by the
2254 MINIBUF argument. If the minibuffer counts, consider all windows
2255 on all frames that share that minibuffer too. The following
2256 non-nil values of ALL-FRAMES have special meanings:
2258 - t means consider all windows on all existing frames.
2260 - `visible' means consider all windows on all visible frames on
2261 the current terminal.
2263 - 0 (the number zero) means consider all windows on all visible
2264 and iconified frames on the current terminal.
2266 - A frame means consider all windows on that frame only.
2268 Anything else means consider all windows on the selected frame
2269 and no others."
2270 (let ((buffer (window-normalize-buffer buffer-or-name))
2271 windows)
2272 (dolist (window (window-list-1 (selected-window) minibuf all-frames))
2273 (when (eq (window-buffer window) buffer)
2274 (setq windows (cons window windows))))
2275 (nreverse windows)))
2277 (defun minibuffer-window-active-p (window)
2278 "Return t if WINDOW is the currently active minibuffer window."
2279 (eq window (active-minibuffer-window)))
2281 (defun count-windows (&optional minibuf)
2282 "Return the number of live windows on the selected frame.
2283 The optional argument MINIBUF specifies whether the minibuffer
2284 window shall be counted. See `walk-windows' for the precise
2285 meaning of this argument."
2286 (length (window-list-1 nil minibuf)))
2288 ;;; Resizing windows.
2289 (defun window--size-to-pixel (window size &optional horizontal pixelwise round-maybe)
2290 "For WINDOW convert SIZE lines to pixels.
2291 SIZE is supposed to specify a height of WINDOW in terms of text
2292 lines. The return value is the number of pixels specifying that
2293 height.
2295 WINDOW must be a valid window. Optional argument HORIZONTAL
2296 non-nil means convert SIZE columns to pixels.
2298 Optional argument PIXELWISE non-nil means SIZE already specifies
2299 pixels but may have to be adjusted to a multiple of the character
2300 size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
2301 means round to the nearest multiple of the character size of
2302 WINDOW's frame if the option `window-resize-pixelwise' is nil."
2303 (setq window (window-normalize-window window))
2304 (let ((char-size (frame-char-size window horizontal)))
2305 (if pixelwise
2306 (if (and round-maybe (not window-resize-pixelwise))
2307 (* (round size char-size) char-size)
2308 size)
2309 (* size char-size))))
2311 (defun window--pixel-to-total-1 (window horizontal char-size)
2312 "Subroutine of `window--pixel-to-total'."
2313 (let ((child (window-child window)))
2314 (if (window-combination-p window horizontal)
2315 ;; In an iso-combination distribute sizes proportionally.
2316 (let ((remainder (window-new-total window))
2317 size best-child rem best-rem)
2318 ;; Initialize total sizes to each child's floor.
2319 (while child
2320 (setq size (max (/ (window-size child horizontal t) char-size) 1))
2321 (set-window-new-total child size)
2322 (setq remainder (- remainder size))
2323 (setq child (window-next-sibling child)))
2324 ;; Distribute remainder.
2325 (while (> remainder 0)
2326 (setq child (window-last-child window))
2327 (setq best-child nil)
2328 (setq best-rem 0)
2329 (while child
2330 (when (and (<= (window-new-total child)
2331 (/ (window-size child horizontal t) char-size))
2332 (> (setq rem (% (window-size child horizontal t)
2333 char-size))
2334 best-rem))
2335 (setq best-child child)
2336 (setq best-rem rem))
2337 (setq child (window-prev-sibling child)))
2338 ;; We MUST have a best-child here.
2339 (set-window-new-total best-child 1 t)
2340 (setq remainder (1- remainder)))
2341 ;; Recurse.
2342 (setq child (window-child window))
2343 (while child
2344 (window--pixel-to-total-1 child horizontal char-size)
2345 (setq child (window-next-sibling child))))
2346 ;; In an ortho-combination assign new sizes directly.
2347 (let ((size (window-new-total window)))
2348 (while child
2349 (set-window-new-total child size)
2350 (window--pixel-to-total-1 child horizontal char-size)
2351 (setq child (window-next-sibling child)))))))
2353 (defun window--pixel-to-total (&optional frame horizontal)
2354 "On FRAME assign new total window heights from pixel heights.
2355 FRAME must be a live frame and defaults to the selected frame.
2357 Optional argument HORIZONTAL non-nil means assign new total
2358 window widths from pixel widths."
2359 (setq frame (window-normalize-frame frame))
2360 (let* ((char-size (frame-char-size frame horizontal))
2361 (root (frame-root-window frame))
2362 (root-size (window-size root horizontal t))
2363 ;; We have to care about the minibuffer window only if it
2364 ;; appears together with the root window on this frame.
2365 (mini (let ((mini (minibuffer-window frame)))
2366 (and (eq (window-frame mini) frame)
2367 (not (eq mini root)) mini)))
2368 (mini-size (and mini (window-size mini horizontal t))))
2369 ;; We round the line/column sizes of windows here to the nearest
2370 ;; integer. In some cases this can make windows appear _larger_
2371 ;; than the containing frame (line/column-wise) because the latter's
2372 ;; sizes are not (yet) rounded. We might eventually fix that.
2373 (if (and mini (not horizontal))
2374 (let (lines)
2375 (set-window-new-total root (max (/ root-size char-size) 1))
2376 (set-window-new-total mini (max (/ mini-size char-size) 1))
2377 (setq lines (- (round (+ root-size mini-size) char-size)
2378 (+ (window-new-total root) (window-new-total mini))))
2379 (while (> lines 0)
2380 (if (>= (% root-size (window-new-total root))
2381 (% mini-size (window-new-total mini)))
2382 (set-window-new-total root 1 t)
2383 (set-window-new-total mini 1 t))
2384 (setq lines (1- lines))))
2385 (set-window-new-total root (round root-size char-size))
2386 (when mini
2387 ;; This is taken in the horizontal case only.
2388 (set-window-new-total mini (round mini-size char-size))))
2389 (unless (window-buffer root)
2390 (window--pixel-to-total-1 root horizontal char-size))
2391 ;; Apply the new sizes.
2392 (window-resize-apply-total frame horizontal)))
2394 (defun window--resize-reset (&optional frame horizontal)
2395 "Reset resize values for all windows on FRAME.
2396 FRAME defaults to the selected frame.
2398 This function stores the current value of `window-size' applied
2399 with argument HORIZONTAL in the new total size of all windows on
2400 FRAME. It also resets the new normal size of each of these
2401 windows."
2402 (window--resize-reset-1
2403 (frame-root-window (window-normalize-frame frame)) horizontal))
2405 (defun window--resize-reset-1 (window horizontal)
2406 "Internal function of `window--resize-reset'."
2407 ;; Register old size in the new total size.
2408 (set-window-new-pixel window (window-size window horizontal t))
2409 (set-window-new-total window (window-size window horizontal))
2410 ;; Reset new normal size.
2411 (set-window-new-normal window)
2412 (when (window-child window)
2413 (window--resize-reset-1 (window-child window) horizontal))
2414 (when (window-right window)
2415 (window--resize-reset-1 (window-right window) horizontal)))
2417 ;; The following routine is used to manually resize the minibuffer
2418 ;; window and is currently used, for example, by ispell.el.
2419 (defun window--resize-mini-window (window delta)
2420 "Resize minibuffer window WINDOW by DELTA pixels.
2421 If WINDOW cannot be resized by DELTA pixels make it as large (or
2422 as small) as possible, but don't signal an error."
2423 (when (window-minibuffer-p window)
2424 (let* ((frame (window-frame window))
2425 (root (frame-root-window frame))
2426 (height (window-pixel-height window))
2427 (min-delta
2428 (- (window-pixel-height root)
2429 (window-min-size root nil nil t))))
2430 ;; Sanitize DELTA.
2431 (cond
2432 ((<= (+ height delta) 0)
2433 (setq delta (- (frame-char-height (window-frame window)) height)))
2434 ((> delta min-delta)
2435 (setq delta min-delta)))
2437 (unless (zerop delta)
2438 ;; Resize now.
2439 (window--resize-reset frame)
2440 ;; Ideally we should be able to resize just the last child of root
2441 ;; here. See the comment in `resize-root-window-vertically' for
2442 ;; why we do not do that.
2443 (window--resize-this-window root (- delta) nil nil t)
2444 (set-window-new-pixel window (+ height delta))
2445 ;; The following routine catches the case where we want to resize
2446 ;; a minibuffer-only frame.
2447 (when (resize-mini-window-internal window)
2448 (window--pixel-to-total frame)
2449 (run-window-configuration-change-hook frame))))))
2451 (defun window--resize-apply-p (frame &optional horizontal)
2452 "Return t when a window on FRAME shall be resized vertically.
2453 Optional argument HORIZONTAL non-nil means return t when a window
2454 shall be resized horizontally."
2455 (catch 'apply
2456 (walk-window-tree
2457 (lambda (window)
2458 (unless (= (window-new-pixel window)
2459 (window-size window horizontal t))
2460 (throw 'apply t)))
2461 frame t)
2462 nil))
2464 (defun window-resize (window delta &optional horizontal ignore pixelwise)
2465 "Resize WINDOW vertically by DELTA lines.
2466 WINDOW can be an arbitrary window and defaults to the selected
2467 one. An attempt to resize the root window of a frame will raise
2468 an error though.
2470 DELTA a positive number means WINDOW shall be enlarged by DELTA
2471 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
2472 lines.
2474 Optional argument HORIZONTAL non-nil means resize WINDOW
2475 horizontally by DELTA columns. In this case a positive DELTA
2476 means enlarge WINDOW by DELTA columns. DELTA negative means
2477 WINDOW shall be shrunk by -DELTA columns.
2479 Optional argument IGNORE, if non-nil, means to ignore restraints
2480 induced by fixed size windows or the values of the variables
2481 `window-min-height' and `window-min-width'. The following values
2482 have special meanings: `safe' means that in addition live windows
2483 are allowed to get as small as `window-safe-min-height' lines and
2484 `window-safe-min-width' columns. `preserved' means to ignore
2485 only restrictions induced by `window-preserve-size'. If IGNORE
2486 is a window, then ignore restrictions for that window only.
2488 Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
2489 pixels.
2491 This function resizes other windows proportionally and never
2492 deletes any windows. If you want to move only the low (right)
2493 edge of WINDOW consider using `adjust-window-trailing-edge'
2494 instead."
2495 (setq window (window-normalize-window window))
2496 (let* ((frame (window-frame window))
2497 (minibuffer-window (minibuffer-window frame))
2498 sibling)
2499 (setq delta (window--size-to-pixel
2500 window delta horizontal pixelwise t))
2501 (cond
2502 ((eq window (frame-root-window frame))
2503 (error "Cannot resize the root window of a frame"))
2504 ((window-minibuffer-p window)
2505 (if horizontal
2506 (error "Cannot resize minibuffer window horizontally")
2507 (window--resize-mini-window window delta)))
2508 ((and (not horizontal)
2509 (window-full-height-p window)
2510 (eq (window-frame minibuffer-window) frame)
2511 (or (not resize-mini-windows)
2512 (eq minibuffer-window (active-minibuffer-window))))
2513 ;; If WINDOW is full height and either `resize-mini-windows' is
2514 ;; nil or the minibuffer window is active, resize the minibuffer
2515 ;; window.
2516 (window--resize-mini-window minibuffer-window (- delta)))
2517 ((or (window--resizable-p
2518 window delta horizontal ignore nil nil nil t)
2519 (and (not ignore)
2520 (setq ignore 'preserved)
2521 (window--resizable-p
2522 window delta horizontal ignore nil nil nil t)))
2523 (window--resize-reset frame horizontal)
2524 (window--resize-this-window window delta horizontal ignore t)
2525 (if (and (not window-combination-resize)
2526 (window-combined-p window horizontal)
2527 (setq sibling (or (window-right window) (window-left window)))
2528 (window-sizable-p
2529 sibling (- delta) horizontal ignore t))
2530 ;; If window-combination-resize is nil, WINDOW is part of an
2531 ;; iso-combination, and WINDOW's neighboring right or left
2532 ;; sibling can be resized as requested, resize that sibling.
2533 (let ((normal-delta
2534 (/ (float delta)
2535 (window-size (window-parent window) horizontal t))))
2536 (window--resize-this-window sibling (- delta) horizontal nil t)
2537 (set-window-new-normal
2538 window (+ (window-normal-size window horizontal)
2539 normal-delta))
2540 (set-window-new-normal
2541 sibling (- (window-normal-size sibling horizontal)
2542 normal-delta)))
2543 ;; Otherwise, resize all other windows in the same combination.
2544 (window--resize-siblings window delta horizontal ignore))
2545 (when (window--resize-apply-p frame horizontal)
2546 (if (window-resize-apply frame horizontal)
2547 (progn
2548 (window--pixel-to-total frame horizontal)
2549 (run-window-configuration-change-hook frame))
2550 (error "Failed to apply resizing %s" window))))
2552 (error "Cannot resize window %s" window)))))
2554 (defun window-resize-no-error (window delta &optional horizontal ignore pixelwise)
2555 "Resize WINDOW vertically if it is resizable by DELTA lines.
2556 This function is like `window-resize' but does not signal an
2557 error when WINDOW cannot be resized. For the meaning of the
2558 optional arguments see the documentation of `window-resize'.
2560 Optional argument PIXELWISE non-nil means interpret DELTA as
2561 pixels."
2562 (when (window--resizable-p
2563 window delta horizontal ignore nil nil nil pixelwise)
2564 (window-resize window delta horizontal ignore pixelwise)))
2566 (defun window--resize-child-windows-skip-p (window)
2567 "Return non-nil if WINDOW shall be skipped by resizing routines."
2568 (memq (window-new-normal window) '(ignore stuck skip)))
2570 (defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
2571 "Recursively set new normal height of child windows of window PARENT.
2572 HORIZONTAL non-nil means set the new normal width of these
2573 windows. WINDOW specifies a child window of PARENT that has been
2574 resized by THIS-DELTA lines (columns).
2576 Optional argument TRAIL either `before' or `after' means set values
2577 only for windows before or after WINDOW. Optional argument
2578 OTHER-DELTA, a number, specifies that this many lines (columns)
2579 have been obtained from (or returned to) an ancestor window of
2580 PARENT in order to resize WINDOW."
2581 (let* ((delta-normal
2582 (if (and (= (- this-delta)
2583 (window-size window horizontal t))
2584 (zerop other-delta))
2585 ;; When WINDOW gets deleted and we can return its entire
2586 ;; space to its siblings, use WINDOW's normal size as the
2587 ;; normal delta.
2588 (- (window-normal-size window horizontal))
2589 ;; In any other case calculate the normal delta from the
2590 ;; relation of THIS-DELTA to the total size of PARENT.
2591 (/ (float this-delta)
2592 (window-size parent horizontal t))))
2593 (sub (window-child parent))
2594 (parent-normal 0.0)
2595 (skip (eq trail 'after)))
2597 ;; Set parent-normal to the sum of the normal sizes of all child
2598 ;; windows of PARENT that shall be resized, excluding only WINDOW
2599 ;; and any windows specified by the optional TRAIL argument.
2600 (while sub
2601 (cond
2602 ((eq sub window)
2603 (setq skip (eq trail 'before)))
2604 (skip)
2606 (setq parent-normal
2607 (+ parent-normal (window-normal-size sub horizontal)))))
2608 (setq sub (window-right sub)))
2610 ;; Set the new normal size of all child windows of PARENT from what
2611 ;; they should have contributed for recovering THIS-DELTA lines
2612 ;; (columns).
2613 (setq sub (window-child parent))
2614 (setq skip (eq trail 'after))
2615 (while sub
2616 (cond
2617 ((eq sub window)
2618 (setq skip (eq trail 'before)))
2619 (skip)
2621 (let ((old-normal (window-normal-size sub horizontal)))
2622 (set-window-new-normal
2623 sub (min 1.0 ; Don't get larger than 1.
2624 (max (- old-normal
2625 (* (/ old-normal parent-normal)
2626 delta-normal))
2627 ;; Don't drop below 0.
2628 0.0))))))
2629 (setq sub (window-right sub)))
2631 (when (numberp other-delta)
2632 ;; Set the new normal size of windows from what they should have
2633 ;; contributed for recovering OTHER-DELTA lines (columns).
2634 (setq delta-normal (/ (float (window-size parent horizontal t))
2635 (+ (window-size parent horizontal t)
2636 other-delta)))
2637 (setq sub (window-child parent))
2638 (setq skip (eq trail 'after))
2639 (while sub
2640 (cond
2641 ((eq sub window)
2642 (setq skip (eq trail 'before)))
2643 (skip)
2645 (set-window-new-normal
2646 sub (min 1.0 ; Don't get larger than 1.
2647 (max (* (window-new-normal sub) delta-normal)
2648 ;; Don't drop below 0.
2649 0.0)))))
2650 (setq sub (window-right sub))))
2652 ;; Set the new normal size of WINDOW to what is left by the sum of
2653 ;; the normal sizes of its siblings.
2654 (set-window-new-normal
2655 window
2656 (let ((sum 0))
2657 (setq sub (window-child parent))
2658 (while sub
2659 (cond
2660 ((eq sub window))
2661 ((not (numberp (window-new-normal sub)))
2662 (setq sum (+ sum (window-normal-size sub horizontal))))
2664 (setq sum (+ sum (window-new-normal sub)))))
2665 (setq sub (window-right sub)))
2666 ;; Don't get larger than 1 or smaller than 0.
2667 (min 1.0 (max (- 1.0 sum) 0.0))))))
2669 (defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge char-size)
2670 "Resize child windows of window PARENT vertically by DELTA pixels.
2671 PARENT must be a vertically combined internal window.
2673 Optional argument HORIZONTAL non-nil means resize child windows
2674 of PARENT horizontally by DELTA pixels. In this case PARENT must
2675 be a horizontally combined internal window.
2677 WINDOW, if specified, must denote a child window of PARENT that
2678 is resized by DELTA pixels.
2680 The optional argument IGNORE has the same meaning as for
2681 `window-resizable'.
2683 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2684 of windows that shall be resized. If TRAIL equals `before',
2685 resize only windows on the left or above EDGE. If TRAIL equals
2686 `after', resize only windows on the right or below EDGE. Also,
2687 preferably only resize windows adjacent to EDGE.
2689 If the optional argument CHAR-SIZE is a positive integer, it specifies
2690 the number of pixels by which windows are incrementally resized.
2691 If CHAR-SIZE is nil, this means to use the value of
2692 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2694 Return the symbol `normalized' if new normal sizes have been
2695 already set by this routine."
2696 (let* ((first (window-child parent))
2697 (last (window-last-child parent))
2698 (parent-total (+ (window-size parent horizontal t)
2699 delta))
2700 (char-size (or char-size
2701 (and window-resize-pixelwise 1)
2702 (frame-char-size window horizontal)))
2703 sub best-window best-value best-delta)
2705 (if (and edge (memq trail '(before after))
2706 (progn
2707 (setq sub first)
2708 (while (and (window-right sub)
2709 (or (and (eq trail 'before)
2710 (not (window--resize-child-windows-skip-p
2711 (window-right sub))))
2712 (and (eq trail 'after)
2713 (window--resize-child-windows-skip-p sub))))
2714 (setq sub (window-right sub)))
2715 sub)
2716 (if horizontal
2717 (if (eq trail 'before)
2718 (= (+ (window-pixel-left sub) (window-pixel-width sub))
2719 edge)
2720 (= (window-pixel-left sub) edge))
2721 (if (eq trail 'before)
2722 (= (+ (window-pixel-top sub) (window-pixel-height sub))
2723 edge)
2724 (= (window-pixel-top sub) edge)))
2725 (window-sizable-p sub delta horizontal ignore t))
2726 ;; Resize only windows adjacent to EDGE.
2727 (progn
2728 (window--resize-this-window
2729 sub delta horizontal ignore t trail edge)
2730 (if (and window (eq (window-parent sub) parent))
2731 (progn
2732 ;; Assign new normal sizes.
2733 (set-window-new-normal
2734 sub (/ (float (window-new-pixel sub)) parent-total))
2735 (set-window-new-normal
2736 window (- (window-normal-size window horizontal)
2737 (- (window-new-normal sub)
2738 (window-normal-size sub horizontal)))))
2739 (window--resize-child-windows-normal
2740 parent horizontal sub 0 trail delta))
2741 ;; Return 'normalized to notify `window--resize-siblings' that
2742 ;; normal sizes have been already set.
2743 'normalized)
2744 ;; Resize all windows proportionally.
2745 (setq sub last)
2746 (while sub
2747 (cond
2748 ((or (window--resize-child-windows-skip-p sub)
2749 ;; Ignore windows to skip and fixed-size child windows -
2750 ;; in the latter case make it a window to skip.
2751 (and (not ignore)
2752 (window-size-fixed-p sub horizontal ignore)
2753 (set-window-new-normal sub 'ignore))))
2754 ((< delta 0)
2755 ;; When shrinking store the number of lines/cols we can get
2756 ;; from this window here together with the total/normal size
2757 ;; factor.
2758 (set-window-new-normal
2760 (cons
2761 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
2762 (window-min-delta sub horizontal ignore trail t nil t)
2763 (- (/ (float (window-size sub horizontal t))
2764 parent-total)
2765 (window-normal-size sub horizontal)))))
2766 ((> delta 0)
2767 ;; When enlarging store the total/normal size factor only
2768 (set-window-new-normal
2770 (- (/ (float (window-size sub horizontal t))
2771 parent-total)
2772 (window-normal-size sub horizontal)))))
2774 (setq sub (window-left sub)))
2776 (cond
2777 ((< delta 0)
2778 ;; Shrink windows by delta.
2779 (setq best-window t)
2780 (while (and best-window (not (zerop delta)))
2781 (setq sub last)
2782 (setq best-window nil)
2783 (setq best-value most-negative-fixnum)
2784 (while sub
2785 (when (and (consp (window-new-normal sub))
2786 (not (<= (car (window-new-normal sub)) 0))
2787 (> (cdr (window-new-normal sub)) best-value))
2788 (setq best-window sub)
2789 (setq best-value (cdr (window-new-normal sub))))
2791 (setq sub (window-left sub)))
2793 (when best-window
2794 (setq best-delta (min (car (window-new-normal best-window))
2795 char-size (- delta)))
2796 (setq delta (+ delta best-delta))
2797 (set-window-new-pixel best-window (- best-delta) t)
2798 (set-window-new-normal
2799 best-window
2800 (if (= (car (window-new-normal best-window)) best-delta)
2801 'skip ; We can't shrink best-window any further.
2802 (cons (- (car (window-new-normal best-window)) best-delta)
2803 (- (/ (float (window-new-pixel best-window))
2804 parent-total)
2805 (window-normal-size best-window horizontal))))))))
2806 ((> delta 0)
2807 ;; Enlarge windows by delta.
2808 (setq best-window t)
2809 (while (and best-window (not (zerop delta)))
2810 (setq sub last)
2811 (setq best-window nil)
2812 (setq best-value most-positive-fixnum)
2813 (while sub
2814 (when (and (numberp (window-new-normal sub))
2815 (< (window-new-normal sub) best-value))
2816 (setq best-window sub)
2817 (setq best-value (window-new-normal sub)))
2819 (setq sub (window-left sub)))
2821 (when best-window
2822 (setq best-delta (min delta char-size))
2823 (setq delta (- delta best-delta))
2824 (set-window-new-pixel best-window best-delta t)
2825 (set-window-new-normal
2826 best-window
2827 (- (/ (float (window-new-pixel best-window))
2828 parent-total)
2829 (window-normal-size best-window horizontal)))))))
2831 (when best-window
2832 (setq sub last)
2833 (while sub
2834 (when (or (consp (window-new-normal sub))
2835 (numberp (window-new-normal sub)))
2836 ;; Reset new normal size fields so `window-resize-apply'
2837 ;; won't use them to apply new sizes.
2838 (set-window-new-normal sub))
2840 (unless (eq (window-new-normal sub) 'ignore)
2841 ;; Resize this window's child windows (back-engineering
2842 ;; delta from sub's old and new total sizes).
2843 (let ((delta (- (window-new-pixel sub)
2844 (window-size sub horizontal t))))
2845 (unless (and (zerop delta) (not trail))
2846 ;; For the TRAIL non-nil case we have to resize SUB
2847 ;; recursively even if it's size does not change.
2848 (window--resize-this-window
2849 sub delta horizontal ignore nil trail edge))))
2850 (setq sub (window-left sub)))))))
2852 (defun window--resize-siblings (window delta &optional horizontal ignore trail edge char-size)
2853 "Resize other windows when WINDOW is resized vertically by DELTA pixels.
2854 Optional argument HORIZONTAL non-nil means resize other windows
2855 when WINDOW is resized horizontally by DELTA pixels. WINDOW
2856 itself is not resized by this function.
2858 The optional argument IGNORE has the same meaning as for
2859 `window-resizable'.
2861 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2862 of windows that shall be resized. If TRAIL equals `before',
2863 resize only windows on the left or above EDGE. If TRAIL equals
2864 `after', resize only windows on the right or below EDGE. Also,
2865 preferably only resize windows adjacent to EDGE."
2866 (when (window-parent window)
2867 (let* ((parent (window-parent window))
2868 (sub (window-child parent)))
2869 (if (window-combined-p sub horizontal)
2870 ;; In an iso-combination try to extract DELTA from WINDOW's
2871 ;; siblings.
2872 (let ((skip (eq trail 'after))
2873 this-delta other-delta)
2874 ;; Decide which windows shall be left alone.
2875 (while sub
2876 (cond
2877 ((eq sub window)
2878 ;; Make sure WINDOW is left alone when
2879 ;; resizing its siblings.
2880 (set-window-new-normal sub 'ignore)
2881 (setq skip (eq trail 'before)))
2882 (skip
2883 ;; Make sure this sibling is left alone when
2884 ;; resizing its siblings.
2885 (set-window-new-normal sub 'ignore))
2886 ((not (window-size-fixed-p sub horizontal ignore))
2887 ;; Set this-delta to t to signal that we found a sibling
2888 ;; of WINDOW whose size is not fixed.
2889 (setq this-delta t)))
2891 (setq sub (window-right sub)))
2893 ;; Set this-delta to what we can get from WINDOW's siblings.
2894 (if (= (- delta) (window-size window horizontal t))
2895 ;; A deletion, presumably. We must handle this case
2896 ;; specially since `window--resizable' can't be used.
2897 (if this-delta
2898 ;; There's at least one resizable sibling we can
2899 ;; give WINDOW's size to.
2900 (setq this-delta delta)
2901 ;; No resizable sibling exists.
2902 (setq this-delta 0))
2903 ;; Any other form of resizing.
2904 (setq this-delta
2905 (window--resizable
2906 window delta horizontal ignore trail t nil t)))
2908 ;; Set other-delta to what we still have to get from
2909 ;; ancestor windows of parent.
2910 (setq other-delta (- delta this-delta))
2911 (unless (zerop other-delta)
2912 ;; Unless we got everything from WINDOW's siblings, PARENT
2913 ;; must be resized by other-delta lines or columns.
2914 (set-window-new-pixel parent other-delta 'add))
2916 (if (zerop this-delta)
2917 ;; We haven't got anything from WINDOW's siblings but we
2918 ;; must update the normal sizes to respect other-delta.
2919 (window--resize-child-windows-normal
2920 parent horizontal window this-delta trail other-delta)
2921 ;; We did get something from WINDOW's siblings which means
2922 ;; we have to resize their child windows.
2923 (unless (eq (window--resize-child-windows
2924 parent (- this-delta) horizontal
2925 window ignore trail edge char-size)
2926 ;; If `window--resize-child-windows' returns
2927 ;; 'normalized, this means it has set the
2928 ;; normal sizes already.
2929 'normalized)
2930 ;; Set the normal sizes.
2931 (window--resize-child-windows-normal
2932 parent horizontal window this-delta trail other-delta))
2933 ;; Set DELTA to what we still have to get from ancestor
2934 ;; windows.
2935 (setq delta other-delta)))
2937 ;; In an ortho-combination all siblings of WINDOW must be
2938 ;; resized by DELTA.
2939 (set-window-new-pixel parent delta 'add)
2940 (while sub
2941 (unless (eq sub window)
2942 (window--resize-this-window
2943 sub delta horizontal ignore t))
2944 (setq sub (window-right sub))))
2946 (unless (zerop delta)
2947 ;; "Go up."
2948 (window--resize-siblings
2949 parent delta horizontal ignore trail edge char-size)))))
2951 (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge char-size)
2952 "Resize WINDOW vertically by DELTA pixels.
2953 Optional argument HORIZONTAL non-nil means resize WINDOW
2954 horizontally by DELTA pixels.
2956 The optional argument IGNORE has the same meaning as for
2957 `window-resizable'. Optional argument ADD non-nil means add
2958 DELTA to the new total size of WINDOW.
2960 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2961 of windows that shall be resized. If TRAIL equals `before',
2962 resize only windows on the left or above EDGE. If TRAIL equals
2963 `after', resize only windows on the right or below EDGE. Also,
2964 preferably only resize windows adjacent to EDGE.
2966 If the optional argument CHAR-SIZE is a positive integer, it specifies
2967 the number of pixels by which windows are incrementally resized.
2968 If CHAR-SIZE is nil, this means to use the value of
2969 `frame-char-height' or `frame-char-width' of WINDOW's frame.
2971 This function recursively resizes WINDOW's child windows to fit the
2972 new size. Make sure that WINDOW is `window--resizable' before
2973 calling this function. Note that this function does not resize
2974 siblings of WINDOW or WINDOW's parent window. You have to
2975 eventually call `window-resize-apply' in order to make resizing
2976 actually take effect."
2977 (when add
2978 ;; Add DELTA to the new total size of WINDOW.
2979 (set-window-new-pixel window delta t))
2981 (let ((sub (window-child window)))
2982 (cond
2983 ((not sub))
2984 ((window-combined-p sub horizontal)
2985 ;; In an iso-combination resize child windows according to their
2986 ;; normal sizes.
2987 (window--resize-child-windows
2988 window delta horizontal nil ignore trail edge char-size))
2989 ;; In an ortho-combination resize each child window by DELTA.
2991 (while sub
2992 (window--resize-this-window
2993 sub delta horizontal ignore t trail edge char-size)
2994 (setq sub (window-right sub)))))))
2996 (defun window--resize-root-window (window delta horizontal ignore pixelwise)
2997 "Resize root window WINDOW vertically by DELTA lines.
2998 HORIZONTAL non-nil means resize root window WINDOW horizontally
2999 by DELTA columns.
3001 IGNORE non-nil means ignore any restrictions imposed by fixed
3002 size windows, `window-min-height' or `window-min-width' settings.
3004 This function is only called by the frame resizing routines. It
3005 resizes windows proportionally and never deletes any windows."
3006 (when (and (windowp window) (numberp delta))
3007 (let ((pixel-delta
3008 (if pixelwise
3009 delta
3010 (window--size-to-pixel window delta horizontal))))
3011 (when (window-sizable-p window pixel-delta horizontal ignore t)
3012 (window--resize-reset (window-frame window) horizontal)
3013 (window--resize-this-window
3014 window pixel-delta horizontal ignore t)))))
3016 (defun window--resize-root-window-vertically (window delta pixelwise)
3017 "Resize root window WINDOW vertically by DELTA lines.
3018 If DELTA is less than zero and we can't shrink WINDOW by DELTA
3019 lines, shrink it as much as possible. If DELTA is greater than
3020 zero, this function can resize fixed-size windows in order to
3021 recover the necessary lines. Return the number of lines that
3022 were recovered.
3024 Third argument PIXELWISE non-nil means to interpret DELTA as
3025 pixels and return the number of pixels that were recovered.
3027 This function is called by the minibuffer window resizing
3028 routines."
3029 (let* ((frame (window-frame window))
3030 (pixel-delta
3031 (cond
3032 (pixelwise
3033 delta)
3034 ((numberp delta)
3035 (* (frame-char-height frame) delta))
3036 (t 0)))
3037 ignore)
3038 (cond
3039 ((zerop pixel-delta))
3040 ((< pixel-delta 0)
3041 (setq pixel-delta (window-sizable window pixel-delta nil nil pixelwise))
3042 (window--resize-reset frame)
3043 ;; When shrinking the root window, emulate an edge drag in order
3044 ;; to not resize other windows if we can avoid it (Bug#12419).
3045 (window--resize-this-window
3046 window pixel-delta nil ignore t 'before
3047 (+ (window-pixel-top window) (window-pixel-height window)))
3048 ;; Don't record new normal sizes to make sure that shrinking back
3049 ;; proportionally works as intended.
3050 (walk-window-tree
3051 (lambda (window) (set-window-new-normal window 'ignore)) frame t))
3052 ((> pixel-delta 0)
3053 (window--resize-reset frame)
3054 (unless (window-sizable window pixel-delta nil nil pixelwise)
3055 (setq ignore t))
3056 ;; When growing the root window, resize proportionally. This
3057 ;; should give windows back their original sizes (hopefully).
3058 (window--resize-this-window
3059 window pixel-delta nil ignore t)))
3060 ;; Return the possibly adjusted DELTA.
3061 (if pixelwise
3062 pixel-delta
3063 (/ pixel-delta (frame-char-height frame)))))
3065 (defun window--sanitize-window-sizes (frame horizontal)
3066 "Assert that all windows on FRAME are large enough.
3067 If necessary and possible, make sure that every window on frame
3068 FRAME has its minimum height. Optional argument HORIZONTAL
3069 non-nil means to make sure that every window on frame FRAME has
3070 its minimum width. The minimum height/width of a window is the
3071 respective value returned by `window-min-size' for that window.
3073 Return t if all windows were resized appropriately. Return nil
3074 if at least one window could not be resized as requested, which
3075 may happen when the FRAME is not large enough to accommodate it."
3076 (let ((value t))
3077 (walk-window-tree
3078 (lambda (window)
3079 (let ((delta (- (window-min-size window horizontal nil t)
3080 (window-size window horizontal t))))
3081 (when (> delta 0)
3082 (if (window-resizable-p window delta horizontal nil t)
3083 (window-resize window delta horizontal nil t)
3084 (setq value nil))))))
3085 value))
3087 (defun adjust-window-trailing-edge (window delta &optional horizontal pixelwise)
3088 "Move WINDOW's bottom edge by DELTA lines.
3089 Optional argument HORIZONTAL non-nil means move WINDOW's right
3090 edge by DELTA columns. WINDOW must be a valid window and
3091 defaults to the selected one.
3093 Optional argument PIXELWISE non-nil means interpret DELTA as
3094 number of pixels.
3096 If DELTA is greater than zero, move the edge downwards or to the
3097 right. If DELTA is less than zero, move the edge upwards or to
3098 the left. If the edge can't be moved by DELTA lines or columns,
3099 move it as far as possible in the desired direction."
3100 (setq window (window-normalize-window window))
3101 (let* ((frame (window-frame window))
3102 (minibuffer-window (minibuffer-window frame))
3103 (right window)
3104 left this-delta min-delta max-delta ignore)
3106 (unless pixelwise
3107 (setq pixelwise t)
3108 (setq delta (* delta (frame-char-size window horizontal))))
3110 ;; Find the edge we want to move.
3111 (while (and (or (not (window-combined-p right horizontal))
3112 (not (window-right right)))
3113 (setq right (window-parent right))))
3114 (cond
3115 ((and (not right) (not horizontal)
3116 ;; Resize the minibuffer window if it's on the same frame as
3117 ;; and immediately below WINDOW and it's either active or
3118 ;; `resize-mini-windows' is nil.
3119 (eq (window-frame minibuffer-window) frame)
3120 (= (nth 1 (window-pixel-edges minibuffer-window))
3121 (nth 3 (window-pixel-edges window)))
3122 (or (not resize-mini-windows)
3123 (eq minibuffer-window (active-minibuffer-window))))
3124 (window--resize-mini-window minibuffer-window (- delta)))
3125 ((or (not (setq left right)) (not (setq right (window-right right))))
3126 (if horizontal
3127 (user-error "No window on the right of this one")
3128 (user-error "No window below this one")))
3130 ;; Set LEFT to the first resizable window on the left. This step is
3131 ;; needed to handle fixed-size windows.
3132 (while (and left
3133 (or (window-size-fixed-p left horizontal)
3134 (and (< delta 0)
3135 (<= (window-size left horizontal t)
3136 (window-min-size left horizontal nil t)))))
3137 (setq left
3138 (or (window-left left)
3139 (progn
3140 (while (and (setq left (window-parent left))
3141 (not (window-combined-p left horizontal))))
3142 (window-left left)))))
3143 (unless left
3144 (setq ignore 'preserved)
3145 (setq left window)
3146 (while (and left
3147 (or (window-size-fixed-p left horizontal 'preserved)
3148 (<= (window-size left horizontal t)
3149 (window-min-size left horizontal 'preserved t))))
3150 (setq left
3151 (or (window-left left)
3152 (progn
3153 (while (and (setq left (window-parent left))
3154 (not (window-combined-p left horizontal))))
3155 (window-left left)))))
3157 (unless left
3158 (if horizontal
3159 (user-error "No resizable window on the left of this one")
3160 (user-error "No resizable window above this one"))))
3162 ;; Set RIGHT to the first resizable window on the right. This step
3163 ;; is needed to handle fixed-size windows.
3164 (while (and right
3165 (or (window-size-fixed-p right horizontal)
3166 (and (> delta 0)
3167 (<= (window-size right horizontal t)
3168 (window-min-size right horizontal 'preserved t)))))
3169 (setq right
3170 (or (window-right right)
3171 (progn
3172 (while (and (setq right (window-parent right))
3173 (not (window-combined-p right horizontal))))
3174 (window-right right)))))
3175 (unless right
3176 (setq ignore 'preserved)
3177 (setq right (window-right window))
3178 (while (and right
3179 (or (window-size-fixed-p right horizontal 'preserved))
3180 (<= (window-size right horizontal t)
3181 (window-min-size right horizontal nil t)))
3182 (setq right
3183 (or (window-right right)
3184 (progn
3185 (while (and (setq right (window-parent right))
3186 (not (window-combined-p right horizontal))))
3187 (window-right right)))))
3188 (unless right
3189 (if horizontal
3190 (user-error "No resizable window on the right of this one")
3191 (user-error "No resizable window below this one"))))
3193 ;; LEFT and RIGHT (which might be both internal windows) are now the
3194 ;; two windows we want to resize.
3195 (cond
3196 ((> delta 0)
3197 (setq max-delta
3198 (window--max-delta-1
3199 left 0 horizontal ignore 'after nil pixelwise))
3200 (setq min-delta
3201 (window--min-delta-1
3202 right (- delta) horizontal ignore 'before nil pixelwise))
3203 (when (or (< max-delta delta) (> min-delta (- delta)))
3204 ;; We can't get the whole DELTA - move as far as possible.
3205 (setq delta (min max-delta (- min-delta))))
3206 (unless (zerop delta)
3207 ;; Start resizing.
3208 (window--resize-reset frame horizontal)
3209 ;; Try to enlarge LEFT first.
3210 (setq this-delta (window--resizable
3211 left delta horizontal ignore 'after nil nil pixelwise))
3212 (unless (zerop this-delta)
3213 (window--resize-this-window
3214 left this-delta horizontal ignore t 'before
3215 (if horizontal
3216 (+ (window-pixel-left left) (window-pixel-width left))
3217 (+ (window-pixel-top left) (window-pixel-height left)))))
3218 ;; Shrink windows on right of LEFT.
3219 (window--resize-siblings
3220 left delta horizontal ignore 'after
3221 (if horizontal
3222 (window-pixel-left right)
3223 (window-pixel-top right)))))
3224 ((< delta 0)
3225 (setq max-delta
3226 (window--max-delta-1
3227 right 0 horizontal ignore 'before nil pixelwise))
3228 (setq min-delta
3229 (window--min-delta-1
3230 left delta horizontal ignore 'after nil pixelwise))
3231 (when (or (< max-delta (- delta)) (> min-delta delta))
3232 ;; We can't get the whole DELTA - move as far as possible.
3233 (setq delta (max (- max-delta) min-delta)))
3234 (unless (zerop delta)
3235 ;; Start resizing.
3236 (window--resize-reset frame horizontal)
3237 ;; Try to enlarge RIGHT.
3238 (setq this-delta
3239 (window--resizable
3240 right (- delta) horizontal ignore 'before nil nil pixelwise))
3241 (unless (zerop this-delta)
3242 (window--resize-this-window
3243 right this-delta horizontal ignore t 'after
3244 (if horizontal
3245 (window-pixel-left right)
3246 (window-pixel-top right))))
3247 ;; Shrink windows on left of RIGHT.
3248 (window--resize-siblings
3249 right (- delta) horizontal ignore 'before
3250 (if horizontal
3251 (+ (window-pixel-left left) (window-pixel-width left))
3252 (+ (window-pixel-top left) (window-pixel-height left)))))))
3253 (unless (zerop delta)
3254 ;; Don't report an error in the standard case.
3255 (when (window--resize-apply-p frame horizontal)
3256 (if (window-resize-apply frame horizontal)
3257 (progn
3258 (window--pixel-to-total frame horizontal)
3259 (run-window-configuration-change-hook frame))
3260 ;; But do report an error if applying the changes fails.
3261 (error "Failed adjusting window %s" window))))))))
3263 (defun enlarge-window (delta &optional horizontal)
3264 "Make the selected window DELTA lines taller.
3265 Interactively, if no argument is given, make the selected window
3266 one line taller. If optional argument HORIZONTAL is non-nil,
3267 make selected window wider by DELTA columns. If DELTA is
3268 negative, shrink selected window by -DELTA lines or columns."
3269 (interactive "p")
3270 (let ((minibuffer-window (minibuffer-window)))
3271 (when (window-preserved-size nil horizontal)
3272 (window-preserve-size nil horizontal))
3273 (cond
3274 ((zerop delta))
3275 ((window-size-fixed-p nil horizontal)
3276 (error "Selected window has fixed size"))
3277 ((window-minibuffer-p)
3278 (if horizontal
3279 (error "Cannot resize minibuffer window horizontally")
3280 (window--resize-mini-window (selected-window) delta)))
3281 ((and (not horizontal)
3282 (window-full-height-p)
3283 (eq (window-frame minibuffer-window) (selected-frame))
3284 (not resize-mini-windows))
3285 ;; If the selected window is full height and `resize-mini-windows'
3286 ;; is nil, resize the minibuffer window.
3287 (window--resize-mini-window minibuffer-window (- delta)))
3288 ((window--resizable-p nil delta horizontal)
3289 (window-resize nil delta horizontal))
3291 (window-resize
3292 nil (if (> delta 0)
3293 (window-max-delta nil horizontal)
3294 (- (window-min-delta nil horizontal)))
3295 horizontal)))))
3297 (defun shrink-window (delta &optional horizontal)
3298 "Make the selected window DELTA lines smaller.
3299 Interactively, if no argument is given, make the selected window
3300 one line smaller. If optional argument HORIZONTAL is non-nil,
3301 make selected window narrower by DELTA columns. If DELTA is
3302 negative, enlarge selected window by -DELTA lines or columns.
3303 Also see the `window-min-height' variable."
3304 (interactive "p")
3305 (let ((minibuffer-window (minibuffer-window)))
3306 (when (window-preserved-size nil horizontal)
3307 (window-preserve-size nil horizontal))
3308 (cond
3309 ((zerop delta))
3310 ((window-size-fixed-p nil horizontal)
3311 (error "Selected window has fixed size"))
3312 ((window-minibuffer-p)
3313 (if horizontal
3314 (error "Cannot resize minibuffer window horizontally")
3315 (window--resize-mini-window (selected-window) (- delta))))
3316 ((and (not horizontal)
3317 (window-full-height-p)
3318 (eq (window-frame minibuffer-window) (selected-frame))
3319 (not resize-mini-windows))
3320 ;; If the selected window is full height and `resize-mini-windows'
3321 ;; is nil, resize the minibuffer window.
3322 (window--resize-mini-window minibuffer-window delta))
3323 ((window--resizable-p nil (- delta) horizontal)
3324 (window-resize nil (- delta) horizontal))
3326 (window-resize
3327 nil (if (> delta 0)
3328 (- (window-min-delta nil horizontal))
3329 (window-max-delta nil horizontal))
3330 horizontal)))))
3332 (defun maximize-window (&optional window)
3333 "Maximize WINDOW.
3334 Make WINDOW as large as possible without deleting any windows.
3335 WINDOW must be a valid window and defaults to the selected one.
3337 If the option `window-resize-pixelwise' is non-nil maximize
3338 WINDOW pixelwise."
3339 (interactive)
3340 (setq window (window-normalize-window window))
3341 (window-resize
3342 window (window-max-delta window nil nil nil nil nil window-resize-pixelwise)
3343 nil nil window-resize-pixelwise)
3344 (window-resize
3345 window (window-max-delta window t nil nil nil nil window-resize-pixelwise)
3346 t nil window-resize-pixelwise))
3348 (defun minimize-window (&optional window)
3349 "Minimize WINDOW.
3350 Make WINDOW as small as possible without deleting any windows.
3351 WINDOW must be a valid window and defaults to the selected one.
3353 If the option `window-resize-pixelwise' is non-nil minimize
3354 WINDOW pixelwise."
3355 (interactive)
3356 (setq window (window-normalize-window window))
3357 (window-resize
3358 window
3359 (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise))
3360 nil nil window-resize-pixelwise)
3361 (window-resize
3362 window
3363 (- (window-min-delta window t nil nil nil nil window-resize-pixelwise))
3364 t nil window-resize-pixelwise))
3366 (defun frame-root-window-p (window)
3367 "Return non-nil if WINDOW is the root window of its frame."
3368 (eq window (frame-root-window window)))
3370 (defun window--subtree (window &optional next)
3371 "Return window subtree rooted at WINDOW.
3372 Optional argument NEXT non-nil means include WINDOW's right
3373 siblings in the return value.
3375 See the documentation of `window-tree' for a description of the
3376 return value."
3377 (let (list)
3378 (while window
3379 (setq list
3380 (cons
3381 (cond
3382 ((window-top-child window)
3383 (cons t (cons (window-edges window)
3384 (window--subtree (window-top-child window) t))))
3385 ((window-left-child window)
3386 (cons nil (cons (window-edges window)
3387 (window--subtree (window-left-child window) t))))
3388 (t window))
3389 list))
3390 (setq window (when next (window-next-sibling window))))
3391 (nreverse list)))
3393 (defun window-tree (&optional frame)
3394 "Return the window tree of frame FRAME.
3395 FRAME must be a live frame and defaults to the selected frame.
3396 The return value is a list of the form (ROOT MINI), where ROOT
3397 represents the window tree of the frame's root window, and MINI
3398 is the frame's minibuffer window.
3400 If the root window is not split, ROOT is the root window itself.
3401 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
3402 for a horizontal split, and t for a vertical split. EDGES gives
3403 the combined size and position of the child windows in the split,
3404 and the rest of the elements are the child windows in the split.
3405 Each of the child windows may again be a window or a list
3406 representing a window split, and so on. EDGES is a list (LEFT
3407 TOP RIGHT BOTTOM) as returned by `window-edges'."
3408 (setq frame (window-normalize-frame frame))
3409 (window--subtree (frame-root-window frame) t))
3411 (defun other-window (count &optional all-frames)
3412 "Select another window in cyclic ordering of windows.
3413 COUNT specifies the number of windows to skip, starting with the
3414 selected window, before making the selection. If COUNT is
3415 positive, skip COUNT windows forwards. If COUNT is negative,
3416 skip -COUNT windows backwards. COUNT zero means do not skip any
3417 window, so select the selected window. In an interactive call,
3418 COUNT is the numeric prefix argument. Return nil.
3420 If the `other-window' parameter of the selected window is a
3421 function and `ignore-window-parameters' is nil, call that
3422 function with the arguments COUNT and ALL-FRAMES.
3424 This function does not select a window whose `no-other-window'
3425 window parameter is non-nil.
3427 This function uses `next-window' for finding the window to
3428 select. The argument ALL-FRAMES has the same meaning as in
3429 `next-window', but the MINIBUF argument of `next-window' is
3430 always effectively nil."
3431 (interactive "p")
3432 (let* ((window (selected-window))
3433 (function (and (not ignore-window-parameters)
3434 (window-parameter window 'other-window)))
3435 old-window old-count)
3436 (if (functionp function)
3437 (funcall function count all-frames)
3438 ;; `next-window' and `previous-window' may return a window we are
3439 ;; not allowed to select. Hence we need an exit strategy in case
3440 ;; all windows are non-selectable.
3441 (catch 'exit
3442 (while (> count 0)
3443 (setq window (next-window window nil all-frames))
3444 (cond
3445 ((eq window old-window)
3446 (when (= count old-count)
3447 ;; Keep out of infinite loops. When COUNT has not changed
3448 ;; since we last looked at `window' we're probably in one.
3449 (throw 'exit nil)))
3450 ((window-parameter window 'no-other-window)
3451 (unless old-window
3452 ;; The first non-selectable window `next-window' got us:
3453 ;; Remember it and the current value of COUNT.
3454 (setq old-window window)
3455 (setq old-count count)))
3457 (setq count (1- count)))))
3458 (while (< count 0)
3459 (setq window (previous-window window nil all-frames))
3460 (cond
3461 ((eq window old-window)
3462 (when (= count old-count)
3463 ;; Keep out of infinite loops. When COUNT has not changed
3464 ;; since we last looked at `window' we're probably in one.
3465 (throw 'exit nil)))
3466 ((window-parameter window 'no-other-window)
3467 (unless old-window
3468 ;; The first non-selectable window `previous-window' got
3469 ;; us: Remember it and the current value of COUNT.
3470 (setq old-window window)
3471 (setq old-count count)))
3473 (setq count (1+ count)))))
3475 (select-window window)
3476 ;; Always return nil.
3477 nil))))
3479 ;; This should probably return non-nil when the selected window is part
3480 ;; of an atomic window whose root is the frame's root window.
3481 (defun one-window-p (&optional nomini all-frames)
3482 "Return non-nil if the selected window is the only window.
3483 Optional arg NOMINI non-nil means don't count the minibuffer
3484 even if it is active. Otherwise, the minibuffer is counted
3485 when it is active.
3487 Optional argument ALL-FRAMES specifies the set of frames to
3488 consider, see also `next-window'. ALL-FRAMES nil or omitted
3489 means consider windows on the selected frame only, plus the
3490 minibuffer window if specified by the NOMINI argument. If the
3491 minibuffer counts, consider all windows on all frames that share
3492 that minibuffer too. The remaining non-nil values of ALL-FRAMES
3493 with a special meaning are:
3495 - t means consider all windows on all existing frames.
3497 - `visible' means consider all windows on all visible frames on
3498 the current terminal.
3500 - 0 (the number zero) means consider all windows on all visible
3501 and iconified frames on the current terminal.
3503 - A frame means consider all windows on that frame only.
3505 Anything else means consider all windows on the selected frame
3506 and no others."
3507 (let ((base-window (selected-window)))
3508 (if (and nomini (eq base-window (minibuffer-window)))
3509 (setq base-window (next-window base-window)))
3510 (eq base-window
3511 (next-window base-window (if nomini 'arg) all-frames))))
3513 ;;; Deleting windows.
3514 (defun window-deletable-p (&optional window)
3515 "Return t if WINDOW can be safely deleted from its frame.
3516 WINDOW must be a valid window and defaults to the selected one.
3517 Return 'frame if deleting WINDOW should also delete its frame."
3518 (setq window (window-normalize-window window))
3520 (unless (or ignore-window-parameters
3521 (eq (window-parameter window 'delete-window) t))
3522 ;; Handle atomicity.
3523 (when (window-parameter window 'window-atom)
3524 (setq window (window-atom-root window))))
3526 (let ((frame (window-frame window)))
3527 (cond
3528 ((frame-root-window-p window)
3529 ;; WINDOW's frame can be deleted only if there are other frames
3530 ;; on the same terminal, and it does not contain the active
3531 ;; minibuffer.
3532 (unless (or (eq frame (next-frame frame 0))
3533 ;; We can delete our frame only if no other frame
3534 ;; currently uses our minibuffer window.
3535 (catch 'other
3536 (dolist (other (frame-list))
3537 (when (and (not (eq other frame))
3538 (eq (window-frame (minibuffer-window other))
3539 frame))
3540 (throw 'other t))))
3541 (let ((minibuf (active-minibuffer-window)))
3542 (and minibuf (eq frame (window-frame minibuf)))))
3543 'frame))
3544 ((or ignore-window-parameters
3545 (not (eq window (window--major-non-side-window frame))))
3546 ;; WINDOW can be deleted unless it is the major non-side window of
3547 ;; its frame.
3548 t))))
3550 (defun window--in-subtree-p (window root)
3551 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
3552 (or (eq window root)
3553 (let ((parent (window-parent window)))
3554 (catch 'done
3555 (while parent
3556 (if (eq parent root)
3557 (throw 'done t)
3558 (setq parent (window-parent parent))))))))
3560 (defun delete-window (&optional window)
3561 "Delete WINDOW.
3562 WINDOW must be a valid window and defaults to the selected one.
3563 Return nil.
3565 If the variable `ignore-window-parameters' is non-nil or the
3566 `delete-window' parameter of WINDOW equals t, do not process any
3567 parameters of WINDOW. Otherwise, if the `delete-window'
3568 parameter of WINDOW specifies a function, call that function with
3569 WINDOW as its sole argument and return the value returned by that
3570 function.
3572 Otherwise, if WINDOW is part of an atomic window, call
3573 `delete-window' with the root of the atomic window as its
3574 argument. Signal an error if WINDOW is either the only window on
3575 its frame, the last non-side window, or part of an atomic window
3576 that is its frame's root window."
3577 (interactive)
3578 (setq window (window-normalize-window window))
3579 (let* ((frame (window-frame window))
3580 (function (window-parameter window 'delete-window))
3581 (parent (window-parent window))
3582 atom-root)
3583 (window--check frame)
3584 (catch 'done
3585 ;; Handle window parameters.
3586 (cond
3587 ;; Ignore window parameters if `ignore-window-parameters' tells
3588 ;; us so or `delete-window' equals t.
3589 ((or ignore-window-parameters (eq function t)))
3590 ((functionp function)
3591 ;; The `delete-window' parameter specifies the function to call.
3592 ;; If that function is `ignore' nothing is done. It's up to the
3593 ;; function called here to avoid infinite recursion.
3594 (throw 'done (funcall function window)))
3595 ((and (window-parameter window 'window-atom)
3596 (setq atom-root (window-atom-root window))
3597 (not (eq atom-root window)))
3598 (if (eq atom-root (frame-root-window frame))
3599 (error "Root of atomic window is root window of its frame")
3600 (throw 'done (delete-window atom-root))))
3601 ((not parent)
3602 (error "Attempt to delete minibuffer or sole ordinary window"))
3603 ((eq window (window--major-non-side-window frame))
3604 (error "Attempt to delete last non-side window")))
3606 (let* ((horizontal (window-left-child parent))
3607 (size (window-size window horizontal t))
3608 (frame-selected
3609 (window--in-subtree-p (frame-selected-window frame) window))
3610 ;; Emacs 23 preferably gives WINDOW's space to its left
3611 ;; sibling.
3612 (sibling (or (window-left window) (window-right window))))
3613 (window--resize-reset frame horizontal)
3614 (cond
3615 ((and (not window-combination-resize)
3616 sibling (window-sizable-p sibling size horizontal nil t))
3617 ;; Resize WINDOW's sibling.
3618 (window--resize-this-window sibling size horizontal nil t)
3619 (set-window-new-normal
3620 sibling (+ (window-normal-size sibling horizontal)
3621 (window-normal-size window horizontal))))
3622 ((window--resizable-p window (- size) horizontal nil nil nil t t)
3623 ;; Can do without resizing fixed-size windows.
3624 (window--resize-siblings window (- size) horizontal))
3626 ;; Can't do without resizing fixed-size windows.
3627 (window--resize-siblings window (- size) horizontal t)))
3628 ;; Actually delete WINDOW.
3629 (delete-window-internal window)
3630 (window--pixel-to-total frame horizontal)
3631 (when (and frame-selected
3632 (window-parameter
3633 (frame-selected-window frame) 'no-other-window))
3634 ;; `delete-window-internal' has selected a window that should
3635 ;; not be selected, fix this here.
3636 (other-window -1 frame))
3637 (run-window-configuration-change-hook frame)
3638 (window--check frame)
3639 ;; Always return nil.
3640 nil))))
3642 (defun delete-other-windows (&optional window)
3643 "Make WINDOW fill its frame.
3644 WINDOW must be a valid window and defaults to the selected one.
3645 Return nil.
3647 If the variable `ignore-window-parameters' is non-nil or the
3648 `delete-other-windows' parameter of WINDOW equals t, do not
3649 process any parameters of WINDOW. Otherwise, if the
3650 `delete-other-windows' parameter of WINDOW specifies a function,
3651 call that function with WINDOW as its sole argument and return
3652 the value returned by that function.
3654 Otherwise, if WINDOW is part of an atomic window, call this
3655 function with the root of the atomic window as its argument. If
3656 WINDOW is a non-side window, make WINDOW the only non-side window
3657 on the frame. Side windows are not deleted. If WINDOW is a side
3658 window signal an error."
3659 (interactive)
3660 (setq window (window-normalize-window window))
3661 (let* ((frame (window-frame window))
3662 (function (window-parameter window 'delete-other-windows))
3663 (window-side (window-parameter window 'window-side))
3664 atom-root side-main)
3665 (window--check frame)
3666 (catch 'done
3667 (cond
3668 ;; Ignore window parameters if `ignore-window-parameters' is t or
3669 ;; `delete-other-windows' is t.
3670 ((or ignore-window-parameters (eq function t)))
3671 ((functionp function)
3672 ;; The `delete-other-windows' parameter specifies the function
3673 ;; to call. If the function is `ignore' no windows are deleted.
3674 ;; It's up to the function called to avoid infinite recursion.
3675 (throw 'done (funcall function window)))
3676 ((and (window-parameter window 'window-atom)
3677 (setq atom-root (window-atom-root window))
3678 (not (eq atom-root window)))
3679 (if (eq atom-root (frame-root-window frame))
3680 (error "Root of atomic window is root window of its frame")
3681 (throw 'done (delete-other-windows atom-root))))
3682 ((memq window-side window-sides)
3683 (error "Cannot make side window the only window"))
3684 ((and (window-minibuffer-p window)
3685 (not (eq window (frame-root-window window))))
3686 (error "Can't expand minibuffer to full frame")))
3688 ;; If WINDOW is the major non-side window, do nothing.
3689 (if (window-with-parameter 'window-side)
3690 (setq side-main (window--major-non-side-window frame))
3691 (setq side-main (frame-root-window frame)))
3692 (unless (eq window side-main)
3693 (delete-other-windows-internal window side-main)
3694 (run-window-configuration-change-hook frame)
3695 (window--check frame))
3696 ;; Always return nil.
3697 nil)))
3699 (defun delete-other-windows-vertically (&optional window)
3700 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
3701 This may be a useful alternative binding for \\[delete-other-windows]
3702 if you often split windows horizontally."
3703 (interactive)
3704 (let* ((window (or window (selected-window)))
3705 (edges (window-edges window))
3706 (w window) delenda)
3707 (while (not (eq (setq w (next-window w 1)) window))
3708 (let ((e (window-edges w)))
3709 (when (and (= (car e) (car edges))
3710 (= (nth 2 e) (nth 2 edges)))
3711 (push w delenda))))
3712 (mapc 'delete-window delenda)))
3714 ;;; Windows and buffers.
3716 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
3717 ;; for (1) determining which buffer to show in the window when its
3718 ;; buffer shall be buried or killed and (2) which buffer to show for
3719 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3721 ;; `prev-buffers' consists of <buffer, window-start, window-point>
3722 ;; triples. The entries on this list are ordered by the time their
3723 ;; buffer has been removed from the window, the most recently removed
3724 ;; buffer's entry being first. The window-start and window-point
3725 ;; components are `window-start' and `window-point' at the time the
3726 ;; buffer was removed from the window which implies that the entry must
3727 ;; be added when `set-window-buffer' removes the buffer from the window.
3729 ;; `next-buffers' is the list of buffers that have been replaced
3730 ;; recently by `switch-to-prev-buffer'. These buffers are the least
3731 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
3732 ;; candidates of `switch-to-next-buffer' to switch to. This list is
3733 ;; reset to nil by any action changing the window's buffer with the
3734 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
3735 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
3736 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
3738 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
3739 ;; if such a buffer was killed while the window was hidden within a
3740 ;; window configuration. Such killed buffers get removed whenever
3741 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
3743 ;; The following function is called by `set-window-buffer' _before_ it
3744 ;; replaces the buffer of the argument window with the new buffer.
3745 (defun record-window-buffer (&optional window)
3746 "Record WINDOW's buffer.
3747 WINDOW must be a live window and defaults to the selected one."
3748 (let* ((window (window-normalize-window window t))
3749 (buffer (window-buffer window))
3750 (entry (assq buffer (window-prev-buffers window))))
3751 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
3752 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3753 (set-window-next-buffers window nil)
3755 (when entry
3756 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
3757 (set-window-prev-buffers
3758 window (assq-delete-all buffer (window-prev-buffers window))))
3760 ;; Don't record insignificant buffers.
3761 (unless (eq (aref (buffer-name buffer) 0) ?\s)
3762 ;; Add an entry for buffer to WINDOW's previous buffers.
3763 (with-current-buffer buffer
3764 (let ((start (window-start window))
3765 (point (window-point window)))
3766 (setq entry
3767 (cons buffer
3768 (if entry
3769 ;; We have an entry, update marker positions.
3770 (list (set-marker (nth 1 entry) start)
3771 (set-marker (nth 2 entry) point))
3772 ;; Make new markers.
3773 (list (copy-marker start)
3774 (copy-marker
3775 ;; Preserve window-point-insertion-type
3776 ;; (Bug#12588).
3777 point window-point-insertion-type)))))
3778 (set-window-prev-buffers
3779 window (cons entry (window-prev-buffers window)))))
3781 (run-hooks 'buffer-list-update-hook))))
3783 (defun unrecord-window-buffer (&optional window buffer)
3784 "Unrecord BUFFER in WINDOW.
3785 WINDOW must be a live window and defaults to the selected one.
3786 BUFFER must be a live buffer and defaults to the buffer of
3787 WINDOW."
3788 (let* ((window (window-normalize-window window t))
3789 (buffer (or buffer (window-buffer window))))
3790 (set-window-prev-buffers
3791 window (assq-delete-all buffer (window-prev-buffers window)))
3792 (set-window-next-buffers
3793 window (delq buffer (window-next-buffers window)))))
3795 (defun set-window-buffer-start-and-point (window buffer &optional start point)
3796 "Set WINDOW's buffer to BUFFER.
3797 WINDOW must be a live window and defaults to the selected one.
3798 Optional argument START non-nil means set WINDOW's start position
3799 to START. Optional argument POINT non-nil means set WINDOW's
3800 point to POINT. If WINDOW is selected this also sets BUFFER's
3801 `point' to POINT. If WINDOW is selected and the buffer it showed
3802 before was current this also makes BUFFER the current buffer."
3803 (setq window (window-normalize-window window t))
3804 (let ((selected (eq window (selected-window)))
3805 (current (eq (window-buffer window) (current-buffer))))
3806 (set-window-buffer window buffer)
3807 (when (and selected current)
3808 (set-buffer buffer))
3809 (when start
3810 ;; Don't force window-start here (even if POINT is nil).
3811 (set-window-start window start t))
3812 (when point
3813 (set-window-point window point))))
3815 (defcustom switch-to-visible-buffer t
3816 "If non-nil, allow switching to an already visible buffer.
3817 If this variable is non-nil, `switch-to-prev-buffer' and
3818 `switch-to-next-buffer' may switch to an already visible buffer
3819 provided the buffer was shown before in the window specified as
3820 argument to those functions. If this variable is nil,
3821 `switch-to-prev-buffer' and `switch-to-next-buffer' always try to
3822 avoid switching to a buffer that is already visible in another
3823 window on the same frame."
3824 :type 'boolean
3825 :version "24.1"
3826 :group 'windows)
3828 (defun switch-to-prev-buffer (&optional window bury-or-kill)
3829 "In WINDOW switch to previous buffer.
3830 WINDOW must be a live window and defaults to the selected one.
3831 Return the buffer switched to, nil if no suitable buffer could be
3832 found.
3834 Optional argument BURY-OR-KILL non-nil means the buffer currently
3835 shown in WINDOW is about to be buried or killed and consequently
3836 shall not be switched to in future invocations of this command.
3838 As a special case, if BURY-OR-KILL equals `append', this means to
3839 move the buffer to the end of WINDOW's previous buffers list so a
3840 future invocation of `switch-to-prev-buffer' less likely switches
3841 to it."
3842 (interactive)
3843 (let* ((window (window-normalize-window window t))
3844 (frame (window-frame window))
3845 (old-buffer (window-buffer window))
3846 ;; Save this since it's destroyed by `set-window-buffer'.
3847 (next-buffers (window-next-buffers window))
3848 (pred (frame-parameter frame 'buffer-predicate))
3849 entry new-buffer killed-buffers visible)
3850 (when (window-minibuffer-p window)
3851 ;; Don't switch in minibuffer window.
3852 (unless (setq window (minibuffer-selected-window))
3853 (error "Window %s is a minibuffer window" window)))
3855 (when (window-dedicated-p window)
3856 ;; Don't switch in dedicated window.
3857 (error "Window %s is dedicated to buffer %s" window old-buffer))
3859 (catch 'found
3860 ;; Scan WINDOW's previous buffers first, skipping entries of next
3861 ;; buffers.
3862 (dolist (entry (window-prev-buffers window))
3863 (when (and (setq new-buffer (car entry))
3864 (or (buffer-live-p new-buffer)
3865 (not (setq killed-buffers
3866 (cons new-buffer killed-buffers))))
3867 (not (eq new-buffer old-buffer))
3868 (or (null pred) (funcall pred new-buffer))
3869 ;; When BURY-OR-KILL is nil, avoid switching to a
3870 ;; buffer in WINDOW's next buffers list.
3871 (or bury-or-kill (not (memq new-buffer next-buffers))))
3872 (if (and (not switch-to-visible-buffer)
3873 (get-buffer-window new-buffer frame))
3874 ;; Try to avoid showing a buffer visible in some other
3875 ;; window.
3876 (setq visible new-buffer)
3877 (set-window-buffer-start-and-point
3878 window new-buffer (nth 1 entry) (nth 2 entry))
3879 (throw 'found t))))
3880 ;; Scan reverted buffer list of WINDOW's frame next, skipping
3881 ;; entries of next buffers. Note that when we bury or kill a
3882 ;; buffer we don't reverse the global buffer list to avoid showing
3883 ;; a buried buffer instead. Otherwise, we must reverse the global
3884 ;; buffer list in order to make sure that switching to the
3885 ;; previous/next buffer traverse it in opposite directions.
3886 (dolist (buffer (if bury-or-kill
3887 (buffer-list frame)
3888 (nreverse (buffer-list frame))))
3889 (when (and (buffer-live-p buffer)
3890 (not (eq buffer old-buffer))
3891 (or (null pred) (funcall pred buffer))
3892 (not (eq (aref (buffer-name buffer) 0) ?\s))
3893 (or bury-or-kill (not (memq buffer next-buffers))))
3894 (if (get-buffer-window buffer frame)
3895 ;; Try to avoid showing a buffer visible in some other window.
3896 (unless visible
3897 (setq visible buffer))
3898 (setq new-buffer buffer)
3899 (set-window-buffer-start-and-point window new-buffer)
3900 (throw 'found t))))
3901 (unless bury-or-kill
3902 ;; Scan reverted next buffers last (must not use nreverse
3903 ;; here!).
3904 (dolist (buffer (reverse next-buffers))
3905 ;; Actually, buffer _must_ be live here since otherwise it
3906 ;; would have been caught in the scan of previous buffers.
3907 (when (and (or (buffer-live-p buffer)
3908 (not (setq killed-buffers
3909 (cons buffer killed-buffers))))
3910 (not (eq buffer old-buffer))
3911 (or (null pred) (funcall pred buffer))
3912 (setq entry (assq buffer (window-prev-buffers window))))
3913 (setq new-buffer buffer)
3914 (set-window-buffer-start-and-point
3915 window new-buffer (nth 1 entry) (nth 2 entry))
3916 (throw 'found t))))
3918 ;; Show a buffer visible in another window.
3919 (when visible
3920 (setq new-buffer visible)
3921 (set-window-buffer-start-and-point window new-buffer)))
3923 (if bury-or-kill
3924 (let ((entry (and (eq bury-or-kill 'append)
3925 (assq old-buffer (window-prev-buffers window)))))
3926 ;; Remove `old-buffer' from WINDOW's previous and (restored list
3927 ;; of) next buffers.
3928 (set-window-prev-buffers
3929 window (assq-delete-all old-buffer (window-prev-buffers window)))
3930 (set-window-next-buffers window (delq old-buffer next-buffers))
3931 (when entry
3932 ;; Append old-buffer's entry to list of WINDOW's previous
3933 ;; buffers so it's less likely to get switched to soon but
3934 ;; `display-buffer-in-previous-window' can nevertheless find
3935 ;; it.
3936 (set-window-prev-buffers
3937 window (append (window-prev-buffers window) (list entry)))))
3938 ;; Move `old-buffer' to head of WINDOW's restored list of next
3939 ;; buffers.
3940 (set-window-next-buffers
3941 window (cons old-buffer (delq old-buffer next-buffers))))
3943 ;; Remove killed buffers from WINDOW's previous and next buffers.
3944 (when killed-buffers
3945 (dolist (buffer killed-buffers)
3946 (set-window-prev-buffers
3947 window (assq-delete-all buffer (window-prev-buffers window)))
3948 (set-window-next-buffers
3949 window (delq buffer (window-next-buffers window)))))
3951 ;; Return new-buffer.
3952 new-buffer))
3954 (defun switch-to-next-buffer (&optional window)
3955 "In WINDOW switch to next buffer.
3956 WINDOW must be a live window and defaults to the selected one.
3957 Return the buffer switched to, nil if no suitable buffer could be
3958 found."
3959 (interactive)
3960 (let* ((window (window-normalize-window window t))
3961 (frame (window-frame window))
3962 (old-buffer (window-buffer window))
3963 (next-buffers (window-next-buffers window))
3964 (pred (frame-parameter frame 'buffer-predicate))
3965 new-buffer entry killed-buffers visible)
3966 (when (window-minibuffer-p window)
3967 ;; Don't switch in minibuffer window.
3968 (unless (setq window (minibuffer-selected-window))
3969 (error "Window %s is a minibuffer window" window)))
3971 (when (window-dedicated-p window)
3972 ;; Don't switch in dedicated window.
3973 (error "Window %s is dedicated to buffer %s" window old-buffer))
3975 (catch 'found
3976 ;; Scan WINDOW's next buffers first.
3977 (dolist (buffer next-buffers)
3978 (when (and (or (buffer-live-p buffer)
3979 (not (setq killed-buffers
3980 (cons buffer killed-buffers))))
3981 (not (eq buffer old-buffer))
3982 (or (null pred) (funcall pred buffer))
3983 (setq entry (assq buffer (window-prev-buffers window))))
3984 (setq new-buffer buffer)
3985 (set-window-buffer-start-and-point
3986 window new-buffer (nth 1 entry) (nth 2 entry))
3987 (throw 'found t)))
3988 ;; Scan the buffer list of WINDOW's frame next, skipping previous
3989 ;; buffers entries.
3990 (dolist (buffer (buffer-list frame))
3991 (when (and (buffer-live-p buffer)
3992 (not (eq buffer old-buffer))
3993 (or (null pred) (funcall pred buffer))
3994 (not (eq (aref (buffer-name buffer) 0) ?\s))
3995 (not (assq buffer (window-prev-buffers window))))
3996 (if (get-buffer-window buffer frame)
3997 ;; Try to avoid showing a buffer visible in some other window.
3998 (setq visible buffer)
3999 (setq new-buffer buffer)
4000 (set-window-buffer-start-and-point window new-buffer)
4001 (throw 'found t))))
4002 ;; Scan WINDOW's reverted previous buffers last (must not use
4003 ;; nreverse here!)
4004 (dolist (entry (reverse (window-prev-buffers window)))
4005 (when (and (setq new-buffer (car entry))
4006 (or (buffer-live-p new-buffer)
4007 (not (setq killed-buffers
4008 (cons new-buffer killed-buffers))))
4009 (not (eq new-buffer old-buffer))
4010 (or (null pred) (funcall pred new-buffer)))
4011 (if (and (not switch-to-visible-buffer)
4012 (get-buffer-window new-buffer frame))
4013 ;; Try to avoid showing a buffer visible in some other window.
4014 (unless visible
4015 (setq visible new-buffer))
4016 (set-window-buffer-start-and-point
4017 window new-buffer (nth 1 entry) (nth 2 entry))
4018 (throw 'found t))))
4020 ;; Show a buffer visible in another window.
4021 (when visible
4022 (setq new-buffer visible)
4023 (set-window-buffer-start-and-point window new-buffer)))
4025 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
4026 (set-window-next-buffers window (delq new-buffer next-buffers))
4028 ;; Remove killed buffers from WINDOW's previous and next buffers.
4029 (when killed-buffers
4030 (dolist (buffer killed-buffers)
4031 (set-window-prev-buffers
4032 window (assq-delete-all buffer (window-prev-buffers window)))
4033 (set-window-next-buffers
4034 window (delq buffer (window-next-buffers window)))))
4036 ;; Return new-buffer.
4037 new-buffer))
4039 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
4040 "Search LIST for a valid buffer to display in FRAME.
4041 Return nil when all buffers in LIST are undesirable for display,
4042 otherwise return the first suitable buffer in LIST.
4044 Buffers not visible in windows are preferred to visible buffers,
4045 unless VISIBLE-OK is non-nil.
4046 If the optional argument FRAME is nil, it defaults to the selected frame.
4047 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
4048 ;; This logic is more or less copied from other-buffer.
4049 (setq frame (or frame (selected-frame)))
4050 (let ((pred (frame-parameter frame 'buffer-predicate))
4051 found buf)
4052 (while (and (not found) list)
4053 (setq buf (car list))
4054 (if (and (not (eq buffer buf))
4055 (buffer-live-p buf)
4056 (or (null pred) (funcall pred buf))
4057 (not (eq (aref (buffer-name buf) 0) ?\s))
4058 (or visible-ok (null (get-buffer-window buf 'visible))))
4059 (setq found buf)
4060 (setq list (cdr list))))
4061 (car list)))
4063 (defun last-buffer (&optional buffer visible-ok frame)
4064 "Return the last buffer in FRAME's buffer list.
4065 If BUFFER is the last buffer, return the preceding buffer
4066 instead. Buffers not visible in windows are preferred to visible
4067 buffers, unless optional argument VISIBLE-OK is non-nil.
4068 Optional third argument FRAME nil or omitted means use the
4069 selected frame's buffer list. If no such buffer exists, return
4070 the buffer `*scratch*', creating it if necessary."
4071 (setq frame (or frame (selected-frame)))
4072 (or (get-next-valid-buffer (nreverse (buffer-list frame))
4073 buffer visible-ok frame)
4074 (get-buffer "*scratch*")
4075 (let ((scratch (get-buffer-create "*scratch*")))
4076 (set-buffer-major-mode scratch)
4077 scratch)))
4079 (defcustom frame-auto-hide-function #'iconify-frame
4080 "Function called to automatically hide frames.
4081 The function is called with one argument - a frame.
4083 Functions affected by this option are those that bury a buffer
4084 shown in a separate frame like `quit-window' and `bury-buffer'."
4085 :type '(choice (const :tag "Iconify" iconify-frame)
4086 (const :tag "Delete" delete-frame)
4087 (const :tag "Do nothing" ignore)
4088 function)
4089 :group 'windows
4090 :group 'frames
4091 :version "24.1")
4093 (defun window--delete (&optional window dedicated-only kill)
4094 "Delete WINDOW if possible.
4095 WINDOW must be a live window and defaults to the selected one.
4096 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
4097 only if it's dedicated to its buffer. Optional argument KILL
4098 means the buffer shown in window will be killed. Return non-nil
4099 if WINDOW gets deleted or its frame is auto-hidden."
4100 (setq window (window-normalize-window window t))
4101 (unless (and dedicated-only (not (window-dedicated-p window)))
4102 (let ((deletable (window-deletable-p window)))
4103 (cond
4104 ((eq deletable 'frame)
4105 (let ((frame (window-frame window)))
4106 (cond
4107 (kill
4108 (delete-frame frame))
4109 ((functionp frame-auto-hide-function)
4110 (funcall frame-auto-hide-function frame))))
4111 'frame)
4112 (deletable
4113 (delete-window window)
4114 t)))))
4116 (defun bury-buffer (&optional buffer-or-name)
4117 "Put BUFFER-OR-NAME at the end of the list of all buffers.
4118 There it is the least likely candidate for `other-buffer' to
4119 return; thus, the least likely buffer for \\[switch-to-buffer] to
4120 select by default.
4122 You can specify a buffer name as BUFFER-OR-NAME, or an actual
4123 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
4124 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
4125 remove the current buffer from the selected window if it is
4126 displayed there."
4127 (interactive)
4128 (let* ((buffer (window-normalize-buffer buffer-or-name)))
4129 ;; If `buffer-or-name' is not on the selected frame we unrecord it
4130 ;; although it's not "here" (call it a feature).
4131 (bury-buffer-internal buffer)
4132 ;; Handle case where `buffer-or-name' is nil and the current buffer
4133 ;; is shown in the selected window.
4134 (cond
4135 ((or buffer-or-name (not (eq buffer (window-buffer)))))
4136 ((window--delete nil t))
4138 ;; Switch to another buffer in window.
4139 (set-window-dedicated-p nil nil)
4140 (switch-to-prev-buffer nil 'bury)))
4142 ;; Always return nil.
4143 nil))
4145 (defun unbury-buffer ()
4146 "Switch to the last buffer in the buffer list."
4147 (interactive)
4148 (switch-to-buffer (last-buffer)))
4150 (defun next-buffer ()
4151 "In selected window switch to next buffer."
4152 (interactive)
4153 (cond
4154 ((window-minibuffer-p)
4155 (error "Cannot switch buffers in minibuffer window"))
4156 ((eq (window-dedicated-p) t)
4157 (error "Window is strongly dedicated to its buffer"))
4159 (switch-to-next-buffer))))
4161 (defun previous-buffer ()
4162 "In selected window switch to previous buffer."
4163 (interactive)
4164 (cond
4165 ((window-minibuffer-p)
4166 (error "Cannot switch buffers in minibuffer window"))
4167 ((eq (window-dedicated-p) t)
4168 (error "Window is strongly dedicated to its buffer"))
4170 (switch-to-prev-buffer))))
4172 (defun delete-windows-on (&optional buffer-or-name frame)
4173 "Delete all windows showing BUFFER-OR-NAME.
4174 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4175 and defaults to the current buffer.
4177 The following non-nil values of the optional argument FRAME
4178 have special meanings:
4180 - t means consider all windows on the selected frame only.
4182 - `visible' means consider all windows on all visible frames on
4183 the current terminal.
4185 - 0 (the number zero) means consider all windows on all visible
4186 and iconified frames on the current terminal.
4188 - A frame means consider all windows on that frame only.
4190 Any other value of FRAME means consider all windows on all
4191 frames.
4193 When a window showing BUFFER-OR-NAME is dedicated and the only
4194 window of its frame, that frame is deleted when there are other
4195 frames left."
4196 (interactive "BDelete windows on (buffer):\nP")
4197 (let ((buffer (window-normalize-buffer buffer-or-name))
4198 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4199 ;; `window-list-1' based function.
4200 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4201 (dolist (window (window-list-1 nil nil all-frames))
4202 (if (eq (window-buffer window) buffer)
4203 (let ((deletable (window-deletable-p window)))
4204 (cond
4205 ((and (eq deletable 'frame) (window-dedicated-p window))
4206 ;; Delete frame if and only if window is dedicated.
4207 (delete-frame (window-frame window)))
4208 ((eq deletable t)
4209 ;; Delete window.
4210 (delete-window window))
4212 ;; In window switch to previous buffer.
4213 (set-window-dedicated-p window nil)
4214 (switch-to-prev-buffer window 'bury))))
4215 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4216 (unrecord-window-buffer window buffer)))))
4218 (defun replace-buffer-in-windows (&optional buffer-or-name)
4219 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
4220 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4221 and defaults to the current buffer.
4223 When a window showing BUFFER-OR-NAME is dedicated, that window is
4224 deleted. If that window is the only window on its frame, the
4225 frame is deleted too when there are other frames left. If there
4226 are no other frames left, some other buffer is displayed in that
4227 window.
4229 This function removes the buffer denoted by BUFFER-OR-NAME from
4230 all window-local buffer lists."
4231 (interactive "bBuffer to replace: ")
4232 (let ((buffer (window-normalize-buffer buffer-or-name)))
4233 (dolist (window (window-list-1 nil nil t))
4234 (if (eq (window-buffer window) buffer)
4235 (unless (window--delete window t t)
4236 ;; Switch to another buffer in window.
4237 (set-window-dedicated-p window nil)
4238 (switch-to-prev-buffer window 'kill))
4239 ;; Unrecord BUFFER in WINDOW.
4240 (unrecord-window-buffer window buffer)))))
4242 (defun quit-restore-window (&optional window bury-or-kill)
4243 "Quit WINDOW and deal with its buffer.
4244 WINDOW must be a live window and defaults to the selected one.
4246 According to information stored in WINDOW's `quit-restore' window
4247 parameter either (1) delete WINDOW and its frame, (2) delete
4248 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4249 or (4) make WINDOW display some other buffer than the present
4250 one. If non-nil, reset `quit-restore' parameter to nil.
4252 Optional second argument BURY-OR-KILL tells how to proceed with
4253 the buffer of WINDOW. The following values are handled:
4255 `nil' means to not handle the buffer in a particular way. This
4256 means that if WINDOW is not deleted by this function, invoking
4257 `switch-to-prev-buffer' will usually show the buffer again.
4259 `append' means that if WINDOW is not deleted, move its buffer to
4260 the end of WINDOW's previous buffers so it's less likely that a
4261 future invocation of `switch-to-prev-buffer' will switch to it.
4262 Also, move the buffer to the end of the frame's buffer list.
4264 `bury' means that if WINDOW is not deleted, remove its buffer
4265 from WINDOW'S list of previous buffers. Also, move the buffer
4266 to the end of the frame's buffer list. This value provides the
4267 most reliable remedy to not have `switch-to-prev-buffer' switch
4268 to this buffer again without killing the buffer.
4270 `kill' means to kill WINDOW's buffer."
4271 (setq window (window-normalize-window window t))
4272 (let* ((buffer (window-buffer window))
4273 (quit-restore (window-parameter window 'quit-restore))
4274 (prev-buffer
4275 (let* ((prev-buffers (window-prev-buffers window))
4276 (prev-buffer (caar prev-buffers)))
4277 (and (or (not (eq prev-buffer buffer))
4278 (and (cdr prev-buffers)
4279 (not (eq (setq prev-buffer (cadr prev-buffers))
4280 buffer))))
4281 prev-buffer)))
4282 quad entry)
4283 (cond
4284 ((and (not prev-buffer)
4285 (or (eq (nth 1 quit-restore) 'frame)
4286 (and (eq (nth 1 quit-restore) 'window)
4287 ;; If the window has been created on an existing
4288 ;; frame and ended up as the sole window on that
4289 ;; frame, do not delete it (Bug#12764).
4290 (not (eq window (frame-root-window window)))))
4291 (eq (nth 3 quit-restore) buffer)
4292 ;; Delete WINDOW if possible.
4293 (window--delete window nil (eq bury-or-kill 'kill)))
4294 ;; If the previously selected window is still alive, select it.
4295 (when (window-live-p (nth 2 quit-restore))
4296 (select-window (nth 2 quit-restore))))
4297 ((and (listp (setq quad (nth 1 quit-restore)))
4298 (buffer-live-p (car quad))
4299 (eq (nth 3 quit-restore) buffer))
4300 ;; Show another buffer stored in quit-restore parameter.
4301 (when (and (integerp (nth 3 quad))
4302 (/= (nth 3 quad) (window-total-height window)))
4303 ;; Try to resize WINDOW to its old height but don't signal an
4304 ;; error.
4305 (condition-case nil
4306 (window-resize window (- (nth 3 quad) (window-total-height window)))
4307 (error nil)))
4308 (set-window-dedicated-p window nil)
4309 ;; Restore WINDOW's previous buffer, start and point position.
4310 (set-window-buffer-start-and-point
4311 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
4312 ;; Deal with the buffer we just removed from WINDOW.
4313 (setq entry (and (eq bury-or-kill 'append)
4314 (assq buffer (window-prev-buffers window))))
4315 (when bury-or-kill
4316 ;; Remove buffer from WINDOW's previous and next buffers.
4317 (set-window-prev-buffers
4318 window (assq-delete-all buffer (window-prev-buffers window)))
4319 (set-window-next-buffers
4320 window (delq buffer (window-next-buffers window))))
4321 (when entry
4322 ;; Append old buffer's entry to list of WINDOW's previous
4323 ;; buffers so it's less likely to get switched to soon but
4324 ;; `display-buffer-in-previous-window' can nevertheless find it.
4325 (set-window-prev-buffers
4326 window (append (window-prev-buffers window) (list entry))))
4327 ;; Reset the quit-restore parameter.
4328 (set-window-parameter window 'quit-restore nil)
4329 ;; Select old window.
4330 (when (window-live-p (nth 2 quit-restore))
4331 (select-window (nth 2 quit-restore))))
4333 ;; Show some other buffer in WINDOW and reset the quit-restore
4334 ;; parameter.
4335 (set-window-parameter window 'quit-restore nil)
4336 ;; Make sure that WINDOW is no more dedicated.
4337 (set-window-dedicated-p window nil)
4338 (switch-to-prev-buffer window bury-or-kill)))
4340 ;; Deal with the buffer.
4341 (cond
4342 ((not (buffer-live-p buffer)))
4343 ((eq bury-or-kill 'kill)
4344 (kill-buffer buffer))
4345 (bury-or-kill
4346 (bury-buffer-internal buffer)))))
4348 (defun quit-window (&optional kill window)
4349 "Quit WINDOW and bury its buffer.
4350 WINDOW must be a live window and defaults to the selected one.
4351 With prefix argument KILL non-nil, kill the buffer instead of
4352 burying it.
4354 According to information stored in WINDOW's `quit-restore' window
4355 parameter either (1) delete WINDOW and its frame, (2) delete
4356 WINDOW, (3) restore the buffer previously displayed in WINDOW,
4357 or (4) make WINDOW display some other buffer than the present
4358 one. If non-nil, reset `quit-restore' parameter to nil."
4359 (interactive "P")
4360 (quit-restore-window window (if kill 'kill 'bury)))
4362 (defun quit-windows-on (&optional buffer-or-name kill frame)
4363 "Quit all windows showing BUFFER-OR-NAME.
4364 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
4365 and defaults to the current buffer. Optional argument KILL
4366 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
4367 BUFFER-OR-NAME. Optional argument FRAME is handled as by
4368 `delete-windows-on'.
4370 This function calls `quit-window' on all candidate windows
4371 showing BUFFER-OR-NAME."
4372 (interactive "BQuit windows on (buffer):\nP")
4373 (let ((buffer (window-normalize-buffer buffer-or-name))
4374 ;; Handle the "inverted" meaning of the FRAME argument wrt other
4375 ;; `window-list-1' based function.
4376 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
4377 (dolist (window (window-list-1 nil nil all-frames))
4378 (if (eq (window-buffer window) buffer)
4379 (quit-window kill window)
4380 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
4381 (unrecord-window-buffer window buffer)))))
4383 (defun split-window (&optional window size side pixelwise)
4384 "Make a new window adjacent to WINDOW.
4385 WINDOW must be a valid window and defaults to the selected one.
4386 Return the new window which is always a live window.
4388 Optional argument SIZE a positive number means make WINDOW SIZE
4389 lines or columns tall. If SIZE is negative, make the new window
4390 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
4391 absolute value can be less than `window-min-height' or
4392 `window-min-width'; so this command can make a new window as
4393 small as one line or two columns. SIZE defaults to half of
4394 WINDOW's size.
4396 Optional third argument SIDE nil (or `below') specifies that the
4397 new window shall be located below WINDOW. SIDE `above' means the
4398 new window shall be located above WINDOW. In both cases SIZE
4399 specifies the new number of lines for WINDOW (or the new window
4400 if SIZE is negative) including space reserved for the mode and/or
4401 header line.
4403 SIDE t (or `right') specifies that the new window shall be
4404 located on the right side of WINDOW. SIDE `left' means the new
4405 window shall be located on the left of WINDOW. In both cases
4406 SIZE specifies the new number of columns for WINDOW (or the new
4407 window provided SIZE is negative) including space reserved for
4408 fringes and the scrollbar or a divider column. Any other non-nil
4409 value for SIDE is currently handled like t (or `right').
4411 PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
4413 If the variable `ignore-window-parameters' is non-nil or the
4414 `split-window' parameter of WINDOW equals t, do not process any
4415 parameters of WINDOW. Otherwise, if the `split-window' parameter
4416 of WINDOW specifies a function, call that function with all three
4417 arguments and return the value returned by that function.
4419 Otherwise, if WINDOW is part of an atomic window, \"split\" the
4420 root of that atomic window. The new window does not become a
4421 member of that atomic window.
4423 If WINDOW is live, properties of the new window like margins and
4424 scrollbars are inherited from WINDOW. If WINDOW is an internal
4425 window, these properties as well as the buffer displayed in the
4426 new window are inherited from the window selected on WINDOW's
4427 frame. The selected window is not changed by this function."
4428 (setq window (window-normalize-window window))
4429 (let* ((side (cond
4430 ((not side) 'below)
4431 ((memq side '(below above right left)) side)
4432 (t 'right)))
4433 (horizontal (not (memq side '(below above))))
4434 (frame (window-frame window))
4435 (parent (window-parent window))
4436 (function (window-parameter window 'split-window))
4437 (window-side (window-parameter window 'window-side))
4438 ;; Rebind the following two variables since in some cases we
4439 ;; have to override their value.
4440 (window-combination-limit window-combination-limit)
4441 (window-combination-resize window-combination-resize)
4442 (char-size (frame-char-size window horizontal))
4443 (pixel-size
4444 (when (numberp size)
4445 (window--size-to-pixel window size horizontal pixelwise t)))
4446 (divider-width (if horizontal
4447 (frame-right-divider-width frame)
4448 (frame-bottom-divider-width frame)))
4449 atom-root ignore)
4450 (window--check frame)
4451 (catch 'done
4452 (cond
4453 ;; Ignore window parameters if either `ignore-window-parameters'
4454 ;; is t or the `split-window' parameter equals t.
4455 ((or ignore-window-parameters (eq function t)))
4456 ((functionp function)
4457 ;; The `split-window' parameter specifies the function to call.
4458 ;; If that function is `ignore', do nothing.
4459 (throw 'done (funcall function window size side)))
4460 ;; If WINDOW is part of an atomic window, split the root window
4461 ;; of that atomic window instead.
4462 ((and (window-parameter window 'window-atom)
4463 (setq atom-root (window-atom-root window))
4464 (not (eq atom-root window)))
4465 (throw 'done (split-window atom-root size side pixelwise)))
4466 ;; If WINDOW is a side window or its first or last child is a
4467 ;; side window, throw an error unless `window-combination-resize'
4468 ;; equals 'side.
4469 ((and (not (eq window-combination-resize 'side))
4470 (window--side-window-p window))
4471 (error "Cannot split side window or parent of side window"))
4472 ;; If `window-combination-resize' is 'side and window has a side
4473 ;; window sibling, bind `window-combination-limit' to t.
4474 ((and (not (eq window-combination-resize 'side))
4475 (or (and (window-prev-sibling window)
4476 (window-parameter
4477 (window-prev-sibling window) 'window-side))
4478 (and (window-next-sibling window)
4479 (window-parameter
4480 (window-next-sibling window) 'window-side))))
4481 (setq window-combination-limit t)))
4483 ;; If `window-combination-resize' is t and SIZE is non-negative,
4484 ;; bind `window-combination-limit' to t.
4485 (when (and (eq window-combination-resize t)
4486 pixel-size (> pixel-size 0))
4487 (setq window-combination-limit t))
4489 (let* ((parent-pixel-size
4490 ;; `parent-pixel-size' is the pixel size of WINDOW's
4491 ;; parent, provided it has one.
4492 (when parent (window-size parent horizontal t)))
4493 ;; `resize' non-nil means we are supposed to resize other
4494 ;; windows in WINDOW's combination.
4495 (resize
4496 (and window-combination-resize
4497 (or (window-parameter window 'window-side)
4498 (not (eq window-combination-resize 'side)))
4499 (not (eq window-combination-limit t))
4500 ;; Resize makes sense in iso-combinations only.
4501 (window-combined-p window horizontal)))
4502 ;; `old-pixel-size' is the current pixel size of WINDOW.
4503 (old-pixel-size (window-size window horizontal t))
4504 ;; `new-size' is the specified or calculated size of the
4505 ;; new window.
4506 new-pixel-size new-parent new-normal)
4507 (cond
4508 ((not pixel-size)
4509 (setq new-pixel-size
4510 (if resize
4511 ;; When resizing try to give the new window the
4512 ;; average size of a window in its combination.
4513 (max (min (- parent-pixel-size
4514 (window-min-size parent horizontal nil t))
4515 (/ parent-pixel-size
4516 (1+ (window-combinations parent horizontal))))
4517 (window-min-pixel-size))
4518 ;; Else try to give the new window half the size
4519 ;; of WINDOW (plus an eventual odd pixel).
4520 (/ old-pixel-size 2)))
4521 (unless window-resize-pixelwise
4522 ;; Round to nearest char-size multiple.
4523 (setq new-pixel-size
4524 (* char-size (round new-pixel-size char-size)))))
4525 ((>= pixel-size 0)
4526 ;; SIZE non-negative specifies the new size of WINDOW.
4528 ;; Note: Specifying a non-negative SIZE is practically
4529 ;; always done as workaround for making the new window
4530 ;; appear above or on the left of the new window (the
4531 ;; ispell window is a typical example of that). In all
4532 ;; these cases the SIDE argument should be set to 'above
4533 ;; or 'left in order to support the 'resize option.
4534 ;; Here we have to nest the windows instead, see above.
4535 (setq new-pixel-size (- old-pixel-size pixel-size)))
4537 ;; SIZE negative specifies the size of the new window.
4538 (setq new-pixel-size (- pixel-size))))
4540 ;; Check SIZE.
4541 (cond
4542 ((not pixel-size)
4543 (cond
4544 (resize
4545 ;; SIZE unspecified, resizing.
4546 (unless (or (window-sizable-p
4547 parent (- (+ new-pixel-size divider-width)) horizontal
4548 nil t)
4549 (window-sizable-p
4550 parent (- (+ new-pixel-size divider-width)) horizontal
4551 (setq ignore 'preserved) t))
4552 (error "Window %s too small for splitting (1)" parent)))
4553 ((and (> (+ new-pixel-size divider-width
4554 (window-min-size window horizontal nil t))
4555 old-pixel-size)
4556 (> (+ new-pixel-size divider-width
4557 (window-min-size
4558 window horizontal (setq ignore 'preserved) t))
4559 old-pixel-size))
4560 ;; SIZE unspecified, no resizing.
4561 (error "Window %s too small for splitting (2)" window))))
4562 ((and (>= pixel-size 0)
4563 (or (>= pixel-size old-pixel-size)
4564 (< new-pixel-size
4565 (window-safe-min-pixel-size window horizontal))))
4566 ;; SIZE specified as new size of old window. If the new size
4567 ;; is larger than the old size or the size of the new window
4568 ;; would be less than the safe minimum, signal an error.
4569 (error "Window %s too small for splitting (3)" window))
4570 (resize
4571 ;; SIZE specified, resizing.
4572 (unless (or (window-sizable-p
4573 parent (- (+ new-pixel-size divider-width)) horizontal
4574 nil t)
4575 (window-sizable-p
4576 parent (- (+ new-pixel-size divider-width)) horizontal
4577 (setq ignore 'preserved) t))
4578 ;; If we cannot resize the parent give up.
4579 (error "Window %s too small for splitting (4)" parent)))
4580 ((or (< new-pixel-size
4581 (window-safe-min-pixel-size window horizontal))
4582 (< (- old-pixel-size new-pixel-size)
4583 (window-safe-min-pixel-size window horizontal)))
4584 ;; SIZE specification violates minimum size restrictions.
4585 (error "Window %s too small for splitting (5)" window)))
4587 (window--resize-reset frame horizontal)
4589 (setq new-parent
4590 ;; Make new-parent non-nil if we need a new parent window;
4591 ;; either because we want to nest or because WINDOW is not
4592 ;; iso-combined.
4593 (or (eq window-combination-limit t)
4594 (not (window-combined-p window horizontal))))
4595 (setq new-normal
4596 ;; Make new-normal the normal size of the new window.
4597 (cond
4598 (pixel-size (/ (float new-pixel-size)
4599 (if new-parent old-pixel-size parent-pixel-size)))
4600 (new-parent 0.5)
4601 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
4602 (t (/ (window-normal-size window horizontal) 2.0))))
4604 (if resize
4605 ;; Try to get space from OLD's siblings. We could go "up" and
4606 ;; try getting additional space from surrounding windows but
4607 ;; we won't be able to return space to those windows when we
4608 ;; delete the one we create here. Hence we do not go up.
4609 (progn
4610 (window--resize-child-windows
4611 parent (- new-pixel-size) horizontal nil ignore)
4612 (let* ((normal (- 1.0 new-normal))
4613 (sub (window-child parent)))
4614 (while sub
4615 (set-window-new-normal
4616 sub (* (window-normal-size sub horizontal) normal))
4617 (setq sub (window-right sub)))))
4618 ;; Get entire space from WINDOW.
4619 (set-window-new-pixel
4620 window (- old-pixel-size new-pixel-size))
4621 (window--resize-this-window
4622 window (- new-pixel-size) horizontal ignore)
4623 (set-window-new-normal
4624 window (- (if new-parent 1.0 (window-normal-size window horizontal))
4625 new-normal)))
4627 (let* ((new (split-window-internal window new-pixel-size side new-normal)))
4628 (window--pixel-to-total frame horizontal)
4629 ;; Assign window-side parameters, if any.
4630 (cond
4631 ((eq window-combination-resize 'side)
4632 (let ((window-side
4633 (cond
4634 (window-side window-side)
4635 ((eq side 'above) 'top)
4636 ((eq side 'below) 'bottom)
4637 (t side))))
4638 ;; We made a new side window.
4639 (set-window-parameter new 'window-side window-side)
4640 (when (and new-parent (window-parameter window 'window-side))
4641 ;; We've been splitting a side root window. Give the
4642 ;; new parent the same window-side parameter.
4643 (set-window-parameter
4644 (window-parent new) 'window-side window-side))))
4645 ((eq window-combination-resize 'atom)
4646 ;; Make sure `window--check-frame' won't destroy an existing
4647 ;; atomic window in case the new window gets nested inside.
4648 (unless (window-parameter window 'window-atom)
4649 (set-window-parameter window 'window-atom t))
4650 (when new-parent
4651 (set-window-parameter (window-parent new) 'window-atom t))
4652 (set-window-parameter new 'window-atom t)))
4654 ;; Sanitize sizes.
4655 (window--sanitize-window-sizes frame horizontal)
4657 (run-window-configuration-change-hook frame)
4658 (run-window-scroll-functions new)
4659 (window--check frame)
4660 ;; Always return the new window.
4661 new)))))
4663 ;; I think this should be the default; I think people will prefer it--rms.
4664 (defcustom split-window-keep-point t
4665 "If non-nil, \\[split-window-below] preserves point in the new window.
4666 If nil, adjust point in the two windows to minimize redisplay.
4667 This option applies only to `split-window-below' and functions
4668 that call it. The low-level `split-window' function always keeps
4669 the original point in both windows."
4670 :type 'boolean
4671 :group 'windows)
4673 (defun split-window-below (&optional size)
4674 "Split the selected window into two windows, one above the other.
4675 The selected window is above. The newly split-off window is
4676 below and displays the same buffer. Return the new window.
4678 If optional argument SIZE is omitted or nil, both windows get the
4679 same height, or close to it. If SIZE is positive, the upper
4680 \(selected) window gets SIZE lines. If SIZE is negative, the
4681 lower (new) window gets -SIZE lines.
4683 If the variable `split-window-keep-point' is non-nil, both
4684 windows get the same value of point as the selected window.
4685 Otherwise, the window starts are chosen so as to minimize the
4686 amount of redisplay; this is convenient on slow terminals."
4687 (interactive "P")
4688 (let ((old-window (selected-window))
4689 (old-point (window-point))
4690 (size (and size (prefix-numeric-value size)))
4691 moved-by-window-height moved new-window bottom)
4692 (when (and size (< size 0) (< (- size) window-min-height))
4693 ;; `split-window' would not signal an error here.
4694 (error "Size of new window too small"))
4695 (setq new-window (split-window nil size))
4696 (unless split-window-keep-point
4697 (with-current-buffer (window-buffer)
4698 ;; Use `save-excursion' around vertical movements below
4699 ;; (Bug#10971). Note: When the selected window's buffer has a
4700 ;; header line, up to two lines of the buffer may not show up
4701 ;; in the resulting configuration.
4702 (save-excursion
4703 (goto-char (window-start))
4704 (setq moved (vertical-motion (window-height)))
4705 (set-window-start new-window (point))
4706 (when (> (point) (window-point new-window))
4707 (set-window-point new-window (point)))
4708 (when (= moved (window-height))
4709 (setq moved-by-window-height t)
4710 (vertical-motion -1))
4711 (setq bottom (point)))
4712 (and moved-by-window-height
4713 (<= bottom (point))
4714 (set-window-point old-window (1- bottom)))
4715 (and moved-by-window-height
4716 (<= (window-start new-window) old-point)
4717 (set-window-point new-window old-point)
4718 (select-window new-window))))
4719 ;; Always copy quit-restore parameter in interactive use.
4720 (let ((quit-restore (window-parameter old-window 'quit-restore)))
4721 (when quit-restore
4722 (set-window-parameter new-window 'quit-restore quit-restore)))
4723 new-window))
4725 (defalias 'split-window-vertically 'split-window-below)
4727 (defun split-window-right (&optional size)
4728 "Split the selected window into two side-by-side windows.
4729 The selected window is on the left. The newly split-off window
4730 is on the right and displays the same buffer. Return the new
4731 window.
4733 If optional argument SIZE is omitted or nil, both windows get the
4734 same width, or close to it. If SIZE is positive, the left-hand
4735 \(selected) window gets SIZE columns. If SIZE is negative, the
4736 right-hand (new) window gets -SIZE columns. Here, SIZE includes
4737 the width of the window's scroll bar; if there are no scroll
4738 bars, it includes the width of the divider column to the window's
4739 right, if any."
4740 (interactive "P")
4741 (let ((old-window (selected-window))
4742 (size (and size (prefix-numeric-value size)))
4743 new-window)
4744 (when (and size (< size 0) (< (- size) window-min-width))
4745 ;; `split-window' would not signal an error here.
4746 (error "Size of new window too small"))
4747 (setq new-window (split-window nil size t))
4748 ;; Always copy quit-restore parameter in interactive use.
4749 (let ((quit-restore (window-parameter old-window 'quit-restore)))
4750 (when quit-restore
4751 (set-window-parameter new-window 'quit-restore quit-restore)))
4752 new-window))
4754 (defalias 'split-window-horizontally 'split-window-right)
4756 ;;; Balancing windows.
4758 ;; The following routine uses the recycled code from an old version of
4759 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
4760 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
4761 ;; more readable (FWIW we'd need three loops - one to calculate the
4762 ;; minimum sizes per window, one to enlarge or shrink windows until the
4763 ;; new parent-size matches, and one where we shrink the largest/enlarge
4764 ;; the smallest window).
4765 (defun balance-windows-2 (window horizontal)
4766 "Subroutine of `balance-windows-1'.
4767 WINDOW must be a vertical combination (horizontal if HORIZONTAL
4768 is non-nil)."
4769 (let* ((char-size (if window-resize-pixelwise
4771 (frame-char-size window horizontal)))
4772 (first (window-child window))
4773 (sub first)
4774 (number-of-children 0)
4775 (parent-size (window-new-pixel window))
4776 (total-sum parent-size)
4777 failed size sub-total sub-delta sub-amount rest)
4778 (while sub
4779 (setq number-of-children (1+ number-of-children))
4780 (when (window-size-fixed-p sub horizontal)
4781 (setq total-sum
4782 (- total-sum (window-size sub horizontal t)))
4783 (set-window-new-normal sub 'ignore))
4784 (setq sub (window-right sub)))
4786 (setq failed t)
4787 (while (and failed (> number-of-children 0))
4788 (setq size (/ total-sum number-of-children))
4789 (setq failed nil)
4790 (setq sub first)
4791 (while (and sub (not failed))
4792 ;; Ignore child windows that should be ignored or are stuck.
4793 (unless (window--resize-child-windows-skip-p sub)
4794 (setq sub-total (window-size sub horizontal t))
4795 (setq sub-delta (- size sub-total))
4796 (setq sub-amount
4797 (window-sizable sub sub-delta horizontal nil t))
4798 ;; Register the new total size for this child window.
4799 (set-window-new-pixel sub (+ sub-total sub-amount))
4800 (unless (= sub-amount sub-delta)
4801 (setq total-sum (- total-sum sub-total sub-amount))
4802 (setq number-of-children (1- number-of-children))
4803 ;; We failed and need a new round.
4804 (setq failed t)
4805 (set-window-new-normal sub 'skip)))
4806 (setq sub (window-right sub))))
4808 ;; How can we be sure that `number-of-children' is NOT zero here ?
4809 (setq rest (% total-sum number-of-children))
4810 ;; Fix rounding by trying to enlarge non-stuck windows by one line
4811 ;; (column) until `rest' is zero.
4812 (setq sub first)
4813 (while (and sub (> rest 0))
4814 (unless (window--resize-child-windows-skip-p window)
4815 (set-window-new-pixel sub (min rest char-size) t)
4816 (setq rest (- rest char-size)))
4817 (setq sub (window-right sub)))
4819 ;; Fix rounding by trying to enlarge stuck windows by one line
4820 ;; (column) until `rest' equals zero.
4821 (setq sub first)
4822 (while (and sub (> rest 0))
4823 (unless (eq (window-new-normal sub) 'ignore)
4824 (set-window-new-pixel sub (min rest char-size) t)
4825 (setq rest (- rest char-size)))
4826 (setq sub (window-right sub)))
4828 (setq sub first)
4829 (while sub
4830 ;; Record new normal sizes.
4831 (set-window-new-normal
4832 sub (/ (if (eq (window-new-normal sub) 'ignore)
4833 (window-size sub horizontal t)
4834 (window-new-pixel sub))
4835 (float parent-size)))
4836 ;; Recursively balance each window's child windows.
4837 (balance-windows-1 sub horizontal)
4838 (setq sub (window-right sub)))))
4840 (defun balance-windows-1 (window &optional horizontal)
4841 "Subroutine of `balance-windows'."
4842 (if (window-child window)
4843 (let ((sub (window-child window)))
4844 (if (window-combined-p sub horizontal)
4845 (balance-windows-2 window horizontal)
4846 (let ((size (window-new-pixel window)))
4847 (while sub
4848 (set-window-new-pixel sub size)
4849 (balance-windows-1 sub horizontal)
4850 (setq sub (window-right sub))))))))
4852 (defun balance-windows (&optional window-or-frame)
4853 "Balance the sizes of windows of WINDOW-OR-FRAME.
4854 WINDOW-OR-FRAME is optional and defaults to the selected frame.
4855 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
4856 windows of that frame. If WINDOW-OR-FRAME denotes a window,
4857 recursively balance the sizes of all child windows of that
4858 window."
4859 (interactive)
4860 (let* ((window
4861 (cond
4862 ((or (not window-or-frame)
4863 (frame-live-p window-or-frame))
4864 (frame-root-window window-or-frame))
4865 ((or (window-live-p window-or-frame)
4866 (window-child window-or-frame))
4867 window-or-frame)
4869 (error "Not a window or frame %s" window-or-frame))))
4870 (frame (window-frame window)))
4871 ;; Balance vertically.
4872 (window--resize-reset (window-frame window))
4873 (balance-windows-1 window)
4874 (when (window--resize-apply-p frame)
4875 (window-resize-apply frame)
4876 (window--pixel-to-total frame)
4877 (run-window-configuration-change-hook frame))
4878 ;; Balance horizontally.
4879 (window--resize-reset (window-frame window) t)
4880 (balance-windows-1 window t)
4881 (when (window--resize-apply-p frame t)
4882 (window-resize-apply frame t)
4883 (window--pixel-to-total frame t)
4884 (run-window-configuration-change-hook frame))))
4886 (defun window-fixed-size-p (&optional window direction)
4887 "Return t if WINDOW cannot be resized in DIRECTION.
4888 WINDOW defaults to the selected window. DIRECTION can be
4889 nil (i.e. any), `height' or `width'."
4890 (with-current-buffer (window-buffer window)
4891 (when (and (boundp 'window-size-fixed) window-size-fixed)
4892 (not (and direction
4893 (member (cons direction window-size-fixed)
4894 '((height . width) (width . height))))))))
4896 ;;; A different solution to balance-windows.
4897 (defvar window-area-factor 1
4898 "Factor by which the window area should be over-estimated.
4899 This is used by `balance-windows-area'.
4900 Changing this globally has no effect.")
4901 (make-variable-buffer-local 'window-area-factor)
4903 (defun balance-windows-area-adjust (window delta horizontal pixelwise)
4904 "Wrapper around `window-resize' with error checking.
4905 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
4906 ;; `window-resize' may fail if delta is too large.
4907 (while (>= (abs delta) 1)
4908 (condition-case nil
4909 (progn
4910 ;; It was wrong to use `window-resize' here. Somehow
4911 ;; `balance-windows-area' depends on resizing windows
4912 ;; asymmetrically.
4913 (adjust-window-trailing-edge window delta horizontal pixelwise)
4914 (setq delta 0))
4915 (error
4916 ;;(message "adjust: %s" (error-message-string err))
4917 (setq delta (/ delta 2))))))
4919 (defun balance-windows-area ()
4920 "Make all visible windows the same area (approximately).
4921 See also `window-area-factor' to change the relative size of
4922 specific buffers."
4923 (interactive)
4924 (let* ((unchanged 0) (carry 0) (round 0)
4925 ;; Remove fixed-size windows.
4926 (wins (delq nil (mapcar (lambda (win)
4927 (if (not (window-fixed-size-p win)) win))
4928 (window-list nil 'nomini))))
4929 (changelog nil)
4930 (pixelwise window-resize-pixelwise)
4931 next)
4932 ;; Resizing a window changes the size of surrounding windows in complex
4933 ;; ways, so it's difficult to balance them all. The introduction of
4934 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
4935 ;; very difficult to do. `balance-window' above takes an off-line
4936 ;; approach: get the whole window tree, then balance it, then try to
4937 ;; adjust the windows so they fit the result.
4938 ;; Here, instead, we take a "local optimization" approach, where we just
4939 ;; go through all the windows several times until nothing needs to be
4940 ;; changed. The main problem with this approach is that it's difficult
4941 ;; to make sure it terminates, so we use some heuristic to try and break
4942 ;; off infinite loops.
4943 ;; After a round without any change, we allow a second, to give a chance
4944 ;; to the carry to propagate a minor imbalance from the end back to
4945 ;; the beginning.
4946 (while (< unchanged 2)
4947 ;; (message "New round")
4948 (setq unchanged (1+ unchanged) round (1+ round))
4949 (dolist (win wins)
4950 (setq next win)
4951 (while (progn (setq next (next-window next))
4952 (window-fixed-size-p next)))
4953 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
4954 (let* ((horiz
4955 (< (car (window-pixel-edges win)) (car (window-pixel-edges next))))
4956 (areadiff (/ (- (* (window-size next nil pixelwise)
4957 (window-size next t pixelwise)
4958 (buffer-local-value 'window-area-factor
4959 (window-buffer next)))
4960 (* (window-size win nil pixelwise)
4961 (window-size win t pixelwise)
4962 (buffer-local-value 'window-area-factor
4963 (window-buffer win))))
4964 (max (buffer-local-value 'window-area-factor
4965 (window-buffer win))
4966 (buffer-local-value 'window-area-factor
4967 (window-buffer next)))))
4968 (edgesize (if horiz
4969 (+ (window-size win nil pixelwise)
4970 (window-size next nil pixelwise))
4971 (+ (window-size win t pixelwise)
4972 (window-size next t pixelwise))))
4973 (diff (/ areadiff edgesize)))
4974 (when (zerop diff)
4975 ;; Maybe diff is actually closer to 1 than to 0.
4976 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
4977 (when (and (zerop diff) (not (zerop areadiff)))
4978 (setq diff (/ (+ areadiff carry) edgesize))
4979 ;; Change things smoothly.
4980 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
4981 (if (zerop diff)
4982 ;; Make sure negligible differences don't accumulate to
4983 ;; become significant.
4984 (setq carry (+ carry areadiff))
4985 ;; This used `adjust-window-trailing-edge' before and uses
4986 ;; `window-resize' now. Error wrapping is still needed.
4987 (balance-windows-area-adjust win diff horiz pixelwise)
4988 ;; (sit-for 0.5)
4989 (let ((change (cons win (window-pixel-edges win))))
4990 ;; If the same change has been seen already for this window,
4991 ;; we're most likely in an endless loop, so don't count it as
4992 ;; a change.
4993 (unless (member change changelog)
4994 (push change changelog)
4995 (setq unchanged 0 carry 0)))))))
4996 ;; We've now basically balanced all the windows.
4997 ;; But there may be some minor off-by-one imbalance left over,
4998 ;; so let's do some fine tuning.
4999 ;; (bw-finetune wins)
5000 ;; (message "Done in %d rounds" round)
5003 ;;; Window states, how to get them and how to put them in a window.
5004 (defun window--state-get-1 (window &optional writable)
5005 "Helper function for `window-state-get'."
5006 (let* ((type
5007 (cond
5008 ((window-top-child window) 'vc)
5009 ((window-left-child window) 'hc)
5010 (t 'leaf)))
5011 (buffer (window-buffer window))
5012 (selected (eq window (selected-window)))
5013 (head
5014 `(,type
5015 ,@(unless (window-next-sibling window) `((last . t)))
5016 (pixel-width . ,(window-pixel-width window))
5017 (pixel-height . ,(window-pixel-height window))
5018 (total-width . ,(window-total-width window))
5019 (total-height . ,(window-total-height window))
5020 (normal-height . ,(window-normal-size window))
5021 (normal-width . ,(window-normal-size window t))
5022 ,@(unless (window-live-p window)
5023 `((combination-limit . ,(window-combination-limit window))))
5024 ,@(let ((parameters (window-parameters window))
5025 list)
5026 ;; Make copies of those window parameters whose
5027 ;; persistence property is `writable' if WRITABLE is
5028 ;; non-nil and non-nil if WRITABLE is nil.
5029 (dolist (par parameters)
5030 (let ((pers (cdr (assq (car par)
5031 window-persistent-parameters))))
5032 (when (and pers (or (not writable) (eq pers 'writable)))
5033 (setq list (cons (cons (car par) (cdr par)) list)))))
5034 ;; Add `clone-of' parameter if necessary.
5035 (let ((pers (cdr (assq 'clone-of
5036 window-persistent-parameters))))
5037 (when (and pers (or (not writable) (eq pers 'writable))
5038 (not (assq 'clone-of list)))
5039 (setq list (cons (cons 'clone-of window) list))))
5040 (when list
5041 `((parameters . ,list))))
5042 ,@(when buffer
5043 ;; All buffer related things go in here.
5044 (let ((point (window-point window))
5045 (start (window-start window)))
5046 `((buffer
5047 ,(buffer-name buffer)
5048 (selected . ,selected)
5049 (hscroll . ,(window-hscroll window))
5050 (fringes . ,(window-fringes window))
5051 (margins . ,(window-margins window))
5052 (scroll-bars . ,(window-scroll-bars window))
5053 (vscroll . ,(window-vscroll window))
5054 (dedicated . ,(window-dedicated-p window))
5055 (point . ,(if writable point
5056 (copy-marker point
5057 (buffer-local-value
5058 'window-point-insertion-type
5059 buffer))))
5060 (start . ,(if writable start (copy-marker start)))))))))
5061 (tail
5062 (when (memq type '(vc hc))
5063 (let (list)
5064 (setq window (window-child window))
5065 (while window
5066 (setq list (cons (window--state-get-1 window writable) list))
5067 (setq window (window-right window)))
5068 (nreverse list)))))
5069 (append head tail)))
5071 (defun window-state-get (&optional window writable)
5072 "Return state of WINDOW as a Lisp object.
5073 WINDOW can be any window and defaults to the root window of the
5074 selected frame.
5076 Optional argument WRITABLE non-nil means do not use markers for
5077 sampling `window-point' and `window-start'. Together, WRITABLE
5078 and the variable `window-persistent-parameters' specify which
5079 window parameters are saved by this function. WRITABLE should be
5080 non-nil when the return value shall be written to a file and read
5081 back in another session. Otherwise, an application may run into
5082 an `invalid-read-syntax' error while attempting to read back the
5083 value from file.
5085 The return value can be used as argument for `window-state-put'
5086 to put the state recorded here into an arbitrary window. The
5087 value can be also stored on disk and read back in a new session."
5088 (setq window
5089 (if window
5090 (if (window-valid-p window)
5091 window
5092 (error "%s is not a live or internal window" window))
5093 (frame-root-window)))
5094 ;; The return value is a cons whose car specifies some constraints on
5095 ;; the size of WINDOW. The cdr lists the states of the child windows
5096 ;; of WINDOW.
5097 (cons
5098 ;; Frame related things would go into a function, say `frame-state',
5099 ;; calling `window-state-get' to insert the frame's root window.
5100 `((min-height . ,(window-min-size window))
5101 (min-width . ,(window-min-size window t))
5102 (min-height-ignore . ,(window-min-size window nil t))
5103 (min-width-ignore . ,(window-min-size window t t))
5104 (min-height-safe . ,(window-min-size window nil 'safe))
5105 (min-width-safe . ,(window-min-size window t 'safe))
5106 (min-pixel-height . ,(window-min-size window nil nil t))
5107 (min-pixel-width . ,(window-min-size window t nil t))
5108 (min-pixel-height-ignore . ,(window-min-size window nil t t))
5109 (min-pixel-width-ignore . ,(window-min-size window t t t))
5110 (min-pixel-height-safe . ,(window-min-size window nil 'safe t))
5111 (min-pixel-width-safe . ,(window-min-size window t 'safe t)))
5112 (window--state-get-1 window writable)))
5114 (defvar window-state-put-list nil
5115 "Helper variable for `window-state-put'.")
5117 (defvar window-state-put-stale-windows nil
5118 "Helper variable for `window-state-put'.")
5120 (defun window--state-put-1 (state &optional window ignore totals pixelwise)
5121 "Helper function for `window-state-put'."
5122 (let ((type (car state)))
5123 (setq state (cdr state))
5124 (cond
5125 ((eq type 'leaf)
5126 ;; For a leaf window just add unprocessed entries to
5127 ;; `window-state-put-list'.
5128 (push (cons window state) window-state-put-list))
5129 ((memq type '(vc hc))
5130 (let* ((horizontal (eq type 'hc))
5131 (total (window-size window horizontal pixelwise))
5132 (first t)
5133 size new)
5134 (dolist (item state)
5135 ;; Find the next child window. WINDOW always points to the
5136 ;; real window that we want to fill with what we find here.
5137 (when (memq (car item) '(leaf vc hc))
5138 (if (assq 'last item)
5139 ;; The last child window. Below `window--state-put-1'
5140 ;; will put into it whatever ITEM has in store.
5141 (setq new nil)
5142 ;; Not the last child window, prepare for splitting
5143 ;; WINDOW. SIZE is the new (and final) size of the old
5144 ;; window.
5145 (setq size
5146 (if totals
5147 ;; Use total size.
5148 (if pixelwise
5149 (cdr (assq (if horizontal
5150 'pixel-width
5151 'pixel-height)
5152 item))
5153 (cdr (assq (if horizontal
5154 'total-width
5155 'total-height)
5156 item)))
5157 ;; Use normalized size and round.
5158 (round
5159 (* total
5160 (cdr (assq (if horizontal 'normal-width 'normal-height)
5161 item))))))
5163 ;; Use safe sizes, we try to resize later.
5164 (setq size (max size
5165 (if horizontal
5166 (* window-safe-min-width
5167 (if pixelwise
5168 (frame-char-width (window-frame window))
5170 (* window-safe-min-height
5171 (if pixelwise
5172 (frame-char-height (window-frame window))
5173 1)))))
5174 (if (window-sizable-p window (- size) horizontal 'safe pixelwise)
5175 (let* ((window-combination-limit
5176 (assq 'combination-limit item)))
5177 ;; We must inherit the combination limit, otherwise
5178 ;; we might mess up handling of atomic and side
5179 ;; window.
5180 (setq new (split-window window size horizontal pixelwise)))
5181 ;; Give up if we can't resize window down to safe sizes.
5182 (error "Cannot resize window %s" window))
5184 (when first
5185 (setq first nil)
5186 ;; When creating the first child window add for parent
5187 ;; unprocessed entries to `window-state-put-list'.
5188 (setq window-state-put-list
5189 (cons (cons (window-parent window) state)
5190 window-state-put-list))))
5192 ;; Now process the current window (either the one we've just
5193 ;; split or the last child of its parent).
5194 (window--state-put-1 item window ignore totals)
5195 ;; Continue with the last window split off.
5196 (setq window new))))))))
5198 (defun window--state-put-2 (ignore pixelwise)
5199 "Helper function for `window-state-put'."
5200 (dolist (item window-state-put-list)
5201 (let ((window (car item))
5202 (combination-limit (cdr (assq 'combination-limit item)))
5203 (parameters (cdr (assq 'parameters item)))
5204 (state (cdr (assq 'buffer item))))
5205 (when combination-limit
5206 (set-window-combination-limit window combination-limit))
5207 ;; Reset window's parameters and assign saved ones (we might want
5208 ;; a `remove-window-parameters' function here).
5209 (dolist (parameter (window-parameters window))
5210 (set-window-parameter window (car parameter) nil))
5211 (when parameters
5212 (dolist (parameter parameters)
5213 (set-window-parameter window (car parameter) (cdr parameter))))
5214 ;; Process buffer related state.
5215 (when state
5216 (let ((buffer (get-buffer (car state))))
5217 (if buffer
5218 (with-current-buffer buffer
5219 (set-window-buffer window buffer)
5220 (set-window-hscroll window (cdr (assq 'hscroll state)))
5221 (apply 'set-window-fringes
5222 (cons window (cdr (assq 'fringes state))))
5223 (let ((margins (cdr (assq 'margins state))))
5224 (set-window-margins window (car margins) (cdr margins)))
5225 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
5226 (set-window-scroll-bars
5227 window (car scroll-bars) (nth 2 scroll-bars)
5228 (nth 3 scroll-bars) (nth 5 scroll-bars)))
5229 (set-window-vscroll window (cdr (assq 'vscroll state)))
5230 ;; Adjust vertically.
5231 (if (memq window-size-fixed '(t height))
5232 ;; A fixed height window, try to restore the
5233 ;; original size.
5234 (let ((delta
5235 (- (cdr (assq
5236 (if pixelwise 'pixel-height 'total-height)
5237 item))
5238 (window-size window nil pixelwise)))
5239 window-size-fixed)
5240 (when (window--resizable-p
5241 window delta nil nil nil nil nil pixelwise)
5242 (window-resize window delta nil nil pixelwise)))
5243 ;; Else check whether the window is not high enough.
5244 (let* ((min-size
5245 (window-min-size window nil ignore pixelwise))
5246 (delta
5247 (- min-size (window-size window nil pixelwise))))
5248 (when (and (> delta 0)
5249 (window--resizable-p
5250 window delta nil ignore nil nil nil pixelwise))
5251 (window-resize window delta nil ignore pixelwise))))
5252 ;; Adjust horizontally.
5253 (if (memq window-size-fixed '(t width))
5254 ;; A fixed width window, try to restore the original
5255 ;; size.
5256 (let ((delta
5257 (- (cdr (assq
5258 (if pixelwise 'pixel-width 'total-width)
5259 item))
5260 (window-size window t pixelwise)))
5261 window-size-fixed)
5262 (when (window--resizable-p
5263 window delta nil nil nil nil nil pixelwise)
5264 (window-resize window delta nil nil pixelwise)))
5265 ;; Else check whether the window is not wide enough.
5266 (let* ((min-size (window-min-size window t ignore pixelwise))
5267 (delta (- min-size (window-size window t pixelwise))))
5268 (when (and (> delta 0)
5269 (window--resizable-p
5270 window delta t ignore nil nil nil pixelwise))
5271 (window-resize window delta t ignore pixelwise))))
5272 ;; Set dedicated status.
5273 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
5274 ;; Install positions (maybe we should do this after all
5275 ;; windows have been created and sized).
5276 (ignore-errors
5277 (set-window-start window (cdr (assq 'start state)))
5278 (set-window-point window (cdr (assq 'point state))))
5279 ;; Select window if it's the selected one.
5280 (when (cdr (assq 'selected state))
5281 (select-window window)))
5282 ;; We don't want to raise an error in case the buffer does
5283 ;; not exist anymore, so we switch to a previous one and
5284 ;; save the window with the intention of deleting it later
5285 ;; if possible.
5286 (switch-to-prev-buffer window)
5287 (push window window-state-put-stale-windows)))))))
5289 (defun window-state-put (state &optional window ignore)
5290 "Put window state STATE into WINDOW.
5291 STATE should be the state of a window returned by an earlier
5292 invocation of `window-state-get'. Optional argument WINDOW must
5293 specify a valid window and defaults to the selected one. If
5294 WINDOW is not live, replace WINDOW by a live one before putting
5295 STATE into it.
5297 Optional argument IGNORE non-nil means ignore minimum window
5298 sizes and fixed size restrictions. IGNORE equal `safe' means
5299 windows can get as small as `window-safe-min-height' and
5300 `window-safe-min-width'."
5301 (setq window-state-put-stale-windows nil)
5302 (setq window (window-normalize-window window))
5304 ;; When WINDOW is internal, reduce it to a live one to put STATE into,
5305 ;; see Bug#16793.
5306 (unless (window-live-p window)
5307 (let ((root (frame-root-window window)))
5308 (if (eq window root)
5309 (setq window (frame-first-window root))
5310 (setq root window)
5311 (setq window (catch 'live
5312 (walk-window-subtree
5313 (lambda (window)
5314 (when (window-live-p window)
5315 (throw 'live window)))
5316 root))))
5317 (delete-other-windows-internal window root)))
5319 (let* ((frame (window-frame window))
5320 (head (car state))
5321 ;; We check here (1) whether the total sizes of root window of
5322 ;; STATE and that of WINDOW are equal so we can avoid
5323 ;; calculating new sizes, and (2) if we do have to resize
5324 ;; whether we can do so without violating size restrictions.
5325 (pixelwise (and (cdr (assq 'pixel-width state))
5326 (cdr (assq 'pixel-height state))))
5327 (totals (or (and pixelwise
5328 (= (window-pixel-width window)
5329 (cdr (assq 'pixel-width state)))
5330 (= (window-pixel-height window)
5331 (cdr (assq 'pixel-height state))))
5332 (and (= (window-total-width window)
5333 (cdr (assq 'total-width state)))
5334 (= (window-total-height window)
5335 (cdr (assq 'total-height state))))))
5336 (min-height (cdr (assq
5337 (if pixelwise 'min-pixel-height 'min-height)
5338 head)))
5339 (min-width (cdr (assq
5340 (if pixelwise 'min-pixel-width 'min-weight)
5341 head))))
5342 (if (and (not totals)
5343 (or (> min-height (window-size window nil pixelwise))
5344 (> min-width (window-size window t pixelwise)))
5345 (or (not ignore)
5346 (and (setq min-height
5347 (cdr (assq
5348 (if pixelwise
5349 'min-pixel-height-ignore
5350 'min-height-ignore)
5351 head)))
5352 (setq min-width
5353 (cdr (assq
5354 (if pixelwise
5355 'min-pixel-width-ignore
5356 'min-width-ignore)
5357 head)))
5358 (or (> min-height
5359 (window-size window nil pixelwise))
5360 (> min-width
5361 (window-size window t pixelwise)))
5362 (or (not (eq ignore 'safe))
5363 (and (setq min-height
5364 (cdr (assq
5365 (if pixelwise
5366 'min-pixel-height-safe
5367 'min-height-safe)
5368 head)))
5369 (setq min-width
5370 (cdr (assq
5371 (if pixelwise
5372 'min-pixel-width-safe
5373 'min-width-safe)
5374 head)))
5375 (or (> min-height
5376 (window-size window nil pixelwise))
5377 (> min-width
5378 (window-size window t pixelwise))))))))
5379 ;; The check above might not catch all errors due to rounding
5380 ;; issues - so IGNORE equal 'safe might not always produce the
5381 ;; minimum possible state. But such configurations hardly make
5382 ;; sense anyway.
5383 (error "Window %s too small to accommodate state" window)
5384 (setq state (cdr state))
5385 (setq window-state-put-list nil)
5386 ;; Work on the windows of a temporary buffer to make sure that
5387 ;; splitting proceeds regardless of any buffer local values of
5388 ;; `window-size-fixed'. Release that buffer after the buffers of
5389 ;; all live windows have been set by `window--state-put-2'.
5390 (with-temp-buffer
5391 (set-window-buffer window (current-buffer))
5392 (window--state-put-1 state window nil totals pixelwise)
5393 (window--state-put-2 ignore pixelwise))
5394 (while window-state-put-stale-windows
5395 (let ((window (pop window-state-put-stale-windows)))
5396 (when (eq (window-deletable-p window) t)
5397 (delete-window window))))
5398 (window--check frame))))
5400 (defun display-buffer-record-window (type window buffer)
5401 "Record information for window used by `display-buffer'.
5402 TYPE specifies the type of the calling operation and must be one
5403 of the symbols 'reuse (when WINDOW existed already and was
5404 reused for displaying BUFFER), 'window (when WINDOW was created
5405 on an already existing frame), or 'frame (when WINDOW was
5406 created on a new frame). WINDOW is the window used for or created
5407 by the `display-buffer' routines. BUFFER is the buffer that
5408 shall be displayed.
5410 This function installs or updates the quit-restore parameter of
5411 WINDOW. The quit-restore parameter is a list of four elements:
5412 The first element is one of the symbols 'window, 'frame, 'same or
5413 'other. The second element is either one of the symbols 'window
5414 or 'frame or a list whose elements are the buffer previously
5415 shown in the window, that buffer's window start and window point,
5416 and the window's height. The third element is the window
5417 selected at the time the parameter was created. The fourth
5418 element is BUFFER."
5419 (cond
5420 ((eq type 'reuse)
5421 (if (eq (window-buffer window) buffer)
5422 ;; WINDOW shows BUFFER already.
5423 (when (consp (window-parameter window 'quit-restore))
5424 ;; If WINDOW has a quit-restore parameter, reset its car.
5425 (setcar (window-parameter window 'quit-restore) 'same))
5426 ;; WINDOW shows another buffer.
5427 (with-current-buffer (window-buffer window)
5428 (set-window-parameter
5429 window 'quit-restore
5430 (list 'other
5431 ;; A quadruple of WINDOW's buffer, start, point and height.
5432 (list (current-buffer) (window-start window)
5433 ;; Preserve window-point-insertion-type (Bug#12588).
5434 (copy-marker
5435 (window-point window) window-point-insertion-type)
5436 (window-total-height window))
5437 (selected-window) buffer)))))
5438 ((eq type 'window)
5439 ;; WINDOW has been created on an existing frame.
5440 (set-window-parameter
5441 window 'quit-restore
5442 (list 'window 'window (selected-window) buffer)))
5443 ((eq type 'frame)
5444 ;; WINDOW has been created on a new frame.
5445 (set-window-parameter
5446 window 'quit-restore
5447 (list 'frame 'frame (selected-window) buffer)))))
5449 (defcustom display-buffer-function nil
5450 "If non-nil, function to call to handle `display-buffer'.
5451 It will receive two args, the buffer and a flag which if non-nil
5452 means that the currently selected window is not acceptable. It
5453 should choose or create a window, display the specified buffer in
5454 it, and return the window.
5456 The specified function should call `display-buffer-record-window'
5457 with corresponding arguments to set up the quit-restore parameter
5458 of the window used."
5459 :type '(choice
5460 (const nil)
5461 (function :tag "function"))
5462 :group 'windows)
5464 (make-obsolete-variable 'display-buffer-function
5465 'display-buffer-alist "24.3")
5467 ;; Eventually, we want to turn this into a defvar; instead of
5468 ;; customizing this, the user should use a `pop-up-frame-parameters'
5469 ;; alist entry in `display-buffer-base-action'.
5470 (defcustom pop-up-frame-alist nil
5471 "Alist of parameters for automatically generated new frames.
5472 If non-nil, the value you specify here is used by the default
5473 `pop-up-frame-function' for the creation of new frames.
5475 Since `pop-up-frame-function' is used by `display-buffer' for
5476 making new frames, any value specified here by default affects
5477 the automatic generation of new frames via `display-buffer' and
5478 all functions based on it. The behavior of `make-frame' is not
5479 affected by this variable."
5480 :type '(repeat (cons :format "%v"
5481 (symbol :tag "Parameter")
5482 (sexp :tag "Value")))
5483 :group 'frames)
5485 (defcustom pop-up-frame-function
5486 (lambda () (make-frame pop-up-frame-alist))
5487 "Function used by `display-buffer' for creating a new frame.
5488 This function is called with no arguments and should return a new
5489 frame. The default value calls `make-frame' with the argument
5490 `pop-up-frame-alist'."
5491 :type 'function
5492 :group 'frames)
5494 (defcustom special-display-buffer-names nil
5495 "List of names of buffers that should be displayed specially.
5496 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5497 its name is in this list, displays the buffer in a way specified
5498 by `special-display-function'. `special-display-popup-frame'
5499 \(the default for `special-display-function') usually displays
5500 the buffer in a separate frame made with the parameters specified
5501 by `special-display-frame-alist'. If `special-display-function'
5502 has been set to some other function, that function is called with
5503 the buffer as first, and nil as second argument.
5505 Alternatively, an element of this list can be specified as
5506 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
5507 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5508 `special-display-popup-frame' will interpret such pairs as frame
5509 parameters when it creates a special frame, overriding the
5510 corresponding values from `special-display-frame-alist'.
5512 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5513 `special-display-popup-frame' displays that buffer in the
5514 selected window. If FRAME-PARAMETERS contains (same-frame . t),
5515 it displays that buffer in a window on the selected frame.
5517 If `special-display-function' specifies some other function than
5518 `special-display-popup-frame', that function is called with the
5519 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
5520 argument.
5522 Finally, an element of this list can be also specified as
5523 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
5524 `special-display-popup-frame' will call FUNCTION with the buffer
5525 named BUFFER-NAME as first argument, and OTHER-ARGS as the
5526 second.
5528 Any alternative function specified here is responsible for
5529 setting up the quit-restore parameter of the window used.
5531 If this variable appears \"not to work\", because you added a
5532 name to it but the corresponding buffer is displayed in the
5533 selected window, look at the values of `same-window-buffer-names'
5534 and `same-window-regexps'. Those variables take precedence over
5535 this one.
5537 See also `special-display-regexps'."
5538 :type '(repeat
5539 (choice :tag "Buffer"
5540 :value ""
5541 (string :format "%v")
5542 (cons :tag "With parameters"
5543 :format "%v"
5544 :value ("" . nil)
5545 (string :format "%v")
5546 (repeat :tag "Parameters"
5547 (cons :format "%v"
5548 (symbol :tag "Parameter")
5549 (sexp :tag "Value"))))
5550 (list :tag "With function"
5551 :format "%v"
5552 :value ("" . nil)
5553 (string :format "%v")
5554 (function :tag "Function")
5555 (repeat :tag "Arguments" (sexp)))))
5556 :group 'windows
5557 :group 'frames)
5558 (make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3")
5559 (put 'special-display-buffer-names 'risky-local-variable t)
5561 (defcustom special-display-regexps nil
5562 "List of regexps saying which buffers should be displayed specially.
5563 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
5564 any regexp in this list matches its name, displays it specially
5565 using `special-display-function'. `special-display-popup-frame'
5566 \(the default for `special-display-function') usually displays
5567 the buffer in a separate frame made with the parameters specified
5568 by `special-display-frame-alist'. If `special-display-function'
5569 has been set to some other function, that function is called with
5570 the buffer as first, and nil as second argument.
5572 Alternatively, an element of this list can be specified as
5573 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
5574 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
5575 `special-display-popup-frame' will then interpret these pairs as
5576 frame parameters when creating a special frame for a buffer whose
5577 name matches REGEXP, overriding the corresponding values from
5578 `special-display-frame-alist'.
5580 As a special case, if FRAME-PARAMETERS contains (same-window . t)
5581 `special-display-popup-frame' displays buffers matching REGEXP in
5582 the selected window. (same-frame . t) in FRAME-PARAMETERS means
5583 to display such buffers in a window on the selected frame.
5585 If `special-display-function' specifies some other function than
5586 `special-display-popup-frame', that function is called with the
5587 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
5588 as second argument.
5590 Finally, an element of this list can be also specified as
5591 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
5592 will then call FUNCTION with the buffer whose name matched
5593 REGEXP as first, and OTHER-ARGS as second argument.
5595 Any alternative function specified here is responsible for
5596 setting up the quit-restore parameter of the window used.
5598 If this variable appears \"not to work\", because you added a
5599 name to it but the corresponding buffer is displayed in the
5600 selected window, look at the values of `same-window-buffer-names'
5601 and `same-window-regexps'. Those variables take precedence over
5602 this one.
5604 See also `special-display-buffer-names'."
5605 :type '(repeat
5606 (choice :tag "Buffer"
5607 :value ""
5608 (regexp :format "%v")
5609 (cons :tag "With parameters"
5610 :format "%v"
5611 :value ("" . nil)
5612 (regexp :format "%v")
5613 (repeat :tag "Parameters"
5614 (cons :format "%v"
5615 (symbol :tag "Parameter")
5616 (sexp :tag "Value"))))
5617 (list :tag "With function"
5618 :format "%v"
5619 :value ("" . nil)
5620 (regexp :format "%v")
5621 (function :tag "Function")
5622 (repeat :tag "Arguments" (sexp)))))
5623 :group 'windows
5624 :group 'frames)
5625 (make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3")
5626 (put 'special-display-regexps 'risky-local-variable t)
5628 (defun special-display-p (buffer-name)
5629 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
5630 More precisely, return t if `special-display-buffer-names' or
5631 `special-display-regexps' contain a string entry equaling or
5632 matching BUFFER-NAME. If `special-display-buffer-names' or
5633 `special-display-regexps' contain a list entry whose car equals
5634 or matches BUFFER-NAME, the return value is the cdr of that
5635 entry."
5636 (let (tmp)
5637 (cond
5638 ((member buffer-name special-display-buffer-names)
5640 ((setq tmp (assoc buffer-name special-display-buffer-names))
5641 (cdr tmp))
5642 ((catch 'found
5643 (dolist (regexp special-display-regexps)
5644 (cond
5645 ((stringp regexp)
5646 (when (string-match-p regexp buffer-name)
5647 (throw 'found t)))
5648 ((and (consp regexp) (stringp (car regexp))
5649 (string-match-p (car regexp) buffer-name))
5650 (throw 'found (cdr regexp))))))))))
5652 (defcustom special-display-frame-alist
5653 '((height . 14) (width . 80) (unsplittable . t))
5654 "Alist of parameters for special frames.
5655 Special frames are used for buffers whose names are listed in
5656 `special-display-buffer-names' and for buffers whose names match
5657 one of the regular expressions in `special-display-regexps'.
5659 This variable can be set in your init file, like this:
5661 (setq special-display-frame-alist '((width . 80) (height . 20)))
5663 These supersede the values given in `default-frame-alist'."
5664 :type '(repeat (cons :format "%v"
5665 (symbol :tag "Parameter")
5666 (sexp :tag "Value")))
5667 :group 'frames)
5668 (make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3")
5670 (defun special-display-popup-frame (buffer &optional args)
5671 "Pop up a frame displaying BUFFER and return its window.
5672 If BUFFER is already displayed in a visible or iconified frame,
5673 raise that frame. Otherwise, display BUFFER in a new frame.
5675 Optional argument ARGS is a list specifying additional
5676 information.
5678 If ARGS is an alist, use it as a list of frame parameters. If
5679 these parameters contain (same-window . t), display BUFFER in
5680 the selected window. If they contain (same-frame . t), display
5681 BUFFER in a window of the selected frame.
5683 If ARGS is a list whose car is a symbol, use (car ARGS) as a
5684 function to do the work. Pass it BUFFER as first argument, and
5685 pass the elements of (cdr ARGS) as the remaining arguments."
5686 (if (and args (symbolp (car args)))
5687 (apply (car args) buffer (cdr args))
5688 (let ((window (get-buffer-window buffer 0)))
5690 ;; If we have a window already, make it visible.
5691 (when window
5692 (let ((frame (window-frame window)))
5693 (make-frame-visible frame)
5694 (raise-frame frame)
5695 (display-buffer-record-window 'reuse window buffer)
5696 window))
5697 ;; Reuse the current window if the user requested it.
5698 (when (cdr (assq 'same-window args))
5699 (condition-case nil
5700 (progn (switch-to-buffer buffer nil t) (selected-window))
5701 (error nil)))
5702 ;; Stay on the same frame if requested.
5703 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
5704 (let* ((pop-up-windows t)
5705 pop-up-frames
5706 special-display-buffer-names special-display-regexps)
5707 (display-buffer buffer)))
5708 ;; If no window yet, make one in a new frame.
5709 (let* ((frame
5710 (with-current-buffer buffer
5711 (make-frame (append args special-display-frame-alist))))
5712 (window (frame-selected-window frame)))
5713 (display-buffer-record-window 'frame window buffer)
5714 (unless (eq buffer (window-buffer window))
5715 (set-window-buffer window buffer)
5716 (set-window-prev-buffers window nil))
5717 (set-window-dedicated-p window t)
5718 window)))))
5720 (defcustom special-display-function 'special-display-popup-frame
5721 "Function to call for displaying special buffers.
5722 This function is called with two arguments - the buffer and,
5723 optionally, a list - and should return a window displaying that
5724 buffer. The default value usually makes a separate frame for the
5725 buffer using `special-display-frame-alist' to specify the frame
5726 parameters. See the definition of `special-display-popup-frame'
5727 for how to specify such a function.
5729 A buffer is special when its name is either listed in
5730 `special-display-buffer-names' or matches a regexp in
5731 `special-display-regexps'.
5733 The specified function should call `display-buffer-record-window'
5734 with corresponding arguments to set up the quit-restore parameter
5735 of the window used."
5736 :type 'function
5737 :group 'frames)
5738 (make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3")
5740 (defcustom same-window-buffer-names nil
5741 "List of names of buffers that should appear in the \"same\" window.
5742 `display-buffer' and `pop-to-buffer' show a buffer whose name is
5743 on this list in the selected rather than some other window.
5745 An element of this list can be a cons cell instead of just a
5746 string. In that case, the cell's car must be a string specifying
5747 the buffer name. This is for compatibility with
5748 `special-display-buffer-names'; the cdr of the cons cell is
5749 ignored.
5751 See also `same-window-regexps'."
5752 :type '(repeat (string :format "%v"))
5753 :group 'windows)
5755 (defcustom same-window-regexps nil
5756 "List of regexps saying which buffers should appear in the \"same\" window.
5757 `display-buffer' and `pop-to-buffer' show a buffer whose name
5758 matches a regexp on this list in the selected rather than some
5759 other window.
5761 An element of this list can be a cons cell instead of just a
5762 string. In that case, the cell's car must be a regexp matching
5763 the buffer name. This is for compatibility with
5764 `special-display-regexps'; the cdr of the cons cell is ignored.
5766 See also `same-window-buffer-names'."
5767 :type '(repeat (regexp :format "%v"))
5768 :group 'windows)
5770 (defun same-window-p (buffer-name)
5771 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
5772 This function returns non-nil if `display-buffer' or
5773 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
5774 selected rather than (as usual) some other window. See
5775 `same-window-buffer-names' and `same-window-regexps'."
5776 (cond
5777 ((not (stringp buffer-name)))
5778 ;; The elements of `same-window-buffer-names' can be buffer
5779 ;; names or cons cells whose cars are buffer names.
5780 ((member buffer-name same-window-buffer-names))
5781 ((assoc buffer-name same-window-buffer-names))
5782 ((catch 'found
5783 (dolist (regexp same-window-regexps)
5784 ;; The elements of `same-window-regexps' can be regexps
5785 ;; or cons cells whose cars are regexps.
5786 (when (or (and (stringp regexp)
5787 (string-match-p regexp buffer-name))
5788 (and (consp regexp) (stringp (car regexp))
5789 (string-match-p (car regexp) buffer-name)))
5790 (throw 'found t)))))))
5792 (defcustom pop-up-frames nil
5793 "Whether `display-buffer' should make a separate frame.
5794 If nil, never make a separate frame.
5795 If the value is `graphic-only', make a separate frame
5796 on graphic displays only.
5797 Any other non-nil value means always make a separate frame."
5798 :type '(choice
5799 (const :tag "Never" nil)
5800 (const :tag "On graphic displays only" graphic-only)
5801 (const :tag "Always" t))
5802 :group 'windows)
5804 (defcustom display-buffer-reuse-frames nil
5805 "Non-nil means `display-buffer' should reuse frames.
5806 If the buffer in question is already displayed in a frame, raise
5807 that frame."
5808 :type 'boolean
5809 :version "21.1"
5810 :group 'windows)
5812 (make-obsolete-variable
5813 'display-buffer-reuse-frames
5814 "use a `reusable-frames' alist entry in `display-buffer-alist'."
5815 "24.3")
5817 (defcustom pop-up-windows t
5818 "Non-nil means `display-buffer' should make a new window."
5819 :type 'boolean
5820 :group 'windows)
5822 (defcustom split-window-preferred-function 'split-window-sensibly
5823 "Function called by `display-buffer' routines to split a window.
5824 This function is called with a window as single argument and is
5825 supposed to split that window and return the new window. If the
5826 window can (or shall) not be split, it is supposed to return nil.
5827 The default is to call the function `split-window-sensibly' which
5828 tries to split the window in a way which seems most suitable.
5829 You can customize the options `split-height-threshold' and/or
5830 `split-width-threshold' in order to have `split-window-sensibly'
5831 prefer either vertical or horizontal splitting.
5833 If you set this to any other function, bear in mind that the
5834 `display-buffer' routines may call this function two times. The
5835 argument of the first call is the largest window on its frame.
5836 If that call fails to return a live window, the function is
5837 called again with the least recently used window as argument. If
5838 that call fails too, `display-buffer' will use an existing window
5839 to display its buffer.
5841 The window selected at the time `display-buffer' was invoked is
5842 still selected when this function is called. Hence you can
5843 compare the window argument with the value of `selected-window'
5844 if you intend to split the selected window instead or if you do
5845 not want to split the selected window."
5846 :type 'function
5847 :version "23.1"
5848 :group 'windows)
5850 (defcustom split-height-threshold 80
5851 "Minimum height for splitting windows sensibly.
5852 If this is an integer, `split-window-sensibly' may split a window
5853 vertically only if it has at least this many lines. If this is
5854 nil, `split-window-sensibly' is not allowed to split a window
5855 vertically. If, however, a window is the only window on its
5856 frame, `split-window-sensibly' may split it vertically
5857 disregarding the value of this variable."
5858 :type '(choice (const nil) (integer :tag "lines"))
5859 :version "23.1"
5860 :group 'windows)
5862 (defcustom split-width-threshold 160
5863 "Minimum width for splitting windows sensibly.
5864 If this is an integer, `split-window-sensibly' may split a window
5865 horizontally only if it has at least this many columns. If this
5866 is nil, `split-window-sensibly' is not allowed to split a window
5867 horizontally."
5868 :type '(choice (const nil) (integer :tag "columns"))
5869 :version "23.1"
5870 :group 'windows)
5872 (defun window-splittable-p (window &optional horizontal)
5873 "Return non-nil if `split-window-sensibly' may split WINDOW.
5874 Optional argument HORIZONTAL nil or omitted means check whether
5875 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
5876 non-nil means check whether WINDOW may be split horizontally.
5878 WINDOW may be split vertically when the following conditions
5879 hold:
5880 - `window-size-fixed' is either nil or equals `width' for the
5881 buffer of WINDOW.
5882 - `split-height-threshold' is an integer and WINDOW is at least as
5883 high as `split-height-threshold'.
5884 - When WINDOW is split evenly, the emanating windows are at least
5885 `window-min-height' lines tall and can accommodate at least one
5886 line plus - if WINDOW has one - a mode line.
5888 WINDOW may be split horizontally when the following conditions
5889 hold:
5890 - `window-size-fixed' is either nil or equals `height' for the
5891 buffer of WINDOW.
5892 - `split-width-threshold' is an integer and WINDOW is at least as
5893 wide as `split-width-threshold'.
5894 - When WINDOW is split evenly, the emanating windows are at least
5895 `window-min-width' or two (whichever is larger) columns wide."
5896 (when (and (window-live-p window) (not (window--side-window-p window)))
5897 (with-current-buffer (window-buffer window)
5898 (if horizontal
5899 ;; A window can be split horizontally when its width is not
5900 ;; fixed, it is at least `split-width-threshold' columns wide
5901 ;; and at least twice as wide as `window-min-width' and 2 (the
5902 ;; latter value is hardcoded).
5903 (and (memq window-size-fixed '(nil height))
5904 ;; Testing `window-full-width-p' here hardly makes any
5905 ;; sense nowadays. This can be done more intuitively by
5906 ;; setting up `split-width-threshold' appropriately.
5907 (numberp split-width-threshold)
5908 (>= (window-width window)
5909 (max split-width-threshold
5910 (* 2 (max window-min-width 2)))))
5911 ;; A window can be split vertically when its height is not
5912 ;; fixed, it is at least `split-height-threshold' lines high,
5913 ;; and it is at least twice as high as `window-min-height' and 2
5914 ;; if it has a mode line or 1.
5915 (and (memq window-size-fixed '(nil width))
5916 (numberp split-height-threshold)
5917 (>= (window-height window)
5918 (max split-height-threshold
5919 (* 2 (max window-min-height
5920 (if mode-line-format 2 1))))))))))
5922 (defun split-window-sensibly (&optional window)
5923 "Split WINDOW in a way suitable for `display-buffer'.
5924 WINDOW defaults to the currently selected window.
5925 If `split-height-threshold' specifies an integer, WINDOW is at
5926 least `split-height-threshold' lines tall and can be split
5927 vertically, split WINDOW into two windows one above the other and
5928 return the lower window. Otherwise, if `split-width-threshold'
5929 specifies an integer, WINDOW is at least `split-width-threshold'
5930 columns wide and can be split horizontally, split WINDOW into two
5931 windows side by side and return the window on the right. If this
5932 can't be done either and WINDOW is the only window on its frame,
5933 try to split WINDOW vertically disregarding any value specified
5934 by `split-height-threshold'. If that succeeds, return the lower
5935 window. Return nil otherwise.
5937 By default `display-buffer' routines call this function to split
5938 the largest or least recently used window. To change the default
5939 customize the option `split-window-preferred-function'.
5941 You can enforce this function to not split WINDOW horizontally,
5942 by setting (or binding) the variable `split-width-threshold' to
5943 nil. If, in addition, you set `split-height-threshold' to zero,
5944 chances increase that this function does split WINDOW vertically.
5946 In order to not split WINDOW vertically, set (or bind) the
5947 variable `split-height-threshold' to nil. Additionally, you can
5948 set `split-width-threshold' to zero to make a horizontal split
5949 more likely to occur.
5951 Have a look at the function `window-splittable-p' if you want to
5952 know how `split-window-sensibly' determines whether WINDOW can be
5953 split."
5954 (let ((window (or window (selected-window))))
5955 (or (and (window-splittable-p window)
5956 ;; Split window vertically.
5957 (with-selected-window window
5958 (split-window-below)))
5959 (and (window-splittable-p window t)
5960 ;; Split window horizontally.
5961 (with-selected-window window
5962 (split-window-right)))
5963 (and (eq window (frame-root-window (window-frame window)))
5964 (not (window-minibuffer-p window))
5965 ;; If WINDOW is the only window on its frame and is not the
5966 ;; minibuffer window, try to split it vertically disregarding
5967 ;; the value of `split-height-threshold'.
5968 (let ((split-height-threshold 0))
5969 (when (window-splittable-p window)
5970 (with-selected-window window
5971 (split-window-below))))))))
5973 (defun window--try-to-split-window (window &optional alist)
5974 "Try to split WINDOW.
5975 Return value returned by `split-window-preferred-function' if it
5976 represents a live window, nil otherwise."
5977 (and (window-live-p window)
5978 (not (frame-parameter (window-frame window) 'unsplittable))
5979 (let* ((window-combination-limit
5980 ;; When `window-combination-limit' equals
5981 ;; `display-buffer' or equals `resize-window' and a
5982 ;; `window-height' or `window-width' alist entry are
5983 ;; present, bind it to t so resizing steals space
5984 ;; preferably from the window that was split.
5985 (if (or (eq window-combination-limit 'display-buffer)
5986 (and (eq window-combination-limit 'window-size)
5987 (or (cdr (assq 'window-height alist))
5988 (cdr (assq 'window-width alist)))))
5990 window-combination-limit))
5991 (new-window
5992 ;; Since `split-window-preferred-function' might
5993 ;; throw an error use `condition-case'.
5994 (condition-case nil
5995 (funcall split-window-preferred-function window)
5996 (error nil))))
5997 (and (window-live-p new-window) new-window))))
5999 (defun window--frame-usable-p (frame)
6000 "Return FRAME if it can be used to display a buffer."
6001 (when (frame-live-p frame)
6002 (let ((window (frame-root-window frame)))
6003 ;; `frame-root-window' may be an internal window which is considered
6004 ;; "dead" by `window-live-p'. Hence if `window' is not live we
6005 ;; implicitly know that `frame' has a visible window we can use.
6006 (unless (and (window-live-p window)
6007 (or (window-minibuffer-p window)
6008 ;; If the window is soft-dedicated, the frame is usable.
6009 ;; Actually, even if the window is really dedicated,
6010 ;; the frame is still usable by splitting it.
6011 ;; At least Emacs-22 allowed it, and it is desirable
6012 ;; when displaying same-frame windows.
6013 nil ; (eq t (window-dedicated-p window))
6015 frame))))
6017 (defcustom even-window-heights t
6018 "If non-nil `display-buffer' will try to even window heights.
6019 Otherwise `display-buffer' will leave the window configuration
6020 alone. Heights are evened only when `display-buffer' chooses a
6021 window that appears above or below the selected window."
6022 :type 'boolean
6023 :group 'windows)
6025 (defun window--even-window-heights (window)
6026 "Even heights of WINDOW and selected window.
6027 Do this only if these windows are vertically adjacent to each
6028 other, `even-window-heights' is non-nil, and the selected window
6029 is higher than WINDOW."
6030 (when (and even-window-heights
6031 ;; Even iff WINDOW forms a vertical combination with the
6032 ;; selected window, and WINDOW's height exceeds that of the
6033 ;; selected window, see also bug#11880.
6034 (window-combined-p window)
6035 (= (window-child-count (window-parent window)) 2)
6036 (eq (window-parent) (window-parent window))
6037 (> (window-total-height) (window-total-height window)))
6038 ;; Don't throw an error if we can't even window heights for
6039 ;; whatever reason.
6040 (condition-case nil
6041 (enlarge-window
6042 (/ (- (window-total-height window) (window-total-height)) 2))
6043 (error nil))))
6045 (defun window--display-buffer (buffer window type &optional alist dedicated)
6046 "Display BUFFER in WINDOW.
6047 TYPE must be one of the symbols `reuse', `window' or `frame' and
6048 is passed unaltered to `display-buffer-record-window'. ALIST is
6049 the alist argument of `display-buffer'. Set `window-dedicated-p'
6050 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
6051 live."
6052 (when (and (buffer-live-p buffer) (window-live-p window))
6053 (display-buffer-record-window type window buffer)
6054 (unless (eq buffer (window-buffer window))
6055 (set-window-dedicated-p window nil)
6056 (set-window-buffer window buffer)
6057 (when dedicated
6058 (set-window-dedicated-p window dedicated))
6059 (when (memq type '(window frame))
6060 (set-window-prev-buffers window nil)))
6061 (let ((parameter (window-parameter window 'quit-restore))
6062 (height (cdr (assq 'window-height alist)))
6063 (width (cdr (assq 'window-width alist)))
6064 (size (cdr (assq 'window-size alist)))
6065 (preserve-size (cdr (assq 'preserve-size alist))))
6066 (cond
6067 ((or (eq type 'frame)
6068 (and (eq (car parameter) 'same)
6069 (eq (nth 1 parameter) 'frame)))
6070 ;; Adjust size of frame if asked for.
6071 (cond
6072 ((not size))
6073 ((consp size)
6074 (let ((width (car size))
6075 (height (cdr size))
6076 (frame (window-frame window)))
6077 (when (and (numberp width) (numberp height))
6078 (set-frame-height
6079 frame (+ (frame-height frame)
6080 (- height (window-total-height window))))
6081 (set-frame-width
6082 frame (+ (frame-width frame)
6083 (- width (window-total-width window)))))))
6084 ((functionp size)
6085 (ignore-errors (funcall size window)))))
6086 ((or (eq type 'window)
6087 (and (eq (car parameter) 'same)
6088 (eq (nth 1 parameter) 'window)))
6089 ;; Adjust height of window if asked for.
6090 (cond
6091 ((not height))
6092 ((numberp height)
6093 (let* ((new-height
6094 (if (integerp height)
6095 height
6096 (round
6097 (* (window-total-height (frame-root-window window))
6098 height))))
6099 (delta (- new-height (window-total-height window))))
6100 (when (and (window--resizable-p window delta nil 'safe)
6101 (window-combined-p window))
6102 (window-resize window delta nil 'safe))))
6103 ((functionp height)
6104 (ignore-errors (funcall height window))))
6105 ;; Adjust width of window if asked for.
6106 (cond
6107 ((not width))
6108 ((numberp width)
6109 (let* ((new-width
6110 (if (integerp width)
6111 width
6112 (round
6113 (* (window-total-width (frame-root-window window))
6114 width))))
6115 (delta (- new-width (window-total-width window))))
6116 (when (and (window--resizable-p window delta t 'safe)
6117 (window-combined-p window t))
6118 (window-resize window delta t 'safe))))
6119 ((functionp width)
6120 (ignore-errors (funcall width window))))
6121 ;; Preserve window size if asked for.
6122 (when (consp preserve-size)
6123 (window-preserve-size window t (car preserve-size))
6124 (window-preserve-size window nil (cdr preserve-size))))))
6126 window))
6128 (defun window--maybe-raise-frame (frame)
6129 (let ((visible (frame-visible-p frame)))
6130 (unless (or (not visible)
6131 ;; Assume the selected frame is already visible enough.
6132 (eq frame (selected-frame))
6133 ;; Assume the frame from which we invoked the
6134 ;; minibuffer is visible.
6135 (and (minibuffer-window-active-p (selected-window))
6136 (eq frame (window-frame (minibuffer-selected-window)))))
6137 (raise-frame frame))))
6139 ;; FIXME: Not implemented.
6140 ;; FIXME: By the way, there could be more levels of dedication:
6141 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
6142 ;; the window hasn't been used for something else yet.
6143 ;; - `soft' (`softly') dedicated only allows reuse when asked explicitly.
6144 ;; - `strongly' never allows reuse.
6145 (defvar display-buffer-mark-dedicated nil
6146 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
6147 The actual non-nil value of this variable will be copied to the
6148 `window-dedicated-p' flag.")
6150 (defconst display-buffer--action-function-custom-type
6151 '(choice :tag "Function"
6152 (const :tag "--" ignore) ; default for insertion
6153 (const display-buffer-reuse-window)
6154 (const display-buffer-pop-up-window)
6155 (const display-buffer-same-window)
6156 (const display-buffer-pop-up-frame)
6157 (const display-buffer-below-selected)
6158 (const display-buffer-at-bottom)
6159 (const display-buffer-in-previous-window)
6160 (const display-buffer-use-some-window)
6161 (function :tag "Other function"))
6162 "Custom type for `display-buffer' action functions.")
6164 (defconst display-buffer--action-custom-type
6165 `(cons :tag "Action"
6166 (choice :tag "Action functions"
6167 ,display-buffer--action-function-custom-type
6168 (repeat
6169 :tag "List of functions"
6170 ,display-buffer--action-function-custom-type))
6171 (alist :tag "Action arguments"
6172 :key-type symbol
6173 :value-type (sexp :tag "Value")))
6174 "Custom type for `display-buffer' actions.")
6176 (defvar display-buffer-overriding-action '(nil . nil)
6177 "Overriding action to perform to display a buffer.
6178 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6179 function or a list of functions. Each function should accept two
6180 arguments: a buffer to display and an alist similar to ALIST.
6181 See `display-buffer' for details.")
6182 (put 'display-buffer-overriding-action 'risky-local-variable t)
6184 (defcustom display-buffer-alist nil
6185 "Alist of conditional actions for `display-buffer'.
6186 This is a list of elements (CONDITION . ACTION), where:
6188 CONDITION is either a regexp matching buffer names, or a
6189 function that takes two arguments - a buffer name and the
6190 ACTION argument of `display-buffer' - and returns a boolean.
6192 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
6193 function or a list of functions. Each such function should
6194 accept two arguments: a buffer to display and an alist of the
6195 same form as ALIST. See `display-buffer' for details.
6197 `display-buffer' scans this alist until it either finds a
6198 matching regular expression or the function specified by a
6199 condition returns non-nil. In any of these cases, it adds the
6200 associated action to the list of actions it will try."
6201 :type `(alist :key-type
6202 (choice :tag "Condition"
6203 regexp
6204 (function :tag "Matcher function"))
6205 :value-type ,display-buffer--action-custom-type)
6206 :risky t
6207 :version "24.1"
6208 :group 'windows)
6210 (defcustom display-buffer-base-action '(nil . nil)
6211 "User-specified default action for `display-buffer'.
6212 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
6213 function or a list of functions. Each function should accept two
6214 arguments: a buffer to display and an alist similar to ALIST.
6215 See `display-buffer' for details."
6216 :type display-buffer--action-custom-type
6217 :risky t
6218 :version "24.1"
6219 :group 'windows)
6221 (defconst display-buffer-fallback-action
6222 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
6223 display-buffer-reuse-window
6224 display-buffer--maybe-pop-up-frame-or-window
6225 display-buffer-in-previous-window
6226 display-buffer-use-some-window
6227 ;; If all else fails, pop up a new frame.
6228 display-buffer-pop-up-frame))
6229 "Default fallback action for `display-buffer'.
6230 This is the action used by `display-buffer' if no other actions
6231 specified, e.g. by the user options `display-buffer-alist' or
6232 `display-buffer-base-action'. See `display-buffer'.")
6233 (put 'display-buffer-fallback-action 'risky-local-variable t)
6235 (defun display-buffer-assq-regexp (buffer-name alist action)
6236 "Retrieve ALIST entry corresponding to BUFFER-NAME.
6237 ACTION is the action argument passed to `display-buffer'."
6238 (catch 'match
6239 (dolist (entry alist)
6240 (let ((key (car entry)))
6241 (when (or (and (stringp key)
6242 (string-match-p key buffer-name))
6243 (and (functionp key)
6244 (funcall key buffer-name action)))
6245 (throw 'match (cdr entry)))))))
6247 (defvar display-buffer--same-window-action
6248 '(display-buffer-same-window
6249 (inhibit-same-window . nil))
6250 "A `display-buffer' action for displaying in the same window.")
6251 (put 'display-buffer--same-window-action 'risky-local-variable t)
6253 (defvar display-buffer--other-frame-action
6254 '((display-buffer-reuse-window
6255 display-buffer-pop-up-frame)
6256 (reusable-frames . 0)
6257 (inhibit-same-window . t))
6258 "A `display-buffer' action for displaying in another frame.")
6259 (put 'display-buffer--other-frame-action 'risky-local-variable t)
6261 (defun display-buffer (buffer-or-name &optional action frame)
6262 "Display BUFFER-OR-NAME in some window, without selecting it.
6263 BUFFER-OR-NAME must be a buffer or the name of an existing
6264 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
6265 or nil if no such window is found.
6267 Optional argument ACTION, if non-nil, should specify a display
6268 action. Its form is described below.
6270 Optional argument FRAME, if non-nil, acts like an additional
6271 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
6272 specifying the frame(s) to search for a window that is already
6273 displaying the buffer. See `display-buffer-reuse-window'
6275 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
6276 where FUNCTION is either a function or a list of functions, and
6277 ALIST is an arbitrary association list (alist).
6279 Each such FUNCTION should accept two arguments: the buffer to
6280 display and an alist. Based on those arguments, it should
6281 display the buffer and return the window. If the caller is
6282 prepared to handle the case of not displaying the buffer
6283 and returning nil from `display-buffer' it should pass
6284 \(allow-no-window . t) as an element of the ALIST.
6286 The `display-buffer' function builds a function list and an alist
6287 by combining the functions and alists specified in
6288 `display-buffer-overriding-action', `display-buffer-alist', the
6289 ACTION argument, `display-buffer-base-action', and
6290 `display-buffer-fallback-action' (in order). Then it calls each
6291 function in the combined function list in turn, passing the
6292 buffer as the first argument and the combined alist as the second
6293 argument, until one of the functions returns non-nil.
6295 If ACTION is nil, the function list and the alist are built using
6296 only the other variables mentioned above.
6298 Available action functions include:
6299 `display-buffer-same-window'
6300 `display-buffer-reuse-window'
6301 `display-buffer-pop-up-frame'
6302 `display-buffer-pop-up-window'
6303 `display-buffer-in-previous-window'
6304 `display-buffer-use-some-window'
6306 Recognized alist entries include:
6308 `inhibit-same-window' -- A non-nil value prevents the same
6309 window from being used for display.
6311 `inhibit-switch-frame' -- A non-nil value prevents any other
6312 frame from being raised or selected,
6313 even if the window is displayed there.
6315 `reusable-frames' -- Value specifies frame(s) to search for a
6316 window that already displays the buffer.
6317 See `display-buffer-reuse-window'.
6319 `pop-up-frame-parameters' -- Value specifies an alist of frame
6320 parameters to give a new frame, if
6321 one is created.
6323 `window-height' -- Value specifies either an integer (the number
6324 of lines of a new window), a floating point number (the
6325 fraction of a new window with respect to the height of the
6326 frame's root window) or a function to be called with one
6327 argument - a new window. The function is supposed to adjust
6328 the height of the window; its return value is ignored.
6329 Suitable functions are `shrink-window-if-larger-than-buffer'
6330 and `fit-window-to-buffer'.
6332 `window-width' -- Value specifies either an integer (the number
6333 of columns of a new window), a floating point number (the
6334 fraction of a new window with respect to the width of the
6335 frame's root window) or a function to be called with one
6336 argument - a new window. The function is supposed to adjust
6337 the width of the window; its return value is ignored.
6339 `allow-no-window' -- A non-nil value indicates readiness for the case
6340 of not displaying the buffer and FUNCTION can safely return
6341 a non-window value to suppress displaying.
6343 `preserve-size' -- Value should be either '(t . nil)' to
6344 preserve the width of the window, '(nil . t)' to preserve its
6345 height or '(t . t)' to preserve both.
6347 The ACTION argument to `display-buffer' can also have a non-nil
6348 and non-list value. This means to display the buffer in a window
6349 other than the selected one, even if it is already displayed in
6350 the selected window. If called interactively with a prefix
6351 argument, ACTION is t."
6352 (interactive (list (read-buffer "Display buffer: " (other-buffer))
6353 (if current-prefix-arg t)))
6354 (let ((buffer (if (bufferp buffer-or-name)
6355 buffer-or-name
6356 (get-buffer buffer-or-name)))
6357 ;; Make sure that when we split windows the old window keeps
6358 ;; point, bug#14829.
6359 (split-window-keep-point t)
6360 ;; Handle the old form of the first argument.
6361 (inhibit-same-window (and action (not (listp action)))))
6362 (unless (listp action) (setq action nil))
6363 (if display-buffer-function
6364 ;; If `display-buffer-function' is defined, let it do the job.
6365 (funcall display-buffer-function buffer inhibit-same-window)
6366 ;; Otherwise, use the defined actions.
6367 (let* ((user-action
6368 (display-buffer-assq-regexp
6369 (buffer-name buffer) display-buffer-alist action))
6370 (special-action (display-buffer--special-action buffer))
6371 ;; Extra actions from the arguments to this function:
6372 (extra-action
6373 (cons nil (append (if inhibit-same-window
6374 '((inhibit-same-window . t)))
6375 (if frame
6376 `((reusable-frames . ,frame))))))
6377 ;; Construct action function list and action alist.
6378 (actions (list display-buffer-overriding-action
6379 user-action special-action action extra-action
6380 display-buffer-base-action
6381 display-buffer-fallback-action))
6382 (functions (apply 'append
6383 (mapcar (lambda (x)
6384 (setq x (car x))
6385 (if (functionp x) (list x) x))
6386 actions)))
6387 (alist (apply 'append (mapcar 'cdr actions)))
6388 window)
6389 (unless (buffer-live-p buffer)
6390 (error "Invalid buffer"))
6391 (while (and functions (not window))
6392 (setq window (funcall (car functions) buffer alist)
6393 functions (cdr functions)))
6394 (and (windowp window) window)))))
6396 (defun display-buffer-other-frame (buffer)
6397 "Display buffer BUFFER preferably in another frame.
6398 This uses the function `display-buffer' as a subroutine; see
6399 its documentation for additional customization information."
6400 (interactive "BDisplay buffer in other frame: ")
6401 (display-buffer buffer display-buffer--other-frame-action t))
6403 ;;; `display-buffer' action functions:
6405 (defun display-buffer-same-window (buffer alist)
6406 "Display BUFFER in the selected window.
6407 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
6408 if the selected window is a minibuffer window or is dedicated to
6409 another buffer; in that case, return nil. Otherwise, return the
6410 selected window."
6411 (unless (or (cdr (assq 'inhibit-same-window alist))
6412 (window-minibuffer-p)
6413 (window-dedicated-p))
6414 (window--display-buffer buffer (selected-window) 'reuse alist)))
6416 (defun display-buffer--maybe-same-window (buffer alist)
6417 "Conditionally display BUFFER in the selected window.
6418 If `same-window-p' returns non-nil for BUFFER's name, call
6419 `display-buffer-same-window' and return its value. Otherwise,
6420 return nil."
6421 (and (same-window-p (buffer-name buffer))
6422 (display-buffer-same-window buffer alist)))
6424 (defun display-buffer-reuse-window (buffer alist)
6425 "Return a window that is already displaying BUFFER.
6426 Return nil if no usable window is found.
6428 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6429 window is not eligible for reuse.
6431 If ALIST contains a `reusable-frames' entry, its value determines
6432 which frames to search for a reusable window:
6433 nil -- the selected frame (actually the last non-minibuffer frame)
6434 A frame -- just that frame
6435 `visible' -- all visible frames
6436 0 -- all frames on the current terminal
6437 t -- all frames.
6439 If ALIST contains no `reusable-frames' entry, search just the
6440 selected frame if `display-buffer-reuse-frames' and
6441 `pop-up-frames' are both nil; search all frames on the current
6442 terminal if either of those variables is non-nil.
6444 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6445 event that a window on another frame is chosen, avoid raising
6446 that frame."
6447 (let* ((alist-entry (assq 'reusable-frames alist))
6448 (frames (cond (alist-entry (cdr alist-entry))
6449 ((if (eq pop-up-frames 'graphic-only)
6450 (display-graphic-p)
6451 pop-up-frames)
6453 (display-buffer-reuse-frames 0)
6454 (t (last-nonminibuffer-frame))))
6455 (window (if (and (eq buffer (window-buffer))
6456 (not (cdr (assq 'inhibit-same-window alist))))
6457 (selected-window)
6458 (car (delq (selected-window)
6459 (get-buffer-window-list buffer 'nomini
6460 frames))))))
6461 (when (window-live-p window)
6462 (prog1 (window--display-buffer buffer window 'reuse alist)
6463 (unless (cdr (assq 'inhibit-switch-frame alist))
6464 (window--maybe-raise-frame (window-frame window)))))))
6466 (defun display-buffer--special-action (buffer)
6467 "Return special display action for BUFFER, if any.
6468 If `special-display-p' returns non-nil for BUFFER, return an
6469 appropriate display action involving `special-display-function'.
6470 See `display-buffer' for the format of display actions."
6471 (and special-display-function
6472 ;; `special-display-p' returns either t or a list of frame
6473 ;; parameters to pass to `special-display-function'.
6474 (let ((pars (special-display-p (buffer-name buffer))))
6475 (when pars
6476 (list (list #'display-buffer-reuse-window
6477 `(lambda (buffer _alist)
6478 (funcall special-display-function
6479 buffer ',(if (listp pars) pars)))))))))
6481 (defun display-buffer-pop-up-frame (buffer alist)
6482 "Display BUFFER in a new frame.
6483 This works by calling `pop-up-frame-function'. If successful,
6484 return the window used; otherwise return nil.
6486 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
6487 raising the new frame.
6489 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
6490 corresponding value is an alist of frame parameters to give the
6491 new frame."
6492 (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
6493 (pop-up-frame-alist (append params pop-up-frame-alist))
6494 (fun pop-up-frame-function)
6495 frame window)
6496 (when (and fun
6497 ;; Make BUFFER current so `make-frame' will use it as the
6498 ;; new frame's buffer (Bug#15133).
6499 (with-current-buffer buffer
6500 (setq frame (funcall fun)))
6501 (setq window (frame-selected-window frame)))
6502 (prog1 (window--display-buffer
6503 buffer window 'frame alist display-buffer-mark-dedicated)
6504 (unless (cdr (assq 'inhibit-switch-frame alist))
6505 (window--maybe-raise-frame frame))))))
6507 (defun display-buffer-pop-up-window (buffer alist)
6508 "Display BUFFER by popping up a new window.
6509 The new window is created on the selected frame, or in
6510 `last-nonminibuffer-frame' if no windows can be created there.
6511 If successful, return the new window; otherwise return nil.
6513 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6514 event that the new window is created on another frame, avoid
6515 raising the frame."
6516 (let ((frame (or (window--frame-usable-p (selected-frame))
6517 (window--frame-usable-p (last-nonminibuffer-frame))))
6518 window)
6519 (when (and (or (not (frame-parameter frame 'unsplittable))
6520 ;; If the selected frame cannot be split, look at
6521 ;; `last-nonminibuffer-frame'.
6522 (and (eq frame (selected-frame))
6523 (setq frame (last-nonminibuffer-frame))
6524 (window--frame-usable-p frame)
6525 (not (frame-parameter frame 'unsplittable))))
6526 ;; Attempt to split largest or least recently used window.
6527 (setq window (or (window--try-to-split-window
6528 (get-largest-window frame t) alist)
6529 (window--try-to-split-window
6530 (get-lru-window frame t) alist))))
6531 (prog1 (window--display-buffer
6532 buffer window 'window alist display-buffer-mark-dedicated)
6533 (unless (cdr (assq 'inhibit-switch-frame alist))
6534 (window--maybe-raise-frame (window-frame window)))))))
6536 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
6537 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
6539 If `pop-up-frames' is non-nil (and not `graphic-only' on a
6540 text-only terminal), try with `display-buffer-pop-up-frame'.
6542 If that cannot be done, and `pop-up-windows' is non-nil, try
6543 again with `display-buffer-pop-up-window'."
6544 (or (and (if (eq pop-up-frames 'graphic-only)
6545 (display-graphic-p)
6546 pop-up-frames)
6547 (display-buffer-pop-up-frame buffer alist))
6548 (and pop-up-windows
6549 (display-buffer-pop-up-window buffer alist))))
6551 (defun display-buffer-below-selected (buffer alist)
6552 "Try displaying BUFFER in a window below the selected window.
6553 This either splits the selected window or reuses the window below
6554 the selected one."
6555 (let (window)
6556 (or (and (setq window (window-in-direction 'below))
6557 (eq buffer (window-buffer window))
6558 (window--display-buffer buffer window 'reuse alist))
6559 (and (not (frame-parameter nil 'unsplittable))
6560 (let ((split-height-threshold 0)
6561 split-width-threshold)
6562 (setq window (window--try-to-split-window (selected-window) alist)))
6563 (window--display-buffer
6564 buffer window 'window alist display-buffer-mark-dedicated))
6565 (and (setq window (window-in-direction 'below))
6566 (not (window-dedicated-p window))
6567 (window--display-buffer
6568 buffer window 'reuse alist display-buffer-mark-dedicated)))))
6570 (defun display-buffer-at-bottom (buffer alist)
6571 "Try displaying BUFFER in a window at the bottom of the selected frame.
6572 This either reuses such a window provided it shows BUFFER
6573 already, splits a window at the bottom of the frame or the
6574 frame's root window, or reuses some window at the bottom of the
6575 selected frame."
6576 (let (bottom-window bottom-window-shows-buffer window)
6577 (walk-window-tree
6578 (lambda (window)
6579 (cond
6580 ((window-in-direction 'below window))
6581 ((and (not bottom-window-shows-buffer)
6582 (eq buffer (window-buffer window)))
6583 (setq bottom-window-shows-buffer t)
6584 (setq bottom-window window))
6585 ((not bottom-window)
6586 (setq bottom-window window)))
6587 nil nil 'nomini))
6588 (or (and bottom-window-shows-buffer
6589 (window--display-buffer
6590 buffer bottom-window 'reuse alist display-buffer-mark-dedicated))
6591 (and (not (frame-parameter nil 'unsplittable))
6592 (let (split-width-threshold)
6593 (setq window (window--try-to-split-window bottom-window alist)))
6594 (window--display-buffer
6595 buffer window 'window alist display-buffer-mark-dedicated))
6596 (and (not (frame-parameter nil 'unsplittable))
6597 (setq window
6598 (condition-case nil
6599 (split-window (window--major-non-side-window))
6600 (error nil)))
6601 (window--display-buffer
6602 buffer window 'window alist display-buffer-mark-dedicated))
6603 (and (setq window bottom-window)
6604 (not (window-dedicated-p window))
6605 (window--display-buffer
6606 buffer window 'reuse alist display-buffer-mark-dedicated)))))
6608 (defun display-buffer-in-previous-window (buffer alist)
6609 "Display BUFFER in a window previously showing it.
6610 If ALIST has a non-nil `inhibit-same-window' entry, the selected
6611 window is not eligible for reuse.
6613 If ALIST contains a `reusable-frames' entry, its value determines
6614 which frames to search for a reusable window:
6615 nil -- the selected frame (actually the last non-minibuffer frame)
6616 A frame -- just that frame
6617 `visible' -- all visible frames
6618 0 -- all frames on the current terminal
6619 t -- all frames.
6621 If ALIST contains no `reusable-frames' entry, search just the
6622 selected frame if `display-buffer-reuse-frames' and
6623 `pop-up-frames' are both nil; search all frames on the current
6624 terminal if either of those variables is non-nil.
6626 If ALIST has a `previous-window' entry, the window specified by
6627 that entry will override any other window found by the methods
6628 above, even if that window never showed BUFFER before."
6629 (let* ((alist-entry (assq 'reusable-frames alist))
6630 (inhibit-same-window
6631 (cdr (assq 'inhibit-same-window alist)))
6632 (frames (cond
6633 (alist-entry (cdr alist-entry))
6634 ((if (eq pop-up-frames 'graphic-only)
6635 (display-graphic-p)
6636 pop-up-frames)
6638 (display-buffer-reuse-frames 0)
6639 (t (last-nonminibuffer-frame))))
6640 best-window second-best-window window)
6641 ;; Scan windows whether they have shown the buffer recently.
6642 (catch 'best
6643 (dolist (window (window-list-1 (frame-first-window) 'nomini frames))
6644 (when (and (assq buffer (window-prev-buffers window))
6645 (not (window-dedicated-p window)))
6646 (if (eq window (selected-window))
6647 (unless inhibit-same-window
6648 (setq second-best-window window))
6649 (setq best-window window)
6650 (throw 'best t)))))
6651 ;; When ALIST has a `previous-window' entry, that entry may override
6652 ;; anything we found so far.
6653 (when (and (setq window (cdr (assq 'previous-window alist)))
6654 (window-live-p window)
6655 (not (window-dedicated-p window)))
6656 (if (eq window (selected-window))
6657 (unless inhibit-same-window
6658 (setq second-best-window window))
6659 (setq best-window window)))
6660 ;; Return best or second best window found.
6661 (when (setq window (or best-window second-best-window))
6662 (window--display-buffer buffer window 'reuse alist))))
6664 (defun display-buffer-use-some-window (buffer alist)
6665 "Display BUFFER in an existing window.
6666 Search for a usable window, set that window to the buffer, and
6667 return the window. If no suitable window is found, return nil.
6669 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
6670 event that a window in another frame is chosen, avoid raising
6671 that frame."
6672 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
6673 (frame (or (window--frame-usable-p (selected-frame))
6674 (window--frame-usable-p (last-nonminibuffer-frame))))
6675 (window
6676 ;; Reuse an existing window.
6677 (or (get-lru-window frame nil not-this-window)
6678 (let ((window (get-buffer-window buffer 'visible)))
6679 (unless (and not-this-window
6680 (eq window (selected-window)))
6681 window))
6682 (get-largest-window 'visible nil not-this-window)
6683 (let ((window (get-buffer-window buffer 0)))
6684 (unless (and not-this-window
6685 (eq window (selected-window)))
6686 window))
6687 (get-largest-window 0 nil not-this-window)))
6688 (quit-restore (and (window-live-p window)
6689 (window-parameter window 'quit-restore)))
6690 (quad (nth 1 quit-restore)))
6691 (when (window-live-p window)
6692 ;; If the window was used by `display-buffer' before, try to
6693 ;; resize it to its old height but don't signal an error.
6694 (when (and (listp quad)
6695 (integerp (nth 3 quad))
6696 (> (nth 3 quad) (window-total-height window)))
6697 (condition-case nil
6698 (window-resize window (- (nth 3 quad) (window-total-height window)))
6699 (error nil)))
6701 (prog1
6702 (window--display-buffer buffer window 'reuse alist)
6703 (window--even-window-heights window)
6704 (unless (cdr (assq 'inhibit-switch-frame alist))
6705 (window--maybe-raise-frame (window-frame window)))))))
6707 (defun display-buffer-no-window (_buffer alist)
6708 "Display BUFFER in no window.
6709 If ALIST has a non-nil `allow-no-window' entry, then don't display
6710 a window at all. This makes possible to override the default action
6711 and avoid displaying the buffer. It is assumed that when the caller
6712 specifies a non-nil `allow-no-window' then it can handle a nil value
6713 returned from `display-buffer' in this case."
6714 (when (cdr (assq 'allow-no-window alist))
6715 'fail))
6717 ;;; Display + selection commands:
6718 (defun pop-to-buffer (buffer &optional action norecord)
6719 "Select buffer BUFFER in some window, preferably a different one.
6720 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6721 is a string not naming an existent buffer, create a buffer with
6722 that name. If BUFFER is nil, choose some other buffer. Return
6723 the buffer.
6725 This uses `display-buffer' as a subroutine. The optional ACTION
6726 argument is passed to `display-buffer' as its ACTION argument.
6727 See `display-buffer' for more information. ACTION is t if called
6728 interactively with a prefix argument, which means to pop to a
6729 window other than the selected one even if the buffer is already
6730 displayed in the selected window.
6732 If the window to show BUFFER is not on the selected
6733 frame, raise that window's frame and give it input focus.
6735 Optional third arg NORECORD non-nil means do not put this buffer
6736 at the front of the list of recently selected ones."
6737 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
6738 (if current-prefix-arg t)))
6739 (setq buffer (window-normalize-buffer-to-switch-to buffer))
6740 ;; This should be done by `select-window' below.
6741 ;; (set-buffer buffer)
6742 (let* ((old-frame (selected-frame))
6743 (window (display-buffer buffer action))
6744 (frame (window-frame window)))
6745 ;; If we chose another frame, make sure it gets input focus.
6746 (unless (eq frame old-frame)
6747 (select-frame-set-input-focus frame norecord))
6748 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
6749 (select-window window norecord)
6750 buffer))
6752 (defun pop-to-buffer-same-window (buffer &optional norecord)
6753 "Select buffer BUFFER in some window, preferably the same one.
6754 BUFFER may be a buffer, a string (a buffer name), or nil. If it
6755 is a string not naming an existent buffer, create a buffer with
6756 that name. If BUFFER is nil, choose some other buffer. Return
6757 the buffer.
6759 Optional argument NORECORD, if non-nil means do not put this
6760 buffer at the front of the list of recently selected ones.
6762 Unlike `pop-to-buffer', this function prefers using the selected
6763 window over popping up a new window or frame."
6764 (pop-to-buffer buffer display-buffer--same-window-action norecord))
6766 (defun read-buffer-to-switch (prompt)
6767 "Read the name of a buffer to switch to, prompting with PROMPT.
6768 Return the name of the buffer as a string.
6770 This function is intended for the `switch-to-buffer' family of
6771 commands since these need to omit the name of the current buffer
6772 from the list of completions and default values."
6773 (let ((rbts-completion-table (internal-complete-buffer-except)))
6774 (minibuffer-with-setup-hook
6775 (lambda ()
6776 (setq minibuffer-completion-table rbts-completion-table)
6777 ;; Since rbts-completion-table is built dynamically, we
6778 ;; can't just add it to the default value of
6779 ;; icomplete-with-completion-tables, so we add it
6780 ;; here manually.
6781 (if (and (boundp 'icomplete-with-completion-tables)
6782 (listp icomplete-with-completion-tables))
6783 (set (make-local-variable 'icomplete-with-completion-tables)
6784 (cons rbts-completion-table
6785 icomplete-with-completion-tables))))
6786 (read-buffer prompt (other-buffer (current-buffer))
6787 (confirm-nonexistent-file-or-buffer)))))
6789 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
6790 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
6791 If BUFFER-OR-NAME is nil, return the buffer returned by
6792 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
6793 exists, return that buffer. If no such buffer exists, create a
6794 buffer with the name BUFFER-OR-NAME and return that buffer."
6795 (if buffer-or-name
6796 (or (get-buffer buffer-or-name)
6797 (let ((buffer (get-buffer-create buffer-or-name)))
6798 (set-buffer-major-mode buffer)
6799 buffer))
6800 (other-buffer)))
6802 (defcustom switch-to-buffer-preserve-window-point nil
6803 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
6804 If this is nil, `switch-to-buffer' displays the buffer at that
6805 buffer's `point'. If this is `already-displayed', it tries to
6806 display the buffer at its previous position in the selected
6807 window, provided the buffer is currently displayed in some other
6808 window on any visible or iconified frame. If this is t, it
6809 unconditionally tries to display the buffer at its previous
6810 position in the selected window.
6812 This variable is ignored if the buffer is already displayed in
6813 the selected window or never appeared in it before, or if
6814 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
6815 :type '(choice
6816 (const :tag "Never" nil)
6817 (const :tag "If already displayed elsewhere" already-displayed)
6818 (const :tag "Always" t))
6819 :group 'windows
6820 :version "24.3")
6822 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
6823 "Display buffer BUFFER-OR-NAME in the selected window.
6825 WARNING: This is NOT the way to work on another buffer temporarily
6826 within a Lisp program! Use `set-buffer' instead. That avoids
6827 messing with the window-buffer correspondences.
6829 If the selected window cannot display the specified
6830 buffer (e.g. if it is a minibuffer window or strongly dedicated
6831 to another buffer), call `pop-to-buffer' to select the buffer in
6832 another window.
6834 If called interactively, read the buffer name using the
6835 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6836 determines whether to request confirmation before creating a new
6837 buffer.
6839 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
6840 If BUFFER-OR-NAME is a string that does not identify an existing
6841 buffer, create a buffer with that name. If BUFFER-OR-NAME is
6842 nil, switch to the buffer returned by `other-buffer'.
6844 If optional argument NORECORD is non-nil, do not put the buffer
6845 at the front of the buffer list, and do not make the window
6846 displaying it the most recently selected one.
6848 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
6849 must be displayed in the selected window; if that is impossible,
6850 signal an error rather than calling `pop-to-buffer'.
6852 The option `switch-to-buffer-preserve-window-point' can be used
6853 to make the buffer appear at its last position in the selected
6854 window.
6856 Return the buffer switched to."
6857 (interactive
6858 (list (read-buffer-to-switch "Switch to buffer: ") nil 'force-same-window))
6859 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
6860 (cond
6861 ;; Don't call set-window-buffer if it's not needed since it
6862 ;; might signal an error (e.g. if the window is dedicated).
6863 ((eq buffer (window-buffer)))
6864 ((window-minibuffer-p)
6865 (if force-same-window
6866 (user-error "Cannot switch buffers in minibuffer window")
6867 (pop-to-buffer buffer norecord)))
6868 ((eq (window-dedicated-p) t)
6869 (if force-same-window
6870 (user-error "Cannot switch buffers in a dedicated window")
6871 (pop-to-buffer buffer norecord)))
6873 (let* ((entry (assq buffer (window-prev-buffers)))
6874 (displayed (and (eq switch-to-buffer-preserve-window-point
6875 'already-displayed)
6876 (get-buffer-window buffer 0))))
6877 (set-window-buffer nil buffer)
6878 (when (and entry
6879 (or (eq switch-to-buffer-preserve-window-point t)
6880 displayed))
6881 ;; Try to restore start and point of buffer in the selected
6882 ;; window (Bug#4041).
6883 (set-window-start (selected-window) (nth 1 entry) t)
6884 (set-window-point nil (nth 2 entry))))))
6886 (unless norecord
6887 (select-window (selected-window)))
6888 (set-buffer buffer)))
6890 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
6891 "Select the buffer specified by BUFFER-OR-NAME in another window.
6892 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6893 nil. Return the buffer switched to.
6895 If called interactively, prompt for the buffer name using the
6896 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6897 determines whether to request confirmation before creating a new
6898 buffer.
6900 If BUFFER-OR-NAME is a string and does not identify an existing
6901 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6902 nil, switch to the buffer returned by `other-buffer'.
6904 Optional second argument NORECORD non-nil means do not put this
6905 buffer at the front of the list of recently selected ones.
6907 This uses the function `display-buffer' as a subroutine; see its
6908 documentation for additional customization information."
6909 (interactive
6910 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
6911 (let ((pop-up-windows t))
6912 (pop-to-buffer buffer-or-name t norecord)))
6914 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
6915 "Switch to buffer BUFFER-OR-NAME in another frame.
6916 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
6917 nil. Return the buffer switched to.
6919 If called interactively, prompt for the buffer name using the
6920 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
6921 determines whether to request confirmation before creating a new
6922 buffer.
6924 If BUFFER-OR-NAME is a string and does not identify an existing
6925 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
6926 nil, switch to the buffer returned by `other-buffer'.
6928 Optional second arg NORECORD non-nil means do not put this
6929 buffer at the front of the list of recently selected ones.
6931 This uses the function `display-buffer' as a subroutine; see its
6932 documentation for additional customization information."
6933 (interactive
6934 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
6935 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
6937 (defun set-window-text-height (window height)
6938 "Set the height in lines of the text display area of WINDOW to HEIGHT.
6939 WINDOW must be a live window and defaults to the selected one.
6940 HEIGHT doesn't include the mode line or header line, if any, or
6941 any partial-height lines in the text display area.
6943 Note that the current implementation of this function cannot
6944 always set the height exactly, but attempts to be conservative,
6945 by allocating more lines than are actually needed in the case
6946 where some error may be present."
6947 (setq window (window-normalize-window window t))
6948 (let ((delta (- height (window-text-height window))))
6949 (unless (zerop delta)
6950 ;; Setting window-min-height to a value like 1 can lead to very
6951 ;; bizarre displays because it also allows Emacs to make *other*
6952 ;; windows one line tall, which means that there's no more space
6953 ;; for the mode line.
6954 (let ((window-min-height (min 2 height)))
6955 (window-resize window delta)))))
6957 (defun enlarge-window-horizontally (delta)
6958 "Make selected window DELTA columns wider.
6959 Interactively, if no argument is given, make selected window one
6960 column wider."
6961 (interactive "p")
6962 (enlarge-window delta t))
6964 (defun shrink-window-horizontally (delta)
6965 "Make selected window DELTA columns narrower.
6966 Interactively, if no argument is given, make selected window one
6967 column narrower."
6968 (interactive "p")
6969 (shrink-window delta t))
6971 (defun count-screen-lines (&optional beg end count-final-newline window)
6972 "Return the number of screen lines in the region.
6973 The number of screen lines may be different from the number of actual lines,
6974 due to line breaking, display table, etc.
6976 Optional arguments BEG and END default to `point-min' and `point-max'
6977 respectively.
6979 If region ends with a newline, ignore it unless optional third argument
6980 COUNT-FINAL-NEWLINE is non-nil.
6982 The optional fourth argument WINDOW specifies the window used for obtaining
6983 parameters such as width, horizontal scrolling, and so on. The default is
6984 to use the selected window's parameters.
6986 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6987 regardless of which buffer is displayed in WINDOW. This makes possible to use
6988 `count-screen-lines' in any buffer, whether or not it is currently displayed
6989 in some window."
6990 (unless beg
6991 (setq beg (point-min)))
6992 (unless end
6993 (setq end (point-max)))
6994 (if (= beg end)
6996 (save-excursion
6997 (save-restriction
6998 (widen)
6999 (narrow-to-region (min beg end)
7000 (if (and (not count-final-newline)
7001 (= ?\n (char-before (max beg end))))
7002 (1- (max beg end))
7003 (max beg end)))
7004 (goto-char (point-min))
7005 (1+ (vertical-motion (buffer-size) window))))))
7007 (defun window-buffer-height (window)
7008 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
7009 WINDOW must be a live window and defaults to the selected one."
7010 (setq window (window-normalize-window window t))
7011 (with-current-buffer (window-buffer window)
7012 (max 1
7013 (count-screen-lines (point-min) (point-max)
7014 ;; If buffer ends with a newline, ignore it when
7015 ;; counting height unless point is after it.
7016 (eobp)
7017 window))))
7019 ;;; Resizing windows and frames to fit their contents exactly.
7020 (defcustom fit-window-to-buffer-horizontally nil
7021 "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
7022 If this is nil, `fit-window-to-buffer' never resizes windows
7023 horizontally. If this is `only', it can resize windows
7024 horizontally only. Any other value means `fit-window-to-buffer'
7025 can resize windows in both dimensions."
7026 :type 'boolean
7027 :version "24.4"
7028 :group 'help)
7030 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
7031 ;; counting title bar and outer borders.
7032 (defcustom fit-frame-to-buffer nil
7033 "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
7034 A frame is fit if and only if its root window is a live window
7035 and this option is non-nil. If this is `horizontally', frames
7036 are resized horizontally only. If this is `vertically', frames
7037 are resized vertically only. Any other non-nil value means
7038 frames can be resized in both dimensions."
7039 :type 'boolean
7040 :version "24.4"
7041 :group 'help)
7043 (defcustom fit-frame-to-buffer-margins '(nil nil nil nil)
7044 "Margins around frame for `fit-frame-to-buffer'.
7045 This specifies the numbers of pixels to be left free on the left,
7046 above, on the right, and below a frame fitted to its buffer. Set
7047 this to avoid obscuring other desktop objects like the taskbar.
7048 The default is nil for each side, which means to not add margins.
7050 The value specified here can be overridden for a specific frame
7051 by that frame's `fit-frame-to-buffer-margins' parameter, if
7052 present. See also `fit-frame-to-buffer-sizes'."
7053 :version "24.4"
7054 :type '(list
7055 (choice
7056 :tag "Left"
7057 :value nil
7058 :format "%[LeftMargin%] %v "
7059 (const :tag "None" :format "%t" nil)
7060 (integer :tag "Pixels" :size 5))
7061 (choice
7062 :tag "Top"
7063 :value nil
7064 :format "%[TopMargin%] %v "
7065 (const :tag "None" :format "%t" nil)
7066 (integer :tag "Pixels" :size 5))
7067 (choice
7068 :tag "Right"
7069 :value nil
7070 :format "%[RightMargin%] %v "
7071 (const :tag "None" :format "%t" nil)
7072 (integer :tag "Pixels" :size 5))
7073 (choice
7074 :tag "Bottom"
7075 :value nil
7076 :format "%[BottomMargin%] %v "
7077 (const :tag "None" :format "%t" nil)
7078 (integer :tag "Pixels" :size 5)))
7079 :group 'help)
7081 (defcustom fit-frame-to-buffer-sizes '(nil nil nil nil)
7082 "Size boundaries of frame for `fit-frame-to-buffer'.
7083 This list specifies the total maximum and minimum lines and
7084 maximum and minimum columns of the root window of any frame that
7085 shall be fit to its buffer. If any of these values is non-nil,
7086 it overrides the corresponding argument of `fit-frame-to-buffer'.
7088 On window systems where the menubar can wrap, fitting a frame to
7089 its buffer may swallow the last line(s). Specifying an
7090 appropriate minimum width value here can avoid such wrapping.
7092 See also `fit-frame-to-buffer-margins'."
7093 :version "24.4"
7094 :type '(list
7095 (choice
7096 :tag "Maximum Height"
7097 :value nil
7098 :format "%[MaxHeight%] %v "
7099 (const :tag "None" :format "%t" nil)
7100 (integer :tag "Lines" :size 5))
7101 (choice
7102 :tag "Minimum Height"
7103 :value nil
7104 :format "%[MinHeight%] %v "
7105 (const :tag "None" :format "%t" nil)
7106 (integer :tag "Lines" :size 5))
7107 (choice
7108 :tag "Maximum Width"
7109 :value nil
7110 :format "%[MaxWidth%] %v "
7111 (const :tag "None" :format "%t" nil)
7112 (integer :tag "Columns" :size 5))
7113 (choice
7114 :tag "Minimum Width"
7115 :value nil
7116 :format "%[MinWidth%] %v\n"
7117 (const :tag "None" :format "%t" nil)
7118 (integer :tag "Columns" :size 5)))
7119 :group 'help)
7121 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
7123 (defun window--sanitize-margin (margin left right)
7124 "Return MARGIN if it's a number between LEFT and RIGHT."
7125 (when (and (numberp margin)
7126 (<= left (- right margin)) (<= margin right))
7127 margin))
7129 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only)
7130 "Adjust size of FRAME to display the contents of its buffer exactly.
7131 FRAME can be any live frame and defaults to the selected one.
7132 Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
7133 MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
7134 FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
7135 `window-min-height' and `window-min-width' respectively.
7137 If the optional argument ONLY is `vertically', resize the frame
7138 vertically only. If ONLY is `horizontally', resize the frame
7139 horizontally only.
7141 The new position and size of FRAME can be additionally determined
7142 by customizing the options `fit-frame-to-buffer-sizes' and
7143 `fit-frame-to-buffer-margins' or the corresponding parameters of
7144 FRAME."
7145 (interactive)
7146 (unless (and (fboundp 'x-display-pixel-height)
7147 ;; We need the respective sizes now.
7148 (fboundp 'display-monitor-attributes-list))
7149 (user-error "Cannot resize frame in non-graphic Emacs"))
7150 (setq frame (window-normalize-frame frame))
7151 (when (window-live-p (frame-root-window frame))
7152 (with-selected-window (frame-root-window frame)
7153 (let* ((window (frame-root-window frame))
7154 (char-width (frame-char-width))
7155 (char-height (frame-char-height))
7156 (monitor-attributes (car (display-monitor-attributes-list
7157 (frame-parameter frame 'display))))
7158 (geometry (cdr (assq 'geometry monitor-attributes)))
7159 (display-width (- (nth 2 geometry) (nth 0 geometry)))
7160 (display-height (- (nth 3 geometry) (nth 1 geometry)))
7161 (workarea (cdr (assq 'workarea monitor-attributes)))
7162 ;; Handle margins.
7163 (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins)
7164 fit-frame-to-buffer-margins))
7165 (left-margin (if (nth 0 margins)
7166 (or (window--sanitize-margin
7167 (nth 0 margins) 0 display-width)
7169 (nth 0 workarea)))
7170 (top-margin (if (nth 1 margins)
7171 (or (window--sanitize-margin
7172 (nth 1 margins) 0 display-height)
7174 (nth 1 workarea)))
7175 (workarea-width (nth 2 workarea))
7176 (right-margin (if (nth 2 margins)
7177 (- display-width
7178 (or (window--sanitize-margin
7179 (nth 2 margins) left-margin display-width)
7181 (nth 2 workarea)))
7182 (workarea-height (nth 3 workarea))
7183 (bottom-margin (if (nth 3 margins)
7184 (- display-height
7185 (or (window--sanitize-margin
7186 (nth 3 margins) top-margin display-height)
7188 (nth 3 workarea)))
7189 ;; The pixel width of FRAME (which does not include the
7190 ;; window manager's decorations).
7191 (frame-width (frame-pixel-width))
7192 ;; The pixel width of the body of FRAME's root window.
7193 (window-body-width (window-body-width nil t))
7194 ;; The difference in pixels between total and body width of
7195 ;; FRAME's window.
7196 (window-extra-width (- (window-pixel-width) window-body-width))
7197 ;; The difference in pixels between the frame's pixel width
7198 ;; and the window's body width. This is the space we can't
7199 ;; use for fitting.
7200 (extra-width (- frame-width window-body-width))
7201 ;; The maximum width we can use for fitting.
7202 (fit-width (- workarea-width extra-width))
7203 ;; The pixel position of FRAME's left border. We usually
7204 ;; try to leave this alone.
7205 (left
7206 (let ((left (frame-parameter nil 'left)))
7207 (if (consp left)
7208 (funcall (car left) (cadr left))
7209 left)))
7210 ;; The pixel height of FRAME (which does not include title
7211 ;; line, decorations, and sometimes neither the menu nor
7212 ;; the toolbar).
7213 (frame-height (frame-pixel-height))
7214 ;; The pixel height of FRAME's root window (we don't care
7215 ;; about the window's body height since the return value of
7216 ;; `window-text-pixel-size' includes header and mode line).
7217 (window-height (window-pixel-height))
7218 ;; The difference in pixels between the frame's pixel
7219 ;; height and the window's height.
7220 (extra-height (- frame-height window-height))
7221 ;; When tool-bar-mode is enabled and we just created a new
7222 ;; frame, reserve lines for toolbar resizing. Needed
7223 ;; because for reasons unknown to me Emacs (1) reserves one
7224 ;; line for the toolbar when making the initial frame and
7225 ;; toolbars are enabled, and (2) later adds the remaining
7226 ;; lines needed. Our code runs IN BETWEEN (1) and (2).
7227 ;; YMMV when you're on a system that behaves differently.
7228 (toolbar-extra-height
7229 (let ((quit-restore (window-parameter window 'quit-restore))
7230 ;; This may have to change when we allow arbitrary
7231 ;; pixel height toolbars.
7232 (lines (tool-bar-height)))
7233 (* char-height
7234 (if (and quit-restore (eq (car quit-restore) 'frame)
7235 (not (zerop lines)))
7236 (1- lines)
7237 0))))
7238 ;; The pixel position of FRAME's top border.
7239 (top
7240 (let ((top (frame-parameter nil 'top)))
7241 (if (consp top)
7242 (funcall (car top) (cadr top))
7243 top)))
7244 ;; Sanitize minimum and maximum sizes.
7245 (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes)
7246 fit-frame-to-buffer-sizes))
7247 (max-height
7248 (cond
7249 ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height))
7250 ((numberp max-height) (* max-height char-height))
7251 (t display-height)))
7252 (min-height
7253 (cond
7254 ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height))
7255 ((numberp min-height) (* min-height char-height))
7256 (t (* window-min-height char-height))))
7257 (max-width
7258 (cond
7259 ((numberp (nth 2 sizes))
7260 (- (* (nth 2 sizes) char-width) window-extra-width))
7261 ((numberp max-width)
7262 (- (* max-width char-width) window-extra-width))
7263 (t display-width)))
7264 (min-width
7265 (cond
7266 ((numberp (nth 3 sizes))
7267 (- (* (nth 3 sizes) char-width) window-extra-width))
7268 ((numberp min-width)
7269 (- (* min-width char-width) window-extra-width))
7270 (t (* window-min-width char-width))))
7271 ;; Note: Currently, for a new frame the sizes of the header
7272 ;; and mode line may be estimated incorrectly
7273 (value (window-text-pixel-size
7274 nil t t workarea-width workarea-height t))
7275 (width (+ (car value) (window-right-divider-width)))
7276 (height
7277 (+ (cdr value)
7278 (window-bottom-divider-width)
7279 (window-scroll-bar-height))))
7280 ;; Don't change height or width when the window's size is fixed
7281 ;; in either direction or ONLY forbids it.
7282 (cond
7283 ((or (eq window-size-fixed 'width) (eq only 'vertically))
7284 (setq width nil))
7285 ((or (eq window-size-fixed 'height) (eq only 'horizontally))
7286 (setq height nil)))
7287 ;; Fit width to constraints.
7288 (when width
7289 (unless frame-resize-pixelwise
7290 ;; Round to character sizes.
7291 (setq width (* (/ (+ width char-width -1) char-width)
7292 char-width)))
7293 ;; Fit to maximum and minimum widths.
7294 (setq width (max (min width max-width) min-width))
7295 ;; Add extra width.
7296 (setq width (+ width extra-width))
7297 ;; Preserve margins.
7298 (let ((right (+ left width)))
7299 (cond
7300 ((> right right-margin)
7301 ;; Move frame to left (we don't know its real width).
7302 (setq left (max left-margin (- left (- right right-margin)))))
7303 ((< left left-margin)
7304 ;; Move frame to right.
7305 (setq left left-margin)))))
7306 ;; Fit height to constraints.
7307 (when height
7308 (unless frame-resize-pixelwise
7309 (setq height (* (/ (+ height char-height -1) char-height)
7310 char-height)))
7311 ;; Fit to maximum and minimum heights.
7312 (setq height (max (min height max-height) min-height))
7313 ;; Add extra height.
7314 (setq height (+ height extra-height))
7315 ;; Preserve margins.
7316 (let ((bottom (+ top height)))
7317 (cond
7318 ((> bottom bottom-margin)
7319 ;; Move frame up (we don't know its real height).
7320 (setq top (max top-margin (- top (- bottom bottom-margin)))))
7321 ((< top top-margin)
7322 ;; Move frame down.
7323 (setq top top-margin)))))
7324 ;; Apply changes.
7325 (set-frame-position frame left top)
7326 ;; Clumsily try to translate our calculations to what
7327 ;; `set-frame-size' wants.
7328 (when width
7329 (setq width (- (+ (frame-text-width) width)
7330 extra-width window-body-width)))
7331 (when height
7332 (setq height (- (+ (frame-text-height) height)
7333 extra-height window-height)))
7334 (set-frame-size
7335 frame
7336 (if width
7337 (if frame-resize-pixelwise
7338 width
7339 (/ width char-width))
7340 (frame-text-width))
7341 (if height
7342 (if frame-resize-pixelwise
7343 height
7344 (/ height char-height))
7345 (frame-text-height))
7346 frame-resize-pixelwise)))))
7348 (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width preserve-size)
7349 "Adjust size of WINDOW to display its buffer's contents exactly.
7350 WINDOW must be a live window and defaults to the selected one.
7352 If WINDOW is part of a vertical combination, adjust WINDOW's
7353 height. The new height is calculated from the actual height of
7354 the accessible portion of its buffer. The optional argument
7355 MAX-HEIGHT specifies a maximum height and defaults to the height
7356 of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
7357 minimum height and defaults to `window-min-height'. Both
7358 MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
7359 and header line and a bottom divider, if any.
7361 If WINDOW is part of a horizontal combination and the value of
7362 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
7363 WINDOW's width. The new width of WINDOW is calculated from the
7364 maximum length of its buffer's lines that follow the current
7365 start position of WINDOW. The optional argument MAX-WIDTH
7366 specifies a maximum width and defaults to the width of WINDOW's
7367 frame. The optional argument MIN-WIDTH specifies a minimum width
7368 and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
7369 are specified in columns and include fringes, margins, a
7370 scrollbar and a vertical divider, if any.
7372 If the optional argument `preserve-size' is non-nil, preserve the
7373 size of WINDOW (see `window-preserve-size').
7375 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
7376 If WINDOW is its frame's root window and the option
7377 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
7378 adjust the frame's size.
7380 Note that even if this function makes WINDOW large enough to show
7381 _all_ parts of its buffer you might not see the first part when
7382 WINDOW was scrolled. If WINDOW is resized horizontally, you will
7383 not see the top of its buffer unless WINDOW starts at its minimum
7384 accessible position."
7385 (interactive)
7386 (setq window (window-normalize-window window t))
7387 (if (eq window (frame-root-window window))
7388 (when fit-frame-to-buffer
7389 ;; Fit WINDOW's frame to buffer.
7390 (fit-frame-to-buffer
7391 (window-frame window)
7392 max-height min-height max-width min-width
7393 (and (memq fit-frame-to-buffer '(vertically horizontally))
7394 fit-frame-to-buffer)))
7395 (with-selected-window window
7396 (let* ((pixelwise window-resize-pixelwise)
7397 (char-height (frame-char-height))
7398 (char-width (frame-char-width))
7399 (total-height (window-size window nil pixelwise))
7400 (body-height (window-body-height window pixelwise))
7401 (body-width (window-body-width window pixelwise))
7402 (min-height
7403 ;; Sanitize MIN-HEIGHT.
7404 (if (numberp min-height)
7405 ;; Can't get smaller than `window-safe-min-height'.
7406 (max (if pixelwise
7407 (* char-height min-height)
7408 min-height)
7409 (if pixelwise
7410 (window-safe-min-pixel-height window)
7411 window-safe-min-height))
7412 ;; Preserve header and mode line if present.
7413 (max (if pixelwise
7414 (* char-height window-min-height)
7415 window-min-height)
7416 (window-min-size window nil window pixelwise))))
7417 (max-height
7418 ;; Sanitize MAX-HEIGHT.
7419 (if (numberp max-height)
7420 (min
7421 (+ total-height
7422 (window-max-delta
7423 window nil window nil nil nil pixelwise))
7424 (if pixelwise
7425 (* char-height max-height)
7426 max-height))
7427 (+ total-height (window-max-delta
7428 window nil window nil nil nil pixelwise))))
7429 height)
7430 (cond
7431 ;; If WINDOW is vertically combined, try to resize it
7432 ;; vertically.
7433 ((and (not (eq fit-window-to-buffer-horizontally 'only))
7434 (not (window-size-fixed-p window 'preserved))
7435 (window-combined-p))
7436 ;; Vertically we always want to fit the entire buffer.
7437 ;; WINDOW'S height can't get larger than its frame's pixel
7438 ;; height. Its width remains fixed.
7439 (setq height (+ (cdr (window-text-pixel-size
7440 nil nil t nil (frame-pixel-height) t))
7441 (window-scroll-bar-height window)
7442 (window-bottom-divider-width)))
7443 ;; Round height.
7444 (unless pixelwise
7445 (setq height (/ (+ height char-height -1) char-height)))
7446 (unless (= height total-height)
7447 (window-preserve-size window)
7448 (window-resize-no-error
7449 window
7450 (- (max min-height (min max-height height)) total-height)
7451 nil window pixelwise)
7452 (when preserve-size
7453 (window-preserve-size window nil t))))
7454 ;; If WINDOW is horizontally combined, try to resize it
7455 ;; horizontally.
7456 ((and fit-window-to-buffer-horizontally
7457 (not (window-size-fixed-p window t 'preserved))
7458 (window-combined-p nil t))
7459 (let* ((total-width (window-size window t pixelwise))
7460 (min-width
7461 ;; Sanitize MIN-WIDTH.
7462 (if (numberp min-width)
7463 ;; Can't get smaller than `window-safe-min-width'.
7464 (max (if pixelwise
7465 (* char-width min-width)
7466 min-width)
7467 (if pixelwise
7468 (window-safe-min-pixel-width)
7469 window-safe-min-width))
7470 ;; Preserve fringes, margins, scrollbars if present.
7471 (max (if pixelwise
7472 (* char-width window-min-width)
7473 window-min-width)
7474 (window-min-size nil nil window pixelwise))))
7475 (max-width
7476 ;; Sanitize MAX-WIDTH.
7477 (if (numberp max-width)
7478 (min (+ total-width
7479 (window-max-delta
7480 window t window nil nil nil pixelwise))
7481 (if pixelwise
7482 (* char-width max-width)
7483 max-width))
7484 (+ total-width (window-max-delta
7485 window t window nil nil nil pixelwise))))
7486 ;; When fitting horizontally, assume that WINDOW's
7487 ;; start position remains unaltered. WINDOW can't get
7488 ;; wider than its frame's pixel width, its height
7489 ;; remains unaltered.
7490 (width (+ (car (window-text-pixel-size
7491 nil (window-start) (point-max)
7492 (frame-pixel-width)
7493 ;; Add one char-height to assure that
7494 ;; we're on the safe side. This
7495 ;; overshoots when the first line below
7496 ;; the bottom is wider than the window.
7497 (* body-height
7498 (if pixelwise 1 char-height))))
7499 (window-right-divider-width))))
7500 (unless pixelwise
7501 (setq width (/ (+ width char-width -1) char-width)))
7502 (unless (= width body-width)
7503 (window-preserve-size window t)
7504 (window-resize-no-error
7505 window
7506 (- (max min-width
7507 (min max-width
7508 (+ total-width (- width body-width))))
7509 total-width)
7510 t window pixelwise)
7511 (when preserve-size
7512 (window-preserve-size window t t))))))))))
7514 (defun window-safely-shrinkable-p (&optional window)
7515 "Return t if WINDOW can be shrunk without shrinking other windows.
7516 WINDOW defaults to the selected window."
7517 (with-selected-window (or window (selected-window))
7518 (let ((edges (window-edges)))
7519 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
7520 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
7522 (defun shrink-window-if-larger-than-buffer (&optional window)
7523 "Shrink height of WINDOW if its buffer doesn't need so many lines.
7524 More precisely, shrink WINDOW vertically to be as small as
7525 possible, while still showing the full contents of its buffer.
7526 WINDOW must be a live window and defaults to the selected one.
7528 Do not shrink WINDOW to less than `window-min-height' lines. Do
7529 nothing if the buffer contains more lines than the present window
7530 height, or if some of the window's contents are scrolled out of
7531 view, or if shrinking this window would also shrink another
7532 window, or if the window is the only window of its frame.
7534 Return non-nil if the window was shrunk, nil otherwise."
7535 (interactive)
7536 (setq window (window-normalize-window window t))
7537 ;; Make sure that WINDOW is vertically combined and `point-min' is
7538 ;; visible (for whatever reason that's needed). The remaining issues
7539 ;; should be taken care of by `fit-window-to-buffer'.
7540 (when (and (window-combined-p window)
7541 (pos-visible-in-window-p (point-min) window))
7542 (fit-window-to-buffer window (window-total-height window))))
7544 (defun kill-buffer-and-window ()
7545 "Kill the current buffer and delete the selected window."
7546 (interactive)
7547 (let ((window-to-delete (selected-window))
7548 (buffer-to-kill (current-buffer))
7549 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
7550 (unwind-protect
7551 (progn
7552 (add-hook 'kill-buffer-hook delete-window-hook t t)
7553 (if (kill-buffer (current-buffer))
7554 ;; If `delete-window' failed before, we rerun it to regenerate
7555 ;; the error so it can be seen in the echo area.
7556 (when (eq (selected-window) window-to-delete)
7557 (delete-window))))
7558 ;; If the buffer is not dead for some reason (probably because
7559 ;; of a `quit' signal), remove the hook again.
7560 (ignore-errors
7561 (with-current-buffer buffer-to-kill
7562 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
7565 (defvar recenter-last-op nil
7566 "Indicates the last recenter operation performed.
7567 Possible values: `top', `middle', `bottom', integer or float numbers.")
7569 (defcustom recenter-positions '(middle top bottom)
7570 "Cycling order for `recenter-top-bottom'.
7571 A list of elements with possible values `top', `middle', `bottom',
7572 integer or float numbers that define the cycling order for
7573 the command `recenter-top-bottom'.
7575 Top and bottom destinations are `scroll-margin' lines from the true
7576 window top and bottom. Middle redraws the frame and centers point
7577 vertically within the window. Integer number moves current line to
7578 the specified absolute window-line. Float number between 0.0 and 1.0
7579 means the percentage of the screen space from the top. The default
7580 cycling order is middle -> top -> bottom."
7581 :type '(repeat (choice
7582 (const :tag "Top" top)
7583 (const :tag "Middle" middle)
7584 (const :tag "Bottom" bottom)
7585 (integer :tag "Line number")
7586 (float :tag "Percentage")))
7587 :version "23.2"
7588 :group 'windows)
7590 (defun recenter-top-bottom (&optional arg)
7591 "Move current buffer line to the specified window line.
7592 With no prefix argument, successive calls place point according
7593 to the cycling order defined by `recenter-positions'.
7595 A prefix argument is handled like `recenter':
7596 With numeric prefix ARG, move current line to window-line ARG.
7597 With plain `C-u', move current line to window center."
7598 (interactive "P")
7599 (cond
7600 (arg (recenter arg)) ; Always respect ARG.
7602 (setq recenter-last-op
7603 (if (eq this-command last-command)
7604 (car (or (cdr (member recenter-last-op recenter-positions))
7605 recenter-positions))
7606 (car recenter-positions)))
7607 (let ((this-scroll-margin
7608 (min (max 0 scroll-margin)
7609 (truncate (/ (window-body-height) 4.0)))))
7610 (cond ((eq recenter-last-op 'middle)
7611 (recenter))
7612 ((eq recenter-last-op 'top)
7613 (recenter this-scroll-margin))
7614 ((eq recenter-last-op 'bottom)
7615 (recenter (- -1 this-scroll-margin)))
7616 ((integerp recenter-last-op)
7617 (recenter recenter-last-op))
7618 ((floatp recenter-last-op)
7619 (recenter (round (* recenter-last-op (window-height))))))))))
7621 (define-key global-map [?\C-l] 'recenter-top-bottom)
7623 (defun move-to-window-line-top-bottom (&optional arg)
7624 "Position point relative to window.
7626 With a prefix argument ARG, acts like `move-to-window-line'.
7628 With no argument, positions point at center of window.
7629 Successive calls position point at positions defined
7630 by `recenter-positions'."
7631 (interactive "P")
7632 (cond
7633 (arg (move-to-window-line arg)) ; Always respect ARG.
7635 (setq recenter-last-op
7636 (if (eq this-command last-command)
7637 (car (or (cdr (member recenter-last-op recenter-positions))
7638 recenter-positions))
7639 (car recenter-positions)))
7640 (let ((this-scroll-margin
7641 (min (max 0 scroll-margin)
7642 (truncate (/ (window-body-height) 4.0)))))
7643 (cond ((eq recenter-last-op 'middle)
7644 (call-interactively 'move-to-window-line))
7645 ((eq recenter-last-op 'top)
7646 (move-to-window-line this-scroll-margin))
7647 ((eq recenter-last-op 'bottom)
7648 (move-to-window-line (- -1 this-scroll-margin)))
7649 ((integerp recenter-last-op)
7650 (move-to-window-line recenter-last-op))
7651 ((floatp recenter-last-op)
7652 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
7654 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
7656 ;;; Scrolling commands.
7658 ;;; Scrolling commands which do not signal errors at top/bottom
7659 ;;; of buffer at first key-press (instead move to top/bottom
7660 ;;; of buffer).
7662 (defcustom scroll-error-top-bottom nil
7663 "Move point to top/bottom of buffer before signaling a scrolling error.
7664 A value of nil means just signal an error if no more scrolling possible.
7665 A value of t means point moves to the beginning or the end of the buffer
7666 \(depending on scrolling direction) when no more scrolling possible.
7667 When point is already on that position, then signal an error."
7668 :type 'boolean
7669 :group 'windows
7670 :version "24.1")
7672 (defun scroll-up-command (&optional arg)
7673 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
7674 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
7675 scroll window further, move cursor to the bottom line.
7676 When point is already on that position, then signal an error.
7677 A near full screen is `next-screen-context-lines' less than a full screen.
7678 Negative ARG means scroll downward.
7679 If ARG is the atom `-', scroll downward by nearly full screen."
7680 (interactive "^P")
7681 (cond
7682 ((null scroll-error-top-bottom)
7683 (scroll-up arg))
7684 ((eq arg '-)
7685 (scroll-down-command nil))
7686 ((< (prefix-numeric-value arg) 0)
7687 (scroll-down-command (- (prefix-numeric-value arg))))
7688 ((eobp)
7689 (scroll-up arg)) ; signal error
7691 (condition-case nil
7692 (scroll-up arg)
7693 (end-of-buffer
7694 (if arg
7695 ;; When scrolling by ARG lines can't be done,
7696 ;; move by ARG lines instead.
7697 (forward-line arg)
7698 ;; When ARG is nil for full-screen scrolling,
7699 ;; move to the bottom of the buffer.
7700 (goto-char (point-max))))))))
7702 (put 'scroll-up-command 'scroll-command t)
7704 (defun scroll-down-command (&optional arg)
7705 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
7706 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
7707 scroll window further, move cursor to the top line.
7708 When point is already on that position, then signal an error.
7709 A near full screen is `next-screen-context-lines' less than a full screen.
7710 Negative ARG means scroll upward.
7711 If ARG is the atom `-', scroll upward by nearly full screen."
7712 (interactive "^P")
7713 (cond
7714 ((null scroll-error-top-bottom)
7715 (scroll-down arg))
7716 ((eq arg '-)
7717 (scroll-up-command nil))
7718 ((< (prefix-numeric-value arg) 0)
7719 (scroll-up-command (- (prefix-numeric-value arg))))
7720 ((bobp)
7721 (scroll-down arg)) ; signal error
7723 (condition-case nil
7724 (scroll-down arg)
7725 (beginning-of-buffer
7726 (if arg
7727 ;; When scrolling by ARG lines can't be done,
7728 ;; move by ARG lines instead.
7729 (forward-line (- arg))
7730 ;; When ARG is nil for full-screen scrolling,
7731 ;; move to the top of the buffer.
7732 (goto-char (point-min))))))))
7734 (put 'scroll-down-command 'scroll-command t)
7736 ;;; Scrolling commands which scroll a line instead of full screen.
7738 (defun scroll-up-line (&optional arg)
7739 "Scroll text of selected window upward ARG lines; or one line if no ARG.
7740 If ARG is omitted or nil, scroll upward by one line.
7741 This is different from `scroll-up-command' that scrolls a full screen."
7742 (interactive "p")
7743 (scroll-up (or arg 1)))
7745 (put 'scroll-up-line 'scroll-command t)
7747 (defun scroll-down-line (&optional arg)
7748 "Scroll text of selected window down ARG lines; or one line if no ARG.
7749 If ARG is omitted or nil, scroll down by one line.
7750 This is different from `scroll-down-command' that scrolls a full screen."
7751 (interactive "p")
7752 (scroll-down (or arg 1)))
7754 (put 'scroll-down-line 'scroll-command t)
7757 (defun scroll-other-window-down (&optional lines)
7758 "Scroll the \"other window\" down.
7759 For more details, see the documentation for `scroll-other-window'."
7760 (interactive "P")
7761 (scroll-other-window
7762 ;; Just invert the argument's meaning.
7763 ;; We can do that without knowing which window it will be.
7764 (if (eq lines '-) nil
7765 (if (null lines) '-
7766 (- (prefix-numeric-value lines))))))
7768 (defun beginning-of-buffer-other-window (arg)
7769 "Move point to the beginning of the buffer in the other window.
7770 Leave mark at previous position.
7771 With arg N, put point N/10 of the way from the true beginning."
7772 (interactive "P")
7773 (let ((orig-window (selected-window))
7774 (window (other-window-for-scrolling)))
7775 ;; We use unwind-protect rather than save-window-excursion
7776 ;; because the latter would preserve the things we want to change.
7777 (unwind-protect
7778 (progn
7779 (select-window window)
7780 ;; Set point and mark in that window's buffer.
7781 (with-no-warnings
7782 (beginning-of-buffer arg))
7783 ;; Set point accordingly.
7784 (recenter '(t)))
7785 (select-window orig-window))))
7787 (defun end-of-buffer-other-window (arg)
7788 "Move point to the end of the buffer in the other window.
7789 Leave mark at previous position.
7790 With arg N, put point N/10 of the way from the true end."
7791 (interactive "P")
7792 ;; See beginning-of-buffer-other-window for comments.
7793 (let ((orig-window (selected-window))
7794 (window (other-window-for-scrolling)))
7795 (unwind-protect
7796 (progn
7797 (select-window window)
7798 (with-no-warnings
7799 (end-of-buffer arg))
7800 (recenter '(t)))
7801 (select-window orig-window))))
7803 (defvar mouse-autoselect-window-timer nil
7804 "Timer used by delayed window autoselection.")
7806 (defvar mouse-autoselect-window-position-1 nil
7807 "First mouse position recorded by delayed window autoselection.")
7809 (defvar mouse-autoselect-window-position nil
7810 "Last mouse position recorded by delayed window autoselection.")
7812 (defvar mouse-autoselect-window-window nil
7813 "Last window recorded by delayed window autoselection.")
7815 (defvar mouse-autoselect-window-state nil
7816 "When non-nil, special state of delayed window autoselection.
7817 Possible values are `suspend' (suspend autoselection after a menu or
7818 scrollbar interaction) and `select' (the next invocation of
7819 `handle-select-window' shall select the window immediately).")
7821 (defun mouse-autoselect-window-cancel (&optional force)
7822 "Cancel delayed window autoselection.
7823 Optional argument FORCE means cancel unconditionally."
7824 (unless (and (not force)
7825 ;; Don't cancel for select-window or select-frame events
7826 ;; or when the user drags a scroll bar.
7827 (or (memq this-command
7828 '(handle-select-window handle-switch-frame))
7829 (and (eq this-command 'scroll-bar-toolkit-scroll)
7830 (memq (nth 4 (event-end last-input-event))
7831 '(handle end-scroll)))))
7832 (setq mouse-autoselect-window-state nil)
7833 (setq mouse-autoselect-window-position-1 nil)
7834 (when (timerp mouse-autoselect-window-timer)
7835 (cancel-timer mouse-autoselect-window-timer))
7836 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
7838 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
7839 "Start delayed window autoselection.
7840 MOUSE-POSITION is the last position where the mouse was seen as returned
7841 by `mouse-position'. Optional argument WINDOW non-nil denotes the
7842 window where the mouse was seen. Optional argument SUSPEND non-nil
7843 means suspend autoselection."
7844 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
7845 (setq mouse-autoselect-window-position mouse-position)
7846 (when window (setq mouse-autoselect-window-window window))
7847 (setq mouse-autoselect-window-state (when suspend 'suspend))
7848 ;; Install timer which runs `mouse-autoselect-window-select' after
7849 ;; `mouse-autoselect-window' seconds.
7850 (setq mouse-autoselect-window-timer
7851 (run-at-time
7852 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
7854 (defun mouse-autoselect-window-select ()
7855 "Select window with delayed window autoselection.
7856 If the mouse position has stabilized in a non-selected window, select
7857 that window. The minibuffer window is selected only if the minibuffer
7858 is active. This function is run by `mouse-autoselect-window-timer'."
7859 (ignore-errors
7860 (let* ((mouse-position (mouse-position))
7861 (window
7862 (ignore-errors
7863 (window-at (cadr mouse-position) (cddr mouse-position)
7864 (car mouse-position)))))
7865 (cond
7866 ((or (and (fboundp 'menu-or-popup-active-p) (menu-or-popup-active-p))
7867 (and window
7868 (let ((coords (coordinates-in-window-p
7869 (cdr mouse-position) window)))
7870 (and (not (consp coords))
7871 (not (memq coords '(left-margin right-margin)))))))
7872 ;; A menu / popup dialog is active or the mouse is not on the
7873 ;; text region of WINDOW: Suspend autoselection temporarily.
7874 (mouse-autoselect-window-start mouse-position nil t))
7875 ((or (eq mouse-autoselect-window-state 'suspend)
7876 ;; When the mouse is at its first recorded position, restart
7877 ;; delayed autoselection. This works around a scenario with
7878 ;; two two-window frames with identical dimensions: select the
7879 ;; first window of the first frame, switch to the second
7880 ;; frame, move the mouse to its second window, minimize the
7881 ;; second frame. Now the second window of the first frame
7882 ;; gets selected although the mouse never really "moved" into
7883 ;; that window.
7884 (and (numberp mouse-autoselect-window)
7885 (equal (mouse-position) mouse-autoselect-window-position-1)))
7886 ;; Delayed autoselection was temporarily suspended, reenable it.
7887 (mouse-autoselect-window-start mouse-position))
7888 ((and window (not (eq window (selected-window)))
7889 (or (not (numberp mouse-autoselect-window))
7890 (and (>= mouse-autoselect-window 0)
7891 ;; If `mouse-autoselect-window' is non-negative,
7892 ;; select window if it's the same as before.
7893 (eq window mouse-autoselect-window-window))
7894 ;; Otherwise select window iff the mouse is at the same
7895 ;; position as before. Observe that the first test
7896 ;; after starting autoselection usually fails since the
7897 ;; value of `mouse-autoselect-window-position' recorded
7898 ;; there is the position where the mouse has entered the
7899 ;; new window and not necessarily where the mouse has
7900 ;; stopped moving.
7901 (equal mouse-position mouse-autoselect-window-position))
7902 ;; The minibuffer is a candidate window if it's active.
7903 (or (not (window-minibuffer-p window))
7904 (eq window (active-minibuffer-window))))
7905 ;; Mouse position has stabilized in non-selected window: Cancel
7906 ;; delayed autoselection and try to select that window.
7907 (mouse-autoselect-window-cancel t)
7908 ;; Select window where mouse appears unless the selected window is the
7909 ;; minibuffer. Use `unread-command-events' in order to execute pre-
7910 ;; and post-command hooks and trigger idle timers. To avoid delaying
7911 ;; autoselection again, set `mouse-autoselect-window-state'."
7912 (unless (window-minibuffer-p)
7913 (setq mouse-autoselect-window-state 'select)
7914 (setq unread-command-events
7915 (cons (list 'select-window (list window))
7916 unread-command-events))))
7917 ((or (and window (eq window (selected-window)))
7918 (not (numberp mouse-autoselect-window))
7919 (equal mouse-position mouse-autoselect-window-position))
7920 ;; Mouse position has either stabilized in the selected window or at
7921 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
7922 (mouse-autoselect-window-cancel t))
7924 ;; Mouse position has not stabilized yet, resume delayed
7925 ;; autoselection.
7926 (mouse-autoselect-window-start mouse-position window))))))
7928 (defun handle-select-window (event)
7929 "Handle select-window events."
7930 (interactive "^e")
7931 (let ((window (posn-window (event-start event))))
7932 (unless (or (not (window-live-p window))
7933 ;; Don't switch if we're currently in the minibuffer.
7934 ;; This tries to work around problems where the
7935 ;; minibuffer gets unselected unexpectedly, and where
7936 ;; you then have to move your mouse all the way down to
7937 ;; the minibuffer to select it.
7938 (window-minibuffer-p)
7939 ;; Don't switch to minibuffer window unless it's active.
7940 (and (window-minibuffer-p window)
7941 (not (minibuffer-window-active-p window)))
7942 ;; Don't switch when autoselection shall be delayed.
7943 (and (numberp mouse-autoselect-window)
7944 (not (eq mouse-autoselect-window-state 'select))
7945 (let ((position (mouse-position)))
7946 ;; Cancel any delayed autoselection.
7947 (mouse-autoselect-window-cancel t)
7948 ;; Start delayed autoselection from current mouse
7949 ;; position and window.
7950 (setq mouse-autoselect-window-position-1 position)
7951 (mouse-autoselect-window-start position window)
7952 ;; Executing a command cancels delayed autoselection.
7953 (add-hook
7954 'pre-command-hook 'mouse-autoselect-window-cancel))))
7955 (when mouse-autoselect-window
7956 ;; Reset state of delayed autoselection.
7957 (setq mouse-autoselect-window-state nil)
7958 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
7959 (run-hooks 'mouse-leave-buffer-hook))
7960 ;; Clear echo area.
7961 (message nil)
7962 (select-window window))))
7964 (defun truncated-partial-width-window-p (&optional window)
7965 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
7966 WINDOW must be a live window and defaults to the selected one.
7967 Return nil if WINDOW is not a partial-width window
7968 (regardless of the value of `truncate-lines').
7969 Otherwise, consult the value of `truncate-partial-width-windows'
7970 for the buffer shown in WINDOW."
7971 (setq window (window-normalize-window window t))
7972 (unless (window-full-width-p window)
7973 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
7974 (window-buffer window))))
7975 (if (integerp t-p-w-w)
7976 (< (window-width window) t-p-w-w)
7977 t-p-w-w))))
7979 ;; Some of these are in tutorial--default-keys, so update that if you
7980 ;; change these.
7981 (define-key ctl-x-map "0" 'delete-window)
7982 (define-key ctl-x-map "1" 'delete-other-windows)
7983 (define-key ctl-x-map "2" 'split-window-below)
7984 (define-key ctl-x-map "3" 'split-window-right)
7985 (define-key ctl-x-map "o" 'other-window)
7986 (define-key ctl-x-map "^" 'enlarge-window)
7987 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
7988 (define-key ctl-x-map "{" 'shrink-window-horizontally)
7989 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
7990 (define-key ctl-x-map "+" 'balance-windows)
7991 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
7993 ;;; window.el ends here