blob: ac8e23f08c5a133ebae8db61179252d652a08d14 [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
Martin v. Löwise7a97962003-09-20 16:08:33 +000045\begin{funcdesc}{clear_history}{}
46Clear the current history. (Note: this function is not available if
47the installed version of GNU readline doesn't support it.)
48\versionadded{2.4}
49\end{funcdesc}
50
Skip Montanaro7cb15242000-07-19 16:56:26 +000051\begin{funcdesc}{get_history_length}{}
52Return the desired length of the history file. Negative values imply
53unlimited history file size.
54\end{funcdesc}
55
56\begin{funcdesc}{set_history_length}{length}
57Set the number of lines to save in the history file.
Fred Drake3fe9a982000-08-09 14:37:05 +000058\function{write_history_file()} uses this value to truncate the
59history file when saving. Negative values imply unlimited history
60file size.
Skip Montanaro7cb15242000-07-19 16:56:26 +000061\end{funcdesc}
62
Phillip J. Eby5068c872004-05-04 19:20:22 +000063\begin{funcdesc}{get_current_history_length}{}
64Return the number of lines currently in the history. (This is different
65from \function{get_history_length()}, which returns the maximum number of
66lines that will be written to a history file.) \versionadded{2.3}
67\end{funcdesc}
68
69\begin{funcdesc}{get_history_item}{index}
70Return the current contents of history item at \var{index}.
71\versionadded{2.3}
72\end{funcdesc}
73
Skip Montanaroe5069012004-08-15 14:32:06 +000074\begin{funcdesc}{remove_history_item}{pos}
75Remove history item specified by its position from the history.
76\versionadded{2.4}
77\end{funcdesc}
78
79\begin{funcdesc}{replace_history_item}{pos, line}
80Replace history item specified by its position with the given line.
81\versionadded{2.4}
82\end{funcdesc}
83
Phillip J. Eby5068c872004-05-04 19:20:22 +000084\begin{funcdesc}{redisplay}{}
85Change what's displayed on the screen to reflect the current contents
86of the line buffer. \versionadded{2.3}
87\end{funcdesc}
88
Martin v. Löwis0daad592001-09-30 21:09:59 +000089\begin{funcdesc}{set_startup_hook}{\optional{function}}
90Set or remove the startup_hook function. If \var{function} is specified,
91it will be used as the new startup_hook function; if omitted or
92\code{None}, any hook function already installed is removed. The
93startup_hook function is called with no arguments just
94before readline prints the first prompt.
95\end{funcdesc}
96
97\begin{funcdesc}{set_pre_input_hook}{\optional{function}}
98Set or remove the pre_input_hook function. If \var{function} is specified,
99it will be used as the new pre_input_hook function; if omitted or
100\code{None}, any hook function already installed is removed. The
101pre_input_hook function is called with no arguments after the first prompt
102has been printed and just before readline starts reading input characters.
103\end{funcdesc}
104
Skip Montanaro510ca1d2000-07-06 03:25:26 +0000105\begin{funcdesc}{set_completer}{\optional{function}}
Fred Drake905dc552001-08-01 21:42:45 +0000106Set or remove the completer function. If \var{function} is specified,
107it will be used as the new completer function; if omitted or
108\code{None}, any completer function already installed is removed. The
109completer function is called as \code{\var{function}(\var{text},
110\var{state})}, for \var{state} in \code{0}, \code{1}, \code{2}, ...,
111until it returns a non-string value. It should return the next
112possible completion starting with \var{text}.
Skip Montanaro510ca1d2000-07-06 03:25:26 +0000113\end{funcdesc}
114
Neal Norwitzb7d1d3c2003-02-21 18:57:05 +0000115\begin{funcdesc}{get_completer}{}
Michael W. Hudsonf5dd7532003-02-21 20:11:09 +0000116Get the completer function, or \code{None} if no completer function
117has been set. \versionadded{2.3}
Neal Norwitzb7d1d3c2003-02-21 18:57:05 +0000118\end{funcdesc}
119
Skip Montanaro510ca1d2000-07-06 03:25:26 +0000120\begin{funcdesc}{get_begidx}{}
121Get the beginning index of the readline tab-completion scope.
122\end{funcdesc}
123
124\begin{funcdesc}{get_endidx}{}
125Get the ending index of the readline tab-completion scope.
126\end{funcdesc}
127
128\begin{funcdesc}{set_completer_delims}{string}
129Set the readline word delimiters for tab-completion.
130\end{funcdesc}
131
132\begin{funcdesc}{get_completer_delims}{}
133Get the readline word delimiters for tab-completion.
134\end{funcdesc}
135
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000136\begin{funcdesc}{add_history}{line}
137Append a line to the history buffer, as if it was the last line typed.
138\end{funcdesc}
139
Skip Montanaro510ca1d2000-07-06 03:25:26 +0000140\begin{seealso}
Fred Drake3c62d9e2000-07-06 04:51:04 +0000141 \seemodule{rlcompleter}{Completion of Python identifiers at the
142 interactive prompt.}
Skip Montanaro510ca1d2000-07-06 03:25:26 +0000143\end{seealso}
Fred Drake3c62d9e2000-07-06 04:51:04 +0000144
145
146\subsection{Example \label{readline-example}}
147
148The following example demonstrates how to use the
149\module{readline} module's history reading and writing functions to
150automatically load and save a history file named \file{.pyhist} from
151the user's home directory. The code below would normally be executed
152automatically during interactive sessions from the user's
153\envvar{PYTHONSTARTUP} file.
154
155\begin{verbatim}
156import os
157histfile = os.path.join(os.environ["HOME"], ".pyhist")
158try:
159 readline.read_history_file(histfile)
160except IOError:
161 pass
162import atexit
163atexit.register(readline.write_history_file, histfile)
164del os, histfile
165\end{verbatim}
Skip Montanaro0dc23102004-05-23 17:46:50 +0000166
Skip Montanarob98a8ba2004-05-23 19:06:41 +0000167The following example extends the \class{code.InteractiveConsole} class to
Skip Montanaro79cddc52004-05-24 14:20:16 +0000168support history save/restore.
Skip Montanarob98a8ba2004-05-23 19:06:41 +0000169
170\begin{verbatim}
171import code
172import readline
173import atexit
174import os
175
176class HistoryConsole(code.InteractiveConsole):
177 def __init__(self, locals=None, filename="<console>",
178 histfile=os.path.expanduser("~/.console-history")):
179 code.InteractiveConsole.__init__(self)
180 self.init_history(histfile)
181
182 def init_history(self, histfile):
183 readline.parse_and_bind("tab: complete")
184 if hasattr(readline, "read_history_file"):
185 try:
186 readline.read_history_file(histfile)
187 except IOError:
188 pass
189 atexit.register(self.save_history, histfile)
190
Skip Montanarob98a8ba2004-05-23 19:06:41 +0000191 def save_history(self, histfile):
192 readline.write_history_file(histfile)
193\end{verbatim}