‘font-lock-keywords’)You can find examples of font-lock expressions in AddKeywords and GenericMode.
Every Element has one of the following forms:
Here is an example for elisp. Assume you want to highlight the expression that explicitly loops over a list. If you never want to break out of the loop, or if you are using the CommonLisp package, you might use dolist. If not, however, it might look something like this:
(while lst
(setq item (car lst)
lst (cdr lst))
...
We now want to highlight “item”, and the three following lst instances within the setq. The following uses a long and complex regexp built using the ReBuilder.
Note that \\( and \\) do the grouping, and that \\sw matches a word character, and \\s_ matches a symbol character. Thus, \\(\\sw\\|\\s_\\)+ matches a variable name. [ \t\n]* matches whitespace including newlines.
(font-lock-add-keywords 'emacs-lisp-mode
'(("\\(\\(\\sw\\|\\s_\\)+\\)[ \t\n]*(car[ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)[ \t\n]*)[ \t\n]*\\(\\3\\)[ \t\n]*(cdr[ \t\n]*\\(\\3\\)[ \t\n]*)"
(1 'font-lock-variable-name-face)
(3 'font-lock-keyword-face)
(5 'font-lock-keyword-face)
(6 'font-lock-keyword-face))))