2000-07-06 00:25:26 -03:00
|
|
|
\section{\module{readline} ---
|
2000-07-06 01:51:04 -03:00
|
|
|
GNU readline interface}
|
2000-07-06 00:25:26 -03:00
|
|
|
|
|
|
|
\declaremodule{builtin}{readline}
|
2000-07-06 01:51:04 -03:00
|
|
|
\platform{Unix}
|
2000-07-06 00:25:26 -03:00
|
|
|
\sectionauthor{Skip Montanaro}{skip@mojam.com}
|
2000-10-10 14:03:45 -03:00
|
|
|
\modulesynopsis{GNU readline support for Python.}
|
2000-07-06 00:25:26 -03:00
|
|
|
|
|
|
|
|
|
|
|
The \module{readline} module defines a number of functions used either
|
2000-07-06 01:51:04 -03:00
|
|
|
directly or from the \refmodule{rlcompleter} module to facilitate
|
|
|
|
completion and history file read and write from the Python
|
|
|
|
interpreter.
|
2000-07-06 00:25:26 -03:00
|
|
|
|
|
|
|
The \module{readline} module defines the following functions:
|
|
|
|
|
2000-07-06 01:51:04 -03:00
|
|
|
|
2000-07-06 00:25:26 -03:00
|
|
|
\begin{funcdesc}{parse_and_bind}{string}
|
|
|
|
Parse and execute single line of a readline init file.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{get_line_buffer}{}
|
|
|
|
Return the current contents of the line buffer.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{insert_text}{string}
|
|
|
|
Insert text into the command line.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{read_init_file}{\optional{filename}}
|
|
|
|
Parse a readline initialization file.
|
|
|
|
The default filename is the last filename used.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{read_history_file}{\optional{filename}}
|
|
|
|
Load a readline history file.
|
2000-07-06 01:51:04 -03:00
|
|
|
The default filename is \file{\~{}/.history}.
|
2000-07-06 00:25:26 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{write_history_file}{\optional{filename}}
|
|
|
|
Save a readline history file.
|
2000-07-06 01:51:04 -03:00
|
|
|
The default filename is \file{\~{}/.history}.
|
2000-07-06 00:25:26 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2003-09-20 13:08:33 -03:00
|
|
|
\begin{funcdesc}{clear_history}{}
|
|
|
|
Clear the current history. (Note: this function is not available if
|
|
|
|
the installed version of GNU readline doesn't support it.)
|
|
|
|
\versionadded{2.4}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
2000-07-19 13:56:26 -03:00
|
|
|
\begin{funcdesc}{get_history_length}{}
|
|
|
|
Return the desired length of the history file. Negative values imply
|
|
|
|
unlimited history file size.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{set_history_length}{length}
|
|
|
|
Set the number of lines to save in the history file.
|
2000-08-09 11:37:05 -03:00
|
|
|
\function{write_history_file()} uses this value to truncate the
|
|
|
|
history file when saving. Negative values imply unlimited history
|
|
|
|
file size.
|
2000-07-19 13:56:26 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2004-05-04 16:20:22 -03:00
|
|
|
\begin{funcdesc}{get_current_history_length}{}
|
|
|
|
Return the number of lines currently in the history. (This is different
|
|
|
|
from \function{get_history_length()}, which returns the maximum number of
|
|
|
|
lines that will be written to a history file.) \versionadded{2.3}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{get_history_item}{index}
|
|
|
|
Return the current contents of history item at \var{index}.
|
|
|
|
\versionadded{2.3}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{redisplay}{}
|
|
|
|
Change what's displayed on the screen to reflect the current contents
|
|
|
|
of the line buffer. \versionadded{2.3}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
2001-09-30 18:09:59 -03:00
|
|
|
\begin{funcdesc}{set_startup_hook}{\optional{function}}
|
|
|
|
Set or remove the startup_hook function. If \var{function} is specified,
|
|
|
|
it will be used as the new startup_hook function; if omitted or
|
|
|
|
\code{None}, any hook function already installed is removed. The
|
|
|
|
startup_hook function is called with no arguments just
|
|
|
|
before readline prints the first prompt.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{set_pre_input_hook}{\optional{function}}
|
|
|
|
Set or remove the pre_input_hook function. If \var{function} is specified,
|
|
|
|
it will be used as the new pre_input_hook function; if omitted or
|
|
|
|
\code{None}, any hook function already installed is removed. The
|
|
|
|
pre_input_hook function is called with no arguments after the first prompt
|
|
|
|
has been printed and just before readline starts reading input characters.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
2000-07-06 00:25:26 -03:00
|
|
|
\begin{funcdesc}{set_completer}{\optional{function}}
|
2001-08-01 18:42:45 -03:00
|
|
|
Set or remove the completer function. If \var{function} is specified,
|
|
|
|
it will be used as the new completer function; if omitted or
|
|
|
|
\code{None}, any completer function already installed is removed. The
|
|
|
|
completer function is called as \code{\var{function}(\var{text},
|
|
|
|
\var{state})}, for \var{state} in \code{0}, \code{1}, \code{2}, ...,
|
|
|
|
until it returns a non-string value. It should return the next
|
|
|
|
possible completion starting with \var{text}.
|
2000-07-06 00:25:26 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2003-02-21 14:57:05 -04:00
|
|
|
\begin{funcdesc}{get_completer}{}
|
2003-02-21 16:11:09 -04:00
|
|
|
Get the completer function, or \code{None} if no completer function
|
|
|
|
has been set. \versionadded{2.3}
|
2003-02-21 14:57:05 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2000-07-06 00:25:26 -03:00
|
|
|
\begin{funcdesc}{get_begidx}{}
|
|
|
|
Get the beginning index of the readline tab-completion scope.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{get_endidx}{}
|
|
|
|
Get the ending index of the readline tab-completion scope.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{set_completer_delims}{string}
|
|
|
|
Set the readline word delimiters for tab-completion.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{get_completer_delims}{}
|
|
|
|
Get the readline word delimiters for tab-completion.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
2001-10-18 22:18:43 -03:00
|
|
|
\begin{funcdesc}{add_history}{line}
|
|
|
|
Append a line to the history buffer, as if it was the last line typed.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
2000-07-06 00:25:26 -03:00
|
|
|
|
2000-07-06 01:51:04 -03:00
|
|
|
\begin{seealso}
|
|
|
|
\seemodule{rlcompleter}{Completion of Python identifiers at the
|
|
|
|
interactive prompt.}
|
|
|
|
\end{seealso}
|
|
|
|
|
|
|
|
|
|
|
|
\subsection{Example \label{readline-example}}
|
|
|
|
|
|
|
|
The following example demonstrates how to use the
|
|
|
|
\module{readline} module's history reading and writing functions to
|
|
|
|
automatically load and save a history file named \file{.pyhist} from
|
|
|
|
the user's home directory. The code below would normally be executed
|
|
|
|
automatically during interactive sessions from the user's
|
|
|
|
\envvar{PYTHONSTARTUP} file.
|
2000-07-06 00:25:26 -03:00
|
|
|
|
|
|
|
\begin{verbatim}
|
2000-07-06 01:51:04 -03:00
|
|
|
import os
|
|
|
|
histfile = os.path.join(os.environ["HOME"], ".pyhist")
|
|
|
|
try:
|
|
|
|
readline.read_history_file(histfile)
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
import atexit
|
|
|
|
atexit.register(readline.write_history_file, histfile)
|
|
|
|
del os, histfile
|
2000-07-06 00:25:26 -03:00
|
|
|
\end{verbatim}
|
2004-05-23 14:46:50 -03:00
|
|
|
|
2004-05-23 16:06:41 -03:00
|
|
|
The following example extends the \class{code.InteractiveConsole} class to
|
2004-05-24 11:20:16 -03:00
|
|
|
support history save/restore.
|
2004-05-23 16:06:41 -03:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
import code
|
|
|
|
import readline
|
|
|
|
import atexit
|
|
|
|
import os
|
|
|
|
|
|
|
|
class HistoryConsole(code.InteractiveConsole):
|
|
|
|
def __init__(self, locals=None, filename="<console>",
|
|
|
|
histfile=os.path.expanduser("~/.console-history")):
|
|
|
|
code.InteractiveConsole.__init__(self)
|
|
|
|
self.init_history(histfile)
|
|
|
|
|
|
|
|
def init_history(self, histfile):
|
|
|
|
readline.parse_and_bind("tab: complete")
|
|
|
|
if hasattr(readline, "read_history_file"):
|
|
|
|
try:
|
|
|
|
readline.read_history_file(histfile)
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
atexit.register(self.save_history, histfile)
|
|
|
|
|
|
|
|
def save_history(self, histfile):
|
|
|
|
readline.write_history_file(histfile)
|
|
|
|
\end{verbatim}
|