]> code.delx.au - gnu-emacs/blob - lispref/positions.texi
Initial revision
[gnu-emacs] / lispref / positions.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/positions
6 @node Positions, Markers, Frames, Top
7 @chapter Positions
8 @cindex position (in buffer)
9
10 A @dfn{position} is the index of a character in the text of 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, the
14 we 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 can
18 also be represented as @dfn{markers}---special objects which relocate
19 automatically when text is inserted or deleted so they stay with the
20 surrounding characters. @xref{Markers}.
21
22 @menu
23 * Point:: The special position where editing takes place.
24 * Motion:: Changing point.
25 * Excursions:: Temporary motion and buffer changes.
26 * Narrowing:: Restricting editing to a portion of the buffer.
27 @end menu
28
29 @node Point
30 @section Point
31 @cindex point
32
33 @dfn{Point} is a special buffer position used by many editing
34 commands, including the self-inserting typed characters and text
35 insertion functions. Other commands move point through the text
36 to allow editing and insertion at different places.
37
38 Like other positions, point designates a place between two characters
39 (or before the first character, or after the last character), rather
40 than a particular character. Many terminals display the cursor over the
41 character that immediately follows point; on such terminals, point is
42 actually before the character on which the cursor sits.
43
44 @cindex point with narrowing
45 The value of point is a number between 1 and the buffer size plus 1.
46 If narrowing is in effect (@pxref{Narrowing}), then point is constrained
47 to fall within the accessible portion of the buffer (possibly at one end
48 of it).
49
50 Each buffer has its own value of point, which is independent of the
51 value of point in other buffers. Each window also has a value of point,
52 which is independent of the value of point in other windows on the same
53 buffer. This is why point can have different values in various windows
54 that display the same buffer. When a buffer appears in only one window,
55 the buffer's point and the window's point normally have the same value,
56 so the distinction is rarely important. @xref{Window Point}, for more
57 details.
58
59 @defun point
60 @cindex current buffer position
61 This function returns the position of point in the current buffer,
62 as an integer.
63
64 @need 700
65 @example
66 @group
67 (point)
68 @result{} 175
69 @end group
70 @end example
71 @end defun
72
73 @defun point-min
74 This function returns the minimum accessible value of point in the
75 current buffer. This is 1, unless narrowing is in effect, in
76 which case it is the position of the start of the region that you
77 narrowed to. (@xref{Narrowing}.)
78 @end defun
79
80 @defun point-max
81 This function returns the maximum accessible value of point in the
82 current buffer. This is @code{(1+ (buffer-size))}, unless narrowing is
83 in effect, in which case it is the position of the end of the region
84 that you narrowed to. (@xref{Narrowing}).
85 @end defun
86
87 @defun buffer-end flag
88 This function returns @code{(point-min)} if @var{flag} is less than 1,
89 @code{(point-max)} otherwise. The argument @var{flag} must be a number.
90 @end defun
91
92 @defun buffer-size
93 This function returns the total number of characters in the current
94 buffer. In the absence of any narrowing (@pxref{Narrowing}),
95 @code{point-max} returns a value one larger than this.
96
97 @example
98 @group
99 (buffer-size)
100 @result{} 35
101 @end group
102 @group
103 (point-max)
104 @result{} 36
105 @end group
106 @end example
107 @end defun
108
109 @defvar buffer-saved-size
110 The value of this buffer-local variable is the former length of the
111 current buffer, as of the last time it was read in, saved or auto-saved.
112 @end defvar
113
114 @node Motion
115 @section Motion
116
117 Motion functions change the value of point, either relative to the
118 current value of point, relative to the beginning or end of the buffer,
119 or relative to the edges of the selected window. @xref{Point}.
120
121 @menu
122 * Character Motion:: Moving in terms of characters.
123 * Word Motion:: Moving in terms of words.
124 * Buffer End Motion:: Moving to the beginning or end of the buffer.
125 * Text Lines:: Moving in terms of lines of text.
126 * Screen Lines:: Moving in terms of lines as displayed.
127 * Vertical Motion:: Implementation of @code{next-line} and
128 @code{previous-line}.
129 * List Motion:: Moving by parsing lists and sexps.
130 * Skipping Characters:: Skipping characters belonging to a certain set.
131 @end menu
132
133 @node Character Motion
134 @subsection Motion by Characters
135
136 These functions move point based on a count of characters.
137 @code{goto-char} is the fundamental primitive; the functions others use
138 that.
139
140 @deffn Command goto-char position
141 This function sets point in the current buffer to the value
142 @var{position}. If @var{position} is less than 1, it moves point to the
143 beginning of the buffer. If @var{position} is greater than the length
144 of the buffer, it moves point to the end.
145
146 If narrowing is in effect, @var{position} still counts from the
147 beginning of the buffer, but point cannot go outside the accessible
148 portion. If @var{position} is out of range, @code{goto-char} moves
149 point to the beginning or the end of the accessible portion.
150
151 When this function is called interactively, @var{position} is the
152 numeric prefix argument, if provided; otherwise it is read from the
153 minibuffer.
154
155 @code{goto-char} returns @var{position}.
156 @end deffn
157
158 @deffn Command forward-char &optional count
159 @c @kindex beginning-of-buffer
160 @c @kindex end-of-buffer
161 This function moves point @var{count} characters forward, towards the
162 end of the buffer (or backward, towards the beginning of the buffer, if
163 @var{count} is negative). If the function attempts to move point past
164 the beginning or end of the buffer (or the limits of the accessible
165 portion, when narrowing is in effect), an error is signaled with error
166 code @code{beginning-of-buffer} or @code{end-of-buffer}.
167
168 In an interactive call, @var{count} is the numeric prefix argument.
169 @end deffn
170
171 @deffn Command backward-char &optional count
172 This function moves point @var{count} characters backward, towards the
173 beginning of the buffer (or forward, towards the end of the buffer, if
174 @var{count} is negative). If the function attempts to move point past
175 the beginning or end of the buffer (or the limits of the accessible
176 portion, when narrowing is in effect), an error is signaled with error
177 code @code{beginning-of-buffer} or @code{end-of-buffer}.
178
179 In an interactive call, @var{count} is the numeric prefix argument.
180 @end deffn
181
182 @node Word Motion
183 @subsection Motion by Words
184
185 These functions for parsing words use the syntax table to decide
186 whether a given character is part of a word. @xref{Syntax Tables}.
187
188 @deffn Command forward-word count
189 This function moves point forward @var{count} words (or backward if
190 @var{count} is negative). Normally it returns @code{t}. If this motion
191 encounters the beginning or end of the buffer, or the limits of the
192 accessible portion when narrowing is in effect, point stops there
193 and the value is @code{nil}.
194
195 In an interactive call, @var{count} is set to the numeric prefix
196 argument.
197 @end deffn
198
199 @deffn Command backward-word count
200 This function just like @code{forward-word}, except that it moves
201 backward until encountering the front of a word, rather than forward.
202
203 In an interactive call, @var{count} is set to the numeric prefix
204 argument.
205
206 This function is rarely used in programs, as it is more efficient to
207 call @code{forward-word} with negative argument.
208 @end deffn
209
210 @defvar words-include-escapes
211 @c Emacs 19 feature
212 This variable affects the behavior of @code{forward-word} and everything
213 that uses it. If it is non-@code{nil}, then characters in the
214 ``escape'' and ``character quote'' syntax classes count as part of
215 words. Otherwise, they do not.
216 @end defvar
217
218 @node Buffer End Motion
219 @subsection Motion to an End of the Buffer
220
221 To move point to the beginning of the buffer, write:
222
223 @example
224 @group
225 (goto-char (point-min))
226 @end group
227 @end example
228
229 @noindent
230 Likewise, to move to the end of the buffer, use:
231
232 @example
233 @group
234 (goto-char (point-max))
235 @end group
236 @end example
237
238 Here are two commands which users use to do these things. They are
239 documented here to warn you not to use them in Lisp programs, because
240 they set the mark and display messages in the echo area.
241
242 @deffn Command beginning-of-buffer &optional n
243 This function moves point to the beginning of the buffer (or the limits
244 of the accessible portion, when narrowing is in effect), setting the
245 mark at the previous position. If @var{n} is non-@code{nil}, then it
246 puts point @var{n} tenths of the way from the beginning of the buffer.
247
248 In an interactive call, @var{n} is the numeric prefix argument,
249 if provided; otherwise @var{n} defaults to @code{nil}.
250
251 Don't use this function in Lisp programs!
252 @end deffn
253
254 @deffn Command end-of-buffer &optional n
255 This function moves point to the end of the buffer (or the limits of
256 the accessible portion, when narrowing is in effect), setting the mark
257 at the previous position. If @var{n} is non-@code{nil}, then it puts
258 point @var{n} tenths of the way from the end.
259
260 In an interactive call, @var{n} is the numeric prefix argument,
261 if provided; otherwise @var{n} defaults to @code{nil}.
262
263 Don't use this function in Lisp programs!
264 @end deffn
265
266 @node Text Lines
267 @subsection Motion by Text Lines
268 @cindex lines
269
270 Text lines are portions of the buffer delimited by newline characters,
271 which are regarded as part of the previous line. The first text line
272 begins at the beginning of the buffer, and the last text line ends at
273 the end of the buffer whether or not the last character is a newline.
274 The division of the buffer into text lines is not affected by the width
275 of the window, by line continuation in display, or by how tabs and
276 control characters are displayed.
277
278 @deffn Command goto-line line
279 This function moves point to the front of the @var{line}th line,
280 counting from line 1 at beginning of buffer. If @var{line} is less than
281 1, it moves point to the beginning of the buffer. If @var{line} is
282 greater than the number of lines in the buffer, it moves point to the
283 @emph{end of the last line} of the buffer.
284
285 If narrowing is in effect, then @var{line} still counts from the
286 beginning of the buffer, but point cannot go outside the accessible
287 portion. So @code{goto-line} moves point to the beginning or end of the
288 accessible portion, if the line number specifies an inaccessible
289 position.
290
291 The return value of @code{goto-line} is the difference between
292 @var{line} and the line number of the line to which point actually was
293 able move (in the full buffer, disregarding any narrowing). Thus, the
294 value is positive if the scan encounters the real end of the buffer.
295
296 In an interactive call, @var{line} is the numeric prefix argument if
297 one has been provided. Otherwise @var{line} is read in the minibuffer.
298 @end deffn
299
300 @deffn Command beginning-of-line &optional count
301 This function moves point to the beginning of the current line. With an
302 argument @var{count} not @code{nil} or 1, it moves forward
303 @var{count}@minus{}1 lines and then to the beginning of the line.
304
305 If this function reaches the end of the buffer (or of the accessible
306 portion, if narrowing is in effect), it positions point at the end of
307 the buffer. No error is signaled.
308 @end deffn
309
310 @deffn Command end-of-line &optional count
311 This function moves point to the end of the current line. With an
312 argument @var{count} not @code{nil} or 1, it moves forward
313 @var{count}@minus{}1 lines and then to the end of the line.
314
315 If this function reaches the end of the buffer (or of the accessible
316 portion, if narrowing is in effect), it positions point at the end of
317 the buffer. No error is signaled.
318 @end deffn
319
320 @deffn Command forward-line &optional count
321 @cindex beginning of line
322 This function moves point forward @var{count} lines, to the beginning of
323 the line. If @var{count} is negative, it moves point
324 @minus{}@var{count} lines backward, to the beginning of the line.
325
326 If @code{forward-line} encounters the beginning or end of the buffer (or
327 of the accessible portion) before finding that many lines, it sets point
328 there. No error is signaled.
329
330 @code{forward-line} returns the difference between @var{count} and the
331 number of lines actually moved. If you attempt to move down five lines
332 from the beginning of a buffer that has only three lines, point stops at
333 the end of the last line, and the value will be 2.
334
335 In an interactive call, @var{count} is the numeric prefix argument.
336 @end deffn
337
338 @defun count-lines start end
339 @cindex lines in region
340 This function returns the number of lines between the positions
341 @var{start} and @var{end} in the current buffer. If @var{start} and
342 @var{end} are equal, then it returns 0. Otherwise it returns at least
343 1, even if @var{start} and @var{end} are on the same line. This is
344 because the text between them, considered in isolation, must contain at
345 least one line unless it is empty.
346
347 Here is an example of using @code{count-lines}:
348
349 @example
350 @group
351 (defun current-line ()
352 "Return the vertical position of point@dots{}"
353 (+ (count-lines (window-start) (point))
354 (if (= (current-column) 0) 1 0)
355 -1))
356 @end group
357 @end example
358 @end defun
359
360 @ignore
361 @c ================
362 The @code{previous-line} and @code{next-line} commands are functions
363 that should not be used in programs. They are for users and are
364 mentioned here only for completeness.
365
366 @deffn Command previous-line count
367 @cindex goal column
368 This function moves point up @var{count} lines (down if @var{count}
369 is negative). In moving, it attempts to keep point in the ``goal column''
370 (normally the same column that it was at the beginning of the move).
371
372 If there is no character in the target line exactly under the current
373 column, point is positioned after the character in that line which
374 spans this column, or at the end of the line if it is not long enough.
375
376 If it attempts to move beyond the top or bottom of the buffer (or clipped
377 region), then point is positioned in the goal column in the top or
378 bottom line. No error is signaled.
379
380 In an interactive call, @var{count} will be the numeric
381 prefix argument.
382
383 The command @code{set-goal-column} can be used to create a semipermanent
384 goal column to which this command always moves. Then it does not try to
385 move vertically.
386
387 If you are thinking of using this in a Lisp program, consider using
388 @code{forward-line} with a negative argument instead. It is usually easier
389 to use and more reliable (no dependence on goal column, etc.).
390 @end deffn
391
392 @deffn Command next-line count
393 This function moves point down @var{count} lines (up if @var{count}
394 is negative). In moving, it attempts to keep point in the ``goal column''
395 (normally the same column that it was at the beginning of the move).
396
397 If there is no character in the target line exactly under the current
398 column, point is positioned after the character in that line which
399 spans this column, or at the end of the line if it is not long enough.
400
401 If it attempts to move beyond the top or bottom of the buffer (or clipped
402 region), then point is positioned in the goal column in the top or
403 bottom line. No error is signaled.
404
405 In the case where the @var{count} is 1, and point is on the last
406 line of the buffer (or clipped region), a new empty line is inserted at the
407 end of the buffer (or clipped region) and point moved there.
408
409 In an interactive call, @var{count} will be the numeric
410 prefix argument.
411
412 The command @code{set-goal-column} can be used to create a semipermanent
413 goal column to which this command always moves. Then it does not try to
414 move vertically.
415
416 If you are thinking of using this in a Lisp program, consider using
417 @code{forward-line} instead. It is usually easier
418 to use and more reliable (no dependence on goal column, etc.).
419 @end deffn
420
421 @c ================
422 @end ignore
423
424 Also see the functions @code{bolp} and @code{eolp} in @ref{Near Point}.
425 These functions do not move point, but test whether it is already at the
426 beginning or end of a line.
427
428 @node Screen Lines
429 @subsection Motion by Screen Lines
430
431 The line functions in the previous section count text lines, delimited
432 only by newline characters. By contrast, these functions count screen
433 lines, which are defined by the way the text appears on the screen. A
434 text line is a single screen line if it is short enough to fit the width
435 of the selected window, but otherwise it may occupy several screen
436 lines.
437
438 In some cases, text lines are truncated on the screen rather than
439 continued onto additional screen lines. In these cases,
440 @code{vertical-motion} moves point much like @code{forward-line}.
441 @xref{Truncation}.
442
443 Because the width of a given string depends on the flags which control
444 the appearance of certain characters, @code{vertical-motion} behaves
445 differently, for a given piece of text, depending on the buffer it is
446 in, and even on the selected window (because the width, the truncation
447 flag, and display table may vary between windows). @xref{Usual
448 Display}.
449
450 @defun vertical-motion count
451 This function moves point to the start of the screen line @var{count}
452 screen lines down from the screen line containing point. If @var{count}
453 is negative, it moves up instead.
454
455 This function returns the number of lines moved. The value may be less
456 in absolute value than @var{count} if the beginning or end of the buffer
457 was reached.
458 @end defun
459
460 @deffn Command move-to-window-line count
461 This function moves point with respect to the text currently displayed
462 in the selected window. It moves point to the beginning of the screen
463 line @var{count} screen lines from the top of the window. If
464 @var{count} is negative, that specifies a position
465 @w{@minus{}@var{count}} lines from the bottom---or else the last line of
466 the buffer, if the buffer ends above the specified screen position.
467
468 If @var{count} is @code{nil}, then point moves to the beginning of the
469 line in the middle of the window. If the absolute value of @var{count}
470 is greater than the size of the window, then point moves to the place
471 which would appear on that screen line if the window were tall enough.
472 This will probably cause the next redisplay to scroll to bring that
473 location onto the screen.
474
475 In an interactive call, @var{count} is the numeric prefix argument.
476
477 The value returned is the window line number, with the top line in the
478 window numbered 0.
479 @end deffn
480
481 @defun compute-motion from frompos to topos width offsets
482 This function scan through the current buffer, calculating screen
483 position. It scans the current buffer forward from position @var{from},
484 assuming that is at screen coordinates @var{frompos}, to position
485 @var{to} or coordinates @var{topos}, whichever comes first. It returns
486 the ending buffer position and screen coordinates.
487
488 The coordinate arguments @var{frompos} and @var{topos} are cons cells of
489 the form @code{(@var{hpos} . @var{vpos})}.
490
491 The argument @var{width} is the number of columns available to display
492 text; this affects handling of continuation lines. Use the value
493 returned by @code{window-width} for the window of your choice.
494
495 The argument @var{offsets} is either @code{nil} or a cons cell of the
496 form @code{(@var{hscroll} . @var{tab-offset})}. Here @var{hscroll} is
497 the number of columns not being displayed at the left margin; in most
498 calls, this comes from @code{window-hscroll}. Meanwhile,
499 @var{tab-offset} is the number of columns of an initial tab character
500 (at @var{from}) that aren't included in the display, perhaps because the
501 line was continued within that character.
502
503 The return value is a list of five elements:
504
505 @example
506 (@var{pos} @var{vpos} @var{hpos} @var{prevhpos} @var{contin})
507 @end example
508
509 @noindent
510 Here @var{pos} is the buffer position where the scan stopped, @var{vpos}
511 is the vertical position, and @var{hpos} is the horizontal position.
512
513 The result @var{prevhpos} is the horizontal position one character back
514 from @var{pos}. The result @var{contin} is @code{t} if a line was
515 continued after (or within) the previous character.
516
517 For example, to find the buffer position of column @var{col} of line
518 @var{line} of a certain window, pass the window's display start location
519 as @var{from} and the window's upper-left coordinates as @var{frompos}.
520 Pass the buffer's @code{(point-max)} as @var{to}, to limit the scan to
521 the end of the visible section of the buffer, and pass @var{line} and
522 @var{col} as @var{topos}. Here's a function that does this:
523
524 @example
525 (defun coordinates-of-position (col line)
526 (car (compute-motion (window-start)
527 '(0 . 0)
528 (point)
529 (cons col line)
530 (window-width)
531 (cons (window-hscroll) 0))))
532 @end example
533 @end defun
534
535 @node Vertical Motion
536 @comment node-name, next, previous, up
537 @subsection The User-Level Vertical Motion Commands
538 @cindex goal column
539 @cindex vertical text line motion
540 @findex next-line
541 @findex previous-line
542
543 A goal column is useful if you want to edit text such as a table in
544 which you want to move point to a certain column on each line. The goal
545 column affects the vertical text line motion commands, @code{next-line}
546 and @code{previous-line}. @xref{Basic,, Basic Editing Commands, emacs,
547 The GNU Emacs Manual}.
548
549 @defopt goal-column
550 This variable holds an explicitly specified goal column for vertical
551 line motion commands. If it is an integer, it specifies a column, and
552 these commands try to move to that column on each line. If it is
553 @code{nil}, then the commands set their own goal columns. Any other
554 value is invalid.
555 @end defopt
556
557 @defvar temporary-goal-column
558 This variable holds the temporary goal column during a sequence of
559 consecutive vertical line motion commands. It is overridden by
560 @code{goal-column} if that is non-@code{nil}. It is set each time a
561 vertical motion command is invoked, unless the previous command was also
562 a vertical motion command.
563 @end defvar
564
565 @defopt track-eol
566 This variable controls how the vertical line motion commands operate
567 when starting at the end of a line. If @code{track-eol} is
568 non-@code{nil}, then vertical motion starting at the end of a line will
569 keep to the ends of lines. This means moving to the end of each line
570 moved onto. The value of @code{track-eol} has no effect if point is not
571 at the end of a line when the first vertical motion command is given.
572
573 @code{track-eol} has its effect by telling line motion commands to set
574 @code{temporary-goal-column} to 9999 instead of to the current column.
575 @end defopt
576
577 @node List Motion
578 @comment node-name, next, previous, up
579 @subsection Moving over Balanced Expressions
580 @cindex sexp motion
581 @cindex Lisp expression motion
582 @cindex list motion
583
584 Here are several functions concerned with balanced-parenthesis
585 expressions (also called @dfn{sexps} in connection with moving across
586 them in Emacs). The syntax table controls how these functions interpret
587 various characters; see @ref{Syntax Tables}. @xref{Parsing
588 Expressions}, for lower-level primitives for scanning sexps or parts of
589 sexps. For user-level commands, see @ref{Lists and Sexps,,, emacs, GNU
590 Emacs Manual}.
591
592 @deffn Command forward-list arg
593 Move forward across @var{arg} balanced groups of parentheses.
594 (Other syntactic entities such as words or paired string quotes
595 are ignored.)
596 @end deffn
597
598 @deffn Command backward-list arg
599 Move backward across @var{arg} balanced groups of parentheses.
600 (Other syntactic entities such as words or paired string quotes
601 are ignored.)
602 @end deffn
603
604 @deffn Command up-list arg
605 Move forward out of @var{arg} levels of parentheses.
606 A negative argument means move backward but still to a less deep spot.
607 @end deffn
608
609 @deffn Command down-list arg
610 Move forward down @var{arg} levels of parentheses. A negative argument
611 means move backward but still go down @var{arg} levels.
612 @end deffn
613
614 @deffn Command forward-sexp arg
615 Move forward across @var{arg} balanced expressions.
616 Balanced expressions include both those delimited by parentheses
617 and other kinds, such as words and string constants. For example,
618
619 @example
620 @group
621 ---------- Buffer: foo ----------
622 (concat@point{} "foo " (car x) y z)
623 ---------- Buffer: foo ----------
624 @end group
625
626 @group
627 (forward-sexp 3)
628 @result{} nil
629
630 ---------- Buffer: foo ----------
631 (concat "foo " (car x) y@point{} z)
632 ---------- Buffer: foo ----------
633 @end group
634 @end example
635 @end deffn
636
637 @deffn Command backward-sexp arg
638 Move backward across @var{arg} balanced expressions.
639 @end deffn
640
641 @node Skipping Characters
642 @comment node-name, next, previous, up
643 @subsection Skipping Characters
644 @cindex skipping characters
645
646 The following two functions move point over a specified set of
647 characters. For example, they are often used to skip whitespace. For
648 related functions, see @ref{Motion and Syntax}.
649
650 @defun skip-chars-forward character-set &optional limit
651 This function moves point in the current buffer forward, skipping over a
652 given set of characters. It examines the character following point,
653 then advances point if the character matches @var{character-set}. This
654 continues until it reaches a character that does not match. The
655 function returns @code{nil}.
656
657 The argument @var{character-set} is like the inside of a
658 @samp{[@dots{}]} in a regular expression except that @samp{]} is never
659 special and @samp{\} quotes @samp{^}, @samp{-} or @samp{\}. Thus,
660 @code{"a-zA-Z"} skips over all letters, stopping before the first
661 nonletter, and @code{"^a-zA-Z}" skips nonletters stopping before the
662 first letter. @xref{Regular Expressions}.
663
664 If @var{limit} is supplied (it must be a number or a marker), it
665 specifies the maximum position in the buffer that point can be skipped
666 to. Point will stop at or before @var{limit}.
667
668 In the following example, point is initially located directly before the
669 @samp{T}. After the form is evaluated, point is located at the end of
670 that line (between the @samp{t} of @samp{hat} and the newline). The
671 function skips all letters and spaces, but not newlines.
672
673 @example
674 @group
675 ---------- Buffer: foo ----------
676 I read "@point{}The cat in the hat
677 comes back" twice.
678 ---------- Buffer: foo ----------
679 @end group
680
681 @group
682 (skip-chars-forward "a-zA-Z ")
683 @result{} nil
684
685 ---------- Buffer: foo ----------
686 I read "The cat in the hat@point{}
687 comes back" twice.
688 ---------- Buffer: foo ----------
689 @end group
690 @end example
691 @end defun
692
693 @defun skip-chars-backward character-set &optional limit
694 This function moves point backward, skipping characters that match
695 @var{character-set}, until @var{limit}. It just like
696 @code{skip-chars-forward} except for the direction of motion.
697 @end defun
698
699 @node Excursions
700 @section Excursions
701 @cindex excursion
702
703 It is often useful to move point ``temporarily'' within a localized
704 portion of the program, or to switch buffers temporarily. This is
705 called an @dfn{excursion}, and it is done with the @code{save-excursion}
706 special form. This construct saves the current buffer and its values of
707 point and the mark so they can be restored after the completion of the
708 excursion.
709
710 The forms for saving and restoring the configuration of windows are
711 described elsewhere (see @ref{Window Configurations}, and @pxref{Frame
712 Configurations}).
713
714 @defspec save-excursion forms@dots{}
715 @cindex mark excursion
716 @cindex point excursion
717 @cindex current buffer excursion
718 The @code{save-excursion} special form saves the identity of the current
719 buffer and the values of point and the mark in it, evaluates @var{forms},
720 and finally restores the buffer and its saved values of point and the mark.
721 All three saved values are restored even in case of an abnormal exit
722 via throw or error (@pxref{Nonlocal Exits}).
723
724 The @code{save-excursion} special form is the standard way to switch
725 buffers or move point within one part of a program and avoid affecting
726 the rest of the program. It is used more than 500 times in the Lisp
727 sources of Emacs.
728
729 @code{save-excursion} does not save the values of point and the mark for
730 other buffers, so changes in other buffers remain in effect after
731 @code{save-excursion} exits.
732
733 @cindex window excursions
734 Likewise, @code{save-excursion} does not restore window-buffer
735 correspondences altered by functions such as @code{switch-to-buffer}.
736 One way to restore these correspondences, and the selected window, is to
737 use @code{save-window-excursion} inside @code{save-excursion}
738 (@pxref{Window Configurations}).
739
740 The value returned by @code{save-excursion} is the result of the last of
741 @var{forms}, or @code{nil} if no @var{forms} are given.
742
743 @example
744 @group
745 (save-excursion
746 @var{forms})
747 @equiv{}
748 (let ((old-buf (current-buffer))
749 (old-pnt (point-marker))
750 (old-mark (copy-marker (mark-marker))))
751 (unwind-protect
752 (progn @var{forms})
753 (set-buffer old-buf)
754 (goto-char old-pnt)
755 (set-marker (mark-marker) old-mark)))
756 @end group
757 @end example
758 @end defspec
759
760 @node Narrowing
761 @section Narrowing
762 @cindex narrowing
763 @cindex restriction (in a buffer)
764 @cindex accessible portion (of a buffer)
765
766 @dfn{Narrowing} means limiting the text addressable by Emacs editing
767 commands to a limited range of characters in a buffer. The text that
768 remains addressable is called the @dfn{accessible portion} of the
769 buffer.
770
771 Narrowing is specified with two buffer positions which become the
772 beginning and end of the accessible portion. For most editing commands
773 and most Emacs primitives, these positions replace the values of the
774 beginning and end of the buffer. While narrowing is in effect, no text
775 outside the accessible portion is displayed, and point cannot move
776 outside the accessible portion.
777
778 Values such as positions or line numbers, that usually count from the
779 beginning of the buffer, do so despite narrowing, but the functions
780 which use them refuse to operate on text that is inaccessible.
781
782 The commands for saving buffers are unaffected by narrowing; they save
783 the entire buffer regardless of the any narrowing.
784
785 @deffn Command narrow-to-region start end
786 This function sets the accessible portion of the current buffer to start
787 at @var{start} and end at @var{end}. Both arguments should be character
788 positions.
789
790 In an interactive call, @var{start} and @var{end} are set to the bounds
791 of the current region (point and the mark, with the smallest first).
792 @end deffn
793
794 @deffn Command narrow-to-page move-count
795 This function sets the accessible portion of the current buffer to
796 include just the current page. An optional first argument
797 @var{move-count} non-@code{nil} means to move forward or backward by
798 @var{move-count} pages and then narrow. The variable
799 @code{page-delimiter} specifies where pages start and end
800 (@pxref{Standard Regexps}).
801
802 In an interactive call, @var{move-count} is set to the numeric prefix
803 argument.
804 @end deffn
805
806 @deffn Command widen
807 @cindex widening
808 This function cancels any narrowing in the current buffer, so that the
809 entire contents are accessible. This is called @dfn{widening}.
810 It is equivalent to the following expression:
811
812 @example
813 (narrow-to-region 1 (1+ (buffer-size)))
814 @end example
815 @end deffn
816
817 @defspec save-restriction body@dots{}
818 This special form saves the current bounds of the accessible portion,
819 evaluates the @var{body} forms, and finally restores the saved bounds,
820 thus restoring the same state of narrowing (or absence thereof) formerly
821 in effect. The state of narrowing is restored even in the event of an
822 abnormal exit via throw or error (@pxref{Nonlocal Exits}). Therefore,
823 this construct is a clean way to narrow a buffer temporarily.
824
825 The value returned by @code{save-restriction} is that returned by the
826 last form in @var{body}, or @code{nil} if no body forms were given.
827
828 @c Wordy to avoid overfull hbox. --rjc 16mar92
829 @strong{Caution:} it is easy to make a mistake when using the
830 @code{save-restriction} construct. Read the entire description here
831 before you try it.
832
833 If @var{body} changes the current buffer, @code{save-restriction} still
834 restores the restrictions on the original buffer (the buffer whose
835 restructions it saved from), but it does not restore the identity of the
836 current buffer.
837
838 @code{save-restriction} does @emph{not} restore point and the mark; use
839 @code{save-excursion} for that. If you use both @code{save-restriction}
840 and @code{save-excursion} together, @code{save-excursion} should come
841 first (on the outside). Otherwise, the old point value would be
842 restored with temporary narrowing still in effect. If the old point
843 value were outside the limits of the temporary narrowing, this would
844 fail to restore it accurately.
845
846 The @code{save-restriction} special form records the values of the
847 beginning and end of the accessible portion as distances from the
848 beginning and end of the buffer. In other words, it records the amount
849 of inaccessible text before and after the accessible portion.
850
851 This method yields correct results if @var{body} does further narrowing.
852 However, @code{save-restriction} can become confused if the body widens
853 and then make changes outside the range of the saved narrowing. When
854 this is what you want to do, @code{save-restriction} is not the right
855 tool for the job. Here is what you must use instead:
856
857 @example
858 @group
859 (let ((beg (point-min-marker))
860 (end (point-max-marker)))
861 (unwind-protect
862 (progn @var{body})
863 (save-excursion
864 (set-buffer (marker-buffer beg))
865 (narrow-to-region beg end))))
866 @end group
867 @end example
868
869 Here is a simple example of correct use of @code{save-restriction}:
870
871 @example
872 @group
873 ---------- Buffer: foo ----------
874 This is the contents of foo
875 This is the contents of foo
876 This is the contents of foo@point{}
877 ---------- Buffer: foo ----------
878 @end group
879
880 @group
881 (save-excursion
882 (save-restriction
883 (goto-char 1)
884 (forward-line 2)
885 (narrow-to-region 1 (point))
886 (goto-char (point-min))
887 (replace-string "foo" "bar")))
888
889 ---------- Buffer: foo ----------
890 This is the contents of bar
891 This is the contents of bar
892 This is the contents of foo@point{}
893 ---------- Buffer: foo ----------
894 @end group
895 @end example
896 @end defspec