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