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