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

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

Re: Font lock question


From: Harald Jörg
Subject: Re: Font lock question
Date: Wed, 17 Mar 2021 17:31:35 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux)

Joost Kremers <joostkremers@fastmail.fm> writes:

> On Wed, Mar 17 2021, Stefan Monnier wrote:
>> [...]
>> 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.
>
> 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))))
> ```

I had a similar case recently, and also failed to solve it with one
regular expression: After all, the match (including the closing "]")
succeeds only once, so there's no more than one capture group for 1, 2,
and 3 each.

My solution was to to combine a rule for [aBc] with a rule of type
(MATCHER . ANCHORED-HIGHLIGHTER): The anchor is the opening "[" and the
first "aBc", and the anchored matcher matches a semicolon and, as a
group, another "aBc".

I just saw that Stefan said this would work, so I'll give it a try:
```
(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))
        ("[[:alnum:]]*B[[:alnum:]]*"
    (";[[:blank:]]*\\(?1:[[:alnum:]]*\\)\\(?2:B\\)\\(?3:[[:alnum:]]*\\)"
     nil nil
     (1 font-lock-warning-face)
     (2 font-lock-keyword-face)
     (3 font-lock-warning-face)))))
```

-- 
Cheers,
haj



reply via email to

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