2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999
4 @c Free Software Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @setfilename ../info/windows
7 @node Windows, Frames, Buffers, Top
10 This chapter describes most of the functions and variables related to
11 Emacs windows. See @ref{Display}, for information on how text is
15 * Basic Windows:: Basic information on using windows.
16 * Splitting Windows:: Splitting one window into two windows.
17 * Deleting Windows:: Deleting a window gives its space to other windows.
18 * Selecting Windows:: The selected window is the one that you edit in.
19 * Cyclic Window Ordering:: Moving around the existing windows.
20 * Buffers and Windows:: Each window displays the contents of a buffer.
21 * Displaying Buffers:: Higher-lever functions for displaying a buffer
22 and choosing a window for it.
23 * Choosing Window:: How to choose a window for displaying a buffer.
24 * Window Point:: Each window has its own location of point.
25 * Window Start:: The display-start position controls which text
26 is on-screen in the window.
27 * Textual Scrolling:: Moving text up and down through the window.
28 * Vertical Scrolling:: Moving the contents up and down on the window.
29 * Horizontal Scrolling:: Moving the contents sideways on the window.
30 * Size of Window:: Accessing the size of a window.
31 * Resizing Windows:: Changing the size of a window.
32 * Coordinates and Windows:: Converting coordinates to windows.
33 * Window Configurations:: Saving and restoring the state of the screen.
34 * Window Hooks:: Hooks for scrolling, window size changes,
35 redisplay going past a certain point,
36 or window configuration changes.
40 @section Basic Concepts of Emacs Windows
42 @cindex selected window
44 A @dfn{window} in Emacs is the physical area of the screen in which a
45 buffer is displayed. The term is also used to refer to a Lisp object that
46 represents that screen area in Emacs Lisp. It should be
47 clear from the context which is meant.
49 Emacs groups windows into frames. A frame represents an area of
50 screen available for Emacs to use. Each frame always contains at least
51 one window, but you can subdivide it vertically or horizontally into
52 multiple nonoverlapping Emacs windows.
54 In each frame, at any time, one and only one window is designated as
55 @dfn{selected within the frame}. The frame's cursor appears in that
56 window. At any time, one frame is the selected frame; and the window
57 selected within that frame is @dfn{the selected window}. The selected
58 window's buffer is usually the current buffer (except when
59 @code{set-buffer} has been used). @xref{Current Buffer}.
61 For practical purposes, a window exists only while it is displayed in
62 a frame. Once removed from the frame, the window is effectively deleted
63 and should not be used, @emph{even though there may still be references
64 to it} from other Lisp objects. Restoring a saved window configuration
65 is the only way for a window no longer on the screen to come back to
66 life. (@xref{Deleting Windows}.)
68 Each window has the following attributes:
81 window edges with respect to the screen or frame
84 the buffer it displays
87 position within the buffer at the upper left of the window
90 amount of horizontal scrolling, in columns
99 how recently the window was selected
102 @cindex multiple windows
103 Users create multiple windows so they can look at several buffers at
104 once. Lisp libraries use multiple windows for a variety of reasons, but
105 most often to display related information. In Rmail, for example, you
106 can move through a summary buffer in one window while the other window
107 shows messages one at a time as they are reached.
109 The meaning of ``window'' in Emacs is similar to what it means in the
110 context of general-purpose window systems such as X, but not identical.
111 The X Window System places X windows on the screen; Emacs uses one or
112 more X windows as frames, and subdivides them into
113 Emacs windows. When you use Emacs on a character-only terminal, Emacs
114 treats the whole terminal screen as one frame.
116 @cindex terminal screen
117 @cindex screen of terminal
118 @cindex tiled windows
119 Most window systems support arbitrarily located overlapping windows.
120 In contrast, Emacs windows are @dfn{tiled}; they never overlap, and
121 together they fill the whole screen or frame. Because of the way in
122 which Emacs creates new windows and resizes them, not all conceivable
123 tilings of windows on an Emacs frame are actually possible.
124 @xref{Splitting Windows}, and @ref{Size of Window}.
126 @xref{Display}, for information on how the contents of the
127 window's buffer are displayed in the window.
129 @defun windowp object
130 This function returns @code{t} if @var{object} is a window.
133 @node Splitting Windows
134 @section Splitting Windows
135 @cindex splitting windows
136 @cindex window splitting
138 The functions described here are the primitives used to split a window
139 into two windows. Two higher level functions sometimes split a window,
140 but not always: @code{pop-to-buffer} and @code{display-buffer}
141 (@pxref{Displaying Buffers}).
143 The functions described here do not accept a buffer as an argument.
144 The two ``halves'' of the split window initially display the same buffer
145 previously visible in the window that was split.
147 @deffn Command split-window &optional window size horizontal
148 This function splits @var{window} into two windows. The original
149 window @var{window} remains the selected window, but occupies only
150 part of its former screen area. The rest is occupied by a newly created
151 window which is returned as the value of this function.
153 If @var{horizontal} is non-@code{nil}, then @var{window} splits into
154 two side by side windows. The original window @var{window} keeps the
155 leftmost @var{size} columns, and gives the rest of the columns to the
156 new window. Otherwise, it splits into windows one above the other, and
157 @var{window} keeps the upper @var{size} lines and gives the rest of the
158 lines to the new window. The original window is therefore the
159 left-hand or upper of the two, and the new window is the right-hand or
162 If @var{window} is omitted or @code{nil}, then the selected window is
163 split. If @var{size} is omitted or @code{nil}, then @var{window} is
164 divided evenly into two parts. (If there is an odd line, it is
165 allocated to the new window.) When @code{split-window} is called
166 interactively, all its arguments are @code{nil}.
168 If splitting would result in making a window that is smaller than
169 @code{window-min-height} or @code{window-min-width}, the function
170 signals an error and does not split the window at all.
172 The following example starts with one window on a screen that is 50
173 lines high by 80 columns wide; then the window is split.
177 (setq w (selected-window))
178 @result{} #<window 8 on windows.texi>
179 (window-edges) ; @r{Edges in order:}
180 @result{} (0 0 80 50) ; @r{left--top--right--bottom}
184 ;; @r{Returns window created}
185 (setq w2 (split-window w 15))
186 @result{} #<window 28 on windows.texi>
190 @result{} (0 15 80 50) ; @r{Bottom window;}
195 @result{} (0 0 80 15) ; @r{Top window}
199 The screen looks like this:
215 Next, the top window is split horizontally:
219 (setq w3 (split-window w 35 t))
220 @result{} #<window 32 on windows.texi>
224 @result{} (35 0 80 15) ; @r{Left edge at column 35}
228 @result{} (0 0 35 15) ; @r{Right edge at column 35}
232 @result{} (0 15 80 50) ; @r{Bottom window unchanged}
237 Now, the screen looks like this:
254 Normally, Emacs indicates the border between two side-by-side windows
255 with a scroll bar (@pxref{Window Frame Parameters,Scroll Bars}) or @samp{|}
256 characters. The display table can specify alternative border
257 characters; see @ref{Display Tables}.
260 @deffn Command split-window-vertically &optional size
261 This function splits the selected window into two windows, one above the
262 other, leaving the upper of the two windows selected, with @var{size}
263 lines. (If @var{size} is negative, then the lower of the two windows
264 gets @minus{} @var{size} lines and the upper window gets the rest, but
265 the upper window is still the one selected.)
268 @deffn Command split-window-horizontally &optional size
269 This function splits the selected window into two windows
270 side-by-side, leaving the selected window with @var{size} columns.
272 This function is basically an interface to @code{split-window}.
273 You could define a simplified version of the function like this:
277 (defun split-window-horizontally (&optional arg)
278 "Split selected window into two windows, side by side..."
282 (let ((size (and arg (prefix-numeric-value arg))))
284 (setq size (+ (window-width) size)))
285 (split-window nil size t)))
290 @defun one-window-p &optional no-mini all-frames
291 This function returns non-@code{nil} if there is only one window. The
292 argument @var{no-mini}, if non-@code{nil}, means don't count the
293 minibuffer even if it is active; otherwise, the minibuffer window is
294 included, if active, in the total number of windows, which is compared
297 The argument @var{all-frames} specifies which frames to consider. Here
298 are the possible values and their meanings:
302 Count the windows in the selected frame, plus the minibuffer used
303 by that frame even if it lies in some other frame.
306 Count all windows in all existing frames.
309 Count all windows in all visible frames.
312 Count all windows in all visible or iconified frames.
315 Count precisely the windows in the selected frame, and no others.
319 @node Deleting Windows
320 @section Deleting Windows
321 @cindex deleting windows
323 A window remains visible on its frame unless you @dfn{delete} it by
324 calling certain functions that delete windows. A deleted window cannot
325 appear on the screen, but continues to exist as a Lisp object until
326 there are no references to it. There is no way to cancel the deletion
327 of a window aside from restoring a saved window configuration
328 (@pxref{Window Configurations}). Restoring a window configuration also
329 deletes any windows that aren't part of that configuration.
331 When you delete a window, the space it took up is given to one
335 @defun window-live-p window
336 This function returns @code{nil} if @var{window} is deleted, and
339 @strong{Warning:} Erroneous information or fatal errors may result from
340 using a deleted window as if it were live.
343 @deffn Command delete-window &optional window
344 This function removes @var{window} from display, and returns @code{nil}.
345 If @var{window} is omitted, then the selected window is deleted. An
346 error is signaled if there is only one window when @code{delete-window}
350 @deffn Command delete-other-windows &optional window
351 This function makes @var{window} the only window on its frame, by
352 deleting the other windows in that frame. If @var{window} is omitted or
353 @code{nil}, then the selected window is used by default.
355 The return value is @code{nil}.
358 @deffn Command delete-windows-on buffer &optional frame
359 This function deletes all windows showing @var{buffer}. If there are
360 no windows showing @var{buffer}, it does nothing.
362 @code{delete-windows-on} operates frame by frame. If a frame has
363 several windows showing different buffers, then those showing
364 @var{buffer} are removed, and the others expand to fill the space. If
365 all windows in some frame are showing @var{buffer} (including the case
366 where there is only one window), then the frame reverts to having a
367 single window showing another buffer chosen with @code{other-buffer}.
368 @xref{The Buffer List}.
370 The argument @var{frame} controls which frames to operate on. This
371 function does not use it in quite the same way as the other functions
372 which scan all windows; specifically, the values @code{t} and @code{nil}
373 have the opposite of their meanings in other functions. Here are the
378 If it is @code{nil}, operate on all frames.
380 If it is @code{t}, operate on the selected frame.
382 If it is @code{visible}, operate on all visible frames.
384 If it is 0, operate on all visible or iconified frames.
386 If it is a frame, operate on that frame.
389 This function always returns @code{nil}.
392 @node Selecting Windows
393 @section Selecting Windows
394 @cindex selecting windows
396 When a window is selected, the buffer in the window becomes the current
397 buffer, and the cursor will appear in it.
399 @defun selected-window
400 This function returns the selected window. This is the window in
401 which the cursor appears and to which many commands apply.
404 @defun select-window window
405 This function makes @var{window} the selected window. The cursor then
406 appears in @var{window} (on redisplay). The buffer being displayed in
407 @var{window} is immediately designated the current buffer.
409 The return value is @var{window}.
413 (setq w (next-window))
415 @result{} #<window 65 on windows.texi>
420 @defmac save-selected-window forms@dots{}
421 This macro records the selected window, executes @var{forms}
422 in sequence, then restores the earlier selected window.
424 This macro does not save or restore anything about the sizes, arrangement
425 or contents of windows; therefore, if the @var{forms} change them,
428 Each frame, at any time, has a window selected within the frame. This
429 macro saves only @emph{the} selected window; it does not save anything
430 about other frames. If the @var{forms} select some other frame and
431 alter the window selected within it, the change persists.
434 @cindex finding windows
435 The following functions choose one of the windows on the screen,
436 offering various criteria for the choice.
438 @defun get-lru-window &optional frame
439 This function returns the window least recently ``used'' (that is,
440 selected). The selected window is always the most recently used window.
442 The selected window can be the least recently used window if it is the
443 only window. A newly created window becomes the least recently used
444 window until it is selected. A minibuffer window is never a candidate.
446 The argument @var{frame} controls which windows are considered.
450 If it is @code{nil}, consider windows on the selected frame.
452 If it is @code{t}, consider windows on all frames.
454 If it is @code{visible}, consider windows on all visible frames.
456 If it is 0, consider windows on all visible or iconified frames.
458 If it is a frame, consider windows on that frame.
462 @defun get-largest-window &optional frame
463 This function returns the window with the largest area (height times
464 width). If there are no side-by-side windows, then this is the window
465 with the most lines. A minibuffer window is never a candidate.
467 If there are two windows of the same size, then the function returns
468 the window that is first in the cyclic ordering of windows (see
469 following section), starting from the selected window.
471 The argument @var{frame} controls which set of windows to
472 consider. See @code{get-lru-window}, above.
475 @cindex window that satisfies a predicate
476 @cindex conditional selection of windows
477 @defun get-window-with-predicate predicate &optional minibuf all-frames default
478 This function returns a window satisfying @var{predicate}. It cycles
479 through all visible windows using @code{walk-windows} (@pxref{Cyclic
480 Window Ordering}), calling @var{predicate} on each one one of them
481 with that window as its argument. The function returns the first
482 window for which @var{predicate} returns a non-@code{nil} value; if
483 that never happens, it returns @var{default}.
485 The optional arguments @var{minibuf} and @var{all-frames} specify the
486 set of windows to include in the scan. See the description of
487 @code{next-window} in @ref{Cyclic Window Ordering}, for details.
490 @node Cyclic Window Ordering
491 @comment node-name, next, previous, up
492 @section Cyclic Ordering of Windows
493 @cindex cyclic ordering of windows
494 @cindex ordering of windows, cyclic
495 @cindex window ordering, cyclic
497 When you use the command @kbd{C-x o} (@code{other-window}) to select
498 the next window, it moves through all the windows on the screen in a
499 specific cyclic order. For any given configuration of windows, this
500 order never varies. It is called the @dfn{cyclic ordering of windows}.
502 This ordering generally goes from top to bottom, and from left to
503 right. But it may go down first or go right first, depending on the
504 order in which the windows were split.
506 If the first split was vertical (into windows one above each other),
507 and then the subwindows were split horizontally, then the ordering is
508 left to right in the top of the frame, and then left to right in the
509 next lower part of the frame, and so on. If the first split was
510 horizontal, the ordering is top to bottom in the left part, and so on.
511 In general, within each set of siblings at any level in the window tree,
512 the order is left to right, or top to bottom.
514 @defun next-window &optional window minibuf all-frames
515 @cindex minibuffer window
516 This function returns the window following @var{window} in the cyclic
517 ordering of windows. This is the window that @kbd{C-x o} would select
518 if typed when @var{window} is selected. If @var{window} is the only
519 window visible, then this function returns @var{window}. If omitted,
520 @var{window} defaults to the selected window.
522 The value of the argument @var{minibuf} determines whether the
523 minibuffer is included in the window order. Normally, when
524 @var{minibuf} is @code{nil}, the minibuffer is included if it is
525 currently active; this is the behavior of @kbd{C-x o}. (The minibuffer
526 window is active while the minibuffer is in use. @xref{Minibuffers}.)
528 If @var{minibuf} is @code{t}, then the cyclic ordering includes the
529 minibuffer window even if it is not active.
531 If @var{minibuf} is neither @code{t} nor @code{nil}, then the minibuffer
532 window is not included even if it is active.
534 The argument @var{all-frames} specifies which frames to consider. Here
535 are the possible values and their meanings:
539 Consider all the windows in @var{window}'s frame, plus the minibuffer
540 used by that frame even if it lies in some other frame.
543 Consider all windows in all existing frames.
546 Consider all windows in all visible frames. (To get useful results, you
547 must ensure @var{window} is in a visible frame.)
550 Consider all windows in all visible or iconified frames.
553 Consider precisely the windows in @var{window}'s frame, and no others.
556 This example assumes there are two windows, both displaying the
557 buffer @samp{windows.texi}:
562 @result{} #<window 56 on windows.texi>
565 (next-window (selected-window))
566 @result{} #<window 52 on windows.texi>
569 (next-window (next-window (selected-window)))
570 @result{} #<window 56 on windows.texi>
575 @defun previous-window &optional window minibuf all-frames
576 This function returns the window preceding @var{window} in the cyclic
577 ordering of windows. The other arguments specify which windows to
578 include in the cycle, as in @code{next-window}.
581 @deffn Command other-window count &optional all-frames
582 This function selects the @var{count}th following window in the cyclic
583 order. If count is negative, then it moves back @minus{}@var{count}
584 windows in the cycle, rather than forward. It returns @code{nil}.
586 The argument @var{all-frames} has the same meaning as in
587 @code{next-window}, but the @var{minibuf} argument of @code{next-window}
588 is always effectively @code{nil}.
590 In an interactive call, @var{count} is the numeric prefix argument.
594 @defun walk-windows proc &optional minibuf all-frames
595 This function cycles through all windows, calling @code{proc}
596 once for each window with the window as its sole argument.
598 The optional arguments @var{minibuf} and @var{all-frames} specify the
599 set of windows to include in the scan. See @code{next-window}, above,
603 @defun window-list &optional frame minibuf window
604 This function returns a list of the windows on @var{frame}, starting
605 with @var{window}. If @var{frame} is @code{nil} or omitted, the
606 selected frame is used instead; if @var{window} is @code{nil} or
607 omitted, the selected window is used instead.
609 The value of @var{minibuf} determines if the minibuffer window will be
610 included in the result list. If @var{minibuf} is @code{t}, the
611 minibuffer window will be included, even if it isn't active. If
612 @var{minibuf} is @code{nil} or omitted, the minibuffer window will
613 only be included in the list if it is active. If @var{minibuf} is
614 neither @code{nil} nor @code{t}, the minibuffer window is not
615 included, whether or not it is active.
618 @node Buffers and Windows
619 @section Buffers and Windows
620 @cindex examining windows
621 @cindex windows, controlling precisely
622 @cindex buffers, controlled in windows
624 This section describes low-level functions to examine windows or to
625 display buffers in windows in a precisely controlled fashion.
627 See the following section for
630 @xref{Displaying Buffers}, for
632 related functions that find a window to use and specify a buffer for it.
633 The functions described there are easier to use than these, but they
634 employ heuristics in choosing or creating a window; use these functions
635 when you need complete control.
637 @defun set-window-buffer window buffer-or-name
638 This function makes @var{window} display @var{buffer-or-name} as its
639 contents. It returns @code{nil}. This is the fundamental primitive
640 for changing which buffer is displayed in a window, and all ways
641 of doing that call this function.
645 (set-window-buffer (selected-window) "foo")
651 @defun window-buffer &optional window
652 This function returns the buffer that @var{window} is displaying. If
653 @var{window} is omitted, this function returns the buffer for the
659 @result{} #<buffer windows.texi>
664 @defun get-buffer-window buffer-or-name &optional all-frames
665 This function returns a window currently displaying
666 @var{buffer-or-name}, or @code{nil} if there is none. If there are
667 several such windows, then the function returns the first one in the
668 cyclic ordering of windows, starting from the selected window.
669 @xref{Cyclic Window Ordering}.
671 The argument @var{all-frames} controls which windows to consider.
675 If it is @code{nil}, consider windows on the selected frame.
677 If it is @code{t}, consider windows on all frames.
679 If it is @code{visible}, consider windows on all visible frames.
681 If it is 0, consider windows on all visible or iconified frames.
683 If it is a frame, consider windows on that frame.
687 @defun get-buffer-window-list buffer-or-name &optional minibuf all-frames
688 This function returns a list of all the windows currently displaying
689 @var{buffer-or-name}.
691 The two optional arguments work like the optional arguments of
692 @code{next-window} (@pxref{Cyclic Window Ordering}); they are @emph{not}
693 like the single optional argument of @code{get-buffer-window}. Perhaps
694 we should change @code{get-buffer-window} in the future to make it
695 compatible with the other functions.
697 The argument @var{all-frames} controls which windows to consider.
701 If it is @code{nil}, consider windows on the selected frame.
703 If it is @code{t}, consider windows on all frames.
705 If it is @code{visible}, consider windows on all visible frames.
707 If it is 0, consider windows on all visible or iconified frames.
709 If it is a frame, consider windows on that frame.
713 @defvar buffer-display-time
714 This variable records the time at which a buffer was last made visible
715 in a window. It is always local in each buffer; each time
716 @code{set-window-buffer} is called, it sets this variable to
717 @code{(current-time)} in the specified buffer (@pxref{Time of Day}).
718 When a buffer is first created, @code{buffer-display-time} starts out
719 with the value @code{nil}.
722 @node Displaying Buffers
723 @section Displaying Buffers in Windows
724 @cindex switching to a buffer
725 @cindex displaying a buffer
727 In this section we describe convenient functions that choose a window
728 automatically and use it to display a specified buffer. These functions
729 can also split an existing window in certain circumstances. We also
730 describe variables that parameterize the heuristics used for choosing a
733 See the preceding section for
736 @xref{Buffers and Windows}, for
738 low-level functions that give you more precise control. All of these
739 functions work by calling @code{set-window-buffer}.
741 Do not use the functions in this section in order to make a buffer
742 current so that a Lisp program can access or modify it; they are too
743 drastic for that purpose, since they change the display of buffers in
744 windows, which would be gratuitous and surprise the user. Instead, use
745 @code{set-buffer} and @code{save-current-buffer} (@pxref{Current
746 Buffer}), which designate buffers as current for programmed access
747 without affecting the display of buffers in windows.
749 @deffn Command switch-to-buffer buffer-or-name &optional norecord
750 This function makes @var{buffer-or-name} the current buffer, and also
751 displays the buffer in the selected window. This means that a human can
752 see the buffer and subsequent keyboard commands will apply to it.
753 Contrast this with @code{set-buffer}, which makes @var{buffer-or-name}
754 the current buffer but does not display it in the selected window.
755 @xref{Current Buffer}.
757 If @var{buffer-or-name} does not identify an existing buffer, then a new
758 buffer by that name is created. The major mode for the new buffer is
759 set according to the variable @code{default-major-mode}. @xref{Auto
762 Normally the specified buffer is put at the front of the buffer list
763 (both the selected frame's buffer list and the frame-independent buffer
764 list). This affects the operation of @code{other-buffer}. However, if
765 @var{norecord} is non-@code{nil}, this is not done. @xref{The Buffer
768 The @code{switch-to-buffer} function is often used interactively, as
769 the binding of @kbd{C-x b}. It is also used frequently in programs. It
770 always returns @code{nil}.
773 @deffn Command switch-to-buffer-other-window buffer-or-name &optional norecord
774 This function makes @var{buffer-or-name} the current buffer and
775 displays it in a window not currently selected. It then selects that
776 window. The handling of the buffer is the same as in
777 @code{switch-to-buffer}.
779 The currently selected window is absolutely never used to do the job.
780 If it is the only window, then it is split to make a distinct window for
781 this purpose. If the selected window is already displaying the buffer,
782 then it continues to do so, but another window is nonetheless found to
783 display it in as well.
785 This function updates the buffer list just like @code{switch-to-buffer}
786 unless @var{norecord} is non-@code{nil}.
789 @defun pop-to-buffer buffer-or-name &optional other-window norecord
790 This function makes @var{buffer-or-name} the current buffer and
791 switches to it in some window, preferably not the window previously
792 selected. The ``popped-to'' window becomes the selected window within
795 If the variable @code{pop-up-frames} is non-@code{nil},
796 @code{pop-to-buffer} looks for a window in any visible frame already
797 displaying the buffer; if there is one, it returns that window and makes
798 it be selected within its frame. If there is none, it creates a new
799 frame and displays the buffer in it.
801 If @code{pop-up-frames} is @code{nil}, then @code{pop-to-buffer}
802 operates entirely within the selected frame. (If the selected frame has
803 just a minibuffer, @code{pop-to-buffer} operates within the most
804 recently selected frame that was not just a minibuffer.)
806 If the variable @code{pop-up-windows} is non-@code{nil}, windows may
807 be split to create a new window that is different from the original
808 window. For details, see @ref{Choosing Window}.
810 If @var{other-window} is non-@code{nil}, @code{pop-to-buffer} finds or
811 creates another window even if @var{buffer-or-name} is already visible
812 in the selected window. Thus @var{buffer-or-name} could end up
813 displayed in two windows. On the other hand, if @var{buffer-or-name} is
814 already displayed in the selected window and @var{other-window} is
815 @code{nil}, then the selected window is considered sufficient display
816 for @var{buffer-or-name}, so that nothing needs to be done.
818 All the variables that affect @code{display-buffer} affect
819 @code{pop-to-buffer} as well. @xref{Choosing Window}.
821 If @var{buffer-or-name} is a string that does not name an existing
822 buffer, a buffer by that name is created. The major mode for the new
823 buffer is set according to the variable @code{default-major-mode}.
824 @xref{Auto Major Mode}.
826 This function updates the buffer list just like @code{switch-to-buffer}
827 unless @var{norecord} is non-@code{nil}.
830 @deffn Command replace-buffer-in-windows buffer
831 This function replaces @var{buffer} with some other buffer in all
832 windows displaying it. The other buffer used is chosen with
833 @code{other-buffer}. In the usual applications of this function, you
834 don't care which other buffer is used; you just want to make sure that
835 @var{buffer} is no longer displayed.
837 This function returns @code{nil}.
840 @node Choosing Window
841 @section Choosing a Window for Display
843 This section describes the basic facility that chooses a window to
844 display a buffer in---@code{display-buffer}. All the higher-level
845 functions and commands use this subroutine. Here we describe how to use
846 @code{display-buffer} and how to customize it.
848 @deffn Command display-buffer buffer-or-name &optional not-this-window frame
849 This command makes @var{buffer-or-name} appear in some window, like
850 @code{pop-to-buffer}, but it does not select that window and does not
851 make the buffer current. The identity of the selected window is
852 unaltered by this function.
854 If @var{not-this-window} is non-@code{nil}, it means to display the
855 specified buffer in a window other than the selected one, even if it is
856 already on display in the selected window. This can cause the buffer to
857 appear in two windows at once. Otherwise, if @var{buffer-or-name} is
858 already being displayed in any window, that is good enough, so this
859 function does nothing.
861 @code{display-buffer} returns the window chosen to display
862 @var{buffer-or-name}.
864 If the argument @var{frame} is non-@code{nil}, it specifies which frames
865 to check when deciding whether the buffer is already displayed. If the
866 buffer is already displayed in some window on one of these frames,
867 @code{display-buffer} simply returns that window. Here are the possible
868 values of @var{frame}:
872 If it is @code{nil}, consider windows on the selected frame.
874 If it is @code{t}, consider windows on all frames.
876 If it is @code{visible}, consider windows on all visible frames.
878 If it is 0, consider windows on all visible or iconified frames.
880 If it is a frame, consider windows on that frame.
883 Precisely how @code{display-buffer} finds or creates a window depends on
884 the variables described below.
887 @defopt display-buffer-reuse-frames
888 If this variable is non-@code{nil}, @code{display-buffer} searches
889 existing frames for a window displaying the buffer. If the buffer is
890 already displayed in a window in some frame, @code{display-buffer} makes
891 the frame visible and raises it, to use that window. If the buffer is
892 not already displayed, or if @code{display-buffer-reuse-frames} is
893 @code{nil}, @code{display-buffer}'s behavior is determined by other
894 variables, described below.
897 @defopt pop-up-windows
898 This variable controls whether @code{display-buffer} makes new windows.
899 If it is non-@code{nil} and there is only one window, then that window
900 is split. If it is @code{nil}, then @code{display-buffer} does not
901 split the single window, but uses it whole.
904 @defopt split-height-threshold
905 This variable determines when @code{display-buffer} may split a window,
906 if there are multiple windows. @code{display-buffer} always splits the
907 largest window if it has at least this many lines. If the largest
908 window is not this tall, it is split only if it is the sole window and
909 @code{pop-up-windows} is non-@code{nil}.
912 @defopt even-window-heights
913 This variable determines if @code{display-buffer} should even out window
914 heights if the buffer gets displayed in an existing window, above or
915 beneath another existing window. If @code{even-window-heights} is
916 @code{t}, the default, window heights will be evened out. If
917 @code{even-window-heights} is @code{nil}, the orginal window heights
922 @defopt pop-up-frames
923 This variable controls whether @code{display-buffer} makes new frames.
924 If it is non-@code{nil}, @code{display-buffer} looks for an existing
925 window already displaying the desired buffer, on any visible frame. If
926 it finds one, it returns that window. Otherwise it makes a new frame.
927 The variables @code{pop-up-windows} and @code{split-height-threshold} do
928 not matter if @code{pop-up-frames} is non-@code{nil}.
930 If @code{pop-up-frames} is @code{nil}, then @code{display-buffer} either
931 splits a window or reuses one.
933 @xref{Frames}, for more information.
937 @defvar pop-up-frame-function
938 This variable specifies how to make a new frame if @code{pop-up-frames}
941 Its value should be a function of no arguments. When
942 @code{display-buffer} makes a new frame, it does so by calling that
943 function, which should return a frame. The default value of the
944 variable is a function that creates a frame using parameters from
945 @code{pop-up-frame-alist}.
948 @defopt pop-up-frame-alist
949 This variable holds an alist specifying frame parameters used when
950 @code{display-buffer} makes a new frame. @xref{Frame Parameters}, for
951 more information about frame parameters.
954 @defopt special-display-buffer-names
955 A list of buffer names for buffers that should be displayed specially.
956 If the buffer's name is in this list, @code{display-buffer} handles the
959 By default, special display means to give the buffer a dedicated frame.
961 If an element is a list, instead of a string, then the @sc{car} of the
962 list is the buffer name, and the rest of the list says how to create the
963 frame. There are two possibilities for the rest of the list. It can be
964 an alist, specifying frame parameters, or it can contain a function and
965 arguments to give to it. (The function's first argument is always the
966 buffer to be displayed; the arguments from the list come after that.)
969 @defopt special-display-regexps
970 A list of regular expressions that specify buffers that should be
971 displayed specially. If the buffer's name matches any of the regular
972 expressions in this list, @code{display-buffer} handles the buffer
975 By default, special display means to give the buffer a dedicated frame.
977 If an element is a list, instead of a string, then the @sc{car} of the
978 list is the regular expression, and the rest of the list says how to
979 create the frame. See above, under @code{special-display-buffer-names}.
982 @defvar special-display-function
983 This variable holds the function to call to display a buffer specially.
984 It receives the buffer as an argument, and should return the window in
985 which it is displayed.
987 The default value of this variable is
988 @code{special-display-popup-frame}.
991 @defun special-display-popup-frame buffer &rest args
992 This function makes @var{buffer} visible in a frame of its own. If
993 @var{buffer} is already displayed in a window in some frame, it makes
994 the frame visible and raises it, to use that window. Otherwise, it
995 creates a frame that will be dedicated to @var{buffer}.
997 If @var{args} is an alist, it specifies frame parameters for the new
1000 If @var{args} is a list whose @sc{car} is a symbol, then @code{(car
1001 @var{args})} is called as a function to actually create and set up the
1002 frame; it is called with @var{buffer} as first argument, and @code{(cdr
1003 @var{args})} as additional arguments.
1005 This function always uses an existing window displaying @var{buffer},
1006 whether or not it is in a frame of its own; but if you set up the above
1007 variables in your init file, before @var{buffer} was created, then
1008 presumably the window was previously made by this function.
1011 @defopt special-display-frame-alist
1012 This variable holds frame parameters for
1013 @code{special-display-popup-frame} to use when it creates a frame.
1016 @defopt same-window-buffer-names
1017 A list of buffer names for buffers that should be displayed in the
1018 selected window. If the buffer's name is in this list,
1019 @code{display-buffer} handles the buffer by switching to it in the
1023 @defopt same-window-regexps
1024 A list of regular expressions that specify buffers that should be
1025 displayed in the selected window. If the buffer's name matches any of
1026 the regular expressions in this list, @code{display-buffer} handles the
1027 buffer by switching to it in the selected window.
1031 @defvar display-buffer-function
1032 This variable is the most flexible way to customize the behavior of
1033 @code{display-buffer}. If it is non-@code{nil}, it should be a function
1034 that @code{display-buffer} calls to do the work. The function should
1035 accept two arguments, the same two arguments that @code{display-buffer}
1036 received. It should choose or create a window, display the specified
1037 buffer, and then return the window.
1039 This hook takes precedence over all the other options and hooks
1044 @cindex dedicated window
1045 A window can be marked as ``dedicated'' to its buffer. Then
1046 @code{display-buffer} will not try to use that window to display any
1049 @defun window-dedicated-p window
1050 This function returns @code{t} if @var{window} is marked as dedicated;
1051 otherwise @code{nil}.
1054 @defun set-window-dedicated-p window flag
1055 This function marks @var{window} as dedicated if @var{flag} is
1056 non-@code{nil}, and nondedicated otherwise.
1060 @section Windows and Point
1061 @cindex window position
1062 @cindex window point
1063 @cindex position in window
1064 @cindex point in window
1066 Each window has its own value of point, independent of the value of
1067 point in other windows displaying the same buffer. This makes it useful
1068 to have multiple windows showing one buffer.
1072 The window point is established when a window is first created; it is
1073 initialized from the buffer's point, or from the window point of another
1074 window opened on the buffer if such a window exists.
1077 Selecting a window sets the value of point in its buffer from the
1078 window's value of point. Conversely, deselecting a window sets the
1079 window's value of point from that of the buffer. Thus, when you switch
1080 between windows that display a given buffer, the point value for the
1081 selected window is in effect in the buffer, while the point values for
1082 the other windows are stored in those windows.
1085 As long as the selected window displays the current buffer, the window's
1086 point and the buffer's point always move together; they remain equal.
1089 @xref{Positions}, for more details on buffer positions.
1092 As far as the user is concerned, point is where the cursor is, and
1093 when the user switches to another buffer, the cursor jumps to the
1094 position of point in that buffer.
1096 @defun window-point &optional window
1097 This function returns the current position of point in @var{window}.
1098 For a nonselected window, this is the value point would have (in that
1099 window's buffer) if that window were selected. If @var{window} is
1100 @code{nil}, the selected window is used.
1102 When @var{window} is the selected window and its buffer is also the
1103 current buffer, the value returned is the same as point in that buffer.
1105 Strictly speaking, it would be more correct to return the
1106 ``top-level'' value of point, outside of any @code{save-excursion}
1107 forms. But that value is hard to find.
1110 @defun set-window-point window position
1111 This function positions point in @var{window} at position
1112 @var{position} in @var{window}'s buffer.
1116 @section The Window Start Position
1118 Each window contains a marker used to keep track of a buffer position
1119 that specifies where in the buffer display should start. This position
1120 is called the @dfn{display-start} position of the window (or just the
1121 @dfn{start}). The character after this position is the one that appears
1122 at the upper left corner of the window. It is usually, but not
1123 inevitably, at the beginning of a text line.
1125 @defun window-start &optional window
1126 @cindex window top line
1127 This function returns the display-start position of window
1128 @var{window}. If @var{window} is @code{nil}, the selected window is
1138 When you create a window, or display a different buffer in it, the
1139 display-start position is set to a display-start position recently used
1140 for the same buffer, or 1 if the buffer doesn't have any.
1142 Redisplay updates the window-start position (if you have not specified
1143 it explicitly since the previous redisplay)---for example, to make sure
1144 point appears on the screen. Nothing except redisplay automatically
1145 changes the window-start position; if you move point, do not expect the
1146 window-start position to change in response until after the next
1149 For a realistic example of using @code{window-start}, see the
1150 description of @code{count-lines} in @ref{Text Lines}.
1153 @defun window-end &optional window update
1154 This function returns the position of the end of the display in window
1155 @var{window}. If @var{window} is @code{nil}, the selected window is
1158 Simply changing the buffer text or moving point does not update the
1159 value that @code{window-end} returns. The value is updated only when
1160 Emacs redisplays and redisplay completes without being preempted.
1162 If the last redisplay of @var{window} was preempted, and did not finish,
1163 Emacs does not know the position of the end of display in that window.
1164 In that case, this function returns @code{nil}.
1166 If @var{update} is non-@code{nil}, @code{window-end} always returns an
1167 up-to-date value for where the window ends, based on the current
1168 @code{window-start} value. If the saved value is valid,
1169 @code{window-end} returns that; otherwise it computes the correct
1170 value by scanning the buffer text.
1172 Even if @var{update} is non-@code{nil}, @code{window-end} does not
1173 attempt to scroll the display if point has moved off the screen, the
1174 way real redisplay would do. It does not alter the
1175 @code{window-start} value. In effect, it reports where the displayed
1176 text will end if scrolling is not required.
1179 @defun set-window-start window position &optional noforce
1180 This function sets the display-start position of @var{window} to
1181 @var{position} in @var{window}'s buffer. It returns @var{position}.
1183 The display routines insist that the position of point be visible when a
1184 buffer is displayed. Normally, they change the display-start position
1185 (that is, scroll the window) whenever necessary to make point visible.
1186 However, if you specify the start position with this function using
1187 @code{nil} for @var{noforce}, it means you want display to start at
1188 @var{position} even if that would put the location of point off the
1189 screen. If this does place point off screen, the display routines move
1190 point to the left margin on the middle line in the window.
1192 For example, if point @w{is 1} and you set the start of the window @w{to
1193 2}, then point would be ``above'' the top of the window. The display
1194 routines will automatically move point if it is still 1 when redisplay
1195 occurs. Here is an example:
1199 ;; @r{Here is what @samp{foo} looks like before executing}
1200 ;; @r{the @code{set-window-start} expression.}
1204 ---------- Buffer: foo ----------
1205 @point{}This is the contents of buffer foo.
1211 ---------- Buffer: foo ----------
1217 (1+ (window-start)))
1222 ;; @r{Here is what @samp{foo} looks like after executing}
1223 ;; @r{the @code{set-window-start} expression.}
1224 ---------- Buffer: foo ----------
1225 his is the contents of buffer foo.
1231 ---------- Buffer: foo ----------
1235 If @var{noforce} is non-@code{nil}, and @var{position} would place point
1236 off screen at the next redisplay, then redisplay computes a new window-start
1237 position that works well with point, and thus @var{position} is not used.
1240 @defun pos-visible-in-window-p &optional position window partially
1241 This function returns @code{t} if @var{position} is within the range of
1242 text currently visible on the screen in @var{window}. It returns
1243 @code{nil} if @var{position} is scrolled vertically or horizontally out
1244 of view. Locations that are partially obscured are not considered
1245 visible unless @var{partially} is non-@code{nil}. The argument
1246 @var{position} defaults to the current position of point in
1247 @var{window}; @var{window}, to the selected window.
1253 (or (pos-visible-in-window-p
1254 (point) (selected-window))
1260 @node Textual Scrolling
1261 @section Textual Scrolling
1262 @cindex textual scrolling
1263 @cindex scrolling textually
1265 @dfn{Textual scrolling} means moving the text up or down though a
1266 window. It works by changing the value of the window's display-start
1267 location. It may also change the value of @code{window-point} to keep
1268 point on the screen.
1270 Textual scrolling was formerly called ``vertical scrolling,'' but we
1271 changed its name to distinguish it from the new vertical fractional
1272 scrolling feature (@pxref{Vertical Scrolling}).
1274 In the commands @code{scroll-up} and @code{scroll-down}, the directions
1275 ``up'' and ``down'' refer to the motion of the text in the buffer at which
1276 you are looking through the window. Imagine that the text is
1277 written on a long roll of paper and that the scrolling commands move the
1278 paper up and down. Thus, if you are looking at text in the middle of a
1279 buffer and repeatedly call @code{scroll-down}, you will eventually see
1280 the beginning of the buffer.
1282 Some people have urged that the opposite convention be used: they
1283 imagine that the window moves over text that remains in place. Then
1284 ``down'' commands would take you to the end of the buffer. This view is
1285 more consistent with the actual relationship between windows and the
1286 text in the buffer, but it is less like what the user sees. The
1287 position of a window on the terminal does not move, and short scrolling
1288 commands clearly move the text up or down on the screen. We have chosen
1289 names that fit the user's point of view.
1291 The textual scrolling functions (aside from
1292 @code{scroll-other-window}) have unpredictable results if the current
1293 buffer is different from the buffer that is displayed in the selected
1294 window. @xref{Current Buffer}.
1296 @deffn Command scroll-up &optional count
1297 This function scrolls the text in the selected window upward
1298 @var{count} lines. If @var{count} is negative, scrolling is actually
1301 If @var{count} is @code{nil} (or omitted), then the length of scroll
1302 is @code{next-screen-context-lines} lines less than the usable height of
1303 the window (not counting its mode line).
1305 @code{scroll-up} returns @code{nil}.
1308 @deffn Command scroll-down &optional count
1309 This function scrolls the text in the selected window downward
1310 @var{count} lines. If @var{count} is negative, scrolling is actually
1313 If @var{count} is omitted or @code{nil}, then the length of the scroll
1314 is @code{next-screen-context-lines} lines less than the usable height of
1315 the window (not counting its mode line).
1317 @code{scroll-down} returns @code{nil}.
1320 @deffn Command scroll-other-window &optional count
1321 This function scrolls the text in another window upward @var{count}
1322 lines. Negative values of @var{count}, or @code{nil}, are handled
1323 as in @code{scroll-up}.
1325 You can specify which buffer to scroll by setting the variable
1326 @code{other-window-scroll-buffer} to a buffer. If that buffer isn't
1327 already displayed, @code{scroll-other-window} displays it in some
1330 When the selected window is the minibuffer, the next window is normally
1331 the one at the top left corner. You can specify a different window to
1332 scroll, when the minibuffer is selected, by setting the variable
1333 @code{minibuffer-scroll-window}. This variable has no effect when any
1334 other window is selected. @xref{Minibuffer Misc}.
1336 When the minibuffer is active, it is the next window if the selected
1337 window is the one at the bottom right corner. In this case,
1338 @code{scroll-other-window} attempts to scroll the minibuffer. If the
1339 minibuffer contains just one line, it has nowhere to scroll to, so the
1340 line reappears after the echo area momentarily displays the message
1341 ``Beginning of buffer''.
1345 @defvar other-window-scroll-buffer
1346 If this variable is non-@code{nil}, it tells @code{scroll-other-window}
1347 which buffer to scroll.
1350 @defopt scroll-margin
1351 This option specifies the size of the scroll margin---a minimum number
1352 of lines between point and the top or bottom of a window. Whenever
1353 point gets within this many lines of the top or bottom of the window,
1354 the window scrolls automatically (if possible) to move point out of the
1355 margin, closer to the center of the window.
1358 @defopt scroll-conservatively
1359 This variable controls how scrolling is done automatically when point
1360 moves off the screen (or into the scroll margin). If the value is zero,
1361 then redisplay scrolls the text to center point vertically in the
1362 window. If the value is a positive integer @var{n}, then redisplay
1363 scrolls the window up to @var{n} lines in either direction, if that will
1364 bring point back into view. Otherwise, it centers point. The default
1367 A value of @code{nil} is equivalent to .5, since it centers point. This
1368 variable automatically becomes buffer-local when set in any fashion.
1371 @defopt scroll-up-aggressively
1372 @tindex scroll-up-aggressively
1373 The value of this variable should be either @code{nil} or a fraction
1374 @var{f} between 0 and 1. If it is a fraction, that specifies where on
1375 the screen to put point when scrolling upward. More precisely, when a
1376 window scrolls up because point is above the window start, the new start
1377 position is chosen to put point @var{f} part of the window height from
1378 the top. The larger @var{f}, the more aggressive the scrolling.
1380 A value of @code{nil} is equivalent to .5, since its effect is to center
1381 point. This variable automatically becomes buffer-local when set in any
1385 @defopt scroll-down-aggressively
1386 @tindex scroll-down-aggressively
1387 Likewise, for scrolling down. The value, @var{f}, specifies how far
1388 point should be placed from the bottom of the window; thus, as with
1389 @code{scroll-up-aggressively}, a larger value scrolls more aggressively.
1393 This variable is an older variant of @code{scroll-conservatively}. The
1394 difference is that it if its value is @var{n}, that permits scrolling
1395 only by precisely @var{n} lines, not a smaller number. This feature
1396 does not work with @code{scroll-margin}. The default value is zero.
1399 @defopt scroll-preserve-screen-position
1400 If this option is non-@code{nil}, the scroll functions move point so
1401 that the vertical position of the cursor is unchanged, when that is
1405 @defopt next-screen-context-lines
1406 The value of this variable is the number of lines of continuity to
1407 retain when scrolling by full screens. For example, @code{scroll-up}
1408 with an argument of @code{nil} scrolls so that this many lines at the
1409 bottom of the window appear instead at the top. The default value is
1413 @deffn Command recenter &optional count
1414 @cindex centering point
1415 This function scrolls the selected window to put the text where point
1416 is located at a specified vertical position within the window.
1418 If @var{count} is a nonnegative number, it puts the line containing
1419 point @var{count} lines down from the top of the window. If @var{count}
1420 is a negative number, then it counts upward from the bottom of the
1421 window, so that @minus{}1 stands for the last usable line in the window.
1422 If @var{count} is a non-@code{nil} list, then it stands for the line in
1423 the middle of the window.
1425 If @var{count} is @code{nil}, @code{recenter} puts the line containing
1426 point in the middle of the window, then clears and redisplays the entire
1429 When @code{recenter} is called interactively, @var{count} is the raw
1430 prefix argument. Thus, typing @kbd{C-u} as the prefix sets the
1431 @var{count} to a non-@code{nil} list, while typing @kbd{C-u 4} sets
1432 @var{count} to 4, which positions the current line four lines from the
1435 With an argument of zero, @code{recenter} positions the current line at
1436 the top of the window. This action is so handy that some people make a
1437 separate key binding to do this. For example,
1441 (defun line-to-top-of-window ()
1442 "Scroll current line to top of window.
1443 Replaces three keystroke sequence C-u 0 C-l."
1447 (global-set-key [kp-multiply] 'line-to-top-of-window)
1452 @node Vertical Scrolling
1453 @section Vertical Fractional Scrolling
1454 @cindex Vertical Fractional Scrolling
1456 @dfn{Vertical fractional scrolling} means shifting the image in the
1457 window up or down by a specified multiple or fraction of a line.
1458 Starting in Emacs 21, each window has a @dfn{vertical scroll position},
1459 which is a number, never less than zero. It specifies how far to raise
1460 the contents of the window. Raising the window contents generally makes
1461 all or part of some lines disappear off the top, and all or part of some
1462 other lines appear at the bottom. The usual value is zero.
1464 The vertical scroll position is measured in units of the normal line
1465 height, which is the height of the default font. Thus, if the value is
1466 .5, that means the window contents are scrolled up half the normal line
1467 height. If it is 3.3, that means the window contents are scrolled up
1468 somewhat over three times the normal line height.
1470 What fraction of a line the vertical scrolling covers, or how many
1471 lines, depends on what the lines contain. A value of .5 could scroll a
1472 line whose height is very short off the screen, while a value of 3.3
1473 could scroll just part of the way through a tall line or an image.
1475 @defun window-vscroll &optional window
1476 This function returns the current vertical scroll position of
1477 @var{window}, If @var{window} is @code{nil}, the selected window is
1488 @defun set-window-vscroll window lines
1489 This function sets @var{window}'s vertical scroll position to
1490 @var{lines}. The argument @var{lines} should be zero or positive; if
1491 not, it is taken as zero.
1493 The actual vertical scroll position must always correspond
1494 to an integral number of pixels, so the value you specify
1495 is rounded accordingly.
1497 The return value is the result of this rounding.
1501 (set-window-vscroll (selected-window) 1.2)
1507 @node Horizontal Scrolling
1508 @section Horizontal Scrolling
1509 @cindex horizontal scrolling
1511 @dfn{Horizontal scrolling} means shifting the image in the window left
1512 or right by a specified multiple of the normal character width. Each
1513 window has a @dfn{vertical scroll position}, which is a number, never
1514 less than zero. It specifies how far to shift the contents left.
1515 Shifting the window contents left generally makes all or part of some
1516 characters disappear off the left, and all or part of some other
1517 characters appear at the right. The usual value is zero.
1519 The horizontal scroll position is measured in units of the normal
1520 character width, which is the width of space in the default font. Thus,
1521 if the value is 5, that means the window contents are scrolled left by 5
1522 times the normal character width. How many characters actually
1523 disappear off to the left depends on their width, and could vary from
1526 Because we read from side to side in the ``inner loop'', and from top
1527 to bottom in the ``outer loop'', the effect of horizontal scrolling is
1528 not like that of textual or vertical scrolling. Textual scrolling
1529 involves selection of a portion of text to display, and vertical
1530 scrolling moves the window contents contiguously; but horizontal
1531 scrolling causes part of @emph{each line} to go off screen.
1533 Usually, no horizontal scrolling is in effect; then the leftmost
1534 column is at the left edge of the window. In this state, scrolling to
1535 the right is meaningless, since there is no data to the left of the edge
1536 to be revealed by it; so this is not allowed. Scrolling to the left is
1537 allowed; it scrolls the first columns of text off the edge of the window
1538 and can reveal additional columns on the right that were truncated
1539 before. Once a window has a nonzero amount of leftward horizontal
1540 scrolling, you can scroll it back to the right, but only so far as to
1541 reduce the net horizontal scroll to zero. There is no limit to how far
1542 left you can scroll, but eventually all the text will disappear off the
1545 @vindex automatic-hscrolling
1546 In Emacs 21, redisplay automatically alters the horizontal scrolling
1547 of a window as necessary to ensure that point is always visible, if
1548 @code{automatic-hscrolling} is set. However, you can still set the
1549 horizontal scrolling value explicitly. The value you specify serves as
1550 a lower bound for automatic scrolling, i.e. automatic scrolling
1551 will not scroll a window to a column less than the specified one.
1553 @deffn Command scroll-left &optional count
1554 This function scrolls the selected window @var{count} columns to the
1555 left (or to the right if @var{count} is negative). The default
1556 for @var{count} is the window width, minus 2.
1558 The return value is the total amount of leftward horizontal scrolling in
1559 effect after the change---just like the value returned by
1560 @code{window-hscroll} (below).
1563 @deffn Command scroll-right &optional count
1564 This function scrolls the selected window @var{count} columns to the
1565 right (or to the left if @var{count} is negative). The default
1566 for @var{count} is the window width, minus 2.
1568 The return value is the total amount of leftward horizontal scrolling in
1569 effect after the change---just like the value returned by
1570 @code{window-hscroll} (below).
1572 Once you scroll a window as far right as it can go, back to its normal
1573 position where the total leftward scrolling is zero, attempts to scroll
1574 any farther right have no effect.
1577 @defun window-hscroll &optional window
1578 This function returns the total leftward horizontal scrolling of
1579 @var{window}---the number of columns by which the text in @var{window}
1580 is scrolled left past the left margin.
1582 The value is never negative. It is zero when no horizontal scrolling
1583 has been done in @var{window} (which is usually the case).
1585 If @var{window} is @code{nil}, the selected window is used.
1603 @defun set-window-hscroll window columns
1604 This function sets the number of columns from the left margin that
1605 @var{window} is scrolled from the value of @var{columns}. The argument
1606 @var{columns} should be zero or positive; if not, it is taken as zero.
1607 Fractional values of @var{columns} are not supported at present.
1609 The value returned is @var{columns}.
1613 (set-window-hscroll (selected-window) 10)
1619 Here is how you can determine whether a given position @var{position}
1620 is off the screen due to horizontal scrolling:
1624 (defun hscroll-on-screen (window position)
1626 (goto-char position)
1628 (>= (- (current-column) (window-hscroll window)) 0)
1629 (< (- (current-column) (window-hscroll window))
1630 (window-width window)))))
1634 @node Size of Window
1635 @section The Size of a Window
1637 @cindex size of window
1639 An Emacs window is rectangular, and its size information consists of
1640 the height (the number of lines) and the width (the number of character
1641 positions in each line). The mode line is included in the height. But
1642 the width does not count the scroll bar or the column of @samp{|}
1643 characters that separates side-by-side windows.
1645 The following three functions return size information about a window:
1647 @defun window-height &optional window
1648 This function returns the number of lines in @var{window}, including its
1649 mode line. If @var{window} fills its entire frame, this is typically
1650 one less than the value of @code{frame-height} on that frame (since the
1651 last line is always reserved for the minibuffer).
1653 If @var{window} is @code{nil}, the function uses the selected window.
1661 (split-window-vertically)
1662 @result{} #<window 4 on windows.texi>
1671 @defun window-width &optional window
1672 This function returns the number of columns in @var{window}. If
1673 @var{window} fills its entire frame, this is the same as the value of
1674 @code{frame-width} on that frame. The width does not include the
1675 window's scroll bar or the column of @samp{|} characters that separates
1676 side-by-side windows.
1678 If @var{window} is @code{nil}, the function uses the selected window.
1688 @defun window-edges &optional window
1689 This function returns a list of the edge coordinates of @var{window}.
1690 If @var{window} is @code{nil}, the selected window is used.
1692 The order of the list is @code{(@var{left} @var{top} @var{right}
1693 @var{bottom})}, all elements relative to 0, 0 at the top left corner of
1694 the frame. The element @var{right} of the value is one more than the
1695 rightmost column used by @var{window}, and @var{bottom} is one more than
1696 the bottommost row used by @var{window} and its mode-line.
1698 If a window has a scroll bar, the right edge value includes the width of
1699 the scroll bar. Otherwise, if the window has a neighbor on the right,
1700 its right edge value includes the width of the separator line between
1701 the window and that neighbor. Since the width of the window does not
1702 include this separator, the width does not usually equal the difference
1703 between the right and left edges.
1705 Here is the result obtained on a typical 24-line terminal with just one
1710 (window-edges (selected-window))
1711 @result{} (0 0 80 23)
1716 The bottom edge is at line 23 because the last line is the echo area.
1718 If @var{window} is at the upper left corner of its frame, then
1719 @var{bottom} is the same as the value of @code{(window-height)},
1720 @var{right} is almost the same as the value of @code{(window-width)},
1721 and @var{top} and @var{left} are zero. For example, the edges of the
1722 following window are @w{@samp{0 0 8 5}}. Assuming that the frame has
1723 more than 8 columns, the last column of the window (column 7) holds a
1724 border rather than text. The last row (row 4) holds the mode line,
1725 shown here with @samp{xxxxxxxxx}.
1741 In the following example, let's suppose that the frame is 7
1742 columns wide. Then the edges of the left window are @w{@samp{0 0 4 3}}
1743 and the edges of the right window are @w{@samp{4 0 8 3}}.
1757 @node Resizing Windows
1758 @section Changing the Size of a Window
1759 @cindex window resizing
1760 @cindex changing window size
1761 @cindex window size, changing
1763 The window size functions fall into two classes: high-level commands
1764 that change the size of windows and low-level functions that access
1765 window size. Emacs does not permit overlapping windows or gaps between
1766 windows, so resizing one window affects other windows.
1768 @deffn Command enlarge-window size &optional horizontal
1769 This function makes the selected window @var{size} lines taller,
1770 stealing lines from neighboring windows. It takes the lines from one
1771 window at a time until that window is used up, then takes from another.
1772 If a window from which lines are stolen shrinks below
1773 @code{window-min-height} lines, that window disappears.
1775 If @var{horizontal} is non-@code{nil}, this function makes
1776 @var{window} wider by @var{size} columns, stealing columns instead of
1777 lines. If a window from which columns are stolen shrinks below
1778 @code{window-min-width} columns, that window disappears.
1780 If the requested size would exceed that of the window's frame, then the
1781 function makes the window occupy the entire height (or width) of the
1784 If there are various other windows from which lines or columns can be
1785 stolen, and some of them specify fixed size (using
1786 @code{window-size-fixed}, see below), they are left untouched while
1787 other windows are ``robbed.'' If it would be necessary to alter the
1788 size of a fixed-size window, @code{enlarge-window} gets an error
1791 If @var{size} is negative, this function shrinks the window by
1792 @minus{}@var{size} lines or columns. If that makes the window smaller
1793 than the minimum size (@code{window-min-height} and
1794 @code{window-min-width}), @code{enlarge-window} deletes the window.
1796 @code{enlarge-window} returns @code{nil}.
1799 @deffn Command enlarge-window-horizontally columns
1800 This function makes the selected window @var{columns} wider.
1801 It could be defined as follows:
1805 (defun enlarge-window-horizontally (columns)
1806 (enlarge-window columns t))
1811 @deffn Command shrink-window size &optional horizontal
1812 This function is like @code{enlarge-window} but negates the argument
1813 @var{size}, making the selected window smaller by giving lines (or
1814 columns) to the other windows. If the window shrinks below
1815 @code{window-min-height} or @code{window-min-width}, then it disappears.
1817 If @var{size} is negative, the window is enlarged by @minus{}@var{size}
1821 @deffn Command shrink-window-horizontally columns
1822 This function makes the selected window @var{columns} narrower.
1823 It could be defined as follows:
1827 (defun shrink-window-horizontally (columns)
1828 (shrink-window columns t))
1833 @deffn Command shrink-window-if-larger-than-buffer &optional window
1834 This command shrinks @var{window} to be as small as possible while still
1835 showing the full contents of its buffer---but not less than
1836 @code{window-min-height} lines. If @var{window} is not given,
1837 it defaults to the selected window.
1839 However, the command does nothing if the window is already too small to
1840 display the whole text of the buffer, or if part of the contents are
1841 currently scrolled off screen, or if the window is not the full width of
1842 its frame, or if the window is the only window in its frame.
1845 @tindex window-size-fixed
1846 @defvar window-size-fixed
1847 If this variable is non-@code{nil}, in any given buffer,
1848 then the size of any window displaying the buffer remains fixed
1849 unless you explicitly change it or Emacs has no other choice.
1850 (This feature is new in Emacs 21.)
1852 If the value is @code{height}, then only the window's height is fixed;
1853 if the value is @code{width}, then only the window's width is fixed.
1854 Any other non-@code{nil} value fixes both the width and the height.
1856 The usual way to use this variable is to give it a buffer-local value in
1857 a particular buffer. That way, the windows (but usually there is only
1858 one) displaying that buffer have fixed size.
1860 Explicit size-change functions such as @code{enlarge-window}
1861 get an error if they would have to change a window size which is fixed.
1862 Therefore, when you want to change the size of such a window,
1863 you should bind @code{window-size-fixed} to @code{nil}, like this:
1866 (let ((window-size-fixed nil))
1867 (enlarge-window 10))
1870 Note that changing the frame size will change the size of a
1871 fixed-size window, if there is no other alternative.
1874 @cindex minimum window size
1875 The following two variables constrain the window-size-changing
1876 functions to a minimum height and width.
1878 @defopt window-min-height
1879 The value of this variable determines how short a window may become
1880 before it is automatically deleted. Making a window smaller than
1881 @code{window-min-height} automatically deletes it, and no window may be
1882 created shorter than this. The absolute minimum height is two (allowing
1883 one line for the mode line, and one line for the buffer display).
1884 Actions that change window sizes reset this variable to two if it is
1885 less than two. The default value is 4.
1888 @defopt window-min-width
1889 The value of this variable determines how narrow a window may become
1890 before it is automatically deleted. Making a window smaller than
1891 @code{window-min-width} automatically deletes it, and no window may be
1892 created narrower than this. The absolute minimum width is one; any
1893 value below that is ignored. The default value is 10.
1896 @node Coordinates and Windows
1897 @section Coordinates and Windows
1899 This section describes how to relate screen coordinates to windows.
1901 @defun window-at x y &optional frame
1902 This function returns the window containing the specified cursor
1903 position in the frame @var{frame}. The coordinates @var{x} and @var{y}
1904 are measured in characters and count from the top left corner of the
1905 frame. If they are out of range, @code{window-at} returns @code{nil}.
1907 If you omit @var{frame}, the selected frame is used.
1910 @defun coordinates-in-window-p coordinates window
1911 This function checks whether a particular frame position falls within
1912 the window @var{window}.
1914 The argument @var{coordinates} is a cons cell of the form @code{(@var{x}
1915 . @var{y})}. The coordinates @var{x} and @var{y} are measured in
1916 characters, and count from the top left corner of the screen or frame.
1918 The value returned by @code{coordinates-in-window-p} is non-@code{nil}
1919 if the coordinates are inside @var{window}. The value also indicates
1920 what part of the window the position is in, as follows:
1923 @item (@var{relx} . @var{rely})
1924 The coordinates are inside @var{window}. The numbers @var{relx} and
1925 @var{rely} are the equivalent window-relative coordinates for the
1926 specified position, counting from 0 at the top left corner of the
1930 The coordinates are in the mode line of @var{window}.
1933 The coordinates are in the header line of @var{window}.
1936 The coordinates are in the vertical line between @var{window} and its
1937 neighbor to the right. This value occurs only if the window doesn't
1938 have a scroll bar; positions in a scroll bar are considered outside the
1939 window for these purposes.
1942 The coordinates are not in any part of @var{window}.
1945 The function @code{coordinates-in-window-p} does not require a frame as
1946 argument because it always uses the frame that @var{window} is on.
1949 @node Window Configurations
1950 @section Window Configurations
1951 @cindex window configurations
1952 @cindex saving window information
1954 A @dfn{window configuration} records the entire layout of one
1955 frame---all windows, their sizes, which buffers they contain, what part
1956 of each buffer is displayed, and the values of point and the mark. You
1957 can bring back an entire previous layout by restoring a window
1958 configuration previously saved.
1960 If you want to record all frames instead of just one, use a frame
1961 configuration instead of a window configuration. @xref{Frame
1964 @defun current-window-configuration &optional frame
1965 This function returns a new object representing @var{frame}'s
1966 current window configuration, including the number of windows, their
1967 sizes and current buffers, which window is the selected window, and for
1968 each window the displayed buffer, the display-start position, and the
1969 positions of point and the mark. It also includes the values of
1970 @code{window-min-height}, @code{window-min-width} and
1971 @code{minibuffer-scroll-window}. An exception is made for point in the
1972 current buffer, whose value is not saved.
1974 If @var{frame} is omitted, the selected frame is used.
1977 @defun set-window-configuration configuration
1978 This function restores the configuration of windows and buffers as
1979 specified by @var{configuration}, for the frame that @var{configuration}
1982 The argument @var{configuration} must be a value that was previously
1983 returned by @code{current-window-configuration}. This configuration is
1984 restored in the frame from which @var{configuration} was made, whether
1985 that frame is selected or not. This always counts as a window size
1986 change and triggers execution of the @code{window-size-change-functions}
1987 (@pxref{Window Hooks}), because @code{set-window-configuration} doesn't
1988 know how to tell whether the new configuration actually differs from the
1991 If the frame which @var{configuration} was saved from is dead, all this
1992 function does is restore the three variables @code{window-min-height},
1993 @code{window-min-width} and @code{minibuffer-scroll-window}.
1995 Here is a way of using this function to get the same effect
1996 as @code{save-window-excursion}:
2000 (let ((config (current-window-configuration)))
2002 (progn (split-window-vertically nil)
2004 (set-window-configuration config)))
2009 @defspec save-window-excursion forms@dots{}
2010 This special form records the window configuration, executes @var{forms}
2011 in sequence, then restores the earlier window configuration. The window
2012 configuration includes the value of point and the portion of the buffer
2013 that is visible. It also includes the choice of selected window.
2014 However, it does not include the value of point in the current buffer;
2015 use @code{save-excursion} also, if you wish to preserve that.
2017 Don't use this construct when @code{save-selected-window} is all you need.
2019 Exit from @code{save-window-excursion} always triggers execution of the
2020 @code{window-size-change-functions}. (It doesn't know how to tell
2021 whether the restored configuration actually differs from the one in
2022 effect at the end of the @var{forms}.)
2024 The return value is the value of the final form in @var{forms}.
2030 @result{} #<window 25 on control.texi>
2033 (setq w (selected-window))
2034 @result{} #<window 19 on control.texi>
2037 (save-window-excursion
2038 (delete-other-windows w)
2039 (switch-to-buffer "foo")
2041 @result{} do-something
2042 ;; @r{The screen is now split again.}
2047 @defun window-configuration-p object
2048 This function returns @code{t} if @var{object} is a window configuration.
2051 @defun compare-window-configurations config1 config2
2052 This function compares two window configurations as regards the
2053 structure of windows, but ignores the values of point and mark and the
2054 saved scrolling positions---it can return @code{t} even if those
2057 The function @code{equal} can also compare two window configurations; it
2058 regards configurations as unequal if they differ in any respect, even a
2059 saved point or mark.
2062 Primitives to look inside of window configurations would make sense,
2063 but none are implemented. It is not clear they are useful enough to be
2067 @section Hooks for Window Scrolling and Changes
2069 This section describes how a Lisp program can take action whenever a
2070 window displays a different part of its buffer or a different buffer.
2071 There are three actions that can change this: scrolling the window,
2072 switching buffers in the window, and changing the size of the window.
2073 The first two actions run @code{window-scroll-functions}; the last runs
2074 @code{window-size-change-functions}. The paradigmatic use of these
2075 hooks is in the implementation of Lazy Lock mode; see @ref{Support
2076 Modes, Lazy Lock, Font Lock Support Modes, emacs, The GNU Emacs Manual}.
2078 @defvar window-scroll-functions
2079 This variable holds a list of functions that Emacs should call before
2080 redisplaying a window with scrolling. It is not a normal hook, because
2081 each function is called with two arguments: the window, and its new
2082 display-start position.
2084 Displaying a different buffer in the window also runs these functions.
2086 These functions must be careful in using @code{window-end}
2087 (@pxref{Window Start}); if you need an up-to-date value, you must use
2088 the @var{update} argument to ensure you get it.
2091 @defvar window-size-change-functions
2092 This variable holds a list of functions to be called if the size of any
2093 window changes for any reason. The functions are called just once per
2094 redisplay, and just once for each frame on which size changes have
2097 Each function receives the frame as its sole argument. There is no
2098 direct way to find out which windows on that frame have changed size, or
2099 precisely how. However, if a size-change function records, at each
2100 call, the existing windows and their sizes, it can also compare the
2101 present sizes and the previous sizes.
2103 Creating or deleting windows counts as a size change, and therefore
2104 causes these functions to be called. Changing the frame size also
2105 counts, because it changes the sizes of the existing windows.
2107 It is not a good idea to use @code{save-window-excursion} (@pxref{Window
2108 Configurations}) in these functions, because that always counts as a
2109 size change, and it would cause these functions to be called over and
2110 over. In most cases, @code{save-selected-window} (@pxref{Selecting
2111 Windows}) is what you need here.
2114 @defvar redisplay-end-trigger-functions
2115 This abnormal hook is run whenever redisplay in a window uses text that
2116 extends past a specified end trigger position. You set the end trigger
2117 position with the function @code{set-window-redisplay-end-trigger}. The
2118 functions are called with two arguments: the window, and the end trigger
2119 position. Storing @code{nil} for the end trigger position turns off the
2120 feature, and the trigger value is automatically reset to @code{nil} just
2121 after the hook is run.
2124 @defun set-window-redisplay-end-trigger window position
2125 This function sets @var{window}'s end trigger position at
2129 @defun window-redisplay-end-trigger &optional window
2130 This function returns @var{window}'s current end trigger position.
2133 @defvar window-configuration-change-hook
2134 A normal hook that is run every time you change the window configuration
2135 of an existing frame. This includes splitting or deleting windows,
2136 changing the sizes of windows, or displaying a different buffer in a
2137 window. The frame whose window configuration has changed is the
2138 selected frame when this hook runs.