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