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