A regular expression extension

1 view
Skip to first unread message

Tero Jäntti

unread,
Nov 23, 2007, 5:19:58 PM11/23/07
to Reinteract
Hi there!

I've started making an extension for presenting matches for regular
expressions. It's name is - you guessed it - rere. It can be found
here:

http://code.google.com/p/xprext/

Currently, all it provides is a search function which resembles the
re.search function from the standard library. Of course, the re.sub
and re.match are going to be implemented as well. The search function
returns a TextView where the matches are highlighted and matched
groups are underlined. The idea is the same as in redemo.py program
which comes with the standard Python distribution (Python-2.5/Tools/
scripts/redemo.py).

So, any further ideas? Comments? Opinions on how the matches should be
presented?

Owen Taylor

unread,
Nov 25, 2007, 4:34:40 PM11/25/07
to reint...@googlegroups.com

Nice! Some minor comments fooling around with it for a few minutes.

- view.set_editable(False)
- I don't see much of a reason to use named tags in PyGTK ... instead of

text.create_tag("match", background="green")
text.apply_tag_by_name("match",
text.get_iter_at_offset(start),
text.get_iter_at_offset(end))

I would generally do:

match_tag = text.create_tag(background="green")
text.apply_tag(match_tag, ...)

- Some internationalization issues ... if typeof(string) == str, then
m.span() gives you bytes not characters. GtkTextBuffer, sadly, doesn't
have get_iter_at_index() . So, the simpliest thing is to probably
string.decode("UTF-8") in that case and the same for pattern.

- it would be nice if the highlighting gave some idea of what the
divisions were between different matches and different groups within
matches. The simple way to do this would be to alternate between
different colors as you go between matches and alternate between
different underline styles, maybe single and double, as you go between
matches.

More ambitiously, I think it would be neat to have tooltip-like popups
to show the matched portion of the pattern string.

- Owen

Tero Jäntti

unread,
Nov 25, 2007, 5:55:22 PM11/25/07
to Reinteract


On Nov 25, 11:34 pm, "Owen Taylor" <owtay...@gmail.com> wrote:
>
> - view.set_editable(False)

Fixed.

> - I don't see much of a reason to use named tags in PyGTK ... instead of
>
> text.create_tag("match", background="green")
> text.apply_tag_by_name("match",
> text.get_iter_at_offset(start),
> text.get_iter_at_offset(end))
>
> I would generally do:
>
> match_tag = text.create_tag(background="green")
> text.apply_tag(match_tag, ...)

Done.

> - Some internationalization issues ... if typeof(string) == str, then
> m.span() gives you bytes not characters. GtkTextBuffer, sadly, doesn't
> have get_iter_at_index() . So, the simpliest thing is to probably
> string.decode("UTF-8") in that case and the same for pattern.

OK, I'll look at that later.

> - it would be nice if the highlighting gave some idea of what the
> divisions were between different matches and different groups within
> matches. The simple way to do this would be to alternate between
> different colors as you go between matches and alternate between
> different underline styles, maybe single and double, as you go between
> matches.
>

That is under work. So far it alternates colors between groups. Adding
alternation to whole matches needs some thought. See:

http://code.google.com/p/xprext/issues/detail?id=3&can=1

> More ambitiously, I think it would be neat to have tooltip-like popups
> to show the matched portion of the pattern string.

Something like

---------------
"Foo Bar"
Match 2 / 6
Group 1 / 2
---------------

perhaps?

Owen Taylor

unread,
Nov 26, 2007, 8:19:25 PM11/26/07
to reint...@googlegroups.com
On Nov 25, 2007 5:55 PM, Tero Jäntti <tkja...@gmail.com> wrote:
[...]

> > - it would be nice if the highlighting gave some idea of what the
> > divisions were between different matches and different groups within
> > matches. The simple way to do this would be to alternate between
> > different colors as you go between matches and alternate between
> > different underline styles, maybe single and double, as you go between
> > matches.
> >
>
> That is under work. So far it alternates colors between groups. Adding
> alternation to whole matches needs some thought. See:
>
> http://code.google.com/p/xprext/issues/detail?id=3&can=1

Hmm, it's going to be hard to use colors for groups and matches ...
you might be able to do something with shades of blue for the first
match, shades of red for the second match, etc, but the result might
not be at all comprehensible. Another idea ... what if you highlighted
one match in full color with the group alternation, then highlighted
the other matches with a gray background, and had next/prev buttons
underneath the whole thing to move the highlighted group. (I'm can
come up with all sorts of ideas. That doesn't mean they are any good
;-)

> > More ambitiously, I think it would be neat to have tooltip-like popups
> > to show the matched portion of the pattern string.
>
> Something like
>
> ---------------
> "Foo Bar"
> Match 2 / 6
> Group 1 / 2
> ---------------
>
> perhaps?

I was thinking that the tooltip would actually show the regular
expression with the group highlighted, but that obviously is a bit of
a pain to implement, since you'd have to parse the regular expression
and figure out where the match groups are. Which is probably not worth
the effort.

- Owen

Tero Jäntti

unread,
Nov 27, 2007, 2:26:23 PM11/27/07
to Reinteract


On Nov 27, 3:19 am, "Owen Taylor" <owtay...@gmail.com> wrote:
>
> Hmm, it's going to be hard to use colors for groups and matches ...
> you might be able to do something with shades of blue for the first
> match, shades of red for the second match, etc, but the result might
> not be at all comprehensible. Another idea ... what if you highlighted
> one match in full color with the group alternation, then highlighted
> the other matches with a gray background, and had next/prev buttons
> underneath the whole thing to move the highlighted group. (I'm can
> come up with all sorts of ideas. That doesn't mean they are any good
> ;-)

Hmm... well... I'll think about it... :^)

Seriously though, I think that highlighting matches with slightly
alternating background colors and groups with single/double underline
is the way to go. At least it remains uncluttered.

>
> I was thinking that the tooltip would actually show the regular
> expression with the group highlighted, but that obviously is a bit of
> a pain to implement, since you'd have to parse the regular expression
> and figure out where the match groups are. Which is probably not worth
> the effort.

Agreed.
Reply all
Reply to author
Forward
0 new messages