(gnus-configure-windows): switch-to-buffer -> set-buffer.
[emacs.git] / lisp / window.el
blob86cb45f92e58d9dd71eb266a5d63db0f6f69faa2
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.
26 ;;; Commentary:
28 ;; Window tree functions.
30 ;;; Code:
32 (defun one-window-p (&optional nomini all-frames)
33 "Return non-nil if the selected window is the only window (in its frame).
34 Optional arg NOMINI non-nil means don't count the minibuffer
35 even if it is active.
37 The optional arg ALL-FRAMES t means count windows on all frames.
38 If it is `visible', count windows on all visible frames.
39 ALL-FRAMES nil or omitted means count only the selected frame,
40 plus the minibuffer it uses (which may be on another frame).
41 If ALL-FRAMES is neither nil nor t, count only the selected frame."
42 (let ((base-window (selected-window)))
43 (if (and nomini (eq base-window (minibuffer-window)))
44 (setq base-window (next-window base-window)))
45 (eq base-window
46 (next-window base-window (if nomini 'arg) all-frames))))
48 (defun walk-windows (proc &optional minibuf all-frames)
49 "Cycle through all visible windows, calling PROC for each one.
50 PROC is called with a window as argument.
52 Optional second arg MINIBUF t means count the minibuffer window even
53 if not active. MINIBUF nil or omitted means count the minibuffer iff
54 it is active. MINIBUF neither t nor nil means not to count the
55 minibuffer even if it is active.
57 Several frames may share a single minibuffer; if the minibuffer
58 counts, all windows on all frames that share that minibuffer count
59 too. Therefore, if you are using a separate minibuffer frame
60 and the minibuffer is active and MINIBUF says it counts,
61 `walk-windows' includes the windows in the frame from which you
62 entered the minibuffer, as well as the minibuffer window.
64 ALL-FRAMES is the optional third argument.
65 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
66 ALL-FRAMES = `visible' means include windows on all visible frames.
67 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
68 ALL-FRAMES = t means include windows on all frames including invisible frames.
69 If ALL-FRAMES is a frame, it means include windows on that frame.
70 Anything else means restrict to the selected frame."
71 ;; If we start from the minibuffer window, don't fail to come back to it.
72 (if (window-minibuffer-p (selected-window))
73 (setq minibuf t))
74 (save-selected-window
75 (if (framep all-frames)
76 (select-window (frame-first-window all-frames)))
77 (let* (walk-windows-already-seen
78 (walk-windows-current (selected-window)))
79 (while (progn
80 (setq walk-windows-current
81 (next-window walk-windows-current minibuf all-frames))
82 (not (memq walk-windows-current walk-windows-already-seen)))
83 (setq walk-windows-already-seen
84 (cons walk-windows-current walk-windows-already-seen))
85 (funcall proc walk-windows-current)))))
87 (defun some-window (predicate &optional minibuf all-frames default)
88 "Return a window satisfying PREDICATE.
90 This function cycles through all visible windows using `walk-windows',
91 calling PREDICATE on each one. PREDICATE is called with a window as
92 argument. The first window for which PREDICATE returns a non-nil
93 value is returned. If no window satisfies PREDICATE, DEFAULT is
94 returned.
96 Optional second arg MINIBUF t means count the minibuffer window even
97 if not active. MINIBUF nil or omitted means count the minibuffer iff
98 it is active. MINIBUF neither t nor nil means not to count the
99 minibuffer even if it is active.
101 Several frames may share a single minibuffer; if the minibuffer
102 counts, all windows on all frames that share that minibuffer count
103 too. Therefore, if you are using a separate minibuffer frame
104 and the minibuffer is active and MINIBUF says it counts,
105 `walk-windows' includes the windows in the frame from which you
106 entered the minibuffer, as well as the minibuffer window.
108 ALL-FRAMES is the optional third argument.
109 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
110 ALL-FRAMES = `visible' means include windows on all visible frames.
111 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
112 ALL-FRAMES = t means include windows on all frames including invisible frames.
113 If ALL-FRAMES is a frame, it means include windows on that frame.
114 Anything else means restrict to the selected frame."
115 (catch 'found
116 (walk-windows #'(lambda (window)
117 (when (funcall predicate window)
118 (throw 'found window)))
119 minibuf all-frames)
120 default))
122 (defun minibuffer-window-active-p (window)
123 "Return t if WINDOW (a minibuffer window) is now active."
124 (eq window (active-minibuffer-window)))
126 (defmacro save-selected-window (&rest body)
127 "Execute BODY, then select the window that was selected before BODY."
128 `(let ((save-selected-window-window (selected-window)))
129 (unwind-protect
130 (progn ,@body)
131 (select-window save-selected-window-window))))
133 (defun count-windows (&optional minibuf)
134 "Return the number of visible windows.
135 This counts the windows in the selected frame and (if the minibuffer is
136 to be counted) its minibuffer frame (if that's not the same frame).
137 The optional arg MINIBUF non-nil means count the minibuffer
138 even if it is inactive."
139 (let ((count 0))
140 (walk-windows (function (lambda (w)
141 (setq count (+ count 1))))
142 minibuf)
143 count))
145 (defun window-safely-shrinkable-p (&optional window)
146 "Non-nil if the WINDOW can be shrunk without shrinking other windows."
147 (save-selected-window
148 (when window (select-window window))
149 (or (and (not (eq window (frame-first-window)))
150 (= (car (window-edges))
151 (car (window-edges (previous-window)))))
152 (= (car (window-edges))
153 (car (window-edges (next-window)))))))
155 (defun balance-windows ()
156 "Make all visible windows the same height (approximately)."
157 (interactive)
158 (let ((count -1) levels newsizes size
159 ;; Don't count the lines that are above the uppermost windows.
160 ;; (These are the menu bar lines, if any.)
161 (mbl (nth 1 (window-edges (frame-first-window (selected-frame))))))
162 ;; Find all the different vpos's at which windows start,
163 ;; then count them. But ignore levels that differ by only 1.
164 (save-window-excursion
165 (let (tops (prev-top -2))
166 (walk-windows (function (lambda (w)
167 (setq tops (cons (nth 1 (window-edges w))
168 tops))))
169 'nomini)
170 (setq tops (sort tops '<))
171 (while tops
172 (if (> (car tops) (1+ prev-top))
173 (setq prev-top (car tops)
174 count (1+ count)))
175 (setq levels (cons (cons (car tops) count) levels))
176 (setq tops (cdr tops)))
177 (setq count (1+ count))))
178 ;; Subdivide the frame into that many vertical levels.
179 (setq size (/ (- (frame-height) mbl) count))
180 (walk-windows (function
181 (lambda (w)
182 (select-window w)
183 (let ((newtop (cdr (assq (nth 1 (window-edges))
184 levels)))
185 (newbot (or (cdr (assq (+ (window-height)
186 (nth 1 (window-edges)))
187 levels))
188 count)))
189 (setq newsizes
190 (cons (cons w (* size (- newbot newtop)))
191 newsizes)))))
192 'nomini)
193 (walk-windows (function (lambda (w)
194 (select-window w)
195 (let ((newsize (cdr (assq w newsizes))))
196 (enlarge-window (- newsize
197 (window-height))))))
198 'nomini)))
200 ;;; I think this should be the default; I think people will prefer it--rms.
201 (defcustom split-window-keep-point t
202 "*If non-nil, split windows keeps the original point in both children.
203 This is often more convenient for editing.
204 If nil, adjust point in each of the two windows to minimize redisplay.
205 This is convenient on slow terminals, but point can move strangely."
206 :type 'boolean
207 :group 'windows)
209 (defun split-window-vertically (&optional arg)
210 "Split current window into two windows, one above the other.
211 The uppermost window gets ARG lines and the other gets the rest.
212 Negative arg means select the size of the lowermost window instead.
213 With no argument, split equally or close to it.
214 Both windows display the same buffer now current.
216 If the variable `split-window-keep-point' is non-nil, both new windows
217 will get the same value of point as the current window. This is often
218 more convenient for editing.
220 Otherwise, we chose window starts so as to minimize the amount of
221 redisplay; this is convenient on slow terminals. The new selected
222 window is the one that the current value of point appears in. The
223 value of point can change if the text around point is hidden by the
224 new mode line."
225 (interactive "P")
226 (let ((old-w (selected-window))
227 (old-point (point))
228 (size (and arg (prefix-numeric-value arg)))
229 (window-full-p nil)
230 new-w bottom switch moved)
231 (and size (< size 0) (setq size (+ (window-height) size)))
232 (setq new-w (split-window nil size))
233 (or split-window-keep-point
234 (progn
235 (save-excursion
236 (set-buffer (window-buffer))
237 (goto-char (window-start))
238 (setq moved (vertical-motion (window-height)))
239 (set-window-start new-w (point))
240 (if (> (point) (window-point new-w))
241 (set-window-point new-w (point)))
242 (and (= moved (window-height))
243 (progn
244 (setq window-full-p t)
245 (vertical-motion -1)))
246 (setq bottom (point)))
247 (and window-full-p
248 (<= bottom (point))
249 (set-window-point old-w (1- bottom)))
250 (and window-full-p
251 (<= (window-start new-w) old-point)
252 (progn
253 (set-window-point new-w old-point)
254 (select-window new-w)))))
255 (split-window-save-restore-data new-w old-w)))
257 ;; This is to avoid compiler warnings.
258 (defvar view-return-to-alist)
260 (defun split-window-save-restore-data (new-w old-w)
261 (save-excursion
262 (set-buffer (window-buffer))
263 (if view-mode
264 (let ((old-info (assq old-w view-return-to-alist)))
265 (setq view-return-to-alist
266 (cons (cons new-w (cons (and old-info (car (cdr old-info))) t))
267 view-return-to-alist))))
268 new-w))
270 (defun split-window-horizontally (&optional arg)
271 "Split current window into two windows side by side.
272 This window becomes the leftmost of the two, and gets ARG columns.
273 Negative arg means select the size of the rightmost window instead.
274 The argument includes the width of the window's scroll bar; if there
275 are no scroll bars, it includes the width of the divider column
276 to the window's right, if any. No arg means split equally."
277 (interactive "P")
278 (let ((old-w (selected-window))
279 (size (and arg (prefix-numeric-value arg))))
280 (and size (< size 0)
281 (setq size (+ (window-width) size)))
282 (split-window-save-restore-data (split-window nil size t) old-w)))
284 (defcustom mode-line-window-height-fudge nil
285 "*Fudge factor returned by `mode-line-window-height-fudge' on graphic displays.
286 If non-nil, it should be the number of lines to add to the sizes of
287 windows to compensate for the extra height of the mode-line, so it
288 doesn't't obscure the last line of text.
290 If nil, an attempt is made to calculate reasonable value.
292 This is a kluge."
293 :type '(choice (const :tag "Guess" nil)
294 (integer :tag "Extra lines" :value 1))
295 :group 'windows)
297 ;; List of face attributes that might change a face's height
298 (defconst height-affecting-face-attributes
299 '(:family :height :box :font :inherit))
301 (defsubst mode-line-window-height-fudge (&optional face)
302 "Return a fudge factor to compensate for the extra height of graphic mode-lines.
303 On a non-graphic display, return 0.
305 FACE is the face used to display the mode-line; it defaults to `mode-line'.
307 If the variable `mode-line-window-height-fudge' has a non-nil value, it
308 is returned. Otherwise, the `mode-line' face is checked to see if it
309 contains any attributes that might affect its height; if it does, 1 is
310 returned, otherwise 0.
312 \[Because mode-lines on a graphics capable display may have a height
313 larger than a normal text line, a window who's size is calculated to
314 exactly show some text, including 1 line for the mode-line, may be
315 displayed with the last text line obscured by the mode-line.
317 To work-around this problem, call `mode-line-window-height-fudge', and
318 add the return value to the requested window size.]
320 This is a kluge."
321 (if (display-graphic-p)
323 ;; Return user-specified value
324 mode-line-window-height-fudge
325 ;; Try and detect whether mode-line face has any attributes that
326 ;; could make it bigger than a default text line, and return a
327 ;; fudge factor of 1 if so.
328 (let ((attrs height-affecting-face-attributes)
329 (fudge 0))
330 (while attrs
331 (let ((val (face-attribute (or face 'mode-line) (pop attrs))))
332 (unless (or (null val) (eq val 'unspecified))
333 (setq fudge 1 attrs nil))))
334 fudge))
338 ;;; These functions should eventually be replaced with versions that
339 ;;; really do the job (instead of using the kludgey mode-line face
340 ;;; hacking junk).
342 (defun window-text-height (&optional window)
343 "Return the height in lines of the text display area of WINDOW.
344 This doesn't include the mode-line (or header-line if any) or any
345 partial-height lines in the text display area.
347 Note that the current implementation of this function may sometimes
348 return an inaccurate value, but attempts to be conservative, by
349 returning fewer lines than actually exist in the case where the real
350 value cannot be determined."
351 (with-current-buffer (window-buffer window)
352 (- (window-height window)
353 (if (and (not (window-minibuffer-p window))
354 mode-line-format)
355 (1+ (mode-line-window-height-fudge))
357 (if header-line-format
358 (1+ (mode-line-window-height-fudge 'header-line))
359 0))))
361 (defun set-window-text-height (window height)
362 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
363 This doesn't include the mode-line (or header-line if any) or any
364 partial-height lines in the text display area.
366 If WINDOW is nil, the selected window is used.
368 Note that the current implementation of this function cannot always set
369 the height exactly, but attempts to be conservative, by allocating more
370 lines than are actually needed in the case where some error may be present."
371 (let ((delta (- height (window-text-height window))))
372 (unless (zerop delta)
373 (let ((window-min-height 1))
374 (if (and window (not (eq window (selected-window))))
375 (save-selected-window
376 (select-window window)
377 (enlarge-window delta))
378 (enlarge-window delta))))))
381 (defun enlarge-window-horizontally (arg)
382 "Make current window ARG columns wider."
383 (interactive "p")
384 (enlarge-window arg t))
386 (defun shrink-window-horizontally (arg)
387 "Make current window ARG columns narrower."
388 (interactive "p")
389 (shrink-window arg t))
391 (defun window-buffer-height (window)
392 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
393 (save-excursion
394 (set-buffer (window-buffer window))
395 (goto-char (point-min))
396 (let ((ignore-final-newline
397 ;; If buffer ends with a newline, ignore it when counting height
398 ;; unless point is after it.
399 (and (not (eobp)) (eq ?\n (char-after (1- (point-max)))))))
400 (+ 1 (nth 2 (compute-motion (point-min)
401 '(0 . 0)
402 (- (point-max) (if ignore-final-newline 1 0))
403 (cons 0 100000000)
404 (window-width window)
406 window))))))
408 (defun count-screen-lines (&optional beg end count-final-newline window)
409 "Return the number of screen lines in the region.
410 The number of screen lines may be different from the number of actual lines,
411 due to line breaking, display table, etc.
413 Optional arguments BEG and END default to `point-min' and `point-max'
414 respectively.
416 If region ends with a newline, ignore it unless optional third argument
417 COUNT-FINAL-NEWLINE is non-nil.
419 The optional fourth argument WINDOW specifies the window used for obtaining
420 parameters such as width, horizontal scrolling, and so on. The default is
421 to use the selected window's parameters.
423 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
424 regardless of which buffer is displayed in WINDOW. This makes possible to use
425 `count-screen-lines' in any buffer, whether or not it is currently displayed
426 in some window."
427 (unless beg
428 (setq beg (point-min)))
429 (unless end
430 (setq end (point-max)))
431 (if (= beg end)
433 (save-excursion
434 (save-restriction
435 (widen)
436 (narrow-to-region (min beg end)
437 (if (and (not count-final-newline)
438 (= ?\n (char-before (max beg end))))
439 (1- (max beg end))
440 (max beg end)))
441 (goto-char (point-min))
442 (1+ (vertical-motion (buffer-size) window))))))
444 (defun fit-window-to-buffer (&optional window max-height min-height)
445 "Make WINDOW the right size to display its contents exactly.
446 If the optional argument MAX-HEIGHT is supplied, it is the maximum height
447 the window is allowed to be, defaulting to the frame height.
448 If the optional argument MIN-HEIGHT is supplied, it is the minimum
449 height the window is allowed to be, defaulting to `window-min-height'.
451 The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or
452 header-line."
453 (interactive)
455 (when (null window)
456 (setq window (selected-window)))
457 (when (null max-height)
458 (setq max-height (frame-height (window-frame window))))
460 (let* ((buf
461 ;; Buffer that is displayed in WINDOW
462 (window-buffer window))
463 (window-height
464 ;; The current height of WINDOW
465 (window-height window))
466 (desired-height
467 ;; The height necessary to show the buffer displayed by WINDOW
468 ;; (`count-screen-lines' always works on the current buffer).
469 (with-current-buffer buf
470 (+ (count-screen-lines)
471 ;; If the buffer is empty, (count-screen-lines) is
472 ;; zero. But, even in that case, we need one text line
473 ;; for cursor.
474 (if (= (point-min) (point-max))
475 1 0)
476 ;; For non-minibuffers, count the mode-line, if any
477 (if (and (not (window-minibuffer-p window))
478 mode-line-format)
479 1 0)
480 ;; Count the header-line, if any
481 (if header-line-format 1 0))))
482 (delta
483 ;; Calculate how much the window height has to change to show
484 ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
485 (- (max (min desired-height max-height)
486 (or min-height window-min-height))
487 window-height))
488 ;; We do our own height checking, so avoid any restrictions due to
489 ;; window-min-height.
490 (window-min-height 1))
492 ;; Don't try to redisplay with the cursor at the end
493 ;; on its own line--that would force a scroll and spoil things.
494 (when (with-current-buffer buf
495 (and (eobp) (bolp) (not (bobp))))
496 (set-window-point window (1- (window-point window))))
498 (save-selected-window
499 (select-window window)
501 ;; Adjust WINDOW to the nominally correct size (which may actually
502 ;; be slightly off because of variable height text, etc).
503 (unless (zerop delta)
504 (enlarge-window delta))
506 ;; Check if the last line is surely fully visible. If not,
507 ;; enlarge the window.
508 (let ((end (with-current-buffer buf
509 (save-excursion
510 (goto-char (point-max))
511 (if (and (bolp) (not (bobp)))
512 (1- (point))
513 (point))))))
514 (set-window-vscroll window 0)
515 (while (and (< desired-height max-height)
516 (= desired-height (window-height window))
517 (not (pos-visible-in-window-p end window t)))
518 (enlarge-window 1)
519 (setq desired-height (1+ desired-height)))))))
521 (defun shrink-window-if-larger-than-buffer (&optional window)
522 "Shrink the WINDOW to be as small as possible to display its contents.
523 Do not shrink to less than `window-min-height' lines.
524 Do nothing if the buffer contains more lines than the present window height,
525 or if some of the window's contents are scrolled out of view,
526 or if shrinking this window would also shrink another window.
527 or if the window is the only window of its frame.
528 Return non-nil if the window was shrunk."
529 (interactive)
530 (when (null window)
531 (setq window (selected-window)))
532 (let* ((frame (window-frame window))
533 (mini (frame-parameter frame 'minibuffer))
534 (edges (window-edges window)))
535 (if (and (not (eq window (frame-root-window frame)))
536 (window-safely-shrinkable-p)
537 (pos-visible-in-window-p (point-min) window)
538 (not (eq mini 'only))
539 (or (not mini)
540 (< (nth 3 edges) (nth 1 (window-edges mini)))
541 (> (nth 1 edges) (frame-parameter frame 'menu-bar-lines))))
542 (fit-window-to-buffer window (window-height window)))))
544 (defun kill-buffer-and-window ()
545 "Kill the current buffer and delete the selected window."
546 (interactive)
547 (if (yes-or-no-p (format "Kill buffer `%s'? " (buffer-name)))
548 (let ((buffer (current-buffer)))
549 (delete-window (selected-window))
550 (kill-buffer buffer))
551 (error "Aborted")))
553 (defun quit-window (&optional kill window)
554 "Quit the current buffer. Bury it, and maybe delete the selected frame.
555 \(The frame is deleted if it is contains a dedicated window for the buffer.)
556 With a prefix argument, kill the buffer instead.
558 Noninteractively, if KILL is non-nil, then kill the current buffer,
559 otherwise bury it.
561 If WINDOW is non-nil, it specifies a window; we delete that window,
562 and the buffer that is killed or buried is the one in that window."
563 (interactive "P")
564 (let ((buffer (window-buffer window))
565 (frame (window-frame (or window (selected-window))))
566 (window-solitary
567 (save-selected-window
568 (if window
569 (select-window window))
570 (one-window-p t)))
571 window-handled)
573 (save-selected-window
574 (if window
575 (select-window window))
576 (or (window-minibuffer-p)
577 (window-dedicated-p (selected-window))
578 (switch-to-buffer (other-buffer))))
580 ;; Get rid of the frame, if it has just one dedicated window
581 ;; and other visible frames exist.
582 (and (or (window-minibuffer-p) (window-dedicated-p window))
583 (delq frame (visible-frame-list))
584 window-solitary
585 (if (and (eq default-minibuffer-frame frame)
586 (= 1 (length (minibuffer-frame-list))))
587 (setq window nil)
588 (delete-frame frame)
589 (setq window-handled t)))
591 ;; Deal with the buffer.
592 (if kill
593 (kill-buffer buffer)
594 (bury-buffer buffer))
596 ;; Maybe get rid of the window.
597 (and window (not window-handled) (not window-solitary)
598 (delete-window window))))
600 (define-key ctl-x-map "2" 'split-window-vertically)
601 (define-key ctl-x-map "3" 'split-window-horizontally)
602 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
603 (define-key ctl-x-map "{" 'shrink-window-horizontally)
604 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
605 (define-key ctl-x-map "+" 'balance-windows)
606 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
608 ;;; window.el ends here