blob: ab4a099e8696f76020d9a63c7e1fc1aac6a0cb19 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`readline` --- GNU readline interface
2==========================================
3
4.. module:: readline
5 :platform: Unix
6 :synopsis: GNU readline support for Python.
Christian Heimes895627f2007-12-08 17:28:33 +00007.. sectionauthor:: Skip Montanaro <skip@pobox.com>
Georg Brandl116aa622007-08-15 14:28:22 +00008
9
10The :mod:`readline` module defines a number of functions to facilitate
11completion and reading/writing of history files from the Python interpreter.
12This module can be used directly or via the :mod:`rlcompleter` module. Settings
13made using this module affect the behaviour of both the interpreter's
Georg Brandl96593ed2007-09-07 14:15:41 +000014interactive prompt and the prompts offered by the built-in :func:`input`
15function.
Georg Brandl116aa622007-08-15 14:28:22 +000016
Ronald Oussoren2efd9242009-09-20 14:53:22 +000017..note::
18
19 On MacOS X the :mod:`readline` module can be implemented using
20 the ``libedit`` library instead of GNU readline.
21
22 The configuration file for ``libedit`` is different from that
23 of GNU readline. If you programmaticly load configuration strings
24 you can check for the text "libedit" in :const:`readline.__doc__`
25 to differentiate between GNU readline and libedit.
26
27
Georg Brandl116aa622007-08-15 14:28:22 +000028The :mod:`readline` module defines the following functions:
29
30
31.. function:: parse_and_bind(string)
32
33 Parse and execute single line of a readline init file.
34
35
36.. function:: get_line_buffer()
37
38 Return the current contents of the line buffer.
39
40
41.. function:: insert_text(string)
42
43 Insert text into the command line.
44
45
46.. function:: read_init_file([filename])
47
48 Parse a readline initialization file. The default filename is the last filename
49 used.
50
51
52.. function:: read_history_file([filename])
53
54 Load a readline history file. The default filename is :file:`~/.history`.
55
56
57.. function:: write_history_file([filename])
58
59 Save a readline history file. The default filename is :file:`~/.history`.
60
61
62.. function:: clear_history()
63
64 Clear the current history. (Note: this function is not available if the
65 installed version of GNU readline doesn't support it.)
66
Georg Brandl116aa622007-08-15 14:28:22 +000067
68.. function:: get_history_length()
69
70 Return the desired length of the history file. Negative values imply unlimited
71 history file size.
72
73
74.. function:: set_history_length(length)
75
76 Set the number of lines to save in the history file. :func:`write_history_file`
77 uses this value to truncate the history file when saving. Negative values imply
78 unlimited history file size.
79
80
81.. function:: get_current_history_length()
82
83 Return the number of lines currently in the history. (This is different from
84 :func:`get_history_length`, which returns the maximum number of lines that will
85 be written to a history file.)
86
Georg Brandl116aa622007-08-15 14:28:22 +000087
88.. function:: get_history_item(index)
89
90 Return the current contents of history item at *index*.
91
Georg Brandl116aa622007-08-15 14:28:22 +000092
93.. function:: remove_history_item(pos)
94
95 Remove history item specified by its position from the history.
96
Georg Brandl116aa622007-08-15 14:28:22 +000097
98.. function:: replace_history_item(pos, line)
99
100 Replace history item specified by its position with the given line.
101
Georg Brandl116aa622007-08-15 14:28:22 +0000102
103.. function:: redisplay()
104
105 Change what's displayed on the screen to reflect the current contents of the
106 line buffer.
107
Georg Brandl116aa622007-08-15 14:28:22 +0000108
109.. function:: set_startup_hook([function])
110
111 Set or remove the startup_hook function. If *function* is specified, it will be
112 used as the new startup_hook function; if omitted or ``None``, any hook function
113 already installed is removed. The startup_hook function is called with no
114 arguments just before readline prints the first prompt.
115
116
117.. function:: set_pre_input_hook([function])
118
119 Set or remove the pre_input_hook function. If *function* is specified, it will
120 be used as the new pre_input_hook function; if omitted or ``None``, any hook
121 function already installed is removed. The pre_input_hook function is called
122 with no arguments after the first prompt has been printed and just before
123 readline starts reading input characters.
124
125
126.. function:: set_completer([function])
127
128 Set or remove the completer function. If *function* is specified, it will be
129 used as the new completer function; if omitted or ``None``, any completer
130 function already installed is removed. The completer function is called as
131 ``function(text, state)``, for *state* in ``0``, ``1``, ``2``, ..., until it
132 returns a non-string value. It should return the next possible completion
133 starting with *text*.
134
135
136.. function:: get_completer()
137
138 Get the completer function, or ``None`` if no completer function has been set.
139
Georg Brandl116aa622007-08-15 14:28:22 +0000140
Thomas Wouters89d996e2007-09-08 17:39:28 +0000141.. function:: get_completion_type()
142
143 Get the type of completion being attempted.
144
Thomas Wouters89d996e2007-09-08 17:39:28 +0000145
Georg Brandl116aa622007-08-15 14:28:22 +0000146.. function:: get_begidx()
147
148 Get the beginning index of the readline tab-completion scope.
149
150
151.. function:: get_endidx()
152
153 Get the ending index of the readline tab-completion scope.
154
155
156.. function:: set_completer_delims(string)
157
158 Set the readline word delimiters for tab-completion.
159
160
161.. function:: get_completer_delims()
162
163 Get the readline word delimiters for tab-completion.
164
Georg Brandl6554cb92007-12-02 23:15:43 +0000165
Thomas Wouters89d996e2007-09-08 17:39:28 +0000166.. function:: set_completion_display_matches_hook([function])
167
168 Set or remove the completion display function. If *function* is
169 specified, it will be used as the new completion display function;
170 if omitted or ``None``, any completion display function already
171 installed is removed. The completion display function is called as
172 ``function(substitution, [matches], longest_match_length)`` once
173 each time matches need to be displayed.
174
Georg Brandl116aa622007-08-15 14:28:22 +0000175
176.. function:: add_history(line)
177
178 Append a line to the history buffer, as if it was the last line typed.
179
Georg Brandl116aa622007-08-15 14:28:22 +0000180.. seealso::
181
182 Module :mod:`rlcompleter`
183 Completion of Python identifiers at the interactive prompt.
184
185
186.. _readline-example:
187
188Example
189-------
190
191The following example demonstrates how to use the :mod:`readline` module's
192history reading and writing functions to automatically load and save a history
193file named :file:`.pyhist` from the user's home directory. The code below would
194normally be executed automatically during interactive sessions from the user's
195:envvar:`PYTHONSTARTUP` file. ::
196
197 import os
198 histfile = os.path.join(os.environ["HOME"], ".pyhist")
199 try:
200 readline.read_history_file(histfile)
201 except IOError:
202 pass
203 import atexit
204 atexit.register(readline.write_history_file, histfile)
205 del os, histfile
206
207The following example extends the :class:`code.InteractiveConsole` class to
208support history save/restore. ::
209
210 import code
211 import readline
212 import atexit
213 import os
214
215 class HistoryConsole(code.InteractiveConsole):
216 def __init__(self, locals=None, filename="<console>",
217 histfile=os.path.expanduser("~/.console-history")):
Georg Brandlee8783d2009-09-16 16:00:31 +0000218 code.InteractiveConsole.__init__(self, locals, filename)
Georg Brandl116aa622007-08-15 14:28:22 +0000219 self.init_history(histfile)
220
221 def init_history(self, histfile):
222 readline.parse_and_bind("tab: complete")
223 if hasattr(readline, "read_history_file"):
224 try:
225 readline.read_history_file(histfile)
226 except IOError:
227 pass
228 atexit.register(self.save_history, histfile)
229
230 def save_history(self, histfile):
231 readline.write_history_file(histfile)
232