NAME
Term::ReadLine::Gnu - Perl extension for the GNU Readline/History Library
SYNOPSIS
use Term::ReadLine;
$term = new Term::ReadLine 'ProgramName';
while ( defined ($_ = $term->readline('prompt>')) ) {
...
}
DESCRIPTION
Overview
This is an implementation of Term::ReadLine using the GNU Readline/History Library.
For basic functions object oriented interface is provided. These are described in the section Methods.
This package also has the interface with the almost all variables and functions which are documented in the GNU Readline/History Library Manual. These variables and functions are documented in the section Variables and Functions briefly. For more detail of the GNU Readline/History Library, see 'GNU Readline Library Manual' and 'GNU History Library Manual'.
Minimal Set of Methods defined by Readline.pm
ReadLine-
returns the actual package that executes the commands. If you have installed this package, possible value is
Term::ReadLine::Gnu. new(NAME,[IN[,OUT]])-
returns the handle for subsequent calls to following functions. Argument is the name of the application. Optionally can be followed by two arguments for
INandOUTfile handles. These arguments should be globs. readline(PROMPT[,PREPUT])-
gets an input line, with actual
GNU Readlinesupport. Trailing newline is removed. ReturnsundefonEOF.PREPUTis an optional argument meaning the initial value of input.The optional argument
PREPUTis granted only if the valuepreputis inFeatures. AddHistory(LINE1, LINE2, ...)-
adds the lines to the history of input, from where it can be used if the actual
readlineis present. IN,OUT-
return the file handles for input and output or
undefifreadlineinput and output cannot be used for Perl. MinLine([MAX])-
If argument
MAXis specified, it is an advice on minimal size of line to be included into history.undefmeans do not include anything into history. Returns the old value. findConsole-
returns an array with two strings that give most appropriate names for files for input and output using conventions
"<$in",">$out". Attribs-
returns a reference to a hash which describes internal configuration (variables) of the package. Names of keys in this hash conform to standard conventions with the leading
rl_stripped.See section "Variables" for supported variables.
Features-
Returns a reference to a hash with keys being features present in current implementation. Several optional features are used in the minimal interface:
appnameshould be present if the first argument tonewis recognized, andminlineshould be present ifMinLinemethod is not dummy.autohistoryshould be present if lines are put into history automatically (maybe subject toMinLine), andaddhistoryifAddHistorymethod is not dummy.preputmeans the second argument toreadlinemethod is processed.getHistoryandsetHistorydenote that the corresponding methods are present.tkRunningdenotes that a Tk application may run while ReadLine is getting input (undocumented feature).
Additional Supported Methods
All these GNU Readline/History Library functions are callable via method interface and have names which conform to standard conventions with the leading rl_ stripped.
Almost methods have lower level functions in Term::ReadLine::Gnu::XS package. To use them full qualified name is required. Using method interface is preferred.
Readline Convenience Functions
- Naming Function
-
add_defun(NAME, FUNC [,KEY=-1])-
Add name to the Perl function
FUNC. If optional argumentKEYis specified, bind it to theFUNC. Returns reference toFunctionPtr.Example: # name name `reverse-line' to a function reverse_line(), # and bind it to "\C-t" $term->add_defun('reverse-line', \&reverse_line, "\ct");
- Selecting a Keymap
-
make_bare_keymap-
Keymap rl_make_bare_keymap() copy_keymap(MAP)-
Keymap rl_copy_keymap(Keymap|str map) make_keymap-
Keymap rl_make_keymap() discard_keymap(MAP)-
Keymap rl_discard_keymap(Keymap|str map) get_keymap-
Keymap rl_get_keymap() set_keymap(MAP)-
Keymap rl_set_keymap(Keymap|str map) get_keymap_by_name(NAME)-
Keymap rl_get_keymap_by_name(str name) get_keymap_name(MAP)-
str rl_get_keymap_name(Keymap map)
- Binding Keys
-
bind_key(KEY, FUNCTION [,MAP])-
int rl_bind_key(int key, FunctionPtr|str function, Keymap|str map = rl_get_keymap())Bind
KEYto theFUNCTION.FUNCTIONis the name added by theadd_defunmethod. If optional argumentMAPis specified, binds inMAP. Returns non-zero in case of error. unbind_key(KEY [,MAP])-
int rl_unbind_key(int key, Keymap|str map = rl_get_keymap())Bind
KEYto the null function. Returns non-zero in case of error. generic_bind(TYPE, KEYSEQ, DATA, [,MAP])-
int rl_generic_bind(int type, str keyseq, FunctionPtr|Keymap|str data, Keymap|str map = rl_get_keymap()) parse_and_bind(LINE)-
void rl_parse_and_bind(str line)Parse
LINEas if it had been read from the ~/.inputrc file and perform any key bindings and variable assignments found. For more detail see 'GNU Readline Library Manual'. read_init_file([FILENAME])-
int rl_read_init_file(str filename = '~/.inputrc')
- Associating Function Names and Bindings
-
call_function(FUNCTION, [COUNT [,KEY]])-
int rl_call_function(FunctionPtr|str function, count = 1, key = -1) named_function(NAME)-
FunctionPtr rl_named_function(str name) get_function_name(FUNCTION)-
str rl_get_function_name(FunctionPtr function) function_of_keyseq(KEYMAP [,MAP])-
(FunctionPtr|Keymap|str data, int type) rl_function_of_keyseq(str keyseq, Keymap|str map = rl_get_keymap()) invoking_keyseqs(FUNCTION [,MAP])-
(@str) rl_invoking_keyseqs(FunctionPtr|str function, Keymap|str map = rl_get_keymap()) function_dumper([READABLE])-
void rl_function_dumper(int readable = 0) list_funmap_names-
void rl_list_funmap_names()
- Allowing Undoing
-
begin_undo_group-
int rl_begin_undo_group() end_undo_group-
int rl_end_undo_group() add_undo(WHAT, START, END, TEXT)-
int rl_add_undo(int what, int start, int end, str text) free_undo_list-
void free_undo_list() do_undo-
int rl_do_undo() modifying([START [,END]])-
int rl_modifying(int start = 0, int end = rl_end)
- Redisplay
- Modifying Text
- Utility Functions
- Alternate Interface
Completion Functions
complete_internal([WHAT_TO_DO])-
int rl_complete_internal(int what_to_do = TAB) completion_matches(TEXT [,FUNC])-
(@str) completion_matches(str text, pfunc func = filename_completion_function) filename_completion_function(TEXT, STATE)-
str filename_completion_function(str text, int state) username_completion_function(TEXT, STATE)-
str username_completion_function(str text, int state) listname_completion_function(TEXT, STATE)-
str list_completion_function(str text, int state)
History Functions
- Initializing History and State Management
-
using_history-
void using_history()
- History List Management
-
addhistory(STRING[, STRING, ...])-
void add_history(str string) StifleHistory(MAX)-
int stifle_history(int max|undef)stifles the history list, remembering only the last
MAXentries. IfMAXis undef, remembers all entries. SetHistory(LINE1 [, LINE2, ...])-
sets the history of input, from where it can be used if the actual
readlineis present. remove_history(WHICH)-
str remove_history(int which) replace_history_entry(WHICH, LINE)-
str replace_history_entry(int which, str line) clear_history-
void clear_history() history_is_stifled-
int history_is_stifled()
- Information About the History List
- Moving Around the History List
- Searching the History List
- Managing the History File
-
ReadHistory([FILENAME [,FROM [,TO]]])-
int read_history_range(str filename = '~/.history', int from = 0, int to = -1)adds the contents of
FILENAMEto the history list, a line at a time. IfFILENAMEis false, then read from ~/.history. Start reading at lineFROMand end atTO. IfFROMis omitted or zero, start at the beginning. IfTOis omitted or less thanFROM, then read until the end of the file. Returns true if successful, or false if not. WriteHistory([FILENAME])-
int write_history(str filename = '~/.history')writes the current history to
FILENAME, overwritingFILENAMEif necessary. IfFILENAMEis false, then write the history list to ~/.history. Returns true if successful, or false if not. append_history(NELEMENTS [,FILENAME])-
int append_history(int nelements, str filename = '~/.history') history_truncate_file([FILENAME [,NLINES]])-
int history_truncate_file(str filename = '~/.history', int nlines = 0)
- History Expansion
Variables
Following GNU Readline/History Library variables can be accessed from Perl program. See 'GNU Readline Library Manual' and ' GNU History Library Manual' for each variable. You can access them with Attribs methods. Names of keys in this hash conform to standard conventions with the leading rl_ stripped.
Examples:
$attribs = $term->Attribs;
$v = $attribs->{library_version}; # rl_library_version
$v = $attribs->{history_base}; # history_base
- Readline Variables
-
str rl_line_buffer int rl_point int rl_end int rl_mark int rl_done int rl_pending_input str rl_prompt (read only) str rl_library_version (read only) str rl_terminal_name str rl_readline_name filehandle rl_instream filehandle rl_outstream pfunc rl_startup_hook pfunc rl_event_hook pfunc rl_getc_function pfunc rl_redisplay_function Keymap rl_executing_keymap (read only) Keymap rl_binding_keymap (read only) - Completion Variables
-
pfunc rl_completion_entry_function pfunc rl_attempted_completion_function rl_filename_quoting_function (not implemented) rl_filename_dequoting_function (not implemented) rl_char_is_quoted_p (not implemented) int rl_completion_query_items str rl_basic_word_break_characters str rl_basic_quote_characters str rl_completer_word_break_characters str rl_completer_quote_characters str rl_filename_quote_characters str rl_special_prefixes int rl_completion_append_character int rl_ignore_completion_duplicates int rl_filename_completion_desired int rl_filename_quoting_desired int rl_inhibit_completion rl_ignore_some_completion_function (not implemented) rl_directory_completion_hook (not implemented) - History Variables
-
int history_base int history_length int max_input_history (not implemented) char history_expansion_char char history_subst_char char history_comment_char str history_no_expand_chars str history_search_delimiter_chars int history_quotes_inhibit_expansion - Function References
-
rl_getc rl_callback_read_char filename_completion_function username_completion_function list_completion_function Term::ReadLine::GnuSpecific Variables-
do_expand # if true history expansion is enabled completion_word # for list_completion_function
Custom Completion
In this section variables and functions for custom completion is described with examples.
Most of descriptions in this section is cited from GNU Readline Library manual.
rl_completion_entry_function-
This variable holds reference refers to a generator function for
completion_matches().A generator function is called repeatedly from
completion_matches(), returning a string each time. The arguments to the generator function areTEXTandSTATE.TEXTis the partial word to be completed.STATEis zero the first time the function is called, allowing the generator to perform any necessary initialization, and a positive non-zero integer for each subsequent call. When the generator function returnsundefthis signalscompletion_matches()that there are no more possibilities left.If the value is undef, built-in
filename_completion_functionis used.A sample generator function,
list_completion_function, is defined in Gnu.pm. You can use it as follows;use Term::ReadLine; ... my $term = new Term::ReadLine 'sample'; my $attribs = $term->Attribs; ... $attribs->{rl_completion_entry_function} = $attribs->{'list_completion_function'}; ... $attribs->{completion_word} = [qw(reference to a list of words which you want to use for completion)]; $term->readline("custom completion>");See also
completion_matches. rl_attempted_completion_function-
A reference to an alternative function to create matches.
The function is called with
TEXT,LINE_BUFFER,START, andEND.LINE_BUFFERis a current input buffer string.STARTandENDare indices inLINE_BUFFERsaying what the boundaries ofTEXTare.If this function exists and returns null list or
undef, or if this variable is set toundef, then an internal functionrl_complete()will call the value of$rl_completion_entry_functionto generate matches, otherwise the array of strings returned will be used.The default value of this variable is
undef. You can use it as follows;use Term::ReadLine; ... my $term = new Term::ReadLine 'sample'; my $attribs = $term->Attribs; ... sub sample_completion { my ($text, $line, $start, $end) = @_; # If first word then username completion, else filename completion if (substr($line, 0, $start) =~ /^\s*$/) { return $term->completion_matches($text, $attribs->{'username_completion_function'}); } else { return (); } } ... $attribs->{attempted_completion_function} = \&sample_completion; completion_matches(TEXT, ENTRY_FUNC)-
Returns an array of strings which is a list of completions for
TEXT. If there are no completions, returnsundef. The first entry in the returned array is the substitution forTEXT. The remaining entries are the possible completions.ENTRY_FUNCis a generator function which has two arguments, and returns a string. The first argument isTEXT. The second is a state argument; it is zero on the first call, and non-zero on subsequent calls.ENTRY_FUNCreturns aundefto the caller when there are no more matches.If the value of
ENTRY_FUNCis undef, built-infilename_completion_functionis used.completion_matchesis a Perl wrapper function of an internal functioncompletion_matches(). See also$rl_completion_entry_function. list_completion_function(TEXT, STATE)-
A sample generator function defined by Term::ReadLine::Gnu.pm. Example code at
rl_completion_entry_functionshows how to use this function.
FILES
- ~/.inputrc
-
Readline init file. Using this file it is possible that you would like to use a different set of key bindings. When a program which uses the Readline library starts up, the init file is read, and the key bindings are set.
Conditional key binding is also available. The program name which is specified by the first argument of
newmethod is used as the application construct.For example, when your program call
newmethod like this;... $term = new Term::ReadLine 'PerlSh'; ...your ~/.inputrc can define key bindings only for it as follows;
... $if PerlSh Meta-Rubout: backward-kill-word "\C-x\C-r": re-read-init-file "\e[11~": "Function Key 1" $endif ...
EXPORTS
None.
SEE ALSO
GNU Readline Library Manual
GNU History Library Manual
Term::ReadLine
Term::ReadLine::Perl (Term-ReadLine-xx.tar.gz)
AUTHOR
Hiroo Hayashi <hiroo.hayashi@toshiba.co.jp>
TODO
Test routines for following variable and functions are required.
rl_read_key()
rl_stuff_char()
rl_callback_handler_install()
rl_callback_read_char()
rl_callback_handler_remove()
rl_complete_internal()
history_search()
history_search_prefix()
BUGS
rl_add_defun() can define up to 16 functions.
rl_message() does not work. See display_readline_version() in t/readline.t.