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