(displaying-byte-compile-warnings): Show
[bpt/emacs.git] / lispref / windows.texi
CommitLineData
b1b12a8e
RS
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3c29caa8 3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
b1b12a8e
RS
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/windows
6@node Windows, Frames, Buffers, Top
7@chapter Windows
8
9 This chapter describes most of the functions and variables related to
10Emacs windows. See @ref{Display}, for information on how text is
11displayed in windows.
12
13@menu
3c29caa8
DH
14* Basic Windows:: Basic information on using windows.
15* Splitting Windows:: Splitting one window into two windows.
16* Deleting Windows:: Deleting a window gives its space to other windows.
17* Selecting Windows:: The selected window is the one that you edit in.
18* Cyclic Window Ordering:: Moving around the existing windows.
19* Buffers and Windows:: Each window displays the contents of a buffer.
20* Displaying Buffers:: Higher-lever functions for displaying a buffer
21 and choosing a window for it.
22* Choosing Window:: How to choose a window for displaying a buffer.
23* Window Point:: Each window has its own location of point.
24* Window Start:: The display-start position controls which text
25 is on-screen in the window.
26* Vertical Scrolling:: Moving text up and down in the window.
27* Scrolling Hooks:: Hooks that run when you scroll a window.
28* Horizontal Scrolling:: Moving text sideways on the window.
29* Size of Window:: Accessing the size of a window.
30* Resizing Windows:: Changing the size of a window.
31* Coordinates and Windows:: Converting coordinates to windows.
32* Window Configurations:: Saving and restoring the state of the screen.
b1b12a8e
RS
33@end menu
34
35@node Basic Windows
36@section Basic Concepts of Emacs Windows
37@cindex window
38@cindex selected window
39
bfe721d1
KH
40 A @dfn{window} in Emacs is the physical area of the screen in which a
41buffer is displayed. The term is also used to refer to a Lisp object that
b1b12a8e
RS
42represents that screen area in Emacs Lisp. It should be
43clear from the context which is meant.
44
bfe721d1
KH
45 Emacs groups windows into frames. A frame represents an area of
46screen available for Emacs to use. Each frame always contains at least
47one window, but you can subdivide it vertically or horizontally into
48multiple nonoverlapping Emacs windows.
49
50 In each frame, at any time, one and only one window is designated as
51@dfn{selected within the frame}. The frame's cursor appears in that
52window. At ant time, one frame is the selected frame; and the window
53selected within that frame is @dfn{the selected window}. The selected
54window's buffer is usually the current buffer (except when
55@code{set-buffer} has been used). @xref{Current Buffer}.
56
57 For practical purposes, a window exists only while it is displayed in
58a frame. Once removed from the frame, the window is effectively deleted
59and should not be used, @emph{even though there may still be references
60to it} from other Lisp objects. Restoring a saved window configuration
61is the only way for a window no longer on the screen to come back to
62life. (@xref{Deleting Windows}.)
b1b12a8e
RS
63
64 Each window has the following attributes:
65
66@itemize @bullet
67@item
68containing frame
69
3c29caa8 70@item
b1b12a8e
RS
71window height
72
3c29caa8 73@item
b1b12a8e
RS
74window width
75
3c29caa8 76@item
b1b12a8e
RS
77window edges with respect to the screen or frame
78
3c29caa8 79@item
b1b12a8e
RS
80the buffer it displays
81
3c29caa8 82@item
b1b12a8e
RS
83position within the buffer at the upper left of the window
84
3c29caa8 85@item
c638661f 86amount of horizontal scrolling, in columns
b1b12a8e 87
3c29caa8 88@item
b1b12a8e
RS
89point
90
3c29caa8 91@item
b1b12a8e
RS
92the mark
93
3c29caa8 94@item
b1b12a8e
RS
95how recently the window was selected
96@end itemize
97
98@cindex multiple windows
99 Users create multiple windows so they can look at several buffers at
100once. Lisp libraries use multiple windows for a variety of reasons, but
bfe721d1
KH
101most often to display related information. In Rmail, for example, you
102can move through a summary buffer in one window while the other window
103shows messages one at a time as they are reached.
b1b12a8e
RS
104
105 The meaning of ``window'' in Emacs is similar to what it means in the
c638661f 106context of general-purpose window systems such as X, but not identical.
bfe721d1
KH
107The X Window System places X windows on the screen; Emacs uses one or
108more X windows as frames, and subdivides them into
109Emacs windows. When you use Emacs on a character-only terminal, Emacs
110treats the whole terminal screen as one frame.
b1b12a8e
RS
111
112@cindex terminal screen
113@cindex screen of terminal
114@cindex tiled windows
115 Most window systems support arbitrarily located overlapping windows.
116In contrast, Emacs windows are @dfn{tiled}; they never overlap, and
c638661f 117together they fill the whole screen or frame. Because of the way
b1b12a8e
RS
118in which Emacs creates new windows and resizes them, you can't create
119every conceivable tiling of windows on an Emacs frame. @xref{Splitting
120Windows}, and @ref{Size of Window}.
121
122 @xref{Display}, for information on how the contents of the
123window's buffer are displayed in the window.
124
125@defun windowp object
126 This function returns @code{t} if @var{object} is a window.
127@end defun
128
129@node Splitting Windows
130@section Splitting Windows
131@cindex splitting windows
132@cindex window splitting
133
134 The functions described here are the primitives used to split a window
135into two windows. Two higher level functions sometimes split a window,
136but not always: @code{pop-to-buffer} and @code{display-buffer}
137(@pxref{Displaying Buffers}).
138
139 The functions described here do not accept a buffer as an argument.
140The two ``halves'' of the split window initially display the same buffer
141previously visible in the window that was split.
142
143@deffn Command split-window &optional window size horizontal
144This function splits @var{window} into two windows. The original
145window @var{window} remains the selected window, but occupies only
146part of its former screen area. The rest is occupied by a newly created
147window which is returned as the value of this function.
148
149 If @var{horizontal} is non-@code{nil}, then @var{window} splits into
150two side by side windows. The original window @var{window} keeps the
151leftmost @var{size} columns, and gives the rest of the columns to the
152new window. Otherwise, it splits into windows one above the other, and
153@var{window} keeps the upper @var{size} lines and gives the rest of the
154lines to the new window. The original window is therefore the
c638661f 155left-hand or upper of the two, and the new window is the right-hand or
b1b12a8e
RS
156lower.
157
158 If @var{window} is omitted or @code{nil}, then the selected window is
159split. If @var{size} is omitted or @code{nil}, then @var{window} is
160divided evenly into two parts. (If there is an odd line, it is
161allocated to the new window.) When @code{split-window} is called
162interactively, all its arguments are @code{nil}.
163
164 The following example starts with one window on a screen that is 50
165lines high by 80 columns wide; then the window is split.
166
167@smallexample
168@group
169(setq w (selected-window))
170 @result{} #<window 8 on windows.texi>
171(window-edges) ; @r{Edges in order:}
172 @result{} (0 0 80 50) ; @r{left--top--right--bottom}
173@end group
174
175@group
176;; @r{Returns window created}
3c29caa8 177(setq w2 (split-window w 15))
b1b12a8e
RS
178 @result{} #<window 28 on windows.texi>
179@end group
180@group
181(window-edges w2)
182 @result{} (0 15 80 50) ; @r{Bottom window;}
183 ; @r{top is line 15}
184@end group
185@group
186(window-edges w)
187 @result{} (0 0 80 15) ; @r{Top window}
188@end group
189@end smallexample
190
191The screen looks like this:
192
193@smallexample
194@group
3c29caa8
DH
195 __________
196 | | line 0
b1b12a8e
RS
197 | w |
198 |__________|
199 | | line 15
200 | w2 |
201 |__________|
202 line 50
203 column 0 column 80
204@end group
205@end smallexample
206
207Next, the top window is split horizontally:
208
209@smallexample
210@group
211(setq w3 (split-window w 35 t))
212 @result{} #<window 32 on windows.texi>
213@end group
214@group
215(window-edges w3)
216 @result{} (35 0 80 15) ; @r{Left edge at column 35}
217@end group
218@group
219(window-edges w)
220 @result{} (0 0 35 15) ; @r{Right edge at column 35}
221@end group
222@group
223(window-edges w2)
224 @result{} (0 15 80 50) ; @r{Bottom window unchanged}
225@end group
226@end smallexample
227
bda144f4 228@need 3000
b1b12a8e
RS
229Now, the screen looks like this:
230
231@smallexample
232@group
233 column 35
3c29caa8
DH
234 __________
235 | | | line 0
b1b12a8e
RS
236 | w | w3 |
237 |___|______|
238 | | line 15
239 | w2 |
240 |__________|
241 line 50
242 column 0 column 80
243@end group
244@end smallexample
aeb2c306
JB
245
246Normally, Emacs indicates the border between two side-by-side windows
247with a scroll bar (@pxref{X Frame Parameters,Scroll Bars}) or @samp{|}
248characters. The display table can specify alternative border
249characters; see @ref{Display Tables}.
b1b12a8e
RS
250@end deffn
251
252@deffn Command split-window-vertically size
253This function splits the selected window into two windows, one above
254the other, leaving the selected window with @var{size} lines.
255
256This function is simply an interface to @code{split-windows}.
257Here is the complete function definition for it:
258
259@smallexample
260@group
261(defun split-window-vertically (&optional arg)
9e2b495b 262 "Split current window into two windows, @dots{}"
b1b12a8e
RS
263 (interactive "P")
264 (split-window nil (and arg (prefix-numeric-value arg))))
265@end group
266@end smallexample
267@end deffn
268
269@deffn Command split-window-horizontally size
270This function splits the selected window into two windows
271side-by-side, leaving the selected window with @var{size} columns.
272
273This function is simply an interface to @code{split-windows}. Here is
274the complete definition for @code{split-window-horizontally} (except for
275part of the documentation string):
276
277@smallexample
278@group
279(defun split-window-horizontally (&optional arg)
280 "Split selected window into two windows, side by side..."
281 (interactive "P")
282 (split-window nil (and arg (prefix-numeric-value arg)) t))
283@end group
284@end smallexample
285@end deffn
286
287@defun one-window-p &optional no-mini all-frames
288This function returns non-@code{nil} if there is only one window. The
289argument @var{no-mini}, if non-@code{nil}, means don't count the
290minibuffer even if it is active; otherwise, the minibuffer window is
eaac2be1 291included, if active, in the total number of windows, which is compared
b1b12a8e
RS
292against one.
293
294The argument @var{all-frames} specifies which frames to consider. Here
295are the possible values and their meanings:
296
297@table @asis
298@item @code{nil}
299Count the windows in the selected frame, plus the minibuffer used
300by that frame even if it lies in some other frame.
301
302@item @code{t}
303Count all windows in all existing frames.
304
305@item @code{visible}
306Count all windows in all visible frames.
307
bfe721d1
KH
308@item 0
309Count all windows in all visible or iconified frames.
310
b1b12a8e
RS
311@item anything else
312Count precisely the windows in the selected frame, and no others.
313@end table
314@end defun
315
316@node Deleting Windows
317@section Deleting Windows
318@cindex deleting windows
319
320A window remains visible on its frame unless you @dfn{delete} it by
321calling certain functions that delete windows. A deleted window cannot
322appear on the screen, but continues to exist as a Lisp object until
323there are no references to it. There is no way to cancel the deletion
324of a window aside from restoring a saved window configuration
325(@pxref{Window Configurations}). Restoring a window configuration also
326deletes any windows that aren't part of that configuration.
327
328 When you delete a window, the space it took up is given to one
329adjacent sibling. (In Emacs version 18, the space was divided evenly
330among all the siblings.)
331
332@c Emacs 19 feature
333@defun window-live-p window
334This function returns @code{nil} if @var{window} is deleted, and
335@code{t} otherwise.
336
b22f3a19 337@strong{Warning:} Erroneous information or fatal errors may result from
b1b12a8e
RS
338using a deleted window as if it were live.
339@end defun
340
341@deffn Command delete-window &optional window
342This function removes @var{window} from the display. If @var{window}
343is omitted, then the selected window is deleted. An error is signaled
344if there is only one window when @code{delete-window} is called.
345
346This function returns @code{nil}.
347
348When @code{delete-window} is called interactively, @var{window}
349defaults to the selected window.
350@end deffn
351
352@deffn Command delete-other-windows &optional window
353This function makes @var{window} the only window on its frame, by
354deleting the other windows in that frame. If @var{window} is omitted or
355@code{nil}, then the selected window is used by default.
356
357The result is @code{nil}.
358@end deffn
359
360@deffn Command delete-windows-on buffer &optional frame
361This function deletes all windows showing @var{buffer}. If there are
362no windows showing @var{buffer}, it does nothing.
363
364@code{delete-windows-on} operates frame by frame. If a frame has
365several windows showing different buffers, then those showing
366@var{buffer} are removed, and the others expand to fill the space. If
367all windows in some frame are showing @var{buffer} (including the case
368where there is only one window), then the frame reverts to having a
369single window showing another buffer chosen with @code{other-buffer}.
370@xref{The Buffer List}.
371
372The argument @var{frame} controls which frames to operate on:
373
374@itemize @bullet
375@item
376If it is @code{nil}, operate on the selected frame.
377@item
378If it is @code{t}, operate on all frames.
379@item
380If it is @code{visible}, operate on all visible frames.
bfe721d1
KH
381@item 0
382If it is 0, operate on all visible or iconified frames.
b1b12a8e
RS
383@item
384If it is a frame, operate on that frame.
385@end itemize
386
387This function always returns @code{nil}.
388@end deffn
389
390@node Selecting Windows
391@section Selecting Windows
392@cindex selecting windows
393
394 When a window is selected, the buffer in the window becomes the current
395buffer, and the cursor will appear in it.
396
397@defun selected-window
398This function returns the selected window. This is the window in
399which the cursor appears and to which many commands apply.
400@end defun
401
402@defun select-window window
403This function makes @var{window} the selected window. The cursor then
404appears in @var{window} (on redisplay). The buffer being displayed in
405@var{window} is immediately designated the current buffer.
406
407The return value is @var{window}.
408
409@example
410@group
411(setq w (next-window))
412(select-window w)
413 @result{} #<window 65 on windows.texi>
414@end group
415@end example
416@end defun
417
bfe721d1
KH
418@defmac save-selected-window forms@dots{}
419This macro records the selected window, executes @var{forms}
420in sequence, then restores the earlier selected window.
3c29caa8
DH
421
422This macro does not save or restore anything about the sizes, arrangement
bfe721d1 423or contents of windows; therefore, if the @var{forms} change them,
3c29caa8
DH
424the change persists.
425
426Each frame, at any time, has a window selected within the frame. This
427macro only saves @emph{the} selected window; it does not save anything
428about other frames. If the @var{forms} select some other frame and
429alter the window selected within it, the change persists.
bfe721d1
KH
430@end defmac
431
b1b12a8e
RS
432@cindex finding windows
433 The following functions choose one of the windows on the screen,
434offering various criteria for the choice.
435
436@defun get-lru-window &optional frame
437This function returns the window least recently ``used'' (that is,
438selected). The selected window is always the most recently used window.
439
440The selected window can be the least recently used window if it is the
441only window. A newly created window becomes the least recently used
442window until it is selected. A minibuffer window is never a candidate.
443
c638661f 444The argument @var{frame} controls which windows are considered.
b1b12a8e
RS
445
446@itemize @bullet
447@item
448If it is @code{nil}, consider windows on the selected frame.
449@item
450If it is @code{t}, consider windows on all frames.
451@item
452If it is @code{visible}, consider windows on all visible frames.
453@item
bfe721d1
KH
454If it is 0, consider windows on all visible or iconified frames.
455@item
b1b12a8e
RS
456If it is a frame, consider windows on that frame.
457@end itemize
458@end defun
459
460@defun get-largest-window &optional frame
461This function returns the window with the largest area (height times
462width). If there are no side-by-side windows, then this is the window
463with the most lines. A minibuffer window is never a candidate.
464
465If there are two windows of the same size, then the function returns
c638661f 466the window that is first in the cyclic ordering of windows (see
b1b12a8e
RS
467following section), starting from the selected window.
468
469The argument @var{frame} controls which set of windows are
470considered. See @code{get-lru-window}, above.
471@end defun
472
473@node Cyclic Window Ordering
474@comment node-name, next, previous, up
475@section Cyclic Ordering of Windows
476@cindex cyclic ordering of windows
477@cindex ordering of windows, cyclic
3c29caa8 478@cindex window ordering, cyclic
b1b12a8e
RS
479
480 When you use the command @kbd{C-x o} (@code{other-window}) to select
481the next window, it moves through all the windows on the screen in a
482specific cyclic order. For any given configuration of windows, this
483order never varies. It is called the @dfn{cyclic ordering of windows}.
484
485 This ordering generally goes from top to bottom, and from left to
486right. But it may go down first or go right first, depending on the
487order in which the windows were split.
488
489 If the first split was vertical (into windows one above each other),
490and then the subwindows were split horizontally, then the ordering is
491left to right in the top of the frame, and then left to right in the
492next lower part of the frame, and so on. If the first split was
493horizontal, the ordering is top to bottom in the left part, and so on.
494In general, within each set of siblings at any level in the window tree,
495the order is left to right, or top to bottom.
496
497@defun next-window &optional window minibuf all-frames
498@cindex minibuffer window
499This function returns the window following @var{window} in the cyclic
c638661f
RS
500ordering of windows. This is the window that @kbd{C-x o} would select
501if typed when @var{window} is selected. If @var{window} is the only
b1b12a8e
RS
502window visible, then this function returns @var{window}. If omitted,
503@var{window} defaults to the selected window.
504
505The value of the argument @var{minibuf} determines whether the
506minibuffer is included in the window order. Normally, when
507@var{minibuf} is @code{nil}, the minibuffer is included if it is
508currently active; this is the behavior of @kbd{C-x o}. (The minibuffer
509window is active while the minibuffer is in use. @xref{Minibuffers}.)
510
511If @var{minibuf} is @code{t}, then the cyclic ordering includes the
512minibuffer window even if it is not active.
513
514If @var{minibuf} is neither @code{t} nor @code{nil}, then the minibuffer
515window is not included even if it is active.
516
517The argument @var{all-frames} specifies which frames to consider. Here
518are the possible values and their meanings:
519
520@table @asis
521@item @code{nil}
522Consider all the windows in @var{window}'s frame, plus the minibuffer
523used by that frame even if it lies in some other frame.
524
525@item @code{t}
526Consider all windows in all existing frames.
527
528@item @code{visible}
529Consider all windows in all visible frames. (To get useful results, you
530must ensure @var{window} is in a visible frame.)
531
83abd543 532@item 0
bfe721d1
KH
533Consider all windows in all visible or iconified frames.
534
b1b12a8e
RS
535@item anything else
536Consider precisely the windows in @var{window}'s frame, and no others.
537@end table
538
3c29caa8 539This example assumes there are two windows, both displaying the
b1b12a8e
RS
540buffer @samp{windows.texi}:
541
542@example
543@group
544(selected-window)
545 @result{} #<window 56 on windows.texi>
546@end group
547@group
548(next-window (selected-window))
549 @result{} #<window 52 on windows.texi>
550@end group
551@group
552(next-window (next-window (selected-window)))
553 @result{} #<window 56 on windows.texi>
554@end group
555@end example
556@end defun
557
558@defun previous-window &optional window minibuf all-frames
559This function returns the window preceding @var{window} in the cyclic
560ordering of windows. The other arguments specify which windows to
561include in the cycle, as in @code{next-window}.
562@end defun
563
564@deffn Command other-window count
565This function selects the @var{count}th following window in the cyclic
566order. If count is negative, then it selects the @minus{}@var{count}th
567preceding window. It returns @code{nil}.
568
569In an interactive call, @var{count} is the numeric prefix argument.
570@end deffn
571
572@c Emacs 19 feature
573@defun walk-windows proc &optional minibuf all-frames
574This function cycles through all windows, calling @code{proc}
575once for each window with the window as its sole argument.
576
577The optional arguments @var{minibuf} and @var{all-frames} specify the
578set of windows to include in the scan. See @code{next-window}, above,
579for details.
580@end defun
581
582@node Buffers and Windows
583@section Buffers and Windows
584@cindex examining windows
585@cindex windows, controlling precisely
586@cindex buffers, controlled in windows
587
588 This section describes low-level functions to examine windows or to
589display buffers in windows in a precisely controlled fashion.
590@iftex
591See the following section for
592@end iftex
593@ifinfo
594@xref{Displaying Buffers}, for
595@end ifinfo
596related functions that find a window to use and specify a buffer for it.
597The functions described there are easier to use than these, but they
598employ heuristics in choosing or creating a window; use these functions
599when you need complete control.
600
601@defun set-window-buffer window buffer-or-name
602This function makes @var{window} display @var{buffer-or-name} as its
603contents. It returns @code{nil}.
604
605@example
606@group
607(set-window-buffer (selected-window) "foo")
608 @result{} nil
609@end group
610@end example
611@end defun
612
613@defun window-buffer &optional window
614This function returns the buffer that @var{window} is displaying. If
615@var{window} is omitted, this function returns the buffer for the
616selected window.
617
618@example
619@group
620(window-buffer)
621 @result{} #<buffer windows.texi>
622@end group
623@end example
624@end defun
625
626@defun get-buffer-window buffer-or-name &optional all-frames
627This function returns a window currently displaying
628@var{buffer-or-name}, or @code{nil} if there is none. If there are
629several such windows, then the function returns the first one in the
630cyclic ordering of windows, starting from the selected window.
631@xref{Cyclic Window Ordering}.
632
633The argument @var{all-frames} controls which windows to consider.
634
635@itemize @bullet
636@item
637If it is @code{nil}, consider windows on the selected frame.
638@item
639If it is @code{t}, consider windows on all frames.
640@item
641If it is @code{visible}, consider windows on all visible frames.
642@item
bfe721d1
KH
643If it is 0, consider windows on all visible or iconified frames.
644@item
b1b12a8e
RS
645If it is a frame, consider windows on that frame.
646@end itemize
647@end defun
648
3c29caa8
DH
649@defun get-buffer-window-list buffer-or-name &optional minibuf all-frames
650This function returns a list of all the windows currently displaying
651@var{buffer-or-name}.
652
653The two optional arguments work like the optional arguments of
654@code{next-window} (@pxref{Cyclic Window Ordering}); they are @emph{not}
655like the single optional argument of @code{get-buffer-window}. Perhaps
656we should change @code{get-buffer-window} in the future to make it
657compatible with the other functions.
658
659The argument @var{all-frames} controls which windows to consider.
660
661@itemize @bullet
662@item
663If it is @code{nil}, consider windows on the selected frame.
664@item
665If it is @code{t}, consider windows on all frames.
666@item
667If it is @code{visible}, consider windows on all visible frames.
668@item
669If it is 0, consider windows on all visible or iconified frames.
670@item
671If it is a frame, consider windows on that frame.
672@end itemize
673@end defun
674
b1b12a8e
RS
675@node Displaying Buffers
676@section Displaying Buffers in Windows
677@cindex switching to a buffer
678@cindex displaying a buffer
679
680 In this section we describe convenient functions that choose a window
681automatically and use it to display a specified buffer. These functions
682can also split an existing window in certain circumstances. We also
683describe variables that parameterize the heuristics used for choosing a
684window.
685@iftex
686See the preceding section for
687@end iftex
688@ifinfo
689@xref{Buffers and Windows}, for
690@end ifinfo
691low-level functions that give you more precise control.
692
693 Do not use the functions in this section in order to make a buffer
694current so that a Lisp program can access or modify it; they are too
695drastic for that purpose, since they change the display of buffers in
696windows, which is gratuitous and will surprise the user. Instead, use
697@code{set-buffer} (@pxref{Current Buffer}) and @code{save-excursion}
698(@pxref{Excursions}), which designate buffers as current for programmed
699access without affecting the display of buffers in windows.
700
701@deffn Command switch-to-buffer buffer-or-name &optional norecord
702This function makes @var{buffer-or-name} the current buffer, and also
703displays the buffer in the selected window. This means that a human can
704see the buffer and subsequent keyboard commands will apply to it.
705Contrast this with @code{set-buffer}, which makes @var{buffer-or-name}
706the current buffer but does not display it in the selected window.
707@xref{Current Buffer}.
708
22697dac
KH
709If @var{buffer-or-name} does not identify an existing buffer, then a new
710buffer by that name is created. The major mode for the new buffer is
711set according to the variable @code{default-major-mode}. @xref{Auto
712Major Mode}.
b1b12a8e
RS
713
714Normally the specified buffer is put at the front of the buffer list.
715This affects the operation of @code{other-buffer}. However, if
716@var{norecord} is non-@code{nil}, this is not done. @xref{The Buffer
717List}.
718
719The @code{switch-to-buffer} function is often used interactively, as
720the binding of @kbd{C-x b}. It is also used frequently in programs. It
721always returns @code{nil}.
722@end deffn
723
724@deffn Command switch-to-buffer-other-window buffer-or-name
725This function makes @var{buffer-or-name} the current buffer and
726displays it in a window not currently selected. It then selects that
727window. The handling of the buffer is the same as in
728@code{switch-to-buffer}.
729
c638661f
RS
730The currently selected window is absolutely never used to do the job.
731If it is the only window, then it is split to make a distinct window for
732this purpose. If the selected window is already displaying the buffer,
733then it continues to do so, but another window is nonetheless found to
734display it in as well.
b1b12a8e
RS
735@end deffn
736
737@defun pop-to-buffer buffer-or-name &optional other-window
738This function makes @var{buffer-or-name} the current buffer and
739switches to it in some window, preferably not the window previously
740selected. The ``popped-to'' window becomes the selected window within
741its frame.
742
743If the variable @code{pop-up-frames} is non-@code{nil},
744@code{pop-to-buffer} looks for a window in any visible frame already
745displaying the buffer; if there is one, it returns that window and makes
746it be selected within its frame. If there is none, it creates a new
747frame and displays the buffer in it.
748
749If @code{pop-up-frames} is @code{nil}, then @code{pop-to-buffer}
750operates entirely within the selected frame. (If the selected frame has
751just a minibuffer, @code{pop-to-buffer} operates within the most
752recently selected frame that was not just a minibuffer.)
753
754If the variable @code{pop-up-windows} is non-@code{nil}, windows may
755be split to create a new window that is different from the original
756window. For details, see @ref{Choosing Window}.
757
758If @var{other-window} is non-@code{nil}, @code{pop-to-buffer} finds or
759creates another window even if @var{buffer-or-name} is already visible
760in the selected window. Thus @var{buffer-or-name} could end up
761displayed in two windows. On the other hand, if @var{buffer-or-name} is
762already displayed in the selected window and @var{other-window} is
763@code{nil}, then the selected window is considered sufficient display
764for @var{buffer-or-name}, so that nothing needs to be done.
765
bfe721d1
KH
766All the variables that affect @code{display-buffer} affect
767@code{pop-to-buffer} as well. @xref{Choosing Window}.
768
b1b12a8e 769If @var{buffer-or-name} is a string that does not name an existing
22697dac
KH
770buffer, a buffer by that name is created. The major mode for the new
771buffer is set according to the variable @code{default-major-mode}.
772@xref{Auto Major Mode}.
b1b12a8e
RS
773@end defun
774
bfe721d1
KH
775@deffn Command replace-buffer-in-windows buffer
776This function replaces @var{buffer} with some other buffer in all
777windows displaying it. The other buffer used is chosen with
778@code{other-buffer}. In the usual applications of this function, you
779don't care which other buffer is used; you just want to make sure that
780@var{buffer} is no longer displayed.
781
782This function returns @code{nil}.
783@end deffn
784
b1b12a8e
RS
785@node Choosing Window
786@section Choosing a Window for Display
787
c638661f 788 This section describes the basic facility that chooses a window to
b1b12a8e
RS
789display a buffer in---@code{display-buffer}. All the higher-level
790functions and commands use this subroutine. Here we describe how to use
791@code{display-buffer} and how to customize it.
792
793@deffn Command display-buffer buffer-or-name &optional not-this-window
794This command makes @var{buffer-or-name} appear in some window, like
795@code{pop-to-buffer}, but it does not select that window and does not
796make the buffer current. The identity of the selected window is
797unaltered by this function.
798
799If @var{not-this-window} is non-@code{nil}, it means to display the
800specified buffer in a window other than the selected one, even if it is
801already on display in the selected window. This can cause the buffer to
802appear in two windows at once. Otherwise, if @var{buffer-or-name} is
803already being displayed in any window, that is good enough, so this
804function does nothing.
805
806@code{display-buffer} returns the window chosen to display
807@var{buffer-or-name}.
808
809Precisely how @code{display-buffer} finds or creates a window depends on
810the variables described below.
811@end deffn
812
813@defopt pop-up-windows
814This variable controls whether @code{display-buffer} makes new windows.
815If it is non-@code{nil} and there is only one window, then that window
816is split. If it is @code{nil}, then @code{display-buffer} does not
817split the single window, but uses it whole.
818@end defopt
819
820@defopt split-height-threshold
821This variable determines when @code{display-buffer} may split a window,
822if there are multiple windows. @code{display-buffer} always splits the
823largest window if it has at least this many lines. If the largest
824window is not this tall, it is split only if it is the sole window and
825@code{pop-up-windows} is non-@code{nil}.
826@end defopt
827
828@c Emacs 19 feature
829@defopt pop-up-frames
830This variable controls whether @code{display-buffer} makes new frames.
831If it is non-@code{nil}, @code{display-buffer} looks for an existing
832window already displaying the desired buffer, on any visible frame. If
833it finds one, it returns that window. Otherwise it makes a new frame.
834The variables @code{pop-up-windows} and @code{split-height-threshold} do
835not matter if @code{pop-up-frames} is non-@code{nil}.
836
837If @code{pop-up-frames} is @code{nil}, then @code{display-buffer} either
838splits a window or reuses one.
839
840@xref{Frames}, for more information.
841@end defopt
842
843@c Emacs 19 feature
844@defvar pop-up-frame-function
845This variable specifies how to make a new frame if @code{pop-up-frames}
846is non-@code{nil}.
847
848Its value should be a function of no arguments. When
849@code{display-buffer} makes a new frame, it does so by calling that
850function, which should return a frame. The default value of the
c638661f 851variable is a function that creates a frame using parameters from
b1b12a8e
RS
852@code{pop-up-frame-alist}.
853@end defvar
854
855@defvar pop-up-frame-alist
856This variable holds an alist specifying frame parameters used when
857@code{display-buffer} makes a new frame. @xref{Frame Parameters}, for
858more information about frame parameters.
859@end defvar
860
c2264295
RS
861@defvar special-display-buffer-names
862A list of buffer names for buffers that should be displayed specially.
863If the buffer's name is in this list, @code{display-buffer} handles the
864buffer specially.
865
866By default, special display means to give the buffer a dedicated frame.
bfe721d1
KH
867
868If an element is a list, instead of a string, then the @sc{car} of the
869list is the buffer name, and the rest of the list says how to create the
870frame. There are two possibilities for the rest of the list. It can be
871an alist, specifying frame parameters, or it can contain a function and
872arguments to give to it. (The function's first argument is always the
873buffer to be displayed; the arguments from the list come after that.)
c2264295
RS
874@end defvar
875
876@defvar special-display-regexps
877A list of regular expressions that specify buffers that should be
878displayed specially. If the buffer's name matches any of the regular
879expressions in this list, @code{display-buffer} handles the buffer
880specially.
881
882By default, special display means to give the buffer a dedicated frame.
bfe721d1
KH
883
884If an element is a list, instead of a string, then the @sc{car} of the
885list is the regular expression, and the rest of the list says how to
886create the frame. See above, under @code{special-display-buffer-names}.
c2264295
RS
887@end defvar
888
889@defvar special-display-function
890This variable holds the function to call to display a buffer specially.
891It receives the buffer as an argument, and should return the window in
892which it is displayed.
893
894The default value of this variable is
895@code{special-display-popup-frame}.
896@end defvar
897
898@defun special-display-popup-frame buffer
899This function makes @var{buffer} visible in a frame of its own. If
900@var{buffer} is already displayed in a window in some frame, it makes
901the frame visible and raises it, to use that window. Otherwise, it
902creates a frame that will be dedicated to @var{buffer}.
c638661f
RS
903
904This function uses an existing window displaying @var{buffer} whether or
905not it is in a frame of its own; but if you set up the above variables
906in your init file, before @var{buffer} was created, then presumably the
907window was previously made by this function.
c2264295
RS
908@end defun
909
910@defopt special-display-frame-alist
911This variable holds frame parameters for
912@code{special-display-popup-frame} to use when it creates a frame.
913@end defopt
914
864bd34b 915@defopt same-window-buffer-names
bfe721d1
KH
916A list of buffer names for buffers that should be displayed in the
917selected window. If the buffer's name is in this list,
918@code{display-buffer} handles the buffer by switching to it in the
919selected window.
864bd34b 920@end defopt
bfe721d1 921
864bd34b 922@defopt same-window-regexps
bfe721d1
KH
923A list of regular expressions that specify buffers that should be
924displayed in the selected window. If the buffer's name matches any of
925the regular expressions in this list, @code{display-buffer} handles the
926buffer by switching to it in the selected window.
864bd34b 927@end defopt
bfe721d1 928
b1b12a8e
RS
929@c Emacs 19 feature
930@defvar display-buffer-function
931This variable is the most flexible way to customize the behavior of
932@code{display-buffer}. If it is non-@code{nil}, it should be a function
933that @code{display-buffer} calls to do the work. The function should
934accept two arguments, the same two arguments that @code{display-buffer}
935received. It should choose or create a window, display the specified
936buffer, and then return the window.
937
938This hook takes precedence over all the other options and hooks
939described above.
940@end defvar
941
942@c Emacs 19 feature
943@cindex dedicated window
944A window can be marked as ``dedicated'' to its buffer. Then
945@code{display-buffer} does not try to use that window.
946
947@defun window-dedicated-p window
948This function returns @code{t} if @var{window} is marked as dedicated;
949otherwise @code{nil}.
950@end defun
951
952@defun set-window-dedicated-p window flag
953This function marks @var{window} as dedicated if @var{flag} is
954non-@code{nil}, and nondedicated otherwise.
955@end defun
956
957@node Window Point
958@section Windows and Point
959@cindex window position
960@cindex window point
961@cindex position in window
962@cindex point in window
963
964 Each window has its own value of point, independent of the value of
965point in other windows displaying the same buffer. This makes it useful
966to have multiple windows showing one buffer.
967
968@itemize @bullet
969@item
970The window point is established when a window is first created; it is
971initialized from the buffer's point, or from the window point of another
972window opened on the buffer if such a window exists.
973
974@item
975Selecting a window sets the value of point in its buffer to the window's
976value of point. Conversely, deselecting a window sets the window's
977value of point from that of the buffer. Thus, when you switch between
978windows that display a given buffer, the point value for the selected
979window is in effect in the buffer, while the point values for the other
980windows are stored in those windows.
981
982@item
983As long as the selected window displays the current buffer, the window's
984point and the buffer's point always move together; they remain equal.
985
986@item
987@xref{Positions}, for more details on buffer positions.
988@end itemize
989
990 As far as the user is concerned, point is where the cursor is, and
991when the user switches to another buffer, the cursor jumps to the
992position of point in that buffer.
993
994@defun window-point window
995This function returns the current position of point in @var{window}.
996For a nonselected window, this is the value point would have (in that
997window's buffer) if that window were selected.
998
999When @var{window} is the selected window and its buffer is also the
1000current buffer, the value returned is the same as point in that buffer.
1001
1002Strictly speaking, it would be more correct to return the
1003``top-level'' value of point, outside of any @code{save-excursion}
1004forms. But that value is hard to find.
1005@end defun
1006
1007@defun set-window-point window position
1008This function positions point in @var{window} at position
1009@var{position} in @var{window}'s buffer.
1010@end defun
1011
1012@node Window Start
1013@section The Window Start Position
1014
1015 Each window contains a marker used to keep track of a buffer position
c638661f 1016that specifies where in the buffer display should start. This position
b1b12a8e
RS
1017is called the @dfn{display-start} position of the window (or just the
1018@dfn{start}). The character after this position is the one that appears
1019at the upper left corner of the window. It is usually, but not
1020inevitably, at the beginning of a text line.
1021
1022@defun window-start &optional window
1023@cindex window top line
1024This function returns the display-start position of window
1025@var{window}. If @var{window} is @code{nil}, the selected window is
3c29caa8 1026used. For example,
b1b12a8e
RS
1027
1028@example
1029@group
1030(window-start)
1031 @result{} 7058
1032@end group
1033@end example
1034
c638661f 1035When you create a window, or display a different buffer in it, the
b1b12a8e
RS
1036display-start position is set to a display-start position recently used
1037for the same buffer, or 1 if the buffer doesn't have any.
1038
ea951766
RS
1039Redisplay updates the window-start position (if you have not specified
1040it explicitly since the previous redisplay) so that point appears on the
1041screen. Nothing except redisplay automatically changes the window-start
1042position; if you move point, do not expect the window-start position to
1043change in response until after the next redisplay.
1044
1045For a realistic example of using @code{window-start}, see the
1046description of @code{count-lines} in @ref{Text Lines}.
b1b12a8e
RS
1047@end defun
1048
1049@defun window-end &optional window
1050This function returns the position of the end of the display in window
1051@var{window}. If @var{window} is @code{nil}, the selected window is
1052used.
c638661f 1053
6c7418db
RS
1054Simply changing the buffer text or moving point does not update the
1055value that @code{window-end} returns. The value is updated only when
1056Emacs redisplays and redisplay actually finishes.
1057
c638661f 1058If the last redisplay of @var{window} was preempted, and did not finish,
a283f4a3
RS
1059Emacs does not know the position of the end of display in that window.
1060In that case, this function returns a value that is not correct. In a
1061future version, @code{window-end} will return @code{nil} in that case.
1062@ignore
c638661f
RS
1063in that case, this function returns @code{nil}. You can compute where
1064the end of the window @emph{would} have been, if redisplay had finished,
1065like this:
1066
1067@example
1068(save-excursion
1069 (goto-char (window-start window))
1070 (vertical-motion (1- (window-height window))
1071 window)
1072 (point))
1073@end example
a283f4a3 1074@end ignore
b1b12a8e
RS
1075@end defun
1076
1077@defun set-window-start window position &optional noforce
1078This function sets the display-start position of @var{window} to
c638661f 1079@var{position} in @var{window}'s buffer. It returns @var{position}.
b1b12a8e
RS
1080
1081The display routines insist that the position of point be visible when a
1082buffer is displayed. Normally, they change the display-start position
1083(that is, scroll the window) whenever necessary to make point visible.
1084However, if you specify the start position with this function using
1085@code{nil} for @var{noforce}, it means you want display to start at
1086@var{position} even if that would put the location of point off the
1087screen. If this does place point off screen, the display routines move
1088point to the left margin on the middle line in the window.
1089
1090For example, if point @w{is 1} and you set the start of the window @w{to
10912}, then point would be ``above'' the top of the window. The display
1092routines will automatically move point if it is still 1 when redisplay
1093occurs. Here is an example:
1094
1095@example
1096@group
1097;; @r{Here is what @samp{foo} looks like before executing}
1098;; @r{the @code{set-window-start} expression.}
1099@end group
1100
1101@group
1102---------- Buffer: foo ----------
1103@point{}This is the contents of buffer foo.
11042
11053
11064
11075
11086
1109---------- Buffer: foo ----------
1110@end group
1111
1112@group
1113(set-window-start
1114 (selected-window)
1115 (1+ (window-start)))
1116@result{} 2
1117@end group
1118
1119@group
1120;; @r{Here is what @samp{foo} looks like after executing}
1121;; @r{the @code{set-window-start} expression.}
1122---------- Buffer: foo ----------
1123his is the contents of buffer foo.
11242
11253
1126@point{}4
11275
11286
1129---------- Buffer: foo ----------
1130@end group
1131@end example
1132
1133If @var{noforce} is non-@code{nil}, and @var{position} would place point
1134off screen at the next redisplay, then redisplay computes a new window-start
1135position that works well with point, and thus @var{position} is not used.
b1b12a8e
RS
1136@end defun
1137
1138@defun pos-visible-in-window-p &optional position window
1139This function returns @code{t} if @var{position} is within the range
1140of text currently visible on the screen in @var{window}. It returns
1141@code{nil} if @var{position} is scrolled vertically out of view. The
1142argument @var{position} defaults to the current position of point;
1143@var{window}, to the selected window. Here is an example:
1144
1145@example
1146@group
1147(or (pos-visible-in-window-p
1148 (point) (selected-window))
1149 (recenter 0))
1150@end group
1151@end example
1152
1153The @code{pos-visible-in-window-p} function considers only vertical
1154scrolling. If @var{position} is out of view only because @var{window}
1155has been scrolled horizontally, @code{pos-visible-in-window-p} returns
1156@code{t}. @xref{Horizontal Scrolling}.
1157@end defun
1158
1159@node Vertical Scrolling
1160@section Vertical Scrolling
1161@cindex vertical scrolling
1162@cindex scrolling vertically
1163
1164 Vertical scrolling means moving the text up or down in a window. It
1165works by changing the value of the window's display-start location. It
1166may also change the value of @code{window-point} to keep it on the
1167screen.
1168
1169 In the commands @code{scroll-up} and @code{scroll-down}, the directions
1170``up'' and ``down'' refer to the motion of the text in the buffer at which
1171you are looking through the window. Imagine that the text is
1172written on a long roll of paper and that the scrolling commands move the
1173paper up and down. Thus, if you are looking at text in the middle of a
1174buffer and repeatedly call @code{scroll-down}, you will eventually see
1175the beginning of the buffer.
1176
1177 Some people have urged that the opposite convention be used: they
1178imagine that the window moves over text that remains in place. Then
1179``down'' commands would take you to the end of the buffer. This view is
1180more consistent with the actual relationship between windows and the
1181text in the buffer, but it is less like what the user sees. The
1182position of a window on the terminal does not move, and short scrolling
1183commands clearly move the text up or down on the screen. We have chosen
1184names that fit the user's point of view.
1185
1186 The scrolling functions (aside from @code{scroll-other-window}) have
1187unpredictable results if the current buffer is different from the buffer
1188that is displayed in the selected window. @xref{Current Buffer}.
1189
1190@deffn Command scroll-up &optional count
1191This function scrolls the text in the selected window upward
1192@var{count} lines. If @var{count} is negative, scrolling is actually
1193downward.
1194
1195If @var{count} is @code{nil} (or omitted), then the length of scroll
1196is @code{next-screen-context-lines} lines less than the usable height of
1197the window (not counting its mode line).
1198
1199@code{scroll-up} returns @code{nil}.
1200@end deffn
1201
1202@deffn Command scroll-down &optional count
1203This function scrolls the text in the selected window downward
1204@var{count} lines. If @var{count} is negative, scrolling is actually
1205upward.
1206
1207If @var{count} is omitted or @code{nil}, then the length of the scroll
1208is @code{next-screen-context-lines} lines less than the usable height of
c638661f 1209the window (not counting its mode line).
b1b12a8e
RS
1210
1211@code{scroll-down} returns @code{nil}.
1212@end deffn
1213
1214@deffn Command scroll-other-window &optional count
1215This function scrolls the text in another window upward @var{count}
1216lines. Negative values of @var{count}, or @code{nil}, are handled
1217as in @code{scroll-up}.
1218
b1b12a8e
RS
1219You can specify a buffer to scroll with the variable
1220@code{other-window-scroll-buffer}. When the selected window is the
1221minibuffer, the next window is normally the one at the top left corner.
1222You can specify a different window to scroll with the variable
1223@code{minibuffer-scroll-window}. This variable has no effect when any
1224other window is selected. @xref{Minibuffer Misc}.
1225
1226When the minibuffer is active, it is the next window if the selected
1227window is the one at the bottom right corner. In this case,
1228@code{scroll-other-window} attempts to scroll the minibuffer. If the
1229minibuffer contains just one line, it has nowhere to scroll to, so the
1230line reappears after the echo area momentarily displays the message
1231``Beginning of buffer''.
1232@end deffn
1233
1234@c Emacs 19 feature
1235@defvar other-window-scroll-buffer
1236If this variable is non-@code{nil}, it tells @code{scroll-other-window}
1237which buffer to scroll.
1238@end defvar
1239
1240@defopt scroll-step
1241This variable controls how scrolling is done automatically when point
1242moves off the screen. If the value is zero, then redisplay scrolls the
1243text to center point vertically in the window. If the value is a
1244positive integer @var{n}, then redisplay brings point back on screen by
1245scrolling @var{n} lines in either direction, if possible; otherwise, it
c638661f 1246centers point. The default value is zero.
b1b12a8e
RS
1247@end defopt
1248
1249@defopt next-screen-context-lines
1250The value of this variable is the number of lines of continuity to
1251retain when scrolling by full screens. For example, @code{scroll-up}
1252with an argument of @code{nil} scrolls so that this many lines at the
1253bottom of the window appear instead at the top. The default value is
1254@code{2}.
1255@end defopt
1256
1257@deffn Command recenter &optional count
1258@cindex centering point
1259This function scrolls the selected window to put the text where point
1260is located at a specified vertical position within the window.
1261
1262If @var{count} is a nonnegative number, it puts the line containing
1263point @var{count} lines down from the top of the window. If @var{count}
1264is a negative number, then it counts upward from the bottom of the
1265window, so that @minus{}1 stands for the last usable line in the window.
1266If @var{count} is a non-@code{nil} list, then it stands for the line in
1267the middle of the window.
1268
1269If @var{count} is @code{nil}, @code{recenter} puts the line containing
1270point in the middle of the window, then clears and redisplays the entire
1271selected frame.
1272
1273When @code{recenter} is called interactively, @var{count} is the raw
1274prefix argument. Thus, typing @kbd{C-u} as the prefix sets the
1275@var{count} to a non-@code{nil} list, while typing @kbd{C-u 4} sets
1276@var{count} to 4, which positions the current line four lines from the
1277top.
1278
c638661f
RS
1279With an argument of zero, @code{recenter} positions the current line at
1280the top of the window. This action is so handy that some people make a
1281separate key binding to do this. For example,
b1b12a8e
RS
1282
1283@example
1284@group
1285(defun line-to-top-of-window ()
1286 "Scroll current line to top of window.
1287Replaces three keystroke sequence C-u 0 C-l."
3c29caa8 1288 (interactive)
b1b12a8e
RS
1289 (recenter 0))
1290
3c29caa8 1291(global-set-key [kp-multiply] 'line-to-top-of-window)
b1b12a8e
RS
1292@end group
1293@end example
1294@end deffn
1295
3c29caa8
DH
1296@node Scrolling Hooks
1297@section Hooks for Vertical Scrolling
1298
1299This section describes how a Lisp program can take action whenever a
1300window displays a different part of its buffer or a different buffer.
1301There are three actions that can change this: scrolling the window,
1302switching buffers in the window, and changing the size of the window.
1303The first two actions run @code{window-scroll-functions}; the last runs
1304@code{window-size-change-functions}. The paradigmatic use of these
1305hooks is Lazy Lock mode; see @ref{Support Modes, Lazy Lock, Font Lock
1306Support Modes, emacs, The GNU Emacs Manual}.
1307
1308@defvar window-scroll-functions
1309This variable holds a list of functions that Emacs should call before
1310redisplaying a window with scrolling. It is not a normal hook, because
1311each function is called with two arguments: the window, and its new
1312display-start position.
1313
1314Displaying a different buffer in the window also runs these functions.
1315
1316These functions cannot expect @code{window-end} (@pxref{Window Start})
1317to return a meaningful value, because that value is updated only by
1318redisplaying the buffer. So if one of these functions needs to know the
1319last character that will fit in the window with its current
1320display-start position, it has to find that character using
1321@code{vertical-motion} (@pxref{Screen Lines}).
1322@end defvar
1323
1324@defvar window-size-change-functions
1325This variable holds a list of functions to be called if the size of any
1326window changes for any reason. The functions are called just once per
1327redisplay, and just once for each frame on which size changes have
1328occurred.
1329
1330Each function receives the frame as its sole argument. There is no
1331direct way to find out which windows on that frame have changed size, or
1332precisely how. However, if a size-change function records, at each
1333call, the existing windows and their sizes, it can also compare the
1334present sizes and the previous sizes.
1335
1336Creating or deleting windows counts as a size change, and therefore
1337causes these functions to be called. Changing the frame size also
1338counts, because it changes the sizes of the existing windows.
1339
1340It is not a good idea to use @code{save-window-excursion} (@pxref{Window
1341Configurations}) in these functions, because that always counts as a
1342size change, and it would cause these functions to be called over and
1343over. In most cases, @code{save-selected-window} (@pxref{Selecting
1344Windows}) is what you need here.
1345@end defvar
1346
b1b12a8e
RS
1347@node Horizontal Scrolling
1348@section Horizontal Scrolling
1349@cindex horizontal scrolling
1350
1351 Because we read English first from top to bottom and second from left
1352to right, horizontal scrolling is not like vertical scrolling. Vertical
1353scrolling involves selection of a contiguous portion of text to display.
1354Horizontal scrolling causes part of each line to go off screen. The
1355amount of horizontal scrolling is therefore specified as a number of
1356columns rather than as a position in the buffer. It has nothing to do
1357with the display-start position returned by @code{window-start}.
1358
1359 Usually, no horizontal scrolling is in effect; then the leftmost
1360column is at the left edge of the window. In this state, scrolling to
1361the right is meaningless, since there is no data to the left of the
1362screen to be revealed by it; so this is not allowed. Scrolling to the
1363left is allowed; it scrolls the first columns of text off the edge of
1364the window and can reveal additional columns on the right that were
1365truncated before. Once a window has a nonzero amount of leftward
1366horizontal scrolling, you can scroll it back to the right, but only so
1367far as to reduce the net horizontal scroll to zero. There is no limit
1368to how far left you can scroll, but eventually all the text will
1369disappear off the left edge.
1370
1371@deffn Command scroll-left count
1372This function scrolls the selected window @var{count} columns to the
1373left (or to the right if @var{count} is negative). The return value is
1374the total amount of leftward horizontal scrolling in effect after the
c638661f 1375change---just like the value returned by @code{window-hscroll} (below).
b1b12a8e
RS
1376@end deffn
1377
1378@deffn Command scroll-right count
1379This function scrolls the selected window @var{count} columns to the
1380right (or to the left if @var{count} is negative). The return value is
1381the total amount of leftward horizontal scrolling in effect after the
c638661f 1382change---just like the value returned by @code{window-hscroll} (below).
b1b12a8e
RS
1383
1384Once you scroll a window as far right as it can go, back to its normal
1385position where the total leftward scrolling is zero, attempts to scroll
1386any farther right have no effect.
1387@end deffn
1388
1389@defun window-hscroll &optional window
1390This function returns the total leftward horizontal scrolling of
1391@var{window}---the number of columns by which the text in @var{window}
1392is scrolled left past the left margin.
1393
1394The value is never negative. It is zero when no horizontal scrolling
1395has been done in @var{window} (which is usually the case).
1396
1397If @var{window} is @code{nil}, the selected window is used.
1398
1399@example
1400@group
1401(window-hscroll)
1402 @result{} 0
1403@end group
1404@group
1405(scroll-left 5)
1406 @result{} 5
1407@end group
1408@group
1409(window-hscroll)
1410 @result{} 5
1411@end group
1412@end example
1413@end defun
1414
1415@defun set-window-hscroll window columns
1416This function sets the number of columns from the left margin that
1417@var{window} is scrolled to the value of @var{columns}. The argument
1418@var{columns} should be zero or positive; if not, it is taken as zero.
1419
1420The value returned is @var{columns}.
1421
1422@example
1423@group
1424(set-window-hscroll (selected-window) 10)
1425 @result{} 10
1426@end group
1427@end example
1428@end defun
1429
1430 Here is how you can determine whether a given position @var{position}
1431is off the screen due to horizontal scrolling:
1432
1433@example
1434@group
c638661f 1435(defun hscroll-on-screen (window position)
3c29caa8 1436 (save-excursion
c638661f 1437 (goto-char position)
3c29caa8 1438 (and
c638661f
RS
1439 (>= (- (current-column) (window-hscroll window)) 0)
1440 (< (- (current-column) (window-hscroll window))
1441 (window-width window)))))
b1b12a8e
RS
1442@end group
1443@end example
1444
1445@node Size of Window
1446@section The Size of a Window
1447@cindex window size
1448@cindex size of window
1449
1450 An Emacs window is rectangular, and its size information consists of
1451the height (the number of lines) and the width (the number of character
1452positions in each line). The mode line is included in the height. But
1453the width does not count the scroll bar or the column of @samp{|}
c638661f 1454characters that separates side-by-side windows.
b1b12a8e
RS
1455
1456 The following three functions return size information about a window:
1457
1458@defun window-height &optional window
1459This function returns the number of lines in @var{window}, including
1460its mode line. If @var{window} fills its entire frame, this is one less
1461than the value of @code{frame-height} on that frame (since the last line
1462is always reserved for the minibuffer).
1463
1464If @var{window} is @code{nil}, the function uses the selected window.
1465
1466@example
1467@group
1468(window-height)
1469 @result{} 23
1470@end group
1471@group
1472(split-window-vertically)
1473 @result{} #<window 4 on windows.texi>
1474@end group
1475@group
1476(window-height)
1477 @result{} 11
1478@end group
1479@end example
1480@end defun
1481
1482@defun window-width &optional window
1483This function returns the number of columns in @var{window}. If
1484@var{window} fills its entire frame, this is the same as the value of
1485@code{frame-width} on that frame. The width does not include the
1486window's scroll bar or the column of @samp{|} characters that separates
1487side-by-side windows.
1488
1489If @var{window} is @code{nil}, the function uses the selected window.
1490
1491@example
1492@group
1493(window-width)
1494 @result{} 80
1495@end group
1496@end example
1497@end defun
1498
1499@defun window-edges &optional window
1500This function returns a list of the edge coordinates of @var{window}.
1501If @var{window} is @code{nil}, the selected window is used.
1502
1503The order of the list is @code{(@var{left} @var{top} @var{right}
1504@var{bottom})}, all elements relative to 0, 0 at the top left corner of
1505the frame. The element @var{right} of the value is one more than the
1506rightmost column used by @var{window}, and @var{bottom} is one more than
1507the bottommost row used by @var{window} and its mode-line.
1508
1509When you have side-by-side windows, the right edge value for a window
1510with a neighbor on the right includes the width of the separator between
1511the window and that neighbor. This separator may be a column of
1512@samp{|} characters or it may be a scroll bar. Since the width of the
1513window does not include this separator, the width does not equal the
1514difference between the right and left edges in this case.
1515
1516Here is the result obtained on a typical 24-line terminal with just one
1517window:
1518
1519@example
1520@group
1521(window-edges (selected-window))
1522 @result{} (0 0 80 23)
1523@end group
1524@end example
1525
c638661f
RS
1526@noindent
1527The bottom edge is at line 23 because the last line is the echo area.
1528
fb1d9004
RS
1529If @var{window} is at the upper left corner of its frame, then
1530@var{bottom} is the same as the value of @code{(window-height)},
1531@var{right} is almost the same as the value of
1532@code{(window-width)}@footnote{They are not exactly equal because
1533@var{right} includes the vertical separator line or scroll bar, while
1534@code{(window-width)} does not.}, and @var{top} and @var{left} are zero.
1535For example, the edges of the following window are @w{@samp{0 0 5 8}}.
1536Assuming that the frame has more than 8 columns, the last column of the
1537window (column 7) holds a border rather than text. The last row (row 4)
1538holds the mode line, shown here with @samp{xxxxxxxxx}.
b1b12a8e
RS
1539
1540@example
1541@group
3c29caa8 1542 0
b1b12a8e 1543 _______
3c29caa8
DH
1544 0 | |
1545 | |
1546 | |
1547 | |
b1b12a8e
RS
1548 xxxxxxxxx 4
1549
3c29caa8 1550 7
b1b12a8e
RS
1551@end group
1552@end example
1553
1554When there are side-by-side windows, any window not at the right edge of
1555its frame has a separator in its last column or columns. The separator
1556counts as one or two columns in the width of the window. A window never
1557includes a separator on its left, since that belongs to the window to
1558the left.
1559
1560In the following example, let's suppose that the frame is 7
1561columns wide. Then the edges of the left window are @w{@samp{0 0 4 3}}
1562and the edges of the right window are @w{@samp{4 0 7 3}}.
1563
1564@example
1565@group
1566 ___ ___
3c29caa8
DH
1567 | | |
1568 | | |
1569 xxxxxxxxx
b1b12a8e
RS
1570
1571 0 34 7
1572@end group
1573@end example
1574@end defun
1575
1576@node Resizing Windows
1577@section Changing the Size of a Window
1578@cindex window resizing
1579@cindex changing window size
1580@cindex window size, changing
1581
1582 The window size functions fall into two classes: high-level commands
1583that change the size of windows and low-level functions that access
1584window size. Emacs does not permit overlapping windows or gaps between
1585windows, so resizing one window affects other windows.
1586
1587@deffn Command enlarge-window size &optional horizontal
c638661f 1588This function makes the selected window @var{size} lines taller,
b1b12a8e
RS
1589stealing lines from neighboring windows. It takes the lines from one
1590window at a time until that window is used up, then takes from another.
1591If a window from which lines are stolen shrinks below
1592@code{window-min-height} lines, that window disappears.
1593
1594If @var{horizontal} is non-@code{nil}, this function makes
1595@var{window} wider by @var{size} columns, stealing columns instead of
1596lines. If a window from which columns are stolen shrinks below
1597@code{window-min-width} columns, that window disappears.
1598
c638661f
RS
1599If the requested size would exceed that of the window's frame, then the
1600function makes the window occupy the entire height (or width) of the
1601frame.
b1b12a8e
RS
1602
1603If @var{size} is negative, this function shrinks the window by
1604@minus{}@var{size} lines or columns. If that makes the window smaller
1605than the minimum size (@code{window-min-height} and
1606@code{window-min-width}), @code{enlarge-window} deletes the window.
1607
3c29caa8 1608@code{enlarge-window} returns @code{nil}.
b1b12a8e
RS
1609@end deffn
1610
1611@deffn Command enlarge-window-horizontally columns
1612This function makes the selected window @var{columns} wider.
1613It could be defined as follows:
1614
1615@example
1616@group
1617(defun enlarge-window-horizontally (columns)
1618 (enlarge-window columns t))
1619@end group
1620@end example
1621@end deffn
1622
1623@deffn Command shrink-window size &optional horizontal
1624This function is like @code{enlarge-window} but negates the argument
1625@var{size}, making the selected window smaller by giving lines (or
1626columns) to the other windows. If the window shrinks below
1627@code{window-min-height} or @code{window-min-width}, then it disappears.
1628
1629If @var{size} is negative, the window is enlarged by @minus{}@var{size}
1630lines or columns.
1631@end deffn
1632
1633@deffn Command shrink-window-horizontally columns
1634This function makes the selected window @var{columns} narrower.
1635It could be defined as follows:
1636
1637@example
1638@group
1639(defun shrink-window-horizontally (columns)
1640 (shrink-window columns t))
1641@end group
1642@end example
1643@end deffn
1644
1645@cindex minimum window size
c638661f 1646 The following two variables constrain the window-size-changing
b1b12a8e
RS
1647functions to a minimum height and width.
1648
1649@defopt window-min-height
1650The value of this variable determines how short a window may become
1651before it is automatically deleted. Making a window smaller than
1652@code{window-min-height} automatically deletes it, and no window may be
1653created shorter than this. The absolute minimum height is two (allowing
1654one line for the mode line, and one line for the buffer display).
c638661f 1655Actions that change window sizes reset this variable to two if it is
b1b12a8e
RS
1656less than two. The default value is 4.
1657@end defopt
1658
1659@defopt window-min-width
1660The value of this variable determines how narrow a window may become
1661before it automatically deleted. Making a window smaller than
1662@code{window-min-width} automatically deletes it, and no window may be
1663created narrower than this. The absolute minimum width is one; any
1664value below that is ignored. The default value is 10.
1665@end defopt
1666
1667@node Coordinates and Windows
1668@section Coordinates and Windows
1669
c638661f 1670This section describes how to relate screen coordinates to windows.
b1b12a8e
RS
1671
1672@defun window-at x y &optional frame
1673This function returns the window containing the specified cursor
1674position in the frame @var{frame}. The coordinates @var{x} and @var{y}
1675are measured in characters and count from the top left corner of the
1676frame. If they are out of range, @code{window-at} returns @code{nil}.
1677
1678If you omit @var{frame}, the selected frame is used.
1679@end defun
1680
1681@defun coordinates-in-window-p coordinates window
1682This function checks whether a particular frame position falls within
1683the window @var{window}.
1684
bda144f4 1685@need 3000
b1b12a8e
RS
1686The argument @var{coordinates} is a cons cell of this form:
1687
1688@example
1689(@var{x} . @var{y})
1690@end example
1691
1692@noindent
1693The coordinates @var{x} and @var{y} are measured in characters, and
1694count from the top left corner of the screen or frame.
1695
1696The value of @code{coordinates-in-window-p} is non-@code{nil} if the
1697coordinates are inside @var{window}. The value also indicates what part
1698of the window the position is in, as follows:
1699
1700@table @code
1701@item (@var{relx} . @var{rely})
1702The coordinates are inside @var{window}. The numbers @var{relx} and
1703@var{rely} are the equivalent window-relative coordinates for the
1704specified position, counting from 0 at the top left corner of the
1705window.
1706
1707@item mode-line
1708The coordinates are in the mode line of @var{window}.
1709
1710@item vertical-split
1711The coordinates are in the vertical line between @var{window} and its
3c29caa8 1712neighbor to the right. This value occurs only if the window doesn't
b1b12a8e
RS
1713have a scroll bar; positions in a scroll bar are considered outside the
1714window.
1715
1716@item nil
1717The coordinates are not in any part of @var{window}.
1718@end table
1719
1720The function @code{coordinates-in-window-p} does not require a frame as
1721argument because it always uses the frame that @var{window} is on.
1722@end defun
1723
1724@node Window Configurations
1725@section Window Configurations
1726@cindex window configurations
1727@cindex saving window information
1728
1729 A @dfn{window configuration} records the entire layout of a
1730frame---all windows, their sizes, which buffers they contain, what part
1731of each buffer is displayed, and the values of point and the mark. You
1732can bring back an entire previous layout by restoring a window
1733configuration previously saved.
1734
1735 If you want to record all frames instead of just one, use a frame
1736configuration instead of a window configuration. @xref{Frame
1737Configurations}.
1738
1739@defun current-window-configuration
1740This function returns a new object representing Emacs's current window
1741configuration, namely the number of windows, their sizes and current
1742buffers, which window is the selected window, and for each window the
1743displayed buffer, the display-start position, and the positions of point
1744and the mark. An exception is made for point in the current buffer,
1745whose value is not saved.
1746@end defun
1747
1748@defun set-window-configuration configuration
1749This function restores the configuration of Emacs's windows and
1750buffers to the state specified by @var{configuration}. The argument
1751@var{configuration} must be a value that was previously returned by
1752@code{current-window-configuration}.
1753
bfe721d1
KH
1754This function always counts as a window size change and triggers
1755execution of the @code{window-size-change-functions}. (It doesn't know
1756how to tell whether the new configuration actually differs from the old
1757one.)
1758
b1b12a8e
RS
1759Here is a way of using this function to get the same effect
1760as @code{save-window-excursion}:
1761
1762@example
1763@group
1764(let ((config (current-window-configuration)))
1765 (unwind-protect
1766 (progn (split-window-vertically nil)
1767 @dots{})
1768 (set-window-configuration config)))
1769@end group
1770@end example
1771@end defun
1772
1773@defspec save-window-excursion forms@dots{}
1774This special form records the window configuration, executes @var{forms}
1775in sequence, then restores the earlier window configuration. The window
1776configuration includes the value of point and the portion of the buffer
c638661f 1777that is visible. It also includes the choice of selected window.
b1b12a8e
RS
1778However, it does not include the value of point in the current buffer;
1779use @code{save-excursion} if you wish to preserve that.
1780
bfe721d1
KH
1781Don't use this construct when @code{save-selected-window} is all you need.
1782
1783Exit from @code{save-window-excursion} always triggers execution of the
1784@code{window-size-change-functions}. (It doesn't know how to tell
1785whether the restored configuration actually differs from the one in
1786effect at the end of the @var{forms}.)
1787
b1b12a8e
RS
1788The return value is the value of the final form in @var{forms}.
1789For example:
1790
1791@example
1792@group
1793(split-window)
1794 @result{} #<window 25 on control.texi>
1795@end group
1796@group
1797(setq w (selected-window))
1798 @result{} #<window 19 on control.texi>
1799@end group
1800@group
1801(save-window-excursion
1802 (delete-other-windows w)
1803 (switch-to-buffer "foo")
1804 'do-something)
1805 @result{} do-something
1806 ;; @r{The screen is now split again.}
1807@end group
1808@end example
1809@end defspec
1810
1811@defun window-configuration-p object
1812This function returns @code{t} if @var{object} is a window configuration.
1813@end defun
1814
1815 Primitives to look inside of window configurations would make sense,
1816but none are implemented. It is not clear they are useful enough to be
1817worth implementing.