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