Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font-lock-keywords: highlighting multiple sub-expression

Tags:

When setting up font-lock-keywords for a GNU/Emacs mode, is it possible to highlight multiple sub-expressions of a regular expression with a single matcher? What I'd like to do is something along the lines of:

("\\(foo\\)-\\(bar\\)" '(1 foo-face) '(2 bar-face))

What would be the correct syntax for something like that - or do I have to split it up into two distinct matchers?

like image 507
Thomas Avatar asked Feb 10 '10 13:02

Thomas


1 Answers

Try something like this:

("\\(foo\\)-\\(bar\\)" (1 foo-face) (2 bar-face))

(i.e. the same as yours but without the extra quotes).

I say this because I have various bits like this in my custom font-lock-keywords definitions. Some of them have nil t on the end of the second one, like this:

("\\(foo\\)-\\(bar\\)" (1 foo-face) (2 bar-face nil t))

which correspond to the OVERRIDE and LAXMATCH optional flags and may be necessary depending on your precise circumstances.

The documentation for font-lock-keywords discusses this in some depth, although it's not always the simplest to follow -- I find it easier just to copy someone else's working setup, like the existing value of c-font-lock-keywords-3, for example.

like image 164
Paul Stephenson Avatar answered Nov 15 '22 06:11

Paul Stephenson