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