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