source: vendor/python/2.5/Doc/lib/libreadline.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 6.5 KB
Line 
1\section{\module{readline} ---
2 GNU readline interface}
3
4\declaremodule{builtin}{readline}
5 \platform{Unix}
6\sectionauthor{Skip Montanaro}{skip@mojam.com}
7\modulesynopsis{GNU readline support for Python.}
8
9
10The \module{readline} module defines a number of functions to
11facilitate completion and reading/writing of history files from the
12Python interpreter. This module can be used directly or via the
13\refmodule{rlcompleter} module. Settings made using
14this module affect the behaviour of both the interpreter's interactive prompt
15and the prompts offered by the \function{raw_input()} and \function{input()}
16built-in functions.
17
18The \module{readline} module defines the following functions:
19
20
21\begin{funcdesc}{parse_and_bind}{string}
22Parse and execute single line of a readline init file.
23\end{funcdesc}
24
25\begin{funcdesc}{get_line_buffer}{}
26Return the current contents of the line buffer.
27\end{funcdesc}
28
29\begin{funcdesc}{insert_text}{string}
30Insert text into the command line.
31\end{funcdesc}
32
33\begin{funcdesc}{read_init_file}{\optional{filename}}
34Parse a readline initialization file.
35The default filename is the last filename used.
36\end{funcdesc}
37
38\begin{funcdesc}{read_history_file}{\optional{filename}}
39Load a readline history file.
40The default filename is \file{\~{}/.history}.
41\end{funcdesc}
42
43\begin{funcdesc}{write_history_file}{\optional{filename}}
44Save a readline history file.
45The default filename is \file{\~{}/.history}.
46\end{funcdesc}
47
48\begin{funcdesc}{clear_history}{}
49Clear the current history. (Note: this function is not available if
50the installed version of GNU readline doesn't support it.)
51\versionadded{2.4}
52\end{funcdesc}
53
54\begin{funcdesc}{get_history_length}{}
55Return the desired length of the history file. Negative values imply
56unlimited history file size.
57\end{funcdesc}
58
59\begin{funcdesc}{set_history_length}{length}
60Set the number of lines to save in the history file.
61\function{write_history_file()} uses this value to truncate the
62history file when saving. Negative values imply unlimited history
63file size.
64\end{funcdesc}
65
66\begin{funcdesc}{get_current_history_length}{}
67Return the number of lines currently in the history. (This is different
68from \function{get_history_length()}, which returns the maximum number of
69lines that will be written to a history file.) \versionadded{2.3}
70\end{funcdesc}
71
72\begin{funcdesc}{get_history_item}{index}
73Return the current contents of history item at \var{index}.
74\versionadded{2.3}
75\end{funcdesc}
76
77\begin{funcdesc}{remove_history_item}{pos}
78Remove history item specified by its position from the history.
79\versionadded{2.4}
80\end{funcdesc}
81
82\begin{funcdesc}{replace_history_item}{pos, line}
83Replace history item specified by its position with the given line.
84\versionadded{2.4}
85\end{funcdesc}
86
87\begin{funcdesc}{redisplay}{}
88Change what's displayed on the screen to reflect the current contents
89of the line buffer. \versionadded{2.3}
90\end{funcdesc}
91
92\begin{funcdesc}{set_startup_hook}{\optional{function}}
93Set or remove the startup_hook function. If \var{function} is specified,
94it will be used as the new startup_hook function; if omitted or
95\code{None}, any hook function already installed is removed. The
96startup_hook function is called with no arguments just
97before readline prints the first prompt.
98\end{funcdesc}
99
100\begin{funcdesc}{set_pre_input_hook}{\optional{function}}
101Set or remove the pre_input_hook function. If \var{function} is specified,
102it will be used as the new pre_input_hook function; if omitted or
103\code{None}, any hook function already installed is removed. The
104pre_input_hook function is called with no arguments after the first prompt
105has been printed and just before readline starts reading input characters.
106\end{funcdesc}
107
108\begin{funcdesc}{set_completer}{\optional{function}}
109Set or remove the completer function. If \var{function} is specified,
110it will be used as the new completer function; if omitted or
111\code{None}, any completer function already installed is removed. The
112completer function is called as \code{\var{function}(\var{text},
113\var{state})}, for \var{state} in \code{0}, \code{1}, \code{2}, ...,
114until it returns a non-string value. It should return the next
115possible completion starting with \var{text}.
116\end{funcdesc}
117
118\begin{funcdesc}{get_completer}{}
119Get the completer function, or \code{None} if no completer function
120has been set. \versionadded{2.3}
121\end{funcdesc}
122
123\begin{funcdesc}{get_begidx}{}
124Get the beginning index of the readline tab-completion scope.
125\end{funcdesc}
126
127\begin{funcdesc}{get_endidx}{}
128Get the ending index of the readline tab-completion scope.
129\end{funcdesc}
130
131\begin{funcdesc}{set_completer_delims}{string}
132Set the readline word delimiters for tab-completion.
133\end{funcdesc}
134
135\begin{funcdesc}{get_completer_delims}{}
136Get the readline word delimiters for tab-completion.
137\end{funcdesc}
138
139\begin{funcdesc}{add_history}{line}
140Append a line to the history buffer, as if it was the last line typed.
141\end{funcdesc}
142
143\begin{seealso}
144 \seemodule{rlcompleter}{Completion of Python identifiers at the
145 interactive prompt.}
146\end{seealso}
147
148
149\subsection{Example \label{readline-example}}
150
151The following example demonstrates how to use the
152\module{readline} module's history reading and writing functions to
153automatically load and save a history file named \file{.pyhist} from
154the user's home directory. The code below would normally be executed
155automatically during interactive sessions from the user's
156\envvar{PYTHONSTARTUP} file.
157
158\begin{verbatim}
159import os
160histfile = os.path.join(os.environ["HOME"], ".pyhist")
161try:
162 readline.read_history_file(histfile)
163except IOError:
164 pass
165import atexit
166atexit.register(readline.write_history_file, histfile)
167del os, histfile
168\end{verbatim}
169
170The following example extends the \class{code.InteractiveConsole} class to
171support history save/restore.
172
173\begin{verbatim}
174import code
175import readline
176import atexit
177import os
178
179class HistoryConsole(code.InteractiveConsole):
180 def __init__(self, locals=None, filename="<console>",
181 histfile=os.path.expanduser("~/.console-history")):
182 code.InteractiveConsole.__init__(self)
183 self.init_history(histfile)
184
185 def init_history(self, histfile):
186 readline.parse_and_bind("tab: complete")
187 if hasattr(readline, "read_history_file"):
188 try:
189 readline.read_history_file(histfile)
190 except IOError:
191 pass
192 atexit.register(self.save_history, histfile)
193
194 def save_history(self, histfile):
195 readline.write_history_file(histfile)
196\end{verbatim}
Note: See TracBrowser for help on using the repository browser.