1 package Term::Complete;
6 our @ISA = qw(Exporter);
7 our @EXPORT = qw(Complete);
10 # @(#)complete.pl,v1.2 (me@anywhere.EBay.Sun.COM) 09/23/91
14 Term::Complete - Perl word completion module
18 $input = Complete('prompt_string', \@completion_list);
19 $input = Complete('prompt_string', @completion_list);
23 This routine provides word completion on the list of words in
24 the array (or array ref).
26 The tty driver is put into raw mode and restored using an operating
27 system specific command, in UNIX-like environments C<stty raw -echo>
28 and C<stty -raw echo>.
30 The following command characters are defined:
36 Attempts word completion.
41 Prints completion list.
42 Defined by I<$Term::Complete::complete>.
46 Erases the current input.
47 Defined by I<$Term::Complete::kill>.
49 =item E<lt>delE<gt>, E<lt>bsE<gt>
52 Defined by I<$Term::Complete::erase1> and I<$Term::Complete::erase2>.
58 Bell sounds when word completion fails.
62 The completion character E<lt>tabE<gt> cannot be changed.
70 our($complete, $kill, $erase1, $erase2, $tty_raw_noecho, $tty_restore);
76 foreach my $stty (qw(/bin/stty /usr/bin/stty)) {
78 $tty_raw_noecho = "$stty raw -echo";
79 $tty_restore = "$stty -raw echo";
86 my($prompt, @cmp_lst, $cmp, $test, $l, @match);
87 my ($return, $r) = ("", 0);
93 if (ref $_[0] || $_[0] =~ /^\*/) {
94 @cmp_lst = sort @{$_[0]};
100 system $tty_raw_noecho if defined $tty_raw_noecho;
102 print($prompt, $return);
103 while (($_ = getc(STDIN)) ne "\r") {
105 # (TAB) attempt completion
107 @match = grep(/^$return/, @cmp_lst);
108 unless ($#match < 0) {
109 $l = length($test = shift(@match));
110 foreach $cmp (@match) {
111 until (substr($cmp, 0, $l) eq substr($test, 0, $l)) {
116 print($test = substr($test, $r, $l - $r));
117 $r = length($return .= $test);
122 # (^D) completion list
123 $_ eq $complete && do {
124 print(join("\r\n", '', grep(/^$return/, @cmp_lst)), "\r\n");
139 # (DEL) || (BS) erase
140 ($_ eq $erase1 || $_ eq $erase2) && do {
159 system $tty_restore if defined $tty_restore;