NAME
Term::ReadLine::Zoid - another ReadLine package
SYNOPSIS
# In your app:
use Term::ReadLine;
my $term = Term::ReadLine->new("my app");
my $prompt = "eval: ";
my $OUT = $term->OUT || \*STDOUT;
while ( defined ($_ = $term->readline($prompt)) ) {
# Think while (<STDIN>) {}
my $res = eval($_);
warn $@ if $@;
print $OUT $res, "\n" unless $@;
}
# In some rc file
export PERL_RL=Zoid
DESCRIPTION
This package provides a set of modules that form an interactive input buffer written in plain perl with minimal dependencies. It features almost all key-bindings described in the posix spec for the sh(1) utility with some extensions like multiline editing; this includes a vi-command mode with a save-buffer (for copy-pasting) and an undo-stack.
Historically this code was part of the Zoidberg shell, but this implementation is complete independent from zoid and uses the Term::ReadLine interface, so it can be used with other perl programs.
ENVIRONMENT
The Term::ReadLine interface module uses the PERL_RL
variable to decide which module to load; so if you want to use this module for all your perl applications, try something like:
export PERL_RL=Zoid
KEY MAPPING
The default key mapping is as follows:
- escape
- ^[
-
Place the line editor in command mode, see Term::ReadLine::Zoid::ViCommand.
- ^C
-
End editing and return an empty string.
- ^D
-
End editing and return
undef
. Disabled when there are any chars on the edit line. - delete
- backspace
- ^H
- ^?
-
Delete and backspace kill the current or previous character. The key '^?' is by default considered a backspace because most modern keyboards use this key for the "backspace" key and an escape sequence for the "delete" key. Of course '^H' is also considered a backspace.
- tab
- ^I
-
Try to complete the bigword on left of the cursor.
There is no default completion included in this package, so unless you define a custom expansion it doesn't do anything. See the completion_function option.
- return
- ^J
-
End editing and return the edit line to the application unless the newline is escaped.
If _all_ lines in the buffer end with a single '\', the newline is considered escaped you can continue typing on the next line. This behaviour can be a bit unexpected because this module has multiline support which historic readline implementations have not, historically the escaping of a newline is done by the application not by the library. The surpress this behaviour, and let the application do it's thing, disable the "automultiline" option.
To enter the real multiline editing mode, press 'escape m', see Term::ReadLine::Zoid::MultiLine.
- ^K
-
Delete from cursor to the end of the line.
- ^L
-
Clear entire screen.
- ^R
-
Enter incremental search mode, see Term::ReadLine::Zoid::ISearch.
- ^U
-
This is also known as the "kill" char. It deletes all characters on the edit line and puts them in the save buffer. You can paste them back in later with 'escape-p'.
- ^V
-
Insert next key literally, ignoring any key-bindings.
WARNING: control or escape chars in the editline can cause unexpected results
- ^W
-
Delete the word before the cursor.
- insert
-
Toggle replace bit.
- ^A
- home
-
Move cursor to the begin of the edit line.
- ^E
- end
-
Move cursor to the end of the edit line.
- ^B
- left
- ^F
- right
-
These keys can be used to move the cursor in the edit line.
- ^P
- up
- ^N
- down
-
These keys are used to rotate the history.
ATTRIBS
The hash with options can be accessed with the Attribs method. Also they can be altered interactively using the mini-buffer of the command mode.
- autohistory
-
If enabled lines are added to the history automaticly, subject to MinLine. By default enabled.
- autoenv
-
If enabled the environment variables
COLUMNS
andLINES
are kept up to date. By default enabled. - autolist
-
If set completions are listed directly when a completion fails, if not set you need to press "tab" twice to see a list of possible completions. By default enabled.
- automultiline
-
See return for a description. By default enabled.
- bell
-
This option can contain a CODE reference. The default is
print "\cG"
, which makes the terminal ring a bell. - completion
-
TODO private completion hook
- completion_function
-
This option can contain either a code ref or the name of a function to perform completion. For compatibility with Term::ReadLine::Perl the global scalar
$readline::rl_completion_function
will be checked if this option isn't defined.The function will get the following arguments:
$word
,$buffer
,$start
. Where$word
is the word before the cursor, while$buffer
is the complete text on the command line;$start
is the offset of$word
in$buffer
.The function should return a list of possible completions of
$word
. The completion list is checked for double entries.There is no default.
- ignore_comment
-
This option can be set to a string, if the edit line starts with this string the line is regarded to be a comment and is not returned to the application, but it will appear in the history if 'autohistory' is also set. Defaults to "#".
When there are multiple lines in the buffer they all need to start with the comment string for the buffer to be regarded as a comment.
- maxcomplete
-
Maximum number of completions to be displayed. By default set to 150.
- minline
-
This option controls which lines are included in the history, lines shorter then this number are ignored. When set to "0" all lines are included in the history, when set to
undef
all lines are ignored. Defaults to "0". - PS2
-
This option can contain the prompt to be used for extra buffer lines. It defaults to
"> "
.Although the "PS1" prompt (as specified as an argument to the
readline()
method) can contain newlines, the PS2 prompt can't. - RPS1
-
This option can contain a string that will be shown on the right side of the screen. This is known as the "right prompt" and the idea is stolen from zsh(1).
- title
-
Used to set the terminal title, defaults to the appname.
- low_latency
-
Changes the escape sequences are read from input. If true delays evalution of the escape key till the next char is known. By default disabled.
FILES
This module reads a rc-file on intialisation, either $HOME/.perl_rl_zoid_rc, $HOME/.zoid/perl_rl_zoid_rc or /etc/perl_rl_zoid_rc. The rc-file is a perl script with access to the Term::ReadLine::Zoid object through the method current()
. If you want to have different behaviour for different applications, try to check for $rl->{appname}
.
# in for example ~/.perl_rl_zoid_rc
my $rl = Term::ReadLine::Zoid->current();
# set low latency
$rl->Attribs()->{low_latency} = 1;
# alias control-space to escape
$rl->bindchr( chr(0), 'escape' );
# create an ad hoc macro
$rl->bindkey('^P', sub { $rl->press('mplayer -vo sdl ') } );
METHODS
ReadLine api
Functions specified by the Term::ReadLine documentation.
new($appname, $IN, $OUT)
-
Simple constructor. Arguments are the application name (used for default prompt and title string) and optional filehandles for input and output.
ReadLine()
-
Returns the name of the current ReadLine module actually used.
readline($prompt, $preput)
-
Returns a string entered by the user. The final newline is stripped, though the string might contain newlines elsewhere.
The prompt only supports the escape "!" for the history number of the current line, use "!!" for a literal "!". All other escapes you need to parse yourself, before supplying the prompt. The prompt defaults to
"$appname !> "
.If you want to do more with your prompt see Env::PS1.
$preput
can be used to set some text on the edit line allready. addhistory($line)
AddHistory($line)
-
Add a command to the history (subject to the minline option).
If autohistory is set this method will be called automaticly by readline.
IN()
-
Returns the filehandle used for input.
OUT()
-
Returns the filehandle used for output.
MinLine($value)
-
Sets minline option to
$value
and returns old value. findConsole()
-
TODO - what uses does this have ?
Attribs()
-
Returns a reference to the options hash.
Features()
-
Returns a reference to a hash with names of implemented features.
Be aware that the naming scheme is quite arbitrary, this module uses the same names as Term::ReadLine::Gnu for common features.
Extended api
SetHistory(@hist)
GetHistory()
-
Simple acces to the history arry, the "set" function supports both a list and a reference, the "get" function uses "wantarray". Not sure which behaviour is compatible with T:RL::Gnu.
TermSize()
-
Returns number of columns and lines on the terminal.
continue()
-
This method can be called to continue the previous
readline()
call. Can be used to build a custom auto-mulitline feature. current()
-
Returns the current T:RL::Zoid object, for use in rc files, see FILES.
bindkey($key, $sub, $map)
-
Bind a CODE reference to a key, the function gets called when the key is typed with the key name as an argument. The
$map
argument is optional and can be either "default", "command", "isearch" or "multiline".If
$sub
is not a reference it is considered an alias; these aliases are not recursive.For alphanumeric characters the name is the character itself, special characters have long speaking names and control characters are prefixed with a '^'.
Binding combination with the meta- or alt-key is not supported.
Private api
Methods for use in overload classes.
Avoid using these methods from the application.
switch_mode($mode)
-
Switch to input mode
$mode
; changes the key map and reblesses the object if the_on_switch
key returns a class name. reset()
-
Reset all temporary attributes.
save()
-
Returns a ref with a copy of some temporary attributes. Can be used to switch between multiple edit lines in combination with restore.
restore($save)
-
Restores saved attributes.
hist_up()
-
Scroll one position backwards in the history and display it in the buffer.
hist_down()
-
Scroll one position forwards in the history and display it in the buffer.
NOTES
With most modern keymappings the combination of the meta key (alt) with a letter is identical with an escape character followed by that letter.
Some functioality may in time be moved to the ::Base package.
TODO
UTF8 support, or general charset support, would be nice but at the moment I lack the means to test these things. If anyone has ideas or suggestions about this please contact me.
BUGS
Line wrap doesn't always displays the last character on the line right, no functional bug though.
Please mail the author if you find any other bugs.
AUTHOR
Jaap Karssenberg || Pardus [Larus] <pardus@cpan.org>
Copyright (c) 2004 Jaap G Karssenberg. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SEE ALSO
Term::ReadLine::Zoid::ViCommand, Term::ReadLine::Zoid::MultiLine, Term::ReadLine::Zoid::ISearch, Term::ReadLine::Zoid::Base, Term::ReadLine, Env::PS1, Zoidberg