(auto-insert-mode): Use define-minor-mode.
[emacs.git] / lisp / window.el
blob31735c093f5d7782407cae920de509d448cb2140
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 (defun enlarge-window-horizontally (arg)
273 "Make current window ARG columns wider."
274 (interactive "p")
275 (enlarge-window arg t))
277 (defun shrink-window-horizontally (arg)
278 "Make current window ARG columns narrower."
279 (interactive "p")
280 (shrink-window arg t))
282 (defun window-buffer-height (window)
283 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
284 (save-excursion
285 (set-buffer (window-buffer window))
286 (goto-char (point-min))
287 (let ((ignore-final-newline
288 ;; If buffer ends with a newline, ignore it when counting height
289 ;; unless point is after it.
290 (and (not (eobp)) (eq ?\n (char-after (1- (point-max)))))))
291 (+ 1 (nth 2 (compute-motion (point-min)
292 '(0 . 0)
293 (- (point-max) (if ignore-final-newline 1 0))
294 (cons 0 100000000)
295 (window-width window)
297 window))))))
299 (defun count-screen-lines (&optional beg end count-final-newline window)
300 "Return the number of screen lines in the region.
301 The number of screen lines may be different from the number of actual lines,
302 due to line breaking, display table, etc.
304 Optional arguments BEG and END default to `point-min' and `point-max'
305 respectively.
307 If region ends with a newline, ignore it unless optinal third argument
308 COUNT-FINAL-NEWLINE is non-nil.
310 The optional fourth argument WINDOW specifies the window used for obtaining
311 parameters such as width, horizontal scrolling, and so on. The default is
312 to use the selected window's parameters.
314 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
315 regardless of which buffer is displayed in WINDOW. This makes possible to use
316 `count-screen-lines' in any buffer, whether or not it is currently displayed
317 in some window."
318 (unless beg
319 (setq beg (point-min)))
320 (unless end
321 (setq end (point-max)))
322 (if (= beg end)
324 (save-excursion
325 (save-restriction
326 (widen)
327 (narrow-to-region (min beg end)
328 (if (and (not count-final-newline)
329 (= ?\n (char-before (max beg end))))
330 (1- (max beg end))
331 (max beg end)))
332 (goto-char (point-min))
333 (1+ (vertical-motion (buffer-size) window))))))
335 (defun shrink-window-if-larger-than-buffer (&optional window)
336 "Shrink the WINDOW to be as small as possible to display its contents.
337 Do not shrink to less than `window-min-height' lines.
338 Do nothing if the buffer contains more lines than the present window height,
339 or if some of the window's contents are scrolled out of view,
340 or if the window is not the full width of the frame,
341 or if the window is the only window of its frame."
342 (interactive)
343 (save-selected-window
344 (if window
345 (select-window window)
346 (setq window (selected-window)))
347 (let* ((mini (frame-parameter nil 'minibuffer))
348 (edges (window-edges)))
349 (if (and (< 1 (count-windows))
350 (= (window-width) (frame-width))
351 (pos-visible-in-window-p (point-min) window)
352 (not (eq mini 'only))
353 (or (not mini)
354 (< (nth 3 edges) (nth 1 (window-edges mini)))
355 (> (nth 1 edges) (frame-parameter nil 'menu-bar-lines))))
356 ;; `count-screen-lines' always works on the current buffer, so
357 ;; make sure it is the buffer displayed by WINDOW.
358 (let ((text-height (with-current-buffer (window-buffer window)
359 (count-screen-lines)))
360 (window-height (window-height)))
361 ;; This is a workaround that adds 1 line to the window
362 ;; if windows have a 3D mode-line. What's really needed
363 ;; is to get rid of the line-based computations. We don't
364 ;; have the infrastructure for doing so, yet.
365 (when (and (display-graphic-p)
366 (face-attribute 'mode-line :box))
367 (setq text-height (1+ text-height)))
368 ;; Don't try to redisplay with the cursor at the end
369 ;; on its own line--that would force a scroll and spoil things.
370 (when (and (eobp) (bolp) (not (bobp)))
371 (forward-char -1))
372 (when (> window-height (1+ text-height))
373 (shrink-window
374 (- window-height (max (1+ text-height) window-min-height)))))))))
376 (defun kill-buffer-and-window ()
377 "Kill the current buffer and delete the selected window."
378 (interactive)
379 (if (yes-or-no-p (format "Kill buffer `%s'? " (buffer-name)))
380 (let ((buffer (current-buffer)))
381 (delete-window (selected-window))
382 (kill-buffer buffer))
383 (error "Aborted")))
385 (defun quit-window (&optional kill window)
386 "Quit the current buffer. Bury it, and maybe delete the selected frame.
387 \(The frame is deleted if it is contains a dedicated window for the buffer.)
388 With a prefix argument, kill the buffer instead.
390 Noninteractively, if KILL is non-nil, then kill the current buffer,
391 otherwise bury it.
393 If WINDOW is non-nil, it specifies a window; we delete that window,
394 and the buffer that is killed or buried is the one in that window."
395 (interactive "P")
396 (let ((buffer (window-buffer window))
397 (frame (window-frame (or window (selected-window))))
398 (window-solitary
399 (save-selected-window
400 (if window
401 (select-window window))
402 (one-window-p t)))
403 window-handled)
405 (save-selected-window
406 (if window
407 (select-window window))
408 (or (window-minibuffer-p)
409 (window-dedicated-p (selected-window))
410 (switch-to-buffer (other-buffer))))
412 ;; Get rid of the frame, if it has just one dedicated window
413 ;; and other visible frames exist.
414 (and (or (window-minibuffer-p) (window-dedicated-p window))
415 (delq frame (visible-frame-list))
416 window-solitary
417 (if (and (eq default-minibuffer-frame frame)
418 (= 1 (length (minibuffer-frame-list))))
419 (setq window nil)
420 (delete-frame frame)
421 (setq window-handled t)))
423 ;; Deal with the buffer.
424 (if kill
425 (kill-buffer buffer)
426 (bury-buffer buffer))
428 ;; Maybe get rid of the window.
429 (and window (not window-handled) (not window-solitary)
430 (delete-window window))))
432 (define-key ctl-x-map "2" 'split-window-vertically)
433 (define-key ctl-x-map "3" 'split-window-horizontally)
434 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
435 (define-key ctl-x-map "{" 'shrink-window-horizontally)
436 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
437 (define-key ctl-x-map "+" 'balance-windows)
438 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
440 ;;; windows.el ends here