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'.

Methods

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 IN and OUT file handles. These arguments should be globs.

readline(PROMPT[,PREPUT])

gets an input line, with actual GNU Readline support. Trailing newline is removed. Returns undef on EOF. PREPUT is an optional argument meaning the initial value of input.

The optional argument PREPUT is granted only if the value preput is in Features.

AddHistory(LINE1, LINE2, ...)

adds the lines to the history of input, from where it can be used if the actual readline is present.

MinLine([MAX])

If argument MAX is specified, it is an advice on minimal size of line to be included into history. undef means do not include anything into history. Returns the old value.

StifleHistory(MAX)

stifles the history list, remembering only the last MAX entries. If MAX is undef, remembers all entries.

SetHistory(LINE1, LINE2, ...)

sets the history of input, from where it can be used if the actual readline is present.

GetHistory

returns the history of input as a list, if actual readline is present.

ReadHistory([FILENAME [,FROM [,TO]]])

adds the contents of FILENAME to the history list, a line at a time. If FILENAME is false, then read from ~/.history. Start reading at line FROM and end at TO. If FROM is omitted or zero, start at the beginning. If TO is omittied or less than FROM, then read until the end of the file. Returns true if successful, or false if not.

WriteHistory([FILENAME])

writes the current history to FILENAME, overwriting FILENAME if necessary. If FILENAME is false, then write the history list to ~/.history. Returns true if successful, or false if not.

AddDefun(NAME, FUNC [,KEY])

Add name to the Perl function FUNC. If optional argument KEY is specified, bind it to the FUNC. Returns non-zero in the case of an invalid KEY.

  Example:
	# name name `reverse-line' to a function reverse_line(), and bind
	# it to "\C-t"
	$term->AddDefun('reverse-line', \&reverse_line, "\ct");
BindKey(KEY, FUNCTION [,MAP])

Bind KEY to the FUNCTION. FUNCTION is the name added by the AddDefun method. If optional argument MAP is specified, binds in MAP. Returns non-zero in case of error.

UnbindKey(KEY [,MAP])

Bind KEY to the null function. Returns non-zero in case of error.

ParseAndBind(LINE)

Parse LINE as 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'.

IN, OUT

return the file handles for input and output or undef if readline input and output cannot be used for Perl.

findConsole

returns an array with two strings that give most appropriate names for files for input and output using conventions "<$in", ">$out".

Features

Returns a reference to a hash with keys being features present in current implementation. Several optional features are used in the minimal interface: appname should be present if the first argument to new is recognized, and minline should be present if MinLine method is not dummy. autohistory should be present if lines are put into history automatically (maybe subject to MinLine), and addHistory if AddHistory method is not dummy. preput means the second argument to readline method is processed. getHistory and setHistory denote that the corresponding methods are present. tkRunning denotes that a Tk application may run while ReadLine is getting input (undocumented feature).

FetchVar(VARIABLE_NAME), StoreVar(VARIABLE_NAME, VALUE)

Fetch and store a value of a GNU Readline Library variable. See section VARIABLES.

Variables

Following GNU Readline 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 FetchVar()/StoreVar() methods and rl_fetch_var()/rl_store_var() functions. And all these variables are tied to Perl scalar variables. These are not exported by default. Full qualified name or explicit "import" is required.

Examples:

# using method
$v = $term->FetchVar('rl_library_version');
# using full qualified name
$v = Term::ReadLine::GNU::rl_fetch_var('rl_library_version');
$v = $Term::ReadLine::GNU::rl_library_version;
# import symbols
use Term::ReadLine::GNU qw(rl_fetch_var rl_library_version);
$v = rl_fetch_var('rl_library_version');
$v = $rl_library_version;
base_function
str rl_library_version (read only)
str rl_terminal_name
str rl_readline_name
keybind_function
Keymap rl_executing_keymap (read only)
Keymap rl_binding_keymap (read only)
misc_function
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)
filehandle rl_instream
filehandle rl_outstream
pfunc rl_startup_hook
pfunc rl_event_hook
pfunc rl_getc_function
pfunc rl_redisplay_function
completion_function
pfunc rl_completion_entry_function
pfunc rl_attempted_completion_function
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
history_function
int history_base
int history_length
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

Functions

Followings GNU Readline/History Library support functions are provided as Perl functions. These are not exported by default. Full qualified name or explicit "import" is required.

Examples:

# using full qualified name
$v = Term::ReadLine::GNU::rl_fetch_var('rl_library_version');
# import symbols
use Term::ReadLine::GNU qw(rl_fetch_var);
$v = rl_fetch_var('rl_library_version');
base_function
any	rl_fetch_var(str name)
any	rl_store_var(str name, any val)
string	rl_readline(prompt = '')
void	add_history(str string)
(int result, str expansion)
	history_expand(str line)
keybind_function
FunctionPtr
	rl_add_defun(str name, pfunc perl_fn, int key = -1)
Keymap	rl_make_bare_keymap()
Keymap	rl_copy_keymap(Keymap|str map)
Keymap	rl_make_keymap()
Keymap	rl_discard_keymap(Keymap|str map)
Keymap	rl_get_keymap()
Keymap	rl_set_keymap(Keymap|str map)
Keymap	rl_get_keymap_by_name(str name)
str	rl_get_keymap_name(Keymap map)
int	rl_bind_key(int key, FunctionPtr|str function,
		    Keymap|str map = rl_get_keymap())
