| 1 |
#!/usr/local/bin/perl -w |
| 2 |
|
| 3 |
# XXX Does not work: |
| 4 |
# "Can't locate object method "CLEAR" via package "Tk::Text::Contents" at tietext line 63" |
| 5 |
|
| 6 |
use Tk; |
| 7 |
|
| 8 |
{package Tk::Text::Contents; |
| 9 |
|
| 10 |
sub TIEARRAY |
| 11 |
{ |
| 12 |
print "TIEARRAY(",join(',',@_),")\n"; |
| 13 |
my ($class,$text) = @_; |
| 14 |
return bless [$text],$class; |
| 15 |
} |
| 16 |
|
| 17 |
sub FETCH |
| 18 |
{ |
| 19 |
# print "FETCH(",join(',',@_),")\n"; |
| 20 |
my ($obj,$key) = @_; |
| 21 |
my $text = ${$obj}[0]; |
| 22 |
$key++; |
| 23 |
return $text->get($key.".0" ,$key.".0+1 line"); |
| 24 |
} |
| 25 |
|
| 26 |
sub STORE |
| 27 |
{ |
| 28 |
# print "STORE(",join(',',@_),")\n"; |
| 29 |
my ($obj,$key,$val) = @_; |
| 30 |
my $text = ${$obj}[0]; |
| 31 |
$key++; |
| 32 |
while ($text->compare($key.".0",'>','end')) |
| 33 |
{ |
| 34 |
$text->insert('end',"\n"); |
| 35 |
} |
| 36 |
if ($text->compare($key.".0+1 line",'<','end')) |
| 37 |
{ |
| 38 |
$text->delete($key. ".0" ,$key.".0+1 line"); |
| 39 |
} |
| 40 |
# $val .= "\n" unless ($val =~ /\n$/); |
| 41 |
chomp($val); |
| 42 |
$text->insert($key . ".0" ,$val."\n"); |
| 43 |
print "$key:$val\n"; |
| 44 |
$text->update; |
| 45 |
} |
| 46 |
|
| 47 |
sub LENGTH |
| 48 |
{ |
| 49 |
my ($obj) = @_; |
| 50 |
my $text = ${$obj}[0]; |
| 51 |
print $text->index("end"),"\n"; |
| 52 |
my ($line,$col) = split(/\./,$text->index("end")); |
| 53 |
$line -= 3; |
| 54 |
return $line; |
| 55 |
} |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
$top = MainWindow->new; |
| 60 |
|
| 61 |
$text = $top->Text; |
| 62 |
$text->pack; |
| 63 |
|
| 64 |
tie @lines,Tk::Text::Contents,$text; |
| 65 |
|
| 66 |
|
| 67 |
@lines = <>; |
| 68 |
|
| 69 |
my $line; |
| 70 |
|
| 71 |
foreach $line (@lines) |
| 72 |
{ |
| 73 |
# print "Got $_"; |
| 74 |
# $line = "New:" . $line; |
| 75 |
s/^/Added:/; |
| 76 |
} |
| 77 |
|
| 78 |
MainLoop; |