开发者

Font-lock-keywords: highlighting multiple sub-expression

开发者 https://www.devze.com 2022-12-19 14:32 出处:网络
When setting up font-lock-ke开发者_如何学Cywords 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 somet

When setting up font-lock-ke开发者_如何学Cywords 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号