A pretty full-function package similar to GNU's readline. Includes support for EUC-encoded Japanese text.
Written by Jeffrey Friedl, Omron Corporation (jfriedl@omron.co.jp)
Comments, corrections welcome.
Thanks to the people at FSF for readline (and the code I referenced while writing this), and for Roland Schemers whose line_edit.pl I used as an early basis for this.
(see the manual for complete info)
Once this package is included (require'd), you can then call:
$text = &readline::readline($input);
to get lines of input from the user.
Normally, it reads ~/.inputrc when loaded. To suppress this, set
$readline::rl_NoInitFromFile = 1;
before requiring the package.
Call rl_bind() to add your own key bindings, as in:
&readline::rl_bind('C-L', 'possible-completions');
Call rl_set to set mode variables yourself, as in:
&readline::rl_set('TcshCompleteMode', 'On');
To change the input mode (emacs or vi) use ~/.inputrc or call
&readline::rl_set('EditingMode', 'vi'); or: &readline::rl_set('EditingMode', 'emacs');
Call rl_basic_commands to set your own command completion, as in:
&readline::rl_basic_commands('print', 'list', 'quit', 'run', 'status');
hey, it's in perl.
Pretty full GNU readline like library...
support for ~/.inputrc
horizontal scrolling
command/file completion
rebinding
history (with search)
undo
numeric prefixes
supports multi-byte characters (at least for the Japanese I use).
Has a tcsh-like completion-function mode.call &readline::rl_set('tcsh-complete-mode', 'On') to turn on.
Can you say HUGE?
I can't spell, so comments riddled with misspellings.
Written by someone that has never really used readline.
History mechanism is slightly different than GNU... may get fixed someday, but I like it as it is now...
Killbuffer not a ring.. just one level.
Obviously not well tested yet.
Written by someone that doesn't have a bell on his terminal, so
proper readline use of the bell may not be here.
Functions beginning with F_ are functions that are mapped to keys. Variables and functions beginning rl_ may be accessed/set/called/read from outside the package. Other things are internal.
F_
rl_
Some notable internal-only variables of global proportions:
$prompt
$line
$D
$InsertMode
$InputLocMsg
%emacs_keymap
@emacs_keymap
$emacs_keymap{'name'}
$emacs_keymap{'default'}
%vi_keymap
%vicmd_keymap
%vipos_keymap
%visearch_keymap
%KeyMap
$LastCommandKilledText
$lastcommand
$lastredisplay
$force_redraw
$AcceptLine
$ReturnEOF
@Pending
@undo
$KillBuffer
@tcsh_complete_selections
Some internal variables modified by &rl_set(). See comment at &rl_set for info about how these set'able variables work.
$var_EditingMode
$var_TcshCompleteMode
Other $var_ things not supported yet.
$var_
Some variables used internally, but may be accessed from outside...
$VERSION
../Makefile.PL
$rl_readline_name
$0
$rl_NoInitFromFile
@rl_History
$rl_HistoryIndex
$rl_completion_function
$rl_basic_word_break_characters
$rl_start_default_at_beginning
$rl_completer_word_break_characters
$rl_completer_terminator_character
' '
$rl_special_prefixes
$
&
@'
%
$text
&@$%
$rl_MaxHistorySize
$rl_screen_width
$rl_correct_sw
$rl_margin
$rl_CLEAR
$rl_max_numeric_arg
$rl_vi_replace_default_on_insert
This behavior similar to many GUI controls' behavior, which select the default text so that new text replaces the old.
Use with $rl_start_default_at_beginning for normal-looking behavior (though it works just fine without it).
Notes/Bugs:
Control characters (like C-w) do not actually terminate this replace mode, for the same reason it does not work in emacs mode.
Spine-crawlingly scary subroutine redefinitions
$rl_mark
$line_rl_mark
$_rl_japanese_mb
When asked to do a completion operation, readline isolates the word to the immediate left of the cursor (i.e. what's just been typed). This information is then passed to some function (which may be supplied by the user of this package) which will return an array of possible completions.
If there is just one, that one is used. Otherwise, they are listed in some way depending upon $var_TcshCompleteMode.
The default is to do filename completion. The function that performs this task is _<readline::rl_filename_list()_.
A minimal-trouble way to have command-completion is to call _readline::rl_basic_commands()_ with an array of command names, such as readline::rl_basic_commands('quit', 'run', 'set', 'list'). Those command names will then be used for completion if the word being completed begins the line. Otherwise, completion is disallowed.
readline::rl_basic_commands('quit', 'run', 'set', 'list')
The way to have the most power is to provide a function to readline which will accept information about a partial word that needs completed, and will return the appropriate list of possibilities. This is done by setting $readline::rl_completion_function to the name of the function to run.
$readline::rl_completion_function
That function will be called with three args ($text, $line, $start). $text is the partial word that should be completed. $line is the entire input line as it stands, and $start is the index of the $text in C$<line>. That is, zero if $text is at the beginning of $line.
($text, $line, $start)
$start
A cool completion function will look at $line and $start and give context- sensitive completion lists. Consider something that will do completion for two commands:
cat FILENAME finger USERNAME status [this|that|other]
It (untested) might look like:
$readline::rl_completion_function = "main::complete"; sub complete { local($text, $_, $start) = @_; ## return commands which may match if at the beginning.... return grep(/^$text/, 'cat', 'finger') if $start == 0; return &rl_filename_list($text) if /^cat\b/; return &my_namelist($text) if /^finger\b/; return grep(/^text/, 'this', 'that','other') if /^status\b/; (); }
A real completion function would be more robust.
To install Term::ReadLine::Perl5, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Term::ReadLine::Perl5
CPAN shell
perl -MCPAN -e shell install Term::ReadLine::Perl5
For more information on module installation, please visit the detailed CPAN module installation guide.