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