Tk-HistEntry-0.46/0000755000175000017500000000000015040140713013303 5ustar eserteeserteTk-HistEntry-0.46/HistEntry.pm0000644000175000017500000004152515040135312015600 0ustar eserteeserte# -*- perl -*- # # Author: Slaven Rezic # # Copyright © 1997, 2000, 2001, 2003, 2008, 2016, 2017 Slaven Rezic. All rights reserved. # This package is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # # Mail: slaven@rezic.de # WWW: http://www.cs.tu-berlin.de/~eserte/ # package Tk::HistEntry; require Tk; use strict; use vars qw($VERSION); $VERSION = '0.46'; sub addBind { my $w = shift; $w->_entry->bind('' => sub { $w->historyUp }); $w->_entry->bind('' => sub { $w->historyUp }); $w->_entry->bind('' => sub { $w->historyDown }); $w->_entry->bind('' => sub { $w->historyDown }); $w->_entry->bind('' => sub { $w->historyBegin }); $w->_entry->bind('' => sub { $w->historyBegin }); $w->_entry->bind('' => sub { $w->historyEnd }); $w->_entry->bind('' => sub { $w->historyEnd }); $w->_entry->bind('' => sub { $w->searchBack }); $w->_entry->bind('' => sub { $w->searchForw }); $w->_entry->bind('' => sub { if ($w->cget(-command) || $w->cget(-auto)) { $w->invoke; } }); $w->_entry->bind('', sub { my $e = $_[0]->XEvent; $w->KeyPress($e->K, $e->s); }); } # XXX del: # sub _isdup { # my($w, $string) = @_; # foreach (@{ $w->privateData->{'history'} }) { # return 1 if $_ eq $string; # } # 0; # } sub _update { my($w, $string) = @_; $w->_entry->delete(0, 'end'); $w->_entry->insert('end', $string); } sub _entry { my $w = shift; $w->Subwidget('entry') ? $w->Subwidget('entry') : $w; } sub _listbox { my $w = shift; $w->Subwidget('slistbox') ? $w->Subwidget('slistbox') : $w; } sub _listbox_method { my $w = shift; my $meth = shift; if ($w->_has_listbox) { $w->_listbox->$meth(@_); } } sub _has_listbox { $_[0]->Subwidget('slistbox') } sub historyAdd { my($w, $string, %args) = @_; $string = $w->_entry->get unless defined $string; return undef if !defined $string || $string eq ''; my $history = $w->privateData->{'history'}; if (!@$history or $string ne $history->[-1]) { my $spliced = 0; if (!$w->cget(-dup)) { for(my $i = 0; $i<=$#$history; $i++) { if ($string eq $history->[$i]) { splice @$history, $i, 1; $spliced++; last; } } } push @$history, $string; if (defined $w->cget(-limit) && @$history > $w->cget(-limit)) { shift @$history; } $w->privateData->{'historyindex'} = $#$history + 1; my @ret = $string; if ($args{-spliceinfo}) { push @ret, $spliced; } return @ret; } undef; } # compatibility with Term::ReadLine *addhistory = \&historyAdd; sub historyUpdate { my $w = shift; $w->_update($w->privateData->{'history'}->[$w->privateData->{'historyindex'}]); $w->_entry->icursor('end'); # suggestion by Jason Smith $w->_entry->xview('insert'); } sub historyUp { my $w = shift; if ($w->privateData->{'historyindex'} > 0) { $w->privateData->{'historyindex'}--; $w->historyUpdate; } else { $w->_bell; } } sub historyDown { my $w = shift; if ($w->privateData->{'historyindex'} <= $#{$w->privateData->{'history'}}) { $w->privateData->{'historyindex'}++; $w->historyUpdate; } else { $w->_bell; } } sub historyBegin { my $w = shift; $w->privateData->{'historyindex'} = 0; $w->historyUpdate; } sub historyEnd { my $w = shift; $w->privateData->{'historyindex'} = $#{$w->privateData->{'history'}}; $w->historyUpdate; } sub historySet { my($w, $index) = @_; my $i; my $history_ref = $w->privateData->{'history'}; for($i = $#{ $history_ref }; $i >= 0; $i--) { if ($index eq $history_ref->[$i]) { $w->privateData->{'historyindex'} = $i; last; } } } sub historyReset { my $w = shift; $w->privateData->{'history'} = []; $w->privateData->{'historyindex'} = 0; $w->_listbox_method("delete", 0, "end"); } sub historySave { my($w, $file) = @_; open(W, ">$file") or die "Can't save to file $file"; print W join("\n", $w->history) . "\n"; close W; } # XXX document sub historyMergeFromFile { my($w, $file) = @_; if (open(W, "<$file")) { while() { chomp; $w->historyAdd($_); } close W; } } sub history { my($w, $history) = @_; if (defined $history) { $w->privateData->{'history'} = [ @$history ]; $w->privateData->{'historyindex'} = $#{$w->privateData->{'history'}} + 1; } @{ $w->privateData->{'history'} }; } sub searchBack { my $w = shift; my $i = $w->privateData->{'historyindex'}-1; while ($i >= 0) { my $search = $w->_entry->get; if ($search eq substr($w->privateData->{'history'}->[$i], 0, length($search))) { $w->privateData->{'historyindex'} = $i; $w->_update($w->privateData->{'history'}->[$w->privateData->{'historyindex'}]); return; } $i--; } $w->_bell; } sub searchForw { my $w = shift; my $i = $w->privateData->{'historyindex'}+1; while ($i <= $#{$w->privateData->{'history'}}) { my $search = $w->_entry->get; if ($search eq substr($w->privateData->{'history'}->[$i], 0, length($search))) { $w->privateData->{'historyindex'} = $i; $w->_update($w->privateData->{'history'}->[$w->privateData->{'historyindex'}]); return; } $i++; } $w->_bell; } sub invoke { my($w, $string) = @_; $string = $w->_entry->get if !defined $string; return unless defined $string; my $added = defined $w->historyAdd($string); $w->Callback(-command => $w, $string, $added); } sub _bell { my $w = shift; return unless $w->cget(-bell); $w->bell; } sub KeyPress { my($w, $key, $state) = @_; my $e = $w->_entry; my(@history) = reverse $w->history; $w->{end} = $#history; # XXXXXXXX? return if ($key =~ /^Shift|^Control|^Left|^Right|^Home|^End/); return if ($state =~ /^Control-/); if ($key eq 'Tab') { # Tab doesn't trigger FocusOut event so clear selection $e->selection('clear'); return; } return if (!$w->cget(-match)); $e->update; my $cursor = $e->index('insert'); if ($key eq 'BackSpace' or $key eq 'Delete') { $w->{start} = 0; $w->{end} = $#history; return; } my $text = $e->get; ###Grab test from entry upto cursor (my $typedtext = $text) =~ s/^(.{$cursor})(.*)/$1/; if ($2 ne "") { ###text after cursor, do not use matching return; } if ($cursor == 0 || $text eq '') { ###No text before cursor, reset list $w->{start} = 0; $w->{end} = $#history; $e->delete(0, 'end'); $e->insert(0,''); } else { my $start = $w->{start}; my $end = $w->{end}; my ($newstart, $newend); ###Locate start of matching & end of matching my $caseregex = ($w->cget(-case) ? "(?i)" : ""); for (; $start <= $end; $start++) { if ($history[$start] =~ /^$caseregex\Q$typedtext\E/) { $newstart = $start if (!defined $newstart); $newend = $start; } else { last if (defined $newstart); } } if (defined $newstart) { $e->selection('clear'); $e->delete(0, 'end'); $e->insert(0, $history[$newstart]); $e->selection('range',$cursor,'end'); $e->icursor($cursor); $w->{start} = $newstart; $w->{end} = $newend; } else { $w->{end} = -1; } } } ###################################################################### package Tk::HistEntry::Simple; require Tk::Entry; use vars qw(@ISA); @ISA = qw(Tk::Derived Tk::Entry Tk::HistEntry); #use base qw(Tk::Derived Tk::Entry Tk::HistEntry); Construct Tk::Widget 'SimpleHistEntry'; sub CreateArgs { my($package, $parent, $args) = @_; $args->{-class} = "SimpleHistEntry" unless exists $args->{-class}; $package->SUPER::CreateArgs($parent, $args); } sub Populate { my($w, $args) = @_; $w->historyReset; $w->SUPER::Populate($args); $w->Advertise(entry => $w); $w->{start} = 0; $w->{end} = 0; $w->addBind; $w->ConfigSpecs (-command => ['CALLBACK', 'command', 'Command', undef], -auto => ['PASSIVE', 'auto', 'Auto', 0], -dup => ['PASSIVE', 'dup', 'Dup', 1], -bell => ['PASSIVE', 'bell', 'Bell', 1], -limit => ['PASSIVE', 'limit', 'Limit', undef], -match => ['PASSIVE', 'match', 'Match', 0], -case => ['PASSIVE', 'case', 'Case', 1], -history => ['METHOD'], ); $w; } ###################################################################### package Tk::HistEntry::Browse; require Tk::BrowseEntry; use vars qw(@ISA); @ISA = qw(Tk::Derived Tk::BrowseEntry Tk::HistEntry); #use base qw(Tk::Derived Tk::BrowseEntry Tk::HistEntry); Construct Tk::Widget 'HistEntry'; sub CreateArgs { my($package, $parent, $args) = @_; $args->{-class} = "HistEntry" unless exists $args->{-class}; $package->SUPER::CreateArgs($parent, $args); } sub Populate { my($w, $args) = @_; $w->historyReset; if ($Tk::VERSION >= 800) { $w->SUPER::Populate($args); } else { my $saveargs; foreach (qw(-auto -command -dup -bell -limit -match -case)) { if (exists $args->{$_}) { $saveargs->{$_} = delete $args->{$_}; } } $w->SUPER::Populate($args); foreach (keys %$saveargs) { $args->{$_} = $saveargs->{$_}; } } $w->addBind; $w->{start} = 0; $w->{end} = 0; my $entry = $w->Subwidget('entry'); $w->ConfigSpecs (-command => ['CALLBACK', 'command', 'Command', undef], -auto => ['PASSIVE', 'auto', 'Auto', 0], -dup => ['PASSIVE', 'dup', 'Dup', 1], -bell => ['PASSIVE', 'bell', 'Bell', 1], -limit => ['PASSIVE', 'limit', 'Limit', undef], -match => ['PASSIVE', 'match', 'Match', 0], -case => ['PASSIVE', 'case', 'Case', 1], -history => ['METHOD'], ); ## Delegation does not work with the new BrowseEntry --- it seems to me ## that delegation only works for composites, not for derivates # $w->Delegates('delete' => $entry, # 'get' => $entry, # 'insert' => $entry, # ); $w; } sub delete { shift->Subwidget('entry')->delete(@_) } sub get { shift->Subwidget('entry')->get (@_) } sub insert { shift->Subwidget('entry')->insert(@_) } sub historyAdd { my($w, $string) = @_; my($inserted, $spliced) = $w->SUPER::historyAdd($string, -spliceinfo => 1); if (defined $inserted) { if ($spliced) { $w->history([ $w->SUPER::history ]); } else { $w->_listbox_method("insert", 'end', $inserted); # XXX Obeying -limit also for the array itself? if (defined $w->cget(-limit) && $w->_listbox_method("size") > $w->cget(-limit)) { $w->_listbox_method("delete", 0); } } $w->_listbox_method("see", 'end'); return $inserted; } undef; } *addhistory = \&historyAdd; sub history { my($w, $history) = @_; if (defined $history) { $w->_listbox_method("delete", 0, 'end'); $w->_listbox_method("insert", 'end', @$history); $w->_listbox_method("see", 'end'); } $w->SUPER::history($history); } 1; =head1 NAME Tk::HistEntry - Entry widget with history capability =head1 SYNOPSIS use Tk::HistEntry; $hist1 = $top->HistEntry(-textvariable => \$var1); $hist2 = $top->SimpleHistEntry(-textvariable => \$var2); =head1 DESCRIPTION C defines entry widgets with history capabilities. The widgets come in two flavours: =over 4 =item C (in package C) - with associated browse entry =item C (in package C) - plain widget without browse entry =back The user may browse with the B and B keys through the history list. New history entries may be added either manually by binding the B key to B or automatically by setting the B<-command> option. =head1 OPTIONS B is an descendant of B and thus supports all of its standard options. B is an descendant of B and supports all of the B options. In addition, the widgets support following specific options: =over 4 =item B<-textvariable> or B<-variable> Variable which is tied to the HistEntry widget. Either B<-textvariable> (like in Entry) or B<-variable> (like in BrowseEntry) may be used. =item B<-command> Specifies a callback, which is executed when the Return key was pressed or the B method is called. The callback reveives three arguments: the reference to the HistEntry widget, the current textvariable value and a boolean value, which tells whether the string was added to the history list (e.g. duplicates and empty values are not added to the history list). =item B<-dup> Specifies whether duplicate entries are allowed in the history list. Defaults to true. =item B<-bell> If set to true, rings the bell if the user tries to move off of the history or if a search was not successful. Defaults to true. =item B<-limit> Limits the number of history entries. Defaults to unlimited. =item B<-match> Turns auto-completion on. =item B<-case> If set to true a true value, then be case sensitive on auto-completion. Defaults to 1. =back =head1 METHODS =over 4 =item B[I]B<)> Adds string (or the current textvariable value if not set) manually to the history list. B is an alias for B. Returns the added string or undef if no addition was made. =item B[I]B<)> Invokes the command specified with B<-command>. =item B[I]B<)> Without argument, returns the current history list. With argument (a reference to an array), replaces the history list. =item BIB<)> Save the history list to the named file. =item BIB<)> Merge the history list from the named file to the end of the current history list of the widget. =item B Remove all entries from the history list. =back =head1 KEY BINDINGS =over 4 =item B, B Selects the previous history entry. =item B, B Selects the next history entry. =item B>, B> Selects first entry. =item B>, B> Selects last entry. =item B The current content of the widget is searched backward in the history. =item B The current content of the widget is searched forward in the history. =item B If B<-command> is set, adds current content to the history list and executes the associated callback. =back =head1 EXAMPLE This is an simple example for Tk::HistEntry. More examples can be found in the t and examples directories of the source distribution. use Tk; use Tk::HistEntry; $top = new MainWindow; $he = $top->HistEntry(-textvariable => \$foo, -command => sub { # automatically adds $foo to history print STDERR "Do something with $foo\n"; })->pack; $b = $top->Button(-text => 'Do it', -command => sub { $he->invoke })->pack; MainLoop; If you like to not depend on the installation of Tk::HistEntry, you can write something like this: $Entry = "Entry"; # default Entry widget eval { # try loading the module, otherwise $Entry is left to the value "Entry" require Tk::HistEntry; $Entry = "SimpleHistEntry"; }; $entry = $mw->$Entry(-textvariable => \$res)->pack; $entry->bind("" => sub { # check whether the historyAdd method is # known to the widget if ($entry->can('historyAdd')) { $entry->historyAdd; } }); In this approach the history lives in an array variable. Here the entry widget does not need to be permanent, that is, it is possible to destroy the containing window and restore the history again: $Entry = "Entry"; eval { require Tk::HistEntry; $Entry = "HistEntry"; }; $entry = $mw->$Entry(-textvariable => \$res)->pack; if ($entry->can('history') && @history) { $entry->history(\@history); } # Later, after clicking on a hypothetical "Ok" button: if ($res ne "" && $entry->can('historyAdd')) { $entry->historyAdd($res); @history = $entry->history; } =head1 BUGS/TODO - C-s/C-r do not work as nice as in gnu readline - use -browsecmd from Tk::BrowseEntry - use Tie::Array if present =head1 AUTHOR Slaven Rezic =head1 CREDITS Thanks for Jason Smith and Benny Khoo for their suggestions. The auto-completion code is stolen from Tk::IntEntry by Dave Collins . =head1 COPYRIGHT Copyright (c) 1997, 2000, 2001, 2003, 2008, 2016, 2017 Slaven Rezic. All rights reserved. This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut Tk-HistEntry-0.46/META.json0000644000175000017500000000211515040140713014723 0ustar eserteeserte{ "abstract" : "Entry widget with history capability", "author" : [ "Slaven Rezic " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.64, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Tk-HistEntry", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Test::More" : "0", "Tk" : "0" } } }, "release_status" : "stable", "resources" : { "repository" : { "type" : "git", "url" : "git://github.com/eserte/tk-histentry.git" } }, "version" : "0.46", "x_serialization_backend" : "JSON::PP version 4.16" } Tk-HistEntry-0.46/examples/0000755000175000017500000000000015040140713015121 5ustar eserteeserteTk-HistEntry-0.46/examples/1.pl0000644000175000017500000000564513015400533015627 0ustar eserteeserte#!/usr/local/bin/perl -w # -*- perl -*- # # $Id: 1.pl,v 1.8 1998/08/28 00:41:44 eserte Exp $ # Author: Slaven Rezic # # Copyright (C) 1997,1998 Slaven Rezic. All rights reserved. # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # # Mail: eserte@cs.tu-berlin.de # WWW: http://user.cs.tu-berlin.de/~eserte/ # use Tk; use Tk::HistEntry; use strict; my $top = new MainWindow; my($foo, $bla, $blubber); my $f = $top->Frame->grid(-row => 0, -column => 0, -sticky => 'n'); my $lb = $top->Scrolled('Listbox', -scrollbars => 'osoe' )->grid(-row => 0, -column => 1); $f->Label(-text => 'HistEntry')->pack; my $b = $f->HistEntry(-textvariable => \$foo)->pack; my $bb = $f->Button(-text => 'Add current', -command => sub { return unless $foo; $b->historyAdd($foo); $lb->delete(0, 'end'); foreach ($b->history) { $lb->insert('end', $_); } $lb->see('end'); $foo = ''; })->pack; $f->Button(-text => 'Replace history', -command => sub { $b->history([keys %ENV]); } )->pack; $b->bind('' => sub { $bb->invoke }); my $f2 = $top->Frame->grid(-row => 1, -column => 0, -sticky => 'n'); my $lb2 = $top->Scrolled('Listbox', -scrollbars => 'osoe' )->grid(-row => 1, -column => 1); $f2->Label(-text => 'HistEntry with invoke, limit ...')->pack; my $b2; $b2 = $f2->HistEntry(-textvariable => \$bla, -match => 1, -label => 'Test label', -labelPack => [-side => 'left'], -width => 10, -bell => 0, -dup => 0, -limit => 6, -command => sub { my $w = shift; # automatic historyAdd $lb2->delete(0, 'end'); foreach ($b2->history) { $lb2->insert('end', $_); } $lb2->see('end'); })->pack; #XXX$b2->configure(-match => 1);#XXX $f2->Button(-text => 'Add current', -command => sub { $b2->invoke })->pack; my $f3 = $top->Frame->grid(-row => 2, -column => 0, -sticky => 'n'); my $lb3 = $top->Scrolled('Listbox', -scrollbars => 'osoe' )->grid(-row => 2, -column => 1); $f3->Label(-text => 'SimpleHistEntry')->pack; my $b3 = $f3->SimpleHistEntry(-textvariable => \$blubber, -command => sub { my($w, $line, $added) = @_; if ($added) { $lb3->insert('end', $line); $lb3->see('end'); } $blubber = ''; })->pack; $f3->Button(-text => 'Add current', -command => sub { $b3->invoke })->pack; # # Autodestroy # my $seconds = 60; # my $autodestroy_text = "Autodestroy in " . $seconds . "s\n"; # $top->Label(-textvariable => \$autodestroy_text, # )->grid(-row => 99, -column => 0, -columnspan => 2); # $top->repeat(1000, sub { if ($seconds <= 0) { $top->destroy } # $seconds--; # $autodestroy_text = "Autodestroy in " . $seconds # . "s\n"; # }); $top->Button(-text => 'Exit', -command => sub { $top->destroy }, )->grid(-row => 99, -column => 0, -columnspan => 2); MainLoop; Tk-HistEntry-0.46/examples/newclass.pl0000644000175000017500000000477713015400533017313 0ustar eserteeserte#!/usr/local/bin/perl -w # -*- perl -*- # # $Id: newclass.pl,v 1.2 1998/05/20 08:38:12 eserte Exp $ # Author: Slaven Rezic # # Copyright (C) 1997,1998 Slaven Rezic. All rights reserved. # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # # Mail: eserte@cs.tu-berlin.de # WWW: http://user.cs.tu-berlin.de/~eserte/ # use Tk; use Tk::HistEntry; use Tk::FireButton; use strict; package MyHistEntry; @MyHistEntry::ISA = qw(Tk::Frame); Construct Tk::Widget 'MyHistEntry'; { my $foo = $Tk::FireButton::INCBITMAP; $foo = $Tk::FireButton::DECBITMAP; } sub Populate { my($f, $args) = @_; my $e = $f->Component(SimpleHistEntry => 'entry'); my $binc = $f->Component( FireButton => 'inc', -bitmap => $Tk::FireButton::INCBITMAP, -command => sub { $e->historyUp }, ); my $bdec = $f->Component( FireButton => 'dec', -bitmap => $Tk::FireButton::DECBITMAP, -command => sub { $e->historyDown }, ); $f->gridColumnconfigure(0, -weight => 1); $f->gridColumnconfigure(1, -weight => 0); $f->gridRowconfigure(0, -weight => 1); $f->gridRowconfigure(1, -weight => 1); $binc->grid(-row => 0, -column => 1, -sticky => 'news'); $bdec->grid(-row => 1, -column => 1, -sticky => 'news'); $e->grid(-row => 0, -column => 0, -rowspan => 2, -sticky => 'news'); $f->ConfigSpecs (-repeatinterval => ['CHILDREN', "repeatInterval", "RepeatInterval", 100 ], -repeatdelay => ['CHILDREN', "repeatDelay", "RepeatDeleay", 300 ], DEFAULT => [$e], ); $f->Delegates(DEFAULT => $e); $f; } package main; my $top = new MainWindow; my($bla); my($b2, $lb2); $b2 = $top->MyHistEntry(-textvariable => \$bla, -repeatinterval => 30, -bell => 1, -dup => 1, -command => sub { my($w, $s, $added) = @_; if ($added) { $lb2->insert('end', $s); $lb2->see('end'); } $bla = ''; })->pack; $lb2 = $top->Scrolled('Listbox', -scrollbars => 'osoe' )->pack; # # Autodestroy # my $seconds = 60; # my $autodestroy_text = "Autodestroy in " . $seconds . "s\n"; # $top->Label(-textvariable => \$autodestroy_text, # )->pack; # $top->repeat(1000, sub { if ($seconds <= 0) { $top->destroy } # $seconds--; # $autodestroy_text = "Autodestroy in " . $seconds # . "s\n"; # }); $top->Button(-text => 'Exit', -command => sub { $top->destroy }, )->pack; MainLoop; Tk-HistEntry-0.46/META.yml0000644000175000017500000000117615040140713014561 0ustar eserteeserte--- abstract: 'Entry widget with history capability' author: - 'Slaven Rezic ' build_requires: ExtUtils::MakeMaker: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.64, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Tk-HistEntry no_index: directory: - t - inc requires: Test::More: '0' Tk: '0' resources: repository: git://github.com/eserte/tk-histentry.git version: '0.46' x_serialization_backend: 'CPAN::Meta::YAML version 0.020' Tk-HistEntry-0.46/MANIFEST0000644000175000017500000000045015040140714014434 0ustar eserteeserteChanges HistEntry.pm MANIFEST Makefile.PL README examples/1.pl examples/newclass.pl t/basic.t t/newclass.t t/match.t t/invoke.t META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Tk-HistEntry-0.46/README0000644000175000017500000000153013015400533014161 0ustar eserteeserteTk::HistEntry Module Tk::HistEntry implements an entry widget with history. You may use the up and down keys to select older entries (or use the associated listbox). Usage: $top->HistEntry(-textvariable => \$foo, -command => sub { # automatically adds $foo to history print STDERR "Do something with $foo\n"; })->pack; The widget comes in two flavors: the "HistEntry" widget is an inherited BrowseEntry widget where you see history in the associated listbox, too. The "SimpleHistEntry" widget is a plain Entry widget. To install, type $ perl Makefile.PL $ make $ make install Testing is done with $ make test There's also a demo which can be started with $ make demo Send bug reports, comments and suggestions to Slaven Rezic . Tk-HistEntry-0.46/Changes0000644000175000017500000000361615040135271014607 0ustar eserteeserteRevision history for Perl extension Tk::HistEntry. 0.46 2025-07-23 - added LICENSE to META file (by manwar) - mainly CI related stuff (GitHub + appveyor workflows) 0.45 2017-12-31 - stable release with all changes in 0.44_50 0.44_50 2017-01-01 - converted t/newclass.t to Test::More 0.44 2016-11-24 - stable release with all changes in 0.43_50..0.43_51 0.43_51 2016-11-23 - tests do not hang anymore on twm - converted one test file to Test::More 0.43_50 2008-09-23 - more robust test against missing DISPLAYs 0.43 2007-02-02 - no code changes, just fixed a possibly failing test (depending on setting of window focus) 0.42 - fixed a failing test (it was a delegation problem) 0.41 - just a Pod example fix 0.40 - ->class() now returns more meaningful names: SimpleHistEntry and HistEntry - do not do auto-completion if typing _before_ the cursor - new -history option (like history method, with tests) 0.37 - fixed bug in historyAdd of Browse version of HistEntry 0.36 - new -case option - fixed Control- handling when -match is on 0.35 - insert, delete and get is now delegated to the Entry subwidget 0.34 - fixed test scripts - another example in the pod 0.33 - bug fix: Home, End and Delete did not work in -match mode 0.32 - new methods historySave and historyMergeFromFile 0.31 - bugfix solving disappearing leading characters 0.30 - using auto-completion (code stolen from Dave Collins), if option -match is set 0.22 - bug fix in t/newclass.t 0.21 - bug fixes for Tk::HistEntry::Browse - new bindings M-< and M-> 0.20 - split Tk::HistEntry into Tk::HistEntry::Simple and Tk::HistEntry::Browse - new options: -dup, -limit, -bell, -command - new methods: invoke, history - some fixes - POD 0.10 Mon Dec 8 03:07:00 1997 - using Tk::BrowseEntry as base class instead of Tk::Entry 0.01 Sat Dec 6 17:43:45 1997 - original version Tk-HistEntry-0.46/Makefile.PL0000755000175000017500000000250715040137766015302 0ustar eserteeserte# -*- perl -*- use ExtUtils::MakeMaker; my $is_devel_host = defined $ENV{USER} && $ENV{USER} eq 'eserte' && ($^O =~ /bsd/i || $ENV{PERL_RELEASE_READY}) && -f "../../perl.release.mk"; my $eumm_recent_enough = $ExtUtils::MakeMaker::VERSION >= 6.54; if (!$eumm_recent_enough) { *MY::dist_core = sub { <<'EOF'; dist : $(NOECHO) $(ECHO) "Sorry, use a newer EUMM!" EOF }; } WriteMakefile( 'NAME' => 'Tk::HistEntry', 'VERSION_FROM' => 'HistEntry.pm', # finds $VERSION 'LICENSE' => 'perl_5', 'dist' => {'POSTOP'=>'-$(CHMOD) 644 $(DISTVNAME).tar$(SUFFIX)'}, 'PREREQ_PM' => { 'Tk' => 0, 'Test::More' => 0, }, ($] >= 5.005 ? ( ABSTRACT_FROM => 'HistEntry.pm', AUTHOR => 'Slaven Rezic ', ) : () ), ($eumm_recent_enough ? (META_ADD => { resources => { repository => 'git://github.com/eserte/tk-histentry.git' }, }) : ()), ); sub MY::postamble { my $postamble = <<'EOF'; demo :: pure_all $(FULLPERL) -w -I$(SITELIBEXP)/Tk/demos/widget_lib -Mblib examples/1.pl $(FULLPERL) -w -I$(SITELIBEXP)/Tk/demos/widget_lib -Mblib examples/newclass.pl EOF if ($is_devel_host) { $postamble .= <<'EOF'; PERL_TEST_DISTRIBUTION_CHANGES=yes .include "../../perl.release.mk" .include "../../perl.git.mk" EOF } $postamble; } Tk-HistEntry-0.46/t/0000755000175000017500000000000015040140713013546 5ustar eserteeserteTk-HistEntry-0.46/t/newclass.t0000644000175000017500000000751014371264757015602 0ustar eserteeserte# -*- perl -*- # # Author: Slaven Rezic # # Copyright (C) 1997,1998,2008,2016,2023 Slaven Rezic. All rights reserved. # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # use strict; use warnings; use Test::More; use Tk; use Tk::HistEntry; my $top = eval { new MainWindow }; if (!$top) { plan skip_all => "cannot open DISPLAY: $@"; } $top->geometry('+10+10'); if (!eval { require Tk::FireButton; 1 }) { plan skip_all => "Tk::FireButton missing"; } if (!eval { $top->event('generate', ''); die "event generate is working different on Win32" if $^O eq 'MSWin32'; 1; }) { plan skip_all => "Cannot generate Button-1 event, required for tests"; } plan tests => 14; { package MyHistEntry; @MyHistEntry::ISA = qw(Tk::Frame); Construct Tk::Widget 'MyHistEntry'; sub Populate { my($f, $args) = @_; no warnings 'once'; my $e = $f->Component(SimpleHistEntry => 'entry'); my $binc = $f->Component( FireButton => 'inc', -bitmap => $Tk::FireButton::INCBITMAP, -command => sub { $e->historyUp }, ); my $bdec = $f->Component( FireButton => 'dec', -bitmap => $Tk::FireButton::DECBITMAP, -command => sub { $e->historyDown }, ); $f->gridColumnconfigure(0, -weight => 1); $f->gridColumnconfigure(1, -weight => 0); $f->gridRowconfigure(0, -weight => 1); $f->gridRowconfigure(1, -weight => 1); $binc->grid(-row => 0, -column => 1, -sticky => 'news'); $bdec->grid(-row => 1, -column => 1, -sticky => 'news'); $e->grid(-row => 0, -column => 0, -rowspan => 2, -sticky => 'news'); $f->ConfigSpecs (-repeatinterval => ['CHILDREN', "repeatInterval", "RepeatInterval", 100 ], -repeatdelay => ['CHILDREN', "repeatDelay", "RepeatDeleay", 300 ], DEFAULT => [$e], ); $f->Delegates(DEFAULT => $e); $f; } } $top->geometry($top->screenwidth . "x" .$top->screenheight . "+0+0"); my($b2, $lb2); { my $bla; $b2 = $top->MyHistEntry(-textvariable => \$bla, -repeatinterval => 30, -bell => 1, -dup => 1, -command => sub { my($w, $s, $added) = @_; if ($added) { $lb2->insert('end', $s); $lb2->see('end'); } $bla = ''; })->pack; ok $b2; $b2->update; pass "after calling update"; } $lb2 = $top->Scrolled('Listbox', -scrollbars => 'osoe')->pack; my $e = $b2->Subwidget('entry'); my $inc = $b2->Subwidget('inc'); my $dec = $b2->Subwidget('dec'); $e->focus; $e->insert("end", 'first'); $e->event('generate', "", -keysym => 'Return'); is_deeply [$b2->history], ['first']; $e->event('generate', "", -keysym => 'Up'); is $e->get, 'first'; $e->event('generate', "", -keysym => 'Down'); is $e->get, ''; $e->insert(0, 'second'); $e->event('generate', "", -keysym => 'Return'); is_deeply [$e->history], ['first', 'second']; $inc->invoke; $inc->invoke; is $e->get, 'first'; $dec->invoke; is $e->get, 'second'; # The next two tests are disabled, because they fail on systems without # configured Alt key. if (0) { $e->focus; $e->event('generate', "", -state => 8, -keysym => 'less'); is $e->get, 'first'; $e->event('generate', "", -state => 8, -keysym => 'greater'); is $e->get, 'second'; } $e->historyAdd("third"); { my @h = $e->history; is @h, 3; is $h[2], 'third'; } $e->invoke("fourth"); { my @h = $lb2->get(0, 'end'); # only three elements (because of use of historyAdd) is @h, 3; is $h[2], 'fourth'; } $e->delete(0, 'end'); $e->insert(0, 'bla'); $e->historyAdd; { my @h = $e->history; is @h, 5; is $h[4], 'bla'; } if ($ENV{PERL_TEST_INTERACTIVE}) { my $cb = $top->Button(-text => "Ok", -command => sub { $top->destroy })->pack; $cb->focus; $top->after(30000, sub { $top->destroy }); MainLoop; } Tk-HistEntry-0.46/t/basic.t0000644000175000017500000001016713015405334015024 0ustar eserteeserte# -*- perl -*- # # Author: Slaven Rezic # # Copyright (C) 1997,1998,2008,2016 Slaven Rezic. All rights reserved. # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # # Mail: eserte@cs.tu-berlin.de # WWW: http://user.cs.tu-berlin.de/~eserte/ # use strict; use warnings; use File::Temp qw(tempfile); use Test::More; use Tk; my $top = eval { new MainWindow }; if (!$top) { plan skip_all => "cannot open DISPLAY: $@"; } $top->geometry('+10+10'); plan tests => 50; use Tk::HistEntry; my($he1, $e1); { $he1 = $top->SimpleHistEntry(-textvariable => \my $foo, -bell => 1, -dup => 0, -case => 1, -auto => 1, -match => 1, )->pack; ok Tk::Exists($he1); is $he1->class, 'SimpleHistEntry'; $he1->update; pass 'ok after update'; $e1 = $he1->_entry; ok $e1; } my($he2, $e2, $lb2); { $he2 = $top->HistEntry(-textvariable => \my $bla, -bell => 1, -dup => 0, -label => 'Browse:', -labelPack => [-side => 'top'], )->pack; ok Tk::Exists($he2); is $he2->class, 'HistEntry'; $he2->update; pass 'ok after update'; SEARCH: for my $sw ($he2->Subwidget) { if ($sw->isa('Tk::LabEntry')) { for my $ssw ($sw->Subwidget) { if ($ssw->isa('Tk::Label')) { my $t = $ssw->cget(-text); is $t, 'Browse:'; last SEARCH; } } } } $e2 = $he2->_entry; ok $e2; $lb2 = $he2->_listbox; ok $lb2; } for my $def ( [$e1, $he1, 1], [$e2, $he2, 2], ) { my($e, $he, $nr) = @$def; $e->insert(0, "first $nr"); $he->historyAdd; is_deeply [$he->history], ["first $nr"]; $he->historyAdd("second $nr"); { my @h = $he->history; is $h[1], "second $nr"; is @h, 2; } $he->addhistory("third $nr"); my @h = $he->history; is $h[2], "third $nr"; is @h, 3; if ($he eq $he2) { is_deeply [$lb2->get(0, 'end')], \@h; } ok $he->can('addhistory'); ok $he->can('historyAdd'); } my %histfiles; my %oldhist; for my $widget (qw(HistEntry SimpleHistEntry)) { my @test_values = qw(bla foo bar); my($histfh,$histfile) = tempfile("hist.save.XXXXXXXX", UNLINK => 1); my $he = $top->$widget->pack; for (@test_values) { $he->historyAdd($_) } is_deeply [$he->history], \@test_values; $he->_entry->insert('end', 'blubber'); $he->addhistory(); is_deeply [$he->history], [@test_values, 'blubber']; $he->OnDestroy(sub { $he->historySave($histfile) }); $histfiles{$widget} = $histfile; $oldhist{$widget} = [$he->history]; $he->destroy; } for my $widget (qw(HistEntry SimpleHistEntry)) { my $he = $top->$widget; $he->historyMergeFromFile($histfiles{$widget}); is_deeply [$he->history], $oldhist{$widget}, "historyMergeFromFile for $widget works"; $he->historyReset; is_deeply [$he->history], [], "historyReset for $widget works"; if ($widget eq 'HistEntry') { is_deeply [$he->_listbox->get(0, "end")], []; } $he->insert('end', 'blablubber'); is $he->get, 'blablubber'; $he->delete(0, 'end'); is $he->get, ''; } # check duplicates for my $he ($he1, $he2) { my $hist_entries = 4; $he->historyAdd("foobar"); is scalar $he->history, $hist_entries; $he->historyAdd("foobar"); is scalar $he->history, $hist_entries; $hist_entries++; $he->historyAdd("foobar2"); is scalar $he->history, $hist_entries; $he->_entry->delete(0, "end"); $he->_entry->insert(0, "foobar"); $he->historyAdd; is scalar $he->history, $hist_entries; } { my $he = $top->SimpleHistEntry(-history => [qw(1 2 3)]); is_deeply [$he->cget(-history)], [qw(1 2 3)], 'check -history option with SimpleHistEntry'; is_deeply [$he->history], [qw(1 2 3)]; } { my $he = $top->HistEntry(-history => [qw(1 2 3)]); is_deeply [$he->cget(-history)], [qw(1 2 3)], 'check -history option with HistEntry'; is_deeply [$he->history], [qw(1 2 3)]; } if ($ENV{PERL_TEST_INTERACTIVE}) { $top->Button( -text => "OK", -command => sub { $top->destroy }, )->pack->focus; $top->after(60*1000, sub { $top->destroy }); MainLoop; } Tk-HistEntry-0.46/t/match.t0000644000175000017500000000306013015405500015024 0ustar eserteeserte# -*- perl -*- # # Author: Slaven Rezic # # Copyright (C) 1997,1998,2007,2008,2016 Slaven Rezic. All rights reserved. # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # # Mail: eserte@cs.tu-berlin.de # WWW: http://user.cs.tu-berlin.de/~eserte/ # BEGIN { if (!eval q{ use Test::More; 1; }) { print "1..0 # skip: no Test::More module\n"; exit; } } use Tk; my $top; BEGIN { if (!eval { $top = new MainWindow }) { print "1..0 # skip cannot open DISPLAY\n"; CORE::exit; } $top->geometry('+10+10'); } use Tk::HistEntry; use strict; plan tests => 4; $top->geometry($top->screenwidth . "x" .$top->screenheight . "+0+0"); my $he = $top->HistEntry(-match => 1, )->pack; isa_ok($he, "Tk::HistEntry"); $he->addhistory('Foo'); $he->addhistory('Bar'); my $e = $he->_entry; isa_ok($e, "Tk::LabEntry"); my $focus_e = $e->Subwidget("entry"); $e->focus; $e->update; eval { $e->event('generate', '', -keysym => 'F'); $e->event('generate', '', -keysym => 'o'); $e->update; }; SKIP: { skip("Focus lost? $@", 1) if $@; skip("Focus lost!", 1) if ($top->focusCurrent||"") ne $focus_e; is($e->get, 'Foo', "Expected first entry"); } { local $TODO = "Rethink BackSpace behavior..."; eval { $e->event('generate', '', -keysym => 'BackSpace'); $e->update; }; SKIP: { skip("Focus lost? $@", 1) if $@; skip("Focus lost!", 1) if ($top->focusCurrent||"") ne $focus_e; is($e->get, 'F', 'Only one character entered'); } } #MainLoop; Tk-HistEntry-0.46/t/invoke.t0000644000175000017500000000202313015405454015231 0ustar eserteeserte# -*- perl -*- # # Author: Slaven Rezic # # Copyright (C) 1997,1998,2008,2016 Slaven Rezic. All rights reserved. # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # # Mail: eserte@cs.tu-berlin.de # WWW: http://user.cs.tu-berlin.de/~eserte/ # use Tk; my $top; BEGIN { if (!eval { $top = new MainWindow }) { print "1..0 # skip cannot open DISPLAY\n"; CORE::exit; } $top->geometry('+10+10'); # for twm } BEGIN { $^W = 1; $| = 1; $loaded = 0; $last = 3; print "1..$last\n"; } END {print "not ok 1\n" unless $loaded;} use Tk::HistEntry; use strict; use vars qw($loaded $last); package main; $loaded = 1; my $ok = 1; print "ok " . $ok++ . "\n"; use Tk; my $he = $top->HistEntry(-command => sub { }, -limit => 1)->pack; $he->invoke("aaa"); my(@h) = $he->history; if (join(",", @h) ne "aaa") { print "not "; } print "ok " . $ok++ . "\n"; $he->invoke("bbb"); @h = $he->history; if (join(",", @h) ne "bbb") { print "not "; } print "ok " . $ok++ . "\n";