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