]> code.delx.au - gnu-emacs/blob - doc/lispref/windows.texi
Merge branch 'master' of git.sv.gnu.org:/srv/git/emacs
[gnu-emacs] / doc / lispref / windows.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2015 Free Software
4 @c Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @node Windows
7 @chapter Windows
8
9 This chapter describes the functions and variables related to Emacs
10 windows. @xref{Frames}, for how windows are assigned an area of screen
11 available for Emacs to use. @xref{Display}, for information on how text
12 is displayed in windows.
13
14 @menu
15 * Basic Windows:: Basic information on using windows.
16 * Windows and Frames:: Relating windows to the frame they appear on.
17 * Window Sizes:: Accessing a window's size.
18 * Resizing Windows:: Changing the sizes of windows.
19 * Preserving Window Sizes:: Preserving the size of windows.
20 * Splitting Windows:: Creating a new window.
21 * Deleting Windows:: Removing a window from its frame.
22 * Recombining Windows:: Preserving the frame layout when splitting and
23 deleting windows.
24 * Selecting Windows:: The selected window is the one that you edit in.
25 * Cyclic Window Ordering:: Moving around the existing windows.
26 * Buffers and Windows:: Each window displays the contents of a buffer.
27 * Switching Buffers:: Higher-level functions for switching to a buffer.
28 * Choosing Window:: How to choose a window for displaying a buffer.
29 * Display Action Functions:: Subroutines for @code{display-buffer}.
30 * Choosing Window Options:: Extra options affecting how buffers are displayed.
31 * Window History:: Each window remembers the buffers displayed in it.
32 * Dedicated Windows:: How to avoid displaying another buffer in
33 a specific window.
34 * Quitting Windows:: How to restore the state prior to displaying a
35 buffer.
36 * Window Point:: Each window has its own location of point.
37 * Window Start and End:: Buffer positions indicating which text is
38 on-screen in a window.
39 * Textual Scrolling:: Moving text up and down through the window.
40 * Vertical Scrolling:: Moving the contents up and down on the window.
41 * Horizontal Scrolling:: Moving the contents sideways on the window.
42 * Coordinates and Windows:: Converting coordinates to windows.
43 * Window Configurations:: Saving and restoring the state of the screen.
44 * Window Parameters:: Associating additional information with windows.
45 * Window Hooks:: Hooks for scrolling, window size changes,
46 redisplay going past a certain point,
47 or window configuration changes.
48 @end menu
49
50
51 @node Basic Windows
52 @section Basic Concepts of Emacs Windows
53 @cindex window
54
55 A @dfn{window} is an area of the screen that is used to display a buffer
56 (@pxref{Buffers}). In Emacs Lisp, windows are represented by a special
57 Lisp object type.
58
59 @cindex multiple windows
60 Windows are grouped into frames (@pxref{Frames}). Each frame
61 contains at least one window; the user can subdivide it into multiple,
62 non-overlapping windows to view several buffers at once. Lisp
63 programs can use multiple windows for a variety of purposes. In
64 Rmail, for example, you can view a summary of message titles in one
65 window, and the contents of the selected message in another window.
66
67 @cindex terminal screen
68 @cindex screen of terminal
69 Emacs uses the word ``window'' with a different meaning than in
70 graphical desktop environments and window systems, such as the X
71 Window System. When Emacs is run on X, each of its graphical X
72 windows is an Emacs frame (containing one or more Emacs windows).
73 When Emacs is run on a text terminal, the frame fills the entire
74 terminal screen.
75
76 @cindex tiled windows
77 Unlike X windows, Emacs windows are @dfn{tiled}; they never overlap
78 within the area of the frame. When a window is created, resized, or
79 deleted, the change in window space is taken from or given to the
80 adjacent windows, so that the total area of the frame is unchanged.
81
82 @defun windowp object
83 This function returns @code{t} if @var{object} is a window (whether or
84 not it displays a buffer). Otherwise, it returns @code{nil}.
85 @end defun
86
87 @cindex live windows
88 A @dfn{live window} is one that is actually displaying a buffer in a
89 frame.
90
91 @defun window-live-p object
92 This function returns @code{t} if @var{object} is a live window and
93 @code{nil} otherwise. A live window is one that displays a buffer.
94 @end defun
95
96 @cindex internal windows
97 The windows in each frame are organized into a @dfn{window tree}.
98 @xref{Windows and Frames}. The leaf nodes of each window tree are live
99 windows---the ones actually displaying buffers. The internal nodes of
100 the window tree are @dfn{internal windows}, which are not live.
101
102 @cindex valid windows
103 A @dfn{valid window} is one that is either live or internal. A valid
104 window can be @dfn{deleted}, i.e., removed from its frame
105 (@pxref{Deleting Windows}); then it is no longer valid, but the Lisp
106 object representing it might be still referenced from other Lisp
107 objects. A deleted window may be made valid again by restoring a saved
108 window configuration (@pxref{Window Configurations}).
109
110 You can distinguish valid windows from deleted windows with
111 @code{window-valid-p}.
112
113 @defun window-valid-p object
114 This function returns @code{t} if @var{object} is a live window, or an
115 internal window in a window tree. Otherwise, it returns @code{nil},
116 including for the case where @var{object} is a deleted window.
117 @end defun
118
119 @cindex selected window
120 @cindex window selected within a frame
121 In each frame, at any time, exactly one Emacs window is designated
122 as @dfn{selected within the frame}. For the selected frame, that
123 window is called the @dfn{selected window}---the one in which most
124 editing takes place, and in which the cursor for selected windows
125 appears (@pxref{Cursor Parameters}). The selected window's buffer is
126 usually also the current buffer, except when @code{set-buffer} has
127 been used (@pxref{Current Buffer}). As for non-selected frames, the
128 window selected within the frame becomes the selected window if the
129 frame is ever selected. @xref{Selecting Windows}.
130
131 @defun selected-window
132 This function returns the selected window (which is always a live
133 window).
134 @end defun
135
136 @node Windows and Frames
137 @section Windows and Frames
138
139 Each window belongs to exactly one frame (@pxref{Frames}).
140
141 @defun window-frame &optional window
142 This function returns the frame that the window @var{window} belongs
143 to. If @var{window} is @code{nil}, it defaults to the selected
144 window.
145 @end defun
146
147 @defun window-list &optional frame minibuffer window
148 This function returns a list of live windows belonging to the frame
149 @var{frame}. If @var{frame} is omitted or @code{nil}, it defaults to
150 the selected frame.
151
152 The optional argument @var{minibuffer} specifies whether to include
153 the minibuffer window in the returned list. If @var{minibuffer} is
154 @code{t}, the minibuffer window is included. If @var{minibuffer} is
155 @code{nil} or omitted, the minibuffer window is included only if it is
156 active. If @var{minibuffer} is neither @code{nil} nor @code{t}, the
157 minibuffer window is never included.
158
159 The optional argument @var{window}, if non-@code{nil}, should be a live
160 window on the specified frame; then @var{window} will be the first
161 element in the returned list. If @var{window} is omitted or @code{nil},
162 the window selected within the frame is the first element.
163 @end defun
164
165 @cindex window tree
166 @cindex root window
167 Windows in the same frame are organized into a @dfn{window tree},
168 whose leaf nodes are the live windows. The internal nodes of a window
169 tree are not live; they exist for the purpose of organizing the
170 relationships between live windows. The root node of a window tree is
171 called the @dfn{root window}. It can be either a live window (if the
172 frame has just one window), or an internal window.
173
174 A minibuffer window (@pxref{Minibuffer Windows}) is not part of its
175 frame's window tree unless the frame is a minibuffer-only frame.
176 Nonetheless, most of the functions in this section accept the
177 minibuffer window as an argument. Also, the function
178 @code{window-tree} described at the end of this section lists the
179 minibuffer window alongside the actual window tree.
180
181 @defun frame-root-window &optional frame-or-window
182 This function returns the root window for @var{frame-or-window}. The
183 argument @var{frame-or-window} should be either a window or a frame;
184 if omitted or @code{nil}, it defaults to the selected frame. If
185 @var{frame-or-window} is a window, the return value is the root window
186 of that window's frame.
187 @end defun
188
189 @cindex parent window
190 @cindex child window
191 @cindex sibling window
192 When a window is split, there are two live windows where previously
193 there was one. One of these is represented by the same Lisp window
194 object as the original window, and the other is represented by a
195 newly-created Lisp window object. Both of these live windows become
196 leaf nodes of the window tree, as @dfn{child windows} of a single
197 internal window. If necessary, Emacs automatically creates this
198 internal window, which is also called the @dfn{parent window}, and
199 assigns it to the appropriate position in the window tree. A set of
200 windows that share the same parent are called @dfn{siblings}.
201
202 @cindex parent window
203 @defun window-parent &optional window
204 This function returns the parent window of @var{window}. If
205 @var{window} is omitted or @code{nil}, it defaults to the selected
206 window. The return value is @code{nil} if @var{window} has no parent
207 (i.e., it is a minibuffer window or the root window of its frame).
208 @end defun
209
210 Each internal window always has at least two child windows. If this
211 number falls to one as a result of window deletion, Emacs
212 automatically deletes the internal window, and its sole remaining
213 child window takes its place in the window tree.
214
215 Each child window can be either a live window, or an internal window
216 (which in turn would have its own child windows). Therefore, each
217 internal window can be thought of as occupying a certain rectangular
218 @dfn{screen area}---the union of the areas occupied by the live
219 windows that are ultimately descended from it.
220
221 @cindex window combination
222 @cindex vertical combination
223 @cindex horizontal combination
224 For each internal window, the screen areas of the immediate children
225 are arranged either vertically or horizontally (never both). If the
226 child windows are arranged one above the other, they are said to form
227 a @dfn{vertical combination}; if they are arranged side by side, they
228 are said to form a @dfn{horizontal combination}. Consider the
229 following example:
230
231 @smallexample
232 @group
233 ______________________________________
234 | ______ ____________________________ |
235 || || __________________________ ||
236 || ||| |||
237 || ||| |||
238 || ||| |||
239 || |||____________W4____________|||
240 || || __________________________ ||
241 || ||| |||
242 || ||| |||
243 || |||____________W5____________|||
244 ||__W2__||_____________W3_____________ |
245 |__________________W1__________________|
246
247 @end group
248 @end smallexample
249
250 @noindent
251 The root window of this frame is an internal window, @var{W1}. Its
252 child windows form a horizontal combination, consisting of the live
253 window @var{W2} and the internal window @var{W3}. The child windows
254 of @var{W3} form a vertical combination, consisting of the live
255 windows @var{W4} and @var{W5}. Hence, the live windows in this
256 window tree are @var{W2}, @var{W4}, and @var{W5}.
257
258 The following functions can be used to retrieve a child window of an
259 internal window, and the siblings of a child window.
260
261 @defun window-top-child &optional window
262 This function returns the topmost child window of @var{window}, if
263 @var{window} is an internal window whose children form a vertical
264 combination. For any other type of window, the return value is
265 @code{nil}.
266 @end defun
267
268 @defun window-left-child &optional window
269 This function returns the leftmost child window of @var{window}, if
270 @var{window} is an internal window whose children form a horizontal
271 combination. For any other type of window, the return value is
272 @code{nil}.
273 @end defun
274
275 @defun window-child window
276 This function returns the first child window of the internal window
277 @var{window}---the topmost child window for a vertical combination, or
278 the leftmost child window for a horizontal combination. If
279 @var{window} is a live window, the return value is @code{nil}.
280 @end defun
281
282 @defun window-combined-p &optional window horizontal
283 This function returns a non-@code{nil} value if and only if
284 @var{window} is part of a vertical combination. If @var{window} is
285 omitted or @code{nil}, it defaults to the selected one.
286
287 If the optional argument @var{horizontal} is non-@code{nil}, this
288 means to return non-@code{nil} if and only if @var{window} is part of
289 a horizontal combination.
290 @end defun
291
292 @defun window-next-sibling &optional window
293 This function returns the next sibling of the window @var{window}. If
294 omitted or @code{nil}, @var{window} defaults to the selected window.
295 The return value is @code{nil} if @var{window} is the last child of
296 its parent.
297 @end defun
298
299 @defun window-prev-sibling &optional window
300 This function returns the previous sibling of the window @var{window}.
301 If omitted or @code{nil}, @var{window} defaults to the selected
302 window. The return value is @code{nil} if @var{window} is the first
303 child of its parent.
304 @end defun
305
306 The functions @code{window-next-sibling} and
307 @code{window-prev-sibling} should not be confused with the functions
308 @code{next-window} and @code{previous-window}, which return the next
309 and previous window, respectively, in the cyclic ordering of windows
310 (@pxref{Cyclic Window Ordering}).
311
312 You can use the following functions to find the first live window on a
313 frame and the window nearest to a given window.
314
315 @defun frame-first-window &optional frame-or-window
316 This function returns the live window at the upper left corner of the
317 frame specified by @var{frame-or-window}. The argument
318 @var{frame-or-window} must denote a window or a live frame and defaults
319 to the selected frame. If @var{frame-or-window} specifies a window,
320 this function returns the first window on that window's frame. Under
321 the assumption that the frame from our canonical example is selected
322 @code{(frame-first-window)} returns @var{W2}.
323 @end defun
324
325 @cindex window in direction
326 @defun window-in-direction direction &optional window ignore sign wrap mini
327 This function returns the nearest live window in direction
328 @var{direction} as seen from the position of @code{window-point} in
329 window @var{window}. The argument @var{direction} must be one of
330 @code{above}, @code{below}, @code{left} or @code{right}. The optional
331 argument @var{window} must denote a live window and defaults to the
332 selected one.
333
334 This function does not return a window whose @code{no-other-window}
335 parameter is non-@code{nil} (@pxref{Window Parameters}). If the nearest
336 window's @code{no-other-window} parameter is non-@code{nil}, this
337 function tries to find another window in the indicated direction whose
338 @code{no-other-window} parameter is @code{nil}. If the optional
339 argument @var{ignore} is non-@code{nil}, a window may be returned even
340 if its @code{no-other-window} parameter is non-@code{nil}.
341
342 If the optional argument @var{sign} is a negative number, it means to
343 use the right or bottom edge of @var{window} as reference position
344 instead of @code{window-point}. If @var{sign} is a positive number, it
345 means to use the left or top edge of @var{window} as reference position.
346
347 If the optional argument @var{wrap} is non-@code{nil}, this means to
348 wrap @var{direction} around frame borders. For example, if @var{window}
349 is at the top of the frame and @var{direction} is @code{above}, then
350 return the minibuffer window provided the frame has one, and a window at
351 the bottom of the frame otherwise.
352
353 If the optional argument @var{mini} is @code{nil}, this means to return
354 the minibuffer window if and only if it is currently active. If
355 @var{mini} is non-@code{nil}, it returns the minibuffer window even when
356 it's not active. However, if @var{wrap} non-@code{nil}, it always acts
357 as if @var{mini} were @code{nil}.
358
359 If it doesn't find a suitable window, this function returns @code{nil}.
360 @end defun
361
362 The following function allows to retrieve the entire window tree of a
363 frame:
364
365 @defun window-tree &optional frame
366 This function returns a list representing the window tree for frame
367 @var{frame}. If @var{frame} is omitted or @code{nil}, it defaults to
368 the selected frame.
369
370 The return value is a list of the form @code{(@var{root} @var{mini})},
371 where @var{root} represents the window tree of the frame's root
372 window, and @var{mini} is the frame's minibuffer window.
373
374 If the root window is live, @var{root} is that window itself.
375 Otherwise, @var{root} is a list @code{(@var{dir} @var{edges} @var{w1}
376 @var{w2} ...)} where @var{dir} is @code{nil} for a horizontal
377 combination and @code{t} for a vertical combination, @var{edges} gives
378 the size and position of the combination, and the remaining elements
379 are the child windows. Each child window may again be a window object
380 (for a live window) or a list with the same format as above (for an
381 internal window). The @var{edges} element is a list @code{(@var{left}
382 @var{top} @var{right} @var{bottom})}, similar to the value returned by
383 @code{window-edges} (@pxref{Coordinates and Windows}).
384 @end defun
385
386
387 @node Window Sizes
388 @section Window Sizes
389 @cindex window size
390 @cindex size of window
391
392 The following schematic shows the structure of a live window:
393
394 @smallexample
395 @group
396 ____________________________________________
397 |______________ Header Line ______________|RD| ^
398 ^ |LS|LM|LF| |RF|RM|RS| | |
399 | | | | | | | | | | |
400 Window | | | | Text Area | | | | | Window
401 Body | | | | | (Window Body) | | | | | Total
402 Height | | | | | | | | | Height
403 | | | | |<- Window Body Width ->| | | | | |
404 v |__|__|__|_______________________|__|__|__| | |
405 |_________ Horizontal Scroll Bar _________| | |
406 |_______________ Mode Line _______________|__| |
407 |_____________ Bottom Divider _______________| v
408 <---------- Window Total Width ------------>
409
410 @end group
411 @end smallexample
412
413 @cindex window body
414 @cindex text area of a window
415 @cindex body of a window
416 At the center of the window is the @dfn{text area}, or @dfn{body},
417 where the buffer text is displayed. The text area can be surrounded by
418 a series of optional areas. On the left and right, from innermost to
419 outermost, these are the left and right fringes, denoted by LF and RF
420 (@pxref{Fringes}); the left and right margins, denoted by LM and RM in
421 the schematic (@pxref{Display Margins}); the left or right vertical
422 scroll bar, only one of which is present at any time, denoted by LS and
423 RS (@pxref{Scroll Bars}); and the right divider, denoted by RD
424 (@pxref{Window Dividers}). At the top of the window is the header line
425 (@pxref{Header Lines}). At the bottom of the window are the horizontal
426 scroll bar (@pxref{Scroll Bars}); the mode line (@pxref{Mode Line
427 Format}); and the bottom divider (@pxref{Window Dividers}).
428
429 Emacs provides miscellaneous functions for finding the height and
430 width of a window. The return value of many of these functions can be
431 specified either in units of pixels or in units of lines and columns.
432 On a graphical display, the latter actually correspond to the height and
433 width of a ``default'' character specified by the frame's default font
434 as returned by @code{frame-char-height} and @code{frame-char-width}
435 (@pxref{Size and Position}). Thus, if a window is displaying text with
436 a different font or size, the reported line height and column width for
437 that window may differ from the actual number of text lines or columns
438 displayed within it.
439
440 @cindex window height
441 @cindex height of a window
442 @cindex total height of a window
443 The @dfn{total height} of a window is the number of lines comprising
444 the window's body, the header line, the horizontal scroll bar, the mode
445 line and the bottom divider (if any).
446
447 @defun window-total-height &optional window round
448 This function returns the total height, in lines, of the window
449 @var{window}. If @var{window} is omitted or @code{nil}, it defaults to
450 the selected window. If @var{window} is an internal window, the return
451 value is the total height occupied by its descendant windows.
452
453 If a window's pixel height is not an integral multiple of its frame's
454 default character height, the number of lines occupied by the window is
455 rounded internally. This is done in a way such that, if the window is a
456 parent window, the sum of the total heights of all its child windows
457 internally equals the total height of their parent. This means that
458 although two windows have the same pixel height, their internal total
459 heights may differ by one line. This means also, that if window is
460 vertically combined and has a next sibling, the topmost row of that
461 sibling can be calculated as the sum of this window's topmost row and
462 total height (@pxref{Coordinates and Windows})
463
464 If the optional argument @var{round} is @code{ceiling}, this
465 function returns the smallest integer larger than @var{window}'s pixel
466 height divided by the character height of its frame; if it is
467 @code{floor}, it returns the largest integer smaller than said value;
468 with any other @var{round} it returns the internal value of
469 @var{windows}'s total height.
470 @end defun
471
472 @cindex window width
473 @cindex width of a window
474 @cindex total width of a window
475 The @dfn{total width} of a window is the number of lines comprising the
476 window's body, its margins, fringes, scroll bars and a right divider (if
477 any).
478
479 @defun window-total-width &optional window round
480 This function returns the total width, in columns, of the window
481 @var{window}. If @var{window} is omitted or @code{nil}, it defaults to
482 the selected window. If @var{window} is internal, the return value is
483 the total width occupied by its descendant windows.
484
485 If a window's pixel width is not an integral multiple of its frame's
486 character width, the number of lines occupied by the window is rounded
487 internally. This is done in a way such that, if the window is a parent
488 window, the sum of the total widths of all its children internally
489 equals the total width of their parent. This means that although two
490 windows have the same pixel width, their internal total widths may
491 differ by one column. This means also, that if this window is
492 horizontally combined and has a next sibling, the leftmost column of
493 that sibling can be calculated as the sum of this window's leftmost
494 column and total width (@pxref{Coordinates and Windows}). The optional
495 argument @var{round} behaves as it does for @code{window-total-height}.
496 @end defun
497
498 @defun window-total-size &optional window horizontal round
499 This function returns either the total height in lines or the total
500 width in columns of the window @var{window}. If @var{horizontal} is
501 omitted or @code{nil}, this is equivalent to calling
502 @code{window-total-height} for @var{window}; otherwise it is equivalent
503 to calling @code{window-total-width} for @var{window}. The optional
504 argument @var{round} behaves as it does for @code{window-total-height}.
505 @end defun
506
507 The following two functions can be used to return the total size of a
508 window in units of pixels.
509
510 @cindex window pixel height
511 @cindex pixel height of a window
512 @cindex total pixel height of a window
513
514 @defun window-pixel-height &optional window
515 This function returns the total height of window @var{window} in pixels.
516 @var{window} must be a valid window and defaults to the selected one.
517
518 The return value includes mode and header line, a horizontal scroll bar
519 and a bottom divider, if any. If @var{window} is an internal window,
520 its pixel height is the pixel height of the screen areas spanned by its
521 children.
522 @end defun
523
524 @cindex window pixel height
525 @cindex pixel height of a window
526 @cindex total pixel height of a window
527
528 @defun window-pixel-width &optional Lisp_Object &optional window
529 This function returns the width of window @var{window} in pixels.
530 @var{window} must be a valid window and defaults to the selected one.
531
532 The return value includes the fringes and margins of @var{window} as
533 well as any vertical dividers or scroll bars belonging to @var{window}.
534 If @var{window} is an internal window, its pixel width is the width of
535 the screen areas spanned by its children.
536 @end defun
537
538 @cindex full-width window
539 @cindex full-height window
540 The following functions can be used to determine whether a given
541 window has any adjacent windows.
542
543 @defun window-full-height-p &optional window
544 This function returns non-@code{nil} if @var{window} has no other window
545 above or below it in its frame. More precisely, this means that the
546 total height of @var{window} equals the total height of the root window
547 on that frame. The minibuffer window does not count in this regard. If
548 @var{window} is omitted or @code{nil}, it defaults to the selected
549 window.
550 @end defun
551
552 @defun window-full-width-p &optional window
553 This function returns non-@code{nil} if @var{window} has no other
554 window to the left or right in its frame, i.e., its total width equals
555 that of the root window on that frame. If @var{window} is omitted or
556 @code{nil}, it defaults to the selected window.
557 @end defun
558
559 @cindex window body height
560 @cindex body height of a window
561 @cindex window body width
562 The @dfn{body height} of a window is the height of its text area, which
563 does not include a mode or header line, a horizontal scroll bar, or a
564 bottom divider.
565
566 @defun window-body-height &optional window pixelwise
567 This function returns the height, in lines, of the body of window
568 @var{window}. If @var{window} is omitted or @code{nil}, it defaults to
569 the selected window; otherwise it must be a live window.
570
571 If the optional argument @var{pixelwise} is non-@code{nil}, this
572 function returns the body height of @var{window} counted in pixels.
573
574 If @var{pixelwise} is @code{nil}, the return value is rounded down to
575 the nearest integer, if necessary. This means that if a line at the
576 bottom of the text area is only partially visible, that line is not
577 counted. It also means that the height of a window's body can never
578 exceed its total height as returned by @code{window-total-height}.
579 @end defun
580
581 @cindex body width of a window
582 @cindex body size of a window
583 @cindex window body size
584 The @dfn{body width} of a window is the width of its text area, which
585 does not include the scroll bar, fringes, margins or a right divider.
586
587 @defun window-body-width &optional window pixelwise
588 This function returns the width, in columns, of the body of window
589 @var{window}. If @var{window} is omitted or @code{nil}, it defaults to
590 the selected window; otherwise it must be a live window.
591
592 If the optional argument @var{pixelwise} is non-@code{nil}, this
593 function returns the body width of @var{window} in units of pixels.
594
595 If @var{pixelwise} is @code{nil}, the return value is rounded down to
596 the nearest integer, if necessary. This means that if a column on the
597 right of the text area is only partially visible, that column is not
598 counted. It also means that the width of a window's body can never
599 exceed its total width as returned by @code{window-total-width}.
600 @end defun
601
602 @defun window-body-size &optional window horizontal pixelwise
603 This function returns the body height or body width of @var{window}. If
604 @var{horizontal} is omitted or @code{nil}, it is equivalent to calling
605 @code{window-body-height} for @var{window}; otherwise it is equivalent
606 to calling @code{window-body-width}. In either case, the optional
607 argument @var{pixelwise} is passed to the function called.
608 @end defun
609
610 For compatibility with previous versions of Emacs,
611 @code{window-height} is an alias for @code{window-total-height}, and
612 @code{window-width} is an alias for @code{window-body-width}. These
613 aliases are considered obsolete and will be removed in the future.
614
615 The pixel heights of a window's mode and header line can be retrieved
616 with the functions given below. Their return value is usually accurate
617 unless the window has not been displayed before: In that case, the
618 return value is based on an estimate of the font used for the window's
619 frame.
620
621 @defun window-mode-line-height &optional window
622 This function returns the height in pixels of @var{window}'s mode line.
623 @var{window} must be a live window and defaults to the selected one. If
624 @var{window} has no mode line, the return value is zero.
625 @end defun
626
627 @defun window-header-line-height &optional window
628 This function returns the height in pixels of @var{window}'s header
629 line. @var{window} must be a live window and defaults to the selected
630 one. If @var{window} has no header line, the return value is zero.
631 @end defun
632
633 Functions for retrieving the height and/or width of window dividers
634 (@pxref{Window Dividers}), fringes (@pxref{Fringes}), scroll bars
635 (@pxref{Scroll Bars}), and display margins (@pxref{Display Margins}) are
636 described in the corresponding sections.
637
638 @cindex fixed-size window
639 @vindex window-min-height
640 @vindex window-min-width
641 Commands that change the size of windows (@pxref{Resizing Windows}),
642 or split them (@pxref{Splitting Windows}), obey the variables
643 @code{window-min-height} and @code{window-min-width}, which specify the
644 smallest allowable window height and width. They also obey the variable
645 @code{window-size-fixed}, with which a window can be @dfn{fixed} in
646 size:
647
648 @defopt window-min-height
649 This option specifies the minimum total height, in lines, of any window.
650 Its value has to accommodate at least one text line as well as a mode
651 and header line, a horizontal scroll bar and a bottom divider, if
652 present.
653 @end defopt
654
655 @defopt window-min-width
656 This option specifies the minimum total width, in columns, of any
657 window. Its value has to accommodate two text columns as well as
658 margins, fringes, a scroll bar and a right divider, if present.
659 @end defopt
660
661 The following function tells how small a specific window can get taking
662 into account the sizes of its areas and the values of
663 @code{window-min-height}, @code{window-min-width} and
664 @code{window-size-fixed}.
665
666 @defun window-min-size &optional window horizontal ignore pixelwise
667 This function returns the minimum size of @var{window}. @var{window}
668 must be a valid window and defaults to the selected one. The optional
669 argument @var{horizontal} non-@code{nil} means to return the minimum
670 number of columns of @var{window}; otherwise return the minimum number
671 of @var{window}'s lines.
672
673 The return value makes sure that all components of @var{window} remain
674 fully visible if @var{window}'s size were actually set to it. With
675 @var{horizontal} @code{nil} it includes the mode and header line, the
676 horizontal scroll bar and the bottom divider. With @var{horizontal}
677 non-@code{nil} it includes the fringes, a scroll bar, and a right
678 divider, if present. It does not, however, include the space reserved
679 for the margins.
680
681 The optional argument @var{ignore}, if non-@code{nil}, means ignore
682 restrictions imposed by fixed size windows, @code{window-min-height} or
683 @code{window-min-width} settings. If @var{ignore} equals @code{safe},
684 live windows may get as small as @code{window-safe-min-height} lines and
685 @code{window-safe-min-width} columns. If @var{ignore} is a window,
686 ignore restrictions for that window only. Any other non-@code{nil}
687 value means ignore all of the above restrictions for all windows.
688
689 The optional argument @var{pixelwise} non-@code{nil} means to return the
690 minimum size of @var{window} counted in pixels.
691 @end defun
692
693 @node Resizing Windows
694 @section Resizing Windows
695 @cindex window resizing
696 @cindex resize window
697 @cindex changing window size
698 @cindex window size, changing
699
700 This section describes functions for resizing a window without
701 changing the size of its frame. Because live windows do not overlap,
702 these functions are meaningful only on frames that contain two or more
703 windows: resizing a window also changes the size of a neighboring
704 window. If there is just one window on a frame, its size cannot be
705 changed except by resizing the frame (@pxref{Size and Position}).
706
707 Except where noted, these functions also accept internal windows as
708 arguments. Resizing an internal window causes its child windows to be
709 resized to fit the same space.
710
711 @defun window-resizable window delta &optional horizontal ignore pixelwise
712 This function returns @var{delta} if the size of @var{window} can be
713 changed vertically by @var{delta} lines. If the optional argument
714 @var{horizontal} is non-@code{nil}, it instead returns @var{delta} if
715 @var{window} can be resized horizontally by @var{delta} columns. It
716 does not actually change the window size.
717
718 If @var{window} is @code{nil}, it defaults to the selected window.
719
720 A positive value of @var{delta} means to check whether the window can be
721 enlarged by that number of lines or columns; a negative value of
722 @var{delta} means to check whether the window can be shrunk by that many
723 lines or columns. If @var{delta} is non-zero, a return value of 0 means
724 that the window cannot be resized.
725
726 Normally, the variables @code{window-min-height} and
727 @code{window-min-width} specify the smallest allowable window size
728 (@pxref{Window Sizes}). However, if the optional argument @var{ignore}
729 is non-@code{nil}, this function ignores @code{window-min-height} and
730 @code{window-min-width}, as well as @code{window-size-fixed}. Instead,
731 it considers the minimum-height window to be one consisting of a header
732 and a mode line, a horizontal scrollbar and a bottom divider (if any),
733 plus a text area one line tall; and a minimum-width window as one
734 consisting of fringes, margins, a scroll bar and a right divider (if
735 any), plus a text area two columns wide.
736
737 If the optional argument @var{pixelwise} is non-@code{nil},
738 @var{delta} is interpreted as pixels.
739 @end defun
740
741 @defun window-resize window delta &optional horizontal ignore pixelwise
742 This function resizes @var{window} by @var{delta} increments. If
743 @var{horizontal} is @code{nil}, it changes the height by @var{delta}
744 lines; otherwise, it changes the width by @var{delta} columns. A
745 positive @var{delta} means to enlarge the window, and a negative
746 @var{delta} means to shrink it.
747
748 If @var{window} is @code{nil}, it defaults to the selected window. If
749 the window cannot be resized as demanded, an error is signaled.
750
751 The optional argument @var{ignore} has the same meaning as for the
752 function @code{window-resizable} above.
753
754 If the optional argument @var{pixelwise} is non-@code{nil},
755 @var{delta} will be interpreted as pixels.
756
757 The choice of which window edges this function alters depends on the
758 values of the option @code{window-combination-resize} and the
759 combination limits of the involved windows; in some cases, it may alter
760 both edges. @xref{Recombining Windows}. To resize by moving only the
761 bottom or right edge of a window, use the function
762 @code{adjust-window-trailing-edge}.
763 @end defun
764
765 @c The commands enlarge-window, enlarge-window-horizontally,
766 @c shrink-window, and shrink-window-horizontally are documented in the
767 @c Emacs manual. They are not preferred for calling from Lisp.
768
769 @defun adjust-window-trailing-edge window delta &optional horizontal pixelwise
770 This function moves @var{window}'s bottom edge by @var{delta} lines.
771 If optional argument @var{horizontal} is non-@code{nil}, it instead
772 moves the right edge by @var{delta} columns. If @var{window} is
773 @code{nil}, it defaults to the selected window.
774
775 If the optional argument @var{pixelwise} is non-@code{nil},
776 @var{delta} is interpreted as pixels.
777
778 A positive @var{delta} moves the edge downwards or to the right; a
779 negative @var{delta} moves it upwards or to the left. If the edge
780 cannot be moved as far as specified by @var{delta}, this function
781 moves it as far as possible but does not signal a error.
782
783 This function tries to resize windows adjacent to the edge that is
784 moved. If this is not possible for some reason (e.g., if that adjacent
785 window is fixed-size), it may resize other windows.
786 @end defun
787
788 @cindex pixelwise, resizing windows
789 @defopt window-resize-pixelwise
790 If the value of this option is non-@code{nil}, Emacs resizes windows in
791 units of pixels. This currently affects functions like
792 @code{split-window} (@pxref{Splitting Windows}), @code{maximize-window},
793 @code{minimize-window}, @code{fit-window-to-buffer},
794 @code{shrink-window-if-larger-than-buffer} (all listed below) and
795 @code{fit-frame-to-buffer} (@pxref{Size and Position}).
796
797 Note that when a frame's pixel size is not a multiple of its character
798 size, at least one window may get resized pixelwise even if this
799 option is @code{nil}. The default value is @code{nil}.
800 @end defopt
801
802 The following commands resize windows in more specific ways. When
803 called interactively, they act on the selected window.
804
805 @deffn Command fit-window-to-buffer &optional window max-height min-height max-width min-width preserve-size
806 This command adjusts the height or width of @var{window} to fit the text
807 in it. It returns non-@code{nil} if it was able to resize @var{window},
808 and @code{nil} otherwise. If @var{window} is omitted or @code{nil}, it
809 defaults to the selected window. Otherwise, it should be a live window.
810
811 If @var{window} is part of a vertical combination, this function adjusts
812 @var{window}'s height. The new height is calculated from the actual
813 height of the accessible portion of its buffer. The optional argument
814 @var{max-height}, if non-@code{nil}, specifies the maximum total height
815 that this function can give @var{window}. The optional argument
816 @var{min-height}, if non-@code{nil}, specifies the minimum total height
817 that it can give, which overrides the variable @code{window-min-height}.
818 Both @var{max-height} and @var{min-height} are specified in lines and
819 include mode and header line and a bottom divider, if any.
820
821 If @var{window} is part of a horizontal combination and the value of the
822 option @code{fit-window-to-buffer-horizontally} (see below) is
823 non-@code{nil}, this function adjusts @var{window}'s height. The new
824 width of @var{window} is calculated from the maximum length of its
825 buffer's lines that follow the current start position of @var{window}.
826 The optional argument @var{max-width} specifies a maximum width and
827 defaults to the width of @var{window}'s frame. The optional argument
828 @var{min-width} specifies a minimum width and defaults to
829 @code{window-min-width}. Both @var{max-width} and @var{min-width} are
830 specified in columns and include fringes, margins and scrollbars, if
831 any.
832
833 The optional argument @var{preserve-size}, if non-@code{nil}, will
834 install a parameter to preserve the size of @var{window} during future
835 resize operations (@pxref{Preserving Window Sizes}).
836
837 If the option @code{fit-frame-to-buffer} (see below) is non-@code{nil},
838 this function will try to resize the frame of @var{window} to fit its
839 contents by calling @code{fit-frame-to-buffer} (@pxref{Size and
840 Position}).
841 @end deffn
842
843 @defopt fit-window-to-buffer-horizontally
844 If this is non-@code{nil}, @code{fit-window-to-buffer} can resize
845 windows horizontally. If this is @code{nil} (the default)
846 @code{fit-window-to-buffer} never resizes windows horizontally. If this
847 is @code{only}, it can resize windows horizontally only. Any other
848 value means @code{fit-window-to-buffer} can resize windows in both
849 dimensions.
850 @end defopt
851
852 @defopt fit-frame-to-buffer
853 If this option is non-@code{nil}, @code{fit-window-to-buffer} can fit a
854 frame to its buffer. A frame is fit if and only if its root window is a
855 live window and this option is non-@code{nil}. If this is
856 @code{horizontally}, frames are fit horizontally only. If this is
857 @code{vertically}, frames are fit vertically only. Any other
858 non-@code{nil} value means frames can be resized in both dimensions.
859 @end defopt
860
861 @deffn Command shrink-window-if-larger-than-buffer &optional window
862 This command attempts to reduce @var{window}'s height as much as
863 possible while still showing its full buffer, but no less than
864 @code{window-min-height} lines. The return value is non-@code{nil} if
865 the window was resized, and @code{nil} otherwise. If @var{window} is
866 omitted or @code{nil}, it defaults to the selected window. Otherwise,
867 it should be a live window.
868
869 This command does nothing if the window is already too short to
870 display all of its buffer, or if any of the buffer is scrolled
871 off-screen, or if the window is the only live window in its frame.
872
873 This command calls @code{fit-window-to-buffer} (see above) to do its
874 work.
875 @end deffn
876
877
878 @cindex balancing window sizes
879 @deffn Command balance-windows &optional window-or-frame
880 This function balances windows in a way that gives more space to
881 full-width and/or full-height windows. If @var{window-or-frame}
882 specifies a frame, it balances all windows on that frame. If
883 @var{window-or-frame} specifies a window, it balances only that window
884 and its siblings (@pxref{Windows and Frames}).
885 @end deffn
886
887 @deffn Command balance-windows-area
888 This function attempts to give all windows on the selected frame
889 approximately the same share of the screen area. Full-width or
890 full-height windows are not given more space than other windows.
891 @end deffn
892
893 @cindex maximizing windows
894 @deffn Command maximize-window &optional window
895 This function attempts to make @var{window} as large as possible, in
896 both dimensions, without resizing its frame or deleting other windows.
897 If @var{window} is omitted or @code{nil}, it defaults to the selected
898 window.
899 @end deffn
900
901 @cindex minimizing windows
902 @deffn Command minimize-window &optional window
903 This function attempts to make @var{window} as small as possible, in
904 both dimensions, without deleting it or resizing its frame. If
905 @var{window} is omitted or @code{nil}, it defaults to the selected
906 window.
907 @end deffn
908
909
910 @node Preserving Window Sizes
911 @section Preserving Window Sizes
912 @cindex preserving window sizes
913
914 A window can get resized explicitly by using one of the functions from
915 the preceding section or implicitly, for example, when resizing an
916 adjacent window, when splitting or deleting a window (@pxref{Splitting
917 Windows}, @pxref{Deleting Windows}) or when resizing the window's frame
918 (@pxref{Size and Position}).
919
920 It is possible to avoid implicit resizing of a specific window when
921 there are one or more other resizable windows on the same frame. For
922 this purpose, Emacs must be advised to @dfn{preserve} the size of that
923 window. There are two basic ways to do that.
924
925 @defvar window-size-fixed
926 If this buffer-local variable is non-@code{nil}, the size of any window
927 displaying the buffer cannot normally be changed. Deleting a window or
928 changing the frame's size may still change the window's size, if there
929 is no choice.
930
931 If the value is @code{height}, then only the window's height is fixed;
932 if the value is @code{width}, then only the window's width is fixed.
933 Any other non-@code{nil} value fixes both the width and the height.
934
935 If this variable is @code{nil}, this does not necessarily mean that any
936 window showing the buffer can be resized in the desired direction. To
937 determine that, use the function @code{window-resizable}.
938 @xref{Resizing Windows}.
939 @end defvar
940
941 Often @code{window-size-fixed} is overly aggressive because it inhibits
942 any attempt to explicitly resize or split an affected window as well.
943 This may even happen after the window has been resized implicitly, for
944 example, when deleting an adjacent window or resizing the window's
945 frame. The following function tries hard to never disallow resizing
946 such a window explicitly:
947
948 @defun window-preserve-size &optional window horizontal preserve
949 This function (un-)marks the height of window @var{window} as preserved
950 for future resize operations. @var{window} must be a live window and
951 defaults to the selected one. If the optional argument @var{horizontal}
952 is non-@code{nil}, it (un-)marks the width of @var{window} as preserved.
953
954 If the optional argument @var{preserve} is @code{t}, this means to
955 preserve the current height/width of @var{window}'s body. The
956 height/width of @var{window} will change only if Emacs has no better
957 choice. Resizing a window whose height/width is preserved by this
958 function never throws an error.
959
960 If @var{preserve} is @code{nil}, this means to stop preserving the
961 height/width of @var{window}, lifting any respective restraint induced
962 by a previous call of this function for @var{window}. Calling
963 @code{enlarge-window}, @code{shrink-window} or
964 @code{fit-window-to-buffer} with @var{window} as argument may also
965 remove the respective restraint.
966 @end defun
967
968 @code{window-preserve-size} is currently invoked by the following
969 functions:
970
971 @table @code
972 @item fit-window-to-buffer
973 If the optional argument @var{preserve-size} of that function
974 (@pxref{Resizing Windows}) is non-@code{nil}, the size established by
975 that function is preserved.
976
977 @item display-buffer
978 If the @var{alist} argument of that function (@pxref{Choosing Window})
979 contains a @code{preserve-size} entry, the size of the window produced
980 by that function is preserved.
981 @end table
982
983 @code{window-preserve-size} installs a window parameter (@pxref{Window
984 Parameters}) called @code{preserved-size} which is consulted by the
985 window resizing functions. This parameter will not prevent resizing the
986 window when the window shows another buffer than the one when
987 @code{window-preserve-size} was invoked or if its size has changed since
988 then.
989
990 The following function can be used to check whether the height of a
991 particular window is preserved:
992
993 @defun window-preserved-size &optional window horizontal
994 This function returns the preserved height of window @var{window} in
995 pixels. @var{window} must be a live window and defaults to the selected
996 one. If the optional argument @var{horizontal} is non-@code{nil}, it
997 returns the preserved width of @var{window}. It returns @code{nil} if
998 the size of @var{window} is not preserved.
999 @end defun
1000
1001
1002 @node Splitting Windows
1003 @section Splitting Windows
1004 @cindex splitting windows
1005 @cindex window splitting
1006
1007 This section describes functions for creating a new window by
1008 @dfn{splitting} an existing one.
1009
1010 @defun split-window &optional window size side pixelwise
1011 This function creates a new live window next to the window
1012 @var{window}. If @var{window} is omitted or @code{nil}, it defaults
1013 to the selected window. That window is ``split'', and reduced in
1014 size. The space is taken up by the new window, which is returned.
1015
1016 The optional second argument @var{size} determines the sizes of
1017 @var{window} and/or the new window. If it is omitted or @code{nil},
1018 both windows are given equal sizes; if there is an odd line, it is
1019 allocated to the new window. If @var{size} is a positive number,
1020 @var{window} is given @var{size} lines (or columns, depending on the
1021 value of @var{side}). If @var{size} is a negative number, the new
1022 window is given @minus{}@var{size} lines (or columns).
1023
1024 If @var{size} is @code{nil}, this function obeys the variables
1025 @code{window-min-height} and @code{window-min-width} (@pxref{Window
1026 Sizes}). Thus, it signals an error if splitting would result in making
1027 a window smaller than those variables specify. However, a
1028 non-@code{nil} value for @var{size} causes those variables to be
1029 ignored; in that case, the smallest allowable window is considered to be
1030 one that has space for a text area one line tall and/or two columns
1031 wide.
1032
1033 Hence, if @var{size} is specified, it's the caller's responsibility to
1034 check whether the emanating windows are large enough to encompass all
1035 areas like a mode line or a scroll bar. The function
1036 @code{window-min-size} (@pxref{Window Sizes}) can be used to determine
1037 the minimum requirements of @var{window} in this regard. Since the new
1038 window usually ``inherits'' areas like the mode line or the scroll bar
1039 from @var{window}, that function is also a good guess for the minimum
1040 size of the new window. The caller should specify a smaller size only
1041 if it correspondingly removes an inherited area before the next
1042 redisplay.
1043
1044 The optional third argument @var{side} determines the position of the
1045 new window relative to @var{window}. If it is @code{nil} or
1046 @code{below}, the new window is placed below @var{window}. If it is
1047 @code{above}, the new window is placed above @var{window}. In both
1048 these cases, @var{size} specifies a total window height, in lines.
1049
1050 If @var{side} is @code{t} or @code{right}, the new window is placed on
1051 the right of @var{window}. If @var{side} is @code{left}, the new
1052 window is placed on the left of @var{window}. In both these cases,
1053 @var{size} specifies a total window width, in columns.
1054
1055 The optional fourth argument @var{pixelwise}, if non-@code{nil}, means
1056 to interpret @var{size} in units of pixels, instead of lines and
1057 columns.
1058
1059 If @var{window} is a live window, the new window inherits various
1060 properties from it, including margins and scroll bars. If
1061 @var{window} is an internal window, the new window inherits the
1062 properties of the window selected within @var{window}'s frame.
1063
1064 The behavior of this function may be altered by the window parameters
1065 of @var{window}, so long as the variable
1066 @code{ignore-window-parameters} is @code{nil}. If the value of
1067 the @code{split-window} window parameter is @code{t}, this function
1068 ignores all other window parameters. Otherwise, if the value of the
1069 @code{split-window} window parameter is a function, that function is
1070 called with the arguments @var{window}, @var{size}, and @var{side}, in
1071 lieu of the usual action of @code{split-window}. Otherwise, this
1072 function obeys the @code{window-atom} or @code{window-side} window
1073 parameter, if any. @xref{Window Parameters}.
1074 @end defun
1075
1076 As an example, here is a sequence of @code{split-window} calls that
1077 yields the window configuration discussed in @ref{Windows and Frames}.
1078 This example demonstrates splitting a live window as well as splitting
1079 an internal window. We begin with a frame containing a single window
1080 (a live root window), which we denote by @var{W4}. Calling
1081 @code{(split-window W4)} yields this window configuration:
1082
1083 @smallexample
1084 @group
1085 ______________________________________
1086 | ____________________________________ |
1087 || ||
1088 || ||
1089 || ||
1090 ||_________________W4_________________||
1091 | ____________________________________ |
1092 || ||
1093 || ||
1094 || ||
1095 ||_________________W5_________________||
1096 |__________________W3__________________|
1097
1098 @end group
1099 @end smallexample
1100
1101 @noindent
1102 The @code{split-window} call has created a new live window, denoted by
1103 @var{W5}. It has also created a new internal window, denoted by
1104 @var{W3}, which becomes the root window and the parent of both
1105 @var{W4} and @var{W5}.
1106
1107 Next, we call @code{(split-window W3 nil 'left)}, passing the
1108 internal window @var{W3} as the argument. The result:
1109
1110 @smallexample
1111 @group
1112 ______________________________________
1113 | ______ ____________________________ |
1114 || || __________________________ ||
1115 || ||| |||
1116 || ||| |||
1117 || ||| |||
1118 || |||____________W4____________|||
1119 || || __________________________ ||
1120 || ||| |||
1121 || ||| |||
1122 || |||____________W5____________|||
1123 ||__W2__||_____________W3_____________ |
1124 |__________________W1__________________|
1125 @end group
1126 @end smallexample
1127
1128 @noindent
1129 A new live window @var{W2} is created, to the left of the internal
1130 window @var{W3}. A new internal window @var{W1} is created, becoming
1131 the new root window.
1132
1133 For interactive use, Emacs provides two commands which always split
1134 the selected window. These call @code{split-window} internally.
1135
1136 @deffn Command split-window-right &optional size
1137 This function splits the selected window into two side-by-side
1138 windows, putting the selected window on the left. If @var{size} is
1139 positive, the left window gets @var{size} columns; if @var{size} is
1140 negative, the right window gets @minus{}@var{size} columns.
1141 @end deffn
1142
1143 @deffn Command split-window-below &optional size
1144 This function splits the selected window into two windows, one above
1145 the other, leaving the upper window selected. If @var{size} is
1146 positive, the upper window gets @var{size} lines; if @var{size} is
1147 negative, the lower window gets @minus{}@var{size} lines.
1148 @end deffn
1149
1150 @defopt split-window-keep-point
1151 If the value of this variable is non-@code{nil} (the default),
1152 @code{split-window-below} behaves as described above.
1153
1154 If it is @code{nil}, @code{split-window-below} adjusts point in each
1155 of the two windows to minimize redisplay. (This is useful on slow
1156 terminals.) It selects whichever window contains the screen line that
1157 point was previously on. Note that this only affects
1158 @code{split-window-below}, not the lower-level @code{split-window}
1159 function.
1160 @end defopt
1161
1162
1163 @node Deleting Windows
1164 @section Deleting Windows
1165 @cindex deleting windows
1166
1167 @dfn{Deleting} a window removes it from the frame's window tree. If
1168 the window is a live window, it disappears from the screen. If the
1169 window is an internal window, its child windows are deleted too.
1170
1171 Even after a window is deleted, it continues to exist as a Lisp
1172 object, until there are no more references to it. Window deletion can
1173 be reversed, by restoring a saved window configuration (@pxref{Window
1174 Configurations}).
1175
1176 @deffn Command delete-window &optional window
1177 This function removes @var{window} from display and returns
1178 @code{nil}. If @var{window} is omitted or @code{nil}, it defaults to
1179 the selected window. If deleting the window would leave no more
1180 windows in the window tree (e.g., if it is the only live window in the
1181 frame), an error is signaled.
1182
1183 By default, the space taken up by @var{window} is given to one of its
1184 adjacent sibling windows, if any. However, if the variable
1185 @code{window-combination-resize} is non-@code{nil}, the space is
1186 proportionally distributed among any remaining windows in the window
1187 combination. @xref{Recombining Windows}.
1188
1189 The behavior of this function may be altered by the window parameters
1190 of @var{window}, so long as the variable
1191 @code{ignore-window-parameters} is @code{nil}. If the value of
1192 the @code{delete-window} window parameter is @code{t}, this function
1193 ignores all other window parameters. Otherwise, if the value of the
1194 @code{delete-window} window parameter is a function, that function is
1195 called with the argument @var{window}, in lieu of the usual action of
1196 @code{delete-window}. Otherwise, this function obeys the
1197 @code{window-atom} or @code{window-side} window parameter, if any.
1198 @xref{Window Parameters}.
1199 @end deffn
1200
1201 @deffn Command delete-other-windows &optional window
1202 This function makes @var{window} fill its frame, by deleting other
1203 windows as necessary. If @var{window} is omitted or @code{nil}, it
1204 defaults to the selected window. The return value is @code{nil}.
1205
1206 The behavior of this function may be altered by the window parameters
1207 of @var{window}, so long as the variable
1208 @code{ignore-window-parameters} is @code{nil}. If the value of
1209 the @code{delete-other-windows} window parameter is @code{t}, this
1210 function ignores all other window parameters. Otherwise, if the value
1211 of the @code{delete-other-windows} window parameter is a function,
1212 that function is called with the argument @var{window}, in lieu of the
1213 usual action of @code{delete-other-windows}. Otherwise, this function
1214 obeys the @code{window-atom} or @code{window-side} window parameter,
1215 if any. @xref{Window Parameters}.
1216 @end deffn
1217
1218 @deffn Command delete-windows-on &optional buffer-or-name frame
1219 This function deletes all windows showing @var{buffer-or-name}, by
1220 calling @code{delete-window} on those windows. @var{buffer-or-name}
1221 should be a buffer, or the name of a buffer; if omitted or @code{nil},
1222 it defaults to the current buffer. If there are no windows showing
1223 the specified buffer, this function does nothing. If the specified
1224 buffer is a minibuffer, an error is signaled.
1225
1226 If there is a dedicated window showing the buffer, and that window is
1227 the only one on its frame, this function also deletes that frame if it
1228 is not the only frame on the terminal.
1229
1230 The optional argument @var{frame} specifies which frames to operate
1231 on:
1232
1233 @itemize @bullet
1234 @item @code{nil}
1235 means operate on all frames.
1236 @item @code{t}
1237 means operate on the selected frame.
1238 @item @code{visible}
1239 means operate on all visible frames.
1240 @item @code{0}
1241 means operate on all visible or iconified frames.
1242 @item A frame
1243 means operate on that frame.
1244 @end itemize
1245
1246 Note that this argument does not have the same meaning as in other
1247 functions which scan all live windows (@pxref{Cyclic Window
1248 Ordering}). Specifically, the meanings of @code{t} and @code{nil} here
1249 are the opposite of what they are in those other functions.
1250 @end deffn
1251
1252
1253 @node Recombining Windows
1254 @section Recombining Windows
1255 @cindex recombining windows
1256 @cindex windows, recombining
1257
1258 When deleting the last sibling of a window @var{W}, its parent window
1259 is deleted too, with @var{W} replacing it in the window tree. This
1260 means that @var{W} must be recombined with its parent's siblings to
1261 form a new window combination (@pxref{Windows and Frames}). In some
1262 occasions, deleting a live window may even entail the deletion of two
1263 internal windows.
1264
1265 @smallexample
1266 @group
1267 ______________________________________
1268 | ______ ____________________________ |
1269 || || __________________________ ||
1270 || ||| ___________ ___________ |||
1271 || |||| || ||||
1272 || ||||____W6_____||_____W7____||||
1273 || |||____________W4____________|||
1274 || || __________________________ ||
1275 || ||| |||
1276 || ||| |||
1277 || |||____________W5____________|||
1278 ||__W2__||_____________W3_____________ |
1279 |__________________W1__________________|
1280
1281 @end group
1282 @end smallexample
1283
1284 @noindent
1285 Deleting @var{W5} in this configuration normally causes the deletion of
1286 @var{W3} and @var{W4}. The remaining live windows @var{W2},
1287 @var{W6} and @var{W7} are recombined to form a new horizontal
1288 combination with parent @var{W1}.
1289
1290 Sometimes, however, it makes sense to not delete a parent window like
1291 @var{W4}. In particular, a parent window should not be removed when it
1292 was used to preserve a combination embedded in a combination of the same
1293 type. Such embeddings make sense to assure that when you split a window
1294 and subsequently delete the new window, Emacs reestablishes the layout
1295 of the associated frame as it existed before the splitting.
1296
1297 Consider a scenario starting with two live windows @var{W2} and
1298 @var{W3} and their parent @var{W1}.
1299
1300 @smallexample
1301 @group
1302 ______________________________________
1303 | ____________________________________ |
1304 || ||
1305 || ||
1306 || ||
1307 || ||
1308 || ||
1309 || ||
1310 ||_________________W2_________________||
1311 | ____________________________________ |
1312 || ||
1313 || ||
1314 ||_________________W3_________________||
1315 |__________________W1__________________|
1316
1317 @end group
1318 @end smallexample
1319
1320 @noindent
1321 Split @var{W2} to make a new window @var{W4} as follows.
1322
1323 @smallexample
1324 @group
1325 ______________________________________
1326 | ____________________________________ |
1327 || ||
1328 || ||
1329 ||_________________W2_________________||
1330 | ____________________________________ |
1331 || ||
1332 || ||
1333 ||_________________W4_________________||
1334 | ____________________________________ |
1335 || ||
1336 || ||
1337 ||_________________W3_________________||
1338 |__________________W1__________________|
1339
1340 @end group
1341 @end smallexample
1342
1343 @noindent
1344 Now, when enlarging a window vertically, Emacs tries to obtain the
1345 corresponding space from its lower sibling, provided such a window
1346 exists. In our scenario, enlarging @var{W4} will steal space from
1347 @var{W3}.
1348
1349 @smallexample
1350 @group
1351 ______________________________________
1352 | ____________________________________ |
1353 || ||
1354 || ||
1355 ||_________________W2_________________||
1356 | ____________________________________ |
1357 || ||
1358 || ||
1359 || ||
1360 || ||
1361 ||_________________W4_________________||
1362 | ____________________________________ |
1363 ||_________________W3_________________||
1364 |__________________W1__________________|
1365
1366 @end group
1367 @end smallexample
1368
1369 @noindent
1370 Deleting @var{W4} will now give its entire space to @var{W2},
1371 including the space earlier stolen from @var{W3}.
1372
1373 @smallexample
1374 @group
1375 ______________________________________
1376 | ____________________________________ |
1377 || ||
1378 || ||
1379 || ||
1380 || ||
1381 || ||
1382 || ||
1383 || ||
1384 || ||
1385 ||_________________W2_________________||
1386 | ____________________________________ |
1387 ||_________________W3_________________||
1388 |__________________W1__________________|
1389
1390 @end group
1391 @end smallexample
1392
1393 @noindent
1394 This can be counterintuitive, in particular if @var{W4} were used for
1395 displaying a buffer only temporarily (@pxref{Temporary Displays}), and
1396 you want to continue working with the initial layout.
1397
1398 The behavior can be fixed by making a new parent window when splitting
1399 @var{W2}. The variable described next allows to do that.
1400
1401 @defopt window-combination-limit
1402 This variable controls whether splitting a window shall make a new
1403 parent window. The following values are recognized:
1404
1405 @table @code
1406 @item nil
1407 This means that the new live window is allowed to share the existing
1408 parent window, if one exists, provided the split occurs in the same
1409 direction as the existing window combination (otherwise, a new internal
1410 window is created anyway).
1411
1412 @item window-size
1413 In this case @code{display-buffer} makes a new parent window if it is
1414 passed a @code{window-height} or @code{window-width} entry in the
1415 @var{alist} argument (@pxref{Display Action Functions}).
1416
1417 @item temp-buffer
1418 This value causes the creation of a new parent window when a window is
1419 split for showing a temporary buffer (@pxref{Temporary Displays}) only.
1420
1421 @item display-buffer
1422 This means that when @code{display-buffer} (@pxref{Choosing Window})
1423 splits a window it always makes a new parent window.
1424
1425 @item t
1426 In this case a new parent window is always created when splitting a
1427 window. Thus, if the value of this variable is at all times @code{t},
1428 then at all times every window tree is a binary tree (a tree where each
1429 window except the root window has exactly one sibling).
1430 @end table
1431
1432 The default is @code{nil}. Other values are reserved for future use.
1433
1434 If, as a consequence of this variable's setting, @code{split-window}
1435 makes a new parent window, it also calls
1436 @code{set-window-combination-limit} (see below) on the newly-created
1437 internal window. This affects how the window tree is rearranged when
1438 the child windows are deleted (see below).
1439 @end defopt
1440
1441 If @code{window-combination-limit} is @code{t}, splitting @var{W2} in
1442 the initial configuration of our scenario would have produced this:
1443
1444 @smallexample
1445 @group
1446 ______________________________________
1447 | ____________________________________ |
1448 || __________________________________ ||
1449 ||| |||
1450 |||________________W2________________|||
1451 || __________________________________ ||
1452 ||| |||
1453 |||________________W4________________|||
1454 ||_________________W5_________________||
1455 | ____________________________________ |
1456 || ||
1457 || ||
1458 ||_________________W3_________________||
1459 |__________________W1__________________|
1460
1461 @end group
1462 @end smallexample
1463
1464 @noindent
1465 A new internal window @var{W5} has been created; its children are
1466 @var{W2} and the new live window @var{W4}. Now, @var{W2} is the only
1467 sibling of @var{W4}, so enlarging @var{W4} will try to shrink
1468 @var{W2}, leaving @var{W3} unaffected. Observe that @var{W5}
1469 represents a vertical combination of two windows embedded in the
1470 vertical combination @var{W1}.
1471
1472 @cindex window combination limit
1473 @defun set-window-combination-limit window limit
1474 This function sets the @dfn{combination limit} of the window
1475 @var{window} to @var{limit}. This value can be retrieved via the
1476 function @code{window-combination-limit}. See below for its effects;
1477 note that it is only meaningful for internal windows. The
1478 @code{split-window} function automatically calls this function, passing
1479 it @code{t} as @var{limit}, provided the value of the variable
1480 @code{window-combination-limit} is @code{t} when it is called.
1481 @end defun
1482
1483 @defun window-combination-limit window
1484 This function returns the combination limit for @var{window}.
1485
1486 The combination limit is meaningful only for an internal window. If it
1487 is @code{nil}, then Emacs is allowed to automatically delete
1488 @var{window}, in response to a window deletion, in order to group the
1489 child windows of @var{window} with its sibling windows to form a new
1490 window combination. If the combination limit is @code{t}, the child
1491 windows of @var{window} are never automatically recombined with its
1492 siblings.
1493
1494 If, in the configuration shown at the beginning of this section, the
1495 combination limit of @var{W4} (the parent window of @var{W6} and
1496 @var{W7}) is @code{t}, deleting @var{W5} will not implicitly delete
1497 @var{W4} too.
1498 @end defun
1499
1500 Alternatively, the problems sketched above can be avoided by always
1501 resizing all windows in the same combination whenever one of its windows
1502 is split or deleted. This also permits to split windows that would be
1503 otherwise too small for such an operation.
1504
1505 @defopt window-combination-resize
1506 If this variable is @code{nil}, @code{split-window} can only split a
1507 window (denoted by @var{window}) if @var{window}'s screen area is large
1508 enough to accommodate both itself and the new window.
1509
1510 If this variable is @code{t}, @code{split-window} tries to resize all
1511 windows that are part of the same combination as @var{window}, in order
1512 to accommodate the new window. In particular, this may allow
1513 @code{split-window} to succeed even if @var{window} is a fixed-size
1514 window or too small to ordinarily split. Furthermore, subsequently
1515 resizing or deleting @var{window} may resize all other windows in its
1516 combination.
1517
1518 The default is @code{nil}. Other values are reserved for future use.
1519 The value of this variable is ignored when
1520 @code{window-combination-limit} is non-@code{nil}.
1521 @end defopt
1522
1523 To illustrate the effect of @code{window-combination-resize}, consider
1524 the following frame layout.
1525
1526 @smallexample
1527 @group
1528 ______________________________________
1529 | ____________________________________ |
1530 || ||
1531 || ||
1532 || ||
1533 || ||
1534 ||_________________W2_________________||
1535 | ____________________________________ |
1536 || ||
1537 || ||
1538 || ||
1539 || ||
1540 ||_________________W3_________________||
1541 |__________________W1__________________|
1542
1543 @end group
1544 @end smallexample
1545
1546 @noindent
1547 If @code{window-combination-resize} is @code{nil}, splitting window
1548 @var{W3} leaves the size of @var{W2} unchanged:
1549
1550 @smallexample
1551 @group
1552 ______________________________________
1553 | ____________________________________ |
1554 || ||
1555 || ||
1556 || ||
1557 || ||
1558 ||_________________W2_________________||
1559 | ____________________________________ |
1560 || ||
1561 ||_________________W3_________________||
1562 | ____________________________________ |
1563 || ||
1564 ||_________________W4_________________||
1565 |__________________W1__________________|
1566
1567 @end group
1568 @end smallexample
1569
1570 @noindent
1571 If @code{window-combination-resize} is @code{t}, splitting @var{W3}
1572 instead leaves all three live windows with approximately the same
1573 height:
1574
1575 @smallexample
1576 @group
1577 ______________________________________
1578 | ____________________________________ |
1579 || ||
1580 || ||
1581 ||_________________W2_________________||
1582 | ____________________________________ |
1583 || ||
1584 || ||
1585 ||_________________W3_________________||
1586 | ____________________________________ |
1587 || ||
1588 || ||
1589 ||_________________W4_________________||
1590 |__________________W1__________________|
1591
1592 @end group
1593 @end smallexample
1594
1595 @noindent
1596 Deleting any of the live windows @var{W2}, @var{W3} or @var{W4} will
1597 distribute its space proportionally among the two remaining live
1598 windows.
1599
1600
1601 @node Selecting Windows
1602 @section Selecting Windows
1603 @cindex selecting a window
1604
1605 @defun select-window window &optional norecord
1606 This function makes @var{window} the selected window and the window
1607 selected within its frame (@pxref{Basic Windows}) and selects that
1608 frame. It also makes @var{window}'s buffer (@pxref{Buffers and
1609 Windows}) current and sets that buffer's value of @code{point} to the
1610 value of @code{window-point} (@pxref{Window Point}) in @var{window}.
1611 @var{window} must be a live window. The return value is @var{window}.
1612
1613 By default, this function also moves @var{window}'s buffer to the front
1614 of the buffer list (@pxref{Buffer List}), and makes @var{window} the
1615 most recently selected window. However, if the optional argument
1616 @var{norecord} is non-@code{nil}, these additional actions are omitted.
1617
1618 This function runs @code{buffer-list-update-hook} (@pxref{Buffer List})
1619 unless @var{norecord} is non-@code{nil}. Note that applications and
1620 internal routines often temporarily select a window in order to simplify
1621 coding. As a rule, such selections (including those made by the macros
1622 @code{save-selected-window} and @code{with-selected-window} below) are
1623 not recorded thus avoiding to pollute @code{buffer-list-update-hook}.
1624 Selections that ``really count'' are those causing a visible change in
1625 the next redisplay of @var{window}'s frame and should be always
1626 recorded. This also means that to run a function each time a window
1627 gets selected, putting it on @code{buffer-list-update-hook} should be
1628 the right choice.
1629 @end defun
1630
1631 @cindex most recently selected windows
1632 The sequence of calls to @code{select-window} with a non-@code{nil}
1633 @var{norecord} argument determines an ordering of windows by their
1634 selection time. The function @code{get-lru-window} can be used to
1635 retrieve the least recently selected live window (@pxref{Cyclic Window
1636 Ordering}).
1637
1638 @defmac save-selected-window forms@dots{}
1639 This macro records the selected frame, as well as the selected window
1640 of each frame, executes @var{forms} in sequence, then restores the
1641 earlier selected frame and windows. It also saves and restores the
1642 current buffer. It returns the value of the last form in @var{forms}.
1643
1644 This macro does not save or restore anything about the sizes,
1645 arrangement or contents of windows; therefore, if @var{forms} change
1646 them, the change persists. If the previously selected window of some
1647 frame is no longer live at the time of exit from @var{forms}, that
1648 frame's selected window is left alone. If the previously selected
1649 window is no longer live, then whatever window is selected at the end of
1650 @var{forms} remains selected. The current buffer is restored if and
1651 only if it is still live when exiting @var{forms}.
1652
1653 This macro changes neither the ordering of recently selected windows nor
1654 the buffer list.
1655 @end defmac
1656
1657 @defmac with-selected-window window forms@dots{}
1658 This macro selects @var{window}, executes @var{forms} in sequence, then
1659 restores the previously selected window and current buffer. The ordering
1660 of recently selected windows and the buffer list remain unchanged unless
1661 you deliberately change them within @var{forms}; for example, by calling
1662 @code{select-window} with argument @var{norecord} @code{nil}.
1663
1664 This macro does not change the order of recently selected windows or
1665 the buffer list.
1666 @end defmac
1667
1668 @defun frame-selected-window &optional frame
1669 This function returns the window on @var{frame} that is selected
1670 within that frame. @var{frame} should be a live frame; if omitted or
1671 @code{nil}, it defaults to the selected frame.
1672 @end defun
1673
1674 @defun set-frame-selected-window frame window &optional norecord
1675 This function makes @var{window} the window selected within the frame
1676 @var{frame}. @var{frame} should be a live frame; if @code{nil}, it
1677 defaults to the selected frame. @var{window} should be a live window;
1678 if @code{nil}, it defaults to the selected window.
1679
1680 If @var{frame} is the selected frame, this makes @var{window} the
1681 selected window.
1682
1683 If the optional argument @var{norecord} is non-@code{nil}, this
1684 function does not alter the list of most recently selected windows,
1685 nor the buffer list.
1686 @end defun
1687
1688 @node Cyclic Window Ordering
1689 @section Cyclic Ordering of Windows
1690 @cindex cyclic ordering of windows
1691 @cindex ordering of windows, cyclic
1692 @cindex window ordering, cyclic
1693
1694 When you use the command @kbd{C-x o} (@code{other-window}) to select
1695 some other window, it moves through live windows in a specific order.
1696 For any given configuration of windows, this order never varies. It
1697 is called the @dfn{cyclic ordering of windows}.
1698
1699 The ordering is determined by a depth-first traversal of the frame's
1700 window tree, retrieving the live windows which are the leaf nodes of
1701 the tree (@pxref{Windows and Frames}). If the minibuffer is active,
1702 the minibuffer window is included too. The ordering is cyclic, so the
1703 last window in the sequence is followed by the first one.
1704
1705 @defun next-window &optional window minibuf all-frames
1706 @cindex minibuffer window, and @code{next-window}
1707 This function returns a live window, the one following @var{window} in
1708 the cyclic ordering of windows. @var{window} should be a live window;
1709 if omitted or @code{nil}, it defaults to the selected window.
1710
1711 The optional argument @var{minibuf} specifies whether minibuffer windows
1712 should be included in the cyclic ordering. Normally, when @var{minibuf}
1713 is @code{nil}, a minibuffer window is included only if it is currently
1714 ``active''; this matches the behavior of @kbd{C-x o}. (Note that a
1715 minibuffer window is active as long as its minibuffer is in use; see
1716 @ref{Minibuffers}).
1717
1718 If @var{minibuf} is @code{t}, the cyclic ordering includes all
1719 minibuffer windows. If @var{minibuf} is neither @code{t} nor
1720 @code{nil}, minibuffer windows are not included even if they are active.
1721
1722 The optional argument @var{all-frames} specifies which frames to
1723 consider:
1724
1725 @itemize @bullet
1726 @item @code{nil}
1727 means to consider windows on @var{window}'s frame. If the minibuffer
1728 window is considered (as specified by the @var{minibuf} argument),
1729 then frames that share the minibuffer window are considered too.
1730
1731 @item @code{t}
1732 means to consider windows on all existing frames.
1733
1734 @item @code{visible}
1735 means to consider windows on all visible frames.
1736
1737 @item 0
1738 means to consider windows on all visible or iconified frames.
1739
1740 @item A frame
1741 means to consider windows on that specific frame.
1742
1743 @item Anything else
1744 means to consider windows on @var{window}'s frame, and no others.
1745 @end itemize
1746
1747 If more than one frame is considered, the cyclic ordering is obtained
1748 by appending the orderings for those frames, in the same order as the
1749 list of all live frames (@pxref{Finding All Frames}).
1750 @end defun
1751
1752 @defun previous-window &optional window minibuf all-frames
1753 This function returns a live window, the one preceding @var{window} in
1754 the cyclic ordering of windows. The other arguments are handled like
1755 in @code{next-window}.
1756 @end defun
1757
1758 @deffn Command other-window count &optional all-frames
1759 This function selects a live window, one @var{count} places from the
1760 selected window in the cyclic ordering of windows. If @var{count} is
1761 a positive number, it skips @var{count} windows forwards; if
1762 @var{count} is negative, it skips @minus{}@var{count} windows
1763 backwards; if @var{count} is zero, that simply re-selects the selected
1764 window. When called interactively, @var{count} is the numeric prefix
1765 argument.
1766
1767 The optional argument @var{all-frames} has the same meaning as in
1768 @code{next-window}, like a @code{nil} @var{minibuf} argument to
1769 @code{next-window}.
1770
1771 This function does not select a window that has a non-@code{nil}
1772 @code{no-other-window} window parameter (@pxref{Window Parameters}).
1773 @end deffn
1774
1775 @defun walk-windows fun &optional minibuf all-frames
1776 This function calls the function @var{fun} once for each live window,
1777 with the window as the argument.
1778
1779 It follows the cyclic ordering of windows. The optional arguments
1780 @var{minibuf} and @var{all-frames} specify the set of windows
1781 included; these have the same arguments as in @code{next-window}. If
1782 @var{all-frames} specifies a frame, the first window walked is the
1783 first window on that frame (the one returned by
1784 @code{frame-first-window}), not necessarily the selected window.
1785
1786 If @var{fun} changes the window configuration by splitting or deleting
1787 windows, that does not alter the set of windows walked, which is
1788 determined prior to calling @var{fun} for the first time.
1789 @end defun
1790
1791 @defun one-window-p &optional no-mini all-frames
1792 This function returns @code{t} if the selected window is the only live
1793 window, and @code{nil} otherwise.
1794
1795 If the minibuffer window is active, it is normally considered (so that
1796 this function returns @code{nil}). However, if the optional argument
1797 @var{no-mini} is non-@code{nil}, the minibuffer window is ignored even
1798 if active. The optional argument @var{all-frames} has the same
1799 meaning as for @code{next-window}.
1800 @end defun
1801
1802 @cindex finding windows
1803 The following functions return a window which satisfies some
1804 criterion, without selecting it:
1805
1806 @cindex least recently used window
1807 @defun get-lru-window &optional all-frames dedicated not-selected
1808 This function returns a live window which is heuristically the ``least
1809 recently used'' window. The optional argument @var{all-frames} has
1810 the same meaning as in @code{next-window}.
1811
1812 If any full-width windows are present, only those windows are
1813 considered. A minibuffer window is never a candidate. A dedicated
1814 window (@pxref{Dedicated Windows}) is never a candidate unless the
1815 optional argument @var{dedicated} is non-@code{nil}. The selected
1816 window is never returned, unless it is the only candidate. However, if
1817 the optional argument @var{not-selected} is non-@code{nil}, this
1818 function returns @code{nil} in that case.
1819 @end defun
1820
1821 @cindex largest window
1822 @defun get-largest-window &optional all-frames dedicated not-selected
1823 This function returns the window with the largest area (height times
1824 width). The optional argument @var{all-frames} specifies the windows to
1825 search, and has the same meaning as in @code{next-window}.
1826
1827 A minibuffer window is never a candidate. A dedicated window
1828 (@pxref{Dedicated Windows}) is never a candidate unless the optional
1829 argument @var{dedicated} is non-@code{nil}. The selected window is not
1830 a candidate if the optional argument @var{not-selected} is
1831 non-@code{nil}. If the optional argument @var{not-selected} is
1832 non-@code{nil} and the selected window is the only candidate, this
1833 function returns @code{nil}.
1834
1835 If there are two candidate windows of the same size, this function
1836 prefers the one that comes first in the cyclic ordering of windows,
1837 starting from the selected window.
1838 @end defun
1839
1840 @cindex window that satisfies a predicate
1841 @cindex conditional selection of windows
1842 @defun get-window-with-predicate predicate &optional minibuf all-frames default
1843 This function calls the function @var{predicate} for each of the
1844 windows in the cyclic order of windows in turn, passing it the window
1845 as an argument. If the predicate returns non-@code{nil} for any
1846 window, this function stops and returns that window. If no such
1847 window is found, the return value is @var{default} (which defaults to
1848 @code{nil}).
1849
1850 The optional arguments @var{minibuf} and @var{all-frames} specify the
1851 windows to search, and have the same meanings as in
1852 @code{next-window}.
1853 @end defun
1854
1855
1856 @node Buffers and Windows
1857 @section Buffers and Windows
1858 @cindex examining windows
1859 @cindex windows, controlling precisely
1860 @cindex buffers, controlled in windows
1861
1862 This section describes low-level functions for examining and setting
1863 the contents of windows. @xref{Switching Buffers}, for higher-level
1864 functions for displaying a specific buffer in a window.
1865
1866 @defun window-buffer &optional window
1867 This function returns the buffer that @var{window} is displaying. If
1868 @var{window} is omitted or @code{nil} it defaults to the selected
1869 window. If @var{window} is an internal window, this function returns
1870 @code{nil}.
1871 @end defun
1872
1873 @defun set-window-buffer window buffer-or-name &optional keep-margins
1874 This function makes @var{window} display @var{buffer-or-name}.
1875 @var{window} should be a live window; if @code{nil}, it defaults to
1876 the selected window. @var{buffer-or-name} should be a buffer, or the
1877 name of an existing buffer. This function does not change which
1878 window is selected, nor does it directly change which buffer is
1879 current (@pxref{Current Buffer}). Its return value is @code{nil}.
1880
1881 If @var{window} is @dfn{strongly dedicated} to a buffer and
1882 @var{buffer-or-name} does not specify that buffer, this function
1883 signals an error. @xref{Dedicated Windows}.
1884
1885 By default, this function resets @var{window}'s position, display
1886 margins, fringe widths, and scroll bar settings, based on the local
1887 variables in the specified buffer. However, if the optional argument
1888 @var{keep-margins} is non-@code{nil}, it leaves the display margins
1889 and fringe widths unchanged.
1890
1891 When writing an application, you should normally use the higher-level
1892 functions described in @ref{Switching Buffers}, instead of calling
1893 @code{set-window-buffer} directly.
1894
1895 This runs @code{window-scroll-functions}, followed by
1896 @code{window-configuration-change-hook}. @xref{Window Hooks}.
1897 @end defun
1898
1899 @defvar buffer-display-count
1900 This buffer-local variable records the number of times a buffer has been
1901 displayed in a window. It is incremented each time
1902 @code{set-window-buffer} is called for the buffer.
1903 @end defvar
1904
1905 @defvar buffer-display-time
1906 This buffer-local variable records the time at which a buffer was last
1907 displayed in a window. The value is @code{nil} if the buffer has
1908 never been displayed. It is updated each time
1909 @code{set-window-buffer} is called for the buffer, with the value
1910 returned by @code{current-time} (@pxref{Time of Day}).
1911 @end defvar
1912
1913 @defun get-buffer-window &optional buffer-or-name all-frames
1914 This function returns the first window displaying @var{buffer-or-name}
1915 in the cyclic ordering of windows, starting from the selected window
1916 (@pxref{Cyclic Window Ordering}). If no such window exists, the
1917 return value is @code{nil}.
1918
1919 @var{buffer-or-name} should be a buffer or the name of a buffer; if
1920 omitted or @code{nil}, it defaults to the current buffer. The
1921 optional argument @var{all-frames} specifies which windows to
1922 consider:
1923
1924 @itemize @bullet
1925 @item
1926 @code{t} means consider windows on all existing frames.
1927 @item
1928 @code{visible} means consider windows on all visible frames.
1929 @item
1930 0 means consider windows on all visible or iconified frames.
1931 @item
1932 A frame means consider windows on that frame only.
1933 @item
1934 Any other value means consider windows on the selected frame.
1935 @end itemize
1936
1937 Note that these meanings differ slightly from those of the
1938 @var{all-frames} argument to @code{next-window} (@pxref{Cyclic Window
1939 Ordering}). This function may be changed in a future version of Emacs
1940 to eliminate this discrepancy.
1941 @end defun
1942
1943 @defun get-buffer-window-list &optional buffer-or-name minibuf all-frames
1944 This function returns a list of all windows currently displaying
1945 @var{buffer-or-name}. @var{buffer-or-name} should be a buffer or the
1946 name of an existing buffer. If omitted or @code{nil}, it defaults to
1947 the current buffer.
1948
1949 The arguments @var{minibuf} and @var{all-frames} have the same
1950 meanings as in the function @code{next-window} (@pxref{Cyclic Window
1951 Ordering}). Note that the @var{all-frames} argument does @emph{not}
1952 behave exactly like in @code{get-buffer-window}.
1953 @end defun
1954
1955 @deffn Command replace-buffer-in-windows &optional buffer-or-name
1956 This command replaces @var{buffer-or-name} with some other buffer, in
1957 all windows displaying it. @var{buffer-or-name} should be a buffer, or
1958 the name of an existing buffer; if omitted or @code{nil}, it defaults to
1959 the current buffer.
1960
1961 The replacement buffer in each window is chosen via
1962 @code{switch-to-prev-buffer} (@pxref{Window History}). Any dedicated
1963 window displaying @var{buffer-or-name} is deleted if possible
1964 (@pxref{Dedicated Windows}). If such a window is the only window on its
1965 frame and there are other frames on the same terminal, the frame is
1966 deleted as well. If the dedicated window is the only window on the only
1967 frame on its terminal, the buffer is replaced anyway.
1968 @end deffn
1969
1970
1971 @node Switching Buffers
1972 @section Switching to a Buffer in a Window
1973 @cindex switching to a buffer
1974 @cindex displaying a buffer
1975
1976 This section describes high-level functions for switching to a specified
1977 buffer in some window. In general, ``switching to a buffer'' means to
1978 (1) show the buffer in some window, (2) make that window the selected
1979 window (and its frame the selected frame), and (3) make the buffer the
1980 current buffer.
1981
1982 Do @emph{not} use these functions to make a buffer temporarily
1983 current just so a Lisp program can access or modify it. They have
1984 side-effects, such as changing window histories (@pxref{Window
1985 History}), which will surprise the user if used that way. If you want
1986 to make a buffer current to modify it in Lisp, use
1987 @code{with-current-buffer}, @code{save-current-buffer}, or
1988 @code{set-buffer}. @xref{Current Buffer}.
1989
1990 @deffn Command switch-to-buffer buffer-or-name &optional norecord force-same-window
1991 This command attempts to display @var{buffer-or-name} in the selected
1992 window and make it the current buffer. It is often used interactively
1993 (as the binding of @kbd{C-x b}), as well as in Lisp programs. The
1994 return value is the buffer switched to.
1995
1996 If @var{buffer-or-name} is @code{nil}, it defaults to the buffer
1997 returned by @code{other-buffer} (@pxref{Buffer List}). If
1998 @var{buffer-or-name} is a string that is not the name of any existing
1999 buffer, this function creates a new buffer with that name; the new
2000 buffer's major mode is determined by the variable @code{major-mode}
2001 (@pxref{Major Modes}).
2002
2003 Normally, the specified buffer is put at the front of the buffer
2004 list---both the global buffer list and the selected frame's buffer
2005 list (@pxref{Buffer List}). However, this is not done if the
2006 optional argument @var{norecord} is non-@code{nil}.
2007
2008 Sometimes, the selected window may not be suitable for displaying the
2009 buffer. This happens if the selected window is a minibuffer window, or
2010 if the selected window is strongly dedicated to its buffer
2011 (@pxref{Dedicated Windows}). In such cases, the command normally tries
2012 to display the buffer in some other window, by invoking
2013 @code{pop-to-buffer} (see below).
2014
2015 If the optional argument @var{force-same-window} is non-@code{nil} and
2016 the selected window is not suitable for displaying the buffer, this
2017 function always signals an error when called non-interactively. In
2018 interactive use, if the selected window is a minibuffer window, this
2019 function will try to use some other window instead. If the selected
2020 window is strongly dedicated to its buffer, the option
2021 @code{switch-to-buffer-in-dedicated-window} described next can be used
2022 to proceed.
2023 @end deffn
2024
2025 @defopt switch-to-buffer-in-dedicated-window
2026 This option, if non-@code{nil}, allows @code{switch-to-buffer} to
2027 proceed when called interactively and the selected window is strongly
2028 dedicated to its buffer.
2029
2030 The following values are respected:
2031
2032 @table @code
2033 @item nil
2034 Disallows switching and signals an error as in non-interactive use.
2035
2036 @item prompt
2037 Prompts the user whether to allow switching.
2038
2039 @item pop
2040 Invokes @code{pop-to-buffer} to proceed.
2041
2042 @item t
2043 Marks the selected window as non-dedicated and proceeds.
2044 @end table
2045
2046 When called non-interactively, @code{switch-to-buffer} always signals an
2047 error when the selected window is dedicated to its buffer and
2048 @var{force-same-window} is non-@code{nil}.
2049 @end defopt
2050
2051 By default, @code{switch-to-buffer} shows the buffer at its position of
2052 @code{point}. This behavior can be tuned using the following option.
2053
2054 @defopt switch-to-buffer-preserve-window-point
2055 If this variable is @code{nil}, @code{switch-to-buffer} displays the
2056 buffer specified by @var{buffer-or-name} at the position of that
2057 buffer's @code{point}. If this variable is @code{already-displayed}, it
2058 tries to display the buffer at its previous position in the selected
2059 window, provided the buffer is currently displayed in some other window
2060 on any visible or iconified frame. If this variable is @code{t},
2061 @code{switch-to-buffer} unconditionally tries to display the buffer at
2062 its previous position in the selected window.
2063
2064 This variable is ignored if the buffer is already displayed in the
2065 selected window or never appeared in it before, or if
2066 @code{switch-to-buffer} calls @code{pop-to-buffer} to display the
2067 buffer.
2068 @end defopt
2069
2070 The next two commands are similar to @code{switch-to-buffer}, except for
2071 the described features.
2072
2073 @deffn Command switch-to-buffer-other-window buffer-or-name &optional norecord
2074 This function displays the buffer specified by @var{buffer-or-name} in
2075 some window other than the selected window. It uses the function
2076 @code{pop-to-buffer} internally (see below).
2077
2078 If the selected window already displays the specified buffer, it
2079 continues to do so, but another window is nonetheless found to display
2080 it as well.
2081
2082 The @var{buffer-or-name} and @var{norecord} arguments have the same
2083 meanings as in @code{switch-to-buffer}.
2084 @end deffn
2085
2086 @deffn Command switch-to-buffer-other-frame buffer-or-name &optional norecord
2087 This function displays the buffer specified by @var{buffer-or-name} in a
2088 new frame. It uses the function @code{pop-to-buffer} internally (see
2089 below).
2090
2091 If the specified buffer is already displayed in another window, in any
2092 frame on the current terminal, this switches to that window instead of
2093 creating a new frame. However, the selected window is never used for
2094 this.
2095
2096 The @var{buffer-or-name} and @var{norecord} arguments have the same
2097 meanings as in @code{switch-to-buffer}.
2098 @end deffn
2099
2100 The above commands use the function @code{pop-to-buffer}, which
2101 flexibly displays a buffer in some window and selects that window for
2102 editing. In turn, @code{pop-to-buffer} uses @code{display-buffer} for
2103 displaying the buffer. Hence, all the variables affecting
2104 @code{display-buffer} will affect it as well. @xref{Choosing Window},
2105 for the documentation of @code{display-buffer}.
2106
2107 @deffn Command pop-to-buffer buffer-or-name &optional action norecord
2108 This function makes @var{buffer-or-name} the current buffer and
2109 displays it in some window, preferably not the window previously
2110 selected. It then selects the displaying window. If that window is
2111 on a different graphical frame, that frame is given input focus if
2112 possible (@pxref{Input Focus}). The return value is the buffer that
2113 was switched to.
2114
2115 If @var{buffer-or-name} is @code{nil}, it defaults to the buffer
2116 returned by @code{other-buffer} (@pxref{Buffer List}). If
2117 @var{buffer-or-name} is a string that is not the name of any existing
2118 buffer, this function creates a new buffer with that name; the new
2119 buffer's major mode is determined by the variable @code{major-mode}
2120 (@pxref{Major Modes}).
2121
2122 If @var{action} is non-@code{nil}, it should be a display action to
2123 pass to @code{display-buffer} (@pxref{Choosing Window}).
2124 Alternatively, a non-@code{nil}, non-list value means to pop to a
2125 window other than the selected one---even if the buffer is already
2126 displayed in the selected window.
2127
2128 Like @code{switch-to-buffer}, this function updates the buffer list
2129 unless @var{norecord} is non-@code{nil}.
2130 @end deffn
2131
2132
2133 @node Choosing Window
2134 @section Choosing a Window for Display
2135
2136 The command @code{display-buffer} flexibly chooses a window for
2137 display, and displays a specified buffer in that window. It can be
2138 called interactively, via the key binding @kbd{C-x 4 C-o}. It is also
2139 used as a subroutine by many functions and commands, including
2140 @code{switch-to-buffer} and @code{pop-to-buffer} (@pxref{Switching
2141 Buffers}).
2142
2143 @cindex display action
2144 @cindex action function, for @code{display-buffer}
2145 @cindex action alist, for @code{display-buffer}
2146 This command performs several complex steps to find a window to
2147 display in. These steps are described by means of @dfn{display
2148 actions}, which have the form @code{(@var{function} . @var{alist})}.
2149 Here, @var{function} is either a function or a list of functions,
2150 which we refer to as @dfn{action functions}; @var{alist} is an
2151 association list, which we refer to as @dfn{action alists}.
2152
2153 An action function accepts two arguments: the buffer to display and
2154 an action alist. It attempts to display the buffer in some window,
2155 picking or creating a window according to its own criteria. If
2156 successful, it returns the window; otherwise, it returns @code{nil}.
2157 @xref{Display Action Functions}, for a list of predefined action
2158 functions.
2159
2160 @code{display-buffer} works by combining display actions from
2161 several sources, and calling the action functions in turn, until one
2162 of them manages to display the buffer and returns a non-@code{nil}
2163 value.
2164
2165 @deffn Command display-buffer buffer-or-name &optional action frame
2166 This command makes @var{buffer-or-name} appear in some window, without
2167 selecting the window or making the buffer current. The argument
2168 @var{buffer-or-name} must be a buffer or the name of an existing
2169 buffer. The return value is the window chosen to display the buffer.
2170
2171 The optional argument @var{action}, if non-@code{nil}, should normally
2172 be a display action (described above). @code{display-buffer} builds a
2173 list of action functions and an action alist, by consolidating display
2174 actions from the following sources (in order):
2175
2176 @itemize
2177 @item
2178 The variable @code{display-buffer-overriding-action}.
2179
2180 @item
2181 The user option @code{display-buffer-alist}.
2182
2183 @item
2184 The @var{action} argument.
2185
2186 @item
2187 The user option @code{display-buffer-base-action}.
2188
2189 @item
2190 The constant @code{display-buffer-fallback-action}.
2191 @end itemize
2192
2193 @noindent
2194 Each action function is called in turn, passing the buffer as the
2195 first argument and the combined action alist as the second argument,
2196 until one of the functions returns non-@code{nil}. The caller can
2197 pass @code{(allow-no-window . t)} as an element of the action alist to
2198 indicate its readiness to handle the case of not displaying the
2199 buffer in a window.
2200
2201 The argument @var{action} can also have a non-@code{nil}, non-list
2202 value. This has the special meaning that the buffer should be
2203 displayed in a window other than the selected one, even if the
2204 selected window is already displaying it. If called interactively
2205 with a prefix argument, @var{action} is @code{t}.
2206
2207 The optional argument @var{frame}, if non-@code{nil}, specifies which
2208 frames to check when deciding whether the buffer is already displayed.
2209 It is equivalent to adding an element @code{(reusable-frames
2210 . @var{frame})} to the action alist of @var{action}. @xref{Display
2211 Action Functions}.
2212 @end deffn
2213
2214 @defvar display-buffer-overriding-action
2215 The value of this variable should be a display action, which is
2216 treated with the highest priority by @code{display-buffer}. The
2217 default value is empty, i.e., @code{(nil . nil)}.
2218 @end defvar
2219
2220 @defopt display-buffer-alist
2221 The value of this option is an alist mapping conditions to display
2222 actions. Each condition may be either a regular expression matching a
2223 buffer name or a function that takes two arguments: a buffer name and
2224 the @var{action} argument passed to @code{display-buffer}. If the name
2225 of the buffer passed to @code{display-buffer} either matches a regular
2226 expression in this alist or the function specified by a condition
2227 returns non-@code{nil}, then @code{display-buffer} uses the
2228 corresponding display action to display the buffer.
2229 @end defopt
2230
2231 @defopt display-buffer-base-action
2232 The value of this option should be a display action. This option can
2233 be used to define a ``standard'' display action for calls to
2234 @code{display-buffer}.
2235 @end defopt
2236
2237 @defvr Constant display-buffer-fallback-action
2238 This display action specifies the fallback behavior for
2239 @code{display-buffer} if no other display actions are given.
2240 @end defvr
2241
2242
2243 @node Display Action Functions
2244 @section Action Functions for @code{display-buffer}
2245
2246 The following basic action functions are defined in Emacs. Each of
2247 these functions takes two arguments: @var{buffer}, the buffer to
2248 display, and @var{alist}, an action alist. Each action function
2249 returns the window if it succeeds, and @code{nil} if it fails.
2250
2251 @defun display-buffer-same-window buffer alist
2252 This function tries to display @var{buffer} in the selected window.
2253 It fails if the selected window is a minibuffer window or is dedicated
2254 to another buffer (@pxref{Dedicated Windows}). It also fails if
2255 @var{alist} has a non-@code{nil} @code{inhibit-same-window} entry.
2256 @end defun
2257
2258 @defun display-buffer-reuse-window buffer alist
2259 This function tries to ``display'' @var{buffer} by finding a window
2260 that is already displaying it.
2261
2262 If @var{alist} has a non-@code{nil} @code{inhibit-same-window} entry,
2263 the selected window is not eligible for reuse. If @var{alist}
2264 contains a @code{reusable-frames} entry, its value determines which
2265 frames to search for a reusable window:
2266
2267 @itemize @bullet
2268 @item
2269 @code{nil} means consider windows on the selected frame.
2270 (Actually, the last non-minibuffer frame.)
2271 @item
2272 @code{t} means consider windows on all frames.
2273 @item
2274 @code{visible} means consider windows on all visible frames.
2275 @item
2276 0 means consider windows on all visible or iconified frames.
2277 @item
2278 A frame means consider windows on that frame only.
2279 @end itemize
2280
2281 Note that these meanings differ slightly from those of the
2282 @var{all-frames} argument to @code{next-window} (@pxref{Cyclic Window
2283 Ordering}).
2284
2285 If @var{alist} contains no @code{reusable-frames} entry, this function
2286 normally searches just the selected frame; however, if the variable
2287 @code{pop-up-frames} is non-@code{nil}, it searches all frames on the
2288 current terminal. @xref{Choosing Window Options}.
2289
2290 If this function chooses a window on another frame, it makes that frame
2291 visible and, unless @var{alist} contains an @code{inhibit-switch-frame}
2292 entry (@pxref{Choosing Window Options}), raises that frame if necessary.
2293 @end defun
2294
2295 @defun display-buffer-pop-up-frame buffer alist
2296 This function creates a new frame, and displays the buffer in that
2297 frame's window. It actually performs the frame creation by calling
2298 the function specified in @code{pop-up-frame-function}
2299 (@pxref{Choosing Window Options}). If @var{alist} contains a
2300 @code{pop-up-frame-parameters} entry, the associated value
2301 is added to the newly created frame's parameters.
2302 @end defun
2303
2304 @defun display-buffer-use-some-frame buffer alist
2305 This function tries to ``display'' @var{buffer} by trying to find a
2306 frame that meets a predicate (by default any frame other than the
2307 current frame).
2308
2309 If this function chooses a window on another frame, it makes that frame
2310 visible and, unless @var{alist} contains an @code{inhibit-switch-frame}
2311 entry (@pxref{Choosing Window Options}), raises that frame if necessary.
2312
2313 If @var{alist} has a non-nil @code{frame-predicate} entry, its value is a
2314 function taking one argument (a frame), returning non-nil if the
2315 frame is a candidate; this function replaces the default predicate.
2316
2317 If @var{alist} has a non-@code{nil} @code{inhibit-same-window} entry,
2318 the selected window is used; thus if the selected frame has a single
2319 window, it is not used.
2320
2321 @end defun
2322
2323 @defun display-buffer-pop-up-window buffer alist
2324 This function tries to display @var{buffer} by splitting the largest
2325 or least recently-used window (typically one on the selected frame).
2326 It actually performs the split by calling the function specified in
2327 @code{split-window-preferred-function} (@pxref{Choosing Window
2328 Options}).
2329
2330 The size of the new window can be adjusted by supplying
2331 @code{window-height} and @code{window-width} entries in @var{alist}. To
2332 adjust the window's height, use an entry whose @sc{car} is
2333 @code{window-height} and whose @sc{cdr} is one of:
2334
2335 @itemize @bullet
2336 @item
2337 @code{nil} means to leave the height of the new window alone.
2338
2339 @item
2340 A number specifies the desired height of the new window. An integer
2341 specifies the number of lines of the window. A floating-point
2342 number gives the fraction of the window's height with respect to the
2343 height of the frame's root window.
2344
2345 @item
2346 If the @sc{cdr} specifies a function, that function is called with one
2347 argument: the new window. The function is supposed to adjust the
2348 height of the window; its return value is ignored. Suitable functions
2349 are @code{shrink-window-if-larger-than-buffer} and
2350 @code{fit-window-to-buffer}, see @ref{Resizing Windows}.
2351 @end itemize
2352
2353 To adjust the window's width, use an entry whose @sc{car} is
2354 @code{window-width} and whose @sc{cdr} is one of:
2355
2356 @itemize @bullet
2357 @item
2358 @code{nil} means to leave the width of the new window alone.
2359
2360 @item
2361 A number specifies the desired width of the new window. An integer
2362 specifies the number of columns of the window. A floating-point
2363 number gives the fraction of the window's width with respect to the
2364 width of the frame's root window.
2365
2366 @item
2367 If the @sc{cdr} specifies a function, that function is called with one
2368 argument: the new window. The function is supposed to adjust the width
2369 of the window; its return value is ignored.
2370 @end itemize
2371
2372 If @var{alist} contains a @code{preserve-size} entry, Emacs will try to
2373 preserve the size of the new window during future resize operations
2374 (@pxref{Preserving Window Sizes}). The @sc{cdr} of that entry must be a
2375 cons cell whose @sc{car}, if non-@code{nil}, means to preserve the width
2376 of the window and whose @sc{cdr}, if non-@code{nil}, means to preserve
2377 the height of the window.
2378
2379 This function can fail if no window splitting can be performed for some
2380 reason (e.g., if the selected frame has an @code{unsplittable} frame
2381 parameter; @pxref{Buffer Parameters}).
2382 @end defun
2383
2384 @defun display-buffer-below-selected buffer alist
2385 This function tries to display @var{buffer} in a window below the
2386 selected window. This means to either split the selected window or use
2387 the window below the selected one. If it does create a new window, it
2388 will also adjust its size provided @var{alist} contains a suitable
2389 @code{window-height} or @code{window-width} entry, see above.
2390 @end defun
2391
2392 @defun display-buffer-in-previous-window buffer alist
2393 This function tries to display @var{buffer} in a window previously
2394 showing it. If @var{alist} has a non-@code{nil}
2395 @code{inhibit-same-window} entry, the selected window is not eligible
2396 for reuse. If @var{alist} contains a @code{reusable-frames} entry, its
2397 value determines which frames to search for a suitable window as with
2398 @code{display-buffer-reuse-window}.
2399
2400 If @var{alist} has a @code{previous-window} entry, the window
2401 specified by that entry will override any other window found by the
2402 methods above, even if that window never showed @var{buffer} before.
2403 @end defun
2404
2405 @defun display-buffer-at-bottom buffer alist
2406 This function tries to display @var{buffer} in a window at the bottom
2407 of the selected frame.
2408
2409 This either splits the window at the bottom of the frame or the
2410 frame's root window, or reuses an existing window at the bottom of the
2411 selected frame.
2412 @end defun
2413
2414 @defun display-buffer-use-some-window buffer alist
2415 This function tries to display @var{buffer} by choosing an existing
2416 window and displaying the buffer in that window. It can fail if all
2417 windows are dedicated to another buffer (@pxref{Dedicated Windows}).
2418 @end defun
2419
2420 @defun display-buffer-no-window buffer alist
2421 If @var{alist} has a non-@code{nil} @code{allow-no-window} entry, then
2422 this function does not display @code{buffer}. This allows to override
2423 the default action and avoid displaying the buffer. It is assumed that
2424 when the caller specifies a non-@code{nil} @code{allow-no-window} value
2425 it can handle a @code{nil} value returned from @code{display-buffer} in
2426 this case.
2427 @end defun
2428
2429 To illustrate the use of action functions, consider the following
2430 example.
2431
2432 @example
2433 @group
2434 (display-buffer
2435 (get-buffer-create "*foo*")
2436 '((display-buffer-reuse-window
2437 display-buffer-pop-up-window
2438 display-buffer-pop-up-frame)
2439 (reusable-frames . 0)
2440 (window-height . 10) (window-width . 40)))
2441 @end group
2442 @end example
2443
2444 @noindent
2445 Evaluating the form above will cause @code{display-buffer} to proceed as
2446 follows: If a buffer called *foo* already appears on a visible or
2447 iconified frame, it will reuse its window. Otherwise, it will try to
2448 pop up a new window or, if that is impossible, a new frame and show the
2449 buffer there. If all these steps fail, it will proceed using whatever
2450 @code{display-buffer-base-action} and
2451 @code{display-buffer-fallback-action} prescribe.
2452
2453 Furthermore, @code{display-buffer} will try to adjust a reused window
2454 (provided *foo* was put by @code{display-buffer} there before) or a
2455 popped-up window as follows: If the window is part of a vertical
2456 combination, it will set its height to ten lines. Note that if, instead
2457 of the number ``10'', we specified the function
2458 @code{fit-window-to-buffer}, @code{display-buffer} would come up with a
2459 one-line window to fit the empty buffer. If the window is part of a
2460 horizontal combination, it sets its width to 40 columns. Whether a new
2461 window is vertically or horizontally combined depends on the shape of
2462 the window split and the values of
2463 @code{split-window-preferred-function}, @code{split-height-threshold}
2464 and @code{split-width-threshold} (@pxref{Choosing Window Options}).
2465
2466 Now suppose we combine this call with a preexisting setup for
2467 @code{display-buffer-alist} as follows.
2468
2469 @example
2470 @group
2471 (let ((display-buffer-alist
2472 (cons
2473 '("\\*foo\\*"
2474 (display-buffer-reuse-window display-buffer-below-selected)
2475 (reusable-frames)
2476 (window-height . 5))
2477 display-buffer-alist)))
2478 (display-buffer
2479 (get-buffer-create "*foo*")
2480 '((display-buffer-reuse-window
2481 display-buffer-pop-up-window
2482 display-buffer-pop-up-frame)
2483 (reusable-frames . 0)
2484 (window-height . 10) (window-width . 40))))
2485 @end group
2486 @end example
2487
2488 @noindent
2489 This form will have @code{display-buffer} first try reusing a window
2490 that shows *foo* on the selected frame. If there's no such window, it
2491 will try to split the selected window or, if that is impossible, use the
2492 window below the selected window.
2493
2494 If there's no window below the selected one, or the window below the
2495 selected one is dedicated to its buffer, @code{display-buffer} will
2496 proceed as described in the previous example. Note, however, that when
2497 it tries to adjust the height of any reused or popped-up window, it will
2498 in any case try to set its number of lines to ``5'' since that value
2499 overrides the corresponding specification in the @var{action} argument
2500 of @code{display-buffer}.
2501
2502
2503 @node Choosing Window Options
2504 @section Additional Options for Displaying Buffers
2505
2506 The behavior of the standard display actions of @code{display-buffer}
2507 (@pxref{Choosing Window}) can be modified by a variety of user
2508 options.
2509
2510 @defopt pop-up-windows
2511 If the value of this variable is non-@code{nil}, @code{display-buffer}
2512 is allowed to split an existing window to make a new window for
2513 displaying in. This is the default.
2514
2515 This variable is provided mainly for backward compatibility. It is
2516 obeyed by @code{display-buffer} via a special mechanism in
2517 @code{display-buffer-fallback-action}, which only calls the action
2518 function @code{display-buffer-pop-up-window} (@pxref{Display Action
2519 Functions}) when the value is @code{nil}. It is not consulted by
2520 @code{display-buffer-pop-up-window} itself, which the user may specify
2521 directly in @code{display-buffer-alist} etc.
2522 @end defopt
2523
2524 @defopt split-window-preferred-function
2525 This variable specifies a function for splitting a window, in order to
2526 make a new window for displaying a buffer. It is used by the
2527 @code{display-buffer-pop-up-window} action function to actually split
2528 the window (@pxref{Display Action Functions}).
2529
2530 The default value is @code{split-window-sensibly}, which is documented
2531 below. The value must be a function that takes one argument, a window,
2532 and return either a new window (which will be used to display the
2533 desired buffer) or @code{nil} (which means the splitting failed).
2534 @end defopt
2535
2536 @defun split-window-sensibly window
2537 This function tries to split @var{window}, and return the newly
2538 created window. If @var{window} cannot be split, it returns
2539 @code{nil}.
2540
2541 This function obeys the usual rules that determine when a window may
2542 be split (@pxref{Splitting Windows}). It first tries to split by
2543 placing the new window below, subject to the restriction imposed by
2544 @code{split-height-threshold} (see below), in addition to any other
2545 restrictions. If that fails, it tries to split by placing the new
2546 window to the right, subject to @code{split-width-threshold} (see
2547 below). If that fails, and the window is the only window on its
2548 frame, this function again tries to split and place the new window
2549 below, disregarding @code{split-height-threshold}. If this fails as
2550 well, this function gives up and returns @code{nil}.
2551 @end defun
2552
2553 @defopt split-height-threshold
2554 This variable, used by @code{split-window-sensibly}, specifies whether
2555 to split the window placing the new window below. If it is an
2556 integer, that means to split only if the original window has at least
2557 that many lines. If it is @code{nil}, that means not to split this
2558 way.
2559 @end defopt
2560
2561 @defopt split-width-threshold
2562 This variable, used by @code{split-window-sensibly}, specifies whether
2563 to split the window placing the new window to the right. If the value
2564 is an integer, that means to split only if the original window has at
2565 least that many columns. If the value is @code{nil}, that means not
2566 to split this way.
2567 @end defopt
2568
2569 @defopt even-window-sizes
2570 This variable, if non-nil, causes @code{display-buffer} to even window
2571 sizes whenever it reuses an existing window and that window is adjacent
2572 to the selected one.
2573
2574 If its value is @code{width-only}, sizes are evened only if the reused
2575 window is on the left or right of the selected one and the selected
2576 window is wider than the reused one. If its value is @code{height-only}
2577 sizes are evened only if the reused window is above or beneath the
2578 selected window and the selected window is higher than the reused one.
2579 Any other non-@code{nil} value means to even sizes in any of these cases
2580 provided the selected window is larger than the reused one in the sense
2581 of their combination.
2582 @end defopt
2583
2584 @defopt pop-up-frames
2585 If the value of this variable is non-@code{nil}, that means
2586 @code{display-buffer} may display buffers by making new frames. The
2587 default is @code{nil}.
2588
2589 A non-@code{nil} value also means that when @code{display-buffer} is
2590 looking for a window already displaying @var{buffer-or-name}, it can
2591 search any visible or iconified frame, not just the selected frame.
2592
2593 This variable is provided mainly for backward compatibility. It is
2594 obeyed by @code{display-buffer} via a special mechanism in
2595 @code{display-buffer-fallback-action}, which calls the action function
2596 @code{display-buffer-pop-up-frame} (@pxref{Display Action Functions})
2597 if the value is non-@code{nil}. (This is done before attempting to
2598 split a window.) This variable is not consulted by
2599 @code{display-buffer-pop-up-frame} itself, which the user may specify
2600 directly in @code{display-buffer-alist} etc.
2601 @end defopt
2602
2603 @defopt pop-up-frame-function
2604 This variable specifies a function for creating a new frame, in order
2605 to make a new window for displaying a buffer. It is used by the
2606 @code{display-buffer-pop-up-frame} action function (@pxref{Display
2607 Action Functions}).
2608
2609 The value should be a function that takes no arguments and returns a
2610 frame, or @code{nil} if no frame could be created. The default value
2611 is a function that creates a frame using the parameters specified by
2612 @code{pop-up-frame-alist} (see below).
2613 @end defopt
2614
2615 @defopt pop-up-frame-alist
2616 This variable holds an alist of frame parameters (@pxref{Frame
2617 Parameters}), which is used by the default function in
2618 @code{pop-up-frame-function} to make a new frame. The default is
2619 @code{nil}.
2620 @end defopt
2621
2622 @defopt same-window-buffer-names
2623 A list of buffer names for buffers that should be displayed in the
2624 selected window. If a buffer's name is in this list,
2625 @code{display-buffer} handles the buffer by showing it in the selected
2626 window.
2627 @end defopt
2628
2629 @defopt same-window-regexps
2630 A list of regular expressions that specify buffers that should be
2631 displayed in the selected window. If the buffer's name matches any of
2632 the regular expressions in this list, @code{display-buffer} handles the
2633 buffer by showing it in the selected window.
2634 @end defopt
2635
2636 @defun same-window-p buffer-name
2637 This function returns @code{t} if displaying a buffer
2638 named @var{buffer-name} with @code{display-buffer} would
2639 put it in the selected window.
2640 @end defun
2641
2642 @node Window History
2643 @section Window History
2644 @cindex window history
2645
2646 Each window remembers in a list the buffers it has previously displayed,
2647 and the order in which these buffers were removed from it. This history
2648 is used, for example, by @code{replace-buffer-in-windows}
2649 (@pxref{Buffers and Windows}). The list is automatically maintained by
2650 Emacs, but you can use the following functions to explicitly inspect or
2651 alter it:
2652
2653 @defun window-prev-buffers &optional window
2654 This function returns a list specifying the previous contents of
2655 @var{window}. The optional argument @var{window} should be a live
2656 window and defaults to the selected one.
2657
2658 Each list element has the form @code{(@var{buffer} @var{window-start}
2659 @var{window-pos})}, where @var{buffer} is a buffer previously shown in
2660 the window, @var{window-start} is the window start position
2661 (@pxref{Window Start and End}) when that buffer was last shown, and
2662 @var{window-pos} is the point position (@pxref{Window Point}) when
2663 that buffer was last shown in @var{window}.
2664
2665 The list is ordered so that earlier elements correspond to more
2666 recently-shown buffers, and the first element usually corresponds to the
2667 buffer most recently removed from the window.
2668 @end defun
2669
2670 @defun set-window-prev-buffers window prev-buffers
2671 This function sets @var{window}'s previous buffers to the value of
2672 @var{prev-buffers}. The argument @var{window} must be a live window
2673 and defaults to the selected one. The argument @var{prev-buffers}
2674 should be a list of the same form as that returned by
2675 @code{window-prev-buffers}.
2676 @end defun
2677
2678 In addition, each buffer maintains a list of @dfn{next buffers}, which
2679 is a list of buffers re-shown by @code{switch-to-prev-buffer} (see
2680 below). This list is mainly used by @code{switch-to-prev-buffer} and
2681 @code{switch-to-next-buffer} for choosing buffers to switch to.
2682
2683 @defun window-next-buffers &optional window
2684 This function returns the list of buffers recently re-shown in
2685 @var{window} via @code{switch-to-prev-buffer}. The @var{window}
2686 argument must denote a live window or @code{nil} (meaning the selected
2687 window).
2688 @end defun
2689
2690 @defun set-window-next-buffers window next-buffers
2691 This function sets the next buffer list of @var{window} to
2692 @var{next-buffers}. The @var{window} argument should be a live window
2693 or @code{nil} (meaning the selected window). The argument
2694 @var{next-buffers} should be a list of buffers.
2695 @end defun
2696
2697 The following commands can be used to cycle through the global buffer
2698 list, much like @code{bury-buffer} and @code{unbury-buffer}. However,
2699 they cycle according to the specified window's history list, rather
2700 than the global buffer list. In addition, they restore
2701 window-specific window start and point positions, and may show a
2702 buffer even if it is already shown in another window. The
2703 @code{switch-to-prev-buffer} command, in particular, is used by
2704 @code{replace-buffer-in-windows}, @code{bury-buffer} and
2705 @code{quit-window} to find a replacement buffer for a window.
2706
2707 @deffn Command switch-to-prev-buffer &optional window bury-or-kill
2708 This command displays the previous buffer in @var{window}. The
2709 argument @var{window} should be a live window or @code{nil} (meaning
2710 the selected window). If the optional argument @var{bury-or-kill} is
2711 non-@code{nil}, this means that the buffer currently shown in
2712 @var{window} is about to be buried or killed and consequently should
2713 not be switched to in future invocations of this command.
2714
2715 The previous buffer is usually the buffer shown before the buffer
2716 currently shown in @var{window}. However, a buffer that has been buried
2717 or killed, or has been already shown by a recent invocation of
2718 @code{switch-to-prev-buffer}, does not qualify as previous buffer.
2719
2720 If repeated invocations of this command have already shown all buffers
2721 previously shown in @var{window}, further invocations will show buffers
2722 from the buffer list of the frame @var{window} appears on (@pxref{Buffer
2723 List}), trying to skip buffers that are already shown in another window
2724 on that frame.
2725 @end deffn
2726
2727 @deffn Command switch-to-next-buffer &optional window
2728 This command switches to the next buffer in @var{window}, thus undoing
2729 the effect of the last @code{switch-to-prev-buffer} command in
2730 @var{window}. The argument @var{window} must be a live window and
2731 defaults to the selected one.
2732
2733 If there is no recent invocation of @code{switch-to-prev-buffer} that
2734 can be undone, this function tries to show a buffer from the buffer list
2735 of the frame @var{window} appears on (@pxref{Buffer List}).
2736 @end deffn
2737
2738 By default @code{switch-to-prev-buffer} and @code{switch-to-next-buffer}
2739 can switch to a buffer that is already shown in another window on the
2740 same frame. The following option can be used to override this behavior.
2741
2742 @defopt switch-to-visible-buffer
2743 If this variable is non-@code{nil}, @code{switch-to-prev-buffer} and
2744 @code{switch-to-next-buffer} may switch to a buffer that is already
2745 visible on the same frame, provided the buffer was shown in the
2746 relevant window before. If it is @code{nil},
2747 @code{switch-to-prev-buffer} and @code{switch-to-next-buffer} always
2748 try to avoid switching to a buffer that is already visible in another
2749 window on the same frame. The default is @code{t}.
2750 @end defopt
2751
2752
2753 @node Dedicated Windows
2754 @section Dedicated Windows
2755 @cindex dedicated window
2756
2757 Functions for displaying a buffer can be told to not use specific
2758 windows by marking these windows as @dfn{dedicated} to their buffers.
2759 @code{display-buffer} (@pxref{Choosing Window}) never uses a dedicated
2760 window for displaying another buffer in it. @code{get-lru-window} and
2761 @code{get-largest-window} (@pxref{Cyclic Window Ordering}) do not
2762 consider dedicated windows as candidates when their @var{dedicated}
2763 argument is non-@code{nil}. The behavior of @code{set-window-buffer}
2764 (@pxref{Buffers and Windows}) with respect to dedicated windows is
2765 slightly different, see below.
2766
2767 Functions supposed to remove a buffer from a window or a window from
2768 a frame can behave specially when a window they operate on is dedicated.
2769 We will distinguish three basic cases, namely where (1) the window is
2770 not the only window on its frame, (2) the window is the only window on
2771 its frame but there are other frames on the same terminal left, and (3)
2772 the window is the only window on the only frame on the same terminal.
2773
2774 In particular, @code{delete-windows-on} (@pxref{Deleting Windows})
2775 handles case (2) by deleting the associated frame and case (3) by
2776 showing another buffer in that frame's only window. The function
2777 @code{replace-buffer-in-windows} (@pxref{Buffers and Windows}) which is
2778 called when a buffer gets killed, deletes the window in case (1) and
2779 behaves like @code{delete-windows-on} otherwise.
2780 @c FIXME: Does replace-buffer-in-windows _delete_ a window in case (1)?
2781
2782 When @code{bury-buffer} (@pxref{Buffer List}) operates on the
2783 selected window (which shows the buffer that shall be buried), it
2784 handles case (2) by calling @code{frame-auto-hide-function}
2785 (@pxref{Quitting Windows}) to deal with the selected frame. The other
2786 two cases are handled as with @code{replace-buffer-in-windows}.
2787
2788 @defun window-dedicated-p &optional window
2789 This function returns non-@code{nil} if @var{window} is dedicated to its
2790 buffer and @code{nil} otherwise. More precisely, the return value is
2791 the value assigned by the last call of @code{set-window-dedicated-p} for
2792 @var{window}, or @code{nil} if that function was never called with
2793 @var{window} as its argument. The default for @var{window} is the
2794 selected window.
2795 @end defun
2796
2797 @defun set-window-dedicated-p window flag
2798 This function marks @var{window} as dedicated to its buffer if
2799 @var{flag} is non-@code{nil}, and non-dedicated otherwise.
2800
2801 As a special case, if @var{flag} is @code{t}, @var{window} becomes
2802 @dfn{strongly} dedicated to its buffer. @code{set-window-buffer}
2803 signals an error when the window it acts upon is strongly dedicated to
2804 its buffer and does not already display the buffer it is asked to
2805 display. Other functions do not treat @code{t} differently from any
2806 non-@code{nil} value.
2807 @end defun
2808
2809
2810 @node Quitting Windows
2811 @section Quitting Windows
2812
2813 When you want to get rid of a window used for displaying a buffer, you
2814 can call @code{delete-window} or @code{delete-windows-on}
2815 (@pxref{Deleting Windows}) to remove that window from its frame. If the
2816 buffer is shown on a separate frame, you might want to call
2817 @code{delete-frame} (@pxref{Deleting Frames}) instead. If, on the other
2818 hand, a window has been reused for displaying the buffer, you might
2819 prefer showing the buffer previously shown in that window, by calling the
2820 function @code{switch-to-prev-buffer} (@pxref{Window History}).
2821 Finally, you might want to either bury (@pxref{Buffer List}) or kill
2822 (@pxref{Killing Buffers}) the window's buffer.
2823
2824 The following command uses information on how the window for
2825 displaying the buffer was obtained in the first place, thus attempting
2826 to automate the above decisions for you.
2827
2828 @deffn Command quit-window &optional kill window
2829 This command quits @var{window} and buries its buffer. The argument
2830 @var{window} must be a live window and defaults to the selected one.
2831 With prefix argument @var{kill} non-@code{nil}, it kills the buffer
2832 instead of burying it. It calls the function @code{quit-restore-window}
2833 described next to deal with the window and its buffer.
2834 @end deffn
2835
2836 @defun quit-restore-window &optional window bury-or-kill
2837 This function tries to restore the state of @var{window} that existed
2838 before its buffer was displayed in it. The optional argument
2839 @var{window} must be a live window and defaults to the selected one.
2840
2841 If @var{window} was created specially for displaying its buffer, this
2842 function deletes @var{window} provided its frame contains at least one
2843 other live window. If @var{window} is the only window on its frame and
2844 there are other frames on the frame's terminal, the value of the
2845 optional argument @var{bury-or-kill} determines how to proceed with the
2846 window. If @var{bury-or-kill} equals @code{kill}, the frame is deleted
2847 unconditionally. Otherwise, the fate of the frame is determined by
2848 calling @code{frame-auto-hide-function} (see below) with that frame as
2849 sole argument.
2850
2851 Otherwise, this function tries to redisplay the buffer previously shown
2852 in @var{window}. It also tries to restore the window start
2853 (@pxref{Window Start and End}) and point (@pxref{Window Point})
2854 positions of the previously shown buffer. If, in addition,
2855 @var{window}'s buffer was temporarily resized, this function will also
2856 try to restore the original height of @var{window}.
2857
2858 The cases described so far require that the buffer shown in @var{window}
2859 is still the buffer displayed by the last buffer display function for
2860 this window. If another buffer has been shown in the meantime, or the
2861 buffer previously shown no longer exists, this function calls
2862 @code{switch-to-prev-buffer} (@pxref{Window History}) to show some other
2863 buffer instead.
2864
2865 The optional argument @var{bury-or-kill} specifies how to deal with
2866 @var{window}'s buffer. The following values are handled:
2867
2868 @table @code
2869 @item nil
2870 This means to not deal with the buffer in any particular way. As a
2871 consequence, if @var{window} is not deleted, invoking
2872 @code{switch-to-prev-buffer} will usually show the buffer again.
2873
2874 @item append
2875 This means that if @var{window} is not deleted, its buffer is moved to
2876 the end of @var{window}'s list of previous buffers, so it's less likely
2877 that a future invocation of @code{switch-to-prev-buffer} will switch to
2878 it. Also, it moves the buffer to the end of the frame's buffer list.
2879
2880 @item bury
2881 This means that if @var{window} is not deleted, its buffer is removed
2882 from @var{window}'s list of previous buffers. Also, it moves the buffer
2883 to the end of the frame's buffer list. This value provides the most
2884 reliable remedy to not have @code{switch-to-prev-buffer} switch to this
2885 buffer again without killing the buffer.
2886
2887 @item kill
2888 This means to kill @var{window}'s buffer.
2889 @end table
2890
2891 @code{quit-restore-window} bases its decisions on information stored in
2892 @var{window}'s @code{quit-restore} window parameter (@pxref{Window
2893 Parameters}), and resets that parameter to @code{nil} after it's done.
2894 @end defun
2895
2896 The following option specifies how to deal with a frame containing just
2897 one window that should be either quit, or whose buffer should be buried.
2898
2899 @defopt frame-auto-hide-function
2900 The function specified by this option is called to automatically hide
2901 frames. This function is called with one argument---a frame.
2902
2903 The function specified here is called by @code{bury-buffer}
2904 (@pxref{Buffer List}) when the selected window is dedicated and shows
2905 the buffer to bury. It is also called by @code{quit-restore-window}
2906 (see above) when the frame of the window to quit has been specially
2907 created for displaying that window's buffer and the buffer is not
2908 killed.
2909
2910 The default is to call @code{iconify-frame} (@pxref{Visibility of
2911 Frames}). Alternatively, you may specify either @code{delete-frame}
2912 (@pxref{Deleting Frames}) to remove the frame from its display,
2913 @code{ignore} to leave the frame unchanged, or any other function that
2914 can take a frame as its sole argument.
2915
2916 Note that the function specified by this option is called only if the
2917 specified frame contains just one live window and there is at least one
2918 other frame on the same terminal.
2919 @end defopt
2920
2921
2922 @node Window Point
2923 @section Windows and Point
2924 @cindex window position
2925 @cindex window point
2926 @cindex position in window
2927 @cindex point in window
2928
2929 Each window has its own value of point (@pxref{Point}), independent of
2930 the value of point in other windows displaying the same buffer. This
2931 makes it useful to have multiple windows showing one buffer.
2932
2933 @itemize @bullet
2934 @item
2935 The window point is established when a window is first created; it is
2936 initialized from the buffer's point, or from the window point of another
2937 window opened on the buffer if such a window exists.
2938
2939 @item
2940 Selecting a window sets the value of point in its buffer from the
2941 window's value of point. Conversely, deselecting a window sets the
2942 window's value of point from that of the buffer. Thus, when you switch
2943 between windows that display a given buffer, the point value for the
2944 selected window is in effect in the buffer, while the point values for
2945 the other windows are stored in those windows.
2946
2947 @item
2948 As long as the selected window displays the current buffer, the window's
2949 point and the buffer's point always move together; they remain equal.
2950 @end itemize
2951
2952 @cindex cursor
2953 As far as the user is concerned, point is where the cursor is, and
2954 when the user switches to another buffer, the cursor jumps to the
2955 position of point in that buffer.
2956
2957 @defun window-point &optional window
2958 This function returns the current position of point in @var{window}.
2959 For a nonselected window, this is the value point would have (in that
2960 window's buffer) if that window were selected. The default for
2961 @var{window} is the selected window.
2962
2963 When @var{window} is the selected window, the value returned is the
2964 value of point in that window's buffer. Strictly speaking, it would be
2965 more correct to return the ``top-level'' value of point, outside of any
2966 @code{save-excursion} forms. But that value is hard to find.
2967 @end defun
2968
2969 @defun set-window-point window position
2970 This function positions point in @var{window} at position
2971 @var{position} in @var{window}'s buffer. It returns @var{position}.
2972
2973 If @var{window} is selected, this simply does @code{goto-char} in
2974 @var{window}'s buffer.
2975 @end defun
2976
2977 @defvar window-point-insertion-type
2978 This variable specifies the marker insertion type (@pxref{Marker
2979 Insertion Types}) of @code{window-point}. The default is @code{nil},
2980 so @code{window-point} will stay behind text inserted there.
2981 @end defvar
2982
2983 @node Window Start and End
2984 @section The Window Start and End Positions
2985 @cindex window start position
2986 @cindex display-start position
2987
2988 Each window maintains a marker used to keep track of a buffer position
2989 that specifies where in the buffer display should start. This position
2990 is called the @dfn{display-start} position of the window (or just the
2991 @dfn{start}). The character after this position is the one that appears
2992 at the upper left corner of the window. It is usually, but not
2993 inevitably, at the beginning of a text line.
2994
2995 After switching windows or buffers, and in some other cases, if the
2996 window start is in the middle of a line, Emacs adjusts the window
2997 start to the start of a line. This prevents certain operations from
2998 leaving the window start at a meaningless point within a line. This
2999 feature may interfere with testing some Lisp code by executing it
3000 using the commands of Lisp mode, because they trigger this
3001 readjustment. To test such code, put it into a command and bind the
3002 command to a key.
3003
3004 @defun window-start &optional window
3005 @cindex window top line
3006 This function returns the display-start position of window
3007 @var{window}. If @var{window} is @code{nil}, the selected window is
3008 used.
3009
3010 When you create a window, or display a different buffer in it, the
3011 display-start position is set to a display-start position recently used
3012 for the same buffer, or to @code{point-min} if the buffer doesn't have
3013 any.
3014
3015 Redisplay updates the window-start position (if you have not specified
3016 it explicitly since the previous redisplay)---to make sure point appears
3017 on the screen. Nothing except redisplay automatically changes the
3018 window-start position; if you move point, do not expect the window-start
3019 position to change in response until after the next redisplay.
3020 @end defun
3021
3022 @cindex window end position
3023 @defun window-end &optional window update
3024 This function returns the position where display of its buffer ends in
3025 @var{window}. The default for @var{window} is the selected window.
3026
3027 Simply changing the buffer text or moving point does not update the
3028 value that @code{window-end} returns. The value is updated only when
3029 Emacs redisplays and redisplay completes without being preempted.
3030
3031 If the last redisplay of @var{window} was preempted, and did not finish,
3032 Emacs does not know the position of the end of display in that window.
3033 In that case, this function returns @code{nil}.
3034
3035 If @var{update} is non-@code{nil}, @code{window-end} always returns an
3036 up-to-date value for where display ends, based on the current
3037 @code{window-start} value. If a previously saved value of that position
3038 is still valid, @code{window-end} returns that value; otherwise it
3039 computes the correct value by scanning the buffer text.
3040
3041 Even if @var{update} is non-@code{nil}, @code{window-end} does not
3042 attempt to scroll the display if point has moved off the screen, the
3043 way real redisplay would do. It does not alter the
3044 @code{window-start} value. In effect, it reports where the displayed
3045 text will end if scrolling is not required.
3046 @end defun
3047
3048 @defun set-window-start window position &optional noforce
3049 This function sets the display-start position of @var{window} to
3050 @var{position} in @var{window}'s buffer. It returns @var{position}.
3051
3052 The display routines insist that the position of point be visible when a
3053 buffer is displayed. Normally, they change the display-start position
3054 (that is, scroll the window) whenever necessary to make point visible.
3055 However, if you specify the start position with this function using
3056 @code{nil} for @var{noforce}, it means you want display to start at
3057 @var{position} even if that would put the location of point off the
3058 screen. If this does place point off screen, the display routines move
3059 point to the left margin on the middle line in the window.
3060
3061 For example, if point @w{is 1} and you set the start of the window
3062 @w{to 37}, the start of the next line, point will be ``above'' the top
3063 of the window. The display routines will automatically move point if
3064 it is still 1 when redisplay occurs. Here is an example:
3065
3066 @example
3067 @group
3068 ;; @r{Here is what @samp{foo} looks like before executing}
3069 ;; @r{the @code{set-window-start} expression.}
3070 @end group
3071
3072 @group
3073 ---------- Buffer: foo ----------
3074 @point{}This is the contents of buffer foo.
3075 2
3076 3
3077 4
3078 5
3079 6
3080 ---------- Buffer: foo ----------
3081 @end group
3082
3083 @group
3084 (set-window-start
3085 (selected-window)
3086 (save-excursion
3087 (goto-char 1)
3088 (forward-line 1)
3089 (point)))
3090 @result{} 37
3091 @end group
3092
3093 @group
3094 ;; @r{Here is what @samp{foo} looks like after executing}
3095 ;; @r{the @code{set-window-start} expression.}
3096 ---------- Buffer: foo ----------
3097 2
3098 3
3099 @point{}4
3100 5
3101 6
3102 ---------- Buffer: foo ----------
3103 @end group
3104 @end example
3105
3106 If @var{noforce} is non-@code{nil}, and @var{position} would place point
3107 off screen at the next redisplay, then redisplay computes a new window-start
3108 position that works well with point, and thus @var{position} is not used.
3109 @end defun
3110
3111 @defun pos-visible-in-window-p &optional position window partially
3112 This function returns non-@code{nil} if @var{position} is within the
3113 range of text currently visible on the screen in @var{window}. It
3114 returns @code{nil} if @var{position} is scrolled vertically out of view.
3115 Locations that are partially obscured are not considered visible unless
3116 @var{partially} is non-@code{nil}. The argument @var{position} defaults
3117 to the current position of point in @var{window}; @var{window}, to the
3118 selected window. If @var{position} is @code{t}, that means to check the
3119 last visible position in @var{window}.
3120
3121 This function considers only vertical scrolling. If @var{position} is
3122 out of view only because @var{window} has been scrolled horizontally,
3123 @code{pos-visible-in-window-p} returns non-@code{nil} anyway.
3124 @xref{Horizontal Scrolling}.
3125
3126 If @var{position} is visible, @code{pos-visible-in-window-p} returns
3127 @code{t} if @var{partially} is @code{nil}; if @var{partially} is
3128 non-@code{nil}, and the character following @var{position} is fully
3129 visible, it returns a list of the form @code{(@var{x} @var{y})}, where
3130 @var{x} and @var{y} are the pixel coordinates relative to the top left
3131 corner of the window; otherwise it returns an extended list of the form
3132 @code{(@var{x} @var{y} @var{rtop} @var{rbot} @var{rowh} @var{vpos})},
3133 where @var{rtop} and @var{rbot} specify the number of off-window pixels
3134 at the top and bottom of the row at @var{position}, @var{rowh} specifies
3135 the visible height of that row, and @var{vpos} specifies the vertical
3136 position (zero-based row number) of that row.
3137
3138 Here is an example:
3139
3140 @example
3141 @group
3142 ;; @r{If point is off the screen now, recenter it now.}
3143 (or (pos-visible-in-window-p
3144 (point) (selected-window))
3145 (recenter 0))
3146 @end group
3147 @end example
3148 @end defun
3149
3150 @defun window-line-height &optional line window
3151 This function returns the height of text line @var{line} in
3152 @var{window}. If @var{line} is one of @code{header-line} or
3153 @code{mode-line}, @code{window-line-height} returns information about
3154 the corresponding line of the window. Otherwise, @var{line} is a text
3155 line number starting from 0. A negative number counts from the end of
3156 the window. The default for @var{line} is the current line in
3157 @var{window}; the default for @var{window} is the selected window.
3158
3159 If the display is not up to date, @code{window-line-height} returns
3160 @code{nil}. In that case, @code{pos-visible-in-window-p} may be used
3161 to obtain related information.
3162
3163 If there is no line corresponding to the specified @var{line},
3164 @code{window-line-height} returns @code{nil}. Otherwise, it returns
3165 a list @code{(@var{height} @var{vpos} @var{ypos} @var{offbot})},
3166 where @var{height} is the height in pixels of the visible part of the
3167 line, @var{vpos} and @var{ypos} are the vertical position in lines and
3168 pixels of the line relative to the top of the first text line, and
3169 @var{offbot} is the number of off-window pixels at the bottom of the
3170 text line. If there are off-window pixels at the top of the (first)
3171 text line, @var{ypos} is negative.
3172 @end defun
3173
3174 @node Textual Scrolling
3175 @section Textual Scrolling
3176 @cindex textual scrolling
3177 @cindex scrolling textually
3178
3179 @dfn{Textual scrolling} means moving the text up or down through a
3180 window. It works by changing the window's display-start location. It
3181 may also change the value of @code{window-point} to keep point on the
3182 screen (@pxref{Window Point}).
3183
3184 The basic textual scrolling functions are @code{scroll-up} (which
3185 scrolls forward) and @code{scroll-down} (which scrolls backward). In
3186 these function names, ``up'' and ``down'' refer to the direction of
3187 motion of the buffer text relative to the window. Imagine that the
3188 text is written on a long roll of paper and that the scrolling
3189 commands move the paper up and down. Thus, if you are looking at the
3190 middle of a buffer and repeatedly call @code{scroll-down}, you will
3191 eventually see the beginning of the buffer.
3192
3193 Unfortunately, this sometimes causes confusion, because some people
3194 tend to think in terms of the opposite convention: they
3195 imagine the window moving over text that remains in place, so that
3196 ``down'' commands take you to the end of the buffer. This convention
3197 is consistent with fact that such a command is bound to a key named
3198 @key{PageDown} on modern keyboards.
3199 @ignore
3200 We have not switched to this convention as that is likely to break
3201 existing Emacs Lisp code.
3202 @end ignore
3203
3204 Textual scrolling functions (aside from @code{scroll-other-window})
3205 have unpredictable results if the current buffer is not the one
3206 displayed in the selected window. @xref{Current Buffer}.
3207
3208 If the window contains a row taller than the height of the window
3209 (for example in the presence of a large image), the scroll functions
3210 will adjust the window's vertical scroll position to scroll the
3211 partially visible row. Lisp callers can disable this feature by
3212 binding the variable @code{auto-window-vscroll} to @code{nil}
3213 (@pxref{Vertical Scrolling}).
3214
3215 @deffn Command scroll-up &optional count
3216 This function scrolls forward by @var{count} lines in the selected
3217 window.
3218
3219 If @var{count} is negative, it scrolls backward instead. If
3220 @var{count} is @code{nil} (or omitted), the distance scrolled is
3221 @code{next-screen-context-lines} lines less than the height of the
3222 window's text area.
3223
3224 If the selected window cannot be scrolled any further, this function
3225 signals an error. Otherwise, it returns @code{nil}.
3226 @end deffn
3227
3228 @deffn Command scroll-down &optional count
3229 This function scrolls backward by @var{count} lines in the selected
3230 window.
3231
3232 If @var{count} is negative, it scrolls forward instead. In other
3233 respects, it behaves the same way as @code{scroll-up} does.
3234 @end deffn
3235
3236 @deffn Command scroll-up-command &optional count
3237 This behaves like @code{scroll-up}, except that if the selected window
3238 cannot be scrolled any further and the value of the variable
3239 @code{scroll-error-top-bottom} is @code{t}, it tries to move to the
3240 end of the buffer instead. If point is already there, it signals an
3241 error.
3242 @end deffn
3243
3244 @deffn Command scroll-down-command &optional count
3245 This behaves like @code{scroll-down}, except that if the selected
3246 window cannot be scrolled any further and the value of the variable
3247 @code{scroll-error-top-bottom} is @code{t}, it tries to move to the
3248 beginning of the buffer instead. If point is already there, it
3249 signals an error.
3250 @end deffn
3251
3252 @deffn Command scroll-other-window &optional count
3253 This function scrolls the text in another window upward @var{count}
3254 lines. Negative values of @var{count}, or @code{nil}, are handled
3255 as in @code{scroll-up}.
3256
3257 You can specify which buffer to scroll by setting the variable
3258 @code{other-window-scroll-buffer} to a buffer. If that buffer isn't
3259 already displayed, @code{scroll-other-window} displays it in some
3260 window.
3261
3262 When the selected window is the minibuffer, the next window is normally
3263 the leftmost one immediately above it. You can specify a different
3264 window to scroll, when the minibuffer is selected, by setting the variable
3265 @code{minibuffer-scroll-window}. This variable has no effect when any
3266 other window is selected. When it is non-@code{nil} and the
3267 minibuffer is selected, it takes precedence over
3268 @code{other-window-scroll-buffer}. @xref{Definition of
3269 minibuffer-scroll-window}.
3270
3271 When the minibuffer is active, it is the next window if the selected
3272 window is the one at the bottom right corner. In this case,
3273 @code{scroll-other-window} attempts to scroll the minibuffer. If the
3274 minibuffer contains just one line, it has nowhere to scroll to, so the
3275 line reappears after the echo area momentarily displays the message
3276 @samp{End of buffer}.
3277 @end deffn
3278
3279 @defvar other-window-scroll-buffer
3280 If this variable is non-@code{nil}, it tells @code{scroll-other-window}
3281 which buffer's window to scroll.
3282 @end defvar
3283
3284 @defopt scroll-margin
3285 This option specifies the size of the scroll margin---a minimum number
3286 of lines between point and the top or bottom of a window. Whenever
3287 point gets within this many lines of the top or bottom of the window,
3288 redisplay scrolls the text automatically (if possible) to move point
3289 out of the margin, closer to the center of the window.
3290 @end defopt
3291
3292 @defopt scroll-conservatively
3293 This variable controls how scrolling is done automatically when point
3294 moves off the screen (or into the scroll margin). If the value is a
3295 positive integer @var{n}, then redisplay scrolls the text up to
3296 @var{n} lines in either direction, if that will bring point back into
3297 proper view. This behavior is called @dfn{conservative scrolling}.
3298 Otherwise, scrolling happens in the usual way, under the control of
3299 other variables such as @code{scroll-up-aggressively} and
3300 @code{scroll-down-aggressively}.
3301
3302 The default value is zero, which means that conservative scrolling
3303 never happens.
3304 @end defopt
3305
3306 @defopt scroll-down-aggressively
3307 The value of this variable should be either @code{nil} or a fraction
3308 @var{f} between 0 and 1. If it is a fraction, that specifies where on
3309 the screen to put point when scrolling down. More precisely, when a
3310 window scrolls down because point is above the window start, the new
3311 start position is chosen to put point @var{f} part of the window
3312 height from the top. The larger @var{f}, the more aggressive the
3313 scrolling.
3314
3315 A value of @code{nil} is equivalent to .5, since its effect is to center
3316 point. This variable automatically becomes buffer-local when set in any
3317 fashion.
3318 @end defopt
3319
3320 @defopt scroll-up-aggressively
3321 Likewise, for scrolling up. The value, @var{f}, specifies how far
3322 point should be placed from the bottom of the window; thus, as with
3323 @code{scroll-up-aggressively}, a larger value scrolls more aggressively.
3324 @end defopt
3325
3326 @defopt scroll-step
3327 This variable is an older variant of @code{scroll-conservatively}.
3328 The difference is that if its value is @var{n}, that permits scrolling
3329 only by precisely @var{n} lines, not a smaller number. This feature
3330 does not work with @code{scroll-margin}. The default value is zero.
3331 @end defopt
3332
3333 @cindex @code{scroll-command} property
3334 @defopt scroll-preserve-screen-position
3335 If this option is @code{t}, whenever a scrolling command moves point
3336 off-window, Emacs tries to adjust point to keep the cursor at its old
3337 vertical position in the window, rather than the window edge.
3338
3339 If the value is non-@code{nil} and not @code{t}, Emacs adjusts point
3340 to keep the cursor at the same vertical position, even if the
3341 scrolling command didn't move point off-window.
3342
3343 This option affects all scroll commands that have a non-@code{nil}
3344 @code{scroll-command} symbol property.
3345 @end defopt
3346
3347 @defopt next-screen-context-lines
3348 The value of this variable is the number of lines of continuity to
3349 retain when scrolling by full screens. For example, @code{scroll-up}
3350 with an argument of @code{nil} scrolls so that this many lines at the
3351 bottom of the window appear instead at the top. The default value is
3352 @code{2}.
3353 @end defopt
3354
3355 @defopt scroll-error-top-bottom
3356 If this option is @code{nil} (the default), @code{scroll-up-command}
3357 and @code{scroll-down-command} simply signal an error when no more
3358 scrolling is possible.
3359
3360 If the value is @code{t}, these commands instead move point to the
3361 beginning or end of the buffer (depending on scrolling direction);
3362 only if point is already on that position do they signal an error.
3363 @end defopt
3364
3365 @deffn Command recenter &optional count
3366 @cindex centering point
3367 This function scrolls the text in the selected window so that point is
3368 displayed at a specified vertical position within the window. It does
3369 not ``move point'' with respect to the text.
3370
3371 If @var{count} is a non-negative number, that puts the line containing
3372 point @var{count} lines down from the top of the window. If
3373 @var{count} is a negative number, then it counts upward from the
3374 bottom of the window, so that @minus{}1 stands for the last usable
3375 line in the window.
3376
3377 If @var{count} is @code{nil} (or a non-@code{nil} list),
3378 @code{recenter} puts the line containing point in the middle of the
3379 window. If @var{count} is @code{nil}, this function may redraw the
3380 frame, according to the value of @code{recenter-redisplay}.
3381
3382 When @code{recenter} is called interactively, @var{count} is the raw
3383 prefix argument. Thus, typing @kbd{C-u} as the prefix sets the
3384 @var{count} to a non-@code{nil} list, while typing @kbd{C-u 4} sets
3385 @var{count} to 4, which positions the current line four lines from the
3386 top.
3387
3388 With an argument of zero, @code{recenter} positions the current line at
3389 the top of the window. The command @code{recenter-top-bottom} offers
3390 a more convenient way to achieve this.
3391 @end deffn
3392
3393 @defopt recenter-redisplay
3394 If this variable is non-@code{nil}, calling @code{recenter} with a
3395 @code{nil} argument redraws the frame. The default value is
3396 @code{tty}, which means only redraw the frame if it is a tty frame.
3397 @end defopt
3398
3399 @deffn Command recenter-top-bottom &optional count
3400 This command, which is the default binding for @kbd{C-l}, acts like
3401 @code{recenter}, except if called with no argument. In that case,
3402 successive calls place point according to the cycling order defined
3403 by the variable @code{recenter-positions}.
3404 @end deffn
3405
3406 @defopt recenter-positions
3407 This variable controls how @code{recenter-top-bottom} behaves when
3408 called with no argument. The default value is @code{(middle top
3409 bottom)}, which means that successive calls of
3410 @code{recenter-top-bottom} with no argument cycle between placing
3411 point at the middle, top, and bottom of the window.
3412 @end defopt
3413
3414
3415 @node Vertical Scrolling
3416 @section Vertical Fractional Scrolling
3417 @cindex vertical fractional scrolling
3418 @cindex vertical scroll position
3419
3420 @dfn{Vertical fractional scrolling} means shifting text in a window
3421 up or down by a specified multiple or fraction of a line. Each window
3422 has a @dfn{vertical scroll position}, which is a number, never less than
3423 zero. It specifies how far to raise the contents of the window.
3424 Raising the window contents generally makes all or part of some lines
3425 disappear off the top, and all or part of some other lines appear at the
3426 bottom. The usual value is zero.
3427
3428 The vertical scroll position is measured in units of the normal line
3429 height, which is the height of the default font. Thus, if the value is
3430 .5, that means the window contents are scrolled up half the normal line
3431 height. If it is 3.3, that means the window contents are scrolled up
3432 somewhat over three times the normal line height.
3433
3434 What fraction of a line the vertical scrolling covers, or how many
3435 lines, depends on what the lines contain. A value of .5 could scroll a
3436 line whose height is very short off the screen, while a value of 3.3
3437 could scroll just part of the way through a tall line or an image.
3438
3439 @defun window-vscroll &optional window pixels-p
3440 This function returns the current vertical scroll position of
3441 @var{window}. The default for @var{window} is the selected window.
3442 If @var{pixels-p} is non-@code{nil}, the return value is measured in
3443 pixels, rather than in units of the normal line height.
3444
3445 @example
3446 @group
3447 (window-vscroll)
3448 @result{} 0
3449 @end group
3450 @end example
3451 @end defun
3452
3453 @defun set-window-vscroll window lines &optional pixels-p
3454 This function sets @var{window}'s vertical scroll position to
3455 @var{lines}. If @var{window} is @code{nil}, the selected window is
3456 used. The argument @var{lines} should be zero or positive; if not, it
3457 is taken as zero.
3458
3459
3460 The actual vertical scroll position must always correspond
3461 to an integral number of pixels, so the value you specify
3462 is rounded accordingly.
3463
3464 The return value is the result of this rounding.
3465
3466 @example
3467 @group
3468 (set-window-vscroll (selected-window) 1.2)
3469 @result{} 1.13
3470 @end group
3471 @end example
3472
3473 If @var{pixels-p} is non-@code{nil}, @var{lines} specifies a number of
3474 pixels. In this case, the return value is @var{lines}.
3475 @end defun
3476
3477 @defvar auto-window-vscroll
3478 If this variable is non-@code{nil}, the @code{line-move},
3479 @code{scroll-up}, and @code{scroll-down} functions will automatically
3480 modify the vertical scroll position to scroll through display rows
3481 that are taller than the height of the window, for example in the
3482 presence of large images.
3483 @end defvar
3484
3485 @node Horizontal Scrolling
3486 @section Horizontal Scrolling
3487 @cindex horizontal scrolling
3488
3489 @dfn{Horizontal scrolling} means shifting the image in the window left
3490 or right by a specified multiple of the normal character width. Each
3491 window has a @dfn{horizontal scroll position}, which is a number, never
3492 less than zero. It specifies how far to shift the contents left.
3493 Shifting the window contents left generally makes all or part of some
3494 characters disappear off the left, and all or part of some other
3495 characters appear at the right. The usual value is zero.
3496
3497 The horizontal scroll position is measured in units of the normal
3498 character width, which is the width of space in the default font. Thus,
3499 if the value is 5, that means the window contents are scrolled left by 5
3500 times the normal character width. How many characters actually
3501 disappear off to the left depends on their width, and could vary from
3502 line to line.
3503
3504 Because we read from side to side in the ``inner loop'', and from top
3505 to bottom in the ``outer loop'', the effect of horizontal scrolling is
3506 not like that of textual or vertical scrolling. Textual scrolling
3507 involves selection of a portion of text to display, and vertical
3508 scrolling moves the window contents contiguously; but horizontal
3509 scrolling causes part of @emph{each line} to go off screen.
3510
3511 Usually, no horizontal scrolling is in effect; then the leftmost
3512 column is at the left edge of the window. In this state, scrolling to
3513 the right is meaningless, since there is no data to the left of the edge
3514 to be revealed by it; so this is not allowed. Scrolling to the left is
3515 allowed; it scrolls the first columns of text off the edge of the window
3516 and can reveal additional columns on the right that were truncated
3517 before. Once a window has a nonzero amount of leftward horizontal
3518 scrolling, you can scroll it back to the right, but only so far as to
3519 reduce the net horizontal scroll to zero. There is no limit to how far
3520 left you can scroll, but eventually all the text will disappear off the
3521 left edge.
3522
3523 @vindex auto-hscroll-mode
3524 If @code{auto-hscroll-mode} is set, redisplay automatically alters
3525 the horizontal scrolling of a window as necessary to ensure that point
3526 is always visible. However, you can still set the horizontal
3527 scrolling value explicitly. The value you specify serves as a lower
3528 bound for automatic scrolling, i.e., automatic scrolling will not
3529 scroll a window to a column less than the specified one.
3530
3531 @deffn Command scroll-left &optional count set-minimum
3532 This function scrolls the selected window @var{count} columns to the
3533 left (or to the right if @var{count} is negative). The default
3534 for @var{count} is the window width, minus 2.
3535
3536 The return value is the total amount of leftward horizontal scrolling in
3537 effect after the change---just like the value returned by
3538 @code{window-hscroll} (below).
3539
3540 Once you scroll a window as far right as it can go, back to its normal
3541 position where the total leftward scrolling is zero, attempts to scroll
3542 any farther right have no effect.
3543
3544 If @var{set-minimum} is non-@code{nil}, the new scroll amount becomes
3545 the lower bound for automatic scrolling; that is, automatic scrolling
3546 will not scroll a window to a column less than the value returned by
3547 this function. Interactive calls pass non-@code{nil} for
3548 @var{set-minimum}.
3549 @end deffn
3550
3551 @deffn Command scroll-right &optional count set-minimum
3552 This function scrolls the selected window @var{count} columns to the
3553 right (or to the left if @var{count} is negative). The default
3554 for @var{count} is the window width, minus 2. Aside from the direction
3555 of scrolling, this works just like @code{scroll-left}.
3556 @end deffn
3557
3558 @defun window-hscroll &optional window
3559 This function returns the total leftward horizontal scrolling of
3560 @var{window}---the number of columns by which the text in @var{window}
3561 is scrolled left past the left margin. The default for
3562 @var{window} is the selected window.
3563
3564 The return value is never negative. It is zero when no horizontal
3565 scrolling has been done in @var{window} (which is usually the case).
3566
3567
3568 @example
3569 @group
3570 (window-hscroll)
3571 @result{} 0
3572 @end group
3573 @group
3574 (scroll-left 5)
3575 @result{} 5
3576 @end group
3577 @group
3578 (window-hscroll)
3579 @result{} 5
3580 @end group
3581 @end example
3582 @end defun
3583
3584 @defun set-window-hscroll window columns
3585 This function sets horizontal scrolling of @var{window}. The value of
3586 @var{columns} specifies the amount of scrolling, in terms of columns
3587 from the left margin. The argument @var{columns} should be zero or
3588 positive; if not, it is taken as zero. Fractional values of
3589 @var{columns} are not supported at present.
3590
3591 Note that @code{set-window-hscroll} may appear not to work if you test
3592 it by evaluating a call with @kbd{M-:} in a simple way. What happens
3593 is that the function sets the horizontal scroll value and returns, but
3594 then redisplay adjusts the horizontal scrolling to make point visible,
3595 and this overrides what the function did. You can observe the
3596 function's effect if you call it while point is sufficiently far from
3597 the left margin that it will remain visible.
3598
3599 The value returned is @var{columns}.
3600
3601 @example
3602 @group
3603 (set-window-hscroll (selected-window) 10)
3604 @result{} 10
3605 @end group
3606 @end example
3607 @end defun
3608
3609 Here is how you can determine whether a given position @var{position}
3610 is off the screen due to horizontal scrolling:
3611
3612 @c FIXME: Maybe hscroll-on-screen-p is a better name?
3613 @example
3614 @group
3615 (defun hscroll-on-screen (window position)
3616 (save-excursion
3617 (goto-char position)
3618 (and
3619 (>= (- (current-column) (window-hscroll window)) 0)
3620 (< (- (current-column) (window-hscroll window))
3621 (window-width window)))))
3622 @end group
3623 @end example
3624
3625 @node Coordinates and Windows
3626 @section Coordinates and Windows
3627 @cindex frame-relative coordinate
3628 @cindex coordinate, relative to frame
3629 @cindex window position
3630
3631 This section describes functions that report the position of a
3632 window. Most of these functions report positions relative to the
3633 window's frame. In this case, the coordinate origin @samp{(0,0)} lies
3634 near the upper left corner of the frame. For technical reasons, on
3635 graphical displays the origin is not located at the exact corner of
3636 the graphical window as it appears on the screen. If Emacs is built
3637 with the GTK+ toolkit, the origin is at the upper left corner of the
3638 frame area used for displaying Emacs windows, below the title-bar,
3639 GTK+ menu bar, and tool bar (since these are drawn by the window
3640 manager and/or GTK+, not by Emacs). But if Emacs is not built with
3641 GTK+, the origin is at the upper left corner of the tool bar (since in
3642 this case Emacs itself draws the tool bar). In both cases, the X and
3643 Y coordinates increase rightward and downward respectively.
3644
3645 Except where noted, X and Y coordinates are reported in integer
3646 character units, i.e., numbers of lines and columns respectively. On a
3647 graphical display, each ``line'' and ``column'' corresponds to the
3648 height and width of a default character specified by the frame's
3649 default font.
3650
3651 @defun window-edges &optional window
3652 This function returns a list of the edge coordinates of @var{window}.
3653 If @var{window} is omitted or @code{nil}, it defaults to the selected
3654 window.
3655
3656 The return value has the form @code{(@var{left} @var{top} @var{right}
3657 @var{bottom})}. These list elements are, respectively, the X
3658 coordinate of the leftmost column occupied by the window, the Y
3659 coordinate of the topmost row, the X coordinate one column to the
3660 right of the rightmost column, and the Y coordinate one row down from
3661 the bottommost row.
3662
3663 Note that these are the actual outer edges of the window, including any
3664 header line, mode line, scroll bar, fringes, window divider and display
3665 margins. On a text terminal, if the window has a neighbor on its right,
3666 its right edge includes the separator line between the window and its
3667 neighbor.
3668 @end defun
3669
3670 @defun window-inside-edges &optional window
3671 This function is similar to @code{window-edges}, but the returned edge
3672 values are for the text area of the window. They exclude any header
3673 line, mode line, scroll bar, fringes, window divider, display margins,
3674 and vertical separator.
3675 @end defun
3676
3677 @defun window-top-line &optional window
3678 This function returns the Y coordinate of the topmost row of
3679 @var{window}, equivalent to the @var{top} entry in the list returned
3680 by @code{window-edges}.
3681 @end defun
3682
3683 @defun window-left-column &optional window
3684 This function returns the X coordinate of the leftmost column of
3685 @var{window}, equivalent to the @var{left} entry in the list returned
3686 by @code{window-edges}.
3687 @end defun
3688
3689 The following functions can be used to relate a set of
3690 frame-relative coordinates to a window:
3691
3692 @defun window-at x y &optional frame
3693 This function returns the live window at the frame-relative
3694 coordinates @var{x} and @var{y}, on frame @var{frame}. If there is no
3695 window at that position, the return value is @code{nil}. If
3696 @var{frame} is omitted or @code{nil}, it defaults to the selected
3697 frame.
3698 @end defun
3699
3700 @defun coordinates-in-window-p coordinates window
3701 This function checks whether a window @var{window} occupies the
3702 frame-relative coordinates @var{coordinates}, and if so, which part of
3703 the window that is. @var{window} should be a live window.
3704 @var{coordinates} should be a cons cell of the form @code{(@var{x}
3705 . @var{y})}, where @var{x} and @var{y} are frame-relative coordinates.
3706
3707 If there is no window at the specified position, the return value is
3708 @code{nil} . Otherwise, the return value is one of the following:
3709
3710 @table @code
3711 @item (@var{relx} . @var{rely})
3712 The coordinates are inside @var{window}. The numbers @var{relx} and
3713 @var{rely} are the equivalent window-relative coordinates for the
3714 specified position, counting from 0 at the top left corner of the
3715 window.
3716
3717 @item mode-line
3718 The coordinates are in the mode line of @var{window}.
3719
3720 @item header-line
3721 The coordinates are in the header line of @var{window}.
3722
3723 @item right-divider
3724 The coordinates are in the divider separating @var{window} from a
3725 window on the right.
3726
3727 @item bottom-divider
3728 The coordinates are in the divider separating @var{window} from a
3729 window beneath.
3730
3731 @item vertical-line
3732 The coordinates are in the vertical line between @var{window} and its
3733 neighbor to the right. This value occurs only if the window doesn't
3734 have a scroll bar; positions in a scroll bar are considered outside the
3735 window for these purposes.
3736
3737 @item left-fringe
3738 @itemx right-fringe
3739 The coordinates are in the left or right fringe of the window.
3740
3741 @item left-margin
3742 @itemx right-margin
3743 The coordinates are in the left or right margin of the window.
3744
3745 @item nil
3746 The coordinates are not in any part of @var{window}.
3747 @end table
3748
3749 The function @code{coordinates-in-window-p} does not require a frame as
3750 argument because it always uses the frame that @var{window} is on.
3751 @end defun
3752
3753 The following functions return window positions in pixels, rather
3754 than character units. Though mostly useful on graphical displays,
3755 they can also be called on text terminals, where the screen area of
3756 each text character is taken to be ``one pixel''.
3757
3758 @defun window-pixel-edges &optional window
3759 This function returns a list of pixel coordinates for the edges of
3760 @var{window}. If @var{window} is omitted or @code{nil}, it defaults
3761 to the selected window.
3762
3763 The return value has the form @code{(@var{left} @var{top} @var{right}
3764 @var{bottom})}. The list elements are, respectively, the X pixel
3765 coordinate of the left window edge, the Y pixel coordinate of the top
3766 edge, one more than the X pixel coordinate of the right edge, and one
3767 more than the Y pixel coordinate of the bottom edge.
3768 @end defun
3769
3770 @defun window-inside-pixel-edges &optional window
3771 This function is like @code{window-pixel-edges}, except that it
3772 returns the pixel coordinates for the edges of the window's text area,
3773 rather than the pixel coordinates for the edges of the window itself.
3774 @var{window} must specify a live window.
3775 @end defun
3776
3777 The following functions return window positions in pixels, relative
3778 to the display screen rather than the frame:
3779
3780 @defun window-absolute-pixel-edges &optional window
3781 This function is like @code{window-pixel-edges}, except that it
3782 returns the edge pixel coordinates relative to the top left corner of
3783 the display screen.
3784 @end defun
3785
3786 @defun window-inside-absolute-pixel-edges &optional window
3787 This function is like @code{window-inside-pixel-edges}, except that it
3788 returns the edge pixel coordinates relative to the top left corner of
3789 the display screen. @var{window} must specify a live window.
3790 @end defun
3791
3792 @defun window-pixel-left &optional window
3793 This function returns the left pixel edge of window @var{window}.
3794 @var{window} must be a valid window and defaults to the selected one.
3795 @end defun
3796
3797 @defun window-pixel-top &optional window
3798 This function returns the top pixel edge of window @var{window}.
3799 @var{window} must be a valid window and defaults to the selected one.
3800 @end defun
3801
3802
3803 @node Window Configurations
3804 @section Window Configurations
3805 @cindex window configurations
3806 @cindex saving window information
3807
3808 A @dfn{window configuration} records the entire layout of one
3809 frame---all windows, their sizes, which buffers they contain, how those
3810 buffers are scrolled, and their value of point; also their
3811 fringes, margins, and scroll bar settings. It also includes the value
3812 of @code{minibuffer-scroll-window}. As a special exception, the window
3813 configuration does not record the value of point in the selected window
3814 for the current buffer.
3815
3816 You can bring back an entire frame layout by restoring a previously
3817 saved window configuration. If you want to record the layout of all
3818 frames instead of just one, use a frame configuration instead of a
3819 window configuration. @xref{Frame Configurations}.
3820
3821 @defun current-window-configuration &optional frame
3822 This function returns a new object representing @var{frame}'s current
3823 window configuration. The default for @var{frame} is the selected
3824 frame. The variable @code{window-persistent-parameters} specifies
3825 which window parameters (if any) are saved by this function.
3826 @xref{Window Parameters}.
3827 @end defun
3828
3829 @defun set-window-configuration configuration
3830 This function restores the configuration of windows and buffers as
3831 specified by @var{configuration}, for the frame that @var{configuration}
3832 was created for.
3833
3834 The argument @var{configuration} must be a value that was previously
3835 returned by @code{current-window-configuration}. The configuration is
3836 restored in the frame from which @var{configuration} was made, whether
3837 that frame is selected or not. This always counts as a window size
3838 change and triggers execution of the @code{window-size-change-functions}
3839 (@pxref{Window Hooks}), because @code{set-window-configuration} doesn't
3840 know how to tell whether the new configuration actually differs from the
3841 old one.
3842
3843 If the frame from which @var{configuration} was saved is dead, all this
3844 function does is restore the three variables @code{window-min-height},
3845 @code{window-min-width} and @code{minibuffer-scroll-window}. In this
3846 case, the function returns @code{nil}. Otherwise, it returns @code{t}.
3847
3848 Here is a way of using this function to get the same effect
3849 as @code{save-window-excursion}:
3850
3851 @example
3852 @group
3853 (let ((config (current-window-configuration)))
3854 (unwind-protect
3855 (progn (split-window-below nil)
3856 @dots{})
3857 (set-window-configuration config)))
3858 @end group
3859 @end example
3860 @end defun
3861
3862 @defmac save-window-excursion forms@dots{}
3863 This macro records the window configuration of the selected frame,
3864 executes @var{forms} in sequence, then restores the earlier window
3865 configuration. The return value is the value of the final form in
3866 @var{forms}.
3867
3868 Most Lisp code should not use this macro; @code{save-selected-window}
3869 is typically sufficient. In particular, this macro cannot reliably
3870 prevent the code in @var{forms} from opening new windows, because new
3871 windows might be opened in other frames (@pxref{Choosing Window}), and
3872 @code{save-window-excursion} only saves and restores the window
3873 configuration on the current frame.
3874
3875 Do not use this macro in @code{window-size-change-functions}; exiting
3876 the macro triggers execution of @code{window-size-change-functions},
3877 leading to an endless loop.
3878 @end defmac
3879
3880 @defun window-configuration-p object
3881 This function returns @code{t} if @var{object} is a window configuration.
3882 @end defun
3883
3884 @defun compare-window-configurations config1 config2
3885 This function compares two window configurations as regards the
3886 structure of windows, but ignores the values of point and the
3887 saved scrolling positions---it can return @code{t} even if those
3888 aspects differ.
3889
3890 The function @code{equal} can also compare two window configurations; it
3891 regards configurations as unequal if they differ in any respect, even a
3892 saved point.
3893 @end defun
3894
3895 @defun window-configuration-frame config
3896 This function returns the frame for which the window configuration
3897 @var{config} was made.
3898 @end defun
3899
3900 Other primitives to look inside of window configurations would make
3901 sense, but are not implemented because we did not need them. See the
3902 file @file{winner.el} for some more operations on windows
3903 configurations.
3904
3905 The objects returned by @code{current-window-configuration} die
3906 together with the Emacs process. In order to store a window
3907 configuration on disk and read it back in another Emacs session, you
3908 can use the functions described next. These functions are also useful
3909 to clone the state of a frame into an arbitrary live window
3910 (@code{set-window-configuration} effectively clones the windows of a
3911 frame into the root window of that very frame only).
3912
3913 @cindex window state
3914 @defun window-state-get &optional window writable
3915 This function returns the state of @var{window} as a Lisp object. The
3916 argument @var{window} must be a valid window and defaults to the root
3917 window of the selected frame.
3918
3919 If the optional argument @var{writable} is non-@code{nil}, this means to
3920 not use markers for sampling positions like @code{window-point} or
3921 @code{window-start}. This argument should be non-@code{nil} when the
3922 state will be written to disk and read back in another session.
3923
3924 Together, the argument @var{writable} and the variable
3925 @code{window-persistent-parameters} specify which window parameters are
3926 saved by this function. @xref{Window Parameters}.
3927 @end defun
3928
3929 The value returned by @code{window-state-get} can be used in the same
3930 session to make a clone of a window in another window. It can be also
3931 written to disk and read back in another session. In either case, use
3932 the following function to restore the state of the window.
3933
3934 @defun window-state-put state &optional window ignore
3935 This function puts the window state @var{state} into @var{window}.
3936 The argument @var{state} should be the state of a window returned by
3937 an earlier invocation of @code{window-state-get}, see above. The
3938 optional argument @var{window} can be either a live window or an
3939 internal window (@pxref{Windows and Frames}) and defaults to the
3940 selected one. If @var{window} is not live, it is replaced by a live
3941 window before putting @var{state} into it.
3942
3943 If the optional argument @var{ignore} is non-@code{nil}, it means to ignore
3944 minimum window sizes and fixed-size restrictions. If @var{ignore}
3945 is @code{safe}, this means windows can get as small as one line
3946 and/or two columns.
3947 @end defun
3948
3949
3950 @node Window Parameters
3951 @section Window Parameters
3952 @cindex window parameters
3953
3954 This section describes how window parameters can be used to associate
3955 additional information with windows.
3956
3957 @defun window-parameter window parameter
3958 This function returns @var{window}'s value for @var{parameter}. The
3959 default for @var{window} is the selected window. If @var{window} has no
3960 setting for @var{parameter}, this function returns @code{nil}.
3961 @end defun
3962
3963 @defun window-parameters &optional window
3964 This function returns all parameters of @var{window} and their values.
3965 The default for @var{window} is the selected window. The return value
3966 is either @code{nil}, or an association list whose elements have the form
3967 @code{(@var{parameter} . @var{value})}.
3968 @end defun
3969
3970 @defun set-window-parameter window parameter value
3971 This function sets @var{window}'s value of @var{parameter} to
3972 @var{value} and returns @var{value}. The default for @var{window}
3973 is the selected window.
3974 @end defun
3975
3976 By default, the functions that save and restore window configurations or the
3977 states of windows (@pxref{Window Configurations}) do not care about
3978 window parameters. This means that when you change the value of a
3979 parameter within the body of a @code{save-window-excursion}, the
3980 previous value is not restored when that macro exits. It also means
3981 that when you restore via @code{window-state-put} a window state saved
3982 earlier by @code{window-state-get}, all cloned windows have their
3983 parameters reset to @code{nil}. The following variable allows you to
3984 override the standard behavior:
3985
3986 @defvar window-persistent-parameters
3987 This variable is an alist specifying which parameters get saved by
3988 @code{current-window-configuration} and @code{window-state-get}, and
3989 subsequently restored by @code{set-window-configuration} and
3990 @code{window-state-put}. @xref{Window Configurations}.
3991
3992 The @sc{car} of each entry of this alist is a symbol specifying the
3993 parameter. The @sc{cdr} should be one of the following:
3994
3995 @table @asis
3996 @item @code{nil}
3997 This value means the parameter is saved neither by
3998 @code{window-state-get} nor by @code{current-window-configuration}.
3999
4000 @item @code{t}
4001 This value specifies that the parameter is saved by
4002 @code{current-window-configuration} and (provided its @var{writable}
4003 argument is @code{nil}) by @code{window-state-get}.
4004
4005 @item @code{writable}
4006 This means that the parameter is saved unconditionally by both
4007 @code{current-window-configuration} and @code{window-state-get}. This
4008 value should not be used for parameters whose values do not have a read
4009 syntax. Otherwise, invoking @code{window-state-put} in another session
4010 may fail with an @code{invalid-read-syntax} error.
4011 @end table
4012 @end defvar
4013
4014 Some functions (notably @code{delete-window},
4015 @code{delete-other-windows} and @code{split-window}), may behave specially
4016 when their @var{window} argument has a parameter set. You can override
4017 such special behavior by binding the following variable to a
4018 non-@code{nil} value:
4019
4020 @defvar ignore-window-parameters
4021 If this variable is non-@code{nil}, some standard functions do not
4022 process window parameters. The functions currently affected by this are
4023 @code{split-window}, @code{delete-window}, @code{delete-other-windows},
4024 and @code{other-window}.
4025
4026 An application can bind this variable to a non-@code{nil} value around
4027 calls to these functions. If it does so, the application is fully
4028 responsible for correctly assigning the parameters of all involved
4029 windows when exiting that function.
4030 @end defvar
4031
4032 The following parameters are currently used by the window management
4033 code:
4034
4035 @table @asis
4036 @item @code{delete-window}
4037 This parameter affects the execution of @code{delete-window}
4038 (@pxref{Deleting Windows}).
4039
4040 @item @code{delete-other-windows}
4041 This parameter affects the execution of @code{delete-other-windows}
4042 (@pxref{Deleting Windows}).
4043
4044 @item @code{split-window}
4045 This parameter affects the execution of @code{split-window}
4046 (@pxref{Splitting Windows}).
4047
4048 @item @code{other-window}
4049 This parameter affects the execution of @code{other-window}
4050 (@pxref{Cyclic Window Ordering}).
4051
4052 @item @code{no-other-window}
4053 This parameter marks the window as not selectable by @code{other-window}
4054 (@pxref{Cyclic Window Ordering}).
4055
4056 @item @code{clone-of}
4057 This parameter specifies the window that this one has been cloned
4058 from. It is installed by @code{window-state-get} (@pxref{Window
4059 Configurations}).
4060
4061 @item @code{preserved-size}
4062 This parameter specifies a buffer, a direction where @code{nil} means
4063 vertical and @code{t} horizontal, and a size in pixels. If this window
4064 displays the specified buffer and its size in the indicated direction
4065 equals the size specified by this parameter, then Emacs will try to
4066 preserve the size of this window in the indicated direction. This
4067 parameter is installed and updated by the function
4068 @code{window-preserve-size} (@pxref{Preserving Window Sizes}).
4069
4070 @item @code{quit-restore}
4071 This parameter is installed by the buffer display functions
4072 (@pxref{Choosing Window}) and consulted by @code{quit-restore-window}
4073 (@pxref{Quitting Windows}). It contains four elements:
4074
4075 The first element is one of the symbols @code{window}, meaning that the
4076 window has been specially created by @code{display-buffer}; @code{frame},
4077 a separate frame has been created; @code{same}, the window has
4078 displayed the same buffer before; or @code{other}, the window showed
4079 another buffer before.
4080
4081 The second element is either one of the symbols @code{window} or
4082 @code{frame}, or a list whose elements are the buffer shown in the
4083 window before, that buffer's window start and window point positions,
4084 and the window's height at that time.
4085
4086 The third element is the window selected at the time the parameter was
4087 created. The function @code{quit-restore-window} tries to reselect that
4088 window when it deletes the window passed to it as argument.
4089
4090 The fourth element is the buffer whose display caused the creation of
4091 this parameter. @code{quit-restore-window} deletes the specified window
4092 only if it still shows that buffer.
4093 @end table
4094
4095 There are additional parameters @code{window-atom} and @code{window-side};
4096 these are reserved and should not be used by applications.
4097
4098
4099 @node Window Hooks
4100 @section Hooks for Window Scrolling and Changes
4101 @cindex hooks for window operations
4102
4103 This section describes how a Lisp program can take action whenever a
4104 window displays a different part of its buffer or a different buffer.
4105 There are three actions that can change this: scrolling the window,
4106 switching buffers in the window, and changing the size of the window.
4107 The first two actions run @code{window-scroll-functions}; the last runs
4108 @code{window-size-change-functions}.
4109
4110 @defvar window-scroll-functions
4111 This variable holds a list of functions that Emacs should call before
4112 redisplaying a window with scrolling. Displaying a different buffer in
4113 the window also runs these functions.
4114
4115 This variable is not a normal hook, because each function is called with
4116 two arguments: the window, and its new display-start position.
4117
4118 These functions must take care when using @code{window-end}
4119 (@pxref{Window Start and End}); if you need an up-to-date value, you
4120 must use the @var{update} argument to ensure you get it.
4121
4122 @strong{Warning:} don't use this feature to alter the way the window
4123 is scrolled. It's not designed for that, and such use probably won't
4124 work.
4125 @end defvar
4126
4127 @defvar window-size-change-functions
4128 This variable holds a list of functions to be called if the size of any
4129 window changes for any reason. The functions are called just once per
4130 redisplay, and just once for each frame on which size changes have
4131 occurred.
4132
4133 Each function receives the frame as its sole argument. There is no
4134 direct way to find out which windows on that frame have changed size, or
4135 precisely how. However, if a size-change function records, at each
4136 call, the existing windows and their sizes, it can also compare the
4137 present sizes and the previous sizes.
4138
4139 Creating or deleting windows counts as a size change, and therefore
4140 causes these functions to be called. Changing the frame size also
4141 counts, because it changes the sizes of the existing windows.
4142
4143 You may use @code{save-selected-window} in these functions
4144 (@pxref{Selecting Windows}). However, do not use
4145 @code{save-window-excursion} (@pxref{Window Configurations}); exiting
4146 that macro counts as a size change, which would cause these functions
4147 to be called over and over.
4148 @end defvar
4149
4150 @defvar window-configuration-change-hook
4151 A normal hook that is run every time you change the window configuration
4152 of an existing frame. This includes splitting or deleting windows,
4153 changing the sizes of windows, or displaying a different buffer in a
4154 window.
4155
4156 The buffer-local part of this hook is run once for each window on the
4157 affected frame, with the relevant window selected and its buffer
4158 current. The global part is run once for the modified frame, with that
4159 frame selected.
4160 @end defvar
4161
4162 In addition, you can use @code{jit-lock-register} to register a Font
4163 Lock fontification function, which will be called whenever parts of a
4164 buffer are (re)fontified because a window was scrolled or its size
4165 changed. @xref{Other Font Lock Variables}.