This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
untodo the no-longer-failing todo test for rgs' patch
[perl5.git] / lib / complete.pl
CommitLineData
a687059c 1;#
a6d71656
GS
2#
3# This library is no longer being maintained, and is included for backward
4# compatibility with Perl 4 programs which may require it.
1aaec297
S
5# This legacy library is deprecated and will be removed in a future
6# release of perl.
a6d71656
GS
7#
8# In particular, this should not be used as an example of modern Perl
9# programming techniques.
10#
11# Suggested alternative: Term::Complete
1aaec297
S
12
13warn( "The 'complete.pl' legacy library is deprecated and will be"
14 . " removed in the next major release of perl. Please use the"
15 . " Term::Complete module instead." );
16
55204971 17;# @(#)complete.pl,v1.1 (me@anywhere.EBay.Sun.COM) 09/23/91
a687059c
LW
18;#
19;# Author: Wayne Thompson
20;#
21;# Description:
22;# This routine provides word completion.
23;# (TAB) attempts word completion.
24;# (^D) prints completion list.
55204971 25;# (These may be changed by setting $Complete'complete, etc.)
a687059c
LW
26;#
27;# Diagnostics:
28;# Bell when word completion fails.
29;#
30;# Dependencies:
31;# The tty driver is put into raw mode.
32;#
33;# Bugs:
a687059c
LW
34;#
35;# Usage:
55204971
LW
36;# $input = &Complete('prompt_string', *completion_list);
37;# or
38;# $input = &Complete('prompt_string', @completion_list);
a687059c
LW
39;#
40
7e1cf235
LW
41CONFIG: {
42 package Complete;
43
55204971
LW
44 $complete = "\004";
45 $kill = "\025";
46 $erase1 = "\177";
47 $erase2 = "\010";
7e1cf235
LW
48}
49
a687059c 50sub Complete {
7e1cf235
LW
51 package Complete;
52
55497cff 53 local($prompt, @cmp_list, $return, @match, $l, $test, $cmp, $r);
55204971
LW
54 if ($_[1] =~ /^StB\0/) {
55 ($prompt, *_) = @_;
a687059c 56 }
55204971
LW
57 else {
58 $prompt = shift(@_);
59 }
60 @cmp_lst = sort(@_);
61
62 system('stty raw -echo');
63 LOOP: {
64 print($prompt, $return);
65 while (($_ = getc(STDIN)) ne "\r") {
66 CASE: {
67 # (TAB) attempt completion
68 $_ eq "\t" && do {
69 @match = grep(/^$return/, @cmp_lst);
70 $l = length($test = shift(@match));
71 unless ($#match < 0) {
72 foreach $cmp (@match) {
73 until (substr($cmp, 0, $l) eq substr($test, 0, $l)) {
74 $l--;
75 }
76 }
77 print("\a");
78 }
79 print($test = substr($test, $r, $l - $r));
80 $r = length($return .= $test);
81 last CASE;
82 };
83
84 # (^D) completion list
85 $_ eq $complete && do {
86 print(join("\r\n", '', grep(/^$return/, @cmp_lst)), "\r\n");
87 redo LOOP;
88 };
89
90 # (^U) kill
91 $_ eq $kill && do {
92 if ($r) {
40da2db3
JH
93 undef $r;
94 undef $return;
55204971
LW
95 print("\r\n");
96 redo LOOP;
97 }
98 last CASE;
99 };
100
101 # (DEL) || (BS) erase
102 ($_ eq $erase1 || $_ eq $erase2) && do {
103 if($r) {
104 print("\b \b");
105 chop($return);
106 $r--;
107 }
108 last CASE;
109 };
110
111 # printable char
112 ord >= 32 && do {
113 $return .= $_;
114 $r++;
115 print;
116 last CASE;
117 };
118 }
119 }
120 }
121 system('stty -raw echo');
122 print("\n");
a687059c
LW
123 $return;
124}
125
1261;