1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992, 1993, 1994, 2000, 2001
4 ;; Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Window tree functions.
32 (defun one-window-p (&optional nomini all-frames
)
33 "Return non-nil if the selected window is the only window (in its frame).
34 Optional arg NOMINI non-nil means don't count the minibuffer
37 The optional arg ALL-FRAMES t means count windows on all frames.
38 If it is `visible', count windows on all visible frames.
39 ALL-FRAMES nil or omitted means count only the selected frame,
40 plus the minibuffer it uses (which may be on another frame).
41 If ALL-FRAMES is neither nil nor t, count only the selected frame."
42 (let ((base-window (selected-window)))
43 (if (and nomini
(eq base-window
(minibuffer-window)))
44 (setq base-window
(next-window base-window
)))
46 (next-window base-window
(if nomini
'arg
) all-frames
))))
48 (defun walk-windows (proc &optional minibuf all-frames
)
49 "Cycle through all visible windows, calling PROC for each one.
50 PROC is called with a window as argument.
52 Optional second arg MINIBUF t means count the minibuffer window even
53 if not active. MINIBUF nil or omitted means count the minibuffer iff
54 it is active. MINIBUF neither t nor nil means not to count the
55 minibuffer even if it is active.
57 Several frames may share a single minibuffer; if the minibuffer
58 counts, all windows on all frames that share that minibuffer count
59 too. Therefore, if you are using a separate minibuffer frame
60 and the minibuffer is active and MINIBUF says it counts,
61 `walk-windows' includes the windows in the frame from which you
62 entered the minibuffer, as well as the minibuffer window.
64 ALL-FRAMES is the optional third argument.
65 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
66 ALL-FRAMES = `visible' means include windows on all visible frames.
67 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
68 ALL-FRAMES = t means include windows on all frames including invisible frames.
69 If ALL-FRAMES is a frame, it means include windows on that frame.
70 Anything else means restrict to the selected frame."
71 ;; If we start from the minibuffer window, don't fail to come back to it.
72 (if (window-minibuffer-p (selected-window))
75 (if (framep all-frames
)
76 (select-window (frame-first-window all-frames
)))
77 (let* (walk-windows-already-seen
78 (walk-windows-current (selected-window)))
80 (setq walk-windows-current
81 (next-window walk-windows-current minibuf all-frames
))
82 (not (memq walk-windows-current walk-windows-already-seen
)))
83 (setq walk-windows-already-seen
84 (cons walk-windows-current walk-windows-already-seen
))
85 (funcall proc walk-windows-current
)))))
87 (defun get-window-with-predicate (predicate &optional minibuf
89 "Return a window satisfying PREDICATE.
91 This function cycles through all visible windows using `walk-windows',
92 calling PREDICATE on each one. PREDICATE is called with a window as
93 argument. The first window for which PREDICATE returns a non-nil
94 value is returned. If no window satisfies PREDICATE, DEFAULT is
97 Optional second arg MINIBUF t means count the minibuffer window even
98 if not active. MINIBUF nil or omitted means count the minibuffer iff
99 it is active. MINIBUF neither t nor nil means not to count the
100 minibuffer even if it is active.
102 Several frames may share a single minibuffer; if the minibuffer
103 counts, all windows on all frames that share that minibuffer count
104 too. Therefore, if you are using a separate minibuffer frame
105 and the minibuffer is active and MINIBUF says it counts,
106 `walk-windows' includes the windows in the frame from which you
107 entered the minibuffer, as well as the minibuffer window.
109 ALL-FRAMES is the optional third argument.
110 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
111 ALL-FRAMES = `visible' means include windows on all visible frames.
112 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
113 ALL-FRAMES = t means include windows on all frames including invisible frames.
114 If ALL-FRAMES is a frame, it means include windows on that frame.
115 Anything else means restrict to the selected frame."
117 (walk-windows #'(lambda (window)
118 (when (funcall predicate window
)
119 (throw 'found window
)))
123 (defalias 'some-window
'get-window-with-predicate
)
125 (defun minibuffer-window-active-p (window)
126 "Return t if WINDOW (a minibuffer window) is now active."
127 (eq window
(active-minibuffer-window)))
129 (defmacro save-selected-window
(&rest body
)
130 "Execute BODY, then select the window that was selected before BODY.
131 However, if that window has become dead, don't get an error,
132 just refrain from switching to it."
133 `(let ((save-selected-window-window (selected-window)))
136 (if (window-live-p save-selected-window-window
)
137 (select-window save-selected-window-window
)))))
139 (defun count-windows (&optional minibuf
)
140 "Return the number of visible windows.
141 This counts the windows in the selected frame and (if the minibuffer is
142 to be counted) its minibuffer frame (if that's not the same frame).
143 The optional arg MINIBUF non-nil means count the minibuffer
144 even if it is inactive."
146 (walk-windows (function (lambda (w)
147 (setq count
(+ count
1))))
151 (defun window-safely-shrinkable-p (&optional window
)
152 "Non-nil if the WINDOW can be shrunk without shrinking other windows.
153 If WINDOW is nil or omitted, it defaults to the currently selected window."
154 (save-selected-window
155 (when window
(select-window window
))
156 (or (and (not (eq window
(frame-first-window)))
157 (= (car (window-edges))
158 (car (window-edges (previous-window)))))
159 (= (car (window-edges))
160 (car (window-edges (next-window)))))))
162 (defun balance-windows ()
163 "Make all visible windows the same height (approximately)."
165 (let ((count -
1) levels newsizes level-size
166 ;; Don't count the lines that are above the uppermost windows.
167 ;; (These are the menu bar lines, if any.)
168 (mbl (nth 1 (window-edges (frame-first-window (selected-frame)))))
169 (last-window (previous-window (frame-first-window (selected-frame))))
170 ;; Don't count the lines that are past the lowest main window.
172 ;; Bottom edge of last window determines what size we have to work with.
174 (+ (window-height last-window
)
175 (nth 1 (window-edges last-window
))))
177 ;; Find all the different vpos's at which windows start,
178 ;; then count them. But ignore levels that differ by only 1.
179 (let (tops (prev-top -
2))
180 (walk-windows (function (lambda (w)
181 (setq tops
(cons (nth 1 (window-edges w
))
184 (setq tops
(sort tops
'<))
186 (if (> (car tops
) (1+ prev-top
))
187 (setq prev-top
(car tops
)
189 (setq levels
(cons (cons (car tops
) count
) levels
))
190 (setq tops
(cdr tops
)))
191 (setq count
(1+ count
)))
192 ;; Subdivide the frame into desired number of vertical levels.
193 (setq level-size
(/ (- total mbl
) count
))
194 (save-selected-window
195 ;; Set up NEWSIZES to map windows to their desired sizes.
196 ;; If a window ends at the bottom level, don't include
197 ;; it in NEWSIZES. Those windows get the right sizes
198 ;; by adjusting the ones above them.
199 (walk-windows (function
201 (let ((newtop (cdr (assq (nth 1 (window-edges w
))
203 (newbot (cdr (assq (+ (window-height w
)
204 (nth 1 (window-edges w
)))
208 (cons (cons w
(* level-size
(- newbot newtop
)))
211 ;; Make walk-windows start with the topmost window.
212 (select-window (previous-window (frame-first-window (selected-frame))))
213 (let (done (count 0))
214 ;; Give each window its precomputed size, or at least try.
215 ;; Keep trying until they all get the intended sizes,
216 ;; but not more than 3 times (to prevent infinite loop).
217 (while (and (not done
) (< count
3))
219 (setq count
(1+ count
))
220 (walk-windows (function (lambda (w)
222 (let ((newsize (cdr (assq w newsizes
))))
224 (enlarge-window (- newsize
227 (unless (= (window-height) newsize
)
231 ;;; I think this should be the default; I think people will prefer it--rms.
232 (defcustom split-window-keep-point t
233 "*If non-nil, split windows keeps the original point in both children.
234 This is often more convenient for editing.
235 If nil, adjust point in each of the two windows to minimize redisplay.
236 This is convenient on slow terminals, but point can move strangely."
240 (defun split-window-vertically (&optional arg
)
241 "Split current window into two windows, one above the other.
242 The uppermost window gets ARG lines and the other gets the rest.
243 Negative arg means select the size of the lowermost window instead.
244 With no argument, split equally or close to it.
245 Both windows display the same buffer now current.
247 If the variable `split-window-keep-point' is non-nil, both new windows
248 will get the same value of point as the current window. This is often
249 more convenient for editing.
251 Otherwise, we chose window starts so as to minimize the amount of
252 redisplay; this is convenient on slow terminals. The new selected
253 window is the one that the current value of point appears in. The
254 value of point can change if the text around point is hidden by the
257 (let ((old-w (selected-window))
259 (size (and arg
(prefix-numeric-value arg
)))
261 new-w bottom switch moved
)
262 (and size
(< size
0) (setq size
(+ (window-height) size
)))
263 (setq new-w
(split-window nil size
))
264 (or split-window-keep-point
267 (set-buffer (window-buffer))
268 (goto-char (window-start))
269 (setq moved
(vertical-motion (window-height)))
270 (set-window-start new-w
(point))
271 (if (> (point) (window-point new-w
))
272 (set-window-point new-w
(point)))
273 (and (= moved
(window-height))
275 (setq window-full-p t
)
276 (vertical-motion -
1)))
277 (setq bottom
(point)))
280 (set-window-point old-w
(1- bottom
)))
282 (<= (window-start new-w
) old-point
)
284 (set-window-point new-w old-point
)
285 (select-window new-w
)))))
286 (split-window-save-restore-data new-w old-w
)))
288 ;; This is to avoid compiler warnings.
289 (defvar view-return-to-alist
)
291 (defun split-window-save-restore-data (new-w old-w
)
293 (set-buffer (window-buffer))
295 (let ((old-info (assq old-w view-return-to-alist
)))
296 (setq view-return-to-alist
297 (cons (cons new-w
(cons (and old-info
(car (cdr old-info
))) t
))
298 view-return-to-alist
))))
301 (defun split-window-horizontally (&optional arg
)
302 "Split current window into two windows side by side.
303 This window becomes the leftmost of the two, and gets ARG columns.
304 Negative arg means select the size of the rightmost window instead.
305 The argument includes the width of the window's scroll bar; if there
306 are no scroll bars, it includes the width of the divider column
307 to the window's right, if any. No arg means split equally."
309 (let ((old-w (selected-window))
310 (size (and arg
(prefix-numeric-value arg
))))
312 (setq size
(+ (window-width) size
)))
313 (split-window-save-restore-data (split-window nil size t
) old-w
)))
316 (defun set-window-text-height (window height
)
317 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
318 This doesn't include the mode-line (or header-line if any) or any
319 partial-height lines in the text display area.
321 If WINDOW is nil, the selected window is used.
323 Note that the current implementation of this function cannot always set
324 the height exactly, but attempts to be conservative, by allocating more
325 lines than are actually needed in the case where some error may be present."
326 (let ((delta (- height
(window-text-height window
))))
327 (unless (zerop delta
)
328 (let ((window-min-height 1))
329 (if (and window
(not (eq window
(selected-window))))
330 (save-selected-window
331 (select-window window
)
332 (enlarge-window delta
))
333 (enlarge-window delta
))))))
336 (defun enlarge-window-horizontally (arg)
337 "Make current window ARG columns wider."
339 (enlarge-window arg t
))
341 (defun shrink-window-horizontally (arg)
342 "Make current window ARG columns narrower."
344 (shrink-window arg t
))
346 (defun window-buffer-height (window)
347 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
349 (set-buffer (window-buffer window
))
350 (goto-char (point-min))
351 (let ((ignore-final-newline
352 ;; If buffer ends with a newline, ignore it when counting height
353 ;; unless point is after it.
354 (and (not (eobp)) (eq ?
\n (char-after (1- (point-max)))))))
355 (+ 1 (nth 2 (compute-motion (point-min)
357 (- (point-max) (if ignore-final-newline
1 0))
359 (window-width window
)
363 (defun count-screen-lines (&optional beg end count-final-newline window
)
364 "Return the number of screen lines in the region.
365 The number of screen lines may be different from the number of actual lines,
366 due to line breaking, display table, etc.
368 Optional arguments BEG and END default to `point-min' and `point-max'
371 If region ends with a newline, ignore it unless optional third argument
372 COUNT-FINAL-NEWLINE is non-nil.
374 The optional fourth argument WINDOW specifies the window used for obtaining
375 parameters such as width, horizontal scrolling, and so on. The default is
376 to use the selected window's parameters.
378 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
379 regardless of which buffer is displayed in WINDOW. This makes possible to use
380 `count-screen-lines' in any buffer, whether or not it is currently displayed
383 (setq beg
(point-min)))
385 (setq end
(point-max)))
391 (narrow-to-region (min beg end
)
392 (if (and (not count-final-newline
)
393 (= ?
\n (char-before (max beg end
))))
396 (goto-char (point-min))
397 (1+ (vertical-motion (buffer-size) window
))))))
399 (defun fit-window-to-buffer (&optional window max-height min-height
)
400 "Make WINDOW the right size to display its contents exactly.
401 If WINDOW is omitted or nil, it defaults to the selected window.
402 If the optional argument MAX-HEIGHT is supplied, it is the maximum height
403 the window is allowed to be, defaulting to the frame height.
404 If the optional argument MIN-HEIGHT is supplied, it is the minimum
405 height the window is allowed to be, defaulting to `window-min-height'.
407 The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or
412 (setq window
(selected-window)))
413 (when (null max-height
)
414 (setq max-height
(frame-height (window-frame window
))))
417 ;; Buffer that is displayed in WINDOW
418 (window-buffer window
))
420 ;; The current height of WINDOW
421 (window-height window
))
423 ;; The height necessary to show the buffer displayed by WINDOW
424 ;; (`count-screen-lines' always works on the current buffer).
425 (with-current-buffer buf
426 (+ (count-screen-lines)
427 ;; If the buffer is empty, (count-screen-lines) is
428 ;; zero. But, even in that case, we need one text line
430 (if (= (point-min) (point-max))
432 ;; For non-minibuffers, count the mode-line, if any
433 (if (and (not (window-minibuffer-p window
))
436 ;; Count the header-line, if any
437 (if header-line-format
1 0))))
439 ;; Calculate how much the window height has to change to show
440 ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
441 (- (max (min desired-height max-height
)
442 (or min-height window-min-height
))
444 ;; We do our own height checking, so avoid any restrictions due to
445 ;; window-min-height.
446 (window-min-height 1))
448 ;; Don't try to redisplay with the cursor at the end
449 ;; on its own line--that would force a scroll and spoil things.
450 (when (with-current-buffer buf
451 (and (eobp) (bolp) (not (bobp))))
452 (set-window-point window
(1- (window-point window
))))
454 (save-selected-window
455 (select-window window
)
457 ;; Adjust WINDOW to the nominally correct size (which may actually
458 ;; be slightly off because of variable height text, etc).
459 (unless (zerop delta
)
460 (enlarge-window delta
))
462 ;; Check if the last line is surely fully visible. If not,
463 ;; enlarge the window.
464 (let ((end (with-current-buffer buf
466 (goto-char (point-max))
467 (when (and (bolp) (not (bobp)))
468 ;; Don't include final newline
471 ;; If line-wrapping is turned off, test the
472 ;; beginning of the last line for visibility
473 ;; instead of the end, as the end of the line
474 ;; could be invisible by virtue of extending past
475 ;; the edge of the window.
478 (set-window-vscroll window
0)
479 (while (and (< desired-height max-height
)
480 (= desired-height
(window-height window
))
481 (not (pos-visible-in-window-p end window
)))
483 (setq desired-height
(1+ desired-height
)))))))
485 (defun shrink-window-if-larger-than-buffer (&optional window
)
486 "Shrink the WINDOW to be as small as possible to display its contents.
487 If WINDOW is omitted or nil, it defaults to the selected window.
488 Do not shrink to less than `window-min-height' lines.
489 Do nothing if the buffer contains more lines than the present window height,
490 or if some of the window's contents are scrolled out of view,
491 or if shrinking this window would also shrink another window.
492 or if the window is the only window of its frame.
493 Return non-nil if the window was shrunk."
496 (setq window
(selected-window)))
497 (let* ((frame (window-frame window
))
498 (mini (frame-parameter frame
'minibuffer
))
499 (edges (window-edges window
)))
500 (if (and (not (eq window
(frame-root-window frame
)))
501 (window-safely-shrinkable-p)
502 (pos-visible-in-window-p (point-min) window
)
503 (not (eq mini
'only
))
505 (let ((mini-window (minibuffer-window frame
)))
506 (or (null mini-window
)
507 (not (eq frame
(window-frame mini-window
)))
509 (nth 1 (window-edges mini-window
)))
511 (frame-parameter frame
'menu-bar-lines
))))))
512 (fit-window-to-buffer window
(window-height window
)))))
514 (defun kill-buffer-and-window ()
515 "Kill the current buffer and delete the selected window."
517 (if (yes-or-no-p (format "Kill buffer `%s'? " (buffer-name)))
518 (let ((buffer (current-buffer)))
519 (delete-window (selected-window))
520 (kill-buffer buffer
))
523 (defun quit-window (&optional kill window
)
524 "Quit the current buffer. Bury it, and maybe delete the selected frame.
525 \(The frame is deleted if it is contains a dedicated window for the buffer.)
526 With a prefix argument, kill the buffer instead.
528 Noninteractively, if KILL is non-nil, then kill the current buffer,
531 If WINDOW is non-nil, it specifies a window; we delete that window,
532 and the buffer that is killed or buried is the one in that window."
534 (let ((buffer (window-buffer window
))
535 (frame (window-frame (or window
(selected-window))))
537 (save-selected-window
539 (select-window window
))
543 (save-selected-window
545 (select-window window
))
546 (or (window-minibuffer-p)
547 (window-dedicated-p (selected-window))
548 (switch-to-buffer (other-buffer))))
550 ;; Get rid of the frame, if it has just one dedicated window
551 ;; and other visible frames exist.
552 (and (or (window-minibuffer-p) (window-dedicated-p window
))
553 (delq frame
(visible-frame-list))
555 (if (and (eq default-minibuffer-frame frame
)
556 (= 1 (length (minibuffer-frame-list))))
559 (setq window-handled t
)))
561 ;; Deal with the buffer.
564 (bury-buffer buffer
))
566 ;; Maybe get rid of the window.
567 (and window
(not window-handled
) (not window-solitary
)
568 (delete-window window
))))
570 (define-key ctl-x-map
"2" 'split-window-vertically
)
571 (define-key ctl-x-map
"3" 'split-window-horizontally
)
572 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
573 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
574 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
575 (define-key ctl-x-map
"+" 'balance-windows
)
576 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
578 ;;; window.el ends here