1 ;;; window.el --- GNU Emacs window commands aside from those written in C
3 ;; Copyright (C) 1985, 1989, 1992, 1993, 1994, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Window tree functions.
32 (defvar window-size-fixed nil
33 "*Non-nil in a buffer means windows displaying the buffer are fixed-size.
34 If the value is `height', then only the window's height is fixed.
35 If the value is `width', then only the window's width is fixed.
36 Any other non-nil value fixes both the width and the height.
37 Emacs won't change the size of any window displaying that buffer,
38 unless you explicitly change the size, or Emacs has no other choice.")
39 (make-variable-buffer-local 'window-size-fixed
)
41 (defmacro save-selected-window
(&rest body
)
42 "Execute BODY, then select the window that was selected before BODY.
43 The value returned is the value of the last form in BODY.
45 This macro saves and restores the current buffer, since otherwise
46 its normal operation could potentially make a different
47 buffer current. It does not alter the buffer list ordering.
49 This macro saves and restores the selected window, as well as
50 the selected window in each frame. If the previously selected
51 window of some frame is no longer live at the end of BODY, that
52 frame's selected window is left alone. If the selected window is
53 no longer live, then whatever window is selected at the end of
54 BODY remains selected."
55 `(let ((save-selected-window-window (selected-window))
56 ;; It is necessary to save all of these, because calling
57 ;; select-window changes frame-selected-window for whatever
58 ;; frame that window is in.
59 (save-selected-window-alist
60 (mapcar (lambda (frame) (list frame
(frame-selected-window frame
)))
65 (dolist (elt save-selected-window-alist
)
66 (and (frame-live-p (car elt
))
67 (window-live-p (cadr elt
))
68 (set-frame-selected-window (car elt
) (cadr elt
))))
69 (if (window-live-p save-selected-window-window
)
70 (select-window save-selected-window-window
))))))
72 (defun window-body-height (&optional window
)
73 "Return number of lines in window WINDOW for actual buffer text.
74 This does not include the mode line (if any) or the header line (if any)."
75 (or window
(setq window
(selected-window)))
76 (if (window-minibuffer-p window
)
77 (window-height window
)
78 (with-current-buffer (window-buffer window
)
79 (max 1 (- (window-height window
)
80 (if mode-line-format
1 0)
81 (if header-line-format
1 0))))))
83 (defun one-window-p (&optional nomini all-frames
)
84 "Return non-nil if the selected window is the only window.
85 Optional arg NOMINI non-nil means don't count the minibuffer
86 even if it is active. Otherwise, the minibuffer is counted
89 The optional arg ALL-FRAMES t means count windows on all frames.
90 If it is `visible', count windows on all visible frames.
91 ALL-FRAMES nil or omitted means count only the selected frame,
92 plus the minibuffer it uses (which may be on another frame).
93 ALL-FRAMES 0 means count all windows in all visible or iconified frames.
94 If ALL-FRAMES is anything else, count only the selected frame."
95 (let ((base-window (selected-window)))
96 (if (and nomini
(eq base-window
(minibuffer-window)))
97 (setq base-window
(next-window base-window
)))
99 (next-window base-window
(if nomini
'arg
) all-frames
))))
101 (defun window-current-scroll-bars (&optional window
)
102 "Return the current scroll-bar settings in window WINDOW.
103 Value is a cons (VERTICAL . HORIZONTAL) where VERTICAL specifies the
104 current location of the vertical scroll-bars (left, right, or nil),
105 and HORIZONTAL specifies the current location of the horizontal scroll
106 bars (top, bottom, or nil)."
107 (let ((vert (nth 2 (window-scroll-bars window
)))
109 (when (or (eq vert t
) (eq hor t
))
110 (let ((fcsb (frame-current-scroll-bars
111 (window-frame (or window
(selected-window))))))
113 (setq vert
(car fcsb
)))
115 (setq hor
(cdr fcsb
)))))
118 (defun walk-windows (proc &optional minibuf all-frames
)
119 "Cycle through all visible windows, calling PROC for each one.
120 PROC is called with a window as argument.
122 Optional second arg MINIBUF t means count the minibuffer window even
123 if not active. MINIBUF nil or omitted means count the minibuffer iff
124 it is active. MINIBUF neither t nor nil means not to count the
125 minibuffer even if it is active.
127 Several frames may share a single minibuffer; if the minibuffer
128 counts, all windows on all frames that share that minibuffer count
129 too. Therefore, if you are using a separate minibuffer frame
130 and the minibuffer is active and MINIBUF says it counts,
131 `walk-windows' includes the windows in the frame from which you
132 entered the minibuffer, as well as the minibuffer window.
134 ALL-FRAMES is the optional third argument.
135 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
136 ALL-FRAMES = `visible' means include windows on all visible frames.
137 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
138 ALL-FRAMES = t means include windows on all frames including invisible frames.
139 If ALL-FRAMES is a frame, it means include windows on that frame.
140 Anything else means restrict to the selected frame."
141 ;; If we start from the minibuffer window, don't fail to come back to it.
142 (if (window-minibuffer-p (selected-window))
144 (save-selected-window
145 (if (framep all-frames
)
146 (select-window (frame-first-window all-frames
)))
147 (let* (walk-windows-already-seen
148 (walk-windows-current (selected-window)))
150 (setq walk-windows-current
151 (next-window walk-windows-current minibuf all-frames
))
152 (not (memq walk-windows-current walk-windows-already-seen
)))
153 (setq walk-windows-already-seen
154 (cons walk-windows-current walk-windows-already-seen
))
155 (funcall proc walk-windows-current
)))))
157 (defun get-window-with-predicate (predicate &optional minibuf
159 "Return a window satisfying PREDICATE.
161 This function cycles through all visible windows using `walk-windows',
162 calling PREDICATE on each one. PREDICATE is called with a window as
163 argument. The first window for which PREDICATE returns a non-nil
164 value is returned. If no window satisfies PREDICATE, DEFAULT is
167 Optional second arg MINIBUF t means count the minibuffer window even
168 if not active. MINIBUF nil or omitted means count the minibuffer iff
169 it is active. MINIBUF neither t nor nil means not to count the
170 minibuffer even if it is active.
172 Several frames may share a single minibuffer; if the minibuffer
173 counts, all windows on all frames that share that minibuffer count
174 too. Therefore, if you are using a separate minibuffer frame
175 and the minibuffer is active and MINIBUF says it counts,
176 `walk-windows' includes the windows in the frame from which you
177 entered the minibuffer, as well as the minibuffer window.
179 ALL-FRAMES is the optional third argument.
180 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
181 ALL-FRAMES = `visible' means include windows on all visible frames.
182 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
183 ALL-FRAMES = t means include windows on all frames including invisible frames.
184 If ALL-FRAMES is a frame, it means include windows on that frame.
185 Anything else means restrict to the selected frame."
187 (walk-windows #'(lambda (window)
188 (when (funcall predicate window
)
189 (throw 'found window
)))
193 (defalias 'some-window
'get-window-with-predicate
)
195 ;; This should probably be written in C (i.e., without using `walk-windows').
196 (defun get-buffer-window-list (buffer &optional minibuf frame
)
197 "Return list of all windows displaying BUFFER, or nil if none.
198 BUFFER can be a buffer or a buffer name.
199 See `walk-windows' for the meaning of MINIBUF and FRAME."
200 (let ((buffer (if (bufferp buffer
) buffer
(get-buffer buffer
))) windows
)
201 (walk-windows (function (lambda (window)
202 (if (eq (window-buffer window
) buffer
)
203 (setq windows
(cons window windows
)))))
207 (defun minibuffer-window-active-p (window)
208 "Return t if WINDOW (a minibuffer window) is now active."
209 (eq window
(active-minibuffer-window)))
211 (defun count-windows (&optional minibuf
)
212 "Return the number of visible windows.
213 This counts the windows in the selected frame and (if the minibuffer is
214 to be counted) its minibuffer frame (if that's not the same frame).
215 The optional arg MINIBUF non-nil means count the minibuffer
216 even if it is inactive."
218 (walk-windows (function (lambda (w)
219 (setq count
(+ count
1))))
223 (defun window-safely-shrinkable-p (&optional window
)
224 "Non-nil if the WINDOW can be shrunk without shrinking other windows.
225 If WINDOW is nil or omitted, it defaults to the currently selected window."
226 (with-selected-window (or window
(selected-window))
227 (let ((edges (window-edges)))
228 (or (= (nth 2 edges
) (nth 2 (window-edges (previous-window))))
229 (= (nth 0 edges
) (nth 0 (window-edges (next-window))))))))
232 (defun balance-windows ()
233 "Make all visible windows the same height (approximately)."
235 (let ((count -
1) levels newsizes level-size
236 ;; Don't count the lines that are above the uppermost windows.
237 ;; (These are the menu bar lines, if any.)
238 (mbl (nth 1 (window-edges (frame-first-window (selected-frame)))))
239 (last-window (previous-window (frame-first-window (selected-frame))))
240 ;; Don't count the lines that are past the lowest main window.
242 ;; Bottom edge of last window determines what size we have to work with.
244 (+ (window-height last-window
)
245 (nth 1 (window-edges last-window
))))
247 ;; Find all the different vpos's at which windows start,
248 ;; then count them. But ignore levels that differ by only 1.
249 (let (tops (prev-top -
2))
250 (walk-windows (function (lambda (w)
251 (setq tops
(cons (nth 1 (window-edges w
))
254 (setq tops
(sort tops
'<))
256 (if (> (car tops
) (1+ prev-top
))
257 (setq prev-top
(car tops
)
259 (setq levels
(cons (cons (car tops
) count
) levels
))
260 (setq tops
(cdr tops
)))
261 (setq count
(1+ count
)))
262 ;; Subdivide the frame into desired number of vertical levels.
263 (setq level-size
(/ (- total mbl
) count
))
264 (save-selected-window
265 ;; Set up NEWSIZES to map windows to their desired sizes.
266 ;; If a window ends at the bottom level, don't include
267 ;; it in NEWSIZES. Those windows get the right sizes
268 ;; by adjusting the ones above them.
269 (walk-windows (function
271 (let ((newtop (cdr (assq (nth 1 (window-edges w
))
273 (newbot (cdr (assq (+ (window-height w
)
274 (nth 1 (window-edges w
)))
278 (cons (cons w
(* level-size
(- newbot newtop
)))
281 ;; Make walk-windows start with the topmost window.
282 (select-window (previous-window (frame-first-window (selected-frame))))
283 (let (done (count 0))
284 ;; Give each window its precomputed size, or at least try.
285 ;; Keep trying until they all get the intended sizes,
286 ;; but not more than 3 times (to prevent infinite loop).
287 (while (and (not done
) (< count
3))
289 (setq count
(1+ count
))
290 (walk-windows (function (lambda (w)
292 (let ((newsize (cdr (assq w newsizes
))))
294 (enlarge-window (- newsize
297 (unless (= (window-height) newsize
)
301 ;; I think this should be the default; I think people will prefer it--rms.
302 (defcustom split-window-keep-point t
303 "*If non-nil, \\[split-window-vertically] keeps the original point \
305 This is often more convenient for editing.
306 If nil, adjust point in each of the two windows to minimize redisplay.
307 This is convenient on slow terminals, but point can move strangely.
309 This option applies only to `split-window-vertically' and
310 functions that call it. `split-window' always keeps the original
311 point in both children,"
315 (defun split-window-vertically (&optional arg
)
316 "Split current window into two windows, one above the other.
317 The uppermost window gets ARG lines and the other gets the rest.
318 Negative ARG means select the size of the lowermost window instead.
319 With no argument, split equally or close to it.
320 Both windows display the same buffer now current.
322 If the variable `split-window-keep-point' is non-nil, both new windows
323 will get the same value of point as the current window. This is often
324 more convenient for editing. The upper window is the selected window.
326 Otherwise, we choose window starts so as to minimize the amount of
327 redisplay; this is convenient on slow terminals. The new selected
328 window is the one that the current value of point appears in. The
329 value of point can change if the text around point is hidden by the
332 Regardless of the value of `split-window-keep-point', the upper
333 window is the original one and the return value is the new, lower
336 (let ((old-w (selected-window))
338 (size (and arg
(prefix-numeric-value arg
)))
340 new-w bottom switch moved
)
341 (and size
(< size
0) (setq size
(+ (window-height) size
)))
342 (setq new-w
(split-window nil size
))
343 (or split-window-keep-point
346 (set-buffer (window-buffer))
347 (goto-char (window-start))
348 (setq moved
(vertical-motion (window-height)))
349 (set-window-start new-w
(point))
350 (if (> (point) (window-point new-w
))
351 (set-window-point new-w
(point)))
352 (and (= moved
(window-height))
354 (setq window-full-p t
)
355 (vertical-motion -
1)))
356 (setq bottom
(point)))
359 (set-window-point old-w
(1- bottom
)))
361 (<= (window-start new-w
) old-point
)
363 (set-window-point new-w old-point
)
364 (select-window new-w
)))))
365 (split-window-save-restore-data new-w old-w
)))
367 ;; This is to avoid compiler warnings.
368 (defvar view-return-to-alist
)
370 (defun split-window-save-restore-data (new-w old-w
)
371 (with-current-buffer (window-buffer)
373 (let ((old-info (assq old-w view-return-to-alist
)))
375 (push (cons new-w
(cons (car (cdr old-info
)) t
))
376 view-return-to-alist
))))
379 (defun split-window-horizontally (&optional arg
)
380 "Split current window into two windows side by side.
381 This window becomes the leftmost of the two, and gets ARG columns.
382 Negative ARG means select the size of the rightmost window instead.
383 The argument includes the width of the window's scroll bar; if there
384 are no scroll bars, it includes the width of the divider column
385 to the window's right, if any. No ARG means split equally.
387 The original, leftmost window remains selected.
388 The return value is the new, rightmost window."
390 (let ((old-w (selected-window))
391 (size (and arg
(prefix-numeric-value arg
))))
393 (setq size
(+ (window-width) size
)))
394 (split-window-save-restore-data (split-window nil size t
) old-w
)))
397 (defun set-window-text-height (window height
)
398 "Sets the height in lines of the text display area of WINDOW to HEIGHT.
399 This doesn't include the mode-line (or header-line if any) or any
400 partial-height lines in the text display area.
402 If WINDOW is nil, the selected window is used.
404 Note that the current implementation of this function cannot always set
405 the height exactly, but attempts to be conservative, by allocating more
406 lines than are actually needed in the case where some error may be present."
407 (let ((delta (- height
(window-text-height window
))))
408 (unless (zerop delta
)
409 (let ((window-min-height 1))
410 (if (and window
(not (eq window
(selected-window))))
411 (save-selected-window
412 (select-window window
)
413 (enlarge-window delta
))
414 (enlarge-window delta
))))))
417 (defun enlarge-window-horizontally (arg)
418 "Make current window ARG columns wider."
420 (enlarge-window arg t
))
422 (defun shrink-window-horizontally (arg)
423 "Make current window ARG columns narrower."
425 (shrink-window arg t
))
427 (defun window-buffer-height (window)
428 "Return the height (in screen lines) of the buffer that WINDOW is displaying."
429 (with-current-buffer (window-buffer window
)
431 (count-screen-lines (point-min) (point-max)
432 ;; If buffer ends with a newline, ignore it when
433 ;; counting height unless point is after it.
437 (defun count-screen-lines (&optional beg end count-final-newline window
)
438 "Return the number of screen lines in the region.
439 The number of screen lines may be different from the number of actual lines,
440 due to line breaking, display table, etc.
442 Optional arguments BEG and END default to `point-min' and `point-max'
445 If region ends with a newline, ignore it unless optional third argument
446 COUNT-FINAL-NEWLINE is non-nil.
448 The optional fourth argument WINDOW specifies the window used for obtaining
449 parameters such as width, horizontal scrolling, and so on. The default is
450 to use the selected window's parameters.
452 Like `vertical-motion', `count-screen-lines' always uses the current buffer,
453 regardless of which buffer is displayed in WINDOW. This makes possible to use
454 `count-screen-lines' in any buffer, whether or not it is currently displayed
457 (setq beg
(point-min)))
459 (setq end
(point-max)))
465 (narrow-to-region (min beg end
)
466 (if (and (not count-final-newline
)
467 (= ?
\n (char-before (max beg end
))))
470 (goto-char (point-min))
471 (1+ (vertical-motion (buffer-size) window
))))))
473 (defun fit-window-to-buffer (&optional window max-height min-height
)
474 "Make WINDOW the right size to display its contents exactly.
475 If WINDOW is omitted or nil, it defaults to the selected window.
476 If the optional argument MAX-HEIGHT is supplied, it is the maximum height
477 the window is allowed to be, defaulting to the frame height.
478 If the optional argument MIN-HEIGHT is supplied, it is the minimum
479 height the window is allowed to be, defaulting to `window-min-height'.
481 The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or
486 (setq window
(selected-window)))
487 (when (null max-height
)
488 (setq max-height
(frame-height (window-frame window
))))
491 ;; Buffer that is displayed in WINDOW
492 (window-buffer window
))
494 ;; The current height of WINDOW
495 (window-height window
))
497 ;; The height necessary to show the buffer displayed by WINDOW
498 ;; (`count-screen-lines' always works on the current buffer).
499 (with-current-buffer buf
500 (+ (count-screen-lines)
501 ;; If the buffer is empty, (count-screen-lines) is
502 ;; zero. But, even in that case, we need one text line
504 (if (= (point-min) (point-max))
506 ;; For non-minibuffers, count the mode-line, if any
507 (if (and (not (window-minibuffer-p window
))
510 ;; Count the header-line, if any
511 (if header-line-format
1 0))))
513 ;; Calculate how much the window height has to change to show
514 ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
515 (- (max (min desired-height max-height
)
516 (or min-height window-min-height
))
518 ;; We do our own height checking, so avoid any restrictions due to
519 ;; window-min-height.
520 (window-min-height 1))
522 ;; Don't try to redisplay with the cursor at the end
523 ;; on its own line--that would force a scroll and spoil things.
524 (when (with-current-buffer buf
525 (and (eobp) (bolp) (not (bobp))))
526 (set-window-point window
(1- (window-point window
))))
528 (save-selected-window
529 (select-window window
)
531 ;; Adjust WINDOW to the nominally correct size (which may actually
532 ;; be slightly off because of variable height text, etc).
533 (unless (zerop delta
)
534 (enlarge-window delta
))
536 ;; Check if the last line is surely fully visible. If not,
537 ;; enlarge the window.
538 (let ((end (with-current-buffer buf
540 (goto-char (point-max))
541 (when (and (bolp) (not (bobp)))
542 ;; Don't include final newline
545 ;; If line-wrapping is turned off, test the
546 ;; beginning of the last line for visibility
547 ;; instead of the end, as the end of the line
548 ;; could be invisible by virtue of extending past
549 ;; the edge of the window.
552 (set-window-vscroll window
0)
553 (while (and (< desired-height max-height
)
554 (= desired-height
(window-height window
))
555 (not (pos-visible-in-window-p end window
)))
557 (setq desired-height
(1+ desired-height
)))))))
559 (defun shrink-window-if-larger-than-buffer (&optional window
)
560 "Shrink the WINDOW to be as small as possible to display its contents.
561 If WINDOW is omitted or nil, it defaults to the selected window.
562 Do not shrink to less than `window-min-height' lines.
563 Do nothing if the buffer contains more lines than the present window height,
564 or if some of the window's contents are scrolled out of view,
565 or if shrinking this window would also shrink another window,
566 or if the window is the only window of its frame."
569 (setq window
(selected-window)))
570 (let* ((frame (window-frame window
))
571 (mini (frame-parameter frame
'minibuffer
))
572 (edges (window-edges window
)))
573 (if (and (not (eq window
(frame-root-window frame
)))
574 (window-safely-shrinkable-p)
575 (pos-visible-in-window-p (point-min) window
)
576 (not (eq mini
'only
))
578 (let ((mini-window (minibuffer-window frame
)))
579 (or (null mini-window
)
580 (not (eq frame
(window-frame mini-window
)))
582 (nth 1 (window-edges mini-window
)))
584 (frame-parameter frame
'menu-bar-lines
))))))
585 (fit-window-to-buffer window
(window-height window
)))))
587 (defun kill-buffer-and-window ()
588 "Kill the current buffer and delete the selected window."
590 (let ((window-to-delete (selected-window))
591 (delete-window-hook (lambda ()
595 (add-hook 'kill-buffer-hook delete-window-hook t t
)
596 (if (kill-buffer (current-buffer))
597 ;; If `delete-window' failed before, we rerun it to regenerate
598 ;; the error so it can be seen in the minibuffer.
599 (when (eq (selected-window) window-to-delete
)
601 (remove-hook 'kill-buffer-hook delete-window-hook t
))))
603 (defun quit-window (&optional kill window
)
604 "Quit the current buffer. Bury it, and maybe delete the selected frame.
605 \(The frame is deleted if it contains a dedicated window for the buffer.)
606 With a prefix argument, kill the buffer instead.
608 Noninteractively, if KILL is non-nil, then kill the current buffer,
611 If WINDOW is non-nil, it specifies a window; we delete that window,
612 and the buffer that is killed or buried is the one in that window."
614 (let ((buffer (window-buffer window
))
615 (frame (window-frame (or window
(selected-window))))
617 (save-selected-window
619 (select-window window
))
623 (save-selected-window
625 (select-window window
))
626 (or (window-minibuffer-p)
627 (window-dedicated-p (selected-window))
628 (switch-to-buffer (other-buffer))))
630 ;; Get rid of the frame, if it has just one dedicated window
631 ;; and other visible frames exist.
632 (and (or (window-minibuffer-p) (window-dedicated-p window
))
633 (delq frame
(visible-frame-list))
635 (if (and (eq default-minibuffer-frame frame
)
636 (= 1 (length (minibuffer-frame-list))))
639 (setq window-handled t
)))
641 ;; Deal with the buffer.
644 (bury-buffer buffer
))
646 ;; Maybe get rid of the window.
647 (and window
(not window-handled
) (not window-solitary
)
648 (delete-window window
))))
650 (defun handle-select-window (event)
651 "Handle select-window events."
653 (let ((window (posn-window (event-start event
))))
654 (if (and (window-live-p window
)
655 ;; Don't switch if we're currently in the minibuffer.
656 ;; This tries to work around problems where the minibuffer gets
657 ;; unselected unexpectedly, and where you then have to move
658 ;; your mouse all the way down to the minibuffer to select it.
659 (not (window-minibuffer-p (selected-window)))
660 ;; Don't switch to a minibuffer window unless it's active.
661 (or (not (window-minibuffer-p window
))
662 (minibuffer-window-active-p window
)))
663 (select-window window
))))
665 (define-key ctl-x-map
"2" 'split-window-vertically
)
666 (define-key ctl-x-map
"3" 'split-window-horizontally
)
667 (define-key ctl-x-map
"}" 'enlarge-window-horizontally
)
668 (define-key ctl-x-map
"{" 'shrink-window-horizontally
)
669 (define-key ctl-x-map
"-" 'shrink-window-if-larger-than-buffer
)
670 (define-key ctl-x-map
"+" 'balance-windows
)
671 (define-key ctl-x-4-map
"0" 'kill-buffer-and-window
)
673 ;; arch-tag: b508dfcc-c353-4c37-89fa-e773fe10cea9
674 ;;; window.el ends here