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