Yes, you can. See the Elisp manual, node Search-based Fontification.
font-lock-keywords
(which you set using font-lock-keywords
, e.g. font-lock-add-keywords
) can use a FACESPEC
expression, which is evaluated to provide the face to use.
FACESPEC
is an expression whose value
specifies the face to use for highlighting. In the simplest case,
FACESPEC
is a Lisp variable (a symbol) whose value is a face name.
;; Highlight occurrences of ‘fubar’,
;; using the face which is the value of ‘fubar-face’.
("fubar" . fubar-face)
However, FACESPEC
can also evaluate to a list of this form:
(face FACE PROP1 VAL1 PROP2 VAL2...)
to specify the face FACE
and various additional text properties to
put on the text that matches. If you do this, be sure to add the
other text property names that you set in this way to the value of
font-lock-extra-managed-props
so that the properties will also be
cleared out when they are no longer appropriate. Alternatively,
you can set the variable font-lock-unfontify-region-function
to a
function that clears these properties. *Note Other Font Lock Variables::.