24.6.8 Syntactic Font Lock

Syntactic fontification uses a syntax table (see Syntax Tables) to find and highlight syntactically relevant text. If enabled, it runs prior to search-based fontification. The variable font-lock-syntactic-face-function, documented below, determines which syntactic constructs to highlight. There are several variables that affect syntactic fontification; you should set them by means of font-lock-defaults (see Font Lock Basics).

Whenever Font Lock mode performs syntactic fontification on a stretch of text, it first calls the function specified by syntax-propertize-function. Major modes can use this to apply syntax-table text properties to override the buffer’s syntax table in special cases. See Syntax Properties.

Variable: font-lock-keywords-only

If the value of this variable is non-nil, Font Lock does not do syntactic fontification, only search-based fontification based on font-lock-keywords. It is normally set by Font Lock mode based on the keywords-only element in font-lock-defaults. If the value is nil, Font Lock will call jit-lock-register (see Other Font Lock Variables) to set up for automatic refontification of buffer text following a modified line to reflect the new syntactic context due to the change.

To use only syntactic fontification, this variable should be non-nil, while font-lock-keywords should be set to nil (see Font Lock Basics).

Variable: font-lock-syntax-table

This variable holds the syntax table to use for fontification of comments and strings. It is normally set by Font Lock mode based on the syntax-alist element in font-lock-defaults. If this value is nil, syntactic fontification uses the buffer’s syntax table (the value returned by the function syntax-table; see Syntax Table Functions).

Variable: font-lock-syntactic-face-function

If this variable is non-nil, it should be a function to determine which face to use for a given syntactic element (a string or a comment).

The function is called with one argument, the parse state at point returned by parse-partial-sexp, and should return a face. The default value returns font-lock-comment-face for comments and font-lock-string-face for strings (see Faces for Font Lock).

This variable is normally set through the “other” elements in font-lock-defaults:

(setq-local font-lock-defaults
            `(,python-font-lock-keywords
              nil nil nil nil
              (font-lock-syntactic-face-function
               . python-font-lock-syntactic-face-function)))