9
9
10
10
The :mod: `readline ` module defines a number of functions to facilitate
11
11
completion and reading/writing of history files from the Python interpreter.
12
- This module can be used directly or via the :mod: `rlcompleter ` module. Settings
12
+ This module can be used directly, or via the :mod: `rlcompleter ` module, which
13
+ supports completion of Python identifiers at the interactive prompt. Settings
13
14
made using this module affect the behaviour of both the interpreter's
14
15
interactive prompt and the prompts offered by the built-in :func: `input `
15
16
function.
16
17
17
18
.. note ::
18
19
19
- On MacOS X the :mod: ` readline ` module can be implemented using
20
+ The underlying Readline library API may be implemented by
20
21
the ``libedit `` library instead of GNU readline.
22
+ On MacOS X the :mod: `readline ` module detects which library is being used
23
+ at run time.
21
24
22
25
The configuration file for ``libedit `` is different from that
23
26
of GNU readline. If you programmatically load configuration strings
24
27
you can check for the text "libedit" in :const: `readline.__doc__ `
25
28
to differentiate between GNU readline and libedit.
26
29
27
30
28
- The :mod: `readline ` module defines the following functions:
31
+ Init file
32
+ ---------
33
+
34
+ The following functions relate to the init file and user configuration:
29
35
30
36
31
37
.. function :: parse_and_bind(string)
32
38
33
- Parse and execute single line of a readline init file.
39
+ Execute the init line provided in the *string * argument. This calls
40
+ :c:func: `rl_parse_and_bind ` in the underlying library.
41
+
42
+
43
+ .. function :: read_init_file([filename])
44
+
45
+ Execute a readline initialization file. The default filename is the last filename
46
+ used. This calls :c:func: `rl_read_init_file ` in the underlying library.
47
+
48
+
49
+ Line buffer
50
+ -----------
51
+
52
+ The following functions operate on the line buffer:
34
53
35
54
36
55
.. function :: get_line_buffer()
37
56
38
- Return the current contents of the line buffer.
57
+ Return the current contents of the line buffer (:c:data: `rl_line_buffer `
58
+ in the underlying library).
39
59
40
60
41
61
.. function :: insert_text(string)
42
62
43
- Insert text into the command line.
63
+ Insert text into the line buffer at the cursor position. This calls
64
+ :c:func: `rl_insert_text ` in the underlying library, but ignores
65
+ the return value.
44
66
45
67
46
- .. function :: read_init_file([filename] )
68
+ .. function :: redisplay( )
47
69
48
- Parse a readline initialization file. The default filename is the last filename
49
- used.
70
+ Change what's displayed on the screen to reflect the current contents of the
71
+ line buffer. This calls :c:func: `rl_redisplay ` in the underlying library.
72
+
73
+
74
+ History file
75
+ ------------
76
+
77
+ The following functions operate on a history file:
50
78
51
79
52
80
.. function :: read_history_file([filename])
53
81
54
- Load a readline history file. The default filename is :file: `~/.history `.
82
+ Load a readline history file, and append it to the history list.
83
+ The default filename is :file: `~/.history `. This calls
84
+ :c:func: `read_history ` in the underlying library.
55
85
56
86
57
87
.. function :: write_history_file([filename])
58
88
59
- Save a readline history file. The default filename is :file: `~/.history `.
89
+ Save the history list to a readline history file, overwriting any
90
+ existing file. The default filename is :file: `~/.history `. This calls
91
+ :c:func: `write_history ` in the underlying library.
60
92
61
93
62
94
.. function :: append_history_file(nelements[, filename])
63
95
64
- Append the last *nelements * of history to a file. The default filename is
65
- :file: `~/.history `. The file must already exist.
96
+ Append the last *nelements * items of history to a file. The default filename is
97
+ :file: `~/.history `. The file must already exist. This calls
98
+ :c:func: `append_history ` in the underlying library.
66
99
67
100
.. versionadded :: 3.5
68
101
69
102
70
- .. function :: clear_history()
103
+ .. function :: get_history_length()
104
+ set_history_length(length)
71
105
72
- Clear the current history. (Note: this function is not available if the
73
- installed version of GNU readline doesn't support it.)
106
+ Set or return the desired number of lines to save in the history file.
107
+ The :func: `write_history_file ` function uses this value to truncate
108
+ the history file, by calling :c:func: `history_truncate_file ` in
109
+ the underlying library. Negative values imply
110
+ unlimited history file size.
74
111
75
112
76
- .. function :: get_history_length()
113
+ History list
114
+ ------------
77
115
78
- Return the desired length of the history file. Negative values imply unlimited
79
- history file size.
116
+ The following functions operate on a global history list:
80
117
81
118
82
- .. function :: set_history_length(length )
119
+ .. function :: clear_history( )
83
120
84
- Set the number of lines to save in the history file. : func: `write_history_file `
85
- uses this value to truncate the history file when saving. Negative values imply
86
- unlimited history file size .
121
+ Clear the current history. This calls :c: func: `clear_history ` in the
122
+ underlying library. The Python function only exists if Python was
123
+ compiled for a version of the library that supports it .
87
124
88
125
89
126
.. function :: get_current_history_length()
90
127
91
- Return the number of lines currently in the history. (This is different from
128
+ Return the number of items currently in the history. (This is different from
92
129
:func: `get_history_length `, which returns the maximum number of lines that will
93
130
be written to a history file.)
94
131
95
132
96
133
.. function :: get_history_item(index)
97
134
98
- Return the current contents of history item at *index *.
135
+ Return the current contents of history item at *index *. The item index
136
+ is one-based. This calls :c:func: `history_get ` in the underlying library.
99
137
100
138
101
139
.. function :: remove_history_item(pos)
102
140
103
141
Remove history item specified by its position from the history.
142
+ The position is zero-based. This calls :c:func: `remove_history ` in
143
+ the underlying library.
104
144
105
145
106
146
.. function :: replace_history_item(pos, line)
107
147
108
- Replace history item specified by its position with the given line.
148
+ Replace history item specified by its position with *line *.
149
+ The position is zero-based. This calls :c:func: `replace_history_entry `
150
+ in the underlying library.
109
151
110
152
111
- .. function :: redisplay()
153
+ .. function :: add_history(line)
154
+
155
+ Append *line * to the history buffer, as if it was the last line typed.
156
+ This calls :c:func: `add_history ` in the underlying library.
112
157
113
- Change what's displayed on the screen to reflect the current contents of the
114
- line buffer.
158
+
159
+ Startup hooks
160
+ -------------
115
161
116
162
117
163
.. function :: set_startup_hook([function])
118
164
119
- Set or remove the startup_hook function. If *function * is specified, it will be
120
- used as the new startup_hook function; if omitted or ``None ``, any hook function
121
- already installed is removed. The startup_hook function is called with no
165
+ Set or remove the function invoked by the :c:data: `rl_startup_hook `
166
+ callback of the underlying library. If *function * is specified, it will
167
+ be used as the new hook function; if omitted or ``None ``, any function
168
+ already installed is removed. The hook is called with no
122
169
arguments just before readline prints the first prompt.
123
170
124
171
125
172
.. function :: set_pre_input_hook([function])
126
173
127
- Set or remove the pre_input_hook function. If *function * is specified, it will
128
- be used as the new pre_input_hook function; if omitted or ``None ``, any hook
129
- function already installed is removed. The pre_input_hook function is called
174
+ Set or remove the function invoked by the :c:data: `rl_pre_input_hook `
175
+ callback of the underlying library. If *function * is specified, it will
176
+ be used as the new hook function; if omitted or ``None ``, any
177
+ function already installed is removed. The hook is called
130
178
with no arguments after the first prompt has been printed and just before
131
179
readline starts reading input characters.
132
180
133
181
182
+ Completion
183
+ ----------
184
+
185
+ The following functions relate to implementing a custom word completion
186
+ function. This is typically operated by the Tab key, and can suggest and
187
+ automatically complete a word being typed. By default, Readline is set up
188
+ to be used by :mod: `rlcompleter ` to complete Python identifiers for
189
+ the interactive interpreter. If the :mod: `readline ` module is to be used
190
+ with a custom completer, a different set of word delimiters should be set.
191
+
192
+
134
193
.. function :: set_completer([function])
135
194
136
195
Set or remove the completer function. If *function * is specified, it will be
@@ -140,6 +199,12 @@ The :mod:`readline` module defines the following functions:
140
199
returns a non-string value. It should return the next possible completion
141
200
starting with *text *.
142
201
202
+ The installed completer function is invoked by the *entry_func * callback
203
+ passed to :c:func: `rl_completion_matches ` in the underlying library.
204
+ The *text * string comes from the first parameter to the
205
+ :c:data: `rl_attempted_completion_function ` callback of the
206
+ underlying library.
207
+
143
208
144
209
.. function :: get_completer()
145
210
@@ -148,49 +213,41 @@ The :mod:`readline` module defines the following functions:
148
213
149
214
.. function :: get_completion_type()
150
215
151
- Get the type of completion being attempted.
216
+ Get the type of completion being attempted. This returns the
217
+ :c:data: `rl_completion_type ` variable in the underlying library as
218
+ an integer.
152
219
153
220
154
221
.. function :: get_begidx()
222
+ get_endidx()
155
223
156
- Get the beginning index of the readline tab-completion scope.
157
-
158
-
159
- .. function :: get_endidx()
160
-
161
- Get the ending index of the readline tab-completion scope.
224
+ Get the beginning or ending index of the completion scope.
225
+ These indexes are the *start * and *end * arguments passed to the
226
+ :c:data: `rl_attempted_completion_function ` callback of the
227
+ underlying library.
162
228
163
229
164
230
.. function :: set_completer_delims(string)
231
+ get_completer_delims()
165
232
166
- Set the readline word delimiters for tab-completion.
167
-
168
-
169
- .. function :: get_completer_delims()
170
-
171
- Get the readline word delimiters for tab-completion.
233
+ Set or get the word delimiters for completion. These determine the
234
+ start of the word to be considered for completion (the completion scope).
235
+ These functions access the :c:data: `rl_completer_word_break_characters `
236
+ variable in the underlying library.
172
237
173
238
174
239
.. function :: set_completion_display_matches_hook([function])
175
240
176
241
Set or remove the completion display function. If *function * is
177
242
specified, it will be used as the new completion display function;
178
243
if omitted or ``None ``, any completion display function already
179
- installed is removed. The completion display function is called as
244
+ installed is removed. This sets or clears the
245
+ :c:data: `rl_completion_display_matches_hook ` callback in the
246
+ underlying library. The completion display function is called as
180
247
``function(substitution, [matches], longest_match_length) `` once
181
248
each time matches need to be displayed.
182
249
183
250
184
- .. function :: add_history(line)
185
-
186
- Append a line to the history buffer, as if it was the last line typed.
187
-
188
- .. seealso ::
189
-
190
- Module :mod: `rlcompleter `
191
- Completion of Python identifiers at the interactive prompt.
192
-
193
-
194
251
.. _readline-example :
195
252
196
253
Example
0 commit comments