Doc fixes.
[emacs.git] / lisp / window.el
blobe5639a5082744150227784fd10b4ec293a0474ec
1 ;;; window.el --- GNU Emacs window commands aside from those written in C.
3 ;; Copyright (C) 1985, 1989, 1992, 1993, 1994 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 ;;; Code:
26 ;;;; Window tree functions.
28 (defun one-window-p (&optional nomini all-frames)
29 "Returns non-nil if the selected window is the only window (in its frame).
30 Optional arg NOMINI non-nil means don't count the minibuffer
31 even if it is active.
33 The optional arg ALL-FRAMES t means count windows on all frames.
34 If it is `visible', count windows on all visible frames.
35 ALL-FRAMES nil or omitted means count only the selected frame,
36 plus the minibuffer it uses (which may be on another frame).
37 If ALL-FRAMES is neither nil nor t, count only the selected frame."
38 (let ((base-window (selected-window)))
39 (if (and nomini (eq base-window (minibuffer-window)))
40 (setq base-window (next-window base-window)))
41 (eq base-window
42 (next-window base-window (if nomini 'arg) all-frames))))
44 (defun walk-windows (proc &optional minibuf all-frames)
45 "Cycle through all visible windows, calling PROC for each one.
46 PROC is called with a window as argument.
48 Optional second arg MINIBUF t means count the minibuffer window even
49 if not active. MINIBUF nil or omitted means count the minibuffer iff
50 it is active. MINIBUF neither t nor nil means not to count the
51 minibuffer even if it is active.
53 Several frames may share a single minibuffer; if the minibuffer
54 counts, all windows on all frames that share that minibuffer count
55 too. Therefore, if you are using a separate minibuffer frame
56 and the minibuffer is active and MINIBUF says it counts,
57 `walk-windows' includes the windows in the frame from which you
58 entered the minibuffer, as well as the minibuffer window.
60 ALL-FRAMES is the optional third argument.
61 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
62 ALL-FRAMES = `visible' means include windows on all visible frames.
63 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
64 ALL-FRAMES = t means include windows on all frames including invisible frames.
65 If ALL-FRAMES is a frame, it means include windows on that frame.
66 Anything else means restrict to the selected frame."
67 ;; If we start from the minibuffer window, don't fail to come back to it.
68 (if (window-minibuffer-p (selected-window))
69 (setq minibuf t))
70 (save-selected-window
71 (if (framep all-frames)
72 (select-window (frame-first-window all-frames)))
73 (let* ((walk-windows-start (selected-window))
74 (walk-windows-current walk-windows-start))
75 (while (progn
76 (setq walk-windows-current
77 (next-window walk-windows-current minibuf all-frames))
78 (funcall proc walk-windows-current)
79 (not (eq walk-windows-current walk-windows-start)))))))
81 (defun minibuffer-window-active-p (window)
82 "Return t if WINDOW (a minibuffer window) is now active."
83 (eq window (active-minibuffer-window)))
85 (defmacro save-selected-window (&rest body)
86 "Execute BODY, then select the window that was selected before BODY."
87 (list 'let
88 '((save-selected-window-window (selected-window)))
89 (list 'unwind-protect
90 (cons 'progn body)
91 (list 'select-window 'save-selected-window-window))))
93 (defun count-windows (&optional minibuf)
94 "Returns the number of visible windows.
95 This counts the windows in the selected frame and (if the minibuffer is
96 to be counted) its minibuffer frame (if that's not the same frame).
97 The optional arg MINIBUF non-nil means count the minibuffer
98 even if it is inactive."
99 (let ((count 0))
100 (walk-windows (function (lambda (w)
101 (setq count (+ count 1))))
102 minibuf)
103 count))
105 (defun balance-windows ()
106 "Makes all visible windows the same height (approximately)."
107 (interactive)
108 (let ((count -1) levels newsizes size
109 ;; Don't count the lines that are above the uppermost windows.
110 ;; (These are the menu bar lines, if any.)
111 (mbl (nth 1 (window-edges (frame-first-window (selected-frame))))))
112 ;; Find all the different vpos's at which windows start,
113 ;; then count them. But ignore levels that differ by only 1.
114 (save-window-excursion
115 (let (tops (prev-top -2))
116 (walk-windows (function (lambda (w)
117 (setq tops (cons (nth 1 (window-edges w))
118 tops))))
119 'nomini)
120 (setq tops (sort tops '<))
121 (while tops
122 (if (> (car tops) (1+ prev-top))
123 (setq prev-top (car tops)
124 count (1+ count)))
125 (setq levels (cons (cons (car tops) count) levels))
126 (setq tops (cdr tops)))
127 (setq count (1+ count))))
128 ;; Subdivide the frame into that many vertical levels.
129 (setq size (/ (- (frame-height) mbl) count))
130 (walk-windows (function
131 (lambda (w)
132 (select-window w)
133 (let ((newtop (cdr (assq (nth 1 (window-edges))
134 levels)))
135 (newbot (or (cdr (assq (+ (window-height)
136 (nth 1 (window-edges)))
137 levels))
138 count)))
139 (setq newsizes
140 (cons (cons w (* size (- newbot newtop)))
141 newsizes)))))
142 'nomini)
143 (walk-windows (function (lambda (w)
144 (select-window w)
145 (let ((newsize (cdr (assq w newsizes))))
146 (enlarge-window (- newsize
147 (window-height))))))
148 'nomini)))
150 ;;; I think this should be the default; I think people will prefer it--rms.
151 (defcustom split-window-keep-point t
152 "*If non-nil, split windows keeps the original point in both children.
153 This is often more convenient for editing.
154 If nil, adjust point in each of the two windows to minimize redisplay.
155 This is convenient on slow terminals, but point can move strangely."
156 :type 'boolean
157 :group 'windows)
159 (defun split-window-vertically (&optional arg)
160 "Split current window into two windows, one above the other.
161 The uppermost window gets ARG lines and the other gets the rest.
162 Negative arg means select the size of the lowermost window instead.
163 With no argument, split equally or close to it.
164 Both windows display the same buffer now current.
166 If the variable `split-window-keep-point' is non-nil, both new windows
167 will get the same value of point as the current window. This is often
168 more convenient for editing.
170 Otherwise, we chose window starts so as to minimize the amount of
171 redisplay; this is convenient on slow terminals. The new selected
172 window is the one that the current value of point appears in. The
173 value of point can change if the text around point is hidden by the
174 new mode line."
175 (interactive "P")
176 (let ((old-w (selected-window))
177 (old-point (point))
178 (size (and arg (prefix-numeric-value arg)))
179 (window-full-p nil)
180 new-w bottom switch moved)
181 (and size (< size 0) (setq size (+ (window-height) size)))
182 (setq new-w (split-window nil size))
183 (or split-window-keep-point
184 (progn
185 (save-excursion
186 (set-buffer (window-buffer))
187 (goto-char (window-start))
188 (setq moved (vertical-motion (window-height)))
189 (set-window-start new-w (point))
190 (if (> (point) (window-point new-w))
191 (set-window-point new-w (point)))
192 (and (= moved (window-height))
193 (progn
194 (setq window-full-p t)
195 (vertical-motion -1)))
196 (setq bottom (point)))
197 (and window-full-p
198 (<= bottom (point))
199 (set-window-point old-w (1- bottom)))
200 (and window-full-p
201 (<= (window-start new-w) old-point)
202 (progn
203 (set-window-point new-w old-point)
204 (select-window new-w)))))
205 (split-window-save-restore-data new-w old-w)))
207 ;; This is to avoid compiler warnings.
208 (defvar view-return-to-alist)
210 (defun split-window-save-restore-data (new-w old-w)
211 (save-excursion
212 (set-buffer (window-buffer))
213 (if view-mode
214 (let ((old-info (assq old-w view-return-to-alist)))
215 (setq view-return-to-alist
216 (cons (cons new-w (cons (and old-info (car (cdr old-info))) t))
217 view-return-to-alist))))
218 new-w))
220 (defun split-window-horizontally (&optional arg)
221 "Split current window into two windows side by side.
222 This window becomes the leftmost of the two, and gets ARG columns.
223 Negative arg means select the size of the rightmost window instead.
224 The argument includes the width of the window's scroll bar; if there
225 are no scroll bars, it includes the width of the divider column
226 to the window's right, if any. No arg means split equally."
227 (interactive "P")
228 (let ((old-w (selected-window))
229 (size (and arg (prefix-numeric-value arg))))
230 (and size (< size 0)
231 (setq size (+ (window-width) size)))
232 (split-window-save-restore-data (split-window nil size t) old-w)))
234 (defun enlarge-window-horizontally (arg)
235 "Make current window ARG columns wider."
236 (interactive "p")
237 (enlarge-window arg t))
239 (defun shrink-window-horizontally (arg)
240 "Make current window ARG columns narrower."
241 (interactive "p")
242 (shrink-window arg t))
244 (defun window-buffer-height (window)
245 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
246 (save-excursion
247 (set-buffer (window-buffer window))
248 (goto-char (point-min))
249 (let ((ignore-final-newline
250 ;; If buffer ends with a newline, ignore it when counting height
251 ;; unless point is after it.
252 (and (not (eobp)) (eq ?\n (char-after (1- (point-max)))))))
253 (+ 1 (nth 2 (compute-motion (point-min)
254 '(0 . 0)
255 (- (point-max) (if ignore-final-newline 1 0))
256 (cons 0 100000000)
257 (window-width window)
259 window))))))
261 (defun shrink-window-if-larger-than-buffer (&optional window)
262 "Shrink the WINDOW to be as small as possible to display its contents.
263 Do not shrink to less than `window-min-height' lines.
264 Do nothing if the buffer contains more lines than the present window height,
265 or if some of the window's contents are scrolled out of view,
266 or if the window is not the full width of the frame,
267 or if the window is the only window of its frame."
268 (interactive)
269 (save-selected-window
270 (if window
271 (select-window window)
272 (setq window (selected-window)))
273 (let* ((params (frame-parameters))
274 (mini (cdr (assq 'minibuffer params)))
275 (edges (window-edges)))
276 (if (and (< 1 (count-windows))
277 (= (window-width) (frame-width))
278 (pos-visible-in-window-p (point-min) window)
279 (not (eq mini 'only))
280 (or (not mini)
281 (< (nth 3 edges) (nth 1 (window-edges mini)))
282 (> (nth 1 edges) (cdr (assq 'menu-bar-lines params)))))
283 (let ((text-height (window-buffer-height window))
284 (window-height (window-height)))
285 ;; Don't try to redisplay with the cursor at the end
286 ;; on its own line--that would force a scroll and spoil things.
287 (when (and (eobp) (bolp) (not (bobp)))
288 (forward-char -1))
289 (when (> window-height (1+ text-height))
290 (shrink-window
291 (- window-height (max (1+ text-height) window-min-height)))))))))
293 (defun kill-buffer-and-window ()
294 "Kill the current buffer and delete the selected window."
295 (interactive)
296 (if (yes-or-no-p (format "Kill buffer `%s'? " (buffer-name)))
297 (let ((buffer (current-buffer)))
298 (delete-window (selected-window))
299 (kill-buffer buffer))
300 (error "Aborted")))
302 (defun quit-window (&optional kill window)
303 "Quit the current buffer. Bury it, and maybe delete the selected frame.
304 \(The frame is deleted if it is contains a dedicated window for the buffer.)
305 With a prefix argument, kill the buffer instead.
307 Noninteractively, if KILL is non-nil, then kill the current buffer,
308 otherwise bury it.
310 If WINDOW is non-nil, it specifies a window; we delete that window,
311 and the buffer that is killed or buried is the one in that window."
312 (interactive "P")
313 (let ((buffer (window-buffer window))
314 (frame (window-frame (or window (selected-window))))
315 (window-solitary
316 (save-selected-window
317 (if window
318 (select-window window))
319 (one-window-p t)))
320 window-handled)
322 (save-selected-window
323 (if window
324 (select-window window))
325 (or (window-minibuffer-p)
326 (window-dedicated-p (selected-window))
327 (switch-to-buffer (other-buffer))))
329 ;; Get rid of the frame, if it has just one dedicated window
330 ;; and other visible frames exist.
331 (and (or (window-minibuffer-p) (window-dedicated-p window))
332 (delq frame (visible-frame-list))
333 window-solitary
334 (if (and (eq default-minibuffer-frame frame)
335 (= 1 (length (minibuffer-frame-list))))
336 (setq window nil)
337 (delete-frame frame)
338 (setq window-handled t)))
340 ;; Deal with the buffer.
341 (if kill
342 (kill-buffer buffer)
343 (bury-buffer buffer))
345 ;; Maybe get rid of the window.
346 (and window (not window-handled) (not window-solitary)
347 (delete-window window))))
349 (define-key ctl-x-map "2" 'split-window-vertically)
350 (define-key ctl-x-map "3" 'split-window-horizontally)
351 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
352 (define-key ctl-x-map "{" 'shrink-window-horizontally)
353 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
354 (define-key ctl-x-map "+" 'balance-windows)
355 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
357 ;;; windows.el ends here