cleanup
[sepia.git] / lib / Sepia / ReadLine.pm
blob4f5fd99e94212c505303dfe674b02f08b8c31caf
1 package Sepia::ReadLine;
2 use Term::ReadLine;
3 use Sepia;
4 require Exporter;
5 @ISA='Exporter';
6 @EXPORT='repl';
8 sub rl_attempted_complete
10 my ($text, $line, $start, $end) = @_;
11 my @xs;
12 if (substr($line, 0, $start) =~ /^\s*$/ && $text =~ /^,(\S*)$/) {
13 my $x = qr/^\Q$1\E/;
14 @xs = map ",$_", grep /$x/, keys %Sepia::REPL;
15 } else {
16 my ($type, $str) = (substr $line, $start && ($start-1), $end) =~ /^([\$\@\%\&]?)(.*)/;
17 my %h = qw(@ ARRAY % HASH & CODE * IO $ VARIABLE);
18 @xs = Sepia::completions $h{$type||'&'}, $str;
20 $TERM->completion_matches($text,
21 sub { $_[1] < @xs ? $xs[$_[1]] : () });
24 sub repl
26 { package main; do $_ for @ARGV }
27 $TERM = new Term::ReadLine;
28 my $attr = $TERM->Attribs;
29 # $attr->{completion_entry_function} = \&rl_complete;
30 $attr->{attempted_completion_function} = \&rl_attempted_complete;
31 $Sepia::READLINE = sub { $TERM->readline(Sepia::prompt()) };
32 goto &Sepia::repl;