aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman1994-03-28 05:41:05 +0000
committerRichard M. Stallman1994-03-28 05:41:05 +0000
commit7015aca4520d036305770c96f7ffbb8428b79ff0 (patch)
tree4aff94bb41ab202b2d14476b755ae19be0f05c59
parent1e0a78a10c4e6289c1c8157b7466dc8fa6ba8ed4 (diff)
downloademacs-7015aca4520d036305770c96f7ffbb8428b79ff0.tar.gz
emacs-7015aca4520d036305770c96f7ffbb8428b79ff0.zip
Initial revision
-rw-r--r--lispref/abbrevs.texi331
-rw-r--r--lispref/positions.texi896
-rw-r--r--lispref/searching.texi1254
-rw-r--r--lispref/syntax.texi707
-rw-r--r--lispref/tips.texi582
5 files changed, 3770 insertions, 0 deletions
diff --git a/lispref/abbrevs.texi b/lispref/abbrevs.texi
new file mode 100644
index 00000000000..59e1995a4d5
--- /dev/null
+++ b/lispref/abbrevs.texi
@@ -0,0 +1,331 @@
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/abbrevs
6@node Abbrevs, Processes, Syntax Tables, Top
7@chapter Abbrevs And Abbrev Expansion
8@cindex abbrev
9@cindex abbrev table
10
11 An abbreviation or @dfn{abbrev} is a string of characters that may be
12expanded to a longer string. The user can insert the abbrev string and
13find it replaced automatically with the expansion of the abbrev. This
14saves typing.
15
16 The set of abbrevs currently in effect is recorded in an @dfn{abbrev
17table}. Each buffer has a local abbrev table, but normally all buffers
18in the same major mode share one abbrev table. There is also a global
19abbrev table. Normally both are used.
20
21 An abbrev table is represented as an obarray containing a symbol for
22each abbreviation. The symbol's name is the abbreviation. Its value is
23the expansion; its function definition is the hook function to do the
24expansion (if any); its property list cell contains the use count, the
25number of times the abbreviation has been expanded. Because these
26symbols are not interned in the usual obarray, they will never appear as
27the result of reading a Lisp expression; in fact, normally they are
28never used except by the code that handles abbrevs. Therefore, it is
29safe to use them in an extremely nonstandard way. @xref{Creating
30Symbols}.
31
32 For the user-level commands for abbrevs, see @ref{Abbrevs,, Abbrev
33Mode, emacs, The GNU Emacs Manual}.
34
35@menu
36* Abbrev Mode:: Setting up Emacs for abbreviation.
37* Tables: Abbrev Tables. Creating and working with abbrev tables.
38* Defining Abbrevs:: Specifying abbreviations and their expansions.
39* Files: Abbrev Files. Saving abbrevs in files.
40* Expansion: Abbrev Expansion. Controlling expansion; expansion subroutines.
41* Standard Abbrev Tables:: Abbrev tables used by various major modes.
42@end menu
43
44@node Abbrev Mode, Abbrev Tables, Abbrevs, Abbrevs
45@comment node-name, next, previous, up
46@section Setting Up Abbrev Mode
47
48 Abbrev mode is a minor mode controlled by the value of the variable
49@code{abbrev-mode}.
50
51@defvar abbrev-mode
52A non-@code{nil} value of this variable turns on the automatic expansion
53of abbrevs when their abbreviations are inserted into a buffer.
54If the value is @code{nil}, abbrevs may be defined, but they are not
55expanded automatically.
56
57This variable automatically becomes local when set in any fashion.
58@end defvar
59
60@defvar default-abbrev-mode
61This is the value @code{abbrev-mode} for buffers that do not override it.
62This is the same as @code{(default-value 'abbrev-mode)}.
63@end defvar
64
65@node Abbrev Tables, Defining Abbrevs, Abbrev Mode, Abbrevs
66@section Abbrev Tables
67
68 This section describes how to create and manipulate abbrev tables.
69
70@defun make-abbrev-table
71This function creates and returns a new, empty abbrev table---an obarray
72containing no symbols. It is a vector filled with zeros.
73@end defun
74
75@defun clear-abbrev-table table
76This function undefines all the abbrevs in abbrev table @var{table},
77leaving it empty. The function returns @code{nil}.
78@end defun
79
80@defun define-abbrev-table tabname definitions
81This function defines @var{tabname} (a symbol) as an abbrev table name,
82i.e., as a variable whose value is an abbrev table. It defines abbrevs
83in the table according to @var{definitions}, a list of elements of the
84form @code{(@var{abbrevname} @var{expansion} @var{hook}
85@var{usecount})}. The value is always @code{nil}.
86@end defun
87
88@defvar abbrev-table-name-list
89This is a list of symbols whose values are abbrev tables.
90@code{define-abbrev-table} adds the new abbrev table name to this list.
91@end defvar
92
93@defun insert-abbrev-table-description name &optional human
94This function inserts before point a description of the abbrev table
95named @var{name}. The argument @var{name} is a symbol whose value is an
96abbrev table. The value is always @code{nil}.
97
98If @var{human} is non-@code{nil}, the description is human-oriented.
99Otherwise the description is a Lisp expression---a call to
100@code{define-abbrev-table} which would define @var{name} exactly as it
101is currently defined.
102@end defun
103
104@node Defining Abbrevs, Abbrev Files, Abbrev Tables, Abbrevs
105@comment node-name, next, previous, up
106@section Defining Abbrevs
107
108 These functions define an abbrev in a specified abbrev table.
109@code{define-abbrev} is the low-level basic function, while
110@code{add-abbrev} is used by commands that ask for information from the
111user.
112
113@defun add-abbrev table type arg
114This function adds an abbreviation to abbrev table @var{table}. The
115argument @var{type} is a string describing in English the kind of abbrev
116this will be (typically, @code{"global"} or @code{"mode-specific"});
117this is used in prompting the user. The argument @var{arg} is the
118number of words in the expansion.
119
120The return value is the symbol which internally represents the new
121abbrev, or @code{nil} if the user declines to confirm redefining an
122existing abbrev.
123@end defun
124
125@defun define-abbrev table name expansion hook
126This function defines an abbrev in @var{table} named @var{name}, to
127expand to @var{expansion}, and call @var{hook}. The return value is an
128uninterned symbol which represents the abbrev inside Emacs; its name is
129@var{name}.
130
131The argument @var{name} should be a string. The argument
132@var{expansion} should be a string, or @code{nil}, to undefine the
133abbrev.
134
135The argument @var{hook} is a function or @code{nil}. If @var{hook} is
136non-@code{nil}, then it is called with no arguments after the abbrev is
137replaced with @var{expansion}; point is located at the end of
138@var{expansion}.
139
140The use count of the abbrev is initialized to zero.
141@end defun
142
143@defopt only-global-abbrevs
144If this variable is non-@code{nil}, it means that the user plans to use
145global abbrevs only. This tells the commands that define mode-specific
146abbrevs to define global ones instead. This variable does not alter the
147functioning of the functions in this section; it is examined by their
148callers.
149@end defopt
150
151@node Abbrev Files, Abbrev Expansion, Defining Abbrevs, Abbrevs
152@section Saving Abbrevs in Files
153
154 A file of saved abbrev definitions is actually a file of Lisp code.
155The abbrevs are saved in the form of a Lisp program to define the same
156abbrev tables with the same contents. Therefore, you can load the file
157with @code{load} (@pxref{How Programs Do Loading}). However, the
158function @code{quietly-read-abbrev-file} is provided as a more
159convenient interface.
160
161 User-level facilities such as @code{save-some-buffers} can save
162abbrevs in a file automatically, under the control of variables
163described here.
164
165@defopt abbrev-file-name
166This is the default file name for reading and saving abbrevs.
167@end defopt
168
169@defun quietly-read-abbrev-file filename
170This function reads abbrev definitions from a file named @var{filename},
171previously written with @code{write-abbrev-file}. If @var{filename} is
172@code{nil}, the file specified in @code{abbrev-file-name} is used.
173@code{save-abbrevs} is set to @code{t} so that changes will be saved.
174
175This function does not display any messages. It returns @code{nil}.
176@end defun
177
178@defopt save-abbrevs
179A non-@code{nil} value for @code{save-abbrev} means that Emacs should
180save abbrevs when files are saved. @code{abbrev-file-name} specifies
181the file to save the abbrevs in.
182@end defopt
183
184@defvar abbrevs-changed
185This variable is set non-@code{nil} by defining or altering any
186abbrevs. This serves as a flag for various Emacs commands to offer to
187save your abbrevs.
188@end defvar
189
190@deffn Command write-abbrev-file filename
191Save all abbrev definitions, in all abbrev tables, in the file
192@var{filename}, in the form of a Lisp program which when loaded will
193define the same abbrevs. This function returns @code{nil}.
194@end deffn
195
196@node Abbrev Expansion, Standard Abbrev Tables, Abbrev Files, Abbrevs
197@comment node-name, next, previous, up
198@section Looking Up and Expanding Abbreviations
199
200 Abbrevs are usually expanded by commands for interactive use,
201including @code{self-insert-command}. This section describes the
202subroutines used in writing such functions, as well as the variables
203they use for communication.
204
205@defun abbrev-symbol abbrev &optional table
206This function returns the symbol representing the abbrev named
207@var{abbrev}. The value returned is @code{nil} if that abbrev is not
208defined. The optional second argument @var{table} is the abbrev table
209to look it up in. If @var{table} is @code{nil}, this function tries
210first the current buffer's local abbrev table, and second the global
211abbrev table.
212@end defun
213
214@defopt abbrev-all-caps
215When this is set non-@code{nil}, an abbrev entered entirely in upper
216case is expanded using all upper case. Otherwise, an abbrev entered
217entirely in upper case is expanded by capitalizing each word of the
218expansion.
219@end defopt
220
221@defun abbrev-expansion abbrev &optional table
222This function returns the string that @var{abbrev} would expand into (as
223defined by the abbrev tables used for the current buffer). The optional
224argument @var{table} specifies the abbrev table to use; if it is
225specified, the abbrev is looked up in that table only.
226@end defun
227
228@defvar abbrev-start-location
229This is the buffer position for @code{expand-abbrev} to use as the start
230of the next abbrev to be expanded. (@code{nil} means use the word
231before point instead.) @code{abbrev-start-location} is set to
232@code{nil} each time @code{expand-abbrev} is called. This variable is
233also set by @code{abbrev-prefix-mark}.
234@end defvar
235
236@defvar abbrev-start-location-buffer
237The value of this variable is the buffer for which
238@code{abbrev-start-location} has been set. Trying to expand an abbrev
239in any other buffer clears @code{abbrev-start-location}. This variable
240is set by @code{abbrev-prefix-mark}.
241@end defvar
242
243@defvar last-abbrev
244This is the @code{abbrev-symbol} of the last abbrev expanded. This
245information is left by @code{expand-abbrev} for the sake of the
246@code{unexpand-abbrev} command.
247@end defvar
248
249@defvar last-abbrev-location
250This is the location of the last abbrev expanded. This contains
251information left by @code{expand-abbrev} for the sake of the
252@code{unexpand-abbrev} command.
253@end defvar
254
255@defvar last-abbrev-text
256This is the exact expansion text of the last abbrev expanded, as
257results from case conversion. Its value is
258@code{nil} if the abbrev has already been unexpanded. This
259contains information left by @code{expand-abbrev} for the sake of the
260@code{unexpand-abbrev} command.
261@end defvar
262
263@c Emacs 19 feature
264@defvar pre-abbrev-expand-hook
265This is a normal hook whose functions are executed, in sequence, just
266before any expansion of an abbrev. @xref{Hooks}. Since it is a normal
267hook, the hook functions receive no arguments. However, they can find
268the abbrev to be expanded by looking in the buffer before point.
269@end defvar
270
271 The following sample code shows a simple use of
272@code{pre-abbrev-expand-hook}. If the user terminates an abbrev with a
273punctuation character, the hook function asks for confirmation. Thus,
274this hook allows the user to decide whether to expand the abbrev, and
275aborts expansion if it is not confirmed.
276
277@smallexample
278(add-hook 'pre-abbrev-expand-hook 'query-if-not-space)
279
280;; @r{This is the function invoked by @code{pre-abbrev-expand-hook}.}
281
282;; @r{If the user terminated the abbrev with a space, the function does}
283;; @r{nothing (that is, it returns so that the abbrev can expand). If the}
284;; @r{user entered some other character, this function asks whether}
285;; @r{expansion should continue.}
286
287;; @r{If the user enters the prompt with @kbd{y}, the function returns}
288;; @r{@code{nil} (because of the @code{not} function), but that is}
289;; @r{acceptable; the return value has no effect on expansion.}
290
291(defun query-if-not-space ()
292 (if (/= ?\ (preceding-char))
293 (if (not (y-or-n-p "Do you want to expand this abbrev? "))
294 (error "Not expanding this abbrev"))))
295@end smallexample
296
297@node Standard Abbrev Tables, , Abbrev Expansion, Abbrevs
298@comment node-name, next, previous, up
299@section Standard Abbrev Tables
300
301 Here we list the variables that hold the abbrev tables for the
302preloaded major modes of Emacs.
303
304@defvar global-abbrev-table
305This is the abbrev table for mode-independent abbrevs. The abbrevs
306defined in it apply to all buffers. Each buffer may also have a local
307abbrev table, whose abbrev definitions take precedence over those in the
308global table.
309@end defvar
310
311@defvar local-abbrev-table
312The value of this buffer-local variable is the (mode-specific)
313abbreviation table of the current buffer.
314@end defvar
315
316@defvar fundamental-mode-abbrev-table
317This is the local abbrev table used in Fundamental mode. It is the
318local abbrev table in all buffers in Fundamental mode.
319@end defvar
320
321@defvar text-mode-abbrev-table
322This is the local abbrev table used in Text mode.
323@end defvar
324
325@defvar c-mode-abbrev-table
326This is the local abbrev table used in C mode.
327@end defvar
328
329@defvar lisp-mode-abbrev-table
330This is the local abbrev table used in Lisp mode and Emacs Lisp mode.
331@end defvar
diff --git a/lispref/positions.texi b/lispref/positions.texi
new file mode 100644
index 00000000000..28f52d719bd
--- /dev/null
+++ b/lispref/positions.texi
@@ -0,0 +1,896 @@
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/positions
6@node Positions, Markers, Frames, Top
7@chapter Positions
8@cindex position (in buffer)
9
10 A @dfn{position} is the index of a character in the text of buffer.
11More precisely, a position identifies the place between two characters
12(or before the first character, or after the last character), so we can
13speak of the character before or after a given position. However, the
14we often speak of the character ``at'' a position, meaning the character
15after that position.
16
17 Positions are usually represented as integers starting from 1, but can
18also be represented as @dfn{markers}---special objects which relocate
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
40than a particular character. Many terminals display the cursor over the
41character that immediately follows point; on such terminals, point is
42actually before the character on which the cursor sits.
43
44@cindex point with narrowing
45 The value of point is a number between 1 and the buffer size plus 1.
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
61 This function returns the position of point in the current buffer,
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
74 This function returns the minimum accessible value of point in the
75current buffer. This is 1, unless narrowing is in effect, in
76which case it is the position of the start of the region that you
77narrowed to. (@xref{Narrowing}.)
78@end defun
79
80@defun point-max
81 This function returns the maximum accessible value of point in the
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
88 This function returns @code{(point-min)} if @var{flag} is less than 1,
89@code{(point-max)} otherwise. The argument @var{flag} must be a number.
90@end defun
91
92@defun buffer-size
93 This function returns the total number of characters in the current
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
109@defvar buffer-saved-size
110 The value of this buffer-local variable is the former length of the
111current buffer, as of the last time it was read in, saved or auto-saved.
112@end defvar
113
114@node Motion
115@section Motion
116
117 Motion functions change the value of point, either relative to the
118current value of point, relative to the beginning or end of the buffer,
119or relative to the edges of the selected window. @xref{Point}.
120
121@menu
122* Character Motion:: Moving in terms of characters.
123* Word Motion:: Moving in terms of words.
124* Buffer End Motion:: Moving to the beginning or end of the buffer.
125* Text Lines:: Moving in terms of lines of text.
126* Screen Lines:: Moving in terms of lines as displayed.
127* Vertical Motion:: Implementation of @code{next-line} and
128 @code{previous-line}.
129* List Motion:: Moving by parsing lists and sexps.
130* Skipping Characters:: Skipping characters belonging to a certain set.
131@end menu
132
133@node Character Motion
134@subsection Motion by Characters
135
136 These functions move point based on a count of characters.
137@code{goto-char} is the fundamental primitive; the functions others use
138that.
139
140@deffn Command goto-char position
141This function sets point in the current buffer to the value
142@var{position}. If @var{position} is less than 1, it moves point to the
143beginning of the buffer. If @var{position} is greater than the length
144of the buffer, it moves point to the end.
145
146If narrowing is in effect, @var{position} still counts from the
147beginning of the buffer, but point cannot go outside the accessible
148portion. If @var{position} is out of range, @code{goto-char} moves
149point to the beginning or the end of the accessible portion.
150
151When this function is called interactively, @var{position} is the
152numeric prefix argument, if provided; otherwise it is read from the
153minibuffer.
154
155@code{goto-char} returns @var{position}.
156@end deffn
157
158@deffn Command forward-char &optional count
159@c @kindex beginning-of-buffer
160@c @kindex end-of-buffer
161This function moves point @var{count} characters forward, towards the
162end of the buffer (or backward, towards the beginning of the buffer, if
163@var{count} is negative). If the function attempts to move point past
164the beginning or end of the buffer (or the limits of the accessible
165portion, when narrowing is in effect), an error is signaled with error
166code @code{beginning-of-buffer} or @code{end-of-buffer}.
167
168In an interactive call, @var{count} is the numeric prefix argument.
169@end deffn
170
171@deffn Command backward-char &optional count
172This function moves point @var{count} characters backward, towards the
173beginning of the buffer (or forward, towards the end of the buffer, if
174@var{count} is negative). If the function attempts to move point past
175the beginning or end of the buffer (or the limits of the accessible
176portion, when narrowing is in effect), an error is signaled with error
177code @code{beginning-of-buffer} or @code{end-of-buffer}.
178
179In an interactive call, @var{count} is the numeric prefix argument.
180@end deffn
181
182@node Word Motion
183@subsection Motion by Words
184
185 These functions for parsing words use the syntax table to decide
186whether a given character is part of a word. @xref{Syntax Tables}.
187
188@deffn Command forward-word count
189This function moves point forward @var{count} words (or backward if
190@var{count} is negative). Normally it returns @code{t}. If this motion
191encounters the beginning or end of the buffer, or the limits of the
192accessible portion when narrowing is in effect, point stops there
193and the value is @code{nil}.
194
195In an interactive call, @var{count} is set to the numeric prefix
196argument.
197@end deffn
198
199@deffn Command backward-word count
200This function just like @code{forward-word}, except that it moves
201backward until encountering the front of a word, rather than forward.
202
203In an interactive call, @var{count} is set to the numeric prefix
204argument.
205
206This function is rarely used in programs, as it is more efficient to
207call @code{forward-word} with negative argument.
208@end deffn
209
210@defvar words-include-escapes
211@c Emacs 19 feature
212This variable affects the behavior of @code{forward-word} and everything
213that uses it. If it is non-@code{nil}, then characters in the
214``escape'' and ``character quote'' syntax classes count as part of
215words. Otherwise, they do not.
216@end defvar
217
218@node Buffer End Motion
219@subsection Motion to an End of the Buffer
220
221 To move point to the beginning of the buffer, write:
222
223@example
224@group
225(goto-char (point-min))
226@end group
227@end example
228
229@noindent
230Likewise, to move to the end of the buffer, use:
231
232@example
233@group
234(goto-char (point-max))
235@end group
236@end example
237
238 Here are two commands which users use to do these things. They are
239documented here to warn you not to use them in Lisp programs, because
240they set the mark and display messages in the echo area.
241
242@deffn Command beginning-of-buffer &optional n
243This function moves point to the beginning of the buffer (or the limits
244of the accessible portion, when narrowing is in effect), setting the
245mark at the previous position. If @var{n} is non-@code{nil}, then it
246puts point @var{n} tenths of the way from the beginning of the buffer.
247
248In an interactive call, @var{n} is the numeric prefix argument,
249if provided; otherwise @var{n} defaults to @code{nil}.
250
251Don't use this function in Lisp programs!
252@end deffn
253
254@deffn Command end-of-buffer &optional n
255This function moves point to the end of the buffer (or the limits of
256the accessible portion, when narrowing is in effect), setting the mark
257at the previous position. If @var{n} is non-@code{nil}, then it puts
258point @var{n} tenths of the way from the end.
259
260In an interactive call, @var{n} is the numeric prefix argument,
261if provided; otherwise @var{n} defaults to @code{nil}.
262
263Don't use this function in Lisp programs!
264@end deffn
265
266@node Text Lines
267@subsection Motion by Text Lines
268@cindex lines
269
270 Text lines are portions of the buffer delimited by newline characters,
271which are regarded as part of the previous line. The first text line
272begins at the beginning of the buffer, and the last text line ends at
273the end of the buffer whether or not the last character is a newline.
274The division of the buffer into text lines is not affected by the width
275of the window, by line continuation in display, or by how tabs and
276control characters are displayed.
277
278@deffn Command goto-line line
279This function moves point to the front of the @var{line}th line,
280counting from line 1 at beginning of buffer. If @var{line} is less than
2811, it moves point to the beginning of the buffer. If @var{line} is
282greater than the number of lines in the buffer, it moves point to the
283@emph{end of the last line} of the buffer.
284
285If narrowing is in effect, then @var{line} still counts from the
286beginning of the buffer, but point cannot go outside the accessible
287portion. So @code{goto-line} moves point to the beginning or end of the
288accessible portion, if the line number specifies an inaccessible
289position.
290
291The return value of @code{goto-line} is the difference between
292@var{line} and the line number of the line to which point actually was
293able move (in the full buffer, disregarding any narrowing). Thus, the
294value is positive if the scan encounters the real end of the buffer.
295
296In an interactive call, @var{line} is the numeric prefix argument if
297one has been provided. Otherwise @var{line} is read in the minibuffer.
298@end deffn
299
300@deffn Command beginning-of-line &optional count
301This function moves point to the beginning of the current line. With an
302argument @var{count} not @code{nil} or 1, it moves forward
303@var{count}@minus{}1 lines and then to the beginning of the line.
304
305If this function reaches the end of the buffer (or of the accessible
306portion, if narrowing is in effect), it positions point at the end of
307the buffer. No error is signaled.
308@end deffn
309
310@deffn Command end-of-line &optional count
311This function moves point to the end of the current line. With an
312argument @var{count} not @code{nil} or 1, it moves forward
313@var{count}@minus{}1 lines and then to the end of the line.
314
315If this function reaches the end of the buffer (or of the accessible
316portion, if narrowing is in effect), it positions point at the end of
317the buffer. No error is signaled.
318@end deffn
319
320@deffn Command forward-line &optional count
321@cindex beginning of line
322This function moves point forward @var{count} lines, to the beginning of
323the line. If @var{count} is negative, it moves point
324@minus{}@var{count} lines backward, to the beginning of the line.
325
326If @code{forward-line} encounters the beginning or end of the buffer (or
327of the accessible portion) before finding that many lines, it sets point
328there. No error is signaled.
329
330@code{forward-line} returns the difference between @var{count} and the
331number of lines actually moved. If you attempt to move down five lines
332from the beginning of a buffer that has only three lines, point stops at
333the end of the last line, and the value will be 2.
334
335In an interactive call, @var{count} is the numeric prefix argument.
336@end deffn
337
338@defun count-lines start end
339@cindex lines in region
340This function returns the number of lines between the positions
341@var{start} and @var{end} in the current buffer. If @var{start} and
342@var{end} are equal, then it returns 0. Otherwise it returns at least
3431, even if @var{start} and @var{end} are on the same line. This is
344because the text between them, considered in isolation, must contain at
345least one line unless it is empty.
346
347Here is an example of using @code{count-lines}:
348
349@example
350@group
351(defun current-line ()
352 "Return the vertical position of point@dots{}"
353 (+ (count-lines (window-start) (point))
354 (if (= (current-column) 0) 1 0)
355 -1))
356@end group
357@end example
358@end defun
359
360@ignore
361@c ================
362The @code{previous-line} and @code{next-line} commands are functions
363that should not be used in programs. They are for users and are
364mentioned here only for completeness.
365
366@deffn Command previous-line count
367@cindex goal column
368This function moves point up @var{count} lines (down if @var{count}
369is negative). In moving, it attempts to keep point in the ``goal column''
370(normally the same column that it was at the beginning of the move).
371
372If there is no character in the target line exactly under the current
373column, point is positioned after the character in that line which
374spans this column, or at the end of the line if it is not long enough.
375
376If it attempts to move beyond the top or bottom of the buffer (or clipped
377region), then point is positioned in the goal column in the top or
378bottom line. No error is signaled.
379
380In an interactive call, @var{count} will be the numeric
381prefix argument.
382
383The command @code{set-goal-column} can be used to create a semipermanent
384goal column to which this command always moves. Then it does not try to
385move vertically.
386
387If you are thinking of using this in a Lisp program, consider using
388@code{forward-line} with a negative argument instead. It is usually easier
389to use and more reliable (no dependence on goal column, etc.).
390@end deffn
391
392@deffn Command next-line count
393This function moves point down @var{count} lines (up if @var{count}
394is negative). In moving, it attempts to keep point in the ``goal column''
395(normally the same column that it was at the beginning of the move).
396
397If there is no character in the target line exactly under the current
398column, point is positioned after the character in that line which
399spans this column, or at the end of the line if it is not long enough.
400
401If it attempts to move beyond the top or bottom of the buffer (or clipped
402region), then point is positioned in the goal column in the top or
403bottom line. No error is signaled.
404
405In the case where the @var{count} is 1, and point is on the last
406line of the buffer (or clipped region), a new empty line is inserted at the
407end of the buffer (or clipped region) and point moved there.
408
409In an interactive call, @var{count} will be the numeric
410prefix argument.
411
412The command @code{set-goal-column} can be used to create a semipermanent
413goal column to which this command always moves. Then it does not try to
414move vertically.
415
416If you are thinking of using this in a Lisp program, consider using
417@code{forward-line} instead. It is usually easier
418to use and more reliable (no dependence on goal column, etc.).
419@end deffn
420
421@c ================
422@end ignore
423
424 Also see the functions @code{bolp} and @code{eolp} in @ref{Near Point}.
425These functions do not move point, but test whether it is already at the
426beginning or end of a line.
427
428@node Screen Lines
429@subsection Motion by Screen Lines
430
431 The line functions in the previous section count text lines, delimited
432only by newline characters. By contrast, these functions count screen
433lines, which are defined by the way the text appears on the screen. A
434text line is a single screen line if it is short enough to fit the width
435of the selected window, but otherwise it may occupy several screen
436lines.
437
438 In some cases, text lines are truncated on the screen rather than
439continued onto additional screen lines. In these cases,
440@code{vertical-motion} moves point much like @code{forward-line}.
441@xref{Truncation}.
442
443 Because the width of a given string depends on the flags which control
444the appearance of certain characters, @code{vertical-motion} behaves
445differently, for a given piece of text, depending on the buffer it is
446in, and even on the selected window (because the width, the truncation
447flag, and display table may vary between windows). @xref{Usual
448Display}.
449
450@defun vertical-motion count
451This function moves point to the start of the screen line @var{count}
452screen lines down from the screen line containing point. If @var{count}
453is negative, it moves up instead.
454
455This function returns the number of lines moved. The value may be less
456in absolute value than @var{count} if the beginning or end of the buffer
457was reached.
458@end defun
459
460@deffn Command move-to-window-line count
461This function moves point with respect to the text currently displayed
462in the selected window. It moves point to the beginning of the screen
463line @var{count} screen lines from the top of the window. If
464@var{count} is negative, that specifies a position
465@w{@minus{}@var{count}} lines from the bottom---or else the last line of
466the buffer, if the buffer ends above the specified screen position.
467
468If @var{count} is @code{nil}, then point moves to the beginning of the
469line in the middle of the window. If the absolute value of @var{count}
470is greater than the size of the window, then point moves to the place
471which would appear on that screen line if the window were tall enough.
472This will probably cause the next redisplay to scroll to bring that
473location onto the screen.
474
475In an interactive call, @var{count} is the numeric prefix argument.
476
477The value returned is the window line number, with the top line in the
478window numbered 0.
479@end deffn
480
481@defun compute-motion from frompos to topos width offsets
482This function scan through the current buffer, calculating screen
483position. It scans the current buffer forward from position @var{from},
484assuming that is at screen coordinates @var{frompos}, to position
485@var{to} or coordinates @var{topos}, whichever comes first. It returns
486the ending buffer position and screen coordinates.
487
488The coordinate arguments @var{frompos} and @var{topos} are cons cells of
489the form @code{(@var{hpos} . @var{vpos})}.
490
491The argument @var{width} is the number of columns available to display
492text; this affects handling of continuation lines. Use the value
493returned by @code{window-width} for the window of your choice.
494
495The argument @var{offsets} is either @code{nil} or a cons cell of the
496form @code{(@var{hscroll} . @var{tab-offset})}. Here @var{hscroll} is
497the number of columns not being displayed at the left margin; in most
498calls, this comes from @code{window-hscroll}. Meanwhile,
499@var{tab-offset} is the number of columns of an initial tab character
500(at @var{from}) that aren't included in the display, perhaps because the
501line was continued within that character.
502
503The return value is a list of five elements:
504
505@example
506(@var{pos} @var{vpos} @var{hpos} @var{prevhpos} @var{contin})
507@end example
508
509@noindent
510Here @var{pos} is the buffer position where the scan stopped, @var{vpos}
511is the vertical position, and @var{hpos} is the horizontal position.
512
513The result @var{prevhpos} is the horizontal position one character back
514from @var{pos}. The result @var{contin} is @code{t} if a line was
515continued after (or within) the previous character.
516
517For example, to find the buffer position of column @var{col} of line
518@var{line} of a certain window, pass the window's display start location
519as @var{from} and the window's upper-left coordinates as @var{frompos}.
520Pass the buffer's @code{(point-max)} as @var{to}, to limit the scan to
521the end of the visible section of the buffer, and pass @var{line} and
522@var{col} as @var{topos}. Here's a function that does this:
523
524@example
525(defun coordinates-of-position (col line)
526 (car (compute-motion (window-start)
527 '(0 . 0)
528 (point)
529 (cons col line)
530 (window-width)
531 (cons (window-hscroll) 0))))
532@end example
533@end defun
534
535@node Vertical Motion
536@comment node-name, next, previous, up
537@subsection The User-Level Vertical Motion Commands
538@cindex goal column
539@cindex vertical text line motion
540@findex next-line
541@findex previous-line
542
543 A goal column is useful if you want to edit text such as a table in
544which you want to move point to a certain column on each line. The goal
545column affects the vertical text line motion commands, @code{next-line}
546and @code{previous-line}. @xref{Basic,, Basic Editing Commands, emacs,
547The GNU Emacs Manual}.
548
549@defopt goal-column
550This variable holds an explicitly specified goal column for vertical
551line motion commands. If it is an integer, it specifies a column, and
552these commands try to move to that column on each line. If it is
553@code{nil}, then the commands set their own goal columns. Any other
554value is invalid.
555@end defopt
556
557@defvar temporary-goal-column
558This variable holds the temporary goal column during a sequence of
559consecutive vertical line motion commands. It is overridden by
560@code{goal-column} if that is non-@code{nil}. It is set each time a
561vertical motion command is invoked, unless the previous command was also
562a vertical motion command.
563@end defvar
564
565@defopt track-eol
566This variable controls how the vertical line motion commands operate
567when starting at the end of a line. If @code{track-eol} is
568non-@code{nil}, then vertical motion starting at the end of a line will
569keep to the ends of lines. This means moving to the end of each line
570moved onto. The value of @code{track-eol} has no effect if point is not
571at the end of a line when the first vertical motion command is given.
572
573@code{track-eol} has its effect by telling line motion commands to set
574@code{temporary-goal-column} to 9999 instead of to the current column.
575@end defopt
576
577@node List Motion
578@comment node-name, next, previous, up
579@subsection Moving over Balanced Expressions
580@cindex sexp motion
581@cindex Lisp expression motion
582@cindex list motion
583
584 Here are several functions concerned with balanced-parenthesis
585expressions (also called @dfn{sexps} in connection with moving across
586them in Emacs). The syntax table controls how these functions interpret
587various characters; see @ref{Syntax Tables}. @xref{Parsing
588Expressions}, for lower-level primitives for scanning sexps or parts of
589sexps. For user-level commands, see @ref{Lists and Sexps,,, emacs, GNU
590Emacs Manual}.
591
592@deffn Command forward-list arg
593Move forward across @var{arg} balanced groups of parentheses.
594(Other syntactic entities such as words or paired string quotes
595are ignored.)
596@end deffn
597
598@deffn Command backward-list arg
599Move backward across @var{arg} balanced groups of parentheses.
600(Other syntactic entities such as words or paired string quotes
601are ignored.)
602@end deffn
603
604@deffn Command up-list arg
605Move forward out of @var{arg} levels of parentheses.
606A negative argument means move backward but still to a less deep spot.
607@end deffn
608
609@deffn Command down-list arg
610Move forward down @var{arg} levels of parentheses. A negative argument
611means move backward but still go down @var{arg} levels.
612@end deffn
613
614@deffn Command forward-sexp arg
615Move forward across @var{arg} balanced expressions.
616Balanced expressions include both those delimited by parentheses
617and other kinds, such as words and string constants. For example,
618
619@example
620@group
621---------- Buffer: foo ----------
622(concat@point{} "foo " (car x) y z)
623---------- Buffer: foo ----------
624@end group
625
626@group
627(forward-sexp 3)
628 @result{} nil
629
630---------- Buffer: foo ----------
631(concat "foo " (car x) y@point{} z)
632---------- Buffer: foo ----------
633@end group
634@end example
635@end deffn
636
637@deffn Command backward-sexp arg
638Move backward across @var{arg} balanced expressions.
639@end deffn
640
641@node Skipping Characters
642@comment node-name, next, previous, up
643@subsection Skipping Characters
644@cindex skipping characters
645
646 The following two functions move point over a specified set of
647characters. For example, they are often used to skip whitespace. For
648related functions, see @ref{Motion and Syntax}.
649
650@defun skip-chars-forward character-set &optional limit
651This function moves point in the current buffer forward, skipping over a
652given set of characters. It examines the character following point,
653then advances point if the character matches @var{character-set}. This
654continues until it reaches a character that does not match. The
655function returns @code{nil}.
656
657The argument @var{character-set} is like the inside of a
658@samp{[@dots{}]} in a regular expression except that @samp{]} is never
659special and @samp{\} quotes @samp{^}, @samp{-} or @samp{\}. Thus,
660@code{"a-zA-Z"} skips over all letters, stopping before the first
661nonletter, and @code{"^a-zA-Z}" skips nonletters stopping before the
662first letter. @xref{Regular Expressions}.
663
664If @var{limit} is supplied (it must be a number or a marker), it
665specifies the maximum position in the buffer that point can be skipped
666to. Point will stop at or before @var{limit}.
667
668In the following example, point is initially located directly before the
669@samp{T}. After the form is evaluated, point is located at the end of
670that line (between the @samp{t} of @samp{hat} and the newline). The
671function skips all letters and spaces, but not newlines.
672
673@example
674@group
675---------- Buffer: foo ----------
676I read "@point{}The cat in the hat
677comes back" twice.
678---------- Buffer: foo ----------
679@end group
680
681@group
682(skip-chars-forward "a-zA-Z ")
683 @result{} nil
684
685---------- Buffer: foo ----------
686I read "The cat in the hat@point{}
687comes back" twice.
688---------- Buffer: foo ----------
689@end group
690@end example
691@end defun
692
693@defun skip-chars-backward character-set &optional limit
694This function moves point backward, skipping characters that match
695@var{character-set}, until @var{limit}. It just like
696@code{skip-chars-forward} except for the direction of motion.
697@end defun
698
699@node Excursions
700@section Excursions
701@cindex excursion
702
703 It is often useful to move point ``temporarily'' within a localized
704portion of the program, or to switch buffers temporarily. This is
705called an @dfn{excursion}, and it is done with the @code{save-excursion}
706special form. This construct saves the current buffer and its values of
707point and the mark so they can be restored after the completion of the
708excursion.
709
710 The forms for saving and restoring the configuration of windows are
711described elsewhere (see @ref{Window Configurations}, and @pxref{Frame
712Configurations}).
713
714@defspec save-excursion forms@dots{}
715@cindex mark excursion
716@cindex point excursion
717@cindex current buffer excursion
718The @code{save-excursion} special form saves the identity of the current
719buffer and the values of point and the mark in it, evaluates @var{forms},
720and finally restores the buffer and its saved values of point and the mark.
721All three saved values are restored even in case of an abnormal exit
722via throw or error (@pxref{Nonlocal Exits}).
723
724The @code{save-excursion} special form is the standard way to switch
725buffers or move point within one part of a program and avoid affecting
726the rest of the program. It is used more than 500 times in the Lisp
727sources of Emacs.
728
729@code{save-excursion} does not save the values of point and the mark for
730other buffers, so changes in other buffers remain in effect after
731@code{save-excursion} exits.
732
733@cindex window excursions
734Likewise, @code{save-excursion} does not restore window-buffer
735correspondences altered by functions such as @code{switch-to-buffer}.
736One way to restore these correspondences, and the selected window, is to
737use @code{save-window-excursion} inside @code{save-excursion}
738(@pxref{Window Configurations}).
739
740The value returned by @code{save-excursion} is the result of the last of
741@var{forms}, or @code{nil} if no @var{forms} are given.
742
743@example
744@group
745(save-excursion
746 @var{forms})
747@equiv{}
748(let ((old-buf (current-buffer))
749 (old-pnt (point-marker))
750 (old-mark (copy-marker (mark-marker))))
751 (unwind-protect
752 (progn @var{forms})
753 (set-buffer old-buf)
754 (goto-char old-pnt)
755 (set-marker (mark-marker) old-mark)))
756@end group
757@end example
758@end defspec
759
760@node Narrowing
761@section Narrowing
762@cindex narrowing
763@cindex restriction (in a buffer)
764@cindex accessible portion (of a buffer)
765
766 @dfn{Narrowing} means limiting the text addressable by Emacs editing
767commands to a limited range of characters in a buffer. The text that
768remains addressable is called the @dfn{accessible portion} of the
769buffer.
770
771 Narrowing is specified with two buffer positions which become the
772beginning and end of the accessible portion. For most editing commands
773and most Emacs primitives, these positions replace the values of the
774beginning and end of the buffer. While narrowing is in effect, no text
775outside the accessible portion is displayed, and point cannot move
776outside the accessible portion.
777
778 Values such as positions or line numbers, that usually count from the
779beginning of the buffer, do so despite narrowing, but the functions
780which use them refuse to operate on text that is inaccessible.
781
782 The commands for saving buffers are unaffected by narrowing; they save
783the entire buffer regardless of the any narrowing.
784
785@deffn Command narrow-to-region start end
786This function sets the accessible portion of the current buffer to start
787at @var{start} and end at @var{end}. Both arguments should be character
788positions.
789
790In an interactive call, @var{start} and @var{end} are set to the bounds
791of the current region (point and the mark, with the smallest first).
792@end deffn
793
794@deffn Command narrow-to-page move-count
795This function sets the accessible portion of the current buffer to
796include just the current page. An optional first argument
797@var{move-count} non-@code{nil} means to move forward or backward by
798@var{move-count} pages and then narrow. The variable
799@code{page-delimiter} specifies where pages start and end
800(@pxref{Standard Regexps}).
801
802In an interactive call, @var{move-count} is set to the numeric prefix
803argument.
804@end deffn
805
806@deffn Command widen
807@cindex widening
808This function cancels any narrowing in the current buffer, so that the
809entire contents are accessible. This is called @dfn{widening}.
810It is equivalent to the following expression:
811
812@example
813(narrow-to-region 1 (1+ (buffer-size)))
814@end example
815@end deffn
816
817@defspec save-restriction body@dots{}
818This special form saves the current bounds of the accessible portion,
819evaluates the @var{body} forms, and finally restores the saved bounds,
820thus restoring the same state of narrowing (or absence thereof) formerly
821in effect. The state of narrowing is restored even in the event of an
822abnormal exit via throw or error (@pxref{Nonlocal Exits}). Therefore,
823this construct is a clean way to narrow a buffer temporarily.
824
825The value returned by @code{save-restriction} is that returned by the
826last form in @var{body}, or @code{nil} if no body forms were given.
827
828@c Wordy to avoid overfull hbox. --rjc 16mar92
829@strong{Caution:} it is easy to make a mistake when using the
830@code{save-restriction} construct. Read the entire description here
831before you try it.
832
833If @var{body} changes the current buffer, @code{save-restriction} still
834restores the restrictions on the original buffer (the buffer whose
835restructions it saved from), but it does not restore the identity of the
836current buffer.
837
838@code{save-restriction} does @emph{not} restore point and the mark; use
839@code{save-excursion} for that. If you use both @code{save-restriction}
840and @code{save-excursion} together, @code{save-excursion} should come
841first (on the outside). Otherwise, the old point value would be
842restored with temporary narrowing still in effect. If the old point
843value were outside the limits of the temporary narrowing, this would
844fail to restore it accurately.
845
846The @code{save-restriction} special form records the values of the
847beginning and end of the accessible portion as distances from the
848beginning and end of the buffer. In other words, it records the amount
849of inaccessible text before and after the accessible portion.
850
851This method yields correct results if @var{body} does further narrowing.
852However, @code{save-restriction} can become confused if the body widens
853and then make changes outside the range of the saved narrowing. When
854this is what you want to do, @code{save-restriction} is not the right
855tool for the job. Here is what you must use instead:
856
857@example
858@group
859(let ((beg (point-min-marker))
860 (end (point-max-marker)))
861 (unwind-protect
862 (progn @var{body})
863 (save-excursion
864 (set-buffer (marker-buffer beg))
865 (narrow-to-region beg end))))
866@end group
867@end example
868
869Here is a simple example of correct use of @code{save-restriction}:
870
871@example
872@group
873---------- Buffer: foo ----------
874This is the contents of foo
875This is the contents of foo
876This is the contents of foo@point{}
877---------- Buffer: foo ----------
878@end group
879
880@group
881(save-excursion
882 (save-restriction
883 (goto-char 1)
884 (forward-line 2)
885 (narrow-to-region 1 (point))
886 (goto-char (point-min))
887 (replace-string "foo" "bar")))
888
889---------- Buffer: foo ----------
890This is the contents of bar
891This is the contents of bar
892This is the contents of foo@point{}
893---------- Buffer: foo ----------
894@end group
895@end example
896@end defspec
diff --git a/lispref/searching.texi b/lispref/searching.texi
new file mode 100644
index 00000000000..28625c25bdb
--- /dev/null
+++ b/lispref/searching.texi
@@ -0,0 +1,1254 @@
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/searching
6@node Searching and Matching, Syntax Tables, Text, Top
7@chapter Searching and Matching
8@cindex searching
9
10 GNU Emacs provides two ways to search through a buffer for specified
11text: exact string searches and regular expression searches. After a
12regular expression search, you can examine the @dfn{match data} to
13determine which text matched the whole regular expression or various
14portions of it.
15
16@menu
17* String Search:: Search for an exact match.
18* Regular Expressions:: Describing classes of strings.
19* Regexp Search:: Searching for a match for a regexp.
20* Search and Replace:: Internals of @code{query-replace}.
21* Match Data:: Finding out which part of the text matched
22 various parts of a regexp, after regexp search.
23* Searching and Case:: Case-independent or case-significant searching.
24* Standard Regexps:: Useful regexps for finding sentences, pages,...
25@end menu
26
27 The @samp{skip-chars@dots{}} functions also perform a kind of searching.
28@xref{Skipping Characters}.
29
30@node String Search
31@section Searching for Strings
32@cindex string search
33
34 These are the primitive functions for searching through the text in a
35buffer. They are meant for use in programs, but you may call them
36interactively. If you do so, they prompt for the search string;
37@var{limit} and @var{noerror} are set to @code{nil}, and @var{repeat}
38is set to 1.
39
40@deffn Command search-forward string &optional limit noerror repeat
41 This function searches forward from point for an exact match for
42@var{string}. If successful, it sets point to the end of the occurrence
43found, and returns the new value of point. If no match is found, the
44value and side effects depend on @var{noerror} (see below).
45@c Emacs 19 feature
46
47 In the following example, point is initially at the beginning of the
48line. Then @code{(search-forward "fox")} moves point after the last
49letter of @samp{fox}:
50
51@example
52@group
53---------- Buffer: foo ----------
54@point{}The quick brown fox jumped over the lazy dog.
55---------- Buffer: foo ----------
56@end group
57
58@group
59(search-forward "fox")
60 @result{} 20
61
62---------- Buffer: foo ----------
63The quick brown fox@point{} jumped over the lazy dog.
64---------- Buffer: foo ----------
65@end group
66@end example
67
68 The argument @var{limit} specifies the upper bound to the search. (It
69must be a position in the current buffer.) No match extending after
70that position is accepted. If @var{limit} is omitted or @code{nil}, it
71defaults to the end of the accessible portion of the buffer.
72
73@kindex search-failed
74 What happens when the search fails depends on the value of
75@var{noerror}. If @var{noerror} is @code{nil}, a @code{search-failed}
76error is signaled. If @var{noerror} is @code{t}, @code{search-forward}
77returns @code{nil} and does nothing. If @var{noerror} is neither
78@code{nil} nor @code{t}, then @code{search-forward} moves point to the
79upper bound and returns @code{nil}. (It would be more consistent now
80to return the new position of point in that case, but some programs
81may depend on a value of @code{nil}.)
82
83 If @var{repeat} is non-@code{nil}, then the search is repeated that
84many times. Point is positioned at the end of the last match.
85@end deffn
86
87@deffn Command search-backward string &optional limit noerror repeat
88This function searches backward from point for @var{string}. It is
89just like @code{search-forward} except that it searches backwards and
90leaves point at the beginning of the match.
91@end deffn
92
93@deffn Command word-search-forward string &optional limit noerror repeat
94@cindex word search
95This function searches forward from point for a ``word'' match for
96@var{string}. If it finds a match, it sets point to the end of the
97match found, and returns the new value of point.
98@c Emacs 19 feature
99
100Word matching regards @var{string} as a sequence of words, disregarding
101punctuation that separates them. It searches the buffer for the same
102sequence of words. Each word must be distinct in the buffer (searching
103for the word @samp{ball} does not match the word @samp{balls}), but the
104details of punctuation and spacing are ignored (searching for @samp{ball
105boy} does match @samp{ball. Boy!}).
106
107In this example, point is initially at the beginning of the buffer; the
108search leaves it between the @samp{y} and the @samp{!}.
109
110@example
111@group
112---------- Buffer: foo ----------
113@point{}He said "Please! Find
114the ball boy!"
115---------- Buffer: foo ----------
116@end group
117
118@group
119(word-search-forward "Please find the ball, boy.")
120 @result{} 35
121
122---------- Buffer: foo ----------
123He said "Please! Find
124the ball boy@point{}!"
125---------- Buffer: foo ----------
126@end group
127@end example
128
129If @var{limit} is non-@code{nil} (it must be a position in the current
130buffer), then it is the upper bound to the search. The match found must
131not extend after that position.
132
133If @var{noerror} is @code{nil}, then @code{word-search-forward} signals
134an error if the search fails. If @var{noerror} is @code{t}, then it
135returns @code{nil} instead of signaling an error. If @var{noerror} is
136neither @code{nil} nor @code{t}, it moves point to @var{limit} (or the
137end of the buffer) and returns @code{nil}.
138
139If @var{repeat} is non-@code{nil}, then the search is repeated that many
140times. Point is positioned at the end of the last match.
141@end deffn
142
143@deffn Command word-search-backward string &optional limit noerror repeat
144This function searches backward from point for a word match to
145@var{string}. This function is just like @code{word-search-forward}
146except that it searches backward and normally leaves point at the
147beginning of the match.
148@end deffn
149
150@node Regular Expressions
151@section Regular Expressions
152@cindex regular expression
153@cindex regexp
154
155 A @dfn{regular expression} (@dfn{regexp}, for short) is a pattern that
156denotes a (possibly infinite) set of strings. Searching for matches for
157a regexp is a very powerful operation. This section explains how to write
158regexps; the following section says how to search for them.
159
160@menu
161* Syntax of Regexps:: Rules for writing regular expressions.
162* Regexp Example:: Illustrates regular expression syntax.
163@end menu
164
165@node Syntax of Regexps
166@subsection Syntax of Regular Expressions
167
168 Regular expressions have a syntax in which a few characters are special
169constructs and the rest are @dfn{ordinary}. An ordinary character is a
170simple regular expression which matches that character and nothing else.
171The special characters are @samp{$}, @samp{^}, @samp{.}, @samp{*},
172@samp{+}, @samp{?}, @samp{[}, @samp{]} and @samp{\}; no new special
173characters will be defined in the future. Any other character appearing
174in a regular expression is ordinary, unless a @samp{\} precedes it.
175
176For example, @samp{f} is not a special character, so it is ordinary, and
177therefore @samp{f} is a regular expression that matches the string
178@samp{f} and no other string. (It does @emph{not} match the string
179@samp{ff}.) Likewise, @samp{o} is a regular expression that matches
180only @samp{o}.@refill
181
182Any two regular expressions @var{a} and @var{b} can be concatenated. The
183result is a regular expression which matches a string if @var{a} matches
184some amount of the beginning of that string and @var{b} matches the rest of
185the string.@refill
186
187As a simple example, we can concatenate the regular expressions @samp{f}
188and @samp{o} to get the regular expression @samp{fo}, which matches only
189the string @samp{fo}. Still trivial. To do something more powerful, you
190need to use one of the special characters. Here is a list of them:
191
192@need 1200
193@table @kbd
194@item .@: @r{(Period)}
195@cindex @samp{.} in regexp
196is a special character that matches any single character except a newline.
197Using concatenation, we can make regular expressions like @samp{a.b}, which
198matches any three-character string that begins with @samp{a} and ends with
199@samp{b}.@refill
200
201@item *
202@cindex @samp{*} in regexp
203is not a construct by itself; it is a suffix operator that means to
204repeat the preceding regular expression as many times as possible. In
205@samp{fo*}, the @samp{*} applies to the @samp{o}, so @samp{fo*} matches
206one @samp{f} followed by any number of @samp{o}s. The case of zero
207@samp{o}s is allowed: @samp{fo*} does match @samp{f}.@refill
208
209@samp{*} always applies to the @emph{smallest} possible preceding
210expression. Thus, @samp{fo*} has a repeating @samp{o}, not a
211repeating @samp{fo}.@refill
212
213The matcher processes a @samp{*} construct by matching, immediately,
214as many repetitions as can be found. Then it continues with the rest
215of the pattern. If that fails, backtracking occurs, discarding some
216of the matches of the @samp{*}-modified construct in case that makes
217it possible to match the rest of the pattern. For example, in matching
218@samp{ca*ar} against the string @samp{caaar}, the @samp{a*} first
219tries to match all three @samp{a}s; but the rest of the pattern is
220@samp{ar} and there is only @samp{r} left to match, so this try fails.
221The next alternative is for @samp{a*} to match only two @samp{a}s.
222With this choice, the rest of the regexp matches successfully.@refill
223
224@item +
225@cindex @samp{+} in regexp
226is a suffix operator similar to @samp{*} except that the preceding
227expression must match at least once. So, for example, @samp{ca+r}
228matches the strings @samp{car} and @samp{caaaar} but not the string
229@samp{cr}, whereas @samp{ca*r} matches all three strings.
230
231@item ?
232@cindex @samp{?} in regexp
233is a suffix operator similar to @samp{*} except that the preceding
234expression can match either once or not at all. For example,
235@samp{ca?r} matches @samp{car} or @samp{cr}, but does not match anyhing
236else.
237
238@item [ @dots{} ]
239@cindex character set (in regexp)
240@cindex @samp{[} in regexp
241@cindex @samp{]} in regexp
242@samp{[} begins a @dfn{character set}, which is terminated by a
243@samp{]}. In the simplest case, the characters between the two brackets
244form the set. Thus, @samp{[ad]} matches either one @samp{a} or one
245@samp{d}, and @samp{[ad]*} matches any string composed of just @samp{a}s
246and @samp{d}s (including the empty string), from which it follows that
247@samp{c[ad]*r} matches @samp{cr}, @samp{car}, @samp{cdr},
248@samp{caddaar}, etc.@refill
249
250The usual regular expression special characters are not special inside a
251character set. A completely different set of special characters exists
252inside character sets: @samp{]}, @samp{-} and @samp{^}.@refill
253
254@samp{-} is used for ranges of characters. To write a range, write two
255characters with a @samp{-} between them. Thus, @samp{[a-z]} matches any
256lower case letter. Ranges may be intermixed freely with individual
257characters, as in @samp{[a-z$%.]}, which matches any lower case letter
258or @samp{$}, @samp{%} or a period.@refill
259
260To include a @samp{]} in a character set, make it the first character.
261For example, @samp{[]a]} matches @samp{]} or @samp{a}. To include a
262@samp{-}, write @samp{-} as the first character in the set, or put
263immediately after a range. (You can replace one individual character
264@var{c} with the range @samp{@var{c}-@var{c}} to make a place to put the
265@samp{-}). There is no way to write a set containing just @samp{-} and
266@samp{]}.
267
268To include @samp{^} in a set, put it anywhere but at the beginning of
269the set.
270
271@item [^ @dots{} ]
272@cindex @samp{^} in regexp
273@samp{[^} begins a @dfn{complement character set}, which matches any
274character except the ones specified. Thus, @samp{[^a-z0-9A-Z]}
275matches all characters @emph{except} letters and digits.@refill
276
277@samp{^} is not special in a character set unless it is the first
278character. The character following the @samp{^} is treated as if it
279were first (thus, @samp{-} and @samp{]} are not special there).
280
281Note that a complement character set can match a newline, unless
282newline is mentioned as one of the characters not to match.
283
284@item ^
285@cindex @samp{^} in regexp
286@cindex beginning of line in regexp
287is a special character that matches the empty string, but only at
288the beginning of a line in the text being matched. Otherwise it fails
289to match anything. Thus, @samp{^foo} matches a @samp{foo} which occurs
290at the beginning of a line.
291
292When matching a string, @samp{^} matches at the beginning of the string
293or after a newline character @samp{\n}.
294
295@item $
296@cindex @samp{$} in regexp
297is similar to @samp{^} but matches only at the end of a line. Thus,
298@samp{x+$} matches a string of one @samp{x} or more at the end of a line.
299
300When matching a string, @samp{$} matches at the end of the string
301or before a newline character @samp{\n}.
302
303@item \
304@cindex @samp{\} in regexp
305has two functions: it quotes the special characters (including
306@samp{\}), and it introduces additional special constructs.
307
308Because @samp{\} quotes special characters, @samp{\$} is a regular
309expression which matches only @samp{$}, and @samp{\[} is a regular
310expression which matches only @samp{[}, and so on.
311
312Note that @samp{\} also has special meaning in the read syntax of Lisp
313strings (@pxref{String Type}), and must be quoted with @samp{\}. For
314example, the regular expression that matches the @samp{\} character is
315@samp{\\}. To write a Lisp string that contains the characters
316@samp{\\}, Lisp syntax requires you to quote each @samp{\} with another
317@samp{\}. Therefore, the read syntax for a regular expression matching
318@samp{\} is @code{"\\\\"}.@refill
319@end table
320
321@strong{Please note:} for historical compatibility, special characters
322are treated as ordinary ones if they are in contexts where their special
323meanings make no sense. For example, @samp{*foo} treats @samp{*} as
324ordinary since there is no preceding expression on which the @samp{*}
325can act. It is poor practice to depend on this behavior; better to
326quote the special character anyway, regardless of where it
327appears.@refill
328
329For the most part, @samp{\} followed by any character matches only
330that character. However, there are several exceptions: characters
331which, when preceded by @samp{\}, are special constructs. Such
332characters are always ordinary when encountered on their own. Here
333is a table of @samp{\} constructs:
334
335@table @kbd
336@item \|
337@cindex @samp{|} in regexp
338@cindex regexp alternative
339specifies an alternative.
340Two regular expressions @var{a} and @var{b} with @samp{\|} in
341between form an expression that matches anything that either @var{a} or
342@var{b} matches.@refill
343
344Thus, @samp{foo\|bar} matches either @samp{foo} or @samp{bar}
345but no other string.@refill
346
347@samp{\|} applies to the largest possible surrounding expressions. Only a
348surrounding @samp{\( @dots{} \)} grouping can limit the grouping power of
349@samp{\|}.@refill
350
351Full backtracking capability exists to handle multiple uses of @samp{\|}.
352
353@item \( @dots{} \)
354@cindex @samp{(} in regexp
355@cindex @samp{)} in regexp
356@cindex regexp grouping
357is a grouping construct that serves three purposes:
358
359@enumerate
360@item
361To enclose a set of @samp{\|} alternatives for other operations.
362Thus, @samp{\(foo\|bar\)x} matches either @samp{foox} or @samp{barx}.
363
364@item
365To enclose an expression for a suffix operator such as @samp{*} to act
366on. Thus, @samp{ba\(na\)*} matches @samp{bananana}, etc., with any
367(zero or more) number of @samp{na} strings.@refill
368
369@item
370To record a matched substring for future reference.
371@end enumerate
372
373This last application is not a consequence of the idea of a
374parenthetical grouping; it is a separate feature which happens to be
375assigned as a second meaning to the same @samp{\( @dots{} \)} construct
376because there is no conflict in practice between the two meanings.
377Here is an explanation of this feature:
378
379@item \@var{digit}
380matches the same text which matched the @var{digit}th occurrence of a
381@samp{\( @dots{} \)} construct.
382
383In other words, after the end of a @samp{\( @dots{} \)} construct. the
384matcher remembers the beginning and end of the text matched by that
385construct. Then, later on in the regular expression, you can use
386@samp{\} followed by @var{digit} to match that same text, whatever it
387may have been.
388
389The strings matching the first nine @samp{\( @dots{} \)} constructs
390appearing in a regular expression are assigned numbers 1 through 9 in
391the order that the open parentheses appear in the regular expression.
392So you can use @samp{\1} through @samp{\9} to refer to the text matched
393by the corresponding @samp{\( @dots{} \)} constructs.
394
395For example, @samp{\(.*\)\1} matches any newline-free string that is
396composed of two identical halves. The @samp{\(.*\)} matches the first
397half, which may be anything, but the @samp{\1} that follows must match
398the same exact text.
399
400@item \w
401@cindex @samp{\w} in regexp
402matches any word-constituent character. The editor syntax table
403determines which characters these are. @xref{Syntax Tables}.
404
405@item \W
406@cindex @samp{\W} in regexp
407matches any character that is not a word-constituent.
408
409@item \s@var{code}
410@cindex @samp{\s} in regexp
411matches any character whose syntax is @var{code}. Here @var{code} is a
412character which represents a syntax code: thus, @samp{w} for word
413constituent, @samp{-} for whitespace, @samp{(} for open parenthesis,
414etc. @xref{Syntax Tables}, for a list of syntax codes and the
415characters that stand for them.
416
417@item \S@var{code}
418@cindex @samp{\S} in regexp
419matches any character whose syntax is not @var{code}.
420@end table
421
422 These regular expression constructs match the empty string---that is,
423they don't use up any characters---but whether they match depends on the
424context.
425
426@table @kbd
427@item \`
428@cindex @samp{\`} in regexp
429matches the empty string, but only at the beginning
430of the buffer or string being matched against.
431
432@item \'
433@cindex @samp{\'} in regexp
434matches the empty string, but only at the end of
435the buffer or string being matched against.
436
437@item \=
438@cindex @samp{\=} in regexp
439matches the empty string, but only at point.
440(This construct is not defined when matching against a string.)
441
442@item \b
443@cindex @samp{\b} in regexp
444matches the empty string, but only at the beginning or
445end of a word. Thus, @samp{\bfoo\b} matches any occurrence of
446@samp{foo} as a separate word. @samp{\bballs?\b} matches
447@samp{ball} or @samp{balls} as a separate word.@refill
448
449@item \B
450@cindex @samp{\B} in regexp
451matches the empty string, but @emph{not} at the beginning or
452end of a word.
453
454@item \<
455@cindex @samp{\<} in regexp
456matches the empty string, but only at the beginning of a word.
457
458@item \>
459@cindex @samp{\>} in regexp
460matches the empty string, but only at the end of a word.
461@end table
462
463@kindex invalid-regexp
464 Not every string is a valid regular expression. For example, a string
465with unbalanced square brackets is invalid (with a few exceptions, such
466as @samp{[]]}, and so is a string that ends with a single @samp{\}. If
467an invalid regular expression is passed to any of the search functions,
468an @code{invalid-regexp} error is signaled.
469
470@defun regexp-quote string
471This function returns a regular expression string that matches exactly
472@var{string} and nothing else. This allows you to request an exact
473string match when calling a function that wants a regular expression.
474
475@example
476@group
477(regexp-quote "^The cat$")
478 @result{} "\\^The cat\\$"
479@end group
480@end example
481
482One use of @code{regexp-quote} is to combine an exact string match with
483context described as a regular expression. For example, this searches
484for the string which is the value of @code{string}, surrounded by
485whitespace:
486
487@example
488@group
489(re-search-forward
490 (concat "\\s " (regexp-quote string) "\\s "))
491@end group
492@end example
493@end defun
494
495@node Regexp Example
496@comment node-name, next, previous, up
497@subsection Complex Regexp Example
498
499 Here is a complicated regexp, used by Emacs to recognize the end of a
500sentence together with any whitespace that follows. It is the value of
501the variable @code{sentence-end}.
502
503 First, we show the regexp as a string in Lisp syntax to distinguish
504spaces from tab characters. The string constant begins and ends with a
505double-quote. @samp{\"} stands for a double-quote as part of the
506string, @samp{\\} for a backslash as part of the string, @samp{\t} for a
507tab and @samp{\n} for a newline.
508
509@example
510"[.?!][]\"')@}]*\\($\\| $\\|\t\\| \\)[ \t\n]*"
511@end example
512
513 In contrast, if you evaluate the variable @code{sentence-end}, you
514will see the following:
515
516@example
517@group
518sentence-end
519@result{}
520"[.?!][]\"')@}]*\\($\\| $\\| \\| \\)[
521]*"
522@end group
523@end example
524
525@noindent
526In this output, tab and newline appear as themselves.
527
528 This regular expression contains four parts in succession and can be
529deciphered as follows:
530
531@table @code
532@item [.?!]
533The first part of the pattern consists of three characters, a period, a
534question mark and an exclamation mark, within square brackets. The
535match must begin with one of these three characters.
536
537@item []\"')@}]*
538The second part of the pattern matches any closing braces and quotation
539marks, zero or more of them, that may follow the period, question mark
540or exclamation mark. The @code{\"} is Lisp syntax for a double-quote in
541a string. The @samp{*} at the end indicates that the immediately
542preceding regular expression (a character set, in this case) may be
543repeated zero or more times.
544
545@item \\($\\|@ \\|\t\\|@ @ \\)
546The third part of the pattern matches the whitespace that follows the
547end of a sentence: the end of a line, or a tab, or two spaces. The
548double backslashes mark the parentheses and vertical bars as regular
549expression syntax; the parentheses mark the group and the vertical bars
550separate alternatives. The dollar sign is used to match the end of a
551line.
552
553@item [ \t\n]*
554Finally, the last part of the pattern matches any additional whitespace
555beyond the minimum needed to end a sentence.
556@end table
557
558@node Regexp Search
559@section Regular Expression Searching
560@cindex regular expression searching
561@cindex regexp searching
562@cindex searching for regexp
563
564 In GNU Emacs, you can search for the next match for a regexp either
565incrementally or not. For incremental search commands, see @ref{Regexp
566Search, , Regular Expression Search, emacs, The GNU Emacs Manual}. Here
567we describe only the search functions useful in programs. The principal
568one is @code{re-search-forward}.
569
570@deffn Command re-search-forward regexp &optional limit noerror repeat
571This function searches forward in the current buffer for a string of
572text that is matched by the regular expression @var{regexp}. The
573function skips over any amount of text that is not matched by
574@var{regexp}, and leaves point at the end of the first match found.
575It returns the new value of point.
576
577If @var{limit} is non-@code{nil} (it must be a position in the current
578buffer), then it is the upper bound to the search. No match extending
579after that position is accepted.
580
581What happens when the search fails depends on the value of
582@var{noerror}. If @var{noerror} is @code{nil}, a @code{search-failed}
583error is signaled. If @var{noerror} is @code{t},
584@code{re-search-forward} does nothing and returns @code{nil}. If
585@var{noerror} is neither @code{nil} nor @code{t}, then
586@code{re-search-forward} moves point to @var{limit} (or the end of the
587buffer) and returns @code{nil}.
588
589If @var{repeat} is supplied (it must be a positive number), then the
590search is repeated that many times (each time starting at the end of the
591previous time's match). If these successive searches succeed, the
592function succeeds, moving point and returning its new value. Otherwise
593the search fails.
594
595In the following example, point is initially before the @samp{T}.
596Evaluating the search call moves point to the end of that line (between
597the @samp{t} of @samp{hat} and the newline).
598
599@example
600@group
601---------- Buffer: foo ----------
602I read "@point{}The cat in the hat
603comes back" twice.
604---------- Buffer: foo ----------
605@end group
606
607@group
608(re-search-forward "[a-z]+" nil t 5)
609 @result{} 27
610
611---------- Buffer: foo ----------
612I read "The cat in the hat@point{}
613comes back" twice.
614---------- Buffer: foo ----------
615@end group
616@end example
617@end deffn
618
619@deffn Command re-search-backward regexp &optional limit noerror repeat
620This function searches backward in the current buffer for a string of
621text that is matched by the regular expression @var{regexp}, leaving
622point at the beginning of the first text found.
623
624This function is analogous to @code{re-search-forward}, but they are
625not simple mirror images. @code{re-search-forward} finds the match
626whose beginning is as close as possible. If @code{re-search-backward}
627were a perfect mirror image, it would find the match whose end is as
628close as possible. However, in fact it finds the match whose beginning
629is as close as possible. The reason is that matching a regular
630expression at a given spot always works from beginning to end, and is
631done at a specified beginning position.
632
633A true mirror-image of @code{re-search-forward} would require a special
634feature for matching regexps from end to beginning. It's not worth the
635trouble of implementing that.
636@end deffn
637
638@defun string-match regexp string &optional start
639This function returns the index of the start of the first match for
640the regular expression @var{regexp} in @var{string}, or @code{nil} if
641there is no match. If @var{start} is non-@code{nil}, the search starts
642at that index in @var{string}.
643
644For example,
645
646@example
647@group
648(string-match
649 "quick" "The quick brown fox jumped quickly.")
650 @result{} 4
651@end group
652@group
653(string-match
654 "quick" "The quick brown fox jumped quickly." 8)
655 @result{} 27
656@end group
657@end example
658
659@noindent
660The index of the first character of the
661string is 0, the index of the second character is 1, and so on.
662
663After this function returns, the index of the first character beyond
664the match is available as @code{(match-end 0)}. @xref{Match Data}.
665
666@example
667@group
668(string-match
669 "quick" "The quick brown fox jumped quickly." 8)
670 @result{} 27
671@end group
672
673@group
674(match-end 0)
675 @result{} 32
676@end group
677@end example
678@end defun
679
680@defun looking-at regexp
681This function determines whether the text in the current buffer directly
682following point matches the regular expression @var{regexp}. ``Directly
683following'' means precisely that: the search is ``anchored'' and it can
684succeed only starting with the first character following point. The
685result is @code{t} if so, @code{nil} otherwise.
686
687This function does not move point, but it updates the match data, which
688you can access using @code{match-beginning} and @code{match-end}.
689@xref{Match Data}.
690
691In this example, point is located directly before the @samp{T}. If it
692were anywhere else, the result would be @code{nil}.
693
694@example
695@group
696---------- Buffer: foo ----------
697I read "@point{}The cat in the hat
698comes back" twice.
699---------- Buffer: foo ----------
700
701(looking-at "The cat in the hat$")
702 @result{} t
703@end group
704@end example
705@end defun
706
707@ignore
708@deffn Command delete-matching-lines regexp
709This function is identical to @code{delete-non-matching-lines}, save
710that it deletes what @code{delete-non-matching-lines} keeps.
711
712In the example below, point is located on the first line of text.
713
714@example
715@group
716---------- Buffer: foo ----------
717We hold these truths
718to be self-evident,
719that all men are created
720equal, and that they are
721---------- Buffer: foo ----------
722@end group
723
724@group
725(delete-matching-lines "the")
726 @result{} nil
727
728---------- Buffer: foo ----------
729to be self-evident,
730that all men are created
731---------- Buffer: foo ----------
732@end group
733@end example
734@end deffn
735
736@deffn Command flush-lines regexp
737This function is the same as @code{delete-matching-lines}.
738@end deffn
739
740@defun delete-non-matching-lines regexp
741This function deletes all lines following point which don't
742contain a match for the regular expression @var{regexp}.
743@end defun
744
745@deffn Command keep-lines regexp
746This function is the same as @code{delete-non-matching-lines}.
747@end deffn
748
749@deffn Command how-many regexp
750This function counts the number of matches for @var{regexp} there are in
751the current buffer following point. It prints this number in
752the echo area, returning the string printed.
753@end deffn
754
755@deffn Command count-matches regexp
756This function is a synonym of @code{how-many}.
757@end deffn
758
759@deffn Command list-matching-lines regexp nlines
760This function is a synonym of @code{occur}.
761Show all lines following point containing a match for @var{regexp}.
762Display each line with @var{nlines} lines before and after,
763or @code{-}@var{nlines} before if @var{nlines} is negative.
764@var{nlines} defaults to @code{list-matching-lines-default-context-lines}.
765Interactively it is the prefix arg.
766
767The lines are shown in a buffer named @samp{*Occur*}.
768It serves as a menu to find any of the occurrences in this buffer.
769@kbd{C-h m} (@code{describe-mode} in that buffer gives help.
770@end deffn
771
772@defopt list-matching-lines-default-context-lines
773Default value is 0.
774Default number of context lines to include around a @code{list-matching-lines}
775match. A negative number means to include that many lines before the match.
776A positive number means to include that many lines both before and after.
777@end defopt
778@end ignore
779
780@node Search and Replace
781@section Search and Replace
782@cindex replacement
783
784@defun perform-replace from-string replacements query-flag regexp-flag delimited-flag &optional repeat-count map
785This function is the guts of @code{query-replace} and related commands.
786It searches for occurrences of @var{from-string} and replaces some or
787all of them. If @var{query-flag} is @code{nil}, it replaces all
788occurrences; otherwise, it asks the user what to do about each one.
789
790If @var{regexp-flag} is non-@code{nil}, then @var{from-string} is
791considered a regular expression; otherwise, it must match literally. If
792@var{delimited-flag} is non-@code{nil}, then only replacements
793surrounded by word boundaries are considered.
794
795The argument @var{replacements} specifies what to replace occurrences
796with. If it is a string, that string is used. It can also be a list of
797strings, to be used in cyclic order.
798
799If @var{repeat-count} is non-@code{nil}, it should be an integer, the
800number of occurrences to consider. In this case, @code{perform-replace}
801returns after considering that many occurrences.
802
803Normally, the keymap @code{query-replace-map} defines the possible user
804responses. The argument @var{map}, if non-@code{nil}, is a keymap to
805use instead of @code{query-replace-map}.
806@end defun
807
808@defvar query-replace-map
809This variable holds a special keymap that defines the valid user
810responses for @code{query-replace} and related functions, as well as
811@code{y-or-n-p} and @code{map-y-or-n-p}. It is unusual in two ways:
812
813@itemize @bullet
814@item
815The ``key bindings'' are not commands, just symbols that are meaningful
816to the functions that use this map.
817
818@item
819Prefix keys are not supported; each key binding must be for a single event
820key sequence. This is because the functions don't use read key sequence to
821get the input; instead, they read a single event and look it up ``by hand.''
822@end itemize
823@end defvar
824
825Here are the meaningful ``bindings'' for @code{query-replace-map}.
826Several of them are meaningful only for @code{query-replace} and
827friends.
828
829@table @code
830@item act
831Do take the action being considered---in other words, ``yes.''
832
833@item skip
834Do not take action for this question---in other words, ``no.''
835
836@item exit
837Answer this question ``no,'' and don't ask any more.
838
839@item act-and-exit
840Answer this question ``yes,'' and don't ask any more.
841
842@item act-and-show
843Answer this question ``yes,'' but show the results---don't advance yet
844to the next question.
845
846@item automatic
847Answer this question and all subsequent questions in the series with
848``yes,'' without further user interaction.
849
850@item backup
851Move back to the previous place that a question was asked about.
852
853@item edit
854Enter a recursive edit to deal with this question---instead of any
855other action that would normally be taken.
856
857@item delete-and-edit
858Delete the text being considered, then enter a recursive edit to replace
859it.
860
861@item recenter
862Redisplay and center the window, then ask the same question again.
863
864@item quit
865Perform a quit right away. Only @code{y-or-n-p} and related functions
866use this answer.
867
868@item help
869Display some help, then ask again.
870@end table
871
872@node Match Data
873@section The Match Data
874@cindex match data
875
876 Emacs keeps track of the positions of the start and end of segments of
877text found during a regular expression search. This means, for example,
878that you can search for a complex pattern, such as a date in an Rmail
879message, and then extract parts of the match under control of the
880pattern.
881
882 Because the match data normally describe the most recent search only,
883you must be careful not to do another search inadvertently between the
884search you wish to refer back to and the use of the match data. If you
885can't avoid another intervening search, you must save and restore the
886match data around it, to prevent it from being overwritten.
887
888@menu
889* Simple Match Data:: Accessing single items of match data,
890 such as where a particular subexpression started.
891* Replacing Match:: Replacing a substring that was matched.
892* Entire Match Data:: Accessing the entire match data at once, as a list.
893* Saving Match Data:: Saving and restoring the match data.
894@end menu
895
896@node Simple Match Data
897@subsection Simple Match Data Access
898
899 This section explains how to use the match data to find the starting
900point or ending point of the text that was matched by a particular
901search, or by a particular parenthetical subexpression of a regular
902expression.
903
904@defun match-beginning count
905This function returns the position of the start of text matched by the
906last regular expression searched for, or a subexpression of it.
907
908The argument @var{count}, a number, specifies a subexpression whose
909start position is the value. If @var{count} is zero, then the value is
910the position of the text matched by the whole regexp. If @var{count} is
911greater than zero, then the value is the position of the beginning of
912the text matched by the @var{count}th subexpression.
913
914Subexpressions of a regular expression are those expressions grouped
915inside of parentheses, @samp{\(@dots{}\)}. The @var{count}th
916subexpression is found by counting occurrences of @samp{\(} from the
917beginning of the whole regular expression. The first subexpression is
918numbered 1, the second 2, and so on.
919
920The value is @code{nil} for a parenthetical grouping inside of a
921@samp{\|} alternative that wasn't used in the match.
922@end defun
923
924@defun match-end count
925This function returns the position of the end of the text that matched
926the last regular expression searched for, or a subexpression of it.
927This function is otherwise similar to @code{match-beginning}.
928@end defun
929
930 Here is an example of using the match data, with a comment showing the
931positions within the text:
932
933@example
934@group
935(string-match "\\(qu\\)\\(ick\\)"
936 "The quick fox jumped quickly.")
937 ;0123456789
938 @result{} 4
939@end group
940
941@group
942(match-beginning 1) ; @r{The beginning of the match}
943 @result{} 4 ; @r{with @samp{qu} is at index 4.}
944@end group
945
946@group
947(match-beginning 2) ; @r{The beginning of the match}
948 @result{} 6 ; @r{with @samp{ick} is at index 6.}
949@end group
950
951@group
952(match-end 1) ; @r{The end of the match}
953 @result{} 6 ; @r{with @samp{qu} is at index 6.}
954
955(match-end 2) ; @r{The end of the match}
956 @result{} 9 ; @r{with @samp{ick} is at index 9.}
957@end group
958@end example
959
960 Here is another example. Point is initially located at the beginning
961of the line. Searching moves point to between the space and the word
962@samp{in}. The beginning of the entire match is at the 9th character of
963the buffer (@samp{T}), and the beginning of the match for the first
964subexpression is at the 13th character (@samp{c}).
965
966@example
967@group
968(list
969 (re-search-forward "The \\(cat \\)")
970 (match-beginning 0)
971 (match-beginning 1))
972 @result{} (t 9 13)
973@end group
974
975@group
976---------- Buffer: foo ----------
977I read "The cat @point{}in the hat comes back" twice.
978 ^ ^
979 9 13
980---------- Buffer: foo ----------
981@end group
982@end example
983
984@noindent
985(In this case, the index returned is a buffer position; the first
986character of the buffer counts as 1.)
987
988@node Replacing Match
989@subsection Replacing the Text That Matched
990
991 This function replaces the text matched by the last search with
992@var{replacement}.
993
994@cindex case in replacements
995@defun replace-match replacement &optional fixedcase literal
996This function replaces the buffer text matched by the last search, with
997@var{replacement}. It applies only to buffers; you can't use
998@code{replace-match} to replace a substring found with
999@code{string-match}.
1000
1001If @var{fixedcase} is non-@code{nil}, then the case of the replacement
1002text is not changed; otherwise, the replacement text is converted to a
1003different case depending upon the capitalization of the text to be
1004replaced. If the original text is all upper case, the replacement text
1005is converted to upper case, except when all of the words in the original
1006text are only one character long. In that event, the replacement text
1007is capitalized. If @emph{any} of the words in the original text is
1008capitalized, then all of the words in the replacement text are
1009capitalized.
1010
1011If @var{literal} is non-@code{nil}, then @var{replacement} is inserted
1012exactly as it is, the only alterations being case changes as needed.
1013If it is @code{nil} (the default), then the character @samp{\} is treated
1014specially. If a @samp{\} appears in @var{replacement}, then it must be
1015part of one of the following sequences:
1016
1017@table @asis
1018@item @samp{\&}
1019@cindex @samp{&} in replacement
1020@samp{\&} stands for the entire text being replaced.
1021
1022@item @samp{\@var{n}}
1023@cindex @samp{\@var{n}} in replacement
1024@samp{\@var{n}} stands for the text that matched the @var{n}th
1025subexpression in the original regexp. Subexpressions are those
1026expressions grouped inside of @samp{\(@dots{}\)}. @var{n} is a digit.
1027
1028@item @samp{\\}
1029@cindex @samp{\} in replacement
1030@samp{\\} stands for a single @samp{\} in the replacement text.
1031@end table
1032
1033@code{replace-match} leaves point at the end of the replacement text,
1034and returns @code{t}.
1035@end defun
1036
1037@node Entire Match Data
1038@subsection Accessing the Entire Match Data
1039
1040 The functions @code{match-data} and @code{set-match-data} read or
1041write the entire match data, all at once.
1042
1043@defun match-data
1044This function returns a newly constructed list containing all the
1045information on what text the last search matched. Element zero is the
1046position of the beginning of the match for the whole expression; element
1047one is the position of the end of the match for the expression. The
1048next two elements are the positions of the beginning and end of the
1049match for the first subexpression, and so on. In general, element
1050@ifinfo
1051number 2@var{n}
1052@end ifinfo
1053@tex
1054number {\mathsurround=0pt $2n$}
1055@end tex
1056corresponds to @code{(match-beginning @var{n})}; and
1057element
1058@ifinfo
1059number 2@var{n} + 1
1060@end ifinfo
1061@tex
1062number {\mathsurround=0pt $2n+1$}
1063@end tex
1064corresponds to @code{(match-end @var{n})}.
1065
1066All the elements are markers or @code{nil} if matching was done on a
1067buffer, and all are integers or @code{nil} if matching was done on a
1068string with @code{string-match}. (In Emacs 18 and earlier versions,
1069markers were used even for matching on a string, except in the case
1070of the integer 0.)
1071
1072As always, there must be no possibility of intervening searches between
1073the call to a search function and the call to @code{match-data} that is
1074intended to access the match data for that search.
1075
1076@example
1077@group
1078(match-data)
1079 @result{} (#<marker at 9 in foo>
1080 #<marker at 17 in foo>
1081 #<marker at 13 in foo>
1082 #<marker at 17 in foo>)
1083@end group
1084@end example
1085@end defun
1086
1087@defun set-match-data match-list
1088This function sets the match data from the elements of @var{match-list},
1089which should be a list that was the value of a previous call to
1090@code{match-data}.
1091
1092If @var{match-list} refers to a buffer that doesn't exist, you don't get
1093an error; that sets the match data in a meaningless but harmless way.
1094
1095@findex store-match-data
1096@code{store-match-data} is an alias for @code{set-match-data}.
1097@end defun
1098
1099@node Saving Match Data
1100@subsection Saving and Restoring the Match Data
1101
1102 All asynchronous process functions (filters and sentinels) and
1103functions that use @code{recursive-edit} should save and restore the
1104match data if they do a search or if they let the user type arbitrary
1105commands. Saving the match data is useful in other cases as
1106well---whenever you want to access the match data resulting from an
1107earlier search, notwithstanding another intervening search.
1108
1109 This example shows the problem that can arise if you fail to
1110attend to this requirement:
1111
1112@example
1113@group
1114(re-search-forward "The \\(cat \\)")
1115 @result{} 48
1116(foo) ; @r{Perhaps @code{foo} does}
1117 ; @r{more searching.}
1118(match-end 0)
1119 @result{} 61 ; @r{Unexpected result---not 48!}
1120@end group
1121@end example
1122
1123 In Emacs versions 19 and later, you can save and restore the match
1124data with @code{save-match-data}:
1125
1126@defspec save-match-data body@dots{}
1127This special form executes @var{body}, saving and restoring the match
1128data around it. This is useful if you wish to do a search without
1129altering the match data that resulted from an earlier search.
1130@end defspec
1131
1132 You can use @code{set-match-data} together with @code{match-data} to
1133imitate the effect of the special form @code{save-match-data}. This is
1134useful for writing code that can run in Emacs 18. Here is how:
1135
1136@example
1137@group
1138(let ((data (match-data)))
1139 (unwind-protect
1140 @dots{} ; @r{May change the original match data.}
1141 (set-match-data data)))
1142@end group
1143@end example
1144
1145@ignore
1146 Here is a function which restores the match data provided the buffer
1147associated with it still exists.
1148
1149@smallexample
1150@group
1151(defun restore-match-data (data)
1152@c It is incorrect to split the first line of a doc string.
1153@c If there's a problem here, it should be solved in some other way.
1154 "Restore the match data DATA unless the buffer is missing."
1155 (catch 'foo
1156 (let ((d data))
1157@end group
1158 (while d
1159 (and (car d)
1160 (null (marker-buffer (car d)))
1161@group
1162 ;; @file{match-data} @r{buffer is deleted.}
1163 (throw 'foo nil))
1164 (setq d (cdr d)))
1165 (set-match-data data))))
1166@end group
1167@end smallexample
1168@end ignore
1169
1170@node Searching and Case
1171@section Searching and Case
1172@cindex searching and case
1173
1174 By default, searches in Emacs ignore the case of the text they are
1175searching through; if you specify searching for @samp{FOO}, then
1176@samp{Foo} or @samp{foo} is also considered a match. Regexps, and in
1177particular character sets, are included: thus, @samp{[aB]} would match
1178@samp{a} or @samp{A} or @samp{b} or @samp{B}.
1179
1180 If you do not want this feature, set the variable
1181@code{case-fold-search} to @code{nil}. Then all letters must match
1182exactly, including case. This is a per-buffer-local variable; altering
1183the variable affects only the current buffer. (@xref{Intro to
1184Buffer-Local}.) Alternatively, you may change the value of
1185@code{default-case-fold-search}, which is the default value of
1186@code{case-fold-search} for buffers that do not override it.
1187
1188 Note that the user-level incremental search feature handles case
1189distinctions differently. When given a lower case letter, it looks for
1190a match of either case, but when given an upper case letter, it looks
1191for an upper case letter only. But this has nothing to do with the
1192searching functions Lisp functions use.
1193
1194@defopt case-replace
1195This variable determines whether @code{query-replace} should preserve
1196case in replacements. If the variable is @code{nil}, then
1197@code{replace-match} should not try to convert case.
1198@end defopt
1199
1200@defopt case-fold-search
1201This buffer-local variable determines whether searches should ignore
1202case. If the variable is @code{nil} they do not ignore case; otherwise
1203they do ignore case.
1204@end defopt
1205
1206@defvar default-case-fold-search
1207The value of this variable is the default value for
1208@code{case-fold-search} in buffers that do not override it. This is the
1209same as @code{(default-value 'case-fold-search)}.
1210@end defvar
1211
1212@node Standard Regexps
1213@section Standard Regular Expressions Used in Editing
1214@cindex regexps used standardly in editing
1215@cindex standard regexps used in editing
1216
1217 This section describes some variables that hold regular expressions
1218used for certain purposes in editing:
1219
1220@defvar page-delimiter
1221This is the regexp describing line-beginnings that separate pages. The
1222default value is @code{"^\014"} (i.e., @code{"^^L"} or @code{"^\C-l"}).
1223@end defvar
1224
1225@defvar paragraph-separate
1226This is the regular expression for recognizing the beginning of a line
1227that separates paragraphs. (If you change this, you may have to
1228change @code{paragraph-start} also.) The default value is @code{"^[
1229\t\f]*$"}, which is a line that consists entirely of spaces, tabs, and
1230form feeds.
1231@end defvar
1232
1233@defvar paragraph-start
1234This is the regular expression for recognizing the beginning of a line
1235that starts @emph{or} separates paragraphs. The default value is
1236@code{"^[ \t\n\f]"}, which matches a line starting with a space, tab,
1237newline, or form feed.
1238@end defvar
1239
1240@defvar sentence-end
1241This is the regular expression describing the end of a sentence. (All
1242paragraph boundaries also end sentences, regardless.) The default value
1243is:
1244
1245@example
1246"[.?!][]\"')@}]*\\($\\|\t\\| \\)[ \t\n]*"
1247@end example
1248
1249This means a period, question mark or exclamation mark, followed by a
1250closing brace, followed by tabs, spaces or new lines.
1251
1252For a detailed explanation of this regular expression, see @ref{Regexp
1253Example}.
1254@end defvar
diff --git a/lispref/syntax.texi b/lispref/syntax.texi
new file mode 100644
index 00000000000..9766df698f2
--- /dev/null
+++ b/lispref/syntax.texi
@@ -0,0 +1,707 @@
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/syntax
6@node Syntax Tables, Abbrevs, Searching and Matching, Top
7@chapter Syntax Tables
8@cindex parsing
9@cindex syntax table
10@cindex text parsing
11
12 A @dfn{syntax table} specifies the syntactic textual function of each
13character. This information is used by the parsing commands, the
14complex movement commands, and others to determine where words, symbols,
15and other syntactic constructs begin and end. The current syntax table
16controls the meaning of the word motion functions (@pxref{Word Motion})
17and the list motion functions (@pxref{List Motion}) as well as the
18functions in this chapter.
19
20@menu
21* Basics: Syntax Basics. Basic concepts of syntax tables.
22* Desc: Syntax Descriptors. How characters are classified.
23* Syntax Table Functions:: How to create, examine and alter syntax tables.
24* Motion and Syntax:: Moving over characters with certain syntaxes.
25* Parsing Expressions:: Parsing balanced expressions
26 using the syntax table.
27* Standard Syntax Tables:: Syntax tables used by various major modes.
28* Syntax Table Internals:: How syntax table information is stored.
29@end menu
30
31@node Syntax Basics
32@section Syntax Table Concepts
33
34@ifinfo
35 A @dfn{syntax table} provides Emacs with the information that
36determines the syntactic use of each character in a buffer. This
37information is used by the parsing commands, the complex movement
38commands, and others to determine where words, symbols, and other
39syntactic constructs begin and end. The current syntax table controls
40the meaning of the word motion functions (@pxref{Word Motion}) and the
41list motion functions (@pxref{List Motion}) as well as the functions in
42this chapter.
43@end ifinfo
44
45 A syntax table is a vector of 256 elements; it contains one entry for
46each of the 256 @sc{ASCII} characters of an 8-bit byte. Each element is
47an integer that encodes the syntax of the character in question.
48
49 Syntax tables are used only for moving across text, not for the Emacs
50Lisp reader. Emacs Lisp uses built-in syntactic rules when reading Lisp
51expressions, and these rules cannot be changed.
52
53 Each buffer has its own major mode, and each major mode has its own
54idea of the syntactic class of various characters. For example, in Lisp
55mode, the character @samp{;} begins a comment, but in C mode, it
56terminates a statement. To support these variations, Emacs makes the
57choice of syntax table local to each buffer. Typically, each major
58mode has its own syntax table and installs that table in each buffer
59which uses that mode. Changing this table alters the syntax in all
60those buffers as well as in any buffers subsequently put in that mode.
61Occasionally several similar modes share one syntax table.
62@xref{Example Major Modes}, for an example of how to set up a syntax
63table.
64
65A syntax table can inherit the data for some characters from the
66standard syntax table, while specifying other characters itself. The
67``inherit'' syntax class means ``inherit this character's syntax from
68the standard syntax table.'' Most major modes' syntax tables inherit
69the syntax of character codes 0 through 31 and 128 through 255. This is
70useful with character sets such as ISO Latin-1 that have additional
71alphabetic characters in the range 128 to 255. Just changing the
72standard syntax for these characters affects all major modes.
73
74@defun syntax-table-p object
75This function returns @code{t} if @var{object} is a vector of length 256
76elements. This means that the vector may be a syntax table. However,
77according to this test, any vector of length 256 is considered to be a
78syntax table, no matter what its contents.
79@end defun
80
81@node Syntax Descriptors
82@section Syntax Descriptors
83@cindex syntax classes
84
85 This section describes the syntax classes and flags that denote the
86syntax of a character, and how they are represented as a @dfn{syntax
87descriptor}, which is a Lisp string that you pass to
88@code{modify-syntax-entry} to specify the desired syntax.
89
90 Emacs defines a number of @dfn{syntax classes}. Each syntax table
91puts each character into one class. There is no necessary relationship
92between the class of a character in one syntax table and its class in
93any other table.
94
95 Each class is designated by a mnemonic character which serves as the
96name of the class when you need to specify a class. Usually the
97designator character is one which is frequently put in that class;
98however, its meaning as a designator is unvarying and independent of
99what syntax that character currently has.
100
101@cindex syntax descriptor
102 A syntax descriptor is a Lisp string which specifies a syntax class, a
103matching character (used only for the parenthesis classes) and flags.
104The first character is the designator for a syntax class. The second
105character is the character to match; if it is unused, put a space there.
106Then come the characters for any desired flags. If no matching
107character or flags are needed, one character is sufficient.
108
109 For example, the descriptor for the character @samp{*} in C mode is
110@samp{@w{. 23}} (i.e., punctuation, matching character slot unused,
111second character of a comment-starter, first character of an
112comment-ender), and the entry for @samp{/} is @samp{@w{. 14}} (i.e.,
113punctuation, matching character slot unused, first character of a
114comment-starter, second character of a comment-ender).
115
116@menu
117* Syntax Class Table:: Table of syntax classes.
118* Syntax Flags:: Additional flags each character can have.
119@end menu
120
121@node Syntax Class Table
122@subsection Table of Syntax Classes
123
124 Here is a table syntax classes, the characters that stand for them,
125their meanings, and examples of their use.
126
127@deffn {Syntax class} @w{whitespace character}
128@dfn{Whitespace characters} (designated with @w{@samp{@ }} or @samp{-})
129separate symbols and words from each other. Typically, whitespace
130characters have no other syntactic significance, and multiple whitespace
131characters are syntactically equivalent to a single one. Space, tab,
132newline and formfeed are almost always classified as whitespace.
133@end deffn
134
135@deffn {Syntax class} @w{word constituent}
136@dfn{Word constituents} (designated with @samp{w}) are parts of normal
137English words and are typically used in variable and command names in
138programs. All upper and lower case letters and the digits are typically
139word constituents.
140@end deffn
141
142@deffn {Syntax class} @w{symbol constituent}
143@dfn{Symbol constituents} (designated with @samp{_}) are the extra
144characters that are used in variable and command names along with word
145constituents. For example, the symbol constituents class is used in
146Lisp mode to indicate that certain characters may be part of symbol
147names even though they are not part of English words. These characters
148are @samp{$&*+-_<>}. In standard C, the only non-word-constituent
149character that is valid in symbols is underscore (@samp{_}).
150@end deffn
151
152@deffn {Syntax class} @w{punctuation character}
153@dfn{Punctuation characters} (@samp{.}) are those characters that are
154used as punctuation in English, or are used in some way in a programming
155language to separate symbols from one another. Most programming
156language modes, including Emacs Lisp mode, have no characters in this
157class since the few characters that are not symbol or word constituents
158all have other uses.
159@end deffn
160
161@deffn {Syntax class} @w{open parenthesis character}
162@deffnx {Syntax class} @w{close parenthesis character}
163@cindex parenthesis syntax
164Open and close @dfn{parenthesis characters} are characters used in
165dissimilar pairs to surround sentences or expressions. Such a grouping
166is begun with an open parenthesis character and terminated with a close.
167Each open parenthesis character matches a particular close parenthesis
168character, and vice versa. Normally, Emacs indicates momentarily the
169matching open parenthesis when you insert a close parenthesis.
170@xref{Blinking}.
171
172The class of open parentheses is designated with @samp{(}, and that of
173close parentheses with @samp{)}.
174
175In English text, and in C code, the parenthesis pairs are @samp{()},
176@samp{[]}, and @samp{@{@}}. In Emacs Lisp, the delimiters for lists and
177vectors (@samp{()} and @samp{[]}) are classified as parenthesis
178characters.
179@end deffn
180
181@deffn {Syntax class} @w{string quote}
182@dfn{String quote characters} (designated with @samp{"}) are used in
183many languages, including Lisp and C, to delimit string constants. The
184same string quote character appears at the beginning and the end of a
185string. Such quoted strings do not nest.
186
187The parsing facilities of Emacs consider a string as a single token.
188The usual syntactic meanings of the characters in the string are
189suppressed.
190
191The Lisp modes have two string quote characters: double-quote (@samp{"})
192and vertical bar (@samp{|}). @samp{|} is not used in Emacs Lisp, but it
193is used in Common Lisp. C also has two string quote characters:
194double-quote for strings, and single-quote (@samp{'}) for character
195constants.
196
197English text has no string quote characters because English is not a
198programming language. Although quotation marks are used in English,
199we do not want them to turn off the usual syntactic properties of
200other characters in the quotation.
201@end deffn
202
203@deffn {Syntax class} @w{escape}
204An @dfn{escape character} (designated with @samp{\}) starts an escape
205sequence such as is used in C string and character constants. The
206character @samp{\} belongs to this class in both C and Lisp. (In C, it
207is used thus only inside strings, but it turns out to cause no trouble
208to treat it this way throughout C code.)
209
210Characters in this class count as part of words if
211@code{words-include-escapes} is non-@code{nil}. @xref{Word Motion}.
212@end deffn
213
214@deffn {Syntax class} @w{character quote}
215A @dfn{character quote character} (designated with @samp{/}) quotes the
216following character so that it loses its normal syntactic meaning. This
217differs from an escape character in that only the character immediately
218following is ever affected.
219
220Characters in this class count as part of words if
221@code{words-include-escapes} is non-@code{nil}. @xref{Word Motion}.
222
223This class is not currently used in any standard Emacs modes.
224@end deffn
225
226@deffn {Syntax class} @w{paired delimiter}
227@dfn{Paired delimiter characters} (designated with @samp{$}) are like
228string quote characters except that the syntactic properties of the
229characters between the delimiters are not suppressed. Only @TeX{} mode
230uses a paired identical delimiter presently---the @samp{$} that both
231enters and leaves math mode.
232@end deffn
233
234@deffn {Syntax class} @w{expression prefix}
235An @dfn{expression prefix operator} (designated with @samp{'}) is used
236for syntactic operators that are part of an expression if they appear
237next to one. These characters in Lisp include the apostrophe, @samp{'}
238(used for quoting), the comma, @samp{,} (used in macros), and @samp{#}
239(used in the read syntax for certain data types).
240@end deffn
241
242@deffn {Syntax class} @w{comment starter}
243@deffnx {Syntax class} @w{comment ender}
244@cindex comment syntax
245The @dfn{comment starter} and @dfn{comment ender} characters are used in
246various languages to delimit comments. These classes are designated
247with @samp{<} and @samp{>}, respectively.
248
249English text has no comment characters. In Lisp, the semicolon
250(@samp{;}) starts a comment and a newline or formfeed ends one.
251@end deffn
252
253@deffn {Syntax class} @w{inherit}
254This syntax class does not specify a syntax. It says to look in the
255standard syntax table to find the syntax of this character. The
256designator for this syntax code is @samp{@@}.
257@end deffn
258
259@node Syntax Flags
260@subsection Syntax Flags
261@cindex syntax flags
262
263 In addition to the classes, entries for characters in a syntax table
264can include flags. There are six possible flags, represented by the
265characters @samp{1}, @samp{2}, @samp{3}, @samp{4}, @samp{b} and
266@samp{p}.
267
268 All the flags except @samp{p} are used to describe multi-character
269comment delimiters. The digit flags indicate that a character can
270@emph{also} be part of a comment sequence, in addition to the syntactic
271properties associated with its character class. The flags are
272independent of the class and each other for the sake of characters such
273as @samp{*} in C mode, which is a punctuation character, @emph{and} the
274second character of a start-of-comment sequence (@samp{/*}), @emph{and}
275the first character of an end-of-comment sequence (@samp{*/}).
276
277The flags for a character @var{c} are:
278
279@itemize @bullet
280@item
281@samp{1} means @var{c} is the start of a two-character comment start
282sequence.
283
284@item
285@samp{2} means @var{c} is the second character of such a sequence.
286
287@item
288@samp{3} means @var{c} is the start of a two-character comment end
289sequence.
290
291@item
292@samp{4} means @var{c} is the second character of such a sequence.
293
294@item
295@c Emacs 19 feature
296@samp{b} means that @var{c} as a comment delimiter belongs to the
297alternative ``b'' comment style.
298
299Emacs supports two comment styles simultaneously in any one syntax
300table. This is for the sake of C++. Each style of comment syntax has
301its own comment-start sequence and its own comment-end sequence. Each
302comment must stick to one style or the other; thus, if it starts with
303the comment-start sequence of style ``b'', it must also end with the
304comment-end sequence of style ``b''.
305
306The two comment-start sequences must begin with the same character; only
307the second character may differ. Mark the second character of the
308``b''-style comment start sequence with the @samp{b} flag.
309
310A comment-end sequence (one or two characters) applies to the ``b''
311style if its first character has the @samp{b} flag set; otherwise, it
312applies to the ``a'' style.
313
314The appropriate comment syntax settings for C++ are as follows:
315
316@table @asis
317@item @samp{/}
318@samp{124b}
319@item @samp{*}
320@samp{23}
321@item newline
322@samp{>b}
323@end table
324
325Thus @samp{/*} is a comment-start sequence for ``a'' style, @samp{//}
326is a comment-start sequence for ``b'' style, @samp{*/} is a
327comment-end sequence for ``a'' style, and newline is a comment-end
328sequence for ``b'' style.
329
330@item
331@c Emacs 19 feature
332@samp{p} identifies an additional ``prefix character'' for Lisp syntax.
333These characters are treated as whitespace when they appear between
334expressions. When they appear within an expression, they are handled
335according to their usual syntax codes.
336
337The function @code{backward-prefix-chars} moves back over these
338characters, as well as over characters whose primary syntax class is
339prefix (@samp{'}). @xref{Motion and Syntax}.
340@end itemize
341
342@node Syntax Table Functions
343@section Syntax Table Functions
344
345 In this section we describe functions for creating, accessing and
346altering syntax tables.
347
348@defun make-syntax-table
349This function creates a new syntax table. Character codes 0 through
35031, and 128 through 255, are set up to inherit from the standard syntax
351table. The other character codes are set up by copying what the
352standard syntax table says about them.
353
354Most major mode syntax tables are created in this way.
355@end defun
356
357@defun copy-syntax-table &optional table
358This function constructs a copy of @var{table} and returns it. If
359@var{table} is not supplied (or is @code{nil}), it returns a copy of the
360current syntax table. Otherwise, an error is signaled if @var{table} is
361not a syntax table.
362@end defun
363
364@deffn Command modify-syntax-entry char syntax-descriptor &optional table
365This function sets the syntax entry for @var{char} according to
366@var{syntax-descriptor}. The syntax is changed only for @var{table},
367which defaults to the current buffer's syntax table, and not in any
368other syntax table. The argument @var{syntax-descriptor} specifies the
369desired syntax; this is a string beginning with a class designator
370character, and optionally containing a matching character and flags as
371well. @xref{Syntax Descriptors}.
372
373This function always returns @code{nil}. The old syntax information in
374the table for this character is discarded.
375
376An error is signaled if the first character of the syntax descriptor is not
377one of the twelve syntax class designator characters. An error is also
378signaled if @var{char} is not a character.
379
380@example
381@group
382@exdent @r{Examples:}
383
384;; @r{Put the space character in class whitespace.}
385(modify-syntax-entry ?\ " ")
386 @result{} nil
387@end group
388
389@group
390;; @r{Make @samp{$} an open parenthesis character,}
391;; @r{with @samp{^} as its matching close.}
392(modify-syntax-entry ?$ "(^")
393 @result{} nil
394@end group
395
396@group
397;; @r{Make @samp{^} a close parenthesis character,}
398;; @r{with @samp{$} as its matching open.}
399(modify-syntax-entry ?^ ")$")
400 @result{} nil
401@end group
402
403@group
404;; @r{Make @samp{/} a punctuation character,}
405;; @r{the first character of a start-comment sequence,}
406;; @r{and the second character of an end-comment sequence.}
407;; @r{This is used in C mode.}
408(modify-syntax-entry ?/ ".13")
409 @result{} nil
410@end group
411@end example
412@end deffn
413
414@defun char-syntax character
415This function returns the syntax class of @var{character}, represented
416by its mnemonic designator character. This @emph{only} returns the
417class, not any matching parenthesis or flags.
418
419An error is signaled if @var{char} is not a character.
420
421The following examples apply to C mode. The first example shows that
422the syntax class of space is whitespace (represented by a space). The
423second example shows that the syntax of @samp{/} is punctuation. This
424does not show the fact that it is also part of comment start and end
425sequence. The third example shows that open parenthesis is in the class
426of open parentheses. This does not show the fact that it has a matching
427character, @samp{)}.
428
429@example
430@group
431(char-to-string (char-syntax ?\ ))
432 @result{} " "
433@end group
434
435@group
436(char-to-string (char-syntax ?/))
437 @result{} "."
438@end group
439
440@group
441(char-to-string (char-syntax ?\())
442 @result{} "("
443@end group
444@end example
445@end defun
446
447@defun set-syntax-table table
448This function makes @var{table} the syntax table for the current buffer.
449It returns @var{table}.
450@end defun
451
452@defun syntax-table
453This function returns the current syntax table, which is the table for
454the current buffer.
455@end defun
456
457@node Motion and Syntax
458@section Motion and Syntax
459
460 This section describes functions for moving across characters in
461certain syntax classes. None of these functions exists in Emacs
462version 18 or earlier.
463
464@defun skip-syntax-forward syntaxes &optional limit
465This function moves point forward across characters having syntax classes
466mentioned in @var{syntaxes}. It stops when it encounters the end of
467the buffer, or position @var{lim} (if specified), or a character it is
468not supposed to skip.
469@ignore @c may want to change this.
470The return value is the distance traveled, which is a nonnegative
471integer.
472@end ignore
473@end defun
474
475@defun skip-syntax-backward syntaxes &optional limit
476This function moves point backward across characters whose syntax
477classes are mentioned in @var{syntaxes}. It stops when it encounters
478the beginning of the buffer, or position @var{lim} (if specified), or a
479character it is not supposed to skip.
480@ignore @c may want to change this.
481The return value indicates the distance traveled. It is an integer that
482is zero or less.
483@end ignore
484@end defun
485
486@defun backward-prefix-chars
487This function moves point backward over any number of characters with
488expression prefix syntax. This includes both characters in the
489expression prefix syntax class, and characters with the @samp{p} flag.
490@end defun
491
492@node Parsing Expressions
493@section Parsing Balanced Expressions
494
495 Here are several functions for parsing and scanning balanced
496expressions, also known as @dfn{sexps}, in which parentheses match in
497pairs. The syntax table controls the interpretation of characters, so
498these functions can be used for Lisp expressions when in Lisp mode and
499for C expressions when in C mode. @xref{List Motion}, for convenient
500higher-level functions for moving over balanced expressions.
501
502@defun parse-partial-sexp start limit &optional target-depth stop-before state stop-comment
503This function parses a sexp in the current buffer starting at
504@var{start}, not scanning past @var{limit}. It stops at @var{limit} or
505when certain criteria described below are met, and sets to the location
506where parsing stops. It returns a value describing the status of the
507parse at the point where it stops.
508
509If @var{state} is @code{nil}, @var{start} is assumed to be at the top
510level of parenthesis structure, such as the beginning of a function
511definition. Alternatively, you might wish to resume parsing in the
512middle of the structure. To do this, you must provide a @var{state}
513argument that describes the initial status of parsing.
514
515@cindex parenthesis depth
516If the third argument @var{target-depth} is non-@code{nil}, parsing
517stops if the depth in parentheses becomes equal to @var{target-depth}.
518The depth starts at 0, or at whatever is given in @var{state}.
519
520If the fourth argument @var{stop-before} is non-@code{nil}, parsing
521stops when it comes to any character that starts a sexp. If
522@var{stop-comment} is non-@code{nil}, parsing stops when it comes to the
523start of a comment.
524
525@cindex parse state
526The fifth argument @var{state} is an eight-element list of the same
527form as the value of this function, described below. The return value
528of one call may be used to initialize the state of the parse on another
529call to @code{parse-partial-sexp}.
530
531The result is a list of eight elements describing the final state of
532the parse:
533
534@enumerate 0
535@item
536The depth in parentheses, counting from 0.
537
538@item
539@cindex innermost containing parentheses
540The character position of the start of the innermost containing
541parenthetical grouping; @code{nil} if none.
542
543@item
544@cindex previous complete subexpression
545The character position of the start of the last complete subexpression
546terminated; @code{nil} if none.
547
548@item
549@cindex inside string
550Non-@code{nil} if inside a string. More precisely, this is the
551character that will terminate the string.
552
553@item
554@cindex inside comment
555@code{t} if inside a comment.
556
557@item
558@cindex quote character
559@code{t} if point is just after a quote character.
560
561@item
562The minimum parenthesis depth encountered during this scan.
563
564@item
565@code{t} if inside a comment of style ``b''.
566@end enumerate
567
568Elements 0, 3, 4, 5 and 7 are significant in the argument @var{state}.
569
570@cindex indenting with parentheses
571This function is most often used to compute indentation for languages
572that have nested parentheses.
573@end defun
574
575@defun scan-lists from count depth
576This function scans forward @var{count} balanced parenthetical groupings
577from character number @var{from}. It returns the character position
578where the scan stops.
579
580If @var{depth} is nonzero, parenthesis depth counting begins from that
581value. The only candidates for stopping are places where the depth in
582parentheses becomes zero; @code{scan-lists} counts @var{count} such
583places and then stops. Thus, a positive value for @var{depth} means go
584out levels of parenthesis.
585
586Scanning ignores comments if @code{parse-sexp-ignore-comments} is
587non-@code{nil}.
588
589If scan reaches the beginning or end of the buffer (or its accessible
590portion), and the depth is not zero, an error is signaled. If the depth
591is zero but the count is not used up, @code{nil} is returned.
592@end defun
593
594@defun scan-sexps from count
595This function scans forward @var{count} sexps from character position
596@var{from}. It returns the character position where the scan stops.
597
598Scanning ignores comments if @code{parse-sexp-ignore-comments} is
599non-@code{nil}.
600
601If scan reaches the beginning or end of (the accessible part of) the
602buffer in the middle of a parenthetical grouping, an error is signaled.
603If it reaches the beginning or end between groupings but before count is
604used up, @code{nil} is returned.
605@end defun
606
607@defvar parse-sexp-ignore-comments
608@cindex skipping comments
609If the value is non-@code{nil}, then comments are treated as
610whitespace by the functions in this section and by @code{forward-sexp}.
611
612In older Emacs versions, this feature worked only when the comment
613terminator is something like @samp{*/}, and appears only to end a
614comment. In languages where newlines terminate comments, it was
615necessary make this variable @code{nil}, since not every newline is the
616end of a comment. This limitation no longer exists.
617@end defvar
618
619You can use @code{forward-comment} to move forward or backward over
620one comment or several comments.
621
622@defun forward-comment count
623This function moves point forward across @var{count} comments (backward,
624if @var{count} is negative). If it finds anything other than a comment
625or whitespace, it stops, leaving point at the place where it stopped.
626It also stops after satisfying @var{count}.
627@end defun
628
629To move forward over all comments and whitespace following point, use
630@code{(forward-comment (buffer-size))}. @code{(buffer-size)} is a good
631argument to use, because the number of comments to in the buffer cannot
632exceed that many.
633
634@node Standard Syntax Tables
635@section Some Standard Syntax Tables
636
637 Each of the major modes in Emacs has its own syntax table. Here are
638several of them:
639
640@defun standard-syntax-table
641This function returns the standard syntax table, which is the syntax
642table used in Fundamental mode.
643@end defun
644
645@defvar text-mode-syntax-table
646The value of this variable is the syntax table used in Text mode.
647@end defvar
648
649@defvar c-mode-syntax-table
650The value of this variable is the syntax table for C-mode buffers.
651@end defvar
652
653@defvar emacs-lisp-mode-syntax-table
654The value of this variable is the syntax table used in Emacs Lisp mode
655by editing commands. (It has no effect on the Lisp @code{read}
656function.)
657@end defvar
658
659@node Syntax Table Internals
660@section Syntax Table Internals
661@cindex syntax table internals
662
663 Each element of a syntax table is an integer that encodes the syntax
664of one character: the syntax class, possible matching character, and
665flags. Lisp programs don't usually work with the elements directly; the
666Lisp-level syntax table functions usually work with syntax descriptors
667(@pxref{Syntax Descriptors}).
668
669 The low 8 bits of each element of a syntax table indicate the
670syntax class.
671
672@table @asis
673@item @i{Integer}
674@i{Class}
675@item 0
676whitespace
677@item 1
678punctuation
679@item 2
680word
681@item 3
682symbol
683@item 4
684open parenthesis
685@item 5
686close parenthesis
687@item 6
688expression prefix
689@item 7
690string quote
691@item 8
692paired delimiter
693@item 9
694escape
695@item 10
696character quote
697@item 11
698comment-start
699@item 12
700comment-end
701@item 13
702inherit
703@end table
704
705 The next 8 bits are the matching opposite parenthesis (if the
706character has parenthesis syntax); otherwise, they are not meaningful.
707The next 6 bits are the flags.
diff --git a/lispref/tips.texi b/lispref/tips.texi
new file mode 100644
index 00000000000..e917e75f463
--- /dev/null
+++ b/lispref/tips.texi
@@ -0,0 +1,582 @@
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/tips
6@node Tips, GNU Emacs Internals, Calendar, Top
7@appendix Tips and Standards
8@cindex tips
9@cindex standards of coding style
10@cindex coding standards
11
12 This chapter describes no additional features of Emacs Lisp.
13Instead it gives advice on making effective use of the features described
14in the previous chapters.
15
16@menu
17* Style Tips:: Writing clean and robust programs.
18* Compilation Tips:: Making compiled code run fast.
19* Documentation Tips:: Writing readable documentation strings.
20* Comment Tips:: Conventions for writing comments.
21* Library Headers:: Standard headers for library packages.
22@end menu
23
24@node Style Tips
25@section Writing Clean Lisp Programs
26
27 Here are some tips for avoiding common errors in writing Lisp code
28intended for widespread use:
29
30@itemize @bullet
31@item
32Since all global variables share the same name space, and all functions
33share another name space, you should choose a short word to distinguish
34your program from other Lisp programs. Then take care to begin the
35names of all global variables, constants, and functions with the chosen
36prefix. This helps avoid name conflicts.
37
38This recommendation applies even to names for traditional Lisp
39primitives that are not primitives in Emacs Lisp---even to @code{cadr}.
40Believe it or not, there is more than one plausible way to define
41@code{cadr}. Play it safe; append your name prefix to produce a name
42like @code{foo-cadr} or @code{mylib-cadr} instead.
43
44If you write a function that you think ought to be added to Emacs under
45a certain name, such as @code{twiddle-files}, don't call it by that name
46in your program. Call it @code{mylib-twiddle-files} in your program,
47and send mail to @samp{bug-gnu-emacs@@prep.ai.mit.edu} suggesting we add
48it to Emacs. If and when we do, we can change the name easily enough.
49
50If one prefix is insufficient, your package may use two or three
51alternative common prefixes, so long as they make sense.
52
53Separate the prefix from the rest of the symbol name with a hyphen,
54@samp{-}. This will be consistent with Emacs itself and with most Emacs
55Lisp programs.
56
57@item
58It is often useful to put a call to @code{provide} in each separate
59library program, at least if there is more than one entry point to the
60program.
61
62@item
63If one file @var{foo} uses a macro defined in another file @var{bar},
64@var{foo} should contain @code{(require '@var{bar})} before the first
65use of the macro. (And @var{bar} should contain @code{(provide
66'@var{bar})}, to make the @code{require} work.) This will cause
67@var{bar} to be loaded when you byte-compile @var{foo}. Otherwise, you
68risk compiling @var{foo} without the necessary macro loaded, and that
69would produce compiled code that won't work right. @xref{Compiling
70Macros}.
71
72@item
73If you define a major mode, make sure to run a hook variable using
74@code{run-hooks}, just as the existing major modes do. @xref{Hooks}.
75
76@item
77Please do not define @kbd{C-c @var{letter}} as a key in your major
78modes. These sequences are reserved for users; they are the
79@strong{only} sequences reserved for users, so we cannot do without
80them.
81
82Instead, define sequences consisting of @kbd{C-c} followed by a
83non-letter. These sequences are reserved for major modes.
84
85Changing all the major modes in Emacs 18 so they would follow this
86convention was a lot of work. Abandoning this convention would waste
87that work and inconvenience the users.
88
89@item
90You should not bind @kbd{C-h} following any prefix character (including
91@kbd{C-c}). If you don't bind @kbd{C-h}, it is automatically available
92as a help character for listing the subcommands of the prefix character.
93
94@item
95You should not bind a key sequence ending in @key{ESC} except following
96another @key{ESC}. (That is, it is ok to bind a sequence ending in
97@kbd{@key{ESC} @key{ESC}}.)
98
99The reason for this rule is that a non-prefix binding for @key{ESC} in
100any context prevents recognition of escape sequences as function keys in
101that context.
102
103@item
104It is a bad idea to define aliases for the Emacs primitives.
105Use the standard names instead.
106
107@item
108Redefining an Emacs primitive is an even worse idea.
109It may do the right thing for a particular program, but
110there is no telling what other programs might break as a result.
111
112@item
113If a file does replace any of the functions or library programs of
114standard Emacs, prominent comments at the beginning of the file should
115say which functions are replaced, and how the behavior of the
116replacements differs from that of the originals.
117
118@item
119If a file requires certain standard library programs to be loaded
120beforehand, then the comments at the beginning of the file should say
121so.
122
123@item
124Please keep the names of your Emacs Lisp source files to 13 characters
125or less. This way, if the files are compiled, the compiled files' names
126will be 14 characters or less, which is short enough to fit on all kinds
127of Unix systems.
128
129@item
130Don't use @code{next-line} or @code{previous-line} in programs; nearly
131always, @code{forward-line} is more convenient as well as more
132predictable and robust. @xref{Text Lines}.
133
134@item
135Don't use functions that set the mark in your Lisp code (unless you are
136writing a command to set the mark). The mark is a user-level feature,
137so it is incorrect to change the mark except to supply a value for the
138user's benefit. @xref{The Mark}.
139
140In particular, don't use these functions:
141
142@itemize @bullet
143@item
144@code{beginning-of-buffer}, @code{end-of-buffer}
145@item
146@code{replace-string}, @code{replace-regexp}
147@end itemize
148
149If you just want to move point, or replace a certain string, without any
150of the other features intended for interactive users, you can replace
151these functions with one or two lines of simple Lisp code.
152
153@item
154The recommended way to print a message in the echo area is with
155the @code{message} function, not @code{princ}. @xref{The Echo Area}.
156
157@item
158When you encounter an error condition, call the function @code{error}
159(or @code{signal}). The function @code{error} does not return.
160@xref{Signaling Errors}.
161
162Do not use @code{message}, @code{throw}, @code{sleep-for},
163or @code{beep} to report errors.
164
165@item
166Avoid using recursive edits. Instead, do what the Rmail @kbd{w} command
167does: use a new local keymap that contains one command defined to
168switch back to the old local keymap. Or do what the @code{edit-options}
169command does: switch to another buffer and let the user switch back at
170will. @xref{Recursive Editing}.
171
172@item
173In some other systems there is a convention of choosing variable names
174that begin and end with @samp{*}. We don't use that convention in Emacs
175Lisp, so please don't use it in your library. (In fact, in Emacs names
176of this form are conventionally used for program-generated buffers.) The
177users will find Emacs more coherent if all libraries use the same
178conventions.
179
180@item
181Indent each function with @kbd{C-M-q} (@code{indent-sexp}) using the
182default indentation parameters.
183
184@item
185Don't make a habit of putting close-parentheses on lines by themselves;
186Lisp programmers find this disconcerting. Once in a while, when there
187is a sequence of many consecutive close-parentheses, it may make sense
188to split them in one or two significant places.
189
190@item
191Please put a copyright notice on the file if you give copies to anyone.
192Use the same lines that appear at the top of the Lisp files in Emacs
193itself. If you have not signed papers to assign the copyright to the
194Foundation, then place your name in the copyright notice in place of the
195Foundation's name.
196@end itemize
197
198@node Compilation Tips
199@section Tips for Making Compiled Code Fast
200@cindex execution speed
201@cindex speedups
202
203 Here are ways of improving the execution speed of byte-compiled
204lisp programs.
205
206@itemize @bullet
207@item
208@cindex profiling
209@cindex timing programs
210@cindex @file{profile.el}
211Use the @file{profile} library to profile your program. See the file
212@file{profile.el} for instructions.
213
214@item
215Use iteration rather than recursion whenever possible.
216Function calls are slow in Emacs Lisp even when a compiled function
217is calling another compiled function.
218
219@item
220Using the primitive list-searching functions @code{memq}, @code{assq} or
221@code{assoc} is even faster than explicit iteration. It may be worth
222rearranging a data structure so that one of these primitive search
223functions can be used.
224
225@item
226Certain built-in functions are handled specially by the byte compiler
227avoiding the need for an ordinary function call. It is a good idea to
228use these functions rather than alternatives. To see whether a function
229is handled specially by the compiler, examine its @code{byte-compile}
230property. If the property is non-@code{nil}, then the function is
231handled specially.
232
233For example, the following input will show you that @code{aref} is
234compiled specially (@pxref{Array Functions}) while @code{elt} is not
235(@pxref{Sequence Functions}):
236
237@smallexample
238@group
239(get 'aref 'byte-compile)
240 @result{} byte-compile-two-args
241@end group
242
243@group
244(get 'elt 'byte-compile)
245 @result{} nil
246@end group
247@end smallexample
248
249@item
250If calling a small function accounts for a substantial part of your
251program's running time, make the function inline. This eliminates
252the function call overhead. Since making a function inline reduces
253the flexibility of changing the program, don't do it unless it gives
254a noticeable speedup in something slow enough for users to care about
255the speed. @xref{Inline Functions}.
256@end itemize
257
258@node Documentation Tips
259@section Tips for Documentation Strings
260
261 Here are some tips for the writing of documentation strings.
262
263@itemize @bullet
264@item
265Every command, function or variable intended for users to know about
266should have a documentation string.
267
268@item
269An internal subroutine of a Lisp program need not have a documentation
270string, and you can save space by using a comment instead.
271
272@item
273The first line of the documentation string should consist of one or two
274complete sentences which stand on their own as a summary. In particular,
275start the line with a capital letter and end with a period.
276For instance, use ``Return the cons of A and B.'' in preference to
277``Returns the cons of A and B@.''
278
279The documentation string can have additional lines which expand on the
280details of how to use the function or variable. The additional lines
281should be made up of complete sentences also, but they may be filled if
282that looks good.
283
284@item
285Write documentation strings in the active voice, not the passive, and in
286the present tense, not the future. For instance, use ``Return a list
287containing A and B.'' instead of ``A list containing A and B will be
288returned.''
289
290@item
291Avoid using the word ``cause'' (or its equivalents) unnecessarily.
292Instead of, ``Cause Emacs to display text in boldface,'' write just
293``Display text in boldface.''
294
295@item
296Do not start or end a documentation string with whitespace.
297
298@item
299Format the documentation string so that it fits in an Emacs window on an
30080 column screen. It is a good idea for most lines to be no wider than
30160 characters. The first line can be wider if necessary to fit the
302information that ought to be there.
303
304However, rather than simply filling the entire documentation string, you
305can make it much more readable by choosing line breaks with care.
306Use blank lines between topics if the documentation string is long.
307
308@item
309@strong{Do not} indent subsequent lines of a documentation string so
310that the text is lined up in the source code with the text of the first
311line. This looks nice in the source code, but looks bizarre when users
312view the documentation. Remember that the indentation before the
313starting double-quote is not part of the string!
314
315@item
316A variable's documentation string should start with @samp{*} if the
317variable is one that users would want to set interactively often. If
318the value is a long list, or a function, or if the variable would only
319be set in init files, then don't start the documentation string with
320@samp{*}. @xref{Defining Variables}.
321
322@item
323The documentation string for a variable that is a yes-or-no flag should
324start with words such as ``Non-nil means@dots{}'', to make it clear both
325that the variable only has two meaningfully distinct values and which value
326means ``yes''.
327
328@item
329When a function's documentation string mentions the value of an argument
330of the function, use the argument name in capital letters as if it were
331a name for that value. Thus, the documentation string of the function
332@code{/} refers to its second argument as @samp{DIVISOR}.
333
334Also use all caps for meta-syntactic variables, such as when you show
335the decomposition of a list or vector into subunits, some of which may
336vary.
337
338@item
339@iftex
340When a documentation string refers to a Lisp symbol, write it as it
341would be printed (which usually means in lower case), with single-quotes
342around it. For example: @samp{`lambda'}. There are two exceptions:
343write @code{t} and @code{nil} without single-quotes.
344@end iftex
345@ifinfo
346When a documentation string refers to a Lisp symbol, write it as it
347would be printed (which usually means in lower case), with single-quotes
348around it. For example: @samp{lambda}. There are two exceptions: write
349t and nil without single-quotes. (In this manual, we normally do use
350single-quotes for those symbols.)
351@end ifinfo
352
353@item
354Don't write key sequences directly in documentation strings. Instead,
355use the @samp{\\[@dots{}]} construct to stand for them. For example,
356instead of writing @samp{C-f}, write @samp{\\[forward-char]}. When the
357documentation string is printed, Emacs will substitute whatever key is
358currently bound to @code{forward-char}. This will usually be
359@samp{C-f}, but if the user has moved key bindings, it will be the
360correct key for that user. @xref{Keys in Documentation}.
361
362@item
363In documentation strings for a major mode, you will want to refer to the
364key bindings of that mode's local map, rather than global ones.
365Therefore, use the construct @samp{\\<@dots{}>} once in the
366documentation string to specify which key map to use. Do this before
367the first use of @samp{\\[@dots{}]}. The text inside the
368@samp{\\<@dots{}>} should be the name of the variable containing the
369local keymap for the major mode.
370
371It is not practical to use @samp{\\[@dots{}]} very many times, because
372display of the documentation string will become slow. So use this to
373describe the most important commands in your major mode, and then use
374@samp{\\@{@dots{}@}} to display the rest of the mode's keymap.
375
376@item
377Don't use the term ``Elisp'', since that is or was a trademark.
378Use the term ``Emacs Lisp''.
379@end itemize
380
381@node Comment Tips
382@section Tips on Writing Comments
383
384 We recommend these conventions for where to put comments and how to
385indent them:
386
387@table @samp
388@item ;
389Comments that start with a single semicolon, @samp{;}, should all be
390aligned to the same column on the right of the source code. Such
391comments usually explain how the code on the same line does its job. In
392Lisp mode and related modes, the @kbd{M-;} (@code{indent-for-comment})
393command automatically inserts such a @samp{;} in the right place, or
394aligns such a comment if it is already inserted.
395
396(The following examples are taken from the Emacs sources.)
397
398@smallexample
399@group
400(setq base-version-list ; there was a base
401 (assoc (substring fn 0 start-vn) ; version to which
402 file-version-assoc-list)) ; this looks like
403 ; a subversion
404@end group
405@end smallexample
406
407@item ;;
408Comments that start with two semicolons, @samp{;;}, should be aligned to
409the same level of indentation as the code. Such comments are used to
410describe the purpose of the following lines or the state of the program
411at that point. For example:
412
413@smallexample
414@group
415(prog1 (setq auto-fill-function
416 @dots{}
417 @dots{}
418 ;; update mode-line
419 (force-mode-line-update)))
420@end group
421@end smallexample
422
423These comments are also written before a function definition to explain
424what the function does and how to call it properly.
425
426@item ;;;
427Comments that start with three semicolons, @samp{;;;}, should start at
428the left margin. Such comments are not used within function
429definitions, but are used to make more general comments. For example:
430
431@smallexample
432@group
433;;; This Lisp code is run in Emacs
434;;; when it is to operate as a server
435;;; for other processes.
436@end group
437@end smallexample
438
439@item ;;;;
440Comments that start with four semicolons, @samp{;;;;}, should be aligned
441to the left margin and are used for headings of major sections of a
442program. For example:
443
444@smallexample
445;;;; The kill ring
446@end smallexample
447@end table
448
449@noindent
450The indentation commands of the Lisp modes in Emacs, such as @kbd{M-;}
451(@code{indent-for-comment}) and @key{TAB} (@code{lisp-indent-line})
452automatically indent comments according to these conventions,
453depending on the the number of semicolons. @xref{Comments,,
454Manipulating Comments, emacs, The GNU Emacs Manual}.
455
456 If you wish to ``comment out'' a number of lines of code, use triple
457semicolons at the beginnings of the lines.
458
459 Any character may be included in a comment, but it is advisable to
460precede a character with syntactic significance in Lisp (such as
461@samp{\} or unpaired @samp{(} or @samp{)}) with a @samp{\}, to prevent
462it from confusing the Emacs commands for editing Lisp.
463
464@node Library Headers
465@section Conventional Headers for Emacs Libraries
466@cindex header comments
467@cindex library header comments
468
469 Emacs 19 has conventions for using special comments in Lisp libraries
470to divide them into sections and give information such as who wrote
471them. This section explains these conventions. First, an example:
472
473@smallexample
474@group
475;;; lisp-mnt.el --- minor mode for Emacs Lisp maintainers
476
477;; Copyright (C) 1992 Free Software Foundation, Inc.
478@end group
479
480;; Author: Eric S. Raymond <esr@@snark.thyrsus.com>
481;; Maintainer: Eric S. Raymond <esr@@snark.thyrsus.com>
482;; Created: 14 Jul 1992
483;; Version: 1.2
484@group
485;; Keywords: docs
486
487;; This file is part of GNU Emacs.
488@var{copying conditions}@dots{}
489@end group
490@end smallexample
491
492 The very first line should have this format:
493
494@example
495;;; @var{filename} --- @var{description}
496@end example
497
498@noindent
499The description should be complete in one line.
500
501 After the copyright notice come several @dfn{header comment} lines,
502each beginning with @samp{;;; @var{header-name}:}. Here is a table of
503the conventional possibilities for @var{header-name}:
504
505@table @samp
506@item Author
507This line states the name and net address of at least the principal
508author of the library.
509
510If there are multiple authors, you can list them on continuation lines
511led by @code{;;<TAB>}, like this:
512
513@smallexample
514@group
515;; Author: Ashwin Ram <Ram-Ashwin@@cs.yale.edu>
516;; Dave Sill <de5@@ornl.gov>
517;; Dave Brennan <brennan@@hal.com>
518;; Eric Raymond <esr@@snark.thyrsus.com>
519@end group
520@end smallexample
521
522@item Maintainer
523This line should contain a single name/address as in the Author line, or
524an address only, or the string ``FSF''. If there is no maintainer line,
525the person(s) in the Author field are presumed to be the maintainers.
526The example above is mildly bogus because the maintainer line is
527redundant.
528
529The idea behind the @samp{Author} and @samp{Maintainer} lines is to make
530possible a Lisp function to ``send mail to the maintainer'' without
531having to mine the name out by hand.
532
533Be sure to surround the network address with @samp{<@dots{}>} if
534you include the person's full name as well as the network address.
535
536@item Created
537This optional line gives the original creation date of the
538file. For historical interest only.
539
540@item Version
541If you wish to record version numbers for the individual Lisp program, put
542them in this line.
543
544@item Adapted-By
545In this header line, place the name of the person who adapted the
546library for installation (to make it fit the style conventions, for
547example).
548
549@item Keywords
550This line lists keywords for the @code{finder-by-keyword} help command.
551This field is important; it's how people will find your package when
552they're looking for things by topic area.
553@end table
554
555 Just about every Lisp library ought to have the @samp{Author} and
556@samp{Keywords} header comment lines. Use the others if they are
557appropriate. You can also put in header lines with other header
558names---they have no standard meanings, so they can't do any harm.
559
560 We use additional stylized comments to subdivide the contents of the
561library file. Here is a table of them:
562
563@table @samp
564@item ;;; Commentary:
565This begins introductory comments that explain how the library works.
566It should come right after the copying permissions.
567
568@item ;;; Change log:
569This begins change log information stored in the library file (if you
570store the change history there). For most of the Lisp
571files distributed with Emacs, the change history is kept in the file
572@file{ChangeLog} and not in the source file at all; these files do
573not have a @samp{;;; Change log:} line.
574
575@item ;;; Code:
576This begins the actual code of the program.
577
578@item ;;; @var{filename} ends here
579This is the @dfn{footer line}; it appears at the very end of the file.
580Its purpose is to enable people to detect truncated versions of the file
581from the lack of a footer line.
582@end table