]> code.delx.au - gnu-emacs/blob - doc/lispref/syntax.texi
8d91db01ad39f4dcebf97bc898f292011ec58cde
[gnu-emacs] / doc / lispref / syntax.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2001,
4 @c 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 @c Free Software Foundation, Inc.
6 @c See the file elisp.texi for copying conditions.
7 @setfilename ../../info/syntax
8 @node Syntax Tables, Abbrevs, Searching and Matching, Top
9 @chapter Syntax Tables
10 @cindex parsing buffer text
11 @cindex syntax table
12 @cindex text parsing
13
14 A @dfn{syntax table} specifies the syntactic textual function of each
15 character. This information is used by the @dfn{parsing functions}, the
16 complex movement commands, and others to determine where words, symbols,
17 and other syntactic constructs begin and end. The current syntax table
18 controls the meaning of the word motion functions (@pxref{Word Motion})
19 and the list motion functions (@pxref{List Motion}), as well as the
20 functions in this chapter.
21
22 @menu
23 * Basics: Syntax Basics. Basic concepts of syntax tables.
24 * Desc: Syntax Descriptors. How characters are classified.
25 * Syntax Table Functions:: How to create, examine and alter syntax tables.
26 * Syntax Properties:: Overriding syntax with text properties.
27 * Motion and Syntax:: Moving over characters with certain syntaxes.
28 * Parsing Expressions:: Parsing balanced expressions
29 using the syntax table.
30 * Standard Syntax Tables:: Syntax tables used by various major modes.
31 * Syntax Table Internals:: How syntax table information is stored.
32 * Categories:: Another way of classifying character syntax.
33 @end menu
34
35 @node Syntax Basics
36 @section Syntax Table Concepts
37
38 @ifnottex
39 A @dfn{syntax table} provides Emacs with the information that
40 determines the syntactic use of each character in a buffer. This
41 information is used by the parsing commands, the complex movement
42 commands, and others to determine where words, symbols, and other
43 syntactic constructs begin and end. The current syntax table controls
44 the meaning of the word motion functions (@pxref{Word Motion}) and the
45 list motion functions (@pxref{List Motion}) as well as the functions in
46 this chapter.
47 @end ifnottex
48
49 A syntax table is a char-table (@pxref{Char-Tables}). The element at
50 index @var{c} describes the character with code @var{c}. The element's
51 value should be a list that encodes the syntax of the character in
52 question.
53
54 Syntax tables are used only for moving across text, not for the Emacs
55 Lisp reader. Emacs Lisp uses built-in syntactic rules when reading Lisp
56 expressions, and these rules cannot be changed. (Some Lisp systems
57 provide ways to redefine the read syntax, but we decided to leave this
58 feature out of Emacs Lisp for simplicity.)
59
60 Each buffer has its own major mode, and each major mode has its own
61 idea of the syntactic class of various characters. For example, in Lisp
62 mode, the character @samp{;} begins a comment, but in C mode, it
63 terminates a statement. To support these variations, Emacs makes the
64 choice of syntax table local to each buffer. Typically, each major
65 mode has its own syntax table and installs that table in each buffer
66 that uses that mode. Changing this table alters the syntax in all
67 those buffers as well as in any buffers subsequently put in that mode.
68 Occasionally several similar modes share one syntax table.
69 @xref{Example Major Modes}, for an example of how to set up a syntax
70 table.
71
72 A syntax table can inherit the data for some characters from the
73 standard syntax table, while specifying other characters itself. The
74 ``inherit'' syntax class means ``inherit this character's syntax from
75 the standard syntax table.'' Just changing the standard syntax for a
76 character affects all syntax tables that inherit from it.
77
78 @defun syntax-table-p object
79 This function returns @code{t} if @var{object} is a syntax table.
80 @end defun
81
82 @node Syntax Descriptors
83 @section Syntax Descriptors
84 @cindex syntax class
85
86 This section describes the syntax classes and flags that denote the
87 syntax of a character, and how they are represented as a @dfn{syntax
88 descriptor}, which is a Lisp string that you pass to
89 @code{modify-syntax-entry} to specify the syntax you want.
90
91 The syntax table specifies a syntax class for each character. There
92 is no necessary relationship between the class of a character in one
93 syntax table and its class in any other table.
94
95 Each class is designated by a mnemonic character, which serves as the
96 name of the class when you need to specify a class. Usually the
97 designator character is one that is often assigned that class; however,
98 its meaning as a designator is unvarying and independent of what syntax
99 that character currently has. Thus, @samp{\} as a designator character
100 always gives ``escape character'' syntax, regardless of what syntax
101 @samp{\} currently has.
102
103 @cindex syntax descriptor
104 A syntax descriptor is a Lisp string that specifies a syntax class, a
105 matching character (used only for the parenthesis classes) and flags.
106 The first character is the designator for a syntax class. The second
107 character is the character to match; if it is unused, put a space there.
108 Then come the characters for any desired flags. If no matching
109 character or flags are needed, one character is sufficient.
110
111 For example, the syntax descriptor for the character @samp{*} in C
112 mode is @samp{@w{. 23}} (i.e., punctuation, matching character slot
113 unused, second character of a comment-starter, first character of a
114 comment-ender), and the entry for @samp{/} is @samp{@w{. 14}} (i.e.,
115 punctuation, matching character slot unused, first character of a
116 comment-starter, second character of a comment-ender).
117
118 @menu
119 * Syntax Class Table:: Table of syntax classes.
120 * Syntax Flags:: Additional flags each character can have.
121 @end menu
122
123 @node Syntax Class Table
124 @subsection Table of Syntax Classes
125
126 Here is a table of syntax classes, the characters that stand for them,
127 their meanings, and examples of their use.
128
129 @deffn {Syntax class} @w{whitespace character}
130 @dfn{Whitespace characters} (designated by @w{@samp{@ }} or @samp{-})
131 separate symbols and words from each other. Typically, whitespace
132 characters have no other syntactic significance, and multiple whitespace
133 characters are syntactically equivalent to a single one. Space, tab,
134 newline and formfeed are classified as whitespace in almost all major
135 modes.
136 @end deffn
137
138 @deffn {Syntax class} @w{word constituent}
139 @dfn{Word constituents} (designated by @samp{w}) are parts of words in
140 human languages, and are typically used in variable and command names
141 in programs. All upper- and lower-case letters, and the digits, are
142 typically word constituents.
143 @end deffn
144
145 @deffn {Syntax class} @w{symbol constituent}
146 @dfn{Symbol constituents} (designated by @samp{_}) are the extra
147 characters that are used in variable and command names along with word
148 constituents. For example, the symbol constituents class is used in
149 Lisp mode to indicate that certain characters may be part of symbol
150 names even though they are not part of English words. These characters
151 are @samp{$&*+-_<>}. In standard C, the only non-word-constituent
152 character that is valid in symbols is underscore (@samp{_}).
153 @end deffn
154
155 @deffn {Syntax class} @w{punctuation character}
156 @dfn{Punctuation characters} (designated by @samp{.}) are those
157 characters that are used as punctuation in English, or are used in some
158 way in a programming language to separate symbols from one another.
159 Some programming language modes, such as Emacs Lisp mode, have no
160 characters in this class since the few characters that are not symbol or
161 word constituents all have other uses. Other programming language modes,
162 such as C mode, use punctuation syntax for operators.
163 @end deffn
164
165 @deffn {Syntax class} @w{open parenthesis character}
166 @deffnx {Syntax class} @w{close parenthesis character}
167 @cindex parenthesis syntax
168 Open and close @dfn{parenthesis characters} are characters used in
169 dissimilar pairs to surround sentences or expressions. Such a grouping
170 is begun with an open parenthesis character and terminated with a close.
171 Each open parenthesis character matches a particular close parenthesis
172 character, and vice versa. Normally, Emacs indicates momentarily the
173 matching open parenthesis when you insert a close parenthesis.
174 @xref{Blinking}.
175
176 The class of open parentheses is designated by @samp{(}, and that of
177 close parentheses by @samp{)}.
178
179 In English text, and in C code, the parenthesis pairs are @samp{()},
180 @samp{[]}, and @samp{@{@}}. In Emacs Lisp, the delimiters for lists and
181 vectors (@samp{()} and @samp{[]}) are classified as parenthesis
182 characters.
183 @end deffn
184
185 @deffn {Syntax class} @w{string quote}
186 @dfn{String quote characters} (designated by @samp{"}) are used in
187 many languages, including Lisp and C, to delimit string constants. The
188 same string quote character appears at the beginning and the end of a
189 string. Such quoted strings do not nest.
190
191 The parsing facilities of Emacs consider a string as a single token.
192 The usual syntactic meanings of the characters in the string are
193 suppressed.
194
195 The Lisp modes have two string quote characters: double-quote (@samp{"})
196 and vertical bar (@samp{|}). @samp{|} is not used in Emacs Lisp, but it
197 is used in Common Lisp. C also has two string quote characters:
198 double-quote for strings, and single-quote (@samp{'}) for character
199 constants.
200
201 English text has no string quote characters because English is not a
202 programming language. Although quotation marks are used in English,
203 we do not want them to turn off the usual syntactic properties of
204 other characters in the quotation.
205 @end deffn
206
207 @deffn {Syntax class} @w{escape-syntax character}
208 An @dfn{escape character} (designated by @samp{\}) starts an escape
209 sequence such as is used in C string and character constants. The
210 character @samp{\} belongs to this class in both C and Lisp. (In C, it
211 is used thus only inside strings, but it turns out to cause no trouble
212 to treat it this way throughout C code.)
213
214 Characters in this class count as part of words if
215 @code{words-include-escapes} is non-@code{nil}. @xref{Word Motion}.
216 @end deffn
217
218 @deffn {Syntax class} @w{character quote}
219 A @dfn{character quote character} (designated by @samp{/}) quotes the
220 following character so that it loses its normal syntactic meaning. This
221 differs from an escape character in that only the character immediately
222 following is ever affected.
223
224 Characters in this class count as part of words if
225 @code{words-include-escapes} is non-@code{nil}. @xref{Word Motion}.
226
227 This class is used for backslash in @TeX{} mode.
228 @end deffn
229
230 @deffn {Syntax class} @w{paired delimiter}
231 @dfn{Paired delimiter characters} (designated by @samp{$}) are like
232 string quote characters except that the syntactic properties of the
233 characters between the delimiters are not suppressed. Only @TeX{} mode
234 uses a paired delimiter presently---the @samp{$} that both enters and
235 leaves math mode.
236 @end deffn
237
238 @deffn {Syntax class} @w{expression prefix}
239 An @dfn{expression prefix operator} (designated by @samp{'}) is used for
240 syntactic operators that are considered as part of an expression if they
241 appear next to one. In Lisp modes, these characters include the
242 apostrophe, @samp{'} (used for quoting), the comma, @samp{,} (used in
243 macros), and @samp{#} (used in the read syntax for certain data types).
244 @end deffn
245
246 @deffn {Syntax class} @w{comment starter}
247 @deffnx {Syntax class} @w{comment ender}
248 @cindex comment syntax
249 The @dfn{comment starter} and @dfn{comment ender} characters are used in
250 various languages to delimit comments. These classes are designated
251 by @samp{<} and @samp{>}, respectively.
252
253 English text has no comment characters. In Lisp, the semicolon
254 (@samp{;}) starts a comment and a newline or formfeed ends one.
255 @end deffn
256
257 @deffn {Syntax class} @w{inherit standard syntax}
258 This syntax class does not specify a particular syntax. It says to look
259 in the standard syntax table to find the syntax of this character. The
260 designator for this syntax class is @samp{@@}.
261 @end deffn
262
263 @deffn {Syntax class} @w{generic comment delimiter}
264 A @dfn{generic comment delimiter} (designated by @samp{!}) starts
265 or ends a special kind of comment. @emph{Any} generic comment delimiter
266 matches @emph{any} generic comment delimiter, but they cannot match
267 a comment starter or comment ender; generic comment delimiters can only
268 match each other.
269
270 This syntax class is primarily meant for use with the
271 @code{syntax-table} text property (@pxref{Syntax Properties}). You can
272 mark any range of characters as forming a comment, by giving the first
273 and last characters of the range @code{syntax-table} properties
274 identifying them as generic comment delimiters.
275 @end deffn
276
277 @deffn {Syntax class} @w{generic string delimiter}
278 A @dfn{generic string delimiter} (designated by @samp{|}) starts or ends
279 a string. This class differs from the string quote class in that @emph{any}
280 generic string delimiter can match any other generic string delimiter; but
281 they do not match ordinary string quote characters.
282
283 This syntax class is primarily meant for use with the
284 @code{syntax-table} text property (@pxref{Syntax Properties}). You can
285 mark any range of characters as forming a string constant, by giving the
286 first and last characters of the range @code{syntax-table} properties
287 identifying them as generic string delimiters.
288 @end deffn
289
290 @node Syntax Flags
291 @subsection Syntax Flags
292 @cindex syntax flags
293
294 In addition to the classes, entries for characters in a syntax table
295 can specify flags. There are eight possible flags, represented by the
296 characters @samp{1}, @samp{2}, @samp{3}, @samp{4}, @samp{b}, @samp{c},
297 @samp{n}, and @samp{p}.
298
299 All the flags except @samp{p} are used to describe comment
300 delimiters. The digit flags are used for comment delimiters made up
301 of 2 characters. They indicate that a character can @emph{also} be
302 part of a comment sequence, in addition to the syntactic properties
303 associated with its character class. The flags are independent of the
304 class and each other for the sake of characters such as @samp{*} in
305 C mode, which is a punctuation character, @emph{and} the second
306 character of a start-of-comment sequence (@samp{/*}), @emph{and} the
307 first character of an end-of-comment sequence (@samp{*/}). The flags
308 @samp{b}, @samp{c}, and @samp{n} are used to qualify the corresponding
309 comment delimiter.
310
311 Here is a table of the possible flags for a character @var{c},
312 and what they mean:
313
314 @itemize @bullet
315 @item
316 @samp{1} means @var{c} is the start of a two-character comment-start
317 sequence.
318
319 @item
320 @samp{2} means @var{c} is the second character of such a sequence.
321
322 @item
323 @samp{3} means @var{c} is the start of a two-character comment-end
324 sequence.
325
326 @item
327 @samp{4} means @var{c} is the second character of such a sequence.
328
329 @item
330 @samp{b} means that @var{c} as a comment delimiter belongs to the
331 alternative ``b'' comment style. For a two-character comment starter,
332 this flag is only significant on the second char, and for a 2-character
333 comment ender it is only significant on the first char.
334
335 @item
336 @samp{c} means that @var{c} as a comment delimiter belongs to the
337 alternative ``c'' comment style. For a two-character comment
338 delimiter, @samp{c} on either character makes it of style ``c''.
339
340 @item
341 @samp{n} on a comment delimiter character specifies
342 that this kind of comment can be nested. For a two-character
343 comment delimiter, @samp{n} on either character makes it
344 nestable.
345
346 Emacs supports several comment styles simultaneously in any one syntax
347 table. A comment style is a set of flags @samp{b}, @samp{c}, and
348 @samp{n}, so there can be up to 8 different comment styles.
349 Each comment delimiter has a style and only matches comment delimiters
350 of the same style. Thus if a comment starts with the comment-start
351 sequence of style ``bn'', it will extend until the next matching
352 comment-end sequence of style ``bn''.
353
354 The appropriate comment syntax settings for C++ can be as follows:
355
356 @table @asis
357 @item @samp{/}
358 @samp{124}
359 @item @samp{*}
360 @samp{23b}
361 @item newline
362 @samp{>}
363 @end table
364
365 This defines four comment-delimiting sequences:
366
367 @table @asis
368 @item @samp{/*}
369 This is a comment-start sequence for ``b'' style because the
370 second character, @samp{*}, has the @samp{b} flag.
371
372 @item @samp{//}
373 This is a comment-start sequence for ``a'' style because the second
374 character, @samp{/}, does not have the @samp{b} flag.
375
376 @item @samp{*/}
377 This is a comment-end sequence for ``b'' style because the first
378 character, @samp{*}, does have the @samp{b} flag.
379
380 @item newline
381 This is a comment-end sequence for ``a'' style, because the newline
382 character does not have the @samp{b} flag.
383 @end table
384
385 @item
386 @c Emacs 19 feature
387 @samp{p} identifies an additional ``prefix character'' for Lisp syntax.
388 These characters are treated as whitespace when they appear between
389 expressions. When they appear within an expression, they are handled
390 according to their usual syntax classes.
391
392 The function @code{backward-prefix-chars} moves back over these
393 characters, as well as over characters whose primary syntax class is
394 prefix (@samp{'}). @xref{Motion and Syntax}.
395 @end itemize
396
397 @node Syntax Table Functions
398 @section Syntax Table Functions
399
400 In this section we describe functions for creating, accessing and
401 altering syntax tables.
402
403 @defun make-syntax-table &optional table
404 This function creates a new syntax table, with all values initialized
405 to @code{nil}. If @var{table} is non-@code{nil}, it becomes the
406 parent of the new syntax table, otherwise the standard syntax table is
407 the parent. Like all char-tables, a syntax table inherits from its
408 parent. Thus the original syntax of all characters in the returned
409 syntax table is determined by the parent. @xref{Char-Tables}.
410
411 Most major mode syntax tables are created in this way.
412 @end defun
413
414 @defun copy-syntax-table &optional table
415 This function constructs a copy of @var{table} and returns it. If
416 @var{table} is not supplied (or is @code{nil}), it returns a copy of the
417 standard syntax table. Otherwise, an error is signaled if @var{table} is
418 not a syntax table.
419 @end defun
420
421 @deffn Command modify-syntax-entry char syntax-descriptor &optional table
422 This function sets the syntax entry for @var{char} according to
423 @var{syntax-descriptor}. @var{char} can be a character, or a cons
424 cell of the form @code{(@var{min} . @var{max})}; in the latter case,
425 the function sets the syntax entries for all characters in the range
426 between @var{min} and @var{max}, inclusive.
427
428 The syntax is changed only for @var{table}, which defaults to the
429 current buffer's syntax table, and not in any other syntax table. The
430 argument @var{syntax-descriptor} specifies the desired syntax; this is
431 a string beginning with a class designator character, and optionally
432 containing a matching character and flags as well. @xref{Syntax
433 Descriptors}.
434
435 This function always returns @code{nil}. The old syntax information in
436 the table for this character is discarded.
437
438 An error is signaled if the first character of the syntax descriptor is not
439 one of the seventeen syntax class designator characters. An error is also
440 signaled if @var{char} is not a character.
441
442 @example
443 @group
444 @exdent @r{Examples:}
445
446 ;; @r{Put the space character in class whitespace.}
447 (modify-syntax-entry ?\s " ")
448 @result{} nil
449 @end group
450
451 @group
452 ;; @r{Make @samp{$} an open parenthesis character,}
453 ;; @r{with @samp{^} as its matching close.}
454 (modify-syntax-entry ?$ "(^")
455 @result{} nil
456 @end group
457
458 @group
459 ;; @r{Make @samp{^} a close parenthesis character,}
460 ;; @r{with @samp{$} as its matching open.}
461 (modify-syntax-entry ?^ ")$")
462 @result{} nil
463 @end group
464
465 @group
466 ;; @r{Make @samp{/} a punctuation character,}
467 ;; @r{the first character of a start-comment sequence,}
468 ;; @r{and the second character of an end-comment sequence.}
469 ;; @r{This is used in C mode.}
470 (modify-syntax-entry ?/ ". 14")
471 @result{} nil
472 @end group
473 @end example
474 @end deffn
475
476 @defun char-syntax character
477 This function returns the syntax class of @var{character}, represented
478 by its mnemonic designator character. This returns @emph{only} the
479 class, not any matching parenthesis or flags.
480
481 An error is signaled if @var{char} is not a character.
482
483 The following examples apply to C mode. The first example shows that
484 the syntax class of space is whitespace (represented by a space). The
485 second example shows that the syntax of @samp{/} is punctuation. This
486 does not show the fact that it is also part of comment-start and -end
487 sequences. The third example shows that open parenthesis is in the class
488 of open parentheses. This does not show the fact that it has a matching
489 character, @samp{)}.
490
491 @example
492 @group
493 (string (char-syntax ?\s))
494 @result{} " "
495 @end group
496
497 @group
498 (string (char-syntax ?/))
499 @result{} "."
500 @end group
501
502 @group
503 (string (char-syntax ?\())
504 @result{} "("
505 @end group
506 @end example
507
508 We use @code{string} to make it easier to see the character returned by
509 @code{char-syntax}.
510 @end defun
511
512 @defun set-syntax-table table
513 This function makes @var{table} the syntax table for the current buffer.
514 It returns @var{table}.
515 @end defun
516
517 @defun syntax-table
518 This function returns the current syntax table, which is the table for
519 the current buffer.
520 @end defun
521
522 @defmac with-syntax-table @var{table} @var{body}@dots{}
523 This macro executes @var{body} using @var{table} as the current syntax
524 table. It returns the value of the last form in @var{body}, after
525 restoring the old current syntax table.
526
527 Since each buffer has its own current syntax table, we should make that
528 more precise: @code{with-syntax-table} temporarily alters the current
529 syntax table of whichever buffer is current at the time the macro
530 execution starts. Other buffers are not affected.
531 @end defmac
532
533 @node Syntax Properties
534 @section Syntax Properties
535 @kindex syntax-table @r{(text property)}
536
537 When the syntax table is not flexible enough to specify the syntax of
538 a language, you can use @code{syntax-table} text properties to
539 override the syntax table for specific character occurrences in the
540 buffer. @xref{Text Properties}. You can use Font Lock mode to set
541 @code{syntax-table} text properties. @xref{Setting Syntax
542 Properties}.
543
544 The valid values of @code{syntax-table} text property are:
545
546 @table @asis
547 @item @var{syntax-table}
548 If the property value is a syntax table, that table is used instead of
549 the current buffer's syntax table to determine the syntax for this
550 occurrence of the character.
551
552 @item @code{(@var{syntax-code} . @var{matching-char})}
553 A cons cell of this format specifies the syntax for this
554 occurrence of the character. (@pxref{Syntax Table Internals})
555
556 @item @code{nil}
557 If the property is @code{nil}, the character's syntax is determined from
558 the current syntax table in the usual way.
559 @end table
560
561 @defvar parse-sexp-lookup-properties
562 If this is non-@code{nil}, the syntax scanning functions pay attention
563 to syntax text properties. Otherwise they use only the current syntax
564 table.
565 @end defvar
566
567 @node Motion and Syntax
568 @section Motion and Syntax
569
570 This section describes functions for moving across characters that
571 have certain syntax classes.
572
573 @defun skip-syntax-forward syntaxes &optional limit
574 This function moves point forward across characters having syntax
575 classes mentioned in @var{syntaxes} (a string of syntax class
576 characters). It stops when it encounters the end of the buffer, or
577 position @var{limit} (if specified), or a character it is not supposed
578 to skip.
579
580 If @var{syntaxes} starts with @samp{^}, then the function skips
581 characters whose syntax is @emph{not} in @var{syntaxes}.
582
583 The return value is the distance traveled, which is a nonnegative
584 integer.
585 @end defun
586
587 @defun skip-syntax-backward syntaxes &optional limit
588 This function moves point backward across characters whose syntax
589 classes are mentioned in @var{syntaxes}. It stops when it encounters
590 the beginning of the buffer, or position @var{limit} (if specified), or
591 a character it is not supposed to skip.
592
593 If @var{syntaxes} starts with @samp{^}, then the function skips
594 characters whose syntax is @emph{not} in @var{syntaxes}.
595
596 The return value indicates the distance traveled. It is an integer that
597 is zero or less.
598 @end defun
599
600 @defun backward-prefix-chars
601 This function moves point backward over any number of characters with
602 expression prefix syntax. This includes both characters in the
603 expression prefix syntax class, and characters with the @samp{p} flag.
604 @end defun
605
606 @node Parsing Expressions
607 @section Parsing Expressions
608
609 This section describes functions for parsing and scanning balanced
610 expressions, also known as @dfn{sexps}. Basically, a sexp is either a
611 balanced parenthetical grouping, a string, or a symbol name (a
612 sequence of characters whose syntax is either word constituent or
613 symbol constituent). However, characters whose syntax is expression
614 prefix are treated as part of the sexp if they appear next to it.
615
616 The syntax table controls the interpretation of characters, so these
617 functions can be used for Lisp expressions when in Lisp mode and for C
618 expressions when in C mode. @xref{List Motion}, for convenient
619 higher-level functions for moving over balanced expressions.
620
621 A character's syntax controls how it changes the state of the
622 parser, rather than describing the state itself. For example, a
623 string delimiter character toggles the parser state between
624 ``in-string'' and ``in-code,'' but the syntax of characters does not
625 directly say whether they are inside a string. For example (note that
626 15 is the syntax code for generic string delimiters),
627
628 @example
629 (put-text-property 1 9 'syntax-table '(15 . nil))
630 @end example
631
632 @noindent
633 does not tell Emacs that the first eight chars of the current buffer
634 are a string, but rather that they are all string delimiters. As a
635 result, Emacs treats them as four consecutive empty string constants.
636
637 @menu
638 * Motion via Parsing:: Motion functions that work by parsing.
639 * Position Parse:: Determining the syntactic state of a position.
640 * Parser State:: How Emacs represents a syntactic state.
641 * Low-Level Parsing:: Parsing across a specified region.
642 * Control Parsing:: Parameters that affect parsing.
643 @end menu
644
645 @node Motion via Parsing
646 @subsection Motion Commands Based on Parsing
647
648 This section describes simple point-motion functions that operate
649 based on parsing expressions.
650
651 @defun scan-lists from count depth
652 This function scans forward @var{count} balanced parenthetical groupings
653 from position @var{from}. It returns the position where the scan stops.
654 If @var{count} is negative, the scan moves backwards.
655
656 If @var{depth} is nonzero, parenthesis depth counting begins from that
657 value. The only candidates for stopping are places where the depth in
658 parentheses becomes zero; @code{scan-lists} counts @var{count} such
659 places and then stops. Thus, a positive value for @var{depth} means go
660 out @var{depth} levels of parenthesis.
661
662 Scanning ignores comments if @code{parse-sexp-ignore-comments} is
663 non-@code{nil}.
664
665 If the scan reaches the beginning or end of the buffer (or its
666 accessible portion), and the depth is not zero, an error is signaled.
667 If the depth is zero but the count is not used up, @code{nil} is
668 returned.
669 @end defun
670
671 @defun scan-sexps from count
672 This function scans forward @var{count} sexps from position @var{from}.
673 It returns the position where the scan stops. If @var{count} is
674 negative, the scan moves backwards.
675
676 Scanning ignores comments if @code{parse-sexp-ignore-comments} is
677 non-@code{nil}.
678
679 If the scan reaches the beginning or end of (the accessible part of) the
680 buffer while in the middle of a parenthetical grouping, an error is
681 signaled. If it reaches the beginning or end between groupings but
682 before count is used up, @code{nil} is returned.
683 @end defun
684
685 @defun forward-comment count
686 This function moves point forward across @var{count} complete comments
687 (that is, including the starting delimiter and the terminating
688 delimiter if any), plus any whitespace encountered on the way. It
689 moves backward if @var{count} is negative. If it encounters anything
690 other than a comment or whitespace, it stops, leaving point at the
691 place where it stopped. This includes (for instance) finding the end
692 of a comment when moving forward and expecting the beginning of one.
693 The function also stops immediately after moving over the specified
694 number of complete comments. If @var{count} comments are found as
695 expected, with nothing except whitespace between them, it returns
696 @code{t}; otherwise it returns @code{nil}.
697
698 This function cannot tell whether the ``comments'' it traverses are
699 embedded within a string. If they look like comments, it treats them
700 as comments.
701 @end defun
702
703 To move forward over all comments and whitespace following point, use
704 @code{(forward-comment (buffer-size))}. @code{(buffer-size)} is a good
705 argument to use, because the number of comments in the buffer cannot
706 exceed that many.
707
708 @node Position Parse
709 @subsection Finding the Parse State for a Position
710
711 For syntactic analysis, such as in indentation, often the useful
712 thing is to compute the syntactic state corresponding to a given buffer
713 position. This function does that conveniently.
714
715 @defun syntax-ppss &optional pos
716 This function returns the parser state (see next section) that the
717 parser would reach at position @var{pos} starting from the beginning
718 of the buffer. This is equivalent to @code{(parse-partial-sexp
719 (point-min) @var{pos})}, except that @code{syntax-ppss} uses a cache
720 to speed up the computation. Due to this optimization, the 2nd value
721 (previous complete subexpression) and 6th value (minimum parenthesis
722 depth) of the returned parser state are not meaningful.
723 @end defun
724
725 @code{syntax-ppss} automatically hooks itself to
726 @code{before-change-functions} to keep its cache consistent. But
727 updating can fail if @code{syntax-ppss} is called while
728 @code{before-change-functions} is temporarily let-bound, or if the
729 buffer is modified without obeying the hook, such as when using
730 @code{inhibit-modification-hooks}. For this reason, it is sometimes
731 necessary to flush the cache manually.
732
733 @defun syntax-ppss-flush-cache beg &rest ignored-args
734 This function flushes the cache used by @code{syntax-ppss}, starting
735 at position @var{beg}. The remaining arguments, @var{ignored-args},
736 are ignored; this function accepts them so that it can be directly
737 used on hooks such as @code{before-change-functions} (@pxref{Change
738 Hooks}).
739 @end defun
740
741 Major modes can make @code{syntax-ppss} run faster by specifying
742 where it needs to start parsing.
743
744 @defvar syntax-begin-function
745 If this is non-@code{nil}, it should be a function that moves to an
746 earlier buffer position where the parser state is equivalent to
747 @code{nil}---in other words, a position outside of any comment,
748 string, or parenthesis. @code{syntax-ppss} uses it to further
749 optimize its computations, when the cache gives no help.
750 @end defvar
751
752 @node Parser State
753 @subsection Parser State
754 @cindex parser state
755
756 A @dfn{parser state} is a list of ten elements describing the final
757 state of parsing text syntactically as part of an expression. The
758 parsing functions in the following sections return a parser state as
759 the value, and in some cases accept one as an argument also, so that
760 you can resume parsing after it stops. Here are the meanings of the
761 elements of the parser state:
762
763 @enumerate 0
764 @item
765 The depth in parentheses, counting from 0. @strong{Warning:} this can
766 be negative if there are more close parens than open parens between
767 the start of the defun and point.
768
769 @item
770 @cindex innermost containing parentheses
771 The character position of the start of the innermost parenthetical
772 grouping containing the stopping point; @code{nil} if none.
773
774 @item
775 @cindex previous complete subexpression
776 The character position of the start of the last complete subexpression
777 terminated; @code{nil} if none.
778
779 @item
780 @cindex inside string
781 Non-@code{nil} if inside a string. More precisely, this is the
782 character that will terminate the string, or @code{t} if a generic
783 string delimiter character should terminate it.
784
785 @item
786 @cindex inside comment
787 @code{t} if inside a comment (of either style),
788 or the comment nesting level if inside a kind of comment
789 that can be nested.
790
791 @item
792 @cindex quote character
793 @code{t} if point is just after a quote character.
794
795 @item
796 The minimum parenthesis depth encountered during this scan.
797
798 @item
799 What kind of comment is active: @code{nil} for a comment of style
800 ``a'' or when not inside a comment, @code{t} for a comment of style
801 ``b,'' and @code{syntax-table} for a comment that should be ended by a
802 generic comment delimiter character.
803
804 @item
805 The string or comment start position. While inside a comment, this is
806 the position where the comment began; while inside a string, this is the
807 position where the string began. When outside of strings and comments,
808 this element is @code{nil}.
809
810 @item
811 Internal data for continuing the parsing. The meaning of this
812 data is subject to change; it is used if you pass this list
813 as the @var{state} argument to another call.
814 @end enumerate
815
816 Elements 1, 2, and 6 are ignored in a state which you pass as an
817 argument to continue parsing, and elements 8 and 9 are used only in
818 trivial cases. Those elements serve primarily to convey information
819 to the Lisp program which does the parsing.
820
821 One additional piece of useful information is available from a
822 parser state using this function:
823
824 @defun syntax-ppss-toplevel-pos state
825 This function extracts, from parser state @var{state}, the last
826 position scanned in the parse which was at top level in grammatical
827 structure. ``At top level'' means outside of any parentheses,
828 comments, or strings.
829
830 The value is @code{nil} if @var{state} represents a parse which has
831 arrived at a top level position.
832 @end defun
833
834 We have provided this access function rather than document how the
835 data is represented in the state, because we plan to change the
836 representation in the future.
837
838 @node Low-Level Parsing
839 @subsection Low-Level Parsing
840
841 The most basic way to use the expression parser is to tell it
842 to start at a given position with a certain state, and parse up to
843 a specified end position.
844
845 @defun parse-partial-sexp start limit &optional target-depth stop-before state stop-comment
846 This function parses a sexp in the current buffer starting at
847 @var{start}, not scanning past @var{limit}. It stops at position
848 @var{limit} or when certain criteria described below are met, and sets
849 point to the location where parsing stops. It returns a parser state
850 describing the status of the parse at the point where it stops.
851
852 @cindex parenthesis depth
853 If the third argument @var{target-depth} is non-@code{nil}, parsing
854 stops if the depth in parentheses becomes equal to @var{target-depth}.
855 The depth starts at 0, or at whatever is given in @var{state}.
856
857 If the fourth argument @var{stop-before} is non-@code{nil}, parsing
858 stops when it comes to any character that starts a sexp. If
859 @var{stop-comment} is non-@code{nil}, parsing stops when it comes to the
860 start of a comment. If @var{stop-comment} is the symbol
861 @code{syntax-table}, parsing stops after the start of a comment or a
862 string, or the end of a comment or a string, whichever comes first.
863
864 If @var{state} is @code{nil}, @var{start} is assumed to be at the top
865 level of parenthesis structure, such as the beginning of a function
866 definition. Alternatively, you might wish to resume parsing in the
867 middle of the structure. To do this, you must provide a @var{state}
868 argument that describes the initial status of parsing. The value
869 returned by a previous call to @code{parse-partial-sexp} will do
870 nicely.
871 @end defun
872
873 @node Control Parsing
874 @subsection Parameters to Control Parsing
875
876 @defvar multibyte-syntax-as-symbol
877 If this variable is non-@code{nil}, @code{scan-sexps} treats all
878 non-@acronym{ASCII} characters as symbol constituents regardless
879 of what the syntax table says about them. (However, text properties
880 can still override the syntax.)
881 @end defvar
882
883 @defopt parse-sexp-ignore-comments
884 @cindex skipping comments
885 If the value is non-@code{nil}, then comments are treated as
886 whitespace by the functions in this section and by @code{forward-sexp},
887 @code{scan-lists} and @code{scan-sexps}.
888 @end defopt
889
890 @vindex parse-sexp-lookup-properties
891 The behavior of @code{parse-partial-sexp} is also affected by
892 @code{parse-sexp-lookup-properties} (@pxref{Syntax Properties}).
893
894 You can use @code{forward-comment} to move forward or backward over
895 one comment or several comments.
896
897 @node Standard Syntax Tables
898 @section Some Standard Syntax Tables
899
900 Most of the major modes in Emacs have their own syntax tables. Here
901 are several of them:
902
903 @defun standard-syntax-table
904 This function returns the standard syntax table, which is the syntax
905 table used in Fundamental mode.
906 @end defun
907
908 @defvar text-mode-syntax-table
909 The value of this variable is the syntax table used in Text mode.
910 @end defvar
911
912 @defvar c-mode-syntax-table
913 The value of this variable is the syntax table for C-mode buffers.
914 @end defvar
915
916 @defvar emacs-lisp-mode-syntax-table
917 The value of this variable is the syntax table used in Emacs Lisp mode
918 by editing commands. (It has no effect on the Lisp @code{read}
919 function.)
920 @end defvar
921
922 @node Syntax Table Internals
923 @section Syntax Table Internals
924 @cindex syntax table internals
925
926 Lisp programs don't usually work with the elements directly; the
927 Lisp-level syntax table functions usually work with syntax descriptors
928 (@pxref{Syntax Descriptors}). Nonetheless, here we document the
929 internal format. This format is used mostly when manipulating
930 syntax properties.
931
932 Each element of a syntax table is a cons cell of the form
933 @code{(@var{syntax-code} . @var{matching-char})}. The @sc{car},
934 @var{syntax-code}, is an integer that encodes the syntax class, and any
935 flags. The @sc{cdr}, @var{matching-char}, is non-@code{nil} if
936 a character to match was specified.
937
938 This table gives the value of @var{syntax-code} which corresponds
939 to each syntactic type.
940
941 @multitable @columnfractions .05 .3 .3 .31
942 @item
943 @tab
944 @i{Integer} @i{Class}
945 @tab
946 @i{Integer} @i{Class}
947 @tab
948 @i{Integer} @i{Class}
949 @item
950 @tab
951 0 @ @ whitespace
952 @tab
953 5 @ @ close parenthesis
954 @tab
955 10 @ @ character quote
956 @item
957 @tab
958 1 @ @ punctuation
959 @tab
960 6 @ @ expression prefix
961 @tab
962 11 @ @ comment-start
963 @item
964 @tab
965 2 @ @ word
966 @tab
967 7 @ @ string quote
968 @tab
969 12 @ @ comment-end
970 @item
971 @tab
972 3 @ @ symbol
973 @tab
974 8 @ @ paired delimiter
975 @tab
976 13 @ @ inherit
977 @item
978 @tab
979 4 @ @ open parenthesis
980 @tab
981 9 @ @ escape
982 @tab
983 14 @ @ generic comment
984 @item
985 @tab
986 15 @ generic string
987 @end multitable
988
989 For example, the usual syntax value for @samp{(} is @code{(4 . 41)}.
990 (41 is the character code for @samp{)}.)
991
992 The flags are encoded in higher order bits, starting 16 bits from the
993 least significant bit. This table gives the power of two which
994 corresponds to each syntax flag.
995
996 @multitable @columnfractions .05 .3 .3 .3
997 @item
998 @tab
999 @i{Prefix} @i{Flag}
1000 @tab
1001 @i{Prefix} @i{Flag}
1002 @tab
1003 @i{Prefix} @i{Flag}
1004 @item
1005 @tab
1006 @samp{1} @ @ @code{(lsh 1 16)}
1007 @tab
1008 @samp{4} @ @ @code{(lsh 1 19)}
1009 @tab
1010 @samp{b} @ @ @code{(lsh 1 21)}
1011 @item
1012 @tab
1013 @samp{2} @ @ @code{(lsh 1 17)}
1014 @tab
1015 @samp{p} @ @ @code{(lsh 1 20)}
1016 @tab
1017 @samp{n} @ @ @code{(lsh 1 22)}
1018 @item
1019 @tab
1020 @samp{3} @ @ @code{(lsh 1 18)}
1021 @end multitable
1022
1023 @defun string-to-syntax @var{desc}
1024 This function returns the internal form corresponding to the syntax
1025 descriptor @var{desc}, a cons cell @code{(@var{syntax-code}
1026 . @var{matching-char})}.
1027 @end defun
1028
1029 @defun syntax-after pos
1030 This function returns the syntax code of the character in the buffer
1031 after position @var{pos}, taking account of syntax properties as well
1032 as the syntax table. If @var{pos} is outside the buffer's accessible
1033 portion (@pxref{Narrowing, accessible portion}), this function returns
1034 @code{nil}.
1035 @end defun
1036
1037 @defun syntax-class syntax
1038 This function returns the syntax class of the syntax code
1039 @var{syntax}. (It masks off the high 16 bits that hold the flags
1040 encoded in the syntax descriptor.) If @var{syntax} is @code{nil}, it
1041 returns @code{nil}; this is so evaluating the expression
1042
1043 @example
1044 (syntax-class (syntax-after pos))
1045 @end example
1046
1047 @noindent
1048 where @code{pos} is outside the buffer's accessible portion, will
1049 yield @code{nil} without throwing errors or producing wrong syntax
1050 class codes.
1051 @end defun
1052
1053 @node Categories
1054 @section Categories
1055 @cindex categories of characters
1056 @cindex character categories
1057
1058 @dfn{Categories} provide an alternate way of classifying characters
1059 syntactically. You can define several categories as needed, then
1060 independently assign each character to one or more categories. Unlike
1061 syntax classes, categories are not mutually exclusive; it is normal for
1062 one character to belong to several categories.
1063
1064 @cindex category table
1065 Each buffer has a @dfn{category table} which records which categories
1066 are defined and also which characters belong to each category. Each
1067 category table defines its own categories, but normally these are
1068 initialized by copying from the standard categories table, so that the
1069 standard categories are available in all modes.
1070
1071 Each category has a name, which is an @acronym{ASCII} printing character in
1072 the range @w{@samp{ }} to @samp{~}. You specify the name of a category
1073 when you define it with @code{define-category}.
1074
1075 The category table is actually a char-table (@pxref{Char-Tables}).
1076 The element of the category table at index @var{c} is a @dfn{category
1077 set}---a bool-vector---that indicates which categories character @var{c}
1078 belongs to. In this category set, if the element at index @var{cat} is
1079 @code{t}, that means category @var{cat} is a member of the set, and that
1080 character @var{c} belongs to category @var{cat}.
1081
1082 For the next three functions, the optional argument @var{table}
1083 defaults to the current buffer's category table.
1084
1085 @defun define-category char docstring &optional table
1086 This function defines a new category, with name @var{char} and
1087 documentation @var{docstring}, for the category table @var{table}.
1088 @end defun
1089
1090 @defun category-docstring category &optional table
1091 This function returns the documentation string of category @var{category}
1092 in category table @var{table}.
1093
1094 @example
1095 (category-docstring ?a)
1096 @result{} "ASCII"
1097 (category-docstring ?l)
1098 @result{} "Latin"
1099 @end example
1100 @end defun
1101
1102 @defun get-unused-category &optional table
1103 This function returns a category name (a character) which is not
1104 currently defined in @var{table}. If all possible categories are in use
1105 in @var{table}, it returns @code{nil}.
1106 @end defun
1107
1108 @defun category-table
1109 This function returns the current buffer's category table.
1110 @end defun
1111
1112 @defun category-table-p object
1113 This function returns @code{t} if @var{object} is a category table,
1114 otherwise @code{nil}.
1115 @end defun
1116
1117 @defun standard-category-table
1118 This function returns the standard category table.
1119 @end defun
1120
1121 @defun copy-category-table &optional table
1122 This function constructs a copy of @var{table} and returns it. If
1123 @var{table} is not supplied (or is @code{nil}), it returns a copy of the
1124 standard category table. Otherwise, an error is signaled if @var{table}
1125 is not a category table.
1126 @end defun
1127
1128 @defun set-category-table table
1129 This function makes @var{table} the category table for the current
1130 buffer. It returns @var{table}.
1131 @end defun
1132
1133 @defun make-category-table
1134 This creates and returns an empty category table. In an empty category
1135 table, no categories have been allocated, and no characters belong to
1136 any categories.
1137 @end defun
1138
1139 @defun make-category-set categories
1140 This function returns a new category set---a bool-vector---whose initial
1141 contents are the categories listed in the string @var{categories}. The
1142 elements of @var{categories} should be category names; the new category
1143 set has @code{t} for each of those categories, and @code{nil} for all
1144 other categories.
1145
1146 @example
1147 (make-category-set "al")
1148 @result{} #&128"\0\0\0\0\0\0\0\0\0\0\0\0\2\20\0\0"
1149 @end example
1150 @end defun
1151
1152 @defun char-category-set char
1153 This function returns the category set for character @var{char} in the
1154 current buffer's category table. This is the bool-vector which
1155 records which categories the character @var{char} belongs to. The
1156 function @code{char-category-set} does not allocate storage, because
1157 it returns the same bool-vector that exists in the category table.
1158
1159 @example
1160 (char-category-set ?a)
1161 @result{} #&128"\0\0\0\0\0\0\0\0\0\0\0\0\2\20\0\0"
1162 @end example
1163 @end defun
1164
1165 @defun category-set-mnemonics category-set
1166 This function converts the category set @var{category-set} into a string
1167 containing the characters that designate the categories that are members
1168 of the set.
1169
1170 @example
1171 (category-set-mnemonics (char-category-set ?a))
1172 @result{} "al"
1173 @end example
1174 @end defun
1175
1176 @defun modify-category-entry char category &optional table reset
1177 This function modifies the category set of @var{char} in category
1178 table @var{table} (which defaults to the current buffer's category
1179 table). @var{char} can be a character, or a cons cell of the form
1180 @code{(@var{min} . @var{max})}; in the latter case, the function
1181 modifies the category sets of all characters in the range between
1182 @var{min} and @var{max}, inclusive.
1183
1184 Normally, it modifies a category set by adding @var{category} to it.
1185 But if @var{reset} is non-@code{nil}, then it deletes @var{category}
1186 instead.
1187 @end defun
1188
1189 @deffn Command describe-categories &optional buffer-or-name
1190 This function describes the category specifications in the current
1191 category table. It inserts the descriptions in a buffer, and then
1192 displays that buffer. If @var{buffer-or-name} is non-@code{nil}, it
1193 describes the category table of that buffer instead.
1194 @end deffn