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