Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Text Highlighting in Tk::Text window

by Rich36 (Chaplain)
on May 24, 2002 at 19:18 UTC ( #169190=snippet: print w/ replies, xml ) Need Help??

Description:

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');
    }
}
Comment on Text Highlighting in Tk::Text window
Download Code

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://169190]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2014-07-26 05:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls