readline
- a simple interface
to the readline and history libraries
local RL = require 'readline' RL.set_options{ keeplines=1000, histfile='~/.synopsis_history' } RL.set_readline_name('fennel') -- the Standard Interface local str = RL.readline('Please enter some filename: ') local save_options = RL.set_options{ completion=false } str = RL.readline('Please type a line which can include Tabs: ') RL.set_options(save_options) str = RL.readline('Now tab-filename-completion is working again: ') ... -- the Alternate Interface local poll = require 'posix.poll'.poll local line = nil local linehandler = function (str) RL.add_history(str) RL.handler_remove() line = str end RL.handler_install("prompt> ", linehandler) local fds = {[0] = {events={IN={true}}}} while true do poll(fds, -1) if fds[0].revents.IN then RL.read_char() -- only if there's something to be read else -- do some useful background task end if line then break end end print("got line: " .. line) -- Custom Completion local reserved_words = { 'and', 'assert', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function', 'if', 'ipairs', 'local', 'nil', 'not', 'pairs', 'print', 'require', 'return', 'then', 'tonumber', 'tostring', 'true', 'type', 'while', } RL.set_complete_list(reserved_words) line = RL.readline('now it expands lua reserved words: ') ... RL.save_history() ; os.exit()
This Lua module offers a simple calling interface to the GNU Readline/History Library.
The function readline() is a wrapper, which invokes the GNU readline, adds the line to the end of the History List, and then returns the line. Usually you call save_history() before the program exits, so that the History List is saved to the histfile.
Various options can be changed using the set_options{} function.
The user can configure the GNU Readline (e.g. vi or emacs keystrokes ?) with their individual ~/.inputrc file, see the INITIALIZATION FILE section of man readline.
By default, the GNU readline library dialogues with the user by reading from stdin and writing to stdout; this fits very badly with applications that want to use stdin and stdout to input and output data. Therefore, this Lua module dialogues with the user on the controlling-terminal of the process (typically /dev/tty) as returned by ctermid().
Most of
readline's Alternate Interface is now included, namely
handler_install,
read_char
and
handler_remove.
Some applications need to interleave keyboard I/O with file, device,
or window system I/O, typically by using a main loop to select() on
various file descriptors.
To accommodate this need, readline
can also be invoked as a 'callback' function from an event loop,
and the Alternate Interface offers functions to do this.
The Alternate Interface does offer tab-completion; but it does not
add to the history file, so you will probably want to call
RL.add_history(s)
explicitly.
See handler_install()
Access to readline's Custom Completion is now provided.
This module does not work with lua -i
because that runs its own readline,
and the two conflict with each other.
Returns the old options, so they can be restored later.
The auto_add option controls whether the line entered will be
added to the History List,
The default options are:
auto_add = true,
histfile = '~/.rl_lua_history',
keeplines = 500,
completion = true,
ignoredups = true,
minlength = 2,
Lines shorter than the minlength option will not be put on the
History List. Tilde expansion is performed on the histfile option.
The histfile option must be a string, so don't set it to nil.
If you want to avoid reading or writing your History List to the filesystem,
set histfile to the empty string.
So if you want no history behaviour (Up or Down arrows etc.) at all, then set
set_options{ histfile='', auto_add=false, }
Sets the internal libreadline variable rl_readline_name
for use with conditional directives in .inputrc
(see the
manual).
It should be
invoked once, before calling readline()
,
so that the name is set before .inputrc
is sourced.
For example: if, in the initialization before first readline prompt, you
RL.set_readline_name('fennel')
then the call to readline()
would execute this conditional in .inputrc
$if fennel
set blink-matching-paren On
set enable-bracketed-paste On
$endif
Displays the prompt and returns the text of the line the user enters. A blank line returns the empty string. If EOF is encountered while reading a line, and the line is empty, nil is returned; if an EOF is read with a non-empty line, it is treated as a newline.
If the auto_add option is true (which is the default), the line the user enters will be added to the History List, unless it's shorter than minlength, or it's the same as the previous line and the ignoredups option is set.
Normally, you should call this function once, just before your program exits. It saves the lines the user has entered onto the end of the histfile file. Then if necessary it truncates lines off the beginning of the histfile to confine it to keeplines long.
Adds the line to the History List.
With the Standard Interface, you'll only need this function
if you want to assume complete control over the strings that get added,
in which case you set:
RL.set_options{ auto_add=false, }
and then after calling readline(prompt)
you can process the line as you wish
and call add_history(line) if appropriate.
But with the Alternative Interface, you have to call
add_history(line) yourself, even if {auto_add=true}
You should do this in the linehandler function, see below.
Some applications need to interleave keyboard I/O with file, device,
or window system I/O, by using a main loop to select() on
various file descriptors.
With the Alernate Interface, readline can
be invoked as a 'callback' function from an event loop.
The Alternate Interface does not add to the history file,
so you will probably want to call
RL.add_history(s) explicitly.
You should do this within the linehandler function !
(This constraint is due to what may be an unadvertised quirk
of libreadline.)
This function sets up the terminal, installs a linehandler function
that will receive the text of the line as an argument,
and displays the string prompt.
A typical linehandler function might be:
linehandler = function (str)
RL.add_history(str)
RL.handler_remove()
line = str -- line is a global, or an upvalue
end
Whenever an application determines that keyboard input is available,
it should call read_char(),
which will read the next character from the current input source.
If that character completes the line, read_char will invoke the
linehandler function installed by handler_install to process the line.
Before calling the linehandler function, the terminal settings are reset to
the values they had before calling handler_install.
If the linehandler function returns, and the line handler remains installed,
the terminal settings are modified for Readline's use again.
EOF is indicated by calling the linehandler handler with a nil line.
Restore the terminal to its initial state and remove the line handler. You may call this function from within the linehandler as well as independently. If the linehandler function does not exit the program, this function should be called before the program exits to reset the terminal settings.
This function sets up custom completion of an array of strings.
For example, the array_of_strings
might be the dictionary-words of a language,
or the reserved words of a programming language.
This is the lower-level function on which
set_complete_list() is based.
Its argument is a function which takes three arguments:
the text of the line as it stands,
and the indexes from and to,
which delimit the segment of the text (for example, the word)
which is to be completed.
This syntax is the same as string.sub(text, from, to)
The completer_function must return an array of the possible
completions.
For example, the completer_function of
set_complete_list() is:
local completer_function = function(text, from, to) local incomplete = string.sub(text, from, to) local matches = {} for i,v in ipairs(array_of_strings) do if incomplete == string.sub(v, 1, #incomplete) then matches[1 + #matches] = v end end return matches endbut the completer_function can also have more complex behaviour. Because it knows the contents of the line so far, it could ask for a date in format 18 Aug 2018 and offer three different completions for the three different fields.
By default, after every completion readline appends a space
to the string, so you can start the next word.
You can change this space to another character by calling
set_completion_append_character(s),
which sets the append_character
to the first byte of the string s.
For example, this sets it to the empty string:
RL.set_completion_append_character('')
It only makes sense to call set_completion_append_character
from within a completer_function.
After the completer_function has executed,
the readline library resets the append_character to the default space.
Setting the append_character to
','
or
':'
or
'.'
or
'-'
may not behave as you expect when trying to tab-complete the
following word, because readline treats those characters
as being part of a 'word', not as a delimiter between words.
This module is available as a LuaRock in
luarocks.org/modules/peterbillam
so you should be able to install it with the command:
$ su
Password:
# luarocks install readline
or:
# luarocks install https://pjb.com.au/comp/lua/readline-3.2-0.rockspec
If this results in an error message such as:
Error: Could not find expected file libreadline.a, or libreadline.so,
then you need to find the appropriate directory with:
find /usr/lib -name 'libreadline.*' -print
and then invoke:
luarocks install readline\
READLINE_INCDIR=/usr/local/Cellar/readline/8.1/include \
READLINE_LIBDIR=/usr/local/Cellar/readline/8.1/lib \
HISTORY_INCDIR=/usr/local/Cellar/readline/8.1/include \
HISTORY_LIBDIR=/usr/local/Cellar/readline/8.1/lib # or wherever
accordingly.
It depends on the readline library and its header-files;
for example on Debian you may need:
# aptitude install libreadline6 libreadline6-dev
or on Centos you may need:
# yum install readline-devel
You can see the source-code in:
https://pjb.com.au/comp/lua/readline-3.2.tar.gz
20221001 3.2 fix a memory leak in readline() 20220420 3.1 reset OldHistoryLength if histfile gets set 20210418 3.0 pass READLINE_INCDIR and READLINE_LIBDIR to gcc 20210127 2.9 fix version number again 20210106 2.8 include string.h 20200801 2.7 add lua 5.4 20200409 2.6 jaawerth: added set_readline_name() 20190110 2.5 fix a lua_rawlen v. lua_objlen bug if using lua 5.1 20180924 2.2 add set_completion_append_character 20180912 2.1 C code stack-bug fix in handler_calls_completion_callback 20180910 2.0 add set_complete_list and set_complete_function 20180827 1.9 add handler_install read_char and handler_remove 20151020 1.8 readline() returns nil correctly on EOF 20150421 1.7 include lua5.3, and move pod and doc back to luarocks.org 20150416 1.6 readline specified as an external dependency 20140608 1.5 switch pod and doc over to using moonrocks 20140519 1.4 installs as readline not Readline under luarocks 2.1.2 20131031 1.3 readline erases final space if tab-completion is used 20131020 1.2 set_options{histfile='~/d'} expands the tilde 20130921 1.1 uses ctermid() (usually /dev/tty) to dialogue with the user 20130918 1.0 first working version
Peter Billam
https://pjb.com.au/comp/contact.html
Alexander Adler, University of Frankfurt, contributed the
Alternate Interface and
Custom Completion
Jesse Wertheim, one of the developers of
Fennel,
contributed the
set_readline_name() function,
wenxichang fixed the memory leak in
readline().
man readline
http://www.gnu.org/s/readline
https://tiswww.case.edu/php/chet/readline/readline.html
https://tiswww.case.edu/php/chet/readline/readline.html#SEC28
Readline Variables
https://tiswww.case.edu/php/chet/readline/readline.html#SEC41
https://tiswww.case.edu/php/chet/readline/readline.html#SEC45
/usr/share/readline/inputrc
~/.inputrc
http://lua-users.org/wiki/CompleteWithReadline
http://luaposix.github.io/luaposix
fennel-lang.org/
terminfo.lua
https://pjb.com.au
https://pjb.com.au/comp/index.html#lua