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;
9 # ABSTRACT: Integrate history with the facilities provided by L<Term::ReadLine>
11 our $VERSION = '1.003028';
13 use Devel::REPL::Plugin;
16 use namespace::autoclean;
18 my $hist_file = $ENV{PERLREPL_HISTFILE} ||
19 File::Spec->catfile(File::HomeDir->my_home, '.perlreplhist');
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
24 my $hist_len=$ENV{PERLREPL_HISTLEN} || 100;
29 if ($self->term->ReadLine eq 'Term::ReadLine::Gnu') {
30 $self->term->stifle_history($hist_len);
32 if ($self->term->ReadLine eq 'Term::ReadLine::Perl') {
33 $self->term->Attribs->{MaxHistorySize} = $hist_len;
36 if ($self->term->ReadLine eq 'Term::ReadLine::Gnu') {
37 $self->term->ReadHistory($hist_file);
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>) {
43 $self->term->addhistory($line);
49 $self->term->Attribs->{do_expand}=1; # for Term::ReadLine::Gnu
50 $self->term->MinLine(2); # don't save one letter commands
52 # let History plugin know we have Term::ReadLine support
53 $self->have_readline_history(1) if $self->can('have_readline_history');
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");
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);
68 $self->print("warning: unable to WriteHistory to $hist_file");
81 This plugin enables loading and saving command line history from
82 a file as well has history expansion of previous commands using
83 the !-syntax a la bash.
85 By default, history expansion is enabled with this plugin when
86 using L<Term::ReadLine::Gnu|Term::ReadLine::Gnu>. That means that
87 "loose" '!' characters will be treated as history events which
88 may not be what you wish.
90 To avoid this, you need to quote the '!' with '\':
94 or place the arguments in single quotes---but enable the
95 C<Term::ReadLine> attribute C<history_quotes_inhibit_expansion>:
97 $_REPL->term->Attribs->{history_quotes_inhibit_expansion} = 1;
100 and to disable history expansion from GNU readline/history do
102 $_REPL->term->Attribs->{do_expand} = 0;
106 Note that L<Term::ReadLine::Perl> does not support a history
107 expansion method. In that case, you may wish to use the
108 L<Devel::REPL History plugin|Devel::REPL::Plugin::History> which provides similar functions.
109 Work is underway to make use of either L<History|Devel::REPL::Plugin::History> or
110 L<ReadLineHistory|Devel::REPL::Plugin::ReadHistory>> consistent for expansion with either the
111 L<Term::ReadLine::Gnu> support or L<Term::ReadLine::Perl>.