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 ;)
4 # epitaph, 22nd April 2007
8 package Devel::REPL::Plugin::ReadLineHistory;
10 use Devel::REPL::Plugin;
13 use namespace::autoclean;
15 my $hist_file = $ENV{PERLREPL_HISTFILE} ||
16 File::Spec->catfile(File::HomeDir->my_home, '.perlreplhist');
18 # HISTLEN should probably be in a config file to stop people accidentally
19 # truncating their history if they start the program and forget to set
21 my $hist_len=$ENV{PERLREPL_HISTLEN} || 100;
26 if ($self->term->ReadLine eq 'Term::ReadLine::Gnu') {
27 $self->term->stifle_history($hist_len);
29 if ($self->term->ReadLine eq 'Term::ReadLine::Perl') {
30 $self->term->Attribs->{MaxHistorySize} = $hist_len;
33 if ($self->term->ReadLine eq 'Term::ReadLine::Gnu') {
34 $self->term->ReadHistory($hist_file);
36 if ($self->term->ReadLine eq 'Term::ReadLine::Perl') {
37 open HIST, $hist_file or die "ReadLineHistory: could not open $hist_file: $!\n";
38 while (my $line = <HIST>) {
40 $self->term->addhistory($line);
46 $self->term->Attribs->{do_expand}=1; # for Term::ReadLine::Gnu
47 $self->term->MinLine(2); # don't save one letter commands
49 # let History plugin know we have Term::ReadLine support
50 $self->have_readline_history(1) if $self->can('have_readline_history');
55 if ($self->term->ReadLine eq 'Term::ReadLine::Gnu') {
56 $self->term->WriteHistory($hist_file) ||
57 $self->print("warning: failed to write history file $hist_file");
59 if ($self->term->ReadLine eq 'Term::ReadLine::Perl') {
60 my @lines = $self->term->GetHistory() if $self->term->can('GetHistory');
61 if( open HIST, ">$hist_file" ) {
62 print HIST join("\n",@lines);
65 $self->print("warning: unable to WriteHistory to $hist_file");
76 Devel::REPL::Plugin::ReadLineHistory - Integrate history with the facilities provided by L<Term::ReadLine>
80 This plugin enables loading and saving command line history from
81 a file as well has history expansion of previous commands using
82 the !-syntax a la bash.
84 By default, history expansion is enabled with this plugin when
85 using L<Term::ReadLine::Gnu|Term::ReadLine::Gnu>. That means that
86 "loose" '!' characters will be treated as history events which
87 may not be what you wish.
89 To avoid this, you need to quote the '!' with '\':
93 or place the arguments in single quotes---but enable the
94 C<Term::ReadLine> attribute C<history_quotes_inhibit_expansion>:
96 $_REPL->term->Attribs->{history_quotes_inhibit_expansion} = 1;
99 and to disable history expansion from GNU readline/history do
101 $_REPL->term->Attribs->{do_expand} = 0;
105 Note that L<Term::ReadLine::Perl> does not support a history
106 expansion method. In that case, you may wish to use the
107 L<Devel::REPL History plugin|Devel::REPL::Plugin::History> which provides similar functions.
108 Work is underway to make use of either L<History|Devel::REPL::Plugin::History> or
109 L<ReadLineHistory|Devel::REPL::Plugin::ReadHistory>> consistent for expansion with either the
110 L<Term::ReadLine::Gnu> support or L<Term::ReadLine::Perl>.