Next: , Previous: Motion via Parsing, Up: Parsing Expressions

35.6.2 Finding the Parse State for a Position

For syntactic analysis, such as in indentation, often the useful thing is to compute the syntactic state corresponding to a given buffer position. This function does that conveniently.

— Function: syntax-ppss &optional pos

This function returns the parser state (see next section) that the parser would reach at position pos starting from the beginning of the buffer. This is equivalent to (parse-partial-sexp (point-min) pos), except that syntax-ppss uses a cache to speed up the computation. Due to this optimization, the 2nd value (previous complete subexpression) and 6th value (minimum parenthesis depth) of the returned parser state are not meaningful.

syntax-ppss automatically hooks itself to before-change-functions to keep its cache consistent. But updating can fail if syntax-ppss is called while before-change-functions is temporarily let-bound, or if the buffer is modified without obeying the hook, such as when using inhibit-modification-hooks. For this reason, it is sometimes necessary to flush the cache manually.

— Function: syntax-ppss-flush-cache beg &rest ignored-args

This function flushes the cache used by syntax-ppss, starting at position beg. The remaining arguments, ignored-args, are ignored; this function accepts them so that it can be directly used on hooks such as before-change-functions (see Change Hooks).

Major modes can make syntax-ppss run faster by specifying where it needs to start parsing.

— Variable: syntax-begin-function

If this is non-nil, it should be a function that moves to an earlier buffer position where the parser state is equivalent to nil—in other words, a position outside of any comment, string, or parenthesis. syntax-ppss uses it to further optimize its computations, when the cache gives no help.