New code for replacing buffers in windows, mostly in Elisp now.
[emacs.git] / lisp / window.el
blobf8a278b185a1efbf9fb4c86b966316879d20eff0
1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992, 1993, 1994, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Maintainer: FSF
8 ;; Keywords: internal
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; TODO
29 ;; Use `walk-window-tree' instead of `window-list-1' wherever possible.
30 ;; Or maybe not because writing code with wwt is not very transparent.
31 ;; Or better, rewrite wwt as a macro.
33 ;; Use (pop-up-windows (or pop-up-windows t)) instead of (pop-up-windows
34 ;; t) wherever this is locally rebound (has some twenty hits in Elisp
35 ;; sources).
37 ;;; Code:
39 (eval-when-compile (require 'cl))
41 (defmacro save-selected-window (&rest body)
42 "Execute BODY, then select the previously selected window.
43 The value returned is the value of the last form in BODY.
45 This macro saves and restores the selected window, as well as the
46 selected window in each frame. If the previously selected window
47 is no longer live, then whatever window is selected at the end of
48 BODY remains selected. If the previously selected window of some
49 frame is no longer live at the end of BODY, that frame's selected
50 window is left alone.
52 This macro saves and restores the current buffer, since otherwise
53 its normal operation could make a different buffer current. The
54 order of recently selected windows and the buffer list ordering
55 are not altered by this macro (unless they are altered in BODY)."
56 `(let ((save-selected-window-window (selected-window))
57 ;; It is necessary to save all of these, because calling
58 ;; select-window changes frame-selected-window for whatever
59 ;; frame that window is in.
60 (save-selected-window-alist
61 (mapcar (lambda (frame) (cons frame (frame-selected-window frame)))
62 (frame-list))))
63 (save-current-buffer
64 (unwind-protect
65 (progn ,@body)
66 (dolist (elt save-selected-window-alist)
67 (and (frame-live-p (car elt))
68 (window-live-p (cdr elt))
69 (set-frame-selected-window (car elt) (cdr elt) 'norecord)))
70 (when (window-live-p save-selected-window-window)
71 (select-window save-selected-window-window 'norecord))))))
73 ;; The following two functions are like `window-next' and `window-prev'
74 ;; but the WINDOW argument is _not_ optional (so they don't substitute
75 ;; the selected window for nil), and they return nil when WINDOW doesn't
76 ;; have a parent (like a frame's root window or a minibuffer window).
77 (defsubst window-right (window)
78 "Return WINDOW's right 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-next window)))
83 (defsubst window-left (window)
84 "Return WINDOW's left sibling.
85 Return nil if WINDOW is the root window of its frame. WINDOW can
86 be any window."
87 (and window (window-parent window) (window-prev window)))
89 (defsubst window-child (window)
90 "Return WINDOW's first child window."
91 (or (window-vchild window) (window-hchild window)))
93 (defsubst window-internal-p (object)
94 "Return t if OBJECT is an internal window and nil otherwise.
95 An internal window is a window that has either a vertical or a
96 horizontal child window."
97 (and (windowp object) (window-child object) t))
99 (defsubst window-any-p (object)
100 "Return t if OBJECT denotes a live or internal window."
101 (and (windowp object)
102 (or (window-buffer object) (window-child object))
105 (defsubst normalize-live-buffer (buffer-or-name)
106 "Return buffer specified by BUFFER-OR-NAME.
107 BUFFER-OR-NAME must be either a buffer or a string naming a live
108 buffer and defaults to the current buffer."
109 (cond
110 ((not buffer-or-name)
111 (current-buffer))
112 ((bufferp buffer-or-name)
113 (if (buffer-live-p buffer-or-name)
114 buffer-or-name
115 (error "Buffer %s is not a live buffer" buffer-or-name)))
116 ((get-buffer buffer-or-name))
118 (error "No such buffer %s" buffer-or-name))))
120 ;; This should probably go to frame.el.
121 (defsubst normalize-live-frame (frame)
122 "Return normalized FRAME argument for live frames."
123 (if frame
124 (if (frame-live-p frame)
125 frame
126 (error "%s is not a live frame" frame))
127 (selected-frame)))
129 (defsubst normalize-any-window (window)
130 "Return normalized WINDOW argument for any window.
131 WINDOW defaults to the selected window."
132 (if window
133 (if (window-any-p window)
134 window
135 (error "%s is not a window" window))
136 (selected-window)))
138 (defsubst normalize-live-window (window)
139 "Return normalized WINDOW argument for live windows.
140 WINDOW defaults to the selected window."
141 (if window
142 (if (and (windowp window) (window-buffer window))
143 window
144 (error "%s is not a live window" window))
145 (selected-window)))
147 (defvar ignore-window-parameters nil
148 "If non-nil standard functions ignore window parameters.
149 The functions currently affected by this are `split-window',
150 `delete-window', `delete-other-windows' and `other-window'.
152 When this variable equals `pre', parameters are not consulted
153 before but are updated after performing the requested operation.
154 When this variable equals `post', parameters are consulted before
155 but are not updated after performing the requested operation.
157 The value t means parameters are not consulted before and not
158 updated after performing the requested operation. Currently any
159 other non-nil value is handled like t.
161 An application may bind this to a non-nil value around calls to
162 these functions. If it does so and the value is not `pre', the
163 application is fully responsible for correctly setting the
164 parameters of all windows participating in the function called.")
166 (defconst window-safe-min-height 1
167 "The absolut minimum number of lines of a window.
168 Anything less might crash Emacs.")
170 (defcustom window-min-height 4
171 "The minimum number of lines of any window.
172 The value has to accomodate a mode- or header-line if present. A
173 value less than `window-safe-min-height' is ignored. The value
174 of this variable is honored when windows are resized or split.
176 Applications should never rebind this variable. To resize a
177 window to a height less than the one specified here, an
178 application should instead call `resize-window' with a non-nil
179 IGNORE argument. In order to have `split-window' make a window
180 shorter, explictly specify the SIZE argument of that function."
181 :type 'integer
182 :version "24.1"
183 :group 'windows)
185 (defconst window-safe-min-width 2
186 "The absolut minimum number of columns of a window.
187 Anything less might crash Emacs.")
189 (defcustom window-min-width 10
190 "The minimum number of columns of any window.
191 The value has to accomodate margins, fringes, or scrollbars if
192 present. A value less than `window-safe-min-width' is ignored.
193 The value of this variable is honored when windows are resized or
194 split.
196 Applications should never rebind this variable. To resize a
197 window to a width less than the one specified here, an
198 application should instead call `resize-window' with a non-nil
199 IGNORE argument. In order to have `split-window' make a window
200 narrower, explictly specify the SIZE argument of that function."
201 :type 'integer
202 :version "24.1"
203 :group 'windows)
205 (defsubst window-iso-combined-p (&optional window horizontal)
206 "Return non-nil if and only if WINDOW is vertically combined.
207 WINDOW can be any window and defaults to the selected one.
208 Optional argument HORIZONTAL non-nil means return non-nil if and
209 only if WINDOW is horizontally combined."
210 (setq window (normalize-any-window window))
211 (when (window-parent window)
212 (if horizontal
213 (window-hchild (window-parent window))
214 (window-vchild (window-parent window)))))
216 (defvar window-size-fixed nil
217 "Non-nil in a buffer means windows displaying the buffer are fixed-size.
218 If the value is `height', then only the window's height is fixed.
219 If the value is `width', then only the window's width is fixed.
220 Any other non-nil value fixes both the width and the height.
221 Emacs won't change the size of any window displaying that buffer,
222 unless you explicitly change the size, or Emacs has no other
223 choice \(like when deleting a neighboring window).")
224 (make-variable-buffer-local 'window-size-fixed)
226 (defsubst window-size-ignore (window ignore)
227 "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
228 (if (window-any-p ignore) (eq window ignore) ignore))
230 (defun window-min-size (&optional window horizontal ignore)
231 "Return the minimum number of lines of WINDOW.
232 WINDOW can be an arbitrary window and defaults to the selected
233 one. Optional argument HORIZONTAL non-nil means return the
234 minimum number of columns of WINDOW.
236 Optional argument IGNORE non-nil means ignore any restrictions
237 imposed by fixed size windows, `window-min-height' or
238 `window-min-width' settings. IGNORE equal `safe' means live
239 windows may get as small as `window-safe-min-height' lines and
240 `window-safe-min-width' columns. IGNORE a window means ignore
241 restrictions for that window only."
242 (window-min-size-1
243 (normalize-any-window window) horizontal ignore))
245 (defun window-min-size-1 (window horizontal ignore)
246 "Internal function of `window-min-size'."
247 (let ((sub (window-child window)))
248 (if sub
249 (let ((value 0))
250 ;; WINDOW is an internal window.
251 (if (window-iso-combined-p sub horizontal)
252 ;; The minimum size of an iso-combination is the sum of
253 ;; the minimum sizes of its subwindows.
254 (while sub
255 (setq value (+ value
256 (window-min-size-1 sub horizontal ignore)))
257 (setq sub (window-right sub)))
258 ;; The minimum size of an ortho-combination is the maximum of
259 ;; the minimum sizes of its subwindows.
260 (while sub
261 (setq value (max value
262 (window-min-size-1 sub horizontal ignore)))
263 (setq sub (window-right sub))))
264 value)
265 (with-current-buffer (window-buffer window)
266 (cond
267 ((and (not (window-size-ignore window ignore))
268 (window-size-fixed-p window horizontal))
269 ;; The minimum size of a fixed size window is its size.
270 (window-total-size window horizontal))
271 ((or (eq ignore 'safe) (eq ignore window))
272 ;; If IGNORE equals `safe' or WINDOW return the safe values.
273 (if horizontal window-safe-min-width window-safe-min-height))
274 (horizontal
275 ;; For the minimum width of a window take fringes and
276 ;; scroll-bars into account. This is questionable and should
277 ;; be removed as soon as we are able to split (and resize)
278 ;; windows such that the new (or resized) windows can get a
279 ;; size less than the user-specified `window-min-height' and
280 ;; `window-min-width'.
281 (let ((frame (window-frame window))
282 (fringes (window-fringes window))
283 (scroll-bars (window-scroll-bars window)))
284 (max
285 (+ window-safe-min-width
286 (ceiling (car fringes) (frame-char-width frame))
287 (ceiling (cadr fringes) (frame-char-width frame))
288 (cond
289 ((memq (nth 2 scroll-bars) '(left right))
290 (nth 1 scroll-bars))
291 ((memq (frame-parameter frame 'vertical-scroll-bars)
292 '(left right))
293 (ceiling (or (frame-parameter frame 'scroll-bar-width) 14)
294 (frame-char-width)))
295 (t 0)))
296 (if (and (not (window-size-ignore window ignore))
297 (numberp window-min-width))
298 window-min-width
299 0))))
301 ;; For the minimum height of a window take any mode- or
302 ;; header-line into account.
303 (max (+ window-safe-min-height
304 (if header-line-format 1 0)
305 (if mode-line-format 1 0))
306 (if (and (not (window-size-ignore window ignore))
307 (numberp window-min-height))
308 window-min-height
309 0))))))))
311 (defun window-sizable (window delta &optional horizontal ignore)
312 "Return DELTA if DELTA lines can be added to WINDOW.
313 Optional argument HORIZONTAL non-nil means return DELTA if DELTA
314 columns can be added to WINDOW. A return value of zero means
315 that no lines (or columns) can be added to WINDOW.
317 This function looks only at WINDOW and its subwindows. The
318 function `window-resizable' looks at other windows as well.
320 DELTA positive means WINDOW shall be enlarged by DELTA lines or
321 columns. If WINDOW cannot be enlarged by DELTA lines or columns
322 return the maximum value in the range 0..DELTA by which WINDOW
323 can be enlarged.
325 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
326 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
327 return the minimum value in the range DELTA..0 by which WINDOW
328 can be shrunk.
330 Optional argument IGNORE non-nil means ignore any restrictions
331 imposed by fixed size windows, `window-min-height' or
332 `window-min-width' settings. IGNORE equal `safe' means live
333 windows may get as small as `window-safe-min-height' lines and
334 `window-safe-min-width' columns. IGNORE any window means ignore
335 restrictions for that window only."
336 (setq window (normalize-any-window window))
337 (cond
338 ((< delta 0)
339 (max (- (window-min-size window horizontal ignore)
340 (window-total-size window horizontal))
341 delta))
342 ((window-size-ignore window ignore)
343 delta)
344 ((> delta 0)
345 (if (window-size-fixed-p window horizontal)
347 delta))
348 (t 0)))
350 (defsubst window-sizable-p (window delta &optional horizontal ignore)
351 "Return t if WINDOW can have DELTA lines.
352 For the meaning of the arguments of this function see the
353 doc-string of `window-sizable'."
354 (setq window (normalize-any-window window))
355 (if (> delta 0)
356 (>= (window-sizable window delta horizontal ignore) delta)
357 (<= (window-sizable window delta horizontal ignore) delta)))
359 (defun window-size-fixed-p (&optional window horizontal)
360 "Return non-nil if WINDOW's height is fixed.
361 WINDOW can be an arbitrary window and defaults to the selected
362 window. Optional argument HORIZONTAL non-nil means return
363 non-nil if WINDOW's width is fixed.
365 If this function returns nil, this does not necessarily mean that
366 WINDOW can be resized in the desired direction. The functions
367 `window-resizable' and `window-resizable-p' will tell that."
368 (window-size-fixed-1
369 (normalize-any-window window) horizontal))
371 (defun window-size-fixed-1 (window horizontal)
372 "Internal function for `window-size-fixed-p'."
373 (let ((sub (window-child window)))
374 (catch 'fixed
375 (if sub
376 ;; WINDOW is an internal window.
377 (if (window-iso-combined-p sub horizontal)
378 ;; An iso-combination is fixed size if all its subwindows
379 ;; are fixed-size.
380 (progn
381 (while sub
382 (unless (window-size-fixed-1 sub horizontal)
383 ;; We found a non-fixed-size subwindow, so WINDOW's
384 ;; size is not fixed.
385 (throw 'fixed nil))
386 (setq sub (window-right sub)))
387 ;; All subwindows are fixed-size, so WINDOW's size is
388 ;; fixed.
389 (throw 'fixed t))
390 ;; An ortho-combination is fixed-size if at least one of its
391 ;; subwindows is fixed-size.
392 (while sub
393 (when (window-size-fixed-1 sub horizontal)
394 ;; We found a fixed-size subwindow, so WINDOW's size is
395 ;; fixed.
396 (throw 'fixed t))
397 (setq sub (window-right sub))))
398 ;; WINDOW is a live window.
399 (with-current-buffer (window-buffer window)
400 (if horizontal
401 (memq window-size-fixed '(width t))
402 (memq window-size-fixed '(height t))))))))
404 (defun window-min-delta (&optional window horizontal ignore side noup nodown)
405 "Return number of lines by which WINDOW can be shrunk.
406 WINDOW can be an arbitrary window and defaults to the selected
407 window. Return zero if WINDOW cannot be shrunk.
409 Optional argument HORIZONTAL non-nil means return number of
410 columns by which WINDOW can be shrunk.
412 Optional argument IGNORE non-nil means ignore any restrictions
413 imposed by fixed size windows, `window-min-height' or
414 `window-min-width' settings. IGNORE a window means ignore
415 restrictions for that window only. IGNORE equal `safe' means
416 live windows may get as small as `window-safe-min-height' lines
417 and `window-safe-min-width' columns.
419 Optional argument SIDE `left' means assume only windows to the
420 left of or above WINDOW can be enlarged. Optional argument SIDE
421 `right' means assumes only windows to the right of or below
422 WINDOW can be enlarged.
424 Optional argument NOUP non-nil means don't go up in the window
425 tree but try to enlarge windows within WINDOW's combination only.
427 Optional argument NODOWN non-nil means don't check whether WINDOW
428 and its subwindows can be shrunk."
429 (setq window (normalize-any-window window))
430 (let ((size (window-total-size window horizontal))
431 (minimum (window-min-size window horizontal ignore)))
432 (if (and (not nodown) (= size minimum))
433 ;; Nothing to recover.
435 (window-min-delta-1
436 ;; Think positive.
437 window (- size minimum) horizontal ignore side noup))))
439 (defun window-min-delta-1 (window delta &optional horizontal ignore side noup)
440 "Internal function for `window-min-delta'."
441 (if (not (window-parent window))
442 0 ; delta
443 ;;; (min delta
444 ;;; (- (window-total-size window horizontal)
445 ;;; (window-min-size window horizontal ignore)))
446 (let* ((parent (window-parent window))
447 (sub (window-child parent)))
448 (catch 'done
449 (if (window-iso-combined-p sub horizontal)
450 ;; In an iso-combination throw DELTA if we find at least one
451 ;; subwindow and that subwindow is either non-fixed-size or
452 ;; we can ignore fixed-sizeness.
453 (let ((skip (eq side 'right)))
454 (while sub
455 (cond
456 ((eq sub window)
457 (setq skip (eq side 'left)))
458 (skip)
459 ((and (not (window-size-ignore window ignore))
460 (window-size-fixed-p sub horizontal)))
462 ;; We found a non-fixed-size subwindow.
463 (throw 'done delta)))
464 (setq sub (window-right sub))))
465 ;; In an ortho-combination set DELTA to the minimum value by
466 ;; which other subwindows can shrink.
467 (while sub
468 (unless (eq sub window)
469 (setq delta
470 (min delta
471 (- (window-total-size sub horizontal)
472 (window-min-size sub horizontal ignore)))))
473 (setq sub (window-right sub))))
474 (if noup
475 delta
476 (window-min-delta-1 parent delta horizontal ignore side))))))
478 (defun window-max-delta (&optional window horizontal ignore side noup nodown)
479 "Return maximum number of lines WINDOW by which WINDOW can be enlarged.
480 WINDOW can be an arbitrary window and defaults to the selected
481 window. The return value is zero if WINDOW cannot be enlarged.
483 Optional argument HORIZONTAL non-nil means return maximum number
484 of columns by which WINDOW can be enlarged.
486 Optional argument IGNORE non-nil means ignore any restrictions
487 imposed by fixed size windows, `window-min-height' or
488 `window-min-width' settings. IGNORE a window means ignore
489 restrictions for that window only. IGNORE equal `safe' means
490 live windows may get as small as `window-safe-min-height' lines
491 and `window-safe-min-width' columns.
493 Optional argument SIDE `left' means assume only windows to the
494 left of or below WINDOW can be shrunk. Optional argument SIDE
495 `right' means assumes only windows to the right of or above
496 WINDOW can be shrunk.
498 Optional argument NOUP non-nil means don't go up in the window
499 tree but try to obtain the entire space from windows within
500 WINDOW's combination.
502 Optional argument NODOWN non-nil means do not check whether
503 WINDOW and its subwindows can be enlarged."
504 (setq window (normalize-any-window window))
505 (if (and (not (window-size-ignore window ignore))
506 (not nodown) (window-size-fixed-p window horizontal))
508 (window-max-delta-1 window 0 horizontal ignore side noup)))
510 (defun window-max-delta-1 (window delta &optional horizontal ignore side noup)
511 "Internal function of `window-max-delta'."
512 (if (not (window-parent window))
513 ;; Can't go up. Return DELTA.
514 delta
515 (let* ((parent (window-parent window))
516 (sub (window-child parent)))
517 (catch 'fixed
518 (if (window-iso-combined-p sub horizontal)
519 ;; For an iso-combination calculate how much we can get from
520 ;; other subwindows.
521 (let ((skip (eq side 'right)))
522 (while sub
523 (cond
524 ((eq sub window)
525 (setq skip (eq side 'left)))
526 (skip)
528 (setq delta
529 (+ delta
530 (- (window-total-size sub horizontal)
531 (window-min-size sub horizontal ignore))))))
532 (setq sub (window-right sub))))
533 ;; For an ortho-combination throw DELTA when at least one
534 ;; subwindow is fixed-size.
535 (while sub
536 (when (and (not (eq sub window))
537 (not (window-size-ignore sub ignore))
538 (window-size-fixed-p sub horizontal))
539 (throw 'fixed delta))
540 (setq sub (window-right sub))))
541 (if noup
542 delta
543 ;; Try to go up.
544 (window-max-delta-1 parent delta horizontal ignore side))))))
546 ;; Make NOUP also inhibit the min-size check.
547 (defun window-resizable (window delta &optional horizontal ignore side noup nodown)
548 "Return DELTA if WINDOW can be resized vertically by DELTA lines.
549 Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
550 can be resized horizontally by DELTA columns. A return value of
551 zero means that WINDOW is not resizable.
553 DELTA positive means WINDOW shall be enlarged by DELTA lines or
554 columns. If WINDOW cannot be enlarged by DELTA lines or columns
555 return the maximum value in the range 0..DELTA by which WINDOW
556 can be enlarged.
558 DELTA negative means WINDOW shall be shrunk by -DELTA lines or
559 columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
560 return the minimum value in the range DELTA..0 that can be used
561 for shrinking WINDOW.
563 Optional argument IGNORE non-nil means ignore any restrictions
564 imposed by fixed size windows, `window-min-height' or
565 `window-min-width' settings. IGNORE a window means ignore
566 restrictions for that window only. IGNORE equal `safe' means
567 live windows may get as small as `window-safe-min-height' lines
568 and `window-safe-min-width' columns.
570 Optional argument NOUP non-nil means don't go up in the window
571 tree but try to distribute the space among the other windows
572 within WINDOW's combination.
574 Optional argument NODOWN non-nil means don't check whether WINDOW
575 and its subwindows can be resized."
576 (setq window (normalize-any-window window))
577 (cond
578 ((< delta 0)
579 (max (- (window-min-delta window horizontal ignore side noup nodown))
580 delta))
581 ((> delta 0)
582 (min (window-max-delta window horizontal ignore side noup nodown)
583 delta))
584 (t 0)))
586 (defun window-resizable-p (window delta &optional horizontal ignore side noup nodown)
587 "Return t if WINDOW can be resized vertically by DELTA lines.
588 For the meaning of the arguments of this function see the
589 doc-string of `window-resizable'."
590 (setq window (normalize-any-window window))
591 (if (> delta 0)
592 (>= (window-resizable window delta horizontal ignore side noup nodown)
593 delta)
594 (<= (window-resizable window delta horizontal ignore side noup nodown)
595 delta)))
597 (defsubst window-total-height (&optional window)
598 "Return the total number of lines of WINDOW.
599 WINDOW can be any window and defaults to the selected one. The
600 return value includes WINDOW's mode line and header line, if any.
601 If WINDOW is internal the return value is the sum of the total
602 number of lines of WINDOW's child windows if these are vertically
603 combined and the height of WINDOW's first child otherwise.
605 Note: This function does not take into account the value of
606 `line-spacing' when calculating the number of lines in WINDOW."
607 (window-total-size window))
609 ;; Eventually we should make `window-height' obsolete.
610 (defalias 'window-height 'window-total-height)
612 ;; See discussion in bug#4543.
613 (defsubst window-full-height-p (&optional window)
614 "Return t if WINDOW is as high as the containing frame.
615 More precisely, return t if and only if the total height of
616 WINDOW equals the total height of the root window of WINDOW's
617 frame. WINDOW can be any window and defaults to the selected
618 one."
619 (setq window (normalize-any-window window))
620 (= (window-total-size window)
621 (window-total-size (frame-root-window window))))
623 (defsubst window-total-width (&optional window)
624 "Return the total number of columns of WINDOW.
625 WINDOW can be any window and defaults to the selected one. The
626 return value includes any vertical dividers or scrollbars of
627 WINDOW. If WINDOW is internal, the return value is the sum of
628 the total number of columns of WINDOW's child windows if these
629 are horizontally combined and the width of WINDOW's first child
630 otherwise."
631 (window-total-size window t))
633 (defsubst window-full-width-p (&optional window)
634 "Return t if WINDOW is as wide as the containing frame.
635 More precisely, return t if and only if the total width of WINDOW
636 equals the total width of the root window of WINDOW's frame.
637 WINDOW can be any window and defaults to the selected one."
638 (setq window (normalize-any-window window))
639 (= (window-total-size window t)
640 (window-total-size (frame-root-window window) t)))
642 (defsubst window-body-height (&optional window)
643 "Return the number of lines of WINDOW's body.
644 WINDOW must be a live window and defaults to the selected one.
646 The return value does not include WINDOW's mode line and header
647 line, if any. If a line at the bottom of the window is only
648 partially visible, that line is included in the return value. If
649 you do not want to include a partially visible bottom line in the
650 return value, use `window-text-height' instead."
651 (window-body-size window))
653 (defsubst window-body-width (&optional window)
654 "Return the number of columns of WINDOW's body.
655 WINDOW must be a live window and defaults to the selected one.
657 The return value does not include any vertical dividers or scroll
658 bars owned by WINDOW. On a window-system the return value does
659 not include the number of columns used for WINDOW's fringes or
660 display margins either."
661 (window-body-size window t))
663 ;; Eventually we should make `window-height' obsolete.
664 (defalias 'window-width 'window-body-width)
666 (defun window-current-scroll-bars (&optional window)
667 "Return the current scroll bar settings for WINDOW.
668 WINDOW must be a live window and defaults to the selected one.
670 The return value is a cons cell (VERTICAL . HORIZONTAL) where
671 VERTICAL specifies the current location of the vertical scroll
672 bars (`left', `right', or nil), and HORIZONTAL specifies the
673 current location of the horizontal scroll bars (`top', `bottom',
674 or nil).
676 Unlike `window-scroll-bars', this function reports the scroll bar
677 type actually used, once frame defaults and `scroll-bar-mode' are
678 taken into account."
679 (setq window (normalize-live-window window))
680 (let ((vert (nth 2 (window-scroll-bars window)))
681 (hor nil))
682 (when (or (eq vert t) (eq hor t))
683 (let ((fcsb (frame-current-scroll-bars (window-frame window))))
684 (if (eq vert t)
685 (setq vert (car fcsb)))
686 (if (eq hor t)
687 (setq hor (cdr fcsb)))))
688 (cons vert hor)))
690 (defun walk-windows (proc &optional minibuf all-frames)
691 "Cycle through all live windows, calling PROC for each one.
692 PROC must specify a function with a window as its sole argument.
693 The optional arguments MINIBUF and ALL-FRAMES specify the set of
694 windows to include in the walk.
696 MINIBUF t means include the minibuffer window even if the
697 minibuffer is not active. MINIBUF nil or omitted means include
698 the minibuffer window only if the minibuffer is active. Any
699 other value means do not include the minibuffer window even if
700 the minibuffer is active.
702 ALL-FRAMES nil or omitted means consider all windows on WINDOW's
703 frame, plus the minibuffer window if specified by the MINIBUF
704 argument. If the minibuffer counts, consider all windows on all
705 frames that share that minibuffer too. The following non-nil
706 values of ALL-FRAMES have special meanings:
708 - t means consider all windows on all existing frames.
710 - `visible' means consider all windows on all visible frames.
712 - 0 (the number zero) means consider all windows on all visible
713 and iconified frames.
715 - A frame means consider all windows on that frame only.
717 Anything else means consider all windows on WINDOW's frame and no
718 others.
720 This function changes neither the order of recently selected
721 windows nor the buffer list."
722 ;; If we start from the minibuffer window, don't fail to come
723 ;; back to it.
724 (when (window-minibuffer-p (selected-window))
725 (setq minibuf t))
726 ;; Make sure to not mess up the order of recently selected
727 ;; windows. Use `save-selected-window' and `select-window'
728 ;; with second argument non-nil for this purpose.
729 (save-selected-window
730 (when (framep all-frames)
731 (select-window (frame-first-window all-frames) 'norecord))
732 (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
733 (funcall proc walk-windows-window))))
735 (defun walk-window-tree-1 (proc walk-window-tree-window any)
736 "Helper function for `walk-window-tree'."
737 (let (walk-window-tree-buffer)
738 (while walk-window-tree-window
739 (setq walk-window-tree-buffer
740 (window-buffer walk-window-tree-window))
741 (when (or walk-window-tree-buffer any)
742 (funcall proc walk-window-tree-window))
743 (unless walk-window-tree-buffer
744 (walk-window-tree-1
745 proc (window-hchild walk-window-tree-window) any)
746 (walk-window-tree-1
747 proc (window-vchild walk-window-tree-window) any))
748 (setq walk-window-tree-window
749 (window-right walk-window-tree-window)))))
751 (defun walk-window-tree (proc &optional frame any)
752 "Run function PROC on each live window of FRAME.
753 PROC must be a function with one argument - a window. FRAME must
754 be a live frame and defaults to the selected one. ANY, if
755 non-nil means to run PROC on all live and internal windows of
756 FRAME.
758 This function performs a pre-order, depth-first traversal of the
759 window tree. If PROC changes the window tree, the result is
760 unpredictable."
761 (let ((walk-window-tree-frame (normalize-live-frame frame)))
762 (walk-window-tree-1
763 proc (frame-root-window walk-window-tree-frame) any)))
765 (defun window-in-direction-2 (window posn &optional horizontal)
766 "Support function for `window-in-direction'."
767 (if horizontal
768 (let ((top (window-top-line window)))
769 (if (> top posn)
770 (- top posn)
771 (- posn top (window-total-height window))))
772 (let ((left (window-left-column window)))
773 (if (> left posn)
774 (- left posn)
775 (- posn left (window-total-width window))))))
777 (defun window-in-direction (direction &optional window ignore)
778 "Return window in DIRECTION as seen from WINDOW.
779 DIRECTION must be one of `above', `below', `left' or `right'.
780 WINDOW must be a live window and defaults to the selected one.
781 IGNORE, when non-nil means a window can be returned even if its
782 `no-other-window' parameter is non-nil."
783 (setq window (normalize-live-window window))
784 (unless (memq direction '(above below left right))
785 (error "Wrong direction %s" direction))
786 (let* ((frame (window-frame window))
787 (hor (memq direction '(left right)))
788 (first (if hor
789 (window-left-column window)
790 (window-top-line window)))
791 (last (+ first (if hor
792 (window-total-width window)
793 (window-total-height window))))
794 (posn-cons (nth 6 (posn-at-point (window-point window) window)))
795 (posn (if hor
796 (+ (cdr posn-cons) (window-top-line window))
797 (+ (car posn-cons) (window-left-column window))))
798 (best-edge
799 (cond
800 ((eq direction 'below) (frame-height frame))
801 ((eq direction 'right) (frame-width frame))
802 (t -1)))
803 (best-edge-2 best-edge)
804 (best-diff-2 (if hor (frame-height frame) (frame-width frame)))
805 best best-2 best-diff-2-new)
806 (walk-window-tree
807 (lambda (w)
808 (let* ((w-top (window-top-line w))
809 (w-left (window-left-column w)))
810 (cond
811 ((or (eq window w)
812 ;; Ignore ourselves.
813 (and (window-parameter w 'no-other-window)
814 ;; Ignore W unless IGNORE is non-nil.
815 (not ignore))))
816 (hor
817 (cond
818 ((and (<= w-top posn)
819 (< posn (+ w-top (window-total-height w))))
820 ;; W is to the left or right of WINDOW and covers POSN.
821 (when (or (and (eq direction 'left)
822 (<= w-left first) (> w-left best-edge))
823 (and (eq direction 'right)
824 (>= w-left last) (< w-left best-edge)))
825 (setq best-edge w-left)
826 (setq best w)))
827 ((and (or (and (eq direction 'left)
828 (<= (+ w-left (window-total-width w)) first))
829 (and (eq direction 'right) (<= last w-left)))
830 ;; W is to the left or right of WINDOW but does not
831 ;; cover POSN.
832 (setq best-diff-2-new
833 (window-in-direction-2 w posn hor))
834 (or (< best-diff-2-new best-diff-2)
835 (and (= best-diff-2-new best-diff-2)
836 (if (eq direction 'left)
837 (> w-left best-edge-2)
838 (< w-left best-edge-2)))))
839 (setq best-edge-2 w-left)
840 (setq best-diff-2 best-diff-2-new)
841 (setq best-2 w))))
843 (cond
844 ((and (<= w-left posn)
845 (< posn (+ w-left (window-total-width w))))
846 ;; W is above or below WINDOW and covers POSN.
847 (when (or (and (eq direction 'above)
848 (<= w-top first) (> w-top best-edge))
849 (and (eq direction 'below)
850 (>= w-top first) (< w-top best-edge)))
851 (setq best-edge w-top)
852 (setq best w)))
853 ((and (or (and (eq direction 'above)
854 (<= (+ w-top (window-total-height w)) first))
855 (and (eq direction 'below) (<= last w-top)))
856 ;; W is above or below WINDOW but does not cover POSN.
857 (setq best-diff-2-new
858 (window-in-direction-2 w posn hor))
859 (or (< best-diff-2-new best-diff-2)
860 (and (= best-diff-2-new best-diff-2)
861 (if (eq direction 'above)
862 (> w-top best-edge-2)
863 (< w-top best-edge-2)))))
864 (setq best-edge-2 w-top)
865 (setq best-diff-2 best-diff-2-new)
866 (setq best-2 w)))))))
867 (window-frame window))
868 (or best best-2)))
870 (defun get-window-with-predicate (predicate &optional minibuf
871 all-frames default)
872 "Return a live window satisfying PREDICATE.
873 More precisely, cycle through all windows calling the function
874 PREDICATE on each one of them with the window as its sole
875 argument. Return the first window for which PREDICATE returns
876 non-nil. If no window satisfies PREDICATE, return DEFAULT.
878 ALL-FRAMES nil or omitted means consider all windows on WINDOW's
879 frame, plus the minibuffer window if specified by the MINIBUF
880 argument. If the minibuffer counts, consider all windows on all
881 frames that share that minibuffer too. The following non-nil
882 values of ALL-FRAMES have special meanings:
884 - t means consider all windows on all existing frames.
886 - `visible' means consider all windows on all visible frames.
888 - 0 (the number zero) means consider all windows on all visible
889 and iconified frames.
891 - A frame means consider all windows on that frame only.
893 Anything else means consider all windows on WINDOW's frame and no
894 others."
895 (catch 'found
896 (dolist (window (window-list-1 nil minibuf all-frames))
897 (when (funcall predicate window)
898 (throw 'found window)))
899 default))
901 (defalias 'some-window 'get-window-with-predicate)
903 (defun get-lru-window (&optional all-frames dedicated)
904 "Return the least recently used window on frames specified by ALL-FRAMES.
905 Return a full-width window if possible. A minibuffer window is
906 never a candidate. A dedicated window is never a candidate
907 unless DEDICATED is non-nil, so if all windows are dedicated, the
908 value is nil. Avoid returning the selected window if possible.
910 The following non-nil values of the optional argument ALL-FRAMES
911 have special meanings:
913 - t means consider all windows on all existing frames.
915 - `visible' means consider all windows on all visible frames.
917 - 0 (the number zero) means consider all windows on all visible
918 and iconified frames.
920 - A frame means consider all windows on that frame only.
922 Any other value of ALL-FRAMES means consider all windows on the
923 selected frame and no others."
924 (let (best-window best-time second-best-window second-best-time time)
925 (dolist (window (window-list-1 nil nil all-frames))
926 (when (or dedicated (not (window-dedicated-p window)))
927 (setq time (window-use-time window))
928 (if (or (eq window (selected-window))
929 (not (window-full-width-p window)))
930 (when (or (not second-best-time) (< time second-best-time))
931 (setq second-best-time time)
932 (setq second-best-window window))
933 (when (or (not best-time) (< time best-time))
934 (setq best-time time)
935 (setq best-window window)))))
936 (or best-window second-best-window)))
938 (defun get-mru-window (&optional all-frames)
939 "Return the least recently used window on frames specified by ALL-FRAMES.
940 Do not return a minibuffer window.
942 The following non-nil values of the optional argument ALL-FRAMES
943 have special meanings:
945 - t means consider all windows on all existing frames.
947 - `visible' means consider all windows on all visible frames.
949 - 0 (the number zero) means consider all windows on all visible
950 and iconified frames.
952 - A frame means consider all windows on that frame only.
954 Any other value of ALL-FRAMES means consider all windows on the
955 selected frame and no others."
956 (let (best-window best-time time)
957 (dolist (window (window-list-1 nil nil all-frames))
958 (setq time (window-use-time window))
959 (when (or (not best-time) (> time best-time))
960 (setq best-time time)
961 (setq best-window window)))
962 best-window))
964 (defun get-largest-window (&optional all-frames dedicated)
965 "Return the largest window on frames specified by ALL-FRAMES.
966 A minibuffer window is never a candidate. A dedicated window is
967 never a candidate unless DEDICATED is non-nil, so if all windows
968 are dedicated, the value is nil.
970 The following non-nil values of the optional argument ALL-FRAMES
971 have special meanings:
973 - t means consider all windows on all existing frames.
975 - `visible' means consider all windows on all visible frames.
977 - 0 (the number zero) means consider all windows on all visible
978 and iconified frames.
980 - A frame means consider all windows on that frame only.
982 Any other value of ALL-FRAMES means consider all windows on the
983 selected frame and no others."
984 (let ((best-size 0)
985 best-window size)
986 (dolist (window (window-list-1 nil nil all-frames))
987 (when (or dedicated (not (window-dedicated-p window)))
988 (setq size (* (window-total-size window)
989 (window-total-size window t)))
990 (when (> size best-size)
991 (setq best-size size)
992 (setq best-window window))))
993 best-window))
995 ;; The following is what `get-buffer-window' would look like if it were
996 ;; implemented in Elisp. Since this function is needed for dumping,
997 ;; leave it in C.
999 ;; (defun get-buffer-window (&optional buffer-or-name all-frames)
1000 ;; "Return a window currently displaying BUFFER-OR-NAME, or nil if none.
1001 ;; BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
1002 ;; the current buffer.
1004 ;; The following non-nil values of the optional argument ALL-FRAMES
1005 ;; have special meanings:
1006 ;; - t means consider all windows on all existing frames.
1007 ;; - `visible' means consider all windows on all visible frames.
1008 ;; - 0 (the number zero) means consider all windows on all visible
1009 ;; and iconified frames.
1010 ;; - A frame means consider all windows on that frame only.
1011 ;; Any other value of ALL-FRAMES means consider all windows on the
1012 ;; selected frame and no others."
1013 ;; (let ((buffer (get-buffer buffer-or-name))
1014 ;; best-window)
1015 ;; (when (bufferp buffer)
1016 ;; (dolist (window (window-list-1 nil t all-frames))
1017 ;; (when (and (eq (window-buffer window) buffer)
1018 ;; ;; The following SHOULD have been handled by
1019 ;; ;; `window-list-1' already ...
1020 ;; (or (not (window-minibuffer-p window))
1021 ;; ;; Don't find any minibuffer window except the
1022 ;; ;; one that is currently in use.
1023 ;; (eq window (minibuffer-window)))
1024 ;; (or (not best-window)
1025 ;; ;; Prefer to return selected window.
1026 ;; (eq window (selected-window))
1027 ;; ;; Prefer windows on selected frame.
1028 ;; (eq (window-frame window) (selected-frame))))
1029 ;; (setq best-window window))))
1030 ;; best-window))
1032 (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
1033 "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
1034 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
1035 and defaults to the current buffer.
1037 Any windows showing BUFFER-OR-NAME on the selected frame are listed
1038 first.
1040 MINIBUF t means include the minibuffer window even if the
1041 minibuffer is not active. MINIBUF nil or omitted means include
1042 the minibuffer window only if the minibuffer is active. Any
1043 other value means do not include the minibuffer window even if
1044 the minibuffer is active.
1046 ALL-FRAMES nil or omitted means consider all windows on WINDOW's
1047 frame, plus the minibuffer window if specified by the MINIBUF
1048 argument. If the minibuffer counts, consider all windows on all
1049 frames that share that minibuffer too. The following non-nil
1050 values of ALL-FRAMES have special meanings:
1052 - t means consider all windows on all existing frames.
1054 - `visible' means consider all windows on all visible frames.
1056 - 0 (the number zero) means consider all windows on all visible
1057 and iconified frames.
1059 - A frame means consider all windows on that frame only.
1061 Anything else means consider all windows on WINDOW's frame and no
1062 others."
1063 (let ((buffer (normalize-live-buffer buffer-or-name))
1064 windows)
1065 (dolist (window (window-list-1 (frame-first-window) minibuf all-frames))
1066 (when (eq (window-buffer window) buffer)
1067 (setq windows (cons window windows))))
1068 (nreverse windows)))
1070 (defun minibuffer-window-active-p (window)
1071 "Return t if WINDOW is the currently active minibuffer window."
1072 (eq window (active-minibuffer-window)))
1074 (defun count-windows (&optional minibuf)
1075 "Return the number of live windows on the selected frame.
1076 The optional argument MINIBUF specifies whether the minibuffer
1077 window shall be counted. See `walk-windows' for the precise
1078 meaning of this argument."
1079 (length (window-list-1 nil minibuf)))
1081 ;;; Resizing windows.
1082 (defun resize-window-reset (&optional frame horizontal)
1083 "Reset resize values for all windows on FRAME.
1084 FRAME defaults to the selected frame.
1086 This function stores the current value of `window-total-size' applied
1087 with argument HORIZONTAL in the new total size of all windows on
1088 FRAME. It also resets the new normal size of each of these
1089 windows."
1090 (resize-window-reset-1
1091 (frame-root-window (normalize-live-frame frame)) horizontal))
1093 (defun resize-window-reset-1 (window horizontal)
1094 "Internal function of `resize-window-reset'."
1095 ;; Register old size in the new total size.
1096 (resize-window-total window (window-total-size window horizontal))
1097 ;; Reset new normal size.
1098 (resize-window-normal window)
1099 (when (window-child window)
1100 (resize-window-reset-1 (window-child window) horizontal))
1101 (when (window-right window)
1102 (resize-window-reset-1 (window-right window) horizontal)))
1104 (defvar resize-window-safe-window nil
1105 "Internal variable bound by `resize-window'.")
1107 (defun resize-window (window delta &optional horizontal ignore)
1108 "Resize WINDOW vertically by DELTA lines.
1109 WINDOW can be an arbitrary window and defaults to the selected
1110 one. An attempt to resize the root window of a frame will raise
1111 an error though.
1113 DELTA a positive number means WINDOW shall be enlarged by DELTA
1114 lines. DELTA negative means WINDOW shall be shrunk by -DELTA
1115 lines.
1117 Optional argument HORIZONTAL non-nil means resize WINDOW
1118 horizontally by DELTA columns. In this case a positive DELTA
1119 means enlarge WINDOW by DELTA columns. DELTA negative means
1120 WINDOW shall be shrunk by -DELTA columns.
1122 Optional argument IGNORE non-nil means ignore any restrictions
1123 imposed by fixed size windows, `window-min-height' or
1124 `window-min-width' settings. IGNORE any window means ignore
1125 restrictions for that window only. IGNORE equal `safe' means
1126 live windows may get as small as `window-safe-min-height' lines
1127 and `window-safe-min-width' columns.
1129 This function resizes other windows proportionally and never
1130 deletes any windows. If you want to move only the low (right)
1131 edge of WINDOW consider using `adjust-window-trailing-edge'
1132 instead."
1133 (setq window (normalize-any-window window))
1134 (let* ((frame (window-frame window))
1135 right)
1136 (cond
1137 ((eq window (frame-root-window frame))
1138 (error "Cannot resize root window of frame"))
1139 ((window-resizable-p window delta horizontal ignore)
1140 (resize-window-reset frame horizontal)
1141 (resize-this-window window delta horizontal ignore t)
1142 (if (and (not (eq window-splits 'resize))
1143 (window-iso-combined-p window horizontal)
1144 (setq right (window-right window))
1145 (or (window-size-ignore window ignore)
1146 (not (window-size-fixed-p right)))
1147 (or (< delta 0)
1148 (> (- (window-total-size right horizontal)
1149 (window-min-size right horizontal))
1150 delta)))
1151 ;; Resize window below/on the right of WINDOW - this is the
1152 ;; classic Emacs behavior, so retain it for `window-splits'
1153 ;; not 'resize, iso-combined windows. It's a PITA, though.
1154 (let ((parent-size
1155 (window-total-size (window-parent window) horizontal)))
1156 (resize-this-window right (- delta) horizontal nil t)
1157 (resize-window-normal
1158 window (/ (float (window-new-total-size window)) parent-size))
1159 (resize-window-normal
1160 right (/ (float (window-new-total-size right)) parent-size)))
1161 (resize-other-windows window delta horizontal ignore))
1162 (resize-window-apply frame horizontal))
1164 (error "Cannot resize window %s" window)))))
1166 (defsubst resize-subwindows-skip-p (window)
1167 "Return non-nil if WINDOW shall be skipped by resizing routines."
1168 (memq (window-new-normal-size window) '(ignore stuck skip)))
1170 (defun resize-subwindows-normal (parent horizontal window delta side)
1171 "Set new normal height of all subwindows of window PARENT.
1172 HORIZONTAL non-nil means set normal width of these windows.
1173 WINDOW has to specify a subwindow of PARENT that has been resized
1174 by DELTA lines \(columns). SIDE non-nil means set values for
1175 windows on the specified side of WINDOW only."
1176 (let* ((parent-new-total (window-new-total-size parent))
1177 (window-new-total
1178 (+ (window-total-size window horizontal) delta))
1179 (window-new-normal
1180 (/ (float window-new-total) parent-new-total))
1181 (others-old-normal
1182 (- 1 (window-normal-size window horizontal)))
1183 (others-new-normal (- 1 window-new-normal))
1184 (sub (window-child parent))
1185 (skip (eq side 'right)))
1187 (when (memq side '(left right))
1188 (while sub
1189 (cond
1190 ((eq sub window)
1191 (setq skip (eq side 'left)))
1192 (skip
1193 (setq others-old-normal
1194 (- others-old-normal
1195 (window-normal-size sub horizontal)))
1196 (setq others-new-normal
1197 (- others-new-normal
1198 (window-normal-size sub horizontal)))))
1199 (setq sub (window-right sub)))
1200 (setq sub (window-child parent))
1201 (setq skip (eq side 'right)))
1203 (setq sub (window-child parent))
1204 (while sub
1205 (cond
1206 ((eq sub window)
1207 (resize-window-normal sub window-new-normal)
1208 (setq skip (eq side 'left)))
1209 (skip)
1211 (resize-window-normal
1212 sub (if (zerop others-old-normal)
1214 (/ (* (window-normal-size sub horizontal)
1215 others-new-normal)
1216 others-old-normal)))))
1217 (setq sub (window-right sub)))))
1219 ;; Calling the following has
1220 ;; 1. SIDE non-nil => WINDOW nil.
1221 ;; 2. WINDOW non-nil => resize PARENT and WINDOW by DELTA.
1222 ;; 3. WINDOW nil => resize PARENT by DELTA.
1223 (defun resize-subwindows (parent delta &optional horizontal ignore side)
1224 "Resize subwindows of window PARENT vertically by DELTA lines.
1225 PARENT must be a vertically combined internal window.
1227 Optional argument HORIZONTAL non-nil means resize subwindows of
1228 PARENT horizontally by DELTA columns. In this case PARENT must
1229 be a horizontally combined internal window.
1231 Optional argument IGNORE non-nil means ignore any restrictions
1232 imposed by fixed size windows, `window-min-height' or
1233 `window-min-width' settings. IGNORE equal `safe' means live
1234 windows may get as small as `window-safe-min-height' lines and
1235 `window-safe-min-width' columns. IGNORE any window means ignore
1236 restrictions for that window only.
1238 Optional argument SIDE `left' means try to resize only the last
1239 subwindow of PARENT provided DELTA is greater zero. SIDE `right'
1240 means try to only resize the first subwindow of PARENT provided
1241 DELTA is greater zero. Any other value of SIDE is ignored."
1242 (let* ((first (window-child parent))
1243 (sub first)
1244 (normal-sum 0.0)
1245 (total-sum delta)
1246 (failed t)
1247 (amount 0)
1248 found sub-total sub-normal sub-int sub-float sub-delta sub-amount
1249 sub-rest best best-rest)
1250 ;; `normal-sum' is the sum of the normal sizes of all resizable
1251 ;; subwindows of PARENT. `total-sum' is the sum of the total
1252 ;; sizes of all resizable subwindows of PARENT plus DELTA.
1253 (catch 'done
1254 (while sub
1255 (unless (or (resize-subwindows-skip-p sub)
1256 (and (not ignore)
1257 ;; Ignore fixed-size subwindows.
1258 (window-size-fixed-p sub horizontal)
1259 (resize-window-normal sub 'ignore)))
1260 (setq normal-sum (+ normal-sum
1261 (window-normal-size sub horizontal)))
1262 (setq total-sum (+ total-sum
1263 (window-total-size sub horizontal)))
1264 ;; `found' non-nil tells that there is at least one subwindow
1265 ;; left that can be resized (should stay `t' now ;-().
1266 (setq found t))
1267 (setq sub (window-right sub)))
1269 ;; When SIDE is non-nil and DELTA is greater zero try to resize
1270 ;; the first subwindow (when SIDE is `right') or the last
1271 ;; subwindow (when SIDE is `left') first. This is the behavior
1272 ;; needed by `adjust-window-trailing-edge' when the edge-adjacent
1273 ;; subwindow the user wants to enlarge is nested in a combination.
1274 (when (and (> delta 0)
1275 ;; Skip a fixed-size window: This is inherently not
1276 ;; TRT because a fixed-size internal window might
1277 ;; still have a resizable subwindow which we could
1278 ;; enlarge. But DTRT here is quite non-trivial :-(
1279 (or (and (eq side 'left)
1280 (progn
1281 (setq sub first)
1282 (while (window-right sub)
1283 (setq sub (window-right sub)))
1284 sub))
1285 (and (eq side 'right) (setq sub first)))
1286 (not (resize-subwindows-skip-p sub)))
1287 ;; DELTA > 0 guarantees that resizing SUB always succeeds.
1288 (resize-this-window sub delta horizontal ignore t side)
1289 ;; Assign new normal sizes.
1290 (resize-subwindows-normal parent horizontal sub delta side)
1291 (throw 'done 0))
1293 ;; We resize subwindows in "rounds". We assume that usually a
1294 ;; resize request succeeds in the first round. If it fails -
1295 ;; which means at least one subwindow cannot be resized as desired
1296 ;; - we need another round. Failures are recorded in the variable
1297 ;; `failed' and, for the failed subwindow, by setting that
1298 ;; window's new normal size to a negative value.
1300 ;; Note that in each round we record (via `resize-window-total')
1301 ;; only the amount by which the window shall be resized. Only
1302 ;; when we know how each inidvidual subwindow shall be resized
1303 ;; (that is after the final round) we add the current size of the
1304 ;; window to the amount recorded previously.
1305 (while (and failed found)
1306 ;; We try to resize each resizable subwindow `sub' by a value
1307 ;; `sub-delta' individually calculated for `sub'. `sub-amount'
1308 ;; specifies the actual amount `sub' can be resized to in the
1309 ;; present round. `amount' represents the sum of the
1310 ;; `sub-amount' for all subwindows we are able to resize in the
1311 ;; present round. `delta' is de-/increased by the sum of
1312 ;; `sub-amount' for all subwindows we we're not able to resize
1313 ;; completely in the present round. So `amount' and `delta'
1314 ;; grow/shrink towards each other and we are done when the have
1315 ;; the same value. `sub-rest' is the remainder when calculating
1316 ;; `sub-delta' and is used when calculating the new normal
1317 ;; sizes.
1318 (setq amount 0)
1319 (setq found nil)
1320 (setq failed nil)
1321 (setq sub first)
1322 ;; The following loop represents one round.
1323 (while (and sub (not failed))
1324 ;; Ignore subwindows that should be ignored or are stuck.
1325 (unless (resize-subwindows-skip-p sub)
1326 ;; Set `found' to t to make sure that if this round fails we
1327 ;; make another round.
1328 (setq found t)
1329 ;; `sub-total' records the total size of this subwindow.
1330 (setq sub-total (window-total-size sub horizontal))
1331 ;; `sub-normal' records the normal of this subwindow.
1332 (setq sub-normal (window-normal-size sub horizontal))
1333 ;; `sub-delta' records the number of lines or columns by
1334 ;; which this subwindow should grow or shrink. `sub-float'
1335 ;; and `sub-int' record the new ideal total size as a float
1336 ;; and integer value.
1337 (setq sub-float (/ (* sub-normal total-sum) normal-sum))
1338 (setq sub-int (floor sub-float))
1339 (setq sub-delta (- sub-int sub-total))
1340 ;; `sub-rest' is the remainder.
1341 (setq sub-rest (abs (- sub-float sub-int)))
1342 (if (and side (< delta 0) (>= sub-delta 0))
1343 ;; With `adjust-window-trailing-edge' some window can
1344 ;; get penalized such that its normal size exceeds its
1345 ;; fractional total size considerably. In that case
1346 ;; dragging a divider in the opposite direction in order
1347 ;; to enlarge some other window may cause this window
1348 ;; get _enlarged_ which looks silly. We try to avoid
1349 ;; such behavior here.
1350 (resize-window-total sub sub-total)
1351 ;; `sub-amount' records the number of lines or columns by
1352 ;; which this subwindow can grow or shrink.
1353 (setq sub-amount
1354 (window-sizable sub sub-delta horizontal ignore))
1355 ;; Register the new total size for this subwindow.
1356 (resize-window-total sub (+ sub-total sub-amount))
1357 (if (= sub-amount sub-delta)
1358 ;; We succeeded to get this subwindow's share.
1359 (progn
1360 (if (and (< delta 0) (zerop sub-amount))
1361 ;; When shrinking avoid that a window that has
1362 ;; not shrunk gets a remainder before a window
1363 ;; that has shrunk.
1364 (resize-window-normal sub 'rest)
1365 ;; Record remainder.
1366 (resize-window-normal sub sub-rest))
1367 (setq amount (+ amount sub-amount)))
1368 ;; We failed and need a new round.
1369 (setq failed t)
1370 ;; Don't consider this subwindow again when calculating
1371 ;; desired sizes.
1372 (setq normal-sum (- normal-sum sub-normal))
1373 (setq total-sum (- total-sum sub-total sub-amount))
1374 (setq delta (- delta sub-amount))
1375 (resize-window-normal sub 'stuck))))
1376 (setq sub (window-right sub))))
1378 ;; Fix rounding by trying to enlarge non-stuck, non-rest windows
1379 ;; by one line (column) until `amount' equals `delta'.
1380 (when found
1381 (catch 'found
1382 (while (< amount delta)
1383 (setq sub first)
1384 (setq best nil)
1385 (setq best-rest 0)
1386 (while sub
1387 (setq sub-normal (window-new-normal-size sub))
1388 (when (and (numberp sub-normal) (>= sub-normal best-rest))
1389 (setq best sub)
1390 (setq best-rest sub-normal)
1391 (setq found t))
1392 (setq sub (window-right sub)))
1393 (if (not best)
1394 (throw 'found nil)
1395 (resize-window-total best 1 'add)
1396 (resize-window-normal best (max 0 (1- best-rest)))
1397 (setq amount (1+ amount))))))
1399 ;; Fix rounding by trying to enlarge "rest" windows by one line
1400 ;; (column) until `amount' equals `delta'.
1401 (catch 'found
1402 (while (< amount delta)
1403 (setq sub first)
1404 (setq best nil)
1405 (when (eq (window-new-normal-size sub) 'rest)
1406 (setq best t)
1407 (resize-window-total sub 1 'add)
1408 (setq amount (1+ amount))
1409 (setq sub (window-right sub)))
1410 (unless best
1411 (throw 'found nil))))
1413 ;; Fix rounding by trying to enlarge stuck windows by one line
1414 ;; (column) until `amount' equals `delta'.
1415 (catch 'found
1416 (while (< amount delta)
1417 (setq sub first)
1418 (setq best nil)
1419 (when (eq (window-new-normal-size sub) 'stuck)
1420 (setq best t)
1421 (resize-window-total sub 1 'add)
1422 (setq amount (1+ amount))
1423 (setq sub (window-right sub)))
1424 (unless best
1425 (throw 'found nil))))
1427 ;; Reset new normal size fields so `resize-window-apply' won't use
1428 ;; them to apply new sizes.
1429 (setq sub first)
1430 (while sub
1431 (when (numberp (window-new-normal-size sub))
1432 (resize-window-normal sub))
1433 (setq sub (window-right sub)))
1435 ;; Now recursively resize each resized subwindow's subwindows.
1436 (setq sub first)
1437 (while sub
1438 (unless (eq (window-new-normal-size sub) 'ignore)
1439 ;; Resize this subwindow's subwindows. Note that above we
1440 ;; recorded (via `resize-window-total') only the amount by
1441 ;; which this subwindow had to be resized. Now we add the old
1442 ;; total size.
1443 (let ((delta (- (window-new-total-size sub)
1444 (window-total-size sub horizontal))))
1445 (unless (and (zerop delta) (not side))
1446 (resize-this-window sub delta horizontal ignore nil side))))
1447 (setq sub (window-right sub))))))
1449 (defun resize-other-windows (window delta &optional horizontal ignore side)
1450 "Resize other windows when WINDOW is resized vertically by DELTA lines.
1451 Optional argument HORIZONTAL non-nil means resize other windows
1452 when WINDOW is resized horizontally by DELTA columns. WINDOW
1453 itself is not resized by this function.
1455 Optional argument IGNORE non-nil means ignore any restrictions
1456 imposed by fixed size windows, `window-min-height' or
1457 `window-min-width' settings. IGNORE equal `safe' means live
1458 windows may get as small as `window-safe-min-height' lines and
1459 `window-safe-min-width' columns. IGNORE any window means ignore
1460 restrictions for that window only.
1462 Optional argument SIDE `left' means resize other windows above
1463 \(on left of) WINDOW only. SIDE `right' means resize other
1464 windows below \(on right of) WINDOW only. Any other value of
1465 SIDE is ignored."
1466 (when (window-parent window)
1467 (let* ((parent (window-parent window))
1468 (sub (window-child parent))
1469 non-fixed)
1470 (if (window-iso-combined-p sub horizontal)
1471 ;; In an iso-combination resize WINDOW's siblings.
1472 (let ((first sub)
1473 (skip (eq side 'right))
1474 this-delta)
1475 ;; Decide which windows shall be left alone.
1476 (while sub
1477 (cond
1478 ((eq sub window)
1479 ;; Make sure WINDOW is left alone when
1480 ;; resizing its siblings.
1481 (resize-window-normal sub 'ignore)
1482 (setq skip (eq side 'left)))
1483 (skip
1484 ;; Make sure this sibling is left alone when
1485 ;; resizing its siblings.
1486 (resize-window-normal sub 'ignore))
1487 ((or (window-size-ignore sub ignore)
1488 (not (window-size-fixed-p sub horizontal)))
1489 (setq non-fixed t)))
1490 (setq sub (window-right sub)))
1491 (if (= (- delta) (window-total-size window horizontal))
1492 ;; A deletion, presumably.
1493 (if non-fixed
1494 ;; There's at least on resizable sibling.
1495 (setq this-delta delta)
1496 ;; No resizable sibling present.
1497 (setq this-delta 0))
1498 (setq this-delta
1499 (window-resizable
1500 window delta horizontal ignore side t)))
1501 (unless (= delta this-delta)
1502 (resize-window-total parent (- delta this-delta) 'add))
1503 (unless (zerop this-delta)
1504 (resize-window-normal window 'ignore)
1505 (resize-subwindows
1506 parent (- this-delta) horizontal ignore side)
1507 ;; Now set the normal sizes.
1508 (resize-subwindows-normal
1509 parent horizontal window this-delta side)
1510 (setq delta (- delta this-delta))))
1512 ;; In an ortho-combination all siblings of WINDOW must be
1513 ;; resized by DELTA. Store the new total size of parent first.
1514 (resize-window-total parent delta 'add)
1515 (while sub
1516 (unless (eq sub window)
1517 (resize-this-window sub delta horizontal ignore t))
1518 (setq sub (window-right sub))))
1520 (unless (zerop delta)
1521 ;; "Go up."
1522 (resize-other-windows parent delta horizontal ignore side)))))
1524 (defun resize-this-window (window delta &optional horizontal ignore add-total side)
1525 "Resize WINDOW vertically by DELTA lines.
1526 Optional argument HORIZONTAL non-nil means resize WINDOW
1527 horizontally by DELTA columns.
1529 Optional argument IGNORE non-nil means ignore any restrictions
1530 imposed by fixed size windows, `window-min-height' or
1531 `window-min-width' settings. IGNORE equal `safe' means live
1532 windows may get as small as `window-safe-min-height' lines and
1533 `window-safe-min-width' columns. IGNORE any window means ignore
1534 restrictions for that window only.
1536 Optional argument ADD-TOTAL non-nil means add DELTA to the new
1537 total size of WINDOW.
1539 Optional argument SIDE `left' means resize other windows above
1540 \(on left of) WINDOW only. SIDE `right' means resize other
1541 windows below \(on right of) WINDOW only. Any other value of
1542 SIDE is ignored.
1544 This function recursively resizes WINDOW's subwindows to fit the
1545 new size. Make sure that WINDOW is `window-resizable' before
1546 calling this function. Note that this function does not resize
1547 siblings of WINDOW or WINDOW's parent window. You have to
1548 eventually call `resize-window-apply' in order to make resizing
1549 actually take effect."
1550 (when add-total
1551 ;; Add DELTA to the new total size of WINDOW.
1552 (resize-window-total window delta t))
1554 (let ((sub (window-child window)))
1555 (cond
1556 ((not sub))
1557 ((window-iso-combined-p sub horizontal)
1558 ;; In an iso-combination resize subwindows according to their
1559 ;; fractions.
1560 (resize-subwindows window delta horizontal ignore side))
1561 ;; In an ortho-combination resize each subwindow by DELTA.
1563 (while sub
1564 (resize-this-window sub delta horizontal ignore t side)
1565 (setq sub (window-right sub)))))))
1567 (defun resize-root-window (window delta horizontal ignore)
1568 "Resize root window WINDOW vertically by DELTA lines.
1569 HORIZONTAL non-nil means resize root window WINDOW horizontally
1570 by DELTA columns.
1572 IGNORE non-nil means ignore any restrictions imposed by fixed
1573 size windows, `window-min-height' or `window-min-width' settings.
1575 This function is called by Emacs' frame resizing routines. It
1576 resizes windows proportionally and never deletes any windows."
1577 (when (and (windowp window) (numberp delta)
1578 (window-sizable-p window delta horizontal ignore))
1579 (resize-window-reset (window-frame window) horizontal)
1580 (resize-this-window window delta horizontal ignore t)))
1582 (defun resize-root-window-vertically (window delta)
1583 "Resize root window WINDOW vertically by DELTA lines.
1584 If DELTA is less than zero and we can't shrink WINDOW by DELTA
1585 lines, shrink it as much as possible. If DELTA is greater than
1586 zero, this function can resize fixed-size subwindows in order to
1587 recover the necessary lines.
1589 Return the number of lines that were recovered.
1591 This function is called by Emacs' minibuffer resizing routines.
1592 It resizes windows proportionally and never deletes any windows."
1593 (when (numberp delta)
1594 (let (ignore)
1595 (cond
1596 ((< delta 0)
1597 (setq delta (window-sizable window delta)))
1598 ((> delta 0)
1599 (unless (window-sizable window delta)
1600 (setq ignore t))))
1601 (resize-window-reset (window-frame window))
1602 (resize-this-window window delta nil ignore t)
1603 delta)))
1605 (defun adjust-window-trailing-edge (window delta &optional horizontal)
1606 "Move WINDOW's bottom edge by DELTA lines.
1607 Optional argument HORIZONTAL non-nil means move WINDOW's right
1608 edge by DELTA columns. WINDOW defaults to the selected window.
1610 If the edge can't be moved by DELTA lines, move it as far as
1611 possible in the desired direction."
1612 (setq window (normalize-any-window window))
1613 (let ((frame (window-frame window))
1614 (right window)
1615 left this-delta min-delta max-delta failed)
1616 ;; Find the edge we want to move.
1617 (while (and (or (not (window-iso-combined-p right horizontal))
1618 (not (window-right right)))
1619 (setq right (window-parent right))))
1620 (unless (and (setq left right) (setq right (window-right right)))
1621 (error "No window following this one"))
1623 ;; Set LEFT to the first resizable window on the left. This step is
1624 ;; needed to handle fixed-size windows.
1625 (while (and left (window-size-fixed-p left horizontal))
1626 (setq left
1627 (or (window-left left)
1628 (progn
1629 (while (and (setq left (window-parent left))
1630 (not (window-iso-combined-p left horizontal))))
1631 (window-left left)))))
1632 (unless left
1633 (error "No resizable window preceding this one"))
1635 ;; Set RIGHT to the first resizable window on the right. This step
1636 ;; is needed to handle fixed-size windows.
1637 (while (and right (window-size-fixed-p right horizontal))
1638 (setq right
1639 (or (window-right right)
1640 (progn
1641 (while (and (setq right (window-parent right))
1642 (not (window-iso-combined-p right horizontal))))
1643 (window-right right)))))
1644 (unless right
1645 (error "No resizable window following this one"))
1647 ;; LEFT and RIGHT (which might be both internal windows) are now the
1648 ;; two windows we want to resize.
1649 (cond
1650 ((> delta 0)
1651 (setq max-delta (window-max-delta-1 left 0 horizontal nil 'right))
1652 (setq min-delta (window-min-delta-1 right (- delta) horizontal nil 'left))
1653 (when (or (< max-delta delta) (> min-delta (- delta)))
1654 ;; We can't get the whole DELTA - move as far as possible.
1655 (setq delta (min max-delta (- min-delta))))
1656 (unless (zerop delta)
1657 ;; Start resizing.
1658 (resize-window-reset frame horizontal)
1659 ;; Try to enlarge LEFT first.
1660 (setq this-delta (window-resizable left delta horizontal))
1661 (unless (zerop this-delta)
1662 (resize-this-window left this-delta horizontal nil t 'left))
1663 (unless (= this-delta delta)
1664 ;; We didn't get it all from LEFT, enlarge windows on left of
1665 ;; LEFT (for this purpose make `resize-other-windows' believe
1666 ;; that we shrink LEFT).
1667 (resize-other-windows
1668 left (- this-delta delta) horizontal nil 'left))
1669 ;; Shrink windows on right of LEFT.
1670 (resize-other-windows left delta horizontal nil 'right)))
1671 ((< delta 0)
1672 (setq max-delta (window-max-delta-1 right 0 horizontal nil 'left))
1673 (setq min-delta (window-min-delta-1 left delta horizontal nil 'right))
1674 (when (or (< max-delta (- delta)) (> min-delta delta))
1675 ;; We can't get the whole DELTA - move as far as possible.
1676 (setq delta (max (- max-delta) min-delta)))
1677 (unless (zerop delta)
1678 ;; Start resizing.
1679 (resize-window-reset frame horizontal)
1680 ;; Try to enlarge RIGHT.
1681 (setq this-delta (window-resizable right (- delta) horizontal))
1682 (unless (zerop this-delta)
1683 (resize-this-window right this-delta horizontal nil t 'right))
1684 (unless (= (- this-delta) delta)
1685 ;; We didn't get it all from RIGHT, enlarge windows on right of
1686 ;; RIGHT (for this purpose make `resize-other-windows' believe
1687 ;; that we grow RIGHT).
1688 (resize-other-windows
1689 right (- this-delta delta) horizontal nil 'right))
1690 ;; Shrink windows on left of RIGHT.
1691 (resize-other-windows right (- delta) horizontal nil 'left))))
1692 (unless (zerop delta)
1693 ;; Don't report an error in the standard case.
1694 (unless (resize-window-apply frame horizontal)
1695 ;; But do report an error it applying the changes fails.
1696 (error "Failed adjusting window %s" window)))))
1698 (defun resize-mini-window (window delta)
1699 "Resize minibuffer window WINDOW by DELTA lines."
1700 (when (window-minibuffer-p window)
1701 (let* ((frame (window-frame window))
1702 (root (frame-root-window frame))
1703 (height (window-total-size window)))
1704 (unless (> (- height delta) 0)
1705 (setq delta (- height 1)))
1706 (when (window-sizable-p root delta)
1707 (resize-window-reset frame)
1708 (resize-this-window root delta nil nil t)
1709 (resize-window-total window (- height delta))
1710 (resize-mini-window-internal window)))))
1712 (defun enlarge-window (delta &optional horizontal)
1713 "Make selected window DELTA lines taller.
1714 Interactively, if no argument is given, make the selected window
1715 one line taller. If optional argument HORIZONTAL is non-nil,
1716 make selected window wider by DELTA columns. If DELTA is
1717 negative, shrink selected window by -DELTA lines or columns.
1718 Return nil."
1719 (interactive "p")
1720 (resize-window (selected-window) delta horizontal))
1722 (defun shrink-window (delta &optional horizontal)
1723 "Make selected window DELTA lines smaller.
1724 Interactively, if no argument is given, make the selected window
1725 one line smaller. If optional argument HORIZONTAL is non-nil,
1726 make selected window narrower by DELTA columns. If DELTA is
1727 negative, enlarge selected window by -DELTA lines or columns.
1728 Return nil."
1729 (interactive "p")
1730 (resize-window (selected-window) (- delta) horizontal))
1732 (defun maximize-window (&optional window)
1733 "Maximize WINDOW.
1734 Make WINDOW as large as possible without deleting any windows.
1735 WINDOW can be any window and defaults to the selected window."
1736 (interactive)
1737 (setq window (normalize-any-window window))
1738 (resize-window window (window-max-delta window))
1739 (resize-window window (window-max-delta window t) t))
1741 (defun minimize-window (&optional window)
1742 "Minimize WINDOW.
1743 Make WINDOW as small as possible without deleting any windows.
1744 WINDOW can be any window and defaults to the selected window."
1745 (interactive)
1746 (setq window (normalize-any-window window))
1747 (resize-window window (- (window-min-delta window)))
1748 (resize-window window (- (window-min-delta window t)) t))
1750 (defsubst frame-root-window-p (window)
1751 "Return non-nil if WINDOW is the root window of its frame."
1752 (eq window (frame-root-window window)))
1754 (defun window-tree-1 (window &optional next)
1755 "Return window tree rooted at WINDOW.
1756 Optional argument NEXT non-nil means include windows right
1757 siblings in the return value.
1759 See the documentation of `window-tree' for a description of the
1760 return value."
1761 (let (list)
1762 (while window
1763 (setq list
1764 (cons
1765 (cond
1766 ((window-vchild window)
1767 (cons t (cons (window-edges window)
1768 (window-tree-1 (window-vchild window) t))))
1769 ((window-hchild window)
1770 (cons nil (cons (window-edges window)
1771 (window-tree-1 (window-hchild window) t))))
1772 (t window))
1773 list))
1774 (setq window (when next (window-next window))))
1775 (nreverse list)))
1777 (defun window-tree (&optional frame)
1778 "Return the window tree of frame FRAME.
1779 FRAME must be a live frame and defaults to the selected frame.
1780 The return value is a list of the form (ROOT MINI), where ROOT
1781 represents the window tree of the frame's root window, and MINI
1782 is the frame's minibuffer window.
1784 If the root window is not split, ROOT is the root window itself.
1785 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
1786 for a horizontal split, and t for a vertical split. EDGES gives
1787 the combined size and position of the subwindows in the split,
1788 and the rest of the elements are the subwindows in the split.
1789 Each of the subwindows may again be a window or a list
1790 representing a window split, and so on. EDGES is a list \(LEFT
1791 TOP RIGHT BOTTOM) as returned by `window-edges'."
1792 (setq frame (normalize-live-frame frame))
1793 (window-tree-1 (frame-root-window frame) t))
1795 ;;; Composite Windows
1797 ;; The basic invariant of the composite window code is:
1799 ;; \A window \in Windows:
1800 ;; \A sibling \in Siblings [window]:
1801 ;; composite-window-p [window] =>
1802 ;; /\ composite-window-p [sibling]
1803 ;; /\ composite-root-window [window] = composite-root-window [sibling]
1805 ;; that is, for any window that is part of a composite window, any
1806 ;; sibling of that window is a subwindow of the same composite window.
1808 ;; This is usually not called as a "predicate" but it's more consistent
1809 ;; to maintain our defsubsts as predicate.
1810 (defsubst composite-window-p (window)
1811 "Return non-nil if WINDOW is a subwindow of a composite window.
1812 The return value is the value of the `composite' window parameter
1813 of WINDOW."
1814 (window-parameter window 'composite))
1816 (defsubst composite-root-window-p (window)
1817 "Return non-nil if WINDOW is the root of a composite window.
1818 The return value is the type of that composite window, either
1819 `compound' or `group'."
1820 (or (window-parameter window 'compound)
1821 (window-parameter window 'group)))
1823 (defsubst composite-main-window-p (window)
1824 "Return t if WINDOW is a main window of a composite window."
1825 (eq (cdr-safe (composite-window-p window)) 'main))
1827 (defsubst composite-support-window-p (window)
1828 "Return t if WINDOW is a support window of a composite window."
1829 (eq (cdr-safe (composite-window-p window)) 'support))
1831 (defun composite-root-window (window)
1832 "Return root window of the composite window WINDOW is a part of.
1833 Return nil if WINDOW is not part of a composite window or the
1834 path from WINDOW to the root of the composite window is broken."
1835 (let ((type (car-safe (window-parameter window 'composite))))
1836 (when type
1837 (setq window (window-parent window))
1838 (catch 'done
1839 (while window
1840 (cond
1841 ((window-parameter window type)
1842 (throw 'done window))
1843 ((eq (car-safe (window-parameter window 'composite)) type))
1845 ;; Broken path.
1846 (throw 'done nil)))
1847 (setq window (window-parent window)))))))
1849 (defun composite-major-window (window)
1850 "Return major window of composite window WINDOW belongs to.
1851 The major window is the last main or root window found by
1852 following the path from WINDOW to the root of the composite
1853 window WINDOW belongs to. Each composite window should have one
1854 and only one major window to make sure that functions on its
1855 component windows behave \"as intended\".
1857 This function returns a meaningful result if and only if WINDOW
1858 is a main window."
1859 (let ((main window))
1860 (setq window (window-parent window))
1861 (while (and window (composite-main-window-p window)
1862 (not (composite-root-window-p window)))
1863 (setq main window)
1864 (setq window (window-parent window)))
1865 ;; We can't go up any further but maybe the window we're looking at
1866 ;; is the root window.
1867 (when (composite-root-window-p window)
1868 (let ((sibling (window-child window)))
1869 ;; Make sure that all children of the group root window are main
1870 ;; windows.
1871 (catch 'done
1872 (while sibling
1873 (if (not (composite-main-window-p sibling))
1874 (throw 'done nil)
1875 (setq sibling (window-right sibling))))
1876 (setq main window))))
1877 main))
1879 (defun composite-main-sibling (window)
1880 "Return first \"main\" sibling of WINDOW.
1881 A main sibling is a main window of a composite window. Both,
1882 WINDOW and the main sibling must have the same parent window and
1883 thus be part of one and the same composite window. Return nil if
1884 no such window can be found."
1885 (let ((parent (window-parent window))
1886 sibling)
1887 (when parent
1888 (setq sibling (window-child parent))
1889 (catch 'done
1890 (while sibling
1891 (if (and (not (eq sibling window))
1892 (eq (cdr-safe (window-parameter sibling 'composite)) 'main))
1893 (throw 'done sibling)
1894 (setq sibling (window-right sibling))))))))
1896 (defun composite-lowest-child-role (window)
1897 "Return lowest \"non-main\" role among WINDOW's children."
1898 (let ((sibling (window-child window))
1899 (highest 'main)
1900 role)
1901 (catch 'done
1902 (while sibling
1903 (setq role (cdr-safe (window-parameter sibling 'composite)))
1904 (cond
1905 ((eq role t)
1906 (setq highest t))
1907 ((eq role 'support)
1908 (throw 'done 'support)))
1909 (setq sibling (window-right sibling)))
1910 highest)))
1912 (defsubst compound-window-p (window)
1913 "Return non-nil if WINDOW is a subwindow of a compound window."
1914 (eq (car-safe (window-parameter window 'composite)) 'compound))
1916 (defsubst compound-main-window-p (window)
1917 "Return non-nil if WINDOW is a main window of a compound window."
1918 (let ((composite (composite-window-p window)))
1919 (and (eq (car-safe composite) 'compound)
1920 (eq (cdr-safe composite) 'main))))
1922 (defun compound-root-window (window)
1923 "Return topmost root window of compound window WINDOW belongs to."
1924 (while (and window (compound-window-p window))
1925 (setq window (window-parent window)))
1926 (when (window-parameter window 'compound)
1927 window))
1929 (defsubst group-window-p (window)
1930 "Return non-nil if WINDOW is a subwindow of a window group."
1931 (eq (car-safe (window-parameter window 'composite)) 'group))
1933 (defsubst group-window-main-p (window)
1934 "Return non-nil if WINDOW is a main window of a window group."
1935 (let ((composite (composite-window-p window)))
1936 (and (eq (car-safe composite) 'group)
1937 (eq (cdr-safe composite) 'main))))
1939 (defun group-root-window (window)
1940 "Return root window of window group WINDOW belongs to.
1941 If WINDOW is part of a compound window, return the root window of
1942 the group the root of the compound window belongs too."
1943 (while (and window (compound-window-p window))
1944 (setq window (window-parent window)))
1945 (while (and window (group-window-p window)
1946 (not (composite-root-window-p window)))
1947 (setq window (window-parent window)))
1948 (when (window-parameter window 'group)
1949 window))
1951 ;;; Getting the "other" window.
1952 ;; FIXME: Handle `ignore-window-parameters' and some other things maybe.
1953 (defun other-window (count &optional all-frames)
1954 "Select another window in cyclic ordering of windows.
1955 COUNT specifies the number of windows to skip, starting with the
1956 selected window, before making the selection. If COUNT is
1957 positive, skip COUNT windows forwards. If COUNT is negative,
1958 skip -COUNT windows backwards. COUNT zero means do not skip any
1959 window, so select the selected window. In an interactive call,
1960 COUNT is the numeric prefix argument. Return nil.
1962 This function does not select a window whose `no-other-window'
1963 parameter is non-nil. Also, this function never selects the
1964 support window of a composite window unless the support window's
1965 `maybe-other-window' parameter is non-nil.
1967 This function uses `next-window' for finding the window to
1968 select. The argument ALL-FRAMES has the same meaning as in
1969 `next-window', but the MINIBUF argument of `next-window' is
1970 always effectively nil."
1971 (interactive "p")
1972 (let* ((window (selected-window))
1973 (function (window-parameter window 'other-window))
1974 old-window old-count)
1975 (if (functionp function)
1976 (funcall function count all-frames)
1977 ;; `next-window' and `previous-window' may return a window we are
1978 ;; not allowed to select. Hence we need an exit strategy in case
1979 ;; all windows are non-selectable.
1980 (catch 'exit
1981 (while (> count 0)
1982 (setq window (next-window window nil all-frames))
1983 (cond
1984 ((eq window old-window)
1985 (when (= count old-count)
1986 ;; Keep out of infinite loops. When COUNT has not changed
1987 ;; since we last looked at `window' we're probably in one.
1988 (throw 'exit nil)))
1989 ((or (and (composite-support-window-p window)
1990 (not (window-parameter window 'maybe-other-window)))
1991 (window-parameter window 'no-other-window))
1992 ;; The first non-selectable window `next-window' got us:
1993 ;; Remember it and the current value of COUNT.
1994 (unless old-window
1995 (setq old-window window)
1996 (setq old-count count)))
1998 (setq count (1- count)))))
1999 (while (< count 0)
2000 (setq window (previous-window window nil all-frames))
2001 (cond
2002 ((eq window old-window)
2003 (when (= count old-count)
2004 ;; Keep out of infinite loops. When COUNT has not changed
2005 ;; since we last looked at `window' we're probably in one.
2006 (throw 'exit nil)))
2007 ((or (and (composite-support-window-p window)
2008 (not (window-parameter window 'maybe-other-window)))
2009 (window-parameter window 'no-other-window))
2010 ;; The first non-selectable window `previous-window' got us:
2011 ;; Remember it and the current value of COUNT.
2012 (unless old-window
2013 (setq old-window window)
2014 (setq old-count count)))
2016 (setq count (1+ count)))))
2017 (select-window window)
2018 nil))))
2020 ;; This should probably return non-nil when the selected window is part
2021 ;; of a compound window whose root is the frame's root window.
2022 (defun one-window-p (&optional nomini all-frames)
2023 "Return non-nil if the selected window is the only window.
2024 Optional arg NOMINI non-nil means don't count the minibuffer
2025 even if it is active. Otherwise, the minibuffer is counted
2026 when it is active.
2028 Optional argument ALL-FRAMES specifies the set of frames to
2029 consider, see also `next-window'. ALL-FRAMES nil or omitted
2030 means consider windows on WINDOW's frame only, plus the
2031 minibuffer window if specified by the NOMINI argument. If the
2032 minibuffer counts, consider all windows on all frames that share
2033 that minibuffer too. The remaining non-nil values of ALL-FRAMES
2034 with a special meaning are:
2036 - t means consider all windows on all existing frames.
2038 - `visible' means consider all windows on all visible frames.
2040 - 0 (the number zero) means consider all windows on all visible
2041 and iconified frames.
2043 - A frame means consider all windows on that frame only.
2045 Anything else means consider all windows on WINDOW's frame and no
2046 others."
2047 (let ((base-window (selected-window)))
2048 (if (and nomini (eq base-window (minibuffer-window)))
2049 (setq base-window (next-window base-window)))
2050 (eq base-window
2051 (next-window base-window (if nomini 'arg) all-frames))))
2053 ;;; Deleting windows.
2054 (defun window-deletable-p (&optional window)
2055 "Return t if WINDOW can be safely deleted from its frame.
2056 Return `frame' if deleting WINDOW should delete its frame
2057 instead."
2058 (setq window (normalize-any-window window))
2059 (let ((frame (window-frame window))
2060 (dedicated (and (window-buffer window) (window-dedicated-p window)))
2061 (quit-restore (car-safe (window-parameter window 'quit-restore)))
2062 composite type role root)
2063 (cond
2064 ((frame-root-window-p window)
2065 (when (and (or dedicated
2066 (and (eq quit-restore t)
2067 (with-current-buffer (window-buffer window)
2068 ;; `view-remove-frame-by-deleting' and
2069 ;; `view-mode' are autoloaded.
2070 (or (not view-mode)
2071 view-remove-frame-by-deleting))))
2072 (other-visible-frames-p frame))
2073 ;; WINDOW is the root window of its frame. Return `frame' but
2074 ;; only if WINDOW is (1) either dedicated or quit-restore's car
2075 ;; is t and (2) there are other frames left.
2076 'frame))
2077 ((setq composite (window-parameter window 'composite))
2078 (setq type (car-safe composite))
2079 (setq role (cdr-safe composite))
2080 (setq root (composite-root-window window))
2081 (cond
2082 ;; When `ignore-window-parameters' or the `delete-window'
2083 ;; parameter say or WINDOW is part of a broken composite window,
2084 ;; WINDOW is deletable. We cannot handle the case where WINDOW's
2085 ;; `delete-window' parameter is a function (that's impossible).
2086 ((or (not (memq ignore-window-parameters '(nil post)))
2087 (eq (window-parameter window 'delete-window) t)
2088 (not root) (not type) (not role))
2090 ((eq type 'compound)
2091 ;; A component of a compound window is deletable if and only if
2092 ;; its root is deletable.
2093 (window-deletable-p root))
2094 ((eq type 'group)
2095 ;; In a window group only a main window with a main sibling is
2096 ;; deletable.
2097 (and (eq role 'main) (composite-main-sibling window)))))
2098 (t))))
2100 (defun window-or-subwindow-p (subwindow window)
2101 "Return t if SUBWINDOW is either WINDOW or a subwindow of WINDOW."
2102 (or (eq subwindow window)
2103 (let ((parent (window-parent subwindow)))
2104 (catch 'done
2105 (while parent
2106 (if (eq parent window)
2107 (throw 'done t)
2108 (setq parent (window-parent parent))))))))
2110 (defun delete-window (&optional window)
2111 "Delete WINDOW.
2112 WINDOW can be an arbitrary window and defaults to the selected
2113 one. Return nil.
2115 This function respects the variable `ignore-window-parameters'
2116 when processing window parameters so any processing of WINDOW's
2117 parameters may be suppressed.
2119 If the `delete-window' parameter WINDOW equals t, delete WINDOW
2120 ignoring any other window parameters. If the `delete-window'
2121 parameter specifies a function, call that function with WINDOW as
2122 its sole argument. It's the responsibility of that function to
2123 adjust the parameters of all remaining windows.
2125 Otherwise, if WINDOW is part of a compound window, call this
2126 function with the root of the compound window as its argument.
2127 If WINDOW is either the only window on its frame, or a support
2128 window or the last main window of a window group, signal an error
2129 and don't delete WINDOW.
2131 This function makes sure that window parameters are reset or
2132 inherited when WINDOW is part of a combination of two windows."
2133 (interactive)
2134 (setq window (normalize-any-window window))
2135 (let* ((function (window-parameter window 'delete-window))
2136 ;; COMPOSITE non-nil means WINDOW is part of a composite
2137 ;; window.
2138 (composite (window-parameter window 'composite))
2139 ;; TYPE is the type of the composite window (either `compound'
2140 ;; or `group'). ROLE is the role of WINDOW within the
2141 ;; composite window (either `main', `support', or t). ROOT is
2142 ;; the root window of the composite window.
2143 (type (car-safe composite))
2144 (role (cdr-safe composite))
2145 (root (and composite (composite-root-window window)))
2146 parent)
2147 (catch 'done
2148 ;; Handle window parameters.
2149 (cond
2150 ;; Ignore window parameters if `ignore-window-parameters' tells
2151 ;; so or the `delete-window' parameter equals t.
2152 ((or (not (memq ignore-window-parameters '(nil post)))
2153 (eq function t)))
2154 ((functionp function)
2155 ;; The `delete-window' parameter specifies the function to call
2156 ;; instead. If that function is `ignore' nothing is done. It's
2157 ;; up to the function called here to avoid infinite recursion.
2158 (throw 'done (funcall function window)))
2159 (composite
2160 (cond
2161 ((or (not root) (not type) (not role))
2162 ;; Something is broken in this composite window. Signal a
2163 ;; message but let the deletion pass through (we might signal
2164 ;; an error here but for everday work this is too nasty).
2165 (message "Broken component %s of composite window" window))
2166 ((eq type 'compound)
2167 ;; Deleting a component of a compound window deletes the
2168 ;; entire compound window.
2169 (throw 'done (delete-window root)))
2170 ((eq type 'group)
2171 (cond
2172 ((not (eq role 'main))
2173 ;; WINDOW is _not_ a main window of a window group. There's
2174 ;; no rule for deleting such a window so we signal an error.
2175 ;; We might swallow this error silently.
2176 (error "Cannot delete non-main window of a window group"))
2177 ((not (composite-main-sibling window))
2178 ;; WINDOW has no main sibling and we can't delete the last
2179 ;; main window of a window group. We might swallow this
2180 ;; error silently.
2181 (error "Cannot delete last main window of a window group")))))))
2183 ;; Set PARENT to WINDOW's parent in the window tree. If there's
2184 ;; no such parent signal an error.
2185 (unless (setq parent (window-parent window))
2186 (error "Attempt to delete minibuffer or sole ordinary window"))
2188 (let* ((horizontal (window-hchild parent))
2189 (size (window-total-size window horizontal))
2190 (frame (window-frame window))
2191 (frame-selected
2192 (window-or-subwindow-p (frame-selected-window frame) window))
2193 ;; LEFT is WINDOW's _left_ sibling - traditionally LEFT
2194 ;; gets enlarged and is selected after the deletion.
2195 (left (window-left window))
2196 ;; RIGHT is WINDOW's right sibling.
2197 (right (window-right window))
2198 ;; SIBLING is WINDOW's sibling provided they are the only
2199 ;; child windows of PARENT.
2200 (sibling
2201 (or (and left (not right) (not (window-left left)) left)
2202 (and right (not left) (not (window-right right)) right)))
2203 ;; Record some of PARENT's parameters (just in case we have
2204 ;; WINDOW replace it in the window tree).
2205 (parent-compound
2206 (and sibling (window-parameter parent 'compound)))
2207 (parent-group
2208 (and sibling (window-parameter parent 'group)))
2209 (parent-composite
2210 (and sibling (window-parameter parent 'composite))))
2211 (resize-window-reset frame horizontal)
2212 (cond
2213 ((or (and (eq window-splits 'nest)
2214 (or (and left (not (window-left left))
2215 (not (window-right window)))
2216 (and (not left)
2217 (setq left (window-right window))
2218 (not (window-right left))))
2219 (not (window-size-fixed-p left horizontal)))
2220 (and left (not window-splits)
2221 (not (window-size-fixed-p left horizontal))))
2222 ;; Resize WINDOW's left sibling.
2223 (resize-this-window left size horizontal nil t)
2224 (resize-window-normal
2225 left (+ (window-normal-size left horizontal)
2226 (window-normal-size window horizontal))))
2227 ((let ((sub (window-child parent)))
2228 (catch 'found
2229 ;; Look for a non-fixed-size sibling.
2230 (while sub
2231 (when (and (not (eq sub window))
2232 (not (window-size-fixed-p sub horizontal)))
2233 (throw 'found t))
2234 (setq sub (window-right sub)))))
2235 ;; We can do it without resizing fixed-size windows.
2236 (resize-other-windows window (- size) horizontal))
2238 ;; Can't do without resizing fixed-size windows. We really
2239 ;; should signal an error here but who would agree :-(
2240 (resize-other-windows window (- size) horizontal t)))
2241 ;; Actually delete WINDOW.
2242 (delete-window-internal window)
2243 (when (and frame-selected
2244 (window-parameter
2245 (frame-selected-window frame) 'no-other-window))
2246 ;; `delete-window-internal' has selected a window that should
2247 ;; not be selected, fix this here (I hate `other-window').
2248 (other-window -1 frame))
2249 ;; Handle composite windows (unless we ignore window
2250 ;; parameters).
2251 (when (and (memq ignore-window-parameters '(nil pre))
2252 sibling (not (eq parent (window-parent sibling)))
2253 (or parent-compound parent-group))
2254 ;; At this moment we know that WINDOW and SIBLING are part of
2255 ;; a composite window and the _sole_ child windows of PARENT.
2256 ;; SIBLING replaces PARENT.
2257 (when parent-group
2258 ;; SIBLING becomes the new root of the window group earlier
2259 ;; headed by PARENT. If PARENT was the root of a compound
2260 ;; window that compound window gets dissolved.
2261 (set-window-parameter sibling 'group t))
2262 (if parent-composite
2263 ;; `sibling' inherits composite state of `parent'.
2264 (set-window-parameter sibling 'composite parent-composite)
2265 ;; `sibling' is no longer part of a composite window.
2266 (set-window-parameter sibling 'composite nil)))
2267 (run-window-configuration-change-hook frame)
2268 nil))))
2270 (defun delete-other-windows (&optional window)
2271 "Make WINDOW fill its frame.
2272 WINDOW may be any window and defaults to the selected one.
2274 This function respects the variable `ignore-window-parameters'
2275 when processing window parameters so any processing of WINDOW's
2276 parameters may be suppressed.
2278 If the `delete-other-windows' parameter of WINDOW equals t,
2279 delete WINDOW ignoring any other window parameters. If the
2280 `delete-other-windows' parameter specifies a function, call that
2281 function with WINDOW as its sole argument. It's the
2282 responsibility of that function to adjust the parameters of all
2283 remaining windows.
2285 Otherwise, if WINDOW is part of a compound window, call this
2286 function with the root of the compound window as its argument.
2287 If WINDOW is a main window in a window group, make WINDOW the
2288 only main window in this group. Any support windows of the group
2289 are left alone. If WINDOW is a support window of a window group,
2290 signal an error and don't delete any windows."
2291 (interactive)
2292 (setq window (normalize-any-window window))
2293 (let* ((function (window-parameter window 'delete-other-windows))
2294 (composite (window-parameter window 'composite))
2295 ;; COMPOSITE non-nil means WINDOW is part of a composite
2296 ;; window.
2297 (type (car-safe composite))
2298 (role (cdr-safe composite))
2299 (root (and composite (composite-root-window window)))
2300 ;; TYPE is the type of the composite window (either `compound'
2301 ;; or `group'). ROLE is the role of WINDOW within the
2302 ;; composite window (either `main', `support', or t). ROOT is
2303 ;; the root window of the composite window.
2304 main)
2305 (catch 'done
2306 ;; Handle composite window parameter.
2307 (cond
2308 ;; Ignore window parameters if `ignore-window-parameters' tells
2309 ;; so or the `delete-other-windows' parameter equals t.
2310 ((or (not (memq ignore-window-parameters '(nil post)))
2311 (eq function t)))
2312 ((functionp function)
2313 ;; The `delete-other-windows' parameter specifies the function
2314 ;; to call instead. If the function is `ignore' no windows are
2315 ;; deleted. It's up to the function called to avoid infinite
2316 ;; recursion.
2317 (throw 'done (funcall function window)))
2318 (composite
2319 (cond
2320 ((or (not root) (not type) (not role))
2321 ;; Something is broken in this composite window. Signal a
2322 ;; message but let the deletion pass through (we might signal
2323 ;; an error here but for everday work this is too nasty).
2324 (message "Broken composite window"))
2325 ((eq type 'compound)
2326 ;; In a compound window call `delete-other-windows' with the
2327 ;; root window as its argument.
2328 (throw 'done (delete-other-windows root)))
2329 ((eq type 'group)
2330 (if (eq role 'main)
2331 ;; In a window group we are allowed to delete main windows
2332 ;; only. Moreover we need an ancestor which is the last
2333 ;; main window found when following the path to the group
2334 ;; root window.
2335 (progn
2336 (setq main (composite-major-window window))
2337 (when (or (not main) (eq main window))
2338 ;; If we don't find an ancestor or the ancestor is
2339 ;; WINDOW itself there's nothing we can delete.
2340 ;; Swallow this quietly.
2341 (throw 'done nil))
2342 (when (and (eq main root)
2343 (memq ignore-window-parameters '(nil pre)))
2344 ;; If we delete right up to the root of this group
2345 ;; (that is, there are no support windows around) give
2346 ;; WINDOW the parameters of `root'.
2347 (set-window-parameter window 'group t)
2348 (set-window-parameter
2349 window 'composite (window-parameter root 'composite))))
2350 ;; We might swallow this message.
2351 (error
2352 "Cannot delete other windows for non-main window %s" window))))))
2354 (delete-other-windows-internal window main)
2355 (when (and (memq ignore-window-parameters '(nil pre))
2356 (frame-root-window-p window))
2357 ;; Clean up for the case where we did something special.
2358 (set-window-parameter window 'composite nil))
2359 nil)))
2361 (defun delete-other-windows-vertically (&optional window)
2362 "Delete the windows in the same column with WINDOW, but not WINDOW itself.
2363 This may be a useful alternative binding for \\[delete-other-windows]
2364 if you often split windows horizontally."
2365 (interactive)
2366 (let* ((window (or window (selected-window)))
2367 (edges (window-edges window))
2368 (w window) delenda)
2369 (while (not (eq (setq w (next-window w 1)) window))
2370 (let ((e (window-edges w)))
2371 (when (and (= (car e) (car edges))
2372 (= (caddr e) (caddr edges)))
2373 (push w delenda))))
2374 (mapc 'delete-window delenda)))
2376 ;;; Windows and buffers.
2378 ;; `prev-buffers' and `next-buffers' are two reserved window slots used
2379 ;; for (1) determining which buffer to show in the window when its
2380 ;; buffer shall be buried or killed and (2) which buffer to show for
2381 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2383 ;; `prev-buffers' consists of <buffer, window-start, window-point>
2384 ;; triples. The entries on this list are ordered by the time their
2385 ;; buffer has been removed from the window, the most recently removed
2386 ;; buffer's entry being first. The window-start and window-point
2387 ;; components are `window-start' and `window-point' at the time the
2388 ;; buffer was removed from the window which implies that the entry must
2389 ;; be added when `set-window-buffer' removes the buffer from the window.
2391 ;; `next-buffers' is the list of buffers that have been replaced
2392 ;; recently by `switch-to-prev-buffer'. These buffers are the least
2393 ;; preferred candidates of `switch-to-prev-buffer' and the preferred
2394 ;; candidates of `switch-to-next-buffer' to switch to. This list is
2395 ;; reset to nil by any action changing the window's buffer with the
2396 ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
2397 ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
2398 ;; `switch-to-next-buffer' pops the last pushed buffer from it.
2400 ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
2401 ;; if such a buffer was killed while the window was hidden within a
2402 ;; window configuration. Such killed buffers get removed whenever
2403 ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
2405 ;; The following function is called by `set-window-buffer' _before_ it
2406 ;; replaces the buffer of the argument window with the new buffer.
2407 (defun record-window-buffer (&optional window)
2408 "Record WINDOW's buffer.
2409 WINDOW must be a live window and defaults to the selected one."
2410 (let* ((window (normalize-live-window window))
2411 (buffer (window-buffer window))
2412 (entry (assq buffer (window-prev-buffers window))))
2413 ;; Reset WINDOW's next buffers. If needed, they are resurrected by
2414 ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
2415 (set-window-next-buffers window nil)
2417 (when entry
2418 ;; Remove all entries for BUFFER from WINDOW's previous buffers.
2419 (set-window-prev-buffers
2420 window (assq-delete-all buffer (window-prev-buffers window))))
2422 ;; Don't record insignificant buffers.
2423 (unless (eq (aref (buffer-name buffer) 0) ?\s)
2424 ;; Add an entry for buffer to WINDOW's previous buffers.
2425 (with-current-buffer buffer
2426 (let ((start (window-start window))
2427 (point (window-point window)))
2428 (setq entry
2429 (cons buffer
2430 (if entry
2431 ;; We have an entry, update marker positions.
2432 (list (set-marker (nth 1 entry) start)
2433 (set-marker (nth 2 entry) point))
2434 ;; Make new markers.
2435 (list (copy-marker start)
2436 (copy-marker point)))))
2438 (set-window-prev-buffers
2439 window (cons entry (window-prev-buffers window))))))))
2441 (defun unrecord-window-buffer (&optional window buffer)
2442 "Unrecord BUFFER in WINDOW.
2443 WINDOW must be a live window and defaults to the selected one.
2444 BUFFER must be a live buffer and defaults to the buffer of
2445 WINDOW."
2446 (let* ((window (normalize-live-window window))
2447 (buffer (or buffer (window-buffer window))))
2448 (set-window-prev-buffers
2449 window (assq-delete-all buffer (window-prev-buffers window)))
2450 (set-window-next-buffers
2451 window (delq buffer (window-next-buffers window)))))
2453 (defun set-window-buffer-start-and-point (window buffer &optional start point)
2454 "Set WINDOW's buffer to BUFFER.
2455 Optional argument START non-nil means set WINDOW's start position
2456 to START. Optional argument POINT non-nil means set WINDOW's
2457 point to POINT. If WINDOW is selected this also sets BUFFER's
2458 `point' to POINT. If WINDOW is selected and the buffer it showed
2459 before was current this also makes BUFFER the current buffer."
2460 (let ((selected (eq window (selected-window)))
2461 (current (eq (window-buffer window) (current-buffer))))
2462 (set-window-buffer window buffer)
2463 (when (and selected current)
2464 (set-buffer buffer))
2465 (when start
2466 (set-window-start window start))
2467 (when point
2468 (if selected
2469 (with-current-buffer buffer
2470 (goto-char point))
2471 (set-window-point window point)))))
2473 (defun switch-to-prev-buffer (&optional window bury-or-kill)
2474 "In WINDOW switch to previous buffer.
2475 WINDOW must be a live window and defaults to the selected one.
2477 Optional argument BURY-OR-KILL non-nil means the buffer currently
2478 shown in WINDOW is about to be buried or killed and consequently
2479 shall not be switched to in future invocations of this command."
2480 (interactive)
2481 (let* ((window (normalize-live-window window))
2482 (old-buffer (window-buffer window))
2483 ;; Save this since it's destroyed by `set-window-buffer'.
2484 (next-buffers (window-next-buffers window))
2485 entry new-buffer killed-buffers deletable)
2486 (cond
2487 ;; When BURY-OR-KILL is non-nil, there's no previous buffer for
2488 ;; this window, and we can delete the window (or the frame) do
2489 ;; that.
2490 ((and bury-or-kill
2491 (or (not (window-prev-buffers window))
2492 (and (eq (caar (window-prev-buffers window)) old-buffer)
2493 (not (cdr (car (window-prev-buffers window))))))
2494 (setq deletable (window-deletable-p window)))
2495 (if (eq deletable 'frame)
2496 (delete-frame (window-frame window))
2497 (delete-window window)))
2498 ((window-dedicated-p window)
2499 (error "Window %s is dedicated to buffer %s" window old-buffer)))
2501 (unless deletable
2502 (catch 'found
2503 ;; Scan WINDOW's previous buffers first, skipping entries of next
2504 ;; buffers.
2505 (dolist (entry (window-prev-buffers window))
2506 (when (and (setq new-buffer (car entry))
2507 (or (buffer-live-p new-buffer)
2508 (not (setq killed-buffers
2509 (cons new-buffer killed-buffers))))
2510 (not (eq new-buffer old-buffer))
2511 (or bury-or-kill
2512 (not (memq new-buffer next-buffers))))
2513 (set-window-buffer-start-and-point
2514 window new-buffer (nth 1 entry) (nth 2 entry))
2515 (throw 'found t)))
2516 ;; Scan reverted buffer list of WINDOW's frame next, skipping
2517 ;; entries of next buffers. Note that when we bury or kill a
2518 ;; buffer we don't reverse the global buffer list to avoid showing
2519 ;; a buried buffer instead. Otherwise, we must reverse the global
2520 ;; buffer list in order to make sure that switching to the
2521 ;; previous/next buffer traverse it in opposite directions.
2522 (dolist (buffer (if bury-or-kill
2523 (buffer-list (window-frame window))
2524 (nreverse (buffer-list (window-frame window)))))
2525 (when (and (buffer-live-p buffer)
2526 (not (eq buffer old-buffer))
2527 (not (eq (aref (buffer-name buffer) 0) ?\s))
2528 (or bury-or-kill (not (memq buffer next-buffers))))
2529 (setq new-buffer buffer)
2530 (set-window-buffer-start-and-point window new-buffer)
2531 (throw 'found t)))
2532 (unless bury-or-kill
2533 ;; Scan reverted next buffers last (must not use nreverse
2534 ;; here!).
2535 (dolist (buffer (reverse next-buffers))
2536 ;; Actually, buffer _must_ be live here since otherwise it
2537 ;; would have been caught in the scan of previous buffers.
2538 (when (and (or (buffer-live-p buffer)
2539 (not (setq killed-buffers
2540 (cons buffer killed-buffers))))
2541 (not (eq buffer old-buffer))
2542 (setq entry (assq buffer (window-prev-buffers window))))
2543 (setq new-buffer buffer)
2544 (set-window-buffer-start-and-point
2545 window new-buffer (nth 1 entry) (nth 2 entry))
2546 (throw 'found t)))))
2548 (if bury-or-kill
2549 ;; Remove `old-buffer' from WINDOW's previous and (restored list
2550 ;; of) next buffers.
2551 (progn
2552 (set-window-prev-buffers
2553 window (assq-delete-all old-buffer (window-prev-buffers window)))
2554 (set-window-next-buffers window (delq old-buffer next-buffers)))
2555 ;; Move `old-buffer' to head of WINDOW's restored list of next
2556 ;; buffers.
2557 (set-window-next-buffers
2558 window (cons old-buffer (delq old-buffer next-buffers)))))
2560 ;; Remove killed buffers from WINDOW's previous and next buffers.
2561 (when killed-buffers
2562 (dolist (buffer killed-buffers)
2563 (set-window-prev-buffers
2564 window (assq-delete-all buffer (window-prev-buffers window)))
2565 (set-window-next-buffers
2566 window (delq buffer (window-next-buffers window)))))
2568 ;; Return new-buffer.
2569 new-buffer))
2571 (defun switch-to-next-buffer (&optional window)
2572 "In WINDOW switch to next buffer.
2573 WINDOW must be a live window and defaults to the selected one."
2574 (interactive)
2575 (let* ((window (normalize-live-window window))
2576 (old-buffer (window-buffer window))
2577 (next-buffers (window-next-buffers window))
2578 new-buffer entry killed-buffers)
2579 (when (window-dedicated-p window)
2580 (error "Window %s is dedicated to buffer %s" window old-buffer))
2582 (catch 'found
2583 ;; Scan WINDOW's next buffers first.
2584 (dolist (buffer next-buffers)
2585 (when (and (or (buffer-live-p buffer)
2586 (not (setq killed-buffers
2587 (cons buffer killed-buffers))))
2588 (not (eq buffer old-buffer))
2589 (setq entry (assq buffer (window-prev-buffers window))))
2590 (setq new-buffer buffer)
2591 (set-window-buffer-start-and-point
2592 window new-buffer (nth 1 entry) (nth 2 entry))
2593 (throw 'found t)))
2594 ;; Scan the buffer list of WINDOW's frame next, skipping previous
2595 ;; buffers entries.
2596 (dolist (buffer (buffer-list (window-frame window)))
2597 (when (and (buffer-live-p buffer) (not (eq buffer old-buffer))
2598 (not (eq (aref (buffer-name buffer) 0) ?\s))
2599 (not (assq buffer (window-prev-buffers window))))
2600 (setq new-buffer buffer)
2601 (set-window-buffer-start-and-point window new-buffer)
2602 (throw 'found t)))
2603 ;; Scan WINDOW's reverted previous buffers last (must not use
2604 ;; nreverse here!)
2605 (dolist (entry (reverse (window-prev-buffers window)))
2606 (when (and (setq new-buffer (car entry))
2607 (or (buffer-live-p new-buffer)
2608 (not (setq killed-buffers
2609 (cons new-buffer killed-buffers))))
2610 (not (eq new-buffer old-buffer)))
2611 (set-window-buffer-start-and-point
2612 window new-buffer (nth 1 entry) (nth 2 entry))
2613 (throw 'found t))))
2615 ;; Remove `new-buffer' from and restore WINDOW's next buffers.
2616 (set-window-next-buffers window (delq new-buffer next-buffers))
2618 ;; Remove killed buffers from WINDOW's previous and next buffers.
2619 (when killed-buffers
2620 (dolist (buffer killed-buffers)
2621 (set-window-prev-buffers
2622 window (assq-delete-all buffer (window-prev-buffers window)))
2623 (set-window-next-buffers
2624 window (delq buffer (window-next-buffers window)))))
2626 ;; Return new-buffer.
2627 new-buffer))
2629 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
2630 "Search LIST for a valid buffer to display in FRAME.
2631 Return nil when all buffers in LIST are undesirable for display,
2632 otherwise return the first suitable buffer in LIST.
2634 Buffers not visible in windows are preferred to visible buffers,
2635 unless VISIBLE-OK is non-nil.
2636 If the optional argument FRAME is nil, it defaults to the selected frame.
2637 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
2638 ;; This logic is more or less copied from other-buffer.
2639 (setq frame (or frame (selected-frame)))
2640 (let ((pred (frame-parameter frame 'buffer-predicate))
2641 found buf)
2642 (while (and (not found) list)
2643 (setq buf (car list))
2644 (if (and (not (eq buffer buf))
2645 (buffer-live-p buf)
2646 (or (null pred) (funcall pred buf))
2647 (not (eq (aref (buffer-name buf) 0) ?\s))
2648 (or visible-ok (null (get-buffer-window buf 'visible))))
2649 (setq found buf)
2650 (setq list (cdr list))))
2651 (car list)))
2653 (defun last-buffer (&optional buffer visible-ok frame)
2654 "Return the last buffer in FRAME's buffer list.
2655 If BUFFER is the last buffer, return the preceding buffer
2656 instead. Buffers not visible in windows are preferred to visible
2657 buffers, unless optional argument VISIBLE-OK is non-nil.
2658 Optional third argument FRAME nil or omitted means use the
2659 selected frame's buffer list. If no such buffer exists, return
2660 the buffer `*scratch*', creating it if necessary."
2661 (setq frame (or frame (selected-frame)))
2662 (or (get-next-valid-buffer (nreverse (buffer-list frame))
2663 buffer visible-ok frame)
2664 (get-buffer "*scratch*")
2665 (let ((scratch (get-buffer-create "*scratch*")))
2666 (set-buffer-major-mode scratch)
2667 scratch)))
2669 (defun bury-buffer (&optional buffer-or-name)
2670 "Put BUFFER-OR-NAME at the end of the list of all buffers.
2671 There it is the least likely candidate for `other-buffer' to
2672 return; thus, the least likely buffer for \\[switch-to-buffer] to
2673 select by default.
2675 You can specify a buffer name as BUFFER-OR-NAME, or an actual
2676 buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
2677 current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
2678 remove the current buffer from the selected window if it is
2679 displayed there."
2680 (interactive)
2681 (let* ((buffer (normalize-live-buffer buffer-or-name)))
2682 ;; If `buffer-or-name' is not on the selected frame we unrecord it
2683 ;; although it's not "here" (call it a feature).
2684 (unrecord-buffer buffer)
2685 ;; Handle case where `buffer-or-name' is nil and the current buffer
2686 ;; is shown in the selected window.
2687 (cond
2688 ((or buffer-or-name (not (eq buffer (window-buffer)))))
2689 ((not (window-dedicated-p))
2690 (switch-to-prev-buffer nil 'bury))
2691 ((frame-root-window-p (selected-window))
2692 (iconify-frame (window-frame (selected-window))))
2693 ((window-deletable-p)
2694 (delete-window)))
2695 ;; Always return nil.
2696 nil))
2698 (defun unbury-buffer ()
2699 "Switch to the last buffer in the buffer list."
2700 (interactive)
2701 (switch-to-buffer (last-buffer)))
2703 (defun next-buffer ()
2704 "In selected window switch to next buffer."
2705 (interactive)
2706 (switch-to-next-buffer))
2708 (defun previous-buffer ()
2709 "In selected window switch to previous buffer."
2710 (interactive)
2711 (switch-to-prev-buffer))
2713 (defun delete-windows-on (&optional buffer-or-name frame)
2714 "Delete all windows showing BUFFER-OR-NAME.
2715 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2716 and defaults to the current buffer.
2718 The following non-nil values of the optional argument FRAME
2719 have special meanings:
2721 - t means consider all windows on the selected frame only.
2723 - `visible' means consider all windows on all visible frames.
2725 - 0 (the number zero) means consider all windows on all visible
2726 and iconified frames.
2728 - A frame means consider all windows on that frame only.
2730 Any other value of FRAME means consider all windows on all
2731 frames.
2733 When a window showing BUFFER-OR-NAME is dedicated and the only
2734 window of its frame, that frame is deleted when there are other
2735 frames left."
2736 (interactive "BDelete windows on (buffer):\nP")
2737 (let ((buffer (normalize-live-buffer buffer-or-name))
2738 ;; Handle the "inverted" meaning of the FRAME argument wrt other
2739 ;; `window-list-1' based function.
2740 (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
2741 (dolist (window (window-list-1 nil nil all-frames))
2742 (if (eq (window-buffer window) buffer)
2743 (let ((deletable (window-deletable-p window)))
2744 (cond
2745 ((eq deletable 'frame)
2746 ;; Delete frame.
2747 (delete-frame (window-frame window)))
2748 (deletable
2749 ;; Delete window only.
2750 (delete-window window))
2752 ;; In window switch to previous buffer.
2753 (set-window-dedicated-p window nil)
2754 (switch-to-prev-buffer window 'bury))))
2755 ;; If a window doesn't show BUFFER, unrecord it nevertheless.
2756 (unrecord-window-buffer window buffer)))))
2758 (defun replace-buffer-in-windows (&optional buffer-or-name)
2759 "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2760 BUFFER-OR-NAME may be a buffer or the name of an existing buffer
2761 and defaults to the current buffer.
2763 When a window showing BUFFER-OR-NAME is dedicated that window is
2764 deleted. If that window is the only window on its frame, that
2765 frame is deleted too when there are other frames left. If there
2766 are no other frames left, some other buffer is displayed in that
2767 window.
2769 This function removes the buffer denoted by BUFFER-OR-NAME from
2770 all window-local buffer lists."
2771 (let ((buffer (normalize-live-buffer buffer-or-name)))
2772 (dolist (window (window-list-1 nil nil t))
2773 (if (eq (window-buffer window) buffer)
2774 (let ((deletable (window-deletable-p window)))
2775 (cond
2776 ((eq deletable 'frame)
2777 ;; Delete frame.
2778 (delete-frame (window-frame window)))
2779 ((and (window-dedicated-p window) deletable)
2780 ;; Delete window.
2781 (delete-window window))
2783 ;; Switch to another buffer in window.
2784 (set-window-dedicated-p window nil)
2785 (switch-to-prev-buffer window 'kill))))
2786 ;; Unrecord BUFFER in WINDOW.
2787 (unrecord-window-buffer window buffer)))))
2789 (defun quit-restore-window (&optional window kill)
2790 "Quit WINDOW in some way.
2791 WINDOW must be a live window and defaults to the selected window.
2792 Return nil.
2794 According to information stored in WINDOW's `quit-restore' window
2795 parameter either \(1) delete WINDOW and its frame, \(2) delete
2796 WINDOW, \(3) restore the buffer previously displayed in WINDOW,
2797 or \(4) make WINDOW display some other buffer than the present
2798 one. If non-nil, reset `quit-restore' parameter to nil.
2800 Optional argument KILL non-nil means in addition kill WINDOW's
2801 buffer. If KILL is nil, put WINDOW's buffer at the end of the
2802 buffer list. Interactively, KILL is the prefix argument."
2803 (interactive "i\nP")
2804 (setq window (normalize-live-window window))
2805 (let ((buffer (window-buffer window))
2806 (parameters (window-parameter window 'quit-restore))
2807 deletable)
2808 (cond
2809 ((and (eq (car-safe parameters) t)
2810 (setq deletable (window-deletable-p window)))
2811 (unrecord-buffer buffer)
2812 ;; WINDOW is deletable.
2813 (if (eq deletable 'frame)
2814 ;; WINDOW's frame can be deleted.
2815 (delete-frame (window-frame window))
2816 ;; Just delete WINDOW.
2817 (delete-window window))
2818 (when (window-live-p (nth 1 parameters))
2819 (select-window (nth 1 parameters))))
2820 ((and (buffer-live-p (nth 0 parameters))
2821 ;; The buffer currently shown in WINDOW must still be the
2822 ;; buffer shown when the `quit-restore' parameter was
2823 ;; created in the first place.
2824 (eq (window-buffer window) (nth 3 parameters)))
2825 ;; Unrecord buffer.
2826 (unrecord-buffer buffer)
2827 (unrecord-window-buffer window buffer)
2828 ;; The `quit-restore' parameters tell us the buffer to display in
2829 ;; WINDOW and how to do that.
2830 (set-window-dedicated-p window nil)
2831 (set-window-buffer window (nth 0 parameters))
2832 (set-window-start window (nth 1 parameters))
2833 (set-window-point window (nth 2 parameters))
2834 (unless (= (nth 4 parameters) (window-total-size window))
2835 (resize-window
2836 window (- (nth 4 parameters) (window-total-size window))))
2837 (set-window-parameter window 'quit-restore nil)
2838 (when (window-live-p (nth 5 parameters))
2839 (select-window (nth 5 parameters))))
2840 ((and (window-dedicated-p window)
2841 (setq deletable (window-deletable-p window)))
2842 (unrecord-buffer buffer)
2843 ;; WINDOW is dedicated and deletable.
2844 (if (eq deletable 'frame)
2845 ;; WINDOW's frame can be deleted.
2846 (delete-frame (window-frame window))
2847 ;; Just delete WINDOW.
2848 (delete-window window)))
2850 ;; Otherwise, show another buffer in WINDOW.
2851 (set-window-parameter window 'quit-restore nil)
2852 (unrecord-buffer buffer)
2853 (switch-to-prev-buffer window 'bury-or-kill)))
2855 ;; Kill WINDOW's old-buffer if requested
2856 (when kill (kill-buffer buffer))
2857 nil))
2859 ;;; Splitting windows.
2860 (defsubst window-split-min-size (&optional horflag)
2861 "Return minimum height of any window.
2862 Optional argument HORFLAG non-nil means return minimum width."
2863 (if horflag
2864 (max window-min-width window-safe-min-width)
2865 (max window-min-height window-safe-min-height)))
2867 (defun window-children-count (window)
2868 "Return number of child windows of WINDOW."
2869 (let ((count 0)
2870 (sub (window-child window)))
2871 (while sub
2872 (setq count (1+ count))
2873 (setq sub (window-right sub)))
2874 count))
2876 (defun split-window (&optional window size horizontal)
2877 "Create a new window adjacent to WINDOW.
2878 WINDOW can be any window and defaults to the selected one. If
2879 WINDOW was selected before invoking this function, it remains
2880 selected. Return the new window which is always a live window.
2882 Optional argument SIZE a positive number means make WINDOW SIZE
2883 lines/columns tall. If SIZE is negative, make the new window
2884 -SIZE lines/columns tall. If and only if SIZE is negative, its
2885 absolute value can be less than `window-min-height' or
2886 `window-min-width'; so this command can make a new window as
2887 small as one line or two columns. SIZE defaults to half of
2888 WINDOW's size. The variable `window-splits' determines how the
2889 size of other windows is affected by this function.
2891 Optional third argument HORIZONTAL nil (or `below') specifies
2892 that the new window shall be located below WINDOW. HORIZONTAL
2893 `above' means the new window shall be located above WINDOW. In
2894 both cases SIZE specifies the new number of lines for WINDOW \(or
2895 the new window if SIZE is negative) including space reserved for
2896 the mode and/or header line.
2898 HORIZONTAL t (or `right') specifies that the new window shall be
2899 located on the right side of WINDOW. HORIZONTAL `left' means the
2900 new window shall be located on the left of WINDOW. In both cases
2901 SIZE specifies the new number of columns for WINDOW \(or the new
2902 window provided SIZE is negative) including space reserved for
2903 fringes and the scrollbar or a divider column. Any other non-nil
2904 value for HORIZONTAL is currently handled like t (or `right').
2906 If WINDOW is a component of a compound window \"split\" the root
2907 of the compound window instead. The new window does not become a
2908 member of the compound window. If WINDOW is a main window of a
2909 window group, the new window becomes a main window in that window
2910 group. If WINDOW is a non-main component of a window group
2911 signal an error.
2913 If you split a live window, properties of the new window like
2914 margins and scrollbars are inherited from WINDOW. If you split
2915 an internal window, these properties as well as the buffer
2916 displayed in the new window are inherited from the selected
2917 window on WINDOW's frame."
2918 (interactive "i")
2919 (setq window (normalize-any-window window))
2920 (let* ((horflag (and horizontal (not (memq horizontal '(below above)))))
2921 (function (window-parameter window 'split-window))
2922 ;; Rebind this locally since in some cases we do have to nest.
2923 (window-splits window-splits)
2924 ;; COMPOSITE non-nil means WINDOW is part of a composite
2925 ;; window. TYPE is the type of the composite window (either
2926 ;; `compound' or `group'). ROLE is the role of WINDOW within
2927 ;; the composite window (either `main', `support', or t). ROOT
2928 ;; is the root window of the composite window.
2929 (composite (window-parameter window 'composite))
2930 (type (car-safe composite))
2931 (role (cdr-safe composite))
2932 (root (and composite (composite-root-window window)))
2933 old-composite new-root new-main)
2934 (catch 'done
2935 (cond
2936 ;; Ignore window parameters if `ignore-window-parameters' tells
2937 ;; so or the `split-window' window parameter equals t.
2938 ((or (not (memq ignore-window-parameters '(nil post)))
2939 (eq function t)))
2940 ((functionp function)
2941 ;; The `split-window' parameter specifies the function to call
2942 ;; instead. If this is `ignore', WINDOW won't be split.
2943 (throw 'done (funcall function window size horizontal)))
2944 ((and (not composite) (window-parameter window 'group)
2945 (window-live-p window))
2946 ;; WINDOW is a live group root window and not part of a
2947 ;; composite window so we need a new group root window. Note
2948 ;; that if WINDOW is also the root of a compound window, that
2949 ;; part remains unaffected by what we do here - WINDOW remains
2950 ;; root of the compound window which is now a component of a
2951 ;; window group.
2952 (setq window-splits 'nest)
2953 (setq new-root t))
2954 (composite
2955 (cond
2956 ((or (not root) (not type) (not role))
2957 ;; Something is broken in this composite window. Signal a
2958 ;; message but let the split pass through (we might signal
2959 ;; an error here but for everday work this is too nasty).
2960 (message "Broken component %s of composite window" window))
2961 ((eq type 'compound)
2962 ;; In a compound window split the root window.
2963 (throw 'done (split-window root size horizontal)))
2964 ((eq type 'group)
2965 (cond
2966 ((not (eq role 'main))
2967 ;; In a window group we are only allowed to split main
2968 ;; windows. We might swallow this error silently.
2969 (error "Cannot split non-main window %s in a window group" window))
2970 ((or (not (window-parent window)) ; Should have been handled above.
2971 (not (eq (composite-lowest-child-role (window-parent window))
2972 'main)))
2973 (setq new-main t)
2974 ;; We must nest since otherwise we might end up with a
2975 ;; window group having two dominating main windows.
2976 (setq window-splits 'nest)))))))
2977 ;; The following line is hopefully not needed ...
2978 ;; (setq window-splits (if (eq window root) 'nest window-splits))
2979 (let* ((frame (window-frame window))
2980 (parent (window-parent window))
2981 ;; Size calculations.
2982 (parent-size (window-total-size parent horflag))
2983 ;; Bind `old-size' to the current size of WINDOW and
2984 ;; `new-size' to the size of the new window.
2985 (old-size (window-total-size window horflag))
2986 (resize
2987 (and (eq window-splits 'resize)
2988 ;; Resize makes sense in iso-combinations only.
2989 (window-iso-combined-p window horflag)))
2990 (new-size
2991 (cond
2992 ((not size)
2993 (max (window-split-min-size horflag)
2994 (if resize
2995 ;; For a "resize" split try to give the new
2996 ;; window a fitting size (which must be at least
2997 ;; as large as what we can get at all).
2998 (min (- parent-size
2999 (window-min-size parent horflag))
3000 (/ parent-size
3001 (1+ (window-children-count parent))))
3002 ;; Else try to give the new window half the size of
3003 ;; WINDOW.
3004 (/ old-size 2))))
3005 ((>= size 0)
3006 ;; SIZE non-negative specifies the new size of WINDOW.
3007 (- old-size size))
3009 ;; SIZE negative specifies the size of the new window.
3010 (- size))))
3011 (root (window-parameter window 'root)))
3012 (cond
3013 ((and resize (not (window-sizable-p parent (- new-size) horflag))
3014 ;; Try agin with minimum acceptable size.
3015 (setq new-size
3016 (max new-size
3017 (window-split-min-size horflag)))
3018 (not (window-sizable-p parent (- new-size) horflag)))
3019 (error "Cannot resize %s" window))
3020 ((and (not resize)
3021 (> (+ new-size (window-min-size window horflag))
3022 old-size))
3023 (error "Cannot resize %s" window))
3024 ((< new-size
3025 (if (and size (< size 0))
3026 ;; If SIZE explicitly specifies a negative value, respect
3027 ;; that.
3028 (if horflag window-safe-min-width window-safe-min-height)
3029 (if horflag window-min-width window-min-height)))
3030 (error "New window too small")))
3032 (resize-window-reset (window-frame window) horflag)
3033 (cond
3034 (resize
3035 ;; Try to get space from OLD's siblings. We could go "up" and
3036 ;; try getting additional space from surrounding windows but we
3037 ;; won't be able to return space to those windows when we delete
3038 ;; the one we create here. Hence we do not go up.
3039 (resize-subwindows parent (- new-size) horflag)
3040 (let* ((parent-size (window-total-size parent horflag))
3041 (sub (window-child parent)))
3042 ;; Assign new normal sizes.
3043 (while sub
3044 (resize-window-normal
3045 sub (/ (* (float (window-normal-size sub horflag))
3046 (- parent-size new-size))
3047 parent-size))
3048 (setq sub (window-right sub)))))
3049 ((eq window-splits 'nest)
3050 ;; Get entire space from WINDOW making sure that a new parent
3051 ;; windows gets created.
3052 (resize-window-total window (- old-size new-size))
3053 (resize-this-window window (- new-size) horflag)
3054 (resize-window-normal
3055 window (/ (float (window-new-total-size window)) old-size)))
3057 ;; Get entire space from WINDOW making a new parent window only
3058 ;; if we need one.
3059 (resize-window-total window (- old-size new-size))
3060 (resize-this-window window (- new-size) horflag)
3061 (resize-window-normal
3062 window (/ (float (window-new-total-size window))
3063 (window-total-size (window-parent window) horflag)))))
3065 (let* ((new (split-window-internal window new-size horizontal))
3066 (new-parent (window-parent new)))
3067 (when (memq ignore-window-parameters '(nil pre))
3068 (cond
3069 ((and new-root (not (eq parent new-parent)))
3070 ;; `new-parent' becomes the new group root window
3071 ;; inheriting WINDOW's composite status. WINDOW and `new'
3072 ;; become main windows of that group.
3073 (set-window-parameter new-parent 'group t)
3074 (set-window-parameter new-parent 'composite composite)
3075 (set-window-parameter window 'group nil)
3076 (set-window-parameter window 'composite (cons 'group 'main))
3077 (set-window-parameter new 'composite (cons 'group 'main)))
3078 ((and new-main (not (eq parent new-parent)))
3079 ;; `new-parent' becomes the new dominating main window of
3080 ;; WINDOW's group.
3081 (set-window-parameter new-parent 'composite (cons 'group 'main))
3082 (set-window-parameter window 'composite (cons 'group 'main))
3083 (set-window-parameter new 'composite (cons 'group 'main)))
3084 (composite
3085 ;; `new' inherits parameters from WINDOW.
3086 (set-window-parameter new 'composite composite)
3087 (when (not (eq parent new-parent))
3088 ;; `new-parent' "inherits" the parameters as well
3089 (set-window-parameter new-parent 'composite composite)))))
3090 ;; We have to check once more how often these hooks are run.
3091 (run-window-configuration-change-hook frame)
3092 ;; Return the new window.
3093 new)))))
3095 ;; I think this should be the default; I think people will prefer it--rms.
3096 (defcustom split-window-keep-point t
3097 "If non-nil, \\[split-window-vertically] keeps the original point \
3098 in both children.
3099 This is often more convenient for editing.
3100 If nil, adjust point in each of the two windows to minimize redisplay.
3101 This is convenient on slow terminals, but point can move strangely.
3103 This option applies only to `split-window-vertically' and
3104 functions that call it. `split-window' always keeps the original
3105 point in both children."
3106 :type 'boolean
3107 :group 'windows)
3109 (defun split-window-quit-restore (new-window old-window)
3110 "Copy `quit-restore' parameter from OLD-WINDOW to NEW-WINDOW.
3111 Do this if and only if NEW-WINDOW's buffer is in `view-mode'."
3112 (let ((parameter (window-parameter old-window 'quit-restore)))
3113 (when (and parameter
3114 (with-current-buffer (window-buffer new-window)
3115 view-mode))
3116 (set-window-parameter new-window 'quit-restore parameter))))
3118 (defun split-window-vertically (&optional size)
3119 "Split selected window into two windows, one above the other.
3120 The upper window gets SIZE lines and the lower one gets the rest.
3121 SIZE negative means the lower window gets -SIZE lines and the
3122 upper one the rest. With no argument, split windows equally or
3123 close to it. Both windows display the same buffer, now current.
3125 If the variable `split-window-keep-point' is non-nil, both new
3126 windows will get the same value of point as the selected window.
3127 This is often more convenient for editing. The upper window is
3128 the selected window.
3130 Otherwise, we choose window starts so as to minimize the amount of
3131 redisplay; this is convenient on slow terminals. The new selected
3132 window is the one that the current value of point appears in. The
3133 value of point can change if the text around point is hidden by the
3134 new mode line.
3136 Regardless of the value of `split-window-keep-point', the upper
3137 window is the original one and the return value is the new, lower
3138 window."
3139 (interactive "P")
3140 (let ((old-window (selected-window))
3141 (old-point (point))
3142 (size (and size (prefix-numeric-value size)))
3143 moved-by-window-height moved new-window bottom)
3144 (when (and size (< size 0) (< (- size) window-min-height))
3145 ;; `split-window' would not signal an error here.
3146 (error "Size of new window too small"))
3147 (setq new-window (split-window nil size))
3148 (unless split-window-keep-point
3149 (with-current-buffer (window-buffer)
3150 (goto-char (window-start))
3151 (setq moved (vertical-motion (window-height)))
3152 (set-window-start new-window (point))
3153 (when (> (point) (window-point new-window))
3154 (set-window-point new-window (point)))
3155 (when (= moved (window-height))
3156 (setq moved-by-window-height t)
3157 (vertical-motion -1))
3158 (setq bottom (point)))
3159 (and moved-by-window-height
3160 (<= bottom (point))
3161 (set-window-point old-window (1- bottom)))
3162 (and moved-by-window-height
3163 (<= (window-start new-window) old-point)
3164 (set-window-point new-window old-point)
3165 (select-window new-window)))
3166 (split-window-quit-restore new-window old-window)
3167 new-window))
3169 (defun split-window-horizontally (&optional size)
3170 "Split selected window into two windows side by side.
3171 The selected window becomes the left one and gets SIZE columns.
3172 SIZE negative means the right window gets -SIZE lines.
3174 SIZE includes the width of the window's scroll bar; if there are
3175 no scroll bars, it includes the width of the divider column to
3176 the window's right, if any. SIZE omitted or nil means split
3177 window equally.
3179 The selected window remains selected. Return the new window."
3180 (interactive "P")
3181 (let ((old-window (selected-window))
3182 (size (and size (prefix-numeric-value size)))
3183 new-window)
3184 (when (and size (< size 0) (< (- size) window-min-width))
3185 ;; `split-window' would not signal an error here.
3186 (error "Size of new window too small"))
3187 (setq new-window (split-window nil size t))
3188 (split-window-quit-restore new-window old-window)
3189 new-window))
3191 ;;; Composite windows.
3192 (defun make-compound-window (&optional window main size horizontal)
3193 "Make WINDOW the main window of a new compound window.
3194 This function creates a new internal window with WINDOW and a new
3195 leaf window as its only children. WINDOW must be a leaf window
3196 and defaults to the selected window.
3198 Optional argument MAIN non-nil makes the new leaf window a main
3199 window. MAIN nil or not provided means the new leaf window
3200 becomes a support window. WINDOW itself becomes a main window.
3202 Optional arguments SIZE and HORIZONTAL are as for `split-window'.
3204 Return the new leaf window."
3205 (setq window (normalize-any-window window))
3206 (unless (or (window-live-p window) (composite-root-window-p window))
3207 (error "Window %s must be live or a composite root window" window))
3208 (let* ((composite (window-parameter window 'composite))
3209 ;; FORCE and NEST.
3210 (ignore-window-parameters t)
3211 (window-splits 'nest)
3212 (new (split-window window size horizontal))
3213 (new-parent (window-parent new)))
3214 (set-window-parameter new-parent 'compound t)
3215 (when composite (set-window-parameter new-parent 'composite composite))
3216 (set-window-parameter window 'composite (cons 'compound 'main))
3217 (set-window-parameter
3218 new 'composite (cons 'compound (if main 'main 'support)))
3219 new))
3221 (defun make-window-group (&optional window)
3222 "Make WINDOW main and root window of a new window group.
3223 WINDOW must be a live window and defaults to the selected one.
3224 Return WINDOW."
3225 (setq window (normalize-live-window window))
3226 (set-window-parameter window 'composite (cons 'group 'main)))
3228 (defun make-support-window (window support &optional size horizontal)
3229 "Add support window of type SUPPORT to WINDOW."
3230 (let* ((compound (window-parameter window 'compound))
3231 (group (window-parameter window 'group))
3232 (composite (window-parameter window 'composite))
3233 (type (car-safe composite))
3234 (role (cdr-safe composite))
3235 ;; `type' is the type of the composite window (either
3236 ;; `compound' or `group'). `role' is the role of WINDOW within
3237 ;; the composite window (either `main', `support', or t).
3238 (root (when composite (composite-root-window window)))
3239 (parent (window-parent window))
3240 (ignore-window-parameters t)
3241 (window-splits 'nest)
3242 new new-parent)
3243 (cond
3244 ((not (memq support '(compound group)))
3245 (error "Invalid support argument %s" support))
3246 ((and (eq support 'compound) (not compound) (not (eq type 'compound)))
3247 (error "Window %s is not a component of a compound window" window))
3248 ((and (eq support 'group) (not group) (not (eq type 'group)))
3249 (error "Window %s is not a component of a window group" window))
3250 ((and (eq type 'main) (not (eq window (composite-major-window window))))
3251 (error "Can't embed support window in main window")))
3252 (setq new (split-window window size horizontal))
3253 (unless (eq parent (window-parent window))
3254 (setq new-parent (window-parent window)))
3255 (cond
3256 ;; This conditional looks incredibly tedious but let's keep the
3257 ;; distinct cases self-contained to avoid further confusion.
3258 ((and compound (eq support 'compound))
3259 (when new-parent
3260 ;; `new-parent' inherits the compound status of `window'
3261 (set-window-parameter new-parent 'compound t)
3262 (set-window-parameter window 'compound nil)
3263 (when composite
3264 ;; `new-parent' inherits the composite status of `window'.
3265 (set-window-parameter new-parent 'composite composite)
3266 (set-window-parameter
3267 ;; Give `window' the highest role of its children.
3268 window 'composite (cons 'compound
3269 (composite-lowest-child-role window)))))
3270 (when group
3271 ;; `new-parent' does not inherit the group status of `window'
3272 ;; (but make sure `window' retains it).
3273 (set-window-parameter window 'group t))
3274 (set-window-parameter new 'composite (cons 'compound 'support)))
3275 ((and group (eq support 'group))
3276 (when new-parent
3277 ;; `new-parent' inherits the group status of `window'
3278 (set-window-parameter new-parent 'group t)
3279 (set-window-parameter window 'group nil)
3280 (when composite
3281 ;; `new-parent' inherits the composite status of `window'.
3282 (set-window-parameter new-parent 'composite composite)
3283 (set-window-parameter
3284 ;; Give `window' the highest role of its children.
3285 window 'composite (cons 'group
3286 (composite-lowest-child-role window)))))
3287 (when compound
3288 ;; `new-parent' does not inherit the compound status of `window'
3289 ;; (but make sure `window' retains it).
3290 (set-window-parameter window 'compound t))
3291 (set-window-parameter new 'composite (cons 'group 'support)))
3292 ((and (eq type 'compound) (eq support 'compound))
3293 (cond
3294 (new-parent
3295 (let ((role (if (eq role 'support) 'support t)))
3296 (set-window-parameter new-parent 'composite (cons 'compound role))))
3297 ((not (compound-window-p parent))
3298 (let ((role (if (composite-support-window-p parent) 'support t)))
3299 (set-window-parameter parent 'composite (cons 'compound role)))))
3300 (when group
3301 ;; `new-parent' does not inherit the group status of `window'
3302 ;; (but make sure `window' retains it).
3303 (set-window-parameter window 'group t))
3304 (set-window-parameter window 'composite composite)
3305 (set-window-parameter new 'composite (cons 'compound 'support)))
3306 ((and (eq type 'group) (eq support 'group))
3307 (cond
3308 (new-parent
3309 (let ((role (if (eq role 'support) 'support t)))
3310 (set-window-parameter new-parent 'composite (cons 'group role))))
3311 ((not (compound-window-p parent))
3312 (let ((role (if (composite-support-window-p parent) 'support t)))
3313 (set-window-parameter parent 'composite (cons 'group role)))))
3314 (when compound
3315 ;; `new-parent' does not inherit the compound status of `window'
3316 ;; (but make sure `window' retains it).
3317 (set-window-parameter window 'compound t))
3318 (set-window-parameter window 'composite composite)
3319 (set-window-parameter new 'composite (cons 'group 'support))))
3320 new))
3322 ;;; Balancing windows.
3323 (defun balance-windows (&optional window-or-frame)
3324 "Balance the sizes of subwindows of WINDOW-OR-FRAME.
3325 WINDOW-OR-FRAME is optional and defaults to the selected frame.
3326 If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
3327 subwindows of that frame's root window. If WINDOW-OR-FRAME
3328 denots a window, balance the sizes of all subwindows of that
3329 window."
3330 (interactive)
3331 (let* ((window
3332 (cond
3333 ((or (not window-or-frame)
3334 (frame-live-p window-or-frame))
3335 (frame-root-window window-or-frame))
3336 ((or (window-live-p window-or-frame)
3337 (window-child window-or-frame))
3338 window-or-frame)
3340 (error "Not a window or frame %s" window-or-frame))))
3341 (frame (window-frame window)))
3342 ;; Balance vertically.
3343 (resize-window-reset (window-frame window))
3344 (balance-windows-1 window)
3345 (resize-window-apply frame)
3346 ;; Balance horizontally.
3347 (resize-window-reset (window-frame window) t)
3348 (balance-windows-1 window t)
3349 (resize-window-apply frame t)))
3351 (defun balance-windows-1 (window &optional horizontal)
3352 "Subroutine of `balance-windows'."
3353 (if (window-child window)
3354 (let ((sub (window-child window)))
3355 (if (window-iso-combined-p sub horizontal)
3356 (balance-windows-2 window horizontal)
3357 (let ((size (window-new-total-size window)))
3358 (while sub
3359 (resize-window-total sub size)
3360 (balance-windows-1 sub horizontal)
3361 (setq sub (window-right sub))))))))
3363 (defun balance-windows-2 (window horizontal)
3364 "Subroutine of `balance-windows-1'.
3365 WINDOW must be an iso-combination."
3366 (let* ((first (window-child window))
3367 (sub first)
3368 (number-of-children 0)
3369 (parent-size (window-new-total-size 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 (resize-window-normal sub 'ignore))
3378 (setq sub (window-right sub)))
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 (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 (resize-window-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 (resize-window-normal sub 'skip)))
3401 (setq sub (window-right sub))))
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 (resize-subwindows-skip-p window)
3409 (resize-window-total sub 1 t)
3410 (setq rest (1- rest)))
3411 (setq sub (window-right sub)))
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-size sub) 'ignore)
3418 (resize-window-total sub 1 t)
3419 (setq rest (1- rest)))
3420 (setq sub (window-right sub)))
3422 (setq sub first)
3423 (while sub
3424 ;; Record new normal sizes.
3425 (resize-window-normal
3426 sub (/ (if (eq (window-new-normal-size sub) 'ignore)
3427 (window-total-size sub horizontal)
3428 (window-new-total-size sub))
3429 (float parent-size)))
3430 ;; Recursively balance each subwindow's subwindows.
3431 (balance-windows-1 sub horizontal)
3432 (setq sub (window-right sub)))))
3434 (defun window-fixed-size-p (&optional window direction)
3435 "Return t if WINDOW cannot be resized in DIRECTION.
3436 WINDOW defaults to the selected window. DIRECTION can be
3437 nil (i.e. any), `height' or `width'."
3438 (with-current-buffer (window-buffer window)
3439 (when (and (boundp 'window-size-fixed) window-size-fixed)
3440 (not (and direction
3441 (member (cons direction window-size-fixed)
3442 '((height . width) (width . height))))))))
3444 ;;; A different solution to balance-windows.
3445 (defvar window-area-factor 1
3446 "Factor by which the window area should be over-estimated.
3447 This is used by `balance-windows-area'.
3448 Changing this globally has no effect.")
3449 (make-variable-buffer-local 'window-area-factor)
3451 (defun balance-windows-area-adjust (window delta horizontal)
3452 "Wrapper around `resize-window' with error checking.
3453 Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
3454 ;; `resize-window' may fail if delta is too large.
3455 (while (>= (abs delta) 1)
3456 (condition-case err
3457 (progn
3458 (resize-window window delta horizontal)
3459 (setq delta 0))
3460 (error
3461 ;;(message "adjust: %s" (error-message-string err))
3462 (setq delta (/ delta 2))))))
3464 (defun balance-windows-area ()
3465 "Make all visible windows the same area (approximately).
3466 See also `window-area-factor' to change the relative size of
3467 specific buffers."
3468 (interactive)
3469 (let* ((unchanged 0) (carry 0) (round 0)
3470 ;; Remove fixed-size windows.
3471 (wins (delq nil (mapcar (lambda (win)
3472 (if (not (window-fixed-size-p win)) win))
3473 (window-list nil 'nomini))))
3474 (changelog nil)
3475 next)
3476 ;; Resizing a window changes the size of surrounding windows in complex
3477 ;; ways, so it's difficult to balance them all. The introduction of
3478 ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
3479 ;; very difficult to do. `balance-window' above takes an off-line
3480 ;; approach: get the whole window tree, then balance it, then try to
3481 ;; adjust the windows so they fit the result.
3482 ;; Here, instead, we take a "local optimization" approach, where we just
3483 ;; go through all the windows several times until nothing needs to be
3484 ;; changed. The main problem with this approach is that it's difficult
3485 ;; to make sure it terminates, so we use some heuristic to try and break
3486 ;; off infinite loops.
3487 ;; After a round without any change, we allow a second, to give a chance
3488 ;; to the carry to propagate a minor imbalance from the end back to
3489 ;; the beginning.
3490 (while (< unchanged 2)
3491 ;; (message "New round")
3492 (setq unchanged (1+ unchanged) round (1+ round))
3493 (dolist (win wins)
3494 (setq next win)
3495 (while (progn (setq next (next-window next))
3496 (window-fixed-size-p next)))
3497 ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
3498 (let* ((horiz
3499 (< (car (window-edges win)) (car (window-edges next))))
3500 (areadiff (/ (- (* (window-height next) (window-width next)
3501 (buffer-local-value 'window-area-factor
3502 (window-buffer next)))
3503 (* (window-height win) (window-width win)
3504 (buffer-local-value 'window-area-factor
3505 (window-buffer win))))
3506 (max (buffer-local-value 'window-area-factor
3507 (window-buffer win))
3508 (buffer-local-value 'window-area-factor
3509 (window-buffer next)))))
3510 (edgesize (if horiz
3511 (+ (window-height win) (window-height next))
3512 (+ (window-width win) (window-width next))))
3513 (diff (/ areadiff edgesize)))
3514 (when (zerop diff)
3515 ;; Maybe diff is actually closer to 1 than to 0.
3516 (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
3517 (when (and (zerop diff) (not (zerop areadiff)))
3518 (setq diff (/ (+ areadiff carry) edgesize))
3519 ;; Change things smoothly.
3520 (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
3521 (if (zerop diff)
3522 ;; Make sure negligible differences don't accumulate to
3523 ;; become significant.
3524 (setq carry (+ carry areadiff))
3525 ;; This used `adjust-window-trailing-edge' before and uses
3526 ;; `resize-window' now. Error wrapping is still needed.
3527 (balance-windows-area-adjust win diff horiz)
3528 ;; (sit-for 0.5)
3529 (let ((change (cons win (window-edges win))))
3530 ;; If the same change has been seen already for this window,
3531 ;; we're most likely in an endless loop, so don't count it as
3532 ;; a change.
3533 (unless (member change changelog)
3534 (push change changelog)
3535 (setq unchanged 0 carry 0)))))))
3536 ;; We've now basically balanced all the windows.
3537 ;; But there may be some minor off-by-one imbalance left over,
3538 ;; so let's do some fine tuning.
3539 ;; (bw-finetune wins)
3540 ;; (message "Done in %d rounds" round)
3544 ;;; Displaying buffers.
3545 (defgroup display-buffer nil
3546 "Displaying buffers in windows."
3547 :version "24.1"
3548 :group 'windows)
3550 (defcustom display-buffer-function nil
3551 "If non-nil, function to call to display a buffer.
3552 `display-buffer' calls this function with two arguments, the
3553 buffer to display and a flag which if non-nil means that the
3554 selected window is not acceptable for displaying the buffer. It
3555 should choose or create a window, display the specified buffer in
3556 it, and return the window.
3558 Commands such as `switch-to-buffer-other-window' and
3559 `find-file-other-window' work using this function."
3560 :type '(choice
3561 (const nil)
3562 (function :tag "function"))
3563 :group 'display-buffer)
3565 (defcustom special-display-buffer-names nil
3566 "List of names of buffers that should be displayed specially.
3567 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
3568 its name is in this list, displays the buffer in a way specified
3569 by `special-display-function'. `special-display-popup-frame'
3570 \(the default for `special-display-function') usually displays
3571 the buffer in a separate frame made with the parameters specified
3572 by `special-display-frame-alist'. If `special-display-function'
3573 has been set to some other function, that function is called with
3574 the buffer as first, and nil as second argument.
3576 Alternatively, an element of this list can be specified as
3577 \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
3578 name and FRAME-PARAMETERS an alist of \(PARAMETER . VALUE) pairs.
3579 `special-display-popup-frame' will interpret such pairs as frame
3580 parameters when it creates a special frame, overriding the
3581 corresponding values from `special-display-frame-alist'.
3583 As a special case, if FRAME-PARAMETERS contains (same-window . t)
3584 `special-display-popup-frame' displays that buffer in the
3585 selected window. If FRAME-PARAMETERS contains (same-frame . t),
3586 it displays that buffer in a window on the selected frame.
3588 If `special-display-function' specifies some other function than
3589 `special-display-popup-frame', that function is called with the
3590 buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
3591 argument.
3593 Finally, an element of this list can be also specified as
3594 \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
3595 `special-display-popup-frame' will call FUNCTION with the buffer
3596 named BUFFER-NAME as first argument, and OTHER-ARGS as the
3597 second. If `special-display-function' specifies some other
3598 function, that function is called with the buffer named
3599 BUFFER-NAME as first, and the element's cdr as second argument.
3601 If this variable appears \"not to work\", because you added a
3602 name to it but the corresponding buffer is displayed in the
3603 selected window, look at the values of `same-window-buffer-names'
3604 and `same-window-regexps'. Those variables take precedence over
3605 this one.
3607 See also `special-display-regexps'."
3608 :type '(repeat
3609 (choice :tag "Buffer"
3610 :value ""
3611 (string :format "%v")
3612 (cons :tag "With parameters"
3613 :format "%v"
3614 :value ("" . nil)
3615 (string :format "%v")
3616 (repeat :tag "Parameters"
3617 (cons :format "%v"
3618 (symbol :tag "Parameter")
3619 (sexp :tag "Value"))))
3620 (list :tag "With function"
3621 :format "%v"
3622 :value ("" . nil)
3623 (string :format "%v")
3624 (function :tag "Function")
3625 (repeat :tag "Arguments" (sexp)))))
3626 :group 'display-buffer
3627 :group 'frames)
3629 ;;;###autoload
3630 (put 'special-display-buffer-names 'risky-local-variable t)
3632 (defcustom special-display-regexps nil
3633 "List of regexps saying which buffers should be displayed specially.
3634 Displaying a buffer with `display-buffer' or `pop-to-buffer', if
3635 any regexp in this list matches its name, displays it specially
3636 using `special-display-function'.
3638 The function `special-display-popup-frame' \(the default for
3639 `special-display-function') usually displays the buffer in a
3640 separate frame made with the parameters specified by
3641 `special-display-frame-alist'. If `special-display-function' has
3642 been set to some other function, that function is called with the
3643 buffer as first, and nil as second argument.
3645 Alternatively, an element of this list can be specified as
3646 \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
3647 FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
3648 `special-display-popup-frame' will then interpret these pairs as
3649 frame parameters when creating a special frame for a buffer whose
3650 name matches REGEXP, overriding the corresponding values from
3651 `special-display-frame-alist'.
3653 As a special case, if FRAME-PARAMETERS contains (same-window . t)
3654 `special-display-popup-frame' displays buffers matching REGEXP in
3655 the selected window. \(same-frame . t) in FRAME-PARAMETERS means
3656 to display such buffers in a window on the selected frame.
3658 If `special-display-function' specifies some other function than
3659 `special-display-popup-frame', that function is called with the
3660 buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
3661 as second argument.
3663 Finally, an element of this list can be also specified as
3664 \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
3665 will then call FUNCTION with the buffer whose name matched
3666 REGEXP as first, and OTHER-ARGS as second argument. If
3667 `special-display-function' specifies some other function, that
3668 function is called with the buffer whose name matched REGEXP
3669 as first, and the element's cdr as second argument.
3671 If this variable appears \"not to work\", because you added a
3672 name to it but the corresponding buffer is displayed in the
3673 selected window, look at the values of `same-window-buffer-names'
3674 and `same-window-regexps'. Those variables take precedence over
3675 this one.
3677 See also `special-display-buffer-names'."
3678 :type '(repeat
3679 (choice :tag "Buffer"
3680 :value ""
3681 (regexp :format "%v")
3682 (cons :tag "With parameters"
3683 :format "%v"
3684 :value ("" . nil)
3685 (regexp :format "%v")
3686 (repeat :tag "Parameters"
3687 (cons :format "%v"
3688 (symbol :tag "Parameter")
3689 (sexp :tag "Value"))))
3690 (list :tag "With function"
3691 :format "%v"
3692 :value ("" . nil)
3693 (regexp :format "%v")
3694 (function :tag "Function")
3695 (repeat :tag "Arguments" (sexp)))))
3696 :group 'display-buffer
3697 :group 'frames)
3699 (defun special-display-p (buffer-name)
3700 "Return non-nil if a buffer named BUFFER-NAME is displayed specially.
3701 More precisely, return t if `special-display-buffer-names' or
3702 `special-display-regexps' contain a string entry equaling or
3703 matching BUFFER-NAME. If `special-display-buffer-names' or
3704 `special-display-regexps' contain a list entry whose car equals
3705 or matches BUFFER-NAME, the return value is the cdr of that
3706 entry."
3707 (let (tmp)
3708 (cond
3709 ((not (stringp buffer-name)))
3710 ((member buffer-name special-display-buffer-names)
3712 ((setq tmp (assoc buffer-name special-display-buffer-names))
3713 (cdr tmp))
3714 ((catch 'found
3715 (dolist (regexp special-display-regexps)
3716 (cond
3717 ((stringp regexp)
3718 (when (string-match-p regexp buffer-name)
3719 (throw 'found t)))
3720 ((and (consp regexp) (stringp (car regexp))
3721 (string-match-p (car regexp) buffer-name))
3722 (throw 'found (cdr regexp))))))))))
3724 (defcustom special-display-function 'special-display-popup-frame
3725 "Function to call for displaying special buffers.
3726 This function is called with two arguments - the buffer and,
3727 optionally, a list - and should return a window displaying that
3728 buffer. The default value usually makes a separate frame for the
3729 buffer using `special-display-frame-alist' to specify the frame
3730 parameters. See the definition of `special-display-popup-frame'
3731 for how to specify such a function.
3733 A buffer is special when its name is either listed in
3734 `special-display-buffer-names' or matches a regexp in
3735 `special-display-regexps'."
3736 :type 'function
3737 :group 'frames)
3739 (defcustom same-window-buffer-names nil
3740 "List of names of buffers that should appear in the \"same\" window.
3741 `display-buffer' and `pop-to-buffer' show a buffer whose name is
3742 on this list in the selected rather than some other window.
3744 An element of this list can be a cons cell instead of just a
3745 string. In that case, the cell's car must be a string specifying
3746 the buffer name. This is for compatibility with
3747 `special-display-buffer-names'; the cdr of the cons cell is
3748 ignored.
3750 See also `same-window-regexps'."
3751 :type '(repeat (string :format "%v"))
3752 :group 'display-buffer)
3754 (defcustom same-window-regexps nil
3755 "List of regexps saying which buffers should appear in the \"same\" window.
3756 `display-buffer' and `pop-to-buffer' show a buffer whose name
3757 matches a regexp on this list in the selected rather than some
3758 other window.
3760 An element of this list can be a cons cell instead of just a
3761 string. In that case, the cell's car must be a regexp matching
3762 the buffer name. This is for compatibility with
3763 `special-display-regexps'; the cdr of the cons cell is ignored.
3765 See also `same-window-buffer-names'."
3766 :type '(repeat (regexp :format "%v"))
3767 :group 'display-buffer)
3769 (defun same-window-p (buffer-name)
3770 "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
3771 This function returns non-nil if `display-buffer' or
3772 `pop-to-buffer' would show a buffer named BUFFER-NAME in the
3773 selected rather than \(as usual\) some other window. See
3774 `same-window-buffer-names' and `same-window-regexps'."
3775 (cond
3776 ((not (stringp buffer-name)))
3777 ;; The elements of `same-window-buffer-names' can be buffer
3778 ;; names or cons cells whose cars are buffer names.
3779 ((member buffer-name same-window-buffer-names))
3780 ((assoc buffer-name same-window-buffer-names))
3781 ((catch 'found
3782 (dolist (regexp same-window-regexps)
3783 ;; The elements of `same-window-regexps' can be regexps
3784 ;; or cons cells whose cars are regexps.
3785 (when (or (and (stringp regexp)
3786 (string-match regexp buffer-name))
3787 (and (consp regexp) (stringp (car regexp))
3788 (string-match-p (car regexp) buffer-name)))
3789 (throw 'found t)))))))
3791 (defcustom pop-up-frames nil
3792 "Whether `display-buffer' should make a separate frame.
3793 If nil, never make a separate frame.
3794 If the value is `graphic-only', make a separate frame
3795 on graphic displays only.
3796 Any other non-nil value means always make a separate frame."
3797 :type '(choice
3798 (const :tag "Never" nil)
3799 (const :tag "On graphic displays only" graphic-only)
3800 (const :tag "Always" t))
3801 :group 'display-buffer
3802 :group 'frames)
3804 (defcustom display-buffer-reuse-frames nil
3805 "Non-nil means `display-buffer' should reuse frames.
3806 If the buffer in question is already displayed in a frame, raise
3807 that frame."
3808 :type 'boolean
3809 :version "21.1"
3810 :group 'display-buffer
3811 :group 'frames)
3813 (defcustom pop-up-windows t
3814 "Non-nil means `display-buffer' is allowed to make a new window.
3815 A non-empty list specifies the windows `display-buffer' will
3816 consider for splitting. The following entries are supported
3817 where \"frame\" refers to the frame chosen to display the buffer:
3819 largest ...... largest window
3820 lru .......... least recently used window
3821 selected ..... frame's selected window
3822 root ......... frame's root window
3824 The default value t stands for the list `(largest lru)'. This
3825 means that `display-buffer' will first try to split the largest
3826 window and, if that fails, the least recently used window."
3827 :type '(choice
3828 (const :tag "Disallow" nil)
3829 (const :tag "Allow" t)
3830 (repeat :tag "Preferences"
3831 (choice
3832 (const :tag "Largest" largest)
3833 (const :tag "Least Recently Used" lru)
3834 (const :tag "Selected" selected)
3835 (const :tag "Frame Root Window" root))))
3836 :group 'display-buffer)
3838 (defcustom split-window-preferred-function 'split-window-sensibly
3839 "Function called by `display-buffer' to split a window.
3840 This function is called with a window as single argument and is
3841 supposed to split that window and return the new window. If the
3842 window can (or shall) not be split, it is supposed to return nil.
3844 The default is to call the function `split-window-sensibly' which
3845 tries to split the window in a way which seems most suitable.
3846 You can customize the options `split-height-threshold' and/or
3847 `split-width-threshold' in order to have `split-window-sensibly'
3848 prefer either vertical or horizontal splitting.
3850 If you set this to any other function, bear in mind that
3851 `display-buffer' may call that function repeatedly; the option
3852 `pop-up-windows' controls which windows may become the argument
3853 of this function.
3855 The window selected at the time `display-buffer' was invoked is
3856 still selected when this function is called. Hence you can
3857 compare the window argument with the value of `selected-window'
3858 if you intend to split the selected window instead or if you do
3859 not want to split the selected window."
3860 :type 'function
3861 :version "23.1"
3862 :group 'display-buffer)
3864 (defcustom split-height-threshold 80
3865 "Minimum height for splitting a window to display a buffer.
3866 If this is an integer, `display-buffer' can split a window
3867 vertically only if it has at least this many lines. If this is
3868 nil, `display-buffer' does not split windows vertically. If a
3869 window is the only window on its frame, `display-buffer' may
3870 split it vertically disregarding the value of this variable."
3871 :type '(choice (const nil) (integer :tag "lines"))
3872 :version "23.1"
3873 :group 'display-buffer)
3875 (defcustom split-width-threshold 160
3876 "Minimum width for splitting a window to display a buffer.
3877 If this is an integer, `display-buffer' can split a window
3878 horizontally only if it has at least this many columns. If this
3879 is nil, `display-buffer' cannot split windows horizontally."
3880 :type '(choice (const nil) (integer :tag "columns"))
3881 :version "23.1"
3882 :group 'display-buffer)
3884 (defun window-sensibly-splittable-p (window &optional horizontal)
3885 "Return non-nil if `split-window-sensibly' may split WINDOW.
3886 Optional argument HORIZONTAL nil or omitted means check whether
3887 `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
3888 non-nil means check whether WINDOW may be split horizontally.
3890 WINDOW may be split vertically when the following conditions
3891 hold:
3892 - `window-size-fixed' is either nil or equals `width' for the
3893 buffer of WINDOW.
3894 - `split-height-threshold' is an integer and WINDOW is at least as
3895 high as `split-height-threshold'.
3896 - When WINDOW is split evenly, the emanating windows are at least
3897 `window-min-height' lines tall and can accommodate at least one
3898 line plus - if WINDOW has one - a mode line.
3900 WINDOW may be split horizontally when the following conditions
3901 hold:
3902 - `window-size-fixed' is either nil or equals `height' for the
3903 buffer of WINDOW.
3904 - `split-width-threshold' is an integer and WINDOW is at least as
3905 wide as `split-width-threshold'.
3906 - When WINDOW is split evenly, the emanating windows are at least
3907 `window-min-width' or two (whichever is larger) columns wide."
3908 (let* ((min-size
3909 ;; The minimum size of any popped-up window.
3910 (if horizontal
3911 (and (numberp split-width-threshold)
3912 (max (/ split-width-threshold 2)
3913 window-min-width))
3914 (and (numberp split-height-threshold)
3915 (max (/ split-height-threshold 2)
3916 window-min-height))))
3917 (parent (window-parent window))
3918 ;; Bind both of these to min-size, only one will matter.
3919 (window-min-width min-size)
3920 (window-min-height min-size))
3921 (when min-size
3922 ;; `display-buffer' is not allowed to override `window-splits'.
3923 (if (and parent (eq window-splits 'resize)
3924 (window-iso-combined-p window horizontal))
3925 (>= (- (window-total-size parent horizontal)
3926 (window-min-size parent horizontal))
3927 min-size)
3928 (and (not (window-size-fixed-p window horizontal))
3929 (>= (window-total-size window horizontal)
3930 (+ (max (window-min-size window horizontal)
3931 min-size)
3932 min-size)))))))
3934 (defun split-window-sensibly (window)
3935 "Split WINDOW in a way suitable for `display-buffer'.
3936 If `split-height-threshold' specifies an integer, WINDOW is at
3937 least `split-height-threshold' lines tall and can be split
3938 vertically, split WINDOW into two windows one above the other and
3939 return the lower window. Otherwise, if `split-width-threshold'
3940 specifies an integer, WINDOW is at least `split-width-threshold'
3941 columns wide and can be split horizontally, split WINDOW into two
3942 windows side by side and return the window on the right. If this
3943 can't be done either and WINDOW is the only window on its frame,
3944 try to split WINDOW vertically disregarding any value specified
3945 by `split-height-threshold'. If that succeeds, return the lower
3946 window. Return nil otherwise.
3948 By default `display-buffer' routines call this function to split
3949 the largest or least recently used window. To change the default
3950 beahvior customize the option `split-window-preferred-function'."
3951 (or (and (window-sensibly-splittable-p window)
3952 ;; Split window vertically.
3953 (if (window-live-p window)
3954 (with-selected-window window
3955 (split-window-vertically))
3956 (split-window
3957 window (- (max window-min-height 1
3958 (/ split-height-threshold 2))))))
3959 (and (window-sensibly-splittable-p window t)
3960 ;; Split window horizontally.
3961 (if (window-live-p window)
3962 (with-selected-window window
3963 (split-window-horizontally))
3964 (split-window
3965 window (- (max window-min-width 2
3966 (/ split-width-threshold 2)))
3967 t)))
3968 (and (frame-root-window-p window)
3969 (not (window-minibuffer-p window))
3970 ;; If WINDOW is the only window on its frame and is not the
3971 ;; minibuffer window, try to split it vertically disregarding
3972 ;; the value of `split-height-threshold'.
3973 (let ((split-height-threshold 0))
3974 (when (window-sensibly-splittable-p window)
3975 (with-selected-window window
3976 (split-window-vertically)))))))
3978 ;; Minibuffer-only frames should be documented better. They really
3979 ;; deserve a separate section in the manual. Also
3980 ;; `last-nonminibuffer-frame' is nowhere documented in the manual.
3981 (defun window--usable-frame (frame)
3982 "Return FRAME if it is live and not a minibuffer-only frame."
3983 (and (frame-live-p frame)
3984 (not (window-minibuffer-p (frame-root-window frame)))
3985 frame))
3987 (defcustom even-window-heights t
3988 "If non-nil `display-buffer' will try to even window heights.
3989 Otherwise `display-buffer' will leave the window configuration
3990 alone. Heights are evened only when `display-buffer' reuses a
3991 window that appears above or below the selected window."
3992 :type 'boolean
3993 :group 'display-buffer)
3995 (defvar display-buffer-window-and-buffer nil
3996 "Window used by `display-buffer' and related information.
3997 After `display-buffer' displays a buffer in some window this
3998 variable is a cons cell whose car denotes the window used to
3999 display the buffer. The cdr is either nil \(which means the same
4000 buffer was displayed before in that window), t \(which means that
4001 window was created by `display-buffer'), or the buffer displayed
4002 previously in that window.
4004 This variable holds the value produced by the last invocation of
4005 `display-buffer' and is nil if there was no such invocation.")
4007 (defsubst display-buffer-window-and-buffer (window &optional value)
4008 "Set `display-buffer-window-and-buffer' to cons of WINDOW and VALUE.
4009 VALUE defaults to nil."
4010 (setq display-buffer-window-and-buffer (cons window value)))
4012 (defun window--display-buffer-in-window (buffer window &optional dedicated)
4013 "Display BUFFER in WINDOW and maybe raise its frame.
4014 If DEDICATED is non-nil, use it to set the `window-dedicated-p'
4015 flag of WINDOW. Return WINDOW."
4016 (when (and (buffer-live-p buffer) (window-live-p window))
4017 (set-window-buffer window buffer)
4018 (when dedicated
4019 (set-window-dedicated-p window dedicated))
4020 (window--raise-window-frame window)))
4022 (defun window--reuse-window (window buffer not-this-window)
4023 "Reuse WINDOW for displaying BUFFER.
4024 If NOT-THIS-WINDOW is non-nil and WINDOW is the selected window
4025 do nothing and return nil. Return nil also if WINDOW is
4026 dedicated to its buffer. Otherwise, display buffer in WINDOW,
4027 even window heights if requested, and return WINDOW."
4028 (unless (or (and not-this-window (eq window (selected-window)))
4029 (and (window-dedicated-p window)
4030 (not (eq (window-buffer window) buffer))))
4031 ;; Save `quit-restore' information: It's absolutely necessary to do
4032 ;; this _before_ handling `even-window-heights'. Do not overwrite
4033 ;; an existing value.
4034 (unless (window-parameter window 'quit-restore)
4035 (set-window-parameter
4036 window 'quit-restore
4037 (list (window-buffer window) (window-start window)
4038 (window-point window) buffer (window-total-size window)
4039 (selected-window))))
4040 ;; Handle `even-window-heights' option.
4041 (when (and even-window-heights
4042 ;; Not needed (but often WINDOW is the selected window):
4043 (not (eq window (selected-window)))
4044 ;; Don't resize minibuffer windows.
4045 (not (window-minibuffer-p))
4046 ;; Resize iff the selected window is higher than WINDOW.
4047 (> (window-total-size) (window-total-size window))
4048 ;; Resize vertical combinations only.
4049 (and (window-parent) (window-iso-combined-p (window-parent))
4050 ;; WINDOW must be adjacent to the selected one.
4051 (or (eq window (window-prev)) (eq window (window-next)))))
4052 ;; Don't throw an error if we can't even window heights for
4053 ;; whatever reason. In any case, enlarging the selected window
4054 ;; might fail anyway if there are other windows above or below
4055 ;; WINDOW and the selected one. But for a simple two windows
4056 ;; configuration the present behavior is good enough so why care?
4057 (condition-case nil
4058 (resize-window
4059 window (/ (- (window-total-size window) (window-total-size)) 2))
4060 (error nil)))
4061 (display-buffer-window-and-buffer window (window-buffer window))
4062 ;; Display BUFFER in WINDOW.
4063 (window--display-buffer-in-window buffer window)))
4065 ;; This variable should be probably documented in the manual.
4066 (defvar display-buffer-mark-dedicated nil
4067 "If non-nil, `display-buffer' marks the windows it creates as dedicated.
4068 The actual non-nil value of this variable will be copied to the
4069 `window-dedicated-p' flag.")
4071 (defun window--pop-up-window (buffer frame)
4072 "Pop up a new window for BUFFER on FRAME.
4073 If FRAME cannot be split, try to pop up a window on the last
4074 nonminibuffer frame instead. Return the new window if splitting
4075 succeeded, nil otherwise.
4077 See the documentation of the variable `pop-up-windows' how to
4078 choose the window to be split for popping up the new one."
4079 (when (or (and (window--usable-frame frame)
4080 (not (frame-parameter frame 'unsplittable)))
4081 (and (setq frame (last-nonminibuffer-frame))
4082 (window--usable-frame frame)
4083 (not (frame-parameter frame 'unsplittable))))
4084 (let* ((selected-window (selected-window))
4085 (windows (window-list-1 (frame-first-window frame) 'nomini frame))
4086 (probes (if (eq pop-up-windows t) '(largest lru) pop-up-windows))
4087 window new-window)
4088 (catch 'done
4089 ;; Try to find a window suiting `pop-up-windows'.
4090 (dolist (probe probes)
4091 (setq window
4092 (cond
4093 ((eq probe 'selected)
4094 (frame-selected-window frame))
4095 ((eq probe 'largest)
4096 (get-largest-window frame t))
4097 ((eq probe 'lru)
4098 (get-lru-window frame t))
4099 ((eq probe 'root)
4100 (frame-root-window frame))))
4101 ;; Do not consider `window' again.
4102 (setq windows (remq window windows))
4103 (when (and (eq (window-frame window) frame)
4104 (not (window-minibuffer-p window))
4105 (not (frame-parameter frame 'unsplittable))
4106 (setq new-window
4107 ;; Since `split-window-preferred-function' might
4108 ;; throw an error use `condition-case'.
4109 (condition-case nil
4110 (funcall split-window-preferred-function window)
4111 (error nil))))
4112 (set-window-parameter
4113 new-window 'quit-restore (list t selected-window))
4114 (display-buffer-window-and-buffer new-window t)
4115 (window--display-buffer-in-window
4116 buffer new-window display-buffer-mark-dedicated)
4117 (throw 'done new-window)))))))
4119 (defun window--pop-up-frame (buffer)
4120 "Pop up a new frame for displaying BUFFER.
4121 Return the window displaying BUFFER if creating the new frame was
4122 successful, nil otherwise."
4123 (let* ((selected-window (selected-window))
4124 (frame (funcall pop-up-frame-function))
4125 (window (when frame (frame-selected-window frame))))
4126 (when window
4127 (display-buffer-window-and-buffer window t)
4128 (window--display-buffer-in-window
4129 buffer window display-buffer-mark-dedicated)
4130 ;; Make sure that quitting is allowed to delete the frame.
4131 (set-window-parameter
4132 window 'quit-restore (list t selected-window))
4133 window)))
4135 (defun window--raise-window-frame (&optional window)
4136 "Raise the frame containing WINDOW.
4137 WINDOW must be a live window and defaults to the selected one.
4138 Return WINDOW.
4140 This function does not raise the selected or an invisible frame."
4141 (setq window (normalize-live-window window))
4142 (let* ((frame (window-frame window))
4143 (visible (frame-visible-p frame)))
4144 (unless (or (not visible)
4145 ;; Assume the selected frame is already visible enough.
4146 (eq frame (selected-frame))
4147 ;; Assume the frame from which we invoked the minibuffer
4148 ;; is visible.
4149 (and (minibuffer-window-active-p (selected-window))
4150 (eq frame (window-frame (minibuffer-selected-window)))))
4151 (raise-frame frame))
4152 window))
4154 (defun display-buffer (buffer-or-name &optional not-this-window frame)
4155 "Make buffer BUFFER-OR-NAME appear in some window but don't select it.
4156 BUFFER-OR-NAME must be a buffer or the name of an existing
4157 buffer. Return the window chosen to display BUFFER-OR-NAME or
4158 nil if no such window is found.
4160 Optional argument NOT-THIS-WINDOW non-nil means display the
4161 buffer in a window other than the selected one, even if it is
4162 already displayed in the selected window.
4164 Optional argument FRAME specifies which frames to investigate
4165 when the specified buffer is already displayed. If the buffer is
4166 already displayed in some window on one of these frames simply
4167 return that window. Possible values of FRAME are:
4169 `visible' - consider windows on all visible frames.
4171 0 - consider windows on all visible or iconified frames.
4173 t - consider windows on all frames.
4175 A specific frame - consider windows on that frame only.
4177 nil - consider windows on the selected frame \(actually the
4178 last non-minibuffer frame\) only. If, however, either
4179 `display-buffer-reuse-frames' or `pop-up-frames' is non-nil
4180 \(non-nil and not graphic-only on a text-only terminal),
4181 consider all visible or iconified frames."
4182 (interactive "BDisplay buffer:\nP")
4183 (let* ((buffer (normalize-live-buffer buffer-or-name))
4184 (buffer-name (buffer-name buffer))
4185 (can-use-selected-window
4186 ;; We can reuse the selected window unless NOT-THIS-WINDOW is
4187 ;; non-nil, or the selected window is either dedicated to its
4188 ;; buffer, or it is a `minibuffer-window'.
4189 (not (or not-this-window
4190 (window-dedicated-p)
4191 (window-minibuffer-p))))
4192 ;; On text-only terminals do not pop up a new frame when
4193 ;; `pop-up-frames' equals graphic-only.
4194 (pop-up-frame (if (eq pop-up-frames 'graphic-only)
4195 (display-graphic-p)
4196 pop-up-frames))
4197 ;; `frame-to-use' is the frame where to show `buffer' - either
4198 ;; the selected frame or the last nonminibuffer frame.
4199 (frame-to-use
4200 (or (window--usable-frame (selected-frame))
4201 (window--usable-frame (last-nonminibuffer-frame))))
4202 ;; `window-to-use' is the window we use for showing `buffer'.
4203 window-to-use)
4204 (cond
4205 (display-buffer-function
4206 ;; Let `display-buffer-function' do the job.
4207 (funcall display-buffer-function buffer not-this-window))
4208 ((and (not not-this-window) (eq (window-buffer) buffer))
4209 ;; The selected window already displays BUFFER and NOT-THIS-WINDOW
4210 ;; is nil, so reuse the selected window.
4211 (selected-window))
4212 ((and can-use-selected-window (same-window-p buffer-name))
4213 ;; If the buffer's name tells us to use the selected window do so.
4214 (display-buffer-window-and-buffer (selected-window))
4215 (window--display-buffer-in-window buffer (selected-window)))
4216 ((let ((frames (or frame
4217 (and (or pop-up-frame
4218 display-buffer-reuse-frames
4219 (not (last-nonminibuffer-frame)))
4221 (last-nonminibuffer-frame))))
4222 (setq window-to-use
4223 (catch 'found
4224 ;; Search frames for a window displaying BUFFER. Return
4225 ;; the selected window only if we are allowed to do so.
4226 (dolist (window (get-buffer-window-list buffer 'nomini frames))
4227 (when (or can-use-selected-window
4228 (not (eq (selected-window) window)))
4229 (throw 'found window))))))
4230 ;; The buffer is already displayed in some window; use that.
4231 (display-buffer-window-and-buffer window-to-use)
4232 (window--raise-window-frame window-to-use))
4233 ((and special-display-function
4234 ;; `special-display-p' returns either t or a list of frame
4235 ;; parameters to pass to `special-display-function'.
4236 (let ((pars (special-display-p buffer-name)))
4237 (when pars
4238 (funcall
4239 special-display-function buffer (and (listp pars) pars))))))
4240 ((and (or pop-up-frame (not frame-to-use))
4241 (window--pop-up-frame buffer)))
4242 ((and pop-up-windows
4243 ;; Try popping up a new window.
4244 (window--pop-up-window buffer frame-to-use)))
4245 ((window--reuse-window
4246 ;; Try reusing least recently used window.
4247 (get-lru-window frame-to-use) buffer not-this-window))
4248 ((window--reuse-window
4249 ;; Try reusing some visible window showing BUFFER.
4250 (get-buffer-window buffer 'visible) buffer not-this-window))
4251 ((window--reuse-window
4252 ;; Try reusing largest visible window.
4253 (get-largest-window 'visible) buffer not-this-window))
4254 ((window--reuse-window
4255 ;; Try reusing some window showing BUFFER on any visible or
4256 ;; iconified frame.
4257 (get-buffer-window buffer 0) buffer not-this-window))
4258 ((window--reuse-window
4259 ;; Try reusing largest window on any visible or iconified frame.
4260 (get-largest-window 0) buffer not-this-window))
4261 ;; As a last resort try popping up a new frame.
4262 ((window--pop-up-frame buffer)))))
4264 (defun pop-to-buffer (buffer-or-name &optional other-window norecord)
4265 "Select buffer BUFFER-OR-NAME in some window, preferably a different one.
4266 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
4267 nil. If BUFFER-OR-NAME is a string not naming an existent
4268 buffer, create a buffer with that name. If BUFFER-OR-NAME is
4269 nil, choose some other buffer. Return the buffer specified by
4270 BUFFER-OR-NAME.
4272 If optional second arg OTHER-WINDOW is non-nil, insist on finding
4273 another window even if the specified buffer is already visible in
4274 the selected window, and ignore the options `same-window-regexps'
4275 and `same-window-buffer-names'.
4277 Optional argument NORECORD non-nil means do not put the buffer
4278 specified by BUFFER-OR-NAME at the front of the buffer list and
4279 do not make the window displaying it the most recently selected
4280 one.
4282 This uses the function `display-buffer' as a subroutine; see the
4283 documentation of `display-buffer' for additional customization
4284 information."
4285 (let ((buffer
4286 ;; FIXME: This behavior is carried over from the previous C
4287 ;; version of pop-to-buffer, but really we should use just
4288 ;; `get-buffer' here.
4289 (if (null buffer-or-name)
4290 (other-buffer (current-buffer))
4291 (or (get-buffer buffer-or-name)
4292 (let ((buf (get-buffer-create buffer-or-name)))
4293 (set-buffer-major-mode buf)
4294 buf))))
4295 (old-frame (selected-frame))
4296 new-window new-frame)
4297 (set-buffer buffer)
4298 (setq new-window (display-buffer buffer other-window))
4299 ;; Select the window chosen.
4300 (select-window new-window norecord)
4301 (setq new-frame (window-frame new-window))
4302 (unless (eq new-frame old-frame)
4303 ;; `display-buffer' has chosen another frame, make sure it gets
4304 ;; input focus and is risen.
4305 (select-frame-set-input-focus new-frame))
4306 buffer))
4308 (defun read-buffer-to-switch (prompt)
4309 "Read the name of a buffer to switch to and return as a string.
4310 It is intended for `switch-to-buffer' family of commands since they
4311 need to omit the name of current buffer from the list of completions
4312 and default values."
4313 (let ((rbts-completion-table (internal-complete-buffer-except)))
4314 (minibuffer-with-setup-hook
4315 (lambda ()
4316 (setq minibuffer-completion-table rbts-completion-table)
4317 ;; Since rbts-completion-table is built dynamically, we
4318 ;; can't just add it to the default value of
4319 ;; icomplete-with-completion-tables, so we add it
4320 ;; here manually.
4321 (if (and (boundp 'icomplete-with-completion-tables)
4322 (listp icomplete-with-completion-tables))
4323 (set (make-local-variable 'icomplete-with-completion-tables)
4324 (cons rbts-completion-table
4325 icomplete-with-completion-tables))))
4326 (read-buffer prompt (other-buffer (current-buffer))
4327 (confirm-nonexistent-file-or-buffer)))))
4329 (defun switch-to-buffer (buffer-or-name &optional norecord)
4330 "Switch to buffer BUFFER-OR-NAME in the selected window.
4331 If BUFFER-OR-NAME does not identify an existing buffer, then this
4332 function creates a buffer with that name.
4334 If called interactively, prompt for the buffer name using the
4335 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
4336 determines whether to request confirmation before creating a new
4337 buffer.
4339 When called from Lisp, BUFFER-OR-NAME may be a buffer, a string
4340 \(a buffer name), or nil. If BUFFER-OR-NAME is nil, then this
4341 function chooses a buffer using `other-buffer'.
4343 Optional argument NORECORD non-nil means do not put the buffer
4344 specified by BUFFER-OR-NAME at the front of the buffer list and
4345 do not make the window displaying it the most recently selected
4346 one.
4348 WARNING: Do NOT use this function to work on another buffer
4349 temporarily within a Lisp program! Use `with-current-buffer'
4350 instead. That avoids messing with the window-buffer
4351 correspondences."
4352 (interactive
4353 (list (read-buffer-to-switch "Switch to buffer: ")))
4354 (let ((buffer (when buffer-or-name (get-buffer buffer-or-name))))
4355 (cond
4356 ((eq buffer (window-buffer))
4357 (unless norecord
4358 (select-window (selected-window)))
4359 (set-buffer buffer))
4360 ((or (window-minibuffer-p) (eq (window-dedicated-p) t))
4361 ;; Cannot switch to another buffer in a minibuffer or strongly
4362 ;; dedicated window. Call `pop-to-buffer' instead.
4363 (pop-to-buffer buffer nil norecord))
4365 (unless buffer
4366 (if buffer-or-name
4367 ;; Create a buffer named BUFFER-OR-NAME.
4368 (progn
4369 (setq buffer (get-buffer-create buffer-or-name))
4370 (set-buffer-major-mode buffer))
4371 ;; Use other buffer.
4372 (setq buffer (other-buffer (current-buffer)))))
4373 (set-window-buffer nil buffer)
4374 (unless norecord
4375 (select-window (selected-window)))
4376 (set-buffer buffer)))))
4378 (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
4379 "Switch to buffer BUFFER-OR-NAME in another window.
4380 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
4381 nil. Return the buffer switched to.
4383 If called interactively, prompt for the buffer name using the
4384 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
4385 determines whether to request confirmation before creating a new
4386 buffer.
4388 If BUFFER-OR-NAME is a string and does not identify an existing
4389 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
4390 nil, switch to the buffer returned by `other-buffer'.
4392 Optional argument NORECORD non-nil means do not put the buffer
4393 specified by BUFFER-OR-NAME at the front of the buffer list and
4394 do not make the window displaying it the most recently selected
4395 one.
4397 This uses the function `display-buffer' as a subroutine; see its
4398 documentation for additional customization information."
4399 (interactive
4400 (list (read-buffer-to-switch "Switch to buffer in other window: ")))
4401 (let ((pop-up-windows (or pop-up-windows t))
4402 same-window-buffer-names same-window-regexps)
4403 (pop-to-buffer buffer-or-name t norecord)))
4405 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
4406 "Switch to buffer BUFFER-OR-NAME in another frame.
4407 BUFFER-OR-NAME may be a buffer, a string \(a buffer name), or
4408 nil. Return the buffer switched to.
4410 If called interactively, prompt for the buffer name using the
4411 minibuffer. The variable `confirm-nonexistent-file-or-buffer'
4412 determines whether to request confirmation before creating a new
4413 buffer.
4415 If BUFFER-OR-NAME is a string and does not identify an existing
4416 buffer, create a new buffer with that name. If BUFFER-OR-NAME is
4417 nil, switch to the buffer returned by `other-buffer'.
4419 Optional argument NORECORD non-nil means do not put the buffer
4420 specified by BUFFER-OR-NAME at the front of the buffer list and
4421 do not make the window displaying it the most recently selected
4422 one.
4424 This uses the function `display-buffer' as a subroutine; see its
4425 documentation for additional customization information."
4426 (interactive
4427 (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
4428 (let ((pop-up-frames t)
4429 same-window-buffer-names same-window-regexps)
4430 (pop-to-buffer buffer-or-name t norecord)))
4432 (defun set-window-text-height (window height)
4433 "Set the height in lines of the text display area of WINDOW to HEIGHT.
4434 WINDOW must be a live window. HEIGHT doesn't include the mode
4435 line or header line, if any, or any partial-height lines in the
4436 text display area.
4438 Note that the current implementation of this function cannot
4439 always set the height exactly, but attempts to be conservative,
4440 by allocating more lines than are actually needed in the case
4441 where some error may be present."
4442 (setq window (normalize-live-window window))
4443 (let ((delta (- height (window-text-height window))))
4444 (unless (zerop delta)
4445 ;; Setting window-min-height to a value like 1 can lead to very
4446 ;; bizarre displays because it also allows Emacs to make *other*
4447 ;; windows 1-line tall, which means that there's no more space for
4448 ;; the modeline.
4449 (let ((window-min-height (min 2 height))) ; One text line plus a modeline.
4450 (resize-window window delta)))))
4452 (defun enlarge-window-horizontally (delta)
4453 "Make selected window DELTA wider.
4454 Interactively, if no argument is given, make selected window one
4455 column wider."
4456 (interactive "p")
4457 (enlarge-window delta t))
4459 (defun shrink-window-horizontally (delta)
4460 "Make selected window DELTA narrower.
4461 Interactively, if no argument is given, make selected window one
4462 column narrower."
4463 (interactive "p")
4464 (shrink-window delta t))
4466 (defun count-screen-lines (&optional beg end count-final-newline window)
4467 "Return the number of screen lines in the region.
4468 The number of screen lines may be different from the number of actual lines,
4469 due to line breaking, display table, etc.
4471 Optional arguments BEG and END default to `point-min' and `point-max'
4472 respectively.
4474 If region ends with a newline, ignore it unless optional third argument
4475 COUNT-FINAL-NEWLINE is non-nil.
4477 The optional fourth argument WINDOW specifies the window used for obtaining
4478 parameters such as width, horizontal scrolling, and so on. The default is
4479 to use the selected window's parameters.
4481 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
4482 regardless of which buffer is displayed in WINDOW. This makes possible to use
4483 `count-screen-lines' in any buffer, whether or not it is currently displayed
4484 in some window."
4485 (unless beg
4486 (setq beg (point-min)))
4487 (unless end
4488 (setq end (point-max)))
4489 (if (= beg end)
4491 (save-excursion
4492 (save-restriction
4493 (widen)
4494 (narrow-to-region (min beg end)
4495 (if (and (not count-final-newline)
4496 (= ?\n (char-before (max beg end))))
4497 (1- (max beg end))
4498 (max beg end)))
4499 (goto-char (point-min))
4500 (1+ (vertical-motion (buffer-size) window))))))
4502 (defun window-buffer-height (window)
4503 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
4504 (with-current-buffer (window-buffer window)
4505 (max 1
4506 (count-screen-lines (point-min) (point-max)
4507 ;; If buffer ends with a newline, ignore it when
4508 ;; counting height unless point is after it.
4509 (eobp)
4510 window))))
4512 ;;; Resizing buffers to fit their contents exactly.
4513 (defun fit-window-to-buffer (&optional window max-height min-height ignore)
4514 "Adjust height of WINDOW to display its buffer's contents exactly.
4515 WINDOW can be any live window and defaults to the selected one.
4517 Optional argument MAX-HEIGHT specifies the maximum height of
4518 WINDOW and defaults to the height of WINDOW's frame. Optional
4519 argument MIN-HEIGHT specifies the minimum height of WINDOW and
4520 defaults to `window-min-height'. Both, MAX-HEIGHT and MIN-HEIGHT
4521 are specified in lines and include the mode line and header line,
4522 if any.
4524 Optional argument IGNORE non-nil means ignore restrictions
4525 imposed by `window-min-height' and `window-min-width' on the size
4526 of WINDOW.
4528 Return the number of lines by which WINDOW was enlarged or
4529 shrunk. If an error occurs during resizing, return nil but don't
4530 signal an error.
4532 Note that even if this function makes WINDOW large enough to show
4533 _all_ lines of its buffer you might not see the first lines when
4534 WINDOW was scrolled."
4535 (interactive)
4536 ;; Do all the work in WINDOW and its buffer and restore the selected
4537 ;; window and the current buffer when we're done.
4538 (setq window (normalize-live-window window))
4539 ;; Can't resize a full height or fixed-size window.
4540 (unless (or (window-size-fixed-p window)
4541 (window-full-height-p window))
4542 ;; `with-selected-window' should orderly restore the current buffer.
4543 (with-selected-window window
4544 ;; We are in WINDOW's buffer now.
4545 (let* ( ;; Adjust MIN-HEIGHT.
4546 (min-height
4547 (if ignore
4548 (window-min-size window nil window)
4549 (max (or min-height window-min-height)
4550 window-safe-min-height)))
4551 (max-window-height
4552 (window-total-size (frame-root-window window)))
4553 ;; Adjust MAX-HEIGHT.
4554 (max-height
4555 (if (or ignore (not max-height))
4556 max-window-height
4557 (min max-height max-window-height)))
4558 ;; Make `desired-height' the height necessary to show
4559 ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
4560 ;; and MAX-HEIGHT.
4561 (desired-height
4562 (max
4563 (min
4564 (+ (count-screen-lines)
4565 ;; For non-minibuffers count the mode line, if any.
4566 (if (and (not (window-minibuffer-p window))
4567 mode-line-format)
4570 ;; Count the header line, if any.
4571 (if header-line-format 1 0))
4572 max-height)
4573 min-height))
4574 (desired-delta
4575 (- desired-height (window-total-size window)))
4576 (delta
4577 (if (> desired-delta 0)
4578 (min desired-delta
4579 (window-max-delta window nil window))
4580 (max desired-delta
4581 (- (window-min-delta window nil window))))))
4582 ;; This `condition-case' shouldn't be necessary, but who knows?
4583 (condition-case nil
4584 (if (zerop delta)
4585 ;; Return zero if DELTA became zero in the proces.
4587 ;; Don't try to redisplay with the cursor at the end on its
4588 ;; own line--that would force a scroll and spoil things.
4589 (when (and (eobp) (bolp) (not (bobp)))
4590 ;; It's silly to put `point' at the end of the previous
4591 ;; line and so maybe force horizontal scrolling.
4592 (set-window-point window (line-beginning-position 0)))
4593 ;; Call `resize-window' with IGNORE argument equal WINDOW.
4594 (resize-window window delta nil window)
4595 ;; Check if the last line is surely fully visible. If
4596 ;; not, enlarge the window.
4597 (let ((end (save-excursion
4598 (goto-char (point-max))
4599 (when (and (bolp) (not (bobp)))
4600 ;; Don't include final newline.
4601 (backward-char 1))
4602 (when truncate-lines
4603 ;; If line-wrapping is turned off, test the
4604 ;; beginning of the last line for
4605 ;; visibility instead of the end, as the
4606 ;; end of the line could be invisible by
4607 ;; virtue of extending past the edge of the
4608 ;; window.
4609 (forward-line 0))
4610 (point))))
4611 (set-window-vscroll window 0)
4612 ;; This loop might in some rare pathological cases raise
4613 ;; an error - another reason for the `condition-case'.
4614 (while (and (< desired-height max-height)
4615 (= desired-height (window-total-size))
4616 (not (pos-visible-in-window-p end)))
4617 (resize-window window 1 nil window)
4618 (setq desired-height (1+ desired-height)))))
4619 (error (setq delta nil)))
4620 delta))))
4622 (defun window-safely-shrinkable-p (&optional window)
4623 "Return t if WINDOW can be shrunk without shrinking other windows.
4624 WINDOW defaults to the selected window."
4625 (with-selected-window (or window (selected-window))
4626 (let ((edges (window-edges)))
4627 (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
4628 (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
4630 (defun shrink-window-if-larger-than-buffer (&optional window)
4631 "Shrink height of WINDOW if its buffer doesn't need so many lines.
4632 More precisely, shrink WINDOW vertically to be as small as
4633 possible, while still showing the full contents of its buffer.
4634 WINDOW defaults to the selected window.
4636 Do not shrink WINDOW to less than `window-min-height' lines. Do
4637 nothing if the buffer contains more lines than the present window
4638 height, or if some of the window's contents are scrolled out of
4639 view, or if shrinking this window would also shrink another
4640 window, or if the window is the only window of its frame.
4642 Return non-nil if the window was shrunk, nil otherwise."
4643 (interactive)
4644 (setq window (normalize-live-window window))
4645 ;; Make sure that WINDOW is vertically combined and `point-min' is
4646 ;; visible (for whatever reason that's needed). The remaining issues
4647 ;; should be taken care of by `fit-window-to-buffer'.
4648 (when (and (window-iso-combined-p window)
4649 (pos-visible-in-window-p (point-min) window))
4650 (fit-window-to-buffer window (window-total-size window))))
4652 (defun kill-buffer-and-window ()
4653 "Kill the current buffer and delete the selected window."
4654 (interactive)
4655 (let ((window-to-delete (selected-window))
4656 (buffer-to-kill (current-buffer))
4657 (delete-window-hook (lambda ()
4658 (condition-case nil
4659 (delete-window)
4660 (error nil)))))
4661 (unwind-protect
4662 (progn
4663 (add-hook 'kill-buffer-hook delete-window-hook t t)
4664 (if (kill-buffer (current-buffer))
4665 ;; If `delete-window' failed before, we rerun it to regenerate
4666 ;; the error so it can be seen in the echo area.
4667 (when (eq (selected-window) window-to-delete)
4668 (delete-window))))
4669 ;; If the buffer is not dead for some reason (probably because
4670 ;; of a `quit' signal), remove the hook again.
4671 (condition-case nil
4672 (with-current-buffer buffer-to-kill
4673 (remove-hook 'kill-buffer-hook delete-window-hook t))
4674 (error nil)))))
4676 (defun quit-window (&optional kill window)
4677 "Quit WINDOW and bury its buffer.
4678 With a prefix argument, kill the buffer instead. WINDOW defaults
4679 to the selected window.
4681 If WINDOW is non-nil, dedicated, or a minibuffer window, delete
4682 it and, if it's alone on its frame, its frame too. Otherwise, or
4683 if deleting WINDOW fails in any of the preceding cases, display
4684 another buffer in WINDOW using `switch-to-buffer'.
4686 Optional argument KILL non-nil means kill WINDOW's buffer.
4687 Otherwise, bury WINDOW's buffer, see `bury-buffer'."
4688 (interactive "P")
4689 (let ((buffer (window-buffer window)))
4690 (if (or window
4691 (window-minibuffer-p window)
4692 (window-dedicated-p window))
4693 ;; WINDOW is either non-nil, a minibuffer window, or dedicated;
4694 ;; try to delete it.
4695 (let* ((window (or window (selected-window)))
4696 (frame (window-frame window)))
4697 (if (frame-root-window-p window)
4698 ;; WINDOW is alone on its frame.
4699 (delete-frame frame)
4700 ;; There are other windows on its frame, delete WINDOW.
4701 (delete-window window)))
4702 ;; Otherwise, switch to another buffer in the selected window.
4703 (switch-to-buffer nil))
4705 ;; Deal with the buffer.
4706 (if kill
4707 (kill-buffer buffer)
4708 (bury-buffer buffer))))
4710 (defvar recenter-last-op nil
4711 "Indicates the last recenter operation performed.
4712 Possible values: `top', `middle', `bottom', integer or float numbers.")
4714 (defcustom recenter-positions '(middle top bottom)
4715 "Cycling order for `recenter-top-bottom'.
4716 A list of elements with possible values `top', `middle', `bottom',
4717 integer or float numbers that define the cycling order for
4718 the command `recenter-top-bottom'.
4720 Top and bottom destinations are `scroll-margin' lines the from true
4721 window top and bottom. Middle redraws the frame and centers point
4722 vertically within the window. Integer number moves current line to
4723 the specified absolute window-line. Float number between 0.0 and 1.0
4724 means the percentage of the screen space from the top. The default
4725 cycling order is middle -> top -> bottom."
4726 :type '(repeat (choice
4727 (const :tag "Top" top)
4728 (const :tag "Middle" middle)
4729 (const :tag "Bottom" bottom)
4730 (integer :tag "Line number")
4731 (float :tag "Percentage")))
4732 :version "23.2"
4733 :group 'windows)
4735 (defun recenter-top-bottom (&optional arg)
4736 "Move current buffer line to the specified window line.
4737 With no prefix argument, successive calls place point according
4738 to the cycling order defined by `recenter-positions'.
4740 A prefix argument is handled like `recenter':
4741 With numeric prefix ARG, move current line to window-line ARG.
4742 With plain `C-u', move current line to window center."
4743 (interactive "P")
4744 (cond
4745 (arg (recenter arg)) ; Always respect ARG.
4747 (setq recenter-last-op
4748 (if (eq this-command last-command)
4749 (car (or (cdr (member recenter-last-op recenter-positions))
4750 recenter-positions))
4751 (car recenter-positions)))
4752 (let ((this-scroll-margin
4753 (min (max 0 scroll-margin)
4754 (truncate (/ (window-body-height) 4.0)))))
4755 (cond ((eq recenter-last-op 'middle)
4756 (recenter))
4757 ((eq recenter-last-op 'top)
4758 (recenter this-scroll-margin))
4759 ((eq recenter-last-op 'bottom)
4760 (recenter (- -1 this-scroll-margin)))
4761 ((integerp recenter-last-op)
4762 (recenter recenter-last-op))
4763 ((floatp recenter-last-op)
4764 (recenter (round (* recenter-last-op (window-height))))))))))
4766 (define-key global-map [?\C-l] 'recenter-top-bottom)
4768 (defun move-to-window-line-top-bottom (&optional arg)
4769 "Position point relative to window.
4771 With a prefix argument ARG, acts like `move-to-window-line'.
4773 With no argument, positions point at center of window.
4774 Successive calls position point at positions defined
4775 by `recenter-positions'."
4776 (interactive "P")
4777 (cond
4778 (arg (move-to-window-line arg)) ; Always respect ARG.
4780 (setq recenter-last-op
4781 (if (eq this-command last-command)
4782 (car (or (cdr (member recenter-last-op recenter-positions))
4783 recenter-positions))
4784 (car recenter-positions)))
4785 (let ((this-scroll-margin
4786 (min (max 0 scroll-margin)
4787 (truncate (/ (window-body-height) 4.0)))))
4788 (cond ((eq recenter-last-op 'middle)
4789 (call-interactively 'move-to-window-line))
4790 ((eq recenter-last-op 'top)
4791 (move-to-window-line this-scroll-margin))
4792 ((eq recenter-last-op 'bottom)
4793 (move-to-window-line (- -1 this-scroll-margin)))
4794 ((integerp recenter-last-op)
4795 (move-to-window-line recenter-last-op))
4796 ((floatp recenter-last-op)
4797 (move-to-window-line (round (* recenter-last-op (window-height))))))))))
4799 (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
4801 ;;; Scrolling commands.
4803 ;;; Scrolling commands which does not signal errors at top/bottom
4804 ;;; of buffer at first key-press (instead moves to top/bottom
4805 ;;; of buffer).
4807 (defcustom scroll-error-top-bottom nil
4808 "Move point to top/bottom of buffer before signalling a scrolling error.
4809 A value of nil means just signal an error if no more scrolling possible.
4810 A value of t means point moves to the beginning or the end of the buffer
4811 \(depending on scrolling direction) when no more scrolling possible.
4812 When point is already on that position, then signal an error."
4813 :type 'boolean
4814 :group 'scrolling
4815 :version "24.1")
4817 (defun scroll-up-command (&optional arg)
4818 "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
4819 If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
4820 scroll window further, move cursor to the bottom line.
4821 When point is already on that position, then signal an error.
4822 A near full screen is `next-screen-context-lines' less than a full screen.
4823 Negative ARG means scroll downward.
4824 If ARG is the atom `-', scroll downward by nearly full screen."
4825 (interactive "^P")
4826 (cond
4827 ((null scroll-error-top-bottom)
4828 (scroll-up arg))
4829 ((eq arg '-)
4830 (scroll-down-command nil))
4831 ((< (prefix-numeric-value arg) 0)
4832 (scroll-down-command (- (prefix-numeric-value arg))))
4833 ((eobp)
4834 (scroll-up arg)) ; signal error
4836 (condition-case nil
4837 (scroll-up arg)
4838 (end-of-buffer
4839 (if arg
4840 ;; When scrolling by ARG lines can't be done,
4841 ;; move by ARG lines instead.
4842 (forward-line arg)
4843 ;; When ARG is nil for full-screen scrolling,
4844 ;; move to the bottom of the buffer.
4845 (goto-char (point-max))))))))
4847 (put 'scroll-up-command 'scroll-command t)
4849 (defun scroll-down-command (&optional arg)
4850 "Scroll text of selected window down ARG lines; or near full screen if no ARG.
4851 If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
4852 scroll window further, move cursor to the top line.
4853 When point is already on that position, then signal an error.
4854 A near full screen is `next-screen-context-lines' less than a full screen.
4855 Negative ARG means scroll upward.
4856 If ARG is the atom `-', scroll upward by nearly full screen."
4857 (interactive "^P")
4858 (cond
4859 ((null scroll-error-top-bottom)
4860 (scroll-down arg))
4861 ((eq arg '-)
4862 (scroll-up-command nil))
4863 ((< (prefix-numeric-value arg) 0)
4864 (scroll-up-command (- (prefix-numeric-value arg))))
4865 ((bobp)
4866 (scroll-down arg)) ; signal error
4868 (condition-case nil
4869 (scroll-down arg)
4870 (beginning-of-buffer
4871 (if arg
4872 ;; When scrolling by ARG lines can't be done,
4873 ;; move by ARG lines instead.
4874 (forward-line (- arg))
4875 ;; When ARG is nil for full-screen scrolling,
4876 ;; move to the top of the buffer.
4877 (goto-char (point-min))))))))
4879 (put 'scroll-down-command 'scroll-command t)
4881 ;;; Scrolling commands which scroll a line instead of full screen.
4883 (defun scroll-up-line (&optional arg)
4884 "Scroll text of selected window upward ARG lines; or one line if no ARG.
4885 If ARG is omitted or nil, scroll upward by one line.
4886 This is different from `scroll-up-command' that scrolls a full screen."
4887 (interactive "p")
4888 (scroll-up (or arg 1)))
4890 (put 'scroll-up-line 'scroll-command t)
4892 (defun scroll-down-line (&optional arg)
4893 "Scroll text of selected window down ARG lines; or one line if no ARG.
4894 If ARG is omitted or nil, scroll down by one line.
4895 This is different from `scroll-down-command' that scrolls a full screen."
4896 (interactive "p")
4897 (scroll-down (or arg 1)))
4899 (put 'scroll-down-line 'scroll-command t)
4902 (defun scroll-other-window-down (lines)
4903 "Scroll the \"other window\" down.
4904 For more details, see the documentation for `scroll-other-window'."
4905 (interactive "P")
4906 (scroll-other-window
4907 ;; Just invert the argument's meaning.
4908 ;; We can do that without knowing which window it will be.
4909 (if (eq lines '-) nil
4910 (if (null lines) '-
4911 (- (prefix-numeric-value lines))))))
4913 (defun beginning-of-buffer-other-window (arg)
4914 "Move point to the beginning of the buffer in the other window.
4915 Leave mark at previous position.
4916 With arg N, put point N/10 of the way from the true beginning."
4917 (interactive "P")
4918 (let ((orig-window (selected-window))
4919 (window (other-window-for-scrolling)))
4920 ;; We use unwind-protect rather than save-window-excursion
4921 ;; because the latter would preserve the things we want to change.
4922 (unwind-protect
4923 (progn
4924 (select-window window)
4925 ;; Set point and mark in that window's buffer.
4926 (with-no-warnings
4927 (beginning-of-buffer arg))
4928 ;; Set point accordingly.
4929 (recenter '(t)))
4930 (select-window orig-window))))
4932 (defun end-of-buffer-other-window (arg)
4933 "Move point to the end of the buffer in the other window.
4934 Leave mark at previous position.
4935 With arg N, put point N/10 of the way from the true end."
4936 (interactive "P")
4937 ;; See beginning-of-buffer-other-window for comments.
4938 (let ((orig-window (selected-window))
4939 (window (other-window-for-scrolling)))
4940 (unwind-protect
4941 (progn
4942 (select-window window)
4943 (with-no-warnings
4944 (end-of-buffer arg))
4945 (recenter '(t)))
4946 (select-window orig-window))))
4948 (defvar mouse-autoselect-window-timer nil
4949 "Timer used by delayed window autoselection.")
4951 (defvar mouse-autoselect-window-position nil
4952 "Last mouse position recorded by delayed window autoselection.")
4954 (defvar mouse-autoselect-window-window nil
4955 "Last window recorded by delayed window autoselection.")
4957 (defvar mouse-autoselect-window-state nil
4958 "When non-nil, special state of delayed window autoselection.
4959 Possible values are `suspend' \(suspend autoselection after a menu or
4960 scrollbar interaction\) and `select' \(the next invocation of
4961 'handle-select-window' shall select the window immediately\).")
4963 (defun mouse-autoselect-window-cancel (&optional force)
4964 "Cancel delayed window autoselection.
4965 Optional argument FORCE means cancel unconditionally."
4966 (unless (and (not force)
4967 ;; Don't cancel for select-window or select-frame events
4968 ;; or when the user drags a scroll bar.
4969 (or (memq this-command
4970 '(handle-select-window handle-switch-frame))
4971 (and (eq this-command 'scroll-bar-toolkit-scroll)
4972 (memq (nth 4 (event-end last-input-event))
4973 '(handle end-scroll)))))
4974 (setq mouse-autoselect-window-state nil)
4975 (when (timerp mouse-autoselect-window-timer)
4976 (cancel-timer mouse-autoselect-window-timer))
4977 (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
4979 (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
4980 "Start delayed window autoselection.
4981 MOUSE-POSITION is the last position where the mouse was seen as returned
4982 by `mouse-position'. Optional argument WINDOW non-nil denotes the
4983 window where the mouse was seen. Optional argument SUSPEND non-nil
4984 means suspend autoselection."
4985 ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
4986 (setq mouse-autoselect-window-position mouse-position)
4987 (when window (setq mouse-autoselect-window-window window))
4988 (setq mouse-autoselect-window-state (when suspend 'suspend))
4989 ;; Install timer which runs `mouse-autoselect-window-select' after
4990 ;; `mouse-autoselect-window' seconds.
4991 (setq mouse-autoselect-window-timer
4992 (run-at-time
4993 (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
4995 (defun mouse-autoselect-window-select ()
4996 "Select window with delayed window autoselection.
4997 If the mouse position has stabilized in a non-selected window, select
4998 that window. The minibuffer window is selected only if the minibuffer is
4999 active. This function is run by `mouse-autoselect-window-timer'."
5000 (condition-case nil
5001 (let* ((mouse-position (mouse-position))
5002 (window
5003 (condition-case nil
5004 (window-at (cadr mouse-position) (cddr mouse-position)
5005 (car mouse-position))
5006 (error nil))))
5007 (cond
5008 ((or (menu-or-popup-active-p)
5009 (and window
5010 (not (coordinates-in-window-p (cdr mouse-position) window))))
5011 ;; A menu / popup dialog is active or the mouse is on the scroll-bar
5012 ;; of WINDOW, temporarily suspend delayed autoselection.
5013 (mouse-autoselect-window-start mouse-position nil t))
5014 ((eq mouse-autoselect-window-state 'suspend)
5015 ;; Delayed autoselection was temporarily suspended, reenable it.
5016 (mouse-autoselect-window-start mouse-position))
5017 ((and window (not (eq window (selected-window)))
5018 (or (not (numberp mouse-autoselect-window))
5019 (and (> mouse-autoselect-window 0)
5020 ;; If `mouse-autoselect-window' is positive, select
5021 ;; window if the window is the same as before.
5022 (eq window mouse-autoselect-window-window))
5023 ;; Otherwise select window if the mouse is at the same
5024 ;; position as before. Observe that the first test after
5025 ;; starting autoselection usually fails since the value of
5026 ;; `mouse-autoselect-window-position' recorded there is the
5027 ;; position where the mouse has entered the new window and
5028 ;; not necessarily where the mouse has stopped moving.
5029 (equal mouse-position mouse-autoselect-window-position))
5030 ;; The minibuffer is a candidate window if it's active.
5031 (or (not (window-minibuffer-p window))
5032 (eq window (active-minibuffer-window))))
5033 ;; Mouse position has stabilized in non-selected window: Cancel
5034 ;; delayed autoselection and try to select that window.
5035 (mouse-autoselect-window-cancel t)
5036 ;; Select window where mouse appears unless the selected window is the
5037 ;; minibuffer. Use `unread-command-events' in order to execute pre-
5038 ;; and post-command hooks and trigger idle timers. To avoid delaying
5039 ;; autoselection again, set `mouse-autoselect-window-state'."
5040 (unless (window-minibuffer-p (selected-window))
5041 (setq mouse-autoselect-window-state 'select)
5042 (setq unread-command-events
5043 (cons (list 'select-window (list window))
5044 unread-command-events))))
5045 ((or (and window (eq window (selected-window)))
5046 (not (numberp mouse-autoselect-window))
5047 (equal mouse-position mouse-autoselect-window-position))
5048 ;; Mouse position has either stabilized in the selected window or at
5049 ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
5050 (mouse-autoselect-window-cancel t))
5052 ;; Mouse position has not stabilized yet, resume delayed
5053 ;; autoselection.
5054 (mouse-autoselect-window-start mouse-position window))))
5055 (error nil)))
5057 (defun handle-select-window (event)
5058 "Handle select-window events."
5059 (interactive "e")
5060 (let ((window (posn-window (event-start event))))
5061 (unless (or (not (window-live-p window))
5062 ;; Don't switch if we're currently in the minibuffer.
5063 ;; This tries to work around problems where the
5064 ;; minibuffer gets unselected unexpectedly, and where
5065 ;; you then have to move your mouse all the way down to
5066 ;; the minibuffer to select it.
5067 (window-minibuffer-p (selected-window))
5068 ;; Don't switch to minibuffer window unless it's active.
5069 (and (window-minibuffer-p window)
5070 (not (minibuffer-window-active-p window)))
5071 ;; Don't switch when autoselection shall be delayed.
5072 (and (numberp mouse-autoselect-window)
5073 (not (zerop mouse-autoselect-window))
5074 (not (eq mouse-autoselect-window-state 'select))
5075 (progn
5076 ;; Cancel any delayed autoselection.
5077 (mouse-autoselect-window-cancel t)
5078 ;; Start delayed autoselection from current mouse
5079 ;; position and window.
5080 (mouse-autoselect-window-start (mouse-position) window)
5081 ;; Executing a command cancels delayed autoselection.
5082 (add-hook
5083 'pre-command-hook 'mouse-autoselect-window-cancel))))
5084 (when mouse-autoselect-window
5085 ;; Reset state of delayed autoselection.
5086 (setq mouse-autoselect-window-state nil)
5087 ;; Run `mouse-leave-buffer-hook' when autoselecting window.
5088 (run-hooks 'mouse-leave-buffer-hook))
5089 (select-window window))))
5091 (defun truncated-partial-width-window-p (&optional window)
5092 "Return non-nil if lines in WINDOW are specifically truncated due to its width.
5093 WINDOW defaults to the selected window.
5094 Return nil if WINDOW is not a partial-width window
5095 (regardless of the value of `truncate-lines').
5096 Otherwise, consult the value of `truncate-partial-width-windows'
5097 for the buffer shown in WINDOW."
5098 (unless window
5099 (setq window (selected-window)))
5100 (unless (window-full-width-p window)
5101 (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
5102 (window-buffer window))))
5103 (if (integerp t-p-w-w)
5104 (< (window-width window) t-p-w-w)
5105 t-p-w-w))))
5107 (define-key ctl-x-map "0" 'delete-window)
5108 (define-key ctl-x-map "1" 'delete-other-windows)
5109 (define-key ctl-x-map "2" 'split-window-vertically)
5110 (define-key ctl-x-map "3" 'split-window-horizontally)
5111 (define-key ctl-x-map "9" 'maximize-window)
5112 (define-key ctl-x-map "o" 'other-window)
5113 (define-key ctl-x-map "^" 'enlarge-window)
5114 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
5115 (define-key ctl-x-map "{" 'shrink-window-horizontally)
5116 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
5117 (define-key ctl-x-map "+" 'balance-windows)
5118 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
5120 ;; arch-tag: b508dfcc-c353-4c37-89fa-e773fe10cea9
5121 ;;; window.el ends here