Another readline package
# 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
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.
( The documentation sometimes referes to 'the application', this is the program using the ReadLine module for input. )
The Term::ReadLine interface module uses the \*(C`PERL_RL\*(C' 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
The function name is given between parenthesis, these can be used for privat key maps.
\$1
The default key mapping is as follows:
Place the line editor in command mode, see Term::ReadLine::Zoid::ViCommand.
End editing and return an empty string.
For a single line buffer ends editing and returns \*(C`undef\*(C' if the line is empty, else it deletes a char. For a multiline buffer, ends editing and returns the lines to the application if the cursor is on the last line and this line is empty, else it deletes a char. Note that the delete_char_or_eof function does what delete_char should do to be compatible with \s-1GNU\s0 readline lib.
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 \*(L"backspace\*(R" key and an escape sequence for the \*(L"delete\*(R" key. Of course '^H' is also considered a backspace.
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 \*(L"completion_function\*(R" option. Uses the \s-1PAGER\s0 environment variable to find a suitable pager when there are more completions to be shown then would fit on the screen. See also the \*(L"autolist\*(R" and \*(L"maxcomplete\*(R" options.
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 \*(L"automultiline\*(R" option. To enter the real multiline editing mode, press 'escape m', see Term::ReadLine::Zoid::MultiLine.
Return the current buffer to the application but remember where we are in history. This can be used to quickly (re-)execute series of commands from history.
Delete from cursor to the end of the line.
Clear entire screen. In contrast with other readline libraries, the prompt will remain at the bottom of the screen.
Enter incremental search mode, see Term::ReadLine::Zoid::ISearch.
This is also known as the \*(L"kill\*(R" 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'.
Insert next key literally, ignoring any key-bindings. \s-1WARNING:\s0 control or escape chars in the editline can cause unexpected results
Delete the word before the cursor.
Toggle replace bit.
Move cursor to the begin of the edit line.
Move cursor to the end of the edit line.
These keys can be used to move the cursor in the edit line.
These keys are used to rotate the history. The following keys are different in mutline mode, the others fall back to the default behaviour.
Insert a newline at the current cursor position.
Move the cursor one line up.
Move the cursor one line down.
Move the cursor one screen down, or to the bottom of the buffer.
Move the cursor one screen up, or to the top of the buffer.
End editing and return \*(C`undef\*(C'.
End editing and return \*(C`undef\*(C' if the buffer is completely empty.
Like complete but only shows the completions without actually doing them.
Redraw the current line. This is done all the time automaticly so you'll almost never need to call this one explicitly.
The hash with options can be accessed with the \*(L"Attribs\*(R" method. These can be modified from the rc-file (see \*(L"\s-1FILES\s0\*(R") or can be set from the \*(C`PERL_RL\*(C' environment variable. For example to disable the \*(L"autolist\*(R" feature you can set \*(C`PERL_RL='Zoid autolist=0'\*(C' before you start the application.
( Also they can be altered interactively using the mini-buffer of the command mode, see Term::ReadLine::Zoid::ViCommand. )
If enabled lines are added to the history automaticly, subject to \*(L"MinLine\*(R". By default enabled.
If enabled the environment variables \*(C`COLUMNS\*(C' and \*(C`LINES\*(C' are kept up to date. By default enabled.
If set completions are listed directly when a completion fails, if not set you need to press \*(L"tab\*(R" twice to see a list of possible completions. By default enabled.
See \*(L"return\*(R" for a description. By default enabled.
This option can contain a \s-1CODE\s0 reference. It is called on the heartbeat event.
This option can contain a \s-1CODE\s0 reference. The default is \*(C`print "\cG"\*(C', which makes the terminal ring a bell.
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 \*(L"#\*(R". 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.
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. \s-1FIXME\s0 tell about the meta fields for advanced completion
Specifies the mode the buffer starts in when you do a \*(C`readline()\*(C', also other modes return to this mode if you exit them. The default is 'insert' which is the single-line insert mode. If you always want to edit in multiline mode set this option to 'multiline'.
Maximum number of completions to be displayed, when the number of completions is bigger the user is asked before displaying them. If set to zero completions are always displayed. If this option is set to the string 'pager' the user is asked when the number of completions is to big to fit on screen and a pager would be used.
This option controls which lines are included in the history, lines shorter then this number are ignored. When set to \*(L"0\*(R" all lines are included in the history, when set to \*(C`undef\*(C' all lines are ignored. Defaults to \*(L"0\*(R".
This option can contain the prompt to be used for extra buffer lines. It defaults to "> ". Although the \*(L"\s-1PS1\s0\*(R" prompt (as specified as an argument to the \*(C`readline()\*(C' method) can contain newlines, the \s-1PS2\s0 prompt can't.
This option can contain a string that will be shown on the right side of the screen. This is known as the \*(L"right prompt\*(R" and the idea is stolen from zsh\|(1).
Used to set the terminal title, defaults to the appname.
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.
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 \*(C`current()\*(C'. If you want to have different behaviour for different applications, try to check for \*(C`$rl->{appname}\*(C'.
# 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 ') } );
Functions specified by the Term::ReadLine documentation. Simple constructor. Arguments are the application name (used for default prompt and title string) and optional filehandles for input and output. Returns the name of the current ReadLine module actually used. 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 \*(L"!\*(R" for the history number of the current line, use \*(L"!!\*(R" for a literal \*(L"!\*(R". 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. Add a command to the history (subject to the \*(L"minline\*(R" option). If \*(L"autohistory\*(R" is set this method will be called automaticly by \*(L"readline\*(R". Returns the filehandle used for input. Returns the filehandle used for output. Sets \*(L"minline\*(R" option to $value and returns old value. \s-1TODO\s0 - what uses does this have ? Returns a reference to the options hash. 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. Simple acces to the history arry, the \*(L"set\*(R" function supports both a list and a reference, the \*(L"get\*(R" function uses \*(L"wantarray\*(R". Not sure which behaviour is compatible with T:RL::Gnu. Returns number of columns and lines on the terminal. This method can be called to continue the previous \*(C`readline()\*(C' call. Can be used to build a custom auto-mulitline feature. Returns the current T:RL::Zoid object, for use in rc files, see \*(L"\s-1FILES\s0\*(R". Bind a \s-1CODE\s0 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 \*(L"default\*(R", \*(L"command\*(R", \*(L"isearch\*(R" or \*(L"multiline\*(R". 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 (see \*(L"\s-1NOTES\s0\*(R"). Methods for use in overload classes.
Avoid using these methods from the application. Switch to input mode $mode; changes the key map and reblesses the object if the \*(C`_on_switch\*(C' key returns a class name. Reset all temporary attributes. Returns a ref with a copy of some temporary attributes. Can be used to switch between multiple edit lines in combination with \*(L"restore\*(R". Restores saved attributes. Sets history entry $int in the buffer. Returns the longest match among the completions followed by the completions itself. Used for completion functions.
\s-1FIXME\s0 minimum subroutines new mode-class
\s-1FIXME\s0 how to set up a keymap
\s-1FIXME\s0 how to add a keymap/mode
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.
\s-1UTF8\s0 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.
Line wrap doesn't always displays the last character on the line right, no functional bug though.
If the buffer size exceeds the screen size some bugs appear in the rendering.
Please mail the author if you find any other bugs.
Jaap Karssenberg || Pardus [Larus] <[email protected]>
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.
Term::ReadLine::Zoid::ViCommand, Term::ReadLine::Zoid::MultiLine, Term::ReadLine::Zoid::ISearch, Term::ReadLine::Zoid::FileBrowse, Term::ReadLine::Zoid::Base, Term::ReadLine, Env::PS1, Zoidberg