Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using regexp and tags in a text-widget

6 views
Skip to first unread message

svogel

unread,
Feb 19, 2003, 8:02:57 AM2/19/03
to
Hi,

I'd like to build some tags in a text-widget based on regular
expressions.
E.g. highlight all "HTML"-Tags.
Now I can get all indices of Tags with
regexp -indices
and I can attach tags to some text with
$txt_widget add html index1 ...

What would be very nice is something like:
eval $txt_widget tag add html [regexp -indices -all "<.*?>"
[$txt_widget get 0.0 end]]

But the indices returned by regexp doesn't match the indices from the
text-widget, which have the form "line.char".

Using "$txt_widget search regexp" is somehow clumsy because it only
returns
the first match.

Has someone managed to find a good solution for that?
Is there some example on that?

Thanks in advance
Stefan

Jeffrey Hobbs

unread,
Feb 20, 2003, 1:42:41 AM2/20/03
to
svogel wrote:
> E.g. highlight all "HTML"-Tags.
> Now I can get all indices of Tags with
> regexp -indices
> and I can attach tags to some text with
> $txt_widget add html index1 ...
>
> What would be very nice is something like:
> eval $txt_widget tag add html [regexp -indices -all "<.*?>"
> [$txt_widget get 0.0 end]]
>
> But the indices returned by regexp doesn't match the indices from the
> text-widget, which have the form "line.char".
>
> Using "$txt_widget search regexp" is somehow clumsy because it only
> returns
> the first match.
>
> Has someone managed to find a good solution for that?
> Is there some example on that?

Hmmm, I know I have an example of this somewhere in my bag of tricks ...
Here are a few things I found which probably relate:

proc for_all_matches { widget start finish pattern script } {
$widget mark set first $start
while {[set ix [$widget search -regexp -count numc -- \
$pattern first $finish]] != ""} {
$widget mark set first $ix
$widget mark set last "$ix + $numc c"
uplevel $script
$widget mark set first last
}
}

proc find_matches { widget start finish string sargs script } {
$widget mark set first $start
if {[lsearch $sargs {-backwards}] != -1} {
while {[set ix [eval $widget search $sargs -count numc -- \
$string first $finish]] != ""} {
$widget mark set first $ix
$widget mark set last "$ix + $numc c"
uplevel $script
$widget mark set first first-1c
}
} else {
while {[set ix [eval $widget search $sargs -count numc -- \
$string first $finish]] != ""} {
$widget mark set first $ix
$widget mark set last "$ix + $numc c"
uplevel $script
$widget mark set first last
}
}
}

proc tag_range { ix ranges } {
scan $ix %d.%d ixl ixc
for {} {[string compare $ranges {}]} \
{set ranges [lreplace $ranges 0 1]} {
scan [lindex $ranges 0] %d.%d l0 c0
scan [lindex $ranges 1] %d.%d l1 c1
if {$ixl >= $l0 && $ixl <= $l1} {
if {$l0 == $l1} {
if {$ixc >= $c0 && $ixc <= $c1} {
return [lrange $ranges 0 1]
}
} elseif {$ixl != $l0 && $ixl != $l1} {
return [lrange $ranges 0 1]
} elseif {$ixl == $l0} {
if {$ixc >= $c0} { return [lrange $ranges 0 1] }
} elseif {$ixl == $l1} {
if {$ixc <= $c1} { return [lrange $ranges 0 1] }
}
}
}
return {}
}


--
Jeff Hobbs The Tcl Guy
Senior Developer http://www.ActiveState.com/
Tcl Support and Productivity Solutions

svogel

unread,
Feb 21, 2003, 6:14:34 AM2/21/03
to
Thanks Jeffrey.

I think I can use some of the procs.
I though my first try was so easy and straigtforward that there had to
be a way in Tcl to do so. :-(

But the hardway is o.k.
Thanks again
Stefan

Jeff Hobbs

unread,
Feb 23, 2003, 5:31:51 PM2/23/03
to
[posted and mailed]

stefan...@avinci.de (svogel) wrote in news:e50b2925.0302210314.130c65a8
@posting.google.com:

> I think I can use some of the procs.
> I though my first try was so easy and straigtforward that there had to
> be a way in Tcl to do so. :-(
>
> But the hardway is o.k.

That's not really the hard way. The procs are not that long, and just
build on the core functionality that the text widget provides. At some
level, the programmer has to make his living writing *something*. ;^)

0 new messages