1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992, 1993, 1994, 2000, 2001
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 (defun window-body-height (&optional window
)
33 "Return number of lines in window WINDOW for actual buffer text.
34 This does not include the mode line (if any) or the header line (if any)."
35 (or window
(setq window
(selected-window)))
36 (with-current-buffer (window-buffer window
)
37 (- (window-height window
)
38 (if mode-line-format
1 0)
39 (if header-line-format
1 0))))
41 (defun one-window-p (&optional nomini all-frames
)
42 "Return non-nil if the selected window is the only window (in its frame).
43 Optional arg NOMINI non-nil means don't count the minibuffer
46 The optional arg ALL-FRAMES t means count windows on all frames.
47 If it is `visible', count windows on all visible frames.
48 ALL-FRAMES nil or omitted means count only the selected frame,
49 plus the minibuffer it uses (which may be on another frame).
50 If ALL-FRAMES is neither nil nor t, count only the selected frame."
51 (let ((base-window (selected-window)))
52 (if (and nomini
(eq base-window
(minibuffer-window)))
53 (setq base-window
(next-window base-window
)))
55 (next-window base-window
(if nomini
'arg
) all-frames
))))
57 (defun walk-windows (proc &optional minibuf all-frames
)
58 "Cycle through all visible windows, calling PROC for each one.
59 PROC is called with a window as argument.
61 Optional second arg MINIBUF t means count the minibuffer window even
62 if not active. MINIBUF nil or omitted means count the minibuffer iff
63 it is active. MINIBUF neither t nor nil means not to count the
64 minibuffer even if it is active.
66 Several frames may share a single minibuffer; if the minibuffer
67 counts, all windows on all frames that share that minibuffer count
68 too. Therefore, if you are using a separate minibuffer frame
69 and the minibuffer is active and MINIBUF says it counts,
70 `walk-windows' includes the windows in the frame from which you
71 entered the minibuffer, as well as the minibuffer window.
73 ALL-FRAMES is the optional third argument.
74 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
75 ALL-FRAMES = `visible' means include windows on all visible frames.
76 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
77 ALL-FRAMES = t means include windows on all frames including invisible frames.
78 If ALL-FRAMES is a frame, it means include windows on that frame.
79 Anything else means restrict to the selected frame."
80 ;; If we start from the minibuffer window, don't fail to come back to it.
81 (if (window-minibuffer-p (selected-window))
84 (if (framep all-frames
)
85 (select-window (frame-first-window all-frames
)))
86 (let* (walk-windows-already-seen
87 (walk-windows-current (selected-window)))
89 (setq walk-windows-current
90 (next-window walk-windows-current minibuf all-frames
))
91 (not (memq walk-windows-current walk-windows-already-seen
)))
92 (setq walk-windows-already-seen
93 (cons walk-windows-current walk-windows-already-seen
))
94 (funcall proc walk-windows-current
)))))
96 (defun get-window-with-predicate (predicate &optional minibuf
98 "Return a window satisfying PREDICATE.
100 This function cycles through all visible windows using `walk-windows',
101 calling PREDICATE on each one. PREDICATE is called with a window as
102 argument. The first window for which PREDICATE returns a non-nil
103 value is returned. If no window satisfies PREDICATE, DEFAULT is
106 Optional second arg MINIBUF t means count the minibuffer window even
107 if not active. MINIBUF nil or omitted means count the minibuffer iff
108 it is active. MINIBUF neither t nor nil means not to count the
109 minibuffer even if it is active.
111 Several frames may share a single minibuffer; if the minibuffer
112 counts, all windows on all frames that share that minibuffer count
113 too. Therefore, if you are using a separate minibuffer frame
114 and the minibuffer is active and MINIBUF says it counts,
115 `walk-windows' includes the windows in the frame from which you
116 entered the minibuffer, as well as the minibuffer window.
118 ALL-FRAMES is the optional third argument.
119 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
120 ALL-FRAMES = `visible' means include windows on all visible frames.
121 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
122 ALL-FRAMES = t means include windows on all frames including invisible frames.
123 If ALL-FRAMES is a frame, it means include windows on that frame.
124 Anything else means restrict to the selected frame."
126 (walk-windows #'(lambda (window)
127 (when (funcall predicate window
)
128 (throw 'found window
)))
132 (defalias 'some-window
'get-window-with-predicate
)
134 (defun minibuffer-window-active-p (window)
135 "Return t if WINDOW (a minibuffer window) is now active."
136 (eq window
(active-minibuffer-window)))
138 (defmacro save-selected-window
(&rest body
)
139 "Execute BODY, then select the window that was selected before BODY.
140 However, if that window has become dead, don't get an error,
141 just refrain from switching to it."
142 `(let ((save-selected-window-window (selected-window)))
145 (if (window-live-p save-selected-window-window
)
146 (select-window save-selected-window-window
)))))
148 (defun count-windows (&optional minibuf
)
149 "Return the number of visible windows.
150 This counts the windows in the selected frame and (if the minibuffer is
151 to be counted) its minibuffer frame (if that's not the same frame).
152 The optional arg MINIBUF non-nil means count the minibuffer
153 even if it is inactive."
155 (walk-windows (function (lambda (w)
156 (setq count
(+ count
1))))
160 (defun window-safely-shrinkable-p (&optional window
)
161 "Non-nil if the WINDOW can be shrunk without shrinking other windows.
162 If WINDOW is nil or omitted, it defaults to the currently selected window."
163 (save-selected-window
164 (when window
(select-window window
))
165 (or (and (not (eq window
(frame-first-window)))
166 (= (car (window-edges))
167 (car (window-edges (previous-window)))))
168 (= (car (window-edges))
169 (car (window-edges (next-window)))))))
171 (defun balance-windows ()
172 "Make all visible windows the same height (approximately)."
174 (let ((count -
1) levels newsizes level-size
175 ;; Don't count the lines that are above the uppermost windows.
176 ;; (These are the menu bar lines, if any.)
177 (mbl (nth 1 (window-edges (frame-first-window (selected-frame)))))
178 (last-window (previous-window (frame-first-window (selected-frame))))
179 ;; Don't count the lines that are past the lowest main window.
181 ;; Bottom edge of last window determines what size we have to work with.
183 (+ (window-height last-window
)
184 (nth 1 (window-edges last-window
))))
186 ;; Find all the different vpos's at which windows start,
187 ;; then count them. But ignore levels that differ by only 1.
188 (let (tops (prev-top -
2))
189 (walk-windows (function (lambda (w)
190 (setq tops
(cons (nth 1 (window-edges w
))
193 (setq tops
(sort tops
'<))
195 (if (> (car tops
) (1+ prev-top
))
196 (setq prev-top
(car tops
)
198 (setq levels
(cons (cons (car tops
) count
) levels
))
199 (setq tops
(cdr tops
)))
200 (setq count
(1+ count
)))
201 ;; Subdivide the frame into desired number of vertical levels.
202 (setq level-size
(/ (- total mbl
) count
))
203 (save-selected-window
204 ;; Set up NEWSIZES to map windows to their desired sizes.
205 ;; If a window ends at the bottom level, don't include
206 ;; it in NEWSIZES. Those windows get the right sizes
207 ;; by adjusting the ones above them.
208 (walk-windows (function
210 (let ((newtop (cdr (assq (nth 1 (window-edges w
))
212 (newbot (cdr (assq (+ (window-height w
)
213 (nth 1 (window-edges w
)))
217 (cons (cons w
(* level-size
(- newbot newtop
)))
220 ;; Make walk-windows start with the topmost window.
221 (select-window (previous-window (frame-first-window (selected-frame))))
222 (let (done (count 0))
223 ;; Give each window its precomputed size, or at least try.
224 ;; Keep trying until they all get the intended sizes,
225 ;; but not more than 3 times (to prevent infinite loop).
226 (while (and (not done
) (< count
3))
228 (setq count
(1+ count
))
229 (walk-windows (function (lambda (w)
231 (let ((newsize (cdr (assq w newsizes
))))
233 (enlarge-window (- newsize
236 (unless (= (window-height) newsize
)
240 ;;; I think this should be the default; I think people will prefer it--rms.
241 (defcustom split-window-keep-point t
242 "*If non-nil, split windows keeps the original point in both children.
243 This is often more convenient for editing.
244 If nil, adjust point in each of the two windows to minimize redisplay.
245 This is convenient on slow terminals, but point can move strangely."
249 (defun split-window-vertically (&optional arg
)
250 "Split current window into two windows, one above the other.
251 The uppermost window gets ARG lines and the other gets the rest.
252 Negative arg means select the size of the lowermost window instead.
253 With no argument, split equally or close to it.
254 Both windows display the same buffer now current.
256 If the variable `split-window-keep-point' is non-nil, both new windows
257 will get the same value of point as the current window. This is often
258 more convenient for editing.
260 Otherwise, we chose window starts so as to minimize the amount of
261 redisplay; this is convenient on slow terminals. The new selected
262 window is the one that the current value of point appears in. The
263 value of point can change if the text around point is hidden by the
266 (let ((old-w (selected-window))
268 (size (and arg
(prefix-numeric-value arg
)))
270 new-w bottom switch moved
)
271 (and size
(< size
0) (setq size
(+ (window-height) size
)))
272 (setq new-w
(split-window nil size
))
273 (or split-window-keep-point
276 (set-buffer (window-buffer))
277 (goto-char (window-start))
278 (setq moved
(vertical-motion (window-height)))
279 (set-window-start new-w
(point))
280 (if (> (point) (window-point new-w
))
281 (set-window-point new-w
(point)))
282 (and (= moved
(window-height))
284 (setq window-full-p t
)
285 (vertical-motion -
1)))
286 (setq bottom
(point)))
289 (set-window-point old-w
(1- bottom
)))
291 (<= (window-start new-w
) old-point
)
293 (set-window-point new-w old-point
)
294 (select-window new-w
)))))
295 (split-window-save-restore-data new-w old-w
)))
297 ;; This is to avoid compiler warnings.
298 (defvar view-return-to-alist
)
300 (defun split-window-save-restore-data (new-w old-w
)
302 (set-buffer (window-buffer))
304 (let ((old-info (assq old-w view-return-to-alist
)))
305 (setq view-return-to-alist
306 (cons (cons new-w
(cons (and old-info
(car (cdr old-info
))) t
))
307 view-return-to-alist
))))
310 (defun split-window-horizontally (&optional arg
)
311 "Split current window into two windows side by side.
312 This window becomes the leftmost of the two, and gets ARG columns.
313 Negative arg means select the size of the rightmost window instead.
314 The argument includes the width of the window's scroll bar; if there
315 are no scroll bars, it includes the width of the divider column
316 to the window's right, if any. No arg means split equally."
318 (let ((old-w (selected-window))
319 (size (and arg
(prefix-numeric-value arg
))))
321 (setq size
(+ (window-width) size
)))
322 (split-window-save-restore-data (split-window nil size t
) old-w
)))
325 (defun set-window-text-height (window height
)
326 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
327 This doesn't include the mode-line (or header-line if any) or any
328 partial-height lines in the text display area.
330 If WINDOW is nil, the selected window is used.
332 Note that the current implementation of this function cannot always set
333 the height exactly, but attempts to be conservative, by allocating more
334 lines than are actually needed in the case where some error may be present."
335 (let ((delta (- height
(window-text-height window
))))
336 (unless (zerop delta
)
337 (let ((window-min-height 1))
338 (if (and window
(not (eq window
(selected-window))))
339 (save-selected-window
340 (select-window window
)
341 (enlarge-window delta
))
342 (enlarge-window delta
))))))
345 (defun enlarge-window-horizontally (arg)
346 "Make current window ARG columns wider."
348 (enlarge-window arg t
))
350 (defun shrink-window-horizontally (arg)
351 "Make current window ARG columns narrower."
353 (shrink-window arg t
))
355 (defun window-buffer-height (window)
356 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
358 (set-buffer (window-buffer window
))
359 (goto-char (point-min))
360 (let ((ignore-final-newline
361 ;; If buffer ends with a newline, ignore it when counting height
362 ;; unless point is after it.
363 (and (not (eobp)) (eq ?
\n (char-after (1- (point-max)))))))
364 (+ 1 (nth 2 (compute-motion (point-min)
366 (- (point-max) (if ignore-final-newline
1 0))
368 (window-width window
)
372 (defun count-screen-lines (&optional beg end count-final-newline window
)
373 "Return the number of screen lines in the region.
374 The number of screen lines may be different from the number of actual lines,
375 due to line breaking, display table, etc.
377 Optional arguments BEG and END default to `point-min' and `point-max'
380 If region ends with a newline, ignore it unless optional third argument
381 COUNT-FINAL-NEWLINE is non-nil.
383 The optional fourth argument WINDOW specifies the window used for obtaining
384 parameters such as width, horizontal scrolling, and so on. The default is
385 to use the selected window's parameters.
387 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
388 regardless of which buffer is displayed in WINDOW. This makes possible to use
389 `count-screen-lines' in any buffer, whether or not it is currently displayed
392 (setq beg
(point-min)))
394 (setq end
(point-max)))
400 (narrow-to-region (min beg end
)
401 (if (and (not count-final-newline
)
402 (= ?
\n (char-before (max beg end
))))
405 (goto-char (point-min))
406 (1+ (vertical-motion (buffer-size) window
))))))
408 (defun fit-window-to-buffer (&optional window max-height min-height
)
409 "Make WINDOW the right size to display its contents exactly.
410 If WINDOW is omitted or nil, it defaults to the selected window.
411 If the optional argument MAX-HEIGHT is supplied, it is the maximum height
412 the window is allowed to be, defaulting to the frame height.
413 If the optional argument MIN-HEIGHT is supplied, it is the minimum
414 height the window is allowed to be, defaulting to `window-min-height'.
416 The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or
421 (setq window
(selected-window)))
422 (when (null max-height
)
423 (setq max-height
(frame-height (window-frame window
))))
426 ;; Buffer that is displayed in WINDOW
427 (window-buffer window
))
429 ;; The current height of WINDOW
430 (window-height window
))
432 ;; The height necessary to show the buffer displayed by WINDOW
433 ;; (`count-screen-lines' always works on the current buffer).
434 (with-current-buffer buf
435 (+ (count-screen-lines)
436 ;; If the buffer is empty, (count-screen-lines) is
437 ;; zero. But, even in that case, we need one text line
439 (if (= (point-min) (point-max))
441 ;; For non-minibuffers, count the mode-line, if any
442 (if (and (not (window-minibuffer-p window
))
445 ;; Count the header-line, if any
446 (if header-line-format
1 0))))
448 ;; Calculate how much the window height has to change to show
449 ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
450 (- (max (min desired-height max-height
)
451 (or min-height window-min-height
))
453 ;; We do our own height checking, so avoid any restrictions due to
454 ;; window-min-height.
455 (window-min-height 1))
457 ;; Don't try to redisplay with the cursor at the end
458 ;; on its own line--that would force a scroll and spoil things.
459 (when (with-current-buffer buf
460 (and (eobp) (bolp) (not (bobp))))
461 (set-window-point window
(1- (window-point window
))))
463 (save-selected-window
464 (select-window window
)
466 ;; Adjust WINDOW to the nominally correct size (which may actually
467 ;; be slightly off because of variable height text, etc).
468 (unless (zerop delta
)
469 (enlarge-window delta
))
471 ;; Check if the last line is surely fully visible. If not,
472 ;; enlarge the window.
473 (let ((end (with-current-buffer buf
475 (goto-char (point-max))
476 (when (and (bolp) (not (bobp)))
477 ;; Don't include final newline
480 ;; If line-wrapping is turned off, test the
481 ;; beginning of the last line for visibility
482 ;; instead of the end, as the end of the line
483 ;; could be invisible by virtue of extending past
484 ;; the edge of the window.
487 (set-window-vscroll window
0)
488 (while (and (< desired-height max-height
)
489 (= desired-height
(window-height window
))
490 (not (pos-visible-in-window-p end window
)))
492 (setq desired-height
(1+ desired-height
)))))))
494 (defun shrink-window-if-larger-than-buffer (&optional window
)
495 "Shrink the WINDOW to be as small as possible to display its contents.
496 If WINDOW is omitted or nil, it defaults to the selected window.
497 Do not shrink to less than `window-min-height' lines.
498 Do nothing if the buffer contains more lines than the present window height,
499 or if some of the window's contents are scrolled out of view,
500 or if shrinking this window would also shrink another window.
501 or if the window is the only window of its frame.
502 Return non-nil if the window was shrunk."
505 (setq window
(selected-window)))
506 (let* ((frame (window-frame window
))
507 (mini (frame-parameter frame
'minibuffer
))
508 (edges (window-edges window
)))
509 (if (and (not (eq window
(frame-root-window frame
)))
510 (window-safely-shrinkable-p)
511 (pos-visible-in-window-p (point-min) window
)
512 (not (eq mini
'only
))
514 (let ((mini-window (minibuffer-window frame
)))
515 (or (null mini-window
)
516 (not (eq frame
(window-frame mini-window
)))
518 (nth 1 (window-edges mini-window
)))
520 (frame-parameter frame
'menu-bar-lines
))))))
521 (fit-window-to-buffer window
(window-height window
)))))
523 (defun kill-buffer-and-window ()
524 "Kill the current buffer and delete the selected window."
526 (if (yes-or-no-p (format "Kill buffer `%s'? " (buffer-name)))
527 (let ((buffer (current-buffer)))
528 (delete-window (selected-window))
529 (kill-buffer buffer
))
532 (defun quit-window (&optional kill window
)
533 "Quit the current buffer. Bury it, and maybe delete the selected frame.
534 \(The frame is deleted if it is contains a dedicated window for the buffer.)
535 With a prefix argument, kill the buffer instead.
537 Noninteractively, if KILL is non-nil, then kill the current buffer,
540 If WINDOW is non-nil, it specifies a window; we delete that window,
541 and the buffer that is killed or buried is the one in that window."
543 (let ((buffer (window-buffer window
))
544 (frame (window-frame (or window
(selected-window))))
546 (save-selected-window
548 (select-window window
))
552 (save-selected-window
554 (select-window window
))
555 (or (window-minibuffer-p)
556 (window-dedicated-p (selected-window))
557 (switch-to-buffer (other-buffer))))
559 ;; Get rid of the frame, if it has just one dedicated window
560 ;; and other visible frames exist.
561 (and (or (window-minibuffer-p) (window-dedicated-p window
))
562 (delq frame
(visible-frame-list))
564 (if (and (eq default-minibuffer-frame frame
)
565 (= 1 (length (minibuffer-frame-list))))
568 (setq window-handled t
)))
570 ;; Deal with the buffer.
573 (bury-buffer buffer
))
575 ;; Maybe get rid of the window.
576 (and window
(not window-handled
) (not window-solitary
)
577 (delete-window window
))))
579 (define-key ctl-x-map
"2" 'split-window-vertically
)
580 (define-key ctl-x-map
"3" 'split-window-horizontally
)
581 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
582 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
583 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
584 (define-key ctl-x-map
"+" 'balance-windows
)
585 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
587 ;;; window.el ends here