This code is used to highlight text in a Tk::Text window. This is useful in the context of highlighting some kind of tags, but could be used for any text. By changing a value, you can turn the text highlighting on and off.
Usage (from Tk widget "-command" option):
[\&toggleTextHighlighting \$highlight, \$regex, \$widget]
Where $highlight equals 1 or 2 (highlighting on or off), $regex is the regular expression used to highlight the text, and $widget is a reference to a Text widget.
sub toggleTextHighlighting {
my ($highlight, $regex, $widget) = @_;
# Deference the variables that you need to get values from
$highlight = ${$highlight};
$regex = ${$regex};
$widget = ${$widget};
# Create a tag to configure the text
$widget->tagConfigure('foundtag',
-foreground => "white",
-background => "red");
if ($highlight == 1) {
$widget->FindAll(-regex, -nocase, $regex);
if ($widget->tagRanges('sel')) {
my %startfinish = $widget->tagRanges('sel');
foreach(sort keys %startfinish) {
$widget->tagAdd("foundtag", $_, $startfinish{$_});
}
$widget->tagRemove('sel', '1.0', 'end');
}
} else {
$widget->tagRemove("foundtag", '1.0', 'end');
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.