blob: 7c5683fea72443ec75d4f2f2bd4763ad3f412c35 [file] [log] [blame]
Skip Montanaro510ca1d2000-07-06 03:25:26 +00001\section{\module{readline} ---
Fred Drake3c62d9e2000-07-06 04:51:04 +00002 GNU readline interface}
Skip Montanaro510ca1d2000-07-06 03:25:26 +00003
4\declaremodule{builtin}{readline}
Fred Drake3c62d9e2000-07-06 04:51:04 +00005 \platform{Unix}
Skip Montanaro510ca1d2000-07-06 03:25:26 +00006\sectionauthor{Skip Montanaro}{skip@mojam.com}
Skip Montanaro510ca1d2000-07-06 03:25:26 +00007\modulesynopsis{GNU Readline in Python.}
8
Skip Montanaro510ca1d2000-07-06 03:25:26 +00009
10The \module{readline} module defines a number of functions used either
Fred Drake3c62d9e2000-07-06 04:51:04 +000011directly or from the \refmodule{rlcompleter} module to facilitate
12completion and history file read and write from the Python
13interpreter.
Skip Montanaro510ca1d2000-07-06 03:25:26 +000014
15The \module{readline} module defines the following functions:
16
Fred Drake3c62d9e2000-07-06 04:51:04 +000017
Skip Montanaro510ca1d2000-07-06 03:25:26 +000018\begin{funcdesc}{parse_and_bind}{string}
19Parse and execute single line of a readline init file.
20\end{funcdesc}
21
22\begin{funcdesc}{get_line_buffer}{}
23Return the current contents of the line buffer.
24\end{funcdesc}
25
26\begin{funcdesc}{insert_text}{string}
27Insert text into the command line.
28\end{funcdesc}
29
30\begin{funcdesc}{read_init_file}{\optional{filename}}
31Parse a readline initialization file.
32The default filename is the last filename used.
33\end{funcdesc}
34
35\begin{funcdesc}{read_history_file}{\optional{filename}}
36Load a readline history file.
Fred Drake3c62d9e2000-07-06 04:51:04 +000037The default filename is \file{\~{}/.history}.
Skip Montanaro510ca1d2000-07-06 03:25:26 +000038\end{funcdesc}
39
40\begin{funcdesc}{write_history_file}{\optional{filename}}
41Save a readline history file.
Fred Drake3c62d9e2000-07-06 04:51:04 +000042The default filename is \file{\~{}/.history}.
Skip Montanaro510ca1d2000-07-06 03:25:26 +000043\end{funcdesc}
44
45\begin{funcdesc}{set_completer}{\optional{function}}
Fred Drake3c62d9e2000-07-06 04:51:04 +000046Set or remove the completer function. The completer function is
47called as \code{\var{function}(\var{text}, \var{state})},
Skip Montanaro510ca1d2000-07-06 03:25:26 +000048\code{for i in [0, 1, 2, ...]} until it returns a non-string.
Fred Drake3c62d9e2000-07-06 04:51:04 +000049It should return the next possible completion starting with \var{text}.
Skip Montanaro510ca1d2000-07-06 03:25:26 +000050\end{funcdesc}
51
52\begin{funcdesc}{get_begidx}{}
53Get the beginning index of the readline tab-completion scope.
54\end{funcdesc}
55
56\begin{funcdesc}{get_endidx}{}
57Get the ending index of the readline tab-completion scope.
58\end{funcdesc}
59
60\begin{funcdesc}{set_completer_delims}{string}
61Set the readline word delimiters for tab-completion.
62\end{funcdesc}
63
64\begin{funcdesc}{get_completer_delims}{}
65Get the readline word delimiters for tab-completion.
66\end{funcdesc}
67
Skip Montanaro510ca1d2000-07-06 03:25:26 +000068
69\begin{seealso}
Fred Drake3c62d9e2000-07-06 04:51:04 +000070 \seemodule{rlcompleter}{Completion of Python identifiers at the
71 interactive prompt.}
Skip Montanaro510ca1d2000-07-06 03:25:26 +000072\end{seealso}
Fred Drake3c62d9e2000-07-06 04:51:04 +000073
74
75\subsection{Example \label{readline-example}}
76
77The following example demonstrates how to use the
78\module{readline} module's history reading and writing functions to
79automatically load and save a history file named \file{.pyhist} from
80the user's home directory. The code below would normally be executed
81automatically during interactive sessions from the user's
82\envvar{PYTHONSTARTUP} file.
83
84\begin{verbatim}
85import os
86histfile = os.path.join(os.environ["HOME"], ".pyhist")
87try:
88 readline.read_history_file(histfile)
89except IOError:
90 pass
91import atexit
92atexit.register(readline.write_history_file, histfile)
93del os, histfile
94\end{verbatim}