(defcustom): Get rid of eval-and-compile.
[emacs.git] / lisp / window.el
blob5be9f870b9df8c720398ade20db98fe1767efe36
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 Anything else means restrict to the selected frame."
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))))))
77 (defun minibuffer-window-active-p (window)
78 "Return t if WINDOW (a minibuffer window) is now active."
79 (eq window (active-minibuffer-window)))
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))))
89 (defun count-windows (&optional minibuf)
90 "Returns the number of visible windows.
91 Optional arg MINIBUF non-nil means count the minibuffer
92 even if it is inactive."
93 (let ((count 0))
94 (walk-windows (function (lambda (w)
95 (setq count (+ count 1))))
96 minibuf)
97 count))
99 (defun balance-windows ()
100 "Makes all visible windows the same height (approximately)."
101 (interactive)
102 (let ((count -1) levels newsizes size
103 ;; Don't count the lines that are above the uppermost windows.
104 ;; (These are the menu bar lines, if any.)
105 (mbl (nth 1 (window-edges (frame-first-window (selected-frame))))))
106 ;; Find all the different vpos's at which windows start,
107 ;; then count them. But ignore levels that differ by only 1.
108 (save-window-excursion
109 (let (tops (prev-top -2))
110 (walk-windows (function (lambda (w)
111 (setq tops (cons (nth 1 (window-edges w))
112 tops))))
113 'nomini)
114 (setq tops (sort tops '<))
115 (while tops
116 (if (> (car tops) (1+ prev-top))
117 (setq prev-top (car tops)
118 count (1+ count)))
119 (setq levels (cons (cons (car tops) count) levels))
120 (setq tops (cdr tops)))
121 (setq count (1+ count))))
122 ;; Subdivide the frame into that many vertical levels.
123 (setq size (/ (- (frame-height) mbl) count))
124 (walk-windows (function
125 (lambda (w)
126 (select-window w)
127 (let ((newtop (cdr (assq (nth 1 (window-edges))
128 levels)))
129 (newbot (or (cdr (assq (+ (window-height)
130 (nth 1 (window-edges)))
131 levels))
132 count)))
133 (setq newsizes
134 (cons (cons w (* size (- newbot newtop)))
135 newsizes)))))
136 'nomini)
137 (walk-windows (function (lambda (w)
138 (select-window w)
139 (let ((newsize (cdr (assq w newsizes))))
140 (enlarge-window (- newsize
141 (window-height))))))
142 'nomini)))
144 ;;; I think this should be the default; I think people will prefer it--rms.
145 (defcustom split-window-keep-point t
146 "*If non-nil, split windows keeps the original point in both children.
147 This is often more convenient for editing.
148 If nil, adjust point in each of the two windows to minimize redisplay.
149 This is convenient on slow terminals, but point can move strangely."
150 :type 'boolean
151 :group 'windows)
153 (defun split-window-vertically (&optional arg)
154 "Split current window into two windows, one above the other.
155 The uppermost window gets ARG lines and the other gets the rest.
156 Negative arg means select the size of the lowermost window instead.
157 With no argument, split equally or close to it.
158 Both windows display the same buffer now current.
160 If the variable split-window-keep-point is non-nil, both new windows
161 will get the same value of point as the current window. This is often
162 more convenient for editing.
164 Otherwise, we chose window starts so as to minimize the amount of
165 redisplay; this is convenient on slow terminals. The new selected
166 window is the one that the current value of point appears in. The
167 value of point can change if the text around point is hidden by the
168 new mode line."
169 (interactive "P")
170 (let ((old-w (selected-window))
171 (old-point (point))
172 (size (and arg (prefix-numeric-value arg)))
173 (window-full-p nil)
174 new-w bottom switch moved)
175 (and size (< size 0) (setq size (+ (window-height) size)))
176 (setq new-w (split-window nil size))
177 (or split-window-keep-point
178 (progn
179 (save-excursion
180 (set-buffer (window-buffer))
181 (goto-char (window-start))
182 (setq moved (vertical-motion (window-height)))
183 (set-window-start new-w (point))
184 (if (> (point) (window-point new-w))
185 (set-window-point new-w (point)))
186 (and (= moved (window-height))
187 (progn
188 (setq window-full-p t)
189 (vertical-motion -1)))
190 (setq bottom (point)))
191 (and window-full-p
192 (<= bottom (point))
193 (set-window-point old-w (1- bottom)))
194 (and window-full-p
195 (<= (window-start new-w) old-point)
196 (progn
197 (set-window-point new-w old-point)
198 (select-window new-w)))))
199 new-w))
201 (defun split-window-horizontally (&optional arg)
202 "Split current window into two windows side by side.
203 This window becomes the leftmost of the two, and gets ARG columns.
204 Negative arg means select the size of the rightmost window instead.
205 No arg means split equally."
206 (interactive "P")
207 (let ((size (and arg (prefix-numeric-value arg))))
208 (and size (< size 0)
209 (setq size (+ (window-width) size)))
210 (split-window nil size t)))
212 (defun enlarge-window-horizontally (arg)
213 "Make current window ARG columns wider."
214 (interactive "p")
215 (enlarge-window arg t))
217 (defun shrink-window-horizontally (arg)
218 "Make current window ARG columns narrower."
219 (interactive "p")
220 (shrink-window arg t))
222 (defun shrink-window-if-larger-than-buffer (&optional window)
223 "Shrink the WINDOW to be as small as possible to display its contents.
224 Do not shrink to less than `window-min-height' lines.
225 Do nothing if the buffer contains more lines than the present window height,
226 or if some of the window's contents are scrolled out of view,
227 or if the window is not the full width of the frame,
228 or if the window is the only window of its frame."
229 (interactive)
230 (or window (setq window (selected-window)))
231 (save-excursion
232 (set-buffer (window-buffer window))
233 (let* ((w (selected-window)) ;save-window-excursion can't win
234 (buffer-file-name buffer-file-name)
235 (p (point))
236 (n 0)
237 (ignore-final-newline
238 ;; If buffer ends with a newline, ignore it when counting height
239 ;; unless point is after it.
240 (and (not (eobp))
241 (eq ?\n (char-after (1- (point-max))))))
242 (buffer-read-only nil)
243 (modified (buffer-modified-p))
244 (buffer (current-buffer))
245 (params (frame-parameters (window-frame window)))
246 (mini (cdr (assq 'minibuffer params)))
247 (edges (window-edges (selected-window))))
248 (if (and (< 1 (let ((frame (selected-frame)))
249 (select-frame (window-frame window))
250 (unwind-protect
251 (count-windows)
252 (select-frame frame))))
253 (= (window-width window) (frame-width (window-frame window)))
254 (pos-visible-in-window-p (point-min) window)
255 (not (eq mini 'only))
256 (or (not mini)
257 (< (nth 3 edges)
258 (nth 1 (window-edges mini)))
259 (> (nth 1 edges)
260 (cdr (assq 'menu-bar-lines params)))))
261 (unwind-protect
262 (progn
263 (select-window (or window w))
264 (goto-char (point-min))
265 (while (pos-visible-in-window-p
266 (- (point-max)
267 (if ignore-final-newline 1 0)))
268 ;; defeat file locking... don't try this at home, kids!
269 (setq buffer-file-name nil)
270 (insert ?\n) (setq n (1+ n)))
271 (if (> n 0)
272 (shrink-window (min (1- n)
273 (- (window-height)
274 window-min-height)))))
275 (delete-region (point-min) (point))
276 (set-buffer-modified-p modified)
277 (goto-char p)
278 (select-window w)
279 ;; Make sure we unbind buffer-read-only
280 ;; with the proper current buffer.
281 (set-buffer buffer))))))
283 (defun kill-buffer-and-window ()
284 "Kill the current buffer and delete the selected window."
285 (interactive)
286 (if (yes-or-no-p (format "Kill buffer `%s'? " (buffer-name)))
287 (let ((buffer (current-buffer)))
288 (delete-window (selected-window))
289 (kill-buffer buffer))
290 (error "Aborted")))
292 (define-key ctl-x-map "2" 'split-window-vertically)
293 (define-key ctl-x-map "3" 'split-window-horizontally)
294 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
295 (define-key ctl-x-map "{" 'shrink-window-horizontally)
296 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
297 (define-key ctl-x-map "+" 'balance-windows)
298 (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
300 ;;; windows.el ends here