blob: a35714449df29bab399bf2c11d12fdfb408904b2 [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}
Fred Drakef8ca7d82000-10-10 17:03:45 +00007\modulesynopsis{GNU readline support for Python.}
Skip Montanaro510ca1d2000-07-06 03:25:26 +00008
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
Skip Montanaro7cb15242000-07-19 16:56:26 +000045\begin{funcdesc}{get_history_length}{}
46Return the desired length of the history file. Negative values imply
47unlimited history file size.
48\end{funcdesc}
49
50\begin{funcdesc}{set_history_length}{length}
51Set the number of lines to save in the history file.
Fred Drake3fe9a982000-08-09 14:37:05 +000052\function{write_history_file()} uses this value to truncate the
53history file when saving. Negative values imply unlimited history
54file size.
Skip Montanaro7cb15242000-07-19 16:56:26 +000055\end{funcdesc}
56
Skip Montanaro510ca1d2000-07-06 03:25:26 +000057\begin{funcdesc}{set_completer}{\optional{function}}
Fred Drake905dc552001-08-01 21:42:45 +000058Set or remove the completer function. If \var{function} is specified,
59it will be used as the new completer function; if omitted or
60\code{None}, any completer function already installed is removed. The
61completer function is called as \code{\var{function}(\var{text},
62\var{state})}, for \var{state} in \code{0}, \code{1}, \code{2}, ...,
63until it returns a non-string value. It should return the next
64possible completion starting with \var{text}.
Skip Montanaro510ca1d2000-07-06 03:25:26 +000065\end{funcdesc}
66
67\begin{funcdesc}{get_begidx}{}
68Get the beginning index of the readline tab-completion scope.
69\end{funcdesc}
70
71\begin{funcdesc}{get_endidx}{}
72Get the ending index of the readline tab-completion scope.
73\end{funcdesc}
74
75\begin{funcdesc}{set_completer_delims}{string}
76Set the readline word delimiters for tab-completion.
77\end{funcdesc}
78
79\begin{funcdesc}{get_completer_delims}{}
80Get the readline word delimiters for tab-completion.
81\end{funcdesc}
82
Skip Montanaro510ca1d2000-07-06 03:25:26 +000083
84\begin{seealso}
Fred Drake3c62d9e2000-07-06 04:51:04 +000085 \seemodule{rlcompleter}{Completion of Python identifiers at the
86 interactive prompt.}
Skip Montanaro510ca1d2000-07-06 03:25:26 +000087\end{seealso}
Fred Drake3c62d9e2000-07-06 04:51:04 +000088
89
90\subsection{Example \label{readline-example}}
91
92The following example demonstrates how to use the
93\module{readline} module's history reading and writing functions to
94automatically load and save a history file named \file{.pyhist} from
95the user's home directory. The code below would normally be executed
96automatically during interactive sessions from the user's
97\envvar{PYTHONSTARTUP} file.
98
99\begin{verbatim}
100import os
101histfile = os.path.join(os.environ["HOME"], ".pyhist")
102try:
103 readline.read_history_file(histfile)
104except IOError:
105 pass
106import atexit
107atexit.register(readline.write_history_file, histfile)
108del os, histfile
109\end{verbatim}