Here are several functions for parsing and scanning balanced expressions, also known as sexps, in which parentheses match in pairs. The syntax table controls the interpretation of characters, so these functions can be used for Lisp expressions when in Lisp mode and for C expressions when in C mode. See section Moving over Balanced Expressions, for convenient higher-level functions for moving over balanced expressions.
If state is nil
, start is assumed to be at the top
level of parenthesis structure, such as the beginning of a function
definition. Alternatively, you might wish to resume parsing in the
middle of the structure. To do this, you must provide a state
argument that describes the initial status of parsing.
If the third argument target-depth is non-nil
, parsing
stops if the depth in parentheses becomes equal to target-depth.
The depth starts at 0, or at whatever is given in state.
If the fourth argument stop-before is non-nil
, parsing
stops when it comes to any character that starts a sexp. If
stop-comment is non-nil
, parsing stops when it comes to the
start of a comment. If stop-comment is the symbol
syntax-table
, parsing stops after the start of a comment or a
string, or the end of a comment or a string, whichever comes first.
The fifth argument state is a nine-element list of the same form
as the value of this function, described below. (It is OK to omit the
last element of the nine.) The return value of one call may be used to
initialize the state of the parse on another call to
parse-partial-sexp
.
The result is a list of nine elements describing the final state of the parse:
nil
if none.
nil
if none.
nil
if inside a string. More precisely, this is the
character that will terminate the string, or t
if a generic
string delimiter character should terminate it.
t
if inside a comment (of either style).
t
if point is just after a quote character.
nil
for a comment of style "a",
t
for a comment of style "b", and syntax-table
for
a comment that should be ended by a generic comment delimiter character.
nil
.
Elements 0, 3, 4, 5 and 7 are significant in the argument state.
This function is most often used to compute indentation for languages that have nested parentheses.
If depth is nonzero, parenthesis depth counting begins from that
value. The only candidates for stopping are places where the depth in
parentheses becomes zero; scan-lists
counts count such
places and then stops. Thus, a positive value for depth means go
out depth levels of parenthesis.
Scanning ignores comments if parse-sexp-ignore-comments
is
non-nil
.
If the scan reaches the beginning or end of the buffer (or its
accessible portion), and the depth is not zero, an error is signaled.
If the depth is zero but the count is not used up, nil
is
returned.
Scanning ignores comments if parse-sexp-ignore-comments
is
non-nil
.
If the scan reaches the beginning or end of (the accessible part of) the
buffer while in the middle of a parenthetical grouping, an error is
signaled. If it reaches the beginning or end between groupings but
before count is used up, nil
is returned.
nil
, then comments are treated as
whitespace by the functions in this section and by forward-sexp
.
In older Emacs versions, this feature worked only when the comment
terminator is something like `*/', and appears only to end a
comment. In languages where newlines terminate comments, it was
necessary make this variable nil
, since not every newline is the
end of a comment. This limitation no longer exists.
You can use forward-comment
to move forward or backward over
one comment or several comments.
To move forward over all comments and whitespace following point, use
(forward-comment (buffer-size))
. (buffer-size)
is a good
argument to use, because the number of comments in the buffer cannot
exceed that many.
Go to the first, previous, next, last section, table of contents.