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'); } }

In reply to Text Highlighting in Tk::Text window by Rich36

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.