int	rl_unbind_key(int key, Keymap|str map = rl_get_keymap())
int	rl_generic_bind(int type, str keyseq,
			FunctionPtr|Keymap|str data,
			Keymap|str map = rl_get_keymap())
void	rl_parse_and_bind(str line)
int	rl_read_init_file(str filename = '~/.inputrc')
int	rl_call_function(FunctionPtr|str function, count = 1, key = -1)
FunctionPtr
	rl_named_function(str name)
str	rl_get_function_name(FunctionPtr function)
(FunctionPtr|Keymap|str data, int type)
	rl_function_of_keyseq(str keyseq,
			      Keymap|str map = rl_get_keymap())
(@str)	rl_invoking_keyseqs(FuntionPtr|str function,
			    Keymap|str map = rl_get_keymap())
void	rl_function_dumper(int readable = 0)
void	rl_list_funmap_names()
misc_function
int	rl_begin_undo_group()
int	rl_end_undo_group()
int	rl_add_undo(int what, int start, int end, str text)
void	free_undo_list()
int	rl_do_undo()
int	rl_modifying(int start = 0, int end = rl_end)

void	rl_redisplay()
int	rl_forced_update_display()
int	rl_on_new_line()
int	rl_reset_line_state()
int	rl_message(str fmt, ...)
int	rl_clear_message()

int	rl_insert_text(str text)
int	rl_delete_text(start = 0, end = rl_end)
str	rl_copy_text(start = 0, end = rl_end)
int	rl_kill_text(start = 0, end = rl_end)
int	rl_read_key()
int	rl_getc(FILE *)
int	rl_stuff_char(int c)
int	rl_initialize()
int	rl_reset_terminal(str terminal_name = getenv($TERM))
int	ding()
callback_function
void	rl_callback_handler_install(str prompt, pfunc lhandler)
void	rl_callback_read_char()
void	rl_callback_handler_remove()
completion_function
int	rl_complete_internal(int what_to_do = TAB)
(@str)	completion_matches(str text,
			   pfunc fn = filename_completion_function)
str	filename_completion_function(str text, int state)
str	username_completion_function(str text, int state)
str	list_completion_function(str text, int state)
history_function
void	using_history()
str	remove_history(int which)
str	replace_history_entry(int which, str line)
void	clear_history()
int	stifle_history(int max|undef)
int	history_is_stifled()
int	where_history()
str	current_history()
str	history_get(offset)
int	history_total_bytes()
int	history_set_pos(int pos)
str	previous_history()
str	next_history()
int	history_search(str string,
		       int direction = -1, int pos = where_hisotry())
int	history_search_prefix(str string, int direction = -1)
int	read_history_range(str filename = '~/.history',
			   int from = 0, int to = -1)
int	write_history(str filename = '~/.history')
int	append_history(int nelements, str filename = '~/.history')
int	history_trancate_file(str filename = '~/.history',
			      int nlines = 0)

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 are TEXT and STATE. TEXT is the partial word to be completed. STATE is 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 returns undef this signals completion_matches() that there are no more possibilities left.

If the value is undef, built-in filename_completion_function is used.

A sample generator function, list_completion_function, is defined in Gnu.pm. You can use it as follows;

    use Term::ReadLine;
    use Term::ReadLine::Gnu qw(:completion_function);
    ...
    my $term = new Term::ReadLine 'sample';
    ...
    $rl_completion_entry_function = \&list_completion_function;
    ...
    @{$term->{CompletionWordList}} =
	qw(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, and END. LINE_BUFFER is a current input buffer string. START and END are indices in LINE_BUFFER saying what the boundaries of TEXT are.

If this function exists and returns null list or undef, or if this variable is set to undef, then an internal function rl_complete() will call the value of $rl_completion_entry_function to 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::Gnu qw(:completion_function);
sub sample_completion {
    my ($text, $line, $start, $end) = @_;
    # If first word then username completion, else filename completion
    if (substr($line, 0, $start) =~ /^\s*$/) {
	    return completion_matches($text, \&username_completion_function);
    } else {
	    return ();
    }
}
...
$rl_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, returns undef. The first entry in the returned array is the substitution for TEXT. The remaining entries are the possible completions.

ENTRY_FUNC is a generator function which has two args, and returns a string. The first argument is TEXT. The second is a state argument; it is zero on the first call, and non-zero on subsequent calls. ENTRY_FUNC returns a undef to the caller when there are no more matches.

If the value of ENTRY_FUNC is undef, built-in filename_completion_function is used.

completion_matches is a Perl wrapper function of an internal function completion_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_function shows 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 new method is used as the application construct.

For example, when your program call new method 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, hayashi@pdcd.ilab.toshiba.co.jp

TODO

support TkRunning as Term::ReadLine::Perl

BUGS

rl_add_defun() can be defined up to 16 functions.

rl_message() does not work. See display_readline_version() in t/readline.t.

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()

Perl 5.002 and 5.003 cause segmentation fault if @histfn is added to %EXPORT_TAGS. But Perl 5.003_08 does not. What's wrong?