(hs-hide-block): Fix message spelling.
[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 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6
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
11 ;; the Free Software Foundation; either version 2, or (at your option)
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
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Code:
24
25 ;;;; Window tree functions.
26
27 (defun one-window-p (&optional nomini all-frames)
28 "Returns non-nil if the selected window is the only window (in its frame).
29 Optional arg NOMINI non-nil means don't count the minibuffer
30 even if it is active.
31
32 The optional arg ALL-FRAMES t means count windows on all frames.
33 If it is `visible', count windows on all visible frames.
34 ALL-FRAMES nil or omitted means count only the selected frame,
35 plus the minibuffer it uses (which may be on another frame).
36 If ALL-FRAMES is neither nil nor t, count only the selected frame."
37 (let ((base-window (selected-window)))
38 (if (and nomini (eq base-window (minibuffer-window)))
39 (setq base-window (next-window base-window)))
40 (eq base-window
41 (next-window base-window (if nomini 'arg) all-frames))))
42
43 (defun walk-windows (proc &optional minibuf all-frames)
44 "Cycle through all visible windows, calling PROC for each one.
45 PROC is called with a window as argument.
46
47 Optional second arg MINIBUF t means count the minibuffer window even
48 if not active. MINIBUF nil or omitted means count the minibuffer iff
49 it is active. MINIBUF neither t nor nil means not to count the
50 minibuffer even if it is active.
51
52 Several frames may share a single minibuffer; if the minibuffer
53 counts, all windows on all frames that share that minibuffer count
54 too. Therefore, when a separate minibuffer frame is active,
55 `walk-windows' includes the windows in the frame from which you
56 entered the minibuffer, as well as the minibuffer window. But if the
57 minibuffer does not count, only windows from WINDOW's frame count.
58
59 ALL-FRAMES is the optional third argument.
60 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
61 ALL-FRAMES = `visible' means include windows on all visible frames.
62 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
63 ALL-FRAMES = t means include windows on all frames including invisible frames.
64 Anything else means restrict to WINDOW's frame."
65 ;; If we start from the minibuffer window, don't fail to come back to it.
66 (if (window-minibuffer-p (selected-window))
67 (setq minibuf t))
68 (let* ((walk-windows-start (selected-window))
69 (walk-windows-current walk-windows-start))
70 (while (progn
71 (setq walk-windows-current
72 (next-window walk-windows-current minibuf all-frames))
73 (funcall proc walk-windows-current)
74 (not (eq walk-windows-current walk-windows-start))))))
75
76 (defun minibuffer-window-active-p (window)
77 "Return t if WINDOW (a minibuffer window) is now active."
78 (eq window (active-minibuffer-window)))
79
80 (defmacro save-selected-window (&rest body)
81 "Execute BODY, then select the window that was selected before BODY."
82 (list 'let
83 '((save-selected-window-window (selected-window)))
84 (list 'unwind-protect
85 (cons 'progn body)
86 (list 'select-window 'save-selected-window-window))))
87 \f
88 (defun count-windows (&optional minibuf)
89 "Returns the number of visible windows.
90 Optional arg NO-MINI non-nil means don't count the minibuffer
91 even if it is active."
92 (let ((count 0))
93 (walk-windows (function (lambda (w)
94 (setq count (+ count 1))))
95 minibuf)
96 count))
97
98 (defun balance-windows ()
99 "Makes all visible windows the same height (approximately)."
100 (interactive)
101 (let ((count -1) levels newsizes size
102 ;; Don't count the lines that are above the uppermost windows.
103 ;; (These are the menu bar lines, if any.)
104 (mbl (nth 1 (window-edges (frame-first-window (selected-frame))))))
105 ;; Find all the different vpos's at which windows start,
106 ;; then count them. But ignore levels that differ by only 1.
107 (save-window-excursion
108 (let (tops (prev-top -2))
109 (walk-windows (function (lambda (w)
110 (setq tops (cons (nth 1 (window-edges w))
111 tops))))
112 'nomini)
113 (setq tops (sort tops '<))
114 (while tops
115 (if (> (car tops) (1+ prev-top))
116 (setq prev-top (car tops)
117 count (1+ count)))
118 (setq levels (cons (cons (car tops) count) levels))
119 (setq tops (cdr tops)))
120 (setq count (1+ count))))
121 ;; Subdivide the frame into that many vertical levels.
122 (setq size (/ (- (frame-height) mbl) count))
123 (walk-windows (function
124 (lambda (w)
125 (select-window w)
126 (let ((newtop (cdr (assq (nth 1 (window-edges))
127 levels)))
128 (newbot (or (cdr (assq (+ (window-height)
129 (nth 1 (window-edges)))
130 levels))
131 count)))
132 (setq newsizes
133 (cons (cons w (* size (- newbot newtop)))
134 newsizes)))))
135 'nomini)
136 (walk-windows (function (lambda (w)
137 (select-window w)
138 (let ((newsize (cdr (assq w newsizes))))
139 (enlarge-window (- newsize
140 (window-height))))))
141 'nomini)))
142 \f
143 ;;; I think this should be the default; I think people will prefer it--rms.
144 (defvar split-window-keep-point t
145 "*If non-nil, split windows keeps the original point in both children.
146 This is often more convenient for editing.
147 If nil, adjust point in each of the two windows to minimize redisplay.
148 This is convenient on slow terminals, but point can move strangely.")
149
150 (defun split-window-vertically (&optional arg)
151 "Split current window into two windows, one above the other.
152 The uppermost window gets ARG lines and the other gets the rest.
153 Negative arg means select the size of the lowermost window instead.
154 With no argument, split equally or close to it.
155 Both windows display the same buffer now current.
156
157 If the variable split-window-keep-point is non-nil, both new windows
158 will get the same value of point as the current window. This is often
159 more convenient for editing.
160
161 Otherwise, we chose window starts so as to minimize the amount of
162 redisplay; this is convenient on slow terminals. The new selected
163 window is the one that the current value of point appears in. The
164 value of point can change if the text around point is hidden by the
165 new mode line."
166 (interactive "P")
167 (let ((old-w (selected-window))
168 (old-point (point))
169 (size (and arg (prefix-numeric-value arg)))
170 (window-full-p nil)
171 new-w bottom switch moved)
172 (and size (< size 0) (setq size (+ (window-height) size)))
173 (setq new-w (split-window nil size))
174 (or split-window-keep-point
175 (progn
176 (save-excursion
177 (set-buffer (window-buffer))
178 (goto-char (window-start))
179 (setq moved (vertical-motion (window-height)))
180 (set-window-start new-w (point))
181 (if (> (point) (window-point new-w))
182 (set-window-point new-w (point)))
183 (and (= moved (window-height))
184 (progn
185 (setq window-full-p t)
186 (vertical-motion -1)))
187 (setq bottom (point)))
188 (and window-full-p
189 (<= bottom (point))
190 (set-window-point old-w (1- bottom)))
191 (and window-full-p
192 (<= (window-start new-w) old-point)
193 (progn
194 (set-window-point new-w old-point)
195 (select-window new-w)))))
196 new-w))
197
198 (defun split-window-horizontally (&optional arg)
199 "Split current window into two windows side by side.
200 This window becomes the leftmost of the two, and gets ARG columns.
201 Negative arg means select the size of the rightmost window instead.
202 No arg means split equally."
203 (interactive "P")
204 (let ((size (and arg (prefix-numeric-value arg))))
205 (and size (< size 0)
206 (setq size (+ (window-width) size)))
207 (split-window nil size t)))
208 \f
209 (defun enlarge-window-horizontally (arg)
210 "Make current window ARG columns wider."
211 (interactive "p")
212 (enlarge-window arg t))
213
214 (defun shrink-window-horizontally (arg)
215 "Make current window ARG columns narrower."
216 (interactive "p")
217 (shrink-window arg t))
218
219 (defun shrink-window-if-larger-than-buffer (&optional window)
220 "Shrink the WINDOW to be as small as possible to display its contents.
221 Do not shrink to less than `window-min-height' lines.
222 Do nothing if the buffer contains more lines than the present window height,
223 or if some of the window's contents are scrolled out of view,
224 or if the window is not the full width of the frame,
225 or if the window is the only window of its frame."
226 (interactive)
227 (or window (setq window (selected-window)))
228 (save-excursion
229 (set-buffer (window-buffer window))
230 (let* ((w (selected-window)) ;save-window-excursion can't win
231 (buffer-file-name buffer-file-name)
232 (p (point))
233 (n 0)
234 (ignore-final-newline
235 ;; If buffer ends with a newline, ignore it when counting height
236 ;; unless point is after it.
237 (and (not (eobp))
238 (eq ?\n (char-after (1- (point-max))))))
239 (buffer-read-only nil)
240 (modified (buffer-modified-p))
241 (buffer (current-buffer))
242 (params (frame-parameters (window-frame window)))
243 (mini (cdr (assq 'minibuffer params)))
244 (edges (window-edges (selected-window))))
245 (if (and (< 1 (let ((frame (selected-frame)))
246 (select-frame (window-frame window))
247 (unwind-protect
248 (count-windows)
249 (select-frame frame))))
250 (= (window-width window) (frame-width (window-frame window)))
251 (pos-visible-in-window-p (point-min) window)
252 (not (eq mini 'only))
253 (or (not mini)
254 (< (nth 3 edges)
255 (nth 1 (window-edges mini)))
256 (> (nth 1 edges)
257 (cdr (assq 'menu-bar-lines params)))))
258 (unwind-protect
259 (progn
260 (select-window (or window w))
261 (goto-char (point-min))
262 (while (pos-visible-in-window-p
263 (- (point-max)
264 (if ignore-final-newline 1 0)))
265 ;; defeat file locking... don't try this at home, kids!
266 (setq buffer-file-name nil)
267 (insert ?\n) (setq n (1+ n)))
268 (if (> n 0)
269 (shrink-window (min (1- n)
270 (- (window-height)
271 window-min-height)))))
272 (delete-region (point-min) (point))
273 (set-buffer-modified-p modified)
274 (goto-char p)
275 (select-window w)
276 ;; Make sure we unbind buffer-read-only
277 ;; with the proper current buffer.
278 (set-buffer buffer))))))
279
280 (define-key ctl-x-map "2" 'split-window-vertically)
281 (define-key ctl-x-map "3" 'split-window-horizontally)
282 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
283 (define-key ctl-x-map "{" 'shrink-window-horizontally)
284 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
285 (define-key ctl-x-map "+" 'balance-windows)
286
287 ;;; windows.el ends here