]> code.delx.au - gnu-emacs/blob - lisp/window.el
(fit-window-to-buffer): Handle windows without mode-lines.
[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 (and (not (window-minibuffer-p window))
342 mode-line-format)
343 (1+ (mode-line-window-height-fudge))
344 0)
345 (if header-line-format
346 (1+ (mode-line-window-height-fudge 'header-line))
347 0))))
348
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.
353
354 If WINDOW is nil, the selected window is used.
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 (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))))))
367
368 \f
369 (defun enlarge-window-horizontally (arg)
370 "Make current window ARG columns wider."
371 (interactive "p")
372 (enlarge-window arg t))
373
374 (defun shrink-window-horizontally (arg)
375 "Make current window ARG columns narrower."
376 (interactive "p")
377 (shrink-window arg t))
378
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)
393 nil
394 window))))))
395
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.
400
401 Optional arguments BEG and END default to `point-min' and `point-max'
402 respectively.
403
404 If region ends with a newline, ignore it unless optinal third argument
405 COUNT-FINAL-NEWLINE is non-nil.
406
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.
410
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)
420 0
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))))))
431
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'.
438
439 The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or
440 header-line."
441 (interactive)
442
443 (when (null window)
444 (setq window (selected-window)))
445 (when (null max-height)
446 (setq max-height (frame-height (window-frame window))))
447
448 (let* ((buf
449 ;; Buffer that is displayed in WINDOW
450 (window-buffer window))
451 (window-height
452 ;; The current height of WINDOW
453 (window-height window))
454 (desired-height
455 ;; The height necessary to show the buffer displayed by WINDOW
456 ;; (`count-screen-lines' always works on the current buffer).
457 (with-current-buffer buf
458 (+ (count-screen-lines)
459 ;; For non-minibuffers, count the mode-line, if any
460 (if (and (not (window-minibuffer-p window))
461 mode-line-format)
462 1 0)
463 ;; Count the header-line, if any
464 (if header-line-format 1 0))))
465 (delta
466 ;; Calculate how much the window height has to change to show
467 ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
468 (- (max (min desired-height max-height)
469 (or min-height window-min-height))
470 window-height))
471 ;; We do our own height checking, so avoid any restrictions due to
472 ;; window-min-height.
473 (window-min-height 1))
474
475 ;; Don't try to redisplay with the cursor at the end
476 ;; on its own line--that would force a scroll and spoil things.
477 (when (with-current-buffer buf
478 (and (eobp) (bolp) (not (bobp))))
479 (set-window-point window (1- (window-point window))))
480
481 (save-selected-window
482 (select-window window)
483
484 ;; Adjust WINDOW to the nominally correct size (which may actually
485 ;; be slightly off because of variable height text, etc).
486 (unless (zerop delta)
487 (enlarge-window delta))
488
489 ;; Check if the last line is surely fully visible. If not,
490 ;; enlarge the window.
491 (let ((end (with-current-buffer buf
492 (save-excursion
493 (goto-char (point-max))
494 (if (and (bolp) (not (bobp)))
495 (1- (point))
496 (point))))))
497 (set-window-vscroll window 0)
498 (while (and (< desired-height max-height)
499 (= desired-height (window-height window))
500 (not (pos-visible-in-window-p end window t)))
501 (enlarge-window 1)
502 (setq desired-height (1+ desired-height)))))))
503
504 (defun shrink-window-if-larger-than-buffer (&optional window)
505 "Shrink the WINDOW to be as small as possible to display its contents.
506 Do not shrink to less than `window-min-height' lines.
507 Do nothing if the buffer contains more lines than the present window height,
508 or if some of the window's contents are scrolled out of view,
509 or if the window is not the full width of the frame,
510 or if the window is the only window of its frame."
511 (interactive)
512 (when (null window)
513 (setq window (selected-window)))
514 (let* ((frame (window-frame window))
515 (mini (frame-parameter frame 'minibuffer))
516 (edges (window-edges window)))
517 (if (and (not (eq window (frame-root-window frame)))
518 (= (window-width) (frame-width))
519 (pos-visible-in-window-p (point-min) window)
520 (not (eq mini 'only))
521 (or (not mini)
522 (< (nth 3 edges) (nth 1 (window-edges mini)))
523 (> (nth 1 edges) (frame-parameter frame 'menu-bar-lines))))
524 (fit-window-to-buffer window (window-height window)))))
525
526 (defun kill-buffer-and-window ()
527 "Kill the current buffer and delete the selected window."
528 (interactive)
529 (if (yes-or-no-p (format "Kill buffer `%s'? " (buffer-name)))
530 (let ((buffer (current-buffer)))
531 (delete-window (selected-window))
532 (kill-buffer buffer))
533 (error "Aborted")))
534
535 (defun quit-window (&optional kill window)
536 "Quit the current buffer. Bury it, and maybe delete the selected frame.
537 \(The frame is deleted if it is contains a dedicated window for the buffer.)
538 With a prefix argument, kill the buffer instead.
539
540 Noninteractively, if KILL is non-nil, then kill the current buffer,
541 otherwise bury it.
542
543 If WINDOW is non-nil, it specifies a window; we delete that window,
544 and the buffer that is killed or buried is the one in that window."
545 (interactive "P")
546 (let ((buffer (window-buffer window))
547 (frame (window-frame (or window (selected-window))))
548 (window-solitary
549 (save-selected-window
550 (if window
551 (select-window window))
552 (one-window-p t)))
553 window-handled)
554
555 (save-selected-window
556 (if window
557 (select-window window))
558 (or (window-minibuffer-p)
559 (window-dedicated-p (selected-window))
560 (switch-to-buffer (other-buffer))))
561
562 ;; Get rid of the frame, if it has just one dedicated window
563 ;; and other visible frames exist.
564 (and (or (window-minibuffer-p) (window-dedicated-p window))
565 (delq frame (visible-frame-list))
566 window-solitary
567 (if (and (eq default-minibuffer-frame frame)
568 (= 1 (length (minibuffer-frame-list))))
569 (setq window nil)
570 (delete-frame frame)
571 (setq window-handled t)))
572
573 ;; Deal with the buffer.
574 (if kill
575 (kill-buffer buffer)
576 (bury-buffer buffer))
577
578 ;; Maybe get rid of the window.
579 (and window (not window-handled) (not window-solitary)
580 (delete-window window))))
581
582 (define-key ctl-x-map "2" 'split-window-vertically)
583 (define-key ctl-x-map "3" 'split-window-horizontally)
584 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
585 (define-key ctl-x-map "{" 'shrink-window-horizontally)
586 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
587 (define-key ctl-x-map "+" 'balance-windows)
588 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
589
590 ;;; windows.el ends here