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