GrandFather has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to provide a default set of tags for characters that are inserted into a Tk::Text widget?

I know that a list of tags can be provided using $text->insert(index, chars, ?tagList, chars, tagList, ...?), but what I want to achieve is applying tags to characters as they are entered from the keyboard. I guess I could do it with a callback bound to the Button event and 'manually' insert the characters at the current insertion point. That seems a bit hands on though.

Update: it turns out part of the problem is that Tk::TextUndo breaks Tk:Text's behaviour whereby characters that are inserted acquire the attributes of the character to the left of the insertion point. It is this problem that I am trying to fix. An option is just to use the Tk::Text widget and forgo the undo provided by Tk::TextUndo.

Update: a bug report has been filed on rt.cpan.org for this problem.


DWIM is Perl's answer to Gödel
  • Comment on Providing default tags for characters inserted into a Tk::Text widget
  • Download Code

Replies are listed 'Best First'.
Re: Providing default tags for characters inserted into a Tk::Text widget
by zentara (Cardinal) on Apr 06, 2006 at 12:46 UTC
    Just off the top of my head ( or, depending on how you look at it, pulled out of my a**) :-) , here is a snippet I found laying around, that might give you ideas. You could setup the callback to detect the key, then insert with tags, assigned from a hash or something. I havn't tested it with tags and what Undo will do with it.
    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::TextUndo; my $top = MainWindow->new; my $text = $top->TextUndo->pack( -expand => 'yes', -fill => 'both' ); $text->bindtags( [ $text, ref($text), $text->toplevel, 'all' ] ); $text->bind( '<KeyPress>', [ \&callback, Ev('K') ] ); MainLoop; sub callback { my ( $widget, $keysym ) = @_; print "\n$keysym key pressed"; return if ( ( length($keysym) > 1 and $keysym =~ /\p{Upper}/ and $keysym !~ /Delete|BackSpace|Return|Tab/ ) or ( $keysym eq 'Delete' and $widget->index('insert') eq $widget->index('end -1c') +) or ( $keysym eq 'BackSpace' and $widget->index('insert') eq '1.0' ) ); print ' - text is edited.'; }

    I'm not really a human, but I play one on earth. flash japh