]> code.delx.au - gnu-emacs/blob - lisp/window.el
(window-text-height, set-window-text-height): New functions.
[gnu-emacs] / lisp / window.el
1 ;;; window.el --- GNU Emacs window commands aside from those written in C.
2
3 ;; Copyright (C) 1985, 1989, 1992, 1993, 1994, 2000
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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.
24
25 ;;; Code:
26
27 ;;;; Window tree functions.
28
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.
33
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))))
44
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.
48
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.
53
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.
60
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)))))
83
84 (defun some-window (predicate &optional minibuf all-frames default)
85 "Return a window satisfying PREDICATE.
86
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.
92
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.
97
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.
104
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))
118
119 (defun minibuffer-window-active-p (window)
120 "Return t if WINDOW (a minibuffer window) is now active."
121 (eq window (active-minibuffer-window)))
122
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))))
130 \f
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))
142
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)))
187 \f
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)
196
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.
203
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.
207
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)))
244
245 ;; This is to avoid compiler warnings.
246 (defvar view-return-to-alist)
247
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))
257
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)))
271 \f
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.
277
278 If nil, an attempt is made to calculate reasonable value.
279
280 This is a kluge."
281 :type '(choice (const :tag "Guess" nil)
282 (integer :tag "Extra lines" :value 1))
283 :group 'windows)
284
285 ;; List of face attributes that might change a face's height
286 (defconst height-affecting-face-attributes
287 '(:family :height :box :font :inherit))
288
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.
292
293 FACE is the face used to display the mode-line; it defaults to `mode-line'.
294
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.
299
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.
304
305 To work-around this problem, call `mode-line-window-height-fudge', and
306 add the return value to the requested window size.]
307
308 This is a kluge."
309 (if (display-graphic-p)
310 (or
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))
323 0))
324
325 \f
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).
329
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.
334
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 mode-line-format
342 (1+ (mode-line-window-height-fudge))
343 0)
344 (if header-line-format
345 (1+ (mode-line-window-height-fudge 'header-line))
346 0))))
347
348 (defun set-window-text-height (window height)
349 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
350 This doesn't include the mode-line (or header-line if any) or any
351 partial-height lines in the text display area.
352
353 If WINDOW is nil, the selected window is used.
354 If HEIGHT is less than `window-min-height', then WINDOW is deleted.
355
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 (if (and window (not (eq window (selected-window))))
362 (save-selected-window
363 (select-window window)
364 (enlarge-window delta))
365 (enlarge-window delta)))))
366
367 \f
368 (defun enlarge-window-horizontally (arg)
369 "Make current window ARG columns wider."
370 (interactive "p")
371 (enlarge-window arg t))
372
373 (defun shrink-window-horizontally (arg)
374 "Make current window ARG columns narrower."
375 (interactive "p")
376 (shrink-window arg t))
377
378 (defun window-buffer-height (window)
379 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
380 (save-excursion
381 (set-buffer (window-buffer window))
382 (goto-char (point-min))
383 (let ((ignore-final-newline
384 ;; If buffer ends with a newline, ignore it when counting height
385 ;; unless point is after it.
386 (and (not (eobp)) (eq ?\n (char-after (1- (point-max)))))))
387 (+ 1 (nth 2 (compute-motion (point-min)
388 '(0 . 0)
389 (- (point-max) (if ignore-final-newline 1 0))
390 (cons 0 100000000)
391 (window-width window)
392 nil
393 window))))))
394
395 (defun count-screen-lines (&optional beg end count-final-newline window)
396 "Return the number of screen lines in the region.
397 The number of screen lines may be different from the number of actual lines,
398 due to line breaking, display table, etc.
399
400 Optional arguments BEG and END default to `point-min' and `point-max'
401 respectively.
402
403 If region ends with a newline, ignore it unless optinal third argument
404 COUNT-FINAL-NEWLINE is non-nil.
405
406 The optional fourth argument WINDOW specifies the window used for obtaining
407 parameters such as width, horizontal scrolling, and so on. The default is
408 to use the selected window's parameters.
409
410 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
411 regardless of which buffer is displayed in WINDOW. This makes possible to use
412 `count-screen-lines' in any buffer, whether or not it is currently displayed
413 in some window."
414 (unless beg
415 (setq beg (point-min)))
416 (unless end
417 (setq end (point-max)))
418 (if (= beg end)
419 0
420 (save-excursion
421 (save-restriction
422 (widen)
423 (narrow-to-region (min beg end)
424 (if (and (not count-final-newline)
425 (= ?\n (char-before (max beg end))))
426 (1- (max beg end))
427 (max beg end)))
428 (goto-char (point-min))
429 (1+ (vertical-motion (buffer-size) window))))))
430
431 (defun shrink-window-if-larger-than-buffer (&optional window)
432 "Shrink the WINDOW to be as small as possible to display its contents.
433 Do not shrink to less than `window-min-height' lines.
434 Do nothing if the buffer contains more lines than the present window height,
435 or if some of the window's contents are scrolled out of view,
436 or if the window is not the full width of the frame,
437 or if the window is the only window of its frame."
438 (interactive)
439 (save-selected-window
440 (if window
441 (select-window window)
442 (setq window (selected-window)))
443 (let* ((mini (frame-parameter nil 'minibuffer))
444 (edges (window-edges)))
445 (if (and (< 1 (count-windows))
446 (= (window-width) (frame-width))
447 (pos-visible-in-window-p (point-min) window)
448 (not (eq mini 'only))
449 (or (not mini)
450 (< (nth 3 edges) (nth 1 (window-edges mini)))
451 (> (nth 1 edges) (frame-parameter nil 'menu-bar-lines))))
452
453 ;; `count-screen-lines' always works on the current buffer, so
454 ;; make sure it is the buffer displayed by WINDOW.
455 (let ((text-height
456 (with-current-buffer (window-buffer window)
457 (count-screen-lines)))
458 (window-height
459 (window-text-height)))
460
461 ;; Don't try to redisplay with the cursor at the end
462 ;; on its own line--that would force a scroll and spoil things.
463 (when (and (eobp) (bolp) (not (bobp)))
464 (forward-char -1))
465
466 (when (> window-height text-height)
467 (shrink-window
468 (- window-height (max text-height window-min-height)))))))))
469
470 (defun kill-buffer-and-window ()
471 "Kill the current buffer and delete the selected window."
472 (interactive)
473 (if (yes-or-no-p (format "Kill buffer `%s'? " (buffer-name)))
474 (let ((buffer (current-buffer)))
475 (delete-window (selected-window))
476 (kill-buffer buffer))
477 (error "Aborted")))
478
479 (defun quit-window (&optional kill window)
480 "Quit the current buffer. Bury it, and maybe delete the selected frame.
481 \(The frame is deleted if it is contains a dedicated window for the buffer.)
482 With a prefix argument, kill the buffer instead.
483
484 Noninteractively, if KILL is non-nil, then kill the current buffer,
485 otherwise bury it.
486
487 If WINDOW is non-nil, it specifies a window; we delete that window,
488 and the buffer that is killed or buried is the one in that window."
489 (interactive "P")
490 (let ((buffer (window-buffer window))
491 (frame (window-frame (or window (selected-window))))
492 (window-solitary
493 (save-selected-window
494 (if window
495 (select-window window))
496 (one-window-p t)))
497 window-handled)
498
499 (save-selected-window
500 (if window
501 (select-window window))
502 (or (window-minibuffer-p)
503 (window-dedicated-p (selected-window))
504 (switch-to-buffer (other-buffer))))
505
506 ;; Get rid of the frame, if it has just one dedicated window
507 ;; and other visible frames exist.
508 (and (or (window-minibuffer-p) (window-dedicated-p window))
509 (delq frame (visible-frame-list))
510 window-solitary
511 (if (and (eq default-minibuffer-frame frame)
512 (= 1 (length (minibuffer-frame-list))))
513 (setq window nil)
514 (delete-frame frame)
515 (setq window-handled t)))
516
517 ;; Deal with the buffer.
518 (if kill
519 (kill-buffer buffer)
520 (bury-buffer buffer))
521
522 ;; Maybe get rid of the window.
523 (and window (not window-handled) (not window-solitary)
524 (delete-window window))))
525
526 (define-key ctl-x-map "2" 'split-window-vertically)
527 (define-key ctl-x-map "3" 'split-window-horizontally)
528 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
529 (define-key ctl-x-map "{" 'shrink-window-horizontally)
530 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
531 (define-key ctl-x-map "+" 'balance-windows)
532 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
533
534 ;;; windows.el ends here