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