help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Font lock question


From: Stefan Monnier
Subject: Re: Font lock question
Date: Wed, 17 Mar 2021 11:33:30 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

>>> I have no trouble composing a regex that matches this entire thing.  I'd 
>>> add a
>>> shy group that includes an optional semicolon and that is repeated at least
>>> once:
>>>
>>>     \[\(?:\(?1:.*?\)B\(?2:.*?\);?\)+\]
>>>
>>> However, this does not work for font-lock, it seems.
>>
>> Can you clarify what you mean by "doe snot work"?
>
> Of course. :-) What I mean is that in my test case, e.g., with the following
> text in the buffer:
>
>     [aBc; aBc; aBc]
>
> only the final `aBc` has the right font-lock faces applied to it. The first 
> two
> occurrences of `aBc` have no special face.

Ah I see: Emacs's regular expression engine is like that of POSIX and
most other languages: it only keeps track of the last repetition of
a given subgroup in the match-data it returns.

> I'm testing with the following:
>
> ```
> (font-lock-add-keywords nil
>                         
> '(("\\[\\(?:\\(?1:[[:alnum:]]*?\\)\\(?2:B\\)\\(?3:[[:alnum:]]*?\\)[;[:blank:]]*?\\)+\\]"
>                            (1 font-lock-warning-face)
>                            (2 font-lock-keyword-face)
>                            (3 font-lock-warning-face))))

Indeed that won't do what you want.  You're going to have to either
manually loop through all the matches of of aBc within the brackets, or
use font-lock-keywords's `MATCH-ANCHORED` to do that for you.

But be aware that these approaches still won't work well if the [...]
thing can span multiple lines.


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]