views:

42

answers:

1

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?

+2  A: 

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.

Paul Stephenson