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