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.
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)
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.
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
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
)))
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))
69 (let* ((walk-windows-start (selected-window))
70 (walk-windows-current walk-windows-start
))
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."
84 '((save-selected-window-window (selected-window)))
87 (list 'select-window
'save-selected-window-window
))))
89 (defun count-windows (&optional minibuf
)
90 "Returns the number of visible windows.
91 This counts the windows in the selected frame and (if the minibuffer is
92 to be counted) its minibuffer frame (if that's not the same frame).
93 The optional arg MINIBUF non-nil means count the minibuffer
94 even if it is inactive."
96 (walk-windows (function (lambda (w)
97 (setq count
(+ count
1))))
101 (defun balance-windows ()
102 "Makes all visible windows the same height (approximately)."
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))))))
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
))
116 (setq tops
(sort tops
'<))
118 (if (> (car tops
) (1+ prev-top
))
119 (setq prev-top
(car tops
)
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.
125 (setq size
(/ (- (frame-height) mbl
) count
))
126 (walk-windows (function
129 (let ((newtop (cdr (assq (nth 1 (window-edges))
131 (newbot (or (cdr (assq (+ (window-height)
132 (nth 1 (window-edges)))
136 (cons (cons w
(* size
(- newbot newtop
)))
139 (walk-windows (function (lambda (w)
141 (let ((newsize (cdr (assq w newsizes
))))
142 (enlarge-window (- newsize
146 ;;; I think this should be the default; I think people will prefer it--rms.
147 (defcustom split-window-keep-point t
148 "*If non-nil, split windows keeps the original point in both children.
149 This is often more convenient for editing.
150 If nil, adjust point in each of the two windows to minimize redisplay.
151 This is convenient on slow terminals, but point can move strangely."
155 (defun split-window-vertically (&optional arg
)
156 "Split current window into two windows, one above the other.
157 The uppermost window gets ARG lines and the other gets the rest.
158 Negative arg means select the size of the lowermost window instead.
159 With no argument, split equally or close to it.
160 Both windows display the same buffer now current.
162 If the variable `split-window-keep-point' is non-nil, both new windows
163 will get the same value of point as the current window. This is often
164 more convenient for editing.
166 Otherwise, we chose window starts so as to minimize the amount of
167 redisplay; this is convenient on slow terminals. The new selected
168 window is the one that the current value of point appears in. The
169 value of point can change if the text around point is hidden by the
172 (let ((old-w (selected-window))
174 (size (and arg
(prefix-numeric-value arg
)))
176 new-w bottom switch moved
)
177 (and size
(< size
0) (setq size
(+ (window-height) size
)))
178 (setq new-w
(split-window nil size
))
179 (or split-window-keep-point
182 (set-buffer (window-buffer))
183 (goto-char (window-start))
184 (setq moved
(vertical-motion (window-height)))
185 (set-window-start new-w
(point))
186 (if (> (point) (window-point new-w
))
187 (set-window-point new-w
(point)))
188 (and (= moved
(window-height))
190 (setq window-full-p t
)
191 (vertical-motion -
1)))
192 (setq bottom
(point)))
195 (set-window-point old-w
(1- bottom
)))
197 (<= (window-start new-w
) old-point
)
199 (set-window-point new-w old-point
)
200 (select-window new-w
)))))
201 (split-window-save-restore-data new-w old-w
)))
203 ;; This is to avoid compiler warnings.
204 (defvar view-return-to-alist
)
206 (defun split-window-save-restore-data (new-w old-w
)
208 (set-buffer (window-buffer))
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
))))
216 (defun split-window-horizontally (&optional arg
)
217 "Split current window into two windows side by side.
218 This window becomes the leftmost of the two, and gets ARG columns.
219 Negative arg means select the size of the rightmost window instead.
220 The argument includes the width of the window's scroll bar; if there
221 are no scroll bars, it includes the width of the divider column
222 to the window's right, if any. No arg means split equally."
224 (let ((old-w (selected-window))
225 (size (and arg
(prefix-numeric-value arg
))))
227 (setq size
(+ (window-width) size
)))
228 (split-window-save-restore-data (split-window nil size t
) old-w
)))
230 (defun enlarge-window-horizontally (arg)
231 "Make current window ARG columns wider."
233 (enlarge-window arg t
))
235 (defun shrink-window-horizontally (arg)
236 "Make current window ARG columns narrower."
238 (shrink-window arg t
))
240 (defun window-buffer-height (window)
241 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
243 (set-buffer (window-buffer window
))
244 (goto-char (point-min))
245 (let ((ignore-final-newline
246 ;; If buffer ends with a newline, ignore it when counting height
247 ;; unless point is after it.
248 (and (not (eobp)) (eq ?
\n (char-after (1- (point-max)))))))
249 (+ 1 (nth 2 (compute-motion (point-min)
251 (- (point-max) (if ignore-final-newline
1 0))
253 (window-width window
)
257 (defun shrink-window-if-larger-than-buffer (&optional window
)
258 "Shrink the WINDOW to be as small as possible to display its contents.
259 Do not shrink to less than `window-min-height' lines.
260 Do nothing if the buffer contains more lines than the present window height,
261 or if some of the window's contents are scrolled out of view,
262 or if the window is not the full width of the frame,
263 or if the window is the only window of its frame."
265 (save-selected-window
267 (select-window window
)
268 (setq window
(selected-window)))
269 (let* ((params (frame-parameters))
270 (mini (cdr (assq 'minibuffer params
)))
271 (edges (window-edges)))
272 (if (and (< 1 (count-windows))
273 (= (window-width) (frame-width))
274 (pos-visible-in-window-p (point-min) window
)
275 (not (eq mini
'only
))
277 (< (nth 3 edges
) (nth 1 (window-edges mini
)))
278 (> (nth 1 edges
) (cdr (assq 'menu-bar-lines params
)))))
279 (let ((text-height (window-buffer-height window
))
280 (window-height (window-height)))
281 ;; Don't try to redisplay with the cursor at the end
282 ;; on its own line--that would force a scroll and spoil things.
283 (when (and (eobp) (bolp) (not (bobp)))
285 (when (> window-height
(1+ text-height
))
287 (- window-height
(max (1+ text-height
) window-min-height
)))))))))
289 (defun kill-buffer-and-window ()
290 "Kill the current buffer and delete the selected window."
292 (if (yes-or-no-p (format "Kill buffer `%s'? " (buffer-name)))
293 (let ((buffer (current-buffer)))
294 (delete-window (selected-window))
295 (kill-buffer buffer
))
298 (defun quit-window (&optional kill window
)
299 "Quit the current buffer. Bury it, and maybe delete the selected frame.
300 \(The frame is deleted if it is contains a dedicated window for the buffer.)
301 With a prefix argument, kill the buffer instead.
303 Noninteractively, if KILL is non-nil, then kill the current buffer,
306 If WINDOW is non-nil, it specifies a window; we delete that window,
307 and the buffer that is killed or buried is the one in that window."
309 (let ((buffer (window-buffer window
))
310 (frame (window-frame (or window
(selected-window))))
312 (save-selected-window
314 (select-window window
))
318 (save-selected-window
320 (select-window window
))
321 (or (window-minibuffer-p)
322 (window-dedicated-p (selected-window))
323 (switch-to-buffer (other-buffer))))
325 ;; Get rid of the frame, if it has just one dedicated window
326 ;; and other visible frames exist.
327 (and (or (window-minibuffer-p) (window-dedicated-p window
))
328 (delq frame
(visible-frame-list))
330 (if (and (eq default-minibuffer-frame frame
)
331 (= 1 (length (minibuffer-frame-list))))
334 (setq window-handled t
)))
336 ;; Deal with the buffer.
339 (bury-buffer buffer
))
341 ;; Maybe get rid of the window.
342 (and window
(not window-handled
) (not window-solitary
)
343 (delete-window window
))))
345 (define-key ctl-x-map
"2" 'split-window-vertically
)
346 (define-key ctl-x-map
"3" 'split-window-horizontally
)
347 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
348 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
349 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
350 (define-key ctl-x-map
"+" 'balance-windows
)
351 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
353 ;;; windows.el ends here