1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992, 1993, 1994, 2000, 2001, 2002, 2004
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 (defmacro save-selected-window
(&rest body
)
33 "Execute BODY, then select the window that was selected before BODY.
34 Also restore the selected window of each frame as it was at the start
36 However, if a window has become dead, don't get an error,
37 just refrain from reselecting it."
38 `(let ((save-selected-window-window (selected-window))
39 (save-selected-window-alist
40 (mapcar (lambda (frame) (list frame
(frame-selected-window frame
)))
44 (dolist (elt save-selected-window-alist
)
45 (and (frame-live-p (car elt
))
46 (window-live-p (cadr elt
))
47 (set-frame-selected-window (car elt
) (cadr elt
))))
48 (if (window-live-p save-selected-window-window
)
49 (select-window save-selected-window-window
)))))
51 (defun window-body-height (&optional window
)
52 "Return number of lines in window WINDOW for actual buffer text.
53 This does not include the mode line (if any) or the header line (if any)."
54 (or window
(setq window
(selected-window)))
55 (if (window-minibuffer-p window
)
56 (window-height window
)
57 (with-current-buffer (window-buffer window
)
58 (max 1 (- (window-height window
)
59 (if mode-line-format
1 0)
60 (if header-line-format
1 0))))))
62 (defun one-window-p (&optional nomini all-frames
)
63 "Return non-nil if the selected window is the only window (in its frame).
64 Optional arg NOMINI non-nil means don't count the minibuffer
67 The optional arg ALL-FRAMES t means count windows on all frames.
68 If it is `visible', count windows on all visible frames.
69 ALL-FRAMES nil or omitted means count only the selected frame,
70 plus the minibuffer it uses (which may be on another frame).
71 If ALL-FRAMES is neither nil nor t, count only the selected frame."
72 (let ((base-window (selected-window)))
73 (if (and nomini
(eq base-window
(minibuffer-window)))
74 (setq base-window
(next-window base-window
)))
76 (next-window base-window
(if nomini
'arg
) all-frames
))))
78 (defun window-current-scroll-bars (&optional window
)
79 "Return the current scroll-bar settings in window WINDOW.
80 Value is a cons (VERTICAL . HORISONTAL) where VERTICAL specifies the
81 current location of the vertical scroll-bars (left, right, or nil),
82 and HORISONTAL specifies the current location of the horisontal scroll
83 bars (top, bottom, or nil)."
84 (let ((vert (nth 2 (window-scroll-bars window
)))
86 (when (or (eq vert t
) (eq hor t
))
87 (let ((fcsb (frame-current-scroll-bars
88 (window-frame (or window
(selected-window))))))
90 (setq vert
(car fcsb
)))
92 (setq hor
(cdr fcsb
)))))
95 (defun walk-windows (proc &optional minibuf all-frames
)
96 "Cycle through all visible windows, calling PROC for each one.
97 PROC is called with a window as argument.
99 Optional second arg MINIBUF t means count the minibuffer window even
100 if not active. MINIBUF nil or omitted means count the minibuffer iff
101 it is active. MINIBUF neither t nor nil means not to count the
102 minibuffer even if it is active.
104 Several frames may share a single minibuffer; if the minibuffer
105 counts, all windows on all frames that share that minibuffer count
106 too. Therefore, if you are using a separate minibuffer frame
107 and the minibuffer is active and MINIBUF says it counts,
108 `walk-windows' includes the windows in the frame from which you
109 entered the minibuffer, as well as the minibuffer window.
111 ALL-FRAMES is the optional third argument.
112 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
113 ALL-FRAMES = `visible' means include windows on all visible frames.
114 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
115 ALL-FRAMES = t means include windows on all frames including invisible frames.
116 If ALL-FRAMES is a frame, it means include windows on that frame.
117 Anything else means restrict to the selected frame."
118 ;; If we start from the minibuffer window, don't fail to come back to it.
119 (if (window-minibuffer-p (selected-window))
121 (save-selected-window
122 (if (framep all-frames
)
123 (select-window (frame-first-window all-frames
)))
124 (let* (walk-windows-already-seen
125 (walk-windows-current (selected-window)))
127 (setq walk-windows-current
128 (next-window walk-windows-current minibuf all-frames
))
129 (not (memq walk-windows-current walk-windows-already-seen
)))
130 (setq walk-windows-already-seen
131 (cons walk-windows-current walk-windows-already-seen
))
132 (funcall proc walk-windows-current
)))))
134 (defun get-window-with-predicate (predicate &optional minibuf
136 "Return a window satisfying PREDICATE.
138 This function cycles through all visible windows using `walk-windows',
139 calling PREDICATE on each one. PREDICATE is called with a window as
140 argument. The first window for which PREDICATE returns a non-nil
141 value is returned. If no window satisfies PREDICATE, DEFAULT is
144 Optional second arg MINIBUF t means count the minibuffer window even
145 if not active. MINIBUF nil or omitted means count the minibuffer iff
146 it is active. MINIBUF neither t nor nil means not to count the
147 minibuffer even if it is active.
149 Several frames may share a single minibuffer; if the minibuffer
150 counts, all windows on all frames that share that minibuffer count
151 too. Therefore, if you are using a separate minibuffer frame
152 and the minibuffer is active and MINIBUF says it counts,
153 `walk-windows' includes the windows in the frame from which you
154 entered the minibuffer, as well as the minibuffer window.
156 ALL-FRAMES is the optional third argument.
157 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
158 ALL-FRAMES = `visible' means include windows on all visible frames.
159 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
160 ALL-FRAMES = t means include windows on all frames including invisible frames.
161 If ALL-FRAMES is a frame, it means include windows on that frame.
162 Anything else means restrict to the selected frame."
164 (walk-windows #'(lambda (window)
165 (when (funcall predicate window
)
166 (throw 'found window
)))
170 (defalias 'some-window
'get-window-with-predicate
)
172 (defun minibuffer-window-active-p (window)
173 "Return t if WINDOW (a minibuffer window) is now active."
174 (eq window
(active-minibuffer-window)))
176 (defun count-windows (&optional minibuf
)
177 "Return the number of visible windows.
178 This counts the windows in the selected frame and (if the minibuffer is
179 to be counted) its minibuffer frame (if that's not the same frame).
180 The optional arg MINIBUF non-nil means count the minibuffer
181 even if it is inactive."
183 (walk-windows (function (lambda (w)
184 (setq count
(+ count
1))))
188 (defun window-safely-shrinkable-p (&optional window
)
189 "Non-nil if the WINDOW can be shrunk without shrinking other windows.
190 If WINDOW is nil or omitted, it defaults to the currently selected window."
191 (with-selected-window (or window
(selected-window))
192 (let ((edges (window-edges)))
193 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
194 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
197 (defun balance-windows ()
198 "Make all visible windows the same height (approximately)."
200 (let ((count -
1) levels newsizes level-size
201 ;; Don't count the lines that are above the uppermost windows.
202 ;; (These are the menu bar lines, if any.)
203 (mbl (nth 1 (window-edges (frame-first-window (selected-frame)))))
204 (last-window (previous-window (frame-first-window (selected-frame))))
205 ;; Don't count the lines that are past the lowest main window.
207 ;; Bottom edge of last window determines what size we have to work with.
209 (+ (window-height last-window
)
210 (nth 1 (window-edges last-window
))))
212 ;; Find all the different vpos's at which windows start,
213 ;; then count them. But ignore levels that differ by only 1.
214 (let (tops (prev-top -
2))
215 (walk-windows (function (lambda (w)
216 (setq tops
(cons (nth 1 (window-edges w
))
219 (setq tops
(sort tops
'<))
221 (if (> (car tops
) (1+ prev-top
))
222 (setq prev-top
(car tops
)
224 (setq levels
(cons (cons (car tops
) count
) levels
))
225 (setq tops
(cdr tops
)))
226 (setq count
(1+ count
)))
227 ;; Subdivide the frame into desired number of vertical levels.
228 (setq level-size
(/ (- total mbl
) count
))
229 (save-selected-window
230 ;; Set up NEWSIZES to map windows to their desired sizes.
231 ;; If a window ends at the bottom level, don't include
232 ;; it in NEWSIZES. Those windows get the right sizes
233 ;; by adjusting the ones above them.
234 (walk-windows (function
236 (let ((newtop (cdr (assq (nth 1 (window-edges w
))
238 (newbot (cdr (assq (+ (window-height w
)
239 (nth 1 (window-edges w
)))
243 (cons (cons w
(* level-size
(- newbot newtop
)))
246 ;; Make walk-windows start with the topmost window.
247 (select-window (previous-window (frame-first-window (selected-frame))))
248 (let (done (count 0))
249 ;; Give each window its precomputed size, or at least try.
250 ;; Keep trying until they all get the intended sizes,
251 ;; but not more than 3 times (to prevent infinite loop).
252 (while (and (not done
) (< count
3))
254 (setq count
(1+ count
))
255 (walk-windows (function (lambda (w)
257 (let ((newsize (cdr (assq w newsizes
))))
259 (enlarge-window (- newsize
262 (unless (= (window-height) newsize
)
266 ;; I think this should be the default; I think people will prefer it--rms.
267 (defcustom split-window-keep-point t
268 "*If non-nil, split windows keeps the original point in both children.
269 This is often more convenient for editing.
270 If nil, adjust point in each of the two windows to minimize redisplay.
271 This is convenient on slow terminals, but point can move strangely."
275 (defun split-window-vertically (&optional arg
)
276 "Split current window into two windows, one above the other.
277 The uppermost window gets ARG lines and the other gets the rest.
278 Negative arg means select the size of the lowermost window instead.
279 With no argument, split equally or close to it.
280 Both windows display the same buffer now current.
282 If the variable `split-window-keep-point' is non-nil, both new windows
283 will get the same value of point as the current window. This is often
284 more convenient for editing.
286 Otherwise, we chose window starts so as to minimize the amount of
287 redisplay; this is convenient on slow terminals. The new selected
288 window is the one that the current value of point appears in. The
289 value of point can change if the text around point is hidden by the
292 (let ((old-w (selected-window))
294 (size (and arg
(prefix-numeric-value arg
)))
296 new-w bottom switch moved
)
297 (and size
(< size
0) (setq size
(+ (window-height) size
)))
298 (setq new-w
(split-window nil size
))
299 (or split-window-keep-point
302 (set-buffer (window-buffer))
303 (goto-char (window-start))
304 (setq moved
(vertical-motion (window-height)))
305 (set-window-start new-w
(point))
306 (if (> (point) (window-point new-w
))
307 (set-window-point new-w
(point)))
308 (and (= moved
(window-height))
310 (setq window-full-p t
)
311 (vertical-motion -
1)))
312 (setq bottom
(point)))
315 (set-window-point old-w
(1- bottom
)))
317 (<= (window-start new-w
) old-point
)
319 (set-window-point new-w old-point
)
320 (select-window new-w
)))))
321 (split-window-save-restore-data new-w old-w
)))
323 ;; This is to avoid compiler warnings.
324 (defvar view-return-to-alist
)
326 (defun split-window-save-restore-data (new-w old-w
)
327 (with-current-buffer (window-buffer)
329 (let ((old-info (assq old-w view-return-to-alist
)))
330 (push (cons new-w
(cons (and old-info
(car (cdr old-info
))) t
))
331 view-return-to-alist
)))
334 (defun split-window-horizontally (&optional arg
)
335 "Split current window into two windows side by side.
336 This window becomes the leftmost of the two, and gets ARG columns.
337 Negative arg means select the size of the rightmost window instead.
338 The argument includes the width of the window's scroll bar; if there
339 are no scroll bars, it includes the width of the divider column
340 to the window's right, if any. No arg means split equally."
342 (let ((old-w (selected-window))
343 (size (and arg
(prefix-numeric-value arg
))))
345 (setq size
(+ (window-width) size
)))
346 (split-window-save-restore-data (split-window nil size t
) old-w
)))
349 (defun set-window-text-height (window height
)
350 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
351 This doesn't include the mode-line (or header-line if any) or any
352 partial-height lines in the text display area.
354 If WINDOW is nil, the selected window is used.
356 Note that the current implementation of this function cannot always set
357 the height exactly, but attempts to be conservative, by allocating more
358 lines than are actually needed in the case where some error may be present."
359 (let ((delta (- height
(window-text-height window
))))
360 (unless (zerop delta
)
361 (let ((window-min-height 1))
362 (if (and window
(not (eq window
(selected-window))))
363 (save-selected-window
364 (select-window window
)
365 (enlarge-window delta
))
366 (enlarge-window delta
))))))
369 (defun enlarge-window-horizontally (arg)
370 "Make current window ARG columns wider."
372 (enlarge-window arg t
))
374 (defun shrink-window-horizontally (arg)
375 "Make current window ARG columns narrower."
377 (shrink-window arg t
))
379 (defun window-buffer-height (window)
380 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
382 (set-buffer (window-buffer window
))
383 (goto-char (point-min))
384 (let ((ignore-final-newline
385 ;; If buffer ends with a newline, ignore it when counting height
386 ;; unless point is after it.
387 (and (not (eobp)) (eq ?
\n (char-after (1- (point-max)))))))
388 (+ 1 (nth 2 (compute-motion (point-min)
390 (- (point-max) (if ignore-final-newline
1 0))
392 (window-width window
)
396 (defun count-screen-lines (&optional beg end count-final-newline window
)
397 "Return the number of screen lines in the region.
398 The number of screen lines may be different from the number of actual lines,
399 due to line breaking, display table, etc.
401 Optional arguments BEG and END default to `point-min' and `point-max'
404 If region ends with a newline, ignore it unless optional third argument
405 COUNT-FINAL-NEWLINE is non-nil.
407 The optional fourth argument WINDOW specifies the window used for obtaining
408 parameters such as width, horizontal scrolling, and so on. The default is
409 to use the selected window's parameters.
411 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
412 regardless of which buffer is displayed in WINDOW. This makes possible to use
413 `count-screen-lines' in any buffer, whether or not it is currently displayed
416 (setq beg
(point-min)))
418 (setq end
(point-max)))
424 (narrow-to-region (min beg end
)
425 (if (and (not count-final-newline
)
426 (= ?
\n (char-before (max beg end
))))
429 (goto-char (point-min))
430 (1+ (vertical-motion (buffer-size) window
))))))
432 (defun fit-window-to-buffer (&optional window max-height min-height
)
433 "Make WINDOW the right size to display its contents exactly.
434 If WINDOW is omitted or nil, it defaults to the selected window.
435 If the optional argument MAX-HEIGHT is supplied, it is the maximum height
436 the window is allowed to be, defaulting to the frame height.
437 If the optional argument MIN-HEIGHT is supplied, it is the minimum
438 height the window is allowed to be, defaulting to `window-min-height'.
440 The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or
445 (setq window
(selected-window)))
446 (when (null max-height
)
447 (setq max-height
(frame-height (window-frame window
))))
450 ;; Buffer that is displayed in WINDOW
451 (window-buffer window
))
453 ;; The current height of WINDOW
454 (window-height window
))
456 ;; The height necessary to show the buffer displayed by WINDOW
457 ;; (`count-screen-lines' always works on the current buffer).
458 (with-current-buffer buf
459 (+ (count-screen-lines)
460 ;; If the buffer is empty, (count-screen-lines) is
461 ;; zero. But, even in that case, we need one text line
463 (if (= (point-min) (point-max))
465 ;; For non-minibuffers, count the mode-line, if any
466 (if (and (not (window-minibuffer-p window
))
469 ;; Count the header-line, if any
470 (if header-line-format
1 0))))
472 ;; Calculate how much the window height has to change to show
473 ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
474 (- (max (min desired-height max-height
)
475 (or min-height window-min-height
))
477 ;; We do our own height checking, so avoid any restrictions due to
478 ;; window-min-height.
479 (window-min-height 1))
481 ;; Don't try to redisplay with the cursor at the end
482 ;; on its own line--that would force a scroll and spoil things.
483 (when (with-current-buffer buf
484 (and (eobp) (bolp) (not (bobp))))
485 (set-window-point window
(1- (window-point window
))))
487 (save-selected-window
488 (select-window window
)
490 ;; Adjust WINDOW to the nominally correct size (which may actually
491 ;; be slightly off because of variable height text, etc).
492 (unless (zerop delta
)
493 (enlarge-window delta
))
495 ;; Check if the last line is surely fully visible. If not,
496 ;; enlarge the window.
497 (let ((end (with-current-buffer buf
499 (goto-char (point-max))
500 (when (and (bolp) (not (bobp)))
501 ;; Don't include final newline
504 ;; If line-wrapping is turned off, test the
505 ;; beginning of the last line for visibility
506 ;; instead of the end, as the end of the line
507 ;; could be invisible by virtue of extending past
508 ;; the edge of the window.
511 (set-window-vscroll window
0)
512 (while (and (< desired-height max-height
)
513 (= desired-height
(window-height window
))
514 (not (pos-visible-in-window-p end window
)))
516 (setq desired-height
(1+ desired-height
)))))))
518 (defun shrink-window-if-larger-than-buffer (&optional window
)
519 "Shrink the WINDOW to be as small as possible to display its contents.
520 If WINDOW is omitted or nil, it defaults to the selected window.
521 Do not shrink to less than `window-min-height' lines.
522 Do nothing if the buffer contains more lines than the present window height,
523 or if some of the window's contents are scrolled out of view,
524 or if shrinking this window would also shrink another window.
525 or if the window is the only window of its frame.
526 Return non-nil if the window was shrunk."
529 (setq window
(selected-window)))
530 (let* ((frame (window-frame window
))
531 (mini (frame-parameter frame
'minibuffer
))
532 (edges (window-edges window
)))
533 (if (and (not (eq window
(frame-root-window frame
)))
534 (window-safely-shrinkable-p)
535 (pos-visible-in-window-p (point-min) window
)
536 (not (eq mini
'only
))
538 (let ((mini-window (minibuffer-window frame
)))
539 (or (null mini-window
)
540 (not (eq frame
(window-frame mini-window
)))
542 (nth 1 (window-edges mini-window
)))
544 (frame-parameter frame
'menu-bar-lines
))))))
545 (fit-window-to-buffer window
(window-height window
)))))
547 (defun kill-buffer-and-window ()
548 "Kill the current buffer and delete the selected window."
550 (let ((window-to-delete (selected-window))
551 (delete-window-hook (lambda ()
555 (add-hook 'kill-buffer-hook delete-window-hook t t
)
556 (if (kill-buffer (current-buffer))
557 ;; If `delete-window' failed before, we rerun it to regenerate
558 ;; the error so it can be seen in the minibuffer.
559 (when (eq (selected-window) window-to-delete
)
561 (remove-hook 'kill-buffer-hook delete-window-hook t
))))
563 (defun quit-window (&optional kill window
)
564 "Quit the current buffer. Bury it, and maybe delete the selected frame.
565 \(The frame is deleted if it is contains a dedicated window for the buffer.)
566 With a prefix argument, kill the buffer instead.
568 Noninteractively, if KILL is non-nil, then kill the current buffer,
571 If WINDOW is non-nil, it specifies a window; we delete that window,
572 and the buffer that is killed or buried is the one in that window."
574 (let ((buffer (window-buffer window
))
575 (frame (window-frame (or window
(selected-window))))
577 (save-selected-window
579 (select-window window
))
583 (save-selected-window
585 (select-window window
))
586 (or (window-minibuffer-p)
587 (window-dedicated-p (selected-window))
588 (switch-to-buffer (other-buffer))))
590 ;; Get rid of the frame, if it has just one dedicated window
591 ;; and other visible frames exist.
592 (and (or (window-minibuffer-p) (window-dedicated-p window
))
593 (delq frame
(visible-frame-list))
595 (if (and (eq default-minibuffer-frame frame
)
596 (= 1 (length (minibuffer-frame-list))))
599 (setq window-handled t
)))
601 ;; Deal with the buffer.
604 (bury-buffer buffer
))
606 ;; Maybe get rid of the window.
607 (and window
(not window-handled
) (not window-solitary
)
608 (delete-window window
))))
610 (defun handle-select-window (event)
611 "Handle select-window events."
613 (let ((window (posn-window (event-start event
))))
614 (if (and (window-live-p window
)
615 (or (not (window-minibuffer-p window
))
616 (minibuffer-window-active-p window
)))
617 (select-window window
))))
619 (define-key ctl-x-map
"2" 'split-window-vertically
)
620 (define-key ctl-x-map
"3" 'split-window-horizontally
)
621 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
622 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
623 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
624 (define-key ctl-x-map
"+" 'balance-windows
)
625 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
627 ;;; arch-tag: b508dfcc-c353-4c37-89fa-e773fe10cea9
628 ;;; window.el ends here