unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: help-gnu-emacs@gnu.org
Subject: Re: Font lock question
Date: Wed, 17 Mar 2021 11:33:30 -0400	[thread overview]
Message-ID: <jwvsg4tiyt6.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: 87r1kdx1ge.fsf@fastmail.fm

>>> 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	other threads:[~2021-03-17 15:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-17  9:40 Font lock question Joost Kremers
2021-03-17 13:39 ` Stefan Monnier
2021-03-17 13:46   ` Joost Kremers
2021-03-17 15:33     ` Stefan Monnier [this message]
2021-03-17 16:09       ` Joost Kremers
2021-03-17 16:46         ` Stefan Monnier
2021-03-18 19:41           ` Joost Kremers
2021-03-17 16:31     ` Harald Jörg
2021-03-18 19:53       ` Joost Kremers
  -- strict thread matches above, loose matches on Subject: below --
2008-05-28 10:34 Shaun Johnson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvsg4tiyt6.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).