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
12 A @dfn{syntax table} specifies the syntactic textual function of each
13 character. This information is used by the parsing commands, the
14 complex movement commands, and others to determine where words, symbols,
15 and other syntactic constructs begin and end. The current syntax table
16 controls the meaning of the word motion functions (@pxref{Word Motion})
17 and the list motion functions (@pxref{List Motion}) as well as the
18 functions in this chapter.
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.
32 @section Syntax Table Concepts
35 A @dfn{syntax table} provides Emacs with the information that
36 determines the syntactic use of each character in a buffer. This
37 information is used by the parsing commands, the complex movement
38 commands, and others to determine where words, symbols, and other
39 syntactic constructs begin and end. The current syntax table controls
40 the meaning of the word motion functions (@pxref{Word Motion}) and the
41 list motion functions (@pxref{List Motion}) as well as the functions in
45 A syntax table is a vector of 256 elements; it contains one entry for
46 each of the 256 @sc{ASCII} characters of an 8-bit byte. Each element is
47 an integer that encodes the syntax of the character in question.
49 Syntax tables are used only for moving across text, not for the Emacs
50 Lisp reader. Emacs Lisp uses built-in syntactic rules when reading Lisp
51 expressions, and these rules cannot be changed.
53 Each buffer has its own major mode, and each major mode has its own
54 idea of the syntactic class of various characters. For example, in Lisp
55 mode, the character @samp{;} begins a comment, but in C mode, it
56 terminates a statement. To support these variations, Emacs makes the
57 choice of syntax table local to each buffer. Typically, each major
58 mode has its own syntax table and installs that table in each buffer
59 which uses that mode. Changing this table alters the syntax in all
60 those buffers as well as in any buffers subsequently put in that mode.
61 Occasionally several similar modes share one syntax table.
62 @xref{Example Major Modes}, for an example of how to set up a syntax
65 A syntax table can inherit the data for some characters from the
66 standard syntax table, while specifying other characters itself. The
67 ``inherit'' syntax class means ``inherit this character's syntax from
68 the standard syntax table.'' Most major modes' syntax tables inherit
69 the syntax of character codes 0 through 31 and 128 through 255. This is
70 useful with character sets such as ISO Latin-1 that have additional
71 alphabetic characters in the range 128 to 255. Just changing the
72 standard syntax for these characters affects all major modes.
74 @defun syntax-table-p object
75 This function returns @code{t} if @var{object} is a vector of length 256
76 elements. This means that the vector may be a syntax table. However,
77 according to this test, any vector of length 256 is considered to be a
78 syntax table, no matter what its contents.
81 @node Syntax Descriptors
82 @section Syntax Descriptors
83 @cindex syntax classes
85 This section describes the syntax classes and flags that denote the
86 syntax of a character, and how they are represented as a @dfn{syntax
87 descriptor}, which is a Lisp string that you pass to
88 @code{modify-syntax-entry} to specify the desired syntax.
90 Emacs defines a number of @dfn{syntax classes}. Each syntax table
91 puts each character into one class. There is no necessary relationship
92 between the class of a character in one syntax table and its class in
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 which is frequently put in that class;
98 however, its meaning as a designator is unvarying and independent of
99 what syntax that character currently has.
101 @cindex syntax descriptor
102 A syntax descriptor is a Lisp string which specifies a syntax class, a
103 matching character (used only for the parenthesis classes) and flags.
104 The first character is the designator for a syntax class. The second
105 character is the character to match; if it is unused, put a space there.
106 Then come the characters for any desired flags. If no matching
107 character or flags are needed, one character is sufficient.
109 For example, the descriptor for the character @samp{*} in C mode is
110 @samp{@w{. 23}} (i.e., punctuation, matching character slot unused,
111 second character of a comment-starter, first character of an
112 comment-ender), and the entry for @samp{/} is @samp{@w{. 14}} (i.e.,
113 punctuation, matching character slot unused, first character of a
114 comment-starter, second character of a comment-ender).
117 * Syntax Class Table:: Table of syntax classes.
118 * Syntax Flags:: Additional flags each character can have.
121 @node Syntax Class Table
122 @subsection Table of Syntax Classes
124 Here is a table syntax classes, the characters that stand for them,
125 their meanings, and examples of their use.
127 @deffn {Syntax class} @w{whitespace character}
128 @dfn{Whitespace characters} (designated with @w{@samp{@ }} or @samp{-})
129 separate symbols and words from each other. Typically, whitespace
130 characters have no other syntactic significance, and multiple whitespace
131 characters are syntactically equivalent to a single one. Space, tab,
132 newline and formfeed are almost always classified as whitespace.
135 @deffn {Syntax class} @w{word constituent}
136 @dfn{Word constituents} (designated with @samp{w}) are parts of normal
137 English words and are typically used in variable and command names in
138 programs. All upper and lower case letters and the digits are typically
142 @deffn {Syntax class} @w{symbol constituent}
143 @dfn{Symbol constituents} (designated with @samp{_}) are the extra
144 characters that are used in variable and command names along with word
145 constituents. For example, the symbol constituents class is used in
146 Lisp mode to indicate that certain characters may be part of symbol
147 names even though they are not part of English words. These characters
148 are @samp{$&*+-_<>}. In standard C, the only non-word-constituent
149 character that is valid in symbols is underscore (@samp{_}).
152 @deffn {Syntax class} @w{punctuation character}
153 @dfn{Punctuation characters} (@samp{.}) are those characters that are
154 used as punctuation in English, or are used in some way in a programming
155 language to separate symbols from one another. Most programming
156 language modes, including Emacs Lisp mode, have no characters in this
157 class since the few characters that are not symbol or word constituents
161 @deffn {Syntax class} @w{open parenthesis character}
162 @deffnx {Syntax class} @w{close parenthesis character}
163 @cindex parenthesis syntax
164 Open and close @dfn{parenthesis characters} are characters used in
165 dissimilar pairs to surround sentences or expressions. Such a grouping
166 is begun with an open parenthesis character and terminated with a close.
167 Each open parenthesis character matches a particular close parenthesis
168 character, and vice versa. Normally, Emacs indicates momentarily the
169 matching open parenthesis when you insert a close parenthesis.
172 The class of open parentheses is designated with @samp{(}, and that of
173 close parentheses with @samp{)}.
175 In English text, and in C code, the parenthesis pairs are @samp{()},
176 @samp{[]}, and @samp{@{@}}. In Emacs Lisp, the delimiters for lists and
177 vectors (@samp{()} and @samp{[]}) are classified as parenthesis
181 @deffn {Syntax class} @w{string quote}
182 @dfn{String quote characters} (designated with @samp{"}) are used in
183 many languages, including Lisp and C, to delimit string constants. The
184 same string quote character appears at the beginning and the end of a
185 string. Such quoted strings do not nest.
187 The parsing facilities of Emacs consider a string as a single token.
188 The usual syntactic meanings of the characters in the string are
191 The Lisp modes have two string quote characters: double-quote (@samp{"})
192 and vertical bar (@samp{|}). @samp{|} is not used in Emacs Lisp, but it
193 is used in Common Lisp. C also has two string quote characters:
194 double-quote for strings, and single-quote (@samp{'}) for character
197 English text has no string quote characters because English is not a
198 programming language. Although quotation marks are used in English,
199 we do not want them to turn off the usual syntactic properties of
200 other characters in the quotation.
203 @deffn {Syntax class} @w{escape}
204 An @dfn{escape character} (designated with @samp{\}) starts an escape
205 sequence such as is used in C string and character constants. The
206 character @samp{\} belongs to this class in both C and Lisp. (In C, it
207 is used thus only inside strings, but it turns out to cause no trouble
208 to treat it this way throughout C code.)
210 Characters in this class count as part of words if
211 @code{words-include-escapes} is non-@code{nil}. @xref{Word Motion}.
214 @deffn {Syntax class} @w{character quote}
215 A @dfn{character quote character} (designated with @samp{/}) quotes the
216 following character so that it loses its normal syntactic meaning. This
217 differs from an escape character in that only the character immediately
218 following is ever affected.
220 Characters in this class count as part of words if
221 @code{words-include-escapes} is non-@code{nil}. @xref{Word Motion}.
223 This class is not currently used in any standard Emacs modes.
226 @deffn {Syntax class} @w{paired delimiter}
227 @dfn{Paired delimiter characters} (designated with @samp{$}) are like
228 string quote characters except that the syntactic properties of the
229 characters between the delimiters are not suppressed. Only @TeX{} mode
230 uses a paired identical delimiter presently---the @samp{$} that both
231 enters and leaves math mode.
234 @deffn {Syntax class} @w{expression prefix}
235 An @dfn{expression prefix operator} (designated with @samp{'}) is used
236 for syntactic operators that are part of an expression if they appear
237 next 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).
242 @deffn {Syntax class} @w{comment starter}
243 @deffnx {Syntax class} @w{comment ender}
244 @cindex comment syntax
245 The @dfn{comment starter} and @dfn{comment ender} characters are used in
246 various languages to delimit comments. These classes are designated
247 with @samp{<} and @samp{>}, respectively.
249 English text has no comment characters. In Lisp, the semicolon
250 (@samp{;}) starts a comment and a newline or formfeed ends one.
253 @deffn {Syntax class} @w{inherit}
254 This syntax class does not specify a syntax. It says to look in the
255 standard syntax table to find the syntax of this character. The
256 designator for this syntax code is @samp{@@}.
260 @subsection Syntax Flags
263 In addition to the classes, entries for characters in a syntax table
264 can include flags. There are six possible flags, represented by the
265 characters @samp{1}, @samp{2}, @samp{3}, @samp{4}, @samp{b} and
268 All the flags except @samp{p} are used to describe multi-character
269 comment delimiters. The digit flags indicate that a character can
270 @emph{also} be part of a comment sequence, in addition to the syntactic
271 properties associated with its character class. The flags are
272 independent of the class and each other for the sake of characters such
273 as @samp{*} in C mode, which is a punctuation character, @emph{and} the
274 second character of a start-of-comment sequence (@samp{/*}), @emph{and}
275 the first character of an end-of-comment sequence (@samp{*/}).
277 The flags for a character @var{c} are:
281 @samp{1} means @var{c} is the start of a two-character comment start
285 @samp{2} means @var{c} is the second character of such a sequence.
288 @samp{3} means @var{c} is the start of a two-character comment end
292 @samp{4} means @var{c} is the second character of such a sequence.
296 @samp{b} means that @var{c} as a comment delimiter belongs to the
297 alternative ``b'' comment style.
299 Emacs supports two comment styles simultaneously in any one syntax
300 table. This is for the sake of C++. Each style of comment syntax has
301 its own comment-start sequence and its own comment-end sequence. Each
302 comment must stick to one style or the other; thus, if it starts with
303 the comment-start sequence of style ``b'', it must also end with the
304 comment-end sequence of style ``b''.
306 The two comment-start sequences must begin with the same character; only
307 the second character may differ. Mark the second character of the
308 ``b''-style comment start sequence with the @samp{b} flag.
310 A comment-end sequence (one or two characters) applies to the ``b''
311 style if its first character has the @samp{b} flag set; otherwise, it
312 applies to the ``a'' style.
314 The appropriate comment syntax settings for C++ are as follows:
325 Thus @samp{/*} is a comment-start sequence for ``a'' style, @samp{//}
326 is a comment-start sequence for ``b'' style, @samp{*/} is a
327 comment-end sequence for ``a'' style, and newline is a comment-end
328 sequence for ``b'' style.
332 @samp{p} identifies an additional ``prefix character'' for Lisp syntax.
333 These characters are treated as whitespace when they appear between
334 expressions. When they appear within an expression, they are handled
335 according to their usual syntax codes.
337 The function @code{backward-prefix-chars} moves back over these
338 characters, as well as over characters whose primary syntax class is
339 prefix (@samp{'}). @xref{Motion and Syntax}.
342 @node Syntax Table Functions
343 @section Syntax Table Functions
345 In this section we describe functions for creating, accessing and
346 altering syntax tables.
348 @defun make-syntax-table
349 This function creates a new syntax table. Character codes 0 through
350 31, and 128 through 255, are set up to inherit from the standard syntax
351 table. The other character codes are set up by copying what the
352 standard syntax table says about them.
354 Most major mode syntax tables are created in this way.
357 @defun copy-syntax-table &optional table
358 This 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
360 current syntax table. Otherwise, an error is signaled if @var{table} is
364 @deffn Command modify-syntax-entry char syntax-descriptor &optional table
365 This function sets the syntax entry for @var{char} according to
366 @var{syntax-descriptor}. The syntax is changed only for @var{table},
367 which defaults to the current buffer's syntax table, and not in any
368 other syntax table. The argument @var{syntax-descriptor} specifies the
369 desired syntax; this is a string beginning with a class designator
370 character, and optionally containing a matching character and flags as
371 well. @xref{Syntax Descriptors}.
373 This function always returns @code{nil}. The old syntax information in
374 the table for this character is discarded.
376 An error is signaled if the first character of the syntax descriptor is not
377 one of the twelve syntax class designator characters. An error is also
378 signaled if @var{char} is not a character.
382 @exdent @r{Examples:}
384 ;; @r{Put the space character in class whitespace.}
385 (modify-syntax-entry ?\ " ")
390 ;; @r{Make @samp{$} an open parenthesis character,}
391 ;; @r{with @samp{^} as its matching close.}
392 (modify-syntax-entry ?$ "(^")
397 ;; @r{Make @samp{^} a close parenthesis character,}
398 ;; @r{with @samp{$} as its matching open.}
399 (modify-syntax-entry ?^ ")$")
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")
414 @defun char-syntax character
415 This function returns the syntax class of @var{character}, represented
416 by its mnemonic designator character. This @emph{only} returns the
417 class, not any matching parenthesis or flags.
419 An error is signaled if @var{char} is not a character.
421 The following examples apply to C mode. The first example shows that
422 the syntax class of space is whitespace (represented by a space). The
423 second example shows that the syntax of @samp{/} is punctuation. This
424 does not show the fact that it is also part of comment start and end
425 sequence. The third example shows that open parenthesis is in the class
426 of open parentheses. This does not show the fact that it has a matching
431 (char-to-string (char-syntax ?\ ))
436 (char-to-string (char-syntax ?/))
441 (char-to-string (char-syntax ?\())
447 @defun set-syntax-table table
448 This function makes @var{table} the syntax table for the current buffer.
449 It returns @var{table}.
453 This function returns the current syntax table, which is the table for
457 @node Motion and Syntax
458 @section Motion and Syntax
460 This section describes functions for moving across characters in
461 certain syntax classes. None of these functions exists in Emacs
462 version 18 or earlier.
464 @defun skip-syntax-forward syntaxes &optional limit
465 This function moves point forward across characters having syntax classes
466 mentioned in @var{syntaxes}. It stops when it encounters the end of
467 the buffer, or position @var{lim} (if specified), or a character it is
468 not supposed to skip.
469 @ignore @c may want to change this.
470 The return value is the distance traveled, which is a nonnegative
475 @defun skip-syntax-backward syntaxes &optional limit
476 This function moves point backward across characters whose syntax
477 classes are mentioned in @var{syntaxes}. It stops when it encounters
478 the beginning of the buffer, or position @var{lim} (if specified), or a
479 character it is not supposed to skip.
480 @ignore @c may want to change this.
481 The return value indicates the distance traveled. It is an integer that
486 @defun backward-prefix-chars
487 This function moves point backward over any number of characters with
488 expression prefix syntax. This includes both characters in the
489 expression prefix syntax class, and characters with the @samp{p} flag.
492 @node Parsing Expressions
493 @section Parsing Balanced Expressions
495 Here are several functions for parsing and scanning balanced
496 expressions, also known as @dfn{sexps}, in which parentheses match in
497 pairs. The syntax table controls the interpretation of characters, so
498 these functions can be used for Lisp expressions when in Lisp mode and
499 for C expressions when in C mode. @xref{List Motion}, for convenient
500 higher-level functions for moving over balanced expressions.
502 @defun parse-partial-sexp start limit &optional target-depth stop-before state stop-comment
503 This function parses a sexp in the current buffer starting at
504 @var{start}, not scanning past @var{limit}. It stops at @var{limit} or
505 when certain criteria described below are met, and sets to the location
506 where parsing stops. It returns a value describing the status of the
507 parse at the point where it stops.
509 If @var{state} is @code{nil}, @var{start} is assumed to be at the top
510 level of parenthesis structure, such as the beginning of a function
511 definition. Alternatively, you might wish to resume parsing in the
512 middle of the structure. To do this, you must provide a @var{state}
513 argument that describes the initial status of parsing.
515 @cindex parenthesis depth
516 If the third argument @var{target-depth} is non-@code{nil}, parsing
517 stops if the depth in parentheses becomes equal to @var{target-depth}.
518 The depth starts at 0, or at whatever is given in @var{state}.
520 If the fourth argument @var{stop-before} is non-@code{nil}, parsing
521 stops 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
526 The fifth argument @var{state} is an eight-element list of the same
527 form as the value of this function, described below. The return value
528 of one call may be used to initialize the state of the parse on another
529 call to @code{parse-partial-sexp}.
531 The result is a list of eight elements describing the final state of
536 The depth in parentheses, counting from 0.
539 @cindex innermost containing parentheses
540 The character position of the start of the innermost containing
541 parenthetical grouping; @code{nil} if none.
544 @cindex previous complete subexpression
545 The character position of the start of the last complete subexpression
546 terminated; @code{nil} if none.
549 @cindex inside string
550 Non-@code{nil} if inside a string. More precisely, this is the
551 character that will terminate the string.
554 @cindex inside comment
555 @code{t} if inside a comment.
558 @cindex quote character
559 @code{t} if point is just after a quote character.
562 The minimum parenthesis depth encountered during this scan.
565 @code{t} if inside a comment of style ``b''.
568 Elements 0, 3, 4, 5 and 7 are significant in the argument @var{state}.
570 @cindex indenting with parentheses
571 This function is most often used to compute indentation for languages
572 that have nested parentheses.
575 @defun scan-lists from count depth
576 This function scans forward @var{count} balanced parenthetical groupings
577 from character number @var{from}. It returns the character position
578 where the scan stops.
580 If @var{depth} is nonzero, parenthesis depth counting begins from that
581 value. The only candidates for stopping are places where the depth in
582 parentheses becomes zero; @code{scan-lists} counts @var{count} such
583 places and then stops. Thus, a positive value for @var{depth} means go
584 out levels of parenthesis.
586 Scanning ignores comments if @code{parse-sexp-ignore-comments} is
589 If scan reaches the beginning or end of the buffer (or its accessible
590 portion), and the depth is not zero, an error is signaled. If the depth
591 is zero but the count is not used up, @code{nil} is returned.
594 @defun scan-sexps from count
595 This function scans forward @var{count} sexps from character position
596 @var{from}. It returns the character position where the scan stops.
598 Scanning ignores comments if @code{parse-sexp-ignore-comments} is
601 If scan reaches the beginning or end of (the accessible part of) the
602 buffer in the middle of a parenthetical grouping, an error is signaled.
603 If it reaches the beginning or end between groupings but before count is
604 used up, @code{nil} is returned.
607 @defvar parse-sexp-ignore-comments
608 @cindex skipping comments
609 If the value is non-@code{nil}, then comments are treated as
610 whitespace by the functions in this section and by @code{forward-sexp}.
612 In older Emacs versions, this feature worked only when the comment
613 terminator is something like @samp{*/}, and appears only to end a
614 comment. In languages where newlines terminate comments, it was
615 necessary make this variable @code{nil}, since not every newline is the
616 end of a comment. This limitation no longer exists.
619 You can use @code{forward-comment} to move forward or backward over
620 one comment or several comments.
622 @defun forward-comment count
623 This function moves point forward across @var{count} comments (backward,
624 if @var{count} is negative). If it finds anything other than a comment
625 or whitespace, it stops, leaving point at the place where it stopped.
626 It also stops after satisfying @var{count}.
629 To move forward over all comments and whitespace following point, use
630 @code{(forward-comment (buffer-size))}. @code{(buffer-size)} is a good
631 argument to use, because the number of comments to in the buffer cannot
634 @node Standard Syntax Tables
635 @section Some Standard Syntax Tables
637 Each of the major modes in Emacs has its own syntax table. Here are
640 @defun standard-syntax-table
641 This function returns the standard syntax table, which is the syntax
642 table used in Fundamental mode.
645 @defvar text-mode-syntax-table
646 The value of this variable is the syntax table used in Text mode.
649 @defvar c-mode-syntax-table
650 The value of this variable is the syntax table for C-mode buffers.
653 @defvar emacs-lisp-mode-syntax-table
654 The value of this variable is the syntax table used in Emacs Lisp mode
655 by editing commands. (It has no effect on the Lisp @code{read}
659 @node Syntax Table Internals
660 @section Syntax Table Internals
661 @cindex syntax table internals
663 Each element of a syntax table is an integer that encodes the syntax
664 of one character: the syntax class, possible matching character, and
665 flags. Lisp programs don't usually work with the elements directly; the
666 Lisp-level syntax table functions usually work with syntax descriptors
667 (@pxref{Syntax Descriptors}).
669 The low 8 bits of each element of a syntax table indicate the
705 The next 8 bits are the matching opposite parenthesis (if the
706 character has parenthesis syntax); otherwise, they are not meaningful.
707 The next 6 bits are the flags.