blob: 1b92d5741c28e368a5e7c68ccb558228e2d4abab [file] [log] [blame]
Skip Montanaro510ca1d2000-07-06 03:25:26 +00001\section{\module{readline} ---
2 Expose GNU readline functionality to Python}
3
4\declaremodule{builtin}{readline}
5\sectionauthor{Skip Montanaro}{skip@mojam.com}
6
7\modulesynopsis{GNU Readline in Python.}
8
9\platform{UNIX}
10
11The \module{readline} module defines a number of functions used either
12directly or from the \module{rlcompleter} module to facilitate completion
13and history file read and write from the Python interpreter.
14
15The \module{readline} module defines the following functions:
16
17\begin{funcdesc}{parse_and_bind}{string}
18Parse and execute single line of a readline init file.
19\end{funcdesc}
20
21\begin{funcdesc}{get_line_buffer}{}
22Return the current contents of the line buffer.
23\end{funcdesc}
24
25\begin{funcdesc}{insert_text}{string}
26Insert text into the command line.
27\end{funcdesc}
28
29\begin{funcdesc}{read_init_file}{\optional{filename}}
30Parse a readline initialization file.
31The default filename is the last filename used.
32\end{funcdesc}
33
34\begin{funcdesc}{read_history_file}{\optional{filename}}
35Load a readline history file.
36The default filename is ~/.history.
37\end{funcdesc}
38
39\begin{funcdesc}{write_history_file}{\optional{filename}}
40Save a readline history file.
41The default filename is ~/.history.
42\end{funcdesc}
43
44\begin{funcdesc}{set_completer}{\optional{function}}
45Set or remove the completer function.
46The function is called as function(text, state),
47\code{for i in [0, 1, 2, ...]} until it returns a non-string.
48It should return the next possible completion starting with 'text'.
49\end{funcdesc}
50
51\begin{funcdesc}{get_begidx}{}
52Get the beginning index of the readline tab-completion scope.
53\end{funcdesc}
54
55\begin{funcdesc}{get_endidx}{}
56Get the ending index of the readline tab-completion scope.
57\end{funcdesc}
58
59\begin{funcdesc}{set_completer_delims}{string}
60Set the readline word delimiters for tab-completion.
61\end{funcdesc}
62
63\begin{funcdesc}{get_completer_delims}{}
64Get the readline word delimiters for tab-completion.
65\end{funcdesc}
66
67\subsection{Example}
68\nodename{Readline Example}
69
70The following example demonstrates how to use the \module{readline} module's
71history reading and writing functions to automatically load and save a
72history file named \code{.pyhist} from the user's home directory. The code
73below would normally be executed automatically during interactive sessions
74from the user's PYTHONSTARTUP file.
75
76\begin{verbatim}
77>>> import os
78>>> histfile = os.path.join(os.environ["HOME"], ".pyhist")
79>>> try:
80>>> readline.read_history_file(histfile)
81>>> except IOError:
82>>> pass
83>>> import atexit
84>>> atexit.register(readline.write_history_file, histfile)
85>>> del os, histfile
86\end{verbatim}
87
88\begin{seealso}
89 \seemodule{rlcompleter}{completion of Python identifiers}
90\end{seealso}