99
1010The :mod: `readline ` module defines a number of functions to facilitate
1111completion 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
1314made using this module affect the behaviour of both the interpreter's
1415interactive prompt and the prompts offered by the built-in :func: `input `
1516function.
1617
1718.. note ::
1819
19- On MacOS X the :mod: ` readline ` module can be implemented using
20+ The underlying Readline library API may be implemented by
2021 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.
2124
2225 The configuration file for ``libedit `` is different from that
2326 of GNU readline. If you programmatically load configuration strings
2427 you can check for the text "libedit" in :const: `readline.__doc__ `
2528 to differentiate between GNU readline and libedit.
2629
2730
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:
2935
3036
3137.. function :: parse_and_bind(string)
3238
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:
3453
3554
3655.. function :: get_line_buffer()
3756
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).
3959
4060
4161.. function :: insert_text(string)
4262
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.
4466
4567
46- .. function :: read_init_file([filename] )
68+ .. function :: redisplay( )
4769
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:
5078
5179
5280.. function :: read_history_file([filename])
5381
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.
5585
5686
5787.. function :: write_history_file([filename])
5888
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.
6092
6193
6294.. function :: append_history_file(nelements[, filename])
6395
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.
6699
67100 .. versionadded :: 3.5
68101
69102
70- .. function :: clear_history()
103+ .. function :: get_history_length()
104+ set_history_length(length)
71105
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.
74111
75112
76- .. function :: get_history_length()
113+ History list
114+ ------------
77115
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:
80117
81118
82- .. function :: set_history_length(length )
119+ .. function :: clear_history( )
83120
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 .
87124
88125
89126.. function :: get_current_history_length()
90127
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
92129 :func: `get_history_length `, which returns the maximum number of lines that will
93130 be written to a history file.)
94131
95132
96133.. function :: get_history_item(index)
97134
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.
99137
100138
101139.. function :: remove_history_item(pos)
102140
103141 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.
104144
105145
106146.. function :: replace_history_item(pos, line)
107147
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.
109151
110152
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.
112157
113- Change what's displayed on the screen to reflect the current contents of the
114- line buffer.
158+
159+ Startup hooks
160+ -------------
115161
116162
117163.. function :: set_startup_hook([function])
118164
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
122169 arguments just before readline prints the first prompt.
123170
124171
125172.. function :: set_pre_input_hook([function])
126173
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
130178 with no arguments after the first prompt has been printed and just before
131179 readline starts reading input characters.
132180
133181
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+
134193.. function :: set_completer([function])
135194
136195 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:
140199 returns a non-string value. It should return the next possible completion
141200 starting with *text *.
142201
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+
143208
144209.. function :: get_completer()
145210
@@ -148,49 +213,41 @@ The :mod:`readline` module defines the following functions:
148213
149214.. function :: get_completion_type()
150215
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.
152219
153220
154221.. function :: get_begidx()
222+ get_endidx()
155223
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.
162228
163229
164230.. function :: set_completer_delims(string)
231+ get_completer_delims()
165232
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.
172237
173238
174239.. function :: set_completion_display_matches_hook([function])
175240
176241 Set or remove the completion display function. If *function * is
177242 specified, it will be used as the new completion display function;
178243 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
180247 ``function(substitution, [matches], longest_match_length) `` once
181248 each time matches need to be displayed.
182249
183250
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-
194251.. _readline-example :
195252
196253Example
0 commit comments