-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

On 07-Oct-2019 13:51 +0200, Dominique Pellé wrote:

> Hi
> 
> Vim now highlights matching text when typing a Ex command like
> :%s/foobar/ I like it. But I wish that it would use a different
> color for the regex captured group(s).
> 
> For example, if I do   :%s/foo\(bar\)/ I would like "bar" (the
> captured group) to be highlighted with a different color than foo 
> in matching text, so it's easier to visually see that my regexp
> matches and what \1 is when I do  :%s/foo\(bar\)/\1/.  In this
> example the regexp and captured group are obvious of course, so a
> different color may not seem useful. But it becomes more useful
> when the the regex is more complex.
> 
> There are 10 possibly regexp groups.  Having 10 different colors
> might be overkill (not sure). The same color for any group may good
> enough, or perhaps alternative colors for \1 \2 \3...?
> 
> I wonder whether other vim users would also find it useful.

I like this idea. It can help by visualizing a complex pattern (while
it's being composed), and it could also be (mis?)used to have multiple
patterns highlighted with different colors in parallel, e.g.
/\(foo\)\|\(bar\)/ would show "foo" in color 1 and "bar" in color 2.
There's definitely demand for that, currently implemented by plugins
(such as my own Mark.vim,
http://www.vim.org/scripts/script.php?script_id=2666) via matchadd().

Regarding the mechanics, I think the biggest challenge lies in the
association of capture group number to highlight group. As your
questions show, this mostly isn't static, but would depend on each
particular pattern. Of course, one could use an off-band mechanism
like separate :hi link Search1 ... | hi link Search2 ... Ex commands
to reconfigure the colors, but that would be cumbersome.

I would suggest to decouple the capture groups from the highlighting,
and instead extend the non-capturing group syntax (\%(...\)) to
optionally supply a color number: \%1(...\) uses Search1, \%2(...\)
uses Search2, and so on. (This is modeled after \%23l, \%23c, ..., and
would not break backwards compatibility.) This allows an in-band
group-to-color mapping, with the downside that captured groups would
have to be explicitly wrapped in a numbered non-capturing group; the
example from above would become a more verbose
/\%1(\(foo\)\)\|\%2(\(bar\)\)/; the benefit would be that another
capture group can be easily added that also uses color 1, or 2, or no
special color at all.

- -- regards, ingo

- -- 
  -- Ingo Karkat --  /^-- /^-- /^-- /^-- /^-- http://ingo-karkat.de/ --
  --      https://www.vim.org/account/profile.php?user_id=9713       --
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBCAAGBQJdm6leAAoJEA7ziXlAzQ/vKIAH/3xpmi7MdaUIJYmbTQ5Xw7CH
cRJTWLFCR5LEQizS+ZKkGjWSuTifpGNBONOfMtsKWyFi4IMuHDIARCea0C1g+DXS
8S8WjQiaQrg2y9PaLiy9rsDmN4nG5Yh5tMaTiQ7eHYKieqq6wxUkzMOouMjtM0ER
TB9JEJDBzfD/Sm36IOfsAr4f8dycSOShZ19PuQSEziWetubTsQ8Csin9M1X+13NA
0oFfM794940huRIBWTGlfiUA+nNtFni6tdsI7B7Uc5WSjfgZc+7qP0Dm+aAdY+UM
+C8Id9eHDwNG1JdKTXrYK5jclnwBSh2cQM2ElU39Lid0JRLVlQIH8zvxXhVh77o=
=jzvb
-----END PGP SIGNATURE-----

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/54680419-6f54-d00e-7c80-b70b839d34b2%40ingo-karkat.de.

Raspunde prin e-mail lui