Include GFDL in org.texi
[emacs.git] / lisp / window.el
blob52909fa9e5fedcaed2892c73afa111a9798bd94b
1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2012
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
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 (&optional buffer action)
112 "Show temporary buffer BUFFER in a window.
113 Return the window showing BUFFER. Pass ACTION as action argument
114 to `display-buffer'."
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 ;; Doc is very similar to with-output-to-temp-buffer.
146 (defmacro with-temp-buffer-window (buffer-or-name action quit-function &rest body)
147 "Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
148 BUFFER-OR-NAME must specify either a live buffer, or the name of a
149 buffer (if it does not exist, this macro creates it).
151 This construct makes buffer BUFFER-OR-NAME empty before running BODY.
152 It does not make the buffer current for BODY.
153 Instead it binds `standard-output' to that buffer, so that output
154 generated with `prin1' and similar functions in BODY goes into
155 the buffer.
157 At the end of BODY, this marks the specified buffer unmodified and
158 read-only, and displays it in a window (but does not select it, or make
159 the buffer current). The display happens by calling `display-buffer'
160 with the ACTION argument. If `temp-buffer-resize-mode' is enabled,
161 the relevant window shrinks automatically.
163 This returns the value returned by BODY, unless QUIT-FUNCTION specifies
164 a function. In that case, it runs the function with two arguments -
165 the window showing the specified buffer and the value returned by
166 BODY - and returns the value returned by that function.
168 If the buffer is displayed on a new frame, the window manager may
169 decide to select that frame. In that case, it's usually a good
170 strategy if QUIT-FUNCTION selects the window showing the buffer
171 before reading any value from the minibuffer; for example, when
172 asking a `yes-or-no-p' question.
174 This runs the hook `temp-buffer-window-setup-hook' before BODY,
175 with the specified buffer temporarily current. It runs the
176 hook `temp-buffer-window-show-hook' after displaying the buffer,
177 with that buffer temporarily current, and the window that was used to
178 display it temporarily selected.
180 This construct is similar to `with-output-to-temp-buffer', but
181 runs different hooks. In particular, it does not run
182 `temp-buffer-setup-hook', which usually puts the buffer in Help mode.
183 Also, it does not call `temp-buffer-show-function' (the ACTION
184 argument replaces this)."
185 (declare (debug t))
186 (let ((buffer (make-symbol "buffer"))
187 (window (make-symbol "window"))
188 (value (make-symbol "value")))
189 `(let* ((,buffer (temp-buffer-window-setup ,buffer-or-name))
190 (standard-output ,buffer)
191 ,window ,value)
192 (with-current-buffer ,buffer
193 (setq ,value (progn ,@body))
194 (setq ,window (temp-buffer-window-show ,buffer ,action)))
196 (if (functionp ,quit-function)
197 (funcall ,quit-function ,window ,value)
198 ,value))))
200 ;; The following two functions are like `window-next-sibling' and
201 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
202 ;; they don't substitute the selected window for nil), and they return
203 ;; nil when WINDOW doesn't have a parent (like a frame's root window or
204 ;; a minibuffer window).
205 (defun window-right (window)
206 "Return WINDOW's right sibling.
207 Return nil if WINDOW is the root window of its frame. WINDOW can
208 be any window."
209 (and window (window-parent window) (window-next-sibling window)))
211 (defun window-left (window)
212 "Return WINDOW's left sibling.
213 Return nil if WINDOW is the root window of its frame. WINDOW can
214 be any window."
215 (and window (window-parent window) (window-prev-sibling window)))
217 (defun window-child (window)
218 "Return WINDOW's first child window.
219 WINDOW can be any window."
220 (or (window-top-child window) (window-left-child window)))
222 (defun window-child-count (window)
223 "Return number of WINDOW's child windows.
224 WINDOW can be any window."
225 (let ((count 0))
226 (when (and (windowp window) (setq window (window-child window)))
227 (while window
228 (setq count (1+ count))
229 (setq window (window-next-sibling window))))
230 count))
232 (defun window-last-child (window)
233 "Return last child window of WINDOW.
234 WINDOW can be any window."
235 (when (and (windowp window) (setq window (window-child window)))
236 (while (window-next-sibling window)
237 (setq window (window-next-sibling window))))
238 window)
240 (defun window-normalize-buffer (buffer-or-name)
241 "Return buffer specified by BUFFER-OR-NAME.
242 BUFFER-OR-NAME must be either a buffer or a string naming a live
243 buffer and defaults to the current buffer."
244 (cond
245 ((not buffer-or-name)
246 (current-buffer))
247 ((bufferp buffer-or-name)
248 (if (buffer-live-p buffer-or-name)
249 buffer-or-name
250 (error "Buffer %s is not a live buffer" buffer-or-name)))
251 ((get-buffer buffer-or-name))
253 (error "No such buffer %s" buffer-or-name))))
255 (defun window-normalize-frame (frame)
256 "Return frame specified by FRAME.
257 FRAME must be a live frame and defaults to the selected frame."
258 (if frame
259 (if (frame-live-p frame)
260 frame
261 (error "%s is not a live frame" frame))
262 (selected-frame)))
264 (defun window-normalize-window (window &optional live-only)
265 "Return the window specified by WINDOW.
266 If WINDOW is nil, return the selected window. Otherwise, if
267 WINDOW is a live or an internal window, return WINDOW; if
268 LIVE-ONLY is non-nil, return WINDOW for a live window only.
269 Otherwise, signal an error."
270 (cond
271 ((null window)
272 (selected-window))
273 (live-only
274 (if (window-live-p window)
275 window
276 (error "%s is not a live window" window)))
277 ((window-valid-p window)
278 window)
280 (error "%s is not a valid window" window))))
282 (defvar ignore-window-parameters nil
283 "If non-nil, standard functions ignore window parameters.
284 The functions currently affected by this are `split-window',
285 `delete-window', `delete-other-windows' and `other-window'.
287 An application may bind this to a non-nil value around calls to
288 these functions to inhibit processing of window parameters.")
290 (defconst window-safe-min-height 1
291 "The absolute minimum number of lines of a window.
292 Anything less might crash Emacs.")
294 (defcustom window-min-height 4
295 "The minimum number of lines of any window.
296 The value has to accommodate a mode- or header-line if present.
297 A value less than `window-safe-min-height' is ignored. The value
298 of this variable is honored when windows are resized or split.
300 Applications should never rebind this variable. To resize a
301 window to a height less than the one specified here, an
302 application should instead call `window-resize' with a non-nil
303 IGNORE argument. In order to have `split-window' make a window
304 shorter, explicitly specify the SIZE argument of that function."
305 :type 'integer
306 :version "24.1"
307 :group 'windows)
309 (defconst window-safe-min-width 2
310 "The absolute minimum number of columns of a window.
311 Anything less might crash Emacs.")
313 (defcustom window-min-width 10
314 "The minimum number of columns of any window.
315 The value has to accommodate margins, fringes, or scrollbars if
316 present. A value less than `window-safe-min-width' is ignored.
317 The value of this variable is honored when windows are resized or
318 split.
320 Applications should never rebind this variable. To resize a
321 window to a width less than the one specified here, an
322 application should instead call `window-resize' with a non-nil
323 IGNORE argument. In order to have `split-window' make a window
324 narrower, explicitly specify the SIZE argument of that function."
325 :type 'integer
326 :version "24.1"
327 :group 'windows)
329 (defun window-combined-p (&optional window horizontal)
330 "Return non-nil if WINDOW has siblings in a given direction.
331 WINDOW must be a valid window and defaults to the selected one.
333 HORIZONTAL determines a direction for the window combination.
334 If HORIZONTAL is omitted or nil, return non-nil if WINDOW is part
335 of a vertical window combination.
336 If HORIZONTAL is non-nil, return non-nil if WINDOW is part of a
337 horizontal window combination."
338 (setq window (window-normalize-window window))
339 (let ((parent (window-parent window)))
340 (and parent
341 (if horizontal
342 (window-left-child parent)
343 (window-top-child parent)))))
345 (defun window-combinations (window &optional horizontal)
346 "Return largest number of windows vertically arranged within WINDOW.
347 WINDOW must be a valid window and defaults to the selected one.
348 If HORIZONTAL is non-nil, return the largest number of
349 windows horizontally arranged within WINDOW."
350 (setq window (window-normalize-window window))
351 (cond
352 ((window-live-p window)
353 ;; If WINDOW is live, return 1.
355 ((if horizontal
356 (window-left-child window)
357 (window-top-child window))
358 ;; If WINDOW is iso-combined, return the sum of the values for all
359 ;; child windows of WINDOW.
360 (let ((child (window-child window))
361 (count 0))
362 (while child
363 (setq count
364 (+ (window-combinations child horizontal)
365 count))
366 (setq child (window-right child)))
367 count))
369 ;; If WINDOW is not iso-combined, return the maximum value of any
370 ;; child window of WINDOW.
371 (let ((child (window-child window))
372 (count 1))
373 (while child
374 (setq count
375 (max (window-combinations child horizontal)
376 count))
377 (setq child (window-right child)))
378 count))))
380 (defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only)
381 "Helper function for `walk-window-tree' and `walk-window-subtree'."
382 (let (walk-window-tree-buffer)
383 (while walk-window-tree-window
384 (setq walk-window-tree-buffer
385 (window-buffer walk-window-tree-window))
386 (when (or walk-window-tree-buffer any)
387 (funcall fun walk-window-tree-window))
388 (unless walk-window-tree-buffer
389 (walk-window-tree-1
390 fun (window-left-child walk-window-tree-window) any)
391 (walk-window-tree-1
392 fun (window-top-child walk-window-tree-window) any))
393 (if sub-only
394 (setq walk-window-tree-window nil)
395 (setq walk-window-tree-window
396 (window-right walk-window-tree-window))))))
398 (defun walk-window-tree (fun &optional frame any minibuf)
399 "Run function FUN on each live window of FRAME.
400 FUN must be a function with one argument - a window. FRAME must
401 be a live frame and defaults to the selected one. ANY, if
402 non-nil, means to run FUN on all live and internal windows of
403 FRAME.
405 Optional argument MINIBUF t means run FUN on FRAME's minibuffer
406 window even if it isn't active. MINIBUF nil or omitted means run
407 FUN on FRAME's minibuffer window only if it's active. In both
408 cases the minibuffer window must be part of FRAME. MINIBUF
409 neither nil nor t means never run FUN on the minibuffer window.
411 This function performs a pre-order, depth-first traversal of the
412 window tree. If FUN changes the window tree, the result is
413 unpredictable."
414 (setq frame (window-normalize-frame frame))
415 (walk-window-tree-1 fun (frame-root-window frame) any)
416 (when (memq minibuf '(nil t))
417 ;; Run FUN on FRAME's minibuffer window if requested.
418 (let ((minibuffer-window (minibuffer-window frame)))
419 (when (and (window-live-p minibuffer-window)
420 (eq (window-frame minibuffer-window) frame)
421 (or (eq minibuf t)
422 (minibuffer-window-active-p minibuffer-window)))
423 (funcall fun minibuffer-window)))))
425 (defun walk-window-subtree (fun &optional window any)
426 "Run function FUN on the subtree of windows rooted at WINDOW.
427 WINDOW defaults to the selected window. FUN must be a function
428 with one argument - a window. By default, run FUN only on live
429 windows of the subtree. If the optional argument ANY is non-nil,
430 run FUN on all live and internal windows of the subtree. If
431 WINDOW is live, run FUN on WINDOW only.
433 This function performs a pre-order, depth-first traversal of the
434 subtree rooted at WINDOW. If FUN changes that tree, the result
435 is unpredictable."
436 (setq window (window-normalize-window window))
437 (walk-window-tree-1 fun window any t))
439 (defun window-with-parameter (parameter &optional value frame any minibuf)
440 "Return first window on FRAME with PARAMETER non-nil.
441 FRAME defaults to the selected frame. Optional argument VALUE
442 non-nil means only return a window whose window-parameter value
443 for PARAMETER equals VALUE (comparison is done with `equal').
444 Optional argument ANY non-nil means consider internal windows
445 too.
447 Optional argument MINIBUF t means consider FRAME's minibuffer
448 window even if it isn't active. MINIBUF nil or omitted means
449 consider FRAME's minibuffer window only if it's active. In both
450 cases the minibuffer window must be part of FRAME. MINIBUF
451 neither nil nor t means never consider the minibuffer window."
452 (let (this-value)
453 (catch 'found
454 (walk-window-tree
455 (lambda (window)
456 (when (and (setq this-value (window-parameter window parameter))
457 (or (not value) (equal value this-value)))
458 (throw 'found window)))
459 frame any minibuf))))
461 ;;; Atomic windows.
462 (defun window-atom-root (&optional window)
463 "Return root of atomic window WINDOW is a part of.
464 WINDOW must be a valid window and defaults to the selected one.
465 Return nil if WINDOW is not part of an atomic window."
466 (setq window (window-normalize-window window))
467 (let (root)
468 (while (and window (window-parameter window 'window-atom))
469 (setq root window)
470 (setq window (window-parent window)))
471 root))
473 (defun window-make-atom (window)
474 "Make WINDOW an atomic window.
475 WINDOW must be an internal window. Return WINDOW."
476 (if (not (window-child window))
477 (error "Window %s is not an internal window" window)
478 (walk-window-subtree
479 (lambda (window)
480 (set-window-parameter window 'window-atom t))
481 window t)
482 window))
484 (defun display-buffer-in-atom-window (buffer alist)
485 "Display BUFFER in an atomic window.
486 This function displays BUFFER in a new window that will be
487 combined with an existing window to form an atomic window. If
488 the existing window is already part of an atomic window, add the
489 new window to that atomic window. Operations like `split-window'
490 or `delete-window', when applied to a constituent of an atomic
491 window, are applied atomically to the root of that atomic window.
493 ALIST is an association list of symbols and values. The
494 following symbols can be used.
496 `window' specifies the existing window the new window shall be
497 combined with. Use `window-atom-root' to make the new window a
498 sibling of an atomic window's root. If an internal window is
499 specified here, all children of that window become part of the
500 atomic window too. If no window is specified, the new window
501 becomes a sibling of the selected window.
503 `side' denotes the side of the existing window where the new
504 window shall be located. Valid values are `below', `right',
505 `above' and `left'. The default is `below'.
507 The return value is the new window, nil when creating that window
508 failed."
509 (let ((ignore-window-parameters t)
510 (window-combination-limit t)
511 (window (cdr (assq 'window alist)))
512 (side (cdr (assq 'side alist)))
513 new)
514 (setq window (window-normalize-window window))
515 ;; Split off new window
516 (when (setq new (split-window window nil side))
517 ;; Make sure we have a valid atomic window.
518 (window-make-atom (window-parent window))
519 ;; Display BUFFER in NEW and return NEW.
520 (window--display-buffer
521 buffer new 'window alist display-buffer-mark-dedicated))))
523 (defun window--atom-check-1 (window)
524 "Subroutine of `window--atom-check'."
525 (when window
526 (if (window-parameter window 'window-atom)
527 (let ((count 0))
528 (when (or (catch 'reset
529 (walk-window-subtree
530 (lambda (window)
531 (if (window-parameter window 'window-atom)
532 (setq count (1+ count))
533 (throw 'reset t)))
534 window t))
535 ;; count >= 1 must hold here. If there's no other
536 ;; window around dissolve this atomic window.
537 (= count 1))
538 ;; Dissolve atomic window.
539 (walk-window-subtree
540 (lambda (window)
541 (set-window-parameter window 'window-atom nil))
542 window t)))
543 ;; Check children.
544 (unless (window-buffer window)
545 (window--atom-check-1 (window-left-child window))
546 (window--atom-check-1 (window-top-child window))))
547 ;; Check right sibling
548 (window--atom-check-1 (window-right window))))
550 (defun window--atom-check (&optional frame)
551 "Check atomicity of all windows on FRAME.
552 FRAME defaults to the selected frame. If an atomic window is
553 wrongly configured, reset the atomicity of all its windows on
554 FRAME to nil. An atomic window is wrongly configured if it has
555 no child windows or one of its child windows is not atomic."
556 (window--atom-check-1 (frame-root-window frame)))
558 ;; Side windows.
559 (defvar window-sides '(left top right bottom)
560 "Window sides.")
562 (defcustom window-sides-vertical nil
563 "If non-nil, left and right side windows are full height.
564 Otherwise, top and bottom side windows are full width."
565 :type 'boolean
566 :group 'windows
567 :version "24.1")
569 (defcustom window-sides-slots '(nil nil nil nil)
570 "Maximum number of side window slots.
571 The value is a list of four elements specifying the number of
572 side window slots on (in this order) the left, top, right and
573 bottom side of each frame. If an element is a number, this means
574 to display at most that many side windows on the corresponding
575 side. If an element is nil, this means there's no bound on the
576 number of slots on that side."
577 :version "24.1"
578 :risky t
579 :type
580 '(list
581 :value (nil nil nil nil)
582 (choice
583 :tag "Left"
584 :help-echo "Maximum slots of left side window."
585 :value nil
586 :format "%[Left%] %v\n"
587 (const :tag "Unlimited" :format "%t" nil)
588 (integer :tag "Number" :value 2 :size 5))
589 (choice
590 :tag "Top"
591 :help-echo "Maximum slots of top side window."
592 :value nil
593 :format "%[Top%] %v\n"
594 (const :tag "Unlimited" :format "%t" nil)
595 (integer :tag "Number" :value 3 :size 5))
596 (choice
597 :tag "Right"
598 :help-echo "Maximum slots of right side window."
599 :value nil
600 :format "%[Right%] %v\n"
601 (const :tag "Unlimited" :format "%t" nil)
602 (integer :tag "Number" :value 2 :size 5))
603 (choice
604 :tag "Bottom"
605 :help-echo "Maximum slots of bottom side window."
606 :value nil
607 :format "%[Bottom%] %v\n"
608 (const :tag "Unlimited" :format "%t" nil)
609 (integer :tag "Number" :value 3 :size 5)))
610 :group 'windows)
612 (defun window--major-non-side-window (&optional frame)
613 "Return the major non-side window of frame FRAME.
614 The optional argument FRAME must be a live frame and defaults to
615 the selected one.
617 If FRAME has at least one side window, the major non-side window
618 is either an internal non-side window such that all other
619 non-side windows on FRAME descend from it, or the single live
620 non-side window of FRAME. If FRAME has no side windows, return
621 its root window."
622 (let ((frame (window-normalize-frame frame))
623 major sibling)
624 ;; Set major to the _last_ window found by `walk-window-tree' that
625 ;; is not a side window but has a side window as its sibling.
626 (walk-window-tree
627 (lambda (window)
628 (and (not (window-parameter window 'window-side))
629 (or (and (setq sibling (window-prev-sibling window))
630 (window-parameter sibling 'window-side))
631 (and (setq sibling (window-next-sibling window))
632 (window-parameter sibling 'window-side)))
633 (setq major window)))
634 frame t)
635 (or major (frame-root-window frame))))
637 (defun window--major-side-window (side)
638 "Return major side window on SIDE.
639 SIDE must be one of the symbols `left', `top', `right' or
640 `bottom'. Return nil if no such window exists."
641 (let ((root (frame-root-window))
642 window)
643 ;; (1) If a window on the opposite side exists, return that window's
644 ;; sibling.
645 ;; (2) If the new window shall span the entire side, return the
646 ;; frame's root window.
647 ;; (3) If a window on an orthogonal side exists, return that
648 ;; window's sibling.
649 ;; (4) Otherwise return the frame's root window.
650 (cond
651 ((or (and (eq side 'left)
652 (setq window (window-with-parameter 'window-side 'right nil t)))
653 (and (eq side 'top)
654 (setq window (window-with-parameter 'window-side 'bottom nil t))))
655 (window-prev-sibling window))
656 ((or (and (eq side 'right)
657 (setq window (window-with-parameter 'window-side 'left nil t)))
658 (and (eq side 'bottom)
659 (setq window (window-with-parameter 'window-side 'top nil t))))
660 (window-next-sibling window))
661 ((memq side '(left right))
662 (cond
663 (window-sides-vertical
664 root)
665 ((setq window (window-with-parameter 'window-side 'top nil t))
666 (window-next-sibling window))
667 ((setq window (window-with-parameter 'window-side 'bottom nil t))
668 (window-prev-sibling window))
669 (t root)))
670 ((memq side '(top bottom))
671 (cond
672 ((not window-sides-vertical)
673 root)
674 ((setq window (window-with-parameter 'window-side 'left nil t))
675 (window-next-sibling window))
676 ((setq window (window-with-parameter 'window-side 'right nil t))
677 (window-prev-sibling window))
678 (t root))))))
680 (defun display-buffer-in-major-side-window (buffer side slot &optional alist)
681 "Display BUFFER in a new window on SIDE of the selected frame.
682 SIDE must be one of `left', `top', `right' or `bottom'. SLOT
683 specifies the slot to use. ALIST is an association list of
684 symbols and values as passed to `display-buffer-in-side-window'.
685 This function may be called only if no window on SIDE exists yet.
686 The new window automatically becomes the \"major\" side window on
687 SIDE. Return the new window, nil if its creation window failed."
688 (let* ((root (frame-root-window))
689 (left-or-right (memq side '(left right)))
690 (major (window--major-side-window side))
691 (selected-window (selected-window))
692 (on-side (cond
693 ((eq side 'top) 'above)
694 ((eq side 'bottom) 'below)
695 (t side)))
696 ;; The following two bindings will tell `split-window' to take
697 ;; the space for the new window from `major' and not make a new
698 ;; parent window unless needed.
699 (window-combination-resize 'side)
700 (window-combination-limit nil)
701 (new (split-window major nil on-side))
702 fun)
703 (when new
704 ;; Initialize `window-side' parameter of new window to SIDE.
705 (set-window-parameter new 'window-side side)
706 ;; Install `window-slot' parameter of new window.
707 (set-window-parameter new 'window-slot slot)
708 ;; Install `delete-window' parameter thus making sure that when
709 ;; the new window is deleted, a side window on the opposite side
710 ;; does not get resized.
711 (set-window-parameter new 'delete-window 'delete-side-window)
712 ;; Auto-adjust height/width of new window unless a size has been
713 ;; explicitly requested.
714 (unless (if left-or-right
715 (cdr (assq 'window-width alist))
716 (cdr (assq 'window-height alist)))
717 (setq alist
718 (cons
719 (cons
720 (if left-or-right 'window-width 'window-height)
721 (/ (window-total-size (frame-root-window) left-or-right)
722 ;; By default use a fourth of the size of the
723 ;; frame's root window.
725 alist)))
726 ;; Install BUFFER in new window and return NEW.
727 (window--display-buffer buffer new 'window alist 'side))))
729 (defun delete-side-window (window)
730 "Delete side window WINDOW."
731 (let ((window-combination-resize
732 (window-parameter (window-parent window) 'window-side))
733 (ignore-window-parameters t))
734 (delete-window window)))
736 (defun display-buffer-in-side-window (buffer alist)
737 "Display BUFFER in a window on side SIDE of the selected frame.
738 ALIST is an association list of symbols and values. The
739 following symbols can be used:
741 `side' denotes the side of the existing window where the new
742 window shall be located. Valid values are `bottom', `right',
743 `top' and `left'. The default is `bottom'.
745 `slot' if non-nil, specifies the window slot where to display
746 BUFFER. A value of zero or nil means use the middle slot on
747 the specified side. A negative value means use a slot
748 preceding (that is, above or on the left of) the middle slot.
749 A positive value means use a slot following (that is, below or
750 on the right of) the middle slot. The default is zero."
751 (let ((side (or (cdr (assq 'side alist)) 'bottom))
752 (slot (or (cdr (assq 'slot alist)) 0))
753 new)
754 (cond
755 ((not (memq side '(top bottom left right)))
756 (error "Invalid side %s specified" side))
757 ((not (numberp slot))
758 (error "Invalid slot %s specified" slot)))
760 (let* ((major (window-with-parameter 'window-side side nil t))
761 ;; `major' is the major window on SIDE, `windows' the list of
762 ;; life windows on SIDE.
763 (windows
764 (when major
765 (let (windows)
766 (walk-window-tree
767 (lambda (window)
768 (when (eq (window-parameter window 'window-side) side)
769 (setq windows (cons window windows)))))
770 (nreverse windows))))
771 (slots (when major (max 1 (window-child-count major))))
772 (max-slots
773 (nth (cond
774 ((eq side 'left) 0)
775 ((eq side 'top) 1)
776 ((eq side 'right) 2)
777 ((eq side 'bottom) 3))
778 window-sides-slots))
779 (selected-window (selected-window))
780 window this-window this-slot prev-window next-window
781 best-window best-slot abs-slot new-window)
783 (cond
784 ((and (numberp max-slots) (<= max-slots 0))
785 ;; No side-slots available on this side. Don't create an error,
786 ;; just return nil.
787 nil)
788 ((not windows)
789 ;; No major window exists on this side, make one.
790 (display-buffer-in-major-side-window buffer side slot alist))
792 ;; Scan windows on SIDE.
793 (catch 'found
794 (dolist (window windows)
795 (setq this-slot (window-parameter window 'window-slot))
796 (cond
797 ;; The following should not happen and probably be checked
798 ;; by window--side-check.
799 ((not (numberp this-slot)))
800 ((= this-slot slot)
801 ;; A window with a matching slot has been found.
802 (setq this-window window)
803 (throw 'found t))
805 ;; Check if this window has a better slot value wrt the
806 ;; slot of the window we want.
807 (setq abs-slot
808 (if (or (and (> this-slot 0) (> slot 0))
809 (and (< this-slot 0) (< slot 0)))
810 (abs (- slot this-slot))
811 (+ (abs slot) (abs this-slot))))
812 (unless (and best-slot (<= best-slot abs-slot))
813 (setq best-window window)
814 (setq best-slot abs-slot))
815 (cond
816 ((<= this-slot slot)
817 (setq prev-window window))
818 ((not next-window)
819 (setq next-window window)))))))
821 ;; `this-window' is the first window with the same SLOT.
822 ;; `prev-window' is the window with the largest slot < SLOT. A new
823 ;; window will be created after it.
824 ;; `next-window' is the window with the smallest slot > SLOT. A new
825 ;; window will be created before it.
826 ;; `best-window' is the window with the smallest absolute difference
827 ;; of its slot and SLOT.
829 ;; Note: We dedicate the window used softly to its buffer to
830 ;; avoid that "other" (non-side) buffer display functions steal
831 ;; it from us. This must eventually become customizable via
832 ;; ALIST (or, better, avoided in the "other" functions).
833 (or (and this-window
834 ;; Reuse `this-window'.
835 (window--display-buffer buffer this-window 'reuse alist 'side))
836 (and (or (not max-slots) (< slots max-slots))
837 (or (and next-window
838 ;; Make new window before `next-window'.
839 (let ((next-side
840 (if (memq side '(left right)) 'above 'left))
841 (window-combination-resize 'side))
842 (setq window (split-window next-window nil next-side))
843 ;; When the new window is deleted, its space
844 ;; is returned to other side windows.
845 (set-window-parameter
846 window 'delete-window 'delete-side-window)
847 window))
848 (and prev-window
849 ;; Make new window after `prev-window'.
850 (let ((prev-side
851 (if (memq side '(left right)) 'below 'right))
852 (window-combination-resize 'side))
853 (setq window (split-window prev-window nil prev-side))
854 ;; When the new window is deleted, its space
855 ;; is returned to other side windows.
856 (set-window-parameter
857 window 'delete-window 'delete-side-window)
858 window)))
859 (set-window-parameter window 'window-slot slot)
860 (window--display-buffer buffer window 'window alist 'side))
861 (and best-window
862 ;; Reuse `best-window'.
863 (progn
864 ;; Give best-window the new slot value.
865 (set-window-parameter best-window 'window-slot slot)
866 (window--display-buffer
867 buffer best-window 'reuse alist 'side)))))))))
869 (defun window--side-check (&optional frame)
870 "Check the side window configuration of FRAME.
871 FRAME defaults to the selected frame.
873 A valid side window configuration preserves the following two
874 invariants:
876 - If there exists a window whose window-side parameter is
877 non-nil, there must exist at least one live window whose
878 window-side parameter is nil.
880 - If a window W has a non-nil window-side parameter (i) it must
881 have a parent window and that parent's window-side parameter
882 must be either nil or the same as for W, and (ii) any child
883 window of W must have the same window-side parameter as W.
885 If the configuration is invalid, reset the window-side parameters
886 of all windows on FRAME to nil."
887 (let (left top right bottom none side parent parent-side)
888 (when (or (catch 'reset
889 (walk-window-tree
890 (lambda (window)
891 (setq side (window-parameter window 'window-side))
892 (setq parent (window-parent window))
893 (setq parent-side
894 (and parent (window-parameter parent 'window-side)))
895 ;; The following `cond' seems a bit tedious, but I'd
896 ;; rather stick to using just the stack.
897 (cond
898 (parent-side
899 (when (not (eq parent-side side))
900 ;; A parent whose window-side is non-nil must
901 ;; have a child with the same window-side.
902 (throw 'reset t)))
903 ((not side)
904 (when (window-buffer window)
905 ;; Record that we have at least one non-side,
906 ;; live window.
907 (setq none t)))
908 ((if (memq side '(left top))
909 (window-prev-sibling window)
910 (window-next-sibling window))
911 ;; Left and top major side windows must not have a
912 ;; previous sibling, right and bottom major side
913 ;; windows must not have a next sibling.
914 (throw 'reset t))
915 ;; Now check that there's no more than one major
916 ;; window for any of left, top, right and bottom.
917 ((eq side 'left)
918 (if left (throw 'reset t) (setq left t)))
919 ((eq side 'top)
920 (if top (throw 'reset t) (setq top t)))
921 ((eq side 'right)
922 (if right (throw 'reset t) (setq right t)))
923 ((eq side 'bottom)
924 (if bottom (throw 'reset t) (setq bottom t)))
926 (throw 'reset t))))
927 frame t))
928 ;; If there's a side window, there must be at least one
929 ;; non-side window.
930 (and (or left top right bottom) (not none)))
931 (walk-window-tree
932 (lambda (window)
933 (set-window-parameter window 'window-side nil))
934 frame t))))
936 (defun window--check (&optional frame)
937 "Check atomic and side windows on FRAME.
938 FRAME defaults to the selected frame."
939 (window--side-check frame)
940 (window--atom-check frame))
942 ;;; Window sizes.
943 (defvar window-size-fixed nil
944 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
945 If the value is `height', then only the window's height is fixed.
946 If the value is `width', then only the window's width is fixed.
947 Any other non-nil value fixes both the width and the height.
949 Emacs won't change the size of any window displaying that buffer,
950 unless it has no other choice (like when deleting a neighboring
951 window).")
952 (make-variable-buffer-local 'window-size-fixed)
954 (defun window--size-ignore-p (window ignore)
955 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
956 (if (window-valid-p ignore) (eq window ignore) ignore))
958 (defun window-min-size (&optional window horizontal ignore)
959 "Return the minimum size of WINDOW.
960 WINDOW must be a valid window and defaults to the selected one.
961 Optional argument HORIZONTAL non-nil means return the minimum
962 number of columns of WINDOW; otherwise return the minimum number
963 of WINDOW's lines.
965 Optional argument IGNORE, if non-nil, means ignore restrictions
966 imposed by fixed size windows, `window-min-height' or
967 `window-min-width' settings. If IGNORE equals `safe', live
968 windows may get as small as `window-safe-min-height' lines and
969 `window-safe-min-width' columns. If IGNORE is a window, ignore
970 restrictions for that window only. Any other non-nil value
971 means ignore all of the above restrictions for all windows."
972 (window--min-size-1
973 (window-normalize-window window) horizontal ignore))
975 (defun window--min-size-1 (window horizontal ignore)
976 "Internal function of `window-min-size'."
977 (let ((sub (window-child window)))
978 (if sub
979 (let ((value 0))
980 ;; WINDOW is an internal window.
981 (if (window-combined-p sub horizontal)
982 ;; The minimum size of an iso-combination is the sum of
983 ;; the minimum sizes of its child windows.
984 (while sub
985 (setq value (+ value
986 (window--min-size-1 sub horizontal ignore)))
987 (setq sub (window-right sub)))
988 ;; The minimum size of an ortho-combination is the maximum of
989 ;; the minimum sizes of its child windows.
990 (while sub
991 (setq value (max value
992 (window--min-size-1 sub horizontal ignore)))
993 (setq sub (window-right sub))))
994 value)
995 (with-current-buffer (window-buffer window)
996 (cond
997 ((and (not (window--size-ignore-p window ignore))
998 (window-size-fixed-p window horizontal))
999 ;; The minimum size of a fixed size window is its size.
1000 (window-total-size window horizontal))
1001 ((or (eq ignore 'safe) (eq ignore window))
1002 ;; If IGNORE equals `safe' or WINDOW return the safe values.
1003 (if horizontal window-safe-min-width window-safe-min-height))
1004 (horizontal
1005 ;; For the minimum width of a window take fringes and
1006 ;; scroll-bars into account. This is questionable and should
1007 ;; be removed as soon as we are able to split (and resize)
1008 ;; windows such that the new (or resized) windows can get a
1009 ;; size less than the user-specified `window-min-height' and
1010 ;; `window-min-width'.
1011 (let ((frame (window-frame window))
1012 (fringes (window-fringes window))
1013 (scroll-bars (window-scroll-bars window)))
1014 (max
1015 (+ window-safe-min-width
1016 (ceiling (car fringes) (frame-char-width frame))
1017 (ceiling (cadr fringes) (frame-char-width frame))
1018 (cond
1019 ((memq (nth 2 scroll-bars) '(left right))
1020 (nth 1 scroll-bars))
1021 ((memq (frame-parameter frame 'vertical-scroll-bars)
1022 '(left right))
1023 (ceiling (or (frame-parameter frame 'scroll-bar-width) 14)
1024 (frame-char-width)))
1025 (t 0)))
1026 (if (and (not (window--size-ignore-p window ignore))
1027 (numberp window-min-width))
1028 window-min-width
1029 0))))
1031 ;; For the minimum height of a window take any mode- or
1032 ;; header-line into account.
1033 (max (+ window-safe-min-height
1034 (if header-line-format 1 0)
1035 (if mode-line-format 1 0))
1036 (if (and (not (window--size-ignore-p window ignore))
1037 (numberp window-min-height))
1038 window-min-height
1039 0))))))))
1041 (defun window-sizable (window delta &optional horizontal ignore)
1042 "Return DELTA if DELTA lines can be added to WINDOW.
1043 WINDOW must be a valid window and defaults to the selected one.
1044 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
1045 columns can be added to WINDOW. A return value of zero means
1046 that no lines (or columns) can be added to WINDOW.
1048 This function looks only at WINDOW and, recursively, its child
1049 windows. The function `window-resizable' looks at other windows
1050 as well.
1052 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1053 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1054 return the maximum value in the range 0..DELTA by which WINDOW
1055 can be enlarged.
1057 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1058 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1059 return the minimum value in the range DELTA..0 by which WINDOW
1060 can be shrunk.
1062 Optional argument IGNORE non-nil means ignore restrictions
1063 imposed by fixed size windows, `window-min-height' or
1064 `window-min-width' settings. If IGNORE equals `safe', live
1065 windows may get as small as `window-safe-min-height' lines and
1066 `window-safe-min-width' columns. If IGNORE is a window, ignore
1067 restrictions for that window only. Any other non-nil value means
1068 ignore all of the above restrictions for all windows."
1069 (setq window (window-normalize-window window))
1070 (cond
1071 ((< delta 0)
1072 (max (- (window-min-size window horizontal ignore)
1073 (window-total-size window horizontal))
1074 delta))
1075 ((window--size-ignore-p window ignore)
1076 delta)
1077 ((> delta 0)
1078 (if (window-size-fixed-p window horizontal)
1080 delta))
1081 (t 0)))
1083 (defun window-sizable-p (window delta &optional horizontal ignore)
1084 "Return t if WINDOW can be resized by DELTA lines.
1085 WINDOW must be a valid window and defaults to the selected one.
1086 For the meaning of the arguments of this function see the
1087 doc-string of `window-sizable'."
1088 (setq window (window-normalize-window window))
1089 (if (> delta 0)
1090 (>= (window-sizable window delta horizontal ignore) delta)
1091 (<= (window-sizable window delta horizontal ignore) delta)))
1093 (defun window--size-fixed-1 (window horizontal)
1094 "Internal function for `window-size-fixed-p'."
1095 (let ((sub (window-child window)))
1096 (catch 'fixed
1097 (if sub
1098 ;; WINDOW is an internal window.
1099 (if (window-combined-p sub horizontal)
1100 ;; An iso-combination is fixed size if all its child
1101 ;; windows are fixed-size.
1102 (progn
1103 (while sub
1104 (unless (window--size-fixed-1 sub horizontal)
1105 ;; We found a non-fixed-size child window, so
1106 ;; WINDOW's size is not fixed.
1107 (throw 'fixed nil))
1108 (setq sub (window-right sub)))
1109 ;; All child windows are fixed-size, so WINDOW's size is
1110 ;; fixed.
1111 (throw 'fixed t))
1112 ;; An ortho-combination is fixed-size if at least one of its
1113 ;; child windows is fixed-size.
1114 (while sub
1115 (when (window--size-fixed-1 sub horizontal)
1116 ;; We found a fixed-size child window, so WINDOW's size
1117 ;; is fixed.
1118 (throw 'fixed t))
1119 (setq sub (window-right sub))))
1120 ;; WINDOW is a live window.
1121 (with-current-buffer (window-buffer window)
1122 (if horizontal
1123 (memq window-size-fixed '(width t))
1124 (memq window-size-fixed '(height t))))))))
1126 (defun window-size-fixed-p (&optional window horizontal)
1127 "Return non-nil if WINDOW's height is fixed.
1128 WINDOW must be a valid window and defaults to the selected one.
1129 Optional argument HORIZONTAL non-nil means return non-nil if
1130 WINDOW's width is fixed.
1132 If this function returns nil, this does not necessarily mean that
1133 WINDOW can be resized in the desired direction. The function
1134 `window-resizable' can tell that."
1135 (window--size-fixed-1
1136 (window-normalize-window window) horizontal))
1138 (defun window--min-delta-1 (window delta &optional horizontal ignore trail noup)
1139 "Internal function for `window-min-delta'."
1140 (if (not (window-parent window))
1141 ;; If we can't go up, return zero.
1143 ;; Else try to find a non-fixed-size sibling of WINDOW.
1144 (let* ((parent (window-parent window))
1145 (sub (window-child parent)))
1146 (catch 'done
1147 (if (window-combined-p sub horizontal)
1148 ;; In an iso-combination throw DELTA if we find at least one
1149 ;; child window and that window is either not fixed-size or
1150 ;; we can ignore fixed-sizeness.
1151 (let ((skip (eq trail 'after)))
1152 (while sub
1153 (cond
1154 ((eq sub window)
1155 (setq skip (eq trail 'before)))
1156 (skip)
1157 ((and (not (window--size-ignore-p window ignore))
1158 (window-size-fixed-p sub horizontal)))
1160 ;; We found a non-fixed-size child window.
1161 (throw 'done delta)))
1162 (setq sub (window-right sub))))
1163 ;; In an ortho-combination set DELTA to the minimum value by
1164 ;; which other child windows can shrink.
1165 (while sub
1166 (unless (eq sub window)
1167 (setq delta
1168 (min delta
1169 (- (window-total-size sub horizontal)
1170 (window-min-size sub horizontal ignore)))))
1171 (setq sub (window-right sub))))
1172 (if noup
1173 delta
1174 (window--min-delta-1 parent delta horizontal ignore trail))))))
1176 (defun window-min-delta (&optional window horizontal ignore trail noup nodown)
1177 "Return number of lines by which WINDOW can be shrunk.
1178 WINDOW must be a valid window and defaults to the selected one.
1179 Return zero if WINDOW cannot be shrunk.
1181 Optional argument HORIZONTAL non-nil means return number of
1182 columns by which WINDOW can be shrunk.
1184 Optional argument IGNORE non-nil means ignore restrictions
1185 imposed by fixed size windows, `window-min-height' or
1186 `window-min-width' settings. If IGNORE is a window, ignore
1187 restrictions for that window only. If IGNORE equals `safe',
1188 live windows may get as small as `window-safe-min-height' lines
1189 and `window-safe-min-width' columns. Any other non-nil value
1190 means ignore all of the above restrictions for all windows.
1192 Optional argument TRAIL restricts the windows that can be enlarged.
1193 If its value is `before', only windows to the left of or above WINDOW
1194 can be enlarged. If it is `after', only windows to the right of or
1195 below WINDOW can be enlarged.
1197 Optional argument NOUP non-nil means don't go up in the window
1198 tree, but try to enlarge windows within WINDOW's combination only.
1200 Optional argument NODOWN non-nil means don't check whether WINDOW
1201 itself (and its child windows) can be shrunk; check only whether
1202 at least one other window can be enlarged appropriately."
1203 (setq window (window-normalize-window window))
1204 (let ((size (window-total-size window horizontal))
1205 (minimum (window-min-size window horizontal ignore)))
1206 (cond
1207 (nodown
1208 ;; If NODOWN is t, try to recover the entire size of WINDOW.
1209 (window--min-delta-1 window size horizontal ignore trail noup))
1210 ((= size minimum)
1211 ;; If NODOWN is nil and WINDOW's size is already at its minimum,
1212 ;; there's nothing to recover.
1215 ;; Otherwise, try to recover whatever WINDOW is larger than its
1216 ;; minimum size.
1217 (window--min-delta-1
1218 window (- size minimum) horizontal ignore trail noup)))))
1220 (defun window--max-delta-1 (window delta &optional horizontal ignore trail noup)
1221 "Internal function of `window-max-delta'."
1222 (if (not (window-parent window))
1223 ;; Can't go up. Return DELTA.
1224 delta
1225 (let* ((parent (window-parent window))
1226 (sub (window-child parent)))
1227 (catch 'fixed
1228 (if (window-combined-p sub horizontal)
1229 ;; For an iso-combination calculate how much we can get from
1230 ;; other child windows.
1231 (let ((skip (eq trail 'after)))
1232 (while sub
1233 (cond
1234 ((eq sub window)
1235 (setq skip (eq trail 'before)))
1236 (skip)
1238 (setq delta
1239 (+ delta
1240 (- (window-total-size sub horizontal)
1241 (window-min-size sub horizontal ignore))))))
1242 (setq sub (window-right sub))))
1243 ;; For an ortho-combination throw DELTA when at least one
1244 ;; child window is fixed-size.
1245 (while sub
1246 (when (and (not (eq sub window))
1247 (not (window--size-ignore-p sub ignore))
1248 (window-size-fixed-p sub horizontal))
1249 (throw 'fixed delta))
1250 (setq sub (window-right sub))))
1251 (if noup
1252 ;; When NOUP is nil, DELTA is all we can get.
1253 delta
1254 ;; Else try with parent of WINDOW, passing the DELTA we
1255 ;; recovered so far.
1256 (window--max-delta-1 parent delta horizontal ignore trail))))))
1258 (defun window-max-delta (&optional window horizontal ignore trail noup nodown)
1259 "Return maximum number of lines by which WINDOW can be enlarged.
1260 WINDOW must be a valid window and defaults to the selected one.
1261 The return value is zero if WINDOW cannot be enlarged.
1263 Optional argument HORIZONTAL non-nil means return maximum number
1264 of columns by which WINDOW can be enlarged.
1266 Optional argument IGNORE non-nil means ignore restrictions
1267 imposed by fixed size windows, `window-min-height' or
1268 `window-min-width' settings. If IGNORE is a window, ignore
1269 restrictions for that window only. If IGNORE equals `safe',
1270 live windows may get as small as `window-safe-min-height' lines
1271 and `window-safe-min-width' columns. Any other non-nil value means
1272 ignore all of the above restrictions for all windows.
1274 Optional argument TRAIL restricts the windows that can be enlarged.
1275 If its value is `before', only windows to the left of or above WINDOW
1276 can be enlarged. If it is `after', only windows to the right of or
1277 below WINDOW can be enlarged.
1279 Optional argument NOUP non-nil means don't go up in the window
1280 tree but try to obtain the entire space from windows within
1281 WINDOW's combination.
1283 Optional argument NODOWN non-nil means do not check whether
1284 WINDOW itself (and its child windows) can be enlarged; check
1285 only whether other windows can be shrunk appropriately."
1286 (setq window (window-normalize-window window))
1287 (if (and (not (window--size-ignore-p window ignore))
1288 (not nodown) (window-size-fixed-p window horizontal))
1289 ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
1290 ;; size.
1292 ;; WINDOW has no fixed size.
1293 (window--max-delta-1 window 0 horizontal ignore trail noup)))
1295 ;; Make NOUP also inhibit the min-size check.
1296 (defun window--resizable (window delta &optional horizontal ignore trail noup nodown)
1297 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1298 WINDOW must be a valid window and defaults to the selected one.
1299 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1300 can be resized horizontally by DELTA columns. A return value of
1301 zero means that WINDOW is not resizable.
1303 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1304 columns. If WINDOW cannot be enlarged by DELTA lines or columns,
1305 return the maximum value in the range 0..DELTA by which WINDOW
1306 can be enlarged.
1308 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1309 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1310 return the minimum value in the range DELTA..0 that can be used
1311 for shrinking WINDOW.
1313 Optional argument IGNORE non-nil means ignore restrictions
1314 imposed by fixed size windows, `window-min-height' or
1315 `window-min-width' settings. If IGNORE is a window, ignore
1316 restrictions for that window only. If IGNORE equals `safe',
1317 live windows may get as small as `window-safe-min-height' lines
1318 and `window-safe-min-width' columns. Any other non-nil value
1319 means ignore all of the above restrictions for all windows.
1321 Optional argument TRAIL `before' means only windows to the left
1322 of or below WINDOW can be shrunk. Optional argument TRAIL
1323 `after' means only windows to the right of or above WINDOW can be
1324 shrunk.
1326 Optional argument NOUP non-nil means don't go up in the window
1327 tree but check only whether space can be obtained from (or given
1328 to) WINDOW's siblings.
1330 Optional argument NODOWN non-nil means don't go down in the
1331 window tree. This means do not check whether resizing would
1332 violate size restrictions of WINDOW or its child windows."
1333 (setq window (window-normalize-window window))
1334 (cond
1335 ((< delta 0)
1336 (max (- (window-min-delta window horizontal ignore trail noup nodown))
1337 delta))
1338 ((> delta 0)
1339 (min (window-max-delta window horizontal ignore trail noup nodown)
1340 delta))
1341 (t 0)))
1343 (defun window--resizable-p (window delta &optional horizontal ignore trail noup nodown)
1344 "Return t if WINDOW can be resized vertically by DELTA lines.
1345 WINDOW must be a valid window and defaults to the selected one.
1346 For the meaning of the arguments of this function see the
1347 doc-string of `window--resizable'."
1348 (setq window (window-normalize-window window))
1349 (if (> delta 0)
1350 (>= (window--resizable window delta horizontal ignore trail noup nodown)
1351 delta)
1352 (<= (window--resizable window delta horizontal ignore trail noup nodown)
1353 delta)))
1355 (defun window-resizable (window delta &optional horizontal ignore)
1356 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
1357 WINDOW must be a valid window and defaults to the selected one.
1358 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
1359 can be resized horizontally by DELTA columns. A return value of
1360 zero means that WINDOW is not resizable.
1362 DELTA positive means WINDOW shall be enlarged by DELTA lines or
1363 columns. If WINDOW cannot be enlarged by DELTA lines or columns
1364 return the maximum value in the range 0..DELTA by which WINDOW
1365 can be enlarged.
1367 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
1368 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
1369 return the minimum value in the range DELTA..0 that can be used
1370 for shrinking WINDOW.
1372 Optional argument IGNORE non-nil means ignore restrictions
1373 imposed by fixed size windows, `window-min-height' or
1374 `window-min-width' settings. If IGNORE is a window, ignore
1375 restrictions for that window only. If IGNORE equals `safe',
1376 live windows may get as small as `window-safe-min-height' lines
1377 and `window-safe-min-width' columns. Any other non-nil value
1378 means ignore all of the above restrictions for all windows."
1379 (setq window (window-normalize-window window))
1380 (window--resizable window delta horizontal ignore))
1382 (defun window-total-size (&optional window horizontal)
1383 "Return the total height or width of WINDOW.
1384 WINDOW must be a valid window and defaults to the selected one.
1386 If HORIZONTAL is omitted or nil, return the total height of
1387 WINDOW, in lines, like `window-total-height'. Otherwise return
1388 the total width, in columns, like `window-total-width'."
1389 (if horizontal
1390 (window-total-width window)
1391 (window-total-height window)))
1393 ;; Eventually we should make `window-height' obsolete.
1394 (defalias 'window-height 'window-total-height)
1396 ;; See discussion in bug#4543.
1397 (defun window-full-height-p (&optional window)
1398 "Return t if WINDOW is as high as its containing frame.
1399 More precisely, return t if and only if the total height of
1400 WINDOW equals the total height of the root window of WINDOW's
1401 frame. WINDOW must be a valid window and defaults to the
1402 selected one."
1403 (setq window (window-normalize-window window))
1404 (= (window-total-size window)
1405 (window-total-size (frame-root-window window))))
1407 (defun window-full-width-p (&optional window)
1408 "Return t if WINDOW is as wide as its containing frame.
1409 More precisely, return t if and only if the total width of WINDOW
1410 equals the total width of the root window of WINDOW's frame.
1411 WINDOW must be a valid window and defaults to the selected one."
1412 (setq window (window-normalize-window window))
1413 (= (window-total-size window t)
1414 (window-total-size (frame-root-window window) t)))
1416 (defun window-body-size (&optional window horizontal)
1417 "Return the height or width of WINDOW's text area.
1418 WINDOW must be a live window and defaults to the selected one.
1420 If HORIZONTAL is omitted or nil, return the height of the text
1421 area, like `window-body-height'. Otherwise, return the width of
1422 the text area, like `window-body-width'."
1423 (if horizontal
1424 (window-body-width window)
1425 (window-body-height window)))
1427 ;; Eventually we should make `window-height' obsolete.
1428 (defalias 'window-width 'window-body-width)
1430 (defun window-current-scroll-bars (&optional window)
1431 "Return the current scroll bar settings for WINDOW.
1432 WINDOW must be a live window and defaults to the selected one.
1434 The return value is a cons cell (VERTICAL . HORIZONTAL) where
1435 VERTICAL specifies the current location of the vertical scroll
1436 bars (`left', `right', or nil), and HORIZONTAL specifies the
1437 current location of the horizontal scroll bars (`top', `bottom',
1438 or nil).
1440 Unlike `window-scroll-bars', this function reports the scroll bar
1441 type actually used, once frame defaults and `scroll-bar-mode' are
1442 taken into account."
1443 (setq window (window-normalize-window window t))
1444 (let ((vert (nth 2 (window-scroll-bars window)))
1445 (hor nil))
1446 (when (or (eq vert t) (eq hor t))
1447 (let ((fcsb (frame-current-scroll-bars (window-frame window))))
1448 (if (eq vert t)
1449 (setq vert (car fcsb)))
1450 (if (eq hor t)
1451 (setq hor (cdr fcsb)))))
1452 (cons vert hor)))
1454 (defun walk-windows (fun &optional minibuf all-frames)
1455 "Cycle through all live windows, calling FUN for each one.
1456 FUN must specify a function with a window as its sole argument.
1457 The optional arguments MINIBUF and ALL-FRAMES specify the set of
1458 windows to include in the walk.
1460 MINIBUF t means include the minibuffer window even if the
1461 minibuffer is not active. MINIBUF nil or omitted means include
1462 the minibuffer window only if the minibuffer is active. Any
1463 other value means do not include the minibuffer window even if
1464 the minibuffer is active.
1466 ALL-FRAMES nil or omitted means consider all windows on the
1467 selected frame, plus the minibuffer window if specified by the
1468 MINIBUF argument. If the minibuffer counts, consider all windows
1469 on all frames that share that minibuffer too. The following
1470 non-nil values of ALL-FRAMES have special meanings:
1472 - t means consider all windows on all existing frames.
1474 - `visible' means consider all windows on all visible frames on
1475 the current terminal.
1477 - 0 (the number zero) means consider all windows on all visible
1478 and iconified frames on the current terminal.
1480 - A frame means consider all windows on that frame only.
1482 Anything else means consider all windows on the selected frame
1483 and no others.
1485 This function changes neither the order of recently selected
1486 windows nor the buffer list."
1487 ;; If we start from the minibuffer window, don't fail to come
1488 ;; back to it.
1489 (when (window-minibuffer-p (selected-window))
1490 (setq minibuf t))
1491 ;; Make sure to not mess up the order of recently selected
1492 ;; windows. Use `save-selected-window' and `select-window'
1493 ;; with second argument non-nil for this purpose.
1494 (save-selected-window
1495 (when (framep all-frames)
1496 (select-window (frame-first-window all-frames) 'norecord))
1497 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
1498 (funcall fun walk-windows-window))))
1500 (defun window-at-side-p (&optional window side)
1501 "Return t if WINDOW is at SIDE of its containing frame.
1502 WINDOW must be a valid window and defaults to the selected one.
1503 SIDE can be any of the symbols `left', `top', `right' or
1504 `bottom'. The default value nil is handled like `bottom'."
1505 (setq window (window-normalize-window window))
1506 (let ((edge
1507 (cond
1508 ((eq side 'left) 0)
1509 ((eq side 'top) 1)
1510 ((eq side 'right) 2)
1511 ((memq side '(bottom nil)) 3))))
1512 (= (nth edge (window-edges window))
1513 (nth edge (window-edges (frame-root-window window))))))
1515 (defun window-at-side-list (&optional frame side)
1516 "Return list of all windows on SIDE of FRAME.
1517 FRAME must be a live frame and defaults to the selected frame.
1518 SIDE can be any of the symbols `left', `top', `right' or
1519 `bottom'. The default value nil is handled like `bottom'."
1520 (setq frame (window-normalize-frame frame))
1521 (let (windows)
1522 (walk-window-tree
1523 (lambda (window)
1524 (when (window-at-side-p window side)
1525 (setq windows (cons window windows))))
1526 frame nil 'nomini)
1527 (nreverse windows)))
1529 (defun window--in-direction-2 (window posn &optional horizontal)
1530 "Support function for `window-in-direction'."
1531 (if horizontal
1532 (let ((top (window-top-line window)))
1533 (if (> top posn)
1534 (- top posn)
1535 (- posn top (window-total-height window))))
1536 (let ((left (window-left-column window)))
1537 (if (> left posn)
1538 (- left posn)
1539 (- posn left (window-total-width window))))))
1541 ;; Predecessors to the below have been devised by Julian Assange in
1542 ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
1543 ;; Neither of these allow to selectively ignore specific windows
1544 ;; (windows whose `no-other-window' parameter is non-nil) as targets of
1545 ;; the movement.
1546 (defun window-in-direction (direction &optional window ignore)
1547 "Return window in DIRECTION as seen from WINDOW.
1548 More precisely, return the nearest window in direction DIRECTION
1549 as seen from the position of `window-point' in window WINDOW.
1550 DIRECTION must be one of `above', `below', `left' or `right'.
1551 WINDOW must be a live window and defaults to the selected one.
1553 Do not return a window whose `no-other-window' parameter is
1554 non-nil. If the nearest window's `no-other-window' parameter is
1555 non-nil, try to find another window in the indicated direction.
1556 If, however, the optional argument IGNORE is non-nil, return that
1557 window even if its `no-other-window' parameter is non-nil.
1559 Return nil if no suitable window can be found."
1560 (setq window (window-normalize-window window t))
1561 (unless (memq direction '(above below left right))
1562 (error "Wrong direction %s" direction))
1563 (let* ((frame (window-frame window))
1564 (hor (memq direction '(left right)))
1565 (first (if hor
1566 (window-left-column window)
1567 (window-top-line window)))
1568 (last (+ first (if hor
1569 (window-total-width window)
1570 (window-total-height window))))
1571 (posn-cons (nth 6 (posn-at-point (window-point window) window)))
1572 ;; The column / row value of `posn-at-point' can be nil for the
1573 ;; mini-window, guard against that.
1574 (posn (if hor
1575 (+ (or (cdr posn-cons) 1) (window-top-line window))
1576 (+ (or (car posn-cons) 1) (window-left-column window))))
1577 (best-edge
1578 (cond
1579 ((eq direction 'below) (frame-height frame))
1580 ((eq direction 'right) (frame-width frame))
1581 (t -1)))
1582 (best-edge-2 best-edge)
1583 (best-diff-2 (if hor (frame-height frame) (frame-width frame)))
1584 best best-2 best-diff-2-new)
1585 (walk-window-tree
1586 (lambda (w)
1587 (let* ((w-top (window-top-line w))
1588 (w-left (window-left-column w)))
1589 (cond
1590 ((or (eq window w)
1591 ;; Ignore ourselves.
1592 (and (window-parameter w 'no-other-window)
1593 ;; Ignore W unless IGNORE is non-nil.
1594 (not ignore))))
1595 (hor
1596 (cond
1597 ((and (<= w-top posn)
1598 (< posn (+ w-top (window-total-height w))))
1599 ;; W is to the left or right of WINDOW and covers POSN.
1600 (when (or (and (eq direction 'left)
1601 (<= w-left first) (> w-left best-edge))
1602 (and (eq direction 'right)
1603 (>= w-left last) (< w-left best-edge)))
1604 (setq best-edge w-left)
1605 (setq best w)))
1606 ((and (or (and (eq direction 'left)
1607 (<= (+ w-left (window-total-width w)) first))
1608 (and (eq direction 'right) (<= last w-left)))
1609 ;; W is to the left or right of WINDOW but does not
1610 ;; cover POSN.
1611 (setq best-diff-2-new
1612 (window--in-direction-2 w posn hor))
1613 (or (< best-diff-2-new best-diff-2)
1614 (and (= best-diff-2-new best-diff-2)
1615 (if (eq direction 'left)
1616 (> w-left best-edge-2)
1617 (< w-left best-edge-2)))))
1618 (setq best-edge-2 w-left)
1619 (setq best-diff-2 best-diff-2-new)
1620 (setq best-2 w))))
1622 (cond
1623 ((and (<= w-left posn)
1624 (< posn (+ w-left (window-total-width w))))
1625 ;; W is above or below WINDOW and covers POSN.
1626 (when (or (and (eq direction 'above)
1627 (<= w-top first) (> w-top best-edge))
1628 (and (eq direction 'below)
1629 (>= w-top first) (< w-top best-edge)))
1630 (setq best-edge w-top)
1631 (setq best w)))
1632 ((and (or (and (eq direction 'above)
1633 (<= (+ w-top (window-total-height w)) first))
1634 (and (eq direction 'below) (<= last w-top)))
1635 ;; W is above or below WINDOW but does not cover POSN.
1636 (setq best-diff-2-new
1637 (window--in-direction-2 w posn hor))
1638 (or (< best-diff-2-new best-diff-2)
1639 (and (= best-diff-2-new best-diff-2)
1640 (if (eq direction 'above)
1641 (> w-top best-edge-2)
1642 (< w-top best-edge-2)))))
1643 (setq best-edge-2 w-top)
1644 (setq best-diff-2 best-diff-2-new)
1645 (setq best-2 w)))))))
1646 frame)
1647 (or best best-2)))
1649 (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
1650 "Return a live window satisfying PREDICATE.
1651 More precisely, cycle through all windows calling the function
1652 PREDICATE on each one of them with the window as its sole
1653 argument. Return the first window for which PREDICATE returns
1654 non-nil. Windows are scanned starting with the window following
1655 the selected window. If no window satisfies PREDICATE, return
1656 DEFAULT.
1658 MINIBUF t means include the minibuffer window even if the
1659 minibuffer is not active. MINIBUF nil or omitted means include
1660 the minibuffer window only if the minibuffer is active. Any
1661 other value means do not include the minibuffer window even if
1662 the minibuffer is active.
1664 ALL-FRAMES nil or omitted means consider all windows on the selected
1665 frame, plus the minibuffer window if specified by the MINIBUF
1666 argument. If the minibuffer counts, consider all windows on all
1667 frames that share that minibuffer too. The following non-nil
1668 values of ALL-FRAMES have special meanings:
1670 - t means consider all windows on all existing frames.
1672 - `visible' means consider all windows on all visible frames on
1673 the current terminal.
1675 - 0 (the number zero) means consider all windows on all visible
1676 and iconified frames on the current terminal.
1678 - A frame means consider all windows on that frame only.
1680 Anything else means consider all windows on the selected frame
1681 and no others."
1682 (catch 'found
1683 (dolist (window (window-list-1
1684 (next-window nil minibuf all-frames)
1685 minibuf all-frames))
1686 (when (funcall predicate window)
1687 (throw 'found window)))
1688 default))
1690 (defalias 'some-window 'get-window-with-predicate)
1692 (defun get-lru-window (&optional all-frames dedicated not-selected)
1693 "Return the least recently used window on frames specified by ALL-FRAMES.
1694 Return a full-width window if possible. A minibuffer window is
1695 never a candidate. A dedicated window is never a candidate
1696 unless DEDICATED is non-nil, so if all windows are dedicated, the
1697 value is nil. Avoid returning the selected window if possible.
1698 Optional argument NOT-SELECTED non-nil means never return the
1699 selected window.
1701 The following non-nil values of the optional argument ALL-FRAMES
1702 have special meanings:
1704 - t means consider all windows on all existing frames.
1706 - `visible' means consider all windows on all visible frames on
1707 the current terminal.
1709 - 0 (the number zero) means consider all windows on all visible
1710 and iconified frames on the current terminal.
1712 - A frame means consider all windows on that frame only.
1714 Any other value of ALL-FRAMES means consider all windows on the
1715 selected frame and no others."
1716 (let (best-window best-time second-best-window second-best-time time)
1717 (dolist (window (window-list-1 nil 'nomini all-frames))
1718 (when (and (or dedicated (not (window-dedicated-p window)))
1719 (or (not not-selected) (not (eq window (selected-window)))))
1720 (setq time (window-use-time window))
1721 (if (or (eq window (selected-window))
1722 (not (window-full-width-p window)))
1723 (when (or (not second-best-time) (< time second-best-time))
1724 (setq second-best-time time)
1725 (setq second-best-window window))
1726 (when (or (not best-time) (< time best-time))
1727 (setq best-time time)
1728 (setq best-window window)))))
1729 (or best-window second-best-window)))
1731 (defun get-mru-window (&optional all-frames dedicated not-selected)
1732 "Return the most recently used window on frames specified by ALL-FRAMES.
1733 A minibuffer window is never a candidate. A dedicated window is
1734 never a candidate unless DEDICATED is non-nil, so if all windows
1735 are dedicated, the value is nil. Optional argument NOT-SELECTED
1736 non-nil means never return the selected window.
1738 The following non-nil values of the optional argument ALL-FRAMES
1739 have special meanings:
1741 - t means consider all windows on all existing frames.
1743 - `visible' means consider all windows on all visible frames on
1744 the current terminal.
1746 - 0 (the number zero) means consider all windows on all visible
1747 and iconified frames on the current terminal.
1749 - A frame means consider all windows on that frame only.
1751 Any other value of ALL-FRAMES means consider all windows on the
1752 selected frame and no others."
1753 (let (best-window best-time time)
1754 (dolist (window (window-list-1 nil 'nomini all-frames))
1755 (setq time (window-use-time window))
1756 (when (and (or dedicated (not (window-dedicated-p window)))
1757 (or (not not-selected) (not (eq window (selected-window))))
1758 (or (not best-time) (> time best-time)))
1759 (setq best-time time)
1760 (setq best-window window)))
1761 best-window))
1763 (defun get-largest-window (&optional all-frames dedicated not-selected)
1764 "Return the largest window on frames specified by ALL-FRAMES.
1765 A minibuffer window is never a candidate. A dedicated window is
1766 never a candidate unless DEDICATED is non-nil, so if all windows
1767 are dedicated, the value is nil. Optional argument NOT-SELECTED
1768 non-nil means never return the selected window.
1770 The following non-nil values of the optional argument ALL-FRAMES
1771 have special meanings:
1773 - t means consider all windows on all existing frames.
1775 - `visible' means consider all windows on all visible frames on
1776 the current terminal.
1778 - 0 (the number zero) means consider all windows on all visible
1779 and iconified frames on the current terminal.
1781 - A frame means consider all windows on that frame only.
1783 Any other value of ALL-FRAMES means consider all windows on the
1784 selected frame and no others."
1785 (let ((best-size 0)
1786 best-window size)
1787 (dolist (window (window-list-1 nil 'nomini all-frames))
1788 (when (and (or dedicated (not (window-dedicated-p window)))
1789 (or (not not-selected) (not (eq window (selected-window)))))
1790 (setq size (* (window-total-size window)
1791 (window-total-size window t)))
1792 (when (> size best-size)
1793 (setq best-size size)
1794 (setq best-window window))))
1795 best-window))
1797 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
1798 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
1799 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
1800 and defaults to the current buffer. Windows are scanned starting
1801 with the selected window.
1803 MINIBUF t means include the minibuffer window even if the
1804 minibuffer is not active. MINIBUF nil or omitted means include
1805 the minibuffer window only if the minibuffer is active. Any
1806 other value means do not include the minibuffer window even if
1807 the minibuffer is active.
1809 ALL-FRAMES nil or omitted means consider all windows on the
1810 selected frame, plus the minibuffer window if specified by the
1811 MINIBUF argument. If the minibuffer counts, consider all windows
1812 on all frames that share that minibuffer too. The following
1813 non-nil values of ALL-FRAMES have special meanings:
1815 - t means consider all windows on all existing frames.
1817 - `visible' means consider all windows on all visible frames on
1818 the current terminal.
1820 - 0 (the number zero) means consider all windows on all visible
1821 and iconified frames on the current terminal.
1823 - A frame means consider all windows on that frame only.
1825 Anything else means consider all windows on the selected frame
1826 and no others."
1827 (let ((buffer (window-normalize-buffer buffer-or-name))
1828 windows)
1829 (dolist (window (window-list-1 (selected-window) minibuf all-frames))
1830 (when (eq (window-buffer window) buffer)
1831 (setq windows (cons window windows))))
1832 (nreverse windows)))
1834 (defun minibuffer-window-active-p (window)
1835 "Return t if WINDOW is the currently active minibuffer window."
1836 (eq window (active-minibuffer-window)))
1838 (defun count-windows (&optional minibuf)
1839 "Return the number of live windows on the selected frame.
1840 The optional argument MINIBUF specifies whether the minibuffer
1841 window shall be counted. See `walk-windows' for the precise
1842 meaning of this argument."
1843 (length (window-list-1 nil minibuf)))
1845 ;;; Resizing windows.
1846 (defun window--resize-reset (&optional frame horizontal)
1847 "Reset resize values for all windows on FRAME.
1848 FRAME defaults to the selected frame.
1850 This function stores the current value of `window-total-size' applied
1851 with argument HORIZONTAL in the new total size of all windows on
1852 FRAME. It also resets the new normal size of each of these
1853 windows."
1854 (window--resize-reset-1
1855 (frame-root-window (window-normalize-frame frame)) horizontal))
1857 (defun window--resize-reset-1 (window horizontal)
1858 "Internal function of `window--resize-reset'."
1859 ;; Register old size in the new total size.
1860 (set-window-new-total window (window-total-size window horizontal))
1861 ;; Reset new normal size.
1862 (set-window-new-normal window)
1863 (when (window-child window)
1864 (window--resize-reset-1 (window-child window) horizontal))
1865 (when (window-right window)
1866 (window--resize-reset-1 (window-right window) horizontal)))
1868 ;; The following routine is used to manually resize the minibuffer
1869 ;; window and is currently used, for example, by ispell.el.
1870 (defun window--resize-mini-window (window delta)
1871 "Resize minibuffer window WINDOW by DELTA lines.
1872 If WINDOW cannot be resized by DELTA lines make it as large (or
1873 as small) as possible, but don't signal an error."
1874 (when (window-minibuffer-p window)
1875 (let* ((frame (window-frame window))
1876 (root (frame-root-window frame))
1877 (height (window-total-size window))
1878 (min-delta
1879 (- (window-total-size root)
1880 (window-min-size root))))
1881 ;; Sanitize DELTA.
1882 (cond
1883 ((<= (+ height delta) 0)
1884 (setq delta (- (- height 1))))
1885 ((> delta min-delta)
1886 (setq delta min-delta)))
1888 ;; Resize now.
1889 (window--resize-reset frame)
1890 ;; Ideally we should be able to resize just the last child of root
1891 ;; here. See the comment in `resize-root-window-vertically' for
1892 ;; why we do not do that.
1893 (window--resize-this-window root (- delta) nil nil t)
1894 (set-window-new-total window (+ height delta))
1895 ;; The following routine catches the case where we want to resize
1896 ;; a minibuffer-only frame.
1897 (resize-mini-window-internal window))))
1899 (defun window-resize (window delta &optional horizontal ignore)
1900 "Resize WINDOW vertically by DELTA lines.
1901 WINDOW can be an arbitrary window and defaults to the selected
1902 one. An attempt to resize the root window of a frame will raise
1903 an error though.
1905 DELTA a positive number means WINDOW shall be enlarged by DELTA
1906 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
1907 lines.
1909 Optional argument HORIZONTAL non-nil means resize WINDOW
1910 horizontally by DELTA columns. In this case a positive DELTA
1911 means enlarge WINDOW by DELTA columns. DELTA negative means
1912 WINDOW shall be shrunk by -DELTA columns.
1914 Optional argument IGNORE non-nil means ignore restrictions
1915 imposed by fixed size windows, `window-min-height' or
1916 `window-min-width' settings. If IGNORE is a window, ignore
1917 restrictions for that window only. If IGNORE equals `safe',
1918 live windows may get as small as `window-safe-min-height' lines
1919 and `window-safe-min-width' columns. Any other non-nil value
1920 means ignore all of the above restrictions for all windows.
1922 This function resizes other windows proportionally and never
1923 deletes any windows. If you want to move only the low (right)
1924 edge of WINDOW consider using `adjust-window-trailing-edge'
1925 instead."
1926 (setq window (window-normalize-window window))
1927 (let* ((frame (window-frame window))
1928 (minibuffer-window (minibuffer-window frame))
1929 sibling)
1930 (cond
1931 ((eq window (frame-root-window frame))
1932 (error "Cannot resize the root window of a frame"))
1933 ((window-minibuffer-p window)
1934 (if horizontal
1935 (error "Cannot resize minibuffer window horizontally")
1936 (window--resize-mini-window window delta)))
1937 ((and (not horizontal)
1938 (window-full-height-p window)
1939 (eq (window-frame minibuffer-window) frame)
1940 (or (not resize-mini-windows)
1941 (eq minibuffer-window (active-minibuffer-window))))
1942 ;; If WINDOW is full height and either `resize-mini-windows' is
1943 ;; nil or the minibuffer window is active, resize the minibuffer
1944 ;; window.
1945 (window--resize-mini-window minibuffer-window (- delta)))
1946 ((window--resizable-p window delta horizontal ignore)
1947 (window--resize-reset frame horizontal)
1948 (window--resize-this-window window delta horizontal ignore t)
1949 (if (and (not window-combination-resize)
1950 (window-combined-p window horizontal)
1951 (setq sibling (or (window-right window) (window-left window)))
1952 (window-sizable-p sibling (- delta) horizontal ignore))
1953 ;; If window-combination-resize is nil, WINDOW is part of an
1954 ;; iso-combination, and WINDOW's neighboring right or left
1955 ;; sibling can be resized as requested, resize that sibling.
1956 (let ((normal-delta
1957 (/ (float delta)
1958 (window-total-size (window-parent window) horizontal))))
1959 (window--resize-this-window sibling (- delta) horizontal nil t)
1960 (set-window-new-normal
1961 window (+ (window-normal-size window horizontal)
1962 normal-delta))
1963 (set-window-new-normal
1964 sibling (- (window-normal-size sibling horizontal)
1965 normal-delta)))
1966 ;; Otherwise, resize all other windows in the same combination.
1967 (window--resize-siblings window delta horizontal ignore))
1968 (window-resize-apply frame horizontal))
1970 (error "Cannot resize window %s" window)))))
1972 (defun window--resize-child-windows-skip-p (window)
1973 "Return non-nil if WINDOW shall be skipped by resizing routines."
1974 (memq (window-new-normal window) '(ignore stuck skip)))
1976 (defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
1977 "Recursively set new normal height of child windows of window PARENT.
1978 HORIZONTAL non-nil means set the new normal width of these
1979 windows. WINDOW specifies a child window of PARENT that has been
1980 resized by THIS-DELTA lines (columns).
1982 Optional argument TRAIL either `before' or `after' means set values
1983 only for windows before or after WINDOW. Optional argument
1984 OTHER-DELTA, a number, specifies that this many lines (columns)
1985 have been obtained from (or returned to) an ancestor window of
1986 PARENT in order to resize WINDOW."
1987 (let* ((delta-normal
1988 (if (and (= (- this-delta) (window-total-size window horizontal))
1989 (zerop other-delta))
1990 ;; When WINDOW gets deleted and we can return its entire
1991 ;; space to its siblings, use WINDOW's normal size as the
1992 ;; normal delta.
1993 (- (window-normal-size window horizontal))
1994 ;; In any other case calculate the normal delta from the
1995 ;; relation of THIS-DELTA to the total size of PARENT.
1996 (/ (float this-delta) (window-total-size parent horizontal))))
1997 (sub (window-child parent))
1998 (parent-normal 0.0)
1999 (skip (eq trail 'after)))
2001 ;; Set parent-normal to the sum of the normal sizes of all child
2002 ;; windows of PARENT that shall be resized, excluding only WINDOW
2003 ;; and any windows specified by the optional TRAIL argument.
2004 (while sub
2005 (cond
2006 ((eq sub window)
2007 (setq skip (eq trail 'before)))
2008 (skip)
2010 (setq parent-normal
2011 (+ parent-normal (window-normal-size sub horizontal)))))
2012 (setq sub (window-right sub)))
2014 ;; Set the new normal size of all child windows of PARENT from what
2015 ;; they should have contributed for recovering THIS-DELTA lines
2016 ;; (columns).
2017 (setq sub (window-child parent))
2018 (setq skip (eq trail 'after))
2019 (while sub
2020 (cond
2021 ((eq sub window)
2022 (setq skip (eq trail 'before)))
2023 (skip)
2025 (let ((old-normal (window-normal-size sub horizontal)))
2026 (set-window-new-normal
2027 sub (min 1.0 ; Don't get larger than 1.
2028 (max (- old-normal
2029 (* (/ old-normal parent-normal)
2030 delta-normal))
2031 ;; Don't drop below 0.
2032 0.0))))))
2033 (setq sub (window-right sub)))
2035 (when (numberp other-delta)
2036 ;; Set the new normal size of windows from what they should have
2037 ;; contributed for recovering OTHER-DELTA lines (columns).
2038 (setq delta-normal (/ (float (window-total-size parent horizontal))
2039 (+ (window-total-size parent horizontal)
2040 other-delta)))
2041 (setq sub (window-child parent))
2042 (setq skip (eq trail 'after))
2043 (while sub
2044 (cond
2045 ((eq sub window)
2046 (setq skip (eq trail 'before)))
2047 (skip)
2049 (set-window-new-normal
2050 sub (min 1.0 ; Don't get larger than 1.
2051 (max (* (window-new-normal sub) delta-normal)
2052 ;; Don't drop below 0.
2053 0.0)))))
2054 (setq sub (window-right sub))))
2056 ;; Set the new normal size of WINDOW to what is left by the sum of
2057 ;; the normal sizes of its siblings.
2058 (set-window-new-normal
2059 window
2060 (let ((sum 0))
2061 (setq sub (window-child parent))
2062 (while sub
2063 (cond
2064 ((eq sub window))
2065 ((not (numberp (window-new-normal sub)))
2066 (setq sum (+ sum (window-normal-size sub horizontal))))
2068 (setq sum (+ sum (window-new-normal sub)))))
2069 (setq sub (window-right sub)))
2070 ;; Don't get larger than 1 or smaller than 0.
2071 (min 1.0 (max (- 1.0 sum) 0.0))))))
2073 (defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge)
2074 "Resize child windows of window PARENT vertically by DELTA lines.
2075 PARENT must be a vertically combined internal window.
2077 Optional argument HORIZONTAL non-nil means resize child windows of
2078 PARENT horizontally by DELTA columns. In this case PARENT must
2079 be a horizontally combined internal window.
2081 WINDOW, if specified, must denote a child window of PARENT that
2082 is resized by DELTA lines.
2084 Optional argument IGNORE non-nil means ignore restrictions
2085 imposed by fixed size windows, `window-min-height' or
2086 `window-min-width' settings. If IGNORE equals `safe', live
2087 windows may get as small as `window-safe-min-height' lines and
2088 `window-safe-min-width' columns. If IGNORE is a window, ignore
2089 restrictions for that window only. Any other non-nil value means
2090 ignore all of the above restrictions for all windows.
2092 Optional arguments TRAIL and EDGE, when non-nil, restrict the set
2093 of windows that shall be resized. If TRAIL equals `before',
2094 resize only windows on the left or above EDGE. If TRAIL equals
2095 `after', resize only windows on the right or below EDGE. Also,
2096 preferably only resize windows adjacent to EDGE.
2098 Return the symbol `normalized' if new normal sizes have been
2099 already set by this routine."
2100 (let* ((first (window-child parent))
2101 (last (window-last-child parent))
2102 (parent-total (+ (window-total-size parent horizontal) delta))
2103 sub best-window best-value)
2105 (if (and edge (memq trail '(before after))
2106 (progn
2107 (setq sub first)
2108 (while (and (window-right sub)
2109 (or (and (eq trail 'before)
2110 (not (window--resize-child-windows-skip-p
2111 (window-right sub))))
2112 (and (eq trail 'after)
2113 (window--resize-child-windows-skip-p sub))))
2114 (setq sub (window-right sub)))
2115 sub)
2116 (if horizontal
2117 (if (eq trail 'before)
2118 (= (+ (window-left-column sub)
2119 (window-total-size sub t))
2120 edge)
2121 (= (window-left-column sub) edge))
2122 (if (eq trail 'before)
2123 (= (+ (window-top-line sub)
2124 (window-total-size sub))
2125 edge)
2126 (= (window-top-line sub) edge)))
2127 (window-sizable-p sub delta horizontal ignore))
2128 ;; Resize only windows adjacent to EDGE.
2129 (progn
2130 (window--resize-this-window
2131 sub delta horizontal ignore t trail edge)
2132 (if (and window (eq (window-parent sub) parent))
2133 (progn
2134 ;; Assign new normal sizes.
2135 (set-window-new-normal
2136 sub (/ (float (window-new-total sub)) parent-total))
2137 (set-window-new-normal
2138 window (- (window-normal-size window horizontal)
2139 (- (window-new-normal sub)
2140 (window-normal-size sub horizontal)))))
2141 (window--resize-child-windows-normal
2142 parent horizontal sub 0 trail delta))
2143 ;; Return 'normalized to notify `window--resize-siblings' that
2144 ;; normal sizes have been already set.
2145 'normalized)
2146 ;; Resize all windows proportionally.
2147 (setq sub last)
2148 (while sub
2149 (cond
2150 ((or (window--resize-child-windows-skip-p sub)
2151 ;; Ignore windows to skip and fixed-size child windows -
2152 ;; in the latter case make it a window to skip.
2153 (and (not ignore)
2154 (window-size-fixed-p sub horizontal)
2155 (set-window-new-normal sub 'ignore))))
2156 ((< delta 0)
2157 ;; When shrinking store the number of lines/cols we can get
2158 ;; from this window here together with the total/normal size
2159 ;; factor.
2160 (set-window-new-normal
2162 (cons
2163 ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
2164 (window-min-delta sub horizontal ignore trail t) ; t)
2165 (- (/ (float (window-total-size sub horizontal))
2166 parent-total)
2167 (window-normal-size sub horizontal)))))
2168 ((> delta 0)
2169 ;; When enlarging store the total/normal size factor only
2170 (set-window-new-normal
2172 (- (/ (float (window-total-size sub horizontal))
2173 parent-total)
2174 (window-normal-size sub horizontal)))))
2176 (setq sub (window-left sub)))
2178 (cond
2179 ((< delta 0)
2180 ;; Shrink windows by delta.
2181 (setq best-window t)
2182 (while (and best-window (not (zerop delta)))
2183 (setq sub last)
2184 (setq best-window nil)
2185 (setq best-value most-negative-fixnum)
2186 (while sub
2187 (when (and (consp (window-new-normal sub))
2188 (not (zerop (car (window-new-normal sub))))
2189 (> (cdr (window-new-normal sub)) best-value))
2190 (setq best-window sub)
2191 (setq best-value (cdr (window-new-normal sub))))
2193 (setq sub (window-left sub)))
2195 (when best-window
2196 (setq delta (1+ delta)))
2197 (set-window-new-total best-window -1 t)
2198 (set-window-new-normal
2199 best-window
2200 (if (= (car (window-new-normal best-window)) 1)
2201 'skip ; We can't shrink best-window any further.
2202 (cons (1- (car (window-new-normal best-window)))
2203 (- (/ (float (window-new-total best-window))
2204 parent-total)
2205 (window-normal-size best-window horizontal)))))))
2206 ((> delta 0)
2207 ;; Enlarge windows by delta.
2208 (setq best-window t)
2209 (while (and best-window (not (zerop delta)))
2210 (setq sub last)
2211 (setq best-window nil)
2212 (setq best-value most-positive-fixnum)
2213 (while sub
2214 (when (and (numberp (window-new-normal sub))
2215 (< (window-new-normal sub) best-value))
2216 (setq best-window sub)
2217 (setq best-value (window-new-normal sub)))
2219 (setq sub (window-left sub)))
2221 (when best-window
2222 (setq delta (1- delta)))
2223 (set-window-new-total best-window 1 t)
2224 (set-window-new-normal
2225 best-window
2226 (- (/ (float (window-new-total best-window))
2227 parent-total)
2228 (window-normal-size best-window horizontal))))))
2230 (when best-window
2231 (setq sub last)
2232 (while sub
2233 (when (or (consp (window-new-normal sub))
2234 (numberp (window-new-normal sub)))
2235 ;; Reset new normal size fields so `window-resize-apply'
2236 ;; won't use them to apply new sizes.
2237 (set-window-new-normal sub))
2239 (unless (eq (window-new-normal sub) 'ignore)
2240 ;; Resize this window's child windows (back-engineering
2241 ;; delta from sub's old and new total sizes).
2242 (let ((delta (- (window-new-total sub)
2243 (window-total-size sub horizontal))))
2244 (unless (and (zerop delta) (not trail))
2245 ;; For the TRAIL non-nil case we have to resize SUB
2246 ;; recursively even if it's size does not change.
2247 (window--resize-this-window
2248 sub delta horizontal ignore nil trail edge))))
2249 (setq sub (window-left sub)))))))
2251 (defun window--resize-siblings (window delta &optional horizontal ignore trail edge)
2252 "Resize other windows when WINDOW is resized vertically by DELTA lines.
2253 Optional argument HORIZONTAL non-nil means resize other windows
2254 when WINDOW is resized horizontally by DELTA columns. WINDOW
2255 itself is not resized by this function.
2257 Optional argument IGNORE non-nil means ignore restrictions
2258 imposed by fixed size windows, `window-min-height' or
2259 `window-min-width' settings. If IGNORE equals `safe', live
2260 windows may get as small as `window-safe-min-height' lines and
2261 `window-safe-min-width' columns. If IGNORE is a window, ignore
2262 restrictions for that window only. Any other non-nil value means
2263 ignore all of the above restrictions for all windows.
2265 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2266 of windows that shall be resized. If TRAIL equals `before',
2267 resize only windows on the left or above EDGE. If TRAIL equals
2268 `after', resize only windows on the right or below EDGE. Also,
2269 preferably only resize windows adjacent to EDGE."
2270 (when (window-parent window)
2271 (let* ((parent (window-parent window))
2272 (sub (window-child parent)))
2273 (if (window-combined-p sub horizontal)
2274 ;; In an iso-combination try to extract DELTA from WINDOW's
2275 ;; siblings.
2276 (let ((skip (eq trail 'after))
2277 this-delta other-delta)
2278 ;; Decide which windows shall be left alone.
2279 (while sub
2280 (cond
2281 ((eq sub window)
2282 ;; Make sure WINDOW is left alone when
2283 ;; resizing its siblings.
2284 (set-window-new-normal sub 'ignore)
2285 (setq skip (eq trail 'before)))
2286 (skip
2287 ;; Make sure this sibling is left alone when
2288 ;; resizing its siblings.
2289 (set-window-new-normal sub 'ignore))
2290 ((or (window--size-ignore-p sub ignore)
2291 (not (window-size-fixed-p sub horizontal)))
2292 ;; Set this-delta to t to signal that we found a sibling
2293 ;; of WINDOW whose size is not fixed.
2294 (setq this-delta t)))
2296 (setq sub (window-right sub)))
2298 ;; Set this-delta to what we can get from WINDOW's siblings.
2299 (if (= (- delta) (window-total-size window horizontal))
2300 ;; A deletion, presumably. We must handle this case
2301 ;; specially since `window--resizable' can't be used.
2302 (if this-delta
2303 ;; There's at least one resizable sibling we can
2304 ;; give WINDOW's size to.
2305 (setq this-delta delta)
2306 ;; No resizable sibling exists.
2307 (setq this-delta 0))
2308 ;; Any other form of resizing.
2309 (setq this-delta
2310 (window--resizable window delta horizontal ignore trail t)))
2312 ;; Set other-delta to what we still have to get from
2313 ;; ancestor windows of parent.
2314 (setq other-delta (- delta this-delta))
2315 (unless (zerop other-delta)
2316 ;; Unless we got everything from WINDOW's siblings, PARENT
2317 ;; must be resized by other-delta lines or columns.
2318 (set-window-new-total parent other-delta 'add))
2320 (if (zerop this-delta)
2321 ;; We haven't got anything from WINDOW's siblings but we
2322 ;; must update the normal sizes to respect other-delta.
2323 (window--resize-child-windows-normal
2324 parent horizontal window this-delta trail other-delta)
2325 ;; We did get something from WINDOW's siblings which means
2326 ;; we have to resize their child windows.
2327 (unless (eq (window--resize-child-windows
2328 parent (- this-delta) horizontal
2329 window ignore trail edge)
2330 ;; If `window--resize-child-windows' returns
2331 ;; 'normalized, this means it has set the
2332 ;; normal sizes already.
2333 'normalized)
2334 ;; Set the normal sizes.
2335 (window--resize-child-windows-normal
2336 parent horizontal window this-delta trail other-delta))
2337 ;; Set DELTA to what we still have to get from ancestor
2338 ;; windows.
2339 (setq delta other-delta)))
2341 ;; In an ortho-combination all siblings of WINDOW must be
2342 ;; resized by DELTA.
2343 (set-window-new-total parent delta 'add)
2344 (while sub
2345 (unless (eq sub window)
2346 (window--resize-this-window sub delta horizontal ignore t))
2347 (setq sub (window-right sub))))
2349 (unless (zerop delta)
2350 ;; "Go up."
2351 (window--resize-siblings
2352 parent delta horizontal ignore trail edge)))))
2354 (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge)
2355 "Resize WINDOW vertically by DELTA lines.
2356 Optional argument HORIZONTAL non-nil means resize WINDOW
2357 horizontally by DELTA columns.
2359 Optional argument IGNORE non-nil means ignore restrictions
2360 imposed by fixed size windows, `window-min-height' or
2361 `window-min-width' settings. If IGNORE equals `safe', live
2362 windows may get as small as `window-safe-min-height' lines and
2363 `window-safe-min-width' columns. If IGNORE is a window, ignore
2364 restrictions for that window only. Any other non-nil value
2365 means ignore all of the above restrictions for all windows.
2367 Optional argument ADD non-nil means add DELTA to the new total
2368 size of WINDOW.
2370 Optional arguments TRAIL and EDGE, when non-nil, refine the set
2371 of windows that shall be resized. If TRAIL equals `before',
2372 resize only windows on the left or above EDGE. If TRAIL equals
2373 `after', resize only windows on the right or below EDGE. Also,
2374 preferably only resize windows adjacent to EDGE.
2376 This function recursively resizes WINDOW's child windows to fit the
2377 new size. Make sure that WINDOW is `window--resizable' before
2378 calling this function. Note that this function does not resize
2379 siblings of WINDOW or WINDOW's parent window. You have to
2380 eventually call `window-resize-apply' in order to make resizing
2381 actually take effect."
2382 (when add
2383 ;; Add DELTA to the new total size of WINDOW.
2384 (set-window-new-total window delta t))
2386 (let ((sub (window-child window)))
2387 (cond
2388 ((not sub))
2389 ((window-combined-p sub horizontal)
2390 ;; In an iso-combination resize child windows according to their
2391 ;; normal sizes.
2392 (window--resize-child-windows
2393 window delta horizontal nil ignore trail edge))
2394 ;; In an ortho-combination resize each child window by DELTA.
2396 (while sub
2397 (window--resize-this-window
2398 sub delta horizontal ignore t trail edge)
2399 (setq sub (window-right sub)))))))
2401 (defun window--resize-root-window (window delta horizontal ignore)
2402 "Resize root window WINDOW vertically by DELTA lines.
2403 HORIZONTAL non-nil means resize root window WINDOW horizontally
2404 by DELTA columns.
2406 IGNORE non-nil means ignore any restrictions imposed by fixed
2407 size windows, `window-min-height' or `window-min-width' settings.
2409 This function is only called by the frame resizing routines. It
2410 resizes windows proportionally and never deletes any windows."
2411 (when (and (windowp window) (numberp delta)
2412 (window-sizable-p window delta horizontal ignore))
2413 (window--resize-reset (window-frame window) horizontal)
2414 (window--resize-this-window window delta horizontal ignore t)))
2416 (defun window--resize-root-window-vertically (window delta)
2417 "Resize root window WINDOW vertically by DELTA lines.
2418 If DELTA is less than zero and we can't shrink WINDOW by DELTA
2419 lines, shrink it as much as possible. If DELTA is greater than
2420 zero, this function can resize fixed-size windows in order to
2421 recover the necessary lines.
2423 Return the number of lines that were recovered.
2425 This function is only called by the minibuffer window resizing
2426 routines. It resizes windows proportionally and never deletes
2427 any windows."
2428 (let ((frame (window-frame window))
2429 ignore)
2430 (cond
2431 ((not (numberp delta))
2432 (setq delta 0))
2433 ((zerop delta))
2434 ((< delta 0)
2435 (setq delta (window-sizable window delta))
2436 (window--resize-reset frame)
2437 ;; When shrinking the root window, emulate an edge drag in order
2438 ;; to not resize other windows if we can avoid it (Bug#12419).
2439 (window--resize-this-window
2440 window delta nil ignore t 'before
2441 (+ (window-top-line window) (window-total-size window)))
2442 ;; Don't record new normal sizes to make sure that shrinking back
2443 ;; proportionally works as intended.
2444 (walk-window-tree
2445 (lambda (window) (set-window-new-normal window 'ignore)) frame t))
2446 ((> delta 0)
2447 (window--resize-reset frame)
2448 (unless (window-sizable window delta)
2449 (setq ignore t))
2450 ;; When growing the root window, resize proportionally. This
2451 ;; should give windows back their original sizes (hopefully).
2452 (window--resize-this-window window delta nil ignore t)))
2453 ;; Return the possibly adjusted DELTA.
2454 delta))
2456 (defun adjust-window-trailing-edge (window delta &optional horizontal)
2457 "Move WINDOW's bottom edge by DELTA lines.
2458 Optional argument HORIZONTAL non-nil means move WINDOW's right
2459 edge by DELTA columns. WINDOW must be a valid window and
2460 defaults to the selected one.
2462 If DELTA is greater than zero, move the edge downwards or to the
2463 right. If DELTA is less than zero, move the edge upwards or to
2464 the left. If the edge can't be moved by DELTA lines or columns,
2465 move it as far as possible in the desired direction."
2466 (setq window (window-normalize-window window))
2467 (let* ((frame (window-frame window))
2468 (minibuffer-window (minibuffer-window frame))
2469 (right window)
2470 left this-delta min-delta max-delta)
2471 ;; Find the edge we want to move.
2472 (while (and (or (not (window-combined-p right horizontal))
2473 (not (window-right right)))
2474 (setq right (window-parent right))))
2475 (cond
2476 ((and (not right) (not horizontal)
2477 ;; Resize the minibuffer window if it's on the same frame as
2478 ;; and immediately below WINDOW and it's either active or
2479 ;; `resize-mini-windows' is nil.
2480 (eq (window-frame minibuffer-window) frame)
2481 (= (nth 1 (window-edges minibuffer-window))
2482 (nth 3 (window-edges window)))
2483 (or (not resize-mini-windows)
2484 (eq minibuffer-window (active-minibuffer-window))))
2485 (window--resize-mini-window minibuffer-window (- delta)))
2486 ((or (not (setq left right)) (not (setq right (window-right right))))
2487 (if horizontal
2488 (error "No window on the right of this one")
2489 (error "No window below this one")))
2491 ;; Set LEFT to the first resizable window on the left. This step is
2492 ;; needed to handle fixed-size windows.
2493 (while (and left (window-size-fixed-p left horizontal))
2494 (setq left
2495 (or (window-left left)
2496 (progn
2497 (while (and (setq left (window-parent left))
2498 (not (window-combined-p left horizontal))))
2499 (window-left left)))))
2500 (unless left
2501 (if horizontal
2502 (error "No resizable window on the left of this one")
2503 (error "No resizable window above this one")))
2505 ;; Set RIGHT to the first resizable window on the right. This step
2506 ;; is needed to handle fixed-size windows.
2507 (while (and right (window-size-fixed-p right horizontal))
2508 (setq right
2509 (or (window-right right)
2510 (progn
2511 (while (and (setq right (window-parent right))
2512 (not (window-combined-p right horizontal))))
2513 (window-right right)))))
2514 (unless right
2515 (if horizontal
2516 (error "No resizable window on the right of this one")
2517 (error "No resizable window below this one")))
2519 ;; LEFT and RIGHT (which might be both internal windows) are now the
2520 ;; two windows we want to resize.
2521 (cond
2522 ((> delta 0)
2523 (setq max-delta (window--max-delta-1 left 0 horizontal nil 'after))
2524 (setq min-delta (window--min-delta-1 right (- delta) horizontal nil 'before))
2525 (when (or (< max-delta delta) (> min-delta (- delta)))
2526 ;; We can't get the whole DELTA - move as far as possible.
2527 (setq delta (min max-delta (- min-delta))))
2528 (unless (zerop delta)
2529 ;; Start resizing.
2530 (window--resize-reset frame horizontal)
2531 ;; Try to enlarge LEFT first.
2532 (setq this-delta (window--resizable left delta horizontal))
2533 (unless (zerop this-delta)
2534 (window--resize-this-window
2535 left this-delta horizontal nil t 'before
2536 (if horizontal
2537 (+ (window-left-column left) (window-total-size left t))
2538 (+ (window-top-line left) (window-total-size left)))))
2539 ;; Shrink windows on right of LEFT.
2540 (window--resize-siblings
2541 left delta horizontal nil 'after
2542 (if horizontal
2543 (window-left-column right)
2544 (window-top-line right)))))
2545 ((< delta 0)
2546 (setq max-delta (window--max-delta-1 right 0 horizontal nil 'before))
2547 (setq min-delta (window--min-delta-1 left delta horizontal nil 'after))
2548 (when (or (< max-delta (- delta)) (> min-delta delta))
2549 ;; We can't get the whole DELTA - move as far as possible.
2550 (setq delta (max (- max-delta) min-delta)))
2551 (unless (zerop delta)
2552 ;; Start resizing.
2553 (window--resize-reset frame horizontal)
2554 ;; Try to enlarge RIGHT.
2555 (setq this-delta (window--resizable right (- delta) horizontal))
2556 (unless (zerop this-delta)
2557 (window--resize-this-window
2558 right this-delta horizontal nil t 'after
2559 (if horizontal
2560 (window-left-column right)
2561 (window-top-line right))))
2562 ;; Shrink windows on left of RIGHT.
2563 (window--resize-siblings
2564 right (- delta) horizontal nil 'before
2565 (if horizontal
2566 (+ (window-left-column left) (window-total-size left t))
2567 (+ (window-top-line left) (window-total-size left)))))))
2568 (unless (zerop delta)
2569 ;; Don't report an error in the standard case.
2570 (unless (window-resize-apply frame horizontal)
2571 ;; But do report an error if applying the changes fails.
2572 (error "Failed adjusting window %s" window)))))))
2574 (defun enlarge-window (delta &optional horizontal)
2575 "Make the selected window DELTA lines taller.
2576 Interactively, if no argument is given, make the selected window
2577 one line taller. If optional argument HORIZONTAL is non-nil,
2578 make selected window wider by DELTA columns. If DELTA is
2579 negative, shrink selected window by -DELTA lines or columns."
2580 (interactive "p")
2581 (let ((minibuffer-window (minibuffer-window)))
2582 (cond
2583 ((zerop delta))
2584 ((window-size-fixed-p nil horizontal)
2585 (error "Selected window has fixed size"))
2586 ((window-minibuffer-p)
2587 (if horizontal
2588 (error "Cannot resize minibuffer window horizontally")
2589 (window--resize-mini-window (selected-window) delta)))
2590 ((and (not horizontal)
2591 (window-full-height-p)
2592 (eq (window-frame minibuffer-window) (selected-frame))
2593 (not resize-mini-windows))
2594 ;; If the selected window is full height and `resize-mini-windows'
2595 ;; is nil, resize the minibuffer window.
2596 (window--resize-mini-window minibuffer-window (- delta)))
2597 ((window--resizable-p nil delta horizontal)
2598 (window-resize nil delta horizontal))
2600 (window-resize
2601 nil (if (> delta 0)
2602 (window-max-delta nil horizontal)
2603 (- (window-min-delta nil horizontal)))
2604 horizontal)))))
2606 (defun shrink-window (delta &optional horizontal)
2607 "Make the selected window DELTA lines smaller.
2608 Interactively, if no argument is given, make the selected window
2609 one line smaller. If optional argument HORIZONTAL is non-nil,
2610 make selected window narrower by DELTA columns. If DELTA is
2611 negative, enlarge selected window by -DELTA lines or columns.
2612 Also see the `window-min-height' variable."
2613 (interactive "p")
2614 (let ((minibuffer-window (minibuffer-window)))
2615 (cond
2616 ((zerop delta))
2617 ((window-size-fixed-p nil horizontal)
2618 (error "Selected window has fixed size"))
2619 ((window-minibuffer-p)
2620 (if horizontal
2621 (error "Cannot resize minibuffer window horizontally")
2622 (window--resize-mini-window (selected-window) (- delta))))
2623 ((and (not horizontal)
2624 (window-full-height-p)
2625 (eq (window-frame minibuffer-window) (selected-frame))
2626 (not resize-mini-windows))
2627 ;; If the selected window is full height and `resize-mini-windows'
2628 ;; is nil, resize the minibuffer window.
2629 (window--resize-mini-window minibuffer-window delta))
2630 ((window--resizable-p nil (- delta) horizontal)
2631 (window-resize nil (- delta) horizontal))
2633 (window-resize
2634 nil (if (> delta 0)
2635 (- (window-min-delta nil horizontal))
2636 (window-max-delta nil horizontal))
2637 horizontal)))))
2639 (defun maximize-window (&optional window)
2640 "Maximize WINDOW.
2641 Make WINDOW as large as possible without deleting any windows.
2642 WINDOW must be a valid window and defaults to the selected one."
2643 (interactive)
2644 (setq window (window-normalize-window window))
2645 (window-resize window (window-max-delta window))
2646 (window-resize window (window-max-delta window t) t))
2648 (defun minimize-window (&optional window)
2649 "Minimize WINDOW.
2650 Make WINDOW as small as possible without deleting any windows.
2651 WINDOW must be a valid window and defaults to the selected one."
2652 (interactive)
2653 (setq window (window-normalize-window window))
2654 (window-resize window (- (window-min-delta window)))
2655 (window-resize window (- (window-min-delta window t)) t))
2657 (defun frame-root-window-p (window)
2658 "Return non-nil if WINDOW is the root window of its frame."
2659 (eq window (frame-root-window window)))
2661 (defun window--subtree (window &optional next)
2662 "Return window subtree rooted at WINDOW.
2663 Optional argument NEXT non-nil means include WINDOW's right
2664 siblings in the return value.
2666 See the documentation of `window-tree' for a description of the
2667 return value."
2668 (let (list)
2669 (while window
2670 (setq list
2671 (cons
2672 (cond
2673 ((window-top-child window)
2674 (cons t (cons (window-edges window)
2675 (window--subtree (window-top-child window) t))))
2676 ((window-left-child window)
2677 (cons nil (cons (window-edges window)
2678 (window--subtree (window-left-child window) t))))
2679 (t window))
2680 list))
2681 (setq window (when next (window-next-sibling window))))
2682 (nreverse list)))
2684 (defun window-tree (&optional frame)
2685 "Return the window tree of frame FRAME.
2686 FRAME must be a live frame and defaults to the selected frame.
2687 The return value is a list of the form (ROOT MINI), where ROOT
2688 represents the window tree of the frame's root window, and MINI
2689 is the frame's minibuffer window.
2691 If the root window is not split, ROOT is the root window itself.
2692 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
2693 for a horizontal split, and t for a vertical split. EDGES gives
2694 the combined size and position of the child windows in the split,
2695 and the rest of the elements are the child windows in the split.
2696 Each of the child windows may again be a window or a list
2697 representing a window split, and so on. EDGES is a list (LEFT
2698 TOP RIGHT BOTTOM) as returned by `window-edges'."
2699 (setq frame (window-normalize-frame frame))
2700 (window--subtree (frame-root-window frame) t))
2702 (defun other-window (count &optional all-frames)
2703 "Select another window in cyclic ordering of windows.
2704 COUNT specifies the number of windows to skip, starting with the
2705 selected window, before making the selection. If COUNT is
2706 positive, skip COUNT windows forwards. If COUNT is negative,
2707 skip -COUNT windows backwards. COUNT zero means do not skip any
2708 window, so select the selected window. In an interactive call,
2709 COUNT is the numeric prefix argument. Return nil.
2711 If the `other-window' parameter of the selected window is a
2712 function and `ignore-window-parameters' is nil, call that
2713 function with the arguments COUNT and ALL-FRAMES.
2715 This function does not select a window whose `no-other-window'
2716 window parameter is non-nil.
2718 This function uses `next-window' for finding the window to
2719 select. The argument ALL-FRAMES has the same meaning as in
2720 `next-window', but the MINIBUF argument of `next-window' is
2721 always effectively nil."
2722 (interactive "p")
2723 (let* ((window (selected-window))
2724 (function (and (not ignore-window-parameters)
2725 (window-parameter window 'other-window)))
2726 old-window old-count)
2727 (if (functionp function)
2728 (funcall function count all-frames)
2729 ;; `next-window' and `previous-window' may return a window we are
2730 ;; not allowed to select. Hence we need an exit strategy in case
2731 ;; all windows are non-selectable.
2732 (catch 'exit
2733 (while (> count 0)
2734 (setq window (next-window window nil all-frames))
2735 (cond
2736 ((eq window old-window)
2737 (when (= count old-count)
2738 ;; Keep out of infinite loops. When COUNT has not changed
2739 ;; since we last looked at `window' we're probably in one.
2740 (throw 'exit nil)))
2741 ((window-parameter window 'no-other-window)
2742 (unless old-window
2743 ;; The first non-selectable window `next-window' got us:
2744 ;; Remember it and the current value of COUNT.
2745 (setq old-window window)
2746 (setq old-count count)))
2748 (setq count (1- count)))))
2749 (while (< count 0)
2750 (setq window (previous-window window nil all-frames))
2751 (cond
2752 ((eq window old-window)
2753 (when (= count old-count)
2754 ;; Keep out of infinite loops. When COUNT has not changed
2755 ;; since we last looked at `window' we're probably in one.
2756 (throw 'exit nil)))
2757 ((window-parameter window 'no-other-window)
2758 (unless old-window
2759 ;; The first non-selectable window `previous-window' got
2760 ;; us: Remember it and the current value of COUNT.
2761 (setq old-window window)
2762 (setq old-count count)))
2764 (setq count (1+ count)))))
2766 (select-window window)
2767 ;; Always return nil.
2768 nil))))
2770 ;; This should probably return non-nil when the selected window is part
2771 ;; of an atomic window whose root is the frame's root window.
2772 (defun one-window-p (&optional nomini all-frames)
2773 "Return non-nil if the selected window is the only window.
2774 Optional arg NOMINI non-nil means don't count the minibuffer
2775 even if it is active. Otherwise, the minibuffer is counted
2776 when it is active.
2778 Optional argument ALL-FRAMES specifies the set of frames to
2779 consider, see also `next-window'. ALL-FRAMES nil or omitted
2780 means consider windows on the selected frame only, plus the
2781 minibuffer window if specified by the NOMINI argument. If the
2782 minibuffer counts, consider all windows on all frames that share
2783 that minibuffer too. The remaining non-nil values of ALL-FRAMES
2784 with a special meaning are:
2786 - t means consider all windows on all existing frames.
2788 - `visible' means consider all windows on all visible frames on
2789 the current terminal.
2791 - 0 (the number zero) means consider all windows on all visible
2792 and iconified frames on the current terminal.
2794 - A frame means consider all windows on that frame only.
2796 Anything else means consider all windows on the selected frame
2797 and no others."
2798 (let ((base-window (selected-window)))
2799 (if (and nomini (eq base-window (minibuffer-window)))
2800 (setq base-window (next-window base-window)))
2801 (eq base-window
2802 (next-window base-window (if nomini 'arg) all-frames))))
2804 ;;; Deleting windows.
2805 (defun window-deletable-p (&optional window)
2806 "Return t if WINDOW can be safely deleted from its frame.
2807 WINDOW must be a valid window and defaults to the selected one.
2808 Return `frame' if deleting WINDOW should also delete its frame."
2809 (setq window (window-normalize-window window))
2811 (unless ignore-window-parameters
2812 ;; Handle atomicity.
2813 (when (window-parameter window 'window-atom)
2814 (setq window (window-atom-root window))))
2816 (let ((frame (window-frame window)))
2817 (cond
2818 ((frame-root-window-p window)
2819 ;; WINDOW's frame can be deleted only if there are other frames
2820 ;; on the same terminal, and it does not contain the active
2821 ;; minibuffer.
2822 (unless (or (eq frame (next-frame frame 0))
2823 (let ((minibuf (active-minibuffer-window)))
2824 (and minibuf (eq frame (window-frame minibuf)))))
2825 'frame))
2826 ((or ignore-window-parameters
2827 (not (eq window (window--major-non-side-window frame))))
2828 ;; WINDOW can be deleted unless it is the major non-side window of
2829 ;; its frame.
2830 t))))
2832 (defun window--in-subtree-p (window root)
2833 "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
2834 (or (eq window root)
2835 (let ((parent (window-parent window)))
2836 (catch 'done
2837 (while parent
2838 (if (eq parent root)
2839 (throw 'done t)
2840 (setq parent (window-parent parent))))))))
2842 (defun delete-window (&optional window)
2843 "Delete WINDOW.
2844 WINDOW must be a valid window and defaults to the selected one.
2845 Return nil.
2847 If the variable `ignore-window-parameters' is non-nil or the
2848 `delete-window' parameter of WINDOW equals t, do not process any
2849 parameters of WINDOW. Otherwise, if the `delete-window'
2850 parameter of WINDOW specifies a function, call that function with
2851 WINDOW as its sole argument and return the value returned by that
2852 function.
2854 Otherwise, if WINDOW is part of an atomic window, call
2855 `delete-window' with the root of the atomic window as its
2856 argument. Signal an error if WINDOW is either the only window on
2857 its frame, the last non-side window, or part of an atomic window
2858 that is its frame's root window."
2859 (interactive)
2860 (setq window (window-normalize-window window))
2861 (let* ((frame (window-frame window))
2862 (function (window-parameter window 'delete-window))
2863 (parent (window-parent window))
2864 atom-root)
2865 (window--check frame)
2866 (catch 'done
2867 ;; Handle window parameters.
2868 (cond
2869 ;; Ignore window parameters if `ignore-window-parameters' tells
2870 ;; us so or `delete-window' equals t.
2871 ((or ignore-window-parameters (eq function t)))
2872 ((functionp function)
2873 ;; The `delete-window' parameter specifies the function to call.
2874 ;; If that function is `ignore' nothing is done. It's up to the
2875 ;; function called here to avoid infinite recursion.
2876 (throw 'done (funcall function window)))
2877 ((and (window-parameter window 'window-atom)
2878 (setq atom-root (window-atom-root window))
2879 (not (eq atom-root window)))
2880 (if (eq atom-root (frame-root-window frame))
2881 (error "Root of atomic window is root window of its frame")
2882 (throw 'done (delete-window atom-root))))
2883 ((not parent)
2884 (error "Attempt to delete minibuffer or sole ordinary window"))
2885 ((eq window (window--major-non-side-window frame))
2886 (error "Attempt to delete last non-side window")))
2888 (let* ((horizontal (window-left-child parent))
2889 (size (window-total-size window horizontal))
2890 (frame-selected
2891 (window--in-subtree-p (frame-selected-window frame) window))
2892 ;; Emacs 23 preferably gives WINDOW's space to its left
2893 ;; sibling.
2894 (sibling (or (window-left window) (window-right window))))
2895 (window--resize-reset frame horizontal)
2896 (cond
2897 ((and (not window-combination-resize)
2898 sibling (window-sizable-p sibling size))
2899 ;; Resize WINDOW's sibling.
2900 (window--resize-this-window sibling size horizontal nil t)
2901 (set-window-new-normal
2902 sibling (+ (window-normal-size sibling horizontal)
2903 (window-normal-size window horizontal))))
2904 ((window--resizable-p window (- size) horizontal nil nil nil t)
2905 ;; Can do without resizing fixed-size windows.
2906 (window--resize-siblings window (- size) horizontal))
2908 ;; Can't do without resizing fixed-size windows.
2909 (window--resize-siblings window (- size) horizontal t)))
2910 ;; Actually delete WINDOW.
2911 (delete-window-internal window)
2912 (when (and frame-selected
2913 (window-parameter
2914 (frame-selected-window frame) 'no-other-window))
2915 ;; `delete-window-internal' has selected a window that should
2916 ;; not be selected, fix this here.
2917 (other-window -1 frame))
2918 (run-window-configuration-change-hook frame)
2919 (window--check frame)
2920 ;; Always return nil.
2921 nil))))
2923 (defun delete-other-windows (&optional window)
2924 "Make WINDOW fill its frame.
2925 WINDOW must be a valid window and defaults to the selected one.
2926 Return nil.
2928 If the variable `ignore-window-parameters' is non-nil or the
2929 `delete-other-windows' parameter of WINDOW equals t, do not
2930 process any parameters of WINDOW. Otherwise, if the
2931 `delete-other-windows' parameter of WINDOW specifies a function,
2932 call that function with WINDOW as its sole argument and return
2933 the value returned by that function.
2935 Otherwise, if WINDOW is part of an atomic window, call this
2936 function with the root of the atomic window as its argument. If
2937 WINDOW is a non-side window, make WINDOW the only non-side window
2938 on the frame. Side windows are not deleted. If WINDOW is a side
2939 window signal an error."
2940 (interactive)
2941 (setq window (window-normalize-window window))
2942 (let* ((frame (window-frame window))
2943 (function (window-parameter window 'delete-other-windows))
2944 (window-side (window-parameter window 'window-side))
2945 atom-root side-main)
2946 (window--check frame)
2947 (catch 'done
2948 (cond
2949 ;; Ignore window parameters if `ignore-window-parameters' is t or
2950 ;; `delete-other-windows' is t.
2951 ((or ignore-window-parameters (eq function t)))
2952 ((functionp function)
2953 ;; The `delete-other-windows' parameter specifies the function
2954 ;; to call. If the function is `ignore' no windows are deleted.
2955 ;; It's up to the function called to avoid infinite recursion.
2956 (throw 'done (funcall function window)))
2957 ((and (window-parameter window 'window-atom)
2958 (setq atom-root (window-atom-root window))
2959 (not (eq atom-root window)))
2960 (if (eq atom-root (frame-root-window frame))
2961 (error "Root of atomic window is root window of its frame")
2962 (throw 'done (delete-other-windows atom-root))))
2963 ((memq window-side window-sides)
2964 (error "Cannot make side window the only window"))
2965 ((and (window-minibuffer-p window)
2966 (not (eq window (frame-root-window window))))
2967 (error "Can't expand minibuffer to full frame")))
2969 ;; If WINDOW is the major non-side window, do nothing.
2970 (if (window-with-parameter 'window-side)
2971 (setq side-main (window--major-non-side-window frame))
2972 (setq side-main (frame-root-window frame)))
2973 (unless (eq window side-main)
2974 (delete-other-windows-internal window side-main)
2975 (run-window-configuration-change-hook frame)
2976 (window--check frame))
2977 ;; Always return nil.
2978 nil)))
2980 (defun delete-other-windows-vertically (&optional window)
2981 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
2982 This may be a useful alternative binding for \\[delete-other-windows]
2983 if you often split windows horizontally."
2984 (interactive)
2985 (let* ((window (or window (selected-window)))
2986 (edges (window-edges window))
2987 (w window) delenda)
2988 (while (not (eq (setq w (next-window w 1)) window))
2989 (let ((e (window-edges w)))
2990 (when (and (= (car e) (car edges))
2991 (= (nth 2 e) (nth 2 edges)))
2992 (push w delenda))))
2993 (mapc 'delete-window delenda)))
2995 ;;; Windows and buffers.
2997 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
2998 ;; for (1) determining which buffer to show in the window when its
2999 ;; buffer shall be buried or killed and (2) which buffer to show for
3000 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3002 ;; `prev-buffers' consists of <buffer, window-start, window-point>
3003 ;; triples. The entries on this list are ordered by the time their
3004 ;; buffer has been removed from the window, the most recently removed
3005 ;; buffer's entry being first. The window-start and window-point
3006 ;; components are `window-start' and `window-point' at the time the
3007 ;; buffer was removed from the window which implies that the entry must
3008 ;; be added when `set-window-buffer' removes the buffer from the window.
3010 ;; `next-buffers' is the list of buffers that have been replaced
3011 ;; recently by `switch-to-prev-buffer'. These buffers are the least
3012 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
3013 ;; candidates of `switch-to-next-buffer' to switch to. This list is
3014 ;; reset to nil by any action changing the window's buffer with the
3015 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
3016 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
3017 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
3019 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
3020 ;; if such a buffer was killed while the window was hidden within a
3021 ;; window configuration. Such killed buffers get removed whenever
3022 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
3024 ;; The following function is called by `set-window-buffer' _before_ it
3025 ;; replaces the buffer of the argument window with the new buffer.
3026 (defun record-window-buffer (&optional window)
3027 "Record WINDOW's buffer.
3028 WINDOW must be a live window and defaults to the selected one."
3029 (let* ((window (window-normalize-window window t))
3030 (buffer (window-buffer window))
3031 (entry (assq buffer (window-prev-buffers window))))
3032 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
3033 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
3034 (set-window-next-buffers window nil)
3036 (when entry
3037 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
3038 (set-window-prev-buffers
3039 window (assq-delete-all buffer (window-prev-buffers window))))
3041 ;; Don't record insignificant buffers.
3042 (unless (eq (aref (buffer-name buffer) 0) ?\s)
3043 ;; Add an entry for buffer to WINDOW's previous buffers.
3044 (with-current-buffer buffer
3045 (let ((start (window-start window))
3046 (point (window-point window)))
3047 (setq entry
3048 (cons buffer
3049 (if entry
3050 ;; We have an entry, update marker positions.
3051 (list (set-marker (nth 1 entry) start)
3052 (set-marker (nth 2 entry) point))
3053 ;; Make new markers.
3054 (list (copy-marker start)
3055 (copy-marker
3056 ;; Preserve window-point-insertion-type
3057 ;; (Bug#12588).
3058 point window-point-insertion-type)))))
3059 (set-window-prev-buffers
3060 window (cons entry (window-prev-buffers window))))))))
3062 (defun unrecord-window-buffer (&optional window buffer)
3063 "Unrecord BUFFER in WINDOW.
3064 WINDOW must be a live window and defaults to the selected one.
3065 BUFFER must be a live buffer and defaults to the buffer of
3066 WINDOW."
3067 (let* ((window (window-normalize-window window t))
3068 (buffer (or buffer (window-buffer window))))
3069 (set-window-prev-buffers
3070 window (assq-delete-all buffer (window-prev-buffers window)))
3071 (set-window-next-buffers
3072 window (delq buffer (window-next-buffers window)))))
3074 (defun set-window-buffer-start-and-point (window buffer &optional start point)
3075 "Set WINDOW's buffer to BUFFER.
3076 WINDOW must be a live window and defaults to the selected one.
3077 Optional argument START non-nil means set WINDOW's start position
3078 to START. Optional argument POINT non-nil means set WINDOW's
3079 point to POINT. If WINDOW is selected this also sets BUFFER's
3080 `point' to POINT. If WINDOW is selected and the buffer it showed
3081 before was current this also makes BUFFER the current buffer."
3082 (setq window (window-normalize-window window t))
3083 (let ((selected (eq window (selected-window)))
3084 (current (eq (window-buffer window) (current-buffer))))
3085 (set-window-buffer window buffer)
3086 (when (and selected current)
3087 (set-buffer buffer))
3088 (when start
3089 ;; Don't force window-start here (even if POINT is nil).
3090 (set-window-start window start t))
3091 (when point
3092 (set-window-point window point))))
3094 (defcustom switch-to-visible-buffer t
3095 "If non-nil, allow switching to an already visible buffer.
3096 If this variable is non-nil, `switch-to-prev-buffer' and
3097 `switch-to-next-buffer' may switch to an already visible buffer
3098 provided the buffer was shown before in the window specified as
3099 argument to those functions. If this variable is nil,
3100 `switch-to-prev-buffer' and `switch-to-next-buffer' always try to
3101 avoid switching to a buffer that is already visible in another
3102 window on the same frame."
3103 :type 'boolean
3104 :version "24.1"
3105 :group 'windows)
3107 (defun switch-to-prev-buffer (&optional window bury-or-kill)
3108 "In WINDOW switch to previous buffer.
3109 WINDOW must be a live window and defaults to the selected one.
3110 Return the buffer switched to, nil if no suitable buffer could be
3111 found.
3113 Optional argument BURY-OR-KILL non-nil means the buffer currently
3114 shown in WINDOW is about to be buried or killed and consequently
3115 shall not be switched to in future invocations of this command.
3117 As a special case, if BURY-OR-KILL equals `append', this means to
3118 move the buffer to the end of WINDOW's previous buffers list so a
3119 future invocation of `switch-to-prev-buffer' less likely switches
3120 to it."
3121 (interactive)
3122 (let* ((window (window-normalize-window window t))
3123 (frame (window-frame window))
3124 (old-buffer (window-buffer window))
3125 ;; Save this since it's destroyed by `set-window-buffer'.
3126 (next-buffers (window-next-buffers window))
3127 (pred (frame-parameter frame 'buffer-predicate))
3128 entry new-buffer killed-buffers visible)
3129 (when (window-minibuffer-p window)
3130 ;; Don't switch in minibuffer window.
3131 (unless (setq window (minibuffer-selected-window))
3132 (error "Window %s is a minibuffer window" window)))
3134 (when (window-dedicated-p window)
3135 ;; Don't switch in dedicated window.
3136 (error "Window %s is dedicated to buffer %s" window old-buffer))
3138 (catch 'found
3139 ;; Scan WINDOW's previous buffers first, skipping entries of next
3140 ;; buffers.
3141 (dolist (entry (window-prev-buffers window))
3142 (when (and (setq new-buffer (car entry))
3143 (or (buffer-live-p new-buffer)
3144 (not (setq killed-buffers
3145 (cons new-buffer killed-buffers))))
3146 (not (eq new-buffer old-buffer))
3147 (or (null pred) (funcall pred new-buffer))
3148 ;; When BURY-OR-KILL is nil, avoid switching to a
3149 ;; buffer in WINDOW's next buffers list.
3150 (or bury-or-kill (not (memq new-buffer next-buffers))))
3151 (if (and (not switch-to-visible-buffer)
3152 (get-buffer-window new-buffer frame))
3153 ;; Try to avoid showing a buffer visible in some other
3154 ;; window.
3155 (setq visible new-buffer)
3156 (set-window-buffer-start-and-point
3157 window new-buffer (nth 1 entry) (nth 2 entry))
3158 (throw 'found t))))
3159 ;; Scan reverted buffer list of WINDOW's frame next, skipping
3160 ;; entries of next buffers. Note that when we bury or kill a
3161 ;; buffer we don't reverse the global buffer list to avoid showing
3162 ;; a buried buffer instead. Otherwise, we must reverse the global
3163 ;; buffer list in order to make sure that switching to the
3164 ;; previous/next buffer traverse it in opposite directions.
3165 (dolist (buffer (if bury-or-kill
3166 (buffer-list frame)
3167 (nreverse (buffer-list frame))))
3168 (when (and (buffer-live-p buffer)
3169 (not (eq buffer old-buffer))
3170 (or (null pred) (funcall pred buffer))
3171 (not (eq (aref (buffer-name buffer) 0) ?\s))
3172 (or bury-or-kill (not (memq buffer next-buffers))))
3173 (if (get-buffer-window buffer frame)
3174 ;; Try to avoid showing a buffer visible in some other window.
3175 (unless visible
3176 (setq visible buffer))
3177 (setq new-buffer buffer)
3178 (set-window-buffer-start-and-point window new-buffer)
3179 (throw 'found t))))
3180 (unless bury-or-kill
3181 ;; Scan reverted next buffers last (must not use nreverse
3182 ;; here!).
3183 (dolist (buffer (reverse next-buffers))
3184 ;; Actually, buffer _must_ be live here since otherwise it
3185 ;; would have been caught in the scan of previous buffers.
3186 (when (and (or (buffer-live-p buffer)
3187 (not (setq killed-buffers
3188 (cons buffer killed-buffers))))
3189 (not (eq buffer old-buffer))
3190 (or (null pred) (funcall pred buffer))
3191 (setq entry (assq buffer (window-prev-buffers window))))
3192 (setq new-buffer buffer)
3193 (set-window-buffer-start-and-point
3194 window new-buffer (nth 1 entry) (nth 2 entry))
3195 (throw 'found t))))
3197 ;; Show a buffer visible in another window.
3198 (when visible
3199 (setq new-buffer visible)
3200 (set-window-buffer-start-and-point window new-buffer)))
3202 (if bury-or-kill
3203 (let ((entry (and (eq bury-or-kill 'append)
3204 (assq old-buffer (window-prev-buffers window)))))
3205 ;; Remove `old-buffer' from WINDOW's previous and (restored list
3206 ;; of) next buffers.
3207 (set-window-prev-buffers
3208 window (assq-delete-all old-buffer (window-prev-buffers window)))
3209 (set-window-next-buffers window (delq old-buffer next-buffers))
3210 (when entry
3211 ;; Append old-buffer's entry to list of WINDOW's previous
3212 ;; buffers so it's less likely to get switched to soon but
3213 ;; `display-buffer-in-previous-window' can nevertheless find
3214 ;; it.
3215 (set-window-prev-buffers
3216 window (append (window-prev-buffers window) (list entry)))))
3217 ;; Move `old-buffer' to head of WINDOW's restored list of next
3218 ;; buffers.
3219 (set-window-next-buffers
3220 window (cons old-buffer (delq old-buffer next-buffers))))
3222 ;; Remove killed buffers from WINDOW's previous and next buffers.
3223 (when killed-buffers
3224 (dolist (buffer killed-buffers)
3225 (set-window-prev-buffers
3226 window (assq-delete-all buffer (window-prev-buffers window)))
3227 (set-window-next-buffers
3228 window (delq buffer (window-next-buffers window)))))
3230 ;; Return new-buffer.
3231 new-buffer))
3233 (defun switch-to-next-buffer (&optional window)
3234 "In WINDOW switch to next buffer.
3235 WINDOW must be a live window and defaults to the selected one.
3236 Return the buffer switched to, nil if no suitable buffer could be
3237 found."
3238 (interactive)
3239 (let* ((window (window-normalize-window window t))
3240 (frame (window-frame window))
3241 (old-buffer (window-buffer window))
3242 (next-buffers (window-next-buffers window))
3243 (pred (frame-parameter frame 'buffer-predicate))
3244 new-buffer entry killed-buffers visible)
3245 (when (window-minibuffer-p window)
3246 ;; Don't switch in minibuffer window.
3247 (unless (setq window (minibuffer-selected-window))
3248 (error "Window %s is a minibuffer window" window)))
3250 (when (window-dedicated-p window)
3251 ;; Don't switch in dedicated window.
3252 (error "Window %s is dedicated to buffer %s" window old-buffer))
3254 (catch 'found
3255 ;; Scan WINDOW's next buffers first.
3256 (dolist (buffer next-buffers)
3257 (when (and (or (buffer-live-p buffer)
3258 (not (setq killed-buffers
3259 (cons buffer killed-buffers))))
3260 (not (eq buffer old-buffer))
3261 (or (null pred) (funcall pred buffer))
3262 (setq entry (assq buffer (window-prev-buffers window))))
3263 (setq new-buffer buffer)
3264 (set-window-buffer-start-and-point
3265 window new-buffer (nth 1 entry) (nth 2 entry))
3266 (throw 'found t)))
3267 ;; Scan the buffer list of WINDOW's frame next, skipping previous
3268 ;; buffers entries.
3269 (dolist (buffer (buffer-list frame))
3270 (when (and (buffer-live-p buffer)
3271 (not (eq buffer old-buffer))
3272 (or (null pred) (funcall pred buffer))
3273 (not (eq (aref (buffer-name buffer) 0) ?\s))
3274 (not (assq buffer (window-prev-buffers window))))
3275 (if (get-buffer-window buffer frame)
3276 ;; Try to avoid showing a buffer visible in some other window.
3277 (setq visible buffer)
3278 (setq new-buffer buffer)
3279 (set-window-buffer-start-and-point window new-buffer)
3280 (throw 'found t))))
3281 ;; Scan WINDOW's reverted previous buffers last (must not use
3282 ;; nreverse here!)
3283 (dolist (entry (reverse (window-prev-buffers window)))
3284 (when (and (setq new-buffer (car entry))
3285 (or (buffer-live-p new-buffer)
3286 (not (setq killed-buffers
3287 (cons new-buffer killed-buffers))))
3288 (not (eq new-buffer old-buffer))
3289 (or (null pred) (funcall pred new-buffer)))
3290 (if (and (not switch-to-visible-buffer)
3291 (get-buffer-window new-buffer frame))
3292 ;; Try to avoid showing a buffer visible in some other window.
3293 (unless visible
3294 (setq visible new-buffer))
3295 (set-window-buffer-start-and-point
3296 window new-buffer (nth 1 entry) (nth 2 entry))
3297 (throw 'found t))))
3299 ;; Show a buffer visible in another window.
3300 (when visible
3301 (setq new-buffer visible)
3302 (set-window-buffer-start-and-point window new-buffer)))
3304 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
3305 (set-window-next-buffers window (delq new-buffer next-buffers))
3307 ;; Remove killed buffers from WINDOW's previous and next buffers.
3308 (when killed-buffers
3309 (dolist (buffer killed-buffers)
3310 (set-window-prev-buffers
3311 window (assq-delete-all buffer (window-prev-buffers window)))
3312 (set-window-next-buffers
3313 window (delq buffer (window-next-buffers window)))))
3315 ;; Return new-buffer.
3316 new-buffer))
3318 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
3319 "Search LIST for a valid buffer to display in FRAME.
3320 Return nil when all buffers in LIST are undesirable for display,
3321 otherwise return the first suitable buffer in LIST.
3323 Buffers not visible in windows are preferred to visible buffers,
3324 unless VISIBLE-OK is non-nil.
3325 If the optional argument FRAME is nil, it defaults to the selected frame.
3326 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
3327 ;; This logic is more or less copied from other-buffer.
3328 (setq frame (or frame (selected-frame)))
3329 (let ((pred (frame-parameter frame 'buffer-predicate))
3330 found buf)
3331 (while (and (not found) list)
3332 (setq buf (car list))
3333 (if (and (not (eq buffer buf))
3334 (buffer-live-p buf)
3335 (or (null pred) (funcall pred buf))
3336 (not (eq (aref (buffer-name buf) 0) ?\s))
3337 (or visible-ok (null (get-buffer-window buf 'visible))))
3338 (setq found buf)
3339 (setq list (cdr list))))
3340 (car list)))
3342 (defun last-buffer (&optional buffer visible-ok frame)
3343 "Return the last buffer in FRAME's buffer list.
3344 If BUFFER is the last buffer, return the preceding buffer
3345 instead. Buffers not visible in windows are preferred to visible
3346 buffers, unless optional argument VISIBLE-OK is non-nil.
3347 Optional third argument FRAME nil or omitted means use the
3348 selected frame's buffer list. If no such buffer exists, return
3349 the buffer `*scratch*', creating it if necessary."
3350 (setq frame (or frame (selected-frame)))
3351 (or (get-next-valid-buffer (nreverse (buffer-list frame))
3352 buffer visible-ok frame)
3353 (get-buffer "*scratch*")
3354 (let ((scratch (get-buffer-create "*scratch*")))
3355 (set-buffer-major-mode scratch)
3356 scratch)))
3358 (defcustom frame-auto-hide-function #'iconify-frame
3359 "Function called to automatically hide frames.
3360 The function is called with one argument - a frame.
3362 Functions affected by this option are those that bury a buffer
3363 shown in a separate frame like `quit-window' and `bury-buffer'."
3364 :type '(choice (const :tag "Iconify" iconify-frame)
3365 (const :tag "Delete" delete-frame)
3366 (const :tag "Do nothing" ignore)
3367 function)
3368 :group 'windows
3369 :group 'frames
3370 :version "24.1")
3372 (defun window--delete (&optional window dedicated-only kill)
3373 "Delete WINDOW if possible.
3374 WINDOW must be a live window and defaults to the selected one.
3375 Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
3376 only if it's dedicated to its buffer. Optional argument KILL
3377 means the buffer shown in window will be killed. Return non-nil
3378 if WINDOW gets deleted or its frame is auto-hidden."
3379 (setq window (window-normalize-window window t))
3380 (unless (and dedicated-only (not (window-dedicated-p window)))
3381 (let ((deletable (window-deletable-p window)))
3382 (cond
3383 ((eq deletable 'frame)
3384 (let ((frame (window-frame window)))
3385 (cond
3386 (kill
3387 (delete-frame frame))
3388 ((functionp frame-auto-hide-function)
3389 (funcall frame-auto-hide-function frame))))
3390 'frame)
3391 (deletable
3392 (delete-window window)
3393 t)))))
3395 (defun bury-buffer (&optional buffer-or-name)
3396 "Put BUFFER-OR-NAME at the end of the list of all buffers.
3397 There it is the least likely candidate for `other-buffer' to
3398 return; thus, the least likely buffer for \\[switch-to-buffer] to
3399 select by default.
3401 You can specify a buffer name as BUFFER-OR-NAME, or an actual
3402 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
3403 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
3404 remove the current buffer from the selected window if it is
3405 displayed there."
3406 (interactive)
3407 (let* ((buffer (window-normalize-buffer buffer-or-name)))
3408 ;; If `buffer-or-name' is not on the selected frame we unrecord it
3409 ;; although it's not "here" (call it a feature).
3410 (bury-buffer-internal buffer)
3411 ;; Handle case where `buffer-or-name' is nil and the current buffer
3412 ;; is shown in the selected window.
3413 (cond
3414 ((or buffer-or-name (not (eq buffer (window-buffer)))))
3415 ((window--delete nil t))
3417 ;; Switch to another buffer in window.
3418 (set-window-dedicated-p nil nil)
3419 (switch-to-prev-buffer nil 'bury)))
3421 ;; Always return nil.
3422 nil))
3424 (defun unbury-buffer ()
3425 "Switch to the last buffer in the buffer list."
3426 (interactive)
3427 (switch-to-buffer (last-buffer)))
3429 (defun next-buffer ()
3430 "In selected window switch to next buffer."
3431 (interactive)
3432 (cond
3433 ((window-minibuffer-p)
3434 (error "Cannot switch buffers in minibuffer window"))
3435 ((eq (window-dedicated-p) t)
3436 (error "Window is strongly dedicated to its buffer"))
3438 (switch-to-next-buffer))))
3440 (defun previous-buffer ()
3441 "In selected window switch to previous buffer."
3442 (interactive)
3443 (cond
3444 ((window-minibuffer-p)
3445 (error "Cannot switch buffers in minibuffer window"))
3446 ((eq (window-dedicated-p) t)
3447 (error "Window is strongly dedicated to its buffer"))
3449 (switch-to-prev-buffer))))
3451 (defun delete-windows-on (&optional buffer-or-name frame)
3452 "Delete all windows showing BUFFER-OR-NAME.
3453 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3454 and defaults to the current buffer.
3456 The following non-nil values of the optional argument FRAME
3457 have special meanings:
3459 - t means consider all windows on the selected frame only.
3461 - `visible' means consider all windows on all visible frames on
3462 the current terminal.
3464 - 0 (the number zero) means consider all windows on all visible
3465 and iconified frames on the current terminal.
3467 - A frame means consider all windows on that frame only.
3469 Any other value of FRAME means consider all windows on all
3470 frames.
3472 When a window showing BUFFER-OR-NAME is dedicated and the only
3473 window of its frame, that frame is deleted when there are other
3474 frames left."
3475 (interactive "BDelete windows on (buffer):\nP")
3476 (let ((buffer (window-normalize-buffer buffer-or-name))
3477 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3478 ;; `window-list-1' based function.
3479 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
3480 (dolist (window (window-list-1 nil nil all-frames))
3481 (if (eq (window-buffer window) buffer)
3482 (let ((deletable (window-deletable-p window)))
3483 (cond
3484 ((and (eq deletable 'frame) (window-dedicated-p window))
3485 ;; Delete frame if and only if window is dedicated.
3486 (delete-frame (window-frame window)))
3487 ((eq deletable t)
3488 ;; Delete window.
3489 (delete-window window))
3491 ;; In window switch to previous buffer.
3492 (set-window-dedicated-p window nil)
3493 (switch-to-prev-buffer window 'bury))))
3494 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3495 (unrecord-window-buffer window buffer)))))
3497 (defun replace-buffer-in-windows (&optional buffer-or-name)
3498 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
3499 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3500 and defaults to the current buffer.
3502 When a window showing BUFFER-OR-NAME is dedicated, that window is
3503 deleted. If that window is the only window on its frame, the
3504 frame is deleted too when there are other frames left. If there
3505 are no other frames left, some other buffer is displayed in that
3506 window.
3508 This function removes the buffer denoted by BUFFER-OR-NAME from
3509 all window-local buffer lists."
3510 (interactive "bBuffer to replace: ")
3511 (let ((buffer (window-normalize-buffer buffer-or-name)))
3512 (dolist (window (window-list-1 nil nil t))
3513 (if (eq (window-buffer window) buffer)
3514 (unless (window--delete window t t)
3515 ;; Switch to another buffer in window.
3516 (set-window-dedicated-p window nil)
3517 (switch-to-prev-buffer window 'kill))
3518 ;; Unrecord BUFFER in WINDOW.
3519 (unrecord-window-buffer window buffer)))))
3521 (defun quit-restore-window (&optional window bury-or-kill)
3522 "Quit WINDOW and deal with its buffer.
3523 WINDOW must be a live window and defaults to the selected one.
3525 According to information stored in WINDOW's `quit-restore' window
3526 parameter either (1) delete WINDOW and its frame, (2) delete
3527 WINDOW, (3) restore the buffer previously displayed in WINDOW,
3528 or (4) make WINDOW display some other buffer than the present
3529 one. If non-nil, reset `quit-restore' parameter to nil.
3531 Optional second argument BURY-OR-KILL tells how to proceed with
3532 the buffer of WINDOW. The following values are handled:
3534 `nil' means to not handle the buffer in a particular way. This
3535 means that if WINDOW is not deleted by this function, invoking
3536 `switch-to-prev-buffer' will usually show the buffer again.
3538 `append' means that if WINDOW is not deleted, move its buffer to
3539 the end of WINDOW's previous buffers so it's less likely that a
3540 future invocation of `switch-to-prev-buffer' will switch to it.
3541 Also, move the buffer to the end of the frame's buffer list.
3543 `bury' means that if WINDOW is not deleted, remove its buffer
3544 from WINDOW'S list of previous buffers. Also, move the buffer
3545 to the end of the frame's buffer list. This value provides the
3546 most reliable remedy to not have `switch-to-prev-buffer' switch
3547 to this buffer again without killing the buffer.
3549 `kill' means to kill WINDOW's buffer."
3550 (setq window (window-normalize-window window t))
3551 (let* ((buffer (window-buffer window))
3552 (quit-restore (window-parameter window 'quit-restore))
3553 (prev-buffer
3554 (let* ((prev-buffers (window-prev-buffers window))
3555 (prev-buffer (caar prev-buffers)))
3556 (and (or (not (eq prev-buffer buffer))
3557 (and (cdr prev-buffers)
3558 (not (eq (setq prev-buffer (cadr prev-buffers))
3559 buffer))))
3560 prev-buffer)))
3561 quad entry)
3562 (cond
3563 ((and (not prev-buffer)
3564 (or (eq (nth 1 quit-restore) 'frame)
3565 (and (eq (nth 1 quit-restore) 'window)
3566 ;; If the window has been created on an existing
3567 ;; frame and ended up as the sole window on that
3568 ;; frame, do not delete it (Bug#12764).
3569 (not (eq window (frame-root-window window)))))
3570 (eq (nth 3 quit-restore) buffer)
3571 ;; Delete WINDOW if possible.
3572 (window--delete window nil (eq bury-or-kill 'kill)))
3573 ;; If the previously selected window is still alive, select it.
3574 (when (window-live-p (nth 2 quit-restore))
3575 (select-window (nth 2 quit-restore))))
3576 ((and (listp (setq quad (nth 1 quit-restore)))
3577 (buffer-live-p (car quad))
3578 (eq (nth 3 quit-restore) buffer))
3579 ;; Show another buffer stored in quit-restore parameter.
3580 (when (and (integerp (nth 3 quad))
3581 (/= (nth 3 quad) (window-total-size window)))
3582 ;; Try to resize WINDOW to its old height but don't signal an
3583 ;; error.
3584 (condition-case nil
3585 (window-resize window (- (nth 3 quad) (window-total-size window)))
3586 (error nil)))
3587 (set-window-dedicated-p window nil)
3588 ;; Restore WINDOW's previous buffer, start and point position.
3589 (set-window-buffer-start-and-point
3590 window (nth 0 quad) (nth 1 quad) (nth 2 quad))
3591 ;; Deal with the buffer we just removed from WINDOW.
3592 (setq entry (and (eq bury-or-kill 'append)
3593 (assq buffer (window-prev-buffers window))))
3594 (when bury-or-kill
3595 ;; Remove buffer from WINDOW's previous and next buffers.
3596 (set-window-prev-buffers
3597 window (assq-delete-all buffer (window-prev-buffers window)))
3598 (set-window-next-buffers
3599 window (delq buffer (window-next-buffers window))))
3600 (when entry
3601 ;; Append old buffer's entry to list of WINDOW's previous
3602 ;; buffers so it's less likely to get switched to soon but
3603 ;; `display-buffer-in-previous-window' can nevertheless find it.
3604 (set-window-prev-buffers
3605 window (append (window-prev-buffers window) (list entry))))
3606 ;; Reset the quit-restore parameter.
3607 (set-window-parameter window 'quit-restore nil)
3608 ;; Select old window.
3609 (when (window-live-p (nth 2 quit-restore))
3610 (select-window (nth 2 quit-restore))))
3612 ;; Show some other buffer in WINDOW and reset the quit-restore
3613 ;; parameter.
3614 (set-window-parameter window 'quit-restore nil)
3615 ;; Make sure that WINDOW is no more dedicated.
3616 (set-window-dedicated-p window nil)
3617 (switch-to-prev-buffer window bury-or-kill)))
3619 ;; Deal with the buffer.
3620 (cond
3621 ((not (buffer-live-p buffer)))
3622 ((eq bury-or-kill 'kill)
3623 (kill-buffer buffer))
3624 (bury-or-kill
3625 (bury-buffer-internal buffer)))))
3627 (defun quit-window (&optional kill window)
3628 "Quit WINDOW and bury its buffer.
3629 WINDOW must be a live window and defaults to the selected one.
3630 With prefix argument KILL non-nil, kill the buffer instead of
3631 burying it.
3633 According to information stored in WINDOW's `quit-restore' window
3634 parameter either (1) delete WINDOW and its frame, (2) delete
3635 WINDOW, (3) restore the buffer previously displayed in WINDOW,
3636 or (4) make WINDOW display some other buffer than the present
3637 one. If non-nil, reset `quit-restore' parameter to nil."
3638 (interactive "P")
3639 (quit-restore-window window (if kill 'kill 'bury)))
3641 (defun quit-windows-on (&optional buffer-or-name kill frame)
3642 "Quit all windows showing BUFFER-OR-NAME.
3643 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
3644 and defaults to the current buffer. Optional argument KILL
3645 non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
3646 BUFFER-OR-NAME. Optional argument FRAME is handled as by
3647 `delete-windows-on'.
3649 This function calls `quit-window' on all candidate windows
3650 showing BUFFER-OR-NAME."
3651 (interactive "BQuit windows on (buffer):\nP")
3652 (let ((buffer (window-normalize-buffer buffer-or-name))
3653 ;; Handle the "inverted" meaning of the FRAME argument wrt other
3654 ;; `window-list-1' based function.
3655 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
3656 (dolist (window (window-list-1 nil nil all-frames))
3657 (if (eq (window-buffer window) buffer)
3658 (quit-window kill window)
3659 ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
3660 (unrecord-window-buffer window buffer)))))
3662 ;;; Splitting windows.
3663 (defun window-split-min-size (&optional horizontal)
3664 "Return minimum height of any window when splitting windows.
3665 Optional argument HORIZONTAL non-nil means return minimum width."
3666 (if horizontal
3667 (max window-min-width window-safe-min-width)
3668 (max window-min-height window-safe-min-height)))
3670 (defun split-window (&optional window size side)
3671 "Make a new window adjacent to WINDOW.
3672 WINDOW must be a valid window and defaults to the selected one.
3673 Return the new window which is always a live window.
3675 Optional argument SIZE a positive number means make WINDOW SIZE
3676 lines or columns tall. If SIZE is negative, make the new window
3677 -SIZE lines or columns tall. If and only if SIZE is non-nil, its
3678 absolute value can be less than `window-min-height' or
3679 `window-min-width'; so this command can make a new window as
3680 small as one line or two columns. SIZE defaults to half of
3681 WINDOW's size. Interactively, SIZE is the prefix argument.
3683 Optional third argument SIDE nil (or `below') specifies that the
3684 new window shall be located below WINDOW. SIDE `above' means the
3685 new window shall be located above WINDOW. In both cases SIZE
3686 specifies the new number of lines for WINDOW (or the new window
3687 if SIZE is negative) including space reserved for the mode and/or
3688 header line.
3690 SIDE t (or `right') specifies that the new window shall be
3691 located on the right side of WINDOW. SIDE `left' means the new
3692 window shall be located on the left of WINDOW. In both cases
3693 SIZE specifies the new number of columns for WINDOW (or the new
3694 window provided SIZE is negative) including space reserved for
3695 fringes and the scrollbar or a divider column. Any other non-nil
3696 value for SIDE is currently handled like t (or `right').
3698 If the variable `ignore-window-parameters' is non-nil or the
3699 `split-window' parameter of WINDOW equals t, do not process any
3700 parameters of WINDOW. Otherwise, if the `split-window' parameter
3701 of WINDOW specifies a function, call that function with all three
3702 arguments and return the value returned by that function.
3704 Otherwise, if WINDOW is part of an atomic window, \"split\" the
3705 root of that atomic window. The new window does not become a
3706 member of that atomic window.
3708 If WINDOW is live, properties of the new window like margins and
3709 scrollbars are inherited from WINDOW. If WINDOW is an internal
3710 window, these properties as well as the buffer displayed in the
3711 new window are inherited from the window selected on WINDOW's
3712 frame. The selected window is not changed by this function."
3713 (interactive "i")
3714 (setq window (window-normalize-window window))
3715 (let* ((side (cond
3716 ((not side) 'below)
3717 ((memq side '(below above right left)) side)
3718 (t 'right)))
3719 (horizontal (not (memq side '(below above))))
3720 (frame (window-frame window))
3721 (parent (window-parent window))
3722 (function (window-parameter window 'split-window))
3723 (window-side (window-parameter window 'window-side))
3724 ;; Rebind the following two variables since in some cases we
3725 ;; have to override their value.
3726 (window-combination-limit window-combination-limit)
3727 (window-combination-resize window-combination-resize)
3728 atom-root)
3730 (window--check frame)
3731 (catch 'done
3732 (cond
3733 ;; Ignore window parameters if either `ignore-window-parameters'
3734 ;; is t or the `split-window' parameter equals t.
3735 ((or ignore-window-parameters (eq function t)))
3736 ((functionp function)
3737 ;; The `split-window' parameter specifies the function to call.
3738 ;; If that function is `ignore', do nothing.
3739 (throw 'done (funcall function window size side)))
3740 ;; If WINDOW is part of an atomic window, split the root window
3741 ;; of that atomic window instead.
3742 ((and (window-parameter window 'window-atom)
3743 (setq atom-root (window-atom-root window))
3744 (not (eq atom-root window)))
3745 (throw 'done (split-window atom-root size side)))
3746 ;; If WINDOW is a side window or its first or last child is a
3747 ;; side window, throw an error unless `window-combination-resize'
3748 ;; equals 'side.
3749 ((and (not (eq window-combination-resize 'side))
3750 (or (window-parameter window 'window-side)
3751 (and (window-child window)
3752 (or (window-parameter
3753 (window-child window) 'window-side)
3754 (window-parameter
3755 (window-last-child window) 'window-side)))))
3756 (error "Cannot split side window or parent of side window"))
3757 ;; If `window-combination-resize' is 'side and window has a side
3758 ;; window sibling, bind `window-combination-limit' to t.
3759 ((and (not (eq window-combination-resize 'side))
3760 (or (and (window-prev-sibling window)
3761 (window-parameter
3762 (window-prev-sibling window) 'window-side))
3763 (and (window-next-sibling window)
3764 (window-parameter
3765 (window-next-sibling window) 'window-side))))
3766 (setq window-combination-limit t)))
3768 ;; If `window-combination-resize' is t and SIZE is non-negative,
3769 ;; bind `window-combination-limit' to t.
3770 (when (and (eq window-combination-resize t) size (> size 0))
3771 (setq window-combination-limit t))
3773 (let* ((parent-size
3774 ;; `parent-size' is the size of WINDOW's parent, provided
3775 ;; it has one.
3776 (when parent (window-total-size parent horizontal)))
3777 ;; `resize' non-nil means we are supposed to resize other
3778 ;; windows in WINDOW's combination.
3779 (resize
3780 (and window-combination-resize
3781 (or (window-parameter window 'window-side)
3782 (not (eq window-combination-resize 'side)))
3783 (not (eq window-combination-limit t))
3784 ;; Resize makes sense in iso-combinations only.
3785 (window-combined-p window horizontal)))
3786 ;; `old-size' is the current size of WINDOW.
3787 (old-size (window-total-size window horizontal))
3788 ;; `new-size' is the specified or calculated size of the
3789 ;; new window.
3790 (new-size
3791 (cond
3792 ((not size)
3793 (max (window-split-min-size horizontal)
3794 (if resize
3795 ;; When resizing try to give the new window the
3796 ;; average size of a window in its combination.
3797 (min (- parent-size
3798 (window-min-size parent horizontal))
3799 (/ parent-size
3800 (1+ (window-combinations
3801 parent horizontal))))
3802 ;; Else try to give the new window half the size
3803 ;; of WINDOW (plus an eventual odd line).
3804 (+ (/ old-size 2) (% old-size 2)))))
3805 ((>= size 0)
3806 ;; SIZE non-negative specifies the new size of WINDOW.
3808 ;; Note: Specifying a non-negative SIZE is practically
3809 ;; always done as workaround for making the new window
3810 ;; appear above or on the left of the new window (the
3811 ;; ispell window is a typical example of that). In all
3812 ;; these cases the SIDE argument should be set to 'above
3813 ;; or 'left in order to support the 'resize option.
3814 ;; Here we have to nest the windows instead, see above.
3815 (- old-size size))
3817 ;; SIZE negative specifies the size of the new window.
3818 (- size))))
3819 new-parent new-normal)
3821 ;; Check SIZE.
3822 (cond
3823 ((not size)
3824 (cond
3825 (resize
3826 ;; SIZE unspecified, resizing.
3827 (when (and (not (window-sizable-p parent (- new-size) horizontal))
3828 ;; Try again with minimum split size.
3829 (setq new-size
3830 (max new-size (window-split-min-size horizontal)))
3831 (not (window-sizable-p parent (- new-size) horizontal)))
3832 (error "Window %s too small for splitting" parent)))
3833 ((> (+ new-size (window-min-size window horizontal)) old-size)
3834 ;; SIZE unspecified, no resizing.
3835 (error "Window %s too small for splitting" window))))
3836 ((and (>= size 0)
3837 (or (>= size old-size)
3838 (< new-size (if horizontal
3839 window-safe-min-width
3840 window-safe-min-width))))
3841 ;; SIZE specified as new size of old window. If the new size
3842 ;; is larger than the old size or the size of the new window
3843 ;; would be less than the safe minimum, signal an error.
3844 (error "Window %s too small for splitting" window))
3845 (resize
3846 ;; SIZE specified, resizing.
3847 (unless (window-sizable-p parent (- new-size) horizontal)
3848 ;; If we cannot resize the parent give up.
3849 (error "Window %s too small for splitting" parent)))
3850 ((or (< new-size
3851 (if horizontal window-safe-min-width window-safe-min-height))
3852 (< (- old-size new-size)
3853 (if horizontal window-safe-min-width window-safe-min-height)))
3854 ;; SIZE specification violates minimum size restrictions.
3855 (error "Window %s too small for splitting" window)))
3857 (window--resize-reset frame horizontal)
3859 (setq new-parent
3860 ;; Make new-parent non-nil if we need a new parent window;
3861 ;; either because we want to nest or because WINDOW is not
3862 ;; iso-combined.
3863 (or (eq window-combination-limit t)
3864 (not (window-combined-p window horizontal))))
3865 (setq new-normal
3866 ;; Make new-normal the normal size of the new window.
3867 (cond
3868 (size (/ (float new-size) (if new-parent old-size parent-size)))
3869 (new-parent 0.5)
3870 (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
3871 (t (/ (window-normal-size window horizontal) 2.0))))
3873 (if resize
3874 ;; Try to get space from OLD's siblings. We could go "up" and
3875 ;; try getting additional space from surrounding windows but
3876 ;; we won't be able to return space to those windows when we
3877 ;; delete the one we create here. Hence we do not go up.
3878 (progn
3879 (window--resize-child-windows parent (- new-size) horizontal)
3880 (let* ((normal (- 1.0 new-normal))
3881 (sub (window-child parent)))
3882 (while sub
3883 (set-window-new-normal
3884 sub (* (window-normal-size sub horizontal) normal))
3885 (setq sub (window-right sub)))))
3886 ;; Get entire space from WINDOW.
3887 (set-window-new-total window (- old-size new-size))
3888 (window--resize-this-window window (- new-size) horizontal)
3889 (set-window-new-normal
3890 window (- (if new-parent 1.0 (window-normal-size window horizontal))
3891 new-normal)))
3893 (let* ((new (split-window-internal window new-size side new-normal)))
3894 ;; Assign window-side parameters, if any.
3895 (when (eq window-combination-resize 'side)
3896 (let ((window-side
3897 (cond
3898 (window-side window-side)
3899 ((eq side 'above) 'top)
3900 ((eq side 'below) 'bottom)
3901 (t side))))
3902 ;; We made a new side window.
3903 (set-window-parameter new 'window-side window-side)
3904 (when (and new-parent (window-parameter window 'window-side))
3905 ;; We've been splitting a side root window. Give the
3906 ;; new parent the same window-side parameter.
3907 (set-window-parameter
3908 (window-parent new) 'window-side window-side))))
3910 (run-window-configuration-change-hook frame)
3911 (window--check frame)
3912 ;; Always return the new window.
3913 new)))))
3915 ;; I think this should be the default; I think people will prefer it--rms.
3916 (defcustom split-window-keep-point t
3917 "If non-nil, \\[split-window-below] preserves point in the new window.
3918 If nil, adjust point in the two windows to minimize redisplay.
3919 This option applies only to `split-window-below' and functions
3920 that call it. The low-level `split-window' function always keeps
3921 the original point in both windows."
3922 :type 'boolean
3923 :group 'windows)
3925 (defun split-window-below (&optional size)
3926 "Split the selected window into two windows, one above the other.
3927 The selected window is above. The newly split-off window is
3928 below, and displays the same buffer. Return the new window.
3930 If optional argument SIZE is omitted or nil, both windows get the
3931 same height, or close to it. If SIZE is positive, the upper
3932 \(selected) window gets SIZE lines. If SIZE is negative, the
3933 lower (new) window gets -SIZE lines.
3935 If the variable `split-window-keep-point' is non-nil, both
3936 windows get the same value of point as the selected window.
3937 Otherwise, the window starts are chosen so as to minimize the
3938 amount of redisplay; this is convenient on slow terminals."
3939 (interactive "P")
3940 (let ((old-window (selected-window))
3941 (old-point (window-point))
3942 (size (and size (prefix-numeric-value size)))
3943 moved-by-window-height moved new-window bottom)
3944 (when (and size (< size 0) (< (- size) window-min-height))
3945 ;; `split-window' would not signal an error here.
3946 (error "Size of new window too small"))
3947 (setq new-window (split-window nil size))
3948 (unless split-window-keep-point
3949 (with-current-buffer (window-buffer)
3950 ;; Use `save-excursion' around vertical movements below
3951 ;; (Bug#10971). Note: When the selected window's buffer has a
3952 ;; header line, up to two lines of the buffer may not show up
3953 ;; in the resulting configuration.
3954 (save-excursion
3955 (goto-char (window-start))
3956 (setq moved (vertical-motion (window-height)))
3957 (set-window-start new-window (point))
3958 (when (> (point) (window-point new-window))
3959 (set-window-point new-window (point)))
3960 (when (= moved (window-height))
3961 (setq moved-by-window-height t)
3962 (vertical-motion -1))
3963 (setq bottom (point)))
3964 (and moved-by-window-height
3965 (<= bottom (point))
3966 (set-window-point old-window (1- bottom)))
3967 (and moved-by-window-height
3968 (<= (window-start new-window) old-point)
3969 (set-window-point new-window old-point)
3970 (select-window new-window))))
3971 ;; Always copy quit-restore parameter in interactive use.
3972 (let ((quit-restore (window-parameter old-window 'quit-restore)))
3973 (when quit-restore
3974 (set-window-parameter new-window 'quit-restore quit-restore)))
3975 new-window))
3977 (defalias 'split-window-vertically 'split-window-below)
3979 (defun split-window-right (&optional size)
3980 "Split the selected window into two side-by-side windows.
3981 The selected window is on the left. The newly split-off window
3982 is on the right, and displays the same buffer. Return the new
3983 window.
3985 If optional argument SIZE is omitted or nil, both windows get the
3986 same width, or close to it. If SIZE is positive, the left-hand
3987 \(selected) window gets SIZE columns. If SIZE is negative, the
3988 right-hand (new) window gets -SIZE columns. Here, SIZE includes
3989 the width of the window's scroll bar; if there are no scroll
3990 bars, it includes the width of the divider column to the window's
3991 right, if any."
3992 (interactive "P")
3993 (let ((old-window (selected-window))
3994 (size (and size (prefix-numeric-value size)))
3995 new-window)
3996 (when (and size (< size 0) (< (- size) window-min-width))
3997 ;; `split-window' would not signal an error here.
3998 (error "Size of new window too small"))
3999 (setq new-window (split-window nil size t))
4000 ;; Always copy quit-restore parameter in interactive use.
4001 (let ((quit-restore (window-parameter old-window 'quit-restore)))
4002 (when quit-restore
4003 (set-window-parameter new-window 'quit-restore quit-restore)))
4004 new-window))
4006 (defalias 'split-window-horizontally 'split-window-right)
4008 ;;; Balancing windows.
4010 ;; The following routine uses the recycled code from an old version of
4011 ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
4012 ;; new `window--resize-child-windows' code does would hardly make it any shorter or
4013 ;; more readable (FWIW we'd need three loops - one to calculate the
4014 ;; minimum sizes per window, one to enlarge or shrink windows until the
4015 ;; new parent-size matches, and one where we shrink the largest/enlarge
4016 ;; the smallest window).
4017 (defun balance-windows-2 (window horizontal)
4018 "Subroutine of `balance-windows-1'.
4019 WINDOW must be a vertical combination (horizontal if HORIZONTAL
4020 is non-nil)."
4021 (let* ((first (window-child window))
4022 (sub first)
4023 (number-of-children 0)
4024 (parent-size (window-new-total window))
4025 (total-sum parent-size)
4026 failed size sub-total sub-delta sub-amount rest)
4027 (while sub
4028 (setq number-of-children (1+ number-of-children))
4029 (when (window-size-fixed-p sub horizontal)
4030 (setq total-sum
4031 (- total-sum (window-total-size sub horizontal)))
4032 (set-window-new-normal sub 'ignore))
4033 (setq sub (window-right sub)))
4035 (setq failed t)
4036 (while (and failed (> number-of-children 0))
4037 (setq size (/ total-sum number-of-children))
4038 (setq failed nil)
4039 (setq sub first)
4040 (while (and sub (not failed))
4041 ;; Ignore child windows that should be ignored or are stuck.
4042 (unless (window--resize-child-windows-skip-p sub)
4043 (setq sub-total (window-total-size sub horizontal))
4044 (setq sub-delta (- size sub-total))
4045 (setq sub-amount
4046 (window-sizable sub sub-delta horizontal))
4047 ;; Register the new total size for this child window.
4048 (set-window-new-total sub (+ sub-total sub-amount))
4049 (unless (= sub-amount sub-delta)
4050 (setq total-sum (- total-sum sub-total sub-amount))
4051 (setq number-of-children (1- number-of-children))
4052 ;; We failed and need a new round.
4053 (setq failed t)
4054 (set-window-new-normal sub 'skip)))
4055 (setq sub (window-right sub))))
4057 (setq rest (% total-sum number-of-children))
4058 ;; Fix rounding by trying to enlarge non-stuck windows by one line
4059 ;; (column) until `rest' is zero.
4060 (setq sub first)
4061 (while (and sub (> rest 0))
4062 (unless (window--resize-child-windows-skip-p window)
4063 (set-window-new-total sub 1 t)
4064 (setq rest (1- rest)))
4065 (setq sub (window-right sub)))
4067 ;; Fix rounding by trying to enlarge stuck windows by one line
4068 ;; (column) until `rest' equals zero.
4069 (setq sub first)
4070 (while (and sub (> rest 0))
4071 (unless (eq (window-new-normal sub) 'ignore)
4072 (set-window-new-total sub 1 t)
4073 (setq rest (1- rest)))
4074 (setq sub (window-right sub)))
4076 (setq sub first)
4077 (while sub
4078 ;; Record new normal sizes.
4079 (set-window-new-normal
4080 sub (/ (if (eq (window-new-normal sub) 'ignore)
4081 (window-total-size sub horizontal)
4082 (window-new-total sub))
4083 (float parent-size)))
4084 ;; Recursively balance each window's child windows.
4085 (balance-windows-1 sub horizontal)
4086 (setq sub (window-right sub)))))
4088 (defun balance-windows-1 (window &optional horizontal)
4089 "Subroutine of `balance-windows'."
4090 (if (window-child window)
4091 (let ((sub (window-child window)))
4092 (if (window-combined-p sub horizontal)
4093 (balance-windows-2 window horizontal)
4094 (let ((size (window-new-total window)))
4095 (while sub
4096 (set-window-new-total sub size)
4097 (balance-windows-1 sub horizontal)
4098 (setq sub (window-right sub))))))))
4100 (defun balance-windows (&optional window-or-frame)
4101 "Balance the sizes of windows of WINDOW-OR-FRAME.
4102 WINDOW-OR-FRAME is optional and defaults to the selected frame.
4103 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
4104 windows of that frame. If WINDOW-OR-FRAME denotes a window,
4105 recursively balance the sizes of all child windows of that
4106 window."
4107 (interactive)
4108 (let* ((window
4109 (cond
4110 ((or (not window-or-frame)
4111 (frame-live-p window-or-frame))
4112 (frame-root-window window-or-frame))
4113 ((or (window-live-p window-or-frame)
4114 (window-child window-or-frame))
4115 window-or-frame)
4117 (error "Not a window or frame %s" window-or-frame))))
4118 (frame (window-frame window)))
4119 ;; Balance vertically.
4120 (window--resize-reset (window-frame window))
4121 (balance-windows-1 window)
4122 (window-resize-apply frame)
4123 ;; Balance horizontally.
4124 (window--resize-reset (window-frame window) t)
4125 (balance-windows-1 window t)
4126 (window-resize-apply frame t)))
4128 (defun window-fixed-size-p (&optional window direction)
4129 "Return t if WINDOW cannot be resized in DIRECTION.
4130 WINDOW defaults to the selected window. DIRECTION can be
4131 nil (i.e. any), `height' or `width'."
4132 (with-current-buffer (window-buffer window)
4133 (when (and (boundp 'window-size-fixed) window-size-fixed)
4134 (not (and direction
4135 (member (cons direction window-size-fixed)
4136 '((height . width) (width . height))))))))
4138 ;;; A different solution to balance-windows.
4139 (defvar window-area-factor 1
4140 "Factor by which the window area should be over-estimated.
4141 This is used by `balance-windows-area'.
4142 Changing this globally has no effect.")
4143 (make-variable-buffer-local 'window-area-factor)
4145 (defun balance-windows-area-adjust (window delta horizontal)
4146 "Wrapper around `window-resize' with error checking.
4147 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
4148 ;; `window-resize' may fail if delta is too large.
4149 (while (>= (abs delta) 1)
4150 (condition-case nil
4151 (progn
4152 (window-resize window delta horizontal)
4153 (setq delta 0))
4154 (error
4155 ;;(message "adjust: %s" (error-message-string err))
4156 (setq delta (/ delta 2))))))
4158 (defun balance-windows-area ()
4159 "Make all visible windows the same area (approximately).
4160 See also `window-area-factor' to change the relative size of
4161 specific buffers."
4162 (interactive)
4163 (let* ((unchanged 0) (carry 0) (round 0)
4164 ;; Remove fixed-size windows.
4165 (wins (delq nil (mapcar (lambda (win)
4166 (if (not (window-fixed-size-p win)) win))
4167 (window-list nil 'nomini))))
4168 (changelog nil)
4169 next)
4170 ;; Resizing a window changes the size of surrounding windows in complex
4171 ;; ways, so it's difficult to balance them all. The introduction of
4172 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
4173 ;; very difficult to do. `balance-window' above takes an off-line
4174 ;; approach: get the whole window tree, then balance it, then try to
4175 ;; adjust the windows so they fit the result.
4176 ;; Here, instead, we take a "local optimization" approach, where we just
4177 ;; go through all the windows several times until nothing needs to be
4178 ;; changed. The main problem with this approach is that it's difficult
4179 ;; to make sure it terminates, so we use some heuristic to try and break
4180 ;; off infinite loops.
4181 ;; After a round without any change, we allow a second, to give a chance
4182 ;; to the carry to propagate a minor imbalance from the end back to
4183 ;; the beginning.
4184 (while (< unchanged 2)
4185 ;; (message "New round")
4186 (setq unchanged (1+ unchanged) round (1+ round))
4187 (dolist (win wins)
4188 (setq next win)
4189 (while (progn (setq next (next-window next))
4190 (window-fixed-size-p next)))
4191 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
4192 (let* ((horiz
4193 (< (car (window-edges win)) (car (window-edges next))))
4194 (areadiff (/ (- (* (window-height next) (window-width next)
4195 (buffer-local-value 'window-area-factor
4196 (window-buffer next)))
4197 (* (window-height win) (window-width win)
4198 (buffer-local-value 'window-area-factor
4199 (window-buffer win))))
4200 (max (buffer-local-value 'window-area-factor
4201 (window-buffer win))
4202 (buffer-local-value 'window-area-factor
4203 (window-buffer next)))))
4204 (edgesize (if horiz
4205 (+ (window-height win) (window-height next))
4206 (+ (window-width win) (window-width next))))
4207 (diff (/ areadiff edgesize)))
4208 (when (zerop diff)
4209 ;; Maybe diff is actually closer to 1 than to 0.
4210 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
4211 (when (and (zerop diff) (not (zerop areadiff)))
4212 (setq diff (/ (+ areadiff carry) edgesize))
4213 ;; Change things smoothly.
4214 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
4215 (if (zerop diff)
4216 ;; Make sure negligible differences don't accumulate to
4217 ;; become significant.
4218 (setq carry (+ carry areadiff))
4219 ;; This used `adjust-window-trailing-edge' before and uses
4220 ;; `window-resize' now. Error wrapping is still needed.
4221 (balance-windows-area-adjust win diff horiz)
4222 ;; (sit-for 0.5)
4223 (let ((change (cons win (window-edges win))))
4224 ;; If the same change has been seen already for this window,
4225 ;; we're most likely in an endless loop, so don't count it as
4226 ;; a change.
4227 (unless (member change changelog)
4228 (push change changelog)
4229 (setq unchanged 0 carry 0)))))))
4230 ;; We've now basically balanced all the windows.
4231 ;; But there may be some minor off-by-one imbalance left over,
4232 ;; so let's do some fine tuning.
4233 ;; (bw-finetune wins)
4234 ;; (message "Done in %d rounds" round)
4237 ;;; Window states, how to get them and how to put them in a window.
4238 (defun window--state-get-1 (window &optional writable)
4239 "Helper function for `window-state-get'."
4240 (let* ((type
4241 (cond
4242 ((window-top-child window) 'vc)
4243 ((window-left-child window) 'hc)
4244 (t 'leaf)))
4245 (buffer (window-buffer window))
4246 (selected (eq window (selected-window)))
4247 (head
4248 `(,type
4249 ,@(unless (window-next-sibling window) `((last . t)))
4250 (total-height . ,(window-total-size window))
4251 (total-width . ,(window-total-size window t))
4252 (normal-height . ,(window-normal-size window))
4253 (normal-width . ,(window-normal-size window t))
4254 (combination-limit . ,(window-combination-limit window))
4255 ,@(let ((parameters (window-parameters window))
4256 list)
4257 ;; Make copies of those window parameters whose
4258 ;; persistence property is `writable' if WRITABLE is
4259 ;; non-nil and non-nil if WRITABLE is nil.
4260 (dolist (par parameters)
4261 (let ((pers (cdr (assq (car par)
4262 window-persistent-parameters))))
4263 (when (and pers (or (not writable) (eq pers 'writable)))
4264 (setq list (cons (cons (car par) (cdr par)) list)))))
4265 ;; Add `clone-of' parameter if necessary.
4266 (let ((pers (cdr (assq 'clone-of
4267 window-persistent-parameters))))
4268 (when (and pers (or (not writable) (eq pers 'writable))
4269 (not (assq 'clone-of list)))
4270 (setq list (cons (cons 'clone-of window) list))))
4271 (when list
4272 `((parameters . ,list))))
4273 ,@(when buffer
4274 ;; All buffer related things go in here.
4275 (let ((point (window-point window))
4276 (start (window-start window)))
4277 `((buffer
4278 ,(buffer-name buffer)
4279 (selected . ,selected)
4280 (hscroll . ,(window-hscroll window))
4281 (fringes . ,(window-fringes window))
4282 (margins . ,(window-margins window))
4283 (scroll-bars . ,(window-scroll-bars window))
4284 (vscroll . ,(window-vscroll window))
4285 (dedicated . ,(window-dedicated-p window))
4286 (point . ,(if writable point
4287 (copy-marker point
4288 (buffer-local-value
4289 'window-point-insertion-type
4290 buffer))))
4291 (start . ,(if writable start (copy-marker start)))))))))
4292 (tail
4293 (when (memq type '(vc hc))
4294 (let (list)
4295 (setq window (window-child window))
4296 (while window
4297 (setq list (cons (window--state-get-1 window writable) list))
4298 (setq window (window-right window)))
4299 (nreverse list)))))
4300 (append head tail)))
4302 (defun window-state-get (&optional window writable)
4303 "Return state of WINDOW as a Lisp object.
4304 WINDOW can be any window and defaults to the root window of the
4305 selected frame.
4307 Optional argument WRITABLE non-nil means do not use markers for
4308 sampling `window-point' and `window-start'. Together, WRITABLE
4309 and the variable `window-persistent-parameters' specify which
4310 window parameters are saved by this function. WRITABLE should be
4311 non-nil when the return value shall be written to a file and read
4312 back in another session. Otherwise, an application may run into
4313 an `invalid-read-syntax' error while attempting to read back the
4314 value from file.
4316 The return value can be used as argument for `window-state-put'
4317 to put the state recorded here into an arbitrary window. The
4318 value can be also stored on disk and read back in a new session."
4319 (setq window
4320 (if window
4321 (if (window-valid-p window)
4322 window
4323 (error "%s is not a live or internal window" window))
4324 (frame-root-window)))
4325 ;; The return value is a cons whose car specifies some constraints on
4326 ;; the size of WINDOW. The cdr lists the states of the child windows
4327 ;; of WINDOW.
4328 (cons
4329 ;; Frame related things would go into a function, say `frame-state',
4330 ;; calling `window-state-get' to insert the frame's root window.
4331 `((min-height . ,(window-min-size window))
4332 (min-width . ,(window-min-size window t))
4333 (min-height-ignore . ,(window-min-size window nil t))
4334 (min-width-ignore . ,(window-min-size window t t))
4335 (min-height-safe . ,(window-min-size window nil 'safe))
4336 (min-width-safe . ,(window-min-size window t 'safe)))
4337 (window--state-get-1 window writable)))
4339 (defvar window-state-put-list nil
4340 "Helper variable for `window-state-put'.")
4342 (defun window--state-put-1 (state &optional window ignore totals)
4343 "Helper function for `window-state-put'."
4344 (let ((type (car state)))
4345 (setq state (cdr state))
4346 (cond
4347 ((eq type 'leaf)
4348 ;; For a leaf window just add unprocessed entries to
4349 ;; `window-state-put-list'.
4350 (push (cons window state) window-state-put-list))
4351 ((memq type '(vc hc))
4352 (let* ((horizontal (eq type 'hc))
4353 (total (window-total-size window horizontal))
4354 (first t)
4355 size new)
4356 (dolist (item state)
4357 ;; Find the next child window. WINDOW always points to the
4358 ;; real window that we want to fill with what we find here.
4359 (when (memq (car item) '(leaf vc hc))
4360 (if (assq 'last item)
4361 ;; The last child window. Below `window--state-put-1'
4362 ;; will put into it whatever ITEM has in store.
4363 (setq new nil)
4364 ;; Not the last child window, prepare for splitting
4365 ;; WINDOW. SIZE is the new (and final) size of the old
4366 ;; window.
4367 (setq size
4368 (if totals
4369 ;; Use total size.
4370 (cdr (assq (if horizontal 'total-width 'total-height) item))
4371 ;; Use normalized size and round.
4372 (round (* total
4373 (cdr (assq
4374 (if horizontal 'normal-width 'normal-height)
4375 item))))))
4377 ;; Use safe sizes, we try to resize later.
4378 (setq size (max size (if horizontal
4379 window-safe-min-height
4380 window-safe-min-width)))
4382 (if (window-sizable-p window (- size) horizontal 'safe)
4383 (let* ((window-combination-limit
4384 (assq 'combination-limit item)))
4385 ;; We must inherit the combination limit, otherwise
4386 ;; we might mess up handling of atomic and side
4387 ;; window.
4388 (setq new (split-window window size horizontal)))
4389 ;; Give up if we can't resize window down to safe sizes.
4390 (error "Cannot resize window %s" window))
4392 (when first
4393 (setq first nil)
4394 ;; When creating the first child window add for parent
4395 ;; unprocessed entries to `window-state-put-list'.
4396 (setq window-state-put-list
4397 (cons (cons (window-parent window) state)
4398 window-state-put-list))))
4400 ;; Now process the current window (either the one we've just
4401 ;; split or the last child of its parent).
4402 (window--state-put-1 item window ignore totals)
4403 ;; Continue with the last window split off.
4404 (setq window new))))))))
4406 (defun window--state-put-2 (ignore)
4407 "Helper function for `window-state-put'."
4408 (dolist (item window-state-put-list)
4409 (let ((window (car item))
4410 (combination-limit (cdr (assq 'combination-limit item)))
4411 (parameters (cdr (assq 'parameters item)))
4412 (state (cdr (assq 'buffer item))))
4413 (when combination-limit
4414 (set-window-combination-limit window combination-limit))
4415 ;; Reset window's parameters and assign saved ones (we might want
4416 ;; a `remove-window-parameters' function here).
4417 (dolist (parameter (window-parameters window))
4418 (set-window-parameter window (car parameter) nil))
4419 (when parameters
4420 (dolist (parameter parameters)
4421 (set-window-parameter window (car parameter) (cdr parameter))))
4422 ;; Process buffer related state.
4423 (when state
4424 ;; We don't want to raise an error here so we create a buffer if
4425 ;; there's none.
4426 (set-window-buffer window (get-buffer-create (car state)))
4427 (with-current-buffer (window-buffer window)
4428 (set-window-hscroll window (cdr (assq 'hscroll state)))
4429 (apply 'set-window-fringes
4430 (cons window (cdr (assq 'fringes state))))
4431 (let ((margins (cdr (assq 'margins state))))
4432 (set-window-margins window (car margins) (cdr margins)))
4433 (let ((scroll-bars (cdr (assq 'scroll-bars state))))
4434 (set-window-scroll-bars
4435 window (car scroll-bars) (nth 2 scroll-bars) (nth 3 scroll-bars)))
4436 (set-window-vscroll window (cdr (assq 'vscroll state)))
4437 ;; Adjust vertically.
4438 (if (memq window-size-fixed '(t height))
4439 ;; A fixed height window, try to restore the original size.
4440 (let ((delta (- (cdr (assq 'total-height item))
4441 (window-total-height window)))
4442 window-size-fixed)
4443 (when (window--resizable-p window delta)
4444 (window-resize window delta)))
4445 ;; Else check whether the window is not high enough.
4446 (let* ((min-size (window-min-size window nil ignore))
4447 (delta (- min-size (window-total-size window))))
4448 (when (and (> delta 0)
4449 (window--resizable-p window delta nil ignore))
4450 (window-resize window delta nil ignore))))
4451 ;; Adjust horizontally.
4452 (if (memq window-size-fixed '(t width))
4453 ;; A fixed width window, try to restore the original size.
4454 (let ((delta (- (cdr (assq 'total-width item))
4455 (window-total-width window)))
4456 window-size-fixed)
4457 (when (window--resizable-p window delta)
4458 (window-resize window delta)))
4459 ;; Else check whether the window is not wide enough.
4460 (let* ((min-size (window-min-size window t ignore))
4461 (delta (- min-size (window-total-size window t))))
4462 (when (and (> delta 0)
4463 (window--resizable-p window delta t ignore))
4464 (window-resize window delta t ignore))))
4465 ;; Set dedicated status.
4466 (set-window-dedicated-p window (cdr (assq 'dedicated state)))
4467 ;; Install positions (maybe we should do this after all windows
4468 ;; have been created and sized).
4469 (ignore-errors
4470 (set-window-start window (cdr (assq 'start state)))
4471 (set-window-point window (cdr (assq 'point state))))
4472 ;; Select window if it's the selected one.
4473 (when (cdr (assq 'selected state))
4474 (select-window window)))))))
4476 (defun window-state-put (state &optional window ignore)
4477 "Put window state STATE into WINDOW.
4478 STATE should be the state of a window returned by an earlier
4479 invocation of `window-state-get'. Optional argument WINDOW must
4480 specify a live window and defaults to the selected one.
4482 Optional argument IGNORE non-nil means ignore minimum window
4483 sizes and fixed size restrictions. IGNORE equal `safe' means
4484 windows can get as small as `window-safe-min-height' and
4485 `window-safe-min-width'."
4486 (setq window (window-normalize-window window t))
4487 (let* ((frame (window-frame window))
4488 (head (car state))
4489 ;; We check here (1) whether the total sizes of root window of
4490 ;; STATE and that of WINDOW are equal so we can avoid
4491 ;; calculating new sizes, and (2) if we do have to resize
4492 ;; whether we can do so without violating size restrictions.
4493 (totals
4494 (and (= (window-total-size window)
4495 (cdr (assq 'total-height state)))
4496 (= (window-total-size window t)
4497 (cdr (assq 'total-width state)))))
4498 (min-height (cdr (assq 'min-height head)))
4499 (min-width (cdr (assq 'min-width head))))
4500 (if (and (not totals)
4501 (or (> min-height (window-total-size window))
4502 (> min-width (window-total-size window t)))
4503 (or (not ignore)
4504 (and (setq min-height
4505 (cdr (assq 'min-height-ignore head)))
4506 (setq min-width
4507 (cdr (assq 'min-width-ignore head)))
4508 (or (> min-height (window-total-size window))
4509 (> min-width (window-total-size window t)))
4510 (or (not (eq ignore 'safe))
4511 (and (setq min-height
4512 (cdr (assq 'min-height-safe head)))
4513 (setq min-width
4514 (cdr (assq 'min-width-safe head)))
4515 (or (> min-height
4516 (window-total-size window))
4517 (> min-width
4518 (window-total-size window t))))))))
4519 ;; The check above might not catch all errors due to rounding
4520 ;; issues - so IGNORE equal 'safe might not always produce the
4521 ;; minimum possible state. But such configurations hardly make
4522 ;; sense anyway.
4523 (error "Window %s too small to accommodate state" window)
4524 (setq state (cdr state))
4525 (setq window-state-put-list nil)
4526 ;; Work on the windows of a temporary buffer to make sure that
4527 ;; splitting proceeds regardless of any buffer local values of
4528 ;; `window-size-fixed'. Release that buffer after the buffers of
4529 ;; all live windows have been set by `window--state-put-2'.
4530 (with-temp-buffer
4531 (set-window-buffer window (current-buffer))
4532 (window--state-put-1 state window nil totals)
4533 (window--state-put-2 ignore))
4534 (window--check frame))))
4536 (defun display-buffer-record-window (type window buffer)
4537 "Record information for window used by `display-buffer'.
4538 TYPE specifies the type of the calling operation and must be one
4539 of the symbols 'reuse (when WINDOW existed already and was
4540 reused for displaying BUFFER), 'window (when WINDOW was created
4541 on an already existing frame), or 'frame (when WINDOW was
4542 created on a new frame). WINDOW is the window used for or created
4543 by the `display-buffer' routines. BUFFER is the buffer that
4544 shall be displayed.
4546 This function installs or updates the quit-restore parameter of
4547 WINDOW. The quit-restore parameter is a list of four elements:
4548 The first element is one of the symbols 'window, 'frame, 'same or
4549 'other. The second element is either one of the symbols 'window
4550 or 'frame or a list whose elements are the buffer previously
4551 shown in the window, that buffer's window start and window point,
4552 and the window's height. The third element is the window
4553 selected at the time the parameter was created. The fourth
4554 element is BUFFER."
4555 (cond
4556 ((eq type 'reuse)
4557 (if (eq (window-buffer window) buffer)
4558 ;; WINDOW shows BUFFER already.
4559 (when (consp (window-parameter window 'quit-restore))
4560 ;; If WINDOW has a quit-restore parameter, reset its car.
4561 (setcar (window-parameter window 'quit-restore) 'same))
4562 ;; WINDOW shows another buffer.
4563 (with-current-buffer (window-buffer window)
4564 (set-window-parameter
4565 window 'quit-restore
4566 (list 'other
4567 ;; A quadruple of WINDOW's buffer, start, point and height.
4568 (list (current-buffer) (window-start window)
4569 ;; Preserve window-point-insertion-type (Bug#12588).
4570 (copy-marker
4571 (window-point window) window-point-insertion-type)
4572 (window-total-size window))
4573 (selected-window) buffer)))))
4574 ((eq type 'window)
4575 ;; WINDOW has been created on an existing frame.
4576 (set-window-parameter
4577 window 'quit-restore
4578 (list 'window 'window (selected-window) buffer)))
4579 ((eq type 'frame)
4580 ;; WINDOW has been created on a new frame.
4581 (set-window-parameter
4582 window 'quit-restore
4583 (list 'frame 'frame (selected-window) buffer)))))
4585 (defcustom display-buffer-function nil
4586 "If non-nil, function to call to handle `display-buffer'.
4587 It will receive two args, the buffer and a flag which if non-nil
4588 means that the currently selected window is not acceptable. It
4589 should choose or create a window, display the specified buffer in
4590 it, and return the window.
4592 The specified function should call `display-buffer-record-window'
4593 with corresponding arguments to set up the quit-restore parameter
4594 of the window used."
4595 :type '(choice
4596 (const nil)
4597 (function :tag "function"))
4598 :group 'windows)
4600 (make-obsolete-variable 'display-buffer-function
4601 'display-buffer-alist "24.3")
4603 ;; Eventually, we want to turn this into a defvar; instead of
4604 ;; customizing this, the user should use a `pop-up-frame-parameters'
4605 ;; alist entry in `display-buffer-base-action'.
4606 (defcustom pop-up-frame-alist nil
4607 "Alist of parameters for automatically generated new frames.
4608 If non-nil, the value you specify here is used by the default
4609 `pop-up-frame-function' for the creation of new frames.
4611 Since `pop-up-frame-function' is used by `display-buffer' for
4612 making new frames, any value specified here by default affects
4613 the automatic generation of new frames via `display-buffer' and
4614 all functions based on it. The behavior of `make-frame' is not
4615 affected by this variable."
4616 :type '(repeat (cons :format "%v"
4617 (symbol :tag "Parameter")
4618 (sexp :tag "Value")))
4619 :group 'frames)
4621 (defcustom pop-up-frame-function
4622 (lambda () (make-frame pop-up-frame-alist))
4623 "Function used by `display-buffer' for creating a new frame.
4624 This function is called with no arguments and should return a new
4625 frame. The default value calls `make-frame' with the argument
4626 `pop-up-frame-alist'."
4627 :type 'function
4628 :group 'frames)
4630 (defcustom special-display-buffer-names nil
4631 "List of names of buffers that should be displayed specially.
4632 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4633 its name is in this list, displays the buffer in a way specified
4634 by `special-display-function'. `special-display-popup-frame'
4635 \(the default for `special-display-function') usually displays
4636 the buffer in a separate frame made with the parameters specified
4637 by `special-display-frame-alist'. If `special-display-function'
4638 has been set to some other function, that function is called with
4639 the buffer as first, and nil as second argument.
4641 Alternatively, an element of this list can be specified as
4642 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
4643 name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4644 `special-display-popup-frame' will interpret such pairs as frame
4645 parameters when it creates a special frame, overriding the
4646 corresponding values from `special-display-frame-alist'.
4648 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4649 `special-display-popup-frame' displays that buffer in the
4650 selected window. If FRAME-PARAMETERS contains (same-frame . t),
4651 it displays that buffer in a window on the selected frame.
4653 If `special-display-function' specifies some other function than
4654 `special-display-popup-frame', that function is called with the
4655 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
4656 argument.
4658 Finally, an element of this list can be also specified as
4659 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
4660 `special-display-popup-frame' will call FUNCTION with the buffer
4661 named BUFFER-NAME as first argument, and OTHER-ARGS as the
4662 second.
4664 Any alternative function specified here is responsible for
4665 setting up the quit-restore parameter of the window used.
4667 If this variable appears \"not to work\", because you added a
4668 name to it but the corresponding buffer is displayed in the
4669 selected window, look at the values of `same-window-buffer-names'
4670 and `same-window-regexps'. Those variables take precedence over
4671 this one.
4673 See also `special-display-regexps'."
4674 :type '(repeat
4675 (choice :tag "Buffer"
4676 :value ""
4677 (string :format "%v")
4678 (cons :tag "With parameters"
4679 :format "%v"
4680 :value ("" . nil)
4681 (string :format "%v")
4682 (repeat :tag "Parameters"
4683 (cons :format "%v"
4684 (symbol :tag "Parameter")
4685 (sexp :tag "Value"))))
4686 (list :tag "With function"
4687 :format "%v"
4688 :value ("" . nil)
4689 (string :format "%v")
4690 (function :tag "Function")
4691 (repeat :tag "Arguments" (sexp)))))
4692 :group 'windows
4693 :group 'frames)
4694 (make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3")
4695 (put 'special-display-buffer-names 'risky-local-variable t)
4697 (defcustom special-display-regexps nil
4698 "List of regexps saying which buffers should be displayed specially.
4699 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
4700 any regexp in this list matches its name, displays it specially
4701 using `special-display-function'. `special-display-popup-frame'
4702 \(the default for `special-display-function') usually displays
4703 the buffer in a separate frame made with the parameters specified
4704 by `special-display-frame-alist'. If `special-display-function'
4705 has been set to some other function, that function is called with
4706 the buffer as first, and nil as second argument.
4708 Alternatively, an element of this list can be specified as
4709 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
4710 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
4711 `special-display-popup-frame' will then interpret these pairs as
4712 frame parameters when creating a special frame for a buffer whose
4713 name matches REGEXP, overriding the corresponding values from
4714 `special-display-frame-alist'.
4716 As a special case, if FRAME-PARAMETERS contains (same-window . t)
4717 `special-display-popup-frame' displays buffers matching REGEXP in
4718 the selected window. (same-frame . t) in FRAME-PARAMETERS means
4719 to display such buffers in a window on the selected frame.
4721 If `special-display-function' specifies some other function than
4722 `special-display-popup-frame', that function is called with the
4723 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
4724 as second argument.
4726 Finally, an element of this list can be also specified as
4727 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
4728 will then call FUNCTION with the buffer whose name matched
4729 REGEXP as first, and OTHER-ARGS as second argument.
4731 Any alternative function specified here is responsible for
4732 setting up the quit-restore parameter of the window used.
4734 If this variable appears \"not to work\", because you added a
4735 name to it but the corresponding buffer is displayed in the
4736 selected window, look at the values of `same-window-buffer-names'
4737 and `same-window-regexps'. Those variables take precedence over
4738 this one.
4740 See also `special-display-buffer-names'."
4741 :type '(repeat
4742 (choice :tag "Buffer"
4743 :value ""
4744 (regexp :format "%v")
4745 (cons :tag "With parameters"
4746 :format "%v"
4747 :value ("" . nil)
4748 (regexp :format "%v")
4749 (repeat :tag "Parameters"
4750 (cons :format "%v"
4751 (symbol :tag "Parameter")
4752 (sexp :tag "Value"))))
4753 (list :tag "With function"
4754 :format "%v"
4755 :value ("" . nil)
4756 (regexp :format "%v")
4757 (function :tag "Function")
4758 (repeat :tag "Arguments" (sexp)))))
4759 :group 'windows
4760 :group 'frames)
4761 (make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3")
4762 (put 'special-display-regexps 'risky-local-variable t)
4764 (defun special-display-p (buffer-name)
4765 "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
4766 More precisely, return t if `special-display-buffer-names' or
4767 `special-display-regexps' contain a string entry equaling or
4768 matching BUFFER-NAME. If `special-display-buffer-names' or
4769 `special-display-regexps' contain a list entry whose car equals
4770 or matches BUFFER-NAME, the return value is the cdr of that
4771 entry."
4772 (let (tmp)
4773 (cond
4774 ((member buffer-name special-display-buffer-names)
4776 ((setq tmp (assoc buffer-name special-display-buffer-names))
4777 (cdr tmp))
4778 ((catch 'found
4779 (dolist (regexp special-display-regexps)
4780 (cond
4781 ((stringp regexp)
4782 (when (string-match-p regexp buffer-name)
4783 (throw 'found t)))
4784 ((and (consp regexp) (stringp (car regexp))
4785 (string-match-p (car regexp) buffer-name))
4786 (throw 'found (cdr regexp))))))))))
4788 (defcustom special-display-frame-alist
4789 '((height . 14) (width . 80) (unsplittable . t))
4790 "Alist of parameters for special frames.
4791 Special frames are used for buffers whose names are listed in
4792 `special-display-buffer-names' and for buffers whose names match
4793 one of the regular expressions in `special-display-regexps'.
4795 This variable can be set in your init file, like this:
4797 (setq special-display-frame-alist '((width . 80) (height . 20)))
4799 These supersede the values given in `default-frame-alist'."
4800 :type '(repeat (cons :format "%v"
4801 (symbol :tag "Parameter")
4802 (sexp :tag "Value")))
4803 :group 'frames)
4804 (make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3")
4806 (defun special-display-popup-frame (buffer &optional args)
4807 "Pop up a frame displaying BUFFER and return its window.
4808 If BUFFER is already displayed in a visible or iconified frame,
4809 raise that frame. Otherwise, display BUFFER in a new frame.
4811 Optional argument ARGS is a list specifying additional
4812 information.
4814 If ARGS is an alist, use it as a list of frame parameters. If
4815 these parameters contain (same-window . t), display BUFFER in
4816 the selected window. If they contain (same-frame . t), display
4817 BUFFER in a window of the selected frame.
4819 If ARGS is a list whose car is a symbol, use (car ARGS) as a
4820 function to do the work. Pass it BUFFER as first argument, and
4821 pass the elements of (cdr ARGS) as the remaining arguments."
4822 (if (and args (symbolp (car args)))
4823 (apply (car args) buffer (cdr args))
4824 (let ((window (get-buffer-window buffer 0)))
4826 ;; If we have a window already, make it visible.
4827 (when window
4828 (let ((frame (window-frame window)))
4829 (make-frame-visible frame)
4830 (raise-frame frame)
4831 (display-buffer-record-window 'reuse window buffer)
4832 window))
4833 ;; Reuse the current window if the user requested it.
4834 (when (cdr (assq 'same-window args))
4835 (condition-case nil
4836 (progn (switch-to-buffer buffer nil t) (selected-window))
4837 (error nil)))
4838 ;; Stay on the same frame if requested.
4839 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
4840 (let* ((pop-up-windows t)
4841 pop-up-frames
4842 special-display-buffer-names special-display-regexps)
4843 (display-buffer buffer)))
4844 ;; If no window yet, make one in a new frame.
4845 (let* ((frame
4846 (with-current-buffer buffer
4847 (make-frame (append args special-display-frame-alist))))
4848 (window (frame-selected-window frame)))
4849 (display-buffer-record-window 'frame window buffer)
4850 (unless (eq buffer (window-buffer window))
4851 (set-window-buffer window buffer)
4852 (set-window-prev-buffers window nil))
4853 (set-window-dedicated-p window t)
4854 window)))))
4856 (defcustom special-display-function 'special-display-popup-frame
4857 "Function to call for displaying special buffers.
4858 This function is called with two arguments - the buffer and,
4859 optionally, a list - and should return a window displaying that
4860 buffer. The default value usually makes a separate frame for the
4861 buffer using `special-display-frame-alist' to specify the frame
4862 parameters. See the definition of `special-display-popup-frame'
4863 for how to specify such a function.
4865 A buffer is special when its name is either listed in
4866 `special-display-buffer-names' or matches a regexp in
4867 `special-display-regexps'.
4869 The specified function should call `display-buffer-record-window'
4870 with corresponding arguments to set up the quit-restore parameter
4871 of the window used."
4872 :type 'function
4873 :group 'frames)
4874 (make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3")
4876 (defcustom same-window-buffer-names nil
4877 "List of names of buffers that should appear in the \"same\" window.
4878 `display-buffer' and `pop-to-buffer' show a buffer whose name is
4879 on this list in the selected rather than some other window.
4881 An element of this list can be a cons cell instead of just a
4882 string. In that case, the cell's car must be a string specifying
4883 the buffer name. This is for compatibility with
4884 `special-display-buffer-names'; the cdr of the cons cell is
4885 ignored.
4887 See also `same-window-regexps'."
4888 :type '(repeat (string :format "%v"))
4889 :group 'windows)
4891 (defcustom same-window-regexps nil
4892 "List of regexps saying which buffers should appear in the \"same\" window.
4893 `display-buffer' and `pop-to-buffer' show a buffer whose name
4894 matches a regexp on this list in the selected rather than some
4895 other window.
4897 An element of this list can be a cons cell instead of just a
4898 string. In that case, the cell's car must be a regexp matching
4899 the buffer name. This is for compatibility with
4900 `special-display-regexps'; the cdr of the cons cell is ignored.
4902 See also `same-window-buffer-names'."
4903 :type '(repeat (regexp :format "%v"))
4904 :group 'windows)
4906 (defun same-window-p (buffer-name)
4907 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
4908 This function returns non-nil if `display-buffer' or
4909 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
4910 selected rather than (as usual) some other window. See
4911 `same-window-buffer-names' and `same-window-regexps'."
4912 (cond
4913 ((not (stringp buffer-name)))
4914 ;; The elements of `same-window-buffer-names' can be buffer
4915 ;; names or cons cells whose cars are buffer names.
4916 ((member buffer-name same-window-buffer-names))
4917 ((assoc buffer-name same-window-buffer-names))
4918 ((catch 'found
4919 (dolist (regexp same-window-regexps)
4920 ;; The elements of `same-window-regexps' can be regexps
4921 ;; or cons cells whose cars are regexps.
4922 (when (or (and (stringp regexp)
4923 (string-match-p regexp buffer-name))
4924 (and (consp regexp) (stringp (car regexp))
4925 (string-match-p (car regexp) buffer-name)))
4926 (throw 'found t)))))))
4928 (defcustom pop-up-frames nil
4929 "Whether `display-buffer' should make a separate frame.
4930 If nil, never make a separate frame.
4931 If the value is `graphic-only', make a separate frame
4932 on graphic displays only.
4933 Any other non-nil value means always make a separate frame."
4934 :type '(choice
4935 (const :tag "Never" nil)
4936 (const :tag "On graphic displays only" graphic-only)
4937 (const :tag "Always" t))
4938 :group 'windows)
4940 (defcustom display-buffer-reuse-frames nil
4941 "Non-nil means `display-buffer' should reuse frames.
4942 If the buffer in question is already displayed in a frame, raise
4943 that frame."
4944 :type 'boolean
4945 :version "21.1"
4946 :group 'windows)
4948 (make-obsolete-variable
4949 'display-buffer-reuse-frames
4950 "use a `reusable-frames' alist entry in `display-buffer-alist'."
4951 "24.3")
4953 (defcustom pop-up-windows t
4954 "Non-nil means `display-buffer' should make a new window."
4955 :type 'boolean
4956 :group 'windows)
4958 (defcustom split-window-preferred-function 'split-window-sensibly
4959 "Function called by `display-buffer' routines to split a window.
4960 This function is called with a window as single argument and is
4961 supposed to split that window and return the new window. If the
4962 window can (or shall) not be split, it is supposed to return nil.
4963 The default is to call the function `split-window-sensibly' which
4964 tries to split the window in a way which seems most suitable.
4965 You can customize the options `split-height-threshold' and/or
4966 `split-width-threshold' in order to have `split-window-sensibly'
4967 prefer either vertical or horizontal splitting.
4969 If you set this to any other function, bear in mind that the
4970 `display-buffer' routines may call this function two times. The
4971 argument of the first call is the largest window on its frame.
4972 If that call fails to return a live window, the function is
4973 called again with the least recently used window as argument. If
4974 that call fails too, `display-buffer' will use an existing window
4975 to display its buffer.
4977 The window selected at the time `display-buffer' was invoked is
4978 still selected when this function is called. Hence you can
4979 compare the window argument with the value of `selected-window'
4980 if you intend to split the selected window instead or if you do
4981 not want to split the selected window."
4982 :type 'function
4983 :version "23.1"
4984 :group 'windows)
4986 (defcustom split-height-threshold 80
4987 "Minimum height for splitting windows sensibly.
4988 If this is an integer, `split-window-sensibly' may split a window
4989 vertically only if it has at least this many lines. If this is
4990 nil, `split-window-sensibly' is not allowed to split a window
4991 vertically. If, however, a window is the only window on its
4992 frame, `split-window-sensibly' may split it vertically
4993 disregarding the value of this variable."
4994 :type '(choice (const nil) (integer :tag "lines"))
4995 :version "23.1"
4996 :group 'windows)
4998 (defcustom split-width-threshold 160
4999 "Minimum width for splitting windows sensibly.
5000 If this is an integer, `split-window-sensibly' may split a window
5001 horizontally only if it has at least this many columns. If this
5002 is nil, `split-window-sensibly' is not allowed to split a window
5003 horizontally."
5004 :type '(choice (const nil) (integer :tag "columns"))
5005 :version "23.1"
5006 :group 'windows)
5008 (defun window-splittable-p (window &optional horizontal)
5009 "Return non-nil if `split-window-sensibly' may split WINDOW.
5010 Optional argument HORIZONTAL nil or omitted means check whether
5011 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
5012 non-nil means check whether WINDOW may be split horizontally.
5014 WINDOW may be split vertically when the following conditions
5015 hold:
5016 - `window-size-fixed' is either nil or equals `width' for the
5017 buffer of WINDOW.
5018 - `split-height-threshold' is an integer and WINDOW is at least as
5019 high as `split-height-threshold'.
5020 - When WINDOW is split evenly, the emanating windows are at least
5021 `window-min-height' lines tall and can accommodate at least one
5022 line plus - if WINDOW has one - a mode line.
5024 WINDOW may be split horizontally when the following conditions
5025 hold:
5026 - `window-size-fixed' is either nil or equals `height' for the
5027 buffer of WINDOW.
5028 - `split-width-threshold' is an integer and WINDOW is at least as
5029 wide as `split-width-threshold'.
5030 - When WINDOW is split evenly, the emanating windows are at least
5031 `window-min-width' or two (whichever is larger) columns wide."
5032 (when (window-live-p window)
5033 (with-current-buffer (window-buffer window)
5034 (if horizontal
5035 ;; A window can be split horizontally when its width is not
5036 ;; fixed, it is at least `split-width-threshold' columns wide
5037 ;; and at least twice as wide as `window-min-width' and 2 (the
5038 ;; latter value is hardcoded).
5039 (and (memq window-size-fixed '(nil height))
5040 ;; Testing `window-full-width-p' here hardly makes any
5041 ;; sense nowadays. This can be done more intuitively by
5042 ;; setting up `split-width-threshold' appropriately.
5043 (numberp split-width-threshold)
5044 (>= (window-width window)
5045 (max split-width-threshold
5046 (* 2 (max window-min-width 2)))))
5047 ;; A window can be split vertically when its height is not
5048 ;; fixed, it is at least `split-height-threshold' lines high,
5049 ;; and it is at least twice as high as `window-min-height' and 2
5050 ;; if it has a mode line or 1.
5051 (and (memq window-size-fixed '(nil width))
5052 (numberp split-height-threshold)
5053 (>= (window-height window)
5054 (max split-height-threshold
5055 (* 2 (max window-min-height
5056 (if mode-line-format 2 1))))))))))
5058 (defun split-window-sensibly (&optional window)
5059 "Split WINDOW in a way suitable for `display-buffer'.
5060 WINDOW defaults to the currently selected window.
5061 If `split-height-threshold' specifies an integer, WINDOW is at
5062 least `split-height-threshold' lines tall and can be split
5063 vertically, split WINDOW into two windows one above the other and
5064 return the lower window. Otherwise, if `split-width-threshold'
5065 specifies an integer, WINDOW is at least `split-width-threshold'
5066 columns wide and can be split horizontally, split WINDOW into two
5067 windows side by side and return the window on the right. If this
5068 can't be done either and WINDOW is the only window on its frame,
5069 try to split WINDOW vertically disregarding any value specified
5070 by `split-height-threshold'. If that succeeds, return the lower
5071 window. Return nil otherwise.
5073 By default `display-buffer' routines call this function to split
5074 the largest or least recently used window. To change the default
5075 customize the option `split-window-preferred-function'.
5077 You can enforce this function to not split WINDOW horizontally,
5078 by setting (or binding) the variable `split-width-threshold' to
5079 nil. If, in addition, you set `split-height-threshold' to zero,
5080 chances increase that this function does split WINDOW vertically.
5082 In order to not split WINDOW vertically, set (or bind) the
5083 variable `split-height-threshold' to nil. Additionally, you can
5084 set `split-width-threshold' to zero to make a horizontal split
5085 more likely to occur.
5087 Have a look at the function `window-splittable-p' if you want to
5088 know how `split-window-sensibly' determines whether WINDOW can be
5089 split."
5090 (let ((window (or window (selected-window))))
5091 (or (and (window-splittable-p window)
5092 ;; Split window vertically.
5093 (with-selected-window window
5094 (split-window-below)))
5095 (and (window-splittable-p window t)
5096 ;; Split window horizontally.
5097 (with-selected-window window
5098 (split-window-right)))
5099 (and (eq window (frame-root-window (window-frame window)))
5100 (not (window-minibuffer-p window))
5101 ;; If WINDOW is the only window on its frame and is not the
5102 ;; minibuffer window, try to split it vertically disregarding
5103 ;; the value of `split-height-threshold'.
5104 (let ((split-height-threshold 0))
5105 (when (window-splittable-p window)
5106 (with-selected-window window
5107 (split-window-below))))))))
5109 (defun window--try-to-split-window (window &optional alist)
5110 "Try to split WINDOW.
5111 Return value returned by `split-window-preferred-function' if it
5112 represents a live window, nil otherwise."
5113 (and (window-live-p window)
5114 (not (frame-parameter (window-frame window) 'unsplittable))
5115 (let* ((window-combination-limit
5116 ;; When `window-combination-limit' equals
5117 ;; `display-buffer' or equals `resize-window' and a
5118 ;; `window-height' or `window-width' alist entry are
5119 ;; present, bind it to t so resizing steals space
5120 ;; preferably from the window that was split.
5121 (if (or (eq window-combination-limit 'display-buffer)
5122 (and (eq window-combination-limit 'window-size)
5123 (or (cdr (assq 'window-height alist))
5124 (cdr (assq 'window-width alist)))))
5126 window-combination-limit))
5127 (new-window
5128 ;; Since `split-window-preferred-function' might
5129 ;; throw an error use `condition-case'.
5130 (condition-case nil
5131 (funcall split-window-preferred-function window)
5132 (error nil))))
5133 (and (window-live-p new-window) new-window))))
5135 (defun window--frame-usable-p (frame)
5136 "Return FRAME if it can be used to display a buffer."
5137 (when (frame-live-p frame)
5138 (let ((window (frame-root-window frame)))
5139 ;; `frame-root-window' may be an internal window which is considered
5140 ;; "dead" by `window-live-p'. Hence if `window' is not live we
5141 ;; implicitly know that `frame' has a visible window we can use.
5142 (unless (and (window-live-p window)
5143 (or (window-minibuffer-p window)
5144 ;; If the window is soft-dedicated, the frame is usable.
5145 ;; Actually, even if the window is really dedicated,
5146 ;; the frame is still usable by splitting it.
5147 ;; At least Emacs-22 allowed it, and it is desirable
5148 ;; when displaying same-frame windows.
5149 nil ; (eq t (window-dedicated-p window))
5151 frame))))
5153 (defcustom even-window-heights t
5154 "If non-nil `display-buffer' will try to even window heights.
5155 Otherwise `display-buffer' will leave the window configuration
5156 alone. Heights are evened only when `display-buffer' chooses a
5157 window that appears above or below the selected window."
5158 :type 'boolean
5159 :group 'windows)
5161 (defun window--even-window-heights (window)
5162 "Even heights of WINDOW and selected window.
5163 Do this only if these windows are vertically adjacent to each
5164 other, `even-window-heights' is non-nil, and the selected window
5165 is higher than WINDOW."
5166 (when (and even-window-heights
5167 ;; Even iff WINDOW forms a vertical combination with the
5168 ;; selected window, and WINDOW's height exceeds that of the
5169 ;; selected window, see also bug#11880.
5170 (window-combined-p window)
5171 (= (window-child-count (window-parent window)) 2)
5172 (eq (window-parent) (window-parent window))
5173 (> (window-total-height) (window-total-height window)))
5174 ;; Don't throw an error if we can't even window heights for
5175 ;; whatever reason.
5176 (condition-case nil
5177 (enlarge-window
5178 (/ (- (window-total-height window) (window-total-height)) 2))
5179 (error nil))))
5181 (defun window--display-buffer (buffer window type &optional alist dedicated)
5182 "Display BUFFER in WINDOW.
5183 TYPE must be one of the symbols `reuse', `window' or `frame' and
5184 is passed unaltered to `display-buffer-record-window'. ALIST is
5185 the alist argument of `display-buffer'. Set `window-dedicated-p'
5186 to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
5187 live."
5188 (when (and (buffer-live-p buffer) (window-live-p window))
5189 (display-buffer-record-window type window buffer)
5190 (unless (eq buffer (window-buffer window))
5191 (set-window-dedicated-p window nil)
5192 (set-window-buffer window buffer)
5193 (when dedicated
5194 (set-window-dedicated-p window dedicated))
5195 (when (memq type '(window frame))
5196 (set-window-prev-buffers window nil)))
5197 (let ((parameter (window-parameter window 'quit-restore))
5198 (height (cdr (assq 'window-height alist)))
5199 (width (cdr (assq 'window-width alist))))
5200 (when (or (eq type 'window)
5201 (and (eq (car parameter) 'same)
5202 (eq (nth 1 parameter) 'window)))
5203 ;; Adjust height of window if asked for.
5204 (cond
5205 ((not height))
5206 ((numberp height)
5207 (let* ((new-height
5208 (if (integerp height)
5209 height
5210 (round
5211 (* (window-total-size (frame-root-window window))
5212 height))))
5213 (delta (- new-height (window-total-size window))))
5214 (when (and (window--resizable-p window delta nil 'safe)
5215 (window-combined-p window))
5216 (window-resize window delta nil 'safe))))
5217 ((functionp height)
5218 (ignore-errors (funcall height window))))
5219 ;; Adjust width of window if asked for.
5220 (cond
5221 ((not width))
5222 ((numberp width)
5223 (let* ((new-width
5224 (if (integerp width)
5225 width
5226 (round
5227 (* (window-total-size (frame-root-window window) t)
5228 width))))
5229 (delta (- new-width (window-total-size window t))))
5230 (when (and (window--resizable-p window delta t 'safe)
5231 (window-combined-p window t))
5232 (window-resize window delta t 'safe))))
5233 ((functionp width)
5234 (ignore-errors (funcall width window))))))
5236 window))
5238 (defun window--maybe-raise-frame (frame)
5239 (let ((visible (frame-visible-p frame)))
5240 (unless (or (not visible)
5241 ;; Assume the selected frame is already visible enough.
5242 (eq frame (selected-frame))
5243 ;; Assume the frame from which we invoked the
5244 ;; minibuffer is visible.
5245 (and (minibuffer-window-active-p (selected-window))
5246 (eq frame (window-frame (minibuffer-selected-window)))))
5247 (raise-frame frame))))
5249 ;; FIXME: Not implemented.
5250 ;; FIXME: By the way, there could be more levels of dedication:
5251 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
5252 ;; the window hasn't been used for something else yet.
5253 ;; - `softly' dedicated only allows reuse when asked explicitly.
5254 ;; - `strongly' never allows reuse.
5255 (defvar display-buffer-mark-dedicated nil
5256 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
5257 The actual non-nil value of this variable will be copied to the
5258 `window-dedicated-p' flag.")
5260 (defconst display-buffer--action-function-custom-type
5261 '(choice :tag "Function"
5262 (const :tag "--" ignore) ; default for insertion
5263 (const display-buffer-reuse-window)
5264 (const display-buffer-pop-up-window)
5265 (const display-buffer-same-window)
5266 (const display-buffer-pop-up-frame)
5267 (const display-buffer-use-some-window)
5268 (function :tag "Other function"))
5269 "Custom type for `display-buffer' action functions.")
5271 (defconst display-buffer--action-custom-type
5272 `(cons :tag "Action"
5273 (choice :tag "Action functions"
5274 ,display-buffer--action-function-custom-type
5275 (repeat
5276 :tag "List of functions"
5277 ,display-buffer--action-function-custom-type))
5278 (alist :tag "Action arguments"
5279 :key-type symbol
5280 :value-type (sexp :tag "Value")))
5281 "Custom type for `display-buffer' actions.")
5283 (defvar display-buffer-overriding-action '(nil . nil)
5284 "Overriding action to perform to display a buffer.
5285 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5286 function or a list of functions. Each function should accept two
5287 arguments: a buffer to display and an alist similar to ALIST.
5288 See `display-buffer' for details.")
5289 (put 'display-buffer-overriding-action 'risky-local-variable t)
5291 (defcustom display-buffer-alist nil
5292 "Alist of conditional actions for `display-buffer'.
5293 This is a list of elements (CONDITION . ACTION), where:
5295 CONDITION is either a regexp matching buffer names, or a
5296 function that takes two arguments - a buffer name and the
5297 ACTION argument of `display-buffer' - and returns a boolean.
5299 ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
5300 function or a list of functions. Each such function should
5301 accept two arguments: a buffer to display and an alist of the
5302 same form as ALIST. See `display-buffer' for details.
5304 `display-buffer' scans this alist until it either finds a
5305 matching regular expression or the function specified by a
5306 condition returns non-nil. In any of these cases, it adds the
5307 associated action to the list of actions it will try."
5308 :type `(alist :key-type
5309 (choice :tag "Condition"
5310 regexp
5311 (function :tag "Matcher function"))
5312 :value-type ,display-buffer--action-custom-type)
5313 :risky t
5314 :version "24.1"
5315 :group 'windows)
5317 (defcustom display-buffer-base-action '(nil . nil)
5318 "User-specified default action for `display-buffer'.
5319 It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
5320 function or a list of functions. Each function should accept two
5321 arguments: a buffer to display and an alist similar to ALIST.
5322 See `display-buffer' for details."
5323 :type display-buffer--action-custom-type
5324 :risky t
5325 :version "24.1"
5326 :group 'windows)
5328 (defconst display-buffer-fallback-action
5329 '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
5330 display-buffer-reuse-window
5331 display-buffer--maybe-pop-up-frame-or-window
5332 display-buffer-use-some-window
5333 ;; If all else fails, pop up a new frame.
5334 display-buffer-pop-up-frame))
5335 "Default fallback action for `display-buffer'.
5336 This is the action used by `display-buffer' if no other actions
5337 specified, e.g. by the user options `display-buffer-alist' or
5338 `display-buffer-base-action'. See `display-buffer'.")
5339 (put 'display-buffer-fallback-action 'risky-local-variable t)
5341 (defun display-buffer-assq-regexp (buffer-name alist action)
5342 "Retrieve ALIST entry corresponding to BUFFER-NAME.
5343 ACTION is the action argument passed to `display-buffer'."
5344 (catch 'match
5345 (dolist (entry alist)
5346 (let ((key (car entry)))
5347 (when (or (and (stringp key)
5348 (string-match-p key buffer-name))
5349 (and (functionp key)
5350 (funcall key buffer-name action)))
5351 (throw 'match (cdr entry)))))))
5353 (defvar display-buffer--same-window-action
5354 '(display-buffer-same-window
5355 (inhibit-same-window . nil))
5356 "A `display-buffer' action for displaying in the same window.")
5357 (put 'display-buffer--same-window-action 'risky-local-variable t)
5359 (defvar display-buffer--other-frame-action
5360 '((display-buffer-reuse-window
5361 display-buffer-pop-up-frame)
5362 (reusable-frames . 0)
5363 (inhibit-same-window . t))
5364 "A `display-buffer' action for displaying in another frame.")
5365 (put 'display-buffer--other-frame-action 'risky-local-variable t)
5367 (defun display-buffer (buffer-or-name &optional action frame)
5368 "Display BUFFER-OR-NAME in some window, without selecting it.
5369 BUFFER-OR-NAME must be a buffer or the name of an existing
5370 buffer. Return the window chosen for displaying BUFFER-OR-NAME,
5371 or nil if no such window is found.
5373 Optional argument ACTION, if non-nil, should specify a display
5374 action. Its form is described below.
5376 Optional argument FRAME, if non-nil, acts like an additional
5377 ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
5378 specifying the frame(s) to search for a window that is already
5379 displaying the buffer. See `display-buffer-reuse-window'
5381 If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
5382 where FUNCTION is either a function or a list of functions, and
5383 ALIST is an arbitrary association list (alist).
5385 Each such FUNCTION should accept two arguments: the buffer to
5386 display and an alist. Based on those arguments, it should either
5387 display the buffer and return the window, or return nil if unable
5388 to display the buffer.
5390 The `display-buffer' function builds a function list and an alist
5391 by combining the functions and alists specified in
5392 `display-buffer-overriding-action', `display-buffer-alist', the
5393 ACTION argument, `display-buffer-base-action', and
5394 `display-buffer-fallback-action' (in order). Then it calls each
5395 function in the combined function list in turn, passing the
5396 buffer as the first argument and the combined alist as the second
5397 argument, until one of the functions returns non-nil.
5399 If ACTION is nil, the function list and the alist are built using
5400 only the other variables mentioned above.
5402 Available action functions include:
5403 `display-buffer-same-window'
5404 `display-buffer-reuse-window'
5405 `display-buffer-pop-up-frame'
5406 `display-buffer-pop-up-window'
5407 `display-buffer-use-some-window'
5409 Recognized alist entries include:
5411 `inhibit-same-window' -- A non-nil value prevents the same
5412 window from being used for display.
5414 `inhibit-switch-frame' -- A non-nil value prevents any other
5415 frame from being raised or selected,
5416 even if the window is displayed there.
5418 `reusable-frames' -- Value specifies frame(s) to search for a
5419 window that already displays the buffer.
5420 See `display-buffer-reuse-window'.
5422 `pop-up-frame-parameters' -- Value specifies an alist of frame
5423 parameters to give a new frame, if
5424 one is created.
5426 `window-height' -- Value specifies either an integer (the number
5427 of lines of a new window), a floating point number (the
5428 fraction of a new window with respect to the height of the
5429 frame's root window) or a function to be called with one
5430 argument - a new window. The function is supposed to adjust
5431 the height of the window; its return value is ignored.
5432 Suitable functions are `shrink-window-if-larger-than-buffer'
5433 and `fit-window-to-buffer'.
5435 `window-width' -- Value specifies either an integer (the number
5436 of columns of a new window), a floating point number (the
5437 fraction of a new window with respect to the width of the
5438 frame's root window) or a function to be called with one
5439 argument - a new window. The function is supposed to adjust
5440 the width of the window; its return value is ignored.
5442 The ACTION argument to `display-buffer' can also have a non-nil
5443 and non-list value. This means to display the buffer in a window
5444 other than the selected one, even if it is already displayed in
5445 the selected window. If called interactively with a prefix
5446 argument, ACTION is t."
5447 (interactive (list (read-buffer "Display buffer: " (other-buffer))
5448 (if current-prefix-arg t)))
5449 (let ((buffer (if (bufferp buffer-or-name)
5450 buffer-or-name
5451 (get-buffer buffer-or-name)))
5452 ;; Handle the old form of the first argument.
5453 (inhibit-same-window (and action (not (listp action)))))
5454 (unless (listp action) (setq action nil))
5455 (if display-buffer-function
5456 ;; If `display-buffer-function' is defined, let it do the job.
5457 (funcall display-buffer-function buffer inhibit-same-window)
5458 ;; Otherwise, use the defined actions.
5459 (let* ((user-action
5460 (display-buffer-assq-regexp
5461 (buffer-name buffer) display-buffer-alist action))
5462 (special-action (display-buffer--special-action buffer))
5463 ;; Extra actions from the arguments to this function:
5464 (extra-action
5465 (cons nil (append (if inhibit-same-window
5466 '((inhibit-same-window . t)))
5467 (if frame
5468 `((reusable-frames . ,frame))))))
5469 ;; Construct action function list and action alist.
5470 (actions (list display-buffer-overriding-action
5471 user-action special-action action extra-action
5472 display-buffer-base-action
5473 display-buffer-fallback-action))
5474 (functions (apply 'append
5475 (mapcar (lambda (x)
5476 (setq x (car x))
5477 (if (functionp x) (list x) x))
5478 actions)))
5479 (alist (apply 'append (mapcar 'cdr actions)))
5480 window)
5481 (unless (buffer-live-p buffer)
5482 (error "Invalid buffer"))
5483 (while (and functions (not window))
5484 (setq window (funcall (car functions) buffer alist)
5485 functions (cdr functions)))
5486 window))))
5488 (defun display-buffer-other-frame (buffer)
5489 "Display buffer BUFFER in another frame.
5490 This uses the function `display-buffer' as a subroutine; see
5491 its documentation for additional customization information."
5492 (interactive "BDisplay buffer in other frame: ")
5493 (display-buffer buffer display-buffer--other-frame-action t))
5495 ;;; `display-buffer' action functions:
5497 (defun display-buffer-same-window (buffer alist)
5498 "Display BUFFER in the selected window.
5499 This fails if ALIST has a non-nil `inhibit-same-window' entry, or
5500 if the selected window is a minibuffer window or is dedicated to
5501 another buffer; in that case, return nil. Otherwise, return the
5502 selected window."
5503 (unless (or (cdr (assq 'inhibit-same-window alist))
5504 (window-minibuffer-p)
5505 (window-dedicated-p))
5506 (window--display-buffer buffer (selected-window) 'reuse alist)))
5508 (defun display-buffer--maybe-same-window (buffer alist)
5509 "Conditionally display BUFFER in the selected window.
5510 If `same-window-p' returns non-nil for BUFFER's name, call
5511 `display-buffer-same-window' and return its value. Otherwise,
5512 return nil."
5513 (and (same-window-p (buffer-name buffer))
5514 (display-buffer-same-window buffer alist)))
5516 (defun display-buffer-reuse-window (buffer alist)
5517 "Return a window that is already displaying BUFFER.
5518 Return nil if no usable window is found.
5520 If ALIST has a non-nil `inhibit-same-window' entry, the selected
5521 window is not eligible for reuse.
5523 If ALIST contains a `reusable-frames' entry, its value determines
5524 which frames to search for a reusable window:
5525 nil -- the selected frame (actually the last non-minibuffer frame)
5526 A frame -- just that frame
5527 `visible' -- all visible frames
5528 0 -- all frames on the current terminal
5529 t -- all frames.
5531 If ALIST contains no `reusable-frames' entry, search just the
5532 selected frame if `display-buffer-reuse-frames' and
5533 `pop-up-frames' are both nil; search all frames on the current
5534 terminal if either of those variables is non-nil.
5536 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5537 event that a window on another frame is chosen, avoid raising
5538 that frame."
5539 (let* ((alist-entry (assq 'reusable-frames alist))
5540 (frames (cond (alist-entry (cdr alist-entry))
5541 ((if (eq pop-up-frames 'graphic-only)
5542 (display-graphic-p)
5543 pop-up-frames)
5545 (display-buffer-reuse-frames 0)
5546 (t (last-nonminibuffer-frame))))
5547 (window (if (and (eq buffer (window-buffer))
5548 (not (cdr (assq 'inhibit-same-window alist))))
5549 (selected-window)
5550 (car (delq (selected-window)
5551 (get-buffer-window-list buffer 'nomini
5552 frames))))))
5553 (when (window-live-p window)
5554 (prog1 (window--display-buffer buffer window 'reuse alist)
5555 (unless (cdr (assq 'inhibit-switch-frame alist))
5556 (window--maybe-raise-frame (window-frame window)))))))
5558 (defun display-buffer--special-action (buffer)
5559 "Return special display action for BUFFER, if any.
5560 If `special-display-p' returns non-nil for BUFFER, return an
5561 appropriate display action involving `special-display-function'.
5562 See `display-buffer' for the format of display actions."
5563 (and special-display-function
5564 ;; `special-display-p' returns either t or a list of frame
5565 ;; parameters to pass to `special-display-function'.
5566 (let ((pars (special-display-p (buffer-name buffer))))
5567 (when pars
5568 (list (list #'display-buffer-reuse-window
5569 `(lambda (buffer _alist)
5570 (funcall special-display-function
5571 buffer ',(if (listp pars) pars)))))))))
5573 (defun display-buffer-pop-up-frame (buffer alist)
5574 "Display BUFFER in a new frame.
5575 This works by calling `pop-up-frame-function'. If successful,
5576 return the window used; otherwise return nil.
5578 If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
5579 raising the new frame.
5581 If ALIST has a non-nil `pop-up-frame-parameters' entry, the
5582 corresponding value is an alist of frame parameters to give the
5583 new frame."
5584 (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
5585 (pop-up-frame-alist (append params pop-up-frame-alist))
5586 (fun pop-up-frame-function)
5587 frame window)
5588 (when (and fun
5589 (setq frame (funcall fun))
5590 (setq window (frame-selected-window frame)))
5591 (prog1 (window--display-buffer
5592 buffer window 'frame alist display-buffer-mark-dedicated)
5593 (unless (cdr (assq 'inhibit-switch-frame alist))
5594 (window--maybe-raise-frame frame))))))
5596 (defun display-buffer-pop-up-window (buffer alist)
5597 "Display BUFFER by popping up a new window.
5598 The new window is created on the selected frame, or in
5599 `last-nonminibuffer-frame' if no windows can be created there.
5600 If successful, return the new window; otherwise return nil.
5602 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5603 event that the new window is created on another frame, avoid
5604 raising the frame."
5605 (let ((frame (or (window--frame-usable-p (selected-frame))
5606 (window--frame-usable-p (last-nonminibuffer-frame))))
5607 window)
5608 (when (and (or (not (frame-parameter frame 'unsplittable))
5609 ;; If the selected frame cannot be split, look at
5610 ;; `last-nonminibuffer-frame'.
5611 (and (eq frame (selected-frame))
5612 (setq frame (last-nonminibuffer-frame))
5613 (window--frame-usable-p frame)
5614 (not (frame-parameter frame 'unsplittable))))
5615 ;; Attempt to split largest or least recently used window.
5616 (setq window (or (window--try-to-split-window
5617 (get-largest-window frame t) alist)
5618 (window--try-to-split-window
5619 (get-lru-window frame t) alist))))
5620 (prog1 (window--display-buffer
5621 buffer window 'window alist display-buffer-mark-dedicated)
5622 (unless (cdr (assq 'inhibit-switch-frame alist))
5623 (window--maybe-raise-frame (window-frame window)))))))
5625 (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
5626 "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
5628 If `pop-up-frames' is non-nil (and not `graphic-only' on a
5629 text-only terminal), try with `display-buffer-pop-up-frame'.
5631 If that cannot be done, and `pop-up-windows' is non-nil, try
5632 again with `display-buffer-pop-up-window'."
5633 (or (and (if (eq pop-up-frames 'graphic-only)
5634 (display-graphic-p)
5635 pop-up-frames)
5636 (display-buffer-pop-up-frame buffer alist))
5637 (and pop-up-windows
5638 (display-buffer-pop-up-window buffer alist))))
5640 (defun display-buffer-below-selected (buffer alist)
5641 "Try displaying BUFFER in a window below the selected window.
5642 This either splits the selected window or reuses the window below
5643 the selected one."
5644 (let (window)
5645 (or (and (not (frame-parameter nil 'unsplittable))
5646 (setq window (window--try-to-split-window (selected-window) alist))
5647 (window--display-buffer
5648 buffer window 'window alist display-buffer-mark-dedicated))
5649 (and (setq window (window-in-direction 'below))
5650 (not (window-dedicated-p window))
5651 (window--display-buffer
5652 buffer window 'reuse alist display-buffer-mark-dedicated)))))
5654 (defun display-buffer-at-bottom (buffer alist)
5655 "Try displaying BUFFER in a window at the bottom of the selected frame.
5656 This either splits the window at the bottom of the frame or the
5657 frame's root window, or reuses an existing window at the bottom
5658 of the selected frame."
5659 (let (bottom-window window)
5660 (walk-window-tree (lambda (window) (setq bottom-window window)))
5661 (or (and (not (frame-parameter nil 'unsplittable))
5662 (setq window (window--try-to-split-window bottom-window alist))
5663 (window--display-buffer
5664 buffer window 'window alist display-buffer-mark-dedicated))
5665 (and (not (frame-parameter nil 'unsplittable))
5666 (setq window
5667 (condition-case nil
5668 (split-window (frame-root-window))
5669 (error nil)))
5670 (window--display-buffer
5671 buffer window 'window alist display-buffer-mark-dedicated))
5672 (and (setq window bottom-window)
5673 (not (window-dedicated-p window))
5674 (window--display-buffer
5675 buffer window 'reuse alist display-buffer-mark-dedicated)))))
5677 (defun display-buffer-in-previous-window (buffer alist)
5678 "Display BUFFER in a window previously showing it.
5679 If ALIST has a non-nil `inhibit-same-window' entry, the selected
5680 window is not eligible for reuse.
5682 If ALIST contains a `reusable-frames' entry, its value determines
5683 which frames to search for a reusable window:
5684 nil -- the selected frame (actually the last non-minibuffer frame)
5685 A frame -- just that frame
5686 `visible' -- all visible frames
5687 0 -- all frames on the current terminal
5688 t -- all frames.
5690 If ALIST contains no `reusable-frames' entry, search just the
5691 selected frame if `display-buffer-reuse-frames' and
5692 `pop-up-frames' are both nil; search all frames on the current
5693 terminal if either of those variables is non-nil.
5695 If ALIST has a `previous-window' entry, the window specified by
5696 that entry will override any other window found by the methods
5697 above, even if that window never showed BUFFER before."
5698 (let* ((alist-entry (assq 'reusable-frames alist))
5699 (inhibit-same-window
5700 (cdr (assq 'inhibit-same-window alist)))
5701 (frames (cond
5702 (alist-entry (cdr alist-entry))
5703 ((if (eq pop-up-frames 'graphic-only)
5704 (display-graphic-p)
5705 pop-up-frames)
5707 (display-buffer-reuse-frames 0)
5708 (t (last-nonminibuffer-frame))))
5709 entry best-window second-best-window window)
5710 ;; Scan windows whether they have shown the buffer recently.
5711 (catch 'best
5712 (dolist (window (window-list-1 (frame-first-window) 'nomini frames))
5713 (when (and (assq buffer (window-prev-buffers window))
5714 (not (window-dedicated-p window)))
5715 (if (eq window (selected-window))
5716 (unless inhibit-same-window
5717 (setq second-best-window window))
5718 (setq best-window window)
5719 (throw 'best t)))))
5720 ;; When ALIST has a `previous-window' entry, that entry may override
5721 ;; anything we found so far.
5722 (when (and (setq window (cdr (assq 'previous-window alist)))
5723 (window-live-p window)
5724 (not (window-dedicated-p window)))
5725 (if (eq window (selected-window))
5726 (unless inhibit-same-window
5727 (setq second-best-window window))
5728 (setq best-window window)))
5729 ;; Return best or second best window found.
5730 (when (setq window (or best-window second-best-window))
5731 (window--display-buffer buffer window 'reuse alist))))
5733 (defun display-buffer-use-some-window (buffer alist)
5734 "Display BUFFER in an existing window.
5735 Search for a usable window, set that window to the buffer, and
5736 return the window. If no suitable window is found, return nil.
5738 If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
5739 event that a window in another frame is chosen, avoid raising
5740 that frame."
5741 (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
5742 (frame (or (window--frame-usable-p (selected-frame))
5743 (window--frame-usable-p (last-nonminibuffer-frame))))
5744 (window
5745 ;; Reuse an existing window.
5746 (or (get-lru-window frame nil not-this-window)
5747 (let ((window (get-buffer-window buffer 'visible)))
5748 (unless (and not-this-window
5749 (eq window (selected-window)))
5750 window))
5751 (get-largest-window 'visible nil not-this-window)
5752 (let ((window (get-buffer-window buffer 0)))
5753 (unless (and not-this-window
5754 (eq window (selected-window)))
5755 window))
5756 (get-largest-window 0 not-this-window))))
5757 (when (window-live-p window)
5758 (prog1
5759 (window--display-buffer buffer window 'reuse alist)
5760 (window--even-window-heights window)
5761 (unless (cdr (assq 'inhibit-switch-frame alist))
5762 (window--maybe-raise-frame (window-frame window)))))))
5764 ;;; Display + selection commands:
5765 (defun pop-to-buffer (buffer &optional action norecord)
5766 "Select buffer BUFFER in some window, preferably a different one.
5767 BUFFER may be a buffer, a string (a buffer name), or nil. If it
5768 is a string not naming an existent buffer, create a buffer with
5769 that name. If BUFFER is nil, choose some other buffer. Return
5770 the buffer.
5772 This uses `display-buffer' as a subroutine. The optional ACTION
5773 argument is passed to `display-buffer' as its ACTION argument.
5774 See `display-buffer' for more information. ACTION is t if called
5775 interactively with a prefix argument, which means to pop to a
5776 window other than the selected one even if the buffer is already
5777 displayed in the selected window.
5779 If the window to show BUFFER is not on the selected
5780 frame, raise that window's frame and give it input focus.
5782 Optional third arg NORECORD non-nil means do not put this buffer
5783 at the front of the list of recently selected ones."
5784 (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
5785 (if current-prefix-arg t)))
5786 (setq buffer (window-normalize-buffer-to-switch-to buffer))
5787 (set-buffer buffer)
5788 (let* ((old-frame (selected-frame))
5789 (window (display-buffer buffer action))
5790 (frame (window-frame window)))
5791 ;; If we chose another frame, make sure it gets input focus.
5792 (unless (eq frame old-frame)
5793 (select-frame-set-input-focus frame norecord))
5794 ;; Make sure new window is selected (Bug#8615), (Bug#6954).
5795 (select-window window norecord)
5796 buffer))
5798 (defun pop-to-buffer-same-window (buffer &optional norecord)
5799 "Select buffer BUFFER in some window, preferably the same one.
5800 This function behaves much like `switch-to-buffer', except it
5801 displays with `special-display-function' if BUFFER has a match in
5802 `special-display-buffer-names' or `special-display-regexps'.
5804 Unlike `pop-to-buffer', this function prefers using the selected
5805 window over popping up a new window or frame.
5807 BUFFER may be a buffer, a string (a buffer name), or nil. If it
5808 is a string not naming an existent buffer, create a buffer with
5809 that name. If BUFFER is nil, choose some other buffer. Return
5810 the buffer.
5812 NORECORD, if non-nil means do not put this buffer at the front of
5813 the list of recently selected ones."
5814 (pop-to-buffer buffer display-buffer--same-window-action norecord))
5816 (defun read-buffer-to-switch (prompt)
5817 "Read the name of a buffer to switch to, prompting with PROMPT.
5818 Return the name of the buffer as a string.
5820 This function is intended for the `switch-to-buffer' family of
5821 commands since these need to omit the name of the current buffer
5822 from the list of completions and default values."
5823 (let ((rbts-completion-table (internal-complete-buffer-except)))
5824 (minibuffer-with-setup-hook
5825 (lambda ()
5826 (setq minibuffer-completion-table rbts-completion-table)
5827 ;; Since rbts-completion-table is built dynamically, we
5828 ;; can't just add it to the default value of
5829 ;; icomplete-with-completion-tables, so we add it
5830 ;; here manually.
5831 (if (and (boundp 'icomplete-with-completion-tables)
5832 (listp icomplete-with-completion-tables))
5833 (set (make-local-variable 'icomplete-with-completion-tables)
5834 (cons rbts-completion-table
5835 icomplete-with-completion-tables))))
5836 (read-buffer prompt (other-buffer (current-buffer))
5837 (confirm-nonexistent-file-or-buffer)))))
5839 (defun window-normalize-buffer-to-switch-to (buffer-or-name)
5840 "Normalize BUFFER-OR-NAME argument of buffer switching functions.
5841 If BUFFER-OR-NAME is nil, return the buffer returned by
5842 `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
5843 exists, return that buffer. If no such buffer exists, create a
5844 buffer with the name BUFFER-OR-NAME and return that buffer."
5845 (if buffer-or-name
5846 (or (get-buffer buffer-or-name)
5847 (let ((buffer (get-buffer-create buffer-or-name)))
5848 (set-buffer-major-mode buffer)
5849 buffer))
5850 (other-buffer)))
5852 (defcustom switch-to-buffer-preserve-window-point nil
5853 "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
5854 If this is nil, `switch-to-buffer' displays the buffer at that
5855 buffer's `point'. If this is `already-displayed', it tries to
5856 display the buffer at its previous position in the selected
5857 window, provided the buffer is currently displayed in some other
5858 window on any visible or iconified frame. If this is t, it
5859 unconditionally tries to display the buffer at its previous
5860 position in the selected window.
5862 This variable is ignored if the buffer is already displayed in
5863 the selected window or never appeared in it before, or if
5864 `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
5865 :type '(choice
5866 (const :tag "Never" nil)
5867 (const :tag "If already displayed elsewhere" already-displayed)
5868 (const :tag "Always" t))
5869 :group 'windows
5870 :version "24.3")
5872 (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
5873 "Display buffer BUFFER-OR-NAME in the selected window.
5875 WARNING: This is NOT the way to work on another buffer temporarily
5876 within a Lisp program! Use `set-buffer' instead. That avoids
5877 messing with the window-buffer correspondences.
5879 If the selected window cannot display the specified
5880 buffer (e.g. if it is a minibuffer window or strongly dedicated
5881 to another buffer), call `pop-to-buffer' to select the buffer in
5882 another window.
5884 If called interactively, read the buffer name using the
5885 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5886 determines whether to request confirmation before creating a new
5887 buffer.
5889 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
5890 If BUFFER-OR-NAME is a string that does not identify an existing
5891 buffer, create a buffer with that name. If BUFFER-OR-NAME is
5892 nil, switch to the buffer returned by `other-buffer'.
5894 If optional argument NORECORD is non-nil, do not put the buffer
5895 at the front of the buffer list, and do not make the window
5896 displaying it the most recently selected one.
5898 If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
5899 must be displayed in the selected window; if that is impossible,
5900 signal an error rather than calling `pop-to-buffer'.
5902 The option `switch-to-buffer-preserve-window-point' can be used
5903 to make the buffer appear at its last position in the selected
5904 window.
5906 Return the buffer switched to."
5907 (interactive
5908 (list (read-buffer-to-switch "Switch to buffer: ") nil 'force-same-window))
5909 (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
5910 (cond
5911 ;; Don't call set-window-buffer if it's not needed since it
5912 ;; might signal an error (e.g. if the window is dedicated).
5913 ((eq buffer (window-buffer)))
5914 ((window-minibuffer-p)
5915 (if force-same-window
5916 (user-error "Cannot switch buffers in minibuffer window")
5917 (pop-to-buffer buffer norecord)))
5918 ((eq (window-dedicated-p) t)
5919 (if force-same-window
5920 (user-error "Cannot switch buffers in a dedicated window")
5921 (pop-to-buffer buffer norecord)))
5923 (let* ((entry (assq buffer (window-prev-buffers)))
5924 (displayed (and (eq switch-to-buffer-preserve-window-point
5925 'already-displayed)
5926 (get-buffer-window buffer 0))))
5927 (set-window-buffer nil buffer)
5928 (when (and entry
5929 (or (eq switch-to-buffer-preserve-window-point t)
5930 displayed))
5931 ;; Try to restore start and point of buffer in the selected
5932 ;; window (Bug#4041).
5933 (set-window-start (selected-window) (nth 1 entry) t)
5934 (set-window-point nil (nth 2 entry))))))
5936 (unless norecord
5937 (select-window (selected-window)))
5938 (set-buffer buffer)))
5940 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
5941 "Select the buffer specified by BUFFER-OR-NAME in another window.
5942 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5943 nil. Return the buffer switched to.
5945 If called interactively, prompt for the buffer name using the
5946 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5947 determines whether to request confirmation before creating a new
5948 buffer.
5950 If BUFFER-OR-NAME is a string and does not identify an existing
5951 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5952 nil, switch to the buffer returned by `other-buffer'.
5954 Optional second argument NORECORD non-nil means do not put this
5955 buffer at the front of the list of recently selected ones.
5957 This uses the function `display-buffer' as a subroutine; see its
5958 documentation for additional customization information."
5959 (interactive
5960 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
5961 (let ((pop-up-windows t))
5962 (pop-to-buffer buffer-or-name t norecord)))
5964 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
5965 "Switch to buffer BUFFER-OR-NAME in another frame.
5966 BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
5967 nil. Return the buffer switched to.
5969 If called interactively, prompt for the buffer name using the
5970 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
5971 determines whether to request confirmation before creating a new
5972 buffer.
5974 If BUFFER-OR-NAME is a string and does not identify an existing
5975 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
5976 nil, switch to the buffer returned by `other-buffer'.
5978 Optional second arg NORECORD non-nil means do not put this
5979 buffer at the front of the list of recently selected ones.
5981 This uses the function `display-buffer' as a subroutine; see its
5982 documentation for additional customization information."
5983 (interactive
5984 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
5985 (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
5987 (defun set-window-text-height (window height)
5988 "Set the height in lines of the text display area of WINDOW to HEIGHT.
5989 WINDOW must be a live window and defaults to the selected one.
5990 HEIGHT doesn't include the mode line or header line, if any, or
5991 any partial-height lines in the text display area.
5993 Note that the current implementation of this function cannot
5994 always set the height exactly, but attempts to be conservative,
5995 by allocating more lines than are actually needed in the case
5996 where some error may be present."
5997 (setq window (window-normalize-window window t))
5998 (let ((delta (- height (window-text-height window))))
5999 (unless (zerop delta)
6000 ;; Setting window-min-height to a value like 1 can lead to very
6001 ;; bizarre displays because it also allows Emacs to make *other*
6002 ;; windows one line tall, which means that there's no more space
6003 ;; for the mode line.
6004 (let ((window-min-height (min 2 height)))
6005 (window-resize window delta)))))
6007 (defun enlarge-window-horizontally (delta)
6008 "Make selected window DELTA columns wider.
6009 Interactively, if no argument is given, make selected window one
6010 column wider."
6011 (interactive "p")
6012 (enlarge-window delta t))
6014 (defun shrink-window-horizontally (delta)
6015 "Make selected window DELTA columns narrower.
6016 Interactively, if no argument is given, make selected window one
6017 column narrower."
6018 (interactive "p")
6019 (shrink-window delta t))
6021 (defun count-screen-lines (&optional beg end count-final-newline window)
6022 "Return the number of screen lines in the region.
6023 The number of screen lines may be different from the number of actual lines,
6024 due to line breaking, display table, etc.
6026 Optional arguments BEG and END default to `point-min' and `point-max'
6027 respectively.
6029 If region ends with a newline, ignore it unless optional third argument
6030 COUNT-FINAL-NEWLINE is non-nil.
6032 The optional fourth argument WINDOW specifies the window used for obtaining
6033 parameters such as width, horizontal scrolling, and so on. The default is
6034 to use the selected window's parameters.
6036 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
6037 regardless of which buffer is displayed in WINDOW. This makes possible to use
6038 `count-screen-lines' in any buffer, whether or not it is currently displayed
6039 in some window."
6040 (unless beg
6041 (setq beg (point-min)))
6042 (unless end
6043 (setq end (point-max)))
6044 (if (= beg end)
6046 (save-excursion
6047 (save-restriction
6048 (widen)
6049 (narrow-to-region (min beg end)
6050 (if (and (not count-final-newline)
6051 (= ?\n (char-before (max beg end))))
6052 (1- (max beg end))
6053 (max beg end)))
6054 (goto-char (point-min))
6055 (1+ (vertical-motion (buffer-size) window))))))
6057 (defun window-buffer-height (window)
6058 "Return the height (in screen lines) of the buffer that WINDOW is displaying.
6059 WINDOW must be a live window and defaults to the selected one."
6060 (setq window (window-normalize-window window t))
6061 (with-current-buffer (window-buffer window)
6062 (max 1
6063 (count-screen-lines (point-min) (point-max)
6064 ;; If buffer ends with a newline, ignore it when
6065 ;; counting height unless point is after it.
6066 (eobp)
6067 window))))
6069 ;;; Resizing buffers to fit their contents exactly.
6070 (defcustom fit-frame-to-buffer nil
6071 "Non-nil means `fit-window-to-buffer' can resize frames.
6072 A frame can be resized if and only if its root window is a live
6073 window. The height of the root window is subject to the values
6074 of `fit-frame-to-buffer-max-height' and `window-min-height'."
6075 :type 'boolean
6076 :version "24.3"
6077 :group 'help)
6079 (defcustom fit-frame-to-buffer-bottom-margin 4
6080 "Bottom margin for the command `fit-frame-to-buffer'.
6081 This is the number of lines that function leaves free at the bottom of
6082 the display, in order to not obscure any system task bar or panel.
6083 If you do not have one (or if it is vertical) you might want to
6084 reduce this. If it is thicker, you might want to increase this."
6085 ;; If you set this too small, fit-frame-to-buffer can shift the
6086 ;; frame up to avoid the panel.
6087 :type 'integer
6088 :version "24.3"
6089 :group 'windows)
6091 (defun fit-frame-to-buffer (&optional frame max-height min-height)
6092 "Adjust height of FRAME to display its buffer contents exactly.
6093 FRAME can be any live frame and defaults to the selected one.
6095 Optional argument MAX-HEIGHT specifies the maximum height of FRAME.
6096 It defaults to the height of the display below the current
6097 top line of FRAME, minus `fit-frame-to-buffer-bottom-margin'.
6098 Optional argument MIN-HEIGHT specifies the minimum height of FRAME.
6099 The default corresponds to `window-min-height'."
6100 (interactive)
6101 (setq frame (window-normalize-frame frame))
6102 (let* ((root (frame-root-window frame))
6103 (frame-min-height
6104 (+ (- (frame-height frame) (window-total-size root))
6105 window-min-height))
6106 (frame-top (frame-parameter frame 'top))
6107 (top (if (consp frame-top)
6108 (funcall (car frame-top) (cadr frame-top))
6109 frame-top))
6110 (frame-max-height
6111 (- (/ (- (x-display-pixel-height frame) top)
6112 (frame-char-height frame))
6113 fit-frame-to-buffer-bottom-margin))
6114 (compensate 0)
6115 delta)
6116 (when (and (window-live-p root) (not (window-size-fixed-p root)))
6117 (with-selected-window root
6118 (cond
6119 ((not max-height)
6120 (setq max-height frame-max-height))
6121 ((numberp max-height)
6122 (setq max-height (min max-height frame-max-height)))
6124 (error "%s is an invalid maximum height" max-height)))
6125 (cond
6126 ((not min-height)
6127 (setq min-height frame-min-height))
6128 ((numberp min-height)
6129 (setq min-height (min min-height frame-min-height)))
6131 (error "%s is an invalid minimum height" min-height)))
6132 ;; When tool-bar-mode is enabled and we have just created a new
6133 ;; frame, reserve lines for toolbar resizing. This is needed
6134 ;; because for reasons unknown to me Emacs (1) reserves one line
6135 ;; for the toolbar when making the initial frame and toolbars
6136 ;; are enabled, and (2) later adds the remaining lines needed.
6137 ;; Our code runs IN BETWEEN (1) and (2). YMMV when you're on a
6138 ;; system that behaves differently.
6139 (let ((quit-restore (window-parameter root 'quit-restore))
6140 (lines (tool-bar-lines-needed frame)))
6141 (when (and quit-restore (eq (car quit-restore) 'frame)
6142 (not (zerop lines)))
6143 (setq compensate (1- lines))))
6144 (message "%s" compensate)
6145 (setq delta
6146 ;; Always count a final newline - we don't do any
6147 ;; post-processing, so let's play safe.
6148 (+ (count-screen-lines nil nil t)
6149 (- (window-body-size))
6150 compensate)))
6151 ;; Move away from final newline.
6152 (when (and (eobp) (bolp) (not (bobp)))
6153 (set-window-point root (line-beginning-position 0)))
6154 (set-window-start root (point-min))
6155 (set-window-vscroll root 0)
6156 (condition-case nil
6157 (set-frame-height
6158 frame
6159 (min (max (+ (frame-height frame) delta)
6160 min-height)
6161 max-height))
6162 (error (setq delta nil))))
6163 delta))
6165 (defun fit-window-to-buffer (&optional window max-height min-height)
6166 "Adjust height of WINDOW to display its buffer's contents exactly.
6167 WINDOW must be a live window and defaults to the selected one.
6169 Optional argument MAX-HEIGHT specifies the maximum height of
6170 WINDOW and defaults to the height of WINDOW's frame. Optional
6171 argument MIN-HEIGHT specifies the minimum height of WINDOW and
6172 defaults to `window-min-height'. Both MAX-HEIGHT and MIN-HEIGHT
6173 are specified in lines and include the mode line and header line,
6174 if any.
6176 If WINDOW is a full height window, then if the option
6177 `fit-frame-to-buffer' is non-nil, this calls the function
6178 `fit-frame-to-buffer' to adjust the frame height.
6180 Return the number of lines by which WINDOW was enlarged or
6181 shrunk. If an error occurs during resizing, return nil but don't
6182 signal an error.
6184 Note that even if this function makes WINDOW large enough to show
6185 _all_ lines of its buffer you might not see the first lines when
6186 WINDOW was scrolled."
6187 (interactive)
6188 (setq window (window-normalize-window window t))
6189 (cond
6190 ((window-size-fixed-p window))
6191 ((window-full-height-p window)
6192 (when fit-frame-to-buffer
6193 (fit-frame-to-buffer (window-frame window))))
6195 (with-selected-window window
6196 (let* ((height (window-total-size))
6197 (min-height
6198 ;; Adjust MIN-HEIGHT.
6199 (if (numberp min-height)
6200 ;; Can't get smaller than `window-safe-min-height'.
6201 (max min-height window-safe-min-height)
6202 ;; Preserve header and mode line if present.
6203 (window-min-size nil nil t)))
6204 (max-height
6205 ;; Adjust MAX-HEIGHT.
6206 (if (numberp max-height)
6207 ;; Can't get larger than height of frame.
6208 (min max-height
6209 (window-total-size (frame-root-window window)))
6210 ;; Don't delete other windows.
6211 (+ height (window-max-delta nil nil window))))
6212 ;; Make `desired-height' the height necessary to show
6213 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
6214 ;; and MAX-HEIGHT.
6215 (desired-height
6216 (max
6217 (min
6218 (+ (count-screen-lines)
6219 ;; For non-minibuffers count the mode line, if any.
6220 (if (and (not (window-minibuffer-p window))
6221 mode-line-format)
6224 ;; Count the header line, if any.
6225 (if header-line-format 1 0))
6226 max-height)
6227 min-height))
6228 (desired-delta
6229 (- desired-height (window-total-size window)))
6230 (delta
6231 (if (> desired-delta 0)
6232 (min desired-delta
6233 (window-max-delta window nil window))
6234 (max desired-delta
6235 (- (window-min-delta window nil window))))))
6236 (condition-case nil
6237 (if (zerop delta)
6238 ;; Return zero if DELTA became zero in the process.
6240 ;; Don't try to redisplay with the cursor at the end on its
6241 ;; own line--that would force a scroll and spoil things.
6242 (when (and (eobp) (bolp) (not (bobp)))
6243 ;; It's silly to put `point' at the end of the previous
6244 ;; line and so maybe force horizontal scrolling.
6245 (set-window-point window (line-beginning-position 0)))
6246 ;; Call `window-resize' with OVERRIDE argument equal WINDOW.
6247 (window-resize window delta nil window)
6248 ;; Check if the last line is surely fully visible. If
6249 ;; not, enlarge the window.
6250 (let ((end (save-excursion
6251 (goto-char (point-max))
6252 (when (and (bolp) (not (bobp)))
6253 ;; Don't include final newline.
6254 (backward-char 1))
6255 (when truncate-lines
6256 ;; If line-wrapping is turned off, test the
6257 ;; beginning of the last line for
6258 ;; visibility instead of the end, as the
6259 ;; end of the line could be invisible by
6260 ;; virtue of extending past the edge of the
6261 ;; window.
6262 (forward-line 0))
6263 (point))))
6264 (set-window-vscroll window 0)
6265 ;; This loop might in some rare pathological cases raise
6266 ;; an error - another reason for the `condition-case'.
6267 (while (and (< desired-height max-height)
6268 (= desired-height (window-total-size))
6269 (not (pos-visible-in-window-p end)))
6270 (window-resize window 1 nil window)
6271 (setq desired-height (1+ desired-height)))))
6272 (error (setq delta nil)))
6273 delta)))))
6275 (defun window-safely-shrinkable-p (&optional window)
6276 "Return t if WINDOW can be shrunk without shrinking other windows.
6277 WINDOW defaults to the selected window."
6278 (with-selected-window (or window (selected-window))
6279 (let ((edges (window-edges)))
6280 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
6281 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
6283 (defun shrink-window-if-larger-than-buffer (&optional window)
6284 "Shrink height of WINDOW if its buffer doesn't need so many lines.
6285 More precisely, shrink WINDOW vertically to be as small as
6286 possible, while still showing the full contents of its buffer.
6287 WINDOW must be a live window and defaults to the selected one.
6289 Do not shrink WINDOW to less than `window-min-height' lines. Do
6290 nothing if the buffer contains more lines than the present window
6291 height, or if some of the window's contents are scrolled out of
6292 view, or if shrinking this window would also shrink another
6293 window, or if the window is the only window of its frame.
6295 Return non-nil if the window was shrunk, nil otherwise."
6296 (interactive)
6297 (setq window (window-normalize-window window t))
6298 ;; Make sure that WINDOW is vertically combined and `point-min' is
6299 ;; visible (for whatever reason that's needed). The remaining issues
6300 ;; should be taken care of by `fit-window-to-buffer'.
6301 (when (and (window-combined-p window)
6302 (pos-visible-in-window-p (point-min) window))
6303 (fit-window-to-buffer window (window-total-size window))))
6305 (defun kill-buffer-and-window ()
6306 "Kill the current buffer and delete the selected window."
6307 (interactive)
6308 (let ((window-to-delete (selected-window))
6309 (buffer-to-kill (current-buffer))
6310 (delete-window-hook (lambda () (ignore-errors (delete-window)))))
6311 (unwind-protect
6312 (progn
6313 (add-hook 'kill-buffer-hook delete-window-hook t t)
6314 (if (kill-buffer (current-buffer))
6315 ;; If `delete-window' failed before, we rerun it to regenerate
6316 ;; the error so it can be seen in the echo area.
6317 (when (eq (selected-window) window-to-delete)
6318 (delete-window))))
6319 ;; If the buffer is not dead for some reason (probably because
6320 ;; of a `quit' signal), remove the hook again.
6321 (ignore-errors
6322 (with-current-buffer buffer-to-kill
6323 (remove-hook 'kill-buffer-hook delete-window-hook t))))))
6326 (defvar recenter-last-op nil
6327 "Indicates the last recenter operation performed.
6328 Possible values: `top', `middle', `bottom', integer or float numbers.")
6330 (defcustom recenter-positions '(middle top bottom)
6331 "Cycling order for `recenter-top-bottom'.
6332 A list of elements with possible values `top', `middle', `bottom',
6333 integer or float numbers that define the cycling order for
6334 the command `recenter-top-bottom'.
6336 Top and bottom destinations are `scroll-margin' lines from the true
6337 window top and bottom. Middle redraws the frame and centers point
6338 vertically within the window. Integer number moves current line to
6339 the specified absolute window-line. Float number between 0.0 and 1.0
6340 means the percentage of the screen space from the top. The default
6341 cycling order is middle -> top -> bottom."
6342 :type '(repeat (choice
6343 (const :tag "Top" top)
6344 (const :tag "Middle" middle)
6345 (const :tag "Bottom" bottom)
6346 (integer :tag "Line number")
6347 (float :tag "Percentage")))
6348 :version "23.2"
6349 :group 'windows)
6351 (defun recenter-top-bottom (&optional arg)
6352 "Move current buffer line to the specified window line.
6353 With no prefix argument, successive calls place point according
6354 to the cycling order defined by `recenter-positions'.
6356 A prefix argument is handled like `recenter':
6357 With numeric prefix ARG, move current line to window-line ARG.
6358 With plain `C-u', move current line to window center."
6359 (interactive "P")
6360 (cond
6361 (arg (recenter arg)) ; Always respect ARG.
6363 (setq recenter-last-op
6364 (if (eq this-command last-command)
6365 (car (or (cdr (member recenter-last-op recenter-positions))
6366 recenter-positions))
6367 (car recenter-positions)))
6368 (let ((this-scroll-margin
6369 (min (max 0 scroll-margin)
6370 (truncate (/ (window-body-height) 4.0)))))
6371 (cond ((eq recenter-last-op 'middle)
6372 (recenter))
6373 ((eq recenter-last-op 'top)
6374 (recenter this-scroll-margin))
6375 ((eq recenter-last-op 'bottom)
6376 (recenter (- -1 this-scroll-margin)))
6377 ((integerp recenter-last-op)
6378 (recenter recenter-last-op))
6379 ((floatp recenter-last-op)
6380 (recenter (round (* recenter-last-op (window-height))))))))))
6382 (define-key global-map [?\C-l] 'recenter-top-bottom)
6384 (defun move-to-window-line-top-bottom (&optional arg)
6385 "Position point relative to window.
6387 With a prefix argument ARG, acts like `move-to-window-line'.
6389 With no argument, positions point at center of window.
6390 Successive calls position point at positions defined
6391 by `recenter-positions'."
6392 (interactive "P")
6393 (cond
6394 (arg (move-to-window-line arg)) ; Always respect ARG.
6396 (setq recenter-last-op
6397 (if (eq this-command last-command)
6398 (car (or (cdr (member recenter-last-op recenter-positions))
6399 recenter-positions))
6400 (car recenter-positions)))
6401 (let ((this-scroll-margin
6402 (min (max 0 scroll-margin)
6403 (truncate (/ (window-body-height) 4.0)))))
6404 (cond ((eq recenter-last-op 'middle)
6405 (call-interactively 'move-to-window-line))
6406 ((eq recenter-last-op 'top)
6407 (move-to-window-line this-scroll-margin))
6408 ((eq recenter-last-op 'bottom)
6409 (move-to-window-line (- -1 this-scroll-margin)))
6410 ((integerp recenter-last-op)
6411 (move-to-window-line recenter-last-op))
6412 ((floatp recenter-last-op)
6413 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
6415 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
6417 ;;; Scrolling commands.
6419 ;;; Scrolling commands which do not signal errors at top/bottom
6420 ;;; of buffer at first key-press (instead move to top/bottom
6421 ;;; of buffer).
6423 (defcustom scroll-error-top-bottom nil
6424 "Move point to top/bottom of buffer before signaling a scrolling error.
6425 A value of nil means just signal an error if no more scrolling possible.
6426 A value of t means point moves to the beginning or the end of the buffer
6427 \(depending on scrolling direction) when no more scrolling possible.
6428 When point is already on that position, then signal an error."
6429 :type 'boolean
6430 :group 'windows
6431 :version "24.1")
6433 (defun scroll-up-command (&optional arg)
6434 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
6435 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
6436 scroll window further, move cursor to the bottom line.
6437 When point is already on that position, then signal an error.
6438 A near full screen is `next-screen-context-lines' less than a full screen.
6439 Negative ARG means scroll downward.
6440 If ARG is the atom `-', scroll downward by nearly full screen."
6441 (interactive "^P")
6442 (cond
6443 ((null scroll-error-top-bottom)
6444 (scroll-up arg))
6445 ((eq arg '-)
6446 (scroll-down-command nil))
6447 ((< (prefix-numeric-value arg) 0)
6448 (scroll-down-command (- (prefix-numeric-value arg))))
6449 ((eobp)
6450 (scroll-up arg)) ; signal error
6452 (condition-case nil
6453 (scroll-up arg)
6454 (end-of-buffer
6455 (if arg
6456 ;; When scrolling by ARG lines can't be done,
6457 ;; move by ARG lines instead.
6458 (forward-line arg)
6459 ;; When ARG is nil for full-screen scrolling,
6460 ;; move to the bottom of the buffer.
6461 (goto-char (point-max))))))))
6463 (put 'scroll-up-command 'scroll-command t)
6465 (defun scroll-down-command (&optional arg)
6466 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
6467 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
6468 scroll window further, move cursor to the top line.
6469 When point is already on that position, then signal an error.
6470 A near full screen is `next-screen-context-lines' less than a full screen.
6471 Negative ARG means scroll upward.
6472 If ARG is the atom `-', scroll upward by nearly full screen."
6473 (interactive "^P")
6474 (cond
6475 ((null scroll-error-top-bottom)
6476 (scroll-down arg))
6477 ((eq arg '-)
6478 (scroll-up-command nil))
6479 ((< (prefix-numeric-value arg) 0)
6480 (scroll-up-command (- (prefix-numeric-value arg))))
6481 ((bobp)
6482 (scroll-down arg)) ; signal error
6484 (condition-case nil
6485 (scroll-down arg)
6486 (beginning-of-buffer
6487 (if arg
6488 ;; When scrolling by ARG lines can't be done,
6489 ;; move by ARG lines instead.
6490 (forward-line (- arg))
6491 ;; When ARG is nil for full-screen scrolling,
6492 ;; move to the top of the buffer.
6493 (goto-char (point-min))))))))
6495 (put 'scroll-down-command 'scroll-command t)
6497 ;;; Scrolling commands which scroll a line instead of full screen.
6499 (defun scroll-up-line (&optional arg)
6500 "Scroll text of selected window upward ARG lines; or one line if no ARG.
6501 If ARG is omitted or nil, scroll upward by one line.
6502 This is different from `scroll-up-command' that scrolls a full screen."
6503 (interactive "p")
6504 (scroll-up (or arg 1)))
6506 (put 'scroll-up-line 'scroll-command t)
6508 (defun scroll-down-line (&optional arg)
6509 "Scroll text of selected window down ARG lines; or one line if no ARG.
6510 If ARG is omitted or nil, scroll down by one line.
6511 This is different from `scroll-down-command' that scrolls a full screen."
6512 (interactive "p")
6513 (scroll-down (or arg 1)))
6515 (put 'scroll-down-line 'scroll-command t)
6518 (defun scroll-other-window-down (&optional lines)
6519 "Scroll the \"other window\" down.
6520 For more details, see the documentation for `scroll-other-window'."
6521 (interactive "P")
6522 (scroll-other-window
6523 ;; Just invert the argument's meaning.
6524 ;; We can do that without knowing which window it will be.
6525 (if (eq lines '-) nil
6526 (if (null lines) '-
6527 (- (prefix-numeric-value lines))))))
6529 (defun beginning-of-buffer-other-window (arg)
6530 "Move point to the beginning of the buffer in the other window.
6531 Leave mark at previous position.
6532 With arg N, put point N/10 of the way from the true beginning."
6533 (interactive "P")
6534 (let ((orig-window (selected-window))
6535 (window (other-window-for-scrolling)))
6536 ;; We use unwind-protect rather than save-window-excursion
6537 ;; because the latter would preserve the things we want to change.
6538 (unwind-protect
6539 (progn
6540 (select-window window)
6541 ;; Set point and mark in that window's buffer.
6542 (with-no-warnings
6543 (beginning-of-buffer arg))
6544 ;; Set point accordingly.
6545 (recenter '(t)))
6546 (select-window orig-window))))
6548 (defun end-of-buffer-other-window (arg)
6549 "Move point to the end of the buffer in the other window.
6550 Leave mark at previous position.
6551 With arg N, put point N/10 of the way from the true end."
6552 (interactive "P")
6553 ;; See beginning-of-buffer-other-window for comments.
6554 (let ((orig-window (selected-window))
6555 (window (other-window-for-scrolling)))
6556 (unwind-protect
6557 (progn
6558 (select-window window)
6559 (with-no-warnings
6560 (end-of-buffer arg))
6561 (recenter '(t)))
6562 (select-window orig-window))))
6564 (defvar mouse-autoselect-window-timer nil
6565 "Timer used by delayed window autoselection.")
6567 (defvar mouse-autoselect-window-position nil
6568 "Last mouse position recorded by delayed window autoselection.")
6570 (defvar mouse-autoselect-window-window nil
6571 "Last window recorded by delayed window autoselection.")
6573 (defvar mouse-autoselect-window-state nil
6574 "When non-nil, special state of delayed window autoselection.
6575 Possible values are `suspend' (suspend autoselection after a menu or
6576 scrollbar interaction) and `select' (the next invocation of
6577 `handle-select-window' shall select the window immediately).")
6579 (defun mouse-autoselect-window-cancel (&optional force)
6580 "Cancel delayed window autoselection.
6581 Optional argument FORCE means cancel unconditionally."
6582 (unless (and (not force)
6583 ;; Don't cancel for select-window or select-frame events
6584 ;; or when the user drags a scroll bar.
6585 (or (memq this-command
6586 '(handle-select-window handle-switch-frame))
6587 (and (eq this-command 'scroll-bar-toolkit-scroll)
6588 (memq (nth 4 (event-end last-input-event))
6589 '(handle end-scroll)))))
6590 (setq mouse-autoselect-window-state nil)
6591 (when (timerp mouse-autoselect-window-timer)
6592 (cancel-timer mouse-autoselect-window-timer))
6593 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
6595 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
6596 "Start delayed window autoselection.
6597 MOUSE-POSITION is the last position where the mouse was seen as returned
6598 by `mouse-position'. Optional argument WINDOW non-nil denotes the
6599 window where the mouse was seen. Optional argument SUSPEND non-nil
6600 means suspend autoselection."
6601 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
6602 (setq mouse-autoselect-window-position mouse-position)
6603 (when window (setq mouse-autoselect-window-window window))
6604 (setq mouse-autoselect-window-state (when suspend 'suspend))
6605 ;; Install timer which runs `mouse-autoselect-window-select' after
6606 ;; `mouse-autoselect-window' seconds.
6607 (setq mouse-autoselect-window-timer
6608 (run-at-time
6609 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
6611 (defun mouse-autoselect-window-select ()
6612 "Select window with delayed window autoselection.
6613 If the mouse position has stabilized in a non-selected window, select
6614 that window. The minibuffer window is selected only if the minibuffer
6615 is active. This function is run by `mouse-autoselect-window-timer'."
6616 (ignore-errors
6617 (let* ((mouse-position (mouse-position))
6618 (window
6619 (ignore-errors
6620 (window-at (cadr mouse-position) (cddr mouse-position)
6621 (car mouse-position)))))
6622 (cond
6623 ((or (menu-or-popup-active-p)
6624 (and window
6625 (not (coordinates-in-window-p (cdr mouse-position) window))))
6626 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
6627 ;; of WINDOW, temporarily suspend delayed autoselection.
6628 (mouse-autoselect-window-start mouse-position nil t))
6629 ((eq mouse-autoselect-window-state 'suspend)
6630 ;; Delayed autoselection was temporarily suspended, reenable it.
6631 (mouse-autoselect-window-start mouse-position))
6632 ((and window (not (eq window (selected-window)))
6633 (or (not (numberp mouse-autoselect-window))
6634 (and (> mouse-autoselect-window 0)
6635 ;; If `mouse-autoselect-window' is positive, select
6636 ;; window if the window is the same as before.
6637 (eq window mouse-autoselect-window-window))
6638 ;; Otherwise select window if the mouse is at the same
6639 ;; position as before. Observe that the first test after
6640 ;; starting autoselection usually fails since the value of
6641 ;; `mouse-autoselect-window-position' recorded there is the
6642 ;; position where the mouse has entered the new window and
6643 ;; not necessarily where the mouse has stopped moving.
6644 (equal mouse-position mouse-autoselect-window-position))
6645 ;; The minibuffer is a candidate window if it's active.
6646 (or (not (window-minibuffer-p window))
6647 (eq window (active-minibuffer-window))))
6648 ;; Mouse position has stabilized in non-selected window: Cancel
6649 ;; delayed autoselection and try to select that window.
6650 (mouse-autoselect-window-cancel t)
6651 ;; Select window where mouse appears unless the selected window is the
6652 ;; minibuffer. Use `unread-command-events' in order to execute pre-
6653 ;; and post-command hooks and trigger idle timers. To avoid delaying
6654 ;; autoselection again, set `mouse-autoselect-window-state'."
6655 (unless (window-minibuffer-p (selected-window))
6656 (setq mouse-autoselect-window-state 'select)
6657 (setq unread-command-events
6658 (cons (list 'select-window (list window))
6659 unread-command-events))))
6660 ((or (and window (eq window (selected-window)))
6661 (not (numberp mouse-autoselect-window))
6662 (equal mouse-position mouse-autoselect-window-position))
6663 ;; Mouse position has either stabilized in the selected window or at
6664 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
6665 (mouse-autoselect-window-cancel t))
6667 ;; Mouse position has not stabilized yet, resume delayed
6668 ;; autoselection.
6669 (mouse-autoselect-window-start mouse-position window))))))
6671 (defun handle-select-window (event)
6672 "Handle select-window events."
6673 (interactive "e")
6674 (let ((window (posn-window (event-start event))))
6675 (unless (or (not (window-live-p window))
6676 ;; Don't switch if we're currently in the minibuffer.
6677 ;; This tries to work around problems where the
6678 ;; minibuffer gets unselected unexpectedly, and where
6679 ;; you then have to move your mouse all the way down to
6680 ;; the minibuffer to select it.
6681 (window-minibuffer-p (selected-window))
6682 ;; Don't switch to minibuffer window unless it's active.
6683 (and (window-minibuffer-p window)
6684 (not (minibuffer-window-active-p window)))
6685 ;; Don't switch when autoselection shall be delayed.
6686 (and (numberp mouse-autoselect-window)
6687 (not (zerop mouse-autoselect-window))
6688 (not (eq mouse-autoselect-window-state 'select))
6689 (progn
6690 ;; Cancel any delayed autoselection.
6691 (mouse-autoselect-window-cancel t)
6692 ;; Start delayed autoselection from current mouse
6693 ;; position and window.
6694 (mouse-autoselect-window-start (mouse-position) window)
6695 ;; Executing a command cancels delayed autoselection.
6696 (add-hook
6697 'pre-command-hook 'mouse-autoselect-window-cancel))))
6698 (when mouse-autoselect-window
6699 ;; Reset state of delayed autoselection.
6700 (setq mouse-autoselect-window-state nil)
6701 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
6702 (run-hooks 'mouse-leave-buffer-hook))
6703 ;; Clear echo area.
6704 (message nil)
6705 (select-window window))))
6707 (defun truncated-partial-width-window-p (&optional window)
6708 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
6709 WINDOW must be a live window and defaults to the selected one.
6710 Return nil if WINDOW is not a partial-width window
6711 (regardless of the value of `truncate-lines').
6712 Otherwise, consult the value of `truncate-partial-width-windows'
6713 for the buffer shown in WINDOW."
6714 (setq window (window-normalize-window window t))
6715 (unless (window-full-width-p window)
6716 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
6717 (window-buffer window))))
6718 (if (integerp t-p-w-w)
6719 (< (window-width window) t-p-w-w)
6720 t-p-w-w))))
6722 ;; Some of these are in tutorial--default-keys, so update that if you
6723 ;; change these.
6724 (define-key ctl-x-map "0" 'delete-window)
6725 (define-key ctl-x-map "1" 'delete-other-windows)
6726 (define-key ctl-x-map "2" 'split-window-below)
6727 (define-key ctl-x-map "3" 'split-window-right)
6728 (define-key ctl-x-map "o" 'other-window)
6729 (define-key ctl-x-map "^" 'enlarge-window)
6730 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
6731 (define-key ctl-x-map "{" 'shrink-window-horizontally)
6732 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
6733 (define-key ctl-x-map "+" 'balance-windows)
6734 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
6736 ;;; window.el ends here