*** empty log message ***
[emacs.git] / lisp / window.el
blob92451fb9cd8a7670f3504840ba00e5a6023d055b
1 ;;; window.el --- GNU Emacs window commands aside from those written in C.
3 ;; Copyright (C) 1985, 1989, 1992, 1993, 1994, 2000
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Code:
27 ;;;; Window tree functions.
29 (defun one-window-p (&optional nomini all-frames)
30 "Returns non-nil if the selected window is the only window (in its frame).
31 Optional arg NOMINI non-nil means don't count the minibuffer
32 even if it is active.
34 The optional arg ALL-FRAMES t means count windows on all frames.
35 If it is `visible', count windows on all visible frames.
36 ALL-FRAMES nil or omitted means count only the selected frame,
37 plus the minibuffer it uses (which may be on another frame).
38 If ALL-FRAMES is neither nil nor t, count only the selected frame."
39 (let ((base-window (selected-window)))
40 (if (and nomini (eq base-window (minibuffer-window)))
41 (setq base-window (next-window base-window)))
42 (eq base-window
43 (next-window base-window (if nomini 'arg) all-frames))))
45 (defun walk-windows (proc &optional minibuf all-frames)
46 "Cycle through all visible windows, calling PROC for each one.
47 PROC is called with a window as argument.
49 Optional second arg MINIBUF t means count the minibuffer window even
50 if not active. MINIBUF nil or omitted means count the minibuffer iff
51 it is active. MINIBUF neither t nor nil means not to count the
52 minibuffer even if it is active.
54 Several frames may share a single minibuffer; if the minibuffer
55 counts, all windows on all frames that share that minibuffer count
56 too. Therefore, if you are using a separate minibuffer frame
57 and the minibuffer is active and MINIBUF says it counts,
58 `walk-windows' includes the windows in the frame from which you
59 entered the minibuffer, as well as the minibuffer window.
61 ALL-FRAMES is the optional third argument.
62 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
63 ALL-FRAMES = `visible' means include windows on all visible frames.
64 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
65 ALL-FRAMES = t means include windows on all frames including invisible frames.
66 If ALL-FRAMES is a frame, it means include windows on that frame.
67 Anything else means restrict to the selected frame."
68 ;; If we start from the minibuffer window, don't fail to come back to it.
69 (if (window-minibuffer-p (selected-window))
70 (setq minibuf t))
71 (save-selected-window
72 (if (framep all-frames)
73 (select-window (frame-first-window all-frames)))
74 (let* (walk-windows-already-seen
75 (walk-windows-current (selected-window)))
76 (while (progn
77 (setq walk-windows-current
78 (next-window walk-windows-current minibuf all-frames))
79 (not (memq walk-windows-current walk-windows-already-seen)))
80 (setq walk-windows-already-seen
81 (cons walk-windows-current walk-windows-already-seen))
82 (funcall proc walk-windows-current)))))
84 (defun some-window (predicate &optional minibuf all-frames default)
85 "Return a window satisfying PREDICATE.
87 This function cycles through all visible windows using `walk-windows',
88 calling PREDICATE on each one. PREDICATE is called with a window as
89 argument. The first window for which PREDICATE returns a non-nil
90 value is returned. If no window satisfies PREDICATE, DEFAULT is
91 returned.
93 Optional second arg MINIBUF t means count the minibuffer window even
94 if not active. MINIBUF nil or omitted means count the minibuffer iff
95 it is active. MINIBUF neither t nor nil means not to count the
96 minibuffer even if it is active.
98 Several frames may share a single minibuffer; if the minibuffer
99 counts, all windows on all frames that share that minibuffer count
100 too. Therefore, if you are using a separate minibuffer frame
101 and the minibuffer is active and MINIBUF says it counts,
102 `walk-windows' includes the windows in the frame from which you
103 entered the minibuffer, as well as the minibuffer window.
105 ALL-FRAMES is the optional third argument.
106 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
107 ALL-FRAMES = `visible' means include windows on all visible frames.
108 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
109 ALL-FRAMES = t means include windows on all frames including invisible frames.
110 If ALL-FRAMES is a frame, it means include windows on that frame.
111 Anything else means restrict to the selected frame."
112 (catch 'found
113 (walk-windows #'(lambda (window)
114 (when (funcall predicate window)
115 (throw 'found window)))
116 minibuf all-frames)
117 default))
119 (defun minibuffer-window-active-p (window)
120 "Return t if WINDOW (a minibuffer window) is now active."
121 (eq window (active-minibuffer-window)))
123 (defmacro save-selected-window (&rest body)
124 "Execute BODY, then select the window that was selected before BODY."
125 (list 'let
126 '((save-selected-window-window (selected-window)))
127 (list 'unwind-protect
128 (cons 'progn body)
129 (list 'select-window 'save-selected-window-window))))
131 (defun count-windows (&optional minibuf)
132 "Returns the number of visible windows.
133 This counts the windows in the selected frame and (if the minibuffer is
134 to be counted) its minibuffer frame (if that's not the same frame).
135 The optional arg MINIBUF non-nil means count the minibuffer
136 even if it is inactive."
137 (let ((count 0))
138 (walk-windows (function (lambda (w)
139 (setq count (+ count 1))))
140 minibuf)
141 count))
143 (defun balance-windows ()
144 "Makes all visible windows the same height (approximately)."
145 (interactive)
146 (let ((count -1) levels newsizes size
147 ;; Don't count the lines that are above the uppermost windows.
148 ;; (These are the menu bar lines, if any.)
149 (mbl (nth 1 (window-edges (frame-first-window (selected-frame))))))
150 ;; Find all the different vpos's at which windows start,
151 ;; then count them. But ignore levels that differ by only 1.
152 (save-window-excursion
153 (let (tops (prev-top -2))
154 (walk-windows (function (lambda (w)
155 (setq tops (cons (nth 1 (window-edges w))
156 tops))))
157 'nomini)
158 (setq tops (sort tops '<))
159 (while tops
160 (if (> (car tops) (1+ prev-top))
161 (setq prev-top (car tops)
162 count (1+ count)))
163 (setq levels (cons (cons (car tops) count) levels))
164 (setq tops (cdr tops)))
165 (setq count (1+ count))))
166 ;; Subdivide the frame into that many vertical levels.
167 (setq size (/ (- (frame-height) mbl) count))
168 (walk-windows (function
169 (lambda (w)
170 (select-window w)
171 (let ((newtop (cdr (assq (nth 1 (window-edges))
172 levels)))
173 (newbot (or (cdr (assq (+ (window-height)
174 (nth 1 (window-edges)))
175 levels))
176 count)))
177 (setq newsizes
178 (cons (cons w (* size (- newbot newtop)))
179 newsizes)))))
180 'nomini)
181 (walk-windows (function (lambda (w)
182 (select-window w)
183 (let ((newsize (cdr (assq w newsizes))))
184 (enlarge-window (- newsize
185 (window-height))))))
186 'nomini)))
188 ;;; I think this should be the default; I think people will prefer it--rms.
189 (defcustom split-window-keep-point t
190 "*If non-nil, split windows keeps the original point in both children.
191 This is often more convenient for editing.
192 If nil, adjust point in each of the two windows to minimize redisplay.
193 This is convenient on slow terminals, but point can move strangely."
194 :type 'boolean
195 :group 'windows)
197 (defun split-window-vertically (&optional arg)
198 "Split current window into two windows, one above the other.
199 The uppermost window gets ARG lines and the other gets the rest.
200 Negative arg means select the size of the lowermost window instead.
201 With no argument, split equally or close to it.
202 Both windows display the same buffer now current.
204 If the variable `split-window-keep-point' is non-nil, both new windows
205 will get the same value of point as the current window. This is often
206 more convenient for editing.
208 Otherwise, we chose window starts so as to minimize the amount of
209 redisplay; this is convenient on slow terminals. The new selected
210 window is the one that the current value of point appears in. The
211 value of point can change if the text around point is hidden by the
212 new mode line."
213 (interactive "P")
214 (let ((old-w (selected-window))
215 (old-point (point))
216 (size (and arg (prefix-numeric-value arg)))
217 (window-full-p nil)
218 new-w bottom switch moved)
219 (and size (< size 0) (setq size (+ (window-height) size)))
220 (setq new-w (split-window nil size))
221 (or split-window-keep-point
222 (progn
223 (save-excursion
224 (set-buffer (window-buffer))
225 (goto-char (window-start))
226 (setq moved (vertical-motion (window-height)))
227 (set-window-start new-w (point))
228 (if (> (point) (window-point new-w))
229 (set-window-point new-w (point)))
230 (and (= moved (window-height))
231 (progn
232 (setq window-full-p t)
233 (vertical-motion -1)))
234 (setq bottom (point)))
235 (and window-full-p
236 (<= bottom (point))
237 (set-window-point old-w (1- bottom)))
238 (and window-full-p
239 (<= (window-start new-w) old-point)
240 (progn
241 (set-window-point new-w old-point)
242 (select-window new-w)))))
243 (split-window-save-restore-data new-w old-w)))
245 ;; This is to avoid compiler warnings.
246 (defvar view-return-to-alist)
248 (defun split-window-save-restore-data (new-w old-w)
249 (save-excursion
250 (set-buffer (window-buffer))
251 (if view-mode
252 (let ((old-info (assq old-w view-return-to-alist)))
253 (setq view-return-to-alist
254 (cons (cons new-w (cons (and old-info (car (cdr old-info))) t))
255 view-return-to-alist))))
256 new-w))
258 (defun split-window-horizontally (&optional arg)
259 "Split current window into two windows side by side.
260 This window becomes the leftmost of the two, and gets ARG columns.
261 Negative arg means select the size of the rightmost window instead.
262 The argument includes the width of the window's scroll bar; if there
263 are no scroll bars, it includes the width of the divider column
264 to the window's right, if any. No arg means split equally."
265 (interactive "P")
266 (let ((old-w (selected-window))
267 (size (and arg (prefix-numeric-value arg))))
268 (and size (< size 0)
269 (setq size (+ (window-width) size)))
270 (split-window-save-restore-data (split-window nil size t) old-w)))
272 (defcustom mode-line-window-height-fudge nil
273 "*Fudge factor returned by `mode-line-window-height-fudge' on graphic displays.
274 If non-nil, it should be the number of lines to add to the sizes of
275 windows to compensate for the extra height of the mode-line, so it
276 doesn't't obscure the last line of text.
278 If nil, an attempt is made to calculate reasonable value.
280 This is a kluge."
281 :type '(choice (const :tag "Guess" nil)
282 (integer :tag "Extra lines" :value 1))
283 :group 'windows)
285 ;; List of face attributes that might change a face's height
286 (defconst height-affecting-face-attributes
287 '(:family :height :box :font :inherit))
289 (defsubst mode-line-window-height-fudge (&optional face)
290 "Return a fudge factor to compensate for the extra height of graphic mode-lines.
291 On a non-graphic display, return 0.
293 FACE is the face used to display the mode-line; it defaults to `mode-line'.
295 If the variable `mode-line-window-height-fudge' has a non-nil value, it
296 is returned. Otherwise, the `mode-line' face is checked to see if it
297 contains any attributes that might affect its height; if it does, 1 is
298 returned, otherwise 0.
300 \[Because mode-lines on a graphics capable display may have a height
301 larger than a normal text line, a window who's size is calculated to
302 exactly show some text, including 1 line for the mode-line, may be
303 displayed with the last text line obscured by the mode-line.
305 To work-around this problem, call `mode-line-window-height-fudge', and
306 add the return value to the requested window size.]
308 This is a kluge."
309 (if (display-graphic-p)
311 ;; Return user-specified value
312 mode-line-window-height-fudge
313 ;; Try and detect whether mode-line face has any attributes that
314 ;; could make it bigger than a default text line, and return a
315 ;; fudge factor of 1 if so.
316 (let ((attrs height-affecting-face-attributes)
317 (fudge 0))
318 (while attrs
319 (let ((val (face-attribute (or face 'mode-line) (pop attrs))))
320 (unless (or (null val) (eq val 'unspecified))
321 (setq fudge 1 attrs nil))))
322 fudge))
326 ;;; These functions should eventually be replaced with versions that
327 ;;; really do the job (instead of using the kludgey mode-line face
328 ;;; hacking junk).
330 (defun window-text-height (&optional window)
331 "Return the height in lines of the text display area of WINDOW.
332 This doesn't include the mode-line (or header-line if any) or any
333 partial-height lines in the text display area.
335 Note that the current implementation of this function may sometimes
336 return an inaccurate value, but attempts to be conservative, by
337 returning fewer lines than actually exist in the case where the real
338 value cannot be determined."
339 (with-current-buffer (window-buffer window)
340 (- (window-height window)
341 (if (and (not (window-minibuffer-p window))
342 mode-line-format)
343 (1+ (mode-line-window-height-fudge))
345 (if header-line-format
346 (1+ (mode-line-window-height-fudge 'header-line))
347 0))))
349 (defun set-window-text-height (window height)
350 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
351 This doesn't include the mode-line (or header-line if any) or any
352 partial-height lines in the text display area.
354 If WINDOW is nil, the selected window is used.
356 Note that the current implementation of this function cannot always set
357 the height exactly, but attempts to be conservative, by allocating more
358 lines than are actually needed in the case where some error may be present."
359 (let ((delta (- height (window-text-height window))))
360 (unless (zerop delta)
361 (let ((window-min-height 1))
362 (if (and window (not (eq window (selected-window))))
363 (save-selected-window
364 (select-window window)
365 (enlarge-window delta))
366 (enlarge-window delta))))))
369 (defun enlarge-window-horizontally (arg)
370 "Make current window ARG columns wider."
371 (interactive "p")
372 (enlarge-window arg t))
374 (defun shrink-window-horizontally (arg)
375 "Make current window ARG columns narrower."
376 (interactive "p")
377 (shrink-window arg t))
379 (defun window-buffer-height (window)
380 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
381 (save-excursion
382 (set-buffer (window-buffer window))
383 (goto-char (point-min))
384 (let ((ignore-final-newline
385 ;; If buffer ends with a newline, ignore it when counting height
386 ;; unless point is after it.
387 (and (not (eobp)) (eq ?\n (char-after (1- (point-max)))))))
388 (+ 1 (nth 2 (compute-motion (point-min)
389 '(0 . 0)
390 (- (point-max) (if ignore-final-newline 1 0))
391 (cons 0 100000000)
392 (window-width window)
394 window))))))
396 (defun count-screen-lines (&optional beg end count-final-newline window)
397 "Return the number of screen lines in the region.
398 The number of screen lines may be different from the number of actual lines,
399 due to line breaking, display table, etc.
401 Optional arguments BEG and END default to `point-min' and `point-max'
402 respectively.
404 If region ends with a newline, ignore it unless optinal third argument
405 COUNT-FINAL-NEWLINE is non-nil.
407 The optional fourth argument WINDOW specifies the window used for obtaining
408 parameters such as width, horizontal scrolling, and so on. The default is
409 to use the selected window's parameters.
411 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
412 regardless of which buffer is displayed in WINDOW. This makes possible to use
413 `count-screen-lines' in any buffer, whether or not it is currently displayed
414 in some window."
415 (unless beg
416 (setq beg (point-min)))
417 (unless end
418 (setq end (point-max)))
419 (if (= beg end)
421 (save-excursion
422 (save-restriction
423 (widen)
424 (narrow-to-region (min beg end)
425 (if (and (not count-final-newline)
426 (= ?\n (char-before (max beg end))))
427 (1- (max beg end))
428 (max beg end)))
429 (goto-char (point-min))
430 (1+ (vertical-motion (buffer-size) window))))))
432 (defun fit-window-to-buffer (&optional window max-height min-height)
433 "Make WINDOW the right size to display its contents exactly.
434 If the optional argument MAX-HEIGHT is supplied, it is the maximum height
435 the window is allowed to be, defaulting to the frame height.
436 If the optional argument MIN-HEIGHT is supplied, it is the minimum
437 height the window is allowed to be, defaulting to `window-min-height'.
439 The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or
440 header-line."
441 (interactive)
443 (when (null window)
444 (setq window (selected-window)))
445 (when (null max-height)
446 (setq max-height (frame-height (window-frame window))))
448 (let* ((window-height
449 ;; The current height of WINDOW
450 (window-height window))
451 (extra
452 ;; The amount by which the text height differs from the window
453 ;; height.
454 (- window-height (window-text-height window)))
455 (text-height
456 ;; The height necessary to show the buffer displayed by WINDOW
457 ;; (`count-screen-lines' always works on the current buffer).
458 (with-current-buffer (window-buffer window)
459 (count-screen-lines)))
460 (delta
461 ;; Calculate how much the window height has to change to show
462 ;; text-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
463 (- (max (min (+ text-height extra) max-height)
464 (or min-height window-min-height))
465 window-height))
466 ;; We do our own height checking, so avoid any restrictions due to
467 ;; window-min-height.
468 (window-min-height 1))
470 ;; Don't try to redisplay with the cursor at the end
471 ;; on its own line--that would force a scroll and spoil things.
472 (if (with-current-buffer (window-buffer window)
473 (and (eobp) (bolp) (not (bobp))))
474 (set-window-point window (1- (window-point window))))
476 (unless (zerop delta)
477 (if (eq window (selected-window))
478 (enlarge-window delta)
479 (save-selected-window
480 (select-window window)
481 (enlarge-window delta))))))
483 (defun shrink-window-if-larger-than-buffer (&optional window)
484 "Shrink the WINDOW to be as small as possible to display its contents.
485 Do not shrink to less than `window-min-height' lines.
486 Do nothing if the buffer contains more lines than the present window height,
487 or if some of the window's contents are scrolled out of view,
488 or if the window is not the full width of the frame,
489 or if the window is the only window of its frame."
490 (interactive)
491 (when (null window)
492 (setq window (selected-window)))
493 (let* ((frame (window-frame window))
494 (mini (frame-parameter frame 'minibuffer))
495 (edges (window-edges window)))
496 (if (and (not (eq window (frame-root-window frame)))
497 (= (window-width) (frame-width))
498 (pos-visible-in-window-p (point-min) window)
499 (not (eq mini 'only))
500 (or (not mini)
501 (< (nth 3 edges) (nth 1 (window-edges mini)))
502 (> (nth 1 edges) (frame-parameter frame 'menu-bar-lines))))
503 (fit-window-to-buffer window (window-height window)))))
505 (defun kill-buffer-and-window ()
506 "Kill the current buffer and delete the selected window."
507 (interactive)
508 (if (yes-or-no-p (format "Kill buffer `%s'? " (buffer-name)))
509 (let ((buffer (current-buffer)))
510 (delete-window (selected-window))
511 (kill-buffer buffer))
512 (error "Aborted")))
514 (defun quit-window (&optional kill window)
515 "Quit the current buffer. Bury it, and maybe delete the selected frame.
516 \(The frame is deleted if it is contains a dedicated window for the buffer.)
517 With a prefix argument, kill the buffer instead.
519 Noninteractively, if KILL is non-nil, then kill the current buffer,
520 otherwise bury it.
522 If WINDOW is non-nil, it specifies a window; we delete that window,
523 and the buffer that is killed or buried is the one in that window."
524 (interactive "P")
525 (let ((buffer (window-buffer window))
526 (frame (window-frame (or window (selected-window))))
527 (window-solitary
528 (save-selected-window
529 (if window
530 (select-window window))
531 (one-window-p t)))
532 window-handled)
534 (save-selected-window
535 (if window
536 (select-window window))
537 (or (window-minibuffer-p)
538 (window-dedicated-p (selected-window))
539 (switch-to-buffer (other-buffer))))
541 ;; Get rid of the frame, if it has just one dedicated window
542 ;; and other visible frames exist.
543 (and (or (window-minibuffer-p) (window-dedicated-p window))
544 (delq frame (visible-frame-list))
545 window-solitary
546 (if (and (eq default-minibuffer-frame frame)
547 (= 1 (length (minibuffer-frame-list))))
548 (setq window nil)
549 (delete-frame frame)
550 (setq window-handled t)))
552 ;; Deal with the buffer.
553 (if kill
554 (kill-buffer buffer)
555 (bury-buffer buffer))
557 ;; Maybe get rid of the window.
558 (and window (not window-handled) (not window-solitary)
559 (delete-window window))))
561 (define-key ctl-x-map "2" 'split-window-vertically)
562 (define-key ctl-x-map "3" 'split-window-horizontally)
563 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
564 (define-key ctl-x-map "{" 'shrink-window-horizontally)
565 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
566 (define-key ctl-x-map "+" 'balance-windows)
567 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
569 ;;; windows.el ends here