Some fixes to follow coding conventions in files maintained by FSF.
[bpt/emacs.git] / 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, 2001
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 ;;; Commentary:
26
27 ;; Window tree functions.
28
29 ;;; Code:
30
31 (defun one-window-p (&optional nomini all-frames)
32 "Return non-nil if the selected window is the only window (in its frame).
33 Optional arg NOMINI non-nil means don't count the minibuffer
34 even if it is active.
35
36 The optional arg ALL-FRAMES t means count windows on all frames.
37 If it is `visible', count windows on all visible frames.
38 ALL-FRAMES nil or omitted means count only the selected frame,
39 plus the minibuffer it uses (which may be on another frame).
40 If ALL-FRAMES is neither nil nor t, count only the selected frame."
41 (let ((base-window (selected-window)))
42 (if (and nomini (eq base-window (minibuffer-window)))
43 (setq base-window (next-window base-window)))
44 (eq base-window
45 (next-window base-window (if nomini 'arg) all-frames))))
46
47 (defun walk-windows (proc &optional minibuf all-frames)
48 "Cycle through all visible windows, calling PROC for each one.
49 PROC is called with a window as argument.
50
51 Optional second arg MINIBUF t means count the minibuffer window even
52 if not active. MINIBUF nil or omitted means count the minibuffer iff
53 it is active. MINIBUF neither t nor nil means not to count the
54 minibuffer even if it is active.
55
56 Several frames may share a single minibuffer; if the minibuffer
57 counts, all windows on all frames that share that minibuffer count
58 too. Therefore, if you are using a separate minibuffer frame
59 and the minibuffer is active and MINIBUF says it counts,
60 `walk-windows' includes the windows in the frame from which you
61 entered the minibuffer, as well as the minibuffer window.
62
63 ALL-FRAMES is the optional third argument.
64 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
65 ALL-FRAMES = `visible' means include windows on all visible frames.
66 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
67 ALL-FRAMES = t means include windows on all frames including invisible frames.
68 If ALL-FRAMES is a frame, it means include windows on that frame.
69 Anything else means restrict to the selected frame."
70 ;; If we start from the minibuffer window, don't fail to come back to it.
71 (if (window-minibuffer-p (selected-window))
72 (setq minibuf t))
73 (save-selected-window
74 (if (framep all-frames)
75 (select-window (frame-first-window all-frames)))
76 (let* (walk-windows-already-seen
77 (walk-windows-current (selected-window)))
78 (while (progn
79 (setq walk-windows-current
80 (next-window walk-windows-current minibuf all-frames))
81 (not (memq walk-windows-current walk-windows-already-seen)))
82 (setq walk-windows-already-seen
83 (cons walk-windows-current walk-windows-already-seen))
84 (funcall proc walk-windows-current)))))
85
86 (defun some-window (predicate &optional minibuf all-frames default)
87 "Return a window satisfying PREDICATE.
88
89 This function cycles through all visible windows using `walk-windows',
90 calling PREDICATE on each one. PREDICATE is called with a window as
91 argument. The first window for which PREDICATE returns a non-nil
92 value is returned. If no window satisfies PREDICATE, DEFAULT is
93 returned.
94
95 Optional second arg MINIBUF t means count the minibuffer window even
96 if not active. MINIBUF nil or omitted means count the minibuffer iff
97 it is active. MINIBUF neither t nor nil means not to count the
98 minibuffer even if it is active.
99
100 Several frames may share a single minibuffer; if the minibuffer
101 counts, all windows on all frames that share that minibuffer count
102 too. Therefore, if you are using a separate minibuffer frame
103 and the minibuffer is active and MINIBUF says it counts,
104 `walk-windows' includes the windows in the frame from which you
105 entered the minibuffer, as well as the minibuffer window.
106
107 ALL-FRAMES is the optional third argument.
108 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
109 ALL-FRAMES = `visible' means include windows on all visible frames.
110 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
111 ALL-FRAMES = t means include windows on all frames including invisible frames.
112 If ALL-FRAMES is a frame, it means include windows on that frame.
113 Anything else means restrict to the selected frame."
114 (catch 'found
115 (walk-windows #'(lambda (window)
116 (when (funcall predicate window)
117 (throw 'found window)))
118 minibuf all-frames)
119 default))
120
121 (defun minibuffer-window-active-p (window)
122 "Return t if WINDOW (a minibuffer window) is now active."
123 (eq window (active-minibuffer-window)))
124
125 (defmacro save-selected-window (&rest body)
126 "Execute BODY, then select the window that was selected before BODY."
127 `(let ((save-selected-window-window (selected-window)))
128 (unwind-protect
129 (progn ,@body)
130 (select-window save-selected-window-window))))
131 \f
132 (defun count-windows (&optional minibuf)
133 "Return the number of visible windows.
134 This counts the windows in the selected frame and (if the minibuffer is
135 to be counted) its minibuffer frame (if that's not the same frame).
136 The optional arg MINIBUF non-nil means count the minibuffer
137 even if it is inactive."
138 (let ((count 0))
139 (walk-windows (function (lambda (w)
140 (setq count (+ count 1))))
141 minibuf)
142 count))
143
144 (defun window-safely-shrinkable-p (&optional window)
145 "Non-nil if the WINDOW can be shrunk without shrinking other windows.
146 If WINDOW is nil or omitted, it defaults to the currently selected window."
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)))))))
154
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)))
199 \f
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)
208
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.
215
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.
219
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)))
256
257 ;; This is to avoid compiler warnings.
258 (defvar view-return-to-alist)
259
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))
269
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)))
283
284 \f
285 (defun set-window-text-height (window height)
286 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
287 This doesn't include the mode-line (or header-line if any) or any
288 partial-height lines in the text display area.
289
290 If WINDOW is nil, the selected window is used.
291
292 Note that the current implementation of this function cannot always set
293 the height exactly, but attempts to be conservative, by allocating more
294 lines than are actually needed in the case where some error may be present."
295 (let ((delta (- height (window-text-height window))))
296 (unless (zerop delta)
297 (let ((window-min-height 1))
298 (if (and window (not (eq window (selected-window))))
299 (save-selected-window
300 (select-window window)
301 (enlarge-window delta))
302 (enlarge-window delta))))))
303
304 \f
305 (defun enlarge-window-horizontally (arg)
306 "Make current window ARG columns wider."
307 (interactive "p")
308 (enlarge-window arg t))
309
310 (defun shrink-window-horizontally (arg)
311 "Make current window ARG columns narrower."
312 (interactive "p")
313 (shrink-window arg t))
314
315 (defun window-buffer-height (window)
316 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
317 (save-excursion
318 (set-buffer (window-buffer window))
319 (goto-char (point-min))
320 (let ((ignore-final-newline
321 ;; If buffer ends with a newline, ignore it when counting height
322 ;; unless point is after it.
323 (and (not (eobp)) (eq ?\n (char-after (1- (point-max)))))))
324 (+ 1 (nth 2 (compute-motion (point-min)
325 '(0 . 0)
326 (- (point-max) (if ignore-final-newline 1 0))
327 (cons 0 100000000)
328 (window-width window)
329 nil
330 window))))))
331
332 (defun count-screen-lines (&optional beg end count-final-newline window)
333 "Return the number of screen lines in the region.
334 The number of screen lines may be different from the number of actual lines,
335 due to line breaking, display table, etc.
336
337 Optional arguments BEG and END default to `point-min' and `point-max'
338 respectively.
339
340 If region ends with a newline, ignore it unless optional third argument
341 COUNT-FINAL-NEWLINE is non-nil.
342
343 The optional fourth argument WINDOW specifies the window used for obtaining
344 parameters such as width, horizontal scrolling, and so on. The default is
345 to use the selected window's parameters.
346
347 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
348 regardless of which buffer is displayed in WINDOW. This makes possible to use
349 `count-screen-lines' in any buffer, whether or not it is currently displayed
350 in some window."
351 (unless beg
352 (setq beg (point-min)))
353 (unless end
354 (setq end (point-max)))
355 (if (= beg end)
356 0
357 (save-excursion
358 (save-restriction
359 (widen)
360 (narrow-to-region (min beg end)
361 (if (and (not count-final-newline)
362 (= ?\n (char-before (max beg end))))
363 (1- (max beg end))
364 (max beg end)))
365 (goto-char (point-min))
366 (1+ (vertical-motion (buffer-size) window))))))
367
368 (defun fit-window-to-buffer (&optional window max-height min-height)
369 "Make WINDOW the right size to display its contents exactly.
370 If WINDOW is omitted or nil, it defaults to the selected window.
371 If the optional argument MAX-HEIGHT is supplied, it is the maximum height
372 the window is allowed to be, defaulting to the frame height.
373 If the optional argument MIN-HEIGHT is supplied, it is the minimum
374 height the window is allowed to be, defaulting to `window-min-height'.
375
376 The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or
377 header-line."
378 (interactive)
379
380 (when (null window)
381 (setq window (selected-window)))
382 (when (null max-height)
383 (setq max-height (frame-height (window-frame window))))
384
385 (let* ((buf
386 ;; Buffer that is displayed in WINDOW
387 (window-buffer window))
388 (window-height
389 ;; The current height of WINDOW
390 (window-height window))
391 (desired-height
392 ;; The height necessary to show the buffer displayed by WINDOW
393 ;; (`count-screen-lines' always works on the current buffer).
394 (with-current-buffer buf
395 (+ (count-screen-lines)
396 ;; If the buffer is empty, (count-screen-lines) is
397 ;; zero. But, even in that case, we need one text line
398 ;; for cursor.
399 (if (= (point-min) (point-max))
400 1 0)
401 ;; For non-minibuffers, count the mode-line, if any
402 (if (and (not (window-minibuffer-p window))
403 mode-line-format)
404 1 0)
405 ;; Count the header-line, if any
406 (if header-line-format 1 0))))
407 (delta
408 ;; Calculate how much the window height has to change to show
409 ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
410 (- (max (min desired-height max-height)
411 (or min-height window-min-height))
412 window-height))
413 ;; We do our own height checking, so avoid any restrictions due to
414 ;; window-min-height.
415 (window-min-height 1))
416
417 ;; Don't try to redisplay with the cursor at the end
418 ;; on its own line--that would force a scroll and spoil things.
419 (when (with-current-buffer buf
420 (and (eobp) (bolp) (not (bobp))))
421 (set-window-point window (1- (window-point window))))
422
423 (save-selected-window
424 (select-window window)
425
426 ;; Adjust WINDOW to the nominally correct size (which may actually
427 ;; be slightly off because of variable height text, etc).
428 (unless (zerop delta)
429 (enlarge-window delta))
430
431 ;; Check if the last line is surely fully visible. If not,
432 ;; enlarge the window.
433 (let ((end (with-current-buffer buf
434 (save-excursion
435 (goto-char (point-max))
436 (when (and (bolp) (not (bobp)))
437 ;; Don't include final newline
438 (backward-char 1))
439 (when truncate-lines
440 ;; If line-wrapping is turned off, test the
441 ;; beginning of the last line for visibility
442 ;; instead of the end, as the end of the line
443 ;; could be invisible by virtue of extending past
444 ;; the edge of the window.
445 (forward-line 0))
446 (point)))))
447 (set-window-vscroll window 0)
448 (while (and (< desired-height max-height)
449 (= desired-height (window-height window))
450 (not (pos-visible-in-window-p end window)))
451 (enlarge-window 1)
452 (setq desired-height (1+ desired-height)))))))
453
454 (defun shrink-window-if-larger-than-buffer (&optional window)
455 "Shrink the WINDOW to be as small as possible to display its contents.
456 If WINDOW is omitted or nil, it defaults to the selected window.
457 Do not shrink to less than `window-min-height' lines.
458 Do nothing if the buffer contains more lines than the present window height,
459 or if some of the window's contents are scrolled out of view,
460 or if shrinking this window would also shrink another window.
461 or if the window is the only window of its frame.
462 Return non-nil if the window was shrunk."
463 (interactive)
464 (when (null window)
465 (setq window (selected-window)))
466 (let* ((frame (window-frame window))
467 (mini (frame-parameter frame 'minibuffer))
468 (edges (window-edges window)))
469 (if (and (not (eq window (frame-root-window frame)))
470 (window-safely-shrinkable-p)
471 (pos-visible-in-window-p (point-min) window)
472 (not (eq mini 'only))
473 (or (not mini)
474 (let ((mini-window (minibuffer-window frame)))
475 (or (null mini-window)
476 (not (eq frame (window-frame mini-window)))
477 (< (nth 3 edges)
478 (nth 1 (window-edges mini-window)))
479 (> (nth 1 edges)
480 (frame-parameter frame 'menu-bar-lines))))))
481 (fit-window-to-buffer window (window-height window)))))
482
483 (defun kill-buffer-and-window ()
484 "Kill the current buffer and delete the selected window."
485 (interactive)
486 (if (yes-or-no-p (format "Kill buffer `%s'? " (buffer-name)))
487 (let ((buffer (current-buffer)))
488 (delete-window (selected-window))
489 (kill-buffer buffer))
490 (error "Aborted")))
491
492 (defun quit-window (&optional kill window)
493 "Quit the current buffer. Bury it, and maybe delete the selected frame.
494 \(The frame is deleted if it is contains a dedicated window for the buffer.)
495 With a prefix argument, kill the buffer instead.
496
497 Noninteractively, if KILL is non-nil, then kill the current buffer,
498 otherwise bury it.
499
500 If WINDOW is non-nil, it specifies a window; we delete that window,
501 and the buffer that is killed or buried is the one in that window."
502 (interactive "P")
503 (let ((buffer (window-buffer window))
504 (frame (window-frame (or window (selected-window))))
505 (window-solitary
506 (save-selected-window
507 (if window
508 (select-window window))
509 (one-window-p t)))
510 window-handled)
511
512 (save-selected-window
513 (if window
514 (select-window window))
515 (or (window-minibuffer-p)
516 (window-dedicated-p (selected-window))
517 (switch-to-buffer (other-buffer))))
518
519 ;; Get rid of the frame, if it has just one dedicated window
520 ;; and other visible frames exist.
521 (and (or (window-minibuffer-p) (window-dedicated-p window))
522 (delq frame (visible-frame-list))
523 window-solitary
524 (if (and (eq default-minibuffer-frame frame)
525 (= 1 (length (minibuffer-frame-list))))
526 (setq window nil)
527 (delete-frame frame)
528 (setq window-handled t)))
529
530 ;; Deal with the buffer.
531 (if kill
532 (kill-buffer buffer)
533 (bury-buffer buffer))
534
535 ;; Maybe get rid of the window.
536 (and window (not window-handled) (not window-solitary)
537 (delete-window window))))
538
539 (define-key ctl-x-map "2" 'split-window-vertically)
540 (define-key ctl-x-map "3" 'split-window-horizontally)
541 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
542 (define-key ctl-x-map "{" 'shrink-window-horizontally)
543 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
544 (define-key ctl-x-map "+" 'balance-windows)
545 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
546
547 ;;; window.el ends here