make room for $VERSION after package declaration (newer [PkgVersion] requires it)
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / ReadLineHistory.pm
CommitLineData
9d03fe04 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 ;)
e4761e81 3#
9d03fe04 4# epitaph, 22nd April 2007
5
1716b200 6use strict;
7use warnings;
9d03fe04 8package Devel::REPL::Plugin::ReadLineHistory;
9
6a5409bc 10use Devel::REPL::Plugin;
ef59b3d9 11use File::HomeDir;
12use File::Spec;
13
14my $hist_file = $ENV{PERLREPL_HISTFILE} ||
15 File::Spec->catfile(File::HomeDir->my_home, '.perlreplhist');
9d03fe04 16
9d03fe04 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
20my $hist_len=$ENV{PERLREPL_HISTLEN} || 100;
21
22around 'run' => sub {
baa665c2 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 }
78453011 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
baa665c2 52 $self->$orig(@args);
78453011 53
baa665c2 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 }
9d03fe04 67};
68
691;
70
cfd1094b 71__END__
72
73=head1 NAME
74
75Devel::REPL::Plugin::ReadLineHistory - Integrate history with the facilities provided by L<Term::ReadLine>
76
265cea44 77=head1 DESCRIPTION
78
79This plugin enables loading and saving command line history from
80a file as well has history expansion of previous commands using
81the !-syntax a la bash.
82
83By default, history expansion is enabled with this plugin when
84using L<Term::ReadLine::Gnu|Term::ReadLine::Gnu>. That means that
85"loose" '!' characters will be treated as history events which
86may not be what you wish.
87
88To avoid this, you need to quote the '!' with '\':
89
90 my $var = "foo\!";
91
92or place the arguments in single quotes---but enable the
93C<Term::ReadLine> attribute C<history_quotes_inhibit_expansion>:
94
95 $_REPL->term->Attribs->{history_quotes_inhibit_expansion} = 1;
96 my $var = 'foo!';
97
98and to disable history expansion from GNU readline/history do
99
100 $_REPL->term->Attribs->{do_expand} = 0;
101
102=head1 CONFLICTS
103
104Note that Term::ReadLine::Perl does not support a history
105expansion method. In that case, you may wish to use the
106Devel::REPL History plugin which provides similar functions.
107Work is underway to make use of either History or
108ReadLineHistory consistent for expansion with either the
109Term::ReadLine::Gnu support or Term::ReadLine::Perl.
110
cfd1094b 111=cut
112