]> code.delx.au - gnu-emacs/blob - doc/lispref/positions.texi
9daf5cef05968b30406d1d348f8c4a3442898108
[gnu-emacs] / doc / lispref / positions.texi
1 @c -*- mode: texinfo; coding: utf-8 -*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-2015 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @node Positions
6 @chapter Positions
7 @cindex position (in buffer)
8 @cindex buffer position
9
10 A @dfn{position} is the index of a character in the text of a buffer.
11 More precisely, a position identifies the place between two characters
12 (or before the first character, or after the last character), so we can
13 speak of the character before or after a given position. However, we
14 often speak of the character ``at'' a position, meaning the character
15 after that position.
16
17 Positions are usually represented as integers starting from 1, but
18 can also be represented as @dfn{markers}---special objects that
19 relocate automatically when text is inserted or deleted so they stay
20 with the surrounding characters. Functions that expect an argument to
21 be a position (an integer), but accept a marker as a substitute,
22 normally ignore which buffer the marker points into; they convert the
23 marker to an integer, and use that integer, exactly as if you had
24 passed the integer as the argument, even if the marker points to the
25 wrong buffer. A marker that points nowhere cannot convert to an
26 integer; using it instead of an integer causes an error.
27 @xref{Markers}.
28
29 See also the field feature (@pxref{Fields}), which provides
30 functions that are used by many cursor-motion commands.
31
32 @menu
33 * Point:: The special position where editing takes place.
34 * Motion:: Changing point.
35 * Excursions:: Temporary motion and buffer changes.
36 * Narrowing:: Restricting editing to a portion of the buffer.
37 @end menu
38
39 @node Point
40 @section Point
41 @cindex point
42
43 @dfn{Point} is a special buffer position used by many editing
44 commands, including the self-inserting typed characters and text
45 insertion functions. Other commands move point through the text
46 to allow editing and insertion at different places.
47
48 Like other positions, point designates a place between two characters
49 (or before the first character, or after the last character), rather
50 than a particular character. Usually terminals display the cursor over
51 the character that immediately follows point; point is actually before
52 the character on which the cursor sits.
53
54 @cindex point with narrowing
55 The value of point is a number no less than 1, and no greater than the
56 buffer size plus 1. If narrowing is in effect (@pxref{Narrowing}), then
57 point is constrained to fall within the accessible portion of the buffer
58 (possibly at one end of it).
59
60 Each buffer has its own value of point, which is independent of the
61 value of point in other buffers. Each window also has a value of point,
62 which is independent of the value of point in other windows on the same
63 buffer. This is why point can have different values in various windows
64 that display the same buffer. When a buffer appears in only one window,
65 the buffer's point and the window's point normally have the same value,
66 so the distinction is rarely important. @xref{Window Point}, for more
67 details.
68
69 @defun point
70 @cindex current buffer position
71 This function returns the value of point in the current buffer,
72 as an integer.
73
74 @need 700
75 @example
76 @group
77 (point)
78 @result{} 175
79 @end group
80 @end example
81 @end defun
82
83 @defun point-min
84 This function returns the minimum accessible value of point in the
85 current buffer. This is normally 1, but if narrowing is in effect, it
86 is the position of the start of the region that you narrowed to.
87 (@xref{Narrowing}.)
88 @end defun
89
90 @defun point-max
91 This function returns the maximum accessible value of point in the
92 current buffer. This is @code{(1+ (buffer-size))}, unless narrowing is
93 in effect, in which case it is the position of the end of the region
94 that you narrowed to. (@xref{Narrowing}.)
95 @end defun
96
97 @defun buffer-end flag
98 This function returns @code{(point-max)} if @var{flag} is greater than
99 0, @code{(point-min)} otherwise. The argument @var{flag} must be a
100 number.
101 @end defun
102
103 @defun buffer-size &optional buffer
104 This function returns the total number of characters in the current
105 buffer. In the absence of any narrowing (@pxref{Narrowing}),
106 @code{point-max} returns a value one larger than this.
107
108 If you specify a buffer, @var{buffer}, then the value is the
109 size of @var{buffer}.
110
111 @example
112 @group
113 (buffer-size)
114 @result{} 35
115 @end group
116 @group
117 (point-max)
118 @result{} 36
119 @end group
120 @end example
121 @end defun
122
123 @node Motion
124 @section Motion
125 @cindex motion by chars, words, lines, lists
126
127 Motion functions change the value of point, either relative to the
128 current value of point, relative to the beginning or end of the buffer,
129 or relative to the edges of the selected window. @xref{Point}.
130
131 @menu
132 * Character Motion:: Moving in terms of characters.
133 * Word Motion:: Moving in terms of words.
134 * Buffer End Motion:: Moving to the beginning or end of the buffer.
135 * Text Lines:: Moving in terms of lines of text.
136 * Screen Lines:: Moving in terms of lines as displayed.
137 * List Motion:: Moving by parsing lists and sexps.
138 * Skipping Characters:: Skipping characters belonging to a certain set.
139 @end menu
140
141 @node Character Motion
142 @subsection Motion by Characters
143
144 These functions move point based on a count of characters.
145 @code{goto-char} is the fundamental primitive; the other functions use
146 that.
147
148 @deffn Command goto-char position
149 This function sets point in the current buffer to the value
150 @var{position}.
151 @c This behavior used to be documented until 2013/08.
152 @ignore
153 If @var{position} is less than 1, it moves point to the beginning of
154 the buffer. If @var{position} is greater than the length of the
155 buffer, it moves point to the end.
156 @end ignore
157
158 If narrowing is in effect, @var{position} still counts from the
159 beginning of the buffer, but point cannot go outside the accessible
160 portion. If @var{position} is out of range, @code{goto-char} moves
161 point to the beginning or the end of the accessible portion.
162
163 When this function is called interactively, @var{position} is the
164 numeric prefix argument, if provided; otherwise it is read from the
165 minibuffer.
166
167 @code{goto-char} returns @var{position}.
168 @end deffn
169
170 @deffn Command forward-char &optional count
171 @c @kindex beginning-of-buffer
172 @c @kindex end-of-buffer
173 This function moves point @var{count} characters forward, towards the
174 end of the buffer (or backward, towards the beginning of the buffer, if
175 @var{count} is negative). If @var{count} is @code{nil}, the default
176 is 1.
177
178 If this attempts to move past the beginning or end of the buffer (or
179 the limits of the accessible portion, when narrowing is in effect), it
180 signals an error with error symbol @code{beginning-of-buffer} or
181 @code{end-of-buffer}.
182
183 In an interactive call, @var{count} is the numeric prefix argument.
184 @end deffn
185
186 @deffn Command backward-char &optional count
187 This is just like @code{forward-char} except that it moves
188 in the opposite direction.
189 @end deffn
190
191 @node Word Motion
192 @subsection Motion by Words
193
194 These functions for parsing words use the syntax table to decide
195 whether a given character is part of a word. @xref{Syntax Tables}.
196
197 @deffn Command forward-word &optional count
198 This function moves point forward @var{count} words (or backward if
199 @var{count} is negative). If @var{count} is omitted or @code{nil}, it
200 defaults to 1.
201
202 ``Moving one word'' means moving until point crosses a
203 word-constituent character and then encounters a word-separator
204 character. However, this function cannot move point past the boundary
205 of the accessible portion of the buffer, or across a field boundary
206 (@pxref{Fields}). The most common case of a field boundary is the end
207 of the prompt in the minibuffer.
208
209 If it is possible to move @var{count} words, without being stopped
210 prematurely by the buffer boundary or a field boundary, the value is
211 @code{t}. Otherwise, the return value is @code{nil} and point stops at
212 the buffer boundary or field boundary.
213
214 If @code{inhibit-field-text-motion} is non-@code{nil},
215 this function ignores field boundaries.
216
217 In an interactive call, @var{count} is specified by the numeric prefix
218 argument.
219 @end deffn
220
221 @deffn Command backward-word &optional count
222 This function is just like @code{forward-word}, except that it moves
223 backward until encountering the front of a word, rather than forward.
224 @end deffn
225
226 @defopt words-include-escapes
227 @c Emacs 19 feature
228 This variable affects the behavior of @code{forward-word} and everything
229 that uses it. If it is non-@code{nil}, then characters in the
230 escape and character-quote syntax classes count as part of
231 words. Otherwise, they do not.
232 @end defopt
233
234 @defvar inhibit-field-text-motion
235 If this variable is non-@code{nil}, certain motion functions including
236 @code{forward-word}, @code{forward-sentence}, and
237 @code{forward-paragraph} ignore field boundaries.
238 @end defvar
239
240 @node Buffer End Motion
241 @subsection Motion to an End of the Buffer
242 @cindex move to beginning or end of buffer
243
244 To move point to the beginning of the buffer, write:
245
246 @example
247 @group
248 (goto-char (point-min))
249 @end group
250 @end example
251
252 @noindent
253 Likewise, to move to the end of the buffer, use:
254
255 @example
256 @group
257 (goto-char (point-max))
258 @end group
259 @end example
260
261 Here are two commands that users use to do these things. They are
262 documented here to warn you not to use them in Lisp programs, because
263 they set the mark and display messages in the echo area.
264
265 @deffn Command beginning-of-buffer &optional n
266 This function moves point to the beginning of the buffer (or the limits
267 of the accessible portion, when narrowing is in effect), setting the
268 mark at the previous position (except in Transient Mark mode, if
269 the mark is already active, it does not set the mark.)
270
271 If @var{n} is non-@code{nil}, then it puts point @var{n} tenths of the
272 way from the beginning of the accessible portion of the buffer. In an
273 interactive call, @var{n} is the numeric prefix argument, if provided;
274 otherwise @var{n} defaults to @code{nil}.
275
276 @strong{Warning:} Don't use this function in Lisp programs!
277 @end deffn
278
279 @deffn Command end-of-buffer &optional n
280 This function moves point to the end of the buffer (or the limits of
281 the accessible portion, when narrowing is in effect), setting the mark
282 at the previous position (except in Transient Mark mode when the mark
283 is already active). If @var{n} is non-@code{nil}, then it puts point
284 @var{n} tenths of the way from the end of the accessible portion of
285 the buffer.
286
287 In an interactive call, @var{n} is the numeric prefix argument,
288 if provided; otherwise @var{n} defaults to @code{nil}.
289
290 @strong{Warning:} Don't use this function in Lisp programs!
291 @end deffn
292
293 @node Text Lines
294 @subsection Motion by Text Lines
295 @cindex lines
296
297 Text lines are portions of the buffer delimited by newline characters,
298 which are regarded as part of the previous line. The first text line
299 begins at the beginning of the buffer, and the last text line ends at
300 the end of the buffer whether or not the last character is a newline.
301 The division of the buffer into text lines is not affected by the width
302 of the window, by line continuation in display, or by how tabs and
303 control characters are displayed.
304
305 @deffn Command beginning-of-line &optional count
306 This function moves point to the beginning of the current line. With an
307 argument @var{count} not @code{nil} or 1, it moves forward
308 @var{count}@minus{}1 lines and then to the beginning of the line.
309
310 This function does not move point across a field boundary
311 (@pxref{Fields}) unless doing so would move beyond there to a
312 different line; therefore, if @var{count} is @code{nil} or 1, and
313 point starts at a field boundary, point does not move. To ignore
314 field boundaries, either bind @code{inhibit-field-text-motion} to
315 @code{t}, or use the @code{forward-line} function instead. For
316 instance, @code{(forward-line 0)} does the same thing as
317 @code{(beginning-of-line)}, except that it ignores field boundaries.
318
319 If this function reaches the end of the buffer (or of the accessible
320 portion, if narrowing is in effect), it positions point there. No error
321 is signaled.
322 @end deffn
323
324 @defun line-beginning-position &optional count
325 Return the position that @code{(beginning-of-line @var{count})}
326 would move to.
327 @end defun
328
329 @deffn Command end-of-line &optional count
330 This function moves point to the end of the current line. With an
331 argument @var{count} not @code{nil} or 1, it moves forward
332 @var{count}@minus{}1 lines and then to the end of the line.
333
334 This function does not move point across a field boundary
335 (@pxref{Fields}) unless doing so would move beyond there to a
336 different line; therefore, if @var{count} is @code{nil} or 1, and
337 point starts at a field boundary, point does not move. To ignore
338 field boundaries, bind @code{inhibit-field-text-motion} to @code{t}.
339
340 If this function reaches the end of the buffer (or of the accessible
341 portion, if narrowing is in effect), it positions point there. No error
342 is signaled.
343 @end deffn
344
345 @defun line-end-position &optional count
346 Return the position that @code{(end-of-line @var{count})}
347 would move to.
348 @end defun
349
350 @deffn Command forward-line &optional count
351 @cindex beginning of line
352 This function moves point forward @var{count} lines, to the beginning of
353 the line following that. If @var{count} is negative, it moves point
354 @minus{}@var{count} lines backward, to the beginning of a line
355 preceding that. If @var{count} is zero, it moves point to the
356 beginning of the current line. If @var{count} is @code{nil}, that
357 means 1.
358
359 If @code{forward-line} encounters the beginning or end of the buffer (or
360 of the accessible portion) before finding that many lines, it sets point
361 there. No error is signaled.
362
363 @code{forward-line} returns the difference between @var{count} and the
364 number of lines actually moved. If you attempt to move down five lines
365 from the beginning of a buffer that has only three lines, point stops at
366 the end of the last line, and the value will be 2. As an explicit
367 exception, if the last accessible line is non-empty, but has no
368 newline (e.g., if the buffer ends without a newline), the function
369 sets point to the end of that line, and the value returned by the
370 function counts that line as one line successfully moved.
371
372 In an interactive call, @var{count} is the numeric prefix argument.
373 @end deffn
374
375 @defun count-lines start end
376 @cindex lines in region
377 @anchor{Definition of count-lines}
378 This function returns the number of lines between the positions
379 @var{start} and @var{end} in the current buffer. If @var{start} and
380 @var{end} are equal, then it returns 0. Otherwise it returns at least
381 1, even if @var{start} and @var{end} are on the same line. This is
382 because the text between them, considered in isolation, must contain at
383 least one line unless it is empty.
384 @end defun
385
386 @deffn Command count-words start end
387 @cindex words in region
388 This function returns the number of words between the positions
389 @var{start} and @var{end} in the current buffer.
390
391 This function can also be called interactively. In that case, it
392 prints a message reporting the number of lines, words, and characters
393 in the buffer, or in the region if the region is active.
394 @end deffn
395
396 @defun line-number-at-pos &optional pos
397 @cindex line number
398 This function returns the line number in the current buffer
399 corresponding to the buffer position @var{pos}. If @var{pos} is @code{nil}
400 or omitted, the current buffer position is used.
401 @end defun
402
403 @ignore
404 @c ================
405 The @code{previous-line} and @code{next-line} commands are functions
406 that should not be used in programs. They are for users and are
407 mentioned here only for completeness.
408
409 @deffn Command previous-line count
410 @cindex goal column
411 This function moves point up @var{count} lines (down if @var{count}
412 is negative). In moving, it attempts to keep point in the @dfn{goal column}
413 (normally the same column that it was at the beginning of the move).
414
415 If there is no character in the target line exactly under the current
416 column, point is positioned after the character in that line which
417 spans this column, or at the end of the line if it is not long enough.
418
419 If it attempts to move beyond the top or bottom of the buffer (or clipped
420 region), then point is positioned in the goal column in the top or
421 bottom line. No error is signaled.
422
423 In an interactive call, @var{count} will be the numeric
424 prefix argument.
425
426 The command @code{set-goal-column} can be used to create a semipermanent
427 goal column to which this command always moves. Then it does not try to
428 move vertically.
429
430 If you are thinking of using this in a Lisp program, consider using
431 @code{forward-line} with a negative argument instead. It is usually easier
432 to use and more reliable (no dependence on goal column, etc.).
433 @end deffn
434
435 @deffn Command next-line count
436 This function moves point down @var{count} lines (up if @var{count}
437 is negative). In moving, it attempts to keep point in the goal column
438 (normally the same column that it was at the beginning of the move).
439
440 If there is no character in the target line exactly under the current
441 column, point is positioned after the character in that line which
442 spans this column, or at the end of the line if it is not long enough.
443
444 If it attempts to move beyond the top or bottom of the buffer (or clipped
445 region), then point is positioned in the goal column in the top or
446 bottom line. No error is signaled.
447
448 In the case where the @var{count} is 1, and point is on the last
449 line of the buffer (or clipped region), a new empty line is inserted at the
450 end of the buffer (or clipped region) and point moved there.
451
452 In an interactive call, @var{count} will be the numeric
453 prefix argument.
454
455 The command @code{set-goal-column} can be used to create a semipermanent
456 goal column to which this command always moves. Then it does not try to
457 move vertically.
458
459 If you are thinking of using this in a Lisp program, consider using
460 @code{forward-line} instead. It is usually easier
461 to use and more reliable (no dependence on goal column, etc.).
462 @end deffn
463
464 @c ================
465 @end ignore
466
467 Also see the functions @code{bolp} and @code{eolp} in @ref{Near Point}.
468 These functions do not move point, but test whether it is already at the
469 beginning or end of a line.
470
471 @node Screen Lines
472 @subsection Motion by Screen Lines
473 @cindex screen lines, moving by
474
475 The line functions in the previous section count text lines, delimited
476 only by newline characters. By contrast, these functions count screen
477 lines, which are defined by the way the text appears on the screen. A
478 text line is a single screen line if it is short enough to fit the width
479 of the selected window, but otherwise it may occupy several screen
480 lines.
481
482 In some cases, text lines are truncated on the screen rather than
483 continued onto additional screen lines. In these cases,
484 @code{vertical-motion} moves point much like @code{forward-line}.
485 @xref{Truncation}.
486
487 Because the width of a given string depends on the flags that control
488 the appearance of certain characters, @code{vertical-motion} behaves
489 differently, for a given piece of text, depending on the buffer it is
490 in, and even on the selected window (because the width, the truncation
491 flag, and display table may vary between windows). @xref{Usual
492 Display}.
493
494 These functions scan text to determine where screen lines break, and
495 thus take time proportional to the distance scanned.
496 @ignore
497 If you intend to use them heavily, Emacs provides caches which may
498 improve the performance of your code. @xref{Truncation, cache-long-scans}.
499 @end ignore
500
501 @defun vertical-motion count &optional window cur-col
502 This function moves point to the start of the screen line @var{count}
503 screen lines down from the screen line containing point. If @var{count}
504 is negative, it moves up instead.
505
506 The @var{count} argument can be a cons cell, @code{(@var{cols}
507 . @var{lines})}, instead of an integer. Then the function moves by
508 @var{lines} screen lines, and puts point @var{cols} columns from the
509 visual start of that screen line. Note that @var{cols} are counted
510 from the @emph{visual} start of the line; if the window is scrolled
511 horizontally (@pxref{Horizontal Scrolling}), the column on which point
512 will end is in addition to the number of columns by which the text is
513 scrolled.
514
515 The return value is the number of screen lines over which point was
516 moved. The value may be less in absolute value than @var{count} if
517 the beginning or end of the buffer was reached.
518
519 The window @var{window} is used for obtaining parameters such as the
520 width, the horizontal scrolling, and the display table. But
521 @code{vertical-motion} always operates on the current buffer, even if
522 @var{window} currently displays some other buffer.
523
524 The optional argument @var{cur-col} specifies the current column when
525 the function is called. This is the window-relative horizontal
526 coordinate of point, measured in units of font width of the frame's
527 default face. Providing it speeds up the function, especially in very
528 long lines, because it doesn't have to go back in the buffer in order
529 to determine the current column. Note that @var{cur-col} is also
530 counted from the visual start of the line.
531 @end defun
532
533 @defun count-screen-lines &optional beg end count-final-newline window
534 This function returns the number of screen lines in the text from
535 @var{beg} to @var{end}. The number of screen lines may be different
536 from the number of actual lines, due to line continuation, the display
537 table, etc. If @var{beg} and @var{end} are @code{nil} or omitted,
538 they default to the beginning and end of the accessible portion of the
539 buffer.
540
541 If the region ends with a newline, that is ignored unless the optional
542 third argument @var{count-final-newline} is non-@code{nil}.
543
544 The optional fourth argument @var{window} specifies the window for
545 obtaining parameters such as width, horizontal scrolling, and so on.
546 The default is to use the selected window's parameters.
547
548 Like @code{vertical-motion}, @code{count-screen-lines} always uses the
549 current buffer, regardless of which buffer is displayed in
550 @var{window}. This makes possible to use @code{count-screen-lines} in
551 any buffer, whether or not it is currently displayed in some window.
552 @end defun
553
554 @deffn Command move-to-window-line count
555 This function moves point with respect to the text currently displayed
556 in the selected window. It moves point to the beginning of the screen
557 line @var{count} screen lines from the top of the window. If
558 @var{count} is negative, that specifies a position
559 @w{@minus{}@var{count}} lines from the bottom (or the last line of the
560 buffer, if the buffer ends above the specified screen position).
561
562 If @var{count} is @code{nil}, then point moves to the beginning of the
563 line in the middle of the window. If the absolute value of @var{count}
564 is greater than the size of the window, then point moves to the place
565 that would appear on that screen line if the window were tall enough.
566 This will probably cause the next redisplay to scroll to bring that
567 location onto the screen.
568
569 In an interactive call, @var{count} is the numeric prefix argument.
570
571 The value returned is the window line number point has moved to, with
572 the top line in the window numbered 0.
573 @end deffn
574
575 @vindex move-to-window-group-line-function
576 @defun move-to-window-group-line count
577 This function is like @code{move-to-window-line}, except that when the
578 selected window is a part of a group of windows (@pxref{Window
579 Group}), @code{move-to-window-group-line} will move to a position with
580 respect to the entire group, not just the single window. This
581 condition holds when the buffer local variable
582 @code{move-to-window-group-line-function} is set to a function. In
583 this case, @code{move-to-window-group-line} calls the function with
584 the argument @var{count}, then returns its result.
585 @end defun
586
587 @defun compute-motion from frompos to topos width offsets window
588 This function scans the current buffer, calculating screen positions.
589 It scans the buffer forward from position @var{from}, assuming that is
590 at screen coordinates @var{frompos}, to position @var{to} or coordinates
591 @var{topos}, whichever comes first. It returns the ending buffer
592 position and screen coordinates.
593
594 The coordinate arguments @var{frompos} and @var{topos} are cons cells of
595 the form @code{(@var{hpos} . @var{vpos})}.
596
597 The argument @var{width} is the number of columns available to display
598 text; this affects handling of continuation lines. @code{nil} means
599 the actual number of usable text columns in the window, which is
600 equivalent to the value returned by @code{(window-width window)}.
601
602 The argument @var{offsets} is either @code{nil} or a cons cell of the
603 form @code{(@var{hscroll} . @var{tab-offset})}. Here @var{hscroll} is
604 the number of columns not being displayed at the left margin; most
605 callers get this by calling @code{window-hscroll}. Meanwhile,
606 @var{tab-offset} is the offset between column numbers on the screen and
607 column numbers in the buffer. This can be nonzero in a continuation
608 line, when the previous screen lines' widths do not add up to a multiple
609 of @code{tab-width}. It is always zero in a non-continuation line.
610
611 The window @var{window} serves only to specify which display table to
612 use. @code{compute-motion} always operates on the current buffer,
613 regardless of what buffer is displayed in @var{window}.
614
615 The return value is a list of five elements:
616
617 @example
618 (@var{pos} @var{hpos} @var{vpos} @var{prevhpos} @var{contin})
619 @end example
620
621 @noindent
622 Here @var{pos} is the buffer position where the scan stopped, @var{vpos}
623 is the vertical screen position, and @var{hpos} is the horizontal screen
624 position.
625
626 The result @var{prevhpos} is the horizontal position one character back
627 from @var{pos}. The result @var{contin} is @code{t} if the last line
628 was continued after (or within) the previous character.
629
630 For example, to find the buffer position of column @var{col} of screen line
631 @var{line} of a certain window, pass the window's display start location
632 as @var{from} and the window's upper-left coordinates as @var{frompos}.
633 Pass the buffer's @code{(point-max)} as @var{to}, to limit the scan to
634 the end of the accessible portion of the buffer, and pass @var{line} and
635 @var{col} as @var{topos}. Here's a function that does this:
636
637 @example
638 (defun coordinates-of-position (col line)
639 (car (compute-motion (window-start)
640 '(0 . 0)
641 (point-max)
642 (cons col line)
643 (window-width)
644 (cons (window-hscroll) 0)
645 (selected-window))))
646 @end example
647
648 When you use @code{compute-motion} for the minibuffer, you need to use
649 @code{minibuffer-prompt-width} to get the horizontal position of the
650 beginning of the first screen line. @xref{Minibuffer Contents}.
651 @end defun
652
653 @node List Motion
654 @subsection Moving over Balanced Expressions
655 @cindex sexp motion
656 @cindex Lisp expression motion
657 @cindex list motion
658 @cindex balanced parenthesis motion
659
660 Here are several functions concerned with balanced-parenthesis
661 expressions (also called @dfn{sexps} in connection with moving across
662 them in Emacs). The syntax table controls how these functions interpret
663 various characters; see @ref{Syntax Tables}. @xref{Parsing
664 Expressions}, for lower-level primitives for scanning sexps or parts of
665 sexps. For user-level commands, see @ref{Parentheses,, Commands for
666 Editing with Parentheses, emacs, The GNU Emacs Manual}.
667
668 @deffn Command forward-list &optional arg
669 This function moves forward across @var{arg} (default 1) balanced groups of
670 parentheses. (Other syntactic entities such as words or paired string
671 quotes are ignored.)
672 @end deffn
673
674 @deffn Command backward-list &optional arg
675 This function moves backward across @var{arg} (default 1) balanced groups of
676 parentheses. (Other syntactic entities such as words or paired string
677 quotes are ignored.)
678 @end deffn
679
680 @deffn Command up-list &optional arg escape-strings no-syntax-crossing
681 This function moves forward out of @var{arg} (default 1) levels of
682 parentheses. A negative argument means move backward but still to a
683 less deep spot. If @var{escape-strings} is non-@code{nil} (as it is
684 interactively), move out of enclosing strings as well. If
685 @var{no-syntax-crossing} is non-@code{nil} (as it is interactively), prefer
686 to break out of any enclosing string instead of moving to the start of
687 a list broken across multiple strings. On error, location of point is
688 unspecified.
689 @end deffn
690
691 @deffn Command backward-up-list &optional arg escape-strings no-syntax-crossing
692 This function is just like @code{up-list}, but with a negated argument.
693 @end deffn
694
695 @deffn Command down-list &optional arg
696 This function moves forward into @var{arg} (default 1) levels of
697 parentheses. A negative argument means move backward but still go
698 deeper in parentheses (@minus{}@var{arg} levels).
699 @end deffn
700
701 @deffn Command forward-sexp &optional arg
702 This function moves forward across @var{arg} (default 1) balanced expressions.
703 Balanced expressions include both those delimited by parentheses and
704 other kinds, such as words and string constants.
705 @xref{Parsing Expressions}. For example,
706
707 @example
708 @group
709 ---------- Buffer: foo ----------
710 (concat@point{} "foo " (car x) y z)
711 ---------- Buffer: foo ----------
712 @end group
713
714 @group
715 (forward-sexp 3)
716 @result{} nil
717
718 ---------- Buffer: foo ----------
719 (concat "foo " (car x) y@point{} z)
720 ---------- Buffer: foo ----------
721 @end group
722 @end example
723 @end deffn
724
725 @deffn Command backward-sexp &optional arg
726 This function moves backward across @var{arg} (default 1) balanced expressions.
727 @end deffn
728
729 @deffn Command beginning-of-defun &optional arg
730 This function moves back to the @var{arg}th beginning of a defun. If
731 @var{arg} is negative, this actually moves forward, but it still moves
732 to the beginning of a defun, not to the end of one. @var{arg} defaults
733 to 1.
734 @end deffn
735
736 @deffn Command end-of-defun &optional arg
737 This function moves forward to the @var{arg}th end of a defun. If
738 @var{arg} is negative, this actually moves backward, but it still moves
739 to the end of a defun, not to the beginning of one. @var{arg} defaults
740 to 1.
741 @end deffn
742
743 @defopt defun-prompt-regexp
744 If non-@code{nil}, this buffer-local variable holds a regular
745 expression that specifies what text can appear before the
746 open-parenthesis that starts a defun. That is to say, a defun begins
747 on a line that starts with a match for this regular expression,
748 followed by a character with open-parenthesis syntax.
749 @end defopt
750
751 @defopt open-paren-in-column-0-is-defun-start
752 If this variable's value is non-@code{nil}, an open parenthesis in
753 column 0 is considered to be the start of a defun. If it is
754 @code{nil}, an open parenthesis in column 0 has no special meaning.
755 The default is @code{t}.
756 @end defopt
757
758 @defvar beginning-of-defun-function
759 If non-@code{nil}, this variable holds a function for finding the
760 beginning of a defun. The function @code{beginning-of-defun}
761 calls this function instead of using its normal method, passing it its
762 optional argument. If the argument is non-@code{nil}, the function
763 should move back by that many functions, like
764 @code{beginning-of-defun} does.
765 @end defvar
766
767 @defvar end-of-defun-function
768 If non-@code{nil}, this variable holds a function for finding the end of
769 a defun. The function @code{end-of-defun} calls this function instead
770 of using its normal method.
771 @end defvar
772
773 @node Skipping Characters
774 @subsection Skipping Characters
775 @cindex skipping characters
776
777 The following two functions move point over a specified set of
778 characters. For example, they are often used to skip whitespace. For
779 related functions, see @ref{Motion and Syntax}.
780
781 These functions convert the set string to multibyte if the buffer is
782 multibyte, and they convert it to unibyte if the buffer is unibyte, as
783 the search functions do (@pxref{Searching and Matching}).
784
785 @defun skip-chars-forward character-set &optional limit
786 This function moves point in the current buffer forward, skipping over a
787 given set of characters. It examines the character following point,
788 then advances point if the character matches @var{character-set}. This
789 continues until it reaches a character that does not match. The
790 function returns the number of characters moved over.
791
792 The argument @var{character-set} is a string, like the inside of a
793 @samp{[@dots{}]} in a regular expression except that @samp{]} does not
794 terminate it, and @samp{\} quotes @samp{^}, @samp{-} or @samp{\}.
795 Thus, @code{"a-zA-Z"} skips over all letters, stopping before the
796 first nonletter, and @code{"^a-zA-Z"} skips nonletters stopping before
797 the first letter. See @xref{Regular Expressions}. Character classes
798 can also be used, e.g., @code{"[:alnum:]"}. See @pxref{Char Classes}.
799
800 If @var{limit} is supplied (it must be a number or a marker), it
801 specifies the maximum position in the buffer that point can be skipped
802 to. Point will stop at or before @var{limit}.
803
804 In the following example, point is initially located directly before the
805 @samp{T}. After the form is evaluated, point is located at the end of
806 that line (between the @samp{t} of @samp{hat} and the newline). The
807 function skips all letters and spaces, but not newlines.
808
809 @example
810 @group
811 ---------- Buffer: foo ----------
812 I read "@point{}The cat in the hat
813 comes back" twice.
814 ---------- Buffer: foo ----------
815 @end group
816
817 @group
818 (skip-chars-forward "a-zA-Z ")
819 @result{} 18
820
821 ---------- Buffer: foo ----------
822 I read "The cat in the hat@point{}
823 comes back" twice.
824 ---------- Buffer: foo ----------
825 @end group
826 @end example
827 @end defun
828
829 @defun skip-chars-backward character-set &optional limit
830 This function moves point backward, skipping characters that match
831 @var{character-set}, until @var{limit}. It is just like
832 @code{skip-chars-forward} except for the direction of motion.
833
834 The return value indicates the distance traveled. It is an integer that
835 is zero or less.
836 @end defun
837
838 @node Excursions
839 @section Excursions
840 @cindex excursion
841
842 It is often useful to move point temporarily within a localized
843 portion of the program. This is called an @dfn{excursion}, and it is
844 done with the @code{save-excursion} special form. This construct
845 remembers the initial identity of the current buffer, and its value
846 of point, and restores them after the excursion
847 completes. It is the standard way to move point within one part of a
848 program and avoid affecting the rest of the program, and is used
849 thousands of times in the Lisp sources of Emacs.
850
851 If you only need to save and restore the identity of the current
852 buffer, use @code{save-current-buffer} or @code{with-current-buffer}
853 instead (@pxref{Current Buffer}). If you need to save or restore
854 window configurations, see the forms described in @ref{Window
855 Configurations} and in @ref{Frame Configurations}. @c frameset?
856
857 @defspec save-excursion body@dots{}
858 @cindex point excursion
859 This special form saves the identity of the current buffer and the
860 value of point in it, evaluates @var{body}, and finally
861 restores the buffer and its saved value of point. Both saved values are
862 restored even in case of an abnormal exit via
863 @code{throw} or error (@pxref{Nonlocal Exits}).
864
865 The value returned by @code{save-excursion} is the result of the last
866 form in @var{body}, or @code{nil} if no body forms were given.
867 @end defspec
868
869 Because @code{save-excursion} only saves point for the
870 buffer that was current at the start of the excursion, any changes
871 made to point in other buffers, during the excursion, will
872 remain in effect afterward. This frequently leads to unintended
873 consequences, so the byte compiler warns if you call @code{set-buffer}
874 during an excursion:
875
876 @example
877 Warning: Use ‘with-current-buffer’ rather than
878 save-excursion+set-buffer
879 @end example
880
881 @noindent
882 To avoid such problems, you should call @code{save-excursion} only
883 after setting the desired current buffer, as in the following example:
884
885 @example
886 @group
887 (defun append-string-to-buffer (string buffer)
888 "Append STRING to the end of BUFFER."
889 (with-current-buffer buffer
890 (save-excursion
891 (goto-char (point-max))
892 (insert string))))
893 @end group
894 @end example
895
896 @cindex window excursions
897 Likewise, @code{save-excursion} does not restore window-buffer
898 correspondences altered by functions such as @code{switch-to-buffer}.
899
900 @strong{Warning:} Ordinary insertion of text adjacent to the saved
901 point value relocates the saved value, just as it relocates all
902 markers. More precisely, the saved value is a marker with insertion
903 type @code{nil}. @xref{Marker Insertion Types}. Therefore, when the
904 saved point value is restored, it normally comes before the inserted
905 text.
906
907 @defmac save-mark-and-excursion body@dots{}
908 @cindex mark excursion
909 @cindex point excursion
910 This macro is like @code{save-excursion}, but also saves and restores
911 the mark location and @code{mark-active}. This macro does what
912 @code{save-excursion} did before Emacs 25.1.
913 @end defmac
914
915 @node Narrowing
916 @section Narrowing
917 @cindex narrowing
918 @cindex restriction (in a buffer)
919 @cindex accessible portion (of a buffer)
920
921 @dfn{Narrowing} means limiting the text addressable by Emacs editing
922 commands to a limited range of characters in a buffer. The text that
923 remains addressable is called the @dfn{accessible portion} of the
924 buffer.
925
926 Narrowing is specified with two buffer positions, which become the
927 beginning and end of the accessible portion. For most editing
928 commands and primitives, these positions replace the values of the
929 beginning and end of the buffer. While narrowing is in effect, no
930 text outside the accessible portion is displayed, and point cannot
931 move outside the accessible portion. Note that narrowing does not
932 alter actual buffer positions (@pxref{Point}); it only determines
933 which positions are considered the accessible portion of the buffer.
934 Most functions refuse to operate on text that is outside the
935 accessible portion.
936
937 Commands for saving buffers are unaffected by narrowing; they save
938 the entire buffer regardless of any narrowing.
939
940 If you need to display in a single buffer several very different
941 types of text, consider using an alternative facility described in
942 @ref{Swapping Text}.
943
944 @deffn Command narrow-to-region start end
945 This function sets the accessible portion of the current buffer to start
946 at @var{start} and end at @var{end}. Both arguments should be character
947 positions.
948
949 In an interactive call, @var{start} and @var{end} are set to the bounds
950 of the current region (point and the mark, with the smallest first).
951 @end deffn
952
953 @deffn Command narrow-to-page &optional move-count
954 This function sets the accessible portion of the current buffer to
955 include just the current page. An optional first argument
956 @var{move-count} non-@code{nil} means to move forward or backward by
957 @var{move-count} pages and then narrow to one page. The variable
958 @code{page-delimiter} specifies where pages start and end
959 (@pxref{Standard Regexps}).
960
961 In an interactive call, @var{move-count} is set to the numeric prefix
962 argument.
963 @end deffn
964
965 @deffn Command widen
966 @cindex widening
967 This function cancels any narrowing in the current buffer, so that the
968 entire contents are accessible. This is called @dfn{widening}.
969 It is equivalent to the following expression:
970
971 @example
972 (narrow-to-region 1 (1+ (buffer-size)))
973 @end example
974 @end deffn
975
976 @defun buffer-narrowed-p
977 This function returns non-@code{nil} if the buffer is narrowed, and
978 @code{nil} otherwise.
979 @end defun
980
981 @defspec save-restriction body@dots{}
982 This special form saves the current bounds of the accessible portion,
983 evaluates the @var{body} forms, and finally restores the saved bounds,
984 thus restoring the same state of narrowing (or absence thereof) formerly
985 in effect. The state of narrowing is restored even in the event of an
986 abnormal exit via @code{throw} or error (@pxref{Nonlocal Exits}).
987 Therefore, this construct is a clean way to narrow a buffer temporarily.
988
989 The value returned by @code{save-restriction} is that returned by the
990 last form in @var{body}, or @code{nil} if no body forms were given.
991
992 @c Wordy to avoid overfull hbox. --rjc 16mar92
993 @strong{Caution:} it is easy to make a mistake when using the
994 @code{save-restriction} construct. Read the entire description here
995 before you try it.
996
997 If @var{body} changes the current buffer, @code{save-restriction} still
998 restores the restrictions on the original buffer (the buffer whose
999 restrictions it saved from), but it does not restore the identity of the
1000 current buffer.
1001
1002 @code{save-restriction} does @emph{not} restore point; use
1003 @code{save-excursion} for that. If you use both @code{save-restriction}
1004 and @code{save-excursion} together, @code{save-excursion} should come
1005 first (on the outside). Otherwise, the old point value would be
1006 restored with temporary narrowing still in effect. If the old point
1007 value were outside the limits of the temporary narrowing, this would
1008 fail to restore it accurately.
1009
1010 Here is a simple example of correct use of @code{save-restriction}:
1011
1012 @example
1013 @group
1014 ---------- Buffer: foo ----------
1015 This is the contents of foo
1016 This is the contents of foo
1017 This is the contents of foo@point{}
1018 ---------- Buffer: foo ----------
1019 @end group
1020
1021 @group
1022 (save-excursion
1023 (save-restriction
1024 (goto-char 1)
1025 (forward-line 2)
1026 (narrow-to-region 1 (point))
1027 (goto-char (point-min))
1028 (replace-string "foo" "bar")))
1029
1030 ---------- Buffer: foo ----------
1031 This is the contents of bar
1032 This is the contents of bar
1033 This is the contents of foo@point{}
1034 ---------- Buffer: foo ----------
1035 @end group
1036 @end example
1037 @end defspec