1 package Term::Complete;
6 our @ISA = qw(Exporter);
7 our @EXPORT = qw(Complete);
8 our $VERSION = '1.402';
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>.
29 The following command characters are defined:
35 Attempts word completion.
40 Prints completion list.
41 Defined by I<$Term::Complete::complete>.
45 Erases the current input.
46 Defined by I<$Term::Complete::kill>.
48 =item E<lt>delE<gt>, E<lt>bsE<gt>
51 Defined by I<$Term::Complete::erase1> and I<$Term::Complete::erase2>.
57 Bell sounds when word completion fails.
61 The completion character E<lt>tabE<gt> cannot be changed.
69 our($complete, $kill, $erase1, $erase2, $tty_raw_noecho, $tty_restore, $stty, $tty_safe_restore);
70 our($tty_saved_state) = '';
76 foreach my $s (qw(/bin/stty /usr/bin/stty)) {
78 $tty_raw_noecho = "$s raw -echo";
79 $tty_restore = "$s -raw echo";
80 $tty_safe_restore = $tty_restore;
88 my($prompt, @cmp_lst, $cmp, $test, $l, @match);
89 my ($return, $r) = ("", 0);
95 if (ref $_[0] || $_[0] =~ /^\*/) {
96 @cmp_lst = sort @{$_[0]};
102 # Attempt to save the current stty state, to be restored later
103 if (defined $stty && defined $tty_saved_state && $tty_saved_state eq '') {
104 $tty_saved_state = qx($stty -g 2>/dev/null);
106 # stty -g not supported
107 $tty_saved_state = undef;
110 $tty_saved_state =~ s/\s+$//g;
111 $tty_restore = qq($stty "$tty_saved_state" 2>/dev/null);
114 system $tty_raw_noecho if defined $tty_raw_noecho;
117 print($prompt, $return);
118 while (($_ = getc(STDIN)) ne "\r") {
120 # (TAB) attempt completion
122 @match = grep(/^\Q$return/, @cmp_lst);
123 unless ($#match < 0) {
124 $l = length($test = shift(@match));
125 foreach $cmp (@match) {
126 until (substr($cmp, 0, $l) eq substr($test, 0, $l)) {
131 print($test = substr($test, $r, $l - $r));
132 $r = length($return .= $test);
137 # (^D) completion list
138 $_ eq $complete && do {
139 print(join("\r\n", '', grep(/^\Q$return/, @cmp_lst)), "\r\n");
154 # (DEL) || (BS) erase
155 ($_ eq $erase1 || $_ eq $erase2) && do {
175 # system $tty_restore if defined $tty_restore;
176 if (defined $tty_saved_state && defined $tty_restore && defined $tty_safe_restore)
180 # tty_restore caused error
181 system $tty_safe_restore;