comparison man/lispref/positions.texi @ 0:376386a54a3c r19-14

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