make "perl -c script/re.pl" not start up the REPL
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / ReadLineHistory.pm
1 # First cut at using the readline history directly rather than reimplementing
2 # it. It does save history but it's a little crappy; still playing with it ;)
3 #
4 # epitaph, 22nd April 2007
5
6 use strict;
7 use warnings;
8 package Devel::REPL::Plugin::ReadLineHistory;
9
10 use Devel::REPL::Plugin;
11 use File::HomeDir;
12 use File::Spec;
13
14 my $hist_file = $ENV{PERLREPL_HISTFILE} ||
15     File::Spec->catfile(File::HomeDir->my_home, '.perlreplhist');
16
17 # HISTLEN should probably be in a config file to stop people accidentally
18 # truncating their history if they start the program and forget to set
19 # PERLREPL_HISTLEN
20 my $hist_len=$ENV{PERLREPL_HISTLEN} || 100;
21
22 around 'run' => sub {
23    my $orig=shift;
24    my ($self, @args)=@_;
25    if ($self->term->ReadLine eq 'Term::ReadLine::Gnu') {
26       $self->term->stifle_history($hist_len);
27    }
28    if ($self->term->ReadLine eq 'Term::ReadLine::Perl') {
29       $self->term->Attribs->{MaxHistorySize} = $hist_len;
30    }
31    if (-f($hist_file)) {
32       if ($self->term->ReadLine eq 'Term::ReadLine::Gnu') {
33          $self->term->ReadHistory($hist_file);
34       }
35       if ($self->term->ReadLine eq 'Term::ReadLine::Perl') {
36          open HIST, $hist_file or die "ReadLineHistory: could not open $hist_file: $!\n";
37          while (my $line = <HIST>) {
38             chomp $line;
39             $self->term->addhistory($line);
40          }
41          close HIST;
42       }
43    }
44
45    $self->term->Attribs->{do_expand}=1;  # for Term::ReadLine::Gnu
46    $self->term->MinLine(2);              # don't save one letter commands
47
48    # let History plugin know we have Term::ReadLine support
49    $self->have_readline_history(1) if $self->can('have_readline_history');
50
51
52    $self->$orig(@args);
53
54    if ($self->term->ReadLine eq 'Term::ReadLine::Gnu') {
55       $self->term->WriteHistory($hist_file) ||
56       $self->print("warning: failed to write history file $hist_file");
57    }
58    if ($self->term->ReadLine eq 'Term::ReadLine::Perl') {
59       my @lines = $self->term->GetHistory() if $self->term->can('GetHistory');
60       if( open HIST, ">$hist_file" ) {
61          print HIST join("\n",@lines);
62          close HIST;
63       } else {
64          $self->print("warning: unable to WriteHistory to $hist_file");
65       }
66    }
67 };
68
69 1;
70
71 __END__
72
73 =head1 NAME
74
75 Devel::REPL::Plugin::ReadLineHistory - Integrate history with the facilities provided by L<Term::ReadLine>
76
77 =head1 DESCRIPTION
78
79 This plugin enables loading and saving command line history from
80 a file as well has history expansion of previous commands using
81 the !-syntax a la bash.
82
83 By default, history expansion is enabled with this plugin when
84 using L<Term::ReadLine::Gnu|Term::ReadLine::Gnu>. That means that
85 "loose" '!' characters will be treated as history events which
86 may not be what you wish.
87
88 To avoid this, you need to quote the '!' with '\':
89
90   my $var = "foo\!";
91
92 or place the arguments in single quotes---but enable the
93 C<Term::ReadLine> attribute C<history_quotes_inhibit_expansion>:
94
95   $_REPL->term->Attribs->{history_quotes_inhibit_expansion} = 1;
96   my $var = 'foo!';
97
98 and to disable history expansion from GNU readline/history do
99
100   $_REPL->term->Attribs->{do_expand} = 0;
101
102 =head1 CONFLICTS
103
104 Note that Term::ReadLine::Perl does not support a history
105 expansion method.  In that case, you may wish to use the
106 Devel::REPL History plugin which provides similar functions.
107 Work is underway to make use of either History or
108 ReadLineHistory consistent for expansion with either the
109 Term::ReadLine::Gnu support or Term::ReadLine::Perl.
110
111 =cut
112