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