X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FTerm%2FReadLine.pm;h=8c58694fd8a0e2a6f2599ff231126ab36bb79df6;hb=045ac3170cd33a7002e5d26e4859e94ca400926d;hp=a9df784d79d8698ce74f37f4f32939953159bae8;hpb=f36776d9df3fd477364b55a7c4728f0820f06f99;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/Term/ReadLine.pm b/lib/Term/ReadLine.pm index a9df784..8c58694 100644 --- a/lib/Term/ReadLine.pm +++ b/lib/Term/ReadLine.pm @@ -6,11 +6,11 @@ no real package is found, substitutes stubs instead of basic functions. =head1 SYNOPSIS use Term::ReadLine; - $term = new Term::ReadLine 'Simple Perl calc'; - $prompt = "Enter your arithmetic expression: "; - $OUT = $term->OUT || STDOUT; + my $term = new Term::ReadLine 'Simple Perl calc'; + my $prompt = "Enter your arithmetic expression: "; + my $OUT = $term->OUT || \*STDOUT; while ( defined ($_ = $term->readline($prompt)) ) { - $res = eval($_), "\n"; + my $res = eval($_), "\n"; warn $@ if $@; print $OUT $res, "\n" unless $@; $term->addhistory($_) if /\S/; @@ -41,7 +41,7 @@ where $term is a return value of Term::ReadLine-EInit. returns the actual package that executes the commands. Among possible values are C, C, -C. +C. =item C @@ -139,31 +139,63 @@ None =head1 ENVIRONMENT -The variable C governs which ReadLine clone is loaded. If the -value is false, a dummy interface is used. If the value is true, it -should be tail of the name of the package to use, such as C or -C. +The environment variable C governs which ReadLine clone is +loaded. If the value is false, a dummy interface is used. If the value +is true, it should be tail of the name of the package to use, such as +C or C. -If the variable is not set, the best available package is loaded. +As a special case, if the value of this variable is space-separated, +the tail might be used to disable the ornaments by setting the tail to +be C or C. The head should be as described above, say + +If the variable is not set, or if the head of space-separated list is +empty, the best available package is loaded. + + export "PERL_RL=Perl o=0" # Use Perl ReadLine without ornaments + export "PERL_RL= o=0" # Use best available ReadLine without ornaments + +(Note that processing of C for ornaments is in the discretion of the +particular used C package). + +=head1 CAVEATS + +It seems that using Term::ReadLine from Emacs minibuffer doesn't work +quite right and one will get an error message like + + Cannot open /dev/tty for read at ... + +One possible workaround for this is to explicitly open /dev/tty like this + + open (FH, "/dev/tty" ) + or eval 'sub Term::ReadLine::findConsole { ("&STDIN", "&STDERR") }'; + die $@ if $@; + close (FH); + +or you can try using the 4-argument form of Term::ReadLine->new(). =cut +use strict; + package Term::ReadLine::Stub; -@ISA = qw'Term::ReadLine::Tk Term::ReadLine::TermCap'; +our @ISA = qw'Term::ReadLine::Tk Term::ReadLine::TermCap'; $DB::emacs = $DB::emacs; # To peacify -w +our @rl_term_set; *rl_term_set = \@Term::ReadLine::TermCap::rl_term_set; sub ReadLine {'Term::ReadLine::Stub'} sub readline { my $self = shift; my ($in,$out,$str) = @$self; - print $out $rl_term_set[0], shift, $rl_term_set[1], $rl_term_set[2]; + my $prompt = shift; + print $out $rl_term_set[0], $prompt, $rl_term_set[1], $rl_term_set[2]; $self->register_Tk if not $Term::ReadLine::registered and $Term::ReadLine::toloop and defined &Tk::DoOneEvent; #$str = scalar <$in>; $str = $self->get_line; + $str =~ s/^\s*\Q$prompt\E// if ($^O eq 'MacOS'); print $out $rl_term_set[3]; # bug in 5.000: chomping empty string creats length -1: chomp $str if defined $str; @@ -174,7 +206,9 @@ sub addhistory {} sub findConsole { my $console; - if (-e "/dev/tty") { + if ($^O eq 'MacOS') { + $console = "Dev:Console"; + } elsif (-e "/dev/tty") { $console = "/dev/tty"; } elsif (-e "con" or $^O eq 'MSWin32') { $console = "con"; @@ -182,7 +216,7 @@ sub findConsole { $console = "sys\$command"; } - if ($^O eq 'amigaos') { + if (($^O eq 'amigaos') || ($^O eq 'beos') || ($^O eq 'epoc')) { $console = undef; } elsif ($^O eq 'os2') { @@ -193,7 +227,7 @@ sub findConsole { } } - $consoleOUT = $console; + my $consoleOUT = $console; $console = "&STDIN" unless defined $console; if (!defined $consoleOUT) { $consoleOUT = defined fileno(STDERR) ? "&STDERR" : "&STDOUT"; @@ -205,25 +239,31 @@ sub new { die "method new called with wrong number of arguments" unless @_==2 or @_==4; #local (*FIN, *FOUT); - my ($FIN, $FOUT); + my ($FIN, $FOUT, $ret); if (@_==2) { - ($console, $consoleOUT) = findConsole; + my($console, $consoleOUT) = $_[0]->findConsole; open(FIN, "<$console"); open(FOUT,">$consoleOUT"); #OUT->autoflush(1); # Conflicts with debugger? - $sel = select(FOUT); + my $sel = select(FOUT); $| = 1; # for DB::OUT select($sel); - bless [\*FIN, \*FOUT]; + $ret = bless [\*FIN, \*FOUT]; } else { # Filehandles supplied $FIN = $_[2]; $FOUT = $_[3]; #OUT->autoflush(1); # Conflicts with debugger? - $sel = select($FOUT); + my $sel = select($FOUT); $| = 1; # for DB::OUT select($sel); - bless [$FIN, $FOUT]; + $ret = bless [$FIN, $FOUT]; + } + if ($ret->Features->{ornaments} + and not ($ENV{PERL_RL} and $ENV{PERL_RL} =~ /\bo\w*=0/)) { + local $Term::ReadLine::termcap_nowarn = 1; + $ret->ornaments(1); } + return $ret; } sub newTTY { @@ -240,12 +280,14 @@ sub OUT { shift->[1] } sub MinLine { undef } sub Attribs { {} } -my %features = (tkRunning => 1, ornaments => 1, newTTY => 1); +my %features = (tkRunning => 1, ornaments => 1, 'newTTY' => 1); sub Features { \%features } package Term::ReadLine; # So late to allow the above code be defined? -my $which = $ENV{PERL_RL}; +our $VERSION = '1.00'; + +my ($which) = exists $ENV{PERL_RL} ? split /\s+/, $ENV{PERL_RL} : undef; if ($which) { if ($which =~ /\bgnu\b/i){ eval "use Term::ReadLine::Gnu;"; @@ -254,7 +296,7 @@ if ($which) { } else { eval "use Term::ReadLine::$which;"; } -} elsif (defined $which) { # Defined but false +} elsif (defined $which and $which ne '') { # Defined but false # Do nothing fancy } else { eval "use Term::ReadLine::Gnu; 1" or eval "use Term::ReadLine::Perl; 1"; @@ -264,11 +306,13 @@ if ($which) { # To make possible switch off RL in debugger: (Not needed, work done # in debugger). - +our @ISA; if (defined &Term::ReadLine::Gnu::readline) { @ISA = qw(Term::ReadLine::Gnu Term::ReadLine::Stub); } elsif (defined &Term::ReadLine::Perl::readline) { @ISA = qw(Term::ReadLine::Perl Term::ReadLine::Stub); +} elsif (defined $which && defined &{"Term::ReadLine::$which\::readline"}) { + @ISA = "Term::ReadLine::$which"; } else { @ISA = qw(Term::ReadLine::Stub); } @@ -277,10 +321,11 @@ package Term::ReadLine::TermCap; # Prompt-start, prompt-end, command-line-start, command-line-end # -- zero-width beautifies to emit around prompt and the command line. -@rl_term_set = ("","","",""); +our @rl_term_set = ("","","",""); # string encoded: -$rl_term_set = ',,,'; +our $rl_term_set = ',,,'; +our $terminal; sub LoadTermCap { return if defined $terminal; @@ -293,10 +338,14 @@ sub ornaments { return $rl_term_set unless @_; $rl_term_set = shift; $rl_term_set ||= ',,,'; - $rl_term_set = 'us,ue,md,me' if $rl_term_set == 1; + $rl_term_set = 'us,ue,md,me' if $rl_term_set eq '1'; my @ts = split /,/, $rl_term_set, 4; eval { LoadTermCap }; - warn("Cannot find termcap: $@\n"), return unless defined $terminal; + unless (defined $terminal) { + warn("Cannot find termcap: $@\n") unless $Term::ReadLine::termcap_nowarn; + $rl_term_set = ',,,'; + return; + } @rl_term_set = map {$_ ? $terminal->Tputs($_,1) || '' : ''} @ts; return $rl_term_set; } @@ -304,8 +353,10 @@ sub ornaments { package Term::ReadLine::Tk; +our($count_handle, $count_DoOne, $count_loop); $count_handle = $count_DoOne = $count_loop = 0; +our($giveup); sub handle {$giveup = 1; $count_handle++} sub Tk_loop { @@ -336,6 +387,7 @@ sub get_line { my $self = shift; $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent; my $in = $self->IN; + local ($/) = "\n"; return scalar <$in>; }