1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992, 1993, 1994, 2000, 2001, 2002, 2004, 2005
4 ;; Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Window tree functions.
32 (defmacro save-selected-window
(&rest body
)
33 "Execute BODY, then select the window that was selected before BODY.
34 Also restore the selected window of each frame as it was at the start
36 However, if a window has become dead, don't get an error,
37 just refrain from reselecting it.
38 Return the value of the last form in BODY."
39 `(let ((save-selected-window-window (selected-window))
40 ;; It is necessary to save all of these, because calling
41 ;; select-window changes frame-selected-window for whatever
42 ;; frame that window is in.
43 (save-selected-window-alist
44 (mapcar (lambda (frame) (list frame
(frame-selected-window frame
)))
48 (dolist (elt save-selected-window-alist
)
49 (and (frame-live-p (car elt
))
50 (window-live-p (cadr elt
))
51 (set-frame-selected-window (car elt
) (cadr elt
))))
52 (if (window-live-p save-selected-window-window
)
53 (select-window save-selected-window-window
)))))
55 (defun window-body-height (&optional window
)
56 "Return number of lines in window WINDOW for actual buffer text.
57 This does not include the mode line (if any) or the header line (if any)."
58 (or window
(setq window
(selected-window)))
59 (if (window-minibuffer-p window
)
60 (window-height window
)
61 (with-current-buffer (window-buffer window
)
62 (max 1 (- (window-height window
)
63 (if mode-line-format
1 0)
64 (if header-line-format
1 0))))))
66 (defun one-window-p (&optional nomini all-frames
)
67 "Return non-nil if the selected window is the only window.
68 Optional arg NOMINI non-nil means don't count the minibuffer
69 even if it is active. Otherwise, the minibuffer is counted
72 The optional arg ALL-FRAMES t means count windows on all frames.
73 If it is `visible', count windows on all visible frames.
74 ALL-FRAMES nil or omitted means count only the selected frame,
75 plus the minibuffer it uses (which may be on another frame).
76 ALL-FRAMES 0 means count all windows in all visible or iconified frames.
77 If ALL-FRAMES is anything else, count only the selected frame."
78 (let ((base-window (selected-window)))
79 (if (and nomini
(eq base-window
(minibuffer-window)))
80 (setq base-window
(next-window base-window
)))
82 (next-window base-window
(if nomini
'arg
) all-frames
))))
84 (defun window-current-scroll-bars (&optional window
)
85 "Return the current scroll-bar settings in window WINDOW.
86 Value is a cons (VERTICAL . HORISONTAL) where VERTICAL specifies the
87 current location of the vertical scroll-bars (left, right, or nil),
88 and HORISONTAL specifies the current location of the horisontal scroll
89 bars (top, bottom, or nil)."
90 (let ((vert (nth 2 (window-scroll-bars window
)))
92 (when (or (eq vert t
) (eq hor t
))
93 (let ((fcsb (frame-current-scroll-bars
94 (window-frame (or window
(selected-window))))))
96 (setq vert
(car fcsb
)))
98 (setq hor
(cdr fcsb
)))))
101 (defun walk-windows (proc &optional minibuf all-frames
)
102 "Cycle through all visible windows, calling PROC for each one.
103 PROC is called with a window as argument.
105 Optional second arg MINIBUF t means count the minibuffer window even
106 if not active. MINIBUF nil or omitted means count the minibuffer iff
107 it is active. MINIBUF neither t nor nil means not to count the
108 minibuffer even if it is active.
110 Several frames may share a single minibuffer; if the minibuffer
111 counts, all windows on all frames that share that minibuffer count
112 too. Therefore, if you are using a separate minibuffer frame
113 and the minibuffer is active and MINIBUF says it counts,
114 `walk-windows' includes the windows in the frame from which you
115 entered the minibuffer, as well as the minibuffer window.
117 ALL-FRAMES is the optional third argument.
118 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
119 ALL-FRAMES = `visible' means include windows on all visible frames.
120 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
121 ALL-FRAMES = t means include windows on all frames including invisible frames.
122 If ALL-FRAMES is a frame, it means include windows on that frame.
123 Anything else means restrict to the selected frame."
124 ;; If we start from the minibuffer window, don't fail to come back to it.
125 (if (window-minibuffer-p (selected-window))
127 (save-selected-window
128 (if (framep all-frames
)
129 (select-window (frame-first-window all-frames
)))
130 (let* (walk-windows-already-seen
131 (walk-windows-current (selected-window)))
133 (setq walk-windows-current
134 (next-window walk-windows-current minibuf all-frames
))
135 (not (memq walk-windows-current walk-windows-already-seen
)))
136 (setq walk-windows-already-seen
137 (cons walk-windows-current walk-windows-already-seen
))
138 (funcall proc walk-windows-current
)))))
140 (defun get-window-with-predicate (predicate &optional minibuf
142 "Return a window satisfying PREDICATE.
144 This function cycles through all visible windows using `walk-windows',
145 calling PREDICATE on each one. PREDICATE is called with a window as
146 argument. The first window for which PREDICATE returns a non-nil
147 value is returned. If no window satisfies PREDICATE, DEFAULT is
150 Optional second arg MINIBUF t means count the minibuffer window even
151 if not active. MINIBUF nil or omitted means count the minibuffer iff
152 it is active. MINIBUF neither t nor nil means not to count the
153 minibuffer even if it is active.
155 Several frames may share a single minibuffer; if the minibuffer
156 counts, all windows on all frames that share that minibuffer count
157 too. Therefore, if you are using a separate minibuffer frame
158 and the minibuffer is active and MINIBUF says it counts,
159 `walk-windows' includes the windows in the frame from which you
160 entered the minibuffer, as well as the minibuffer window.
162 ALL-FRAMES is the optional third argument.
163 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
164 ALL-FRAMES = `visible' means include windows on all visible frames.
165 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
166 ALL-FRAMES = t means include windows on all frames including invisible frames.
167 If ALL-FRAMES is a frame, it means include windows on that frame.
168 Anything else means restrict to the selected frame."
170 (walk-windows #'(lambda (window)
171 (when (funcall predicate window
)
172 (throw 'found window
)))
176 (defalias 'some-window
'get-window-with-predicate
)
178 (defun minibuffer-window-active-p (window)
179 "Return t if WINDOW (a minibuffer window) is now active."
180 (eq window
(active-minibuffer-window)))
182 (defun count-windows (&optional minibuf
)
183 "Return the number of visible windows.
184 This counts the windows in the selected frame and (if the minibuffer is
185 to be counted) its minibuffer frame (if that's not the same frame).
186 The optional arg MINIBUF non-nil means count the minibuffer
187 even if it is inactive."
189 (walk-windows (function (lambda (w)
190 (setq count
(+ count
1))))
194 (defun window-safely-shrinkable-p (&optional window
)
195 "Non-nil if the WINDOW can be shrunk without shrinking other windows.
196 If WINDOW is nil or omitted, it defaults to the currently selected window."
197 (with-selected-window (or window
(selected-window))
198 (let ((edges (window-edges)))
199 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
200 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
203 (defun balance-windows ()
204 "Make all visible windows the same height (approximately)."
206 (let ((count -
1) levels newsizes level-size
207 ;; Don't count the lines that are above the uppermost windows.
208 ;; (These are the menu bar lines, if any.)
209 (mbl (nth 1 (window-edges (frame-first-window (selected-frame)))))
210 (last-window (previous-window (frame-first-window (selected-frame))))
211 ;; Don't count the lines that are past the lowest main window.
213 ;; Bottom edge of last window determines what size we have to work with.
215 (+ (window-height last-window
)
216 (nth 1 (window-edges last-window
))))
218 ;; Find all the different vpos's at which windows start,
219 ;; then count them. But ignore levels that differ by only 1.
220 (let (tops (prev-top -
2))
221 (walk-windows (function (lambda (w)
222 (setq tops
(cons (nth 1 (window-edges w
))
225 (setq tops
(sort tops
'<))
227 (if (> (car tops
) (1+ prev-top
))
228 (setq prev-top
(car tops
)
230 (setq levels
(cons (cons (car tops
) count
) levels
))
231 (setq tops
(cdr tops
)))
232 (setq count
(1+ count
)))
233 ;; Subdivide the frame into desired number of vertical levels.
234 (setq level-size
(/ (- total mbl
) count
))
235 (save-selected-window
236 ;; Set up NEWSIZES to map windows to their desired sizes.
237 ;; If a window ends at the bottom level, don't include
238 ;; it in NEWSIZES. Those windows get the right sizes
239 ;; by adjusting the ones above them.
240 (walk-windows (function
242 (let ((newtop (cdr (assq (nth 1 (window-edges w
))
244 (newbot (cdr (assq (+ (window-height w
)
245 (nth 1 (window-edges w
)))
249 (cons (cons w
(* level-size
(- newbot newtop
)))
252 ;; Make walk-windows start with the topmost window.
253 (select-window (previous-window (frame-first-window (selected-frame))))
254 (let (done (count 0))
255 ;; Give each window its precomputed size, or at least try.
256 ;; Keep trying until they all get the intended sizes,
257 ;; but not more than 3 times (to prevent infinite loop).
258 (while (and (not done
) (< count
3))
260 (setq count
(1+ count
))
261 (walk-windows (function (lambda (w)
263 (let ((newsize (cdr (assq w newsizes
))))
265 (enlarge-window (- newsize
268 (unless (= (window-height) newsize
)
272 ;; I think this should be the default; I think people will prefer it--rms.
273 (defcustom split-window-keep-point t
274 "*If non-nil, \\[split-window-vertically] keeps the original point \
276 This is often more convenient for editing.
277 If nil, adjust point in each of the two windows to minimize redisplay.
278 This is convenient on slow terminals, but point can move strangely.
280 This option applies only to `split-window-vertically' and
281 functions that call it. `split-window' always keeps the original
282 point in both children,"
286 (defun split-window-vertically (&optional arg
)
287 "Split current window into two windows, one above the other.
288 The uppermost window gets ARG lines and the other gets the rest.
289 Negative ARG means select the size of the lowermost window instead.
290 With no argument, split equally or close to it.
291 Both windows display the same buffer now current.
293 If the variable `split-window-keep-point' is non-nil, both new windows
294 will get the same value of point as the current window. This is often
295 more convenient for editing. The upper window is the selected window.
297 Otherwise, we choose window starts so as to minimize the amount of
298 redisplay; this is convenient on slow terminals. The new selected
299 window is the one that the current value of point appears in. The
300 value of point can change if the text around point is hidden by the
303 Regardless of the value of `split-window-keep-point', the upper
304 window is the original one and the return value is the new, lower
307 (let ((old-w (selected-window))
309 (size (and arg
(prefix-numeric-value arg
)))
311 new-w bottom switch moved
)
312 (and size
(< size
0) (setq size
(+ (window-height) size
)))
313 (setq new-w
(split-window nil size
))
314 (or split-window-keep-point
317 (set-buffer (window-buffer))
318 (goto-char (window-start))
319 (setq moved
(vertical-motion (window-height)))
320 (set-window-start new-w
(point))
321 (if (> (point) (window-point new-w
))
322 (set-window-point new-w
(point)))
323 (and (= moved
(window-height))
325 (setq window-full-p t
)
326 (vertical-motion -
1)))
327 (setq bottom
(point)))
330 (set-window-point old-w
(1- bottom
)))
332 (<= (window-start new-w
) old-point
)
334 (set-window-point new-w old-point
)
335 (select-window new-w
)))))
336 (split-window-save-restore-data new-w old-w
)))
338 ;; This is to avoid compiler warnings.
339 (defvar view-return-to-alist
)
341 (defun split-window-save-restore-data (new-w old-w
)
342 (with-current-buffer (window-buffer)
344 (let ((old-info (assq old-w view-return-to-alist
)))
346 (push (cons new-w
(cons (car (cdr old-info
)) t
))
347 view-return-to-alist
))))
350 (defun split-window-horizontally (&optional arg
)
351 "Split current window into two windows side by side.
352 This window becomes the leftmost of the two, and gets ARG columns.
353 Negative ARG means select the size of the rightmost window instead.
354 The argument includes the width of the window's scroll bar; if there
355 are no scroll bars, it includes the width of the divider column
356 to the window's right, if any. No ARG means split equally.
358 The original, leftmost window remains selected.
359 The return value is the new, rightmost window."
361 (let ((old-w (selected-window))
362 (size (and arg
(prefix-numeric-value arg
))))
364 (setq size
(+ (window-width) size
)))
365 (split-window-save-restore-data (split-window nil size t
) old-w
)))
368 (defun set-window-text-height (window height
)
369 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
370 This doesn't include the mode-line (or header-line if any) or any
371 partial-height lines in the text display area.
373 If WINDOW is nil, the selected window is used.
375 Note that the current implementation of this function cannot always set
376 the height exactly, but attempts to be conservative, by allocating more
377 lines than are actually needed in the case where some error may be present."
378 (let ((delta (- height
(window-text-height window
))))
379 (unless (zerop delta
)
380 (let ((window-min-height 1))
381 (if (and window
(not (eq window
(selected-window))))
382 (save-selected-window
383 (select-window window
)
384 (enlarge-window delta
))
385 (enlarge-window delta
))))))
388 (defun enlarge-window-horizontally (arg)
389 "Make current window ARG columns wider."
391 (enlarge-window arg t
))
393 (defun shrink-window-horizontally (arg)
394 "Make current window ARG columns narrower."
396 (shrink-window arg t
))
398 (defun window-buffer-height (window)
399 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
401 (set-buffer (window-buffer window
))
402 (goto-char (point-min))
403 (let ((ignore-final-newline
404 ;; If buffer ends with a newline, ignore it when counting height
405 ;; unless point is after it.
406 (and (not (eobp)) (eq ?
\n (char-after (1- (point-max)))))))
407 (+ 1 (nth 2 (compute-motion (point-min)
409 (- (point-max) (if ignore-final-newline
1 0))
415 (defun count-screen-lines (&optional beg end count-final-newline window
)
416 "Return the number of screen lines in the region.
417 The number of screen lines may be different from the number of actual lines,
418 due to line breaking, display table, etc.
420 Optional arguments BEG and END default to `point-min' and `point-max'
423 If region ends with a newline, ignore it unless optional third argument
424 COUNT-FINAL-NEWLINE is non-nil.
426 The optional fourth argument WINDOW specifies the window used for obtaining
427 parameters such as width, horizontal scrolling, and so on. The default is
428 to use the selected window's parameters.
430 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
431 regardless of which buffer is displayed in WINDOW. This makes possible to use
432 `count-screen-lines' in any buffer, whether or not it is currently displayed
435 (setq beg
(point-min)))
437 (setq end
(point-max)))
443 (narrow-to-region (min beg end
)
444 (if (and (not count-final-newline
)
445 (= ?
\n (char-before (max beg end
))))
448 (goto-char (point-min))
449 (1+ (vertical-motion (buffer-size) window
))))))
451 (defun fit-window-to-buffer (&optional window max-height min-height
)
452 "Make WINDOW the right size to display its contents exactly.
453 If WINDOW is omitted or nil, it defaults to the selected window.
454 If the optional argument MAX-HEIGHT is supplied, it is the maximum height
455 the window is allowed to be, defaulting to the frame height.
456 If the optional argument MIN-HEIGHT is supplied, it is the minimum
457 height the window is allowed to be, defaulting to `window-min-height'.
459 The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or
464 (setq window
(selected-window)))
465 (when (null max-height
)
466 (setq max-height
(frame-height (window-frame window
))))
469 ;; Buffer that is displayed in WINDOW
470 (window-buffer window
))
472 ;; The current height of WINDOW
473 (window-height window
))
475 ;; The height necessary to show the buffer displayed by WINDOW
476 ;; (`count-screen-lines' always works on the current buffer).
477 (with-current-buffer buf
478 (+ (count-screen-lines)
479 ;; If the buffer is empty, (count-screen-lines) is
480 ;; zero. But, even in that case, we need one text line
482 (if (= (point-min) (point-max))
484 ;; For non-minibuffers, count the mode-line, if any
485 (if (and (not (window-minibuffer-p window
))
488 ;; Count the header-line, if any
489 (if header-line-format
1 0))))
491 ;; Calculate how much the window height has to change to show
492 ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
493 (- (max (min desired-height max-height
)
494 (or min-height window-min-height
))
496 ;; We do our own height checking, so avoid any restrictions due to
497 ;; window-min-height.
498 (window-min-height 1))
500 ;; Don't try to redisplay with the cursor at the end
501 ;; on its own line--that would force a scroll and spoil things.
502 (when (with-current-buffer buf
503 (and (eobp) (bolp) (not (bobp))))
504 (set-window-point window
(1- (window-point window
))))
506 (save-selected-window
507 (select-window window
)
509 ;; Adjust WINDOW to the nominally correct size (which may actually
510 ;; be slightly off because of variable height text, etc).
511 (unless (zerop delta
)
512 (enlarge-window delta
))
514 ;; Check if the last line is surely fully visible. If not,
515 ;; enlarge the window.
516 (let ((end (with-current-buffer buf
518 (goto-char (point-max))
519 (when (and (bolp) (not (bobp)))
520 ;; Don't include final newline
523 ;; If line-wrapping is turned off, test the
524 ;; beginning of the last line for visibility
525 ;; instead of the end, as the end of the line
526 ;; could be invisible by virtue of extending past
527 ;; the edge of the window.
530 (set-window-vscroll window
0)
531 (while (and (< desired-height max-height
)
532 (= desired-height
(window-height window
))
533 (not (pos-visible-in-window-p end window
)))
535 (setq desired-height
(1+ desired-height
)))))))
537 (defun shrink-window-if-larger-than-buffer (&optional window
)
538 "Shrink the WINDOW to be as small as possible to display its contents.
539 If WINDOW is omitted or nil, it defaults to the selected window.
540 Do not shrink to less than `window-min-height' lines.
541 Do nothing if the buffer contains more lines than the present window height,
542 or if some of the window's contents are scrolled out of view,
543 or if shrinking this window would also shrink another window.
544 or if the window is the only window of its frame.
545 Return non-nil if the window was shrunk."
548 (setq window
(selected-window)))
549 (let* ((frame (window-frame window
))
550 (mini (frame-parameter frame
'minibuffer
))
551 (edges (window-edges window
)))
552 (if (and (not (eq window
(frame-root-window frame
)))
553 (window-safely-shrinkable-p)
554 (pos-visible-in-window-p (point-min) window
)
555 (not (eq mini
'only
))
557 (let ((mini-window (minibuffer-window frame
)))
558 (or (null mini-window
)
559 (not (eq frame
(window-frame mini-window
)))
561 (nth 1 (window-edges mini-window
)))
563 (frame-parameter frame
'menu-bar-lines
))))))
564 (fit-window-to-buffer window
(window-height window
)))))
566 (defun kill-buffer-and-window ()
567 "Kill the current buffer and delete the selected window."
569 (let ((window-to-delete (selected-window))
570 (delete-window-hook (lambda ()
574 (add-hook 'kill-buffer-hook delete-window-hook t t
)
575 (if (kill-buffer (current-buffer))
576 ;; If `delete-window' failed before, we rerun it to regenerate
577 ;; the error so it can be seen in the minibuffer.
578 (when (eq (selected-window) window-to-delete
)
580 (remove-hook 'kill-buffer-hook delete-window-hook t
))))
582 (defun quit-window (&optional kill window
)
583 "Quit the current buffer. Bury it, and maybe delete the selected frame.
584 \(The frame is deleted if it is contains a dedicated window for the buffer.)
585 With a prefix argument, kill the buffer instead.
587 Noninteractively, if KILL is non-nil, then kill the current buffer,
590 If WINDOW is non-nil, it specifies a window; we delete that window,
591 and the buffer that is killed or buried is the one in that window."
593 (let ((buffer (window-buffer window
))
594 (frame (window-frame (or window
(selected-window))))
596 (save-selected-window
598 (select-window window
))
602 (save-selected-window
604 (select-window window
))
605 (or (window-minibuffer-p)
606 (window-dedicated-p (selected-window))
607 (switch-to-buffer (other-buffer))))
609 ;; Get rid of the frame, if it has just one dedicated window
610 ;; and other visible frames exist.
611 (and (or (window-minibuffer-p) (window-dedicated-p window
))
612 (delq frame
(visible-frame-list))
614 (if (and (eq default-minibuffer-frame frame
)
615 (= 1 (length (minibuffer-frame-list))))
618 (setq window-handled t
)))
620 ;; Deal with the buffer.
623 (bury-buffer buffer
))
625 ;; Maybe get rid of the window.
626 (and window
(not window-handled
) (not window-solitary
)
627 (delete-window window
))))
629 (defun handle-select-window (event)
630 "Handle select-window events."
632 (let ((window (posn-window (event-start event
))))
633 (if (and (window-live-p window
)
634 ;; Don't switch if we're currently in the minibuffer.
635 ;; This tries to work around problems where the minibuffer gets
636 ;; unselected unexpectedly, and where you then have to move
637 ;; your mouse all the way down to the minibuffer to select it.
638 (not (window-minibuffer-p (selected-window)))
639 ;; Don't switch to a minibuffer window unless it's active.
640 (or (not (window-minibuffer-p window
))
641 (minibuffer-window-active-p window
)))
642 (select-window window
))))
644 (define-key ctl-x-map
"2" 'split-window-vertically
)
645 (define-key ctl-x-map
"3" 'split-window-horizontally
)
646 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
647 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
648 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
649 (define-key ctl-x-map
"+" 'balance-windows
)
650 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
652 ;; arch-tag: b508dfcc-c353-4c37-89fa-e773fe10cea9
653 ;;; window.el ends here